ae23c9
From c8efd4c89062a362a9505fae8e1c8612dfc88e8a Mon Sep 17 00:00:00 2001
ae23c9
From: Kevin Wolf <kwolf@redhat.com>
ae23c9
Date: Tue, 26 Jun 2018 09:48:40 +0200
ae23c9
Subject: [PATCH 132/268] block/create: Make x-blockdev-create a job
ae23c9
ae23c9
RH-Author: Kevin Wolf <kwolf@redhat.com>
ae23c9
Message-id: <20180626094856.6924-58-kwolf@redhat.com>
ae23c9
Patchwork-id: 81066
ae23c9
O-Subject: [RHV-7.6 qemu-kvm-rhev PATCH v2 57/73] block/create: Make x-blockdev-create a job
ae23c9
Bugzilla: 1513543
ae23c9
RH-Acked-by: Jeffrey Cody <jcody@redhat.com>
ae23c9
RH-Acked-by: Max Reitz <mreitz@redhat.com>
ae23c9
RH-Acked-by: Fam Zheng <famz@redhat.com>
ae23c9
ae23c9
This changes the x-blockdev-create QMP command so that it doesn't block
ae23c9
the monitor and the main loop any more, but starts a background job that
ae23c9
performs the image creation.
ae23c9
ae23c9
The basic job as implemented here is all that is necessary to make image
ae23c9
creation asynchronous and to provide a QMP interface that can be marked
ae23c9
stable, but it still lacks a few features that jobs usually provide: The
ae23c9
job will ignore pause commands and it doesn't publish more than very
ae23c9
basic progress yet (total-progress is 1 and current-progress advances
ae23c9
from 0 to 1 when the driver callbacks returns). These features can be
ae23c9
added later without breaking compatibility.
ae23c9
ae23c9
Signed-off-by: Kevin Wolf <kwolf@redhat.com>
ae23c9
Reviewed-by: Max Reitz <mreitz@redhat.com>
ae23c9
Reviewed-by: Jeff Cody <jcody@redhat.com>
ae23c9
(cherry picked from commit e5ab4347f9f53495e31fcef5e232c7c6be4a0567)
ae23c9
Signed-off-by: Kevin Wolf <kwolf@redhat.com>
ae23c9
Signed-off-by: Miroslav Rezanina <mrezanin@redhat.com>
ae23c9
---
ae23c9
 block/create.c           | 67 +++++++++++++++++++++++++++++++++---------------
ae23c9
 qapi/block-core.json     | 14 ++++++----
ae23c9
 qapi/job.json            |  4 ++-
ae23c9
 tests/qemu-iotests/group | 14 +++++-----
ae23c9
 4 files changed, 66 insertions(+), 33 deletions(-)
ae23c9
ae23c9
diff --git a/block/create.c b/block/create.c
ae23c9
index 8bd8a03..1a263e4 100644
ae23c9
--- a/block/create.c
ae23c9
+++ b/block/create.c
ae23c9
@@ -24,28 +24,51 @@
ae23c9
 
ae23c9
 #include "qemu/osdep.h"
ae23c9
 #include "block/block_int.h"
ae23c9
+#include "qemu/job.h"
ae23c9
 #include "qapi/qapi-commands-block-core.h"
ae23c9
+#include "qapi/qapi-visit-block-core.h"
ae23c9
+#include "qapi/clone-visitor.h"
ae23c9
 #include "qapi/error.h"
ae23c9
 
ae23c9
-typedef struct BlockdevCreateCo {
ae23c9
+typedef struct BlockdevCreateJob {
ae23c9
+    Job common;
ae23c9
     BlockDriver *drv;
ae23c9
     BlockdevCreateOptions *opts;
ae23c9
     int ret;
ae23c9
-    Error **errp;
ae23c9
-} BlockdevCreateCo;
ae23c9
+    Error *err;
ae23c9
+} BlockdevCreateJob;
ae23c9
 
