ae23c9
From 72dcce395974c2cc4ae59c9ba11243abf04f1bbd Mon Sep 17 00:00:00 2001
ae23c9
From: Kevin Wolf <kwolf@redhat.com>
ae23c9
Date: Tue, 26 Jun 2018 09:48:09 +0200
ae23c9
Subject: [PATCH 101/268] job: Move coroutine and related code to Job
ae23c9
ae23c9
RH-Author: Kevin Wolf <kwolf@redhat.com>
ae23c9
Message-id: <20180626094856.6924-27-kwolf@redhat.com>
ae23c9
Patchwork-id: 81113
ae23c9
O-Subject: [RHV-7.6 qemu-kvm-rhev PATCH v2 26/73] job: Move coroutine and related code to Job
ae23c9
Bugzilla: 1513543
ae23c9
RH-Acked-by: Jeffrey Cody <jcody@redhat.com>
ae23c9
RH-Acked-by: Max Reitz <mreitz@redhat.com>
ae23c9
RH-Acked-by: Fam Zheng <famz@redhat.com>
ae23c9
ae23c9
This commit moves some core functions for dealing with the job coroutine
ae23c9
from BlockJob to Job. This includes primarily entering the coroutine
ae23c9
(both for the first and reentering) and yielding explicitly and at pause
ae23c9
points.
ae23c9
ae23c9
Signed-off-by: Kevin Wolf <kwolf@redhat.com>
ae23c9
Reviewed-by: John Snow <jsnow@redhat.com>
ae23c9
(cherry picked from commit da01ff7f38f52791f93fc3ca59afcfbb220f15af)
ae23c9
Signed-off-by: Kevin Wolf <kwolf@redhat.com>
ae23c9
Signed-off-by: Miroslav Rezanina <mrezanin@redhat.com>
ae23c9
---
ae23c9
 block/backup.c               |   2 +-
ae23c9
 block/commit.c               |   4 +-
ae23c9
 block/mirror.c               |  22 ++---
ae23c9
 block/replication.c          |   2 +-
ae23c9
 block/stream.c               |   4 +-
ae23c9
 blockdev.c                   |   8 +-
ae23c9
 blockjob.c                   | 219 ++++++++-----------------------------------
ae23c9
 include/block/blockjob.h     |  40 --------
ae23c9
 include/block/blockjob_int.h |  26 -----
ae23c9
 include/qemu/job.h           |  76 +++++++++++++++
ae23c9
 job.c                        | 137 +++++++++++++++++++++++++++
ae23c9
 tests/test-bdrv-drain.c      |  38 ++++----
ae23c9
 tests/test-blockjob-txn.c    |  12 +--
ae23c9
 tests/test-blockjob.c        |  14 +--
ae23c9
 14 files changed, 305 insertions(+), 299 deletions(-)
ae23c9
ae23c9
diff --git a/block/backup.c b/block/backup.c
ae23c9
index 22dd368..7d9aad9 100644
ae23c9
--- a/block/backup.c
ae23c9
+++ b/block/backup.c
ae23c9
@@ -528,8 +528,8 @@ static const BlockJobDriver backup_job_driver = {
ae23c9
         .instance_size          = sizeof(BackupBlockJob),
ae23c9
         .job_type               = JOB_TYPE_BACKUP,
ae23c9
         .free                   = block_job_free,
ae23c9
+        .start                  = backup_run,
ae23c9
     },
ae23c9
-    .start                  = backup_run,
ae23c9
     .commit                 = backup_commit,
ae23c9
     .abort                  = backup_abort,
ae23c9
     .clean                  = backup_clean,
ae23c9
diff --git a/block/commit.c b/block/commit.c
ae23c9
index d326766..2fbc310 100644
ae23c9
--- a/block/commit.c
ae23c9
+++ b/block/commit.c
ae23c9
@@ -220,8 +220,8 @@ static const BlockJobDriver commit_job_driver = {
ae23c9
         .instance_size = sizeof(CommitBlockJob),
ae23c9
         .job_type      = JOB_TYPE_COMMIT,
ae23c9
         .free          = block_job_free,
ae23c9
+        .start         = commit_run,
ae23c9
     },
ae23c9
-    .start         = commit_run,
ae23c9
 };
ae23c9
 
ae23c9
 static int coroutine_fn bdrv_commit_top_preadv(BlockDriverState *bs,
ae23c9
@@ -371,7 +371,7 @@ void commit_start(const char *job_id, BlockDriverState *bs,
ae23c9
     s->on_error = on_error;
ae23c9
 
ae23c9
     trace_commit_start(bs, base, top, s);
ae23c9
-    block_job_start(&s->common);
ae23c9
+    job_start(&s->common.job);
ae23c9
     return;
ae23c9
 
ae23c9
 fail:
ae23c9
diff --git a/block/mirror.c b/block/mirror.c
ae23c9
index 90d4ac9..95fc807 100644
ae23c9
--- a/block/mirror.c
ae23c9
+++ b/block/mirror.c
ae23c9
@@ -126,7 +126,7 @@ static void mirror_iteration_done(MirrorOp *op, int ret)
ae23c9
     g_free(op);
ae23c9
 
ae23c9
     if (s->waiting_for_io) {
ae23c9
-        qemu_coroutine_enter(s->common.co);
ae23c9
+        qemu_coroutine_enter(s->common.job.co);
ae23c9
     }
ae23c9
 }
ae23c9
 
ae23c9
@@ -345,7 +345,7 @@ static uint64_t coroutine_fn mirror_iteration(MirrorBlockJob *s)
ae23c9
         mirror_wait_for_io(s);
ae23c9
     }
ae23c9
 
ae23c9
-    block_job_pause_point(&s->common);
ae23c9
+    job_pause_point(&s->common.job);
ae23c9
 
ae23c9
     /* Find the number of consective dirty chunks following the first dirty
ae23c9
      * one, and wait for in flight requests in them. */
ae23c9
@@ -597,7 +597,7 @@ static void mirror_throttle(MirrorBlockJob *s)
ae23c9
         s->last_pause_ns = now;
ae23c9
         block_job_sleep_ns(&s->common, 0);
ae23c9
     } else {
ae23c9
-        block_job_pause_point(&s->common);
ae23c9
+        job_pause_point(&s->common.job);
ae23c9
     }
ae23c9
 }
ae23c9
 
ae23c9
@@ -786,7 +786,7 @@ static void coroutine_fn mirror_run(void *opaque)
ae23c9
             goto immediate_exit;
ae23c9
         }
ae23c9
 
ae23c9
-        block_job_pause_point(&s->common);
ae23c9
+        job_pause_point(&s->common.job);
ae23c9
 
ae23c9
         cnt = bdrv_get_dirty_count(s->dirty_bitmap);
