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