26ba25
From 426d98dc12ef863627d90890278e6de92a81446f Mon Sep 17 00:00:00 2001
26ba25
From: Kevin Wolf <kwolf@redhat.com>
26ba25
Date: Tue, 26 Jun 2018 09:48:23 +0200
26ba25
Subject: [PATCH 115/268] job: Move completion and cancellation to Job
26ba25
26ba25
RH-Author: Kevin Wolf <kwolf@redhat.com>
26ba25
Message-id: <20180626094856.6924-41-kwolf@redhat.com>
26ba25
Patchwork-id: 81116
26ba25
O-Subject: [RHV-7.6 qemu-kvm-rhev PATCH v2 40/73] job: Move completion and cancellation to Job
26ba25
Bugzilla: 1513543
26ba25
RH-Acked-by: Jeffrey Cody <jcody@redhat.com>
26ba25
RH-Acked-by: Max Reitz <mreitz@redhat.com>
26ba25
RH-Acked-by: Fam Zheng <famz@redhat.com>
26ba25
26ba25
This moves the top-level job completion and cancellation functions from
26ba25
BlockJob to Job.
26ba25
26ba25
Signed-off-by: Kevin Wolf <kwolf@redhat.com>
26ba25
(cherry picked from commit 3d70ff53b6bf90d9eec6f97024ec9895f6799d9e)
26ba25
Signed-off-by: Kevin Wolf <kwolf@redhat.com>
26ba25
Signed-off-by: Miroslav Rezanina <mrezanin@redhat.com>
26ba25
---
26ba25
 block.c                      |  4 ++-
26ba25
 block/backup.c               |  3 +-
26ba25
 block/commit.c               |  6 ++--
26ba25
 block/mirror.c               |  6 ++--
26ba25
 block/replication.c          |  4 +--
26ba25
 block/stream.c               |  2 +-
26ba25
 block/trace-events           |  3 --
26ba25
 blockdev.c                   |  8 ++---
26ba25
 blockjob.c                   | 76 ---------------------------------------
26ba25
 include/block/blockjob.h     | 55 -----------------------------
26ba25
 include/block/blockjob_int.h | 18 ----------
26ba25
 include/qemu/job.h           | 68 +++++++++++++++++++++++++++++------
26ba25
 job.c                        | 84 +++++++++++++++++++++++++++++++++++++++-----
26ba25
 qemu-img.c                   |  2 +-
26ba25
 tests/test-bdrv-drain.c      |  5 ++-
26ba25
 tests/test-blockjob-txn.c    | 14 ++++----
26ba25
 tests/test-blockjob.c        | 21 ++++++-----
26ba25
 trace-events                 |  1 +
26ba25
 18 files changed, 171 insertions(+), 209 deletions(-)
26ba25
26ba25
diff --git a/block.c b/block.c
26ba25
index d991a09..a83787f 100644
26ba25
--- a/block.c
26ba25
+++ b/block.c
26ba25
@@ -3374,7 +3374,9 @@ static void bdrv_close(BlockDriverState *bs)
26ba25
 
26ba25
 void bdrv_close_all(void)
26ba25
 {
26ba25
-    block_job_cancel_sync_all();
26ba25
+    /* TODO We do want to cancel all jobs instead of just block jobs on
26ba25
+     * shutdown, but bdrv_close_all() isn't the right place any more. */
26ba25
+    job_cancel_sync_all();
26ba25
     nbd_export_close_all();
26ba25
 
26ba25
     /* Drop references from requests still in flight, such as canceled block
26ba25
diff --git a/block/backup.c b/block/backup.c
26ba25
index 6172f90..b13f91d 100644
26ba25
--- a/block/backup.c
26ba25
+++ b/block/backup.c
26ba25
@@ -319,10 +319,9 @@ typedef struct {
26ba25
 
26ba25
 static void backup_complete(Job *job, void *opaque)
26ba25
 {
26ba25
-    BlockJob *bjob = container_of(job, BlockJob, job);
26ba25
     BackupCompleteData *data = opaque;
26ba25
 
26ba25
-    block_job_completed(bjob, data->ret);
26ba25
+    job_completed(job, data->ret);
26ba25
     g_free(data);
26ba25
 }
26ba25
 
26ba25
diff --git a/block/commit.c b/block/commit.c
26ba25
index 40d97a3..b0a847e 100644
26ba25
--- a/block/commit.c
26ba25
+++ b/block/commit.c
26ba25
@@ -112,12 +112,12 @@ static void commit_complete(Job *job, void *opaque)
26ba25
     blk_unref(s->top);
26ba25
 
26ba25
     /* If there is more than one reference to the job (e.g. if called from
26ba25
-     * job_finish_sync()), block_job_completed() won't free it and therefore
26ba25
-     * the blockers on the intermediate nodes remain. This would cause
26ba25
+     * job_finish_sync()), job_completed() won't free it and therefore the
26ba25
+     * blockers on the intermediate nodes remain. This would cause
26ba25
      * bdrv_set_backing_hd() to fail. */
26ba25
     block_job_remove_all_bdrv(bjob);
26ba25
 
26ba25
-    block_job_completed(&s->common, ret);
26ba25
+    job_completed(job, ret);
26ba25
     g_free(data);
26ba25
 
26ba25
     /* If bdrv_drop_intermediate() didn't already do that, remove the commit
26ba25
diff --git a/block/mirror.c b/block/mirror.c
26ba25
index 656237a..c63cf7c 100644
26ba25
--- a/block/mirror.c
26ba25
+++ b/block/mirror.c
26ba25
@@ -498,7 +498,7 @@ static void mirror_exit(Job *job, void *opaque)
26ba25
     bdrv_release_dirty_bitmap(src, s->dirty_bitmap);
26ba25
 
26ba25
     /* Make sure that the source BDS doesn't go away before we called
26ba25
-     * block_job_completed(). */
26ba25
+     * job_completed(). */
26ba25
     bdrv_ref(src);
26ba25
     bdrv_ref(mirror_top_bs);
26ba25
     bdrv_ref(target_bs);
26ba25
@@ -581,7 +581,7 @@ static void mirror_exit(Job *job, void *opaque)
26ba25
     blk_set_perm(bjob->blk, 0, BLK_PERM_ALL, &error_abort);
26ba25
     blk_insert_bs(bjob->blk, mirror_top_bs, &error_abort);
26ba25
 
26ba25
-    block_job_completed(&s->common, data->ret);
26ba25
+    job_completed(job, data->ret);
26ba25
 
26ba25
     g_free(data);
26ba25
     bdrv_drained_end(src);
26ba25
@@ -954,7 +954,7 @@ static void mirror_complete(Job *job, Error **errp)
26ba25
     }
