Blame SOURCES/kvm-test-bdrv-drain-Add-test-for-node-deletion.patch

357786
From e830be5d2de1d07f54b741f7a6e8bdea357bf044 Mon Sep 17 00:00:00 2001
357786
From: Kevin Wolf <kwolf@redhat.com>
357786
Date: Fri, 14 Sep 2018 10:55:07 +0200
357786
Subject: [PATCH 16/49] test-bdrv-drain: Add test for node deletion
357786
357786
RH-Author: Kevin Wolf <kwolf@redhat.com>
357786
Message-id: <20180914105540.18077-10-kwolf@redhat.com>
357786
Patchwork-id: 82162
357786
O-Subject: [RHV-7.6 qemu-kvm-rhev PATCH 09/42] test-bdrv-drain: Add test for node deletion
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
From: Max Reitz <mreitz@redhat.com>
357786
357786
This patch adds two bdrv-drain tests for what happens if some BDS goes
357786
away during the drainage.
357786
357786
The basic idea is that you have a parent BDS with some child nodes.
357786
Then, you drain one of the children.  Because of that, the party who
357786
actually owns the parent decides to (A) delete it, or (B) detach all its
357786
children from it -- both while the child is still being drained.
357786
357786
A real-world case where this can happen is the mirror block job, which
357786
may exit if you drain one of its children.
357786
357786
Signed-off-by: Max Reitz <mreitz@redhat.com>
357786
Signed-off-by: Kevin Wolf <kwolf@redhat.com>
357786
(cherry picked from commit 4c8158e359d194394c64acd21caf5e3f3f3141c2)
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 | 169 ++++++++++++++++++++++++++++++++++++++++++++++++
357786
 1 file changed, 169 insertions(+)
357786
357786
diff --git a/tests/test-bdrv-drain.c b/tests/test-bdrv-drain.c
357786
index 49786ea..8918a94 100644
357786
--- a/tests/test-bdrv-drain.c
357786
+++ b/tests/test-bdrv-drain.c
357786
@@ -792,6 +792,172 @@ static void test_blockjob_drain_subtree(void)
357786
     test_blockjob_common(BDRV_SUBTREE_DRAIN);
357786
 }
357786
 
