From nobody Tue Feb 10 21:19:26 2026 Delivered-To: importer@patchew.org Received-SPF: pass (zoho.com: domain of gnu.org designates 208.118.235.17 as permitted sender) client-ip=208.118.235.17; envelope-from=qemu-devel-bounces+importer=patchew.org@nongnu.org; helo=lists.gnu.org; Authentication-Results: mx.zohomail.com; spf=pass (zoho.com: domain of gnu.org designates 208.118.235.17 as permitted sender) smtp.mailfrom=qemu-devel-bounces+importer=patchew.org@nongnu.org Return-Path: Received: from lists.gnu.org (lists.gnu.org [208.118.235.17]) by mx.zohomail.com with SMTPS id 1515700677683203.2808317117475; Thu, 11 Jan 2018 11:57:57 -0800 (PST) Received: from localhost ([::1]:38085 helo=lists.gnu.org) by lists.gnu.org with esmtp (Exim 4.71) (envelope-from ) id 1eZiz6-00012m-Sg for importer@patchew.org; Thu, 11 Jan 2018 14:57:56 -0500 Received: from eggs.gnu.org ([2001:4830:134:3::10]:57685) by lists.gnu.org with esmtp (Exim 4.71) (envelope-from ) id 1eZiuk-0005RF-Lh for qemu-devel@nongnu.org; Thu, 11 Jan 2018 14:53:28 -0500 Received: from Debian-exim by eggs.gnu.org with spam-scanned (Exim 4.71) (envelope-from ) id 1eZiuj-0000XY-Bu for qemu-devel@nongnu.org; Thu, 11 Jan 2018 14:53:26 -0500 Received: from mx1.redhat.com ([209.132.183.28]:55078) by eggs.gnu.org with esmtps (TLS1.0:DHE_RSA_AES_256_CBC_SHA1:32) (Exim 4.71) (envelope-from ) id 1eZiug-0000TG-2R; Thu, 11 Jan 2018 14:53:22 -0500 Received: from smtp.corp.redhat.com (int-mx06.intmail.prod.int.phx2.redhat.com [10.5.11.16]) (using TLSv1.2 with cipher AECDH-AES256-SHA (256/256 bits)) (No client certificate requested) by mx1.redhat.com (Postfix) with ESMTPS id 0858EC098D0A; Thu, 11 Jan 2018 19:53:21 +0000 (UTC) Received: from localhost.localdomain.com (unknown [10.36.118.59]) by smtp.corp.redhat.com (Postfix) with ESMTP id AB89617163; Thu, 11 Jan 2018 19:53:16 +0000 (UTC) From: Kevin Wolf To: qemu-block@nongnu.org Date: Thu, 11 Jan 2018 20:52:20 +0100 Message-Id: <20180111195225.4226-6-kwolf@redhat.com> In-Reply-To: <20180111195225.4226-1-kwolf@redhat.com> References: <20180111195225.4226-1-kwolf@redhat.com> X-Scanned-By: MIMEDefang 2.79 on 10.5.11.16 X-Greylist: Sender IP whitelisted, not delayed by milter-greylist-4.5.16 (mx1.redhat.com [10.5.110.31]); Thu, 11 Jan 2018 19:53:21 +0000 (UTC) X-detected-operating-system: by eggs.gnu.org: GNU/Linux 2.2.x-3.x [generic] [fuzzy] X-Received-From: 209.132.183.28 Subject: [Qemu-devel] [RFC PATCH 05/10] qcow2: Use BlockdevRef in qcow2_create2() 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, pkrempa@redhat.com, qemu-devel@nongnu.org, mreitz@redhat.com Errors-To: qemu-devel-bounces+importer=patchew.org@nongnu.org Sender: "Qemu-devel" X-ZohoMail: RSF_0 Z_629925259 SPT_0 Content-Transfer-Encoding: quoted-printable MIME-Version: 1.0 Content-Type: text/plain; charset="utf-8" Instead of passing a separate BlockDriverState* into qcow2_create2(), make use of the BlockdevRef that is included in BlockdevCreateOptions. Signed-off-by: Kevin Wolf Reviewed-by: Eric Blake --- include/block/block.h | 1 + block.c | 39 +++++++++++++++++++++++++++++++++++++++ block/qcow2.c | 33 +++++++++++++++++++++------------ 3 files changed, 61 insertions(+), 12 deletions(-) diff --git a/include/block/block.h b/include/block/block.h index 9b12774ddf..4b11f814a8 100644 --- a/include/block/block.h +++ b/include/block/block.h @@ -258,6 +258,7 @@ BdrvChild *bdrv_open_child(const char *filename, BlockDriverState* parent, const BdrvChildRole *child_role, bool allow_none, Error **errp); +BlockDriverState *bdrv_open_blockdev_ref(BlockdevRef *ref, Error **errp); void bdrv_set_backing_hd(BlockDriverState *bs, BlockDriverState *backing_h= d, Error **errp); int bdrv_open_backing_file(BlockDriverState *bs, QDict *parent_options, diff --git a/block.c b/block.c index a8da4f2b25..c9b0e1d6d3 100644 --- a/block.c +++ b/block.c @@ -32,6 +32,8 @@ #include "qapi/qmp/qerror.h" #include "qapi/qmp/qbool.h" #include "qapi/qmp/qjson.h" +#include "qapi/qobject-output-visitor.h" +#include "qapi-visit.h" #include "sysemu/block-backend.h" #include "sysemu/sysemu.h" #include "qemu/notify.h" @@ -2405,6 +2407,43 @@ BdrvChild *bdrv_open_child(const char *filename, return c; } =20 +/* TODO Future callers may need to specify parent/child_role in order for + * option inheritance to work. Existing callers use it for the root node. = */ +BlockDriverState *bdrv_open_blockdev_ref(BlockdevRef *ref, Error **errp) +{ + BlockDriverState *bs =3D NULL; + Error *local_err =3D NULL; + QObject *obj =3D NULL; + QDict *qdict =3D NULL; + const char *reference =3D NULL; + Visitor *v =3D NULL; + + if (ref->type =3D=3D QTYPE_QSTRING) { + reference =3D ref->u.reference; + } else { + BlockdevOptions *options =3D &ref->u.definition; + assert(ref->type =3D=3D QTYPE_QDICT); + + v =3D qobject_output_visitor_new(&obj); + visit_type_BlockdevOptions(v, NULL, &options, &local_err); + if (local_err) { + error_propagate(errp, local_err); + goto fail; + } + visit_complete(v, &obj); + + qdict =3D qobject_to_qdict(obj); + qdict_flatten(qdict); + } + + bs =3D bdrv_open_inherit(NULL, reference, qdict, 0, NULL, NULL, errp); + +fail: + qobject_decref(obj); + visit_free(v); + return bs; +} + static BlockDriverState *bdrv_append_temp_snapshot(BlockDriverState *bs, int flags, QDict *snapshot_options, diff --git a/block/qcow2.c b/block/qcow2.c index 09e567324d..1a0f8f2e6d 100644 --- a/block/qcow2.c +++ b/block/qcow2.c @@ -2697,8 +2697,7 @@ static uint64_t qcow2_opt_get_refcount_bits_del(QemuO= pts *opts, int version, return refcount_bits; } =20 -static int qcow2_create2(BlockDriverState *bs, - BlockdevCreateOptions *create_options, +static int qcow2_create2(BlockdevCreateOptions *create_options, QemuOpts *opts, const char *encryptfmt, Error **e= rrp) { BlockdevCreateOptionsQcow2 *qcow2_opts; @@ -2716,7 +2715,8 @@ static int qcow2_create2(BlockDriverState *bs, * 2 GB for 64k clusters, and we don't want to have a 2 GB initial file * size for any qcow2 image. */ - BlockBackend *blk; + BlockBackend *blk =3D NULL; + BlockDriverState *bs =3D NULL; QCowHeader *header; size_t cluster_size; int version; @@ -2725,6 +2725,11 @@ static int qcow2_create2(BlockDriverState *bs, Error *local_err =3D NULL; int ret; =20 + bs =3D bdrv_open_blockdev_ref(create_options->node, errp); + if (bs =3D=3D NULL) { + return -EIO; + } + /* Validate options and set default values */ assert(create_options->driver =3D=3D BLOCKDEV_DRIVER_QCOW2); qcow2_opts =3D &create_options->u.qcow2; @@ -2757,7 +2762,8 @@ static int qcow2_create2(BlockDriverState *bs, } =20 if (!validate_cluster_size(cluster_size, errp)) { - return -EINVAL; + ret =3D -EINVAL; + goto out; } =20 if (!qcow2_opts->has_preallocation) { @@ -2768,7 +2774,8 @@ static int qcow2_create2(BlockDriverState *bs, { error_setg(errp, "Backing file and preallocation cannot be used at= " "the same time"); - return -EINVAL; + ret =3D -EINVAL; + goto out; } =20 if (!qcow2_opts->has_lazy_refcounts) { @@ -2777,7 +2784,8 @@ static int qcow2_create2(BlockDriverState *bs, if (version < 3 && qcow2_opts->lazy_refcounts) { error_setg(errp, "Lazy refcounts only supported with compatibility= " "level 1.1 and above (use compat=3D1.1 or greater)"); - return -EINVAL; + ret =3D -EINVAL; + goto out; } =20 if (!qcow2_opts->has_refcount_bits) { @@ -2788,13 +2796,15 @@ static int qcow2_create2(BlockDriverState *bs, { error_setg(errp, "Refcount width must be a power of two and may no= t " "exceed 64 bits"); - return -EINVAL; + ret =3D -EINVAL; + goto out; } if (version < 3 && qcow2_opts->refcount_bits !=3D 16) { error_setg(errp, "Different refcount widths than 16 bits require " "compatibility level 1.1 or above (use compat=3D1.1 or " "greater)"); - return -EINVAL; + ret =3D -EINVAL; + goto out; } refcount_order =3D ctz32(qcow2_opts->refcount_bits); =20 @@ -2952,9 +2962,8 @@ static int qcow2_create2(BlockDriverState *bs, =20 ret =3D 0; out: - if (blk) { - blk_unref(blk); - } + blk_unref(blk); + bdrv_unref(bs); return ret; } =20 @@ -3082,7 +3091,7 @@ static int qcow2_create(const char *filename, QemuOpt= s *opts, Error **errp) .refcount_bits =3D refcount_bits, }, }; - ret =3D qcow2_create2(bs, &create_options, opts, encryptfmt, errp); + ret =3D qcow2_create2(&create_options, opts, encryptfmt, errp); if (ret < 0) { goto finish; } --=20 2.13.6