block: Allow reference for bdrv_file_open()
Allow specifying a reference to an existing block device (by name) for bdrv_file_open() instead of a filename and/or options. Signed-off-by: Max Reitz <mreitz@redhat.com> Reviewed-by: Kevin Wolf <kwolf@redhat.com> Signed-off-by: Kevin Wolf <kwolf@redhat.com>
This commit is contained in:
parent
89f2b21e36
commit
72daa72eee
25
block.c
25
block.c
@ -858,9 +858,10 @@ free_and_fail:
|
|||||||
* dictionary, it needs to use QINCREF() before calling bdrv_file_open.
|
* dictionary, it needs to use QINCREF() before calling bdrv_file_open.
|
||||||
*/
|
*/
|
||||||
int bdrv_file_open(BlockDriverState **pbs, const char *filename,
|
int bdrv_file_open(BlockDriverState **pbs, const char *filename,
|
||||||
QDict *options, int flags, Error **errp)
|
const char *reference, QDict *options, int flags,
|
||||||
|
Error **errp)
|
||||||
{
|
{
|
||||||
BlockDriverState *bs;
|
BlockDriverState *bs = NULL;
|
||||||
BlockDriver *drv;
|
BlockDriver *drv;
|
||||||
const char *drvname;
|
const char *drvname;
|
||||||
bool allow_protocol_prefix = false;
|
bool allow_protocol_prefix = false;
|
||||||
@ -872,6 +873,24 @@ int bdrv_file_open(BlockDriverState **pbs, const char *filename,
|
|||||||
options = qdict_new();
|
options = qdict_new();
|
||||||
}
|
}
|
||||||
|
|
||||||
|
if (reference) {
|
||||||
|
if (filename || qdict_size(options)) {
|
||||||
|
error_setg(errp, "Cannot reference an existing block device with "
|
||||||
|
"additional options or a new filename");
|
||||||
|
return -EINVAL;
|
||||||
|
}
|
||||||
|
QDECREF(options);
|
||||||
|
|
||||||
|
bs = bdrv_find(reference);
|
||||||
|
if (!bs) {
|
||||||
|
error_setg(errp, "Cannot find block device '%s'", reference);
|
||||||
|
return -ENODEV;
|
||||||
|
}
|
||||||
|
bdrv_ref(bs);
|
||||||
|
*pbs = bs;
|
||||||
|
return 0;
|
||||||
|
}
|
||||||
|
|
||||||
bs = bdrv_new("");
|
bs = bdrv_new("");
|
||||||
bs->options = options;
|
bs->options = options;
|
||||||
options = qdict_clone_shallow(options);
|
options = qdict_clone_shallow(options);
|
||||||
@ -1124,7 +1143,7 @@ int bdrv_open(BlockDriverState *bs, const char *filename, QDict *options,
|
|||||||
|
|
||||||
qdict_extract_subqdict(options, &file_options, "file.");
|
qdict_extract_subqdict(options, &file_options, "file.");
|
||||||
|
|
||||||
ret = bdrv_file_open(&file, filename, file_options,
|
ret = bdrv_file_open(&file, filename, NULL, file_options,
|
||||||
bdrv_open_flags(bs, flags | BDRV_O_UNMAP), &local_err);
|
bdrv_open_flags(bs, flags | BDRV_O_UNMAP), &local_err);
|
||||||
if (ret < 0) {
|
if (ret < 0) {
|
||||||
goto fail;
|
goto fail;
|
||||||
|
@ -403,7 +403,7 @@ static int blkdebug_open(BlockDriverState *bs, QDict *options, int flags,
|
|||||||
goto fail;
|
goto fail;
|
||||||
}
|
}
|
||||||
|
|
||||||
ret = bdrv_file_open(&bs->file, filename, NULL, flags, &local_err);
|
ret = bdrv_file_open(&bs->file, filename, NULL, NULL, flags, &local_err);
|
||||||
if (ret < 0) {
|
if (ret < 0) {
|
||||||
error_propagate(errp, local_err);
|
error_propagate(errp, local_err);
|
||||||
goto fail;
|
goto fail;
|
||||||
|
@ -141,7 +141,7 @@ static int blkverify_open(BlockDriverState *bs, QDict *options, int flags,
|
|||||||
goto fail;
|
goto fail;
|
||||||
}
|
}
|
||||||
|
|
||||||
ret = bdrv_file_open(&bs->file, raw, NULL, flags, &local_err);
|
ret = bdrv_file_open(&bs->file, raw, NULL, NULL, flags, &local_err);
|
||||||
if (ret < 0) {
|
if (ret < 0) {
|
||||||
error_propagate(errp, local_err);
|
error_propagate(errp, local_err);
|
||||||
goto fail;
|
goto fail;
|
||||||
|
@ -351,7 +351,8 @@ static int cow_create(const char *filename, QEMUOptionParameter *options,
|
|||||||
return ret;
|
return ret;
|
||||||
}
|
}
|
||||||
|
|
||||||
ret = bdrv_file_open(&cow_bs, filename, NULL, BDRV_O_RDWR, &local_err);
|
ret = bdrv_file_open(&cow_bs, filename, NULL, NULL, BDRV_O_RDWR,
|
||||||
|
&local_err);
|
||||||
if (ret < 0) {
|
if (ret < 0) {
|
||||||
qerror_report_err(local_err);
|
qerror_report_err(local_err);
|
||||||
error_free(local_err);
|
error_free(local_err);
|
||||||
|
@ -691,7 +691,8 @@ static int qcow_create(const char *filename, QEMUOptionParameter *options,
|
|||||||
return ret;
|
return ret;
|
||||||
}
|
}
|
||||||
|
|
||||||
ret = bdrv_file_open(&qcow_bs, filename, NULL, BDRV_O_RDWR, &local_err);
|
ret = bdrv_file_open(&qcow_bs, filename, NULL, NULL, BDRV_O_RDWR,
|
||||||
|
&local_err);
|
||||||
if (ret < 0) {
|
if (ret < 0) {
|
||||||
qerror_report_err(local_err);
|
qerror_report_err(local_err);
|
||||||
error_free(local_err);
|
error_free(local_err);
|
||||||
|
@ -1483,7 +1483,7 @@ static int qcow2_create2(const char *filename, int64_t total_size,
|
|||||||
return ret;
|
return ret;
|
||||||
}
|
}
|
||||||
|
|
||||||
ret = bdrv_file_open(&bs, filename, NULL, BDRV_O_RDWR, &local_err);
|
ret = bdrv_file_open(&bs, filename, NULL, NULL, BDRV_O_RDWR, &local_err);
|
||||||
if (ret < 0) {
|
if (ret < 0) {
|
||||||
error_propagate(errp, local_err);
|
error_propagate(errp, local_err);
|
||||||
return ret;
|
return ret;
|
||||||
|
@ -563,8 +563,8 @@ static int qed_create(const char *filename, uint32_t cluster_size,
|
|||||||
return ret;
|
return ret;
|
||||||
}
|
}
|
||||||
|
|
||||||
ret = bdrv_file_open(&bs, filename, NULL, BDRV_O_RDWR | BDRV_O_CACHE_WB,
|
ret = bdrv_file_open(&bs, filename, NULL, NULL,
|
||||||
&local_err);
|
BDRV_O_RDWR | BDRV_O_CACHE_WB, &local_err);
|
||||||
if (ret < 0) {
|
if (ret < 0) {
|
||||||
qerror_report_err(local_err);
|
qerror_report_err(local_err);
|
||||||
error_free(local_err);
|
error_free(local_err);
|
||||||
|
@ -1534,7 +1534,7 @@ static int sd_prealloc(const char *filename)
|
|||||||
Error *local_err = NULL;
|
Error *local_err = NULL;
|
||||||
int ret;
|
int ret;
|
||||||
|
|
||||||
ret = bdrv_file_open(&bs, filename, NULL, BDRV_O_RDWR, &local_err);
|
ret = bdrv_file_open(&bs, filename, NULL, NULL, BDRV_O_RDWR, &local_err);
|
||||||
if (ret < 0) {
|
if (ret < 0) {
|
||||||
qerror_report_err(local_err);
|
qerror_report_err(local_err);
|
||||||
error_free(local_err);
|
error_free(local_err);
|
||||||
@ -1695,7 +1695,7 @@ static int sd_create(const char *filename, QEMUOptionParameter *options,
|
|||||||
goto out;
|
goto out;
|
||||||
}
|
}
|
||||||
|
|
||||||
ret = bdrv_file_open(&bs, backing_file, NULL, 0, &local_err);
|
ret = bdrv_file_open(&bs, backing_file, NULL, NULL, 0, &local_err);
|
||||||
if (ret < 0) {
|
if (ret < 0) {
|
||||||
qerror_report_err(local_err);
|
qerror_report_err(local_err);
|
||||||
error_free(local_err);
|
error_free(local_err);
|
||||||
|
@ -1797,7 +1797,7 @@ static int vhdx_create(const char *filename, QEMUOptionParameter *options,
|
|||||||
goto exit;
|
goto exit;
|
||||||
}
|
}
|
||||||
|
|
||||||
ret = bdrv_file_open(&bs, filename, NULL, BDRV_O_RDWR, &local_err);
|
ret = bdrv_file_open(&bs, filename, NULL, NULL, BDRV_O_RDWR, &local_err);
|
||||||
if (ret < 0) {
|
if (ret < 0) {
|
||||||
error_propagate(errp, local_err);
|
error_propagate(errp, local_err);
|
||||||
goto exit;
|
goto exit;
|
||||||
|
@ -769,8 +769,8 @@ static int vmdk_parse_extents(const char *desc, BlockDriverState *bs,
|
|||||||
|
|
||||||
path_combine(extent_path, sizeof(extent_path),
|
path_combine(extent_path, sizeof(extent_path),
|
||||||
desc_file_path, fname);
|
desc_file_path, fname);
|
||||||
ret = bdrv_file_open(&extent_file, extent_path, NULL, bs->open_flags,
|
ret = bdrv_file_open(&extent_file, extent_path, NULL, NULL,
|
||||||
errp);
|
bs->open_flags, errp);
|
||||||
if (ret) {
|
if (ret) {
|
||||||
return ret;
|
return ret;
|
||||||
}
|
}
|
||||||
@ -1469,7 +1469,7 @@ static int vmdk_create_extent(const char *filename, int64_t filesize,
|
|||||||
goto exit;
|
goto exit;
|
||||||
}
|
}
|
||||||
|
|
||||||
ret = bdrv_file_open(&bs, filename, NULL, BDRV_O_RDWR, &local_err);
|
ret = bdrv_file_open(&bs, filename, NULL, NULL, BDRV_O_RDWR, &local_err);
|
||||||
if (ret < 0) {
|
if (ret < 0) {
|
||||||
error_propagate(errp, local_err);
|
error_propagate(errp, local_err);
|
||||||
goto exit;
|
goto exit;
|
||||||
@ -1807,7 +1807,7 @@ static int vmdk_create(const char *filename, QEMUOptionParameter *options,
|
|||||||
goto exit;
|
goto exit;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
ret = bdrv_file_open(&new_bs, filename, NULL, BDRV_O_RDWR, &local_err);
|
ret = bdrv_file_open(&new_bs, filename, NULL, NULL, BDRV_O_RDWR, &local_err);
|
||||||
if (ret < 0) {
|
if (ret < 0) {
|
||||||
error_setg_errno(errp, -ret, "Could not write description");
|
error_setg_errno(errp, -ret, "Could not write description");
|
||||||
goto exit;
|
goto exit;
|
||||||
|
@ -184,7 +184,8 @@ void bdrv_append(BlockDriverState *bs_new, BlockDriverState *bs_top);
|
|||||||
int bdrv_parse_cache_flags(const char *mode, int *flags);
|
int bdrv_parse_cache_flags(const char *mode, int *flags);
|
||||||
int bdrv_parse_discard_flags(const char *mode, int *flags);
|
int bdrv_parse_discard_flags(const char *mode, int *flags);
|
||||||
int bdrv_file_open(BlockDriverState **pbs, const char *filename,
|
int bdrv_file_open(BlockDriverState **pbs, const char *filename,
|
||||||
QDict *options, int flags, Error **errp);
|
const char *reference, QDict *options, int flags,
|
||||||
|
Error **errp);
|
||||||
int bdrv_open_backing_file(BlockDriverState *bs, QDict *options, Error **errp);
|
int bdrv_open_backing_file(BlockDriverState *bs, QDict *options, Error **errp);
|
||||||
int bdrv_open(BlockDriverState *bs, const char *filename, QDict *options,
|
int bdrv_open(BlockDriverState *bs, const char *filename, QDict *options,
|
||||||
int flags, BlockDriver *drv, Error **errp);
|
int flags, BlockDriver *drv, Error **errp);
|
||||||
|
@ -59,7 +59,7 @@ static int openfile(char *name, int flags, int growable, QDict *opts)
|
|||||||
}
|
}
|
||||||
|
|
||||||
if (growable) {
|
if (growable) {
|
||||||
if (bdrv_file_open(&qemuio_bs, name, opts, flags, &local_err)) {
|
if (bdrv_file_open(&qemuio_bs, name, NULL, opts, flags, &local_err)) {
|
||||||
fprintf(stderr, "%s: can't open device %s: %s\n", progname, name,
|
fprintf(stderr, "%s: can't open device %s: %s\n", progname, name,
|
||||||
error_get_pretty(local_err));
|
error_get_pretty(local_err));
|
||||||
error_free(local_err);
|
error_free(local_err);
|
||||||
|
Loading…
Reference in New Issue
Block a user