Maxim Levitsky | ced914d | 2020-06-25 14:55:45 +0200 | [diff] [blame] | 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" |
Markus Armbruster | e2c1c34 | 2022-12-21 14:35:49 +0100 | [diff] [blame] | 29 | #include "block/block-io.h" |
Maxim Levitsky | ced914d | 2020-06-25 14:55:45 +0200 | [diff] [blame] | 30 | #include "block/block_int.h" |
| 31 | #include "qemu/job.h" |
| 32 | #include "qemu/main-loop.h" |
| 33 | #include "qapi/qapi-commands-block-core.h" |
| 34 | #include "qapi/qapi-visit-block-core.h" |
| 35 | #include "qapi/clone-visitor.h" |
| 36 | #include "qapi/error.h" |
| 37 | |
| 38 | typedef struct BlockdevAmendJob { |
| 39 | Job common; |
| 40 | BlockdevAmendOptions *opts; |
| 41 | BlockDriverState *bs; |
| 42 | bool force; |
| 43 | } BlockdevAmendJob; |
| 44 | |
| 45 | static int coroutine_fn blockdev_amend_run(Job *job, Error **errp) |
| 46 | { |
| 47 | BlockdevAmendJob *s = container_of(job, BlockdevAmendJob, common); |
| 48 | int ret; |
Emanuele Giuseppe Esposito | 840428a | 2023-05-04 13:57:46 +0200 | [diff] [blame] | 49 | GRAPH_RDLOCK_GUARD(); |
Maxim Levitsky | ced914d | 2020-06-25 14:55:45 +0200 | [diff] [blame] | 50 | |
| 51 | job_progress_set_remaining(&s->common, 1); |
| 52 | ret = s->bs->drv->bdrv_co_amend(s->bs, s->opts, s->force, errp); |
| 53 | job_progress_update(&s->common, 1); |
| 54 | qapi_free_BlockdevAmendOptions(s->opts); |
| 55 | return ret; |
| 56 | } |
| 57 | |
Emanuele Giuseppe Esposito | 840428a | 2023-05-04 13:57:46 +0200 | [diff] [blame] | 58 | static int GRAPH_RDLOCK |
| 59 | blockdev_amend_pre_run(BlockdevAmendJob *s, Error **errp) |
Emanuele Giuseppe Esposito | c1019d1 | 2022-02-09 05:54:48 -0500 | [diff] [blame] | 60 | { |
| 61 | if (s->bs->drv->bdrv_amend_pre_run) { |
| 62 | return s->bs->drv->bdrv_amend_pre_run(s->bs, errp); |
| 63 | } |
| 64 | |
| 65 | return 0; |
| 66 | } |
| 67 | |
Hanna Reitz | b8ba600 | 2022-03-04 16:37:26 +0100 | [diff] [blame] | 68 | static void blockdev_amend_free(Job *job) |
Emanuele Giuseppe Esposito | c1019d1 | 2022-02-09 05:54:48 -0500 | [diff] [blame] | 69 | { |
| 70 | BlockdevAmendJob *s = container_of(job, BlockdevAmendJob, common); |
| 71 | |
Emanuele Giuseppe Esposito | 840428a | 2023-05-04 13:57:46 +0200 | [diff] [blame] | 72 | bdrv_graph_rdlock_main_loop(); |
Emanuele Giuseppe Esposito | c1019d1 | 2022-02-09 05:54:48 -0500 | [diff] [blame] | 73 | if (s->bs->drv->bdrv_amend_clean) { |
| 74 | s->bs->drv->bdrv_amend_clean(s->bs); |
| 75 | } |
Emanuele Giuseppe Esposito | 840428a | 2023-05-04 13:57:46 +0200 | [diff] [blame] | 76 | bdrv_graph_rdunlock_main_loop(); |
Hanna Reitz | 78fa41f | 2022-03-04 16:37:28 +0100 | [diff] [blame] | 77 | |
| 78 | bdrv_unref(s->bs); |
Emanuele Giuseppe Esposito | c1019d1 | 2022-02-09 05:54:48 -0500 | [diff] [blame] | 79 | } |
| 80 | |
Maxim Levitsky | ced914d | 2020-06-25 14:55:45 +0200 | [diff] [blame] | 81 | static const JobDriver blockdev_amend_job_driver = { |
| 82 | .instance_size = sizeof(BlockdevAmendJob), |
| 83 | .job_type = JOB_TYPE_AMEND, |
| 84 | .run = blockdev_amend_run, |
Hanna Reitz | b8ba600 | 2022-03-04 16:37:26 +0100 | [diff] [blame] | 85 | .free = blockdev_amend_free, |
Maxim Levitsky | ced914d | 2020-06-25 14:55:45 +0200 | [diff] [blame] | 86 | }; |
| 87 | |
| 88 | void qmp_x_blockdev_amend(const char *job_id, |
| 89 | const char *node_name, |
| 90 | BlockdevAmendOptions *options, |
| 91 | bool has_force, |
| 92 | bool force, |
| 93 | Error **errp) |
| 94 | { |
| 95 | BlockdevAmendJob *s; |
| 96 | const char *fmt = BlockdevDriver_str(options->driver); |
| 97 | BlockDriver *drv = bdrv_find_format(fmt); |
Max Reitz | 984c367 | 2020-07-10 11:50:37 +0200 | [diff] [blame] | 98 | BlockDriverState *bs; |
Maxim Levitsky | ced914d | 2020-06-25 14:55:45 +0200 | [diff] [blame] | 99 | |
Emanuele Giuseppe Esposito | 840428a | 2023-05-04 13:57:46 +0200 | [diff] [blame] | 100 | GRAPH_RDLOCK_GUARD_MAINLOOP(); |
| 101 | |
Max Reitz | 984c367 | 2020-07-10 11:50:37 +0200 | [diff] [blame] | 102 | bs = bdrv_lookup_bs(NULL, node_name, errp); |
| 103 | if (!bs) { |
| 104 | return; |
| 105 | } |
Maxim Levitsky | ced914d | 2020-06-25 14:55:45 +0200 | [diff] [blame] | 106 | |
| 107 | if (!drv) { |
| 108 | error_setg(errp, "Block driver '%s' not found or not supported", fmt); |
| 109 | return; |
| 110 | } |
| 111 | |
| 112 | /* |
| 113 | * If the driver is in the schema, we know that it exists. But it may not |
| 114 | * be whitelisted. |
| 115 | */ |
| 116 | if (bdrv_uses_whitelist() && !bdrv_is_whitelisted(drv, false)) { |
| 117 | error_setg(errp, "Driver is not whitelisted"); |
| 118 | return; |
| 119 | } |
| 120 | |
| 121 | if (bs->drv != drv) { |
| 122 | error_setg(errp, |
| 123 | "x-blockdev-amend doesn't support changing the block driver"); |
| 124 | return; |
| 125 | } |
| 126 | |
| 127 | /* Error out if the driver doesn't support .bdrv_co_amend */ |
| 128 | if (!drv->bdrv_co_amend) { |
| 129 | error_setg(errp, "Driver does not support x-blockdev-amend"); |
| 130 | return; |
| 131 | } |
| 132 | |
| 133 | /* Create the block job */ |
| 134 | s = job_create(job_id, &blockdev_amend_job_driver, NULL, |
| 135 | bdrv_get_aio_context(bs), JOB_DEFAULT | JOB_MANUAL_DISMISS, |
| 136 | NULL, NULL, errp); |
| 137 | if (!s) { |
| 138 | return; |
| 139 | } |
| 140 | |
Hanna Reitz | 78fa41f | 2022-03-04 16:37:28 +0100 | [diff] [blame] | 141 | bdrv_ref(bs); |
Maxim Levitsky | ced914d | 2020-06-25 14:55:45 +0200 | [diff] [blame] | 142 | s->bs = bs, |
| 143 | s->opts = QAPI_CLONE(BlockdevAmendOptions, options), |
| 144 | s->force = has_force ? force : false; |
Emanuele Giuseppe Esposito | c1019d1 | 2022-02-09 05:54:48 -0500 | [diff] [blame] | 145 | |
| 146 | if (blockdev_amend_pre_run(s, errp)) { |
| 147 | job_early_fail(&s->common); |
| 148 | return; |
| 149 | } |
| 150 | |
Maxim Levitsky | ced914d | 2020-06-25 14:55:45 +0200 | [diff] [blame] | 151 | job_start(&s->common); |
| 152 | } |