Blame SOURCES/kvm-job-Move-transactions-to-Job.patch

357786
From 870c205da77467ee664de7f74ba9c47b6b8e528b Mon Sep 17 00:00:00 2001
357786
From: Kevin Wolf <kwolf@redhat.com>
357786
Date: Tue, 26 Jun 2018 09:48:22 +0200
357786
Subject: [PATCH 53/89] job: Move transactions to Job
357786
357786
RH-Author: Kevin Wolf <kwolf@redhat.com>
357786
Message-id: <20180626094856.6924-40-kwolf@redhat.com>
357786
Patchwork-id: 81097
357786
O-Subject: [RHV-7.6 qemu-kvm-rhev PATCH v2 39/73] job: Move transactions 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 logic that implements job transactions from BlockJob to
357786
Job.
357786
357786
Signed-off-by: Kevin Wolf <kwolf@redhat.com>
357786
Reviewed-by: Max Reitz <mreitz@redhat.com>
357786
(cherry picked from commit 7eaa8fb57da96301f4a8ce176ad503f80efc7cc0)
357786
Signed-off-by: Kevin Wolf <kwolf@redhat.com>
357786
Signed-off-by: Miroslav Rezanina <mrezanin@redhat.com>
357786
---
357786
 blockdev.c                   |   6 +-
357786
 blockjob.c                   | 238 +------------------------------------------
357786
 include/block/blockjob.h     |  54 ----------
357786
 include/block/blockjob_int.h |  10 --
357786
 include/qemu/job.h           |  71 +++++++++++--
357786
 job.c                        | 234 ++++++++++++++++++++++++++++++++++++++++--
357786
 tests/test-blockjob-txn.c    |  12 +--
357786
 tests/test-blockjob.c        |   2 +-
357786
 8 files changed, 303 insertions(+), 324 deletions(-)
357786
357786
diff --git a/blockdev.c b/blockdev.c
357786
index b6f3e92..422b5ac 100644
357786
--- a/blockdev.c
357786
+++ b/blockdev.c
357786
@@ -2297,7 +2297,7 @@ void qmp_transaction(TransactionActionList *dev_list,
357786
      */
357786
     props = get_transaction_properties(props);
357786
     if (props->completion_mode != ACTION_COMPLETION_MODE_INDIVIDUAL) {
357786
-        block_job_txn = block_job_txn_new();
357786
+        block_job_txn = job_txn_new();
357786
     }
357786
 
357786
     /* drain all i/o before any operations */
357786
@@ -2356,7 +2356,7 @@ exit:
357786
     if (!has_props) {
357786
         qapi_free_TransactionProperties(props);
357786
     }
357786
-    block_job_txn_unref(block_job_txn);
357786
+    job_txn_unref(block_job_txn);
357786
 }
357786
 
357786
 void qmp_eject(bool has_device, const char *device,
357786
@@ -3955,7 +3955,7 @@ void qmp_block_job_finalize(const char *id, Error **errp)
357786
     }
357786
 
357786
     trace_qmp_block_job_finalize(job);
357786
-    block_job_finalize(job, errp);
357786
+    job_finalize(&job->job, errp);
357786
     aio_context_release(aio_context);
357786
 }
357786
 
357786
diff --git a/blockjob.c b/blockjob.c
357786
index bd35c4f..14b21c8 100644
357786
--- a/blockjob.c
357786
+++ b/blockjob.c
357786
@@ -36,19 +36,6 @@
357786
 #include "qemu/coroutine.h"
357786
 #include "qemu/timer.h"
357786
 
