ae23c9
From 949b0b3562c3c98ce64dc15825f30e2c82260c01 Mon Sep 17 00:00:00 2001
ae23c9
From: Kevin Wolf <kwolf@redhat.com>
ae23c9
Date: Tue, 26 Jun 2018 09:48:17 +0200
ae23c9
Subject: [PATCH 109/268] job: Convert block_job_cancel_async() to Job
ae23c9
ae23c9
RH-Author: Kevin Wolf <kwolf@redhat.com>
ae23c9
Message-id: <20180626094856.6924-35-kwolf@redhat.com>
ae23c9
Patchwork-id: 81072
ae23c9
O-Subject: [RHV-7.6 qemu-kvm-rhev PATCH v2 34/73] job: Convert block_job_cancel_async() 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
block_job_cancel_async() did two things that were still block job
ae23c9
specific:
ae23c9
ae23c9
* Setting job->force. This field makes sense on the Job level, so we can
ae23c9
  just move it. While at it, rename it to job->force_cancel to make its
ae23c9
  purpose more obvious.
ae23c9
ae23c9
* Resetting the I/O status. This can't be moved because generic Jobs
ae23c9
  don't have an I/O status. What the function really implements is a
ae23c9
  user resume, except without entering the coroutine. Consequently, it
ae23c9
  makes sense to call the .user_resume driver callback here which
ae23c9
  already resets the I/O status.
ae23c9
ae23c9
  The old block_job_cancel_async() has two separate if statements that
ae23c9
  check job->iostatus != BLOCK_DEVICE_IO_STATUS_OK and job->user_paused.
ae23c9
  However, the former condition always implies the latter (as is
ae23c9
  asserted in block_job_iostatus_reset()), so changing the explicit call
ae23c9
  of block_job_iostatus_reset() on the former condition with the
ae23c9
  .user_resume callback on the latter condition is equivalent and
ae23c9
  doesn't need to access any BlockJob specific state.
ae23c9
ae23c9
Signed-off-by: Kevin Wolf <kwolf@redhat.com>
ae23c9
Reviewed-by: Max Reitz <mreitz@redhat.com>
ae23c9
(cherry picked from commit 004e95df98266da33e08c9f1731aca71b6d6d7c4)
ae23c9
Signed-off-by: Kevin Wolf <kwolf@redhat.com>
ae23c9
Signed-off-by: Miroslav Rezanina <mrezanin@redhat.com>
ae23c9
---
ae23c9
 block/mirror.c           |  4 ++--
ae23c9
 blockjob.c               | 25 +++++++++++++------------
ae23c9
 include/block/blockjob.h |  6 ------
ae23c9
 include/qemu/job.h       |  6 ++++++
ae23c9
 4 files changed, 21 insertions(+), 20 deletions(-)
ae23c9
ae23c9
diff --git a/block/mirror.c b/block/mirror.c
ae23c9
index e9a90ea..c3951d1 100644
ae23c9
--- a/block/mirror.c
ae23c9
+++ b/block/mirror.c
ae23c9
@@ -871,7 +871,7 @@ static void coroutine_fn mirror_run(void *opaque)
ae23c9
         trace_mirror_before_sleep(s, cnt, s->synced, delay_ns);
ae23c9
         job_sleep_ns(&s->common.job, delay_ns);
ae23c9
         if (job_is_cancelled(&s->common.job) &&
ae23c9
-            (!s->synced || s->common.force))
ae23c9
+            (!s->synced || s->common.job.force_cancel))
ae23c9
         {
ae23c9
             break;
ae23c9
         }
ae23c9
@@ -884,7 +884,7 @@ immediate_exit:
ae23c9
          * or it was cancelled prematurely so that we do not guarantee that
ae23c9
          * the target is a copy of the source.
ae23c9
          */
