Merge tag 'pull-loongarch-20241016' of https://gitlab.com/gaosong/qemu into staging
[qemu/armbru.git] / block / copy-on-read.c
blobc36f253d169a954496931f1c43e9a0218137c88b
1 /*
2 * Copy-on-read filter block driver
4 * Copyright (c) 2018 Red Hat, Inc.
6 * Author:
7 * Max Reitz <mreitz@redhat.com>
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.
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.
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/>.
23 #include "qemu/osdep.h"
24 #include "block/block-io.h"
25 #include "block/block_int.h"
26 #include "qemu/module.h"
27 #include "qapi/error.h"
28 #include "qapi/qmp/qdict.h"
29 #include "block/copy-on-read.h"
32 typedef struct BDRVStateCOR {
33 BlockDriverState *bottom_bs;
34 bool chain_frozen;
35 } BDRVStateCOR;
38 static int GRAPH_UNLOCKED
39 cor_open(BlockDriverState *bs, QDict *options, int flags, Error **errp)
41 BlockDriverState *bottom_bs = NULL;
42 BDRVStateCOR *state = bs->opaque;
43 /* Find a bottom node name, if any */
44 const char *bottom_node = qdict_get_try_str(options, "bottom");
45 int ret;
47 GLOBAL_STATE_CODE();
49 ret = bdrv_open_file_child(NULL, options, "file", bs, errp);
50 if (ret < 0) {
51 return ret;
54 GRAPH_RDLOCK_GUARD_MAINLOOP();
56 bs->supported_read_flags = BDRV_REQ_PREFETCH;
58 bs->supported_write_flags = BDRV_REQ_WRITE_UNCHANGED |
59 (BDRV_REQ_FUA & bs->file->bs->supported_write_flags);
61 bs->supported_zero_flags = BDRV_REQ_WRITE_UNCHANGED |
62 ((BDRV_REQ_FUA | BDRV_REQ_MAY_UNMAP | BDRV_REQ_NO_FALLBACK) &
63 bs->file->bs->supported_zero_flags);
65 if (bottom_node) {
66 bottom_bs = bdrv_find_node(bottom_node);
67 if (!bottom_bs) {
68 error_setg(errp, "Bottom node '%s' not found", bottom_node);
69 qdict_del(options, "bottom");
70 return -EINVAL;
72 qdict_del(options, "bottom");
74 if (!bottom_bs->drv) {
75 error_setg(errp, "Bottom node '%s' not opened", bottom_node);
76 return -EINVAL;
79 if (bottom_bs->drv->is_filter) {
80 error_setg(errp, "Bottom node '%s' is a filter", bottom_node);
81 return -EINVAL;
84 if (bdrv_freeze_backing_chain(bs, bottom_bs, errp) < 0) {
85 return -EINVAL;
87 state->chain_frozen = true;
90 * We do freeze the chain, so it shouldn't be removed. Still, storing a
91 * pointer worth bdrv_ref().
93 bdrv_ref(bottom_bs);
95 state->bottom_bs = bottom_bs;
98 * We don't need to call bdrv_child_refresh_perms() now as the permissions
99 * will be updated later when the filter node gets its parent.
102 return 0;
106 #define PERM_PASSTHROUGH (BLK_PERM_CONSISTENT_READ \
107 | BLK_PERM_WRITE \
108 | BLK_PERM_RESIZE)
109 #define PERM_UNCHANGED (BLK_PERM_ALL & ~PERM_PASSTHROUGH)
111 static void cor_child_perm(BlockDriverState *bs, BdrvChild *c,
112 BdrvChildRole role,
113 BlockReopenQueue *reopen_queue,
114 uint64_t perm, uint64_t shared,
115 uint64_t *nperm, uint64_t *nshared)
117 *nperm = perm & PERM_PASSTHROUGH;
118 *nshared = (shared & PERM_PASSTHROUGH) | PERM_UNCHANGED;
120 /* We must not request write permissions for an inactive node, the child
121 * cannot provide it. */
122 if (!(bs->open_flags & BDRV_O_INACTIVE)) {
123 *nperm |= BLK_PERM_WRITE_UNCHANGED;
128 static int64_t coroutine_fn GRAPH_RDLOCK cor_co_getlength(BlockDriverState *bs)
130 return bdrv_co_getlength(bs->file->bs);
134 static int coroutine_fn GRAPH_RDLOCK
135 cor_co_preadv_part(BlockDriverState *bs, int64_t offset, int64_t bytes,
136 QEMUIOVector *qiov, size_t qiov_offset,
137 BdrvRequestFlags flags)
139 int64_t n;
140 int local_flags;
141 int ret;
142 BDRVStateCOR *state = bs->opaque;
144 if (!state->bottom_bs) {
145 return bdrv_co_preadv_part(bs->file, offset, bytes, qiov, qiov_offset,
146 flags | BDRV_REQ_COPY_ON_READ);
149 while (bytes) {
150 local_flags = flags;
152 /* In case of failure, try to copy-on-read anyway */
153 ret = bdrv_co_is_allocated(bs->file->bs, offset, bytes, &n);
154 if (ret <= 0) {
155 ret = bdrv_co_is_allocated_above(bdrv_backing_chain_next(bs->file->bs),
156 state->bottom_bs, true, offset,
157 n, &n);
158 if (ret > 0 || ret < 0) {
159 local_flags |= BDRV_REQ_COPY_ON_READ;
161 /* Finish earlier if the end of a backing file has been reached */
162 if (n == 0) {
163 break;
167 /* Skip if neither read nor write are needed */
168 if ((local_flags & (BDRV_REQ_PREFETCH | BDRV_REQ_COPY_ON_READ)) !=
169 BDRV_REQ_PREFETCH) {
170 ret = bdrv_co_preadv_part(bs->file, offset, n, qiov, qiov_offset,
171 local_flags);
172 if (ret < 0) {
173 return ret;
177 offset += n;
178 qiov_offset += n;
179 bytes -= n;
182 return 0;
186 static int coroutine_fn GRAPH_RDLOCK
187 cor_co_pwritev_part(BlockDriverState *bs, int64_t offset, int64_t bytes,
188 QEMUIOVector *qiov, size_t qiov_offset,
189 BdrvRequestFlags flags)
191 return bdrv_co_pwritev_part(bs->file, offset, bytes, qiov, qiov_offset,
192 flags);
196 static int coroutine_fn GRAPH_RDLOCK
197 cor_co_pwrite_zeroes(BlockDriverState *bs, int64_t offset, int64_t bytes,
198 BdrvRequestFlags flags)
200 return bdrv_co_pwrite_zeroes(bs->file, offset, bytes, flags);
204 static int coroutine_fn GRAPH_RDLOCK
205 cor_co_pdiscard(BlockDriverState *bs, int64_t offset, int64_t bytes)
207 return bdrv_co_pdiscard(bs->file, offset, bytes);
211 static int coroutine_fn GRAPH_RDLOCK
212 cor_co_pwritev_compressed(BlockDriverState *bs, int64_t offset, int64_t bytes,
213 QEMUIOVector *qiov)
215 return bdrv_co_pwritev(bs->file, offset, bytes, qiov,
216 BDRV_REQ_WRITE_COMPRESSED);
220 static void coroutine_fn GRAPH_RDLOCK
221 cor_co_eject(BlockDriverState *bs, bool eject_flag)
223 bdrv_co_eject(bs->file->bs, eject_flag);
227 static void coroutine_fn GRAPH_RDLOCK
228 cor_co_lock_medium(BlockDriverState *bs, bool locked)
230 bdrv_co_lock_medium(bs->file->bs, locked);
234 static void GRAPH_UNLOCKED cor_close(BlockDriverState *bs)
236 BDRVStateCOR *s = bs->opaque;
238 GLOBAL_STATE_CODE();
240 if (s->chain_frozen) {
241 bdrv_graph_rdlock_main_loop();
242 s->chain_frozen = false;
243 bdrv_unfreeze_backing_chain(bs, s->bottom_bs);
244 bdrv_graph_rdunlock_main_loop();
247 bdrv_unref(s->bottom_bs);
251 static BlockDriver bdrv_copy_on_read = {
252 .format_name = "copy-on-read",
253 .instance_size = sizeof(BDRVStateCOR),
255 .bdrv_open = cor_open,
256 .bdrv_close = cor_close,
257 .bdrv_child_perm = cor_child_perm,
259 .bdrv_co_getlength = cor_co_getlength,
261 .bdrv_co_preadv_part = cor_co_preadv_part,
262 .bdrv_co_pwritev_part = cor_co_pwritev_part,
263 .bdrv_co_pwrite_zeroes = cor_co_pwrite_zeroes,
264 .bdrv_co_pdiscard = cor_co_pdiscard,
265 .bdrv_co_pwritev_compressed = cor_co_pwritev_compressed,
267 .bdrv_co_eject = cor_co_eject,
268 .bdrv_co_lock_medium = cor_co_lock_medium,
270 .is_filter = true,
274 void no_coroutine_fn bdrv_cor_filter_drop(BlockDriverState *cor_filter_bs)
276 BDRVStateCOR *s = cor_filter_bs->opaque;
278 GLOBAL_STATE_CODE();
280 /* unfreeze, as otherwise bdrv_replace_node() will fail */
281 if (s->chain_frozen) {
282 GRAPH_RDLOCK_GUARD_MAINLOOP();
283 s->chain_frozen = false;
284 bdrv_unfreeze_backing_chain(cor_filter_bs, s->bottom_bs);
286 bdrv_drop_filter(cor_filter_bs, &error_abort);
287 bdrv_unref(cor_filter_bs);
291 static void bdrv_copy_on_read_init(void)
293 bdrv_register(&bdrv_copy_on_read);
296 block_init(bdrv_copy_on_read_init);