1881cfd17SKevin Wolf /*
2881cfd17SKevin Wolf * Block node draining tests
3881cfd17SKevin Wolf *
4881cfd17SKevin Wolf * Copyright (c) 2017 Kevin Wolf <kwolf@redhat.com>
5881cfd17SKevin Wolf *
6881cfd17SKevin Wolf * Permission is hereby granted, free of charge, to any person obtaining a copy
7881cfd17SKevin Wolf * of this software and associated documentation files (the "Software"), to deal
8881cfd17SKevin Wolf * in the Software without restriction, including without limitation the rights
9881cfd17SKevin Wolf * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
10881cfd17SKevin Wolf * copies of the Software, and to permit persons to whom the Software is
11881cfd17SKevin Wolf * furnished to do so, subject to the following conditions:
12881cfd17SKevin Wolf *
13881cfd17SKevin Wolf * The above copyright notice and this permission notice shall be included in
14881cfd17SKevin Wolf * all copies or substantial portions of the Software.
15881cfd17SKevin Wolf *
16881cfd17SKevin Wolf * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
17881cfd17SKevin Wolf * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
18881cfd17SKevin Wolf * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
19881cfd17SKevin Wolf * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
20881cfd17SKevin Wolf * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
21881cfd17SKevin Wolf * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
22881cfd17SKevin Wolf * THE SOFTWARE.
23881cfd17SKevin Wolf */
24881cfd17SKevin Wolf
25881cfd17SKevin Wolf #include "qemu/osdep.h"
26e2c1c34fSMarkus Armbruster #include "block/block_int.h"
277253220dSKevin Wolf #include "block/blockjob_int.h"
2832cad1ffSPhilippe Mathieu-Daudé #include "system/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
sleep_in_drain_begin(void * opaque)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
bdrv_test_drain_begin(BlockDriverState * bs)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
bdrv_test_drain_end(BlockDriverState * bs)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
bdrv_test_close(BlockDriverState * bs)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
co_reenter_bh(void * opaque)72bb675689SKevin Wolf static void co_reenter_bh(void *opaque)
73bb675689SKevin Wolf {
74bb675689SKevin Wolf aio_co_wake(opaque);
75bb675689SKevin Wolf }
76bb675689SKevin Wolf
bdrv_test_co_preadv(BlockDriverState * bs,int64_t offset,int64_t bytes,QEMUIOVector * qiov,BdrvRequestFlags flags)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
bdrv_test_co_change_backing_file(BlockDriverState * bs,const char * backing_file,const char * backing_fmt)99e2dd2737SKevin Wolf static int bdrv_test_co_change_backing_file(BlockDriverState *bs,
1009746b35cSMax Reitz const char *backing_file,
1019746b35cSMax Reitz const char *backing_fmt)
1029746b35cSMax Reitz {
1039746b35cSMax Reitz return 0;
1049746b35cSMax Reitz }
1059746b35cSMax Reitz
106881cfd17SKevin Wolf static BlockDriver bdrv_test = {
107881cfd17SKevin Wolf .format_name = "test",
108881cfd17SKevin Wolf .instance_size = sizeof(BDRVTestState),
10925f78d9eSVladimir Sementsov-Ogievskiy .supports_backing = true,
110881cfd17SKevin Wolf
111881cfd17SKevin Wolf .bdrv_close = bdrv_test_close,
112881cfd17SKevin Wolf .bdrv_co_preadv = bdrv_test_co_preadv,
113881cfd17SKevin Wolf
1145e8ac217SKevin Wolf .bdrv_drain_begin = bdrv_test_drain_begin,
1155e8ac217SKevin Wolf .bdrv_drain_end = bdrv_test_drain_end,
11686e1c840SKevin Wolf
117e5d8a406SMax Reitz .bdrv_child_perm = bdrv_default_perms,
1189746b35cSMax Reitz
119e2dd2737SKevin Wolf .bdrv_co_change_backing_file = bdrv_test_co_change_backing_file,
120881cfd17SKevin Wolf };
121881cfd17SKevin Wolf
aio_ret_cb(void * opaque,int ret)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
call_in_coroutine_entry(void * opaque)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
call_in_coroutine(void (* entry)(void))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
do_drain_begin(enum drain_type drain_type,BlockDriverState * bs)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
do_drain_end(enum drain_type drain_type,BlockDriverState * bs)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
do_drain_begin_unlocked(enum drain_type drain_type,BlockDriverState * bs)180f62c1729SKevin Wolf static void do_drain_begin_unlocked(enum drain_type drain_type, BlockDriverState *bs)
181f62c1729SKevin Wolf {
182f62c1729SKevin Wolf do_drain_begin(drain_type, bs);
183f62c1729SKevin Wolf }
184f62c1729SKevin Wolf
test_setup(void)18557f3d07bSKevin Wolf static BlockBackend * no_coroutine_fn test_setup(void)
18657f3d07bSKevin Wolf {
18757f3d07bSKevin Wolf BlockBackend *blk;
18857f3d07bSKevin Wolf BlockDriverState *bs, *backing;
18957f3d07bSKevin Wolf
19057f3d07bSKevin Wolf blk = blk_new(qemu_get_aio_context(), BLK_PERM_ALL, BLK_PERM_ALL);
19157f3d07bSKevin Wolf bs = bdrv_new_open_driver(&bdrv_test, "test-node", BDRV_O_RDWR,
19257f3d07bSKevin Wolf &error_abort);
19357f3d07bSKevin Wolf blk_insert_bs(blk, bs, &error_abort);
19457f3d07bSKevin Wolf
19557f3d07bSKevin Wolf backing = bdrv_new_open_driver(&bdrv_test, "backing", 0, &error_abort);
19657f3d07bSKevin Wolf bdrv_set_backing_hd(bs, backing, &error_abort);
19757f3d07bSKevin Wolf
19857f3d07bSKevin Wolf bdrv_unref(backing);
19957f3d07bSKevin Wolf bdrv_unref(bs);
20057f3d07bSKevin Wolf
20157f3d07bSKevin Wolf return blk;
20257f3d07bSKevin Wolf }
20357f3d07bSKevin Wolf
do_drain_end_unlocked(enum drain_type drain_type,BlockDriverState * bs)204f62c1729SKevin Wolf static void do_drain_end_unlocked(enum drain_type drain_type, BlockDriverState *bs)
205f62c1729SKevin Wolf {
206f62c1729SKevin Wolf do_drain_end(drain_type, bs);
207f62c1729SKevin Wolf }
208f62c1729SKevin Wolf
209004915a9SKevin Wolf /*
210004915a9SKevin Wolf * Locking the block graph would be a bit cumbersome here because this function
211004915a9SKevin Wolf * is called both in coroutine and non-coroutine context. We know this is a test
212004915a9SKevin Wolf * and nothing else is running, so don't bother with TSA.
213004915a9SKevin Wolf */
214004915a9SKevin Wolf static void coroutine_mixed_fn TSA_NO_TSA
test_drv_cb_common(BlockBackend * blk,enum drain_type drain_type,bool recursive)215004915a9SKevin Wolf test_drv_cb_common(BlockBackend *blk, enum drain_type drain_type,
21657f3d07bSKevin Wolf bool recursive)
217881cfd17SKevin Wolf {
21857f3d07bSKevin Wolf BlockDriverState *bs = blk_bs(blk);
21957f3d07bSKevin Wolf BlockDriverState *backing = bs->backing->bs;
22086e1c840SKevin Wolf BDRVTestState *s, *backing_s;
221881cfd17SKevin Wolf BlockAIOCB *acb;
222881cfd17SKevin Wolf int aio_ret;
223881cfd17SKevin Wolf
224405d8fe0SVladimir Sementsov-Ogievskiy QEMUIOVector qiov = QEMU_IOVEC_INIT_BUF(qiov, NULL, 0);
225881cfd17SKevin Wolf
226881cfd17SKevin Wolf s = bs->opaque;
22786e1c840SKevin Wolf backing_s = backing->opaque;
22886e1c840SKevin Wolf
229881cfd17SKevin Wolf /* Simple bdrv_drain_all_begin/end pair, check that CBs are called */
230881cfd17SKevin Wolf g_assert_cmpint(s->drain_count, ==, 0);
23186e1c840SKevin Wolf g_assert_cmpint(backing_s->drain_count, ==, 0);
23286e1c840SKevin Wolf
23386e1c840SKevin Wolf do_drain_begin(drain_type, bs);
23486e1c840SKevin Wolf
235881cfd17SKevin Wolf g_assert_cmpint(s->drain_count, ==, 1);
23686e1c840SKevin Wolf g_assert_cmpint(backing_s->drain_count, ==, !!recursive);
23786e1c840SKevin Wolf
23886e1c840SKevin Wolf do_drain_end(drain_type, bs);
23986e1c840SKevin Wolf
240881cfd17SKevin Wolf g_assert_cmpint(s->drain_count, ==, 0);
24186e1c840SKevin Wolf g_assert_cmpint(backing_s->drain_count, ==, 0);
242881cfd17SKevin Wolf
243881cfd17SKevin Wolf /* Now do the same while a request is pending */
244881cfd17SKevin Wolf aio_ret = -EINPROGRESS;
245881cfd17SKevin Wolf acb = blk_aio_preadv(blk, 0, &qiov, 0, aio_ret_cb, &aio_ret);
246881cfd17SKevin Wolf g_assert(acb != NULL);
247881cfd17SKevin Wolf g_assert_cmpint(aio_ret, ==, -EINPROGRESS);
248881cfd17SKevin Wolf
249881cfd17SKevin Wolf g_assert_cmpint(s->drain_count, ==, 0);
25086e1c840SKevin Wolf g_assert_cmpint(backing_s->drain_count, ==, 0);
25186e1c840SKevin Wolf
25286e1c840SKevin Wolf do_drain_begin(drain_type, bs);
25386e1c840SKevin Wolf
254881cfd17SKevin Wolf g_assert_cmpint(aio_ret, ==, 0);
255881cfd17SKevin Wolf g_assert_cmpint(s->drain_count, ==, 1);
25686e1c840SKevin Wolf g_assert_cmpint(backing_s->drain_count, ==, !!recursive);
257881cfd17SKevin Wolf
25886e1c840SKevin Wolf do_drain_end(drain_type, bs);
25986e1c840SKevin Wolf
26086e1c840SKevin Wolf g_assert_cmpint(s->drain_count, ==, 0);
26186e1c840SKevin Wolf g_assert_cmpint(backing_s->drain_count, ==, 0);
262881cfd17SKevin Wolf }
263881cfd17SKevin Wolf
test_drv_cb_drain_all(void)26486e1c840SKevin Wolf static void test_drv_cb_drain_all(void)
26586e1c840SKevin Wolf {
26657f3d07bSKevin Wolf BlockBackend *blk = test_setup();
26757f3d07bSKevin Wolf test_drv_cb_common(blk, BDRV_DRAIN_ALL, true);
26857f3d07bSKevin Wolf blk_unref(blk);
26986e1c840SKevin Wolf }
27086e1c840SKevin Wolf
test_drv_cb_drain(void)27186e1c840SKevin Wolf static void test_drv_cb_drain(void)
27286e1c840SKevin Wolf {
27357f3d07bSKevin Wolf BlockBackend *blk = test_setup();
27457f3d07bSKevin Wolf test_drv_cb_common(blk, BDRV_DRAIN, false);
27557f3d07bSKevin Wolf blk_unref(blk);
27657f3d07bSKevin Wolf }
27757f3d07bSKevin Wolf
test_drv_cb_co_drain_all_entry(void)27857f3d07bSKevin Wolf static void coroutine_fn test_drv_cb_co_drain_all_entry(void)
27957f3d07bSKevin Wolf {
28057f3d07bSKevin Wolf BlockBackend *blk = blk_all_next(NULL);
28157f3d07bSKevin Wolf test_drv_cb_common(blk, BDRV_DRAIN_ALL, true);
28286e1c840SKevin Wolf }
28386e1c840SKevin Wolf
test_drv_cb_co_drain_all(void)2846d0252f2SKevin Wolf static void test_drv_cb_co_drain_all(void)
2856d0252f2SKevin Wolf {
28657f3d07bSKevin Wolf BlockBackend *blk = test_setup();
28757f3d07bSKevin Wolf call_in_coroutine(test_drv_cb_co_drain_all_entry);
28857f3d07bSKevin Wolf blk_unref(blk);
28957f3d07bSKevin Wolf }
29057f3d07bSKevin Wolf
test_drv_cb_co_drain_entry(void)29157f3d07bSKevin Wolf static void coroutine_fn test_drv_cb_co_drain_entry(void)
29257f3d07bSKevin Wolf {
29357f3d07bSKevin Wolf BlockBackend *blk = blk_all_next(NULL);
29457f3d07bSKevin Wolf test_drv_cb_common(blk, BDRV_DRAIN, false);
2956d0252f2SKevin Wolf }
2966d0252f2SKevin Wolf
test_drv_cb_co_drain(void)2970582eb10SKevin Wolf static void test_drv_cb_co_drain(void)
2980582eb10SKevin Wolf {
29957f3d07bSKevin Wolf BlockBackend *blk = test_setup();
30057f3d07bSKevin Wolf call_in_coroutine(test_drv_cb_co_drain_entry);
30157f3d07bSKevin Wolf blk_unref(blk);
3020582eb10SKevin Wolf }
3030582eb10SKevin Wolf
304004915a9SKevin Wolf /*
305004915a9SKevin Wolf * Locking the block graph would be a bit cumbersome here because this function
306004915a9SKevin Wolf * is called both in coroutine and non-coroutine context. We know this is a test
307004915a9SKevin Wolf * and nothing else is running, so don't bother with TSA.
308004915a9SKevin Wolf */
309004915a9SKevin Wolf static void coroutine_mixed_fn TSA_NO_TSA
test_quiesce_common(BlockBackend * blk,enum drain_type drain_type,bool recursive)310004915a9SKevin Wolf test_quiesce_common(BlockBackend *blk, enum drain_type drain_type,
31157f3d07bSKevin Wolf bool recursive)
31289a6ceabSKevin Wolf {
31357f3d07bSKevin Wolf BlockDriverState *bs = blk_bs(blk);
31457f3d07bSKevin Wolf BlockDriverState *backing = bs->backing->bs;
31589a6ceabSKevin Wolf
31689a6ceabSKevin Wolf g_assert_cmpint(bs->quiesce_counter, ==, 0);
31789a6ceabSKevin Wolf g_assert_cmpint(backing->quiesce_counter, ==, 0);
31889a6ceabSKevin Wolf
31989a6ceabSKevin Wolf do_drain_begin(drain_type, bs);
32089a6ceabSKevin Wolf
32157e05be3SKevin Wolf if (drain_type == BDRV_DRAIN_ALL) {
32257e05be3SKevin Wolf g_assert_cmpint(bs->quiesce_counter, ==, 2);
32357e05be3SKevin Wolf } else {
32489a6ceabSKevin Wolf g_assert_cmpint(bs->quiesce_counter, ==, 1);
32557e05be3SKevin Wolf }
32689a6ceabSKevin Wolf g_assert_cmpint(backing->quiesce_counter, ==, !!recursive);
32789a6ceabSKevin Wolf
32889a6ceabSKevin Wolf do_drain_end(drain_type, bs);
32989a6ceabSKevin Wolf
33089a6ceabSKevin Wolf g_assert_cmpint(bs->quiesce_counter, ==, 0);
33189a6ceabSKevin Wolf g_assert_cmpint(backing->quiesce_counter, ==, 0);
33289a6ceabSKevin Wolf }
33389a6ceabSKevin Wolf
test_quiesce_drain_all(void)33489a6ceabSKevin Wolf static void test_quiesce_drain_all(void)
33589a6ceabSKevin Wolf {
33657f3d07bSKevin Wolf BlockBackend *blk = test_setup();
33757f3d07bSKevin Wolf test_quiesce_common(blk, BDRV_DRAIN_ALL, true);
33857f3d07bSKevin Wolf blk_unref(blk);
33989a6ceabSKevin Wolf }
34089a6ceabSKevin Wolf
test_quiesce_drain(void)34189a6ceabSKevin Wolf static void test_quiesce_drain(void)
34289a6ceabSKevin Wolf {
34357f3d07bSKevin Wolf BlockBackend *blk = test_setup();
34457f3d07bSKevin Wolf test_quiesce_common(blk, BDRV_DRAIN, false);
34557f3d07bSKevin Wolf blk_unref(blk);
34657f3d07bSKevin Wolf }
34757f3d07bSKevin Wolf
test_quiesce_co_drain_all_entry(void)34857f3d07bSKevin Wolf static void coroutine_fn test_quiesce_co_drain_all_entry(void)
34957f3d07bSKevin Wolf {
35057f3d07bSKevin Wolf BlockBackend *blk = blk_all_next(NULL);
35157f3d07bSKevin Wolf test_quiesce_common(blk, BDRV_DRAIN_ALL, true);
35289a6ceabSKevin Wolf }
35389a6ceabSKevin Wolf
test_quiesce_co_drain_all(void)3546d0252f2SKevin Wolf static void test_quiesce_co_drain_all(void)
3556d0252f2SKevin Wolf {
35657f3d07bSKevin Wolf BlockBackend *blk = test_setup();
35757f3d07bSKevin Wolf call_in_coroutine(test_quiesce_co_drain_all_entry);
35857f3d07bSKevin Wolf blk_unref(blk);
35957f3d07bSKevin Wolf }
36057f3d07bSKevin Wolf
test_quiesce_co_drain_entry(void)36157f3d07bSKevin Wolf static void coroutine_fn test_quiesce_co_drain_entry(void)
36257f3d07bSKevin Wolf {
36357f3d07bSKevin Wolf BlockBackend *blk = blk_all_next(NULL);
36457f3d07bSKevin Wolf test_quiesce_common(blk, BDRV_DRAIN, false);
3656d0252f2SKevin Wolf }
3666d0252f2SKevin Wolf
test_quiesce_co_drain(void)3670582eb10SKevin Wolf static void test_quiesce_co_drain(void)
3680582eb10SKevin Wolf {
36957f3d07bSKevin Wolf BlockBackend *blk = test_setup();
37057f3d07bSKevin Wolf call_in_coroutine(test_quiesce_co_drain_entry);
37157f3d07bSKevin Wolf blk_unref(blk);
3720582eb10SKevin Wolf }
3730582eb10SKevin Wolf
test_nested(void)3746c429a6aSKevin Wolf static void test_nested(void)
3756c429a6aSKevin Wolf {
3766c429a6aSKevin Wolf BlockBackend *blk;
3776c429a6aSKevin Wolf BlockDriverState *bs, *backing;
3786c429a6aSKevin Wolf BDRVTestState *s, *backing_s;
3796c429a6aSKevin Wolf enum drain_type outer, inner;
3806c429a6aSKevin Wolf
381d861ab3aSKevin Wolf blk = blk_new(qemu_get_aio_context(), BLK_PERM_ALL, BLK_PERM_ALL);
3826c429a6aSKevin Wolf bs = bdrv_new_open_driver(&bdrv_test, "test-node", BDRV_O_RDWR,
3836c429a6aSKevin Wolf &error_abort);
3846c429a6aSKevin Wolf s = bs->opaque;
3856c429a6aSKevin Wolf blk_insert_bs(blk, bs, &error_abort);
3866c429a6aSKevin Wolf
3876c429a6aSKevin Wolf backing = bdrv_new_open_driver(&bdrv_test, "backing", 0, &error_abort);
3886c429a6aSKevin Wolf backing_s = backing->opaque;
3896c429a6aSKevin Wolf bdrv_set_backing_hd(bs, backing, &error_abort);
3906c429a6aSKevin Wolf
3916c429a6aSKevin Wolf for (outer = 0; outer < DRAIN_TYPE_MAX; outer++) {
3926c429a6aSKevin Wolf for (inner = 0; inner < DRAIN_TYPE_MAX; inner++) {
39357e05be3SKevin Wolf int backing_quiesce = (outer == BDRV_DRAIN_ALL) +
39457e05be3SKevin Wolf (inner == BDRV_DRAIN_ALL);
3956c429a6aSKevin Wolf
3966c429a6aSKevin Wolf g_assert_cmpint(bs->quiesce_counter, ==, 0);
3976c429a6aSKevin Wolf g_assert_cmpint(backing->quiesce_counter, ==, 0);
3986c429a6aSKevin Wolf g_assert_cmpint(s->drain_count, ==, 0);
3996c429a6aSKevin Wolf g_assert_cmpint(backing_s->drain_count, ==, 0);
4006c429a6aSKevin Wolf
4016c429a6aSKevin Wolf do_drain_begin(outer, bs);
4026c429a6aSKevin Wolf do_drain_begin(inner, bs);
4036c429a6aSKevin Wolf
40457e05be3SKevin Wolf g_assert_cmpint(bs->quiesce_counter, ==, 2 + !!backing_quiesce);
4056c429a6aSKevin Wolf g_assert_cmpint(backing->quiesce_counter, ==, backing_quiesce);
40657e05be3SKevin Wolf g_assert_cmpint(s->drain_count, ==, 1);
40757e05be3SKevin Wolf g_assert_cmpint(backing_s->drain_count, ==, !!backing_quiesce);
4086c429a6aSKevin Wolf
4096c429a6aSKevin Wolf do_drain_end(inner, bs);
4106c429a6aSKevin Wolf do_drain_end(outer, bs);
4116c429a6aSKevin Wolf
4126c429a6aSKevin Wolf g_assert_cmpint(bs->quiesce_counter, ==, 0);
4136c429a6aSKevin Wolf g_assert_cmpint(backing->quiesce_counter, ==, 0);
4146c429a6aSKevin Wolf g_assert_cmpint(s->drain_count, ==, 0);
4156c429a6aSKevin Wolf g_assert_cmpint(backing_s->drain_count, ==, 0);
4166c429a6aSKevin Wolf }
4176c429a6aSKevin Wolf }
4186c429a6aSKevin Wolf
4196c429a6aSKevin Wolf bdrv_unref(backing);
4206c429a6aSKevin Wolf bdrv_unref(bs);
4216c429a6aSKevin Wolf blk_unref(blk);
4226c429a6aSKevin Wolf }
4236c429a6aSKevin Wolf
test_graph_change_drain_all(void)42419f7a7e5SKevin Wolf static void test_graph_change_drain_all(void)
42519f7a7e5SKevin Wolf {
42619f7a7e5SKevin Wolf BlockBackend *blk_a, *blk_b;
42719f7a7e5SKevin Wolf BlockDriverState *bs_a, *bs_b;
42819f7a7e5SKevin Wolf BDRVTestState *a_s, *b_s;
42919f7a7e5SKevin Wolf
43019f7a7e5SKevin Wolf /* Create node A with a BlockBackend */
431d861ab3aSKevin Wolf blk_a = blk_new(qemu_get_aio_context(), BLK_PERM_ALL, BLK_PERM_ALL);
43219f7a7e5SKevin Wolf bs_a = bdrv_new_open_driver(&bdrv_test, "test-node-a", BDRV_O_RDWR,
43319f7a7e5SKevin Wolf &error_abort);
43419f7a7e5SKevin Wolf a_s = bs_a->opaque;
43519f7a7e5SKevin Wolf blk_insert_bs(blk_a, bs_a, &error_abort);
43619f7a7e5SKevin Wolf
43719f7a7e5SKevin Wolf g_assert_cmpint(bs_a->quiesce_counter, ==, 0);
43819f7a7e5SKevin Wolf g_assert_cmpint(a_s->drain_count, ==, 0);
43919f7a7e5SKevin Wolf
44019f7a7e5SKevin Wolf /* Call bdrv_drain_all_begin() */
44119f7a7e5SKevin Wolf bdrv_drain_all_begin();
44219f7a7e5SKevin Wolf
44319f7a7e5SKevin Wolf g_assert_cmpint(bs_a->quiesce_counter, ==, 1);
44419f7a7e5SKevin Wolf g_assert_cmpint(a_s->drain_count, ==, 1);
44519f7a7e5SKevin Wolf
44619f7a7e5SKevin Wolf /* Create node B with a BlockBackend */
447d861ab3aSKevin Wolf blk_b = blk_new(qemu_get_aio_context(), BLK_PERM_ALL, BLK_PERM_ALL);
44819f7a7e5SKevin Wolf bs_b = bdrv_new_open_driver(&bdrv_test, "test-node-b", BDRV_O_RDWR,
44919f7a7e5SKevin Wolf &error_abort);
45019f7a7e5SKevin Wolf b_s = bs_b->opaque;
45119f7a7e5SKevin Wolf blk_insert_bs(blk_b, bs_b, &error_abort);
45219f7a7e5SKevin Wolf
45319f7a7e5SKevin Wolf g_assert_cmpint(bs_a->quiesce_counter, ==, 1);
45419f7a7e5SKevin Wolf g_assert_cmpint(bs_b->quiesce_counter, ==, 1);
45519f7a7e5SKevin Wolf g_assert_cmpint(a_s->drain_count, ==, 1);
45619f7a7e5SKevin Wolf g_assert_cmpint(b_s->drain_count, ==, 1);
45719f7a7e5SKevin Wolf
45819f7a7e5SKevin Wolf /* Unref and finally delete node A */
45919f7a7e5SKevin Wolf blk_unref(blk_a);
46019f7a7e5SKevin Wolf
46119f7a7e5SKevin Wolf g_assert_cmpint(bs_a->quiesce_counter, ==, 1);
46219f7a7e5SKevin Wolf g_assert_cmpint(bs_b->quiesce_counter, ==, 1);
46319f7a7e5SKevin Wolf g_assert_cmpint(a_s->drain_count, ==, 1);
46419f7a7e5SKevin Wolf g_assert_cmpint(b_s->drain_count, ==, 1);
46519f7a7e5SKevin Wolf
46619f7a7e5SKevin Wolf bdrv_unref(bs_a);
46719f7a7e5SKevin Wolf
46819f7a7e5SKevin Wolf g_assert_cmpint(bs_b->quiesce_counter, ==, 1);
46919f7a7e5SKevin Wolf g_assert_cmpint(b_s->drain_count, ==, 1);
47019f7a7e5SKevin Wolf
47119f7a7e5SKevin Wolf /* End the drained section */
47219f7a7e5SKevin Wolf bdrv_drain_all_end();
47319f7a7e5SKevin Wolf
47419f7a7e5SKevin Wolf g_assert_cmpint(bs_b->quiesce_counter, ==, 0);
47519f7a7e5SKevin Wolf g_assert_cmpint(b_s->drain_count, ==, 0);
47619f7a7e5SKevin Wolf
47719f7a7e5SKevin Wolf bdrv_unref(bs_b);
47819f7a7e5SKevin Wolf blk_unref(blk_b);
47919f7a7e5SKevin Wolf }
48019f7a7e5SKevin Wolf
481bb675689SKevin Wolf struct test_iothread_data {
482bb675689SKevin Wolf BlockDriverState *bs;
483bb675689SKevin Wolf enum drain_type drain_type;
484bb675689SKevin Wolf int *aio_ret;
485ab613350SStefan Hajnoczi bool co_done;
486bb675689SKevin Wolf };
487bb675689SKevin Wolf
test_iothread_drain_co_entry(void * opaque)488ab613350SStefan Hajnoczi static void coroutine_fn test_iothread_drain_co_entry(void *opaque)
489bb675689SKevin Wolf {
490bb675689SKevin Wolf struct test_iothread_data *data = opaque;
491bb675689SKevin Wolf
492bb675689SKevin Wolf do_drain_begin(data->drain_type, data->bs);
493bb675689SKevin Wolf g_assert_cmpint(*data->aio_ret, ==, 0);
494bb675689SKevin Wolf do_drain_end(data->drain_type, data->bs);
495bb675689SKevin Wolf
496ab613350SStefan Hajnoczi data->co_done = true;
497ab613350SStefan Hajnoczi aio_wait_kick();
498bb675689SKevin Wolf }
499bb675689SKevin Wolf
test_iothread_aio_cb(void * opaque,int ret)500bb675689SKevin Wolf static void test_iothread_aio_cb(void *opaque, int ret)
501bb675689SKevin Wolf {
502bb675689SKevin Wolf int *aio_ret = opaque;
503bb675689SKevin Wolf *aio_ret = ret;
504bb675689SKevin Wolf qemu_event_set(&done_event);
505bb675689SKevin Wolf }
506bb675689SKevin Wolf
test_iothread_main_thread_bh(void * opaque)507ecc1a5c7SKevin Wolf static void test_iothread_main_thread_bh(void *opaque)
508ecc1a5c7SKevin Wolf {
509ecc1a5c7SKevin Wolf struct test_iothread_data *data = opaque;
510ecc1a5c7SKevin Wolf
511ecc1a5c7SKevin Wolf bdrv_flush(data->bs);
512c8bf923dSStefan Hajnoczi bdrv_dec_in_flight(data->bs); /* incremented by test_iothread_common() */
513ecc1a5c7SKevin Wolf }
514ecc1a5c7SKevin Wolf
515bb675689SKevin Wolf /*
516bb675689SKevin Wolf * Starts an AIO request on a BDS that runs in the AioContext of iothread 1.
517bb675689SKevin Wolf * The request involves a BH on iothread 2 before it can complete.
518bb675689SKevin Wolf *
519bb675689SKevin Wolf * @drain_thread = 0 means that do_drain_begin/end are called from the main
520bb675689SKevin Wolf * thread, @drain_thread = 1 means that they are called from iothread 1. Drain
521bb675689SKevin Wolf * for this BDS cannot be called from iothread 2 because only the main thread
522bb675689SKevin Wolf * may do cross-AioContext polling.
523bb675689SKevin Wolf */
test_iothread_common(enum drain_type drain_type,int drain_thread)524bb675689SKevin Wolf static void test_iothread_common(enum drain_type drain_type, int drain_thread)
525bb675689SKevin Wolf {
526bb675689SKevin Wolf BlockBackend *blk;
527bb675689SKevin Wolf BlockDriverState *bs;
528bb675689SKevin Wolf BDRVTestState *s;
529bb675689SKevin Wolf BlockAIOCB *acb;
530ab613350SStefan Hajnoczi Coroutine *co;
531bb675689SKevin Wolf int aio_ret;
532bb675689SKevin Wolf struct test_iothread_data data;
533bb675689SKevin Wolf
534bb675689SKevin Wolf IOThread *a = iothread_new();
535bb675689SKevin Wolf IOThread *b = iothread_new();
536bb675689SKevin Wolf AioContext *ctx_a = iothread_get_aio_context(a);
537bb675689SKevin Wolf AioContext *ctx_b = iothread_get_aio_context(b);
538bb675689SKevin Wolf
539405d8fe0SVladimir Sementsov-Ogievskiy QEMUIOVector qiov = QEMU_IOVEC_INIT_BUF(qiov, NULL, 0);
540bb675689SKevin Wolf
541bb675689SKevin Wolf /* bdrv_drain_all() may only be called from the main loop thread */
542bb675689SKevin Wolf if (drain_type == BDRV_DRAIN_ALL && drain_thread != 0) {
543bb675689SKevin Wolf goto out;
544bb675689SKevin Wolf }
545bb675689SKevin Wolf
546d861ab3aSKevin Wolf blk = blk_new(qemu_get_aio_context(), BLK_PERM_ALL, BLK_PERM_ALL);
547bb675689SKevin Wolf bs = bdrv_new_open_driver(&bdrv_test, "test-node", BDRV_O_RDWR,
548bb675689SKevin Wolf &error_abort);
549bb675689SKevin Wolf s = bs->opaque;
550bb675689SKevin Wolf blk_insert_bs(blk, bs, &error_abort);
551cf312932SKevin Wolf blk_set_disable_request_queuing(blk, true);
552bb675689SKevin Wolf
55397896a48SKevin Wolf blk_set_aio_context(blk, ctx_a, &error_abort);
554bb675689SKevin Wolf
555bb675689SKevin Wolf s->bh_indirection_ctx = ctx_b;
556bb675689SKevin Wolf
557bb675689SKevin Wolf aio_ret = -EINPROGRESS;
558dd353157SKevin Wolf qemu_event_reset(&done_event);
559dd353157SKevin Wolf
560bb675689SKevin Wolf if (drain_thread == 0) {
561bb675689SKevin Wolf acb = blk_aio_preadv(blk, 0, &qiov, 0, test_iothread_aio_cb, &aio_ret);
562bb675689SKevin Wolf } else {
563bb675689SKevin Wolf acb = blk_aio_preadv(blk, 0, &qiov, 0, aio_ret_cb, &aio_ret);
564bb675689SKevin Wolf }
565bb675689SKevin Wolf g_assert(acb != NULL);
566bb675689SKevin Wolf g_assert_cmpint(aio_ret, ==, -EINPROGRESS);
567bb675689SKevin Wolf
568bb675689SKevin Wolf data = (struct test_iothread_data) {
569bb675689SKevin Wolf .bs = bs,
570bb675689SKevin Wolf .drain_type = drain_type,
571bb675689SKevin Wolf .aio_ret = &aio_ret,
572bb675689SKevin Wolf };
573bb675689SKevin Wolf
574bb675689SKevin Wolf switch (drain_thread) {
575bb675689SKevin Wolf case 0:
576c8bf923dSStefan Hajnoczi /*
577c8bf923dSStefan Hajnoczi * Increment in_flight so that do_drain_begin() waits for
578c8bf923dSStefan Hajnoczi * test_iothread_main_thread_bh(). This prevents the race between
579c8bf923dSStefan Hajnoczi * test_iothread_main_thread_bh() in IOThread a and do_drain_begin() in
580c8bf923dSStefan Hajnoczi * this thread. test_iothread_main_thread_bh() decrements in_flight.
581c8bf923dSStefan Hajnoczi */
582c8bf923dSStefan Hajnoczi bdrv_inc_in_flight(bs);
583ecc1a5c7SKevin Wolf aio_bh_schedule_oneshot(ctx_a, test_iothread_main_thread_bh, &data);
584ecc1a5c7SKevin Wolf
585bb675689SKevin Wolf /* The request is running on the IOThread a. Draining its block device
586bb675689SKevin Wolf * will make sure that it has completed as far as the BDS is concerned,
587bb675689SKevin Wolf * but the drain in this thread can continue immediately after
588bb675689SKevin Wolf * bdrv_dec_in_flight() and aio_ret might be assigned only slightly
589bb675689SKevin Wolf * later. */
590bb675689SKevin Wolf do_drain_begin(drain_type, bs);
591bb675689SKevin Wolf g_assert_cmpint(bs->in_flight, ==, 0);
592bb675689SKevin Wolf
593bb675689SKevin Wolf qemu_event_wait(&done_event);
594bb675689SKevin Wolf
595bb675689SKevin Wolf g_assert_cmpint(aio_ret, ==, 0);
596bb675689SKevin Wolf do_drain_end(drain_type, bs);
597bb675689SKevin Wolf break;
598bb675689SKevin Wolf case 1:
599ab613350SStefan Hajnoczi co = qemu_coroutine_create(test_iothread_drain_co_entry, &data);
600ab613350SStefan Hajnoczi aio_co_enter(ctx_a, co);
601ab613350SStefan Hajnoczi AIO_WAIT_WHILE_UNLOCKED(NULL, !data.co_done);
602bb675689SKevin Wolf break;
603bb675689SKevin Wolf default:
604bb675689SKevin Wolf g_assert_not_reached();
605bb675689SKevin Wolf }
606bb675689SKevin Wolf
60797896a48SKevin Wolf blk_set_aio_context(blk, qemu_get_aio_context(), &error_abort);
608bb675689SKevin Wolf
609bb675689SKevin Wolf bdrv_unref(bs);
610bb675689SKevin Wolf blk_unref(blk);
611bb675689SKevin Wolf
612bb675689SKevin Wolf out:
613bb675689SKevin Wolf iothread_join(a);
614bb675689SKevin Wolf iothread_join(b);
615bb675689SKevin Wolf }
616bb675689SKevin Wolf
test_iothread_drain_all(void)617bb675689SKevin Wolf static void test_iothread_drain_all(void)
618bb675689SKevin Wolf {
619bb675689SKevin Wolf test_iothread_common(BDRV_DRAIN_ALL, 0);
620bb675689SKevin Wolf test_iothread_common(BDRV_DRAIN_ALL, 1);
621bb675689SKevin Wolf }
622bb675689SKevin Wolf
test_iothread_drain(void)623bb675689SKevin Wolf static void test_iothread_drain(void)
624bb675689SKevin Wolf {
625bb675689SKevin Wolf test_iothread_common(BDRV_DRAIN, 0);
626bb675689SKevin Wolf test_iothread_common(BDRV_DRAIN, 1);
627bb675689SKevin Wolf }
628bb675689SKevin Wolf
6297253220dSKevin Wolf
6307253220dSKevin Wolf typedef struct TestBlockJob {
6317253220dSKevin Wolf BlockJob common;
6321b177bbeSVladimir Sementsov-Ogievskiy BlockDriverState *bs;
633d49725afSKevin Wolf int run_ret;
634d49725afSKevin Wolf int prepare_ret;
635*f8222bfbSVitalii Mordan
636*f8222bfbSVitalii Mordan /* Accessed with atomics */
637d8b3afd5SKevin Wolf bool running;
6387253220dSKevin Wolf bool should_complete;
6397253220dSKevin Wolf } TestBlockJob;
6407253220dSKevin Wolf
test_job_prepare(Job * job)641ae23dde9SKevin Wolf static int test_job_prepare(Job *job)
642ae23dde9SKevin Wolf {
643ae23dde9SKevin Wolf TestBlockJob *s = container_of(job, TestBlockJob, common.job);
644ae23dde9SKevin Wolf
645ae23dde9SKevin Wolf /* Provoke an AIO_WAIT_WHILE() call to verify there is no deadlock */
6461b177bbeSVladimir Sementsov-Ogievskiy bdrv_flush(s->bs);
647d49725afSKevin Wolf return s->prepare_ret;
648d49725afSKevin Wolf }
649d49725afSKevin Wolf
test_job_commit(Job * job)650d49725afSKevin Wolf static void test_job_commit(Job *job)
651d49725afSKevin Wolf {
652d49725afSKevin Wolf TestBlockJob *s = container_of(job, TestBlockJob, common.job);
653d49725afSKevin Wolf
654d49725afSKevin Wolf /* Provoke an AIO_WAIT_WHILE() call to verify there is no deadlock */
6551b177bbeSVladimir Sementsov-Ogievskiy bdrv_flush(s->bs);
656d49725afSKevin Wolf }
657d49725afSKevin Wolf
test_job_abort(Job * job)658d49725afSKevin Wolf static void test_job_abort(Job *job)
659d49725afSKevin Wolf {
660d49725afSKevin Wolf TestBlockJob *s = container_of(job, TestBlockJob, common.job);
661d49725afSKevin Wolf
662d49725afSKevin Wolf /* Provoke an AIO_WAIT_WHILE() call to verify there is no deadlock */
6631b177bbeSVladimir Sementsov-Ogievskiy bdrv_flush(s->bs);
664ae23dde9SKevin Wolf }
665ae23dde9SKevin Wolf
test_job_run(Job * job,Error ** errp)666f67432a2SJohn Snow static int coroutine_fn test_job_run(Job *job, Error **errp)
6677253220dSKevin Wolf {
668f67432a2SJohn Snow TestBlockJob *s = container_of(job, TestBlockJob, common.job);
6697253220dSKevin Wolf
670d8b3afd5SKevin Wolf /* We are running the actual job code past the pause point in
671d8b3afd5SKevin Wolf * job_co_entry(). */
672*f8222bfbSVitalii Mordan qatomic_set(&s->running, true);
673d8b3afd5SKevin Wolf
6742e1795b5SKevin Wolf job_transition_to_ready(&s->common.job);
675*f8222bfbSVitalii Mordan while (!qatomic_read(&s->should_complete)) {
6765599c162SKevin Wolf /* Avoid job_sleep_ns() because it marks the job as !busy. We want to
6775599c162SKevin Wolf * emulate some actual activity (probably some I/O) here so that drain
6785599c162SKevin Wolf * has to wait for this activity to stop. */
679d8b3afd5SKevin Wolf qemu_co_sleep_ns(QEMU_CLOCK_REALTIME, 1000000);
680d8b3afd5SKevin Wolf
68189bd0305SKevin Wolf job_pause_point(&s->common.job);
6827253220dSKevin Wolf }
6837253220dSKevin Wolf
684d49725afSKevin Wolf return s->run_ret;
6857253220dSKevin Wolf }
6867253220dSKevin Wolf
test_job_complete(Job * job,Error ** errp)6873453d972SKevin Wolf static void test_job_complete(Job *job, Error **errp)
6887253220dSKevin Wolf {
6893453d972SKevin Wolf TestBlockJob *s = container_of(job, TestBlockJob, common.job);
690*f8222bfbSVitalii Mordan qatomic_set(&s->should_complete, true);
6917253220dSKevin Wolf }
6927253220dSKevin Wolf
6937253220dSKevin Wolf BlockJobDriver test_job_driver = {
69433e9e9bdSKevin Wolf .job_driver = {
6957253220dSKevin Wolf .instance_size = sizeof(TestBlockJob),
69680fa2c75SKevin Wolf .free = block_job_free,
697b15de828SKevin Wolf .user_resume = block_job_user_resume,
698f67432a2SJohn Snow .run = test_job_run,
6997253220dSKevin Wolf .complete = test_job_complete,
700ae23dde9SKevin Wolf .prepare = test_job_prepare,
701d49725afSKevin Wolf .commit = test_job_commit,
702d49725afSKevin Wolf .abort = test_job_abort,
7033453d972SKevin Wolf },
7047253220dSKevin Wolf };
7057253220dSKevin Wolf
706d49725afSKevin Wolf enum test_job_result {
707d49725afSKevin Wolf TEST_JOB_SUCCESS,
708d49725afSKevin Wolf TEST_JOB_FAIL_RUN,
709d49725afSKevin Wolf TEST_JOB_FAIL_PREPARE,
710d49725afSKevin Wolf };
711d49725afSKevin Wolf
712d8b3afd5SKevin Wolf enum test_job_drain_node {
713d8b3afd5SKevin Wolf TEST_JOB_DRAIN_SRC,
714d8b3afd5SKevin Wolf TEST_JOB_DRAIN_SRC_CHILD,
715d8b3afd5SKevin Wolf };
716d8b3afd5SKevin Wolf
test_blockjob_common_drain_node(enum drain_type drain_type,bool use_iothread,enum test_job_result result,enum test_job_drain_node drain_node)717d8b3afd5SKevin Wolf static void test_blockjob_common_drain_node(enum drain_type drain_type,
718d8b3afd5SKevin Wolf bool use_iothread,
719d8b3afd5SKevin Wolf enum test_job_result result,
720d8b3afd5SKevin Wolf enum test_job_drain_node drain_node)
7217253220dSKevin Wolf {
7227253220dSKevin Wolf BlockBackend *blk_src, *blk_target;
723d8b3afd5SKevin Wolf BlockDriverState *src, *src_backing, *src_overlay, *target, *drain_bs;
7247253220dSKevin Wolf BlockJob *job;
725d49725afSKevin Wolf TestBlockJob *tjob;
726f62c1729SKevin Wolf IOThread *iothread = NULL;
7274770030bSMarc-André Lureau int ret = -1;
7287253220dSKevin Wolf
7297253220dSKevin Wolf src = bdrv_new_open_driver(&bdrv_test, "source", BDRV_O_RDWR,
7307253220dSKevin Wolf &error_abort);
731d8b3afd5SKevin Wolf src_backing = bdrv_new_open_driver(&bdrv_test, "source-backing",
732d8b3afd5SKevin Wolf BDRV_O_RDWR, &error_abort);
733d8b3afd5SKevin Wolf src_overlay = bdrv_new_open_driver(&bdrv_test, "source-overlay",
734d8b3afd5SKevin Wolf BDRV_O_RDWR, &error_abort);
735d8b3afd5SKevin Wolf
736d8b3afd5SKevin Wolf bdrv_set_backing_hd(src_overlay, src, &error_abort);
737d8b3afd5SKevin Wolf bdrv_unref(src);
738d8b3afd5SKevin Wolf bdrv_set_backing_hd(src, src_backing, &error_abort);
739d8b3afd5SKevin Wolf bdrv_unref(src_backing);
740d8b3afd5SKevin Wolf
741d861ab3aSKevin Wolf blk_src = blk_new(qemu_get_aio_context(), BLK_PERM_ALL, BLK_PERM_ALL);
742d8b3afd5SKevin Wolf blk_insert_bs(blk_src, src_overlay, &error_abort);
743d8b3afd5SKevin Wolf
744d8b3afd5SKevin Wolf switch (drain_node) {
745d8b3afd5SKevin Wolf case TEST_JOB_DRAIN_SRC:
746d8b3afd5SKevin Wolf drain_bs = src;
747d8b3afd5SKevin Wolf break;
748d8b3afd5SKevin Wolf case TEST_JOB_DRAIN_SRC_CHILD:
749d8b3afd5SKevin Wolf drain_bs = src_backing;
750d8b3afd5SKevin Wolf break;
751d8b3afd5SKevin Wolf default:
752d8b3afd5SKevin Wolf g_assert_not_reached();
753d8b3afd5SKevin Wolf }
7547253220dSKevin Wolf
755f62c1729SKevin Wolf if (use_iothread) {
756b49f4755SStefan Hajnoczi AioContext *ctx;
757b49f4755SStefan Hajnoczi
758f62c1729SKevin Wolf iothread = iothread_new();
759f62c1729SKevin Wolf ctx = iothread_get_aio_context(iothread);
76097896a48SKevin Wolf blk_set_aio_context(blk_src, ctx, &error_abort);
761f62c1729SKevin Wolf }
762f62c1729SKevin Wolf
7637253220dSKevin Wolf target = bdrv_new_open_driver(&bdrv_test, "target", BDRV_O_RDWR,
7647253220dSKevin Wolf &error_abort);
765d861ab3aSKevin Wolf blk_target = blk_new(qemu_get_aio_context(), BLK_PERM_ALL, BLK_PERM_ALL);
7667253220dSKevin Wolf blk_insert_bs(blk_target, target, &error_abort);
767132ada80SKevin Wolf blk_set_allow_aio_context_change(blk_target, true);
7687253220dSKevin Wolf
769d49725afSKevin Wolf tjob = block_job_create("job0", &test_job_driver, NULL, src,
770d49725afSKevin Wolf 0, BLK_PERM_ALL,
77175859b94SJohn Snow 0, 0, NULL, NULL, &error_abort);
7721b177bbeSVladimir Sementsov-Ogievskiy tjob->bs = src;
773d49725afSKevin Wolf job = &tjob->common;
774f3bbc53dSKevin Wolf
7756bc30f19SStefan Hajnoczi bdrv_graph_wrlock();
7767253220dSKevin Wolf block_job_add_bdrv(job, "target", target, 0, BLK_PERM_ALL, &error_abort);
7776bc30f19SStefan Hajnoczi bdrv_graph_wrunlock();
778d49725afSKevin Wolf
779d49725afSKevin Wolf switch (result) {
780d49725afSKevin Wolf case TEST_JOB_SUCCESS:
781d49725afSKevin Wolf break;
782d49725afSKevin Wolf case TEST_JOB_FAIL_RUN:
783d49725afSKevin Wolf tjob->run_ret = -EIO;
784d49725afSKevin Wolf break;
785d49725afSKevin Wolf case TEST_JOB_FAIL_PREPARE:
786d49725afSKevin Wolf tjob->prepare_ret = -EIO;
787d49725afSKevin Wolf break;
788d49725afSKevin Wolf }
789d49725afSKevin Wolf
790da01ff7fSKevin Wolf job_start(&job->job);
7917253220dSKevin Wolf
792d8b3afd5SKevin Wolf if (use_iothread) {
793d8b3afd5SKevin Wolf /* job_co_entry() is run in the I/O thread, wait for the actual job
794d8b3afd5SKevin Wolf * code to start (we don't want to catch the job in the pause point in
795d8b3afd5SKevin Wolf * job_co_entry(). */
796*f8222bfbSVitalii Mordan while (!qatomic_read(&tjob->running)) {
797d8b3afd5SKevin Wolf aio_poll(qemu_get_aio_context(), false);
798d8b3afd5SKevin Wolf }
799d8b3afd5SKevin Wolf }
800d8b3afd5SKevin Wolf
801191e7af3SEmanuele Giuseppe Esposito WITH_JOB_LOCK_GUARD() {
802da01ff7fSKevin Wolf g_assert_cmpint(job->job.pause_count, ==, 0);
803da01ff7fSKevin Wolf g_assert_false(job->job.paused);
804*f8222bfbSVitalii Mordan g_assert_true(qatomic_read(&tjob->running));
8055599c162SKevin Wolf g_assert_true(job->job.busy); /* We're in qemu_co_sleep_ns() */
806191e7af3SEmanuele Giuseppe Esposito }
8077253220dSKevin Wolf
808d8b3afd5SKevin Wolf do_drain_begin_unlocked(drain_type, drain_bs);
8097253220dSKevin Wolf
810191e7af3SEmanuele Giuseppe Esposito WITH_JOB_LOCK_GUARD() {
8117253220dSKevin Wolf if (drain_type == BDRV_DRAIN_ALL) {
81281193349SKevin Wolf /* bdrv_drain_all() drains both src and target */
813da01ff7fSKevin Wolf g_assert_cmpint(job->job.pause_count, ==, 2);
8147253220dSKevin Wolf } else {
815da01ff7fSKevin Wolf g_assert_cmpint(job->job.pause_count, ==, 1);
8167253220dSKevin Wolf }
81789bd0305SKevin Wolf g_assert_true(job->job.paused);
818da01ff7fSKevin Wolf g_assert_false(job->job.busy); /* The job is paused */
819191e7af3SEmanuele Giuseppe Esposito }
8207253220dSKevin Wolf
821d8b3afd5SKevin Wolf do_drain_end_unlocked(drain_type, drain_bs);
822f62c1729SKevin Wolf
823f62c1729SKevin Wolf if (use_iothread) {
824191e7af3SEmanuele Giuseppe Esposito /*
825191e7af3SEmanuele Giuseppe Esposito * Here we are waiting for the paused status to change,
826191e7af3SEmanuele Giuseppe Esposito * so don't bother protecting the read every time.
827191e7af3SEmanuele Giuseppe Esposito *
828191e7af3SEmanuele Giuseppe Esposito * paused is reset in the I/O thread, wait for it
829191e7af3SEmanuele Giuseppe Esposito */
830*f8222bfbSVitalii Mordan while (job_is_paused(&job->job)) {
831f62c1729SKevin Wolf aio_poll(qemu_get_aio_context(), false);
832f62c1729SKevin Wolf }
833f62c1729SKevin Wolf }
8347253220dSKevin Wolf
835191e7af3SEmanuele Giuseppe Esposito WITH_JOB_LOCK_GUARD() {
836da01ff7fSKevin Wolf g_assert_cmpint(job->job.pause_count, ==, 0);
837da01ff7fSKevin Wolf g_assert_false(job->job.paused);
83889bd0305SKevin Wolf g_assert_true(job->job.busy); /* We're in qemu_co_sleep_ns() */
839191e7af3SEmanuele Giuseppe Esposito }
8407253220dSKevin Wolf
841132ada80SKevin Wolf do_drain_begin_unlocked(drain_type, target);
8427253220dSKevin Wolf
843191e7af3SEmanuele Giuseppe Esposito WITH_JOB_LOCK_GUARD() {
8447253220dSKevin Wolf if (drain_type == BDRV_DRAIN_ALL) {
84581193349SKevin Wolf /* bdrv_drain_all() drains both src and target */
846da01ff7fSKevin Wolf g_assert_cmpint(job->job.pause_count, ==, 2);
8477253220dSKevin Wolf } else {
848da01ff7fSKevin Wolf g_assert_cmpint(job->job.pause_count, ==, 1);
8497253220dSKevin Wolf }
85089bd0305SKevin Wolf g_assert_true(job->job.paused);
851da01ff7fSKevin Wolf g_assert_false(job->job.busy); /* The job is paused */
852191e7af3SEmanuele Giuseppe Esposito }
8537253220dSKevin Wolf
854132ada80SKevin Wolf do_drain_end_unlocked(drain_type, target);
8557253220dSKevin Wolf
856f62c1729SKevin Wolf if (use_iothread) {
857191e7af3SEmanuele Giuseppe Esposito /*
858191e7af3SEmanuele Giuseppe Esposito * Here we are waiting for the paused status to change,
859191e7af3SEmanuele Giuseppe Esposito * so don't bother protecting the read every time.
860191e7af3SEmanuele Giuseppe Esposito *
861191e7af3SEmanuele Giuseppe Esposito * paused is reset in the I/O thread, wait for it
862191e7af3SEmanuele Giuseppe Esposito */
863*f8222bfbSVitalii Mordan while (job_is_paused(&job->job)) {
864f62c1729SKevin Wolf aio_poll(qemu_get_aio_context(), false);
865f62c1729SKevin Wolf }
866f62c1729SKevin Wolf }
867f62c1729SKevin Wolf
868191e7af3SEmanuele Giuseppe Esposito WITH_JOB_LOCK_GUARD() {
869da01ff7fSKevin Wolf g_assert_cmpint(job->job.pause_count, ==, 0);
870da01ff7fSKevin Wolf g_assert_false(job->job.paused);
8715599c162SKevin Wolf g_assert_true(job->job.busy); /* We're in qemu_co_sleep_ns() */
872191e7af3SEmanuele Giuseppe Esposito }
8737253220dSKevin Wolf
874191e7af3SEmanuele Giuseppe Esposito WITH_JOB_LOCK_GUARD() {
875191e7af3SEmanuele Giuseppe Esposito ret = job_complete_sync_locked(&job->job, &error_abort);
876191e7af3SEmanuele Giuseppe Esposito }
877d49725afSKevin Wolf g_assert_cmpint(ret, ==, (result == TEST_JOB_SUCCESS ? 0 : -EIO));
8787253220dSKevin Wolf
879f62c1729SKevin Wolf if (use_iothread) {
88097896a48SKevin Wolf blk_set_aio_context(blk_src, qemu_get_aio_context(), &error_abort);
881ad943dcbSKevin Wolf assert(blk_get_aio_context(blk_target) == qemu_get_aio_context());
882f62c1729SKevin Wolf }
883f62c1729SKevin Wolf
8847253220dSKevin Wolf blk_unref(blk_src);
8857253220dSKevin Wolf blk_unref(blk_target);
886d8b3afd5SKevin Wolf bdrv_unref(src_overlay);
8877253220dSKevin Wolf bdrv_unref(target);
888f62c1729SKevin Wolf
889f62c1729SKevin Wolf if (iothread) {
890f62c1729SKevin Wolf iothread_join(iothread);
891f62c1729SKevin Wolf }
8927253220dSKevin Wolf }
8937253220dSKevin Wolf
test_blockjob_common(enum drain_type drain_type,bool use_iothread,enum test_job_result result)894d8b3afd5SKevin Wolf static void test_blockjob_common(enum drain_type drain_type, bool use_iothread,
895d8b3afd5SKevin Wolf enum test_job_result result)
896d8b3afd5SKevin Wolf {
897d8b3afd5SKevin Wolf test_blockjob_common_drain_node(drain_type, use_iothread, result,
898d8b3afd5SKevin Wolf TEST_JOB_DRAIN_SRC);
899d8b3afd5SKevin Wolf test_blockjob_common_drain_node(drain_type, use_iothread, result,
900d8b3afd5SKevin Wolf TEST_JOB_DRAIN_SRC_CHILD);
901d8b3afd5SKevin Wolf }
902d8b3afd5SKevin Wolf
test_blockjob_drain_all(void)9037253220dSKevin Wolf static void test_blockjob_drain_all(void)
9047253220dSKevin Wolf {
905d49725afSKevin Wolf test_blockjob_common(BDRV_DRAIN_ALL, false, TEST_JOB_SUCCESS);
9067253220dSKevin Wolf }
9077253220dSKevin Wolf
test_blockjob_drain(void)9087253220dSKevin Wolf static void test_blockjob_drain(void)
9097253220dSKevin Wolf {
910d49725afSKevin Wolf test_blockjob_common(BDRV_DRAIN, false, TEST_JOB_SUCCESS);
9117253220dSKevin Wolf }
9127253220dSKevin Wolf
test_blockjob_error_drain_all(void)913d49725afSKevin Wolf static void test_blockjob_error_drain_all(void)
914d49725afSKevin Wolf {
915d49725afSKevin Wolf test_blockjob_common(BDRV_DRAIN_ALL, false, TEST_JOB_FAIL_RUN);
916d49725afSKevin Wolf test_blockjob_common(BDRV_DRAIN_ALL, false, TEST_JOB_FAIL_PREPARE);
917d49725afSKevin Wolf }
918d49725afSKevin Wolf
test_blockjob_error_drain(void)919d49725afSKevin Wolf static void test_blockjob_error_drain(void)
920d49725afSKevin Wolf {
921d49725afSKevin Wolf test_blockjob_common(BDRV_DRAIN, false, TEST_JOB_FAIL_RUN);
922d49725afSKevin Wolf test_blockjob_common(BDRV_DRAIN, false, TEST_JOB_FAIL_PREPARE);
923d49725afSKevin Wolf }
924d49725afSKevin Wolf
test_blockjob_iothread_drain_all(void)925f62c1729SKevin Wolf static void test_blockjob_iothread_drain_all(void)
926f62c1729SKevin Wolf {
927d49725afSKevin Wolf test_blockjob_common(BDRV_DRAIN_ALL, true, TEST_JOB_SUCCESS);
928f62c1729SKevin Wolf }
929f62c1729SKevin Wolf
test_blockjob_iothread_drain(void)930f62c1729SKevin Wolf static void test_blockjob_iothread_drain(void)
931f62c1729SKevin Wolf {
932d49725afSKevin Wolf test_blockjob_common(BDRV_DRAIN, true, TEST_JOB_SUCCESS);
933f62c1729SKevin Wolf }
934f62c1729SKevin Wolf
test_blockjob_iothread_error_drain_all(void)935d49725afSKevin Wolf static void test_blockjob_iothread_error_drain_all(void)
936d49725afSKevin Wolf {
937d49725afSKevin Wolf test_blockjob_common(BDRV_DRAIN_ALL, true, TEST_JOB_FAIL_RUN);
938d49725afSKevin Wolf test_blockjob_common(BDRV_DRAIN_ALL, true, TEST_JOB_FAIL_PREPARE);
939d49725afSKevin Wolf }
940d49725afSKevin Wolf
test_blockjob_iothread_error_drain(void)941d49725afSKevin Wolf static void test_blockjob_iothread_error_drain(void)
942d49725afSKevin Wolf {
943d49725afSKevin Wolf test_blockjob_common(BDRV_DRAIN, true, TEST_JOB_FAIL_RUN);
944d49725afSKevin Wolf test_blockjob_common(BDRV_DRAIN, true, TEST_JOB_FAIL_PREPARE);
945d49725afSKevin Wolf }
946d49725afSKevin Wolf
9474c8158e3SMax Reitz
9484c8158e3SMax Reitz typedef struct BDRVTestTopState {
9494c8158e3SMax Reitz BdrvChild *wait_child;
9504c8158e3SMax Reitz } BDRVTestTopState;
9514c8158e3SMax Reitz
bdrv_test_top_close(BlockDriverState * bs)9524c8158e3SMax Reitz static void bdrv_test_top_close(BlockDriverState *bs)
9534c8158e3SMax Reitz {
9544c8158e3SMax Reitz BdrvChild *c, *next_c;
95532a8aba3SKevin Wolf
9566bc30f19SStefan Hajnoczi bdrv_graph_wrlock();
9574c8158e3SMax Reitz QLIST_FOREACH_SAFE(c, &bs->children, next, next_c) {
9584c8158e3SMax Reitz bdrv_unref_child(bs, c);
9594c8158e3SMax Reitz }
9606bc30f19SStefan Hajnoczi bdrv_graph_wrunlock();
9614c8158e3SMax Reitz }
9624c8158e3SMax Reitz
963b9b10c35SKevin Wolf static int coroutine_fn GRAPH_RDLOCK
bdrv_test_top_co_preadv(BlockDriverState * bs,int64_t offset,int64_t bytes,QEMUIOVector * qiov,BdrvRequestFlags flags)964b9b10c35SKevin Wolf bdrv_test_top_co_preadv(BlockDriverState *bs, int64_t offset, int64_t bytes,
965b9b10c35SKevin Wolf QEMUIOVector *qiov, BdrvRequestFlags flags)
9664c8158e3SMax Reitz {
9674c8158e3SMax Reitz BDRVTestTopState *tts = bs->opaque;
9684c8158e3SMax Reitz return bdrv_co_preadv(tts->wait_child, offset, bytes, qiov, flags);
9694c8158e3SMax Reitz }
9704c8158e3SMax Reitz
9714c8158e3SMax Reitz static BlockDriver bdrv_test_top_driver = {
9724c8158e3SMax Reitz .format_name = "test_top_driver",
9734c8158e3SMax Reitz .instance_size = sizeof(BDRVTestTopState),
9744c8158e3SMax Reitz
9754c8158e3SMax Reitz .bdrv_close = bdrv_test_top_close,
9764c8158e3SMax Reitz .bdrv_co_preadv = bdrv_test_top_co_preadv,
9774c8158e3SMax Reitz
97869dca43dSMax Reitz .bdrv_child_perm = bdrv_default_perms,
9794c8158e3SMax Reitz };
9804c8158e3SMax Reitz
9814c8158e3SMax Reitz typedef struct TestCoDeleteByDrainData {
9824c8158e3SMax Reitz BlockBackend *blk;
9834c8158e3SMax Reitz bool detach_instead_of_delete;
9844c8158e3SMax Reitz bool done;
9854c8158e3SMax Reitz } TestCoDeleteByDrainData;
9864c8158e3SMax Reitz
test_co_delete_by_drain(void * opaque)9874c8158e3SMax Reitz static void coroutine_fn test_co_delete_by_drain(void *opaque)
9884c8158e3SMax Reitz {
9894c8158e3SMax Reitz TestCoDeleteByDrainData *dbdd = opaque;
9904c8158e3SMax Reitz BlockBackend *blk = dbdd->blk;
9914c8158e3SMax Reitz BlockDriverState *bs = blk_bs(blk);
9924c8158e3SMax Reitz BDRVTestTopState *tts = bs->opaque;
9934c8158e3SMax Reitz void *buffer = g_malloc(65536);
994405d8fe0SVladimir Sementsov-Ogievskiy QEMUIOVector qiov = QEMU_IOVEC_INIT_BUF(qiov, buffer, 65536);
9954c8158e3SMax Reitz
9964c8158e3SMax Reitz /* Pretend some internal write operation from parent to child.
9974c8158e3SMax Reitz * Important: We have to read from the child, not from the parent!
9984c8158e3SMax Reitz * Draining works by first propagating it all up the tree to the
9994c8158e3SMax Reitz * root and then waiting for drainage from root to the leaves
10004c8158e3SMax Reitz * (protocol nodes). If we have a request waiting on the root,
10014c8158e3SMax Reitz * everything will be drained before we go back down the tree, but
10024c8158e3SMax Reitz * we do not want that. We want to be in the middle of draining
10034c8158e3SMax Reitz * when this following requests returns. */
100487f130bdSKevin Wolf bdrv_graph_co_rdlock();
10054c8158e3SMax Reitz bdrv_co_preadv(tts->wait_child, 0, 65536, &qiov, 0);
100687f130bdSKevin Wolf bdrv_graph_co_rdunlock();
10074c8158e3SMax Reitz
10084c8158e3SMax Reitz g_assert_cmpint(bs->refcnt, ==, 1);
10094c8158e3SMax Reitz
10104c8158e3SMax Reitz if (!dbdd->detach_instead_of_delete) {
101101a10c24SKevin Wolf blk_co_unref(blk);
10124c8158e3SMax Reitz } else {
10134c8158e3SMax Reitz BdrvChild *c, *next_c;
1014680e0cc4SKevin Wolf bdrv_graph_co_rdlock();
10154c8158e3SMax Reitz QLIST_FOREACH_SAFE(c, &bs->children, next, next_c) {
1016680e0cc4SKevin Wolf bdrv_graph_co_rdunlock();
101732a8aba3SKevin Wolf bdrv_co_unref_child(bs, c);
1018680e0cc4SKevin Wolf bdrv_graph_co_rdlock();
10194c8158e3SMax Reitz }
1020680e0cc4SKevin Wolf bdrv_graph_co_rdunlock();
10214c8158e3SMax Reitz }
10224c8158e3SMax Reitz
10234c8158e3SMax Reitz dbdd->done = true;
10247b43db3cSMarc-André Lureau g_free(buffer);
10254c8158e3SMax Reitz }
10264c8158e3SMax Reitz
10274c8158e3SMax Reitz /**
10284c8158e3SMax Reitz * Test what happens when some BDS has some children, you drain one of
10294c8158e3SMax Reitz * them and this results in the BDS being deleted.
10304c8158e3SMax Reitz *
10314c8158e3SMax Reitz * If @detach_instead_of_delete is set, the BDS is not going to be
10324c8158e3SMax Reitz * deleted but will only detach all of its children.
10334c8158e3SMax Reitz */
do_test_delete_by_drain(bool detach_instead_of_delete,enum drain_type drain_type)1034ebd31837SKevin Wolf static void do_test_delete_by_drain(bool detach_instead_of_delete,
1035ebd31837SKevin Wolf enum drain_type drain_type)
10364c8158e3SMax Reitz {
10374c8158e3SMax Reitz BlockBackend *blk;
10384c8158e3SMax Reitz BlockDriverState *bs, *child_bs, *null_bs;
10394c8158e3SMax Reitz BDRVTestTopState *tts;
10404c8158e3SMax Reitz TestCoDeleteByDrainData dbdd;
10414c8158e3SMax Reitz Coroutine *co;
10424c8158e3SMax Reitz
10434c8158e3SMax Reitz bs = bdrv_new_open_driver(&bdrv_test_top_driver, "top", BDRV_O_RDWR,
10444c8158e3SMax Reitz &error_abort);
10454c8158e3SMax Reitz bs->total_sectors = 65536 >> BDRV_SECTOR_BITS;
10464c8158e3SMax Reitz tts = bs->opaque;
10474c8158e3SMax Reitz
10484c8158e3SMax Reitz null_bs = bdrv_open("null-co://", NULL, NULL, BDRV_O_RDWR | BDRV_O_PROTOCOL,
10494c8158e3SMax Reitz &error_abort);
10506bc30f19SStefan Hajnoczi bdrv_graph_wrlock();
1051a16be3cdSMax Reitz bdrv_attach_child(bs, null_bs, "null-child", &child_of_bds,
1052a16be3cdSMax Reitz BDRV_CHILD_DATA, &error_abort);
10536bc30f19SStefan Hajnoczi bdrv_graph_wrunlock();
10544c8158e3SMax Reitz
10554c8158e3SMax Reitz /* This child will be the one to pass to requests through to, and
10564c8158e3SMax Reitz * it will stall until a drain occurs */
10574c8158e3SMax Reitz child_bs = bdrv_new_open_driver(&bdrv_test, "child", BDRV_O_RDWR,
10584c8158e3SMax Reitz &error_abort);
10594c8158e3SMax Reitz child_bs->total_sectors = 65536 >> BDRV_SECTOR_BITS;
10604c8158e3SMax Reitz /* Takes our reference to child_bs */
10616bc30f19SStefan Hajnoczi bdrv_graph_wrlock();
1062a16be3cdSMax Reitz tts->wait_child = bdrv_attach_child(bs, child_bs, "wait-child",
1063a16be3cdSMax Reitz &child_of_bds,
1064a16be3cdSMax Reitz BDRV_CHILD_DATA | BDRV_CHILD_PRIMARY,
1065a16be3cdSMax Reitz &error_abort);
10666bc30f19SStefan Hajnoczi bdrv_graph_wrunlock();
10674c8158e3SMax Reitz
10684c8158e3SMax Reitz /* This child is just there to be deleted
10694c8158e3SMax Reitz * (for detach_instead_of_delete == true) */
10704c8158e3SMax Reitz null_bs = bdrv_open("null-co://", NULL, NULL, BDRV_O_RDWR | BDRV_O_PROTOCOL,
10714c8158e3SMax Reitz &error_abort);
10726bc30f19SStefan Hajnoczi bdrv_graph_wrlock();
1073a16be3cdSMax Reitz bdrv_attach_child(bs, null_bs, "null-child", &child_of_bds, BDRV_CHILD_DATA,
1074a16be3cdSMax Reitz &error_abort);
10756bc30f19SStefan Hajnoczi bdrv_graph_wrunlock();
10764c8158e3SMax Reitz
1077d861ab3aSKevin Wolf blk = blk_new(qemu_get_aio_context(), BLK_PERM_ALL, BLK_PERM_ALL);
10784c8158e3SMax Reitz blk_insert_bs(blk, bs, &error_abort);
10794c8158e3SMax Reitz
10804c8158e3SMax Reitz /* Referenced by blk now */
10814c8158e3SMax Reitz bdrv_unref(bs);
10824c8158e3SMax Reitz
10834c8158e3SMax Reitz g_assert_cmpint(bs->refcnt, ==, 1);
10844c8158e3SMax Reitz g_assert_cmpint(child_bs->refcnt, ==, 1);
10854c8158e3SMax Reitz g_assert_cmpint(null_bs->refcnt, ==, 1);
10864c8158e3SMax Reitz
10874c8158e3SMax Reitz
10884c8158e3SMax Reitz dbdd = (TestCoDeleteByDrainData){
10894c8158e3SMax Reitz .blk = blk,
10904c8158e3SMax Reitz .detach_instead_of_delete = detach_instead_of_delete,
10914c8158e3SMax Reitz .done = false,
10924c8158e3SMax Reitz };
10934c8158e3SMax Reitz co = qemu_coroutine_create(test_co_delete_by_drain, &dbdd);
10944c8158e3SMax Reitz qemu_coroutine_enter(co);
10954c8158e3SMax Reitz
10964c8158e3SMax Reitz /* Drain the child while the read operation is still pending.
10974c8158e3SMax Reitz * This should result in the operation finishing and
10984c8158e3SMax Reitz * test_co_delete_by_drain() resuming. Thus, @bs will be deleted
10994c8158e3SMax Reitz * and the coroutine will exit while this drain operation is still
11004c8158e3SMax Reitz * in progress. */
1101ebd31837SKevin Wolf switch (drain_type) {
1102ebd31837SKevin Wolf case BDRV_DRAIN:
11034c8158e3SMax Reitz bdrv_ref(child_bs);
11044c8158e3SMax Reitz bdrv_drain(child_bs);
11054c8158e3SMax Reitz bdrv_unref(child_bs);
1106ebd31837SKevin Wolf break;
110719f7a7e5SKevin Wolf case BDRV_DRAIN_ALL:
110819f7a7e5SKevin Wolf bdrv_drain_all_begin();
110919f7a7e5SKevin Wolf bdrv_drain_all_end();
111019f7a7e5SKevin Wolf break;
1111ebd31837SKevin Wolf default:
1112ebd31837SKevin Wolf g_assert_not_reached();
1113ebd31837SKevin Wolf }
11144c8158e3SMax Reitz
11154c8158e3SMax Reitz while (!dbdd.done) {
11164c8158e3SMax Reitz aio_poll(qemu_get_aio_context(), true);
11174c8158e3SMax Reitz }
11184c8158e3SMax Reitz
11194c8158e3SMax Reitz if (detach_instead_of_delete) {
11204c8158e3SMax Reitz /* Here, the reference has not passed over to the coroutine,
11214c8158e3SMax Reitz * so we have to delete the BB ourselves */
11224c8158e3SMax Reitz blk_unref(blk);
11234c8158e3SMax Reitz }
11244c8158e3SMax Reitz }
11254c8158e3SMax Reitz
test_delete_by_drain(void)11264c8158e3SMax Reitz static void test_delete_by_drain(void)
11274c8158e3SMax Reitz {
1128ebd31837SKevin Wolf do_test_delete_by_drain(false, BDRV_DRAIN);
11294c8158e3SMax Reitz }
11304c8158e3SMax Reitz
test_detach_by_drain_all(void)113119f7a7e5SKevin Wolf static void test_detach_by_drain_all(void)
113219f7a7e5SKevin Wolf {
113319f7a7e5SKevin Wolf do_test_delete_by_drain(true, BDRV_DRAIN_ALL);
113419f7a7e5SKevin Wolf }
113519f7a7e5SKevin Wolf
test_detach_by_drain(void)11364c8158e3SMax Reitz static void test_detach_by_drain(void)
11374c8158e3SMax Reitz {
1138ebd31837SKevin Wolf do_test_delete_by_drain(true, BDRV_DRAIN);
1139ebd31837SKevin Wolf }
1140ebd31837SKevin Wolf
11414c8158e3SMax Reitz
1142231281abSKevin Wolf struct detach_by_parent_data {
1143231281abSKevin Wolf BlockDriverState *parent_b;
1144231281abSKevin Wolf BdrvChild *child_b;
1145231281abSKevin Wolf BlockDriverState *c;
1146231281abSKevin Wolf BdrvChild *child_c;
114757320ca9SKevin Wolf bool by_parent_cb;
1148617f3a96SKevin Wolf bool detach_on_drain;
1149231281abSKevin Wolf };
115057320ca9SKevin Wolf static struct detach_by_parent_data detach_by_parent_data;
1151231281abSKevin Wolf
detach_indirect_bh(void * opaque)1152903df115SKevin Wolf static void no_coroutine_fn detach_indirect_bh(void *opaque)
1153231281abSKevin Wolf {
1154231281abSKevin Wolf struct detach_by_parent_data *data = opaque;
1155231281abSKevin Wolf
1156617f3a96SKevin Wolf bdrv_dec_in_flight(data->child_b->bs);
115732a8aba3SKevin Wolf
11586bc30f19SStefan Hajnoczi bdrv_graph_wrlock();
1159231281abSKevin Wolf bdrv_unref_child(data->parent_b, data->child_b);
1160231281abSKevin Wolf
1161231281abSKevin Wolf bdrv_ref(data->c);
1162231281abSKevin Wolf data->child_c = bdrv_attach_child(data->parent_b, data->c, "PB-C",
1163a16be3cdSMax Reitz &child_of_bds, BDRV_CHILD_DATA,
1164a16be3cdSMax Reitz &error_abort);
11656bc30f19SStefan Hajnoczi bdrv_graph_wrunlock();
1166231281abSKevin Wolf }
1167231281abSKevin Wolf
detach_by_parent_aio_cb(void * opaque,int ret)1168903df115SKevin Wolf static void coroutine_mixed_fn detach_by_parent_aio_cb(void *opaque, int ret)
116957320ca9SKevin Wolf {
117057320ca9SKevin Wolf struct detach_by_parent_data *data = &detach_by_parent_data;
117157320ca9SKevin Wolf
117257320ca9SKevin Wolf g_assert_cmpint(ret, ==, 0);
117357320ca9SKevin Wolf if (data->by_parent_cb) {
1174617f3a96SKevin Wolf bdrv_inc_in_flight(data->child_b->bs);
1175903df115SKevin Wolf aio_bh_schedule_oneshot(qemu_get_current_aio_context(),
1176903df115SKevin Wolf detach_indirect_bh, &detach_by_parent_data);
117757320ca9SKevin Wolf }
117857320ca9SKevin Wolf }
117957320ca9SKevin Wolf
detach_by_driver_cb_drained_begin(BdrvChild * child)1180d05ab380SEmanuele Giuseppe Esposito static void GRAPH_RDLOCK detach_by_driver_cb_drained_begin(BdrvChild *child)
118157320ca9SKevin Wolf {
1182617f3a96SKevin Wolf struct detach_by_parent_data *data = &detach_by_parent_data;
1183617f3a96SKevin Wolf
1184617f3a96SKevin Wolf if (!data->detach_on_drain) {
1185617f3a96SKevin Wolf return;
1186617f3a96SKevin Wolf }
1187617f3a96SKevin Wolf data->detach_on_drain = false;
1188617f3a96SKevin Wolf
1189617f3a96SKevin Wolf bdrv_inc_in_flight(data->child_b->bs);
119057320ca9SKevin Wolf aio_bh_schedule_oneshot(qemu_get_current_aio_context(),
119157320ca9SKevin Wolf detach_indirect_bh, &detach_by_parent_data);
1192a16be3cdSMax Reitz child_of_bds.drained_begin(child);
119357320ca9SKevin Wolf }
119457320ca9SKevin Wolf
1195bd86fb99SMax Reitz static BdrvChildClass detach_by_driver_cb_class;
119657320ca9SKevin Wolf
1197231281abSKevin Wolf /*
1198231281abSKevin Wolf * Initial graph:
1199231281abSKevin Wolf *
1200231281abSKevin Wolf * PA PB
1201231281abSKevin Wolf * \ / \
1202231281abSKevin Wolf * A B C
1203231281abSKevin Wolf *
120457320ca9SKevin Wolf * by_parent_cb == true: Test that parent callbacks don't poll
120557320ca9SKevin Wolf *
120657320ca9SKevin Wolf * PA has a pending write request whose callback changes the child nodes of
120757320ca9SKevin Wolf * PB: It removes B and adds C instead. The subtree of PB is drained, which
120857320ca9SKevin Wolf * will indirectly drain the write request, too.
120957320ca9SKevin Wolf *
121057320ca9SKevin Wolf * by_parent_cb == false: Test that bdrv_drain_invoke() doesn't poll
121157320ca9SKevin Wolf *
1212bd86fb99SMax Reitz * PA's BdrvChildClass has a .drained_begin callback that schedules a BH
121357320ca9SKevin Wolf * that does the same graph change. If bdrv_drain_invoke() calls it, the
121457320ca9SKevin Wolf * state is messed up, but if it is only polled in the single
121557320ca9SKevin Wolf * BDRV_POLL_WHILE() at the end of the drain, this should work fine.
1216231281abSKevin Wolf */
test_detach_indirect(bool by_parent_cb)1217d05ab380SEmanuele Giuseppe Esposito static void TSA_NO_TSA test_detach_indirect(bool by_parent_cb)
1218231281abSKevin Wolf {
1219231281abSKevin Wolf BlockBackend *blk;
1220231281abSKevin Wolf BlockDriverState *parent_a, *parent_b, *a, *b, *c;
1221231281abSKevin Wolf BdrvChild *child_a, *child_b;
1222231281abSKevin Wolf BlockAIOCB *acb;
1223231281abSKevin Wolf
1224405d8fe0SVladimir Sementsov-Ogievskiy QEMUIOVector qiov = QEMU_IOVEC_INIT_BUF(qiov, NULL, 0);
1225231281abSKevin Wolf
122657320ca9SKevin Wolf if (!by_parent_cb) {
1227a16be3cdSMax Reitz detach_by_driver_cb_class = child_of_bds;
1228bd86fb99SMax Reitz detach_by_driver_cb_class.drained_begin =
122957320ca9SKevin Wolf detach_by_driver_cb_drained_begin;
1230617f3a96SKevin Wolf detach_by_driver_cb_class.drained_end = NULL;
1231617f3a96SKevin Wolf detach_by_driver_cb_class.drained_poll = NULL;
123257320ca9SKevin Wolf }
123357320ca9SKevin Wolf
1234617f3a96SKevin Wolf detach_by_parent_data = (struct detach_by_parent_data) {
1235617f3a96SKevin Wolf .detach_on_drain = false,
1236617f3a96SKevin Wolf };
1237617f3a96SKevin Wolf
1238231281abSKevin Wolf /* Create all involved nodes */
1239231281abSKevin Wolf parent_a = bdrv_new_open_driver(&bdrv_test, "parent-a", BDRV_O_RDWR,
1240231281abSKevin Wolf &error_abort);
1241231281abSKevin Wolf parent_b = bdrv_new_open_driver(&bdrv_test, "parent-b", 0,
1242231281abSKevin Wolf &error_abort);
1243231281abSKevin Wolf
1244231281abSKevin Wolf a = bdrv_new_open_driver(&bdrv_test, "a", BDRV_O_RDWR, &error_abort);
1245231281abSKevin Wolf b = bdrv_new_open_driver(&bdrv_test, "b", BDRV_O_RDWR, &error_abort);
1246231281abSKevin Wolf c = bdrv_new_open_driver(&bdrv_test, "c", BDRV_O_RDWR, &error_abort);
1247231281abSKevin Wolf
1248231281abSKevin Wolf /* blk is a BB for parent-a */
1249d861ab3aSKevin Wolf blk = blk_new(qemu_get_aio_context(), BLK_PERM_ALL, BLK_PERM_ALL);
1250231281abSKevin Wolf blk_insert_bs(blk, parent_a, &error_abort);
1251231281abSKevin Wolf bdrv_unref(parent_a);
1252231281abSKevin Wolf
125357320ca9SKevin Wolf /* If we want to get bdrv_drain_invoke() to call aio_poll(), the driver
125457320ca9SKevin Wolf * callback must not return immediately. */
125557320ca9SKevin Wolf if (!by_parent_cb) {
125657320ca9SKevin Wolf BDRVTestState *s = parent_a->opaque;
125757320ca9SKevin Wolf s->sleep_in_drain_begin = true;
125857320ca9SKevin Wolf }
125957320ca9SKevin Wolf
1260231281abSKevin Wolf /* Set child relationships */
1261231281abSKevin Wolf bdrv_ref(b);
1262231281abSKevin Wolf bdrv_ref(a);
12636bc30f19SStefan Hajnoczi bdrv_graph_wrlock();
1264a16be3cdSMax Reitz child_b = bdrv_attach_child(parent_b, b, "PB-B", &child_of_bds,
1265a16be3cdSMax Reitz BDRV_CHILD_DATA, &error_abort);
126625191e5fSMax Reitz child_a = bdrv_attach_child(parent_b, a, "PB-A", &child_of_bds,
126725191e5fSMax Reitz BDRV_CHILD_COW, &error_abort);
1268231281abSKevin Wolf
1269231281abSKevin Wolf bdrv_ref(a);
127057320ca9SKevin Wolf bdrv_attach_child(parent_a, a, "PA-A",
1271a16be3cdSMax Reitz by_parent_cb ? &child_of_bds : &detach_by_driver_cb_class,
1272a16be3cdSMax Reitz BDRV_CHILD_DATA, &error_abort);
12736bc30f19SStefan Hajnoczi bdrv_graph_wrunlock();
1274231281abSKevin Wolf
1275231281abSKevin Wolf g_assert_cmpint(parent_a->refcnt, ==, 1);
1276231281abSKevin Wolf g_assert_cmpint(parent_b->refcnt, ==, 1);
1277231281abSKevin Wolf g_assert_cmpint(a->refcnt, ==, 3);
1278231281abSKevin Wolf g_assert_cmpint(b->refcnt, ==, 2);
1279231281abSKevin Wolf g_assert_cmpint(c->refcnt, ==, 1);
1280231281abSKevin Wolf
1281231281abSKevin Wolf g_assert(QLIST_FIRST(&parent_b->children) == child_a);
1282231281abSKevin Wolf g_assert(QLIST_NEXT(child_a, next) == child_b);
1283231281abSKevin Wolf g_assert(QLIST_NEXT(child_b, next) == NULL);
1284231281abSKevin Wolf
1285231281abSKevin Wolf /* Start the evil write request */
128657320ca9SKevin Wolf detach_by_parent_data = (struct detach_by_parent_data) {
1287231281abSKevin Wolf .parent_b = parent_b,
1288231281abSKevin Wolf .child_b = child_b,
1289231281abSKevin Wolf .c = c,
129057320ca9SKevin Wolf .by_parent_cb = by_parent_cb,
1291617f3a96SKevin Wolf .detach_on_drain = true,
1292231281abSKevin Wolf };
129357320ca9SKevin Wolf acb = blk_aio_preadv(blk, 0, &qiov, 0, detach_by_parent_aio_cb, NULL);
1294231281abSKevin Wolf g_assert(acb != NULL);
1295231281abSKevin Wolf
1296231281abSKevin Wolf /* Drain and check the expected result */
1297299403aeSKevin Wolf bdrv_drained_begin(parent_b);
1298299403aeSKevin Wolf bdrv_drained_begin(a);
1299299403aeSKevin Wolf bdrv_drained_begin(b);
1300299403aeSKevin Wolf bdrv_drained_begin(c);
1301231281abSKevin Wolf
130257320ca9SKevin Wolf g_assert(detach_by_parent_data.child_c != NULL);
1303231281abSKevin Wolf
1304231281abSKevin Wolf g_assert_cmpint(parent_a->refcnt, ==, 1);
1305231281abSKevin Wolf g_assert_cmpint(parent_b->refcnt, ==, 1);
1306231281abSKevin Wolf g_assert_cmpint(a->refcnt, ==, 3);
1307231281abSKevin Wolf g_assert_cmpint(b->refcnt, ==, 1);
1308231281abSKevin Wolf g_assert_cmpint(c->refcnt, ==, 2);
1309231281abSKevin Wolf
131057320ca9SKevin Wolf g_assert(QLIST_FIRST(&parent_b->children) == detach_by_parent_data.child_c);
131157320ca9SKevin Wolf g_assert(QLIST_NEXT(detach_by_parent_data.child_c, next) == child_a);
1312231281abSKevin Wolf g_assert(QLIST_NEXT(child_a, next) == NULL);
1313231281abSKevin Wolf
1314231281abSKevin Wolf g_assert_cmpint(parent_a->quiesce_counter, ==, 1);
1315299403aeSKevin Wolf g_assert_cmpint(parent_b->quiesce_counter, ==, 3);
1316231281abSKevin Wolf g_assert_cmpint(a->quiesce_counter, ==, 1);
1317299403aeSKevin Wolf g_assert_cmpint(b->quiesce_counter, ==, 1);
1318231281abSKevin Wolf g_assert_cmpint(c->quiesce_counter, ==, 1);
1319231281abSKevin Wolf
1320299403aeSKevin Wolf bdrv_drained_end(parent_b);
1321299403aeSKevin Wolf bdrv_drained_end(a);
1322299403aeSKevin Wolf bdrv_drained_end(b);
1323299403aeSKevin Wolf bdrv_drained_end(c);
1324231281abSKevin Wolf
1325231281abSKevin Wolf bdrv_unref(parent_b);
1326231281abSKevin Wolf blk_unref(blk);
1327231281abSKevin Wolf
1328231281abSKevin Wolf g_assert_cmpint(a->refcnt, ==, 1);
1329231281abSKevin Wolf g_assert_cmpint(b->refcnt, ==, 1);
1330231281abSKevin Wolf g_assert_cmpint(c->refcnt, ==, 1);
1331231281abSKevin Wolf bdrv_unref(a);
1332231281abSKevin Wolf bdrv_unref(b);
1333231281abSKevin Wolf bdrv_unref(c);
1334231281abSKevin Wolf }
1335231281abSKevin Wolf
test_detach_by_parent_cb(void)133657320ca9SKevin Wolf static void test_detach_by_parent_cb(void)
133757320ca9SKevin Wolf {
133857320ca9SKevin Wolf test_detach_indirect(true);
133957320ca9SKevin Wolf }
134057320ca9SKevin Wolf
test_detach_by_driver_cb(void)134157320ca9SKevin Wolf static void test_detach_by_driver_cb(void)
134257320ca9SKevin Wolf {
134357320ca9SKevin Wolf test_detach_indirect(false);
134457320ca9SKevin Wolf }
1345231281abSKevin Wolf
test_append_to_drained(void)1346b994c5bcSKevin Wolf static void test_append_to_drained(void)
1347b994c5bcSKevin Wolf {
1348b994c5bcSKevin Wolf BlockBackend *blk;
1349b994c5bcSKevin Wolf BlockDriverState *base, *overlay;
1350b994c5bcSKevin Wolf BDRVTestState *base_s, *overlay_s;
1351b994c5bcSKevin Wolf
1352d861ab3aSKevin Wolf blk = blk_new(qemu_get_aio_context(), BLK_PERM_ALL, BLK_PERM_ALL);
1353b994c5bcSKevin Wolf base = bdrv_new_open_driver(&bdrv_test, "base", BDRV_O_RDWR, &error_abort);
1354b994c5bcSKevin Wolf base_s = base->opaque;
1355b994c5bcSKevin Wolf blk_insert_bs(blk, base, &error_abort);
1356b994c5bcSKevin Wolf
1357b994c5bcSKevin Wolf overlay = bdrv_new_open_driver(&bdrv_test, "overlay", BDRV_O_RDWR,
1358b994c5bcSKevin Wolf &error_abort);
1359b994c5bcSKevin Wolf overlay_s = overlay->opaque;
1360b994c5bcSKevin Wolf
1361b994c5bcSKevin Wolf do_drain_begin(BDRV_DRAIN, base);
1362b994c5bcSKevin Wolf g_assert_cmpint(base->quiesce_counter, ==, 1);
1363b994c5bcSKevin Wolf g_assert_cmpint(base_s->drain_count, ==, 1);
1364b994c5bcSKevin Wolf g_assert_cmpint(base->in_flight, ==, 0);
1365b994c5bcSKevin Wolf
1366b994c5bcSKevin Wolf bdrv_append(overlay, base, &error_abort);
1367487b9187SKevin Wolf
1368b994c5bcSKevin Wolf g_assert_cmpint(base->in_flight, ==, 0);
1369b994c5bcSKevin Wolf g_assert_cmpint(overlay->in_flight, ==, 0);
1370b994c5bcSKevin Wolf
1371b994c5bcSKevin Wolf g_assert_cmpint(base->quiesce_counter, ==, 1);
1372b994c5bcSKevin Wolf g_assert_cmpint(base_s->drain_count, ==, 1);
1373b994c5bcSKevin Wolf g_assert_cmpint(overlay->quiesce_counter, ==, 1);
1374b994c5bcSKevin Wolf g_assert_cmpint(overlay_s->drain_count, ==, 1);
1375b994c5bcSKevin Wolf
1376b994c5bcSKevin Wolf do_drain_end(BDRV_DRAIN, base);
1377b994c5bcSKevin Wolf
1378b994c5bcSKevin Wolf g_assert_cmpint(base->quiesce_counter, ==, 0);
1379b994c5bcSKevin Wolf g_assert_cmpint(base_s->drain_count, ==, 0);
1380b994c5bcSKevin Wolf g_assert_cmpint(overlay->quiesce_counter, ==, 0);
1381b994c5bcSKevin Wolf g_assert_cmpint(overlay_s->drain_count, ==, 0);
1382b994c5bcSKevin Wolf
1383ae9d4417SVladimir Sementsov-Ogievskiy bdrv_unref(overlay);
1384b994c5bcSKevin Wolf bdrv_unref(base);
1385b994c5bcSKevin Wolf blk_unref(blk);
1386b994c5bcSKevin Wolf }
1387b994c5bcSKevin Wolf
test_set_aio_context(void)1388247d2737SKevin Wolf static void test_set_aio_context(void)
1389247d2737SKevin Wolf {
1390247d2737SKevin Wolf BlockDriverState *bs;
1391247d2737SKevin Wolf IOThread *a = iothread_new();
1392247d2737SKevin Wolf IOThread *b = iothread_new();
1393247d2737SKevin Wolf AioContext *ctx_a = iothread_get_aio_context(a);
1394247d2737SKevin Wolf AioContext *ctx_b = iothread_get_aio_context(b);
1395247d2737SKevin Wolf
1396247d2737SKevin Wolf bs = bdrv_new_open_driver(&bdrv_test, "test-node", BDRV_O_RDWR,
1397247d2737SKevin Wolf &error_abort);
1398247d2737SKevin Wolf
1399247d2737SKevin Wolf bdrv_drained_begin(bs);
1400142e6907SEmanuele Giuseppe Esposito bdrv_try_change_aio_context(bs, ctx_a, NULL, &error_abort);
1401247d2737SKevin Wolf bdrv_drained_end(bs);
1402247d2737SKevin Wolf
1403247d2737SKevin Wolf bdrv_drained_begin(bs);
1404142e6907SEmanuele Giuseppe Esposito bdrv_try_change_aio_context(bs, ctx_b, NULL, &error_abort);
1405142e6907SEmanuele Giuseppe Esposito bdrv_try_change_aio_context(bs, qemu_get_aio_context(), NULL, &error_abort);
1406247d2737SKevin Wolf bdrv_drained_end(bs);
1407247d2737SKevin Wolf
1408247d2737SKevin Wolf bdrv_unref(bs);
1409247d2737SKevin Wolf iothread_join(a);
1410247d2737SKevin Wolf iothread_join(b);
1411247d2737SKevin Wolf }
1412247d2737SKevin Wolf
14138e442810SMax Reitz
14148e442810SMax Reitz typedef struct TestDropBackingBlockJob {
14158e442810SMax Reitz BlockJob common;
14168e442810SMax Reitz bool *did_complete;
14172afdc790SMax Reitz BlockDriverState *detach_also;
14181b177bbeSVladimir Sementsov-Ogievskiy BlockDriverState *bs;
1419*f8222bfbSVitalii Mordan
1420*f8222bfbSVitalii Mordan /* Accessed with atomics */
1421*f8222bfbSVitalii Mordan bool should_complete;
14228e442810SMax Reitz } TestDropBackingBlockJob;
14238e442810SMax Reitz
test_drop_backing_job_run(Job * job,Error ** errp)14248e442810SMax Reitz static int coroutine_fn test_drop_backing_job_run(Job *job, Error **errp)
14258e442810SMax Reitz {
14268e442810SMax Reitz TestDropBackingBlockJob *s =
14278e442810SMax Reitz container_of(job, TestDropBackingBlockJob, common.job);
14288e442810SMax Reitz
1429*f8222bfbSVitalii Mordan while (!qatomic_read(&s->should_complete)) {
14308e442810SMax Reitz job_sleep_ns(job, 0);
14318e442810SMax Reitz }
14328e442810SMax Reitz
14338e442810SMax Reitz return 0;
14348e442810SMax Reitz }
14358e442810SMax Reitz
test_drop_backing_job_commit(Job * job)14368e442810SMax Reitz static void test_drop_backing_job_commit(Job *job)
14378e442810SMax Reitz {
14388e442810SMax Reitz TestDropBackingBlockJob *s =
14398e442810SMax Reitz container_of(job, TestDropBackingBlockJob, common.job);
14408e442810SMax Reitz
14411b177bbeSVladimir Sementsov-Ogievskiy bdrv_set_backing_hd(s->bs, NULL, &error_abort);
14422afdc790SMax Reitz bdrv_set_backing_hd(s->detach_also, NULL, &error_abort);
14438e442810SMax Reitz
14448e442810SMax Reitz *s->did_complete = true;
14458e442810SMax Reitz }
14468e442810SMax Reitz
14478e442810SMax Reitz static const BlockJobDriver test_drop_backing_job_driver = {
14488e442810SMax Reitz .job_driver = {
14498e442810SMax Reitz .instance_size = sizeof(TestDropBackingBlockJob),
14508e442810SMax Reitz .free = block_job_free,
14518e442810SMax Reitz .user_resume = block_job_user_resume,
14528e442810SMax Reitz .run = test_drop_backing_job_run,
14538e442810SMax Reitz .commit = test_drop_backing_job_commit,
14548e442810SMax Reitz }
14558e442810SMax Reitz };
14568e442810SMax Reitz
14578e442810SMax Reitz /**
14588e442810SMax Reitz * Creates a child node with three parent nodes on it, and then runs a
14598e442810SMax Reitz * block job on the final one, parent-node-2.
14608e442810SMax Reitz *
14618e442810SMax Reitz * The job is then asked to complete before a section where the child
14628e442810SMax Reitz * is drained.
14638e442810SMax Reitz *
14648e442810SMax Reitz * Ending this section will undrain the child's parents, first
14658e442810SMax Reitz * parent-node-2, then parent-node-1, then parent-node-0 -- the parent
14668e442810SMax Reitz * list is in reverse order of how they were added. Ending the drain
14678e442810SMax Reitz * on parent-node-2 will resume the job, thus completing it and
14688e442810SMax Reitz * scheduling job_exit().
14698e442810SMax Reitz *
14708e442810SMax Reitz * Ending the drain on parent-node-1 will poll the AioContext, which
14718e442810SMax Reitz * lets job_exit() and thus test_drop_backing_job_commit() run. That
14722afdc790SMax Reitz * function first removes the child as parent-node-2's backing file.
14738e442810SMax Reitz *
14748e442810SMax Reitz * In old (and buggy) implementations, there are two problems with
14758e442810SMax Reitz * that:
14768e442810SMax Reitz * (A) bdrv_drain_invoke() polls for every node that leaves the
14778e442810SMax Reitz * drained section. This means that job_exit() is scheduled
14788e442810SMax Reitz * before the child has left the drained section. Its
14798e442810SMax Reitz * quiesce_counter is therefore still 1 when it is removed from
14808e442810SMax Reitz * parent-node-2.
14818e442810SMax Reitz *
14828e442810SMax Reitz * (B) bdrv_replace_child_noperm() calls drained_end() on the old
14838e442810SMax Reitz * child's parents as many times as the child is quiesced. This
14848e442810SMax Reitz * means it will call drained_end() on parent-node-2 once.
14858e442810SMax Reitz * Because parent-node-2 is no longer quiesced at this point, this
14868e442810SMax Reitz * will fail.
14878e442810SMax Reitz *
14888e442810SMax Reitz * bdrv_replace_child_noperm() therefore must call drained_end() on
14898e442810SMax Reitz * the parent only if it really is still drained because the child is
14908e442810SMax Reitz * drained.
14912afdc790SMax Reitz *
14922afdc790SMax Reitz * If removing child from parent-node-2 was successful (as it should
14932afdc790SMax Reitz * be), test_drop_backing_job_commit() will then also remove the child
14942afdc790SMax Reitz * from parent-node-0.
14952afdc790SMax Reitz *
14962afdc790SMax Reitz * With an old version of our drain infrastructure ((A) above), that
14972afdc790SMax Reitz * resulted in the following flow:
14982afdc790SMax Reitz *
14992afdc790SMax Reitz * 1. child attempts to leave its drained section. The call recurses
15002afdc790SMax Reitz * to its parents.
15012afdc790SMax Reitz *
15022afdc790SMax Reitz * 2. parent-node-2 leaves the drained section. Polling in
15032afdc790SMax Reitz * bdrv_drain_invoke() will schedule job_exit().
15042afdc790SMax Reitz *
15052afdc790SMax Reitz * 3. parent-node-1 leaves the drained section. Polling in
15062afdc790SMax Reitz * bdrv_drain_invoke() will run job_exit(), thus disconnecting
15072afdc790SMax Reitz * parent-node-0 from the child node.
15082afdc790SMax Reitz *
15092afdc790SMax Reitz * 4. bdrv_parent_drained_end() uses a QLIST_FOREACH_SAFE() loop to
15102afdc790SMax Reitz * iterate over the parents. Thus, it now accesses the BdrvChild
15112afdc790SMax Reitz * object that used to connect parent-node-0 and the child node.
15122afdc790SMax Reitz * However, that object no longer exists, so it accesses a dangling
15132afdc790SMax Reitz * pointer.
15142afdc790SMax Reitz *
15152afdc790SMax Reitz * The solution is to only poll once when running a bdrv_drained_end()
15162afdc790SMax Reitz * operation, specifically at the end when all drained_end()
15172afdc790SMax Reitz * operations for all involved nodes have been scheduled.
15182afdc790SMax Reitz * Note that this also solves (A) above, thus hiding (B).
15198e442810SMax Reitz */
test_blockjob_commit_by_drained_end(void)15208e442810SMax Reitz static void test_blockjob_commit_by_drained_end(void)
15218e442810SMax Reitz {
15228e442810SMax Reitz BlockDriverState *bs_child, *bs_parents[3];
15238e442810SMax Reitz TestDropBackingBlockJob *job;
15248e442810SMax Reitz bool job_has_completed = false;
15258e442810SMax Reitz int i;
15268e442810SMax Reitz
15278e442810SMax Reitz bs_child = bdrv_new_open_driver(&bdrv_test, "child-node", BDRV_O_RDWR,
15288e442810SMax Reitz &error_abort);
15298e442810SMax Reitz
15308e442810SMax Reitz for (i = 0; i < 3; i++) {
15318e442810SMax Reitz char name[32];
15328e442810SMax Reitz snprintf(name, sizeof(name), "parent-node-%i", i);
15338e442810SMax Reitz bs_parents[i] = bdrv_new_open_driver(&bdrv_test, name, BDRV_O_RDWR,
15348e442810SMax Reitz &error_abort);
15358e442810SMax Reitz bdrv_set_backing_hd(bs_parents[i], bs_child, &error_abort);
15368e442810SMax Reitz }
15378e442810SMax Reitz
15388e442810SMax Reitz job = block_job_create("job", &test_drop_backing_job_driver, NULL,
15398e442810SMax Reitz bs_parents[2], 0, BLK_PERM_ALL, 0, 0, NULL, NULL,
15408e442810SMax Reitz &error_abort);
15411b177bbeSVladimir Sementsov-Ogievskiy job->bs = bs_parents[2];
15428e442810SMax Reitz
15432afdc790SMax Reitz job->detach_also = bs_parents[0];
15448e442810SMax Reitz job->did_complete = &job_has_completed;
15458e442810SMax Reitz
15468e442810SMax Reitz job_start(&job->common.job);
15478e442810SMax Reitz
1548*f8222bfbSVitalii Mordan qatomic_set(&job->should_complete, true);
15498e442810SMax Reitz bdrv_drained_begin(bs_child);
15508e442810SMax Reitz g_assert(!job_has_completed);
15518e442810SMax Reitz bdrv_drained_end(bs_child);
15525e8ac217SKevin Wolf aio_poll(qemu_get_aio_context(), false);
15538e442810SMax Reitz g_assert(job_has_completed);
15548e442810SMax Reitz
15558e442810SMax Reitz bdrv_unref(bs_parents[0]);
15568e442810SMax Reitz bdrv_unref(bs_parents[1]);
15578e442810SMax Reitz bdrv_unref(bs_parents[2]);
15588e442810SMax Reitz bdrv_unref(bs_child);
15598e442810SMax Reitz }
15608e442810SMax Reitz
15619746b35cSMax Reitz
15629746b35cSMax Reitz typedef struct TestSimpleBlockJob {
15639746b35cSMax Reitz BlockJob common;
15649746b35cSMax Reitz bool *did_complete;
1565*f8222bfbSVitalii Mordan
1566*f8222bfbSVitalii Mordan /* Accessed with atomics */
1567*f8222bfbSVitalii Mordan bool should_complete;
15689746b35cSMax Reitz } TestSimpleBlockJob;
15699746b35cSMax Reitz
test_simple_job_run(Job * job,Error ** errp)15709746b35cSMax Reitz static int coroutine_fn test_simple_job_run(Job *job, Error **errp)
15719746b35cSMax Reitz {
15729746b35cSMax Reitz TestSimpleBlockJob *s = container_of(job, TestSimpleBlockJob, common.job);
15739746b35cSMax Reitz
1574*f8222bfbSVitalii Mordan while (!qatomic_read(&s->should_complete)) {
15759746b35cSMax Reitz job_sleep_ns(job, 0);
15769746b35cSMax Reitz }
15779746b35cSMax Reitz
15789746b35cSMax Reitz return 0;
15799746b35cSMax Reitz }
15809746b35cSMax Reitz
test_simple_job_clean(Job * job)15819746b35cSMax Reitz static void test_simple_job_clean(Job *job)
15829746b35cSMax Reitz {
15839746b35cSMax Reitz TestSimpleBlockJob *s = container_of(job, TestSimpleBlockJob, common.job);
15849746b35cSMax Reitz *s->did_complete = true;
15859746b35cSMax Reitz }
15869746b35cSMax Reitz
15879746b35cSMax Reitz static const BlockJobDriver test_simple_job_driver = {
15889746b35cSMax Reitz .job_driver = {
15899746b35cSMax Reitz .instance_size = sizeof(TestSimpleBlockJob),
15909746b35cSMax Reitz .free = block_job_free,
15919746b35cSMax Reitz .user_resume = block_job_user_resume,
15929746b35cSMax Reitz .run = test_simple_job_run,
15939746b35cSMax Reitz .clean = test_simple_job_clean,
15949746b35cSMax Reitz },
15959746b35cSMax Reitz };
15969746b35cSMax Reitz
drop_intermediate_poll_update_filename(BdrvChild * child,BlockDriverState * new_base,const char * filename,bool backing_mask_protocol,Error ** errp)15979746b35cSMax Reitz static int drop_intermediate_poll_update_filename(BdrvChild *child,
15989746b35cSMax Reitz BlockDriverState *new_base,
15999746b35cSMax Reitz const char *filename,
16004b028cbeSPeter Krempa bool backing_mask_protocol,
16019746b35cSMax Reitz Error **errp)
16029746b35cSMax Reitz {
16039746b35cSMax Reitz /*
16049746b35cSMax Reitz * We are free to poll here, which may change the block graph, if
16059746b35cSMax Reitz * it is not drained.
16069746b35cSMax Reitz */
16079746b35cSMax Reitz
16089746b35cSMax Reitz /* If the job is not drained: Complete it, schedule job_exit() */
16099746b35cSMax Reitz aio_poll(qemu_get_current_aio_context(), false);
16109746b35cSMax Reitz /* If the job is not drained: Run job_exit(), finish the job */
16119746b35cSMax Reitz aio_poll(qemu_get_current_aio_context(), false);
16129746b35cSMax Reitz
16139746b35cSMax Reitz return 0;
16149746b35cSMax Reitz }
16159746b35cSMax Reitz
16169746b35cSMax Reitz /**
16179746b35cSMax Reitz * Test a poll in the midst of bdrv_drop_intermediate().
16189746b35cSMax Reitz *
1619bd86fb99SMax Reitz * bdrv_drop_intermediate() calls BdrvChildClass.update_filename(),
16209746b35cSMax Reitz * which can yield or poll. This may lead to graph changes, unless
16219746b35cSMax Reitz * the whole subtree in question is drained.
16229746b35cSMax Reitz *
16239746b35cSMax Reitz * We test this on the following graph:
16249746b35cSMax Reitz *
16259746b35cSMax Reitz * Job
16269746b35cSMax Reitz *
16279746b35cSMax Reitz * |
16289746b35cSMax Reitz * job-node
16299746b35cSMax Reitz * |
16309746b35cSMax Reitz * v
16319746b35cSMax Reitz *
16329746b35cSMax Reitz * job-node
16339746b35cSMax Reitz *
16349746b35cSMax Reitz * |
16359746b35cSMax Reitz * backing
16369746b35cSMax Reitz * |
16379746b35cSMax Reitz * v
16389746b35cSMax Reitz *
16399746b35cSMax Reitz * node-2 --chain--> node-1 --chain--> node-0
16409746b35cSMax Reitz *
16419746b35cSMax Reitz * We drop node-1 with bdrv_drop_intermediate(top=node-1, base=node-0).
16429746b35cSMax Reitz *
16439746b35cSMax Reitz * This first updates node-2's backing filename by invoking
16449746b35cSMax Reitz * drop_intermediate_poll_update_filename(), which polls twice. This
16459746b35cSMax Reitz * causes the job to finish, which in turns causes the job-node to be
16469746b35cSMax Reitz * deleted.
16479746b35cSMax Reitz *
16489746b35cSMax Reitz * bdrv_drop_intermediate() uses a QLIST_FOREACH_SAFE() loop, so it
16499746b35cSMax Reitz * already has a pointer to the BdrvChild edge between job-node and
16509746b35cSMax Reitz * node-1. When it tries to handle that edge, we probably get a
16519746b35cSMax Reitz * segmentation fault because the object no longer exists.
16529746b35cSMax Reitz *
16539746b35cSMax Reitz *
16549746b35cSMax Reitz * The solution is for bdrv_drop_intermediate() to drain top's
16559746b35cSMax Reitz * subtree. This prevents graph changes from happening just because
1656bd86fb99SMax Reitz * BdrvChildClass.update_filename() yields or polls. Thus, the block
16579746b35cSMax Reitz * job is paused during that drained section and must finish before or
16589746b35cSMax Reitz * after.
16599746b35cSMax Reitz *
16609746b35cSMax Reitz * (In addition, bdrv_replace_child() must keep the job paused.)
16619746b35cSMax Reitz */
test_drop_intermediate_poll(void)16629746b35cSMax Reitz static void test_drop_intermediate_poll(void)
16639746b35cSMax Reitz {
1664bd86fb99SMax Reitz static BdrvChildClass chain_child_class;
16659746b35cSMax Reitz BlockDriverState *chain[3];
16669746b35cSMax Reitz TestSimpleBlockJob *job;
16679746b35cSMax Reitz BlockDriverState *job_node;
16689746b35cSMax Reitz bool job_has_completed = false;
16699746b35cSMax Reitz int i;
16709746b35cSMax Reitz int ret;
16719746b35cSMax Reitz
167225191e5fSMax Reitz chain_child_class = child_of_bds;
1673bd86fb99SMax Reitz chain_child_class.update_filename = drop_intermediate_poll_update_filename;
16749746b35cSMax Reitz
16759746b35cSMax Reitz for (i = 0; i < 3; i++) {
16769746b35cSMax Reitz char name[32];
16779746b35cSMax Reitz snprintf(name, 32, "node-%i", i);
16789746b35cSMax Reitz
16799746b35cSMax Reitz chain[i] = bdrv_new_open_driver(&bdrv_test, name, 0, &error_abort);
16809746b35cSMax Reitz }
16819746b35cSMax Reitz
16829746b35cSMax Reitz job_node = bdrv_new_open_driver(&bdrv_test, "job-node", BDRV_O_RDWR,
16839746b35cSMax Reitz &error_abort);
16849746b35cSMax Reitz bdrv_set_backing_hd(job_node, chain[1], &error_abort);
16859746b35cSMax Reitz
16869746b35cSMax Reitz /*
16879746b35cSMax Reitz * Establish the chain last, so the chain links are the first
16889746b35cSMax Reitz * elements in the BDS.parents lists
16899746b35cSMax Reitz */
16906bc30f19SStefan Hajnoczi bdrv_graph_wrlock();
16919746b35cSMax Reitz for (i = 0; i < 3; i++) {
16929746b35cSMax Reitz if (i) {
16939746b35cSMax Reitz /* Takes the reference to chain[i - 1] */
16945bb04747SVladimir Sementsov-Ogievskiy bdrv_attach_child(chain[i], chain[i - 1], "chain",
16955bb04747SVladimir Sementsov-Ogievskiy &chain_child_class, BDRV_CHILD_COW, &error_abort);
16969746b35cSMax Reitz }
16979746b35cSMax Reitz }
16986bc30f19SStefan Hajnoczi bdrv_graph_wrunlock();
16999746b35cSMax Reitz
17009746b35cSMax Reitz job = block_job_create("job", &test_simple_job_driver, NULL, job_node,
17019746b35cSMax Reitz 0, BLK_PERM_ALL, 0, 0, NULL, NULL, &error_abort);
17029746b35cSMax Reitz
17039746b35cSMax Reitz /* The job has a reference now */
17049746b35cSMax Reitz bdrv_unref(job_node);
17059746b35cSMax Reitz
17069746b35cSMax Reitz job->did_complete = &job_has_completed;
17079746b35cSMax Reitz
17089746b35cSMax Reitz job_start(&job->common.job);
1709*f8222bfbSVitalii Mordan qatomic_set(&job->should_complete, true);
17109746b35cSMax Reitz
17119746b35cSMax Reitz g_assert(!job_has_completed);
17124b028cbeSPeter Krempa ret = bdrv_drop_intermediate(chain[1], chain[0], NULL, false);
17135e8ac217SKevin Wolf aio_poll(qemu_get_aio_context(), false);
17149746b35cSMax Reitz g_assert(ret == 0);
17159746b35cSMax Reitz g_assert(job_has_completed);
17169746b35cSMax Reitz
17179746b35cSMax Reitz bdrv_unref(chain[2]);
17189746b35cSMax Reitz }
17199746b35cSMax Reitz
17200513f984SMax Reitz
17210513f984SMax Reitz typedef struct BDRVReplaceTestState {
172223987471SKevin Wolf bool setup_completed;
17230513f984SMax Reitz bool was_drained;
17240513f984SMax Reitz bool was_undrained;
17250513f984SMax Reitz bool has_read;
17260513f984SMax Reitz
17270513f984SMax Reitz int drain_count;
17280513f984SMax Reitz
17290513f984SMax Reitz bool yield_before_read;
17300513f984SMax Reitz Coroutine *io_co;
17310513f984SMax Reitz Coroutine *drain_co;
17320513f984SMax Reitz } BDRVReplaceTestState;
17330513f984SMax Reitz
bdrv_replace_test_close(BlockDriverState * bs)17340513f984SMax Reitz static void bdrv_replace_test_close(BlockDriverState *bs)
17350513f984SMax Reitz {
17360513f984SMax Reitz }
17370513f984SMax Reitz
17380513f984SMax Reitz /**
17390513f984SMax Reitz * If @bs has a backing file:
17400513f984SMax Reitz * Yield if .yield_before_read is true (and wait for drain_begin to
17410513f984SMax Reitz * wake us up).
17420513f984SMax Reitz * Forward the read to bs->backing. Set .has_read to true.
17430513f984SMax Reitz * If drain_begin has woken us, wake it in turn.
17440513f984SMax Reitz *
17450513f984SMax Reitz * Otherwise:
17460513f984SMax Reitz * Set .has_read to true and return success.
17470513f984SMax Reitz */
1748b9b10c35SKevin Wolf static int coroutine_fn GRAPH_RDLOCK
bdrv_replace_test_co_preadv(BlockDriverState * bs,int64_t offset,int64_t bytes,QEMUIOVector * qiov,BdrvRequestFlags flags)1749b9b10c35SKevin Wolf bdrv_replace_test_co_preadv(BlockDriverState *bs, int64_t offset, int64_t bytes,
1750b9b10c35SKevin Wolf QEMUIOVector *qiov, BdrvRequestFlags flags)
17510513f984SMax Reitz {
17520513f984SMax Reitz BDRVReplaceTestState *s = bs->opaque;
17530513f984SMax Reitz
17540513f984SMax Reitz if (bs->backing) {
17550513f984SMax Reitz int ret;
17560513f984SMax Reitz
17570513f984SMax Reitz g_assert(!s->drain_count);
17580513f984SMax Reitz
17590513f984SMax Reitz s->io_co = qemu_coroutine_self();
17600513f984SMax Reitz if (s->yield_before_read) {
17610513f984SMax Reitz s->yield_before_read = false;
17620513f984SMax Reitz qemu_coroutine_yield();
17630513f984SMax Reitz }
17640513f984SMax Reitz s->io_co = NULL;
17650513f984SMax Reitz
1766fae2681aSVladimir Sementsov-Ogievskiy ret = bdrv_co_preadv(bs->backing, offset, bytes, qiov, 0);
17670513f984SMax Reitz s->has_read = true;
17680513f984SMax Reitz
17690513f984SMax Reitz /* Wake up drain_co if it runs */
17700513f984SMax Reitz if (s->drain_co) {
17710513f984SMax Reitz aio_co_wake(s->drain_co);
17720513f984SMax Reitz }
17730513f984SMax Reitz
17740513f984SMax Reitz return ret;
17750513f984SMax Reitz }
17760513f984SMax Reitz
17770513f984SMax Reitz s->has_read = true;
17780513f984SMax Reitz return 0;
17790513f984SMax Reitz }
17800513f984SMax Reitz
bdrv_replace_test_drain_co(void * opaque)17817bce1c29SKevin Wolf static void coroutine_fn bdrv_replace_test_drain_co(void *opaque)
17827bce1c29SKevin Wolf {
17837bce1c29SKevin Wolf BlockDriverState *bs = opaque;
17847bce1c29SKevin Wolf BDRVReplaceTestState *s = bs->opaque;
17857bce1c29SKevin Wolf
17867bce1c29SKevin Wolf /* Keep waking io_co up until it is done */
17877bce1c29SKevin Wolf while (s->io_co) {
17887bce1c29SKevin Wolf aio_co_wake(s->io_co);
17897bce1c29SKevin Wolf s->io_co = NULL;
17907bce1c29SKevin Wolf qemu_coroutine_yield();
17917bce1c29SKevin Wolf }
17927bce1c29SKevin Wolf s->drain_co = NULL;
17937bce1c29SKevin Wolf bdrv_dec_in_flight(bs);
17947bce1c29SKevin Wolf }
17957bce1c29SKevin Wolf
17960513f984SMax Reitz /**
17970513f984SMax Reitz * If .drain_count is 0, wake up .io_co if there is one; and set
17980513f984SMax Reitz * .was_drained.
17990513f984SMax Reitz * Increment .drain_count.
18000513f984SMax Reitz */
bdrv_replace_test_drain_begin(BlockDriverState * bs)18015e8ac217SKevin Wolf static void bdrv_replace_test_drain_begin(BlockDriverState *bs)
18020513f984SMax Reitz {
18030513f984SMax Reitz BDRVReplaceTestState *s = bs->opaque;
18040513f984SMax Reitz
180523987471SKevin Wolf if (!s->setup_completed) {
180623987471SKevin Wolf return;
180723987471SKevin Wolf }
180823987471SKevin Wolf
18090513f984SMax Reitz if (!s->drain_count) {
18107bce1c29SKevin Wolf s->drain_co = qemu_coroutine_create(bdrv_replace_test_drain_co, bs);
18117bce1c29SKevin Wolf bdrv_inc_in_flight(bs);
18127bce1c29SKevin Wolf aio_co_enter(bdrv_get_aio_context(bs), s->drain_co);
18130513f984SMax Reitz s->was_drained = true;
18140513f984SMax Reitz }
18150513f984SMax Reitz s->drain_count++;
18160513f984SMax Reitz }
18170513f984SMax Reitz
bdrv_replace_test_read_entry(void * opaque)18187bce1c29SKevin Wolf static void coroutine_fn bdrv_replace_test_read_entry(void *opaque)
18197bce1c29SKevin Wolf {
18207bce1c29SKevin Wolf BlockDriverState *bs = opaque;
18217bce1c29SKevin Wolf char data;
18227bce1c29SKevin Wolf QEMUIOVector qiov = QEMU_IOVEC_INIT_BUF(qiov, &data, 1);
18237bce1c29SKevin Wolf int ret;
18247bce1c29SKevin Wolf
18257bce1c29SKevin Wolf /* Queue a read request post-drain */
1826b9b10c35SKevin Wolf bdrv_graph_co_rdlock();
18277bce1c29SKevin Wolf ret = bdrv_replace_test_co_preadv(bs, 0, 1, &qiov, 0);
1828b9b10c35SKevin Wolf bdrv_graph_co_rdunlock();
1829b9b10c35SKevin Wolf
18307bce1c29SKevin Wolf g_assert(ret >= 0);
18317bce1c29SKevin Wolf bdrv_dec_in_flight(bs);
18327bce1c29SKevin Wolf }
18337bce1c29SKevin Wolf
18340513f984SMax Reitz /**
18350513f984SMax Reitz * Reduce .drain_count, set .was_undrained once it reaches 0.
18360513f984SMax Reitz * If .drain_count reaches 0 and the node has a backing file, issue a
18370513f984SMax Reitz * read request.
18380513f984SMax Reitz */
bdrv_replace_test_drain_end(BlockDriverState * bs)18395e8ac217SKevin Wolf static void bdrv_replace_test_drain_end(BlockDriverState *bs)
18400513f984SMax Reitz {
18410513f984SMax Reitz BDRVReplaceTestState *s = bs->opaque;
18420513f984SMax Reitz
1843004915a9SKevin Wolf GRAPH_RDLOCK_GUARD_MAINLOOP();
1844004915a9SKevin Wolf
184523987471SKevin Wolf if (!s->setup_completed) {
184623987471SKevin Wolf return;
184723987471SKevin Wolf }
184823987471SKevin Wolf
18490513f984SMax Reitz g_assert(s->drain_count > 0);
18500513f984SMax Reitz if (!--s->drain_count) {
18510513f984SMax Reitz s->was_undrained = true;
18520513f984SMax Reitz
18530513f984SMax Reitz if (bs->backing) {
18547bce1c29SKevin Wolf Coroutine *co = qemu_coroutine_create(bdrv_replace_test_read_entry,
18557bce1c29SKevin Wolf bs);
18567bce1c29SKevin Wolf bdrv_inc_in_flight(bs);
18577bce1c29SKevin Wolf aio_co_enter(bdrv_get_aio_context(bs), co);
18580513f984SMax Reitz }
18590513f984SMax Reitz }
18600513f984SMax Reitz }
18610513f984SMax Reitz
18620513f984SMax Reitz static BlockDriver bdrv_replace_test = {
18630513f984SMax Reitz .format_name = "replace_test",
18640513f984SMax Reitz .instance_size = sizeof(BDRVReplaceTestState),
18659ebfc111SVladimir Sementsov-Ogievskiy .supports_backing = true,
18660513f984SMax Reitz
18670513f984SMax Reitz .bdrv_close = bdrv_replace_test_close,
18680513f984SMax Reitz .bdrv_co_preadv = bdrv_replace_test_co_preadv,
18690513f984SMax Reitz
18705e8ac217SKevin Wolf .bdrv_drain_begin = bdrv_replace_test_drain_begin,
18715e8ac217SKevin Wolf .bdrv_drain_end = bdrv_replace_test_drain_end,
18720513f984SMax Reitz
187369dca43dSMax Reitz .bdrv_child_perm = bdrv_default_perms,
18740513f984SMax Reitz };
18750513f984SMax Reitz
test_replace_child_mid_drain_read_co(void * opaque)18760513f984SMax Reitz static void coroutine_fn test_replace_child_mid_drain_read_co(void *opaque)
18770513f984SMax Reitz {
18780513f984SMax Reitz int ret;
18790513f984SMax Reitz char data;
18800513f984SMax Reitz
18810513f984SMax Reitz ret = blk_co_pread(opaque, 0, 1, &data, 0);
18820513f984SMax Reitz g_assert(ret >= 0);
18830513f984SMax Reitz }
18840513f984SMax Reitz
18850513f984SMax Reitz /**
18860513f984SMax Reitz * We test two things:
18870513f984SMax Reitz * (1) bdrv_replace_child_noperm() must not undrain the parent if both
18880513f984SMax Reitz * children are drained.
18890513f984SMax Reitz * (2) bdrv_replace_child_noperm() must never flush I/O requests to a
18900513f984SMax Reitz * drained child. If the old child is drained, it must flush I/O
18910513f984SMax Reitz * requests after the new one has been attached. If the new child
18920513f984SMax Reitz * is drained, it must flush I/O requests before the old one is
18930513f984SMax Reitz * detached.
18940513f984SMax Reitz *
18950513f984SMax Reitz * To do so, we create one parent node and two child nodes; then
18960513f984SMax Reitz * attach one of the children (old_child_bs) to the parent, then
18970513f984SMax Reitz * drain both old_child_bs and new_child_bs according to
18980513f984SMax Reitz * old_drain_count and new_drain_count, respectively, and finally
18990513f984SMax Reitz * we invoke bdrv_replace_node() to replace old_child_bs by
19000513f984SMax Reitz * new_child_bs.
19010513f984SMax Reitz *
19020513f984SMax Reitz * The test block driver we use here (bdrv_replace_test) has a read
19030513f984SMax Reitz * function that:
19040513f984SMax Reitz * - For the parent node, can optionally yield, and then forwards the
19050513f984SMax Reitz * read to bdrv_preadv(),
19060513f984SMax Reitz * - For the child node, just returns immediately.
19070513f984SMax Reitz *
19080513f984SMax Reitz * If the read yields, the drain_begin function will wake it up.
19090513f984SMax Reitz *
19100513f984SMax Reitz * The drain_end function issues a read on the parent once it is fully
19110513f984SMax Reitz * undrained (which simulates requests starting to come in again).
19120513f984SMax Reitz */
do_test_replace_child_mid_drain(int old_drain_count,int new_drain_count)19130513f984SMax Reitz static void do_test_replace_child_mid_drain(int old_drain_count,
19140513f984SMax Reitz int new_drain_count)
19150513f984SMax Reitz {
19160513f984SMax Reitz BlockBackend *parent_blk;
19170513f984SMax Reitz BlockDriverState *parent_bs;
19180513f984SMax Reitz BlockDriverState *old_child_bs, *new_child_bs;
19190513f984SMax Reitz BDRVReplaceTestState *parent_s;
19200513f984SMax Reitz BDRVReplaceTestState *old_child_s, *new_child_s;
19210513f984SMax Reitz Coroutine *io_co;
19220513f984SMax Reitz int i;
19230513f984SMax Reitz
19240513f984SMax Reitz parent_bs = bdrv_new_open_driver(&bdrv_replace_test, "parent", 0,
19250513f984SMax Reitz &error_abort);
19260513f984SMax Reitz parent_s = parent_bs->opaque;
19270513f984SMax Reitz
19280513f984SMax Reitz parent_blk = blk_new(qemu_get_aio_context(),
19290513f984SMax Reitz BLK_PERM_CONSISTENT_READ, BLK_PERM_ALL);
19300513f984SMax Reitz blk_insert_bs(parent_blk, parent_bs, &error_abort);
19310513f984SMax Reitz
19320513f984SMax Reitz old_child_bs = bdrv_new_open_driver(&bdrv_replace_test, "old-child", 0,
19330513f984SMax Reitz &error_abort);
19340513f984SMax Reitz new_child_bs = bdrv_new_open_driver(&bdrv_replace_test, "new-child", 0,
19350513f984SMax Reitz &error_abort);
19360513f984SMax Reitz old_child_s = old_child_bs->opaque;
19370513f984SMax Reitz new_child_s = new_child_bs->opaque;
19380513f984SMax Reitz
19390513f984SMax Reitz /* So that we can read something */
19400513f984SMax Reitz parent_bs->total_sectors = 1;
19410513f984SMax Reitz old_child_bs->total_sectors = 1;
19420513f984SMax Reitz new_child_bs->total_sectors = 1;
19430513f984SMax Reitz
19440513f984SMax Reitz bdrv_ref(old_child_bs);
19456bc30f19SStefan Hajnoczi bdrv_graph_wrlock();
19465bb04747SVladimir Sementsov-Ogievskiy bdrv_attach_child(parent_bs, old_child_bs, "child", &child_of_bds,
19475bb04747SVladimir Sementsov-Ogievskiy BDRV_CHILD_COW, &error_abort);
19486bc30f19SStefan Hajnoczi bdrv_graph_wrunlock();
194923987471SKevin Wolf parent_s->setup_completed = true;
19500513f984SMax Reitz
19510513f984SMax Reitz for (i = 0; i < old_drain_count; i++) {
19520513f984SMax Reitz bdrv_drained_begin(old_child_bs);
19530513f984SMax Reitz }
19540513f984SMax Reitz for (i = 0; i < new_drain_count; i++) {
19550513f984SMax Reitz bdrv_drained_begin(new_child_bs);
19560513f984SMax Reitz }
19570513f984SMax Reitz
19580513f984SMax Reitz if (!old_drain_count) {
19590513f984SMax Reitz /*
19600513f984SMax Reitz * Start a read operation that will yield, so it will not
19610513f984SMax Reitz * complete before the node is drained.
19620513f984SMax Reitz */
19630513f984SMax Reitz parent_s->yield_before_read = true;
19640513f984SMax Reitz io_co = qemu_coroutine_create(test_replace_child_mid_drain_read_co,
19650513f984SMax Reitz parent_blk);
19660513f984SMax Reitz qemu_coroutine_enter(io_co);
19670513f984SMax Reitz }
19680513f984SMax Reitz
19690513f984SMax Reitz /* If we have started a read operation, it should have yielded */
19700513f984SMax Reitz g_assert(!parent_s->has_read);
19710513f984SMax Reitz
19720513f984SMax Reitz /* Reset drained status so we can see what bdrv_replace_node() does */
19730513f984SMax Reitz parent_s->was_drained = false;
19740513f984SMax Reitz parent_s->was_undrained = false;
19750513f984SMax Reitz
19760513f984SMax Reitz g_assert(parent_bs->quiesce_counter == old_drain_count);
1977ccd6a379SKevin Wolf bdrv_drained_begin(old_child_bs);
1978ccd6a379SKevin Wolf bdrv_drained_begin(new_child_bs);
19796bc30f19SStefan Hajnoczi bdrv_graph_wrlock();
19800513f984SMax Reitz bdrv_replace_node(old_child_bs, new_child_bs, &error_abort);
19816bc30f19SStefan Hajnoczi bdrv_graph_wrunlock();
1982ccd6a379SKevin Wolf bdrv_drained_end(new_child_bs);
1983ccd6a379SKevin Wolf bdrv_drained_end(old_child_bs);
19840513f984SMax Reitz g_assert(parent_bs->quiesce_counter == new_drain_count);
19850513f984SMax Reitz
19860513f984SMax Reitz if (!old_drain_count && !new_drain_count) {
19870513f984SMax Reitz /*
19880513f984SMax Reitz * From undrained to undrained drains and undrains the parent,
19890513f984SMax Reitz * because bdrv_replace_node() contains a drained section for
19900513f984SMax Reitz * @old_child_bs.
19910513f984SMax Reitz */
19920513f984SMax Reitz g_assert(parent_s->was_drained && parent_s->was_undrained);
19930513f984SMax Reitz } else if (!old_drain_count && new_drain_count) {
19940513f984SMax Reitz /*
19950513f984SMax Reitz * From undrained to drained should drain the parent and keep
19960513f984SMax Reitz * it that way.
19970513f984SMax Reitz */
19980513f984SMax Reitz g_assert(parent_s->was_drained && !parent_s->was_undrained);
19990513f984SMax Reitz } else if (old_drain_count && !new_drain_count) {
20000513f984SMax Reitz /*
20010513f984SMax Reitz * From drained to undrained should undrain the parent and
20020513f984SMax Reitz * keep it that way.
20030513f984SMax Reitz */
20040513f984SMax Reitz g_assert(!parent_s->was_drained && parent_s->was_undrained);
20050513f984SMax Reitz } else /* if (old_drain_count && new_drain_count) */ {
20060513f984SMax Reitz /*
20070513f984SMax Reitz * From drained to drained must not undrain the parent at any
20080513f984SMax Reitz * point
20090513f984SMax Reitz */
20100513f984SMax Reitz g_assert(!parent_s->was_drained && !parent_s->was_undrained);
20110513f984SMax Reitz }
20120513f984SMax Reitz
20130513f984SMax Reitz if (!old_drain_count || !new_drain_count) {
20140513f984SMax Reitz /*
20150513f984SMax Reitz * If !old_drain_count, we have started a read request before
20160513f984SMax Reitz * bdrv_replace_node(). If !new_drain_count, the parent must
20170513f984SMax Reitz * have been undrained at some point, and
20180513f984SMax Reitz * bdrv_replace_test_co_drain_end() starts a read request
20190513f984SMax Reitz * then.
20200513f984SMax Reitz */
20210513f984SMax Reitz g_assert(parent_s->has_read);
20220513f984SMax Reitz } else {
20230513f984SMax Reitz /*
20240513f984SMax Reitz * If the parent was never undrained, there is no way to start
20250513f984SMax Reitz * a read request.
20260513f984SMax Reitz */
20270513f984SMax Reitz g_assert(!parent_s->has_read);
20280513f984SMax Reitz }
20290513f984SMax Reitz
20300513f984SMax Reitz /* A drained child must have not received any request */
20310513f984SMax Reitz g_assert(!(old_drain_count && old_child_s->has_read));
20320513f984SMax Reitz g_assert(!(new_drain_count && new_child_s->has_read));
20330513f984SMax Reitz
20340513f984SMax Reitz for (i = 0; i < new_drain_count; i++) {
20350513f984SMax Reitz bdrv_drained_end(new_child_bs);
20360513f984SMax Reitz }
20370513f984SMax Reitz for (i = 0; i < old_drain_count; i++) {
20380513f984SMax Reitz bdrv_drained_end(old_child_bs);
20390513f984SMax Reitz }
20400513f984SMax Reitz
20410513f984SMax Reitz /*
20420513f984SMax Reitz * By now, bdrv_replace_test_co_drain_end() must have been called
20430513f984SMax Reitz * at some point while the new child was attached to the parent.
20440513f984SMax Reitz */
20450513f984SMax Reitz g_assert(parent_s->has_read);
20460513f984SMax Reitz g_assert(new_child_s->has_read);
20470513f984SMax Reitz
20480513f984SMax Reitz blk_unref(parent_blk);
20490513f984SMax Reitz bdrv_unref(parent_bs);
20500513f984SMax Reitz bdrv_unref(old_child_bs);
20510513f984SMax Reitz bdrv_unref(new_child_bs);
20520513f984SMax Reitz }
20530513f984SMax Reitz
test_replace_child_mid_drain(void)20540513f984SMax Reitz static void test_replace_child_mid_drain(void)
20550513f984SMax Reitz {
20560513f984SMax Reitz int old_drain_count, new_drain_count;
20570513f984SMax Reitz
20580513f984SMax Reitz for (old_drain_count = 0; old_drain_count < 2; old_drain_count++) {
20590513f984SMax Reitz for (new_drain_count = 0; new_drain_count < 2; new_drain_count++) {
20600513f984SMax Reitz do_test_replace_child_mid_drain(old_drain_count, new_drain_count);
20610513f984SMax Reitz }
20620513f984SMax Reitz }
20630513f984SMax Reitz }
20640513f984SMax Reitz
main(int argc,char ** argv)2065881cfd17SKevin Wolf int main(int argc, char **argv)
2066881cfd17SKevin Wolf {
2067bb675689SKevin Wolf int ret;
2068bb675689SKevin Wolf
2069881cfd17SKevin Wolf bdrv_init();
2070881cfd17SKevin Wolf qemu_init_main_loop(&error_abort);
2071881cfd17SKevin Wolf
2072881cfd17SKevin Wolf g_test_init(&argc, &argv, NULL);
2073bb675689SKevin Wolf qemu_event_init(&done_event, false);
2074881cfd17SKevin Wolf
2075881cfd17SKevin Wolf g_test_add_func("/bdrv-drain/driver-cb/drain_all", test_drv_cb_drain_all);
207686e1c840SKevin Wolf g_test_add_func("/bdrv-drain/driver-cb/drain", test_drv_cb_drain);
2077881cfd17SKevin Wolf
20786d0252f2SKevin Wolf g_test_add_func("/bdrv-drain/driver-cb/co/drain_all",
20796d0252f2SKevin Wolf test_drv_cb_co_drain_all);
20800582eb10SKevin Wolf g_test_add_func("/bdrv-drain/driver-cb/co/drain", test_drv_cb_co_drain);
20810582eb10SKevin Wolf
208289a6ceabSKevin Wolf g_test_add_func("/bdrv-drain/quiesce/drain_all", test_quiesce_drain_all);
208389a6ceabSKevin Wolf g_test_add_func("/bdrv-drain/quiesce/drain", test_quiesce_drain);
208489a6ceabSKevin Wolf
20856d0252f2SKevin Wolf g_test_add_func("/bdrv-drain/quiesce/co/drain_all",
20866d0252f2SKevin Wolf test_quiesce_co_drain_all);
20870582eb10SKevin Wolf g_test_add_func("/bdrv-drain/quiesce/co/drain", test_quiesce_co_drain);
20880582eb10SKevin Wolf
20896c429a6aSKevin Wolf g_test_add_func("/bdrv-drain/nested", test_nested);
209019f7a7e5SKevin Wolf
209119f7a7e5SKevin Wolf g_test_add_func("/bdrv-drain/graph-change/drain_all",
209219f7a7e5SKevin Wolf test_graph_change_drain_all);
20936c429a6aSKevin Wolf
2094bb675689SKevin Wolf g_test_add_func("/bdrv-drain/iothread/drain_all", test_iothread_drain_all);
2095bb675689SKevin Wolf g_test_add_func("/bdrv-drain/iothread/drain", test_iothread_drain);
2096bb675689SKevin Wolf
20977253220dSKevin Wolf g_test_add_func("/bdrv-drain/blockjob/drain_all", test_blockjob_drain_all);
20987253220dSKevin Wolf g_test_add_func("/bdrv-drain/blockjob/drain", test_blockjob_drain);
20997253220dSKevin Wolf
2100d49725afSKevin Wolf g_test_add_func("/bdrv-drain/blockjob/error/drain_all",
2101d49725afSKevin Wolf test_blockjob_error_drain_all);
2102d49725afSKevin Wolf g_test_add_func("/bdrv-drain/blockjob/error/drain",
2103d49725afSKevin Wolf test_blockjob_error_drain);
2104d49725afSKevin Wolf
2105f62c1729SKevin Wolf g_test_add_func("/bdrv-drain/blockjob/iothread/drain_all",
2106f62c1729SKevin Wolf test_blockjob_iothread_drain_all);
2107f62c1729SKevin Wolf g_test_add_func("/bdrv-drain/blockjob/iothread/drain",
2108f62c1729SKevin Wolf test_blockjob_iothread_drain);
2109f62c1729SKevin Wolf
2110d49725afSKevin Wolf g_test_add_func("/bdrv-drain/blockjob/iothread/error/drain_all",
2111d49725afSKevin Wolf test_blockjob_iothread_error_drain_all);
2112d49725afSKevin Wolf g_test_add_func("/bdrv-drain/blockjob/iothread/error/drain",
2113d49725afSKevin Wolf test_blockjob_iothread_error_drain);
2114d49725afSKevin Wolf
2115ebd31837SKevin Wolf g_test_add_func("/bdrv-drain/deletion/drain", test_delete_by_drain);
211619f7a7e5SKevin Wolf g_test_add_func("/bdrv-drain/detach/drain_all", test_detach_by_drain_all);
2117ebd31837SKevin Wolf g_test_add_func("/bdrv-drain/detach/drain", test_detach_by_drain);
2118231281abSKevin Wolf g_test_add_func("/bdrv-drain/detach/parent_cb", test_detach_by_parent_cb);
211957320ca9SKevin Wolf g_test_add_func("/bdrv-drain/detach/driver_cb", test_detach_by_driver_cb);
21204c8158e3SMax Reitz
2121b994c5bcSKevin Wolf g_test_add_func("/bdrv-drain/attach/drain", test_append_to_drained);
2122b994c5bcSKevin Wolf
2123247d2737SKevin Wolf g_test_add_func("/bdrv-drain/set_aio_context", test_set_aio_context);
2124247d2737SKevin Wolf
21258e442810SMax Reitz g_test_add_func("/bdrv-drain/blockjob/commit_by_drained_end",
21268e442810SMax Reitz test_blockjob_commit_by_drained_end);
21278e442810SMax Reitz
21289746b35cSMax Reitz g_test_add_func("/bdrv-drain/bdrv_drop_intermediate/poll",
21299746b35cSMax Reitz test_drop_intermediate_poll);
21309746b35cSMax Reitz
21310513f984SMax Reitz g_test_add_func("/bdrv-drain/replace_child/mid-drain",
21320513f984SMax Reitz test_replace_child_mid_drain);
21330513f984SMax Reitz
2134bb675689SKevin Wolf ret = g_test_run();
2135bb675689SKevin Wolf qemu_event_destroy(&done_event);
2136bb675689SKevin Wolf return ret;
2137881cfd17SKevin Wolf }
2138