357786
-/* Transactional group of block jobs */
357786
-struct JobTxn {
357786
-
357786
-    /* Is this txn being cancelled? */
357786
-    bool aborting;
357786
-
357786
-    /* List of jobs */
357786
-    QLIST_HEAD(, Job) jobs;
357786
-
357786
-    /* Reference count */
357786
-    int refcnt;
357786
-};
357786
-
357786
 /*
357786
  * The block job API is composed of two categories of functions.
357786
  *
357786
@@ -94,48 +81,6 @@ BlockJob *block_job_get(const char *id)
357786
     }
357786
 }
357786
 
357786
-JobTxn *block_job_txn_new(void)
357786
-{
357786
-    JobTxn *txn = g_new0(JobTxn, 1);
357786
-    QLIST_INIT(&txn->jobs);
357786
-    txn->refcnt = 1;
357786
-    return txn;
357786
-}
357786
-
357786
-static void block_job_txn_ref(JobTxn *txn)
357786
-{
357786
-    txn->refcnt++;
357786
-}
357786
-
357786
-void block_job_txn_unref(JobTxn *txn)
357786
-{
357786
-    if (txn && --txn->refcnt == 0) {
357786
-        g_free(txn);
357786
-    }
357786
-}
357786
-
357786
-void block_job_txn_add_job(JobTxn *txn, BlockJob *job)
357786
-{
357786
-    if (!txn) {
357786
-        return;
357786
-    }
357786
-
357786
-    assert(!job->txn);
357786
-    job->txn = txn;
357786
-
357786
-    QLIST_INSERT_HEAD(&txn->jobs, &job->job, txn_list);
357786
-    block_job_txn_ref(txn);
357786
-}
357786
-
357786
-void block_job_txn_del_job(BlockJob *job)
357786
-{
357786
-    if (job->txn) {
357786
-        QLIST_REMOVE(&job->job, txn_list);
357786
-        block_job_txn_unref(job->txn);
357786
-        job->txn = NULL;
357786
-    }
357786
-}
357786
-
357786
 static void block_job_attached_aio_context(AioContext *new_context,
357786
                                            void *opaque);
357786
 static void block_job_detach_aio_context(void *opaque);
357786
@@ -145,8 +90,6 @@ void block_job_free(Job *job)
357786
     BlockJob *bjob = container_of(job, BlockJob, job);
357786
     BlockDriverState *bs = blk_bs(bjob->blk);
357786
 
357786
-    assert(!bjob->txn);
357786
-
357786
     bs->job = NULL;
357786
     block_job_remove_all_bdrv(bjob);
357786
     blk_remove_aio_context_notifier(bjob->blk,
357786
@@ -261,158 +204,6 @@ const BlockJobDriver *block_job_driver(BlockJob *job)
357786
     return job->driver;
357786
 }
357786
 
357786
-static int block_job_prepare(BlockJob *job)
357786
-{
357786
-    if (job->job.ret == 0 && job->driver->prepare) {
357786
-        job->job.ret = job->driver->prepare(job);
357786
-    }
357786
-    return job->job.ret;
357786
-}
357786
-
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
-        job->user_paused = false;
357786
-        if (job->driver->user_resume) {
357786
-            job->driver->user_resume(job);
357786
-        }
357786
-        assert(job->pause_count > 0);
357786
-        job->pause_count--;
357786
-    }
357786
-    job->cancelled = true;
357786
-    /* To prevent 'force == false' overriding a previous 'force == true' */
357786
-    job->force_cancel |= force;
357786
-}
357786
-
357786
-static int block_job_txn_apply(JobTxn *txn, int fn(BlockJob *), bool lock)
357786
-{
357786
-    AioContext *ctx;
357786
-    Job *job, *next;
357786
-    BlockJob *bjob;
357786
-    int rc = 0;
357786
-
357786
-    QLIST_FOREACH_SAFE(job, &txn->jobs, txn_list, next) {
357786
-        assert(is_block_job(job));
357786
-        bjob = container_of(job, BlockJob, job);
357786
-
357786
-        if (lock) {
357786
-            ctx = job->aio_context;
357786
-            aio_context_acquire(ctx);
357786
-        }
357786
-        rc = fn(bjob);
357786
-        if (lock) {
357786
-            aio_context_release(ctx);
357786
-        }
357786
-        if (rc) {
357786
-            break;
357786
-        }
357786
-    }
357786
-    return rc;
357786
-}
357786
-
357786
-static void block_job_completed_txn_abort(BlockJob *job)
357786
-{
357786
-    AioContext *ctx;
357786
-    JobTxn *txn = job->txn;
357786
-    Job *other_job;
357786
-
357786
-    if (txn->aborting) {
357786
-        /*
357786
-         * We are cancelled by another job, which will handle everything.
357786
-         */
357786
-        return;
357786
-    }
357786
-    txn->aborting = true;
357786
-    block_job_txn_ref(txn);
357786
-
357786
-    /* We are the first failed job. Cancel other jobs. */
357786
-    QLIST_FOREACH(other_job, &txn->jobs, txn_list) {
357786
-        ctx = other_job->aio_context;
357786
-        aio_context_acquire(ctx);
357786
-    }
357786
-
357786
-    /* Other jobs are effectively cancelled by us, set the status for
357786
-     * them; this job, however, may or may not be cancelled, depending
357786
-     * on the caller, so leave it. */
357786
-    QLIST_FOREACH(other_job, &txn->jobs, txn_list) {
357786
-        if (other_job != &job->job) {
357786
-            job_cancel_async(other_job, false);
357786
-        }
357786
-    }
357786
-    while (!QLIST_EMPTY(&txn->jobs)) {
357786
-        other_job = QLIST_FIRST(&txn->jobs);
357786
-        ctx = other_job->aio_context;
357786
-        if (!job_is_completed(other_job)) {
357786
-            assert(job_is_cancelled(other_job));
357786
-            job_finish_sync(other_job, NULL, NULL);
357786
-        }
357786
-        job_finalize_single(other_job);
357786
-        aio_context_release(ctx);
357786
-    }
357786
-
357786
-    block_job_txn_unref(txn);
357786
-}
357786
-
357786
-static int block_job_needs_finalize(BlockJob *job)
357786
-{
357786
-    return !job->job.auto_finalize;
357786
-}
357786
-
357786
-static int block_job_finalize_single(BlockJob *job)
357786
-{
357786
-    return job_finalize_single(&job->job);
357786
-}
357786
-
357786
-static void block_job_do_finalize(BlockJob *job)
357786
-{
357786
-    int rc;
357786
-    assert(job && job->txn);
357786
-
357786
-    /* prepare the transaction to complete */
357786
-    rc = block_job_txn_apply(job->txn, block_job_prepare, true);
357786
-    if (rc) {
357786
-        block_job_completed_txn_abort(job);
357786
-    } else {
357786
-        block_job_txn_apply(job->txn, block_job_finalize_single, true);
357786
-    }
357786
-}
357786
-
357786
-static int block_job_transition_to_pending(BlockJob *job)
357786
-{
357786
-    job_state_transition(&job->job, JOB_STATUS_PENDING);
357786
-    if (!job->job.auto_finalize) {
357786
-        job_event_pending(&job->job);
357786
-    }
357786
-    return 0;
357786
-}
357786
-
357786
-static void block_job_completed_txn_success(BlockJob *job)
357786
-{
357786
-    JobTxn *txn = job->txn;
357786
-    Job *other_job;
357786
-
357786
-    job_state_transition(&job->job, JOB_STATUS_WAITING);
357786
-
357786
-    /*
357786
-     * Successful completion, see if there are other running jobs in this
357786
-     * txn.
357786
-     */
357786
-    QLIST_FOREACH(other_job, &txn->jobs, txn_list) {
357786
-        if (!job_is_completed(other_job)) {
357786
-            return;
357786
-        }
357786
-        assert(other_job->ret == 0);
357786
-    }
357786
-
357786
-    block_job_txn_apply(txn, block_job_transition_to_pending, false);
357786
-
357786
-    /* If no jobs need manual finalization, automatically do so */
357786
-    if (block_job_txn_apply(txn, block_job_needs_finalize, false) == 0) {
357786
-        block_job_do_finalize(job);
357786
-    }
357786
-}
357786
-
357786
 /* Assumes the job_mutex is held */
