block: add bdrv_set_backing_noperm() transaction action

Split out no-perm part of bdrv_set_backing_hd() as a separate
transaction action. Note the in case of existing BdrvChild we reuse it,
not recreate, just to do less actions.

We don't need to create extra reference to backing_hd as we don't lose
it in bdrv_attach_child().

Signed-off-by: Vladimir Sementsov-Ogievskiy <vsementsov@virtuozzo.com>
Reviewed-by: Kevin Wolf <kwolf@redhat.com>
Message-Id: <20210428151804.439460-29-vsementsov@virtuozzo.com>
Signed-off-by: Kevin Wolf <kwolf@redhat.com>
This commit is contained in:
Vladimir Sementsov-Ogievskiy 2021-04-28 18:17:56 +03:00 committed by Kevin Wolf
parent 1e4c797c75
commit 160333e1fe

54
block.c
View file

@ -92,6 +92,8 @@ static int bdrv_attach_child_noperm(BlockDriverState *parent_bs,
BdrvChild **child, BdrvChild **child,
Transaction *tran, Transaction *tran,
Error **errp); Error **errp);
static void bdrv_remove_filter_or_cow_child(BlockDriverState *bs,
Transaction *tran);
static int bdrv_reopen_prepare(BDRVReopenState *reopen_state, BlockReopenQueue static int bdrv_reopen_prepare(BDRVReopenState *reopen_state, BlockReopenQueue
*queue, Error **errp); *queue, Error **errp);
@ -3322,8 +3324,9 @@ static BdrvChildRole bdrv_backing_role(BlockDriverState *bs)
* Sets the bs->backing link of a BDS. A new reference is created; callers * Sets the bs->backing link of a BDS. A new reference is created; callers
* which don't need their own reference any more must call bdrv_unref(). * which don't need their own reference any more must call bdrv_unref().
*/ */
int bdrv_set_backing_hd(BlockDriverState *bs, BlockDriverState *backing_hd, static int bdrv_set_backing_noperm(BlockDriverState *bs,
Error **errp) BlockDriverState *backing_hd,
Transaction *tran, Error **errp)
{ {
int ret = 0; int ret = 0;
bool update_inherits_from = bdrv_chain_contains(bs, backing_hd) && bool update_inherits_from = bdrv_chain_contains(bs, backing_hd) &&
@ -3333,36 +3336,53 @@ int bdrv_set_backing_hd(BlockDriverState *bs, BlockDriverState *backing_hd,
return -EPERM; return -EPERM;
} }
if (backing_hd) {
bdrv_ref(backing_hd);
}
if (bs->backing) { if (bs->backing) {
/* Cannot be frozen, we checked that above */ /* Cannot be frozen, we checked that above */
bdrv_unref_child(bs, bs->backing); bdrv_unset_inherits_from(bs, bs->backing, tran);
bs->backing = NULL; bdrv_remove_filter_or_cow_child(bs, tran);
} }
if (!backing_hd) { if (!backing_hd) {
goto out; goto out;
} }
bs->backing = bdrv_attach_child(bs, backing_hd, "backing", &child_of_bds, ret = bdrv_attach_child_noperm(bs, backing_hd, "backing",
bdrv_backing_role(bs), errp); &child_of_bds, bdrv_backing_role(bs),
if (!bs->backing) { &bs->backing, tran, errp);
ret = -EPERM; if (ret < 0) {
goto out; return ret;
} }
/* If backing_hd was already part of bs's backing chain, and
/*
* If backing_hd was already part of bs's backing chain, and
* inherits_from pointed recursively to bs then let's update it to * inherits_from pointed recursively to bs then let's update it to
* point directly to bs (else it will become NULL). */ * point directly to bs (else it will become NULL).
*/
if (update_inherits_from) { if (update_inherits_from) {
backing_hd->inherits_from = bs; bdrv_set_inherits_from(backing_hd, bs, tran);
} }
out: out:
bdrv_refresh_limits(bs, NULL, NULL); bdrv_refresh_limits(bs, tran, NULL);
return 0;
}
int bdrv_set_backing_hd(BlockDriverState *bs, BlockDriverState *backing_hd,
Error **errp)
{
int ret;
Transaction *tran = tran_new();
ret = bdrv_set_backing_noperm(bs, backing_hd, tran, errp);
if (ret < 0) {
goto out;
}
ret = bdrv_refresh_perms(bs, errp);
out:
tran_finalize(tran, ret);
return ret; return ret;
} }