From nobody Thu Oct 30 15:17:01 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 1525885523821306.2729201180945; Wed, 9 May 2018 10:05:23 -0700 (PDT) Received: from localhost ([::1]:57763 helo=lists.gnu.org) by lists.gnu.org with esmtp (Exim 4.71) (envelope-from ) id 1fGSWk-0002Zl-9c for importer@patchew.org; Wed, 09 May 2018 13:05:18 -0400 Received: from eggs.gnu.org ([2001:4830:134:3::10]:59924) by lists.gnu.org with esmtp (Exim 4.71) (envelope-from ) id 1fGSNm-0003pF-V5 for qemu-devel@nongnu.org; Wed, 09 May 2018 12:56:05 -0400 Received: from Debian-exim by eggs.gnu.org with spam-scanned (Exim 4.71) (envelope-from ) id 1fGSNk-0007Av-RJ for qemu-devel@nongnu.org; Wed, 09 May 2018 12:56:03 -0400 Received: from mx3-rdu2.redhat.com ([66.187.233.73]:53536 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 1fGSNf-0006xR-Ce; Wed, 09 May 2018 12:55:55 -0400 Received: from smtp.corp.redhat.com (int-mx03.intmail.prod.int.rdu2.redhat.com [10.11.54.3]) (using TLSv1.2 with cipher AECDH-AES256-SHA (256/256 bits)) (No client certificate requested) by mx1.redhat.com (Postfix) with ESMTPS id A751D4059FE2; Wed, 9 May 2018 16:55:54 +0000 (UTC) Received: from localhost (unknown [10.40.205.23]) by smtp.corp.redhat.com (Postfix) with ESMTPS id 2E56510B2B4F; Wed, 9 May 2018 16:55:54 +0000 (UTC) From: Max Reitz To: qemu-block@nongnu.org Date: Wed, 9 May 2018 18:55:28 +0200 Message-Id: <20180509165530.29561-12-mreitz@redhat.com> In-Reply-To: <20180509165530.29561-1-mreitz@redhat.com> References: <20180509165530.29561-1-mreitz@redhat.com> X-Scanned-By: MIMEDefang 2.78 on 10.11.54.3 X-Greylist: Sender IP whitelisted, not delayed by milter-greylist-4.5.16 (mx1.redhat.com [10.11.55.5]); Wed, 09 May 2018 16:55:54 +0000 (UTC) X-Greylist: inspected by milter-greylist-4.5.16 (mx1.redhat.com [10.11.55.5]); Wed, 09 May 2018 16:55:54 +0000 (UTC) for IP:'10.11.54.3' DOMAIN:'int-mx03.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] [PATCH 11/13] block: Try to create well typed json:{} filenames 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 , qemu-devel@nongnu.org, Markus Armbruster , Michael Roth , 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" By applying a health mix of qdict_flatten(), qdict_crumple(), qdict_stringify_for_keyval(), the keyval input visitor, and the QObject output visitor (not necessarily in that order), we can at least try to bring bs->full_open_options into accordance with the QAPI schema. This may not always work (there are some options left that have not been QAPI-fied yet), but in practice it usually will. In any case, sometimes emitting wrongly typed json:{} filenames is better than doing it effectively half the time. This affects some iotests because json:{} filenames are now usually crumpled. Buglink: https://bugzilla.redhat.com/show_bug.cgi?id=3D1534396 Signed-off-by: Max Reitz --- block.c | 69 ++++++++++++++++++++++++++++++++++++++++++= +++- tests/qemu-iotests/059.out | 2 +- tests/qemu-iotests/099.out | 4 +-- tests/qemu-iotests/110.out | 2 +- tests/qemu-iotests/198.out | 4 +-- tests/qemu-iotests/207.out | 10 +++---- 6 files changed, 79 insertions(+), 12 deletions(-) diff --git a/block.c b/block.c index 3c3e8fd11d..9e4a6c0d30 100644 --- a/block.c +++ b/block.c @@ -36,6 +36,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" @@ -5143,6 +5144,59 @@ static bool append_open_options(QDict *d, BlockDrive= rState *bs) return found_any; } =20 +/** + * Take a blockdev @options QDict and convert its values to the + * correct type. + * + * Fail if @options does not match the QAPI schema of BlockdevOptions. + * + * In case of failure, return NULL and set @errp. + * + * In case of success, return a correctly typed new QDict. + */ +static QDict *bdrv_type_blockdev_opts(const QDict *options, Error **errp) +{ + Visitor *v; + BlockdevOptions *blockdev_options; + QObject *typed_opts, *crumpled_opts; + QDict *string_options; + Error *local_err =3D NULL; + + string_options =3D qdict_clone_shallow(options); + + qdict_flatten(string_options); + qdict_stringify_for_keyval(string_options); + crumpled_opts =3D qdict_crumple(string_options, errp); + qobject_unref(string_options); + if (!crumpled_opts) { + error_prepend(errp, "Failed to crumple options: "); + return NULL; + } + + v =3D qobject_input_visitor_new_keyval(crumpled_opts); + visit_type_BlockdevOptions(v, NULL, &blockdev_options, &local_err); + visit_free(v); + if (local_err) { + error_propagate(errp, local_err); + error_prepend(errp, "Not a valid BlockdevOptions object: "); + return NULL; + } + + 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); + qapi_free_BlockdevOptions(blockdev_options); + if (local_err) { + error_propagate(errp, local_err); + return NULL; + } + + return qobject_to(QDict, typed_opts); +} + /* Updates the following BDS fields: * - exact_filename: A filename which may be used for opening a block dev= ice * which (mostly) equals the given BDS (even without any @@ -5244,10 +5298,23 @@ void bdrv_refresh_filename(BlockDriverState *bs) if (bs->exact_filename[0]) { pstrcpy(bs->filename, sizeof(bs->filename), bs->exact_filename); } else if (bs->full_open_options) { - QString *json =3D qobject_to_json(QOBJECT(bs->full_open_options)); + QString *json; + QDict *typed_opts, *json_opts; + + typed_opts =3D bdrv_type_blockdev_opts(bs->full_open_options, NULL= ); + + /* We cannot be certain that bs->full_open_options matches + * BlockdevOptions, so bdrv_type_blockdev_opts() may fail. + * That is not fatal, we can just emit bs->full_open_options + * directly -- qemu will accept that, even if it does not + * match the schema. */ + json_opts =3D typed_opts ?: bs->full_open_options; + + json =3D qobject_to_json(QOBJECT(json_opts)); snprintf(bs->filename, sizeof(bs->filename), "json:%s", qstring_get_str(json)); qobject_unref(json); + qobject_unref(typed_opts); } } =20 diff --git a/tests/qemu-iotests/059.out b/tests/qemu-iotests/059.out index f6dce7947c..0238b9e9a8 100644 --- a/tests/qemu-iotests/059.out +++ b/tests/qemu-iotests/059.out @@ -2050,7 +2050,7 @@ wrote 512/512 bytes at offset 10240 =20 =3D=3D=3D Testing monolithicFlat with internally generated JSON file name = =3D=3D=3D Formatting 'TEST_DIR/t.IMGFMT', fmt=3DIMGFMT size=3D67108864 subformat=3Dm= onolithicFlat -can't open: Cannot use relative extent paths with VMDK descriptor file 'js= on:{"image": {"driver": "file", "filename": "TEST_DIR/t.IMGFMT"}, "driver":= "blkdebug", "inject-error.0.event": "read_aio"}' +can't open: Cannot use relative extent paths with VMDK descriptor file 'js= on:{"inject-error": [{"event": "read_aio"}], "image": {"driver": "file", "f= ilename": "TEST_DIR/t.IMGFMT"}, "driver": "blkdebug"}' =20 =3D=3D=3D Testing version 3 =3D=3D=3D image: TEST_DIR/iotest-version3.IMGFMT diff --git a/tests/qemu-iotests/099.out b/tests/qemu-iotests/099.out index 8cce627529..0a9c434148 100644 --- a/tests/qemu-iotests/099.out +++ b/tests/qemu-iotests/099.out @@ -12,11 +12,11 @@ blkverify:TEST_DIR/t.IMGFMT.compare:TEST_DIR/t.IMGFMT =20 =3D=3D=3D Testing JSON filename for blkdebug =3D=3D=3D =20 -json:{"driver": "IMGFMT", "file": {"image": {"driver": "file", "filename":= "TEST_DIR/t.IMGFMT"}, "driver": "blkdebug", "inject-error.0.event": "l1_up= date"}} +json:{"driver": "IMGFMT", "file": {"inject-error": [{"event": "l1_update"}= ], "image": {"driver": "file", "filename": "TEST_DIR/t.IMGFMT"}, "driver": = "blkdebug"}} =20 =3D=3D=3D Testing indirectly enforced JSON filename =3D=3D=3D =20 -json:{"driver": "raw", "file": {"test": {"driver": "IMGFMT", "file": {"ima= ge": {"driver": "file", "filename": "TEST_DIR/t.IMGFMT"}, "driver": "blkdeb= ug", "inject-error.0.event": "l1_update"}}, "driver": "blkverify", "raw": {= "driver": "file", "filename": "TEST_DIR/t.IMGFMT.compare"}}} +json:{"driver": "raw", "file": {"test": {"driver": "IMGFMT", "file": {"inj= ect-error": [{"event": "l1_update"}], "image": {"driver": "file", "filename= ": "TEST_DIR/t.IMGFMT"}, "driver": "blkdebug"}}, "driver": "blkverify", "ra= w": {"driver": "file", "filename": "TEST_DIR/t.IMGFMT.compare"}}} =20 =3D=3D=3D Testing plain filename for blkdebug =3D=3D=3D =20 diff --git a/tests/qemu-iotests/110.out b/tests/qemu-iotests/110.out index b3584ff87f..fa19315dfb 100644 --- a/tests/qemu-iotests/110.out +++ b/tests/qemu-iotests/110.out @@ -11,7 +11,7 @@ backing file: t.IMGFMT.base (actual path: TEST_DIR/t.IMGF= MT.base) =20 =3D=3D=3D Non-reconstructable filename =3D=3D=3D =20 -image: json:{"driver": "IMGFMT", "file": {"set-state.0.event": "read_aio",= "image": {"driver": "file", "filename": "TEST_DIR/t.IMGFMT"}, "driver": "b= lkdebug", "set-state.0.new_state": 42}} +image: json:{"driver": "IMGFMT", "file": {"set-state": [{"new_state": 42, = "event": "read_aio"}], "image": {"driver": "file", "filename": "TEST_DIR/t.= IMGFMT"}, "driver": "blkdebug"}} file format: IMGFMT virtual size: 64M (67108864 bytes) backing file: t.IMGFMT.base (cannot determine actual path) diff --git a/tests/qemu-iotests/198.out b/tests/qemu-iotests/198.out index adb805cce9..2a02077d0b 100644 --- a/tests/qemu-iotests/198.out +++ b/tests/qemu-iotests/198.out @@ -32,7 +32,7 @@ read 16777216/16777216 bytes at offset 0 16 MiB, X ops; XX:XX:XX.X (XXX YYY/sec and XXX ops/sec) =20 =3D=3D checking image base =3D=3D -image: json:{"encrypt.key-secret": "sec0", "driver": "IMGFMT", "file": {"d= river": "file", "filename": "TEST_DIR/t.IMGFMT.base"}} +image: json:{"driver": "IMGFMT", "encrypt": {"key-secret": "sec0"}, "file"= : {"driver": "file", "filename": "TEST_DIR/t.IMGFMT.base"}} file format: IMGFMT virtual size: 16M (16777216 bytes) Format specific information: @@ -74,7 +74,7 @@ Format specific information: master key iters: 1024 =20 =3D=3D checking image layer =3D=3D -image: json:{"encrypt.key-secret": "sec1", "driver": "IMGFMT", "file": {"d= river": "file", "filename": "TEST_DIR/t.IMGFMT"}} +image: json:{"driver": "IMGFMT", "encrypt": {"key-secret": "sec1"}, "file"= : {"driver": "file", "filename": "TEST_DIR/t.IMGFMT"}} file format: IMGFMT virtual size: 16M (16777216 bytes) backing file: TEST_DIR/t.IMGFMT.base diff --git a/tests/qemu-iotests/207.out b/tests/qemu-iotests/207.out index 417deee970..effb74bf02 100644 --- a/tests/qemu-iotests/207.out +++ b/tests/qemu-iotests/207.out @@ -9,7 +9,7 @@ QMP_VERSION {"return": {}} {"timestamp": {"seconds": TIMESTAMP, "microseconds": TIMESTAMP}, "event"= : "SHUTDOWN", "data": {"guest": false}} =20 -image: json:{"driver": "IMGFMT", "file": {"server.host": "127.0.0.1", "ser= ver.port": "22", "driver": "ssh", "path": "TEST_DIR/t.IMGFMT"}} +image: json:{"driver": "IMGFMT", "file": {"driver": "ssh", "path": "TEST_D= IR/t.IMGFMT", "server": { "port": "22", "host": "127.0.0.1" }}} file format: IMGFMT virtual size: 4.0M (4194304 bytes) =20 @@ -26,7 +26,7 @@ QMP_VERSION {"return": {}} {"timestamp": {"seconds": TIMESTAMP, "microseconds": TIMESTAMP}, "event"= : "SHUTDOWN", "data": {"guest": false}} =20 -image: json:{"driver": "IMGFMT", "file": {"server.host": "127.0.0.1", "ser= ver.port": "22", "driver": "ssh", "path": "TEST_DIR/t.IMGFMT"}} +image: json:{"driver": "IMGFMT", "file": {"driver": "ssh", "path": "TEST_D= IR/t.IMGFMT", "server": { "port": "22", "host": "127.0.0.1" }}} file format: IMGFMT virtual size: 8.0M (8388608 bytes) Testing: @@ -36,7 +36,7 @@ QMP_VERSION {"return": {}} {"timestamp": {"seconds": TIMESTAMP, "microseconds": TIMESTAMP}, "event"= : "SHUTDOWN", "data": {"guest": false}} =20 -image: json:{"driver": "IMGFMT", "file": {"server.host": "127.0.0.1", "ser= ver.port": "22", "driver": "ssh", "path": "TEST_DIR/t.IMGFMT"}} +image: json:{"driver": "IMGFMT", "file": {"driver": "ssh", "path": "TEST_D= IR/t.IMGFMT", "server": { "port": "22", "host": "127.0.0.1" }}} file format: IMGFMT virtual size: 4.0M (4194304 bytes) Testing: @@ -47,7 +47,7 @@ QMP_VERSION {"return": {}} {"timestamp": {"seconds": TIMESTAMP, "microseconds": TIMESTAMP}, "event"= : "SHUTDOWN", "data": {"guest": false}} =20 -image: json:{"driver": "IMGFMT", "file": {"server.host": "127.0.0.1", "ser= ver.port": "22", "driver": "ssh", "path": "TEST_DIR/t.IMGFMT"}} +image: json:{"driver": "IMGFMT", "file": {"driver": "ssh", "path": "TEST_D= IR/t.IMGFMT", "server": { "port": "22", "host": "127.0.0.1" }}} file format: IMGFMT virtual size: 8.0M (8388608 bytes) Testing: @@ -58,7 +58,7 @@ QMP_VERSION {"return": {}} {"timestamp": {"seconds": TIMESTAMP, "microseconds": TIMESTAMP}, "event"= : "SHUTDOWN", "data": {"guest": false}} =20 -image: json:{"driver": "IMGFMT", "file": {"server.host": "127.0.0.1", "ser= ver.port": "22", "driver": "ssh", "path": "TEST_DIR/t.IMGFMT"}} +image: json:{"driver": "IMGFMT", "file": {"driver": "ssh", "path": "TEST_D= IR/t.IMGFMT", "server": { "port": "22", "host": "127.0.0.1" }}} file format: IMGFMT virtual size: 4.0M (4194304 bytes) =20 --=20 2.14.3