qemu-devel.nongnu.org archive mirror
 help / color / mirror / Atom feed
From: Max Reitz <mreitz@redhat.com>
To: qemu-block@nongnu.org
Cc: qemu-devel@nongnu.org, Max Reitz <mreitz@redhat.com>,
	Kevin Wolf <kwolf@redhat.com>,
	Markus Armbruster <armbru@redhat.com>
Subject: [Qemu-devel] [RFC 6/7] block: Use {blk_new, bdrv}_open_string_opts()
Date: Wed,  2 May 2018 23:32:18 +0200	[thread overview]
Message-ID: <20180502213219.7842-7-mreitz@redhat.com> (raw)
In-Reply-To: <20180502213219.7842-1-mreitz@redhat.com>

This patch makes every caller of blk_new_open() and bdrv_open() instead
call blk_new_open_string_opts() or bdrv_open_string_opts(),
respectively, when needed.  That is the case when the blockdev options
QDict may contain incorrectly typed string values.

In fact, all callers converted in this patch pass dicts that contain
string values only; and after this patch, all remaining callers of
blk_new_open() and bdrv_open() indeed guarantee that the dicts contain
only values that are correctly typed.

Signed-off-by: Max Reitz <mreitz@redhat.com>
---
Assuming that bdrv_open_inherit() always receives correctly typed
options and that parse_json_filename() always returns a correctly typed
dict, bs->options will always be correctly typed after this patch (as
far as I can see, that is).  All other callers of bdrv_open_inherit()
besides bdrv_open() and bdrv_open_string_opts() will inductively fulfill
these conditions, if they are met before them.

Now, of course both assumptions are wrong in the general case.
parse_json_filename() will only return a correctly typed dict if the
user used the correct types, or if all values were strings (which can be
converted with bdrv_type_blockdev_opts()).  Forbidding all other cases
could be seen as a bug fix, though.

But even more importantly, bdrv_open_string_opts() may receive a dict
that does not match the schema because there are string values that do
not match it.
---
 blockdev.c               | 8 ++++++--
 qemu-img.c               | 3 ++-
 qemu-io.c                | 2 +-
 qemu-nbd.c               | 2 +-
 tests/test-replication.c | 6 +++---
 5 files changed, 13 insertions(+), 8 deletions(-)

diff --git a/blockdev.c b/blockdev.c
index 9d4955f23e..3e1125cbfa 100644
--- a/blockdev.c
+++ b/blockdev.c
@@ -595,7 +595,7 @@ static BlockBackend *blockdev_init(const char *file, QDict *bs_opts,
             bdrv_flags |= BDRV_O_INACTIVE;
         }
 
-        blk = blk_new_open(file, NULL, bs_opts, bdrv_flags, errp);
+        blk = blk_new_open_string_opts(file, bs_opts, bdrv_flags, errp);
         if (!blk) {
             goto err_no_bs_opts;
         }
@@ -670,7 +670,11 @@ static BlockDriverState *bds_tree_init(QDict *bs_opts, bool string_opts,
         bdrv_flags |= BDRV_O_INACTIVE;
     }
 
-    return bdrv_open(NULL, NULL, bs_opts, bdrv_flags, errp);
+    if (string_opts) {
+        return bdrv_open_string_opts(NULL, bs_opts, bdrv_flags, errp);
+    } else {
+        return bdrv_open(NULL, NULL, bs_opts, bdrv_flags, errp);
+    }
 }
 
 void blockdev_close_all_bdrv_states(void)
diff --git a/qemu-img.c b/qemu-img.c
index 42b60917b0..a29d76797f 100644
--- a/qemu-img.c
+++ b/qemu-img.c
@@ -284,7 +284,7 @@ static BlockBackend *img_open_opts(const char *optstr,
         }
         qdict_put_str(options, BDRV_OPT_FORCE_SHARE, "on");
     }
