Blame SOURCES/kvm-job-Move-pause-resume-functions-to-Job.patch

357786
From 6e9a5d0ef7ada1b153e0808c0fe73eeab940cfbd Mon Sep 17 00:00:00 2001
357786
From: Kevin Wolf <kwolf@redhat.com>
357786
Date: Tue, 26 Jun 2018 09:48:11 +0200
357786
Subject: [PATCH 42/89] job: Move pause/resume functions to Job
357786
357786
RH-Author: Kevin Wolf <kwolf@redhat.com>
357786
Message-id: <20180626094856.6924-29-kwolf@redhat.com>
357786
Patchwork-id: 81062
357786
O-Subject: [RHV-7.6 qemu-kvm-rhev PATCH v2 28/73] job: Move pause/resume functions 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
While we already moved the state related to job pausing to Job, the
357786
functions to do were still BlockJob only. This commit moves them over to
357786
Job.
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 b15de82867975e0b4acf644b5ee36d84904b6612)
357786
Signed-off-by: Kevin Wolf <kwolf@redhat.com>
357786
Signed-off-by: Miroslav Rezanina <mrezanin@redhat.com>
357786
---
357786
 block/backup.c               |  1 +
357786
 block/commit.c               |  1 +
357786
 block/mirror.c               |  2 ++
357786
 block/stream.c               |  1 +
357786
 blockdev.c                   |  6 ++--
357786
 blockjob.c                   | 81 +++++++++-----------------------------------
357786
 include/block/blockjob.h     | 32 -----------------
357786
 include/block/blockjob_int.h |  7 ++++
357786
 include/qemu/job.h           | 37 ++++++++++++++++++++
357786
 job.c                        | 59 ++++++++++++++++++++++++++++++++
357786
 tests/test-bdrv-drain.c      |  1 +
357786
 tests/test-blockjob-txn.c    |  1 +
357786
 tests/test-blockjob.c        |  6 ++--
357786
 13 files changed, 133 insertions(+), 102 deletions(-)
357786
357786
diff --git a/block/backup.c b/block/backup.c
357786
index f3a4f7c..4d011d5 100644
357786
--- a/block/backup.c
357786
+++ b/block/backup.c
357786
@@ -528,6 +528,7 @@ static const BlockJobDriver backup_job_driver = {
357786
         .instance_size          = sizeof(BackupBlockJob),
357786
         .job_type               = JOB_TYPE_BACKUP,
357786
         .free                   = block_job_free,
357786
+        .user_resume            = block_job_user_resume,
357786
         .start                  = backup_run,
357786
     },
357786
     .commit                 = backup_commit,
357786
diff --git a/block/commit.c b/block/commit.c
357786
index 1c6cb6c..c4a98e5 100644
357786
--- a/block/commit.c
357786
+++ b/block/commit.c
357786
@@ -220,6 +220,7 @@ static const BlockJobDriver commit_job_driver = {
357786
         .instance_size = sizeof(CommitBlockJob),
357786
         .job_type      = JOB_TYPE_COMMIT,
357786
         .free          = block_job_free,
357786
+        .user_resume   = block_job_user_resume,
357786
         .start         = commit_run,
357786
     },
357786
 };
357786
diff --git a/block/mirror.c b/block/mirror.c
357786
index 5d8f75c..9a7226f 100644
357786
--- a/block/mirror.c
357786
+++ b/block/mirror.c
357786
@@ -991,6 +991,7 @@ static const BlockJobDriver mirror_job_driver = {
357786
         .instance_size          = sizeof(MirrorBlockJob),
357786
         .job_type               = JOB_TYPE_MIRROR,
357786
         .free                   = block_job_free,
357786
+        .user_resume            = block_job_user_resume,
357786
         .start                  = mirror_run,
357786
         .pause                  = mirror_pause,
357786
     },
357786
@@ -1004,6 +1005,7 @@ static const BlockJobDriver commit_active_job_driver = {
357786
         .instance_size          = sizeof(MirrorBlockJob),
357786
         .job_type               = JOB_TYPE_COMMIT,
357786
         .free                   = block_job_free,
357786
+        .user_resume            = block_job_user_resume,
357786
         .start                  = mirror_run,
357786
         .pause                  = mirror_pause,
357786
     },
357786
diff --git a/block/stream.c b/block/stream.c
357786
index 1faab02..e81b488 100644
357786
--- a/block/stream.c
357786
+++ b/block/stream.c
357786
@@ -214,6 +214,7 @@ static const BlockJobDriver stream_job_driver = {
357786
         .job_type      = JOB_TYPE_STREAM,
357786
         .free          = block_job_free,
357786
         .start         = stream_run,
357786
+        .user_resume   = block_job_user_resume,
357786
     },
