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 Date: Tue, 15 Mar 2011 15:11:32 +0100 From: Christoph Hellwig Message-ID: <20110315141132.GB30710@lst.de> References: <20110315141049.GA30627@lst.de> MIME-Version: 1.0 Content-Type: text/plain; charset=us-ascii Content-Disposition: inline In-Reply-To: <20110315141049.GA30627@lst.de> Subject: [Qemu-devel] [PATCH 2/4] block: add a helper to change writeback mode on the fly List-Id: qemu-devel.nongnu.org List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , To: qemu-devel@nongnu.org Cc: kwolf@redhat.com, stefanha@gmail.com, aliguori@us.ibm.com, prerna@linux.vnet.ibm.com 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);