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