Blame SOURCES/kvm-job-Add-job_drain.patch

357786
From eb408c486b9f287df241f65b8abcb05b3c8772da Mon Sep 17 00:00:00 2001
357786
From: Kevin Wolf <kwolf@redhat.com>
357786
Date: Tue, 26 Jun 2018 09:48:18 +0200
357786
Subject: [PATCH 49/89] job: Add job_drain()
357786
357786
RH-Author: Kevin Wolf <kwolf@redhat.com>
357786
Message-id: <20180626094856.6924-36-kwolf@redhat.com>
357786
Patchwork-id: 81119
357786
O-Subject: [RHV-7.6 qemu-kvm-rhev PATCH v2 35/73] job: Add job_drain()
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
block_job_drain() contains a blk_drain() call which cannot be moved to
357786
Job, so add a new JobDriver callback JobDriver.drain which has a common
357786
implementation for all BlockJobs. In addition to this we keep the
357786
existing BlockJobDriver.drain callback that is called by the common
357786
drain implementation for all block jobs.
357786
357786
Signed-off-by: Kevin Wolf <kwolf@redhat.com>
357786
Reviewed-by: Max Reitz <mreitz@redhat.com>
357786
(cherry picked from commit b69f777dd9ba992fdd35828a90eefcd88c0ec332)
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
 blockjob.c                   | 20 ++++++++++----------
357786
 include/block/blockjob_int.h | 12 ++++++++++++
357786
 include/qemu/job.h           | 13 +++++++++++++
357786
 job.c                        | 11 +++++++++++
357786
 tests/test-bdrv-drain.c      |  1 +
357786
 tests/test-blockjob-txn.c    |  1 +
357786
 tests/test-blockjob.c        |  2 ++
357786
 11 files changed, 55 insertions(+), 10 deletions(-)
357786
357786
diff --git a/block/backup.c b/block/backup.c
357786
index bd31282..ca7d990 100644
357786
--- a/block/backup.c
357786
+++ b/block/backup.c
357786
@@ -529,6 +529,7 @@ static const BlockJobDriver backup_job_driver = {
357786
         .job_type               = JOB_TYPE_BACKUP,
357786
         .free                   = block_job_free,
357786
         .user_resume            = block_job_user_resume,
357786
+        .drain                  = block_job_drain,
357786
         .start                  = backup_run,
357786
         .commit                 = backup_commit,
357786
         .abort                  = backup_abort,
357786
diff --git a/block/commit.c b/block/commit.c
357786
index e53b2d7..02a8af9 100644
357786
--- a/block/commit.c
357786
+++ b/block/commit.c
357786
@@ -221,6 +221,7 @@ static const BlockJobDriver commit_job_driver = {
357786
         .job_type      = JOB_TYPE_COMMIT,
357786
         .free          = block_job_free,
357786
         .user_resume   = block_job_user_resume,
357786
+        .drain         = block_job_drain,
357786
         .start         = commit_run,
357786
     },
357786
 };
357786
diff --git a/block/mirror.c b/block/mirror.c
357786
index c3951d1..a579bd8 100644
357786
--- a/block/mirror.c
357786
+++ b/block/mirror.c
357786
@@ -992,6 +992,7 @@ static const BlockJobDriver mirror_job_driver = {
357786
         .job_type               = JOB_TYPE_MIRROR,
357786
         .free                   = block_job_free,
357786
         .user_resume            = block_job_user_resume,
357786
+        .drain                  = block_job_drain,
357786
         .start                  = mirror_run,
357786
         .pause                  = mirror_pause,
357786
     },
357786
@@ -1006,6 +1007,7 @@ static const BlockJobDriver commit_active_job_driver = {
357786
         .job_type               = JOB_TYPE_COMMIT,
357786
         .free                   = block_job_free,
357786
         .user_resume            = block_job_user_resume,
357786
+        .drain                  = block_job_drain,
357786
         .start                  = mirror_run,
357786
         .pause                  = mirror_pause,
357786
     },
