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

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