357786
 static bool job_timer_pending(Job *job)
357786
 {
357786
@@ -451,15 +242,6 @@ int64_t block_job_ratelimit_get_delay(BlockJob *job, uint64_t n)
357786
     return ratelimit_calculate_delay(&job->limit, n);
357786
 }
357786
 
357786
-void block_job_finalize(BlockJob *job, Error **errp)
357786
-{
357786
-    assert(job && job->job.id);
357786
-    if (job_apply_verb(&job->job, JOB_VERB_FINALIZE, errp)) {
357786
-        return;
357786
-    }
357786
-    block_job_do_finalize(job);
357786
-}
357786
-
357786
 void block_job_dismiss(BlockJob **jobptr, Error **errp)
357786
 {
357786
     BlockJob *job = *jobptr;
357786
@@ -483,7 +265,7 @@ void block_job_cancel(BlockJob *job, bool force)
357786
     if (!job_started(&job->job)) {
357786
         block_job_completed(job, -ECANCELED);
357786
     } else if (job->job.deferred_to_main_loop) {
357786
-        block_job_completed_txn_abort(job);
357786
+        job_completed_txn_abort(&job->job);
357786
     } else {
357786
         block_job_enter(job);
357786
     }
357786
@@ -656,7 +438,7 @@ void *block_job_create(const char *job_id, const BlockJobDriver *driver,
357786
         return NULL;
357786
     }
357786
 
357786
-    job = job_create(job_id, &driver->job_driver, blk_get_aio_context(blk),
357786
+    job = job_create(job_id, &driver->job_driver, txn, blk_get_aio_context(blk),
357786
                      flags, cb, opaque, errp);
357786
     if (job == NULL) {
357786
         blk_unref(blk);
357786
@@ -703,30 +485,20 @@ void *block_job_create(const char *job_id, const BlockJobDriver *driver,
357786
         }
357786
     }
357786
 
357786
-    /* Single jobs are modeled as single-job transactions for sake of
357786
-     * consolidating the job management logic */
357786
-    if (!txn) {
357786
-        txn = block_job_txn_new();
357786
-        block_job_txn_add_job(txn, job);
357786
-        block_job_txn_unref(txn);
357786
-    } else {
357786
-        block_job_txn_add_job(txn, job);
357786
-    }
357786
-
357786
     return job;
357786
 }
357786
 
357786
 void block_job_completed(BlockJob *job, int ret)
357786
 {
357786
-    assert(job && job->txn && !job_is_completed(&job->job));
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
-        block_job_completed_txn_abort(job);
357786
+        job_completed_txn_abort(&job->job);
357786
     } else {
357786
-        block_job_completed_txn_success(job);
357786
+        job_completed_txn_success(&job->job);
357786
     }
357786
 }
357786
 
