Lines Matching +full:closed +full:- +full:loop
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.
618 error_setg_errno(errp, -ret,
655 return -EINVAL;
661 return -ENOTSUP;
665 qdict_put_str(options, "driver", drv->format_name);
674 drv->format_name);
675 return -EINVAL;
707 return -ENOENT;
710 if (!drv->create_opts) {
712 drv->format_name);
713 return -ENOTSUP;
721 * in 'opts->list'. So if the protocol has options with the same name
731 protocol_opts = qemu_opts_from_qdict(drv->create_opts, qdict, errp);
733 ret = -EINVAL;
753 if (!bs->drv) {
754 error_setg(errp, "Block node '%s' is not opened", bs->filename);
755 return -ENOMEDIUM;
758 if (!bs->drv->bdrv_co_delete_file) {
760 bs->drv->format_name);
761 return -ENOTSUP;
764 ret = bs->drv->bdrv_co_delete_file(bs, &local_err);
788 if (ret == -ENOTSUP) {
798 * On failure return -errno.
803 BlockDriver *drv = bs->drv;
807 if (drv && drv->bdrv_probe_blocksizes) {
808 return drv->bdrv_probe_blocksizes(bs, bsz);
813 return -ENOTSUP;
819 * On failure return -errno.
824 BlockDriver *drv = bs->drv;
830 if (drv && drv->bdrv_probe_geometry) {
831 return drv->bdrv_probe_geometry(bs, geo);
839 return -ENOTSUP;
843 * Create a uniquely-named empty temporary file.
859 * See commit 69bef79 ("block: use /var/tmp instead of /tmp for -snapshot")
861 * This function is used to create temporary disk images (like -snapshot),
893 if (d->bdrv_probe_device) {
894 score = d->bdrv_probe_device(filename);
911 if (drv1->protocol_name && !strcmp(drv1->protocol_name, protocol)) {
935 * Thanks to the brain-dead persistent naming schemes on udev-
949 len = p - filename;
950 if (len > sizeof(protocol) - 1)
951 len = sizeof(protocol) - 1;
981 * This is not a good idea when your image is raw (CVE-2008-2004), but
1001 if (d->bdrv_probe) {
1002 score = d->bdrv_probe(buf, buf_size, filename);
1022 /* Return the raw BlockDriver * to scsi-generic devices or empty drives */
1030 error_setg_errno(errp, -ret, "Could not read image for determining its "
1041 return -ENOENT;
1050 * Return 0 on success, -errno on error.
1055 BlockDriver *drv = bs->drv;
1060 return -ENOMEDIUM;
1063 /* Do not attempt drv->bdrv_co_getlength() on scsi-generic devices */
1068 if (drv->bdrv_co_getlength) {
1069 int64_t length = drv->bdrv_co_getlength(bs);
1076 bs->total_sectors = hint;
1078 if (bs->total_sectors * BDRV_SECTOR_SIZE > BDRV_MAX_LENGTH) {
1079 return -EFBIG;
1093 if (bs->drv && bs->drv->bdrv_join_options) {
1094 bs->drv->bdrv_join_options(options, old_options);
1105 char *value = qemu_opt_get_del(opts, "detect-zeroes");
1119 error_setg(errp, "setting detect-zeroes to unmap is not allowed "
1129 * Return 0 on success, -1 if the engine specified is invalid
1142 return -1;
1151 * Return 0 on success, -1 if the discard mode was invalid.
1162 return -1;
1171 * Return 0 on success, -1 if the cache mode was invalid.
1191 return -1;
1199 BlockDriverState *parent = c->opaque;
1205 BlockDriverState *bs = child->opaque;
1211 BlockDriverState *bs = child->opaque;
1217 BlockDriverState *bs = child->opaque;
1223 BlockDriverState *bs = child->opaque;
1225 assert(bs->open_flags & BDRV_O_INACTIVE);
1234 BlockDriverState *bs = child->opaque;
1253 /* Copy the read-only and discard options from the parent */
1264 BlockDriverState *parent = c->opaque;
1265 BlockDriverState *backing_hd = c->bs;
1268 assert(!parent->backing_blocker);
1269 error_setg(&parent->backing_blocker,
1275 parent->open_flags &= ~BDRV_O_NO_BACKING;
1277 bdrv_op_block_all(backing_hd, parent->backing_blocker);
1280 parent->backing_blocker);
1282 parent->backing_blocker);
1297 parent->backing_blocker);
1299 parent->backing_blocker);
1304 BlockDriverState *parent = c->opaque;
1307 assert(parent->backing_blocker);
1308 bdrv_op_unblock_all(c->bs, parent->backing_blocker);
1309 error_free(parent->backing_blocker);
1310 parent->backing_blocker = NULL;
1318 BlockDriverState *parent = c->opaque;
1331 if (base->drv) {
1338 if (backing_mask_protocol && base->drv->protocol_name) {
1341 format_name = base->drv->format_name;
1349 error_setg_errno(errp, -ret, "Could not update backing file link");
1373 * format-probed by default?
1377 * Pure and non-filtered data children of non-format nodes should
1381 * Force-clear BDRV_O_PROTOCOL then.
1392 * metadata children in general should never be format-probed.
1393 * Force-set BDRV_O_PROTOCOL then.
1402 * If the cache mode isn't explicitly set, inherit direct and no-flush from
1410 /* backing files are opened read-only by default */
1414 /* Inherit the read-only option from the parent if it's not set */
1442 BlockDriverState *bs = child->opaque;
1445 QLIST_INSERT_HEAD(&bs->children, child, next);
1446 if (bs->drv->is_filter || (child->role & BDRV_CHILD_FILTERED)) {
1448 * Here we handle filters and block/raw-format.c when it behave like
1452 * So bs->file will be the PRIMARY child, unless the PRIMARY child goes
1453 * into bs->backing on exceptional cases; and bs->backing will be
1456 assert(!(child->role & BDRV_CHILD_COW));
1457 if (child->role & BDRV_CHILD_PRIMARY) {
1458 assert(child->role & BDRV_CHILD_FILTERED);
1459 assert(!bs->backing);
1460 assert(!bs->file);
1462 if (bs->drv->filtered_child_is_backing) {
1463 bs->backing = child;
1465 bs->file = child;
1468 assert(!(child->role & BDRV_CHILD_FILTERED));
1470 } else if (child->role & BDRV_CHILD_COW) {
1471 assert(bs->drv->supports_backing);
1472 assert(!(child->role & BDRV_CHILD_PRIMARY));
1473 assert(!bs->backing);
1474 bs->backing = child;
1476 } else if (child->role & BDRV_CHILD_PRIMARY) {
1477 assert(!bs->file);
1478 bs->file = child;
1484 BlockDriverState *bs = child->opaque;
1486 if (child->role & BDRV_CHILD_COW) {
1492 if (child == bs->backing) {
1493 assert(child != bs->file);
1494 bs->backing = NULL;
1495 } else if (child == bs->file) {
1496 bs->file = NULL;
1505 if (c->role & BDRV_CHILD_COW) {
1515 BlockDriverState *bs = c->opaque;
1539 return c->klass->get_parent_aio_context(c);
1616 error_setg(errp, "Invalid node-name: '%s'", node_name);
1622 error_setg(errp, "node-name=%s is conflicting with a device id",
1629 error_setg(errp, "Duplicate nodes with node-name='%s'", node_name);
1634 if (strlen(node_name) >= sizeof(bs->node_name)) {
1640 pstrcpy(bs->node_name, sizeof(bs->node_name), node_name);
1657 return -EINVAL;
1660 bs->drv = drv;
1661 bs->opaque = g_malloc0(drv->instance_size);
1663 assert(!drv->bdrv_needs_filename || bs->filename[0]);
1664 if (drv->bdrv_open) {
1665 ret = drv->bdrv_open(bs, options, open_flags, &local_err);
1673 } else if (bs->filename[0]) {
1674 error_setg_errno(errp, -ret, "Could not open '%s'", bs->filename);
1676 error_setg_errno(errp, -ret, "Could not open image");
1681 assert(!(bs->supported_read_flags & ~BDRV_REQ_MASK));
1682 assert(!(bs->supported_write_flags & ~BDRV_REQ_MASK));
1692 bs->supported_read_flags |= BDRV_REQ_REGISTERED_BUF;
1693 bs->supported_write_flags |= BDRV_REQ_REGISTERED_BUF;
1695 ret = bdrv_refresh_total_sectors(bs, bs->total_sectors);
1697 error_setg_errno(errp, -ret, "Could not refresh total sector count");
1707 return -EINVAL;
1712 assert(is_power_of_2(bs->bl.request_alignment));
1714 for (i = 0; i < bs->quiesce_counter; i++) {
1715 if (drv->bdrv_drain_begin) {
1716 drv->bdrv_drain_begin(bs);
1722 bs->drv = NULL;
1726 if (bs->file != NULL) {
1727 bdrv_unref_child(bs, bs->file);
1728 assert(!bs->file);
1733 g_free(bs->opaque);
1734 bs->opaque = NULL;
1757 bs->open_flags = flags;
1758 bs->options = options ?: qdict_new();
1759 bs->explicit_options = qdict_clone_shallow(bs->options);
1760 bs->opaque = NULL;
1762 update_options_from_flags(bs->options, flags);
1764 ret = bdrv_open_driver(bs, drv, node_name, bs->options, flags, errp);
1766 qobject_unref(bs->explicit_options);
1767 bs->explicit_options = NULL;
1768 qobject_unref(bs->options);
1769 bs->options = NULL;
1790 .name = "node-name",
1817 .help = "Node is opened in read-only mode",
1822 .help = "Node can become read-only if opening read-write fails",
1825 .name = "detect-zeroes",
1844 .name = "simple-create-opts",
1882 assert(bs->file == NULL);
1883 assert(options != NULL && bs->options != options);
1888 ret = -EINVAL;
1892 update_flags_from_options(&bs->open_flags, opts);
1898 bs->force_share = qemu_opt_get_bool(opts, BDRV_OPT_FORCE_SHARE, false);
1900 if (bs->force_share && (bs->open_flags & BDRV_O_RDWR)) {
1903 "=on can only be used with read-only images");
1904 ret = -EINVAL;
1913 filename = blk_bs(file)->filename;
1917 * non-string types would require more care. When @options
1918 * come from -blockdev or blockdev_add, its members are typed
1920 * -drive, they're all QString.
1925 if (drv->bdrv_needs_filename && (!filename || !filename[0])) {
1927 drv->format_name);
1928 ret = -EINVAL;
1932 trace_bdrv_open_common(bs, filename ?: "", bs->open_flags,
1933 drv->format_name);
1943 ret = -ENOTSUP;
1948 ? "Driver '%s' can only be used for read-only devices"
1950 drv->format_name);
1956 assert(qatomic_read(&bs->copy_on_read) == 0);
1958 if (bs->open_flags & BDRV_O_COPY_ON_READ) {
1962 error_setg(errp, "Can't use copy-on-read on read-only device");
1963 ret = -EINVAL;
1970 if (bdrv_parse_discard_flags(discard, &bs->open_flags) != 0) {
1972 ret = -EINVAL;
1977 bs->detect_zeroes =
1978 bdrv_parse_detect_zeroes(opts, bs->open_flags, &local_err);
1981 ret = -EINVAL;
1986 pstrcpy(bs->filename, sizeof(bs->filename), filename);
1988 bs->filename[0] = '\0';
1990 pstrcpy(bs->exact_filename, sizeof(bs->exact_filename), bs->filename);
1993 open_flags = bdrv_open_flags(bs, bs->open_flags);
1994 node_name = qemu_opt_get(opts, "node-name");
1996 assert(!drv->protocol_name || file == NULL);
2046 /* Parse json: pseudo-protocol */
2083 * Caution: while qdict_get_try_str() is fine, getting non-string
2085 * -blockdev or blockdev_add, its members are typed according to
2086 * the QAPI schema, but when they come from -drive, they're all
2094 return -ENOENT;
2098 protocol = drv->protocol_name;
2118 return -EINVAL;
2130 return -EINVAL;
2133 drvname = drv->format_name;
2137 return -EINVAL;
2143 /* Driver-specific filename parsing */
2144 if (drv && drv->bdrv_parse_filename && parse_filename) {
2145 drv->bdrv_parse_filename(filename, *options, &local_err);
2148 return -EINVAL;
2151 if (!drv->bdrv_needs_filename) {
2176 if (entry->state.bs == bs) {
2177 return entry->state.flags;
2182 return bs->open_flags;
2198 * be written to but do not count as read-only images.
2209 return c->klass->get_parent_desc(c);
2223 assert(a->bs);
2224 assert(a->bs == b->bs);
2227 if ((b->perm & a->shared_perm) == b->perm) {
2231 child_bs_name = bdrv_get_node_name(b->bs);
2234 perms = bdrv_perm_names(b->perm & ~a->shared_perm);
2240 b_user, child_bs_name, b->name,
2241 a_user, child_bs_name, a->name);
2253 * During the loop we'll look at each pair twice. That's correct because
2257 QLIST_FOREACH(a, &bs->parents, next_parent) {
2258 QLIST_FOREACH(b, &bs->parents, next_parent) {
2279 assert(bs->drv && bs->drv->bdrv_child_perm);
2281 bs->drv->bdrv_child_perm(bs, c, role, reopen_queue,
2285 if (child_bs && child_bs->force_share) {
2319 QLIST_FOREACH(child, &bs->children, next) {
2320 list = bdrv_topological_dfs(list, found, child->bs);
2338 s->child->perm = s->old_perm;
2339 s->child->shared_perm = s->old_shared_perm;
2355 .old_perm = c->perm,
2356 .old_shared_perm = c->shared_perm,
2359 c->perm = perm;
2360 c->shared_perm = shared;
2371 if (bs->drv->bdrv_set_perm) {
2374 bs->drv->bdrv_set_perm(bs, cumulative_perms, cumulative_shared_perms);
2383 if (bs->drv->bdrv_abort_perm_update) {
2384 bs->drv->bdrv_abort_perm_update(bs);
2402 if (!bs->drv) {
2406 if (bs->drv->bdrv_check_perm) {
2407 int ret = bs->drv->bdrv_check_perm(bs, perm, shared_perm, errp);
2430 bdrv_schedule_unref(s->old_bs);
2436 BlockDriverState *new_bs = s->child->bs;
2441 /* old_bs reference is transparently moved from @s to @s->child */
2442 if (!s->child->bs) {
2452 bdrv_parent_drained_begin_single(s->child);
2453 assert(!bdrv_parent_drained_poll_single(s->child));
2455 assert(s->child->quiesced_parent);
2456 bdrv_replace_child_noperm(s->child, s->old_bs);
2472 * Both @child->bs and @new_bs (if non-NULL) must be drained. @new_bs must be
2486 assert(child->quiesced_parent);
2487 assert(!new_bs || new_bs->quiesce_counter);
2491 .old_bs = child->bs,
2514 BlockDriver *drv = bs->drv;
2522 /* Write permissions never work with read-only images */
2527 error_setg(errp, "Block node is read-only");
2529 error_setg(errp, "Read-only block node '%s' cannot support "
2530 "read-write users", bdrv_get_node_name(bs));
2533 return -EPERM;
2544 if ((bs->total_sectors * BDRV_SECTOR_SIZE) % bs->bl.request_alignment) {
2548 return -EPERM;
2564 if (!drv->bdrv_child_perm) {
2565 assert(QLIST_EMPTY(&bs->children));
2570 QLIST_FOREACH(c, &bs->children, next) {
2573 bdrv_child_perm(bs, c->bs, c, c->role, q,
2583 * @list is a product of bdrv_topological_dfs() (may be called several times) -
2597 for ( ; list; list = list->next) {
2598 bs = list->data;
2601 return -EINVAL;
2628 for ( ; list; list = list->next) {
2629 refresh_list = bdrv_topological_dfs(refresh_list, found, list->data);
2644 QLIST_FOREACH(c, &bs->parents, next_parent) {
2645 cumulative_perms |= c->perm;
2646 cumulative_shared_perms &= c->shared_perm;
2669 for (p = permissions; p->name; p++) {
2670 if (perm & p->perm) {
2671 if (result->len > 0) {
2674 g_string_append(result, p->name);
2720 ret = bdrv_refresh_perms(c->bs, tran, &local_err);
2725 if ((perm & ~c->perm) || (c->shared_perm & ~shared)) {
2751 bdrv_child_perm(bs, c->bs, c, c->role, NULL,
2801 if (bs->open_flags & BDRV_O_INACTIVE) {
2836 * bs->file always needs to be consistent because of the
2859 * is split into fixed-size data files).
2866 * write copied clusters on copy-on-read.
2881 if (bs->open_flags & BDRV_O_INACTIVE) {
2932 * If @new_bs is non-NULL, the parent of @child must already be drained through
2938 BlockDriverState *old_bs = child->bs;
2941 assert(!child->frozen);
2945 * child->bs, we need to make sure that its new parent is drained, too. In
2946 * other words, either child->quiesce_parent must already be true or we must
2952 * To keep things simple, we pick the first option (child->quiesce_parent
2963 assert(!new_bs || child->quiesced_parent);
2972 if (child->klass->detach) {
2973 child->klass->detach(child);
2978 child->bs = new_bs;
2981 QLIST_INSERT_HEAD(&new_bs->parents, child, next_parent);
2982 if (child->klass->attach) {
2983 child->klass->attach(child);
2992 new_bs_quiesce_counter = (new_bs ? new_bs->quiesce_counter : 0);
2993 if (!new_bs_quiesce_counter && child->quiesced_parent) {
3001 * The child must be empty (i.e. `child->bs == NULL`) and it must be
3006 assert(!child->bs);
3010 assert(!child->next.le_prev); /* not in children list */
3012 g_free(child->name);
3025 BlockDriverState *bs = s->child->bs;
3030 bdrv_replace_child_noperm(s->child, NULL);
3032 if (bdrv_get_aio_context(bs) != s->old_child_ctx) {
3033 bdrv_try_change_aio_context_locked(bs, s->old_child_ctx, NULL,
3037 if (bdrv_child_get_parent_aio_context(s->child) != s->old_parent_ctx) {
3046 ret = s->child->klass->change_aio_ctx(s->child, s->old_parent_ctx,
3056 bdrv_child_free(s->child);
3093 assert(child_class->get_parent_desc);
3125 if (ret < 0 && child_class->change_aio_ctx) {
3131 ret_child = child_class->change_aio_ctx(new_child, child_ctx,
3138 tran_finalize(aio_ctx_tran, ret_child == true ? 0 : -1);
3204 assert(parent_bs->drv);
3209 child_bs->node_name, child_name, parent_bs->node_name);
3214 child_bs->node_name, child_name, parent_bs->node_name);
3253 ret = -EINVAL;
3293 ret = -EINVAL;
3311 * Callers must ensure that child->frozen is false.
3317 BlockDriverState *child_bs = child->bs;
3332 * When the parent requiring a non-default AioContext is removed, the
3351 s->bs->inherits_from = s->old_inherits_from;
3369 .old_inherits_from = bs->inherits_from,
3375 bs->inherits_from = new_inherits_from;
3389 if (child->bs->inherits_from == root) {
3392 * child->bs goes away.
3394 QLIST_FOREACH(c, &root->children, next) {
3395 if (c != child && c->bs == child->bs) {
3400 bdrv_set_inherits_from(child->bs, NULL, tran);
3404 QLIST_FOREACH(c, &child->bs->children, next) {
3410 * Callers must ensure that child->frozen is false.
3431 QLIST_FOREACH(c, &bs->parents, next_parent) {
3432 if (c->klass->change_media) {
3433 c->klass->change_media(c, load);
3438 /* Return true if you can reach parent going through child->inherits_from
3444 child = child->inherits_from;
3451 * Return the BdrvChildRole for @bs's backing child. bs->backing is
3457 if (bs->drv && bs->drv->is_filter) {
3465 * Sets the bs->backing or bs->file link of a BDS. A new reference is created;
3490 BdrvChild *child = is_backing ? parent_bs->backing : parent_bs->file;
3495 if (!parent_bs->drv) {
3498 * qcow2 driver to never clear bs->drv and implement format corruption
3502 return -EINVAL;
3505 if (child && child->frozen) {
3507 child->name, parent_bs->node_name, child->bs->node_name);
3508 return -EPERM;
3511 if (is_backing && !parent_bs->drv->is_filter &&
3512 !parent_bs->drv->supports_backing)
3515 "files", parent_bs->drv->format_name, parent_bs->node_name);
3516 return -EINVAL;
3519 if (parent_bs->drv->is_filter) {
3531 return -EINVAL;
3533 role = child->role;
3537 assert(child->bs->quiesce_counter);
3551 return -EINVAL;
3583 assert(bs->quiesce_counter > 0);
3584 if (bs->backing) {
3585 assert(bs->backing->bs->quiesce_counter > 0);
3641 if (bs->backing != NULL) {
3651 bs->open_flags &= ~BDRV_O_NO_BACKING;
3658 * Caution: while qdict_get_try_str() is fine, getting non-string
3660 * -blockdev or blockdev_add, its members are typed according to
3661 * the QAPI schema, but when they come from -drive, they're all
3667 } else if (bs->backing_file[0] == '\0' && qdict_size(options) == 0) {
3676 * only possible with -drive anyway (otherwise the QAPI
3678 implicit_backing = !strcmp(bs->auto_backing_file, bs->backing_file);
3683 ret = -EINVAL;
3690 if (!bs->drv || !bs->drv->supports_backing) {
3691 ret = -EINVAL;
3698 bs->backing_format[0] != '\0' && !qdict_haskey(options, "driver")) {
3699 qdict_put_str(options, "driver", bs->backing_format);
3706 bs->open_flags |= BDRV_O_NO_BACKING;
3708 ret = -EINVAL;
3714 pstrcpy(bs->auto_backing_file, sizeof(bs->auto_backing_file),
3715 backing_hd->filename);
3753 * Caution: while qdict_get_try_str() is fine, getting non-string
3755 * -blockdev or blockdev_add, its members are typed according to
3756 * the QAPI schema, but when they come from -drive, they're all
3852 assert(!parent->drv->filtered_child_is_backing);
3853 role = parent->drv->is_filter ?
3859 return -EINVAL;
3879 if (ref->type == QTYPE_QSTRING) {
3880 reference = ref->u.reference;
3882 BlockdevOptions *options = &ref->u.definition;
3883 assert(ref->type == QTYPE_QDICT);
3931 error_setg_errno(errp, -total_size, "Could not get image size");
4048 bs->explicit_options = qdict_clone_shallow(options);
4053 if (parent->drv) {
4054 parent_is_format = parent->drv->is_format;
4057 * parent->drv is not set yet because this node is opened for
4064 bs->inherits_from = parent;
4065 child_class->inherit_options(child_role, parent_is_format,
4067 parent->open_flags, parent->options);
4079 * When @options come from -blockdev or blockdev_add, members are
4081 * -drive, they're all QString.
4094 /* Let bdrv_backing_options() override "read-only" */
4100 bs->open_flags = flags;
4101 bs->options = options;
4127 qdict_del(bs->explicit_options, "backing");
4128 qdict_del(bs->options, "backing");
4162 bs->probed = !drv;
4170 * but we first need to open bs->file for the probing to work, while
4171 * opening bs->file already requires the (mostly) final set of options
4176 * sure to update both bs->options (which has the full effective
4179 qdict_put_str(bs->options, "driver", drv->format_name);
4180 qdict_put_str(options, "driver", drv->format_name);
4187 assert(!!(flags & BDRV_O_PROTOCOL) == !!drv->protocol_name);
4212 * from bs->options and bs->explicit_options */
4213 QLIST_FOREACH(child, &bs->children, next) {
4215 child_key_dot = g_strdup_printf("%s.", child->name);
4216 qdict_extract_subqdict(bs->explicit_options, NULL, child_key_dot);
4217 qdict_extract_subqdict(bs->options, NULL, child_key_dot);
4218 qdict_del(bs->explicit_options, child->name);
4219 qdict_del(bs->options, child->name);
4228 "'%s'", drv->format_name, entry->key);
4232 drv->format_name, entry->key);
4266 qobject_unref(bs->explicit_options);
4267 qobject_unref(bs->options);
4269 bs->options = NULL;
4270 bs->explicit_options = NULL;
4292 /* Return true if the NULL-terminated @list contains @str */
4307 * Check that every option set in @bs->options is also set in
4311 * @bs->drv->mutable_opts are skipped.
4313 * Return 0 on success, otherwise return -EINVAL and set @errp.
4322 "node-name", "discard", "cache.direct", "cache.no-flush",
4323 "read-only", "auto-read-only", "detect-zeroes", NULL
4326 for (e = qdict_first(bs->options); e; e = qdict_next(bs->options, e)) {
4327 if (!qdict_haskey(new_opts, e->key) &&
4328 !is_str_in_list(e->key, common_options) &&
4329 !is_str_in_list(e->key, bs->drv->mutable_opts)) {
4331 "to its default value", e->key);
4332 return -EINVAL;
4351 QLIST_FOREACH(c, &bs->children, next) {
4352 if (bdrv_recurse_has_child(c->bs, child)) {
4399 assert(bs->quiesce_counter > 0);
4412 if (bs == bs_entry->state.bs) {
4428 bs_entry->state.explicit_options :
4429 bs->explicit_options);
4439 klass->inherit_options(role, parent_is_format, &flags, options,
4447 old_options = qdict_clone_shallow(bs->options);
4470 qobject_unref(bs_entry->state.options);
4471 qobject_unref(bs_entry->state.explicit_options);
4474 bs_entry->state.bs = bs;
4475 bs_entry->state.options = options;
4476 bs_entry->state.explicit_options = explicit_options;
4477 bs_entry->state.flags = flags;
4486 bs_entry->state.backing_missing =
4491 QLIST_FOREACH(child, &bs->children, next) {
4498 if (child->bs->inherits_from != bs) {
4503 if (qdict_haskey(options, child->name)) {
4504 const char *childref = qdict_get_try_str(options, child->name);
4509 if (g_strcmp0(childref, child->bs->node_name)) {
4519 /* Extract child options ("child-name.*") */
4520 char *child_key_dot = g_strdup_printf("%s.", child->name);
4526 bdrv_reopen_queue_child(bs_queue, child->bs, new_child_options,
4527 child->klass, child->role, bs->drv->is_format,
4557 qobject_unref(bs_entry->state.explicit_options);
4558 qobject_unref(bs_entry->state.options);
4589 int ret = -1;
4599 ret = bdrv_flush(bs_entry->state.bs);
4601 error_setg_errno(errp, -ret, "Error flushing drive");
4607 assert(bs_entry->state.bs->quiesce_counter > 0);
4608 ret = bdrv_reopen_prepare(&bs_entry->state, bs_queue, tran, errp);
4612 bs_entry->prepared = true;
4616 BDRVReopenState *state = &bs_entry->state;
4618 refresh_list = g_slist_prepend(refresh_list, state->bs);
4619 if (state->old_backing_bs) {
4620 refresh_list = g_slist_prepend(refresh_list, state->old_backing_bs);
4622 if (state->old_file_bs) {
4623 refresh_list = g_slist_prepend(refresh_list, state->old_file_bs);
4628 * Note that file-posix driver rely on permission update done during reopen
4647 * children are usually goes after parents in reopen-queue, so go from last
4651 bdrv_reopen_commit(&bs_entry->state);
4659 BlockDriverState *bs = bs_entry->state.bs;
4661 if (bs->drv->bdrv_reopen_commit_post) {
4662 bs->drv->bdrv_reopen_commit_post(&bs_entry->state);
4675 if (bs_entry->prepared) {
4676 bdrv_reopen_abort(&bs_entry->state);
4712 * reopen_state->options QDict is valid or not.
4717 * reopen_state->bs then return 0.
4721 * If that's the case then reopen_state->replace_backing_bs is set to
4722 * true and reopen_state->new_backing_bs contains a pointer to the new
4730 * @reopen_state->bs can move to a different AioContext in this function.
4740 BlockDriverState *bs = reopen_state->bs;
4752 value = qdict_get(reopen_state->options, child_name);
4768 ret = -EINVAL;
4775 "cycle", str, child_name, bs->node_name);
4776 ret = -EINVAL;
4788 old_child_bs = is_backing ? child_bs(bs->backing) : child_bs(bs->file);
4800 if (old_child_bs->implicit) {
4802 child_name, bs->node_name);
4803 ret = -EPERM;
4808 if (bs->drv->is_filter && !old_child_bs) {
4814 "%s child", bs->node_name, bs->drv->format_name, child_name);
4815 ret = -EINVAL;
4820 reopen_state->old_backing_bs = old_child_bs;
4822 reopen_state->old_file_bs = old_child_bs;
4827 assert(old_child_bs->quiesce_counter > 0);
4858 * Returns 0 on success, non-zero on error. On error errp will be set
4875 int ret = -1;
4886 assert(reopen_state->bs->drv != NULL);
4888 drv = reopen_state->bs->drv;
4891 * entries from reopen_state->options as they are processed, so
4893 orig_reopen_opts = qdict_clone_shallow(reopen_state->options);
4897 if (!qemu_opts_absorb_qdict(opts, reopen_state->options, errp)) {
4898 ret = -EINVAL;
4903 * are up-to-date. This time we simply want to remove the options from
4905 old_flags = reopen_state->flags;
4906 update_flags_from_options(&reopen_state->flags, opts);
4907 assert(old_flags == reopen_state->flags);
4911 if (bdrv_parse_discard_flags(discard, &reopen_state->flags) != 0) {
4913 ret = -EINVAL;
4918 reopen_state->detect_zeroes =
4919 bdrv_parse_detect_zeroes(opts, reopen_state->flags, &local_err);
4922 ret = -EINVAL;
4926 /* All other options (including node-name and driver) must be unchanged.
4929 qemu_opts_to_qdict(opts, reopen_state->options);
4931 /* If we are to stay read-only, do not allow permission change
4934 read_only = !(reopen_state->flags & BDRV_O_RDWR);
4937 ret = bdrv_can_set_read_only(reopen_state->bs, read_only, true, &local_err);
4944 if (drv->bdrv_reopen_prepare) {
4946 * If a driver-specific option is missing, it means that we
4950 ret = bdrv_reset_options_allowed(reopen_state->bs,
4951 reopen_state->options, errp);
4956 ret = drv->bdrv_reopen_prepare(reopen_state, queue, &local_err);
4962 bdrv_refresh_filename(reopen_state->bs);
4965 reopen_state->bs->filename);
4974 "does not support reopening files", drv->format_name,
4975 bdrv_get_device_or_node_name(reopen_state->bs));
4977 ret = -1;
4989 if (drv->supports_backing && reopen_state->backing_missing &&
4990 (reopen_state->bs->backing || reopen_state->bs->backing_file[0])) {
4992 reopen_state->bs->node_name);
4994 ret = -EINVAL;
5009 qdict_del(reopen_state->options, "backing");
5017 qdict_del(reopen_state->options, "file");
5022 if (qdict_size(reopen_state->options)) {
5023 const QDictEntry *entry = qdict_first(reopen_state->options);
5028 QObject *new = entry->value;
5029 QObject *old = qdict_get(reopen_state->bs->options, entry->key);
5035 QLIST_FOREACH(child, &reopen_state->bs->children, next) {
5036 if (!strcmp(child->name, entry->key)) {
5042 if (!strcmp(child->bs->node_name,
5050 * TODO: When using -drive to specify blockdev options, all values
5051 * will be strings; however, when using -blockdev, blockdev-add or
5052 * filenames using the json:{} pseudo-protocol, they will be
5055 * (because you can only specify them through qemu-io; all other
5057 * Therefore, when using anything other than -drive to create a BDS,
5058 * this cannot detect non-string options as unchanged, because
5066 error_setg(errp, "Cannot change the option '%s'", entry->key);
5067 ret = -EINVAL;
5070 } while ((entry = qdict_next(reopen_state->options, entry)));
5075 /* Restore the original reopen_state->options QDict */
5076 qobject_unref(reopen_state->options);
5077 reopen_state->options = qobject_ref(orig_reopen_opts);
5081 /* drv->bdrv_reopen_prepare() has succeeded, so we need to
5082 * call drv->bdrv_reopen_abort() before signaling an error
5085 if (drv->bdrv_reopen_abort) {
5086 drv->bdrv_reopen_abort(reopen_state);
5107 bs = reopen_state->bs;
5108 drv = bs->drv;
5113 if (drv->bdrv_reopen_commit) {
5114 drv->bdrv_reopen_commit(reopen_state);
5120 qobject_unref(bs->explicit_options);
5121 qobject_unref(bs->options);
5122 qobject_ref(reopen_state->explicit_options);
5123 qobject_ref(reopen_state->options);
5125 bs->explicit_options = reopen_state->explicit_options;
5126 bs->options = reopen_state->options;
5127 bs->open_flags = reopen_state->flags;
5128 bs->detect_zeroes = reopen_state->detect_zeroes;
5130 /* Remove child references from bs->options and bs->explicit_options.
5132 QLIST_FOREACH(child, &bs->children, next) {
5133 qdict_del(bs->explicit_options, child->name);
5134 qdict_del(bs->options, child->name);
5136 /* backing is probably removed, so it's not handled by previous loop */
5137 qdict_del(bs->explicit_options, "backing");
5138 qdict_del(bs->options, "backing");
5141 bdrv_refresh_total_sectors(bs, bs->total_sectors);
5153 drv = reopen_state->bs->drv;
5157 if (drv->bdrv_reopen_abort) {
5158 drv->bdrv_reopen_abort(reopen_state);
5169 assert(!bs->refcnt);
5175 if (bs->drv) {
5176 if (bs->drv->bdrv_close) {
5178 bs->drv->bdrv_close(bs);
5180 bs->drv = NULL;
5185 QLIST_FOREACH_SAFE(child, &bs->children, next, next) {
5189 assert(!bs->backing);
5190 assert(!bs->file);
5194 g_free(bs->opaque);
5195 bs->opaque = NULL;
5196 qatomic_set(&bs->copy_on_read, 0);
5197 bs->backing_file[0] = '\0';
5198 bs->backing_format[0] = '\0';
5199 bs->total_sectors = 0;
5200 bs->encrypted = false;
5201 bs->sg = false;
5202 qobject_unref(bs->options);
5203 qobject_unref(bs->explicit_options);
5204 bs->options = NULL;
5205 bs->explicit_options = NULL;
5206 qobject_unref(bs->full_open_options);
5207 bs->full_open_options = NULL;
5208 g_free(bs->block_status_cache);
5209 bs->block_status_cache = NULL;
5212 assert(QLIST_EMPTY(&bs->dirty_bitmaps));
5214 QLIST_FOREACH_SAFE(ban, &bs->aio_notifiers, list, ban_next) {
5217 QLIST_INIT(&bs->aio_notifiers);
5225 if (bs->quiesce_counter) {
5251 if (c->klass->stay_at_node) {
5256 * c->bs by @to would mean to create a loop.
5261 * guest device -> node A -> further backing chain...
5269 * guest device -> node A -> further backing chain...
5272 * replace all pointers to A by pointers to B -- except for the
5273 * pointer from B because (1) that would create a loop, and (2)
5276 * guest device -> node B
5279 * node A -> further backing chain...
5281 * In general, when replacing a node A (c->bs) by a node B (@to),
5283 * because that would create a loop. Silently detaching A from B
5287 * We would also create a loop in any cases where @c is only
5289 * if @c is found (by breadth-first search) anywhere in the whole
5303 QLIST_FOREACH(c2, &v->children, next) {
5309 if (g_hash_table_contains(found, c2->bs)) {
5313 g_queue_push_tail(queue, c2->bs);
5314 g_hash_table_add(found, c2->bs);
5337 * @child->bs (if non-NULL) must be drained.
5348 if (child->bs) {
5349 assert(child->quiesced_parent);
5357 * Both @from and @to (if non-NULL) must be drained. @to must be kept drained
5373 assert(from->quiesce_counter);
5374 assert(to->quiesce_counter);
5376 QLIST_FOREACH_SAFE(c, &from->parents, next_parent, next) {
5377 assert(c->bs == from);
5383 c->name, from->node_name);
5384 return -EINVAL;
5386 if (c->frozen) {
5388 c->name, from->node_name);
5389 return -EPERM;
5402 * if it creates a parent-child relation loop or if parent is block-job.
5408 * case backing link of the cow-parent of @to is removed.
5421 assert(from->quiesce_counter);
5422 assert(to->quiesce_counter);
5439 * permissions based on new graph. If we fail, we'll roll-back the
5515 assert(!bs_new->backing);
5525 ret = -EINVAL;
5552 BlockDriverState *old_bs = child->bs;
5581 assert(!bs->refcnt);
5585 if (bs->node_name[0] != '\0') {
5592 qemu_mutex_destroy(&bs->reqs_lock);
5631 node_name = qdict_get_try_str(options, "node-name");
5676 * free of errors) or -errno when an internal error occurred. The results of the
5684 if (bs->drv == NULL) {
5685 return -ENOMEDIUM;
5687 if (bs->drv->bdrv_co_check == NULL) {
5688 return -ENOTSUP;
5692 return bs->drv->bdrv_co_check(bs, res, fix);
5697 * 0 - success
5698 * -EINVAL - backing format specified, but no file
5699 * -ENOSPC - can't update the backing file because no space is left in the
5701 * -ENOTSUP - format driver doesn't support changing the backing file
5707 BlockDriver *drv = bs->drv;
5713 return -ENOMEDIUM;
5718 return -EINVAL;
5722 return -EINVAL;
5725 if (drv->bdrv_co_change_backing_file != NULL) {
5726 ret = drv->bdrv_co_change_backing_file(bs, backing_file, backing_fmt);
5728 ret = -ENOTSUP;
5732 pstrcpy(bs->backing_file, sizeof(bs->backing_file), backing_file ?: "");
5733 pstrcpy(bs->backing_format, sizeof(bs->backing_format), backing_fmt ?: "");
5734 pstrcpy(bs->auto_backing_file, sizeof(bs->auto_backing_file),
5741 * Finds the first non-filter node above bs in the chain between
5795 if (child && child->frozen) {
5797 child->name, i->node_name, child->bs->node_name);
5821 return -EPERM;
5826 if (child && child->bs->never_freeze) {
5828 child->name, child->bs->node_name);
5829 return -EPERM;
5836 child->frozen = true;
5859 assert(child->frozen);
5860 child->frozen = false;
5873 * bottom <- base <- intermediate <- top <- active
5877 * bottom <- base <- active
5881 * base <- intermediate <- top <- active
5885 * base <- active
5887 * If backing_file_str is non-NULL, it will be used when modifying top's
5902 int ret = -EIO;
5912 if (!top->drv || !base->drv) {
5922 * base->inherits_from to top->inherits_from after 'top' and all
5929 /* success - we can delete the intermediate states, and link top->base */
5932 backing_file_str = base->filename;
5935 QLIST_FOREACH(c, &top->parents, next_parent) {
5941 * one more yet not fixed bug, when due to nested aio_poll loop we switch to
5946 * test-bdrv-drain. test_drop_intermediate_poll() test-case will crash.
5957 for (p = updated_children; p; p = p->next) {
5958 c = p->data;
5960 if (c->klass->update_filename) {
5961 ret = c->klass->update_filename(c, base, backing_file_str,
5967 * of this loop, and (which is almost impossible) previous
5970 * Note, that c->klass->update_filename may lead to permission
5981 base->inherits_from = explicit_top->inherits_from;
5997 * sums the size of all data-bearing children. (This excludes backing
6006 QLIST_FOREACH(child, &bs->children, next) {
6007 if (child->role & (BDRV_CHILD_DATA | BDRV_CHILD_METADATA |
6010 child_size = bdrv_co_get_allocated_file_size(child->bs);
6027 BlockDriver *drv = bs->drv;
6032 return -ENOMEDIUM;
6034 if (drv->bdrv_co_get_allocated_file_size) {
6035 return drv->bdrv_co_get_allocated_file_size(bs);
6038 if (drv->protocol_name) {
6040 * Protocol drivers default to -ENOTSUP (most of their data is
6044 return -ENOTSUP;
6045 } else if (drv->is_filter) {
6081 if (!drv->bdrv_measure) {
6083 drv->format_name);
6087 return drv->bdrv_measure(opts, in_bs, errp);
6091 * Return number of sectors on success, -errno on error.
6095 BlockDriver *drv = bs->drv;
6100 return -ENOMEDIUM;
6102 if (bs->bl.has_variable_length) {
6103 int ret = bdrv_co_refresh_total_sectors(bs, bs->total_sectors);
6108 return bs->total_sectors;
6117 BlockDriver *drv = bs->drv;
6121 return -ENOMEDIUM;
6123 if (bs->bl.has_variable_length) {
6124 int ret = bdrv_refresh_total_sectors(bs, bs->total_sectors);
6130 return bs->total_sectors;
6134 * Return length in bytes on success, -errno on error.
6148 return -EFBIG;
6156 return bs->sg;
6167 if (!bs->drv || !block_driver_can_compress(bs->drv)) {
6186 return bs->drv ? bs->drv->format_name : NULL;
6205 if (drv->format_name) {
6214 found = !strcmp(formats[--i], drv->format_name);
6219 formats[count++] = drv->format_name;
6237 found = !strcmp(formats[--j], format_name);
6265 if (!strcmp(node_name, bs->node_name)) {
6304 gr->graph = g_new0(XDbgBlockGraph, 1);
6305 gr->graph_nodes = g_hash_table_new(NULL, NULL);
6312 XDbgBlockGraph *graph = gr->graph;
6314 g_hash_table_destroy(gr->graph_nodes);
6322 uintptr_t ret = (uintptr_t)g_hash_table_lookup(gr->graph_nodes, node);
6329 * Start counting from 1, not 0, because 0 interferes with not-found (NULL)
6332 ret = g_hash_table_size(gr->graph_nodes) + 1;
6333 g_hash_table_insert(gr->graph_nodes, node, (void *)ret);
6345 n->id = xdbg_graph_node_num(gr, node);
6346 n->type = type;
6347 n->name = g_strdup(name);
6349 QAPI_LIST_PREPEND(gr->graph->nodes, n);
6361 edge->parent = xdbg_graph_node_num(gr, parent);
6362 edge->child = xdbg_graph_node_num(gr, child->bs);
6363 edge->name = g_strdup(child->name);
6368 if (flag & child->perm) {
6369 QAPI_LIST_PREPEND(edge->perm, qapi_perm);
6371 if (flag & child->shared_perm) {
6372 QAPI_LIST_PREPEND(edge->shared_perm, qapi_perm);
6376 QAPI_LIST_PREPEND(gr->graph->edges, edge);
6411 job->job.id);
6412 for (el = job->nodes; el; el = el->next) {
6413 xdbg_graph_add_edge(gr, job, (BdrvChild *)el->data);
6420 bs->node_name);
6421 QLIST_FOREACH(child, &bs->children, next) {
6459 error_setg(errp, "Cannot find device=\'%s\' nor node-name=\'%s\'",
6500 return bs->node_name;
6510 QLIST_FOREACH(c, &bs->parents, next_parent) {
6511 if (c->klass->get_name) {
6512 name = c->klass->get_name(c);
6522 /* TODO check what callers really want: bs->node_name or blk_name() */
6532 * absent, then this returns an empty (non-null) string. */
6536 return bdrv_get_parent_name(bs) ?: bs->node_name;
6542 return bs->open_flags;
6556 if (!bs->drv) {
6565 if (bs->drv->bdrv_has_zero_init) {
6566 return bs->drv->bdrv_has_zero_init(bs);
6581 if (!(bs->open_flags & BDRV_O_UNMAP)) {
6585 return bs->supported_zero_flags & BDRV_REQ_MAY_UNMAP;
6592 pstrcpy(filename, filename_size, bs->backing_file);
6598 BlockDriver *drv = bs->drv;
6602 /* if bs->drv == NULL, bs is closed, so there's nothing to do here */
6604 return -ENOMEDIUM;
6606 if (!drv->bdrv_co_get_info) {
6611 return -ENOTSUP;
6614 ret = drv->bdrv_co_get_info(bs, bdi);
6615 if (bdi->subcluster_size == 0) {
6620 bdi->subcluster_size = bdi->cluster_size;
6626 if (bdi->cluster_size > BDRV_MAX_ALIGNMENT) {
6627 return -EINVAL;
6636 BlockDriver *drv = bs->drv;
6638 if (drv && drv->bdrv_get_specific_info) {
6639 return drv->bdrv_get_specific_info(bs, errp);
6646 BlockDriver *drv = bs->drv;
6648 if (!drv || !drv->bdrv_get_specific_stats) {
6651 return drv->bdrv_get_specific_stats(bs);
6659 if (!bs || !bs->drv || !bs->drv->bdrv_co_debug_event) {
6663 bs->drv->bdrv_co_debug_event(bs, event);
6670 while (bs && bs->drv && !bs->drv->bdrv_debug_breakpoint) {
6674 if (bs && bs->drv && bs->drv->bdrv_debug_breakpoint) {
6675 assert(bs->drv->bdrv_debug_remove_breakpoint);
6690 return bs->drv->bdrv_debug_breakpoint(bs, event, tag);
6693 return -ENOTSUP;
6703 return bs->drv->bdrv_debug_remove_breakpoint(bs, tag);
6706 return -ENOTSUP;
6714 while (bs && (!bs->drv || !bs->drv->bdrv_debug_resume)) {
6718 if (bs && bs->drv && bs->drv->bdrv_debug_resume) {
6719 return bs->drv->bdrv_debug_resume(bs, tag);
6722 return -ENOTSUP;
6730 while (bs && bs->drv && !bs->drv->bdrv_debug_is_suspended) {
6734 if (bs && bs->drv && bs->drv->bdrv_debug_is_suspended) {
6735 return bs->drv->bdrv_debug_is_suspended(bs, tag);
6742 * relative, it must be relative to the chain. So, passing in bs->filename
6760 if (!bs || !bs->drv || !backing_file) {
6797 if (strcmp(backing_file, bs_below->filename) == 0) {
6801 } else if (is_protocol || path_has_protocol(curr_bs->backing_file)) {
6808 if (strcmp(backing_file, curr_bs->backing_file) == 0) {
6871 return bs->open_flags & BDRV_O_INACTIVE;
6884 if (!bs->drv) {
6885 return -ENOMEDIUM;
6888 QLIST_FOREACH(child, &bs->children, next) {
6889 bdrv_activate(child->bs, &local_err);
6892 return -EINVAL;
6909 if (bs->open_flags & BDRV_O_INACTIVE) {
6910 bs->open_flags &= ~BDRV_O_INACTIVE;
6913 bs->open_flags |= BDRV_O_INACTIVE;
6919 bs->open_flags |= BDRV_O_INACTIVE;
6927 ret = bdrv_refresh_total_sectors(bs, bs->total_sectors);
6929 bs->open_flags |= BDRV_O_INACTIVE;
6930 error_setg_errno(errp, -ret, "Could not refresh total sector count");
6935 QLIST_FOREACH(parent, &bs->parents, next_parent) {
6936 if (parent->klass->activate) {
6937 parent->klass->activate(parent, &local_err);
6939 bs->open_flags |= BDRV_O_INACTIVE;
6941 return -EINVAL;
6954 assert(!(bs->open_flags & BDRV_O_INACTIVE));
6957 if (bs->drv->bdrv_co_invalidate_cache) {
6958 bs->drv->bdrv_co_invalidate_cache(bs, &local_err);
6961 return -EINVAL;
6993 QLIST_FOREACH(parent, &bs->parents, next_parent) {
6994 if (parent->klass->parent_is_bds) {
6995 BlockDriverState *parent_bs = parent->opaque;
6996 if (!only_active || !(parent_bs->open_flags & BDRV_O_INACTIVE)) {
7014 assert(bs->quiesce_counter > 0);
7016 if (!bs->drv) {
7017 return -ENOMEDIUM;
7030 if (bs->open_flags & BDRV_O_INACTIVE) {
7036 if (bs->drv->bdrv_inactivate) {
7037 ret = bs->drv->bdrv_inactivate(bs);
7043 QLIST_FOREACH(parent, &bs->parents, next_parent) {
7044 if (parent->klass->inactivate) {
7045 ret = parent->klass->inactivate(parent);
7056 return -EPERM;
7059 bs->open_flags |= BDRV_O_INACTIVE;
7069 QLIST_FOREACH(child, &bs->children, next) {
7070 ret = bdrv_inactivate_recurse(child->bs, false);
7090 ret = -EPERM;
7096 error_setg_errno(errp, -ret, "Failed to inactivate node");
7145 BlockDriver *drv = bs->drv;
7153 if (drv->bdrv_co_is_inserted) {
7154 return drv->bdrv_co_is_inserted(bs);
7156 QLIST_FOREACH(child, &bs->children, next) {
7157 if (!bdrv_co_is_inserted(child->bs)) {
7169 BlockDriver *drv = bs->drv;
7173 if (drv && drv->bdrv_co_eject) {
7174 drv->bdrv_co_eject(bs, eject_flag);
7184 BlockDriver *drv = bs->drv;
7189 if (drv && drv->bdrv_co_lock_medium) {
7190 drv->bdrv_co_lock_medium(bs, locked);
7198 bs->refcnt++;
7210 assert(bs->refcnt > 0);
7211 if (--bs->refcnt == 0) {
7250 if (!QLIST_EMPTY(&bs->op_blockers[op])) {
7251 blocker = QLIST_FIRST(&bs->op_blockers[op]);
7252 error_propagate_prepend(errp, error_copy(blocker->reason),
7267 blocker->reason = reason;
7268 QLIST_INSERT_HEAD(&bs->op_blockers[op], blocker, list);
7276 QLIST_FOREACH_SAFE(blocker, &bs->op_blockers[op], list, next) {
7277 if (blocker->reason == reason) {
7307 if (!QLIST_EMPTY(&bs->op_blockers[i])) {
7341 if (!drv->create_opts) {
7343 drv->format_name);
7347 if (!proto_drv->create_opts) {
7349 proto_drv->format_name);
7354 create_opts = qemu_opts_append(create_opts, drv->create_opts);
7355 create_opts = qemu_opts_append(create_opts, proto_drv->create_opts);
7359 /* Parse -o options */
7368 } else if (img_size != UINT64_C(-1)) {
7447 bs->drv->format_name);
7450 if (size == -1) {
7454 error_setg_errno(errp, -size, "Could not get size of '%s'",
7471 if (size == -1 &&
7473 qemu_opt_get_bool(opts, "detached-header", false))) {
7487 if (ret == -EFBIG) {
7510 return bs ? bs->aio_context : qemu_get_aio_context();
7521 * Increase bs->in_flight to ensure that this operation is completed before
7549 assert(!bs->walking_aio_notifiers);
7551 bs->walking_aio_notifiers = true;
7552 QLIST_FOREACH_SAFE(baf, &bs->aio_notifiers, list, baf_tmp) {
7553 if (baf->deleted) {
7556 baf->detach_aio_context(baf->opaque);
7559 /* Never mind iterating again to check for ->deleted. bdrv_close() will
7562 bs->walking_aio_notifiers = false;
7564 if (bs->drv && bs->drv->bdrv_detach_aio_context) {
7565 bs->drv->bdrv_detach_aio_context(bs);
7568 bs->aio_context = NULL;
7577 bs->aio_context = new_context;
7579 if (bs->drv && bs->drv->bdrv_attach_aio_context) {
7580 bs->drv->bdrv_attach_aio_context(bs, new_context);
7583 assert(!bs->walking_aio_notifiers);
7584 bs->walking_aio_notifiers = true;
7585 QLIST_FOREACH_SAFE(ban, &bs->aio_notifiers, list, ban_tmp) {
7586 if (ban->deleted) {
7589 ban->attached_aio_context(new_context, ban->opaque);
7592 bs->walking_aio_notifiers = false;
7626 if (!c->klass->change_aio_ctx) {
7632 if (!c->klass->change_aio_ctx(c, ctx, visited, tran, errp)) {
7640 * Changes the AioContext of @c->bs to @ctx and recursively for all its children
7659 return bdrv_change_aio_context(c->bs, ctx, visited, tran, errp);
7672 BlockDriverState *bs = (BlockDriverState *) state->bs;
7673 AioContext *new_context = state->new_ctx;
7708 QLIST_FOREACH(c, &bs->parents, next_parent) {
7714 QLIST_FOREACH(c, &bs->children, next) {
7726 assert(bs->quiesce_counter > 0);
7776 return -EPERM;
7818 QLIST_INSERT_HEAD(&bs->aio_notifiers, ban, list);
7830 QLIST_FOREACH_SAFE(ban, &bs->aio_notifiers, list, ban_next) {
7831 if (ban->attached_aio_context == attached_aio_context &&
7832 ban->detach_aio_context == detach_aio_context &&
7833 ban->opaque == opaque &&
7834 ban->deleted == false)
7836 if (bs->walking_aio_notifiers) {
7837 ban->deleted = true;
7854 if (!bs->drv) {
7856 return -ENOMEDIUM;
7858 if (!bs->drv->bdrv_amend_options) {
7860 bs->drv->format_name);
7861 return -ENOTSUP;
7863 return bs->drv->bdrv_amend_options(bs, opts, status_cb,
7886 if (!bs || !bs->drv) {
7895 if (bs->drv->bdrv_recurse_can_replace) {
7896 return bs->drv->bdrv_recurse_can_replace(bs, to_replace);
7928 error_setg(errp, "Failed to find node with node-name='%s'", node_name);
7945 node_name, parent_bs->node_name);
7956 * "read-zeroes" options are strong, but its "latency-ns" option is
7974 if (curopt == &global_options[ARRAY_SIZE(global_options) - 1] && bs->drv) {
7975 curopt = bs->drv->strong_runtime_opts;
7982 * Copies all strong runtime options from bs->options to the given
7986 * Returns true iff any strong option was present in bs->options (and
7997 if (!bs->drv) {
8005 if ((*option_name)[strlen(*option_name) - 1] != '.') {
8006 QObject *entry = qdict_get(bs->options, *option_name);
8015 for (entry = qdict_first(bs->options); entry;
8016 entry = qdict_next(bs->options, entry))
8038 qdict_put_str(d, "driver", bs->drv->format_name);
8046 * would result in exactly bs->backing. */
8050 if (bs->backing) {
8051 return strcmp(bs->auto_backing_file,
8052 bs->backing->bs->filename);
8056 return bs->auto_backing_file[0] != '\0';
8061 * - exact_filename: A filename which may be used for opening a block device
8065 * - full_open_options: Options which, when given when opening a block device
8068 * - filename: If exact_filename is set, it is copied here. Otherwise,
8074 BlockDriver *drv = bs->drv;
8090 QLIST_FOREACH(child, &bs->children, next) {
8091 bdrv_refresh_filename(child->bs);
8094 if (bs->implicit) {
8096 child = QLIST_FIRST(&bs->children);
8099 pstrcpy(bs->exact_filename, sizeof(bs->exact_filename),
8100 child->bs->exact_filename);
8101 pstrcpy(bs->filename, sizeof(bs->filename), child->bs->filename);
8103 qobject_unref(bs->full_open_options);
8104 bs->full_open_options = qobject_ref(child->bs->full_open_options);
8111 if (bs->open_flags & BDRV_O_NO_IO) {
8113 * Therefore, in such a case (primarily qemu-img), we can
8124 if (drv->bdrv_gather_child_options) {
8127 drv->bdrv_gather_child_options(bs, opts, backing_overridden);
8129 QLIST_FOREACH(child, &bs->children, next) {
8130 if (child == bs->backing && !backing_overridden) {
8135 qdict_put(opts, child->name,
8136 qobject_ref(child->bs->full_open_options));
8139 if (backing_overridden && !bs->backing) {
8145 qobject_unref(bs->full_open_options);
8146 bs->full_open_options = opts;
8150 if (drv->bdrv_refresh_filename) {
8153 bs->exact_filename[0] = '\0';
8155 drv->bdrv_refresh_filename(bs);
8159 * file -- this only works for format nodes (filter nodes
8166 bs->exact_filename[0] = '\0';
8170 * - it has a filename,
8171 * - the current BDS is not a filter,
8172 * - the file is a protocol BDS, and
8173 * - opening that file (as this BDS's format) will automatically create
8175 * - the user did not significantly change this BDS's behavior with
8177 * - no non-file child of this BDS has been overridden by the user
8180 if (primary_child_bs->exact_filename[0] &&
8181 primary_child_bs->drv->protocol_name &&
8182 !drv->is_filter && !generate_json_filename)
8184 strcpy(bs->exact_filename, primary_child_bs->exact_filename);
8188 if (bs->exact_filename[0]) {
8189 pstrcpy(bs->filename, sizeof(bs->filename), bs->exact_filename);
8191 GString *json = qobject_to_json(QOBJECT(bs->full_open_options));
8192 if (snprintf(bs->filename, sizeof(bs->filename), "json:%s",
8193 json->str) >= sizeof(bs->filename)) {
8195 strcpy(bs->filename + sizeof(bs->filename) - 4, "...");
8203 BlockDriver *drv = bs->drv;
8209 error_setg(errp, "Node '%s' is ejected", bs->node_name);
8213 if (drv->bdrv_dirname) {
8214 return drv->bdrv_dirname(bs, errp);
8223 if (bs->exact_filename[0] != '\0') {
8224 return path_combine(bs->exact_filename, "");
8228 drv->format_name);
8242 if (!parent_bs->drv || !parent_bs->drv->bdrv_add_child) {
8249 * Non-zoned block drivers do not follow zoned storage constraints
8250 * (i.e. sequential writes to zones). Refuse mixing zoned and non-zoned
8253 if (!parent_bs->drv->supports_zoned_children &&
8254 child_bs->bl.zoned == BLK_Z_HM) {
8256 * The host-aware model allows zoned storage constraints and random
8257 * write. Allow mixing host-aware and non-zoned drivers. Using
8258 * host-aware device as a regular device.
8261 child_bs->bl.zoned == BLK_Z_HM ? "zoned" : "non-zoned",
8262 parent_bs->drv->supports_zoned_children ?
8267 if (!QLIST_EMPTY(&child_bs->parents)) {
8269 child_bs->node_name);
8273 parent_bs->drv->bdrv_add_child(parent_bs, child_bs, errp);
8287 if (!parent_bs->drv || !parent_bs->drv->bdrv_del_child) {
8293 QLIST_FOREACH(tmp, &parent_bs->children, next) {
8302 bdrv_get_device_or_node_name(child->bs));
8306 parent_bs->drv->bdrv_del_child(parent_bs, child, errp);
8311 BlockDriver *drv = c->bs->drv;
8315 assert(c->perm & (BLK_PERM_WRITE | BLK_PERM_WRITE_UNCHANGED));
8317 if (!drv->bdrv_make_empty) {
8319 drv->format_name);
8320 return -ENOTSUP;
8323 ret = drv->bdrv_make_empty(c->bs);
8325 error_setg_errno(errp, -ret, "Failed to empty %s",
8326 c->bs->filename);
8341 if (!bs || !bs->drv) {
8345 if (bs->drv->is_filter) {
8349 if (!bs->backing) {
8353 assert(bs->backing->role & BDRV_CHILD_COW);
8354 return bs->backing;
8366 if (!bs || !bs->drv) {
8370 if (!bs->drv->is_filter) {
8375 assert(!(bs->backing && bs->file));
8377 c = bs->backing ?: bs->file;
8382 assert(c->role & BDRV_CHILD_FILTERED);
8388 * whichever is non-NULL.
8419 QLIST_FOREACH(c, &bs->children, next) {
8420 if (c->role & BDRV_CHILD_PRIMARY) {
8438 while (!(stop_on_explicit_filter && !bs->implicit)) {
8447 assert(!bs->drv || !bs->drv->is_filter);
8450 bs = c->bs;
8453 * Note that this treats nodes with bs->drv == NULL as not being
8454 * filters (bs->drv == NULL should be replaced by something else
8457 * always return a non-NULL value (given a non-NULL @bs).
8485 * For a backing chain, return the first non-filter backing image of
8486 * the first non-filter image.
8496 * block-status data region.
8498 * If so, and @pnum is not NULL, set *pnum to `bsc.data_end - offset`,
8506 BdrvBlockStatusCache *bsc = qatomic_rcu_read(&bs->block_status_cache);
8510 qatomic_read(&bsc->valid) &&
8511 ranges_overlap(offset, bytes, bsc->data_start,
8512 bsc->data_end - bsc->data_start);
8515 *pnum = bsc->data_end - offset;
8541 qatomic_set(&bs->block_status_cache->valid, false);
8560 QEMU_LOCK_GUARD(&bs->bsc_modify_lock);
8562 old_bsc = qatomic_rcu_read(&bs->block_status_cache);
8563 qatomic_rcu_set(&bs->block_status_cache, new_bsc);