From patchwork Thu Nov 17 13:40:28 2011 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Stefan Hajnoczi X-Patchwork-Id: 126235 Return-Path: X-Original-To: incoming@patchwork.ozlabs.org Delivered-To: patchwork-incoming@bilbo.ozlabs.org Received: from lists.gnu.org (lists.gnu.org [140.186.70.17]) (using TLSv1 with cipher AES256-SHA (256/256 bits)) (Client did not present a certificate) by ozlabs.org (Postfix) with ESMTPS id F092AB71DC for ; Fri, 18 Nov 2011 01:32:38 +1100 (EST) Received: from localhost ([::1]:54176 helo=lists.gnu.org) by lists.gnu.org with esmtp (Exim 4.71) (envelope-from ) id 1RR2Da-00047H-JE for incoming@patchwork.ozlabs.org; Thu, 17 Nov 2011 08:41:30 -0500 Received: from eggs.gnu.org ([140.186.70.92]:49240) by lists.gnu.org with esmtp (Exim 4.71) (envelope-from ) id 1RR2Cu-0002ax-Fi for qemu-devel@nongnu.org; Thu, 17 Nov 2011 08:40:59 -0500 Received: from Debian-exim by eggs.gnu.org with spam-scanned (Exim 4.71) (envelope-from ) id 1RR2Cn-0000vo-3t for qemu-devel@nongnu.org; Thu, 17 Nov 2011 08:40:48 -0500 Received: from mtagate7.uk.ibm.com ([194.196.100.167]:49658) by eggs.gnu.org with esmtp (Exim 4.71) (envelope-from ) id 1RR2Cm-0000uN-3H for qemu-devel@nongnu.org; Thu, 17 Nov 2011 08:40:41 -0500 Received: from d06nrmr1707.portsmouth.uk.ibm.com (d06nrmr1707.portsmouth.uk.ibm.com [9.149.39.225]) by mtagate7.uk.ibm.com (8.13.1/8.13.1) with ESMTP id pAHDeccw006100 for ; Thu, 17 Nov 2011 13:40:38 GMT Received: from d06av01.portsmouth.uk.ibm.com (d06av01.portsmouth.uk.ibm.com [9.149.37.212]) by d06nrmr1707.portsmouth.uk.ibm.com (8.13.8/8.13.8/NCO v10.0) with ESMTP id pAHDec1K2486446 for ; Thu, 17 Nov 2011 13:40:38 GMT Received: from d06av01.portsmouth.uk.ibm.com (loopback [127.0.0.1]) by d06av01.portsmouth.uk.ibm.com (8.14.4/8.13.1/NCO v10.0 AVout) with ESMTP id pAHDecnY025080 for ; Thu, 17 Nov 2011 06:40:38 -0700 Received: from localhost (stefanha-thinkpad.manchester-maybrook.uk.ibm.com [9.174.219.31]) by d06av01.portsmouth.uk.ibm.com (8.14.4/8.13.1/NCO v10.0 AVin) with ESMTP id pAHDebc7024834; Thu, 17 Nov 2011 06:40:38 -0700 From: Stefan Hajnoczi To: Date: Thu, 17 Nov 2011 13:40:28 +0000 Message-Id: <1321537232-799-5-git-send-email-stefanha@linux.vnet.ibm.com> X-Mailer: git-send-email 1.7.7.1 In-Reply-To: <1321537232-799-1-git-send-email-stefanha@linux.vnet.ibm.com> References: <1321537232-799-1-git-send-email-stefanha@linux.vnet.ibm.com> X-detected-operating-system: by eggs.gnu.org: GNU/Linux 2.6, seldom 2.4 (older, 4) X-Received-From: 194.196.100.167 Cc: Kevin Wolf , Paolo Bonzini , Marcelo Tosatti , Stefan Hajnoczi Subject: [Qemu-devel] [PATCH v2 4/8] block: add bdrv_set_copy_on_read() X-BeenThere: qemu-devel@nongnu.org X-Mailman-Version: 2.1.14 Precedence: list List-Id: List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Errors-To: qemu-devel-bounces+incoming=patchwork.ozlabs.org@nongnu.org Sender: qemu-devel-bounces+incoming=patchwork.ozlabs.org@nongnu.org The bdrv_set_copy_on_read() function can be used to programmatically enable or disable copy-on-read for a block device. Later patches add the actual copy-on-read logic. Signed-off-by: Stefan Hajnoczi --- block.c | 22 ++++++++++++++++++++++ block.h | 3 +++ block_int.h | 2 ++ 3 files changed, 27 insertions(+), 0 deletions(-) diff --git a/block.c b/block.c index 27c4e84..c90880b 100644 --- a/block.c +++ b/block.c @@ -538,6 +538,22 @@ int bdrv_parse_cache_flags(const char *mode, int *flags) return 0; } +/** + * Enable/disable copy-on-read + * + * This is based on a reference count so multiple users may call this function + * without worrying about clobbering the previous state. Copy-on-read stays + * enabled until all users have called to disable it. + */ +void bdrv_set_copy_on_read(BlockDriverState *bs, bool enable) +{ + if (enable) { + bs->copy_on_read++; + } else { + bs->copy_on_read--; + } +} + /* * Common part for opening disk images and files */ @@ -559,6 +575,11 @@ static int bdrv_open_common(BlockDriverState *bs, const char *filename, bs->growable = 0; bs->buffer_alignment = 512; + assert(bs->copy_on_read == 0); /* bdrv_new() and bdrv_close() make it so */ + if (flags & BDRV_O_RDWR) { + bdrv_set_copy_on_read(bs, !!(flags & BDRV_O_COPY_ON_READ)); + } + pstrcpy(bs->filename, sizeof(bs->filename), filename); bs->backing_file[0] = '\0'; @@ -801,6 +822,7 @@ void bdrv_close(BlockDriverState *bs) #endif bs->opaque = NULL; bs->drv = NULL; + bs->copy_on_read = 0; if (bs->file != NULL) { bdrv_close(bs->file); diff --git a/block.h b/block.h index ad8dd48..68b4b14 100644 --- a/block.h +++ b/block.h @@ -70,6 +70,7 @@ typedef struct BlockDevOps { #define BDRV_O_NATIVE_AIO 0x0080 /* use native AIO instead of the thread pool */ #define BDRV_O_NO_BACKING 0x0100 /* don't open the backing file */ #define BDRV_O_NO_FLUSH 0x0200 /* disable flushing on this disk */ +#define BDRV_O_COPY_ON_READ 0x0400 /* copy read backing sectors into image */ #define BDRV_O_CACHE_MASK (BDRV_O_NOCACHE | BDRV_O_CACHE_WB | BDRV_O_NO_FLUSH) @@ -308,6 +309,8 @@ void bdrv_reset_dirty(BlockDriverState *bs, int64_t cur_sector, int nr_sectors); int64_t bdrv_get_dirty_count(BlockDriverState *bs); +void bdrv_set_copy_on_read(BlockDriverState *bs, bool enable); + void bdrv_set_in_use(BlockDriverState *bs, int in_use); int bdrv_in_use(BlockDriverState *bs); diff --git a/block_int.h b/block_int.h index 788fde9..3c5bacb 100644 --- a/block_int.h +++ b/block_int.h @@ -193,6 +193,8 @@ struct BlockDriverState { int encrypted; /* if true, the media is encrypted */ int valid_key; /* if true, a valid encryption key has been set */ int sg; /* if true, the device is a /dev/sg* */ + int copy_on_read; /* if true, copy read backing sectors into image + note this is a reference count */ BlockDriver *drv; /* NULL means no media */ void *opaque;