thebeanogamer / rpms / qemu-kvm

Forked from rpms/qemu-kvm 5 months ago
Clone

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

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