block-job: drop BlockJob.blk
cover block-job: drop BlockJob.blk
Hi all!
Block jobs usually operate with several block nodes, and better to handle them symmetrically, than use one from s->common.blk and one from s->target (or something like this). Moreover, generic blockjob layer has no use of BlockJob.blk. And more-moreover, most of block-jobs don't really use this blk. Actually only block-stream use it.
I've started this thing (unbinding block-job and its main node) long ago. First step was removing bs->job pointer in b23c580c946644b. Then block_job_drain was dropped in bb0c94099382b5273.
Now let's finally drop job->blk pointer.
Vladimir Sementsov-Ogievskiy (5):
blockjob: implement and use block_job_get_aio_context
test-blockjob-txn: don't abuse job->blk block/stream: add own blk
test-bdrv-drain: don't use BlockJob.blk
blockjob: drop BlockJob.blk field
include/block/blockjob.h | 10 +++++++--- block/mirror.c | 7 ------- block/stream.c | 24 ++++++++++++++++++------ blockdev.c | 6 +++--- blockjob.c | 24 +++++++----------------- qemu-img.c | 2 +- tests/unit/test-bdrv-drain.c | 12 ++++++++---- tests/unit/test-blockjob-txn.c | 10 +--------- tests/qemu-iotests/141.out | 2 +- 9 files changed, 46 insertions(+), 51 deletions(-)
Signature · 1 line
2.29.2
PATCH 1/5 blockjob: implement and use block_job_get_aio_context
We are going to drop BlockJob.blk. So let's retrieve block job context from underlying job instead of main node.
--- include/block/blockjob.h | 7 +++++++ blockdev.c | 6 +++--- blockjob.c | 5 +++++ qemu-img.c | 2 +- 4 files changed, 16 insertions(+), 4 deletions(-)
include/block/blockjob.h +7 -0
@@ -173,4 +173,11 @@ bool block_job_is_internal(BlockJob *job);*/ const BlockJobDriver *block_job_driver (BlockJob *job); +/* +* block_job_get_aio_context: +* +* Returns aio context associated with a block job. +*/ +AioContext *block_job_get_aio_context (BlockJob *job); +#endif
blockdev.c +3 -3
@@ -3328,7 +3328,7 @@ static BlockJob *find_block_job(const char *id, AioContext **aio_context,return NULL ; } - *aio_context =blk_get_aio_context (job->blk); + *aio_context =block_job_get_aio_context (job);aio_context_acquire (*aio_context);return job;
@@ -3433,7 +3433,7 @@ void qmp_block_job_finalize(const char *id, Error **errp)* automatically acquires the new one), so make sure we release the correct * one. */ - aio_context =blk_get_aio_context (job->blk); + aio_context =block_job_get_aio_context (job);job_unref (&job->job);aio_context_release (aio_context); }
@@ -3710,7 +3710,7 @@ BlockJobInfoList *qmp_query_block_jobs(Error **errp)if (block_job_is_internal (job)) {continue ; } - aio_context =blk_get_aio_context (job->blk); + aio_context =block_job_get_aio_context (job);aio_context_acquire (aio_context); value =block_job_query (job, errp);aio_context_release (aio_context);
blockjob.c +5 -0
@@ -534,3 +534,8 @@ BlockErrorAction block_job_error_action(BlockJob *job, BlockdevOnError on_err, }return action; } + +AioContext *block_job_get_aio_context (BlockJob *job) +{ +return job->job.aio_context; +}
qemu-img.c +1 -1
@@ -900,7 +900,7 @@ static void common_block_job_cb(void *opaque, int ret)static void run_block_job (BlockJob *job,Error **errp) { -AioContext *aio_context =blk_get_aio_context (job->blk); +AioContext *aio_context =block_job_get_aio_context (job);int ret =0 ;aio_context_acquire (aio_context);
Signature · 1 line
2.29.2
PATCH 2/5 test-blockjob-txn: don't abuse job->blk
In reply to Vladimir Sementsov-OgievskiyHere we use job->blk to drop our own reference in job cleanup. Let's do
simpler: drop our reference immediately after job creation.
--- tests/unit/test-blockjob-txn.c | 10 +--------- 1 file changed, 1 insertion(+), 9 deletions(-)
tests/unit/test-blockjob-txn.c +1 -9
@@ -25,14 +25,6 @@ typedef struct {int *result; } TestBlockJob; -static void test_block_job_clean (Job *job) -{ -BlockJob *bjob =container_of (job, BlockJob, job); -BlockDriverState *bs =blk_bs (bjob->blk); - -bdrv_unref (bs); -} - static int coroutine_fn test_block_job_run(Job *job, Error **errp) {TestBlockJob *s =container_of (job, TestBlockJob, common.job);
@@ -73,7 +65,6 @@ static const BlockJobDriver test_block_job_driver = { .free = block_job_free, .user_resume = block_job_user_resume, .run = test_block_job_run, - .clean = test_block_job_clean, }, };
@@ -105,6 +96,7 @@ static BlockJob *test_block_job_start(unsigned int iterations, s =block_job_create (job_id, &test_block_job_driver, txn, bs,0 ,BLK_PERM_ALL ,0 ,JOB_DEFAULT , test_block_job_cb, data, &error_abort); +bdrv_unref (bs);/* referenced by job now */ s->iterations = iterations; s->use_timer = use_timer; s->rc = rc;
Signature · 1 line
2.29.2
PATCH 3/5 block/stream: add own blk
In reply to Vladimir Sementsov-Ogievskiyblock-stream is the only block-job, that reasonably use BlockJob.blk. We are going to drop BlockJob.blk soon. So, let block-stream have own blk.
--- block/stream.c | 24 ++++++++++++++++++------ 1 file changed, 18 insertions(+), 6 deletions(-)
block/stream.c +18 -6
@@ -33,6 +33,7 @@ enum { typedef struct StreamBlockJob { BlockJob common; + BlockBackend *blk; BlockDriverState *base_overlay; /* COW overlay (stream from this) */ BlockDriverState *above_base; /* Node directly above the base */ BlockDriverState *cor_filter_bs;
@@ -85,17 +86,18 @@ static int stream_prepare(Job *job)static void stream_clean (Job *job) {StreamBlockJob *s =container_of (job, StreamBlockJob, common.job); -BlockJob *bjob = &s->common;if (s->cor_filter_bs) {bdrv_cor_filter_drop (s->cor_filter_bs); s->cor_filter_bs =NULL ; } +blk_unref (s->blk); + s->blk =NULL ; +/* Reopen the image back in read-only mode if necessary */ if (s->bs_read_only) {/* Give up write permissions before making it read-only */ -blk_set_perm (bjob->blk,0 ,BLK_PERM_ALL , &error_abort);bdrv_reopen_set_read_only (s->target_bs, true,NULL ); }
@@ -105,7 +107,6 @@ static void stream_clean(Job *job) static int coroutine_fn stream_run(Job *job, Error **errp) {StreamBlockJob *s =container_of (job, StreamBlockJob, common.job); -BlockBackend *blk = s->common.blk;BlockDriverState *unfiltered_bs =bdrv_skip_filters (s->target_bs);int64_t len;int64_t offset =0 ;
@@ -156,7 +157,7 @@ static int coroutine_fn stream_run(Job *job, Error **errp) }trace_stream_one_iteration (s, offset, n, ret);if (copy) { - ret =stream_populate (blk, offset, n); + ret =stream_populate (s->blk, offset, n); }if (ret <0 ) { BlockErrorAction action =
@@ -291,13 +292,24 @@ void stream_start(const char *job_id, BlockDriverState *bs, } s =block_job_create (job_id, &stream_job_driver,NULL , cor_filter_bs, -BLK_PERM_CONSISTENT_READ , - basic_flags |BLK_PERM_WRITE , +0 ,BLK_PERM_ALL , speed, creation_flags,NULL ,NULL , errp);if (!s) { goto fail; } + s->blk =blk_new_with_bs (cor_filter_bs,BLK_PERM_CONSISTENT_READ , + basic_flags |BLK_PERM_WRITE , errp); +if (!s->blk) { + goto fail; + } +/* +* Disable request queuing in the BlockBackend to avoid deadlocks on drain: +* The job reports that it's busy until it reaches a pause point. +*/ +blk_set_disable_request_queuing (s->blk, true); +blk_set_allow_aio_context_change (s->blk, true); +/* * Prevent concurrent jobs trying to modify the graph structure here, we * already have our own plans. Also don't allow resize as the image size is
Signature · 1 line
2.29.2
PATCH 4/5 test-bdrv-drain: don't use BlockJob.blk
In reply to Vladimir Sementsov-OgievskiyWe are going to drop BlockJob.blk in further commit. For tests it's enough to simply pass bs pointer.
--- tests/unit/test-bdrv-drain.c | 12 ++++++++---- 1 file changed, 8 insertions(+), 4 deletions(-)
tests/unit/test-bdrv-drain.c +8 -4
@@ -770,6 +770,7 @@ static void test_iothread_drain_subtree(void) typedef struct TestBlockJob { BlockJob common; + BlockDriverState *bs; int run_ret; int prepare_ret; bool running;
@@ -781,7 +782,7 @@ static int test_job_prepare(Job *job)TestBlockJob *s =container_of (job, TestBlockJob, common.job);/* Provoke an AIO_WAIT_WHILE() call to verify there is no deadlock */ -blk_flush (s->common.blk); +bdrv_flush (s->bs);return s->prepare_ret; }
@@ -790,7 +791,7 @@ static void test_job_commit(Job *job)TestBlockJob *s =container_of (job, TestBlockJob, common.job);/* Provoke an AIO_WAIT_WHILE() call to verify there is no deadlock */ -blk_flush (s->common.blk); +bdrv_flush (s->bs); }static void test_job_abort (Job *job)
@@ -798,7 +799,7 @@ static void test_job_abort(Job *job)TestBlockJob *s =container_of (job, TestBlockJob, common.job);/* Provoke an AIO_WAIT_WHILE() call to verify there is no deadlock */ -blk_flush (s->common.blk); +bdrv_flush (s->bs); } static int coroutine_fn test_job_run(Job *job, Error **errp)
@@ -913,6 +914,7 @@ static void test_blockjob_common_drain_node(enum drain_type drain_type, tjob =block_job_create ("job0" , &test_job_driver,NULL , src,0 ,BLK_PERM_ALL ,0 ,0 ,NULL ,NULL , &error_abort); + tjob->bs = src; job = &tjob->common;block_job_add_bdrv (job,"target" , target,0 ,BLK_PERM_ALL , &error_abort);
@@ -1535,6 +1537,7 @@ typedef struct TestDropBackingBlockJob {bool should_complete;bool *did_complete;BlockDriverState *detach_also; +BlockDriverState *bs; } TestDropBackingBlockJob; static int coroutine_fn test_drop_backing_job_run(Job *job, Error **errp)
@@ -1554,7 +1557,7 @@ static void test_drop_backing_job_commit(Job *job)TestDropBackingBlockJob *s =container_of (job, TestDropBackingBlockJob, common.job); -bdrv_set_backing_hd (blk_bs (s->common.blk),NULL , &error_abort); +bdrv_set_backing_hd (s->bs,NULL , &error_abort);bdrv_set_backing_hd (s->detach_also,NULL , &error_abort); *s->did_complete = true;
@@ -1654,6 +1657,7 @@ static void test_blockjob_commit_by_drained_end(void) job =block_job_create ("job" , &test_drop_backing_job_driver,NULL , bs_parents[2 ],0 ,BLK_PERM_ALL ,0 ,0 ,NULL ,NULL , &error_abort); + job->bs = bs_parents[2 ]; job->detach_also = bs_parents[0 ]; job->did_complete = &job_has_completed;
Signature · 1 line
2.29.2
PATCH 5/5 blockjob: drop BlockJob.blk field
In reply to Vladimir Sementsov-OgievskiyIt's unused now (except for permission handling)[*]. The only reasonable user of it was block-stream job, recently updated to use own blk. And other block jobs prefer to use own source node related objects.
So, the arguments of dropping the field are:
- block jobs prefer not to use it
- block jobs usually has more then one node to operate on, and better to operate symmetrically (for example has both source and target blk's in specific block-job state structure)
*: BlockJob.blk is used to keep some permissions. We simply move permissions to block-job child created in block_job_create() together with blk.
In mirror, we just should not care anymore about restoring state of blk. Most probably this code could be dropped long ago, after dropping bs->job pointer. Now it finally goes away together with BlockJob.blk itself.
iotest 141 output is updated, as "bdrv_has_blk(bs)" check in qmp_blockdev_del() doesn't fail (we don't have blk now). Still, new error message looks even better.
--- include/block/blockjob.h | 3 --- block/mirror.c | 7 ------- blockjob.c | 19 ++----------------- tests/qemu-iotests/141.out | 2 +- 4 files changed, 3 insertions(+), 28 deletions(-)
include/block/blockjob.h +0 -3
@@ -43,9 +43,6 @@ typedef struct BlockJob {/** Data belonging to the generic Job infrastructure */ Job job; -/** The block device on which the job is operating. */ -BlockBackend *blk; -/** Status that is published by the query-block-jobs QMP API */ BlockDeviceIoStatus iostatus;
block/mirror.c +0 -7
@@ -754,13 +754,6 @@ static int mirror_exit_common(Job *job)block_job_remove_all_bdrv (bjob);bdrv_replace_node (mirror_top_bs, mirror_top_bs->backing->bs, &error_abort); -/* We just changed the BDS the job BB refers to (with either or both of the -* bdrv_replace_node() calls), so switch the BB back so the cleanup does -* the right thing. We don't need any permissions any more now. */ -blk_remove_bs (bjob->blk); -blk_set_perm (bjob->blk,0 ,BLK_PERM_ALL , &error_abort); -blk_insert_bs (bjob->blk, mirror_top_bs, &error_abort); - bs_opaque->job =NULL ;bdrv_drained_end (src);
blockjob.c +2 -17
@@ -86,7 +86,6 @@ void block_job_free(Job *job)BlockJob *bjob =container_of (job, BlockJob, job);block_job_remove_all_bdrv (bjob); -blk_unref (bjob->blk);error_free (bjob->blocker); }
@@ -419,22 +418,15 @@ void *block_job_create(const char *job_id, const BlockJobDriver *driver, uint64_t shared_perm, int64_t speed, int flags, BlockCompletionFunc *cb, void *opaque, Error **errp) { - BlockBackend *blk; BlockJob *job;if (job_id ==NULL && !(flags &JOB_INTERNAL )) { job_id =bdrv_get_device_name (bs); } - blk =blk_new_with_bs (bs, perm, shared_perm, errp); -if (!blk) { -return NULL ; - } - - job =job_create (job_id, &driver->job_driver, txn,blk_get_aio_context (blk), + job =job_create (job_id, &driver->job_driver, txn,bdrv_get_aio_context (bs), flags, cb, opaque, errp);if (job ==NULL ) { -blk_unref (blk);return NULL ; }
@@ -442,8 +434,6 @@ void *block_job_create(const char *job_id, const BlockJobDriver *driver,assert (job->job.driver->free == &block_job_free);assert (job->job.driver->user_resume == &block_job_user_resume); - job->blk = blk; - job->finalize_cancelled_notifier.notify = block_job_event_cancelled; job->finalize_completed_notifier.notify = block_job_event_completed; job->pending_notifier.notify = block_job_event_pending;
@@ -460,15 +450,10 @@ void *block_job_create(const char *job_id, const BlockJobDriver *driver,error_setg (&job->blocker,"block device is in use by block job: %s" ,job_type_str (&job->job)); -block_job_add_bdrv (job,"main node" , bs,0 ,BLK_PERM_ALL , &error_abort); +block_job_add_bdrv (job,"main node" , bs, perm, shared_perm, &error_abort);bdrv_op_unblock (bs,BLOCK_OP_TYPE_DATAPLANE , job->blocker); -/* Disable request queuing in the BlockBackend to avoid deadlocks on drain: -* The job reports that it's busy until it reaches a pause point. */ -blk_set_disable_request_queuing (blk, true); -blk_set_allow_aio_context_change (blk, true); -/* Only set speed when necessary to avoid NotSupported error */ if (speed !=0 ) {if (!block_job_set_speed (job, speed, errp)) {
tests/qemu-iotests/141.out +1 -1
@@ -132,7 +132,7 @@ wrote 1048576/1048576 bytes at offset 0 {"timestamp": {"seconds": TIMESTAMP, "microseconds": TIMESTAMP}, "event": "JOB_STATUS_CHANGE", "data": {"status": "running", "id": "job0"}} {'execute': 'blockdev-del', 'arguments': {'node-name': 'drv0'}} -{"error": {"class": "GenericError", "desc": "Node drv0 is in use"}} +{"error": {"class": "GenericError", "desc": "Node 'drv0' is busy: block device is in use by block job: commit"}} {'execute': 'block-job-cancel', 'arguments': {'device': 'job0'}} {"return": {}}
Signature · 1 line
2.29.2