Blame SOURCES/kvm-job-Maintain-a-list-of-all-jobs.patch

357786
From 40315feeaa8186057e85d18aa653a0916dc55bfd Mon Sep 17 00:00:00 2001
357786
From: Kevin Wolf <kwolf@redhat.com>
357786
Date: Tue, 26 Jun 2018 09:48:03 +0200
357786
Subject: [PATCH 34/89] job: Maintain a list of all jobs
357786
357786
RH-Author: Kevin Wolf <kwolf@redhat.com>
357786
Message-id: <20180626094856.6924-21-kwolf@redhat.com>
357786
Patchwork-id: 81105
357786
O-Subject: [RHV-7.6 qemu-kvm-rhev PATCH v2 20/73] job: Maintain a list of all jobs
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
This moves the job list from BlockJob to Job. Now we can check for
357786
duplicate IDs in job_create().
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 e7c1d78bbd5867804debeb7159b137fd9a6c44d3)
357786
Signed-off-by: Kevin Wolf <kwolf@redhat.com>
357786
Signed-off-by: Miroslav Rezanina <mrezanin@redhat.com>
357786
---
357786
 blockjob.c               | 46 ++++++++++++++++++++++++----------------------
357786
 include/block/blockjob.h |  3 ---
357786
 include/qemu/job.h       | 19 +++++++++++++++++++
357786
 job.c                    | 31 +++++++++++++++++++++++++++++++
357786
 4 files changed, 74 insertions(+), 25 deletions(-)
357786
357786
diff --git a/blockjob.c b/blockjob.c
357786
index 430a67b..c69b2e7 100644
357786
--- a/blockjob.c
357786
+++ b/blockjob.c
357786
@@ -129,8 +129,6 @@ struct BlockJobTxn {
357786
     int refcnt;
357786
 };
357786
 
357786
-static QLIST_HEAD(, BlockJob) block_jobs = QLIST_HEAD_INITIALIZER(block_jobs);
357786
-
357786
 /*
357786
  * The block job API is composed of two categories of functions.
357786
  *
357786
@@ -146,25 +144,34 @@ static QLIST_HEAD(, BlockJob) block_jobs = QLIST_HEAD_INITIALIZER(block_jobs);
357786
  * blockjob_int.h.
357786
  */
357786
 
357786
-BlockJob *block_job_next(BlockJob *job)
357786
+static bool is_block_job(Job *job)
357786
 {
357786
-    if (!job) {
357786
-        return QLIST_FIRST(&block_jobs);
357786
-    }
357786
-    return QLIST_NEXT(job, job_list);
357786
+    return job_type(job) == JOB_TYPE_BACKUP ||
357786
+           job_type(job) == JOB_TYPE_COMMIT ||
357786
+           job_type(job) == JOB_TYPE_MIRROR ||
357786
+           job_type(job) == JOB_TYPE_STREAM;
357786
+}
357786
+
357786
+BlockJob *block_job_next(BlockJob *bjob)
357786
+{
357786
+    Job *job = bjob ? &bjob->job : NULL;
357786
+
357786
+    do {
357786
+        job = job_next(job);
357786
+    } while (job && !is_block_job(job));
357786
+
357786
+    return job ? container_of(job, BlockJob, job) : NULL;
357786
 }
357786
 
357786
 BlockJob *block_job_get(const char *id)
