Max Reitz | 6c6f24f | 2018-04-21 15:29:21 +0200 | [diff] [blame] | 1 | /* |
| 2 | * Copy-on-read filter block driver |
| 3 | * |
| 4 | * Copyright (c) 2018 Red Hat, Inc. |
| 5 | * |
| 6 | * Author: |
| 7 | * Max Reitz <mreitz@redhat.com> |
| 8 | * |
| 9 | * This program is free software; you can redistribute it and/or |
| 10 | * modify it under the terms of the GNU General Public License as |
| 11 | * published by the Free Software Foundation; either version 2 or |
| 12 | * (at your option) version 3 of the License. |
| 13 | * |
| 14 | * This program is distributed in the hope that it will be useful, |
| 15 | * but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 16 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 17 | * GNU General Public License for more details. |
| 18 | * |
| 19 | * You should have received a copy of the GNU General Public License |
| 20 | * along with this program; if not, see <http://www.gnu.org/licenses/>. |
| 21 | */ |
| 22 | |
| 23 | #include "qemu/osdep.h" |
Markus Armbruster | e2c1c34 | 2022-12-21 14:35:49 +0100 | [diff] [blame] | 24 | #include "block/block-io.h" |
Max Reitz | 6c6f24f | 2018-04-21 15:29:21 +0200 | [diff] [blame] | 25 | #include "block/block_int.h" |
Markus Armbruster | 0b8fa32 | 2019-05-23 16:35:07 +0200 | [diff] [blame] | 26 | #include "qemu/module.h" |
Andrey Shinkevich | 16e09a2 | 2020-12-16 09:16:53 +0300 | [diff] [blame] | 27 | #include "qapi/error.h" |
Andrey Shinkevich | e4c8fdd | 2020-12-16 09:16:55 +0300 | [diff] [blame] | 28 | #include "qapi/qmp/qdict.h" |
Andrey Shinkevich | 16e09a2 | 2020-12-16 09:16:53 +0300 | [diff] [blame] | 29 | #include "block/copy-on-read.h" |
| 30 | |
| 31 | |
| 32 | typedef struct BDRVStateCOR { |
Andrey Shinkevich | e4c8fdd | 2020-12-16 09:16:55 +0300 | [diff] [blame] | 33 | BlockDriverState *bottom_bs; |
| 34 | bool chain_frozen; |
Andrey Shinkevich | 16e09a2 | 2020-12-16 09:16:53 +0300 | [diff] [blame] | 35 | } BDRVStateCOR; |
Max Reitz | 6c6f24f | 2018-04-21 15:29:21 +0200 | [diff] [blame] | 36 | |
| 37 | |
| 38 | static int cor_open(BlockDriverState *bs, QDict *options, int flags, |
| 39 | Error **errp) |
| 40 | { |
Andrey Shinkevich | e4c8fdd | 2020-12-16 09:16:55 +0300 | [diff] [blame] | 41 | BlockDriverState *bottom_bs = NULL; |
Andrey Shinkevich | 16e09a2 | 2020-12-16 09:16:53 +0300 | [diff] [blame] | 42 | BDRVStateCOR *state = bs->opaque; |
Andrey Shinkevich | e4c8fdd | 2020-12-16 09:16:55 +0300 | [diff] [blame] | 43 | /* Find a bottom node name, if any */ |
| 44 | const char *bottom_node = qdict_get_try_str(options, "bottom"); |
Vladimir Sementsov-Ogievskiy | 8393078 | 2022-07-26 23:11:21 +0300 | [diff] [blame] | 45 | int ret; |
Andrey Shinkevich | 16e09a2 | 2020-12-16 09:16:53 +0300 | [diff] [blame] | 46 | |
Vladimir Sementsov-Ogievskiy | 8393078 | 2022-07-26 23:11:21 +0300 | [diff] [blame] | 47 | ret = bdrv_open_file_child(NULL, options, "file", bs, errp); |
| 48 | if (ret < 0) { |
| 49 | return ret; |
Max Reitz | 6c6f24f | 2018-04-21 15:29:21 +0200 | [diff] [blame] | 50 | } |
| 51 | |
Andrey Shinkevich | e275458 | 2020-12-16 09:16:58 +0300 | [diff] [blame] | 52 | bs->supported_read_flags = BDRV_REQ_PREFETCH; |
| 53 | |
Max Reitz | 228345b | 2018-04-21 15:29:26 +0200 | [diff] [blame] | 54 | bs->supported_write_flags = BDRV_REQ_WRITE_UNCHANGED | |
Kevin Wolf | 80f5c33 | 2019-03-22 13:42:39 +0100 | [diff] [blame] | 55 | (BDRV_REQ_FUA & bs->file->bs->supported_write_flags); |
Max Reitz | 6c6f24f | 2018-04-21 15:29:21 +0200 | [diff] [blame] | 56 | |
Max Reitz | 228345b | 2018-04-21 15:29:26 +0200 | [diff] [blame] | 57 | bs->supported_zero_flags = BDRV_REQ_WRITE_UNCHANGED | |
Kevin Wolf | 80f5c33 | 2019-03-22 13:42:39 +0100 | [diff] [blame] | 58 | ((BDRV_REQ_FUA | BDRV_REQ_MAY_UNMAP | BDRV_REQ_NO_FALLBACK) & |
| 59 | bs->file->bs->supported_zero_flags); |
Max Reitz | 6c6f24f | 2018-04-21 15:29:21 +0200 | [diff] [blame] | 60 | |
Andrey Shinkevich | e4c8fdd | 2020-12-16 09:16:55 +0300 | [diff] [blame] | 61 | if (bottom_node) { |
| 62 | bottom_bs = bdrv_find_node(bottom_node); |
| 63 | if (!bottom_bs) { |
| 64 | error_setg(errp, "Bottom node '%s' not found", bottom_node); |
| 65 | qdict_del(options, "bottom"); |
| 66 | return -EINVAL; |
| 67 | } |
| 68 | qdict_del(options, "bottom"); |
| 69 | |
| 70 | if (!bottom_bs->drv) { |
| 71 | error_setg(errp, "Bottom node '%s' not opened", bottom_node); |
| 72 | return -EINVAL; |
| 73 | } |
| 74 | |
| 75 | if (bottom_bs->drv->is_filter) { |
| 76 | error_setg(errp, "Bottom node '%s' is a filter", bottom_node); |
| 77 | return -EINVAL; |
| 78 | } |
| 79 | |
| 80 | if (bdrv_freeze_backing_chain(bs, bottom_bs, errp) < 0) { |
| 81 | return -EINVAL; |
| 82 | } |
| 83 | state->chain_frozen = true; |
| 84 | |
| 85 | /* |
| 86 | * We do freeze the chain, so it shouldn't be removed. Still, storing a |
| 87 | * pointer worth bdrv_ref(). |
| 88 | */ |
| 89 | bdrv_ref(bottom_bs); |
| 90 | } |
Andrey Shinkevich | e4c8fdd | 2020-12-16 09:16:55 +0300 | [diff] [blame] | 91 | state->bottom_bs = bottom_bs; |
Andrey Shinkevich | 16e09a2 | 2020-12-16 09:16:53 +0300 | [diff] [blame] | 92 | |
| 93 | /* |
| 94 | * We don't need to call bdrv_child_refresh_perms() now as the permissions |
| 95 | * will be updated later when the filter node gets its parent. |
| 96 | */ |
| 97 | |
Max Reitz | 6c6f24f | 2018-04-21 15:29:21 +0200 | [diff] [blame] | 98 | return 0; |
| 99 | } |
| 100 | |
| 101 | |
Max Reitz | 6c6f24f | 2018-04-21 15:29:21 +0200 | [diff] [blame] | 102 | #define PERM_PASSTHROUGH (BLK_PERM_CONSISTENT_READ \ |
| 103 | | BLK_PERM_WRITE \ |
| 104 | | BLK_PERM_RESIZE) |
| 105 | #define PERM_UNCHANGED (BLK_PERM_ALL & ~PERM_PASSTHROUGH) |
| 106 | |
| 107 | static void cor_child_perm(BlockDriverState *bs, BdrvChild *c, |
Max Reitz | bf8e925 | 2020-05-13 13:05:16 +0200 | [diff] [blame] | 108 | BdrvChildRole role, |
Max Reitz | 6c6f24f | 2018-04-21 15:29:21 +0200 | [diff] [blame] | 109 | BlockReopenQueue *reopen_queue, |
| 110 | uint64_t perm, uint64_t shared, |
| 111 | uint64_t *nperm, uint64_t *nshared) |
| 112 | { |
Kevin Wolf | 2b23f28 | 2019-07-29 12:45:14 +0200 | [diff] [blame] | 113 | *nperm = perm & PERM_PASSTHROUGH; |
| 114 | *nshared = (shared & PERM_PASSTHROUGH) | PERM_UNCHANGED; |
Max Reitz | 6c6f24f | 2018-04-21 15:29:21 +0200 | [diff] [blame] | 115 | |
Kevin Wolf | 2b23f28 | 2019-07-29 12:45:14 +0200 | [diff] [blame] | 116 | /* We must not request write permissions for an inactive node, the child |
| 117 | * cannot provide it. */ |
| 118 | if (!(bs->open_flags & BDRV_O_INACTIVE)) { |
| 119 | *nperm |= BLK_PERM_WRITE_UNCHANGED; |
| 120 | } |
Max Reitz | 6c6f24f | 2018-04-21 15:29:21 +0200 | [diff] [blame] | 121 | } |
| 122 | |
| 123 | |
Emanuele Giuseppe Esposito | c86422c | 2023-01-13 21:42:04 +0100 | [diff] [blame] | 124 | static int64_t coroutine_fn cor_co_getlength(BlockDriverState *bs) |
Max Reitz | 6c6f24f | 2018-04-21 15:29:21 +0200 | [diff] [blame] | 125 | { |
Emanuele Giuseppe Esposito | c86422c | 2023-01-13 21:42:04 +0100 | [diff] [blame] | 126 | return bdrv_co_getlength(bs->file->bs); |
Max Reitz | 6c6f24f | 2018-04-21 15:29:21 +0200 | [diff] [blame] | 127 | } |
| 128 | |
| 129 | |
Andrey Shinkevich | 1252e03 | 2020-12-16 09:16:51 +0300 | [diff] [blame] | 130 | static int coroutine_fn cor_co_preadv_part(BlockDriverState *bs, |
Vladimir Sementsov-Ogievskiy | f7ef38d | 2021-09-03 13:27:59 +0300 | [diff] [blame] | 131 | int64_t offset, int64_t bytes, |
Andrey Shinkevich | 1252e03 | 2020-12-16 09:16:51 +0300 | [diff] [blame] | 132 | QEMUIOVector *qiov, |
| 133 | size_t qiov_offset, |
Vladimir Sementsov-Ogievskiy | f7ef38d | 2021-09-03 13:27:59 +0300 | [diff] [blame] | 134 | BdrvRequestFlags flags) |
Max Reitz | 6c6f24f | 2018-04-21 15:29:21 +0200 | [diff] [blame] | 135 | { |
Andrey Shinkevich | e4c8fdd | 2020-12-16 09:16:55 +0300 | [diff] [blame] | 136 | int64_t n; |
| 137 | int local_flags; |
| 138 | int ret; |
| 139 | BDRVStateCOR *state = bs->opaque; |
| 140 | |
| 141 | if (!state->bottom_bs) { |
| 142 | return bdrv_co_preadv_part(bs->file, offset, bytes, qiov, qiov_offset, |
| 143 | flags | BDRV_REQ_COPY_ON_READ); |
| 144 | } |
| 145 | |
| 146 | while (bytes) { |
| 147 | local_flags = flags; |
| 148 | |
| 149 | /* In case of failure, try to copy-on-read anyway */ |
| 150 | ret = bdrv_is_allocated(bs->file->bs, offset, bytes, &n); |
| 151 | if (ret <= 0) { |
| 152 | ret = bdrv_is_allocated_above(bdrv_backing_chain_next(bs->file->bs), |
| 153 | state->bottom_bs, true, offset, |
| 154 | n, &n); |
| 155 | if (ret > 0 || ret < 0) { |
| 156 | local_flags |= BDRV_REQ_COPY_ON_READ; |
| 157 | } |
| 158 | /* Finish earlier if the end of a backing file has been reached */ |
| 159 | if (n == 0) { |
| 160 | break; |
| 161 | } |
| 162 | } |
| 163 | |
Andrey Shinkevich | e275458 | 2020-12-16 09:16:58 +0300 | [diff] [blame] | 164 | /* Skip if neither read nor write are needed */ |
| 165 | if ((local_flags & (BDRV_REQ_PREFETCH | BDRV_REQ_COPY_ON_READ)) != |
| 166 | BDRV_REQ_PREFETCH) { |
| 167 | ret = bdrv_co_preadv_part(bs->file, offset, n, qiov, qiov_offset, |
| 168 | local_flags); |
| 169 | if (ret < 0) { |
| 170 | return ret; |
| 171 | } |
Andrey Shinkevich | e4c8fdd | 2020-12-16 09:16:55 +0300 | [diff] [blame] | 172 | } |
| 173 | |
| 174 | offset += n; |
| 175 | qiov_offset += n; |
| 176 | bytes -= n; |
| 177 | } |
| 178 | |
| 179 | return 0; |
Max Reitz | 6c6f24f | 2018-04-21 15:29:21 +0200 | [diff] [blame] | 180 | } |
| 181 | |
| 182 | |
Andrey Shinkevich | 1252e03 | 2020-12-16 09:16:51 +0300 | [diff] [blame] | 183 | static int coroutine_fn cor_co_pwritev_part(BlockDriverState *bs, |
Vladimir Sementsov-Ogievskiy | e75abed | 2021-09-03 13:28:00 +0300 | [diff] [blame] | 184 | int64_t offset, |
| 185 | int64_t bytes, |
Andrey Shinkevich | 1252e03 | 2020-12-16 09:16:51 +0300 | [diff] [blame] | 186 | QEMUIOVector *qiov, |
Vladimir Sementsov-Ogievskiy | e75abed | 2021-09-03 13:28:00 +0300 | [diff] [blame] | 187 | size_t qiov_offset, |
| 188 | BdrvRequestFlags flags) |
Max Reitz | 6c6f24f | 2018-04-21 15:29:21 +0200 | [diff] [blame] | 189 | { |
Andrey Shinkevich | 1252e03 | 2020-12-16 09:16:51 +0300 | [diff] [blame] | 190 | return bdrv_co_pwritev_part(bs->file, offset, bytes, qiov, qiov_offset, |
| 191 | flags); |
Max Reitz | 6c6f24f | 2018-04-21 15:29:21 +0200 | [diff] [blame] | 192 | } |
| 193 | |
| 194 | |
| 195 | static int coroutine_fn cor_co_pwrite_zeroes(BlockDriverState *bs, |
Vladimir Sementsov-Ogievskiy | f34b2bc | 2021-09-03 13:28:03 +0300 | [diff] [blame] | 196 | int64_t offset, int64_t bytes, |
Max Reitz | 6c6f24f | 2018-04-21 15:29:21 +0200 | [diff] [blame] | 197 | BdrvRequestFlags flags) |
| 198 | { |
| 199 | return bdrv_co_pwrite_zeroes(bs->file, offset, bytes, flags); |
| 200 | } |
| 201 | |
| 202 | |
| 203 | static int coroutine_fn cor_co_pdiscard(BlockDriverState *bs, |
Vladimir Sementsov-Ogievskiy | 0c80228 | 2021-09-03 13:28:06 +0300 | [diff] [blame] | 204 | int64_t offset, int64_t bytes) |
Max Reitz | 6c6f24f | 2018-04-21 15:29:21 +0200 | [diff] [blame] | 205 | { |
Fam Zheng | 0b9fd3f | 2018-07-10 14:31:17 +0800 | [diff] [blame] | 206 | return bdrv_co_pdiscard(bs->file, offset, bytes); |
Max Reitz | 6c6f24f | 2018-04-21 15:29:21 +0200 | [diff] [blame] | 207 | } |
| 208 | |
| 209 | |
Max Reitz | 4935e8b | 2019-11-28 16:12:36 +0100 | [diff] [blame] | 210 | static int coroutine_fn cor_co_pwritev_compressed(BlockDriverState *bs, |
Vladimir Sementsov-Ogievskiy | e75abed | 2021-09-03 13:28:00 +0300 | [diff] [blame] | 211 | int64_t offset, |
| 212 | int64_t bytes, |
Max Reitz | 4935e8b | 2019-11-28 16:12:36 +0100 | [diff] [blame] | 213 | QEMUIOVector *qiov) |
| 214 | { |
| 215 | return bdrv_co_pwritev(bs->file, offset, bytes, qiov, |
| 216 | BDRV_REQ_WRITE_COMPRESSED); |
| 217 | } |
| 218 | |
| 219 | |
Emanuele Giuseppe Esposito | 2531b39 | 2023-01-13 21:42:09 +0100 | [diff] [blame] | 220 | static void coroutine_fn cor_co_eject(BlockDriverState *bs, bool eject_flag) |
Max Reitz | 6c6f24f | 2018-04-21 15:29:21 +0200 | [diff] [blame] | 221 | { |
Emanuele Giuseppe Esposito | 2531b39 | 2023-01-13 21:42:09 +0100 | [diff] [blame] | 222 | bdrv_co_eject(bs->file->bs, eject_flag); |
Max Reitz | 6c6f24f | 2018-04-21 15:29:21 +0200 | [diff] [blame] | 223 | } |
| 224 | |
| 225 | |
Emanuele Giuseppe Esposito | 2c75261 | 2023-01-13 21:42:10 +0100 | [diff] [blame] | 226 | static void coroutine_fn cor_co_lock_medium(BlockDriverState *bs, bool locked) |
Max Reitz | 6c6f24f | 2018-04-21 15:29:21 +0200 | [diff] [blame] | 227 | { |
Emanuele Giuseppe Esposito | 2c75261 | 2023-01-13 21:42:10 +0100 | [diff] [blame] | 228 | bdrv_co_lock_medium(bs->file->bs, locked); |
Max Reitz | 6c6f24f | 2018-04-21 15:29:21 +0200 | [diff] [blame] | 229 | } |
| 230 | |
| 231 | |
Andrey Shinkevich | e4c8fdd | 2020-12-16 09:16:55 +0300 | [diff] [blame] | 232 | static void cor_close(BlockDriverState *bs) |
| 233 | { |
| 234 | BDRVStateCOR *s = bs->opaque; |
| 235 | |
| 236 | if (s->chain_frozen) { |
| 237 | s->chain_frozen = false; |
| 238 | bdrv_unfreeze_backing_chain(bs, s->bottom_bs); |
| 239 | } |
| 240 | |
| 241 | bdrv_unref(s->bottom_bs); |
| 242 | } |
| 243 | |
| 244 | |
Alberto Garcia | 782b9d0 | 2019-03-18 17:48:01 +0200 | [diff] [blame] | 245 | static BlockDriver bdrv_copy_on_read = { |
Max Reitz | 6c6f24f | 2018-04-21 15:29:21 +0200 | [diff] [blame] | 246 | .format_name = "copy-on-read", |
Andrey Shinkevich | 16e09a2 | 2020-12-16 09:16:53 +0300 | [diff] [blame] | 247 | .instance_size = sizeof(BDRVStateCOR), |
Max Reitz | 6c6f24f | 2018-04-21 15:29:21 +0200 | [diff] [blame] | 248 | |
| 249 | .bdrv_open = cor_open, |
Andrey Shinkevich | e4c8fdd | 2020-12-16 09:16:55 +0300 | [diff] [blame] | 250 | .bdrv_close = cor_close, |
Max Reitz | 6c6f24f | 2018-04-21 15:29:21 +0200 | [diff] [blame] | 251 | .bdrv_child_perm = cor_child_perm, |
| 252 | |
Emanuele Giuseppe Esposito | c86422c | 2023-01-13 21:42:04 +0100 | [diff] [blame] | 253 | .bdrv_co_getlength = cor_co_getlength, |
Max Reitz | 6c6f24f | 2018-04-21 15:29:21 +0200 | [diff] [blame] | 254 | |
Andrey Shinkevich | 1252e03 | 2020-12-16 09:16:51 +0300 | [diff] [blame] | 255 | .bdrv_co_preadv_part = cor_co_preadv_part, |
| 256 | .bdrv_co_pwritev_part = cor_co_pwritev_part, |
Max Reitz | 6c6f24f | 2018-04-21 15:29:21 +0200 | [diff] [blame] | 257 | .bdrv_co_pwrite_zeroes = cor_co_pwrite_zeroes, |
| 258 | .bdrv_co_pdiscard = cor_co_pdiscard, |
Max Reitz | 4935e8b | 2019-11-28 16:12:36 +0100 | [diff] [blame] | 259 | .bdrv_co_pwritev_compressed = cor_co_pwritev_compressed, |
Max Reitz | 6c6f24f | 2018-04-21 15:29:21 +0200 | [diff] [blame] | 260 | |
Emanuele Giuseppe Esposito | 2531b39 | 2023-01-13 21:42:09 +0100 | [diff] [blame] | 261 | .bdrv_co_eject = cor_co_eject, |
Emanuele Giuseppe Esposito | 2c75261 | 2023-01-13 21:42:10 +0100 | [diff] [blame] | 262 | .bdrv_co_lock_medium = cor_co_lock_medium, |
Max Reitz | 6c6f24f | 2018-04-21 15:29:21 +0200 | [diff] [blame] | 263 | |
Max Reitz | 6c6f24f | 2018-04-21 15:29:21 +0200 | [diff] [blame] | 264 | .has_variable_length = true, |
| 265 | .is_filter = true, |
| 266 | }; |
| 267 | |
Andrey Shinkevich | 16e09a2 | 2020-12-16 09:16:53 +0300 | [diff] [blame] | 268 | |
| 269 | void bdrv_cor_filter_drop(BlockDriverState *cor_filter_bs) |
| 270 | { |
Andrey Shinkevich | 16e09a2 | 2020-12-16 09:16:53 +0300 | [diff] [blame] | 271 | BDRVStateCOR *s = cor_filter_bs->opaque; |
| 272 | |
Andrey Shinkevich | e4c8fdd | 2020-12-16 09:16:55 +0300 | [diff] [blame] | 273 | /* unfreeze, as otherwise bdrv_replace_node() will fail */ |
| 274 | if (s->chain_frozen) { |
| 275 | s->chain_frozen = false; |
| 276 | bdrv_unfreeze_backing_chain(cor_filter_bs, s->bottom_bs); |
| 277 | } |
Vladimir Sementsov-Ogievskiy | bcc8584 | 2021-05-06 22:41:43 +0300 | [diff] [blame] | 278 | bdrv_drop_filter(cor_filter_bs, &error_abort); |
Andrey Shinkevich | 16e09a2 | 2020-12-16 09:16:53 +0300 | [diff] [blame] | 279 | bdrv_unref(cor_filter_bs); |
| 280 | } |
| 281 | |
| 282 | |
Max Reitz | 6c6f24f | 2018-04-21 15:29:21 +0200 | [diff] [blame] | 283 | static void bdrv_copy_on_read_init(void) |
| 284 | { |
| 285 | bdrv_register(&bdrv_copy_on_read); |
| 286 | } |
| 287 | |
| 288 | block_init(bdrv_copy_on_read_init); |