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

357786
From e799889203d2f06adac2c1636b15be360e50f5a2 Mon Sep 17 00:00:00 2001
357786
From: Kevin Wolf <kwolf@redhat.com>
357786
Date: Tue, 26 Jun 2018 09:48:06 +0200
357786
Subject: [PATCH 37/89] job: Move cancelled to Job
357786
357786
RH-Author: Kevin Wolf <kwolf@redhat.com>
357786
Message-id: <20180626094856.6924-24-kwolf@redhat.com>
357786
Patchwork-id: 81103
357786
O-Subject: [RHV-7.6 qemu-kvm-rhev PATCH v2 23/73] job: Move cancelled to Job
357786
Bugzilla: 1513543
357786
RH-Acked-by: Jeffrey Cody <jcody@redhat.com>
357786
RH-Acked-by: Max Reitz <mreitz@redhat.com>
357786
RH-Acked-by: Fam Zheng <famz@redhat.com>
357786
357786
We cannot yet move the whole logic around job cancelling to Job because
357786
it depends on quite a few other things that are still only in BlockJob,
357786
but we can move the cancelled field at least.
357786
357786
Signed-off-by: Kevin Wolf <kwolf@redhat.com>
357786
Reviewed-by: Max Reitz <mreitz@redhat.com>
357786
Reviewed-by: John Snow <jsnow@redhat.com>
357786
(cherry picked from commit daa7f2f9467bc5624f04f28d4b01b88f08c6589c)
357786
Signed-off-by: Kevin Wolf <kwolf@redhat.com>
357786
Signed-off-by: Miroslav Rezanina <mrezanin@redhat.com>
357786
---
357786
 block/backup.c               |  6 +++---
357786
 block/commit.c               |  4 ++--
357786
 block/mirror.c               | 20 ++++++++++----------
357786
 block/stream.c               |  4 ++--
357786
 blockjob.c                   | 28 +++++++++++++---------------
357786
 include/block/blockjob.h     |  8 --------
357786
 include/block/blockjob_int.h |  8 --------
357786
 include/qemu/job.h           | 11 +++++++++++
357786
 job.c                        |  5 +++++
357786
 tests/test-blockjob-txn.c    |  6 +++---
357786
 tests/test-blockjob.c        |  2 +-
357786
 11 files changed, 50 insertions(+), 52 deletions(-)
