blob: 89812669df45f7f3f2f75d7cb0590f81a97f989e [file] [log] [blame]
Kevin Wolfb0292b82018-01-09 16:50:57 +01001/*
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"
Kevin Wolfe5ab4342018-01-18 14:33:04 +010027#include "qemu/job.h"
Markus Armbrusterdb725812019-08-12 07:23:50 +020028#include "qemu/main-loop.h"
Kevin Wolfb0292b82018-01-09 16:50:57 +010029#include "qapi/qapi-commands-block-core.h"
Kevin Wolfe5ab4342018-01-18 14:33:04 +010030#include "qapi/qapi-visit-block-core.h"
31#include "qapi/clone-visitor.h"
Kevin Wolfb0292b82018-01-09 16:50:57 +010032#include "qapi/error.h"
33
Kevin Wolfe5ab4342018-01-18 14:33:04 +010034typedef struct BlockdevCreateJob {
35 Job common;
Kevin Wolfb0292b82018-01-09 16:50:57 +010036 BlockDriver *drv;
37 BlockdevCreateOptions *opts;
Kevin Wolfe5ab4342018-01-18 14:33:04 +010038} BlockdevCreateJob;
Kevin Wolfb0292b82018-01-09 16:50:57 +010039
John Snowf67432a2018-08-29 21:57:26 -040040static int coroutine_fn blockdev_create_run(Job *job, Error **errp)
Kevin Wolfb0292b82018-01-09 16:50:57 +010041{
John Snowf67432a2018-08-29 21:57:26 -040042 BlockdevCreateJob *s = container_of(job, BlockdevCreateJob, common);
John Snoweb236542018-08-29 21:57:31 -040043 int ret;
Kevin Wolfe5ab4342018-01-18 14:33:04 +010044
45 job_progress_set_remaining(&s->common, 1);
John Snoweb236542018-08-29 21:57:31 -040046 ret = s->drv->bdrv_co_create(s->opts, errp);
Kevin Wolfe5ab4342018-01-18 14:33:04 +010047 job_progress_update(&s->common, 1);
48
49 qapi_free_BlockdevCreateOptions(s->opts);
John Snowf67432a2018-08-29 21:57:26 -040050
John Snoweb236542018-08-29 21:57:31 -040051 return ret;
Kevin Wolfe5ab4342018-01-18 14:33:04 +010052}
53
54static const JobDriver blockdev_create_job_driver = {
55 .instance_size = sizeof(BlockdevCreateJob),
56 .job_type = JOB_TYPE_CREATE,
John Snowf67432a2018-08-29 21:57:26 -040057 .run = blockdev_create_run,
Kevin Wolfe5ab4342018-01-18 14:33:04 +010058};
59
Kevin Wolf3fb588a2018-05-25 18:24:51 +020060void qmp_blockdev_create(const char *job_id, BlockdevCreateOptions *options,
61 Error **errp)
Kevin Wolfe5ab4342018-01-18 14:33:04 +010062{
63 BlockdevCreateJob *s;
Kevin Wolfb0292b82018-01-09 16:50:57 +010064 const char *fmt = BlockdevDriver_str(options->driver);
65 BlockDriver *drv = bdrv_find_format(fmt);
Kevin Wolfb0292b82018-01-09 16:50:57 +010066
Philippe Mathieu-Daudéd90d5ca2019-09-12 00:08:49 +020067 if (!drv) {
68 error_setg(errp, "Block driver '%s' not found or not supported", fmt);
69 return;
70 }
71
Kevin Wolfb0292b82018-01-09 16:50:57 +010072 /* If the driver is in the schema, we know that it exists. But it may not
73 * be whitelisted. */
Kevin Wolfb0292b82018-01-09 16:50:57 +010074 if (bdrv_uses_whitelist() && !bdrv_is_whitelisted(drv, false)) {
75 error_setg(errp, "Driver is not whitelisted");
76 return;
77 }
78
Kevin Wolfe5ab4342018-01-18 14:33:04 +010079 /* Error out if the driver doesn't support .bdrv_co_create */
Kevin Wolfb0292b82018-01-09 16:50:57 +010080 if (!drv->bdrv_co_create) {
81 error_setg(errp, "Driver does not support blockdev-create");
82 return;
83 }
84
Kevin Wolfe5ab4342018-01-18 14:33:04 +010085 /* Create the block job */
86 /* TODO Running in the main context. Block drivers need to error out or add
87 * locking when they use a BDS in a different AioContext. */
88 s = job_create(job_id, &blockdev_create_job_driver, NULL,
89 qemu_get_aio_context(), JOB_DEFAULT | JOB_MANUAL_DISMISS,
90 NULL, NULL, errp);
91 if (!s) {
92 return;
Kevin Wolfb0292b82018-01-09 16:50:57 +010093 }
Kevin Wolfe5ab4342018-01-18 14:33:04 +010094
95 s->drv = drv,
96 s->opts = QAPI_CLONE(BlockdevCreateOptions, options),
97
98 job_start(&s->common);
Kevin Wolfb0292b82018-01-09 16:50:57 +010099}