xref: /qemu/tests/unit/test-bdrv-drain.c (revision e2dd273754eb9a47c33660b4e14074e8e96ada4d)
1881cfd17SKevin Wolf /*
2881cfd17SKevin Wolf  * Block node draining tests
3881cfd17SKevin Wolf  *
4881cfd17SKevin Wolf  * Copyright (c) 2017 Kevin Wolf <kwolf@redhat.com>
5881cfd17SKevin Wolf  *
6881cfd17SKevin Wolf  * Permission is hereby granted, free of charge, to any person obtaining a copy
7881cfd17SKevin Wolf  * of this software and associated documentation files (the "Software"), to deal
8881cfd17SKevin Wolf  * in the Software without restriction, including without limitation the rights
9881cfd17SKevin Wolf  * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
10881cfd17SKevin Wolf  * copies of the Software, and to permit persons to whom the Software is
11881cfd17SKevin Wolf  * furnished to do so, subject to the following conditions:
12881cfd17SKevin Wolf  *
13881cfd17SKevin Wolf  * The above copyright notice and this permission notice shall be included in
14881cfd17SKevin Wolf  * all copies or substantial portions of the Software.
15881cfd17SKevin Wolf  *
16881cfd17SKevin Wolf  * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
17881cfd17SKevin Wolf  * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
18881cfd17SKevin Wolf  * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
19881cfd17SKevin Wolf  * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
20881cfd17SKevin Wolf  * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
21881cfd17SKevin Wolf  * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
22881cfd17SKevin Wolf  * THE SOFTWARE.
23881cfd17SKevin Wolf  */
24881cfd17SKevin Wolf 
25881cfd17SKevin Wolf #include "qemu/osdep.h"
26e2c1c34fSMarkus Armbruster #include "block/block_int.h"
277253220dSKevin Wolf #include "block/blockjob_int.h"
28881cfd17SKevin Wolf #include "sysemu/block-backend.h"
29881cfd17SKevin Wolf #include "qapi/error.h"
30db725815SMarkus Armbruster #include "qemu/main-loop.h"
31bb675689SKevin Wolf #include "iothread.h"
32bb675689SKevin Wolf 
33bb675689SKevin Wolf static QemuEvent done_event;
34881cfd17SKevin Wolf 
35881cfd17SKevin Wolf typedef struct BDRVTestState {
36881cfd17SKevin Wolf     int drain_count;
37bb675689SKevin Wolf     AioContext *bh_indirection_ctx;
3857320ca9SKevin Wolf     bool sleep_in_drain_begin;
39881cfd17SKevin Wolf } BDRVTestState;
40881cfd17SKevin Wolf 
417bce1c29SKevin Wolf static void coroutine_fn sleep_in_drain_begin(void *opaque)
427bce1c29SKevin Wolf {
437bce1c29SKevin Wolf     BlockDriverState *bs = opaque;
447bce1c29SKevin Wolf 
457bce1c29SKevin Wolf     qemu_co_sleep_ns(QEMU_CLOCK_REALTIME, 100000);
467bce1c29SKevin Wolf     bdrv_dec_in_flight(bs);
477bce1c29SKevin Wolf }
487bce1c29SKevin Wolf 
495e8ac217SKevin Wolf static void bdrv_test_drain_begin(BlockDriverState *bs)
50881cfd17SKevin Wolf {
51881cfd17SKevin Wolf     BDRVTestState *s = bs->opaque;
52881cfd17SKevin Wolf     s->drain_count++;
5357320ca9SKevin Wolf     if (s->sleep_in_drain_begin) {
547bce1c29SKevin Wolf         Coroutine *co = qemu_coroutine_create(sleep_in_drain_begin, bs);
557bce1c29SKevin Wolf         bdrv_inc_in_flight(bs);
567bce1c29SKevin Wolf         aio_co_enter(bdrv_get_aio_context(bs), co);
5757320ca9SKevin Wolf     }
58881cfd17SKevin Wolf }
59881cfd17SKevin Wolf 
605e8ac217SKevin Wolf static void bdrv_test_drain_end(BlockDriverState *bs)
61881cfd17SKevin Wolf {
62881cfd17SKevin Wolf     BDRVTestState *s = bs->opaque;
63881cfd17SKevin Wolf     s->drain_count--;
64881cfd17SKevin Wolf }
65881cfd17SKevin Wolf 
66881cfd17SKevin Wolf static void bdrv_test_close(BlockDriverState *bs)
67881cfd17SKevin Wolf {
68881cfd17SKevin Wolf     BDRVTestState *s = bs->opaque;
69881cfd17SKevin Wolf     g_assert_cmpint(s->drain_count, >, 0);
70881cfd17SKevin Wolf }
71881cfd17SKevin Wolf 
72bb675689SKevin Wolf static void co_reenter_bh(void *opaque)
73bb675689SKevin Wolf {
74bb675689SKevin Wolf     aio_co_wake(opaque);
75bb675689SKevin Wolf }
76bb675689SKevin Wolf 
77881cfd17SKevin Wolf static int coroutine_fn bdrv_test_co_preadv(BlockDriverState *bs,
78f7ef38ddSVladimir Sementsov-Ogievskiy                                             int64_t offset, int64_t bytes,
79f7ef38ddSVladimir Sementsov-Ogievskiy                                             QEMUIOVector *qiov,
80f7ef38ddSVladimir Sementsov-Ogievskiy                                             BdrvRequestFlags flags)
81881cfd17SKevin Wolf {
82bb675689SKevin Wolf     BDRVTestState *s = bs->opaque;
83bb675689SKevin Wolf 
84881cfd17SKevin Wolf     /* We want this request to stay until the polling loop in drain waits for
85881cfd17SKevin Wolf      * it to complete. We need to sleep a while as bdrv_drain_invoke() comes
86881cfd17SKevin Wolf      * first and polls its result, too, but it shouldn't accidentally complete
87881cfd17SKevin Wolf      * this request yet. */
88881cfd17SKevin Wolf     qemu_co_sleep_ns(QEMU_CLOCK_REALTIME, 100000);
89881cfd17SKevin Wolf 
90bb675689SKevin Wolf     if (s->bh_indirection_ctx) {
91bb675689SKevin Wolf         aio_bh_schedule_oneshot(s->bh_indirection_ctx, co_reenter_bh,
92bb675689SKevin Wolf                                 qemu_coroutine_self());
93bb675689SKevin Wolf         qemu_coroutine_yield();
94bb675689SKevin Wolf     }
95bb675689SKevin Wolf 
96881cfd17SKevin Wolf     return 0;
97881cfd17SKevin Wolf }
98881cfd17SKevin Wolf 
99*e2dd2737SKevin Wolf static int bdrv_test_co_change_backing_file(BlockDriverState *bs,
1009746b35cSMax Reitz                                             const char *backing_file,
1019746b35cSMax Reitz                                             const char *backing_fmt)
1029746b35cSMax Reitz {
1039746b35cSMax Reitz     return 0;
1049746b35cSMax Reitz }
1059746b35cSMax Reitz 
106881cfd17SKevin Wolf static BlockDriver bdrv_test = {
107881cfd17SKevin Wolf     .format_name            = "test",
108881cfd17SKevin Wolf     .instance_size          = sizeof(BDRVTestState),
10925f78d9eSVladimir Sementsov-Ogievskiy     .supports_backing       = true,
110881cfd17SKevin Wolf 
111881cfd17SKevin Wolf     .bdrv_close             = bdrv_test_close,
112881cfd17SKevin Wolf     .bdrv_co_preadv         = bdrv_test_co_preadv,
113881cfd17SKevin Wolf 
1145e8ac217SKevin Wolf     .bdrv_drain_begin       = bdrv_test_drain_begin,
1155e8ac217SKevin Wolf     .bdrv_drain_end         = bdrv_test_drain_end,
11686e1c840SKevin Wolf 
117e5d8a406SMax Reitz     .bdrv_child_perm        = bdrv_default_perms,
1189746b35cSMax Reitz 
119*e2dd2737SKevin Wolf     .bdrv_co_change_backing_file = bdrv_test_co_change_backing_file,
120881cfd17SKevin Wolf };
121881cfd17SKevin Wolf 
122881cfd17SKevin Wolf static void aio_ret_cb(void *opaque, int ret)
123881cfd17SKevin Wolf {
124881cfd17SKevin Wolf     int *aio_ret = opaque;
125881cfd17SKevin Wolf     *aio_ret = ret;
126881cfd17SKevin Wolf }
127881cfd17SKevin Wolf 
1280582eb10SKevin Wolf typedef struct CallInCoroutineData {
1290582eb10SKevin Wolf     void (*entry)(void);
1300582eb10SKevin Wolf     bool done;
1310582eb10SKevin Wolf } CallInCoroutineData;
1320582eb10SKevin Wolf 
1330582eb10SKevin Wolf static coroutine_fn void call_in_coroutine_entry(void *opaque)
1340582eb10SKevin Wolf {
1350582eb10SKevin Wolf     CallInCoroutineData *data = opaque;
1360582eb10SKevin Wolf 
1370582eb10SKevin Wolf     data->entry();
1380582eb10SKevin Wolf     data->done = true;
1390582eb10SKevin Wolf }
1400582eb10SKevin Wolf 
1410582eb10SKevin Wolf static void call_in_coroutine(void (*entry)(void))
1420582eb10SKevin Wolf {
1430582eb10SKevin Wolf     Coroutine *co;
1440582eb10SKevin Wolf     CallInCoroutineData data = {
1450582eb10SKevin Wolf         .entry  = entry,
1460582eb10SKevin Wolf         .done   = false,
1470582eb10SKevin Wolf     };
1480582eb10SKevin Wolf 
1490582eb10SKevin Wolf     co = qemu_coroutine_create(call_in_coroutine_entry, &data);
1500582eb10SKevin Wolf     qemu_coroutine_enter(co);
1510582eb10SKevin Wolf     while (!data.done) {
1520582eb10SKevin Wolf         aio_poll(qemu_get_aio_context(), true);
1530582eb10SKevin Wolf     }
1540582eb10SKevin Wolf }
1550582eb10SKevin Wolf 
15686e1c840SKevin Wolf enum drain_type {
15786e1c840SKevin Wolf     BDRV_DRAIN_ALL,
15886e1c840SKevin Wolf     BDRV_DRAIN,
1596c429a6aSKevin Wolf     DRAIN_TYPE_MAX,
16086e1c840SKevin Wolf };
16186e1c840SKevin Wolf 
16286e1c840SKevin Wolf static void do_drain_begin(enum drain_type drain_type, BlockDriverState *bs)
16386e1c840SKevin Wolf {
16486e1c840SKevin Wolf     switch (drain_type) {
16586e1c840SKevin Wolf     case BDRV_DRAIN_ALL:        bdrv_drain_all_begin(); break;
16686e1c840SKevin Wolf     case BDRV_DRAIN:            bdrv_drained_begin(bs); break;
16786e1c840SKevin Wolf     default:                    g_assert_not_reached();
16886e1c840SKevin Wolf     }
16986e1c840SKevin Wolf }
17086e1c840SKevin Wolf 
17186e1c840SKevin Wolf static void do_drain_end(enum drain_type drain_type, BlockDriverState *bs)
17286e1c840SKevin Wolf {
17386e1c840SKevin Wolf     switch (drain_type) {
17486e1c840SKevin Wolf     case BDRV_DRAIN_ALL:        bdrv_drain_all_end(); break;
17586e1c840SKevin Wolf     case BDRV_DRAIN:            bdrv_drained_end(bs); break;
17686e1c840SKevin Wolf     default:                    g_assert_not_reached();
17786e1c840SKevin Wolf     }
17886e1c840SKevin Wolf }
17986e1c840SKevin Wolf 
180f62c1729SKevin Wolf static void do_drain_begin_unlocked(enum drain_type drain_type, BlockDriverState *bs)
181f62c1729SKevin Wolf {
182f62c1729SKevin Wolf     if (drain_type != BDRV_DRAIN_ALL) {
183f62c1729SKevin Wolf         aio_context_acquire(bdrv_get_aio_context(bs));
184f62c1729SKevin Wolf     }
185f62c1729SKevin Wolf     do_drain_begin(drain_type, bs);
186f62c1729SKevin Wolf     if (drain_type != BDRV_DRAIN_ALL) {
187f62c1729SKevin Wolf         aio_context_release(bdrv_get_aio_context(bs));
188f62c1729SKevin Wolf     }
189f62c1729SKevin Wolf }
190f62c1729SKevin Wolf 
19157f3d07bSKevin Wolf static BlockBackend * no_coroutine_fn test_setup(void)
19257f3d07bSKevin Wolf {
19357f3d07bSKevin Wolf     BlockBackend *blk;
19457f3d07bSKevin Wolf     BlockDriverState *bs, *backing;
19557f3d07bSKevin Wolf 
19657f3d07bSKevin Wolf     blk = blk_new(qemu_get_aio_context(), BLK_PERM_ALL, BLK_PERM_ALL);
19757f3d07bSKevin Wolf     bs = bdrv_new_open_driver(&bdrv_test, "test-node", BDRV_O_RDWR,
19857f3d07bSKevin Wolf                               &error_abort);
19957f3d07bSKevin Wolf     blk_insert_bs(blk, bs, &error_abort);
20057f3d07bSKevin Wolf 
20157f3d07bSKevin Wolf     backing = bdrv_new_open_driver(&bdrv_test, "backing", 0, &error_abort);
20257f3d07bSKevin Wolf     bdrv_set_backing_hd(bs, backing, &error_abort);
20357f3d07bSKevin Wolf 
20457f3d07bSKevin Wolf     bdrv_unref(backing);
20557f3d07bSKevin Wolf     bdrv_unref(bs);
20657f3d07bSKevin Wolf 
20757f3d07bSKevin Wolf     return blk;
20857f3d07bSKevin Wolf }
20957f3d07bSKevin Wolf 
210f62c1729SKevin Wolf static void do_drain_end_unlocked(enum drain_type drain_type, BlockDriverState *bs)
211f62c1729SKevin Wolf {
212f62c1729SKevin Wolf     if (drain_type != BDRV_DRAIN_ALL) {
213f62c1729SKevin Wolf         aio_context_acquire(bdrv_get_aio_context(bs));
214f62c1729SKevin Wolf     }
215f62c1729SKevin Wolf     do_drain_end(drain_type, bs);
216f62c1729SKevin Wolf     if (drain_type != BDRV_DRAIN_ALL) {
217f62c1729SKevin Wolf         aio_context_release(bdrv_get_aio_context(bs));
218f62c1729SKevin Wolf     }
219f62c1729SKevin Wolf }
220f62c1729SKevin Wolf 
221004915a9SKevin Wolf /*
222004915a9SKevin Wolf  * Locking the block graph would be a bit cumbersome here because this function
223004915a9SKevin Wolf  * is called both in coroutine and non-coroutine context. We know this is a test
224004915a9SKevin Wolf  * and nothing else is running, so don't bother with TSA.
225004915a9SKevin Wolf  */
226004915a9SKevin Wolf static void coroutine_mixed_fn TSA_NO_TSA
227004915a9SKevin Wolf test_drv_cb_common(BlockBackend *blk, enum drain_type drain_type,
22857f3d07bSKevin Wolf                    bool recursive)
229881cfd17SKevin Wolf {
23057f3d07bSKevin Wolf     BlockDriverState *bs = blk_bs(blk);
23157f3d07bSKevin Wolf     BlockDriverState *backing = bs->backing->bs;
23286e1c840SKevin Wolf     BDRVTestState *s, *backing_s;
233881cfd17SKevin Wolf     BlockAIOCB *acb;
234881cfd17SKevin Wolf     int aio_ret;
235881cfd17SKevin Wolf 
236405d8fe0SVladimir Sementsov-Ogievskiy     QEMUIOVector qiov = QEMU_IOVEC_INIT_BUF(qiov, NULL, 0);
237881cfd17SKevin Wolf 
238881cfd17SKevin Wolf     s = bs->opaque;
23986e1c840SKevin Wolf     backing_s = backing->opaque;
24086e1c840SKevin Wolf 
241881cfd17SKevin Wolf     /* Simple bdrv_drain_all_begin/end pair, check that CBs are called */
242881cfd17SKevin Wolf     g_assert_cmpint(s->drain_count, ==, 0);
24386e1c840SKevin Wolf     g_assert_cmpint(backing_s->drain_count, ==, 0);
24486e1c840SKevin Wolf 
24586e1c840SKevin Wolf     do_drain_begin(drain_type, bs);
24686e1c840SKevin Wolf 
247881cfd17SKevin Wolf     g_assert_cmpint(s->drain_count, ==, 1);
24886e1c840SKevin Wolf     g_assert_cmpint(backing_s->drain_count, ==, !!recursive);
24986e1c840SKevin Wolf 
25086e1c840SKevin Wolf     do_drain_end(drain_type, bs);
25186e1c840SKevin Wolf 
252881cfd17SKevin Wolf     g_assert_cmpint(s->drain_count, ==, 0);
25386e1c840SKevin Wolf     g_assert_cmpint(backing_s->drain_count, ==, 0);
254881cfd17SKevin Wolf 
255881cfd17SKevin Wolf     /* Now do the same while a request is pending */
256881cfd17SKevin Wolf     aio_ret = -EINPROGRESS;
257881cfd17SKevin Wolf     acb = blk_aio_preadv(blk, 0, &qiov, 0, aio_ret_cb, &aio_ret);
258881cfd17SKevin Wolf     g_assert(acb != NULL);
259881cfd17SKevin Wolf     g_assert_cmpint(aio_ret, ==, -EINPROGRESS);
260881cfd17SKevin Wolf 
261881cfd17SKevin Wolf     g_assert_cmpint(s->drain_count, ==, 0);
26286e1c840SKevin Wolf     g_assert_cmpint(backing_s->drain_count, ==, 0);
26386e1c840SKevin Wolf 
26486e1c840SKevin Wolf     do_drain_begin(drain_type, bs);
26586e1c840SKevin Wolf 
266881cfd17SKevin Wolf     g_assert_cmpint(aio_ret, ==, 0);
267881cfd17SKevin Wolf     g_assert_cmpint(s->drain_count, ==, 1);
26886e1c840SKevin Wolf     g_assert_cmpint(backing_s->drain_count, ==, !!recursive);
269881cfd17SKevin Wolf 
27086e1c840SKevin Wolf     do_drain_end(drain_type, bs);
27186e1c840SKevin Wolf 
27286e1c840SKevin Wolf     g_assert_cmpint(s->drain_count, ==, 0);
27386e1c840SKevin Wolf     g_assert_cmpint(backing_s->drain_count, ==, 0);
274881cfd17SKevin Wolf }
275881cfd17SKevin Wolf 
27686e1c840SKevin Wolf static void test_drv_cb_drain_all(void)
27786e1c840SKevin Wolf {
27857f3d07bSKevin Wolf     BlockBackend *blk = test_setup();
27957f3d07bSKevin Wolf     test_drv_cb_common(blk, BDRV_DRAIN_ALL, true);
28057f3d07bSKevin Wolf     blk_unref(blk);
28186e1c840SKevin Wolf }
28286e1c840SKevin Wolf 
28386e1c840SKevin Wolf static void test_drv_cb_drain(void)
28486e1c840SKevin Wolf {
28557f3d07bSKevin Wolf     BlockBackend *blk = test_setup();
28657f3d07bSKevin Wolf     test_drv_cb_common(blk, BDRV_DRAIN, false);
28757f3d07bSKevin Wolf     blk_unref(blk);
28857f3d07bSKevin Wolf }
28957f3d07bSKevin Wolf 
29057f3d07bSKevin Wolf static void coroutine_fn test_drv_cb_co_drain_all_entry(void)
29157f3d07bSKevin Wolf {
29257f3d07bSKevin Wolf     BlockBackend *blk = blk_all_next(NULL);
29357f3d07bSKevin Wolf     test_drv_cb_common(blk, BDRV_DRAIN_ALL, true);
29486e1c840SKevin Wolf }
29586e1c840SKevin Wolf 
2966d0252f2SKevin Wolf static void test_drv_cb_co_drain_all(void)
2976d0252f2SKevin Wolf {
29857f3d07bSKevin Wolf     BlockBackend *blk = test_setup();
29957f3d07bSKevin Wolf     call_in_coroutine(test_drv_cb_co_drain_all_entry);
30057f3d07bSKevin Wolf     blk_unref(blk);
30157f3d07bSKevin Wolf }
30257f3d07bSKevin Wolf 
30357f3d07bSKevin Wolf static void coroutine_fn test_drv_cb_co_drain_entry(void)
30457f3d07bSKevin Wolf {
30557f3d07bSKevin Wolf     BlockBackend *blk = blk_all_next(NULL);
30657f3d07bSKevin Wolf     test_drv_cb_common(blk, BDRV_DRAIN, false);
3076d0252f2SKevin Wolf }
3086d0252f2SKevin Wolf 
3090582eb10SKevin Wolf static void test_drv_cb_co_drain(void)
3100582eb10SKevin Wolf {
31157f3d07bSKevin Wolf     BlockBackend *blk = test_setup();
31257f3d07bSKevin Wolf     call_in_coroutine(test_drv_cb_co_drain_entry);
31357f3d07bSKevin Wolf     blk_unref(blk);
3140582eb10SKevin Wolf }
3150582eb10SKevin Wolf 
316004915a9SKevin Wolf /*
317004915a9SKevin Wolf  * Locking the block graph would be a bit cumbersome here because this function
318004915a9SKevin Wolf  * is called both in coroutine and non-coroutine context. We know this is a test
319004915a9SKevin Wolf  * and nothing else is running, so don't bother with TSA.
320004915a9SKevin Wolf  */
321004915a9SKevin Wolf static void coroutine_mixed_fn TSA_NO_TSA
322004915a9SKevin Wolf test_quiesce_common(BlockBackend *blk, enum drain_type drain_type,
32357f3d07bSKevin Wolf                     bool recursive)
32489a6ceabSKevin Wolf {
32557f3d07bSKevin Wolf     BlockDriverState *bs = blk_bs(blk);
32657f3d07bSKevin Wolf     BlockDriverState *backing = bs->backing->bs;
32789a6ceabSKevin Wolf 
32889a6ceabSKevin Wolf     g_assert_cmpint(bs->quiesce_counter, ==, 0);
32989a6ceabSKevin Wolf     g_assert_cmpint(backing->quiesce_counter, ==, 0);
33089a6ceabSKevin Wolf 
33189a6ceabSKevin Wolf     do_drain_begin(drain_type, bs);
33289a6ceabSKevin Wolf 
33357e05be3SKevin Wolf     if (drain_type == BDRV_DRAIN_ALL) {
33457e05be3SKevin Wolf         g_assert_cmpint(bs->quiesce_counter, ==, 2);
33557e05be3SKevin Wolf     } else {
33689a6ceabSKevin Wolf         g_assert_cmpint(bs->quiesce_counter, ==, 1);
33757e05be3SKevin Wolf     }
33889a6ceabSKevin Wolf     g_assert_cmpint(backing->quiesce_counter, ==, !!recursive);
33989a6ceabSKevin Wolf 
34089a6ceabSKevin Wolf     do_drain_end(drain_type, bs);
34189a6ceabSKevin Wolf 
34289a6ceabSKevin Wolf     g_assert_cmpint(bs->quiesce_counter, ==, 0);
34389a6ceabSKevin Wolf     g_assert_cmpint(backing->quiesce_counter, ==, 0);
34489a6ceabSKevin Wolf }
34589a6ceabSKevin Wolf 
34689a6ceabSKevin Wolf static void test_quiesce_drain_all(void)
34789a6ceabSKevin Wolf {
34857f3d07bSKevin Wolf     BlockBackend *blk = test_setup();
34957f3d07bSKevin Wolf     test_quiesce_common(blk, BDRV_DRAIN_ALL, true);
35057f3d07bSKevin Wolf     blk_unref(blk);
35189a6ceabSKevin Wolf }
35289a6ceabSKevin Wolf 
35389a6ceabSKevin Wolf static void test_quiesce_drain(void)
35489a6ceabSKevin Wolf {
35557f3d07bSKevin Wolf     BlockBackend *blk = test_setup();
35657f3d07bSKevin Wolf     test_quiesce_common(blk, BDRV_DRAIN, false);
35757f3d07bSKevin Wolf     blk_unref(blk);
35857f3d07bSKevin Wolf }
35957f3d07bSKevin Wolf 
36057f3d07bSKevin Wolf static void coroutine_fn test_quiesce_co_drain_all_entry(void)
36157f3d07bSKevin Wolf {
36257f3d07bSKevin Wolf     BlockBackend *blk = blk_all_next(NULL);
36357f3d07bSKevin Wolf     test_quiesce_common(blk, BDRV_DRAIN_ALL, true);
36489a6ceabSKevin Wolf }
36589a6ceabSKevin Wolf 
3666d0252f2SKevin Wolf static void test_quiesce_co_drain_all(void)
3676d0252f2SKevin Wolf {
36857f3d07bSKevin Wolf     BlockBackend *blk = test_setup();
36957f3d07bSKevin Wolf     call_in_coroutine(test_quiesce_co_drain_all_entry);
37057f3d07bSKevin Wolf     blk_unref(blk);
37157f3d07bSKevin Wolf }
37257f3d07bSKevin Wolf 
37357f3d07bSKevin Wolf static void coroutine_fn test_quiesce_co_drain_entry(void)
37457f3d07bSKevin Wolf {
37557f3d07bSKevin Wolf     BlockBackend *blk = blk_all_next(NULL);
37657f3d07bSKevin Wolf     test_quiesce_common(blk, BDRV_DRAIN, false);
3776d0252f2SKevin Wolf }
3786d0252f2SKevin Wolf 
3790582eb10SKevin Wolf static void test_quiesce_co_drain(void)
3800582eb10SKevin Wolf {
38157f3d07bSKevin Wolf     BlockBackend *blk = test_setup();
38257f3d07bSKevin Wolf     call_in_coroutine(test_quiesce_co_drain_entry);
38357f3d07bSKevin Wolf     blk_unref(blk);
3840582eb10SKevin Wolf }
3850582eb10SKevin Wolf 
3866c429a6aSKevin Wolf static void test_nested(void)
3876c429a6aSKevin Wolf {
3886c429a6aSKevin Wolf     BlockBackend *blk;
3896c429a6aSKevin Wolf     BlockDriverState *bs, *backing;
3906c429a6aSKevin Wolf     BDRVTestState *s, *backing_s;
3916c429a6aSKevin Wolf     enum drain_type outer, inner;
3926c429a6aSKevin Wolf 
393d861ab3aSKevin Wolf     blk = blk_new(qemu_get_aio_context(), BLK_PERM_ALL, BLK_PERM_ALL);
3946c429a6aSKevin Wolf     bs = bdrv_new_open_driver(&bdrv_test, "test-node", BDRV_O_RDWR,
3956c429a6aSKevin Wolf                               &error_abort);
3966c429a6aSKevin Wolf     s = bs->opaque;
3976c429a6aSKevin Wolf     blk_insert_bs(blk, bs, &error_abort);
3986c429a6aSKevin Wolf 
3996c429a6aSKevin Wolf     backing = bdrv_new_open_driver(&bdrv_test, "backing", 0, &error_abort);
4006c429a6aSKevin Wolf     backing_s = backing->opaque;
4016c429a6aSKevin Wolf     bdrv_set_backing_hd(bs, backing, &error_abort);
4026c429a6aSKevin Wolf 
4036c429a6aSKevin Wolf     for (outer = 0; outer < DRAIN_TYPE_MAX; outer++) {
4046c429a6aSKevin Wolf         for (inner = 0; inner < DRAIN_TYPE_MAX; inner++) {
40557e05be3SKevin Wolf             int backing_quiesce = (outer == BDRV_DRAIN_ALL) +
40657e05be3SKevin Wolf                                   (inner == BDRV_DRAIN_ALL);
4076c429a6aSKevin Wolf 
4086c429a6aSKevin Wolf             g_assert_cmpint(bs->quiesce_counter, ==, 0);
4096c429a6aSKevin Wolf             g_assert_cmpint(backing->quiesce_counter, ==, 0);
4106c429a6aSKevin Wolf             g_assert_cmpint(s->drain_count, ==, 0);
4116c429a6aSKevin Wolf             g_assert_cmpint(backing_s->drain_count, ==, 0);
4126c429a6aSKevin Wolf 
4136c429a6aSKevin Wolf             do_drain_begin(outer, bs);
4146c429a6aSKevin Wolf             do_drain_begin(inner, bs);
4156c429a6aSKevin Wolf 
41657e05be3SKevin Wolf             g_assert_cmpint(bs->quiesce_counter, ==, 2 + !!backing_quiesce);
4176c429a6aSKevin Wolf             g_assert_cmpint(backing->quiesce_counter, ==, backing_quiesce);
41857e05be3SKevin Wolf             g_assert_cmpint(s->drain_count, ==, 1);
41957e05be3SKevin Wolf             g_assert_cmpint(backing_s->drain_count, ==, !!backing_quiesce);
4206c429a6aSKevin Wolf 
4216c429a6aSKevin Wolf             do_drain_end(inner, bs);
4226c429a6aSKevin Wolf             do_drain_end(outer, bs);
4236c429a6aSKevin Wolf 
4246c429a6aSKevin Wolf             g_assert_cmpint(bs->quiesce_counter, ==, 0);
4256c429a6aSKevin Wolf             g_assert_cmpint(backing->quiesce_counter, ==, 0);
4266c429a6aSKevin Wolf             g_assert_cmpint(s->drain_count, ==, 0);
4276c429a6aSKevin Wolf             g_assert_cmpint(backing_s->drain_count, ==, 0);
4286c429a6aSKevin Wolf         }
4296c429a6aSKevin Wolf     }
4306c429a6aSKevin Wolf 
4316c429a6aSKevin Wolf     bdrv_unref(backing);
4326c429a6aSKevin Wolf     bdrv_unref(bs);
4336c429a6aSKevin Wolf     blk_unref(blk);
4346c429a6aSKevin Wolf }
4356c429a6aSKevin Wolf 
43619f7a7e5SKevin Wolf static void test_graph_change_drain_all(void)
43719f7a7e5SKevin Wolf {
43819f7a7e5SKevin Wolf     BlockBackend *blk_a, *blk_b;
43919f7a7e5SKevin Wolf     BlockDriverState *bs_a, *bs_b;
44019f7a7e5SKevin Wolf     BDRVTestState *a_s, *b_s;
44119f7a7e5SKevin Wolf 
44219f7a7e5SKevin Wolf     /* Create node A with a BlockBackend */
443d861ab3aSKevin Wolf     blk_a = blk_new(qemu_get_aio_context(), BLK_PERM_ALL, BLK_PERM_ALL);
44419f7a7e5SKevin Wolf     bs_a = bdrv_new_open_driver(&bdrv_test, "test-node-a", BDRV_O_RDWR,
44519f7a7e5SKevin Wolf                                 &error_abort);
44619f7a7e5SKevin Wolf     a_s = bs_a->opaque;
44719f7a7e5SKevin Wolf     blk_insert_bs(blk_a, bs_a, &error_abort);
44819f7a7e5SKevin Wolf 
44919f7a7e5SKevin Wolf     g_assert_cmpint(bs_a->quiesce_counter, ==, 0);
45019f7a7e5SKevin Wolf     g_assert_cmpint(a_s->drain_count, ==, 0);
45119f7a7e5SKevin Wolf 
45219f7a7e5SKevin Wolf     /* Call bdrv_drain_all_begin() */
45319f7a7e5SKevin Wolf     bdrv_drain_all_begin();
45419f7a7e5SKevin Wolf 
45519f7a7e5SKevin Wolf     g_assert_cmpint(bs_a->quiesce_counter, ==, 1);
45619f7a7e5SKevin Wolf     g_assert_cmpint(a_s->drain_count, ==, 1);
45719f7a7e5SKevin Wolf 
45819f7a7e5SKevin Wolf     /* Create node B with a BlockBackend */
459d861ab3aSKevin Wolf     blk_b = blk_new(qemu_get_aio_context(), BLK_PERM_ALL, BLK_PERM_ALL);
46019f7a7e5SKevin Wolf     bs_b = bdrv_new_open_driver(&bdrv_test, "test-node-b", BDRV_O_RDWR,
46119f7a7e5SKevin Wolf                                 &error_abort);
46219f7a7e5SKevin Wolf     b_s = bs_b->opaque;
46319f7a7e5SKevin Wolf     blk_insert_bs(blk_b, bs_b, &error_abort);
46419f7a7e5SKevin Wolf 
46519f7a7e5SKevin Wolf     g_assert_cmpint(bs_a->quiesce_counter, ==, 1);
46619f7a7e5SKevin Wolf     g_assert_cmpint(bs_b->quiesce_counter, ==, 1);
46719f7a7e5SKevin Wolf     g_assert_cmpint(a_s->drain_count, ==, 1);
46819f7a7e5SKevin Wolf     g_assert_cmpint(b_s->drain_count, ==, 1);
46919f7a7e5SKevin Wolf 
47019f7a7e5SKevin Wolf     /* Unref and finally delete node A */
47119f7a7e5SKevin Wolf     blk_unref(blk_a);
47219f7a7e5SKevin Wolf 
47319f7a7e5SKevin Wolf     g_assert_cmpint(bs_a->quiesce_counter, ==, 1);
47419f7a7e5SKevin Wolf     g_assert_cmpint(bs_b->quiesce_counter, ==, 1);
47519f7a7e5SKevin Wolf     g_assert_cmpint(a_s->drain_count, ==, 1);
47619f7a7e5SKevin Wolf     g_assert_cmpint(b_s->drain_count, ==, 1);
47719f7a7e5SKevin Wolf 
47819f7a7e5SKevin Wolf     bdrv_unref(bs_a);
47919f7a7e5SKevin Wolf 
48019f7a7e5SKevin Wolf     g_assert_cmpint(bs_b->quiesce_counter, ==, 1);
48119f7a7e5SKevin Wolf     g_assert_cmpint(b_s->drain_count, ==, 1);
48219f7a7e5SKevin Wolf 
48319f7a7e5SKevin Wolf     /* End the drained section */
48419f7a7e5SKevin Wolf     bdrv_drain_all_end();
48519f7a7e5SKevin Wolf 
48619f7a7e5SKevin Wolf     g_assert_cmpint(bs_b->quiesce_counter, ==, 0);
48719f7a7e5SKevin Wolf     g_assert_cmpint(b_s->drain_count, ==, 0);
48819f7a7e5SKevin Wolf 
48919f7a7e5SKevin Wolf     bdrv_unref(bs_b);
49019f7a7e5SKevin Wolf     blk_unref(blk_b);
49119f7a7e5SKevin Wolf }
49219f7a7e5SKevin Wolf 
493bb675689SKevin Wolf struct test_iothread_data {
494bb675689SKevin Wolf     BlockDriverState *bs;
495bb675689SKevin Wolf     enum drain_type drain_type;
496bb675689SKevin Wolf     int *aio_ret;
497ab613350SStefan Hajnoczi     bool co_done;
498bb675689SKevin Wolf };
499bb675689SKevin Wolf 
500ab613350SStefan Hajnoczi static void coroutine_fn test_iothread_drain_co_entry(void *opaque)
501bb675689SKevin Wolf {
502bb675689SKevin Wolf     struct test_iothread_data *data = opaque;
503bb675689SKevin Wolf 
504bb675689SKevin Wolf     do_drain_begin(data->drain_type, data->bs);
505bb675689SKevin Wolf     g_assert_cmpint(*data->aio_ret, ==, 0);
506bb675689SKevin Wolf     do_drain_end(data->drain_type, data->bs);
507bb675689SKevin Wolf 
508ab613350SStefan Hajnoczi     data->co_done = true;
509ab613350SStefan Hajnoczi     aio_wait_kick();
510bb675689SKevin Wolf }
511bb675689SKevin Wolf 
512bb675689SKevin Wolf static void test_iothread_aio_cb(void *opaque, int ret)
513bb675689SKevin Wolf {
514bb675689SKevin Wolf     int *aio_ret = opaque;
515bb675689SKevin Wolf     *aio_ret = ret;
516bb675689SKevin Wolf     qemu_event_set(&done_event);
517bb675689SKevin Wolf }
518bb675689SKevin Wolf 
519ecc1a5c7SKevin Wolf static void test_iothread_main_thread_bh(void *opaque)
520ecc1a5c7SKevin Wolf {
521ecc1a5c7SKevin Wolf     struct test_iothread_data *data = opaque;
522ecc1a5c7SKevin Wolf 
523ecc1a5c7SKevin Wolf     /* Test that the AioContext is not yet locked in a random BH that is
524ecc1a5c7SKevin Wolf      * executed during drain, otherwise this would deadlock. */
525ecc1a5c7SKevin Wolf     aio_context_acquire(bdrv_get_aio_context(data->bs));
526ecc1a5c7SKevin Wolf     bdrv_flush(data->bs);
527c8bf923dSStefan Hajnoczi     bdrv_dec_in_flight(data->bs); /* incremented by test_iothread_common() */
528ecc1a5c7SKevin Wolf     aio_context_release(bdrv_get_aio_context(data->bs));
529ecc1a5c7SKevin Wolf }
530ecc1a5c7SKevin Wolf 
531bb675689SKevin Wolf /*
532bb675689SKevin Wolf  * Starts an AIO request on a BDS that runs in the AioContext of iothread 1.
533bb675689SKevin Wolf  * The request involves a BH on iothread 2 before it can complete.
534bb675689SKevin Wolf  *
535bb675689SKevin Wolf  * @drain_thread = 0 means that do_drain_begin/end are called from the main
536bb675689SKevin Wolf  * thread, @drain_thread = 1 means that they are called from iothread 1. Drain
537bb675689SKevin Wolf  * for this BDS cannot be called from iothread 2 because only the main thread
538bb675689SKevin Wolf  * may do cross-AioContext polling.
539bb675689SKevin Wolf  */
540bb675689SKevin Wolf static void test_iothread_common(enum drain_type drain_type, int drain_thread)
541bb675689SKevin Wolf {
542bb675689SKevin Wolf     BlockBackend *blk;
543bb675689SKevin Wolf     BlockDriverState *bs;
544bb675689SKevin Wolf     BDRVTestState *s;
545bb675689SKevin Wolf     BlockAIOCB *acb;
546ab613350SStefan Hajnoczi     Coroutine *co;
547bb675689SKevin Wolf     int aio_ret;
548bb675689SKevin Wolf     struct test_iothread_data data;
549bb675689SKevin Wolf 
550bb675689SKevin Wolf     IOThread *a = iothread_new();
551bb675689SKevin Wolf     IOThread *b = iothread_new();
552bb675689SKevin Wolf     AioContext *ctx_a = iothread_get_aio_context(a);
553bb675689SKevin Wolf     AioContext *ctx_b = iothread_get_aio_context(b);
554bb675689SKevin Wolf 
555405d8fe0SVladimir Sementsov-Ogievskiy     QEMUIOVector qiov = QEMU_IOVEC_INIT_BUF(qiov, NULL, 0);
556bb675689SKevin Wolf 
557bb675689SKevin Wolf     /* bdrv_drain_all() may only be called from the main loop thread */
558bb675689SKevin Wolf     if (drain_type == BDRV_DRAIN_ALL && drain_thread != 0) {
559bb675689SKevin Wolf         goto out;
560bb675689SKevin Wolf     }
561bb675689SKevin Wolf 
562d861ab3aSKevin Wolf     blk = blk_new(qemu_get_aio_context(), BLK_PERM_ALL, BLK_PERM_ALL);
563bb675689SKevin Wolf     bs = bdrv_new_open_driver(&bdrv_test, "test-node", BDRV_O_RDWR,
564bb675689SKevin Wolf                               &error_abort);
565bb675689SKevin Wolf     s = bs->opaque;
566bb675689SKevin Wolf     blk_insert_bs(blk, bs, &error_abort);
567cf312932SKevin Wolf     blk_set_disable_request_queuing(blk, true);
568bb675689SKevin Wolf 
56997896a48SKevin Wolf     blk_set_aio_context(blk, ctx_a, &error_abort);
570bb675689SKevin Wolf     aio_context_acquire(ctx_a);
571bb675689SKevin Wolf 
572bb675689SKevin Wolf     s->bh_indirection_ctx = ctx_b;
573bb675689SKevin Wolf 
574bb675689SKevin Wolf     aio_ret = -EINPROGRESS;
575dd353157SKevin Wolf     qemu_event_reset(&done_event);
576dd353157SKevin Wolf 
577bb675689SKevin Wolf     if (drain_thread == 0) {
578bb675689SKevin Wolf         acb = blk_aio_preadv(blk, 0, &qiov, 0, test_iothread_aio_cb, &aio_ret);
579bb675689SKevin Wolf     } else {
580bb675689SKevin Wolf         acb = blk_aio_preadv(blk, 0, &qiov, 0, aio_ret_cb, &aio_ret);
581bb675689SKevin Wolf     }
582bb675689SKevin Wolf     g_assert(acb != NULL);
583bb675689SKevin Wolf     g_assert_cmpint(aio_ret, ==, -EINPROGRESS);
584bb675689SKevin Wolf 
585bb675689SKevin Wolf     aio_context_release(ctx_a);
586bb675689SKevin Wolf 
587bb675689SKevin Wolf     data = (struct test_iothread_data) {
588bb675689SKevin Wolf         .bs         = bs,
589bb675689SKevin Wolf         .drain_type = drain_type,
590bb675689SKevin Wolf         .aio_ret    = &aio_ret,
591bb675689SKevin Wolf     };
592bb675689SKevin Wolf 
593bb675689SKevin Wolf     switch (drain_thread) {
594bb675689SKevin Wolf     case 0:
595bb675689SKevin Wolf         if (drain_type != BDRV_DRAIN_ALL) {
596bb675689SKevin Wolf             aio_context_acquire(ctx_a);
597bb675689SKevin Wolf         }
598bb675689SKevin Wolf 
599c8bf923dSStefan Hajnoczi         /*
600c8bf923dSStefan Hajnoczi          * Increment in_flight so that do_drain_begin() waits for
601c8bf923dSStefan Hajnoczi          * test_iothread_main_thread_bh(). This prevents the race between
602c8bf923dSStefan Hajnoczi          * test_iothread_main_thread_bh() in IOThread a and do_drain_begin() in
603c8bf923dSStefan Hajnoczi          * this thread. test_iothread_main_thread_bh() decrements in_flight.
604c8bf923dSStefan Hajnoczi          */
605c8bf923dSStefan Hajnoczi         bdrv_inc_in_flight(bs);
606ecc1a5c7SKevin Wolf         aio_bh_schedule_oneshot(ctx_a, test_iothread_main_thread_bh, &data);
607ecc1a5c7SKevin Wolf 
608bb675689SKevin Wolf         /* The request is running on the IOThread a. Draining its block device
609bb675689SKevin Wolf          * will make sure that it has completed as far as the BDS is concerned,
610bb675689SKevin Wolf          * but the drain in this thread can continue immediately after
611bb675689SKevin Wolf          * bdrv_dec_in_flight() and aio_ret might be assigned only slightly
612bb675689SKevin Wolf          * later. */
613bb675689SKevin Wolf         do_drain_begin(drain_type, bs);
614bb675689SKevin Wolf         g_assert_cmpint(bs->in_flight, ==, 0);
615bb675689SKevin Wolf 
616bb675689SKevin Wolf         if (drain_type != BDRV_DRAIN_ALL) {
617bb675689SKevin Wolf             aio_context_release(ctx_a);
618bb675689SKevin Wolf         }
619bb675689SKevin Wolf         qemu_event_wait(&done_event);
620bb675689SKevin Wolf         if (drain_type != BDRV_DRAIN_ALL) {
621bb675689SKevin Wolf             aio_context_acquire(ctx_a);
622bb675689SKevin Wolf         }
623bb675689SKevin Wolf 
624bb675689SKevin Wolf         g_assert_cmpint(aio_ret, ==, 0);
625bb675689SKevin Wolf         do_drain_end(drain_type, bs);
626bb675689SKevin Wolf 
627bb675689SKevin Wolf         if (drain_type != BDRV_DRAIN_ALL) {
628bb675689SKevin Wolf             aio_context_release(ctx_a);
629bb675689SKevin Wolf         }
630bb675689SKevin Wolf         break;
631bb675689SKevin Wolf     case 1:
632ab613350SStefan Hajnoczi         co = qemu_coroutine_create(test_iothread_drain_co_entry, &data);
633ab613350SStefan Hajnoczi         aio_co_enter(ctx_a, co);
634ab613350SStefan Hajnoczi         AIO_WAIT_WHILE_UNLOCKED(NULL, !data.co_done);
635bb675689SKevin Wolf         break;
636bb675689SKevin Wolf     default:
637bb675689SKevin Wolf         g_assert_not_reached();
638bb675689SKevin Wolf     }
639bb675689SKevin Wolf 
640bb675689SKevin Wolf     aio_context_acquire(ctx_a);
64197896a48SKevin Wolf     blk_set_aio_context(blk, qemu_get_aio_context(), &error_abort);
642bb675689SKevin Wolf     aio_context_release(ctx_a);
643bb675689SKevin Wolf 
644bb675689SKevin Wolf     bdrv_unref(bs);
645bb675689SKevin Wolf     blk_unref(blk);
646bb675689SKevin Wolf 
647bb675689SKevin Wolf out:
648bb675689SKevin Wolf     iothread_join(a);
649bb675689SKevin Wolf     iothread_join(b);
650bb675689SKevin Wolf }
651bb675689SKevin Wolf 
652bb675689SKevin Wolf static void test_iothread_drain_all(void)
653bb675689SKevin Wolf {
654bb675689SKevin Wolf     test_iothread_common(BDRV_DRAIN_ALL, 0);
655bb675689SKevin Wolf     test_iothread_common(BDRV_DRAIN_ALL, 1);
656bb675689SKevin Wolf }
657bb675689SKevin Wolf 
658bb675689SKevin Wolf static void test_iothread_drain(void)
659bb675689SKevin Wolf {
660bb675689SKevin Wolf     test_iothread_common(BDRV_DRAIN, 0);
661bb675689SKevin Wolf     test_iothread_common(BDRV_DRAIN, 1);
662bb675689SKevin Wolf }
663bb675689SKevin Wolf 
6647253220dSKevin Wolf 
6657253220dSKevin Wolf typedef struct TestBlockJob {
6667253220dSKevin Wolf     BlockJob common;
6671b177bbeSVladimir Sementsov-Ogievskiy     BlockDriverState *bs;
668d49725afSKevin Wolf     int run_ret;
669d49725afSKevin Wolf     int prepare_ret;
670d8b3afd5SKevin Wolf     bool running;
6717253220dSKevin Wolf     bool should_complete;
6727253220dSKevin Wolf } TestBlockJob;
6737253220dSKevin Wolf 
674ae23dde9SKevin Wolf static int test_job_prepare(Job *job)
675ae23dde9SKevin Wolf {
676ae23dde9SKevin Wolf     TestBlockJob *s = container_of(job, TestBlockJob, common.job);
677ae23dde9SKevin Wolf 
678ae23dde9SKevin Wolf     /* Provoke an AIO_WAIT_WHILE() call to verify there is no deadlock */
6791b177bbeSVladimir Sementsov-Ogievskiy     bdrv_flush(s->bs);
680d49725afSKevin Wolf     return s->prepare_ret;
681d49725afSKevin Wolf }
682d49725afSKevin Wolf 
683d49725afSKevin Wolf static void test_job_commit(Job *job)
684d49725afSKevin Wolf {
685d49725afSKevin Wolf     TestBlockJob *s = container_of(job, TestBlockJob, common.job);
686d49725afSKevin Wolf 
687d49725afSKevin Wolf     /* Provoke an AIO_WAIT_WHILE() call to verify there is no deadlock */
6881b177bbeSVladimir Sementsov-Ogievskiy     bdrv_flush(s->bs);
689d49725afSKevin Wolf }
690d49725afSKevin Wolf 
691d49725afSKevin Wolf static void test_job_abort(Job *job)
692d49725afSKevin Wolf {
693d49725afSKevin Wolf     TestBlockJob *s = container_of(job, TestBlockJob, common.job);
694d49725afSKevin Wolf 
695d49725afSKevin Wolf     /* Provoke an AIO_WAIT_WHILE() call to verify there is no deadlock */
6961b177bbeSVladimir Sementsov-Ogievskiy     bdrv_flush(s->bs);
697ae23dde9SKevin Wolf }
698ae23dde9SKevin Wolf 
699f67432a2SJohn Snow static int coroutine_fn test_job_run(Job *job, Error **errp)
7007253220dSKevin Wolf {
701f67432a2SJohn Snow     TestBlockJob *s = container_of(job, TestBlockJob, common.job);
7027253220dSKevin Wolf 
703d8b3afd5SKevin Wolf     /* We are running the actual job code past the pause point in
704d8b3afd5SKevin Wolf      * job_co_entry(). */
705d8b3afd5SKevin Wolf     s->running = true;
706d8b3afd5SKevin Wolf 
7072e1795b5SKevin Wolf     job_transition_to_ready(&s->common.job);
7087253220dSKevin Wolf     while (!s->should_complete) {
7095599c162SKevin Wolf         /* Avoid job_sleep_ns() because it marks the job as !busy. We want to
7105599c162SKevin Wolf          * emulate some actual activity (probably some I/O) here so that drain
7115599c162SKevin Wolf          * has to wait for this activity to stop. */
712d8b3afd5SKevin Wolf         qemu_co_sleep_ns(QEMU_CLOCK_REALTIME, 1000000);
713d8b3afd5SKevin Wolf 
71489bd0305SKevin Wolf         job_pause_point(&s->common.job);
7157253220dSKevin Wolf     }
7167253220dSKevin Wolf 
717d49725afSKevin Wolf     return s->run_ret;
7187253220dSKevin Wolf }
7197253220dSKevin Wolf 
7203453d972SKevin Wolf static void test_job_complete(Job *job, Error **errp)
7217253220dSKevin Wolf {
7223453d972SKevin Wolf     TestBlockJob *s = container_of(job, TestBlockJob, common.job);
7237253220dSKevin Wolf     s->should_complete = true;
7247253220dSKevin Wolf }
7257253220dSKevin Wolf 
7267253220dSKevin Wolf BlockJobDriver test_job_driver = {
72733e9e9bdSKevin Wolf     .job_driver = {
7287253220dSKevin Wolf         .instance_size  = sizeof(TestBlockJob),
72980fa2c75SKevin Wolf         .free           = block_job_free,
730b15de828SKevin Wolf         .user_resume    = block_job_user_resume,
731f67432a2SJohn Snow         .run            = test_job_run,
7327253220dSKevin Wolf         .complete       = test_job_complete,
733ae23dde9SKevin Wolf         .prepare        = test_job_prepare,
734d49725afSKevin Wolf         .commit         = test_job_commit,
735d49725afSKevin Wolf         .abort          = test_job_abort,
7363453d972SKevin Wolf     },
7377253220dSKevin Wolf };
7387253220dSKevin Wolf 
739d49725afSKevin Wolf enum test_job_result {
740d49725afSKevin Wolf     TEST_JOB_SUCCESS,
741d49725afSKevin Wolf     TEST_JOB_FAIL_RUN,
742d49725afSKevin Wolf     TEST_JOB_FAIL_PREPARE,
743d49725afSKevin Wolf };
744d49725afSKevin Wolf 
745d8b3afd5SKevin Wolf enum test_job_drain_node {
746d8b3afd5SKevin Wolf     TEST_JOB_DRAIN_SRC,
747d8b3afd5SKevin Wolf     TEST_JOB_DRAIN_SRC_CHILD,
748d8b3afd5SKevin Wolf };
749d8b3afd5SKevin Wolf 
750d8b3afd5SKevin Wolf static void test_blockjob_common_drain_node(enum drain_type drain_type,
751d8b3afd5SKevin Wolf                                             bool use_iothread,
752d8b3afd5SKevin Wolf                                             enum test_job_result result,
753d8b3afd5SKevin Wolf                                             enum test_job_drain_node drain_node)
7547253220dSKevin Wolf {
7557253220dSKevin Wolf     BlockBackend *blk_src, *blk_target;
756d8b3afd5SKevin Wolf     BlockDriverState *src, *src_backing, *src_overlay, *target, *drain_bs;
7577253220dSKevin Wolf     BlockJob *job;
758d49725afSKevin Wolf     TestBlockJob *tjob;
759f62c1729SKevin Wolf     IOThread *iothread = NULL;
760f62c1729SKevin Wolf     AioContext *ctx;
7617253220dSKevin Wolf     int ret;
7627253220dSKevin Wolf 
7637253220dSKevin Wolf     src = bdrv_new_open_driver(&bdrv_test, "source", BDRV_O_RDWR,
7647253220dSKevin Wolf                                &error_abort);
765d8b3afd5SKevin Wolf     src_backing = bdrv_new_open_driver(&bdrv_test, "source-backing",
766d8b3afd5SKevin Wolf                                        BDRV_O_RDWR, &error_abort);
767d8b3afd5SKevin Wolf     src_overlay = bdrv_new_open_driver(&bdrv_test, "source-overlay",
768d8b3afd5SKevin Wolf                                        BDRV_O_RDWR, &error_abort);
769d8b3afd5SKevin Wolf 
770d8b3afd5SKevin Wolf     bdrv_set_backing_hd(src_overlay, src, &error_abort);
771d8b3afd5SKevin Wolf     bdrv_unref(src);
772d8b3afd5SKevin Wolf     bdrv_set_backing_hd(src, src_backing, &error_abort);
773d8b3afd5SKevin Wolf     bdrv_unref(src_backing);
774d8b3afd5SKevin Wolf 
775d861ab3aSKevin Wolf     blk_src = blk_new(qemu_get_aio_context(), BLK_PERM_ALL, BLK_PERM_ALL);
776d8b3afd5SKevin Wolf     blk_insert_bs(blk_src, src_overlay, &error_abort);
777d8b3afd5SKevin Wolf 
778d8b3afd5SKevin Wolf     switch (drain_node) {
779d8b3afd5SKevin Wolf     case TEST_JOB_DRAIN_SRC:
780d8b3afd5SKevin Wolf         drain_bs = src;
781d8b3afd5SKevin Wolf         break;
782d8b3afd5SKevin Wolf     case TEST_JOB_DRAIN_SRC_CHILD:
783d8b3afd5SKevin Wolf         drain_bs = src_backing;
784d8b3afd5SKevin Wolf         break;
785d8b3afd5SKevin Wolf     default:
786d8b3afd5SKevin Wolf         g_assert_not_reached();
787d8b3afd5SKevin Wolf     }
7887253220dSKevin Wolf 
789f62c1729SKevin Wolf     if (use_iothread) {
790f62c1729SKevin Wolf         iothread = iothread_new();
791f62c1729SKevin Wolf         ctx = iothread_get_aio_context(iothread);
79297896a48SKevin Wolf         blk_set_aio_context(blk_src, ctx, &error_abort);
793f62c1729SKevin Wolf     } else {
794f62c1729SKevin Wolf         ctx = qemu_get_aio_context();
795f62c1729SKevin Wolf     }
796f62c1729SKevin Wolf 
7977253220dSKevin Wolf     target = bdrv_new_open_driver(&bdrv_test, "target", BDRV_O_RDWR,
7987253220dSKevin Wolf                                   &error_abort);
799d861ab3aSKevin Wolf     blk_target = blk_new(qemu_get_aio_context(), BLK_PERM_ALL, BLK_PERM_ALL);
8007253220dSKevin Wolf     blk_insert_bs(blk_target, target, &error_abort);
801132ada80SKevin Wolf     blk_set_allow_aio_context_change(blk_target, true);
8027253220dSKevin Wolf 
803f62c1729SKevin Wolf     aio_context_acquire(ctx);
804d49725afSKevin Wolf     tjob = block_job_create("job0", &test_job_driver, NULL, src,
805d49725afSKevin Wolf                             0, BLK_PERM_ALL,
80675859b94SJohn Snow                             0, 0, NULL, NULL, &error_abort);
8071b177bbeSVladimir Sementsov-Ogievskiy     tjob->bs = src;
808d49725afSKevin Wolf     job = &tjob->common;
809f3bbc53dSKevin Wolf 
810f3bbc53dSKevin Wolf     bdrv_graph_wrlock(target);
8117253220dSKevin Wolf     block_job_add_bdrv(job, "target", target, 0, BLK_PERM_ALL, &error_abort);
812f3bbc53dSKevin Wolf     bdrv_graph_wrunlock();
813d49725afSKevin Wolf 
814d49725afSKevin Wolf     switch (result) {
815d49725afSKevin Wolf     case TEST_JOB_SUCCESS:
816d49725afSKevin Wolf         break;
817d49725afSKevin Wolf     case TEST_JOB_FAIL_RUN:
818d49725afSKevin Wolf         tjob->run_ret = -EIO;
819d49725afSKevin Wolf         break;
820d49725afSKevin Wolf     case TEST_JOB_FAIL_PREPARE:
821d49725afSKevin Wolf         tjob->prepare_ret = -EIO;
822d49725afSKevin Wolf         break;
823d49725afSKevin Wolf     }
8246f592e5aSEmanuele Giuseppe Esposito     aio_context_release(ctx);
825d49725afSKevin Wolf 
826da01ff7fSKevin Wolf     job_start(&job->job);
8277253220dSKevin Wolf 
828d8b3afd5SKevin Wolf     if (use_iothread) {
829d8b3afd5SKevin Wolf         /* job_co_entry() is run in the I/O thread, wait for the actual job
830d8b3afd5SKevin Wolf          * code to start (we don't want to catch the job in the pause point in
831d8b3afd5SKevin Wolf          * job_co_entry(). */
832d8b3afd5SKevin Wolf         while (!tjob->running) {
833d8b3afd5SKevin Wolf             aio_poll(qemu_get_aio_context(), false);
834d8b3afd5SKevin Wolf         }
835d8b3afd5SKevin Wolf     }
836d8b3afd5SKevin Wolf 
837191e7af3SEmanuele Giuseppe Esposito     WITH_JOB_LOCK_GUARD() {
838da01ff7fSKevin Wolf         g_assert_cmpint(job->job.pause_count, ==, 0);
839da01ff7fSKevin Wolf         g_assert_false(job->job.paused);
840d8b3afd5SKevin Wolf         g_assert_true(tjob->running);
8415599c162SKevin Wolf         g_assert_true(job->job.busy); /* We're in qemu_co_sleep_ns() */
842191e7af3SEmanuele Giuseppe Esposito     }
8437253220dSKevin Wolf 
844d8b3afd5SKevin Wolf     do_drain_begin_unlocked(drain_type, drain_bs);
8457253220dSKevin Wolf 
846191e7af3SEmanuele Giuseppe Esposito     WITH_JOB_LOCK_GUARD() {
8477253220dSKevin Wolf         if (drain_type == BDRV_DRAIN_ALL) {
84881193349SKevin Wolf             /* bdrv_drain_all() drains both src and target */
849da01ff7fSKevin Wolf             g_assert_cmpint(job->job.pause_count, ==, 2);
8507253220dSKevin Wolf         } else {
851da01ff7fSKevin Wolf             g_assert_cmpint(job->job.pause_count, ==, 1);
8527253220dSKevin Wolf         }
85389bd0305SKevin Wolf         g_assert_true(job->job.paused);
854da01ff7fSKevin Wolf         g_assert_false(job->job.busy); /* The job is paused */
855191e7af3SEmanuele Giuseppe Esposito     }
8567253220dSKevin Wolf 
857d8b3afd5SKevin Wolf     do_drain_end_unlocked(drain_type, drain_bs);
858f62c1729SKevin Wolf 
859f62c1729SKevin Wolf     if (use_iothread) {
860191e7af3SEmanuele Giuseppe Esposito         /*
861191e7af3SEmanuele Giuseppe Esposito          * Here we are waiting for the paused status to change,
862191e7af3SEmanuele Giuseppe Esposito          * so don't bother protecting the read every time.
863191e7af3SEmanuele Giuseppe Esposito          *
864191e7af3SEmanuele Giuseppe Esposito          * paused is reset in the I/O thread, wait for it
865191e7af3SEmanuele Giuseppe Esposito          */
866f62c1729SKevin Wolf         while (job->job.paused) {
867f62c1729SKevin Wolf             aio_poll(qemu_get_aio_context(), false);
868f62c1729SKevin Wolf         }
869f62c1729SKevin Wolf     }
8707253220dSKevin Wolf 
871191e7af3SEmanuele Giuseppe Esposito     WITH_JOB_LOCK_GUARD() {
872da01ff7fSKevin Wolf         g_assert_cmpint(job->job.pause_count, ==, 0);
873da01ff7fSKevin Wolf         g_assert_false(job->job.paused);
87489bd0305SKevin Wolf         g_assert_true(job->job.busy); /* We're in qemu_co_sleep_ns() */
875191e7af3SEmanuele Giuseppe Esposito     }
8767253220dSKevin Wolf 
877132ada80SKevin Wolf     do_drain_begin_unlocked(drain_type, target);
8787253220dSKevin Wolf 
879191e7af3SEmanuele Giuseppe Esposito     WITH_JOB_LOCK_GUARD() {
8807253220dSKevin Wolf         if (drain_type == BDRV_DRAIN_ALL) {
88181193349SKevin Wolf             /* bdrv_drain_all() drains both src and target */
882da01ff7fSKevin Wolf             g_assert_cmpint(job->job.pause_count, ==, 2);
8837253220dSKevin Wolf         } else {
884da01ff7fSKevin Wolf             g_assert_cmpint(job->job.pause_count, ==, 1);
8857253220dSKevin Wolf         }
88689bd0305SKevin Wolf         g_assert_true(job->job.paused);
887da01ff7fSKevin Wolf         g_assert_false(job->job.busy); /* The job is paused */
888191e7af3SEmanuele Giuseppe Esposito     }
8897253220dSKevin Wolf 
890132ada80SKevin Wolf     do_drain_end_unlocked(drain_type, target);
8917253220dSKevin Wolf 
892f62c1729SKevin Wolf     if (use_iothread) {
893191e7af3SEmanuele Giuseppe Esposito         /*
894191e7af3SEmanuele Giuseppe Esposito          * Here we are waiting for the paused status to change,
895191e7af3SEmanuele Giuseppe Esposito          * so don't bother protecting the read every time.
896191e7af3SEmanuele Giuseppe Esposito          *
897191e7af3SEmanuele Giuseppe Esposito          * paused is reset in the I/O thread, wait for it
898191e7af3SEmanuele Giuseppe Esposito          */
899f62c1729SKevin Wolf         while (job->job.paused) {
900f62c1729SKevin Wolf             aio_poll(qemu_get_aio_context(), false);
901f62c1729SKevin Wolf         }
902f62c1729SKevin Wolf     }
903f62c1729SKevin Wolf 
904191e7af3SEmanuele Giuseppe Esposito     WITH_JOB_LOCK_GUARD() {
905da01ff7fSKevin Wolf         g_assert_cmpint(job->job.pause_count, ==, 0);
906da01ff7fSKevin Wolf         g_assert_false(job->job.paused);
9075599c162SKevin Wolf         g_assert_true(job->job.busy); /* We're in qemu_co_sleep_ns() */
908191e7af3SEmanuele Giuseppe Esposito     }
9097253220dSKevin Wolf 
910191e7af3SEmanuele Giuseppe Esposito     WITH_JOB_LOCK_GUARD() {
911191e7af3SEmanuele Giuseppe Esposito         ret = job_complete_sync_locked(&job->job, &error_abort);
912191e7af3SEmanuele Giuseppe Esposito     }
913d49725afSKevin Wolf     g_assert_cmpint(ret, ==, (result == TEST_JOB_SUCCESS ? 0 : -EIO));
9147253220dSKevin Wolf 
9156f592e5aSEmanuele Giuseppe Esposito     aio_context_acquire(ctx);
916f62c1729SKevin Wolf     if (use_iothread) {
91797896a48SKevin Wolf         blk_set_aio_context(blk_src, qemu_get_aio_context(), &error_abort);
918ad943dcbSKevin Wolf         assert(blk_get_aio_context(blk_target) == qemu_get_aio_context());
919f62c1729SKevin Wolf     }
920f62c1729SKevin Wolf     aio_context_release(ctx);
921f62c1729SKevin Wolf 
9227253220dSKevin Wolf     blk_unref(blk_src);
9237253220dSKevin Wolf     blk_unref(blk_target);
924d8b3afd5SKevin Wolf     bdrv_unref(src_overlay);
9257253220dSKevin Wolf     bdrv_unref(target);
926f62c1729SKevin Wolf 
927f62c1729SKevin Wolf     if (iothread) {
928f62c1729SKevin Wolf         iothread_join(iothread);
929f62c1729SKevin Wolf     }
9307253220dSKevin Wolf }
9317253220dSKevin Wolf 
932d8b3afd5SKevin Wolf static void test_blockjob_common(enum drain_type drain_type, bool use_iothread,
933d8b3afd5SKevin Wolf                                  enum test_job_result result)
934d8b3afd5SKevin Wolf {
935d8b3afd5SKevin Wolf     test_blockjob_common_drain_node(drain_type, use_iothread, result,
936d8b3afd5SKevin Wolf                                     TEST_JOB_DRAIN_SRC);
937d8b3afd5SKevin Wolf     test_blockjob_common_drain_node(drain_type, use_iothread, result,
938d8b3afd5SKevin Wolf                                     TEST_JOB_DRAIN_SRC_CHILD);
939d8b3afd5SKevin Wolf }
940d8b3afd5SKevin Wolf 
9417253220dSKevin Wolf static void test_blockjob_drain_all(void)
9427253220dSKevin Wolf {
943d49725afSKevin Wolf     test_blockjob_common(BDRV_DRAIN_ALL, false, TEST_JOB_SUCCESS);
9447253220dSKevin Wolf }
9457253220dSKevin Wolf 
9467253220dSKevin Wolf static void test_blockjob_drain(void)
9477253220dSKevin Wolf {
948d49725afSKevin Wolf     test_blockjob_common(BDRV_DRAIN, false, TEST_JOB_SUCCESS);
9497253220dSKevin Wolf }
9507253220dSKevin Wolf 
951d49725afSKevin Wolf static void test_blockjob_error_drain_all(void)
952d49725afSKevin Wolf {
953d49725afSKevin Wolf     test_blockjob_common(BDRV_DRAIN_ALL, false, TEST_JOB_FAIL_RUN);
954d49725afSKevin Wolf     test_blockjob_common(BDRV_DRAIN_ALL, false, TEST_JOB_FAIL_PREPARE);
955d49725afSKevin Wolf }
956d49725afSKevin Wolf 
957d49725afSKevin Wolf static void test_blockjob_error_drain(void)
958d49725afSKevin Wolf {
959d49725afSKevin Wolf     test_blockjob_common(BDRV_DRAIN, false, TEST_JOB_FAIL_RUN);
960d49725afSKevin Wolf     test_blockjob_common(BDRV_DRAIN, false, TEST_JOB_FAIL_PREPARE);
961d49725afSKevin Wolf }
962d49725afSKevin Wolf 
963f62c1729SKevin Wolf static void test_blockjob_iothread_drain_all(void)
964f62c1729SKevin Wolf {
965d49725afSKevin Wolf     test_blockjob_common(BDRV_DRAIN_ALL, true, TEST_JOB_SUCCESS);
966f62c1729SKevin Wolf }
967f62c1729SKevin Wolf 
968f62c1729SKevin Wolf static void test_blockjob_iothread_drain(void)
969f62c1729SKevin Wolf {
970d49725afSKevin Wolf     test_blockjob_common(BDRV_DRAIN, true, TEST_JOB_SUCCESS);
971f62c1729SKevin Wolf }
972f62c1729SKevin Wolf 
973d49725afSKevin Wolf static void test_blockjob_iothread_error_drain_all(void)
974d49725afSKevin Wolf {
975d49725afSKevin Wolf     test_blockjob_common(BDRV_DRAIN_ALL, true, TEST_JOB_FAIL_RUN);
976d49725afSKevin Wolf     test_blockjob_common(BDRV_DRAIN_ALL, true, TEST_JOB_FAIL_PREPARE);
977d49725afSKevin Wolf }
978d49725afSKevin Wolf 
979d49725afSKevin Wolf static void test_blockjob_iothread_error_drain(void)
980d49725afSKevin Wolf {
981d49725afSKevin Wolf     test_blockjob_common(BDRV_DRAIN, true, TEST_JOB_FAIL_RUN);
982d49725afSKevin Wolf     test_blockjob_common(BDRV_DRAIN, true, TEST_JOB_FAIL_PREPARE);
983d49725afSKevin Wolf }
984d49725afSKevin Wolf 
9854c8158e3SMax Reitz 
9864c8158e3SMax Reitz typedef struct BDRVTestTopState {
9874c8158e3SMax Reitz     BdrvChild *wait_child;
9884c8158e3SMax Reitz } BDRVTestTopState;
9894c8158e3SMax Reitz 
9904c8158e3SMax Reitz static void bdrv_test_top_close(BlockDriverState *bs)
9914c8158e3SMax Reitz {
9924c8158e3SMax Reitz     BdrvChild *c, *next_c;
99332a8aba3SKevin Wolf 
99432a8aba3SKevin Wolf     bdrv_graph_wrlock(NULL);
9954c8158e3SMax Reitz     QLIST_FOREACH_SAFE(c, &bs->children, next, next_c) {
9964c8158e3SMax Reitz         bdrv_unref_child(bs, c);
9974c8158e3SMax Reitz     }
99832a8aba3SKevin Wolf     bdrv_graph_wrunlock();
9994c8158e3SMax Reitz }
10004c8158e3SMax Reitz 
1001b9b10c35SKevin Wolf static int coroutine_fn GRAPH_RDLOCK
1002b9b10c35SKevin Wolf bdrv_test_top_co_preadv(BlockDriverState *bs, int64_t offset, int64_t bytes,
1003b9b10c35SKevin Wolf                         QEMUIOVector *qiov, BdrvRequestFlags flags)
10044c8158e3SMax Reitz {
10054c8158e3SMax Reitz     BDRVTestTopState *tts = bs->opaque;
10064c8158e3SMax Reitz     return bdrv_co_preadv(tts->wait_child, offset, bytes, qiov, flags);
10074c8158e3SMax Reitz }
10084c8158e3SMax Reitz 
10094c8158e3SMax Reitz static BlockDriver bdrv_test_top_driver = {
10104c8158e3SMax Reitz     .format_name            = "test_top_driver",
10114c8158e3SMax Reitz     .instance_size          = sizeof(BDRVTestTopState),
10124c8158e3SMax Reitz 
10134c8158e3SMax Reitz     .bdrv_close             = bdrv_test_top_close,
10144c8158e3SMax Reitz     .bdrv_co_preadv         = bdrv_test_top_co_preadv,
10154c8158e3SMax Reitz 
101669dca43dSMax Reitz     .bdrv_child_perm        = bdrv_default_perms,
10174c8158e3SMax Reitz };
10184c8158e3SMax Reitz 
10194c8158e3SMax Reitz typedef struct TestCoDeleteByDrainData {
10204c8158e3SMax Reitz     BlockBackend *blk;
10214c8158e3SMax Reitz     bool detach_instead_of_delete;
10224c8158e3SMax Reitz     bool done;
10234c8158e3SMax Reitz } TestCoDeleteByDrainData;
10244c8158e3SMax Reitz 
10254c8158e3SMax Reitz static void coroutine_fn test_co_delete_by_drain(void *opaque)
10264c8158e3SMax Reitz {
10274c8158e3SMax Reitz     TestCoDeleteByDrainData *dbdd = opaque;
10284c8158e3SMax Reitz     BlockBackend *blk = dbdd->blk;
10294c8158e3SMax Reitz     BlockDriverState *bs = blk_bs(blk);
10304c8158e3SMax Reitz     BDRVTestTopState *tts = bs->opaque;
10314c8158e3SMax Reitz     void *buffer = g_malloc(65536);
1032405d8fe0SVladimir Sementsov-Ogievskiy     QEMUIOVector qiov = QEMU_IOVEC_INIT_BUF(qiov, buffer, 65536);
10334c8158e3SMax Reitz 
10344c8158e3SMax Reitz     /* Pretend some internal write operation from parent to child.
10354c8158e3SMax Reitz      * Important: We have to read from the child, not from the parent!
10364c8158e3SMax Reitz      * Draining works by first propagating it all up the tree to the
10374c8158e3SMax Reitz      * root and then waiting for drainage from root to the leaves
10384c8158e3SMax Reitz      * (protocol nodes).  If we have a request waiting on the root,
10394c8158e3SMax Reitz      * everything will be drained before we go back down the tree, but
10404c8158e3SMax Reitz      * we do not want that.  We want to be in the middle of draining
10414c8158e3SMax Reitz      * when this following requests returns. */
104287f130bdSKevin Wolf     bdrv_graph_co_rdlock();
10434c8158e3SMax Reitz     bdrv_co_preadv(tts->wait_child, 0, 65536, &qiov, 0);
104487f130bdSKevin Wolf     bdrv_graph_co_rdunlock();
10454c8158e3SMax Reitz 
10464c8158e3SMax Reitz     g_assert_cmpint(bs->refcnt, ==, 1);
10474c8158e3SMax Reitz 
10484c8158e3SMax Reitz     if (!dbdd->detach_instead_of_delete) {
104901a10c24SKevin Wolf         blk_co_unref(blk);
10504c8158e3SMax Reitz     } else {
10514c8158e3SMax Reitz         BdrvChild *c, *next_c;
1052680e0cc4SKevin Wolf         bdrv_graph_co_rdlock();
10534c8158e3SMax Reitz         QLIST_FOREACH_SAFE(c, &bs->children, next, next_c) {
1054680e0cc4SKevin Wolf             bdrv_graph_co_rdunlock();
105532a8aba3SKevin Wolf             bdrv_co_unref_child(bs, c);
1056680e0cc4SKevin Wolf             bdrv_graph_co_rdlock();
10574c8158e3SMax Reitz         }
1058680e0cc4SKevin Wolf         bdrv_graph_co_rdunlock();
10594c8158e3SMax Reitz     }
10604c8158e3SMax Reitz 
10614c8158e3SMax Reitz     dbdd->done = true;
10627b43db3cSMarc-André Lureau     g_free(buffer);
10634c8158e3SMax Reitz }
10644c8158e3SMax Reitz 
10654c8158e3SMax Reitz /**
10664c8158e3SMax Reitz  * Test what happens when some BDS has some children, you drain one of
10674c8158e3SMax Reitz  * them and this results in the BDS being deleted.
10684c8158e3SMax Reitz  *
10694c8158e3SMax Reitz  * If @detach_instead_of_delete is set, the BDS is not going to be
10704c8158e3SMax Reitz  * deleted but will only detach all of its children.
10714c8158e3SMax Reitz  */
1072ebd31837SKevin Wolf static void do_test_delete_by_drain(bool detach_instead_of_delete,
1073ebd31837SKevin Wolf                                     enum drain_type drain_type)
10744c8158e3SMax Reitz {
10754c8158e3SMax Reitz     BlockBackend *blk;
10764c8158e3SMax Reitz     BlockDriverState *bs, *child_bs, *null_bs;
10774c8158e3SMax Reitz     BDRVTestTopState *tts;
10784c8158e3SMax Reitz     TestCoDeleteByDrainData dbdd;
10794c8158e3SMax Reitz     Coroutine *co;
10804c8158e3SMax Reitz 
10814c8158e3SMax Reitz     bs = bdrv_new_open_driver(&bdrv_test_top_driver, "top", BDRV_O_RDWR,
10824c8158e3SMax Reitz                               &error_abort);
10834c8158e3SMax Reitz     bs->total_sectors = 65536 >> BDRV_SECTOR_BITS;
10844c8158e3SMax Reitz     tts = bs->opaque;
10854c8158e3SMax Reitz 
10864c8158e3SMax Reitz     null_bs = bdrv_open("null-co://", NULL, NULL, BDRV_O_RDWR | BDRV_O_PROTOCOL,
10874c8158e3SMax Reitz                         &error_abort);
1088afdaeb9eSKevin Wolf     bdrv_graph_wrlock(NULL);
1089a16be3cdSMax Reitz     bdrv_attach_child(bs, null_bs, "null-child", &child_of_bds,
1090a16be3cdSMax Reitz                       BDRV_CHILD_DATA, &error_abort);
1091afdaeb9eSKevin Wolf     bdrv_graph_wrunlock();
10924c8158e3SMax Reitz 
10934c8158e3SMax Reitz     /* This child will be the one to pass to requests through to, and
10944c8158e3SMax Reitz      * it will stall until a drain occurs */
10954c8158e3SMax Reitz     child_bs = bdrv_new_open_driver(&bdrv_test, "child", BDRV_O_RDWR,
10964c8158e3SMax Reitz                                     &error_abort);
10974c8158e3SMax Reitz     child_bs->total_sectors = 65536 >> BDRV_SECTOR_BITS;
10984c8158e3SMax Reitz     /* Takes our reference to child_bs */
1099afdaeb9eSKevin Wolf     bdrv_graph_wrlock(NULL);
1100a16be3cdSMax Reitz     tts->wait_child = bdrv_attach_child(bs, child_bs, "wait-child",
1101a16be3cdSMax Reitz                                         &child_of_bds,
1102a16be3cdSMax Reitz                                         BDRV_CHILD_DATA | BDRV_CHILD_PRIMARY,
1103a16be3cdSMax Reitz                                         &error_abort);
1104afdaeb9eSKevin Wolf     bdrv_graph_wrunlock();
11054c8158e3SMax Reitz 
11064c8158e3SMax Reitz     /* This child is just there to be deleted
11074c8158e3SMax Reitz      * (for detach_instead_of_delete == true) */
11084c8158e3SMax Reitz     null_bs = bdrv_open("null-co://", NULL, NULL, BDRV_O_RDWR | BDRV_O_PROTOCOL,
11094c8158e3SMax Reitz                         &error_abort);
1110afdaeb9eSKevin Wolf     bdrv_graph_wrlock(NULL);
1111a16be3cdSMax Reitz     bdrv_attach_child(bs, null_bs, "null-child", &child_of_bds, BDRV_CHILD_DATA,
1112a16be3cdSMax Reitz                       &error_abort);
1113afdaeb9eSKevin Wolf     bdrv_graph_wrunlock();
11144c8158e3SMax Reitz 
1115d861ab3aSKevin Wolf     blk = blk_new(qemu_get_aio_context(), BLK_PERM_ALL, BLK_PERM_ALL);
11164c8158e3SMax Reitz     blk_insert_bs(blk, bs, &error_abort);
11174c8158e3SMax Reitz 
11184c8158e3SMax Reitz     /* Referenced by blk now */
11194c8158e3SMax Reitz     bdrv_unref(bs);
11204c8158e3SMax Reitz 
11214c8158e3SMax Reitz     g_assert_cmpint(bs->refcnt, ==, 1);
11224c8158e3SMax Reitz     g_assert_cmpint(child_bs->refcnt, ==, 1);
11234c8158e3SMax Reitz     g_assert_cmpint(null_bs->refcnt, ==, 1);
11244c8158e3SMax Reitz 
11254c8158e3SMax Reitz 
11264c8158e3SMax Reitz     dbdd = (TestCoDeleteByDrainData){
11274c8158e3SMax Reitz         .blk = blk,
11284c8158e3SMax Reitz         .detach_instead_of_delete = detach_instead_of_delete,
11294c8158e3SMax Reitz         .done = false,
11304c8158e3SMax Reitz     };
11314c8158e3SMax Reitz     co = qemu_coroutine_create(test_co_delete_by_drain, &dbdd);
11324c8158e3SMax Reitz     qemu_coroutine_enter(co);
11334c8158e3SMax Reitz 
11344c8158e3SMax Reitz     /* Drain the child while the read operation is still pending.
11354c8158e3SMax Reitz      * This should result in the operation finishing and
11364c8158e3SMax Reitz      * test_co_delete_by_drain() resuming.  Thus, @bs will be deleted
11374c8158e3SMax Reitz      * and the coroutine will exit while this drain operation is still
11384c8158e3SMax Reitz      * in progress. */
1139ebd31837SKevin Wolf     switch (drain_type) {
1140ebd31837SKevin Wolf     case BDRV_DRAIN:
11414c8158e3SMax Reitz         bdrv_ref(child_bs);
11424c8158e3SMax Reitz         bdrv_drain(child_bs);
11434c8158e3SMax Reitz         bdrv_unref(child_bs);
1144ebd31837SKevin Wolf         break;
114519f7a7e5SKevin Wolf     case BDRV_DRAIN_ALL:
114619f7a7e5SKevin Wolf         bdrv_drain_all_begin();
114719f7a7e5SKevin Wolf         bdrv_drain_all_end();
114819f7a7e5SKevin Wolf         break;
1149ebd31837SKevin Wolf     default:
1150ebd31837SKevin Wolf         g_assert_not_reached();
1151ebd31837SKevin Wolf     }
11524c8158e3SMax Reitz 
11534c8158e3SMax Reitz     while (!dbdd.done) {
11544c8158e3SMax Reitz         aio_poll(qemu_get_aio_context(), true);
11554c8158e3SMax Reitz     }
11564c8158e3SMax Reitz 
11574c8158e3SMax Reitz     if (detach_instead_of_delete) {
11584c8158e3SMax Reitz         /* Here, the reference has not passed over to the coroutine,
11594c8158e3SMax Reitz          * so we have to delete the BB ourselves */
11604c8158e3SMax Reitz         blk_unref(blk);
11614c8158e3SMax Reitz     }
11624c8158e3SMax Reitz }
11634c8158e3SMax Reitz 
11644c8158e3SMax Reitz static void test_delete_by_drain(void)
11654c8158e3SMax Reitz {
1166ebd31837SKevin Wolf     do_test_delete_by_drain(false, BDRV_DRAIN);
11674c8158e3SMax Reitz }
11684c8158e3SMax Reitz 
116919f7a7e5SKevin Wolf static void test_detach_by_drain_all(void)
117019f7a7e5SKevin Wolf {
117119f7a7e5SKevin Wolf     do_test_delete_by_drain(true, BDRV_DRAIN_ALL);
117219f7a7e5SKevin Wolf }
117319f7a7e5SKevin Wolf 
11744c8158e3SMax Reitz static void test_detach_by_drain(void)
11754c8158e3SMax Reitz {
1176ebd31837SKevin Wolf     do_test_delete_by_drain(true, BDRV_DRAIN);
1177ebd31837SKevin Wolf }
1178ebd31837SKevin Wolf 
11794c8158e3SMax Reitz 
1180231281abSKevin Wolf struct detach_by_parent_data {
1181231281abSKevin Wolf     BlockDriverState *parent_b;
1182231281abSKevin Wolf     BdrvChild *child_b;
1183231281abSKevin Wolf     BlockDriverState *c;
1184231281abSKevin Wolf     BdrvChild *child_c;
118557320ca9SKevin Wolf     bool by_parent_cb;
1186617f3a96SKevin Wolf     bool detach_on_drain;
1187231281abSKevin Wolf };
118857320ca9SKevin Wolf static struct detach_by_parent_data detach_by_parent_data;
1189231281abSKevin Wolf 
1190903df115SKevin Wolf static void no_coroutine_fn detach_indirect_bh(void *opaque)
1191231281abSKevin Wolf {
1192231281abSKevin Wolf     struct detach_by_parent_data *data = opaque;
1193231281abSKevin Wolf 
1194617f3a96SKevin Wolf     bdrv_dec_in_flight(data->child_b->bs);
119532a8aba3SKevin Wolf 
119632a8aba3SKevin Wolf     bdrv_graph_wrlock(NULL);
1197231281abSKevin Wolf     bdrv_unref_child(data->parent_b, data->child_b);
1198231281abSKevin Wolf 
1199231281abSKevin Wolf     bdrv_ref(data->c);
1200231281abSKevin Wolf     data->child_c = bdrv_attach_child(data->parent_b, data->c, "PB-C",
1201a16be3cdSMax Reitz                                       &child_of_bds, BDRV_CHILD_DATA,
1202a16be3cdSMax Reitz                                       &error_abort);
1203afdaeb9eSKevin Wolf     bdrv_graph_wrunlock();
1204231281abSKevin Wolf }
1205231281abSKevin Wolf 
1206903df115SKevin Wolf static void coroutine_mixed_fn detach_by_parent_aio_cb(void *opaque, int ret)
120757320ca9SKevin Wolf {
120857320ca9SKevin Wolf     struct detach_by_parent_data *data = &detach_by_parent_data;
120957320ca9SKevin Wolf 
121057320ca9SKevin Wolf     g_assert_cmpint(ret, ==, 0);
121157320ca9SKevin Wolf     if (data->by_parent_cb) {
1212617f3a96SKevin Wolf         bdrv_inc_in_flight(data->child_b->bs);
1213903df115SKevin Wolf         aio_bh_schedule_oneshot(qemu_get_current_aio_context(),
1214903df115SKevin Wolf                                 detach_indirect_bh, &detach_by_parent_data);
121557320ca9SKevin Wolf     }
121657320ca9SKevin Wolf }
121757320ca9SKevin Wolf 
1218d05ab380SEmanuele Giuseppe Esposito static void GRAPH_RDLOCK detach_by_driver_cb_drained_begin(BdrvChild *child)
121957320ca9SKevin Wolf {
1220617f3a96SKevin Wolf     struct detach_by_parent_data *data = &detach_by_parent_data;
1221617f3a96SKevin Wolf 
1222617f3a96SKevin Wolf     if (!data->detach_on_drain) {
1223617f3a96SKevin Wolf         return;
1224617f3a96SKevin Wolf     }
1225617f3a96SKevin Wolf     data->detach_on_drain = false;
1226617f3a96SKevin Wolf 
1227617f3a96SKevin Wolf     bdrv_inc_in_flight(data->child_b->bs);
122857320ca9SKevin Wolf     aio_bh_schedule_oneshot(qemu_get_current_aio_context(),
122957320ca9SKevin Wolf                             detach_indirect_bh, &detach_by_parent_data);
1230a16be3cdSMax Reitz     child_of_bds.drained_begin(child);
123157320ca9SKevin Wolf }
123257320ca9SKevin Wolf 
1233bd86fb99SMax Reitz static BdrvChildClass detach_by_driver_cb_class;
123457320ca9SKevin Wolf 
1235231281abSKevin Wolf /*
1236231281abSKevin Wolf  * Initial graph:
1237231281abSKevin Wolf  *
1238231281abSKevin Wolf  * PA     PB
1239231281abSKevin Wolf  *    \ /   \
1240231281abSKevin Wolf  *     A     B     C
1241231281abSKevin Wolf  *
124257320ca9SKevin Wolf  * by_parent_cb == true:  Test that parent callbacks don't poll
124357320ca9SKevin Wolf  *
124457320ca9SKevin Wolf  *     PA has a pending write request whose callback changes the child nodes of
124557320ca9SKevin Wolf  *     PB: It removes B and adds C instead. The subtree of PB is drained, which
124657320ca9SKevin Wolf  *     will indirectly drain the write request, too.
124757320ca9SKevin Wolf  *
124857320ca9SKevin Wolf  * by_parent_cb == false: Test that bdrv_drain_invoke() doesn't poll
124957320ca9SKevin Wolf  *
1250bd86fb99SMax Reitz  *     PA's BdrvChildClass has a .drained_begin callback that schedules a BH
125157320ca9SKevin Wolf  *     that does the same graph change. If bdrv_drain_invoke() calls it, the
125257320ca9SKevin Wolf  *     state is messed up, but if it is only polled in the single
125357320ca9SKevin Wolf  *     BDRV_POLL_WHILE() at the end of the drain, this should work fine.
1254231281abSKevin Wolf  */
1255d05ab380SEmanuele Giuseppe Esposito static void TSA_NO_TSA test_detach_indirect(bool by_parent_cb)
1256231281abSKevin Wolf {
1257231281abSKevin Wolf     BlockBackend *blk;
1258231281abSKevin Wolf     BlockDriverState *parent_a, *parent_b, *a, *b, *c;
1259231281abSKevin Wolf     BdrvChild *child_a, *child_b;
1260231281abSKevin Wolf     BlockAIOCB *acb;
1261231281abSKevin Wolf 
1262405d8fe0SVladimir Sementsov-Ogievskiy     QEMUIOVector qiov = QEMU_IOVEC_INIT_BUF(qiov, NULL, 0);
1263231281abSKevin Wolf 
126457320ca9SKevin Wolf     if (!by_parent_cb) {
1265a16be3cdSMax Reitz         detach_by_driver_cb_class = child_of_bds;
1266bd86fb99SMax Reitz         detach_by_driver_cb_class.drained_begin =
126757320ca9SKevin Wolf             detach_by_driver_cb_drained_begin;
1268617f3a96SKevin Wolf         detach_by_driver_cb_class.drained_end = NULL;
1269617f3a96SKevin Wolf         detach_by_driver_cb_class.drained_poll = NULL;
127057320ca9SKevin Wolf     }
127157320ca9SKevin Wolf 
1272617f3a96SKevin Wolf     detach_by_parent_data = (struct detach_by_parent_data) {
1273617f3a96SKevin Wolf         .detach_on_drain = false,
1274617f3a96SKevin Wolf     };
1275617f3a96SKevin Wolf 
1276231281abSKevin Wolf     /* Create all involved nodes */
1277231281abSKevin Wolf     parent_a = bdrv_new_open_driver(&bdrv_test, "parent-a", BDRV_O_RDWR,
1278231281abSKevin Wolf                                     &error_abort);
1279231281abSKevin Wolf     parent_b = bdrv_new_open_driver(&bdrv_test, "parent-b", 0,
1280231281abSKevin Wolf                                     &error_abort);
1281231281abSKevin Wolf 
1282231281abSKevin Wolf     a = bdrv_new_open_driver(&bdrv_test, "a", BDRV_O_RDWR, &error_abort);
1283231281abSKevin Wolf     b = bdrv_new_open_driver(&bdrv_test, "b", BDRV_O_RDWR, &error_abort);
1284231281abSKevin Wolf     c = bdrv_new_open_driver(&bdrv_test, "c", BDRV_O_RDWR, &error_abort);
1285231281abSKevin Wolf 
1286231281abSKevin Wolf     /* blk is a BB for parent-a */
1287d861ab3aSKevin Wolf     blk = blk_new(qemu_get_aio_context(), BLK_PERM_ALL, BLK_PERM_ALL);
1288231281abSKevin Wolf     blk_insert_bs(blk, parent_a, &error_abort);
1289231281abSKevin Wolf     bdrv_unref(parent_a);
1290231281abSKevin Wolf 
129157320ca9SKevin Wolf     /* If we want to get bdrv_drain_invoke() to call aio_poll(), the driver
129257320ca9SKevin Wolf      * callback must not return immediately. */
129357320ca9SKevin Wolf     if (!by_parent_cb) {
129457320ca9SKevin Wolf         BDRVTestState *s = parent_a->opaque;
129557320ca9SKevin Wolf         s->sleep_in_drain_begin = true;
129657320ca9SKevin Wolf     }
129757320ca9SKevin Wolf 
1298231281abSKevin Wolf     /* Set child relationships */
1299231281abSKevin Wolf     bdrv_ref(b);
1300231281abSKevin Wolf     bdrv_ref(a);
1301afdaeb9eSKevin Wolf     bdrv_graph_wrlock(NULL);
1302a16be3cdSMax Reitz     child_b = bdrv_attach_child(parent_b, b, "PB-B", &child_of_bds,
1303a16be3cdSMax Reitz                                 BDRV_CHILD_DATA, &error_abort);
130425191e5fSMax Reitz     child_a = bdrv_attach_child(parent_b, a, "PB-A", &child_of_bds,
130525191e5fSMax Reitz                                 BDRV_CHILD_COW, &error_abort);
1306231281abSKevin Wolf 
1307231281abSKevin Wolf     bdrv_ref(a);
130857320ca9SKevin Wolf     bdrv_attach_child(parent_a, a, "PA-A",
1309a16be3cdSMax Reitz                       by_parent_cb ? &child_of_bds : &detach_by_driver_cb_class,
1310a16be3cdSMax Reitz                       BDRV_CHILD_DATA, &error_abort);
1311afdaeb9eSKevin Wolf     bdrv_graph_wrunlock();
1312231281abSKevin Wolf 
1313231281abSKevin Wolf     g_assert_cmpint(parent_a->refcnt, ==, 1);
1314231281abSKevin Wolf     g_assert_cmpint(parent_b->refcnt, ==, 1);
1315231281abSKevin Wolf     g_assert_cmpint(a->refcnt, ==, 3);
1316231281abSKevin Wolf     g_assert_cmpint(b->refcnt, ==, 2);
1317231281abSKevin Wolf     g_assert_cmpint(c->refcnt, ==, 1);
1318231281abSKevin Wolf 
1319231281abSKevin Wolf     g_assert(QLIST_FIRST(&parent_b->children) == child_a);
1320231281abSKevin Wolf     g_assert(QLIST_NEXT(child_a, next) == child_b);
1321231281abSKevin Wolf     g_assert(QLIST_NEXT(child_b, next) == NULL);
1322231281abSKevin Wolf 
1323231281abSKevin Wolf     /* Start the evil write request */
132457320ca9SKevin Wolf     detach_by_parent_data = (struct detach_by_parent_data) {
1325231281abSKevin Wolf         .parent_b = parent_b,
1326231281abSKevin Wolf         .child_b = child_b,
1327231281abSKevin Wolf         .c = c,
132857320ca9SKevin Wolf         .by_parent_cb = by_parent_cb,
1329617f3a96SKevin Wolf         .detach_on_drain = true,
1330231281abSKevin Wolf     };
133157320ca9SKevin Wolf     acb = blk_aio_preadv(blk, 0, &qiov, 0, detach_by_parent_aio_cb, NULL);
1332231281abSKevin Wolf     g_assert(acb != NULL);
1333231281abSKevin Wolf 
1334231281abSKevin Wolf     /* Drain and check the expected result */
1335299403aeSKevin Wolf     bdrv_drained_begin(parent_b);
1336299403aeSKevin Wolf     bdrv_drained_begin(a);
1337299403aeSKevin Wolf     bdrv_drained_begin(b);
1338299403aeSKevin Wolf     bdrv_drained_begin(c);
1339231281abSKevin Wolf 
134057320ca9SKevin Wolf     g_assert(detach_by_parent_data.child_c != NULL);
1341231281abSKevin Wolf 
1342231281abSKevin Wolf     g_assert_cmpint(parent_a->refcnt, ==, 1);
1343231281abSKevin Wolf     g_assert_cmpint(parent_b->refcnt, ==, 1);
1344231281abSKevin Wolf     g_assert_cmpint(a->refcnt, ==, 3);
1345231281abSKevin Wolf     g_assert_cmpint(b->refcnt, ==, 1);
1346231281abSKevin Wolf     g_assert_cmpint(c->refcnt, ==, 2);
1347231281abSKevin Wolf 
134857320ca9SKevin Wolf     g_assert(QLIST_FIRST(&parent_b->children) == detach_by_parent_data.child_c);
134957320ca9SKevin Wolf     g_assert(QLIST_NEXT(detach_by_parent_data.child_c, next) == child_a);
1350231281abSKevin Wolf     g_assert(QLIST_NEXT(child_a, next) == NULL);
1351231281abSKevin Wolf 
1352231281abSKevin Wolf     g_assert_cmpint(parent_a->quiesce_counter, ==, 1);
1353299403aeSKevin Wolf     g_assert_cmpint(parent_b->quiesce_counter, ==, 3);
1354231281abSKevin Wolf     g_assert_cmpint(a->quiesce_counter, ==, 1);
1355299403aeSKevin Wolf     g_assert_cmpint(b->quiesce_counter, ==, 1);
1356231281abSKevin Wolf     g_assert_cmpint(c->quiesce_counter, ==, 1);
1357231281abSKevin Wolf 
1358299403aeSKevin Wolf     bdrv_drained_end(parent_b);
1359299403aeSKevin Wolf     bdrv_drained_end(a);
1360299403aeSKevin Wolf     bdrv_drained_end(b);
1361299403aeSKevin Wolf     bdrv_drained_end(c);
1362231281abSKevin Wolf 
1363231281abSKevin Wolf     bdrv_unref(parent_b);
1364231281abSKevin Wolf     blk_unref(blk);
1365231281abSKevin Wolf 
1366231281abSKevin Wolf     g_assert_cmpint(a->refcnt, ==, 1);
1367231281abSKevin Wolf     g_assert_cmpint(b->refcnt, ==, 1);
1368231281abSKevin Wolf     g_assert_cmpint(c->refcnt, ==, 1);
1369231281abSKevin Wolf     bdrv_unref(a);
1370231281abSKevin Wolf     bdrv_unref(b);
1371231281abSKevin Wolf     bdrv_unref(c);
1372231281abSKevin Wolf }
1373231281abSKevin Wolf 
137457320ca9SKevin Wolf static void test_detach_by_parent_cb(void)
137557320ca9SKevin Wolf {
137657320ca9SKevin Wolf     test_detach_indirect(true);
137757320ca9SKevin Wolf }
137857320ca9SKevin Wolf 
137957320ca9SKevin Wolf static void test_detach_by_driver_cb(void)
138057320ca9SKevin Wolf {
138157320ca9SKevin Wolf     test_detach_indirect(false);
138257320ca9SKevin Wolf }
1383231281abSKevin Wolf 
1384b994c5bcSKevin Wolf static void test_append_to_drained(void)
1385b994c5bcSKevin Wolf {
1386b994c5bcSKevin Wolf     BlockBackend *blk;
1387b994c5bcSKevin Wolf     BlockDriverState *base, *overlay;
1388b994c5bcSKevin Wolf     BDRVTestState *base_s, *overlay_s;
1389b994c5bcSKevin Wolf 
1390d861ab3aSKevin Wolf     blk = blk_new(qemu_get_aio_context(), BLK_PERM_ALL, BLK_PERM_ALL);
1391b994c5bcSKevin Wolf     base = bdrv_new_open_driver(&bdrv_test, "base", BDRV_O_RDWR, &error_abort);
1392b994c5bcSKevin Wolf     base_s = base->opaque;
1393b994c5bcSKevin Wolf     blk_insert_bs(blk, base, &error_abort);
1394b994c5bcSKevin Wolf 
1395b994c5bcSKevin Wolf     overlay = bdrv_new_open_driver(&bdrv_test, "overlay", BDRV_O_RDWR,
1396b994c5bcSKevin Wolf                                    &error_abort);
1397b994c5bcSKevin Wolf     overlay_s = overlay->opaque;
1398b994c5bcSKevin Wolf 
1399b994c5bcSKevin Wolf     do_drain_begin(BDRV_DRAIN, base);
1400b994c5bcSKevin Wolf     g_assert_cmpint(base->quiesce_counter, ==, 1);
1401b994c5bcSKevin Wolf     g_assert_cmpint(base_s->drain_count, ==, 1);
1402b994c5bcSKevin Wolf     g_assert_cmpint(base->in_flight, ==, 0);
1403b994c5bcSKevin Wolf 
1404487b9187SKevin Wolf     aio_context_acquire(qemu_get_aio_context());
1405b994c5bcSKevin Wolf     bdrv_append(overlay, base, &error_abort);
1406487b9187SKevin Wolf     aio_context_release(qemu_get_aio_context());
1407487b9187SKevin Wolf 
1408b994c5bcSKevin Wolf     g_assert_cmpint(base->in_flight, ==, 0);
1409b994c5bcSKevin Wolf     g_assert_cmpint(overlay->in_flight, ==, 0);
1410b994c5bcSKevin Wolf 
1411b994c5bcSKevin Wolf     g_assert_cmpint(base->quiesce_counter, ==, 1);
1412b994c5bcSKevin Wolf     g_assert_cmpint(base_s->drain_count, ==, 1);
1413b994c5bcSKevin Wolf     g_assert_cmpint(overlay->quiesce_counter, ==, 1);
1414b994c5bcSKevin Wolf     g_assert_cmpint(overlay_s->drain_count, ==, 1);
1415b994c5bcSKevin Wolf 
1416b994c5bcSKevin Wolf     do_drain_end(BDRV_DRAIN, base);
1417b994c5bcSKevin Wolf 
1418b994c5bcSKevin Wolf     g_assert_cmpint(base->quiesce_counter, ==, 0);
1419b994c5bcSKevin Wolf     g_assert_cmpint(base_s->drain_count, ==, 0);
1420b994c5bcSKevin Wolf     g_assert_cmpint(overlay->quiesce_counter, ==, 0);
1421b994c5bcSKevin Wolf     g_assert_cmpint(overlay_s->drain_count, ==, 0);
1422b994c5bcSKevin Wolf 
1423ae9d4417SVladimir Sementsov-Ogievskiy     bdrv_unref(overlay);
1424b994c5bcSKevin Wolf     bdrv_unref(base);
1425b994c5bcSKevin Wolf     blk_unref(blk);
1426b994c5bcSKevin Wolf }
1427b994c5bcSKevin Wolf 
1428247d2737SKevin Wolf static void test_set_aio_context(void)
1429247d2737SKevin Wolf {
1430247d2737SKevin Wolf     BlockDriverState *bs;
1431247d2737SKevin Wolf     IOThread *a = iothread_new();
1432247d2737SKevin Wolf     IOThread *b = iothread_new();
1433247d2737SKevin Wolf     AioContext *ctx_a = iothread_get_aio_context(a);
1434247d2737SKevin Wolf     AioContext *ctx_b = iothread_get_aio_context(b);
1435247d2737SKevin Wolf 
1436247d2737SKevin Wolf     bs = bdrv_new_open_driver(&bdrv_test, "test-node", BDRV_O_RDWR,
1437247d2737SKevin Wolf                               &error_abort);
1438247d2737SKevin Wolf 
1439247d2737SKevin Wolf     bdrv_drained_begin(bs);
1440142e6907SEmanuele Giuseppe Esposito     bdrv_try_change_aio_context(bs, ctx_a, NULL, &error_abort);
1441247d2737SKevin Wolf 
1442247d2737SKevin Wolf     aio_context_acquire(ctx_a);
1443247d2737SKevin Wolf     bdrv_drained_end(bs);
1444247d2737SKevin Wolf 
1445247d2737SKevin Wolf     bdrv_drained_begin(bs);
1446142e6907SEmanuele Giuseppe Esposito     bdrv_try_change_aio_context(bs, ctx_b, NULL, &error_abort);
1447247d2737SKevin Wolf     aio_context_release(ctx_a);
1448247d2737SKevin Wolf     aio_context_acquire(ctx_b);
1449142e6907SEmanuele Giuseppe Esposito     bdrv_try_change_aio_context(bs, qemu_get_aio_context(), NULL, &error_abort);
1450247d2737SKevin Wolf     aio_context_release(ctx_b);
1451247d2737SKevin Wolf     bdrv_drained_end(bs);
1452247d2737SKevin Wolf 
1453247d2737SKevin Wolf     bdrv_unref(bs);
1454247d2737SKevin Wolf     iothread_join(a);
1455247d2737SKevin Wolf     iothread_join(b);
1456247d2737SKevin Wolf }
1457247d2737SKevin Wolf 
14588e442810SMax Reitz 
14598e442810SMax Reitz typedef struct TestDropBackingBlockJob {
14608e442810SMax Reitz     BlockJob common;
14618e442810SMax Reitz     bool should_complete;
14628e442810SMax Reitz     bool *did_complete;
14632afdc790SMax Reitz     BlockDriverState *detach_also;
14641b177bbeSVladimir Sementsov-Ogievskiy     BlockDriverState *bs;
14658e442810SMax Reitz } TestDropBackingBlockJob;
14668e442810SMax Reitz 
14678e442810SMax Reitz static int coroutine_fn test_drop_backing_job_run(Job *job, Error **errp)
14688e442810SMax Reitz {
14698e442810SMax Reitz     TestDropBackingBlockJob *s =
14708e442810SMax Reitz         container_of(job, TestDropBackingBlockJob, common.job);
14718e442810SMax Reitz 
14728e442810SMax Reitz     while (!s->should_complete) {
14738e442810SMax Reitz         job_sleep_ns(job, 0);
14748e442810SMax Reitz     }
14758e442810SMax Reitz 
14768e442810SMax Reitz     return 0;
14778e442810SMax Reitz }
14788e442810SMax Reitz 
14798e442810SMax Reitz static void test_drop_backing_job_commit(Job *job)
14808e442810SMax Reitz {
14818e442810SMax Reitz     TestDropBackingBlockJob *s =
14828e442810SMax Reitz         container_of(job, TestDropBackingBlockJob, common.job);
14838e442810SMax Reitz 
14841b177bbeSVladimir Sementsov-Ogievskiy     bdrv_set_backing_hd(s->bs, NULL, &error_abort);
14852afdc790SMax Reitz     bdrv_set_backing_hd(s->detach_also, NULL, &error_abort);
14868e442810SMax Reitz 
14878e442810SMax Reitz     *s->did_complete = true;
14888e442810SMax Reitz }
14898e442810SMax Reitz 
14908e442810SMax Reitz static const BlockJobDriver test_drop_backing_job_driver = {
14918e442810SMax Reitz     .job_driver = {
14928e442810SMax Reitz         .instance_size  = sizeof(TestDropBackingBlockJob),
14938e442810SMax Reitz         .free           = block_job_free,
14948e442810SMax Reitz         .user_resume    = block_job_user_resume,
14958e442810SMax Reitz         .run            = test_drop_backing_job_run,
14968e442810SMax Reitz         .commit         = test_drop_backing_job_commit,
14978e442810SMax Reitz     }
14988e442810SMax Reitz };
14998e442810SMax Reitz 
15008e442810SMax Reitz /**
15018e442810SMax Reitz  * Creates a child node with three parent nodes on it, and then runs a
15028e442810SMax Reitz  * block job on the final one, parent-node-2.
15038e442810SMax Reitz  *
15048e442810SMax Reitz  * The job is then asked to complete before a section where the child
15058e442810SMax Reitz  * is drained.
15068e442810SMax Reitz  *
15078e442810SMax Reitz  * Ending this section will undrain the child's parents, first
15088e442810SMax Reitz  * parent-node-2, then parent-node-1, then parent-node-0 -- the parent
15098e442810SMax Reitz  * list is in reverse order of how they were added.  Ending the drain
15108e442810SMax Reitz  * on parent-node-2 will resume the job, thus completing it and
15118e442810SMax Reitz  * scheduling job_exit().
15128e442810SMax Reitz  *
15138e442810SMax Reitz  * Ending the drain on parent-node-1 will poll the AioContext, which
15148e442810SMax Reitz  * lets job_exit() and thus test_drop_backing_job_commit() run.  That
15152afdc790SMax Reitz  * function first removes the child as parent-node-2's backing file.
15168e442810SMax Reitz  *
15178e442810SMax Reitz  * In old (and buggy) implementations, there are two problems with
15188e442810SMax Reitz  * that:
15198e442810SMax Reitz  * (A) bdrv_drain_invoke() polls for every node that leaves the
15208e442810SMax Reitz  *     drained section.  This means that job_exit() is scheduled
15218e442810SMax Reitz  *     before the child has left the drained section.  Its
15228e442810SMax Reitz  *     quiesce_counter is therefore still 1 when it is removed from
15238e442810SMax Reitz  *     parent-node-2.
15248e442810SMax Reitz  *
15258e442810SMax Reitz  * (B) bdrv_replace_child_noperm() calls drained_end() on the old
15268e442810SMax Reitz  *     child's parents as many times as the child is quiesced.  This
15278e442810SMax Reitz  *     means it will call drained_end() on parent-node-2 once.
15288e442810SMax Reitz  *     Because parent-node-2 is no longer quiesced at this point, this
15298e442810SMax Reitz  *     will fail.
15308e442810SMax Reitz  *
15318e442810SMax Reitz  * bdrv_replace_child_noperm() therefore must call drained_end() on
15328e442810SMax Reitz  * the parent only if it really is still drained because the child is
15338e442810SMax Reitz  * drained.
15342afdc790SMax Reitz  *
15352afdc790SMax Reitz  * If removing child from parent-node-2 was successful (as it should
15362afdc790SMax Reitz  * be), test_drop_backing_job_commit() will then also remove the child
15372afdc790SMax Reitz  * from parent-node-0.
15382afdc790SMax Reitz  *
15392afdc790SMax Reitz  * With an old version of our drain infrastructure ((A) above), that
15402afdc790SMax Reitz  * resulted in the following flow:
15412afdc790SMax Reitz  *
15422afdc790SMax Reitz  * 1. child attempts to leave its drained section.  The call recurses
15432afdc790SMax Reitz  *    to its parents.
15442afdc790SMax Reitz  *
15452afdc790SMax Reitz  * 2. parent-node-2 leaves the drained section.  Polling in
15462afdc790SMax Reitz  *    bdrv_drain_invoke() will schedule job_exit().
15472afdc790SMax Reitz  *
15482afdc790SMax Reitz  * 3. parent-node-1 leaves the drained section.  Polling in
15492afdc790SMax Reitz  *    bdrv_drain_invoke() will run job_exit(), thus disconnecting
15502afdc790SMax Reitz  *    parent-node-0 from the child node.
15512afdc790SMax Reitz  *
15522afdc790SMax Reitz  * 4. bdrv_parent_drained_end() uses a QLIST_FOREACH_SAFE() loop to
15532afdc790SMax Reitz  *    iterate over the parents.  Thus, it now accesses the BdrvChild
15542afdc790SMax Reitz  *    object that used to connect parent-node-0 and the child node.
15552afdc790SMax Reitz  *    However, that object no longer exists, so it accesses a dangling
15562afdc790SMax Reitz  *    pointer.
15572afdc790SMax Reitz  *
15582afdc790SMax Reitz  * The solution is to only poll once when running a bdrv_drained_end()
15592afdc790SMax Reitz  * operation, specifically at the end when all drained_end()
15602afdc790SMax Reitz  * operations for all involved nodes have been scheduled.
15612afdc790SMax Reitz  * Note that this also solves (A) above, thus hiding (B).
15628e442810SMax Reitz  */
15638e442810SMax Reitz static void test_blockjob_commit_by_drained_end(void)
15648e442810SMax Reitz {
15658e442810SMax Reitz     BlockDriverState *bs_child, *bs_parents[3];
15668e442810SMax Reitz     TestDropBackingBlockJob *job;
15678e442810SMax Reitz     bool job_has_completed = false;
15688e442810SMax Reitz     int i;
15698e442810SMax Reitz 
15708e442810SMax Reitz     bs_child = bdrv_new_open_driver(&bdrv_test, "child-node", BDRV_O_RDWR,
15718e442810SMax Reitz                                     &error_abort);
15728e442810SMax Reitz 
15738e442810SMax Reitz     for (i = 0; i < 3; i++) {
15748e442810SMax Reitz         char name[32];
15758e442810SMax Reitz         snprintf(name, sizeof(name), "parent-node-%i", i);
15768e442810SMax Reitz         bs_parents[i] = bdrv_new_open_driver(&bdrv_test, name, BDRV_O_RDWR,
15778e442810SMax Reitz                                              &error_abort);
15788e442810SMax Reitz         bdrv_set_backing_hd(bs_parents[i], bs_child, &error_abort);
15798e442810SMax Reitz     }
15808e442810SMax Reitz 
15818e442810SMax Reitz     job = block_job_create("job", &test_drop_backing_job_driver, NULL,
15828e442810SMax Reitz                            bs_parents[2], 0, BLK_PERM_ALL, 0, 0, NULL, NULL,
15838e442810SMax Reitz                            &error_abort);
15841b177bbeSVladimir Sementsov-Ogievskiy     job->bs = bs_parents[2];
15858e442810SMax Reitz 
15862afdc790SMax Reitz     job->detach_also = bs_parents[0];
15878e442810SMax Reitz     job->did_complete = &job_has_completed;
15888e442810SMax Reitz 
15898e442810SMax Reitz     job_start(&job->common.job);
15908e442810SMax Reitz 
15918e442810SMax Reitz     job->should_complete = true;
15928e442810SMax Reitz     bdrv_drained_begin(bs_child);
15938e442810SMax Reitz     g_assert(!job_has_completed);
15948e442810SMax Reitz     bdrv_drained_end(bs_child);
15955e8ac217SKevin Wolf     aio_poll(qemu_get_aio_context(), false);
15968e442810SMax Reitz     g_assert(job_has_completed);
15978e442810SMax Reitz 
15988e442810SMax Reitz     bdrv_unref(bs_parents[0]);
15998e442810SMax Reitz     bdrv_unref(bs_parents[1]);
16008e442810SMax Reitz     bdrv_unref(bs_parents[2]);
16018e442810SMax Reitz     bdrv_unref(bs_child);
16028e442810SMax Reitz }
16038e442810SMax Reitz 
16049746b35cSMax Reitz 
16059746b35cSMax Reitz typedef struct TestSimpleBlockJob {
16069746b35cSMax Reitz     BlockJob common;
16079746b35cSMax Reitz     bool should_complete;
16089746b35cSMax Reitz     bool *did_complete;
16099746b35cSMax Reitz } TestSimpleBlockJob;
16109746b35cSMax Reitz 
16119746b35cSMax Reitz static int coroutine_fn test_simple_job_run(Job *job, Error **errp)
16129746b35cSMax Reitz {
16139746b35cSMax Reitz     TestSimpleBlockJob *s = container_of(job, TestSimpleBlockJob, common.job);
16149746b35cSMax Reitz 
16159746b35cSMax Reitz     while (!s->should_complete) {
16169746b35cSMax Reitz         job_sleep_ns(job, 0);
16179746b35cSMax Reitz     }
16189746b35cSMax Reitz 
16199746b35cSMax Reitz     return 0;
16209746b35cSMax Reitz }
16219746b35cSMax Reitz 
16229746b35cSMax Reitz static void test_simple_job_clean(Job *job)
16239746b35cSMax Reitz {
16249746b35cSMax Reitz     TestSimpleBlockJob *s = container_of(job, TestSimpleBlockJob, common.job);
16259746b35cSMax Reitz     *s->did_complete = true;
16269746b35cSMax Reitz }
16279746b35cSMax Reitz 
16289746b35cSMax Reitz static const BlockJobDriver test_simple_job_driver = {
16299746b35cSMax Reitz     .job_driver = {
16309746b35cSMax Reitz         .instance_size  = sizeof(TestSimpleBlockJob),
16319746b35cSMax Reitz         .free           = block_job_free,
16329746b35cSMax Reitz         .user_resume    = block_job_user_resume,
16339746b35cSMax Reitz         .run            = test_simple_job_run,
16349746b35cSMax Reitz         .clean          = test_simple_job_clean,
16359746b35cSMax Reitz     },
16369746b35cSMax Reitz };
16379746b35cSMax Reitz 
16389746b35cSMax Reitz static int drop_intermediate_poll_update_filename(BdrvChild *child,
16399746b35cSMax Reitz                                                   BlockDriverState *new_base,
16409746b35cSMax Reitz                                                   const char *filename,
16419746b35cSMax Reitz                                                   Error **errp)
16429746b35cSMax Reitz {
16439746b35cSMax Reitz     /*
16449746b35cSMax Reitz      * We are free to poll here, which may change the block graph, if
16459746b35cSMax Reitz      * it is not drained.
16469746b35cSMax Reitz      */
16479746b35cSMax Reitz 
16489746b35cSMax Reitz     /* If the job is not drained: Complete it, schedule job_exit() */
16499746b35cSMax Reitz     aio_poll(qemu_get_current_aio_context(), false);
16509746b35cSMax Reitz     /* If the job is not drained: Run job_exit(), finish the job */
16519746b35cSMax Reitz     aio_poll(qemu_get_current_aio_context(), false);
16529746b35cSMax Reitz 
16539746b35cSMax Reitz     return 0;
16549746b35cSMax Reitz }
16559746b35cSMax Reitz 
16569746b35cSMax Reitz /**
16579746b35cSMax Reitz  * Test a poll in the midst of bdrv_drop_intermediate().
16589746b35cSMax Reitz  *
1659bd86fb99SMax Reitz  * bdrv_drop_intermediate() calls BdrvChildClass.update_filename(),
16609746b35cSMax Reitz  * which can yield or poll.  This may lead to graph changes, unless
16619746b35cSMax Reitz  * the whole subtree in question is drained.
16629746b35cSMax Reitz  *
16639746b35cSMax Reitz  * We test this on the following graph:
16649746b35cSMax Reitz  *
16659746b35cSMax Reitz  *                    Job
16669746b35cSMax Reitz  *
16679746b35cSMax Reitz  *                     |
16689746b35cSMax Reitz  *                  job-node
16699746b35cSMax Reitz  *                     |
16709746b35cSMax Reitz  *                     v
16719746b35cSMax Reitz  *
16729746b35cSMax Reitz  *                  job-node
16739746b35cSMax Reitz  *
16749746b35cSMax Reitz  *                     |
16759746b35cSMax Reitz  *                  backing
16769746b35cSMax Reitz  *                     |
16779746b35cSMax Reitz  *                     v
16789746b35cSMax Reitz  *
16799746b35cSMax Reitz  * node-2 --chain--> node-1 --chain--> node-0
16809746b35cSMax Reitz  *
16819746b35cSMax Reitz  * We drop node-1 with bdrv_drop_intermediate(top=node-1, base=node-0).
16829746b35cSMax Reitz  *
16839746b35cSMax Reitz  * This first updates node-2's backing filename by invoking
16849746b35cSMax Reitz  * drop_intermediate_poll_update_filename(), which polls twice.  This
16859746b35cSMax Reitz  * causes the job to finish, which in turns causes the job-node to be
16869746b35cSMax Reitz  * deleted.
16879746b35cSMax Reitz  *
16889746b35cSMax Reitz  * bdrv_drop_intermediate() uses a QLIST_FOREACH_SAFE() loop, so it
16899746b35cSMax Reitz  * already has a pointer to the BdrvChild edge between job-node and
16909746b35cSMax Reitz  * node-1.  When it tries to handle that edge, we probably get a
16919746b35cSMax Reitz  * segmentation fault because the object no longer exists.
16929746b35cSMax Reitz  *
16939746b35cSMax Reitz  *
16949746b35cSMax Reitz  * The solution is for bdrv_drop_intermediate() to drain top's
16959746b35cSMax Reitz  * subtree.  This prevents graph changes from happening just because
1696bd86fb99SMax Reitz  * BdrvChildClass.update_filename() yields or polls.  Thus, the block
16979746b35cSMax Reitz  * job is paused during that drained section and must finish before or
16989746b35cSMax Reitz  * after.
16999746b35cSMax Reitz  *
17009746b35cSMax Reitz  * (In addition, bdrv_replace_child() must keep the job paused.)
17019746b35cSMax Reitz  */
17029746b35cSMax Reitz static void test_drop_intermediate_poll(void)
17039746b35cSMax Reitz {
1704bd86fb99SMax Reitz     static BdrvChildClass chain_child_class;
17059746b35cSMax Reitz     BlockDriverState *chain[3];
17069746b35cSMax Reitz     TestSimpleBlockJob *job;
17079746b35cSMax Reitz     BlockDriverState *job_node;
17089746b35cSMax Reitz     bool job_has_completed = false;
17099746b35cSMax Reitz     int i;
17109746b35cSMax Reitz     int ret;
17119746b35cSMax Reitz 
171225191e5fSMax Reitz     chain_child_class = child_of_bds;
1713bd86fb99SMax Reitz     chain_child_class.update_filename = drop_intermediate_poll_update_filename;
17149746b35cSMax Reitz 
17159746b35cSMax Reitz     for (i = 0; i < 3; i++) {
17169746b35cSMax Reitz         char name[32];
17179746b35cSMax Reitz         snprintf(name, 32, "node-%i", i);
17189746b35cSMax Reitz 
17199746b35cSMax Reitz         chain[i] = bdrv_new_open_driver(&bdrv_test, name, 0, &error_abort);
17209746b35cSMax Reitz     }
17219746b35cSMax Reitz 
17229746b35cSMax Reitz     job_node = bdrv_new_open_driver(&bdrv_test, "job-node", BDRV_O_RDWR,
17239746b35cSMax Reitz                                     &error_abort);
17249746b35cSMax Reitz     bdrv_set_backing_hd(job_node, chain[1], &error_abort);
17259746b35cSMax Reitz 
17269746b35cSMax Reitz     /*
17279746b35cSMax Reitz      * Establish the chain last, so the chain links are the first
17289746b35cSMax Reitz      * elements in the BDS.parents lists
17299746b35cSMax Reitz      */
1730afdaeb9eSKevin Wolf     bdrv_graph_wrlock(NULL);
17319746b35cSMax Reitz     for (i = 0; i < 3; i++) {
17329746b35cSMax Reitz         if (i) {
17339746b35cSMax Reitz             /* Takes the reference to chain[i - 1] */
17345bb04747SVladimir Sementsov-Ogievskiy             bdrv_attach_child(chain[i], chain[i - 1], "chain",
17355bb04747SVladimir Sementsov-Ogievskiy                               &chain_child_class, BDRV_CHILD_COW, &error_abort);
17369746b35cSMax Reitz         }
17379746b35cSMax Reitz     }
1738afdaeb9eSKevin Wolf     bdrv_graph_wrunlock();
17399746b35cSMax Reitz 
17409746b35cSMax Reitz     job = block_job_create("job", &test_simple_job_driver, NULL, job_node,
17419746b35cSMax Reitz                            0, BLK_PERM_ALL, 0, 0, NULL, NULL, &error_abort);
17429746b35cSMax Reitz 
17439746b35cSMax Reitz     /* The job has a reference now */
17449746b35cSMax Reitz     bdrv_unref(job_node);
17459746b35cSMax Reitz 
17469746b35cSMax Reitz     job->did_complete = &job_has_completed;
17479746b35cSMax Reitz 
17489746b35cSMax Reitz     job_start(&job->common.job);
17499746b35cSMax Reitz     job->should_complete = true;
17509746b35cSMax Reitz 
17519746b35cSMax Reitz     g_assert(!job_has_completed);
17529746b35cSMax Reitz     ret = bdrv_drop_intermediate(chain[1], chain[0], NULL);
17535e8ac217SKevin Wolf     aio_poll(qemu_get_aio_context(), false);
17549746b35cSMax Reitz     g_assert(ret == 0);
17559746b35cSMax Reitz     g_assert(job_has_completed);
17569746b35cSMax Reitz 
17579746b35cSMax Reitz     bdrv_unref(chain[2]);
17589746b35cSMax Reitz }
17599746b35cSMax Reitz 
17600513f984SMax Reitz 
17610513f984SMax Reitz typedef struct BDRVReplaceTestState {
176223987471SKevin Wolf     bool setup_completed;
17630513f984SMax Reitz     bool was_drained;
17640513f984SMax Reitz     bool was_undrained;
17650513f984SMax Reitz     bool has_read;
17660513f984SMax Reitz 
17670513f984SMax Reitz     int drain_count;
17680513f984SMax Reitz 
17690513f984SMax Reitz     bool yield_before_read;
17700513f984SMax Reitz     Coroutine *io_co;
17710513f984SMax Reitz     Coroutine *drain_co;
17720513f984SMax Reitz } BDRVReplaceTestState;
17730513f984SMax Reitz 
17740513f984SMax Reitz static void bdrv_replace_test_close(BlockDriverState *bs)
17750513f984SMax Reitz {
17760513f984SMax Reitz }
17770513f984SMax Reitz 
17780513f984SMax Reitz /**
17790513f984SMax Reitz  * If @bs has a backing file:
17800513f984SMax Reitz  *   Yield if .yield_before_read is true (and wait for drain_begin to
17810513f984SMax Reitz  *   wake us up).
17820513f984SMax Reitz  *   Forward the read to bs->backing.  Set .has_read to true.
17830513f984SMax Reitz  *   If drain_begin has woken us, wake it in turn.
17840513f984SMax Reitz  *
17850513f984SMax Reitz  * Otherwise:
17860513f984SMax Reitz  *   Set .has_read to true and return success.
17870513f984SMax Reitz  */
1788b9b10c35SKevin Wolf static int coroutine_fn GRAPH_RDLOCK
1789b9b10c35SKevin Wolf bdrv_replace_test_co_preadv(BlockDriverState *bs, int64_t offset, int64_t bytes,
1790b9b10c35SKevin Wolf                             QEMUIOVector *qiov, BdrvRequestFlags flags)
17910513f984SMax Reitz {
17920513f984SMax Reitz     BDRVReplaceTestState *s = bs->opaque;
17930513f984SMax Reitz 
17940513f984SMax Reitz     if (bs->backing) {
17950513f984SMax Reitz         int ret;
17960513f984SMax Reitz 
17970513f984SMax Reitz         g_assert(!s->drain_count);
17980513f984SMax Reitz 
17990513f984SMax Reitz         s->io_co = qemu_coroutine_self();
18000513f984SMax Reitz         if (s->yield_before_read) {
18010513f984SMax Reitz             s->yield_before_read = false;
18020513f984SMax Reitz             qemu_coroutine_yield();
18030513f984SMax Reitz         }
18040513f984SMax Reitz         s->io_co = NULL;
18050513f984SMax Reitz 
1806fae2681aSVladimir Sementsov-Ogievskiy         ret = bdrv_co_preadv(bs->backing, offset, bytes, qiov, 0);
18070513f984SMax Reitz         s->has_read = true;
18080513f984SMax Reitz 
18090513f984SMax Reitz         /* Wake up drain_co if it runs */
18100513f984SMax Reitz         if (s->drain_co) {
18110513f984SMax Reitz             aio_co_wake(s->drain_co);
18120513f984SMax Reitz         }
18130513f984SMax Reitz 
18140513f984SMax Reitz         return ret;
18150513f984SMax Reitz     }
18160513f984SMax Reitz 
18170513f984SMax Reitz     s->has_read = true;
18180513f984SMax Reitz     return 0;
18190513f984SMax Reitz }
18200513f984SMax Reitz 
18217bce1c29SKevin Wolf static void coroutine_fn bdrv_replace_test_drain_co(void *opaque)
18227bce1c29SKevin Wolf {
18237bce1c29SKevin Wolf     BlockDriverState *bs = opaque;
18247bce1c29SKevin Wolf     BDRVReplaceTestState *s = bs->opaque;
18257bce1c29SKevin Wolf 
18267bce1c29SKevin Wolf     /* Keep waking io_co up until it is done */
18277bce1c29SKevin Wolf     while (s->io_co) {
18287bce1c29SKevin Wolf         aio_co_wake(s->io_co);
18297bce1c29SKevin Wolf         s->io_co = NULL;
18307bce1c29SKevin Wolf         qemu_coroutine_yield();
18317bce1c29SKevin Wolf     }
18327bce1c29SKevin Wolf     s->drain_co = NULL;
18337bce1c29SKevin Wolf     bdrv_dec_in_flight(bs);
18347bce1c29SKevin Wolf }
18357bce1c29SKevin Wolf 
18360513f984SMax Reitz /**
18370513f984SMax Reitz  * If .drain_count is 0, wake up .io_co if there is one; and set
18380513f984SMax Reitz  * .was_drained.
18390513f984SMax Reitz  * Increment .drain_count.
18400513f984SMax Reitz  */
18415e8ac217SKevin Wolf static void bdrv_replace_test_drain_begin(BlockDriverState *bs)
18420513f984SMax Reitz {
18430513f984SMax Reitz     BDRVReplaceTestState *s = bs->opaque;
18440513f984SMax Reitz 
184523987471SKevin Wolf     if (!s->setup_completed) {
184623987471SKevin Wolf         return;
184723987471SKevin Wolf     }
184823987471SKevin Wolf 
18490513f984SMax Reitz     if (!s->drain_count) {
18507bce1c29SKevin Wolf         s->drain_co = qemu_coroutine_create(bdrv_replace_test_drain_co, bs);
18517bce1c29SKevin Wolf         bdrv_inc_in_flight(bs);
18527bce1c29SKevin Wolf         aio_co_enter(bdrv_get_aio_context(bs), s->drain_co);
18530513f984SMax Reitz         s->was_drained = true;
18540513f984SMax Reitz     }
18550513f984SMax Reitz     s->drain_count++;
18560513f984SMax Reitz }
18570513f984SMax Reitz 
18587bce1c29SKevin Wolf static void coroutine_fn bdrv_replace_test_read_entry(void *opaque)
18597bce1c29SKevin Wolf {
18607bce1c29SKevin Wolf     BlockDriverState *bs = opaque;
18617bce1c29SKevin Wolf     char data;
18627bce1c29SKevin Wolf     QEMUIOVector qiov = QEMU_IOVEC_INIT_BUF(qiov, &data, 1);
18637bce1c29SKevin Wolf     int ret;
18647bce1c29SKevin Wolf 
18657bce1c29SKevin Wolf     /* Queue a read request post-drain */
1866b9b10c35SKevin Wolf     bdrv_graph_co_rdlock();
18677bce1c29SKevin Wolf     ret = bdrv_replace_test_co_preadv(bs, 0, 1, &qiov, 0);
1868b9b10c35SKevin Wolf     bdrv_graph_co_rdunlock();
1869b9b10c35SKevin Wolf 
18707bce1c29SKevin Wolf     g_assert(ret >= 0);
18717bce1c29SKevin Wolf     bdrv_dec_in_flight(bs);
18727bce1c29SKevin Wolf }
18737bce1c29SKevin Wolf 
18740513f984SMax Reitz /**
18750513f984SMax Reitz  * Reduce .drain_count, set .was_undrained once it reaches 0.
18760513f984SMax Reitz  * If .drain_count reaches 0 and the node has a backing file, issue a
18770513f984SMax Reitz  * read request.
18780513f984SMax Reitz  */
18795e8ac217SKevin Wolf static void bdrv_replace_test_drain_end(BlockDriverState *bs)
18800513f984SMax Reitz {
18810513f984SMax Reitz     BDRVReplaceTestState *s = bs->opaque;
18820513f984SMax Reitz 
1883004915a9SKevin Wolf     GRAPH_RDLOCK_GUARD_MAINLOOP();
1884004915a9SKevin Wolf 
188523987471SKevin Wolf     if (!s->setup_completed) {
188623987471SKevin Wolf         return;
188723987471SKevin Wolf     }
188823987471SKevin Wolf 
18890513f984SMax Reitz     g_assert(s->drain_count > 0);
18900513f984SMax Reitz     if (!--s->drain_count) {
18910513f984SMax Reitz         s->was_undrained = true;
18920513f984SMax Reitz 
18930513f984SMax Reitz         if (bs->backing) {
18947bce1c29SKevin Wolf             Coroutine *co = qemu_coroutine_create(bdrv_replace_test_read_entry,
18957bce1c29SKevin Wolf                                                   bs);
18967bce1c29SKevin Wolf             bdrv_inc_in_flight(bs);
18977bce1c29SKevin Wolf             aio_co_enter(bdrv_get_aio_context(bs), co);
18980513f984SMax Reitz         }
18990513f984SMax Reitz     }
19000513f984SMax Reitz }
19010513f984SMax Reitz 
19020513f984SMax Reitz static BlockDriver bdrv_replace_test = {
19030513f984SMax Reitz     .format_name            = "replace_test",
19040513f984SMax Reitz     .instance_size          = sizeof(BDRVReplaceTestState),
19059ebfc111SVladimir Sementsov-Ogievskiy     .supports_backing       = true,
19060513f984SMax Reitz 
19070513f984SMax Reitz     .bdrv_close             = bdrv_replace_test_close,
19080513f984SMax Reitz     .bdrv_co_preadv         = bdrv_replace_test_co_preadv,
19090513f984SMax Reitz 
19105e8ac217SKevin Wolf     .bdrv_drain_begin       = bdrv_replace_test_drain_begin,
19115e8ac217SKevin Wolf     .bdrv_drain_end         = bdrv_replace_test_drain_end,
19120513f984SMax Reitz 
191369dca43dSMax Reitz     .bdrv_child_perm        = bdrv_default_perms,
19140513f984SMax Reitz };
19150513f984SMax Reitz 
19160513f984SMax Reitz static void coroutine_fn test_replace_child_mid_drain_read_co(void *opaque)
19170513f984SMax Reitz {
19180513f984SMax Reitz     int ret;
19190513f984SMax Reitz     char data;
19200513f984SMax Reitz 
19210513f984SMax Reitz     ret = blk_co_pread(opaque, 0, 1, &data, 0);
19220513f984SMax Reitz     g_assert(ret >= 0);
19230513f984SMax Reitz }
19240513f984SMax Reitz 
19250513f984SMax Reitz /**
19260513f984SMax Reitz  * We test two things:
19270513f984SMax Reitz  * (1) bdrv_replace_child_noperm() must not undrain the parent if both
19280513f984SMax Reitz  *     children are drained.
19290513f984SMax Reitz  * (2) bdrv_replace_child_noperm() must never flush I/O requests to a
19300513f984SMax Reitz  *     drained child.  If the old child is drained, it must flush I/O
19310513f984SMax Reitz  *     requests after the new one has been attached.  If the new child
19320513f984SMax Reitz  *     is drained, it must flush I/O requests before the old one is
19330513f984SMax Reitz  *     detached.
19340513f984SMax Reitz  *
19350513f984SMax Reitz  * To do so, we create one parent node and two child nodes; then
19360513f984SMax Reitz  * attach one of the children (old_child_bs) to the parent, then
19370513f984SMax Reitz  * drain both old_child_bs and new_child_bs according to
19380513f984SMax Reitz  * old_drain_count and new_drain_count, respectively, and finally
19390513f984SMax Reitz  * we invoke bdrv_replace_node() to replace old_child_bs by
19400513f984SMax Reitz  * new_child_bs.
19410513f984SMax Reitz  *
19420513f984SMax Reitz  * The test block driver we use here (bdrv_replace_test) has a read
19430513f984SMax Reitz  * function that:
19440513f984SMax Reitz  * - For the parent node, can optionally yield, and then forwards the
19450513f984SMax Reitz  *   read to bdrv_preadv(),
19460513f984SMax Reitz  * - For the child node, just returns immediately.
19470513f984SMax Reitz  *
19480513f984SMax Reitz  * If the read yields, the drain_begin function will wake it up.
19490513f984SMax Reitz  *
19500513f984SMax Reitz  * The drain_end function issues a read on the parent once it is fully
19510513f984SMax Reitz  * undrained (which simulates requests starting to come in again).
19520513f984SMax Reitz  */
19530513f984SMax Reitz static void do_test_replace_child_mid_drain(int old_drain_count,
19540513f984SMax Reitz                                             int new_drain_count)
19550513f984SMax Reitz {
19560513f984SMax Reitz     BlockBackend *parent_blk;
19570513f984SMax Reitz     BlockDriverState *parent_bs;
19580513f984SMax Reitz     BlockDriverState *old_child_bs, *new_child_bs;
19590513f984SMax Reitz     BDRVReplaceTestState *parent_s;
19600513f984SMax Reitz     BDRVReplaceTestState *old_child_s, *new_child_s;
19610513f984SMax Reitz     Coroutine *io_co;
19620513f984SMax Reitz     int i;
19630513f984SMax Reitz 
19640513f984SMax Reitz     parent_bs = bdrv_new_open_driver(&bdrv_replace_test, "parent", 0,
19650513f984SMax Reitz                                      &error_abort);
19660513f984SMax Reitz     parent_s = parent_bs->opaque;
19670513f984SMax Reitz 
19680513f984SMax Reitz     parent_blk = blk_new(qemu_get_aio_context(),
19690513f984SMax Reitz                          BLK_PERM_CONSISTENT_READ, BLK_PERM_ALL);
19700513f984SMax Reitz     blk_insert_bs(parent_blk, parent_bs, &error_abort);
19710513f984SMax Reitz 
19720513f984SMax Reitz     old_child_bs = bdrv_new_open_driver(&bdrv_replace_test, "old-child", 0,
19730513f984SMax Reitz                                         &error_abort);
19740513f984SMax Reitz     new_child_bs = bdrv_new_open_driver(&bdrv_replace_test, "new-child", 0,
19750513f984SMax Reitz                                         &error_abort);
19760513f984SMax Reitz     old_child_s = old_child_bs->opaque;
19770513f984SMax Reitz     new_child_s = new_child_bs->opaque;
19780513f984SMax Reitz 
19790513f984SMax Reitz     /* So that we can read something */
19800513f984SMax Reitz     parent_bs->total_sectors = 1;
19810513f984SMax Reitz     old_child_bs->total_sectors = 1;
19820513f984SMax Reitz     new_child_bs->total_sectors = 1;
19830513f984SMax Reitz 
19840513f984SMax Reitz     bdrv_ref(old_child_bs);
1985afdaeb9eSKevin Wolf     bdrv_graph_wrlock(NULL);
19865bb04747SVladimir Sementsov-Ogievskiy     bdrv_attach_child(parent_bs, old_child_bs, "child", &child_of_bds,
19875bb04747SVladimir Sementsov-Ogievskiy                       BDRV_CHILD_COW, &error_abort);
1988afdaeb9eSKevin Wolf     bdrv_graph_wrunlock();
198923987471SKevin Wolf     parent_s->setup_completed = true;
19900513f984SMax Reitz 
19910513f984SMax Reitz     for (i = 0; i < old_drain_count; i++) {
19920513f984SMax Reitz         bdrv_drained_begin(old_child_bs);
19930513f984SMax Reitz     }
19940513f984SMax Reitz     for (i = 0; i < new_drain_count; i++) {
19950513f984SMax Reitz         bdrv_drained_begin(new_child_bs);
19960513f984SMax Reitz     }
19970513f984SMax Reitz 
19980513f984SMax Reitz     if (!old_drain_count) {
19990513f984SMax Reitz         /*
20000513f984SMax Reitz          * Start a read operation that will yield, so it will not
20010513f984SMax Reitz          * complete before the node is drained.
20020513f984SMax Reitz          */
20030513f984SMax Reitz         parent_s->yield_before_read = true;
20040513f984SMax Reitz         io_co = qemu_coroutine_create(test_replace_child_mid_drain_read_co,
20050513f984SMax Reitz                                       parent_blk);
20060513f984SMax Reitz         qemu_coroutine_enter(io_co);
20070513f984SMax Reitz     }
20080513f984SMax Reitz 
20090513f984SMax Reitz     /* If we have started a read operation, it should have yielded */
20100513f984SMax Reitz     g_assert(!parent_s->has_read);
20110513f984SMax Reitz 
20120513f984SMax Reitz     /* Reset drained status so we can see what bdrv_replace_node() does */
20130513f984SMax Reitz     parent_s->was_drained = false;
20140513f984SMax Reitz     parent_s->was_undrained = false;
20150513f984SMax Reitz 
20160513f984SMax Reitz     g_assert(parent_bs->quiesce_counter == old_drain_count);
2017ccd6a379SKevin Wolf     bdrv_drained_begin(old_child_bs);
2018ccd6a379SKevin Wolf     bdrv_drained_begin(new_child_bs);
2019ccd6a379SKevin Wolf     bdrv_graph_wrlock(NULL);
20200513f984SMax Reitz     bdrv_replace_node(old_child_bs, new_child_bs, &error_abort);
2021ccd6a379SKevin Wolf     bdrv_graph_wrunlock();
2022ccd6a379SKevin Wolf     bdrv_drained_end(new_child_bs);
2023ccd6a379SKevin Wolf     bdrv_drained_end(old_child_bs);
20240513f984SMax Reitz     g_assert(parent_bs->quiesce_counter == new_drain_count);
20250513f984SMax Reitz 
20260513f984SMax Reitz     if (!old_drain_count && !new_drain_count) {
20270513f984SMax Reitz         /*
20280513f984SMax Reitz          * From undrained to undrained drains and undrains the parent,
20290513f984SMax Reitz          * because bdrv_replace_node() contains a drained section for
20300513f984SMax Reitz          * @old_child_bs.
20310513f984SMax Reitz          */
20320513f984SMax Reitz         g_assert(parent_s->was_drained && parent_s->was_undrained);
20330513f984SMax Reitz     } else if (!old_drain_count && new_drain_count) {
20340513f984SMax Reitz         /*
20350513f984SMax Reitz          * From undrained to drained should drain the parent and keep
20360513f984SMax Reitz          * it that way.
20370513f984SMax Reitz          */
20380513f984SMax Reitz         g_assert(parent_s->was_drained && !parent_s->was_undrained);
20390513f984SMax Reitz     } else if (old_drain_count && !new_drain_count) {
20400513f984SMax Reitz         /*
20410513f984SMax Reitz          * From drained to undrained should undrain the parent and
20420513f984SMax Reitz          * keep it that way.
20430513f984SMax Reitz          */
20440513f984SMax Reitz         g_assert(!parent_s->was_drained && parent_s->was_undrained);
20450513f984SMax Reitz     } else /* if (old_drain_count && new_drain_count) */ {
20460513f984SMax Reitz         /*
20470513f984SMax Reitz          * From drained to drained must not undrain the parent at any
20480513f984SMax Reitz          * point
20490513f984SMax Reitz          */
20500513f984SMax Reitz         g_assert(!parent_s->was_drained && !parent_s->was_undrained);
20510513f984SMax Reitz     }
20520513f984SMax Reitz 
20530513f984SMax Reitz     if (!old_drain_count || !new_drain_count) {
20540513f984SMax Reitz         /*
20550513f984SMax Reitz          * If !old_drain_count, we have started a read request before
20560513f984SMax Reitz          * bdrv_replace_node().  If !new_drain_count, the parent must
20570513f984SMax Reitz          * have been undrained at some point, and
20580513f984SMax Reitz          * bdrv_replace_test_co_drain_end() starts a read request
20590513f984SMax Reitz          * then.
20600513f984SMax Reitz          */
20610513f984SMax Reitz         g_assert(parent_s->has_read);
20620513f984SMax Reitz     } else {
20630513f984SMax Reitz         /*
20640513f984SMax Reitz          * If the parent was never undrained, there is no way to start
20650513f984SMax Reitz          * a read request.
20660513f984SMax Reitz          */
20670513f984SMax Reitz         g_assert(!parent_s->has_read);
20680513f984SMax Reitz     }
20690513f984SMax Reitz 
20700513f984SMax Reitz     /* A drained child must have not received any request */
20710513f984SMax Reitz     g_assert(!(old_drain_count && old_child_s->has_read));
20720513f984SMax Reitz     g_assert(!(new_drain_count && new_child_s->has_read));
20730513f984SMax Reitz 
20740513f984SMax Reitz     for (i = 0; i < new_drain_count; i++) {
20750513f984SMax Reitz         bdrv_drained_end(new_child_bs);
20760513f984SMax Reitz     }
20770513f984SMax Reitz     for (i = 0; i < old_drain_count; i++) {
20780513f984SMax Reitz         bdrv_drained_end(old_child_bs);
20790513f984SMax Reitz     }
20800513f984SMax Reitz 
20810513f984SMax Reitz     /*
20820513f984SMax Reitz      * By now, bdrv_replace_test_co_drain_end() must have been called
20830513f984SMax Reitz      * at some point while the new child was attached to the parent.
20840513f984SMax Reitz      */
20850513f984SMax Reitz     g_assert(parent_s->has_read);
20860513f984SMax Reitz     g_assert(new_child_s->has_read);
20870513f984SMax Reitz 
20880513f984SMax Reitz     blk_unref(parent_blk);
20890513f984SMax Reitz     bdrv_unref(parent_bs);
20900513f984SMax Reitz     bdrv_unref(old_child_bs);
20910513f984SMax Reitz     bdrv_unref(new_child_bs);
20920513f984SMax Reitz }
20930513f984SMax Reitz 
20940513f984SMax Reitz static void test_replace_child_mid_drain(void)
20950513f984SMax Reitz {
20960513f984SMax Reitz     int old_drain_count, new_drain_count;
20970513f984SMax Reitz 
20980513f984SMax Reitz     for (old_drain_count = 0; old_drain_count < 2; old_drain_count++) {
20990513f984SMax Reitz         for (new_drain_count = 0; new_drain_count < 2; new_drain_count++) {
21000513f984SMax Reitz             do_test_replace_child_mid_drain(old_drain_count, new_drain_count);
21010513f984SMax Reitz         }
21020513f984SMax Reitz     }
21030513f984SMax Reitz }
21040513f984SMax Reitz 
2105881cfd17SKevin Wolf int main(int argc, char **argv)
2106881cfd17SKevin Wolf {
2107bb675689SKevin Wolf     int ret;
2108bb675689SKevin Wolf 
2109881cfd17SKevin Wolf     bdrv_init();
2110881cfd17SKevin Wolf     qemu_init_main_loop(&error_abort);
2111881cfd17SKevin Wolf 
2112881cfd17SKevin Wolf     g_test_init(&argc, &argv, NULL);
2113bb675689SKevin Wolf     qemu_event_init(&done_event, false);
2114881cfd17SKevin Wolf 
2115881cfd17SKevin Wolf     g_test_add_func("/bdrv-drain/driver-cb/drain_all", test_drv_cb_drain_all);
211686e1c840SKevin Wolf     g_test_add_func("/bdrv-drain/driver-cb/drain", test_drv_cb_drain);
2117881cfd17SKevin Wolf 
21186d0252f2SKevin Wolf     g_test_add_func("/bdrv-drain/driver-cb/co/drain_all",
21196d0252f2SKevin Wolf                     test_drv_cb_co_drain_all);
21200582eb10SKevin Wolf     g_test_add_func("/bdrv-drain/driver-cb/co/drain", test_drv_cb_co_drain);
21210582eb10SKevin Wolf 
212289a6ceabSKevin Wolf     g_test_add_func("/bdrv-drain/quiesce/drain_all", test_quiesce_drain_all);
212389a6ceabSKevin Wolf     g_test_add_func("/bdrv-drain/quiesce/drain", test_quiesce_drain);
212489a6ceabSKevin Wolf 
21256d0252f2SKevin Wolf     g_test_add_func("/bdrv-drain/quiesce/co/drain_all",
21266d0252f2SKevin Wolf                     test_quiesce_co_drain_all);
21270582eb10SKevin Wolf     g_test_add_func("/bdrv-drain/quiesce/co/drain", test_quiesce_co_drain);
21280582eb10SKevin Wolf 
21296c429a6aSKevin Wolf     g_test_add_func("/bdrv-drain/nested", test_nested);
213019f7a7e5SKevin Wolf 
213119f7a7e5SKevin Wolf     g_test_add_func("/bdrv-drain/graph-change/drain_all",
213219f7a7e5SKevin Wolf                     test_graph_change_drain_all);
21336c429a6aSKevin Wolf 
2134bb675689SKevin Wolf     g_test_add_func("/bdrv-drain/iothread/drain_all", test_iothread_drain_all);
2135bb675689SKevin Wolf     g_test_add_func("/bdrv-drain/iothread/drain", test_iothread_drain);
2136bb675689SKevin Wolf 
21377253220dSKevin Wolf     g_test_add_func("/bdrv-drain/blockjob/drain_all", test_blockjob_drain_all);
21387253220dSKevin Wolf     g_test_add_func("/bdrv-drain/blockjob/drain", test_blockjob_drain);
21397253220dSKevin Wolf 
2140d49725afSKevin Wolf     g_test_add_func("/bdrv-drain/blockjob/error/drain_all",
2141d49725afSKevin Wolf                     test_blockjob_error_drain_all);
2142d49725afSKevin Wolf     g_test_add_func("/bdrv-drain/blockjob/error/drain",
2143d49725afSKevin Wolf                     test_blockjob_error_drain);
2144d49725afSKevin Wolf 
2145f62c1729SKevin Wolf     g_test_add_func("/bdrv-drain/blockjob/iothread/drain_all",
2146f62c1729SKevin Wolf                     test_blockjob_iothread_drain_all);
2147f62c1729SKevin Wolf     g_test_add_func("/bdrv-drain/blockjob/iothread/drain",
2148f62c1729SKevin Wolf                     test_blockjob_iothread_drain);
2149f62c1729SKevin Wolf 
2150d49725afSKevin Wolf     g_test_add_func("/bdrv-drain/blockjob/iothread/error/drain_all",
2151d49725afSKevin Wolf                     test_blockjob_iothread_error_drain_all);
2152d49725afSKevin Wolf     g_test_add_func("/bdrv-drain/blockjob/iothread/error/drain",
2153d49725afSKevin Wolf                     test_blockjob_iothread_error_drain);
2154d49725afSKevin Wolf 
2155ebd31837SKevin Wolf     g_test_add_func("/bdrv-drain/deletion/drain", test_delete_by_drain);
215619f7a7e5SKevin Wolf     g_test_add_func("/bdrv-drain/detach/drain_all", test_detach_by_drain_all);
2157ebd31837SKevin Wolf     g_test_add_func("/bdrv-drain/detach/drain", test_detach_by_drain);
2158231281abSKevin Wolf     g_test_add_func("/bdrv-drain/detach/parent_cb", test_detach_by_parent_cb);
215957320ca9SKevin Wolf     g_test_add_func("/bdrv-drain/detach/driver_cb", test_detach_by_driver_cb);
21604c8158e3SMax Reitz 
2161b994c5bcSKevin Wolf     g_test_add_func("/bdrv-drain/attach/drain", test_append_to_drained);
2162b994c5bcSKevin Wolf 
2163247d2737SKevin Wolf     g_test_add_func("/bdrv-drain/set_aio_context", test_set_aio_context);
2164247d2737SKevin Wolf 
21658e442810SMax Reitz     g_test_add_func("/bdrv-drain/blockjob/commit_by_drained_end",
21668e442810SMax Reitz                     test_blockjob_commit_by_drained_end);
21678e442810SMax Reitz 
21689746b35cSMax Reitz     g_test_add_func("/bdrv-drain/bdrv_drop_intermediate/poll",
21699746b35cSMax Reitz                     test_drop_intermediate_poll);
21709746b35cSMax Reitz 
21710513f984SMax Reitz     g_test_add_func("/bdrv-drain/replace_child/mid-drain",
21720513f984SMax Reitz                     test_replace_child_mid_drain);
21730513f984SMax Reitz 
2174bb675689SKevin Wolf     ret = g_test_run();
2175bb675689SKevin Wolf     qemu_event_destroy(&done_event);
2176bb675689SKevin Wolf     return ret;
2177881cfd17SKevin Wolf }
2178