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