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