The idiom of "blk_new() + blk_insert_bs()" is common. Use a new BB interface that does both things to make the coming transition that adds AioContext to BB easier.
The added blk_new_with_ctx doesn't handle the passed ctx and is purely here to avoid a small code churn. Signed-off-by: Fam Zheng <f...@redhat.com> --- block/backup.c | 3 +-- block/block-backend.c | 24 ++++++++++++++++++------ block/commit.c | 12 ++++-------- block/mirror.c | 3 +-- blockjob.c | 3 +-- hmp.c | 3 +-- hw/core/qdev-properties-system.c | 3 +-- include/sysemu/block-backend.h | 1 + nbd/server.c | 3 +-- tests/test-blockjob.c | 3 +-- 10 files changed, 30 insertions(+), 28 deletions(-) diff --git a/block/backup.c b/block/backup.c index 582bd0f..3eed9a1 100644 --- a/block/backup.c +++ b/block/backup.c @@ -601,8 +601,7 @@ void backup_start(const char *job_id, BlockDriverState *bs, goto error; } - job->target = blk_new(); - blk_insert_bs(job->target, target); + job->target = blk_new_with_root(target); job->on_source_error = on_source_error; job->on_target_error = on_target_error; diff --git a/block/block-backend.c b/block/block-backend.c index 0bd19ab..b71babe 100644 --- a/block/block-backend.c +++ b/block/block-backend.c @@ -115,12 +115,7 @@ static const BdrvChildRole child_root = { .drained_end = blk_root_drained_end, }; -/* - * Create a new BlockBackend with a reference count of one. - * Store an error through @errp on failure, unless it's null. - * Return the new BlockBackend on success, null on failure. - */ -BlockBackend *blk_new(void) +static BlockBackend *blk_new_with_ctx(AioContext *ctx) { BlockBackend *blk; @@ -139,6 +134,23 @@ BlockBackend *blk_new(void) } /* + * Create a new BlockBackend with a reference count of one. + * Store an error through @errp on failure, unless it's null. + * Return the new BlockBackend on success, null on failure. + */ +BlockBackend *blk_new(void) +{ + return blk_new_with_ctx(qemu_get_aio_context()); +} + +BlockBackend *blk_new_with_root(BlockDriverState *root) +{ + BlockBackend *blk = blk_new_with_ctx(bdrv_get_aio_context(root)); + blk_insert_bs(blk, root); + return blk; +} + +/* * Creates a new BlockBackend, opens a new BlockDriverState, and connects both. * * Just as with bdrv_open(), after having called this function the reference to diff --git a/block/commit.c b/block/commit.c index 9f67a8b..45d4f96 100644 --- a/block/commit.c +++ b/block/commit.c @@ -260,11 +260,9 @@ void commit_start(const char *job_id, BlockDriverState *bs, } - s->base = blk_new(); - blk_insert_bs(s->base, base); + s->base = blk_new_with_root(base); - s->top = blk_new(); - blk_insert_bs(s->top, top); + s->top = blk_new_with_root(top); s->active = bs; @@ -314,11 +312,9 @@ int bdrv_commit(BlockDriverState *bs) } } - src = blk_new(); - blk_insert_bs(src, bs); + src = blk_new_with_root(bs); - backing = blk_new(); - blk_insert_bs(backing, bs->backing->bs); + backing = blk_new_with_root(bs->backing->bs); length = blk_getlength(src); if (length < 0) { diff --git a/block/mirror.c b/block/mirror.c index f9d1fec..8910d53 100644 --- a/block/mirror.c +++ b/block/mirror.c @@ -941,8 +941,7 @@ static void mirror_start_job(const char *job_id, BlockDriverState *bs, return; } - s->target = blk_new(); - blk_insert_bs(s->target, target); + s->target = blk_new_with_root(target); s->replaces = g_strdup(replaces); s->on_source_error = on_source_error; diff --git a/blockjob.c b/blockjob.c index a167f96..8fe6d5d 100644 --- a/blockjob.c +++ b/blockjob.c @@ -148,8 +148,7 @@ void *block_job_create(const char *job_id, const BlockJobDriver *driver, return NULL; } - blk = blk_new(); - blk_insert_bs(blk, bs); + blk = blk_new_with_root(bs); job = g_malloc0(driver->instance_size); error_setg(&job->blocker, "block device is in use by block job: %s", diff --git a/hmp.c b/hmp.c index 336e7bf..1ee83d2 100644 --- a/hmp.c +++ b/hmp.c @@ -1934,8 +1934,7 @@ void hmp_qemu_io(Monitor *mon, const QDict *qdict) if (!blk) { BlockDriverState *bs = bdrv_lookup_bs(NULL, device, &err); if (bs) { - blk = local_blk = blk_new(); - blk_insert_bs(blk, bs); + blk = local_blk = blk_new_with_root(bs); } else { goto fail; } diff --git a/hw/core/qdev-properties-system.c b/hw/core/qdev-properties-system.c index e55afe6..2b07beb 100644 --- a/hw/core/qdev-properties-system.c +++ b/hw/core/qdev-properties-system.c @@ -78,8 +78,7 @@ static void parse_drive(DeviceState *dev, const char *str, void **ptr, if (!blk) { BlockDriverState *bs = bdrv_lookup_bs(NULL, str, NULL); if (bs) { - blk = blk_new(); - blk_insert_bs(blk, bs); + blk = blk_new_with_root(bs); blk_created = true; } } diff --git a/include/sysemu/block-backend.h b/include/sysemu/block-backend.h index 3b29317..a25ee69 100644 --- a/include/sysemu/block-backend.h +++ b/include/sysemu/block-backend.h @@ -79,6 +79,7 @@ typedef struct BlockBackendPublic { } BlockBackendPublic; BlockBackend *blk_new(void); +BlockBackend *blk_new_with_root(BlockDriverState *root); BlockBackend *blk_new_open(const char *filename, const char *reference, QDict *options, int flags, Error **errp); int blk_get_refcnt(BlockBackend *blk); diff --git a/nbd/server.c b/nbd/server.c index 472f584..c965cce 100644 --- a/nbd/server.c +++ b/nbd/server.c @@ -816,8 +816,7 @@ NBDExport *nbd_export_new(BlockDriverState *bs, off_t dev_offset, off_t size, BlockBackend *blk; NBDExport *exp = g_malloc0(sizeof(NBDExport)); - blk = blk_new(); - blk_insert_bs(blk, bs); + blk = blk_new_with_root(bs); blk_set_enable_write_cache(blk, !writethrough); exp->refcount = 1; diff --git a/tests/test-blockjob.c b/tests/test-blockjob.c index 5b0e934..74dc750 100644 --- a/tests/test-blockjob.c +++ b/tests/test-blockjob.c @@ -53,10 +53,9 @@ static BlockJob *do_test_id(BlockBackend *blk, const char *id, * BlockDriverState inserted. */ static BlockBackend *create_blk(const char *name) { - BlockBackend *blk = blk_new(); BlockDriverState *bs = bdrv_new(); + BlockBackend *blk = blk_new_with_root(bs); - blk_insert_bs(blk, bs); bdrv_unref(bs); if (name) { -- 2.7.4