ae23c9
         /* cnt is the number of dirty bytes remaining and s->bytes_in_flight is
ae23c9
@@ -957,9 +957,9 @@ static void mirror_complete(BlockJob *job, Error **errp)
ae23c9
     block_job_enter(&s->common);
ae23c9
 }
ae23c9
 
ae23c9
-static void mirror_pause(BlockJob *job)
ae23c9
+static void mirror_pause(Job *job)
ae23c9
 {
ae23c9
-    MirrorBlockJob *s = container_of(job, MirrorBlockJob, common);
ae23c9
+    MirrorBlockJob *s = container_of(job, MirrorBlockJob, common.job);
ae23c9
 
ae23c9
     mirror_wait_for_all_io(s);
ae23c9
 }
ae23c9
@@ -991,10 +991,10 @@ static const BlockJobDriver mirror_job_driver = {
ae23c9
         .instance_size          = sizeof(MirrorBlockJob),
ae23c9
         .job_type               = JOB_TYPE_MIRROR,
ae23c9
         .free                   = block_job_free,
ae23c9
+        .start                  = mirror_run,
ae23c9
+        .pause                  = mirror_pause,
ae23c9
     },
ae23c9
-    .start                  = mirror_run,
ae23c9
     .complete               = mirror_complete,
ae23c9
-    .pause                  = mirror_pause,
ae23c9
     .attached_aio_context   = mirror_attached_aio_context,
ae23c9
     .drain                  = mirror_drain,
ae23c9
 };
ae23c9
@@ -1004,10 +1004,10 @@ static const BlockJobDriver commit_active_job_driver = {
ae23c9
         .instance_size          = sizeof(MirrorBlockJob),
ae23c9
         .job_type               = JOB_TYPE_COMMIT,
ae23c9
         .free                   = block_job_free,
ae23c9
+        .start                  = mirror_run,
ae23c9
+        .pause                  = mirror_pause,
ae23c9
     },
ae23c9
-    .start                  = mirror_run,
ae23c9
     .complete               = mirror_complete,
ae23c9
-    .pause                  = mirror_pause,
ae23c9
     .attached_aio_context   = mirror_attached_aio_context,
ae23c9
     .drain                  = mirror_drain,
ae23c9
 };
ae23c9
@@ -1244,7 +1244,7 @@ static void mirror_start_job(const char *job_id, BlockDriverState *bs,
ae23c9
     }
ae23c9
 
ae23c9
     trace_mirror_start(bs, s, opaque);
ae23c9
-    block_job_start(&s->common);
ae23c9
+    job_start(&s->common.job);
ae23c9
     return;
ae23c9
 
ae23c9
 fail:
ae23c9
diff --git a/block/replication.c b/block/replication.c
ae23c9
index 6c0c718..3f7500e 100644
ae23c9
--- a/block/replication.c
ae23c9
+++ b/block/replication.c
ae23c9
@@ -574,7 +574,7 @@ static void replication_start(ReplicationState *rs, ReplicationMode mode,
ae23c9
             aio_context_release(aio_context);
ae23c9
             return;
ae23c9
         }
ae23c9
-        block_job_start(job);
ae23c9
+        job_start(&job->job);
ae23c9
         break;
ae23c9
     default:
ae23c9
         aio_context_release(aio_context);
ae23c9
diff --git a/block/stream.c b/block/stream.c
ae23c9
index 0bba816..6d8b7b6 100644
ae23c9
--- a/block/stream.c
ae23c9
+++ b/block/stream.c
ae23c9
@@ -213,8 +213,8 @@ static const BlockJobDriver stream_job_driver = {
ae23c9
         .instance_size = sizeof(StreamBlockJob),
ae23c9
         .job_type      = JOB_TYPE_STREAM,
ae23c9
         .free          = block_job_free,
ae23c9
+        .start         = stream_run,
ae23c9
     },
ae23c9
-    .start         = stream_run,
ae23c9
 };
ae23c9
 
ae23c9
 void stream_start(const char *job_id, BlockDriverState *bs,
ae23c9
@@ -262,7 +262,7 @@ void stream_start(const char *job_id, BlockDriverState *bs,
ae23c9
 
ae23c9
     s->on_error = on_error;
ae23c9
     trace_stream_start(bs, base, s);
ae23c9
-    block_job_start(&s->common);
ae23c9
+    job_start(&s->common.job);
ae23c9
     return;
ae23c9
 
ae23c9
 fail:
ae23c9
diff --git a/blockdev.c b/blockdev.c
ae23c9
index 19c04d9..93a4cdf 100644
ae23c9
--- a/blockdev.c
ae23c9
+++ b/blockdev.c
ae23c9
@@ -1911,7 +1911,7 @@ static void drive_backup_commit(BlkActionState *common)
ae23c9
     aio_context_acquire(aio_context);
ae23c9
 
ae23c9
     assert(state->job);
ae23c9
-    block_job_start(state->job);
ae23c9
+    job_start(&state->job->job);
ae23c9
 
ae23c9
     aio_context_release(aio_context);
ae23c9
 }
ae23c9
@@ -2009,7 +2009,7 @@ static void blockdev_backup_commit(BlkActionState *common)
ae23c9
     aio_context_acquire(aio_context);
ae23c9
 
ae23c9
     assert(state->job);
ae23c9
-    block_job_start(state->job);
ae23c9
+    job_start(&state->job->job);
ae23c9
 
ae23c9
     aio_context_release(aio_context);
ae23c9
 }
ae23c9
@@ -3426,7 +3426,7 @@ void qmp_drive_backup(DriveBackup *arg, Error **errp)
ae23c9
     BlockJob *job;
ae23c9
     job = do_drive_backup(arg, NULL, errp);
ae23c9
     if (job) {
ae23c9
-        block_job_start(job);
ae23c9
+        job_start(&job->job);
ae23c9
     }
ae23c9
 }
ae23c9
 
ae23c9
@@ -3514,7 +3514,7 @@ void qmp_blockdev_backup(BlockdevBackup *arg, Error **errp)
ae23c9
     BlockJob *job;
ae23c9
     job = do_blockdev_backup(arg, NULL, errp);
ae23c9
     if (job) {
ae23c9
-        block_job_start(job);
ae23c9
+        job_start(&job->job);
ae23c9
     }
ae23c9
 }
ae23c9
 
ae23c9
diff --git a/blockjob.c b/blockjob.c
ae23c9
index 3ede511..313b1ff 100644
ae23c9
--- a/blockjob.c
ae23c9
+++ b/blockjob.c
ae23c9
@@ -36,30 +36,9 @@
ae23c9
 #include "qemu/coroutine.h"
ae23c9
 #include "qemu/timer.h"
ae23c9
 
ae23c9
-/* Right now, this mutex is only needed to synchronize accesses to job->busy
ae23c9
- * and job->sleep_timer, such as concurrent calls to block_job_do_yield and
ae23c9
- * block_job_enter. */
ae23c9
-static QemuMutex block_job_mutex;
ae23c9
-
ae23c9
-static void block_job_lock(void)
ae23c9
-{
ae23c9
-    qemu_mutex_lock(&block_job_mutex);
ae23c9
-}
ae23c9
-
ae23c9
-static void block_job_unlock(void)
ae23c9
-{
ae23c9
-    qemu_mutex_unlock(&block_job_mutex);
ae23c9
-}
ae23c9
-
ae23c9
-static void __attribute__((__constructor__)) block_job_init(void)
ae23c9
-{
ae23c9
-    qemu_mutex_init(&block_job_mutex);
ae23c9
-}
ae23c9
-
ae23c9
 static void block_job_event_cancelled(BlockJob *job);
ae23c9
 static void block_job_event_completed(BlockJob *job, const char *msg);
ae23c9
 static int block_job_event_pending(BlockJob *job);
ae23c9
-static void block_job_enter_cond(BlockJob *job, bool(*fn)(BlockJob *job));
ae23c9
 
ae23c9
 /* Transactional group of block jobs */
ae23c9
 struct BlockJobTxn {
ae23c9
@@ -161,33 +140,27 @@ static void block_job_txn_del_job(BlockJob *job)
ae23c9
     }
ae23c9
 }
ae23c9
 
ae23c9
-/* Assumes the block_job_mutex is held */
ae23c9
-static bool block_job_timer_pending(BlockJob *job)
ae23c9
-{
ae23c9
-    return timer_pending(&job->sleep_timer);
ae23c9
-}
ae23c9
-
ae23c9
-/* Assumes the block_job_mutex is held */
ae23c9
-static bool block_job_timer_not_pending(BlockJob *job)
ae23c9
+/* Assumes the job_mutex is held */
ae23c9
+static bool job_timer_not_pending(Job *job)
ae23c9
 {
ae23c9
-    return !block_job_timer_pending(job);
ae23c9
+    return !timer_pending(&job->sleep_timer);
ae23c9
 }
ae23c9
 
ae23c9
 static void block_job_pause(BlockJob *job)
