]> git.proxmox.com Git - mirror_qemu.git/blame - block/create.c
block/mirror: utilize job_exit shim
[mirror_qemu.git] / block / create.c
CommitLineData
b0292b85
KW
1/*
2 * Block layer code related to image creation
3 *
4 * Copyright (c) 2018 Kevin Wolf <kwolf@redhat.com>
5 *
6 * Permission is hereby granted, free of charge, to any person obtaining a copy
7 * of this software and associated documentation files (the "Software"), to deal
8 * in the Software without restriction, including without limitation the rights
9 * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
10 * copies of the Software, and to permit persons to whom the Software is
11 * furnished to do so, subject to the following conditions:
12 *
13 * The above copyright notice and this permission notice shall be included in
14 * all copies or substantial portions of the Software.
15 *
16 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
17 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
18 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
19 * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
20 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
21 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
22 * THE SOFTWARE.
23 */
24
25#include "qemu/osdep.h"
26#include "block/block_int.h"
e5ab4347 27#include "qemu/job.h"
b0292b85 28#include "qapi/qapi-commands-block-core.h"
e5ab4347
KW
29#include "qapi/qapi-visit-block-core.h"
30#include "qapi/clone-visitor.h"
b0292b85
KW
31#include "qapi/error.h"
32
e5ab4347
KW
33typedef struct BlockdevCreateJob {
34 Job common;
b0292b85
KW
35 BlockDriver *drv;
36 BlockdevCreateOptions *opts;
37 int ret;
e5ab4347 38} BlockdevCreateJob;
b0292b85 39
e5ab4347 40static void blockdev_create_complete(Job *job, void *opaque)
b0292b85 41{
e5ab4347
KW
42 BlockdevCreateJob *s = container_of(job, BlockdevCreateJob, common);
43
3d1f8b07 44 job_completed(job, s->ret);
b0292b85
KW
45}
46
f67432a2 47static int coroutine_fn blockdev_create_run(Job *job, Error **errp)
b0292b85 48{
f67432a2 49 BlockdevCreateJob *s = container_of(job, BlockdevCreateJob, common);
e5ab4347
KW
50
51 job_progress_set_remaining(&s->common, 1);
3d1f8b07 52 s->ret = s->drv->bdrv_co_create(s->opts, errp);
e5ab4347
KW
53 job_progress_update(&s->common, 1);
54
55 qapi_free_BlockdevCreateOptions(s->opts);
56 job_defer_to_main_loop(&s->common, blockdev_create_complete, NULL);
f67432a2
JS
57
58 return s->ret;
e5ab4347
KW
59}
60
61static const JobDriver blockdev_create_job_driver = {
62 .instance_size = sizeof(BlockdevCreateJob),
63 .job_type = JOB_TYPE_CREATE,
f67432a2 64 .run = blockdev_create_run,
e5ab4347
KW
65};
66
3fb588a0
KW
67void qmp_blockdev_create(const char *job_id, BlockdevCreateOptions *options,
68 Error **errp)
e5ab4347
KW
69{
70 BlockdevCreateJob *s;
b0292b85
KW
71 const char *fmt = BlockdevDriver_str(options->driver);
72 BlockDriver *drv = bdrv_find_format(fmt);
b0292b85
KW
73
74 /* If the driver is in the schema, we know that it exists. But it may not
75 * be whitelisted. */
76 assert(drv);
77 if (bdrv_uses_whitelist() && !bdrv_is_whitelisted(drv, false)) {
78 error_setg(errp, "Driver is not whitelisted");
79 return;
80 }
81
e5ab4347 82 /* Error out if the driver doesn't support .bdrv_co_create */
b0292b85
KW
83 if (!drv->bdrv_co_create) {
84 error_setg(errp, "Driver does not support blockdev-create");
85 return;
86 }
87
e5ab4347
KW
88 /* Create the block job */
89 /* TODO Running in the main context. Block drivers need to error out or add
90 * locking when they use a BDS in a different AioContext. */
91 s = job_create(job_id, &blockdev_create_job_driver, NULL,
92 qemu_get_aio_context(), JOB_DEFAULT | JOB_MANUAL_DISMISS,
93 NULL, NULL, errp);
94 if (!s) {
95 return;
b0292b85 96 }
e5ab4347
KW
97
98 s->drv = drv,
99 s->opts = QAPI_CLONE(BlockdevCreateOptions, options),
100
101 job_start(&s->common);
b0292b85 102}