357786
diff --git a/include/block/blockjob.h b/include/block/blockjob.h
357786
index 44df025..09e6bb4 100644
357786
--- a/include/block/blockjob.h
357786
+++ b/include/block/blockjob.h
357786
@@ -33,7 +33,6 @@
357786
 #define BLOCK_JOB_SLICE_TIME 100000000ULL /* ns */
357786
 
357786
 typedef struct BlockJobDriver BlockJobDriver;
357786
-typedef struct JobTxn JobTxn;
357786
 
357786
 /**
357786
  * BlockJob:
357786
@@ -84,8 +83,6 @@ typedef struct BlockJob {
357786
 
357786
     /** BlockDriverStates that are involved in this block job */
357786
     GSList *nodes;
357786
-
357786
-    JobTxn *txn;
357786
 } BlockJob;
357786
 
357786
 /**
357786
@@ -153,22 +150,6 @@ void block_job_set_speed(BlockJob *job, int64_t speed, Error **errp);
357786
 void block_job_cancel(BlockJob *job, bool force);
357786
 
357786
 /**
357786
- * block_job_finalize:
357786
- * @job: The job to fully commit and finish.
357786
- * @errp: Error object.
357786
- *
357786
- * For jobs that have finished their work and are pending
357786
- * awaiting explicit acknowledgement to commit their work,
357786
- * This will commit that work.
357786
- *
357786
- * FIXME: Make the below statement universally true:
357786
- * For jobs that support the manual workflow mode, all graph
357786
- * changes that occur as a result will occur after this command
357786
- * and before a successful reply.
357786
- */
357786
-void block_job_finalize(BlockJob *job, Error **errp);
357786
-
357786
-/**
357786
  * block_job_dismiss:
357786
  * @job: The job to be dismissed.
357786
  * @errp: Error object.
357786
@@ -260,41 +241,6 @@ int block_job_complete_sync(BlockJob *job, Error **errp);
357786
 void block_job_iostatus_reset(BlockJob *job);
357786
 
357786
 /**
357786
- * block_job_txn_new:
357786
- *
357786
- * Allocate and return a new block job transaction.  Jobs can be added to the
357786
- * transaction using block_job_txn_add_job().
357786
- *
357786
- * The transaction is automatically freed when the last job completes or is
357786
- * cancelled.
357786
- *
357786
- * All jobs in the transaction either complete successfully or fail/cancel as a
357786
- * group.  Jobs wait for each other before completing.  Cancelling one job
357786
- * cancels all jobs in the transaction.
357786
- */
357786
-JobTxn *block_job_txn_new(void);
357786
-
357786
-/**
357786
- * block_job_txn_unref:
357786
- *
357786
- * Release a reference that was previously acquired with block_job_txn_add_job
357786
- * or block_job_txn_new. If it's the last reference to the object, it will be
357786
- * freed.
357786
- */
357786
-void block_job_txn_unref(JobTxn *txn);
357786
-
357786
-/**
357786
- * block_job_txn_add_job:
357786
- * @txn: The transaction (may be NULL)
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 block_job_txn_unref() or block_job_completed()
357786
- * to release the reference that is automatically grabbed here.
357786
- */
357786
-void block_job_txn_add_job(JobTxn *txn, BlockJob *job);
357786
-
357786
-/**
357786
  * block_job_is_internal:
357786
  * @job: The job to determine if it is user-visible or not.
357786
  *
357786
diff --git a/include/block/blockjob_int.h b/include/block/blockjob_int.h
357786
index ce66a9b..29a2802 100644
357786
--- a/include/block/blockjob_int.h
357786
+++ b/include/block/blockjob_int.h
357786
@@ -38,16 +38,6 @@ struct BlockJobDriver {
357786
     /** Generic JobDriver callbacks and settings */
357786
     JobDriver job_driver;
357786
 
357786
-    /**
357786
-     * If the callback is not NULL, prepare will be invoked when all the jobs
357786
-     * belonging to the same transaction complete; or upon this job's completion
357786
-     * if it is not in a transaction.
357786
-     *
357786
-     * This callback will not be invoked if the job has already failed.
357786
-     * If it fails, abort and then clean will be called.
357786
-     */
357786
-    int (*prepare)(BlockJob *job);
357786
-
357786
     /*
357786
      * If the callback is not NULL, it will be invoked before the job is
357786
      * resumed in a new AioContext.  This is the place to move any resources
357786
diff --git a/include/qemu/job.h b/include/qemu/job.h
357786
index d4aa7fa..39d74ab 100644
357786
--- a/include/qemu/job.h
357786
+++ b/include/qemu/job.h
357786
@@ -32,6 +32,8 @@
357786
 #include "block/aio.h"
357786
 
357786
 typedef struct JobDriver JobDriver;
357786
+typedef struct JobTxn JobTxn;
357786
+
357786
 
357786
 /**
357786
  * Long-running operation.
357786
@@ -133,6 +135,9 @@ typedef struct Job {
357786
     /** Element of the list of jobs */
