26ba25
From 15f474087cf25ad960213ec63a7376c639fceb05 Mon Sep 17 00:00:00 2001
26ba25
From: Kevin Wolf <kwolf@redhat.com>
26ba25
Date: Tue, 26 Jun 2018 09:48:27 +0200
26ba25
Subject: [PATCH 119/268] job: Add job_is_ready()
26ba25
26ba25
RH-Author: Kevin Wolf <kwolf@redhat.com>
26ba25
Message-id: <20180626094856.6924-45-kwolf@redhat.com>
26ba25
Patchwork-id: 81128
26ba25
O-Subject: [RHV-7.6 qemu-kvm-rhev PATCH v2 44/73] job: Add job_is_ready()
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
Instead of having a 'bool ready' in BlockJob, add a function that
26ba25
derives its value from the job status.
26ba25
26ba25
At the same time, this fixes the behaviour to match what the QAPI
26ba25
documentation promises for query-block-job: 'true if the job may be
26ba25
completed'. When the ready flag was introduced in commit ef6dbf1e46e,
26ba25
the flag never had to be reset to match the description because after
26ba25
being ready, the jobs would immediately complete and disappear.
26ba25
26ba25
Job transactions and manual job finalisation were introduced only later.
26ba25
With these changes, jobs may stay around even after having completed
26ba25
(and they are not ready to be completed a second time), however their
26ba25
patches forgot to reset the ready flag.
26ba25
26ba25
Signed-off-by: Kevin Wolf <kwolf@redhat.com>
26ba25
Reviewed-by: Max Reitz <mreitz@redhat.com>
26ba25
(cherry picked from commit df956ae2014340bf7de0190edb1d09be55d9eadf)
26ba25
Signed-off-by: Kevin Wolf <kwolf@redhat.com>
26ba25
Signed-off-by: Miroslav Rezanina <mrezanin@redhat.com>
26ba25
---
26ba25
 blockjob.c               |  3 +--
26ba25
 include/block/blockjob.h |  5 -----
26ba25
 include/qemu/job.h       |  3 +++
26ba25
 job.c                    | 22 ++++++++++++++++++++++
26ba25
 qemu-img.c               |  2 +-
26ba25
 tests/test-blockjob.c    |  2 +-
26ba25
 6 files changed, 28 insertions(+), 9 deletions(-)
26ba25
26ba25
diff --git a/blockjob.c b/blockjob.c
26ba25
index 3ca009b..38f18e9 100644
26ba25
--- a/blockjob.c
26ba25
+++ b/blockjob.c
26ba25
@@ -269,7 +269,7 @@ BlockJobInfo *block_job_query(BlockJob *job, Error **errp)
26ba25
     info->offset    = job->offset;
26ba25
     info->speed     = job->speed;
26ba25
     info->io_status = job->iostatus;
26ba25
-    info->ready     = job->ready;
26ba25
+    info->ready     = job_is_ready(&job->job),
26ba25
     info->status    = job->job.status;
26ba25
     info->auto_finalize = job->job.auto_finalize;
26ba25
     info->auto_dismiss  = job->job.auto_dismiss;
26ba25
@@ -436,7 +436,6 @@ void block_job_user_resume(Job *job)
26ba25
 void block_job_event_ready(BlockJob *job)
