]> git.proxmox.com Git - mirror_qemu.git/blame - tests/unit/test-bdrv-drain.c
test-bdrv-drain: Don't yield in .bdrv_co_drained_begin/end()
[mirror_qemu.git] / tests / unit / test-bdrv-drain.c
CommitLineData
881cfd17
KW
1/*
2 * Block node draining tests
3 *
4 * Copyright (c) 2017 Kevin Wolf <kwolf@redhat.com>
5 *
6 * Permission is hereby granted, free of charge, to any person obtaining a copy
7 * of this software and associated documentation files (the "Software"), to deal
8 * in the Software without restriction, including without limitation the rights
9 * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
10 * copies of the Software, and to permit persons to whom the Software is
11 * furnished to do so, subject to the following conditions:
12 *
13 * The above copyright notice and this permission notice shall be included in
14 * all copies or substantial portions of the Software.
15 *
16 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
17 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
18 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
19 * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
20 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
21 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
22 * THE SOFTWARE.
23 */
24
25#include "qemu/osdep.h"
26#include "block/block.h"
7253220d 27#include "block/blockjob_int.h"
881cfd17
KW
28#include "sysemu/block-backend.h"
29#include "qapi/error.h"
db725815 30#include "qemu/main-loop.h"
bb675689
KW
31#include "iothread.h"
32
33static QemuEvent done_event;
881cfd17
KW
34
35typedef struct BDRVTestState {
36 int drain_count;
bb675689 37 AioContext *bh_indirection_ctx;
57320ca9 38 bool sleep_in_drain_begin;
881cfd17
KW
39} BDRVTestState;
40
7bce1c29
KW
41static void coroutine_fn sleep_in_drain_begin(void *opaque)
42{
43 BlockDriverState *bs = opaque;
44
45 qemu_co_sleep_ns(QEMU_CLOCK_REALTIME, 100000);
46 bdrv_dec_in_flight(bs);
47}
48
881cfd17
KW
49static void coroutine_fn bdrv_test_co_drain_begin(BlockDriverState *bs)
50{
51 BDRVTestState *s = bs->opaque;
52 s->drain_count++;
57320ca9 53 if (s->sleep_in_drain_begin) {
7bce1c29
KW
54 Coroutine *co = qemu_coroutine_create(sleep_in_drain_begin, bs);
55 bdrv_inc_in_flight(bs);
56 aio_co_enter(bdrv_get_aio_context(bs), co);
57320ca9 57 }
881cfd17
KW
58}
59
60static void coroutine_fn bdrv_test_co_drain_end(BlockDriverState *bs)
61{
62 BDRVTestState *s = bs->opaque;
63 s->drain_count--;
64}
65
66static void bdrv_test_close(BlockDriverState *bs)
67{
68 BDRVTestState *s = bs->opaque;
69 g_assert_cmpint(s->drain_count, >, 0);
70}
71
bb675689
KW
72static void co_reenter_bh(void *opaque)
73{
74 aio_co_wake(opaque);
75}
76
881cfd17 77static int coroutine_fn bdrv_test_co_preadv(BlockDriverState *bs,
f7ef38dd
VSO
78 int64_t offset, int64_t bytes,
79 QEMUIOVector *qiov,
80 BdrvRequestFlags flags)
881cfd17 81{
bb675689
KW
82 BDRVTestState *s = bs->opaque;
83
881cfd17
KW
84 /* We want this request to stay until the polling loop in drain waits for
85 * it to complete. We need to sleep a while as bdrv_drain_invoke() comes
86 * first and polls its result, too, but it shouldn't accidentally complete
87 * this request yet. */
88 qemu_co_sleep_ns(QEMU_CLOCK_REALTIME, 100000);
89
bb675689
KW
90 if (s->bh_indirection_ctx) {
91 aio_bh_schedule_oneshot(s->bh_indirection_ctx, co_reenter_bh,
92 qemu_coroutine_self());
93 qemu_coroutine_yield();
94 }
95
881cfd17
KW
96 return 0;
97}
98
9746b35c
HR
99static int bdrv_test_change_backing_file(BlockDriverState *bs,
100 const char *backing_file,
101 const char *backing_fmt)
102{
103 return 0;
104}
105
881cfd17
KW
106static BlockDriver bdrv_test = {
107 .format_name = "test",
108 .instance_size = sizeof(BDRVTestState),
25f78d9e 109 .supports_backing = true,
881cfd17
KW
110
111 .bdrv_close = bdrv_test_close,
112 .bdrv_co_preadv = bdrv_test_co_preadv,
113
114 .bdrv_co_drain_begin = bdrv_test_co_drain_begin,
115 .bdrv_co_drain_end = bdrv_test_co_drain_end,
86e1c840 116
e5d8a406 117 .bdrv_child_perm = bdrv_default_perms,
9746b35c
HR
118
119 .bdrv_change_backing_file = bdrv_test_change_backing_file,
881cfd17
KW
120};
121
122static void aio_ret_cb(void *opaque, int ret)
123{
124 int *aio_ret = opaque;
125 *aio_ret = ret;
126}
127
0582eb10
KW
128typedef struct CallInCoroutineData {
129 void (*entry)(void);
130 bool done;
131} CallInCoroutineData;
132
133static coroutine_fn void call_in_coroutine_entry(void *opaque)
134{
135 CallInCoroutineData *data = opaque;
136
137 data->entry();
138 data->done = true;
139}
140
141static void call_in_coroutine(void (*entry)(void))
142{
143 Coroutine *co;
144 CallInCoroutineData data = {
145 .entry = entry,
146 .done = false,
147 };
148
149 co = qemu_coroutine_create(call_in_coroutine_entry, &data);
150 qemu_coroutine_enter(co);
151 while (!data.done) {
152 aio_poll(qemu_get_aio_context(), true);
153 }
154}
155
86e1c840
KW
156enum drain_type {
157 BDRV_DRAIN_ALL,
158 BDRV_DRAIN,
d2a85d0f 159 BDRV_SUBTREE_DRAIN,
6c429a6a 160 DRAIN_TYPE_MAX,
86e1c840
KW
161};
162
163static void do_drain_begin(enum drain_type drain_type, BlockDriverState *bs)
164{
165 switch (drain_type) {
166 case BDRV_DRAIN_ALL: bdrv_drain_all_begin(); break;
167 case BDRV_DRAIN: bdrv_drained_begin(bs); break;
d2a85d0f 168 case BDRV_SUBTREE_DRAIN: bdrv_subtree_drained_begin(bs); break;
86e1c840
KW
169 default: g_assert_not_reached();
170 }
171}
172
173static void do_drain_end(enum drain_type drain_type, BlockDriverState *bs)
174{
175 switch (drain_type) {
176 case BDRV_DRAIN_ALL: bdrv_drain_all_end(); break;
177 case BDRV_DRAIN: bdrv_drained_end(bs); break;
d2a85d0f 178 case BDRV_SUBTREE_DRAIN: bdrv_subtree_drained_end(bs); break;
86e1c840
KW
179 default: g_assert_not_reached();
180 }
181}
182
f62c1729
KW
183static void do_drain_begin_unlocked(enum drain_type drain_type, BlockDriverState *bs)
184{
185 if (drain_type != BDRV_DRAIN_ALL) {
186 aio_context_acquire(bdrv_get_aio_context(bs));
187 }
188 do_drain_begin(drain_type, bs);
189 if (drain_type != BDRV_DRAIN_ALL) {
190 aio_context_release(bdrv_get_aio_context(bs));
191 }
192}
193
194static void do_drain_end_unlocked(enum drain_type drain_type, BlockDriverState *bs)
195{
196 if (drain_type != BDRV_DRAIN_ALL) {
197 aio_context_acquire(bdrv_get_aio_context(bs));
198 }
199 do_drain_end(drain_type, bs);
200 if (drain_type != BDRV_DRAIN_ALL) {
201 aio_context_release(bdrv_get_aio_context(bs));
202 }
203}
204
86e1c840 205static void test_drv_cb_common(enum drain_type drain_type, bool recursive)
881cfd17
KW
206{
207 BlockBackend *blk;
86e1c840
KW
208 BlockDriverState *bs, *backing;
209 BDRVTestState *s, *backing_s;
881cfd17
KW
210 BlockAIOCB *acb;
211 int aio_ret;
212
405d8fe0 213 QEMUIOVector qiov = QEMU_IOVEC_INIT_BUF(qiov, NULL, 0);
881cfd17 214
d861ab3a 215 blk = blk_new(qemu_get_aio_context(), BLK_PERM_ALL, BLK_PERM_ALL);
881cfd17
KW
216 bs = bdrv_new_open_driver(&bdrv_test, "test-node", BDRV_O_RDWR,
217 &error_abort);
218 s = bs->opaque;
219 blk_insert_bs(blk, bs, &error_abort);
220
86e1c840
KW
221 backing = bdrv_new_open_driver(&bdrv_test, "backing", 0, &error_abort);
222 backing_s = backing->opaque;
223 bdrv_set_backing_hd(bs, backing, &error_abort);
224
881cfd17
KW
225 /* Simple bdrv_drain_all_begin/end pair, check that CBs are called */
226 g_assert_cmpint(s->drain_count, ==, 0);
86e1c840
KW
227 g_assert_cmpint(backing_s->drain_count, ==, 0);
228
229 do_drain_begin(drain_type, bs);
230
881cfd17 231 g_assert_cmpint(s->drain_count, ==, 1);
86e1c840
KW
232 g_assert_cmpint(backing_s->drain_count, ==, !!recursive);
233
234 do_drain_end(drain_type, bs);
235
881cfd17 236 g_assert_cmpint(s->drain_count, ==, 0);
86e1c840 237 g_assert_cmpint(backing_s->drain_count, ==, 0);
881cfd17
KW
238
239 /* Now do the same while a request is pending */
240 aio_ret = -EINPROGRESS;
241 acb = blk_aio_preadv(blk, 0, &qiov, 0, aio_ret_cb, &aio_ret);
242 g_assert(acb != NULL);
243 g_assert_cmpint(aio_ret, ==, -EINPROGRESS);
244
245 g_assert_cmpint(s->drain_count, ==, 0);
86e1c840
KW
246 g_assert_cmpint(backing_s->drain_count, ==, 0);
247
248 do_drain_begin(drain_type, bs);
249
881cfd17
KW
250 g_assert_cmpint(aio_ret, ==, 0);
251 g_assert_cmpint(s->drain_count, ==, 1);
86e1c840
KW
252 g_assert_cmpint(backing_s->drain_count, ==, !!recursive);
253
254 do_drain_end(drain_type, bs);
255
881cfd17 256 g_assert_cmpint(s->drain_count, ==, 0);
86e1c840 257 g_assert_cmpint(backing_s->drain_count, ==, 0);
881cfd17 258
86e1c840 259 bdrv_unref(backing);
881cfd17
KW
260 bdrv_unref(bs);
261 blk_unref(blk);
262}
263
86e1c840
KW
264static void test_drv_cb_drain_all(void)
265{
266 test_drv_cb_common(BDRV_DRAIN_ALL, true);
267}
268
269static void test_drv_cb_drain(void)
270{
271 test_drv_cb_common(BDRV_DRAIN, false);
272}
273
d2a85d0f
KW
274static void test_drv_cb_drain_subtree(void)
275{
276 test_drv_cb_common(BDRV_SUBTREE_DRAIN, true);
277}
278
6d0252f2
KW
279static void test_drv_cb_co_drain_all(void)
280{
281 call_in_coroutine(test_drv_cb_drain_all);
282}
283
0582eb10
KW
284static void test_drv_cb_co_drain(void)
285{
286 call_in_coroutine(test_drv_cb_drain);
287}
288
289static void test_drv_cb_co_drain_subtree(void)
290{
291 call_in_coroutine(test_drv_cb_drain_subtree);
292}
293
89a6ceab
KW
294static void test_quiesce_common(enum drain_type drain_type, bool recursive)
295{
296 BlockBackend *blk;
297 BlockDriverState *bs, *backing;
298
d861ab3a 299 blk = blk_new(qemu_get_aio_context(), BLK_PERM_ALL, BLK_PERM_ALL);
89a6ceab
KW
300 bs = bdrv_new_open_driver(&bdrv_test, "test-node", BDRV_O_RDWR,
301 &error_abort);
302 blk_insert_bs(blk, bs, &error_abort);
303
304 backing = bdrv_new_open_driver(&bdrv_test, "backing", 0, &error_abort);
305 bdrv_set_backing_hd(bs, backing, &error_abort);
306
307 g_assert_cmpint(bs->quiesce_counter, ==, 0);
308 g_assert_cmpint(backing->quiesce_counter, ==, 0);
309
310 do_drain_begin(drain_type, bs);
311
312 g_assert_cmpint(bs->quiesce_counter, ==, 1);
313 g_assert_cmpint(backing->quiesce_counter, ==, !!recursive);
314
315 do_drain_end(drain_type, bs);
316
317 g_assert_cmpint(bs->quiesce_counter, ==, 0);
318 g_assert_cmpint(backing->quiesce_counter, ==, 0);
319
320 bdrv_unref(backing);
321 bdrv_unref(bs);
322 blk_unref(blk);
323}
324
325static void test_quiesce_drain_all(void)
326{
79ab8b21 327 test_quiesce_common(BDRV_DRAIN_ALL, true);
89a6ceab
KW
328}
329
330static void test_quiesce_drain(void)
331{
332 test_quiesce_common(BDRV_DRAIN, false);
333}
334
d2a85d0f
KW
335static void test_quiesce_drain_subtree(void)
336{
337 test_quiesce_common(BDRV_SUBTREE_DRAIN, true);
338}
339
6d0252f2
KW
340static void test_quiesce_co_drain_all(void)
341{
342 call_in_coroutine(test_quiesce_drain_all);
343}
344
0582eb10
KW
345static void test_quiesce_co_drain(void)
346{
347 call_in_coroutine(test_quiesce_drain);
348}
349
350static void test_quiesce_co_drain_subtree(void)
351{
352 call_in_coroutine(test_quiesce_drain_subtree);
353}
354
6c429a6a
KW
355static void test_nested(void)
356{
357 BlockBackend *blk;
358 BlockDriverState *bs, *backing;
359 BDRVTestState *s, *backing_s;
360 enum drain_type outer, inner;
361
d861ab3a 362 blk = blk_new(qemu_get_aio_context(), BLK_PERM_ALL, BLK_PERM_ALL);
6c429a6a
KW
363 bs = bdrv_new_open_driver(&bdrv_test, "test-node", BDRV_O_RDWR,
364 &error_abort);
365 s = bs->opaque;
366 blk_insert_bs(blk, bs, &error_abort);
367
368 backing = bdrv_new_open_driver(&bdrv_test, "backing", 0, &error_abort);
369 backing_s = backing->opaque;
370 bdrv_set_backing_hd(bs, backing, &error_abort);
371
372 for (outer = 0; outer < DRAIN_TYPE_MAX; outer++) {
373 for (inner = 0; inner < DRAIN_TYPE_MAX; inner++) {
79ab8b21 374 int backing_quiesce = (outer != BDRV_DRAIN) +
6c429a6a
KW
375 (inner != BDRV_DRAIN);
376
377 g_assert_cmpint(bs->quiesce_counter, ==, 0);
378 g_assert_cmpint(backing->quiesce_counter, ==, 0);
379 g_assert_cmpint(s->drain_count, ==, 0);
380 g_assert_cmpint(backing_s->drain_count, ==, 0);
381
382 do_drain_begin(outer, bs);
383 do_drain_begin(inner, bs);
384
79ab8b21 385 g_assert_cmpint(bs->quiesce_counter, ==, 2);
6c429a6a
KW
386 g_assert_cmpint(backing->quiesce_counter, ==, backing_quiesce);
387 g_assert_cmpint(s->drain_count, ==, 2);
79ab8b21 388 g_assert_cmpint(backing_s->drain_count, ==, backing_quiesce);
6c429a6a
KW
389
390 do_drain_end(inner, bs);
391 do_drain_end(outer, bs);
392
393 g_assert_cmpint(bs->quiesce_counter, ==, 0);
394 g_assert_cmpint(backing->quiesce_counter, ==, 0);
395 g_assert_cmpint(s->drain_count, ==, 0);
396 g_assert_cmpint(backing_s->drain_count, ==, 0);
397 }
398 }
399
400 bdrv_unref(backing);
401 bdrv_unref(bs);
402 blk_unref(blk);
403}
404
27e64474
KW
405static void test_multiparent(void)
406{
407 BlockBackend *blk_a, *blk_b;
408 BlockDriverState *bs_a, *bs_b, *backing;
409 BDRVTestState *a_s, *b_s, *backing_s;
410
d861ab3a 411 blk_a = blk_new(qemu_get_aio_context(), BLK_PERM_ALL, BLK_PERM_ALL);
27e64474
KW
412 bs_a = bdrv_new_open_driver(&bdrv_test, "test-node-a", BDRV_O_RDWR,
413 &error_abort);
414 a_s = bs_a->opaque;
415 blk_insert_bs(blk_a, bs_a, &error_abort);
416
d861ab3a 417 blk_b = blk_new(qemu_get_aio_context(), BLK_PERM_ALL, BLK_PERM_ALL);
27e64474
KW
418 bs_b = bdrv_new_open_driver(&bdrv_test, "test-node-b", BDRV_O_RDWR,
419 &error_abort);
420 b_s = bs_b->opaque;
421 blk_insert_bs(blk_b, bs_b, &error_abort);
422
423 backing = bdrv_new_open_driver(&bdrv_test, "backing", 0, &error_abort);
424 backing_s = backing->opaque;
425 bdrv_set_backing_hd(bs_a, backing, &error_abort);
426 bdrv_set_backing_hd(bs_b, backing, &error_abort);
427
428 g_assert_cmpint(bs_a->quiesce_counter, ==, 0);
429 g_assert_cmpint(bs_b->quiesce_counter, ==, 0);
430 g_assert_cmpint(backing->quiesce_counter, ==, 0);
431 g_assert_cmpint(a_s->drain_count, ==, 0);
432 g_assert_cmpint(b_s->drain_count, ==, 0);
433 g_assert_cmpint(backing_s->drain_count, ==, 0);
434
435 do_drain_begin(BDRV_SUBTREE_DRAIN, bs_a);
436
437 g_assert_cmpint(bs_a->quiesce_counter, ==, 1);
438 g_assert_cmpint(bs_b->quiesce_counter, ==, 1);
439 g_assert_cmpint(backing->quiesce_counter, ==, 1);
440 g_assert_cmpint(a_s->drain_count, ==, 1);
441 g_assert_cmpint(b_s->drain_count, ==, 1);
442 g_assert_cmpint(backing_s->drain_count, ==, 1);
443
444 do_drain_begin(BDRV_SUBTREE_DRAIN, bs_b);
445
446 g_assert_cmpint(bs_a->quiesce_counter, ==, 2);
447 g_assert_cmpint(bs_b->quiesce_counter, ==, 2);
448 g_assert_cmpint(backing->quiesce_counter, ==, 2);
449 g_assert_cmpint(a_s->drain_count, ==, 2);
450 g_assert_cmpint(b_s->drain_count, ==, 2);
451 g_assert_cmpint(backing_s->drain_count, ==, 2);
452
453 do_drain_end(BDRV_SUBTREE_DRAIN, bs_b);
454
455 g_assert_cmpint(bs_a->quiesce_counter, ==, 1);
456 g_assert_cmpint(bs_b->quiesce_counter, ==, 1);
457 g_assert_cmpint(backing->quiesce_counter, ==, 1);
458 g_assert_cmpint(a_s->drain_count, ==, 1);
459 g_assert_cmpint(b_s->drain_count, ==, 1);
460 g_assert_cmpint(backing_s->drain_count, ==, 1);
461
462 do_drain_end(BDRV_SUBTREE_DRAIN, bs_a);
463
464 g_assert_cmpint(bs_a->quiesce_counter, ==, 0);
465 g_assert_cmpint(bs_b->quiesce_counter, ==, 0);
466 g_assert_cmpint(backing->quiesce_counter, ==, 0);
467 g_assert_cmpint(a_s->drain_count, ==, 0);
468 g_assert_cmpint(b_s->drain_count, ==, 0);
469 g_assert_cmpint(backing_s->drain_count, ==, 0);
470
471 bdrv_unref(backing);
472 bdrv_unref(bs_a);
473 bdrv_unref(bs_b);
474 blk_unref(blk_a);
475 blk_unref(blk_b);
476}
477
19f7a7e5 478static void test_graph_change_drain_subtree(void)
acebcf8d
KW
479{
480 BlockBackend *blk_a, *blk_b;
481 BlockDriverState *bs_a, *bs_b, *backing;
482 BDRVTestState *a_s, *b_s, *backing_s;
483
d861ab3a 484 blk_a = blk_new(qemu_get_aio_context(), BLK_PERM_ALL, BLK_PERM_ALL);
acebcf8d
KW
485 bs_a = bdrv_new_open_driver(&bdrv_test, "test-node-a", BDRV_O_RDWR,
486 &error_abort);
487 a_s = bs_a->opaque;
488 blk_insert_bs(blk_a, bs_a, &error_abort);
489
d861ab3a 490 blk_b = blk_new(qemu_get_aio_context(), BLK_PERM_ALL, BLK_PERM_ALL);
acebcf8d
KW
491 bs_b = bdrv_new_open_driver(&bdrv_test, "test-node-b", BDRV_O_RDWR,
492 &error_abort);
493 b_s = bs_b->opaque;
494 blk_insert_bs(blk_b, bs_b, &error_abort);
495
496 backing = bdrv_new_open_driver(&bdrv_test, "backing", 0, &error_abort);
497 backing_s = backing->opaque;
498 bdrv_set_backing_hd(bs_a, backing, &error_abort);
499
500 g_assert_cmpint(bs_a->quiesce_counter, ==, 0);
501 g_assert_cmpint(bs_b->quiesce_counter, ==, 0);
502 g_assert_cmpint(backing->quiesce_counter, ==, 0);
503 g_assert_cmpint(a_s->drain_count, ==, 0);
504 g_assert_cmpint(b_s->drain_count, ==, 0);
505 g_assert_cmpint(backing_s->drain_count, ==, 0);
506
507 do_drain_begin(BDRV_SUBTREE_DRAIN, bs_a);
508 do_drain_begin(BDRV_SUBTREE_DRAIN, bs_a);
509 do_drain_begin(BDRV_SUBTREE_DRAIN, bs_a);
510 do_drain_begin(BDRV_SUBTREE_DRAIN, bs_b);
511 do_drain_begin(BDRV_SUBTREE_DRAIN, bs_b);
512
513 bdrv_set_backing_hd(bs_b, backing, &error_abort);
514 g_assert_cmpint(bs_a->quiesce_counter, ==, 5);
515 g_assert_cmpint(bs_b->quiesce_counter, ==, 5);
516 g_assert_cmpint(backing->quiesce_counter, ==, 5);
517 g_assert_cmpint(a_s->drain_count, ==, 5);
518 g_assert_cmpint(b_s->drain_count, ==, 5);
519 g_assert_cmpint(backing_s->drain_count, ==, 5);
520
521 bdrv_set_backing_hd(bs_b, NULL, &error_abort);
522 g_assert_cmpint(bs_a->quiesce_counter, ==, 3);
523 g_assert_cmpint(bs_b->quiesce_counter, ==, 2);
524 g_assert_cmpint(backing->quiesce_counter, ==, 3);
525 g_assert_cmpint(a_s->drain_count, ==, 3);
526 g_assert_cmpint(b_s->drain_count, ==, 2);
527 g_assert_cmpint(backing_s->drain_count, ==, 3);
528
529 bdrv_set_backing_hd(bs_b, backing, &error_abort);
530 g_assert_cmpint(bs_a->quiesce_counter, ==, 5);
531 g_assert_cmpint(bs_b->quiesce_counter, ==, 5);
532 g_assert_cmpint(backing->quiesce_counter, ==, 5);
533 g_assert_cmpint(a_s->drain_count, ==, 5);
534 g_assert_cmpint(b_s->drain_count, ==, 5);
535 g_assert_cmpint(backing_s->drain_count, ==, 5);
536
537 do_drain_end(BDRV_SUBTREE_DRAIN, bs_b);
538 do_drain_end(BDRV_SUBTREE_DRAIN, bs_b);
539 do_drain_end(BDRV_SUBTREE_DRAIN, bs_a);
540 do_drain_end(BDRV_SUBTREE_DRAIN, bs_a);
541 do_drain_end(BDRV_SUBTREE_DRAIN, bs_a);
542
543 g_assert_cmpint(bs_a->quiesce_counter, ==, 0);
544 g_assert_cmpint(bs_b->quiesce_counter, ==, 0);
545 g_assert_cmpint(backing->quiesce_counter, ==, 0);
546 g_assert_cmpint(a_s->drain_count, ==, 0);
547 g_assert_cmpint(b_s->drain_count, ==, 0);
548 g_assert_cmpint(backing_s->drain_count, ==, 0);
549
550 bdrv_unref(backing);
551 bdrv_unref(bs_a);
552 bdrv_unref(bs_b);
553 blk_unref(blk_a);
554 blk_unref(blk_b);
555}
556
19f7a7e5
KW
557static void test_graph_change_drain_all(void)
558{
559 BlockBackend *blk_a, *blk_b;
560 BlockDriverState *bs_a, *bs_b;
561 BDRVTestState *a_s, *b_s;
562
563 /* Create node A with a BlockBackend */
d861ab3a 564 blk_a = blk_new(qemu_get_aio_context(), BLK_PERM_ALL, BLK_PERM_ALL);
19f7a7e5
KW
565 bs_a = bdrv_new_open_driver(&bdrv_test, "test-node-a", BDRV_O_RDWR,
566 &error_abort);
567 a_s = bs_a->opaque;
568 blk_insert_bs(blk_a, bs_a, &error_abort);
569
570 g_assert_cmpint(bs_a->quiesce_counter, ==, 0);
571 g_assert_cmpint(a_s->drain_count, ==, 0);
572
573 /* Call bdrv_drain_all_begin() */
574 bdrv_drain_all_begin();
575
576 g_assert_cmpint(bs_a->quiesce_counter, ==, 1);
577 g_assert_cmpint(a_s->drain_count, ==, 1);
578
579 /* Create node B with a BlockBackend */
d861ab3a 580 blk_b = blk_new(qemu_get_aio_context(), BLK_PERM_ALL, BLK_PERM_ALL);
19f7a7e5
KW
581 bs_b = bdrv_new_open_driver(&bdrv_test, "test-node-b", BDRV_O_RDWR,
582 &error_abort);
583 b_s = bs_b->opaque;
584 blk_insert_bs(blk_b, bs_b, &error_abort);
585
586 g_assert_cmpint(bs_a->quiesce_counter, ==, 1);
587 g_assert_cmpint(bs_b->quiesce_counter, ==, 1);
588 g_assert_cmpint(a_s->drain_count, ==, 1);
589 g_assert_cmpint(b_s->drain_count, ==, 1);
590
591 /* Unref and finally delete node A */
592 blk_unref(blk_a);
593
594 g_assert_cmpint(bs_a->quiesce_counter, ==, 1);
595 g_assert_cmpint(bs_b->quiesce_counter, ==, 1);
596 g_assert_cmpint(a_s->drain_count, ==, 1);
597 g_assert_cmpint(b_s->drain_count, ==, 1);
598
599 bdrv_unref(bs_a);
600
601 g_assert_cmpint(bs_b->quiesce_counter, ==, 1);
602 g_assert_cmpint(b_s->drain_count, ==, 1);
603
604 /* End the drained section */
605 bdrv_drain_all_end();
606
607 g_assert_cmpint(bs_b->quiesce_counter, ==, 0);
608 g_assert_cmpint(b_s->drain_count, ==, 0);
1a6d3bd2 609 g_assert_cmpint(qemu_get_aio_context()->external_disable_cnt, ==, 0);
19f7a7e5
KW
610
611 bdrv_unref(bs_b);
612 blk_unref(blk_b);
613}
614
bb675689
KW
615struct test_iothread_data {
616 BlockDriverState *bs;
617 enum drain_type drain_type;
618 int *aio_ret;
619};
620
621static void test_iothread_drain_entry(void *opaque)
622{
623 struct test_iothread_data *data = opaque;
624
625 aio_context_acquire(bdrv_get_aio_context(data->bs));
626 do_drain_begin(data->drain_type, data->bs);
627 g_assert_cmpint(*data->aio_ret, ==, 0);
628 do_drain_end(data->drain_type, data->bs);
629 aio_context_release(bdrv_get_aio_context(data->bs));
630
631 qemu_event_set(&done_event);
632}
633
634static void test_iothread_aio_cb(void *opaque, int ret)
635{
636 int *aio_ret = opaque;
637 *aio_ret = ret;
638 qemu_event_set(&done_event);
639}
640
ecc1a5c7
KW
641static void test_iothread_main_thread_bh(void *opaque)
642{
643 struct test_iothread_data *data = opaque;
644
645 /* Test that the AioContext is not yet locked in a random BH that is
646 * executed during drain, otherwise this would deadlock. */
647 aio_context_acquire(bdrv_get_aio_context(data->bs));
648 bdrv_flush(data->bs);
649 aio_context_release(bdrv_get_aio_context(data->bs));
650}
651
bb675689
KW
652/*
653 * Starts an AIO request on a BDS that runs in the AioContext of iothread 1.
654 * The request involves a BH on iothread 2 before it can complete.
655 *
656 * @drain_thread = 0 means that do_drain_begin/end are called from the main
657 * thread, @drain_thread = 1 means that they are called from iothread 1. Drain
658 * for this BDS cannot be called from iothread 2 because only the main thread
659 * may do cross-AioContext polling.
660 */
661static void test_iothread_common(enum drain_type drain_type, int drain_thread)
662{
663 BlockBackend *blk;
664 BlockDriverState *bs;
665 BDRVTestState *s;
666 BlockAIOCB *acb;
667 int aio_ret;
668 struct test_iothread_data data;
669
670 IOThread *a = iothread_new();
671 IOThread *b = iothread_new();
672 AioContext *ctx_a = iothread_get_aio_context(a);
673 AioContext *ctx_b = iothread_get_aio_context(b);
674
405d8fe0 675 QEMUIOVector qiov = QEMU_IOVEC_INIT_BUF(qiov, NULL, 0);
bb675689
KW
676
677 /* bdrv_drain_all() may only be called from the main loop thread */
678 if (drain_type == BDRV_DRAIN_ALL && drain_thread != 0) {
679 goto out;
680 }
681
d861ab3a 682 blk = blk_new(qemu_get_aio_context(), BLK_PERM_ALL, BLK_PERM_ALL);
bb675689
KW
683 bs = bdrv_new_open_driver(&bdrv_test, "test-node", BDRV_O_RDWR,
684 &error_abort);
685 s = bs->opaque;
686 blk_insert_bs(blk, bs, &error_abort);
cf312932 687 blk_set_disable_request_queuing(blk, true);
bb675689 688
97896a48 689 blk_set_aio_context(blk, ctx_a, &error_abort);
bb675689
KW
690 aio_context_acquire(ctx_a);
691
692 s->bh_indirection_ctx = ctx_b;
693
694 aio_ret = -EINPROGRESS;
dd353157
KW
695 qemu_event_reset(&done_event);
696
bb675689
KW
697 if (drain_thread == 0) {
698 acb = blk_aio_preadv(blk, 0, &qiov, 0, test_iothread_aio_cb, &aio_ret);
699 } else {
700 acb = blk_aio_preadv(blk, 0, &qiov, 0, aio_ret_cb, &aio_ret);
701 }
702 g_assert(acb != NULL);
703 g_assert_cmpint(aio_ret, ==, -EINPROGRESS);
704
705 aio_context_release(ctx_a);
706
707 data = (struct test_iothread_data) {
708 .bs = bs,
709 .drain_type = drain_type,
710 .aio_ret = &aio_ret,
711 };
712
713 switch (drain_thread) {
714 case 0:
715 if (drain_type != BDRV_DRAIN_ALL) {
716 aio_context_acquire(ctx_a);
717 }
718
ecc1a5c7
KW
719 aio_bh_schedule_oneshot(ctx_a, test_iothread_main_thread_bh, &data);
720
bb675689
KW
721 /* The request is running on the IOThread a. Draining its block device
722 * will make sure that it has completed as far as the BDS is concerned,
723 * but the drain in this thread can continue immediately after
724 * bdrv_dec_in_flight() and aio_ret might be assigned only slightly
725 * later. */
bb675689
KW
726 do_drain_begin(drain_type, bs);
727 g_assert_cmpint(bs->in_flight, ==, 0);
728
729 if (drain_type != BDRV_DRAIN_ALL) {
730 aio_context_release(ctx_a);
731 }
732 qemu_event_wait(&done_event);
733 if (drain_type != BDRV_DRAIN_ALL) {
734 aio_context_acquire(ctx_a);
735 }
736
737 g_assert_cmpint(aio_ret, ==, 0);
738 do_drain_end(drain_type, bs);
739
740 if (drain_type != BDRV_DRAIN_ALL) {
741 aio_context_release(ctx_a);
742 }
743 break;
744 case 1:
bb675689
KW
745 aio_bh_schedule_oneshot(ctx_a, test_iothread_drain_entry, &data);
746 qemu_event_wait(&done_event);
747 break;
748 default:
749 g_assert_not_reached();
750 }
751
752 aio_context_acquire(ctx_a);
97896a48 753 blk_set_aio_context(blk, qemu_get_aio_context(), &error_abort);
bb675689
KW
754 aio_context_release(ctx_a);
755
756 bdrv_unref(bs);
757 blk_unref(blk);
758
759out:
760 iothread_join(a);
761 iothread_join(b);
762}
763
764static void test_iothread_drain_all(void)
765{
766 test_iothread_common(BDRV_DRAIN_ALL, 0);
767 test_iothread_common(BDRV_DRAIN_ALL, 1);
768}
769
770static void test_iothread_drain(void)
771{
772 test_iothread_common(BDRV_DRAIN, 0);
773 test_iothread_common(BDRV_DRAIN, 1);
774}
775
776static void test_iothread_drain_subtree(void)
777{
778 test_iothread_common(BDRV_SUBTREE_DRAIN, 0);
779 test_iothread_common(BDRV_SUBTREE_DRAIN, 1);
780}
781
7253220d
KW
782
783typedef struct TestBlockJob {
784 BlockJob common;
1b177bbe 785 BlockDriverState *bs;
d49725af
KW
786 int run_ret;
787 int prepare_ret;
d8b3afd5 788 bool running;
7253220d
KW
789 bool should_complete;
790} TestBlockJob;
791
ae23dde9
KW
792static int test_job_prepare(Job *job)
793{
794 TestBlockJob *s = container_of(job, TestBlockJob, common.job);
795
796 /* Provoke an AIO_WAIT_WHILE() call to verify there is no deadlock */
1b177bbe 797 bdrv_flush(s->bs);
d49725af
KW
798 return s->prepare_ret;
799}
800
801static void test_job_commit(Job *job)
802{
803 TestBlockJob *s = container_of(job, TestBlockJob, common.job);
804
805 /* Provoke an AIO_WAIT_WHILE() call to verify there is no deadlock */
1b177bbe 806 bdrv_flush(s->bs);
d49725af
KW
807}
808
809static void test_job_abort(Job *job)
810{
811 TestBlockJob *s = container_of(job, TestBlockJob, common.job);
812
813 /* Provoke an AIO_WAIT_WHILE() call to verify there is no deadlock */
1b177bbe 814 bdrv_flush(s->bs);
ae23dde9
KW
815}
816
f67432a2 817static int coroutine_fn test_job_run(Job *job, Error **errp)
7253220d 818{
f67432a2 819 TestBlockJob *s = container_of(job, TestBlockJob, common.job);
7253220d 820
d8b3afd5
KW
821 /* We are running the actual job code past the pause point in
822 * job_co_entry(). */
823 s->running = true;
824
2e1795b5 825 job_transition_to_ready(&s->common.job);
7253220d 826 while (!s->should_complete) {
5599c162
KW
827 /* Avoid job_sleep_ns() because it marks the job as !busy. We want to
828 * emulate some actual activity (probably some I/O) here so that drain
829 * has to wait for this activity to stop. */
d8b3afd5
KW
830 qemu_co_sleep_ns(QEMU_CLOCK_REALTIME, 1000000);
831
89bd0305 832 job_pause_point(&s->common.job);
7253220d
KW
833 }
834
d49725af 835 return s->run_ret;
7253220d
KW
836}
837
3453d972 838static void test_job_complete(Job *job, Error **errp)
7253220d 839{
3453d972 840 TestBlockJob *s = container_of(job, TestBlockJob, common.job);
7253220d
KW
841 s->should_complete = true;
842}
843
844BlockJobDriver test_job_driver = {
33e9e9bd
KW
845 .job_driver = {
846 .instance_size = sizeof(TestBlockJob),
80fa2c75 847 .free = block_job_free,
b15de828 848 .user_resume = block_job_user_resume,
f67432a2 849 .run = test_job_run,
3453d972 850 .complete = test_job_complete,
ae23dde9 851 .prepare = test_job_prepare,
d49725af
KW
852 .commit = test_job_commit,
853 .abort = test_job_abort,
33e9e9bd 854 },
7253220d
KW
855};
856
d49725af
KW
857enum test_job_result {
858 TEST_JOB_SUCCESS,
859 TEST_JOB_FAIL_RUN,
860 TEST_JOB_FAIL_PREPARE,
861};
862
d8b3afd5
KW
863enum test_job_drain_node {
864 TEST_JOB_DRAIN_SRC,
865 TEST_JOB_DRAIN_SRC_CHILD,
866 TEST_JOB_DRAIN_SRC_PARENT,
867};
868
869static void test_blockjob_common_drain_node(enum drain_type drain_type,
870 bool use_iothread,
871 enum test_job_result result,
872 enum test_job_drain_node drain_node)
7253220d
KW
873{
874 BlockBackend *blk_src, *blk_target;
d8b3afd5 875 BlockDriverState *src, *src_backing, *src_overlay, *target, *drain_bs;
7253220d 876 BlockJob *job;
d49725af 877 TestBlockJob *tjob;
f62c1729
KW
878 IOThread *iothread = NULL;
879 AioContext *ctx;
7253220d
KW
880 int ret;
881
882 src = bdrv_new_open_driver(&bdrv_test, "source", BDRV_O_RDWR,
883 &error_abort);
d8b3afd5
KW
884 src_backing = bdrv_new_open_driver(&bdrv_test, "source-backing",
885 BDRV_O_RDWR, &error_abort);
886 src_overlay = bdrv_new_open_driver(&bdrv_test, "source-overlay",
887 BDRV_O_RDWR, &error_abort);
888
889 bdrv_set_backing_hd(src_overlay, src, &error_abort);
890 bdrv_unref(src);
891 bdrv_set_backing_hd(src, src_backing, &error_abort);
892 bdrv_unref(src_backing);
893
d861ab3a 894 blk_src = blk_new(qemu_get_aio_context(), BLK_PERM_ALL, BLK_PERM_ALL);
d8b3afd5
KW
895 blk_insert_bs(blk_src, src_overlay, &error_abort);
896
897 switch (drain_node) {
898 case TEST_JOB_DRAIN_SRC:
899 drain_bs = src;
900 break;
901 case TEST_JOB_DRAIN_SRC_CHILD:
902 drain_bs = src_backing;
903 break;
904 case TEST_JOB_DRAIN_SRC_PARENT:
905 drain_bs = src_overlay;
906 break;
907 default:
908 g_assert_not_reached();
909 }
7253220d 910
f62c1729
KW
911 if (use_iothread) {
912 iothread = iothread_new();
913 ctx = iothread_get_aio_context(iothread);
97896a48 914 blk_set_aio_context(blk_src, ctx, &error_abort);
f62c1729
KW
915 } else {
916 ctx = qemu_get_aio_context();
917 }
918
7253220d
KW
919 target = bdrv_new_open_driver(&bdrv_test, "target", BDRV_O_RDWR,
920 &error_abort);
d861ab3a 921 blk_target = blk_new(qemu_get_aio_context(), BLK_PERM_ALL, BLK_PERM_ALL);
7253220d 922 blk_insert_bs(blk_target, target, &error_abort);
132ada80 923 blk_set_allow_aio_context_change(blk_target, true);
7253220d 924
f62c1729 925 aio_context_acquire(ctx);
d49725af
KW
926 tjob = block_job_create("job0", &test_job_driver, NULL, src,
927 0, BLK_PERM_ALL,
928 0, 0, NULL, NULL, &error_abort);
1b177bbe 929 tjob->bs = src;
d49725af 930 job = &tjob->common;
7253220d 931 block_job_add_bdrv(job, "target", target, 0, BLK_PERM_ALL, &error_abort);
d49725af
KW
932
933 switch (result) {
934 case TEST_JOB_SUCCESS:
935 break;
936 case TEST_JOB_FAIL_RUN:
937 tjob->run_ret = -EIO;
938 break;
939 case TEST_JOB_FAIL_PREPARE:
940 tjob->prepare_ret = -EIO;
941 break;
942 }
6f592e5a 943 aio_context_release(ctx);
d49725af 944
da01ff7f 945 job_start(&job->job);
7253220d 946
d8b3afd5
KW
947 if (use_iothread) {
948 /* job_co_entry() is run in the I/O thread, wait for the actual job
949 * code to start (we don't want to catch the job in the pause point in
950 * job_co_entry(). */
951 while (!tjob->running) {
952 aio_poll(qemu_get_aio_context(), false);
953 }
954 }
955
191e7af3
EGE
956 WITH_JOB_LOCK_GUARD() {
957 g_assert_cmpint(job->job.pause_count, ==, 0);
958 g_assert_false(job->job.paused);
959 g_assert_true(tjob->running);
960 g_assert_true(job->job.busy); /* We're in qemu_co_sleep_ns() */
961 }
7253220d 962
d8b3afd5 963 do_drain_begin_unlocked(drain_type, drain_bs);
7253220d 964
191e7af3
EGE
965 WITH_JOB_LOCK_GUARD() {
966 if (drain_type == BDRV_DRAIN_ALL) {
967 /* bdrv_drain_all() drains both src and target */
968 g_assert_cmpint(job->job.pause_count, ==, 2);
969 } else {
970 g_assert_cmpint(job->job.pause_count, ==, 1);
971 }
972 g_assert_true(job->job.paused);
973 g_assert_false(job->job.busy); /* The job is paused */
7253220d 974 }
7253220d 975
d8b3afd5 976 do_drain_end_unlocked(drain_type, drain_bs);
f62c1729
KW
977
978 if (use_iothread) {
191e7af3
EGE
979 /*
980 * Here we are waiting for the paused status to change,
981 * so don't bother protecting the read every time.
982 *
983 * paused is reset in the I/O thread, wait for it
984 */
f62c1729
KW
985 while (job->job.paused) {
986 aio_poll(qemu_get_aio_context(), false);
987 }
988 }
7253220d 989
191e7af3
EGE
990 WITH_JOB_LOCK_GUARD() {
991 g_assert_cmpint(job->job.pause_count, ==, 0);
992 g_assert_false(job->job.paused);
993 g_assert_true(job->job.busy); /* We're in qemu_co_sleep_ns() */
994 }
7253220d 995
132ada80 996 do_drain_begin_unlocked(drain_type, target);
7253220d 997
191e7af3
EGE
998 WITH_JOB_LOCK_GUARD() {
999 if (drain_type == BDRV_DRAIN_ALL) {
1000 /* bdrv_drain_all() drains both src and target */
1001 g_assert_cmpint(job->job.pause_count, ==, 2);
1002 } else {
1003 g_assert_cmpint(job->job.pause_count, ==, 1);
1004 }
1005 g_assert_true(job->job.paused);
1006 g_assert_false(job->job.busy); /* The job is paused */
7253220d 1007 }
7253220d 1008
132ada80 1009 do_drain_end_unlocked(drain_type, target);
7253220d 1010
f62c1729 1011 if (use_iothread) {
191e7af3
EGE
1012 /*
1013 * Here we are waiting for the paused status to change,
1014 * so don't bother protecting the read every time.
1015 *
1016 * paused is reset in the I/O thread, wait for it
1017 */
f62c1729
KW
1018 while (job->job.paused) {
1019 aio_poll(qemu_get_aio_context(), false);
1020 }
1021 }
1022
191e7af3
EGE
1023 WITH_JOB_LOCK_GUARD() {
1024 g_assert_cmpint(job->job.pause_count, ==, 0);
1025 g_assert_false(job->job.paused);
1026 g_assert_true(job->job.busy); /* We're in qemu_co_sleep_ns() */
1027 }
7253220d 1028
191e7af3
EGE
1029 WITH_JOB_LOCK_GUARD() {
1030 ret = job_complete_sync_locked(&job->job, &error_abort);
1031 }
d49725af 1032 g_assert_cmpint(ret, ==, (result == TEST_JOB_SUCCESS ? 0 : -EIO));
7253220d 1033
6f592e5a 1034 aio_context_acquire(ctx);
f62c1729 1035 if (use_iothread) {
97896a48 1036 blk_set_aio_context(blk_src, qemu_get_aio_context(), &error_abort);
ad943dcb 1037 assert(blk_get_aio_context(blk_target) == qemu_get_aio_context());
f62c1729
KW
1038 }
1039 aio_context_release(ctx);
1040
7253220d
KW
1041 blk_unref(blk_src);
1042 blk_unref(blk_target);
d8b3afd5 1043 bdrv_unref(src_overlay);
7253220d 1044 bdrv_unref(target);
f62c1729
KW
1045
1046 if (iothread) {
1047 iothread_join(iothread);
1048 }
7253220d
KW
1049}
1050
d8b3afd5
KW
1051static void test_blockjob_common(enum drain_type drain_type, bool use_iothread,
1052 enum test_job_result result)
1053{
1054 test_blockjob_common_drain_node(drain_type, use_iothread, result,
1055 TEST_JOB_DRAIN_SRC);
1056 test_blockjob_common_drain_node(drain_type, use_iothread, result,
1057 TEST_JOB_DRAIN_SRC_CHILD);
1058 if (drain_type == BDRV_SUBTREE_DRAIN) {
1059 test_blockjob_common_drain_node(drain_type, use_iothread, result,
1060 TEST_JOB_DRAIN_SRC_PARENT);
1061 }
1062}
1063
7253220d
KW
1064static void test_blockjob_drain_all(void)
1065{
d49725af 1066 test_blockjob_common(BDRV_DRAIN_ALL, false, TEST_JOB_SUCCESS);
7253220d
KW
1067}
1068
1069static void test_blockjob_drain(void)
1070{
d49725af 1071 test_blockjob_common(BDRV_DRAIN, false, TEST_JOB_SUCCESS);
7253220d
KW
1072}
1073
d2a85d0f
KW
1074static void test_blockjob_drain_subtree(void)
1075{
d49725af
KW
1076 test_blockjob_common(BDRV_SUBTREE_DRAIN, false, TEST_JOB_SUCCESS);
1077}
1078
1079static void test_blockjob_error_drain_all(void)
1080{
1081 test_blockjob_common(BDRV_DRAIN_ALL, false, TEST_JOB_FAIL_RUN);
1082 test_blockjob_common(BDRV_DRAIN_ALL, false, TEST_JOB_FAIL_PREPARE);
1083}
1084
1085static void test_blockjob_error_drain(void)
1086{
1087 test_blockjob_common(BDRV_DRAIN, false, TEST_JOB_FAIL_RUN);
1088 test_blockjob_common(BDRV_DRAIN, false, TEST_JOB_FAIL_PREPARE);
1089}
1090
1091static void test_blockjob_error_drain_subtree(void)
1092{
1093 test_blockjob_common(BDRV_SUBTREE_DRAIN, false, TEST_JOB_FAIL_RUN);
1094 test_blockjob_common(BDRV_SUBTREE_DRAIN, false, TEST_JOB_FAIL_PREPARE);
f62c1729
KW
1095}
1096
1097static void test_blockjob_iothread_drain_all(void)
1098{
d49725af 1099 test_blockjob_common(BDRV_DRAIN_ALL, true, TEST_JOB_SUCCESS);
f62c1729
KW
1100}
1101
1102static void test_blockjob_iothread_drain(void)
1103{
d49725af 1104 test_blockjob_common(BDRV_DRAIN, true, TEST_JOB_SUCCESS);
f62c1729
KW
1105}
1106
1107static void test_blockjob_iothread_drain_subtree(void)
1108{
d49725af
KW
1109 test_blockjob_common(BDRV_SUBTREE_DRAIN, true, TEST_JOB_SUCCESS);
1110}
1111
1112static void test_blockjob_iothread_error_drain_all(void)
1113{
1114 test_blockjob_common(BDRV_DRAIN_ALL, true, TEST_JOB_FAIL_RUN);
1115 test_blockjob_common(BDRV_DRAIN_ALL, true, TEST_JOB_FAIL_PREPARE);
1116}
1117
1118static void test_blockjob_iothread_error_drain(void)
1119{
1120 test_blockjob_common(BDRV_DRAIN, true, TEST_JOB_FAIL_RUN);
1121 test_blockjob_common(BDRV_DRAIN, true, TEST_JOB_FAIL_PREPARE);
1122}
1123
1124static void test_blockjob_iothread_error_drain_subtree(void)
1125{
1126 test_blockjob_common(BDRV_SUBTREE_DRAIN, true, TEST_JOB_FAIL_RUN);
1127 test_blockjob_common(BDRV_SUBTREE_DRAIN, true, TEST_JOB_FAIL_PREPARE);
d2a85d0f
KW
1128}
1129
4c8158e3
HR
1130
1131typedef struct BDRVTestTopState {
1132 BdrvChild *wait_child;
1133} BDRVTestTopState;
1134
1135static void bdrv_test_top_close(BlockDriverState *bs)
1136{
1137 BdrvChild *c, *next_c;
1138 QLIST_FOREACH_SAFE(c, &bs->children, next, next_c) {
1139 bdrv_unref_child(bs, c);
1140 }
1141}
1142
1143static int coroutine_fn bdrv_test_top_co_preadv(BlockDriverState *bs,
f7ef38dd
VSO
1144 int64_t offset, int64_t bytes,
1145 QEMUIOVector *qiov,
1146 BdrvRequestFlags flags)
4c8158e3
HR
1147{
1148 BDRVTestTopState *tts = bs->opaque;
1149 return bdrv_co_preadv(tts->wait_child, offset, bytes, qiov, flags);
1150}
1151
1152static BlockDriver bdrv_test_top_driver = {
1153 .format_name = "test_top_driver",
1154 .instance_size = sizeof(BDRVTestTopState),
1155
1156 .bdrv_close = bdrv_test_top_close,
1157 .bdrv_co_preadv = bdrv_test_top_co_preadv,
1158
69dca43d 1159 .bdrv_child_perm = bdrv_default_perms,
4c8158e3
HR
1160};
1161
1162typedef struct TestCoDeleteByDrainData {
1163 BlockBackend *blk;
1164 bool detach_instead_of_delete;
1165 bool done;
1166} TestCoDeleteByDrainData;
1167
1168static void coroutine_fn test_co_delete_by_drain(void *opaque)
1169{
1170 TestCoDeleteByDrainData *dbdd = opaque;
1171 BlockBackend *blk = dbdd->blk;
1172 BlockDriverState *bs = blk_bs(blk);
1173 BDRVTestTopState *tts = bs->opaque;
1174 void *buffer = g_malloc(65536);
405d8fe0 1175 QEMUIOVector qiov = QEMU_IOVEC_INIT_BUF(qiov, buffer, 65536);
4c8158e3
HR
1176
1177 /* Pretend some internal write operation from parent to child.
1178 * Important: We have to read from the child, not from the parent!
1179 * Draining works by first propagating it all up the tree to the
1180 * root and then waiting for drainage from root to the leaves
1181 * (protocol nodes). If we have a request waiting on the root,
1182 * everything will be drained before we go back down the tree, but
1183 * we do not want that. We want to be in the middle of draining
1184 * when this following requests returns. */
1185 bdrv_co_preadv(tts->wait_child, 0, 65536, &qiov, 0);
1186
1187 g_assert_cmpint(bs->refcnt, ==, 1);
1188
1189 if (!dbdd->detach_instead_of_delete) {
1190 blk_unref(blk);
1191 } else {
1192 BdrvChild *c, *next_c;
1193 QLIST_FOREACH_SAFE(c, &bs->children, next, next_c) {
1194 bdrv_unref_child(bs, c);
1195 }
1196 }
1197
1198 dbdd->done = true;
7b43db3c 1199 g_free(buffer);
4c8158e3
HR
1200}
1201
1202/**
1203 * Test what happens when some BDS has some children, you drain one of
1204 * them and this results in the BDS being deleted.
1205 *
1206 * If @detach_instead_of_delete is set, the BDS is not going to be
1207 * deleted but will only detach all of its children.
1208 */
ebd31837
KW
1209static void do_test_delete_by_drain(bool detach_instead_of_delete,
1210 enum drain_type drain_type)
4c8158e3
HR
1211{
1212 BlockBackend *blk;
1213 BlockDriverState *bs, *child_bs, *null_bs;
1214 BDRVTestTopState *tts;
1215 TestCoDeleteByDrainData dbdd;
1216 Coroutine *co;
1217
1218 bs = bdrv_new_open_driver(&bdrv_test_top_driver, "top", BDRV_O_RDWR,
1219 &error_abort);
1220 bs->total_sectors = 65536 >> BDRV_SECTOR_BITS;
1221 tts = bs->opaque;
1222
1223 null_bs = bdrv_open("null-co://", NULL, NULL, BDRV_O_RDWR | BDRV_O_PROTOCOL,
1224 &error_abort);
a16be3cd
HR
1225 bdrv_attach_child(bs, null_bs, "null-child", &child_of_bds,
1226 BDRV_CHILD_DATA, &error_abort);
4c8158e3
HR
1227
1228 /* This child will be the one to pass to requests through to, and
1229 * it will stall until a drain occurs */
1230 child_bs = bdrv_new_open_driver(&bdrv_test, "child", BDRV_O_RDWR,
1231 &error_abort);
1232 child_bs->total_sectors = 65536 >> BDRV_SECTOR_BITS;
1233 /* Takes our reference to child_bs */
a16be3cd
HR
1234 tts->wait_child = bdrv_attach_child(bs, child_bs, "wait-child",
1235 &child_of_bds,
1236 BDRV_CHILD_DATA | BDRV_CHILD_PRIMARY,
1237 &error_abort);
4c8158e3
HR
1238
1239 /* This child is just there to be deleted
1240 * (for detach_instead_of_delete == true) */
1241 null_bs = bdrv_open("null-co://", NULL, NULL, BDRV_O_RDWR | BDRV_O_PROTOCOL,
1242 &error_abort);
a16be3cd
HR
1243 bdrv_attach_child(bs, null_bs, "null-child", &child_of_bds, BDRV_CHILD_DATA,
1244 &error_abort);
4c8158e3 1245
d861ab3a 1246 blk = blk_new(qemu_get_aio_context(), BLK_PERM_ALL, BLK_PERM_ALL);
4c8158e3
HR
1247 blk_insert_bs(blk, bs, &error_abort);
1248
1249 /* Referenced by blk now */
1250 bdrv_unref(bs);
1251
1252 g_assert_cmpint(bs->refcnt, ==, 1);
1253 g_assert_cmpint(child_bs->refcnt, ==, 1);
1254 g_assert_cmpint(null_bs->refcnt, ==, 1);
1255
1256
1257 dbdd = (TestCoDeleteByDrainData){
1258 .blk = blk,
1259 .detach_instead_of_delete = detach_instead_of_delete,
1260 .done = false,
1261 };
1262 co = qemu_coroutine_create(test_co_delete_by_drain, &dbdd);
1263 qemu_coroutine_enter(co);
1264
1265 /* Drain the child while the read operation is still pending.
1266 * This should result in the operation finishing and
1267 * test_co_delete_by_drain() resuming. Thus, @bs will be deleted
1268 * and the coroutine will exit while this drain operation is still
1269 * in progress. */
ebd31837
KW
1270 switch (drain_type) {
1271 case BDRV_DRAIN:
1272 bdrv_ref(child_bs);
1273 bdrv_drain(child_bs);
1274 bdrv_unref(child_bs);
1275 break;
1276 case BDRV_SUBTREE_DRAIN:
1277 /* Would have to ref/unref bs here for !detach_instead_of_delete, but
1278 * then the whole test becomes pointless because the graph changes
1279 * don't occur during the drain any more. */
1280 assert(detach_instead_of_delete);
1281 bdrv_subtree_drained_begin(bs);
1282 bdrv_subtree_drained_end(bs);
1283 break;
19f7a7e5
KW
1284 case BDRV_DRAIN_ALL:
1285 bdrv_drain_all_begin();
1286 bdrv_drain_all_end();
1287 break;
ebd31837
KW
1288 default:
1289 g_assert_not_reached();
1290 }
4c8158e3
HR
1291
1292 while (!dbdd.done) {
1293 aio_poll(qemu_get_aio_context(), true);
1294 }
1295
1296 if (detach_instead_of_delete) {
1297 /* Here, the reference has not passed over to the coroutine,
1298 * so we have to delete the BB ourselves */
1299 blk_unref(blk);
1300 }
1301}
1302
4c8158e3
HR
1303static void test_delete_by_drain(void)
1304{
ebd31837 1305 do_test_delete_by_drain(false, BDRV_DRAIN);
4c8158e3
HR
1306}
1307
19f7a7e5
KW
1308static void test_detach_by_drain_all(void)
1309{
1310 do_test_delete_by_drain(true, BDRV_DRAIN_ALL);
1311}
1312
4c8158e3
HR
1313static void test_detach_by_drain(void)
1314{
ebd31837
KW
1315 do_test_delete_by_drain(true, BDRV_DRAIN);
1316}
1317
1318static void test_detach_by_drain_subtree(void)
1319{
1320 do_test_delete_by_drain(true, BDRV_SUBTREE_DRAIN);
4c8158e3
HR
1321}
1322
1323
231281ab
KW
1324struct detach_by_parent_data {
1325 BlockDriverState *parent_b;
1326 BdrvChild *child_b;
1327 BlockDriverState *c;
1328 BdrvChild *child_c;
57320ca9 1329 bool by_parent_cb;
231281ab 1330};
57320ca9 1331static struct detach_by_parent_data detach_by_parent_data;
231281ab 1332
57320ca9 1333static void detach_indirect_bh(void *opaque)
231281ab
KW
1334{
1335 struct detach_by_parent_data *data = opaque;
1336
231281ab
KW
1337 bdrv_unref_child(data->parent_b, data->child_b);
1338
1339 bdrv_ref(data->c);
1340 data->child_c = bdrv_attach_child(data->parent_b, data->c, "PB-C",
a16be3cd
HR
1341 &child_of_bds, BDRV_CHILD_DATA,
1342 &error_abort);
231281ab
KW
1343}
1344
57320ca9
KW
1345static void detach_by_parent_aio_cb(void *opaque, int ret)
1346{
1347 struct detach_by_parent_data *data = &detach_by_parent_data;
1348
1349 g_assert_cmpint(ret, ==, 0);
1350 if (data->by_parent_cb) {
1351 detach_indirect_bh(data);
1352 }
1353}
1354
1355static void detach_by_driver_cb_drained_begin(BdrvChild *child)
1356{
1357 aio_bh_schedule_oneshot(qemu_get_current_aio_context(),
1358 detach_indirect_bh, &detach_by_parent_data);
a16be3cd 1359 child_of_bds.drained_begin(child);
57320ca9
KW
1360}
1361
bd86fb99 1362static BdrvChildClass detach_by_driver_cb_class;
57320ca9 1363
231281ab
KW
1364/*
1365 * Initial graph:
1366 *
1367 * PA PB
1368 * \ / \
1369 * A B C
1370 *
57320ca9
KW
1371 * by_parent_cb == true: Test that parent callbacks don't poll
1372 *
1373 * PA has a pending write request whose callback changes the child nodes of
1374 * PB: It removes B and adds C instead. The subtree of PB is drained, which
1375 * will indirectly drain the write request, too.
1376 *
1377 * by_parent_cb == false: Test that bdrv_drain_invoke() doesn't poll
1378 *
bd86fb99 1379 * PA's BdrvChildClass has a .drained_begin callback that schedules a BH
57320ca9
KW
1380 * that does the same graph change. If bdrv_drain_invoke() calls it, the
1381 * state is messed up, but if it is only polled in the single
1382 * BDRV_POLL_WHILE() at the end of the drain, this should work fine.
231281ab 1383 */
57320ca9 1384static void test_detach_indirect(bool by_parent_cb)
231281ab
KW
1385{
1386 BlockBackend *blk;
1387 BlockDriverState *parent_a, *parent_b, *a, *b, *c;
1388 BdrvChild *child_a, *child_b;
1389 BlockAIOCB *acb;
231281ab 1390
405d8fe0 1391 QEMUIOVector qiov = QEMU_IOVEC_INIT_BUF(qiov, NULL, 0);
231281ab 1392
57320ca9 1393 if (!by_parent_cb) {
a16be3cd 1394 detach_by_driver_cb_class = child_of_bds;
bd86fb99 1395 detach_by_driver_cb_class.drained_begin =
57320ca9
KW
1396 detach_by_driver_cb_drained_begin;
1397 }
1398
231281ab
KW
1399 /* Create all involved nodes */
1400 parent_a = bdrv_new_open_driver(&bdrv_test, "parent-a", BDRV_O_RDWR,
1401 &error_abort);
1402 parent_b = bdrv_new_open_driver(&bdrv_test, "parent-b", 0,
1403 &error_abort);
1404
1405 a = bdrv_new_open_driver(&bdrv_test, "a", BDRV_O_RDWR, &error_abort);
1406 b = bdrv_new_open_driver(&bdrv_test, "b", BDRV_O_RDWR, &error_abort);
1407 c = bdrv_new_open_driver(&bdrv_test, "c", BDRV_O_RDWR, &error_abort);
1408
1409 /* blk is a BB for parent-a */
d861ab3a 1410 blk = blk_new(qemu_get_aio_context(), BLK_PERM_ALL, BLK_PERM_ALL);
231281ab
KW
1411 blk_insert_bs(blk, parent_a, &error_abort);
1412 bdrv_unref(parent_a);
1413
57320ca9
KW
1414 /* If we want to get bdrv_drain_invoke() to call aio_poll(), the driver
1415 * callback must not return immediately. */
1416 if (!by_parent_cb) {
1417 BDRVTestState *s = parent_a->opaque;
1418 s->sleep_in_drain_begin = true;
1419 }
1420
231281ab
KW
1421 /* Set child relationships */
1422 bdrv_ref(b);
1423 bdrv_ref(a);
a16be3cd
HR
1424 child_b = bdrv_attach_child(parent_b, b, "PB-B", &child_of_bds,
1425 BDRV_CHILD_DATA, &error_abort);
25191e5f
HR
1426 child_a = bdrv_attach_child(parent_b, a, "PB-A", &child_of_bds,
1427 BDRV_CHILD_COW, &error_abort);
231281ab
KW
1428
1429 bdrv_ref(a);
57320ca9 1430 bdrv_attach_child(parent_a, a, "PA-A",
a16be3cd
HR
1431 by_parent_cb ? &child_of_bds : &detach_by_driver_cb_class,
1432 BDRV_CHILD_DATA, &error_abort);
231281ab
KW
1433
1434 g_assert_cmpint(parent_a->refcnt, ==, 1);
1435 g_assert_cmpint(parent_b->refcnt, ==, 1);
1436 g_assert_cmpint(a->refcnt, ==, 3);
1437 g_assert_cmpint(b->refcnt, ==, 2);
1438 g_assert_cmpint(c->refcnt, ==, 1);
1439
1440 g_assert(QLIST_FIRST(&parent_b->children) == child_a);
1441 g_assert(QLIST_NEXT(child_a, next) == child_b);
1442 g_assert(QLIST_NEXT(child_b, next) == NULL);
1443
1444 /* Start the evil write request */
57320ca9 1445 detach_by_parent_data = (struct detach_by_parent_data) {
231281ab
KW
1446 .parent_b = parent_b,
1447 .child_b = child_b,
1448 .c = c,
57320ca9 1449 .by_parent_cb = by_parent_cb,
231281ab 1450 };
57320ca9 1451 acb = blk_aio_preadv(blk, 0, &qiov, 0, detach_by_parent_aio_cb, NULL);
231281ab
KW
1452 g_assert(acb != NULL);
1453
1454 /* Drain and check the expected result */
1455 bdrv_subtree_drained_begin(parent_b);
1456
57320ca9 1457 g_assert(detach_by_parent_data.child_c != NULL);
231281ab
KW
1458
1459 g_assert_cmpint(parent_a->refcnt, ==, 1);
1460 g_assert_cmpint(parent_b->refcnt, ==, 1);
1461 g_assert_cmpint(a->refcnt, ==, 3);
1462 g_assert_cmpint(b->refcnt, ==, 1);
1463 g_assert_cmpint(c->refcnt, ==, 2);
1464
57320ca9
KW
1465 g_assert(QLIST_FIRST(&parent_b->children) == detach_by_parent_data.child_c);
1466 g_assert(QLIST_NEXT(detach_by_parent_data.child_c, next) == child_a);
231281ab
KW
1467 g_assert(QLIST_NEXT(child_a, next) == NULL);
1468
1469 g_assert_cmpint(parent_a->quiesce_counter, ==, 1);
1470 g_assert_cmpint(parent_b->quiesce_counter, ==, 1);
1471 g_assert_cmpint(a->quiesce_counter, ==, 1);
1472 g_assert_cmpint(b->quiesce_counter, ==, 0);
1473 g_assert_cmpint(c->quiesce_counter, ==, 1);
1474
1475 bdrv_subtree_drained_end(parent_b);
1476
1477 bdrv_unref(parent_b);
1478 blk_unref(blk);
1479
231281ab
KW
1480 g_assert_cmpint(a->refcnt, ==, 1);
1481 g_assert_cmpint(b->refcnt, ==, 1);
1482 g_assert_cmpint(c->refcnt, ==, 1);
1483 bdrv_unref(a);
1484 bdrv_unref(b);
1485 bdrv_unref(c);
1486}
1487
57320ca9
KW
1488static void test_detach_by_parent_cb(void)
1489{
1490 test_detach_indirect(true);
1491}
1492
1493static void test_detach_by_driver_cb(void)
1494{
1495 test_detach_indirect(false);
1496}
231281ab 1497
b994c5bc
KW
1498static void test_append_to_drained(void)
1499{
1500 BlockBackend *blk;
1501 BlockDriverState *base, *overlay;
1502 BDRVTestState *base_s, *overlay_s;
1503
d861ab3a 1504 blk = blk_new(qemu_get_aio_context(), BLK_PERM_ALL, BLK_PERM_ALL);
b994c5bc
KW
1505 base = bdrv_new_open_driver(&bdrv_test, "base", BDRV_O_RDWR, &error_abort);
1506 base_s = base->opaque;
1507 blk_insert_bs(blk, base, &error_abort);
1508
1509 overlay = bdrv_new_open_driver(&bdrv_test, "overlay", BDRV_O_RDWR,
1510 &error_abort);
1511 overlay_s = overlay->opaque;
1512
1513 do_drain_begin(BDRV_DRAIN, base);
1514 g_assert_cmpint(base->quiesce_counter, ==, 1);
1515 g_assert_cmpint(base_s->drain_count, ==, 1);
1516 g_assert_cmpint(base->in_flight, ==, 0);
1517
b994c5bc
KW
1518 bdrv_append(overlay, base, &error_abort);
1519 g_assert_cmpint(base->in_flight, ==, 0);
1520 g_assert_cmpint(overlay->in_flight, ==, 0);
1521
1522 g_assert_cmpint(base->quiesce_counter, ==, 1);
1523 g_assert_cmpint(base_s->drain_count, ==, 1);
1524 g_assert_cmpint(overlay->quiesce_counter, ==, 1);
1525 g_assert_cmpint(overlay_s->drain_count, ==, 1);
1526
1527 do_drain_end(BDRV_DRAIN, base);
1528
1529 g_assert_cmpint(base->quiesce_counter, ==, 0);
1530 g_assert_cmpint(base_s->drain_count, ==, 0);
1531 g_assert_cmpint(overlay->quiesce_counter, ==, 0);
1532 g_assert_cmpint(overlay_s->drain_count, ==, 0);
1533
ae9d4417 1534 bdrv_unref(overlay);
b994c5bc
KW
1535 bdrv_unref(base);
1536 blk_unref(blk);
1537}
1538
247d2737
KW
1539static void test_set_aio_context(void)
1540{
1541 BlockDriverState *bs;
1542 IOThread *a = iothread_new();
1543 IOThread *b = iothread_new();
1544 AioContext *ctx_a = iothread_get_aio_context(a);
1545 AioContext *ctx_b = iothread_get_aio_context(b);
1546
1547 bs = bdrv_new_open_driver(&bdrv_test, "test-node", BDRV_O_RDWR,
1548 &error_abort);
1549
1550 bdrv_drained_begin(bs);
142e6907 1551 bdrv_try_change_aio_context(bs, ctx_a, NULL, &error_abort);
247d2737
KW
1552
1553 aio_context_acquire(ctx_a);
1554 bdrv_drained_end(bs);
1555
1556 bdrv_drained_begin(bs);
142e6907 1557 bdrv_try_change_aio_context(bs, ctx_b, NULL, &error_abort);
247d2737
KW
1558 aio_context_release(ctx_a);
1559 aio_context_acquire(ctx_b);
142e6907 1560 bdrv_try_change_aio_context(bs, qemu_get_aio_context(), NULL, &error_abort);
247d2737
KW
1561 aio_context_release(ctx_b);
1562 bdrv_drained_end(bs);
1563
1564 bdrv_unref(bs);
1565 iothread_join(a);
1566 iothread_join(b);
1567}
1568
8e442810
HR
1569
1570typedef struct TestDropBackingBlockJob {
1571 BlockJob common;
1572 bool should_complete;
1573 bool *did_complete;
2afdc790 1574 BlockDriverState *detach_also;
1b177bbe 1575 BlockDriverState *bs;
8e442810
HR
1576} TestDropBackingBlockJob;
1577
1578static int coroutine_fn test_drop_backing_job_run(Job *job, Error **errp)
1579{
1580 TestDropBackingBlockJob *s =
1581 container_of(job, TestDropBackingBlockJob, common.job);
1582
1583 while (!s->should_complete) {
1584 job_sleep_ns(job, 0);
1585 }
1586
1587 return 0;
1588}
1589
1590static void test_drop_backing_job_commit(Job *job)
1591{
1592 TestDropBackingBlockJob *s =
1593 container_of(job, TestDropBackingBlockJob, common.job);
1594
1b177bbe 1595 bdrv_set_backing_hd(s->bs, NULL, &error_abort);
2afdc790 1596 bdrv_set_backing_hd(s->detach_also, NULL, &error_abort);
8e442810
HR
1597
1598 *s->did_complete = true;
1599}
1600
1601static const BlockJobDriver test_drop_backing_job_driver = {
1602 .job_driver = {
1603 .instance_size = sizeof(TestDropBackingBlockJob),
1604 .free = block_job_free,
1605 .user_resume = block_job_user_resume,
8e442810
HR
1606 .run = test_drop_backing_job_run,
1607 .commit = test_drop_backing_job_commit,
1608 }
1609};
1610
1611/**
1612 * Creates a child node with three parent nodes on it, and then runs a
1613 * block job on the final one, parent-node-2.
1614 *
8e442810
HR
1615 * The job is then asked to complete before a section where the child
1616 * is drained.
1617 *
1618 * Ending this section will undrain the child's parents, first
1619 * parent-node-2, then parent-node-1, then parent-node-0 -- the parent
1620 * list is in reverse order of how they were added. Ending the drain
1621 * on parent-node-2 will resume the job, thus completing it and
1622 * scheduling job_exit().
1623 *
1624 * Ending the drain on parent-node-1 will poll the AioContext, which
1625 * lets job_exit() and thus test_drop_backing_job_commit() run. That
2afdc790 1626 * function first removes the child as parent-node-2's backing file.
8e442810
HR
1627 *
1628 * In old (and buggy) implementations, there are two problems with
1629 * that:
1630 * (A) bdrv_drain_invoke() polls for every node that leaves the
1631 * drained section. This means that job_exit() is scheduled
1632 * before the child has left the drained section. Its
1633 * quiesce_counter is therefore still 1 when it is removed from
1634 * parent-node-2.
1635 *
1636 * (B) bdrv_replace_child_noperm() calls drained_end() on the old
1637 * child's parents as many times as the child is quiesced. This
1638 * means it will call drained_end() on parent-node-2 once.
1639 * Because parent-node-2 is no longer quiesced at this point, this
1640 * will fail.
1641 *
1642 * bdrv_replace_child_noperm() therefore must call drained_end() on
1643 * the parent only if it really is still drained because the child is
1644 * drained.
2afdc790
HR
1645 *
1646 * If removing child from parent-node-2 was successful (as it should
1647 * be), test_drop_backing_job_commit() will then also remove the child
1648 * from parent-node-0.
1649 *
1650 * With an old version of our drain infrastructure ((A) above), that
1651 * resulted in the following flow:
1652 *
1653 * 1. child attempts to leave its drained section. The call recurses
1654 * to its parents.
1655 *
1656 * 2. parent-node-2 leaves the drained section. Polling in
1657 * bdrv_drain_invoke() will schedule job_exit().
1658 *
1659 * 3. parent-node-1 leaves the drained section. Polling in
1660 * bdrv_drain_invoke() will run job_exit(), thus disconnecting
1661 * parent-node-0 from the child node.
1662 *
1663 * 4. bdrv_parent_drained_end() uses a QLIST_FOREACH_SAFE() loop to
1664 * iterate over the parents. Thus, it now accesses the BdrvChild
1665 * object that used to connect parent-node-0 and the child node.
1666 * However, that object no longer exists, so it accesses a dangling
1667 * pointer.
1668 *
1669 * The solution is to only poll once when running a bdrv_drained_end()
1670 * operation, specifically at the end when all drained_end()
1671 * operations for all involved nodes have been scheduled.
1672 * Note that this also solves (A) above, thus hiding (B).
8e442810
HR
1673 */
1674static void test_blockjob_commit_by_drained_end(void)
1675{
1676 BlockDriverState *bs_child, *bs_parents[3];
1677 TestDropBackingBlockJob *job;
1678 bool job_has_completed = false;
1679 int i;
1680
1681 bs_child = bdrv_new_open_driver(&bdrv_test, "child-node", BDRV_O_RDWR,
1682 &error_abort);
1683
1684 for (i = 0; i < 3; i++) {
1685 char name[32];
1686 snprintf(name, sizeof(name), "parent-node-%i", i);
1687 bs_parents[i] = bdrv_new_open_driver(&bdrv_test, name, BDRV_O_RDWR,
1688 &error_abort);
1689 bdrv_set_backing_hd(bs_parents[i], bs_child, &error_abort);
1690 }
1691
1692 job = block_job_create("job", &test_drop_backing_job_driver, NULL,
1693 bs_parents[2], 0, BLK_PERM_ALL, 0, 0, NULL, NULL,
1694 &error_abort);
1b177bbe 1695 job->bs = bs_parents[2];
8e442810 1696
2afdc790 1697 job->detach_also = bs_parents[0];
8e442810
HR
1698 job->did_complete = &job_has_completed;
1699
1700 job_start(&job->common.job);
1701
1702 job->should_complete = true;
1703 bdrv_drained_begin(bs_child);
1704 g_assert(!job_has_completed);
1705 bdrv_drained_end(bs_child);
1706 g_assert(job_has_completed);
1707
1708 bdrv_unref(bs_parents[0]);
1709 bdrv_unref(bs_parents[1]);
1710 bdrv_unref(bs_parents[2]);
1711 bdrv_unref(bs_child);
1712}
1713
9746b35c
HR
1714
1715typedef struct TestSimpleBlockJob {
1716 BlockJob common;
1717 bool should_complete;
1718 bool *did_complete;
1719} TestSimpleBlockJob;
1720
1721static int coroutine_fn test_simple_job_run(Job *job, Error **errp)
1722{
1723 TestSimpleBlockJob *s = container_of(job, TestSimpleBlockJob, common.job);
1724
1725 while (!s->should_complete) {
1726 job_sleep_ns(job, 0);
1727 }
1728
1729 return 0;
1730}
1731
1732static void test_simple_job_clean(Job *job)
1733{
1734 TestSimpleBlockJob *s = container_of(job, TestSimpleBlockJob, common.job);
1735 *s->did_complete = true;
1736}
1737
1738static const BlockJobDriver test_simple_job_driver = {
1739 .job_driver = {
1740 .instance_size = sizeof(TestSimpleBlockJob),
1741 .free = block_job_free,
1742 .user_resume = block_job_user_resume,
9746b35c
HR
1743 .run = test_simple_job_run,
1744 .clean = test_simple_job_clean,
1745 },
1746};
1747
1748static int drop_intermediate_poll_update_filename(BdrvChild *child,
1749 BlockDriverState *new_base,
1750 const char *filename,
1751 Error **errp)
1752{
1753 /*
1754 * We are free to poll here, which may change the block graph, if
1755 * it is not drained.
1756 */
1757
1758 /* If the job is not drained: Complete it, schedule job_exit() */
1759 aio_poll(qemu_get_current_aio_context(), false);
1760 /* If the job is not drained: Run job_exit(), finish the job */
1761 aio_poll(qemu_get_current_aio_context(), false);
1762
1763 return 0;
1764}
1765
1766/**
1767 * Test a poll in the midst of bdrv_drop_intermediate().
1768 *
bd86fb99 1769 * bdrv_drop_intermediate() calls BdrvChildClass.update_filename(),
9746b35c
HR
1770 * which can yield or poll. This may lead to graph changes, unless
1771 * the whole subtree in question is drained.
1772 *
1773 * We test this on the following graph:
1774 *
1775 * Job
1776 *
1777 * |
1778 * job-node
1779 * |
1780 * v
1781 *
1782 * job-node
1783 *
1784 * |
1785 * backing
1786 * |
1787 * v
1788 *
1789 * node-2 --chain--> node-1 --chain--> node-0
1790 *
1791 * We drop node-1 with bdrv_drop_intermediate(top=node-1, base=node-0).
1792 *
1793 * This first updates node-2's backing filename by invoking
1794 * drop_intermediate_poll_update_filename(), which polls twice. This
1795 * causes the job to finish, which in turns causes the job-node to be
1796 * deleted.
1797 *
1798 * bdrv_drop_intermediate() uses a QLIST_FOREACH_SAFE() loop, so it
1799 * already has a pointer to the BdrvChild edge between job-node and
1800 * node-1. When it tries to handle that edge, we probably get a
1801 * segmentation fault because the object no longer exists.
1802 *
1803 *
1804 * The solution is for bdrv_drop_intermediate() to drain top's
1805 * subtree. This prevents graph changes from happening just because
bd86fb99 1806 * BdrvChildClass.update_filename() yields or polls. Thus, the block
9746b35c
HR
1807 * job is paused during that drained section and must finish before or
1808 * after.
1809 *
1810 * (In addition, bdrv_replace_child() must keep the job paused.)
1811 */
1812static void test_drop_intermediate_poll(void)
1813{
bd86fb99 1814 static BdrvChildClass chain_child_class;
9746b35c
HR
1815 BlockDriverState *chain[3];
1816 TestSimpleBlockJob *job;
1817 BlockDriverState *job_node;
1818 bool job_has_completed = false;
1819 int i;
1820 int ret;
1821
25191e5f 1822 chain_child_class = child_of_bds;
bd86fb99 1823 chain_child_class.update_filename = drop_intermediate_poll_update_filename;
9746b35c
HR
1824
1825 for (i = 0; i < 3; i++) {
1826 char name[32];
1827 snprintf(name, 32, "node-%i", i);
1828
1829 chain[i] = bdrv_new_open_driver(&bdrv_test, name, 0, &error_abort);
1830 }
1831
1832 job_node = bdrv_new_open_driver(&bdrv_test, "job-node", BDRV_O_RDWR,
1833 &error_abort);
1834 bdrv_set_backing_hd(job_node, chain[1], &error_abort);
1835
1836 /*
1837 * Establish the chain last, so the chain links are the first
1838 * elements in the BDS.parents lists
1839 */
1840 for (i = 0; i < 3; i++) {
1841 if (i) {
1842 /* Takes the reference to chain[i - 1] */
5bb04747
VSO
1843 bdrv_attach_child(chain[i], chain[i - 1], "chain",
1844 &chain_child_class, BDRV_CHILD_COW, &error_abort);
9746b35c
HR
1845 }
1846 }
1847
1848 job = block_job_create("job", &test_simple_job_driver, NULL, job_node,
1849 0, BLK_PERM_ALL, 0, 0, NULL, NULL, &error_abort);
1850
1851 /* The job has a reference now */
1852 bdrv_unref(job_node);
1853
1854 job->did_complete = &job_has_completed;
1855
1856 job_start(&job->common.job);
1857 job->should_complete = true;
1858
1859 g_assert(!job_has_completed);
1860 ret = bdrv_drop_intermediate(chain[1], chain[0], NULL);
1861 g_assert(ret == 0);
1862 g_assert(job_has_completed);
1863
1864 bdrv_unref(chain[2]);
1865}
1866
0513f984
HR
1867
1868typedef struct BDRVReplaceTestState {
1869 bool was_drained;
1870 bool was_undrained;
1871 bool has_read;
1872
1873 int drain_count;
1874
1875 bool yield_before_read;
1876 Coroutine *io_co;
1877 Coroutine *drain_co;
1878} BDRVReplaceTestState;
1879
1880static void bdrv_replace_test_close(BlockDriverState *bs)
1881{
1882}
1883
1884/**
1885 * If @bs has a backing file:
1886 * Yield if .yield_before_read is true (and wait for drain_begin to
1887 * wake us up).
1888 * Forward the read to bs->backing. Set .has_read to true.
1889 * If drain_begin has woken us, wake it in turn.
1890 *
1891 * Otherwise:
1892 * Set .has_read to true and return success.
1893 */
1894static int coroutine_fn bdrv_replace_test_co_preadv(BlockDriverState *bs,
f7ef38dd
VSO
1895 int64_t offset,
1896 int64_t bytes,
0513f984 1897 QEMUIOVector *qiov,
f7ef38dd 1898 BdrvRequestFlags flags)
0513f984
HR
1899{
1900 BDRVReplaceTestState *s = bs->opaque;
1901
1902 if (bs->backing) {
1903 int ret;
1904
1905 g_assert(!s->drain_count);
1906
1907 s->io_co = qemu_coroutine_self();
1908 if (s->yield_before_read) {
1909 s->yield_before_read = false;
1910 qemu_coroutine_yield();
1911 }
1912 s->io_co = NULL;
1913
fae2681a 1914 ret = bdrv_co_preadv(bs->backing, offset, bytes, qiov, 0);
0513f984
HR
1915 s->has_read = true;
1916
1917 /* Wake up drain_co if it runs */
1918 if (s->drain_co) {
1919 aio_co_wake(s->drain_co);
1920 }
1921
1922 return ret;
1923 }
1924
1925 s->has_read = true;
1926 return 0;
1927}
1928
7bce1c29
KW
1929static void coroutine_fn bdrv_replace_test_drain_co(void *opaque)
1930{
1931 BlockDriverState *bs = opaque;
1932 BDRVReplaceTestState *s = bs->opaque;
1933
1934 /* Keep waking io_co up until it is done */
1935 while (s->io_co) {
1936 aio_co_wake(s->io_co);
1937 s->io_co = NULL;
1938 qemu_coroutine_yield();
1939 }
1940 s->drain_co = NULL;
1941 bdrv_dec_in_flight(bs);
1942}
1943
0513f984
HR
1944/**
1945 * If .drain_count is 0, wake up .io_co if there is one; and set
1946 * .was_drained.
1947 * Increment .drain_count.
1948 */
1949static void coroutine_fn bdrv_replace_test_co_drain_begin(BlockDriverState *bs)
1950{
1951 BDRVReplaceTestState *s = bs->opaque;
1952
1953 if (!s->drain_count) {
7bce1c29
KW
1954 s->drain_co = qemu_coroutine_create(bdrv_replace_test_drain_co, bs);
1955 bdrv_inc_in_flight(bs);
1956 aio_co_enter(bdrv_get_aio_context(bs), s->drain_co);
0513f984
HR
1957 s->was_drained = true;
1958 }
1959 s->drain_count++;
1960}
1961
7bce1c29
KW
1962static void coroutine_fn bdrv_replace_test_read_entry(void *opaque)
1963{
1964 BlockDriverState *bs = opaque;
1965 char data;
1966 QEMUIOVector qiov = QEMU_IOVEC_INIT_BUF(qiov, &data, 1);
1967 int ret;
1968
1969 /* Queue a read request post-drain */
1970 ret = bdrv_replace_test_co_preadv(bs, 0, 1, &qiov, 0);
1971 g_assert(ret >= 0);
1972 bdrv_dec_in_flight(bs);
1973}
1974
0513f984
HR
1975/**
1976 * Reduce .drain_count, set .was_undrained once it reaches 0.
1977 * If .drain_count reaches 0 and the node has a backing file, issue a
1978 * read request.
1979 */
1980static void coroutine_fn bdrv_replace_test_co_drain_end(BlockDriverState *bs)
1981{
1982 BDRVReplaceTestState *s = bs->opaque;
1983
1984 g_assert(s->drain_count > 0);
1985 if (!--s->drain_count) {
0513f984
HR
1986 s->was_undrained = true;
1987
1988 if (bs->backing) {
7bce1c29
KW
1989 Coroutine *co = qemu_coroutine_create(bdrv_replace_test_read_entry,
1990 bs);
1991 bdrv_inc_in_flight(bs);
1992 aio_co_enter(bdrv_get_aio_context(bs), co);
0513f984
HR
1993 }
1994 }
1995}
1996
1997static BlockDriver bdrv_replace_test = {
1998 .format_name = "replace_test",
1999 .instance_size = sizeof(BDRVReplaceTestState),
9ebfc111 2000 .supports_backing = true,
0513f984
HR
2001
2002 .bdrv_close = bdrv_replace_test_close,
2003 .bdrv_co_preadv = bdrv_replace_test_co_preadv,
2004
2005 .bdrv_co_drain_begin = bdrv_replace_test_co_drain_begin,
2006 .bdrv_co_drain_end = bdrv_replace_test_co_drain_end,
2007
69dca43d 2008 .bdrv_child_perm = bdrv_default_perms,
0513f984
HR
2009};
2010
2011static void coroutine_fn test_replace_child_mid_drain_read_co(void *opaque)
2012{
2013 int ret;
2014 char data;
2015
2016 ret = blk_co_pread(opaque, 0, 1, &data, 0);
2017 g_assert(ret >= 0);
2018}
2019
2020/**
2021 * We test two things:
2022 * (1) bdrv_replace_child_noperm() must not undrain the parent if both
2023 * children are drained.
2024 * (2) bdrv_replace_child_noperm() must never flush I/O requests to a
2025 * drained child. If the old child is drained, it must flush I/O
2026 * requests after the new one has been attached. If the new child
2027 * is drained, it must flush I/O requests before the old one is
2028 * detached.
2029 *
2030 * To do so, we create one parent node and two child nodes; then
2031 * attach one of the children (old_child_bs) to the parent, then
2032 * drain both old_child_bs and new_child_bs according to
2033 * old_drain_count and new_drain_count, respectively, and finally
2034 * we invoke bdrv_replace_node() to replace old_child_bs by
2035 * new_child_bs.
2036 *
2037 * The test block driver we use here (bdrv_replace_test) has a read
2038 * function that:
2039 * - For the parent node, can optionally yield, and then forwards the
2040 * read to bdrv_preadv(),
2041 * - For the child node, just returns immediately.
2042 *
2043 * If the read yields, the drain_begin function will wake it up.
2044 *
2045 * The drain_end function issues a read on the parent once it is fully
2046 * undrained (which simulates requests starting to come in again).
2047 */
2048static void do_test_replace_child_mid_drain(int old_drain_count,
2049 int new_drain_count)
2050{
2051 BlockBackend *parent_blk;
2052 BlockDriverState *parent_bs;
2053 BlockDriverState *old_child_bs, *new_child_bs;
2054 BDRVReplaceTestState *parent_s;
2055 BDRVReplaceTestState *old_child_s, *new_child_s;
2056 Coroutine *io_co;
2057 int i;
2058
2059 parent_bs = bdrv_new_open_driver(&bdrv_replace_test, "parent", 0,
2060 &error_abort);
2061 parent_s = parent_bs->opaque;
2062
2063 parent_blk = blk_new(qemu_get_aio_context(),
2064 BLK_PERM_CONSISTENT_READ, BLK_PERM_ALL);
2065 blk_insert_bs(parent_blk, parent_bs, &error_abort);
2066
2067 old_child_bs = bdrv_new_open_driver(&bdrv_replace_test, "old-child", 0,
2068 &error_abort);
2069 new_child_bs = bdrv_new_open_driver(&bdrv_replace_test, "new-child", 0,
2070 &error_abort);
2071 old_child_s = old_child_bs->opaque;
2072 new_child_s = new_child_bs->opaque;
2073
2074 /* So that we can read something */
2075 parent_bs->total_sectors = 1;
2076 old_child_bs->total_sectors = 1;
2077 new_child_bs->total_sectors = 1;
2078
2079 bdrv_ref(old_child_bs);
5bb04747
VSO
2080 bdrv_attach_child(parent_bs, old_child_bs, "child", &child_of_bds,
2081 BDRV_CHILD_COW, &error_abort);
0513f984
HR
2082
2083 for (i = 0; i < old_drain_count; i++) {
2084 bdrv_drained_begin(old_child_bs);
2085 }
2086 for (i = 0; i < new_drain_count; i++) {
2087 bdrv_drained_begin(new_child_bs);
2088 }
2089
2090 if (!old_drain_count) {
2091 /*
2092 * Start a read operation that will yield, so it will not
2093 * complete before the node is drained.
2094 */
2095 parent_s->yield_before_read = true;
2096 io_co = qemu_coroutine_create(test_replace_child_mid_drain_read_co,
2097 parent_blk);
2098 qemu_coroutine_enter(io_co);
2099 }
2100
2101 /* If we have started a read operation, it should have yielded */
2102 g_assert(!parent_s->has_read);
2103
2104 /* Reset drained status so we can see what bdrv_replace_node() does */
2105 parent_s->was_drained = false;
2106 parent_s->was_undrained = false;
2107
2108 g_assert(parent_bs->quiesce_counter == old_drain_count);
2109 bdrv_replace_node(old_child_bs, new_child_bs, &error_abort);
2110 g_assert(parent_bs->quiesce_counter == new_drain_count);
2111
2112 if (!old_drain_count && !new_drain_count) {
2113 /*
2114 * From undrained to undrained drains and undrains the parent,
2115 * because bdrv_replace_node() contains a drained section for
2116 * @old_child_bs.
2117 */
2118 g_assert(parent_s->was_drained && parent_s->was_undrained);
2119 } else if (!old_drain_count && new_drain_count) {
2120 /*
2121 * From undrained to drained should drain the parent and keep
2122 * it that way.
2123 */
2124 g_assert(parent_s->was_drained && !parent_s->was_undrained);
2125 } else if (old_drain_count && !new_drain_count) {
2126 /*
2127 * From drained to undrained should undrain the parent and
2128 * keep it that way.
2129 */
2130 g_assert(!parent_s->was_drained && parent_s->was_undrained);
2131 } else /* if (old_drain_count && new_drain_count) */ {
2132 /*
2133 * From drained to drained must not undrain the parent at any
2134 * point
2135 */
2136 g_assert(!parent_s->was_drained && !parent_s->was_undrained);
2137 }
2138
2139 if (!old_drain_count || !new_drain_count) {
2140 /*
2141 * If !old_drain_count, we have started a read request before
2142 * bdrv_replace_node(). If !new_drain_count, the parent must
2143 * have been undrained at some point, and
2144 * bdrv_replace_test_co_drain_end() starts a read request
2145 * then.
2146 */
2147 g_assert(parent_s->has_read);
2148 } else {
2149 /*
2150 * If the parent was never undrained, there is no way to start
2151 * a read request.
2152 */
2153 g_assert(!parent_s->has_read);
2154 }
2155
2156 /* A drained child must have not received any request */
2157 g_assert(!(old_drain_count && old_child_s->has_read));
2158 g_assert(!(new_drain_count && new_child_s->has_read));
2159
2160 for (i = 0; i < new_drain_count; i++) {
2161 bdrv_drained_end(new_child_bs);
2162 }
2163 for (i = 0; i < old_drain_count; i++) {
2164 bdrv_drained_end(old_child_bs);
2165 }
2166
2167 /*
2168 * By now, bdrv_replace_test_co_drain_end() must have been called
2169 * at some point while the new child was attached to the parent.
2170 */
2171 g_assert(parent_s->has_read);
2172 g_assert(new_child_s->has_read);
2173
2174 blk_unref(parent_blk);
2175 bdrv_unref(parent_bs);
2176 bdrv_unref(old_child_bs);
2177 bdrv_unref(new_child_bs);
2178}
2179
2180static void test_replace_child_mid_drain(void)
2181{
2182 int old_drain_count, new_drain_count;
2183
2184 for (old_drain_count = 0; old_drain_count < 2; old_drain_count++) {
2185 for (new_drain_count = 0; new_drain_count < 2; new_drain_count++) {
2186 do_test_replace_child_mid_drain(old_drain_count, new_drain_count);
2187 }
2188 }
2189}
2190
881cfd17
KW
2191int main(int argc, char **argv)
2192{
bb675689
KW
2193 int ret;
2194
881cfd17
KW
2195 bdrv_init();
2196 qemu_init_main_loop(&error_abort);
2197
2198 g_test_init(&argc, &argv, NULL);
bb675689 2199 qemu_event_init(&done_event, false);
881cfd17
KW
2200
2201 g_test_add_func("/bdrv-drain/driver-cb/drain_all", test_drv_cb_drain_all);
86e1c840 2202 g_test_add_func("/bdrv-drain/driver-cb/drain", test_drv_cb_drain);
d2a85d0f
KW
2203 g_test_add_func("/bdrv-drain/driver-cb/drain_subtree",
2204 test_drv_cb_drain_subtree);
881cfd17 2205
6d0252f2
KW
2206 g_test_add_func("/bdrv-drain/driver-cb/co/drain_all",
2207 test_drv_cb_co_drain_all);
0582eb10
KW
2208 g_test_add_func("/bdrv-drain/driver-cb/co/drain", test_drv_cb_co_drain);
2209 g_test_add_func("/bdrv-drain/driver-cb/co/drain_subtree",
2210 test_drv_cb_co_drain_subtree);
2211
2212
89a6ceab
KW
2213 g_test_add_func("/bdrv-drain/quiesce/drain_all", test_quiesce_drain_all);
2214 g_test_add_func("/bdrv-drain/quiesce/drain", test_quiesce_drain);
d2a85d0f
KW
2215 g_test_add_func("/bdrv-drain/quiesce/drain_subtree",
2216 test_quiesce_drain_subtree);
89a6ceab 2217
6d0252f2
KW
2218 g_test_add_func("/bdrv-drain/quiesce/co/drain_all",
2219 test_quiesce_co_drain_all);
0582eb10
KW
2220 g_test_add_func("/bdrv-drain/quiesce/co/drain", test_quiesce_co_drain);
2221 g_test_add_func("/bdrv-drain/quiesce/co/drain_subtree",
2222 test_quiesce_co_drain_subtree);
2223
6c429a6a 2224 g_test_add_func("/bdrv-drain/nested", test_nested);
27e64474 2225 g_test_add_func("/bdrv-drain/multiparent", test_multiparent);
19f7a7e5
KW
2226
2227 g_test_add_func("/bdrv-drain/graph-change/drain_subtree",
2228 test_graph_change_drain_subtree);
2229 g_test_add_func("/bdrv-drain/graph-change/drain_all",
2230 test_graph_change_drain_all);
6c429a6a 2231
bb675689
KW
2232 g_test_add_func("/bdrv-drain/iothread/drain_all", test_iothread_drain_all);
2233 g_test_add_func("/bdrv-drain/iothread/drain", test_iothread_drain);
2234 g_test_add_func("/bdrv-drain/iothread/drain_subtree",
2235 test_iothread_drain_subtree);
2236
7253220d
KW
2237 g_test_add_func("/bdrv-drain/blockjob/drain_all", test_blockjob_drain_all);
2238 g_test_add_func("/bdrv-drain/blockjob/drain", test_blockjob_drain);
d2a85d0f
KW
2239 g_test_add_func("/bdrv-drain/blockjob/drain_subtree",
2240 test_blockjob_drain_subtree);
7253220d 2241
d49725af
KW
2242 g_test_add_func("/bdrv-drain/blockjob/error/drain_all",
2243 test_blockjob_error_drain_all);
2244 g_test_add_func("/bdrv-drain/blockjob/error/drain",
2245 test_blockjob_error_drain);
2246 g_test_add_func("/bdrv-drain/blockjob/error/drain_subtree",
2247 test_blockjob_error_drain_subtree);
2248
f62c1729
KW
2249 g_test_add_func("/bdrv-drain/blockjob/iothread/drain_all",
2250 test_blockjob_iothread_drain_all);
2251 g_test_add_func("/bdrv-drain/blockjob/iothread/drain",
2252 test_blockjob_iothread_drain);
2253 g_test_add_func("/bdrv-drain/blockjob/iothread/drain_subtree",
2254 test_blockjob_iothread_drain_subtree);
2255
d49725af
KW
2256 g_test_add_func("/bdrv-drain/blockjob/iothread/error/drain_all",
2257 test_blockjob_iothread_error_drain_all);
2258 g_test_add_func("/bdrv-drain/blockjob/iothread/error/drain",
2259 test_blockjob_iothread_error_drain);
2260 g_test_add_func("/bdrv-drain/blockjob/iothread/error/drain_subtree",
2261 test_blockjob_iothread_error_drain_subtree);
2262
ebd31837 2263 g_test_add_func("/bdrv-drain/deletion/drain", test_delete_by_drain);
19f7a7e5 2264 g_test_add_func("/bdrv-drain/detach/drain_all", test_detach_by_drain_all);
ebd31837
KW
2265 g_test_add_func("/bdrv-drain/detach/drain", test_detach_by_drain);
2266 g_test_add_func("/bdrv-drain/detach/drain_subtree", test_detach_by_drain_subtree);
231281ab 2267 g_test_add_func("/bdrv-drain/detach/parent_cb", test_detach_by_parent_cb);
57320ca9 2268 g_test_add_func("/bdrv-drain/detach/driver_cb", test_detach_by_driver_cb);
4c8158e3 2269
b994c5bc
KW
2270 g_test_add_func("/bdrv-drain/attach/drain", test_append_to_drained);
2271
247d2737
KW
2272 g_test_add_func("/bdrv-drain/set_aio_context", test_set_aio_context);
2273
8e442810
HR
2274 g_test_add_func("/bdrv-drain/blockjob/commit_by_drained_end",
2275 test_blockjob_commit_by_drained_end);
2276
9746b35c
HR
2277 g_test_add_func("/bdrv-drain/bdrv_drop_intermediate/poll",
2278 test_drop_intermediate_poll);
2279
0513f984
HR
2280 g_test_add_func("/bdrv-drain/replace_child/mid-drain",
2281 test_replace_child_mid_drain);
2282
bb675689
KW
2283 ret = g_test_run();
2284 qemu_event_destroy(&done_event);
2285 return ret;
881cfd17 2286}