-    blk = blk_new_open(NULL, NULL, options, flags, &local_err);
+    blk = blk_new_open_string_opts(NULL, options, flags, &local_err);
     if (!blk) {
         error_reportf_err(local_err, "Could not open '%s': ", optstr);
         return NULL;
@@ -294,6 +294,7 @@ static BlockBackend *img_open_opts(const char *optstr,
     return blk;
 }
 
+/* @options must be correctly typed */
 static BlockBackend *img_open_file(const char *filename,
                                    QDict *options,
                                    const char *fmt, int flags,
diff --git a/qemu-io.c b/qemu-io.c
index 0755a30447..544e47b7fc 100644
--- a/qemu-io.c
+++ b/qemu-io.c
@@ -102,7 +102,7 @@ static int openfile(char *name, int flags, bool writethrough, bool force_share,
         }
         qdict_put_str(opts, BDRV_OPT_FORCE_SHARE, "on");
     }
-    qemuio_blk = blk_new_open(name, NULL, opts, flags, &local_err);
+    qemuio_blk = blk_new_open_string_opts(name, opts, flags, &local_err);
     if (!qemuio_blk) {
         error_reportf_err(local_err, "can't open%s%s: ",
                           name ? " device " : "", name ?: "");
diff --git a/qemu-nbd.c b/qemu-nbd.c
index 0af0560ad1..e5e1877106 100644
--- a/qemu-nbd.c
+++ b/qemu-nbd.c
@@ -944,7 +944,7 @@ int main(int argc, char **argv)
         }
         options = qemu_opts_to_qdict(opts, NULL);
         qemu_opts_reset(&file_opts);
-        blk = blk_new_open(NULL, NULL, options, flags, &local_err);
+        blk = blk_new_open_string_opts(NULL, options, flags, &local_err);
     } else {
         if (fmt) {
             options = qdict_new();
diff --git a/tests/test-replication.c b/tests/test-replication.c
index 68c0d04f2a..5a2bae66fd 100644
--- a/tests/test-replication.c
+++ b/tests/test-replication.c
@@ -191,7 +191,7 @@ static BlockBackend *start_primary(void)
     qdict_set_default_str(qdict, BDRV_OPT_CACHE_DIRECT, "off");
     qdict_set_default_str(qdict, BDRV_OPT_CACHE_NO_FLUSH, "off");
 
-    blk = blk_new_open(NULL, NULL, qdict, BDRV_O_RDWR, &local_err);
+    blk = blk_new_open_string_opts(NULL, qdict, BDRV_O_RDWR, &local_err);
     g_assert(blk);
     g_assert(!local_err);
 
@@ -323,7 +323,7 @@ static BlockBackend *start_secondary(void)
     qdict_set_default_str(qdict, BDRV_OPT_CACHE_DIRECT, "off");
     qdict_set_default_str(qdict, BDRV_OPT_CACHE_NO_FLUSH, "off");
 
-    blk = blk_new_open(NULL, NULL, qdict, BDRV_O_RDWR, &local_err);
+    blk = blk_new_open_string_opts(NULL, qdict, BDRV_O_RDWR, &local_err);
     assert(blk);
     monitor_add_blk(blk, S_LOCAL_DISK_ID, &local_err);
     g_assert(!local_err);
@@ -350,7 +350,7 @@ static BlockBackend *start_secondary(void)
     qdict_set_default_str(qdict, BDRV_OPT_CACHE_DIRECT, "off");
     qdict_set_default_str(qdict, BDRV_OPT_CACHE_NO_FLUSH, "off");
 
-    blk = blk_new_open(NULL, NULL, qdict, BDRV_O_RDWR, &local_err);
+    blk = blk_new_open_string_opts(NULL, qdict, BDRV_O_RDWR, &local_err);
     assert(blk);
     monitor_add_blk(blk, S_ID, &local_err);
     g_assert(!local_err);
-- 
2.14.3

  parent reply	other threads:[~2018-05-02 21:32 UTC|newest]

Thread overview: 12+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2018-05-02 21:32 [Qemu-devel] [RFC 0/7] block: Try to use correctly typed blockdev options Max Reitz
2018-05-02 21:32 ` [Qemu-devel] [RFC 1/7] qdict: Add qdict_set_default_bool() Max Reitz
2018-05-02 21:32 ` [Qemu-devel] [RFC 2/7] block: Let change-medium add detect-zeroes as bool Max Reitz
2018-05-02 21:32 ` [Qemu-devel] [RFC 3/7] block: Make use of qdict_set_default_bool() Max Reitz
2018-05-02 21:32 ` [Qemu-devel] [RFC 4/7] block: Add bdrv_open_string_opts() Max Reitz
2018-05-02 21:32 ` [Qemu-devel] [RFC 5/7] block: Add blk_new_open_string_opts() Max Reitz
2018-05-02 21:32 ` Max Reitz [this message]
2018-05-02 21:32 ` [Qemu-devel] [RFC 7/7] iotests: Test internal option typing Max Reitz
2018-05-02 21:36 ` [Qemu-devel] [Qemu-block] [RFC 0/7] block: Try to use correctly typed blockdev options Max Reitz
2018-05-03  8:16 ` [Qemu-devel] " Markus Armbruster
2018-05-04 15:53   ` Max Reitz
2018-05-04 16:11     ` Max Reitz

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=20180502213219.7842-7-mreitz@redhat.com \
    --to=mreitz@redhat.com \
    --cc=armbru@redhat.com \
    --cc=kwolf@redhat.com \
    --cc=qemu-block@nongnu.org \
    --cc=qemu-devel@nongnu.org \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for NNTP newsgroup(s).