26ba25
 {
26ba25
     job_state_transition(&job->job, JOB_STATUS_READY);
26ba25
-    job->ready = true;
26ba25
 
26ba25
     if (block_job_is_internal(job)) {
26ba25
         return;
26ba25
diff --git a/include/block/blockjob.h b/include/block/blockjob.h
26ba25
index 5a81afc..8e1e1ee 100644
26ba25
--- a/include/block/blockjob.h
26ba25
+++ b/include/block/blockjob.h
26ba25
@@ -49,11 +49,6 @@ typedef struct BlockJob {
26ba25
     /** The block device on which the job is operating.  */
26ba25
     BlockBackend *blk;
26ba25
 
26ba25
-    /**
26ba25
-     * Set to true when the job is ready to be completed.
26ba25
-     */
26ba25
-    bool ready;
26ba25
-
26ba25
     /** Status that is published by the query-block-jobs QMP API */
26ba25
     BlockDeviceIoStatus iostatus;
26ba25
 
26ba25
diff --git a/include/qemu/job.h b/include/qemu/job.h
26ba25
index 1e8050c..487f9d9 100644
26ba25
--- a/include/qemu/job.h
26ba25
+++ b/include/qemu/job.h
26ba25
@@ -367,6 +367,9 @@ bool job_is_cancelled(Job *job);
26ba25
 /** Returns whether the job is in a completed state. */
26ba25
 bool job_is_completed(Job *job);
26ba25
 
26ba25
+/** Returns whether the job is ready to be completed. */
26ba25
+bool job_is_ready(Job *job);
26ba25
+
26ba25
 /**
26ba25
  * Request @job to pause at the next pause point. Must be paired with
26ba25
  * job_resume(). If the job is supposed to be resumed by user action, call
26ba25
diff --git a/job.c b/job.c
26ba25
index 7cd3602..aa4c746 100644
26ba25
--- a/job.c
26ba25
+++ b/job.c
26ba25
@@ -199,6 +199,28 @@ bool job_is_cancelled(Job *job)
26ba25
     return job->cancelled;
26ba25
 }
26ba25
 
26ba25
+bool job_is_ready(Job *job)
26ba25
+{
26ba25
+    switch (job->status) {
26ba25
+    case JOB_STATUS_UNDEFINED:
26ba25
+    case JOB_STATUS_CREATED:
26ba25
+    case JOB_STATUS_RUNNING:
26ba25
+    case JOB_STATUS_PAUSED:
26ba25
+    case JOB_STATUS_WAITING:
26ba25
+    case JOB_STATUS_PENDING:
26ba25
+    case JOB_STATUS_ABORTING:
26ba25
+    case JOB_STATUS_CONCLUDED:
26ba25
+    case JOB_STATUS_NULL:
26ba25
+        return false;
26ba25
+    case JOB_STATUS_READY:
26ba25
+    case JOB_STATUS_STANDBY:
26ba25
+        return true;
26ba25
+    default:
26ba25
+        g_assert_not_reached();
26ba25
+    }
26ba25
+    return false;
26ba25
+}
26ba25
+
26ba25
 bool job_is_completed(Job *job)
26ba25
 {
26ba25
     switch (job->status) {
26ba25
diff --git a/qemu-img.c b/qemu-img.c
26ba25
index 734ea94..3c449a2 100644
26ba25
--- a/qemu-img.c
26ba25
+++ b/qemu-img.c
26ba25
@@ -878,7 +878,7 @@ static void run_block_job(BlockJob *job, Error **errp)
26ba25
         aio_poll(aio_context, true);
26ba25
         qemu_progress_print(job->len ?
26ba25
                             ((float)job->offset / job->len * 100.f) : 0.0f, 0);
26ba25
-    } while (!job->ready && !job_is_completed(&job->job));
26ba25
+    } while (!job_is_ready(&job->job) && !job_is_completed(&job->job));
26ba25
 
26ba25
     if (!job_is_completed(&job->job)) {
26ba25
         ret = job_complete_sync(&job->job, errp);
26ba25
diff --git a/tests/test-blockjob.c b/tests/test-blockjob.c
26ba25
index 7131cab..8180d03 100644
26ba25
--- a/tests/test-blockjob.c
26ba25
+++ b/tests/test-blockjob.c
26ba25
@@ -185,7 +185,7 @@ static void coroutine_fn cancel_job_start(void *opaque)
26ba25
             goto defer;
26ba25
         }
26ba25
 
26ba25
-        if (!s->common.ready && s->should_converge) {
26ba25
+        if (!job_is_ready(&s->common.job) && s->should_converge) {
26ba25
             block_job_event_ready(&s->common);
26ba25
         }
26ba25
 
26ba25
-- 
26ba25
1.8.3.1
26ba25