From nobody Thu Dec 18 13:25:05 2025 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; dmarc=fail(p=none dis=none) header.from=redhat.com Return-Path: Received: from lists.gnu.org (lists.gnu.org [208.118.235.17]) by mx.zohomail.com with SMTPS id 1520614424313808.8210517292757; Fri, 9 Mar 2018 08:53:44 -0800 (PST) Received: from localhost ([::1]:46488 helo=lists.gnu.org) by lists.gnu.org with esmtp (Exim 4.71) (envelope-from ) id 1euLH5-0004VP-CH for importer@patchew.org; Fri, 09 Mar 2018 11:53:43 -0500 Received: from eggs.gnu.org ([2001:4830:134:3::10]:33473) by lists.gnu.org with esmtp (Exim 4.71) (envelope-from ) id 1euKkv-000136-Mb for qemu-devel@nongnu.org; Fri, 09 Mar 2018 11:20:31 -0500 Received: from Debian-exim by eggs.gnu.org with spam-scanned (Exim 4.71) (envelope-from ) id 1euKku-0006rs-46 for qemu-devel@nongnu.org; Fri, 09 Mar 2018 11:20:29 -0500 Received: from mx3-rdu2.redhat.com ([66.187.233.73]:44676 helo=mx1.redhat.com) by eggs.gnu.org with esmtps (TLS1.0:DHE_RSA_AES_256_CBC_SHA1:32) (Exim 4.71) (envelope-from ) id 1euKko-0006hW-Ue; Fri, 09 Mar 2018 11:20:23 -0500 Received: from smtp.corp.redhat.com (int-mx06.intmail.prod.int.rdu2.redhat.com [10.11.54.6]) (using TLSv1.2 with cipher AECDH-AES256-SHA (256/256 bits)) (No client certificate requested) by mx1.redhat.com (Postfix) with ESMTPS id 857B28151D47; Fri, 9 Mar 2018 16:20:22 +0000 (UTC) Received: from localhost.localdomain.com (ovpn-117-234.ams2.redhat.com [10.36.117.234]) by smtp.corp.redhat.com (Postfix) with ESMTP id D5A94215CDAA; Fri, 9 Mar 2018 16:20:21 +0000 (UTC) From: Kevin Wolf To: qemu-block@nongnu.org Date: Fri, 9 Mar 2018 17:18:57 +0100 Message-Id: <20180309161933.8168-21-kwolf@redhat.com> In-Reply-To: <20180309161933.8168-1-kwolf@redhat.com> References: <20180309161933.8168-1-kwolf@redhat.com> X-Scanned-By: MIMEDefang 2.78 on 10.11.54.6 X-Greylist: Sender IP whitelisted, not delayed by milter-greylist-4.5.16 (mx1.redhat.com [10.11.55.8]); Fri, 09 Mar 2018 16:20:22 +0000 (UTC) X-Greylist: inspected by milter-greylist-4.5.16 (mx1.redhat.com [10.11.55.8]); Fri, 09 Mar 2018 16:20:22 +0000 (UTC) for IP:'10.11.54.6' DOMAIN:'int-mx06.intmail.prod.int.rdu2.redhat.com' HELO:'smtp.corp.redhat.com' FROM:'kwolf@redhat.com' RCPT:'' X-detected-operating-system: by eggs.gnu.org: GNU/Linux 2.2.x-3.x [generic] [fuzzy] X-Received-From: 66.187.233.73 Subject: [Qemu-devel] [PULL 20/56] qcow2: Use BlockdevRef in qcow2_co_create() 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+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_co_create(), make use of the BlockdevRef that is included in BlockdevCreateOptions. Signed-off-by: Kevin Wolf Reviewed-by: Eric Blake Reviewed-by: Max Reitz --- include/block/block.h | 1 + block.c | 47 +++++++++++++++++++++++++++++++++++++++++++++++ block/qcow2.c | 39 +++++++++++++++++++++++++-------------- 3 files changed, 73 insertions(+), 14 deletions(-) diff --git a/include/block/block.h b/include/block/block.h index 8b6db952a2..7805187b30 100644 --- a/include/block/block.h +++ b/include/block/block.h @@ -246,6 +246,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 8f1c43d037..4fc65f7621 100644 --- a/block.c +++ b/block.c @@ -34,6 +34,8 @@ #include "qapi/qmp/qdict.h" #include "qapi/qmp/qjson.h" #include "qapi/qmp/qstring.h" +#include "qapi/qobject-output-visitor.h" +#include "qapi/qapi-visit-block-core.h" #include "sysemu/block-backend.h" #include "sysemu/sysemu.h" #include "qemu/notify.h" @@ -2406,6 +2408,51 @@ 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); + + /* bdrv_open_inherit() defaults to the values in bdrv_flags (for + * compatibility with other callers) rather than what we want as t= he + * real defaults. Apply the defaults here instead. */ + qdict_set_default_str(qdict, BDRV_OPT_CACHE_DIRECT, "off"); + qdict_set_default_str(qdict, BDRV_OPT_CACHE_NO_FLUSH, "off"); + qdict_set_default_str(qdict, BDRV_OPT_READ_ONLY, "off"); + } + + bs =3D bdrv_open_inherit(NULL, reference, qdict, 0, NULL, NULL, errp); + obj =3D NULL; + +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 7679c28f57..b7df2d5cab 100644 --- a/block/qcow2.c +++ b/block/qcow2.c @@ -2768,8 +2768,8 @@ static uint64_t qcow2_opt_get_refcount_bits_del(QemuO= pts *opts, int version, } =20 static int coroutine_fn -qcow2_co_create(BlockDriverState *bs, BlockdevCreateOptions *create_option= s, - QemuOpts *opts, const char *encryptfmt, Error **errp) +qcow2_co_create(BlockdevCreateOptions *create_options, QemuOpts *opts, + const char *encryptfmt, Error **errp) { BlockdevCreateOptionsQcow2 *qcow2_opts; QDict *options; @@ -2786,7 +2786,8 @@ qcow2_co_create(BlockDriverState *bs, BlockdevCreateO= ptions *create_options, * 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; @@ -2795,10 +2796,15 @@ qcow2_co_create(BlockDriverState *bs, BlockdevCreat= eOptions *create_options, Error *local_err =3D NULL; int ret; =20 - /* Validate options and set default values */ assert(create_options->driver =3D=3D BLOCKDEV_DRIVER_QCOW2); qcow2_opts =3D &create_options->u.qcow2; =20 + bs =3D bdrv_open_blockdev_ref(qcow2_opts->file, errp); + if (bs =3D=3D NULL) { + return -EIO; + } + + /* Validate options and set default values */ if (!QEMU_IS_ALIGNED(qcow2_opts->size, BDRV_SECTOR_SIZE)) { error_setg(errp, "Image size must be a multiple of 512 bytes"); ret =3D -EINVAL; @@ -2827,7 +2833,8 @@ qcow2_co_create(BlockDriverState *bs, BlockdevCreateO= ptions *create_options, } =20 if (!validate_cluster_size(cluster_size, errp)) { - return -EINVAL; + ret =3D -EINVAL; + goto out; } =20 if (!qcow2_opts->has_preallocation) { @@ -2838,11 +2845,13 @@ qcow2_co_create(BlockDriverState *bs, BlockdevCreat= eOptions *create_options, { error_setg(errp, "Backing file and preallocation cannot be used at= " "the same time"); - return -EINVAL; + ret =3D -EINVAL; + goto out; } if (qcow2_opts->has_backing_fmt && !qcow2_opts->has_backing_file) { error_setg(errp, "Backing format cannot be used without backing fi= le"); - return -EINVAL; + ret =3D -EINVAL; + goto out; } =20 if (!qcow2_opts->has_lazy_refcounts) { @@ -2851,7 +2860,8 @@ qcow2_co_create(BlockDriverState *bs, BlockdevCreateO= ptions *create_options, 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) { @@ -2862,13 +2872,15 @@ qcow2_co_create(BlockDriverState *bs, BlockdevCreat= eOptions *create_options, { 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 @@ -3026,9 +3038,8 @@ qcow2_co_create(BlockDriverState *bs, BlockdevCreateO= ptions *create_options, =20 ret =3D 0; out: - if (blk) { - blk_unref(blk); - } + blk_unref(blk); + bdrv_unref(bs); return ret; } =20 @@ -3157,7 +3168,7 @@ static int coroutine_fn qcow2_co_create_opts(const ch= ar *filename, QemuOpts *opt .refcount_bits =3D refcount_bits, }, }; - ret =3D qcow2_co_create(bs, &create_options, opts, encryptfmt, errp); + ret =3D qcow2_co_create(&create_options, opts, encryptfmt, errp); if (ret < 0) { goto finish; } --=20 2.13.6