1 /* 2 * Block layer code related to image options amend 3 * 4 * Copyright (c) 2018 Kevin Wolf <kwolf@redhat.com> 5 * Copyright (c) 2020 Red Hat. Inc 6 * 7 * Heavily based on create.c 8 * 9 * Permission is hereby granted, free of charge, to any person obtaining a copy 10 * of this software and associated documentation files (the "Software"), to deal 11 * in the Software without restriction, including without limitation the rights 12 * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell 13 * copies of the Software, and to permit persons to whom the Software is 14 * furnished to do so, subject to the following conditions: 15 * 16 * The above copyright notice and this permission notice shall be included in 17 * all copies or substantial portions of the Software. 18 * 19 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 20 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 21 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL 22 * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 23 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, 24 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN 25 * THE SOFTWARE. 26 */ 27 28 #include "qemu/osdep.h" 29 #include "block/block_int.h" 30 #include "qemu/job.h" 31 #include "qemu/main-loop.h" 32 #include "qapi/qapi-commands-block-core.h" 33 #include "qapi/qapi-visit-block-core.h" 34 #include "qapi/clone-visitor.h" 35 #include "qapi/error.h" 36 37 typedef struct BlockdevAmendJob { 38 Job common; 39 BlockdevAmendOptions *opts; 40 BlockDriverState *bs; 41 bool force; 42 } BlockdevAmendJob; 43 44 static int coroutine_fn blockdev_amend_run(Job *job, Error **errp) 45 { 46 BlockdevAmendJob *s = container_of(job, BlockdevAmendJob, common); 47 int ret; 48 49 job_progress_set_remaining(&s->common, 1); 50 ret = s->bs->drv->bdrv_co_amend(s->bs, s->opts, s->force, errp); 51 job_progress_update(&s->common, 1); 52 qapi_free_BlockdevAmendOptions(s->opts); 53 return ret; 54 } 55 56 static int blockdev_amend_pre_run(BlockdevAmendJob *s, Error **errp) 57 { 58 if (s->bs->drv->bdrv_amend_pre_run) { 59 return s->bs->drv->bdrv_amend_pre_run(s->bs, errp); 60 } 61 62 return 0; 63 } 64 65 static void blockdev_amend_clean(Job *job) 66 { 67 BlockdevAmendJob *s = container_of(job, BlockdevAmendJob, common); 68 69 if (s->bs->drv->bdrv_amend_clean) { 70 s->bs->drv->bdrv_amend_clean(s->bs); 71 } 72 } 73 74 static const JobDriver blockdev_amend_job_driver = { 75 .instance_size = sizeof(BlockdevAmendJob), 76 .job_type = JOB_TYPE_AMEND, 77 .run = blockdev_amend_run, 78 .clean = blockdev_amend_clean, 79 }; 80 81 void qmp_x_blockdev_amend(const char *job_id, 82 const char *node_name, 83 BlockdevAmendOptions *options, 84 bool has_force, 85 bool force, 86 Error **errp) 87 { 88 BlockdevAmendJob *s; 89 const char *fmt = BlockdevDriver_str(options->driver); 90 BlockDriver *drv = bdrv_find_format(fmt); 91 BlockDriverState *bs; 92 93 bs = bdrv_lookup_bs(NULL, node_name, errp); 94 if (!bs) { 95 return; 96 } 97 98 if (!drv) { 99 error_setg(errp, "Block driver '%s' not found or not supported", fmt); 100 return; 101 } 102 103 /* 104 * If the driver is in the schema, we know that it exists. But it may not 105 * be whitelisted. 106 */ 107 if (bdrv_uses_whitelist() && !bdrv_is_whitelisted(drv, false)) { 108 error_setg(errp, "Driver is not whitelisted"); 109 return; 110 } 111 112 if (bs->drv != drv) { 113 error_setg(errp, 114 "x-blockdev-amend doesn't support changing the block driver"); 115 return; 116 } 117 118 /* Error out if the driver doesn't support .bdrv_co_amend */ 119 if (!drv->bdrv_co_amend) { 120 error_setg(errp, "Driver does not support x-blockdev-amend"); 121 return; 122 } 123 124 /* Create the block job */ 125 s = job_create(job_id, &blockdev_amend_job_driver, NULL, 126 bdrv_get_aio_context(bs), JOB_DEFAULT | JOB_MANUAL_DISMISS, 127 NULL, NULL, errp); 128 if (!s) { 129 return; 130 } 131 132 s->bs = bs, 133 s->opts = QAPI_CLONE(BlockdevAmendOptions, options), 134 s->force = has_force ? force : false; 135 136 if (blockdev_amend_pre_run(s, errp)) { 137 job_early_fail(&s->common); 138 return; 139 } 140 141 job_start(&s->common); 142 } 143