block.c (ccd6a37947574707613e826e2bf04d55f1d5f238) block.c (004915a96a7a40e942ac85e6d22518cbcd283506)
1/*
2 * QEMU System Emulator block driver
3 *
4 * Copyright (c) 2003 Fabrice Bellard
5 * Copyright (c) 2020 Virtuozzo International GmbH.
6 *
7 * Permission is hereby granted, free of charge, to any person obtaining a copy
8 * of this software and associated documentation files (the "Software"), to deal

--- 3546 unchanged lines hidden (view full) ---

3555out:
3556 tran_finalize(tran, ret);
3557 return ret;
3558}
3559
3560int bdrv_set_backing_hd(BlockDriverState *bs, BlockDriverState *backing_hd,
3561 Error **errp)
3562{
1/*
2 * QEMU System Emulator block driver
3 *
4 * Copyright (c) 2003 Fabrice Bellard
5 * Copyright (c) 2020 Virtuozzo International GmbH.
6 *
7 * Permission is hereby granted, free of charge, to any person obtaining a copy
8 * of this software and associated documentation files (the "Software"), to deal

--- 3546 unchanged lines hidden (view full) ---

3555out:
3556 tran_finalize(tran, ret);
3557 return ret;
3558}
3559
3560int bdrv_set_backing_hd(BlockDriverState *bs, BlockDriverState *backing_hd,
3561 Error **errp)
3562{
3563 BlockDriverState *drain_bs = bs->backing ? bs->backing->bs : bs;
3563 BlockDriverState *drain_bs;
3564 int ret;
3565 GLOBAL_STATE_CODE();
3566
3564 int ret;
3565 GLOBAL_STATE_CODE();
3566
3567 bdrv_graph_rdlock_main_loop();
3568 drain_bs = bs->backing ? bs->backing->bs : bs;
3569 bdrv_graph_rdunlock_main_loop();
3570
3567 bdrv_ref(drain_bs);
3568 bdrv_drained_begin(drain_bs);
3569 bdrv_graph_wrlock(backing_hd);
3570 ret = bdrv_set_backing_hd_drained(bs, backing_hd, errp);
3571 bdrv_graph_wrunlock();
3572 bdrv_drained_end(drain_bs);
3573 bdrv_unref(drain_bs);
3574

--- 22 unchanged lines hidden (view full) ---

3597 bool implicit_backing = false;
3598 BlockDriverState *backing_hd;
3599 AioContext *backing_hd_ctx;
3600 QDict *options;
3601 QDict *tmp_parent_options = NULL;
3602 Error *local_err = NULL;
3603
3604 GLOBAL_STATE_CODE();
3571 bdrv_ref(drain_bs);
3572 bdrv_drained_begin(drain_bs);
3573 bdrv_graph_wrlock(backing_hd);
3574 ret = bdrv_set_backing_hd_drained(bs, backing_hd, errp);
3575 bdrv_graph_wrunlock();
3576 bdrv_drained_end(drain_bs);
3577 bdrv_unref(drain_bs);
3578

--- 22 unchanged lines hidden (view full) ---

3601 bool implicit_backing = false;
3602 BlockDriverState *backing_hd;
3603 AioContext *backing_hd_ctx;
3604 QDict *options;
3605 QDict *tmp_parent_options = NULL;
3606 Error *local_err = NULL;
3607
3608 GLOBAL_STATE_CODE();
3609 GRAPH_RDLOCK_GUARD_MAINLOOP();
3605
3606 if (bs->backing != NULL) {
3607 goto free_exit;
3608 }
3609
3610 /* NULL means an empty set of options */
3611 if (parent_options == NULL) {
3612 tmp_parent_options = qdict_new();

--- 25 unchanged lines hidden (view full) ---

3638 * backing file's behavior, we might still consider it the
3639 * implicit backing file. But it's easier this way, and
3640 * just specifying some of the backing BDS's options is
3641 * only possible with -drive anyway (otherwise the QAPI
3642 * schema forces the user to specify everything). */
3643 implicit_backing = !strcmp(bs->auto_backing_file, bs->backing_file);
3644 }
3645
3610
3611 if (bs->backing != NULL) {
3612 goto free_exit;
3613 }
3614
3615 /* NULL means an empty set of options */
3616 if (parent_options == NULL) {
3617 tmp_parent_options = qdict_new();

--- 25 unchanged lines hidden (view full) ---

3643 * backing file's behavior, we might still consider it the
3644 * implicit backing file. But it's easier this way, and
3645 * just specifying some of the backing BDS's options is
3646 * only possible with -drive anyway (otherwise the QAPI
3647 * schema forces the user to specify everything). */
3648 implicit_backing = !strcmp(bs->auto_backing_file, bs->backing_file);
3649 }
3650
3646 bdrv_graph_rdlock_main_loop();
3647 backing_filename = bdrv_get_full_backing_filename(bs, &local_err);
3651 backing_filename = bdrv_get_full_backing_filename(bs, &local_err);
3648 bdrv_graph_rdunlock_main_loop();
3649
3650 if (local_err) {
3651 ret = -EINVAL;
3652 error_propagate(errp, local_err);
3653 qobject_unref(options);
3654 goto free_exit;
3655 }
3656 }
3657

