block: Introduce .bdrv_parse_filename callback

If a driver needs structured data and not just a string, it can provide
a .bdrv_parse_filename callback now that parses the command line string
into separate options. Keeping this separate from .bdrv_open_filename
ensures that the preferred way of directly specifying the options always
works as well if parsing the string works.

Signed-off-by: Kevin Wolf <kwolf@redhat.com>
Reviewed-by: Eric Blake <eblake@redhat.com>
This commit is contained in:
Kevin Wolf 2013-03-15 18:47:22 +01:00
parent f53a1febcd
commit 6963a30d82
3 changed files with 25 additions and 16 deletions

11
block.c
View File

@ -770,6 +770,17 @@ 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);
if (drv->bdrv_parse_filename) {
Error *local_err = NULL;
drv->bdrv_parse_filename(filename, options, &local_err);
if (error_is_set(&local_err)) {
qerror_report_err(local_err);
error_free(local_err);
ret = -EINVAL;
goto fail;
}
}
ret = bdrv_open_common(bs, NULL, filename, options, flags, drv); ret = bdrv_open_common(bs, NULL, filename, options, flags, drv);
if (ret < 0) { if (ret < 0) {
goto fail; goto fail;

View File

@ -143,17 +143,20 @@ out:
return ret; return ret;
} }
static int nbd_parse_filename(const char *filename, QDict *options) static void nbd_parse_filename(const char *filename, QDict *options,
Error **errp)
{ {
char *file; char *file;
char *export_name; char *export_name;
const char *host_spec; const char *host_spec;
const char *unixpath; const char *unixpath;
int ret = -EINVAL;
Error *local_err = NULL;
if (strstr(filename, "://")) { if (strstr(filename, "://")) {
return nbd_parse_uri(filename, options); int ret = nbd_parse_uri(filename, options);
if (ret < 0) {
error_setg(errp, "No valid URL specified");
}
return;
} }
file = g_strdup(filename); file = g_strdup(filename);
@ -171,11 +174,11 @@ static int nbd_parse_filename(const char *filename, QDict *options)
/* extract the host_spec - fail if it's not nbd:... */ /* extract the host_spec - fail if it's not nbd:... */
if (!strstart(file, "nbd:", &host_spec)) { if (!strstart(file, "nbd:", &host_spec)) {
error_setg(errp, "File name string for NBD must start with 'nbd:'");
goto out; goto out;
} }
if (!*host_spec) { if (!*host_spec) {
ret = 1;
goto out; goto out;
} }
@ -185,10 +188,8 @@ static int nbd_parse_filename(const char *filename, QDict *options)
} else { } else {
InetSocketAddress *addr = NULL; InetSocketAddress *addr = NULL;
addr = inet_parse(host_spec, &local_err); addr = inet_parse(host_spec, errp);
if (local_err != NULL) { if (error_is_set(errp)) {
qerror_report_err(local_err);
error_free(local_err);
goto out; goto out;
} }
@ -197,10 +198,8 @@ static int nbd_parse_filename(const char *filename, QDict *options)
qapi_free_InetSocketAddress(addr); qapi_free_InetSocketAddress(addr);
} }
ret = 1;
out: out:
g_free(file); g_free(file);
return ret;
} }
static int nbd_config(BDRVNBDState *s, QDict *options) static int nbd_config(BDRVNBDState *s, QDict *options)
@ -437,11 +436,6 @@ static int nbd_open(BlockDriverState *bs, const char* filename,
qemu_co_mutex_init(&s->free_sema); qemu_co_mutex_init(&s->free_sema);
/* Pop the config into our state object. Exit if invalid. */ /* Pop the config into our state object. Exit if invalid. */
result = nbd_parse_filename(filename, options);
if (result < 0) {
return result;
}
result = nbd_config(s, options); result = nbd_config(s, options);
if (result != 0) { if (result != 0) {
return result; return result;
@ -622,6 +616,7 @@ static BlockDriver bdrv_nbd = {
.format_name = "nbd", .format_name = "nbd",
.protocol_name = "nbd", .protocol_name = "nbd",
.instance_size = sizeof(BDRVNBDState), .instance_size = sizeof(BDRVNBDState),
.bdrv_parse_filename = nbd_parse_filename,
.bdrv_file_open = nbd_open, .bdrv_file_open = nbd_open,
.bdrv_co_readv = nbd_co_readv, .bdrv_co_readv = nbd_co_readv,
.bdrv_co_writev = nbd_co_writev, .bdrv_co_writev = nbd_co_writev,
@ -635,6 +630,7 @@ static BlockDriver bdrv_nbd_tcp = {
.format_name = "nbd", .format_name = "nbd",
.protocol_name = "nbd+tcp", .protocol_name = "nbd+tcp",
.instance_size = sizeof(BDRVNBDState), .instance_size = sizeof(BDRVNBDState),
.bdrv_parse_filename = nbd_parse_filename,
.bdrv_file_open = nbd_open, .bdrv_file_open = nbd_open,
.bdrv_co_readv = nbd_co_readv, .bdrv_co_readv = nbd_co_readv,
.bdrv_co_writev = nbd_co_writev, .bdrv_co_writev = nbd_co_writev,
@ -648,6 +644,7 @@ static BlockDriver bdrv_nbd_unix = {
.format_name = "nbd", .format_name = "nbd",
.protocol_name = "nbd+unix", .protocol_name = "nbd+unix",
.instance_size = sizeof(BDRVNBDState), .instance_size = sizeof(BDRVNBDState),
.bdrv_parse_filename = nbd_parse_filename,
.bdrv_file_open = nbd_open, .bdrv_file_open = nbd_open,
.bdrv_co_readv = nbd_co_readv, .bdrv_co_readv = nbd_co_readv,
.bdrv_co_writev = nbd_co_writev, .bdrv_co_writev = nbd_co_writev,

View File

@ -75,6 +75,7 @@ struct BlockDriver {
int instance_size; int instance_size;
int (*bdrv_probe)(const uint8_t *buf, int buf_size, const char *filename); int (*bdrv_probe)(const uint8_t *buf, int buf_size, const char *filename);
int (*bdrv_probe_device)(const char *filename); int (*bdrv_probe_device)(const char *filename);
void (*bdrv_parse_filename)(const char *filename, QDict *options, Error **errp);
/* For handling image reopen for split or non-split files */ /* For handling image reopen for split or non-split files */
int (*bdrv_reopen_prepare)(BDRVReopenState *reopen_state, int (*bdrv_reopen_prepare)(BDRVReopenState *reopen_state,