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 <mre...@redhat.com> --- block.c | 35 +++++++++++++++++++++++++++++------ 1 file changed, 29 insertions(+), 6 deletions(-) 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; } -- 2.0.0