Lines Matching refs:bs

91 static bool bdrv_recurse_has_child(BlockDriverState *bs,
106 static bool bdrv_backing_overridden(BlockDriverState *bs);
108 static bool bdrv_change_aio_context(BlockDriverState *bs, AioContext *ctx,
135 size_t bdrv_opt_mem_align(BlockDriverState *bs)
137 if (!bs || !bs->drv) {
143 return bs->bl.opt_mem_alignment;
146 size_t bdrv_min_mem_align(BlockDriverState *bs)
148 if (!bs || !bs->drv) {
154 return bs->bl.min_mem_alignment;
276 bool bdrv_is_read_only(BlockDriverState *bs)
279 return !(bs->open_flags & BDRV_O_RDWR);
283 bdrv_can_set_read_only(BlockDriverState *bs, bool read_only,
289 if (bs->copy_on_read && read_only) {
291 bdrv_get_device_or_node_name(bs));
296 if (!read_only && !(bs->open_flags & BDRV_O_ALLOW_RDWR) &&
300 bdrv_get_device_or_node_name(bs));
317 int bdrv_apply_auto_read_only(BlockDriverState *bs, const char *errmsg,
323 if (!(bs->open_flags & BDRV_O_RDWR)) {
326 if (!(bs->open_flags & BDRV_O_AUTO_RDONLY)) {
330 ret = bdrv_can_set_read_only(bs, true, false, NULL);
335 bs->open_flags &= ~BDRV_O_RDWR;
397 char *bdrv_get_full_backing_filename(BlockDriverState *bs, Error **errp)
400 return bdrv_make_absolute_filename(bs, bs->backing_file, errp);
412 BlockDriverState *bs;
417 bs = g_new0(BlockDriverState, 1);
418 QLIST_INIT(&bs->dirty_bitmaps);
420 QLIST_INIT(&bs->op_blockers[i]);
422 qemu_mutex_init(&bs->reqs_lock);
423 qemu_mutex_init(&bs->dirty_bitmap_mutex);
424 bs->refcnt = 1;
425 bs->aio_context = qemu_get_aio_context();
427 qemu_co_queue_init(&bs->flush_queue);
429 qemu_co_mutex_init(&bs->bsc_modify_lock);
430 bs->block_status_cache = g_new0(BdrvBlockStatusCache, 1);
433 bdrv_drained_begin(bs);
436 QTAILQ_INSERT_TAIL(&all_bdrv_states, bs, bs_list);
438 return bs;
743 int coroutine_fn bdrv_co_delete_file(BlockDriverState *bs, Error **errp)
749 assert(bs != NULL);
752 if (!bs->drv) {
753 error_setg(errp, "Block node '%s' is not opened", bs->filename);
757 if (!bs->drv->bdrv_co_delete_file) {
759 bs->drv->format_name);
763 ret = bs->drv->bdrv_co_delete_file(bs, &local_err);
771 void coroutine_fn bdrv_co_delete_file_noerr(BlockDriverState *bs)
777 if (!bs) {
781 ret = bdrv_co_delete_file(bs, &local_err);
795 * Try to get @bs's logical and physical block size.
798 * @bs must not be empty.
800 int bdrv_probe_blocksizes(BlockDriverState *bs, BlockSizes *bsz)
802 BlockDriver *drv = bs->drv;
803 BlockDriverState *filtered = bdrv_filter_bs(bs);
807 return drv->bdrv_probe_blocksizes(bs, bsz);
816 * Try to get @bs's geometry (cyls, heads, sectors).
819 * @bs must not be empty.
821 int bdrv_probe_geometry(BlockDriverState *bs, HDGeometry *geo)
823 BlockDriver *drv = bs->drv;
830 return drv->bdrv_probe_geometry(bs, geo);
833 filtered = bdrv_filter_bs(bs);
1052 int coroutine_fn bdrv_co_refresh_total_sectors(BlockDriverState *bs,
1055 BlockDriver *drv = bs->drv;
1064 if (bdrv_is_sg(bs))
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) {
1089 static void bdrv_join_options(BlockDriverState *bs, QDict *options,
1093 if (bs->drv && bs->drv->bdrv_join_options) {
1094 bs->drv->bdrv_join_options(options, old_options);
1205 BlockDriverState *bs = child->opaque;
1206 bdrv_do_drained_begin_quiesce(bs, NULL);
1211 BlockDriverState *bs = child->opaque;
1212 return bdrv_drain_poll(bs, NULL, false);
1217 BlockDriverState *bs = child->opaque;
1218 bdrv_drained_end(bs);
1223 BlockDriverState *bs = child->opaque;
1225 assert(bs->open_flags & BDRV_O_INACTIVE);
1233 BlockDriverState *bs = child->opaque;
1234 return bdrv_change_aio_context(bs, ctx, visited, tran, errp);
1264 BlockDriverState *backing_hd = c->bs;
1285 * The target bs is new opened, and the source is top BDS
1307 bdrv_op_unblock_all(c->bs, parent->backing_blocker);
1441 BlockDriverState *bs = child->opaque;
1444 QLIST_INSERT_HEAD(&bs->children, child, next);
1445 if (bs->drv->is_filter || (child->role & BDRV_CHILD_FILTERED)) {
1451 * So bs->file will be the PRIMARY child, unless the PRIMARY child goes
1452 * into bs->backing on exceptional cases; and bs->backing will be
1458 assert(!bs->backing);
1459 assert(!bs->file);
1461 if (bs->drv->filtered_child_is_backing) {
1462 bs->backing = child;
1464 bs->file = child;
1470 assert(bs->drv->supports_backing);
1472 assert(!bs->backing);
1473 bs->backing = child;
1476 assert(!bs->file);
1477 bs->file = child;
1483 BlockDriverState *bs = child->opaque;
1491 if (child == bs->backing) {
1492 assert(child != bs->file);
1493 bs->backing = NULL;
1494 } else if (child == bs->file) {
1495 bs->file = NULL;
1514 BlockDriverState *bs = c->opaque;
1517 return bdrv_get_aio_context(bs);
1541 static int bdrv_open_flags(BlockDriverState *bs, int flags)
1597 static void bdrv_assign_node_name(BlockDriverState *bs,
1629 if (strlen(node_name) >= sizeof(bs->node_name)) {
1634 /* copy node name into the bs and insert it into the graph list */
1635 pstrcpy(bs->node_name, sizeof(bs->node_name), node_name);
1636 QTAILQ_INSERT_TAIL(&graph_bdrv_states, bs, node_list);
1642 bdrv_open_driver(BlockDriverState *bs, BlockDriver *drv, const char *node_name,
1649 bdrv_assign_node_name(bs, node_name, &local_err);
1655 bs->drv = drv;
1656 bs->opaque = g_malloc0(drv->instance_size);
1659 assert(!drv->bdrv_needs_filename || bs->filename[0]);
1660 ret = drv->bdrv_file_open(bs, options, open_flags, &local_err);
1662 ret = drv->bdrv_open(bs, options, open_flags, &local_err);
1670 } else if (bs->filename[0]) {
1671 error_setg_errno(errp, -ret, "Could not open '%s'", bs->filename);
1678 assert(!(bs->supported_read_flags & ~BDRV_REQ_MASK));
1679 assert(!(bs->supported_write_flags & ~BDRV_REQ_MASK));
1689 bs->supported_read_flags |= BDRV_REQ_REGISTERED_BUF;
1690 bs->supported_write_flags |= BDRV_REQ_REGISTERED_BUF;
1692 ret = bdrv_refresh_total_sectors(bs, bs->total_sectors);
1699 bdrv_refresh_limits(bs, NULL, &local_err);
1707 assert(bdrv_opt_mem_align(bs) != 0);
1708 assert(bdrv_min_mem_align(bs) != 0);
1709 assert(is_power_of_2(bs->bl.request_alignment));
1711 for (i = 0; i < bs->quiesce_counter; i++) {
1713 drv->bdrv_drain_begin(bs);
1719 bs->drv = NULL;
1722 if (bs->file != NULL) {
1723 bdrv_unref_child(bs, bs->file);
1724 assert(!bs->file);
1728 g_free(bs->opaque);
1729 bs->opaque = NULL;
1746 BlockDriverState *bs;
1751 bs = bdrv_new();
1752 bs->open_flags = flags;
1753 bs->options = options ?: qdict_new();
1754 bs->explicit_options = qdict_clone_shallow(bs->options);
1755 bs->opaque = NULL;
1757 update_options_from_flags(bs->options, flags);
1759 ret = bdrv_open_driver(bs, drv, node_name, bs->options, flags, errp);
1761 qobject_unref(bs->explicit_options);
1762 bs->explicit_options = NULL;
1763 qobject_unref(bs->options);
1764 bs->options = NULL;
1765 bdrv_unref(bs);
1769 return bs;
1856 static int bdrv_open_common(BlockDriverState *bs, BlockBackend *file,
1872 assert(bs->file == NULL);
1873 assert(options != NULL && bs->options != options);
1882 update_flags_from_options(&bs->open_flags, opts);
1888 bs->force_share = qemu_opt_get_bool(opts, BDRV_OPT_FORCE_SHARE, false);
1890 if (bs->force_share && (bs->open_flags & BDRV_O_RDWR)) {
1922 trace_bdrv_open_common(bs, filename ?: "", bs->open_flags,
1925 ro = bdrv_is_read_only(bs);
1930 ret = bdrv_apply_auto_read_only(bs, NULL, NULL);
1946 assert(qatomic_read(&bs->copy_on_read) == 0);
1948 if (bs->open_flags & BDRV_O_COPY_ON_READ) {
1950 bdrv_enable_copy_on_read(bs);
1960 if (bdrv_parse_discard_flags(discard, &bs->open_flags) != 0) {
1967 bs->detect_zeroes =
1968 bdrv_parse_detect_zeroes(opts, bs->open_flags, &local_err);
1976 pstrcpy(bs->filename, sizeof(bs->filename), filename);
1978 bs->filename[0] = '\0';
1980 pstrcpy(bs->exact_filename, sizeof(bs->exact_filename), bs->filename);
1983 open_flags = bdrv_open_flags(bs, bs->open_flags);
1987 ret = bdrv_open_driver(bs, drv, node_name, options, open_flags, errp);
2155 * Return the flags that @bs will have after the reopens in @q have
2156 * successfully completed. If @q is NULL (or @bs is not contained in @q),
2159 static int bdrv_reopen_get_flags(BlockReopenQueue *q, BlockDriverState *bs)
2165 if (entry->state.bs == bs) {
2171 return bs->open_flags;
2176 static bool bdrv_is_writable_after_reopen(BlockDriverState *bs,
2179 int flags = bdrv_reopen_get_flags(q, bs);
2186 * the same as !bdrv_is_read_only(bs), as inactivated images may not
2189 bool bdrv_is_writable(BlockDriverState *bs)
2192 return bdrv_is_writable_after_reopen(bs, NULL);
2212 assert(a->bs);
2213 assert(a->bs == b->bs);
2220 child_bs_name = bdrv_get_node_name(b->bs);
2236 bdrv_parent_perms_conflict(BlockDriverState *bs, Error **errp)
2246 QLIST_FOREACH(a, &bs->parents, next_parent) {
2247 QLIST_FOREACH(b, &bs->parents, next_parent) {
2262 bdrv_child_perm(BlockDriverState *bs, BlockDriverState *child_bs,
2268 assert(bs->drv && bs->drv->bdrv_child_perm);
2270 bs->drv->bdrv_child_perm(bs, c, role, reopen_queue,
2280 * Adds the whole subtree of @bs (including @bs itself) to the @list (except for
2291 bdrv_topological_dfs(GSList *list, GHashTable *found, BlockDriverState *bs)
2303 if (g_hash_table_contains(found, bs)) {
2306 g_hash_table_add(found, bs);
2308 QLIST_FOREACH(child, &bs->children, next) {
2309 list = bdrv_topological_dfs(list, found, child->bs);
2312 return g_slist_prepend(list, bs);
2356 BlockDriverState *bs = opaque;
2360 if (bs->drv->bdrv_set_perm) {
2361 bdrv_get_cumulative_perm(bs, &cumulative_perms,
2363 bs->drv->bdrv_set_perm(bs, cumulative_perms, cumulative_shared_perms);
2369 BlockDriverState *bs = opaque;
2372 if (bs->drv->bdrv_abort_perm_update) {
2373 bs->drv->bdrv_abort_perm_update(bs);
2387 bdrv_drv_set_perm(BlockDriverState *bs, uint64_t perm, uint64_t shared_perm,
2391 if (!bs->drv) {
2395 if (bs->drv->bdrv_check_perm) {
2396 int ret = bs->drv->bdrv_check_perm(bs, perm, shared_perm, errp);
2403 tran_add(tran, &bdrv_drv_set_perm_drv, bs);
2425 BlockDriverState *new_bs = s->child->bs;
2431 if (!s->child->bs) {
2461 * Both @child->bs and @new_bs (if non-NULL) must be drained. @new_bs must be
2480 .old_bs = child->bs,
2493 * Refresh permissions in @bs subtree. The function is intended to be called
2500 bdrv_node_refresh_perm(BlockDriverState *bs, BlockReopenQueue *q,
2503 BlockDriver *drv = bs->drv;
2509 bdrv_get_cumulative_perm(bs, &cumulative_perms, &cumulative_shared_perms);
2513 !bdrv_is_writable_after_reopen(bs, q))
2515 if (!bdrv_is_writable_after_reopen(bs, NULL)) {
2519 "read-write users", bdrv_get_node_name(bs));
2533 if ((bs->total_sectors * BDRV_SECTOR_SIZE) % bs->bl.request_alignment) {
2546 ret = bdrv_drv_set_perm(bs, cumulative_perms, cumulative_shared_perms, tran,
2554 assert(QLIST_EMPTY(&bs->children));
2559 QLIST_FOREACH(c, &bs->children, next) {
2562 bdrv_child_perm(bs, c->bs, c, c->role, q,
2583 BlockDriverState *bs;
2587 bs = list->data;
2589 if (bdrv_parent_perms_conflict(bs, errp)) {
2593 ret = bdrv_node_refresh_perm(bs, q, tran, errp);
2624 void bdrv_get_cumulative_perm(BlockDriverState *bs, uint64_t *perm,
2633 QLIST_FOREACH(c, &bs->parents, next_parent) {
2678 bdrv_refresh_perms(BlockDriverState *bs, Transaction *tran, Error **errp)
2682 g_autoptr(GSList) list = bdrv_topological_dfs(NULL, NULL, bs);
2709 ret = bdrv_refresh_perms(c->bs, tran, &local_err);
2732 int bdrv_child_refresh_perms(BlockDriverState *bs, BdrvChild *c, Error **errp)
2739 bdrv_get_cumulative_perm(bs, &parent_perms, &parent_shared);
2740 bdrv_child_perm(bs, c->bs, c, c->role, NULL,
2751 static void bdrv_filter_default_perms(BlockDriverState *bs, BdrvChild *c,
2762 static void bdrv_default_perms_for_cow(BlockDriverState *bs, BdrvChild *c,
2790 if (bs->open_flags & BDRV_O_INACTIVE) {
2798 static void bdrv_default_perms_for_storage(BlockDriverState *bs, BdrvChild *c,
2809 flags = bdrv_reopen_get_flags(reopen_queue, bs);
2815 bdrv_filter_default_perms(bs, c, role, reopen_queue,
2820 if (bdrv_is_writable_after_reopen(bs, reopen_queue)) {
2825 * bs->file always needs to be consistent because of the
2870 if (bs->open_flags & BDRV_O_INACTIVE) {
2878 void bdrv_default_perms(BlockDriverState *bs, BdrvChild *c,
2887 bdrv_filter_default_perms(bs, c, role, reopen_queue,
2891 bdrv_default_perms_for_cow(bs, c, role, reopen_queue,
2894 bdrv_default_perms_for_storage(bs, c, role, reopen_queue,
2927 BlockDriverState *old_bs = child->bs;
2934 * child->bs, we need to make sure that its new parent is drained, too. In
2967 child->bs = new_bs;
2990 * The child must be empty (i.e. `child->bs == NULL`) and it must be
2995 assert(!child->bs);
3014 BlockDriverState *bs = s->child->bs;
3021 if (bdrv_get_aio_context(bs) != s->old_child_ctx) {
3022 bdrv_try_change_aio_context(bs, s->old_child_ctx, NULL, &error_abort);
3043 bdrv_schedule_unref(bs);
3053 * Common part of attaching bdrv child to bs or to blk or to job
3083 .bs = NULL,
3279 BlockDriverState *child_bs = child->bs;
3305 BlockDriverState *bs;
3313 s->bs->inherits_from = s->old_inherits_from;
3322 static void bdrv_set_inherits_from(BlockDriverState *bs,
3330 .bs = bs,
3331 .old_inherits_from = bs->inherits_from,
3337 bs->inherits_from = new_inherits_from;
3351 if (child->bs->inherits_from == root) {
3354 * child->bs goes away.
3357 if (c != child && c->bs == child->bs) {
3362 bdrv_set_inherits_from(child->bs, NULL, tran);
3366 QLIST_FOREACH(c, &child->bs->children, next) {
3385 bdrv_parent_cb_change_media(BlockDriverState *bs, bool load)
3389 QLIST_FOREACH(c, &bs->parents, next_parent) {
3409 * Return the BdrvChildRole for @bs's backing child. bs->backing is
3413 static BdrvChildRole bdrv_backing_role(BlockDriverState *bs)
3415 if (bs->drv && bs->drv->is_filter) {
3423 * Sets the bs->backing or bs->file link of a BDS. A new reference is created;
3453 * qcow2 driver to never clear bs->drv and implement format corruption
3462 child->name, parent_bs->node_name, child->bs->node_name);
3492 assert(child->bs->quiesce_counter);
3511 * If inherits_from pointed recursively to bs then let's update it to
3512 * point directly to bs (else it will become NULL).
3525 * Both @bs and @backing_hd can move to a different AioContext in this
3531 int bdrv_set_backing_hd_drained(BlockDriverState *bs,
3539 assert(bs->quiesce_counter > 0);
3540 if (bs->backing) {
3541 assert(bs->backing->bs->quiesce_counter > 0);
3544 ret = bdrv_set_file_or_backing_noperm(bs, backing_hd, true, tran, errp);
3549 ret = bdrv_refresh_perms(bs, tran, errp);
3555 int bdrv_set_backing_hd(BlockDriverState *bs, BlockDriverState *backing_hd,
3563 drain_bs = bs->backing ? bs->backing->bs : bs;
3569 ret = bdrv_set_backing_hd_drained(bs, backing_hd, errp);
3587 int bdrv_open_backing_file(BlockDriverState *bs, QDict *parent_options,
3604 if (bs->backing != NULL) {
3614 bs->open_flags &= ~BDRV_O_NO_BACKING;
3630 } else if (bs->backing_file[0] == '\0' && qdict_size(options) == 0) {
3641 implicit_backing = !strcmp(bs->auto_backing_file, bs->backing_file);
3644 backing_filename = bdrv_get_full_backing_filename(bs, &local_err);
3653 if (!bs->drv || !bs->drv->supports_backing) {
3661 bs->backing_format[0] != '\0' && !qdict_haskey(options, "driver")) {
3662 qdict_put_str(options, "driver", bs->backing_format);
3665 backing_hd = bdrv_open_inherit(backing_filename, reference, options, 0, bs,
3666 &child_of_bds, bdrv_backing_role(bs), errp);
3668 bs->open_flags |= BDRV_O_NO_BACKING;
3676 pstrcpy(bs->auto_backing_file, sizeof(bs->auto_backing_file),
3680 /* Hook up the backing file link; drop our reference, bs owns the
3682 ret = bdrv_set_backing_hd(bs, backing_hd, errp);
3702 BlockDriverState *bs = NULL;
3730 bs = bdrv_open_inherit(filename, reference, image_options, 0,
3732 if (!bs) {
3738 return bs;
3764 BlockDriverState *bs;
3769 bs = bdrv_open_child_bs(filename, options, bdref_key, parent, child_class,
3771 if (bs == NULL) {
3776 child = bdrv_attach_child(parent, bs, bdref_key, child_class, child_role,
3784 * Wrapper on bdrv_open_child() for most popular case: open primary child of bs.
3814 BlockDriverState *bs = NULL;
3845 bs = bdrv_open_inherit(NULL, reference, qdict, 0, NULL, NULL, 0, errp);
3849 return bs;
3852 static BlockDriverState *bdrv_append_temp_snapshot(BlockDriverState *bs,
3870 total_size = bdrv_getlength(bs);
3905 ret = bdrv_append(bs_snapshot, bs, errp);
3939 BlockDriverState *bs;
3966 bs = bdrv_lookup_bs(reference, reference, errp);
3967 if (!bs) {
3971 bdrv_ref(bs);
3972 return bs;
3975 bs = bdrv_new();
3988 bs->explicit_options = qdict_clone_shallow(options);
4004 bs->inherits_from = parent;
4039 bs->open_flags = flags;
4040 bs->options = options;
4066 qdict_del(bs->explicit_options, "backing");
4067 qdict_del(bs->options, "backing");
4077 file_bs = bdrv_open_child_bs(filename, options, "file", bs,
4101 bs->probed = !drv;
4109 * but we first need to open bs->file for the probing to work, while
4110 * opening bs->file already requires the (mostly) final set of options
4115 * sure to update both bs->options (which has the full effective
4116 * options for bs) and options (which has file.* already removed).
4118 qdict_put_str(bs->options, "driver", drv->format_name);
4132 ret = bdrv_open_common(bs, file, options, &local_err);
4144 ret = bdrv_open_backing_file(bs, options, "backing", &local_err);
4151 * from bs->options and bs->explicit_options */
4152 QLIST_FOREACH(child, &bs->children, next) {
4155 qdict_extract_subqdict(bs->explicit_options, NULL, child_key_dot);
4156 qdict_extract_subqdict(bs->options, NULL, child_key_dot);
4157 qdict_del(bs->explicit_options, child->name);
4158 qdict_del(bs->options, child->name);
4177 bdrv_parent_cb_change_media(bs, true);
4182 /* For snapshot=on, create a temporary qcow2 overlay. bs points to the
4186 snapshot_bs = bdrv_append_temp_snapshot(bs, snapshot_flags,
4192 /* We are not going to return bs but the overlay on top of it
4193 * (snapshot_bs); thus, we have to drop the strong reference to bs
4194 * (which we obtained by calling bdrv_new()). bs will not be deleted,
4196 bdrv_unref(bs);
4197 bs = snapshot_bs;
4200 return bs;
4205 qobject_unref(bs->explicit_options);
4206 qobject_unref(bs->options);
4208 bs->options = NULL;
4209 bs->explicit_options = NULL;
4210 bdrv_unref(bs);
4215 bdrv_unref(bs);
4246 * Check that every option set in @bs->options is also set in
4250 * @bs->drv->mutable_opts are skipped.
4254 static int bdrv_reset_options_allowed(BlockDriverState *bs,
4265 for (e = qdict_first(bs->options); e; e = qdict_next(bs->options, e)) {
4268 !is_str_in_list(e->key, bs->drv->mutable_opts)) {
4279 * Returns true if @child can be reached recursively from @bs
4282 bdrv_recurse_has_child(BlockDriverState *bs, BlockDriverState *child)
4286 if (bs == child) {
4290 QLIST_FOREACH(c, &bs->children, next) {
4291 if (bdrv_recurse_has_child(c->bs, child)) {
4309 * bs is the BlockDriverState to add to the reopen queue.
4311 * options contains the changed options for the associated bs
4314 * flags contains the open flags for the associated bs
4319 * bs is drained here and undrained by bdrv_reopen_queue_free().
4321 * To be called with bs->aio_context locked.
4324 bdrv_reopen_queue_child(BlockReopenQueue *bs_queue, BlockDriverState *bs,
4330 assert(bs != NULL);
4345 bdrv_drained_begin(bs);
4358 if (bs == bs_entry->state.bs) {
4366 * 2. Retained from explicitly set options of bs
4368 * 4. Retained from effective options of bs
4375 bs->explicit_options);
4376 bdrv_join_options(bs, options, old_options);
4388 flags = bdrv_get_flags(bs);
4393 old_options = qdict_clone_shallow(bs->options);
4394 bdrv_join_options(bs, options, old_options);
4420 bs_entry->state.bs = bs;
4437 QLIST_FOREACH(child, &bs->children, next) {
4444 if (child->bs->inherits_from != bs) {
4455 if (g_strcmp0(childref, child->bs->node_name)) {
4472 bdrv_reopen_queue_child(bs_queue, child->bs, new_child_options,
4473 child->klass, child->role, bs->drv->is_format,
4480 /* To be called with bs->aio_context locked */
4482 BlockDriverState *bs,
4488 return bdrv_reopen_queue_child(bs_queue, bs, options, NULL, 0, false,
4498 bdrv_drained_end(bs_entry->state.bs);
4538 ret = bdrv_flush(bs_entry->state.bs);
4546 assert(bs_entry->state.bs->quiesce_counter > 0);
4557 refresh_list = g_slist_prepend(refresh_list, state->bs);
4598 BlockDriverState *bs = bs_entry->state.bs;
4600 if (bs->drv->bdrv_reopen_commit_post) {
4601 bs->drv->bdrv_reopen_commit_post(&bs_entry->state);
4625 int bdrv_reopen(BlockDriverState *bs, QDict *opts, bool keep_old_opts,
4632 queue = bdrv_reopen_queue(NULL, bs, opts, keep_old_opts);
4637 int bdrv_reopen_set_read_only(BlockDriverState *bs, bool read_only,
4646 return bdrv_reopen(bs, opts, true, errp);
4656 * reopen_state->bs then return 0.
4669 * @reopen_state->bs can move to a different AioContext in this function.
4676 BlockDriverState *bs = reopen_state->bs;
4708 has_child = bdrv_recurse_has_child(new_child_bs, bs);
4711 "cycle", str, child_name, bs->node_name);
4724 old_child_bs = is_backing ? child_bs(bs->backing) : child_bs(bs->file);
4738 child_name, bs->node_name);
4744 if (bs->drv->is_filter && !old_child_bs) {
4750 "%s child", bs->node_name, bs->drv->format_name, child_name);
4769 ret = bdrv_set_file_or_backing_noperm(bs, new_child_bs, is_backing,
4791 * bs is the BlockDriverState to reopen
4820 assert(reopen_state->bs->drv != NULL);
4822 drv = reopen_state->bs->drv;
4871 ret = bdrv_can_set_read_only(reopen_state->bs, read_only, true, &local_err);
4884 ret = bdrv_reset_options_allowed(reopen_state->bs,
4896 bdrv_refresh_filename(reopen_state->bs);
4899 reopen_state->bs->filename);
4909 bdrv_get_device_or_node_name(reopen_state->bs));
4924 (reopen_state->bs->backing || reopen_state->bs->backing_file[0])) {
4926 reopen_state->bs->node_name);
4963 QObject *old = qdict_get(reopen_state->bs->options, entry->key);
4969 QLIST_FOREACH(child, &reopen_state->bs->children, next) {
4976 if (!strcmp(child->bs->node_name,
5037 BlockDriverState *bs;
5041 bs = reopen_state->bs;
5042 drv = bs->drv;
5054 qobject_unref(bs->explicit_options);
5055 qobject_unref(bs->options);
5059 bs->explicit_options = reopen_state->explicit_options;
5060 bs->options = reopen_state->options;
5061 bs->open_flags = reopen_state->flags;
5062 bs->detect_zeroes = reopen_state->detect_zeroes;
5064 /* Remove child references from bs->options and bs->explicit_options.
5066 QLIST_FOREACH(child, &bs->children, next) {
5067 qdict_del(bs->explicit_options, child->name);
5068 qdict_del(bs->options, child->name);
5071 qdict_del(bs->explicit_options, "backing");
5072 qdict_del(bs->options, "backing");
5074 bdrv_refresh_limits(bs, NULL, NULL);
5075 bdrv_refresh_total_sectors(bs, bs->total_sectors);
5087 drv = reopen_state->bs->drv;
5097 static void bdrv_close(BlockDriverState *bs)
5103 assert(!bs->refcnt);
5105 bdrv_drained_begin(bs); /* complete I/O */
5106 bdrv_flush(bs);
5107 bdrv_drain(bs); /* in case flush left pending I/O */
5109 if (bs->drv) {
5110 if (bs->drv->bdrv_close) {
5112 bs->drv->bdrv_close(bs);
5114 bs->drv = NULL;
5118 QLIST_FOREACH_SAFE(child, &bs->children, next, next) {
5119 bdrv_unref_child(bs, child);
5122 assert(!bs->backing);
5123 assert(!bs->file);
5126 g_free(bs->opaque);
5127 bs->opaque = NULL;
5128 qatomic_set(&bs->copy_on_read, 0);
5129 bs->backing_file[0] = '\0';
5130 bs->backing_format[0] = '\0';
5131 bs->total_sectors = 0;
5132 bs->encrypted = false;
5133 bs->sg = false;
5134 qobject_unref(bs->options);
5135 qobject_unref(bs->explicit_options);
5136 bs->options = NULL;
5137 bs->explicit_options = NULL;
5138 qobject_unref(bs->full_open_options);
5139 bs->full_open_options = NULL;
5140 g_free(bs->block_status_cache);
5141 bs->block_status_cache = NULL;
5143 bdrv_release_named_dirty_bitmaps(bs);
5144 assert(QLIST_EMPTY(&bs->dirty_bitmaps));
5146 QLIST_FOREACH_SAFE(ban, &bs->aio_notifiers, list, ban_next) {
5149 QLIST_INIT(&bs->aio_notifiers);
5150 bdrv_drained_end(bs);
5157 if (bs->quiesce_counter) {
5158 bdrv_drain_all_end_quiesce(bs);
5188 * c->bs by @to would mean to create a loop.
5213 * In general, when replacing a node A (c->bs) by a node B (@to),
5241 if (g_hash_table_contains(found, c2->bs)) {
5245 g_queue_push_tail(queue, c2->bs);
5246 g_hash_table_add(found, c2->bs);
5269 * @child->bs (if non-NULL) must be drained.
5280 if (child->bs) {
5309 assert(c->bs == from);
5405 int bdrv_drop_filter(BlockDriverState *bs, Error **errp)
5413 child_bs = bdrv_filter_or_cow_bs(bs);
5418 ret = bdrv_replace_node_common(bs, child_bs, true, true, errp);
5426 * Add new bs contents at the top of an image chain while the chain is
5488 BlockDriverState *old_bs = child->bs;
5514 static void bdrv_delete(BlockDriverState *bs)
5516 assert(bdrv_op_blocker_is_empty(bs));
5517 assert(!bs->refcnt);
5521 if (bs->node_name[0] != '\0') {
5522 QTAILQ_REMOVE(&graph_bdrv_states, bs, node_list);
5524 QTAILQ_REMOVE(&all_bdrv_states, bs, bs_list);
5526 bdrv_close(bs);
5528 qemu_mutex_destroy(&bs->reqs_lock);
5530 g_free(bs);
5535 * Replace @bs by newly created block node.
5542 * The caller must make sure that @bs stays in the same AioContext, i.e.
5545 BlockDriverState *bdrv_insert_node(BlockDriverState *bs, QDict *options,
5550 AioContext *ctx = bdrv_get_aio_context(bs);
5573 assert(bdrv_get_aio_context(bs) == ctx);
5582 * Make sure that @bs doesn't go away until we have successfully attached
5585 bdrv_ref(bs);
5586 bdrv_drained_begin(bs);
5589 ret = bdrv_replace_node(bs, new_node_bs, errp);
5592 bdrv_drained_end(bs);
5593 bdrv_unref(bs);
5615 int coroutine_fn bdrv_co_check(BlockDriverState *bs,
5620 if (bs->drv == NULL) {
5623 if (bs->drv->bdrv_co_check == NULL) {
5628 return bs->drv->bdrv_co_check(bs, res, fix);
5640 bdrv_co_change_backing_file(BlockDriverState *bs, const char *backing_file,
5643 BlockDriver *drv = bs->drv;
5662 ret = drv->bdrv_co_change_backing_file(bs, backing_file, backing_fmt);
5668 pstrcpy(bs->backing_file, sizeof(bs->backing_file), backing_file ?: "");
5669 pstrcpy(bs->backing_format, sizeof(bs->backing_format), backing_fmt ?: "");
5670 pstrcpy(bs->auto_backing_file, sizeof(bs->auto_backing_file),
5677 * Finds the first non-filter node above bs in the chain between
5678 * active and bs. The returned node is either an immediate parent of
5679 * bs, or there are only filter nodes between the two.
5681 * Returns NULL if bs is not found in active's image chain,
5682 * or if active == bs.
5684 * Returns the bottommost base image if bs == NULL.
5687 BlockDriverState *bs)
5692 bs = bdrv_skip_filters(bs);
5697 if (bs == next) {
5707 BlockDriverState *bdrv_find_base(BlockDriverState *bs)
5711 return bdrv_find_overlay(bs, NULL);
5716 * between @bs and @base is frozen. @errp is set if that's the case.
5717 * @base must be reachable from @bs, or NULL.
5720 bdrv_is_backing_chain_frozen(BlockDriverState *bs, BlockDriverState *base,
5728 for (i = bs; i != base; i = child_bs(child)) {
5733 child->name, i->node_name, child->bs->node_name);
5742 * Freeze all COW (backing) and filter links between @bs and @base.
5745 * @base must be reachable from @bs, or NULL.
5748 int bdrv_freeze_backing_chain(BlockDriverState *bs, BlockDriverState *base,
5756 if (bdrv_is_backing_chain_frozen(bs, base, errp)) {
5760 for (i = bs; i != base; i = child_bs(child)) {
5762 if (child && child->bs->never_freeze) {
5764 child->name, child->bs->node_name);
5769 for (i = bs; i != base; i = child_bs(child)) {
5780 * Unfreeze all COW (backing) and filter links between @bs and @base.
5783 * @base must be reachable from @bs, or NULL.
5785 void bdrv_unfreeze_backing_chain(BlockDriverState *bs, BlockDriverState *base)
5792 for (i = bs; i != base; i = child_bs(child)) {
5806 * information in 'bs' can be properly updated.
5937 bdrv_sum_allocated_file_size(BlockDriverState *bs)
5942 QLIST_FOREACH(child, &bs->children, next) {
5946 child_size = bdrv_co_get_allocated_file_size(child->bs);
5961 int64_t coroutine_fn bdrv_co_get_allocated_file_size(BlockDriverState *bs)
5963 BlockDriver *drv = bs->drv;
5971 return drv->bdrv_co_get_allocated_file_size(bs);
5983 return bdrv_co_get_allocated_file_size(bdrv_filter_bs(bs));
5986 return bdrv_sum_allocated_file_size(bs);
6029 int64_t coroutine_fn bdrv_co_nb_sectors(BlockDriverState *bs)
6031 BlockDriver *drv = bs->drv;
6038 if (bs->bl.has_variable_length) {
6039 int ret = bdrv_co_refresh_total_sectors(bs, bs->total_sectors);
6044 return bs->total_sectors;
6051 int64_t coroutine_mixed_fn bdrv_nb_sectors(BlockDriverState *bs)
6053 BlockDriver *drv = bs->drv;
6059 if (bs->bl.has_variable_length) {
6060 int ret = bdrv_refresh_total_sectors(bs, bs->total_sectors);
6066 return bs->total_sectors;
6073 int64_t coroutine_fn bdrv_co_getlength(BlockDriverState *bs)
6079 ret = bdrv_co_nb_sectors(bs);
6089 bool bdrv_is_sg(BlockDriverState *bs)
6092 return bs->sg;
6098 bool bdrv_supports_compressed_writes(BlockDriverState *bs)
6103 if (!bs->drv || !block_driver_can_compress(bs->drv)) {
6107 filtered = bdrv_filter_bs(bs);
6119 const char *bdrv_get_format_name(BlockDriverState *bs)
6122 return bs->drv ? bs->drv->format_name : NULL;
6192 /* This function is to find a node in the bs graph */
6195 BlockDriverState *bs;
6200 QTAILQ_FOREACH(bs, &graph_bdrv_states, node_list) {
6201 if (!strcmp(node_name, bs->node_name)) {
6202 return bs;
6213 BlockDriverState *bs;
6219 QTAILQ_FOREACH(bs, &graph_bdrv_states, node_list) {
6220 BlockDeviceInfo *info = bdrv_block_device_info(NULL, bs, flat, errp);
6298 edge->child = xdbg_graph_node_num(gr, child->bs);
6320 BlockDriverState *bs;
6354 QTAILQ_FOREACH(bs, &graph_bdrv_states, node_list) {
6355 xdbg_graph_add_node(gr, bs, X_DBG_BLOCK_GRAPH_NODE_TYPE_BLOCK_DRIVER,
6356 bs->node_name);
6357 QLIST_FOREACH(child, &bs->children, next) {
6358 xdbg_graph_add_edge(gr, bs, child);
6370 BlockDriverState *bs;
6378 bs = blk_bs(blk);
6379 if (!bs) {
6383 return bs;
6388 bs = bdrv_find_node(node_name);
6390 if (bs) {
6391 return bs;
6415 BlockDriverState *bdrv_next_node(BlockDriverState *bs)
6418 if (!bs) {
6421 return QTAILQ_NEXT(bs, node_list);
6424 BlockDriverState *bdrv_next_all_states(BlockDriverState *bs)
6427 if (!bs) {
6430 return QTAILQ_NEXT(bs, bs_list);
6433 const char *bdrv_get_node_name(const BlockDriverState *bs)
6436 return bs->node_name;
6439 const char *bdrv_get_parent_name(const BlockDriverState *bs)
6446 QLIST_FOREACH(c, &bs->parents, next_parent) {
6458 /* TODO check what callers really want: bs->node_name or blk_name() */
6459 const char *bdrv_get_device_name(const BlockDriverState *bs)
6462 return bdrv_get_parent_name(bs) ?: "";
6469 const char *bdrv_get_device_or_node_name(const BlockDriverState *bs)
6472 return bdrv_get_parent_name(bs) ?: bs->node_name;
6475 int bdrv_get_flags(BlockDriverState *bs)
6478 return bs->open_flags;
6481 int bdrv_has_zero_init_1(BlockDriverState *bs)
6487 int coroutine_mixed_fn bdrv_has_zero_init(BlockDriverState *bs)
6492 if (!bs->drv) {
6498 if (bdrv_cow_child(bs)) {
6501 if (bs->drv->bdrv_has_zero_init) {
6502 return bs->drv->bdrv_has_zero_init(bs);
6505 filtered = bdrv_filter_bs(bs);
6514 bool bdrv_can_write_zeroes_with_unmap(BlockDriverState *bs)
6517 if (!(bs->open_flags & BDRV_O_UNMAP)) {
6521 return bs->supported_zero_flags & BDRV_REQ_MAY_UNMAP;
6524 void bdrv_get_backing_filename(BlockDriverState *bs,
6528 pstrcpy(filename, filename_size, bs->backing_file);
6531 int coroutine_fn bdrv_co_get_info(BlockDriverState *bs, BlockDriverInfo *bdi)
6534 BlockDriver *drv = bs->drv;
6538 /* if bs->drv == NULL, bs is closed, so there's nothing to do here */
6543 BlockDriverState *filtered = bdrv_filter_bs(bs);
6550 ret = drv->bdrv_co_get_info(bs, bdi);
6569 ImageInfoSpecific *bdrv_get_specific_info(BlockDriverState *bs,
6572 BlockDriver *drv = bs->drv;
6575 return drv->bdrv_get_specific_info(bs, errp);
6580 BlockStatsSpecific *bdrv_get_specific_stats(BlockDriverState *bs)
6582 BlockDriver *drv = bs->drv;
6587 return drv->bdrv_get_specific_stats(bs);
6590 void coroutine_fn bdrv_co_debug_event(BlockDriverState *bs, BlkdebugEvent event)
6595 if (!bs || !bs->drv || !bs->drv->bdrv_co_debug_event) {
6599 bs->drv->bdrv_co_debug_event(bs, event);
6603 bdrv_find_debug_node(BlockDriverState *bs)
6606 while (bs && bs->drv && !bs->drv->bdrv_debug_breakpoint) {
6607 bs = bdrv_primary_bs(bs);
6610 if (bs && bs->drv && bs->drv->bdrv_debug_breakpoint) {
6611 assert(bs->drv->bdrv_debug_remove_breakpoint);
6612 return bs;
6618 int bdrv_debug_breakpoint(BlockDriverState *bs, const char *event,
6624 bs = bdrv_find_debug_node(bs);
6625 if (bs) {
6626 return bs->drv->bdrv_debug_breakpoint(bs, event, tag);
6632 int bdrv_debug_remove_breakpoint(BlockDriverState *bs, const char *tag)
6637 bs = bdrv_find_debug_node(bs);
6638 if (bs) {
6639 return bs->drv->bdrv_debug_remove_breakpoint(bs, tag);
6645 int bdrv_debug_resume(BlockDriverState *bs, const char *tag)
6650 while (bs && (!bs->drv || !bs->drv->bdrv_debug_resume)) {
6651 bs = bdrv_primary_bs(bs);
6654 if (bs && bs->drv && bs->drv->bdrv_debug_resume) {
6655 return bs->drv->bdrv_debug_resume(bs, tag);
6661 bool bdrv_debug_is_suspended(BlockDriverState *bs, const char *tag)
6666 while (bs && bs->drv && !bs->drv->bdrv_debug_is_suspended) {
6667 bs = bdrv_primary_bs(bs);
6670 if (bs && bs->drv && bs->drv->bdrv_debug_is_suspended) {
6671 return bs->drv->bdrv_debug_is_suspended(bs, tag);
6678 * relative, it must be relative to the chain. So, passing in bs->filename
6681 BlockDriverState *bdrv_find_backing_image(BlockDriverState *bs,
6696 if (!bs || !bs->drv || !backing_file) {
6711 for (curr_bs = bdrv_skip_filters(bs);
6806 int bdrv_activate(BlockDriverState *bs, Error **errp)
6816 if (!bs->drv) {
6820 QLIST_FOREACH(child, &bs->children, next) {
6821 bdrv_activate(child->bs, &local_err);
6841 if (bs->open_flags & BDRV_O_INACTIVE) {
6842 bs->open_flags &= ~BDRV_O_INACTIVE;
6843 ret = bdrv_refresh_perms(bs, NULL, errp);
6845 bs->open_flags |= BDRV_O_INACTIVE;
6849 ret = bdrv_invalidate_cache(bs, errp);
6851 bs->open_flags |= BDRV_O_INACTIVE;
6855 FOR_EACH_DIRTY_BITMAP(bs, bm) {
6859 ret = bdrv_refresh_total_sectors(bs, bs->total_sectors);
6861 bs->open_flags |= BDRV_O_INACTIVE;
6867 QLIST_FOREACH(parent, &bs->parents, next_parent) {
6871 bs->open_flags |= BDRV_O_INACTIVE;
6881 int coroutine_fn bdrv_co_invalidate_cache(BlockDriverState *bs, Error **errp)
6886 assert(!(bs->open_flags & BDRV_O_INACTIVE));
6889 if (bs->drv->bdrv_co_invalidate_cache) {
6890 bs->drv->bdrv_co_invalidate_cache(bs, &local_err);
6902 BlockDriverState *bs;
6908 for (bs = bdrv_first(&it); bs; bs = bdrv_next(&it)) {
6911 ret = bdrv_activate(bs, errp);
6920 bdrv_has_bds_parent(BlockDriverState *bs, bool only_active)
6925 QLIST_FOREACH(parent, &bs->parents, next_parent) {
6937 static int GRAPH_RDLOCK bdrv_inactivate_recurse(BlockDriverState *bs)
6945 if (!bs->drv) {
6951 if (bdrv_has_bds_parent(bs, true)) {
6955 assert(!(bs->open_flags & BDRV_O_INACTIVE));
6958 if (bs->drv->bdrv_inactivate) {
6959 ret = bs->drv->bdrv_inactivate(bs);
6965 QLIST_FOREACH(parent, &bs->parents, next_parent) {
6974 bdrv_get_cumulative_perm(bs, &cumulative_perms,
6981 bs->open_flags |= BDRV_O_INACTIVE;
6988 bdrv_refresh_perms(bs, NULL, NULL);
6991 QLIST_FOREACH(child, &bs->children, next) {
6992 ret = bdrv_inactivate_recurse(child->bs);
7003 BlockDriverState *bs = NULL;
7010 for (bs = bdrv_first(&it); bs; bs = bdrv_next(&it)) {
7014 if (bdrv_has_bds_parent(bs, false)) {
7017 ret = bdrv_inactivate_recurse(bs);
7033 bool coroutine_fn bdrv_co_is_inserted(BlockDriverState *bs)
7035 BlockDriver *drv = bs->drv;
7044 return drv->bdrv_co_is_inserted(bs);
7046 QLIST_FOREACH(child, &bs->children, next) {
7047 if (!bdrv_co_is_inserted(child->bs)) {
7057 void coroutine_fn bdrv_co_eject(BlockDriverState *bs, bool eject_flag)
7059 BlockDriver *drv = bs->drv;
7064 drv->bdrv_co_eject(bs, eject_flag);
7072 void coroutine_fn bdrv_co_lock_medium(BlockDriverState *bs, bool locked)
7074 BlockDriver *drv = bs->drv;
7077 trace_bdrv_lock_medium(bs, locked);
7080 drv->bdrv_co_lock_medium(bs, locked);
7084 /* Get a reference to bs */
7085 void bdrv_ref(BlockDriverState *bs)
7088 bs->refcnt++;
7091 /* Release a previously grabbed reference to bs.
7094 void bdrv_unref(BlockDriverState *bs)
7097 if (!bs) {
7100 assert(bs->refcnt > 0);
7101 if (--bs->refcnt == 0) {
7102 bdrv_delete(bs);
7108 BlockDriverState *bs = opaque;
7110 bdrv_unref(bs);
7119 * possibly closing @bs until the graph lock is released.
7121 void bdrv_schedule_unref(BlockDriverState *bs)
7123 if (!bs) {
7126 aio_bh_schedule_oneshot(qemu_get_aio_context(), bdrv_schedule_unref_bh, bs);
7134 bool bdrv_op_is_blocked(BlockDriverState *bs, BlockOpType op, Error **errp)
7140 if (!QLIST_EMPTY(&bs->op_blockers[op])) {
7141 blocker = QLIST_FIRST(&bs->op_blockers[op]);
7144 bdrv_get_device_or_node_name(bs));
7150 void bdrv_op_block(BlockDriverState *bs, BlockOpType op, Error *reason)
7158 QLIST_INSERT_HEAD(&bs->op_blockers[op], blocker, list);
7161 void bdrv_op_unblock(BlockDriverState *bs, BlockOpType op, Error *reason)
7166 QLIST_FOREACH_SAFE(blocker, &bs->op_blockers[op], list, next) {
7174 void bdrv_op_block_all(BlockDriverState *bs, Error *reason)
7179 bdrv_op_block(bs, i, reason);
7183 void bdrv_op_unblock_all(BlockDriverState *bs, Error *reason)
7188 bdrv_op_unblock(bs, i, reason);
7192 bool bdrv_op_blocker_is_empty(BlockDriverState *bs)
7197 if (!QLIST_EMPTY(&bs->op_blockers[i])) {
7303 BlockDriverState *bs;
7330 bs = bdrv_open(full_backing, NULL, backing_options, back_flags,
7333 if (!bs) {
7341 bs->drv->format_name);
7346 size = bdrv_getlength(bs);
7350 bdrv_unref(bs);
7355 bdrv_unref(bs);
7401 AioContext *bdrv_get_aio_context(BlockDriverState *bs)
7404 return bs ? bs->aio_context : qemu_get_aio_context();
7407 AioContext *coroutine_fn bdrv_co_enter(BlockDriverState *bs)
7415 * Increase bs->in_flight to ensure that this operation is completed before
7418 bdrv_inc_in_flight(bs);
7420 new_ctx = bdrv_get_aio_context(bs);
7425 void coroutine_fn bdrv_co_leave(BlockDriverState *bs, AioContext *old_ctx)
7429 bdrv_dec_in_flight(bs);
7439 static void bdrv_detach_aio_context(BlockDriverState *bs)
7443 assert(!bs->walking_aio_notifiers);
7445 bs->walking_aio_notifiers = true;
7446 QLIST_FOREACH_SAFE(baf, &bs->aio_notifiers, list, baf_tmp) {
7456 bs->walking_aio_notifiers = false;
7458 if (bs->drv && bs->drv->bdrv_detach_aio_context) {
7459 bs->drv->bdrv_detach_aio_context(bs);
7462 bs->aio_context = NULL;
7465 static void bdrv_attach_aio_context(BlockDriverState *bs,
7471 bs->aio_context = new_context;
7473 if (bs->drv && bs->drv->bdrv_attach_aio_context) {
7474 bs->drv->bdrv_attach_aio_context(bs, new_context);
7477 assert(!bs->walking_aio_notifiers);
7478 bs->walking_aio_notifiers = true;
7479 QLIST_FOREACH_SAFE(ban, &bs->aio_notifiers, list, ban_tmp) {
7486 bs->walking_aio_notifiers = false;
7491 BlockDriverState *bs;
7531 return bdrv_change_aio_context(c->bs, ctx, visited, tran, errp);
7537 BlockDriverState *bs = (BlockDriverState *) state->bs;
7540 bdrv_drained_end(bs);
7548 BlockDriverState *bs = (BlockDriverState *) state->bs;
7551 bdrv_detach_aio_context(bs);
7552 bdrv_attach_aio_context(bs, new_context);
7569 static bool bdrv_change_aio_context(BlockDriverState *bs, AioContext *ctx,
7578 if (bdrv_get_aio_context(bs) == ctx) {
7583 QLIST_FOREACH(c, &bs->parents, next_parent) {
7590 QLIST_FOREACH(c, &bs->children, next) {
7601 .bs = bs,
7605 bdrv_drained_begin(bs);
7613 * Change bs's and recursively all of its parents' and children's AioContext
7619 int bdrv_try_change_aio_context(BlockDriverState *bs, AioContext *ctx,
7638 ret = bdrv_change_aio_context(bs, ctx, visited, tran, errp);
7658 void bdrv_add_aio_context_notifier(BlockDriverState *bs,
7670 QLIST_INSERT_HEAD(&bs->aio_notifiers, ban, list);
7673 void bdrv_remove_aio_context_notifier(BlockDriverState *bs,
7682 QLIST_FOREACH_SAFE(ban, &bs->aio_notifiers, list, ban_next) {
7688 if (bs->walking_aio_notifiers) {
7700 int bdrv_amend_options(BlockDriverState *bs, QemuOpts *opts,
7706 if (!bs->drv) {
7710 if (!bs->drv->bdrv_amend_options) {
7712 bs->drv->format_name);
7715 return bs->drv->bdrv_amend_options(bs, opts, status_cb,
7721 * replaced by a node that always shows the same data as @bs. This is
7723 * @to_replace by the target mirrored from @bs.
7724 * To be replaceable, @bs and @to_replace may either be guaranteed to
7731 bool bdrv_recurse_can_replace(BlockDriverState *bs,
7738 if (!bs || !bs->drv) {
7742 if (bs == to_replace) {
7747 if (bs->drv->bdrv_recurse_can_replace) {
7748 return bs->drv->bdrv_recurse_can_replace(bs, to_replace);
7752 filtered = bdrv_filter_bs(bs);
7814 static const char *const *strong_options(BlockDriverState *bs,
7826 if (curopt == &global_options[ARRAY_SIZE(global_options) - 1] && bs->drv) {
7827 curopt = bs->drv->strong_runtime_opts;
7834 * Copies all strong runtime options from bs->options to the given
7838 * Returns true iff any strong option was present in bs->options (and
7844 static bool append_strong_runtime_options(QDict *d, BlockDriverState *bs)
7849 if (!bs->drv) {
7853 while ((option_name = strong_options(bs, option_name))) {
7858 QObject *entry = qdict_get(bs->options, *option_name);
7867 for (entry = qdict_first(bs->options); entry;
7868 entry = qdict_next(bs->options, entry))
7890 qdict_put_str(d, "driver", bs->drv->format_name);
7897 * even if opening the backing file specified by bs's image header
7898 * would result in exactly bs->backing. */
7899 static bool GRAPH_RDLOCK bdrv_backing_overridden(BlockDriverState *bs)
7902 if (bs->backing) {
7903 return strcmp(bs->auto_backing_file,
7904 bs->backing->bs->filename);
7908 return bs->auto_backing_file[0] != '\0';
7924 void bdrv_refresh_filename(BlockDriverState *bs)
7926 BlockDriver *drv = bs->drv;
7942 QLIST_FOREACH(child, &bs->children, next) {
7943 bdrv_refresh_filename(child->bs);
7946 if (bs->implicit) {
7948 child = QLIST_FIRST(&bs->children);
7951 pstrcpy(bs->exact_filename, sizeof(bs->exact_filename),
7952 child->bs->exact_filename);
7953 pstrcpy(bs->filename, sizeof(bs->filename), child->bs->filename);
7955 qobject_unref(bs->full_open_options);
7956 bs->full_open_options = qobject_ref(child->bs->full_open_options);
7961 backing_overridden = bdrv_backing_overridden(bs);
7963 if (bs->open_flags & BDRV_O_NO_IO) {
7973 generate_json_filename = append_strong_runtime_options(opts, bs);
7979 drv->bdrv_gather_child_options(bs, opts, backing_overridden);
7981 QLIST_FOREACH(child, &bs->children, next) {
7982 if (child == bs->backing && !backing_overridden) {
7988 qobject_ref(child->bs->full_open_options));
7991 if (backing_overridden && !bs->backing) {
7997 qobject_unref(bs->full_open_options);
7998 bs->full_open_options = opts;
8000 primary_child_bs = bdrv_primary_bs(bs);
8005 bs->exact_filename[0] = '\0';
8007 drv->bdrv_refresh_filename(bs);
8018 bs->exact_filename[0] = '\0';
8036 strcpy(bs->exact_filename, primary_child_bs->exact_filename);
8040 if (bs->exact_filename[0]) {
8041 pstrcpy(bs->filename, sizeof(bs->filename), bs->exact_filename);
8043 GString *json = qobject_to_json(QOBJECT(bs->full_open_options));
8044 if (snprintf(bs->filename, sizeof(bs->filename), "json:%s",
8045 json->str) >= sizeof(bs->filename)) {
8047 strcpy(bs->filename + sizeof(bs->filename) - 4, "...");
8053 char *bdrv_dirname(BlockDriverState *bs, Error **errp)
8055 BlockDriver *drv = bs->drv;
8061 error_setg(errp, "Node '%s' is ejected", bs->node_name);
8066 return drv->bdrv_dirname(bs, errp);
8069 child_bs = bdrv_primary_bs(bs);
8074 bdrv_refresh_filename(bs);
8075 if (bs->exact_filename[0] != '\0') {
8076 return path_combine(bs->exact_filename, "");
8146 bdrv_get_device_or_node_name(child->bs));
8155 BlockDriver *drv = c->bs->drv;
8167 ret = drv->bdrv_make_empty(c->bs);
8170 c->bs->filename);
8178 * Return the child that @bs acts as an overlay for, and from which data may be
8181 BdrvChild *bdrv_cow_child(BlockDriverState *bs)
8185 if (!bs || !bs->drv) {
8189 if (bs->drv->is_filter) {
8193 if (!bs->backing) {
8197 assert(bs->backing->role & BDRV_CHILD_COW);
8198 return bs->backing;
8202 * If @bs acts as a filter for exactly one of its children, return
8205 BdrvChild *bdrv_filter_child(BlockDriverState *bs)
8210 if (!bs || !bs->drv) {
8214 if (!bs->drv->is_filter) {
8219 assert(!(bs->backing && bs->file));
8221 c = bs->backing ?: bs->file;
8236 BdrvChild *bdrv_filter_or_cow_child(BlockDriverState *bs)
8238 BdrvChild *cow_child = bdrv_cow_child(bs);
8239 BdrvChild *filter_child = bdrv_filter_child(bs);
8253 * child that has the same filename as @bs.)
8258 BdrvChild *bdrv_primary_child(BlockDriverState *bs)
8263 QLIST_FOREACH(c, &bs->children, next) {
8274 bdrv_do_skip_filters(BlockDriverState *bs, bool stop_on_explicit_filter)
8278 if (!bs) {
8282 while (!(stop_on_explicit_filter && !bs->implicit)) {
8283 c = bdrv_filter_child(bs);
8291 assert(!bs->drv || !bs->drv->is_filter);
8294 bs = c->bs;
8297 * Note that this treats nodes with bs->drv == NULL as not being
8298 * filters (bs->drv == NULL should be replaced by something else
8301 * always return a non-NULL value (given a non-NULL @bs).
8304 return bs;
8309 * does not have a filtered child down the chain starting from @bs
8310 * (including @bs itself).
8312 BlockDriverState *bdrv_skip_implicit_filters(BlockDriverState *bs)
8315 return bdrv_do_skip_filters(bs, true);
8320 * chain starting from @bs (including @bs itself).
8322 BlockDriverState *bdrv_skip_filters(BlockDriverState *bs)
8325 return bdrv_do_skip_filters(bs, false);
8332 BlockDriverState *bdrv_backing_chain_next(BlockDriverState *bs)
8335 return bdrv_skip_filters(bdrv_cow_bs(bdrv_skip_filters(bs)));
8346 static bool bdrv_bsc_range_overlaps_locked(BlockDriverState *bs,
8350 BdrvBlockStatusCache *bsc = qatomic_rcu_read(&bs->block_status_cache);
8368 bool bdrv_bsc_is_data(BlockDriverState *bs, int64_t offset, int64_t *pnum)
8372 return bdrv_bsc_range_overlaps_locked(bs, offset, 1, pnum);
8378 void bdrv_bsc_invalidate_range(BlockDriverState *bs,
8384 if (bdrv_bsc_range_overlaps_locked(bs, offset, bytes, NULL)) {
8385 qatomic_set(&bs->block_status_cache->valid, false);
8392 void bdrv_bsc_fill(BlockDriverState *bs, int64_t offset, int64_t bytes)
8404 QEMU_LOCK_GUARD(&bs->bsc_modify_lock);
8406 old_bsc = qatomic_rcu_read(&bs->block_status_cache);
8407 qatomic_rcu_set(&bs->block_status_cache, new_bsc);