yeahuh / rpms / qemu-kvm

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