Skip to content

Commit

Permalink
block: document child argument of bdrv_attach_child_common()
Browse files Browse the repository at this point in the history
The logic around **child is not obvious: this reference is used not
only to return resulting child, but also to rollback NULL value on
transaction abort.

So, let's add documentation and some assertions.

While being here, drop extra declaration of bdrv_attach_child_noperm().

Signed-off-by: Vladimir Sementsov-Ogievskiy <vsementsov@virtuozzo.com>
Message-Id: <20210601075218.79249-2-vsementsov@virtuozzo.com>
Signed-off-by: Kevin Wolf <kwolf@redhat.com>
  • Loading branch information
Vladimir Sementsov-Ogievskiy authored and kevmw committed Jun 2, 2021
1 parent fa95e9f commit f8d2ad7
Showing 1 changed file with 15 additions and 9 deletions.
24 changes: 15 additions & 9 deletions block.c
Expand Up @@ -84,14 +84,6 @@ static BlockDriverState *bdrv_open_inherit(const char *filename,

static void bdrv_replace_child_noperm(BdrvChild *child,
BlockDriverState *new_bs);
static int bdrv_attach_child_noperm(BlockDriverState *parent_bs,
BlockDriverState *child_bs,
const char *child_name,
const BdrvChildClass *child_class,
BdrvChildRole child_role,
BdrvChild **child,
Transaction *tran,
Error **errp);
static void bdrv_remove_filter_or_cow_child(BlockDriverState *bs,
Transaction *tran);

Expand Down Expand Up @@ -2759,6 +2751,12 @@ static TransactionActionDrv bdrv_attach_child_common_drv = {

/*
* Common part of attaching bdrv child to bs or to blk or to job
*
* Resulting new child is returned through @child.
* At start *@child must be NULL.
* @child is saved to a new entry of @tran, so that *@child could be reverted to
* NULL on abort(). So referenced variable must live at least until transaction
* end.
*/
static int bdrv_attach_child_common(BlockDriverState *child_bs,
const char *child_name,
Expand Down Expand Up @@ -2833,6 +2831,10 @@ static int bdrv_attach_child_common(BlockDriverState *child_bs,
return 0;
}

/*
* Variable referenced by @child must live at least until transaction end.
* (see bdrv_attach_child_common() doc for details)
*/
static int bdrv_attach_child_noperm(BlockDriverState *parent_bs,
BlockDriverState *child_bs,
const char *child_name,
Expand Down Expand Up @@ -2915,14 +2917,16 @@ BdrvChild *bdrv_root_attach_child(BlockDriverState *child_bs,
child_role, perm, shared_perm, opaque,
&child, tran, errp);
if (ret < 0) {
assert(child == NULL);
goto out;
}

ret = bdrv_refresh_perms(child_bs, errp);

out:
tran_finalize(tran, ret);
/* child is unset on failure by bdrv_attach_child_common_abort() */
assert((ret < 0) == !child);

bdrv_unref(child_bs);
return child;
}
Expand Down Expand Up @@ -2962,6 +2966,8 @@ BdrvChild *bdrv_attach_child(BlockDriverState *parent_bs,

out:
tran_finalize(tran, ret);
/* child is unset on failure by bdrv_attach_child_common_abort() */
assert((ret < 0) == !child);

bdrv_unref(child_bs);

Expand Down

0 comments on commit f8d2ad7

Please sign in to comment.