357786
357786
diff --git a/block/backup.c b/block/backup.c
357786
index cfdb89d..ef0aa0e 100644
357786
--- a/block/backup.c
357786
+++ b/block/backup.c
357786
@@ -329,7 +329,7 @@ static bool coroutine_fn yield_and_check(BackupBlockJob *job)
357786
 {
357786
     uint64_t delay_ns;
357786
 
357786
-    if (block_job_is_cancelled(&job->common)) {
357786
+    if (job_is_cancelled(&job->common.job)) {
357786
         return true;
357786
     }
357786
 
357786
@@ -339,7 +339,7 @@ static bool coroutine_fn yield_and_check(BackupBlockJob *job)
357786
     job->bytes_read = 0;
357786
     block_job_sleep_ns(&job->common, delay_ns);
357786
 
357786
-    if (block_job_is_cancelled(&job->common)) {
357786
+    if (job_is_cancelled(&job->common.job)) {
357786
         return true;
357786
     }
357786
 
357786
@@ -441,7 +441,7 @@ static void coroutine_fn backup_run(void *opaque)
357786
     if (job->sync_mode == MIRROR_SYNC_MODE_NONE) {
357786
         /* All bits are set in copy_bitmap to allow any cluster to be copied.
357786
          * This does not actually require them to be copied. */
357786
-        while (!block_job_is_cancelled(&job->common)) {
357786
+        while (!job_is_cancelled(&job->common.job)) {
357786
             /* Yield until the job is cancelled.  We just let our before_write
357786
              * notify callback service CoW requests. */
357786
             block_job_yield(&job->common);
357786
diff --git a/block/commit.c b/block/commit.c
357786
index 925c96a..85baea8 100644
357786
--- a/block/commit.c
357786
+++ b/block/commit.c
357786
@@ -90,7 +90,7 @@ static void commit_complete(BlockJob *job, void *opaque)
357786
      * the normal backing chain can be restored. */
357786
     blk_unref(s->base);
357786
 
357786
-    if (!block_job_is_cancelled(&s->common) && ret == 0) {
357786
+    if (!job_is_cancelled(&s->common.job) && ret == 0) {
357786
         /* success */
357786
         ret = bdrv_drop_intermediate(s->commit_top_bs, base,
357786
                                      s->backing_file_str);
357786
@@ -172,7 +172,7 @@ static void coroutine_fn commit_run(void *opaque)
357786
          * with no pending I/O here so that bdrv_drain_all() returns.
357786
          */
357786
         block_job_sleep_ns(&s->common, delay_ns);
357786
-        if (block_job_is_cancelled(&s->common)) {
357786
+        if (job_is_cancelled(&s->common.job)) {
357786
             break;
357786
         }
357786
         /* Copy if allocated above the base */
357786
diff --git a/block/mirror.c b/block/mirror.c
357786
index 0df4f70..424072e 100644
357786
--- a/block/mirror.c
357786
+++ b/block/mirror.c
357786
@@ -622,7 +622,7 @@ static int coroutine_fn mirror_dirty_init(MirrorBlockJob *s)
357786
 
357786
             mirror_throttle(s);
357786
 
357786
-            if (block_job_is_cancelled(&s->common)) {
357786
+            if (job_is_cancelled(&s->common.job)) {
357786
                 s->initial_zeroing_ongoing = false;
357786
                 return 0;
357786
             }
357786
@@ -650,7 +650,7 @@ static int coroutine_fn mirror_dirty_init(MirrorBlockJob *s)
357786
 
357786
         mirror_throttle(s);
357786
 
357786
-        if (block_job_is_cancelled(&s->common)) {
357786
+        if (job_is_cancelled(&s->common.job)) {
357786
             return 0;
357786
         }
357786
 
357786
@@ -695,7 +695,7 @@ static void coroutine_fn mirror_run(void *opaque)
357786
                                  checking for a NULL string */
357786
     int ret = 0;
357786
 
357786
-    if (block_job_is_cancelled(&s->common)) {
357786
+    if (job_is_cancelled(&s->common.job)) {
357786
         goto immediate_exit;
357786
     }
357786
 
357786
@@ -729,10 +729,10 @@ static void coroutine_fn mirror_run(void *opaque)
357786
         /* Report BLOCK_JOB_READY and wait for complete. */
357786
         block_job_event_ready(&s->common);
357786
         s->synced = true;
357786
-        while (!block_job_is_cancelled(&s->common) && !s->should_complete) {
357786
+        while (!job_is_cancelled(&s->common.job) && !s->should_complete) {
357786
             block_job_yield(&s->common);
357786
         }
357786
-        s->common.cancelled = false;
357786
+        s->common.job.cancelled = false;
357786
         goto immediate_exit;
357786
     }
357786
 
357786
@@ -768,7 +768,7 @@ static void coroutine_fn mirror_run(void *opaque)
357786
     s->last_pause_ns = qemu_clock_get_ns(QEMU_CLOCK_REALTIME);
357786
     if (!s->is_none_mode) {
357786
         ret = mirror_dirty_init(s);
357786
-        if (ret < 0 || block_job_is_cancelled(&s->common)) {
357786
+        if (ret < 0 || job_is_cancelled(&s->common.job)) {
357786
             goto immediate_exit;
357786
         }
357786
     }
357786
@@ -828,7 +828,7 @@ static void coroutine_fn mirror_run(void *opaque)
357786
             }
357786
 
357786
             should_complete = s->should_complete ||
357786
-                block_job_is_cancelled(&s->common);
357786
+                job_is_cancelled(&s->common.job);
357786
             cnt = bdrv_get_dirty_count(s->dirty_bitmap);
357786
         }
357786
 
357786
@@ -856,7 +856,7 @@ static void coroutine_fn mirror_run(void *opaque)
357786
              * completion.
357786
              */
357786
             assert(QLIST_EMPTY(&bs->tracked_requests));
357786
-            s->common.cancelled = false;
357786
+            s->common.job.cancelled = false;
357786
             need_drain = false;
357786
             break;
357786
         }
357786
@@ -869,7 +869,7 @@ static void coroutine_fn mirror_run(void *opaque)
357786
         }
357786
         trace_mirror_before_sleep(s, cnt, s->synced, delay_ns);
357786
         block_job_sleep_ns(&s->common, delay_ns);
357786
-        if (block_job_is_cancelled(&s->common) &&
357786
+        if (job_is_cancelled(&s->common.job) &&
357786
             (!s->synced || s->common.force))
357786
         {
357786
             break;
357786
@@ -884,7 +884,7 @@ immediate_exit:
357786
          * the target is a copy of the source.
357786
          */
357786
         assert(ret < 0 || ((s->common.force || !s->synced) &&
357786
-               block_job_is_cancelled(&s->common)));
357786
+               job_is_cancelled(&s->common.job)));
357786
         assert(need_drain);
357786
         mirror_wait_for_all_io(s);
357786
     }
357786
diff --git a/block/stream.c b/block/stream.c
357786
index 7273d22..22c71ae 100644
357786
--- a/block/stream.c
357786
+++ b/block/stream.c
357786
@@ -66,7 +66,7 @@ static void stream_complete(BlockJob *job, void *opaque)
357786
     BlockDriverState *base = s->base;
357786
     Error *local_err = NULL;
357786
 
357786
-    if (!block_job_is_cancelled(&s->common) && bs->backing &&
357786
+    if (!job_is_cancelled(&s->common.job) && bs->backing &&
357786
         data->ret == 0) {
357786
         const char *base_id = NULL, *base_fmt = NULL;
357786
         if (base) {
357786
@@ -141,7 +141,7 @@ static void coroutine_fn stream_run(void *opaque)
357786
          * with no pending I/O here so that bdrv_drain_all() returns.
357786
          */
357786
         block_job_sleep_ns(&s->common, delay_ns);
357786
-        if (block_job_is_cancelled(&s->common)) {
357786
+        if (job_is_cancelled(&s->common.job)) {
357786
             break;
357786
         }
357786
 
357786
diff --git a/blockjob.c b/blockjob.c
357786
index 0bf0a26..f4f9956 100644
357786
--- a/blockjob.c
357786
+++ b/blockjob.c
357786
@@ -379,7 +379,7 @@ static void block_job_conclude(BlockJob *job)
357786
 
357786
 static void block_job_update_rc(BlockJob *job)
357786
 {
357786
-    if (!job->ret && block_job_is_cancelled(job)) {
357786
+    if (!job->ret && job_is_cancelled(&job->job)) {
357786
         job->ret = -ECANCELED;
357786
     }
357786
     if (job->ret) {
357786
@@ -438,7 +438,7 @@ static int block_job_finalize_single(BlockJob *job)
357786
 
357786
     /* Emit events only if we actually started */
357786
     if (block_job_started(job)) {
357786
-        if (block_job_is_cancelled(job)) {
357786
+        if (job_is_cancelled(&job->job)) {
357786
             block_job_event_cancelled(job);
357786
         } else {
357786
             const char *msg = NULL;
357786
@@ -464,7 +464,7 @@ static void block_job_cancel_async(BlockJob *job, bool force)
357786
         job->user_paused = false;
357786
         job->pause_count--;
357786
     }
357786
-    job->cancelled = true;
357786
+    job->job.cancelled = true;
357786
     /* To prevent 'force == false' overriding a previous 'force == true' */
357786
     job->force |= force;
357786
 }
357786
@@ -519,7 +519,8 @@ static int block_job_finish_sync(BlockJob *job,
357786
     while (!job->completed) {
357786
         aio_poll(qemu_get_aio_context(), true);
357786
     }
357786
-    ret = (job->cancelled && job->ret == 0) ? -ECANCELED : job->ret;
357786
+    ret = (job_is_cancelled(&job->job) && job->ret == 0)
357786
+          ? -ECANCELED : job->ret;
357786
     job_unref(&job->job);
357786
     return ret;
357786
 }
357786
@@ -557,7 +558,7 @@ static void block_job_completed_txn_abort(BlockJob *job)
357786
         other_job = QLIST_FIRST(&txn->jobs);
357786
         ctx = blk_get_aio_context(other_job->blk);
357786
         if (!other_job->completed) {
357786
-            assert(other_job->cancelled);
357786
+            assert(job_is_cancelled(&other_job->job));
357786
             block_job_finish_sync(other_job, NULL, NULL);
357786
         }
357786
         block_job_finalize_single(other_job);
357786
@@ -651,7 +652,9 @@ void block_job_complete(BlockJob *job, Error **errp)
357786
     if (job_apply_verb(&job->job, JOB_VERB_COMPLETE, errp)) {
357786
         return;
357786
     }
357786
-    if (job->pause_count || job->cancelled || !job->driver->complete) {
357786
+    if (job->pause_count || job_is_cancelled(&job->job) ||
357786
+        !job->driver->complete)
357786
+    {
357786
         error_setg(errp, "The active block job '%s' cannot be completed",
357786
                    job->job.id);
357786
         return;
357786
@@ -1006,7 +1009,7 @@ void coroutine_fn block_job_pause_point(BlockJob *job)
357786
     if (!block_job_should_pause(job)) {
357786
         return;
357786
     }
357786
-    if (block_job_is_cancelled(job)) {
357786
+    if (job_is_cancelled(&job->job)) {
357786
         return;
357786
     }
357786
 
357786
@@ -1014,7 +1017,7 @@ void coroutine_fn block_job_pause_point(BlockJob *job)
357786
         job->driver->pause(job);
357786
     }
357786
 
357786
-    if (block_job_should_pause(job) && !block_job_is_cancelled(job)) {
357786
+    if (block_job_should_pause(job) && !job_is_cancelled(&job->job)) {
357786
         JobStatus status = job->job.status;
357786
         job_state_transition(&job->job, status == JOB_STATUS_READY
357786
                                         ? JOB_STATUS_STANDBY
357786
@@ -1066,17 +1069,12 @@ void block_job_enter(BlockJob *job)
357786
     block_job_enter_cond(job, NULL);
357786
 }
357786
 
357786
-bool block_job_is_cancelled(BlockJob *job)
357786
-{
357786
-    return job->cancelled;
357786
-}
357786
-
357786
 void block_job_sleep_ns(BlockJob *job, int64_t ns)
357786
 {
357786
     assert(job->busy);
357786
 
357786
     /* Check cancellation *before* setting busy = false, too!  */
357786
-    if (block_job_is_cancelled(job)) {
357786
+    if (job_is_cancelled(&job->job)) {
357786
         return;
357786
     }
357786
 
357786
@@ -1092,7 +1090,7 @@ void block_job_yield(BlockJob *job)
357786
     assert(job->busy);
357786
 
357786
     /* Check cancellation *before* setting busy = false, too!  */
357786
-    if (block_job_is_cancelled(job)) {
357786
+    if (job_is_cancelled(&job->job)) {
357786
         return;
357786
     }
357786
 
357786
diff --git a/include/block/blockjob.h b/include/block/blockjob.h
357786
index 087e782..1e708f4 100644
357786
--- a/include/block/blockjob.h
357786
+++ b/include/block/blockjob.h
357786
@@ -57,14 +57,6 @@ typedef struct BlockJob {
357786
     Coroutine *co;
357786
 
357786
     /**
357786
-     * Set to true if the job should cancel itself.  The flag must
357786
-     * always be tested just before toggling the busy flag from false
357786
-     * to true.  After a job has been cancelled, it should only yield
357786
-     * if #aio_poll will ("sooner or later") reenter the coroutine.
357786
-     */
357786
-    bool cancelled;
357786
-
357786
-    /**
357786
      * Set to true if the job should abort immediately without waiting
357786
      * for data to be in sync.
357786
      */
357786
diff --git a/include/block/blockjob_int.h b/include/block/blockjob_int.h
357786
index 6f0fe3c..d64f30e 100644
357786
--- a/include/block/blockjob_int.h
357786
+++ b/include/block/blockjob_int.h
357786
@@ -196,14 +196,6 @@ void block_job_early_fail(BlockJob *job);
357786
 void block_job_completed(BlockJob *job, int ret);
357786
 
357786
 /**
357786
- * block_job_is_cancelled:
357786
- * @job: The job being queried.
357786
- *
357786
- * Returns whether the job is scheduled for cancellation.
357786
- */
357786
-bool block_job_is_cancelled(BlockJob *job);
357786
-
357786
-/**
357786
  * block_job_pause_point:
357786
  * @job: The job that is ready to pause.
357786
  *
357786
diff --git a/include/qemu/job.h b/include/qemu/job.h
357786
index 0751e2a..5dfbec5 100644
357786
--- a/include/qemu/job.h
357786
+++ b/include/qemu/job.h
357786
@@ -47,6 +47,14 @@ typedef struct Job {
357786
     /** Current state; See @JobStatus for details. */
357786
     JobStatus status;
357786
 
357786
+    /**
357786
+     * Set to true if the job should cancel itself.  The flag must
357786
+     * always be tested just before toggling the busy flag from false
357786
+     * to true.  After a job has been cancelled, it should only yield
357786
+     * if #aio_poll will ("sooner or later") reenter the coroutine.
357786
+     */
357786
+    bool cancelled;
357786
+
357786
     /** Element of the list of jobs */
357786
     QLIST_ENTRY(Job) job_list;
357786
 } Job;
357786
@@ -93,6 +101,9 @@ JobType job_type(const Job *job);
357786
 /** Returns the enum string for the JobType of a given Job. */
357786
 const char *job_type_str(const Job *job);
357786
 
357786
+/** Returns whether the job is scheduled for cancellation. */
357786
+bool job_is_cancelled(Job *job);
357786
+
357786
 /**
357786
  * Get the next element from the list of block jobs after @job, or the
357786
  * first one if @job is %NULL.
357786
diff --git a/job.c b/job.c
357786
index 926f1de..1abca6a 100644
357786
--- a/job.c
357786
+++ b/job.c
357786
@@ -95,6 +95,11 @@ const char *job_type_str(const Job *job)
357786
     return JobType_str(job_type(job));
357786
 }
357786
 
357786
+bool job_is_cancelled(Job *job)
357786
+{
357786
+    return job->cancelled;
357786
+}
357786
+
357786
 Job *job_next(Job *job)
357786
 {
357786
     if (!job) {
357786
diff --git a/tests/test-blockjob-txn.c b/tests/test-blockjob-txn.c
357786
index b49b28c..26b4bbb 100644
357786
--- a/tests/test-blockjob-txn.c
357786
+++ b/tests/test-blockjob-txn.c
357786
@@ -29,7 +29,7 @@ static void test_block_job_complete(BlockJob *job, void *opaque)
357786
     BlockDriverState *bs = blk_bs(job->blk);
357786
     int rc = (intptr_t)opaque;
357786
 
357786
-    if (block_job_is_cancelled(job)) {
357786
+    if (job_is_cancelled(&job->job)) {
357786
         rc = -ECANCELED;
357786
     }
357786
 
357786
@@ -49,7 +49,7 @@ static void coroutine_fn test_block_job_run(void *opaque)
357786
             block_job_yield(job);
357786
         }
357786
 
357786
-        if (block_job_is_cancelled(job)) {
357786
+        if (job_is_cancelled(&job->job)) {
357786
             break;
357786
         }
357786
     }
357786
@@ -66,7 +66,7 @@ typedef struct {
357786
 static void test_block_job_cb(void *opaque, int ret)
357786
 {
357786
     TestBlockJobCBData *data = opaque;
357786
-    if (!ret && block_job_is_cancelled(&data->job->common)) {
357786
+    if (!ret && job_is_cancelled(&data->job->common.job)) {
357786
         ret = -ECANCELED;
357786
     }
357786
     *data->result = ret;
357786
diff --git a/tests/test-blockjob.c b/tests/test-blockjob.c
357786
index e24fc3f..fa31481 100644
357786
--- a/tests/test-blockjob.c
357786
+++ b/tests/test-blockjob.c
357786
@@ -179,7 +179,7 @@ static void coroutine_fn cancel_job_start(void *opaque)
357786
     CancelJob *s = opaque;
357786
 
357786
     while (!s->should_complete) {
357786
-        if (block_job_is_cancelled(&s->common)) {
357786
+        if (job_is_cancelled(&s->common.job)) {
357786
             goto defer;
357786
         }
357786
 
357786
-- 
357786
1.8.3.1
357786