Move the BDS replacing code to a separate function so that it can be used in the next patch. A new field "should_replace" is added and set to true so the function is always called for now.
Signed-off-by: Fam Zheng <f...@redhat.com> --- block/mirror.c | 36 +++++++++++++++++++++++------------- 1 file changed, 23 insertions(+), 13 deletions(-) diff --git a/block/mirror.c b/block/mirror.c index d56e30e..6c3fe43 100644 --- a/block/mirror.c +++ b/block/mirror.c @@ -441,26 +441,20 @@ static void mirror_drain(MirrorBlockJob *s) } typedef struct { + bool should_replace; int ret; } MirrorExitData; -static void mirror_exit(BlockJob *job, void *opaque) +static void mirror_replace(MirrorBlockJob *s, int *ret) { - MirrorBlockJob *s = container_of(job, MirrorBlockJob, common); - MirrorExitData *data = opaque; AioContext *replace_aio_context = NULL; - BlockDriverState *src = s->common.bs; - - /* Make sure that the source BDS doesn't go away before we called - * block_job_completed(). */ - bdrv_ref(src); if (s->to_replace) { replace_aio_context = bdrv_get_aio_context(s->to_replace); aio_context_acquire(replace_aio_context); } - if (s->should_complete && data->ret == 0) { + if (s->should_complete && *ret == 0) { BlockDriverState *to_replace = s->common.bs; if (s->to_replace) { to_replace = s->to_replace; @@ -470,7 +464,7 @@ static void mirror_exit(BlockJob *job, void *opaque) * nodes could have been newly attached to a BlockBackend. */ if (to_replace->blk && s->target->blk) { error_report("block job: Can't create node with two BlockBackends"); - data->ret = -EINVAL; + *ret = -EINVAL; goto out; } @@ -481,14 +475,29 @@ static void mirror_exit(BlockJob *job, void *opaque) } out: + if (replace_aio_context) { + aio_context_release(replace_aio_context); + } +} + +static void mirror_exit(BlockJob *job, void *opaque) +{ + MirrorBlockJob *s = container_of(job, MirrorBlockJob, common); + MirrorExitData *data = opaque; + BlockDriverState *src = s->common.bs; + + /* Make sure that the source BDS doesn't go away before we called + * block_job_completed(). */ + bdrv_ref(src); + + if (data->should_replace) { + mirror_replace(s, &data->ret); + } if (s->to_replace) { bdrv_op_unblock_all(s->to_replace, s->replace_blocker); error_free(s->replace_blocker); bdrv_unref(s->to_replace); } - if (replace_aio_context) { - aio_context_release(replace_aio_context); - } g_free(s->replaces); bdrv_op_unblock_all(s->target, s->common.blocker); bdrv_unref(s->target); @@ -713,6 +722,7 @@ immediate_exit: data = g_malloc(sizeof(*data)); data->ret = ret; + data->should_replace = true; /* Before we switch to target in mirror_exit, make sure data doesn't * change. */ bdrv_drained_begin(s->common.bs); -- 2.8.0