ae23c9
-static void coroutine_fn bdrv_co_create_co_entry(void *opaque)
ae23c9
+static void blockdev_create_complete(Job *job, void *opaque)
ae23c9
 {
ae23c9
-    BlockdevCreateCo *cco = opaque;
ae23c9
-    cco->ret = cco->drv->bdrv_co_create(cco->opts, cco->errp);
ae23c9
+    BlockdevCreateJob *s = container_of(job, BlockdevCreateJob, common);
ae23c9
+
ae23c9
+    job_completed(job, s->ret, s->err);
ae23c9
 }
ae23c9
 
ae23c9
-void qmp_x_blockdev_create(BlockdevCreateOptions *options, Error **errp)
ae23c9
+static void coroutine_fn blockdev_create_run(void *opaque)
ae23c9
 {
ae23c9
+    BlockdevCreateJob *s = opaque;
ae23c9
+
ae23c9
+    job_progress_set_remaining(&s->common, 1);
ae23c9
+    s->ret = s->drv->bdrv_co_create(s->opts, &s->err);
ae23c9
+    job_progress_update(&s->common, 1);
ae23c9
+
ae23c9
+    qapi_free_BlockdevCreateOptions(s->opts);
ae23c9
+    job_defer_to_main_loop(&s->common, blockdev_create_complete, NULL);
ae23c9
+}
ae23c9
+
ae23c9
+static const JobDriver blockdev_create_job_driver = {
ae23c9
+    .instance_size = sizeof(BlockdevCreateJob),
ae23c9
+    .job_type      = JOB_TYPE_CREATE,
ae23c9
+    .start         = blockdev_create_run,
ae23c9
+};
ae23c9
+
ae23c9
+void qmp_x_blockdev_create(const char *job_id, BlockdevCreateOptions *options,
ae23c9
+                           Error **errp)
ae23c9
+{
ae23c9
+    BlockdevCreateJob *s;
ae23c9
     const char *fmt = BlockdevDriver_str(options->driver);
ae23c9
     BlockDriver *drv = bdrv_find_format(fmt);
ae23c9
-    Coroutine *co;
ae23c9
-    BlockdevCreateCo cco;
ae23c9
 
ae23c9
     /* If the driver is in the schema, we know that it exists. But it may not
ae23c9
      * be whitelisted. */
ae23c9
@@ -55,22 +78,24 @@ void qmp_x_blockdev_create(BlockdevCreateOptions *options, Error **errp)
ae23c9
         return;
ae23c9
     }
ae23c9
 
ae23c9
-    /* Call callback if it exists */
ae23c9
+    /* Error out if the driver doesn't support .bdrv_co_create */
ae23c9
     if (!drv->bdrv_co_create) {
ae23c9
         error_setg(errp, "Driver does not support blockdev-create");
ae23c9
         return;
ae23c9
     }
ae23c9
 
