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 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 45*da359909SEmanuele Giuseppe Esposito GLOBAL_STATE_CODE(); 46*da359909SEmanuele 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 623fb588a0SKevin Wolf void qmp_blockdev_create(const char *job_id, BlockdevCreateOptions *options, 63e5ab4347SKevin Wolf Error **errp) 64e5ab4347SKevin Wolf { 65e5ab4347SKevin Wolf BlockdevCreateJob *s; 66b0292b85SKevin Wolf const char *fmt = BlockdevDriver_str(options->driver); 67b0292b85SKevin Wolf BlockDriver *drv = bdrv_find_format(fmt); 68b0292b85SKevin Wolf 69d90d5caeSPhilippe Mathieu-Daudé if (!drv) { 70d90d5caeSPhilippe Mathieu-Daudé error_setg(errp, "Block driver '%s' not found or not supported", fmt); 71d90d5caeSPhilippe Mathieu-Daudé return; 72d90d5caeSPhilippe Mathieu-Daudé } 73d90d5caeSPhilippe Mathieu-Daudé 74b0292b85SKevin Wolf /* If the driver is in the schema, we know that it exists. But it may not 75b0292b85SKevin Wolf * be whitelisted. */ 76b0292b85SKevin Wolf if (bdrv_uses_whitelist() && !bdrv_is_whitelisted(drv, false)) { 77b0292b85SKevin Wolf error_setg(errp, "Driver is not whitelisted"); 78b0292b85SKevin Wolf return; 79b0292b85SKevin Wolf } 80b0292b85SKevin Wolf 81e5ab4347SKevin Wolf /* Error out if the driver doesn't support .bdrv_co_create */ 82b0292b85SKevin Wolf if (!drv->bdrv_co_create) { 83b0292b85SKevin Wolf error_setg(errp, "Driver does not support blockdev-create"); 84b0292b85SKevin Wolf return; 85b0292b85SKevin Wolf } 86b0292b85SKevin Wolf 87e5ab4347SKevin Wolf /* Create the block job */ 88e5ab4347SKevin Wolf /* TODO Running in the main context. Block drivers need to error out or add 89e5ab4347SKevin Wolf * locking when they use a BDS in a different AioContext. */ 90e5ab4347SKevin Wolf s = job_create(job_id, &blockdev_create_job_driver, NULL, 91e5ab4347SKevin Wolf qemu_get_aio_context(), JOB_DEFAULT | JOB_MANUAL_DISMISS, 92e5ab4347SKevin Wolf NULL, NULL, errp); 93e5ab4347SKevin Wolf if (!s) { 94e5ab4347SKevin Wolf return; 95b0292b85SKevin Wolf } 96e5ab4347SKevin Wolf 97e5ab4347SKevin Wolf s->drv = drv, 98e5ab4347SKevin Wolf s->opts = QAPI_CLONE(BlockdevCreateOptions, options), 99e5ab4347SKevin Wolf 100e5ab4347SKevin Wolf job_start(&s->common); 101b0292b85SKevin Wolf } 102