From patchwork Fri May 24 14:32:21 2013 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Stefan Hajnoczi X-Patchwork-Id: 246174 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 5072C2C0091 for ; Sat, 25 May 2013 00:35:07 +1000 (EST) Received: from localhost ([::1]:55206 helo=lists.gnu.org) by lists.gnu.org with esmtp (Exim 4.71) (envelope-from ) id 1Uft5F-0002XT-Fo for incoming@patchwork.ozlabs.org; Fri, 24 May 2013 10:35:05 -0400 Received: from eggs.gnu.org ([208.118.235.92]:54606) by lists.gnu.org with esmtp (Exim 4.71) (envelope-from ) id 1Uft34-0007az-QL for qemu-devel@nongnu.org; Fri, 24 May 2013 10:33:02 -0400 Received: from Debian-exim by eggs.gnu.org with spam-scanned (Exim 4.71) (envelope-from ) id 1Uft30-0003um-2c for qemu-devel@nongnu.org; Fri, 24 May 2013 10:32:50 -0400 Received: from mx1.redhat.com ([209.132.183.28]:18119) by eggs.gnu.org with esmtp (Exim 4.71) (envelope-from ) id 1Uft2z-0003ud-Mz for qemu-devel@nongnu.org; Fri, 24 May 2013 10:32:45 -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 r4OEWhDi020214 (version=TLSv1/SSLv3 cipher=DHE-RSA-AES256-SHA bits=256 verify=OK); Fri, 24 May 2013 10:32:43 -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 r4OEWgxT010509; Fri, 24 May 2013 10:32:42 -0400 From: Stefan Hajnoczi To: Date: Fri, 24 May 2013 16:32:21 +0200 Message-Id: <1369405947-14818-6-git-send-email-stefanha@redhat.com> In-Reply-To: <1369405947-14818-1-git-send-email-stefanha@redhat.com> References: <1369405947-14818-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 , Anthony Liguori , Wenchao Xia Subject: [Qemu-devel] [PATCH 05/11] 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 From: Wenchao Xia 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 Reviewed-by: Kevin Wolf Reviewed-by: Stefan Hajnoczi Reviewed-by: Eric Blake Signed-off-by: Kevin Wolf --- blockdev.c | 95 ++++++++++++++++++++++++++++++++++++++++++++++---------------- 1 file changed, 71 insertions(+), 24 deletions(-) diff --git a/blockdev.c b/blockdev.c index 2131e13..617501c 100644 --- a/blockdev.c +++ b/blockdev.c @@ -779,14 +779,43 @@ 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 + abort() will be called, clean() will always be called if it present. */ +typedef struct BdrvActionOps { + /* Size of state struct, in bytes. */ + size_t instance_size; + /* Prepare the work, must NOT be NULL. */ + void (*prepare)(BlkTransactionStates *common, Error **errp); + /* Commit the changes, must NOT be NULL. */ + void (*commit)(BlkTransactionStates *common); + /* Abort the changes on fail, can be NULL. */ + void (*abort)(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 child 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 +826,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 +903,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 +917,24 @@ static void external_snapshot_commit(BlkTransactionStates *states) NULL); } -static void external_snapshot_abort(BlkTransactionStates *states) +static void external_snapshot_abort(BlkTransactionStates *common) { + ExternalSnapshotStates *states = + DO_UPCAST(ExternalSnapshotStates, common, common); if (states->new_bs) { bdrv_delete(states->new_bs); } } +static const BdrvActionOps actions[] = { + [BLOCKDEV_ACTION_KIND_BLOCKDEV_SNAPSHOT_SYNC] = { + .instance_size = sizeof(ExternalSnapshotStates), + .prepare = external_snapshot_prepare, + .commit = external_snapshot_commit, + .abort = external_snapshot_abort, + }, +}; + /* * '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 +955,28 @@ 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; + const BdrvActionOps *ops; dev_info = dev_entry->value; dev_entry = dev_entry->next; - states = g_malloc0(sizeof(BlkTransactionStates)); + assert(dev_info->kind < ARRAY_SIZE(actions)); + + ops = &actions[dev_info->kind]; + states = g_malloc0(ops->instance_size); + states->ops = ops; + states->action = dev_info; 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; - } - break; - default: - abort(); + 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 +988,15 @@ delete_and_fail: * the original bs for all images */ QSIMPLEQ_FOREACH(states, &snap_bdrv_states, entry) { - external_snapshot_abort(states); + if (states->ops->abort) { + states->ops->abort(states); + } } exit: QSIMPLEQ_FOREACH_SAFE(states, &snap_bdrv_states, entry, next) { + if (states->ops->clean) { + states->ops->clean(states); + } g_free(states); } }