]>
Commit | Line | Data |
---|---|---|
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 | ||
32 | typedef struct BDRVStateCOR { | |
e4c8fddd AS |
33 | BlockDriverState *bottom_bs; |
34 | bool chain_frozen; | |
16e09a21 | 35 | } BDRVStateCOR; |
6c6f24fd HR |
36 | |
37 | ||
9275fc72 KW |
38 | static int GRAPH_UNLOCKED |
39 | cor_open(BlockDriverState *bs, QDict *options, int flags, Error **errp) | |
6c6f24fd | 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 | |
9275fc72 KW |
47 | GLOBAL_STATE_CODE(); |
48 | ||
83930780 VSO |
49 | ret = bdrv_open_file_child(NULL, options, "file", bs, errp); |
50 | if (ret < 0) { | |
51 | return ret; | |
6c6f24fd HR |
52 | } |
53 | ||
a4b740db KW |
54 | GRAPH_RDLOCK_GUARD_MAINLOOP(); |
55 | ||
e275458b AS |
56 | bs->supported_read_flags = BDRV_REQ_PREFETCH; |
57 | ||
228345bf | 58 | bs->supported_write_flags = BDRV_REQ_WRITE_UNCHANGED | |
80f5c33f | 59 | (BDRV_REQ_FUA & bs->file->bs->supported_write_flags); |
6c6f24fd | 60 | |
228345bf | 61 | bs->supported_zero_flags = BDRV_REQ_WRITE_UNCHANGED | |
80f5c33f KW |
62 | ((BDRV_REQ_FUA | BDRV_REQ_MAY_UNMAP | BDRV_REQ_NO_FALLBACK) & |
63 | bs->file->bs->supported_zero_flags); | |
6c6f24fd | 64 | |
e4c8fddd AS |
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; | |
71 | } | |
72 | qdict_del(options, "bottom"); | |
73 | ||
74 | if (!bottom_bs->drv) { | |
75 | error_setg(errp, "Bottom node '%s' not opened", bottom_node); | |
76 | return -EINVAL; | |
77 | } | |
78 | ||
79 | if (bottom_bs->drv->is_filter) { | |
80 | error_setg(errp, "Bottom node '%s' is a filter", bottom_node); | |
81 | return -EINVAL; | |
82 | } | |
83 | ||
84 | if (bdrv_freeze_backing_chain(bs, bottom_bs, errp) < 0) { | |
85 | return -EINVAL; | |
86 | } | |
87 | state->chain_frozen = true; | |
88 | ||
89 | /* | |
90 | * We do freeze the chain, so it shouldn't be removed. Still, storing a | |
91 | * pointer worth bdrv_ref(). | |
92 | */ | |
93 | bdrv_ref(bottom_bs); | |
94 | } | |
e4c8fddd | 95 | state->bottom_bs = bottom_bs; |
16e09a21 AS |
96 | |
97 | /* | |
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. | |
100 | */ | |
101 | ||
6c6f24fd HR |
102 | return 0; |
103 | } | |
104 | ||
105 | ||
6c6f24fd HR |
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) | |
110 | ||
111 | static void cor_child_perm(BlockDriverState *bs, BdrvChild *c, | |
bf8e925e | 112 | BdrvChildRole role, |
6c6f24fd HR |
113 | BlockReopenQueue *reopen_queue, |
114 | uint64_t perm, uint64_t shared, | |
115 | uint64_t *nperm, uint64_t *nshared) | |
116 | { | |
2b23f286 KW |
117 | *nperm = perm & PERM_PASSTHROUGH; |
118 | *nshared = (shared & PERM_PASSTHROUGH) | PERM_UNCHANGED; | |
6c6f24fd | 119 | |
2b23f286 KW |
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; | |
124 | } | |
6c6f24fd HR |
125 | } |
126 | ||
127 | ||
8ab8140a | 128 | static int64_t coroutine_fn GRAPH_RDLOCK cor_co_getlength(BlockDriverState *bs) |
6c6f24fd | 129 | { |
c86422c5 | 130 | return bdrv_co_getlength(bs->file->bs); |
6c6f24fd HR |
131 | } |
132 | ||
133 | ||
b9b10c35 KW |
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) | |
6c6f24fd | 138 | { |
e4c8fddd AS |
139 | int64_t n; |
140 | int local_flags; | |
141 | int ret; | |
142 | BDRVStateCOR *state = bs->opaque; | |
143 | ||
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); | |
147 | } | |
148 | ||
149 | while (bytes) { | |
150 | local_flags = flags; | |
151 | ||
152 | /* In case of failure, try to copy-on-read anyway */ | |
cc323997 | 153 | ret = bdrv_co_is_allocated(bs->file->bs, offset, bytes, &n); |
e4c8fddd | 154 | if (ret <= 0) { |
cc323997 PB |
155 | ret = bdrv_co_is_allocated_above(bdrv_backing_chain_next(bs->file->bs), |
156 | state->bottom_bs, true, offset, | |
157 | n, &n); | |
e4c8fddd AS |
158 | if (ret > 0 || ret < 0) { |
159 | local_flags |= BDRV_REQ_COPY_ON_READ; | |
160 | } | |
161 | /* Finish earlier if the end of a backing file has been reached */ | |
162 | if (n == 0) { | |
163 | break; | |
164 | } | |
165 | } | |
166 | ||
e275458b AS |
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; | |
174 | } | |
e4c8fddd AS |
175 | } |
176 | ||
177 | offset += n; | |
178 | qiov_offset += n; | |
179 | bytes -= n; | |
180 | } | |
181 | ||
182 | return 0; | |
6c6f24fd HR |
183 | } |
184 | ||
185 | ||
b9b10c35 KW |
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) | |
6c6f24fd | 190 | { |
1252e03b AS |
191 | return bdrv_co_pwritev_part(bs->file, offset, bytes, qiov, qiov_offset, |
192 | flags); | |
6c6f24fd HR |
193 | } |
194 | ||
195 | ||
abaf8b75 KW |
196 | static int coroutine_fn GRAPH_RDLOCK |
197 | cor_co_pwrite_zeroes(BlockDriverState *bs, int64_t offset, int64_t bytes, | |
198 | BdrvRequestFlags flags) | |
6c6f24fd HR |
199 | { |
200 | return bdrv_co_pwrite_zeroes(bs->file, offset, bytes, flags); | |
201 | } | |
202 | ||
203 | ||
9a5a1c62 EGE |
204 | static int coroutine_fn GRAPH_RDLOCK |
205 | cor_co_pdiscard(BlockDriverState *bs, int64_t offset, int64_t bytes) | |
6c6f24fd | 206 | { |
0b9fd3f4 | 207 | return bdrv_co_pdiscard(bs->file, offset, bytes); |
6c6f24fd HR |
208 | } |
209 | ||
210 | ||
b9b10c35 KW |
211 | static int coroutine_fn GRAPH_RDLOCK |
212 | cor_co_pwritev_compressed(BlockDriverState *bs, int64_t offset, int64_t bytes, | |
213 | QEMUIOVector *qiov) | |
4935e8be HR |
214 | { |
215 | return bdrv_co_pwritev(bs->file, offset, bytes, qiov, | |
216 | BDRV_REQ_WRITE_COMPRESSED); | |
217 | } | |
218 | ||
219 | ||
79a292e5 KW |
220 | static void coroutine_fn GRAPH_RDLOCK |
221 | cor_co_eject(BlockDriverState *bs, bool eject_flag) | |
6c6f24fd | 222 | { |
2531b390 | 223 | bdrv_co_eject(bs->file->bs, eject_flag); |
6c6f24fd HR |
224 | } |
225 | ||
226 | ||
79a292e5 KW |
227 | static void coroutine_fn GRAPH_RDLOCK |
228 | cor_co_lock_medium(BlockDriverState *bs, bool locked) | |
6c6f24fd | 229 | { |
2c75261c | 230 | bdrv_co_lock_medium(bs->file->bs, locked); |
6c6f24fd HR |
231 | } |
232 | ||
233 | ||
9275fc72 | 234 | static void GRAPH_UNLOCKED cor_close(BlockDriverState *bs) |
e4c8fddd AS |
235 | { |
236 | BDRVStateCOR *s = bs->opaque; | |
237 | ||
9275fc72 KW |
238 | GLOBAL_STATE_CODE(); |
239 | ||
e4c8fddd | 240 | if (s->chain_frozen) { |
9275fc72 | 241 | bdrv_graph_rdlock_main_loop(); |
e4c8fddd AS |
242 | s->chain_frozen = false; |
243 | bdrv_unfreeze_backing_chain(bs, s->bottom_bs); | |
9275fc72 | 244 | bdrv_graph_rdunlock_main_loop(); |
e4c8fddd AS |
245 | } |
246 | ||
247 | bdrv_unref(s->bottom_bs); | |
248 | } | |
249 | ||
250 | ||
782b9d06 | 251 | static BlockDriver bdrv_copy_on_read = { |
6c6f24fd | 252 | .format_name = "copy-on-read", |
16e09a21 | 253 | .instance_size = sizeof(BDRVStateCOR), |
6c6f24fd HR |
254 | |
255 | .bdrv_open = cor_open, | |
e4c8fddd | 256 | .bdrv_close = cor_close, |
6c6f24fd HR |
257 | .bdrv_child_perm = cor_child_perm, |
258 | ||
c86422c5 | 259 | .bdrv_co_getlength = cor_co_getlength, |
6c6f24fd | 260 | |
1252e03b AS |
261 | .bdrv_co_preadv_part = cor_co_preadv_part, |
262 | .bdrv_co_pwritev_part = cor_co_pwritev_part, | |
6c6f24fd HR |
263 | .bdrv_co_pwrite_zeroes = cor_co_pwrite_zeroes, |
264 | .bdrv_co_pdiscard = cor_co_pdiscard, | |
4935e8be | 265 | .bdrv_co_pwritev_compressed = cor_co_pwritev_compressed, |
6c6f24fd | 266 | |
2531b390 | 267 | .bdrv_co_eject = cor_co_eject, |
2c75261c | 268 | .bdrv_co_lock_medium = cor_co_lock_medium, |
6c6f24fd | 269 | |
6c6f24fd HR |
270 | .is_filter = true, |
271 | }; | |
272 | ||
16e09a21 | 273 | |
9275fc72 | 274 | void no_coroutine_fn bdrv_cor_filter_drop(BlockDriverState *cor_filter_bs) |
16e09a21 | 275 | { |
16e09a21 AS |
276 | BDRVStateCOR *s = cor_filter_bs->opaque; |
277 | ||
9275fc72 KW |
278 | GLOBAL_STATE_CODE(); |
279 | ||
e4c8fddd AS |
280 | /* unfreeze, as otherwise bdrv_replace_node() will fail */ |
281 | if (s->chain_frozen) { | |
9275fc72 | 282 | GRAPH_RDLOCK_GUARD_MAINLOOP(); |
e4c8fddd AS |
283 | s->chain_frozen = false; |
284 | bdrv_unfreeze_backing_chain(cor_filter_bs, s->bottom_bs); | |
285 | } | |
bcc8584c | 286 | bdrv_drop_filter(cor_filter_bs, &error_abort); |
16e09a21 AS |
287 | bdrv_unref(cor_filter_bs); |
288 | } | |
289 | ||
290 | ||
6c6f24fd HR |
291 | static void bdrv_copy_on_read_init(void) |
292 | { | |
293 | bdrv_register(&bdrv_copy_on_read); | |
294 | } | |
295 | ||
296 | block_init(bdrv_copy_on_read_init); |