thebeanogamer / rpms / qemu-kvm

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