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