Blame SOURCES/kvm-block-create-Make-x-blockdev-create-a-job.patch

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