From nobody Thu Oct 30 05:19:31 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 152529705406358.5082434561906; Wed, 2 May 2018 14:37:34 -0700 (PDT) Received: from localhost ([::1]:52652 helo=lists.gnu.org) by lists.gnu.org with esmtp (Exim 4.71) (envelope-from ) id 1fDzRN-0002rr-1B for importer@patchew.org; Wed, 02 May 2018 17:37:33 -0400 Received: from eggs.gnu.org ([2001:4830:134:3::10]:47448) by lists.gnu.org with esmtp (Exim 4.71) (envelope-from ) id 1fDzMb-0007Vk-J3 for qemu-devel@nongnu.org; Wed, 02 May 2018 17:32:38 -0400 Received: from Debian-exim by eggs.gnu.org with spam-scanned (Exim 4.71) (envelope-from ) id 1fDzMa-0000NA-FI for qemu-devel@nongnu.org; Wed, 02 May 2018 17:32:37 -0400 Received: from mx3-rdu2.redhat.com ([66.187.233.73]:47080 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 1fDzMX-0000KQ-1w; Wed, 02 May 2018 17:32:33 -0400 Received: from smtp.corp.redhat.com (int-mx04.intmail.prod.int.rdu2.redhat.com [10.11.54.4]) (using TLSv1.2 with cipher AECDH-AES256-SHA (256/256 bits)) (No client certificate requested) by mx1.redhat.com (Postfix) with ESMTPS id 8198A406EA4F; Wed, 2 May 2018 21:32:32 +0000 (UTC) Received: from localhost (unknown [10.40.205.26]) by smtp.corp.redhat.com (Postfix) with ESMTPS id 256872022EE0; Wed, 2 May 2018 21:32:31 +0000 (UTC) From: Max Reitz To: qemu-block@nongnu.org Date: Wed, 2 May 2018 23:32:16 +0200 Message-Id: <20180502213219.7842-5-mreitz@redhat.com> In-Reply-To: <20180502213219.7842-1-mreitz@redhat.com> References: <20180502213219.7842-1-mreitz@redhat.com> X-Scanned-By: MIMEDefang 2.78 on 10.11.54.4 X-Greylist: Sender IP whitelisted, not delayed by milter-greylist-4.5.16 (mx1.redhat.com [10.11.55.7]); Wed, 02 May 2018 21:32:32 +0000 (UTC) X-Greylist: inspected by milter-greylist-4.5.16 (mx1.redhat.com [10.11.55.7]); Wed, 02 May 2018 21:32:32 +0000 (UTC) for IP:'10.11.54.4' DOMAIN:'int-mx04.intmail.prod.int.rdu2.redhat.com' HELO:'smtp.corp.redhat.com' FROM:'mreitz@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] [RFC 4/7] block: Add bdrv_open_string_opts() 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 , Markus Armbruster , qemu-devel@nongnu.org, Max Reitz 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" This function is to be used by callers that cannot guarantee that all values in @options are correctly typed. In the future, we would like this function to be gone, of course, but for now it at least lets us begin a proper separation of legacy interfaces. Signed-off-by: Max Reitz --- include/block/block.h | 2 ++ block.c | 84 +++++++++++++++++++++++++++++++++++++++++++++++= ++-- 2 files changed, 83 insertions(+), 3 deletions(-) diff --git a/include/block/block.h b/include/block/block.h index 3894edda9d..5335fa8c20 100644 --- a/include/block/block.h +++ b/include/block/block.h @@ -261,6 +261,8 @@ int bdrv_open_backing_file(BlockDriverState *bs, QDict = *parent_options, const char *bdref_key, Error **errp); BlockDriverState *bdrv_open(const char *filename, const char *reference, QDict *options, int flags, Error **errp); +BlockDriverState *bdrv_open_string_opts(const char *filename, QDict *optio= ns, + int flags, Error **errp); BlockDriverState *bdrv_new_open_driver(BlockDriver *drv, const char *node_= name, int flags, Error **errp); BlockReopenQueue *bdrv_reopen_queue(BlockReopenQueue *bs_queue, diff --git a/block.c b/block.c index 8a8d9c02a9..1969fa303f 100644 --- a/block.c +++ b/block.c @@ -35,6 +35,7 @@ #include "qapi/qmp/qjson.h" #include "qapi/qmp/qnull.h" #include "qapi/qmp/qstring.h" +#include "qapi/qobject-input-visitor.h" #include "qapi/qobject-output-visitor.h" #include "qapi/qapi-visit-block-core.h" #include "sysemu/block-backend.h" @@ -77,6 +78,8 @@ static BlockDriverState *bdrv_open_inherit(const char *fi= lename, const BdrvChildRole *child_role, Error **errp); =20 +static QDict *bdrv_type_blockdev_opts(QDict *options); + /* If non-zero, use only whitelisted block drivers */ static int use_bdrv_whitelist; =20 @@ -1465,9 +1468,7 @@ static QDict *parse_json_filename(const char *filenam= e, Error **errp) return NULL; } =20 - qdict_flatten(options); - - return options; + return bdrv_type_blockdev_opts(options); } =20 static void parse_json_protocol(QDict *options, const char **pfilename, @@ -2801,6 +2802,83 @@ BlockDriverState *bdrv_open(const char *filename, co= nst char *reference, NULL, errp); } =20 +/* + * Take a blockdev @options QDict and convert its values to the + * correct type. This function takes ownership of @options. + * + * On both success and failure, @options is flattened. On success, + * its refcount is decreased and a new well typed flattened QDict is + * returned. On failure, @options is returned. + * + * TODO: Currently, this function cannot cope with partially typed + * dicts. If everything is typed correctly, the keyval visitor will + * complain and we will return the original @options -- which is + * correct. If all values are strings, the visitor will convert them + * to the correct type (if possible). But if both are mixed, the + * visitor will fail and the partially typed @options is returned. In + * practice, this should only be an issue with json:{} filenames, + * though. + * + * TODO: If @options does not conform to the schema, that should be a + * real error. + * + * TODO: Ideally, bdrv_open() should take BlockdevOptions, and the BDS + * should only contain BlockdevOptions. But this is not possible + * until this function really rejects anything it does not recognize + * (without breaking existing interfaces). + */ +static QDict *bdrv_type_blockdev_opts(QDict *options) +{ + Visitor *v; + BlockdevOptions *blockdev_options =3D NULL; + QObject *typed_opts, *crumpled_opts =3D NULL; + Error *local_err =3D NULL; + + if (!options) { + return NULL; + } + + qdict_flatten(options); + crumpled_opts =3D qdict_crumple(options, &local_err); + if (local_err) { + goto done; + } + + v =3D qobject_input_visitor_new_keyval(crumpled_opts); + visit_type_BlockdevOptions(v, NULL, &blockdev_options, &local_err); + visit_free(v); + if (local_err) { + goto done; + } + + v =3D qobject_output_visitor_new(&typed_opts); + visit_type_BlockdevOptions(v, NULL, &blockdev_options, &local_err); + if (!local_err) { + visit_complete(v, &typed_opts); + } + visit_free(v); + if (local_err) { + goto done; + } + + QDECREF(options); + options =3D qobject_to(QDict, typed_opts); + qdict_flatten(options); + +done: + error_free(local_err); + qapi_free_BlockdevOptions(blockdev_options); + qobject_decref(crumpled_opts); + return options; +} + +BlockDriverState *bdrv_open_string_opts(const char *filename, QDict *optio= ns, + int flags, Error **errp) +{ + return bdrv_open_inherit(filename, NULL, bdrv_type_blockdev_opts(optio= ns), + flags, NULL, NULL, errp); +} + /* * Adds a BlockDriverState to a simple queue for an atomic, transactional * reopen of multiple devices. --=20 2.14.3