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

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