block/copy-before-write: bdrv_cbw_append(): replace child at last
Refactor the function to replace child at last. Thus we don't need to revert it and code is simplified. block-copy state initialization being done before replacing the child doesn't need any drained section. Signed-off-by: Vladimir Sementsov-Ogievskiy <vsementsov@virtuozzo.com> Reviewed-by: Max Reitz <mreitz@redhat.com> Message-Id: <20210824083856.17408-14-vsementsov@virtuozzo.com> Signed-off-by: Hanna Reitz <hreitz@redhat.com>
This commit is contained in:
parent
3c1e63277e
commit
7ddbce2dec
@ -172,7 +172,6 @@ BlockDriverState *bdrv_cbw_append(BlockDriverState *source,
|
|||||||
int ret;
|
int ret;
|
||||||
BDRVCopyBeforeWriteState *state;
|
BDRVCopyBeforeWriteState *state;
|
||||||
BlockDriverState *top;
|
BlockDriverState *top;
|
||||||
bool appended = false;
|
|
||||||
|
|
||||||
assert(source->total_sectors == target->total_sectors);
|
assert(source->total_sectors == target->total_sectors);
|
||||||
|
|
||||||
@ -196,8 +195,7 @@ BlockDriverState *bdrv_cbw_append(BlockDriverState *source,
|
|||||||
BDRV_CHILD_DATA, errp);
|
BDRV_CHILD_DATA, errp);
|
||||||
if (!state->target) {
|
if (!state->target) {
|
||||||
error_prepend(errp, "Cannot attach target child: ");
|
error_prepend(errp, "Cannot attach target child: ");
|
||||||
bdrv_unref(top);
|
goto fail;
|
||||||
return NULL;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
bdrv_ref(source);
|
bdrv_ref(source);
|
||||||
@ -206,18 +204,8 @@ BlockDriverState *bdrv_cbw_append(BlockDriverState *source,
|
|||||||
errp);
|
errp);
|
||||||
if (!top->file) {
|
if (!top->file) {
|
||||||
error_prepend(errp, "Cannot attach file child: ");
|
error_prepend(errp, "Cannot attach file child: ");
|
||||||
bdrv_unref(top);
|
|
||||||
return NULL;
|
|
||||||
}
|
|
||||||
|
|
||||||
bdrv_drained_begin(source);
|
|
||||||
|
|
||||||
ret = bdrv_replace_node(source, top, errp);
|
|
||||||
if (ret < 0) {
|
|
||||||
error_prepend(errp, "Cannot append copy-before-write filter: ");
|
|
||||||
goto fail;
|
goto fail;
|
||||||
}
|
}
|
||||||
appended = true;
|
|
||||||
|
|
||||||
state->bcs = block_copy_state_new(top->file, state->target, false, compress,
|
state->bcs = block_copy_state_new(top->file, state->target, false, compress,
|
||||||
errp);
|
errp);
|
||||||
@ -225,21 +213,22 @@ BlockDriverState *bdrv_cbw_append(BlockDriverState *source,
|
|||||||
error_prepend(errp, "Cannot create block-copy-state: ");
|
error_prepend(errp, "Cannot create block-copy-state: ");
|
||||||
goto fail;
|
goto fail;
|
||||||
}
|
}
|
||||||
*bcs = state->bcs;
|
|
||||||
|
|
||||||
|
bdrv_drained_begin(source);
|
||||||
|
ret = bdrv_replace_node(source, top, errp);
|
||||||
bdrv_drained_end(source);
|
bdrv_drained_end(source);
|
||||||
|
if (ret < 0) {
|
||||||
|
error_prepend(errp, "Cannot append copy-before-write filter: ");
|
||||||
|
goto fail;
|
||||||
|
}
|
||||||
|
|
||||||
|
*bcs = state->bcs;
|
||||||
|
|
||||||
return top;
|
return top;
|
||||||
|
|
||||||
fail:
|
fail:
|
||||||
if (appended) {
|
block_copy_state_free(state->bcs);
|
||||||
bdrv_cbw_drop(top);
|
bdrv_unref(top);
|
||||||
} else {
|
|
||||||
bdrv_unref(top);
|
|
||||||
}
|
|
||||||
|
|
||||||
bdrv_drained_end(source);
|
|
||||||
|
|
||||||
return NULL;
|
return NULL;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
Loading…
x
Reference in New Issue
Block a user