26ba25
From 7586908f543ae6e8981ed4bc17b60adec3b7456d Mon Sep 17 00:00:00 2001
26ba25
From: Kevin Wolf <kwolf@redhat.com>
26ba25
Date: Tue, 26 Jun 2018 09:48:21 +0200
26ba25
Subject: [PATCH 113/268] job: Switch transactions to JobTxn
26ba25
26ba25
RH-Author: Kevin Wolf <kwolf@redhat.com>
26ba25
Message-id: <20180626094856.6924-39-kwolf@redhat.com>
26ba25
Patchwork-id: 81086
26ba25
O-Subject: [RHV-7.6 qemu-kvm-rhev PATCH v2 38/73] job: Switch transactions to JobTxn
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 doesn't actually move any transaction code to Job yet, but it
26ba25
renames the type for transactions from BlockJobTxn to JobTxn and makes
26ba25
them contain Jobs rather than BlockJobs
26ba25
26ba25
Signed-off-by: Kevin Wolf <kwolf@redhat.com>
26ba25
Reviewed-by: Max Reitz <mreitz@redhat.com>
26ba25
(cherry picked from commit 62c9e4162a7bc26a1389e50d17d3b2637028bbc3)
26ba25
Signed-off-by: Kevin Wolf <kwolf@redhat.com>
26ba25
Signed-off-by: Miroslav Rezanina <mrezanin@redhat.com>
26ba25
---
26ba25
 block/backup.c               |  2 +-
26ba25
 blockdev.c                   | 14 +++++------
26ba25
 blockjob.c                   | 60 +++++++++++++++++++++++---------------------
26ba25
 include/block/block_int.h    |  2 +-
26ba25
 include/block/blockjob.h     | 11 ++++----
26ba25
 include/block/blockjob_int.h |  2 +-
26ba25
 include/qemu/job.h           |  3 +++
26ba25
 tests/test-blockjob-txn.c    |  8 +++---
26ba25
 8 files changed, 54 insertions(+), 48 deletions(-)
