From patchwork Wed May 25 17:39:28 2016 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Kevin Wolf X-Patchwork-Id: 9135939 Return-Path: Received: from mail.wl.linuxfoundation.org (pdx-wl-mail.web.codeaurora.org [172.30.200.125]) by pdx-korg-patchwork.web.codeaurora.org (Postfix) with ESMTP id B08D76075C for ; Wed, 25 May 2016 17:42:27 +0000 (UTC) Received: from mail.wl.linuxfoundation.org (localhost [127.0.0.1]) by mail.wl.linuxfoundation.org (Postfix) with ESMTP id A5B1B1FFC9 for ; Wed, 25 May 2016 17:42:27 +0000 (UTC) Received: by mail.wl.linuxfoundation.org (Postfix, from userid 486) id 9A8C02829C; Wed, 25 May 2016 17:42:27 +0000 (UTC) X-Spam-Checker-Version: SpamAssassin 3.3.1 (2010-03-16) on pdx-wl-mail.web.codeaurora.org X-Spam-Level: X-Spam-Status: No, score=-6.9 required=2.0 tests=BAYES_00,RCVD_IN_DNSWL_HI autolearn=ham version=3.3.1 Received: from lists.gnu.org (lists.gnu.org [208.118.235.17]) (using TLSv1 with cipher AES256-SHA (256/256 bits)) (No client certificate requested) by mail.wl.linuxfoundation.org (Postfix) with ESMTPS id 35C751FFC9 for ; Wed, 25 May 2016 17:42:27 +0000 (UTC) Received: from localhost ([::1]:34289 helo=lists.gnu.org) by lists.gnu.org with esmtp (Exim 4.71) (envelope-from ) id 1b5cp8-00077h-8N for patchwork-qemu-devel@patchwork.kernel.org; Wed, 25 May 2016 13:42:26 -0400 Received: from eggs.gnu.org ([2001:4830:134:3::10]:49218) by lists.gnu.org with esmtp (Exim 4.71) (envelope-from ) id 1b5cnF-0005ZA-4Z for qemu-devel@nongnu.org; Wed, 25 May 2016 13:40:35 -0400 Received: from Debian-exim by eggs.gnu.org with spam-scanned (Exim 4.71) (envelope-from ) id 1b5cnC-0004PI-Ty for qemu-devel@nongnu.org; Wed, 25 May 2016 13:40:28 -0400 Received: from mx1.redhat.com ([209.132.183.28]:46681) by eggs.gnu.org with esmtp (Exim 4.71) (envelope-from ) id 1b5cn4-0004Le-VM; Wed, 25 May 2016 13:40:19 -0400 Received: from int-mx09.intmail.prod.int.phx2.redhat.com (int-mx09.intmail.prod.int.phx2.redhat.com [10.5.11.22]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by mx1.redhat.com (Postfix) with ESMTPS id 846E268D; Wed, 25 May 2016 17:40:18 +0000 (UTC) Received: from noname.redhat.com (ovpn-116-86.ams2.redhat.com [10.36.116.86]) by int-mx09.intmail.prod.int.phx2.redhat.com (8.14.4/8.14.4) with ESMTP id u4PHeAtL024245; Wed, 25 May 2016 13:40:14 -0400 From: Kevin Wolf To: qemu-block@nongnu.org Date: Wed, 25 May 2016 19:39:28 +0200 Message-Id: <1464197996-4581-4-git-send-email-kwolf@redhat.com> In-Reply-To: <1464197996-4581-1-git-send-email-kwolf@redhat.com> References: <1464197996-4581-1-git-send-email-kwolf@redhat.com> X-Scanned-By: MIMEDefang 2.68 on 10.5.11.22 X-Greylist: Sender IP whitelisted, not delayed by milter-greylist-4.5.16 (mx1.redhat.com [10.5.110.30]); Wed, 25 May 2016 17:40:18 +0000 (UTC) X-detected-operating-system: by eggs.gnu.org: GNU/Linux 2.2.x-3.x [generic] X-Received-From: 209.132.183.28 Subject: [Qemu-devel] [PULL 03/31] block: Let bdrv_open_inherit() return the snapshot X-BeenThere: qemu-devel@nongnu.org X-Mailman-Version: 2.1.21 Precedence: list List-Id: List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Cc: kwolf@redhat.com, qemu-devel@nongnu.org Errors-To: qemu-devel-bounces+patchwork-qemu-devel=patchwork.kernel.org@nongnu.org Sender: "Qemu-devel" X-Virus-Scanned: ClamAV using ClamSMTP From: Max Reitz If bdrv_open_inherit() creates a snapshot BDS and *pbs is NULL, that snapshot BDS should be returned instead of the BDS under it. This has worked so far because (nearly) all users of BDRV_O_SNAPSHOT use blk_new_open() to create the BDS tree. bdrv_append() (which is called by bdrv_append_temp_snapshot()) redirects pointers from parents (i.e. the BB in this case) to the newly appended child (i.e. the overlay), therefore, while bdrv_open_inherit() did not return the root BDS, the BB still pointed to it. The only instance where BDRV_O_SNAPSHOT is used but blk_new_open() is not is in blockdev_init() if no BDS tree is created, and instead blk_new() is used and the flags are stored in the BB root state. However, qmp_blockdev_change_medium() filters the BDRV_O_SNAPSHOT flag before invoking bdrv_open(), so it will not have any effect. In any case, it would be nicer if bdrv_open_inherit() could just always return the root of the BDS tree that has been created. To this end, bdrv_append_temp_snapshot() now returns the snapshot BDS instead of just appending it on top of the snapshotted BDS. Also, it calls bdrv_ref() before bdrv_append() (which bdrv_open_inherit() has to undo if not returning the overlay). Signed-off-by: Max Reitz Reviewed-by: Kevin Wolf Signed-off-by: Kevin Wolf --- block.c | 47 ++++++++++++++++++++++++++++++++++++++++------- 1 file changed, 40 insertions(+), 7 deletions(-) diff --git a/block.c b/block.c index f85c5a2..17ee088 100644 --- a/block.c +++ b/block.c @@ -1422,8 +1422,10 @@ done: return c; } -static int bdrv_append_temp_snapshot(BlockDriverState *bs, int flags, - QDict *snapshot_options, Error **errp) +static BlockDriverState *bdrv_append_temp_snapshot(BlockDriverState *bs, + int flags, + QDict *snapshot_options, + Error **errp) { /* TODO: extra byte is a hack to ensure MAX_PATH space on Windows. */ char *tmp_filename = g_malloc0(PATH_MAX + 1); @@ -1439,7 +1441,6 @@ static int bdrv_append_temp_snapshot(BlockDriverState *bs, int flags, /* Get the required size from the image */ total_size = bdrv_getlength(bs); if (total_size < 0) { - ret = total_size; error_setg_errno(errp, -total_size, "Could not get image size"); goto out; } @@ -1479,12 +1480,19 @@ static int bdrv_append_temp_snapshot(BlockDriverState *bs, int flags, goto out; } + /* bdrv_append() consumes a strong reference to bs_snapshot (i.e. it will + * call bdrv_unref() on it), so in order to be able to return one, we have + * to increase bs_snapshot's refcount here */ + bdrv_ref(bs_snapshot); bdrv_append(bs_snapshot, bs); + g_free(tmp_filename); + return bs_snapshot; + out: QDECREF(snapshot_options); g_free(tmp_filename); - return ret; + return NULL; } /* @@ -1704,17 +1712,42 @@ static int bdrv_open_inherit(BlockDriverState **pbs, const char *filename, } QDECREF(options); - *pbs = bs; /* For snapshot=on, create a temporary qcow2 overlay. bs points to the * temporary snapshot afterwards. */ if (snapshot_flags) { - ret = bdrv_append_temp_snapshot(bs, snapshot_flags, snapshot_options, - &local_err); + BlockDriverState *snapshot_bs; + snapshot_bs = bdrv_append_temp_snapshot(bs, snapshot_flags, + snapshot_options, &local_err); snapshot_options = NULL; if (local_err) { + ret = -EINVAL; goto close_and_fail; } + if (!*pbs) { + /* We are not going to return bs but the overlay on top of it + * (snapshot_bs); thus, we have to drop the strong reference to bs + * (which we obtained by calling bdrv_new()). bs will not be + * deleted, though, because the overlay still has a reference to it. + */ + bdrv_unref(bs); + bs = snapshot_bs; + } else { + /* We are not going to return snapshot_bs, so we have to drop the + * strong reference to it (which was returned by + * bdrv_append_temp_snapshot()). snapshot_bs will not be deleted, + * though, because bdrv_append_temp_snapshot() made all parental + * references to bs (*pbs) point to snapshot_bs. + * In fact, if *pbs was not NULL, we are not going to return any new + * BDS. But we do not need to decrement bs's refcount here as is + * done above, because with a non-NULL *pbs this function never even + * had a strong reference to bs. */ + bdrv_unref(snapshot_bs); + } + } + + if (!*pbs) { + *pbs = bs; } return 0;