--- 14 unchanged lines hidden (view full) ---

3672 if (!backing_hd) {
3673 bs->open_flags |= BDRV_O_NO_BACKING;
3674 error_prepend(errp, "Could not open backing file: ");
3675 ret = -EINVAL;
3676 goto free_exit;
3677 }
3678
3679 if (implicit_backing) {
3652 if (local_err) {
3653 ret = -EINVAL;
3654 error_propagate(errp, local_err);
3655 qobject_unref(options);
3656 goto free_exit;
3657 }
3658 }
3659

--- 14 unchanged lines hidden (view full) ---

3674 if (!backing_hd) {
3675 bs->open_flags |= BDRV_O_NO_BACKING;
3676 error_prepend(errp, "Could not open backing file: ");
3677 ret = -EINVAL;
3678 goto free_exit;
3679 }
3680
3681 if (implicit_backing) {
3680 bdrv_graph_rdlock_main_loop();
3681 bdrv_refresh_filename(backing_hd);
3682 bdrv_refresh_filename(backing_hd);
3682 bdrv_graph_rdunlock_main_loop();
3683 pstrcpy(bs->auto_backing_file, sizeof(bs->auto_backing_file),
3684 backing_hd->filename);
3685 }
3686
3687 /* Hook up the backing file link; drop our reference, bs owns the
3688 * backing_hd reference now */
3689 backing_hd_ctx = bdrv_get_aio_context(backing_hd);
3690 aio_context_acquire(backing_hd_ctx);

--- 1054 unchanged lines hidden (view full) ---

4745 */
4746static int GRAPH_UNLOCKED
4747bdrv_reopen_parse_file_or_backing(BDRVReopenState *reopen_state,
4748 bool is_backing, Transaction *tran,
4749 Error **errp)
4750{
4751 BlockDriverState *bs = reopen_state->bs;
4752 BlockDriverState *new_child_bs;
3683 pstrcpy(bs->auto_backing_file, sizeof(bs->auto_backing_file),
3684 backing_hd->filename);
3685 }
3686
3687 /* Hook up the backing file link; drop our reference, bs owns the
3688 * backing_hd reference now */
3689 backing_hd_ctx = bdrv_get_aio_context(backing_hd);
3690 aio_context_acquire(backing_hd_ctx);

--- 1054 unchanged lines hidden (view full) ---

