Lines Matching +full:role +full:- +full:switch +full:- +full:default +full:- +full:mode

30 #include "block/dirty-bitmap.h"
34 #include "qemu/error-report.h"
36 #include "qemu/main-loop.h"
43 #include "qapi/qobject-output-visitor.h"
44 #include "qapi/qapi-visit-block-core.h"
45 #include "system/block-backend.h"
113 /* If non-zero, use only whitelisted block drivers */
138 if (!bs || !bs->drv) {
144 return bs->bl.opt_mem_alignment;
149 if (!bs || !bs->drv) {
155 return bs->bl.min_mem_alignment;
229 len = p - base_path;
261 assert(!path_has_protocol(fat_filename->str));
267 * filename as-is */
274 /* Returns whether the image file is opened as read-only. Note that this can
280 return !(bs->open_flags & BDRV_O_RDWR);
290 if (bs->copy_on_read && read_only) {
291 error_setg(errp, "Can't set node '%s' to r/o with copy-on-read enabled",
293 return -EINVAL;
297 if (!read_only && !(bs->open_flags & BDRV_O_ALLOW_RDWR) &&
302 return -EPERM;
309 * Called by a driver that can only provide a read-only image.
311 * Returns 0 if the node is already read-only or it could switch the node to
312 * read-only because BDRV_O_AUTO_RDONLY is set.
314 * Returns -EACCES if the node is read-write and BDRV_O_AUTO_RDONLY is not set
315 * or bdrv_can_set_read_only() forbids making the node read-only. If @errmsg
324 if (!(bs->open_flags & BDRV_O_RDWR)) {
327 if (!(bs->open_flags & BDRV_O_AUTO_RDONLY)) {
336 bs->open_flags &= ~BDRV_O_RDWR;
341 error_setg(errp, "%s", errmsg ?: "Image is read-only");
342 return -EACCES;
401 return bdrv_make_absolute_filename(bs, bs->backing_file, errp);
406 assert(bdrv->format_name);
419 QLIST_INIT(&bs->dirty_bitmaps);
421 QLIST_INIT(&bs->op_blockers[i]);
423 qemu_mutex_init(&bs->reqs_lock);
424 qemu_mutex_init(&bs->dirty_bitmap_mutex);
425 bs->refcnt = 1;
426 bs->aio_context = qemu_get_aio_context();
428 qemu_co_queue_init(&bs->flush_queue);
430 qemu_co_mutex_init(&bs->bsc_modify_lock);
431 bs->block_status_cache = g_new0(BdrvBlockStatusCache, 1);
448 if (!strcmp(drv1->format_name, format_name)) {
519 return bdrv_format_is_whitelisted(drv->format_name, read_only);
542 if (!drv->bdrv_co_create_opts) {
544 drv->format_name);
545 return -ENOTSUP;
548 ret = drv->bdrv_co_create_opts(drv, filename, opts, errp);
550 error_setg_errno(errp, -ret, "Could not create image");
561 * Otherwise, return -errno.
575 if (ret < 0 && ret != -ENOTSUP) {
583 error_setg_errno(errp, -size,
591 return -ENOTSUP;
602 * sector to remove any potentially pre-existing image header.
619 error_setg_errno(errp, -ret,
656 return -EINVAL;
660 error_setg(errp, "Unsupported preallocation mode '%s'",
662 return -ENOTSUP;
666 qdict_put_str(options, "driver", drv->format_name);
675 drv->format_name);
676 return -EINVAL;
708 return -ENOENT;
711 if (!drv->create_opts) {
713 drv->format_name);
714 return -ENOTSUP;
719 * default values.
721 * The format properly removes its options, but the default values remain
722 * in 'opts->list'. So if the protocol has options with the same name
723 * (e.g. rbd has 'cluster_size' as qcow2), it will see the default values
732 protocol_opts = qemu_opts_from_qdict(drv->create_opts, qdict, errp);
734 ret = -EINVAL;
754 if (!bs->drv) {
755 error_setg(errp, "Block node '%s' is not opened", bs->filename);
756 return -ENOMEDIUM;
759 if (!bs->drv->bdrv_co_delete_file) {
761 bs->drv->format_name);
762 return -ENOTSUP;
765 ret = bs->drv->bdrv_co_delete_file(bs, &local_err);
789 if (ret == -ENOTSUP) {
799 * On failure return -errno.
804 BlockDriver *drv = bs->drv;
808 if (drv && drv->bdrv_probe_blocksizes) {
809 return drv->bdrv_probe_blocksizes(bs, bsz);
814 return -ENOTSUP;
820 * On failure return -errno.
825 BlockDriver *drv = bs->drv;
831 if (drv && drv->bdrv_probe_geometry) {
832 return drv->bdrv_probe_geometry(bs, geo);
840 return -ENOTSUP;
844 * Create a uniquely-named empty temporary file.
860 * See commit 69bef79 ("block: use /var/tmp instead of /tmp for -snapshot")
862 * This function is used to create temporary disk images (like -snapshot),
894 if (d->bdrv_probe_device) {
895 score = d->bdrv_probe_device(filename);
912 if (drv1->protocol_name && !strcmp(drv1->protocol_name, protocol)) {
936 * Thanks to the brain-dead persistent naming schemes on udev-
950 len = p - filename;
951 if (len > sizeof(protocol) - 1)
952 len = sizeof(protocol) - 1;
982 * This is not a good idea when your image is raw (CVE-2008-2004), but
1002 if (d->bdrv_probe) {
1003 score = d->bdrv_probe(buf, buf_size, filename);
1023 /* Return the raw BlockDriver * to scsi-generic devices or empty drives */
1031 error_setg_errno(errp, -ret, "Could not read image for determining its "
1042 return -ENOENT;
1051 * Return 0 on success, -errno on error.
1056 BlockDriver *drv = bs->drv;
1061 return -ENOMEDIUM;
1064 /* Do not attempt drv->bdrv_co_getlength() on scsi-generic devices */
1069 if (drv->bdrv_co_getlength) {
1070 int64_t length = drv->bdrv_co_getlength(bs);
1077 bs->total_sectors = hint;
1079 if (bs->total_sectors * BDRV_SECTOR_SIZE > BDRV_MAX_LENGTH) {
1080 return -EFBIG;
1094 if (bs->drv && bs->drv->bdrv_join_options) {
1095 bs->drv->bdrv_join_options(options, old_options);
1106 char *value = qemu_opt_get_del(opts, "detect-zeroes");
1120 error_setg(errp, "setting detect-zeroes to unmap is not allowed "
1130 * Return 0 on success, -1 if the engine specified is invalid
1132 int bdrv_parse_aio(const char *mode, int *flags)
1134 if (!strcmp(mode, "threads")) {
1135 /* do nothing, default */
1136 } else if (!strcmp(mode, "native")) {
1139 } else if (!strcmp(mode, "io_uring")) {
1143 return -1;
1150 * Set open flags for a given discard mode
1152 * Return 0 on success, -1 if the discard mode was invalid.
1154 int bdrv_parse_discard_flags(const char *mode, int *flags)
1158 if (!strcmp(mode, "off") || !strcmp(mode, "ignore")) {
1160 } else if (!strcmp(mode, "on") || !strcmp(mode, "unmap")) {
1163 return -1;
1170 * Set open flags for a given cache mode
1172 * Return 0 on success, -1 if the cache mode was invalid.
1174 int bdrv_parse_cache_mode(const char *mode, int *flags, bool *writethrough)
1178 if (!strcmp(mode, "off") || !strcmp(mode, "none")) {
1181 } else if (!strcmp(mode, "directsync")) {
1184 } else if (!strcmp(mode, "writeback")) {
1186 } else if (!strcmp(mode, "unsafe")) {
1189 } else if (!strcmp(mode, "writethrough")) {
1192 return -1;
1200 BlockDriverState *parent = c->opaque;
1206 BlockDriverState *bs = child->opaque;
1212 BlockDriverState *bs = child->opaque;
1218 BlockDriverState *bs = child->opaque;
1224 BlockDriverState *bs = child->opaque;
1226 assert(bs->open_flags & BDRV_O_INACTIVE);
1235 BlockDriverState *bs = child->opaque;
1254 /* Copy the read-only and discard options from the parent */
1265 BlockDriverState *parent = c->opaque;
1266 BlockDriverState *backing_hd = c->bs;
1269 assert(!parent->backing_blocker);
1270 error_setg(&parent->backing_blocker,
1276 parent->open_flags &= ~BDRV_O_NO_BACKING;
1278 bdrv_op_block_all(backing_hd, parent->backing_blocker);
1281 parent->backing_blocker);
1283 parent->backing_blocker);
1298 parent->backing_blocker);
1300 parent->backing_blocker);
1305 BlockDriverState *parent = c->opaque;
1308 assert(parent->backing_blocker);
1309 bdrv_op_unblock_all(c->bs, parent->backing_blocker);
1310 error_free(parent->backing_blocker);
1311 parent->backing_blocker = NULL;
1319 BlockDriverState *parent = c->opaque;
1332 if (base->drv) {
1339 if (backing_mask_protocol && base->drv->protocol_name) {
1342 format_name = base->drv->format_name;
1350 error_setg_errno(errp, -ret, "Could not update backing file link");
1364 static void bdrv_inherited_options(BdrvChildRole role, bool parent_is_format,
1374 * format-probed by default?
1378 * Pure and non-filtered data children of non-format nodes should
1379 * be probed by default (even when the node itself has BDRV_O_PROTOCOL
1382 * Force-clear BDRV_O_PROTOCOL then.
1385 (role & BDRV_CHILD_DATA) &&
1386 !(role & (BDRV_CHILD_METADATA | BDRV_CHILD_FILTERED)))
1393 * metadata children in general should never be format-probed.
1394 * Force-set BDRV_O_PROTOCOL then.
1396 if ((parent_is_format && !(role & BDRV_CHILD_COW)) ||
1397 (role & BDRV_CHILD_METADATA))
1403 * If the cache mode isn't explicitly set, inherit direct and no-flush from
1410 if (role & BDRV_CHILD_COW) {
1411 /* backing files are opened read-only by default */
1415 /* Inherit the read-only option from the parent if it's not set */
1423 * can default to enable it on lower layers regardless of the
1431 if (role & BDRV_CHILD_METADATA) {
1434 if (role & BDRV_CHILD_COW) {
1443 BlockDriverState *bs = child->opaque;
1446 QLIST_INSERT_HEAD(&bs->children, child, next);
1447 if (bs->drv->is_filter || (child->role & BDRV_CHILD_FILTERED)) {
1449 * Here we handle filters and block/raw-format.c when it behave like
1453 * So bs->file will be the PRIMARY child, unless the PRIMARY child goes
1454 * into bs->backing on exceptional cases; and bs->backing will be
1457 assert(!(child->role & BDRV_CHILD_COW));
1458 if (child->role & BDRV_CHILD_PRIMARY) {
1459 assert(child->role & BDRV_CHILD_FILTERED);
1460 assert(!bs->backing);
1461 assert(!bs->file);
1463 if (bs->drv->filtered_child_is_backing) {
1464 bs->backing = child;
1466 bs->file = child;
1469 assert(!(child->role & BDRV_CHILD_FILTERED));
1471 } else if (child->role & BDRV_CHILD_COW) {
1472 assert(bs->drv->supports_backing);
1473 assert(!(child->role & BDRV_CHILD_PRIMARY));
1474 assert(!bs->backing);
1475 bs->backing = child;
1477 } else if (child->role & BDRV_CHILD_PRIMARY) {
1478 assert(!bs->file);
1479 bs->file = child;
1485 BlockDriverState *bs = child->opaque;
1487 if (child->role & BDRV_CHILD_COW) {
1493 if (child == bs->backing) {
1494 assert(child != bs->file);
1495 bs->backing = NULL;
1496 } else if (child == bs->file) {
1497 bs->file = NULL;
1506 if (c->role & BDRV_CHILD_COW) {
1516 BlockDriverState *bs = c->opaque;
1540 return c->klass->get_parent_aio_context(c);
1617 error_setg(errp, "Invalid node-name: '%s'", node_name);
1623 error_setg(errp, "node-name=%s is conflicting with a device id",
1630 error_setg(errp, "Duplicate nodes with node-name='%s'", node_name);
1635 if (strlen(node_name) >= sizeof(bs->node_name)) {
1641 pstrcpy(bs->node_name, sizeof(bs->node_name), node_name);
1658 return -EINVAL;
1661 bs->drv = drv;
1662 bs->opaque = g_malloc0(drv->instance_size);
1664 assert(!drv->bdrv_needs_filename || bs->filename[0]);
1665 if (drv->bdrv_open) {
1666 ret = drv->bdrv_open(bs, options, open_flags, &local_err);
1674 } else if (bs->filename[0]) {
1675 error_setg_errno(errp, -ret, "Could not open '%s'", bs->filename);
1677 error_setg_errno(errp, -ret, "Could not open image");
1682 assert(!(bs->supported_read_flags & ~BDRV_REQ_MASK));
1683 assert(!(bs->supported_write_flags & ~BDRV_REQ_MASK));
1693 bs->supported_read_flags |= BDRV_REQ_REGISTERED_BUF;
1694 bs->supported_write_flags |= BDRV_REQ_REGISTERED_BUF;
1696 ret = bdrv_refresh_total_sectors(bs, bs->total_sectors);
1698 error_setg_errno(errp, -ret, "Could not refresh total sector count");
1708 return -EINVAL;
1713 assert(is_power_of_2(bs->bl.request_alignment));
1715 for (i = 0; i < bs->quiesce_counter; i++) {
1716 if (drv->bdrv_drain_begin) {
1717 drv->bdrv_drain_begin(bs);
1723 bs->drv = NULL;
1726 if (bs->file != NULL) {
1727 bdrv_unref_child(bs, bs->file);
1728 assert(!bs->file);
1732 g_free(bs->opaque);
1733 bs->opaque = NULL;
1756 bs->open_flags = flags;
1757 bs->options = options ?: qdict_new();
1758 bs->explicit_options = qdict_clone_shallow(bs->options);
1759 bs->opaque = NULL;
1761 update_options_from_flags(bs->options, flags);
1763 ret = bdrv_open_driver(bs, drv, node_name, bs->options, flags, errp);
1765 qobject_unref(bs->explicit_options);
1766 bs->explicit_options = NULL;
1767 qobject_unref(bs->options);
1768 bs->options = NULL;
1789 .name = "node-name",
1816 .help = "Node is opened in read-only mode",
1821 .help = "Node can become read-only if opening read-write fails",
1824 .name = "detect-zeroes",
1836 .help = "always accept other writers (default: off)",
1843 .name = "simple-create-opts",
1854 .help = "Preallocation mode (allowed values: off)"
1881 assert(bs->file == NULL);
1882 assert(options != NULL && bs->options != options);
1887 ret = -EINVAL;
1891 update_flags_from_options(&bs->open_flags, opts);
1897 bs->force_share = qemu_opt_get_bool(opts, BDRV_OPT_FORCE_SHARE, false);
1899 if (bs->force_share && (bs->open_flags & BDRV_O_RDWR)) {
1902 "=on can only be used with read-only images");
1903 ret = -EINVAL;
1912 filename = blk_bs(file)->filename;
1916 * non-string types would require more care. When @options
1917 * come from -blockdev or blockdev_add, its members are typed
1919 * -drive, they're all QString.
1924 if (drv->bdrv_needs_filename && (!filename || !filename[0])) {
1926 drv->format_name);
1927 ret = -EINVAL;
1931 trace_bdrv_open_common(bs, filename ?: "", bs->open_flags,
1932 drv->format_name);
1942 ret = -ENOTSUP;
1947 ? "Driver '%s' can only be used for read-only devices"
1949 drv->format_name);
1955 assert(qatomic_read(&bs->copy_on_read) == 0);
1957 if (bs->open_flags & BDRV_O_COPY_ON_READ) {
1961 error_setg(errp, "Can't use copy-on-read on read-only device");
1962 ret = -EINVAL;
1969 if (bdrv_parse_discard_flags(discard, &bs->open_flags) != 0) {
1971 ret = -EINVAL;
1976 bs->detect_zeroes =
1977 bdrv_parse_detect_zeroes(opts, bs->open_flags, &local_err);
1980 ret = -EINVAL;
1985 pstrcpy(bs->filename, sizeof(bs->filename), filename);
1987 bs->filename[0] = '\0';
1989 pstrcpy(bs->exact_filename, sizeof(bs->exact_filename), bs->filename);
1992 open_flags = bdrv_open_flags(bs, bs->open_flags);
1993 node_name = qemu_opt_get(opts, "node-name");
1995 assert(!drv->protocol_name || file == NULL);
2045 /* Parse json: pseudo-protocol */
2064 * Fills in default options for opening images and converts the legacy
2082 * Caution: while qdict_get_try_str() is fine, getting non-string
2084 * -blockdev or blockdev_add, its members are typed according to
2085 * the QAPI schema, but when they come from -drive, they're all
2093 return -ENOENT;
2097 protocol = drv->protocol_name;
2117 return -EINVAL;
2129 return -EINVAL;
2132 drvname = drv->format_name;
2136 return -EINVAL;
2142 /* Driver-specific filename parsing */
2143 if (drv && drv->bdrv_parse_filename && parse_filename) {
2144 drv->bdrv_parse_filename(filename, *options, &local_err);
2147 return -EINVAL;
2150 if (!drv->bdrv_needs_filename) {
2175 if (entry->state.bs == bs) {
2176 return entry->state.flags;
2181 return bs->open_flags;
2197 * be written to but do not count as read-only images.
2208 return c->klass->get_parent_desc(c);
2222 assert(a->bs);
2223 assert(a->bs == b->bs);
2226 if ((b->perm & a->shared_perm) == b->perm) {
2230 child_bs_name = bdrv_get_node_name(b->bs);
2233 perms = bdrv_perm_names(b->perm & ~a->shared_perm);
2239 b_user, child_bs_name, b->name,
2240 a_user, child_bs_name, a->name);
2256 QLIST_FOREACH(a, &bs->parents, next_parent) {
2257 QLIST_FOREACH(b, &bs->parents, next_parent) {
2273 BdrvChild *c, BdrvChildRole role,
2278 assert(bs->drv && bs->drv->bdrv_child_perm);
2280 bs->drv->bdrv_child_perm(bs, c, role, reopen_queue,
2284 if (child_bs && child_bs->force_share) {
2318 QLIST_FOREACH(child, &bs->children, next) {
2319 list = bdrv_topological_dfs(list, found, child->bs);
2337 s->child->perm = s->old_perm;
2338 s->child->shared_perm = s->old_shared_perm;
2354 .old_perm = c->perm,
2355 .old_shared_perm = c->shared_perm,
2358 c->perm = perm;
2359 c->shared_perm = shared;
2370 if (bs->drv->bdrv_set_perm) {
2373 bs->drv->bdrv_set_perm(bs, cumulative_perms, cumulative_shared_perms);
2382 if (bs->drv->bdrv_abort_perm_update) {
2383 bs->drv->bdrv_abort_perm_update(bs);
2401 if (!bs->drv) {
2405 if (bs->drv->bdrv_check_perm) {
2406 int ret = bs->drv->bdrv_check_perm(bs, perm, shared_perm, errp);
2429 bdrv_schedule_unref(s->old_bs);
2435 BlockDriverState *new_bs = s->child->bs;
2440 /* old_bs reference is transparently moved from @s to @s->child */
2441 if (!s->child->bs) {
2451 bdrv_parent_drained_begin_single(s->child);
2452 assert(!bdrv_parent_drained_poll_single(s->child));
2454 assert(s->child->quiesced_parent);
2455 bdrv_replace_child_noperm(s->child, s->old_bs);
2471 * Both @child->bs and @new_bs (if non-NULL) must be drained. @new_bs must be
2485 assert(child->quiesced_parent);
2486 assert(!new_bs || new_bs->quiesce_counter);
2490 .old_bs = child->bs,
2513 BlockDriver *drv = bs->drv;
2521 /* Write permissions never work with read-only images */
2526 error_setg(errp, "Block node is read-only");
2528 error_setg(errp, "Read-only block node '%s' cannot support "
2529 "read-write users", bdrv_get_node_name(bs));
2532 return -EPERM;
2543 if ((bs->total_sectors * BDRV_SECTOR_SIZE) % bs->bl.request_alignment) {
2547 return -EPERM;
2563 if (!drv->bdrv_child_perm) {
2564 assert(QLIST_EMPTY(&bs->children));
2569 QLIST_FOREACH(c, &bs->children, next) {
2572 bdrv_child_perm(bs, c->bs, c, c->role, q,
2582 * @list is a product of bdrv_topological_dfs() (may be called several times) -
2596 for ( ; list; list = list->next) {
2597 bs = list->data;
2600 return -EINVAL;
2627 for ( ; list; list = list->next) {
2628 refresh_list = bdrv_topological_dfs(refresh_list, found, list->data);
2643 QLIST_FOREACH(c, &bs->parents, next_parent) {
2644 cumulative_perms |= c->perm;
2645 cumulative_shared_perms &= c->shared_perm;
2668 for (p = permissions; p->name; p++) {
2669 if (perm & p->perm) {
2670 if (result->len > 0) {
2673 g_string_append(result, p->name);
2719 ret = bdrv_refresh_perms(c->bs, tran, &local_err);
2724 if ((perm & ~c->perm) || (c->shared_perm & ~shared)) {
2750 bdrv_child_perm(bs, c->bs, c, c->role, NULL,
2757 * Default implementation for .bdrv_child_perm() for block filters:
2762 BdrvChildRole role,
2773 BdrvChildRole role,
2778 assert(role & BDRV_CHILD_COW);
2800 if (bs->open_flags & BDRV_O_INACTIVE) {
2809 BdrvChildRole role,
2817 assert(role & (BDRV_CHILD_METADATA | BDRV_CHILD_DATA));
2825 bdrv_filter_default_perms(bs, c, role, reopen_queue,
2828 if (role & BDRV_CHILD_METADATA) {
2835 * bs->file always needs to be consistent because of the
2845 if (role & BDRV_CHILD_DATA) {
2858 * is split into fixed-size data files).
2865 * write copied clusters on copy-on-read.
2880 if (bs->open_flags & BDRV_O_INACTIVE) {
2889 BdrvChildRole role, BlockReopenQueue *reopen_queue,
2894 if (role & BDRV_CHILD_FILTERED) {
2895 assert(!(role & (BDRV_CHILD_DATA | BDRV_CHILD_METADATA |
2897 bdrv_filter_default_perms(bs, c, role, reopen_queue,
2899 } else if (role & BDRV_CHILD_COW) {
2900 assert(!(role & (BDRV_CHILD_DATA | BDRV_CHILD_METADATA)));
2901 bdrv_default_perms_for_cow(bs, c, role, reopen_queue,
2903 } else if (role & (BDRV_CHILD_METADATA | BDRV_CHILD_DATA)) {
2904 bdrv_default_perms_for_storage(bs, c, role, reopen_queue,
2931 * If @new_bs is non-NULL, the parent of @child must already be drained through
2937 BlockDriverState *old_bs = child->bs;
2940 assert(!child->frozen);
2944 * child->bs, we need to make sure that its new parent is drained, too. In
2945 * other words, either child->quiesce_parent must already be true or we must
2951 * To keep things simple, we pick the first option (child->quiesce_parent
2962 assert(!new_bs || child->quiesced_parent);
2971 if (child->klass->detach) {
2972 child->klass->detach(child);
2977 child->bs = new_bs;
2980 QLIST_INSERT_HEAD(&new_bs->parents, child, next_parent);
2981 if (child->klass->attach) {
2982 child->klass->attach(child);
2991 new_bs_quiesce_counter = (new_bs ? new_bs->quiesce_counter : 0);
2992 if (!new_bs_quiesce_counter && child->quiesced_parent) {
3000 * The child must be empty (i.e. `child->bs == NULL`) and it must be
3005 assert(!child->bs);
3009 assert(!child->next.le_prev); /* not in children list */
3011 g_free(child->name);
3024 BlockDriverState *bs = s->child->bs;
3029 bdrv_replace_child_noperm(s->child, NULL);
3031 if (bdrv_get_aio_context(bs) != s->old_child_ctx) {
3032 bdrv_try_change_aio_context_locked(bs, s->old_child_ctx, NULL,
3036 if (bdrv_child_get_parent_aio_context(s->child) != s->old_parent_ctx) {
3045 ret = s->child->klass->change_aio_ctx(s->child, s->old_parent_ctx,
3055 bdrv_child_free(s->child);
3092 assert(child_class->get_parent_desc);
3107 .role = child_role,
3124 if (ret < 0 && child_class->change_aio_ctx) {
3130 ret_child = child_class->change_aio_ctx(new_child, child_ctx,
3137 tran_finalize(aio_ctx_tran, ret_child == true ? 0 : -1);
3203 assert(parent_bs->drv);
3208 child_bs->node_name, child_name, parent_bs->node_name);
3213 child_bs->node_name, child_name, parent_bs->node_name);
3252 ret = -EINVAL;
3292 ret = -EINVAL;
3310 * Callers must ensure that child->frozen is false.
3316 BlockDriverState *child_bs = child->bs;
3331 * When the parent requiring a non-default AioContext is removed, the
3350 s->bs->inherits_from = s->old_inherits_from;
3368 .old_inherits_from = bs->inherits_from,
3374 bs->inherits_from = new_inherits_from;
3388 if (child->bs->inherits_from == root) {
3391 * child->bs goes away.
3393 QLIST_FOREACH(c, &root->children, next) {
3394 if (c != child && c->bs == child->bs) {
3399 bdrv_set_inherits_from(child->bs, NULL, tran);
3403 QLIST_FOREACH(c, &child->bs->children, next) {
3409 * Callers must ensure that child->frozen is false.
3430 QLIST_FOREACH(c, &bs->parents, next_parent) {
3431 if (c->klass->change_media) {
3432 c->klass->change_media(c, load);
3437 /* Return true if you can reach parent going through child->inherits_from
3443 child = child->inherits_from;
3450 * Return the BdrvChildRole for @bs's backing child. bs->backing is
3451 * mostly used for COW backing children (role = COW), but also for
3452 * filtered children (role = FILTERED | PRIMARY).
3456 if (bs->drv && bs->drv->is_filter) {
3464 * Sets the bs->backing or bs->file link of a BDS. A new reference is created;
3489 BdrvChild *child = is_backing ? parent_bs->backing : parent_bs->file;
3490 BdrvChildRole role;
3494 if (!parent_bs->drv) {
3497 * qcow2 driver to never clear bs->drv and implement format corruption
3501 return -EINVAL;
3504 if (child && child->frozen) {
3506 child->name, parent_bs->node_name, child->bs->node_name);
3507 return -EPERM;
3510 if (is_backing && !parent_bs->drv->is_filter &&
3511 !parent_bs->drv->supports_backing)
3514 "files", parent_bs->drv->format_name, parent_bs->node_name);
3515 return -EINVAL;
3518 if (parent_bs->drv->is_filter) {
3519 role = BDRV_CHILD_FILTERED | BDRV_CHILD_PRIMARY;
3521 role = BDRV_CHILD_COW;
3524 * We only can use same role as it is in existing child. We don't have
3525 * infrastructure to determine role of file child in generic way
3530 return -EINVAL;
3532 role = child->role;
3536 assert(child->bs->quiesce_counter);
3547 &child_of_bds, role,
3550 return -EINVAL;
3581 assert(bs->quiesce_counter > 0);
3582 if (bs->backing) {
3583 assert(bs->backing->bs->quiesce_counter > 0);
3625 if (bs->backing != NULL) {
3635 bs->open_flags &= ~BDRV_O_NO_BACKING;
3642 * Caution: while qdict_get_try_str() is fine, getting non-string
3644 * -blockdev or blockdev_add, its members are typed according to
3645 * the QAPI schema, but when they come from -drive, they're all
3651 } else if (bs->backing_file[0] == '\0' && qdict_size(options) == 0) {
3660 * only possible with -drive anyway (otherwise the QAPI
3662 implicit_backing = !strcmp(bs->auto_backing_file, bs->backing_file);
3667 ret = -EINVAL;
3674 if (!bs->drv || !bs->drv->supports_backing) {
3675 ret = -EINVAL;
3682 bs->backing_format[0] != '\0' && !qdict_haskey(options, "driver")) {
3683 qdict_put_str(options, "driver", bs->backing_format);
3690 bs->open_flags |= BDRV_O_NO_BACKING;
3692 ret = -EINVAL;
3698 pstrcpy(bs->auto_backing_file, sizeof(bs->auto_backing_file),
3699 backing_hd->filename);
3742 * Caution: while qdict_get_try_str() is fine, getting non-string
3744 * -blockdev or blockdev_add, its members are typed according to
3745 * the QAPI schema, but when they come from -drive, they're all
3835 BdrvChildRole role;
3838 assert(!parent->drv->filtered_child_is_backing);
3839 role = parent->drv->is_filter ?
3843 &child_of_bds, role, false, true, errp))
3845 return -EINVAL;
3865 if (ref->type == QTYPE_QSTRING) {
3866 reference = ref->u.reference;
3868 BlockdevOptions *options = &ref->u.definition;
3869 assert(ref->type == QTYPE_QDICT);
3917 error_setg_errno(errp, -total_size, "Could not get image size");
4034 bs->explicit_options = qdict_clone_shallow(options);
4039 if (parent->drv) {
4040 parent_is_format = parent->drv->is_format;
4043 * parent->drv is not set yet because this node is opened for
4050 bs->inherits_from = parent;
4051 child_class->inherit_options(child_role, parent_is_format,
4053 parent->open_flags, parent->options);
4065 * When @options come from -blockdev or blockdev_add, members are
4067 * -drive, they're all QString.
4080 /* Let bdrv_backing_options() override "read-only" */
4086 bs->open_flags = flags;
4087 bs->options = options;
4113 qdict_del(bs->explicit_options, "backing");
4114 qdict_del(bs->options, "backing");
4148 bs->probed = !drv;
4156 * but we first need to open bs->file for the probing to work, while
4157 * opening bs->file already requires the (mostly) final set of options
4158 * so that cache mode etc. can be inherited.
4162 * sure to update both bs->options (which has the full effective
4165 qdict_put_str(bs->options, "driver", drv->format_name);
4166 qdict_put_str(options, "driver", drv->format_name);
4173 assert(!!(flags & BDRV_O_PROTOCOL) == !!drv->protocol_name);
4198 * from bs->options and bs->explicit_options */
4199 QLIST_FOREACH(child, &bs->children, next) {
4201 child_key_dot = g_strdup_printf("%s.", child->name);
4202 qdict_extract_subqdict(bs->explicit_options, NULL, child_key_dot);
4203 qdict_extract_subqdict(bs->options, NULL, child_key_dot);
4204 qdict_del(bs->explicit_options, child->name);
4205 qdict_del(bs->options, child->name);
4214 "'%s'", drv->format_name, entry->key);
4218 drv->format_name, entry->key);
4252 qobject_unref(bs->explicit_options);
4253 qobject_unref(bs->options);
4255 bs->options = NULL;
4256 bs->explicit_options = NULL;
4278 /* Return true if the NULL-terminated @list contains @str */
4293 * Check that every option set in @bs->options is also set in
4297 * @bs->drv->mutable_opts are skipped.
4299 * Return 0 on success, otherwise return -EINVAL and set @errp.
4308 "node-name", "discard", "cache.direct", "cache.no-flush",
4309 "read-only", "auto-read-only", "detect-zeroes", NULL
4312 for (e = qdict_first(bs->options); e; e = qdict_next(bs->options, e)) {
4313 if (!qdict_haskey(new_opts, e->key) &&
4314 !is_str_in_list(e->key, common_options) &&
4315 !is_str_in_list(e->key, bs->drv->mutable_opts)) {
4317 "to its default value", e->key);
4318 return -EINVAL;
4337 QLIST_FOREACH(c, &bs->children, next) {
4338 if (bdrv_recurse_has_child(c->bs, child)) {
4371 BdrvChildRole role, bool parent_is_format,
4385 assert(bs->quiesce_counter > 0);
4398 if (bs == bs_entry->state.bs) {
4414 bs_entry->state.explicit_options :
4415 bs->explicit_options);
4425 klass->inherit_options(role, parent_is_format, &flags, options,
4433 old_options = qdict_clone_shallow(bs->options);
4456 qobject_unref(bs_entry->state.options);
4457 qobject_unref(bs_entry->state.explicit_options);
4460 bs_entry->state.bs = bs;
4461 bs_entry->state.options = options;
4462 bs_entry->state.explicit_options = explicit_options;
4463 bs_entry->state.flags = flags;
4472 bs_entry->state.backing_missing =
4477 QLIST_FOREACH(child, &bs->children, next) {
4484 if (child->bs->inherits_from != bs) {
4489 if (qdict_haskey(options, child->name)) {
4490 const char *childref = qdict_get_try_str(options, child->name);
4495 if (g_strcmp0(childref, child->bs->node_name)) {
4505 /* Extract child options ("child-name.*") */
4506 char *child_key_dot = g_strdup_printf("%s.", child->name);
4512 bdrv_reopen_queue_child(bs_queue, child->bs, new_child_options,
4513 child->klass, child->role, bs->drv->is_format,
4543 qobject_unref(bs_entry->state.explicit_options);
4544 qobject_unref(bs_entry->state.options);
4575 int ret = -1;
4585 ret = bdrv_flush(bs_entry->state.bs);
4587 error_setg_errno(errp, -ret, "Error flushing drive");
4593 assert(bs_entry->state.bs->quiesce_counter > 0);
4594 ret = bdrv_reopen_prepare(&bs_entry->state, bs_queue, tran, errp);
4598 bs_entry->prepared = true;
4602 BDRVReopenState *state = &bs_entry->state;
4604 refresh_list = g_slist_prepend(refresh_list, state->bs);
4605 if (state->old_backing_bs) {
4606 refresh_list = g_slist_prepend(refresh_list, state->old_backing_bs);
4608 if (state->old_file_bs) {
4609 refresh_list = g_slist_prepend(refresh_list, state->old_file_bs);
4614 * Note that file-posix driver rely on permission update done during reopen
4633 * children are usually goes after parents in reopen-queue, so go from last
4637 bdrv_reopen_commit(&bs_entry->state);
4645 BlockDriverState *bs = bs_entry->state.bs;
4647 if (bs->drv->bdrv_reopen_commit_post) {
4648 bs->drv->bdrv_reopen_commit_post(&bs_entry->state);
4661 if (bs_entry->prepared) {
4662 bdrv_reopen_abort(&bs_entry->state);
4698 * reopen_state->options QDict is valid or not.
4703 * reopen_state->bs then return 0.
4707 * If that's the case then reopen_state->replace_backing_bs is set to
4708 * true and reopen_state->new_backing_bs contains a pointer to the new
4716 * @reopen_state->bs can move to a different AioContext in this function.
4726 BlockDriverState *bs = reopen_state->bs;
4738 value = qdict_get(reopen_state->options, child_name);
4745 switch (qobject_type(value)) {
4754 ret = -EINVAL;
4761 "cycle", str, child_name, bs->node_name);
4762 ret = -EINVAL;
4766 default:
4774 old_child_bs = is_backing ? child_bs(bs->backing) : child_bs(bs->file);
4786 if (old_child_bs->implicit) {
4788 child_name, bs->node_name);
4789 ret = -EPERM;
4794 if (bs->drv->is_filter && !old_child_bs) {
4800 "%s child", bs->node_name, bs->drv->format_name, child_name);
4801 ret = -EINVAL;
4806 reopen_state->old_backing_bs = old_child_bs;
4808 reopen_state->old_file_bs = old_child_bs;
4813 assert(old_child_bs->quiesce_counter > 0);
4844 * Returns 0 on success, non-zero on error. On error errp will be set
4861 int ret = -1;
4872 assert(reopen_state->bs->drv != NULL);
4874 drv = reopen_state->bs->drv;
4877 * entries from reopen_state->options as they are processed, so
4879 orig_reopen_opts = qdict_clone_shallow(reopen_state->options);
4883 if (!qemu_opts_absorb_qdict(opts, reopen_state->options, errp)) {
4884 ret = -EINVAL;
4889 * are up-to-date. This time we simply want to remove the options from
4891 old_flags = reopen_state->flags;
4892 update_flags_from_options(&reopen_state->flags, opts);
4893 assert(old_flags == reopen_state->flags);
4897 if (bdrv_parse_discard_flags(discard, &reopen_state->flags) != 0) {
4899 ret = -EINVAL;
4904 reopen_state->detect_zeroes =
4905 bdrv_parse_detect_zeroes(opts, reopen_state->flags, &local_err);
4908 ret = -EINVAL;
4912 /* All other options (including node-name and driver) must be unchanged.
4915 qemu_opts_to_qdict(opts, reopen_state->options);
4917 /* If we are to stay read-only, do not allow permission change
4920 read_only = !(reopen_state->flags & BDRV_O_RDWR);
4923 ret = bdrv_can_set_read_only(reopen_state->bs, read_only, true, &local_err);
4930 if (drv->bdrv_reopen_prepare) {
4932 * If a driver-specific option is missing, it means that we
4933 * should reset it to its default value.
4936 ret = bdrv_reset_options_allowed(reopen_state->bs,
4937 reopen_state->options, errp);
4942 ret = drv->bdrv_reopen_prepare(reopen_state, queue, &local_err);
4948 bdrv_refresh_filename(reopen_state->bs);
4951 reopen_state->bs->filename);
4960 "does not support reopening files", drv->format_name,
4961 bdrv_get_device_or_node_name(reopen_state->bs));
4963 ret = -1;
4975 if (drv->supports_backing && reopen_state->backing_missing &&
4976 (reopen_state->bs->backing || reopen_state->bs->backing_file[0])) {
4978 reopen_state->bs->node_name);
4980 ret = -EINVAL;
4995 qdict_del(reopen_state->options, "backing");
5003 qdict_del(reopen_state->options, "file");
5008 if (qdict_size(reopen_state->options)) {
5009 const QDictEntry *entry = qdict_first(reopen_state->options);
5014 QObject *new = entry->value;
5015 QObject *old = qdict_get(reopen_state->bs->options, entry->key);
5021 QLIST_FOREACH(child, &reopen_state->bs->children, next) {
5022 if (!strcmp(child->name, entry->key)) {
5028 if (!strcmp(child->bs->node_name,
5036 * TODO: When using -drive to specify blockdev options, all values
5037 * will be strings; however, when using -blockdev, blockdev-add or
5038 * filenames using the json:{} pseudo-protocol, they will be
5041 * (because you can only specify them through qemu-io; all other
5043 * Therefore, when using anything other than -drive to create a BDS,
5044 * this cannot detect non-string options as unchanged, because
5052 error_setg(errp, "Cannot change the option '%s'", entry->key);
5053 ret = -EINVAL;
5056 } while ((entry = qdict_next(reopen_state->options, entry)));
5061 /* Restore the original reopen_state->options QDict */
5062 qobject_unref(reopen_state->options);
5063 reopen_state->options = qobject_ref(orig_reopen_opts);
5067 /* drv->bdrv_reopen_prepare() has succeeded, so we need to
5068 * call drv->bdrv_reopen_abort() before signaling an error
5071 if (drv->bdrv_reopen_abort) {
5072 drv->bdrv_reopen_abort(reopen_state);
5093 bs = reopen_state->bs;
5094 drv = bs->drv;
5099 if (drv->bdrv_reopen_commit) {
5100 drv->bdrv_reopen_commit(reopen_state);
5106 qobject_unref(bs->explicit_options);
5107 qobject_unref(bs->options);
5108 qobject_ref(reopen_state->explicit_options);
5109 qobject_ref(reopen_state->options);
5111 bs->explicit_options = reopen_state->explicit_options;
5112 bs->options = reopen_state->options;
5113 bs->open_flags = reopen_state->flags;
5114 bs->detect_zeroes = reopen_state->detect_zeroes;
5116 /* Remove child references from bs->options and bs->explicit_options.
5118 QLIST_FOREACH(child, &bs->children, next) {
5119 qdict_del(bs->explicit_options, child->name);
5120 qdict_del(bs->options, child->name);
5123 qdict_del(bs->explicit_options, "backing");
5124 qdict_del(bs->options, "backing");
5127 bdrv_refresh_total_sectors(bs, bs->total_sectors);
5139 drv = reopen_state->bs->drv;
5143 if (drv->bdrv_reopen_abort) {
5144 drv->bdrv_reopen_abort(reopen_state);
5155 assert(!bs->refcnt);
5161 if (bs->drv) {
5162 if (bs->drv->bdrv_close) {
5164 bs->drv->bdrv_close(bs);
5166 bs->drv = NULL;
5170 QLIST_FOREACH_SAFE(child, &bs->children, next, next) {
5174 assert(!bs->backing);
5175 assert(!bs->file);
5178 g_free(bs->opaque);
5179 bs->opaque = NULL;
5180 qatomic_set(&bs->copy_on_read, 0);
5181 bs->backing_file[0] = '\0';
5182 bs->backing_format[0] = '\0';
5183 bs->total_sectors = 0;
5184 bs->encrypted = false;
5185 bs->sg = false;
5186 qobject_unref(bs->options);
5187 qobject_unref(bs->explicit_options);
5188 bs->options = NULL;
5189 bs->explicit_options = NULL;
5190 qobject_unref(bs->full_open_options);
5191 bs->full_open_options = NULL;
5192 g_free(bs->block_status_cache);
5193 bs->block_status_cache = NULL;
5196 assert(QLIST_EMPTY(&bs->dirty_bitmaps));
5198 QLIST_FOREACH_SAFE(ban, &bs->aio_notifiers, list, ban_next) {
5201 QLIST_INIT(&bs->aio_notifiers);
5209 if (bs->quiesce_counter) {
5235 if (c->klass->stay_at_node) {
5240 * c->bs by @to would mean to create a loop.
5245 * guest device -> node A -> further backing chain...
5253 * guest device -> node A -> further backing chain...
5256 * replace all pointers to A by pointers to B -- except for the
5260 * guest device -> node B
5263 * node A -> further backing chain...
5265 * In general, when replacing a node A (c->bs) by a node B (@to),
5273 * if @c is found (by breadth-first search) anywhere in the whole
5287 QLIST_FOREACH(c2, &v->children, next) {
5293 if (g_hash_table_contains(found, c2->bs)) {
5297 g_queue_push_tail(queue, c2->bs);
5298 g_hash_table_add(found, c2->bs);
5321 * @child->bs (if non-NULL) must be drained.
5332 if (child->bs) {
5333 assert(child->quiesced_parent);
5341 * Both @from and @to (if non-NULL) must be drained. @to must be kept drained
5357 assert(from->quiesce_counter);
5358 assert(to->quiesce_counter);
5360 QLIST_FOREACH_SAFE(c, &from->parents, next_parent, next) {
5361 assert(c->bs == from);
5367 c->name, from->node_name);
5368 return -EINVAL;
5370 if (c->frozen) {
5372 c->name, from->node_name);
5373 return -EPERM;
5382 * Switch all parents of @from to point to @to instead. @from and @to must be in
5386 * if it creates a parent-child relation loop or if parent is block-job.
5392 * case backing link of the cow-parent of @to is removed.
5405 assert(from->quiesce_counter);
5406 assert(to->quiesce_counter);
5423 * permissions based on new graph. If we fail, we'll roll-back the
5499 assert(!bs_new->backing);
5508 ret = -EINVAL;
5534 BlockDriverState *old_bs = child->bs;
5563 assert(!bs->refcnt);
5567 if (bs->node_name[0] != '\0') {
5574 qemu_mutex_destroy(&bs->reqs_lock);
5613 node_name = qdict_get_try_str(options, "node-name");
5658 * free of errors) or -errno when an internal error occurred. The results of the
5666 if (bs->drv == NULL) {
5667 return -ENOMEDIUM;
5669 if (bs->drv->bdrv_co_check == NULL) {
5670 return -ENOTSUP;
5674 return bs->drv->bdrv_co_check(bs, res, fix);
5679 * 0 - success
5680 * -EINVAL - backing format specified, but no file
5681 * -ENOSPC - can't update the backing file because no space is left in the
5683 * -ENOTSUP - format driver doesn't support changing the backing file
5689 BlockDriver *drv = bs->drv;
5695 return -ENOMEDIUM;
5700 return -EINVAL;
5704 return -EINVAL;
5707 if (drv->bdrv_co_change_backing_file != NULL) {
5708 ret = drv->bdrv_co_change_backing_file(bs, backing_file, backing_fmt);
5710 ret = -ENOTSUP;
5714 pstrcpy(bs->backing_file, sizeof(bs->backing_file), backing_file ?: "");
5715 pstrcpy(bs->backing_format, sizeof(bs->backing_format), backing_fmt ?: "");
5716 pstrcpy(bs->auto_backing_file, sizeof(bs->auto_backing_file),
5723 * Finds the first non-filter node above bs in the chain between
5777 if (child && child->frozen) {
5779 child->name, i->node_name, child->bs->node_name);
5803 return -EPERM;
5808 if (child && child->bs->never_freeze) {
5810 child->name, child->bs->node_name);
5811 return -EPERM;
5818 child->frozen = true;
5841 assert(child->frozen);
5842 child->frozen = false;
5855 * bottom <- base <- intermediate <- top <- active
5859 * bottom <- base <- active
5863 * base <- intermediate <- top <- active
5867 * base <- active
5869 * If backing_file_str is non-NULL, it will be used when modifying top's
5884 int ret = -EIO;
5894 if (!top->drv || !base->drv) {
5904 * base->inherits_from to top->inherits_from after 'top' and all
5911 /* success - we can delete the intermediate states, and link top->base */
5914 backing_file_str = base->filename;
5917 QLIST_FOREACH(c, &top->parents, next_parent) {
5923 * one more yet not fixed bug, when due to nested aio_poll loop we switch to
5928 * test-bdrv-drain. test_drop_intermediate_poll() test-case will crash.
5939 for (p = updated_children; p; p = p->next) {
5940 c = p->data;
5942 if (c->klass->update_filename) {
5943 ret = c->klass->update_filename(c, base, backing_file_str,
5952 * Note, that c->klass->update_filename may lead to permission
5963 base->inherits_from = explicit_top->inherits_from;
5979 * sums the size of all data-bearing children. (This excludes backing
5988 QLIST_FOREACH(child, &bs->children, next) {
5989 if (child->role & (BDRV_CHILD_DATA | BDRV_CHILD_METADATA |
5992 child_size = bdrv_co_get_allocated_file_size(child->bs);
6009 BlockDriver *drv = bs->drv;
6014 return -ENOMEDIUM;
6016 if (drv->bdrv_co_get_allocated_file_size) {
6017 return drv->bdrv_co_get_allocated_file_size(bs);
6020 if (drv->protocol_name) {
6022 * Protocol drivers default to -ENOTSUP (most of their data is
6026 return -ENOTSUP;
6027 } else if (drv->is_filter) {
6028 /* Filter drivers default to the size of their filtered child */
6031 /* Other drivers default to summing their children's sizes */
6063 if (!drv->bdrv_measure) {
6065 drv->format_name);
6069 return drv->bdrv_measure(opts, in_bs, errp);
6073 * Return number of sectors on success, -errno on error.
6077 BlockDriver *drv = bs->drv;
6082 return -ENOMEDIUM;
6084 if (bs->bl.has_variable_length) {
6085 int ret = bdrv_co_refresh_total_sectors(bs, bs->total_sectors);
6090 return bs->total_sectors;
6099 BlockDriver *drv = bs->drv;
6103 return -ENOMEDIUM;
6105 if (bs->bl.has_variable_length) {
6106 int ret = bdrv_refresh_total_sectors(bs, bs->total_sectors);
6112 return bs->total_sectors;
6116 * Return length in bytes on success, -errno on error.
6130 return -EFBIG;
6138 return bs->sg;
6149 if (!bs->drv || !block_driver_can_compress(bs->drv)) {
6168 return bs->drv ? bs->drv->format_name : NULL;
6187 if (drv->format_name) {
6196 found = !strcmp(formats[--i], drv->format_name);
6201 formats[count++] = drv->format_name;
6219 found = !strcmp(formats[--j], format_name);
6247 if (!strcmp(node_name, bs->node_name)) {
6286 gr->graph = g_new0(XDbgBlockGraph, 1);
6287 gr->graph_nodes = g_hash_table_new(NULL, NULL);
6294 XDbgBlockGraph *graph = gr->graph;
6296 g_hash_table_destroy(gr->graph_nodes);
6304 uintptr_t ret = (uintptr_t)g_hash_table_lookup(gr->graph_nodes, node);
6311 * Start counting from 1, not 0, because 0 interferes with not-found (NULL)
6314 ret = g_hash_table_size(gr->graph_nodes) + 1;
6315 g_hash_table_insert(gr->graph_nodes, node, (void *)ret);
6327 n->id = xdbg_graph_node_num(gr, node);
6328 n->type = type;
6329 n->name = g_strdup(name);
6331 QAPI_LIST_PREPEND(gr->graph->nodes, n);
6343 edge->parent = xdbg_graph_node_num(gr, parent);
6344 edge->child = xdbg_graph_node_num(gr, child->bs);
6345 edge->name = g_strdup(child->name);
6350 if (flag & child->perm) {
6351 QAPI_LIST_PREPEND(edge->perm, qapi_perm);
6353 if (flag & child->shared_perm) {
6354 QAPI_LIST_PREPEND(edge->shared_perm, qapi_perm);
6358 QAPI_LIST_PREPEND(gr->graph->edges, edge);
6393 job->job.id);
6394 for (el = job->nodes; el; el = el->next) {
6395 xdbg_graph_add_edge(gr, job, (BdrvChild *)el->data);
6402 bs->node_name);
6403 QLIST_FOREACH(child, &bs->children, next) {
6441 error_setg(errp, "Cannot find device=\'%s\' nor node-name=\'%s\'",
6482 return bs->node_name;
6492 QLIST_FOREACH(c, &bs->parents, next_parent) {
6493 if (c->klass->get_name) {
6494 name = c->klass->get_name(c);
6504 /* TODO check what callers really want: bs->node_name or blk_name() */
6514 * absent, then this returns an empty (non-null) string. */
6518 return bdrv_get_parent_name(bs) ?: bs->node_name;
6524 return bs->open_flags;
6538 if (!bs->drv) {
6547 if (bs->drv->bdrv_has_zero_init) {
6548 return bs->drv->bdrv_has_zero_init(bs);
6556 /* safe default */
6563 if (!(bs->open_flags & BDRV_O_UNMAP)) {
6567 return bs->supported_zero_flags & BDRV_REQ_MAY_UNMAP;
6574 pstrcpy(filename, filename_size, bs->backing_file);
6580 BlockDriver *drv = bs->drv;
6584 /* if bs->drv == NULL, bs is closed, so there's nothing to do here */
6586 return -ENOMEDIUM;
6588 if (!drv->bdrv_co_get_info) {
6593 return -ENOTSUP;
6596 ret = drv->bdrv_co_get_info(bs, bdi);
6597 if (bdi->subcluster_size == 0) {
6602 bdi->subcluster_size = bdi->cluster_size;
6608 if (bdi->cluster_size > BDRV_MAX_ALIGNMENT) {
6609 return -EINVAL;
6618 BlockDriver *drv = bs->drv;
6620 if (drv && drv->bdrv_get_specific_info) {
6621 return drv->bdrv_get_specific_info(bs, errp);
6628 BlockDriver *drv = bs->drv;
6630 if (!drv || !drv->bdrv_get_specific_stats) {
6633 return drv->bdrv_get_specific_stats(bs);
6641 if (!bs || !bs->drv || !bs->drv->bdrv_co_debug_event) {
6645 bs->drv->bdrv_co_debug_event(bs, event);
6652 while (bs && bs->drv && !bs->drv->bdrv_debug_breakpoint) {
6656 if (bs && bs->drv && bs->drv->bdrv_debug_breakpoint) {
6657 assert(bs->drv->bdrv_debug_remove_breakpoint);
6672 return bs->drv->bdrv_debug_breakpoint(bs, event, tag);
6675 return -ENOTSUP;
6685 return bs->drv->bdrv_debug_remove_breakpoint(bs, tag);
6688 return -ENOTSUP;
6696 while (bs && (!bs->drv || !bs->drv->bdrv_debug_resume)) {
6700 if (bs && bs->drv && bs->drv->bdrv_debug_resume) {
6701 return bs->drv->bdrv_debug_resume(bs, tag);
6704 return -ENOTSUP;
6712 while (bs && bs->drv && !bs->drv->bdrv_debug_is_suspended) {
6716 if (bs && bs->drv && bs->drv->bdrv_debug_is_suspended) {
6717 return bs->drv->bdrv_debug_is_suspended(bs, tag);
6724 * relative, it must be relative to the chain. So, passing in bs->filename
6742 if (!bs || !bs->drv || !backing_file) {
6779 if (strcmp(backing_file, bs_below->filename) == 0) {
6783 } else if (is_protocol || path_has_protocol(curr_bs->backing_file)) {
6790 if (strcmp(backing_file, curr_bs->backing_file) == 0) {
6853 return bs->open_flags & BDRV_O_INACTIVE;
6866 if (!bs->drv) {
6867 return -ENOMEDIUM;
6870 QLIST_FOREACH(child, &bs->children, next) {
6871 bdrv_activate(child->bs, &local_err);
6874 return -EINVAL;
6891 if (bs->open_flags & BDRV_O_INACTIVE) {
6892 bs->open_flags &= ~BDRV_O_INACTIVE;
6895 bs->open_flags |= BDRV_O_INACTIVE;
6901 bs->open_flags |= BDRV_O_INACTIVE;
6909 ret = bdrv_refresh_total_sectors(bs, bs->total_sectors);
6911 bs->open_flags |= BDRV_O_INACTIVE;
6912 error_setg_errno(errp, -ret, "Could not refresh total sector count");
6917 QLIST_FOREACH(parent, &bs->parents, next_parent) {
6918 if (parent->klass->activate) {
6919 parent->klass->activate(parent, &local_err);
6921 bs->open_flags |= BDRV_O_INACTIVE;
6923 return -EINVAL;
6936 assert(!(bs->open_flags & BDRV_O_INACTIVE));
6939 if (bs->drv->bdrv_co_invalidate_cache) {
6940 bs->drv->bdrv_co_invalidate_cache(bs, &local_err);
6943 return -EINVAL;
6975 QLIST_FOREACH(parent, &bs->parents, next_parent) {
6976 if (parent->klass->parent_is_bds) {
6977 BlockDriverState *parent_bs = parent->opaque;
6978 if (!only_active || !(parent_bs->open_flags & BDRV_O_INACTIVE)) {
6996 assert(bs->quiesce_counter > 0);
6998 if (!bs->drv) {
6999 return -ENOMEDIUM;
7012 if (bs->open_flags & BDRV_O_INACTIVE) {
7018 if (bs->drv->bdrv_inactivate) {
7019 ret = bs->drv->bdrv_inactivate(bs);
7025 QLIST_FOREACH(parent, &bs->parents, next_parent) {
7026 if (parent->klass->inactivate) {
7027 ret = parent->klass->inactivate(parent);
7038 return -EPERM;
7041 bs->open_flags |= BDRV_O_INACTIVE;
7051 QLIST_FOREACH(child, &bs->children, next) {
7052 ret = bdrv_inactivate_recurse(child->bs, false);
7070 return -EPERM;
7075 error_setg_errno(errp, -ret, "Failed to inactivate node");
7121 BlockDriver *drv = bs->drv;
7129 if (drv->bdrv_co_is_inserted) {
7130 return drv->bdrv_co_is_inserted(bs);
7132 QLIST_FOREACH(child, &bs->children, next) {
7133 if (!bdrv_co_is_inserted(child->bs)) {
7145 BlockDriver *drv = bs->drv;
7149 if (drv && drv->bdrv_co_eject) {
7150 drv->bdrv_co_eject(bs, eject_flag);
7160 BlockDriver *drv = bs->drv;
7165 if (drv && drv->bdrv_co_lock_medium) {
7166 drv->bdrv_co_lock_medium(bs, locked);
7174 bs->refcnt++;
7186 assert(bs->refcnt > 0);
7187 if (--bs->refcnt == 0) {
7226 if (!QLIST_EMPTY(&bs->op_blockers[op])) {
7227 blocker = QLIST_FIRST(&bs->op_blockers[op]);
7228 error_propagate_prepend(errp, error_copy(blocker->reason),
7243 blocker->reason = reason;
7244 QLIST_INSERT_HEAD(&bs->op_blockers[op], blocker, list);
7252 QLIST_FOREACH_SAFE(blocker, &bs->op_blockers[op], list, next) {
7253 if (blocker->reason == reason) {
7283 if (!QLIST_EMPTY(&bs->op_blockers[i])) {
7317 if (!drv->create_opts) {
7319 drv->format_name);
7323 if (!proto_drv->create_opts) {
7325 proto_drv->format_name);
7330 create_opts = qemu_opts_append(create_opts, drv->create_opts);
7331 create_opts = qemu_opts_append(create_opts, proto_drv->create_opts);
7335 /* Parse -o options */
7344 } else if (img_size != UINT64_C(-1)) {
7423 bs->drv->format_name);
7426 if (size == -1) {
7430 error_setg_errno(errp, -size, "Could not get size of '%s'",
7447 if (size == -1 &&
7449 qemu_opt_get_bool(opts, "detached-header", false))) {
7463 if (ret == -EFBIG) {
7486 return bs ? bs->aio_context : qemu_get_aio_context();
7497 * Increase bs->in_flight to ensure that this operation is completed before
7525 assert(!bs->walking_aio_notifiers);
7527 bs->walking_aio_notifiers = true;
7528 QLIST_FOREACH_SAFE(baf, &bs->aio_notifiers, list, baf_tmp) {
7529 if (baf->deleted) {
7532 baf->detach_aio_context(baf->opaque);
7535 /* Never mind iterating again to check for ->deleted. bdrv_close() will
7538 bs->walking_aio_notifiers = false;
7540 if (bs->drv && bs->drv->bdrv_detach_aio_context) {
7541 bs->drv->bdrv_detach_aio_context(bs);
7544 bs->aio_context = NULL;
7553 bs->aio_context = new_context;
7555 if (bs->drv && bs->drv->bdrv_attach_aio_context) {
7556 bs->drv->bdrv_attach_aio_context(bs, new_context);
7559 assert(!bs->walking_aio_notifiers);
7560 bs->walking_aio_notifiers = true;
7561 QLIST_FOREACH_SAFE(ban, &bs->aio_notifiers, list, ban_tmp) {
7562 if (ban->deleted) {
7565 ban->attached_aio_context(new_context, ban->opaque);
7568 bs->walking_aio_notifiers = false;
7602 if (!c->klass->change_aio_ctx) {
7608 if (!c->klass->change_aio_ctx(c, ctx, visited, tran, errp)) {
7616 * Changes the AioContext of @c->bs to @ctx and recursively for all its children
7635 return bdrv_change_aio_context(c->bs, ctx, visited, tran, errp);
7648 BlockDriverState *bs = (BlockDriverState *) state->bs;
7649 AioContext *new_context = state->new_ctx;
7684 QLIST_FOREACH(c, &bs->parents, next_parent) {
7690 QLIST_FOREACH(c, &bs->children, next) {
7702 assert(bs->quiesce_counter > 0);
7744 * Run all callbacks collected in the recursion to switch every node's
7752 return -EPERM;
7794 QLIST_INSERT_HEAD(&bs->aio_notifiers, ban, list);
7806 QLIST_FOREACH_SAFE(ban, &bs->aio_notifiers, list, ban_next) {
7807 if (ban->attached_aio_context == attached_aio_context &&
7808 ban->detach_aio_context == detach_aio_context &&
7809 ban->opaque == opaque &&
7810 ban->deleted == false)
7812 if (bs->walking_aio_notifiers) {
7813 ban->deleted = true;
7830 if (!bs->drv) {
7832 return -ENOMEDIUM;
7834 if (!bs->drv->bdrv_amend_options) {
7836 bs->drv->format_name);
7837 return -ENOTSUP;
7839 return bs->drv->bdrv_amend_options(bs, opts, status_cb,
7862 if (!bs || !bs->drv) {
7871 if (bs->drv->bdrv_recurse_can_replace) {
7872 return bs->drv->bdrv_recurse_can_replace(bs, to_replace);
7881 /* Safe default */
7904 error_setg(errp, "Failed to find node with node-name='%s'", node_name);
7921 node_name, parent_bs->node_name);
7932 * "read-zeroes" options are strong, but its "latency-ns" option is
7950 if (curopt == &global_options[ARRAY_SIZE(global_options) - 1] && bs->drv) {
7951 curopt = bs->drv->strong_runtime_opts;
7958 * Copies all strong runtime options from bs->options to the given
7962 * Returns true iff any strong option was present in bs->options (and
7973 if (!bs->drv) {
7981 if ((*option_name)[strlen(*option_name) - 1] != '.') {
7982 QObject *entry = qdict_get(bs->options, *option_name);
7991 for (entry = qdict_first(bs->options); entry;
7992 entry = qdict_next(bs->options, entry))
8014 qdict_put_str(d, "driver", bs->drv->format_name);
8022 * would result in exactly bs->backing. */
8026 if (bs->backing) {
8027 return strcmp(bs->auto_backing_file,
8028 bs->backing->bs->filename);
8032 return bs->auto_backing_file[0] != '\0';
8037 * - exact_filename: A filename which may be used for opening a block device
8041 * - full_open_options: Options which, when given when opening a block device
8044 * - filename: If exact_filename is set, it is copied here. Otherwise,
8050 BlockDriver *drv = bs->drv;
8055 bool generate_json_filename; /* Whether our default implementation should
8066 QLIST_FOREACH(child, &bs->children, next) {
8067 bdrv_refresh_filename(child->bs);
8070 if (bs->implicit) {
8072 child = QLIST_FIRST(&bs->children);
8075 pstrcpy(bs->exact_filename, sizeof(bs->exact_filename),
8076 child->bs->exact_filename);
8077 pstrcpy(bs->filename, sizeof(bs->filename), child->bs->filename);
8079 qobject_unref(bs->full_open_options);
8080 bs->full_open_options = qobject_ref(child->bs->full_open_options);
8087 if (bs->open_flags & BDRV_O_NO_IO) {
8089 * Therefore, in such a case (primarily qemu-img), we can
8100 if (drv->bdrv_gather_child_options) {
8103 drv->bdrv_gather_child_options(bs, opts, backing_overridden);
8105 QLIST_FOREACH(child, &bs->children, next) {
8106 if (child == bs->backing && !backing_overridden) {
8111 qdict_put(opts, child->name,
8112 qobject_ref(child->bs->full_open_options));
8115 if (backing_overridden && !bs->backing) {
8121 qobject_unref(bs->full_open_options);
8122 bs->full_open_options = opts;
8126 if (drv->bdrv_refresh_filename) {
8129 bs->exact_filename[0] = '\0';
8131 drv->bdrv_refresh_filename(bs);
8135 * file -- this only works for format nodes (filter nodes
8142 bs->exact_filename[0] = '\0';
8146 * - it has a filename,
8147 * - the current BDS is not a filter,
8148 * - the file is a protocol BDS, and
8149 * - opening that file (as this BDS's format) will automatically create
8151 * - the user did not significantly change this BDS's behavior with
8153 * - no non-file child of this BDS has been overridden by the user
8156 if (primary_child_bs->exact_filename[0] &&
8157 primary_child_bs->drv->protocol_name &&
8158 !drv->is_filter && !generate_json_filename)
8160 strcpy(bs->exact_filename, primary_child_bs->exact_filename);
8164 if (bs->exact_filename[0]) {
8165 pstrcpy(bs->filename, sizeof(bs->filename), bs->exact_filename);
8167 GString *json = qobject_to_json(QOBJECT(bs->full_open_options));
8168 if (snprintf(bs->filename, sizeof(bs->filename), "json:%s",
8169 json->str) >= sizeof(bs->filename)) {
8171 strcpy(bs->filename + sizeof(bs->filename) - 4, "...");
8179 BlockDriver *drv = bs->drv;
8185 error_setg(errp, "Node '%s' is ejected", bs->node_name);
8189 if (drv->bdrv_dirname) {
8190 return drv->bdrv_dirname(bs, errp);
8199 if (bs->exact_filename[0] != '\0') {
8200 return path_combine(bs->exact_filename, "");
8204 drv->format_name);
8218 if (!parent_bs->drv || !parent_bs->drv->bdrv_add_child) {
8225 * Non-zoned block drivers do not follow zoned storage constraints
8226 * (i.e. sequential writes to zones). Refuse mixing zoned and non-zoned
8229 if (!parent_bs->drv->supports_zoned_children &&
8230 child_bs->bl.zoned == BLK_Z_HM) {
8232 * The host-aware model allows zoned storage constraints and random
8233 * write. Allow mixing host-aware and non-zoned drivers. Using
8234 * host-aware device as a regular device.
8237 child_bs->bl.zoned == BLK_Z_HM ? "zoned" : "non-zoned",
8238 parent_bs->drv->supports_zoned_children ?
8243 if (!QLIST_EMPTY(&child_bs->parents)) {
8245 child_bs->node_name);
8249 parent_bs->drv->bdrv_add_child(parent_bs, child_bs, errp);
8263 if (!parent_bs->drv || !parent_bs->drv->bdrv_del_child) {
8269 QLIST_FOREACH(tmp, &parent_bs->children, next) {
8278 bdrv_get_device_or_node_name(child->bs));
8282 parent_bs->drv->bdrv_del_child(parent_bs, child, errp);
8287 BlockDriver *drv = c->bs->drv;
8291 assert(c->perm & (BLK_PERM_WRITE | BLK_PERM_WRITE_UNCHANGED));
8293 if (!drv->bdrv_make_empty) {
8295 drv->format_name);
8296 return -ENOTSUP;
8299 ret = drv->bdrv_make_empty(c->bs);
8301 error_setg_errno(errp, -ret, "Failed to empty %s",
8302 c->bs->filename);
8317 if (!bs || !bs->drv) {
8321 if (bs->drv->is_filter) {
8325 if (!bs->backing) {
8329 assert(bs->backing->role & BDRV_CHILD_COW);
8330 return bs->backing;
8342 if (!bs || !bs->drv) {
8346 if (!bs->drv->is_filter) {
8351 assert(!(bs->backing && bs->file));
8353 c = bs->backing ?: bs->file;
8358 assert(c->role & BDRV_CHILD_FILTERED);
8364 * whichever is non-NULL.
8395 QLIST_FOREACH(c, &bs->children, next) {
8396 if (c->role & BDRV_CHILD_PRIMARY) {
8414 while (!(stop_on_explicit_filter && !bs->implicit)) {
8423 assert(!bs->drv || !bs->drv->is_filter);
8426 bs = c->bs;
8429 * Note that this treats nodes with bs->drv == NULL as not being
8430 * filters (bs->drv == NULL should be replaced by something else
8433 * always return a non-NULL value (given a non-NULL @bs).
8461 * For a backing chain, return the first non-filter backing image of
8462 * the first non-filter image.
8472 * block-status data region.
8474 * If so, and @pnum is not NULL, set *pnum to `bsc.data_end - offset`,
8482 BdrvBlockStatusCache *bsc = qatomic_rcu_read(&bs->block_status_cache);
8486 qatomic_read(&bsc->valid) &&
8487 ranges_overlap(offset, bytes, bsc->data_start,
8488 bsc->data_end - bsc->data_start);
8491 *pnum = bsc->data_end - offset;
8517 qatomic_set(&bs->block_status_cache->valid, false);
8536 QEMU_LOCK_GUARD(&bs->bsc_modify_lock);
8538 old_bsc = qatomic_rcu_read(&bs->block_status_cache);
8539 qatomic_rcu_set(&bs->block_status_cache, new_bsc);