26ba25
 
26ba25
     s->should_complete = true;
26ba25
-    block_job_enter(&s->common);
26ba25
+    job_enter(job);
26ba25
 }
26ba25
 
26ba25
 static void mirror_pause(Job *job)
26ba25
diff --git a/block/replication.c b/block/replication.c
26ba25
index 8241400..f3b2839 100644
26ba25
--- a/block/replication.c
26ba25
+++ b/block/replication.c
26ba25
@@ -145,7 +145,7 @@ static void replication_close(BlockDriverState *bs)
26ba25
         replication_stop(s->rs, false, NULL);
26ba25
     }
26ba25
     if (s->stage == BLOCK_REPLICATION_FAILOVER) {
26ba25
-        block_job_cancel_sync(s->active_disk->bs->job);
26ba25
+        job_cancel_sync(&s->active_disk->bs->job->job);
26ba25
     }
26ba25
 
26ba25
     if (s->mode == REPLICATION_MODE_SECONDARY) {
26ba25
@@ -679,7 +679,7 @@ static void replication_stop(ReplicationState *rs, bool failover, Error **errp)
26ba25
          * disk, secondary disk in backup_job_completed().
26ba25
          */
26ba25
         if (s->secondary_disk->bs->job) {
26ba25
-            block_job_cancel_sync(s->secondary_disk->bs->job);
26ba25
+            job_cancel_sync(&s->secondary_disk->bs->job->job);
26ba25
         }
26ba25
 
26ba25
         if (!failover) {
26ba25
diff --git a/block/stream.c b/block/stream.c
26ba25
index b996278..8546c41 100644
26ba25
--- a/block/stream.c
26ba25
+++ b/block/stream.c
26ba25
@@ -93,7 +93,7 @@ out:
26ba25
     }
26ba25
 
26ba25
     g_free(s->backing_file_str);
26ba25
-    block_job_completed(&s->common, data->ret);
26ba25
+    job_completed(job, data->ret);
26ba25
     g_free(data);
26ba25
 }
26ba25
 
26ba25
diff --git a/block/trace-events b/block/trace-events
26ba25
index 93b9279..2d59b53 100644
26ba25
--- a/block/trace-events
26ba25
+++ b/block/trace-events
26ba25
@@ -4,9 +4,6 @@
26ba25
 bdrv_open_common(void *bs, const char *filename, int flags, const char *format_name) "bs %p filename \"%s\" flags 0x%x format_name \"%s\""
26ba25
 bdrv_lock_medium(void *bs, bool locked) "bs %p locked %d"
26ba25
 
26ba25
-# blockjob.c
26ba25
-block_job_completed(void *job, int ret, int jret) "job %p ret %d corrected ret %d"
26ba25
-
26ba25
 # block/block-backend.c
26ba25
 blk_co_preadv(void *blk, void *bs, int64_t offset, unsigned int bytes, int flags) "blk %p bs %p offset %"PRId64" bytes %u flags 0x%x"
26ba25
 blk_co_pwritev(void *blk, void *bs, int64_t offset, unsigned int bytes, int flags) "blk %p bs %p offset %"PRId64" bytes %u flags 0x%x"
26ba25
diff --git a/blockdev.c b/blockdev.c
26ba25
index 9aa2e79..c768e68 100644
26ba25
--- a/blockdev.c
26ba25
+++ b/blockdev.c
26ba25
@@ -151,7 +151,7 @@ void blockdev_mark_auto_del(BlockBackend *blk)
26ba25
         aio_context_acquire(aio_context);
26ba25
 
26ba25
         if (bs->job) {
26ba25
-            block_job_cancel(bs->job, false);
26ba25
+            job_cancel(&bs->job->job, false);
26ba25
         }
26ba25
 
26ba25
         aio_context_release(aio_context);
