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