]> git.proxmox.com Git - mirror_qemu.git/blame - block/copy-on-read.c
block: switch to co_wrapper for bdrv_is_allocated_*
[mirror_qemu.git] / block / copy-on-read.c
CommitLineData
6c6f24fd
HR
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"
e2c1c34f 24#include "block/block-io.h"
6c6f24fd 25#include "block/block_int.h"
0b8fa32f 26#include "qemu/module.h"
16e09a21 27#include "qapi/error.h"
e4c8fddd 28#include "qapi/qmp/qdict.h"
16e09a21
AS
29#include "block/copy-on-read.h"
30
31
32typedef struct BDRVStateCOR {
e4c8fddd
AS
33 BlockDriverState *bottom_bs;
34 bool chain_frozen;
16e09a21 35} BDRVStateCOR;
6c6f24fd
HR
36
37
38static int cor_open(BlockDriverState *bs, QDict *options, int flags,
39 Error **errp)
40{
e4c8fddd 41 BlockDriverState *bottom_bs = NULL;
16e09a21 42 BDRVStateCOR *state = bs->opaque;
e4c8fddd
AS
43 /* Find a bottom node name, if any */
44 const char *bottom_node = qdict_get_try_str(options, "bottom");
83930780 45 int ret;
16e09a21 46
83930780
VSO
47 ret = bdrv_open_file_child(NULL, options, "file", bs, errp);
48 if (ret < 0) {
49 return ret;
6c6f24fd
HR
50 }
51
e275458b
AS
52 bs->supported_read_flags = BDRV_REQ_PREFETCH;
53
228345bf 54 bs->supported_write_flags = BDRV_REQ_WRITE_UNCHANGED |
80f5c33f 55 (BDRV_REQ_FUA & bs->file->bs->supported_write_flags);
6c6f24fd 56
228345bf 57 bs->supported_zero_flags = BDRV_REQ_WRITE_UNCHANGED |
80f5c33f
KW
58 ((BDRV_REQ_FUA | BDRV_REQ_MAY_UNMAP | BDRV_REQ_NO_FALLBACK) &
59 bs->file->bs->supported_zero_flags);
6c6f24fd 60
e4c8fddd
AS
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 }
e4c8fddd 91 state->bottom_bs = bottom_bs;
16e09a21
AS
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
6c6f24fd
HR
98 return 0;
99}
100
101
6c6f24fd
HR
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
107static void cor_child_perm(BlockDriverState *bs, BdrvChild *c,
bf8e925e 108 BdrvChildRole role,
6c6f24fd
HR
109 BlockReopenQueue *reopen_queue,
110 uint64_t perm, uint64_t shared,
111 uint64_t *nperm, uint64_t *nshared)
112{
2b23f286
KW
113 *nperm = perm & PERM_PASSTHROUGH;
114 *nshared = (shared & PERM_PASSTHROUGH) | PERM_UNCHANGED;
6c6f24fd 115
2b23f286
KW
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 }
6c6f24fd
HR
121}
122
123
8ab8140a 124static int64_t coroutine_fn GRAPH_RDLOCK cor_co_getlength(BlockDriverState *bs)
6c6f24fd 125{
c86422c5 126 return bdrv_co_getlength(bs->file->bs);
6c6f24fd
HR
127}
128
129
b9b10c35
KW
130static int coroutine_fn GRAPH_RDLOCK
131cor_co_preadv_part(BlockDriverState *bs, int64_t offset, int64_t bytes,
132 QEMUIOVector *qiov, size_t qiov_offset,
133 BdrvRequestFlags flags)
6c6f24fd 134{
e4c8fddd
AS
135 int64_t n;
136 int local_flags;
137 int ret;
138 BDRVStateCOR *state = bs->opaque;
139
140 if (!state->bottom_bs) {
141 return bdrv_co_preadv_part(bs->file, offset, bytes, qiov, qiov_offset,
142 flags | BDRV_REQ_COPY_ON_READ);
143 }
144
145 while (bytes) {
146 local_flags = flags;
147
148 /* In case of failure, try to copy-on-read anyway */
149 ret = bdrv_is_allocated(bs->file->bs, offset, bytes, &n);
150 if (ret <= 0) {
151 ret = bdrv_is_allocated_above(bdrv_backing_chain_next(bs->file->bs),
152 state->bottom_bs, true, offset,
153 n, &n);
154 if (ret > 0 || ret < 0) {
155 local_flags |= BDRV_REQ_COPY_ON_READ;
156 }
157 /* Finish earlier if the end of a backing file has been reached */
158 if (n == 0) {
159 break;
160 }
161 }
162
e275458b
AS
163 /* Skip if neither read nor write are needed */
164 if ((local_flags & (BDRV_REQ_PREFETCH | BDRV_REQ_COPY_ON_READ)) !=
165 BDRV_REQ_PREFETCH) {
166 ret = bdrv_co_preadv_part(bs->file, offset, n, qiov, qiov_offset,
167 local_flags);
168 if (ret < 0) {
169 return ret;
170 }
e4c8fddd
AS
171 }
172
173 offset += n;
174 qiov_offset += n;
175 bytes -= n;
176 }
177
178 return 0;
6c6f24fd
HR
179}
180
181
b9b10c35
KW
182static int coroutine_fn GRAPH_RDLOCK
183cor_co_pwritev_part(BlockDriverState *bs, int64_t offset, int64_t bytes,
184 QEMUIOVector *qiov, size_t qiov_offset,
185 BdrvRequestFlags flags)
6c6f24fd 186{
1252e03b
AS
187 return bdrv_co_pwritev_part(bs->file, offset, bytes, qiov, qiov_offset,
188 flags);
6c6f24fd
HR
189}
190
191
abaf8b75
KW
192static int coroutine_fn GRAPH_RDLOCK
193cor_co_pwrite_zeroes(BlockDriverState *bs, int64_t offset, int64_t bytes,
194 BdrvRequestFlags flags)
6c6f24fd
HR
195{
196 return bdrv_co_pwrite_zeroes(bs->file, offset, bytes, flags);
197}
198
199
9a5a1c62
EGE
200static int coroutine_fn GRAPH_RDLOCK
201cor_co_pdiscard(BlockDriverState *bs, int64_t offset, int64_t bytes)
6c6f24fd 202{
0b9fd3f4 203 return bdrv_co_pdiscard(bs->file, offset, bytes);
6c6f24fd
HR
204}
205
206
b9b10c35
KW
207static int coroutine_fn GRAPH_RDLOCK
208cor_co_pwritev_compressed(BlockDriverState *bs, int64_t offset, int64_t bytes,
209 QEMUIOVector *qiov)
4935e8be
HR
210{
211 return bdrv_co_pwritev(bs->file, offset, bytes, qiov,
212 BDRV_REQ_WRITE_COMPRESSED);
213}
214
215
79a292e5
KW
216static void coroutine_fn GRAPH_RDLOCK
217cor_co_eject(BlockDriverState *bs, bool eject_flag)
6c6f24fd 218{
2531b390 219 bdrv_co_eject(bs->file->bs, eject_flag);
6c6f24fd
HR
220}
221
222
79a292e5
KW
223static void coroutine_fn GRAPH_RDLOCK
224cor_co_lock_medium(BlockDriverState *bs, bool locked)
6c6f24fd 225{
2c75261c 226 bdrv_co_lock_medium(bs->file->bs, locked);
6c6f24fd
HR
227}
228
229
e4c8fddd
AS
230static void cor_close(BlockDriverState *bs)
231{
232 BDRVStateCOR *s = bs->opaque;
233
234 if (s->chain_frozen) {
235 s->chain_frozen = false;
236 bdrv_unfreeze_backing_chain(bs, s->bottom_bs);
237 }
238
239 bdrv_unref(s->bottom_bs);
240}
241
242
782b9d06 243static BlockDriver bdrv_copy_on_read = {
6c6f24fd 244 .format_name = "copy-on-read",
16e09a21 245 .instance_size = sizeof(BDRVStateCOR),
6c6f24fd
HR
246
247 .bdrv_open = cor_open,
e4c8fddd 248 .bdrv_close = cor_close,
6c6f24fd
HR
249 .bdrv_child_perm = cor_child_perm,
250
c86422c5 251 .bdrv_co_getlength = cor_co_getlength,
6c6f24fd 252
1252e03b
AS
253 .bdrv_co_preadv_part = cor_co_preadv_part,
254 .bdrv_co_pwritev_part = cor_co_pwritev_part,
6c6f24fd
HR
255 .bdrv_co_pwrite_zeroes = cor_co_pwrite_zeroes,
256 .bdrv_co_pdiscard = cor_co_pdiscard,
4935e8be 257 .bdrv_co_pwritev_compressed = cor_co_pwritev_compressed,
6c6f24fd 258
2531b390 259 .bdrv_co_eject = cor_co_eject,
2c75261c 260 .bdrv_co_lock_medium = cor_co_lock_medium,
6c6f24fd 261
6c6f24fd
HR
262 .is_filter = true,
263};
264
16e09a21
AS
265
266void bdrv_cor_filter_drop(BlockDriverState *cor_filter_bs)
267{
16e09a21
AS
268 BDRVStateCOR *s = cor_filter_bs->opaque;
269
e4c8fddd
AS
270 /* unfreeze, as otherwise bdrv_replace_node() will fail */
271 if (s->chain_frozen) {
272 s->chain_frozen = false;
273 bdrv_unfreeze_backing_chain(cor_filter_bs, s->bottom_bs);
274 }
bcc8584c 275 bdrv_drop_filter(cor_filter_bs, &error_abort);
16e09a21
AS
276 bdrv_unref(cor_filter_bs);
277}
278
279
6c6f24fd
HR
280static void bdrv_copy_on_read_init(void)
281{
282 bdrv_register(&bdrv_copy_on_read);
283}
284
285block_init(bdrv_copy_on_read_init);