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" 27 #include "qemu/job.h" 28 #include "qapi/qapi-commands-block-core.h" 29 #include "qapi/qapi-visit-block-core.h" 30 #include "qapi/clone-visitor.h" 31 #include "qapi/error.h" 32 33 typedef struct BlockdevCreateJob { 34 Job common; 35 BlockDriver *drv; 36 BlockdevCreateOptions *opts; 37 int ret; 38 Error *err; 39 } BlockdevCreateJob; 40 41 static void blockdev_create_complete(Job *job, void *opaque) 42 { 43 BlockdevCreateJob *s = container_of(job, BlockdevCreateJob, common); 44 45 job_completed(job, s->ret, s->err); 46 } 47 48 static void coroutine_fn blockdev_create_run(void *opaque) 49 { 50 BlockdevCreateJob *s = opaque; 51 52 job_progress_set_remaining(&s->common, 1); 53 s->ret = s->drv->bdrv_co_create(s->opts, &s->err); 54 job_progress_update(&s->common, 1); 55 56 qapi_free_BlockdevCreateOptions(s->opts); 57 job_defer_to_main_loop(&s->common, blockdev_create_complete, NULL); 58 } 59 60 static const JobDriver blockdev_create_job_driver = { 61 .instance_size = sizeof(BlockdevCreateJob), 62 .job_type = JOB_TYPE_CREATE, 63 .start = blockdev_create_run, 64 }; 65 66 void qmp_blockdev_create(const char *job_id, BlockdevCreateOptions *options, 67 Error **errp) 68 { 69 BlockdevCreateJob *s; 70 const char *fmt = BlockdevDriver_str(options->driver); 71 BlockDriver *drv = bdrv_find_format(fmt); 72 73 /* If the driver is in the schema, we know that it exists. But it may not 74 * be whitelisted. */ 75 assert(drv); 76 if (bdrv_uses_whitelist() && !bdrv_is_whitelisted(drv, false)) { 77 error_setg(errp, "Driver is not whitelisted"); 78 return; 79 } 80 81 /* Error out if the driver doesn't support .bdrv_co_create */ 82 if (!drv->bdrv_co_create) { 83 error_setg(errp, "Driver does not support blockdev-create"); 84 return; 85 } 86 87 /* Create the block job */ 88 /* TODO Running in the main context. Block drivers need to error out or add 89 * locking when they use a BDS in a different AioContext. */ 90 s = job_create(job_id, &blockdev_create_job_driver, NULL, 91 qemu_get_aio_context(), JOB_DEFAULT | JOB_MANUAL_DISMISS, 92 NULL, NULL, errp); 93 if (!s) { 94 return; 95 } 96 97 s->drv = drv, 98 s->opts = QAPI_CLONE(BlockdevCreateOptions, options), 99 100 job_start(&s->common); 101 } 102