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