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

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