Blame SOURCES/kvm-job-Move-completion-and-cancellation-to-Job.patch

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