26ba25
@@ -1926,7 +1926,7 @@ static void drive_backup_abort(BlkActionState *common)
26ba25
         aio_context = bdrv_get_aio_context(state->bs);
26ba25
         aio_context_acquire(aio_context);
26ba25
 
26ba25
-        block_job_cancel_sync(state->job);
26ba25
+        job_cancel_sync(&state->job->job);
26ba25
 
26ba25
         aio_context_release(aio_context);
26ba25
     }
26ba25
@@ -2024,7 +2024,7 @@ static void blockdev_backup_abort(BlkActionState *common)
26ba25
         aio_context = bdrv_get_aio_context(state->bs);
26ba25
         aio_context_acquire(aio_context);
26ba25
 
26ba25
-        block_job_cancel_sync(state->job);
26ba25
+        job_cancel_sync(&state->job->job);
26ba25
 
26ba25
         aio_context_release(aio_context);
26ba25
     }
26ba25
@@ -3852,7 +3852,7 @@ void qmp_block_job_cancel(const char *device,
26ba25
     }
26ba25
 
26ba25
     trace_qmp_block_job_cancel(job);
26ba25
-    block_job_user_cancel(job, force, errp);
26ba25
+    job_user_cancel(&job->job, force, errp);
26ba25
 out:
26ba25
     aio_context_release(aio_context);
26ba25
 }
26ba25
diff --git a/blockjob.c b/blockjob.c
26ba25
index 14b21c8..438baa1 100644
26ba25
--- a/blockjob.c
26ba25
+++ b/blockjob.c
26ba25
@@ -255,63 +255,6 @@ void block_job_dismiss(BlockJob **jobptr, Error **errp)
26ba25
     *jobptr = NULL;
26ba25
 }
26ba25
 
26ba25
-void block_job_cancel(BlockJob *job, bool force)
26ba25
-{
26ba25
-    if (job->job.status == JOB_STATUS_CONCLUDED) {
26ba25
-        job_do_dismiss(&job->job);
26ba25
-        return;
26ba25
-    }
26ba25
-    job_cancel_async(&job->job, force);
26ba25
-    if (!job_started(&job->job)) {
26ba25
-        block_job_completed(job, -ECANCELED);
26ba25
-    } else if (job->job.deferred_to_main_loop) {
26ba25
-        job_completed_txn_abort(&job->job);
26ba25
-    } else {
26ba25
-        block_job_enter(job);
26ba25
-    }
26ba25
-}
26ba25
-
26ba25
-void block_job_user_cancel(BlockJob *job, bool force, Error **errp)
26ba25
-{
26ba25
-    if (job_apply_verb(&job->job, JOB_VERB_CANCEL, errp)) {
26ba25
-        return;
26ba25
-    }
26ba25
-    block_job_cancel(job, force);
26ba25
-}
26ba25
-
26ba25
-/* A wrapper around block_job_cancel() taking an Error ** parameter so it may be
26ba25
- * used with job_finish_sync() without the need for (rather nasty) function
26ba25
- * pointer casts there. */
26ba25
-static void block_job_cancel_err(Job *job, Error **errp)
26ba25
-{
26ba25
-    BlockJob *bjob = container_of(job, BlockJob, job);
26ba25
-    assert(is_block_job(job));
26ba25
-    block_job_cancel(bjob, false);
26ba25
-}
26ba25
-
26ba25
-int block_job_cancel_sync(BlockJob *job)
26ba25
-{
26ba25
-    return job_finish_sync(&job->job, &block_job_cancel_err, NULL);
26ba25
-}
26ba25
-
26ba25
-void block_job_cancel_sync_all(void)
26ba25
-{
26ba25
-    BlockJob *job;
26ba25
-    AioContext *aio_context;
26ba25
-
26ba25
-    while ((job = block_job_next(NULL))) {
26ba25
-        aio_context = blk_get_aio_context(job->blk);
26ba25
-        aio_context_acquire(aio_context);
26ba25
-        block_job_cancel_sync(job);
26ba25
-        aio_context_release(aio_context);
26ba25
-    }
26ba25
-}
26ba25
-
26ba25
-int block_job_complete_sync(BlockJob *job, Error **errp)
26ba25
-{
26ba25
-    return job_finish_sync(&job->job, job_complete, errp);
26ba25
-}
26ba25
-
26ba25
 void block_job_progress_update(BlockJob *job, uint64_t done)
