Lines Matching +full:- +full:- +full:disable +full:- +full:linux +full:- +full:aio
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-
936 * based Linux systems those actually are quite common.
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 "
1127 * Set open flags for aio engine
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 */
1257 /* aio=native doesn't work for cache.direct=off, so disable it for the
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;
1725 if (bs->file != NULL) {
1726 bdrv_unref_child(bs, bs->file);
1727 assert(!bs->file);
1731 g_free(bs->opaque);
1732 bs->opaque = NULL;
1755 bs->open_flags = flags;
1756 bs->options = options ?: qdict_new();
1757 bs->explicit_options = qdict_clone_shallow(bs->options);
1758 bs->opaque = NULL;
1760 update_options_from_flags(bs->options, flags);
1762 ret = bdrv_open_driver(bs, drv, node_name, bs->options, flags, errp);
1764 qobject_unref(bs->explicit_options);
1765 bs->explicit_options = NULL;
1766 qobject_unref(bs->options);
1767 bs->options = NULL;
1788 .name = "node-name",
1815 .help = "Node is opened in read-only mode",
1820 .help = "Node can become read-only if opening read-write fails",
1823 .name = "detect-zeroes",
1842 .name = "simple-create-opts",
1880 assert(bs->file == NULL);
1881 assert(options != NULL && bs->options != options);
1886 ret = -EINVAL;
1890 update_flags_from_options(&bs->open_flags, opts);
1896 bs->force_share = qemu_opt_get_bool(opts, BDRV_OPT_FORCE_SHARE, false);
1898 if (bs->force_share && (bs->open_flags & BDRV_O_RDWR)) {
1901 "=on can only be used with read-only images");
1902 ret = -EINVAL;
1911 filename = blk_bs(file)->filename;
1915 * non-string types would require more care. When @options
1916 * come from -blockdev or blockdev_add, its members are typed
1918 * -drive, they're all QString.
1923 if (drv->bdrv_needs_filename && (!filename || !filename[0])) {
1925 drv->format_name);
1926 ret = -EINVAL;
1930 trace_bdrv_open_common(bs, filename ?: "", bs->open_flags,
1931 drv->format_name);
1941 ret = -ENOTSUP;
1946 ? "Driver '%s' can only be used for read-only devices"
1948 drv->format_name);
1954 assert(qatomic_read(&bs->copy_on_read) == 0);
1956 if (bs->open_flags & BDRV_O_COPY_ON_READ) {
1960 error_setg(errp, "Can't use copy-on-read on read-only device");
1961 ret = -EINVAL;
1968 if (bdrv_parse_discard_flags(discard, &bs->open_flags) != 0) {
1970 ret = -EINVAL;
1975 bs->detect_zeroes =
1976 bdrv_parse_detect_zeroes(opts, bs->open_flags, &local_err);
1979 ret = -EINVAL;
1984 pstrcpy(bs->filename, sizeof(bs->filename), filename);
1986 bs->filename[0] = '\0';
1988 pstrcpy(bs->exact_filename, sizeof(bs->exact_filename), bs->filename);
1991 open_flags = bdrv_open_flags(bs, bs->open_flags);
1992 node_name = qemu_opt_get(opts, "node-name");
1994 assert(!drv->protocol_name || file == NULL);
2044 /* Parse json: pseudo-protocol */
2081 * Caution: while qdict_get_try_str() is fine, getting non-string
2083 * -blockdev or blockdev_add, its members are typed according to
2084 * the QAPI schema, but when they come from -drive, they're all
2092 return -ENOENT;
2096 protocol = drv->protocol_name;
2116 return -EINVAL;
2128 return -EINVAL;
2131 drvname = drv->format_name;
2135 return -EINVAL;
2141 /* Driver-specific filename parsing */
2142 if (drv && drv->bdrv_parse_filename && parse_filename) {
2143 drv->bdrv_parse_filename(filename, *options, &local_err);
2146 return -EINVAL;
2149 if (!drv->bdrv_needs_filename) {
2174 if (entry->state.bs == bs) {
2175 return entry->state.flags;
2180 return bs->open_flags;
2196 * be written to but do not count as read-only images.
2207 return c->klass->get_parent_desc(c);
2221 assert(a->bs);
2222 assert(a->bs == b->bs);
2225 if ((b->perm & a->shared_perm) == b->perm) {
2229 child_bs_name = bdrv_get_node_name(b->bs);
2232 perms = bdrv_perm_names(b->perm & ~a->shared_perm);
2238 b_user, child_bs_name, b->name,
2239 a_user, child_bs_name, a->name);
2255 QLIST_FOREACH(a, &bs->parents, next_parent) {
2256 QLIST_FOREACH(b, &bs->parents, next_parent) {
2277 assert(bs->drv && bs->drv->bdrv_child_perm);
2279 bs->drv->bdrv_child_perm(bs, c, role, reopen_queue,
2283 if (child_bs && child_bs->force_share) {
2317 QLIST_FOREACH(child, &bs->children, next) {
2318 list = bdrv_topological_dfs(list, found, child->bs);
2336 s->child->perm = s->old_perm;
2337 s->child->shared_perm = s->old_shared_perm;
2353 .old_perm = c->perm,
2354 .old_shared_perm = c->shared_perm,
2357 c->perm = perm;
2358 c->shared_perm = shared;
2369 if (bs->drv->bdrv_set_perm) {
2372 bs->drv->bdrv_set_perm(bs, cumulative_perms, cumulative_shared_perms);
2381 if (bs->drv->bdrv_abort_perm_update) {
2382 bs->drv->bdrv_abort_perm_update(bs);
2400 if (!bs->drv) {
2404 if (bs->drv->bdrv_check_perm) {
2405 int ret = bs->drv->bdrv_check_perm(bs, perm, shared_perm, errp);
2428 bdrv_schedule_unref(s->old_bs);
2434 BlockDriverState *new_bs = s->child->bs;
2439 /* old_bs reference is transparently moved from @s to @s->child */
2440 if (!s->child->bs) {
2450 bdrv_parent_drained_begin_single(s->child);
2451 assert(!bdrv_parent_drained_poll_single(s->child));
2453 assert(s->child->quiesced_parent);
2454 bdrv_replace_child_noperm(s->child, s->old_bs);
2470 * Both @child->bs and @new_bs (if non-NULL) must be drained. @new_bs must be
2484 assert(child->quiesced_parent);
2485 assert(!new_bs || new_bs->quiesce_counter);
2489 .old_bs = child->bs,
2512 BlockDriver *drv = bs->drv;
2520 /* Write permissions never work with read-only images */
2525 error_setg(errp, "Block node is read-only");
2527 error_setg(errp, "Read-only block node '%s' cannot support "
2528 "read-write users", bdrv_get_node_name(bs));
2531 return -EPERM;
2542 if ((bs->total_sectors * BDRV_SECTOR_SIZE) % bs->bl.request_alignment) {
2546 return -EPERM;
2562 if (!drv->bdrv_child_perm) {
2563 assert(QLIST_EMPTY(&bs->children));
2568 QLIST_FOREACH(c, &bs->children, next) {
2571 bdrv_child_perm(bs, c->bs, c, c->role, q,
2581 * @list is a product of bdrv_topological_dfs() (may be called several times) -
2595 for ( ; list; list = list->next) {
2596 bs = list->data;
2599 return -EINVAL;
2626 for ( ; list; list = list->next) {
2627 refresh_list = bdrv_topological_dfs(refresh_list, found, list->data);
2642 QLIST_FOREACH(c, &bs->parents, next_parent) {
2643 cumulative_perms |= c->perm;
2644 cumulative_shared_perms &= c->shared_perm;
2667 for (p = permissions; p->name; p++) {
2668 if (perm & p->perm) {
2669 if (result->len > 0) {
2672 g_string_append(result, p->name);
2718 ret = bdrv_refresh_perms(c->bs, tran, &local_err);
2723 if ((perm & ~c->perm) || (c->shared_perm & ~shared)) {
2749 bdrv_child_perm(bs, c->bs, c, c->role, NULL,
2799 if (bs->open_flags & BDRV_O_INACTIVE) {
2834 * bs->file always needs to be consistent because of the
2857 * is split into fixed-size data files).
2864 * write copied clusters on copy-on-read.
2879 if (bs->open_flags & BDRV_O_INACTIVE) {
2930 * If @new_bs is non-NULL, the parent of @child must already be drained through
2936 BlockDriverState *old_bs = child->bs;
2939 assert(!child->frozen);
2943 * child->bs, we need to make sure that its new parent is drained, too. In
2944 * other words, either child->quiesce_parent must already be true or we must
2950 * To keep things simple, we pick the first option (child->quiesce_parent
2961 assert(!new_bs || child->quiesced_parent);
2970 if (child->klass->detach) {
2971 child->klass->detach(child);
2976 child->bs = new_bs;
2979 QLIST_INSERT_HEAD(&new_bs->parents, child, next_parent);
2980 if (child->klass->attach) {
2981 child->klass->attach(child);
2990 new_bs_quiesce_counter = (new_bs ? new_bs->quiesce_counter : 0);
2991 if (!new_bs_quiesce_counter && child->quiesced_parent) {
2999 * The child must be empty (i.e. `child->bs == NULL`) and it must be
3004 assert(!child->bs);
3008 assert(!child->next.le_prev); /* not in children list */
3010 g_free(child->name);
3023 BlockDriverState *bs = s->child->bs;
3028 bdrv_replace_child_noperm(s->child, NULL);
3030 if (bdrv_get_aio_context(bs) != s->old_child_ctx) {
3031 bdrv_try_change_aio_context_locked(bs, s->old_child_ctx, NULL,
3035 if (bdrv_child_get_parent_aio_context(s->child) != s->old_parent_ctx) {
3044 ret = s->child->klass->change_aio_ctx(s->child, s->old_parent_ctx,
3054 bdrv_child_free(s->child);
3091 assert(child_class->get_parent_desc);
3123 if (ret < 0 && child_class->change_aio_ctx) {
3129 ret_child = child_class->change_aio_ctx(new_child, child_ctx,
3136 tran_finalize(aio_ctx_tran, ret_child == true ? 0 : -1);
3202 assert(parent_bs->drv);
3207 child_bs->node_name, child_name, parent_bs->node_name);
3212 child_bs->node_name, child_name, parent_bs->node_name);
3251 ret = -EINVAL;
3291 ret = -EINVAL;
3309 * Callers must ensure that child->frozen is false.
3315 BlockDriverState *child_bs = child->bs;
3330 * When the parent requiring a non-default AioContext is removed, the
3349 s->bs->inherits_from = s->old_inherits_from;
3367 .old_inherits_from = bs->inherits_from,
3373 bs->inherits_from = new_inherits_from;
3387 if (child->bs->inherits_from == root) {
3390 * child->bs goes away.
3392 QLIST_FOREACH(c, &root->children, next) {
3393 if (c != child && c->bs == child->bs) {
3398 bdrv_set_inherits_from(child->bs, NULL, tran);
3402 QLIST_FOREACH(c, &child->bs->children, next) {
3408 * Callers must ensure that child->frozen is false.
3429 QLIST_FOREACH(c, &bs->parents, next_parent) {
3430 if (c->klass->change_media) {
3431 c->klass->change_media(c, load);
3436 /* Return true if you can reach parent going through child->inherits_from
3442 child = child->inherits_from;
3449 * Return the BdrvChildRole for @bs's backing child. bs->backing is
3455 if (bs->drv && bs->drv->is_filter) {
3463 * Sets the bs->backing or bs->file link of a BDS. A new reference is created;
3488 BdrvChild *child = is_backing ? parent_bs->backing : parent_bs->file;
3493 if (!parent_bs->drv) {
3496 * qcow2 driver to never clear bs->drv and implement format corruption
3500 return -EINVAL;
3503 if (child && child->frozen) {
3505 child->name, parent_bs->node_name, child->bs->node_name);
3506 return -EPERM;
3509 if (is_backing && !parent_bs->drv->is_filter &&
3510 !parent_bs->drv->supports_backing)
3513 "files", parent_bs->drv->format_name, parent_bs->node_name);
3514 return -EINVAL;
3517 if (parent_bs->drv->is_filter) {
3529 return -EINVAL;
3531 role = child->role;
3535 assert(child->bs->quiesce_counter);
3549 return -EINVAL;
3580 assert(bs->quiesce_counter > 0);
3581 if (bs->backing) {
3582 assert(bs->backing->bs->quiesce_counter > 0);
3624 if (bs->backing != NULL) {
3634 bs->open_flags &= ~BDRV_O_NO_BACKING;
3641 * Caution: while qdict_get_try_str() is fine, getting non-string
3643 * -blockdev or blockdev_add, its members are typed according to
3644 * the QAPI schema, but when they come from -drive, they're all
3650 } else if (bs->backing_file[0] == '\0' && qdict_size(options) == 0) {
3659 * only possible with -drive anyway (otherwise the QAPI
3661 implicit_backing = !strcmp(bs->auto_backing_file, bs->backing_file);
3666 ret = -EINVAL;
3673 if (!bs->drv || !bs->drv->supports_backing) {
3674 ret = -EINVAL;
3681 bs->backing_format[0] != '\0' && !qdict_haskey(options, "driver")) {
3682 qdict_put_str(options, "driver", bs->backing_format);
3689 bs->open_flags |= BDRV_O_NO_BACKING;
3691 ret = -EINVAL;
3697 pstrcpy(bs->auto_backing_file, sizeof(bs->auto_backing_file),
3698 backing_hd->filename);
3741 * Caution: while qdict_get_try_str() is fine, getting non-string
3743 * -blockdev or blockdev_add, its members are typed according to
3744 * the QAPI schema, but when they come from -drive, they're all
3837 assert(!parent->drv->filtered_child_is_backing);
3838 role = parent->drv->is_filter ?
3844 return -EINVAL;
3864 if (ref->type == QTYPE_QSTRING) {
3865 reference = ref->u.reference;
3867 BlockdevOptions *options = &ref->u.definition;
3868 assert(ref->type == QTYPE_QDICT);
3916 error_setg_errno(errp, -total_size, "Could not get image size");
4033 bs->explicit_options = qdict_clone_shallow(options);
4038 if (parent->drv) {
4039 parent_is_format = parent->drv->is_format;
4042 * parent->drv is not set yet because this node is opened for
4049 bs->inherits_from = parent;
4050 child_class->inherit_options(child_role, parent_is_format,
4052 parent->open_flags, parent->options);
4064 * When @options come from -blockdev or blockdev_add, members are
4066 * -drive, they're all QString.
4079 /* Let bdrv_backing_options() override "read-only" */
4085 bs->open_flags = flags;
4086 bs->options = options;
4112 qdict_del(bs->explicit_options, "backing");
4113 qdict_del(bs->options, "backing");
4147 bs->probed = !drv;
4155 * but we first need to open bs->file for the probing to work, while
4156 * opening bs->file already requires the (mostly) final set of options
4161 * sure to update both bs->options (which has the full effective
4164 qdict_put_str(bs->options, "driver", drv->format_name);
4165 qdict_put_str(options, "driver", drv->format_name);
4172 assert(!!(flags & BDRV_O_PROTOCOL) == !!drv->protocol_name);
4197 * from bs->options and bs->explicit_options */
4198 QLIST_FOREACH(child, &bs->children, next) {
4200 child_key_dot = g_strdup_printf("%s.", child->name);
4201 qdict_extract_subqdict(bs->explicit_options, NULL, child_key_dot);
4202 qdict_extract_subqdict(bs->options, NULL, child_key_dot);
4203 qdict_del(bs->explicit_options, child->name);
4204 qdict_del(bs->options, child->name);
4213 "'%s'", drv->format_name, entry->key);
4217 drv->format_name, entry->key);
4251 qobject_unref(bs->explicit_options);
4252 qobject_unref(bs->options);
4254 bs->options = NULL;
4255 bs->explicit_options = NULL;
4277 /* Return true if the NULL-terminated @list contains @str */
4292 * Check that every option set in @bs->options is also set in
4296 * @bs->drv->mutable_opts are skipped.
4298 * Return 0 on success, otherwise return -EINVAL and set @errp.
4307 "node-name", "discard", "cache.direct", "cache.no-flush",
4308 "read-only", "auto-read-only", "detect-zeroes", NULL
4311 for (e = qdict_first(bs->options); e; e = qdict_next(bs->options, e)) {
4312 if (!qdict_haskey(new_opts, e->key) &&
4313 !is_str_in_list(e->key, common_options) &&
4314 !is_str_in_list(e->key, bs->drv->mutable_opts)) {
4316 "to its default value", e->key);
4317 return -EINVAL;
4336 QLIST_FOREACH(c, &bs->children, next) {
4337 if (bdrv_recurse_has_child(c->bs, child)) {
4384 assert(bs->quiesce_counter > 0);
4397 if (bs == bs_entry->state.bs) {
4413 bs_entry->state.explicit_options :
4414 bs->explicit_options);
4424 klass->inherit_options(role, parent_is_format, &flags, options,
4432 old_options = qdict_clone_shallow(bs->options);
4455 qobject_unref(bs_entry->state.options);
4456 qobject_unref(bs_entry->state.explicit_options);
4459 bs_entry->state.bs = bs;
4460 bs_entry->state.options = options;
4461 bs_entry->state.explicit_options = explicit_options;
4462 bs_entry->state.flags = flags;
4471 bs_entry->state.backing_missing =
4476 QLIST_FOREACH(child, &bs->children, next) {
4483 if (child->bs->inherits_from != bs) {
4488 if (qdict_haskey(options, child->name)) {
4489 const char *childref = qdict_get_try_str(options, child->name);
4494 if (g_strcmp0(childref, child->bs->node_name)) {
4504 /* Extract child options ("child-name.*") */
4505 char *child_key_dot = g_strdup_printf("%s.", child->name);
4511 bdrv_reopen_queue_child(bs_queue, child->bs, new_child_options,
4512 child->klass, child->role, bs->drv->is_format,
4542 qobject_unref(bs_entry->state.explicit_options);
4543 qobject_unref(bs_entry->state.options);
4574 int ret = -1;
4584 ret = bdrv_flush(bs_entry->state.bs);
4586 error_setg_errno(errp, -ret, "Error flushing drive");
4592 assert(bs_entry->state.bs->quiesce_counter > 0);
4593 ret = bdrv_reopen_prepare(&bs_entry->state, bs_queue, tran, errp);
4597 bs_entry->prepared = true;
4601 BDRVReopenState *state = &bs_entry->state;
4603 refresh_list = g_slist_prepend(refresh_list, state->bs);
4604 if (state->old_backing_bs) {
4605 refresh_list = g_slist_prepend(refresh_list, state->old_backing_bs);
4607 if (state->old_file_bs) {
4608 refresh_list = g_slist_prepend(refresh_list, state->old_file_bs);
4613 * Note that file-posix driver rely on permission update done during reopen
4632 * children are usually goes after parents in reopen-queue, so go from last
4636 bdrv_reopen_commit(&bs_entry->state);
4644 BlockDriverState *bs = bs_entry->state.bs;
4646 if (bs->drv->bdrv_reopen_commit_post) {
4647 bs->drv->bdrv_reopen_commit_post(&bs_entry->state);
4660 if (bs_entry->prepared) {
4661 bdrv_reopen_abort(&bs_entry->state);
4697 * reopen_state->options QDict is valid or not.
4702 * reopen_state->bs then return 0.
4706 * If that's the case then reopen_state->replace_backing_bs is set to
4707 * true and reopen_state->new_backing_bs contains a pointer to the new
4715 * @reopen_state->bs can move to a different AioContext in this function.
4725 BlockDriverState *bs = reopen_state->bs;
4737 value = qdict_get(reopen_state->options, child_name);
4753 ret = -EINVAL;
4760 "cycle", str, child_name, bs->node_name);
4761 ret = -EINVAL;
4773 old_child_bs = is_backing ? child_bs(bs->backing) : child_bs(bs->file);
4785 if (old_child_bs->implicit) {
4787 child_name, bs->node_name);
4788 ret = -EPERM;
4793 if (bs->drv->is_filter && !old_child_bs) {
4799 "%s child", bs->node_name, bs->drv->format_name, child_name);
4800 ret = -EINVAL;
4805 reopen_state->old_backing_bs = old_child_bs;
4807 reopen_state->old_file_bs = old_child_bs;
4812 assert(old_child_bs->quiesce_counter > 0);
4843 * Returns 0 on success, non-zero on error. On error errp will be set
4860 int ret = -1;
4871 assert(reopen_state->bs->drv != NULL);
4873 drv = reopen_state->bs->drv;
4876 * entries from reopen_state->options as they are processed, so
4878 orig_reopen_opts = qdict_clone_shallow(reopen_state->options);
4882 if (!qemu_opts_absorb_qdict(opts, reopen_state->options, errp)) {
4883 ret = -EINVAL;
4888 * are up-to-date. This time we simply want to remove the options from
4890 old_flags = reopen_state->flags;
4891 update_flags_from_options(&reopen_state->flags, opts);
4892 assert(old_flags == reopen_state->flags);
4896 if (bdrv_parse_discard_flags(discard, &reopen_state->flags) != 0) {
4898 ret = -EINVAL;
4903 reopen_state->detect_zeroes =
4904 bdrv_parse_detect_zeroes(opts, reopen_state->flags, &local_err);
4907 ret = -EINVAL;
4911 /* All other options (including node-name and driver) must be unchanged.
4914 qemu_opts_to_qdict(opts, reopen_state->options);
4916 /* If we are to stay read-only, do not allow permission change
4919 read_only = !(reopen_state->flags & BDRV_O_RDWR);
4922 ret = bdrv_can_set_read_only(reopen_state->bs, read_only, true, &local_err);
4929 if (drv->bdrv_reopen_prepare) {
4931 * If a driver-specific option is missing, it means that we
4935 ret = bdrv_reset_options_allowed(reopen_state->bs,
4936 reopen_state->options, errp);
4941 ret = drv->bdrv_reopen_prepare(reopen_state, queue, &local_err);
4947 bdrv_refresh_filename(reopen_state->bs);
4950 reopen_state->bs->filename);
4959 "does not support reopening files", drv->format_name,
4960 bdrv_get_device_or_node_name(reopen_state->bs));
4962 ret = -1;
4974 if (drv->supports_backing && reopen_state->backing_missing &&
4975 (reopen_state->bs->backing || reopen_state->bs->backing_file[0])) {
4977 reopen_state->bs->node_name);
4979 ret = -EINVAL;
4994 qdict_del(reopen_state->options, "backing");
5002 qdict_del(reopen_state->options, "file");
5007 if (qdict_size(reopen_state->options)) {
5008 const QDictEntry *entry = qdict_first(reopen_state->options);
5013 QObject *new = entry->value;
5014 QObject *old = qdict_get(reopen_state->bs->options, entry->key);
5020 QLIST_FOREACH(child, &reopen_state->bs->children, next) {
5021 if (!strcmp(child->name, entry->key)) {
5027 if (!strcmp(child->bs->node_name,
5035 * TODO: When using -drive to specify blockdev options, all values
5036 * will be strings; however, when using -blockdev, blockdev-add or
5037 * filenames using the json:{} pseudo-protocol, they will be
5040 * (because you can only specify them through qemu-io; all other
5042 * Therefore, when using anything other than -drive to create a BDS,
5043 * this cannot detect non-string options as unchanged, because
5051 error_setg(errp, "Cannot change the option '%s'", entry->key);
5052 ret = -EINVAL;
5055 } while ((entry = qdict_next(reopen_state->options, entry)));
5060 /* Restore the original reopen_state->options QDict */
5061 qobject_unref(reopen_state->options);
5062 reopen_state->options = qobject_ref(orig_reopen_opts);
5066 /* drv->bdrv_reopen_prepare() has succeeded, so we need to
5067 * call drv->bdrv_reopen_abort() before signaling an error
5070 if (drv->bdrv_reopen_abort) {
5071 drv->bdrv_reopen_abort(reopen_state);
5092 bs = reopen_state->bs;
5093 drv = bs->drv;
5098 if (drv->bdrv_reopen_commit) {
5099 drv->bdrv_reopen_commit(reopen_state);
5105 qobject_unref(bs->explicit_options);
5106 qobject_unref(bs->options);
5107 qobject_ref(reopen_state->explicit_options);
5108 qobject_ref(reopen_state->options);
5110 bs->explicit_options = reopen_state->explicit_options;
5111 bs->options = reopen_state->options;
5112 bs->open_flags = reopen_state->flags;
5113 bs->detect_zeroes = reopen_state->detect_zeroes;
5115 /* Remove child references from bs->options and bs->explicit_options.
5117 QLIST_FOREACH(child, &bs->children, next) {
5118 qdict_del(bs->explicit_options, child->name);
5119 qdict_del(bs->options, child->name);
5122 qdict_del(bs->explicit_options, "backing");
5123 qdict_del(bs->options, "backing");
5126 bdrv_refresh_total_sectors(bs, bs->total_sectors);
5138 drv = reopen_state->bs->drv;
5142 if (drv->bdrv_reopen_abort) {
5143 drv->bdrv_reopen_abort(reopen_state);
5154 assert(!bs->refcnt);
5160 if (bs->drv) {
5161 if (bs->drv->bdrv_close) {
5163 bs->drv->bdrv_close(bs);
5165 bs->drv = NULL;
5169 QLIST_FOREACH_SAFE(child, &bs->children, next, next) {
5173 assert(!bs->backing);
5174 assert(!bs->file);
5177 g_free(bs->opaque);
5178 bs->opaque = NULL;
5179 qatomic_set(&bs->copy_on_read, 0);
5180 bs->backing_file[0] = '\0';
5181 bs->backing_format[0] = '\0';
5182 bs->total_sectors = 0;
5183 bs->encrypted = false;
5184 bs->sg = false;
5185 qobject_unref(bs->options);
5186 qobject_unref(bs->explicit_options);
5187 bs->options = NULL;
5188 bs->explicit_options = NULL;
5189 qobject_unref(bs->full_open_options);
5190 bs->full_open_options = NULL;
5191 g_free(bs->block_status_cache);
5192 bs->block_status_cache = NULL;
5195 assert(QLIST_EMPTY(&bs->dirty_bitmaps));
5197 QLIST_FOREACH_SAFE(ban, &bs->aio_notifiers, list, ban_next) {
5200 QLIST_INIT(&bs->aio_notifiers);
5208 if (bs->quiesce_counter) {
5219 * jobs whose AIO context has not been polled yet */
5234 if (c->klass->stay_at_node) {
5239 * c->bs by @to would mean to create a loop.
5244 * guest device -> node A -> further backing chain...
5252 * guest device -> node A -> further backing chain...
5255 * replace all pointers to A by pointers to B -- except for the
5259 * guest device -> node B
5262 * node A -> further backing chain...
5264 * In general, when replacing a node A (c->bs) by a node B (@to),
5272 * if @c is found (by breadth-first search) anywhere in the whole
5286 QLIST_FOREACH(c2, &v->children, next) {
5292 if (g_hash_table_contains(found, c2->bs)) {
5296 g_queue_push_tail(queue, c2->bs);
5297 g_hash_table_add(found, c2->bs);
5320 * @child->bs (if non-NULL) must be drained.
5331 if (child->bs) {
5332 assert(child->quiesced_parent);
5340 * Both @from and @to (if non-NULL) must be drained. @to must be kept drained
5356 assert(from->quiesce_counter);
5357 assert(to->quiesce_counter);
5359 QLIST_FOREACH_SAFE(c, &from->parents, next_parent, next) {
5360 assert(c->bs == from);
5366 c->name, from->node_name);
5367 return -EINVAL;
5369 if (c->frozen) {
5371 c->name, from->node_name);
5372 return -EPERM;
5385 * if it creates a parent-child relation loop or if parent is block-job.
5391 * case backing link of the cow-parent of @to is removed.
5404 assert(from->quiesce_counter);
5405 assert(to->quiesce_counter);
5422 * permissions based on new graph. If we fail, we'll roll-back the
5498 assert(!bs_new->backing);
5507 ret = -EINVAL;
5533 BlockDriverState *old_bs = child->bs;
5562 assert(!bs->refcnt);
5566 if (bs->node_name[0] != '\0') {
5573 qemu_mutex_destroy(&bs->reqs_lock);
5612 node_name = qdict_get_try_str(options, "node-name");
5657 * free of errors) or -errno when an internal error occurred. The results of the
5665 if (bs->drv == NULL) {
5666 return -ENOMEDIUM;
5668 if (bs->drv->bdrv_co_check == NULL) {
5669 return -ENOTSUP;
5673 return bs->drv->bdrv_co_check(bs, res, fix);
5678 * 0 - success
5679 * -EINVAL - backing format specified, but no file
5680 * -ENOSPC - can't update the backing file because no space is left in the
5682 * -ENOTSUP - format driver doesn't support changing the backing file
5688 BlockDriver *drv = bs->drv;
5694 return -ENOMEDIUM;
5699 return -EINVAL;
5703 return -EINVAL;
5706 if (drv->bdrv_co_change_backing_file != NULL) {
5707 ret = drv->bdrv_co_change_backing_file(bs, backing_file, backing_fmt);
5709 ret = -ENOTSUP;
5713 pstrcpy(bs->backing_file, sizeof(bs->backing_file), backing_file ?: "");
5714 pstrcpy(bs->backing_format, sizeof(bs->backing_format), backing_fmt ?: "");
5715 pstrcpy(bs->auto_backing_file, sizeof(bs->auto_backing_file),
5722 * Finds the first non-filter node above bs in the chain between
5776 if (child && child->frozen) {
5778 child->name, i->node_name, child->bs->node_name);
5802 return -EPERM;
5807 if (child && child->bs->never_freeze) {
5809 child->name, child->bs->node_name);
5810 return -EPERM;
5817 child->frozen = true;
5840 assert(child->frozen);
5841 child->frozen = false;
5854 * bottom <- base <- intermediate <- top <- active
5858 * bottom <- base <- active
5862 * base <- intermediate <- top <- active
5866 * base <- active
5868 * If backing_file_str is non-NULL, it will be used when modifying top's
5883 int ret = -EIO;
5893 if (!top->drv || !base->drv) {
5903 * base->inherits_from to top->inherits_from after 'top' and all
5910 /* success - we can delete the intermediate states, and link top->base */
5913 backing_file_str = base->filename;
5916 QLIST_FOREACH(c, &top->parents, next_parent) {
5927 * test-bdrv-drain. test_drop_intermediate_poll() test-case will crash.
5938 for (p = updated_children; p; p = p->next) {
5939 c = p->data;
5941 if (c->klass->update_filename) {
5942 ret = c->klass->update_filename(c, base, backing_file_str,
5951 * Note, that c->klass->update_filename may lead to permission
5962 base->inherits_from = explicit_top->inherits_from;
5978 * sums the size of all data-bearing children. (This excludes backing
5987 QLIST_FOREACH(child, &bs->children, next) {
5988 if (child->role & (BDRV_CHILD_DATA | BDRV_CHILD_METADATA |
5991 child_size = bdrv_co_get_allocated_file_size(child->bs);
6008 BlockDriver *drv = bs->drv;
6013 return -ENOMEDIUM;
6015 if (drv->bdrv_co_get_allocated_file_size) {
6016 return drv->bdrv_co_get_allocated_file_size(bs);
6019 if (drv->protocol_name) {
6021 * Protocol drivers default to -ENOTSUP (most of their data is
6025 return -ENOTSUP;
6026 } else if (drv->is_filter) {
6062 if (!drv->bdrv_measure) {
6064 drv->format_name);
6068 return drv->bdrv_measure(opts, in_bs, errp);
6072 * Return number of sectors on success, -errno on error.
6076 BlockDriver *drv = bs->drv;
6081 return -ENOMEDIUM;
6083 if (bs->bl.has_variable_length) {
6084 int ret = bdrv_co_refresh_total_sectors(bs, bs->total_sectors);
6089 return bs->total_sectors;
6098 BlockDriver *drv = bs->drv;
6102 return -ENOMEDIUM;
6104 if (bs->bl.has_variable_length) {
6105 int ret = bdrv_refresh_total_sectors(bs, bs->total_sectors);
6111 return bs->total_sectors;
6115 * Return length in bytes on success, -errno on error.
6129 return -EFBIG;
6137 return bs->sg;
6148 if (!bs->drv || !block_driver_can_compress(bs->drv)) {
6167 return bs->drv ? bs->drv->format_name : NULL;
6186 if (drv->format_name) {
6195 found = !strcmp(formats[--i], drv->format_name);
6200 formats[count++] = drv->format_name;
6218 found = !strcmp(formats[--j], format_name);
6246 if (!strcmp(node_name, bs->node_name)) {
6285 gr->graph = g_new0(XDbgBlockGraph, 1);
6286 gr->graph_nodes = g_hash_table_new(NULL, NULL);
6293 XDbgBlockGraph *graph = gr->graph;
6295 g_hash_table_destroy(gr->graph_nodes);
6303 uintptr_t ret = (uintptr_t)g_hash_table_lookup(gr->graph_nodes, node);
6310 * Start counting from 1, not 0, because 0 interferes with not-found (NULL)
6313 ret = g_hash_table_size(gr->graph_nodes) + 1;
6314 g_hash_table_insert(gr->graph_nodes, node, (void *)ret);
6326 n->id = xdbg_graph_node_num(gr, node);
6327 n->type = type;
6328 n->name = g_strdup(name);
6330 QAPI_LIST_PREPEND(gr->graph->nodes, n);
6342 edge->parent = xdbg_graph_node_num(gr, parent);
6343 edge->child = xdbg_graph_node_num(gr, child->bs);
6344 edge->name = g_strdup(child->name);
6349 if (flag & child->perm) {
6350 QAPI_LIST_PREPEND(edge->perm, qapi_perm);
6352 if (flag & child->shared_perm) {
6353 QAPI_LIST_PREPEND(edge->shared_perm, qapi_perm);
6357 QAPI_LIST_PREPEND(gr->graph->edges, edge);
6392 job->job.id);
6393 for (el = job->nodes; el; el = el->next) {
6394 xdbg_graph_add_edge(gr, job, (BdrvChild *)el->data);
6401 bs->node_name);
6402 QLIST_FOREACH(child, &bs->children, next) {
6440 error_setg(errp, "Cannot find device=\'%s\' nor node-name=\'%s\'",
6481 return bs->node_name;
6491 QLIST_FOREACH(c, &bs->parents, next_parent) {
6492 if (c->klass->get_name) {
6493 name = c->klass->get_name(c);
6503 /* TODO check what callers really want: bs->node_name or blk_name() */
6513 * absent, then this returns an empty (non-null) string. */
6517 return bdrv_get_parent_name(bs) ?: bs->node_name;
6523 return bs->open_flags;
6537 if (!bs->drv) {
6546 if (bs->drv->bdrv_has_zero_init) {
6547 return bs->drv->bdrv_has_zero_init(bs);
6562 if (!(bs->open_flags & BDRV_O_UNMAP)) {
6566 return bs->supported_zero_flags & BDRV_REQ_MAY_UNMAP;
6573 pstrcpy(filename, filename_size, bs->backing_file);
6579 BlockDriver *drv = bs->drv;
6583 /* if bs->drv == NULL, bs is closed, so there's nothing to do here */
6585 return -ENOMEDIUM;
6587 if (!drv->bdrv_co_get_info) {
6592 return -ENOTSUP;
6595 ret = drv->bdrv_co_get_info(bs, bdi);
6596 if (bdi->subcluster_size == 0) {
6601 bdi->subcluster_size = bdi->cluster_size;
6607 if (bdi->cluster_size > BDRV_MAX_ALIGNMENT) {
6608 return -EINVAL;
6617 BlockDriver *drv = bs->drv;
6619 if (drv && drv->bdrv_get_specific_info) {
6620 return drv->bdrv_get_specific_info(bs, errp);
6627 BlockDriver *drv = bs->drv;
6629 if (!drv || !drv->bdrv_get_specific_stats) {
6632 return drv->bdrv_get_specific_stats(bs);
6640 if (!bs || !bs->drv || !bs->drv->bdrv_co_debug_event) {
6644 bs->drv->bdrv_co_debug_event(bs, event);
6651 while (bs && bs->drv && !bs->drv->bdrv_debug_breakpoint) {
6655 if (bs && bs->drv && bs->drv->bdrv_debug_breakpoint) {
6656 assert(bs->drv->bdrv_debug_remove_breakpoint);
6671 return bs->drv->bdrv_debug_breakpoint(bs, event, tag);
6674 return -ENOTSUP;
6684 return bs->drv->bdrv_debug_remove_breakpoint(bs, tag);
6687 return -ENOTSUP;
6695 while (bs && (!bs->drv || !bs->drv->bdrv_debug_resume)) {
6699 if (bs && bs->drv && bs->drv->bdrv_debug_resume) {
6700 return bs->drv->bdrv_debug_resume(bs, tag);
6703 return -ENOTSUP;
6711 while (bs && bs->drv && !bs->drv->bdrv_debug_is_suspended) {
6715 if (bs && bs->drv && bs->drv->bdrv_debug_is_suspended) {
6716 return bs->drv->bdrv_debug_is_suspended(bs, tag);
6723 * relative, it must be relative to the chain. So, passing in bs->filename
6741 if (!bs || !bs->drv || !backing_file) {
6778 if (strcmp(backing_file, bs_below->filename) == 0) {
6782 } else if (is_protocol || path_has_protocol(curr_bs->backing_file)) {
6789 if (strcmp(backing_file, curr_bs->backing_file) == 0) {
6852 return bs->open_flags & BDRV_O_INACTIVE;
6865 if (!bs->drv) {
6866 return -ENOMEDIUM;
6869 QLIST_FOREACH(child, &bs->children, next) {
6870 bdrv_activate(child->bs, &local_err);
6873 return -EINVAL;
6890 if (bs->open_flags & BDRV_O_INACTIVE) {
6891 bs->open_flags &= ~BDRV_O_INACTIVE;
6894 bs->open_flags |= BDRV_O_INACTIVE;
6900 bs->open_flags |= BDRV_O_INACTIVE;
6908 ret = bdrv_refresh_total_sectors(bs, bs->total_sectors);
6910 bs->open_flags |= BDRV_O_INACTIVE;
6911 error_setg_errno(errp, -ret, "Could not refresh total sector count");
6916 QLIST_FOREACH(parent, &bs->parents, next_parent) {
6917 if (parent->klass->activate) {
6918 parent->klass->activate(parent, &local_err);
6920 bs->open_flags |= BDRV_O_INACTIVE;
6922 return -EINVAL;
6935 assert(!(bs->open_flags & BDRV_O_INACTIVE));
6938 if (bs->drv->bdrv_co_invalidate_cache) {
6939 bs->drv->bdrv_co_invalidate_cache(bs, &local_err);
6942 return -EINVAL;
6974 QLIST_FOREACH(parent, &bs->parents, next_parent) {
6975 if (parent->klass->parent_is_bds) {
6976 BlockDriverState *parent_bs = parent->opaque;
6977 if (!only_active || !(parent_bs->open_flags & BDRV_O_INACTIVE)) {
6995 assert(bs->quiesce_counter > 0);
6997 if (!bs->drv) {
6998 return -ENOMEDIUM;
7011 if (bs->open_flags & BDRV_O_INACTIVE) {
7017 if (bs->drv->bdrv_inactivate) {
7018 ret = bs->drv->bdrv_inactivate(bs);
7024 QLIST_FOREACH(parent, &bs->parents, next_parent) {
7025 if (parent->klass->inactivate) {
7026 ret = parent->klass->inactivate(parent);
7037 return -EPERM;
7040 bs->open_flags |= BDRV_O_INACTIVE;
7050 QLIST_FOREACH(child, &bs->children, next) {
7051 ret = bdrv_inactivate_recurse(child->bs, false);
7069 return -EPERM;
7074 error_setg_errno(errp, -ret, "Failed to inactivate node");
7120 BlockDriver *drv = bs->drv;
7128 if (drv->bdrv_co_is_inserted) {
7129 return drv->bdrv_co_is_inserted(bs);
7131 QLIST_FOREACH(child, &bs->children, next) {
7132 if (!bdrv_co_is_inserted(child->bs)) {
7144 BlockDriver *drv = bs->drv;
7148 if (drv && drv->bdrv_co_eject) {
7149 drv->bdrv_co_eject(bs, eject_flag);
7159 BlockDriver *drv = bs->drv;
7164 if (drv && drv->bdrv_co_lock_medium) {
7165 drv->bdrv_co_lock_medium(bs, locked);
7173 bs->refcnt++;
7185 assert(bs->refcnt > 0);
7186 if (--bs->refcnt == 0) {
7225 if (!QLIST_EMPTY(&bs->op_blockers[op])) {
7226 blocker = QLIST_FIRST(&bs->op_blockers[op]);
7227 error_propagate_prepend(errp, error_copy(blocker->reason),
7242 blocker->reason = reason;
7243 QLIST_INSERT_HEAD(&bs->op_blockers[op], blocker, list);
7251 QLIST_FOREACH_SAFE(blocker, &bs->op_blockers[op], list, next) {
7252 if (blocker->reason == reason) {
7282 if (!QLIST_EMPTY(&bs->op_blockers[i])) {
7316 if (!drv->create_opts) {
7318 drv->format_name);
7322 if (!proto_drv->create_opts) {
7324 proto_drv->format_name);
7329 create_opts = qemu_opts_append(create_opts, drv->create_opts);
7330 create_opts = qemu_opts_append(create_opts, proto_drv->create_opts);
7334 /* Parse -o options */
7343 } else if (img_size != UINT64_C(-1)) {
7422 bs->drv->format_name);
7425 if (size == -1) {
7429 error_setg_errno(errp, -size, "Could not get size of '%s'",
7446 if (size == -1 &&
7448 qemu_opt_get_bool(opts, "detached-header", false))) {
7462 if (ret == -EFBIG) {
7485 return bs ? bs->aio_context : qemu_get_aio_context();
7496 * Increase bs->in_flight to ensure that this operation is completed before
7524 assert(!bs->walking_aio_notifiers);
7526 bs->walking_aio_notifiers = true;
7527 QLIST_FOREACH_SAFE(baf, &bs->aio_notifiers, list, baf_tmp) {
7528 if (baf->deleted) {
7531 baf->detach_aio_context(baf->opaque);
7534 /* Never mind iterating again to check for ->deleted. bdrv_close() will
7535 * remove remaining aio notifiers if we aren't called again.
7537 bs->walking_aio_notifiers = false;
7539 if (bs->drv && bs->drv->bdrv_detach_aio_context) {
7540 bs->drv->bdrv_detach_aio_context(bs);
7543 bs->aio_context = NULL;
7552 bs->aio_context = new_context;
7554 if (bs->drv && bs->drv->bdrv_attach_aio_context) {
7555 bs->drv->bdrv_attach_aio_context(bs, new_context);
7558 assert(!bs->walking_aio_notifiers);
7559 bs->walking_aio_notifiers = true;
7560 QLIST_FOREACH_SAFE(ban, &bs->aio_notifiers, list, ban_tmp) {
7561 if (ban->deleted) {
7564 ban->attached_aio_context(new_context, ban->opaque);
7567 bs->walking_aio_notifiers = false;
7601 if (!c->klass->change_aio_ctx) {
7607 if (!c->klass->change_aio_ctx(c, ctx, visited, tran, errp)) {
7615 * Changes the AioContext of @c->bs to @ctx and recursively for all its children
7634 return bdrv_change_aio_context(c->bs, ctx, visited, tran, errp);
7647 BlockDriverState *bs = (BlockDriverState *) state->bs;
7648 AioContext *new_context = state->new_ctx;
7683 QLIST_FOREACH(c, &bs->parents, next_parent) {
7689 QLIST_FOREACH(c, &bs->children, next) {
7701 assert(bs->quiesce_counter > 0);
7751 return -EPERM;
7793 QLIST_INSERT_HEAD(&bs->aio_notifiers, ban, list);
7805 QLIST_FOREACH_SAFE(ban, &bs->aio_notifiers, list, ban_next) {
7806 if (ban->attached_aio_context == attached_aio_context &&
7807 ban->detach_aio_context == detach_aio_context &&
7808 ban->opaque == opaque &&
7809 ban->deleted == false)
7811 if (bs->walking_aio_notifiers) {
7812 ban->deleted = true;
7829 if (!bs->drv) {
7831 return -ENOMEDIUM;
7833 if (!bs->drv->bdrv_amend_options) {
7835 bs->drv->format_name);
7836 return -ENOTSUP;
7838 return bs->drv->bdrv_amend_options(bs, opts, status_cb,
7861 if (!bs || !bs->drv) {
7870 if (bs->drv->bdrv_recurse_can_replace) {
7871 return bs->drv->bdrv_recurse_can_replace(bs, to_replace);
7903 error_setg(errp, "Failed to find node with node-name='%s'", node_name);
7920 node_name, parent_bs->node_name);
7931 * "read-zeroes" options are strong, but its "latency-ns" option is
7949 if (curopt == &global_options[ARRAY_SIZE(global_options) - 1] && bs->drv) {
7950 curopt = bs->drv->strong_runtime_opts;
7957 * Copies all strong runtime options from bs->options to the given
7961 * Returns true iff any strong option was present in bs->options (and
7972 if (!bs->drv) {
7980 if ((*option_name)[strlen(*option_name) - 1] != '.') {
7981 QObject *entry = qdict_get(bs->options, *option_name);
7990 for (entry = qdict_first(bs->options); entry;
7991 entry = qdict_next(bs->options, entry))
8013 qdict_put_str(d, "driver", bs->drv->format_name);
8021 * would result in exactly bs->backing. */
8025 if (bs->backing) {
8026 return strcmp(bs->auto_backing_file,
8027 bs->backing->bs->filename);
8031 return bs->auto_backing_file[0] != '\0';
8036 * - exact_filename: A filename which may be used for opening a block device
8040 * - full_open_options: Options which, when given when opening a block device
8043 * - filename: If exact_filename is set, it is copied here. Otherwise,
8049 BlockDriver *drv = bs->drv;
8065 QLIST_FOREACH(child, &bs->children, next) {
8066 bdrv_refresh_filename(child->bs);
8069 if (bs->implicit) {
8071 child = QLIST_FIRST(&bs->children);
8074 pstrcpy(bs->exact_filename, sizeof(bs->exact_filename),
8075 child->bs->exact_filename);
8076 pstrcpy(bs->filename, sizeof(bs->filename), child->bs->filename);
8078 qobject_unref(bs->full_open_options);
8079 bs->full_open_options = qobject_ref(child->bs->full_open_options);
8086 if (bs->open_flags & BDRV_O_NO_IO) {
8088 * Therefore, in such a case (primarily qemu-img), we can
8099 if (drv->bdrv_gather_child_options) {
8102 drv->bdrv_gather_child_options(bs, opts, backing_overridden);
8104 QLIST_FOREACH(child, &bs->children, next) {
8105 if (child == bs->backing && !backing_overridden) {
8110 qdict_put(opts, child->name,
8111 qobject_ref(child->bs->full_open_options));
8114 if (backing_overridden && !bs->backing) {
8120 qobject_unref(bs->full_open_options);
8121 bs->full_open_options = opts;
8125 if (drv->bdrv_refresh_filename) {
8128 bs->exact_filename[0] = '\0';
8130 drv->bdrv_refresh_filename(bs);
8134 * file -- this only works for format nodes (filter nodes
8141 bs->exact_filename[0] = '\0';
8145 * - it has a filename,
8146 * - the current BDS is not a filter,
8147 * - the file is a protocol BDS, and
8148 * - opening that file (as this BDS's format) will automatically create
8150 * - the user did not significantly change this BDS's behavior with
8152 * - no non-file child of this BDS has been overridden by the user
8155 if (primary_child_bs->exact_filename[0] &&
8156 primary_child_bs->drv->protocol_name &&
8157 !drv->is_filter && !generate_json_filename)
8159 strcpy(bs->exact_filename, primary_child_bs->exact_filename);
8163 if (bs->exact_filename[0]) {
8164 pstrcpy(bs->filename, sizeof(bs->filename), bs->exact_filename);
8166 GString *json = qobject_to_json(QOBJECT(bs->full_open_options));
8167 if (snprintf(bs->filename, sizeof(bs->filename), "json:%s",
8168 json->str) >= sizeof(bs->filename)) {
8170 strcpy(bs->filename + sizeof(bs->filename) - 4, "...");
8178 BlockDriver *drv = bs->drv;
8184 error_setg(errp, "Node '%s' is ejected", bs->node_name);
8188 if (drv->bdrv_dirname) {
8189 return drv->bdrv_dirname(bs, errp);
8198 if (bs->exact_filename[0] != '\0') {
8199 return path_combine(bs->exact_filename, "");
8203 drv->format_name);
8217 if (!parent_bs->drv || !parent_bs->drv->bdrv_add_child) {
8224 * Non-zoned block drivers do not follow zoned storage constraints
8225 * (i.e. sequential writes to zones). Refuse mixing zoned and non-zoned
8228 if (!parent_bs->drv->supports_zoned_children &&
8229 child_bs->bl.zoned == BLK_Z_HM) {
8231 * The host-aware model allows zoned storage constraints and random
8232 * write. Allow mixing host-aware and non-zoned drivers. Using
8233 * host-aware device as a regular device.
8236 child_bs->bl.zoned == BLK_Z_HM ? "zoned" : "non-zoned",
8237 parent_bs->drv->supports_zoned_children ?
8242 if (!QLIST_EMPTY(&child_bs->parents)) {
8244 child_bs->node_name);
8248 parent_bs->drv->bdrv_add_child(parent_bs, child_bs, errp);
8262 if (!parent_bs->drv || !parent_bs->drv->bdrv_del_child) {
8268 QLIST_FOREACH(tmp, &parent_bs->children, next) {
8277 bdrv_get_device_or_node_name(child->bs));
8281 parent_bs->drv->bdrv_del_child(parent_bs, child, errp);
8286 BlockDriver *drv = c->bs->drv;
8290 assert(c->perm & (BLK_PERM_WRITE | BLK_PERM_WRITE_UNCHANGED));
8292 if (!drv->bdrv_make_empty) {
8294 drv->format_name);
8295 return -ENOTSUP;
8298 ret = drv->bdrv_make_empty(c->bs);
8300 error_setg_errno(errp, -ret, "Failed to empty %s",
8301 c->bs->filename);
8316 if (!bs || !bs->drv) {
8320 if (bs->drv->is_filter) {
8324 if (!bs->backing) {
8328 assert(bs->backing->role & BDRV_CHILD_COW);
8329 return bs->backing;
8341 if (!bs || !bs->drv) {
8345 if (!bs->drv->is_filter) {
8350 assert(!(bs->backing && bs->file));
8352 c = bs->backing ?: bs->file;
8357 assert(c->role & BDRV_CHILD_FILTERED);
8363 * whichever is non-NULL.
8394 QLIST_FOREACH(c, &bs->children, next) {
8395 if (c->role & BDRV_CHILD_PRIMARY) {
8413 while (!(stop_on_explicit_filter && !bs->implicit)) {
8422 assert(!bs->drv || !bs->drv->is_filter);
8425 bs = c->bs;
8428 * Note that this treats nodes with bs->drv == NULL as not being
8429 * filters (bs->drv == NULL should be replaced by something else
8432 * always return a non-NULL value (given a non-NULL @bs).
8460 * For a backing chain, return the first non-filter backing image of
8461 * the first non-filter image.
8471 * block-status data region.
8473 * If so, and @pnum is not NULL, set *pnum to `bsc.data_end - offset`,
8481 BdrvBlockStatusCache *bsc = qatomic_rcu_read(&bs->block_status_cache);
8485 qatomic_read(&bsc->valid) &&
8486 ranges_overlap(offset, bytes, bsc->data_start,
8487 bsc->data_end - bsc->data_start);
8490 *pnum = bsc->data_end - offset;
8516 qatomic_set(&bs->block_status_cache->valid, false);
8535 QEMU_LOCK_GUARD(&bs->bsc_modify_lock);
8537 old_bsc = qatomic_rcu_read(&bs->block_status_cache);
8538 qatomic_rcu_set(&bs->block_status_cache, new_bsc);