ae23c9
 {
ae23c9
-    job->pause_count++;
ae23c9
+    job->job.pause_count++;
ae23c9
 }
ae23c9
 
ae23c9
 static void block_job_resume(BlockJob *job)
ae23c9
 {
ae23c9
-    assert(job->pause_count > 0);
ae23c9
-    job->pause_count--;
ae23c9
-    if (job->pause_count) {
ae23c9
+    assert(job->job.pause_count > 0);
ae23c9
+    job->job.pause_count--;
ae23c9
+    if (job->job.pause_count) {
ae23c9
         return;
ae23c9
     }
ae23c9
 
ae23c9
     /* kick only if no timer is pending */
ae23c9
-    block_job_enter_cond(job, block_job_timer_not_pending);
ae23c9
+    job_enter_cond(&job->job, job_timer_not_pending);
ae23c9
 }
ae23c9
 
ae23c9
 static void block_job_attached_aio_context(AioContext *new_context,
ae23c9
@@ -208,7 +181,7 @@ void block_job_free(Job *job)
ae23c9
                                     block_job_detach_aio_context, bjob);
ae23c9
     blk_unref(bjob->blk);
ae23c9
     error_free(bjob->blocker);
ae23c9
-    assert(!timer_pending(&bjob->sleep_timer));
ae23c9
+    assert(!timer_pending(&bjob->job.sleep_timer));
ae23c9
 }
ae23c9
 
