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