From patchwork Tue Mar 15 14:11:32 2011 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Christoph Hellwig X-Patchwork-Id: 86986 Return-Path: X-Original-To: incoming@patchwork.ozlabs.org Delivered-To: patchwork-incoming@bilbo.ozlabs.org Received: from lists.gnu.org (lists.gnu.org [199.232.76.165]) (using TLSv1 with cipher DHE-RSA-AES256-SHA (256/256 bits)) (Client did not present a certificate) by ozlabs.org (Postfix) with ESMTPS id 9F4DFB6FAC for ; Wed, 16 Mar 2011 01:13:27 +1100 (EST) Received: from localhost ([127.0.0.1]:38507 helo=lists.gnu.org) by lists.gnu.org with esmtp (Exim 4.43) id 1PzV00-0004wk-NA for incoming@patchwork.ozlabs.org; Tue, 15 Mar 2011 10:13:24 -0400 Received: from [140.186.70.92] (port=60476 helo=eggs.gnu.org) by lists.gnu.org with esmtp (Exim 4.43) id 1PzUyE-0004WI-Jh for qemu-devel@nongnu.org; Tue, 15 Mar 2011 10:11:37 -0400 Received: from Debian-exim by eggs.gnu.org with spam-scanned (Exim 4.71) (envelope-from ) id 1PzUyD-0002cd-BW for qemu-devel@nongnu.org; Tue, 15 Mar 2011 10:11:34 -0400 Received: from verein.lst.de ([213.95.11.211]:38239 helo=newverein.lst.de) by eggs.gnu.org with esmtp (Exim 4.71) (envelope-from ) id 1PzUyD-0002cU-6Z for qemu-devel@nongnu.org; Tue, 15 Mar 2011 10:11:33 -0400 Received: by newverein.lst.de (Postfix, from userid 2407) id CFD2013F9C; Tue, 15 Mar 2011 15:11:32 +0100 (CET) Date: Tue, 15 Mar 2011 15:11:32 +0100 From: Christoph Hellwig To: qemu-devel@nongnu.org Message-ID: <20110315141132.GB30710@lst.de> References: <20110315141049.GA30627@lst.de> MIME-Version: 1.0 Content-Disposition: inline In-Reply-To: <20110315141049.GA30627@lst.de> User-Agent: Mutt/1.5.17 (2007-11-01) X-detected-operating-system: by eggs.gnu.org: GNU/Linux 2.6 (newer, 3) X-Received-From: 213.95.11.211 Cc: kwolf@redhat.com, stefanha@gmail.com, aliguori@us.ibm.com, prerna@linux.vnet.ibm.com Subject: [Qemu-devel] [PATCH 2/4] block: add a helper to change writeback mode on the fly X-BeenThere: qemu-devel@nongnu.org X-Mailman-Version: 2.1.5 Precedence: list List-Id: qemu-devel.nongnu.org List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Sender: qemu-devel-bounces+incoming=patchwork.ozlabs.org@nongnu.org Errors-To: qemu-devel-bounces+incoming=patchwork.ozlabs.org@nongnu.org Add a new bdrv_change_cache that can set/clear the writeback flag at runtime by stopping all I/O and closing/reopening the image file. All code is based on a patch from Prerna Saxena with minimal refactoring. Signed-off-by: Christoph Hellwig Index: qemu/block.c =================================================================== --- qemu.orig/block.c 2011-03-15 11:47:31.285634626 +0100 +++ qemu/block.c 2011-03-15 14:57:03.680633093 +0100 @@ -441,6 +441,8 @@ static int bdrv_open_common(BlockDriverS if (flags & BDRV_O_CACHE_WB) bs->enable_write_cache = 1; + else + bs->enable_write_cache = 0; /* * Clear flags that are internal to the block layer before opening the @@ -651,6 +653,44 @@ unlink_and_fail: return ret; } +static int bdrv_reopen(BlockDriverState *bs, int bdrv_flags) +{ + BlockDriver *drv = bs->drv; + int ret; + + if (bdrv_flags == bs->open_flags) { + return 0; + } + + /* Quiesce IO for the given block device */ + qemu_aio_flush(); + bdrv_flush(bs); + + bdrv_close(bs); + ret = bdrv_open(bs, bs->filename, bdrv_flags, drv); + + /* + * A failed attempt to reopen the image file must lead to 'abort()' + */ + if (ret != 0) { + abort(); + } + + return ret; +} + +int bdrv_change_cache(BlockDriverState *bs, bool enable) +{ + int bdrv_flags = 0; + + bdrv_flags = bs->open_flags & ~BDRV_O_CACHE_WB; + if (enable) { + bdrv_flags |= BDRV_O_CACHE_WB; + } + + return bdrv_reopen(bs, bdrv_flags); +} + void bdrv_close(BlockDriverState *bs) { if (bs->drv) { Index: qemu/block.h =================================================================== --- qemu.orig/block.h 2011-03-15 11:47:18.664136441 +0100 +++ qemu/block.h 2011-03-15 11:47:31.813634525 +0100 @@ -87,6 +87,7 @@ int bdrv_pwrite_sync(BlockDriverState *b int bdrv_write_sync(BlockDriverState *bs, int64_t sector_num, const uint8_t *buf, int nb_sectors); int bdrv_truncate(BlockDriverState *bs, int64_t offset); +int bdrv_change_cache(BlockDriverState *bs, bool enable); int64_t bdrv_getlength(BlockDriverState *bs); void bdrv_get_geometry(BlockDriverState *bs, uint64_t *nb_sectors_ptr); void bdrv_guess_geometry(BlockDriverState *bs, int *pcyls, int *pheads, int *psecs);