block: Always pass NULL as drv for bdrv_open()
Change all callers of bdrv_open() to pass the driver name in the options QDict instead of passing its BlockDriver pointer. Signed-off-by: Max Reitz <mreitz@redhat.com> Reviewed-by: Alberto Garcia <berto@igalia.com> Signed-off-by: Kevin Wolf <kwolf@redhat.com>
This commit is contained in:
parent
2b750d9d26
commit
e6641719fe
24
block.c
24
block.c
@ -1385,11 +1385,13 @@ int bdrv_append_temp_snapshot(BlockDriverState *bs, int flags, Error **errp)
|
||||
qstring_from_str("file"));
|
||||
qdict_put(snapshot_options, "file.filename",
|
||||
qstring_from_str(tmp_filename));
|
||||
qdict_put(snapshot_options, "driver",
|
||||
qstring_from_str("qcow2"));
|
||||
|
||||
bs_snapshot = bdrv_new();
|
||||
|
||||
ret = bdrv_open(&bs_snapshot, NULL, NULL, snapshot_options,
|
||||
flags, &bdrv_qcow2, &local_err);
|
||||
flags, NULL, &local_err);
|
||||
if (ret < 0) {
|
||||
error_propagate(errp, local_err);
|
||||
goto out;
|
||||
@ -3739,7 +3741,6 @@ void bdrv_img_create(const char *filename, const char *fmt,
|
||||
const char *backing_fmt, *backing_file;
|
||||
int64_t size;
|
||||
BlockDriver *drv, *proto_drv;
|
||||
BlockDriver *backing_drv = NULL;
|
||||
Error *local_err = NULL;
|
||||
int ret = 0;
|
||||
|
||||
@ -3813,14 +3814,6 @@ void bdrv_img_create(const char *filename, const char *fmt,
|
||||
}
|
||||
|
||||
backing_fmt = qemu_opt_get(opts, BLOCK_OPT_BACKING_FMT);
|
||||
if (backing_fmt) {
|
||||
backing_drv = bdrv_find_format(backing_fmt);
|
||||
if (!backing_drv) {
|
||||
error_setg(errp, "Unknown backing file format '%s'",
|
||||
backing_fmt);
|
||||
goto out;
|
||||
}
|
||||
}
|
||||
|
||||
// The size for the image must always be specified, with one exception:
|
||||
// If we are using a backing file, we can obtain the size from there
|
||||
@ -3831,6 +3824,7 @@ void bdrv_img_create(const char *filename, const char *fmt,
|
||||
char *full_backing = g_new0(char, PATH_MAX);
|
||||
int64_t size;
|
||||
int back_flags;
|
||||
QDict *backing_options = NULL;
|
||||
|
||||
bdrv_get_full_backing_filename_from_filename(filename, backing_file,
|
||||
full_backing, PATH_MAX,
|
||||
@ -3844,9 +3838,15 @@ void bdrv_img_create(const char *filename, const char *fmt,
|
||||
back_flags =
|
||||
flags & ~(BDRV_O_RDWR | BDRV_O_SNAPSHOT | BDRV_O_NO_BACKING);
|
||||
|
||||
if (backing_fmt) {
|
||||
backing_options = qdict_new();
|
||||
qdict_put(backing_options, "driver",
|
||||
qstring_from_str(backing_fmt));
|
||||
}
|
||||
|
||||
bs = NULL;
|
||||
ret = bdrv_open(&bs, full_backing, NULL, NULL, back_flags,
|
||||
backing_drv, &local_err);
|
||||
ret = bdrv_open(&bs, full_backing, NULL, backing_options,
|
||||
back_flags, NULL, &local_err);
|
||||
g_free(full_backing);
|
||||
if (ret < 0) {
|
||||
goto out;
|
||||
|
@ -1873,8 +1873,10 @@ static int qcow2_create2(const char *filename, int64_t total_size,
|
||||
QemuOpts *opts, int version, int refcount_order,
|
||||
Error **errp)
|
||||
{
|
||||
/* Calculate cluster_bits */
|
||||
int cluster_bits;
|
||||
QDict *options;
|
||||
|
||||
/* Calculate cluster_bits */
|
||||
cluster_bits = ctz32(cluster_size);
|
||||
if (cluster_bits < MIN_CLUSTER_BITS || cluster_bits > MAX_CLUSTER_BITS ||
|
||||
(1 << cluster_bits) != cluster_size)
|
||||
@ -2032,9 +2034,11 @@ static int qcow2_create2(const char *filename, int64_t total_size,
|
||||
* refcount of the cluster that is occupied by the header and the refcount
|
||||
* table)
|
||||
*/
|
||||
ret = bdrv_open(&bs, filename, NULL, NULL,
|
||||
options = qdict_new();
|
||||
qdict_put(options, "driver", qstring_from_str("qcow2"));
|
||||
ret = bdrv_open(&bs, filename, NULL, options,
|
||||
BDRV_O_RDWR | BDRV_O_CACHE_WB | BDRV_O_NO_FLUSH,
|
||||
&bdrv_qcow2, &local_err);
|
||||
NULL, &local_err);
|
||||
if (ret < 0) {
|
||||
error_propagate(errp, local_err);
|
||||
goto out;
|
||||
@ -2084,9 +2088,11 @@ static int qcow2_create2(const char *filename, int64_t total_size,
|
||||
bs = NULL;
|
||||
|
||||
/* Reopen the image without BDRV_O_NO_FLUSH to flush it before returning */
|
||||
ret = bdrv_open(&bs, filename, NULL, NULL,
|
||||
options = qdict_new();
|
||||
qdict_put(options, "driver", qstring_from_str("qcow2"));
|
||||
ret = bdrv_open(&bs, filename, NULL, options,
|
||||
BDRV_O_RDWR | BDRV_O_CACHE_WB | BDRV_O_NO_BACKING,
|
||||
&bdrv_qcow2, &local_err);
|
||||
NULL, &local_err);
|
||||
if (local_err) {
|
||||
error_propagate(errp, local_err);
|
||||
goto out;
|
||||
|
@ -2926,6 +2926,8 @@ static int enable_write_target(BDRVVVFATState *s, Error **errp)
|
||||
QemuOpts *opts = NULL;
|
||||
int ret;
|
||||
int size = sector2cluster(s, s->sector_count);
|
||||
QDict *options;
|
||||
|
||||
s->used_clusters = calloc(size, 1);
|
||||
|
||||
array_init(&(s->commits), sizeof(commit_t));
|
||||
@ -2956,9 +2958,11 @@ static int enable_write_target(BDRVVVFATState *s, Error **errp)
|
||||
}
|
||||
|
||||
s->qcow = NULL;
|
||||
ret = bdrv_open(&s->qcow, s->qcow_filename, NULL, NULL,
|
||||
options = qdict_new();
|
||||
qdict_put(options, "driver", qstring_from_str("qcow"));
|
||||
ret = bdrv_open(&s->qcow, s->qcow_filename, NULL, options,
|
||||
BDRV_O_RDWR | BDRV_O_CACHE_WB | BDRV_O_NO_FLUSH,
|
||||
bdrv_qcow, errp);
|
||||
NULL, errp);
|
||||
if (ret < 0) {
|
||||
goto err;
|
||||
}
|
||||
|
72
blockdev.c
72
blockdev.c
@ -1422,9 +1422,8 @@ typedef struct ExternalSnapshotState {
|
||||
static void external_snapshot_prepare(BlkTransactionState *common,
|
||||
Error **errp)
|
||||
{
|
||||
BlockDriver *drv;
|
||||
int flags, ret;
|
||||
QDict *options = NULL;
|
||||
QDict *options;
|
||||
Error *local_err = NULL;
|
||||
bool has_device = false;
|
||||
const char *device;
|
||||
@ -1459,12 +1458,6 @@ static void external_snapshot_prepare(BlkTransactionState *common,
|
||||
}
|
||||
|
||||
/* start processing */
|
||||
drv = bdrv_find_format(format);
|
||||
if (!drv) {
|
||||
error_setg(errp, QERR_INVALID_BLOCK_FORMAT, format);
|
||||
return;
|
||||
}
|
||||
|
||||
state->old_bs = bdrv_lookup_bs(has_device ? device : NULL,
|
||||
has_node_name ? node_name : NULL,
|
||||
&local_err);
|
||||
@ -1523,17 +1516,18 @@ static void external_snapshot_prepare(BlkTransactionState *common,
|
||||
}
|
||||
}
|
||||
|
||||
options = qdict_new();
|
||||
if (has_snapshot_node_name) {
|
||||
options = qdict_new();
|
||||
qdict_put(options, "node-name",
|
||||
qstring_from_str(snapshot_node_name));
|
||||
}
|
||||
qdict_put(options, "driver", qstring_from_str(format));
|
||||
|
||||
/* TODO Inherit bs->options or only take explicit options with an
|
||||
* extended QMP command? */
|
||||
assert(state->new_bs == NULL);
|
||||
ret = bdrv_open(&state->new_bs, new_image_file, NULL, options,
|
||||
flags | BDRV_O_NO_BACKING, drv, &local_err);
|
||||
flags | BDRV_O_NO_BACKING, NULL, &local_err);
|
||||
/* We will manually add the backing_hd field to the bs later */
|
||||
if (ret != 0) {
|
||||
error_propagate(errp, local_err);
|
||||
@ -1895,13 +1889,19 @@ void qmp_block_passwd(bool has_device, const char *device,
|
||||
|
||||
/* Assumes AioContext is held */
|
||||
static void qmp_bdrv_open_encrypted(BlockDriverState *bs, const char *filename,
|
||||
int bdrv_flags, BlockDriver *drv,
|
||||
int bdrv_flags, const char *format,
|
||||
const char *password, Error **errp)
|
||||
{
|
||||
Error *local_err = NULL;
|
||||
QDict *options = NULL;
|
||||
int ret;
|
||||
|
||||
ret = bdrv_open(&bs, filename, NULL, NULL, bdrv_flags, drv, &local_err);
|
||||
if (format) {
|
||||
options = qdict_new();
|
||||
qdict_put(options, "driver", qstring_from_str(format));
|
||||
}
|
||||
|
||||
ret = bdrv_open(&bs, filename, NULL, options, bdrv_flags, NULL, &local_err);
|
||||
if (ret < 0) {
|
||||
error_propagate(errp, local_err);
|
||||
return;
|
||||
@ -1916,7 +1916,6 @@ void qmp_change_blockdev(const char *device, const char *filename,
|
||||
BlockBackend *blk;
|
||||
BlockDriverState *bs;
|
||||
AioContext *aio_context;
|
||||
BlockDriver *drv = NULL;
|
||||
int bdrv_flags;
|
||||
Error *err = NULL;
|
||||
|
||||
@ -1931,14 +1930,6 @@ void qmp_change_blockdev(const char *device, const char *filename,
|
||||
aio_context = bdrv_get_aio_context(bs);
|
||||
aio_context_acquire(aio_context);
|
||||
|
||||
if (format) {
|
||||
drv = bdrv_find_whitelisted_format(format, bs->read_only);
|
||||
if (!drv) {
|
||||
error_setg(errp, QERR_INVALID_BLOCK_FORMAT, format);
|
||||
goto out;
|
||||
}
|
||||
}
|
||||
|
||||
eject_device(blk, 0, &err);
|
||||
if (err) {
|
||||
error_propagate(errp, err);
|
||||
@ -1948,7 +1939,7 @@ void qmp_change_blockdev(const char *device, const char *filename,
|
||||
bdrv_flags = bdrv_is_read_only(bs) ? 0 : BDRV_O_RDWR;
|
||||
bdrv_flags |= bdrv_is_snapshot(bs) ? BDRV_O_SNAPSHOT : 0;
|
||||
|
||||
qmp_bdrv_open_encrypted(bs, filename, bdrv_flags, drv, NULL, errp);
|
||||
qmp_bdrv_open_encrypted(bs, filename, bdrv_flags, format, NULL, errp);
|
||||
|
||||
out:
|
||||
aio_context_release(aio_context);
|
||||
@ -2466,7 +2457,7 @@ void qmp_drive_backup(const char *device, const char *target,
|
||||
BlockDriverState *source = NULL;
|
||||
BdrvDirtyBitmap *bmap = NULL;
|
||||
AioContext *aio_context;
|
||||
BlockDriver *drv = NULL;
|
||||
QDict *options = NULL;
|
||||
Error *local_err = NULL;
|
||||
int flags;
|
||||
int64_t size;
|
||||
@ -2506,13 +2497,6 @@ void qmp_drive_backup(const char *device, const char *target,
|
||||
if (!has_format) {
|
||||
format = mode == NEW_IMAGE_MODE_EXISTING ? NULL : bs->drv->format_name;
|
||||
}
|
||||
if (format) {
|
||||
drv = bdrv_find_format(format);
|
||||
if (!drv) {
|
||||
error_setg(errp, QERR_INVALID_BLOCK_FORMAT, format);
|
||||
goto out;
|
||||
}
|
||||
}
|
||||
|
||||
/* Early check to avoid creating target */
|
||||
if (bdrv_op_is_blocked(bs, BLOCK_OP_TYPE_BACKUP_SOURCE, errp)) {
|
||||
@ -2540,7 +2524,7 @@ void qmp_drive_backup(const char *device, const char *target,
|
||||
}
|
||||
|
||||
if (mode != NEW_IMAGE_MODE_EXISTING) {
|
||||
assert(format && drv);
|
||||
assert(format);
|
||||
if (source) {
|
||||
bdrv_img_create(target, format, source->filename,
|
||||
source->drv->format_name, NULL,
|
||||
@ -2556,8 +2540,13 @@ void qmp_drive_backup(const char *device, const char *target,
|
||||
goto out;
|
||||
}
|
||||
|
||||
if (format) {
|
||||
options = qdict_new();
|
||||
qdict_put(options, "driver", qstring_from_str(format));
|
||||
}
|
||||
|
||||
target_bs = NULL;
|
||||
ret = bdrv_open(&target_bs, target, NULL, NULL, flags, drv, &local_err);
|
||||
ret = bdrv_open(&target_bs, target, NULL, options, flags, NULL, &local_err);
|
||||
if (ret < 0) {
|
||||
error_propagate(errp, local_err);
|
||||
goto out;
|
||||
@ -2663,9 +2652,8 @@ void qmp_drive_mirror(const char *device, const char *target,
|
||||
BlockDriverState *bs;
|
||||
BlockDriverState *source, *target_bs;
|
||||
AioContext *aio_context;
|
||||
BlockDriver *drv = NULL;
|
||||
Error *local_err = NULL;
|
||||
QDict *options = NULL;
|
||||
QDict *options;
|
||||
int flags;
|
||||
int64_t size;
|
||||
int ret;
|
||||
@ -2722,13 +2710,6 @@ void qmp_drive_mirror(const char *device, const char *target,
|
||||
if (!has_format) {
|
||||
format = mode == NEW_IMAGE_MODE_EXISTING ? NULL : bs->drv->format_name;
|
||||
}
|
||||
if (format) {
|
||||
drv = bdrv_find_format(format);
|
||||
if (!drv) {
|
||||
error_setg(errp, QERR_INVALID_BLOCK_FORMAT, format);
|
||||
goto out;
|
||||
}
|
||||
}
|
||||
|
||||
if (bdrv_op_is_blocked(bs, BLOCK_OP_TYPE_MIRROR, errp)) {
|
||||
goto out;
|
||||
@ -2783,7 +2764,7 @@ void qmp_drive_mirror(const char *device, const char *target,
|
||||
&& mode != NEW_IMAGE_MODE_EXISTING)
|
||||
{
|
||||
/* create new image w/o backing file */
|
||||
assert(format && drv);
|
||||
assert(format);
|
||||
bdrv_img_create(target, format,
|
||||
NULL, NULL, NULL, size, flags, &local_err, false);
|
||||
} else {
|
||||
@ -2807,17 +2788,20 @@ void qmp_drive_mirror(const char *device, const char *target,
|
||||
goto out;
|
||||
}
|
||||
|
||||
options = qdict_new();
|
||||
if (has_node_name) {
|
||||
options = qdict_new();
|
||||
qdict_put(options, "node-name", qstring_from_str(node_name));
|
||||
}
|
||||
if (format) {
|
||||
qdict_put(options, "driver", qstring_from_str(format));
|
||||
}
|
||||
|
||||
/* Mirroring takes care of copy-on-write using the source's backing
|
||||
* file.
|
||||
*/
|
||||
target_bs = NULL;
|
||||
ret = bdrv_open(&target_bs, target, NULL, options,
|
||||
flags | BDRV_O_NO_BACKING, drv, &local_err);
|
||||
flags | BDRV_O_NO_BACKING, NULL, &local_err);
|
||||
if (ret < 0) {
|
||||
error_propagate(errp, local_err);
|
||||
goto out;
|
||||
|
Loading…
Reference in New Issue
Block a user