block: Enable filename option

This allows using the file.filename option instead of the string that
comes from -drive file=... and is passed around as a separate parameter.
The goal is to get rid of this parameter and use the options QDict more
consistently.

With this option you can access not only the top-level image, but
specify a filename for the backing file (currently only if no backing
file exists, but we'll allow overriding it later)

Signed-off-by: Kevin Wolf <kwolf@redhat.com>
Reviewed-by: Eric Blake <eblake@redhat.com>
This commit is contained in:
Kevin Wolf 2013-04-09 14:34:19 +02:00
parent 31ca6d077c
commit 035fccdf79

31
block.c
View File

@ -667,10 +667,10 @@ static int bdrv_open_flags(BlockDriverState *bs, int flags)
* Removes all processed options from *options. * Removes all processed options from *options.
*/ */
static int bdrv_open_common(BlockDriverState *bs, BlockDriverState *file, static int bdrv_open_common(BlockDriverState *bs, BlockDriverState *file,
const char *filename, QDict *options, QDict *options, int flags, BlockDriver *drv)
int flags, BlockDriver *drv)
{ {
int ret, open_flags; int ret, open_flags;
const char *filename;
assert(drv != NULL); assert(drv != NULL);
assert(bs->file == NULL); assert(bs->file == NULL);
@ -698,6 +698,12 @@ static int bdrv_open_common(BlockDriverState *bs, BlockDriverState *file,
bdrv_enable_copy_on_read(bs); bdrv_enable_copy_on_read(bs);
} }
if (file != NULL) {
filename = file->filename;
} else {
filename = qdict_get_try_str(options, "filename");
}
if (filename != NULL) { if (filename != NULL) {
pstrcpy(bs->filename, sizeof(bs->filename), filename); pstrcpy(bs->filename, sizeof(bs->filename), filename);
} else { } else {
@ -780,6 +786,18 @@ int bdrv_file_open(BlockDriverState **pbs, const char *filename,
bs->options = options; bs->options = options;
options = qdict_clone_shallow(options); options = qdict_clone_shallow(options);
/* Fetch the file name from the options QDict if necessary */
if (!filename) {
filename = qdict_get_try_str(options, "filename");
} else if (filename && !qdict_haskey(options, "filename")) {
qdict_put(options, "filename", qstring_from_str(filename));
} else {
qerror_report(ERROR_CLASS_GENERIC_ERROR, "Can't specify 'file' and "
"'filename' options at the same time");
ret = -EINVAL;
goto fail;
}
/* Find the right block driver */ /* Find the right block driver */
drvname = qdict_get_try_str(options, "driver"); drvname = qdict_get_try_str(options, "driver");
if (drvname) { if (drvname) {
@ -816,11 +834,16 @@ int bdrv_file_open(BlockDriverState **pbs, const char *filename,
goto fail; goto fail;
} }
ret = bdrv_open_common(bs, NULL, filename, options, flags, drv); ret = bdrv_open_common(bs, NULL, options, flags, drv);
if (ret < 0) { if (ret < 0) {
goto fail; goto fail;
} }
/* TODO Remove once all protocols know the filename option */
if (qdict_haskey(options, "filename")) {
qdict_del(options, "filename");
}
/* Check if any unknown options were used */ /* Check if any unknown options were used */
if (qdict_size(options) != 0) { if (qdict_size(options) != 0) {
const QDictEntry *entry = qdict_first(options); const QDictEntry *entry = qdict_first(options);
@ -1031,7 +1054,7 @@ int bdrv_open(BlockDriverState *bs, const char *filename, QDict *options,
} }
/* Open the image */ /* Open the image */
ret = bdrv_open_common(bs, file, filename, options, flags, drv); ret = bdrv_open_common(bs, file, options, flags, drv);
if (ret < 0) { if (ret < 0) {
goto unlink_and_fail; goto unlink_and_fail;
} }