ae23c9
-    cco = (BlockdevCreateCo) {
ae23c9
-        .drv = drv,
ae23c9
-        .opts = options,
ae23c9
-        .ret = -EINPROGRESS,
ae23c9
-        .errp = errp,
ae23c9
-    };
ae23c9
-
ae23c9
-    co = qemu_coroutine_create(bdrv_co_create_co_entry, &cco;;
ae23c9
-    qemu_coroutine_enter(co);
ae23c9
-    while (cco.ret == -EINPROGRESS) {
ae23c9
-        aio_poll(qemu_get_aio_context(), true);
ae23c9
+    /* Create the block job */
ae23c9
+    /* TODO Running in the main context. Block drivers need to error out or add
ae23c9
+     * locking when they use a BDS in a different AioContext. */
ae23c9
+    s = job_create(job_id, &blockdev_create_job_driver, NULL,
ae23c9
+                   qemu_get_aio_context(), JOB_DEFAULT | JOB_MANUAL_DISMISS,
ae23c9
+                   NULL, NULL, errp);
ae23c9
+    if (!s) {
ae23c9
+        return;
ae23c9
     }
ae23c9
+
ae23c9
+    s->drv = drv,
ae23c9
+    s->opts = QAPI_CLONE(BlockdevCreateOptions, options),
ae23c9
+
ae23c9
+    job_start(&s->common);
ae23c9
 }
ae23c9
diff --git a/qapi/block-core.json b/qapi/block-core.json
ae23c9
index 96ddf87..050fbf3 100644
ae23c9
--- a/qapi/block-core.json
ae23c9
+++ b/qapi/block-core.json
ae23c9
@@ -4027,14 +4027,18 @@
ae23c9
 ##
ae23c9
 # @x-blockdev-create:
ae23c9
 #
ae23c9
-# Create an image format on a given node.
ae23c9
-# TODO Replace with something asynchronous (block job?)
ae23c9
+# Starts a job to create an image format on a given node. The job is
ae23c9
+# automatically finalized, but a manual job-dismiss is required.
ae23c9
 #
ae23c9
-# Since: 2.12
ae23c9
+# @job-id:          Identifier for the newly created job.
ae23c9
+#
ae23c9
+# @options:         Options for the image creation.
ae23c9
+#
ae23c9
+# Since: 3.0
ae23c9
 ##
ae23c9
 { 'command': 'x-blockdev-create',
ae23c9
-  'data': 'BlockdevCreateOptions',
ae23c9
-  'boxed': true }
ae23c9
+  'data': { 'job-id': 'str',
ae23c9
+            'options': 'BlockdevCreateOptions' } }
ae23c9
 
ae23c9
 ##
ae23c9
 # @blockdev-open-tray:
ae23c9
diff --git a/qapi/job.json b/qapi/job.json
ae23c9
index 970124d..69c1970 100644
ae23c9
--- a/qapi/job.json
ae23c9
+++ b/qapi/job.json
ae23c9
@@ -17,10 +17,12 @@
ae23c9
 #
ae23c9
 # @backup: drive backup job type, see "drive-backup"
ae23c9
 #
ae23c9
+# @create: image creation job type, see "x-blockdev-create" (since 3.0)
ae23c9
+#
ae23c9
 # Since: 1.7
ae23c9
 ##
ae23c9
 { 'enum': 'JobType',
ae23c9
-  'data': ['commit', 'stream', 'mirror', 'backup'] }
ae23c9
+  'data': ['commit', 'stream', 'mirror', 'backup', 'create'] }
ae23c9
 
ae23c9
 ##
ae23c9
 # @JobStatus:
ae23c9
diff --git a/tests/qemu-iotests/group b/tests/qemu-iotests/group
ae23c9
index 5c55adc..37ec5f1 100644
ae23c9
--- a/tests/qemu-iotests/group
ae23c9
+++ b/tests/qemu-iotests/group
ae23c9
@@ -204,14 +204,16 @@
ae23c9
 203 rw auto
ae23c9
 204 rw auto quick
ae23c9
 205 rw auto quick
ae23c9
-206 rw auto
ae23c9
-207 rw auto
ae23c9
+# TODO The following commented out tests need to be reworked to work
ae23c9
+# with the x-blockdev-create job
ae23c9
+#206 rw auto
ae23c9
+#207 rw auto
ae23c9
 208 rw auto quick
ae23c9
 209 rw auto quick
ae23c9
-210 rw auto
ae23c9
-211 rw auto quick
ae23c9
-212 rw auto quick
ae23c9
-213 rw auto quick
ae23c9
+#210 rw auto
ae23c9
+#211 rw auto quick
ae23c9
+#212 rw auto quick
ae23c9
+#213 rw auto quick
ae23c9
 214 rw auto
ae23c9
 215 rw auto quick
ae23c9
 216 rw auto quick
ae23c9
-- 
ae23c9
1.8.3.1
ae23c9