357786
 };
357786
 
357786
diff --git a/blockdev.c b/blockdev.c
357786
index efb83c4..0fa1990 100644
357786
--- a/blockdev.c
357786
+++ b/blockdev.c
357786
@@ -3891,7 +3891,7 @@ void qmp_block_job_cancel(const char *device,
357786
         force = false;
357786
     }
357786
 
357786
-    if (block_job_user_paused(job) && !force) {
357786
+    if (job_user_paused(&job->job) && !force) {
357786
         error_setg(errp, "The block job for device '%s' is currently paused",
357786
                    device);
357786
         goto out;
357786
@@ -3913,7 +3913,7 @@ void qmp_block_job_pause(const char *device, Error **errp)
357786
     }
357786
 
357786
     trace_qmp_block_job_pause(job);
357786
-    block_job_user_pause(job, errp);
357786
+    job_user_pause(&job->job, errp);
357786
     aio_context_release(aio_context);
357786
 }
357786
 
357786
@@ -3927,7 +3927,7 @@ void qmp_block_job_resume(const char *device, Error **errp)
357786
     }
357786
 
357786
     trace_qmp_block_job_resume(job);
357786
-    block_job_user_resume(job, errp);
357786
+    job_user_resume(&job->job, errp);
357786
     aio_context_release(aio_context);
357786
 }
357786
 
357786
diff --git a/blockjob.c b/blockjob.c
357786
index 4dc360c..6334a54 100644
357786
--- a/blockjob.c
357786
+++ b/blockjob.c
357786
@@ -140,29 +140,6 @@ static void block_job_txn_del_job(BlockJob *job)
357786
     }
357786
 }
357786
 
357786
-/* Assumes the job_mutex is held */
357786
-static bool job_timer_not_pending(Job *job)
357786
-{
357786
-    return !timer_pending(&job->sleep_timer);
357786
-}
357786
-
357786
-static void block_job_pause(BlockJob *job)
357786
-{
357786
-    job->job.pause_count++;
357786
-}
357786
-
357786
-static void block_job_resume(BlockJob *job)
357786
-{
357786
-    assert(job->job.pause_count > 0);
357786
-    job->job.pause_count--;
357786
-    if (job->job.pause_count) {
357786
-        return;
357786
-    }
357786
-
357786
-    /* kick only if no timer is pending */
357786
-    job_enter_cond(&job->job, job_timer_not_pending);
357786
-}
357786
-
357786
 static void block_job_attached_aio_context(AioContext *new_context,
357786
                                            void *opaque);
357786
 static void block_job_detach_aio_context(void *opaque);
357786
@@ -193,7 +170,7 @@ static void block_job_attached_aio_context(AioContext *new_context,
357786
         job->driver->attached_aio_context(job, new_context);
357786
     }
357786
 
357786
-    block_job_resume(job);
357786
+    job_resume(&job->job);
357786
 }
357786
 
357786
 static void block_job_drain(BlockJob *job)
357786
@@ -214,7 +191,7 @@ static void block_job_detach_aio_context(void *opaque)
357786
     /* In case the job terminates during aio_poll()... */
357786
     job_ref(&job->job);
357786
 
357786
-    block_job_pause(job);
357786
+    job_pause(&job->job);
357786
 