357786
     QLIST_ENTRY(Job) job_list;
357786
 
357786
+    /** Transaction this job is part of */
357786
+    JobTxn *txn;
357786
+
357786
     /** Element of the list of jobs in a job transaction */
357786
     QLIST_ENTRY(Job) txn_list;
357786
 } Job;
357786
@@ -184,6 +189,16 @@ struct JobDriver {
357786
     void (*drain)(Job *job);
357786
 
357786
     /**
357786
+     * If the callback is not NULL, prepare will be invoked when all the jobs
357786
+     * belonging to the same transaction complete; or upon this job's completion
357786
+     * if it is not in a transaction.
357786
+     *
357786
+     * This callback will not be invoked if the job has already failed.
357786
+     * If it fails, abort and then clean will be called.
357786
+     */
357786
+    int (*prepare)(Job *job);
357786
+
357786
+    /**
357786
      * If the callback is not NULL, it will be invoked when all the jobs
357786
      * belonging to the same transaction complete; or upon this job's
357786
      * completion if it is not in a transaction. Skipped if NULL.
357786
@@ -227,20 +242,52 @@ typedef enum JobCreateFlags {
357786
     JOB_MANUAL_DISMISS = 0x04,
357786
 } JobCreateFlags;
357786
 
357786
+/**
357786
+ * Allocate and return a new job transaction. Jobs can be added to the
357786
+ * transaction using job_txn_add_job().
357786
+ *
357786
+ * The transaction is automatically freed when the last job completes or is
357786
+ * cancelled.
357786
+ *
357786
+ * All jobs in the transaction either complete successfully or fail/cancel as a
357786
+ * group.  Jobs wait for each other before completing.  Cancelling one job
357786
+ * cancels all jobs in the transaction.
357786
+ */
357786
+JobTxn *job_txn_new(void);
357786
+
357786
+/**
357786
+ * Release a reference that was previously acquired with job_txn_add_job or
357786
+ * job_txn_new. If it's the last reference to the object, it will be freed.
357786
+ */
357786
+void job_txn_unref(JobTxn *txn);
357786
+
357786
+/**
357786
+ * @txn: The transaction (may be NULL)
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
+ *
357786
+ * If @txn is NULL, the function does nothing.
357786
+ */
357786
+void job_txn_add_job(JobTxn *txn, Job *job);
357786
 
357786
 /**
357786
  * Create a new long-running job and return it.
357786
  *
357786
  * @job_id: The id of the newly-created job, or %NULL for internal jobs
357786
  * @driver: The class object for the newly-created job.
357786
+ * @txn: The transaction this job belongs to, if any. %NULL otherwise.
357786
  * @ctx: The AioContext to run the job coroutine in.
357786
  * @flags: Creation flags for the job. See @JobCreateFlags.
357786
  * @cb: Completion function for the job.
357786
  * @opaque: Opaque pointer value passed to @cb.
357786
  * @errp: Error object.
357786
  */
357786
-void *job_create(const char *job_id, const JobDriver *driver, AioContext *ctx,
357786
-                 int flags, BlockCompletionFunc *cb, void *opaque, Error **errp);
357786
+void *job_create(const char *job_id, const JobDriver *driver, JobTxn *txn,
357786
+                 AioContext *ctx, int flags, BlockCompletionFunc *cb,
357786
+                 void *opaque, Error **errp);
357786
 
357786
 /**
357786
  * Add a reference to Job refcnt, it will be decreased with job_unref, and then
357786
@@ -260,9 +307,6 @@ void job_event_cancelled(Job *job);
357786
 /** To be called when a successfully completed job is finalised. */
357786
 void job_event_completed(Job *job);
357786
 
357786
-/** To be called when the job transitions to PENDING */
357786
-void job_event_pending(Job *job);
357786
-
357786
 /**
357786
  * Conditionally enter the job coroutine if the job is ready to run, not
357786
  * already busy and fn() returns true. fn() is called while under the job_lock
357786
@@ -375,6 +419,16 @@ void job_early_fail(Job *job);
357786
 /** Asynchronously complete the specified @job. */
357786
 void job_complete(Job *job, Error **errp);;
357786
 
357786
+/**
357786
+ * For a @job that has finished its work and is pending awaiting explicit
357786
+ * acknowledgement to commit its work, this will commit that work.
357786
+ *
357786
+ * FIXME: Make the below statement universally true:
357786
+ * For jobs that support the manual workflow mode, all graph changes that occur
357786
+ * as a result will occur after this command and before a successful reply.
357786
+ */
357786
+void job_finalize(Job *job, Error **errp);
357786
+
357786
 typedef void JobDeferToMainLoopFn(Job *job, void *opaque);
357786
 
357786
 /**
357786
@@ -407,10 +461,9 @@ 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
-int job_finalize_single(Job *job);
357786
 void job_update_rc(Job *job);
357786
-
357786
-typedef struct BlockJob BlockJob;
357786
-void block_job_txn_del_job(BlockJob *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 aa74b4c..4f6fd73 100644
357786
--- a/job.c
357786
+++ b/job.c
357786
@@ -60,6 +60,19 @@ bool JobVerbTable[JOB_VERB__MAX][JOB_STATUS__MAX] = {
357786
     [JOB_VERB_DISMISS]              = {0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 0},
357786
 };
357786
 
357786
+/* Transactional group of jobs */
357786
+struct JobTxn {
357786
+
357786
+    /* Is this txn being cancelled? */
357786
+    bool aborting;
357786
+
357786
+    /* List of jobs */
357786
+    QLIST_HEAD(, Job) jobs;
357786
+
357786
+    /* Reference count */
357786
+    int refcnt;
357786
+};
357786
+
357786
 /* Right now, this mutex is only needed to synchronize accesses to job->busy
357786
  * and job->sleep_timer, such as concurrent calls to job_do_yield and
357786
  * job_enter. */
357786
@@ -80,6 +93,71 @@ static void __attribute__((__constructor__)) job_init(void)
357786
     qemu_mutex_init(&job_mutex);
357786
 }
