]>
Commit | Line | Data |
---|---|---|
fc01f7e7 FB |
1 | /* |
2 | * QEMU System Emulator block driver | |
5fafdf24 | 3 | * |
fc01f7e7 | 4 | * Copyright (c) 2003 Fabrice Bellard |
5fafdf24 | 5 | * |
fc01f7e7 FB |
6 | * Permission is hereby granted, free of charge, to any person obtaining a copy |
7 | * of this software and associated documentation files (the "Software"), to deal | |
8 | * in the Software without restriction, including without limitation the rights | |
9 | * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell | |
10 | * copies of the Software, and to permit persons to whom the Software is | |
11 | * furnished to do so, subject to the following conditions: | |
12 | * | |
13 | * The above copyright notice and this permission notice shall be included in | |
14 | * all copies or substantial portions of the Software. | |
15 | * | |
16 | * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR | |
17 | * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, | |
18 | * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL | |
19 | * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER | |
20 | * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, | |
21 | * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN | |
22 | * THE SOFTWARE. | |
23 | */ | |
e688df6b | 24 | |
d38ea87a | 25 | #include "qemu/osdep.h" |
0ab8ed18 | 26 | #include "block/trace.h" |
737e150e PB |
27 | #include "block/block_int.h" |
28 | #include "block/blockjob.h" | |
0c9b70d5 | 29 | #include "block/fuse.h" |
cd7fca95 | 30 | #include "block/nbd.h" |
609f45ea | 31 | #include "block/qdict.h" |
d49b6836 | 32 | #include "qemu/error-report.h" |
5e5733e5 | 33 | #include "block/module_block.h" |
db725815 | 34 | #include "qemu/main-loop.h" |
1de7afc9 | 35 | #include "qemu/module.h" |
e688df6b | 36 | #include "qapi/error.h" |
452fcdbc | 37 | #include "qapi/qmp/qdict.h" |
7b1b5d19 | 38 | #include "qapi/qmp/qjson.h" |
e59a0cf1 | 39 | #include "qapi/qmp/qnull.h" |
fc81fa1e | 40 | #include "qapi/qmp/qstring.h" |
e1d74bc6 KW |
41 | #include "qapi/qobject-output-visitor.h" |
42 | #include "qapi/qapi-visit-block-core.h" | |
bfb197e0 | 43 | #include "sysemu/block-backend.h" |
9c17d615 | 44 | #include "sysemu/sysemu.h" |
1de7afc9 | 45 | #include "qemu/notify.h" |
922a01a0 | 46 | #include "qemu/option.h" |
10817bf0 | 47 | #include "qemu/coroutine.h" |
c13163fb | 48 | #include "block/qapi.h" |
1de7afc9 | 49 | #include "qemu/timer.h" |
f348b6d1 VB |
50 | #include "qemu/cutils.h" |
51 | #include "qemu/id.h" | |
b0defa83 | 52 | #include "qemu/transactions.h" |
21c2283e | 53 | #include "block/coroutines.h" |
fc01f7e7 | 54 | |
71e72a19 | 55 | #ifdef CONFIG_BSD |
7674e7bf | 56 | #include <sys/ioctl.h> |
72cf2d4f | 57 | #include <sys/queue.h> |
c5e97233 | 58 | #ifndef __DragonFly__ |
7674e7bf FB |
59 | #include <sys/disk.h> |
60 | #endif | |
c5e97233 | 61 | #endif |
7674e7bf | 62 | |
49dc768d AL |
63 | #ifdef _WIN32 |
64 | #include <windows.h> | |
65 | #endif | |
66 | ||
1c9805a3 SH |
67 | #define NOT_DONE 0x7fffffff /* used while emulated sync operation in progress */ |
68 | ||
dc364f4c BC |
69 | static QTAILQ_HEAD(, BlockDriverState) graph_bdrv_states = |
70 | QTAILQ_HEAD_INITIALIZER(graph_bdrv_states); | |
71 | ||
2c1d04e0 HR |
72 | static QTAILQ_HEAD(, BlockDriverState) all_bdrv_states = |
73 | QTAILQ_HEAD_INITIALIZER(all_bdrv_states); | |
74 | ||
8a22f02a SH |
75 | static QLIST_HEAD(, BlockDriver) bdrv_drivers = |
76 | QLIST_HEAD_INITIALIZER(bdrv_drivers); | |
ea2384d3 | 77 | |
5b363937 HR |
78 | static BlockDriverState *bdrv_open_inherit(const char *filename, |
79 | const char *reference, | |
80 | QDict *options, int flags, | |
81 | BlockDriverState *parent, | |
bd86fb99 | 82 | const BdrvChildClass *child_class, |
272c02ea | 83 | BdrvChildRole child_role, |
5b363937 | 84 | Error **errp); |
f3930ed0 | 85 | |
53e96d1e VSO |
86 | static int bdrv_reopen_prepare(BDRVReopenState *reopen_state, BlockReopenQueue |
87 | *queue, Error **errp); | |
88 | static void bdrv_reopen_commit(BDRVReopenState *reopen_state); | |
89 | static void bdrv_reopen_abort(BDRVReopenState *reopen_state); | |
90 | ||
eb852011 MA |
91 | /* If non-zero, use only whitelisted block drivers */ |
92 | static int use_bdrv_whitelist; | |
93 | ||
9e0b22f4 SH |
94 | #ifdef _WIN32 |
95 | static int is_windows_drive_prefix(const char *filename) | |
96 | { | |
97 | return (((filename[0] >= 'a' && filename[0] <= 'z') || | |
98 | (filename[0] >= 'A' && filename[0] <= 'Z')) && | |
99 | filename[1] == ':'); | |
100 | } | |
101 | ||
102 | int is_windows_drive(const char *filename) | |
103 | { | |
104 | if (is_windows_drive_prefix(filename) && | |
105 | filename[2] == '\0') | |
106 | return 1; | |
107 | if (strstart(filename, "\\\\.\\", NULL) || | |
108 | strstart(filename, "//./", NULL)) | |
109 | return 1; | |
110 | return 0; | |
111 | } | |
112 | #endif | |
113 | ||
339064d5 KW |
114 | size_t bdrv_opt_mem_align(BlockDriverState *bs) |
115 | { | |
116 | if (!bs || !bs->drv) { | |
459b4e66 | 117 | /* page size or 4k (hdd sector size) should be on the safe side */ |
038adc2f | 118 | return MAX(4096, qemu_real_host_page_size); |
339064d5 KW |
119 | } |
120 | ||
121 | return bs->bl.opt_mem_alignment; | |
122 | } | |
123 | ||
4196d2f0 DL |
124 | size_t bdrv_min_mem_align(BlockDriverState *bs) |
125 | { | |
126 | if (!bs || !bs->drv) { | |
459b4e66 | 127 | /* page size or 4k (hdd sector size) should be on the safe side */ |
038adc2f | 128 | return MAX(4096, qemu_real_host_page_size); |
4196d2f0 DL |
129 | } |
130 | ||
131 | return bs->bl.min_mem_alignment; | |
132 | } | |
133 | ||
9e0b22f4 | 134 | /* check if the path starts with "<protocol>:" */ |
5c98415b | 135 | int path_has_protocol(const char *path) |
9e0b22f4 | 136 | { |
947995c0 PB |
137 | const char *p; |
138 | ||
9e0b22f4 SH |
139 | #ifdef _WIN32 |
140 | if (is_windows_drive(path) || | |
141 | is_windows_drive_prefix(path)) { | |
142 | return 0; | |
143 | } | |
947995c0 PB |
144 | p = path + strcspn(path, ":/\\"); |
145 | #else | |
146 | p = path + strcspn(path, ":/"); | |
9e0b22f4 SH |
147 | #endif |
148 | ||
947995c0 | 149 | return *p == ':'; |
9e0b22f4 SH |
150 | } |
151 | ||
83f64091 | 152 | int path_is_absolute(const char *path) |
3b0d4f61 | 153 | { |
21664424 FB |
154 | #ifdef _WIN32 |
155 | /* specific case for names like: "\\.\d:" */ | |
f53f4da9 | 156 | if (is_windows_drive(path) || is_windows_drive_prefix(path)) { |
21664424 | 157 | return 1; |
f53f4da9 PB |
158 | } |
159 | return (*path == '/' || *path == '\\'); | |
3b9f94e1 | 160 | #else |
f53f4da9 | 161 | return (*path == '/'); |
3b9f94e1 | 162 | #endif |
3b0d4f61 FB |
163 | } |
164 | ||
009b03aa | 165 | /* if filename is absolute, just return its duplicate. Otherwise, build a |
83f64091 FB |
166 | path to it by considering it is relative to base_path. URL are |
167 | supported. */ | |
009b03aa | 168 | char *path_combine(const char *base_path, const char *filename) |
3b0d4f61 | 169 | { |
009b03aa | 170 | const char *protocol_stripped = NULL; |
83f64091 | 171 | const char *p, *p1; |
009b03aa | 172 | char *result; |
83f64091 FB |
173 | int len; |
174 | ||
83f64091 | 175 | if (path_is_absolute(filename)) { |
009b03aa HR |
176 | return g_strdup(filename); |
177 | } | |
0d54a6fe | 178 | |
009b03aa HR |
179 | if (path_has_protocol(base_path)) { |
180 | protocol_stripped = strchr(base_path, ':'); | |
181 | if (protocol_stripped) { | |
182 | protocol_stripped++; | |
0d54a6fe | 183 | } |
009b03aa HR |
184 | } |
185 | p = protocol_stripped ?: base_path; | |
0d54a6fe | 186 | |
009b03aa | 187 | p1 = strrchr(base_path, '/'); |
3b9f94e1 | 188 | #ifdef _WIN32 |
009b03aa HR |
189 | { |
190 | const char *p2; | |
191 | p2 = strrchr(base_path, '\\'); | |
192 | if (!p1 || p2 > p1) { | |
193 | p1 = p2; | |
3b9f94e1 | 194 | } |
009b03aa | 195 | } |
3b9f94e1 | 196 | #endif |
009b03aa HR |
197 | if (p1) { |
198 | p1++; | |
199 | } else { | |
200 | p1 = base_path; | |
201 | } | |
202 | if (p1 > p) { | |
203 | p = p1; | |
3b0d4f61 | 204 | } |
009b03aa HR |
205 | len = p - base_path; |
206 | ||
207 | result = g_malloc(len + strlen(filename) + 1); | |
208 | memcpy(result, base_path, len); | |
209 | strcpy(result + len, filename); | |
210 | ||
211 | return result; | |
212 | } | |
213 | ||
03c320d8 HR |
214 | /* |
215 | * Helper function for bdrv_parse_filename() implementations to remove optional | |
216 | * protocol prefixes (especially "file:") from a filename and for putting the | |
217 | * stripped filename into the options QDict if there is such a prefix. | |
218 | */ | |
219 | void bdrv_parse_filename_strip_prefix(const char *filename, const char *prefix, | |
220 | QDict *options) | |
221 | { | |
222 | if (strstart(filename, prefix, &filename)) { | |
223 | /* Stripping the explicit protocol prefix may result in a protocol | |
224 | * prefix being (wrongly) detected (if the filename contains a colon) */ | |
225 | if (path_has_protocol(filename)) { | |
18cf67c5 | 226 | GString *fat_filename; |
03c320d8 HR |
227 | |
228 | /* This means there is some colon before the first slash; therefore, | |
229 | * this cannot be an absolute path */ | |
230 | assert(!path_is_absolute(filename)); | |
231 | ||
232 | /* And we can thus fix the protocol detection issue by prefixing it | |
233 | * by "./" */ | |
18cf67c5 MA |
234 | fat_filename = g_string_new("./"); |
235 | g_string_append(fat_filename, filename); | |
03c320d8 | 236 | |
18cf67c5 | 237 | assert(!path_has_protocol(fat_filename->str)); |
03c320d8 | 238 | |
18cf67c5 MA |
239 | qdict_put(options, "filename", |
240 | qstring_from_gstring(fat_filename)); | |
03c320d8 HR |
241 | } else { |
242 | /* If no protocol prefix was detected, we can use the shortened | |
243 | * filename as-is */ | |
244 | qdict_put_str(options, "filename", filename); | |
245 | } | |
246 | } | |
247 | } | |
248 | ||
249 | ||
9c5e6594 KW |
250 | /* Returns whether the image file is opened as read-only. Note that this can |
251 | * return false and writing to the image file is still not possible because the | |
252 | * image is inactivated. */ | |
93ed524e JC |
253 | bool bdrv_is_read_only(BlockDriverState *bs) |
254 | { | |
255 | return bs->read_only; | |
256 | } | |
257 | ||
54a32bfe KW |
258 | int bdrv_can_set_read_only(BlockDriverState *bs, bool read_only, |
259 | bool ignore_allow_rdw, Error **errp) | |
fe5241bf | 260 | { |
e2b8247a JC |
261 | /* Do not set read_only if copy_on_read is enabled */ |
262 | if (bs->copy_on_read && read_only) { | |
263 | error_setg(errp, "Can't set node '%s' to r/o with copy-on-read enabled", | |
264 | bdrv_get_device_or_node_name(bs)); | |
265 | return -EINVAL; | |
266 | } | |
267 | ||
d6fcdf06 | 268 | /* Do not clear read_only if it is prohibited */ |
54a32bfe KW |
269 | if (!read_only && !(bs->open_flags & BDRV_O_ALLOW_RDWR) && |
270 | !ignore_allow_rdw) | |
271 | { | |
d6fcdf06 JC |
272 | error_setg(errp, "Node '%s' is read only", |
273 | bdrv_get_device_or_node_name(bs)); | |
274 | return -EPERM; | |
275 | } | |
276 | ||
45803a03 JC |
277 | return 0; |
278 | } | |
279 | ||
eaa2410f KW |
280 | /* |
281 | * Called by a driver that can only provide a read-only image. | |
282 | * | |
283 | * Returns 0 if the node is already read-only or it could switch the node to | |
284 | * read-only because BDRV_O_AUTO_RDONLY is set. | |
285 | * | |
286 | * Returns -EACCES if the node is read-write and BDRV_O_AUTO_RDONLY is not set | |
287 | * or bdrv_can_set_read_only() forbids making the node read-only. If @errmsg | |
288 | * is not NULL, it is used as the error message for the Error object. | |
289 | */ | |
290 | int bdrv_apply_auto_read_only(BlockDriverState *bs, const char *errmsg, | |
291 | Error **errp) | |
45803a03 JC |
292 | { |
293 | int ret = 0; | |
294 | ||
eaa2410f KW |
295 | if (!(bs->open_flags & BDRV_O_RDWR)) { |
296 | return 0; | |
297 | } | |
298 | if (!(bs->open_flags & BDRV_O_AUTO_RDONLY)) { | |
299 | goto fail; | |
45803a03 JC |
300 | } |
301 | ||
eaa2410f KW |
302 | ret = bdrv_can_set_read_only(bs, true, false, NULL); |
303 | if (ret < 0) { | |
304 | goto fail; | |
eeae6a59 KW |
305 | } |
306 | ||
eaa2410f KW |
307 | bs->read_only = true; |
308 | bs->open_flags &= ~BDRV_O_RDWR; | |
309 | ||
e2b8247a | 310 | return 0; |
eaa2410f KW |
311 | |
312 | fail: | |
313 | error_setg(errp, "%s", errmsg ?: "Image is read-only"); | |
314 | return -EACCES; | |
fe5241bf JC |
315 | } |
316 | ||
645ae7d8 HR |
317 | /* |
318 | * If @backing is empty, this function returns NULL without setting | |
319 | * @errp. In all other cases, NULL will only be returned with @errp | |
320 | * set. | |
321 | * | |
322 | * Therefore, a return value of NULL without @errp set means that | |
323 | * there is no backing file; if @errp is set, there is one but its | |
324 | * absolute filename cannot be generated. | |
325 | */ | |
326 | char *bdrv_get_full_backing_filename_from_filename(const char *backed, | |
327 | const char *backing, | |
328 | Error **errp) | |
dc5a1371 | 329 | { |
645ae7d8 HR |
330 | if (backing[0] == '\0') { |
331 | return NULL; | |
332 | } else if (path_has_protocol(backing) || path_is_absolute(backing)) { | |
333 | return g_strdup(backing); | |
9f07429e HR |
334 | } else if (backed[0] == '\0' || strstart(backed, "json:", NULL)) { |
335 | error_setg(errp, "Cannot use relative backing file names for '%s'", | |
336 | backed); | |
645ae7d8 | 337 | return NULL; |
dc5a1371 | 338 | } else { |
645ae7d8 | 339 | return path_combine(backed, backing); |
dc5a1371 PB |
340 | } |
341 | } | |
342 | ||
9f4793d8 HR |
343 | /* |
344 | * If @filename is empty or NULL, this function returns NULL without | |
345 | * setting @errp. In all other cases, NULL will only be returned with | |
346 | * @errp set. | |
347 | */ | |
348 | static char *bdrv_make_absolute_filename(BlockDriverState *relative_to, | |
349 | const char *filename, Error **errp) | |
0a82855a | 350 | { |
8df68616 | 351 | char *dir, *full_name; |
9f4793d8 | 352 | |
8df68616 HR |
353 | if (!filename || filename[0] == '\0') { |
354 | return NULL; | |
355 | } else if (path_has_protocol(filename) || path_is_absolute(filename)) { | |
356 | return g_strdup(filename); | |
357 | } | |
9f07429e | 358 | |
8df68616 HR |
359 | dir = bdrv_dirname(relative_to, errp); |
360 | if (!dir) { | |
361 | return NULL; | |
362 | } | |
f30c66ba | 363 | |
8df68616 HR |
364 | full_name = g_strconcat(dir, filename, NULL); |
365 | g_free(dir); | |
366 | return full_name; | |
9f4793d8 HR |
367 | } |
368 | ||
369 | char *bdrv_get_full_backing_filename(BlockDriverState *bs, Error **errp) | |
370 | { | |
371 | return bdrv_make_absolute_filename(bs, bs->backing_file, errp); | |
0a82855a HR |
372 | } |
373 | ||
0eb7217e SH |
374 | void bdrv_register(BlockDriver *bdrv) |
375 | { | |
a15f08dc | 376 | assert(bdrv->format_name); |
8a22f02a | 377 | QLIST_INSERT_HEAD(&bdrv_drivers, bdrv, list); |
ea2384d3 | 378 | } |
b338082b | 379 | |
e4e9986b MA |
380 | BlockDriverState *bdrv_new(void) |
381 | { | |
382 | BlockDriverState *bs; | |
383 | int i; | |
384 | ||
5839e53b | 385 | bs = g_new0(BlockDriverState, 1); |
e4654d2d | 386 | QLIST_INIT(&bs->dirty_bitmaps); |
fbe40ff7 FZ |
387 | for (i = 0; i < BLOCK_OP_TYPE_MAX; i++) { |
388 | QLIST_INIT(&bs->op_blockers[i]); | |
389 | } | |
d616b224 | 390 | notifier_with_return_list_init(&bs->before_write_notifiers); |
3783fa3d | 391 | qemu_co_mutex_init(&bs->reqs_lock); |
2119882c | 392 | qemu_mutex_init(&bs->dirty_bitmap_mutex); |
9fcb0251 | 393 | bs->refcnt = 1; |
dcd04228 | 394 | bs->aio_context = qemu_get_aio_context(); |
d7d512f6 | 395 | |
3ff2f67a EY |
396 | qemu_co_queue_init(&bs->flush_queue); |
397 | ||
0f12264e KW |
398 | for (i = 0; i < bdrv_drain_all_count; i++) { |
399 | bdrv_drained_begin(bs); | |
400 | } | |
401 | ||
2c1d04e0 HR |
402 | QTAILQ_INSERT_TAIL(&all_bdrv_states, bs, bs_list); |
403 | ||
b338082b FB |
404 | return bs; |
405 | } | |
406 | ||
88d88798 | 407 | static BlockDriver *bdrv_do_find_format(const char *format_name) |
ea2384d3 FB |
408 | { |
409 | BlockDriver *drv1; | |
88d88798 | 410 | |
8a22f02a SH |
411 | QLIST_FOREACH(drv1, &bdrv_drivers, list) { |
412 | if (!strcmp(drv1->format_name, format_name)) { | |
ea2384d3 | 413 | return drv1; |
8a22f02a | 414 | } |
ea2384d3 | 415 | } |
88d88798 | 416 | |
ea2384d3 FB |
417 | return NULL; |
418 | } | |
419 | ||
88d88798 MM |
420 | BlockDriver *bdrv_find_format(const char *format_name) |
421 | { | |
422 | BlockDriver *drv1; | |
423 | int i; | |
424 | ||
425 | drv1 = bdrv_do_find_format(format_name); | |
426 | if (drv1) { | |
427 | return drv1; | |
428 | } | |
429 | ||
430 | /* The driver isn't registered, maybe we need to load a module */ | |
431 | for (i = 0; i < (int)ARRAY_SIZE(block_driver_modules); ++i) { | |
432 | if (!strcmp(block_driver_modules[i].format_name, format_name)) { | |
433 | block_module_load_one(block_driver_modules[i].library_name); | |
434 | break; | |
435 | } | |
436 | } | |
437 | ||
438 | return bdrv_do_find_format(format_name); | |
439 | } | |
440 | ||
9ac404c5 | 441 | static int bdrv_format_is_whitelisted(const char *format_name, bool read_only) |
eb852011 | 442 | { |
b64ec4e4 FZ |
443 | static const char *whitelist_rw[] = { |
444 | CONFIG_BDRV_RW_WHITELIST | |
859aef02 | 445 | NULL |
b64ec4e4 FZ |
446 | }; |
447 | static const char *whitelist_ro[] = { | |
448 | CONFIG_BDRV_RO_WHITELIST | |
859aef02 | 449 | NULL |
eb852011 MA |
450 | }; |
451 | const char **p; | |
452 | ||
b64ec4e4 | 453 | if (!whitelist_rw[0] && !whitelist_ro[0]) { |
eb852011 | 454 | return 1; /* no whitelist, anything goes */ |
b64ec4e4 | 455 | } |
eb852011 | 456 | |
b64ec4e4 | 457 | for (p = whitelist_rw; *p; p++) { |
9ac404c5 | 458 | if (!strcmp(format_name, *p)) { |
eb852011 MA |
459 | return 1; |
460 | } | |
461 | } | |
b64ec4e4 FZ |
462 | if (read_only) { |
463 | for (p = whitelist_ro; *p; p++) { | |
9ac404c5 | 464 | if (!strcmp(format_name, *p)) { |
b64ec4e4 FZ |
465 | return 1; |
466 | } | |
467 | } | |
468 | } | |
eb852011 MA |
469 | return 0; |
470 | } | |
471 | ||
9ac404c5 AS |
472 | int bdrv_is_whitelisted(BlockDriver *drv, bool read_only) |
473 | { | |
474 | return bdrv_format_is_whitelisted(drv->format_name, read_only); | |
475 | } | |
476 | ||
e6ff69bf DB |
477 | bool bdrv_uses_whitelist(void) |
478 | { | |
479 | return use_bdrv_whitelist; | |
480 | } | |
481 | ||
5b7e1542 ZYW |
482 | typedef struct CreateCo { |
483 | BlockDriver *drv; | |
484 | char *filename; | |
83d0521a | 485 | QemuOpts *opts; |
5b7e1542 | 486 | int ret; |
cc84d90f | 487 | Error *err; |
5b7e1542 ZYW |
488 | } CreateCo; |
489 | ||
490 | static void coroutine_fn bdrv_create_co_entry(void *opaque) | |
491 | { | |
cc84d90f HR |
492 | Error *local_err = NULL; |
493 | int ret; | |
494 | ||
5b7e1542 ZYW |
495 | CreateCo *cco = opaque; |
496 | assert(cco->drv); | |
497 | ||
b92902df ML |
498 | ret = cco->drv->bdrv_co_create_opts(cco->drv, |
499 | cco->filename, cco->opts, &local_err); | |
621ff94d | 500 | error_propagate(&cco->err, local_err); |
cc84d90f | 501 | cco->ret = ret; |
5b7e1542 ZYW |
502 | } |
503 | ||
0e7e1989 | 504 | int bdrv_create(BlockDriver *drv, const char* filename, |
83d0521a | 505 | QemuOpts *opts, Error **errp) |
ea2384d3 | 506 | { |
5b7e1542 ZYW |
507 | int ret; |
508 | ||
509 | Coroutine *co; | |
510 | CreateCo cco = { | |
511 | .drv = drv, | |
512 | .filename = g_strdup(filename), | |
83d0521a | 513 | .opts = opts, |
5b7e1542 | 514 | .ret = NOT_DONE, |
cc84d90f | 515 | .err = NULL, |
5b7e1542 ZYW |
516 | }; |
517 | ||
efc75e2a | 518 | if (!drv->bdrv_co_create_opts) { |
cc84d90f | 519 | error_setg(errp, "Driver '%s' does not support image creation", drv->format_name); |
80168bff LC |
520 | ret = -ENOTSUP; |
521 | goto out; | |
5b7e1542 ZYW |
522 | } |
523 | ||
524 | if (qemu_in_coroutine()) { | |
525 | /* Fast-path if already in coroutine context */ | |
526 | bdrv_create_co_entry(&cco); | |
527 | } else { | |
0b8b8753 PB |
528 | co = qemu_coroutine_create(bdrv_create_co_entry, &cco); |
529 | qemu_coroutine_enter(co); | |
5b7e1542 | 530 | while (cco.ret == NOT_DONE) { |
b47ec2c4 | 531 | aio_poll(qemu_get_aio_context(), true); |
5b7e1542 ZYW |
532 | } |
533 | } | |
534 | ||
535 | ret = cco.ret; | |
cc84d90f | 536 | if (ret < 0) { |
84d18f06 | 537 | if (cco.err) { |
cc84d90f HR |
538 | error_propagate(errp, cco.err); |
539 | } else { | |
540 | error_setg_errno(errp, -ret, "Could not create image"); | |
541 | } | |
542 | } | |
0e7e1989 | 543 | |
80168bff LC |
544 | out: |
545 | g_free(cco.filename); | |
5b7e1542 | 546 | return ret; |
ea2384d3 FB |
547 | } |
548 | ||
fd17146c HR |
549 | /** |
550 | * Helper function for bdrv_create_file_fallback(): Resize @blk to at | |
551 | * least the given @minimum_size. | |
552 | * | |
553 | * On success, return @blk's actual length. | |
554 | * Otherwise, return -errno. | |
555 | */ | |
556 | static int64_t create_file_fallback_truncate(BlockBackend *blk, | |
557 | int64_t minimum_size, Error **errp) | |
84a12e66 | 558 | { |
cc84d90f | 559 | Error *local_err = NULL; |
fd17146c | 560 | int64_t size; |
cc84d90f | 561 | int ret; |
84a12e66 | 562 | |
8c6242b6 KW |
563 | ret = blk_truncate(blk, minimum_size, false, PREALLOC_MODE_OFF, 0, |
564 | &local_err); | |
fd17146c HR |
565 | if (ret < 0 && ret != -ENOTSUP) { |
566 | error_propagate(errp, local_err); | |
567 | return ret; | |
568 | } | |
569 | ||
570 | size = blk_getlength(blk); | |
571 | if (size < 0) { | |
572 | error_free(local_err); | |
573 | error_setg_errno(errp, -size, | |
574 | "Failed to inquire the new image file's length"); | |
575 | return size; | |
576 | } | |
577 | ||
578 | if (size < minimum_size) { | |
579 | /* Need to grow the image, but we failed to do that */ | |
580 | error_propagate(errp, local_err); | |
581 | return -ENOTSUP; | |
582 | } | |
583 | ||
584 | error_free(local_err); | |
585 | local_err = NULL; | |
586 | ||
587 | return size; | |
588 | } | |
589 | ||
590 | /** | |
591 | * Helper function for bdrv_create_file_fallback(): Zero the first | |
592 | * sector to remove any potentially pre-existing image header. | |
593 | */ | |
594 | static int create_file_fallback_zero_first_sector(BlockBackend *blk, | |
595 | int64_t current_size, | |
596 | Error **errp) | |
597 | { | |
598 | int64_t bytes_to_clear; | |
599 | int ret; | |
600 | ||
601 | bytes_to_clear = MIN(current_size, BDRV_SECTOR_SIZE); | |
602 | if (bytes_to_clear) { | |
603 | ret = blk_pwrite_zeroes(blk, 0, bytes_to_clear, BDRV_REQ_MAY_UNMAP); | |
604 | if (ret < 0) { | |
605 | error_setg_errno(errp, -ret, | |
606 | "Failed to clear the new image's first sector"); | |
607 | return ret; | |
608 | } | |
609 | } | |
610 | ||
611 | return 0; | |
612 | } | |
613 | ||
5a5e7f8c ML |
614 | /** |
615 | * Simple implementation of bdrv_co_create_opts for protocol drivers | |
616 | * which only support creation via opening a file | |
617 | * (usually existing raw storage device) | |
618 | */ | |
619 | int coroutine_fn bdrv_co_create_opts_simple(BlockDriver *drv, | |
620 | const char *filename, | |
621 | QemuOpts *opts, | |
622 | Error **errp) | |
fd17146c HR |
623 | { |
624 | BlockBackend *blk; | |
eeea1faa | 625 | QDict *options; |
fd17146c HR |
626 | int64_t size = 0; |
627 | char *buf = NULL; | |
628 | PreallocMode prealloc; | |
629 | Error *local_err = NULL; | |
630 | int ret; | |
631 | ||
632 | size = qemu_opt_get_size_del(opts, BLOCK_OPT_SIZE, 0); | |
633 | buf = qemu_opt_get_del(opts, BLOCK_OPT_PREALLOC); | |
634 | prealloc = qapi_enum_parse(&PreallocMode_lookup, buf, | |
635 | PREALLOC_MODE_OFF, &local_err); | |
636 | g_free(buf); | |
637 | if (local_err) { | |
638 | error_propagate(errp, local_err); | |
639 | return -EINVAL; | |
640 | } | |
641 | ||
642 | if (prealloc != PREALLOC_MODE_OFF) { | |
643 | error_setg(errp, "Unsupported preallocation mode '%s'", | |
644 | PreallocMode_str(prealloc)); | |
645 | return -ENOTSUP; | |
646 | } | |
647 | ||
eeea1faa | 648 | options = qdict_new(); |
fd17146c HR |
649 | qdict_put_str(options, "driver", drv->format_name); |
650 | ||
651 | blk = blk_new_open(filename, NULL, options, | |
652 | BDRV_O_RDWR | BDRV_O_RESIZE, errp); | |
653 | if (!blk) { | |
654 | error_prepend(errp, "Protocol driver '%s' does not support image " | |
655 | "creation, and opening the image failed: ", | |
656 | drv->format_name); | |
657 | return -EINVAL; | |
658 | } | |
659 | ||
660 | size = create_file_fallback_truncate(blk, size, errp); | |
661 | if (size < 0) { | |
662 | ret = size; | |
663 | goto out; | |
664 | } | |
665 | ||
666 | ret = create_file_fallback_zero_first_sector(blk, size, errp); | |
667 | if (ret < 0) { | |
668 | goto out; | |
669 | } | |
670 | ||
671 | ret = 0; | |
672 | out: | |
673 | blk_unref(blk); | |
674 | return ret; | |
675 | } | |
676 | ||
677 | int bdrv_create_file(const char *filename, QemuOpts *opts, Error **errp) | |
678 | { | |
729222af | 679 | QemuOpts *protocol_opts; |
fd17146c | 680 | BlockDriver *drv; |
729222af SG |
681 | QDict *qdict; |
682 | int ret; | |
fd17146c | 683 | |
b65a5e12 | 684 | drv = bdrv_find_protocol(filename, true, errp); |
84a12e66 | 685 | if (drv == NULL) { |
16905d71 | 686 | return -ENOENT; |
84a12e66 CH |
687 | } |
688 | ||
729222af SG |
689 | if (!drv->create_opts) { |
690 | error_setg(errp, "Driver '%s' does not support image creation", | |
691 | drv->format_name); | |
692 | return -ENOTSUP; | |
693 | } | |
694 | ||
695 | /* | |
696 | * 'opts' contains a QemuOptsList with a combination of format and protocol | |
697 | * default values. | |
698 | * | |
699 | * The format properly removes its options, but the default values remain | |
700 | * in 'opts->list'. So if the protocol has options with the same name | |
701 | * (e.g. rbd has 'cluster_size' as qcow2), it will see the default values | |
702 | * of the format, since for overlapping options, the format wins. | |
703 | * | |
704 | * To avoid this issue, lets convert QemuOpts to QDict, in this way we take | |
705 | * only the set options, and then convert it back to QemuOpts, using the | |
706 | * create_opts of the protocol. So the new QemuOpts, will contain only the | |
707 | * protocol defaults. | |
708 | */ | |
709 | qdict = qemu_opts_to_qdict(opts, NULL); | |
710 | protocol_opts = qemu_opts_from_qdict(drv->create_opts, qdict, errp); | |
711 | if (protocol_opts == NULL) { | |
712 | ret = -EINVAL; | |
713 | goto out; | |
714 | } | |
715 | ||
716 | ret = bdrv_create(drv, filename, protocol_opts, errp); | |
717 | out: | |
718 | qemu_opts_del(protocol_opts); | |
719 | qobject_unref(qdict); | |
720 | return ret; | |
84a12e66 CH |
721 | } |
722 | ||
e1d7f8bb DHB |
723 | int coroutine_fn bdrv_co_delete_file(BlockDriverState *bs, Error **errp) |
724 | { | |
725 | Error *local_err = NULL; | |
726 | int ret; | |
727 | ||
728 | assert(bs != NULL); | |
729 | ||
730 | if (!bs->drv) { | |
731 | error_setg(errp, "Block node '%s' is not opened", bs->filename); | |
732 | return -ENOMEDIUM; | |
733 | } | |
734 | ||
735 | if (!bs->drv->bdrv_co_delete_file) { | |
736 | error_setg(errp, "Driver '%s' does not support image deletion", | |
737 | bs->drv->format_name); | |
738 | return -ENOTSUP; | |
739 | } | |
740 | ||
741 | ret = bs->drv->bdrv_co_delete_file(bs, &local_err); | |
742 | if (ret < 0) { | |
743 | error_propagate(errp, local_err); | |
744 | } | |
745 | ||
746 | return ret; | |
747 | } | |
748 | ||
a890f08e ML |
749 | void coroutine_fn bdrv_co_delete_file_noerr(BlockDriverState *bs) |
750 | { | |
751 | Error *local_err = NULL; | |
752 | int ret; | |
753 | ||
754 | if (!bs) { | |
755 | return; | |
756 | } | |
757 | ||
758 | ret = bdrv_co_delete_file(bs, &local_err); | |
759 | /* | |
760 | * ENOTSUP will happen if the block driver doesn't support | |
761 | * the 'bdrv_co_delete_file' interface. This is a predictable | |
762 | * scenario and shouldn't be reported back to the user. | |
763 | */ | |
764 | if (ret == -ENOTSUP) { | |
765 | error_free(local_err); | |
766 | } else if (ret < 0) { | |
767 | error_report_err(local_err); | |
768 | } | |
769 | } | |
770 | ||
892b7de8 ET |
771 | /** |
772 | * Try to get @bs's logical and physical block size. | |
773 | * On success, store them in @bsz struct and return 0. | |
774 | * On failure return -errno. | |
775 | * @bs must not be empty. | |
776 | */ | |
777 | int bdrv_probe_blocksizes(BlockDriverState *bs, BlockSizes *bsz) | |
778 | { | |
779 | BlockDriver *drv = bs->drv; | |
93393e69 | 780 | BlockDriverState *filtered = bdrv_filter_bs(bs); |
892b7de8 ET |
781 | |
782 | if (drv && drv->bdrv_probe_blocksizes) { | |
783 | return drv->bdrv_probe_blocksizes(bs, bsz); | |
93393e69 HR |
784 | } else if (filtered) { |
785 | return bdrv_probe_blocksizes(filtered, bsz); | |
892b7de8 ET |
786 | } |
787 | ||
788 | return -ENOTSUP; | |
789 | } | |
790 | ||
791 | /** | |
792 | * Try to get @bs's geometry (cyls, heads, sectors). | |
793 | * On success, store them in @geo struct and return 0. | |
794 | * On failure return -errno. | |
795 | * @bs must not be empty. | |
796 | */ | |
797 | int bdrv_probe_geometry(BlockDriverState *bs, HDGeometry *geo) | |
798 | { | |
799 | BlockDriver *drv = bs->drv; | |
93393e69 | 800 | BlockDriverState *filtered = bdrv_filter_bs(bs); |
892b7de8 ET |
801 | |
802 | if (drv && drv->bdrv_probe_geometry) { | |
803 | return drv->bdrv_probe_geometry(bs, geo); | |
93393e69 HR |
804 | } else if (filtered) { |
805 | return bdrv_probe_geometry(filtered, geo); | |
892b7de8 ET |
806 | } |
807 | ||
808 | return -ENOTSUP; | |
809 | } | |
810 | ||
eba25057 JM |
811 | /* |
812 | * Create a uniquely-named empty temporary file. | |
813 | * Return 0 upon success, otherwise a negative errno value. | |
814 | */ | |
815 | int get_tmp_filename(char *filename, int size) | |
d5249393 | 816 | { |
eba25057 | 817 | #ifdef _WIN32 |
3b9f94e1 | 818 | char temp_dir[MAX_PATH]; |
eba25057 JM |
819 | /* GetTempFileName requires that its output buffer (4th param) |
820 | have length MAX_PATH or greater. */ | |
821 | assert(size >= MAX_PATH); | |
822 | return (GetTempPath(MAX_PATH, temp_dir) | |
823 | && GetTempFileName(temp_dir, "qem", 0, filename) | |
824 | ? 0 : -GetLastError()); | |
d5249393 | 825 | #else |
67b915a5 | 826 | int fd; |
7ccfb2eb | 827 | const char *tmpdir; |
0badc1ee | 828 | tmpdir = getenv("TMPDIR"); |
69bef793 AS |
829 | if (!tmpdir) { |
830 | tmpdir = "/var/tmp"; | |
831 | } | |
eba25057 JM |
832 | if (snprintf(filename, size, "%s/vl.XXXXXX", tmpdir) >= size) { |
833 | return -EOVERFLOW; | |
834 | } | |
ea2384d3 | 835 | fd = mkstemp(filename); |
fe235a06 DH |
836 | if (fd < 0) { |
837 | return -errno; | |
838 | } | |
839 | if (close(fd) != 0) { | |
840 | unlink(filename); | |
eba25057 JM |
841 | return -errno; |
842 | } | |
843 | return 0; | |
d5249393 | 844 | #endif |
eba25057 | 845 | } |
fc01f7e7 | 846 | |
84a12e66 CH |
847 | /* |
848 | * Detect host devices. By convention, /dev/cdrom[N] is always | |
849 | * recognized as a host CDROM. | |
850 | */ | |
851 | static BlockDriver *find_hdev_driver(const char *filename) | |
852 | { | |
853 | int score_max = 0, score; | |
854 | BlockDriver *drv = NULL, *d; | |
855 | ||
856 | QLIST_FOREACH(d, &bdrv_drivers, list) { | |
857 | if (d->bdrv_probe_device) { | |
858 | score = d->bdrv_probe_device(filename); | |
859 | if (score > score_max) { | |
860 | score_max = score; | |
861 | drv = d; | |
862 | } | |
863 | } | |
864 | } | |
865 | ||
866 | return drv; | |
867 | } | |
868 | ||
88d88798 MM |
869 | static BlockDriver *bdrv_do_find_protocol(const char *protocol) |
870 | { | |
871 | BlockDriver *drv1; | |
872 | ||
873 | QLIST_FOREACH(drv1, &bdrv_drivers, list) { | |
874 | if (drv1->protocol_name && !strcmp(drv1->protocol_name, protocol)) { | |
875 | return drv1; | |
876 | } | |
877 | } | |
878 | ||
879 | return NULL; | |
880 | } | |
881 | ||
98289620 | 882 | BlockDriver *bdrv_find_protocol(const char *filename, |
b65a5e12 HR |
883 | bool allow_protocol_prefix, |
884 | Error **errp) | |
83f64091 FB |
885 | { |
886 | BlockDriver *drv1; | |
887 | char protocol[128]; | |
1cec71e3 | 888 | int len; |
83f64091 | 889 | const char *p; |
88d88798 | 890 | int i; |
19cb3738 | 891 | |
66f82cee KW |
892 | /* TODO Drivers without bdrv_file_open must be specified explicitly */ |
893 | ||
39508e7a CH |
894 | /* |
895 | * XXX(hch): we really should not let host device detection | |
896 | * override an explicit protocol specification, but moving this | |
897 | * later breaks access to device names with colons in them. | |
898 | * Thanks to the brain-dead persistent naming schemes on udev- | |
899 | * based Linux systems those actually are quite common. | |
900 | */ | |
901 | drv1 = find_hdev_driver(filename); | |
902 | if (drv1) { | |
903 | return drv1; | |
904 | } | |
905 | ||
98289620 | 906 | if (!path_has_protocol(filename) || !allow_protocol_prefix) { |
ef810437 | 907 | return &bdrv_file; |
84a12e66 | 908 | } |
98289620 | 909 | |
9e0b22f4 SH |
910 | p = strchr(filename, ':'); |
911 | assert(p != NULL); | |
1cec71e3 AL |
912 | len = p - filename; |
913 | if (len > sizeof(protocol) - 1) | |
914 | len = sizeof(protocol) - 1; | |
915 | memcpy(protocol, filename, len); | |
916 | protocol[len] = '\0'; | |
88d88798 MM |
917 | |
918 | drv1 = bdrv_do_find_protocol(protocol); | |
919 | if (drv1) { | |
920 | return drv1; | |
921 | } | |
922 | ||
923 | for (i = 0; i < (int)ARRAY_SIZE(block_driver_modules); ++i) { | |
924 | if (block_driver_modules[i].protocol_name && | |
925 | !strcmp(block_driver_modules[i].protocol_name, protocol)) { | |
926 | block_module_load_one(block_driver_modules[i].library_name); | |
927 | break; | |
8a22f02a | 928 | } |
83f64091 | 929 | } |
b65a5e12 | 930 | |
88d88798 MM |
931 | drv1 = bdrv_do_find_protocol(protocol); |
932 | if (!drv1) { | |
933 | error_setg(errp, "Unknown protocol '%s'", protocol); | |
934 | } | |
935 | return drv1; | |
83f64091 FB |
936 | } |
937 | ||
c6684249 MA |
938 | /* |
939 | * Guess image format by probing its contents. | |
940 | * This is not a good idea when your image is raw (CVE-2008-2004), but | |
941 | * we do it anyway for backward compatibility. | |
942 | * | |
943 | * @buf contains the image's first @buf_size bytes. | |
7cddd372 KW |
944 | * @buf_size is the buffer size in bytes (generally BLOCK_PROBE_BUF_SIZE, |
945 | * but can be smaller if the image file is smaller) | |
c6684249 MA |
946 | * @filename is its filename. |
947 | * | |
948 | * For all block drivers, call the bdrv_probe() method to get its | |
949 | * probing score. | |
950 | * Return the first block driver with the highest probing score. | |
951 | */ | |
38f3ef57 KW |
952 | BlockDriver *bdrv_probe_all(const uint8_t *buf, int buf_size, |
953 | const char *filename) | |
c6684249 MA |
954 | { |
955 | int score_max = 0, score; | |
956 | BlockDriver *drv = NULL, *d; | |
957 | ||
958 | QLIST_FOREACH(d, &bdrv_drivers, list) { | |
959 | if (d->bdrv_probe) { | |
960 | score = d->bdrv_probe(buf, buf_size, filename); | |
961 | if (score > score_max) { | |
962 | score_max = score; | |
963 | drv = d; | |
964 | } | |
965 | } | |
966 | } | |
967 | ||
968 | return drv; | |
969 | } | |
970 | ||
5696c6e3 | 971 | static int find_image_format(BlockBackend *file, const char *filename, |
34b5d2c6 | 972 | BlockDriver **pdrv, Error **errp) |
f3a5d3f8 | 973 | { |
c6684249 | 974 | BlockDriver *drv; |
7cddd372 | 975 | uint8_t buf[BLOCK_PROBE_BUF_SIZE]; |
f500a6d3 | 976 | int ret = 0; |
f8ea0b00 | 977 | |
08a00559 | 978 | /* Return the raw BlockDriver * to scsi-generic devices or empty drives */ |
5696c6e3 | 979 | if (blk_is_sg(file) || !blk_is_inserted(file) || blk_getlength(file) == 0) { |
ef810437 | 980 | *pdrv = &bdrv_raw; |
c98ac35d | 981 | return ret; |
1a396859 | 982 | } |
f8ea0b00 | 983 | |
5696c6e3 | 984 | ret = blk_pread(file, 0, buf, sizeof(buf)); |
83f64091 | 985 | if (ret < 0) { |
34b5d2c6 HR |
986 | error_setg_errno(errp, -ret, "Could not read image for determining its " |
987 | "format"); | |
c98ac35d SW |
988 | *pdrv = NULL; |
989 | return ret; | |
83f64091 FB |
990 | } |
991 | ||
c6684249 | 992 | drv = bdrv_probe_all(buf, ret, filename); |
c98ac35d | 993 | if (!drv) { |
34b5d2c6 HR |
994 | error_setg(errp, "Could not determine image format: No compatible " |
995 | "driver found"); | |
c98ac35d SW |
996 | ret = -ENOENT; |
997 | } | |
998 | *pdrv = drv; | |
999 | return ret; | |
ea2384d3 FB |
1000 | } |
1001 | ||
51762288 SH |
1002 | /** |
1003 | * Set the current 'total_sectors' value | |
65a9bb25 | 1004 | * Return 0 on success, -errno on error. |
51762288 | 1005 | */ |
3d9f2d2a | 1006 | int refresh_total_sectors(BlockDriverState *bs, int64_t hint) |
51762288 SH |
1007 | { |
1008 | BlockDriver *drv = bs->drv; | |
1009 | ||
d470ad42 HR |
1010 | if (!drv) { |
1011 | return -ENOMEDIUM; | |
1012 | } | |
1013 | ||
396759ad | 1014 | /* Do not attempt drv->bdrv_getlength() on scsi-generic devices */ |
b192af8a | 1015 | if (bdrv_is_sg(bs)) |
396759ad NB |
1016 | return 0; |
1017 | ||
51762288 SH |
1018 | /* query actual device if possible, otherwise just trust the hint */ |
1019 | if (drv->bdrv_getlength) { | |
1020 | int64_t length = drv->bdrv_getlength(bs); | |
1021 | if (length < 0) { | |
1022 | return length; | |
1023 | } | |
7e382003 | 1024 | hint = DIV_ROUND_UP(length, BDRV_SECTOR_SIZE); |
51762288 SH |
1025 | } |
1026 | ||
1027 | bs->total_sectors = hint; | |
8b117001 VSO |
1028 | |
1029 | if (bs->total_sectors * BDRV_SECTOR_SIZE > BDRV_MAX_LENGTH) { | |
1030 | return -EFBIG; | |
1031 | } | |
1032 | ||
51762288 SH |
1033 | return 0; |
1034 | } | |
1035 | ||
cddff5ba KW |
1036 | /** |
1037 | * Combines a QDict of new block driver @options with any missing options taken | |
1038 | * from @old_options, so that leaving out an option defaults to its old value. | |
1039 | */ | |
1040 | static void bdrv_join_options(BlockDriverState *bs, QDict *options, | |
1041 | QDict *old_options) | |
1042 | { | |
1043 | if (bs->drv && bs->drv->bdrv_join_options) { | |
1044 | bs->drv->bdrv_join_options(options, old_options); | |
1045 | } else { | |
1046 | qdict_join(options, old_options, false); | |
1047 | } | |
1048 | } | |
1049 | ||
543770bd AG |
1050 | static BlockdevDetectZeroesOptions bdrv_parse_detect_zeroes(QemuOpts *opts, |
1051 | int open_flags, | |
1052 | Error **errp) | |
1053 | { | |
1054 | Error *local_err = NULL; | |
1055 | char *value = qemu_opt_get_del(opts, "detect-zeroes"); | |
1056 | BlockdevDetectZeroesOptions detect_zeroes = | |
1057 | qapi_enum_parse(&BlockdevDetectZeroesOptions_lookup, value, | |
1058 | BLOCKDEV_DETECT_ZEROES_OPTIONS_OFF, &local_err); | |
1059 | g_free(value); | |
1060 | if (local_err) { | |
1061 | error_propagate(errp, local_err); | |
1062 | return detect_zeroes; | |
1063 | } | |
1064 | ||
1065 | if (detect_zeroes == BLOCKDEV_DETECT_ZEROES_OPTIONS_UNMAP && | |
1066 | !(open_flags & BDRV_O_UNMAP)) | |
1067 | { | |
1068 | error_setg(errp, "setting detect-zeroes to unmap is not allowed " | |
1069 | "without setting discard operation to unmap"); | |
1070 | } | |
1071 | ||
1072 | return detect_zeroes; | |
1073 | } | |
1074 | ||
f80f2673 AM |
1075 | /** |
1076 | * Set open flags for aio engine | |
1077 | * | |
1078 | * Return 0 on success, -1 if the engine specified is invalid | |
1079 | */ | |
1080 | int bdrv_parse_aio(const char *mode, int *flags) | |
1081 | { | |
1082 | if (!strcmp(mode, "threads")) { | |
1083 | /* do nothing, default */ | |
1084 | } else if (!strcmp(mode, "native")) { | |
1085 | *flags |= BDRV_O_NATIVE_AIO; | |
1086 | #ifdef CONFIG_LINUX_IO_URING | |
1087 | } else if (!strcmp(mode, "io_uring")) { | |
1088 | *flags |= BDRV_O_IO_URING; | |
1089 | #endif | |
1090 | } else { | |
1091 | return -1; | |
1092 | } | |
1093 | ||
1094 | return 0; | |
1095 | } | |
1096 | ||
9e8f1835 PB |
1097 | /** |
1098 | * Set open flags for a given discard mode | |
1099 | * | |
1100 | * Return 0 on success, -1 if the discard mode was invalid. | |
1101 | */ | |
1102 | int bdrv_parse_discard_flags(const char *mode, int *flags) | |
1103 | { | |
1104 | *flags &= ~BDRV_O_UNMAP; | |
1105 | ||
1106 | if (!strcmp(mode, "off") || !strcmp(mode, "ignore")) { | |
1107 | /* do nothing */ | |
1108 | } else if (!strcmp(mode, "on") || !strcmp(mode, "unmap")) { | |
1109 | *flags |= BDRV_O_UNMAP; | |
1110 | } else { | |
1111 | return -1; | |
1112 | } | |
1113 | ||
1114 | return 0; | |
1115 | } | |
1116 | ||
c3993cdc SH |
1117 | /** |
1118 | * Set open flags for a given cache mode | |
1119 | * | |
1120 | * Return 0 on success, -1 if the cache mode was invalid. | |
1121 | */ | |
53e8ae01 | 1122 | int bdrv_parse_cache_mode(const char *mode, int *flags, bool *writethrough) |
c3993cdc SH |
1123 | { |
1124 | *flags &= ~BDRV_O_CACHE_MASK; | |
1125 | ||
1126 | if (!strcmp(mode, "off") || !strcmp(mode, "none")) { | |
53e8ae01 KW |
1127 | *writethrough = false; |
1128 | *flags |= BDRV_O_NOCACHE; | |
92196b2f | 1129 | } else if (!strcmp(mode, "directsync")) { |
53e8ae01 | 1130 | *writethrough = true; |
92196b2f | 1131 | *flags |= BDRV_O_NOCACHE; |
c3993cdc | 1132 | } else if (!strcmp(mode, "writeback")) { |
53e8ae01 | 1133 | *writethrough = false; |
c3993cdc | 1134 | } else if (!strcmp(mode, "unsafe")) { |
53e8ae01 | 1135 | *writethrough = false; |
c3993cdc SH |
1136 | *flags |= BDRV_O_NO_FLUSH; |
1137 | } else if (!strcmp(mode, "writethrough")) { | |
53e8ae01 | 1138 | *writethrough = true; |
c3993cdc SH |
1139 | } else { |
1140 | return -1; | |
1141 | } | |
1142 | ||
1143 | return 0; | |
1144 | } | |
1145 | ||
b5411555 KW |
1146 | static char *bdrv_child_get_parent_desc(BdrvChild *c) |
1147 | { | |
1148 | BlockDriverState *parent = c->opaque; | |
1149 | return g_strdup(bdrv_get_device_or_node_name(parent)); | |
1150 | } | |
1151 | ||
20018e12 KW |
1152 | static void bdrv_child_cb_drained_begin(BdrvChild *child) |
1153 | { | |
1154 | BlockDriverState *bs = child->opaque; | |
6cd5c9d7 | 1155 | bdrv_do_drained_begin_quiesce(bs, NULL, false); |
20018e12 KW |
1156 | } |
1157 | ||
89bd0305 KW |
1158 | static bool bdrv_child_cb_drained_poll(BdrvChild *child) |
1159 | { | |
1160 | BlockDriverState *bs = child->opaque; | |
6cd5c9d7 | 1161 | return bdrv_drain_poll(bs, false, NULL, false); |
89bd0305 KW |
1162 | } |
1163 | ||
e037c09c HR |
1164 | static void bdrv_child_cb_drained_end(BdrvChild *child, |
1165 | int *drained_end_counter) | |
20018e12 KW |
1166 | { |
1167 | BlockDriverState *bs = child->opaque; | |
e037c09c | 1168 | bdrv_drained_end_no_poll(bs, drained_end_counter); |
20018e12 KW |
1169 | } |
1170 | ||
38701b6a KW |
1171 | static int bdrv_child_cb_inactivate(BdrvChild *child) |
1172 | { | |
1173 | BlockDriverState *bs = child->opaque; | |
1174 | assert(bs->open_flags & BDRV_O_INACTIVE); | |
1175 | return 0; | |
1176 | } | |
1177 | ||
5d231849 KW |
1178 | static bool bdrv_child_cb_can_set_aio_ctx(BdrvChild *child, AioContext *ctx, |
1179 | GSList **ignore, Error **errp) | |
1180 | { | |
1181 | BlockDriverState *bs = child->opaque; | |
1182 | return bdrv_can_set_aio_context(bs, ctx, ignore, errp); | |
1183 | } | |
1184 | ||
53a7d041 KW |
1185 | static void bdrv_child_cb_set_aio_ctx(BdrvChild *child, AioContext *ctx, |
1186 | GSList **ignore) | |
1187 | { | |
1188 | BlockDriverState *bs = child->opaque; | |
1189 | return bdrv_set_aio_context_ignore(bs, ctx, ignore); | |
1190 | } | |
1191 | ||
b1e6fc08 | 1192 | /* |
73176bee KW |
1193 | * Returns the options and flags that a temporary snapshot should get, based on |
1194 | * the originally requested flags (the originally requested image will have | |
1195 | * flags like a backing file) | |
b1e6fc08 | 1196 | */ |
73176bee KW |
1197 | static void bdrv_temp_snapshot_options(int *child_flags, QDict *child_options, |
1198 | int parent_flags, QDict *parent_options) | |
b1e6fc08 | 1199 | { |
73176bee KW |
1200 | *child_flags = (parent_flags & ~BDRV_O_SNAPSHOT) | BDRV_O_TEMPORARY; |
1201 | ||
1202 | /* For temporary files, unconditional cache=unsafe is fine */ | |
73176bee KW |
1203 | qdict_set_default_str(child_options, BDRV_OPT_CACHE_DIRECT, "off"); |
1204 | qdict_set_default_str(child_options, BDRV_OPT_CACHE_NO_FLUSH, "on"); | |
41869044 | 1205 | |
3f48686f | 1206 | /* Copy the read-only and discard options from the parent */ |
f87a0e29 | 1207 | qdict_copy_default(child_options, parent_options, BDRV_OPT_READ_ONLY); |
3f48686f | 1208 | qdict_copy_default(child_options, parent_options, BDRV_OPT_DISCARD); |
f87a0e29 | 1209 | |
41869044 KW |
1210 | /* aio=native doesn't work for cache.direct=off, so disable it for the |
1211 | * temporary snapshot */ | |
1212 | *child_flags &= ~BDRV_O_NATIVE_AIO; | |
b1e6fc08 KW |
1213 | } |
1214 | ||
db95dbba KW |
1215 | static void bdrv_backing_attach(BdrvChild *c) |
1216 | { | |
1217 | BlockDriverState *parent = c->opaque; | |
1218 | BlockDriverState *backing_hd = c->bs; | |
1219 | ||
1220 | assert(!parent->backing_blocker); | |
1221 | error_setg(&parent->backing_blocker, | |
1222 | "node is used as backing hd of '%s'", | |
1223 | bdrv_get_device_or_node_name(parent)); | |
1224 | ||
f30c66ba HR |
1225 | bdrv_refresh_filename(backing_hd); |
1226 | ||
db95dbba | 1227 | parent->open_flags &= ~BDRV_O_NO_BACKING; |
db95dbba KW |
1228 | |
1229 | bdrv_op_block_all(backing_hd, parent->backing_blocker); | |
1230 | /* Otherwise we won't be able to commit or stream */ | |
1231 | bdrv_op_unblock(backing_hd, BLOCK_OP_TYPE_COMMIT_TARGET, | |
1232 | parent->backing_blocker); | |
1233 | bdrv_op_unblock(backing_hd, BLOCK_OP_TYPE_STREAM, | |
1234 | parent->backing_blocker); | |
1235 | /* | |
1236 | * We do backup in 3 ways: | |
1237 | * 1. drive backup | |
1238 | * The target bs is new opened, and the source is top BDS | |
1239 | * 2. blockdev backup | |
1240 | * Both the source and the target are top BDSes. | |
1241 | * 3. internal backup(used for block replication) | |
1242 | * Both the source and the target are backing file | |
1243 | * | |
1244 | * In case 1 and 2, neither the source nor the target is the backing file. | |
1245 | * In case 3, we will block the top BDS, so there is only one block job | |
1246 | * for the top BDS and its backing chain. | |
1247 | */ | |
1248 | bdrv_op_unblock(backing_hd, BLOCK_OP_TYPE_BACKUP_SOURCE, | |
1249 | parent->backing_blocker); | |
1250 | bdrv_op_unblock(backing_hd, BLOCK_OP_TYPE_BACKUP_TARGET, | |
1251 | parent->backing_blocker); | |
ca2f1234 | 1252 | } |
d736f119 | 1253 | |
db95dbba KW |
1254 | static void bdrv_backing_detach(BdrvChild *c) |
1255 | { | |
1256 | BlockDriverState *parent = c->opaque; | |
1257 | ||
1258 | assert(parent->backing_blocker); | |
1259 | bdrv_op_unblock_all(c->bs, parent->backing_blocker); | |
1260 | error_free(parent->backing_blocker); | |
1261 | parent->backing_blocker = NULL; | |
48e08288 | 1262 | } |
d736f119 | 1263 | |
6858eba0 KW |
1264 | static int bdrv_backing_update_filename(BdrvChild *c, BlockDriverState *base, |
1265 | const char *filename, Error **errp) | |
1266 | { | |
1267 | BlockDriverState *parent = c->opaque; | |
e94d3dba | 1268 | bool read_only = bdrv_is_read_only(parent); |
6858eba0 KW |
1269 | int ret; |
1270 | ||
e94d3dba AG |
1271 | if (read_only) { |
1272 | ret = bdrv_reopen_set_read_only(parent, false, errp); | |
61f09cea KW |
1273 | if (ret < 0) { |
1274 | return ret; | |
1275 | } | |
1276 | } | |
1277 | ||
6858eba0 | 1278 | ret = bdrv_change_backing_file(parent, filename, |
e54ee1b3 EB |
1279 | base->drv ? base->drv->format_name : "", |
1280 | false); | |
6858eba0 | 1281 | if (ret < 0) { |
64730694 | 1282 | error_setg_errno(errp, -ret, "Could not update backing file link"); |
6858eba0 KW |
1283 | } |
1284 | ||
e94d3dba AG |
1285 | if (read_only) { |
1286 | bdrv_reopen_set_read_only(parent, true, NULL); | |
61f09cea KW |
1287 | } |
1288 | ||
6858eba0 KW |
1289 | return ret; |
1290 | } | |
1291 | ||
fae8bd39 HR |
1292 | /* |
1293 | * Returns the options and flags that a generic child of a BDS should | |
1294 | * get, based on the given options and flags for the parent BDS. | |
1295 | */ | |
00ff7ffd HR |
1296 | static void bdrv_inherited_options(BdrvChildRole role, bool parent_is_format, |
1297 | int *child_flags, QDict *child_options, | |
1298 | int parent_flags, QDict *parent_options) | |
fae8bd39 HR |
1299 | { |
1300 | int flags = parent_flags; | |
1301 | ||
1302 | /* | |
1303 | * First, decide whether to set, clear, or leave BDRV_O_PROTOCOL. | |
1304 | * Generally, the question to answer is: Should this child be | |
1305 | * format-probed by default? | |
1306 | */ | |
1307 | ||
1308 | /* | |
1309 | * Pure and non-filtered data children of non-format nodes should | |
1310 | * be probed by default (even when the node itself has BDRV_O_PROTOCOL | |
1311 | * set). This only affects a very limited set of drivers (namely | |
1312 | * quorum and blkverify when this comment was written). | |
1313 | * Force-clear BDRV_O_PROTOCOL then. | |
1314 | */ | |
1315 | if (!parent_is_format && | |
1316 | (role & BDRV_CHILD_DATA) && | |
1317 | !(role & (BDRV_CHILD_METADATA | BDRV_CHILD_FILTERED))) | |
1318 | { | |
1319 | flags &= ~BDRV_O_PROTOCOL; | |
1320 | } | |
1321 | ||
1322 | /* | |
1323 | * All children of format nodes (except for COW children) and all | |
1324 | * metadata children in general should never be format-probed. | |
1325 | * Force-set BDRV_O_PROTOCOL then. | |
1326 | */ | |
1327 | if ((parent_is_format && !(role & BDRV_CHILD_COW)) || | |
1328 | (role & BDRV_CHILD_METADATA)) | |
1329 | { | |
1330 | flags |= BDRV_O_PROTOCOL; | |
1331 | } | |
1332 | ||
1333 | /* | |
1334 | * If the cache mode isn't explicitly set, inherit direct and no-flush from | |
1335 | * the parent. | |
1336 | */ | |
1337 | qdict_copy_default(child_options, parent_options, BDRV_OPT_CACHE_DIRECT); | |
1338 | qdict_copy_default(child_options, parent_options, BDRV_OPT_CACHE_NO_FLUSH); | |
1339 | qdict_copy_default(child_options, parent_options, BDRV_OPT_FORCE_SHARE); | |
1340 | ||
1341 | if (role & BDRV_CHILD_COW) { | |
1342 | /* backing files are opened read-only by default */ | |
1343 | qdict_set_default_str(child_options, BDRV_OPT_READ_ONLY, "on"); | |
1344 | qdict_set_default_str(child_options, BDRV_OPT_AUTO_READ_ONLY, "off"); | |
1345 | } else { | |
1346 | /* Inherit the read-only option from the parent if it's not set */ | |
1347 | qdict_copy_default(child_options, parent_options, BDRV_OPT_READ_ONLY); | |
1348 | qdict_copy_default(child_options, parent_options, | |
1349 | BDRV_OPT_AUTO_READ_ONLY); | |
1350 | } | |
1351 | ||
1352 | /* | |
1353 | * bdrv_co_pdiscard() respects unmap policy for the parent, so we | |
1354 | * can default to enable it on lower layers regardless of the | |
1355 | * parent option. | |
1356 | */ | |
1357 | qdict_set_default_str(child_options, BDRV_OPT_DISCARD, "unmap"); | |
1358 | ||
1359 | /* Clear flags that only apply to the top layer */ | |
1360 | flags &= ~(BDRV_O_SNAPSHOT | BDRV_O_NO_BACKING | BDRV_O_COPY_ON_READ); | |
1361 | ||
1362 | if (role & BDRV_CHILD_METADATA) { | |
1363 | flags &= ~BDRV_O_NO_IO; | |
1364 | } | |
1365 | if (role & BDRV_CHILD_COW) { | |
1366 | flags &= ~BDRV_O_TEMPORARY; | |
1367 | } | |
1368 | ||
1369 | *child_flags = flags; | |
1370 | } | |
1371 | ||
ca2f1234 HR |
1372 | static void bdrv_child_cb_attach(BdrvChild *child) |
1373 | { | |
1374 | BlockDriverState *bs = child->opaque; | |
1375 | ||
1376 | if (child->role & BDRV_CHILD_COW) { | |
1377 | bdrv_backing_attach(child); | |
1378 | } | |
1379 | ||
1380 | bdrv_apply_subtree_drain(child, bs); | |
1381 | } | |
1382 | ||
48e08288 HR |
1383 | static void bdrv_child_cb_detach(BdrvChild *child) |
1384 | { | |
1385 | BlockDriverState *bs = child->opaque; | |
1386 | ||
1387 | if (child->role & BDRV_CHILD_COW) { | |
1388 | bdrv_backing_detach(child); | |
1389 | } | |
1390 | ||
1391 | bdrv_unapply_subtree_drain(child, bs); | |
1392 | } | |
1393 | ||
43483550 HR |
1394 | static int bdrv_child_cb_update_filename(BdrvChild *c, BlockDriverState *base, |
1395 | const char *filename, Error **errp) | |
1396 | { | |
1397 | if (c->role & BDRV_CHILD_COW) { | |
1398 | return bdrv_backing_update_filename(c, base, filename, errp); | |
1399 | } | |
1400 | return 0; | |
1401 | } | |
1402 | ||
3ca1f322 VSO |
1403 | static AioContext *bdrv_child_cb_get_parent_aio_context(BdrvChild *c) |
1404 | { | |
1405 | BlockDriverState *bs = c->opaque; | |
1406 | ||
1407 | return bdrv_get_aio_context(bs); | |
1408 | } | |
1409 | ||
43483550 HR |
1410 | const BdrvChildClass child_of_bds = { |
1411 | .parent_is_bds = true, | |
1412 | .get_parent_desc = bdrv_child_get_parent_desc, | |
1413 | .inherit_options = bdrv_inherited_options, | |
1414 | .drained_begin = bdrv_child_cb_drained_begin, | |
1415 | .drained_poll = bdrv_child_cb_drained_poll, | |
1416 | .drained_end = bdrv_child_cb_drained_end, | |
1417 | .attach = bdrv_child_cb_attach, | |
1418 | .detach = bdrv_child_cb_detach, | |
1419 | .inactivate = bdrv_child_cb_inactivate, | |
1420 | .can_set_aio_ctx = bdrv_child_cb_can_set_aio_ctx, | |
1421 | .set_aio_ctx = bdrv_child_cb_set_aio_ctx, | |
1422 | .update_filename = bdrv_child_cb_update_filename, | |
3ca1f322 | 1423 | .get_parent_aio_context = bdrv_child_cb_get_parent_aio_context, |
43483550 HR |
1424 | }; |
1425 | ||
3ca1f322 VSO |
1426 | AioContext *bdrv_child_get_parent_aio_context(BdrvChild *c) |
1427 | { | |
1428 | return c->klass->get_parent_aio_context(c); | |
1429 | } | |
1430 | ||
7b272452 KW |
1431 | static int bdrv_open_flags(BlockDriverState *bs, int flags) |
1432 | { | |
61de4c68 | 1433 | int open_flags = flags; |
7b272452 KW |
1434 | |
1435 | /* | |
1436 | * Clear flags that are internal to the block layer before opening the | |
1437 | * image. | |
1438 | */ | |
20cca275 | 1439 | open_flags &= ~(BDRV_O_SNAPSHOT | BDRV_O_NO_BACKING | BDRV_O_PROTOCOL); |
7b272452 | 1440 | |
7b272452 KW |
1441 | return open_flags; |
1442 | } | |
1443 | ||
91a097e7 KW |
1444 | static void update_flags_from_options(int *flags, QemuOpts *opts) |
1445 | { | |
2a3d4331 | 1446 | *flags &= ~(BDRV_O_CACHE_MASK | BDRV_O_RDWR | BDRV_O_AUTO_RDONLY); |
91a097e7 | 1447 | |
57f9db9a | 1448 | if (qemu_opt_get_bool_del(opts, BDRV_OPT_CACHE_NO_FLUSH, false)) { |
91a097e7 KW |
1449 | *flags |= BDRV_O_NO_FLUSH; |
1450 | } | |
1451 | ||
57f9db9a | 1452 | if (qemu_opt_get_bool_del(opts, BDRV_OPT_CACHE_DIRECT, false)) { |
91a097e7 KW |
1453 | *flags |= BDRV_O_NOCACHE; |
1454 | } | |
f87a0e29 | 1455 | |
57f9db9a | 1456 | if (!qemu_opt_get_bool_del(opts, BDRV_OPT_READ_ONLY, false)) { |
f87a0e29 AG |
1457 | *flags |= BDRV_O_RDWR; |
1458 | } | |
1459 | ||
e35bdc12 KW |
1460 | if (qemu_opt_get_bool_del(opts, BDRV_OPT_AUTO_READ_ONLY, false)) { |
1461 | *flags |= BDRV_O_AUTO_RDONLY; | |
1462 | } | |
91a097e7 KW |
1463 | } |
1464 | ||
1465 | static void update_options_from_flags(QDict *options, int flags) | |
1466 | { | |
91a097e7 | 1467 | if (!qdict_haskey(options, BDRV_OPT_CACHE_DIRECT)) { |
46f5ac20 | 1468 | qdict_put_bool(options, BDRV_OPT_CACHE_DIRECT, flags & BDRV_O_NOCACHE); |
91a097e7 KW |
1469 | } |
1470 | if (!qdict_haskey(options, BDRV_OPT_CACHE_NO_FLUSH)) { | |
46f5ac20 EB |
1471 | qdict_put_bool(options, BDRV_OPT_CACHE_NO_FLUSH, |
1472 | flags & BDRV_O_NO_FLUSH); | |
91a097e7 | 1473 | } |
f87a0e29 | 1474 | if (!qdict_haskey(options, BDRV_OPT_READ_ONLY)) { |
46f5ac20 | 1475 | qdict_put_bool(options, BDRV_OPT_READ_ONLY, !(flags & BDRV_O_RDWR)); |
f87a0e29 | 1476 | } |
e35bdc12 KW |
1477 | if (!qdict_haskey(options, BDRV_OPT_AUTO_READ_ONLY)) { |
1478 | qdict_put_bool(options, BDRV_OPT_AUTO_READ_ONLY, | |
1479 | flags & BDRV_O_AUTO_RDONLY); | |
1480 | } | |
91a097e7 KW |
1481 | } |
1482 | ||
636ea370 KW |
1483 | static void bdrv_assign_node_name(BlockDriverState *bs, |
1484 | const char *node_name, | |
1485 | Error **errp) | |
6913c0c2 | 1486 | { |
15489c76 | 1487 | char *gen_node_name = NULL; |
6913c0c2 | 1488 | |
15489c76 JC |
1489 | if (!node_name) { |
1490 | node_name = gen_node_name = id_generate(ID_BLOCK); | |
1491 | } else if (!id_wellformed(node_name)) { | |
1492 | /* | |
1493 | * Check for empty string or invalid characters, but not if it is | |
1494 | * generated (generated names use characters not available to the user) | |
1495 | */ | |
785ec4b1 | 1496 | error_setg(errp, "Invalid node-name: '%s'", node_name); |
636ea370 | 1497 | return; |
6913c0c2 BC |
1498 | } |
1499 | ||
0c5e94ee | 1500 | /* takes care of avoiding namespaces collisions */ |
7f06d47e | 1501 | if (blk_by_name(node_name)) { |
0c5e94ee BC |
1502 | error_setg(errp, "node-name=%s is conflicting with a device id", |
1503 | node_name); | |
15489c76 | 1504 | goto out; |
0c5e94ee BC |
1505 | } |
1506 | ||
6913c0c2 BC |
1507 | /* takes care of avoiding duplicates node names */ |
1508 | if (bdrv_find_node(node_name)) { | |
785ec4b1 | 1509 | error_setg(errp, "Duplicate nodes with node-name='%s'", node_name); |
15489c76 | 1510 | goto out; |
6913c0c2 BC |
1511 | } |
1512 | ||
824808dd KW |
1513 | /* Make sure that the node name isn't truncated */ |
1514 | if (strlen(node_name) >= sizeof(bs->node_name)) { | |
1515 | error_setg(errp, "Node name too long"); | |
1516 | goto out; | |
1517 | } | |
1518 | ||
6913c0c2 BC |
1519 | /* copy node name into the bs and insert it into the graph list */ |
1520 | pstrcpy(bs->node_name, sizeof(bs->node_name), node_name); | |
1521 | QTAILQ_INSERT_TAIL(&graph_bdrv_states, bs, node_list); | |
15489c76 JC |
1522 | out: |
1523 | g_free(gen_node_name); | |
6913c0c2 BC |
1524 | } |
1525 | ||
01a56501 KW |
1526 | static int bdrv_open_driver(BlockDriverState *bs, BlockDriver *drv, |
1527 | const char *node_name, QDict *options, | |
1528 | int open_flags, Error **errp) | |
1529 | { | |
1530 | Error *local_err = NULL; | |
0f12264e | 1531 | int i, ret; |
01a56501 KW |
1532 | |
1533 | bdrv_assign_node_name(bs, node_name, &local_err); | |
1534 | if (local_err) { | |
1535 | error_propagate(errp, local_err); | |
1536 | return -EINVAL; | |
1537 | } | |
1538 | ||
1539 | bs->drv = drv; | |
680c7f96 | 1540 | bs->read_only = !(bs->open_flags & BDRV_O_RDWR); |
01a56501 KW |
1541 | bs->opaque = g_malloc0(drv->instance_size); |
1542 | ||
1543 | if (drv->bdrv_file_open) { | |
1544 | assert(!drv->bdrv_needs_filename || bs->filename[0]); | |
1545 | ret = drv->bdrv_file_open(bs, options, open_flags, &local_err); | |
680c7f96 | 1546 | } else if (drv->bdrv_open) { |
01a56501 | 1547 | ret = drv->bdrv_open(bs, options, open_flags, &local_err); |
680c7f96 KW |
1548 | } else { |
1549 | ret = 0; | |
01a56501 KW |
1550 | } |
1551 | ||
1552 | if (ret < 0) { | |
1553 | if (local_err) { | |
1554 | error_propagate(errp, local_err); | |
1555 | } else if (bs->filename[0]) { | |
1556 | error_setg_errno(errp, -ret, "Could not open '%s'", bs->filename); | |
1557 | } else { | |
1558 | error_setg_errno(errp, -ret, "Could not open image"); | |
1559 | } | |
180ca19a | 1560 | goto open_failed; |
01a56501 KW |
1561 | } |
1562 | ||
1563 | ret = refresh_total_sectors(bs, bs->total_sectors); | |
1564 | if (ret < 0) { | |
1565 | error_setg_errno(errp, -ret, "Could not refresh total sector count"); | |
180ca19a | 1566 | return ret; |
01a56501 KW |
1567 | } |
1568 | ||
1569 | bdrv_refresh_limits(bs, &local_err); | |
1570 | if (local_err) { | |
1571 | error_propagate(errp, local_err); | |
180ca19a | 1572 | return -EINVAL; |
01a56501 KW |
1573 | } |
1574 | ||
1575 | assert(bdrv_opt_mem_align(bs) != 0); | |
1576 | assert(bdrv_min_mem_align(bs) != 0); | |
1577 | assert(is_power_of_2(bs->bl.request_alignment)); | |
1578 | ||
0f12264e KW |
1579 | for (i = 0; i < bs->quiesce_counter; i++) { |
1580 | if (drv->bdrv_co_drain_begin) { | |
1581 | drv->bdrv_co_drain_begin(bs); | |
1582 | } | |
1583 | } | |
1584 | ||
01a56501 | 1585 | return 0; |
180ca19a MP |
1586 | open_failed: |
1587 | bs->drv = NULL; | |
1588 | if (bs->file != NULL) { | |
1589 | bdrv_unref_child(bs, bs->file); | |
1590 | bs->file = NULL; | |
1591 | } | |
01a56501 KW |
1592 | g_free(bs->opaque); |
1593 | bs->opaque = NULL; | |
01a56501 KW |
1594 | return ret; |
1595 | } | |
1596 | ||
680c7f96 KW |
1597 | BlockDriverState *bdrv_new_open_driver(BlockDriver *drv, const char *node_name, |
1598 | int flags, Error **errp) | |
1599 | { | |
1600 | BlockDriverState *bs; | |
1601 | int ret; | |
1602 | ||
1603 | bs = bdrv_new(); | |
1604 | bs->open_flags = flags; | |
1605 | bs->explicit_options = qdict_new(); | |
1606 | bs->options = qdict_new(); | |
1607 | bs->opaque = NULL; | |
1608 | ||
1609 | update_options_from_flags(bs->options, flags); | |
1610 | ||
1611 | ret = bdrv_open_driver(bs, drv, node_name, bs->options, flags, errp); | |
1612 | if (ret < 0) { | |
cb3e7f08 | 1613 | qobject_unref(bs->explicit_options); |
180ca19a | 1614 | bs->explicit_options = NULL; |
cb3e7f08 | 1615 | qobject_unref(bs->options); |
180ca19a | 1616 | bs->options = NULL; |
680c7f96 KW |
1617 | bdrv_unref(bs); |
1618 | return NULL; | |
1619 | } | |
1620 | ||
1621 | return bs; | |
1622 | } | |
1623 | ||
c5f3014b | 1624 | QemuOptsList bdrv_runtime_opts = { |
18edf289 KW |
1625 | .name = "bdrv_common", |
1626 | .head = QTAILQ_HEAD_INITIALIZER(bdrv_runtime_opts.head), | |
1627 | .desc = { | |
1628 | { | |
1629 | .name = "node-name", | |
1630 | .type = QEMU_OPT_STRING, | |
1631 | .help = "Node name of the block device node", | |
1632 | }, | |
62392ebb KW |
1633 | { |
1634 | .name = "driver", | |
1635 | .type = QEMU_OPT_STRING, | |
1636 | .help = "Block driver to use for the node", | |
1637 | }, | |
91a097e7 KW |
1638 | { |
1639 | .name = BDRV_OPT_CACHE_DIRECT, | |
1640 | .type = QEMU_OPT_BOOL, | |
1641 | .help = "Bypass software writeback cache on the host", | |
1642 | }, | |
1643 | { | |
1644 | .name = BDRV_OPT_CACHE_NO_FLUSH, | |
1645 | .type = QEMU_OPT_BOOL, | |
1646 | .help = "Ignore flush requests", | |
1647 | }, | |
f87a0e29 AG |
1648 | { |
1649 | .name = BDRV_OPT_READ_ONLY, | |
1650 | .type = QEMU_OPT_BOOL, | |
1651 | .help = "Node is opened in read-only mode", | |
1652 | }, | |
e35bdc12 KW |
1653 | { |
1654 | .name = BDRV_OPT_AUTO_READ_ONLY, | |
1655 | .type = QEMU_OPT_BOOL, | |
1656 | .help = "Node can become read-only if opening read-write fails", | |
1657 | }, | |
692e01a2 KW |
1658 | { |
1659 | .name = "detect-zeroes", | |
1660 | .type = QEMU_OPT_STRING, | |
1661 | .help = "try to optimize zero writes (off, on, unmap)", | |
1662 | }, | |
818584a4 | 1663 | { |
415bbca8 | 1664 | .name = BDRV_OPT_DISCARD, |
818584a4 KW |
1665 | .type = QEMU_OPT_STRING, |
1666 | .help = "discard operation (ignore/off, unmap/on)", | |
1667 | }, | |
5a9347c6 FZ |
1668 | { |
1669 | .name = BDRV_OPT_FORCE_SHARE, | |
1670 | .type = QEMU_OPT_BOOL, | |
1671 | .help = "always accept other writers (default: off)", | |
1672 | }, | |
18edf289 KW |
1673 | { /* end of list */ } |
1674 | }, | |
1675 | }; | |
1676 | ||
5a5e7f8c ML |
1677 | QemuOptsList bdrv_create_opts_simple = { |
1678 | .name = "simple-create-opts", | |
1679 | .head = QTAILQ_HEAD_INITIALIZER(bdrv_create_opts_simple.head), | |
fd17146c HR |
1680 | .desc = { |
1681 | { | |
1682 | .name = BLOCK_OPT_SIZE, | |
1683 | .type = QEMU_OPT_SIZE, | |
1684 | .help = "Virtual disk size" | |
1685 | }, | |
1686 | { | |
1687 | .name = BLOCK_OPT_PREALLOC, | |
1688 | .type = QEMU_OPT_STRING, | |
1689 | .help = "Preallocation mode (allowed values: off)" | |
1690 | }, | |
1691 | { /* end of list */ } | |
1692 | } | |
1693 | }; | |
1694 | ||
57915332 KW |
1695 | /* |
1696 | * Common part for opening disk images and files | |
b6ad491a KW |
1697 | * |
1698 | * Removes all processed options from *options. | |
57915332 | 1699 | */ |
5696c6e3 | 1700 | static int bdrv_open_common(BlockDriverState *bs, BlockBackend *file, |
82dc8b41 | 1701 | QDict *options, Error **errp) |
57915332 KW |
1702 | { |
1703 | int ret, open_flags; | |
035fccdf | 1704 | const char *filename; |
62392ebb | 1705 | const char *driver_name = NULL; |
6913c0c2 | 1706 | const char *node_name = NULL; |
818584a4 | 1707 | const char *discard; |
18edf289 | 1708 | QemuOpts *opts; |
62392ebb | 1709 | BlockDriver *drv; |
34b5d2c6 | 1710 | Error *local_err = NULL; |
57915332 | 1711 | |
6405875c | 1712 | assert(bs->file == NULL); |
707ff828 | 1713 | assert(options != NULL && bs->options != options); |
57915332 | 1714 | |
62392ebb | 1715 | opts = qemu_opts_create(&bdrv_runtime_opts, NULL, 0, &error_abort); |
af175e85 | 1716 | if (!qemu_opts_absorb_qdict(opts, options, errp)) { |
62392ebb KW |
1717 | ret = -EINVAL; |
1718 | goto fail_opts; | |
1719 | } | |
1720 | ||
9b7e8691 AG |
1721 | update_flags_from_options(&bs->open_flags, opts); |
1722 | ||
62392ebb KW |
1723 | driver_name = qemu_opt_get(opts, "driver"); |
1724 | drv = bdrv_find_format(driver_name); | |
1725 | assert(drv != NULL); | |
1726 | ||
5a9347c6 FZ |
1727 | bs->force_share = qemu_opt_get_bool(opts, BDRV_OPT_FORCE_SHARE, false); |
1728 | ||
1729 | if (bs->force_share && (bs->open_flags & BDRV_O_RDWR)) { | |
1730 | error_setg(errp, | |
1731 | BDRV_OPT_FORCE_SHARE | |
1732 | "=on can only be used with read-only images"); | |
1733 | ret = -EINVAL; | |
1734 | goto fail_opts; | |
1735 | } | |
1736 | ||
45673671 | 1737 | if (file != NULL) { |
f30c66ba | 1738 | bdrv_refresh_filename(blk_bs(file)); |
5696c6e3 | 1739 | filename = blk_bs(file)->filename; |
45673671 | 1740 | } else { |
129c7d1c MA |
1741 | /* |
1742 | * Caution: while qdict_get_try_str() is fine, getting | |
1743 | * non-string types would require more care. When @options | |
1744 | * come from -blockdev or blockdev_add, its members are typed | |
1745 | * according to the QAPI schema, but when they come from | |
1746 | * -drive, they're all QString. | |
1747 | */ | |
45673671 KW |
1748 | filename = qdict_get_try_str(options, "filename"); |
1749 | } | |
1750 | ||
4a008240 | 1751 | if (drv->bdrv_needs_filename && (!filename || !filename[0])) { |
765003db KW |
1752 | error_setg(errp, "The '%s' block driver requires a file name", |
1753 | drv->format_name); | |
18edf289 KW |
1754 | ret = -EINVAL; |
1755 | goto fail_opts; | |
6913c0c2 | 1756 | } |
6913c0c2 | 1757 | |
82dc8b41 KW |
1758 | trace_bdrv_open_common(bs, filename ?: "", bs->open_flags, |
1759 | drv->format_name); | |
62392ebb | 1760 | |
82dc8b41 | 1761 | bs->read_only = !(bs->open_flags & BDRV_O_RDWR); |
b64ec4e4 FZ |
1762 | |
1763 | if (use_bdrv_whitelist && !bdrv_is_whitelisted(drv, bs->read_only)) { | |
8be25de6 KW |
1764 | if (!bs->read_only && bdrv_is_whitelisted(drv, true)) { |
1765 | ret = bdrv_apply_auto_read_only(bs, NULL, NULL); | |
1766 | } else { | |
1767 | ret = -ENOTSUP; | |
1768 | } | |
1769 | if (ret < 0) { | |
1770 | error_setg(errp, | |
1771 | !bs->read_only && bdrv_is_whitelisted(drv, true) | |
1772 | ? "Driver '%s' can only be used for read-only devices" | |
1773 | : "Driver '%s' is not whitelisted", | |
1774 | drv->format_name); | |
1775 | goto fail_opts; | |
1776 | } | |
b64ec4e4 | 1777 | } |
57915332 | 1778 | |
d3faa13e | 1779 | /* bdrv_new() and bdrv_close() make it so */ |
d73415a3 | 1780 | assert(qatomic_read(&bs->copy_on_read) == 0); |
d3faa13e | 1781 | |
82dc8b41 | 1782 | if (bs->open_flags & BDRV_O_COPY_ON_READ) { |
0ebd24e0 KW |
1783 | if (!bs->read_only) { |
1784 | bdrv_enable_copy_on_read(bs); | |
1785 | } else { | |
1786 | error_setg(errp, "Can't use copy-on-read on read-only device"); | |
18edf289 KW |
1787 | ret = -EINVAL; |
1788 | goto fail_opts; | |
0ebd24e0 | 1789 | } |
53fec9d3 SH |
1790 | } |
1791 | ||
415bbca8 | 1792 | discard = qemu_opt_get(opts, BDRV_OPT_DISCARD); |
818584a4 KW |
1793 | if (discard != NULL) { |
1794 | if (bdrv_parse_discard_flags(discard, &bs->open_flags) != 0) { | |
1795 | error_setg(errp, "Invalid discard option"); | |
1796 | ret = -EINVAL; | |
1797 | goto fail_opts; | |
1798 | } | |
1799 | } | |
1800 | ||
543770bd AG |
1801 | bs->detect_zeroes = |
1802 | bdrv_parse_detect_zeroes(opts, bs->open_flags, &local_err); | |
1803 | if (local_err) { | |
1804 | error_propagate(errp, local_err); | |
1805 | ret = -EINVAL; | |
1806 | goto fail_opts; | |
692e01a2 KW |
1807 | } |
1808 | ||
c2ad1b0c KW |
1809 | if (filename != NULL) { |
1810 | pstrcpy(bs->filename, sizeof(bs->filename), filename); | |
1811 | } else { | |
1812 | bs->filename[0] = '\0'; | |
1813 | } | |
91af7014 | 1814 | pstrcpy(bs->exact_filename, sizeof(bs->exact_filename), bs->filename); |
57915332 | 1815 | |
66f82cee | 1816 | /* Open the image, either directly or using a protocol */ |
82dc8b41 | 1817 | open_flags = bdrv_open_flags(bs, bs->open_flags); |
01a56501 | 1818 | node_name = qemu_opt_get(opts, "node-name"); |
57915332 | 1819 | |
01a56501 KW |
1820 | assert(!drv->bdrv_file_open || file == NULL); |
1821 | ret = bdrv_open_driver(bs, drv, node_name, options, open_flags, errp); | |
51762288 | 1822 | if (ret < 0) { |
01a56501 | 1823 | goto fail_opts; |
3baca891 KW |
1824 | } |
1825 | ||
18edf289 | 1826 | qemu_opts_del(opts); |
57915332 KW |
1827 | return 0; |
1828 | ||
18edf289 KW |
1829 | fail_opts: |
1830 | qemu_opts_del(opts); | |
57915332 KW |
1831 | return ret; |
1832 | } | |
1833 | ||
5e5c4f63 KW |
1834 | static QDict *parse_json_filename(const char *filename, Error **errp) |
1835 | { | |
1836 | QObject *options_obj; | |
1837 | QDict *options; | |
1838 | int ret; | |
1839 | ||
1840 | ret = strstart(filename, "json:", &filename); | |
1841 | assert(ret); | |
1842 | ||
5577fff7 | 1843 | options_obj = qobject_from_json(filename, errp); |
5e5c4f63 | 1844 | if (!options_obj) { |
5577fff7 | 1845 | error_prepend(errp, "Could not parse the JSON options: "); |
5e5c4f63 KW |
1846 | return NULL; |
1847 | } | |
1848 | ||
7dc847eb | 1849 | options = qobject_to(QDict, options_obj); |
ca6b6e1e | 1850 | if (!options) { |
cb3e7f08 | 1851 | qobject_unref(options_obj); |
5e5c4f63 KW |
1852 | error_setg(errp, "Invalid JSON object given"); |
1853 | return NULL; | |
1854 | } | |
1855 | ||
5e5c4f63 KW |
1856 | qdict_flatten(options); |
1857 | ||
1858 | return options; | |
1859 | } | |
1860 | ||
de3b53f0 KW |
1861 | static void parse_json_protocol(QDict *options, const char **pfilename, |
1862 | Error **errp) | |
1863 | { | |
1864 | QDict *json_options; | |
1865 | Error *local_err = NULL; | |
1866 | ||
1867 | /* Parse json: pseudo-protocol */ | |
1868 | if (!*pfilename || !g_str_has_prefix(*pfilename, "json:")) { | |
1869 | return; | |
1870 | } | |
1871 | ||
1872 | json_options = parse_json_filename(*pfilename, &local_err); | |
1873 | if (local_err) { | |
1874 | error_propagate(errp, local_err); | |
1875 | return; | |
1876 | } | |
1877 | ||
1878 | /* Options given in the filename have lower priority than options | |
1879 | * specified directly */ | |
1880 | qdict_join(options, json_options, false); | |
cb3e7f08 | 1881 | qobject_unref(json_options); |
de3b53f0 KW |
1882 | *pfilename = NULL; |
1883 | } | |
1884 | ||
b6ce07aa | 1885 | /* |
f54120ff KW |
1886 | * Fills in default options for opening images and converts the legacy |
1887 | * filename/flags pair to option QDict entries. | |
53a29513 HR |
1888 | * The BDRV_O_PROTOCOL flag in *flags will be set or cleared accordingly if a |
1889 | * block driver has been specified explicitly. | |
b6ce07aa | 1890 | */ |
de3b53f0 | 1891 | static int bdrv_fill_options(QDict **options, const char *filename, |
053e1578 | 1892 | int *flags, Error **errp) |
ea2384d3 | 1893 | { |
c2ad1b0c | 1894 | const char *drvname; |
53a29513 | 1895 | bool protocol = *flags & BDRV_O_PROTOCOL; |
e3fa4bfa | 1896 | bool parse_filename = false; |
053e1578 | 1897 | BlockDriver *drv = NULL; |
34b5d2c6 | 1898 | Error *local_err = NULL; |
83f64091 | 1899 | |
129c7d1c MA |
1900 | /* |
1901 | * Caution: while qdict_get_try_str() is fine, getting non-string | |
1902 | * types would require more care. When @options come from | |
1903 | * -blockdev or blockdev_add, its members are typed according to | |
1904 | * the QAPI schema, but when they come from -drive, they're all | |
1905 | * QString. | |
1906 | */ | |
53a29513 | 1907 | drvname = qdict_get_try_str(*options, "driver"); |
053e1578 HR |
1908 | if (drvname) { |
1909 | drv = bdrv_find_format(drvname); | |
1910 | if (!drv) { | |
1911 | error_setg(errp, "Unknown driver '%s'", drvname); | |
1912 | return -ENOENT; | |
1913 | } | |
1914 | /* If the user has explicitly specified the driver, this choice should | |
1915 | * override the BDRV_O_PROTOCOL flag */ | |
1916 | protocol = drv->bdrv_file_open; | |
53a29513 HR |
1917 | } |
1918 | ||
1919 | if (protocol) { | |
1920 | *flags |= BDRV_O_PROTOCOL; | |
1921 | } else { | |
1922 | *flags &= ~BDRV_O_PROTOCOL; | |
1923 | } | |
1924 | ||
91a097e7 KW |
1925 | /* Translate cache options from flags into options */ |
1926 | update_options_from_flags(*options, *flags); | |
1927 | ||
035fccdf | 1928 | /* Fetch the file name from the options QDict if necessary */ |
17b005f1 | 1929 | if (protocol && filename) { |
f54120ff | 1930 | if (!qdict_haskey(*options, "filename")) { |
46f5ac20 | 1931 | qdict_put_str(*options, "filename", filename); |
f54120ff KW |
1932 | parse_filename = true; |
1933 | } else { | |
1934 | error_setg(errp, "Can't specify 'file' and 'filename' options at " | |
1935 | "the same time"); | |
1936 | return -EINVAL; | |
1937 | } | |
035fccdf KW |
1938 | } |
1939 | ||
c2ad1b0c | 1940 | /* Find the right block driver */ |
129c7d1c | 1941 | /* See cautionary note on accessing @options above */ |
f54120ff | 1942 | filename = qdict_get_try_str(*options, "filename"); |
f54120ff | 1943 | |
053e1578 HR |
1944 | if (!drvname && protocol) { |
1945 | if (filename) { | |
1946 | drv = bdrv_find_protocol(filename, parse_filename, errp); | |
17b005f1 | 1947 | if (!drv) { |
053e1578 | 1948 | return -EINVAL; |
17b005f1 | 1949 | } |
053e1578 HR |
1950 | |
1951 | drvname = drv->format_name; | |
46f5ac20 | 1952 | qdict_put_str(*options, "driver", drvname); |
053e1578 HR |
1953 | } else { |
1954 | error_setg(errp, "Must specify either driver or file"); | |
1955 | return -EINVAL; | |
98289620 | 1956 | } |
c2ad1b0c KW |
1957 | } |
1958 | ||
17b005f1 | 1959 | assert(drv || !protocol); |
c2ad1b0c | 1960 | |
f54120ff | 1961 | /* Driver-specific filename parsing */ |
17b005f1 | 1962 | if (drv && drv->bdrv_parse_filename && parse_filename) { |
5acd9d81 | 1963 | drv->bdrv_parse_filename(filename, *options, &local_err); |
84d18f06 | 1964 | if (local_err) { |
34b5d2c6 | 1965 | error_propagate(errp, local_err); |
f54120ff | 1966 | return -EINVAL; |
6963a30d | 1967 | } |
cd5d031e HR |
1968 | |
1969 | if (!drv->bdrv_needs_filename) { | |
1970 | qdict_del(*options, "filename"); | |
cd5d031e | 1971 | } |
6963a30d KW |
1972 | } |
1973 | ||
f54120ff KW |
1974 | return 0; |
1975 | } | |
1976 | ||
3ef45e02 VSO |
1977 | static int bdrv_check_update_perm(BlockDriverState *bs, BlockReopenQueue *q, |
1978 | uint64_t new_used_perm, | |
1979 | uint64_t new_shared_perm, | |
1980 | GSList *ignore_children, | |
1981 | Error **errp); | |
c1cef672 | 1982 | |
148eb13c KW |
1983 | typedef struct BlockReopenQueueEntry { |
1984 | bool prepared; | |
69b736e7 | 1985 | bool perms_checked; |
148eb13c | 1986 | BDRVReopenState state; |
859443b0 | 1987 | QTAILQ_ENTRY(BlockReopenQueueEntry) entry; |
148eb13c KW |
1988 | } BlockReopenQueueEntry; |
1989 | ||
1990 | /* | |
1991 | * Return the flags that @bs will have after the reopens in @q have | |
1992 | * successfully completed. If @q is NULL (or @bs is not contained in @q), | |
1993 | * return the current flags. | |
1994 | */ | |
1995 | static int bdrv_reopen_get_flags(BlockReopenQueue *q, BlockDriverState *bs) | |
1996 | { | |
1997 | BlockReopenQueueEntry *entry; | |
1998 | ||
1999 | if (q != NULL) { | |
859443b0 | 2000 | QTAILQ_FOREACH(entry, q, entry) { |
148eb13c KW |
2001 | if (entry->state.bs == bs) { |
2002 | return entry->state.flags; | |
2003 | } | |
2004 | } | |
2005 | } | |
2006 | ||
2007 | return bs->open_flags; | |
2008 | } | |
2009 | ||
2010 | /* Returns whether the image file can be written to after the reopen queue @q | |
2011 | * has been successfully applied, or right now if @q is NULL. */ | |
cc022140 HR |
2012 | static bool bdrv_is_writable_after_reopen(BlockDriverState *bs, |
2013 | BlockReopenQueue *q) | |
148eb13c KW |
2014 | { |
2015 | int flags = bdrv_reopen_get_flags(q, bs); | |
2016 | ||
2017 | return (flags & (BDRV_O_RDWR | BDRV_O_INACTIVE)) == BDRV_O_RDWR; | |
2018 | } | |
2019 | ||
cc022140 HR |
2020 | /* |
2021 | * Return whether the BDS can be written to. This is not necessarily | |
2022 | * the same as !bdrv_is_read_only(bs), as inactivated images may not | |
2023 | * be written to but do not count as read-only images. | |
2024 | */ | |
2025 | bool bdrv_is_writable(BlockDriverState *bs) | |
2026 | { | |
2027 | return bdrv_is_writable_after_reopen(bs, NULL); | |
2028 | } | |
2029 | ||
3bf416ba VSO |
2030 | static char *bdrv_child_user_desc(BdrvChild *c) |
2031 | { | |
2032 | if (c->klass->get_parent_desc) { | |
2033 | return c->klass->get_parent_desc(c); | |
2034 | } | |
2035 | ||
2036 | return g_strdup("another user"); | |
2037 | } | |
2038 | ||
2039 | static bool bdrv_a_allow_b(BdrvChild *a, BdrvChild *b, Error **errp) | |
2040 | { | |
2041 | g_autofree char *user = NULL; | |
2042 | g_autofree char *perm_names = NULL; | |
2043 | ||
2044 | if ((b->perm & a->shared_perm) == b->perm) { | |
2045 | return true; | |
2046 | } | |
2047 | ||
2048 | perm_names = bdrv_perm_names(b->perm & ~a->shared_perm); | |
2049 | user = bdrv_child_user_desc(a); | |
2050 | error_setg(errp, "Conflicts with use by %s as '%s', which does not " | |
2051 | "allow '%s' on %s", | |
2052 | user, a->name, perm_names, bdrv_get_node_name(b->bs)); | |
2053 | ||
2054 | return false; | |
2055 | } | |
2056 | ||
bd57f8f7 VSO |
2057 | static bool bdrv_parent_perms_conflict(BlockDriverState *bs, |
2058 | GSList *ignore_children, | |
2059 | Error **errp) | |
3bf416ba VSO |
2060 | { |
2061 | BdrvChild *a, *b; | |
2062 | ||
2063 | /* | |
2064 | * During the loop we'll look at each pair twice. That's correct because | |
2065 | * bdrv_a_allow_b() is asymmetric and we should check each pair in both | |
2066 | * directions. | |
2067 | */ | |
2068 | QLIST_FOREACH(a, &bs->parents, next_parent) { | |
bd57f8f7 VSO |
2069 | if (g_slist_find(ignore_children, a)) { |
2070 | continue; | |
2071 | } | |
2072 | ||
3bf416ba | 2073 | QLIST_FOREACH(b, &bs->parents, next_parent) { |
bd57f8f7 | 2074 | if (a == b || g_slist_find(ignore_children, b)) { |
3bf416ba VSO |
2075 | continue; |
2076 | } | |
2077 | ||
2078 | if (!bdrv_a_allow_b(a, b, errp)) { | |
2079 | return true; | |
2080 | } | |
2081 | } | |
2082 | } | |
2083 | ||
2084 | return false; | |
2085 | } | |
2086 | ||
ffd1a5a2 | 2087 | static void bdrv_child_perm(BlockDriverState *bs, BlockDriverState *child_bs, |
e5d8a406 HR |
2088 | BdrvChild *c, BdrvChildRole role, |
2089 | BlockReopenQueue *reopen_queue, | |
ffd1a5a2 FZ |
2090 | uint64_t parent_perm, uint64_t parent_shared, |
2091 | uint64_t *nperm, uint64_t *nshared) | |
2092 | { | |
0b3ca76e | 2093 | assert(bs->drv && bs->drv->bdrv_child_perm); |
e5d8a406 | 2094 | bs->drv->bdrv_child_perm(bs, c, role, reopen_queue, |
0b3ca76e AG |
2095 | parent_perm, parent_shared, |
2096 | nperm, nshared); | |
e0995dc3 | 2097 | /* TODO Take force_share from reopen_queue */ |
ffd1a5a2 FZ |
2098 | if (child_bs && child_bs->force_share) { |
2099 | *nshared = BLK_PERM_ALL; | |
2100 | } | |
2101 | } | |
2102 | ||
bd57f8f7 VSO |
2103 | /* |
2104 | * Adds the whole subtree of @bs (including @bs itself) to the @list (except for | |
2105 | * nodes that are already in the @list, of course) so that final list is | |
2106 | * topologically sorted. Return the result (GSList @list object is updated, so | |
2107 | * don't use old reference after function call). | |
2108 | * | |
2109 | * On function start @list must be already topologically sorted and for any node | |
2110 | * in the @list the whole subtree of the node must be in the @list as well. The | |
2111 | * simplest way to satisfy this criteria: use only result of | |
2112 | * bdrv_topological_dfs() or NULL as @list parameter. | |
2113 | */ | |
2114 | static GSList *bdrv_topological_dfs(GSList *list, GHashTable *found, | |
2115 | BlockDriverState *bs) | |
2116 | { | |
2117 | BdrvChild *child; | |
2118 | g_autoptr(GHashTable) local_found = NULL; | |
2119 | ||
2120 | if (!found) { | |
2121 | assert(!list); | |
2122 | found = local_found = g_hash_table_new(NULL, NULL); | |
2123 | } | |
2124 | ||
2125 | if (g_hash_table_contains(found, bs)) { | |
2126 | return list; | |
2127 | } | |
2128 | g_hash_table_add(found, bs); | |
2129 | ||
2130 | QLIST_FOREACH(child, &bs->children, next) { | |
2131 | list = bdrv_topological_dfs(list, found, child->bs); | |
2132 | } | |
2133 | ||
2134 | return g_slist_prepend(list, bs); | |
2135 | } | |
2136 | ||
b0defa83 VSO |
2137 | static void bdrv_child_set_perm_commit(void *opaque) |
2138 | { | |
2139 | BdrvChild *c = opaque; | |
2140 | ||
2141 | c->has_backup_perm = false; | |
2142 | } | |
2143 | ||
2144 | static void bdrv_child_set_perm_abort(void *opaque) | |
2145 | { | |
2146 | BdrvChild *c = opaque; | |
2147 | /* | |
2148 | * We may have child->has_backup_perm unset at this point, as in case of | |
2149 | * _check_ stage of permission update failure we may _check_ not the whole | |
2150 | * subtree. Still, _abort_ is called on the whole subtree anyway. | |
2151 | */ | |
2152 | if (c->has_backup_perm) { | |
2153 | c->perm = c->backup_perm; | |
2154 | c->shared_perm = c->backup_shared_perm; | |
2155 | c->has_backup_perm = false; | |
2156 | } | |
2157 | } | |
2158 | ||
2159 | static TransactionActionDrv bdrv_child_set_pem_drv = { | |
2160 | .abort = bdrv_child_set_perm_abort, | |
2161 | .commit = bdrv_child_set_perm_commit, | |
2162 | }; | |
2163 | ||
2164 | /* | |
2165 | * With tran=NULL needs to be followed by direct call to either | |
2166 | * bdrv_child_set_perm_commit() or bdrv_child_set_perm_abort(). | |
2167 | * | |
2168 | * With non-NULL tran needs to be followed by tran_abort() or tran_commit() | |
2169 | * instead. | |
2170 | */ | |
2171 | static void bdrv_child_set_perm_safe(BdrvChild *c, uint64_t perm, | |
2172 | uint64_t shared, Transaction *tran) | |
2173 | { | |
2174 | if (!c->has_backup_perm) { | |
2175 | c->has_backup_perm = true; | |
2176 | c->backup_perm = c->perm; | |
2177 | c->backup_shared_perm = c->shared_perm; | |
2178 | } | |
2179 | /* | |
2180 | * Note: it's OK if c->has_backup_perm was already set, as we can find the | |
2181 | * same c twice during check_perm procedure | |
2182 | */ | |
2183 | ||
2184 | c->perm = perm; | |
2185 | c->shared_perm = shared; | |
2186 | ||
2187 | if (tran) { | |
2188 | tran_add(tran, &bdrv_child_set_pem_drv, c); | |
2189 | } | |
2190 | } | |
2191 | ||
2513ef59 VSO |
2192 | static void bdrv_drv_set_perm_commit(void *opaque) |
2193 | { | |
2194 | BlockDriverState *bs = opaque; | |
2195 | uint64_t cumulative_perms, cumulative_shared_perms; | |
2196 | ||
2197 | if (bs->drv->bdrv_set_perm) { | |
2198 | bdrv_get_cumulative_perm(bs, &cumulative_perms, | |
2199 | &cumulative_shared_perms); | |
2200 | bs->drv->bdrv_set_perm(bs, cumulative_perms, cumulative_shared_perms); | |
2201 | } | |
2202 | } | |
2203 | ||
2204 | static void bdrv_drv_set_perm_abort(void *opaque) | |
2205 | { | |
2206 | BlockDriverState *bs = opaque; | |
2207 | ||
2208 | if (bs->drv->bdrv_abort_perm_update) { | |
2209 | bs->drv->bdrv_abort_perm_update(bs); | |
2210 | } | |
2211 | } | |
2212 | ||
2213 | TransactionActionDrv bdrv_drv_set_perm_drv = { | |
2214 | .abort = bdrv_drv_set_perm_abort, | |
2215 | .commit = bdrv_drv_set_perm_commit, | |
2216 | }; | |
2217 | ||
2218 | static int bdrv_drv_set_perm(BlockDriverState *bs, uint64_t perm, | |
2219 | uint64_t shared_perm, Transaction *tran, | |
2220 | Error **errp) | |
2221 | { | |
2222 | if (!bs->drv) { | |
2223 | return 0; | |
2224 | } | |
2225 | ||
2226 | if (bs->drv->bdrv_check_perm) { | |
2227 | int ret = bs->drv->bdrv_check_perm(bs, perm, shared_perm, errp); | |
2228 | if (ret < 0) { | |
2229 | return ret; | |
2230 | } | |
2231 | } | |
2232 | ||
2233 | if (tran) { | |
2234 | tran_add(tran, &bdrv_drv_set_perm_drv, bs); | |
2235 | } | |
2236 | ||
2237 | return 0; | |
2238 | } | |
2239 | ||
33a610c3 KW |
2240 | /* |
2241 | * Check whether permissions on this node can be changed in a way that | |
2242 | * @cumulative_perms and @cumulative_shared_perms are the new cumulative | |
2243 | * permissions of all its parents. This involves checking whether all necessary | |
2244 | * permission changes to child nodes can be performed. | |
2245 | * | |
2246 | * A call to this function must always be followed by a call to bdrv_set_perm() | |
2247 | * or bdrv_abort_perm_update(). | |
2248 | */ | |
bd57f8f7 VSO |
2249 | static int bdrv_node_check_perm(BlockDriverState *bs, BlockReopenQueue *q, |
2250 | uint64_t cumulative_perms, | |
2251 | uint64_t cumulative_shared_perms, | |
2252 | GSList *ignore_children, Error **errp) | |
33a610c3 KW |
2253 | { |
2254 | BlockDriver *drv = bs->drv; | |
2255 | BdrvChild *c; | |
2256 | int ret; | |
2257 | ||
2258 | /* Write permissions never work with read-only images */ | |
2259 | if ((cumulative_perms & (BLK_PERM_WRITE | BLK_PERM_WRITE_UNCHANGED)) && | |
cc022140 | 2260 | !bdrv_is_writable_after_reopen(bs, q)) |
33a610c3 | 2261 | { |
481e0eee HR |
2262 | if (!bdrv_is_writable_after_reopen(bs, NULL)) { |
2263 | error_setg(errp, "Block node is read-only"); | |
2264 | } else { | |
2265 | uint64_t current_perms, current_shared; | |
2266 | bdrv_get_cumulative_perm(bs, ¤t_perms, ¤t_shared); | |
2267 | if (current_perms & (BLK_PERM_WRITE | BLK_PERM_WRITE_UNCHANGED)) { | |
2268 | error_setg(errp, "Cannot make block node read-only, there is " | |
2269 | "a writer on it"); | |
2270 | } else { | |
2271 | error_setg(errp, "Cannot make block node read-only and create " | |
2272 | "a writer on it"); | |
2273 | } | |
2274 | } | |
2275 | ||
33a610c3 KW |
2276 | return -EPERM; |
2277 | } | |
2278 | ||
9c60a5d1 KW |
2279 | /* |
2280 | * Unaligned requests will automatically be aligned to bl.request_alignment | |
2281 | * and without RESIZE we can't extend requests to write to space beyond the | |
2282 | * end of the image, so it's required that the image size is aligned. | |
2283 | */ | |
2284 | if ((cumulative_perms & (BLK_PERM_WRITE | BLK_PERM_WRITE_UNCHANGED)) && | |
2285 | !(cumulative_perms & BLK_PERM_RESIZE)) | |
2286 | { | |
2287 | if ((bs->total_sectors * BDRV_SECTOR_SIZE) % bs->bl.request_alignment) { | |
2288 | error_setg(errp, "Cannot get 'write' permission without 'resize': " | |
2289 | "Image size is not a multiple of request " | |
2290 | "alignment"); | |
2291 | return -EPERM; | |
2292 | } | |
2293 | } | |
2294 | ||
33a610c3 KW |
2295 | /* Check this node */ |
2296 | if (!drv) { | |
2297 | return 0; | |
2298 | } | |
2299 | ||
2513ef59 VSO |
2300 | ret = bdrv_drv_set_perm(bs, cumulative_perms, cumulative_shared_perms, NULL, |
2301 | errp); | |
2302 | if (ret < 0) { | |
2303 | return ret; | |
33a610c3 KW |
2304 | } |
2305 | ||
78e421c9 | 2306 | /* Drivers that never have children can omit .bdrv_child_perm() */ |
33a610c3 | 2307 | if (!drv->bdrv_child_perm) { |
78e421c9 | 2308 | assert(QLIST_EMPTY(&bs->children)); |
33a610c3 KW |
2309 | return 0; |
2310 | } | |
2311 | ||
2312 | /* Check all children */ | |
2313 | QLIST_FOREACH(c, &bs->children, next) { | |
2314 | uint64_t cur_perm, cur_shared; | |
9eab1544 | 2315 | |
e5d8a406 | 2316 | bdrv_child_perm(bs, c->bs, c, c->role, q, |
ffd1a5a2 FZ |
2317 | cumulative_perms, cumulative_shared_perms, |
2318 | &cur_perm, &cur_shared); | |
bd57f8f7 VSO |
2319 | bdrv_child_set_perm_safe(c, cur_perm, cur_shared, NULL); |
2320 | } | |
2321 | ||
2322 | return 0; | |
2323 | } | |
3ef45e02 | 2324 | |
bd57f8f7 VSO |
2325 | static int bdrv_check_perm(BlockDriverState *bs, BlockReopenQueue *q, |
2326 | uint64_t cumulative_perms, | |
2327 | uint64_t cumulative_shared_perms, | |
2328 | GSList *ignore_children, Error **errp) | |
2329 | { | |
2330 | int ret; | |
2331 | BlockDriverState *root = bs; | |
2332 | g_autoptr(GSList) list = bdrv_topological_dfs(NULL, NULL, root); | |
2333 | ||
2334 | for ( ; list; list = list->next) { | |
2335 | bs = list->data; | |
2336 | ||
2337 | if (bs != root) { | |
2338 | if (bdrv_parent_perms_conflict(bs, ignore_children, errp)) { | |
2339 | return -EINVAL; | |
2340 | } | |
2341 | ||
2342 | bdrv_get_cumulative_perm(bs, &cumulative_perms, | |
2343 | &cumulative_shared_perms); | |
2344 | } | |
2345 | ||
2346 | ret = bdrv_node_check_perm(bs, q, cumulative_perms, | |
2347 | cumulative_shared_perms, | |
2348 | ignore_children, errp); | |
33a610c3 KW |
2349 | if (ret < 0) { |
2350 | return ret; | |
2351 | } | |
2352 | } | |
2353 | ||
2354 | return 0; | |
2355 | } | |
2356 | ||
2357 | /* | |
2358 | * Notifies drivers that after a previous bdrv_check_perm() call, the | |
2359 | * permission update is not performed and any preparations made for it (e.g. | |
2360 | * taken file locks) need to be undone. | |
33a610c3 | 2361 | */ |
bd57f8f7 | 2362 | static void bdrv_node_abort_perm_update(BlockDriverState *bs) |
33a610c3 KW |
2363 | { |
2364 | BlockDriver *drv = bs->drv; | |
2365 | BdrvChild *c; | |
2366 | ||
2367 | if (!drv) { | |
2368 | return; | |
2369 | } | |
2370 | ||
2513ef59 | 2371 | bdrv_drv_set_perm_abort(bs); |
33a610c3 KW |
2372 | |
2373 | QLIST_FOREACH(c, &bs->children, next) { | |
3ef45e02 | 2374 | bdrv_child_set_perm_abort(c); |
33a610c3 KW |
2375 | } |
2376 | } | |
2377 | ||
bd57f8f7 VSO |
2378 | static void bdrv_abort_perm_update(BlockDriverState *bs) |
2379 | { | |
2380 | g_autoptr(GSList) list = bdrv_topological_dfs(NULL, NULL, bs); | |
2381 | ||
2382 | for ( ; list; list = list->next) { | |
2383 | bdrv_node_abort_perm_update((BlockDriverState *)list->data); | |
2384 | } | |
2385 | } | |
2386 | ||
2387 | static void bdrv_node_set_perm(BlockDriverState *bs) | |
33a610c3 KW |
2388 | { |
2389 | BlockDriver *drv = bs->drv; | |
2390 | BdrvChild *c; | |
2391 | ||
2392 | if (!drv) { | |
2393 | return; | |
2394 | } | |
2395 | ||
2513ef59 | 2396 | bdrv_drv_set_perm_commit(bs); |
33a610c3 | 2397 | |
78e421c9 | 2398 | /* Drivers that never have children can omit .bdrv_child_perm() */ |
33a610c3 | 2399 | if (!drv->bdrv_child_perm) { |
78e421c9 | 2400 | assert(QLIST_EMPTY(&bs->children)); |
33a610c3 KW |
2401 | return; |
2402 | } | |
2403 | ||
2404 | /* Update all children */ | |
2405 | QLIST_FOREACH(c, &bs->children, next) { | |
3ef45e02 | 2406 | bdrv_child_set_perm_commit(c); |
bd57f8f7 VSO |
2407 | } |
2408 | } | |
2409 | ||
2410 | static void bdrv_set_perm(BlockDriverState *bs) | |
2411 | { | |
2412 | g_autoptr(GSList) list = bdrv_topological_dfs(NULL, NULL, bs); | |
2413 | ||
2414 | for ( ; list; list = list->next) { | |
2415 | bdrv_node_set_perm((BlockDriverState *)list->data); | |
33a610c3 KW |
2416 | } |
2417 | } | |
2418 | ||
c7a0f2be KW |
2419 | void bdrv_get_cumulative_perm(BlockDriverState *bs, uint64_t *perm, |
2420 | uint64_t *shared_perm) | |
33a610c3 KW |
2421 | { |
2422 | BdrvChild *c; | |
2423 | uint64_t cumulative_perms = 0; | |
2424 | uint64_t cumulative_shared_perms = BLK_PERM_ALL; | |
2425 | ||
2426 | QLIST_FOREACH(c, &bs->parents, next_parent) { | |
2427 | cumulative_perms |= c->perm; | |
2428 | cumulative_shared_perms &= c->shared_perm; | |
2429 | } | |
2430 | ||
2431 | *perm = cumulative_perms; | |
2432 | *shared_perm = cumulative_shared_perms; | |
2433 | } | |
2434 | ||
5176196c | 2435 | char *bdrv_perm_names(uint64_t perm) |
d083319f KW |
2436 | { |
2437 | struct perm_name { | |
2438 | uint64_t perm; | |
2439 | const char *name; | |
2440 | } permissions[] = { | |
2441 | { BLK_PERM_CONSISTENT_READ, "consistent read" }, | |
2442 | { BLK_PERM_WRITE, "write" }, | |
2443 | { BLK_PERM_WRITE_UNCHANGED, "write unchanged" }, | |
2444 | { BLK_PERM_RESIZE, "resize" }, | |
2445 | { BLK_PERM_GRAPH_MOD, "change children" }, | |
2446 | { 0, NULL } | |
2447 | }; | |
2448 | ||
e2a7423a | 2449 | GString *result = g_string_sized_new(30); |
d083319f KW |
2450 | struct perm_name *p; |
2451 | ||
2452 | for (p = permissions; p->name; p++) { | |
2453 | if (perm & p->perm) { | |
e2a7423a AG |
2454 | if (result->len > 0) { |
2455 | g_string_append(result, ", "); | |
2456 | } | |
2457 | g_string_append(result, p->name); | |
d083319f KW |
2458 | } |
2459 | } | |
2460 | ||
e2a7423a | 2461 | return g_string_free(result, FALSE); |
d083319f KW |
2462 | } |
2463 | ||
33a610c3 KW |
2464 | /* |
2465 | * Checks whether a new reference to @bs can be added if the new user requires | |
46181129 KW |
2466 | * @new_used_perm/@new_shared_perm as its permissions. If @ignore_children is |
2467 | * set, the BdrvChild objects in this list are ignored in the calculations; | |
2468 | * this allows checking permission updates for an existing reference. | |
33a610c3 KW |
2469 | * |
2470 | * Needs to be followed by a call to either bdrv_set_perm() or | |
2471 | * bdrv_abort_perm_update(). */ | |
3121fb45 KW |
2472 | static int bdrv_check_update_perm(BlockDriverState *bs, BlockReopenQueue *q, |
2473 | uint64_t new_used_perm, | |
d5e6f437 | 2474 | uint64_t new_shared_perm, |
9eab1544 | 2475 | GSList *ignore_children, |
9eab1544 | 2476 | Error **errp) |
d5e6f437 KW |
2477 | { |
2478 | BdrvChild *c; | |
33a610c3 KW |
2479 | uint64_t cumulative_perms = new_used_perm; |
2480 | uint64_t cumulative_shared_perms = new_shared_perm; | |
d5e6f437 | 2481 | |
9eab1544 | 2482 | |
d5e6f437 KW |
2483 | /* There is no reason why anyone couldn't tolerate write_unchanged */ |
2484 | assert(new_shared_perm & BLK_PERM_WRITE_UNCHANGED); | |
2485 | ||
2486 | QLIST_FOREACH(c, &bs->parents, next_parent) { | |
46181129 | 2487 | if (g_slist_find(ignore_children, c)) { |
d5e6f437 KW |
2488 | continue; |
2489 | } | |
2490 | ||
d083319f KW |
2491 | if ((new_used_perm & c->shared_perm) != new_used_perm) { |
2492 | char *user = bdrv_child_user_desc(c); | |
2493 | char *perm_names = bdrv_perm_names(new_used_perm & ~c->shared_perm); | |
9eab1544 | 2494 | |
d083319f KW |
2495 | error_setg(errp, "Conflicts with use by %s as '%s', which does not " |
2496 | "allow '%s' on %s", | |
2497 | user, c->name, perm_names, bdrv_get_node_name(c->bs)); | |
2498 | g_free(user); | |
2499 | g_free(perm_names); | |
2500 | return -EPERM; | |
2501 | } | |
2502 | ||
2503 | if ((c->perm & new_shared_perm) != c->perm) { | |
2504 | char *user = bdrv_child_user_desc(c); | |
2505 | char *perm_names = bdrv_perm_names(c->perm & ~new_shared_perm); | |
9eab1544 | 2506 | |
d083319f KW |
2507 | error_setg(errp, "Conflicts with use by %s as '%s', which uses " |
2508 | "'%s' on %s", | |
2509 | user, c->name, perm_names, bdrv_get_node_name(c->bs)); | |
2510 | g_free(user); | |
2511 | g_free(perm_names); | |
d5e6f437 KW |
2512 | return -EPERM; |
2513 | } | |
33a610c3 KW |
2514 | |
2515 | cumulative_perms |= c->perm; | |
2516 | cumulative_shared_perms &= c->shared_perm; | |
d5e6f437 KW |
2517 | } |
2518 | ||
3121fb45 | 2519 | return bdrv_check_perm(bs, q, cumulative_perms, cumulative_shared_perms, |
071b474f | 2520 | ignore_children, errp); |
33a610c3 KW |
2521 | } |
2522 | ||
071b474f | 2523 | static int bdrv_refresh_perms(BlockDriverState *bs, Error **errp) |
bb87e4d1 VSO |
2524 | { |
2525 | int ret; | |
2526 | uint64_t perm, shared_perm; | |
2527 | ||
bd57f8f7 | 2528 | if (bdrv_parent_perms_conflict(bs, NULL, errp)) { |
3bf416ba VSO |
2529 | return -EPERM; |
2530 | } | |
bb87e4d1 | 2531 | bdrv_get_cumulative_perm(bs, &perm, &shared_perm); |
071b474f | 2532 | ret = bdrv_check_perm(bs, NULL, perm, shared_perm, NULL, errp); |
bb87e4d1 VSO |
2533 | if (ret < 0) { |
2534 | bdrv_abort_perm_update(bs); | |
2535 | return ret; | |
2536 | } | |
74ad9a3b | 2537 | bdrv_set_perm(bs); |
bb87e4d1 VSO |
2538 | |
2539 | return 0; | |
2540 | } | |
2541 | ||
33a610c3 KW |
2542 | int bdrv_child_try_set_perm(BdrvChild *c, uint64_t perm, uint64_t shared, |
2543 | Error **errp) | |
2544 | { | |
1046779e | 2545 | Error *local_err = NULL; |
83928dc4 | 2546 | Transaction *tran = tran_new(); |
33a610c3 KW |
2547 | int ret; |
2548 | ||
83928dc4 VSO |
2549 | bdrv_child_set_perm_safe(c, perm, shared, tran); |
2550 | ||
2551 | ret = bdrv_refresh_perms(c->bs, &local_err); | |
2552 | ||
2553 | tran_finalize(tran, ret); | |
2554 | ||
33a610c3 | 2555 | if (ret < 0) { |
071b474f VSO |
2556 | if ((perm & ~c->perm) || (c->shared_perm & ~shared)) { |
2557 | /* tighten permissions */ | |
1046779e HR |
2558 | error_propagate(errp, local_err); |
2559 | } else { | |
2560 | /* | |
2561 | * Our caller may intend to only loosen restrictions and | |
2562 | * does not expect this function to fail. Errors are not | |
2563 | * fatal in such a case, so we can just hide them from our | |
2564 | * caller. | |
2565 | */ | |
2566 | error_free(local_err); | |
2567 | ret = 0; | |
2568 | } | |
33a610c3 KW |
2569 | } |
2570 | ||
83928dc4 | 2571 | return ret; |
d5e6f437 KW |
2572 | } |
2573 | ||
c1087f12 HR |
2574 | int bdrv_child_refresh_perms(BlockDriverState *bs, BdrvChild *c, Error **errp) |
2575 | { | |
2576 | uint64_t parent_perms, parent_shared; | |
2577 | uint64_t perms, shared; | |
2578 | ||
2579 | bdrv_get_cumulative_perm(bs, &parent_perms, &parent_shared); | |
e5d8a406 | 2580 | bdrv_child_perm(bs, c->bs, c, c->role, NULL, |
bf8e925e | 2581 | parent_perms, parent_shared, &perms, &shared); |
c1087f12 HR |
2582 | |
2583 | return bdrv_child_try_set_perm(c, perms, shared, errp); | |
2584 | } | |
2585 | ||
87278af1 HR |
2586 | /* |
2587 | * Default implementation for .bdrv_child_perm() for block filters: | |
2588 | * Forward CONSISTENT_READ, WRITE, WRITE_UNCHANGED, and RESIZE to the | |
2589 | * filtered child. | |
2590 | */ | |
2591 | static void bdrv_filter_default_perms(BlockDriverState *bs, BdrvChild *c, | |
87278af1 HR |
2592 | BdrvChildRole role, |
2593 | BlockReopenQueue *reopen_queue, | |
2594 | uint64_t perm, uint64_t shared, | |
2595 | uint64_t *nperm, uint64_t *nshared) | |
6a1b9ee1 | 2596 | { |
e444fa83 KW |
2597 | *nperm = perm & DEFAULT_PERM_PASSTHROUGH; |
2598 | *nshared = (shared & DEFAULT_PERM_PASSTHROUGH) | DEFAULT_PERM_UNCHANGED; | |
6a1b9ee1 KW |
2599 | } |
2600 | ||
70082db4 | 2601 | static void bdrv_default_perms_for_cow(BlockDriverState *bs, BdrvChild *c, |
70082db4 HR |
2602 | BdrvChildRole role, |
2603 | BlockReopenQueue *reopen_queue, | |
2604 | uint64_t perm, uint64_t shared, | |
2605 | uint64_t *nperm, uint64_t *nshared) | |
2606 | { | |
e5d8a406 | 2607 | assert(role & BDRV_CHILD_COW); |
70082db4 HR |
2608 | |
2609 | /* | |
2610 | * We want consistent read from backing files if the parent needs it. | |
2611 | * No other operations are performed on backing files. | |
2612 | */ | |
2613 | perm &= BLK_PERM_CONSISTENT_READ; | |
2614 | ||
2615 | /* | |
2616 | * If the parent can deal with changing data, we're okay with a | |
2617 | * writable and resizable backing file. | |
2618 | * TODO Require !(perm & BLK_PERM_CONSISTENT_READ), too? | |
2619 | */ | |
2620 | if (shared & BLK_PERM_WRITE) { | |
2621 | shared = BLK_PERM_WRITE | BLK_PERM_RESIZE; | |
2622 | } else { | |
2623 | shared = 0; | |
2624 | } | |
2625 | ||
2626 | shared |= BLK_PERM_CONSISTENT_READ | BLK_PERM_GRAPH_MOD | | |
2627 | BLK_PERM_WRITE_UNCHANGED; | |
2628 | ||
2629 | if (bs->open_flags & BDRV_O_INACTIVE) { | |
2630 | shared |= BLK_PERM_WRITE | BLK_PERM_RESIZE; | |
2631 | } | |
2632 | ||
2633 | *nperm = perm; | |
2634 | *nshared = shared; | |
2635 | } | |
2636 | ||
6f838a4b | 2637 | static void bdrv_default_perms_for_storage(BlockDriverState *bs, BdrvChild *c, |
6f838a4b HR |
2638 | BdrvChildRole role, |
2639 | BlockReopenQueue *reopen_queue, | |
2640 | uint64_t perm, uint64_t shared, | |
2641 | uint64_t *nperm, uint64_t *nshared) | |
2642 | { | |
2643 | int flags; | |
2644 | ||
e5d8a406 | 2645 | assert(role & (BDRV_CHILD_METADATA | BDRV_CHILD_DATA)); |
6f838a4b HR |
2646 | |
2647 | flags = bdrv_reopen_get_flags(reopen_queue, bs); | |
2648 | ||
2649 | /* | |
2650 | * Apart from the modifications below, the same permissions are | |
2651 | * forwarded and left alone as for filters | |
2652 | */ | |
e5d8a406 | 2653 | bdrv_filter_default_perms(bs, c, role, reopen_queue, |
6f838a4b HR |
2654 | perm, shared, &perm, &shared); |
2655 | ||
f889054f HR |
2656 | if (role & BDRV_CHILD_METADATA) { |
2657 | /* Format drivers may touch metadata even if the guest doesn't write */ | |
2658 | if (bdrv_is_writable_after_reopen(bs, reopen_queue)) { | |
2659 | perm |= BLK_PERM_WRITE | BLK_PERM_RESIZE; | |
2660 | } | |
2661 | ||
2662 | /* | |
2663 | * bs->file always needs to be consistent because of the | |
2664 | * metadata. We can never allow other users to resize or write | |
2665 | * to it. | |
2666 | */ | |
2667 | if (!(flags & BDRV_O_NO_IO)) { | |
2668 | perm |= BLK_PERM_CONSISTENT_READ; | |
2669 | } | |
2670 | shared &= ~(BLK_PERM_WRITE | BLK_PERM_RESIZE); | |
6f838a4b HR |
2671 | } |
2672 | ||
f889054f HR |
2673 | if (role & BDRV_CHILD_DATA) { |
2674 | /* | |
2675 | * Technically, everything in this block is a subset of the | |
2676 | * BDRV_CHILD_METADATA path taken above, and so this could | |
2677 | * be an "else if" branch. However, that is not obvious, and | |
2678 | * this function is not performance critical, therefore we let | |
2679 | * this be an independent "if". | |
2680 | */ | |
2681 | ||
2682 | /* | |
2683 | * We cannot allow other users to resize the file because the | |
2684 | * format driver might have some assumptions about the size | |
2685 | * (e.g. because it is stored in metadata, or because the file | |
2686 | * is split into fixed-size data files). | |
2687 | */ | |
2688 | shared &= ~BLK_PERM_RESIZE; | |
2689 | ||
2690 | /* | |
2691 | * WRITE_UNCHANGED often cannot be performed as such on the | |
2692 | * data file. For example, the qcow2 driver may still need to | |
2693 | * write copied clusters on copy-on-read. | |
2694 | */ | |
2695 | if (perm & BLK_PERM_WRITE_UNCHANGED) { | |
2696 | perm |= BLK_PERM_WRITE; | |
2697 | } | |
2698 | ||
2699 | /* | |
2700 | * If the data file is written to, the format driver may | |
2701 | * expect to be able to resize it by writing beyond the EOF. | |
2702 | */ | |
2703 | if (perm & BLK_PERM_WRITE) { | |
2704 | perm |= BLK_PERM_RESIZE; | |
2705 | } | |
6f838a4b | 2706 | } |
6f838a4b HR |
2707 | |
2708 | if (bs->open_flags & BDRV_O_INACTIVE) { | |
2709 | shared |= BLK_PERM_WRITE | BLK_PERM_RESIZE; | |
2710 | } | |
2711 | ||
2712 | *nperm = perm; | |
2713 | *nshared = shared; | |
2714 | } | |
2715 | ||
2519f549 | 2716 | void bdrv_default_perms(BlockDriverState *bs, BdrvChild *c, |
e5d8a406 | 2717 | BdrvChildRole role, BlockReopenQueue *reopen_queue, |
2519f549 HR |
2718 | uint64_t perm, uint64_t shared, |
2719 | uint64_t *nperm, uint64_t *nshared) | |
2720 | { | |
2519f549 HR |
2721 | if (role & BDRV_CHILD_FILTERED) { |
2722 | assert(!(role & (BDRV_CHILD_DATA | BDRV_CHILD_METADATA | | |
2723 | BDRV_CHILD_COW))); | |
e5d8a406 | 2724 | bdrv_filter_default_perms(bs, c, role, reopen_queue, |
2519f549 HR |
2725 | perm, shared, nperm, nshared); |
2726 | } else if (role & BDRV_CHILD_COW) { | |
2727 | assert(!(role & (BDRV_CHILD_DATA | BDRV_CHILD_METADATA))); | |
e5d8a406 | 2728 | bdrv_default_perms_for_cow(bs, c, role, reopen_queue, |
2519f549 HR |
2729 | perm, shared, nperm, nshared); |
2730 | } else if (role & (BDRV_CHILD_METADATA | BDRV_CHILD_DATA)) { | |
e5d8a406 | 2731 | bdrv_default_perms_for_storage(bs, c, role, reopen_queue, |
2519f549 HR |
2732 | perm, shared, nperm, nshared); |
2733 | } else { | |
2734 | g_assert_not_reached(); | |
2735 | } | |
2736 | } | |
2737 | ||
7b1d9c4d HR |
2738 | uint64_t bdrv_qapi_perm_to_blk_perm(BlockPermission qapi_perm) |
2739 | { | |
2740 | static const uint64_t permissions[] = { | |
2741 | [BLOCK_PERMISSION_CONSISTENT_READ] = BLK_PERM_CONSISTENT_READ, | |
2742 | [BLOCK_PERMISSION_WRITE] = BLK_PERM_WRITE, | |
2743 | [BLOCK_PERMISSION_WRITE_UNCHANGED] = BLK_PERM_WRITE_UNCHANGED, | |
2744 | [BLOCK_PERMISSION_RESIZE] = BLK_PERM_RESIZE, | |
2745 | [BLOCK_PERMISSION_GRAPH_MOD] = BLK_PERM_GRAPH_MOD, | |
2746 | }; | |
2747 | ||
2748 | QEMU_BUILD_BUG_ON(ARRAY_SIZE(permissions) != BLOCK_PERMISSION__MAX); | |
2749 | QEMU_BUILD_BUG_ON(1UL << ARRAY_SIZE(permissions) != BLK_PERM_ALL + 1); | |
2750 | ||
2751 | assert(qapi_perm < BLOCK_PERMISSION__MAX); | |
2752 | ||
2753 | return permissions[qapi_perm]; | |
2754 | } | |
2755 | ||
8ee03995 KW |
2756 | static void bdrv_replace_child_noperm(BdrvChild *child, |
2757 | BlockDriverState *new_bs) | |
e9740bc6 KW |
2758 | { |
2759 | BlockDriverState *old_bs = child->bs; | |
debc2927 HR |
2760 | int new_bs_quiesce_counter; |
2761 | int drain_saldo; | |
e9740bc6 | 2762 | |
2cad1ebe AG |
2763 | assert(!child->frozen); |
2764 | ||
bb2614e9 FZ |
2765 | if (old_bs && new_bs) { |
2766 | assert(bdrv_get_aio_context(old_bs) == bdrv_get_aio_context(new_bs)); | |
2767 | } | |
debc2927 HR |
2768 | |
2769 | new_bs_quiesce_counter = (new_bs ? new_bs->quiesce_counter : 0); | |
2770 | drain_saldo = new_bs_quiesce_counter - child->parent_quiesce_counter; | |
2771 | ||
2772 | /* | |
2773 | * If the new child node is drained but the old one was not, flush | |
2774 | * all outstanding requests to the old child node. | |
2775 | */ | |
bd86fb99 | 2776 | while (drain_saldo > 0 && child->klass->drained_begin) { |
debc2927 HR |
2777 | bdrv_parent_drained_begin_single(child, true); |
2778 | drain_saldo--; | |
2779 | } | |
2780 | ||
e9740bc6 | 2781 | if (old_bs) { |
d736f119 KW |
2782 | /* Detach first so that the recursive drain sections coming from @child |
2783 | * are already gone and we only end the drain sections that came from | |
2784 | * elsewhere. */ | |
bd86fb99 HR |
2785 | if (child->klass->detach) { |
2786 | child->klass->detach(child); | |
d736f119 | 2787 | } |
e9740bc6 KW |
2788 | QLIST_REMOVE(child, next_parent); |
2789 | } | |
36fe1331 KW |
2790 | |
2791 | child->bs = new_bs; | |
2792 | ||
e9740bc6 KW |
2793 | if (new_bs) { |
2794 | QLIST_INSERT_HEAD(&new_bs->parents, child, next_parent); | |
debc2927 HR |
2795 | |
2796 | /* | |
2797 | * Detaching the old node may have led to the new node's | |
2798 | * quiesce_counter having been decreased. Not a problem, we | |
2799 | * just need to recognize this here and then invoke | |
2800 | * drained_end appropriately more often. | |
2801 | */ | |
2802 | assert(new_bs->quiesce_counter <= new_bs_quiesce_counter); | |
2803 | drain_saldo += new_bs->quiesce_counter - new_bs_quiesce_counter; | |
33a610c3 | 2804 | |
d736f119 KW |
2805 | /* Attach only after starting new drained sections, so that recursive |
2806 | * drain sections coming from @child don't get an extra .drained_begin | |
2807 | * callback. */ | |
bd86fb99 HR |
2808 | if (child->klass->attach) { |
2809 | child->klass->attach(child); | |
8ee03995 KW |
2810 | } |
2811 | } | |
debc2927 HR |
2812 | |
2813 | /* | |
2814 | * If the old child node was drained but the new one is not, allow | |
2815 | * requests to come in only after the new node has been attached. | |
2816 | */ | |
bd86fb99 | 2817 | while (drain_saldo < 0 && child->klass->drained_end) { |
debc2927 HR |
2818 | bdrv_parent_drained_end_single(child); |
2819 | drain_saldo++; | |
2820 | } | |
8ee03995 | 2821 | } |
33a610c3 | 2822 | |
466787fb KW |
2823 | /* |
2824 | * Updates @child to change its reference to point to @new_bs, including | |
e3a6e0da | 2825 | * checking and applying the necessary permission updates both to the old node |
466787fb KW |
2826 | * and to @new_bs. |
2827 | * | |
2828 | * NULL is passed as @new_bs for removing the reference before freeing @child. | |
2829 | * | |
2830 | * If @new_bs is not NULL, bdrv_check_perm() must be called beforehand, as this | |
2831 | * function uses bdrv_set_perm() to update the permissions according to the new | |
2832 | * reference that @new_bs gets. | |
7b99a266 HR |
2833 | * |
2834 | * Callers must ensure that child->frozen is false. | |
466787fb KW |
2835 | */ |
2836 | static void bdrv_replace_child(BdrvChild *child, BlockDriverState *new_bs) | |
8ee03995 KW |
2837 | { |
2838 | BlockDriverState *old_bs = child->bs; | |
8ee03995 | 2839 | |
7b99a266 | 2840 | /* Asserts that child->frozen == false */ |
8aecf1d1 KW |
2841 | bdrv_replace_child_noperm(child, new_bs); |
2842 | ||
87ace5f8 HR |
2843 | /* |
2844 | * Start with the new node's permissions. If @new_bs is a (direct | |
2845 | * or indirect) child of @old_bs, we must complete the permission | |
2846 | * update on @new_bs before we loosen the restrictions on @old_bs. | |
2847 | * Otherwise, bdrv_check_perm() on @old_bs would re-initiate | |
2848 | * updating the permissions of @new_bs, and thus not purely loosen | |
2849 | * restrictions. | |
2850 | */ | |
2851 | if (new_bs) { | |
74ad9a3b | 2852 | bdrv_set_perm(new_bs); |
87ace5f8 HR |
2853 | } |
2854 | ||
8ee03995 | 2855 | if (old_bs) { |
bb87e4d1 VSO |
2856 | /* |
2857 | * Update permissions for old node. We're just taking a parent away, so | |
2858 | * we're loosening restrictions. Errors of permission update are not | |
2859 | * fatal in this case, ignore them. | |
2860 | */ | |
071b474f | 2861 | bdrv_refresh_perms(old_bs, NULL); |
ad943dcb KW |
2862 | |
2863 | /* When the parent requiring a non-default AioContext is removed, the | |
2864 | * node moves back to the main AioContext */ | |
2865 | bdrv_try_set_aio_context(old_bs, qemu_get_aio_context(), NULL); | |
e9740bc6 | 2866 | } |
e9740bc6 KW |
2867 | } |
2868 | ||
b441dc71 AG |
2869 | /* |
2870 | * This function steals the reference to child_bs from the caller. | |
2871 | * That reference is later dropped by bdrv_root_unref_child(). | |
2872 | * | |
2873 | * On failure NULL is returned, errp is set and the reference to | |
2874 | * child_bs is also dropped. | |
132ada80 KW |
2875 | * |
2876 | * The caller must hold the AioContext lock @child_bs, but not that of @ctx | |
2877 | * (unless @child_bs is already in @ctx). | |
b441dc71 | 2878 | */ |
f21d96d0 KW |
2879 | BdrvChild *bdrv_root_attach_child(BlockDriverState *child_bs, |
2880 | const char *child_name, | |
bd86fb99 | 2881 | const BdrvChildClass *child_class, |
258b7765 | 2882 | BdrvChildRole child_role, |
d5e6f437 KW |
2883 | uint64_t perm, uint64_t shared_perm, |
2884 | void *opaque, Error **errp) | |
df581792 | 2885 | { |
d5e6f437 | 2886 | BdrvChild *child; |
132ada80 | 2887 | Error *local_err = NULL; |
d5e6f437 | 2888 | int ret; |
228ca37e | 2889 | AioContext *ctx; |
d5e6f437 | 2890 | |
071b474f | 2891 | ret = bdrv_check_update_perm(child_bs, NULL, perm, shared_perm, NULL, errp); |
d5e6f437 | 2892 | if (ret < 0) { |
33a610c3 | 2893 | bdrv_abort_perm_update(child_bs); |
b441dc71 | 2894 | bdrv_unref(child_bs); |
d5e6f437 KW |
2895 | return NULL; |
2896 | } | |
2897 | ||
2898 | child = g_new(BdrvChild, 1); | |
df581792 | 2899 | *child = (BdrvChild) { |
d5e6f437 KW |
2900 | .bs = NULL, |
2901 | .name = g_strdup(child_name), | |
bd86fb99 | 2902 | .klass = child_class, |
258b7765 | 2903 | .role = child_role, |
d5e6f437 KW |
2904 | .perm = perm, |
2905 | .shared_perm = shared_perm, | |
2906 | .opaque = opaque, | |
df581792 KW |
2907 | }; |
2908 | ||
228ca37e VSO |
2909 | ctx = bdrv_child_get_parent_aio_context(child); |
2910 | ||
132ada80 KW |
2911 | /* If the AioContexts don't match, first try to move the subtree of |
2912 | * child_bs into the AioContext of the new parent. If this doesn't work, | |
2913 | * try moving the parent into the AioContext of child_bs instead. */ | |
2914 | if (bdrv_get_aio_context(child_bs) != ctx) { | |
2915 | ret = bdrv_try_set_aio_context(child_bs, ctx, &local_err); | |
bd86fb99 | 2916 | if (ret < 0 && child_class->can_set_aio_ctx) { |
0beab811 | 2917 | GSList *ignore = g_slist_prepend(NULL, child); |
132ada80 | 2918 | ctx = bdrv_get_aio_context(child_bs); |
bd86fb99 | 2919 | if (child_class->can_set_aio_ctx(child, ctx, &ignore, NULL)) { |
132ada80 KW |
2920 | error_free(local_err); |
2921 | ret = 0; | |
2922 | g_slist_free(ignore); | |
0beab811 | 2923 | ignore = g_slist_prepend(NULL, child); |
bd86fb99 | 2924 | child_class->set_aio_ctx(child, ctx, &ignore); |
132ada80 KW |
2925 | } |
2926 | g_slist_free(ignore); | |
2927 | } | |
2928 | if (ret < 0) { | |
2929 | error_propagate(errp, local_err); | |
2930 | g_free(child); | |
2931 | bdrv_abort_perm_update(child_bs); | |
7a26df20 | 2932 | bdrv_unref(child_bs); |
132ada80 KW |
2933 | return NULL; |
2934 | } | |
2935 | } | |
2936 | ||
33a610c3 | 2937 | /* This performs the matching bdrv_set_perm() for the above check. */ |
466787fb | 2938 | bdrv_replace_child(child, child_bs); |
b4b059f6 KW |
2939 | |
2940 | return child; | |
df581792 KW |
2941 | } |
2942 | ||
b441dc71 AG |
2943 | /* |
2944 | * This function transfers the reference to child_bs from the caller | |
2945 | * to parent_bs. That reference is later dropped by parent_bs on | |
2946 | * bdrv_close() or if someone calls bdrv_unref_child(). | |
2947 | * | |
2948 | * On failure NULL is returned, errp is set and the reference to | |
2949 | * child_bs is also dropped. | |
132ada80 KW |
2950 | * |
2951 | * If @parent_bs and @child_bs are in different AioContexts, the caller must | |
2952 | * hold the AioContext lock for @child_bs, but not for @parent_bs. | |
b441dc71 | 2953 | */ |
98292c61 WC |
2954 | BdrvChild *bdrv_attach_child(BlockDriverState *parent_bs, |
2955 | BlockDriverState *child_bs, | |
2956 | const char *child_name, | |
bd86fb99 | 2957 | const BdrvChildClass *child_class, |
258b7765 | 2958 | BdrvChildRole child_role, |
8b2ff529 | 2959 | Error **errp) |
f21d96d0 | 2960 | { |
d5e6f437 | 2961 | BdrvChild *child; |
f68c598b KW |
2962 | uint64_t perm, shared_perm; |
2963 | ||
2964 | bdrv_get_cumulative_perm(parent_bs, &perm, &shared_perm); | |
2965 | ||
2966 | assert(parent_bs->drv); | |
e5d8a406 | 2967 | bdrv_child_perm(parent_bs, child_bs, NULL, child_role, NULL, |
ffd1a5a2 | 2968 | perm, shared_perm, &perm, &shared_perm); |
d5e6f437 | 2969 | |
bd86fb99 | 2970 | child = bdrv_root_attach_child(child_bs, child_name, child_class, |
228ca37e VSO |
2971 | child_role, perm, shared_perm, parent_bs, |
2972 | errp); | |
d5e6f437 KW |
2973 | if (child == NULL) { |
2974 | return NULL; | |
2975 | } | |
2976 | ||
f21d96d0 KW |
2977 | QLIST_INSERT_HEAD(&parent_bs->children, child, next); |
2978 | return child; | |
2979 | } | |
2980 | ||
3f09bfbc | 2981 | static void bdrv_detach_child(BdrvChild *child) |
33a60407 | 2982 | { |
195ed8cb | 2983 | QLIST_SAFE_REMOVE(child, next); |
e9740bc6 | 2984 | |
466787fb | 2985 | bdrv_replace_child(child, NULL); |
e9740bc6 | 2986 | |
260fecf1 | 2987 | g_free(child->name); |
33a60407 KW |
2988 | g_free(child); |
2989 | } | |
2990 | ||
7b99a266 | 2991 | /* Callers must ensure that child->frozen is false. */ |
f21d96d0 | 2992 | void bdrv_root_unref_child(BdrvChild *child) |
33a60407 | 2993 | { |
779020cb KW |
2994 | BlockDriverState *child_bs; |
2995 | ||
f21d96d0 KW |
2996 | child_bs = child->bs; |
2997 | bdrv_detach_child(child); | |
2998 | bdrv_unref(child_bs); | |
2999 | } | |
3000 | ||
3cf746b3 HR |
3001 | /** |
3002 | * Clear all inherits_from pointers from children and grandchildren of | |
3003 | * @root that point to @root, where necessary. | |
3004 | */ | |
3005 | static void bdrv_unset_inherits_from(BlockDriverState *root, BdrvChild *child) | |
f21d96d0 | 3006 | { |
3cf746b3 | 3007 | BdrvChild *c; |
4e4bf5c4 | 3008 | |
3cf746b3 HR |
3009 | if (child->bs->inherits_from == root) { |
3010 | /* | |
3011 | * Remove inherits_from only when the last reference between root and | |
3012 | * child->bs goes away. | |
3013 | */ | |
3014 | QLIST_FOREACH(c, &root->children, next) { | |
4e4bf5c4 KW |
3015 | if (c != child && c->bs == child->bs) { |
3016 | break; | |
3017 | } | |
3018 | } | |
3019 | if (c == NULL) { | |
3020 | child->bs->inherits_from = NULL; | |
3021 | } | |
33a60407 KW |
3022 | } |
3023 | ||
3cf746b3 HR |
3024 | QLIST_FOREACH(c, &child->bs->children, next) { |
3025 | bdrv_unset_inherits_from(root, c); | |
3026 | } | |
3027 | } | |
3028 | ||
7b99a266 | 3029 | /* Callers must ensure that child->frozen is false. */ |
3cf746b3 HR |
3030 | void bdrv_unref_child(BlockDriverState *parent, BdrvChild *child) |
3031 | { | |
3032 | if (child == NULL) { | |
3033 | return; | |
3034 | } | |
3035 | ||
3036 | bdrv_unset_inherits_from(parent, child); | |
f21d96d0 | 3037 | bdrv_root_unref_child(child); |
33a60407 KW |
3038 | } |
3039 | ||
5c8cab48 KW |
3040 | |
3041 | static void bdrv_parent_cb_change_media(BlockDriverState *bs, bool load) | |
3042 | { | |
3043 | BdrvChild *c; | |
3044 | QLIST_FOREACH(c, &bs->parents, next_parent) { | |
bd86fb99 HR |
3045 | if (c->klass->change_media) { |
3046 | c->klass->change_media(c, load); | |
5c8cab48 KW |
3047 | } |
3048 | } | |
3049 | } | |
3050 | ||
0065c455 AG |
3051 | /* Return true if you can reach parent going through child->inherits_from |
3052 | * recursively. If parent or child are NULL, return false */ | |
3053 | static bool bdrv_inherits_from_recursive(BlockDriverState *child, | |
3054 | BlockDriverState *parent) | |
3055 | { | |
3056 | while (child && child != parent) { | |
3057 | child = child->inherits_from; | |
3058 | } | |
3059 | ||
3060 | return child != NULL; | |
3061 | } | |
3062 | ||
25191e5f HR |
3063 | /* |
3064 | * Return the BdrvChildRole for @bs's backing child. bs->backing is | |
3065 | * mostly used for COW backing children (role = COW), but also for | |
3066 | * filtered children (role = FILTERED | PRIMARY). | |
3067 | */ | |
3068 | static BdrvChildRole bdrv_backing_role(BlockDriverState *bs) | |
3069 | { | |
3070 | if (bs->drv && bs->drv->is_filter) { | |
3071 | return BDRV_CHILD_FILTERED | BDRV_CHILD_PRIMARY; | |
3072 | } else { | |
3073 | return BDRV_CHILD_COW; | |
3074 | } | |
3075 | } | |
3076 | ||
5db15a57 | 3077 | /* |
9ee413cb | 3078 | * Sets the bs->backing link of a BDS. A new reference is created; callers |
5db15a57 KW |
3079 | * which don't need their own reference any more must call bdrv_unref(). |
3080 | */ | |
a1e708fc VSO |
3081 | int bdrv_set_backing_hd(BlockDriverState *bs, BlockDriverState *backing_hd, |
3082 | Error **errp) | |
8d24cce1 | 3083 | { |
a1e708fc | 3084 | int ret = 0; |
0065c455 AG |
3085 | bool update_inherits_from = bdrv_chain_contains(bs, backing_hd) && |
3086 | bdrv_inherits_from_recursive(backing_hd, bs); | |
3087 | ||
9ee413cb | 3088 | if (bdrv_is_backing_chain_frozen(bs, child_bs(bs->backing), errp)) { |
a1e708fc | 3089 | return -EPERM; |
2cad1ebe AG |
3090 | } |
3091 | ||
5db15a57 KW |
3092 | if (backing_hd) { |
3093 | bdrv_ref(backing_hd); | |
3094 | } | |
8d24cce1 | 3095 | |
760e0063 | 3096 | if (bs->backing) { |
7b99a266 | 3097 | /* Cannot be frozen, we checked that above */ |
5db15a57 | 3098 | bdrv_unref_child(bs, bs->backing); |
6e57963a | 3099 | bs->backing = NULL; |
826b6ca0 FZ |
3100 | } |
3101 | ||
8d24cce1 FZ |
3102 | if (!backing_hd) { |
3103 | goto out; | |
3104 | } | |
12fa4af6 | 3105 | |
25191e5f HR |
3106 | bs->backing = bdrv_attach_child(bs, backing_hd, "backing", &child_of_bds, |
3107 | bdrv_backing_role(bs), errp); | |
a1e708fc VSO |
3108 | if (!bs->backing) { |
3109 | ret = -EPERM; | |
3110 | goto out; | |
3111 | } | |
3112 | ||
0065c455 AG |
3113 | /* If backing_hd was already part of bs's backing chain, and |
3114 | * inherits_from pointed recursively to bs then let's update it to | |
3115 | * point directly to bs (else it will become NULL). */ | |
a1e708fc | 3116 | if (update_inherits_from) { |
0065c455 AG |
3117 | backing_hd->inherits_from = bs; |
3118 | } | |
826b6ca0 | 3119 | |
8d24cce1 | 3120 | out: |
3baca891 | 3121 | bdrv_refresh_limits(bs, NULL); |
a1e708fc VSO |
3122 | |
3123 | return ret; | |
8d24cce1 FZ |
3124 | } |
3125 | ||
31ca6d07 KW |
3126 | /* |
3127 | * Opens the backing file for a BlockDriverState if not yet open | |
3128 | * | |
d9b7b057 KW |
3129 | * bdref_key specifies the key for the image's BlockdevRef in the options QDict. |
3130 | * That QDict has to be flattened; therefore, if the BlockdevRef is a QDict | |
3131 | * itself, all options starting with "${bdref_key}." are considered part of the | |
3132 | * BlockdevRef. | |
3133 | * | |
3134 | * TODO Can this be unified with bdrv_open_image()? | |
31ca6d07 | 3135 | */ |
d9b7b057 KW |
3136 | int bdrv_open_backing_file(BlockDriverState *bs, QDict *parent_options, |
3137 | const char *bdref_key, Error **errp) | |
9156df12 | 3138 | { |
6b6833c1 | 3139 | char *backing_filename = NULL; |
d9b7b057 KW |
3140 | char *bdref_key_dot; |
3141 | const char *reference = NULL; | |
317fc44e | 3142 | int ret = 0; |
998c2019 | 3143 | bool implicit_backing = false; |
8d24cce1 | 3144 | BlockDriverState *backing_hd; |
d9b7b057 KW |
3145 | QDict *options; |
3146 | QDict *tmp_parent_options = NULL; | |
34b5d2c6 | 3147 | Error *local_err = NULL; |
9156df12 | 3148 | |
760e0063 | 3149 | if (bs->backing != NULL) { |
1ba4b6a5 | 3150 | goto free_exit; |
9156df12 PB |
3151 | } |
3152 | ||
31ca6d07 | 3153 | /* NULL means an empty set of options */ |
d9b7b057 KW |
3154 | if (parent_options == NULL) { |
3155 | tmp_parent_options = qdict_new(); | |
3156 | parent_options = tmp_parent_options; | |
31ca6d07 KW |
3157 | } |
3158 | ||
9156df12 | 3159 | bs->open_flags &= ~BDRV_O_NO_BACKING; |
d9b7b057 KW |
3160 | |
3161 | bdref_key_dot = g_strdup_printf("%s.", bdref_key); | |
3162 | qdict_extract_subqdict(parent_options, &options, bdref_key_dot); | |
3163 | g_free(bdref_key_dot); | |
3164 | ||
129c7d1c MA |
3165 | /* |
3166 | * Caution: while qdict_get_try_str() is fine, getting non-string | |
3167 | * types would require more care. When @parent_options come from | |
3168 | * -blockdev or blockdev_add, its members are typed according to | |
3169 | * the QAPI schema, but when they come from -drive, they're all | |
3170 | * QString. | |
3171 | */ | |
d9b7b057 KW |
3172 | reference = qdict_get_try_str(parent_options, bdref_key); |
3173 | if (reference || qdict_haskey(options, "file.filename")) { | |
6b6833c1 | 3174 | /* keep backing_filename NULL */ |
1cb6f506 | 3175 | } else if (bs->backing_file[0] == '\0' && qdict_size(options) == 0) { |
cb3e7f08 | 3176 | qobject_unref(options); |
1ba4b6a5 | 3177 | goto free_exit; |
dbecebdd | 3178 | } else { |
998c2019 HR |
3179 | if (qdict_size(options) == 0) { |
3180 | /* If the user specifies options that do not modify the | |
3181 | * backing file's behavior, we might still consider it the | |
3182 | * implicit backing file. But it's easier this way, and | |
3183 | * just specifying some of the backing BDS's options is | |
3184 | * only possible with -drive anyway (otherwise the QAPI | |
3185 | * schema forces the user to specify everything). */ | |
3186 | implicit_backing = !strcmp(bs->auto_backing_file, bs->backing_file); | |
3187 | } | |
3188 | ||
6b6833c1 | 3189 | backing_filename = bdrv_get_full_backing_filename(bs, &local_err); |
9f07429e HR |
3190 | if (local_err) { |
3191 | ret = -EINVAL; | |
3192 | error_propagate(errp, local_err); | |
cb3e7f08 | 3193 | qobject_unref(options); |
9f07429e HR |
3194 | goto free_exit; |
3195 | } | |
9156df12 PB |
3196 | } |
3197 | ||
8ee79e70 KW |
3198 | if (!bs->drv || !bs->drv->supports_backing) { |
3199 | ret = -EINVAL; | |
3200 | error_setg(errp, "Driver doesn't support backing files"); | |
cb3e7f08 | 3201 | qobject_unref(options); |
8ee79e70 KW |
3202 | goto free_exit; |
3203 | } | |
3204 | ||
6bff597b PK |
3205 | if (!reference && |
3206 | bs->backing_format[0] != '\0' && !qdict_haskey(options, "driver")) { | |
46f5ac20 | 3207 | qdict_put_str(options, "driver", bs->backing_format); |
9156df12 PB |
3208 | } |
3209 | ||
6b6833c1 | 3210 | backing_hd = bdrv_open_inherit(backing_filename, reference, options, 0, bs, |
25191e5f | 3211 | &child_of_bds, bdrv_backing_role(bs), errp); |
5b363937 | 3212 | if (!backing_hd) { |
9156df12 | 3213 | bs->open_flags |= BDRV_O_NO_BACKING; |
e43bfd9c | 3214 | error_prepend(errp, "Could not open backing file: "); |
5b363937 | 3215 | ret = -EINVAL; |
1ba4b6a5 | 3216 | goto free_exit; |
9156df12 | 3217 | } |
df581792 | 3218 | |
998c2019 HR |
3219 | if (implicit_backing) { |
3220 | bdrv_refresh_filename(backing_hd); | |
3221 | pstrcpy(bs->auto_backing_file, sizeof(bs->auto_backing_file), | |
3222 | backing_hd->filename); | |
3223 | } | |
3224 | ||
5db15a57 KW |
3225 | /* Hook up the backing file link; drop our reference, bs owns the |
3226 | * backing_hd reference now */ | |
dc9c10a1 | 3227 | ret = bdrv_set_backing_hd(bs, backing_hd, errp); |
5db15a57 | 3228 | bdrv_unref(backing_hd); |
dc9c10a1 | 3229 | if (ret < 0) { |
12fa4af6 KW |
3230 | goto free_exit; |
3231 | } | |
d80ac658 | 3232 | |
d9b7b057 KW |
3233 | qdict_del(parent_options, bdref_key); |
3234 | ||
1ba4b6a5 BC |
3235 | free_exit: |
3236 | g_free(backing_filename); | |
cb3e7f08 | 3237 | qobject_unref(tmp_parent_options); |
1ba4b6a5 | 3238 | return ret; |
9156df12 PB |
3239 | } |
3240 | ||
2d6b86af KW |
3241 | static BlockDriverState * |
3242 | bdrv_open_child_bs(const char *filename, QDict *options, const char *bdref_key, | |
bd86fb99 | 3243 | BlockDriverState *parent, const BdrvChildClass *child_class, |
272c02ea | 3244 | BdrvChildRole child_role, bool allow_none, Error **errp) |
da557aac | 3245 | { |
2d6b86af | 3246 | BlockDriverState *bs = NULL; |
da557aac | 3247 | QDict *image_options; |
da557aac HR |
3248 | char *bdref_key_dot; |
3249 | const char *reference; | |
3250 | ||
bd86fb99 | 3251 | assert(child_class != NULL); |
f67503e5 | 3252 | |
da557aac HR |
3253 | bdref_key_dot = g_strdup_printf("%s.", bdref_key); |
3254 | qdict_extract_subqdict(options, &image_options, bdref_key_dot); | |
3255 | g_free(bdref_key_dot); | |
3256 | ||
129c7d1c MA |
3257 | /* |
3258 | * Caution: while qdict_get_try_str() is fine, getting non-string | |
3259 | * types would require more care. When @options come from | |
3260 | * -blockdev or blockdev_add, its members are typed according to | |
3261 | * the QAPI schema, but when they come from -drive, they're all | |
3262 | * QString. | |
3263 | */ | |
da557aac HR |
3264 | reference = qdict_get_try_str(options, bdref_key); |
3265 | if (!filename && !reference && !qdict_size(image_options)) { | |
b4b059f6 | 3266 | if (!allow_none) { |
da557aac HR |
3267 | error_setg(errp, "A block device must be specified for \"%s\"", |
3268 | bdref_key); | |
da557aac | 3269 | } |
cb3e7f08 | 3270 | qobject_unref(image_options); |
da557aac HR |
3271 | goto done; |
3272 | } | |
3273 | ||
5b363937 | 3274 | bs = bdrv_open_inherit(filename, reference, image_options, 0, |
272c02ea | 3275 | parent, child_class, child_role, errp); |
5b363937 | 3276 | if (!bs) { |
df581792 KW |
3277 | goto done; |
3278 | } | |
3279 | ||
da557aac HR |
3280 | done: |
3281 | qdict_del(options, bdref_key); | |
2d6b86af KW |
3282 | return bs; |
3283 | } | |
3284 | ||
3285 | /* | |
3286 | * Opens a disk image whose options are given as BlockdevRef in another block | |
3287 | * device's options. | |
3288 | * | |
3289 | * If allow_none is true, no image will be opened if filename is false and no | |
3290 | * BlockdevRef is given. NULL will be returned, but errp remains unset. | |
3291 | * | |
3292 | * bdrev_key specifies the key for the image's BlockdevRef in the options QDict. | |
3293 | * That QDict has to be flattened; therefore, if the BlockdevRef is a QDict | |
3294 | * itself, all options starting with "${bdref_key}." are considered part of the | |
3295 | * BlockdevRef. | |
3296 | * | |
3297 | * The BlockdevRef will be removed from the options QDict. | |
3298 | */ | |
3299 | BdrvChild *bdrv_open_child(const char *filename, | |
3300 | QDict *options, const char *bdref_key, | |
3301 | BlockDriverState *parent, | |
bd86fb99 | 3302 | const BdrvChildClass *child_class, |
258b7765 | 3303 | BdrvChildRole child_role, |
2d6b86af KW |
3304 | bool allow_none, Error **errp) |
3305 | { | |
3306 | BlockDriverState *bs; | |
3307 | ||
bd86fb99 | 3308 | bs = bdrv_open_child_bs(filename, options, bdref_key, parent, child_class, |
272c02ea | 3309 | child_role, allow_none, errp); |
2d6b86af KW |
3310 | if (bs == NULL) { |
3311 | return NULL; | |
3312 | } | |
3313 | ||
258b7765 HR |
3314 | return bdrv_attach_child(parent, bs, bdref_key, child_class, child_role, |
3315 | errp); | |
b4b059f6 KW |
3316 | } |
3317 | ||
bd86fb99 HR |
3318 | /* |
3319 | * TODO Future callers may need to specify parent/child_class in order for | |
3320 | * option inheritance to work. Existing callers use it for the root node. | |
3321 | */ | |
e1d74bc6 KW |
3322 | BlockDriverState *bdrv_open_blockdev_ref(BlockdevRef *ref, Error **errp) |
3323 | { | |
3324 | BlockDriverState *bs = NULL; | |
e1d74bc6 KW |
3325 | QObject *obj = NULL; |
3326 | QDict *qdict = NULL; | |
3327 | const char *reference = NULL; | |
3328 | Visitor *v = NULL; | |
3329 | ||
3330 | if (ref->type == QTYPE_QSTRING) { | |
3331 | reference = ref->u.reference; | |
3332 | } else { | |
3333 | BlockdevOptions *options = &ref->u.definition; | |
3334 | assert(ref->type == QTYPE_QDICT); | |
3335 | ||
3336 | v = qobject_output_visitor_new(&obj); | |
1f584248 | 3337 | visit_type_BlockdevOptions(v, NULL, &options, &error_abort); |
e1d74bc6 KW |
3338 | visit_complete(v, &obj); |
3339 | ||
7dc847eb | 3340 | qdict = qobject_to(QDict, obj); |
e1d74bc6 KW |
3341 | qdict_flatten(qdict); |
3342 | ||
3343 | /* bdrv_open_inherit() defaults to the values in bdrv_flags (for | |
3344 | * compatibility with other callers) rather than what we want as the | |
3345 | * real defaults. Apply the defaults here instead. */ | |
3346 | qdict_set_default_str(qdict, BDRV_OPT_CACHE_DIRECT, "off"); | |
3347 | qdict_set_default_str(qdict, BDRV_OPT_CACHE_NO_FLUSH, "off"); | |
3348 | qdict_set_default_str(qdict, BDRV_OPT_READ_ONLY, "off"); | |
e35bdc12 KW |
3349 | qdict_set_default_str(qdict, BDRV_OPT_AUTO_READ_ONLY, "off"); |
3350 | ||
e1d74bc6 KW |
3351 | } |
3352 | ||
272c02ea | 3353 | bs = bdrv_open_inherit(NULL, reference, qdict, 0, NULL, NULL, 0, errp); |
e1d74bc6 | 3354 | obj = NULL; |
cb3e7f08 | 3355 | qobject_unref(obj); |
e1d74bc6 KW |
3356 | visit_free(v); |
3357 | return bs; | |
3358 | } | |
3359 | ||
66836189 HR |
3360 | static BlockDriverState *bdrv_append_temp_snapshot(BlockDriverState *bs, |
3361 | int flags, | |
3362 | QDict *snapshot_options, | |
3363 | Error **errp) | |
b998875d KW |
3364 | { |
3365 | /* TODO: extra byte is a hack to ensure MAX_PATH space on Windows. */ | |
1ba4b6a5 | 3366 | char *tmp_filename = g_malloc0(PATH_MAX + 1); |
b998875d | 3367 | int64_t total_size; |
83d0521a | 3368 | QemuOpts *opts = NULL; |
ff6ed714 | 3369 | BlockDriverState *bs_snapshot = NULL; |
b998875d KW |
3370 | int ret; |
3371 | ||
3372 | /* if snapshot, we create a temporary backing file and open it | |
3373 | instead of opening 'filename' directly */ | |
3374 | ||
3375 | /* Get the required size from the image */ | |
f187743a KW |
3376 | total_size = bdrv_getlength(bs); |
3377 | if (total_size < 0) { | |
3378 | error_setg_errno(errp, -total_size, "Could not get image size"); | |
1ba4b6a5 | 3379 | goto out; |
f187743a | 3380 | } |
b998875d KW |
3381 | |
3382 | /* Create the temporary image */ | |
1ba4b6a5 | 3383 | ret = get_tmp_filename(tmp_filename, PATH_MAX + 1); |
b998875d KW |
3384 | if (ret < 0) { |
3385 | error_setg_errno(errp, -ret, "Could not get temporary filename"); | |
1ba4b6a5 | 3386 | goto out; |
b998875d KW |
3387 | } |
3388 | ||
ef810437 | 3389 | opts = qemu_opts_create(bdrv_qcow2.create_opts, NULL, 0, |
c282e1fd | 3390 | &error_abort); |
39101f25 | 3391 | qemu_opt_set_number(opts, BLOCK_OPT_SIZE, total_size, &error_abort); |
e43bfd9c | 3392 | ret = bdrv_create(&bdrv_qcow2, tmp_filename, opts, errp); |
83d0521a | 3393 | qemu_opts_del(opts); |
b998875d | 3394 | if (ret < 0) { |
e43bfd9c MA |
3395 | error_prepend(errp, "Could not create temporary overlay '%s': ", |
3396 | tmp_filename); | |
1ba4b6a5 | 3397 | goto out; |
b998875d KW |
3398 | } |
3399 | ||
73176bee | 3400 | /* Prepare options QDict for the temporary file */ |
46f5ac20 EB |
3401 | qdict_put_str(snapshot_options, "file.driver", "file"); |
3402 | qdict_put_str(snapshot_options, "file.filename", tmp_filename); | |
3403 | qdict_put_str(snapshot_options, "driver", "qcow2"); | |
b998875d | 3404 | |
5b363937 | 3405 | bs_snapshot = bdrv_open(NULL, NULL, snapshot_options, flags, errp); |
73176bee | 3406 | snapshot_options = NULL; |
5b363937 | 3407 | if (!bs_snapshot) { |
1ba4b6a5 | 3408 | goto out; |
b998875d KW |
3409 | } |
3410 | ||
934aee14 VSO |
3411 | ret = bdrv_append(bs_snapshot, bs, errp); |
3412 | if (ret < 0) { | |
ff6ed714 | 3413 | bs_snapshot = NULL; |
b2c2832c KW |
3414 | goto out; |
3415 | } | |
1ba4b6a5 BC |
3416 | |
3417 | out: | |
cb3e7f08 | 3418 | qobject_unref(snapshot_options); |
1ba4b6a5 | 3419 | g_free(tmp_filename); |
ff6ed714 | 3420 | return bs_snapshot; |
b998875d KW |
3421 | } |
3422 | ||
b6ce07aa KW |
3423 | /* |
3424 | * Opens a disk image (raw, qcow2, vmdk, ...) | |
de9c0cec KW |
3425 | * |
3426 | * options is a QDict of options to pass to the block drivers, or NULL for an | |
3427 | * empty set of options. The reference to the QDict belongs to the block layer | |
3428 | * after the call (even on failure), so if the caller intends to reuse the | |
cb3e7f08 | 3429 | * dictionary, it needs to use qobject_ref() before calling bdrv_open. |
f67503e5 HR |
3430 | * |
3431 | * If *pbs is NULL, a new BDS will be created with a pointer to it stored there. | |
3432 | * If it is not NULL, the referenced BDS will be reused. | |
ddf5636d HR |
3433 | * |
3434 | * The reference parameter may be used to specify an existing block device which | |
3435 | * should be opened. If specified, neither options nor a filename may be given, | |
3436 | * nor can an existing BDS be reused (that is, *pbs has to be NULL). | |
b6ce07aa | 3437 | */ |
5b363937 HR |
3438 | static BlockDriverState *bdrv_open_inherit(const char *filename, |
3439 | const char *reference, | |
3440 | QDict *options, int flags, | |
3441 | BlockDriverState *parent, | |
bd86fb99 | 3442 | const BdrvChildClass *child_class, |
272c02ea | 3443 | BdrvChildRole child_role, |
5b363937 | 3444 | Error **errp) |
ea2384d3 | 3445 | { |
b6ce07aa | 3446 | int ret; |
5696c6e3 | 3447 | BlockBackend *file = NULL; |
9a4f4c31 | 3448 | BlockDriverState *bs; |
ce343771 | 3449 | BlockDriver *drv = NULL; |
2f624b80 | 3450 | BdrvChild *child; |
74fe54f2 | 3451 | const char *drvname; |
3e8c2e57 | 3452 | const char *backing; |
34b5d2c6 | 3453 | Error *local_err = NULL; |
73176bee | 3454 | QDict *snapshot_options = NULL; |
b1e6fc08 | 3455 | int snapshot_flags = 0; |
712e7874 | 3456 | |
bd86fb99 HR |
3457 | assert(!child_class || !flags); |
3458 | assert(!child_class == !parent); | |
f67503e5 | 3459 | |
ddf5636d HR |
3460 | if (reference) { |
3461 | bool options_non_empty = options ? qdict_size(options) : false; | |
cb3e7f08 | 3462 | qobject_unref(options); |
ddf5636d | 3463 | |
ddf5636d HR |
3464 | if (filename || options_non_empty) { |
3465 | error_setg(errp, "Cannot reference an existing block device with " | |
3466 | "additional options or a new filename"); | |
5b363937 | 3467 | return NULL; |
ddf5636d HR |
3468 | } |
3469 | ||
3470 | bs = bdrv_lookup_bs(reference, reference, errp); | |
3471 | if (!bs) { | |
5b363937 | 3472 | return NULL; |
ddf5636d | 3473 | } |
76b22320 | 3474 | |
ddf5636d | 3475 | bdrv_ref(bs); |
5b363937 | 3476 | return bs; |
ddf5636d HR |
3477 | } |
3478 | ||
5b363937 | 3479 | bs = bdrv_new(); |
f67503e5 | 3480 | |
de9c0cec KW |
3481 | /* NULL means an empty set of options */ |
3482 | if (options == NULL) { | |
3483 | options = qdict_new(); | |
3484 | } | |
3485 | ||
145f598e | 3486 | /* json: syntax counts as explicit options, as if in the QDict */ |
de3b53f0 KW |
3487 | parse_json_protocol(options, &filename, &local_err); |
3488 | if (local_err) { | |
de3b53f0 KW |
3489 | goto fail; |
3490 | } | |
3491 | ||
145f598e KW |
3492 | bs->explicit_options = qdict_clone_shallow(options); |
3493 | ||
bd86fb99 | 3494 | if (child_class) { |
3cdc69d3 HR |
3495 | bool parent_is_format; |
3496 | ||
3497 | if (parent->drv) { | |
3498 | parent_is_format = parent->drv->is_format; | |
3499 | } else { | |
3500 | /* | |
3501 | * parent->drv is not set yet because this node is opened for | |
3502 | * (potential) format probing. That means that @parent is going | |
3503 | * to be a format node. | |
3504 | */ | |
3505 | parent_is_format = true; | |
3506 | } | |
3507 | ||
bddcec37 | 3508 | bs->inherits_from = parent; |
3cdc69d3 HR |
3509 | child_class->inherit_options(child_role, parent_is_format, |
3510 | &flags, options, | |
bd86fb99 | 3511 | parent->open_flags, parent->options); |
f3930ed0 KW |
3512 | } |
3513 | ||
de3b53f0 | 3514 | ret = bdrv_fill_options(&options, filename, &flags, &local_err); |
dfde483e | 3515 | if (ret < 0) { |
462f5bcf KW |
3516 | goto fail; |
3517 | } | |
3518 | ||
129c7d1c MA |
3519 | /* |
3520 | * Set the BDRV_O_RDWR and BDRV_O_ALLOW_RDWR flags. | |
3521 | * Caution: getting a boolean member of @options requires care. | |
3522 | * When @options come from -blockdev or blockdev_add, members are | |
3523 | * typed according to the QAPI schema, but when they come from | |
3524 | * -drive, they're all QString. | |
3525 | */ | |
f87a0e29 AG |
3526 | if (g_strcmp0(qdict_get_try_str(options, BDRV_OPT_READ_ONLY), "on") && |
3527 | !qdict_get_try_bool(options, BDRV_OPT_READ_ONLY, false)) { | |
3528 | flags |= (BDRV_O_RDWR | BDRV_O_ALLOW_RDWR); | |
3529 | } else { | |
3530 | flags &= ~BDRV_O_RDWR; | |
14499ea5 AG |
3531 | } |
3532 | ||
3533 | if (flags & BDRV_O_SNAPSHOT) { | |
3534 | snapshot_options = qdict_new(); | |
3535 | bdrv_temp_snapshot_options(&snapshot_flags, snapshot_options, | |
3536 | flags, options); | |
f87a0e29 AG |
3537 | /* Let bdrv_backing_options() override "read-only" */ |
3538 | qdict_del(options, BDRV_OPT_READ_ONLY); | |
00ff7ffd HR |
3539 | bdrv_inherited_options(BDRV_CHILD_COW, true, |
3540 | &flags, options, flags, options); | |
14499ea5 AG |
3541 | } |
3542 | ||
62392ebb KW |
3543 | bs->open_flags = flags; |
3544 | bs->options = options; | |
3545 | options = qdict_clone_shallow(options); | |
3546 | ||
76c591b0 | 3547 | /* Find the right image format driver */ |
129c7d1c | 3548 | /* See cautionary note on accessing @options above */ |
76c591b0 KW |
3549 | drvname = qdict_get_try_str(options, "driver"); |
3550 | if (drvname) { | |
3551 | drv = bdrv_find_format(drvname); | |
76c591b0 KW |
3552 | if (!drv) { |
3553 | error_setg(errp, "Unknown driver: '%s'", drvname); | |
76c591b0 KW |
3554 | goto fail; |
3555 | } | |
3556 | } | |
3557 | ||
3558 | assert(drvname || !(flags & BDRV_O_PROTOCOL)); | |
76c591b0 | 3559 | |
129c7d1c | 3560 | /* See cautionary note on accessing @options above */ |
3e8c2e57 | 3561 | backing = qdict_get_try_str(options, "backing"); |
e59a0cf1 HR |
3562 | if (qobject_to(QNull, qdict_get(options, "backing")) != NULL || |
3563 | (backing && *backing == '\0')) | |
3564 | { | |
4f7be280 HR |
3565 | if (backing) { |
3566 | warn_report("Use of \"backing\": \"\" is deprecated; " | |
3567 | "use \"backing\": null instead"); | |
3568 | } | |
3e8c2e57 | 3569 | flags |= BDRV_O_NO_BACKING; |
ae0f57f0 KW |
3570 | qdict_del(bs->explicit_options, "backing"); |
3571 | qdict_del(bs->options, "backing"); | |
3e8c2e57 AG |
3572 | qdict_del(options, "backing"); |
3573 | } | |
3574 | ||
5696c6e3 | 3575 | /* Open image file without format layer. This BlockBackend is only used for |
4e4bf5c4 KW |
3576 | * probing, the block drivers will do their own bdrv_open_child() for the |
3577 | * same BDS, which is why we put the node name back into options. */ | |
f4788adc | 3578 | if ((flags & BDRV_O_PROTOCOL) == 0) { |
5696c6e3 KW |
3579 | BlockDriverState *file_bs; |
3580 | ||
3581 | file_bs = bdrv_open_child_bs(filename, options, "file", bs, | |
58944401 HR |
3582 | &child_of_bds, BDRV_CHILD_IMAGE, |
3583 | true, &local_err); | |
1fdd6933 | 3584 | if (local_err) { |
f4788adc KW |
3585 | goto fail; |
3586 | } | |
5696c6e3 | 3587 | if (file_bs != NULL) { |
dacaa162 KW |
3588 | /* Not requesting BLK_PERM_CONSISTENT_READ because we're only |
3589 | * looking at the header to guess the image format. This works even | |
3590 | * in cases where a guest would not see a consistent state. */ | |
d861ab3a | 3591 | file = blk_new(bdrv_get_aio_context(file_bs), 0, BLK_PERM_ALL); |
d7086422 | 3592 | blk_insert_bs(file, file_bs, &local_err); |
5696c6e3 | 3593 | bdrv_unref(file_bs); |
d7086422 KW |
3594 | if (local_err) { |
3595 | goto fail; | |
3596 | } | |
5696c6e3 | 3597 | |
46f5ac20 | 3598 | qdict_put_str(options, "file", bdrv_get_node_name(file_bs)); |
4e4bf5c4 | 3599 | } |
f500a6d3 KW |
3600 | } |
3601 | ||
76c591b0 | 3602 | /* Image format probing */ |
38f3ef57 | 3603 | bs->probed = !drv; |
76c591b0 | 3604 | if (!drv && file) { |
cf2ab8fc | 3605 | ret = find_image_format(file, filename, &drv, &local_err); |
17b005f1 | 3606 | if (ret < 0) { |
8bfea15d | 3607 | goto fail; |
2a05cbe4 | 3608 | } |
62392ebb KW |
3609 | /* |
3610 | * This option update would logically belong in bdrv_fill_options(), | |
3611 | * but we first need to open bs->file for the probing to work, while | |
3612 | * opening bs->file already requires the (mostly) final set of options | |
3613 | * so that cache mode etc. can be inherited. | |
3614 | * | |
3615 | * Adding the driver later is somewhat ugly, but it's not an option | |
3616 | * that would ever be inherited, so it's correct. We just need to make | |
3617 | * sure to update both bs->options (which has the full effective | |
3618 | * options for bs) and options (which has file.* already removed). | |
3619 | */ | |
46f5ac20 EB |
3620 | qdict_put_str(bs->options, "driver", drv->format_name); |
3621 | qdict_put_str(options, "driver", drv->format_name); | |
76c591b0 | 3622 | } else if (!drv) { |
17b005f1 | 3623 | error_setg(errp, "Must specify either driver or file"); |
8bfea15d | 3624 | goto fail; |
ea2384d3 | 3625 | } |
b6ce07aa | 3626 | |
53a29513 HR |
3627 | /* BDRV_O_PROTOCOL must be set iff a protocol BDS is about to be created */ |
3628 | assert(!!(flags & BDRV_O_PROTOCOL) == !!drv->bdrv_file_open); | |
3629 | /* file must be NULL if a protocol BDS is about to be created | |
3630 | * (the inverse results in an error message from bdrv_open_common()) */ | |
3631 | assert(!(flags & BDRV_O_PROTOCOL) || !file); | |
3632 | ||
b6ce07aa | 3633 | /* Open the image */ |
82dc8b41 | 3634 | ret = bdrv_open_common(bs, file, options, &local_err); |
b6ce07aa | 3635 | if (ret < 0) { |
8bfea15d | 3636 | goto fail; |
6987307c CH |
3637 | } |
3638 | ||
4e4bf5c4 | 3639 | if (file) { |
5696c6e3 | 3640 | blk_unref(file); |
f500a6d3 KW |
3641 | file = NULL; |
3642 | } | |
3643 | ||
b6ce07aa | 3644 | /* If there is a backing file, use it */ |
9156df12 | 3645 | if ((flags & BDRV_O_NO_BACKING) == 0) { |
d9b7b057 | 3646 | ret = bdrv_open_backing_file(bs, options, "backing", &local_err); |
b6ce07aa | 3647 | if (ret < 0) { |
b6ad491a | 3648 | goto close_and_fail; |
b6ce07aa | 3649 | } |
b6ce07aa KW |
3650 | } |
3651 | ||
50196d7a AG |
3652 | /* Remove all children options and references |
3653 | * from bs->options and bs->explicit_options */ | |
2f624b80 AG |
3654 | QLIST_FOREACH(child, &bs->children, next) { |
3655 | char *child_key_dot; | |
3656 | child_key_dot = g_strdup_printf("%s.", child->name); | |
3657 | qdict_extract_subqdict(bs->explicit_options, NULL, child_key_dot); | |
3658 | qdict_extract_subqdict(bs->options, NULL, child_key_dot); | |
50196d7a AG |
3659 | qdict_del(bs->explicit_options, child->name); |
3660 | qdict_del(bs->options, child->name); | |
2f624b80 AG |
3661 | g_free(child_key_dot); |
3662 | } | |
3663 | ||
b6ad491a | 3664 | /* Check if any unknown options were used */ |
7ad2757f | 3665 | if (qdict_size(options) != 0) { |
b6ad491a | 3666 | const QDictEntry *entry = qdict_first(options); |
5acd9d81 HR |
3667 | if (flags & BDRV_O_PROTOCOL) { |
3668 | error_setg(errp, "Block protocol '%s' doesn't support the option " | |
3669 | "'%s'", drv->format_name, entry->key); | |
3670 | } else { | |
d0e46a55 HR |
3671 | error_setg(errp, |
3672 | "Block format '%s' does not support the option '%s'", | |
3673 | drv->format_name, entry->key); | |
5acd9d81 | 3674 | } |
b6ad491a | 3675 | |
b6ad491a KW |
3676 | goto close_and_fail; |
3677 | } | |
b6ad491a | 3678 | |
c01c214b | 3679 | bdrv_parent_cb_change_media(bs, true); |
b6ce07aa | 3680 | |
cb3e7f08 | 3681 | qobject_unref(options); |
8961be33 | 3682 | options = NULL; |
dd62f1ca KW |
3683 | |
3684 | /* For snapshot=on, create a temporary qcow2 overlay. bs points to the | |
3685 | * temporary snapshot afterwards. */ | |
3686 | if (snapshot_flags) { | |
66836189 HR |
3687 | BlockDriverState *snapshot_bs; |
3688 | snapshot_bs = bdrv_append_temp_snapshot(bs, snapshot_flags, | |
3689 | snapshot_options, &local_err); | |
73176bee | 3690 | snapshot_options = NULL; |
dd62f1ca KW |
3691 | if (local_err) { |
3692 | goto close_and_fail; | |
3693 | } | |
5b363937 HR |
3694 | /* We are not going to return bs but the overlay on top of it |
3695 | * (snapshot_bs); thus, we have to drop the strong reference to bs | |
3696 | * (which we obtained by calling bdrv_new()). bs will not be deleted, | |
3697 | * though, because the overlay still has a reference to it. */ | |
3698 | bdrv_unref(bs); | |
3699 | bs = snapshot_bs; | |
dd62f1ca KW |
3700 | } |
3701 | ||
5b363937 | 3702 | return bs; |
b6ce07aa | 3703 | |
8bfea15d | 3704 | fail: |
5696c6e3 | 3705 | blk_unref(file); |
cb3e7f08 MAL |
3706 | qobject_unref(snapshot_options); |
3707 | qobject_unref(bs->explicit_options); | |
3708 | qobject_unref(bs->options); | |
3709 | qobject_unref(options); | |
de9c0cec | 3710 | bs->options = NULL; |
998cbd6a | 3711 | bs->explicit_options = NULL; |
5b363937 | 3712 | bdrv_unref(bs); |
621ff94d | 3713 | error_propagate(errp, local_err); |
5b363937 | 3714 | return NULL; |
de9c0cec | 3715 | |
b6ad491a | 3716 | close_and_fail: |
5b363937 | 3717 | bdrv_unref(bs); |
cb3e7f08 MAL |
3718 | qobject_unref(snapshot_options); |
3719 | qobject_unref(options); | |
621ff94d | 3720 | error_propagate(errp, local_err); |
5b363937 | 3721 | return NULL; |
b6ce07aa KW |
3722 | } |
3723 | ||
5b363937 HR |
3724 | BlockDriverState *bdrv_open(const char *filename, const char *reference, |
3725 | QDict *options, int flags, Error **errp) | |
f3930ed0 | 3726 | { |
5b363937 | 3727 | return bdrv_open_inherit(filename, reference, options, flags, NULL, |
272c02ea | 3728 | NULL, 0, errp); |
f3930ed0 KW |
3729 | } |
3730 | ||
faf116b4 AG |
3731 | /* Return true if the NULL-terminated @list contains @str */ |
3732 | static bool is_str_in_list(const char *str, const char *const *list) | |
3733 | { | |
3734 | if (str && list) { | |
3735 | int i; | |
3736 | for (i = 0; list[i] != NULL; i++) { | |
3737 | if (!strcmp(str, list[i])) { | |
3738 | return true; | |
3739 | } | |
3740 | } | |
3741 | } | |
3742 | return false; | |
3743 | } | |
3744 | ||
3745 | /* | |
3746 | * Check that every option set in @bs->options is also set in | |
3747 | * @new_opts. | |
3748 | * | |
3749 | * Options listed in the common_options list and in | |
3750 | * @bs->drv->mutable_opts are skipped. | |
3751 | * | |
3752 | * Return 0 on success, otherwise return -EINVAL and set @errp. | |
3753 | */ | |
3754 | static int bdrv_reset_options_allowed(BlockDriverState *bs, | |
3755 | const QDict *new_opts, Error **errp) | |
3756 | { | |
3757 | const QDictEntry *e; | |
3758 | /* These options are common to all block drivers and are handled | |
3759 | * in bdrv_reopen_prepare() so they can be left out of @new_opts */ | |
3760 | const char *const common_options[] = { | |
3761 | "node-name", "discard", "cache.direct", "cache.no-flush", | |
3762 | "read-only", "auto-read-only", "detect-zeroes", NULL | |
3763 | }; | |
3764 | ||
3765 | for (e = qdict_first(bs->options); e; e = qdict_next(bs->options, e)) { | |
3766 | if (!qdict_haskey(new_opts, e->key) && | |
3767 | !is_str_in_list(e->key, common_options) && | |
3768 | !is_str_in_list(e->key, bs->drv->mutable_opts)) { | |
3769 | error_setg(errp, "Option '%s' cannot be reset " | |
3770 | "to its default value", e->key); | |
3771 | return -EINVAL; | |
3772 | } | |
3773 | } | |
3774 | ||
3775 | return 0; | |
3776 | } | |
3777 | ||
cb828c31 AG |
3778 | /* |
3779 | * Returns true if @child can be reached recursively from @bs | |
3780 | */ | |
3781 | static bool bdrv_recurse_has_child(BlockDriverState *bs, | |
3782 | BlockDriverState *child) | |
3783 | { | |
3784 | BdrvChild *c; | |
3785 | ||
3786 | if (bs == child) { | |
3787 | return true; | |
3788 | } | |
3789 | ||
3790 | QLIST_FOREACH(c, &bs->children, next) { | |
3791 | if (bdrv_recurse_has_child(c->bs, child)) { | |
3792 | return true; | |
3793 | } | |
3794 | } | |
3795 | ||
3796 | return false; | |
3797 | } | |
3798 | ||
e971aa12 JC |
3799 | /* |
3800 | * Adds a BlockDriverState to a simple queue for an atomic, transactional | |
3801 | * reopen of multiple devices. | |
3802 | * | |
859443b0 | 3803 | * bs_queue can either be an existing BlockReopenQueue that has had QTAILQ_INIT |
e971aa12 JC |
3804 | * already performed, or alternatively may be NULL a new BlockReopenQueue will |
3805 | * be created and initialized. This newly created BlockReopenQueue should be | |
3806 | * passed back in for subsequent calls that are intended to be of the same | |
3807 | * atomic 'set'. | |
3808 | * | |
3809 | * bs is the BlockDriverState to add to the reopen queue. | |
3810 | * | |
4d2cb092 KW |
3811 | * options contains the changed options for the associated bs |
3812 | * (the BlockReopenQueue takes ownership) | |
3813 | * | |
e971aa12 JC |
3814 | * flags contains the open flags for the associated bs |
3815 | * | |
3816 | * returns a pointer to bs_queue, which is either the newly allocated | |
3817 | * bs_queue, or the existing bs_queue being used. | |
3818 | * | |
1a63a907 | 3819 | * bs must be drained between bdrv_reopen_queue() and bdrv_reopen_multiple(). |
e971aa12 | 3820 | */ |
28518102 KW |
3821 | static BlockReopenQueue *bdrv_reopen_queue_child(BlockReopenQueue *bs_queue, |
3822 | BlockDriverState *bs, | |
3823 | QDict *options, | |
bd86fb99 | 3824 | const BdrvChildClass *klass, |
272c02ea | 3825 | BdrvChildRole role, |
3cdc69d3 | 3826 | bool parent_is_format, |
28518102 | 3827 | QDict *parent_options, |
077e8e20 AG |
3828 | int parent_flags, |
3829 | bool keep_old_opts) | |
e971aa12 JC |
3830 | { |
3831 | assert(bs != NULL); | |
3832 | ||
3833 | BlockReopenQueueEntry *bs_entry; | |
67251a31 | 3834 | BdrvChild *child; |
9aa09ddd AG |
3835 | QDict *old_options, *explicit_options, *options_copy; |
3836 | int flags; | |
3837 | QemuOpts *opts; | |
67251a31 | 3838 | |
1a63a907 KW |
3839 | /* Make sure that the caller remembered to use a drained section. This is |
3840 | * important to avoid graph changes between the recursive queuing here and | |
3841 | * bdrv_reopen_multiple(). */ | |
3842 | assert(bs->quiesce_counter > 0); | |
3843 | ||
e971aa12 JC |
3844 | if (bs_queue == NULL) { |
3845 | bs_queue = g_new0(BlockReopenQueue, 1); | |
859443b0 | 3846 | QTAILQ_INIT(bs_queue); |
e971aa12 JC |
3847 | } |
3848 | ||
4d2cb092 KW |
3849 | if (!options) { |
3850 | options = qdict_new(); | |
3851 | } | |
3852 | ||
5b7ba05f | 3853 | /* Check if this BlockDriverState is already in the queue */ |
859443b0 | 3854 | QTAILQ_FOREACH(bs_entry, bs_queue, entry) { |
5b7ba05f AG |
3855 | if (bs == bs_entry->state.bs) { |
3856 | break; | |
3857 | } | |
3858 | } | |
3859 | ||
28518102 KW |
3860 | /* |
3861 | * Precedence of options: | |
3862 | * 1. Explicitly passed in options (highest) | |
9aa09ddd AG |
3863 | * 2. Retained from explicitly set options of bs |
3864 | * 3. Inherited from parent node | |
3865 | * 4. Retained from effective options of bs | |
28518102 KW |
3866 | */ |
3867 | ||
145f598e | 3868 | /* Old explicitly set values (don't overwrite by inherited value) */ |
077e8e20 AG |
3869 | if (bs_entry || keep_old_opts) { |
3870 | old_options = qdict_clone_shallow(bs_entry ? | |
3871 | bs_entry->state.explicit_options : | |
3872 | bs->explicit_options); | |
3873 | bdrv_join_options(bs, options, old_options); | |
3874 | qobject_unref(old_options); | |
5b7ba05f | 3875 | } |
145f598e KW |
3876 | |
3877 | explicit_options = qdict_clone_shallow(options); | |
3878 | ||
28518102 KW |
3879 | /* Inherit from parent node */ |
3880 | if (parent_options) { | |
9aa09ddd | 3881 | flags = 0; |
3cdc69d3 | 3882 | klass->inherit_options(role, parent_is_format, &flags, options, |
272c02ea | 3883 | parent_flags, parent_options); |
9aa09ddd AG |
3884 | } else { |
3885 | flags = bdrv_get_flags(bs); | |
28518102 KW |
3886 | } |
3887 | ||
077e8e20 AG |
3888 | if (keep_old_opts) { |
3889 | /* Old values are used for options that aren't set yet */ | |
3890 | old_options = qdict_clone_shallow(bs->options); | |
3891 | bdrv_join_options(bs, options, old_options); | |
3892 | qobject_unref(old_options); | |
3893 | } | |
4d2cb092 | 3894 | |
9aa09ddd AG |
3895 | /* We have the final set of options so let's update the flags */ |
3896 | options_copy = qdict_clone_shallow(options); | |
3897 | opts = qemu_opts_create(&bdrv_runtime_opts, NULL, 0, &error_abort); | |
3898 | qemu_opts_absorb_qdict(opts, options_copy, NULL); | |
3899 | update_flags_from_options(&flags, opts); | |
3900 | qemu_opts_del(opts); | |
3901 | qobject_unref(options_copy); | |
3902 | ||
fd452021 | 3903 | /* bdrv_open_inherit() sets and clears some additional flags internally */ |
f1f25a2e | 3904 | flags &= ~BDRV_O_PROTOCOL; |
fd452021 KW |
3905 | if (flags & BDRV_O_RDWR) { |
3906 | flags |= BDRV_O_ALLOW_RDWR; | |
3907 | } | |
f1f25a2e | 3908 | |
1857c97b KW |
3909 | if (!bs_entry) { |
3910 | bs_entry = g_new0(BlockReopenQueueEntry, 1); | |
859443b0 | 3911 | QTAILQ_INSERT_TAIL(bs_queue, bs_entry, entry); |
1857c97b | 3912 | } else { |
cb3e7f08 MAL |
3913 | qobject_unref(bs_entry->state.options); |
3914 | qobject_unref(bs_entry->state.explicit_options); | |
1857c97b KW |
3915 | } |
3916 | ||
3917 | bs_entry->state.bs = bs; | |
3918 | bs_entry->state.options = options; | |
3919 | bs_entry->state.explicit_options = explicit_options; | |
3920 | bs_entry->state.flags = flags; | |
3921 | ||
30450259 KW |
3922 | /* This needs to be overwritten in bdrv_reopen_prepare() */ |
3923 | bs_entry->state.perm = UINT64_MAX; | |
3924 | bs_entry->state.shared_perm = 0; | |
3925 | ||
8546632e AG |
3926 | /* |
3927 | * If keep_old_opts is false then it means that unspecified | |
3928 | * options must be reset to their original value. We don't allow | |
3929 | * resetting 'backing' but we need to know if the option is | |
3930 | * missing in order to decide if we have to return an error. | |
3931 | */ | |
3932 | if (!keep_old_opts) { | |
3933 | bs_entry->state.backing_missing = | |
3934 | !qdict_haskey(options, "backing") && | |
3935 | !qdict_haskey(options, "backing.driver"); | |
3936 | } | |
3937 | ||
67251a31 | 3938 | QLIST_FOREACH(child, &bs->children, next) { |
8546632e AG |
3939 | QDict *new_child_options = NULL; |
3940 | bool child_keep_old = keep_old_opts; | |
67251a31 | 3941 | |
4c9dfe5d KW |
3942 | /* reopen can only change the options of block devices that were |
3943 | * implicitly created and inherited options. For other (referenced) | |
3944 | * block devices, a syntax like "backing.foo" results in an error. */ | |
67251a31 KW |
3945 | if (child->bs->inherits_from != bs) { |
3946 | continue; | |
3947 | } | |
3948 | ||
8546632e AG |
3949 | /* Check if the options contain a child reference */ |
3950 | if (qdict_haskey(options, child->name)) { | |
3951 | const char *childref = qdict_get_try_str(options, child->name); | |
3952 | /* | |
3953 | * The current child must not be reopened if the child | |
3954 | * reference is null or points to a different node. | |
3955 | */ | |
3956 | if (g_strcmp0(childref, child->bs->node_name)) { | |
3957 | continue; | |
3958 | } | |
3959 | /* | |
3960 | * If the child reference points to the current child then | |
3961 | * reopen it with its existing set of options (note that | |
3962 | * it can still inherit new options from the parent). | |
3963 | */ | |
3964 | child_keep_old = true; | |
3965 | } else { | |
3966 | /* Extract child options ("child-name.*") */ | |
3967 | char *child_key_dot = g_strdup_printf("%s.", child->name); | |
3968 | qdict_extract_subqdict(explicit_options, NULL, child_key_dot); | |
3969 | qdict_extract_subqdict(options, &new_child_options, child_key_dot); | |
3970 | g_free(child_key_dot); | |
3971 | } | |
4c9dfe5d | 3972 | |
9aa09ddd | 3973 | bdrv_reopen_queue_child(bs_queue, child->bs, new_child_options, |
3cdc69d3 HR |
3974 | child->klass, child->role, bs->drv->is_format, |
3975 | options, flags, child_keep_old); | |
e971aa12 JC |
3976 | } |
3977 | ||
e971aa12 JC |
3978 | return bs_queue; |
3979 | } | |
3980 | ||
28518102 KW |
3981 | BlockReopenQueue *bdrv_reopen_queue(BlockReopenQueue *bs_queue, |
3982 | BlockDriverState *bs, | |
077e8e20 | 3983 | QDict *options, bool keep_old_opts) |
28518102 | 3984 | { |
3cdc69d3 HR |
3985 | return bdrv_reopen_queue_child(bs_queue, bs, options, NULL, 0, false, |
3986 | NULL, 0, keep_old_opts); | |
28518102 KW |
3987 | } |
3988 | ||
e971aa12 JC |
3989 | /* |
3990 | * Reopen multiple BlockDriverStates atomically & transactionally. | |
3991 | * | |
3992 | * The queue passed in (bs_queue) must have been built up previous | |
3993 | * via bdrv_reopen_queue(). | |
3994 | * | |
3995 | * Reopens all BDS specified in the queue, with the appropriate | |
3996 | * flags. All devices are prepared for reopen, and failure of any | |
50d6a8a3 | 3997 | * device will cause all device changes to be abandoned, and intermediate |
e971aa12 JC |
3998 | * data cleaned up. |
3999 | * | |
4000 | * If all devices prepare successfully, then the changes are committed | |
4001 | * to all devices. | |
4002 | * | |
1a63a907 KW |
4003 | * All affected nodes must be drained between bdrv_reopen_queue() and |
4004 | * bdrv_reopen_multiple(). | |
e971aa12 | 4005 | */ |
5019aece | 4006 | int bdrv_reopen_multiple(BlockReopenQueue *bs_queue, Error **errp) |
e971aa12 JC |
4007 | { |
4008 | int ret = -1; | |
4009 | BlockReopenQueueEntry *bs_entry, *next; | |
e971aa12 JC |
4010 | |
4011 | assert(bs_queue != NULL); | |
4012 | ||
859443b0 | 4013 | QTAILQ_FOREACH(bs_entry, bs_queue, entry) { |
1a63a907 | 4014 | assert(bs_entry->state.bs->quiesce_counter > 0); |
a4615ab3 | 4015 | if (bdrv_reopen_prepare(&bs_entry->state, bs_queue, errp)) { |
e971aa12 JC |
4016 | goto cleanup; |
4017 | } | |
4018 | bs_entry->prepared = true; | |
4019 | } | |
4020 | ||
859443b0 | 4021 | QTAILQ_FOREACH(bs_entry, bs_queue, entry) { |
69b736e7 KW |
4022 | BDRVReopenState *state = &bs_entry->state; |
4023 | ret = bdrv_check_perm(state->bs, bs_queue, state->perm, | |
071b474f | 4024 | state->shared_perm, NULL, errp); |
69b736e7 KW |
4025 | if (ret < 0) { |
4026 | goto cleanup_perm; | |
4027 | } | |
cb828c31 AG |
4028 | /* Check if new_backing_bs would accept the new permissions */ |
4029 | if (state->replace_backing_bs && state->new_backing_bs) { | |
4030 | uint64_t nperm, nshared; | |
4031 | bdrv_child_perm(state->bs, state->new_backing_bs, | |
e5d8a406 | 4032 | NULL, bdrv_backing_role(state->bs), |
25191e5f | 4033 | bs_queue, state->perm, state->shared_perm, |
cb828c31 AG |
4034 | &nperm, &nshared); |
4035 | ret = bdrv_check_update_perm(state->new_backing_bs, NULL, | |
071b474f | 4036 | nperm, nshared, NULL, errp); |
cb828c31 AG |
4037 | if (ret < 0) { |
4038 | goto cleanup_perm; | |
4039 | } | |
4040 | } | |
69b736e7 KW |
4041 | bs_entry->perms_checked = true; |
4042 | } | |
4043 | ||
fcd6a4f4 VSO |
4044 | /* |
4045 | * If we reach this point, we have success and just need to apply the | |
4046 | * changes. | |
4047 | * | |
4048 | * Reverse order is used to comfort qcow2 driver: on commit it need to write | |
4049 | * IN_USE flag to the image, to mark bitmaps in the image as invalid. But | |
4050 | * children are usually goes after parents in reopen-queue, so go from last | |
4051 | * to first element. | |
e971aa12 | 4052 | */ |
fcd6a4f4 | 4053 | QTAILQ_FOREACH_REVERSE(bs_entry, bs_queue, entry) { |
e971aa12 JC |
4054 | bdrv_reopen_commit(&bs_entry->state); |
4055 | } | |
4056 | ||
4057 | ret = 0; | |
69b736e7 | 4058 | cleanup_perm: |
859443b0 | 4059 | QTAILQ_FOREACH_SAFE(bs_entry, bs_queue, entry, next) { |
69b736e7 KW |
4060 | BDRVReopenState *state = &bs_entry->state; |
4061 | ||
4062 | if (!bs_entry->perms_checked) { | |
4063 | continue; | |
4064 | } | |
e971aa12 | 4065 | |
69b736e7 | 4066 | if (ret == 0) { |
74ad9a3b VSO |
4067 | uint64_t perm, shared; |
4068 | ||
4069 | bdrv_get_cumulative_perm(state->bs, &perm, &shared); | |
4070 | assert(perm == state->perm); | |
4071 | assert(shared == state->shared_perm); | |
4072 | ||
4073 | bdrv_set_perm(state->bs); | |
69b736e7 KW |
4074 | } else { |
4075 | bdrv_abort_perm_update(state->bs); | |
cb828c31 AG |
4076 | if (state->replace_backing_bs && state->new_backing_bs) { |
4077 | bdrv_abort_perm_update(state->new_backing_bs); | |
4078 | } | |
69b736e7 KW |
4079 | } |
4080 | } | |
17e1e2be PK |
4081 | |
4082 | if (ret == 0) { | |
4083 | QTAILQ_FOREACH_REVERSE(bs_entry, bs_queue, entry) { | |
4084 | BlockDriverState *bs = bs_entry->state.bs; | |
4085 | ||
4086 | if (bs->drv->bdrv_reopen_commit_post) | |
4087 | bs->drv->bdrv_reopen_commit_post(&bs_entry->state); | |
4088 | } | |
4089 | } | |
e971aa12 | 4090 | cleanup: |
859443b0 | 4091 | QTAILQ_FOREACH_SAFE(bs_entry, bs_queue, entry, next) { |
1bab38e7 AG |
4092 | if (ret) { |
4093 | if (bs_entry->prepared) { | |
4094 | bdrv_reopen_abort(&bs_entry->state); | |
4095 | } | |
cb3e7f08 | 4096 | qobject_unref(bs_entry->state.explicit_options); |
4c8350fe | 4097 | qobject_unref(bs_entry->state.options); |
e971aa12 | 4098 | } |
cb828c31 AG |
4099 | if (bs_entry->state.new_backing_bs) { |
4100 | bdrv_unref(bs_entry->state.new_backing_bs); | |
4101 | } | |
e971aa12 JC |
4102 | g_free(bs_entry); |
4103 | } | |
4104 | g_free(bs_queue); | |
40840e41 | 4105 | |
e971aa12 JC |
4106 | return ret; |
4107 | } | |
4108 | ||
6e1000a8 AG |
4109 | int bdrv_reopen_set_read_only(BlockDriverState *bs, bool read_only, |
4110 | Error **errp) | |
4111 | { | |
4112 | int ret; | |
4113 | BlockReopenQueue *queue; | |
4114 | QDict *opts = qdict_new(); | |
4115 | ||
4116 | qdict_put_bool(opts, BDRV_OPT_READ_ONLY, read_only); | |
4117 | ||
4118 | bdrv_subtree_drained_begin(bs); | |
077e8e20 | 4119 | queue = bdrv_reopen_queue(NULL, bs, opts, true); |
5019aece | 4120 | ret = bdrv_reopen_multiple(queue, errp); |
6e1000a8 AG |
4121 | bdrv_subtree_drained_end(bs); |
4122 | ||
4123 | return ret; | |
4124 | } | |
4125 | ||
30450259 KW |
4126 | static BlockReopenQueueEntry *find_parent_in_reopen_queue(BlockReopenQueue *q, |
4127 | BdrvChild *c) | |
4128 | { | |
4129 | BlockReopenQueueEntry *entry; | |
4130 | ||
859443b0 | 4131 | QTAILQ_FOREACH(entry, q, entry) { |
30450259 KW |
4132 | BlockDriverState *bs = entry->state.bs; |
4133 | BdrvChild *child; | |
4134 | ||
4135 | QLIST_FOREACH(child, &bs->children, next) { | |
4136 | if (child == c) { | |
4137 | return entry; | |
4138 | } | |
4139 | } | |
4140 | } | |
4141 | ||
4142 | return NULL; | |
4143 | } | |
4144 | ||
4145 | static void bdrv_reopen_perm(BlockReopenQueue *q, BlockDriverState *bs, | |
4146 | uint64_t *perm, uint64_t *shared) | |
4147 | { | |
4148 | BdrvChild *c; | |
4149 | BlockReopenQueueEntry *parent; | |
4150 | uint64_t cumulative_perms = 0; | |
4151 | uint64_t cumulative_shared_perms = BLK_PERM_ALL; | |
4152 | ||
4153 | QLIST_FOREACH(c, &bs->parents, next_parent) { | |
4154 | parent = find_parent_in_reopen_queue(q, c); | |
4155 | if (!parent) { | |
4156 | cumulative_perms |= c->perm; | |
4157 | cumulative_shared_perms &= c->shared_perm; | |
4158 | } else { | |
4159 | uint64_t nperm, nshared; | |
4160 | ||
e5d8a406 | 4161 | bdrv_child_perm(parent->state.bs, bs, c, c->role, q, |
30450259 KW |
4162 | parent->state.perm, parent->state.shared_perm, |
4163 | &nperm, &nshared); | |
4164 | ||
4165 | cumulative_perms |= nperm; | |
4166 | cumulative_shared_perms &= nshared; | |
4167 | } | |
4168 | } | |
4169 | *perm = cumulative_perms; | |
4170 | *shared = cumulative_shared_perms; | |
4171 | } | |
e971aa12 | 4172 | |
1de6b45f KW |
4173 | static bool bdrv_reopen_can_attach(BlockDriverState *parent, |
4174 | BdrvChild *child, | |
4175 | BlockDriverState *new_child, | |
4176 | Error **errp) | |
4177 | { | |
4178 | AioContext *parent_ctx = bdrv_get_aio_context(parent); | |
4179 | AioContext *child_ctx = bdrv_get_aio_context(new_child); | |
4180 | GSList *ignore; | |
4181 | bool ret; | |
4182 | ||
4183 | ignore = g_slist_prepend(NULL, child); | |
4184 | ret = bdrv_can_set_aio_context(new_child, parent_ctx, &ignore, NULL); | |
4185 | g_slist_free(ignore); | |
4186 | if (ret) { | |
4187 | return ret; | |
4188 | } | |
4189 | ||
4190 | ignore = g_slist_prepend(NULL, child); | |
4191 | ret = bdrv_can_set_aio_context(parent, child_ctx, &ignore, errp); | |
4192 | g_slist_free(ignore); | |
4193 | return ret; | |
4194 | } | |
4195 | ||
cb828c31 AG |
4196 | /* |
4197 | * Take a BDRVReopenState and check if the value of 'backing' in the | |
4198 | * reopen_state->options QDict is valid or not. | |
4199 | * | |
4200 | * If 'backing' is missing from the QDict then return 0. | |
4201 | * | |
4202 | * If 'backing' contains the node name of the backing file of | |
4203 | * reopen_state->bs then return 0. | |
4204 | * | |
4205 | * If 'backing' contains a different node name (or is null) then check | |
4206 | * whether the current backing file can be replaced with the new one. | |
4207 | * If that's the case then reopen_state->replace_backing_bs is set to | |
4208 | * true and reopen_state->new_backing_bs contains a pointer to the new | |
4209 | * backing BlockDriverState (or NULL). | |
4210 | * | |
4211 | * Return 0 on success, otherwise return < 0 and set @errp. | |
4212 | */ | |
4213 | static int bdrv_reopen_parse_backing(BDRVReopenState *reopen_state, | |
4214 | Error **errp) | |
4215 | { | |
4216 | BlockDriverState *bs = reopen_state->bs; | |
1d42f48c | 4217 | BlockDriverState *overlay_bs, *below_bs, *new_backing_bs; |
cb828c31 AG |
4218 | QObject *value; |
4219 | const char *str; | |
4220 | ||
4221 | value = qdict_get(reopen_state->options, "backing"); | |
4222 | if (value == NULL) { | |
4223 | return 0; | |
4224 | } | |
4225 | ||
4226 | switch (qobject_type(value)) { | |
4227 | case QTYPE_QNULL: | |
4228 | new_backing_bs = NULL; | |
4229 | break; | |
4230 | case QTYPE_QSTRING: | |
410f44f5 | 4231 | str = qstring_get_str(qobject_to(QString, value)); |
cb828c31 AG |
4232 | new_backing_bs = bdrv_lookup_bs(NULL, str, errp); |
4233 | if (new_backing_bs == NULL) { | |
4234 | return -EINVAL; | |
4235 | } else if (bdrv_recurse_has_child(new_backing_bs, bs)) { | |
4236 | error_setg(errp, "Making '%s' a backing file of '%s' " | |
4237 | "would create a cycle", str, bs->node_name); | |
4238 | return -EINVAL; | |
4239 | } | |
4240 | break; | |
4241 | default: | |
4242 | /* 'backing' does not allow any other data type */ | |
4243 | g_assert_not_reached(); | |
4244 | } | |
4245 | ||
4246 | /* | |
1de6b45f KW |
4247 | * Check AioContext compatibility so that the bdrv_set_backing_hd() call in |
4248 | * bdrv_reopen_commit() won't fail. | |
cb828c31 AG |
4249 | */ |
4250 | if (new_backing_bs) { | |
1de6b45f | 4251 | if (!bdrv_reopen_can_attach(bs, bs->backing, new_backing_bs, errp)) { |
cb828c31 AG |
4252 | return -EINVAL; |
4253 | } | |
4254 | } | |
4255 | ||
1d42f48c HR |
4256 | /* |
4257 | * Ensure that @bs can really handle backing files, because we are | |
4258 | * about to give it one (or swap the existing one) | |
4259 | */ | |
4260 | if (bs->drv->is_filter) { | |
4261 | /* Filters always have a file or a backing child */ | |
4262 | if (!bs->backing) { | |
4263 | error_setg(errp, "'%s' is a %s filter node that does not support a " | |
4264 | "backing child", bs->node_name, bs->drv->format_name); | |
4265 | return -EINVAL; | |
4266 | } | |
4267 | } else if (!bs->drv->supports_backing) { | |
4268 | error_setg(errp, "Driver '%s' of node '%s' does not support backing " | |
4269 | "files", bs->drv->format_name, bs->node_name); | |
4270 | return -EINVAL; | |
4271 | } | |
4272 | ||
cb828c31 AG |
4273 | /* |
4274 | * Find the "actual" backing file by skipping all links that point | |
4275 | * to an implicit node, if any (e.g. a commit filter node). | |
1d42f48c HR |
4276 | * We cannot use any of the bdrv_skip_*() functions here because |
4277 | * those return the first explicit node, while we are looking for | |
4278 | * its overlay here. | |
cb828c31 AG |
4279 | */ |
4280 | overlay_bs = bs; | |
1d42f48c HR |
4281 | for (below_bs = bdrv_filter_or_cow_bs(overlay_bs); |
4282 | below_bs && below_bs->implicit; | |
4283 | below_bs = bdrv_filter_or_cow_bs(overlay_bs)) | |
4284 | { | |
4285 | overlay_bs = below_bs; | |
cb828c31 AG |
4286 | } |
4287 | ||
4288 | /* If we want to replace the backing file we need some extra checks */ | |
1d42f48c | 4289 | if (new_backing_bs != bdrv_filter_or_cow_bs(overlay_bs)) { |
cb828c31 AG |
4290 | /* Check for implicit nodes between bs and its backing file */ |
4291 | if (bs != overlay_bs) { | |
4292 | error_setg(errp, "Cannot change backing link if '%s' has " | |
4293 | "an implicit backing file", bs->node_name); | |
4294 | return -EPERM; | |
4295 | } | |
1d42f48c HR |
4296 | /* |
4297 | * Check if the backing link that we want to replace is frozen. | |
4298 | * Note that | |
4299 | * bdrv_filter_or_cow_child(overlay_bs) == overlay_bs->backing, | |
4300 | * because we know that overlay_bs == bs, and that @bs | |
4301 | * either is a filter that uses ->backing or a COW format BDS | |
4302 | * with bs->drv->supports_backing == true. | |
4303 | */ | |
4304 | if (bdrv_is_backing_chain_frozen(overlay_bs, | |
4305 | child_bs(overlay_bs->backing), errp)) | |
4306 | { | |
cb828c31 AG |
4307 | return -EPERM; |
4308 | } | |
4309 | reopen_state->replace_backing_bs = true; | |
4310 | if (new_backing_bs) { | |
4311 | bdrv_ref(new_backing_bs); | |
4312 | reopen_state->new_backing_bs = new_backing_bs; | |
4313 | } | |
4314 | } | |
4315 | ||
4316 | return 0; | |
4317 | } | |
4318 | ||
e971aa12 JC |
4319 | /* |
4320 | * Prepares a BlockDriverState for reopen. All changes are staged in the | |
4321 | * 'opaque' field of the BDRVReopenState, which is used and allocated by | |
4322 | * the block driver layer .bdrv_reopen_prepare() | |
4323 | * | |
4324 | * bs is the BlockDriverState to reopen | |
4325 | * flags are the new open flags | |
4326 | * queue is the reopen queue | |
4327 | * | |
4328 | * Returns 0 on success, non-zero on error. On error errp will be set | |
4329 | * as well. | |
4330 | * | |
4331 | * On failure, bdrv_reopen_abort() will be called to clean up any data. | |
4332 | * It is the responsibility of the caller to then call the abort() or | |
4333 | * commit() for any other BDS that have been left in a prepare() state | |
4334 | * | |
4335 | */ | |
53e96d1e VSO |
4336 | static int bdrv_reopen_prepare(BDRVReopenState *reopen_state, |
4337 | BlockReopenQueue *queue, Error **errp) | |
e971aa12 JC |
4338 | { |
4339 | int ret = -1; | |
e6d79c41 | 4340 | int old_flags; |
e971aa12 JC |
4341 | Error *local_err = NULL; |
4342 | BlockDriver *drv; | |
ccf9dc07 | 4343 | QemuOpts *opts; |
4c8350fe | 4344 | QDict *orig_reopen_opts; |
593b3071 | 4345 | char *discard = NULL; |
3d8ce171 | 4346 | bool read_only; |
9ad08c44 | 4347 | bool drv_prepared = false; |
e971aa12 JC |
4348 | |
4349 | assert(reopen_state != NULL); | |
4350 | assert(reopen_state->bs->drv != NULL); | |
4351 | drv = reopen_state->bs->drv; | |
4352 | ||
4c8350fe AG |
4353 | /* This function and each driver's bdrv_reopen_prepare() remove |
4354 | * entries from reopen_state->options as they are processed, so | |
4355 | * we need to make a copy of the original QDict. */ | |
4356 | orig_reopen_opts = qdict_clone_shallow(reopen_state->options); | |
4357 | ||
ccf9dc07 KW |
4358 | /* Process generic block layer options */ |
4359 | opts = qemu_opts_create(&bdrv_runtime_opts, NULL, 0, &error_abort); | |
af175e85 | 4360 | if (!qemu_opts_absorb_qdict(opts, reopen_state->options, errp)) { |
ccf9dc07 KW |
4361 | ret = -EINVAL; |
4362 | goto error; | |
4363 | } | |
4364 | ||
e6d79c41 AG |
4365 | /* This was already called in bdrv_reopen_queue_child() so the flags |
4366 | * are up-to-date. This time we simply want to remove the options from | |
4367 | * QemuOpts in order to indicate that they have been processed. */ | |
4368 | old_flags = reopen_state->flags; | |
91a097e7 | 4369 | update_flags_from_options(&reopen_state->flags, opts); |
e6d79c41 | 4370 | assert(old_flags == reopen_state->flags); |
91a097e7 | 4371 | |
415bbca8 | 4372 | discard = qemu_opt_get_del(opts, BDRV_OPT_DISCARD); |
593b3071 AG |
4373 | if (discard != NULL) { |
4374 | if (bdrv_parse_discard_flags(discard, &reopen_state->flags) != 0) { | |
4375 | error_setg(errp, "Invalid discard option"); | |
4376 | ret = -EINVAL; | |
4377 | goto error; | |
4378 | } | |
4379 | } | |
4380 | ||
543770bd AG |
4381 | reopen_state->detect_zeroes = |
4382 | bdrv_parse_detect_zeroes(opts, reopen_state->flags, &local_err); | |
4383 | if (local_err) { | |
4384 | error_propagate(errp, local_err); | |
4385 | ret = -EINVAL; | |
4386 | goto error; | |
4387 | } | |
4388 | ||
57f9db9a AG |
4389 | /* All other options (including node-name and driver) must be unchanged. |
4390 | * Put them back into the QDict, so that they are checked at the end | |
4391 | * of this function. */ | |
4392 | qemu_opts_to_qdict(opts, reopen_state->options); | |
ccf9dc07 | 4393 | |
3d8ce171 JC |
4394 | /* If we are to stay read-only, do not allow permission change |
4395 | * to r/w. Attempting to set to r/w may fail if either BDRV_O_ALLOW_RDWR is | |
4396 | * not set, or if the BDS still has copy_on_read enabled */ | |
4397 | read_only = !(reopen_state->flags & BDRV_O_RDWR); | |
54a32bfe | 4398 | ret = bdrv_can_set_read_only(reopen_state->bs, read_only, true, &local_err); |
3d8ce171 JC |
4399 | if (local_err) { |
4400 | error_propagate(errp, local_err); | |
e971aa12 JC |
4401 | goto error; |
4402 | } | |
4403 | ||
30450259 KW |
4404 | /* Calculate required permissions after reopening */ |
4405 | bdrv_reopen_perm(queue, reopen_state->bs, | |
4406 | &reopen_state->perm, &reopen_state->shared_perm); | |
e971aa12 JC |
4407 | |
4408 | ret = bdrv_flush(reopen_state->bs); | |
4409 | if (ret) { | |
455b0fde | 4410 | error_setg_errno(errp, -ret, "Error flushing drive"); |
e971aa12 JC |
4411 | goto error; |
4412 | } | |
4413 | ||
4414 | if (drv->bdrv_reopen_prepare) { | |
faf116b4 AG |
4415 | /* |
4416 | * If a driver-specific option is missing, it means that we | |
4417 | * should reset it to its default value. | |
4418 | * But not all options allow that, so we need to check it first. | |
4419 | */ | |
4420 | ret = bdrv_reset_options_allowed(reopen_state->bs, | |
4421 | reopen_state->options, errp); | |
4422 | if (ret) { | |
4423 | goto error; | |
4424 | } | |
4425 | ||
e971aa12 JC |
4426 | ret = drv->bdrv_reopen_prepare(reopen_state, queue, &local_err); |
4427 | if (ret) { | |
4428 | if (local_err != NULL) { | |
4429 | error_propagate(errp, local_err); | |
4430 | } else { | |
f30c66ba | 4431 | bdrv_refresh_filename(reopen_state->bs); |
d8b6895f LC |
4432 | error_setg(errp, "failed while preparing to reopen image '%s'", |
4433 | reopen_state->bs->filename); | |
e971aa12 JC |
4434 | } |
4435 | goto error; | |
4436 | } | |
4437 | } else { | |
4438 | /* It is currently mandatory to have a bdrv_reopen_prepare() | |
4439 | * handler for each supported drv. */ | |
81e5f78a AG |
4440 | error_setg(errp, "Block format '%s' used by node '%s' " |
4441 | "does not support reopening files", drv->format_name, | |
4442 | bdrv_get_device_or_node_name(reopen_state->bs)); | |
e971aa12 JC |
4443 | ret = -1; |
4444 | goto error; | |
4445 | } | |
4446 | ||
9ad08c44 HR |
4447 | drv_prepared = true; |
4448 | ||
bacd9b87 AG |
4449 | /* |
4450 | * We must provide the 'backing' option if the BDS has a backing | |
4451 | * file or if the image file has a backing file name as part of | |
4452 | * its metadata. Otherwise the 'backing' option can be omitted. | |
4453 | */ | |
4454 | if (drv->supports_backing && reopen_state->backing_missing && | |
1d42f48c | 4455 | (reopen_state->bs->backing || reopen_state->bs->backing_file[0])) { |
8546632e AG |
4456 | error_setg(errp, "backing is missing for '%s'", |
4457 | reopen_state->bs->node_name); | |
4458 | ret = -EINVAL; | |
4459 | goto error; | |
4460 | } | |
4461 | ||
cb828c31 AG |
4462 | /* |
4463 | * Allow changing the 'backing' option. The new value can be | |
4464 | * either a reference to an existing node (using its node name) | |
4465 | * or NULL to simply detach the current backing file. | |
4466 | */ | |
4467 | ret = bdrv_reopen_parse_backing(reopen_state, errp); | |
4468 | if (ret < 0) { | |
4469 | goto error; | |
4470 | } | |
4471 | qdict_del(reopen_state->options, "backing"); | |
4472 | ||
4d2cb092 KW |
4473 | /* Options that are not handled are only okay if they are unchanged |
4474 | * compared to the old state. It is expected that some options are only | |
4475 | * used for the initial open, but not reopen (e.g. filename) */ | |
4476 | if (qdict_size(reopen_state->options)) { | |
4477 | const QDictEntry *entry = qdict_first(reopen_state->options); | |
4478 | ||
4479 | do { | |
54fd1b0d HR |
4480 | QObject *new = entry->value; |
4481 | QObject *old = qdict_get(reopen_state->bs->options, entry->key); | |
4482 | ||
db905283 AG |
4483 | /* Allow child references (child_name=node_name) as long as they |
4484 | * point to the current child (i.e. everything stays the same). */ | |
4485 | if (qobject_type(new) == QTYPE_QSTRING) { | |
4486 | BdrvChild *child; | |
4487 | QLIST_FOREACH(child, &reopen_state->bs->children, next) { | |
4488 | if (!strcmp(child->name, entry->key)) { | |
4489 | break; | |
4490 | } | |
4491 | } | |
4492 | ||
4493 | if (child) { | |
410f44f5 MA |
4494 | if (!strcmp(child->bs->node_name, |
4495 | qstring_get_str(qobject_to(QString, new)))) { | |
db905283 AG |
4496 | continue; /* Found child with this name, skip option */ |
4497 | } | |
4498 | } | |
4499 | } | |
4500 | ||
129c7d1c | 4501 | /* |
54fd1b0d HR |
4502 | * TODO: When using -drive to specify blockdev options, all values |
4503 | * will be strings; however, when using -blockdev, blockdev-add or | |
4504 | * filenames using the json:{} pseudo-protocol, they will be | |
4505 | * correctly typed. | |
4506 | * In contrast, reopening options are (currently) always strings | |
4507 | * (because you can only specify them through qemu-io; all other | |
4508 | * callers do not specify any options). | |
4509 | * Therefore, when using anything other than -drive to create a BDS, | |
4510 | * this cannot detect non-string options as unchanged, because | |
4511 | * qobject_is_equal() always returns false for objects of different | |
4512 | * type. In the future, this should be remedied by correctly typing | |
4513 | * all options. For now, this is not too big of an issue because | |
4514 | * the user can simply omit options which cannot be changed anyway, | |
4515 | * so they will stay unchanged. | |
129c7d1c | 4516 | */ |
54fd1b0d | 4517 | if (!qobject_is_equal(new, old)) { |
4d2cb092 KW |
4518 | error_setg(errp, "Cannot change the option '%s'", entry->key); |
4519 | ret = -EINVAL; | |
4520 | goto error; | |
4521 | } | |
4522 | } while ((entry = qdict_next(reopen_state->options, entry))); | |
4523 | } | |
4524 | ||
e971aa12 JC |
4525 | ret = 0; |
4526 | ||
4c8350fe AG |
4527 | /* Restore the original reopen_state->options QDict */ |
4528 | qobject_unref(reopen_state->options); | |
4529 | reopen_state->options = qobject_ref(orig_reopen_opts); | |
4530 | ||
e971aa12 | 4531 | error: |
9ad08c44 HR |
4532 | if (ret < 0 && drv_prepared) { |
4533 | /* drv->bdrv_reopen_prepare() has succeeded, so we need to | |
4534 | * call drv->bdrv_reopen_abort() before signaling an error | |
4535 | * (bdrv_reopen_multiple() will not call bdrv_reopen_abort() | |
4536 | * when the respective bdrv_reopen_prepare() has failed) */ | |
4537 | if (drv->bdrv_reopen_abort) { | |
4538 | drv->bdrv_reopen_abort(reopen_state); | |
4539 | } | |
4540 | } | |
ccf9dc07 | 4541 | qemu_opts_del(opts); |
4c8350fe | 4542 | qobject_unref(orig_reopen_opts); |
593b3071 | 4543 | g_free(discard); |
e971aa12 JC |
4544 | return ret; |
4545 | } | |
4546 | ||
4547 | /* | |
4548 | * Takes the staged changes for the reopen from bdrv_reopen_prepare(), and | |
4549 | * makes them final by swapping the staging BlockDriverState contents into | |
4550 | * the active BlockDriverState contents. | |
4551 | */ | |
53e96d1e | 4552 | static void bdrv_reopen_commit(BDRVReopenState *reopen_state) |
e971aa12 JC |
4553 | { |
4554 | BlockDriver *drv; | |
50bf65ba | 4555 | BlockDriverState *bs; |
50196d7a | 4556 | BdrvChild *child; |
e971aa12 JC |
4557 | |
4558 | assert(reopen_state != NULL); | |
50bf65ba VSO |
4559 | bs = reopen_state->bs; |
4560 | drv = bs->drv; | |
e971aa12 JC |
4561 | assert(drv != NULL); |
4562 | ||
4563 | /* If there are any driver level actions to take */ | |
4564 | if (drv->bdrv_reopen_commit) { | |
4565 | drv->bdrv_reopen_commit(reopen_state); | |
4566 | } | |
4567 | ||
4568 | /* set BDS specific flags now */ | |
cb3e7f08 | 4569 | qobject_unref(bs->explicit_options); |
4c8350fe | 4570 | qobject_unref(bs->options); |
145f598e | 4571 | |
50bf65ba | 4572 | bs->explicit_options = reopen_state->explicit_options; |
4c8350fe | 4573 | bs->options = reopen_state->options; |
50bf65ba VSO |
4574 | bs->open_flags = reopen_state->flags; |
4575 | bs->read_only = !(reopen_state->flags & BDRV_O_RDWR); | |
543770bd | 4576 | bs->detect_zeroes = reopen_state->detect_zeroes; |
355ef4ac | 4577 | |
cb828c31 AG |
4578 | if (reopen_state->replace_backing_bs) { |
4579 | qdict_del(bs->explicit_options, "backing"); | |
4580 | qdict_del(bs->options, "backing"); | |
4581 | } | |
4582 | ||
50196d7a AG |
4583 | /* Remove child references from bs->options and bs->explicit_options. |
4584 | * Child options were already removed in bdrv_reopen_queue_child() */ | |
4585 | QLIST_FOREACH(child, &bs->children, next) { | |
4586 | qdict_del(bs->explicit_options, child->name); | |
4587 | qdict_del(bs->options, child->name); | |
4588 | } | |
4589 | ||
cb828c31 AG |
4590 | /* |
4591 | * Change the backing file if a new one was specified. We do this | |
4592 | * after updating bs->options, so bdrv_refresh_filename() (called | |
4593 | * from bdrv_set_backing_hd()) has the new values. | |
4594 | */ | |
4595 | if (reopen_state->replace_backing_bs) { | |
1d42f48c | 4596 | BlockDriverState *old_backing_bs = child_bs(bs->backing); |
cb828c31 AG |
4597 | assert(!old_backing_bs || !old_backing_bs->implicit); |
4598 | /* Abort the permission update on the backing bs we're detaching */ | |
4599 | if (old_backing_bs) { | |
4600 | bdrv_abort_perm_update(old_backing_bs); | |
4601 | } | |
4602 | bdrv_set_backing_hd(bs, reopen_state->new_backing_bs, &error_abort); | |
4603 | } | |
4604 | ||
50bf65ba | 4605 | bdrv_refresh_limits(bs, NULL); |
e971aa12 JC |
4606 | } |
4607 | ||
4608 | /* | |
4609 | * Abort the reopen, and delete and free the staged changes in | |
4610 | * reopen_state | |
4611 | */ | |
53e96d1e | 4612 | static void bdrv_reopen_abort(BDRVReopenState *reopen_state) |
e971aa12 JC |
4613 | { |
4614 | BlockDriver *drv; | |
4615 | ||
4616 | assert(reopen_state != NULL); | |
4617 | drv = reopen_state->bs->drv; | |
4618 | assert(drv != NULL); | |
4619 | ||
4620 | if (drv->bdrv_reopen_abort) { | |
4621 | drv->bdrv_reopen_abort(reopen_state); | |
4622 | } | |
4623 | } | |
4624 | ||
4625 | ||
64dff520 | 4626 | static void bdrv_close(BlockDriverState *bs) |
fc01f7e7 | 4627 | { |
33384421 | 4628 | BdrvAioNotifier *ban, *ban_next; |
50a3efb0 | 4629 | BdrvChild *child, *next; |
33384421 | 4630 | |
30f55fb8 | 4631 | assert(!bs->refcnt); |
99b7e775 | 4632 | |
fc27291d | 4633 | bdrv_drained_begin(bs); /* complete I/O */ |
58fda173 | 4634 | bdrv_flush(bs); |
53ec73e2 | 4635 | bdrv_drain(bs); /* in case flush left pending I/O */ |
fc27291d | 4636 | |
3cbc002c | 4637 | if (bs->drv) { |
3c005293 | 4638 | if (bs->drv->bdrv_close) { |
7b99a266 | 4639 | /* Must unfreeze all children, so bdrv_unref_child() works */ |
3c005293 VSO |
4640 | bs->drv->bdrv_close(bs); |
4641 | } | |
9a4f4c31 | 4642 | bs->drv = NULL; |
50a3efb0 | 4643 | } |
9a7dedbc | 4644 | |
50a3efb0 | 4645 | QLIST_FOREACH_SAFE(child, &bs->children, next, next) { |
dd4118c7 | 4646 | bdrv_unref_child(bs, child); |
b338082b | 4647 | } |
98f90dba | 4648 | |
dd4118c7 AG |
4649 | bs->backing = NULL; |
4650 | bs->file = NULL; | |
50a3efb0 AG |
4651 | g_free(bs->opaque); |
4652 | bs->opaque = NULL; | |
d73415a3 | 4653 | qatomic_set(&bs->copy_on_read, 0); |
50a3efb0 AG |
4654 | bs->backing_file[0] = '\0'; |
4655 | bs->backing_format[0] = '\0'; | |
4656 | bs->total_sectors = 0; | |
4657 | bs->encrypted = false; | |
4658 | bs->sg = false; | |
cb3e7f08 MAL |
4659 | qobject_unref(bs->options); |
4660 | qobject_unref(bs->explicit_options); | |
50a3efb0 AG |
4661 | bs->options = NULL; |
4662 | bs->explicit_options = NULL; | |
cb3e7f08 | 4663 | qobject_unref(bs->full_open_options); |
50a3efb0 AG |
4664 | bs->full_open_options = NULL; |
4665 | ||
cca43ae1 VSO |
4666 | bdrv_release_named_dirty_bitmaps(bs); |
4667 | assert(QLIST_EMPTY(&bs->dirty_bitmaps)); | |
4668 | ||
33384421 HR |
4669 | QLIST_FOREACH_SAFE(ban, &bs->aio_notifiers, list, ban_next) { |
4670 | g_free(ban); | |
4671 | } | |
4672 | QLIST_INIT(&bs->aio_notifiers); | |
fc27291d | 4673 | bdrv_drained_end(bs); |
1a6d3bd2 GK |
4674 | |
4675 | /* | |
4676 | * If we're still inside some bdrv_drain_all_begin()/end() sections, end | |
4677 | * them now since this BDS won't exist anymore when bdrv_drain_all_end() | |
4678 | * gets called. | |
4679 | */ | |
4680 | if (bs->quiesce_counter) { | |
4681 | bdrv_drain_all_end_quiesce(bs); | |
4682 | } | |
b338082b FB |
4683 | } |
4684 | ||
2bc93fed MK |
4685 | void bdrv_close_all(void) |
4686 | { | |
b3b5299d | 4687 | assert(job_next(NULL) == NULL); |
ca9bd24c HR |
4688 | |
4689 | /* Drop references from requests still in flight, such as canceled block | |
4690 | * jobs whose AIO context has not been polled yet */ | |
4691 | bdrv_drain_all(); | |
2bc93fed | 4692 | |
ca9bd24c HR |
4693 | blk_remove_all_bs(); |
4694 | blockdev_close_all_bdrv_states(); | |
ed78cda3 | 4695 | |
a1a2af07 | 4696 | assert(QTAILQ_EMPTY(&all_bdrv_states)); |
2bc93fed MK |
4697 | } |
4698 | ||
d0ac0380 KW |
4699 | static bool should_update_child(BdrvChild *c, BlockDriverState *to) |
4700 | { | |
2f30b7c3 VSO |
4701 | GQueue *queue; |
4702 | GHashTable *found; | |
4703 | bool ret; | |
d0ac0380 | 4704 | |
bd86fb99 | 4705 | if (c->klass->stay_at_node) { |
d0ac0380 KW |
4706 | return false; |
4707 | } | |
4708 | ||
ec9f10fe HR |
4709 | /* If the child @c belongs to the BDS @to, replacing the current |
4710 | * c->bs by @to would mean to create a loop. | |
4711 | * | |
4712 | * Such a case occurs when appending a BDS to a backing chain. | |
4713 | * For instance, imagine the following chain: | |
4714 | * | |
4715 | * guest device -> node A -> further backing chain... | |
4716 | * | |
4717 | * Now we create a new BDS B which we want to put on top of this | |
4718 | * chain, so we first attach A as its backing node: | |
4719 | * | |
4720 | * node B | |
4721 | * | | |
4722 | * v | |
4723 | * guest device -> node A -> further backing chain... | |
4724 | * | |
4725 | * Finally we want to replace A by B. When doing that, we want to | |
4726 | * replace all pointers to A by pointers to B -- except for the | |
4727 | * pointer from B because (1) that would create a loop, and (2) | |
4728 | * that pointer should simply stay intact: | |
4729 | * | |
4730 | * guest device -> node B | |
4731 | * | | |
4732 | * v | |
4733 | * node A -> further backing chain... | |
4734 | * | |
4735 | * In general, when replacing a node A (c->bs) by a node B (@to), | |
4736 | * if A is a child of B, that means we cannot replace A by B there | |
4737 | * because that would create a loop. Silently detaching A from B | |
4738 | * is also not really an option. So overall just leaving A in | |
2f30b7c3 VSO |
4739 | * place there is the most sensible choice. |
4740 | * | |
4741 | * We would also create a loop in any cases where @c is only | |
4742 | * indirectly referenced by @to. Prevent this by returning false | |
4743 | * if @c is found (by breadth-first search) anywhere in the whole | |
4744 | * subtree of @to. | |
4745 | */ | |
4746 | ||
4747 | ret = true; | |
4748 | found = g_hash_table_new(NULL, NULL); | |
4749 | g_hash_table_add(found, to); | |
4750 | queue = g_queue_new(); | |
4751 | g_queue_push_tail(queue, to); | |
4752 | ||
4753 | while (!g_queue_is_empty(queue)) { | |
4754 | BlockDriverState *v = g_queue_pop_head(queue); | |
4755 | BdrvChild *c2; | |
4756 | ||
4757 | QLIST_FOREACH(c2, &v->children, next) { | |
4758 | if (c2 == c) { | |
4759 | ret = false; | |
4760 | break; | |
4761 | } | |
4762 | ||
4763 | if (g_hash_table_contains(found, c2->bs)) { | |
4764 | continue; | |
4765 | } | |
4766 | ||
4767 | g_queue_push_tail(queue, c2->bs); | |
4768 | g_hash_table_add(found, c2->bs); | |
d0ac0380 KW |
4769 | } |
4770 | } | |
4771 | ||
2f30b7c3 VSO |
4772 | g_queue_free(queue); |
4773 | g_hash_table_destroy(found); | |
4774 | ||
4775 | return ret; | |
d0ac0380 KW |
4776 | } |
4777 | ||
313274bb VSO |
4778 | /* |
4779 | * With auto_skip=true bdrv_replace_node_common skips updating from parents | |
4780 | * if it creates a parent-child relation loop or if parent is block-job. | |
4781 | * | |
4782 | * With auto_skip=false the error is returned if from has a parent which should | |
4783 | * not be updated. | |
4784 | */ | |
a1e708fc VSO |
4785 | static int bdrv_replace_node_common(BlockDriverState *from, |
4786 | BlockDriverState *to, | |
4787 | bool auto_skip, Error **errp) | |
dd62f1ca | 4788 | { |
d0ac0380 | 4789 | BdrvChild *c, *next; |
234ac1a9 | 4790 | GSList *list = NULL, *p; |
234ac1a9 KW |
4791 | uint64_t perm = 0, shared = BLK_PERM_ALL; |
4792 | int ret; | |
4793 | ||
4794 | /* Make sure that @from doesn't go away until we have successfully attached | |
4795 | * all of its parents to @to. */ | |
4796 | bdrv_ref(from); | |
dd62f1ca | 4797 | |
f871abd6 | 4798 | assert(qemu_get_current_aio_context() == qemu_get_aio_context()); |
30dd65f3 | 4799 | assert(bdrv_get_aio_context(from) == bdrv_get_aio_context(to)); |
f871abd6 KW |
4800 | bdrv_drained_begin(from); |
4801 | ||
234ac1a9 | 4802 | /* Put all parents into @list and calculate their cumulative permissions */ |
dd62f1ca | 4803 | QLIST_FOREACH_SAFE(c, &from->parents, next_parent, next) { |
ec9f10fe | 4804 | assert(c->bs == from); |
d0ac0380 | 4805 | if (!should_update_child(c, to)) { |
313274bb VSO |
4806 | if (auto_skip) { |
4807 | continue; | |
4808 | } | |
a1e708fc | 4809 | ret = -EINVAL; |
313274bb VSO |
4810 | error_setg(errp, "Should not change '%s' link to '%s'", |
4811 | c->name, from->node_name); | |
4812 | goto out; | |
26de9438 | 4813 | } |
2cad1ebe | 4814 | if (c->frozen) { |
a1e708fc | 4815 | ret = -EPERM; |
2cad1ebe AG |
4816 | error_setg(errp, "Cannot change '%s' link to '%s'", |
4817 | c->name, from->node_name); | |
4818 | goto out; | |
4819 | } | |
234ac1a9 KW |
4820 | list = g_slist_prepend(list, c); |
4821 | perm |= c->perm; | |
4822 | shared &= c->shared_perm; | |
4823 | } | |
4824 | ||
4825 | /* Check whether the required permissions can be granted on @to, ignoring | |
4826 | * all BdrvChild in @list so that they can't block themselves. */ | |
071b474f | 4827 | ret = bdrv_check_update_perm(to, NULL, perm, shared, list, errp); |
234ac1a9 KW |
4828 | if (ret < 0) { |
4829 | bdrv_abort_perm_update(to); | |
4830 | goto out; | |
4831 | } | |
4832 | ||
4833 | /* Now actually perform the change. We performed the permission check for | |
4834 | * all elements of @list at once, so set the permissions all at once at the | |
4835 | * very end. */ | |
4836 | for (p = list; p != NULL; p = p->next) { | |
4837 | c = p->data; | |
9bd910e2 | 4838 | |
dd62f1ca | 4839 | bdrv_ref(to); |
234ac1a9 | 4840 | bdrv_replace_child_noperm(c, to); |
dd62f1ca KW |
4841 | bdrv_unref(from); |
4842 | } | |
234ac1a9 | 4843 | |
74ad9a3b | 4844 | bdrv_set_perm(to); |
234ac1a9 | 4845 | |
a1e708fc VSO |
4846 | ret = 0; |
4847 | ||
234ac1a9 KW |
4848 | out: |
4849 | g_slist_free(list); | |
f871abd6 | 4850 | bdrv_drained_end(from); |
234ac1a9 | 4851 | bdrv_unref(from); |
a1e708fc VSO |
4852 | |
4853 | return ret; | |
dd62f1ca KW |
4854 | } |
4855 | ||
a1e708fc VSO |
4856 | int bdrv_replace_node(BlockDriverState *from, BlockDriverState *to, |
4857 | Error **errp) | |
313274bb VSO |
4858 | { |
4859 | return bdrv_replace_node_common(from, to, true, errp); | |
4860 | } | |
4861 | ||
4ddc07ca PB |
4862 | /* |
4863 | * Add new bs contents at the top of an image chain while the chain is | |
4864 | * live, while keeping required fields on the top layer. | |
4865 | * | |
4866 | * This will modify the BlockDriverState fields, and swap contents | |
4867 | * between bs_new and bs_top. Both bs_new and bs_top are modified. | |
4868 | * | |
bfb197e0 | 4869 | * bs_new must not be attached to a BlockBackend. |
4ddc07ca PB |
4870 | * |
4871 | * This function does not create any image files. | |
4872 | */ | |
a1e708fc VSO |
4873 | int bdrv_append(BlockDriverState *bs_new, BlockDriverState *bs_top, |
4874 | Error **errp) | |
4ddc07ca | 4875 | { |
a1e708fc VSO |
4876 | int ret = bdrv_set_backing_hd(bs_new, bs_top, errp); |
4877 | if (ret < 0) { | |
ae9d4417 | 4878 | return ret; |
b2c2832c | 4879 | } |
dd62f1ca | 4880 | |
a1e708fc VSO |
4881 | ret = bdrv_replace_node(bs_top, bs_new, errp); |
4882 | if (ret < 0) { | |
234ac1a9 | 4883 | bdrv_set_backing_hd(bs_new, NULL, &error_abort); |
ae9d4417 | 4884 | return ret; |
234ac1a9 | 4885 | } |
4ddc07ca | 4886 | |
ae9d4417 | 4887 | return 0; |
8802d1fd JC |
4888 | } |
4889 | ||
4f6fd349 | 4890 | static void bdrv_delete(BlockDriverState *bs) |
b338082b | 4891 | { |
3718d8ab | 4892 | assert(bdrv_op_blocker_is_empty(bs)); |
4f6fd349 | 4893 | assert(!bs->refcnt); |
18846dee | 4894 | |
1b7bdbc1 | 4895 | /* remove from list, if necessary */ |
63eaaae0 KW |
4896 | if (bs->node_name[0] != '\0') { |
4897 | QTAILQ_REMOVE(&graph_bdrv_states, bs, node_list); | |
4898 | } | |
2c1d04e0 HR |
4899 | QTAILQ_REMOVE(&all_bdrv_states, bs, bs_list); |
4900 | ||
30c321f9 AK |
4901 | bdrv_close(bs); |
4902 | ||
7267c094 | 4903 | g_free(bs); |
fc01f7e7 FB |
4904 | } |
4905 | ||
8872ef78 AS |
4906 | BlockDriverState *bdrv_insert_node(BlockDriverState *bs, QDict *node_options, |
4907 | int flags, Error **errp) | |
4908 | { | |
4909 | BlockDriverState *new_node_bs; | |
4910 | Error *local_err = NULL; | |
4911 | ||
4912 | new_node_bs = bdrv_open(NULL, NULL, node_options, flags, errp); | |
4913 | if (new_node_bs == NULL) { | |
4914 | error_prepend(errp, "Could not create node: "); | |
4915 | return NULL; | |
4916 | } | |
4917 | ||
4918 | bdrv_drained_begin(bs); | |
4919 | bdrv_replace_node(bs, new_node_bs, &local_err); | |
4920 | bdrv_drained_end(bs); | |
4921 | ||
4922 | if (local_err) { | |
4923 | bdrv_unref(new_node_bs); | |
4924 | error_propagate(errp, local_err); | |
4925 | return NULL; | |
4926 | } | |
4927 | ||
4928 | return new_node_bs; | |
4929 | } | |
4930 | ||
e97fc193 AL |
4931 | /* |
4932 | * Run consistency checks on an image | |
4933 | * | |
e076f338 | 4934 | * Returns 0 if the check could be completed (it doesn't mean that the image is |
a1c7273b | 4935 | * free of errors) or -errno when an internal error occurred. The results of the |
e076f338 | 4936 | * check are stored in res. |
e97fc193 | 4937 | */ |
21c2283e VSO |
4938 | int coroutine_fn bdrv_co_check(BlockDriverState *bs, |
4939 | BdrvCheckResult *res, BdrvCheckMode fix) | |
e97fc193 | 4940 | { |
908bcd54 HR |
4941 | if (bs->drv == NULL) { |
4942 | return -ENOMEDIUM; | |
4943 | } | |
2fd61638 | 4944 | if (bs->drv->bdrv_co_check == NULL) { |
e97fc193 AL |
4945 | return -ENOTSUP; |
4946 | } | |
4947 | ||
e076f338 | 4948 | memset(res, 0, sizeof(*res)); |
2fd61638 PB |
4949 | return bs->drv->bdrv_co_check(bs, res, fix); |
4950 | } | |
4951 | ||
756e6736 KW |
4952 | /* |
4953 | * Return values: | |
4954 | * 0 - success | |
4955 | * -EINVAL - backing format specified, but no file | |
4956 | * -ENOSPC - can't update the backing file because no space is left in the | |
4957 | * image file header | |
4958 | * -ENOTSUP - format driver doesn't support changing the backing file | |
4959 | */ | |
e54ee1b3 EB |
4960 | int bdrv_change_backing_file(BlockDriverState *bs, const char *backing_file, |
4961 | const char *backing_fmt, bool warn) | |
756e6736 KW |
4962 | { |
4963 | BlockDriver *drv = bs->drv; | |
469ef350 | 4964 | int ret; |
756e6736 | 4965 | |
d470ad42 HR |
4966 | if (!drv) { |
4967 | return -ENOMEDIUM; | |
4968 | } | |
4969 | ||
5f377794 PB |
4970 | /* Backing file format doesn't make sense without a backing file */ |
4971 | if (backing_fmt && !backing_file) { | |
4972 | return -EINVAL; | |
4973 | } | |
4974 | ||
e54ee1b3 EB |
4975 | if (warn && backing_file && !backing_fmt) { |
4976 | warn_report("Deprecated use of backing file without explicit " | |
4977 | "backing format, use of this image requires " | |
4978 | "potentially unsafe format probing"); | |
4979 | } | |
4980 | ||
756e6736 | 4981 | if (drv->bdrv_change_backing_file != NULL) { |
469ef350 | 4982 | ret = drv->bdrv_change_backing_file(bs, backing_file, backing_fmt); |
756e6736 | 4983 | } else { |
469ef350 | 4984 | ret = -ENOTSUP; |
756e6736 | 4985 | } |
469ef350 PB |
4986 | |
4987 | if (ret == 0) { | |
4988 | pstrcpy(bs->backing_file, sizeof(bs->backing_file), backing_file ?: ""); | |
4989 | pstrcpy(bs->backing_format, sizeof(bs->backing_format), backing_fmt ?: ""); | |
998c2019 HR |
4990 | pstrcpy(bs->auto_backing_file, sizeof(bs->auto_backing_file), |
4991 | backing_file ?: ""); | |
469ef350 PB |
4992 | } |
4993 | return ret; | |
756e6736 KW |
4994 | } |
4995 | ||
6ebdcee2 | 4996 | /* |
dcf3f9b2 HR |
4997 | * Finds the first non-filter node above bs in the chain between |
4998 | * active and bs. The returned node is either an immediate parent of | |
4999 | * bs, or there are only filter nodes between the two. | |
6ebdcee2 JC |
5000 | * |
5001 | * Returns NULL if bs is not found in active's image chain, | |
5002 | * or if active == bs. | |
4caf0fcd JC |
5003 | * |
5004 | * Returns the bottommost base image if bs == NULL. | |
6ebdcee2 JC |
5005 | */ |
5006 | BlockDriverState *bdrv_find_overlay(BlockDriverState *active, | |
5007 | BlockDriverState *bs) | |
5008 | { | |
dcf3f9b2 HR |
5009 | bs = bdrv_skip_filters(bs); |
5010 | active = bdrv_skip_filters(active); | |
5011 | ||
5012 | while (active) { | |
5013 | BlockDriverState *next = bdrv_backing_chain_next(active); | |
5014 | if (bs == next) { | |
5015 | return active; | |
5016 | } | |
5017 | active = next; | |
6ebdcee2 JC |
5018 | } |
5019 | ||
dcf3f9b2 | 5020 | return NULL; |
4caf0fcd | 5021 | } |
6ebdcee2 | 5022 | |
4caf0fcd JC |
5023 | /* Given a BDS, searches for the base layer. */ |
5024 | BlockDriverState *bdrv_find_base(BlockDriverState *bs) | |
5025 | { | |
5026 | return bdrv_find_overlay(bs, NULL); | |
6ebdcee2 JC |
5027 | } |
5028 | ||
2cad1ebe | 5029 | /* |
7b99a266 HR |
5030 | * Return true if at least one of the COW (backing) and filter links |
5031 | * between @bs and @base is frozen. @errp is set if that's the case. | |
0f0998f6 | 5032 | * @base must be reachable from @bs, or NULL. |
2cad1ebe AG |
5033 | */ |
5034 | bool bdrv_is_backing_chain_frozen(BlockDriverState *bs, BlockDriverState *base, | |
5035 | Error **errp) | |
5036 | { | |
5037 | BlockDriverState *i; | |
7b99a266 | 5038 | BdrvChild *child; |
2cad1ebe | 5039 | |
7b99a266 HR |
5040 | for (i = bs; i != base; i = child_bs(child)) { |
5041 | child = bdrv_filter_or_cow_child(i); | |
5042 | ||
5043 | if (child && child->frozen) { | |
2cad1ebe | 5044 | error_setg(errp, "Cannot change '%s' link from '%s' to '%s'", |
7b99a266 | 5045 | child->name, i->node_name, child->bs->node_name); |
2cad1ebe AG |
5046 | return true; |
5047 | } | |
5048 | } | |
5049 | ||
5050 | return false; | |
5051 | } | |
5052 | ||
5053 | /* | |
7b99a266 | 5054 | * Freeze all COW (backing) and filter links between @bs and @base. |
2cad1ebe AG |
5055 | * If any of the links is already frozen the operation is aborted and |
5056 | * none of the links are modified. | |
0f0998f6 | 5057 | * @base must be reachable from @bs, or NULL. |
2cad1ebe AG |
5058 | * Returns 0 on success. On failure returns < 0 and sets @errp. |
5059 | */ | |
5060 | int bdrv_freeze_backing_chain(BlockDriverState *bs, BlockDriverState *base, | |
5061 | Error **errp) | |
5062 | { | |
5063 | BlockDriverState *i; | |
7b99a266 | 5064 | BdrvChild *child; |
2cad1ebe AG |
5065 | |
5066 | if (bdrv_is_backing_chain_frozen(bs, base, errp)) { | |
5067 | return -EPERM; | |
5068 | } | |
5069 | ||
7b99a266 HR |
5070 | for (i = bs; i != base; i = child_bs(child)) { |
5071 | child = bdrv_filter_or_cow_child(i); | |
5072 | if (child && child->bs->never_freeze) { | |
e5182c1c | 5073 | error_setg(errp, "Cannot freeze '%s' link to '%s'", |
7b99a266 | 5074 | child->name, child->bs->node_name); |
e5182c1c HR |
5075 | return -EPERM; |
5076 | } | |
5077 | } | |
5078 | ||
7b99a266 HR |
5079 | for (i = bs; i != base; i = child_bs(child)) { |
5080 | child = bdrv_filter_or_cow_child(i); | |
5081 | if (child) { | |
5082 | child->frozen = true; | |
0f0998f6 | 5083 | } |
2cad1ebe AG |
5084 | } |
5085 | ||
5086 | return 0; | |
5087 | } | |
5088 | ||
5089 | /* | |
7b99a266 HR |
5090 | * Unfreeze all COW (backing) and filter links between @bs and @base. |
5091 | * The caller must ensure that all links are frozen before using this | |
5092 | * function. | |
0f0998f6 | 5093 | * @base must be reachable from @bs, or NULL. |
2cad1ebe AG |
5094 | */ |
5095 | void bdrv_unfreeze_backing_chain(BlockDriverState *bs, BlockDriverState *base) | |
5096 | { | |
5097 | BlockDriverState *i; | |
7b99a266 | 5098 | BdrvChild *child; |
2cad1ebe | 5099 | |
7b99a266 HR |
5100 | for (i = bs; i != base; i = child_bs(child)) { |
5101 | child = bdrv_filter_or_cow_child(i); | |
5102 | if (child) { | |
5103 | assert(child->frozen); | |
5104 | child->frozen = false; | |
0f0998f6 | 5105 | } |
2cad1ebe AG |
5106 | } |
5107 | } | |
5108 | ||
6ebdcee2 JC |
5109 | /* |
5110 | * Drops images above 'base' up to and including 'top', and sets the image | |
5111 | * above 'top' to have base as its backing file. | |
5112 | * | |
5113 | * Requires that the overlay to 'top' is opened r/w, so that the backing file | |
5114 | * information in 'bs' can be properly updated. | |
5115 | * | |
5116 | * E.g., this will convert the following chain: | |
5117 | * bottom <- base <- intermediate <- top <- active | |
5118 | * | |
5119 | * to | |
5120 | * | |
5121 | * bottom <- base <- active | |
5122 | * | |
5123 | * It is allowed for bottom==base, in which case it converts: | |
5124 | * | |
5125 | * base <- intermediate <- top <- active | |
5126 | * | |
5127 | * to | |
5128 | * | |
5129 | * base <- active | |
5130 | * | |
54e26900 JC |
5131 | * If backing_file_str is non-NULL, it will be used when modifying top's |
5132 | * overlay image metadata. | |
5133 | * | |
6ebdcee2 JC |
5134 | * Error conditions: |
5135 | * if active == top, that is considered an error | |
5136 | * | |
5137 | */ | |
bde70715 KW |
5138 | int bdrv_drop_intermediate(BlockDriverState *top, BlockDriverState *base, |
5139 | const char *backing_file_str) | |
6ebdcee2 | 5140 | { |
6bd858b3 AG |
5141 | BlockDriverState *explicit_top = top; |
5142 | bool update_inherits_from; | |
d669ed6a | 5143 | BdrvChild *c; |
12fa4af6 | 5144 | Error *local_err = NULL; |
6ebdcee2 | 5145 | int ret = -EIO; |
d669ed6a VSO |
5146 | g_autoptr(GSList) updated_children = NULL; |
5147 | GSList *p; | |
6ebdcee2 | 5148 | |
6858eba0 | 5149 | bdrv_ref(top); |
637d54a5 | 5150 | bdrv_subtree_drained_begin(top); |
6858eba0 | 5151 | |
6ebdcee2 JC |
5152 | if (!top->drv || !base->drv) { |
5153 | goto exit; | |
5154 | } | |
5155 | ||
5db15a57 KW |
5156 | /* Make sure that base is in the backing chain of top */ |
5157 | if (!bdrv_chain_contains(top, base)) { | |
6ebdcee2 JC |
5158 | goto exit; |
5159 | } | |
5160 | ||
6bd858b3 AG |
5161 | /* If 'base' recursively inherits from 'top' then we should set |
5162 | * base->inherits_from to top->inherits_from after 'top' and all | |
5163 | * other intermediate nodes have been dropped. | |
5164 | * If 'top' is an implicit node (e.g. "commit_top") we should skip | |
5165 | * it because no one inherits from it. We use explicit_top for that. */ | |
dcf3f9b2 | 5166 | explicit_top = bdrv_skip_implicit_filters(explicit_top); |
6bd858b3 AG |
5167 | update_inherits_from = bdrv_inherits_from_recursive(base, explicit_top); |
5168 | ||
6ebdcee2 | 5169 | /* success - we can delete the intermediate states, and link top->base */ |
bde70715 KW |
5170 | /* TODO Check graph modification op blockers (BLK_PERM_GRAPH_MOD) once |
5171 | * we've figured out how they should work. */ | |
f30c66ba HR |
5172 | if (!backing_file_str) { |
5173 | bdrv_refresh_filename(base); | |
5174 | backing_file_str = base->filename; | |
5175 | } | |
61f09cea | 5176 | |
d669ed6a VSO |
5177 | QLIST_FOREACH(c, &top->parents, next_parent) { |
5178 | updated_children = g_slist_prepend(updated_children, c); | |
5179 | } | |
5180 | ||
5181 | bdrv_replace_node_common(top, base, false, &local_err); | |
5182 | if (local_err) { | |
5183 | error_report_err(local_err); | |
5184 | goto exit; | |
5185 | } | |
5186 | ||
5187 | for (p = updated_children; p; p = p->next) { | |
5188 | c = p->data; | |
12fa4af6 | 5189 | |
bd86fb99 HR |
5190 | if (c->klass->update_filename) { |
5191 | ret = c->klass->update_filename(c, base, backing_file_str, | |
5192 | &local_err); | |
61f09cea | 5193 | if (ret < 0) { |
d669ed6a VSO |
5194 | /* |
5195 | * TODO: Actually, we want to rollback all previous iterations | |
5196 | * of this loop, and (which is almost impossible) previous | |
5197 | * bdrv_replace_node()... | |
5198 | * | |
5199 | * Note, that c->klass->update_filename may lead to permission | |
5200 | * update, so it's a bad idea to call it inside permission | |
5201 | * update transaction of bdrv_replace_node. | |
5202 | */ | |
61f09cea KW |
5203 | error_report_err(local_err); |
5204 | goto exit; | |
5205 | } | |
5206 | } | |
12fa4af6 | 5207 | } |
6ebdcee2 | 5208 | |
6bd858b3 AG |
5209 | if (update_inherits_from) { |
5210 | base->inherits_from = explicit_top->inherits_from; | |
5211 | } | |
5212 | ||
6ebdcee2 | 5213 | ret = 0; |
6ebdcee2 | 5214 | exit: |
637d54a5 | 5215 | bdrv_subtree_drained_end(top); |
6858eba0 | 5216 | bdrv_unref(top); |
6ebdcee2 JC |
5217 | return ret; |
5218 | } | |
5219 | ||
081e4650 HR |
5220 | /** |
5221 | * Implementation of BlockDriver.bdrv_get_allocated_file_size() that | |
5222 | * sums the size of all data-bearing children. (This excludes backing | |
5223 | * children.) | |
5224 | */ | |
5225 | static int64_t bdrv_sum_allocated_file_size(BlockDriverState *bs) | |
5226 | { | |
5227 | BdrvChild *child; | |
5228 | int64_t child_size, sum = 0; | |
5229 | ||
5230 | QLIST_FOREACH(child, &bs->children, next) { | |
5231 | if (child->role & (BDRV_CHILD_DATA | BDRV_CHILD_METADATA | | |
5232 | BDRV_CHILD_FILTERED)) | |
5233 | { | |
5234 | child_size = bdrv_get_allocated_file_size(child->bs); | |
5235 | if (child_size < 0) { | |
5236 | return child_size; | |
5237 | } | |
5238 | sum += child_size; | |
5239 | } | |
5240 | } | |
5241 | ||
5242 | return sum; | |
5243 | } | |
5244 | ||
61007b31 SH |
5245 | /** |
5246 | * Length of a allocated file in bytes. Sparse files are counted by actual | |
5247 | * allocated space. Return < 0 if error or unknown. | |
5248 | */ | |
5249 | int64_t bdrv_get_allocated_file_size(BlockDriverState *bs) | |
71d0770c | 5250 | { |
61007b31 SH |
5251 | BlockDriver *drv = bs->drv; |
5252 | if (!drv) { | |
5253 | return -ENOMEDIUM; | |
8f4754ed | 5254 | } |
61007b31 SH |
5255 | if (drv->bdrv_get_allocated_file_size) { |
5256 | return drv->bdrv_get_allocated_file_size(bs); | |
5257 | } | |
081e4650 HR |
5258 | |
5259 | if (drv->bdrv_file_open) { | |
5260 | /* | |
5261 | * Protocol drivers default to -ENOTSUP (most of their data is | |
5262 | * not stored in any of their children (if they even have any), | |
5263 | * so there is no generic way to figure it out). | |
5264 | */ | |
5265 | return -ENOTSUP; | |
5266 | } else if (drv->is_filter) { | |
5267 | /* Filter drivers default to the size of their filtered child */ | |
5268 | return bdrv_get_allocated_file_size(bdrv_filter_bs(bs)); | |
5269 | } else { | |
5270 | /* Other drivers default to summing their children's sizes */ | |
5271 | return bdrv_sum_allocated_file_size(bs); | |
1c9805a3 SH |
5272 | } |
5273 | } | |
e7a8a783 | 5274 | |
90880ff1 SH |
5275 | /* |
5276 | * bdrv_measure: | |
5277 | * @drv: Format driver | |
5278 | * @opts: Creation options for new image | |
5279 | * @in_bs: Existing image containing data for new image (may be NULL) | |
5280 | * @errp: Error object | |
5281 | * Returns: A #BlockMeasureInfo (free using qapi_free_BlockMeasureInfo()) | |
5282 | * or NULL on error | |
5283 | * | |
5284 | * Calculate file size required to create a new image. | |
5285 | * | |
5286 | * If @in_bs is given then space for allocated clusters and zero clusters | |
5287 | * from that image are included in the calculation. If @opts contains a | |
5288 | * backing file that is shared by @in_bs then backing clusters may be omitted | |
5289 | * from the calculation. | |
5290 | * | |
5291 | * If @in_bs is NULL then the calculation includes no allocated clusters | |
5292 | * unless a preallocation option is given in @opts. | |
5293 | * | |
5294 | * Note that @in_bs may use a different BlockDriver from @drv. | |
5295 | * | |
5296 | * If an error occurs the @errp pointer is set. | |
5297 | */ | |
5298 | BlockMeasureInfo *bdrv_measure(BlockDriver *drv, QemuOpts *opts, | |
5299 | BlockDriverState *in_bs, Error **errp) | |
5300 | { | |
5301 | if (!drv->bdrv_measure) { | |
5302 | error_setg(errp, "Block driver '%s' does not support size measurement", | |
5303 | drv->format_name); | |
5304 | return NULL; | |
5305 | } | |
5306 | ||
5307 | return drv->bdrv_measure(opts, in_bs, errp); | |
5308 | } | |
5309 | ||
61007b31 SH |
5310 | /** |
5311 | * Return number of sectors on success, -errno on error. | |
1c9805a3 | 5312 | */ |
61007b31 | 5313 | int64_t bdrv_nb_sectors(BlockDriverState *bs) |
1c9805a3 | 5314 | { |
61007b31 | 5315 | BlockDriver *drv = bs->drv; |
498e386c | 5316 | |
61007b31 SH |
5317 | if (!drv) |
5318 | return -ENOMEDIUM; | |
2572b37a | 5319 | |
61007b31 SH |
5320 | if (drv->has_variable_length) { |
5321 | int ret = refresh_total_sectors(bs, bs->total_sectors); | |
5322 | if (ret < 0) { | |
5323 | return ret; | |
1c9805a3 SH |
5324 | } |
5325 | } | |
61007b31 | 5326 | return bs->total_sectors; |
1c9805a3 | 5327 | } |
b338082b | 5328 | |
61007b31 SH |
5329 | /** |
5330 | * Return length in bytes on success, -errno on error. | |
5331 | * The length is always a multiple of BDRV_SECTOR_SIZE. | |
8d3b1a2d | 5332 | */ |
61007b31 | 5333 | int64_t bdrv_getlength(BlockDriverState *bs) |
8d3b1a2d | 5334 | { |
61007b31 | 5335 | int64_t ret = bdrv_nb_sectors(bs); |
8d3b1a2d | 5336 | |
122860ba EB |
5337 | if (ret < 0) { |
5338 | return ret; | |
5339 | } | |
5340 | if (ret > INT64_MAX / BDRV_SECTOR_SIZE) { | |
5341 | return -EFBIG; | |
5342 | } | |
5343 | return ret * BDRV_SECTOR_SIZE; | |
fc01f7e7 FB |
5344 | } |
5345 | ||
61007b31 SH |
5346 | /* return 0 as number of sectors if no device present or error */ |
5347 | void bdrv_get_geometry(BlockDriverState *bs, uint64_t *nb_sectors_ptr) | |
07d27a44 | 5348 | { |
61007b31 | 5349 | int64_t nb_sectors = bdrv_nb_sectors(bs); |
07d27a44 | 5350 | |
61007b31 | 5351 | *nb_sectors_ptr = nb_sectors < 0 ? 0 : nb_sectors; |
07d27a44 MA |
5352 | } |
5353 | ||
54115412 | 5354 | bool bdrv_is_sg(BlockDriverState *bs) |
f08145fe | 5355 | { |
61007b31 | 5356 | return bs->sg; |
f08145fe KW |
5357 | } |
5358 | ||
ae23f786 HR |
5359 | /** |
5360 | * Return whether the given node supports compressed writes. | |
5361 | */ | |
5362 | bool bdrv_supports_compressed_writes(BlockDriverState *bs) | |
5363 | { | |
5364 | BlockDriverState *filtered; | |
5365 | ||
5366 | if (!bs->drv || !block_driver_can_compress(bs->drv)) { | |
5367 | return false; | |
5368 | } | |
5369 | ||
5370 | filtered = bdrv_filter_bs(bs); | |
5371 | if (filtered) { | |
5372 | /* | |
5373 | * Filters can only forward compressed writes, so we have to | |
5374 | * check the child. | |
5375 | */ | |
5376 | return bdrv_supports_compressed_writes(filtered); | |
5377 | } | |
5378 | ||
5379 | return true; | |
5380 | } | |
5381 | ||
61007b31 | 5382 | const char *bdrv_get_format_name(BlockDriverState *bs) |
40b4f539 | 5383 | { |
61007b31 | 5384 | return bs->drv ? bs->drv->format_name : NULL; |
40b4f539 KW |
5385 | } |
5386 | ||
61007b31 | 5387 | static int qsort_strcmp(const void *a, const void *b) |
40b4f539 | 5388 | { |
ceff5bd7 | 5389 | return strcmp(*(char *const *)a, *(char *const *)b); |
40b4f539 KW |
5390 | } |
5391 | ||
61007b31 | 5392 | void bdrv_iterate_format(void (*it)(void *opaque, const char *name), |
9ac404c5 | 5393 | void *opaque, bool read_only) |
40b4f539 | 5394 | { |
61007b31 SH |
5395 | BlockDriver *drv; |
5396 | int count = 0; | |
5397 | int i; | |
5398 | const char **formats = NULL; | |
40b4f539 | 5399 | |
61007b31 SH |
5400 | QLIST_FOREACH(drv, &bdrv_drivers, list) { |
5401 | if (drv->format_name) { | |
5402 | bool found = false; | |
5403 | int i = count; | |
9ac404c5 AS |
5404 | |
5405 | if (use_bdrv_whitelist && !bdrv_is_whitelisted(drv, read_only)) { | |
5406 | continue; | |
5407 | } | |
5408 | ||
61007b31 SH |
5409 | while (formats && i && !found) { |
5410 | found = !strcmp(formats[--i], drv->format_name); | |
5411 | } | |
e2a305fb | 5412 | |
61007b31 SH |
5413 | if (!found) { |
5414 | formats = g_renew(const char *, formats, count + 1); | |
5415 | formats[count++] = drv->format_name; | |
5416 | } | |
6c5a42ac | 5417 | } |
61007b31 | 5418 | } |
6c5a42ac | 5419 | |
eb0df69f HR |
5420 | for (i = 0; i < (int)ARRAY_SIZE(block_driver_modules); i++) { |
5421 | const char *format_name = block_driver_modules[i].format_name; | |
5422 | ||
5423 | if (format_name) { | |
5424 | bool found = false; | |
5425 | int j = count; | |
5426 | ||
9ac404c5 AS |
5427 | if (use_bdrv_whitelist && |
5428 | !bdrv_format_is_whitelisted(format_name, read_only)) { | |
5429 | continue; | |
5430 | } | |
5431 | ||
eb0df69f HR |
5432 | while (formats && j && !found) { |
5433 | found = !strcmp(formats[--j], format_name); | |
5434 | } | |
5435 | ||
5436 | if (!found) { | |
5437 | formats = g_renew(const char *, formats, count + 1); | |
5438 | formats[count++] = format_name; | |
5439 | } | |
5440 | } | |
5441 | } | |
5442 | ||
61007b31 | 5443 | qsort(formats, count, sizeof(formats[0]), qsort_strcmp); |
40b4f539 | 5444 | |
61007b31 SH |
5445 | for (i = 0; i < count; i++) { |
5446 | it(opaque, formats[i]); | |
5447 | } | |
40b4f539 | 5448 | |
61007b31 SH |
5449 | g_free(formats); |
5450 | } | |
40b4f539 | 5451 | |
61007b31 SH |
5452 | /* This function is to find a node in the bs graph */ |
5453 | BlockDriverState *bdrv_find_node(const char *node_name) | |
5454 | { | |
5455 | BlockDriverState *bs; | |
391827eb | 5456 | |
61007b31 | 5457 | assert(node_name); |
40b4f539 | 5458 | |
61007b31 SH |
5459 | QTAILQ_FOREACH(bs, &graph_bdrv_states, node_list) { |
5460 | if (!strcmp(node_name, bs->node_name)) { | |
5461 | return bs; | |
40b4f539 KW |
5462 | } |
5463 | } | |
61007b31 | 5464 | return NULL; |
40b4f539 KW |
5465 | } |
5466 | ||
61007b31 | 5467 | /* Put this QMP function here so it can access the static graph_bdrv_states. */ |
facda544 PK |
5468 | BlockDeviceInfoList *bdrv_named_nodes_list(bool flat, |
5469 | Error **errp) | |
40b4f539 | 5470 | { |
9812e712 | 5471 | BlockDeviceInfoList *list; |
61007b31 | 5472 | BlockDriverState *bs; |
40b4f539 | 5473 | |
61007b31 SH |
5474 | list = NULL; |
5475 | QTAILQ_FOREACH(bs, &graph_bdrv_states, node_list) { | |
facda544 | 5476 | BlockDeviceInfo *info = bdrv_block_device_info(NULL, bs, flat, errp); |
61007b31 SH |
5477 | if (!info) { |
5478 | qapi_free_BlockDeviceInfoList(list); | |
5479 | return NULL; | |
301db7c2 | 5480 | } |
9812e712 | 5481 | QAPI_LIST_PREPEND(list, info); |
301db7c2 RH |
5482 | } |
5483 | ||
61007b31 SH |
5484 | return list; |
5485 | } | |
40b4f539 | 5486 | |
5d3b4e99 VSO |
5487 | typedef struct XDbgBlockGraphConstructor { |
5488 | XDbgBlockGraph *graph; | |
5489 | GHashTable *graph_nodes; | |
5490 | } XDbgBlockGraphConstructor; | |
5491 | ||
5492 | static XDbgBlockGraphConstructor *xdbg_graph_new(void) | |
5493 | { | |
5494 | XDbgBlockGraphConstructor *gr = g_new(XDbgBlockGraphConstructor, 1); | |
5495 | ||
5496 | gr->graph = g_new0(XDbgBlockGraph, 1); | |
5497 | gr->graph_nodes = g_hash_table_new(NULL, NULL); | |
5498 | ||
5499 | return gr; | |
5500 | } | |
5501 | ||
5502 | static XDbgBlockGraph *xdbg_graph_finalize(XDbgBlockGraphConstructor *gr) | |
5503 | { | |
5504 | XDbgBlockGraph *graph = gr->graph; | |
5505 | ||
5506 | g_hash_table_destroy(gr->graph_nodes); | |
5507 | g_free(gr); | |
5508 | ||
5509 | return graph; | |
5510 | } | |
5511 | ||
5512 | static uintptr_t xdbg_graph_node_num(XDbgBlockGraphConstructor *gr, void *node) | |
5513 | { | |
5514 | uintptr_t ret = (uintptr_t)g_hash_table_lookup(gr->graph_nodes, node); | |
5515 | ||
5516 | if (ret != 0) { | |
5517 | return ret; | |
5518 | } | |
5519 | ||
5520 | /* | |
5521 | * Start counting from 1, not 0, because 0 interferes with not-found (NULL) | |
5522 | * answer of g_hash_table_lookup. | |
5523 | */ | |
5524 | ret = g_hash_table_size(gr->graph_nodes) + 1; | |
5525 | g_hash_table_insert(gr->graph_nodes, node, (void *)ret); | |
5526 | ||
5527 | return ret; | |
5528 | } | |
5529 | ||
5530 | static void xdbg_graph_add_node(XDbgBlockGraphConstructor *gr, void *node, | |
5531 | XDbgBlockGraphNodeType type, const char *name) | |
5532 | { | |
5533 | XDbgBlockGraphNode *n; | |
5534 | ||
5535 | n = g_new0(XDbgBlockGraphNode, 1); | |
5536 | ||
5537 | n->id = xdbg_graph_node_num(gr, node); | |
5538 | n->type = type; | |
5539 | n->name = g_strdup(name); | |
5540 | ||
9812e712 | 5541 | QAPI_LIST_PREPEND(gr->graph->nodes, n); |
5d3b4e99 VSO |
5542 | } |
5543 | ||
5544 | static void xdbg_graph_add_edge(XDbgBlockGraphConstructor *gr, void *parent, | |
5545 | const BdrvChild *child) | |
5546 | { | |
cdb1cec8 | 5547 | BlockPermission qapi_perm; |
5d3b4e99 VSO |
5548 | XDbgBlockGraphEdge *edge; |
5549 | ||
5d3b4e99 VSO |
5550 | edge = g_new0(XDbgBlockGraphEdge, 1); |
5551 | ||
5552 | edge->parent = xdbg_graph_node_num(gr, parent); | |
5553 | edge->child = xdbg_graph_node_num(gr, child->bs); | |
5554 | edge->name = g_strdup(child->name); | |
5555 | ||
cdb1cec8 HR |
5556 | for (qapi_perm = 0; qapi_perm < BLOCK_PERMISSION__MAX; qapi_perm++) { |
5557 | uint64_t flag = bdrv_qapi_perm_to_blk_perm(qapi_perm); | |
5558 | ||
5559 | if (flag & child->perm) { | |
9812e712 | 5560 | QAPI_LIST_PREPEND(edge->perm, qapi_perm); |
5d3b4e99 | 5561 | } |
cdb1cec8 | 5562 | if (flag & child->shared_perm) { |
9812e712 | 5563 | QAPI_LIST_PREPEND(edge->shared_perm, qapi_perm); |
5d3b4e99 VSO |
5564 | } |
5565 | } | |
5566 | ||
9812e712 | 5567 | QAPI_LIST_PREPEND(gr->graph->edges, edge); |
5d3b4e99 VSO |
5568 | } |
5569 | ||
5570 | ||
5571 | XDbgBlockGraph *bdrv_get_xdbg_block_graph(Error **errp) | |
5572 | { | |
5573 | BlockBackend *blk; | |
5574 | BlockJob *job; | |
5575 | BlockDriverState *bs; | |
5576 | BdrvChild *child; | |
5577 | XDbgBlockGraphConstructor *gr = xdbg_graph_new(); | |
5578 | ||
5579 | for (blk = blk_all_next(NULL); blk; blk = blk_all_next(blk)) { | |
5580 | char *allocated_name = NULL; | |
5581 | const char *name = blk_name(blk); | |
5582 | ||
5583 | if (!*name) { | |
5584 | name = allocated_name = blk_get_attached_dev_id(blk); | |
5585 | } | |
5586 | xdbg_graph_add_node(gr, blk, X_DBG_BLOCK_GRAPH_NODE_TYPE_BLOCK_BACKEND, | |
5587 | name); | |
5588 | g_free(allocated_name); | |
5589 | if (blk_root(blk)) { | |
5590 | xdbg_graph_add_edge(gr, blk, blk_root(blk)); | |
5591 | } | |
5592 | } | |
5593 | ||
5594 | for (job = block_job_next(NULL); job; job = block_job_next(job)) { | |
5595 | GSList *el; | |
5596 | ||
5597 | xdbg_graph_add_node(gr, job, X_DBG_BLOCK_GRAPH_NODE_TYPE_BLOCK_JOB, | |
5598 | job->job.id); | |
5599 | for (el = job->nodes; el; el = el->next) { | |
5600 | xdbg_graph_add_edge(gr, job, (BdrvChild *)el->data); | |
5601 | } | |
5602 | } | |
5603 | ||
5604 | QTAILQ_FOREACH(bs, &graph_bdrv_states, node_list) { | |
5605 | xdbg_graph_add_node(gr, bs, X_DBG_BLOCK_GRAPH_NODE_TYPE_BLOCK_DRIVER, | |
5606 | bs->node_name); | |
5607 | QLIST_FOREACH(child, &bs->children, next) { | |
5608 | xdbg_graph_add_edge(gr, bs, child); | |
5609 | } | |
5610 | } | |
5611 | ||
5612 | return xdbg_graph_finalize(gr); | |
5613 | } | |
5614 | ||
61007b31 SH |
5615 | BlockDriverState *bdrv_lookup_bs(const char *device, |
5616 | const char *node_name, | |
5617 | Error **errp) | |
5618 | { | |
5619 | BlockBackend *blk; | |
5620 | BlockDriverState *bs; | |
40b4f539 | 5621 | |
61007b31 SH |
5622 | if (device) { |
5623 | blk = blk_by_name(device); | |
40b4f539 | 5624 | |
61007b31 | 5625 | if (blk) { |
9f4ed6fb AG |
5626 | bs = blk_bs(blk); |
5627 | if (!bs) { | |
5433c24f | 5628 | error_setg(errp, "Device '%s' has no medium", device); |
5433c24f HR |
5629 | } |
5630 | ||
9f4ed6fb | 5631 | return bs; |
61007b31 SH |
5632 | } |
5633 | } | |
40b4f539 | 5634 | |
61007b31 SH |
5635 | if (node_name) { |
5636 | bs = bdrv_find_node(node_name); | |
6d519a5f | 5637 | |
61007b31 SH |
5638 | if (bs) { |
5639 | return bs; | |
5640 | } | |
40b4f539 KW |
5641 | } |
5642 | ||
785ec4b1 | 5643 | error_setg(errp, "Cannot find device=\'%s\' nor node-name=\'%s\'", |
61007b31 SH |
5644 | device ? device : "", |
5645 | node_name ? node_name : ""); | |
5646 | return NULL; | |
40b4f539 KW |
5647 | } |
5648 | ||
61007b31 SH |
5649 | /* If 'base' is in the same chain as 'top', return true. Otherwise, |
5650 | * return false. If either argument is NULL, return false. */ | |
5651 | bool bdrv_chain_contains(BlockDriverState *top, BlockDriverState *base) | |
83f64091 | 5652 | { |
61007b31 | 5653 | while (top && top != base) { |
dcf3f9b2 | 5654 | top = bdrv_filter_or_cow_bs(top); |
02c50efe | 5655 | } |
61007b31 SH |
5656 | |
5657 | return top != NULL; | |
02c50efe FZ |
5658 | } |
5659 | ||
61007b31 | 5660 | BlockDriverState *bdrv_next_node(BlockDriverState *bs) |
02c50efe | 5661 | { |
61007b31 SH |
5662 | if (!bs) { |
5663 | return QTAILQ_FIRST(&graph_bdrv_states); | |
02c50efe | 5664 | } |
61007b31 | 5665 | return QTAILQ_NEXT(bs, node_list); |
83f64091 FB |
5666 | } |
5667 | ||
0f12264e KW |
5668 | BlockDriverState *bdrv_next_all_states(BlockDriverState *bs) |
5669 | { | |
5670 | if (!bs) { | |
5671 | return QTAILQ_FIRST(&all_bdrv_states); | |
5672 | } | |
5673 | return QTAILQ_NEXT(bs, bs_list); | |
5674 | } | |
5675 | ||
61007b31 | 5676 | const char *bdrv_get_node_name(const BlockDriverState *bs) |
83f64091 | 5677 | { |
61007b31 | 5678 | return bs->node_name; |
beac80cd FB |
5679 | } |
5680 | ||
1f0c461b | 5681 | const char *bdrv_get_parent_name(const BlockDriverState *bs) |
4c265bf9 KW |
5682 | { |
5683 | BdrvChild *c; | |
5684 | const char *name; | |
5685 | ||
5686 | /* If multiple parents have a name, just pick the first one. */ | |
5687 | QLIST_FOREACH(c, &bs->parents, next_parent) { | |
bd86fb99 HR |
5688 | if (c->klass->get_name) { |
5689 | name = c->klass->get_name(c); | |
4c265bf9 KW |
5690 | if (name && *name) { |
5691 | return name; | |
5692 | } | |
5693 | } | |
5694 | } | |
5695 | ||
5696 | return NULL; | |
5697 | } | |
5698 | ||
61007b31 SH |
5699 | /* TODO check what callers really want: bs->node_name or blk_name() */ |
5700 | const char *bdrv_get_device_name(const BlockDriverState *bs) | |
beac80cd | 5701 | { |
4c265bf9 | 5702 | return bdrv_get_parent_name(bs) ?: ""; |
f141eafe | 5703 | } |
83f64091 | 5704 | |
61007b31 SH |
5705 | /* This can be used to identify nodes that might not have a device |
5706 | * name associated. Since node and device names live in the same | |
5707 | * namespace, the result is unambiguous. The exception is if both are | |
5708 | * absent, then this returns an empty (non-null) string. */ | |
5709 | const char *bdrv_get_device_or_node_name(const BlockDriverState *bs) | |
f141eafe | 5710 | { |
4c265bf9 | 5711 | return bdrv_get_parent_name(bs) ?: bs->node_name; |
beac80cd | 5712 | } |
beac80cd | 5713 | |
61007b31 | 5714 | int bdrv_get_flags(BlockDriverState *bs) |
0b5a2445 | 5715 | { |
61007b31 | 5716 | return bs->open_flags; |
0b5a2445 PB |
5717 | } |
5718 | ||
61007b31 | 5719 | int bdrv_has_zero_init_1(BlockDriverState *bs) |
68485420 | 5720 | { |
61007b31 | 5721 | return 1; |
0b5a2445 PB |
5722 | } |
5723 | ||
61007b31 | 5724 | int bdrv_has_zero_init(BlockDriverState *bs) |
0b5a2445 | 5725 | { |
93393e69 HR |
5726 | BlockDriverState *filtered; |
5727 | ||
d470ad42 HR |
5728 | if (!bs->drv) { |
5729 | return 0; | |
5730 | } | |
0b5a2445 | 5731 | |
61007b31 SH |
5732 | /* If BS is a copy on write image, it is initialized to |
5733 | the contents of the base image, which may not be zeroes. */ | |
34778172 | 5734 | if (bdrv_cow_child(bs)) { |
61007b31 SH |
5735 | return 0; |
5736 | } | |
5737 | if (bs->drv->bdrv_has_zero_init) { | |
5738 | return bs->drv->bdrv_has_zero_init(bs); | |
0b5a2445 | 5739 | } |
93393e69 HR |
5740 | |
5741 | filtered = bdrv_filter_bs(bs); | |
5742 | if (filtered) { | |
5743 | return bdrv_has_zero_init(filtered); | |
5a612c00 | 5744 | } |
61007b31 SH |
5745 | |
5746 | /* safe default */ | |
5747 | return 0; | |
68485420 KW |
5748 | } |
5749 | ||
61007b31 | 5750 | bool bdrv_can_write_zeroes_with_unmap(BlockDriverState *bs) |
68485420 | 5751 | { |
2f0342ef | 5752 | if (!(bs->open_flags & BDRV_O_UNMAP)) { |
61007b31 SH |
5753 | return false; |
5754 | } | |
68485420 | 5755 | |
e24d813b | 5756 | return bs->supported_zero_flags & BDRV_REQ_MAY_UNMAP; |
68485420 KW |
5757 | } |
5758 | ||
61007b31 SH |
5759 | void bdrv_get_backing_filename(BlockDriverState *bs, |
5760 | char *filename, int filename_size) | |
016f5cf6 | 5761 | { |
61007b31 SH |
5762 | pstrcpy(filename, filename_size, bs->backing_file); |
5763 | } | |
d318aea9 | 5764 | |
61007b31 SH |
5765 | int bdrv_get_info(BlockDriverState *bs, BlockDriverInfo *bdi) |
5766 | { | |
8b117001 | 5767 | int ret; |
61007b31 | 5768 | BlockDriver *drv = bs->drv; |
5a612c00 MP |
5769 | /* if bs->drv == NULL, bs is closed, so there's nothing to do here */ |
5770 | if (!drv) { | |
61007b31 | 5771 | return -ENOMEDIUM; |
5a612c00 MP |
5772 | } |
5773 | if (!drv->bdrv_get_info) { | |
93393e69 HR |
5774 | BlockDriverState *filtered = bdrv_filter_bs(bs); |
5775 | if (filtered) { | |
5776 | return bdrv_get_info(filtered, bdi); | |
5a612c00 | 5777 | } |
61007b31 | 5778 | return -ENOTSUP; |
5a612c00 | 5779 | } |
61007b31 | 5780 | memset(bdi, 0, sizeof(*bdi)); |
8b117001 VSO |
5781 | ret = drv->bdrv_get_info(bs, bdi); |
5782 | if (ret < 0) { | |
5783 | return ret; | |
5784 | } | |
5785 | ||
5786 | if (bdi->cluster_size > BDRV_MAX_ALIGNMENT) { | |
5787 | return -EINVAL; | |
5788 | } | |
5789 | ||
5790 | return 0; | |
61007b31 | 5791 | } |
016f5cf6 | 5792 | |
1bf6e9ca AS |
5793 | ImageInfoSpecific *bdrv_get_specific_info(BlockDriverState *bs, |
5794 | Error **errp) | |
61007b31 SH |
5795 | { |
5796 | BlockDriver *drv = bs->drv; | |
5797 | if (drv && drv->bdrv_get_specific_info) { | |
1bf6e9ca | 5798 | return drv->bdrv_get_specific_info(bs, errp); |
61007b31 SH |
5799 | } |
5800 | return NULL; | |
016f5cf6 AG |
5801 | } |
5802 | ||
d9245599 AN |
5803 | BlockStatsSpecific *bdrv_get_specific_stats(BlockDriverState *bs) |
5804 | { | |
5805 | BlockDriver *drv = bs->drv; | |
5806 | if (!drv || !drv->bdrv_get_specific_stats) { | |
5807 | return NULL; | |
5808 | } | |
5809 | return drv->bdrv_get_specific_stats(bs); | |
5810 | } | |
5811 | ||
a31939e6 | 5812 | void bdrv_debug_event(BlockDriverState *bs, BlkdebugEvent event) |
4265d620 | 5813 | { |
61007b31 SH |
5814 | if (!bs || !bs->drv || !bs->drv->bdrv_debug_event) { |
5815 | return; | |
5816 | } | |
4265d620 | 5817 | |
61007b31 | 5818 | bs->drv->bdrv_debug_event(bs, event); |
4265d620 PB |
5819 | } |
5820 | ||
d10529a2 | 5821 | static BlockDriverState *bdrv_find_debug_node(BlockDriverState *bs) |
4265d620 | 5822 | { |
61007b31 | 5823 | while (bs && bs->drv && !bs->drv->bdrv_debug_breakpoint) { |
f706a92f | 5824 | bs = bdrv_primary_bs(bs); |
61007b31 | 5825 | } |
4265d620 | 5826 | |
61007b31 | 5827 | if (bs && bs->drv && bs->drv->bdrv_debug_breakpoint) { |
d10529a2 VSO |
5828 | assert(bs->drv->bdrv_debug_remove_breakpoint); |
5829 | return bs; | |
5830 | } | |
5831 | ||
5832 | return NULL; | |
5833 | } | |
5834 | ||
5835 | int bdrv_debug_breakpoint(BlockDriverState *bs, const char *event, | |
5836 | const char *tag) | |
5837 | { | |
5838 | bs = bdrv_find_debug_node(bs); | |
5839 | if (bs) { | |
61007b31 SH |
5840 | return bs->drv->bdrv_debug_breakpoint(bs, event, tag); |
5841 | } | |
4265d620 | 5842 | |
61007b31 | 5843 | return -ENOTSUP; |
4265d620 PB |
5844 | } |
5845 | ||
61007b31 | 5846 | int bdrv_debug_remove_breakpoint(BlockDriverState *bs, const char *tag) |
ea2384d3 | 5847 | { |
d10529a2 VSO |
5848 | bs = bdrv_find_debug_node(bs); |
5849 | if (bs) { | |
61007b31 SH |
5850 | return bs->drv->bdrv_debug_remove_breakpoint(bs, tag); |
5851 | } | |
5852 | ||
5853 | return -ENOTSUP; | |
eb852011 MA |
5854 | } |
5855 | ||
61007b31 | 5856 | int bdrv_debug_resume(BlockDriverState *bs, const char *tag) |
ce1a14dc | 5857 | { |
61007b31 | 5858 | while (bs && (!bs->drv || !bs->drv->bdrv_debug_resume)) { |
f706a92f | 5859 | bs = bdrv_primary_bs(bs); |
61007b31 | 5860 | } |
ce1a14dc | 5861 | |
61007b31 SH |
5862 | if (bs && bs->drv && bs->drv->bdrv_debug_resume) { |
5863 | return bs->drv->bdrv_debug_resume(bs, tag); | |
5864 | } | |
ce1a14dc | 5865 | |
61007b31 | 5866 | return -ENOTSUP; |
f197fe2b FZ |
5867 | } |
5868 | ||
61007b31 | 5869 | bool bdrv_debug_is_suspended(BlockDriverState *bs, const char *tag) |
ce1a14dc | 5870 | { |
61007b31 | 5871 | while (bs && bs->drv && !bs->drv->bdrv_debug_is_suspended) { |
f706a92f | 5872 | bs = bdrv_primary_bs(bs); |
f197fe2b | 5873 | } |
19cb3738 | 5874 | |
61007b31 SH |
5875 | if (bs && bs->drv && bs->drv->bdrv_debug_is_suspended) { |
5876 | return bs->drv->bdrv_debug_is_suspended(bs, tag); | |
5877 | } | |
f9f05dc5 | 5878 | |
61007b31 SH |
5879 | return false; |
5880 | } | |
f9f05dc5 | 5881 | |
61007b31 SH |
5882 | /* backing_file can either be relative, or absolute, or a protocol. If it is |
5883 | * relative, it must be relative to the chain. So, passing in bs->filename | |
5884 | * from a BDS as backing_file should not be done, as that may be relative to | |
5885 | * the CWD rather than the chain. */ | |
5886 | BlockDriverState *bdrv_find_backing_image(BlockDriverState *bs, | |
5887 | const char *backing_file) | |
f9f05dc5 | 5888 | { |
61007b31 SH |
5889 | char *filename_full = NULL; |
5890 | char *backing_file_full = NULL; | |
5891 | char *filename_tmp = NULL; | |
5892 | int is_protocol = 0; | |
0b877d09 | 5893 | bool filenames_refreshed = false; |
61007b31 SH |
5894 | BlockDriverState *curr_bs = NULL; |
5895 | BlockDriverState *retval = NULL; | |
dcf3f9b2 | 5896 | BlockDriverState *bs_below; |
f9f05dc5 | 5897 | |
61007b31 SH |
5898 | if (!bs || !bs->drv || !backing_file) { |
5899 | return NULL; | |
f9f05dc5 KW |
5900 | } |
5901 | ||
61007b31 SH |
5902 | filename_full = g_malloc(PATH_MAX); |
5903 | backing_file_full = g_malloc(PATH_MAX); | |
f9f05dc5 | 5904 | |
61007b31 | 5905 | is_protocol = path_has_protocol(backing_file); |
f9f05dc5 | 5906 | |
dcf3f9b2 HR |
5907 | /* |
5908 | * Being largely a legacy function, skip any filters here | |
5909 | * (because filters do not have normal filenames, so they cannot | |
5910 | * match anyway; and allowing json:{} filenames is a bit out of | |
5911 | * scope). | |
5912 | */ | |
5913 | for (curr_bs = bdrv_skip_filters(bs); | |
5914 | bdrv_cow_child(curr_bs) != NULL; | |
5915 | curr_bs = bs_below) | |
5916 | { | |
5917 | bs_below = bdrv_backing_chain_next(curr_bs); | |
f9f05dc5 | 5918 | |
0b877d09 HR |
5919 | if (bdrv_backing_overridden(curr_bs)) { |
5920 | /* | |
5921 | * If the backing file was overridden, we can only compare | |
5922 | * directly against the backing node's filename. | |
5923 | */ | |
5924 | ||
5925 | if (!filenames_refreshed) { | |
5926 | /* | |
5927 | * This will automatically refresh all of the | |
5928 | * filenames in the rest of the backing chain, so we | |
5929 | * only need to do this once. | |
5930 | */ | |
5931 | bdrv_refresh_filename(bs_below); | |
5932 | filenames_refreshed = true; | |
5933 | } | |
5934 | ||
5935 | if (strcmp(backing_file, bs_below->filename) == 0) { | |
5936 | retval = bs_below; | |
5937 | break; | |
5938 | } | |
5939 | } else if (is_protocol || path_has_protocol(curr_bs->backing_file)) { | |
5940 | /* | |
5941 | * If either of the filename paths is actually a protocol, then | |
5942 | * compare unmodified paths; otherwise make paths relative. | |
5943 | */ | |
6b6833c1 HR |
5944 | char *backing_file_full_ret; |
5945 | ||
61007b31 | 5946 | if (strcmp(backing_file, curr_bs->backing_file) == 0) { |
dcf3f9b2 | 5947 | retval = bs_below; |
61007b31 SH |
5948 | break; |
5949 | } | |
418661e0 | 5950 | /* Also check against the full backing filename for the image */ |
6b6833c1 HR |
5951 | backing_file_full_ret = bdrv_get_full_backing_filename(curr_bs, |
5952 | NULL); | |
5953 | if (backing_file_full_ret) { | |
5954 | bool equal = strcmp(backing_file, backing_file_full_ret) == 0; | |
5955 | g_free(backing_file_full_ret); | |
5956 | if (equal) { | |
dcf3f9b2 | 5957 | retval = bs_below; |
418661e0 JC |
5958 | break; |
5959 | } | |
418661e0 | 5960 | } |
61007b31 SH |
5961 | } else { |
5962 | /* If not an absolute filename path, make it relative to the current | |
5963 | * image's filename path */ | |
2d9158ce HR |
5964 | filename_tmp = bdrv_make_absolute_filename(curr_bs, backing_file, |
5965 | NULL); | |
5966 | /* We are going to compare canonicalized absolute pathnames */ | |
5967 | if (!filename_tmp || !realpath(filename_tmp, filename_full)) { | |
5968 | g_free(filename_tmp); | |
61007b31 SH |
5969 | continue; |
5970 | } | |
2d9158ce | 5971 | g_free(filename_tmp); |
07f07615 | 5972 | |
61007b31 SH |
5973 | /* We need to make sure the backing filename we are comparing against |
5974 | * is relative to the current image filename (or absolute) */ | |
2d9158ce HR |
5975 | filename_tmp = bdrv_get_full_backing_filename(curr_bs, NULL); |
5976 | if (!filename_tmp || !realpath(filename_tmp, backing_file_full)) { | |
5977 | g_free(filename_tmp); | |
61007b31 SH |
5978 | continue; |
5979 | } | |
2d9158ce | 5980 | g_free(filename_tmp); |
eb489bb1 | 5981 | |
61007b31 | 5982 | if (strcmp(backing_file_full, filename_full) == 0) { |
dcf3f9b2 | 5983 | retval = bs_below; |
61007b31 SH |
5984 | break; |
5985 | } | |
5986 | } | |
eb489bb1 KW |
5987 | } |
5988 | ||
61007b31 SH |
5989 | g_free(filename_full); |
5990 | g_free(backing_file_full); | |
61007b31 SH |
5991 | return retval; |
5992 | } | |
5993 | ||
61007b31 SH |
5994 | void bdrv_init(void) |
5995 | { | |
5996 | module_call_init(MODULE_INIT_BLOCK); | |
5997 | } | |
29cdb251 | 5998 | |
61007b31 SH |
5999 | void bdrv_init_with_whitelist(void) |
6000 | { | |
6001 | use_bdrv_whitelist = 1; | |
6002 | bdrv_init(); | |
07f07615 PB |
6003 | } |
6004 | ||
21c2283e | 6005 | int coroutine_fn bdrv_co_invalidate_cache(BlockDriverState *bs, Error **errp) |
0f15423c | 6006 | { |
4417ab7a | 6007 | BdrvChild *child, *parent; |
5a8a30db KW |
6008 | Error *local_err = NULL; |
6009 | int ret; | |
9c98f145 | 6010 | BdrvDirtyBitmap *bm; |
5a8a30db | 6011 | |
3456a8d1 | 6012 | if (!bs->drv) { |
5416645f | 6013 | return -ENOMEDIUM; |
3456a8d1 KW |
6014 | } |
6015 | ||
16e977d5 | 6016 | QLIST_FOREACH(child, &bs->children, next) { |
2b148f39 | 6017 | bdrv_co_invalidate_cache(child->bs, &local_err); |
0d1c5c91 | 6018 | if (local_err) { |
0d1c5c91 | 6019 | error_propagate(errp, local_err); |
5416645f | 6020 | return -EINVAL; |
0d1c5c91 | 6021 | } |
5a8a30db | 6022 | } |
0d1c5c91 | 6023 | |
dafe0960 KW |
6024 | /* |
6025 | * Update permissions, they may differ for inactive nodes. | |
6026 | * | |
6027 | * Note that the required permissions of inactive images are always a | |
6028 | * subset of the permissions required after activating the image. This | |
6029 | * allows us to just get the permissions upfront without restricting | |
6030 | * drv->bdrv_invalidate_cache(). | |
6031 | * | |
6032 | * It also means that in error cases, we don't have to try and revert to | |
6033 | * the old permissions (which is an operation that could fail, too). We can | |
6034 | * just keep the extended permissions for the next time that an activation | |
6035 | * of the image is tried. | |
6036 | */ | |
7bb4941a KW |
6037 | if (bs->open_flags & BDRV_O_INACTIVE) { |
6038 | bs->open_flags &= ~BDRV_O_INACTIVE; | |
071b474f | 6039 | ret = bdrv_refresh_perms(bs, errp); |
7bb4941a | 6040 | if (ret < 0) { |
0d1c5c91 | 6041 | bs->open_flags |= BDRV_O_INACTIVE; |
5416645f | 6042 | return ret; |
0d1c5c91 | 6043 | } |
3456a8d1 | 6044 | |
7bb4941a KW |
6045 | if (bs->drv->bdrv_co_invalidate_cache) { |
6046 | bs->drv->bdrv_co_invalidate_cache(bs, &local_err); | |
6047 | if (local_err) { | |
6048 | bs->open_flags |= BDRV_O_INACTIVE; | |
6049 | error_propagate(errp, local_err); | |
5416645f | 6050 | return -EINVAL; |
7bb4941a KW |
6051 | } |
6052 | } | |
9c98f145 | 6053 | |
7bb4941a KW |
6054 | FOR_EACH_DIRTY_BITMAP(bs, bm) { |
6055 | bdrv_dirty_bitmap_skip_store(bm, false); | |
6056 | } | |
6057 | ||
6058 | ret = refresh_total_sectors(bs, bs->total_sectors); | |
6059 | if (ret < 0) { | |
6060 | bs->open_flags |= BDRV_O_INACTIVE; | |
6061 | error_setg_errno(errp, -ret, "Could not refresh total sector count"); | |
5416645f | 6062 | return ret; |
7bb4941a | 6063 | } |
5a8a30db | 6064 | } |
4417ab7a KW |
6065 | |
6066 | QLIST_FOREACH(parent, &bs->parents, next_parent) { | |
bd86fb99 HR |
6067 | if (parent->klass->activate) { |
6068 | parent->klass->activate(parent, &local_err); | |
4417ab7a | 6069 | if (local_err) { |
78fc3b3a | 6070 | bs->open_flags |= BDRV_O_INACTIVE; |
4417ab7a | 6071 | error_propagate(errp, local_err); |
5416645f | 6072 | return -EINVAL; |
4417ab7a KW |
6073 | } |
6074 | } | |
6075 | } | |
5416645f VSO |
6076 | |
6077 | return 0; | |
0f15423c AL |
6078 | } |
6079 | ||
5a8a30db | 6080 | void bdrv_invalidate_cache_all(Error **errp) |
0f15423c | 6081 | { |
7c8eece4 | 6082 | BlockDriverState *bs; |
88be7b4b | 6083 | BdrvNextIterator it; |
0f15423c | 6084 | |
88be7b4b | 6085 | for (bs = bdrv_first(&it); bs; bs = bdrv_next(&it)) { |
ed78cda3 | 6086 | AioContext *aio_context = bdrv_get_aio_context(bs); |
5416645f | 6087 | int ret; |
ed78cda3 SH |
6088 | |
6089 | aio_context_acquire(aio_context); | |
5416645f | 6090 | ret = bdrv_invalidate_cache(bs, errp); |
ed78cda3 | 6091 | aio_context_release(aio_context); |
5416645f | 6092 | if (ret < 0) { |
5e003f17 | 6093 | bdrv_next_cleanup(&it); |
5a8a30db KW |
6094 | return; |
6095 | } | |
0f15423c AL |
6096 | } |
6097 | } | |
6098 | ||
9e37271f KW |
6099 | static bool bdrv_has_bds_parent(BlockDriverState *bs, bool only_active) |
6100 | { | |
6101 | BdrvChild *parent; | |
6102 | ||
6103 | QLIST_FOREACH(parent, &bs->parents, next_parent) { | |
bd86fb99 | 6104 | if (parent->klass->parent_is_bds) { |
9e37271f KW |
6105 | BlockDriverState *parent_bs = parent->opaque; |
6106 | if (!only_active || !(parent_bs->open_flags & BDRV_O_INACTIVE)) { | |
6107 | return true; | |
6108 | } | |
6109 | } | |
6110 | } | |
6111 | ||
6112 | return false; | |
6113 | } | |
6114 | ||
6115 | static int bdrv_inactivate_recurse(BlockDriverState *bs) | |
76b1c7fe | 6116 | { |
cfa1a572 | 6117 | BdrvChild *child, *parent; |
76b1c7fe KW |
6118 | int ret; |
6119 | ||
d470ad42 HR |
6120 | if (!bs->drv) { |
6121 | return -ENOMEDIUM; | |
6122 | } | |
6123 | ||
9e37271f KW |
6124 | /* Make sure that we don't inactivate a child before its parent. |
6125 | * It will be covered by recursion from the yet active parent. */ | |
6126 | if (bdrv_has_bds_parent(bs, true)) { | |
6127 | return 0; | |
6128 | } | |
6129 | ||
6130 | assert(!(bs->open_flags & BDRV_O_INACTIVE)); | |
6131 | ||
6132 | /* Inactivate this node */ | |
6133 | if (bs->drv->bdrv_inactivate) { | |
76b1c7fe KW |
6134 | ret = bs->drv->bdrv_inactivate(bs); |
6135 | if (ret < 0) { | |
6136 | return ret; | |
6137 | } | |
6138 | } | |
6139 | ||
9e37271f | 6140 | QLIST_FOREACH(parent, &bs->parents, next_parent) { |
bd86fb99 HR |
6141 | if (parent->klass->inactivate) { |
6142 | ret = parent->klass->inactivate(parent); | |
9e37271f KW |
6143 | if (ret < 0) { |
6144 | return ret; | |
cfa1a572 KW |
6145 | } |
6146 | } | |
9e37271f | 6147 | } |
9c5e6594 | 6148 | |
9e37271f | 6149 | bs->open_flags |= BDRV_O_INACTIVE; |
7d5b5261 | 6150 | |
bb87e4d1 VSO |
6151 | /* |
6152 | * Update permissions, they may differ for inactive nodes. | |
6153 | * We only tried to loosen restrictions, so errors are not fatal, ignore | |
6154 | * them. | |
6155 | */ | |
071b474f | 6156 | bdrv_refresh_perms(bs, NULL); |
9e37271f KW |
6157 | |
6158 | /* Recursively inactivate children */ | |
38701b6a | 6159 | QLIST_FOREACH(child, &bs->children, next) { |
9e37271f | 6160 | ret = bdrv_inactivate_recurse(child->bs); |
38701b6a KW |
6161 | if (ret < 0) { |
6162 | return ret; | |
6163 | } | |
6164 | } | |
6165 | ||
76b1c7fe KW |
6166 | return 0; |
6167 | } | |
6168 | ||
6169 | int bdrv_inactivate_all(void) | |
6170 | { | |
79720af6 | 6171 | BlockDriverState *bs = NULL; |
88be7b4b | 6172 | BdrvNextIterator it; |
aad0b7a0 | 6173 | int ret = 0; |
bd6458e4 | 6174 | GSList *aio_ctxs = NULL, *ctx; |
76b1c7fe | 6175 | |
88be7b4b | 6176 | for (bs = bdrv_first(&it); bs; bs = bdrv_next(&it)) { |
bd6458e4 PB |
6177 | AioContext *aio_context = bdrv_get_aio_context(bs); |
6178 | ||
6179 | if (!g_slist_find(aio_ctxs, aio_context)) { | |
6180 | aio_ctxs = g_slist_prepend(aio_ctxs, aio_context); | |
6181 | aio_context_acquire(aio_context); | |
6182 | } | |
aad0b7a0 | 6183 | } |
76b1c7fe | 6184 | |
9e37271f KW |
6185 | for (bs = bdrv_first(&it); bs; bs = bdrv_next(&it)) { |
6186 | /* Nodes with BDS parents are covered by recursion from the last | |
6187 | * parent that gets inactivated. Don't inactivate them a second | |
6188 | * time if that has already happened. */ | |
6189 | if (bdrv_has_bds_parent(bs, false)) { | |
6190 | continue; | |
6191 | } | |
6192 | ret = bdrv_inactivate_recurse(bs); | |
6193 | if (ret < 0) { | |
6194 | bdrv_next_cleanup(&it); | |
6195 | goto out; | |
76b1c7fe KW |
6196 | } |
6197 | } | |
6198 | ||
aad0b7a0 | 6199 | out: |
bd6458e4 PB |
6200 | for (ctx = aio_ctxs; ctx != NULL; ctx = ctx->next) { |
6201 | AioContext *aio_context = ctx->data; | |
6202 | aio_context_release(aio_context); | |
aad0b7a0 | 6203 | } |
bd6458e4 | 6204 | g_slist_free(aio_ctxs); |
aad0b7a0 FZ |
6205 | |
6206 | return ret; | |
76b1c7fe KW |
6207 | } |
6208 | ||
19cb3738 FB |
6209 | /**************************************************************/ |
6210 | /* removable device support */ | |
6211 | ||
6212 | /** | |
6213 | * Return TRUE if the media is present | |
6214 | */ | |
e031f750 | 6215 | bool bdrv_is_inserted(BlockDriverState *bs) |
19cb3738 FB |
6216 | { |
6217 | BlockDriver *drv = bs->drv; | |
28d7a789 | 6218 | BdrvChild *child; |
a1aff5bf | 6219 | |
e031f750 HR |
6220 | if (!drv) { |
6221 | return false; | |
6222 | } | |
28d7a789 HR |
6223 | if (drv->bdrv_is_inserted) { |
6224 | return drv->bdrv_is_inserted(bs); | |
6225 | } | |
6226 | QLIST_FOREACH(child, &bs->children, next) { | |
6227 | if (!bdrv_is_inserted(child->bs)) { | |
6228 | return false; | |
6229 | } | |
e031f750 | 6230 | } |
28d7a789 | 6231 | return true; |
19cb3738 FB |
6232 | } |
6233 | ||
19cb3738 FB |
6234 | /** |
6235 | * If eject_flag is TRUE, eject the media. Otherwise, close the tray | |
6236 | */ | |
f36f3949 | 6237 | void bdrv_eject(BlockDriverState *bs, bool eject_flag) |
19cb3738 FB |
6238 | { |
6239 | BlockDriver *drv = bs->drv; | |
19cb3738 | 6240 | |
822e1cd1 MA |
6241 | if (drv && drv->bdrv_eject) { |
6242 | drv->bdrv_eject(bs, eject_flag); | |
19cb3738 FB |
6243 | } |
6244 | } | |
6245 | ||
19cb3738 FB |
6246 | /** |
6247 | * Lock or unlock the media (if it is locked, the user won't be able | |
6248 | * to eject it manually). | |
6249 | */ | |
025e849a | 6250 | void bdrv_lock_medium(BlockDriverState *bs, bool locked) |
19cb3738 FB |
6251 | { |
6252 | BlockDriver *drv = bs->drv; | |
6253 | ||
025e849a | 6254 | trace_bdrv_lock_medium(bs, locked); |
b8c6d095 | 6255 | |
025e849a MA |
6256 | if (drv && drv->bdrv_lock_medium) { |
6257 | drv->bdrv_lock_medium(bs, locked); | |
19cb3738 FB |
6258 | } |
6259 | } | |
985a03b0 | 6260 | |
9fcb0251 FZ |
6261 | /* Get a reference to bs */ |
6262 | void bdrv_ref(BlockDriverState *bs) | |
6263 | { | |
6264 | bs->refcnt++; | |
6265 | } | |
6266 | ||
6267 | /* Release a previously grabbed reference to bs. | |
6268 | * If after releasing, reference count is zero, the BlockDriverState is | |
6269 | * deleted. */ | |
6270 | void bdrv_unref(BlockDriverState *bs) | |
6271 | { | |
9a4d5ca6 JC |
6272 | if (!bs) { |
6273 | return; | |
6274 | } | |
9fcb0251 FZ |
6275 | assert(bs->refcnt > 0); |
6276 | if (--bs->refcnt == 0) { | |
6277 | bdrv_delete(bs); | |
6278 | } | |
6279 | } | |
6280 | ||
fbe40ff7 FZ |
6281 | struct BdrvOpBlocker { |
6282 | Error *reason; | |
6283 | QLIST_ENTRY(BdrvOpBlocker) list; | |
6284 | }; | |
6285 | ||
6286 | bool bdrv_op_is_blocked(BlockDriverState *bs, BlockOpType op, Error **errp) | |
6287 | { | |
6288 | BdrvOpBlocker *blocker; | |
6289 | assert((int) op >= 0 && op < BLOCK_OP_TYPE_MAX); | |
6290 | if (!QLIST_EMPTY(&bs->op_blockers[op])) { | |
6291 | blocker = QLIST_FIRST(&bs->op_blockers[op]); | |
4b576648 MA |
6292 | error_propagate_prepend(errp, error_copy(blocker->reason), |
6293 | "Node '%s' is busy: ", | |
6294 | bdrv_get_device_or_node_name(bs)); | |
fbe40ff7 FZ |
6295 | return true; |
6296 | } | |
6297 | return false; | |
6298 | } | |
6299 | ||
6300 | void bdrv_op_block(BlockDriverState *bs, BlockOpType op, Error *reason) | |
6301 | { | |
6302 | BdrvOpBlocker *blocker; | |
6303 | assert((int) op >= 0 && op < BLOCK_OP_TYPE_MAX); | |
6304 | ||
5839e53b | 6305 | blocker = g_new0(BdrvOpBlocker, 1); |
fbe40ff7 FZ |
6306 | blocker->reason = reason; |
6307 | QLIST_INSERT_HEAD(&bs->op_blockers[op], blocker, list); | |
6308 | } | |
6309 | ||
6310 | void bdrv_op_unblock(BlockDriverState *bs, BlockOpType op, Error *reason) | |
6311 | { | |
6312 | BdrvOpBlocker *blocker, *next; | |
6313 | assert((int) op >= 0 && op < BLOCK_OP_TYPE_MAX); | |
6314 | QLIST_FOREACH_SAFE(blocker, &bs->op_blockers[op], list, next) { | |
6315 | if (blocker->reason == reason) { | |
6316 | QLIST_REMOVE(blocker, list); | |
6317 | g_free(blocker); | |
6318 | } | |
6319 | } | |
6320 | } | |
6321 | ||
6322 | void bdrv_op_block_all(BlockDriverState *bs, Error *reason) | |
6323 | { | |
6324 | int i; | |
6325 | for (i = 0; i < BLOCK_OP_TYPE_MAX; i++) { | |
6326 | bdrv_op_block(bs, i, reason); | |
6327 | } | |
6328 | } | |
6329 | ||
6330 | void bdrv_op_unblock_all(BlockDriverState *bs, Error *reason) | |
6331 | { | |
6332 | int i; | |
6333 | for (i = 0; i < BLOCK_OP_TYPE_MAX; i++) { | |
6334 | bdrv_op_unblock(bs, i, reason); | |
6335 | } | |
6336 | } | |
6337 | ||
6338 | bool bdrv_op_blocker_is_empty(BlockDriverState *bs) | |
6339 | { | |
6340 | int i; | |
6341 | ||
6342 | for (i = 0; i < BLOCK_OP_TYPE_MAX; i++) { | |
6343 | if (!QLIST_EMPTY(&bs->op_blockers[i])) { | |
6344 | return false; | |
6345 | } | |
6346 | } | |
6347 | return true; | |
6348 | } | |
6349 | ||
d92ada22 LC |
6350 | void bdrv_img_create(const char *filename, const char *fmt, |
6351 | const char *base_filename, const char *base_fmt, | |
9217283d FZ |
6352 | char *options, uint64_t img_size, int flags, bool quiet, |
6353 | Error **errp) | |
f88e1a42 | 6354 | { |
83d0521a CL |
6355 | QemuOptsList *create_opts = NULL; |
6356 | QemuOpts *opts = NULL; | |
6357 | const char *backing_fmt, *backing_file; | |
6358 | int64_t size; | |
f88e1a42 | 6359 | BlockDriver *drv, *proto_drv; |
cc84d90f | 6360 | Error *local_err = NULL; |
f88e1a42 JS |
6361 | int ret = 0; |
6362 | ||
6363 | /* Find driver and parse its options */ | |
6364 | drv = bdrv_find_format(fmt); | |
6365 | if (!drv) { | |
71c79813 | 6366 | error_setg(errp, "Unknown file format '%s'", fmt); |
d92ada22 | 6367 | return; |
f88e1a42 JS |
6368 | } |
6369 | ||
b65a5e12 | 6370 | proto_drv = bdrv_find_protocol(filename, true, errp); |
f88e1a42 | 6371 | if (!proto_drv) { |
d92ada22 | 6372 | return; |
f88e1a42 JS |
6373 | } |
6374 | ||
c6149724 HR |
6375 | if (!drv->create_opts) { |
6376 | error_setg(errp, "Format driver '%s' does not support image creation", | |
6377 | drv->format_name); | |
6378 | return; | |
6379 | } | |
6380 | ||
5a5e7f8c ML |
6381 | if (!proto_drv->create_opts) { |
6382 | error_setg(errp, "Protocol driver '%s' does not support image creation", | |
6383 | proto_drv->format_name); | |
6384 | return; | |
6385 | } | |
6386 | ||
f6dc1c31 | 6387 | /* Create parameter list */ |
c282e1fd | 6388 | create_opts = qemu_opts_append(create_opts, drv->create_opts); |
5a5e7f8c | 6389 | create_opts = qemu_opts_append(create_opts, proto_drv->create_opts); |
f88e1a42 | 6390 | |
83d0521a | 6391 | opts = qemu_opts_create(create_opts, NULL, 0, &error_abort); |
f88e1a42 JS |
6392 | |
6393 | /* Parse -o options */ | |
6394 | if (options) { | |
a5f9b9df | 6395 | if (!qemu_opts_do_parse(opts, options, NULL, errp)) { |
f88e1a42 JS |
6396 | goto out; |
6397 | } | |
6398 | } | |
6399 | ||
f6dc1c31 KW |
6400 | if (!qemu_opt_get(opts, BLOCK_OPT_SIZE)) { |
6401 | qemu_opt_set_number(opts, BLOCK_OPT_SIZE, img_size, &error_abort); | |
6402 | } else if (img_size != UINT64_C(-1)) { | |
6403 | error_setg(errp, "The image size must be specified only once"); | |
6404 | goto out; | |
6405 | } | |
6406 | ||
f88e1a42 | 6407 | if (base_filename) { |
235e59cf | 6408 | if (!qemu_opt_set(opts, BLOCK_OPT_BACKING_FILE, base_filename, |
3882578b | 6409 | NULL)) { |
71c79813 LC |
6410 | error_setg(errp, "Backing file not supported for file format '%s'", |
6411 | fmt); | |
f88e1a42 JS |
6412 | goto out; |
6413 | } | |
6414 | } | |
6415 | ||
6416 | if (base_fmt) { | |
3882578b | 6417 | if (!qemu_opt_set(opts, BLOCK_OPT_BACKING_FMT, base_fmt, NULL)) { |
71c79813 LC |
6418 | error_setg(errp, "Backing file format not supported for file " |
6419 | "format '%s'", fmt); | |
f88e1a42 JS |
6420 | goto out; |
6421 | } | |
6422 | } | |
6423 | ||
83d0521a CL |
6424 | backing_file = qemu_opt_get(opts, BLOCK_OPT_BACKING_FILE); |
6425 | if (backing_file) { | |
6426 | if (!strcmp(filename, backing_file)) { | |
71c79813 LC |
6427 | error_setg(errp, "Error: Trying to create an image with the " |
6428 | "same filename as the backing file"); | |
792da93a JS |
6429 | goto out; |
6430 | } | |
975a7bd2 CK |
6431 | if (backing_file[0] == '\0') { |
6432 | error_setg(errp, "Expected backing file name, got empty string"); | |
6433 | goto out; | |
6434 | } | |
792da93a JS |
6435 | } |
6436 | ||
83d0521a | 6437 | backing_fmt = qemu_opt_get(opts, BLOCK_OPT_BACKING_FMT); |
f88e1a42 | 6438 | |
6e6e55f5 JS |
6439 | /* The size for the image must always be specified, unless we have a backing |
6440 | * file and we have not been forbidden from opening it. */ | |
a8b42a1c | 6441 | size = qemu_opt_get_size(opts, BLOCK_OPT_SIZE, img_size); |
6e6e55f5 JS |
6442 | if (backing_file && !(flags & BDRV_O_NO_BACKING)) { |
6443 | BlockDriverState *bs; | |
645ae7d8 | 6444 | char *full_backing; |
6e6e55f5 JS |
6445 | int back_flags; |
6446 | QDict *backing_options = NULL; | |
6447 | ||
645ae7d8 HR |
6448 | full_backing = |
6449 | bdrv_get_full_backing_filename_from_filename(filename, backing_file, | |
6450 | &local_err); | |
6e6e55f5 | 6451 | if (local_err) { |
6e6e55f5 JS |
6452 | goto out; |
6453 | } | |
645ae7d8 | 6454 | assert(full_backing); |
29168018 | 6455 | |
6e6e55f5 JS |
6456 | /* backing files always opened read-only */ |
6457 | back_flags = flags; | |
6458 | back_flags &= ~(BDRV_O_RDWR | BDRV_O_SNAPSHOT | BDRV_O_NO_BACKING); | |
f88e1a42 | 6459 | |
cc954f01 | 6460 | backing_options = qdict_new(); |
6e6e55f5 | 6461 | if (backing_fmt) { |
6e6e55f5 JS |
6462 | qdict_put_str(backing_options, "driver", backing_fmt); |
6463 | } | |
cc954f01 | 6464 | qdict_put_bool(backing_options, BDRV_OPT_FORCE_SHARE, true); |
e6641719 | 6465 | |
6e6e55f5 JS |
6466 | bs = bdrv_open(full_backing, NULL, backing_options, back_flags, |
6467 | &local_err); | |
6468 | g_free(full_backing); | |
add8200d EB |
6469 | if (!bs) { |
6470 | error_append_hint(&local_err, "Could not open backing image.\n"); | |
6e6e55f5 JS |
6471 | goto out; |
6472 | } else { | |
d9f059aa EB |
6473 | if (!backing_fmt) { |
6474 | warn_report("Deprecated use of backing file without explicit " | |
6475 | "backing format (detected format of %s)", | |
6476 | bs->drv->format_name); | |
6477 | if (bs->drv != &bdrv_raw) { | |
6478 | /* | |
6479 | * A probe of raw deserves the most attention: | |
6480 | * leaving the backing format out of the image | |
6481 | * will ensure bs->probed is set (ensuring we | |
6482 | * don't accidentally commit into the backing | |
6483 | * file), and allow more spots to warn the users | |
6484 | * to fix their toolchain when opening this image | |
6485 | * later. For other images, we can safely record | |
6486 | * the format that we probed. | |
6487 | */ | |
6488 | backing_fmt = bs->drv->format_name; | |
6489 | qemu_opt_set(opts, BLOCK_OPT_BACKING_FMT, backing_fmt, | |
6490 | NULL); | |
6491 | } | |
6492 | } | |
6e6e55f5 JS |
6493 | if (size == -1) { |
6494 | /* Opened BS, have no size */ | |
6495 | size = bdrv_getlength(bs); | |
6496 | if (size < 0) { | |
6497 | error_setg_errno(errp, -size, "Could not get size of '%s'", | |
6498 | backing_file); | |
6499 | bdrv_unref(bs); | |
6500 | goto out; | |
6501 | } | |
6502 | qemu_opt_set_number(opts, BLOCK_OPT_SIZE, size, &error_abort); | |
52bf1e72 | 6503 | } |
66f6b814 | 6504 | bdrv_unref(bs); |
f88e1a42 | 6505 | } |
d9f059aa EB |
6506 | /* (backing_file && !(flags & BDRV_O_NO_BACKING)) */ |
6507 | } else if (backing_file && !backing_fmt) { | |
6508 | warn_report("Deprecated use of unopened backing file without " | |
6509 | "explicit backing format, use of this image requires " | |
6510 | "potentially unsafe format probing"); | |
6511 | } | |
6e6e55f5 JS |
6512 | |
6513 | if (size == -1) { | |
6514 | error_setg(errp, "Image creation needs a size parameter"); | |
6515 | goto out; | |
f88e1a42 JS |
6516 | } |
6517 | ||
f382d43a | 6518 | if (!quiet) { |
fe646693 | 6519 | printf("Formatting '%s', fmt=%s ", filename, fmt); |
43c5d8f8 | 6520 | qemu_opts_print(opts, " "); |
f382d43a | 6521 | puts(""); |
4e2f4418 | 6522 | fflush(stdout); |
f382d43a | 6523 | } |
83d0521a | 6524 | |
c282e1fd | 6525 | ret = bdrv_create(drv, filename, opts, &local_err); |
83d0521a | 6526 | |
cc84d90f HR |
6527 | if (ret == -EFBIG) { |
6528 | /* This is generally a better message than whatever the driver would | |
6529 | * deliver (especially because of the cluster_size_hint), since that | |
6530 | * is most probably not much different from "image too large". */ | |
6531 | const char *cluster_size_hint = ""; | |
83d0521a | 6532 | if (qemu_opt_get_size(opts, BLOCK_OPT_CLUSTER_SIZE, 0)) { |
cc84d90f | 6533 | cluster_size_hint = " (try using a larger cluster size)"; |
f88e1a42 | 6534 | } |
cc84d90f HR |
6535 | error_setg(errp, "The image size is too large for file format '%s'" |
6536 | "%s", fmt, cluster_size_hint); | |
6537 | error_free(local_err); | |
6538 | local_err = NULL; | |
f88e1a42 JS |
6539 | } |
6540 | ||
6541 | out: | |
83d0521a CL |
6542 | qemu_opts_del(opts); |
6543 | qemu_opts_free(create_opts); | |
621ff94d | 6544 | error_propagate(errp, local_err); |
f88e1a42 | 6545 | } |
85d126f3 SH |
6546 | |
6547 | AioContext *bdrv_get_aio_context(BlockDriverState *bs) | |
6548 | { | |
33f2a757 | 6549 | return bs ? bs->aio_context : qemu_get_aio_context(); |
dcd04228 SH |
6550 | } |
6551 | ||
e336fd4c KW |
6552 | AioContext *coroutine_fn bdrv_co_enter(BlockDriverState *bs) |
6553 | { | |
6554 | Coroutine *self = qemu_coroutine_self(); | |
6555 | AioContext *old_ctx = qemu_coroutine_get_aio_context(self); | |
6556 | AioContext *new_ctx; | |
6557 | ||
6558 | /* | |
6559 | * Increase bs->in_flight to ensure that this operation is completed before | |
6560 | * moving the node to a different AioContext. Read new_ctx only afterwards. | |
6561 | */ | |
6562 | bdrv_inc_in_flight(bs); | |
6563 | ||
6564 | new_ctx = bdrv_get_aio_context(bs); | |
6565 | aio_co_reschedule_self(new_ctx); | |
6566 | return old_ctx; | |
6567 | } | |
6568 | ||
6569 | void coroutine_fn bdrv_co_leave(BlockDriverState *bs, AioContext *old_ctx) | |
6570 | { | |
6571 | aio_co_reschedule_self(old_ctx); | |
6572 | bdrv_dec_in_flight(bs); | |
6573 | } | |
6574 | ||
18c6ac1c KW |
6575 | void coroutine_fn bdrv_co_lock(BlockDriverState *bs) |
6576 | { | |
6577 | AioContext *ctx = bdrv_get_aio_context(bs); | |
6578 | ||
6579 | /* In the main thread, bs->aio_context won't change concurrently */ | |
6580 | assert(qemu_get_current_aio_context() == qemu_get_aio_context()); | |
6581 | ||
6582 | /* | |
6583 | * We're in coroutine context, so we already hold the lock of the main | |
6584 | * loop AioContext. Don't lock it twice to avoid deadlocks. | |
6585 | */ | |
6586 | assert(qemu_in_coroutine()); | |
6587 | if (ctx != qemu_get_aio_context()) { | |
6588 | aio_context_acquire(ctx); | |
6589 | } | |
6590 | } | |
6591 | ||
6592 | void coroutine_fn bdrv_co_unlock(BlockDriverState *bs) | |
6593 | { | |
6594 | AioContext *ctx = bdrv_get_aio_context(bs); | |
6595 | ||
6596 | assert(qemu_in_coroutine()); | |
6597 | if (ctx != qemu_get_aio_context()) { | |
6598 | aio_context_release(ctx); | |
6599 | } | |
6600 | } | |
6601 | ||
052a7572 FZ |
6602 | void bdrv_coroutine_enter(BlockDriverState *bs, Coroutine *co) |
6603 | { | |
6604 | aio_co_enter(bdrv_get_aio_context(bs), co); | |
6605 | } | |
6606 | ||
e8a095da SH |
6607 | static void bdrv_do_remove_aio_context_notifier(BdrvAioNotifier *ban) |
6608 | { | |
6609 | QLIST_REMOVE(ban, list); | |
6610 | g_free(ban); | |
6611 | } | |
6612 | ||
a3a683c3 | 6613 | static void bdrv_detach_aio_context(BlockDriverState *bs) |
dcd04228 | 6614 | { |
e8a095da | 6615 | BdrvAioNotifier *baf, *baf_tmp; |
33384421 | 6616 | |
e8a095da SH |
6617 | assert(!bs->walking_aio_notifiers); |
6618 | bs->walking_aio_notifiers = true; | |
6619 | QLIST_FOREACH_SAFE(baf, &bs->aio_notifiers, list, baf_tmp) { | |
6620 | if (baf->deleted) { | |
6621 | bdrv_do_remove_aio_context_notifier(baf); | |
6622 | } else { | |
6623 | baf->detach_aio_context(baf->opaque); | |
6624 | } | |
33384421 | 6625 | } |
e8a095da SH |
6626 | /* Never mind iterating again to check for ->deleted. bdrv_close() will |
6627 | * remove remaining aio notifiers if we aren't called again. | |
6628 | */ | |
6629 | bs->walking_aio_notifiers = false; | |
33384421 | 6630 | |
1bffe1ae | 6631 | if (bs->drv && bs->drv->bdrv_detach_aio_context) { |
dcd04228 SH |
6632 | bs->drv->bdrv_detach_aio_context(bs); |
6633 | } | |
dcd04228 | 6634 | |
e64f25f3 KW |
6635 | if (bs->quiesce_counter) { |
6636 | aio_enable_external(bs->aio_context); | |
6637 | } | |
dcd04228 SH |
6638 | bs->aio_context = NULL; |
6639 | } | |
6640 | ||
a3a683c3 KW |
6641 | static void bdrv_attach_aio_context(BlockDriverState *bs, |
6642 | AioContext *new_context) | |
dcd04228 | 6643 | { |
e8a095da | 6644 | BdrvAioNotifier *ban, *ban_tmp; |
33384421 | 6645 | |
e64f25f3 KW |
6646 | if (bs->quiesce_counter) { |
6647 | aio_disable_external(new_context); | |
6648 | } | |
6649 | ||
dcd04228 SH |
6650 | bs->aio_context = new_context; |
6651 | ||
1bffe1ae | 6652 | if (bs->drv && bs->drv->bdrv_attach_aio_context) { |
dcd04228 SH |
6653 | bs->drv->bdrv_attach_aio_context(bs, new_context); |
6654 | } | |
33384421 | 6655 | |
e8a095da SH |
6656 | assert(!bs->walking_aio_notifiers); |
6657 | bs->walking_aio_notifiers = true; | |
6658 | QLIST_FOREACH_SAFE(ban, &bs->aio_notifiers, list, ban_tmp) { | |
6659 | if (ban->deleted) { | |
6660 | bdrv_do_remove_aio_context_notifier(ban); | |
6661 | } else { | |
6662 | ban->attached_aio_context(new_context, ban->opaque); | |
6663 | } | |
33384421 | 6664 | } |
e8a095da | 6665 | bs->walking_aio_notifiers = false; |
dcd04228 SH |
6666 | } |
6667 | ||
42a65f02 KW |
6668 | /* |
6669 | * Changes the AioContext used for fd handlers, timers, and BHs by this | |
6670 | * BlockDriverState and all its children and parents. | |
6671 | * | |
43eaaaef HR |
6672 | * Must be called from the main AioContext. |
6673 | * | |
42a65f02 KW |
6674 | * The caller must own the AioContext lock for the old AioContext of bs, but it |
6675 | * must not own the AioContext lock for new_context (unless new_context is the | |
6676 | * same as the current context of bs). | |
6677 | * | |
6678 | * @ignore will accumulate all visited BdrvChild object. The caller is | |
6679 | * responsible for freeing the list afterwards. | |
6680 | */ | |
53a7d041 KW |
6681 | void bdrv_set_aio_context_ignore(BlockDriverState *bs, |
6682 | AioContext *new_context, GSList **ignore) | |
dcd04228 | 6683 | { |
e037c09c | 6684 | AioContext *old_context = bdrv_get_aio_context(bs); |
722d8e73 SL |
6685 | GSList *children_to_process = NULL; |
6686 | GSList *parents_to_process = NULL; | |
6687 | GSList *entry; | |
6688 | BdrvChild *child, *parent; | |
0d83708a | 6689 | |
43eaaaef HR |
6690 | g_assert(qemu_get_current_aio_context() == qemu_get_aio_context()); |
6691 | ||
e037c09c | 6692 | if (old_context == new_context) { |
57830a49 DP |
6693 | return; |
6694 | } | |
6695 | ||
d70d5954 | 6696 | bdrv_drained_begin(bs); |
0d83708a KW |
6697 | |
6698 | QLIST_FOREACH(child, &bs->children, next) { | |
53a7d041 KW |
6699 | if (g_slist_find(*ignore, child)) { |
6700 | continue; | |
6701 | } | |
6702 | *ignore = g_slist_prepend(*ignore, child); | |
722d8e73 | 6703 | children_to_process = g_slist_prepend(children_to_process, child); |
53a7d041 | 6704 | } |
722d8e73 SL |
6705 | |
6706 | QLIST_FOREACH(parent, &bs->parents, next_parent) { | |
6707 | if (g_slist_find(*ignore, parent)) { | |
53a7d041 KW |
6708 | continue; |
6709 | } | |
722d8e73 SL |
6710 | *ignore = g_slist_prepend(*ignore, parent); |
6711 | parents_to_process = g_slist_prepend(parents_to_process, parent); | |
6712 | } | |
6713 | ||
6714 | for (entry = children_to_process; | |
6715 | entry != NULL; | |
6716 | entry = g_slist_next(entry)) { | |
6717 | child = entry->data; | |
6718 | bdrv_set_aio_context_ignore(child->bs, new_context, ignore); | |
6719 | } | |
6720 | g_slist_free(children_to_process); | |
6721 | ||
6722 | for (entry = parents_to_process; | |
6723 | entry != NULL; | |
6724 | entry = g_slist_next(entry)) { | |
6725 | parent = entry->data; | |
6726 | assert(parent->klass->set_aio_ctx); | |
6727 | parent->klass->set_aio_ctx(parent, new_context, ignore); | |
0d83708a | 6728 | } |
722d8e73 | 6729 | g_slist_free(parents_to_process); |
0d83708a | 6730 | |
dcd04228 SH |
6731 | bdrv_detach_aio_context(bs); |
6732 | ||
e037c09c | 6733 | /* Acquire the new context, if necessary */ |
43eaaaef | 6734 | if (qemu_get_aio_context() != new_context) { |
e037c09c HR |
6735 | aio_context_acquire(new_context); |
6736 | } | |
6737 | ||
dcd04228 | 6738 | bdrv_attach_aio_context(bs, new_context); |
e037c09c HR |
6739 | |
6740 | /* | |
6741 | * If this function was recursively called from | |
6742 | * bdrv_set_aio_context_ignore(), there may be nodes in the | |
6743 | * subtree that have not yet been moved to the new AioContext. | |
6744 | * Release the old one so bdrv_drained_end() can poll them. | |
6745 | */ | |
43eaaaef | 6746 | if (qemu_get_aio_context() != old_context) { |
e037c09c HR |
6747 | aio_context_release(old_context); |
6748 | } | |
6749 | ||
d70d5954 | 6750 | bdrv_drained_end(bs); |
e037c09c | 6751 | |
43eaaaef | 6752 | if (qemu_get_aio_context() != old_context) { |
e037c09c HR |
6753 | aio_context_acquire(old_context); |
6754 | } | |
43eaaaef | 6755 | if (qemu_get_aio_context() != new_context) { |
e037c09c HR |
6756 | aio_context_release(new_context); |
6757 | } | |
85d126f3 | 6758 | } |
d616b224 | 6759 | |
5d231849 KW |
6760 | static bool bdrv_parent_can_set_aio_context(BdrvChild *c, AioContext *ctx, |
6761 | GSList **ignore, Error **errp) | |
6762 | { | |
6763 | if (g_slist_find(*ignore, c)) { | |
6764 | return true; | |
6765 | } | |
6766 | *ignore = g_slist_prepend(*ignore, c); | |
6767 | ||
bd86fb99 HR |
6768 | /* |
6769 | * A BdrvChildClass that doesn't handle AioContext changes cannot | |
6770 | * tolerate any AioContext changes | |
6771 | */ | |
6772 | if (!c->klass->can_set_aio_ctx) { | |
5d231849 KW |
6773 | char *user = bdrv_child_user_desc(c); |
6774 | error_setg(errp, "Changing iothreads is not supported by %s", user); | |
6775 | g_free(user); | |
6776 | return false; | |
6777 | } | |
bd86fb99 | 6778 | if (!c->klass->can_set_aio_ctx(c, ctx, ignore, errp)) { |
5d231849 KW |
6779 | assert(!errp || *errp); |
6780 | return false; | |
6781 | } | |
6782 | return true; | |
6783 | } | |
6784 | ||
6785 | bool bdrv_child_can_set_aio_context(BdrvChild *c, AioContext *ctx, | |
6786 | GSList **ignore, Error **errp) | |
6787 | { | |
6788 | if (g_slist_find(*ignore, c)) { | |
6789 | return true; | |
6790 | } | |
6791 | *ignore = g_slist_prepend(*ignore, c); | |
6792 | return bdrv_can_set_aio_context(c->bs, ctx, ignore, errp); | |
6793 | } | |
6794 | ||
6795 | /* @ignore will accumulate all visited BdrvChild object. The caller is | |
6796 | * responsible for freeing the list afterwards. */ | |
6797 | bool bdrv_can_set_aio_context(BlockDriverState *bs, AioContext *ctx, | |
6798 | GSList **ignore, Error **errp) | |
6799 | { | |
6800 | BdrvChild *c; | |
6801 | ||
6802 | if (bdrv_get_aio_context(bs) == ctx) { | |
6803 | return true; | |
6804 | } | |
6805 | ||
6806 | QLIST_FOREACH(c, &bs->parents, next_parent) { | |
6807 | if (!bdrv_parent_can_set_aio_context(c, ctx, ignore, errp)) { | |
6808 | return false; | |
6809 | } | |
6810 | } | |
6811 | QLIST_FOREACH(c, &bs->children, next) { | |
6812 | if (!bdrv_child_can_set_aio_context(c, ctx, ignore, errp)) { | |
6813 | return false; | |
6814 | } | |
6815 | } | |
6816 | ||
6817 | return true; | |
6818 | } | |
6819 | ||
6820 | int bdrv_child_try_set_aio_context(BlockDriverState *bs, AioContext *ctx, | |
6821 | BdrvChild *ignore_child, Error **errp) | |
6822 | { | |
6823 | GSList *ignore; | |
6824 | bool ret; | |
6825 | ||
6826 | ignore = ignore_child ? g_slist_prepend(NULL, ignore_child) : NULL; | |
6827 | ret = bdrv_can_set_aio_context(bs, ctx, &ignore, errp); | |
6828 | g_slist_free(ignore); | |
6829 | ||
6830 | if (!ret) { | |
6831 | return -EPERM; | |
6832 | } | |
6833 | ||
53a7d041 KW |
6834 | ignore = ignore_child ? g_slist_prepend(NULL, ignore_child) : NULL; |
6835 | bdrv_set_aio_context_ignore(bs, ctx, &ignore); | |
6836 | g_slist_free(ignore); | |
6837 | ||
5d231849 KW |
6838 | return 0; |
6839 | } | |
6840 | ||
6841 | int bdrv_try_set_aio_context(BlockDriverState *bs, AioContext *ctx, | |
6842 | Error **errp) | |
6843 | { | |
6844 | return bdrv_child_try_set_aio_context(bs, ctx, NULL, errp); | |
6845 | } | |
6846 | ||
33384421 HR |
6847 | void bdrv_add_aio_context_notifier(BlockDriverState *bs, |
6848 | void (*attached_aio_context)(AioContext *new_context, void *opaque), | |
6849 | void (*detach_aio_context)(void *opaque), void *opaque) | |
6850 | { | |
6851 | BdrvAioNotifier *ban = g_new(BdrvAioNotifier, 1); | |
6852 | *ban = (BdrvAioNotifier){ | |
6853 | .attached_aio_context = attached_aio_context, | |
6854 | .detach_aio_context = detach_aio_context, | |
6855 | .opaque = opaque | |
6856 | }; | |
6857 | ||
6858 | QLIST_INSERT_HEAD(&bs->aio_notifiers, ban, list); | |
6859 | } | |
6860 | ||
6861 | void bdrv_remove_aio_context_notifier(BlockDriverState *bs, | |
6862 | void (*attached_aio_context)(AioContext *, | |
6863 | void *), | |
6864 | void (*detach_aio_context)(void *), | |
6865 | void *opaque) | |
6866 | { | |
6867 | BdrvAioNotifier *ban, *ban_next; | |
6868 | ||
6869 | QLIST_FOREACH_SAFE(ban, &bs->aio_notifiers, list, ban_next) { | |
6870 | if (ban->attached_aio_context == attached_aio_context && | |
6871 | ban->detach_aio_context == detach_aio_context && | |
e8a095da SH |
6872 | ban->opaque == opaque && |
6873 | ban->deleted == false) | |
33384421 | 6874 | { |
e8a095da SH |
6875 | if (bs->walking_aio_notifiers) { |
6876 | ban->deleted = true; | |
6877 | } else { | |
6878 | bdrv_do_remove_aio_context_notifier(ban); | |
6879 | } | |
33384421 HR |
6880 | return; |
6881 | } | |
6882 | } | |
6883 | ||
6884 | abort(); | |
6885 | } | |
6886 | ||
77485434 | 6887 | int bdrv_amend_options(BlockDriverState *bs, QemuOpts *opts, |
d1402b50 | 6888 | BlockDriverAmendStatusCB *status_cb, void *cb_opaque, |
a3579bfa | 6889 | bool force, |
d1402b50 | 6890 | Error **errp) |
6f176b48 | 6891 | { |
d470ad42 | 6892 | if (!bs->drv) { |
d1402b50 | 6893 | error_setg(errp, "Node is ejected"); |
d470ad42 HR |
6894 | return -ENOMEDIUM; |
6895 | } | |
c282e1fd | 6896 | if (!bs->drv->bdrv_amend_options) { |
d1402b50 HR |
6897 | error_setg(errp, "Block driver '%s' does not support option amendment", |
6898 | bs->drv->format_name); | |
6f176b48 HR |
6899 | return -ENOTSUP; |
6900 | } | |
a3579bfa ML |
6901 | return bs->drv->bdrv_amend_options(bs, opts, status_cb, |
6902 | cb_opaque, force, errp); | |
6f176b48 | 6903 | } |
f6186f49 | 6904 | |
5d69b5ab HR |
6905 | /* |
6906 | * This function checks whether the given @to_replace is allowed to be | |
6907 | * replaced by a node that always shows the same data as @bs. This is | |
6908 | * used for example to verify whether the mirror job can replace | |
6909 | * @to_replace by the target mirrored from @bs. | |
6910 | * To be replaceable, @bs and @to_replace may either be guaranteed to | |
6911 | * always show the same data (because they are only connected through | |
6912 | * filters), or some driver may allow replacing one of its children | |
6913 | * because it can guarantee that this child's data is not visible at | |
6914 | * all (for example, for dissenting quorum children that have no other | |
6915 | * parents). | |
6916 | */ | |
6917 | bool bdrv_recurse_can_replace(BlockDriverState *bs, | |
6918 | BlockDriverState *to_replace) | |
6919 | { | |
93393e69 HR |
6920 | BlockDriverState *filtered; |
6921 | ||
5d69b5ab HR |
6922 | if (!bs || !bs->drv) { |
6923 | return false; | |
6924 | } | |
6925 | ||
6926 | if (bs == to_replace) { | |
6927 | return true; | |
6928 | } | |
6929 | ||
6930 | /* See what the driver can do */ | |
6931 | if (bs->drv->bdrv_recurse_can_replace) { | |
6932 | return bs->drv->bdrv_recurse_can_replace(bs, to_replace); | |
6933 | } | |
6934 | ||
6935 | /* For filters without an own implementation, we can recurse on our own */ | |
93393e69 HR |
6936 | filtered = bdrv_filter_bs(bs); |
6937 | if (filtered) { | |
6938 | return bdrv_recurse_can_replace(filtered, to_replace); | |
5d69b5ab HR |
6939 | } |
6940 | ||
6941 | /* Safe default */ | |
6942 | return false; | |
6943 | } | |
6944 | ||
810803a8 HR |
6945 | /* |
6946 | * Check whether the given @node_name can be replaced by a node that | |
6947 | * has the same data as @parent_bs. If so, return @node_name's BDS; | |
6948 | * NULL otherwise. | |
6949 | * | |
6950 | * @node_name must be a (recursive) *child of @parent_bs (or this | |
6951 | * function will return NULL). | |
6952 | * | |
6953 | * The result (whether the node can be replaced or not) is only valid | |
6954 | * for as long as no graph or permission changes occur. | |
6955 | */ | |
e12f3784 WC |
6956 | BlockDriverState *check_to_replace_node(BlockDriverState *parent_bs, |
6957 | const char *node_name, Error **errp) | |
09158f00 BC |
6958 | { |
6959 | BlockDriverState *to_replace_bs = bdrv_find_node(node_name); | |
5a7e7a0b SH |
6960 | AioContext *aio_context; |
6961 | ||
09158f00 | 6962 | if (!to_replace_bs) { |
785ec4b1 | 6963 | error_setg(errp, "Failed to find node with node-name='%s'", node_name); |
09158f00 BC |
6964 | return NULL; |
6965 | } | |
6966 | ||
5a7e7a0b SH |
6967 | aio_context = bdrv_get_aio_context(to_replace_bs); |
6968 | aio_context_acquire(aio_context); | |
6969 | ||
09158f00 | 6970 | if (bdrv_op_is_blocked(to_replace_bs, BLOCK_OP_TYPE_REPLACE, errp)) { |
5a7e7a0b SH |
6971 | to_replace_bs = NULL; |
6972 | goto out; | |
09158f00 BC |
6973 | } |
6974 | ||
6975 | /* We don't want arbitrary node of the BDS chain to be replaced only the top | |
6976 | * most non filter in order to prevent data corruption. | |
6977 | * Another benefit is that this tests exclude backing files which are | |
6978 | * blocked by the backing blockers. | |
6979 | */ | |
810803a8 HR |
6980 | if (!bdrv_recurse_can_replace(parent_bs, to_replace_bs)) { |
6981 | error_setg(errp, "Cannot replace '%s' by a node mirrored from '%s', " | |
6982 | "because it cannot be guaranteed that doing so would not " | |
6983 | "lead to an abrupt change of visible data", | |
6984 | node_name, parent_bs->node_name); | |
5a7e7a0b SH |
6985 | to_replace_bs = NULL; |
6986 | goto out; | |
09158f00 BC |
6987 | } |
6988 | ||
5a7e7a0b SH |
6989 | out: |
6990 | aio_context_release(aio_context); | |
09158f00 BC |
6991 | return to_replace_bs; |
6992 | } | |
448ad91d | 6993 | |
97e2f021 HR |
6994 | /** |
6995 | * Iterates through the list of runtime option keys that are said to | |
6996 | * be "strong" for a BDS. An option is called "strong" if it changes | |
6997 | * a BDS's data. For example, the null block driver's "size" and | |
6998 | * "read-zeroes" options are strong, but its "latency-ns" option is | |
6999 | * not. | |
7000 | * | |
7001 | * If a key returned by this function ends with a dot, all options | |
7002 | * starting with that prefix are strong. | |
7003 | */ | |
7004 | static const char *const *strong_options(BlockDriverState *bs, | |
7005 | const char *const *curopt) | |
7006 | { | |
7007 | static const char *const global_options[] = { | |
7008 | "driver", "filename", NULL | |
7009 | }; | |
7010 | ||
7011 | if (!curopt) { | |
7012 | return &global_options[0]; | |
7013 | } | |
7014 | ||
7015 | curopt++; | |
7016 | if (curopt == &global_options[ARRAY_SIZE(global_options) - 1] && bs->drv) { | |
7017 | curopt = bs->drv->strong_runtime_opts; | |
7018 | } | |
7019 | ||
7020 | return (curopt && *curopt) ? curopt : NULL; | |
7021 | } | |
7022 | ||
7023 | /** | |
7024 | * Copies all strong runtime options from bs->options to the given | |
7025 | * QDict. The set of strong option keys is determined by invoking | |
7026 | * strong_options(). | |
7027 | * | |
7028 | * Returns true iff any strong option was present in bs->options (and | |
7029 | * thus copied to the target QDict) with the exception of "filename" | |
7030 | * and "driver". The caller is expected to use this value to decide | |
7031 | * whether the existence of strong options prevents the generation of | |
7032 | * a plain filename. | |
7033 | */ | |
7034 | static bool append_strong_runtime_options(QDict *d, BlockDriverState *bs) | |
7035 | { | |
7036 | bool found_any = false; | |
7037 | const char *const *option_name = NULL; | |
7038 | ||
7039 | if (!bs->drv) { | |
7040 | return false; | |
7041 | } | |
7042 | ||
7043 | while ((option_name = strong_options(bs, option_name))) { | |
7044 | bool option_given = false; | |
7045 | ||
7046 | assert(strlen(*option_name) > 0); | |
7047 | if ((*option_name)[strlen(*option_name) - 1] != '.') { | |
7048 | QObject *entry = qdict_get(bs->options, *option_name); | |
7049 | if (!entry) { | |
7050 | continue; | |
7051 | } | |
7052 | ||
7053 | qdict_put_obj(d, *option_name, qobject_ref(entry)); | |
7054 | option_given = true; | |
7055 | } else { | |
7056 | const QDictEntry *entry; | |
7057 | for (entry = qdict_first(bs->options); entry; | |
7058 | entry = qdict_next(bs->options, entry)) | |
7059 | { | |
7060 | if (strstart(qdict_entry_key(entry), *option_name, NULL)) { | |
7061 | qdict_put_obj(d, qdict_entry_key(entry), | |
7062 | qobject_ref(qdict_entry_value(entry))); | |
7063 | option_given = true; | |
7064 | } | |
7065 | } | |
7066 | } | |
7067 | ||
7068 | /* While "driver" and "filename" need to be included in a JSON filename, | |
7069 | * their existence does not prohibit generation of a plain filename. */ | |
7070 | if (!found_any && option_given && | |
7071 | strcmp(*option_name, "driver") && strcmp(*option_name, "filename")) | |
7072 | { | |
7073 | found_any = true; | |
7074 | } | |
7075 | } | |
7076 | ||
62a01a27 HR |
7077 | if (!qdict_haskey(d, "driver")) { |
7078 | /* Drivers created with bdrv_new_open_driver() may not have a | |
7079 | * @driver option. Add it here. */ | |
7080 | qdict_put_str(d, "driver", bs->drv->format_name); | |
7081 | } | |
7082 | ||
97e2f021 HR |
7083 | return found_any; |
7084 | } | |
7085 | ||
90993623 HR |
7086 | /* Note: This function may return false positives; it may return true |
7087 | * even if opening the backing file specified by bs's image header | |
7088 | * would result in exactly bs->backing. */ | |
0b877d09 | 7089 | bool bdrv_backing_overridden(BlockDriverState *bs) |
90993623 HR |
7090 | { |
7091 | if (bs->backing) { | |
7092 | return strcmp(bs->auto_backing_file, | |
7093 | bs->backing->bs->filename); | |
7094 | } else { | |
7095 | /* No backing BDS, so if the image header reports any backing | |
7096 | * file, it must have been suppressed */ | |
7097 | return bs->auto_backing_file[0] != '\0'; | |
7098 | } | |
7099 | } | |
7100 | ||
91af7014 HR |
7101 | /* Updates the following BDS fields: |
7102 | * - exact_filename: A filename which may be used for opening a block device | |
7103 | * which (mostly) equals the given BDS (even without any | |
7104 | * other options; so reading and writing must return the same | |
7105 | * results, but caching etc. may be different) | |
7106 | * - full_open_options: Options which, when given when opening a block device | |
7107 | * (without a filename), result in a BDS (mostly) | |
7108 | * equalling the given one | |
7109 | * - filename: If exact_filename is set, it is copied here. Otherwise, | |
7110 | * full_open_options is converted to a JSON object, prefixed with | |
7111 | * "json:" (for use through the JSON pseudo protocol) and put here. | |
7112 | */ | |
7113 | void bdrv_refresh_filename(BlockDriverState *bs) | |
7114 | { | |
7115 | BlockDriver *drv = bs->drv; | |
e24518e3 | 7116 | BdrvChild *child; |
52f72d6f | 7117 | BlockDriverState *primary_child_bs; |
91af7014 | 7118 | QDict *opts; |
90993623 | 7119 | bool backing_overridden; |
998b3a1e HR |
7120 | bool generate_json_filename; /* Whether our default implementation should |
7121 | fill exact_filename (false) or not (true) */ | |
91af7014 HR |
7122 | |
7123 | if (!drv) { | |
7124 | return; | |
7125 | } | |
7126 | ||
e24518e3 HR |
7127 | /* This BDS's file name may depend on any of its children's file names, so |
7128 | * refresh those first */ | |
7129 | QLIST_FOREACH(child, &bs->children, next) { | |
7130 | bdrv_refresh_filename(child->bs); | |
91af7014 HR |
7131 | } |
7132 | ||
bb808d5f HR |
7133 | if (bs->implicit) { |
7134 | /* For implicit nodes, just copy everything from the single child */ | |
7135 | child = QLIST_FIRST(&bs->children); | |
7136 | assert(QLIST_NEXT(child, next) == NULL); | |
7137 | ||
7138 | pstrcpy(bs->exact_filename, sizeof(bs->exact_filename), | |
7139 | child->bs->exact_filename); | |
7140 | pstrcpy(bs->filename, sizeof(bs->filename), child->bs->filename); | |
7141 | ||
cb895614 | 7142 | qobject_unref(bs->full_open_options); |
bb808d5f HR |
7143 | bs->full_open_options = qobject_ref(child->bs->full_open_options); |
7144 | ||
7145 | return; | |
7146 | } | |
7147 | ||
90993623 HR |
7148 | backing_overridden = bdrv_backing_overridden(bs); |
7149 | ||
7150 | if (bs->open_flags & BDRV_O_NO_IO) { | |
7151 | /* Without I/O, the backing file does not change anything. | |
7152 | * Therefore, in such a case (primarily qemu-img), we can | |
7153 | * pretend the backing file has not been overridden even if | |
7154 | * it technically has been. */ | |
7155 | backing_overridden = false; | |
7156 | } | |
7157 | ||
97e2f021 HR |
7158 | /* Gather the options QDict */ |
7159 | opts = qdict_new(); | |
998b3a1e HR |
7160 | generate_json_filename = append_strong_runtime_options(opts, bs); |
7161 | generate_json_filename |= backing_overridden; | |
97e2f021 HR |
7162 | |
7163 | if (drv->bdrv_gather_child_options) { | |
7164 | /* Some block drivers may not want to present all of their children's | |
7165 | * options, or name them differently from BdrvChild.name */ | |
7166 | drv->bdrv_gather_child_options(bs, opts, backing_overridden); | |
7167 | } else { | |
7168 | QLIST_FOREACH(child, &bs->children, next) { | |
25191e5f | 7169 | if (child == bs->backing && !backing_overridden) { |
97e2f021 HR |
7170 | /* We can skip the backing BDS if it has not been overridden */ |
7171 | continue; | |
7172 | } | |
7173 | ||
7174 | qdict_put(opts, child->name, | |
7175 | qobject_ref(child->bs->full_open_options)); | |
7176 | } | |
7177 | ||
7178 | if (backing_overridden && !bs->backing) { | |
7179 | /* Force no backing file */ | |
7180 | qdict_put_null(opts, "backing"); | |
7181 | } | |
7182 | } | |
7183 | ||
7184 | qobject_unref(bs->full_open_options); | |
7185 | bs->full_open_options = opts; | |
7186 | ||
52f72d6f HR |
7187 | primary_child_bs = bdrv_primary_bs(bs); |
7188 | ||
998b3a1e HR |
7189 | if (drv->bdrv_refresh_filename) { |
7190 | /* Obsolete information is of no use here, so drop the old file name | |
7191 | * information before refreshing it */ | |
7192 | bs->exact_filename[0] = '\0'; | |
7193 | ||
7194 | drv->bdrv_refresh_filename(bs); | |
52f72d6f HR |
7195 | } else if (primary_child_bs) { |
7196 | /* | |
7197 | * Try to reconstruct valid information from the underlying | |
7198 | * file -- this only works for format nodes (filter nodes | |
7199 | * cannot be probed and as such must be selected by the user | |
7200 | * either through an options dict, or through a special | |
7201 | * filename which the filter driver must construct in its | |
7202 | * .bdrv_refresh_filename() implementation). | |
7203 | */ | |
998b3a1e HR |
7204 | |
7205 | bs->exact_filename[0] = '\0'; | |
7206 | ||
fb695c74 HR |
7207 | /* |
7208 | * We can use the underlying file's filename if: | |
7209 | * - it has a filename, | |
52f72d6f | 7210 | * - the current BDS is not a filter, |
fb695c74 HR |
7211 | * - the file is a protocol BDS, and |
7212 | * - opening that file (as this BDS's format) will automatically create | |
7213 | * the BDS tree we have right now, that is: | |
7214 | * - the user did not significantly change this BDS's behavior with | |
7215 | * some explicit (strong) options | |
7216 | * - no non-file child of this BDS has been overridden by the user | |
7217 | * Both of these conditions are represented by generate_json_filename. | |
7218 | */ | |
52f72d6f HR |
7219 | if (primary_child_bs->exact_filename[0] && |
7220 | primary_child_bs->drv->bdrv_file_open && | |
7221 | !drv->is_filter && !generate_json_filename) | |
fb695c74 | 7222 | { |
52f72d6f | 7223 | strcpy(bs->exact_filename, primary_child_bs->exact_filename); |
998b3a1e HR |
7224 | } |
7225 | } | |
7226 | ||
91af7014 HR |
7227 | if (bs->exact_filename[0]) { |
7228 | pstrcpy(bs->filename, sizeof(bs->filename), bs->exact_filename); | |
97e2f021 | 7229 | } else { |
eab3a467 | 7230 | GString *json = qobject_to_json(QOBJECT(bs->full_open_options)); |
5c86bdf1 | 7231 | if (snprintf(bs->filename, sizeof(bs->filename), "json:%s", |
eab3a467 | 7232 | json->str) >= sizeof(bs->filename)) { |
5c86bdf1 EB |
7233 | /* Give user a hint if we truncated things. */ |
7234 | strcpy(bs->filename + sizeof(bs->filename) - 4, "..."); | |
7235 | } | |
eab3a467 | 7236 | g_string_free(json, true); |
91af7014 HR |
7237 | } |
7238 | } | |
e06018ad | 7239 | |
1e89d0f9 HR |
7240 | char *bdrv_dirname(BlockDriverState *bs, Error **errp) |
7241 | { | |
7242 | BlockDriver *drv = bs->drv; | |
52f72d6f | 7243 | BlockDriverState *child_bs; |
1e89d0f9 HR |
7244 | |
7245 | if (!drv) { | |
7246 | error_setg(errp, "Node '%s' is ejected", bs->node_name); | |
7247 | return NULL; | |
7248 | } | |
7249 | ||
7250 | if (drv->bdrv_dirname) { | |
7251 | return drv->bdrv_dirname(bs, errp); | |
7252 | } | |
7253 | ||
52f72d6f HR |
7254 | child_bs = bdrv_primary_bs(bs); |
7255 | if (child_bs) { | |
7256 | return bdrv_dirname(child_bs, errp); | |
1e89d0f9 HR |
7257 | } |
7258 | ||
7259 | bdrv_refresh_filename(bs); | |
7260 | if (bs->exact_filename[0] != '\0') { | |
7261 | return path_combine(bs->exact_filename, ""); | |
7262 | } | |
7263 | ||
7264 | error_setg(errp, "Cannot generate a base directory for %s nodes", | |
7265 | drv->format_name); | |
7266 | return NULL; | |
7267 | } | |
7268 | ||
e06018ad WC |
7269 | /* |
7270 | * Hot add/remove a BDS's child. So the user can take a child offline when | |
7271 | * it is broken and take a new child online | |
7272 | */ | |
7273 | void bdrv_add_child(BlockDriverState *parent_bs, BlockDriverState *child_bs, | |
7274 | Error **errp) | |
7275 | { | |
7276 | ||
7277 | if (!parent_bs->drv || !parent_bs->drv->bdrv_add_child) { | |
7278 | error_setg(errp, "The node %s does not support adding a child", | |
7279 | bdrv_get_device_or_node_name(parent_bs)); | |
7280 | return; | |
7281 | } | |
7282 | ||
7283 | if (!QLIST_EMPTY(&child_bs->parents)) { | |
7284 | error_setg(errp, "The node %s already has a parent", | |
7285 | child_bs->node_name); | |
7286 | return; | |
7287 | } | |
7288 | ||
7289 | parent_bs->drv->bdrv_add_child(parent_bs, child_bs, errp); | |
7290 | } | |
7291 | ||
7292 | void bdrv_del_child(BlockDriverState *parent_bs, BdrvChild *child, Error **errp) | |
7293 | { | |
7294 | BdrvChild *tmp; | |
7295 | ||
7296 | if (!parent_bs->drv || !parent_bs->drv->bdrv_del_child) { | |
7297 | error_setg(errp, "The node %s does not support removing a child", | |
7298 | bdrv_get_device_or_node_name(parent_bs)); | |
7299 | return; | |
7300 | } | |
7301 | ||
7302 | QLIST_FOREACH(tmp, &parent_bs->children, next) { | |
7303 | if (tmp == child) { | |
7304 | break; | |
7305 | } | |
7306 | } | |
7307 | ||
7308 | if (!tmp) { | |
7309 | error_setg(errp, "The node %s does not have a child named %s", | |
7310 | bdrv_get_device_or_node_name(parent_bs), | |
7311 | bdrv_get_device_or_node_name(child->bs)); | |
7312 | return; | |
7313 | } | |
7314 | ||
7315 | parent_bs->drv->bdrv_del_child(parent_bs, child, errp); | |
7316 | } | |
6f7a3b53 HR |
7317 | |
7318 | int bdrv_make_empty(BdrvChild *c, Error **errp) | |
7319 | { | |
7320 | BlockDriver *drv = c->bs->drv; | |
7321 | int ret; | |
7322 | ||
7323 | assert(c->perm & (BLK_PERM_WRITE | BLK_PERM_WRITE_UNCHANGED)); | |
7324 | ||
7325 | if (!drv->bdrv_make_empty) { | |
7326 | error_setg(errp, "%s does not support emptying nodes", | |
7327 | drv->format_name); | |
7328 | return -ENOTSUP; | |
7329 | } | |
7330 | ||
7331 | ret = drv->bdrv_make_empty(c->bs); | |
7332 | if (ret < 0) { | |
7333 | error_setg_errno(errp, -ret, "Failed to empty %s", | |
7334 | c->bs->filename); | |
7335 | return ret; | |
7336 | } | |
7337 | ||
7338 | return 0; | |
7339 | } | |
9a6fc887 HR |
7340 | |
7341 | /* | |
7342 | * Return the child that @bs acts as an overlay for, and from which data may be | |
7343 | * copied in COW or COR operations. Usually this is the backing file. | |
7344 | */ | |
7345 | BdrvChild *bdrv_cow_child(BlockDriverState *bs) | |
7346 | { | |
7347 | if (!bs || !bs->drv) { | |
7348 | return NULL; | |
7349 | } | |
7350 | ||
7351 | if (bs->drv->is_filter) { | |
7352 | return NULL; | |
7353 | } | |
7354 | ||
7355 | if (!bs->backing) { | |
7356 | return NULL; | |
7357 | } | |
7358 | ||
7359 | assert(bs->backing->role & BDRV_CHILD_COW); | |
7360 | return bs->backing; | |
7361 | } | |
7362 | ||
7363 | /* | |
7364 | * If @bs acts as a filter for exactly one of its children, return | |
7365 | * that child. | |
7366 | */ | |
7367 | BdrvChild *bdrv_filter_child(BlockDriverState *bs) | |
7368 | { | |
7369 | BdrvChild *c; | |
7370 | ||
7371 | if (!bs || !bs->drv) { | |
7372 | return NULL; | |
7373 | } | |
7374 | ||
7375 | if (!bs->drv->is_filter) { | |
7376 | return NULL; | |
7377 | } | |
7378 | ||
7379 | /* Only one of @backing or @file may be used */ | |
7380 | assert(!(bs->backing && bs->file)); | |
7381 | ||
7382 | c = bs->backing ?: bs->file; | |
7383 | if (!c) { | |
7384 | return NULL; | |
7385 | } | |
7386 | ||
7387 | assert(c->role & BDRV_CHILD_FILTERED); | |
7388 | return c; | |
7389 | } | |
7390 | ||
7391 | /* | |
7392 | * Return either the result of bdrv_cow_child() or bdrv_filter_child(), | |
7393 | * whichever is non-NULL. | |
7394 | * | |
7395 | * Return NULL if both are NULL. | |
7396 | */ | |
7397 | BdrvChild *bdrv_filter_or_cow_child(BlockDriverState *bs) | |
7398 | { | |
7399 | BdrvChild *cow_child = bdrv_cow_child(bs); | |
7400 | BdrvChild *filter_child = bdrv_filter_child(bs); | |
7401 | ||
7402 | /* Filter nodes cannot have COW backing files */ | |
7403 | assert(!(cow_child && filter_child)); | |
7404 | ||
7405 | return cow_child ?: filter_child; | |
7406 | } | |
7407 | ||
7408 | /* | |
7409 | * Return the primary child of this node: For filters, that is the | |
7410 | * filtered child. For other nodes, that is usually the child storing | |
7411 | * metadata. | |
7412 | * (A generally more helpful description is that this is (usually) the | |
7413 | * child that has the same filename as @bs.) | |
7414 | * | |
7415 | * Drivers do not necessarily have a primary child; for example quorum | |
7416 | * does not. | |
7417 | */ | |
7418 | BdrvChild *bdrv_primary_child(BlockDriverState *bs) | |
7419 | { | |
7420 | BdrvChild *c, *found = NULL; | |
7421 | ||
7422 | QLIST_FOREACH(c, &bs->children, next) { | |
7423 | if (c->role & BDRV_CHILD_PRIMARY) { | |
7424 | assert(!found); | |
7425 | found = c; | |
7426 | } | |
7427 | } | |
7428 | ||
7429 | return found; | |
7430 | } | |
d38d7eb8 HR |
7431 | |
7432 | static BlockDriverState *bdrv_do_skip_filters(BlockDriverState *bs, | |
7433 | bool stop_on_explicit_filter) | |
7434 | { | |
7435 | BdrvChild *c; | |
7436 | ||
7437 | if (!bs) { | |
7438 | return NULL; | |
7439 | } | |
7440 | ||
7441 | while (!(stop_on_explicit_filter && !bs->implicit)) { | |
7442 | c = bdrv_filter_child(bs); | |
7443 | if (!c) { | |
7444 | /* | |
7445 | * A filter that is embedded in a working block graph must | |
7446 | * have a child. Assert this here so this function does | |
7447 | * not return a filter node that is not expected by the | |
7448 | * caller. | |
7449 | */ | |
7450 | assert(!bs->drv || !bs->drv->is_filter); | |
7451 | break; | |
7452 | } | |
7453 | bs = c->bs; | |
7454 | } | |
7455 | /* | |
7456 | * Note that this treats nodes with bs->drv == NULL as not being | |
7457 | * filters (bs->drv == NULL should be replaced by something else | |
7458 | * anyway). | |
7459 | * The advantage of this behavior is that this function will thus | |
7460 | * always return a non-NULL value (given a non-NULL @bs). | |
7461 | */ | |
7462 | ||
7463 | return bs; | |
7464 | } | |
7465 | ||
7466 | /* | |
7467 | * Return the first BDS that has not been added implicitly or that | |
7468 | * does not have a filtered child down the chain starting from @bs | |
7469 | * (including @bs itself). | |
7470 | */ | |
7471 | BlockDriverState *bdrv_skip_implicit_filters(BlockDriverState *bs) | |
7472 | { | |
7473 | return bdrv_do_skip_filters(bs, true); | |
7474 | } | |
7475 | ||
7476 | /* | |
7477 | * Return the first BDS that does not have a filtered child down the | |
7478 | * chain starting from @bs (including @bs itself). | |
7479 | */ | |
7480 | BlockDriverState *bdrv_skip_filters(BlockDriverState *bs) | |
7481 | { | |
7482 | return bdrv_do_skip_filters(bs, false); | |
7483 | } | |
7484 | ||
7485 | /* | |
7486 | * For a backing chain, return the first non-filter backing image of | |
7487 | * the first non-filter image. | |
7488 | */ | |
7489 | BlockDriverState *bdrv_backing_chain_next(BlockDriverState *bs) | |
7490 | { | |
7491 | return bdrv_skip_filters(bdrv_cow_bs(bdrv_skip_filters(bs))); | |
7492 | } |