xref: /qemu/tests/unit/test-bdrv-drain.c (revision 57e05be343f33f4e5899a8d8946a8596d68424a1)
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"
26881cfd17SKevin Wolf #include "block/block.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 
999746b35cSMax Reitz static int bdrv_test_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 
1199746b35cSMax Reitz     .bdrv_change_backing_file = bdrv_test_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 
191f62c1729SKevin Wolf static void do_drain_end_unlocked(enum drain_type drain_type, BlockDriverState *bs)
192f62c1729SKevin Wolf {
193f62c1729SKevin Wolf     if (drain_type != BDRV_DRAIN_ALL) {
194f62c1729SKevin Wolf         aio_context_acquire(bdrv_get_aio_context(bs));
195f62c1729SKevin Wolf     }
196f62c1729SKevin Wolf     do_drain_end(drain_type, bs);
197f62c1729SKevin Wolf     if (drain_type != BDRV_DRAIN_ALL) {
198f62c1729SKevin Wolf         aio_context_release(bdrv_get_aio_context(bs));
199f62c1729SKevin Wolf     }
200f62c1729SKevin Wolf }
201f62c1729SKevin Wolf 
20286e1c840SKevin Wolf static void test_drv_cb_common(enum drain_type drain_type, bool recursive)
203881cfd17SKevin Wolf {
204881cfd17SKevin Wolf     BlockBackend *blk;
20586e1c840SKevin Wolf     BlockDriverState *bs, *backing;
20686e1c840SKevin Wolf     BDRVTestState *s, *backing_s;
207881cfd17SKevin Wolf     BlockAIOCB *acb;
208881cfd17SKevin Wolf     int aio_ret;
209881cfd17SKevin Wolf 
210405d8fe0SVladimir Sementsov-Ogievskiy     QEMUIOVector qiov = QEMU_IOVEC_INIT_BUF(qiov, NULL, 0);
211881cfd17SKevin Wolf 
212d861ab3aSKevin Wolf     blk = blk_new(qemu_get_aio_context(), BLK_PERM_ALL, BLK_PERM_ALL);
213881cfd17SKevin Wolf     bs = bdrv_new_open_driver(&bdrv_test, "test-node", BDRV_O_RDWR,
214881cfd17SKevin Wolf                               &error_abort);
215881cfd17SKevin Wolf     s = bs->opaque;
216881cfd17SKevin Wolf     blk_insert_bs(blk, bs, &error_abort);
217881cfd17SKevin Wolf 
21886e1c840SKevin Wolf     backing = bdrv_new_open_driver(&bdrv_test, "backing", 0, &error_abort);
21986e1c840SKevin Wolf     backing_s = backing->opaque;
22086e1c840SKevin Wolf     bdrv_set_backing_hd(bs, backing, &error_abort);
22186e1c840SKevin Wolf 
222881cfd17SKevin Wolf     /* Simple bdrv_drain_all_begin/end pair, check that CBs are called */
223881cfd17SKevin Wolf     g_assert_cmpint(s->drain_count, ==, 0);
22486e1c840SKevin Wolf     g_assert_cmpint(backing_s->drain_count, ==, 0);
22586e1c840SKevin Wolf 
22686e1c840SKevin Wolf     do_drain_begin(drain_type, bs);
22786e1c840SKevin Wolf 
228881cfd17SKevin Wolf     g_assert_cmpint(s->drain_count, ==, 1);
22986e1c840SKevin Wolf     g_assert_cmpint(backing_s->drain_count, ==, !!recursive);
23086e1c840SKevin Wolf 
23186e1c840SKevin Wolf     do_drain_end(drain_type, bs);
23286e1c840SKevin Wolf 
233881cfd17SKevin Wolf     g_assert_cmpint(s->drain_count, ==, 0);
23486e1c840SKevin Wolf     g_assert_cmpint(backing_s->drain_count, ==, 0);
235881cfd17SKevin Wolf 
236881cfd17SKevin Wolf     /* Now do the same while a request is pending */
237881cfd17SKevin Wolf     aio_ret = -EINPROGRESS;
238881cfd17SKevin Wolf     acb = blk_aio_preadv(blk, 0, &qiov, 0, aio_ret_cb, &aio_ret);
239881cfd17SKevin Wolf     g_assert(acb != NULL);
240881cfd17SKevin Wolf     g_assert_cmpint(aio_ret, ==, -EINPROGRESS);
241881cfd17SKevin Wolf 
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(aio_ret, ==, 0);
248881cfd17SKevin Wolf     g_assert_cmpint(s->drain_count, ==, 1);
24986e1c840SKevin Wolf     g_assert_cmpint(backing_s->drain_count, ==, !!recursive);
250881cfd17SKevin Wolf 
25186e1c840SKevin Wolf     do_drain_end(drain_type, bs);
25286e1c840SKevin Wolf 
25386e1c840SKevin Wolf     g_assert_cmpint(s->drain_count, ==, 0);
25486e1c840SKevin Wolf     g_assert_cmpint(backing_s->drain_count, ==, 0);
25586e1c840SKevin Wolf 
25686e1c840SKevin Wolf     bdrv_unref(backing);
257881cfd17SKevin Wolf     bdrv_unref(bs);
258881cfd17SKevin Wolf     blk_unref(blk);
259881cfd17SKevin Wolf }
260881cfd17SKevin Wolf 
26186e1c840SKevin Wolf static void test_drv_cb_drain_all(void)
26286e1c840SKevin Wolf {
26386e1c840SKevin Wolf     test_drv_cb_common(BDRV_DRAIN_ALL, true);
26486e1c840SKevin Wolf }
26586e1c840SKevin Wolf 
26686e1c840SKevin Wolf static void test_drv_cb_drain(void)
26786e1c840SKevin Wolf {
26886e1c840SKevin Wolf     test_drv_cb_common(BDRV_DRAIN, false);
26986e1c840SKevin Wolf }
27086e1c840SKevin Wolf 
2716d0252f2SKevin Wolf static void test_drv_cb_co_drain_all(void)
2726d0252f2SKevin Wolf {
2736d0252f2SKevin Wolf     call_in_coroutine(test_drv_cb_drain_all);
2746d0252f2SKevin Wolf }
2756d0252f2SKevin Wolf 
2760582eb10SKevin Wolf static void test_drv_cb_co_drain(void)
2770582eb10SKevin Wolf {
2780582eb10SKevin Wolf     call_in_coroutine(test_drv_cb_drain);
2790582eb10SKevin Wolf }
2800582eb10SKevin Wolf 
28189a6ceabSKevin Wolf static void test_quiesce_common(enum drain_type drain_type, bool recursive)
28289a6ceabSKevin Wolf {
28389a6ceabSKevin Wolf     BlockBackend *blk;
28489a6ceabSKevin Wolf     BlockDriverState *bs, *backing;
28589a6ceabSKevin Wolf 
286d861ab3aSKevin Wolf     blk = blk_new(qemu_get_aio_context(), BLK_PERM_ALL, BLK_PERM_ALL);
28789a6ceabSKevin Wolf     bs = bdrv_new_open_driver(&bdrv_test, "test-node", BDRV_O_RDWR,
28889a6ceabSKevin Wolf                               &error_abort);
28989a6ceabSKevin Wolf     blk_insert_bs(blk, bs, &error_abort);
29089a6ceabSKevin Wolf 
29189a6ceabSKevin Wolf     backing = bdrv_new_open_driver(&bdrv_test, "backing", 0, &error_abort);
29289a6ceabSKevin Wolf     bdrv_set_backing_hd(bs, backing, &error_abort);
29389a6ceabSKevin Wolf 
29489a6ceabSKevin Wolf     g_assert_cmpint(bs->quiesce_counter, ==, 0);
29589a6ceabSKevin Wolf     g_assert_cmpint(backing->quiesce_counter, ==, 0);
29689a6ceabSKevin Wolf 
29789a6ceabSKevin Wolf     do_drain_begin(drain_type, bs);
29889a6ceabSKevin Wolf 
299*57e05be3SKevin Wolf     if (drain_type == BDRV_DRAIN_ALL) {
300*57e05be3SKevin Wolf         g_assert_cmpint(bs->quiesce_counter, ==, 2);
301*57e05be3SKevin Wolf     } else {
30289a6ceabSKevin Wolf         g_assert_cmpint(bs->quiesce_counter, ==, 1);
303*57e05be3SKevin Wolf     }
30489a6ceabSKevin Wolf     g_assert_cmpint(backing->quiesce_counter, ==, !!recursive);
30589a6ceabSKevin Wolf 
30689a6ceabSKevin Wolf     do_drain_end(drain_type, bs);
30789a6ceabSKevin Wolf 
30889a6ceabSKevin Wolf     g_assert_cmpint(bs->quiesce_counter, ==, 0);
30989a6ceabSKevin Wolf     g_assert_cmpint(backing->quiesce_counter, ==, 0);
31089a6ceabSKevin Wolf 
31189a6ceabSKevin Wolf     bdrv_unref(backing);
31289a6ceabSKevin Wolf     bdrv_unref(bs);
31389a6ceabSKevin Wolf     blk_unref(blk);
31489a6ceabSKevin Wolf }
31589a6ceabSKevin Wolf 
31689a6ceabSKevin Wolf static void test_quiesce_drain_all(void)
31789a6ceabSKevin Wolf {
31879ab8b21SKevin Wolf     test_quiesce_common(BDRV_DRAIN_ALL, true);
31989a6ceabSKevin Wolf }
32089a6ceabSKevin Wolf 
32189a6ceabSKevin Wolf static void test_quiesce_drain(void)
32289a6ceabSKevin Wolf {
32389a6ceabSKevin Wolf     test_quiesce_common(BDRV_DRAIN, false);
32489a6ceabSKevin Wolf }
32589a6ceabSKevin Wolf 
3266d0252f2SKevin Wolf static void test_quiesce_co_drain_all(void)
3276d0252f2SKevin Wolf {
3286d0252f2SKevin Wolf     call_in_coroutine(test_quiesce_drain_all);
3296d0252f2SKevin Wolf }
3306d0252f2SKevin Wolf 
3310582eb10SKevin Wolf static void test_quiesce_co_drain(void)
3320582eb10SKevin Wolf {
3330582eb10SKevin Wolf     call_in_coroutine(test_quiesce_drain);
3340582eb10SKevin Wolf }
3350582eb10SKevin Wolf 
3366c429a6aSKevin Wolf static void test_nested(void)
3376c429a6aSKevin Wolf {
3386c429a6aSKevin Wolf     BlockBackend *blk;
3396c429a6aSKevin Wolf     BlockDriverState *bs, *backing;
3406c429a6aSKevin Wolf     BDRVTestState *s, *backing_s;
3416c429a6aSKevin Wolf     enum drain_type outer, inner;
3426c429a6aSKevin Wolf 
343d861ab3aSKevin Wolf     blk = blk_new(qemu_get_aio_context(), BLK_PERM_ALL, BLK_PERM_ALL);
3446c429a6aSKevin Wolf     bs = bdrv_new_open_driver(&bdrv_test, "test-node", BDRV_O_RDWR,
3456c429a6aSKevin Wolf                               &error_abort);
3466c429a6aSKevin Wolf     s = bs->opaque;
3476c429a6aSKevin Wolf     blk_insert_bs(blk, bs, &error_abort);
3486c429a6aSKevin Wolf 
3496c429a6aSKevin Wolf     backing = bdrv_new_open_driver(&bdrv_test, "backing", 0, &error_abort);
3506c429a6aSKevin Wolf     backing_s = backing->opaque;
3516c429a6aSKevin Wolf     bdrv_set_backing_hd(bs, backing, &error_abort);
3526c429a6aSKevin Wolf 
3536c429a6aSKevin Wolf     for (outer = 0; outer < DRAIN_TYPE_MAX; outer++) {
3546c429a6aSKevin Wolf         for (inner = 0; inner < DRAIN_TYPE_MAX; inner++) {
355*57e05be3SKevin Wolf             int backing_quiesce = (outer == BDRV_DRAIN_ALL) +
356*57e05be3SKevin Wolf                                   (inner == BDRV_DRAIN_ALL);
3576c429a6aSKevin Wolf 
3586c429a6aSKevin Wolf             g_assert_cmpint(bs->quiesce_counter, ==, 0);
3596c429a6aSKevin Wolf             g_assert_cmpint(backing->quiesce_counter, ==, 0);
3606c429a6aSKevin Wolf             g_assert_cmpint(s->drain_count, ==, 0);
3616c429a6aSKevin Wolf             g_assert_cmpint(backing_s->drain_count, ==, 0);
3626c429a6aSKevin Wolf 
3636c429a6aSKevin Wolf             do_drain_begin(outer, bs);
3646c429a6aSKevin Wolf             do_drain_begin(inner, bs);
3656c429a6aSKevin Wolf 
366*57e05be3SKevin Wolf             g_assert_cmpint(bs->quiesce_counter, ==, 2 + !!backing_quiesce);
3676c429a6aSKevin Wolf             g_assert_cmpint(backing->quiesce_counter, ==, backing_quiesce);
368*57e05be3SKevin Wolf             g_assert_cmpint(s->drain_count, ==, 1);
369*57e05be3SKevin Wolf             g_assert_cmpint(backing_s->drain_count, ==, !!backing_quiesce);
3706c429a6aSKevin Wolf 
3716c429a6aSKevin Wolf             do_drain_end(inner, bs);
3726c429a6aSKevin Wolf             do_drain_end(outer, bs);
3736c429a6aSKevin Wolf 
3746c429a6aSKevin Wolf             g_assert_cmpint(bs->quiesce_counter, ==, 0);
3756c429a6aSKevin Wolf             g_assert_cmpint(backing->quiesce_counter, ==, 0);
3766c429a6aSKevin Wolf             g_assert_cmpint(s->drain_count, ==, 0);
3776c429a6aSKevin Wolf             g_assert_cmpint(backing_s->drain_count, ==, 0);
3786c429a6aSKevin Wolf         }
3796c429a6aSKevin Wolf     }
3806c429a6aSKevin Wolf 
3816c429a6aSKevin Wolf     bdrv_unref(backing);
3826c429a6aSKevin Wolf     bdrv_unref(bs);
3836c429a6aSKevin Wolf     blk_unref(blk);
3846c429a6aSKevin Wolf }
3856c429a6aSKevin Wolf 
38619f7a7e5SKevin Wolf static void test_graph_change_drain_all(void)
38719f7a7e5SKevin Wolf {
38819f7a7e5SKevin Wolf     BlockBackend *blk_a, *blk_b;
38919f7a7e5SKevin Wolf     BlockDriverState *bs_a, *bs_b;
39019f7a7e5SKevin Wolf     BDRVTestState *a_s, *b_s;
39119f7a7e5SKevin Wolf 
39219f7a7e5SKevin Wolf     /* Create node A with a BlockBackend */
393d861ab3aSKevin Wolf     blk_a = blk_new(qemu_get_aio_context(), BLK_PERM_ALL, BLK_PERM_ALL);
39419f7a7e5SKevin Wolf     bs_a = bdrv_new_open_driver(&bdrv_test, "test-node-a", BDRV_O_RDWR,
39519f7a7e5SKevin Wolf                                 &error_abort);
39619f7a7e5SKevin Wolf     a_s = bs_a->opaque;
39719f7a7e5SKevin Wolf     blk_insert_bs(blk_a, bs_a, &error_abort);
39819f7a7e5SKevin Wolf 
39919f7a7e5SKevin Wolf     g_assert_cmpint(bs_a->quiesce_counter, ==, 0);
40019f7a7e5SKevin Wolf     g_assert_cmpint(a_s->drain_count, ==, 0);
40119f7a7e5SKevin Wolf 
40219f7a7e5SKevin Wolf     /* Call bdrv_drain_all_begin() */
40319f7a7e5SKevin Wolf     bdrv_drain_all_begin();
40419f7a7e5SKevin Wolf 
40519f7a7e5SKevin Wolf     g_assert_cmpint(bs_a->quiesce_counter, ==, 1);
40619f7a7e5SKevin Wolf     g_assert_cmpint(a_s->drain_count, ==, 1);
40719f7a7e5SKevin Wolf 
40819f7a7e5SKevin Wolf     /* Create node B with a BlockBackend */
409d861ab3aSKevin Wolf     blk_b = blk_new(qemu_get_aio_context(), BLK_PERM_ALL, BLK_PERM_ALL);
41019f7a7e5SKevin Wolf     bs_b = bdrv_new_open_driver(&bdrv_test, "test-node-b", BDRV_O_RDWR,
41119f7a7e5SKevin Wolf                                 &error_abort);
41219f7a7e5SKevin Wolf     b_s = bs_b->opaque;
41319f7a7e5SKevin Wolf     blk_insert_bs(blk_b, bs_b, &error_abort);
41419f7a7e5SKevin Wolf 
41519f7a7e5SKevin Wolf     g_assert_cmpint(bs_a->quiesce_counter, ==, 1);
41619f7a7e5SKevin Wolf     g_assert_cmpint(bs_b->quiesce_counter, ==, 1);
41719f7a7e5SKevin Wolf     g_assert_cmpint(a_s->drain_count, ==, 1);
41819f7a7e5SKevin Wolf     g_assert_cmpint(b_s->drain_count, ==, 1);
41919f7a7e5SKevin Wolf 
42019f7a7e5SKevin Wolf     /* Unref and finally delete node A */
42119f7a7e5SKevin Wolf     blk_unref(blk_a);
42219f7a7e5SKevin Wolf 
42319f7a7e5SKevin Wolf     g_assert_cmpint(bs_a->quiesce_counter, ==, 1);
42419f7a7e5SKevin Wolf     g_assert_cmpint(bs_b->quiesce_counter, ==, 1);
42519f7a7e5SKevin Wolf     g_assert_cmpint(a_s->drain_count, ==, 1);
42619f7a7e5SKevin Wolf     g_assert_cmpint(b_s->drain_count, ==, 1);
42719f7a7e5SKevin Wolf 
42819f7a7e5SKevin Wolf     bdrv_unref(bs_a);
42919f7a7e5SKevin Wolf 
43019f7a7e5SKevin Wolf     g_assert_cmpint(bs_b->quiesce_counter, ==, 1);
43119f7a7e5SKevin Wolf     g_assert_cmpint(b_s->drain_count, ==, 1);
43219f7a7e5SKevin Wolf 
43319f7a7e5SKevin Wolf     /* End the drained section */
43419f7a7e5SKevin Wolf     bdrv_drain_all_end();
43519f7a7e5SKevin Wolf 
43619f7a7e5SKevin Wolf     g_assert_cmpint(bs_b->quiesce_counter, ==, 0);
43719f7a7e5SKevin Wolf     g_assert_cmpint(b_s->drain_count, ==, 0);
4381a6d3bd2SGreg Kurz     g_assert_cmpint(qemu_get_aio_context()->external_disable_cnt, ==, 0);
43919f7a7e5SKevin Wolf 
44019f7a7e5SKevin Wolf     bdrv_unref(bs_b);
44119f7a7e5SKevin Wolf     blk_unref(blk_b);
44219f7a7e5SKevin Wolf }
44319f7a7e5SKevin Wolf 
444bb675689SKevin Wolf struct test_iothread_data {
445bb675689SKevin Wolf     BlockDriverState *bs;
446bb675689SKevin Wolf     enum drain_type drain_type;
447bb675689SKevin Wolf     int *aio_ret;
448bb675689SKevin Wolf };
449bb675689SKevin Wolf 
450bb675689SKevin Wolf static void test_iothread_drain_entry(void *opaque)
451bb675689SKevin Wolf {
452bb675689SKevin Wolf     struct test_iothread_data *data = opaque;
453bb675689SKevin Wolf 
454bb675689SKevin Wolf     aio_context_acquire(bdrv_get_aio_context(data->bs));
455bb675689SKevin Wolf     do_drain_begin(data->drain_type, data->bs);
456bb675689SKevin Wolf     g_assert_cmpint(*data->aio_ret, ==, 0);
457bb675689SKevin Wolf     do_drain_end(data->drain_type, data->bs);
458bb675689SKevin Wolf     aio_context_release(bdrv_get_aio_context(data->bs));
459bb675689SKevin Wolf 
460bb675689SKevin Wolf     qemu_event_set(&done_event);
461bb675689SKevin Wolf }
462bb675689SKevin Wolf 
463bb675689SKevin Wolf static void test_iothread_aio_cb(void *opaque, int ret)
464bb675689SKevin Wolf {
465bb675689SKevin Wolf     int *aio_ret = opaque;
466bb675689SKevin Wolf     *aio_ret = ret;
467bb675689SKevin Wolf     qemu_event_set(&done_event);
468bb675689SKevin Wolf }
469bb675689SKevin Wolf 
470ecc1a5c7SKevin Wolf static void test_iothread_main_thread_bh(void *opaque)
471ecc1a5c7SKevin Wolf {
472ecc1a5c7SKevin Wolf     struct test_iothread_data *data = opaque;
473ecc1a5c7SKevin Wolf 
474ecc1a5c7SKevin Wolf     /* Test that the AioContext is not yet locked in a random BH that is
475ecc1a5c7SKevin Wolf      * executed during drain, otherwise this would deadlock. */
476ecc1a5c7SKevin Wolf     aio_context_acquire(bdrv_get_aio_context(data->bs));
477ecc1a5c7SKevin Wolf     bdrv_flush(data->bs);
478ecc1a5c7SKevin Wolf     aio_context_release(bdrv_get_aio_context(data->bs));
479ecc1a5c7SKevin Wolf }
480ecc1a5c7SKevin Wolf 
481bb675689SKevin Wolf /*
482bb675689SKevin Wolf  * Starts an AIO request on a BDS that runs in the AioContext of iothread 1.
483bb675689SKevin Wolf  * The request involves a BH on iothread 2 before it can complete.
484bb675689SKevin Wolf  *
485bb675689SKevin Wolf  * @drain_thread = 0 means that do_drain_begin/end are called from the main
486bb675689SKevin Wolf  * thread, @drain_thread = 1 means that they are called from iothread 1. Drain
487bb675689SKevin Wolf  * for this BDS cannot be called from iothread 2 because only the main thread
488bb675689SKevin Wolf  * may do cross-AioContext polling.
489bb675689SKevin Wolf  */
490bb675689SKevin Wolf static void test_iothread_common(enum drain_type drain_type, int drain_thread)
491bb675689SKevin Wolf {
492bb675689SKevin Wolf     BlockBackend *blk;
493bb675689SKevin Wolf     BlockDriverState *bs;
494bb675689SKevin Wolf     BDRVTestState *s;
495bb675689SKevin Wolf     BlockAIOCB *acb;
496bb675689SKevin Wolf     int aio_ret;
497bb675689SKevin Wolf     struct test_iothread_data data;
498bb675689SKevin Wolf 
499bb675689SKevin Wolf     IOThread *a = iothread_new();
500bb675689SKevin Wolf     IOThread *b = iothread_new();
501bb675689SKevin Wolf     AioContext *ctx_a = iothread_get_aio_context(a);
502bb675689SKevin Wolf     AioContext *ctx_b = iothread_get_aio_context(b);
503bb675689SKevin Wolf 
504405d8fe0SVladimir Sementsov-Ogievskiy     QEMUIOVector qiov = QEMU_IOVEC_INIT_BUF(qiov, NULL, 0);
505bb675689SKevin Wolf 
506bb675689SKevin Wolf     /* bdrv_drain_all() may only be called from the main loop thread */
507bb675689SKevin Wolf     if (drain_type == BDRV_DRAIN_ALL && drain_thread != 0) {
508bb675689SKevin Wolf         goto out;
509bb675689SKevin Wolf     }
510bb675689SKevin Wolf 
511d861ab3aSKevin Wolf     blk = blk_new(qemu_get_aio_context(), BLK_PERM_ALL, BLK_PERM_ALL);
512bb675689SKevin Wolf     bs = bdrv_new_open_driver(&bdrv_test, "test-node", BDRV_O_RDWR,
513bb675689SKevin Wolf                               &error_abort);
514bb675689SKevin Wolf     s = bs->opaque;
515bb675689SKevin Wolf     blk_insert_bs(blk, bs, &error_abort);
516cf312932SKevin Wolf     blk_set_disable_request_queuing(blk, true);
517bb675689SKevin Wolf 
51897896a48SKevin Wolf     blk_set_aio_context(blk, ctx_a, &error_abort);
519bb675689SKevin Wolf     aio_context_acquire(ctx_a);
520bb675689SKevin Wolf 
521bb675689SKevin Wolf     s->bh_indirection_ctx = ctx_b;
522bb675689SKevin Wolf 
523bb675689SKevin Wolf     aio_ret = -EINPROGRESS;
524dd353157SKevin Wolf     qemu_event_reset(&done_event);
525dd353157SKevin Wolf 
526bb675689SKevin Wolf     if (drain_thread == 0) {
527bb675689SKevin Wolf         acb = blk_aio_preadv(blk, 0, &qiov, 0, test_iothread_aio_cb, &aio_ret);
528bb675689SKevin Wolf     } else {
529bb675689SKevin Wolf         acb = blk_aio_preadv(blk, 0, &qiov, 0, aio_ret_cb, &aio_ret);
530bb675689SKevin Wolf     }
531bb675689SKevin Wolf     g_assert(acb != NULL);
532bb675689SKevin Wolf     g_assert_cmpint(aio_ret, ==, -EINPROGRESS);
533bb675689SKevin Wolf 
534bb675689SKevin Wolf     aio_context_release(ctx_a);
535bb675689SKevin Wolf 
536bb675689SKevin Wolf     data = (struct test_iothread_data) {
537bb675689SKevin Wolf         .bs         = bs,
538bb675689SKevin Wolf         .drain_type = drain_type,
539bb675689SKevin Wolf         .aio_ret    = &aio_ret,
540bb675689SKevin Wolf     };
541bb675689SKevin Wolf 
542bb675689SKevin Wolf     switch (drain_thread) {
543bb675689SKevin Wolf     case 0:
544bb675689SKevin Wolf         if (drain_type != BDRV_DRAIN_ALL) {
545bb675689SKevin Wolf             aio_context_acquire(ctx_a);
546bb675689SKevin Wolf         }
547bb675689SKevin Wolf 
548ecc1a5c7SKevin Wolf         aio_bh_schedule_oneshot(ctx_a, test_iothread_main_thread_bh, &data);
549ecc1a5c7SKevin Wolf 
550bb675689SKevin Wolf         /* The request is running on the IOThread a. Draining its block device
551bb675689SKevin Wolf          * will make sure that it has completed as far as the BDS is concerned,
552bb675689SKevin Wolf          * but the drain in this thread can continue immediately after
553bb675689SKevin Wolf          * bdrv_dec_in_flight() and aio_ret might be assigned only slightly
554bb675689SKevin Wolf          * later. */
555bb675689SKevin Wolf         do_drain_begin(drain_type, bs);
556bb675689SKevin Wolf         g_assert_cmpint(bs->in_flight, ==, 0);
557bb675689SKevin Wolf 
558bb675689SKevin Wolf         if (drain_type != BDRV_DRAIN_ALL) {
559bb675689SKevin Wolf             aio_context_release(ctx_a);
560bb675689SKevin Wolf         }
561bb675689SKevin Wolf         qemu_event_wait(&done_event);
562bb675689SKevin Wolf         if (drain_type != BDRV_DRAIN_ALL) {
563bb675689SKevin Wolf             aio_context_acquire(ctx_a);
564bb675689SKevin Wolf         }
565bb675689SKevin Wolf 
566bb675689SKevin Wolf         g_assert_cmpint(aio_ret, ==, 0);
567bb675689SKevin Wolf         do_drain_end(drain_type, bs);
568bb675689SKevin Wolf 
569bb675689SKevin Wolf         if (drain_type != BDRV_DRAIN_ALL) {
570bb675689SKevin Wolf             aio_context_release(ctx_a);
571bb675689SKevin Wolf         }
572bb675689SKevin Wolf         break;
573bb675689SKevin Wolf     case 1:
574bb675689SKevin Wolf         aio_bh_schedule_oneshot(ctx_a, test_iothread_drain_entry, &data);
575bb675689SKevin Wolf         qemu_event_wait(&done_event);
576bb675689SKevin Wolf         break;
577bb675689SKevin Wolf     default:
578bb675689SKevin Wolf         g_assert_not_reached();
579bb675689SKevin Wolf     }
580bb675689SKevin Wolf 
581bb675689SKevin Wolf     aio_context_acquire(ctx_a);
58297896a48SKevin Wolf     blk_set_aio_context(blk, qemu_get_aio_context(), &error_abort);
583bb675689SKevin Wolf     aio_context_release(ctx_a);
584bb675689SKevin Wolf 
585bb675689SKevin Wolf     bdrv_unref(bs);
586bb675689SKevin Wolf     blk_unref(blk);
587bb675689SKevin Wolf 
588bb675689SKevin Wolf out:
589bb675689SKevin Wolf     iothread_join(a);
590bb675689SKevin Wolf     iothread_join(b);
591bb675689SKevin Wolf }
592bb675689SKevin Wolf 
593bb675689SKevin Wolf static void test_iothread_drain_all(void)
594bb675689SKevin Wolf {
595bb675689SKevin Wolf     test_iothread_common(BDRV_DRAIN_ALL, 0);
596bb675689SKevin Wolf     test_iothread_common(BDRV_DRAIN_ALL, 1);
597bb675689SKevin Wolf }
598bb675689SKevin Wolf 
599bb675689SKevin Wolf static void test_iothread_drain(void)
600bb675689SKevin Wolf {
601bb675689SKevin Wolf     test_iothread_common(BDRV_DRAIN, 0);
602bb675689SKevin Wolf     test_iothread_common(BDRV_DRAIN, 1);
603bb675689SKevin Wolf }
604bb675689SKevin Wolf 
6057253220dSKevin Wolf 
6067253220dSKevin Wolf typedef struct TestBlockJob {
6077253220dSKevin Wolf     BlockJob common;
6081b177bbeSVladimir Sementsov-Ogievskiy     BlockDriverState *bs;
609d49725afSKevin Wolf     int run_ret;
610d49725afSKevin Wolf     int prepare_ret;
611d8b3afd5SKevin Wolf     bool running;
6127253220dSKevin Wolf     bool should_complete;
6137253220dSKevin Wolf } TestBlockJob;
6147253220dSKevin Wolf 
615ae23dde9SKevin Wolf static int test_job_prepare(Job *job)
616ae23dde9SKevin Wolf {
617ae23dde9SKevin Wolf     TestBlockJob *s = container_of(job, TestBlockJob, common.job);
618ae23dde9SKevin Wolf 
619ae23dde9SKevin Wolf     /* Provoke an AIO_WAIT_WHILE() call to verify there is no deadlock */
6201b177bbeSVladimir Sementsov-Ogievskiy     bdrv_flush(s->bs);
621d49725afSKevin Wolf     return s->prepare_ret;
622d49725afSKevin Wolf }
623d49725afSKevin Wolf 
624d49725afSKevin Wolf static void test_job_commit(Job *job)
625d49725afSKevin Wolf {
626d49725afSKevin Wolf     TestBlockJob *s = container_of(job, TestBlockJob, common.job);
627d49725afSKevin Wolf 
628d49725afSKevin Wolf     /* Provoke an AIO_WAIT_WHILE() call to verify there is no deadlock */
6291b177bbeSVladimir Sementsov-Ogievskiy     bdrv_flush(s->bs);
630d49725afSKevin Wolf }
631d49725afSKevin Wolf 
632d49725afSKevin Wolf static void test_job_abort(Job *job)
633d49725afSKevin Wolf {
634d49725afSKevin Wolf     TestBlockJob *s = container_of(job, TestBlockJob, common.job);
635d49725afSKevin Wolf 
636d49725afSKevin Wolf     /* Provoke an AIO_WAIT_WHILE() call to verify there is no deadlock */
6371b177bbeSVladimir Sementsov-Ogievskiy     bdrv_flush(s->bs);
638ae23dde9SKevin Wolf }
639ae23dde9SKevin Wolf 
640f67432a2SJohn Snow static int coroutine_fn test_job_run(Job *job, Error **errp)
6417253220dSKevin Wolf {
642f67432a2SJohn Snow     TestBlockJob *s = container_of(job, TestBlockJob, common.job);
6437253220dSKevin Wolf 
644d8b3afd5SKevin Wolf     /* We are running the actual job code past the pause point in
645d8b3afd5SKevin Wolf      * job_co_entry(). */
646d8b3afd5SKevin Wolf     s->running = true;
647d8b3afd5SKevin Wolf 
6482e1795b5SKevin Wolf     job_transition_to_ready(&s->common.job);
6497253220dSKevin Wolf     while (!s->should_complete) {
6505599c162SKevin Wolf         /* Avoid job_sleep_ns() because it marks the job as !busy. We want to
6515599c162SKevin Wolf          * emulate some actual activity (probably some I/O) here so that drain
6525599c162SKevin Wolf          * has to wait for this activity to stop. */
653d8b3afd5SKevin Wolf         qemu_co_sleep_ns(QEMU_CLOCK_REALTIME, 1000000);
654d8b3afd5SKevin Wolf 
65589bd0305SKevin Wolf         job_pause_point(&s->common.job);
6567253220dSKevin Wolf     }
6577253220dSKevin Wolf 
658d49725afSKevin Wolf     return s->run_ret;
6597253220dSKevin Wolf }
6607253220dSKevin Wolf 
6613453d972SKevin Wolf static void test_job_complete(Job *job, Error **errp)
6627253220dSKevin Wolf {
6633453d972SKevin Wolf     TestBlockJob *s = container_of(job, TestBlockJob, common.job);
6647253220dSKevin Wolf     s->should_complete = true;
6657253220dSKevin Wolf }
6667253220dSKevin Wolf 
6677253220dSKevin Wolf BlockJobDriver test_job_driver = {
66833e9e9bdSKevin Wolf     .job_driver = {
6697253220dSKevin Wolf         .instance_size  = sizeof(TestBlockJob),
67080fa2c75SKevin Wolf         .free           = block_job_free,
671b15de828SKevin Wolf         .user_resume    = block_job_user_resume,
672f67432a2SJohn Snow         .run            = test_job_run,
6737253220dSKevin Wolf         .complete       = test_job_complete,
674ae23dde9SKevin Wolf         .prepare        = test_job_prepare,
675d49725afSKevin Wolf         .commit         = test_job_commit,
676d49725afSKevin Wolf         .abort          = test_job_abort,
6773453d972SKevin Wolf     },
6787253220dSKevin Wolf };
6797253220dSKevin Wolf 
680d49725afSKevin Wolf enum test_job_result {
681d49725afSKevin Wolf     TEST_JOB_SUCCESS,
682d49725afSKevin Wolf     TEST_JOB_FAIL_RUN,
683d49725afSKevin Wolf     TEST_JOB_FAIL_PREPARE,
684d49725afSKevin Wolf };
685d49725afSKevin Wolf 
686d8b3afd5SKevin Wolf enum test_job_drain_node {
687d8b3afd5SKevin Wolf     TEST_JOB_DRAIN_SRC,
688d8b3afd5SKevin Wolf     TEST_JOB_DRAIN_SRC_CHILD,
689d8b3afd5SKevin Wolf };
690d8b3afd5SKevin Wolf 
691d8b3afd5SKevin Wolf static void test_blockjob_common_drain_node(enum drain_type drain_type,
692d8b3afd5SKevin Wolf                                             bool use_iothread,
693d8b3afd5SKevin Wolf                                             enum test_job_result result,
694d8b3afd5SKevin Wolf                                             enum test_job_drain_node drain_node)
6957253220dSKevin Wolf {
6967253220dSKevin Wolf     BlockBackend *blk_src, *blk_target;
697d8b3afd5SKevin Wolf     BlockDriverState *src, *src_backing, *src_overlay, *target, *drain_bs;
6987253220dSKevin Wolf     BlockJob *job;
699d49725afSKevin Wolf     TestBlockJob *tjob;
700f62c1729SKevin Wolf     IOThread *iothread = NULL;
701f62c1729SKevin Wolf     AioContext *ctx;
7027253220dSKevin Wolf     int ret;
7037253220dSKevin Wolf 
7047253220dSKevin Wolf     src = bdrv_new_open_driver(&bdrv_test, "source", BDRV_O_RDWR,
7057253220dSKevin Wolf                                &error_abort);
706d8b3afd5SKevin Wolf     src_backing = bdrv_new_open_driver(&bdrv_test, "source-backing",
707d8b3afd5SKevin Wolf                                        BDRV_O_RDWR, &error_abort);
708d8b3afd5SKevin Wolf     src_overlay = bdrv_new_open_driver(&bdrv_test, "source-overlay",
709d8b3afd5SKevin Wolf                                        BDRV_O_RDWR, &error_abort);
710d8b3afd5SKevin Wolf 
711d8b3afd5SKevin Wolf     bdrv_set_backing_hd(src_overlay, src, &error_abort);
712d8b3afd5SKevin Wolf     bdrv_unref(src);
713d8b3afd5SKevin Wolf     bdrv_set_backing_hd(src, src_backing, &error_abort);
714d8b3afd5SKevin Wolf     bdrv_unref(src_backing);
715d8b3afd5SKevin Wolf 
716d861ab3aSKevin Wolf     blk_src = blk_new(qemu_get_aio_context(), BLK_PERM_ALL, BLK_PERM_ALL);
717d8b3afd5SKevin Wolf     blk_insert_bs(blk_src, src_overlay, &error_abort);
718d8b3afd5SKevin Wolf 
719d8b3afd5SKevin Wolf     switch (drain_node) {
720d8b3afd5SKevin Wolf     case TEST_JOB_DRAIN_SRC:
721d8b3afd5SKevin Wolf         drain_bs = src;
722d8b3afd5SKevin Wolf         break;
723d8b3afd5SKevin Wolf     case TEST_JOB_DRAIN_SRC_CHILD:
724d8b3afd5SKevin Wolf         drain_bs = src_backing;
725d8b3afd5SKevin Wolf         break;
726d8b3afd5SKevin Wolf     default:
727d8b3afd5SKevin Wolf         g_assert_not_reached();
728d8b3afd5SKevin Wolf     }
7297253220dSKevin Wolf 
730f62c1729SKevin Wolf     if (use_iothread) {
731f62c1729SKevin Wolf         iothread = iothread_new();
732f62c1729SKevin Wolf         ctx = iothread_get_aio_context(iothread);
73397896a48SKevin Wolf         blk_set_aio_context(blk_src, ctx, &error_abort);
734f62c1729SKevin Wolf     } else {
735f62c1729SKevin Wolf         ctx = qemu_get_aio_context();
736f62c1729SKevin Wolf     }
737f62c1729SKevin Wolf 
7387253220dSKevin Wolf     target = bdrv_new_open_driver(&bdrv_test, "target", BDRV_O_RDWR,
7397253220dSKevin Wolf                                   &error_abort);
740d861ab3aSKevin Wolf     blk_target = blk_new(qemu_get_aio_context(), BLK_PERM_ALL, BLK_PERM_ALL);
7417253220dSKevin Wolf     blk_insert_bs(blk_target, target, &error_abort);
742132ada80SKevin Wolf     blk_set_allow_aio_context_change(blk_target, true);
7437253220dSKevin Wolf 
744f62c1729SKevin Wolf     aio_context_acquire(ctx);
745d49725afSKevin Wolf     tjob = block_job_create("job0", &test_job_driver, NULL, src,
746d49725afSKevin Wolf                             0, BLK_PERM_ALL,
74775859b94SJohn Snow                             0, 0, NULL, NULL, &error_abort);
7481b177bbeSVladimir Sementsov-Ogievskiy     tjob->bs = src;
749d49725afSKevin Wolf     job = &tjob->common;
7507253220dSKevin Wolf     block_job_add_bdrv(job, "target", target, 0, BLK_PERM_ALL, &error_abort);
751d49725afSKevin Wolf 
752d49725afSKevin Wolf     switch (result) {
753d49725afSKevin Wolf     case TEST_JOB_SUCCESS:
754d49725afSKevin Wolf         break;
755d49725afSKevin Wolf     case TEST_JOB_FAIL_RUN:
756d49725afSKevin Wolf         tjob->run_ret = -EIO;
757d49725afSKevin Wolf         break;
758d49725afSKevin Wolf     case TEST_JOB_FAIL_PREPARE:
759d49725afSKevin Wolf         tjob->prepare_ret = -EIO;
760d49725afSKevin Wolf         break;
761d49725afSKevin Wolf     }
7626f592e5aSEmanuele Giuseppe Esposito     aio_context_release(ctx);
763d49725afSKevin Wolf 
764da01ff7fSKevin Wolf     job_start(&job->job);
7657253220dSKevin Wolf 
766d8b3afd5SKevin Wolf     if (use_iothread) {
767d8b3afd5SKevin Wolf         /* job_co_entry() is run in the I/O thread, wait for the actual job
768d8b3afd5SKevin Wolf          * code to start (we don't want to catch the job in the pause point in
769d8b3afd5SKevin Wolf          * job_co_entry(). */
770d8b3afd5SKevin Wolf         while (!tjob->running) {
771d8b3afd5SKevin Wolf             aio_poll(qemu_get_aio_context(), false);
772d8b3afd5SKevin Wolf         }
773d8b3afd5SKevin Wolf     }
774d8b3afd5SKevin Wolf 
775191e7af3SEmanuele Giuseppe Esposito     WITH_JOB_LOCK_GUARD() {
776da01ff7fSKevin Wolf         g_assert_cmpint(job->job.pause_count, ==, 0);
777da01ff7fSKevin Wolf         g_assert_false(job->job.paused);
778d8b3afd5SKevin Wolf         g_assert_true(tjob->running);
7795599c162SKevin Wolf         g_assert_true(job->job.busy); /* We're in qemu_co_sleep_ns() */
780191e7af3SEmanuele Giuseppe Esposito     }
7817253220dSKevin Wolf 
782d8b3afd5SKevin Wolf     do_drain_begin_unlocked(drain_type, drain_bs);
7837253220dSKevin Wolf 
784191e7af3SEmanuele Giuseppe Esposito     WITH_JOB_LOCK_GUARD() {
7857253220dSKevin Wolf         if (drain_type == BDRV_DRAIN_ALL) {
78681193349SKevin Wolf             /* bdrv_drain_all() drains both src and target */
787da01ff7fSKevin Wolf             g_assert_cmpint(job->job.pause_count, ==, 2);
7887253220dSKevin Wolf         } else {
789da01ff7fSKevin Wolf             g_assert_cmpint(job->job.pause_count, ==, 1);
7907253220dSKevin Wolf         }
79189bd0305SKevin Wolf         g_assert_true(job->job.paused);
792da01ff7fSKevin Wolf         g_assert_false(job->job.busy); /* The job is paused */
793191e7af3SEmanuele Giuseppe Esposito     }
7947253220dSKevin Wolf 
795d8b3afd5SKevin Wolf     do_drain_end_unlocked(drain_type, drain_bs);
796f62c1729SKevin Wolf 
797f62c1729SKevin Wolf     if (use_iothread) {
798191e7af3SEmanuele Giuseppe Esposito         /*
799191e7af3SEmanuele Giuseppe Esposito          * Here we are waiting for the paused status to change,
800191e7af3SEmanuele Giuseppe Esposito          * so don't bother protecting the read every time.
801191e7af3SEmanuele Giuseppe Esposito          *
802191e7af3SEmanuele Giuseppe Esposito          * paused is reset in the I/O thread, wait for it
803191e7af3SEmanuele Giuseppe Esposito          */
804f62c1729SKevin Wolf         while (job->job.paused) {
805f62c1729SKevin Wolf             aio_poll(qemu_get_aio_context(), false);
806f62c1729SKevin Wolf         }
807f62c1729SKevin Wolf     }
8087253220dSKevin Wolf 
809191e7af3SEmanuele Giuseppe Esposito     WITH_JOB_LOCK_GUARD() {
810da01ff7fSKevin Wolf         g_assert_cmpint(job->job.pause_count, ==, 0);
811da01ff7fSKevin Wolf         g_assert_false(job->job.paused);
81289bd0305SKevin Wolf         g_assert_true(job->job.busy); /* We're in qemu_co_sleep_ns() */
813191e7af3SEmanuele Giuseppe Esposito     }
8147253220dSKevin Wolf 
815132ada80SKevin Wolf     do_drain_begin_unlocked(drain_type, target);
8167253220dSKevin Wolf 
817191e7af3SEmanuele Giuseppe Esposito     WITH_JOB_LOCK_GUARD() {
8187253220dSKevin Wolf         if (drain_type == BDRV_DRAIN_ALL) {
81981193349SKevin Wolf             /* bdrv_drain_all() drains both src and target */
820da01ff7fSKevin Wolf             g_assert_cmpint(job->job.pause_count, ==, 2);
8217253220dSKevin Wolf         } else {
822da01ff7fSKevin Wolf             g_assert_cmpint(job->job.pause_count, ==, 1);
8237253220dSKevin Wolf         }
82489bd0305SKevin Wolf         g_assert_true(job->job.paused);
825da01ff7fSKevin Wolf         g_assert_false(job->job.busy); /* The job is paused */
826191e7af3SEmanuele Giuseppe Esposito     }
8277253220dSKevin Wolf 
828132ada80SKevin Wolf     do_drain_end_unlocked(drain_type, target);
8297253220dSKevin Wolf 
830f62c1729SKevin Wolf     if (use_iothread) {
831191e7af3SEmanuele Giuseppe Esposito         /*
832191e7af3SEmanuele Giuseppe Esposito          * Here we are waiting for the paused status to change,
833191e7af3SEmanuele Giuseppe Esposito          * so don't bother protecting the read every time.
834191e7af3SEmanuele Giuseppe Esposito          *
835191e7af3SEmanuele Giuseppe Esposito          * paused is reset in the I/O thread, wait for it
836191e7af3SEmanuele Giuseppe Esposito          */
837f62c1729SKevin Wolf         while (job->job.paused) {
838f62c1729SKevin Wolf             aio_poll(qemu_get_aio_context(), false);
839f62c1729SKevin Wolf         }
840f62c1729SKevin Wolf     }
841f62c1729SKevin Wolf 
842191e7af3SEmanuele Giuseppe Esposito     WITH_JOB_LOCK_GUARD() {
843da01ff7fSKevin Wolf         g_assert_cmpint(job->job.pause_count, ==, 0);
844da01ff7fSKevin Wolf         g_assert_false(job->job.paused);
8455599c162SKevin Wolf         g_assert_true(job->job.busy); /* We're in qemu_co_sleep_ns() */
846191e7af3SEmanuele Giuseppe Esposito     }
8477253220dSKevin Wolf 
848191e7af3SEmanuele Giuseppe Esposito     WITH_JOB_LOCK_GUARD() {
849191e7af3SEmanuele Giuseppe Esposito         ret = job_complete_sync_locked(&job->job, &error_abort);
850191e7af3SEmanuele Giuseppe Esposito     }
851d49725afSKevin Wolf     g_assert_cmpint(ret, ==, (result == TEST_JOB_SUCCESS ? 0 : -EIO));
8527253220dSKevin Wolf 
8536f592e5aSEmanuele Giuseppe Esposito     aio_context_acquire(ctx);
854f62c1729SKevin Wolf     if (use_iothread) {
85597896a48SKevin Wolf         blk_set_aio_context(blk_src, qemu_get_aio_context(), &error_abort);
856ad943dcbSKevin Wolf         assert(blk_get_aio_context(blk_target) == qemu_get_aio_context());
857f62c1729SKevin Wolf     }
858f62c1729SKevin Wolf     aio_context_release(ctx);
859f62c1729SKevin Wolf 
8607253220dSKevin Wolf     blk_unref(blk_src);
8617253220dSKevin Wolf     blk_unref(blk_target);
862d8b3afd5SKevin Wolf     bdrv_unref(src_overlay);
8637253220dSKevin Wolf     bdrv_unref(target);
864f62c1729SKevin Wolf 
865f62c1729SKevin Wolf     if (iothread) {
866f62c1729SKevin Wolf         iothread_join(iothread);
867f62c1729SKevin Wolf     }
8687253220dSKevin Wolf }
8697253220dSKevin Wolf 
870d8b3afd5SKevin Wolf static void test_blockjob_common(enum drain_type drain_type, bool use_iothread,
871d8b3afd5SKevin Wolf                                  enum test_job_result result)
872d8b3afd5SKevin Wolf {
873d8b3afd5SKevin Wolf     test_blockjob_common_drain_node(drain_type, use_iothread, result,
874d8b3afd5SKevin Wolf                                     TEST_JOB_DRAIN_SRC);
875d8b3afd5SKevin Wolf     test_blockjob_common_drain_node(drain_type, use_iothread, result,
876d8b3afd5SKevin Wolf                                     TEST_JOB_DRAIN_SRC_CHILD);
877d8b3afd5SKevin Wolf }
878d8b3afd5SKevin Wolf 
8797253220dSKevin Wolf static void test_blockjob_drain_all(void)
8807253220dSKevin Wolf {
881d49725afSKevin Wolf     test_blockjob_common(BDRV_DRAIN_ALL, false, TEST_JOB_SUCCESS);
8827253220dSKevin Wolf }
8837253220dSKevin Wolf 
8847253220dSKevin Wolf static void test_blockjob_drain(void)
8857253220dSKevin Wolf {
886d49725afSKevin Wolf     test_blockjob_common(BDRV_DRAIN, false, TEST_JOB_SUCCESS);
8877253220dSKevin Wolf }
8887253220dSKevin Wolf 
889d49725afSKevin Wolf static void test_blockjob_error_drain_all(void)
890d49725afSKevin Wolf {
891d49725afSKevin Wolf     test_blockjob_common(BDRV_DRAIN_ALL, false, TEST_JOB_FAIL_RUN);
892d49725afSKevin Wolf     test_blockjob_common(BDRV_DRAIN_ALL, false, TEST_JOB_FAIL_PREPARE);
893d49725afSKevin Wolf }
894d49725afSKevin Wolf 
895d49725afSKevin Wolf static void test_blockjob_error_drain(void)
896d49725afSKevin Wolf {
897d49725afSKevin Wolf     test_blockjob_common(BDRV_DRAIN, false, TEST_JOB_FAIL_RUN);
898d49725afSKevin Wolf     test_blockjob_common(BDRV_DRAIN, false, TEST_JOB_FAIL_PREPARE);
899d49725afSKevin Wolf }
900d49725afSKevin Wolf 
901f62c1729SKevin Wolf static void test_blockjob_iothread_drain_all(void)
902f62c1729SKevin Wolf {
903d49725afSKevin Wolf     test_blockjob_common(BDRV_DRAIN_ALL, true, TEST_JOB_SUCCESS);
904f62c1729SKevin Wolf }
905f62c1729SKevin Wolf 
906f62c1729SKevin Wolf static void test_blockjob_iothread_drain(void)
907f62c1729SKevin Wolf {
908d49725afSKevin Wolf     test_blockjob_common(BDRV_DRAIN, true, TEST_JOB_SUCCESS);
909f62c1729SKevin Wolf }
910f62c1729SKevin Wolf 
911d49725afSKevin Wolf static void test_blockjob_iothread_error_drain_all(void)
912d49725afSKevin Wolf {
913d49725afSKevin Wolf     test_blockjob_common(BDRV_DRAIN_ALL, true, TEST_JOB_FAIL_RUN);
914d49725afSKevin Wolf     test_blockjob_common(BDRV_DRAIN_ALL, true, TEST_JOB_FAIL_PREPARE);
915d49725afSKevin Wolf }
916d49725afSKevin Wolf 
917d49725afSKevin Wolf static void test_blockjob_iothread_error_drain(void)
918d49725afSKevin Wolf {
919d49725afSKevin Wolf     test_blockjob_common(BDRV_DRAIN, true, TEST_JOB_FAIL_RUN);
920d49725afSKevin Wolf     test_blockjob_common(BDRV_DRAIN, true, TEST_JOB_FAIL_PREPARE);
921d49725afSKevin Wolf }
922d49725afSKevin Wolf 
9234c8158e3SMax Reitz 
9244c8158e3SMax Reitz typedef struct BDRVTestTopState {
9254c8158e3SMax Reitz     BdrvChild *wait_child;
9264c8158e3SMax Reitz } BDRVTestTopState;
9274c8158e3SMax Reitz 
9284c8158e3SMax Reitz static void bdrv_test_top_close(BlockDriverState *bs)
9294c8158e3SMax Reitz {
9304c8158e3SMax Reitz     BdrvChild *c, *next_c;
9314c8158e3SMax Reitz     QLIST_FOREACH_SAFE(c, &bs->children, next, next_c) {
9324c8158e3SMax Reitz         bdrv_unref_child(bs, c);
9334c8158e3SMax Reitz     }
9344c8158e3SMax Reitz }
9354c8158e3SMax Reitz 
9364c8158e3SMax Reitz static int coroutine_fn bdrv_test_top_co_preadv(BlockDriverState *bs,
937f7ef38ddSVladimir Sementsov-Ogievskiy                                                 int64_t offset, int64_t bytes,
938f7ef38ddSVladimir Sementsov-Ogievskiy                                                 QEMUIOVector *qiov,
939f7ef38ddSVladimir Sementsov-Ogievskiy                                                 BdrvRequestFlags flags)
9404c8158e3SMax Reitz {
9414c8158e3SMax Reitz     BDRVTestTopState *tts = bs->opaque;
9424c8158e3SMax Reitz     return bdrv_co_preadv(tts->wait_child, offset, bytes, qiov, flags);
9434c8158e3SMax Reitz }
9444c8158e3SMax Reitz 
9454c8158e3SMax Reitz static BlockDriver bdrv_test_top_driver = {
9464c8158e3SMax Reitz     .format_name            = "test_top_driver",
9474c8158e3SMax Reitz     .instance_size          = sizeof(BDRVTestTopState),
9484c8158e3SMax Reitz 
9494c8158e3SMax Reitz     .bdrv_close             = bdrv_test_top_close,
9504c8158e3SMax Reitz     .bdrv_co_preadv         = bdrv_test_top_co_preadv,
9514c8158e3SMax Reitz 
95269dca43dSMax Reitz     .bdrv_child_perm        = bdrv_default_perms,
9534c8158e3SMax Reitz };
9544c8158e3SMax Reitz 
9554c8158e3SMax Reitz typedef struct TestCoDeleteByDrainData {
9564c8158e3SMax Reitz     BlockBackend *blk;
9574c8158e3SMax Reitz     bool detach_instead_of_delete;
9584c8158e3SMax Reitz     bool done;
9594c8158e3SMax Reitz } TestCoDeleteByDrainData;
9604c8158e3SMax Reitz 
9614c8158e3SMax Reitz static void coroutine_fn test_co_delete_by_drain(void *opaque)
9624c8158e3SMax Reitz {
9634c8158e3SMax Reitz     TestCoDeleteByDrainData *dbdd = opaque;
9644c8158e3SMax Reitz     BlockBackend *blk = dbdd->blk;
9654c8158e3SMax Reitz     BlockDriverState *bs = blk_bs(blk);
9664c8158e3SMax Reitz     BDRVTestTopState *tts = bs->opaque;
9674c8158e3SMax Reitz     void *buffer = g_malloc(65536);
968405d8fe0SVladimir Sementsov-Ogievskiy     QEMUIOVector qiov = QEMU_IOVEC_INIT_BUF(qiov, buffer, 65536);
9694c8158e3SMax Reitz 
9704c8158e3SMax Reitz     /* Pretend some internal write operation from parent to child.
9714c8158e3SMax Reitz      * Important: We have to read from the child, not from the parent!
9724c8158e3SMax Reitz      * Draining works by first propagating it all up the tree to the
9734c8158e3SMax Reitz      * root and then waiting for drainage from root to the leaves
9744c8158e3SMax Reitz      * (protocol nodes).  If we have a request waiting on the root,
9754c8158e3SMax Reitz      * everything will be drained before we go back down the tree, but
9764c8158e3SMax Reitz      * we do not want that.  We want to be in the middle of draining
9774c8158e3SMax Reitz      * when this following requests returns. */
9784c8158e3SMax Reitz     bdrv_co_preadv(tts->wait_child, 0, 65536, &qiov, 0);
9794c8158e3SMax Reitz 
9804c8158e3SMax Reitz     g_assert_cmpint(bs->refcnt, ==, 1);
9814c8158e3SMax Reitz 
9824c8158e3SMax Reitz     if (!dbdd->detach_instead_of_delete) {
9834c8158e3SMax Reitz         blk_unref(blk);
9844c8158e3SMax Reitz     } else {
9854c8158e3SMax Reitz         BdrvChild *c, *next_c;
9864c8158e3SMax Reitz         QLIST_FOREACH_SAFE(c, &bs->children, next, next_c) {
9874c8158e3SMax Reitz             bdrv_unref_child(bs, c);
9884c8158e3SMax Reitz         }
9894c8158e3SMax Reitz     }
9904c8158e3SMax Reitz 
9914c8158e3SMax Reitz     dbdd->done = true;
9927b43db3cSMarc-André Lureau     g_free(buffer);
9934c8158e3SMax Reitz }
9944c8158e3SMax Reitz 
9954c8158e3SMax Reitz /**
9964c8158e3SMax Reitz  * Test what happens when some BDS has some children, you drain one of
9974c8158e3SMax Reitz  * them and this results in the BDS being deleted.
9984c8158e3SMax Reitz  *
9994c8158e3SMax Reitz  * If @detach_instead_of_delete is set, the BDS is not going to be
10004c8158e3SMax Reitz  * deleted but will only detach all of its children.
10014c8158e3SMax Reitz  */
1002ebd31837SKevin Wolf static void do_test_delete_by_drain(bool detach_instead_of_delete,
1003ebd31837SKevin Wolf                                     enum drain_type drain_type)
10044c8158e3SMax Reitz {
10054c8158e3SMax Reitz     BlockBackend *blk;
10064c8158e3SMax Reitz     BlockDriverState *bs, *child_bs, *null_bs;
10074c8158e3SMax Reitz     BDRVTestTopState *tts;
10084c8158e3SMax Reitz     TestCoDeleteByDrainData dbdd;
10094c8158e3SMax Reitz     Coroutine *co;
10104c8158e3SMax Reitz 
10114c8158e3SMax Reitz     bs = bdrv_new_open_driver(&bdrv_test_top_driver, "top", BDRV_O_RDWR,
10124c8158e3SMax Reitz                               &error_abort);
10134c8158e3SMax Reitz     bs->total_sectors = 65536 >> BDRV_SECTOR_BITS;
10144c8158e3SMax Reitz     tts = bs->opaque;
10154c8158e3SMax Reitz 
10164c8158e3SMax Reitz     null_bs = bdrv_open("null-co://", NULL, NULL, BDRV_O_RDWR | BDRV_O_PROTOCOL,
10174c8158e3SMax Reitz                         &error_abort);
1018a16be3cdSMax Reitz     bdrv_attach_child(bs, null_bs, "null-child", &child_of_bds,
1019a16be3cdSMax Reitz                       BDRV_CHILD_DATA, &error_abort);
10204c8158e3SMax Reitz 
10214c8158e3SMax Reitz     /* This child will be the one to pass to requests through to, and
10224c8158e3SMax Reitz      * it will stall until a drain occurs */
10234c8158e3SMax Reitz     child_bs = bdrv_new_open_driver(&bdrv_test, "child", BDRV_O_RDWR,
10244c8158e3SMax Reitz                                     &error_abort);
10254c8158e3SMax Reitz     child_bs->total_sectors = 65536 >> BDRV_SECTOR_BITS;
10264c8158e3SMax Reitz     /* Takes our reference to child_bs */
1027a16be3cdSMax Reitz     tts->wait_child = bdrv_attach_child(bs, child_bs, "wait-child",
1028a16be3cdSMax Reitz                                         &child_of_bds,
1029a16be3cdSMax Reitz                                         BDRV_CHILD_DATA | BDRV_CHILD_PRIMARY,
1030a16be3cdSMax Reitz                                         &error_abort);
10314c8158e3SMax Reitz 
10324c8158e3SMax Reitz     /* This child is just there to be deleted
10334c8158e3SMax Reitz      * (for detach_instead_of_delete == true) */
10344c8158e3SMax Reitz     null_bs = bdrv_open("null-co://", NULL, NULL, BDRV_O_RDWR | BDRV_O_PROTOCOL,
10354c8158e3SMax Reitz                         &error_abort);
1036a16be3cdSMax Reitz     bdrv_attach_child(bs, null_bs, "null-child", &child_of_bds, BDRV_CHILD_DATA,
1037a16be3cdSMax Reitz                       &error_abort);
10384c8158e3SMax Reitz 
1039d861ab3aSKevin Wolf     blk = blk_new(qemu_get_aio_context(), BLK_PERM_ALL, BLK_PERM_ALL);
10404c8158e3SMax Reitz     blk_insert_bs(blk, bs, &error_abort);
10414c8158e3SMax Reitz 
10424c8158e3SMax Reitz     /* Referenced by blk now */
10434c8158e3SMax Reitz     bdrv_unref(bs);
10444c8158e3SMax Reitz 
10454c8158e3SMax Reitz     g_assert_cmpint(bs->refcnt, ==, 1);
10464c8158e3SMax Reitz     g_assert_cmpint(child_bs->refcnt, ==, 1);
10474c8158e3SMax Reitz     g_assert_cmpint(null_bs->refcnt, ==, 1);
10484c8158e3SMax Reitz 
10494c8158e3SMax Reitz 
10504c8158e3SMax Reitz     dbdd = (TestCoDeleteByDrainData){
10514c8158e3SMax Reitz         .blk = blk,
10524c8158e3SMax Reitz         .detach_instead_of_delete = detach_instead_of_delete,
10534c8158e3SMax Reitz         .done = false,
10544c8158e3SMax Reitz     };
10554c8158e3SMax Reitz     co = qemu_coroutine_create(test_co_delete_by_drain, &dbdd);
10564c8158e3SMax Reitz     qemu_coroutine_enter(co);
10574c8158e3SMax Reitz 
10584c8158e3SMax Reitz     /* Drain the child while the read operation is still pending.
10594c8158e3SMax Reitz      * This should result in the operation finishing and
10604c8158e3SMax Reitz      * test_co_delete_by_drain() resuming.  Thus, @bs will be deleted
10614c8158e3SMax Reitz      * and the coroutine will exit while this drain operation is still
10624c8158e3SMax Reitz      * in progress. */
1063ebd31837SKevin Wolf     switch (drain_type) {
1064ebd31837SKevin Wolf     case BDRV_DRAIN:
10654c8158e3SMax Reitz         bdrv_ref(child_bs);
10664c8158e3SMax Reitz         bdrv_drain(child_bs);
10674c8158e3SMax Reitz         bdrv_unref(child_bs);
1068ebd31837SKevin Wolf         break;
106919f7a7e5SKevin Wolf     case BDRV_DRAIN_ALL:
107019f7a7e5SKevin Wolf         bdrv_drain_all_begin();
107119f7a7e5SKevin Wolf         bdrv_drain_all_end();
107219f7a7e5SKevin Wolf         break;
1073ebd31837SKevin Wolf     default:
1074ebd31837SKevin Wolf         g_assert_not_reached();
1075ebd31837SKevin Wolf     }
10764c8158e3SMax Reitz 
10774c8158e3SMax Reitz     while (!dbdd.done) {
10784c8158e3SMax Reitz         aio_poll(qemu_get_aio_context(), true);
10794c8158e3SMax Reitz     }
10804c8158e3SMax Reitz 
10814c8158e3SMax Reitz     if (detach_instead_of_delete) {
10824c8158e3SMax Reitz         /* Here, the reference has not passed over to the coroutine,
10834c8158e3SMax Reitz          * so we have to delete the BB ourselves */
10844c8158e3SMax Reitz         blk_unref(blk);
10854c8158e3SMax Reitz     }
10864c8158e3SMax Reitz }
10874c8158e3SMax Reitz 
10884c8158e3SMax Reitz static void test_delete_by_drain(void)
10894c8158e3SMax Reitz {
1090ebd31837SKevin Wolf     do_test_delete_by_drain(false, BDRV_DRAIN);
10914c8158e3SMax Reitz }
10924c8158e3SMax Reitz 
109319f7a7e5SKevin Wolf static void test_detach_by_drain_all(void)
109419f7a7e5SKevin Wolf {
109519f7a7e5SKevin Wolf     do_test_delete_by_drain(true, BDRV_DRAIN_ALL);
109619f7a7e5SKevin Wolf }
109719f7a7e5SKevin Wolf 
10984c8158e3SMax Reitz static void test_detach_by_drain(void)
10994c8158e3SMax Reitz {
1100ebd31837SKevin Wolf     do_test_delete_by_drain(true, BDRV_DRAIN);
1101ebd31837SKevin Wolf }
1102ebd31837SKevin Wolf 
11034c8158e3SMax Reitz 
1104231281abSKevin Wolf struct detach_by_parent_data {
1105231281abSKevin Wolf     BlockDriverState *parent_b;
1106231281abSKevin Wolf     BdrvChild *child_b;
1107231281abSKevin Wolf     BlockDriverState *c;
1108231281abSKevin Wolf     BdrvChild *child_c;
110957320ca9SKevin Wolf     bool by_parent_cb;
1110231281abSKevin Wolf };
111157320ca9SKevin Wolf static struct detach_by_parent_data detach_by_parent_data;
1112231281abSKevin Wolf 
111357320ca9SKevin Wolf static void detach_indirect_bh(void *opaque)
1114231281abSKevin Wolf {
1115231281abSKevin Wolf     struct detach_by_parent_data *data = opaque;
1116231281abSKevin Wolf 
1117231281abSKevin Wolf     bdrv_unref_child(data->parent_b, data->child_b);
1118231281abSKevin Wolf 
1119231281abSKevin Wolf     bdrv_ref(data->c);
1120231281abSKevin Wolf     data->child_c = bdrv_attach_child(data->parent_b, data->c, "PB-C",
1121a16be3cdSMax Reitz                                       &child_of_bds, BDRV_CHILD_DATA,
1122a16be3cdSMax Reitz                                       &error_abort);
1123231281abSKevin Wolf }
1124231281abSKevin Wolf 
112557320ca9SKevin Wolf static void detach_by_parent_aio_cb(void *opaque, int ret)
112657320ca9SKevin Wolf {
112757320ca9SKevin Wolf     struct detach_by_parent_data *data = &detach_by_parent_data;
112857320ca9SKevin Wolf 
112957320ca9SKevin Wolf     g_assert_cmpint(ret, ==, 0);
113057320ca9SKevin Wolf     if (data->by_parent_cb) {
113157320ca9SKevin Wolf         detach_indirect_bh(data);
113257320ca9SKevin Wolf     }
113357320ca9SKevin Wolf }
113457320ca9SKevin Wolf 
113557320ca9SKevin Wolf static void detach_by_driver_cb_drained_begin(BdrvChild *child)
113657320ca9SKevin Wolf {
113757320ca9SKevin Wolf     aio_bh_schedule_oneshot(qemu_get_current_aio_context(),
113857320ca9SKevin Wolf                             detach_indirect_bh, &detach_by_parent_data);
1139a16be3cdSMax Reitz     child_of_bds.drained_begin(child);
114057320ca9SKevin Wolf }
114157320ca9SKevin Wolf 
1142bd86fb99SMax Reitz static BdrvChildClass detach_by_driver_cb_class;
114357320ca9SKevin Wolf 
1144231281abSKevin Wolf /*
1145231281abSKevin Wolf  * Initial graph:
1146231281abSKevin Wolf  *
1147231281abSKevin Wolf  * PA     PB
1148231281abSKevin Wolf  *    \ /   \
1149231281abSKevin Wolf  *     A     B     C
1150231281abSKevin Wolf  *
115157320ca9SKevin Wolf  * by_parent_cb == true:  Test that parent callbacks don't poll
115257320ca9SKevin Wolf  *
115357320ca9SKevin Wolf  *     PA has a pending write request whose callback changes the child nodes of
115457320ca9SKevin Wolf  *     PB: It removes B and adds C instead. The subtree of PB is drained, which
115557320ca9SKevin Wolf  *     will indirectly drain the write request, too.
115657320ca9SKevin Wolf  *
115757320ca9SKevin Wolf  * by_parent_cb == false: Test that bdrv_drain_invoke() doesn't poll
115857320ca9SKevin Wolf  *
1159bd86fb99SMax Reitz  *     PA's BdrvChildClass has a .drained_begin callback that schedules a BH
116057320ca9SKevin Wolf  *     that does the same graph change. If bdrv_drain_invoke() calls it, the
116157320ca9SKevin Wolf  *     state is messed up, but if it is only polled in the single
116257320ca9SKevin Wolf  *     BDRV_POLL_WHILE() at the end of the drain, this should work fine.
1163231281abSKevin Wolf  */
116457320ca9SKevin Wolf static void test_detach_indirect(bool by_parent_cb)
1165231281abSKevin Wolf {
1166231281abSKevin Wolf     BlockBackend *blk;
1167231281abSKevin Wolf     BlockDriverState *parent_a, *parent_b, *a, *b, *c;
1168231281abSKevin Wolf     BdrvChild *child_a, *child_b;
1169231281abSKevin Wolf     BlockAIOCB *acb;
1170231281abSKevin Wolf 
1171405d8fe0SVladimir Sementsov-Ogievskiy     QEMUIOVector qiov = QEMU_IOVEC_INIT_BUF(qiov, NULL, 0);
1172231281abSKevin Wolf 
117357320ca9SKevin Wolf     if (!by_parent_cb) {
1174a16be3cdSMax Reitz         detach_by_driver_cb_class = child_of_bds;
1175bd86fb99SMax Reitz         detach_by_driver_cb_class.drained_begin =
117657320ca9SKevin Wolf             detach_by_driver_cb_drained_begin;
117757320ca9SKevin Wolf     }
117857320ca9SKevin Wolf 
1179231281abSKevin Wolf     /* Create all involved nodes */
1180231281abSKevin Wolf     parent_a = bdrv_new_open_driver(&bdrv_test, "parent-a", BDRV_O_RDWR,
1181231281abSKevin Wolf                                     &error_abort);
1182231281abSKevin Wolf     parent_b = bdrv_new_open_driver(&bdrv_test, "parent-b", 0,
1183231281abSKevin Wolf                                     &error_abort);
1184231281abSKevin Wolf 
1185231281abSKevin Wolf     a = bdrv_new_open_driver(&bdrv_test, "a", BDRV_O_RDWR, &error_abort);
1186231281abSKevin Wolf     b = bdrv_new_open_driver(&bdrv_test, "b", BDRV_O_RDWR, &error_abort);
1187231281abSKevin Wolf     c = bdrv_new_open_driver(&bdrv_test, "c", BDRV_O_RDWR, &error_abort);
1188231281abSKevin Wolf 
1189231281abSKevin Wolf     /* blk is a BB for parent-a */
1190d861ab3aSKevin Wolf     blk = blk_new(qemu_get_aio_context(), BLK_PERM_ALL, BLK_PERM_ALL);
1191231281abSKevin Wolf     blk_insert_bs(blk, parent_a, &error_abort);
1192231281abSKevin Wolf     bdrv_unref(parent_a);
1193231281abSKevin Wolf 
119457320ca9SKevin Wolf     /* If we want to get bdrv_drain_invoke() to call aio_poll(), the driver
119557320ca9SKevin Wolf      * callback must not return immediately. */
119657320ca9SKevin Wolf     if (!by_parent_cb) {
119757320ca9SKevin Wolf         BDRVTestState *s = parent_a->opaque;
119857320ca9SKevin Wolf         s->sleep_in_drain_begin = true;
119957320ca9SKevin Wolf     }
120057320ca9SKevin Wolf 
1201231281abSKevin Wolf     /* Set child relationships */
1202231281abSKevin Wolf     bdrv_ref(b);
1203231281abSKevin Wolf     bdrv_ref(a);
1204a16be3cdSMax Reitz     child_b = bdrv_attach_child(parent_b, b, "PB-B", &child_of_bds,
1205a16be3cdSMax Reitz                                 BDRV_CHILD_DATA, &error_abort);
120625191e5fSMax Reitz     child_a = bdrv_attach_child(parent_b, a, "PB-A", &child_of_bds,
120725191e5fSMax Reitz                                 BDRV_CHILD_COW, &error_abort);
1208231281abSKevin Wolf 
1209231281abSKevin Wolf     bdrv_ref(a);
121057320ca9SKevin Wolf     bdrv_attach_child(parent_a, a, "PA-A",
1211a16be3cdSMax Reitz                       by_parent_cb ? &child_of_bds : &detach_by_driver_cb_class,
1212a16be3cdSMax Reitz                       BDRV_CHILD_DATA, &error_abort);
1213231281abSKevin Wolf 
1214231281abSKevin Wolf     g_assert_cmpint(parent_a->refcnt, ==, 1);
1215231281abSKevin Wolf     g_assert_cmpint(parent_b->refcnt, ==, 1);
1216231281abSKevin Wolf     g_assert_cmpint(a->refcnt, ==, 3);
1217231281abSKevin Wolf     g_assert_cmpint(b->refcnt, ==, 2);
1218231281abSKevin Wolf     g_assert_cmpint(c->refcnt, ==, 1);
1219231281abSKevin Wolf 
1220231281abSKevin Wolf     g_assert(QLIST_FIRST(&parent_b->children) == child_a);
1221231281abSKevin Wolf     g_assert(QLIST_NEXT(child_a, next) == child_b);
1222231281abSKevin Wolf     g_assert(QLIST_NEXT(child_b, next) == NULL);
1223231281abSKevin Wolf 
1224231281abSKevin Wolf     /* Start the evil write request */
122557320ca9SKevin Wolf     detach_by_parent_data = (struct detach_by_parent_data) {
1226231281abSKevin Wolf         .parent_b = parent_b,
1227231281abSKevin Wolf         .child_b = child_b,
1228231281abSKevin Wolf         .c = c,
122957320ca9SKevin Wolf         .by_parent_cb = by_parent_cb,
1230231281abSKevin Wolf     };
123157320ca9SKevin Wolf     acb = blk_aio_preadv(blk, 0, &qiov, 0, detach_by_parent_aio_cb, NULL);
1232231281abSKevin Wolf     g_assert(acb != NULL);
1233231281abSKevin Wolf 
1234231281abSKevin Wolf     /* Drain and check the expected result */
1235299403aeSKevin Wolf     bdrv_drained_begin(parent_b);
1236299403aeSKevin Wolf     bdrv_drained_begin(a);
1237299403aeSKevin Wolf     bdrv_drained_begin(b);
1238299403aeSKevin Wolf     bdrv_drained_begin(c);
1239231281abSKevin Wolf 
124057320ca9SKevin Wolf     g_assert(detach_by_parent_data.child_c != NULL);
1241231281abSKevin Wolf 
1242231281abSKevin Wolf     g_assert_cmpint(parent_a->refcnt, ==, 1);
1243231281abSKevin Wolf     g_assert_cmpint(parent_b->refcnt, ==, 1);
1244231281abSKevin Wolf     g_assert_cmpint(a->refcnt, ==, 3);
1245231281abSKevin Wolf     g_assert_cmpint(b->refcnt, ==, 1);
1246231281abSKevin Wolf     g_assert_cmpint(c->refcnt, ==, 2);
1247231281abSKevin Wolf 
124857320ca9SKevin Wolf     g_assert(QLIST_FIRST(&parent_b->children) == detach_by_parent_data.child_c);
124957320ca9SKevin Wolf     g_assert(QLIST_NEXT(detach_by_parent_data.child_c, next) == child_a);
1250231281abSKevin Wolf     g_assert(QLIST_NEXT(child_a, next) == NULL);
1251231281abSKevin Wolf 
1252231281abSKevin Wolf     g_assert_cmpint(parent_a->quiesce_counter, ==, 1);
1253299403aeSKevin Wolf     g_assert_cmpint(parent_b->quiesce_counter, ==, 3);
1254231281abSKevin Wolf     g_assert_cmpint(a->quiesce_counter, ==, 1);
1255299403aeSKevin Wolf     g_assert_cmpint(b->quiesce_counter, ==, 1);
1256231281abSKevin Wolf     g_assert_cmpint(c->quiesce_counter, ==, 1);
1257231281abSKevin Wolf 
1258299403aeSKevin Wolf     bdrv_drained_end(parent_b);
1259299403aeSKevin Wolf     bdrv_drained_end(a);
1260299403aeSKevin Wolf     bdrv_drained_end(b);
1261299403aeSKevin Wolf     bdrv_drained_end(c);
1262231281abSKevin Wolf 
1263231281abSKevin Wolf     bdrv_unref(parent_b);
1264231281abSKevin Wolf     blk_unref(blk);
1265231281abSKevin Wolf 
1266231281abSKevin Wolf     g_assert_cmpint(a->refcnt, ==, 1);
1267231281abSKevin Wolf     g_assert_cmpint(b->refcnt, ==, 1);
1268231281abSKevin Wolf     g_assert_cmpint(c->refcnt, ==, 1);
1269231281abSKevin Wolf     bdrv_unref(a);
1270231281abSKevin Wolf     bdrv_unref(b);
1271231281abSKevin Wolf     bdrv_unref(c);
1272231281abSKevin Wolf }
1273231281abSKevin Wolf 
127457320ca9SKevin Wolf static void test_detach_by_parent_cb(void)
127557320ca9SKevin Wolf {
127657320ca9SKevin Wolf     test_detach_indirect(true);
127757320ca9SKevin Wolf }
127857320ca9SKevin Wolf 
127957320ca9SKevin Wolf static void test_detach_by_driver_cb(void)
128057320ca9SKevin Wolf {
128157320ca9SKevin Wolf     test_detach_indirect(false);
128257320ca9SKevin Wolf }
1283231281abSKevin Wolf 
1284b994c5bcSKevin Wolf static void test_append_to_drained(void)
1285b994c5bcSKevin Wolf {
1286b994c5bcSKevin Wolf     BlockBackend *blk;
1287b994c5bcSKevin Wolf     BlockDriverState *base, *overlay;
1288b994c5bcSKevin Wolf     BDRVTestState *base_s, *overlay_s;
1289b994c5bcSKevin Wolf 
1290d861ab3aSKevin Wolf     blk = blk_new(qemu_get_aio_context(), BLK_PERM_ALL, BLK_PERM_ALL);
1291b994c5bcSKevin Wolf     base = bdrv_new_open_driver(&bdrv_test, "base", BDRV_O_RDWR, &error_abort);
1292b994c5bcSKevin Wolf     base_s = base->opaque;
1293b994c5bcSKevin Wolf     blk_insert_bs(blk, base, &error_abort);
1294b994c5bcSKevin Wolf 
1295b994c5bcSKevin Wolf     overlay = bdrv_new_open_driver(&bdrv_test, "overlay", BDRV_O_RDWR,
1296b994c5bcSKevin Wolf                                    &error_abort);
1297b994c5bcSKevin Wolf     overlay_s = overlay->opaque;
1298b994c5bcSKevin Wolf 
1299b994c5bcSKevin Wolf     do_drain_begin(BDRV_DRAIN, base);
1300b994c5bcSKevin Wolf     g_assert_cmpint(base->quiesce_counter, ==, 1);
1301b994c5bcSKevin Wolf     g_assert_cmpint(base_s->drain_count, ==, 1);
1302b994c5bcSKevin Wolf     g_assert_cmpint(base->in_flight, ==, 0);
1303b994c5bcSKevin Wolf 
1304b994c5bcSKevin Wolf     bdrv_append(overlay, base, &error_abort);
1305b994c5bcSKevin Wolf     g_assert_cmpint(base->in_flight, ==, 0);
1306b994c5bcSKevin Wolf     g_assert_cmpint(overlay->in_flight, ==, 0);
1307b994c5bcSKevin Wolf 
1308b994c5bcSKevin Wolf     g_assert_cmpint(base->quiesce_counter, ==, 1);
1309b994c5bcSKevin Wolf     g_assert_cmpint(base_s->drain_count, ==, 1);
1310b994c5bcSKevin Wolf     g_assert_cmpint(overlay->quiesce_counter, ==, 1);
1311b994c5bcSKevin Wolf     g_assert_cmpint(overlay_s->drain_count, ==, 1);
1312b994c5bcSKevin Wolf 
1313b994c5bcSKevin Wolf     do_drain_end(BDRV_DRAIN, base);
1314b994c5bcSKevin Wolf 
1315b994c5bcSKevin Wolf     g_assert_cmpint(base->quiesce_counter, ==, 0);
1316b994c5bcSKevin Wolf     g_assert_cmpint(base_s->drain_count, ==, 0);
1317b994c5bcSKevin Wolf     g_assert_cmpint(overlay->quiesce_counter, ==, 0);
1318b994c5bcSKevin Wolf     g_assert_cmpint(overlay_s->drain_count, ==, 0);
1319b994c5bcSKevin Wolf 
1320ae9d4417SVladimir Sementsov-Ogievskiy     bdrv_unref(overlay);
1321b994c5bcSKevin Wolf     bdrv_unref(base);
1322b994c5bcSKevin Wolf     blk_unref(blk);
1323b994c5bcSKevin Wolf }
1324b994c5bcSKevin Wolf 
1325247d2737SKevin Wolf static void test_set_aio_context(void)
1326247d2737SKevin Wolf {
1327247d2737SKevin Wolf     BlockDriverState *bs;
1328247d2737SKevin Wolf     IOThread *a = iothread_new();
1329247d2737SKevin Wolf     IOThread *b = iothread_new();
1330247d2737SKevin Wolf     AioContext *ctx_a = iothread_get_aio_context(a);
1331247d2737SKevin Wolf     AioContext *ctx_b = iothread_get_aio_context(b);
1332247d2737SKevin Wolf 
1333247d2737SKevin Wolf     bs = bdrv_new_open_driver(&bdrv_test, "test-node", BDRV_O_RDWR,
1334247d2737SKevin Wolf                               &error_abort);
1335247d2737SKevin Wolf 
1336247d2737SKevin Wolf     bdrv_drained_begin(bs);
1337142e6907SEmanuele Giuseppe Esposito     bdrv_try_change_aio_context(bs, ctx_a, NULL, &error_abort);
1338247d2737SKevin Wolf 
1339247d2737SKevin Wolf     aio_context_acquire(ctx_a);
1340247d2737SKevin Wolf     bdrv_drained_end(bs);
1341247d2737SKevin Wolf 
1342247d2737SKevin Wolf     bdrv_drained_begin(bs);
1343142e6907SEmanuele Giuseppe Esposito     bdrv_try_change_aio_context(bs, ctx_b, NULL, &error_abort);
1344247d2737SKevin Wolf     aio_context_release(ctx_a);
1345247d2737SKevin Wolf     aio_context_acquire(ctx_b);
1346142e6907SEmanuele Giuseppe Esposito     bdrv_try_change_aio_context(bs, qemu_get_aio_context(), NULL, &error_abort);
1347247d2737SKevin Wolf     aio_context_release(ctx_b);
1348247d2737SKevin Wolf     bdrv_drained_end(bs);
1349247d2737SKevin Wolf 
1350247d2737SKevin Wolf     bdrv_unref(bs);
1351247d2737SKevin Wolf     iothread_join(a);
1352247d2737SKevin Wolf     iothread_join(b);
1353247d2737SKevin Wolf }
1354247d2737SKevin Wolf 
13558e442810SMax Reitz 
13568e442810SMax Reitz typedef struct TestDropBackingBlockJob {
13578e442810SMax Reitz     BlockJob common;
13588e442810SMax Reitz     bool should_complete;
13598e442810SMax Reitz     bool *did_complete;
13602afdc790SMax Reitz     BlockDriverState *detach_also;
13611b177bbeSVladimir Sementsov-Ogievskiy     BlockDriverState *bs;
13628e442810SMax Reitz } TestDropBackingBlockJob;
13638e442810SMax Reitz 
13648e442810SMax Reitz static int coroutine_fn test_drop_backing_job_run(Job *job, Error **errp)
13658e442810SMax Reitz {
13668e442810SMax Reitz     TestDropBackingBlockJob *s =
13678e442810SMax Reitz         container_of(job, TestDropBackingBlockJob, common.job);
13688e442810SMax Reitz 
13698e442810SMax Reitz     while (!s->should_complete) {
13708e442810SMax Reitz         job_sleep_ns(job, 0);
13718e442810SMax Reitz     }
13728e442810SMax Reitz 
13738e442810SMax Reitz     return 0;
13748e442810SMax Reitz }
13758e442810SMax Reitz 
13768e442810SMax Reitz static void test_drop_backing_job_commit(Job *job)
13778e442810SMax Reitz {
13788e442810SMax Reitz     TestDropBackingBlockJob *s =
13798e442810SMax Reitz         container_of(job, TestDropBackingBlockJob, common.job);
13808e442810SMax Reitz 
13811b177bbeSVladimir Sementsov-Ogievskiy     bdrv_set_backing_hd(s->bs, NULL, &error_abort);
13822afdc790SMax Reitz     bdrv_set_backing_hd(s->detach_also, NULL, &error_abort);
13838e442810SMax Reitz 
13848e442810SMax Reitz     *s->did_complete = true;
13858e442810SMax Reitz }
13868e442810SMax Reitz 
13878e442810SMax Reitz static const BlockJobDriver test_drop_backing_job_driver = {
13888e442810SMax Reitz     .job_driver = {
13898e442810SMax Reitz         .instance_size  = sizeof(TestDropBackingBlockJob),
13908e442810SMax Reitz         .free           = block_job_free,
13918e442810SMax Reitz         .user_resume    = block_job_user_resume,
13928e442810SMax Reitz         .run            = test_drop_backing_job_run,
13938e442810SMax Reitz         .commit         = test_drop_backing_job_commit,
13948e442810SMax Reitz     }
13958e442810SMax Reitz };
13968e442810SMax Reitz 
13978e442810SMax Reitz /**
13988e442810SMax Reitz  * Creates a child node with three parent nodes on it, and then runs a
13998e442810SMax Reitz  * block job on the final one, parent-node-2.
14008e442810SMax Reitz  *
14018e442810SMax Reitz  * The job is then asked to complete before a section where the child
14028e442810SMax Reitz  * is drained.
14038e442810SMax Reitz  *
14048e442810SMax Reitz  * Ending this section will undrain the child's parents, first
14058e442810SMax Reitz  * parent-node-2, then parent-node-1, then parent-node-0 -- the parent
14068e442810SMax Reitz  * list is in reverse order of how they were added.  Ending the drain
14078e442810SMax Reitz  * on parent-node-2 will resume the job, thus completing it and
14088e442810SMax Reitz  * scheduling job_exit().
14098e442810SMax Reitz  *
14108e442810SMax Reitz  * Ending the drain on parent-node-1 will poll the AioContext, which
14118e442810SMax Reitz  * lets job_exit() and thus test_drop_backing_job_commit() run.  That
14122afdc790SMax Reitz  * function first removes the child as parent-node-2's backing file.
14138e442810SMax Reitz  *
14148e442810SMax Reitz  * In old (and buggy) implementations, there are two problems with
14158e442810SMax Reitz  * that:
14168e442810SMax Reitz  * (A) bdrv_drain_invoke() polls for every node that leaves the
14178e442810SMax Reitz  *     drained section.  This means that job_exit() is scheduled
14188e442810SMax Reitz  *     before the child has left the drained section.  Its
14198e442810SMax Reitz  *     quiesce_counter is therefore still 1 when it is removed from
14208e442810SMax Reitz  *     parent-node-2.
14218e442810SMax Reitz  *
14228e442810SMax Reitz  * (B) bdrv_replace_child_noperm() calls drained_end() on the old
14238e442810SMax Reitz  *     child's parents as many times as the child is quiesced.  This
14248e442810SMax Reitz  *     means it will call drained_end() on parent-node-2 once.
14258e442810SMax Reitz  *     Because parent-node-2 is no longer quiesced at this point, this
14268e442810SMax Reitz  *     will fail.
14278e442810SMax Reitz  *
14288e442810SMax Reitz  * bdrv_replace_child_noperm() therefore must call drained_end() on
14298e442810SMax Reitz  * the parent only if it really is still drained because the child is
14308e442810SMax Reitz  * drained.
14312afdc790SMax Reitz  *
14322afdc790SMax Reitz  * If removing child from parent-node-2 was successful (as it should
14332afdc790SMax Reitz  * be), test_drop_backing_job_commit() will then also remove the child
14342afdc790SMax Reitz  * from parent-node-0.
14352afdc790SMax Reitz  *
14362afdc790SMax Reitz  * With an old version of our drain infrastructure ((A) above), that
14372afdc790SMax Reitz  * resulted in the following flow:
14382afdc790SMax Reitz  *
14392afdc790SMax Reitz  * 1. child attempts to leave its drained section.  The call recurses
14402afdc790SMax Reitz  *    to its parents.
14412afdc790SMax Reitz  *
14422afdc790SMax Reitz  * 2. parent-node-2 leaves the drained section.  Polling in
14432afdc790SMax Reitz  *    bdrv_drain_invoke() will schedule job_exit().
14442afdc790SMax Reitz  *
14452afdc790SMax Reitz  * 3. parent-node-1 leaves the drained section.  Polling in
14462afdc790SMax Reitz  *    bdrv_drain_invoke() will run job_exit(), thus disconnecting
14472afdc790SMax Reitz  *    parent-node-0 from the child node.
14482afdc790SMax Reitz  *
14492afdc790SMax Reitz  * 4. bdrv_parent_drained_end() uses a QLIST_FOREACH_SAFE() loop to
14502afdc790SMax Reitz  *    iterate over the parents.  Thus, it now accesses the BdrvChild
14512afdc790SMax Reitz  *    object that used to connect parent-node-0 and the child node.
14522afdc790SMax Reitz  *    However, that object no longer exists, so it accesses a dangling
14532afdc790SMax Reitz  *    pointer.
14542afdc790SMax Reitz  *
14552afdc790SMax Reitz  * The solution is to only poll once when running a bdrv_drained_end()
14562afdc790SMax Reitz  * operation, specifically at the end when all drained_end()
14572afdc790SMax Reitz  * operations for all involved nodes have been scheduled.
14582afdc790SMax Reitz  * Note that this also solves (A) above, thus hiding (B).
14598e442810SMax Reitz  */
14608e442810SMax Reitz static void test_blockjob_commit_by_drained_end(void)
14618e442810SMax Reitz {
14628e442810SMax Reitz     BlockDriverState *bs_child, *bs_parents[3];
14638e442810SMax Reitz     TestDropBackingBlockJob *job;
14648e442810SMax Reitz     bool job_has_completed = false;
14658e442810SMax Reitz     int i;
14668e442810SMax Reitz 
14678e442810SMax Reitz     bs_child = bdrv_new_open_driver(&bdrv_test, "child-node", BDRV_O_RDWR,
14688e442810SMax Reitz                                     &error_abort);
14698e442810SMax Reitz 
14708e442810SMax Reitz     for (i = 0; i < 3; i++) {
14718e442810SMax Reitz         char name[32];
14728e442810SMax Reitz         snprintf(name, sizeof(name), "parent-node-%i", i);
14738e442810SMax Reitz         bs_parents[i] = bdrv_new_open_driver(&bdrv_test, name, BDRV_O_RDWR,
14748e442810SMax Reitz                                              &error_abort);
14758e442810SMax Reitz         bdrv_set_backing_hd(bs_parents[i], bs_child, &error_abort);
14768e442810SMax Reitz     }
14778e442810SMax Reitz 
14788e442810SMax Reitz     job = block_job_create("job", &test_drop_backing_job_driver, NULL,
14798e442810SMax Reitz                            bs_parents[2], 0, BLK_PERM_ALL, 0, 0, NULL, NULL,
14808e442810SMax Reitz                            &error_abort);
14811b177bbeSVladimir Sementsov-Ogievskiy     job->bs = bs_parents[2];
14828e442810SMax Reitz 
14832afdc790SMax Reitz     job->detach_also = bs_parents[0];
14848e442810SMax Reitz     job->did_complete = &job_has_completed;
14858e442810SMax Reitz 
14868e442810SMax Reitz     job_start(&job->common.job);
14878e442810SMax Reitz 
14888e442810SMax Reitz     job->should_complete = true;
14898e442810SMax Reitz     bdrv_drained_begin(bs_child);
14908e442810SMax Reitz     g_assert(!job_has_completed);
14918e442810SMax Reitz     bdrv_drained_end(bs_child);
14925e8ac217SKevin Wolf     aio_poll(qemu_get_aio_context(), false);
14938e442810SMax Reitz     g_assert(job_has_completed);
14948e442810SMax Reitz 
14958e442810SMax Reitz     bdrv_unref(bs_parents[0]);
14968e442810SMax Reitz     bdrv_unref(bs_parents[1]);
14978e442810SMax Reitz     bdrv_unref(bs_parents[2]);
14988e442810SMax Reitz     bdrv_unref(bs_child);
14998e442810SMax Reitz }
15008e442810SMax Reitz 
15019746b35cSMax Reitz 
15029746b35cSMax Reitz typedef struct TestSimpleBlockJob {
15039746b35cSMax Reitz     BlockJob common;
15049746b35cSMax Reitz     bool should_complete;
15059746b35cSMax Reitz     bool *did_complete;
15069746b35cSMax Reitz } TestSimpleBlockJob;
15079746b35cSMax Reitz 
15089746b35cSMax Reitz static int coroutine_fn test_simple_job_run(Job *job, Error **errp)
15099746b35cSMax Reitz {
15109746b35cSMax Reitz     TestSimpleBlockJob *s = container_of(job, TestSimpleBlockJob, common.job);
15119746b35cSMax Reitz 
15129746b35cSMax Reitz     while (!s->should_complete) {
15139746b35cSMax Reitz         job_sleep_ns(job, 0);
15149746b35cSMax Reitz     }
15159746b35cSMax Reitz 
15169746b35cSMax Reitz     return 0;
15179746b35cSMax Reitz }
15189746b35cSMax Reitz 
15199746b35cSMax Reitz static void test_simple_job_clean(Job *job)
15209746b35cSMax Reitz {
15219746b35cSMax Reitz     TestSimpleBlockJob *s = container_of(job, TestSimpleBlockJob, common.job);
15229746b35cSMax Reitz     *s->did_complete = true;
15239746b35cSMax Reitz }
15249746b35cSMax Reitz 
15259746b35cSMax Reitz static const BlockJobDriver test_simple_job_driver = {
15269746b35cSMax Reitz     .job_driver = {
15279746b35cSMax Reitz         .instance_size  = sizeof(TestSimpleBlockJob),
15289746b35cSMax Reitz         .free           = block_job_free,
15299746b35cSMax Reitz         .user_resume    = block_job_user_resume,
15309746b35cSMax Reitz         .run            = test_simple_job_run,
15319746b35cSMax Reitz         .clean          = test_simple_job_clean,
15329746b35cSMax Reitz     },
15339746b35cSMax Reitz };
15349746b35cSMax Reitz 
15359746b35cSMax Reitz static int drop_intermediate_poll_update_filename(BdrvChild *child,
15369746b35cSMax Reitz                                                   BlockDriverState *new_base,
15379746b35cSMax Reitz                                                   const char *filename,
15389746b35cSMax Reitz                                                   Error **errp)
15399746b35cSMax Reitz {
15409746b35cSMax Reitz     /*
15419746b35cSMax Reitz      * We are free to poll here, which may change the block graph, if
15429746b35cSMax Reitz      * it is not drained.
15439746b35cSMax Reitz      */
15449746b35cSMax Reitz 
15459746b35cSMax Reitz     /* If the job is not drained: Complete it, schedule job_exit() */
15469746b35cSMax Reitz     aio_poll(qemu_get_current_aio_context(), false);
15479746b35cSMax Reitz     /* If the job is not drained: Run job_exit(), finish the job */
15489746b35cSMax Reitz     aio_poll(qemu_get_current_aio_context(), false);
15499746b35cSMax Reitz 
15509746b35cSMax Reitz     return 0;
15519746b35cSMax Reitz }
15529746b35cSMax Reitz 
15539746b35cSMax Reitz /**
15549746b35cSMax Reitz  * Test a poll in the midst of bdrv_drop_intermediate().
15559746b35cSMax Reitz  *
1556bd86fb99SMax Reitz  * bdrv_drop_intermediate() calls BdrvChildClass.update_filename(),
15579746b35cSMax Reitz  * which can yield or poll.  This may lead to graph changes, unless
15589746b35cSMax Reitz  * the whole subtree in question is drained.
15599746b35cSMax Reitz  *
15609746b35cSMax Reitz  * We test this on the following graph:
15619746b35cSMax Reitz  *
15629746b35cSMax Reitz  *                    Job
15639746b35cSMax Reitz  *
15649746b35cSMax Reitz  *                     |
15659746b35cSMax Reitz  *                  job-node
15669746b35cSMax Reitz  *                     |
15679746b35cSMax Reitz  *                     v
15689746b35cSMax Reitz  *
15699746b35cSMax Reitz  *                  job-node
15709746b35cSMax Reitz  *
15719746b35cSMax Reitz  *                     |
15729746b35cSMax Reitz  *                  backing
15739746b35cSMax Reitz  *                     |
15749746b35cSMax Reitz  *                     v
15759746b35cSMax Reitz  *
15769746b35cSMax Reitz  * node-2 --chain--> node-1 --chain--> node-0
15779746b35cSMax Reitz  *
15789746b35cSMax Reitz  * We drop node-1 with bdrv_drop_intermediate(top=node-1, base=node-0).
15799746b35cSMax Reitz  *
15809746b35cSMax Reitz  * This first updates node-2's backing filename by invoking
15819746b35cSMax Reitz  * drop_intermediate_poll_update_filename(), which polls twice.  This
15829746b35cSMax Reitz  * causes the job to finish, which in turns causes the job-node to be
15839746b35cSMax Reitz  * deleted.
15849746b35cSMax Reitz  *
15859746b35cSMax Reitz  * bdrv_drop_intermediate() uses a QLIST_FOREACH_SAFE() loop, so it
15869746b35cSMax Reitz  * already has a pointer to the BdrvChild edge between job-node and
15879746b35cSMax Reitz  * node-1.  When it tries to handle that edge, we probably get a
15889746b35cSMax Reitz  * segmentation fault because the object no longer exists.
15899746b35cSMax Reitz  *
15909746b35cSMax Reitz  *
15919746b35cSMax Reitz  * The solution is for bdrv_drop_intermediate() to drain top's
15929746b35cSMax Reitz  * subtree.  This prevents graph changes from happening just because
1593bd86fb99SMax Reitz  * BdrvChildClass.update_filename() yields or polls.  Thus, the block
15949746b35cSMax Reitz  * job is paused during that drained section and must finish before or
15959746b35cSMax Reitz  * after.
15969746b35cSMax Reitz  *
15979746b35cSMax Reitz  * (In addition, bdrv_replace_child() must keep the job paused.)
15989746b35cSMax Reitz  */
15999746b35cSMax Reitz static void test_drop_intermediate_poll(void)
16009746b35cSMax Reitz {
1601bd86fb99SMax Reitz     static BdrvChildClass chain_child_class;
16029746b35cSMax Reitz     BlockDriverState *chain[3];
16039746b35cSMax Reitz     TestSimpleBlockJob *job;
16049746b35cSMax Reitz     BlockDriverState *job_node;
16059746b35cSMax Reitz     bool job_has_completed = false;
16069746b35cSMax Reitz     int i;
16079746b35cSMax Reitz     int ret;
16089746b35cSMax Reitz 
160925191e5fSMax Reitz     chain_child_class = child_of_bds;
1610bd86fb99SMax Reitz     chain_child_class.update_filename = drop_intermediate_poll_update_filename;
16119746b35cSMax Reitz 
16129746b35cSMax Reitz     for (i = 0; i < 3; i++) {
16139746b35cSMax Reitz         char name[32];
16149746b35cSMax Reitz         snprintf(name, 32, "node-%i", i);
16159746b35cSMax Reitz 
16169746b35cSMax Reitz         chain[i] = bdrv_new_open_driver(&bdrv_test, name, 0, &error_abort);
16179746b35cSMax Reitz     }
16189746b35cSMax Reitz 
16199746b35cSMax Reitz     job_node = bdrv_new_open_driver(&bdrv_test, "job-node", BDRV_O_RDWR,
16209746b35cSMax Reitz                                     &error_abort);
16219746b35cSMax Reitz     bdrv_set_backing_hd(job_node, chain[1], &error_abort);
16229746b35cSMax Reitz 
16239746b35cSMax Reitz     /*
16249746b35cSMax Reitz      * Establish the chain last, so the chain links are the first
16259746b35cSMax Reitz      * elements in the BDS.parents lists
16269746b35cSMax Reitz      */
16279746b35cSMax Reitz     for (i = 0; i < 3; i++) {
16289746b35cSMax Reitz         if (i) {
16299746b35cSMax Reitz             /* Takes the reference to chain[i - 1] */
16305bb04747SVladimir Sementsov-Ogievskiy             bdrv_attach_child(chain[i], chain[i - 1], "chain",
16315bb04747SVladimir Sementsov-Ogievskiy                               &chain_child_class, BDRV_CHILD_COW, &error_abort);
16329746b35cSMax Reitz         }
16339746b35cSMax Reitz     }
16349746b35cSMax Reitz 
16359746b35cSMax Reitz     job = block_job_create("job", &test_simple_job_driver, NULL, job_node,
16369746b35cSMax Reitz                            0, BLK_PERM_ALL, 0, 0, NULL, NULL, &error_abort);
16379746b35cSMax Reitz 
16389746b35cSMax Reitz     /* The job has a reference now */
16399746b35cSMax Reitz     bdrv_unref(job_node);
16409746b35cSMax Reitz 
16419746b35cSMax Reitz     job->did_complete = &job_has_completed;
16429746b35cSMax Reitz 
16439746b35cSMax Reitz     job_start(&job->common.job);
16449746b35cSMax Reitz     job->should_complete = true;
16459746b35cSMax Reitz 
16469746b35cSMax Reitz     g_assert(!job_has_completed);
16479746b35cSMax Reitz     ret = bdrv_drop_intermediate(chain[1], chain[0], NULL);
16485e8ac217SKevin Wolf     aio_poll(qemu_get_aio_context(), false);
16499746b35cSMax Reitz     g_assert(ret == 0);
16509746b35cSMax Reitz     g_assert(job_has_completed);
16519746b35cSMax Reitz 
16529746b35cSMax Reitz     bdrv_unref(chain[2]);
16539746b35cSMax Reitz }
16549746b35cSMax Reitz 
16550513f984SMax Reitz 
16560513f984SMax Reitz typedef struct BDRVReplaceTestState {
16570513f984SMax Reitz     bool was_drained;
16580513f984SMax Reitz     bool was_undrained;
16590513f984SMax Reitz     bool has_read;
16600513f984SMax Reitz 
16610513f984SMax Reitz     int drain_count;
16620513f984SMax Reitz 
16630513f984SMax Reitz     bool yield_before_read;
16640513f984SMax Reitz     Coroutine *io_co;
16650513f984SMax Reitz     Coroutine *drain_co;
16660513f984SMax Reitz } BDRVReplaceTestState;
16670513f984SMax Reitz 
16680513f984SMax Reitz static void bdrv_replace_test_close(BlockDriverState *bs)
16690513f984SMax Reitz {
16700513f984SMax Reitz }
16710513f984SMax Reitz 
16720513f984SMax Reitz /**
16730513f984SMax Reitz  * If @bs has a backing file:
16740513f984SMax Reitz  *   Yield if .yield_before_read is true (and wait for drain_begin to
16750513f984SMax Reitz  *   wake us up).
16760513f984SMax Reitz  *   Forward the read to bs->backing.  Set .has_read to true.
16770513f984SMax Reitz  *   If drain_begin has woken us, wake it in turn.
16780513f984SMax Reitz  *
16790513f984SMax Reitz  * Otherwise:
16800513f984SMax Reitz  *   Set .has_read to true and return success.
16810513f984SMax Reitz  */
16820513f984SMax Reitz static int coroutine_fn bdrv_replace_test_co_preadv(BlockDriverState *bs,
1683f7ef38ddSVladimir Sementsov-Ogievskiy                                                     int64_t offset,
1684f7ef38ddSVladimir Sementsov-Ogievskiy                                                     int64_t bytes,
16850513f984SMax Reitz                                                     QEMUIOVector *qiov,
1686f7ef38ddSVladimir Sementsov-Ogievskiy                                                     BdrvRequestFlags flags)
16870513f984SMax Reitz {
16880513f984SMax Reitz     BDRVReplaceTestState *s = bs->opaque;
16890513f984SMax Reitz 
16900513f984SMax Reitz     if (bs->backing) {
16910513f984SMax Reitz         int ret;
16920513f984SMax Reitz 
16930513f984SMax Reitz         g_assert(!s->drain_count);
16940513f984SMax Reitz 
16950513f984SMax Reitz         s->io_co = qemu_coroutine_self();
16960513f984SMax Reitz         if (s->yield_before_read) {
16970513f984SMax Reitz             s->yield_before_read = false;
16980513f984SMax Reitz             qemu_coroutine_yield();
16990513f984SMax Reitz         }
17000513f984SMax Reitz         s->io_co = NULL;
17010513f984SMax Reitz 
1702fae2681aSVladimir Sementsov-Ogievskiy         ret = bdrv_co_preadv(bs->backing, offset, bytes, qiov, 0);
17030513f984SMax Reitz         s->has_read = true;
17040513f984SMax Reitz 
17050513f984SMax Reitz         /* Wake up drain_co if it runs */
17060513f984SMax Reitz         if (s->drain_co) {
17070513f984SMax Reitz             aio_co_wake(s->drain_co);
17080513f984SMax Reitz         }
17090513f984SMax Reitz 
17100513f984SMax Reitz         return ret;
17110513f984SMax Reitz     }
17120513f984SMax Reitz 
17130513f984SMax Reitz     s->has_read = true;
17140513f984SMax Reitz     return 0;
17150513f984SMax Reitz }
17160513f984SMax Reitz 
17177bce1c29SKevin Wolf static void coroutine_fn bdrv_replace_test_drain_co(void *opaque)
17187bce1c29SKevin Wolf {
17197bce1c29SKevin Wolf     BlockDriverState *bs = opaque;
17207bce1c29SKevin Wolf     BDRVReplaceTestState *s = bs->opaque;
17217bce1c29SKevin Wolf 
17227bce1c29SKevin Wolf     /* Keep waking io_co up until it is done */
17237bce1c29SKevin Wolf     while (s->io_co) {
17247bce1c29SKevin Wolf         aio_co_wake(s->io_co);
17257bce1c29SKevin Wolf         s->io_co = NULL;
17267bce1c29SKevin Wolf         qemu_coroutine_yield();
17277bce1c29SKevin Wolf     }
17287bce1c29SKevin Wolf     s->drain_co = NULL;
17297bce1c29SKevin Wolf     bdrv_dec_in_flight(bs);
17307bce1c29SKevin Wolf }
17317bce1c29SKevin Wolf 
17320513f984SMax Reitz /**
17330513f984SMax Reitz  * If .drain_count is 0, wake up .io_co if there is one; and set
17340513f984SMax Reitz  * .was_drained.
17350513f984SMax Reitz  * Increment .drain_count.
17360513f984SMax Reitz  */
17375e8ac217SKevin Wolf static void bdrv_replace_test_drain_begin(BlockDriverState *bs)
17380513f984SMax Reitz {
17390513f984SMax Reitz     BDRVReplaceTestState *s = bs->opaque;
17400513f984SMax Reitz 
17410513f984SMax Reitz     if (!s->drain_count) {
17427bce1c29SKevin Wolf         s->drain_co = qemu_coroutine_create(bdrv_replace_test_drain_co, bs);
17437bce1c29SKevin Wolf         bdrv_inc_in_flight(bs);
17447bce1c29SKevin Wolf         aio_co_enter(bdrv_get_aio_context(bs), s->drain_co);
17450513f984SMax Reitz         s->was_drained = true;
17460513f984SMax Reitz     }
17470513f984SMax Reitz     s->drain_count++;
17480513f984SMax Reitz }
17490513f984SMax Reitz 
17507bce1c29SKevin Wolf static void coroutine_fn bdrv_replace_test_read_entry(void *opaque)
17517bce1c29SKevin Wolf {
17527bce1c29SKevin Wolf     BlockDriverState *bs = opaque;
17537bce1c29SKevin Wolf     char data;
17547bce1c29SKevin Wolf     QEMUIOVector qiov = QEMU_IOVEC_INIT_BUF(qiov, &data, 1);
17557bce1c29SKevin Wolf     int ret;
17567bce1c29SKevin Wolf 
17577bce1c29SKevin Wolf     /* Queue a read request post-drain */
17587bce1c29SKevin Wolf     ret = bdrv_replace_test_co_preadv(bs, 0, 1, &qiov, 0);
17597bce1c29SKevin Wolf     g_assert(ret >= 0);
17607bce1c29SKevin Wolf     bdrv_dec_in_flight(bs);
17617bce1c29SKevin Wolf }
17627bce1c29SKevin Wolf 
17630513f984SMax Reitz /**
17640513f984SMax Reitz  * Reduce .drain_count, set .was_undrained once it reaches 0.
17650513f984SMax Reitz  * If .drain_count reaches 0 and the node has a backing file, issue a
17660513f984SMax Reitz  * read request.
17670513f984SMax Reitz  */
17685e8ac217SKevin Wolf static void bdrv_replace_test_drain_end(BlockDriverState *bs)
17690513f984SMax Reitz {
17700513f984SMax Reitz     BDRVReplaceTestState *s = bs->opaque;
17710513f984SMax Reitz 
17720513f984SMax Reitz     g_assert(s->drain_count > 0);
17730513f984SMax Reitz     if (!--s->drain_count) {
17740513f984SMax Reitz         s->was_undrained = true;
17750513f984SMax Reitz 
17760513f984SMax Reitz         if (bs->backing) {
17777bce1c29SKevin Wolf             Coroutine *co = qemu_coroutine_create(bdrv_replace_test_read_entry,
17787bce1c29SKevin Wolf                                                   bs);
17797bce1c29SKevin Wolf             bdrv_inc_in_flight(bs);
17807bce1c29SKevin Wolf             aio_co_enter(bdrv_get_aio_context(bs), co);
17810513f984SMax Reitz         }
17820513f984SMax Reitz     }
17830513f984SMax Reitz }
17840513f984SMax Reitz 
17850513f984SMax Reitz static BlockDriver bdrv_replace_test = {
17860513f984SMax Reitz     .format_name            = "replace_test",
17870513f984SMax Reitz     .instance_size          = sizeof(BDRVReplaceTestState),
17889ebfc111SVladimir Sementsov-Ogievskiy     .supports_backing       = true,
17890513f984SMax Reitz 
17900513f984SMax Reitz     .bdrv_close             = bdrv_replace_test_close,
17910513f984SMax Reitz     .bdrv_co_preadv         = bdrv_replace_test_co_preadv,
17920513f984SMax Reitz 
17935e8ac217SKevin Wolf     .bdrv_drain_begin       = bdrv_replace_test_drain_begin,
17945e8ac217SKevin Wolf     .bdrv_drain_end         = bdrv_replace_test_drain_end,
17950513f984SMax Reitz 
179669dca43dSMax Reitz     .bdrv_child_perm        = bdrv_default_perms,
17970513f984SMax Reitz };
17980513f984SMax Reitz 
17990513f984SMax Reitz static void coroutine_fn test_replace_child_mid_drain_read_co(void *opaque)
18000513f984SMax Reitz {
18010513f984SMax Reitz     int ret;
18020513f984SMax Reitz     char data;
18030513f984SMax Reitz 
18040513f984SMax Reitz     ret = blk_co_pread(opaque, 0, 1, &data, 0);
18050513f984SMax Reitz     g_assert(ret >= 0);
18060513f984SMax Reitz }
18070513f984SMax Reitz 
18080513f984SMax Reitz /**
18090513f984SMax Reitz  * We test two things:
18100513f984SMax Reitz  * (1) bdrv_replace_child_noperm() must not undrain the parent if both
18110513f984SMax Reitz  *     children are drained.
18120513f984SMax Reitz  * (2) bdrv_replace_child_noperm() must never flush I/O requests to a
18130513f984SMax Reitz  *     drained child.  If the old child is drained, it must flush I/O
18140513f984SMax Reitz  *     requests after the new one has been attached.  If the new child
18150513f984SMax Reitz  *     is drained, it must flush I/O requests before the old one is
18160513f984SMax Reitz  *     detached.
18170513f984SMax Reitz  *
18180513f984SMax Reitz  * To do so, we create one parent node and two child nodes; then
18190513f984SMax Reitz  * attach one of the children (old_child_bs) to the parent, then
18200513f984SMax Reitz  * drain both old_child_bs and new_child_bs according to
18210513f984SMax Reitz  * old_drain_count and new_drain_count, respectively, and finally
18220513f984SMax Reitz  * we invoke bdrv_replace_node() to replace old_child_bs by
18230513f984SMax Reitz  * new_child_bs.
18240513f984SMax Reitz  *
18250513f984SMax Reitz  * The test block driver we use here (bdrv_replace_test) has a read
18260513f984SMax Reitz  * function that:
18270513f984SMax Reitz  * - For the parent node, can optionally yield, and then forwards the
18280513f984SMax Reitz  *   read to bdrv_preadv(),
18290513f984SMax Reitz  * - For the child node, just returns immediately.
18300513f984SMax Reitz  *
18310513f984SMax Reitz  * If the read yields, the drain_begin function will wake it up.
18320513f984SMax Reitz  *
18330513f984SMax Reitz  * The drain_end function issues a read on the parent once it is fully
18340513f984SMax Reitz  * undrained (which simulates requests starting to come in again).
18350513f984SMax Reitz  */
18360513f984SMax Reitz static void do_test_replace_child_mid_drain(int old_drain_count,
18370513f984SMax Reitz                                             int new_drain_count)
18380513f984SMax Reitz {
18390513f984SMax Reitz     BlockBackend *parent_blk;
18400513f984SMax Reitz     BlockDriverState *parent_bs;
18410513f984SMax Reitz     BlockDriverState *old_child_bs, *new_child_bs;
18420513f984SMax Reitz     BDRVReplaceTestState *parent_s;
18430513f984SMax Reitz     BDRVReplaceTestState *old_child_s, *new_child_s;
18440513f984SMax Reitz     Coroutine *io_co;
18450513f984SMax Reitz     int i;
18460513f984SMax Reitz 
18470513f984SMax Reitz     parent_bs = bdrv_new_open_driver(&bdrv_replace_test, "parent", 0,
18480513f984SMax Reitz                                      &error_abort);
18490513f984SMax Reitz     parent_s = parent_bs->opaque;
18500513f984SMax Reitz 
18510513f984SMax Reitz     parent_blk = blk_new(qemu_get_aio_context(),
18520513f984SMax Reitz                          BLK_PERM_CONSISTENT_READ, BLK_PERM_ALL);
18530513f984SMax Reitz     blk_insert_bs(parent_blk, parent_bs, &error_abort);
18540513f984SMax Reitz 
18550513f984SMax Reitz     old_child_bs = bdrv_new_open_driver(&bdrv_replace_test, "old-child", 0,
18560513f984SMax Reitz                                         &error_abort);
18570513f984SMax Reitz     new_child_bs = bdrv_new_open_driver(&bdrv_replace_test, "new-child", 0,
18580513f984SMax Reitz                                         &error_abort);
18590513f984SMax Reitz     old_child_s = old_child_bs->opaque;
18600513f984SMax Reitz     new_child_s = new_child_bs->opaque;
18610513f984SMax Reitz 
18620513f984SMax Reitz     /* So that we can read something */
18630513f984SMax Reitz     parent_bs->total_sectors = 1;
18640513f984SMax Reitz     old_child_bs->total_sectors = 1;
18650513f984SMax Reitz     new_child_bs->total_sectors = 1;
18660513f984SMax Reitz 
18670513f984SMax Reitz     bdrv_ref(old_child_bs);
18685bb04747SVladimir Sementsov-Ogievskiy     bdrv_attach_child(parent_bs, old_child_bs, "child", &child_of_bds,
18695bb04747SVladimir Sementsov-Ogievskiy                       BDRV_CHILD_COW, &error_abort);
18700513f984SMax Reitz 
18710513f984SMax Reitz     for (i = 0; i < old_drain_count; i++) {
18720513f984SMax Reitz         bdrv_drained_begin(old_child_bs);
18730513f984SMax Reitz     }
18740513f984SMax Reitz     for (i = 0; i < new_drain_count; i++) {
18750513f984SMax Reitz         bdrv_drained_begin(new_child_bs);
18760513f984SMax Reitz     }
18770513f984SMax Reitz 
18780513f984SMax Reitz     if (!old_drain_count) {
18790513f984SMax Reitz         /*
18800513f984SMax Reitz          * Start a read operation that will yield, so it will not
18810513f984SMax Reitz          * complete before the node is drained.
18820513f984SMax Reitz          */
18830513f984SMax Reitz         parent_s->yield_before_read = true;
18840513f984SMax Reitz         io_co = qemu_coroutine_create(test_replace_child_mid_drain_read_co,
18850513f984SMax Reitz                                       parent_blk);
18860513f984SMax Reitz         qemu_coroutine_enter(io_co);
18870513f984SMax Reitz     }
18880513f984SMax Reitz 
18890513f984SMax Reitz     /* If we have started a read operation, it should have yielded */
18900513f984SMax Reitz     g_assert(!parent_s->has_read);
18910513f984SMax Reitz 
18920513f984SMax Reitz     /* Reset drained status so we can see what bdrv_replace_node() does */
18930513f984SMax Reitz     parent_s->was_drained = false;
18940513f984SMax Reitz     parent_s->was_undrained = false;
18950513f984SMax Reitz 
18960513f984SMax Reitz     g_assert(parent_bs->quiesce_counter == old_drain_count);
18970513f984SMax Reitz     bdrv_replace_node(old_child_bs, new_child_bs, &error_abort);
18980513f984SMax Reitz     g_assert(parent_bs->quiesce_counter == new_drain_count);
18990513f984SMax Reitz 
19000513f984SMax Reitz     if (!old_drain_count && !new_drain_count) {
19010513f984SMax Reitz         /*
19020513f984SMax Reitz          * From undrained to undrained drains and undrains the parent,
19030513f984SMax Reitz          * because bdrv_replace_node() contains a drained section for
19040513f984SMax Reitz          * @old_child_bs.
19050513f984SMax Reitz          */
19060513f984SMax Reitz         g_assert(parent_s->was_drained && parent_s->was_undrained);
19070513f984SMax Reitz     } else if (!old_drain_count && new_drain_count) {
19080513f984SMax Reitz         /*
19090513f984SMax Reitz          * From undrained to drained should drain the parent and keep
19100513f984SMax Reitz          * it that way.
19110513f984SMax Reitz          */
19120513f984SMax Reitz         g_assert(parent_s->was_drained && !parent_s->was_undrained);
19130513f984SMax Reitz     } else if (old_drain_count && !new_drain_count) {
19140513f984SMax Reitz         /*
19150513f984SMax Reitz          * From drained to undrained should undrain the parent and
19160513f984SMax Reitz          * keep it that way.
19170513f984SMax Reitz          */
19180513f984SMax Reitz         g_assert(!parent_s->was_drained && parent_s->was_undrained);
19190513f984SMax Reitz     } else /* if (old_drain_count && new_drain_count) */ {
19200513f984SMax Reitz         /*
19210513f984SMax Reitz          * From drained to drained must not undrain the parent at any
19220513f984SMax Reitz          * point
19230513f984SMax Reitz          */
19240513f984SMax Reitz         g_assert(!parent_s->was_drained && !parent_s->was_undrained);
19250513f984SMax Reitz     }
19260513f984SMax Reitz 
19270513f984SMax Reitz     if (!old_drain_count || !new_drain_count) {
19280513f984SMax Reitz         /*
19290513f984SMax Reitz          * If !old_drain_count, we have started a read request before
19300513f984SMax Reitz          * bdrv_replace_node().  If !new_drain_count, the parent must
19310513f984SMax Reitz          * have been undrained at some point, and
19320513f984SMax Reitz          * bdrv_replace_test_co_drain_end() starts a read request
19330513f984SMax Reitz          * then.
19340513f984SMax Reitz          */
19350513f984SMax Reitz         g_assert(parent_s->has_read);
19360513f984SMax Reitz     } else {
19370513f984SMax Reitz         /*
19380513f984SMax Reitz          * If the parent was never undrained, there is no way to start
19390513f984SMax Reitz          * a read request.
19400513f984SMax Reitz          */
19410513f984SMax Reitz         g_assert(!parent_s->has_read);
19420513f984SMax Reitz     }
19430513f984SMax Reitz 
19440513f984SMax Reitz     /* A drained child must have not received any request */
19450513f984SMax Reitz     g_assert(!(old_drain_count && old_child_s->has_read));
19460513f984SMax Reitz     g_assert(!(new_drain_count && new_child_s->has_read));
19470513f984SMax Reitz 
19480513f984SMax Reitz     for (i = 0; i < new_drain_count; i++) {
19490513f984SMax Reitz         bdrv_drained_end(new_child_bs);
19500513f984SMax Reitz     }
19510513f984SMax Reitz     for (i = 0; i < old_drain_count; i++) {
19520513f984SMax Reitz         bdrv_drained_end(old_child_bs);
19530513f984SMax Reitz     }
19540513f984SMax Reitz 
19550513f984SMax Reitz     /*
19560513f984SMax Reitz      * By now, bdrv_replace_test_co_drain_end() must have been called
19570513f984SMax Reitz      * at some point while the new child was attached to the parent.
19580513f984SMax Reitz      */
19590513f984SMax Reitz     g_assert(parent_s->has_read);
19600513f984SMax Reitz     g_assert(new_child_s->has_read);
19610513f984SMax Reitz 
19620513f984SMax Reitz     blk_unref(parent_blk);
19630513f984SMax Reitz     bdrv_unref(parent_bs);
19640513f984SMax Reitz     bdrv_unref(old_child_bs);
19650513f984SMax Reitz     bdrv_unref(new_child_bs);
19660513f984SMax Reitz }
19670513f984SMax Reitz 
19680513f984SMax Reitz static void test_replace_child_mid_drain(void)
19690513f984SMax Reitz {
19700513f984SMax Reitz     int old_drain_count, new_drain_count;
19710513f984SMax Reitz 
19720513f984SMax Reitz     for (old_drain_count = 0; old_drain_count < 2; old_drain_count++) {
19730513f984SMax Reitz         for (new_drain_count = 0; new_drain_count < 2; new_drain_count++) {
19740513f984SMax Reitz             do_test_replace_child_mid_drain(old_drain_count, new_drain_count);
19750513f984SMax Reitz         }
19760513f984SMax Reitz     }
19770513f984SMax Reitz }
19780513f984SMax Reitz 
1979881cfd17SKevin Wolf int main(int argc, char **argv)
1980881cfd17SKevin Wolf {
1981bb675689SKevin Wolf     int ret;
1982bb675689SKevin Wolf 
1983881cfd17SKevin Wolf     bdrv_init();
1984881cfd17SKevin Wolf     qemu_init_main_loop(&error_abort);
1985881cfd17SKevin Wolf 
1986881cfd17SKevin Wolf     g_test_init(&argc, &argv, NULL);
1987bb675689SKevin Wolf     qemu_event_init(&done_event, false);
1988881cfd17SKevin Wolf 
1989881cfd17SKevin Wolf     g_test_add_func("/bdrv-drain/driver-cb/drain_all", test_drv_cb_drain_all);
199086e1c840SKevin Wolf     g_test_add_func("/bdrv-drain/driver-cb/drain", test_drv_cb_drain);
1991881cfd17SKevin Wolf 
19926d0252f2SKevin Wolf     g_test_add_func("/bdrv-drain/driver-cb/co/drain_all",
19936d0252f2SKevin Wolf                     test_drv_cb_co_drain_all);
19940582eb10SKevin Wolf     g_test_add_func("/bdrv-drain/driver-cb/co/drain", test_drv_cb_co_drain);
19950582eb10SKevin Wolf 
199689a6ceabSKevin Wolf     g_test_add_func("/bdrv-drain/quiesce/drain_all", test_quiesce_drain_all);
199789a6ceabSKevin Wolf     g_test_add_func("/bdrv-drain/quiesce/drain", test_quiesce_drain);
199889a6ceabSKevin Wolf 
19996d0252f2SKevin Wolf     g_test_add_func("/bdrv-drain/quiesce/co/drain_all",
20006d0252f2SKevin Wolf                     test_quiesce_co_drain_all);
20010582eb10SKevin Wolf     g_test_add_func("/bdrv-drain/quiesce/co/drain", test_quiesce_co_drain);
20020582eb10SKevin Wolf 
20036c429a6aSKevin Wolf     g_test_add_func("/bdrv-drain/nested", test_nested);
200419f7a7e5SKevin Wolf 
200519f7a7e5SKevin Wolf     g_test_add_func("/bdrv-drain/graph-change/drain_all",
200619f7a7e5SKevin Wolf                     test_graph_change_drain_all);
20076c429a6aSKevin Wolf 
2008bb675689SKevin Wolf     g_test_add_func("/bdrv-drain/iothread/drain_all", test_iothread_drain_all);
2009bb675689SKevin Wolf     g_test_add_func("/bdrv-drain/iothread/drain", test_iothread_drain);
2010bb675689SKevin Wolf 
20117253220dSKevin Wolf     g_test_add_func("/bdrv-drain/blockjob/drain_all", test_blockjob_drain_all);
20127253220dSKevin Wolf     g_test_add_func("/bdrv-drain/blockjob/drain", test_blockjob_drain);
20137253220dSKevin Wolf 
2014d49725afSKevin Wolf     g_test_add_func("/bdrv-drain/blockjob/error/drain_all",
2015d49725afSKevin Wolf                     test_blockjob_error_drain_all);
2016d49725afSKevin Wolf     g_test_add_func("/bdrv-drain/blockjob/error/drain",
2017d49725afSKevin Wolf                     test_blockjob_error_drain);
2018d49725afSKevin Wolf 
2019f62c1729SKevin Wolf     g_test_add_func("/bdrv-drain/blockjob/iothread/drain_all",
2020f62c1729SKevin Wolf                     test_blockjob_iothread_drain_all);
2021f62c1729SKevin Wolf     g_test_add_func("/bdrv-drain/blockjob/iothread/drain",
2022f62c1729SKevin Wolf                     test_blockjob_iothread_drain);
2023f62c1729SKevin Wolf 
2024d49725afSKevin Wolf     g_test_add_func("/bdrv-drain/blockjob/iothread/error/drain_all",
2025d49725afSKevin Wolf                     test_blockjob_iothread_error_drain_all);
2026d49725afSKevin Wolf     g_test_add_func("/bdrv-drain/blockjob/iothread/error/drain",
2027d49725afSKevin Wolf                     test_blockjob_iothread_error_drain);
2028d49725afSKevin Wolf 
2029ebd31837SKevin Wolf     g_test_add_func("/bdrv-drain/deletion/drain", test_delete_by_drain);
203019f7a7e5SKevin Wolf     g_test_add_func("/bdrv-drain/detach/drain_all", test_detach_by_drain_all);
2031ebd31837SKevin Wolf     g_test_add_func("/bdrv-drain/detach/drain", test_detach_by_drain);
2032231281abSKevin Wolf     g_test_add_func("/bdrv-drain/detach/parent_cb", test_detach_by_parent_cb);
203357320ca9SKevin Wolf     g_test_add_func("/bdrv-drain/detach/driver_cb", test_detach_by_driver_cb);
20344c8158e3SMax Reitz 
2035b994c5bcSKevin Wolf     g_test_add_func("/bdrv-drain/attach/drain", test_append_to_drained);
2036b994c5bcSKevin Wolf 
2037247d2737SKevin Wolf     g_test_add_func("/bdrv-drain/set_aio_context", test_set_aio_context);
2038247d2737SKevin Wolf 
20398e442810SMax Reitz     g_test_add_func("/bdrv-drain/blockjob/commit_by_drained_end",
20408e442810SMax Reitz                     test_blockjob_commit_by_drained_end);
20418e442810SMax Reitz 
20429746b35cSMax Reitz     g_test_add_func("/bdrv-drain/bdrv_drop_intermediate/poll",
20439746b35cSMax Reitz                     test_drop_intermediate_poll);
20449746b35cSMax Reitz 
20450513f984SMax Reitz     g_test_add_func("/bdrv-drain/replace_child/mid-drain",
20460513f984SMax Reitz                     test_replace_child_mid_drain);
20470513f984SMax Reitz 
2048bb675689SKevin Wolf     ret = g_test_run();
2049bb675689SKevin Wolf     qemu_event_destroy(&done_event);
2050bb675689SKevin Wolf     return ret;
2051881cfd17SKevin Wolf }
2052