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