blob: 3280eb2febffe4a79778cfccdc145a62da563dbd [file] [log] [blame]
Max Reitz6c6f24f2018-04-21 15:29:21 +02001/*
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 Armbrustere2c1c342022-12-21 14:35:49 +010024#include "block/block-io.h"
Max Reitz6c6f24f2018-04-21 15:29:21 +020025#include "block/block_int.h"
Markus Armbruster0b8fa322019-05-23 16:35:07 +020026#include "qemu/module.h"
Andrey Shinkevich16e09a22020-12-16 09:16:53 +030027#include "qapi/error.h"
Andrey Shinkeviche4c8fdd2020-12-16 09:16:55 +030028#include "qapi/qmp/qdict.h"
Andrey Shinkevich16e09a22020-12-16 09:16:53 +030029#include "block/copy-on-read.h"
30
31
32typedef struct BDRVStateCOR {
Andrey Shinkeviche4c8fdd2020-12-16 09:16:55 +030033 BlockDriverState *bottom_bs;
34 bool chain_frozen;
Andrey Shinkevich16e09a22020-12-16 09:16:53 +030035} BDRVStateCOR;
Max Reitz6c6f24f2018-04-21 15:29:21 +020036
37
38static int cor_open(BlockDriverState *bs, QDict *options, int flags,
39 Error **errp)
40{
Andrey Shinkeviche4c8fdd2020-12-16 09:16:55 +030041 BlockDriverState *bottom_bs = NULL;
Andrey Shinkevich16e09a22020-12-16 09:16:53 +030042 BDRVStateCOR *state = bs->opaque;
Andrey Shinkeviche4c8fdd2020-12-16 09:16:55 +030043 /* Find a bottom node name, if any */
44 const char *bottom_node = qdict_get_try_str(options, "bottom");
Vladimir Sementsov-Ogievskiy83930782022-07-26 23:11:21 +030045 int ret;
Andrey Shinkevich16e09a22020-12-16 09:16:53 +030046
Vladimir Sementsov-Ogievskiy83930782022-07-26 23:11:21 +030047 ret = bdrv_open_file_child(NULL, options, "file", bs, errp);
48 if (ret < 0) {
49 return ret;
Max Reitz6c6f24f2018-04-21 15:29:21 +020050 }
51
Andrey Shinkeviche2754582020-12-16 09:16:58 +030052 bs->supported_read_flags = BDRV_REQ_PREFETCH;
53
Max Reitz228345b2018-04-21 15:29:26 +020054 bs->supported_write_flags = BDRV_REQ_WRITE_UNCHANGED |
Kevin Wolf80f5c332019-03-22 13:42:39 +010055 (BDRV_REQ_FUA & bs->file->bs->supported_write_flags);
Max Reitz6c6f24f2018-04-21 15:29:21 +020056
Max Reitz228345b2018-04-21 15:29:26 +020057 bs->supported_zero_flags = BDRV_REQ_WRITE_UNCHANGED |
Kevin Wolf80f5c332019-03-22 13:42:39 +010058 ((BDRV_REQ_FUA | BDRV_REQ_MAY_UNMAP | BDRV_REQ_NO_FALLBACK) &
59 bs->file->bs->supported_zero_flags);
Max Reitz6c6f24f2018-04-21 15:29:21 +020060
Andrey Shinkeviche4c8fdd2020-12-16 09:16:55 +030061 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 Shinkeviche4c8fdd2020-12-16 09:16:55 +030091 state->bottom_bs = bottom_bs;
Andrey Shinkevich16e09a22020-12-16 09:16:53 +030092
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 Reitz6c6f24f2018-04-21 15:29:21 +020098 return 0;
99}
100
101
Max Reitz6c6f24f2018-04-21 15:29:21 +0200102#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
107static void cor_child_perm(BlockDriverState *bs, BdrvChild *c,
Max Reitzbf8e9252020-05-13 13:05:16 +0200108 BdrvChildRole role,
Max Reitz6c6f24f2018-04-21 15:29:21 +0200109 BlockReopenQueue *reopen_queue,
110 uint64_t perm, uint64_t shared,
111 uint64_t *nperm, uint64_t *nshared)
112{
Kevin Wolf2b23f282019-07-29 12:45:14 +0200113 *nperm = perm & PERM_PASSTHROUGH;
114 *nshared = (shared & PERM_PASSTHROUGH) | PERM_UNCHANGED;
Max Reitz6c6f24f2018-04-21 15:29:21 +0200115
Kevin Wolf2b23f282019-07-29 12:45:14 +0200116 /* 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 Reitz6c6f24f2018-04-21 15:29:21 +0200121}
122
123
Emanuele Giuseppe Espositoc86422c2023-01-13 21:42:04 +0100124static int64_t coroutine_fn cor_co_getlength(BlockDriverState *bs)
Max Reitz6c6f24f2018-04-21 15:29:21 +0200125{
Emanuele Giuseppe Espositoc86422c2023-01-13 21:42:04 +0100126 return bdrv_co_getlength(bs->file->bs);
Max Reitz6c6f24f2018-04-21 15:29:21 +0200127}
128
129
Andrey Shinkevich1252e032020-12-16 09:16:51 +0300130static int coroutine_fn cor_co_preadv_part(BlockDriverState *bs,
Vladimir Sementsov-Ogievskiyf7ef38d2021-09-03 13:27:59 +0300131 int64_t offset, int64_t bytes,
Andrey Shinkevich1252e032020-12-16 09:16:51 +0300132 QEMUIOVector *qiov,
133 size_t qiov_offset,
Vladimir Sementsov-Ogievskiyf7ef38d2021-09-03 13:27:59 +0300134 BdrvRequestFlags flags)
Max Reitz6c6f24f2018-04-21 15:29:21 +0200135{
Andrey Shinkeviche4c8fdd2020-12-16 09:16:55 +0300136 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 Shinkeviche2754582020-12-16 09:16:58 +0300164 /* 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 Shinkeviche4c8fdd2020-12-16 09:16:55 +0300172 }
173
174 offset += n;
175 qiov_offset += n;
176 bytes -= n;
177 }
178
179 return 0;
Max Reitz6c6f24f2018-04-21 15:29:21 +0200180}
181
182
Andrey Shinkevich1252e032020-12-16 09:16:51 +0300183static int coroutine_fn cor_co_pwritev_part(BlockDriverState *bs,
Vladimir Sementsov-Ogievskiye75abed2021-09-03 13:28:00 +0300184 int64_t offset,
185 int64_t bytes,
Andrey Shinkevich1252e032020-12-16 09:16:51 +0300186 QEMUIOVector *qiov,
Vladimir Sementsov-Ogievskiye75abed2021-09-03 13:28:00 +0300187 size_t qiov_offset,
188 BdrvRequestFlags flags)
Max Reitz6c6f24f2018-04-21 15:29:21 +0200189{
Andrey Shinkevich1252e032020-12-16 09:16:51 +0300190 return bdrv_co_pwritev_part(bs->file, offset, bytes, qiov, qiov_offset,
191 flags);
Max Reitz6c6f24f2018-04-21 15:29:21 +0200192}
193
194
195static int coroutine_fn cor_co_pwrite_zeroes(BlockDriverState *bs,
Vladimir Sementsov-Ogievskiyf34b2bc2021-09-03 13:28:03 +0300196 int64_t offset, int64_t bytes,
Max Reitz6c6f24f2018-04-21 15:29:21 +0200197 BdrvRequestFlags flags)
198{
199 return bdrv_co_pwrite_zeroes(bs->file, offset, bytes, flags);
200}
201
202
203static int coroutine_fn cor_co_pdiscard(BlockDriverState *bs,
Vladimir Sementsov-Ogievskiy0c802282021-09-03 13:28:06 +0300204 int64_t offset, int64_t bytes)
Max Reitz6c6f24f2018-04-21 15:29:21 +0200205{
Fam Zheng0b9fd3f2018-07-10 14:31:17 +0800206 return bdrv_co_pdiscard(bs->file, offset, bytes);
Max Reitz6c6f24f2018-04-21 15:29:21 +0200207}
208
209
Max Reitz4935e8b2019-11-28 16:12:36 +0100210static int coroutine_fn cor_co_pwritev_compressed(BlockDriverState *bs,
Vladimir Sementsov-Ogievskiye75abed2021-09-03 13:28:00 +0300211 int64_t offset,
212 int64_t bytes,
Max Reitz4935e8b2019-11-28 16:12:36 +0100213 QEMUIOVector *qiov)
214{
215 return bdrv_co_pwritev(bs->file, offset, bytes, qiov,
216 BDRV_REQ_WRITE_COMPRESSED);
217}
218
219
Emanuele Giuseppe Esposito2531b392023-01-13 21:42:09 +0100220static void coroutine_fn cor_co_eject(BlockDriverState *bs, bool eject_flag)
Max Reitz6c6f24f2018-04-21 15:29:21 +0200221{
Emanuele Giuseppe Esposito2531b392023-01-13 21:42:09 +0100222 bdrv_co_eject(bs->file->bs, eject_flag);
Max Reitz6c6f24f2018-04-21 15:29:21 +0200223}
224
225
Emanuele Giuseppe Esposito2c752612023-01-13 21:42:10 +0100226static void coroutine_fn cor_co_lock_medium(BlockDriverState *bs, bool locked)
Max Reitz6c6f24f2018-04-21 15:29:21 +0200227{
Emanuele Giuseppe Esposito2c752612023-01-13 21:42:10 +0100228 bdrv_co_lock_medium(bs->file->bs, locked);
Max Reitz6c6f24f2018-04-21 15:29:21 +0200229}
230
231
Andrey Shinkeviche4c8fdd2020-12-16 09:16:55 +0300232static 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 Garcia782b9d02019-03-18 17:48:01 +0200245static BlockDriver bdrv_copy_on_read = {
Max Reitz6c6f24f2018-04-21 15:29:21 +0200246 .format_name = "copy-on-read",
Andrey Shinkevich16e09a22020-12-16 09:16:53 +0300247 .instance_size = sizeof(BDRVStateCOR),
Max Reitz6c6f24f2018-04-21 15:29:21 +0200248
249 .bdrv_open = cor_open,
Andrey Shinkeviche4c8fdd2020-12-16 09:16:55 +0300250 .bdrv_close = cor_close,
Max Reitz6c6f24f2018-04-21 15:29:21 +0200251 .bdrv_child_perm = cor_child_perm,
252
Emanuele Giuseppe Espositoc86422c2023-01-13 21:42:04 +0100253 .bdrv_co_getlength = cor_co_getlength,
Max Reitz6c6f24f2018-04-21 15:29:21 +0200254
Andrey Shinkevich1252e032020-12-16 09:16:51 +0300255 .bdrv_co_preadv_part = cor_co_preadv_part,
256 .bdrv_co_pwritev_part = cor_co_pwritev_part,
Max Reitz6c6f24f2018-04-21 15:29:21 +0200257 .bdrv_co_pwrite_zeroes = cor_co_pwrite_zeroes,
258 .bdrv_co_pdiscard = cor_co_pdiscard,
Max Reitz4935e8b2019-11-28 16:12:36 +0100259 .bdrv_co_pwritev_compressed = cor_co_pwritev_compressed,
Max Reitz6c6f24f2018-04-21 15:29:21 +0200260
Emanuele Giuseppe Esposito2531b392023-01-13 21:42:09 +0100261 .bdrv_co_eject = cor_co_eject,
Emanuele Giuseppe Esposito2c752612023-01-13 21:42:10 +0100262 .bdrv_co_lock_medium = cor_co_lock_medium,
Max Reitz6c6f24f2018-04-21 15:29:21 +0200263
Max Reitz6c6f24f2018-04-21 15:29:21 +0200264 .has_variable_length = true,
265 .is_filter = true,
266};
267
Andrey Shinkevich16e09a22020-12-16 09:16:53 +0300268
269void bdrv_cor_filter_drop(BlockDriverState *cor_filter_bs)
270{
Andrey Shinkevich16e09a22020-12-16 09:16:53 +0300271 BDRVStateCOR *s = cor_filter_bs->opaque;
272
Andrey Shinkeviche4c8fdd2020-12-16 09:16:55 +0300273 /* 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-Ogievskiybcc85842021-05-06 22:41:43 +0300278 bdrv_drop_filter(cor_filter_bs, &error_abort);
Andrey Shinkevich16e09a22020-12-16 09:16:53 +0300279 bdrv_unref(cor_filter_bs);
280}
281
282
Max Reitz6c6f24f2018-04-21 15:29:21 +0200283static void bdrv_copy_on_read_init(void)
284{
285 bdrv_register(&bdrv_copy_on_read);
286}
287
288block_init(bdrv_copy_on_read_init);