From patchwork Fri Apr 19 00:57:10 2013 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Wayne Xia X-Patchwork-Id: 237781 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 309332C01E5 for ; Fri, 19 Apr 2013 11:01:00 +1000 (EST) Received: from localhost ([::1]:52556 helo=lists.gnu.org) by lists.gnu.org with esmtp (Exim 4.71) (envelope-from ) id 1USzhC-0000I9-Dl for incoming@patchwork.ozlabs.org; Thu, 18 Apr 2013 21:00:58 -0400 Received: from eggs.gnu.org ([208.118.235.92]:43858) by lists.gnu.org with esmtp (Exim 4.71) (envelope-from ) id 1USzfe-0006TV-3W for qemu-devel@nongnu.org; Thu, 18 Apr 2013 20:59:24 -0400 Received: from Debian-exim by eggs.gnu.org with spam-scanned (Exim 4.71) (envelope-from ) id 1USzfc-0006wv-Rh for qemu-devel@nongnu.org; Thu, 18 Apr 2013 20:59:22 -0400 Received: from e28smtp04.in.ibm.com ([122.248.162.4]:53115) by eggs.gnu.org with esmtp (Exim 4.71) (envelope-from ) id 1USzfc-0006wX-2r for qemu-devel@nongnu.org; Thu, 18 Apr 2013 20:59:20 -0400 Received: from /spool/local by e28smtp04.in.ibm.com with IBM ESMTP SMTP Gateway: Authorized Use Only! Violators will be prosecuted for from ; Fri, 19 Apr 2013 06:25:02 +0530 Received: from d28dlp02.in.ibm.com (9.184.220.127) by e28smtp04.in.ibm.com (192.168.1.134) with IBM ESMTP SMTP Gateway: Authorized Use Only! Violators will be prosecuted; Fri, 19 Apr 2013 06:25:00 +0530 Received: from d28relay02.in.ibm.com (d28relay02.in.ibm.com [9.184.220.59]) by d28dlp02.in.ibm.com (Postfix) with ESMTP id 41C2A3940023 for ; Fri, 19 Apr 2013 06:29:13 +0530 (IST) Received: from d28av03.in.ibm.com (d28av03.in.ibm.com [9.184.220.65]) by d28relay02.in.ibm.com (8.13.8/8.13.8/NCO v10.0) with ESMTP id r3J0x9o65308682 for ; Fri, 19 Apr 2013 06:29:09 +0530 Received: from d28av03.in.ibm.com (loopback [127.0.0.1]) by d28av03.in.ibm.com (8.14.4/8.13.1/NCO v10.0 AVout) with ESMTP id r3J0xCYv018281 for ; Fri, 19 Apr 2013 10:59:12 +1000 Received: from RH63Wenchao ([9.77.178.27]) by d28av03.in.ibm.com (8.14.4/8.13.1/NCO v10.0 AVin) with ESMTP id r3J0vQaa014233; Fri, 19 Apr 2013 10:59:10 +1000 From: Wenchao Xia To: qemu-devel@nongnu.org Date: Fri, 19 Apr 2013 08:57:10 +0800 Message-Id: <1366333030-8564-6-git-send-email-xiawenc@linux.vnet.ibm.com> X-Mailer: git-send-email 1.7.1 In-Reply-To: <1366333030-8564-1-git-send-email-xiawenc@linux.vnet.ibm.com> References: <1366333030-8564-1-git-send-email-xiawenc@linux.vnet.ibm.com> X-TM-AS-MML: No X-Content-Scanned: Fidelis XPS MAILER x-cbid: 13041900-5564-0000-0000-0000079077FB X-detected-operating-system: by eggs.gnu.org: GNU/Linux 2.4.x-2.6.x [generic] X-Received-From: 122.248.162.4 Cc: kwolf@redhat.com, stefanha@gmail.com, dietmar@proxmox.com, pbonzini@redhat.com, Wenchao Xia Subject: [Qemu-devel] [PATCH V3 5/5] block: make all steps in qmp_transaction() as callback 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 Make it easier to add other operations to qmp_transaction() by using callbacks, with external snapshots serving as an example implementation of the callbacks. Signed-off-by: Wenchao Xia --- blockdev.c | 84 +++++++++++++++++++++++++++++++++++++++++++++++------------ 1 files changed, 67 insertions(+), 17 deletions(-) diff --git a/blockdev.c b/blockdev.c index 051be98..b336794 100644 --- a/blockdev.c +++ b/blockdev.c @@ -779,14 +779,41 @@ void qmp_blockdev_snapshot_sync(const char *device, const char *snapshot_file, /* New and old BlockDriverState structs for group snapshots */ -typedef struct BlkTransactionStates { + +typedef struct BlkTransactionStates BlkTransactionStates; + +/* Only prepare() may fail. In a single transaction, only one of commit() or + rollback() will be called. */ +typedef struct BdrvActionOps { + /* Prepare the work, must NOT be NULL. */ + void (*prepare)(BlkTransactionStates *common, Error **errp); + /* Commit the changes, must NOT be NULL. */ + void (*commit)(BlkTransactionStates *common); + /* Rollback the changes on fail, can be NULL. */ + void (*rollback)(BlkTransactionStates *common); + /* Clean up resource in the end, can be NULL. */ + void (*clean)(BlkTransactionStates *common); +} BdrvActionOps; + +/* + * This structure must be arranged as first member in parent type, assuming + * that compiler will also arrange it to the same address with parent instance. + * Later it will be used in free(). + */ +struct BlkTransactionStates { + BlockdevAction *action; + const BdrvActionOps *ops; + QSIMPLEQ_ENTRY(BlkTransactionStates) entry; +}; + +/* external snapshot private data */ +typedef struct ExternalSnapshotStates { + BlkTransactionStates common; BlockDriverState *old_bs; BlockDriverState *new_bs; - QSIMPLEQ_ENTRY(BlkTransactionStates) entry; -} BlkTransactionStates; +} ExternalSnapshotStates; -static void external_snapshot_prepare(BlockdevAction *action, - BlkTransactionStates *states, +static void external_snapshot_prepare(BlkTransactionStates *common, Error **errp) { BlockDriver *proto_drv; @@ -797,6 +824,9 @@ static void external_snapshot_prepare(BlockdevAction *action, const char *new_image_file; const char *format = "qcow2"; enum NewImageMode mode = NEW_IMAGE_MODE_ABSOLUTE_PATHS; + ExternalSnapshotStates *states = + DO_UPCAST(ExternalSnapshotStates, common, common); + BlockdevAction *action = common->action; /* get parameters */ g_assert(action->kind == BLOCKDEV_ACTION_KIND_BLOCKDEV_SNAPSHOT_SYNC); @@ -871,8 +901,11 @@ static void external_snapshot_prepare(BlockdevAction *action, } } -static void external_snapshot_commit(BlkTransactionStates *states) +static void external_snapshot_commit(BlkTransactionStates *common) { + ExternalSnapshotStates *states = + DO_UPCAST(ExternalSnapshotStates, common, common); + /* This removes our old bs from the bdrv_states, and adds the new bs */ bdrv_append(states->new_bs, states->old_bs); /* We don't need (or want) to use the transactional @@ -882,13 +915,21 @@ static void external_snapshot_commit(BlkTransactionStates *states) NULL); } -static void external_snapshot_rollback(BlkTransactionStates *states) +static void external_snapshot_rollback(BlkTransactionStates *common) { + ExternalSnapshotStates *states = + DO_UPCAST(ExternalSnapshotStates, common, common); if (states->new_bs) { bdrv_delete(states->new_bs); } } +const BdrvActionOps external_snapshot_ops = { + .prepare = external_snapshot_prepare, + .commit = external_snapshot_commit, + .rollback = external_snapshot_rollback, +}; + /* * 'Atomic' group snapshots. The snapshots are taken as a set, and if any fail * then we do not pivot any of the devices in the group, and abandon the @@ -909,32 +950,36 @@ void qmp_transaction(BlockdevActionList *dev_list, Error **errp) /* We don't do anything in this loop that commits us to the snapshot */ while (NULL != dev_entry) { BlockdevAction *dev_info = NULL; + ExternalSnapshotStates *ext; dev_info = dev_entry->value; dev_entry = dev_entry->next; - states = g_malloc0(sizeof(BlkTransactionStates)); - QSIMPLEQ_INSERT_TAIL(&snap_bdrv_states, states, entry); - switch (dev_info->kind) { case BLOCKDEV_ACTION_KIND_BLOCKDEV_SNAPSHOT_SYNC: - external_snapshot_prepare(dev_info, states, errp); - if (error_is_set(&local_err)) { - error_propagate(errp, local_err); - goto delete_and_fail; - } + ext = g_malloc0(sizeof(ExternalSnapshotStates)); + states = &ext->common; + states->ops = &external_snapshot_ops; break; default: abort(); } + states->action = dev_info; + QSIMPLEQ_INSERT_TAIL(&snap_bdrv_states, states, entry); + + states->ops->prepare(states, &local_err); + if (error_is_set(&local_err)) { + error_propagate(errp, local_err); + goto delete_and_fail; + } } /* Now we are going to do the actual pivot. Everything up to this point * is reversible, but we are committed at this point */ QSIMPLEQ_FOREACH(states, &snap_bdrv_states, entry) { - external_snapshot_commit(states); + states->ops->commit(states); } /* success */ @@ -946,10 +991,15 @@ delete_and_fail: * the original bs for all images */ QSIMPLEQ_FOREACH(states, &snap_bdrv_states, entry) { - external_snapshot_rollback(states); + if (states->ops->rollback) { + states->ops->rollback(states); + } } exit: QSIMPLEQ_FOREACH_SAFE(states, &snap_bdrv_states, entry, next) { + if (states->ops->clean) { + states->ops->clean(states); + } g_free(states); } }