357786
 
357786
+JobTxn *job_txn_new(void)
357786
+{
357786
+    JobTxn *txn = g_new0(JobTxn, 1);
357786
+    QLIST_INIT(&txn->jobs);
357786
+    txn->refcnt = 1;
357786
+    return txn;
357786
+}
357786
+
357786
+static void job_txn_ref(JobTxn *txn)
357786
+{
357786
+    txn->refcnt++;
357786
+}
357786
+
357786
+void job_txn_unref(JobTxn *txn)
357786
+{
357786
+    if (txn && --txn->refcnt == 0) {
357786
+        g_free(txn);
357786
+    }
357786
+}
357786
+
357786
+void job_txn_add_job(JobTxn *txn, Job *job)
357786
+{
357786
+    if (!txn) {
357786
+        return;
357786
+    }
357786
+
357786
+    assert(!job->txn);
357786
+    job->txn = txn;
357786
+
357786
+    QLIST_INSERT_HEAD(&txn->jobs, job, txn_list);
357786
+    job_txn_ref(txn);
357786
+}
357786
+
357786
+static void job_txn_del_job(Job *job)
357786
+{
357786
+    if (job->txn) {
357786
+        QLIST_REMOVE(job, txn_list);
357786
+        job_txn_unref(job->txn);
357786
+        job->txn = NULL;
357786
+    }
357786
+}
357786
+
357786
+static int job_txn_apply(JobTxn *txn, int fn(Job *), bool lock)
357786
+{
357786
+    AioContext *ctx;
357786
+    Job *job, *next;
357786
+    int rc = 0;
357786
+
357786
+    QLIST_FOREACH_SAFE(job, &txn->jobs, txn_list, next) {
357786
+        if (lock) {
357786
+            ctx = job->aio_context;
357786
+            aio_context_acquire(ctx);
357786
+        }
357786
+        rc = fn(job);
357786
+        if (lock) {
357786
+            aio_context_release(ctx);
357786
+        }
357786
+        if (rc) {
357786
+            break;
357786
+        }
357786
+    }
357786
+    return rc;
357786
+}
357786
+
357786
+
357786
 /* TODO Make static once the whole state machine is in job.c */
357786
 void job_state_transition(Job *job, JobStatus s1)
357786
 {
357786
@@ -181,8 +259,9 @@ static void job_sleep_timer_cb(void *opaque)
357786
     job_enter(job);
357786
 }
357786
 
357786
-void *job_create(const char *job_id, const JobDriver *driver, AioContext *ctx,
357786
-                 int flags, BlockCompletionFunc *cb, void *opaque, Error **errp)
357786
+void *job_create(const char *job_id, const JobDriver *driver, JobTxn *txn,
357786
+                 AioContext *ctx, int flags, BlockCompletionFunc *cb,
357786
+                 void *opaque, Error **errp)
357786
 {
357786
     Job *job;
357786
 
357786
@@ -228,6 +307,16 @@ void *job_create(const char *job_id, const JobDriver *driver, AioContext *ctx,
357786
 
357786
     QLIST_INSERT_HEAD(&jobs, job, job_list);
357786
 
357786
+    /* Single jobs are modeled as single-job transactions for sake of
357786
+     * consolidating the job management logic */
357786
+    if (!txn) {
357786
+        txn = job_txn_new();
357786
+        job_txn_add_job(txn, job);
357786
+        job_txn_unref(txn);
357786
+    } else {
357786
+        job_txn_add_job(txn, job);
357786
+    }
357786
+
357786
     return job;
357786
 }
357786
 
357786
@@ -241,6 +330,7 @@ void job_unref(Job *job)
357786
     if (--job->refcnt == 0) {
357786
         assert(job->status == JOB_STATUS_NULL);
357786
         assert(!timer_pending(&job->sleep_timer));
357786
+        assert(!job->txn);
357786
 
357786
         if (job->driver->free) {
357786
             job->driver->free(job);
357786
@@ -263,7 +353,7 @@ void job_event_completed(Job *job)
357786
     notifier_list_notify(&job->on_finalize_completed, job);
357786
 }
357786
 
357786
-void job_event_pending(Job *job)
357786
+static void job_event_pending(Job *job)
357786
 {
357786
     notifier_list_notify(&job->on_pending, job);
357786
 }
357786
@@ -469,8 +559,7 @@ void job_do_dismiss(Job *job)
357786
     job->paused = false;
357786
     job->deferred_to_main_loop = true;
357786
 
357786
-    /* TODO Don't assume it's a BlockJob */
357786
-    block_job_txn_del_job((BlockJob*) job);
357786
+    job_txn_del_job(job);
357786
 
357786
     job_state_transition(job, JOB_STATUS_NULL);
357786
     job_unref(job);
357786
@@ -523,7 +612,7 @@ static void job_clean(Job *job)
357786
     }
357786
 }
