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