ae23c9
-        assert(ret < 0 || ((s->common.force || !s->synced) &&
ae23c9
+        assert(ret < 0 || ((s->common.job.force_cancel || !s->synced) &&
ae23c9
                job_is_cancelled(&s->common.job)));
ae23c9
         assert(need_drain);
ae23c9
         mirror_wait_for_all_io(s);
ae23c9
diff --git a/blockjob.c b/blockjob.c
ae23c9
index 34c57da..4cac367 100644
ae23c9
--- a/blockjob.c
ae23c9
+++ b/blockjob.c
ae23c9
@@ -270,19 +270,20 @@ static int block_job_prepare(BlockJob *job)
ae23c9
     return job->job.ret;
ae23c9
 }
ae23c9
 
ae23c9
-static void block_job_cancel_async(BlockJob *job, bool force)
ae23c9
+static void job_cancel_async(Job *job, bool force)
ae23c9
 {
ae23c9
-    if (job->iostatus != BLOCK_DEVICE_IO_STATUS_OK) {
ae23c9
-        block_job_iostatus_reset(job);
ae23c9
-    }
ae23c9
-    if (job->job.user_paused) {
ae23c9
-        /* Do not call block_job_enter here, the caller will handle it.  */
ae23c9
-        job->job.user_paused = false;
ae23c9
-        job->job.pause_count--;
ae23c9
+    if (job->user_paused) {
ae23c9
+        /* Do not call job_enter here, the caller will handle it.  */
ae23c9
+        job->user_paused = false;
ae23c9
+        if (job->driver->user_resume) {
ae23c9
+            job->driver->user_resume(job);
ae23c9
+        }
ae23c9
+        assert(job->pause_count > 0);
ae23c9
+        job->pause_count--;
ae23c9
     }
ae23c9
-    job->job.cancelled = true;
ae23c9
+    job->cancelled = true;
ae23c9
     /* To prevent 'force == false' overriding a previous 'force == true' */
ae23c9
-    job->force |= force;
ae23c9
+    job->force_cancel |= force;
ae23c9
 }
ae23c9
 
ae23c9
 static int block_job_txn_apply(BlockJobTxn *txn, int fn(BlockJob *), bool lock)
ae23c9
@@ -367,7 +368,7 @@ static void block_job_completed_txn_abort(BlockJob *job)
ae23c9
      * on the caller, so leave it. */
ae23c9
     QLIST_FOREACH(other_job, &txn->jobs, txn_list) {
ae23c9
         if (other_job != job) {
ae23c9
-            block_job_cancel_async(other_job, false);
ae23c9
+            job_cancel_async(&other_job->job, false);
ae23c9
         }
ae23c9
     }
ae23c9
     while (!QLIST_EMPTY(&txn->jobs)) {
ae23c9
@@ -527,7 +528,7 @@ void block_job_cancel(BlockJob *job, bool force)
ae23c9
         job_do_dismiss(&job->job);
ae23c9
         return;
ae23c9
     }
ae23c9
-    block_job_cancel_async(job, force);
ae23c9
+    job_cancel_async(&job->job, force);
ae23c9
     if (!job_started(&job->job)) {
ae23c9
         block_job_completed(job, -ECANCELED);
ae23c9
     } else if (job->job.deferred_to_main_loop) {
ae23c9
diff --git a/include/block/blockjob.h b/include/block/blockjob.h
ae23c9
index 3f405d1..d975efe 100644
ae23c9
--- a/include/block/blockjob.h
ae23c9
+++ b/include/block/blockjob.h
ae23c9
@@ -51,12 +51,6 @@ typedef struct BlockJob {
ae23c9
     BlockBackend *blk;
ae23c9
 
ae23c9
     /**
ae23c9
-     * Set to true if the job should abort immediately without waiting
ae23c9
-     * for data to be in sync.
ae23c9
-     */
ae23c9
-    bool force;
ae23c9
-
ae23c9
-    /**
ae23c9
      * Set to true when the job is ready to be completed.
ae23c9
      */
ae23c9
     bool ready;
ae23c9
diff --git a/include/qemu/job.h b/include/qemu/job.h
ae23c9
index 3e817be..2648c74 100644
ae23c9
--- a/include/qemu/job.h
ae23c9
+++ b/include/qemu/job.h
ae23c9
@@ -97,6 +97,12 @@ typedef struct Job {
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
+    bool force_cancel;
ae23c9
+
ae23c9
     /** Set to true when the job has deferred work to the main loop. */
ae23c9
     bool deferred_to_main_loop;
ae23c9
 
ae23c9
-- 
ae23c9
1.8.3.1
ae23c9