ae23c9
 static void block_job_attached_aio_context(AioContext *new_context,
ae23c9
@@ -226,7 +199,7 @@ static void block_job_attached_aio_context(AioContext *new_context,
ae23c9
 
ae23c9
 static void block_job_drain(BlockJob *job)
ae23c9
 {
ae23c9
-    /* If job is !job->busy this kicks it into the next pause point. */
ae23c9
+    /* If job is !job->job.busy this kicks it into the next pause point. */
ae23c9
     block_job_enter(job);
ae23c9
 
ae23c9
     blk_drain(job->blk);
ae23c9
@@ -244,7 +217,7 @@ static void block_job_detach_aio_context(void *opaque)
ae23c9
 
ae23c9
     block_job_pause(job);
ae23c9
 
ae23c9
-    while (!job->paused && !job->completed) {
ae23c9
+    while (!job->job.paused && !job->completed) {
ae23c9
         block_job_drain(job);
ae23c9
     }
ae23c9
 
ae23c9
@@ -312,29 +285,11 @@ bool block_job_is_internal(BlockJob *job)
ae23c9
     return (job->job.id == NULL);
ae23c9
 }
ae23c9
 
ae23c9
-static bool block_job_started(BlockJob *job)
ae23c9
-{
ae23c9
-    return job->co;
ae23c9
-}
ae23c9
-
ae23c9
 const BlockJobDriver *block_job_driver(BlockJob *job)
ae23c9
 {
ae23c9
     return job->driver;
ae23c9
 }
ae23c9
 
ae23c9
-/**
ae23c9
- * All jobs must allow a pause point before entering their job proper. This
ae23c9
- * ensures that jobs can be paused prior to being started, then resumed later.
ae23c9
- */
ae23c9
-static void coroutine_fn block_job_co_entry(void *opaque)
ae23c9
-{
ae23c9
-    BlockJob *job = opaque;
ae23c9
-
ae23c9
-    assert(job && job->driver && job->driver->start);
ae23c9
-    block_job_pause_point(job);
ae23c9
-    job->driver->start(job);
ae23c9
-}
ae23c9
-
ae23c9
 static void block_job_sleep_timer_cb(void *opaque)
ae23c9
 {
ae23c9
     BlockJob *job = opaque;
ae23c9
@@ -342,24 +297,12 @@ static void block_job_sleep_timer_cb(void *opaque)
ae23c9
     block_job_enter(job);
ae23c9
 }
ae23c9
 
ae23c9
-void block_job_start(BlockJob *job)
ae23c9
-{
ae23c9
-    assert(job && !block_job_started(job) && job->paused &&
ae23c9
-           job->driver && job->driver->start);
ae23c9
-    job->co = qemu_coroutine_create(block_job_co_entry, job);
ae23c9
-    job->pause_count--;
ae23c9
-    job->busy = true;
ae23c9
-    job->paused = false;
ae23c9
-    job_state_transition(&job->job, JOB_STATUS_RUNNING);
ae23c9
-    bdrv_coroutine_enter(blk_bs(job->blk), job->co);
ae23c9
-}
ae23c9
-
ae23c9
 static void block_job_decommission(BlockJob *job)
ae23c9
 {
ae23c9
     assert(job);
ae23c9
     job->completed = true;
ae23c9
-    job->busy = false;
ae23c9
-    job->paused = false;
ae23c9
+    job->job.busy = false;
ae23c9
+    job->job.paused = false;
ae23c9
     job->job.deferred_to_main_loop = true;
ae23c9
     block_job_txn_del_job(job);
ae23c9
     job_state_transition(&job->job, JOB_STATUS_NULL);
ae23c9
@@ -374,7 +317,7 @@ static void block_job_do_dismiss(BlockJob *job)
ae23c9
 static void block_job_conclude(BlockJob *job)
ae23c9
 {
ae23c9
     job_state_transition(&job->job, JOB_STATUS_CONCLUDED);
ae23c9
-    if (job->auto_dismiss || !block_job_started(job)) {
ae23c9
+    if (job->auto_dismiss || !job_started(&job->job)) {
ae23c9
         block_job_do_dismiss(job);
ae23c9
     }
ae23c9
 }
ae23c9
@@ -439,7 +382,7 @@ static int block_job_finalize_single(BlockJob *job)
ae23c9
     }
ae23c9
 
ae23c9
     /* Emit events only if we actually started */
ae23c9
-    if (block_job_started(job)) {
ae23c9
+    if (job_started(&job->job)) {
ae23c9
         if (job_is_cancelled(&job->job)) {
ae23c9
             block_job_event_cancelled(job);
ae23c9
         } else {
ae23c9
@@ -464,7 +407,7 @@ static void block_job_cancel_async(BlockJob *job, bool force)
ae23c9
     if (job->user_paused) {
ae23c9
         /* Do not call block_job_enter here, the caller will handle it.  */
ae23c9
         job->user_paused = false;
ae23c9
-        job->pause_count--;
ae23c9
+        job->job.pause_count--;
ae23c9
     }
ae23c9
     job->job.cancelled = true;
ae23c9
     /* To prevent 'force == false' overriding a previous 'force == true' */
ae23c9
@@ -615,6 +558,12 @@ static void block_job_completed_txn_success(BlockJob *job)
ae23c9
     }
ae23c9
 }
ae23c9
 
ae23c9
+/* Assumes the job_mutex is held */
ae23c9
+static bool job_timer_pending(Job *job)
ae23c9
+{
ae23c9
+    return timer_pending(&job->sleep_timer);
ae23c9
+}
ae23c9
+
ae23c9
 void block_job_set_speed(BlockJob *job, int64_t speed, Error **errp)
ae23c9
 {
ae23c9
     int64_t old_speed = job->speed;
ae23c9
@@ -635,7 +584,7 @@ void block_job_set_speed(BlockJob *job, int64_t speed, Error **errp)
ae23c9
     }
ae23c9
 
ae23c9
     /* kick only if a timer is pending */
ae23c9
-    block_job_enter_cond(job, block_job_timer_pending);
ae23c9
+    job_enter_cond(&job->job, job_timer_pending);
ae23c9
 }
ae23c9
 
ae23c9
 int64_t block_job_ratelimit_get_delay(BlockJob *job, uint64_t n)
ae23c9
@@ -654,7 +603,7 @@ void block_job_complete(BlockJob *job, Error **errp)
ae23c9
     if (job_apply_verb(&job->job, JOB_VERB_COMPLETE, errp)) {
ae23c9
         return;
ae23c9
     }
ae23c9
-    if (job->pause_count || job_is_cancelled(&job->job) ||
ae23c9
+    if (job->job.pause_count || job_is_cancelled(&job->job) ||
ae23c9
         !job->driver->complete)
ae23c9
     {
ae23c9
         error_setg(errp, "The active block job '%s' cannot be completed",
ae23c9
@@ -708,7 +657,7 @@ bool block_job_user_paused(BlockJob *job)
ae23c9
 void block_job_user_resume(BlockJob *job, Error **errp)
ae23c9
 {
ae23c9
     assert(job);
ae23c9
-    if (!job->user_paused || job->pause_count <= 0) {
ae23c9
+    if (!job->user_paused || job->job.pause_count <= 0) {
ae23c9
         error_setg(errp, "Can't resume a job that was not paused");
ae23c9
         return;
ae23c9
     }
ae23c9
@@ -727,7 +676,7 @@ void block_job_cancel(BlockJob *job, bool force)
ae23c9
         return;
ae23c9
     }
ae23c9
     block_job_cancel_async(job, force);
ae23c9
-    if (!block_job_started(job)) {
ae23c9
+    if (!job_started(&job->job)) {
ae23c9
         block_job_completed(job, -ECANCELED);
ae23c9
     } else if (job->job.deferred_to_main_loop) {
ae23c9
         block_job_completed_txn_abort(job);
ae23c9
@@ -797,8 +746,8 @@ BlockJobInfo *block_job_query(BlockJob *job, Error **errp)
ae23c9
     info->type      = g_strdup(job_type_str(&job->job));
ae23c9
     info->device    = g_strdup(job->job.id);
ae23c9
     info->len       = job->len;
ae23c9
-    info->busy      = atomic_read(&job->busy);
ae23c9
-    info->paused    = job->pause_count > 0;
ae23c9
+    info->busy      = atomic_read(&job->job.busy);
ae23c9
+    info->paused    = job->job.pause_count > 0;
ae23c9
     info->offset    = job->offset;
ae23c9
     info->speed     = job->speed;
ae23c9
     info->io_status = job->iostatus;
ae23c9
@@ -915,12 +864,9 @@ void *block_job_create(const char *job_id, const BlockJobDriver *driver,
ae23c9
     job->blk           = blk;
ae23c9
     job->cb            = cb;
ae23c9
     job->opaque        = opaque;
ae23c9
-    job->busy          = false;
ae23c9
-    job->paused        = true;
ae23c9
-    job->pause_count   = 1;
ae23c9
     job->auto_finalize = !(flags & BLOCK_JOB_MANUAL_FINALIZE);
ae23c9
     job->auto_dismiss  = !(flags & BLOCK_JOB_MANUAL_DISMISS);
ae23c9
-    aio_timer_init(qemu_get_aio_context(), &job->sleep_timer,
ae23c9
+    aio_timer_init(qemu_get_aio_context(), &job->job.sleep_timer,
ae23c9
                    QEMU_CLOCK_REALTIME, SCALE_NS,
ae23c9
                    block_job_sleep_timer_cb, job);
ae23c9
 
ae23c9
@@ -980,128 +926,41 @@ void block_job_completed(BlockJob *job, int ret)
ae23c9
     }
ae23c9
 }
ae23c9
 
ae23c9
-static bool block_job_should_pause(BlockJob *job)
ae23c9
-{
ae23c9
-    return job->pause_count > 0;
ae23c9
-}
ae23c9
-
ae23c9
-/* Yield, and schedule a timer to reenter the coroutine after @ns nanoseconds.
ae23c9
- * Reentering the job coroutine with block_job_enter() before the timer has
ae23c9
- * expired is allowed and cancels the timer.
ae23c9
- *
ae23c9
- * If @ns is (uint64_t) -1, no timer is scheduled and block_job_enter() must be
ae23c9
- * called explicitly. */
ae23c9
-static void block_job_do_yield(BlockJob *job, uint64_t ns)
ae23c9
-{
ae23c9
-    block_job_lock();
ae23c9
-    if (ns != -1) {
ae23c9
-        timer_mod(&job->sleep_timer, ns);
ae23c9
-    }
ae23c9
-    job->busy = false;
ae23c9
-    block_job_unlock();
ae23c9
-    qemu_coroutine_yield();
ae23c9
-
ae23c9
-    /* Set by block_job_enter before re-entering the coroutine.  */
ae23c9
-    assert(job->busy);
ae23c9
-}
ae23c9
-
ae23c9
-void coroutine_fn block_job_pause_point(BlockJob *job)
ae23c9
-{
ae23c9
-    assert(job && block_job_started(job));
ae23c9
-
ae23c9
-    if (!block_job_should_pause(job)) {
ae23c9
-        return;
ae23c9
-    }
ae23c9
-    if (job_is_cancelled(&job->job)) {
ae23c9
-        return;
ae23c9
-    }
ae23c9
-
ae23c9
-    if (job->driver->pause) {
ae23c9
-        job->driver->pause(job);
ae23c9
-    }
ae23c9
-
ae23c9
-    if (block_job_should_pause(job) && !job_is_cancelled(&job->job)) {
ae23c9
-        JobStatus status = job->job.status;
ae23c9
-        job_state_transition(&job->job, status == JOB_STATUS_READY
ae23c9
-                                        ? JOB_STATUS_STANDBY
ae23c9
-                                        : JOB_STATUS_PAUSED);
ae23c9
-        job->paused = true;
ae23c9
-        block_job_do_yield(job, -1);
ae23c9
-        job->paused = false;
ae23c9
-        job_state_transition(&job->job, status);
ae23c9
-    }
ae23c9
-
ae23c9
-    if (job->driver->resume) {
ae23c9
-        job->driver->resume(job);
ae23c9
-    }
ae23c9
-}
ae23c9
-
ae23c9
-/*
ae23c9
- * Conditionally enter a block_job pending a call to fn() while
ae23c9
- * under the block_job_lock critical section.
ae23c9
- */
ae23c9
-static void block_job_enter_cond(BlockJob *job, bool(*fn)(BlockJob *job))
ae23c9
-{
ae23c9
-    if (!block_job_started(job)) {
ae23c9
-        return;
ae23c9
-    }
ae23c9
-    if (job->job.deferred_to_main_loop) {
ae23c9
-        return;
ae23c9
-    }
ae23c9
-
ae23c9
-    block_job_lock();
ae23c9
-    if (job->busy) {
ae23c9
-        block_job_unlock();
ae23c9
-        return;
ae23c9
-    }
ae23c9
-
ae23c9
-    if (fn && !fn(job)) {
ae23c9
-        block_job_unlock();
ae23c9
-        return;
ae23c9
-    }
ae23c9
-
ae23c9
-    assert(!job->job.deferred_to_main_loop);
ae23c9
-    timer_del(&job->sleep_timer);
ae23c9
-    job->busy = true;
ae23c9
-    block_job_unlock();
ae23c9
-    aio_co_wake(job->co);
ae23c9
-}
ae23c9
-
ae23c9
 void block_job_enter(BlockJob *job)
ae23c9
 {
ae23c9
-    block_job_enter_cond(job, NULL);
ae23c9
+    job_enter_cond(&job->job, NULL);
ae23c9
 }
ae23c9
 
ae23c9
 void block_job_sleep_ns(BlockJob *job, int64_t ns)
ae23c9
 {
ae23c9
-    assert(job->busy);
ae23c9
+    assert(job->job.busy);
ae23c9
 
ae23c9
     /* Check cancellation *before* setting busy = false, too!  */
ae23c9
     if (job_is_cancelled(&job->job)) {
ae23c9
         return;
ae23c9
     }
ae23c9
 
ae23c9
-    if (!block_job_should_pause(job)) {
ae23c9
-        block_job_do_yield(job, qemu_clock_get_ns(QEMU_CLOCK_REALTIME) + ns);
ae23c9
+    if (!job_should_pause(&job->job)) {
ae23c9
+        job_do_yield(&job->job, qemu_clock_get_ns(QEMU_CLOCK_REALTIME) + ns);
ae23c9
     }
ae23c9
 
ae23c9
-    block_job_pause_point(job);
ae23c9
+    job_pause_point(&job->job);
ae23c9
 }
ae23c9
 
ae23c9
 void block_job_yield(BlockJob *job)
ae23c9
 {
ae23c9
-    assert(job->busy);
ae23c9
+    assert(job->job.busy);
ae23c9
 
ae23c9
     /* Check cancellation *before* setting busy = false, too!  */
ae23c9
     if (job_is_cancelled(&job->job)) {
ae23c9
         return;
ae23c9
     }
ae23c9
 
ae23c9
-    if (!block_job_should_pause(job)) {
ae23c9
-        block_job_do_yield(job, -1);
ae23c9
+    if (!job_should_pause(&job->job)) {
ae23c9
+        job_do_yield(&job->job, -1);
ae23c9
     }
ae23c9
 
ae23c9
-    block_job_pause_point(job);
ae23c9
+    job_pause_point(&job->job);
ae23c9
 }
ae23c9
 
ae23c9
 void block_job_iostatus_reset(BlockJob *job)
ae23c9
@@ -1109,7 +968,7 @@ void block_job_iostatus_reset(BlockJob *job)
ae23c9
     if (job->iostatus == BLOCK_DEVICE_IO_STATUS_OK) {
ae23c9
         return;
ae23c9
     }
ae23c9
-    assert(job->user_paused && job->pause_count > 0);
ae23c9
+    assert(job->user_paused && job->job.pause_count > 0);
ae23c9
     job->iostatus = BLOCK_DEVICE_IO_STATUS_OK;
ae23c9
 }
ae23c9
 
ae23c9
diff --git a/include/block/blockjob.h b/include/block/blockjob.h
ae23c9
index 2a9e865..b60d919 100644
ae23c9
--- a/include/block/blockjob.h
ae23c9
+++ b/include/block/blockjob.h
ae23c9
@@ -51,43 +51,18 @@ typedef struct BlockJob {
ae23c9
     BlockBackend *blk;
ae23c9
 
ae23c9
     /**
ae23c9
-     * The coroutine that executes the job.  If not NULL, it is
ae23c9
-     * reentered when busy is false and the job is cancelled.
ae23c9
-     */
ae23c9
-    Coroutine *co;
ae23c9
-
ae23c9
-    /**
ae23c9
      * Set to true if the job should abort immediately without waiting
ae23c9
      * for data to be in sync.
ae23c9
      */
ae23c9
     bool force;
ae23c9
 
ae23c9
     /**
ae23c9
-     * Counter for pause request. If non-zero, the block job is either paused,
ae23c9
-     * or if busy == true will pause itself as soon as possible.
ae23c9
-     */
ae23c9
-    int pause_count;
ae23c9
-
ae23c9
-    /**
ae23c9
      * Set to true if the job is paused by user.  Can be unpaused with the
ae23c9
      * block-job-resume QMP command.
ae23c9
      */
ae23c9
     bool user_paused;
ae23c9
 
ae23c9
     /**
ae23c9
-     * Set to false by the job while the coroutine has yielded and may be
ae23c9
-     * re-entered by block_job_enter().  There may still be I/O or event loop
ae23c9
-     * activity pending.  Accessed under block_job_mutex (in blockjob.c).
ae23c9
-     */
ae23c9
-    bool busy;
ae23c9
-
ae23c9
-    /**
ae23c9
-     * Set to true by the job while it is in a quiescent state, where
ae23c9
-     * no I/O or event loop activity is pending.
ae23c9
-     */
ae23c9
-    bool paused;
ae23c9
-
ae23c9
-    /**
ae23c9
      * Set to true when the job is ready to be completed.
ae23c9
      */
ae23c9
     bool ready;
ae23c9
@@ -125,12 +100,6 @@ typedef struct BlockJob {
ae23c9
     /** ret code passed to block_job_completed. */
ae23c9
     int ret;
ae23c9
 
ae23c9
-    /**
ae23c9
-     * Timer that is used by @block_job_sleep_ns. Accessed under
ae23c9
-     * block_job_mutex (in blockjob.c).
ae23c9
-     */
ae23c9
-    QEMUTimer sleep_timer;
ae23c9
-
ae23c9
     /** True if this job should automatically finalize itself */
ae23c9
     bool auto_finalize;
ae23c9
 
ae23c9
@@ -208,15 +177,6 @@ void block_job_remove_all_bdrv(BlockJob *job);
ae23c9
 void block_job_set_speed(BlockJob *job, int64_t speed, Error **errp);
ae23c9
 
ae23c9
 /**
ae23c9
- * block_job_start:
ae23c9
- * @job: A job that has not yet been started.
ae23c9
- *
ae23c9
- * Begins execution of a block job.
ae23c9
- * Takes ownership of one reference to the job object.
ae23c9
- */
ae23c9
-void block_job_start(BlockJob *job);
ae23c9
-
ae23c9
-/**
ae23c9
  * block_job_cancel:
ae23c9
  * @job: The job to be canceled.
ae23c9
  * @force: Quit a job without waiting for data to be in sync.
ae23c9
diff --git a/include/block/blockjob_int.h b/include/block/blockjob_int.h
ae23c9
index 0c2f8de..0a614a8 100644
ae23c9
--- a/include/block/blockjob_int.h
ae23c9
+++ b/include/block/blockjob_int.h
ae23c9
@@ -38,9 +38,6 @@ struct BlockJobDriver {
ae23c9
     /** Generic JobDriver callbacks and settings */
ae23c9
     JobDriver job_driver;
ae23c9
 
ae23c9
-    /** Mandatory: Entrypoint for the Coroutine. */
ae23c9
-    CoroutineEntry *start;
ae23c9
-
ae23c9
     /**
ae23c9
      * Optional callback for job types whose completion must be triggered
ae23c9
      * manually.
ae23c9
@@ -85,20 +82,6 @@ struct BlockJobDriver {
ae23c9
      */
ae23c9
     void (*clean)(BlockJob *job);
ae23c9
 
ae23c9
-    /**
ae23c9
-     * If the callback is not NULL, it will be invoked when the job transitions
ae23c9
-     * into the paused state.  Paused jobs must not perform any asynchronous
ae23c9
-     * I/O or event loop activity.  This callback is used to quiesce jobs.
ae23c9
-     */
ae23c9
-    void coroutine_fn (*pause)(BlockJob *job);
ae23c9
-
ae23c9
-    /**
ae23c9
-     * If the callback is not NULL, it will be invoked when the job transitions
ae23c9
-     * out of the paused state.  Any asynchronous I/O or event loop activity
ae23c9
-     * should be restarted from this callback.
ae23c9
-     */
ae23c9
-    void coroutine_fn (*resume)(BlockJob *job);
ae23c9
-
ae23c9
     /*
ae23c9
      * If the callback is not NULL, it will be invoked before the job is
ae23c9
      * resumed in a new AioContext.  This is the place to move any resources
ae23c9
@@ -196,15 +179,6 @@ void block_job_early_fail(BlockJob *job);
ae23c9
 void block_job_completed(BlockJob *job, int ret);
ae23c9
 
ae23c9
 /**
ae23c9
- * block_job_pause_point:
ae23c9
- * @job: The job that is ready to pause.
ae23c9
- *
ae23c9
- * Pause now if block_job_pause() has been called.  Block jobs that perform
ae23c9
- * lots of I/O must call this between requests so that the job can be paused.
ae23c9
- */
ae23c9
-void coroutine_fn block_job_pause_point(BlockJob *job);
ae23c9
-
ae23c9
-/**
ae23c9
  * block_job_enter:
ae23c9
  * @job: The job to enter.
ae23c9
  *
ae23c9
diff --git a/include/qemu/job.h b/include/qemu/job.h
ae23c9
index 933e0ab..9dcff12 100644
ae23c9
--- a/include/qemu/job.h
ae23c9
+++ b/include/qemu/job.h
ae23c9
@@ -28,6 +28,7 @@
ae23c9
 
ae23c9
 #include "qapi/qapi-types-block-core.h"
ae23c9
 #include "qemu/queue.h"
ae23c9
+#include "qemu/coroutine.h"
ae23c9
 
ae23c9
 typedef struct JobDriver JobDriver;
ae23c9
 
ae23c9
@@ -51,6 +52,37 @@ typedef struct Job {
ae23c9
     AioContext *aio_context;
ae23c9
 
ae23c9
     /**
ae23c9
+     * The coroutine that executes the job.  If not NULL, it is reentered when
ae23c9
+     * busy is false and the job is cancelled.
ae23c9
+     */
ae23c9
+    Coroutine *co;
ae23c9
+
ae23c9
+    /**
ae23c9
+     * Timer that is used by @block_job_sleep_ns. Accessed under job_mutex (in
ae23c9
+     * job.c).
ae23c9
+     */
ae23c9
+    QEMUTimer sleep_timer;
ae23c9
+
ae23c9
+    /**
ae23c9
+     * Counter for pause request. If non-zero, the block job is either paused,
ae23c9
+     * or if busy == true will pause itself as soon as possible.
ae23c9
+     */
ae23c9
+    int pause_count;
ae23c9
+
ae23c9
+    /**
ae23c9
+     * Set to false by the job while the coroutine has yielded and may be
ae23c9
+     * re-entered by block_job_enter().  There may still be I/O or event loop
ae23c9
+     * activity pending.  Accessed under block_job_mutex (in blockjob.c).
ae23c9
+     */
ae23c9
+    bool busy;
ae23c9
+
ae23c9
+    /**
ae23c9
+     * Set to true by the job while it is in a quiescent state, where
ae23c9
+     * no I/O or event loop activity is pending.
ae23c9
+     */
ae23c9
+    bool paused;
ae23c9
+
ae23c9
+    /**
ae23c9
      * Set to true if the job should cancel itself.  The flag must
ae23c9
      * always be tested just before toggling the busy flag from false
ae23c9
      * to true.  After a job has been cancelled, it should only yield
ae23c9
@@ -75,6 +107,23 @@ struct JobDriver {
ae23c9
     /** Enum describing the operation */
ae23c9
     JobType job_type;
ae23c9
 
ae23c9
+    /** Mandatory: Entrypoint for the Coroutine. */
ae23c9
+    CoroutineEntry *start;
ae23c9
+
ae23c9
+    /**
ae23c9
+     * If the callback is not NULL, it will be invoked when the job transitions
ae23c9
+     * into the paused state.  Paused jobs must not perform any asynchronous
ae23c9
+     * I/O or event loop activity.  This callback is used to quiesce jobs.
ae23c9
+     */
ae23c9
+    void coroutine_fn (*pause)(Job *job);
ae23c9
+
ae23c9
+    /**
ae23c9
+     * If the callback is not NULL, it will be invoked when the job transitions
ae23c9
+     * out of the paused state.  Any asynchronous I/O or event loop activity
ae23c9
+     * should be restarted from this callback.
ae23c9
+     */
ae23c9
+    void coroutine_fn (*resume)(Job *job);
ae23c9
+
ae23c9
     /** Called when the job is freed */
ae23c9
     void (*free)(Job *job);
ae23c9
 };
ae23c9
@@ -103,6 +152,30 @@ void job_ref(Job *job);
ae23c9
  */
ae23c9
 void job_unref(Job *job);
ae23c9
 
ae23c9
+/**
ae23c9
+ * Conditionally enter the job coroutine if the job is ready to run, not
ae23c9
+ * already busy and fn() returns true. fn() is called while under the job_lock
ae23c9
+ * critical section.
ae23c9
+ */
ae23c9
+void job_enter_cond(Job *job, bool(*fn)(Job *job));
ae23c9
+
ae23c9
+/**
ae23c9
+ * @job: A job that has not yet been started.
ae23c9
+ *
ae23c9
+ * Begins execution of a job.
ae23c9
+ * Takes ownership of one reference to the job object.
ae23c9
+ */
ae23c9
+void job_start(Job *job);
ae23c9
+
ae23c9
+/**
ae23c9
+ * @job: The job that is ready to pause.
ae23c9
+ *
ae23c9
+ * Pause now if job_pause() has been called. Jobs that perform lots of I/O
ae23c9
+ * must call this between requests so that the job can be paused.
ae23c9
+ */
ae23c9
+void coroutine_fn job_pause_point(Job *job);
ae23c9
+
ae23c9
+
ae23c9
 /** Returns the JobType of a given Job. */
ae23c9
 JobType job_type(const Job *job);
ae23c9
 
ae23c9
@@ -153,5 +226,8 @@ void job_defer_to_main_loop(Job *job, JobDeferToMainLoopFn *fn, void *opaque);
ae23c9
 
ae23c9
 /* TODO To be removed from the public interface */
ae23c9
 void job_state_transition(Job *job, JobStatus s1);
ae23c9
+void coroutine_fn job_do_yield(Job *job, uint64_t ns);
ae23c9
+bool job_should_pause(Job *job);
ae23c9
+bool job_started(Job *job);
ae23c9
 
ae23c9
 #endif
ae23c9
diff --git a/job.c b/job.c
ae23c9
index c5a37fb..78497fd 100644
ae23c9
--- a/job.c
ae23c9
+++ b/job.c
ae23c9
@@ -60,6 +60,26 @@ bool JobVerbTable[JOB_VERB__MAX][JOB_STATUS__MAX] = {
ae23c9
     [JOB_VERB_DISMISS]              = {0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 0},
ae23c9
 };
ae23c9
 
ae23c9
+/* Right now, this mutex is only needed to synchronize accesses to job->busy
ae23c9
+ * and job->sleep_timer, such as concurrent calls to job_do_yield and
ae23c9
+ * job_enter. */
ae23c9
+static QemuMutex job_mutex;
ae23c9
+
ae23c9
+static void job_lock(void)
ae23c9
+{
ae23c9
+    qemu_mutex_lock(&job_mutex);
ae23c9
+}
ae23c9
+
ae23c9
+static void job_unlock(void)
ae23c9
+{
ae23c9
+    qemu_mutex_unlock(&job_mutex);
ae23c9
+}
ae23c9
+
ae23c9
+static void __attribute__((__constructor__)) job_init(void)
ae23c9
+{
ae23c9
+    qemu_mutex_init(&job_mutex);
ae23c9
+}
ae23c9
+
ae23c9
 /* TODO Make static once the whole state machine is in job.c */
ae23c9
 void job_state_transition(Job *job, JobStatus s1)
ae23c9
 {
ae23c9
@@ -101,6 +121,16 @@ bool job_is_cancelled(Job *job)
ae23c9
     return job->cancelled;
ae23c9
 }
ae23c9
 
ae23c9
+bool job_started(Job *job)
ae23c9
+{
ae23c9
+    return job->co;
ae23c9
+}
ae23c9
+
ae23c9
+bool job_should_pause(Job *job)
ae23c9
+{
ae23c9
+    return job->pause_count > 0;
ae23c9
+}
ae23c9
+
ae23c9
 Job *job_next(Job *job)
ae23c9
 {
ae23c9
     if (!job) {
ae23c9
@@ -143,6 +173,9 @@ void *job_create(const char *job_id, const JobDriver *driver, AioContext *ctx,
ae23c9
     job->id            = g_strdup(job_id);
ae23c9
     job->refcnt        = 1;
ae23c9
     job->aio_context   = ctx;
ae23c9
+    job->busy          = false;
ae23c9
+    job->paused        = true;
ae23c9
+    job->pause_count   = 1;
ae23c9
 
ae23c9
     job_state_transition(job, JOB_STATUS_CREATED);
ae23c9
 
ae23c9
@@ -172,6 +205,110 @@ void job_unref(Job *job)
ae23c9
     }
ae23c9
 }
ae23c9
 
ae23c9
+void job_enter_cond(Job *job, bool(*fn)(Job *job))
ae23c9
+{
ae23c9
+    if (!job_started(job)) {
ae23c9
+        return;
ae23c9
+    }
ae23c9
+    if (job->deferred_to_main_loop) {
ae23c9
+        return;
ae23c9
+    }
ae23c9
+
ae23c9
+    job_lock();
ae23c9
+    if (job->busy) {
ae23c9
+        job_unlock();
ae23c9
+        return;
ae23c9
+    }
ae23c9
+
ae23c9
+    if (fn && !fn(job)) {
ae23c9
+        job_unlock();
ae23c9
+        return;
ae23c9
+    }
ae23c9
+
ae23c9
+    assert(!job->deferred_to_main_loop);
ae23c9
+    timer_del(&job->sleep_timer);
ae23c9
+    job->busy = true;
ae23c9
+    job_unlock();
ae23c9
+    aio_co_wake(job->co);
ae23c9
+}
ae23c9
+
ae23c9
+/* Yield, and schedule a timer to reenter the coroutine after @ns nanoseconds.
ae23c9
+ * Reentering the job coroutine with block_job_enter() before the timer has
ae23c9
+ * expired is allowed and cancels the timer.
ae23c9
+ *
ae23c9
+ * If @ns is (uint64_t) -1, no timer is scheduled and block_job_enter() must be
ae23c9
+ * called explicitly. */
ae23c9
+void coroutine_fn job_do_yield(Job *job, uint64_t ns)
ae23c9
+{
ae23c9
+    job_lock();
ae23c9
+    if (ns != -1) {
ae23c9
+        timer_mod(&job->sleep_timer, ns);
ae23c9
+    }
ae23c9
+    job->busy = false;
ae23c9
+    job_unlock();
ae23c9
+    qemu_coroutine_yield();
ae23c9
+
ae23c9
+    /* Set by job_enter_cond() before re-entering the coroutine.  */
ae23c9
+    assert(job->busy);
ae23c9
+}
ae23c9
+
ae23c9
+void coroutine_fn job_pause_point(Job *job)
ae23c9
+{
ae23c9
+    assert(job && job_started(job));
ae23c9
+
ae23c9
+    if (!job_should_pause(job)) {
ae23c9
+        return;
ae23c9
+    }
ae23c9
+    if (job_is_cancelled(job)) {
ae23c9
+        return;
ae23c9
+    }
ae23c9
+
ae23c9
+    if (job->driver->pause) {
ae23c9
+        job->driver->pause(job);
ae23c9
+    }
ae23c9
+
ae23c9
+    if (job_should_pause(job) && !job_is_cancelled(job)) {
ae23c9
+        JobStatus status = job->status;
ae23c9
+        job_state_transition(job, status == JOB_STATUS_READY
ae23c9
+                                  ? JOB_STATUS_STANDBY
ae23c9
+                                  : JOB_STATUS_PAUSED);
ae23c9
+        job->paused = true;
ae23c9
+        job_do_yield(job, -1);
ae23c9
+        job->paused = false;
ae23c9
+        job_state_transition(job, status);
ae23c9
+    }
ae23c9
+
ae23c9
+    if (job->driver->resume) {
ae23c9
+        job->driver->resume(job);
ae23c9
+    }
ae23c9
+}
ae23c9
+
ae23c9
+/**
ae23c9
+ * All jobs must allow a pause point before entering their job proper. This
ae23c9
+ * ensures that jobs can be paused prior to being started, then resumed later.
ae23c9
+ */
ae23c9
+static void coroutine_fn job_co_entry(void *opaque)
ae23c9
+{
ae23c9
+    Job *job = opaque;
ae23c9
+
ae23c9
+    assert(job && job->driver && job->driver->start);
ae23c9
+    job_pause_point(job);
ae23c9
+    job->driver->start(job);
ae23c9
+}
ae23c9
+
ae23c9
+
ae23c9
+void job_start(Job *job)
ae23c9
+{
ae23c9
+    assert(job && !job_started(job) && job->paused &&
ae23c9
+           job->driver && job->driver->start);
ae23c9
+    job->co = qemu_coroutine_create(job_co_entry, job);
ae23c9
+    job->pause_count--;
ae23c9
+    job->busy = true;
ae23c9
+    job->paused = false;
ae23c9
+    job_state_transition(job, JOB_STATUS_RUNNING);
ae23c9
+    aio_co_enter(job->aio_context, job->co);
ae23c9
+}
ae23c9
+
ae23c9
 typedef struct {
ae23c9
     Job *job;
ae23c9
     JobDeferToMainLoopFn *fn;
ae23c9
diff --git a/tests/test-bdrv-drain.c b/tests/test-bdrv-drain.c
ae23c9
index 4f8cba8..c9f2f9b 100644
ae23c9
--- a/tests/test-bdrv-drain.c
ae23c9
+++ b/tests/test-bdrv-drain.c
ae23c9
@@ -524,8 +524,8 @@ BlockJobDriver test_job_driver = {
ae23c9
     .job_driver = {
ae23c9
         .instance_size  = sizeof(TestBlockJob),
ae23c9
         .free           = block_job_free,
ae23c9
+        .start          = test_job_start,
ae23c9
     },
ae23c9
-    .start          = test_job_start,
ae23c9
     .complete       = test_job_complete,
ae23c9
 };
ae23c9
 
ae23c9
@@ -549,47 +549,47 @@ static void test_blockjob_common(enum drain_type drain_type)
ae23c9
     job = block_job_create("job0", &test_job_driver, NULL, src, 0, BLK_PERM_ALL,
ae23c9
                            0, 0, NULL, NULL, &error_abort);
ae23c9
     block_job_add_bdrv(job, "target", target, 0, BLK_PERM_ALL, &error_abort);
ae23c9
-    block_job_start(job);
ae23c9
+    job_start(&job->job);
ae23c9
 
ae23c9
-    g_assert_cmpint(job->pause_count, ==, 0);
ae23c9
-    g_assert_false(job->paused);
ae23c9
-    g_assert_false(job->busy); /* We're in block_job_sleep_ns() */
ae23c9
+    g_assert_cmpint(job->job.pause_count, ==, 0);
ae23c9
+    g_assert_false(job->job.paused);
ae23c9
+    g_assert_false(job->job.busy); /* We're in block_job_sleep_ns() */
ae23c9
 
ae23c9
     do_drain_begin(drain_type, src);
ae23c9
 
ae23c9
     if (drain_type == BDRV_DRAIN_ALL) {
ae23c9
         /* bdrv_drain_all() drains both src and target */
ae23c9
-        g_assert_cmpint(job->pause_count, ==, 2);
ae23c9
+        g_assert_cmpint(job->job.pause_count, ==, 2);
ae23c9
     } else {
ae23c9
-        g_assert_cmpint(job->pause_count, ==, 1);
ae23c9
+        g_assert_cmpint(job->job.pause_count, ==, 1);
ae23c9
     }
ae23c9
     /* XXX We don't wait until the job is actually paused. Is this okay? */
ae23c9
-    /* g_assert_true(job->paused); */
ae23c9
-    g_assert_false(job->busy); /* The job is paused */
ae23c9
+    /* g_assert_true(job->job.paused); */
ae23c9
+    g_assert_false(job->job.busy); /* The job is paused */
ae23c9
 
ae23c9
     do_drain_end(drain_type, src);
ae23c9
 
ae23c9
-    g_assert_cmpint(job->pause_count, ==, 0);
ae23c9
-    g_assert_false(job->paused);
ae23c9
-    g_assert_false(job->busy); /* We're in block_job_sleep_ns() */
ae23c9
+    g_assert_cmpint(job->job.pause_count, ==, 0);
ae23c9
+    g_assert_false(job->job.paused);
ae23c9
+    g_assert_false(job->job.busy); /* We're in block_job_sleep_ns() */
ae23c9
 
ae23c9
     do_drain_begin(drain_type, target);
ae23c9
 
ae23c9
     if (drain_type == BDRV_DRAIN_ALL) {
ae23c9
         /* bdrv_drain_all() drains both src and target */
ae23c9
-        g_assert_cmpint(job->pause_count, ==, 2);
ae23c9
+        g_assert_cmpint(job->job.pause_count, ==, 2);
ae23c9
     } else {
ae23c9
-        g_assert_cmpint(job->pause_count, ==, 1);
ae23c9
+        g_assert_cmpint(job->job.pause_count, ==, 1);
ae23c9
     }
ae23c9
     /* XXX We don't wait until the job is actually paused. Is this okay? */
ae23c9
-    /* g_assert_true(job->paused); */
ae23c9
-    g_assert_false(job->busy); /* The job is paused */
ae23c9
+    /* g_assert_true(job->job.paused); */
ae23c9
+    g_assert_false(job->job.busy); /* The job is paused */
ae23c9
 
ae23c9
     do_drain_end(drain_type, target);
ae23c9
 
ae23c9
-    g_assert_cmpint(job->pause_count, ==, 0);
ae23c9
-    g_assert_false(job->paused);
ae23c9
-    g_assert_false(job->busy); /* We're in block_job_sleep_ns() */
ae23c9
+    g_assert_cmpint(job->job.pause_count, ==, 0);
ae23c9
+    g_assert_false(job->job.paused);
ae23c9
+    g_assert_false(job->job.busy); /* We're in block_job_sleep_ns() */
ae23c9
 
ae23c9
     ret = block_job_complete_sync(job, &error_abort);
ae23c9
     g_assert_cmpint(ret, ==, 0);
ae23c9
diff --git a/tests/test-blockjob-txn.c b/tests/test-blockjob-txn.c
ae23c9
index c03f966..323e154 100644
ae23c9
--- a/tests/test-blockjob-txn.c
ae23c9
+++ b/tests/test-blockjob-txn.c
ae23c9
@@ -78,8 +78,8 @@ static const BlockJobDriver test_block_job_driver = {
ae23c9
     .job_driver = {
ae23c9
         .instance_size = sizeof(TestBlockJob),
ae23c9
         .free          = block_job_free,
ae23c9
+        .start         = test_block_job_run,
ae23c9
     },
ae23c9
-    .start = test_block_job_run,
ae23c9
 };
ae23c9
 
ae23c9
 /* Create a block job that completes with a given return code after a given
ae23c9
@@ -125,7 +125,7 @@ static void test_single_job(int expected)
ae23c9
 
ae23c9
     txn = block_job_txn_new();
ae23c9
     job = test_block_job_start(1, true, expected, &result, txn);
ae23c9
-    block_job_start(job);
ae23c9
+    job_start(&job->job);
ae23c9
 
ae23c9
     if (expected == -ECANCELED) {
ae23c9
         block_job_cancel(job, false);
ae23c9
@@ -165,8 +165,8 @@ static void test_pair_jobs(int expected1, int expected2)
ae23c9
     txn = block_job_txn_new();
ae23c9
     job1 = test_block_job_start(1, true, expected1, &result1, txn);
ae23c9
     job2 = test_block_job_start(2, true, expected2, &result2, txn);
ae23c9
-    block_job_start(job1);
ae23c9
-    block_job_start(job2);
ae23c9
+    job_start(&job1->job);
ae23c9
+    job_start(&job2->job);
ae23c9
 
ae23c9
     /* Release our reference now to trigger as many nice
ae23c9
      * use-after-free bugs as possible.
ae23c9
@@ -227,8 +227,8 @@ static void test_pair_jobs_fail_cancel_race(void)
ae23c9
     txn = block_job_txn_new();
ae23c9
     job1 = test_block_job_start(1, true, -ECANCELED, &result1, txn);
ae23c9
     job2 = test_block_job_start(2, false, 0, &result2, txn);
ae23c9
-    block_job_start(job1);
ae23c9
-    block_job_start(job2);
ae23c9
+    job_start(&job1->job);
ae23c9
+    job_start(&job2->job);
ae23c9
 
ae23c9
     block_job_cancel(job1, false);
ae23c9
 
ae23c9
diff --git a/tests/test-blockjob.c b/tests/test-blockjob.c
ae23c9
index 5f43bd7..1d18325 100644
ae23c9
--- a/tests/test-blockjob.c
ae23c9
+++ b/tests/test-blockjob.c
ae23c9
@@ -199,8 +199,8 @@ static const BlockJobDriver test_cancel_driver = {
ae23c9
     .job_driver = {
ae23c9
         .instance_size = sizeof(CancelJob),
ae23c9
         .free          = block_job_free,
ae23c9
+        .start         = cancel_job_start,
ae23c9
     },
ae23c9
-    .start         = cancel_job_start,
ae23c9
     .complete      = cancel_job_complete,
ae23c9
 };
ae23c9
 
ae23c9
@@ -254,7 +254,7 @@ static void test_cancel_running(void)
ae23c9
 
ae23c9
     s = create_common(&job;;
ae23c9
 
ae23c9
-    block_job_start(job);
ae23c9
+    job_start(&job->job);
ae23c9
     assert(job->job.status == JOB_STATUS_RUNNING);
ae23c9
 
ae23c9
     cancel_common(s);
ae23c9
@@ -267,7 +267,7 @@ static void test_cancel_paused(void)
ae23c9
 
ae23c9
     s = create_common(&job;;
ae23c9
 
ae23c9
-    block_job_start(job);
ae23c9
+    job_start(&job->job);
ae23c9
     assert(job->job.status == JOB_STATUS_RUNNING);
ae23c9
 
ae23c9
     block_job_user_pause(job, &error_abort);
ae23c9
@@ -284,7 +284,7 @@ static void test_cancel_ready(void)
ae23c9
 
ae23c9
     s = create_common(&job;;
ae23c9
 
ae23c9
-    block_job_start(job);
ae23c9
+    job_start(&job->job);
ae23c9
     assert(job->job.status == JOB_STATUS_RUNNING);
ae23c9
 
ae23c9
     s->should_converge = true;
ae23c9
@@ -301,7 +301,7 @@ static void test_cancel_standby(void)
ae23c9
 
ae23c9
     s = create_common(&job;;
ae23c9
 
ae23c9
-    block_job_start(job);
ae23c9
+    job_start(&job->job);
ae23c9
     assert(job->job.status == JOB_STATUS_RUNNING);
ae23c9
 
ae23c9
     s->should_converge = true;
ae23c9
@@ -322,7 +322,7 @@ static void test_cancel_pending(void)
ae23c9
 
ae23c9
     s = create_common(&job;;
ae23c9
 
ae23c9
-    block_job_start(job);
ae23c9
+    job_start(&job->job);
ae23c9
     assert(job->job.status == JOB_STATUS_RUNNING);
ae23c9
 
ae23c9
     s->should_converge = true;
ae23c9
@@ -346,7 +346,7 @@ static void test_cancel_concluded(void)
ae23c9
 
ae23c9
     s = create_common(&job;;
ae23c9
 
ae23c9
-    block_job_start(job);
ae23c9
+    job_start(&job->job);
ae23c9
     assert(job->job.status == JOB_STATUS_RUNNING);
ae23c9
 
ae23c9
     s->should_converge = true;
ae23c9
-- 
ae23c9
1.8.3.1
ae23c9