thebeanogamer / rpms / qemu-kvm

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