357786
 
357786
-int job_finalize_single(Job *job)
357786
+static int job_finalize_single(Job *job)
357786
 {
357786
     assert(job_is_completed(job));
357786
 
357786
@@ -550,12 +639,141 @@ int job_finalize_single(Job *job)
357786
         }
357786
     }
357786
 
357786
-    /* TODO Don't assume it's a BlockJob */
357786
-    block_job_txn_del_job((BlockJob*) job);
357786
+    job_txn_del_job(job);
357786
     job_conclude(job);
357786
     return 0;
357786
 }
357786
 
357786
+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
+        job->user_paused = false;
357786
+        if (job->driver->user_resume) {
357786
+            job->driver->user_resume(job);
357786
+        }
357786
+        assert(job->pause_count > 0);
357786
+        job->pause_count--;
357786
+    }
357786
+    job->cancelled = true;
357786
+    /* To prevent 'force == false' overriding a previous 'force == true' */
357786
+    job->force_cancel |= force;
357786
+}
357786
+
357786
+void job_completed_txn_abort(Job *job)
357786
+{
357786
+    AioContext *ctx;
357786
+    JobTxn *txn = job->txn;
357786
+    Job *other_job;
357786
+
357786
+    if (txn->aborting) {
357786
+        /*
357786
+         * We are cancelled by another job, which will handle everything.
357786
+         */
357786
+        return;
357786
+    }
357786
+    txn->aborting = true;
357786
+    job_txn_ref(txn);
357786
+
357786
+    /* We are the first failed job. Cancel other jobs. */
357786
+    QLIST_FOREACH(other_job, &txn->jobs, txn_list) {
357786
+        ctx = other_job->aio_context;
357786
+        aio_context_acquire(ctx);
357786
+    }
357786
+
357786
+    /* Other jobs are effectively cancelled by us, set the status for
357786
+     * them; this job, however, may or may not be cancelled, depending
357786
+     * on the caller, so leave it. */
357786
+    QLIST_FOREACH(other_job, &txn->jobs, txn_list) {
357786
+        if (other_job != job) {
357786
+            job_cancel_async(other_job, false);
357786
+        }
357786
+    }
357786
+    while (!QLIST_EMPTY(&txn->jobs)) {
357786
+        other_job = QLIST_FIRST(&txn->jobs);
357786
+        ctx = other_job->aio_context;
357786
+        if (!job_is_completed(other_job)) {
357786
+            assert(job_is_cancelled(other_job));
357786
+            job_finish_sync(other_job, NULL, NULL);
357786
+        }
357786
+        job_finalize_single(other_job);
357786
+        aio_context_release(ctx);
357786
+    }
357786
+
357786
+    job_txn_unref(txn);
357786
+}
357786
+
357786
+static int job_prepare(Job *job)
357786
+{
357786
+    if (job->ret == 0 && job->driver->prepare) {
357786
+        job->ret = job->driver->prepare(job);
357786
+    }
357786
+    return job->ret;
357786
+}
357786
+
357786
+static int job_needs_finalize(Job *job)
357786
+{
357786
+    return !job->auto_finalize;
357786
+}
357786
+
357786
+static void job_do_finalize(Job *job)
357786
+{
357786
+    int rc;
357786
+    assert(job && job->txn);
357786
+
357786
+    /* prepare the transaction to complete */
357786
+    rc = job_txn_apply(job->txn, job_prepare, true);
357786
+    if (rc) {
357786
+        job_completed_txn_abort(job);
357786
+    } else {
357786
+        job_txn_apply(job->txn, job_finalize_single, true);
357786
+    }
357786
+}
357786
+
357786
+void job_finalize(Job *job, Error **errp)
357786
+{
357786
+    assert(job && job->id);
357786
+    if (job_apply_verb(job, JOB_VERB_FINALIZE, errp)) {
357786
+        return;
357786
+    }
357786
+    job_do_finalize(job);
357786
+}
357786
+
357786
+static int job_transition_to_pending(Job *job)
357786
+{
357786
+    job_state_transition(job, JOB_STATUS_PENDING);
357786
+    if (!job->auto_finalize) {
357786
+        job_event_pending(job);
357786
+    }
357786
+    return 0;
357786
+}
357786
+
357786
+void job_completed_txn_success(Job *job)
357786
+{
357786
+    JobTxn *txn = job->txn;
357786
+    Job *other_job;
357786
+
357786
+    job_state_transition(job, JOB_STATUS_WAITING);
357786
+
357786
+    /*
357786
+     * Successful completion, see if there are other running jobs in this
357786
+     * txn.
357786
+     */
357786
+    QLIST_FOREACH(other_job, &txn->jobs, txn_list) {
357786
+        if (!job_is_completed(other_job)) {
357786
+            return;
357786
+        }
357786
+        assert(other_job->ret == 0);
357786
+    }
357786
+
357786
+    job_txn_apply(txn, job_transition_to_pending, false);
357786
+
357786
+    /* If no jobs need manual finalization, automatically do so */
357786
+    if (job_txn_apply(txn, job_needs_finalize, false) == 0) {
357786
+        job_do_finalize(job);
357786
+    }
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/tests/test-blockjob-txn.c b/tests/test-blockjob-txn.c
357786
index ec5d592..6ee31d5 100644
357786
--- a/tests/test-blockjob-txn.c
357786
+++ b/tests/test-blockjob-txn.c
357786
@@ -125,7 +125,7 @@ static void test_single_job(int expected)
357786
     JobTxn *txn;
357786
     int result = -EINPROGRESS;
357786
 
357786
-    txn = block_job_txn_new();
357786
+    txn = job_txn_new();
357786
     job = test_block_job_start(1, true, expected, &result, txn);
357786
     job_start(&job->job);
357786
 
357786
@@ -138,7 +138,7 @@ static void test_single_job(int expected)
357786
     }
