aboutsummaryrefslogtreecommitdiff
path: root/blockjob.c
diff options
context:
space:
mode:
authorStefan Hajnoczi <stefanha@redhat.com>2016-06-16 17:56:27 +0100
committerStefan Hajnoczi <stefanha@redhat.com>2016-06-20 14:25:41 +0100
commit463e0be101cb5a78ca6ee517d58604c3f3637bcd (patch)
tree9fcee9ddef069c53575c6af45baa52c4490da974 /blockjob.c
parente8a095dadb70e2ea6d5169d261920db3747bfa45 (diff)
blockjob: add AioContext attached callback
Block jobs that use additional BDSes or event loop resources need a callback to get their affairs in order when the AioContext is switched. Simple block jobs don't need an attach callback, they automatically work thanks to the generic attach/detach notifiers that this patch adds. Signed-off-by: Stefan Hajnoczi <stefanha@redhat.com> Reviewed-by: Paolo Bonzini <pbonzini@redhat.com> Reviewed-by: Fam Zheng <famz@redhat.com> Message-id: 1466096189-6477-7-git-send-email-stefanha@redhat.com
Diffstat (limited to 'blockjob.c')
-rw-r--r--blockjob.c38
1 files changed, 38 insertions, 0 deletions
diff --git a/blockjob.c b/blockjob.c
index 096a1b8715..90c4e262b0 100644
--- a/blockjob.c
+++ b/blockjob.c
@@ -71,6 +71,38 @@ static AioContext *block_job_get_aio_context(BlockJob *job)
blk_get_aio_context(job->blk);
}
+static void block_job_attached_aio_context(AioContext *new_context,
+ void *opaque)
+{
+ BlockJob *job = opaque;
+
+ if (job->driver->attached_aio_context) {
+ job->driver->attached_aio_context(job, new_context);
+ }
+
+ block_job_resume(job);
+}
+
+static void block_job_detach_aio_context(void *opaque)
+{
+ BlockJob *job = opaque;
+
+ /* In case the job terminates during aio_poll()... */
+ block_job_ref(job);
+
+ block_job_pause(job);
+
+ if (!job->paused) {
+ /* If job is !job->busy this kicks it into the next pause point. */
+ block_job_enter(job);
+ }
+ while (!job->paused && !job->completed) {
+ aio_poll(block_job_get_aio_context(job), true);
+ }
+
+ block_job_unref(job);
+}
+
void *block_job_create(const BlockJobDriver *driver, BlockDriverState *bs,
int64_t speed, BlockCompletionFunc *cb,
void *opaque, Error **errp)
@@ -103,6 +135,9 @@ void *block_job_create(const BlockJobDriver *driver, BlockDriverState *bs,
QLIST_INSERT_HEAD(&block_jobs, job, job_list);
+ blk_add_aio_context_notifier(blk, block_job_attached_aio_context,
+ block_job_detach_aio_context, job);
+
/* Only set speed when necessary to avoid NotSupported error */
if (speed != 0) {
Error *local_err = NULL;
@@ -128,6 +163,9 @@ void block_job_unref(BlockJob *job)
BlockDriverState *bs = blk_bs(job->blk);
bs->job = NULL;
bdrv_op_unblock_all(bs, job->blocker);
+ blk_remove_aio_context_notifier(job->blk,
+ block_job_attached_aio_context,
+ block_job_detach_aio_context, job);
blk_unref(job->blk);
error_free(job->blocker);
g_free(job->id);