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