From patchwork Mon Apr 1 10:01:30 2013 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Wayne Xia X-Patchwork-Id: 232720 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 75AE72C00CE for ; Mon, 1 Apr 2013 21:04:47 +1100 (EST) Received: from localhost ([::1]:60674 helo=lists.gnu.org) by lists.gnu.org with esmtp (Exim 4.71) (envelope-from ) id 1UMbbZ-0002b3-Kv for incoming@patchwork.ozlabs.org; Mon, 01 Apr 2013 06:04:45 -0400 Received: from eggs.gnu.org ([208.118.235.92]:59465) by lists.gnu.org with esmtp (Exim 4.71) (envelope-from ) id 1UMbav-0002TF-Fg for qemu-devel@nongnu.org; Mon, 01 Apr 2013 06:04:09 -0400 Received: from Debian-exim by eggs.gnu.org with spam-scanned (Exim 4.71) (envelope-from ) id 1UMbaq-0002ZM-PC for qemu-devel@nongnu.org; Mon, 01 Apr 2013 06:04:05 -0400 Received: from e28smtp07.in.ibm.com ([122.248.162.7]:35974) by eggs.gnu.org with esmtp (Exim 4.71) (envelope-from ) id 1UMbap-0002Yv-OO for qemu-devel@nongnu.org; Mon, 01 Apr 2013 06:04:00 -0400 Received: from /spool/local by e28smtp07.in.ibm.com with IBM ESMTP SMTP Gateway: Authorized Use Only! Violators will be prosecuted for from ; Mon, 1 Apr 2013 15:29:47 +0530 Received: from d28dlp01.in.ibm.com (9.184.220.126) by e28smtp07.in.ibm.com (192.168.1.137) with IBM ESMTP SMTP Gateway: Authorized Use Only! Violators will be prosecuted; Mon, 1 Apr 2013 15:29:45 +0530 Received: from d28relay01.in.ibm.com (d28relay01.in.ibm.com [9.184.220.58]) by d28dlp01.in.ibm.com (Postfix) with ESMTP id 20DE1E0053 for ; Mon, 1 Apr 2013 15:35:35 +0530 (IST) Received: from d28av03.in.ibm.com (d28av03.in.ibm.com [9.184.220.65]) by d28relay01.in.ibm.com (8.13.8/8.13.8/NCO v10.0) with ESMTP id r31A3ngu30015594 for ; Mon, 1 Apr 2013 15:33:49 +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 r31A3pnC029160 for ; Mon, 1 Apr 2013 21:03:53 +1100 Received: from RH63Wenchao (wenchaox.cn.ibm.com [9.115.122.29]) by d28av03.in.ibm.com (8.14.4/8.13.1/NCO v10.0 AVin) with ESMTP id r31A1YhZ016168; Mon, 1 Apr 2013 21:03:49 +1100 From: Wenchao Xia To: qemu-devel@nongnu.org Date: Mon, 1 Apr 2013 18:01:30 +0800 Message-Id: <1364810491-21404-3-git-send-email-xiawenc@linux.vnet.ibm.com> X-Mailer: git-send-email 1.7.1 In-Reply-To: <1364810491-21404-1-git-send-email-xiawenc@linux.vnet.ibm.com> References: <1364810491-21404-1-git-send-email-xiawenc@linux.vnet.ibm.com> X-TM-AS-MML: No X-Content-Scanned: Fidelis XPS MAILER x-cbid: 13040109-8878-0000-0000-000006874394 X-detected-operating-system: by eggs.gnu.org: GNU/Linux 2.4.x-2.6.x [generic] X-Received-From: 122.248.162.7 Cc: kwolf@redhat.com, pbonzini@redhat.com, Wenchao Xia , dietmar@proxmox.com, stefanha@gmail.com Subject: [Qemu-devel] [PATCH 2/3] block: adjust qmp_transaction to be extendable 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 Now code for external snapshot are packaged as one case in qmp_transaction, so later other operation could be added. The logic in qmp_transaction is changed a bit: Original code tries to create all images first and then update all *bdrv once together, new code create and update one *bdrv one time, and use bdrv_deappend() to rollback on fail. This allows mixing different kind of requests in qmp_transaction() later. Signed-off-by: Wenchao Xia --- blockdev.c | 250 +++++++++++++++++++++++++++++++++++++----------------------- 1 files changed, 153 insertions(+), 97 deletions(-) diff --git a/blockdev.c b/blockdev.c index 8cdc9ce..75416fb 100644 --- a/blockdev.c +++ b/blockdev.c @@ -779,9 +779,155 @@ 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 BdrvActionOps { + int (*commit)(BlockdevAction *action, void **p_opaque, Error **errp); + void (*rollback)(BlockdevAction *action, void *opaque); + void (*clean)(BlockdevAction *action, void *opaque); +} BdrvActionOps; + +typedef struct ExternalSnapshotState { BlockDriverState *old_bs; BlockDriverState *new_bs; +} ExternalSnapshotState; + +static int external_snapshot_commit(BlockdevAction *action, + void **p_opaque, + Error **errp) +{ + const char *device; + const char *new_image_file; + const char *format = "qcow2"; + enum NewImageMode mode = NEW_IMAGE_MODE_ABSOLUTE_PATHS; + BlockDriver *drv, *proto_drv; + ExternalSnapshotState *states = NULL; + int flags, ret; + Error *local_err = NULL; + + g_assert(action->kind == BLOCKDEV_ACTION_KIND_BLOCKDEV_SNAPSHOT_SYNC); + + /* get parameters */ + device = action->blockdev_snapshot_sync->device; + new_image_file = action->blockdev_snapshot_sync->snapshot_file; + if (action->blockdev_snapshot_sync->has_format) { + format = action->blockdev_snapshot_sync->format; + } + if (action->blockdev_snapshot_sync->has_mode) { + mode = action->blockdev_snapshot_sync->mode; + } + + /* start processing */ + states = g_malloc0(sizeof(*states)); + drv = bdrv_find_format(format); + if (!drv) { + error_set(errp, QERR_INVALID_BLOCK_FORMAT, format); + goto fail; + } + + states->old_bs = bdrv_find(device); + if (!states->old_bs) { + error_set(errp, QERR_DEVICE_NOT_FOUND, device); + goto fail; + } + + if (!bdrv_is_inserted(states->old_bs)) { + error_set(errp, QERR_DEVICE_HAS_NO_MEDIUM, device); + goto fail; + } + + if (bdrv_in_use(states->old_bs)) { + error_set(errp, QERR_DEVICE_IN_USE, device); + goto fail; + } + + if (!bdrv_is_read_only(states->old_bs)) { + if (bdrv_flush(states->old_bs)) { + error_set(errp, QERR_IO_ERROR); + goto fail; + } + } + + proto_drv = bdrv_find_protocol(new_image_file); + if (!proto_drv) { + error_set(errp, QERR_INVALID_BLOCK_FORMAT, format); + goto fail; + } + + flags = states->old_bs->open_flags; + + /* create new image w/backing file */ + if (mode != NEW_IMAGE_MODE_EXISTING) { + bdrv_img_create(new_image_file, format, + states->old_bs->filename, + states->old_bs->drv->format_name, + NULL, -1, flags, &local_err, false); + if (error_is_set(&local_err)) { + error_propagate(errp, local_err); + goto fail; + } + } + + states->new_bs = bdrv_new(""); + /* TODO Inherit bs->options or only take explicit options with an + * extended QMP command? */ + ret = bdrv_open(states->new_bs, new_image_file, NULL, + flags | BDRV_O_NO_BACKING, drv); + if (ret != 0) { + error_set(errp, QERR_OPEN_FILE_FAILED, new_image_file); + goto fail; + } + + /* 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 + * bdrv_reopen_multiple() across all the entries at once, because we + * don't want to abort all of them if one of them fails the reopen */ + bdrv_reopen(states->new_bs, states->new_bs->open_flags & ~BDRV_O_RDWR, + NULL); + + *p_opaque = states; + return 0; + +fail: + if (states) { + if (states->new_bs) { + bdrv_delete(states->new_bs); + } + g_free(states); + } + return -1; +} + +static void external_snapshot_rollback(BlockdevAction *action, + void *opaque) +{ + ExternalSnapshotState *states = opaque; + int open_flags; + + if (states) { + open_flags = states->old_bs->open_flags; + bdrv_deappend(states->old_bs); + bdrv_reopen(states->old_bs, open_flags, NULL); + } +} + +static void external_snapshot_clean(BlockdevAction *action, + void *opaque) +{ + ExternalSnapshotState *states = opaque; + + g_free(states); +} + +const BdrvActionOps external_snapshot_ops = { + .commit = external_snapshot_commit, + .rollback = external_snapshot_rollback, + .clean = external_snapshot_clean, +}; + +typedef struct BlkTransactionStates { + BlockdevAction *action; + void *opaque; + const BdrvActionOps *ops; QSIMPLEQ_ENTRY(BlkTransactionStates) entry; } BlkTransactionStates; @@ -792,10 +938,8 @@ typedef struct BlkTransactionStates { */ void qmp_transaction(BlockdevActionList *dev_list, Error **errp) { - int ret = 0; BlockdevActionList *dev_entry = dev_list; BlkTransactionStates *states, *next; - Error *local_err = NULL; QSIMPLEQ_HEAD(snap_bdrv_states, BlkTransactionStates) snap_bdrv_states; QSIMPLEQ_INIT(&snap_bdrv_states); @@ -806,125 +950,37 @@ 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; - BlockDriver *proto_drv; - BlockDriver *drv; - int flags; - enum NewImageMode mode; - const char *new_image_file; - const char *device; - const char *format = "qcow2"; - dev_info = dev_entry->value; dev_entry = dev_entry->next; states = g_malloc0(sizeof(BlkTransactionStates)); QSIMPLEQ_INSERT_TAIL(&snap_bdrv_states, states, entry); + states->action = dev_info; switch (dev_info->kind) { case BLOCKDEV_ACTION_KIND_BLOCKDEV_SNAPSHOT_SYNC: - device = dev_info->blockdev_snapshot_sync->device; - if (!dev_info->blockdev_snapshot_sync->has_mode) { - dev_info->blockdev_snapshot_sync->mode = NEW_IMAGE_MODE_ABSOLUTE_PATHS; - } - new_image_file = dev_info->blockdev_snapshot_sync->snapshot_file; - if (dev_info->blockdev_snapshot_sync->has_format) { - format = dev_info->blockdev_snapshot_sync->format; - } - mode = dev_info->blockdev_snapshot_sync->mode; + states->ops = &external_snapshot_ops; break; default: abort(); } - drv = bdrv_find_format(format); - if (!drv) { - error_set(errp, QERR_INVALID_BLOCK_FORMAT, format); - goto delete_and_fail; - } - - states->old_bs = bdrv_find(device); - if (!states->old_bs) { - error_set(errp, QERR_DEVICE_NOT_FOUND, device); - goto delete_and_fail; - } - - if (!bdrv_is_inserted(states->old_bs)) { - error_set(errp, QERR_DEVICE_HAS_NO_MEDIUM, device); - goto delete_and_fail; - } - - if (bdrv_in_use(states->old_bs)) { - error_set(errp, QERR_DEVICE_IN_USE, device); - goto delete_and_fail; - } - - if (!bdrv_is_read_only(states->old_bs)) { - if (bdrv_flush(states->old_bs)) { - error_set(errp, QERR_IO_ERROR); - goto delete_and_fail; - } - } - - flags = states->old_bs->open_flags; - - proto_drv = bdrv_find_protocol(new_image_file); - if (!proto_drv) { - error_set(errp, QERR_INVALID_BLOCK_FORMAT, format); - goto delete_and_fail; - } - - /* create new image w/backing file */ - if (mode != NEW_IMAGE_MODE_EXISTING) { - bdrv_img_create(new_image_file, format, - states->old_bs->filename, - states->old_bs->drv->format_name, - NULL, -1, flags, &local_err, false); - if (error_is_set(&local_err)) { - error_propagate(errp, local_err); - goto delete_and_fail; - } - } - - /* We will manually add the backing_hd field to the bs later */ - states->new_bs = bdrv_new(""); - /* TODO Inherit bs->options or only take explicit options with an - * extended QMP command? */ - ret = bdrv_open(states->new_bs, new_image_file, NULL, - flags | BDRV_O_NO_BACKING, drv); - if (ret != 0) { - error_set(errp, QERR_OPEN_FILE_FAILED, new_image_file); + if (states->ops->commit(states->action, &states->opaque, errp)) { 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) { - /* 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 - * bdrv_reopen_multiple() across all the entries at once, because we - * don't want to abort all of them if one of them fails the reopen */ - bdrv_reopen(states->new_bs, states->new_bs->open_flags & ~BDRV_O_RDWR, - NULL); - } - /* success */ goto exit; delete_and_fail: - /* - * failure, and it is all-or-none; abandon each new bs, and keep using - * the original bs for all images - */ QSIMPLEQ_FOREACH(states, &snap_bdrv_states, entry) { - if (states->new_bs) { - bdrv_delete(states->new_bs); - } + states->ops->rollback(states->action, states->opaque); } + exit: QSIMPLEQ_FOREACH_SAFE(states, &snap_bdrv_states, entry, next) { + states->ops->clean(states->action, states->opaque); g_free(states); } }