Lines Matching refs:bs

92 static bool bdrv_recurse_has_child(BlockDriverState *bs,
107 static bool bdrv_backing_overridden(BlockDriverState *bs);
109 static bool bdrv_change_aio_context(BlockDriverState *bs, AioContext *ctx,
136 size_t bdrv_opt_mem_align(BlockDriverState *bs)
138 if (!bs || !bs->drv) {
144 return bs->bl.opt_mem_alignment;
147 size_t bdrv_min_mem_align(BlockDriverState *bs)
149 if (!bs || !bs->drv) {
155 return bs->bl.min_mem_alignment;
277 bool bdrv_is_read_only(BlockDriverState *bs)
280 return !(bs->open_flags & BDRV_O_RDWR);
284 bdrv_can_set_read_only(BlockDriverState *bs, bool read_only,
290 if (bs->copy_on_read && read_only) {
292 bdrv_get_device_or_node_name(bs));
297 if (!read_only && !(bs->open_flags & BDRV_O_ALLOW_RDWR) &&
301 bdrv_get_device_or_node_name(bs));
318 int bdrv_apply_auto_read_only(BlockDriverState *bs, const char *errmsg,
324 if (!(bs->open_flags & BDRV_O_RDWR)) {
327 if (!(bs->open_flags & BDRV_O_AUTO_RDONLY)) {
331 ret = bdrv_can_set_read_only(bs, true, false, NULL);
336 bs->open_flags &= ~BDRV_O_RDWR;
398 char *bdrv_get_full_backing_filename(BlockDriverState *bs, Error **errp)
401 return bdrv_make_absolute_filename(bs, bs->backing_file, errp);
413 BlockDriverState *bs;
418 bs = g_new0(BlockDriverState, 1);
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);
434 bdrv_drained_begin(bs);
437 QTAILQ_INSERT_TAIL(&all_bdrv_states, bs, bs_list);
439 return bs;
744 int coroutine_fn bdrv_co_delete_file(BlockDriverState *bs, Error **errp)
750 assert(bs != NULL);
753 if (!bs->drv) {
754 error_setg(errp, "Block node '%s' is not opened", bs->filename);
758 if (!bs->drv->bdrv_co_delete_file) {
760 bs->drv->format_name);
764 ret = bs->drv->bdrv_co_delete_file(bs, &local_err);
772 void coroutine_fn bdrv_co_delete_file_noerr(BlockDriverState *bs)
778 if (!bs) {
782 ret = bdrv_co_delete_file(bs, &local_err);
796 * Try to get @bs's logical and physical block size.
799 * @bs must not be empty.
801 int bdrv_probe_blocksizes(BlockDriverState *bs, BlockSizes *bsz)
803 BlockDriver *drv = bs->drv;
804 BlockDriverState *filtered = bdrv_filter_bs(bs);
808 return drv->bdrv_probe_blocksizes(bs, bsz);
817 * Try to get @bs's geometry (cyls, heads, sectors).
820 * @bs must not be empty.
822 int bdrv_probe_geometry(BlockDriverState *bs, HDGeometry *geo)
824 BlockDriver *drv = bs->drv;
831 return drv->bdrv_probe_geometry(bs, geo);
834 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);
1658 assert(!drv->bdrv_needs_filename || bs->filename[0]);
1660 ret = drv->bdrv_open(bs, options, open_flags, &local_err);
1668 } else if (bs->filename[0]) {
1669 error_setg_errno(errp, -ret, "Could not open '%s'", bs->filename);
1676 assert(!(bs->supported_read_flags & ~BDRV_REQ_MASK));
1677 assert(!(bs->supported_write_flags & ~BDRV_REQ_MASK));
1687 bs->supported_read_flags |= BDRV_REQ_REGISTERED_BUF;
1688 bs->supported_write_flags |= BDRV_REQ_REGISTERED_BUF;
1690 ret = bdrv_refresh_total_sectors(bs, bs->total_sectors);
1697 bdrv_refresh_limits(bs, NULL, &local_err);
1705 assert(bdrv_opt_mem_align(bs) != 0);
1706 assert(bdrv_min_mem_align(bs) != 0);
1707 assert(is_power_of_2(bs->bl.request_alignment));
1709 for (i = 0; i < bs->quiesce_counter; i++) {
1711 drv->bdrv_drain_begin(bs);
1717 bs->drv = NULL;
1720 if (bs->file != NULL) {
1721 bdrv_unref_child(bs, bs->file);
1722 assert(!bs->file);
1726 g_free(bs->opaque);
1727 bs->opaque = NULL;
1744 BlockDriverState *bs;
1749 bs = bdrv_new();
1750 bs->open_flags = flags;
1751 bs->options = options ?: qdict_new();
1752 bs->explicit_options = qdict_clone_shallow(bs->options);
1753 bs->opaque = NULL;
1755 update_options_from_flags(bs->options, flags);
1757 ret = bdrv_open_driver(bs, drv, node_name, bs->options, flags, errp);
1759 qobject_unref(bs->explicit_options);
1760 bs->explicit_options = NULL;
1761 qobject_unref(bs->options);
1762 bs->options = NULL;
1763 bdrv_unref(bs);
1767 return bs;
1854 static int bdrv_open_common(BlockDriverState *bs, BlockBackend *file,
1870 assert(bs->file == NULL);
1871 assert(options != NULL && bs->options != options);
1880 update_flags_from_options(&bs->open_flags, opts);
1886 bs->force_share = qemu_opt_get_bool(opts, BDRV_OPT_FORCE_SHARE, false);
1888 if (bs->force_share && (bs->open_flags & BDRV_O_RDWR)) {
1920 trace_bdrv_open_common(bs, filename ?: "", bs->open_flags,
1923 ro = bdrv_is_read_only(bs);
1928 ret = bdrv_apply_auto_read_only(bs, NULL, NULL);
1944 assert(qatomic_read(&bs->copy_on_read) == 0);
1946 if (bs->open_flags & BDRV_O_COPY_ON_READ) {
1948 bdrv_enable_copy_on_read(bs);
1958 if (bdrv_parse_discard_flags(discard, &bs->open_flags) != 0) {
1965 bs->detect_zeroes =
1966 bdrv_parse_detect_zeroes(opts, bs->open_flags, &local_err);
1974 pstrcpy(bs->filename, sizeof(bs->filename), filename);
1976 bs->filename[0] = '\0';
1978 pstrcpy(bs->exact_filename, sizeof(bs->exact_filename), bs->filename);
1981 open_flags = bdrv_open_flags(bs, bs->open_flags);
1985 ret = bdrv_open_driver(bs, drv, node_name, options, open_flags, errp);
2154 * Return the flags that @bs will have after the reopens in @q have
2155 * successfully completed. If @q is NULL (or @bs is not contained in @q),
2158 static int bdrv_reopen_get_flags(BlockReopenQueue *q, BlockDriverState *bs)
2164 if (entry->state.bs == bs) {
2170 return bs->open_flags;
2175 static bool bdrv_is_writable_after_reopen(BlockDriverState *bs,
2178 int flags = bdrv_reopen_get_flags(q, bs);
2185 * the same as !bdrv_is_read_only(bs), as inactivated images may not
2188 bool bdrv_is_writable(BlockDriverState *bs)
2191 return bdrv_is_writable_after_reopen(bs, NULL);
2211 assert(a->bs);
2212 assert(a->bs == b->bs);
2219 child_bs_name = bdrv_get_node_name(b->bs);
2235 bdrv_parent_perms_conflict(BlockDriverState *bs, Error **errp)
2245 QLIST_FOREACH(a, &bs->parents, next_parent) {
2246 QLIST_FOREACH(b, &bs->parents, next_parent) {
2261 bdrv_child_perm(BlockDriverState *bs, BlockDriverState *child_bs,
2267 assert(bs->drv && bs->drv->bdrv_child_perm);
2269 bs->drv->bdrv_child_perm(bs, c, role, reopen_queue,
2279 * Adds the whole subtree of @bs (including @bs itself) to the @list (except for
2290 bdrv_topological_dfs(GSList *list, GHashTable *found, BlockDriverState *bs)
2302 if (g_hash_table_contains(found, bs)) {
2305 g_hash_table_add(found, bs);
2307 QLIST_FOREACH(child, &bs->children, next) {
2308 list = bdrv_topological_dfs(list, found, child->bs);
2311 return g_slist_prepend(list, bs);
2355 BlockDriverState *bs = opaque;
2359 if (bs->drv->bdrv_set_perm) {
2360 bdrv_get_cumulative_perm(bs, &cumulative_perms,
2362 bs->drv->bdrv_set_perm(bs, cumulative_perms, cumulative_shared_perms);
2368 BlockDriverState *bs = opaque;
2371 if (bs->drv->bdrv_abort_perm_update) {
2372 bs->drv->bdrv_abort_perm_update(bs);
2386 bdrv_drv_set_perm(BlockDriverState *bs, uint64_t perm, uint64_t shared_perm,
2390 if (!bs->drv) {
2394 if (bs->drv->bdrv_check_perm) {
2395 int ret = bs->drv->bdrv_check_perm(bs, perm, shared_perm, errp);
2402 tran_add(tran, &bdrv_drv_set_perm_drv, bs);
2424 BlockDriverState *new_bs = s->child->bs;
2430 if (!s->child->bs) {
2460 * Both @child->bs and @new_bs (if non-NULL) must be drained. @new_bs must be
2479 .old_bs = child->bs,
2492 * Refresh permissions in @bs subtree. The function is intended to be called
2499 bdrv_node_refresh_perm(BlockDriverState *bs, BlockReopenQueue *q,
2502 BlockDriver *drv = bs->drv;
2508 bdrv_get_cumulative_perm(bs, &cumulative_perms, &cumulative_shared_perms);
2512 !bdrv_is_writable_after_reopen(bs, q))
2514 if (!bdrv_is_writable_after_reopen(bs, NULL)) {
2518 "read-write users", bdrv_get_node_name(bs));
2532 if ((bs->total_sectors * BDRV_SECTOR_SIZE) % bs->bl.request_alignment) {
2545 ret = bdrv_drv_set_perm(bs, cumulative_perms, cumulative_shared_perms, tran,
2553 assert(QLIST_EMPTY(&bs->children));
2558 QLIST_FOREACH(c, &bs->children, next) {
2561 bdrv_child_perm(bs, c->bs, c, c->role, q,
2582 BlockDriverState *bs;
2586 bs = list->data;
2588 if (bdrv_parent_perms_conflict(bs, errp)) {
2592 ret = bdrv_node_refresh_perm(bs, q, tran, errp);
2623 void bdrv_get_cumulative_perm(BlockDriverState *bs, uint64_t *perm,
2632 QLIST_FOREACH(c, &bs->parents, next_parent) {
2677 bdrv_refresh_perms(BlockDriverState *bs, Transaction *tran, Error **errp)
2681 g_autoptr(GSList) list = bdrv_topological_dfs(NULL, NULL, bs);
2708 ret = bdrv_refresh_perms(c->bs, tran, &local_err);
2731 int bdrv_child_refresh_perms(BlockDriverState *bs, BdrvChild *c, Error **errp)
2738 bdrv_get_cumulative_perm(bs, &parent_perms, &parent_shared);
2739 bdrv_child_perm(bs, c->bs, c, c->role, NULL,
2750 static void bdrv_filter_default_perms(BlockDriverState *bs, BdrvChild *c,
2761 static void bdrv_default_perms_for_cow(BlockDriverState *bs, BdrvChild *c,
2789 if (bs->open_flags & BDRV_O_INACTIVE) {
2797 static void bdrv_default_perms_for_storage(BlockDriverState *bs, BdrvChild *c,
2808 flags = bdrv_reopen_get_flags(reopen_queue, bs);
2814 bdrv_filter_default_perms(bs, c, role, reopen_queue,
2819 if (bdrv_is_writable_after_reopen(bs, reopen_queue)) {
2824 * bs->file always needs to be consistent because of the
2869 if (bs->open_flags & BDRV_O_INACTIVE) {
2877 void bdrv_default_perms(BlockDriverState *bs, BdrvChild *c,
2886 bdrv_filter_default_perms(bs, c, role, reopen_queue,
2890 bdrv_default_perms_for_cow(bs, c, role, reopen_queue,
2893 bdrv_default_perms_for_storage(bs, c, role, reopen_queue,
2926 BlockDriverState *old_bs = child->bs;
2933 * child->bs, we need to make sure that its new parent is drained, too. In
2966 child->bs = new_bs;
2989 * The child must be empty (i.e. `child->bs == NULL`) and it must be
2994 assert(!child->bs);
3013 BlockDriverState *bs = s->child->bs;
3020 if (bdrv_get_aio_context(bs) != s->old_child_ctx) {
3021 bdrv_try_change_aio_context(bs, s->old_child_ctx, NULL, &error_abort);
3042 bdrv_schedule_unref(bs);
3052 * Common part of attaching bdrv child to bs or to blk or to job
3082 .bs = NULL,
3278 BlockDriverState *child_bs = child->bs;
3304 BlockDriverState *bs;
3312 s->bs->inherits_from = s->old_inherits_from;
3321 static void bdrv_set_inherits_from(BlockDriverState *bs,
3329 .bs = bs,
3330 .old_inherits_from = bs->inherits_from,
3336 bs->inherits_from = new_inherits_from;
3350 if (child->bs->inherits_from == root) {
3353 * child->bs goes away.
3356 if (c != child && c->bs == child->bs) {
3361 bdrv_set_inherits_from(child->bs, NULL, tran);
3365 QLIST_FOREACH(c, &child->bs->children, next) {
3384 bdrv_parent_cb_change_media(BlockDriverState *bs, bool load)
3388 QLIST_FOREACH(c, &bs->parents, next_parent) {
3408 * Return the BdrvChildRole for @bs's backing child. bs->backing is
3412 static BdrvChildRole bdrv_backing_role(BlockDriverState *bs)
3414 if (bs->drv && bs->drv->is_filter) {
3422 * Sets the bs->backing or bs->file link of a BDS. A new reference is created;
3452 * qcow2 driver to never clear bs->drv and implement format corruption
3461 child->name, parent_bs->node_name, child->bs->node_name);
3491 assert(child->bs->quiesce_counter);
3510 * If inherits_from pointed recursively to bs then let's update it to
3511 * point directly to bs (else it will become NULL).
3524 * Both @bs and @backing_hd can move to a different AioContext in this
3530 int bdrv_set_backing_hd_drained(BlockDriverState *bs,
3538 assert(bs->quiesce_counter > 0);
3539 if (bs->backing) {
3540 assert(bs->backing->bs->quiesce_counter > 0);
3543 ret = bdrv_set_file_or_backing_noperm(bs, backing_hd, true, tran, errp);
3548 ret = bdrv_refresh_perms(bs, tran, errp);
3554 int bdrv_set_backing_hd(BlockDriverState *bs, BlockDriverState *backing_hd,
3562 drain_bs = bs->backing ? bs->backing->bs : bs;
3568 ret = bdrv_set_backing_hd_drained(bs, backing_hd, errp);
3586 int bdrv_open_backing_file(BlockDriverState *bs, QDict *parent_options,
3603 if (bs->backing != NULL) {
3613 bs->open_flags &= ~BDRV_O_NO_BACKING;
3629 } else if (bs->backing_file[0] == '\0' && qdict_size(options) == 0) {
3640 implicit_backing = !strcmp(bs->auto_backing_file, bs->backing_file);
3643 backing_filename = bdrv_get_full_backing_filename(bs, &local_err);
3652 if (!bs->drv || !bs->drv->supports_backing) {
3660 bs->backing_format[0] != '\0' && !qdict_haskey(options, "driver")) {
3661 qdict_put_str(options, "driver", bs->backing_format);
3664 backing_hd = bdrv_open_inherit(backing_filename, reference, options, 0, bs,
3665 &child_of_bds, bdrv_backing_role(bs), true,
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);
3703 BlockDriverState *bs = NULL;
3731 bs = bdrv_open_inherit(filename, reference, image_options, 0,
3734 if (!bs) {
3740 return bs;
3751 BlockDriverState *bs;
3756 bs = bdrv_open_child_bs(filename, options, bdref_key, parent, child_class,
3758 if (bs == NULL) {
3763 child = bdrv_attach_child(parent, bs, bdref_key, child_class, child_role,
3831 BlockDriverState *bs = NULL;
3862 bs = bdrv_open_inherit(NULL, reference, qdict, 0, NULL, NULL, 0, false,
3867 return bs;
3870 static BlockDriverState *bdrv_append_temp_snapshot(BlockDriverState *bs,
3888 total_size = bdrv_getlength(bs);
3923 ret = bdrv_append(bs_snapshot, bs, errp);
3957 BlockDriverState *bs;
3984 bs = bdrv_lookup_bs(reference, reference, errp);
3985 if (!bs) {
3989 bdrv_ref(bs);
3990 return bs;
3993 bs = bdrv_new();
4008 bs->explicit_options = qdict_clone_shallow(options);
4024 bs->inherits_from = parent;
4060 bs->open_flags = flags;
4061 bs->options = options;
4087 qdict_del(bs->explicit_options, "backing");
4088 qdict_del(bs->options, "backing");
4098 file_bs = bdrv_open_child_bs(filename, options, "file", bs,
4122 bs->probed = !drv;
4130 * but we first need to open bs->file for the probing to work, while
4131 * opening bs->file already requires the (mostly) final set of options
4136 * sure to update both bs->options (which has the full effective
4137 * options for bs) and options (which has file.* already removed).
4139 qdict_put_str(bs->options, "driver", drv->format_name);
4153 ret = bdrv_open_common(bs, file, options, &local_err);
4165 ret = bdrv_open_backing_file(bs, options, "backing", &local_err);
4172 * from bs->options and bs->explicit_options */
4173 QLIST_FOREACH(child, &bs->children, next) {
4176 qdict_extract_subqdict(bs->explicit_options, NULL, child_key_dot);
4177 qdict_extract_subqdict(bs->options, NULL, child_key_dot);
4178 qdict_del(bs->explicit_options, child->name);
4179 qdict_del(bs->options, child->name);
4198 bdrv_parent_cb_change_media(bs, true);
4203 /* For snapshot=on, create a temporary qcow2 overlay. bs points to the
4207 snapshot_bs = bdrv_append_temp_snapshot(bs, snapshot_flags,
4213 /* We are not going to return bs but the overlay on top of it
4214 * (snapshot_bs); thus, we have to drop the strong reference to bs
4215 * (which we obtained by calling bdrv_new()). bs will not be deleted,
4217 bdrv_unref(bs);
4218 bs = snapshot_bs;
4221 return bs;
4226 qobject_unref(bs->explicit_options);
4227 qobject_unref(bs->options);
4229 bs->options = NULL;
4230 bs->explicit_options = NULL;
4231 bdrv_unref(bs);
4236 bdrv_unref(bs);
4267 * Check that every option set in @bs->options is also set in
4271 * @bs->drv->mutable_opts are skipped.
4275 static int bdrv_reset_options_allowed(BlockDriverState *bs,
4286 for (e = qdict_first(bs->options); e; e = qdict_next(bs->options, e)) {
4289 !is_str_in_list(e->key, bs->drv->mutable_opts)) {
4300 * Returns true if @child can be reached recursively from @bs
4303 bdrv_recurse_has_child(BlockDriverState *bs, BlockDriverState *child)
4307 if (bs == child) {
4311 QLIST_FOREACH(c, &bs->children, next) {
4312 if (bdrv_recurse_has_child(c->bs, child)) {
4330 * bs is the BlockDriverState to add to the reopen queue.
4332 * options contains the changed options for the associated bs
4335 * flags contains the open flags for the associated bs
4340 * bs is drained here and undrained by bdrv_reopen_queue_free().
4342 * To be called with bs->aio_context locked.
4345 bdrv_reopen_queue_child(BlockReopenQueue *bs_queue, BlockDriverState *bs,
4351 assert(bs != NULL);
4366 bdrv_drained_begin(bs);
4379 if (bs == bs_entry->state.bs) {
4387 * 2. Retained from explicitly set options of bs
4389 * 4. Retained from effective options of bs
4396 bs->explicit_options);
4397 bdrv_join_options(bs, options, old_options);
4409 flags = bdrv_get_flags(bs);
4414 old_options = qdict_clone_shallow(bs->options);
4415 bdrv_join_options(bs, options, old_options);
4441 bs_entry->state.bs = bs;
4458 QLIST_FOREACH(child, &bs->children, next) {
4465 if (child->bs->inherits_from != bs) {
4476 if (g_strcmp0(childref, child->bs->node_name)) {
4493 bdrv_reopen_queue_child(bs_queue, child->bs, new_child_options,
4494 child->klass, child->role, bs->drv->is_format,
4501 /* To be called with bs->aio_context locked */
4503 BlockDriverState *bs,
4509 return bdrv_reopen_queue_child(bs_queue, bs, options, NULL, 0, false,
4519 bdrv_drained_end(bs_entry->state.bs);
4559 ret = bdrv_flush(bs_entry->state.bs);
4567 assert(bs_entry->state.bs->quiesce_counter > 0);
4578 refresh_list = g_slist_prepend(refresh_list, state->bs);
4619 BlockDriverState *bs = bs_entry->state.bs;
4621 if (bs->drv->bdrv_reopen_commit_post) {
4622 bs->drv->bdrv_reopen_commit_post(&bs_entry->state);
4646 int bdrv_reopen(BlockDriverState *bs, QDict *opts, bool keep_old_opts,
4653 queue = bdrv_reopen_queue(NULL, bs, opts, keep_old_opts);
4658 int bdrv_reopen_set_read_only(BlockDriverState *bs, bool read_only,
4667 return bdrv_reopen(bs, opts, true, errp);
4677 * reopen_state->bs then return 0.
4690 * @reopen_state->bs can move to a different AioContext in this function.
4697 BlockDriverState *bs = reopen_state->bs;
4729 has_child = bdrv_recurse_has_child(new_child_bs, bs);
4732 "cycle", str, child_name, bs->node_name);
4745 old_child_bs = is_backing ? child_bs(bs->backing) : child_bs(bs->file);
4759 child_name, bs->node_name);
4765 if (bs->drv->is_filter && !old_child_bs) {
4771 "%s child", bs->node_name, bs->drv->format_name, child_name);
4790 ret = bdrv_set_file_or_backing_noperm(bs, new_child_bs, is_backing,
4812 * bs is the BlockDriverState to reopen
4841 assert(reopen_state->bs->drv != NULL);
4843 drv = reopen_state->bs->drv;
4892 ret = bdrv_can_set_read_only(reopen_state->bs, read_only, true, &local_err);
4905 ret = bdrv_reset_options_allowed(reopen_state->bs,
4917 bdrv_refresh_filename(reopen_state->bs);
4920 reopen_state->bs->filename);
4930 bdrv_get_device_or_node_name(reopen_state->bs));
4945 (reopen_state->bs->backing || reopen_state->bs->backing_file[0])) {
4947 reopen_state->bs->node_name);
4984 QObject *old = qdict_get(reopen_state->bs->options, entry->key);
4990 QLIST_FOREACH(child, &reopen_state->bs->children, next) {
4997 if (!strcmp(child->bs->node_name,
5058 BlockDriverState *bs;
5062 bs = reopen_state->bs;
5063 drv = bs->drv;
5075 qobject_unref(bs->explicit_options);
5076 qobject_unref(bs->options);
5080 bs->explicit_options = reopen_state->explicit_options;
5081 bs->options = reopen_state->options;
5082 bs->open_flags = reopen_state->flags;
5083 bs->detect_zeroes = reopen_state->detect_zeroes;
5085 /* Remove child references from bs->options and bs->explicit_options.
5087 QLIST_FOREACH(child, &bs->children, next) {
5088 qdict_del(bs->explicit_options, child->name);
5089 qdict_del(bs->options, child->name);
5092 qdict_del(bs->explicit_options, "backing");
5093 qdict_del(bs->options, "backing");
5095 bdrv_refresh_limits(bs, NULL, NULL);
5096 bdrv_refresh_total_sectors(bs, bs->total_sectors);
5108 drv = reopen_state->bs->drv;
5118 static void bdrv_close(BlockDriverState *bs)
5124 assert(!bs->refcnt);
5126 bdrv_drained_begin(bs); /* complete I/O */
5127 bdrv_flush(bs);
5128 bdrv_drain(bs); /* in case flush left pending I/O */
5130 if (bs->drv) {
5131 if (bs->drv->bdrv_close) {
5133 bs->drv->bdrv_close(bs);
5135 bs->drv = NULL;
5139 QLIST_FOREACH_SAFE(child, &bs->children, next, next) {
5140 bdrv_unref_child(bs, child);
5143 assert(!bs->backing);
5144 assert(!bs->file);
5147 g_free(bs->opaque);
5148 bs->opaque = NULL;
5149 qatomic_set(&bs->copy_on_read, 0);
5150 bs->backing_file[0] = '\0';
5151 bs->backing_format[0] = '\0';
5152 bs->total_sectors = 0;
5153 bs->encrypted = false;
5154 bs->sg = false;
5155 qobject_unref(bs->options);
5156 qobject_unref(bs->explicit_options);
5157 bs->options = NULL;
5158 bs->explicit_options = NULL;
5159 qobject_unref(bs->full_open_options);
5160 bs->full_open_options = NULL;
5161 g_free(bs->block_status_cache);
5162 bs->block_status_cache = NULL;
5164 bdrv_release_named_dirty_bitmaps(bs);
5165 assert(QLIST_EMPTY(&bs->dirty_bitmaps));
5167 QLIST_FOREACH_SAFE(ban, &bs->aio_notifiers, list, ban_next) {
5170 QLIST_INIT(&bs->aio_notifiers);
5171 bdrv_drained_end(bs);
5178 if (bs->quiesce_counter) {
5179 bdrv_drain_all_end_quiesce(bs);
5209 * c->bs by @to would mean to create a loop.
5234 * In general, when replacing a node A (c->bs) by a node B (@to),
5262 if (g_hash_table_contains(found, c2->bs)) {
5266 g_queue_push_tail(queue, c2->bs);
5267 g_hash_table_add(found, c2->bs);
5290 * @child->bs (if non-NULL) must be drained.
5301 if (child->bs) {
5330 assert(c->bs == from);
5426 int bdrv_drop_filter(BlockDriverState *bs, Error **errp)
5434 child_bs = bdrv_filter_or_cow_bs(bs);
5439 ret = bdrv_replace_node_common(bs, child_bs, true, true, errp);
5447 * Add new bs contents at the top of an image chain while the chain is
5509 BlockDriverState *old_bs = child->bs;
5535 static void bdrv_delete(BlockDriverState *bs)
5537 assert(bdrv_op_blocker_is_empty(bs));
5538 assert(!bs->refcnt);
5542 if (bs->node_name[0] != '\0') {
5543 QTAILQ_REMOVE(&graph_bdrv_states, bs, node_list);
5545 QTAILQ_REMOVE(&all_bdrv_states, bs, bs_list);
5547 bdrv_close(bs);
5549 qemu_mutex_destroy(&bs->reqs_lock);
5551 g_free(bs);
5556 * Replace @bs by newly created block node.
5563 * The caller must make sure that @bs stays in the same AioContext, i.e.
5566 BlockDriverState *bdrv_insert_node(BlockDriverState *bs, QDict *options,
5571 AioContext *ctx = bdrv_get_aio_context(bs);
5594 assert(bdrv_get_aio_context(bs) == ctx);
5603 * Make sure that @bs doesn't go away until we have successfully attached
5606 bdrv_ref(bs);
5607 bdrv_drained_begin(bs);
5610 ret = bdrv_replace_node(bs, new_node_bs, errp);
5613 bdrv_drained_end(bs);
5614 bdrv_unref(bs);
5636 int coroutine_fn bdrv_co_check(BlockDriverState *bs,
5641 if (bs->drv == NULL) {
5644 if (bs->drv->bdrv_co_check == NULL) {
5649 return bs->drv->bdrv_co_check(bs, res, fix);
5661 bdrv_co_change_backing_file(BlockDriverState *bs, const char *backing_file,
5664 BlockDriver *drv = bs->drv;
5683 ret = drv->bdrv_co_change_backing_file(bs, backing_file, backing_fmt);
5689 pstrcpy(bs->backing_file, sizeof(bs->backing_file), backing_file ?: "");
5690 pstrcpy(bs->backing_format, sizeof(bs->backing_format), backing_fmt ?: "");
5691 pstrcpy(bs->auto_backing_file, sizeof(bs->auto_backing_file),
5698 * Finds the first non-filter node above bs in the chain between
5699 * active and bs. The returned node is either an immediate parent of
5700 * bs, or there are only filter nodes between the two.
5702 * Returns NULL if bs is not found in active's image chain,
5703 * or if active == bs.
5705 * Returns the bottommost base image if bs == NULL.
5708 BlockDriverState *bs)
5713 bs = bdrv_skip_filters(bs);
5718 if (bs == next) {
5728 BlockDriverState *bdrv_find_base(BlockDriverState *bs)
5732 return bdrv_find_overlay(bs, NULL);
5737 * between @bs and @base is frozen. @errp is set if that's the case.
5738 * @base must be reachable from @bs, or NULL.
5741 bdrv_is_backing_chain_frozen(BlockDriverState *bs, BlockDriverState *base,
5749 for (i = bs; i != base; i = child_bs(child)) {
5754 child->name, i->node_name, child->bs->node_name);
5763 * Freeze all COW (backing) and filter links between @bs and @base.
5766 * @base must be reachable from @bs, or NULL.
5769 int bdrv_freeze_backing_chain(BlockDriverState *bs, BlockDriverState *base,
5777 if (bdrv_is_backing_chain_frozen(bs, base, errp)) {
5781 for (i = bs; i != base; i = child_bs(child)) {
5783 if (child && child->bs->never_freeze) {
5785 child->name, child->bs->node_name);
5790 for (i = bs; i != base; i = child_bs(child)) {
5801 * Unfreeze all COW (backing) and filter links between @bs and @base.
5804 * @base must be reachable from @bs, or NULL.
5806 void bdrv_unfreeze_backing_chain(BlockDriverState *bs, BlockDriverState *base)
5813 for (i = bs; i != base; i = child_bs(child)) {
5827 * information in 'bs' can be properly updated.
5958 bdrv_sum_allocated_file_size(BlockDriverState *bs)
5963 QLIST_FOREACH(child, &bs->children, next) {
5967 child_size = bdrv_co_get_allocated_file_size(child->bs);
5982 int64_t coroutine_fn bdrv_co_get_allocated_file_size(BlockDriverState *bs)
5984 BlockDriver *drv = bs->drv;
5992 return drv->bdrv_co_get_allocated_file_size(bs);
6004 return bdrv_co_get_allocated_file_size(bdrv_filter_bs(bs));
6007 return bdrv_sum_allocated_file_size(bs);
6050 int64_t coroutine_fn bdrv_co_nb_sectors(BlockDriverState *bs)
6052 BlockDriver *drv = bs->drv;
6059 if (bs->bl.has_variable_length) {
6060 int ret = bdrv_co_refresh_total_sectors(bs, bs->total_sectors);
6065 return bs->total_sectors;
6072 int64_t coroutine_mixed_fn bdrv_nb_sectors(BlockDriverState *bs)
6074 BlockDriver *drv = bs->drv;
6080 if (bs->bl.has_variable_length) {
6081 int ret = bdrv_refresh_total_sectors(bs, bs->total_sectors);
6087 return bs->total_sectors;
6094 int64_t coroutine_fn bdrv_co_getlength(BlockDriverState *bs)
6100 ret = bdrv_co_nb_sectors(bs);
6110 bool bdrv_is_sg(BlockDriverState *bs)
6113 return bs->sg;
6119 bool bdrv_supports_compressed_writes(BlockDriverState *bs)
6124 if (!bs->drv || !block_driver_can_compress(bs->drv)) {
6128 filtered = bdrv_filter_bs(bs);
6140 const char *bdrv_get_format_name(BlockDriverState *bs)
6143 return bs->drv ? bs->drv->format_name : NULL;
6213 /* This function is to find a node in the bs graph */
6216 BlockDriverState *bs;
6221 QTAILQ_FOREACH(bs, &graph_bdrv_states, node_list) {
6222 if (!strcmp(node_name, bs->node_name)) {
6223 return bs;
6234 BlockDriverState *bs;
6240 QTAILQ_FOREACH(bs, &graph_bdrv_states, node_list) {
6241 BlockDeviceInfo *info = bdrv_block_device_info(NULL, bs, flat, errp);
6319 edge->child = xdbg_graph_node_num(gr, child->bs);
6341 BlockDriverState *bs;
6375 QTAILQ_FOREACH(bs, &graph_bdrv_states, node_list) {
6376 xdbg_graph_add_node(gr, bs, X_DBG_BLOCK_GRAPH_NODE_TYPE_BLOCK_DRIVER,
6377 bs->node_name);
6378 QLIST_FOREACH(child, &bs->children, next) {
6379 xdbg_graph_add_edge(gr, bs, child);
6391 BlockDriverState *bs;
6399 bs = blk_bs(blk);
6400 if (!bs) {
6404 return bs;
6409 bs = bdrv_find_node(node_name);
6411 if (bs) {
6412 return bs;
6436 BlockDriverState *bdrv_next_node(BlockDriverState *bs)
6439 if (!bs) {
6442 return QTAILQ_NEXT(bs, node_list);
6445 BlockDriverState *bdrv_next_all_states(BlockDriverState *bs)
6448 if (!bs) {
6451 return QTAILQ_NEXT(bs, bs_list);
6454 const char *bdrv_get_node_name(const BlockDriverState *bs)
6457 return bs->node_name;
6460 const char *bdrv_get_parent_name(const BlockDriverState *bs)
6467 QLIST_FOREACH(c, &bs->parents, next_parent) {
6479 /* TODO check what callers really want: bs->node_name or blk_name() */
6480 const char *bdrv_get_device_name(const BlockDriverState *bs)
6483 return bdrv_get_parent_name(bs) ?: "";
6490 const char *bdrv_get_device_or_node_name(const BlockDriverState *bs)
6493 return bdrv_get_parent_name(bs) ?: bs->node_name;
6496 int bdrv_get_flags(BlockDriverState *bs)
6499 return bs->open_flags;
6502 int bdrv_has_zero_init_1(BlockDriverState *bs)
6508 int coroutine_mixed_fn bdrv_has_zero_init(BlockDriverState *bs)
6513 if (!bs->drv) {
6519 if (bdrv_cow_child(bs)) {
6522 if (bs->drv->bdrv_has_zero_init) {
6523 return bs->drv->bdrv_has_zero_init(bs);
6526 filtered = bdrv_filter_bs(bs);
6535 bool bdrv_can_write_zeroes_with_unmap(BlockDriverState *bs)
6538 if (!(bs->open_flags & BDRV_O_UNMAP)) {
6542 return bs->supported_zero_flags & BDRV_REQ_MAY_UNMAP;
6545 void bdrv_get_backing_filename(BlockDriverState *bs,
6549 pstrcpy(filename, filename_size, bs->backing_file);
6552 int coroutine_fn bdrv_co_get_info(BlockDriverState *bs, BlockDriverInfo *bdi)
6555 BlockDriver *drv = bs->drv;
6559 /* if bs->drv == NULL, bs is closed, so there's nothing to do here */
6564 BlockDriverState *filtered = bdrv_filter_bs(bs);
6571 ret = drv->bdrv_co_get_info(bs, bdi);
6590 ImageInfoSpecific *bdrv_get_specific_info(BlockDriverState *bs,
6593 BlockDriver *drv = bs->drv;
6596 return drv->bdrv_get_specific_info(bs, errp);
6601 BlockStatsSpecific *bdrv_get_specific_stats(BlockDriverState *bs)
6603 BlockDriver *drv = bs->drv;
6608 return drv->bdrv_get_specific_stats(bs);
6611 void coroutine_fn bdrv_co_debug_event(BlockDriverState *bs, BlkdebugEvent event)
6616 if (!bs || !bs->drv || !bs->drv->bdrv_co_debug_event) {
6620 bs->drv->bdrv_co_debug_event(bs, event);
6624 bdrv_find_debug_node(BlockDriverState *bs)
6627 while (bs && bs->drv && !bs->drv->bdrv_debug_breakpoint) {
6628 bs = bdrv_primary_bs(bs);
6631 if (bs && bs->drv && bs->drv->bdrv_debug_breakpoint) {
6632 assert(bs->drv->bdrv_debug_remove_breakpoint);
6633 return bs;
6639 int bdrv_debug_breakpoint(BlockDriverState *bs, const char *event,
6645 bs = bdrv_find_debug_node(bs);
6646 if (bs) {
6647 return bs->drv->bdrv_debug_breakpoint(bs, event, tag);
6653 int bdrv_debug_remove_breakpoint(BlockDriverState *bs, const char *tag)
6658 bs = bdrv_find_debug_node(bs);
6659 if (bs) {
6660 return bs->drv->bdrv_debug_remove_breakpoint(bs, tag);
6666 int bdrv_debug_resume(BlockDriverState *bs, const char *tag)
6671 while (bs && (!bs->drv || !bs->drv->bdrv_debug_resume)) {
6672 bs = bdrv_primary_bs(bs);
6675 if (bs && bs->drv && bs->drv->bdrv_debug_resume) {
6676 return bs->drv->bdrv_debug_resume(bs, tag);
6682 bool bdrv_debug_is_suspended(BlockDriverState *bs, const char *tag)
6687 while (bs && bs->drv && !bs->drv->bdrv_debug_is_suspended) {
6688 bs = bdrv_primary_bs(bs);
6691 if (bs && bs->drv && bs->drv->bdrv_debug_is_suspended) {
6692 return bs->drv->bdrv_debug_is_suspended(bs, tag);
6699 * relative, it must be relative to the chain. So, passing in bs->filename
6702 BlockDriverState *bdrv_find_backing_image(BlockDriverState *bs,
6717 if (!bs || !bs->drv || !backing_file) {
6732 for (curr_bs = bdrv_skip_filters(bs);
6827 int bdrv_activate(BlockDriverState *bs, Error **errp)
6837 if (!bs->drv) {
6841 QLIST_FOREACH(child, &bs->children, next) {
6842 bdrv_activate(child->bs, &local_err);
6862 if (bs->open_flags & BDRV_O_INACTIVE) {
6863 bs->open_flags &= ~BDRV_O_INACTIVE;
6864 ret = bdrv_refresh_perms(bs, NULL, errp);
6866 bs->open_flags |= BDRV_O_INACTIVE;
6870 ret = bdrv_invalidate_cache(bs, errp);
6872 bs->open_flags |= BDRV_O_INACTIVE;
6876 FOR_EACH_DIRTY_BITMAP(bs, bm) {
6880 ret = bdrv_refresh_total_sectors(bs, bs->total_sectors);
6882 bs->open_flags |= BDRV_O_INACTIVE;
6888 QLIST_FOREACH(parent, &bs->parents, next_parent) {
6892 bs->open_flags |= BDRV_O_INACTIVE;
6902 int coroutine_fn bdrv_co_invalidate_cache(BlockDriverState *bs, Error **errp)
6907 assert(!(bs->open_flags & BDRV_O_INACTIVE));
6910 if (bs->drv->bdrv_co_invalidate_cache) {
6911 bs->drv->bdrv_co_invalidate_cache(bs, &local_err);
6923 BlockDriverState *bs;
6929 for (bs = bdrv_first(&it); bs; bs = bdrv_next(&it)) {
6932 ret = bdrv_activate(bs, errp);
6941 bdrv_has_bds_parent(BlockDriverState *bs, bool only_active)
6946 QLIST_FOREACH(parent, &bs->parents, next_parent) {
6958 static int GRAPH_RDLOCK bdrv_inactivate_recurse(BlockDriverState *bs)
6966 if (!bs->drv) {
6972 if (bdrv_has_bds_parent(bs, true)) {
6976 assert(!(bs->open_flags & BDRV_O_INACTIVE));
6979 if (bs->drv->bdrv_inactivate) {
6980 ret = bs->drv->bdrv_inactivate(bs);
6986 QLIST_FOREACH(parent, &bs->parents, next_parent) {
6995 bdrv_get_cumulative_perm(bs, &cumulative_perms,
7002 bs->open_flags |= BDRV_O_INACTIVE;
7009 bdrv_refresh_perms(bs, NULL, NULL);
7012 QLIST_FOREACH(child, &bs->children, next) {
7013 ret = bdrv_inactivate_recurse(child->bs);
7024 BlockDriverState *bs = NULL;
7031 for (bs = bdrv_first(&it); bs; bs = bdrv_next(&it)) {
7035 if (bdrv_has_bds_parent(bs, false)) {
7038 ret = bdrv_inactivate_recurse(bs);
7054 bool coroutine_fn bdrv_co_is_inserted(BlockDriverState *bs)
7056 BlockDriver *drv = bs->drv;
7065 return drv->bdrv_co_is_inserted(bs);
7067 QLIST_FOREACH(child, &bs->children, next) {
7068 if (!bdrv_co_is_inserted(child->bs)) {
7078 void coroutine_fn bdrv_co_eject(BlockDriverState *bs, bool eject_flag)
7080 BlockDriver *drv = bs->drv;
7085 drv->bdrv_co_eject(bs, eject_flag);
7093 void coroutine_fn bdrv_co_lock_medium(BlockDriverState *bs, bool locked)
7095 BlockDriver *drv = bs->drv;
7098 trace_bdrv_lock_medium(bs, locked);
7101 drv->bdrv_co_lock_medium(bs, locked);
7105 /* Get a reference to bs */
7106 void bdrv_ref(BlockDriverState *bs)
7109 bs->refcnt++;
7112 /* Release a previously grabbed reference to bs.
7115 void bdrv_unref(BlockDriverState *bs)
7118 if (!bs) {
7121 assert(bs->refcnt > 0);
7122 if (--bs->refcnt == 0) {
7123 bdrv_delete(bs);
7129 BlockDriverState *bs = opaque;
7131 bdrv_unref(bs);
7140 * possibly closing @bs until the graph lock is released.
7142 void bdrv_schedule_unref(BlockDriverState *bs)
7144 if (!bs) {
7147 aio_bh_schedule_oneshot(qemu_get_aio_context(), bdrv_schedule_unref_bh, bs);
7155 bool bdrv_op_is_blocked(BlockDriverState *bs, BlockOpType op, Error **errp)
7161 if (!QLIST_EMPTY(&bs->op_blockers[op])) {
7162 blocker = QLIST_FIRST(&bs->op_blockers[op]);
7165 bdrv_get_device_or_node_name(bs));
7171 void bdrv_op_block(BlockDriverState *bs, BlockOpType op, Error *reason)
7179 QLIST_INSERT_HEAD(&bs->op_blockers[op], blocker, list);
7182 void bdrv_op_unblock(BlockDriverState *bs, BlockOpType op, Error *reason)
7187 QLIST_FOREACH_SAFE(blocker, &bs->op_blockers[op], list, next) {
7195 void bdrv_op_block_all(BlockDriverState *bs, Error *reason)
7200 bdrv_op_block(bs, i, reason);
7204 void bdrv_op_unblock_all(BlockDriverState *bs, Error *reason)
7209 bdrv_op_unblock(bs, i, reason);
7213 bool bdrv_op_blocker_is_empty(BlockDriverState *bs)
7218 if (!QLIST_EMPTY(&bs->op_blockers[i])) {
7324 BlockDriverState *bs;
7351 bs = bdrv_open(full_backing, NULL, backing_options, back_flags,
7354 if (!bs) {
7362 bs->drv->format_name);
7367 size = bdrv_getlength(bs);
7371 bdrv_unref(bs);
7376 bdrv_unref(bs);
7422 AioContext *bdrv_get_aio_context(BlockDriverState *bs)
7425 return bs ? bs->aio_context : qemu_get_aio_context();
7428 AioContext *coroutine_fn bdrv_co_enter(BlockDriverState *bs)
7436 * Increase bs->in_flight to ensure that this operation is completed before
7439 bdrv_inc_in_flight(bs);
7441 new_ctx = bdrv_get_aio_context(bs);
7446 void coroutine_fn bdrv_co_leave(BlockDriverState *bs, AioContext *old_ctx)
7450 bdrv_dec_in_flight(bs);
7460 static void bdrv_detach_aio_context(BlockDriverState *bs)
7464 assert(!bs->walking_aio_notifiers);
7466 bs->walking_aio_notifiers = true;
7467 QLIST_FOREACH_SAFE(baf, &bs->aio_notifiers, list, baf_tmp) {
7477 bs->walking_aio_notifiers = false;
7479 if (bs->drv && bs->drv->bdrv_detach_aio_context) {
7480 bs->drv->bdrv_detach_aio_context(bs);
7483 bs->aio_context = NULL;
7486 static void bdrv_attach_aio_context(BlockDriverState *bs,
7492 bs->aio_context = new_context;
7494 if (bs->drv && bs->drv->bdrv_attach_aio_context) {
7495 bs->drv->bdrv_attach_aio_context(bs, new_context);
7498 assert(!bs->walking_aio_notifiers);
7499 bs->walking_aio_notifiers = true;
7500 QLIST_FOREACH_SAFE(ban, &bs->aio_notifiers, list, ban_tmp) {
7507 bs->walking_aio_notifiers = false;
7512 BlockDriverState *bs;
7552 return bdrv_change_aio_context(c->bs, ctx, visited, tran, errp);
7558 BlockDriverState *bs = (BlockDriverState *) state->bs;
7561 bdrv_drained_end(bs);
7569 BlockDriverState *bs = (BlockDriverState *) state->bs;
7572 bdrv_detach_aio_context(bs);
7573 bdrv_attach_aio_context(bs, new_context);
7590 static bool bdrv_change_aio_context(BlockDriverState *bs, AioContext *ctx,
7599 if (bdrv_get_aio_context(bs) == ctx) {
7604 QLIST_FOREACH(c, &bs->parents, next_parent) {
7611 QLIST_FOREACH(c, &bs->children, next) {
7622 .bs = bs,
7626 bdrv_drained_begin(bs);
7634 * Change bs's and recursively all of its parents' and children's AioContext
7640 int bdrv_try_change_aio_context(BlockDriverState *bs, AioContext *ctx,
7659 ret = bdrv_change_aio_context(bs, ctx, visited, tran, errp);
7679 void bdrv_add_aio_context_notifier(BlockDriverState *bs,
7691 QLIST_INSERT_HEAD(&bs->aio_notifiers, ban, list);
7694 void bdrv_remove_aio_context_notifier(BlockDriverState *bs,
7703 QLIST_FOREACH_SAFE(ban, &bs->aio_notifiers, list, ban_next) {
7709 if (bs->walking_aio_notifiers) {
7721 int bdrv_amend_options(BlockDriverState *bs, QemuOpts *opts,
7727 if (!bs->drv) {
7731 if (!bs->drv->bdrv_amend_options) {
7733 bs->drv->format_name);
7736 return bs->drv->bdrv_amend_options(bs, opts, status_cb,
7742 * replaced by a node that always shows the same data as @bs. This is
7744 * @to_replace by the target mirrored from @bs.
7745 * To be replaceable, @bs and @to_replace may either be guaranteed to
7752 bool bdrv_recurse_can_replace(BlockDriverState *bs,
7759 if (!bs || !bs->drv) {
7763 if (bs == to_replace) {
7768 if (bs->drv->bdrv_recurse_can_replace) {
7769 return bs->drv->bdrv_recurse_can_replace(bs, to_replace);
7773 filtered = bdrv_filter_bs(bs);
7835 static const char *const *strong_options(BlockDriverState *bs,
7847 if (curopt == &global_options[ARRAY_SIZE(global_options) - 1] && bs->drv) {
7848 curopt = bs->drv->strong_runtime_opts;
7855 * Copies all strong runtime options from bs->options to the given
7859 * Returns true iff any strong option was present in bs->options (and
7865 static bool append_strong_runtime_options(QDict *d, BlockDriverState *bs)
7870 if (!bs->drv) {
7874 while ((option_name = strong_options(bs, option_name))) {
7879 QObject *entry = qdict_get(bs->options, *option_name);
7888 for (entry = qdict_first(bs->options); entry;
7889 entry = qdict_next(bs->options, entry))
7911 qdict_put_str(d, "driver", bs->drv->format_name);
7918 * even if opening the backing file specified by bs's image header
7919 * would result in exactly bs->backing. */
7920 static bool GRAPH_RDLOCK bdrv_backing_overridden(BlockDriverState *bs)
7923 if (bs->backing) {
7924 return strcmp(bs->auto_backing_file,
7925 bs->backing->bs->filename);
7929 return bs->auto_backing_file[0] != '\0';
7945 void bdrv_refresh_filename(BlockDriverState *bs)
7947 BlockDriver *drv = bs->drv;
7963 QLIST_FOREACH(child, &bs->children, next) {
7964 bdrv_refresh_filename(child->bs);
7967 if (bs->implicit) {
7969 child = QLIST_FIRST(&bs->children);
7972 pstrcpy(bs->exact_filename, sizeof(bs->exact_filename),
7973 child->bs->exact_filename);
7974 pstrcpy(bs->filename, sizeof(bs->filename), child->bs->filename);
7976 qobject_unref(bs->full_open_options);
7977 bs->full_open_options = qobject_ref(child->bs->full_open_options);
7982 backing_overridden = bdrv_backing_overridden(bs);
7984 if (bs->open_flags & BDRV_O_NO_IO) {
7994 generate_json_filename = append_strong_runtime_options(opts, bs);
8000 drv->bdrv_gather_child_options(bs, opts, backing_overridden);
8002 QLIST_FOREACH(child, &bs->children, next) {
8003 if (child == bs->backing && !backing_overridden) {
8009 qobject_ref(child->bs->full_open_options));
8012 if (backing_overridden && !bs->backing) {
8018 qobject_unref(bs->full_open_options);
8019 bs->full_open_options = opts;
8021 primary_child_bs = bdrv_primary_bs(bs);
8026 bs->exact_filename[0] = '\0';
8028 drv->bdrv_refresh_filename(bs);
8039 bs->exact_filename[0] = '\0';
8057 strcpy(bs->exact_filename, primary_child_bs->exact_filename);
8061 if (bs->exact_filename[0]) {
8062 pstrcpy(bs->filename, sizeof(bs->filename), bs->exact_filename);
8064 GString *json = qobject_to_json(QOBJECT(bs->full_open_options));
8065 if (snprintf(bs->filename, sizeof(bs->filename), "json:%s",
8066 json->str) >= sizeof(bs->filename)) {
8068 strcpy(bs->filename + sizeof(bs->filename) - 4, "...");
8074 char *bdrv_dirname(BlockDriverState *bs, Error **errp)
8076 BlockDriver *drv = bs->drv;
8082 error_setg(errp, "Node '%s' is ejected", bs->node_name);
8087 return drv->bdrv_dirname(bs, errp);
8090 child_bs = bdrv_primary_bs(bs);
8095 bdrv_refresh_filename(bs);
8096 if (bs->exact_filename[0] != '\0') {
8097 return path_combine(bs->exact_filename, "");
8167 bdrv_get_device_or_node_name(child->bs));
8176 BlockDriver *drv = c->bs->drv;
8188 ret = drv->bdrv_make_empty(c->bs);
8191 c->bs->filename);
8199 * Return the child that @bs acts as an overlay for, and from which data may be
8202 BdrvChild *bdrv_cow_child(BlockDriverState *bs)
8206 if (!bs || !bs->drv) {
8210 if (bs->drv->is_filter) {
8214 if (!bs->backing) {
8218 assert(bs->backing->role & BDRV_CHILD_COW);
8219 return bs->backing;
8223 * If @bs acts as a filter for exactly one of its children, return
8226 BdrvChild *bdrv_filter_child(BlockDriverState *bs)
8231 if (!bs || !bs->drv) {
8235 if (!bs->drv->is_filter) {
8240 assert(!(bs->backing && bs->file));
8242 c = bs->backing ?: bs->file;
8257 BdrvChild *bdrv_filter_or_cow_child(BlockDriverState *bs)
8259 BdrvChild *cow_child = bdrv_cow_child(bs);
8260 BdrvChild *filter_child = bdrv_filter_child(bs);
8274 * child that has the same filename as @bs.)
8279 BdrvChild *bdrv_primary_child(BlockDriverState *bs)
8284 QLIST_FOREACH(c, &bs->children, next) {
8295 bdrv_do_skip_filters(BlockDriverState *bs, bool stop_on_explicit_filter)
8299 if (!bs) {
8303 while (!(stop_on_explicit_filter && !bs->implicit)) {
8304 c = bdrv_filter_child(bs);
8312 assert(!bs->drv || !bs->drv->is_filter);
8315 bs = c->bs;
8318 * Note that this treats nodes with bs->drv == NULL as not being
8319 * filters (bs->drv == NULL should be replaced by something else
8322 * always return a non-NULL value (given a non-NULL @bs).
8325 return bs;
8330 * does not have a filtered child down the chain starting from @bs
8331 * (including @bs itself).
8333 BlockDriverState *bdrv_skip_implicit_filters(BlockDriverState *bs)
8336 return bdrv_do_skip_filters(bs, true);
8341 * chain starting from @bs (including @bs itself).
8343 BlockDriverState *bdrv_skip_filters(BlockDriverState *bs)
8346 return bdrv_do_skip_filters(bs, false);
8353 BlockDriverState *bdrv_backing_chain_next(BlockDriverState *bs)
8356 return bdrv_skip_filters(bdrv_cow_bs(bdrv_skip_filters(bs)));
8367 static bool bdrv_bsc_range_overlaps_locked(BlockDriverState *bs,
8371 BdrvBlockStatusCache *bsc = qatomic_rcu_read(&bs->block_status_cache);
8389 bool bdrv_bsc_is_data(BlockDriverState *bs, int64_t offset, int64_t *pnum)
8393 return bdrv_bsc_range_overlaps_locked(bs, offset, 1, pnum);
8399 void bdrv_bsc_invalidate_range(BlockDriverState *bs,
8405 if (bdrv_bsc_range_overlaps_locked(bs, offset, bytes, NULL)) {
8406 qatomic_set(&bs->block_status_cache->valid, false);
8413 void bdrv_bsc_fill(BlockDriverState *bs, int64_t offset, int64_t bytes)
8425 QEMU_LOCK_GUARD(&bs->bsc_modify_lock);
8427 old_bsc = qatomic_rcu_read(&bs->block_status_cache);
8428 qatomic_rcu_set(&bs->block_status_cache, new_bsc);