block: Keep nodes drained between reopen_queue/multiple

The bdrv_reopen*() implementation doesn't like it if the graph is
changed between queuing nodes for reopen and actually reopening them
(one of the reasons is that queuing can be recursive).

So instead of draining the device only in bdrv_reopen_multiple(),
require that callers already drained all affected nodes, and assert this
in bdrv_reopen_queue().

Signed-off-by: Kevin Wolf <kwolf@redhat.com>
Reviewed-by: Fam Zheng <famz@redhat.com>
This commit is contained in:
Kevin Wolf 2017-12-06 20:24:44 +01:00
parent 44487eb973
commit 1a63a90750
3 changed files with 25 additions and 7 deletions

23
block.c
View File

@ -2766,6 +2766,7 @@ BlockDriverState *bdrv_open(const char *filename, const char *reference,
* returns a pointer to bs_queue, which is either the newly allocated * returns a pointer to bs_queue, which is either the newly allocated
* bs_queue, or the existing bs_queue being used. * bs_queue, or the existing bs_queue being used.
* *
* bs must be drained between bdrv_reopen_queue() and bdrv_reopen_multiple().
*/ */
static BlockReopenQueue *bdrv_reopen_queue_child(BlockReopenQueue *bs_queue, static BlockReopenQueue *bdrv_reopen_queue_child(BlockReopenQueue *bs_queue,
BlockDriverState *bs, BlockDriverState *bs,
@ -2781,6 +2782,11 @@ static BlockReopenQueue *bdrv_reopen_queue_child(BlockReopenQueue *bs_queue,
BdrvChild *child; BdrvChild *child;
QDict *old_options, *explicit_options; QDict *old_options, *explicit_options;
/* Make sure that the caller remembered to use a drained section. This is
* important to avoid graph changes between the recursive queuing here and
* bdrv_reopen_multiple(). */
assert(bs->quiesce_counter > 0);
if (bs_queue == NULL) { if (bs_queue == NULL) {
bs_queue = g_new0(BlockReopenQueue, 1); bs_queue = g_new0(BlockReopenQueue, 1);
QSIMPLEQ_INIT(bs_queue); QSIMPLEQ_INIT(bs_queue);
@ -2905,6 +2911,8 @@ BlockReopenQueue *bdrv_reopen_queue(BlockReopenQueue *bs_queue,
* If all devices prepare successfully, then the changes are committed * If all devices prepare successfully, then the changes are committed
* to all devices. * to all devices.
* *
* All affected nodes must be drained between bdrv_reopen_queue() and
* bdrv_reopen_multiple().
*/ */
int bdrv_reopen_multiple(AioContext *ctx, BlockReopenQueue *bs_queue, Error **errp) int bdrv_reopen_multiple(AioContext *ctx, BlockReopenQueue *bs_queue, Error **errp)
{ {
@ -2914,11 +2922,8 @@ int bdrv_reopen_multiple(AioContext *ctx, BlockReopenQueue *bs_queue, Error **er
assert(bs_queue != NULL); assert(bs_queue != NULL);
aio_context_release(ctx);
bdrv_drain_all_begin();
aio_context_acquire(ctx);
QSIMPLEQ_FOREACH(bs_entry, bs_queue, entry) { QSIMPLEQ_FOREACH(bs_entry, bs_queue, entry) {
assert(bs_entry->state.bs->quiesce_counter > 0);
if (bdrv_reopen_prepare(&bs_entry->state, bs_queue, &local_err)) { if (bdrv_reopen_prepare(&bs_entry->state, bs_queue, &local_err)) {
error_propagate(errp, local_err); error_propagate(errp, local_err);
goto cleanup; goto cleanup;
@ -2947,8 +2952,6 @@ cleanup:
} }
g_free(bs_queue); g_free(bs_queue);
bdrv_drain_all_end();
return ret; return ret;
} }
@ -2958,12 +2961,18 @@ int bdrv_reopen(BlockDriverState *bs, int bdrv_flags, Error **errp)
{ {
int ret = -1; int ret = -1;
Error *local_err = NULL; Error *local_err = NULL;
BlockReopenQueue *queue = bdrv_reopen_queue(NULL, bs, NULL, bdrv_flags); BlockReopenQueue *queue;
bdrv_subtree_drained_begin(bs);
queue = bdrv_reopen_queue(NULL, bs, NULL, bdrv_flags);
ret = bdrv_reopen_multiple(bdrv_get_aio_context(bs), queue, &local_err); ret = bdrv_reopen_multiple(bdrv_get_aio_context(bs), queue, &local_err);
if (local_err != NULL) { if (local_err != NULL) {
error_propagate(errp, local_err); error_propagate(errp, local_err);
} }
bdrv_subtree_drained_end(bs);
return ret; return ret;
} }

View File

@ -394,6 +394,9 @@ static void reopen_backing_file(BlockDriverState *bs, bool writable,
new_secondary_flags = s->orig_secondary_flags; new_secondary_flags = s->orig_secondary_flags;
} }
bdrv_subtree_drained_begin(s->hidden_disk->bs);
bdrv_subtree_drained_begin(s->secondary_disk->bs);
if (orig_hidden_flags != new_hidden_flags) { if (orig_hidden_flags != new_hidden_flags) {
reopen_queue = bdrv_reopen_queue(reopen_queue, s->hidden_disk->bs, NULL, reopen_queue = bdrv_reopen_queue(reopen_queue, s->hidden_disk->bs, NULL,
new_hidden_flags); new_hidden_flags);
@ -409,6 +412,9 @@ static void reopen_backing_file(BlockDriverState *bs, bool writable,
reopen_queue, &local_err); reopen_queue, &local_err);
error_propagate(errp, local_err); error_propagate(errp, local_err);
} }
bdrv_subtree_drained_end(s->hidden_disk->bs);
bdrv_subtree_drained_end(s->secondary_disk->bs);
} }
static void backup_job_cleanup(BlockDriverState *bs) static void backup_job_cleanup(BlockDriverState *bs)

View File

@ -2013,8 +2013,11 @@ static int reopen_f(BlockBackend *blk, int argc, char **argv)
opts = qopts ? qemu_opts_to_qdict(qopts, NULL) : NULL; opts = qopts ? qemu_opts_to_qdict(qopts, NULL) : NULL;
qemu_opts_reset(&reopen_opts); qemu_opts_reset(&reopen_opts);
bdrv_subtree_drained_begin(bs);
brq = bdrv_reopen_queue(NULL, bs, opts, flags); brq = bdrv_reopen_queue(NULL, bs, opts, flags);
bdrv_reopen_multiple(bdrv_get_aio_context(bs), brq, &local_err); bdrv_reopen_multiple(bdrv_get_aio_context(bs), brq, &local_err);
bdrv_subtree_drained_end(bs);
if (local_err) { if (local_err) {
error_report_err(local_err); error_report_err(local_err);
} else { } else {