Blame SOURCES/kvm-test-bdrv-drain-Graph-change-through-parent-callback.patch

357786
From d365fdc837cbf347d8575592aeb96097b6a85923 Mon Sep 17 00:00:00 2001
357786
From: Kevin Wolf <kwolf@redhat.com>
357786
Date: Fri, 14 Sep 2018 10:55:11 +0200
357786
Subject: [PATCH 20/49] test-bdrv-drain: Graph change through parent callback
357786
357786
RH-Author: Kevin Wolf <kwolf@redhat.com>
357786
Message-id: <20180914105540.18077-14-kwolf@redhat.com>
357786
Patchwork-id: 82165
357786
O-Subject: [RHV-7.6 qemu-kvm-rhev PATCH 13/42] test-bdrv-drain: Graph change through parent callback
357786
Bugzilla: 1601212
357786
RH-Acked-by: John Snow <jsnow@redhat.com>
357786
RH-Acked-by: Max Reitz <mreitz@redhat.com>
357786
RH-Acked-by: Fam Zheng <famz@redhat.com>
357786
357786
Signed-off-by: Kevin Wolf <kwolf@redhat.com>
357786
(cherry picked from commit 231281ab42dad2b407b941e36ad11cbc6586e937)
357786
Signed-off-by: Kevin Wolf <kwolf@redhat.com>
357786
Signed-off-by: Miroslav Rezanina <mrezanin@redhat.com>
357786
---
357786
 tests/test-bdrv-drain.c | 130 ++++++++++++++++++++++++++++++++++++++++++++++++
357786
 1 file changed, 130 insertions(+)
357786
357786
diff --git a/tests/test-bdrv-drain.c b/tests/test-bdrv-drain.c
357786
index 38706b0..f786326 100644
357786
--- a/tests/test-bdrv-drain.c
357786
+++ b/tests/test-bdrv-drain.c
357786
@@ -977,6 +977,135 @@ static void test_detach_by_drain_subtree(void)
357786
 }
357786
 
357786
 
