Received: from eggs.gnu.org ([2001:4830:134:3::10]:34686) by lists.gnu.org with esmtp (Exim 4.71) (envelope-from ) id 1VQcCI-0001Au-SS for qemu-devel@nongnu.org; Mon, 30 Sep 2013 08:03:36 -0400 Received: from Debian-exim by eggs.gnu.org with spam-scanned (Exim 4.71) (envelope-from ) id 1VQcC9-0005rU-VG for qemu-devel@nongnu.org; Mon, 30 Sep 2013 08:03:30 -0400 Received: from mx1.redhat.com ([209.132.183.28]:29347) by eggs.gnu.org with esmtp (Exim 4.71) (envelope-from ) id 1VQcC9-0005rD-MY for qemu-devel@nongnu.org; Mon, 30 Sep 2013 08:03:21 -0400 Received: from int-mx09.intmail.prod.int.phx2.redhat.com (int-mx09.intmail.prod.int.phx2.redhat.com [10.5.11.22]) by mx1.redhat.com (8.14.4/8.14.4) with ESMTP id r8UC3LEW012270 (version=TLSv1/SSLv3 cipher=DHE-RSA-AES256-SHA bits=256 verify=OK) for ; Mon, 30 Sep 2013 08:03:21 -0400 From: Fam Zheng Date: Mon, 30 Sep 2013 20:02:56 +0800 Message-Id: <1380542578-2387-6-git-send-email-famz@redhat.com> In-Reply-To: <1380542578-2387-1-git-send-email-famz@redhat.com> References: <1380542578-2387-1-git-send-email-famz@redhat.com> Subject: [Qemu-devel] [PATCH v4 5/7] commit: support commit active layer List-Id: List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , To: qemu-devel@nongnu.org Cc: kwolf@redhat.com, pbonzini@redhat.com, jcody@redhat.com, famz@redhat.com, stefanha@redhat.com If active is top, it will be mirrored to base, (with block/mirror.c code), then the image is switched when user completes the block job. Signed-off-by: Fam Zheng --- blockdev.c | 42 ++++++++++++++++++++++++++++++++++++++++-- 1 file changed, 40 insertions(+), 2 deletions(-) diff --git a/blockdev.c b/blockdev.c index 032913e..f730e6f 100644 --- a/blockdev.c +++ b/blockdev.c @@ -1663,6 +1663,30 @@ void qmp_block_stream(const char *device, bool has_base, trace_qmp_block_stream(bs, bs->job); } +typedef struct { + BlockDriverState *bs; + BlockDriverState *base; +} commit_active_info_t; + +static void commit_active_cb(void *_info, int ret) +{ + commit_active_info_t *info = _info; + BlockDriverState *p; + assert(info->bs); + assert(info->base); + assert(info->base->backing_hd); + if (!ret) { + /* Mirror code already swapped bs and base, we drop the bs loop chain + * formed by the swap: break the loop chain, trigger the chain unref. + */ + p = info->base->backing_hd; + info->base->backing_hd = NULL; + bdrv_unref(p); + } + block_job_cb(info->bs, ret); + g_free(info); +} + void qmp_block_commit(const char *device, bool has_base, const char *base, const char *top, bool has_speed, int64_t speed, @@ -1710,8 +1734,22 @@ void qmp_block_commit(const char *device, return; } - commit_start(bs, base_bs, top_bs, speed, on_error, block_job_cb, bs, - &local_err); + if (top_bs == bs) { + commit_active_info_t *info = g_malloc(sizeof(commit_active_info_t)); + info->bs = bs; + info->base = base_bs; + if (bdrv_reopen(base_bs, bs->open_flags, &local_err)) { + error_propagate(errp, local_err); + return; + } + bdrv_ref(base_bs); + mirror_start(bs, base_bs, speed, 0, 0, MIRROR_SYNC_MODE_COMMON, + on_error, on_error, true, + commit_active_cb, info, &local_err); + } else { + commit_start(bs, base_bs, top_bs, speed, on_error, block_job_cb, bs, + &local_err); + } if (local_err != NULL) { error_propagate(errp, local_err); return; -- 1.8.3.1