4745 */
4746static int GRAPH_UNLOCKED
4747bdrv_reopen_parse_file_or_backing(BDRVReopenState *reopen_state,
4748 bool is_backing, Transaction *tran,
4749 Error **errp)
4750{
4751 BlockDriverState *bs = reopen_state->bs;
4752 BlockDriverState *new_child_bs;
4753 BlockDriverState *old_child_bs = is_backing ? child_bs(bs->backing) :
4754 child_bs(bs->file);
4753 BlockDriverState *old_child_bs;
4754
4755 const char *child_name = is_backing ? "backing" : "file";
4756 QObject *value;
4757 const char *str;
4758 AioContext *ctx, *old_ctx;
4759 bool has_child;
4760 int ret;
4761
4762 GLOBAL_STATE_CODE();

--- 29 unchanged lines hidden (view full) ---

4792 default:
4793 /*
4794 * The options QDict has been flattened, so 'backing' and 'file'
4795 * do not allow any other data type here.
4796 */
4797 g_assert_not_reached();
4798 }
4799
4755 const char *child_name = is_backing ? "backing" : "file";
4756 QObject *value;
4757 const char *str;
4758 AioContext *ctx, *old_ctx;
4759 bool has_child;
4760 int ret;
4761
4762 GLOBAL_STATE_CODE();

--- 29 unchanged lines hidden (view full) ---

4792 default:
4793 /*
4794 * The options QDict has been flattened, so 'backing' and 'file'
4795 * do not allow any other data type here.
4796 */
4797 g_assert_not_reached();
4798 }
4799
4800 old_child_bs = is_backing ? child_bs(bs->backing) : child_bs(bs->file);
4800 if (old_child_bs == new_child_bs) {
4801 ret = 0;
4802 goto out_rdlock;
4803 }
4804
4805 if (old_child_bs) {
4806 if (bdrv_skip_implicit_filters(old_child_bs) == new_child_bs) {
4807 ret = 0;

--- 195 unchanged lines hidden (view full) ---

5003
5004 drv_prepared = true;
5005
5006 /*
5007 * We must provide the 'backing' option if the BDS has a backing
5008 * file or if the image file has a backing file name as part of
5009 * its metadata. Otherwise the 'backing' option can be omitted.
5010 */
4801 if (old_child_bs == new_child_bs) {
4802 ret = 0;
4803 goto out_rdlock;
4804 }
4805
4806 if (old_child_bs) {
4807 if (bdrv_skip_implicit_filters(old_child_bs) == new_child_bs) {
4808 ret = 0;

--- 195 unchanged lines hidden (view full) ---

5004
5005 drv_prepared = true;
5006
5007 /*
5008 * We must provide the 'backing' option if the BDS has a backing
5009 * file or if the image file has a backing file name as part of
5010 * its metadata. Otherwise the 'backing' option can be omitted.
5011 */
5012 bdrv_graph_rdlock_main_loop();
5011 if (drv->supports_backing && reopen_state->backing_missing &&
5012 (reopen_state->bs->backing || reopen_state->bs->backing_file[0])) {
5013 error_setg(errp, "backing is missing for '%s'",
5014 reopen_state->bs->node_name);
5013 if (drv->supports_backing && reopen_state->backing_missing &&
5014 (reopen_state->bs->backing || reopen_state->bs->backing_file[0])) {
5015 error_setg(errp, "backing is missing for '%s'",
5016 reopen_state->bs->node_name);
5017 bdrv_graph_rdunlock_main_loop();
5015 ret = -EINVAL;
5016 goto error;
5017 }
5018 ret = -EINVAL;
5019 goto error;
5020 }
5021 bdrv_graph_rdunlock_main_loop();
5018
5019 /*
5020 * Allow changing the 'backing' option. The new value can be
5021 * either a reference to an existing node (using its node name)
5022 * or NULL to simply detach the current backing file.
5023 */
5024 ret = bdrv_reopen_parse_file_or_backing(reopen_state, true,
5025 change_child_tran, errp);

--- 173 unchanged lines hidden (view full) ---

5199 }
5200 bs->drv = NULL;
5201 }
5202
5203 bdrv_graph_wrlock(bs);
5204 QLIST_FOREACH_SAFE(child, &bs->children, next, next) {
5205 bdrv_unref_child(bs, child);
5206 }
5022
5023 /*
5024 * Allow changing the 'backing' option. The new value can be
5025 * either a reference to an existing node (using its node name)
5026 * or NULL to simply detach the current backing file.
5027 */
5028 ret = bdrv_reopen_parse_file_or_backing(reopen_state, true,
5029 change_child_tran, errp);

--- 173 unchanged lines hidden (view full) ---

5203 }
5204 bs->drv = NULL;
5205 }
5206
5207 bdrv_graph_wrlock(bs);
5208 QLIST_FOREACH_SAFE(child, &bs->children, next, next) {
5209 bdrv_unref_child(bs, child);
5210 }
5207 bdrv_graph_wrunlock();
5208
5209 assert(!bs->backing);
5210 assert(!bs->file);
5211
5212 assert(!bs->backing);
5213 assert(!bs->file);
5214 bdrv_graph_wrunlock();
5215
5211 g_free(bs->opaque);
5212 bs->opaque = NULL;
5213 qatomic_set(&bs->copy_on_read, 0);
5214 bs->backing_file[0] = '\0';
5215 bs->backing_format[0] = '\0';
5216 bs->total_sectors = 0;
5217 bs->encrypted = false;
5218 bs->sg = false;