357786
+struct detach_by_parent_data {
357786
+    BlockDriverState *parent_b;
357786
+    BdrvChild *child_b;
357786
+    BlockDriverState *c;
357786
+    BdrvChild *child_c;
357786
+};
357786
+
357786
+static void detach_by_parent_aio_cb(void *opaque, int ret)
357786
+{
357786
+    struct detach_by_parent_data *data = opaque;
357786
+
357786
+    g_assert_cmpint(ret, ==, 0);
357786
+    bdrv_unref_child(data->parent_b, data->child_b);
357786
+
357786
+    bdrv_ref(data->c);
357786
+    data->child_c = bdrv_attach_child(data->parent_b, data->c, "PB-C",
357786
+                                      &child_file, &error_abort);
357786
+}
357786
+
357786
+/*
357786
+ * Initial graph:
357786
+ *
357786
+ * PA     PB
357786
+ *    \ /   \
357786
+ *     A     B     C
357786
+ *
357786
+ * PA has a pending write request whose callback changes the child nodes of PB:
357786
+ * It removes B and adds C instead. The subtree of PB is drained, which will
357786
+ * indirectly drain the write request, too.
357786
+ */
357786
+static void test_detach_by_parent_cb(void)
357786
+{
357786
+    BlockBackend *blk;
357786
+    BlockDriverState *parent_a, *parent_b, *a, *b, *c;
357786
+    BdrvChild *child_a, *child_b;
357786
+    BlockAIOCB *acb;
357786
+    struct detach_by_parent_data data;
357786
+
357786
+    QEMUIOVector qiov;
357786
+    struct iovec iov = {
357786
+        .iov_base = NULL,
357786
+        .iov_len = 0,
357786
+    };
357786
+    qemu_iovec_init_external(&qiov, &iov, 1);
357786
+
357786
+    /* Create all involved nodes */
357786
+    parent_a = bdrv_new_open_driver(&bdrv_test, "parent-a", BDRV_O_RDWR,
357786
+                                    &error_abort);
357786
+    parent_b = bdrv_new_open_driver(&bdrv_test, "parent-b", 0,
357786
+                                    &error_abort);
357786
+
357786
+    a = bdrv_new_open_driver(&bdrv_test, "a", BDRV_O_RDWR, &error_abort);
357786
+    b = bdrv_new_open_driver(&bdrv_test, "b", BDRV_O_RDWR, &error_abort);
357786
+    c = bdrv_new_open_driver(&bdrv_test, "c", BDRV_O_RDWR, &error_abort);
357786
+
357786
+    /* blk is a BB for parent-a */
357786
+    blk = blk_new(BLK_PERM_ALL, BLK_PERM_ALL);
357786
+    blk_insert_bs(blk, parent_a, &error_abort);
357786
+    bdrv_unref(parent_a);
357786
+
357786
+    /* Set child relationships */
357786
+    bdrv_ref(b);
357786
+    bdrv_ref(a);
357786
+    child_b = bdrv_attach_child(parent_b, b, "PB-B", &child_file, &error_abort);
357786
+    child_a = bdrv_attach_child(parent_b, a, "PB-A", &child_backing, &error_abort);
357786
+
357786
+    bdrv_ref(a);
357786
+    bdrv_attach_child(parent_a, a, "PA-A", &child_file, &error_abort);
357786
+
357786
+    g_assert_cmpint(parent_a->refcnt, ==, 1);
357786
+    g_assert_cmpint(parent_b->refcnt, ==, 1);
357786
+    g_assert_cmpint(a->refcnt, ==, 3);
357786
+    g_assert_cmpint(b->refcnt, ==, 2);
357786
+    g_assert_cmpint(c->refcnt, ==, 1);
357786
+
357786
+    g_assert(QLIST_FIRST(&parent_b->children) == child_a);
357786
+    g_assert(QLIST_NEXT(child_a, next) == child_b);
357786
+    g_assert(QLIST_NEXT(child_b, next) == NULL);
357786
+
357786
+    /* Start the evil write request */
357786
+    data = (struct detach_by_parent_data) {
357786
+        .parent_b = parent_b,
357786
+        .child_b = child_b,
357786
+        .c = c,
357786
+    };
357786
+    acb = blk_aio_preadv(blk, 0, &qiov, 0, detach_by_parent_aio_cb, &data);
357786
+    g_assert(acb != NULL);
357786
+
357786
+    /* Drain and check the expected result */
357786
+    bdrv_subtree_drained_begin(parent_b);
357786
+
357786
+    g_assert(data.child_c != NULL);
357786
+
357786
+    g_assert_cmpint(parent_a->refcnt, ==, 1);
357786
+    g_assert_cmpint(parent_b->refcnt, ==, 1);
357786
+    g_assert_cmpint(a->refcnt, ==, 3);
357786
+    g_assert_cmpint(b->refcnt, ==, 1);
357786
+    g_assert_cmpint(c->refcnt, ==, 2);
357786
+
357786
+    g_assert(QLIST_FIRST(&parent_b->children) == data.child_c);
357786
+    g_assert(QLIST_NEXT(data.child_c, next) == child_a);
357786
+    g_assert(QLIST_NEXT(child_a, next) == NULL);
357786
+
357786
+    g_assert_cmpint(parent_a->quiesce_counter, ==, 1);
357786
+    g_assert_cmpint(parent_b->quiesce_counter, ==, 1);
357786
+    g_assert_cmpint(a->quiesce_counter, ==, 1);
357786
+    g_assert_cmpint(b->quiesce_counter, ==, 0);
357786
+    g_assert_cmpint(c->quiesce_counter, ==, 1);
357786
+
357786
+    bdrv_subtree_drained_end(parent_b);
357786
+
357786
+    bdrv_unref(parent_b);
357786
+    blk_unref(blk);
357786
+
357786
+    /* XXX Once bdrv_close() unref's children instead of just detaching them,
357786
+     * this won't be necessary any more. */
357786
+    bdrv_unref(a);
357786
+    bdrv_unref(a);
357786
+    bdrv_unref(c);
357786
+
357786
+    g_assert_cmpint(a->refcnt, ==, 1);
357786
+    g_assert_cmpint(b->refcnt, ==, 1);
357786
+    g_assert_cmpint(c->refcnt, ==, 1);
357786
+    bdrv_unref(a);
357786
+    bdrv_unref(b);
357786
+    bdrv_unref(c);
357786
+}
357786
+
357786
+
357786
 int main(int argc, char **argv)
357786
 {
357786
     int ret;
357786
@@ -1027,6 +1156,7 @@ int main(int argc, char **argv)
357786
     g_test_add_func("/bdrv-drain/deletion/drain", test_delete_by_drain);
357786
     g_test_add_func("/bdrv-drain/detach/drain", test_detach_by_drain);
357786
     g_test_add_func("/bdrv-drain/detach/drain_subtree", test_detach_by_drain_subtree);
357786
+    g_test_add_func("/bdrv-drain/detach/parent_cb", test_detach_by_parent_cb);
357786
 
357786
     ret = g_test_run();
357786
     qemu_event_destroy(&done_event);
357786
-- 
357786
1.8.3.1
357786