[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]
[Qemu-devel] [PULL 22/71] block: improve should_update_child
From: |
Kevin Wolf |
Subject: |
[Qemu-devel] [PULL 22/71] block: improve should_update_child |
Date: |
Mon, 25 Feb 2019 16:20:04 +0100 |
From: Vladimir Sementsov-Ogievskiy <address@hidden>
As it already said in the comment, we don't want to create loops in
parent->child relations. So, when we try to append @to to @c, we should
check that @c is not in @to children subtree, and we should check it
recursively, not only the first level. The patch provides BFS-based
search, to check the relations.
This is needed for further fleecing-hook filter usage: we need to
append it to source, when the hook is already a parent of target, and
source may be in a backing chain of target (fleecing-scheme). So, on
appending, the hook should not became a child (direct or through
children subtree) of the target.
Signed-off-by: Vladimir Sementsov-Ogievskiy <address@hidden>
Signed-off-by: Kevin Wolf <address@hidden>
---
block.c | 43 +++++++++++++++++++++++++++++++++++++------
1 file changed, 37 insertions(+), 6 deletions(-)
diff --git a/block.c b/block.c
index 375a216f76..bb4bf1237c 100644
--- a/block.c
+++ b/block.c
@@ -3542,7 +3542,9 @@ void bdrv_close_all(void)
static bool should_update_child(BdrvChild *c, BlockDriverState *to)
{
- BdrvChild *to_c;
+ GQueue *queue;
+ GHashTable *found;
+ bool ret;
if (c->role->stay_at_node) {
return false;
@@ -3578,14 +3580,43 @@ static bool should_update_child(BdrvChild *c,
BlockDriverState *to)
* if A is a child of B, that means we cannot replace A by B there
* because that would create a loop. Silently detaching A from B
* is also not really an option. So overall just leaving A in
- * place there is the most sensible choice. */
- QLIST_FOREACH(to_c, &to->children, next) {
- if (to_c == c) {
- return false;
+ * place there is the most sensible choice.
+ *
+ * We would also create a loop in any cases where @c is only
+ * indirectly referenced by @to. Prevent this by returning false
+ * if @c is found (by breadth-first search) anywhere in the whole
+ * subtree of @to.
+ */
+
+ ret = true;
+ found = g_hash_table_new(NULL, NULL);
+ g_hash_table_add(found, to);
+ queue = g_queue_new();
+ g_queue_push_tail(queue, to);
+
+ while (!g_queue_is_empty(queue)) {
+ BlockDriverState *v = g_queue_pop_head(queue);
+ BdrvChild *c2;
+
+ QLIST_FOREACH(c2, &v->children, next) {
+ if (c2 == c) {
+ ret = false;
+ break;
+ }
+
+ if (g_hash_table_contains(found, c2->bs)) {
+ continue;
+ }
+
+ g_queue_push_tail(queue, c2->bs);
+ g_hash_table_add(found, c2->bs);
}
}
- return true;
+ g_queue_free(queue);
+ g_hash_table_destroy(found);
+
+ return ret;
}
void bdrv_replace_node(BlockDriverState *from, BlockDriverState *to,
--
2.20.1
- [Qemu-devel] [PULL 12/71] io: Make qio_channel_yield() interruptible, (continued)
- [Qemu-devel] [PULL 12/71] io: Make qio_channel_yield() interruptible, Kevin Wolf, 2019/02/25
- [Qemu-devel] [PULL 14/71] nbd: Move nbd_read_eof() to nbd/client.c, Kevin Wolf, 2019/02/25
- [Qemu-devel] [PULL 18/71] block: Fix AioContext switch for drained node, Kevin Wolf, 2019/02/25
- [Qemu-devel] [PULL 13/71] io: Remove redundant read/write_coroutine assignments, Kevin Wolf, 2019/02/25
- [Qemu-devel] [PULL 16/71] nbd: Increase bs->in_flight during AioContext switch, Kevin Wolf, 2019/02/25
- [Qemu-devel] [PULL 20/71] block: Use normal drain for bdrv_set_aio_context(), Kevin Wolf, 2019/02/25
- [Qemu-devel] [PULL 19/71] test-bdrv-drain: AioContext switch in drained section, Kevin Wolf, 2019/02/25
- [Qemu-devel] [PULL 15/71] nbd: Use low-level QIOChannel API in nbd_read_eof(), Kevin Wolf, 2019/02/25
- [Qemu-devel] [PULL 17/71] block: Don't poll in bdrv_set_aio_context(), Kevin Wolf, 2019/02/25
- [Qemu-devel] [PULL 21/71] aio-posix: Assert that aio_poll() is always called in home thread, Kevin Wolf, 2019/02/25
- [Qemu-devel] [PULL 22/71] block: improve should_update_child,
Kevin Wolf <=
- [Qemu-devel] [PULL 23/71] block: fix bdrv_check_perm for non-tree subgraph, Kevin Wolf, 2019/02/25
- [Qemu-devel] [PULL 24/71] tests: add test-bdrv-graph-mod, Kevin Wolf, 2019/02/25
- [Qemu-devel] [PULL 25/71] qcow2: Assert that L2 table offsets fit in the L1 table, Kevin Wolf, 2019/02/25
- [Qemu-devel] [PULL 29/71] block: Skip implicit nodes for filename info, Kevin Wolf, 2019/02/25
- [Qemu-devel] [PULL 32/71] iotests.py: Add filter_imgfmt(), Kevin Wolf, 2019/02/25
- [Qemu-devel] [PULL 33/71] iotests.py: Add node_info(), Kevin Wolf, 2019/02/25
- [Qemu-devel] [PULL 38/71] block: Add bdrv_make_absolute_filename(), Kevin Wolf, 2019/02/25
- [Qemu-devel] [PULL 41/71] blkverify: Make bdrv_dirname() return NULL, Kevin Wolf, 2019/02/25
- [Qemu-devel] [PULL 36/71] block: bdrv_get_full_backing_filename_from_...'s ret. val., Kevin Wolf, 2019/02/25
- [Qemu-devel] [PULL 28/71] block: Use children list in bdrv_refresh_filename, Kevin Wolf, 2019/02/25