26ba25
26ba25
diff --git a/block/backup.c b/block/backup.c
26ba25
index ca7d990..6172f90 100644
26ba25
--- a/block/backup.c
26ba25
+++ b/block/backup.c
26ba25
@@ -547,7 +547,7 @@ BlockJob *backup_job_create(const char *job_id, BlockDriverState *bs,
26ba25
                   BlockdevOnError on_target_error,
26ba25
                   int creation_flags,
26ba25
                   BlockCompletionFunc *cb, void *opaque,
26ba25
-                  BlockJobTxn *txn, Error **errp)
26ba25
+                  JobTxn *txn, Error **errp)
26ba25
 {
26ba25
     int64_t len;
26ba25
     BlockDriverInfo bdi;
26ba25
diff --git a/blockdev.c b/blockdev.c
26ba25
index dd9a080..6efdb30 100644
26ba25
--- a/blockdev.c
26ba25
+++ b/blockdev.c
26ba25
@@ -1447,7 +1447,7 @@ typedef struct BlkActionOps {
26ba25
 struct BlkActionState {
26ba25
     TransactionAction *action;
26ba25
     const BlkActionOps *ops;
26ba25
-    BlockJobTxn *block_job_txn;
26ba25
+    JobTxn *block_job_txn;
26ba25
     TransactionProperties *txn_props;
26ba25
     QSIMPLEQ_ENTRY(BlkActionState) entry;
26ba25
 };
26ba25
@@ -1865,7 +1865,7 @@ typedef struct DriveBackupState {
26ba25
     BlockJob *job;
26ba25
 } DriveBackupState;
26ba25
 
26ba25
-static BlockJob *do_drive_backup(DriveBackup *backup, BlockJobTxn *txn,
26ba25
+static BlockJob *do_drive_backup(DriveBackup *backup, JobTxn *txn,
26ba25
                             Error **errp);
26ba25
 
26ba25
 static void drive_backup_prepare(BlkActionState *common, Error **errp)
26ba25
@@ -1955,7 +1955,7 @@ typedef struct BlockdevBackupState {
26ba25
     BlockJob *job;
26ba25
 } BlockdevBackupState;
26ba25
 
26ba25
-static BlockJob *do_blockdev_backup(BlockdevBackup *backup, BlockJobTxn *txn,
26ba25
+static BlockJob *do_blockdev_backup(BlockdevBackup *backup, JobTxn *txn,
26ba25
                                     Error **errp);
26ba25
 
26ba25
 static void blockdev_backup_prepare(BlkActionState *common, Error **errp)
26ba25
@@ -2244,7 +2244,7 @@ void qmp_transaction(TransactionActionList *dev_list,
26ba25
                      Error **errp)
26ba25
 {
26ba25
     TransactionActionList *dev_entry = dev_list;
26ba25
-    BlockJobTxn *block_job_txn = NULL;
26ba25
+    JobTxn *block_job_txn = NULL;
26ba25
     BlkActionState *state, *next;
26ba25
     Error *local_err = NULL;
26ba25
 
26ba25
@@ -2252,7 +2252,7 @@ void qmp_transaction(TransactionActionList *dev_list,
26ba25
     QSIMPLEQ_INIT(&snap_bdrv_states);
26ba25
 
26ba25
     /* Does this transaction get canceled as a group on failure?
26ba25
-     * If not, we don't really need to make a BlockJobTxn.
26ba25
+     * If not, we don't really need to make a JobTxn.
26ba25
      */
26ba25
     props = get_transaction_properties(props);
26ba25
     if (props->completion_mode != ACTION_COMPLETION_MODE_INDIVIDUAL) {
26ba25
@@ -3265,7 +3265,7 @@ out:
26ba25
     aio_context_release(aio_context);
26ba25
 }
26ba25
 
26ba25
-static BlockJob *do_drive_backup(DriveBackup *backup, BlockJobTxn *txn,
26ba25
+static BlockJob *do_drive_backup(DriveBackup *backup, JobTxn *txn,
26ba25
                                  Error **errp)
26ba25
 {
26ba25
     BlockDriverState *bs;
26ba25
@@ -3435,7 +3435,7 @@ BlockDeviceInfoList *qmp_query_named_block_nodes(Error **errp)
26ba25
     return bdrv_named_nodes_list(errp);
26ba25
 }
26ba25
 
26ba25
-BlockJob *do_blockdev_backup(BlockdevBackup *backup, BlockJobTxn *txn,
26ba25
+BlockJob *do_blockdev_backup(BlockdevBackup *backup, JobTxn *txn,
26ba25
                              Error **errp)
26ba25
 {
26ba25
     BlockDriverState *bs;
26ba25
diff --git a/blockjob.c b/blockjob.c
26ba25
index 1ed3e9c..bd35c4f 100644
26ba25
--- a/blockjob.c
26ba25
+++ b/blockjob.c
26ba25
@@ -37,13 +37,13 @@
26ba25
 #include "qemu/timer.h"
26ba25
 
26ba25
 /* Transactional group of block jobs */
26ba25
-struct BlockJobTxn {
26ba25
+struct JobTxn {
26ba25
 
26ba25
     /* Is this txn being cancelled? */
26ba25
     bool aborting;
26ba25
 
26ba25
     /* List of jobs */
26ba25
-    QLIST_HEAD(, BlockJob) jobs;
26ba25
+    QLIST_HEAD(, Job) jobs;
26ba25
 
26ba25
     /* Reference count */
26ba25
     int refcnt;
26ba25
@@ -94,27 +94,27 @@ BlockJob *block_job_get(const char *id)
26ba25
     }
26ba25
 }
26ba25
 
26ba25
-BlockJobTxn *block_job_txn_new(void)
26ba25
+JobTxn *block_job_txn_new(void)
26ba25
 {
26ba25
-    BlockJobTxn *txn = g_new0(BlockJobTxn, 1);
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(BlockJobTxn *txn)
26ba25
+static void block_job_txn_ref(JobTxn *txn)
26ba25
 {
26ba25
     txn->refcnt++;
26ba25
 }
26ba25
 
26ba25
-void block_job_txn_unref(BlockJobTxn *txn)
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(BlockJobTxn *txn, BlockJob *job)
26ba25
+void block_job_txn_add_job(JobTxn *txn, BlockJob *job)
26ba25
 {
26ba25
     if (!txn) {
26ba25
         return;
26ba25
@@ -123,14 +123,14 @@ void block_job_txn_add_job(BlockJobTxn *txn, BlockJob *job)
26ba25
     assert(!job->txn);
26ba25
     job->txn = txn;
26ba25
 
26ba25
-    QLIST_INSERT_HEAD(&txn->jobs, job, txn_list);
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, txn_list);
26ba25
+        QLIST_REMOVE(&job->job, txn_list);
26ba25
         block_job_txn_unref(job->txn);
26ba25
         job->txn = NULL;
26ba25
     }
26ba25
@@ -285,18 +285,22 @@ static void job_cancel_async(Job *job, bool force)
26ba25
     job->force_cancel |= force;
26ba25
 }
26ba25
 
26ba25
-static int block_job_txn_apply(BlockJobTxn *txn, int fn(BlockJob *), bool lock)
26ba25
+static int block_job_txn_apply(JobTxn *txn, int fn(BlockJob *), bool lock)
26ba25
 {
26ba25
     AioContext *ctx;
26ba25
-    BlockJob *job, *next;
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 = blk_get_aio_context(job->blk);
26ba25
+            ctx = job->aio_context;
26ba25
             aio_context_acquire(ctx);
26ba25
         }
26ba25
-        rc = fn(job);
26ba25
+        rc = fn(bjob);
26ba25
         if (lock) {
26ba25
             aio_context_release(ctx);
26ba25
         }
26ba25
@@ -310,8 +314,8 @@ static int block_job_txn_apply(BlockJobTxn *txn, int fn(BlockJob *), bool lock)
26ba25
 static void block_job_completed_txn_abort(BlockJob *job)
26ba25
 {
26ba25
     AioContext *ctx;
26ba25
-    BlockJobTxn *txn = job->txn;
26ba25
-    BlockJob *other_job;
26ba25
+    JobTxn *txn = job->txn;
26ba25
+    Job *other_job;
26ba25
 
26ba25
     if (txn->aborting) {
26ba25
         /*
26ba25
@@ -324,7 +328,7 @@ static void block_job_completed_txn_abort(BlockJob *job)
26ba25
 
26ba25
     /* We are the first failed job. Cancel other jobs. */
26ba25
     QLIST_FOREACH(other_job, &txn->jobs, txn_list) {
26ba25
-        ctx = blk_get_aio_context(other_job->blk);
26ba25
+        ctx = other_job->aio_context;
26ba25
         aio_context_acquire(ctx);
26ba25
     }
26ba25
 
26ba25
@@ -332,18 +336,18 @@ static void block_job_completed_txn_abort(BlockJob *job)
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->job, false);
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 = blk_get_aio_context(other_job->blk);
26ba25
-        if (!job_is_completed(&other_job->job)) {
26ba25
-            assert(job_is_cancelled(&other_job->job));
26ba25
-            job_finish_sync(&other_job->job, NULL, NULL);
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->job);
26ba25
+        job_finalize_single(other_job);
26ba25
         aio_context_release(ctx);
26ba25
     }
26ba25
 
26ba25
@@ -385,8 +389,8 @@ static int block_job_transition_to_pending(BlockJob *job)
26ba25
 
26ba25
 static void block_job_completed_txn_success(BlockJob *job)
26ba25
 {
26ba25
-    BlockJobTxn *txn = job->txn;
26ba25
-    BlockJob *other_job;
26ba25
+    JobTxn *txn = job->txn;
26ba25
+    Job *other_job;
26ba25
 
26ba25
     job_state_transition(&job->job, JOB_STATUS_WAITING);
26ba25
 
26ba25
@@ -395,10 +399,10 @@ static void block_job_completed_txn_success(BlockJob *job)
26ba25
      * txn.
26ba25
      */
26ba25
     QLIST_FOREACH(other_job, &txn->jobs, txn_list) {
26ba25
-        if (!job_is_completed(&other_job->job)) {
26ba25
+        if (!job_is_completed(other_job)) {
26ba25
             return;
26ba25
         }
26ba25
-        assert(other_job->job.ret == 0);
26ba25
+        assert(other_job->ret == 0);
26ba25
     }
26ba25
 
26ba25
     block_job_txn_apply(txn, block_job_transition_to_pending, false);
26ba25
@@ -628,7 +632,7 @@ static void block_job_event_pending(Notifier *n, void *opaque)
26ba25
  */
26ba25
 
26ba25
 void *block_job_create(const char *job_id, const BlockJobDriver *driver,
26ba25
-                       BlockJobTxn *txn, BlockDriverState *bs, uint64_t perm,
26ba25
+                       JobTxn *txn, BlockDriverState *bs, uint64_t perm,
26ba25
                        uint64_t shared_perm, int64_t speed, int flags,
26ba25
                        BlockCompletionFunc *cb, void *opaque, Error **errp)
26ba25
 {
26ba25
diff --git a/include/block/block_int.h b/include/block/block_int.h
26ba25
index d913ed1..ad2b852 100644
26ba25
--- a/include/block/block_int.h
26ba25
+++ b/include/block/block_int.h
26ba25
@@ -1018,7 +1018,7 @@ BlockJob *backup_job_create(const char *job_id, BlockDriverState *bs,
26ba25
                             BlockdevOnError on_target_error,
26ba25
                             int creation_flags,
26ba25
                             BlockCompletionFunc *cb, void *opaque,
26ba25
-                            BlockJobTxn *txn, Error **errp);
26ba25
+                            JobTxn *txn, Error **errp);
26ba25
 
26ba25
 void hmp_drive_add_node(Monitor *mon, const char *optstr);
26ba25
 
26ba25
diff --git a/include/block/blockjob.h b/include/block/blockjob.h
26ba25
index 85ce18a..44df025 100644
26ba25
--- a/include/block/blockjob.h
26ba25
+++ b/include/block/blockjob.h
26ba25
@@ -33,7 +33,7 @@
26ba25
 #define BLOCK_JOB_SLICE_TIME 100000000ULL /* ns */
26ba25
 
26ba25
 typedef struct BlockJobDriver BlockJobDriver;
26ba25
-typedef struct BlockJobTxn BlockJobTxn;
26ba25
+typedef struct JobTxn JobTxn;
26ba25
 
26ba25
 /**
26ba25
  * BlockJob:
26ba25
@@ -85,8 +85,7 @@ typedef struct BlockJob {
26ba25
     /** BlockDriverStates that are involved in this block job */
26ba25
     GSList *nodes;
26ba25
 
26ba25
-    BlockJobTxn *txn;
26ba25
-    QLIST_ENTRY(BlockJob) txn_list;
26ba25
+    JobTxn *txn;
26ba25
 } BlockJob;
26ba25
 
26ba25
 /**
26ba25
@@ -273,7 +272,7 @@ void block_job_iostatus_reset(BlockJob *job);
26ba25
  * group.  Jobs wait for each other before completing.  Cancelling one job
26ba25
  * cancels all jobs in the transaction.
26ba25
  */
26ba25
-BlockJobTxn *block_job_txn_new(void);
26ba25
+JobTxn *block_job_txn_new(void);
26ba25
 
26ba25
 /**
26ba25
  * block_job_txn_unref:
26ba25
@@ -282,7 +281,7 @@ BlockJobTxn *block_job_txn_new(void);
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(BlockJobTxn *txn);
26ba25
+void block_job_txn_unref(JobTxn *txn);
26ba25
 
26ba25
 /**
26ba25
  * block_job_txn_add_job:
26ba25
@@ -293,7 +292,7 @@ void block_job_txn_unref(BlockJobTxn *txn);
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(BlockJobTxn *txn, BlockJob *job);
26ba25
+void block_job_txn_add_job(JobTxn *txn, BlockJob *job);
26ba25
 
26ba25
 /**
26ba25
  * block_job_is_internal:
26ba25
diff --git a/include/block/blockjob_int.h b/include/block/blockjob_int.h
26ba25
index b8ca7bb..ce66a9b 100644
26ba25
--- a/include/block/blockjob_int.h
26ba25
+++ b/include/block/blockjob_int.h
26ba25
@@ -91,7 +91,7 @@ struct BlockJobDriver {
26ba25
  * called from a wrapper that is specific to the job type.
26ba25
  */
26ba25
 void *block_job_create(const char *job_id, const BlockJobDriver *driver,
26ba25
-                       BlockJobTxn *txn, BlockDriverState *bs, uint64_t perm,
26ba25
+                       JobTxn *txn, BlockDriverState *bs, uint64_t perm,
26ba25
                        uint64_t shared_perm, int64_t speed, int flags,
26ba25
                        BlockCompletionFunc *cb, void *opaque, Error **errp);
26ba25
 
26ba25
diff --git a/include/qemu/job.h b/include/qemu/job.h
26ba25
index 17e2cec..d4aa7fa 100644
26ba25
--- a/include/qemu/job.h
26ba25
+++ b/include/qemu/job.h
26ba25
@@ -132,6 +132,9 @@ typedef struct Job {
26ba25
 
26ba25
     /** Element of the list of jobs */
26ba25
     QLIST_ENTRY(Job) job_list;
26ba25
+
26ba25
+    /** Element of the list of jobs in a job transaction */
26ba25
+    QLIST_ENTRY(Job) txn_list;
26ba25
 } Job;
26ba25
 
26ba25
 /**
26ba25
diff --git a/tests/test-blockjob-txn.c b/tests/test-blockjob-txn.c
26ba25
index 1572f8d..ec5d592 100644
26ba25
--- a/tests/test-blockjob-txn.c
26ba25
+++ b/tests/test-blockjob-txn.c
26ba25
@@ -93,7 +93,7 @@ static const BlockJobDriver test_block_job_driver = {
26ba25
  */
26ba25
 static BlockJob *test_block_job_start(unsigned int iterations,
26ba25
                                       bool use_timer,
26ba25
-                                      int rc, int *result, BlockJobTxn *txn)
26ba25
+                                      int rc, int *result, JobTxn *txn)
26ba25
 {
26ba25
     BlockDriverState *bs;
26ba25
     TestBlockJob *s;
26ba25
@@ -122,7 +122,7 @@ static BlockJob *test_block_job_start(unsigned int iterations,
26ba25
 static void test_single_job(int expected)
26ba25
 {
26ba25
     BlockJob *job;
26ba25
-    BlockJobTxn *txn;
26ba25
+    JobTxn *txn;
26ba25
     int result = -EINPROGRESS;
26ba25
 
26ba25
     txn = block_job_txn_new();
26ba25
@@ -160,7 +160,7 @@ static void test_pair_jobs(int expected1, int expected2)
26ba25
 {
26ba25
     BlockJob *job1;
26ba25
     BlockJob *job2;
26ba25
-    BlockJobTxn *txn;
26ba25
+    JobTxn *txn;
26ba25
     int result1 = -EINPROGRESS;
26ba25
     int result2 = -EINPROGRESS;
26ba25
 
26ba25
@@ -222,7 +222,7 @@ static void test_pair_jobs_fail_cancel_race(void)
26ba25
 {
26ba25
     BlockJob *job1;
26ba25
     BlockJob *job2;
26ba25
-    BlockJobTxn *txn;
26ba25
+    JobTxn *txn;
26ba25
     int result1 = -EINPROGRESS;
26ba25
     int result2 = -EINPROGRESS;
26ba25
 
26ba25
-- 
26ba25
1.8.3.1
26ba25