357786
     g_assert_cmpint(result, ==, expected);
357786
 
357786
-    block_job_txn_unref(txn);
357786
+    job_txn_unref(txn);
357786
 }
357786
 
357786
 static void test_single_job_success(void)
357786
@@ -164,7 +164,7 @@ static void test_pair_jobs(int expected1, int expected2)
357786
     int result1 = -EINPROGRESS;
357786
     int result2 = -EINPROGRESS;
357786
 
357786
-    txn = block_job_txn_new();
357786
+    txn = job_txn_new();
357786
     job1 = test_block_job_start(1, true, expected1, &result1, txn);
357786
     job2 = test_block_job_start(2, true, expected2, &result2, txn);
357786
     job_start(&job1->job);
357786
@@ -173,7 +173,7 @@ static void test_pair_jobs(int expected1, int expected2)
357786
     /* Release our reference now to trigger as many nice
357786
      * use-after-free bugs as possible.
357786
      */
357786
-    block_job_txn_unref(txn);
357786
+    job_txn_unref(txn);
357786
 
357786
     if (expected1 == -ECANCELED) {
357786
         block_job_cancel(job1, false);
357786
@@ -226,7 +226,7 @@ static void test_pair_jobs_fail_cancel_race(void)
357786
     int result1 = -EINPROGRESS;
357786
     int result2 = -EINPROGRESS;
357786
 
357786
-    txn = block_job_txn_new();
357786
+    txn = job_txn_new();
357786
     job1 = test_block_job_start(1, true, -ECANCELED, &result1, txn);
357786
     job2 = test_block_job_start(2, false, 0, &result2, txn);
357786
     job_start(&job1->job);
357786
@@ -247,7 +247,7 @@ static void test_pair_jobs_fail_cancel_race(void)
357786
     g_assert_cmpint(result1, ==, -ECANCELED);
357786
     g_assert_cmpint(result2, ==, -ECANCELED);
357786
 
357786
-    block_job_txn_unref(txn);
357786
+    job_txn_unref(txn);
357786
 }
357786
 
357786
 int main(int argc, char **argv)
357786
diff --git a/tests/test-blockjob.c b/tests/test-blockjob.c
357786
index e44c608..1e052c2 100644
357786
--- a/tests/test-blockjob.c
357786
+++ b/tests/test-blockjob.c
357786
@@ -364,7 +364,7 @@ static void test_cancel_concluded(void)
357786
     }
357786
     assert(job->job.status == JOB_STATUS_PENDING);
357786
 
357786
-    block_job_finalize(job, &error_abort);
357786
+    job_finalize(&job->job, &error_abort);
357786
     assert(job->job.status == JOB_STATUS_CONCLUDED);
357786
 
357786
     cancel_common(s);
357786
-- 
357786
1.8.3.1
357786