block: Drop bdrv_new_root()
[qemu/ar7.git] / block.c
blobed4b487778126b80dbc7996f2abfa9a1cb67a748
1 /*
2 * QEMU System Emulator block driver
4 * Copyright (c) 2003 Fabrice Bellard
6 * Permission is hereby granted, free of charge, to any person obtaining a copy
7 * of this software and associated documentation files (the "Software"), to deal
8 * in the Software without restriction, including without limitation the rights
9 * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
10 * copies of the Software, and to permit persons to whom the Software is
11 * furnished to do so, subject to the following conditions:
13 * The above copyright notice and this permission notice shall be included in
14 * all copies or substantial portions of the Software.
16 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
17 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
18 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
19 * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
20 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
21 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
22 * THE SOFTWARE.
24 #include "qemu/osdep.h"
25 #include "trace.h"
26 #include "block/block_int.h"
27 #include "block/blockjob.h"
28 #include "qemu/error-report.h"
29 #include "qemu/module.h"
30 #include "qapi/qmp/qerror.h"
31 #include "qapi/qmp/qbool.h"
32 #include "qapi/qmp/qjson.h"
33 #include "sysemu/block-backend.h"
34 #include "sysemu/sysemu.h"
35 #include "qemu/notify.h"
36 #include "qemu/coroutine.h"
37 #include "block/qapi.h"
38 #include "qmp-commands.h"
39 #include "qemu/timer.h"
40 #include "qapi-event.h"
41 #include "qemu/cutils.h"
42 #include "qemu/id.h"
44 #ifdef CONFIG_BSD
45 #include <sys/ioctl.h>
46 #include <sys/queue.h>
47 #ifndef __DragonFly__
48 #include <sys/disk.h>
49 #endif
50 #endif
52 #ifdef _WIN32
53 #include <windows.h>
54 #endif
56 #define NOT_DONE 0x7fffffff /* used while emulated sync operation in progress */
58 static QTAILQ_HEAD(, BlockDriverState) graph_bdrv_states =
59 QTAILQ_HEAD_INITIALIZER(graph_bdrv_states);
61 static QTAILQ_HEAD(, BlockDriverState) all_bdrv_states =
62 QTAILQ_HEAD_INITIALIZER(all_bdrv_states);
64 static QLIST_HEAD(, BlockDriver) bdrv_drivers =
65 QLIST_HEAD_INITIALIZER(bdrv_drivers);
67 static int bdrv_open_inherit(BlockDriverState **pbs, const char *filename,
68 const char *reference, QDict *options, int flags,
69 BlockDriverState *parent,
70 const BdrvChildRole *child_role, Error **errp);
72 /* If non-zero, use only whitelisted block drivers */
73 static int use_bdrv_whitelist;
75 static void bdrv_close(BlockDriverState *bs);
77 #ifdef _WIN32
78 static int is_windows_drive_prefix(const char *filename)
80 return (((filename[0] >= 'a' && filename[0] <= 'z') ||
81 (filename[0] >= 'A' && filename[0] <= 'Z')) &&
82 filename[1] == ':');
85 int is_windows_drive(const char *filename)
87 if (is_windows_drive_prefix(filename) &&
88 filename[2] == '\0')
89 return 1;
90 if (strstart(filename, "\\\\.\\", NULL) ||
91 strstart(filename, "//./", NULL))
92 return 1;
93 return 0;
95 #endif
97 size_t bdrv_opt_mem_align(BlockDriverState *bs)
99 if (!bs || !bs->drv) {
100 /* page size or 4k (hdd sector size) should be on the safe side */
101 return MAX(4096, getpagesize());
104 return bs->bl.opt_mem_alignment;
107 size_t bdrv_min_mem_align(BlockDriverState *bs)
109 if (!bs || !bs->drv) {
110 /* page size or 4k (hdd sector size) should be on the safe side */
111 return MAX(4096, getpagesize());
114 return bs->bl.min_mem_alignment;
117 /* check if the path starts with "<protocol>:" */
118 int path_has_protocol(const char *path)
120 const char *p;
122 #ifdef _WIN32
123 if (is_windows_drive(path) ||
124 is_windows_drive_prefix(path)) {
125 return 0;
127 p = path + strcspn(path, ":/\\");
128 #else
129 p = path + strcspn(path, ":/");
130 #endif
132 return *p == ':';
135 int path_is_absolute(const char *path)
137 #ifdef _WIN32
138 /* specific case for names like: "\\.\d:" */
139 if (is_windows_drive(path) || is_windows_drive_prefix(path)) {
140 return 1;
142 return (*path == '/' || *path == '\\');
143 #else
144 return (*path == '/');
145 #endif
148 /* if filename is absolute, just copy it to dest. Otherwise, build a
149 path to it by considering it is relative to base_path. URL are
150 supported. */
151 void path_combine(char *dest, int dest_size,
152 const char *base_path,
153 const char *filename)
155 const char *p, *p1;
156 int len;
158 if (dest_size <= 0)
159 return;
160 if (path_is_absolute(filename)) {
161 pstrcpy(dest, dest_size, filename);
162 } else {
163 p = strchr(base_path, ':');
164 if (p)
165 p++;
166 else
167 p = base_path;
168 p1 = strrchr(base_path, '/');
169 #ifdef _WIN32
171 const char *p2;
172 p2 = strrchr(base_path, '\\');
173 if (!p1 || p2 > p1)
174 p1 = p2;
176 #endif
177 if (p1)
178 p1++;
179 else
180 p1 = base_path;
181 if (p1 > p)
182 p = p1;
183 len = p - base_path;
184 if (len > dest_size - 1)
185 len = dest_size - 1;
186 memcpy(dest, base_path, len);
187 dest[len] = '\0';
188 pstrcat(dest, dest_size, filename);
192 void bdrv_get_full_backing_filename_from_filename(const char *backed,
193 const char *backing,
194 char *dest, size_t sz,
195 Error **errp)
197 if (backing[0] == '\0' || path_has_protocol(backing) ||
198 path_is_absolute(backing))
200 pstrcpy(dest, sz, backing);
201 } else if (backed[0] == '\0' || strstart(backed, "json:", NULL)) {
202 error_setg(errp, "Cannot use relative backing file names for '%s'",
203 backed);
204 } else {
205 path_combine(dest, sz, backed, backing);
209 void bdrv_get_full_backing_filename(BlockDriverState *bs, char *dest, size_t sz,
210 Error **errp)
212 char *backed = bs->exact_filename[0] ? bs->exact_filename : bs->filename;
214 bdrv_get_full_backing_filename_from_filename(backed, bs->backing_file,
215 dest, sz, errp);
218 void bdrv_register(BlockDriver *bdrv)
220 QLIST_INSERT_HEAD(&bdrv_drivers, bdrv, list);
223 BlockDriverState *bdrv_new(void)
225 BlockDriverState *bs;
226 int i;
228 bs = g_new0(BlockDriverState, 1);
229 QLIST_INIT(&bs->dirty_bitmaps);
230 for (i = 0; i < BLOCK_OP_TYPE_MAX; i++) {
231 QLIST_INIT(&bs->op_blockers[i]);
233 notifier_with_return_list_init(&bs->before_write_notifiers);
234 bs->refcnt = 1;
235 bs->aio_context = qemu_get_aio_context();
237 QTAILQ_INSERT_TAIL(&all_bdrv_states, bs, bs_list);
239 return bs;
242 BlockDriver *bdrv_find_format(const char *format_name)
244 BlockDriver *drv1;
245 QLIST_FOREACH(drv1, &bdrv_drivers, list) {
246 if (!strcmp(drv1->format_name, format_name)) {
247 return drv1;
250 return NULL;
253 static int bdrv_is_whitelisted(BlockDriver *drv, bool read_only)
255 static const char *whitelist_rw[] = {
256 CONFIG_BDRV_RW_WHITELIST
258 static const char *whitelist_ro[] = {
259 CONFIG_BDRV_RO_WHITELIST
261 const char **p;
263 if (!whitelist_rw[0] && !whitelist_ro[0]) {
264 return 1; /* no whitelist, anything goes */
267 for (p = whitelist_rw; *p; p++) {
268 if (!strcmp(drv->format_name, *p)) {
269 return 1;
272 if (read_only) {
273 for (p = whitelist_ro; *p; p++) {
274 if (!strcmp(drv->format_name, *p)) {
275 return 1;
279 return 0;
282 bool bdrv_uses_whitelist(void)
284 return use_bdrv_whitelist;
287 typedef struct CreateCo {
288 BlockDriver *drv;
289 char *filename;
290 QemuOpts *opts;
291 int ret;
292 Error *err;
293 } CreateCo;
295 static void coroutine_fn bdrv_create_co_entry(void *opaque)
297 Error *local_err = NULL;
298 int ret;
300 CreateCo *cco = opaque;
301 assert(cco->drv);
303 ret = cco->drv->bdrv_create(cco->filename, cco->opts, &local_err);
304 if (local_err) {
305 error_propagate(&cco->err, local_err);
307 cco->ret = ret;
310 int bdrv_create(BlockDriver *drv, const char* filename,
311 QemuOpts *opts, Error **errp)
313 int ret;
315 Coroutine *co;
316 CreateCo cco = {
317 .drv = drv,
318 .filename = g_strdup(filename),
319 .opts = opts,
320 .ret = NOT_DONE,
321 .err = NULL,
324 if (!drv->bdrv_create) {
325 error_setg(errp, "Driver '%s' does not support image creation", drv->format_name);
326 ret = -ENOTSUP;
327 goto out;
330 if (qemu_in_coroutine()) {
331 /* Fast-path if already in coroutine context */
332 bdrv_create_co_entry(&cco);
333 } else {
334 co = qemu_coroutine_create(bdrv_create_co_entry);
335 qemu_coroutine_enter(co, &cco);
336 while (cco.ret == NOT_DONE) {
337 aio_poll(qemu_get_aio_context(), true);
341 ret = cco.ret;
342 if (ret < 0) {
343 if (cco.err) {
344 error_propagate(errp, cco.err);
345 } else {
346 error_setg_errno(errp, -ret, "Could not create image");
350 out:
351 g_free(cco.filename);
352 return ret;
355 int bdrv_create_file(const char *filename, QemuOpts *opts, Error **errp)
357 BlockDriver *drv;
358 Error *local_err = NULL;
359 int ret;
361 drv = bdrv_find_protocol(filename, true, errp);
362 if (drv == NULL) {
363 return -ENOENT;
366 ret = bdrv_create(drv, filename, opts, &local_err);
367 if (local_err) {
368 error_propagate(errp, local_err);
370 return ret;
374 * Try to get @bs's logical and physical block size.
375 * On success, store them in @bsz struct and return 0.
376 * On failure return -errno.
377 * @bs must not be empty.
379 int bdrv_probe_blocksizes(BlockDriverState *bs, BlockSizes *bsz)
381 BlockDriver *drv = bs->drv;
383 if (drv && drv->bdrv_probe_blocksizes) {
384 return drv->bdrv_probe_blocksizes(bs, bsz);
387 return -ENOTSUP;
391 * Try to get @bs's geometry (cyls, heads, sectors).
392 * On success, store them in @geo struct and return 0.
393 * On failure return -errno.
394 * @bs must not be empty.
396 int bdrv_probe_geometry(BlockDriverState *bs, HDGeometry *geo)
398 BlockDriver *drv = bs->drv;
400 if (drv && drv->bdrv_probe_geometry) {
401 return drv->bdrv_probe_geometry(bs, geo);
404 return -ENOTSUP;
408 * Create a uniquely-named empty temporary file.
409 * Return 0 upon success, otherwise a negative errno value.
411 int get_tmp_filename(char *filename, int size)
413 #ifdef _WIN32
414 char temp_dir[MAX_PATH];
415 /* GetTempFileName requires that its output buffer (4th param)
416 have length MAX_PATH or greater. */
417 assert(size >= MAX_PATH);
418 return (GetTempPath(MAX_PATH, temp_dir)
419 && GetTempFileName(temp_dir, "qem", 0, filename)
420 ? 0 : -GetLastError());
421 #else
422 int fd;
423 const char *tmpdir;
424 tmpdir = getenv("TMPDIR");
425 if (!tmpdir) {
426 tmpdir = "/var/tmp";
428 if (snprintf(filename, size, "%s/vl.XXXXXX", tmpdir) >= size) {
429 return -EOVERFLOW;
431 fd = mkstemp(filename);
432 if (fd < 0) {
433 return -errno;
435 if (close(fd) != 0) {
436 unlink(filename);
437 return -errno;
439 return 0;
440 #endif
444 * Detect host devices. By convention, /dev/cdrom[N] is always
445 * recognized as a host CDROM.
447 static BlockDriver *find_hdev_driver(const char *filename)
449 int score_max = 0, score;
450 BlockDriver *drv = NULL, *d;
452 QLIST_FOREACH(d, &bdrv_drivers, list) {
453 if (d->bdrv_probe_device) {
454 score = d->bdrv_probe_device(filename);
455 if (score > score_max) {
456 score_max = score;
457 drv = d;
462 return drv;
465 BlockDriver *bdrv_find_protocol(const char *filename,
466 bool allow_protocol_prefix,
467 Error **errp)
469 BlockDriver *drv1;
470 char protocol[128];
471 int len;
472 const char *p;
474 /* TODO Drivers without bdrv_file_open must be specified explicitly */
477 * XXX(hch): we really should not let host device detection
478 * override an explicit protocol specification, but moving this
479 * later breaks access to device names with colons in them.
480 * Thanks to the brain-dead persistent naming schemes on udev-
481 * based Linux systems those actually are quite common.
483 drv1 = find_hdev_driver(filename);
484 if (drv1) {
485 return drv1;
488 if (!path_has_protocol(filename) || !allow_protocol_prefix) {
489 return &bdrv_file;
492 p = strchr(filename, ':');
493 assert(p != NULL);
494 len = p - filename;
495 if (len > sizeof(protocol) - 1)
496 len = sizeof(protocol) - 1;
497 memcpy(protocol, filename, len);
498 protocol[len] = '\0';
499 QLIST_FOREACH(drv1, &bdrv_drivers, list) {
500 if (drv1->protocol_name &&
501 !strcmp(drv1->protocol_name, protocol)) {
502 return drv1;
506 error_setg(errp, "Unknown protocol '%s'", protocol);
507 return NULL;
511 * Guess image format by probing its contents.
512 * This is not a good idea when your image is raw (CVE-2008-2004), but
513 * we do it anyway for backward compatibility.
515 * @buf contains the image's first @buf_size bytes.
516 * @buf_size is the buffer size in bytes (generally BLOCK_PROBE_BUF_SIZE,
517 * but can be smaller if the image file is smaller)
518 * @filename is its filename.
520 * For all block drivers, call the bdrv_probe() method to get its
521 * probing score.
522 * Return the first block driver with the highest probing score.
524 BlockDriver *bdrv_probe_all(const uint8_t *buf, int buf_size,
525 const char *filename)
527 int score_max = 0, score;
528 BlockDriver *drv = NULL, *d;
530 QLIST_FOREACH(d, &bdrv_drivers, list) {
531 if (d->bdrv_probe) {
532 score = d->bdrv_probe(buf, buf_size, filename);
533 if (score > score_max) {
534 score_max = score;
535 drv = d;
540 return drv;
543 static int find_image_format(BlockDriverState *bs, const char *filename,
544 BlockDriver **pdrv, Error **errp)
546 BlockDriver *drv;
547 uint8_t buf[BLOCK_PROBE_BUF_SIZE];
548 int ret = 0;
550 /* Return the raw BlockDriver * to scsi-generic devices or empty drives */
551 if (bdrv_is_sg(bs) || !bdrv_is_inserted(bs) || bdrv_getlength(bs) == 0) {
552 *pdrv = &bdrv_raw;
553 return ret;
556 ret = bdrv_pread(bs, 0, buf, sizeof(buf));
557 if (ret < 0) {
558 error_setg_errno(errp, -ret, "Could not read image for determining its "
559 "format");
560 *pdrv = NULL;
561 return ret;
564 drv = bdrv_probe_all(buf, ret, filename);
565 if (!drv) {
566 error_setg(errp, "Could not determine image format: No compatible "
567 "driver found");
568 ret = -ENOENT;
570 *pdrv = drv;
571 return ret;
575 * Set the current 'total_sectors' value
576 * Return 0 on success, -errno on error.
578 static int refresh_total_sectors(BlockDriverState *bs, int64_t hint)
580 BlockDriver *drv = bs->drv;
582 /* Do not attempt drv->bdrv_getlength() on scsi-generic devices */
583 if (bdrv_is_sg(bs))
584 return 0;
586 /* query actual device if possible, otherwise just trust the hint */
587 if (drv->bdrv_getlength) {
588 int64_t length = drv->bdrv_getlength(bs);
589 if (length < 0) {
590 return length;
592 hint = DIV_ROUND_UP(length, BDRV_SECTOR_SIZE);
595 bs->total_sectors = hint;
596 return 0;
600 * Combines a QDict of new block driver @options with any missing options taken
601 * from @old_options, so that leaving out an option defaults to its old value.
603 static void bdrv_join_options(BlockDriverState *bs, QDict *options,
604 QDict *old_options)
606 if (bs->drv && bs->drv->bdrv_join_options) {
607 bs->drv->bdrv_join_options(options, old_options);
608 } else {
609 qdict_join(options, old_options, false);
614 * Set open flags for a given discard mode
616 * Return 0 on success, -1 if the discard mode was invalid.
618 int bdrv_parse_discard_flags(const char *mode, int *flags)
620 *flags &= ~BDRV_O_UNMAP;
622 if (!strcmp(mode, "off") || !strcmp(mode, "ignore")) {
623 /* do nothing */
624 } else if (!strcmp(mode, "on") || !strcmp(mode, "unmap")) {
625 *flags |= BDRV_O_UNMAP;
626 } else {
627 return -1;
630 return 0;
634 * Set open flags for a given cache mode
636 * Return 0 on success, -1 if the cache mode was invalid.
638 int bdrv_parse_cache_mode(const char *mode, int *flags, bool *writethrough)
640 *flags &= ~BDRV_O_CACHE_MASK;
642 if (!strcmp(mode, "off") || !strcmp(mode, "none")) {
643 *writethrough = false;
644 *flags |= BDRV_O_NOCACHE;
645 } else if (!strcmp(mode, "directsync")) {
646 *writethrough = true;
647 *flags |= BDRV_O_NOCACHE;
648 } else if (!strcmp(mode, "writeback")) {
649 *writethrough = false;
650 } else if (!strcmp(mode, "unsafe")) {
651 *writethrough = false;
652 *flags |= BDRV_O_NO_FLUSH;
653 } else if (!strcmp(mode, "writethrough")) {
654 *writethrough = true;
655 } else {
656 return -1;
659 return 0;
663 * Returns the options and flags that a temporary snapshot should get, based on
664 * the originally requested flags (the originally requested image will have
665 * flags like a backing file)
667 static void bdrv_temp_snapshot_options(int *child_flags, QDict *child_options,
668 int parent_flags, QDict *parent_options)
670 *child_flags = (parent_flags & ~BDRV_O_SNAPSHOT) | BDRV_O_TEMPORARY;
672 /* For temporary files, unconditional cache=unsafe is fine */
673 qdict_set_default_str(child_options, BDRV_OPT_CACHE_DIRECT, "off");
674 qdict_set_default_str(child_options, BDRV_OPT_CACHE_NO_FLUSH, "on");
678 * Returns the options and flags that bs->file should get if a protocol driver
679 * is expected, based on the given options and flags for the parent BDS
681 static void bdrv_inherited_options(int *child_flags, QDict *child_options,
682 int parent_flags, QDict *parent_options)
684 int flags = parent_flags;
686 /* Enable protocol handling, disable format probing for bs->file */
687 flags |= BDRV_O_PROTOCOL;
689 /* If the cache mode isn't explicitly set, inherit direct and no-flush from
690 * the parent. */
691 qdict_copy_default(child_options, parent_options, BDRV_OPT_CACHE_DIRECT);
692 qdict_copy_default(child_options, parent_options, BDRV_OPT_CACHE_NO_FLUSH);
694 /* Our block drivers take care to send flushes and respect unmap policy,
695 * so we can default to enable both on lower layers regardless of the
696 * corresponding parent options. */
697 flags |= BDRV_O_UNMAP;
699 /* Clear flags that only apply to the top layer */
700 flags &= ~(BDRV_O_SNAPSHOT | BDRV_O_NO_BACKING | BDRV_O_COPY_ON_READ |
701 BDRV_O_NO_IO);
703 *child_flags = flags;
706 const BdrvChildRole child_file = {
707 .inherit_options = bdrv_inherited_options,
711 * Returns the options and flags that bs->file should get if the use of formats
712 * (and not only protocols) is permitted for it, based on the given options and
713 * flags for the parent BDS
715 static void bdrv_inherited_fmt_options(int *child_flags, QDict *child_options,
716 int parent_flags, QDict *parent_options)
718 child_file.inherit_options(child_flags, child_options,
719 parent_flags, parent_options);
721 *child_flags &= ~(BDRV_O_PROTOCOL | BDRV_O_NO_IO);
724 const BdrvChildRole child_format = {
725 .inherit_options = bdrv_inherited_fmt_options,
729 * Returns the options and flags that bs->backing should get, based on the
730 * given options and flags for the parent BDS
732 static void bdrv_backing_options(int *child_flags, QDict *child_options,
733 int parent_flags, QDict *parent_options)
735 int flags = parent_flags;
737 /* The cache mode is inherited unmodified for backing files; except WCE,
738 * which is only applied on the top level (BlockBackend) */
739 qdict_copy_default(child_options, parent_options, BDRV_OPT_CACHE_DIRECT);
740 qdict_copy_default(child_options, parent_options, BDRV_OPT_CACHE_NO_FLUSH);
742 /* backing files always opened read-only */
743 flags &= ~(BDRV_O_RDWR | BDRV_O_COPY_ON_READ);
745 /* snapshot=on is handled on the top layer */
746 flags &= ~(BDRV_O_SNAPSHOT | BDRV_O_TEMPORARY);
748 *child_flags = flags;
751 static const BdrvChildRole child_backing = {
752 .inherit_options = bdrv_backing_options,
755 static int bdrv_open_flags(BlockDriverState *bs, int flags)
757 int open_flags = flags;
760 * Clear flags that are internal to the block layer before opening the
761 * image.
763 open_flags &= ~(BDRV_O_SNAPSHOT | BDRV_O_NO_BACKING | BDRV_O_PROTOCOL);
766 * Snapshots should be writable.
768 if (flags & BDRV_O_TEMPORARY) {
769 open_flags |= BDRV_O_RDWR;
772 return open_flags;
775 static void update_flags_from_options(int *flags, QemuOpts *opts)
777 *flags &= ~BDRV_O_CACHE_MASK;
779 assert(qemu_opt_find(opts, BDRV_OPT_CACHE_NO_FLUSH));
780 if (qemu_opt_get_bool(opts, BDRV_OPT_CACHE_NO_FLUSH, false)) {
781 *flags |= BDRV_O_NO_FLUSH;
784 assert(qemu_opt_find(opts, BDRV_OPT_CACHE_DIRECT));
785 if (qemu_opt_get_bool(opts, BDRV_OPT_CACHE_DIRECT, false)) {
786 *flags |= BDRV_O_NOCACHE;
790 static void update_options_from_flags(QDict *options, int flags)
792 if (!qdict_haskey(options, BDRV_OPT_CACHE_DIRECT)) {
793 qdict_put(options, BDRV_OPT_CACHE_DIRECT,
794 qbool_from_bool(flags & BDRV_O_NOCACHE));
796 if (!qdict_haskey(options, BDRV_OPT_CACHE_NO_FLUSH)) {
797 qdict_put(options, BDRV_OPT_CACHE_NO_FLUSH,
798 qbool_from_bool(flags & BDRV_O_NO_FLUSH));
802 static void bdrv_assign_node_name(BlockDriverState *bs,
803 const char *node_name,
804 Error **errp)
806 char *gen_node_name = NULL;
808 if (!node_name) {
809 node_name = gen_node_name = id_generate(ID_BLOCK);
810 } else if (!id_wellformed(node_name)) {
812 * Check for empty string or invalid characters, but not if it is
813 * generated (generated names use characters not available to the user)
815 error_setg(errp, "Invalid node name");
816 return;
819 /* takes care of avoiding namespaces collisions */
820 if (blk_by_name(node_name)) {
821 error_setg(errp, "node-name=%s is conflicting with a device id",
822 node_name);
823 goto out;
826 /* takes care of avoiding duplicates node names */
827 if (bdrv_find_node(node_name)) {
828 error_setg(errp, "Duplicate node name");
829 goto out;
832 /* copy node name into the bs and insert it into the graph list */
833 pstrcpy(bs->node_name, sizeof(bs->node_name), node_name);
834 QTAILQ_INSERT_TAIL(&graph_bdrv_states, bs, node_list);
835 out:
836 g_free(gen_node_name);
839 static QemuOptsList bdrv_runtime_opts = {
840 .name = "bdrv_common",
841 .head = QTAILQ_HEAD_INITIALIZER(bdrv_runtime_opts.head),
842 .desc = {
844 .name = "node-name",
845 .type = QEMU_OPT_STRING,
846 .help = "Node name of the block device node",
849 .name = "driver",
850 .type = QEMU_OPT_STRING,
851 .help = "Block driver to use for the node",
854 .name = BDRV_OPT_CACHE_DIRECT,
855 .type = QEMU_OPT_BOOL,
856 .help = "Bypass software writeback cache on the host",
859 .name = BDRV_OPT_CACHE_NO_FLUSH,
860 .type = QEMU_OPT_BOOL,
861 .help = "Ignore flush requests",
863 { /* end of list */ }
868 * Common part for opening disk images and files
870 * Removes all processed options from *options.
872 static int bdrv_open_common(BlockDriverState *bs, BdrvChild *file,
873 QDict *options, Error **errp)
875 int ret, open_flags;
876 const char *filename;
877 const char *driver_name = NULL;
878 const char *node_name = NULL;
879 QemuOpts *opts;
880 BlockDriver *drv;
881 Error *local_err = NULL;
883 assert(bs->file == NULL);
884 assert(options != NULL && bs->options != options);
886 opts = qemu_opts_create(&bdrv_runtime_opts, NULL, 0, &error_abort);
887 qemu_opts_absorb_qdict(opts, options, &local_err);
888 if (local_err) {
889 error_propagate(errp, local_err);
890 ret = -EINVAL;
891 goto fail_opts;
894 driver_name = qemu_opt_get(opts, "driver");
895 drv = bdrv_find_format(driver_name);
896 assert(drv != NULL);
898 if (file != NULL) {
899 filename = file->bs->filename;
900 } else {
901 filename = qdict_get_try_str(options, "filename");
904 if (drv->bdrv_needs_filename && !filename) {
905 error_setg(errp, "The '%s' block driver requires a file name",
906 drv->format_name);
907 ret = -EINVAL;
908 goto fail_opts;
911 trace_bdrv_open_common(bs, filename ?: "", bs->open_flags,
912 drv->format_name);
914 node_name = qemu_opt_get(opts, "node-name");
915 bdrv_assign_node_name(bs, node_name, &local_err);
916 if (local_err) {
917 error_propagate(errp, local_err);
918 ret = -EINVAL;
919 goto fail_opts;
922 bs->request_alignment = 512;
923 bs->zero_beyond_eof = true;
924 bs->read_only = !(bs->open_flags & BDRV_O_RDWR);
926 if (use_bdrv_whitelist && !bdrv_is_whitelisted(drv, bs->read_only)) {
927 error_setg(errp,
928 !bs->read_only && bdrv_is_whitelisted(drv, true)
929 ? "Driver '%s' can only be used for read-only devices"
930 : "Driver '%s' is not whitelisted",
931 drv->format_name);
932 ret = -ENOTSUP;
933 goto fail_opts;
936 assert(bs->copy_on_read == 0); /* bdrv_new() and bdrv_close() make it so */
937 if (bs->open_flags & BDRV_O_COPY_ON_READ) {
938 if (!bs->read_only) {
939 bdrv_enable_copy_on_read(bs);
940 } else {
941 error_setg(errp, "Can't use copy-on-read on read-only device");
942 ret = -EINVAL;
943 goto fail_opts;
947 if (filename != NULL) {
948 pstrcpy(bs->filename, sizeof(bs->filename), filename);
949 } else {
950 bs->filename[0] = '\0';
952 pstrcpy(bs->exact_filename, sizeof(bs->exact_filename), bs->filename);
954 bs->drv = drv;
955 bs->opaque = g_malloc0(drv->instance_size);
957 /* Apply cache mode options */
958 update_flags_from_options(&bs->open_flags, opts);
960 /* Open the image, either directly or using a protocol */
961 open_flags = bdrv_open_flags(bs, bs->open_flags);
962 if (drv->bdrv_file_open) {
963 assert(file == NULL);
964 assert(!drv->bdrv_needs_filename || filename != NULL);
965 ret = drv->bdrv_file_open(bs, options, open_flags, &local_err);
966 } else {
967 if (file == NULL) {
968 error_setg(errp, "Can't use '%s' as a block driver for the "
969 "protocol level", drv->format_name);
970 ret = -EINVAL;
971 goto free_and_fail;
973 bs->file = file;
974 ret = drv->bdrv_open(bs, options, open_flags, &local_err);
977 if (ret < 0) {
978 if (local_err) {
979 error_propagate(errp, local_err);
980 } else if (bs->filename[0]) {
981 error_setg_errno(errp, -ret, "Could not open '%s'", bs->filename);
982 } else {
983 error_setg_errno(errp, -ret, "Could not open image");
985 goto free_and_fail;
988 ret = refresh_total_sectors(bs, bs->total_sectors);
989 if (ret < 0) {
990 error_setg_errno(errp, -ret, "Could not refresh total sector count");
991 goto free_and_fail;
994 bdrv_refresh_limits(bs, &local_err);
995 if (local_err) {
996 error_propagate(errp, local_err);
997 ret = -EINVAL;
998 goto free_and_fail;
1001 assert(bdrv_opt_mem_align(bs) != 0);
1002 assert(bdrv_min_mem_align(bs) != 0);
1003 assert((bs->request_alignment != 0) || bdrv_is_sg(bs));
1005 qemu_opts_del(opts);
1006 return 0;
1008 free_and_fail:
1009 bs->file = NULL;
1010 g_free(bs->opaque);
1011 bs->opaque = NULL;
1012 bs->drv = NULL;
1013 fail_opts:
1014 qemu_opts_del(opts);
1015 return ret;
1018 static QDict *parse_json_filename(const char *filename, Error **errp)
1020 QObject *options_obj;
1021 QDict *options;
1022 int ret;
1024 ret = strstart(filename, "json:", &filename);
1025 assert(ret);
1027 options_obj = qobject_from_json(filename);
1028 if (!options_obj) {
1029 error_setg(errp, "Could not parse the JSON options");
1030 return NULL;
1033 if (qobject_type(options_obj) != QTYPE_QDICT) {
1034 qobject_decref(options_obj);
1035 error_setg(errp, "Invalid JSON object given");
1036 return NULL;
1039 options = qobject_to_qdict(options_obj);
1040 qdict_flatten(options);
1042 return options;
1045 static void parse_json_protocol(QDict *options, const char **pfilename,
1046 Error **errp)
1048 QDict *json_options;
1049 Error *local_err = NULL;
1051 /* Parse json: pseudo-protocol */
1052 if (!*pfilename || !g_str_has_prefix(*pfilename, "json:")) {
1053 return;
1056 json_options = parse_json_filename(*pfilename, &local_err);
1057 if (local_err) {
1058 error_propagate(errp, local_err);
1059 return;
1062 /* Options given in the filename have lower priority than options
1063 * specified directly */
1064 qdict_join(options, json_options, false);
1065 QDECREF(json_options);
1066 *pfilename = NULL;
1070 * Fills in default options for opening images and converts the legacy
1071 * filename/flags pair to option QDict entries.
1072 * The BDRV_O_PROTOCOL flag in *flags will be set or cleared accordingly if a
1073 * block driver has been specified explicitly.
1075 static int bdrv_fill_options(QDict **options, const char *filename,
1076 int *flags, Error **errp)
1078 const char *drvname;
1079 bool protocol = *flags & BDRV_O_PROTOCOL;
1080 bool parse_filename = false;
1081 BlockDriver *drv = NULL;
1082 Error *local_err = NULL;
1084 drvname = qdict_get_try_str(*options, "driver");
1085 if (drvname) {
1086 drv = bdrv_find_format(drvname);
1087 if (!drv) {
1088 error_setg(errp, "Unknown driver '%s'", drvname);
1089 return -ENOENT;
1091 /* If the user has explicitly specified the driver, this choice should
1092 * override the BDRV_O_PROTOCOL flag */
1093 protocol = drv->bdrv_file_open;
1096 if (protocol) {
1097 *flags |= BDRV_O_PROTOCOL;
1098 } else {
1099 *flags &= ~BDRV_O_PROTOCOL;
1102 /* Translate cache options from flags into options */
1103 update_options_from_flags(*options, *flags);
1105 /* Fetch the file name from the options QDict if necessary */
1106 if (protocol && filename) {
1107 if (!qdict_haskey(*options, "filename")) {
1108 qdict_put(*options, "filename", qstring_from_str(filename));
1109 parse_filename = true;
1110 } else {
1111 error_setg(errp, "Can't specify 'file' and 'filename' options at "
1112 "the same time");
1113 return -EINVAL;
1117 /* Find the right block driver */
1118 filename = qdict_get_try_str(*options, "filename");
1120 if (!drvname && protocol) {
1121 if (filename) {
1122 drv = bdrv_find_protocol(filename, parse_filename, errp);
1123 if (!drv) {
1124 return -EINVAL;
1127 drvname = drv->format_name;
1128 qdict_put(*options, "driver", qstring_from_str(drvname));
1129 } else {
1130 error_setg(errp, "Must specify either driver or file");
1131 return -EINVAL;
1135 assert(drv || !protocol);
1137 /* Driver-specific filename parsing */
1138 if (drv && drv->bdrv_parse_filename && parse_filename) {
1139 drv->bdrv_parse_filename(filename, *options, &local_err);
1140 if (local_err) {
1141 error_propagate(errp, local_err);
1142 return -EINVAL;
1145 if (!drv->bdrv_needs_filename) {
1146 qdict_del(*options, "filename");
1150 return 0;
1153 BdrvChild *bdrv_root_attach_child(BlockDriverState *child_bs,
1154 const char *child_name,
1155 const BdrvChildRole *child_role)
1157 BdrvChild *child = g_new(BdrvChild, 1);
1158 *child = (BdrvChild) {
1159 .bs = child_bs,
1160 .name = g_strdup(child_name),
1161 .role = child_role,
1164 QLIST_INSERT_HEAD(&child_bs->parents, child, next_parent);
1166 return child;
1169 BdrvChild *bdrv_attach_child(BlockDriverState *parent_bs,
1170 BlockDriverState *child_bs,
1171 const char *child_name,
1172 const BdrvChildRole *child_role)
1174 BdrvChild *child = bdrv_root_attach_child(child_bs, child_name, child_role);
1175 QLIST_INSERT_HEAD(&parent_bs->children, child, next);
1176 return child;
1179 static void bdrv_detach_child(BdrvChild *child)
1181 if (child->next.le_prev) {
1182 QLIST_REMOVE(child, next);
1183 child->next.le_prev = NULL;
1185 QLIST_REMOVE(child, next_parent);
1186 g_free(child->name);
1187 g_free(child);
1190 void bdrv_root_unref_child(BdrvChild *child)
1192 BlockDriverState *child_bs;
1194 child_bs = child->bs;
1195 bdrv_detach_child(child);
1196 bdrv_unref(child_bs);
1199 void bdrv_unref_child(BlockDriverState *parent, BdrvChild *child)
1201 if (child == NULL) {
1202 return;
1205 if (child->bs->inherits_from == parent) {
1206 child->bs->inherits_from = NULL;
1209 bdrv_root_unref_child(child);
1213 static void bdrv_parent_cb_change_media(BlockDriverState *bs, bool load)
1215 BdrvChild *c;
1216 QLIST_FOREACH(c, &bs->parents, next_parent) {
1217 if (c->role->change_media) {
1218 c->role->change_media(c, load);
1223 static void bdrv_parent_cb_resize(BlockDriverState *bs)
1225 BdrvChild *c;
1226 QLIST_FOREACH(c, &bs->parents, next_parent) {
1227 if (c->role->resize) {
1228 c->role->resize(c);
1234 * Sets the backing file link of a BDS. A new reference is created; callers
1235 * which don't need their own reference any more must call bdrv_unref().
1237 void bdrv_set_backing_hd(BlockDriverState *bs, BlockDriverState *backing_hd)
1239 if (backing_hd) {
1240 bdrv_ref(backing_hd);
1243 if (bs->backing) {
1244 assert(bs->backing_blocker);
1245 bdrv_op_unblock_all(bs->backing->bs, bs->backing_blocker);
1246 bdrv_unref_child(bs, bs->backing);
1247 } else if (backing_hd) {
1248 error_setg(&bs->backing_blocker,
1249 "node is used as backing hd of '%s'",
1250 bdrv_get_device_or_node_name(bs));
1253 if (!backing_hd) {
1254 error_free(bs->backing_blocker);
1255 bs->backing_blocker = NULL;
1256 bs->backing = NULL;
1257 goto out;
1259 bs->backing = bdrv_attach_child(bs, backing_hd, "backing", &child_backing);
1260 bs->open_flags &= ~BDRV_O_NO_BACKING;
1261 pstrcpy(bs->backing_file, sizeof(bs->backing_file), backing_hd->filename);
1262 pstrcpy(bs->backing_format, sizeof(bs->backing_format),
1263 backing_hd->drv ? backing_hd->drv->format_name : "");
1265 bdrv_op_block_all(backing_hd, bs->backing_blocker);
1266 /* Otherwise we won't be able to commit due to check in bdrv_commit */
1267 bdrv_op_unblock(backing_hd, BLOCK_OP_TYPE_COMMIT_TARGET,
1268 bs->backing_blocker);
1269 out:
1270 bdrv_refresh_limits(bs, NULL);
1274 * Opens the backing file for a BlockDriverState if not yet open
1276 * bdref_key specifies the key for the image's BlockdevRef in the options QDict.
1277 * That QDict has to be flattened; therefore, if the BlockdevRef is a QDict
1278 * itself, all options starting with "${bdref_key}." are considered part of the
1279 * BlockdevRef.
1281 * TODO Can this be unified with bdrv_open_image()?
1283 int bdrv_open_backing_file(BlockDriverState *bs, QDict *parent_options,
1284 const char *bdref_key, Error **errp)
1286 char *backing_filename = g_malloc0(PATH_MAX);
1287 char *bdref_key_dot;
1288 const char *reference = NULL;
1289 int ret = 0;
1290 BlockDriverState *backing_hd;
1291 QDict *options;
1292 QDict *tmp_parent_options = NULL;
1293 Error *local_err = NULL;
1295 if (bs->backing != NULL) {
1296 goto free_exit;
1299 /* NULL means an empty set of options */
1300 if (parent_options == NULL) {
1301 tmp_parent_options = qdict_new();
1302 parent_options = tmp_parent_options;
1305 bs->open_flags &= ~BDRV_O_NO_BACKING;
1307 bdref_key_dot = g_strdup_printf("%s.", bdref_key);
1308 qdict_extract_subqdict(parent_options, &options, bdref_key_dot);
1309 g_free(bdref_key_dot);
1311 reference = qdict_get_try_str(parent_options, bdref_key);
1312 if (reference || qdict_haskey(options, "file.filename")) {
1313 backing_filename[0] = '\0';
1314 } else if (bs->backing_file[0] == '\0' && qdict_size(options) == 0) {
1315 QDECREF(options);
1316 goto free_exit;
1317 } else {
1318 bdrv_get_full_backing_filename(bs, backing_filename, PATH_MAX,
1319 &local_err);
1320 if (local_err) {
1321 ret = -EINVAL;
1322 error_propagate(errp, local_err);
1323 QDECREF(options);
1324 goto free_exit;
1328 if (!bs->drv || !bs->drv->supports_backing) {
1329 ret = -EINVAL;
1330 error_setg(errp, "Driver doesn't support backing files");
1331 QDECREF(options);
1332 goto free_exit;
1335 if (bs->backing_format[0] != '\0' && !qdict_haskey(options, "driver")) {
1336 qdict_put(options, "driver", qstring_from_str(bs->backing_format));
1339 backing_hd = NULL;
1340 ret = bdrv_open_inherit(&backing_hd,
1341 *backing_filename ? backing_filename : NULL,
1342 reference, options, 0, bs, &child_backing,
1343 errp);
1344 if (ret < 0) {
1345 bs->open_flags |= BDRV_O_NO_BACKING;
1346 error_prepend(errp, "Could not open backing file: ");
1347 goto free_exit;
1350 /* Hook up the backing file link; drop our reference, bs owns the
1351 * backing_hd reference now */
1352 bdrv_set_backing_hd(bs, backing_hd);
1353 bdrv_unref(backing_hd);
1355 qdict_del(parent_options, bdref_key);
1357 free_exit:
1358 g_free(backing_filename);
1359 QDECREF(tmp_parent_options);
1360 return ret;
1364 * Opens a disk image whose options are given as BlockdevRef in another block
1365 * device's options.
1367 * If allow_none is true, no image will be opened if filename is false and no
1368 * BlockdevRef is given. NULL will be returned, but errp remains unset.
1370 * bdrev_key specifies the key for the image's BlockdevRef in the options QDict.
1371 * That QDict has to be flattened; therefore, if the BlockdevRef is a QDict
1372 * itself, all options starting with "${bdref_key}." are considered part of the
1373 * BlockdevRef.
1375 * The BlockdevRef will be removed from the options QDict.
1377 BdrvChild *bdrv_open_child(const char *filename,
1378 QDict *options, const char *bdref_key,
1379 BlockDriverState* parent,
1380 const BdrvChildRole *child_role,
1381 bool allow_none, Error **errp)
1383 BdrvChild *c = NULL;
1384 BlockDriverState *bs;
1385 QDict *image_options;
1386 int ret;
1387 char *bdref_key_dot;
1388 const char *reference;
1390 assert(child_role != NULL);
1392 bdref_key_dot = g_strdup_printf("%s.", bdref_key);
1393 qdict_extract_subqdict(options, &image_options, bdref_key_dot);
1394 g_free(bdref_key_dot);
1396 reference = qdict_get_try_str(options, bdref_key);
1397 if (!filename && !reference && !qdict_size(image_options)) {
1398 if (!allow_none) {
1399 error_setg(errp, "A block device must be specified for \"%s\"",
1400 bdref_key);
1402 QDECREF(image_options);
1403 goto done;
1406 bs = NULL;
1407 ret = bdrv_open_inherit(&bs, filename, reference, image_options, 0,
1408 parent, child_role, errp);
1409 if (ret < 0) {
1410 goto done;
1413 c = bdrv_attach_child(parent, bs, bdref_key, child_role);
1415 done:
1416 qdict_del(options, bdref_key);
1417 return c;
1420 static BlockDriverState *bdrv_append_temp_snapshot(BlockDriverState *bs,
1421 int flags,
1422 QDict *snapshot_options,
1423 Error **errp)
1425 /* TODO: extra byte is a hack to ensure MAX_PATH space on Windows. */
1426 char *tmp_filename = g_malloc0(PATH_MAX + 1);
1427 int64_t total_size;
1428 QemuOpts *opts = NULL;
1429 BlockDriverState *bs_snapshot;
1430 Error *local_err = NULL;
1431 int ret;
1433 /* if snapshot, we create a temporary backing file and open it
1434 instead of opening 'filename' directly */
1436 /* Get the required size from the image */
1437 total_size = bdrv_getlength(bs);
1438 if (total_size < 0) {
1439 error_setg_errno(errp, -total_size, "Could not get image size");
1440 goto out;
1443 /* Create the temporary image */
1444 ret = get_tmp_filename(tmp_filename, PATH_MAX + 1);
1445 if (ret < 0) {
1446 error_setg_errno(errp, -ret, "Could not get temporary filename");
1447 goto out;
1450 opts = qemu_opts_create(bdrv_qcow2.create_opts, NULL, 0,
1451 &error_abort);
1452 qemu_opt_set_number(opts, BLOCK_OPT_SIZE, total_size, &error_abort);
1453 ret = bdrv_create(&bdrv_qcow2, tmp_filename, opts, errp);
1454 qemu_opts_del(opts);
1455 if (ret < 0) {
1456 error_prepend(errp, "Could not create temporary overlay '%s': ",
1457 tmp_filename);
1458 goto out;
1461 /* Prepare options QDict for the temporary file */
1462 qdict_put(snapshot_options, "file.driver",
1463 qstring_from_str("file"));
1464 qdict_put(snapshot_options, "file.filename",
1465 qstring_from_str(tmp_filename));
1466 qdict_put(snapshot_options, "driver",
1467 qstring_from_str("qcow2"));
1469 bs_snapshot = NULL;
1470 ret = bdrv_open(&bs_snapshot, NULL, NULL, snapshot_options,
1471 flags, &local_err);
1472 snapshot_options = NULL;
1473 if (ret < 0) {
1474 error_propagate(errp, local_err);
1475 goto out;
1478 /* bdrv_append() consumes a strong reference to bs_snapshot (i.e. it will
1479 * call bdrv_unref() on it), so in order to be able to return one, we have
1480 * to increase bs_snapshot's refcount here */
1481 bdrv_ref(bs_snapshot);
1482 bdrv_append(bs_snapshot, bs);
1484 g_free(tmp_filename);
1485 return bs_snapshot;
1487 out:
1488 QDECREF(snapshot_options);
1489 g_free(tmp_filename);
1490 return NULL;
1494 * Opens a disk image (raw, qcow2, vmdk, ...)
1496 * options is a QDict of options to pass to the block drivers, or NULL for an
1497 * empty set of options. The reference to the QDict belongs to the block layer
1498 * after the call (even on failure), so if the caller intends to reuse the
1499 * dictionary, it needs to use QINCREF() before calling bdrv_open.
1501 * If *pbs is NULL, a new BDS will be created with a pointer to it stored there.
1502 * If it is not NULL, the referenced BDS will be reused.
1504 * The reference parameter may be used to specify an existing block device which
1505 * should be opened. If specified, neither options nor a filename may be given,
1506 * nor can an existing BDS be reused (that is, *pbs has to be NULL).
1508 static int bdrv_open_inherit(BlockDriverState **pbs, const char *filename,
1509 const char *reference, QDict *options, int flags,
1510 BlockDriverState *parent,
1511 const BdrvChildRole *child_role, Error **errp)
1513 int ret;
1514 BdrvChild *file = NULL;
1515 BlockDriverState *bs;
1516 BlockDriver *drv = NULL;
1517 const char *drvname;
1518 const char *backing;
1519 Error *local_err = NULL;
1520 QDict *snapshot_options = NULL;
1521 int snapshot_flags = 0;
1523 assert(pbs);
1524 assert(!child_role || !flags);
1525 assert(!child_role == !parent);
1527 if (reference) {
1528 bool options_non_empty = options ? qdict_size(options) : false;
1529 QDECREF(options);
1531 if (*pbs) {
1532 error_setg(errp, "Cannot reuse an existing BDS when referencing "
1533 "another block device");
1534 return -EINVAL;
1537 if (filename || options_non_empty) {
1538 error_setg(errp, "Cannot reference an existing block device with "
1539 "additional options or a new filename");
1540 return -EINVAL;
1543 bs = bdrv_lookup_bs(reference, reference, errp);
1544 if (!bs) {
1545 return -ENODEV;
1548 bdrv_ref(bs);
1549 *pbs = bs;
1550 return 0;
1553 if (*pbs) {
1554 bs = *pbs;
1555 } else {
1556 bs = bdrv_new();
1559 /* NULL means an empty set of options */
1560 if (options == NULL) {
1561 options = qdict_new();
1564 /* json: syntax counts as explicit options, as if in the QDict */
1565 parse_json_protocol(options, &filename, &local_err);
1566 if (local_err) {
1567 ret = -EINVAL;
1568 goto fail;
1571 bs->explicit_options = qdict_clone_shallow(options);
1573 if (child_role) {
1574 bs->inherits_from = parent;
1575 child_role->inherit_options(&flags, options,
1576 parent->open_flags, parent->options);
1579 ret = bdrv_fill_options(&options, filename, &flags, &local_err);
1580 if (local_err) {
1581 goto fail;
1584 bs->open_flags = flags;
1585 bs->options = options;
1586 options = qdict_clone_shallow(options);
1588 /* Find the right image format driver */
1589 drvname = qdict_get_try_str(options, "driver");
1590 if (drvname) {
1591 drv = bdrv_find_format(drvname);
1592 if (!drv) {
1593 error_setg(errp, "Unknown driver: '%s'", drvname);
1594 ret = -EINVAL;
1595 goto fail;
1599 assert(drvname || !(flags & BDRV_O_PROTOCOL));
1601 backing = qdict_get_try_str(options, "backing");
1602 if (backing && *backing == '\0') {
1603 flags |= BDRV_O_NO_BACKING;
1604 qdict_del(options, "backing");
1607 /* Open image file without format layer */
1608 if ((flags & BDRV_O_PROTOCOL) == 0) {
1609 if (flags & BDRV_O_RDWR) {
1610 flags |= BDRV_O_ALLOW_RDWR;
1612 if (flags & BDRV_O_SNAPSHOT) {
1613 snapshot_options = qdict_new();
1614 bdrv_temp_snapshot_options(&snapshot_flags, snapshot_options,
1615 flags, options);
1616 bdrv_backing_options(&flags, options, flags, options);
1619 bs->open_flags = flags;
1621 file = bdrv_open_child(filename, options, "file", bs,
1622 &child_file, true, &local_err);
1623 if (local_err) {
1624 ret = -EINVAL;
1625 goto fail;
1629 /* Image format probing */
1630 bs->probed = !drv;
1631 if (!drv && file) {
1632 ret = find_image_format(file->bs, filename, &drv, &local_err);
1633 if (ret < 0) {
1634 goto fail;
1637 * This option update would logically belong in bdrv_fill_options(),
1638 * but we first need to open bs->file for the probing to work, while
1639 * opening bs->file already requires the (mostly) final set of options
1640 * so that cache mode etc. can be inherited.
1642 * Adding the driver later is somewhat ugly, but it's not an option
1643 * that would ever be inherited, so it's correct. We just need to make
1644 * sure to update both bs->options (which has the full effective
1645 * options for bs) and options (which has file.* already removed).
1647 qdict_put(bs->options, "driver", qstring_from_str(drv->format_name));
1648 qdict_put(options, "driver", qstring_from_str(drv->format_name));
1649 } else if (!drv) {
1650 error_setg(errp, "Must specify either driver or file");
1651 ret = -EINVAL;
1652 goto fail;
1655 /* BDRV_O_PROTOCOL must be set iff a protocol BDS is about to be created */
1656 assert(!!(flags & BDRV_O_PROTOCOL) == !!drv->bdrv_file_open);
1657 /* file must be NULL if a protocol BDS is about to be created
1658 * (the inverse results in an error message from bdrv_open_common()) */
1659 assert(!(flags & BDRV_O_PROTOCOL) || !file);
1661 /* Open the image */
1662 ret = bdrv_open_common(bs, file, options, &local_err);
1663 if (ret < 0) {
1664 goto fail;
1667 if (file && (bs->file != file)) {
1668 bdrv_unref_child(bs, file);
1669 file = NULL;
1672 /* If there is a backing file, use it */
1673 if ((flags & BDRV_O_NO_BACKING) == 0) {
1674 ret = bdrv_open_backing_file(bs, options, "backing", &local_err);
1675 if (ret < 0) {
1676 goto close_and_fail;
1680 bdrv_refresh_filename(bs);
1682 /* Check if any unknown options were used */
1683 if (options && (qdict_size(options) != 0)) {
1684 const QDictEntry *entry = qdict_first(options);
1685 if (flags & BDRV_O_PROTOCOL) {
1686 error_setg(errp, "Block protocol '%s' doesn't support the option "
1687 "'%s'", drv->format_name, entry->key);
1688 } else {
1689 error_setg(errp,
1690 "Block format '%s' does not support the option '%s'",
1691 drv->format_name, entry->key);
1694 ret = -EINVAL;
1695 goto close_and_fail;
1698 if (!bdrv_key_required(bs)) {
1699 bdrv_parent_cb_change_media(bs, true);
1700 } else if (!runstate_check(RUN_STATE_PRELAUNCH)
1701 && !runstate_check(RUN_STATE_INMIGRATE)
1702 && !runstate_check(RUN_STATE_PAUSED)) { /* HACK */
1703 error_setg(errp,
1704 "Guest must be stopped for opening of encrypted image");
1705 ret = -EBUSY;
1706 goto close_and_fail;
1709 QDECREF(options);
1711 /* For snapshot=on, create a temporary qcow2 overlay. bs points to the
1712 * temporary snapshot afterwards. */
1713 if (snapshot_flags) {
1714 BlockDriverState *snapshot_bs;
1715 snapshot_bs = bdrv_append_temp_snapshot(bs, snapshot_flags,
1716 snapshot_options, &local_err);
1717 snapshot_options = NULL;
1718 if (local_err) {
1719 ret = -EINVAL;
1720 goto close_and_fail;
1722 if (!*pbs) {
1723 /* We are not going to return bs but the overlay on top of it
1724 * (snapshot_bs); thus, we have to drop the strong reference to bs
1725 * (which we obtained by calling bdrv_new()). bs will not be
1726 * deleted, though, because the overlay still has a reference to it.
1728 bdrv_unref(bs);
1729 bs = snapshot_bs;
1730 } else {
1731 /* We are not going to return snapshot_bs, so we have to drop the
1732 * strong reference to it (which was returned by
1733 * bdrv_append_temp_snapshot()). snapshot_bs will not be deleted,
1734 * though, because bdrv_append_temp_snapshot() made all parental
1735 * references to bs (*pbs) point to snapshot_bs.
1736 * In fact, if *pbs was not NULL, we are not going to return any new
1737 * BDS. But we do not need to decrement bs's refcount here as is
1738 * done above, because with a non-NULL *pbs this function never even
1739 * had a strong reference to bs. */
1740 bdrv_unref(snapshot_bs);
1744 if (!*pbs) {
1745 *pbs = bs;
1748 return 0;
1750 fail:
1751 if (file != NULL) {
1752 bdrv_unref_child(bs, file);
1754 QDECREF(snapshot_options);
1755 QDECREF(bs->explicit_options);
1756 QDECREF(bs->options);
1757 QDECREF(options);
1758 bs->options = NULL;
1759 if (!*pbs) {
1760 /* If *pbs is NULL, a new BDS has been created in this function and
1761 needs to be freed now. Otherwise, it does not need to be closed,
1762 since it has not really been opened yet. */
1763 bdrv_unref(bs);
1765 if (local_err) {
1766 error_propagate(errp, local_err);
1768 return ret;
1770 close_and_fail:
1771 /* See fail path, but now the BDS has to be always closed */
1772 if (*pbs) {
1773 bdrv_close(bs);
1774 } else {
1775 bdrv_unref(bs);
1777 QDECREF(snapshot_options);
1778 QDECREF(options);
1779 if (local_err) {
1780 error_propagate(errp, local_err);
1782 return ret;
1785 int bdrv_open(BlockDriverState **pbs, const char *filename,
1786 const char *reference, QDict *options, int flags, Error **errp)
1788 return bdrv_open_inherit(pbs, filename, reference, options, flags, NULL,
1789 NULL, errp);
1792 typedef struct BlockReopenQueueEntry {
1793 bool prepared;
1794 BDRVReopenState state;
1795 QSIMPLEQ_ENTRY(BlockReopenQueueEntry) entry;
1796 } BlockReopenQueueEntry;
1799 * Adds a BlockDriverState to a simple queue for an atomic, transactional
1800 * reopen of multiple devices.
1802 * bs_queue can either be an existing BlockReopenQueue that has had QSIMPLE_INIT
1803 * already performed, or alternatively may be NULL a new BlockReopenQueue will
1804 * be created and initialized. This newly created BlockReopenQueue should be
1805 * passed back in for subsequent calls that are intended to be of the same
1806 * atomic 'set'.
1808 * bs is the BlockDriverState to add to the reopen queue.
1810 * options contains the changed options for the associated bs
1811 * (the BlockReopenQueue takes ownership)
1813 * flags contains the open flags for the associated bs
1815 * returns a pointer to bs_queue, which is either the newly allocated
1816 * bs_queue, or the existing bs_queue being used.
1819 static BlockReopenQueue *bdrv_reopen_queue_child(BlockReopenQueue *bs_queue,
1820 BlockDriverState *bs,
1821 QDict *options,
1822 int flags,
1823 const BdrvChildRole *role,
1824 QDict *parent_options,
1825 int parent_flags)
1827 assert(bs != NULL);
1829 BlockReopenQueueEntry *bs_entry;
1830 BdrvChild *child;
1831 QDict *old_options, *explicit_options;
1833 if (bs_queue == NULL) {
1834 bs_queue = g_new0(BlockReopenQueue, 1);
1835 QSIMPLEQ_INIT(bs_queue);
1838 if (!options) {
1839 options = qdict_new();
1843 * Precedence of options:
1844 * 1. Explicitly passed in options (highest)
1845 * 2. Set in flags (only for top level)
1846 * 3. Retained from explicitly set options of bs
1847 * 4. Inherited from parent node
1848 * 5. Retained from effective options of bs
1851 if (!parent_options) {
1853 * Any setting represented by flags is always updated. If the
1854 * corresponding QDict option is set, it takes precedence. Otherwise
1855 * the flag is translated into a QDict option. The old setting of bs is
1856 * not considered.
1858 update_options_from_flags(options, flags);
1861 /* Old explicitly set values (don't overwrite by inherited value) */
1862 old_options = qdict_clone_shallow(bs->explicit_options);
1863 bdrv_join_options(bs, options, old_options);
1864 QDECREF(old_options);
1866 explicit_options = qdict_clone_shallow(options);
1868 /* Inherit from parent node */
1869 if (parent_options) {
1870 assert(!flags);
1871 role->inherit_options(&flags, options, parent_flags, parent_options);
1874 /* Old values are used for options that aren't set yet */
1875 old_options = qdict_clone_shallow(bs->options);
1876 bdrv_join_options(bs, options, old_options);
1877 QDECREF(old_options);
1879 /* bdrv_open() masks this flag out */
1880 flags &= ~BDRV_O_PROTOCOL;
1882 QLIST_FOREACH(child, &bs->children, next) {
1883 QDict *new_child_options;
1884 char *child_key_dot;
1886 /* reopen can only change the options of block devices that were
1887 * implicitly created and inherited options. For other (referenced)
1888 * block devices, a syntax like "backing.foo" results in an error. */
1889 if (child->bs->inherits_from != bs) {
1890 continue;
1893 child_key_dot = g_strdup_printf("%s.", child->name);
1894 qdict_extract_subqdict(options, &new_child_options, child_key_dot);
1895 g_free(child_key_dot);
1897 bdrv_reopen_queue_child(bs_queue, child->bs, new_child_options, 0,
1898 child->role, options, flags);
1901 bs_entry = g_new0(BlockReopenQueueEntry, 1);
1902 QSIMPLEQ_INSERT_TAIL(bs_queue, bs_entry, entry);
1904 bs_entry->state.bs = bs;
1905 bs_entry->state.options = options;
1906 bs_entry->state.explicit_options = explicit_options;
1907 bs_entry->state.flags = flags;
1909 return bs_queue;
1912 BlockReopenQueue *bdrv_reopen_queue(BlockReopenQueue *bs_queue,
1913 BlockDriverState *bs,
1914 QDict *options, int flags)
1916 return bdrv_reopen_queue_child(bs_queue, bs, options, flags,
1917 NULL, NULL, 0);
1921 * Reopen multiple BlockDriverStates atomically & transactionally.
1923 * The queue passed in (bs_queue) must have been built up previous
1924 * via bdrv_reopen_queue().
1926 * Reopens all BDS specified in the queue, with the appropriate
1927 * flags. All devices are prepared for reopen, and failure of any
1928 * device will cause all device changes to be abandonded, and intermediate
1929 * data cleaned up.
1931 * If all devices prepare successfully, then the changes are committed
1932 * to all devices.
1935 int bdrv_reopen_multiple(BlockReopenQueue *bs_queue, Error **errp)
1937 int ret = -1;
1938 BlockReopenQueueEntry *bs_entry, *next;
1939 Error *local_err = NULL;
1941 assert(bs_queue != NULL);
1943 bdrv_drain_all();
1945 QSIMPLEQ_FOREACH(bs_entry, bs_queue, entry) {
1946 if (bdrv_reopen_prepare(&bs_entry->state, bs_queue, &local_err)) {
1947 error_propagate(errp, local_err);
1948 goto cleanup;
1950 bs_entry->prepared = true;
1953 /* If we reach this point, we have success and just need to apply the
1954 * changes
1956 QSIMPLEQ_FOREACH(bs_entry, bs_queue, entry) {
1957 bdrv_reopen_commit(&bs_entry->state);
1960 ret = 0;
1962 cleanup:
1963 QSIMPLEQ_FOREACH_SAFE(bs_entry, bs_queue, entry, next) {
1964 if (ret && bs_entry->prepared) {
1965 bdrv_reopen_abort(&bs_entry->state);
1966 } else if (ret) {
1967 QDECREF(bs_entry->state.explicit_options);
1969 QDECREF(bs_entry->state.options);
1970 g_free(bs_entry);
1972 g_free(bs_queue);
1973 return ret;
1977 /* Reopen a single BlockDriverState with the specified flags. */
1978 int bdrv_reopen(BlockDriverState *bs, int bdrv_flags, Error **errp)
1980 int ret = -1;
1981 Error *local_err = NULL;
1982 BlockReopenQueue *queue = bdrv_reopen_queue(NULL, bs, NULL, bdrv_flags);
1984 ret = bdrv_reopen_multiple(queue, &local_err);
1985 if (local_err != NULL) {
1986 error_propagate(errp, local_err);
1988 return ret;
1993 * Prepares a BlockDriverState for reopen. All changes are staged in the
1994 * 'opaque' field of the BDRVReopenState, which is used and allocated by
1995 * the block driver layer .bdrv_reopen_prepare()
1997 * bs is the BlockDriverState to reopen
1998 * flags are the new open flags
1999 * queue is the reopen queue
2001 * Returns 0 on success, non-zero on error. On error errp will be set
2002 * as well.
2004 * On failure, bdrv_reopen_abort() will be called to clean up any data.
2005 * It is the responsibility of the caller to then call the abort() or
2006 * commit() for any other BDS that have been left in a prepare() state
2009 int bdrv_reopen_prepare(BDRVReopenState *reopen_state, BlockReopenQueue *queue,
2010 Error **errp)
2012 int ret = -1;
2013 Error *local_err = NULL;
2014 BlockDriver *drv;
2015 QemuOpts *opts;
2016 const char *value;
2018 assert(reopen_state != NULL);
2019 assert(reopen_state->bs->drv != NULL);
2020 drv = reopen_state->bs->drv;
2022 /* Process generic block layer options */
2023 opts = qemu_opts_create(&bdrv_runtime_opts, NULL, 0, &error_abort);
2024 qemu_opts_absorb_qdict(opts, reopen_state->options, &local_err);
2025 if (local_err) {
2026 error_propagate(errp, local_err);
2027 ret = -EINVAL;
2028 goto error;
2031 update_flags_from_options(&reopen_state->flags, opts);
2033 /* node-name and driver must be unchanged. Put them back into the QDict, so
2034 * that they are checked at the end of this function. */
2035 value = qemu_opt_get(opts, "node-name");
2036 if (value) {
2037 qdict_put(reopen_state->options, "node-name", qstring_from_str(value));
2040 value = qemu_opt_get(opts, "driver");
2041 if (value) {
2042 qdict_put(reopen_state->options, "driver", qstring_from_str(value));
2045 /* if we are to stay read-only, do not allow permission change
2046 * to r/w */
2047 if (!(reopen_state->bs->open_flags & BDRV_O_ALLOW_RDWR) &&
2048 reopen_state->flags & BDRV_O_RDWR) {
2049 error_setg(errp, "Node '%s' is read only",
2050 bdrv_get_device_or_node_name(reopen_state->bs));
2051 goto error;
2055 ret = bdrv_flush(reopen_state->bs);
2056 if (ret) {
2057 error_setg_errno(errp, -ret, "Error flushing drive");
2058 goto error;
2061 if (drv->bdrv_reopen_prepare) {
2062 ret = drv->bdrv_reopen_prepare(reopen_state, queue, &local_err);
2063 if (ret) {
2064 if (local_err != NULL) {
2065 error_propagate(errp, local_err);
2066 } else {
2067 error_setg(errp, "failed while preparing to reopen image '%s'",
2068 reopen_state->bs->filename);
2070 goto error;
2072 } else {
2073 /* It is currently mandatory to have a bdrv_reopen_prepare()
2074 * handler for each supported drv. */
2075 error_setg(errp, "Block format '%s' used by node '%s' "
2076 "does not support reopening files", drv->format_name,
2077 bdrv_get_device_or_node_name(reopen_state->bs));
2078 ret = -1;
2079 goto error;
2082 /* Options that are not handled are only okay if they are unchanged
2083 * compared to the old state. It is expected that some options are only
2084 * used for the initial open, but not reopen (e.g. filename) */
2085 if (qdict_size(reopen_state->options)) {
2086 const QDictEntry *entry = qdict_first(reopen_state->options);
2088 do {
2089 QString *new_obj = qobject_to_qstring(entry->value);
2090 const char *new = qstring_get_str(new_obj);
2091 const char *old = qdict_get_try_str(reopen_state->bs->options,
2092 entry->key);
2094 if (!old || strcmp(new, old)) {
2095 error_setg(errp, "Cannot change the option '%s'", entry->key);
2096 ret = -EINVAL;
2097 goto error;
2099 } while ((entry = qdict_next(reopen_state->options, entry)));
2102 ret = 0;
2104 error:
2105 qemu_opts_del(opts);
2106 return ret;
2110 * Takes the staged changes for the reopen from bdrv_reopen_prepare(), and
2111 * makes them final by swapping the staging BlockDriverState contents into
2112 * the active BlockDriverState contents.
2114 void bdrv_reopen_commit(BDRVReopenState *reopen_state)
2116 BlockDriver *drv;
2118 assert(reopen_state != NULL);
2119 drv = reopen_state->bs->drv;
2120 assert(drv != NULL);
2122 /* If there are any driver level actions to take */
2123 if (drv->bdrv_reopen_commit) {
2124 drv->bdrv_reopen_commit(reopen_state);
2127 /* set BDS specific flags now */
2128 QDECREF(reopen_state->bs->explicit_options);
2130 reopen_state->bs->explicit_options = reopen_state->explicit_options;
2131 reopen_state->bs->open_flags = reopen_state->flags;
2132 reopen_state->bs->read_only = !(reopen_state->flags & BDRV_O_RDWR);
2134 bdrv_refresh_limits(reopen_state->bs, NULL);
2138 * Abort the reopen, and delete and free the staged changes in
2139 * reopen_state
2141 void bdrv_reopen_abort(BDRVReopenState *reopen_state)
2143 BlockDriver *drv;
2145 assert(reopen_state != NULL);
2146 drv = reopen_state->bs->drv;
2147 assert(drv != NULL);
2149 if (drv->bdrv_reopen_abort) {
2150 drv->bdrv_reopen_abort(reopen_state);
2153 QDECREF(reopen_state->explicit_options);
2157 static void bdrv_close(BlockDriverState *bs)
2159 BdrvAioNotifier *ban, *ban_next;
2161 assert(!bs->job);
2163 bdrv_drained_begin(bs); /* complete I/O */
2164 bdrv_flush(bs);
2165 bdrv_drain(bs); /* in case flush left pending I/O */
2167 bdrv_release_named_dirty_bitmaps(bs);
2168 assert(QLIST_EMPTY(&bs->dirty_bitmaps));
2170 bdrv_parent_cb_change_media(bs, false);
2172 if (bs->drv) {
2173 BdrvChild *child, *next;
2175 bs->drv->bdrv_close(bs);
2176 bs->drv = NULL;
2178 bdrv_set_backing_hd(bs, NULL);
2180 if (bs->file != NULL) {
2181 bdrv_unref_child(bs, bs->file);
2182 bs->file = NULL;
2185 QLIST_FOREACH_SAFE(child, &bs->children, next, next) {
2186 /* TODO Remove bdrv_unref() from drivers' close function and use
2187 * bdrv_unref_child() here */
2188 if (child->bs->inherits_from == bs) {
2189 child->bs->inherits_from = NULL;
2191 bdrv_detach_child(child);
2194 g_free(bs->opaque);
2195 bs->opaque = NULL;
2196 bs->copy_on_read = 0;
2197 bs->backing_file[0] = '\0';
2198 bs->backing_format[0] = '\0';
2199 bs->total_sectors = 0;
2200 bs->encrypted = 0;
2201 bs->valid_key = 0;
2202 bs->sg = 0;
2203 bs->zero_beyond_eof = false;
2204 QDECREF(bs->options);
2205 QDECREF(bs->explicit_options);
2206 bs->options = NULL;
2207 QDECREF(bs->full_open_options);
2208 bs->full_open_options = NULL;
2211 QLIST_FOREACH_SAFE(ban, &bs->aio_notifiers, list, ban_next) {
2212 g_free(ban);
2214 QLIST_INIT(&bs->aio_notifiers);
2215 bdrv_drained_end(bs);
2218 void bdrv_close_all(void)
2220 BlockDriverState *bs;
2221 AioContext *aio_context;
2223 /* Drop references from requests still in flight, such as canceled block
2224 * jobs whose AIO context has not been polled yet */
2225 bdrv_drain_all();
2227 blk_remove_all_bs();
2228 blockdev_close_all_bdrv_states();
2230 /* Cancel all block jobs */
2231 while (!QTAILQ_EMPTY(&all_bdrv_states)) {
2232 QTAILQ_FOREACH(bs, &all_bdrv_states, bs_list) {
2233 aio_context = bdrv_get_aio_context(bs);
2235 aio_context_acquire(aio_context);
2236 if (bs->job) {
2237 block_job_cancel_sync(bs->job);
2238 aio_context_release(aio_context);
2239 break;
2241 aio_context_release(aio_context);
2244 /* All the remaining BlockDriverStates are referenced directly or
2245 * indirectly from block jobs, so there needs to be at least one BDS
2246 * directly used by a block job */
2247 assert(bs);
2251 static void change_parent_backing_link(BlockDriverState *from,
2252 BlockDriverState *to)
2254 BdrvChild *c, *next;
2256 QLIST_FOREACH_SAFE(c, &from->parents, next_parent, next) {
2257 assert(c->role != &child_backing);
2258 c->bs = to;
2259 QLIST_REMOVE(c, next_parent);
2260 QLIST_INSERT_HEAD(&to->parents, c, next_parent);
2261 bdrv_ref(to);
2262 bdrv_unref(from);
2267 * Add new bs contents at the top of an image chain while the chain is
2268 * live, while keeping required fields on the top layer.
2270 * This will modify the BlockDriverState fields, and swap contents
2271 * between bs_new and bs_top. Both bs_new and bs_top are modified.
2273 * bs_new must not be attached to a BlockBackend.
2275 * This function does not create any image files.
2277 * bdrv_append() takes ownership of a bs_new reference and unrefs it because
2278 * that's what the callers commonly need. bs_new will be referenced by the old
2279 * parents of bs_top after bdrv_append() returns. If the caller needs to keep a
2280 * reference of its own, it must call bdrv_ref().
2282 void bdrv_append(BlockDriverState *bs_new, BlockDriverState *bs_top)
2284 assert(!bdrv_requests_pending(bs_top));
2285 assert(!bdrv_requests_pending(bs_new));
2287 bdrv_ref(bs_top);
2289 change_parent_backing_link(bs_top, bs_new);
2290 bdrv_set_backing_hd(bs_new, bs_top);
2291 bdrv_unref(bs_top);
2293 /* bs_new is now referenced by its new parents, we don't need the
2294 * additional reference any more. */
2295 bdrv_unref(bs_new);
2298 void bdrv_replace_in_backing_chain(BlockDriverState *old, BlockDriverState *new)
2300 assert(!bdrv_requests_pending(old));
2301 assert(!bdrv_requests_pending(new));
2303 bdrv_ref(old);
2305 change_parent_backing_link(old, new);
2307 /* Change backing files if a previously independent node is added to the
2308 * chain. For active commit, we replace top by its own (indirect) backing
2309 * file and don't do anything here so we don't build a loop. */
2310 if (new->backing == NULL && !bdrv_chain_contains(backing_bs(old), new)) {
2311 bdrv_set_backing_hd(new, backing_bs(old));
2312 bdrv_set_backing_hd(old, NULL);
2315 bdrv_unref(old);
2318 static void bdrv_delete(BlockDriverState *bs)
2320 assert(!bs->job);
2321 assert(bdrv_op_blocker_is_empty(bs));
2322 assert(!bs->refcnt);
2324 bdrv_close(bs);
2326 /* remove from list, if necessary */
2327 if (bs->node_name[0] != '\0') {
2328 QTAILQ_REMOVE(&graph_bdrv_states, bs, node_list);
2330 QTAILQ_REMOVE(&all_bdrv_states, bs, bs_list);
2332 g_free(bs);
2336 * Run consistency checks on an image
2338 * Returns 0 if the check could be completed (it doesn't mean that the image is
2339 * free of errors) or -errno when an internal error occurred. The results of the
2340 * check are stored in res.
2342 int bdrv_check(BlockDriverState *bs, BdrvCheckResult *res, BdrvCheckMode fix)
2344 if (bs->drv == NULL) {
2345 return -ENOMEDIUM;
2347 if (bs->drv->bdrv_check == NULL) {
2348 return -ENOTSUP;
2351 memset(res, 0, sizeof(*res));
2352 return bs->drv->bdrv_check(bs, res, fix);
2355 #define COMMIT_BUF_SECTORS 2048
2357 /* commit COW file into the raw image */
2358 int bdrv_commit(BlockDriverState *bs)
2360 BlockDriver *drv = bs->drv;
2361 int64_t sector, total_sectors, length, backing_length;
2362 int n, ro, open_flags;
2363 int ret = 0;
2364 uint8_t *buf = NULL;
2366 if (!drv)
2367 return -ENOMEDIUM;
2369 if (!bs->backing) {
2370 return -ENOTSUP;
2373 if (bdrv_op_is_blocked(bs, BLOCK_OP_TYPE_COMMIT_SOURCE, NULL) ||
2374 bdrv_op_is_blocked(bs->backing->bs, BLOCK_OP_TYPE_COMMIT_TARGET, NULL)) {
2375 return -EBUSY;
2378 ro = bs->backing->bs->read_only;
2379 open_flags = bs->backing->bs->open_flags;
2381 if (ro) {
2382 if (bdrv_reopen(bs->backing->bs, open_flags | BDRV_O_RDWR, NULL)) {
2383 return -EACCES;
2387 length = bdrv_getlength(bs);
2388 if (length < 0) {
2389 ret = length;
2390 goto ro_cleanup;
2393 backing_length = bdrv_getlength(bs->backing->bs);
2394 if (backing_length < 0) {
2395 ret = backing_length;
2396 goto ro_cleanup;
2399 /* If our top snapshot is larger than the backing file image,
2400 * grow the backing file image if possible. If not possible,
2401 * we must return an error */
2402 if (length > backing_length) {
2403 ret = bdrv_truncate(bs->backing->bs, length);
2404 if (ret < 0) {
2405 goto ro_cleanup;
2409 total_sectors = length >> BDRV_SECTOR_BITS;
2411 /* qemu_try_blockalign() for bs will choose an alignment that works for
2412 * bs->backing->bs as well, so no need to compare the alignment manually. */
2413 buf = qemu_try_blockalign(bs, COMMIT_BUF_SECTORS * BDRV_SECTOR_SIZE);
2414 if (buf == NULL) {
2415 ret = -ENOMEM;
2416 goto ro_cleanup;
2419 for (sector = 0; sector < total_sectors; sector += n) {
2420 ret = bdrv_is_allocated(bs, sector, COMMIT_BUF_SECTORS, &n);
2421 if (ret < 0) {
2422 goto ro_cleanup;
2424 if (ret) {
2425 ret = bdrv_read(bs, sector, buf, n);
2426 if (ret < 0) {
2427 goto ro_cleanup;
2430 ret = bdrv_write(bs->backing->bs, sector, buf, n);
2431 if (ret < 0) {
2432 goto ro_cleanup;
2437 if (drv->bdrv_make_empty) {
2438 ret = drv->bdrv_make_empty(bs);
2439 if (ret < 0) {
2440 goto ro_cleanup;
2442 bdrv_flush(bs);
2446 * Make sure all data we wrote to the backing device is actually
2447 * stable on disk.
2449 if (bs->backing) {
2450 bdrv_flush(bs->backing->bs);
2453 ret = 0;
2454 ro_cleanup:
2455 qemu_vfree(buf);
2457 if (ro) {
2458 /* ignoring error return here */
2459 bdrv_reopen(bs->backing->bs, open_flags & ~BDRV_O_RDWR, NULL);
2462 return ret;
2466 * Return values:
2467 * 0 - success
2468 * -EINVAL - backing format specified, but no file
2469 * -ENOSPC - can't update the backing file because no space is left in the
2470 * image file header
2471 * -ENOTSUP - format driver doesn't support changing the backing file
2473 int bdrv_change_backing_file(BlockDriverState *bs,
2474 const char *backing_file, const char *backing_fmt)
2476 BlockDriver *drv = bs->drv;
2477 int ret;
2479 /* Backing file format doesn't make sense without a backing file */
2480 if (backing_fmt && !backing_file) {
2481 return -EINVAL;
2484 if (drv->bdrv_change_backing_file != NULL) {
2485 ret = drv->bdrv_change_backing_file(bs, backing_file, backing_fmt);
2486 } else {
2487 ret = -ENOTSUP;
2490 if (ret == 0) {
2491 pstrcpy(bs->backing_file, sizeof(bs->backing_file), backing_file ?: "");
2492 pstrcpy(bs->backing_format, sizeof(bs->backing_format), backing_fmt ?: "");
2494 return ret;
2498 * Finds the image layer in the chain that has 'bs' as its backing file.
2500 * active is the current topmost image.
2502 * Returns NULL if bs is not found in active's image chain,
2503 * or if active == bs.
2505 * Returns the bottommost base image if bs == NULL.
2507 BlockDriverState *bdrv_find_overlay(BlockDriverState *active,
2508 BlockDriverState *bs)
2510 while (active && bs != backing_bs(active)) {
2511 active = backing_bs(active);
2514 return active;
2517 /* Given a BDS, searches for the base layer. */
2518 BlockDriverState *bdrv_find_base(BlockDriverState *bs)
2520 return bdrv_find_overlay(bs, NULL);
2524 * Drops images above 'base' up to and including 'top', and sets the image
2525 * above 'top' to have base as its backing file.
2527 * Requires that the overlay to 'top' is opened r/w, so that the backing file
2528 * information in 'bs' can be properly updated.
2530 * E.g., this will convert the following chain:
2531 * bottom <- base <- intermediate <- top <- active
2533 * to
2535 * bottom <- base <- active
2537 * It is allowed for bottom==base, in which case it converts:
2539 * base <- intermediate <- top <- active
2541 * to
2543 * base <- active
2545 * If backing_file_str is non-NULL, it will be used when modifying top's
2546 * overlay image metadata.
2548 * Error conditions:
2549 * if active == top, that is considered an error
2552 int bdrv_drop_intermediate(BlockDriverState *active, BlockDriverState *top,
2553 BlockDriverState *base, const char *backing_file_str)
2555 BlockDriverState *new_top_bs = NULL;
2556 int ret = -EIO;
2558 if (!top->drv || !base->drv) {
2559 goto exit;
2562 new_top_bs = bdrv_find_overlay(active, top);
2564 if (new_top_bs == NULL) {
2565 /* we could not find the image above 'top', this is an error */
2566 goto exit;
2569 /* special case of new_top_bs->backing->bs already pointing to base - nothing
2570 * to do, no intermediate images */
2571 if (backing_bs(new_top_bs) == base) {
2572 ret = 0;
2573 goto exit;
2576 /* Make sure that base is in the backing chain of top */
2577 if (!bdrv_chain_contains(top, base)) {
2578 goto exit;
2581 /* success - we can delete the intermediate states, and link top->base */
2582 backing_file_str = backing_file_str ? backing_file_str : base->filename;
2583 ret = bdrv_change_backing_file(new_top_bs, backing_file_str,
2584 base->drv ? base->drv->format_name : "");
2585 if (ret) {
2586 goto exit;
2588 bdrv_set_backing_hd(new_top_bs, base);
2590 ret = 0;
2591 exit:
2592 return ret;
2596 * Truncate file to 'offset' bytes (needed only for file protocols)
2598 int bdrv_truncate(BlockDriverState *bs, int64_t offset)
2600 BlockDriver *drv = bs->drv;
2601 int ret;
2602 if (!drv)
2603 return -ENOMEDIUM;
2604 if (!drv->bdrv_truncate)
2605 return -ENOTSUP;
2606 if (bs->read_only)
2607 return -EACCES;
2609 ret = drv->bdrv_truncate(bs, offset);
2610 if (ret == 0) {
2611 ret = refresh_total_sectors(bs, offset >> BDRV_SECTOR_BITS);
2612 bdrv_dirty_bitmap_truncate(bs);
2613 bdrv_parent_cb_resize(bs);
2615 return ret;
2619 * Length of a allocated file in bytes. Sparse files are counted by actual
2620 * allocated space. Return < 0 if error or unknown.
2622 int64_t bdrv_get_allocated_file_size(BlockDriverState *bs)
2624 BlockDriver *drv = bs->drv;
2625 if (!drv) {
2626 return -ENOMEDIUM;
2628 if (drv->bdrv_get_allocated_file_size) {
2629 return drv->bdrv_get_allocated_file_size(bs);
2631 if (bs->file) {
2632 return bdrv_get_allocated_file_size(bs->file->bs);
2634 return -ENOTSUP;
2638 * Return number of sectors on success, -errno on error.
2640 int64_t bdrv_nb_sectors(BlockDriverState *bs)
2642 BlockDriver *drv = bs->drv;
2644 if (!drv)
2645 return -ENOMEDIUM;
2647 if (drv->has_variable_length) {
2648 int ret = refresh_total_sectors(bs, bs->total_sectors);
2649 if (ret < 0) {
2650 return ret;
2653 return bs->total_sectors;
2657 * Return length in bytes on success, -errno on error.
2658 * The length is always a multiple of BDRV_SECTOR_SIZE.
2660 int64_t bdrv_getlength(BlockDriverState *bs)
2662 int64_t ret = bdrv_nb_sectors(bs);
2664 ret = ret > INT64_MAX / BDRV_SECTOR_SIZE ? -EFBIG : ret;
2665 return ret < 0 ? ret : ret * BDRV_SECTOR_SIZE;
2668 /* return 0 as number of sectors if no device present or error */
2669 void bdrv_get_geometry(BlockDriverState *bs, uint64_t *nb_sectors_ptr)
2671 int64_t nb_sectors = bdrv_nb_sectors(bs);
2673 *nb_sectors_ptr = nb_sectors < 0 ? 0 : nb_sectors;
2676 int bdrv_is_read_only(BlockDriverState *bs)
2678 return bs->read_only;
2681 int bdrv_is_sg(BlockDriverState *bs)
2683 return bs->sg;
2686 int bdrv_is_encrypted(BlockDriverState *bs)
2688 if (bs->backing && bs->backing->bs->encrypted) {
2689 return 1;
2691 return bs->encrypted;
2694 int bdrv_key_required(BlockDriverState *bs)
2696 BdrvChild *backing = bs->backing;
2698 if (backing && backing->bs->encrypted && !backing->bs->valid_key) {
2699 return 1;
2701 return (bs->encrypted && !bs->valid_key);
2704 int bdrv_set_key(BlockDriverState *bs, const char *key)
2706 int ret;
2707 if (bs->backing && bs->backing->bs->encrypted) {
2708 ret = bdrv_set_key(bs->backing->bs, key);
2709 if (ret < 0)
2710 return ret;
2711 if (!bs->encrypted)
2712 return 0;
2714 if (!bs->encrypted) {
2715 return -EINVAL;
2716 } else if (!bs->drv || !bs->drv->bdrv_set_key) {
2717 return -ENOMEDIUM;
2719 ret = bs->drv->bdrv_set_key(bs, key);
2720 if (ret < 0) {
2721 bs->valid_key = 0;
2722 } else if (!bs->valid_key) {
2723 /* call the change callback now, we skipped it on open */
2724 bs->valid_key = 1;
2725 bdrv_parent_cb_change_media(bs, true);
2727 return ret;
2731 * Provide an encryption key for @bs.
2732 * If @key is non-null:
2733 * If @bs is not encrypted, fail.
2734 * Else if the key is invalid, fail.
2735 * Else set @bs's key to @key, replacing the existing key, if any.
2736 * If @key is null:
2737 * If @bs is encrypted and still lacks a key, fail.
2738 * Else do nothing.
2739 * On failure, store an error object through @errp if non-null.
2741 void bdrv_add_key(BlockDriverState *bs, const char *key, Error **errp)
2743 if (key) {
2744 if (!bdrv_is_encrypted(bs)) {
2745 error_setg(errp, "Node '%s' is not encrypted",
2746 bdrv_get_device_or_node_name(bs));
2747 } else if (bdrv_set_key(bs, key) < 0) {
2748 error_setg(errp, QERR_INVALID_PASSWORD);
2750 } else {
2751 if (bdrv_key_required(bs)) {
2752 error_set(errp, ERROR_CLASS_DEVICE_ENCRYPTED,
2753 "'%s' (%s) is encrypted",
2754 bdrv_get_device_or_node_name(bs),
2755 bdrv_get_encrypted_filename(bs));
2760 const char *bdrv_get_format_name(BlockDriverState *bs)
2762 return bs->drv ? bs->drv->format_name : NULL;
2765 static int qsort_strcmp(const void *a, const void *b)
2767 return strcmp(a, b);
2770 void bdrv_iterate_format(void (*it)(void *opaque, const char *name),
2771 void *opaque)
2773 BlockDriver *drv;
2774 int count = 0;
2775 int i;
2776 const char **formats = NULL;
2778 QLIST_FOREACH(drv, &bdrv_drivers, list) {
2779 if (drv->format_name) {
2780 bool found = false;
2781 int i = count;
2782 while (formats && i && !found) {
2783 found = !strcmp(formats[--i], drv->format_name);
2786 if (!found) {
2787 formats = g_renew(const char *, formats, count + 1);
2788 formats[count++] = drv->format_name;
2793 qsort(formats, count, sizeof(formats[0]), qsort_strcmp);
2795 for (i = 0; i < count; i++) {
2796 it(opaque, formats[i]);
2799 g_free(formats);
2802 /* This function is to find a node in the bs graph */
2803 BlockDriverState *bdrv_find_node(const char *node_name)
2805 BlockDriverState *bs;
2807 assert(node_name);
2809 QTAILQ_FOREACH(bs, &graph_bdrv_states, node_list) {
2810 if (!strcmp(node_name, bs->node_name)) {
2811 return bs;
2814 return NULL;
2817 /* Put this QMP function here so it can access the static graph_bdrv_states. */
2818 BlockDeviceInfoList *bdrv_named_nodes_list(Error **errp)
2820 BlockDeviceInfoList *list, *entry;
2821 BlockDriverState *bs;
2823 list = NULL;
2824 QTAILQ_FOREACH(bs, &graph_bdrv_states, node_list) {
2825 BlockDeviceInfo *info = bdrv_block_device_info(NULL, bs, errp);
2826 if (!info) {
2827 qapi_free_BlockDeviceInfoList(list);
2828 return NULL;
2830 entry = g_malloc0(sizeof(*entry));
2831 entry->value = info;
2832 entry->next = list;
2833 list = entry;
2836 return list;
2839 BlockDriverState *bdrv_lookup_bs(const char *device,
2840 const char *node_name,
2841 Error **errp)
2843 BlockBackend *blk;
2844 BlockDriverState *bs;
2846 if (device) {
2847 blk = blk_by_name(device);
2849 if (blk) {
2850 bs = blk_bs(blk);
2851 if (!bs) {
2852 error_setg(errp, "Device '%s' has no medium", device);
2855 return bs;
2859 if (node_name) {
2860 bs = bdrv_find_node(node_name);
2862 if (bs) {
2863 return bs;
2867 error_setg(errp, "Cannot find device=%s nor node_name=%s",
2868 device ? device : "",
2869 node_name ? node_name : "");
2870 return NULL;
2873 /* If 'base' is in the same chain as 'top', return true. Otherwise,
2874 * return false. If either argument is NULL, return false. */
2875 bool bdrv_chain_contains(BlockDriverState *top, BlockDriverState *base)
2877 while (top && top != base) {
2878 top = backing_bs(top);
2881 return top != NULL;
2884 BlockDriverState *bdrv_next_node(BlockDriverState *bs)
2886 if (!bs) {
2887 return QTAILQ_FIRST(&graph_bdrv_states);
2889 return QTAILQ_NEXT(bs, node_list);
2892 const char *bdrv_get_node_name(const BlockDriverState *bs)
2894 return bs->node_name;
2897 const char *bdrv_get_parent_name(const BlockDriverState *bs)
2899 BdrvChild *c;
2900 const char *name;
2902 /* If multiple parents have a name, just pick the first one. */
2903 QLIST_FOREACH(c, &bs->parents, next_parent) {
2904 if (c->role->get_name) {
2905 name = c->role->get_name(c);
2906 if (name && *name) {
2907 return name;
2912 return NULL;
2915 /* TODO check what callers really want: bs->node_name or blk_name() */
2916 const char *bdrv_get_device_name(const BlockDriverState *bs)
2918 return bdrv_get_parent_name(bs) ?: "";
2921 /* This can be used to identify nodes that might not have a device
2922 * name associated. Since node and device names live in the same
2923 * namespace, the result is unambiguous. The exception is if both are
2924 * absent, then this returns an empty (non-null) string. */
2925 const char *bdrv_get_device_or_node_name(const BlockDriverState *bs)
2927 return bdrv_get_parent_name(bs) ?: bs->node_name;
2930 int bdrv_get_flags(BlockDriverState *bs)
2932 return bs->open_flags;
2935 int bdrv_has_zero_init_1(BlockDriverState *bs)
2937 return 1;
2940 int bdrv_has_zero_init(BlockDriverState *bs)
2942 assert(bs->drv);
2944 /* If BS is a copy on write image, it is initialized to
2945 the contents of the base image, which may not be zeroes. */
2946 if (bs->backing) {
2947 return 0;
2949 if (bs->drv->bdrv_has_zero_init) {
2950 return bs->drv->bdrv_has_zero_init(bs);
2953 /* safe default */
2954 return 0;
2957 bool bdrv_unallocated_blocks_are_zero(BlockDriverState *bs)
2959 BlockDriverInfo bdi;
2961 if (bs->backing) {
2962 return false;
2965 if (bdrv_get_info(bs, &bdi) == 0) {
2966 return bdi.unallocated_blocks_are_zero;
2969 return false;
2972 bool bdrv_can_write_zeroes_with_unmap(BlockDriverState *bs)
2974 BlockDriverInfo bdi;
2976 if (bs->backing || !(bs->open_flags & BDRV_O_UNMAP)) {
2977 return false;
2980 if (bdrv_get_info(bs, &bdi) == 0) {
2981 return bdi.can_write_zeroes_with_unmap;
2984 return false;
2987 const char *bdrv_get_encrypted_filename(BlockDriverState *bs)
2989 if (bs->backing && bs->backing->bs->encrypted)
2990 return bs->backing_file;
2991 else if (bs->encrypted)
2992 return bs->filename;
2993 else
2994 return NULL;
2997 void bdrv_get_backing_filename(BlockDriverState *bs,
2998 char *filename, int filename_size)
3000 pstrcpy(filename, filename_size, bs->backing_file);
3003 int bdrv_get_info(BlockDriverState *bs, BlockDriverInfo *bdi)
3005 BlockDriver *drv = bs->drv;
3006 if (!drv)
3007 return -ENOMEDIUM;
3008 if (!drv->bdrv_get_info)
3009 return -ENOTSUP;
3010 memset(bdi, 0, sizeof(*bdi));
3011 return drv->bdrv_get_info(bs, bdi);
3014 ImageInfoSpecific *bdrv_get_specific_info(BlockDriverState *bs)
3016 BlockDriver *drv = bs->drv;
3017 if (drv && drv->bdrv_get_specific_info) {
3018 return drv->bdrv_get_specific_info(bs);
3020 return NULL;
3023 void bdrv_debug_event(BlockDriverState *bs, BlkdebugEvent event)
3025 if (!bs || !bs->drv || !bs->drv->bdrv_debug_event) {
3026 return;
3029 bs->drv->bdrv_debug_event(bs, event);
3032 int bdrv_debug_breakpoint(BlockDriverState *bs, const char *event,
3033 const char *tag)
3035 while (bs && bs->drv && !bs->drv->bdrv_debug_breakpoint) {
3036 bs = bs->file ? bs->file->bs : NULL;
3039 if (bs && bs->drv && bs->drv->bdrv_debug_breakpoint) {
3040 return bs->drv->bdrv_debug_breakpoint(bs, event, tag);
3043 return -ENOTSUP;
3046 int bdrv_debug_remove_breakpoint(BlockDriverState *bs, const char *tag)
3048 while (bs && bs->drv && !bs->drv->bdrv_debug_remove_breakpoint) {
3049 bs = bs->file ? bs->file->bs : NULL;
3052 if (bs && bs->drv && bs->drv->bdrv_debug_remove_breakpoint) {
3053 return bs->drv->bdrv_debug_remove_breakpoint(bs, tag);
3056 return -ENOTSUP;
3059 int bdrv_debug_resume(BlockDriverState *bs, const char *tag)
3061 while (bs && (!bs->drv || !bs->drv->bdrv_debug_resume)) {
3062 bs = bs->file ? bs->file->bs : NULL;
3065 if (bs && bs->drv && bs->drv->bdrv_debug_resume) {
3066 return bs->drv->bdrv_debug_resume(bs, tag);
3069 return -ENOTSUP;
3072 bool bdrv_debug_is_suspended(BlockDriverState *bs, const char *tag)
3074 while (bs && bs->drv && !bs->drv->bdrv_debug_is_suspended) {
3075 bs = bs->file ? bs->file->bs : NULL;
3078 if (bs && bs->drv && bs->drv->bdrv_debug_is_suspended) {
3079 return bs->drv->bdrv_debug_is_suspended(bs, tag);
3082 return false;
3085 int bdrv_is_snapshot(BlockDriverState *bs)
3087 return !!(bs->open_flags & BDRV_O_SNAPSHOT);
3090 /* backing_file can either be relative, or absolute, or a protocol. If it is
3091 * relative, it must be relative to the chain. So, passing in bs->filename
3092 * from a BDS as backing_file should not be done, as that may be relative to
3093 * the CWD rather than the chain. */
3094 BlockDriverState *bdrv_find_backing_image(BlockDriverState *bs,
3095 const char *backing_file)
3097 char *filename_full = NULL;
3098 char *backing_file_full = NULL;
3099 char *filename_tmp = NULL;
3100 int is_protocol = 0;
3101 BlockDriverState *curr_bs = NULL;
3102 BlockDriverState *retval = NULL;
3104 if (!bs || !bs->drv || !backing_file) {
3105 return NULL;
3108 filename_full = g_malloc(PATH_MAX);
3109 backing_file_full = g_malloc(PATH_MAX);
3110 filename_tmp = g_malloc(PATH_MAX);
3112 is_protocol = path_has_protocol(backing_file);
3114 for (curr_bs = bs; curr_bs->backing; curr_bs = curr_bs->backing->bs) {
3116 /* If either of the filename paths is actually a protocol, then
3117 * compare unmodified paths; otherwise make paths relative */
3118 if (is_protocol || path_has_protocol(curr_bs->backing_file)) {
3119 if (strcmp(backing_file, curr_bs->backing_file) == 0) {
3120 retval = curr_bs->backing->bs;
3121 break;
3123 } else {
3124 /* If not an absolute filename path, make it relative to the current
3125 * image's filename path */
3126 path_combine(filename_tmp, PATH_MAX, curr_bs->filename,
3127 backing_file);
3129 /* We are going to compare absolute pathnames */
3130 if (!realpath(filename_tmp, filename_full)) {
3131 continue;
3134 /* We need to make sure the backing filename we are comparing against
3135 * is relative to the current image filename (or absolute) */
3136 path_combine(filename_tmp, PATH_MAX, curr_bs->filename,
3137 curr_bs->backing_file);
3139 if (!realpath(filename_tmp, backing_file_full)) {
3140 continue;
3143 if (strcmp(backing_file_full, filename_full) == 0) {
3144 retval = curr_bs->backing->bs;
3145 break;
3150 g_free(filename_full);
3151 g_free(backing_file_full);
3152 g_free(filename_tmp);
3153 return retval;
3156 int bdrv_get_backing_file_depth(BlockDriverState *bs)
3158 if (!bs->drv) {
3159 return 0;
3162 if (!bs->backing) {
3163 return 0;
3166 return 1 + bdrv_get_backing_file_depth(bs->backing->bs);
3169 void bdrv_init(void)
3171 module_call_init(MODULE_INIT_BLOCK);
3174 void bdrv_init_with_whitelist(void)
3176 use_bdrv_whitelist = 1;
3177 bdrv_init();
3180 void bdrv_invalidate_cache(BlockDriverState *bs, Error **errp)
3182 BdrvChild *child;
3183 Error *local_err = NULL;
3184 int ret;
3186 if (!bs->drv) {
3187 return;
3190 if (!(bs->open_flags & BDRV_O_INACTIVE)) {
3191 return;
3193 bs->open_flags &= ~BDRV_O_INACTIVE;
3195 if (bs->drv->bdrv_invalidate_cache) {
3196 bs->drv->bdrv_invalidate_cache(bs, &local_err);
3197 if (local_err) {
3198 bs->open_flags |= BDRV_O_INACTIVE;
3199 error_propagate(errp, local_err);
3200 return;
3204 QLIST_FOREACH(child, &bs->children, next) {
3205 bdrv_invalidate_cache(child->bs, &local_err);
3206 if (local_err) {
3207 bs->open_flags |= BDRV_O_INACTIVE;
3208 error_propagate(errp, local_err);
3209 return;
3213 ret = refresh_total_sectors(bs, bs->total_sectors);
3214 if (ret < 0) {
3215 bs->open_flags |= BDRV_O_INACTIVE;
3216 error_setg_errno(errp, -ret, "Could not refresh total sector count");
3217 return;
3221 void bdrv_invalidate_cache_all(Error **errp)
3223 BlockDriverState *bs;
3224 Error *local_err = NULL;
3225 BdrvNextIterator it;
3227 for (bs = bdrv_first(&it); bs; bs = bdrv_next(&it)) {
3228 AioContext *aio_context = bdrv_get_aio_context(bs);
3230 aio_context_acquire(aio_context);
3231 bdrv_invalidate_cache(bs, &local_err);
3232 aio_context_release(aio_context);
3233 if (local_err) {
3234 error_propagate(errp, local_err);
3235 return;
3240 static int bdrv_inactivate_recurse(BlockDriverState *bs,
3241 bool setting_flag)
3243 BdrvChild *child;
3244 int ret;
3246 if (!setting_flag && bs->drv->bdrv_inactivate) {
3247 ret = bs->drv->bdrv_inactivate(bs);
3248 if (ret < 0) {
3249 return ret;
3253 QLIST_FOREACH(child, &bs->children, next) {
3254 ret = bdrv_inactivate_recurse(child->bs, setting_flag);
3255 if (ret < 0) {
3256 return ret;
3260 if (setting_flag) {
3261 bs->open_flags |= BDRV_O_INACTIVE;
3263 return 0;
3266 int bdrv_inactivate_all(void)
3268 BlockDriverState *bs = NULL;
3269 BdrvNextIterator it;
3270 int ret = 0;
3271 int pass;
3273 for (bs = bdrv_first(&it); bs; bs = bdrv_next(&it)) {
3274 aio_context_acquire(bdrv_get_aio_context(bs));
3277 /* We do two passes of inactivation. The first pass calls to drivers'
3278 * .bdrv_inactivate callbacks recursively so all cache is flushed to disk;
3279 * the second pass sets the BDRV_O_INACTIVE flag so that no further write
3280 * is allowed. */
3281 for (pass = 0; pass < 2; pass++) {
3282 for (bs = bdrv_first(&it); bs; bs = bdrv_next(&it)) {
3283 ret = bdrv_inactivate_recurse(bs, pass);
3284 if (ret < 0) {
3285 goto out;
3290 out:
3291 for (bs = bdrv_first(&it); bs; bs = bdrv_next(&it)) {
3292 aio_context_release(bdrv_get_aio_context(bs));
3295 return ret;
3298 /**************************************************************/
3299 /* removable device support */
3302 * Return TRUE if the media is present
3304 bool bdrv_is_inserted(BlockDriverState *bs)
3306 BlockDriver *drv = bs->drv;
3307 BdrvChild *child;
3309 if (!drv) {
3310 return false;
3312 if (drv->bdrv_is_inserted) {
3313 return drv->bdrv_is_inserted(bs);
3315 QLIST_FOREACH(child, &bs->children, next) {
3316 if (!bdrv_is_inserted(child->bs)) {
3317 return false;
3320 return true;
3324 * Return whether the media changed since the last call to this
3325 * function, or -ENOTSUP if we don't know. Most drivers don't know.
3327 int bdrv_media_changed(BlockDriverState *bs)
3329 BlockDriver *drv = bs->drv;
3331 if (drv && drv->bdrv_media_changed) {
3332 return drv->bdrv_media_changed(bs);
3334 return -ENOTSUP;
3338 * If eject_flag is TRUE, eject the media. Otherwise, close the tray
3340 void bdrv_eject(BlockDriverState *bs, bool eject_flag)
3342 BlockDriver *drv = bs->drv;
3343 const char *device_name;
3345 if (drv && drv->bdrv_eject) {
3346 drv->bdrv_eject(bs, eject_flag);
3349 device_name = bdrv_get_device_name(bs);
3350 if (device_name[0] != '\0') {
3351 qapi_event_send_device_tray_moved(device_name,
3352 eject_flag, &error_abort);
3357 * Lock or unlock the media (if it is locked, the user won't be able
3358 * to eject it manually).
3360 void bdrv_lock_medium(BlockDriverState *bs, bool locked)
3362 BlockDriver *drv = bs->drv;
3364 trace_bdrv_lock_medium(bs, locked);
3366 if (drv && drv->bdrv_lock_medium) {
3367 drv->bdrv_lock_medium(bs, locked);
3371 /* Get a reference to bs */
3372 void bdrv_ref(BlockDriverState *bs)
3374 bs->refcnt++;
3377 /* Release a previously grabbed reference to bs.
3378 * If after releasing, reference count is zero, the BlockDriverState is
3379 * deleted. */
3380 void bdrv_unref(BlockDriverState *bs)
3382 if (!bs) {
3383 return;
3385 assert(bs->refcnt > 0);
3386 if (--bs->refcnt == 0) {
3387 bdrv_delete(bs);
3391 struct BdrvOpBlocker {
3392 Error *reason;
3393 QLIST_ENTRY(BdrvOpBlocker) list;
3396 bool bdrv_op_is_blocked(BlockDriverState *bs, BlockOpType op, Error **errp)
3398 BdrvOpBlocker *blocker;
3399 assert((int) op >= 0 && op < BLOCK_OP_TYPE_MAX);
3400 if (!QLIST_EMPTY(&bs->op_blockers[op])) {
3401 blocker = QLIST_FIRST(&bs->op_blockers[op]);
3402 if (errp) {
3403 *errp = error_copy(blocker->reason);
3404 error_prepend(errp, "Node '%s' is busy: ",
3405 bdrv_get_device_or_node_name(bs));
3407 return true;
3409 return false;
3412 void bdrv_op_block(BlockDriverState *bs, BlockOpType op, Error *reason)
3414 BdrvOpBlocker *blocker;
3415 assert((int) op >= 0 && op < BLOCK_OP_TYPE_MAX);
3417 blocker = g_new0(BdrvOpBlocker, 1);
3418 blocker->reason = reason;
3419 QLIST_INSERT_HEAD(&bs->op_blockers[op], blocker, list);
3422 void bdrv_op_unblock(BlockDriverState *bs, BlockOpType op, Error *reason)
3424 BdrvOpBlocker *blocker, *next;
3425 assert((int) op >= 0 && op < BLOCK_OP_TYPE_MAX);
3426 QLIST_FOREACH_SAFE(blocker, &bs->op_blockers[op], list, next) {
3427 if (blocker->reason == reason) {
3428 QLIST_REMOVE(blocker, list);
3429 g_free(blocker);
3434 void bdrv_op_block_all(BlockDriverState *bs, Error *reason)
3436 int i;
3437 for (i = 0; i < BLOCK_OP_TYPE_MAX; i++) {
3438 bdrv_op_block(bs, i, reason);
3442 void bdrv_op_unblock_all(BlockDriverState *bs, Error *reason)
3444 int i;
3445 for (i = 0; i < BLOCK_OP_TYPE_MAX; i++) {
3446 bdrv_op_unblock(bs, i, reason);
3450 bool bdrv_op_blocker_is_empty(BlockDriverState *bs)
3452 int i;
3454 for (i = 0; i < BLOCK_OP_TYPE_MAX; i++) {
3455 if (!QLIST_EMPTY(&bs->op_blockers[i])) {
3456 return false;
3459 return true;
3462 void bdrv_img_create(const char *filename, const char *fmt,
3463 const char *base_filename, const char *base_fmt,
3464 char *options, uint64_t img_size, int flags,
3465 Error **errp, bool quiet)
3467 QemuOptsList *create_opts = NULL;
3468 QemuOpts *opts = NULL;
3469 const char *backing_fmt, *backing_file;
3470 int64_t size;
3471 BlockDriver *drv, *proto_drv;
3472 Error *local_err = NULL;
3473 int ret = 0;
3475 /* Find driver and parse its options */
3476 drv = bdrv_find_format(fmt);
3477 if (!drv) {
3478 error_setg(errp, "Unknown file format '%s'", fmt);
3479 return;
3482 proto_drv = bdrv_find_protocol(filename, true, errp);
3483 if (!proto_drv) {
3484 return;
3487 if (!drv->create_opts) {
3488 error_setg(errp, "Format driver '%s' does not support image creation",
3489 drv->format_name);
3490 return;
3493 if (!proto_drv->create_opts) {
3494 error_setg(errp, "Protocol driver '%s' does not support image creation",
3495 proto_drv->format_name);
3496 return;
3499 create_opts = qemu_opts_append(create_opts, drv->create_opts);
3500 create_opts = qemu_opts_append(create_opts, proto_drv->create_opts);
3502 /* Create parameter list with default values */
3503 opts = qemu_opts_create(create_opts, NULL, 0, &error_abort);
3504 qemu_opt_set_number(opts, BLOCK_OPT_SIZE, img_size, &error_abort);
3506 /* Parse -o options */
3507 if (options) {
3508 qemu_opts_do_parse(opts, options, NULL, &local_err);
3509 if (local_err) {
3510 error_report_err(local_err);
3511 local_err = NULL;
3512 error_setg(errp, "Invalid options for file format '%s'", fmt);
3513 goto out;
3517 if (base_filename) {
3518 qemu_opt_set(opts, BLOCK_OPT_BACKING_FILE, base_filename, &local_err);
3519 if (local_err) {
3520 error_setg(errp, "Backing file not supported for file format '%s'",
3521 fmt);
3522 goto out;
3526 if (base_fmt) {
3527 qemu_opt_set(opts, BLOCK_OPT_BACKING_FMT, base_fmt, &local_err);
3528 if (local_err) {
3529 error_setg(errp, "Backing file format not supported for file "
3530 "format '%s'", fmt);
3531 goto out;
3535 backing_file = qemu_opt_get(opts, BLOCK_OPT_BACKING_FILE);
3536 if (backing_file) {
3537 if (!strcmp(filename, backing_file)) {
3538 error_setg(errp, "Error: Trying to create an image with the "
3539 "same filename as the backing file");
3540 goto out;
3544 backing_fmt = qemu_opt_get(opts, BLOCK_OPT_BACKING_FMT);
3546 // The size for the image must always be specified, with one exception:
3547 // If we are using a backing file, we can obtain the size from there
3548 size = qemu_opt_get_size(opts, BLOCK_OPT_SIZE, 0);
3549 if (size == -1) {
3550 if (backing_file) {
3551 BlockDriverState *bs;
3552 char *full_backing = g_new0(char, PATH_MAX);
3553 int64_t size;
3554 int back_flags;
3555 QDict *backing_options = NULL;
3557 bdrv_get_full_backing_filename_from_filename(filename, backing_file,
3558 full_backing, PATH_MAX,
3559 &local_err);
3560 if (local_err) {
3561 g_free(full_backing);
3562 goto out;
3565 /* backing files always opened read-only */
3566 back_flags = flags;
3567 back_flags &= ~(BDRV_O_RDWR | BDRV_O_SNAPSHOT | BDRV_O_NO_BACKING);
3569 if (backing_fmt) {
3570 backing_options = qdict_new();
3571 qdict_put(backing_options, "driver",
3572 qstring_from_str(backing_fmt));
3575 bs = NULL;
3576 ret = bdrv_open(&bs, full_backing, NULL, backing_options,
3577 back_flags, &local_err);
3578 g_free(full_backing);
3579 if (ret < 0) {
3580 goto out;
3582 size = bdrv_getlength(bs);
3583 if (size < 0) {
3584 error_setg_errno(errp, -size, "Could not get size of '%s'",
3585 backing_file);
3586 bdrv_unref(bs);
3587 goto out;
3590 qemu_opt_set_number(opts, BLOCK_OPT_SIZE, size, &error_abort);
3592 bdrv_unref(bs);
3593 } else {
3594 error_setg(errp, "Image creation needs a size parameter");
3595 goto out;
3599 if (!quiet) {
3600 printf("Formatting '%s', fmt=%s ", filename, fmt);
3601 qemu_opts_print(opts, " ");
3602 puts("");
3605 ret = bdrv_create(drv, filename, opts, &local_err);
3607 if (ret == -EFBIG) {
3608 /* This is generally a better message than whatever the driver would
3609 * deliver (especially because of the cluster_size_hint), since that
3610 * is most probably not much different from "image too large". */
3611 const char *cluster_size_hint = "";
3612 if (qemu_opt_get_size(opts, BLOCK_OPT_CLUSTER_SIZE, 0)) {
3613 cluster_size_hint = " (try using a larger cluster size)";
3615 error_setg(errp, "The image size is too large for file format '%s'"
3616 "%s", fmt, cluster_size_hint);
3617 error_free(local_err);
3618 local_err = NULL;
3621 out:
3622 qemu_opts_del(opts);
3623 qemu_opts_free(create_opts);
3624 if (local_err) {
3625 error_propagate(errp, local_err);
3629 AioContext *bdrv_get_aio_context(BlockDriverState *bs)
3631 return bs->aio_context;
3634 void bdrv_detach_aio_context(BlockDriverState *bs)
3636 BdrvAioNotifier *baf;
3637 BdrvChild *child;
3639 if (!bs->drv) {
3640 return;
3643 QLIST_FOREACH(baf, &bs->aio_notifiers, list) {
3644 baf->detach_aio_context(baf->opaque);
3647 if (bs->drv->bdrv_detach_aio_context) {
3648 bs->drv->bdrv_detach_aio_context(bs);
3650 QLIST_FOREACH(child, &bs->children, next) {
3651 bdrv_detach_aio_context(child->bs);
3654 bs->aio_context = NULL;
3657 void bdrv_attach_aio_context(BlockDriverState *bs,
3658 AioContext *new_context)
3660 BdrvAioNotifier *ban;
3661 BdrvChild *child;
3663 if (!bs->drv) {
3664 return;
3667 bs->aio_context = new_context;
3669 QLIST_FOREACH(child, &bs->children, next) {
3670 bdrv_attach_aio_context(child->bs, new_context);
3672 if (bs->drv->bdrv_attach_aio_context) {
3673 bs->drv->bdrv_attach_aio_context(bs, new_context);
3676 QLIST_FOREACH(ban, &bs->aio_notifiers, list) {
3677 ban->attached_aio_context(new_context, ban->opaque);
3681 void bdrv_set_aio_context(BlockDriverState *bs, AioContext *new_context)
3683 bdrv_drain(bs); /* ensure there are no in-flight requests */
3685 bdrv_detach_aio_context(bs);
3687 /* This function executes in the old AioContext so acquire the new one in
3688 * case it runs in a different thread.
3690 aio_context_acquire(new_context);
3691 bdrv_attach_aio_context(bs, new_context);
3692 aio_context_release(new_context);
3695 void bdrv_add_aio_context_notifier(BlockDriverState *bs,
3696 void (*attached_aio_context)(AioContext *new_context, void *opaque),
3697 void (*detach_aio_context)(void *opaque), void *opaque)
3699 BdrvAioNotifier *ban = g_new(BdrvAioNotifier, 1);
3700 *ban = (BdrvAioNotifier){
3701 .attached_aio_context = attached_aio_context,
3702 .detach_aio_context = detach_aio_context,
3703 .opaque = opaque
3706 QLIST_INSERT_HEAD(&bs->aio_notifiers, ban, list);
3709 void bdrv_remove_aio_context_notifier(BlockDriverState *bs,
3710 void (*attached_aio_context)(AioContext *,
3711 void *),
3712 void (*detach_aio_context)(void *),
3713 void *opaque)
3715 BdrvAioNotifier *ban, *ban_next;
3717 QLIST_FOREACH_SAFE(ban, &bs->aio_notifiers, list, ban_next) {
3718 if (ban->attached_aio_context == attached_aio_context &&
3719 ban->detach_aio_context == detach_aio_context &&
3720 ban->opaque == opaque)
3722 QLIST_REMOVE(ban, list);
3723 g_free(ban);
3725 return;
3729 abort();
3732 int bdrv_amend_options(BlockDriverState *bs, QemuOpts *opts,
3733 BlockDriverAmendStatusCB *status_cb, void *cb_opaque)
3735 if (!bs->drv->bdrv_amend_options) {
3736 return -ENOTSUP;
3738 return bs->drv->bdrv_amend_options(bs, opts, status_cb, cb_opaque);
3741 /* This function will be called by the bdrv_recurse_is_first_non_filter method
3742 * of block filter and by bdrv_is_first_non_filter.
3743 * It is used to test if the given bs is the candidate or recurse more in the
3744 * node graph.
3746 bool bdrv_recurse_is_first_non_filter(BlockDriverState *bs,
3747 BlockDriverState *candidate)
3749 /* return false if basic checks fails */
3750 if (!bs || !bs->drv) {
3751 return false;
3754 /* the code reached a non block filter driver -> check if the bs is
3755 * the same as the candidate. It's the recursion termination condition.
3757 if (!bs->drv->is_filter) {
3758 return bs == candidate;
3760 /* Down this path the driver is a block filter driver */
3762 /* If the block filter recursion method is defined use it to recurse down
3763 * the node graph.
3765 if (bs->drv->bdrv_recurse_is_first_non_filter) {
3766 return bs->drv->bdrv_recurse_is_first_non_filter(bs, candidate);
3769 /* the driver is a block filter but don't allow to recurse -> return false
3771 return false;
3774 /* This function checks if the candidate is the first non filter bs down it's
3775 * bs chain. Since we don't have pointers to parents it explore all bs chains
3776 * from the top. Some filters can choose not to pass down the recursion.
3778 bool bdrv_is_first_non_filter(BlockDriverState *candidate)
3780 BlockDriverState *bs;
3781 BdrvNextIterator it;
3783 /* walk down the bs forest recursively */
3784 for (bs = bdrv_first(&it); bs; bs = bdrv_next(&it)) {
3785 bool perm;
3787 /* try to recurse in this top level bs */
3788 perm = bdrv_recurse_is_first_non_filter(bs, candidate);
3790 /* candidate is the first non filter */
3791 if (perm) {
3792 return true;
3796 return false;
3799 BlockDriverState *check_to_replace_node(BlockDriverState *parent_bs,
3800 const char *node_name, Error **errp)
3802 BlockDriverState *to_replace_bs = bdrv_find_node(node_name);
3803 AioContext *aio_context;
3805 if (!to_replace_bs) {
3806 error_setg(errp, "Node name '%s' not found", node_name);
3807 return NULL;
3810 aio_context = bdrv_get_aio_context(to_replace_bs);
3811 aio_context_acquire(aio_context);
3813 if (bdrv_op_is_blocked(to_replace_bs, BLOCK_OP_TYPE_REPLACE, errp)) {
3814 to_replace_bs = NULL;
3815 goto out;
3818 /* We don't want arbitrary node of the BDS chain to be replaced only the top
3819 * most non filter in order to prevent data corruption.
3820 * Another benefit is that this tests exclude backing files which are
3821 * blocked by the backing blockers.
3823 if (!bdrv_recurse_is_first_non_filter(parent_bs, to_replace_bs)) {
3824 error_setg(errp, "Only top most non filter can be replaced");
3825 to_replace_bs = NULL;
3826 goto out;
3829 out:
3830 aio_context_release(aio_context);
3831 return to_replace_bs;
3834 static bool append_open_options(QDict *d, BlockDriverState *bs)
3836 const QDictEntry *entry;
3837 QemuOptDesc *desc;
3838 BdrvChild *child;
3839 bool found_any = false;
3840 const char *p;
3842 for (entry = qdict_first(bs->options); entry;
3843 entry = qdict_next(bs->options, entry))
3845 /* Exclude options for children */
3846 QLIST_FOREACH(child, &bs->children, next) {
3847 if (strstart(qdict_entry_key(entry), child->name, &p)
3848 && (!*p || *p == '.'))
3850 break;
3853 if (child) {
3854 continue;
3857 /* And exclude all non-driver-specific options */
3858 for (desc = bdrv_runtime_opts.desc; desc->name; desc++) {
3859 if (!strcmp(qdict_entry_key(entry), desc->name)) {
3860 break;
3863 if (desc->name) {
3864 continue;
3867 qobject_incref(qdict_entry_value(entry));
3868 qdict_put_obj(d, qdict_entry_key(entry), qdict_entry_value(entry));
3869 found_any = true;
3872 return found_any;
3875 /* Updates the following BDS fields:
3876 * - exact_filename: A filename which may be used for opening a block device
3877 * which (mostly) equals the given BDS (even without any
3878 * other options; so reading and writing must return the same
3879 * results, but caching etc. may be different)
3880 * - full_open_options: Options which, when given when opening a block device
3881 * (without a filename), result in a BDS (mostly)
3882 * equalling the given one
3883 * - filename: If exact_filename is set, it is copied here. Otherwise,
3884 * full_open_options is converted to a JSON object, prefixed with
3885 * "json:" (for use through the JSON pseudo protocol) and put here.
3887 void bdrv_refresh_filename(BlockDriverState *bs)
3889 BlockDriver *drv = bs->drv;
3890 QDict *opts;
3892 if (!drv) {
3893 return;
3896 /* This BDS's file name will most probably depend on its file's name, so
3897 * refresh that first */
3898 if (bs->file) {
3899 bdrv_refresh_filename(bs->file->bs);
3902 if (drv->bdrv_refresh_filename) {
3903 /* Obsolete information is of no use here, so drop the old file name
3904 * information before refreshing it */
3905 bs->exact_filename[0] = '\0';
3906 if (bs->full_open_options) {
3907 QDECREF(bs->full_open_options);
3908 bs->full_open_options = NULL;
3911 opts = qdict_new();
3912 append_open_options(opts, bs);
3913 drv->bdrv_refresh_filename(bs, opts);
3914 QDECREF(opts);
3915 } else if (bs->file) {
3916 /* Try to reconstruct valid information from the underlying file */
3917 bool has_open_options;
3919 bs->exact_filename[0] = '\0';
3920 if (bs->full_open_options) {
3921 QDECREF(bs->full_open_options);
3922 bs->full_open_options = NULL;
3925 opts = qdict_new();
3926 has_open_options = append_open_options(opts, bs);
3928 /* If no specific options have been given for this BDS, the filename of
3929 * the underlying file should suffice for this one as well */
3930 if (bs->file->bs->exact_filename[0] && !has_open_options) {
3931 strcpy(bs->exact_filename, bs->file->bs->exact_filename);
3933 /* Reconstructing the full options QDict is simple for most format block
3934 * drivers, as long as the full options are known for the underlying
3935 * file BDS. The full options QDict of that file BDS should somehow
3936 * contain a representation of the filename, therefore the following
3937 * suffices without querying the (exact_)filename of this BDS. */
3938 if (bs->file->bs->full_open_options) {
3939 qdict_put_obj(opts, "driver",
3940 QOBJECT(qstring_from_str(drv->format_name)));
3941 QINCREF(bs->file->bs->full_open_options);
3942 qdict_put_obj(opts, "file",
3943 QOBJECT(bs->file->bs->full_open_options));
3945 bs->full_open_options = opts;
3946 } else {
3947 QDECREF(opts);
3949 } else if (!bs->full_open_options && qdict_size(bs->options)) {
3950 /* There is no underlying file BDS (at least referenced by BDS.file),
3951 * so the full options QDict should be equal to the options given
3952 * specifically for this block device when it was opened (plus the
3953 * driver specification).
3954 * Because those options don't change, there is no need to update
3955 * full_open_options when it's already set. */
3957 opts = qdict_new();
3958 append_open_options(opts, bs);
3959 qdict_put_obj(opts, "driver",
3960 QOBJECT(qstring_from_str(drv->format_name)));
3962 if (bs->exact_filename[0]) {
3963 /* This may not work for all block protocol drivers (some may
3964 * require this filename to be parsed), but we have to find some
3965 * default solution here, so just include it. If some block driver
3966 * does not support pure options without any filename at all or
3967 * needs some special format of the options QDict, it needs to
3968 * implement the driver-specific bdrv_refresh_filename() function.
3970 qdict_put_obj(opts, "filename",
3971 QOBJECT(qstring_from_str(bs->exact_filename)));
3974 bs->full_open_options = opts;
3977 if (bs->exact_filename[0]) {
3978 pstrcpy(bs->filename, sizeof(bs->filename), bs->exact_filename);
3979 } else if (bs->full_open_options) {
3980 QString *json = qobject_to_json(QOBJECT(bs->full_open_options));
3981 snprintf(bs->filename, sizeof(bs->filename), "json:%s",
3982 qstring_get_str(json));
3983 QDECREF(json);
3988 * Hot add/remove a BDS's child. So the user can take a child offline when
3989 * it is broken and take a new child online
3991 void bdrv_add_child(BlockDriverState *parent_bs, BlockDriverState *child_bs,
3992 Error **errp)
3995 if (!parent_bs->drv || !parent_bs->drv->bdrv_add_child) {
3996 error_setg(errp, "The node %s does not support adding a child",
3997 bdrv_get_device_or_node_name(parent_bs));
3998 return;
4001 if (!QLIST_EMPTY(&child_bs->parents)) {
4002 error_setg(errp, "The node %s already has a parent",
4003 child_bs->node_name);
4004 return;
4007 parent_bs->drv->bdrv_add_child(parent_bs, child_bs, errp);
4010 void bdrv_del_child(BlockDriverState *parent_bs, BdrvChild *child, Error **errp)
4012 BdrvChild *tmp;
4014 if (!parent_bs->drv || !parent_bs->drv->bdrv_del_child) {
4015 error_setg(errp, "The node %s does not support removing a child",
4016 bdrv_get_device_or_node_name(parent_bs));
4017 return;
4020 QLIST_FOREACH(tmp, &parent_bs->children, next) {
4021 if (tmp == child) {
4022 break;
4026 if (!tmp) {
4027 error_setg(errp, "The node %s does not have a child named %s",
4028 bdrv_get_device_or_node_name(parent_bs),
4029 bdrv_get_device_or_node_name(child->bs));
4030 return;
4033 parent_bs->drv->bdrv_del_child(parent_bs, child, errp);