From patchwork Thu May 30 12:34:50 2013 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Stefan Hajnoczi X-Patchwork-Id: 247560 Return-Path: X-Original-To: incoming@patchwork.ozlabs.org Delivered-To: patchwork-incoming@bilbo.ozlabs.org Received: from lists.gnu.org (lists.gnu.org [208.118.235.17]) (using TLSv1 with cipher AES256-SHA (256/256 bits)) (Client did not present a certificate) by ozlabs.org (Postfix) with ESMTPS id F31672C0087 for ; Thu, 30 May 2013 22:43:41 +1000 (EST) Received: from localhost ([::1]:43323 helo=lists.gnu.org) by lists.gnu.org with esmtp (Exim 4.71) (envelope-from ) id 1Ui2Cg-0000ZF-Cr for incoming@patchwork.ozlabs.org; Thu, 30 May 2013 08:43:38 -0400 Received: from eggs.gnu.org ([208.118.235.92]:44537) by lists.gnu.org with esmtp (Exim 4.71) (envelope-from ) id 1Ui2CG-0000Nh-Jz for qemu-devel@nongnu.org; Thu, 30 May 2013 08:43:19 -0400 Received: from Debian-exim by eggs.gnu.org with spam-scanned (Exim 4.71) (envelope-from ) id 1Ui2C8-0001iu-O8 for qemu-devel@nongnu.org; Thu, 30 May 2013 08:43:12 -0400 Received: from mx1.redhat.com ([209.132.183.28]:57620) by eggs.gnu.org with esmtp (Exim 4.71) (envelope-from ) id 1Ui24Z-0005wq-9t for qemu-devel@nongnu.org; Thu, 30 May 2013 08:35:15 -0400 Received: from int-mx11.intmail.prod.int.phx2.redhat.com (int-mx11.intmail.prod.int.phx2.redhat.com [10.5.11.24]) by mx1.redhat.com (8.14.4/8.14.4) with ESMTP id r4UCZDLS004111 (version=TLSv1/SSLv3 cipher=DHE-RSA-AES256-SHA bits=256 verify=OK); Thu, 30 May 2013 08:35:13 -0400 Received: from localhost (ovpn-112-44.ams2.redhat.com [10.36.112.44]) by int-mx11.intmail.prod.int.phx2.redhat.com (8.14.4/8.14.4) with ESMTP id r4UCZBus004107; Thu, 30 May 2013 08:35:12 -0400 From: Stefan Hajnoczi To: Date: Thu, 30 May 2013 14:34:50 +0200 Message-Id: <1369917299-5725-3-git-send-email-stefanha@redhat.com> In-Reply-To: <1369917299-5725-1-git-send-email-stefanha@redhat.com> References: <1369917299-5725-1-git-send-email-stefanha@redhat.com> X-Scanned-By: MIMEDefang 2.68 on 10.5.11.24 X-detected-operating-system: by eggs.gnu.org: GNU/Linux 3.x X-Received-From: 209.132.183.28 Cc: Kevin Wolf , Fam Zheng , xiawenc@linux.vnet.ibm.com, imain@redhat.com, Stefan Hajnoczi , Paolo Bonzini , dietmar@proxmox.com Subject: [Qemu-devel] [PATCH v5 02/11] block: add bdrv_add_before_write_notifier() 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_add_before_write_notifier() function installs a callback that is invoked before a write request is processed. This will be used to implement copy-on-write point-in-time snapshots where we need to copy out old data before overwriting it. Note that BdrvTrackedRequest is moved to block_int.h since it is passed to .notify() functions. Signed-off-by: Stefan Hajnoczi Reviewed-by: Eric Blake Reviewed-by: Kevin Wolf --- block.c | 23 ++++++++++++----------- include/block/block_int.h | 23 ++++++++++++++++++++++- 2 files changed, 34 insertions(+), 12 deletions(-) diff --git a/block.c b/block.c index 65c0b60..9e43f20 100644 --- a/block.c +++ b/block.c @@ -308,6 +308,7 @@ BlockDriverState *bdrv_new(const char *device_name) } bdrv_iostatus_disable(bs); notifier_list_init(&bs->close_notifiers); + notifier_with_return_list_init(&bs->before_write_notifiers); return bs; } @@ -1850,16 +1851,6 @@ int bdrv_commit_all(void) return 0; } -struct BdrvTrackedRequest { - BlockDriverState *bs; - int64_t sector_num; - int nb_sectors; - bool is_write; - QLIST_ENTRY(BdrvTrackedRequest) list; - Coroutine *co; /* owner, used for deadlock detection */ - CoQueue wait_queue; /* coroutines blocked on this request */ -}; - /** * Remove an active request from the tracked requests list * @@ -2630,7 +2621,11 @@ static int coroutine_fn bdrv_co_do_writev(BlockDriverState *bs, tracked_request_begin(&req, bs, sector_num, nb_sectors, true); - if (flags & BDRV_REQ_ZERO_WRITE) { + ret = notifier_with_return_list_notify(&bs->before_write_notifiers, &req); + + if (ret < 0) { + /* Do nothing, write notifier decided to fail this request */ + } else if (flags & BDRV_REQ_ZERO_WRITE) { ret = bdrv_co_do_write_zeroes(bs, sector_num, nb_sectors); } else { ret = drv->bdrv_co_writev(bs, sector_num, nb_sectors, qiov); @@ -4894,3 +4889,9 @@ AioContext *bdrv_get_aio_context(BlockDriverState *bs) /* Currently BlockDriverState always uses the main loop AioContext */ return qemu_get_aio_context(); } + +void bdrv_add_before_write_notifier(BlockDriverState *bs, + NotifierWithReturn *notifier) +{ + notifier_with_return_list_add(&bs->before_write_notifiers, notifier); +} diff --git a/include/block/block_int.h b/include/block/block_int.h index 6078dd3..440d510 100644 --- a/include/block/block_int.h +++ b/include/block/block_int.h @@ -58,7 +58,16 @@ #define BLOCK_OPT_LAZY_REFCOUNTS "lazy_refcounts" #define BLOCK_OPT_ADAPTER_TYPE "adapter_type" -typedef struct BdrvTrackedRequest BdrvTrackedRequest; +typedef struct BdrvTrackedRequest { + BlockDriverState *bs; + int64_t sector_num; + int nb_sectors; + bool is_write; + QLIST_ENTRY(BdrvTrackedRequest) list; + Coroutine *co; /* owner, used for deadlock detection */ + CoQueue wait_queue; /* coroutines blocked on this request */ +} BdrvTrackedRequest; + typedef struct BlockIOLimit { int64_t bps[3]; @@ -247,6 +256,9 @@ struct BlockDriverState { NotifierList close_notifiers; + /* Callback before write request is processed */ + NotifierWithReturnList before_write_notifiers; + /* number of in-flight copy-on-read requests */ unsigned int copy_on_read_in_flight; @@ -298,6 +310,15 @@ void bdrv_set_io_limits(BlockDriverState *bs, BlockIOLimit *io_limits); /** + * bdrv_add_before_write_notifier: + * + * Register a callback that is invoked before write requests are processed but + * after any throttling or waiting for overlapping requests. + */ +void bdrv_add_before_write_notifier(BlockDriverState *bs, + NotifierWithReturn *notifier); + +/** * bdrv_get_aio_context: * * Returns: the currently bound #AioContext