357786
     while (!job->job.paused && !job->completed) {
357786
         block_job_drain(job);
357786
@@ -233,13 +210,13 @@ static char *child_job_get_parent_desc(BdrvChild *c)
357786
 static void child_job_drained_begin(BdrvChild *c)
357786
 {
357786
     BlockJob *job = c->opaque;
357786
-    block_job_pause(job);
357786
+    job_pause(&job->job);
357786
 }
357786
 
357786
 static void child_job_drained_end(BdrvChild *c)
357786
 {
357786
     BlockJob *job = c->opaque;
357786
-    block_job_resume(job);
357786
+    job_resume(&job->job);
357786
 }
357786
 
357786
 static const BdrvChildRole child_job = {
357786
@@ -396,9 +373,9 @@ static void block_job_cancel_async(BlockJob *job, bool force)
357786
     if (job->iostatus != BLOCK_DEVICE_IO_STATUS_OK) {
357786
         block_job_iostatus_reset(job);
357786
     }
357786
-    if (job->user_paused) {
357786
+    if (job->job.user_paused) {
357786
         /* Do not call block_job_enter here, the caller will handle it.  */
357786
-        job->user_paused = false;
357786
+        job->job.user_paused = false;
357786
         job->job.pause_count--;
357786
     }
357786
     job->job.cancelled = true;
357786
@@ -628,39 +605,6 @@ void block_job_dismiss(BlockJob **jobptr, Error **errp)
357786
     *jobptr = NULL;
357786
 }
357786
 
357786
-void block_job_user_pause(BlockJob *job, Error **errp)
357786
-{
357786
-    if (job_apply_verb(&job->job, JOB_VERB_PAUSE, errp)) {
357786
-        return;
357786
-    }
357786
-    if (job->user_paused) {
357786
-        error_setg(errp, "Job is already paused");
357786
-        return;
357786
-    }
357786
-    job->user_paused = true;
357786
-    block_job_pause(job);
357786
-}
357786
-
357786
-bool block_job_user_paused(BlockJob *job)
357786
-{
357786
-    return job->user_paused;
357786
-}
357786
-
357786
-void block_job_user_resume(BlockJob *job, Error **errp)
357786
-{
357786
-    assert(job);
357786
-    if (!job->user_paused || job->job.pause_count <= 0) {
357786
-        error_setg(errp, "Can't resume a job that was not paused");
357786
-        return;
357786
-    }
357786
-    if (job_apply_verb(&job->job, JOB_VERB_RESUME, errp)) {
357786
-        return;
357786
-    }
357786
-    block_job_iostatus_reset(job);
357786
-    job->user_paused = false;
357786
-    block_job_resume(job);
357786
-}
357786
-
357786
 void block_job_cancel(BlockJob *job, bool force)
357786
 {
357786
     if (job->job.status == JOB_STATUS_CONCLUDED) {
357786
@@ -851,6 +795,7 @@ void *block_job_create(const char *job_id, const BlockJobDriver *driver,
357786
 
357786
     assert(is_block_job(&job->job));
357786
     assert(job->job.driver->free == &block_job_free);
357786
+    assert(job->job.driver->user_resume == &block_job_user_resume);
357786
 
357786
     job->driver        = driver;
357786
     job->blk           = blk;
357786
@@ -941,10 +886,16 @@ void block_job_iostatus_reset(BlockJob *job)
357786
     if (job->iostatus == BLOCK_DEVICE_IO_STATUS_OK) {
357786
         return;
357786
     }
357786
-    assert(job->user_paused && job->job.pause_count > 0);
357786
+    assert(job->job.user_paused && job->job.pause_count > 0);
357786
     job->iostatus = BLOCK_DEVICE_IO_STATUS_OK;
357786
 }
357786
 
357786
+void block_job_user_resume(Job *job)
357786
+{
357786
+    BlockJob *bjob = container_of(job, BlockJob, job);
357786
+    block_job_iostatus_reset(bjob);
357786
+}
357786
+
357786
 void block_job_event_ready(BlockJob *job)
357786
 {
357786
     job_state_transition(&job->job, JOB_STATUS_READY);
357786
@@ -991,9 +942,9 @@ BlockErrorAction block_job_error_action(BlockJob *job, BlockdevOnError on_err,
357786
                                         action, &error_abort);
357786
     }
357786
     if (action == BLOCK_ERROR_ACTION_STOP) {
357786
-        block_job_pause(job);
357786
+        job_pause(&job->job);
357786
         /* make the pause user visible, which will be resumed from QMP. */
357786
-        job->user_paused = true;
357786
+        job->job.user_paused = true;
357786
         block_job_iostatus_set_err(job, error);
357786
     }
357786
     return action;
357786
diff --git a/include/block/blockjob.h b/include/block/blockjob.h
357786
index b60d919..556a8f6 100644
357786
--- a/include/block/blockjob.h
357786
+++ b/include/block/blockjob.h
357786
@@ -57,12 +57,6 @@ typedef struct BlockJob {
357786
     bool force;
357786
 
357786
     /**
357786
-     * Set to true if the job is paused by user.  Can be unpaused with the
357786
-     * block-job-resume QMP command.
357786
-     */
357786
-    bool user_paused;
357786
-
357786
-    /**
357786
      * Set to true when the job is ready to be completed.
357786
      */
357786
     bool ready;
357786
@@ -248,32 +242,6 @@ void block_job_progress_set_remaining(BlockJob *job, uint64_t remaining);
357786
 BlockJobInfo *block_job_query(BlockJob *job, Error **errp);
357786
 
357786
 /**
357786
- * block_job_user_pause:
357786
- * @job: The job to be paused.
357786
- *
357786
- * Asynchronously pause the specified job.
357786
- * Do not allow a resume until a matching call to block_job_user_resume.
357786
- */
357786
-void block_job_user_pause(BlockJob *job, Error **errp);
357786
-
357786
-/**
357786
- * block_job_paused:
357786
- * @job: The job to query.
357786
- *
357786
- * Returns true if the job is user-paused.
357786
- */
357786
-bool block_job_user_paused(BlockJob *job);
357786
-
357786
-/**
357786
- * block_job_user_resume:
357786
- * @job: The job to be resumed.
357786
- *
357786
- * Resume the specified job.
357786
- * Must be paired with a preceding block_job_user_pause.
357786
- */
357786
-void block_job_user_resume(BlockJob *job, Error **errp);
357786
-
357786
-/**
357786
  * block_job_user_cancel:
357786
  * @job: The job to be cancelled.
357786
  * @force: Quit a job without waiting for data to be in sync.
357786
diff --git a/include/block/blockjob_int.h b/include/block/blockjob_int.h
357786
index 8937f5b..7e705ae 100644
357786
--- a/include/block/blockjob_int.h
357786
+++ b/include/block/blockjob_int.h
357786
@@ -134,6 +134,13 @@ void *block_job_create(const char *job_id, const BlockJobDriver *driver,
357786
 void block_job_free(Job *job);
357786
 
357786
 /**
357786
+ * block_job_user_resume:
357786
+ * Callback to be used for JobDriver.user_resume in all block jobs. Resets the
357786
+ * iostatus when the user resumes @job.
357786
+ */
357786
+void block_job_user_resume(Job *job);
357786
+
357786
+/**
357786
  * block_job_yield:
357786
  * @job: The job that calls the function.
357786
  *
357786
diff --git a/include/qemu/job.h b/include/qemu/job.h
357786
index 509408f..bc63985 100644
357786
--- a/include/qemu/job.h
357786
+++ b/include/qemu/job.h
357786
@@ -83,6 +83,12 @@ typedef struct Job {
357786
     bool paused;
357786
 
357786
     /**
357786
+     * Set to true if the job is paused by user.  Can be unpaused with the
357786
+     * block-job-resume QMP command.
357786
+     */
357786
+    bool user_paused;
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
@@ -124,6 +130,12 @@ struct JobDriver {
357786
      */
357786
     void coroutine_fn (*resume)(Job *job);
357786
 
357786
+    /**
357786
+     * Called when the job is resumed by the user (i.e. user_paused becomes
357786
+     * false). .user_resume is called before .resume.
357786
+     */
357786
+    void (*user_resume)(Job *job);
357786
+
357786
     /** Called when the job is freed */
357786
     void (*free)(Job *job);
357786
 };
357786
@@ -203,6 +215,31 @@ const char *job_type_str(const Job *job);
357786
 bool job_is_cancelled(Job *job);
357786
 
357786
 /**
357786
+ * Request @job to pause at the next pause point. Must be paired with
357786
+ * job_resume(). If the job is supposed to be resumed by user action, call
357786
+ * job_user_pause() instead.
357786
+ */
357786
+void job_pause(Job *job);
357786
+
357786
+/** Resumes a @job paused with job_pause. */
357786
+void job_resume(Job *job);
357786
+
357786
+/**
357786
+ * Asynchronously pause the specified @job.
357786
+ * Do not allow a resume until a matching call to job_user_resume.
357786
+ */
357786
+void job_user_pause(Job *job, Error **errp);
357786
+
357786
+/** Returns true if the job is user-paused. */
357786
+bool job_user_paused(Job *job);
357786
+
357786
+/**
357786
+ * Resume the specified @job.
357786
+ * Must be paired with a preceding job_user_pause.
357786
+ */
357786
+void job_user_resume(Job *job, Error **errp);
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
  *
357786
diff --git a/job.c b/job.c
357786
index 1b8cba1..fd10b1d 100644
357786
--- a/job.c
357786
+++ b/job.c
357786
@@ -341,6 +341,65 @@ void job_start(Job *job)
357786
     aio_co_enter(job->aio_context, job->co);
357786
 }
357786
 
357786
+/* Assumes the block_job_mutex is held */
357786
+static bool job_timer_not_pending(Job *job)
357786
+{
357786
+    return !timer_pending(&job->sleep_timer);
357786
+}
357786
+
357786
+void job_pause(Job *job)
357786
+{
357786
+    job->pause_count++;
357786
+}
357786
+
357786
+void job_resume(Job *job)
357786
+{
357786
+    assert(job->pause_count > 0);
357786
+    job->pause_count--;
357786
+    if (job->pause_count) {
357786
+        return;
357786
+    }
357786
+
357786
+    /* kick only if no timer is pending */
357786
+    job_enter_cond(job, job_timer_not_pending);
357786
+}
357786
+
357786
+void job_user_pause(Job *job, Error **errp)
357786
+{
357786
+    if (job_apply_verb(job, JOB_VERB_PAUSE, errp)) {
357786
+        return;
357786
+    }
357786
+    if (job->user_paused) {
357786
+        error_setg(errp, "Job is already paused");
357786
+        return;
357786
+    }
357786
+    job->user_paused = true;
357786
+    job_pause(job);
357786
+}
357786
+
357786
+bool job_user_paused(Job *job)
357786
+{
357786
+    return job->user_paused;
357786
+}
357786
+
357786
+void job_user_resume(Job *job, Error **errp)
357786
+{
357786
+    assert(job);
357786
+    if (!job->user_paused || job->pause_count <= 0) {
357786
+        error_setg(errp, "Can't resume a job that was not paused");
357786
+        return;
357786
+    }
357786
+    if (job_apply_verb(job, JOB_VERB_RESUME, errp)) {
357786
+        return;
357786
+    }
357786
+    if (job->driver->user_resume) {
357786
+        job->driver->user_resume(job);
357786
+    }
357786
+    job->user_paused = false;
357786
+    job_resume(job);
357786
+}
357786
+
357786
+
357786
 typedef struct {
357786
     Job *job;
357786
     JobDeferToMainLoopFn *fn;
357786
diff --git a/tests/test-bdrv-drain.c b/tests/test-bdrv-drain.c
357786
index 50232f5..c993512 100644
357786
--- a/tests/test-bdrv-drain.c
357786
+++ b/tests/test-bdrv-drain.c
357786
@@ -524,6 +524,7 @@ BlockJobDriver test_job_driver = {
357786
     .job_driver = {
357786
         .instance_size  = sizeof(TestBlockJob),
357786
         .free           = block_job_free,
357786
+        .user_resume    = block_job_user_resume,
357786
         .start          = test_job_start,
357786
     },
357786
     .complete       = test_job_complete,
357786
diff --git a/tests/test-blockjob-txn.c b/tests/test-blockjob-txn.c
357786
index 0e6162b..93d1ff0 100644
357786
--- a/tests/test-blockjob-txn.c
357786
+++ b/tests/test-blockjob-txn.c
357786
@@ -78,6 +78,7 @@ static const BlockJobDriver test_block_job_driver = {
357786
     .job_driver = {
357786
         .instance_size = sizeof(TestBlockJob),
357786
         .free          = block_job_free,
357786
+        .user_resume   = block_job_user_resume,
357786
         .start         = test_block_job_run,
357786
     },
357786
 };
357786
diff --git a/tests/test-blockjob.c b/tests/test-blockjob.c
357786
index b329bd5..ceb5960 100644
357786
--- a/tests/test-blockjob.c
357786
+++ b/tests/test-blockjob.c
357786
@@ -20,6 +20,7 @@ static const BlockJobDriver test_block_job_driver = {
357786
     .job_driver = {
357786
         .instance_size = sizeof(BlockJob),
357786
         .free          = block_job_free,
357786
+        .user_resume   = block_job_user_resume,
357786
     },
357786
 };
357786
 
357786
@@ -199,6 +200,7 @@ static const BlockJobDriver test_cancel_driver = {
357786
     .job_driver = {
357786
         .instance_size = sizeof(CancelJob),
357786
         .free          = block_job_free,
357786
+        .user_resume   = block_job_user_resume,
357786
         .start         = cancel_job_start,
357786
     },
357786
     .complete      = cancel_job_complete,
357786
@@ -270,7 +272,7 @@ static void test_cancel_paused(void)
357786
     job_start(&job->job);
357786
     assert(job->job.status == JOB_STATUS_RUNNING);
357786
 
357786
-    block_job_user_pause(job, &error_abort);
357786
+    job_user_pause(&job->job, &error_abort);
357786
     block_job_enter(job);
357786
     assert(job->job.status == JOB_STATUS_PAUSED);
357786
 
357786
@@ -308,7 +310,7 @@ static void test_cancel_standby(void)
357786
     block_job_enter(job);
357786
     assert(job->job.status == JOB_STATUS_READY);
357786
 
357786
-    block_job_user_pause(job, &error_abort);
357786
+    job_user_pause(&job->job, &error_abort);
357786
     block_job_enter(job);
357786
     assert(job->job.status == JOB_STATUS_STANDBY);
357786
 
357786
-- 
357786
1.8.3.1
357786