If a long-running operation on a BDS wants to always remain in the same AIO context, it somehow needs to keep track of the BDS changing its context. This adds a function for registering callbacks on a BDS which are called whenever the BDS is attached or detached from an AIO context.
Signed-off-by: Max Reitz <mre...@redhat.com> --- block.c | 55 +++++++++++++++++++++++++++++++++++++++++++++++ include/block/block_int.h | 40 ++++++++++++++++++++++++++++++++++ 2 files changed, 95 insertions(+) diff --git a/block.c b/block.c index fe85d78..9c8dd5b 100644 --- a/block.c +++ b/block.c @@ -1775,6 +1775,8 @@ void bdrv_reopen_abort(BDRVReopenState *reopen_state) void bdrv_close(BlockDriverState *bs) { + BdrvAioFollower *baf, *baf_next; + if (bs->job) { block_job_cancel_sync(bs->job); } @@ -1816,6 +1818,11 @@ void bdrv_close(BlockDriverState *bs) if (bs->io_limits_enabled) { bdrv_io_limits_disable(bs); } + + QLIST_FOREACH_SAFE(baf, &bs->aio_followers, list, baf_next) { + g_free(baf); + } + QLIST_INIT(&bs->aio_followers); } void bdrv_close_all(void) @@ -5580,10 +5587,16 @@ AioContext *bdrv_get_aio_context(BlockDriverState *bs) void bdrv_detach_aio_context(BlockDriverState *bs) { + BdrvAioFollower *baf; + if (!bs->drv) { return; } + QLIST_FOREACH(baf, &bs->aio_followers, list) { + baf->aio_context_detached(baf->opaque); + } + if (bs->drv->bdrv_detach_aio_context) { bs->drv->bdrv_detach_aio_context(bs); } @@ -5600,6 +5613,8 @@ void bdrv_detach_aio_context(BlockDriverState *bs) void bdrv_attach_aio_context(BlockDriverState *bs, AioContext *new_context) { + BdrvAioFollower *baf; + if (!bs->drv) { return; } @@ -5615,6 +5630,10 @@ void bdrv_attach_aio_context(BlockDriverState *bs, if (bs->drv->bdrv_attach_aio_context) { bs->drv->bdrv_attach_aio_context(bs, new_context); } + + QLIST_FOREACH(baf, &bs->aio_followers, list) { + baf->aio_context_attached(new_context, baf->opaque); + } } void bdrv_set_aio_context(BlockDriverState *bs, AioContext *new_context) @@ -5631,6 +5650,42 @@ void bdrv_set_aio_context(BlockDriverState *bs, AioContext *new_context) aio_context_release(new_context); } +void bdrv_follow_aio(BlockDriverState *bs, + void (*aio_context_attached)(AioContext *new_context, + void *opaque), + void (*aio_context_detached)(void *opaque), void *opaque) +{ + BdrvAioFollower *baf = g_new(BdrvAioFollower, 1); + *baf = (BdrvAioFollower){ + .aio_context_attached = aio_context_attached, + .aio_context_detached = aio_context_detached, + .opaque = opaque + }; + + QLIST_INSERT_HEAD(&bs->aio_followers, baf, list); +} + +void bdrv_unfollow_aio(BlockDriverState *bs, + void (*aio_context_attached)(AioContext *, void *), + void (*aio_context_detached)(void *), void *opaque) +{ + BdrvAioFollower *baf, *baf_next; + + QLIST_FOREACH_SAFE(baf, &bs->aio_followers, list, baf_next) { + if (baf->aio_context_attached == aio_context_attached && + baf->aio_context_detached == aio_context_detached && + baf->opaque == opaque) + { + QLIST_REMOVE(baf, list); + g_free(baf); + + return; + } + } + + abort(); +} + void bdrv_add_before_write_notifier(BlockDriverState *bs, NotifierWithReturn *notifier) { diff --git a/include/block/block_int.h b/include/block/block_int.h index 7d156e3..aacc22f 100644 --- a/include/block/block_int.h +++ b/include/block/block_int.h @@ -283,6 +283,15 @@ typedef struct BlockLimits { size_t opt_mem_alignment; } BlockLimits; +typedef struct BdrvAioFollower { + void (*aio_context_attached)(AioContext *new_context, void *opaque); + void (*aio_context_detached)(void *opaque); + + void *opaque; + + QLIST_ENTRY(BdrvAioFollower) list; +} BdrvAioFollower; + /* * Note: the function bdrv_append() copies and swaps contents of * BlockDriverStates, so if you add new fields to this struct, please @@ -309,6 +318,10 @@ struct BlockDriverState { void *dev_opaque; AioContext *aio_context; /* event loop used for fd handlers, timers, etc */ + /* long-running tasks intended to always use the same AioContext as this + * BDS may register themselves in this list to be notified of changes + * regarding this BDS's context */ + QLIST_HEAD(, BdrvAioFollower) aio_followers; char filename[1024]; char backing_file[1024]; /* if non zero, the image is a diff of @@ -425,6 +438,33 @@ void bdrv_detach_aio_context(BlockDriverState *bs); void bdrv_attach_aio_context(BlockDriverState *bs, AioContext *new_context); +/** + * bdrv_follow_aio: + * + * If a long-running job intends to be always run in the same AioContext as a + * certain BDS, it may use this function to be notified of changes regarding the + * association of the BDS to an AioContext. + * + * aio_context_attached() is called after the target BDS has been attached to a + * new AioContext; aio_context_detached() is called before the target BDS is + * being detached from its old AioContext. + */ +void bdrv_follow_aio(BlockDriverState *bs, + void (*aio_context_attached)(AioContext *new_context, + void *opaque), + void (*aio_context_detached)(void *opaque), void *opaque); + +/** + * bdrv_unfollow_aio: + * + * Unsubscribe of change notifications regarding the BDS's AioContext. The + * parameters given here have to be the same as those given to + * bdrv_follow_aio(). + */ +void bdrv_unfollow_aio(BlockDriverState *bs, + void (*aio_context_attached)(AioContext *, void *), + void (*aio_context_detached)(void *), void *opaque); + #ifdef _WIN32 int is_windows_drive(const char *filename); #endif -- 1.9.3