--- 307 unchanged lines hidden (view full) ---

5526{
5527 int ret;
5528 BdrvChild *child;
5529 Transaction *tran = tran_new();
5530 AioContext *old_context, *new_context = NULL;
5531
5532 GLOBAL_STATE_CODE();
5533
5216 g_free(bs->opaque);
5217 bs->opaque = NULL;
5218 qatomic_set(&bs->copy_on_read, 0);
5219 bs->backing_file[0] = '\0';
5220 bs->backing_format[0] = '\0';
5221 bs->total_sectors = 0;
5222 bs->encrypted = false;
5223 bs->sg = false;

--- 307 unchanged lines hidden (view full) ---

5531{
5532 int ret;
5533 BdrvChild *child;
5534 Transaction *tran = tran_new();
5535 AioContext *old_context, *new_context = NULL;
5536
5537 GLOBAL_STATE_CODE();
5538
5539 bdrv_graph_rdlock_main_loop();
5534 assert(!bs_new->backing);
5540 assert(!bs_new->backing);
5541 bdrv_graph_rdunlock_main_loop();
5535
5536 old_context = bdrv_get_aio_context(bs_top);
5537 bdrv_drained_begin(bs_top);
5538
5539 /*
5540 * bdrv_drained_begin() requires that only the AioContext of the drained
5541 * node is locked, and at this point it can still differ from the AioContext
5542 * of bs_top.

--- 2567 unchanged lines hidden (view full) ---

8110 }
8111
8112 return found_any;
8113}
8114
8115/* Note: This function may return false positives; it may return true
8116 * even if opening the backing file specified by bs's image header
8117 * would result in exactly bs->backing. */
5542
5543 old_context = bdrv_get_aio_context(bs_top);
5544 bdrv_drained_begin(bs_top);
5545
5546 /*
5547 * bdrv_drained_begin() requires that only the AioContext of the drained
5548 * node is locked, and at this point it can still differ from the AioContext
5549 * of bs_top.

--- 2567 unchanged lines hidden (view full) ---

8117 }
8118
8119 return found_any;
8120}
8121
8122/* Note: This function may return false positives; it may return true
8123 * even if opening the backing file specified by bs's image header
8124 * would result in exactly bs->backing. */
8118static bool bdrv_backing_overridden(BlockDriverState *bs)
8125static bool GRAPH_RDLOCK bdrv_backing_overridden(BlockDriverState *bs)
8119{
8120 GLOBAL_STATE_CODE();
8121 if (bs->backing) {
8122 return strcmp(bs->auto_backing_file,
8123 bs->backing->bs->filename);
8124 } else {
8125 /* No backing BDS, so if the image header reports any backing
8126 * file, it must have been suppressed */

--- 504 unchanged lines hidden ---
8126{
8127 GLOBAL_STATE_CODE();
8128 if (bs->backing) {
8129 return strcmp(bs->auto_backing_file,
8130 bs->backing->bs->filename);
8131 } else {
8132 /* No backing BDS, so if the image header reports any backing
8133 * file, it must have been suppressed */

--- 504 unchanged lines hidden ---