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