26ba25
 {
26ba25
     job->offset += done;
26ba25
@@ -488,25 +431,6 @@ void *block_job_create(const char *job_id, const BlockJobDriver *driver,
26ba25
     return job;
26ba25
 }
26ba25
 
26ba25
-void block_job_completed(BlockJob *job, int ret)
26ba25
-{
26ba25
-    assert(job && job->job.txn && !job_is_completed(&job->job));
26ba25
-    assert(blk_bs(job->blk)->job == job);
26ba25
-    job->job.ret = ret;
26ba25
-    job_update_rc(&job->job);
26ba25
-    trace_block_job_completed(job, ret, job->job.ret);
26ba25
-    if (job->job.ret) {
26ba25
-        job_completed_txn_abort(&job->job);
26ba25
-    } else {
26ba25
-        job_completed_txn_success(&job->job);
26ba25
-    }
26ba25
-}
26ba25
-
26ba25
-void block_job_enter(BlockJob *job)
26ba25
-{
26ba25
-    job_enter_cond(&job->job, NULL);
26ba25
-}
26ba25
-
26ba25
 void block_job_yield(BlockJob *job)
26ba25
 {
26ba25
     assert(job->job.busy);
26ba25
diff --git a/include/block/blockjob.h b/include/block/blockjob.h
26ba25
index 09e6bb4..e9ed7b8 100644
26ba25
--- a/include/block/blockjob.h
26ba25
+++ b/include/block/blockjob.h
26ba25
@@ -141,15 +141,6 @@ void block_job_remove_all_bdrv(BlockJob *job);
26ba25
 void block_job_set_speed(BlockJob *job, int64_t speed, Error **errp);
26ba25
 
26ba25
 /**
26ba25
- * block_job_cancel:
26ba25
- * @job: The job to be canceled.
26ba25
- * @force: Quit a job without waiting for data to be in sync.
26ba25
- *
26ba25
- * Asynchronously cancel the specified job.
26ba25
- */
26ba25
-void block_job_cancel(BlockJob *job, bool force);
26ba25
-
26ba25
-/**
26ba25
  * block_job_dismiss:
26ba25
  * @job: The job to be dismissed.
26ba25
  * @errp: Error object.
26ba25
@@ -186,52 +177,6 @@ void block_job_progress_set_remaining(BlockJob *job, uint64_t remaining);
26ba25
 BlockJobInfo *block_job_query(BlockJob *job, Error **errp);
26ba25
 
26ba25
 /**
26ba25
- * block_job_user_cancel:
26ba25
- * @job: The job to be cancelled.
26ba25
- * @force: Quit a job without waiting for data to be in sync.
26ba25
- *
26ba25
- * Cancels the specified job, but may refuse to do so if the
26ba25
- * operation isn't currently meaningful.
26ba25
- */
26ba25
-void block_job_user_cancel(BlockJob *job, bool force, Error **errp);
26ba25
-
26ba25
-/**
26ba25
- * block_job_cancel_sync:
26ba25
- * @job: The job to be canceled.
26ba25
- *
26ba25
- * Synchronously cancel the job.  The completion callback is called
26ba25
- * before the function returns.  The job may actually complete
26ba25
- * instead of canceling itself; the circumstances under which this
26ba25
- * happens depend on the kind of job that is active.
26ba25
- *
26ba25
- * Returns the return value from the job if the job actually completed
26ba25
- * during the call, or -ECANCELED if it was canceled.
26ba25
- */
26ba25
-int block_job_cancel_sync(BlockJob *job);
26ba25
-
26ba25
-/**
26ba25
- * block_job_cancel_sync_all:
26ba25
- *
26ba25
- * Synchronously cancels all jobs using block_job_cancel_sync().
26ba25
- */
26ba25
-void block_job_cancel_sync_all(void);
26ba25
-
26ba25
-/**
26ba25
- * block_job_complete_sync:
26ba25
- * @job: The job to be completed.
26ba25
- * @errp: Error object which may be set by block_job_complete(); this is not
26ba25
- *        necessarily set on every error, the job return value has to be
26ba25
- *        checked as well.
26ba25
- *
26ba25
- * Synchronously complete the job.  The completion callback is called before the
26ba25
- * function returns, unless it is NULL (which is permissible when using this
26ba25
- * function).
26ba25
- *
26ba25
- * Returns the return value from the job.
26ba25
- */
26ba25
-int block_job_complete_sync(BlockJob *job, Error **errp);
26ba25
-
26ba25
-/**
26ba25
  * block_job_iostatus_reset:
26ba25
  * @job: The job whose I/O status should be reset.
26ba25
  *
26ba25
diff --git a/include/block/blockjob_int.h b/include/block/blockjob_int.h
26ba25
index 29a2802..7df07b2 100644
26ba25
--- a/include/block/blockjob_int.h
26ba25
+++ b/include/block/blockjob_int.h
26ba25
@@ -124,24 +124,6 @@ void block_job_yield(BlockJob *job);
26ba25
 int64_t block_job_ratelimit_get_delay(BlockJob *job, uint64_t n);
26ba25
 
26ba25
 /**
26ba25
- * block_job_completed:
26ba25
- * @job: The job being completed.
26ba25
- * @ret: The status code.
26ba25
- *
26ba25
- * Call the completion function that was registered at creation time, and
26ba25
- * free @job.
26ba25
- */
26ba25
-void block_job_completed(BlockJob *job, int ret);
26ba25
-
26ba25
-/**
26ba25
- * block_job_enter:
26ba25
- * @job: The job to enter.
26ba25
- *
26ba25
- * Continue the specified job by entering the coroutine.
26ba25
- */
26ba25
-void block_job_enter(BlockJob *job);
26ba25
-
26ba25
-/**
26ba25
  * block_job_event_ready:
26ba25
  * @job: The job which is now ready to be completed.
26ba25
  *
26ba25
diff --git a/include/qemu/job.h b/include/qemu/job.h
26ba25
index 39d74ab..bbe1b0c 100644
26ba25
--- a/include/qemu/job.h
26ba25
+++ b/include/qemu/job.h
26ba25
@@ -74,8 +74,8 @@ typedef struct Job {
26ba25
 
26ba25
     /**
26ba25
      * Set to false by the job while the coroutine has yielded and may be
26ba25
-     * re-entered by block_job_enter().  There may still be I/O or event loop
26ba25
-     * activity pending.  Accessed under block_job_mutex (in blockjob.c).
26ba25
+     * re-entered by job_enter(). There may still be I/O or event loop activity
26ba25
+     * pending. Accessed under block_job_mutex (in blockjob.c).
26ba25
      */
26ba25
     bool busy;
26ba25
 
26ba25
@@ -114,7 +114,7 @@ typedef struct Job {
26ba25
     /** True if this job should automatically dismiss itself */
26ba25
     bool auto_dismiss;
26ba25
 
26ba25
-    /** ret code passed to block_job_completed. */
26ba25
+    /** ret code passed to job_completed. */
26ba25
     int ret;
26ba25
 
26ba25
     /** The completion function that will be called when the job completes.  */
26ba25
@@ -266,8 +266,8 @@ void job_txn_unref(JobTxn *txn);
26ba25
  * @job: Job to add to the transaction
26ba25
  *
26ba25
  * Add @job to the transaction.  The @job must not already be in a transaction.
26ba25
- * The caller must call either job_txn_unref() or block_job_completed() to
26ba25
- * release the reference that is automatically grabbed here.
26ba25
+ * The caller must call either job_txn_unref() or job_completed() to release
26ba25
+ * the reference that is automatically grabbed here.
26ba25
  *
26ba25
  * If @txn is NULL, the function does nothing.
26ba25
  */
26ba25
@@ -416,8 +416,59 @@ int job_apply_verb(Job *job, JobVerb verb, Error **errp);
26ba25
 /** The @job could not be started, free it. */
26ba25
 void job_early_fail(Job *job);
26ba25
 
26ba25
+/**
26ba25
+ * @job: The job being completed.
26ba25
+ * @ret: The status code.
26ba25
+ *
26ba25
+ * Marks @job as completed. If @ret is non-zero, the job transaction it is part
26ba25
+ * of is aborted. If @ret is zero, the job moves into the WAITING state. If it
26ba25
+ * is the last job to complete in its transaction, all jobs in the transaction
26ba25
+ * move from WAITING to PENDING.
26ba25
+ */
26ba25
+void job_completed(Job *job, int ret);
26ba25
+
26ba25
 /** Asynchronously complete the specified @job. */
26ba25
-void job_complete(Job *job, Error **errp);;
26ba25
+void job_complete(Job *job, Error **errp);
26ba25
+
26ba25
+/**
26ba25
+ * Asynchronously cancel the specified @job. If @force is true, the job should
26ba25
+ * be cancelled immediately without waiting for a consistent state.
26ba25
+ */
26ba25
+void job_cancel(Job *job, bool force);
26ba25
+
26ba25
+/**
26ba25
+ * Cancels the specified job like job_cancel(), but may refuse to do so if the
26ba25
+ * operation isn't meaningful in the current state of the job.
26ba25
+ */
26ba25
+void job_user_cancel(Job *job, bool force, Error **errp);
26ba25
+
26ba25
+/**
26ba25
+ * Synchronously cancel the @job.  The completion callback is called
26ba25
+ * before the function returns.  The job may actually complete
26ba25
+ * instead of canceling itself; the circumstances under which this
26ba25
+ * happens depend on the kind of job that is active.
26ba25
+ *
26ba25
+ * Returns the return value from the job if the job actually completed
26ba25
+ * during the call, or -ECANCELED if it was canceled.
26ba25
+ */
26ba25
+int job_cancel_sync(Job *job);
26ba25
+
26ba25
+/** Synchronously cancels all jobs using job_cancel_sync(). */
26ba25
+void job_cancel_sync_all(void);
26ba25
+
26ba25
+/**
26ba25
+ * @job: The job to be completed.
26ba25
+ * @errp: Error object which may be set by job_complete(); this is not
26ba25
+ *        necessarily set on every error, the job return value has to be
26ba25
+ *        checked as well.
26ba25
+ *
26ba25
+ * Synchronously complete the job.  The completion callback is called before the
26ba25
+ * function returns, unless it is NULL (which is permissible when using this
26ba25
+ * function).
26ba25
+ *
26ba25
+ * Returns the return value from the job.
26ba25
+ */
26ba25
+int job_complete_sync(Job *job, Error **errp);
26ba25
 
26ba25
 /**
26ba25
  * For a @job that has finished its work and is pending awaiting explicit
26ba25
@@ -459,11 +510,6 @@ int job_finish_sync(Job *job, void (*finish)(Job *, Error **errp), Error **errp)
26ba25
 void job_state_transition(Job *job, JobStatus s1);
26ba25
 void coroutine_fn job_do_yield(Job *job, uint64_t ns);
26ba25
 bool job_should_pause(Job *job);
26ba25
-bool job_started(Job *job);
26ba25
 void job_do_dismiss(Job *job);
26ba25
-void job_update_rc(Job *job);
26ba25
-void job_cancel_async(Job *job, bool force);
26ba25
-void job_completed_txn_abort(Job *job);
26ba25
-void job_completed_txn_success(Job *job);
26ba25
 
26ba25
 #endif
26ba25
diff --git a/job.c b/job.c
26ba25
index 4f6fd73..2e453f6 100644
26ba25
--- a/job.c
26ba25
+++ b/job.c
26ba25
@@ -221,7 +221,7 @@ bool job_is_completed(Job *job)
26ba25
     return false;
26ba25
 }
26ba25
 
26ba25
-bool job_started(Job *job)
26ba25
+static bool job_started(Job *job)
26ba25
 {
26ba25
     return job->co;
26ba25
 }
26ba25
@@ -391,10 +391,10 @@ void job_enter(Job *job)
26ba25
 }
26ba25
 
26ba25
 /* Yield, and schedule a timer to reenter the coroutine after @ns nanoseconds.
26ba25
- * Reentering the job coroutine with block_job_enter() before the timer has
26ba25
- * expired is allowed and cancels the timer.
26ba25
+ * Reentering the job coroutine with job_enter() before the timer has expired
26ba25
+ * is allowed and cancels the timer.
26ba25
  *
26ba25
- * If @ns is (uint64_t) -1, no timer is scheduled and block_job_enter() must be
26ba25
+ * If @ns is (uint64_t) -1, no timer is scheduled and job_enter() must be
26ba25
  * called explicitly. */
26ba25
 void coroutine_fn job_do_yield(Job *job, uint64_t ns)
26ba25
 {
26ba25
@@ -579,7 +579,7 @@ static void job_conclude(Job *job)
26ba25
     }
26ba25
 }
26ba25
 
26ba25
-void job_update_rc(Job *job)
26ba25
+static void job_update_rc(Job *job)
26ba25
 {
26ba25
     if (!job->ret && job_is_cancelled(job)) {
26ba25
         job->ret = -ECANCELED;
26ba25
@@ -644,7 +644,7 @@ static int job_finalize_single(Job *job)
26ba25
     return 0;
26ba25
 }
26ba25
 
26ba25
-void job_cancel_async(Job *job, bool force)
26ba25
+static void job_cancel_async(Job *job, bool force)
26ba25
 {
26ba25
     if (job->user_paused) {
26ba25
         /* Do not call job_enter here, the caller will handle it.  */
26ba25
@@ -660,7 +660,7 @@ void job_cancel_async(Job *job, bool force)
26ba25
     job->force_cancel |= force;
26ba25
 }
26ba25
 
26ba25
-void job_completed_txn_abort(Job *job)
26ba25
+static void job_completed_txn_abort(Job *job)
26ba25
 {
26ba25
     AioContext *ctx;
26ba25
     JobTxn *txn = job->txn;
26ba25
@@ -748,7 +748,7 @@ static int job_transition_to_pending(Job *job)
26ba25
     return 0;
26ba25
 }
26ba25
 
26ba25
-void job_completed_txn_success(Job *job)
26ba25
+static void job_completed_txn_success(Job *job)
26ba25
 {
26ba25
     JobTxn *txn = job->txn;
26ba25
     Job *other_job;
26ba25
@@ -774,6 +774,74 @@ void job_completed_txn_success(Job *job)
26ba25
     }
26ba25
 }
26ba25
 
26ba25
+void job_completed(Job *job, int ret)
26ba25
+{
26ba25
+    assert(job && job->txn && !job_is_completed(job));
26ba25
+    job->ret = ret;
26ba25
+    job_update_rc(job);
26ba25
+    trace_job_completed(job, ret, job->ret);
26ba25
+    if (job->ret) {
26ba25
+        job_completed_txn_abort(job);
26ba25
+    } else {
26ba25
+        job_completed_txn_success(job);
26ba25
+    }
26ba25
+}
26ba25
+
26ba25
+void job_cancel(Job *job, bool force)
26ba25
+{
26ba25
+    if (job->status == JOB_STATUS_CONCLUDED) {
26ba25
+        job_do_dismiss(job);
26ba25
+        return;
26ba25
+    }
26ba25
+    job_cancel_async(job, force);
26ba25
+    if (!job_started(job)) {
26ba25
+        job_completed(job, -ECANCELED);
26ba25
+    } else if (job->deferred_to_main_loop) {
26ba25
+        job_completed_txn_abort(job);
26ba25
+    } else {
26ba25
+        job_enter(job);
26ba25
+    }
26ba25
+}
26ba25
+
26ba25
+void job_user_cancel(Job *job, bool force, Error **errp)
26ba25
+{
26ba25
+    if (job_apply_verb(job, JOB_VERB_CANCEL, errp)) {
26ba25
+        return;
26ba25
+    }
26ba25
+    job_cancel(job, force);
26ba25
+}
26ba25
+
26ba25
+/* A wrapper around job_cancel() taking an Error ** parameter so it may be
26ba25
+ * used with job_finish_sync() without the need for (rather nasty) function
26ba25
+ * pointer casts there. */
26ba25
+static void job_cancel_err(Job *job, Error **errp)
26ba25
+{
26ba25
+    job_cancel(job, false);
26ba25
+}
26ba25
+
26ba25
+int job_cancel_sync(Job *job)
26ba25
+{
26ba25
+    return job_finish_sync(job, &job_cancel_err, NULL);
26ba25
+}
26ba25
+
26ba25
+void job_cancel_sync_all(void)
26ba25
+{
26ba25
+    Job *job;
26ba25
+    AioContext *aio_context;
26ba25
+
26ba25
+    while ((job = job_next(NULL))) {
26ba25
+        aio_context = job->aio_context;
26ba25
+        aio_context_acquire(aio_context);
26ba25
+        job_cancel_sync(job);
26ba25
+        aio_context_release(aio_context);
26ba25
+    }
26ba25
+}
26ba25
+
26ba25
+int job_complete_sync(Job *job, Error **errp)
26ba25
+{
26ba25
+    return job_finish_sync(job, job_complete, errp);
26ba25
+}
26ba25
+
26ba25
 void job_complete(Job *job, Error **errp)
26ba25
 {
26ba25
     /* Should not be reachable via external interface for internal jobs */
26ba25
diff --git a/qemu-img.c b/qemu-img.c
26ba25
index 91b3151..734ea94 100644
26ba25
--- a/qemu-img.c
26ba25
+++ b/qemu-img.c
26ba25
@@ -881,7 +881,7 @@ static void run_block_job(BlockJob *job, Error **errp)
26ba25
     } while (!job->ready && !job_is_completed(&job->job));
26ba25
 
26ba25
     if (!job_is_completed(&job->job)) {
26ba25
-        ret = block_job_complete_sync(job, errp);
26ba25
+        ret = job_complete_sync(&job->job, errp);
26ba25
     } else {
26ba25
         ret = job->job.ret;
26ba25
     }
26ba25
diff --git a/tests/test-bdrv-drain.c b/tests/test-bdrv-drain.c
26ba25
index b428aac..3600ffd 100644
26ba25
--- a/tests/test-bdrv-drain.c
26ba25
+++ b/tests/test-bdrv-drain.c
26ba25
@@ -498,8 +498,7 @@ typedef struct TestBlockJob {
26ba25
 
26ba25
 static void test_job_completed(Job *job, void *opaque)
26ba25
 {
26ba25
-    BlockJob *bjob = container_of(job, BlockJob, job);
26ba25
-    block_job_completed(bjob, 0);
26ba25
+    job_completed(job, 0);
26ba25
 }
26ba25
 
26ba25
 static void coroutine_fn test_job_start(void *opaque)
26ba25
@@ -593,7 +592,7 @@ static void test_blockjob_common(enum drain_type drain_type)
26ba25
     g_assert_false(job->job.paused);
26ba25
     g_assert_false(job->job.busy); /* We're in job_sleep_ns() */
26ba25
 
26ba25
-    ret = block_job_complete_sync(job, &error_abort);
26ba25
+    ret = job_complete_sync(&job->job, &error_abort);
26ba25
     g_assert_cmpint(ret, ==, 0);
26ba25
 
26ba25
     blk_unref(blk_src);
26ba25
diff --git a/tests/test-blockjob-txn.c b/tests/test-blockjob-txn.c
26ba25
index 6ee31d5..34ee179 100644
26ba25
--- a/tests/test-blockjob-txn.c
26ba25
+++ b/tests/test-blockjob-txn.c
26ba25
@@ -34,7 +34,7 @@ static void test_block_job_complete(Job *job, void *opaque)
26ba25
         rc = -ECANCELED;
26ba25
     }
26ba25
 
26ba25
-    block_job_completed(bjob, rc);
26ba25
+    job_completed(job, rc);
26ba25
     bdrv_unref(bs);
26ba25
 }
26ba25
 
26ba25
@@ -130,7 +130,7 @@ static void test_single_job(int expected)
26ba25
     job_start(&job->job);
26ba25
 
26ba25
     if (expected == -ECANCELED) {
26ba25
-        block_job_cancel(job, false);
26ba25
+        job_cancel(&job->job, false);
26ba25
     }
26ba25
 
26ba25
     while (result == -EINPROGRESS) {
26ba25
@@ -176,10 +176,10 @@ static void test_pair_jobs(int expected1, int expected2)
26ba25
     job_txn_unref(txn);
26ba25
 
26ba25
     if (expected1 == -ECANCELED) {
26ba25
-        block_job_cancel(job1, false);
26ba25
+        job_cancel(&job1->job, false);
26ba25
     }
26ba25
     if (expected2 == -ECANCELED) {
26ba25
-        block_job_cancel(job2, false);
26ba25
+        job_cancel(&job2->job, false);
26ba25
     }
26ba25
 
26ba25
     while (result1 == -EINPROGRESS || result2 == -EINPROGRESS) {
26ba25
@@ -232,13 +232,13 @@ static void test_pair_jobs_fail_cancel_race(void)
26ba25
     job_start(&job1->job);
26ba25
     job_start(&job2->job);
26ba25
 
26ba25
-    block_job_cancel(job1, false);
26ba25
+    job_cancel(&job1->job, false);
26ba25
 
26ba25
     /* Now make job2 finish before the main loop kicks jobs.  This simulates
26ba25
      * the race between a pending kick and another job completing.
26ba25
      */
26ba25
-    block_job_enter(job2);
26ba25
-    block_job_enter(job2);
26ba25
+    job_enter(&job2->job);
26ba25
+    job_enter(&job2->job);
26ba25
 
26ba25
     while (result1 == -EINPROGRESS || result2 == -EINPROGRESS) {
26ba25
         aio_poll(qemu_get_aio_context(), true);
26ba25
diff --git a/tests/test-blockjob.c b/tests/test-blockjob.c
26ba25
index 1e052c2..46a7873 100644
26ba25
--- a/tests/test-blockjob.c
26ba25
+++ b/tests/test-blockjob.c
26ba25
@@ -165,10 +165,9 @@ typedef struct CancelJob {
26ba25
 
26ba25
 static void cancel_job_completed(Job *job, void *opaque)
26ba25
 {
26ba25
-    BlockJob *bjob = container_of(job, BlockJob, job);
26ba25
     CancelJob *s = opaque;
26ba25
     s->completed = true;
26ba25
-    block_job_completed(bjob, 0);
26ba25
+    job_completed(job, 0);
26ba25
 }
26ba25
 
26ba25
 static void cancel_job_complete(Job *job, Error **errp)
26ba25
@@ -232,7 +231,7 @@ static void cancel_common(CancelJob *s)
26ba25
     BlockBackend *blk = s->blk;
26ba25
     JobStatus sts = job->job.status;
26ba25
 
26ba25
-    block_job_cancel_sync(job);
26ba25
+    job_cancel_sync(&job->job);
26ba25
     if (sts != JOB_STATUS_CREATED && sts != JOB_STATUS_CONCLUDED) {
26ba25
         BlockJob *dummy = job;
26ba25
         block_job_dismiss(&dummy, &error_abort);
26ba25
@@ -275,7 +274,7 @@ static void test_cancel_paused(void)
26ba25
     assert(job->job.status == JOB_STATUS_RUNNING);
26ba25
 
26ba25
     job_user_pause(&job->job, &error_abort);
26ba25
-    block_job_enter(job);
26ba25
+    job_enter(&job->job);
26ba25
     assert(job->job.status == JOB_STATUS_PAUSED);
26ba25
 
26ba25
     cancel_common(s);
26ba25
@@ -292,7 +291,7 @@ static void test_cancel_ready(void)
26ba25
     assert(job->job.status == JOB_STATUS_RUNNING);
26ba25
 
26ba25
     s->should_converge = true;
26ba25
-    block_job_enter(job);
26ba25
+    job_enter(&job->job);
26ba25
     assert(job->job.status == JOB_STATUS_READY);
26ba25
 
26ba25
     cancel_common(s);
26ba25
@@ -309,11 +308,11 @@ static void test_cancel_standby(void)
26ba25
     assert(job->job.status == JOB_STATUS_RUNNING);
26ba25
 
26ba25
     s->should_converge = true;
26ba25
-    block_job_enter(job);
26ba25
+    job_enter(&job->job);
26ba25
     assert(job->job.status == JOB_STATUS_READY);
26ba25
 
26ba25
     job_user_pause(&job->job, &error_abort);
26ba25
-    block_job_enter(job);
26ba25
+    job_enter(&job->job);
26ba25
     assert(job->job.status == JOB_STATUS_STANDBY);
26ba25
 
26ba25
     cancel_common(s);
26ba25
@@ -330,11 +329,11 @@ static void test_cancel_pending(void)
26ba25
     assert(job->job.status == JOB_STATUS_RUNNING);
26ba25
 
26ba25
     s->should_converge = true;
26ba25
-    block_job_enter(job);
26ba25
+    job_enter(&job->job);
26ba25
     assert(job->job.status == JOB_STATUS_READY);
26ba25
 
26ba25
     job_complete(&job->job, &error_abort);
26ba25
-    block_job_enter(job);
26ba25
+    job_enter(&job->job);
26ba25
     while (!s->completed) {
26ba25
         aio_poll(qemu_get_aio_context(), true);
26ba25
     }
26ba25
@@ -354,11 +353,11 @@ static void test_cancel_concluded(void)
26ba25
     assert(job->job.status == JOB_STATUS_RUNNING);
26ba25
 
26ba25
     s->should_converge = true;
26ba25
-    block_job_enter(job);
26ba25
+    job_enter(&job->job);
26ba25
     assert(job->job.status == JOB_STATUS_READY);
26ba25
 
26ba25
     job_complete(&job->job, &error_abort);
26ba25
-    block_job_enter(job);
26ba25
+    job_enter(&job->job);
26ba25
     while (!s->completed) {
26ba25
         aio_poll(qemu_get_aio_context(), true);
26ba25
     }
26ba25
diff --git a/trace-events b/trace-events
26ba25
index 2507e13..ef7579a 100644
26ba25
--- a/trace-events
26ba25
+++ b/trace-events
26ba25
@@ -107,6 +107,7 @@ gdbstub_err_checksum_incorrect(uint8_t expected, uint8_t got) "got command packe
26ba25
 # job.c
26ba25
 job_state_transition(void *job,  int ret, const char *legal, const char *s0, const char *s1) "job %p (ret: %d) attempting %s transition (%s-->%s)"
26ba25
 job_apply_verb(void *job, const char *state, const char *verb, const char *legal) "job %p in state %s; applying verb %s (%s)"
26ba25
+job_completed(void *job, int ret, int jret) "job %p ret %d corrected ret %d"
26ba25
 
26ba25
 ### Guest events, keep at bottom
26ba25
 
26ba25
-- 
26ba25
1.8.3.1
26ba25