357786
+
357786
+typedef struct BDRVTestTopState {
357786
+    BdrvChild *wait_child;
357786
+} BDRVTestTopState;
357786
+
357786
+static void bdrv_test_top_close(BlockDriverState *bs)
357786
+{
357786
+    BdrvChild *c, *next_c;
357786
+    QLIST_FOREACH_SAFE(c, &bs->children, next, next_c) {
357786
+        bdrv_unref_child(bs, c);
357786
+    }
357786
+}
357786
+
357786
+static int coroutine_fn bdrv_test_top_co_preadv(BlockDriverState *bs,
357786
+                                                uint64_t offset, uint64_t bytes,
357786
+                                                QEMUIOVector *qiov, int flags)
357786
+{
357786
+    BDRVTestTopState *tts = bs->opaque;
357786
+    return bdrv_co_preadv(tts->wait_child, offset, bytes, qiov, flags);
357786
+}
357786
+
357786
+static BlockDriver bdrv_test_top_driver = {
357786
+    .format_name            = "test_top_driver",
357786
+    .instance_size          = sizeof(BDRVTestTopState),
357786
+
357786
+    .bdrv_close             = bdrv_test_top_close,
357786
+    .bdrv_co_preadv         = bdrv_test_top_co_preadv,
357786
+
357786
+    .bdrv_child_perm        = bdrv_format_default_perms,
357786
+};
357786
+
357786
+typedef struct TestCoDeleteByDrainData {
357786
+    BlockBackend *blk;
357786
+    bool detach_instead_of_delete;
357786
+    bool done;
357786
+} TestCoDeleteByDrainData;
357786
+
357786
+static void coroutine_fn test_co_delete_by_drain(void *opaque)
357786
+{
357786
+    TestCoDeleteByDrainData *dbdd = opaque;
357786
+    BlockBackend *blk = dbdd->blk;
357786
+    BlockDriverState *bs = blk_bs(blk);
357786
+    BDRVTestTopState *tts = bs->opaque;
357786
+    void *buffer = g_malloc(65536);
357786
+    QEMUIOVector qiov;
357786
+    struct iovec iov = {
357786
+        .iov_base = buffer,
357786
+        .iov_len  = 65536,
357786
+    };
357786
+
357786
+    qemu_iovec_init_external(&qiov, &iov, 1);
357786
+
357786
+    /* Pretend some internal write operation from parent to child.
357786
+     * Important: We have to read from the child, not from the parent!
357786
+     * Draining works by first propagating it all up the tree to the
357786
+     * root and then waiting for drainage from root to the leaves
357786
+     * (protocol nodes).  If we have a request waiting on the root,
357786
+     * everything will be drained before we go back down the tree, but
357786
+     * we do not want that.  We want to be in the middle of draining
357786
+     * when this following requests returns. */
357786
+    bdrv_co_preadv(tts->wait_child, 0, 65536, &qiov, 0);
357786
+
357786
+    g_assert_cmpint(bs->refcnt, ==, 1);
357786
+
357786
+    if (!dbdd->detach_instead_of_delete) {
357786
+        blk_unref(blk);
357786
+    } else {
357786
+        BdrvChild *c, *next_c;
357786
+        QLIST_FOREACH_SAFE(c, &bs->children, next, next_c) {
357786
+            bdrv_unref_child(bs, c);
357786
+        }
357786
+    }
357786
+
357786
+    dbdd->done = true;
357786
+}
357786
+
357786
+/**
357786
+ * Test what happens when some BDS has some children, you drain one of
357786
+ * them and this results in the BDS being deleted.
357786
+ *
357786
+ * If @detach_instead_of_delete is set, the BDS is not going to be
357786
+ * deleted but will only detach all of its children.
357786
+ */
357786
+static void do_test_delete_by_drain(bool detach_instead_of_delete)
357786
+{
357786
+    BlockBackend *blk;
357786
+    BlockDriverState *bs, *child_bs, *null_bs;
357786
+    BDRVTestTopState *tts;
357786
+    TestCoDeleteByDrainData dbdd;
357786
+    Coroutine *co;
357786
+
357786
+    bs = bdrv_new_open_driver(&bdrv_test_top_driver, "top", BDRV_O_RDWR,
357786
+                              &error_abort);
357786
+    bs->total_sectors = 65536 >> BDRV_SECTOR_BITS;
357786
+    tts = bs->opaque;
357786
+
357786
+    null_bs = bdrv_open("null-co://", NULL, NULL, BDRV_O_RDWR | BDRV_O_PROTOCOL,
357786
+                        &error_abort);
357786
+    bdrv_attach_child(bs, null_bs, "null-child", &child_file, &error_abort);
357786
+
357786
+    /* This child will be the one to pass to requests through to, and
357786
+     * it will stall until a drain occurs */
357786
+    child_bs = bdrv_new_open_driver(&bdrv_test, "child", BDRV_O_RDWR,
357786
+                                    &error_abort);
357786
+    child_bs->total_sectors = 65536 >> BDRV_SECTOR_BITS;
357786
+    /* Takes our reference to child_bs */
357786
+    tts->wait_child = bdrv_attach_child(bs, child_bs, "wait-child", &child_file,
357786
+                                        &error_abort);
357786
+
357786
+    /* This child is just there to be deleted
357786
+     * (for detach_instead_of_delete == true) */
357786
+    null_bs = bdrv_open("null-co://", NULL, NULL, BDRV_O_RDWR | BDRV_O_PROTOCOL,
357786
+                        &error_abort);
357786
+    bdrv_attach_child(bs, null_bs, "null-child", &child_file, &error_abort);
357786
+
357786
+    blk = blk_new(BLK_PERM_ALL, BLK_PERM_ALL);
357786
+    blk_insert_bs(blk, bs, &error_abort);
357786
+
357786
+    /* Referenced by blk now */
357786
+    bdrv_unref(bs);
357786
+
357786
+    g_assert_cmpint(bs->refcnt, ==, 1);
357786
+    g_assert_cmpint(child_bs->refcnt, ==, 1);
357786
+    g_assert_cmpint(null_bs->refcnt, ==, 1);
357786
+
357786
+
357786
+    dbdd = (TestCoDeleteByDrainData){
357786
+        .blk = blk,
357786
+        .detach_instead_of_delete = detach_instead_of_delete,
357786
+        .done = false,
357786
+    };
357786
+    co = qemu_coroutine_create(test_co_delete_by_drain, &dbdd);
357786
+    qemu_coroutine_enter(co);
357786
+
357786
+    /* Drain the child while the read operation is still pending.
357786
+     * This should result in the operation finishing and
357786
+     * test_co_delete_by_drain() resuming.  Thus, @bs will be deleted
357786
+     * and the coroutine will exit while this drain operation is still
357786
+     * in progress. */
357786
+    bdrv_ref(child_bs);
357786
+    bdrv_drain(child_bs);
357786
+    bdrv_unref(child_bs);
357786
+
357786
+    while (!dbdd.done) {
357786
+        aio_poll(qemu_get_aio_context(), true);
357786
+    }
357786
+
357786
+    if (detach_instead_of_delete) {
357786
+        /* Here, the reference has not passed over to the coroutine,
357786
+         * so we have to delete the BB ourselves */
357786
+        blk_unref(blk);
357786
+    }
357786
+}
357786
+
357786
+
357786
+static void test_delete_by_drain(void)
357786
+{
357786
+    do_test_delete_by_drain(false);
357786
+}
357786
+
357786
+static void test_detach_by_drain(void)
357786
+{
357786
+    do_test_delete_by_drain(true);
357786
+}
357786
+
357786
+
357786
 int main(int argc, char **argv)
357786
 {
357786
     int ret;
357786
@@ -839,6 +1005,9 @@ int main(int argc, char **argv)
357786
     g_test_add_func("/bdrv-drain/blockjob/drain_subtree",
357786
                     test_blockjob_drain_subtree);
357786
 
357786
+    g_test_add_func("/bdrv-drain/deletion", test_delete_by_drain);
357786
+    g_test_add_func("/bdrv-drain/detach", test_detach_by_drain);
357786
+
357786
     ret = g_test_run();
357786
     qemu_event_destroy(&done_event);
357786
     return ret;
357786
-- 
357786
1.8.3.1
357786