thebeanogamer / rpms / qemu-kvm

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