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