357786
 {
357786
-    BlockJob *job;
357786
+    Job *job = job_get(id);
357786
 
357786
-    QLIST_FOREACH(job, &block_jobs, job_list) {
357786
-        if (job->job.id && !strcmp(id, job->job.id)) {
357786
-            return job;
357786
-        }
357786
+    if (job && is_block_job(job)) {
357786
+        return container_of(job, BlockJob, job);
357786
+    } else {
357786
+        return NULL;
357786
     }
357786
-
357786
-    return NULL;
357786
 }
357786
 
357786
 BlockJobTxn *block_job_txn_new(void)
357786
@@ -253,7 +260,6 @@ void block_job_unref(BlockJob *job)
357786
         assert(job->status == BLOCK_JOB_STATUS_NULL);
357786
         assert(!job->txn);
357786
         BlockDriverState *bs = blk_bs(job->blk);
357786
-        QLIST_REMOVE(job, job_list);
357786
         bs->job = NULL;
357786
         block_job_remove_all_bdrv(job);
357786
         blk_remove_aio_context_notifier(job->blk,
357786
@@ -812,7 +818,7 @@ void block_job_cancel_sync_all(void)
357786
     BlockJob *job;
357786
     AioContext *aio_context;
357786
 
357786
-    while ((job = QLIST_FIRST(&block_jobs))) {
357786
+    while ((job = block_job_next(NULL))) {
357786
         aio_context = blk_get_aio_context(job->blk);
357786
         aio_context_acquire(aio_context);
357786
         block_job_cancel_sync(job);
357786
@@ -942,10 +948,6 @@ void *block_job_create(const char *job_id, const BlockJobDriver *driver,
357786
             error_setg(errp, "Cannot specify job ID for internal block job");
357786
             return NULL;
357786
         }
357786
-        if (block_job_get(job_id)) {
357786
-            error_setg(errp, "Job ID '%s' already in use", job_id);
357786
-            return NULL;
357786
-        }
357786
     }
357786
 
357786
     blk = blk_new(perm, shared_perm);
357786
@@ -961,6 +963,8 @@ void *block_job_create(const char *job_id, const BlockJobDriver *driver,
357786
         return NULL;
357786
     }
357786
 
357786
+    assert(is_block_job(&job->job));
357786
+
357786
     job->driver        = driver;
357786
     job->blk           = blk;
357786
     job->cb            = cb;
357786
@@ -983,8 +987,6 @@ void *block_job_create(const char *job_id, const BlockJobDriver *driver,
357786
 
357786
     bdrv_op_unblock(bs, BLOCK_OP_TYPE_DATAPLANE, job->blocker);
357786
 
357786
-    QLIST_INSERT_HEAD(&block_jobs, job, job_list);
357786
-
357786
     blk_add_aio_context_notifier(blk, block_job_attached_aio_context,
357786
                                  block_job_detach_aio_context, job);
357786
 
357786
diff --git a/include/block/blockjob.h b/include/block/blockjob.h
357786
index 640e649..10bd9f7 100644
357786
--- a/include/block/blockjob.h
357786
+++ b/include/block/blockjob.h
357786
@@ -105,9 +105,6 @@ typedef struct BlockJob {
357786
      */
357786
     bool deferred_to_main_loop;
357786
 
357786
-    /** Element of the list of block jobs */
357786
-    QLIST_ENTRY(BlockJob) job_list;
357786
-
357786
     /** Status that is published by the query-block-jobs QMP API */
357786
     BlockDeviceIoStatus iostatus;
357786
 
357786
diff --git a/include/qemu/job.h b/include/qemu/job.h
357786
index 43dc2e4..bae2b09 100644
357786
--- a/include/qemu/job.h
357786
+++ b/include/qemu/job.h
357786
@@ -27,6 +27,7 @@
357786
 #define JOB_H
357786
 
357786
 #include "qapi/qapi-types-block-core.h"
357786
+#include "qemu/queue.h"
357786
 
357786
 typedef struct JobDriver JobDriver;
357786
 
357786
@@ -39,6 +40,9 @@ typedef struct Job {
357786
 
357786
     /** The type of this job. */
357786
     const JobDriver *driver;
357786
+
357786
+    /** Element of the list of jobs */
357786
+    QLIST_ENTRY(Job) job_list;
357786
 } Job;
357786
 
357786
 /**
357786
@@ -71,4 +75,19 @@ JobType job_type(const Job *job);
357786
 /** Returns the enum string for the JobType of a given Job. */
357786
 const char *job_type_str(const 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
+ *
357786
+ * Returns the requested job, or %NULL if there are no more jobs left.
357786
+ */
357786
+Job *job_next(Job *job);
357786
+
357786
+/**
357786
+ * Get the job identified by @id (which must not be %NULL).
357786
+ *
357786
+ * Returns the requested job, or %NULL if it doesn't exist.
357786
+ */
357786
+Job *job_get(const char *id);
357786
+
357786
 #endif
357786
diff --git a/job.c b/job.c
357786
index cfdd008..e57303c 100644
357786
--- a/job.c
357786
+++ b/job.c
357786
@@ -29,6 +29,8 @@
357786
 #include "qemu/job.h"
357786
 #include "qemu/id.h"
357786
 
357786
+static QLIST_HEAD(, Job) jobs = QLIST_HEAD_INITIALIZER(jobs);
357786
+
357786
 JobType job_type(const Job *job)
357786
 {
357786
     return job->driver->job_type;
357786
@@ -39,6 +41,27 @@ const char *job_type_str(const Job *job)
357786
     return JobType_str(job_type(job));
357786
 }
357786
 
357786
+Job *job_next(Job *job)
357786
+{
357786
+    if (!job) {
357786
+        return QLIST_FIRST(&jobs);
357786
+    }
357786
+    return QLIST_NEXT(job, job_list);
357786
+}
357786
+
357786
+Job *job_get(const char *id)
357786
+{
357786
+    Job *job;
357786
+
357786
+    QLIST_FOREACH(job, &jobs, job_list) {
357786
+        if (job->id && !strcmp(id, job->id)) {
357786
+            return job;
357786
+        }
357786
+    }
357786
+
357786
+    return NULL;
357786
+}
357786
+
357786
 void *job_create(const char *job_id, const JobDriver *driver, Error **errp)
357786
 {
357786
     Job *job;
357786
@@ -48,17 +71,25 @@ void *job_create(const char *job_id, const JobDriver *driver, Error **errp)
357786
             error_setg(errp, "Invalid job ID '%s'", job_id);
357786
             return NULL;
357786
         }
357786
+        if (job_get(job_id)) {
357786
+            error_setg(errp, "Job ID '%s' already in use", job_id);
357786
+            return NULL;
357786
+        }
357786
     }
357786
 
357786
     job = g_malloc0(driver->instance_size);
357786
     job->driver        = driver;
357786
     job->id            = g_strdup(job_id);
357786
 
357786
+    QLIST_INSERT_HEAD(&jobs, job, job_list);
357786
+
357786
     return job;
357786
 }
357786
 
357786
 void job_delete(Job *job)
357786
 {
357786
+    QLIST_REMOVE(job, job_list);
357786
+
357786
     g_free(job->id);
357786
     g_free(job);
357786
 }
357786
-- 
357786
1.8.3.1
357786