diff mbox

[v3,1/2] block: Do not prematurely remove "filename"

Message ID 1403818727-11215-2-git-send-email-mreitz@redhat.com
State New
Headers show

Commit Message

Max Reitz June 26, 2014, 9:38 p.m. UTC
If "filename" is removed from the options QDict before entering
bdrv_open_common(), it cannot be stored in the BDS.  Therefore, wait
until it has been copied there and remove it from the options only
afterwards.

This fixes "filename" in the BDS being empty for block drivers which do
not need the filename because they have parsed it already (e.g. NBD).

Signed-off-by: Max Reitz <mreitz@redhat.com>
---
 block.c | 35 +++++++++++++++++++++++++++++------
 1 file changed, 29 insertions(+), 6 deletions(-)

Comments

Kevin Wolf June 30, 2014, 9:42 a.m. UTC | #1
Am 26.06.2014 um 23:38 hat Max Reitz geschrieben:
> If "filename" is removed from the options QDict before entering
> bdrv_open_common(), it cannot be stored in the BDS.  Therefore, wait
> until it has been copied there and remove it from the options only
> afterwards.
> 
> This fixes "filename" in the BDS being empty for block drivers which do
> not need the filename because they have parsed it already (e.g. NBD).
> 
> Signed-off-by: Max Reitz <mreitz@redhat.com>

I can't say I like this approach. It looks a bit odd to pass a boolean
variable to bdrv_open(), and in some other function called from there
the cleanup is done that logically really belong to bdrv_fill_options().

More importantly, the goal was to get rid of the filename and handle
everything through the options so that we get a uniform state again.
This would involve replacing bs->filename by a new callback function in
BlockDriver that constructs a filename that describes the BDS. This way
we would get useful output not only for "nbd:localhost:10809", but also
for "driver=nbd,host=localhost".

In hard cases, the callback might just use "json:{...}" syntax. This
suggests that maybe in the end we'll want to have two different
callbacks, one giving a short human-readable description
('localhost:10809') and another one giving something that can be used on
the command line ('json:{"driver": "nbd", "host": "localhost", "ipv6":
true}').

Kevin
Max Reitz July 1, 2014, 11:46 a.m. UTC | #2
On 30.06.2014 11:42, Kevin Wolf wrote:
> Am 26.06.2014 um 23:38 hat Max Reitz geschrieben:
>> If "filename" is removed from the options QDict before entering
>> bdrv_open_common(), it cannot be stored in the BDS.  Therefore, wait
>> until it has been copied there and remove it from the options only
>> afterwards.
>>
>> This fixes "filename" in the BDS being empty for block drivers which do
>> not need the filename because they have parsed it already (e.g. NBD).
>>
>> Signed-off-by: Max Reitz <mreitz@redhat.com>
> I can't say I like this approach. It looks a bit odd to pass a boolean
> variable to bdrv_open(), and in some other function called from there
> the cleanup is done that logically really belong to bdrv_fill_options().
>
> More importantly, the goal was to get rid of the filename and handle
> everything through the options so that we get a uniform state again.
> This would involve replacing bs->filename by a new callback function in
> BlockDriver that constructs a filename that describes the BDS. This way
> we would get useful output not only for "nbd:localhost:10809", but also
> for "driver=nbd,host=localhost".

Right. This "bug" isn't that severe either, so I guess it's fine to take 
more time to fix the general problem.

Max

> In hard cases, the callback might just use "json:{...}" syntax. This
> suggests that maybe in the end we'll want to have two different
> callbacks, one giving a short human-readable description
> ('localhost:10809') and another one giving something that can be used on
> the command line ('json:{"driver": "nbd", "host": "localhost", "ipv6":
> true}').
>
> Kevin
diff mbox

Patch

diff --git a/block.c b/block.c
index 7d69f31..beab752 100644
--- a/block.c
+++ b/block.c
@@ -881,7 +881,8 @@  static void bdrv_assign_node_name(BlockDriverState *bs,
  * Removes all processed options from *options.
  */
 static int bdrv_open_common(BlockDriverState *bs, BlockDriverState *file,
-    QDict *options, int flags, BlockDriver *drv, Error **errp)
+    QDict *options, int flags, BlockDriver *drv, bool remove_filename,
+    Error **errp)
 {
     int ret, open_flags;
     const char *filename;
@@ -955,6 +956,20 @@  static int bdrv_open_common(BlockDriverState *bs, BlockDriverState *file,
         bs->filename[0] = '\0';
     }
 
+    if (remove_filename) {
+        qdict_del(options, "filename");
+
+        /* remove_filename is set by bdrv_fill_options() only if
+         * bdrv_needs_filename is false */
+        assert(!drv->bdrv_needs_filename);
+        /* The pointer "filename" may reference the just deleted QDict entry; in
+         * any case, it is only read once from here on and that is for
+         * determining whether it is set in case bdrv_needs_filename is true.
+         * Because bdrv_needs_filename is false here, that means it is not used
+         * at all anymore, so indicate that by setting it to NULL. */
+        filename = NULL;
+    }
+
     bs->drv = drv;
     bs->opaque = g_malloc0(drv->instance_size);
 
@@ -1036,9 +1051,14 @@  static QDict *parse_json_filename(const char *filename, Error **errp)
 /*
  * Fills in default options for opening images and converts the legacy
  * filename/flags pair to option QDict entries.
+ *
+ * *remove_filename is set to true if the "filename" entry should be removed
+ * from the option QDict before drv->bdrv_open() or drv->bdrv_file_open() is
+ * called.
  */
-static int bdrv_fill_options(QDict **options, const char **pfilename, int flags,
-                             BlockDriver *drv, Error **errp)
+static int bdrv_fill_options(QDict **options, const char **pfilename,
+                             bool *remove_filename, int flags, BlockDriver *drv,
+                             Error **errp)
 {
     const char *filename = *pfilename;
     const char *drvname;
@@ -1119,7 +1139,7 @@  static int bdrv_fill_options(QDict **options, const char **pfilename, int flags,
         }
 
         if (!drv->bdrv_needs_filename) {
-            qdict_del(*options, "filename");
+            *remove_filename = true;
         }
     }
 
@@ -1368,6 +1388,7 @@  int bdrv_open(BlockDriverState **pbs, const char *filename,
     const char *drvname;
     Error *local_err = NULL;
     int snapshot_flags = 0;
+    bool remove_filename = false;
 
     assert(pbs);
 
@@ -1407,7 +1428,8 @@  int bdrv_open(BlockDriverState **pbs, const char *filename,
         options = qdict_new();
     }
 
-    ret = bdrv_fill_options(&options, &filename, flags, drv, &local_err);
+    ret = bdrv_fill_options(&options, &filename, &remove_filename, flags, drv,
+                            &local_err);
     if (local_err) {
         goto fail;
     }
@@ -1467,7 +1489,8 @@  int bdrv_open(BlockDriverState **pbs, const char *filename,
     }
 
     /* Open the image */
-    ret = bdrv_open_common(bs, file, options, flags, drv, &local_err);
+    ret = bdrv_open_common(bs, file, options, flags, drv, remove_filename,
+                           &local_err);
     if (ret < 0) {
         goto fail;
     }