block/copy-before-write: refactor option parsing

We are going to add one more option of enum type. Let's refactor option
parsing so that we can simply work with BlockdevOptionsCbw object.

Signed-off-by: Vladimir Sementsov-Ogievskiy <vsementsov@openvz.org>
Reviewed-by: Hanna Reitz <hreitz@redhat.com>
Signed-off-by: Vladimir Sementsov-Ogievskiy <vsementsov@yandex-team.ru>
This commit is contained in:
Vladimir Sementsov-Ogievskiy 2022-04-07 16:27:20 +03:00 committed by Vladimir Sementsov-Ogievskiy
parent ad4c7f529a
commit 79ef0cebb5

View file

@ -24,6 +24,7 @@
*/ */
#include "qemu/osdep.h" #include "qemu/osdep.h"
#include "qapi/qmp/qjson.h"
#include "sysemu/block-backend.h" #include "sysemu/block-backend.h"
#include "qemu/cutils.h" #include "qemu/cutils.h"
@ -328,46 +329,34 @@ static void cbw_child_perm(BlockDriverState *bs, BdrvChild *c,
} }
} }
static bool cbw_parse_bitmap_option(QDict *options, BdrvDirtyBitmap **bitmap, static BlockdevOptions *cbw_parse_options(QDict *options, Error **errp)
Error **errp)
{ {
QDict *bitmap_qdict = NULL; BlockdevOptions *opts = NULL;
BlockDirtyBitmap *bmp_param = NULL;
Visitor *v = NULL; Visitor *v = NULL;
bool ret = false;
*bitmap = NULL; qdict_put_str(options, "driver", "copy-before-write");
qdict_extract_subqdict(options, &bitmap_qdict, "bitmap."); v = qobject_input_visitor_new_flat_confused(options, errp);
if (!qdict_size(bitmap_qdict)) {
ret = true;
goto out;
}
v = qobject_input_visitor_new_flat_confused(bitmap_qdict, errp);
if (!v) { if (!v) {
goto out; goto out;
} }
visit_type_BlockDirtyBitmap(v, NULL, &bmp_param, errp); visit_type_BlockdevOptions(v, NULL, &opts, errp);
if (!bmp_param) { if (!opts) {
goto out; goto out;
} }
*bitmap = block_dirty_bitmap_lookup(bmp_param->node, bmp_param->name, NULL, /*
errp); * Delete options which we are going to parse through BlockdevOptions
if (!*bitmap) { * object for original options.
goto out; */
} qdict_extract_subqdict(options, NULL, "bitmap");
ret = true;
out: out:
qapi_free_BlockDirtyBitmap(bmp_param);
visit_free(v); visit_free(v);
qobject_unref(bitmap_qdict); qdict_del(options, "driver");
return ret; return opts;
} }
static int cbw_open(BlockDriverState *bs, QDict *options, int flags, static int cbw_open(BlockDriverState *bs, QDict *options, int flags,
@ -376,6 +365,15 @@ static int cbw_open(BlockDriverState *bs, QDict *options, int flags,
BDRVCopyBeforeWriteState *s = bs->opaque; BDRVCopyBeforeWriteState *s = bs->opaque;
BdrvDirtyBitmap *bitmap = NULL; BdrvDirtyBitmap *bitmap = NULL;
int64_t cluster_size; int64_t cluster_size;
g_autoptr(BlockdevOptions) full_opts = NULL;
BlockdevOptionsCbw *opts;
full_opts = cbw_parse_options(options, errp);
if (!full_opts) {
return -EINVAL;
}
assert(full_opts->driver == BLOCKDEV_DRIVER_COPY_BEFORE_WRITE);
opts = &full_opts->u.copy_before_write;
bs->file = bdrv_open_child(NULL, options, "file", bs, &child_of_bds, bs->file = bdrv_open_child(NULL, options, "file", bs, &child_of_bds,
BDRV_CHILD_FILTERED | BDRV_CHILD_PRIMARY, BDRV_CHILD_FILTERED | BDRV_CHILD_PRIMARY,
@ -390,8 +388,12 @@ static int cbw_open(BlockDriverState *bs, QDict *options, int flags,
return -EINVAL; return -EINVAL;
} }
if (!cbw_parse_bitmap_option(options, &bitmap, errp)) { if (opts->has_bitmap) {
return -EINVAL; bitmap = block_dirty_bitmap_lookup(opts->bitmap->node,
opts->bitmap->name, NULL, errp);
if (!bitmap) {
return -EINVAL;
}
} }
bs->total_sectors = bs->file->bs->total_sectors; bs->total_sectors = bs->file->bs->total_sectors;