From patchwork Fri Apr 8 17:10:09 2016 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Max Reitz X-Patchwork-Id: 8785021 Return-Path: X-Original-To: patchwork-qemu-devel@patchwork.kernel.org Delivered-To: patchwork-parsemail@patchwork1.web.kernel.org Received: from mail.kernel.org (mail.kernel.org [198.145.29.136]) by patchwork1.web.kernel.org (Postfix) with ESMTP id D799F9F36E for ; Fri, 8 Apr 2016 17:13:27 +0000 (UTC) Received: from mail.kernel.org (localhost [127.0.0.1]) by mail.kernel.org (Postfix) with ESMTP id 283E920295 for ; Fri, 8 Apr 2016 17:13:27 +0000 (UTC) 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.kernel.org (Postfix) with ESMTPS id 564AF2028D for ; Fri, 8 Apr 2016 17:13:26 +0000 (UTC) Received: from localhost ([::1]:57382 helo=lists.gnu.org) by lists.gnu.org with esmtp (Exim 4.71) (envelope-from ) id 1aoZyH-0000do-KW for patchwork-qemu-devel@patchwork.kernel.org; Fri, 08 Apr 2016 13:13:25 -0400 Received: from eggs.gnu.org ([2001:4830:134:3::10]:55813) by lists.gnu.org with esmtp (Exim 4.71) (envelope-from ) id 1aoZvS-000418-Ig for qemu-devel@nongnu.org; Fri, 08 Apr 2016 13:10:31 -0400 Received: from Debian-exim by eggs.gnu.org with spam-scanned (Exim 4.71) (envelope-from ) id 1aoZvO-0002RZ-38 for qemu-devel@nongnu.org; Fri, 08 Apr 2016 13:10:30 -0400 Received: from mx1.redhat.com ([209.132.183.28]:42266) by eggs.gnu.org with esmtp (Exim 4.71) (envelope-from ) id 1aoZvL-0002Nx-6l; Fri, 08 Apr 2016 13:10:23 -0400 Received: from int-mx14.intmail.prod.int.phx2.redhat.com (int-mx14.intmail.prod.int.phx2.redhat.com [10.5.11.27]) (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 E8829C4648; Fri, 8 Apr 2016 17:10:22 +0000 (UTC) Received: from localhost (ovpn-116-78.ams2.redhat.com [10.36.116.78]) by int-mx14.intmail.prod.int.phx2.redhat.com (8.14.4/8.14.4) with ESMTP id u38HALEv015695 (version=TLSv1/SSLv3 cipher=DHE-RSA-AES256-GCM-SHA384 bits=256 verify=NO); Fri, 8 Apr 2016 13:10:22 -0400 From: Max Reitz To: qemu-block@nongnu.org Date: Fri, 8 Apr 2016 19:10:09 +0200 Message-Id: <1460135415-21763-3-git-send-email-mreitz@redhat.com> In-Reply-To: <1460135415-21763-1-git-send-email-mreitz@redhat.com> References: <1460135415-21763-1-git-send-email-mreitz@redhat.com> X-Scanned-By: MIMEDefang 2.68 on 10.5.11.27 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] [PATCH v3 for-2.7 2/8] 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: Kevin Wolf , Alberto Garcia , qemu-devel@nongnu.org, Max Reitz Errors-To: qemu-devel-bounces+patchwork-qemu-devel=patchwork.kernel.org@nongnu.org Sender: "Qemu-devel" X-Spam-Status: No, score=-6.9 required=5.0 tests=BAYES_00, RCVD_IN_DNSWL_HI, UNPARSEABLE_RELAY autolearn=unavailable version=3.3.1 X-Spam-Checker-Version: SpamAssassin 3.3.1 (2010-03-16) on mail.kernel.org X-Virus-Scanned: ClamAV using ClamSMTP 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 --- block.c | 47 ++++++++++++++++++++++++++++++++++++++++------- 1 file changed, 40 insertions(+), 7 deletions(-) diff --git a/block.c b/block.c index b6a452a..bb029dd 100644 --- a/block.c +++ b/block.c @@ -1424,8 +1424,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); @@ -1441,7 +1443,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; } @@ -1481,12 +1482,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; } /* @@ -1705,17 +1713,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;