357786
diff --git a/block/stream.c b/block/stream.c
357786
index eee0253..b996278 100644
357786
--- a/block/stream.c
357786
+++ b/block/stream.c
357786
@@ -215,6 +215,7 @@ static const BlockJobDriver stream_job_driver = {
357786
         .free          = block_job_free,
357786
         .start         = stream_run,
357786
         .user_resume   = block_job_user_resume,
357786
+        .drain         = block_job_drain,
357786
     },
357786
 };
357786
 
357786
diff --git a/blockjob.c b/blockjob.c
357786
index 4cac367..63e1669 100644
357786
--- a/blockjob.c
357786
+++ b/blockjob.c
357786
@@ -169,14 +169,13 @@ static void block_job_attached_aio_context(AioContext *new_context,
357786
     job_resume(&job->job);
357786
 }
357786
 
357786
-static void block_job_drain(BlockJob *job)
357786
+void block_job_drain(Job *job)
357786
 {
357786
-    /* If job is !job->job.busy this kicks it into the next pause point. */
357786
-    block_job_enter(job);
357786
+    BlockJob *bjob = container_of(job, BlockJob, job);
357786
 
357786
-    blk_drain(job->blk);
357786
-    if (job->driver->drain) {
357786
-        job->driver->drain(job);
357786
+    blk_drain(bjob->blk);
357786
+    if (bjob->driver->drain) {
357786
+        bjob->driver->drain(bjob);
357786
     }
357786
 }
357786
 
357786
@@ -190,7 +189,7 @@ static void block_job_detach_aio_context(void *opaque)
357786
     job_pause(&job->job);
357786
 
357786
     while (!job->job.paused && !job_is_completed(&job->job)) {
357786
-        block_job_drain(job);
357786
+        job_drain(&job->job);
357786
     }
357786
 
357786
     job->job.aio_context = NULL;
357786
@@ -327,11 +326,11 @@ static int block_job_finish_sync(BlockJob *job,
357786
         job_unref(&job->job);
357786
         return -EBUSY;
357786
     }
357786
-    /* block_job_drain calls block_job_enter, and it should be enough to
357786
-     * induce progress until the job completes or moves to the main thread.
357786
+    /* job_drain calls job_enter, and it should be enough to induce progress
357786
+     * until the job completes or moves to the main thread.
357786
     */
357786
     while (!job->job.deferred_to_main_loop && !job_is_completed(&job->job)) {
357786
-        block_job_drain(job);
357786
+        job_drain(&job->job);
357786
     }
357786
     while (!job_is_completed(&job->job)) {
357786
         aio_poll(qemu_get_aio_context(), true);
357786
@@ -713,6 +712,7 @@ void *block_job_create(const char *job_id, const BlockJobDriver *driver,
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
+    assert(job->job.driver->drain == &block_job_drain);
357786
 
357786
     job->driver        = driver;
357786
     job->blk           = blk;
357786
diff --git a/include/block/blockjob_int.h b/include/block/blockjob_int.h
357786
index bf2b762..38fe22d 100644
357786
--- a/include/block/blockjob_int.h
357786
+++ b/include/block/blockjob_int.h
357786
@@ -65,6 +65,10 @@ struct BlockJobDriver {
357786
      * If the callback is not NULL, it will be invoked when the job has to be
357786
      * synchronously cancelled or completed; it should drain BlockDriverStates
357786
      * as required to ensure progress.
357786
+     *
357786
+     * Block jobs must use the default implementation for job_driver.drain,
357786
+     * which will in turn call this callback after doing generic block job
357786
+     * stuff.
357786
      */
357786
     void (*drain)(BlockJob *job);
357786
 };
357786
@@ -112,6 +116,14 @@ void block_job_free(Job *job);
357786
 void block_job_user_resume(Job *job);
357786
 
357786
 /**
357786
+ * block_job_drain:
357786
+ * Callback to be used for JobDriver.drain in all block jobs. Drains the main
357786
+ * block node associated with the block jobs and calls BlockJobDriver.drain for
357786
+ * job-specific actions.
357786
+ */
357786
+void block_job_drain(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 2648c74..aebc195 100644
357786
--- a/include/qemu/job.h
357786
+++ b/include/qemu/job.h
357786
@@ -167,6 +167,13 @@ struct JobDriver {
357786
      */
357786
     void (*user_resume)(Job *job);
357786
 
357786
+    /*
357786
+     * If the callback is not NULL, it will be invoked when the job has to be
357786
+     * synchronously cancelled or completed; it should drain any activities
357786
+     * as required to ensure progress.
357786
+     */
357786
+    void (*drain)(Job *job);
357786
+
357786
     /**
357786
      * If the callback is not NULL, it will be invoked when all the jobs
357786
      * belonging to the same transaction complete; or upon this job's
357786
@@ -325,6 +332,12 @@ bool job_user_paused(Job *job);
357786
  */
357786
 void job_user_resume(Job *job, Error **errp);
357786
 
357786
+/*
357786
+ * Drain any activities as required to ensure progress. This can be called in a
357786
+ * loop to synchronously complete a job.
357786
+ */
357786
+void job_drain(Job *job);
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
diff --git a/job.c b/job.c
357786
index 64b64da..3772a35 100644
357786
--- a/job.c
357786
+++ b/job.c
357786
@@ -367,6 +367,17 @@ void coroutine_fn job_sleep_ns(Job *job, int64_t ns)
357786
     job_pause_point(job);
357786
 }
357786
 
357786
+void job_drain(Job *job)
357786
+{
357786
+    /* If job is !busy this kicks it into the next pause point. */
357786
+    job_enter(job);
357786
+
357786
+    if (job->driver->drain) {
357786
+        job->driver->drain(job);
357786
+    }
357786
+}
357786
+
357786
+
357786
 /**
357786
  * All jobs must allow a pause point before entering their job proper. This
357786
  * ensures that jobs can be paused prior to being started, then resumed later.
357786
diff --git a/tests/test-bdrv-drain.c b/tests/test-bdrv-drain.c
357786
index c993512..58ea566 100644
357786
--- a/tests/test-bdrv-drain.c
357786
+++ b/tests/test-bdrv-drain.c
357786
@@ -525,6 +525,7 @@ BlockJobDriver test_job_driver = {
357786
         .instance_size  = sizeof(TestBlockJob),
357786
         .free           = block_job_free,
357786
         .user_resume    = block_job_user_resume,
357786
+        .drain          = block_job_drain,
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 60e9fa2..1572f8d 100644
357786
--- a/tests/test-blockjob-txn.c
357786
+++ b/tests/test-blockjob-txn.c
357786
@@ -79,6 +79,7 @@ static const BlockJobDriver test_block_job_driver = {
357786
         .instance_size = sizeof(TestBlockJob),
357786
         .free          = block_job_free,
357786
         .user_resume   = block_job_user_resume,
357786
+        .drain         = block_job_drain,
357786
         .start         = test_block_job_run,
357786
     },
357786
 };
357786
diff --git a/tests/test-blockjob.c b/tests/test-blockjob.c
357786
index 1fe6803..592a136 100644
357786
--- a/tests/test-blockjob.c
357786
+++ b/tests/test-blockjob.c
357786
@@ -21,6 +21,7 @@ static const BlockJobDriver test_block_job_driver = {
357786
         .instance_size = sizeof(BlockJob),
357786
         .free          = block_job_free,
357786
         .user_resume   = block_job_user_resume,
357786
+        .drain         = block_job_drain,
357786
     },
357786
 };
357786
 
357786
@@ -201,6 +202,7 @@ static const BlockJobDriver test_cancel_driver = {
357786
         .instance_size = sizeof(CancelJob),
357786
         .free          = block_job_free,
357786
         .user_resume   = block_job_user_resume,
357786
+        .drain         = block_job_drain,
357786
         .start         = cancel_job_start,
357786
     },
357786
     .complete      = cancel_job_complete,
357786
-- 
357786
1.8.3.1
357786