Blame SOURCES/kvm-job-Switch-transactions-to-JobTxn.patch

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