2 * Block node draining tests
4 * Copyright (c) 2017 Kevin Wolf <kwolf@redhat.com>
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:
13 * The above copyright notice and this permission notice shall be included in
14 * all copies or substantial portions of the Software.
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
25 #include "qemu/osdep.h"
26 #include "block/block_int.h"
27 #include "block/blockjob_int.h"
28 #include "sysemu/block-backend.h"
29 #include "qapi/error.h"
30 #include "qemu/main-loop.h"
33 static QemuEvent done_event
;
35 typedef struct BDRVTestState
{
37 AioContext
*bh_indirection_ctx
;
38 bool sleep_in_drain_begin
;
41 static void coroutine_fn
sleep_in_drain_begin(void *opaque
)
43 BlockDriverState
*bs
= opaque
;
45 qemu_co_sleep_ns(QEMU_CLOCK_REALTIME
, 100000);
46 bdrv_dec_in_flight(bs
);
49 static void bdrv_test_drain_begin(BlockDriverState
*bs
)
51 BDRVTestState
*s
= bs
->opaque
;
53 if (s
->sleep_in_drain_begin
) {
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
);
60 static void bdrv_test_drain_end(BlockDriverState
*bs
)
62 BDRVTestState
*s
= bs
->opaque
;
66 static void bdrv_test_close(BlockDriverState
*bs
)
68 BDRVTestState
*s
= bs
->opaque
;
69 g_assert_cmpint(s
->drain_count
, >, 0);
72 static void co_reenter_bh(void *opaque
)
77 static int coroutine_fn
bdrv_test_co_preadv(BlockDriverState
*bs
,
78 int64_t offset
, int64_t bytes
,
80 BdrvRequestFlags flags
)
82 BDRVTestState
*s
= bs
->opaque
;
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);
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();
99 static int bdrv_test_change_backing_file(BlockDriverState
*bs
,
100 const char *backing_file
,
101 const char *backing_fmt
)
106 static BlockDriver bdrv_test
= {
107 .format_name
= "test",
108 .instance_size
= sizeof(BDRVTestState
),
109 .supports_backing
= true,
111 .bdrv_close
= bdrv_test_close
,
112 .bdrv_co_preadv
= bdrv_test_co_preadv
,
114 .bdrv_drain_begin
= bdrv_test_drain_begin
,
115 .bdrv_drain_end
= bdrv_test_drain_end
,
117 .bdrv_child_perm
= bdrv_default_perms
,
119 .bdrv_change_backing_file
= bdrv_test_change_backing_file
,
122 static void aio_ret_cb(void *opaque
, int ret
)
124 int *aio_ret
= opaque
;
128 typedef struct CallInCoroutineData
{
131 } CallInCoroutineData
;
133 static coroutine_fn
void call_in_coroutine_entry(void *opaque
)
135 CallInCoroutineData
*data
= opaque
;
141 static void call_in_coroutine(void (*entry
)(void))
144 CallInCoroutineData data
= {
149 co
= qemu_coroutine_create(call_in_coroutine_entry
, &data
);
150 qemu_coroutine_enter(co
);
152 aio_poll(qemu_get_aio_context(), true);
162 static void do_drain_begin(enum drain_type drain_type
, BlockDriverState
*bs
)
164 switch (drain_type
) {
165 case BDRV_DRAIN_ALL
: bdrv_drain_all_begin(); break;
166 case BDRV_DRAIN
: bdrv_drained_begin(bs
); break;
167 default: g_assert_not_reached();
171 static void do_drain_end(enum drain_type drain_type
, BlockDriverState
*bs
)
173 switch (drain_type
) {
174 case BDRV_DRAIN_ALL
: bdrv_drain_all_end(); break;
175 case BDRV_DRAIN
: bdrv_drained_end(bs
); break;
176 default: g_assert_not_reached();
180 static void do_drain_begin_unlocked(enum drain_type drain_type
, BlockDriverState
*bs
)
182 if (drain_type
!= BDRV_DRAIN_ALL
) {
183 aio_context_acquire(bdrv_get_aio_context(bs
));
185 do_drain_begin(drain_type
, bs
);
186 if (drain_type
!= BDRV_DRAIN_ALL
) {
187 aio_context_release(bdrv_get_aio_context(bs
));
191 static BlockBackend
* no_coroutine_fn
test_setup(void)
194 BlockDriverState
*bs
, *backing
;
196 blk
= blk_new(qemu_get_aio_context(), BLK_PERM_ALL
, BLK_PERM_ALL
);
197 bs
= bdrv_new_open_driver(&bdrv_test
, "test-node", BDRV_O_RDWR
,
199 blk_insert_bs(blk
, bs
, &error_abort
);
201 backing
= bdrv_new_open_driver(&bdrv_test
, "backing", 0, &error_abort
);
202 bdrv_set_backing_hd(bs
, backing
, &error_abort
);
210 static void do_drain_end_unlocked(enum drain_type drain_type
, BlockDriverState
*bs
)
212 if (drain_type
!= BDRV_DRAIN_ALL
) {
213 aio_context_acquire(bdrv_get_aio_context(bs
));
215 do_drain_end(drain_type
, bs
);
216 if (drain_type
!= BDRV_DRAIN_ALL
) {
217 aio_context_release(bdrv_get_aio_context(bs
));
221 static void test_drv_cb_common(BlockBackend
*blk
, enum drain_type drain_type
,
224 BlockDriverState
*bs
= blk_bs(blk
);
225 BlockDriverState
*backing
= bs
->backing
->bs
;
226 BDRVTestState
*s
, *backing_s
;
230 QEMUIOVector qiov
= QEMU_IOVEC_INIT_BUF(qiov
, NULL
, 0);
233 backing_s
= backing
->opaque
;
235 /* Simple bdrv_drain_all_begin/end pair, check that CBs are called */
236 g_assert_cmpint(s
->drain_count
, ==, 0);
237 g_assert_cmpint(backing_s
->drain_count
, ==, 0);
239 do_drain_begin(drain_type
, bs
);
241 g_assert_cmpint(s
->drain_count
, ==, 1);
242 g_assert_cmpint(backing_s
->drain_count
, ==, !!recursive
);
244 do_drain_end(drain_type
, bs
);
246 g_assert_cmpint(s
->drain_count
, ==, 0);
247 g_assert_cmpint(backing_s
->drain_count
, ==, 0);
249 /* Now do the same while a request is pending */
250 aio_ret
= -EINPROGRESS
;
251 acb
= blk_aio_preadv(blk
, 0, &qiov
, 0, aio_ret_cb
, &aio_ret
);
252 g_assert(acb
!= NULL
);
253 g_assert_cmpint(aio_ret
, ==, -EINPROGRESS
);
255 g_assert_cmpint(s
->drain_count
, ==, 0);
256 g_assert_cmpint(backing_s
->drain_count
, ==, 0);
258 do_drain_begin(drain_type
, bs
);
260 g_assert_cmpint(aio_ret
, ==, 0);
261 g_assert_cmpint(s
->drain_count
, ==, 1);
262 g_assert_cmpint(backing_s
->drain_count
, ==, !!recursive
);
264 do_drain_end(drain_type
, bs
);
266 g_assert_cmpint(s
->drain_count
, ==, 0);
267 g_assert_cmpint(backing_s
->drain_count
, ==, 0);
270 static void test_drv_cb_drain_all(void)
272 BlockBackend
*blk
= test_setup();
273 test_drv_cb_common(blk
, BDRV_DRAIN_ALL
, true);
277 static void test_drv_cb_drain(void)
279 BlockBackend
*blk
= test_setup();
280 test_drv_cb_common(blk
, BDRV_DRAIN
, false);
284 static void coroutine_fn
test_drv_cb_co_drain_all_entry(void)
286 BlockBackend
*blk
= blk_all_next(NULL
);
287 test_drv_cb_common(blk
, BDRV_DRAIN_ALL
, true);
290 static void test_drv_cb_co_drain_all(void)
292 BlockBackend
*blk
= test_setup();
293 call_in_coroutine(test_drv_cb_co_drain_all_entry
);
297 static void coroutine_fn
test_drv_cb_co_drain_entry(void)
299 BlockBackend
*blk
= blk_all_next(NULL
);
300 test_drv_cb_common(blk
, BDRV_DRAIN
, false);
303 static void test_drv_cb_co_drain(void)
305 BlockBackend
*blk
= test_setup();
306 call_in_coroutine(test_drv_cb_co_drain_entry
);
310 static void test_quiesce_common(BlockBackend
*blk
, enum drain_type drain_type
,
313 BlockDriverState
*bs
= blk_bs(blk
);
314 BlockDriverState
*backing
= bs
->backing
->bs
;
316 g_assert_cmpint(bs
->quiesce_counter
, ==, 0);
317 g_assert_cmpint(backing
->quiesce_counter
, ==, 0);
319 do_drain_begin(drain_type
, bs
);
321 if (drain_type
== BDRV_DRAIN_ALL
) {
322 g_assert_cmpint(bs
->quiesce_counter
, ==, 2);
324 g_assert_cmpint(bs
->quiesce_counter
, ==, 1);
326 g_assert_cmpint(backing
->quiesce_counter
, ==, !!recursive
);
328 do_drain_end(drain_type
, bs
);
330 g_assert_cmpint(bs
->quiesce_counter
, ==, 0);
331 g_assert_cmpint(backing
->quiesce_counter
, ==, 0);
334 static void test_quiesce_drain_all(void)
336 BlockBackend
*blk
= test_setup();
337 test_quiesce_common(blk
, BDRV_DRAIN_ALL
, true);
341 static void test_quiesce_drain(void)
343 BlockBackend
*blk
= test_setup();
344 test_quiesce_common(blk
, BDRV_DRAIN
, false);
348 static void coroutine_fn
test_quiesce_co_drain_all_entry(void)
350 BlockBackend
*blk
= blk_all_next(NULL
);
351 test_quiesce_common(blk
, BDRV_DRAIN_ALL
, true);
354 static void test_quiesce_co_drain_all(void)
356 BlockBackend
*blk
= test_setup();
357 call_in_coroutine(test_quiesce_co_drain_all_entry
);
361 static void coroutine_fn
test_quiesce_co_drain_entry(void)
363 BlockBackend
*blk
= blk_all_next(NULL
);
364 test_quiesce_common(blk
, BDRV_DRAIN
, false);
367 static void test_quiesce_co_drain(void)
369 BlockBackend
*blk
= test_setup();
370 call_in_coroutine(test_quiesce_co_drain_entry
);
374 static void test_nested(void)
377 BlockDriverState
*bs
, *backing
;
378 BDRVTestState
*s
, *backing_s
;
379 enum drain_type outer
, inner
;
381 blk
= blk_new(qemu_get_aio_context(), BLK_PERM_ALL
, BLK_PERM_ALL
);
382 bs
= bdrv_new_open_driver(&bdrv_test
, "test-node", BDRV_O_RDWR
,
385 blk_insert_bs(blk
, bs
, &error_abort
);
387 backing
= bdrv_new_open_driver(&bdrv_test
, "backing", 0, &error_abort
);
388 backing_s
= backing
->opaque
;
389 bdrv_set_backing_hd(bs
, backing
, &error_abort
);
391 for (outer
= 0; outer
< DRAIN_TYPE_MAX
; outer
++) {
392 for (inner
= 0; inner
< DRAIN_TYPE_MAX
; inner
++) {
393 int backing_quiesce
= (outer
== BDRV_DRAIN_ALL
) +
394 (inner
== BDRV_DRAIN_ALL
);
396 g_assert_cmpint(bs
->quiesce_counter
, ==, 0);
397 g_assert_cmpint(backing
->quiesce_counter
, ==, 0);
398 g_assert_cmpint(s
->drain_count
, ==, 0);
399 g_assert_cmpint(backing_s
->drain_count
, ==, 0);
401 do_drain_begin(outer
, bs
);
402 do_drain_begin(inner
, bs
);
404 g_assert_cmpint(bs
->quiesce_counter
, ==, 2 + !!backing_quiesce
);
405 g_assert_cmpint(backing
->quiesce_counter
, ==, backing_quiesce
);
406 g_assert_cmpint(s
->drain_count
, ==, 1);
407 g_assert_cmpint(backing_s
->drain_count
, ==, !!backing_quiesce
);
409 do_drain_end(inner
, bs
);
410 do_drain_end(outer
, bs
);
412 g_assert_cmpint(bs
->quiesce_counter
, ==, 0);
413 g_assert_cmpint(backing
->quiesce_counter
, ==, 0);
414 g_assert_cmpint(s
->drain_count
, ==, 0);
415 g_assert_cmpint(backing_s
->drain_count
, ==, 0);
424 static void test_graph_change_drain_all(void)
426 BlockBackend
*blk_a
, *blk_b
;
427 BlockDriverState
*bs_a
, *bs_b
;
428 BDRVTestState
*a_s
, *b_s
;
430 /* Create node A with a BlockBackend */
431 blk_a
= blk_new(qemu_get_aio_context(), BLK_PERM_ALL
, BLK_PERM_ALL
);
432 bs_a
= bdrv_new_open_driver(&bdrv_test
, "test-node-a", BDRV_O_RDWR
,
435 blk_insert_bs(blk_a
, bs_a
, &error_abort
);
437 g_assert_cmpint(bs_a
->quiesce_counter
, ==, 0);
438 g_assert_cmpint(a_s
->drain_count
, ==, 0);
440 /* Call bdrv_drain_all_begin() */
441 bdrv_drain_all_begin();
443 g_assert_cmpint(bs_a
->quiesce_counter
, ==, 1);
444 g_assert_cmpint(a_s
->drain_count
, ==, 1);
446 /* Create node B with a BlockBackend */
447 blk_b
= blk_new(qemu_get_aio_context(), BLK_PERM_ALL
, BLK_PERM_ALL
);
448 bs_b
= bdrv_new_open_driver(&bdrv_test
, "test-node-b", BDRV_O_RDWR
,
451 blk_insert_bs(blk_b
, bs_b
, &error_abort
);
453 g_assert_cmpint(bs_a
->quiesce_counter
, ==, 1);
454 g_assert_cmpint(bs_b
->quiesce_counter
, ==, 1);
455 g_assert_cmpint(a_s
->drain_count
, ==, 1);
456 g_assert_cmpint(b_s
->drain_count
, ==, 1);
458 /* Unref and finally delete node A */
461 g_assert_cmpint(bs_a
->quiesce_counter
, ==, 1);
462 g_assert_cmpint(bs_b
->quiesce_counter
, ==, 1);
463 g_assert_cmpint(a_s
->drain_count
, ==, 1);
464 g_assert_cmpint(b_s
->drain_count
, ==, 1);
468 g_assert_cmpint(bs_b
->quiesce_counter
, ==, 1);
469 g_assert_cmpint(b_s
->drain_count
, ==, 1);
471 /* End the drained section */
472 bdrv_drain_all_end();
474 g_assert_cmpint(bs_b
->quiesce_counter
, ==, 0);
475 g_assert_cmpint(b_s
->drain_count
, ==, 0);
481 struct test_iothread_data
{
482 BlockDriverState
*bs
;
483 enum drain_type drain_type
;
488 static void coroutine_fn
test_iothread_drain_co_entry(void *opaque
)
490 struct test_iothread_data
*data
= opaque
;
492 do_drain_begin(data
->drain_type
, data
->bs
);
493 g_assert_cmpint(*data
->aio_ret
, ==, 0);
494 do_drain_end(data
->drain_type
, data
->bs
);
496 data
->co_done
= true;
500 static void test_iothread_aio_cb(void *opaque
, int ret
)
502 int *aio_ret
= opaque
;
504 qemu_event_set(&done_event
);
507 static void test_iothread_main_thread_bh(void *opaque
)
509 struct test_iothread_data
*data
= opaque
;
511 /* Test that the AioContext is not yet locked in a random BH that is
512 * executed during drain, otherwise this would deadlock. */
513 aio_context_acquire(bdrv_get_aio_context(data
->bs
));
514 bdrv_flush(data
->bs
);
515 bdrv_dec_in_flight(data
->bs
); /* incremented by test_iothread_common() */
516 aio_context_release(bdrv_get_aio_context(data
->bs
));
520 * Starts an AIO request on a BDS that runs in the AioContext of iothread 1.
521 * The request involves a BH on iothread 2 before it can complete.
523 * @drain_thread = 0 means that do_drain_begin/end are called from the main
524 * thread, @drain_thread = 1 means that they are called from iothread 1. Drain
525 * for this BDS cannot be called from iothread 2 because only the main thread
526 * may do cross-AioContext polling.
528 static void test_iothread_common(enum drain_type drain_type
, int drain_thread
)
531 BlockDriverState
*bs
;
536 struct test_iothread_data data
;
538 IOThread
*a
= iothread_new();
539 IOThread
*b
= iothread_new();
540 AioContext
*ctx_a
= iothread_get_aio_context(a
);
541 AioContext
*ctx_b
= iothread_get_aio_context(b
);
543 QEMUIOVector qiov
= QEMU_IOVEC_INIT_BUF(qiov
, NULL
, 0);
545 /* bdrv_drain_all() may only be called from the main loop thread */
546 if (drain_type
== BDRV_DRAIN_ALL
&& drain_thread
!= 0) {
550 blk
= blk_new(qemu_get_aio_context(), BLK_PERM_ALL
, BLK_PERM_ALL
);
551 bs
= bdrv_new_open_driver(&bdrv_test
, "test-node", BDRV_O_RDWR
,
554 blk_insert_bs(blk
, bs
, &error_abort
);
555 blk_set_disable_request_queuing(blk
, true);
557 blk_set_aio_context(blk
, ctx_a
, &error_abort
);
558 aio_context_acquire(ctx_a
);
560 s
->bh_indirection_ctx
= ctx_b
;
562 aio_ret
= -EINPROGRESS
;
563 qemu_event_reset(&done_event
);
565 if (drain_thread
== 0) {
566 acb
= blk_aio_preadv(blk
, 0, &qiov
, 0, test_iothread_aio_cb
, &aio_ret
);
568 acb
= blk_aio_preadv(blk
, 0, &qiov
, 0, aio_ret_cb
, &aio_ret
);
570 g_assert(acb
!= NULL
);
571 g_assert_cmpint(aio_ret
, ==, -EINPROGRESS
);
573 aio_context_release(ctx_a
);
575 data
= (struct test_iothread_data
) {
577 .drain_type
= drain_type
,
581 switch (drain_thread
) {
583 if (drain_type
!= BDRV_DRAIN_ALL
) {
584 aio_context_acquire(ctx_a
);
588 * Increment in_flight so that do_drain_begin() waits for
589 * test_iothread_main_thread_bh(). This prevents the race between
590 * test_iothread_main_thread_bh() in IOThread a and do_drain_begin() in
591 * this thread. test_iothread_main_thread_bh() decrements in_flight.
593 bdrv_inc_in_flight(bs
);
594 aio_bh_schedule_oneshot(ctx_a
, test_iothread_main_thread_bh
, &data
);
596 /* The request is running on the IOThread a. Draining its block device
597 * will make sure that it has completed as far as the BDS is concerned,
598 * but the drain in this thread can continue immediately after
599 * bdrv_dec_in_flight() and aio_ret might be assigned only slightly
601 do_drain_begin(drain_type
, bs
);
602 g_assert_cmpint(bs
->in_flight
, ==, 0);
604 if (drain_type
!= BDRV_DRAIN_ALL
) {
605 aio_context_release(ctx_a
);
607 qemu_event_wait(&done_event
);
608 if (drain_type
!= BDRV_DRAIN_ALL
) {
609 aio_context_acquire(ctx_a
);
612 g_assert_cmpint(aio_ret
, ==, 0);
613 do_drain_end(drain_type
, bs
);
615 if (drain_type
!= BDRV_DRAIN_ALL
) {
616 aio_context_release(ctx_a
);
620 co
= qemu_coroutine_create(test_iothread_drain_co_entry
, &data
);
621 aio_co_enter(ctx_a
, co
);
622 AIO_WAIT_WHILE_UNLOCKED(NULL
, !data
.co_done
);
625 g_assert_not_reached();
628 aio_context_acquire(ctx_a
);
629 blk_set_aio_context(blk
, qemu_get_aio_context(), &error_abort
);
630 aio_context_release(ctx_a
);
640 static void test_iothread_drain_all(void)
642 test_iothread_common(BDRV_DRAIN_ALL
, 0);
643 test_iothread_common(BDRV_DRAIN_ALL
, 1);
646 static void test_iothread_drain(void)
648 test_iothread_common(BDRV_DRAIN
, 0);
649 test_iothread_common(BDRV_DRAIN
, 1);
653 typedef struct TestBlockJob
{
655 BlockDriverState
*bs
;
659 bool should_complete
;
662 static int test_job_prepare(Job
*job
)
664 TestBlockJob
*s
= container_of(job
, TestBlockJob
, common
.job
);
666 /* Provoke an AIO_WAIT_WHILE() call to verify there is no deadlock */
668 return s
->prepare_ret
;
671 static void test_job_commit(Job
*job
)
673 TestBlockJob
*s
= container_of(job
, TestBlockJob
, common
.job
);
675 /* Provoke an AIO_WAIT_WHILE() call to verify there is no deadlock */
679 static void test_job_abort(Job
*job
)
681 TestBlockJob
*s
= container_of(job
, TestBlockJob
, common
.job
);
683 /* Provoke an AIO_WAIT_WHILE() call to verify there is no deadlock */
687 static int coroutine_fn
test_job_run(Job
*job
, Error
**errp
)
689 TestBlockJob
*s
= container_of(job
, TestBlockJob
, common
.job
);
691 /* We are running the actual job code past the pause point in
695 job_transition_to_ready(&s
->common
.job
);
696 while (!s
->should_complete
) {
697 /* Avoid job_sleep_ns() because it marks the job as !busy. We want to
698 * emulate some actual activity (probably some I/O) here so that drain
699 * has to wait for this activity to stop. */
700 qemu_co_sleep_ns(QEMU_CLOCK_REALTIME
, 1000000);
702 job_pause_point(&s
->common
.job
);
708 static void test_job_complete(Job
*job
, Error
**errp
)
710 TestBlockJob
*s
= container_of(job
, TestBlockJob
, common
.job
);
711 s
->should_complete
= true;
714 BlockJobDriver test_job_driver
= {
716 .instance_size
= sizeof(TestBlockJob
),
717 .free
= block_job_free
,
718 .user_resume
= block_job_user_resume
,
720 .complete
= test_job_complete
,
721 .prepare
= test_job_prepare
,
722 .commit
= test_job_commit
,
723 .abort
= test_job_abort
,
727 enum test_job_result
{
730 TEST_JOB_FAIL_PREPARE
,
733 enum test_job_drain_node
{
735 TEST_JOB_DRAIN_SRC_CHILD
,
738 static void test_blockjob_common_drain_node(enum drain_type drain_type
,
740 enum test_job_result result
,
741 enum test_job_drain_node drain_node
)
743 BlockBackend
*blk_src
, *blk_target
;
744 BlockDriverState
*src
, *src_backing
, *src_overlay
, *target
, *drain_bs
;
747 IOThread
*iothread
= NULL
;
751 src
= bdrv_new_open_driver(&bdrv_test
, "source", BDRV_O_RDWR
,
753 src_backing
= bdrv_new_open_driver(&bdrv_test
, "source-backing",
754 BDRV_O_RDWR
, &error_abort
);
755 src_overlay
= bdrv_new_open_driver(&bdrv_test
, "source-overlay",
756 BDRV_O_RDWR
, &error_abort
);
758 bdrv_set_backing_hd(src_overlay
, src
, &error_abort
);
760 bdrv_set_backing_hd(src
, src_backing
, &error_abort
);
761 bdrv_unref(src_backing
);
763 blk_src
= blk_new(qemu_get_aio_context(), BLK_PERM_ALL
, BLK_PERM_ALL
);
764 blk_insert_bs(blk_src
, src_overlay
, &error_abort
);
766 switch (drain_node
) {
767 case TEST_JOB_DRAIN_SRC
:
770 case TEST_JOB_DRAIN_SRC_CHILD
:
771 drain_bs
= src_backing
;
774 g_assert_not_reached();
778 iothread
= iothread_new();
779 ctx
= iothread_get_aio_context(iothread
);
780 blk_set_aio_context(blk_src
, ctx
, &error_abort
);
782 ctx
= qemu_get_aio_context();
785 target
= bdrv_new_open_driver(&bdrv_test
, "target", BDRV_O_RDWR
,
787 blk_target
= blk_new(qemu_get_aio_context(), BLK_PERM_ALL
, BLK_PERM_ALL
);
788 blk_insert_bs(blk_target
, target
, &error_abort
);
789 blk_set_allow_aio_context_change(blk_target
, true);
791 aio_context_acquire(ctx
);
792 tjob
= block_job_create("job0", &test_job_driver
, NULL
, src
,
794 0, 0, NULL
, NULL
, &error_abort
);
797 block_job_add_bdrv(job
, "target", target
, 0, BLK_PERM_ALL
, &error_abort
);
800 case TEST_JOB_SUCCESS
:
802 case TEST_JOB_FAIL_RUN
:
803 tjob
->run_ret
= -EIO
;
805 case TEST_JOB_FAIL_PREPARE
:
806 tjob
->prepare_ret
= -EIO
;
809 aio_context_release(ctx
);
811 job_start(&job
->job
);
814 /* job_co_entry() is run in the I/O thread, wait for the actual job
815 * code to start (we don't want to catch the job in the pause point in
817 while (!tjob
->running
) {
818 aio_poll(qemu_get_aio_context(), false);
822 WITH_JOB_LOCK_GUARD() {
823 g_assert_cmpint(job
->job
.pause_count
, ==, 0);
824 g_assert_false(job
->job
.paused
);
825 g_assert_true(tjob
->running
);
826 g_assert_true(job
->job
.busy
); /* We're in qemu_co_sleep_ns() */
829 do_drain_begin_unlocked(drain_type
, drain_bs
);
831 WITH_JOB_LOCK_GUARD() {
832 if (drain_type
== BDRV_DRAIN_ALL
) {
833 /* bdrv_drain_all() drains both src and target */
834 g_assert_cmpint(job
->job
.pause_count
, ==, 2);
836 g_assert_cmpint(job
->job
.pause_count
, ==, 1);
838 g_assert_true(job
->job
.paused
);
839 g_assert_false(job
->job
.busy
); /* The job is paused */
842 do_drain_end_unlocked(drain_type
, drain_bs
);
846 * Here we are waiting for the paused status to change,
847 * so don't bother protecting the read every time.
849 * paused is reset in the I/O thread, wait for it
851 while (job
->job
.paused
) {
852 aio_poll(qemu_get_aio_context(), false);
856 WITH_JOB_LOCK_GUARD() {
857 g_assert_cmpint(job
->job
.pause_count
, ==, 0);
858 g_assert_false(job
->job
.paused
);
859 g_assert_true(job
->job
.busy
); /* We're in qemu_co_sleep_ns() */
862 do_drain_begin_unlocked(drain_type
, target
);
864 WITH_JOB_LOCK_GUARD() {
865 if (drain_type
== BDRV_DRAIN_ALL
) {
866 /* bdrv_drain_all() drains both src and target */
867 g_assert_cmpint(job
->job
.pause_count
, ==, 2);
869 g_assert_cmpint(job
->job
.pause_count
, ==, 1);
871 g_assert_true(job
->job
.paused
);
872 g_assert_false(job
->job
.busy
); /* The job is paused */
875 do_drain_end_unlocked(drain_type
, target
);
879 * Here we are waiting for the paused status to change,
880 * so don't bother protecting the read every time.
882 * paused is reset in the I/O thread, wait for it
884 while (job
->job
.paused
) {
885 aio_poll(qemu_get_aio_context(), false);
889 WITH_JOB_LOCK_GUARD() {
890 g_assert_cmpint(job
->job
.pause_count
, ==, 0);
891 g_assert_false(job
->job
.paused
);
892 g_assert_true(job
->job
.busy
); /* We're in qemu_co_sleep_ns() */
895 WITH_JOB_LOCK_GUARD() {
896 ret
= job_complete_sync_locked(&job
->job
, &error_abort
);
898 g_assert_cmpint(ret
, ==, (result
== TEST_JOB_SUCCESS
? 0 : -EIO
));
900 aio_context_acquire(ctx
);
902 blk_set_aio_context(blk_src
, qemu_get_aio_context(), &error_abort
);
903 assert(blk_get_aio_context(blk_target
) == qemu_get_aio_context());
905 aio_context_release(ctx
);
908 blk_unref(blk_target
);
909 bdrv_unref(src_overlay
);
913 iothread_join(iothread
);
917 static void test_blockjob_common(enum drain_type drain_type
, bool use_iothread
,
918 enum test_job_result result
)
920 test_blockjob_common_drain_node(drain_type
, use_iothread
, result
,
922 test_blockjob_common_drain_node(drain_type
, use_iothread
, result
,
923 TEST_JOB_DRAIN_SRC_CHILD
);
926 static void test_blockjob_drain_all(void)
928 test_blockjob_common(BDRV_DRAIN_ALL
, false, TEST_JOB_SUCCESS
);
931 static void test_blockjob_drain(void)
933 test_blockjob_common(BDRV_DRAIN
, false, TEST_JOB_SUCCESS
);
936 static void test_blockjob_error_drain_all(void)
938 test_blockjob_common(BDRV_DRAIN_ALL
, false, TEST_JOB_FAIL_RUN
);
939 test_blockjob_common(BDRV_DRAIN_ALL
, false, TEST_JOB_FAIL_PREPARE
);
942 static void test_blockjob_error_drain(void)
944 test_blockjob_common(BDRV_DRAIN
, false, TEST_JOB_FAIL_RUN
);
945 test_blockjob_common(BDRV_DRAIN
, false, TEST_JOB_FAIL_PREPARE
);
948 static void test_blockjob_iothread_drain_all(void)
950 test_blockjob_common(BDRV_DRAIN_ALL
, true, TEST_JOB_SUCCESS
);
953 static void test_blockjob_iothread_drain(void)
955 test_blockjob_common(BDRV_DRAIN
, true, TEST_JOB_SUCCESS
);
958 static void test_blockjob_iothread_error_drain_all(void)
960 test_blockjob_common(BDRV_DRAIN_ALL
, true, TEST_JOB_FAIL_RUN
);
961 test_blockjob_common(BDRV_DRAIN_ALL
, true, TEST_JOB_FAIL_PREPARE
);
964 static void test_blockjob_iothread_error_drain(void)
966 test_blockjob_common(BDRV_DRAIN
, true, TEST_JOB_FAIL_RUN
);
967 test_blockjob_common(BDRV_DRAIN
, true, TEST_JOB_FAIL_PREPARE
);
971 typedef struct BDRVTestTopState
{
972 BdrvChild
*wait_child
;
975 static void bdrv_test_top_close(BlockDriverState
*bs
)
977 BdrvChild
*c
, *next_c
;
979 bdrv_graph_wrlock(NULL
);
980 QLIST_FOREACH_SAFE(c
, &bs
->children
, next
, next_c
) {
981 bdrv_unref_child(bs
, c
);
983 bdrv_graph_wrunlock();
986 static int coroutine_fn GRAPH_RDLOCK
987 bdrv_test_top_co_preadv(BlockDriverState
*bs
, int64_t offset
, int64_t bytes
,
988 QEMUIOVector
*qiov
, BdrvRequestFlags flags
)
990 BDRVTestTopState
*tts
= bs
->opaque
;
991 return bdrv_co_preadv(tts
->wait_child
, offset
, bytes
, qiov
, flags
);
994 static BlockDriver bdrv_test_top_driver
= {
995 .format_name
= "test_top_driver",
996 .instance_size
= sizeof(BDRVTestTopState
),
998 .bdrv_close
= bdrv_test_top_close
,
999 .bdrv_co_preadv
= bdrv_test_top_co_preadv
,
1001 .bdrv_child_perm
= bdrv_default_perms
,
1004 typedef struct TestCoDeleteByDrainData
{
1006 bool detach_instead_of_delete
;
1008 } TestCoDeleteByDrainData
;
1010 static void coroutine_fn
test_co_delete_by_drain(void *opaque
)
1012 TestCoDeleteByDrainData
*dbdd
= opaque
;
1013 BlockBackend
*blk
= dbdd
->blk
;
1014 BlockDriverState
*bs
= blk_bs(blk
);
1015 BDRVTestTopState
*tts
= bs
->opaque
;
1016 void *buffer
= g_malloc(65536);
1017 QEMUIOVector qiov
= QEMU_IOVEC_INIT_BUF(qiov
, buffer
, 65536);
1019 /* Pretend some internal write operation from parent to child.
1020 * Important: We have to read from the child, not from the parent!
1021 * Draining works by first propagating it all up the tree to the
1022 * root and then waiting for drainage from root to the leaves
1023 * (protocol nodes). If we have a request waiting on the root,
1024 * everything will be drained before we go back down the tree, but
1025 * we do not want that. We want to be in the middle of draining
1026 * when this following requests returns. */
1027 bdrv_graph_co_rdlock();
1028 bdrv_co_preadv(tts
->wait_child
, 0, 65536, &qiov
, 0);
1029 bdrv_graph_co_rdunlock();
1031 g_assert_cmpint(bs
->refcnt
, ==, 1);
1033 if (!dbdd
->detach_instead_of_delete
) {
1036 BdrvChild
*c
, *next_c
;
1037 bdrv_graph_co_rdlock();
1038 QLIST_FOREACH_SAFE(c
, &bs
->children
, next
, next_c
) {
1039 bdrv_graph_co_rdunlock();
1040 bdrv_co_unref_child(bs
, c
);
1041 bdrv_graph_co_rdlock();
1043 bdrv_graph_co_rdunlock();
1051 * Test what happens when some BDS has some children, you drain one of
1052 * them and this results in the BDS being deleted.
1054 * If @detach_instead_of_delete is set, the BDS is not going to be
1055 * deleted but will only detach all of its children.
1057 static void do_test_delete_by_drain(bool detach_instead_of_delete
,
1058 enum drain_type drain_type
)
1061 BlockDriverState
*bs
, *child_bs
, *null_bs
;
1062 BDRVTestTopState
*tts
;
1063 TestCoDeleteByDrainData dbdd
;
1066 bs
= bdrv_new_open_driver(&bdrv_test_top_driver
, "top", BDRV_O_RDWR
,
1068 bs
->total_sectors
= 65536 >> BDRV_SECTOR_BITS
;
1071 null_bs
= bdrv_open("null-co://", NULL
, NULL
, BDRV_O_RDWR
| BDRV_O_PROTOCOL
,
1073 bdrv_graph_wrlock(NULL
);
1074 bdrv_attach_child(bs
, null_bs
, "null-child", &child_of_bds
,
1075 BDRV_CHILD_DATA
, &error_abort
);
1076 bdrv_graph_wrunlock();
1078 /* This child will be the one to pass to requests through to, and
1079 * it will stall until a drain occurs */
1080 child_bs
= bdrv_new_open_driver(&bdrv_test
, "child", BDRV_O_RDWR
,
1082 child_bs
->total_sectors
= 65536 >> BDRV_SECTOR_BITS
;
1083 /* Takes our reference to child_bs */
1084 bdrv_graph_wrlock(NULL
);
1085 tts
->wait_child
= bdrv_attach_child(bs
, child_bs
, "wait-child",
1087 BDRV_CHILD_DATA
| BDRV_CHILD_PRIMARY
,
1089 bdrv_graph_wrunlock();
1091 /* This child is just there to be deleted
1092 * (for detach_instead_of_delete == true) */
1093 null_bs
= bdrv_open("null-co://", NULL
, NULL
, BDRV_O_RDWR
| BDRV_O_PROTOCOL
,
1095 bdrv_graph_wrlock(NULL
);
1096 bdrv_attach_child(bs
, null_bs
, "null-child", &child_of_bds
, BDRV_CHILD_DATA
,
1098 bdrv_graph_wrunlock();
1100 blk
= blk_new(qemu_get_aio_context(), BLK_PERM_ALL
, BLK_PERM_ALL
);
1101 blk_insert_bs(blk
, bs
, &error_abort
);
1103 /* Referenced by blk now */
1106 g_assert_cmpint(bs
->refcnt
, ==, 1);
1107 g_assert_cmpint(child_bs
->refcnt
, ==, 1);
1108 g_assert_cmpint(null_bs
->refcnt
, ==, 1);
1111 dbdd
= (TestCoDeleteByDrainData
){
1113 .detach_instead_of_delete
= detach_instead_of_delete
,
1116 co
= qemu_coroutine_create(test_co_delete_by_drain
, &dbdd
);
1117 qemu_coroutine_enter(co
);
1119 /* Drain the child while the read operation is still pending.
1120 * This should result in the operation finishing and
1121 * test_co_delete_by_drain() resuming. Thus, @bs will be deleted
1122 * and the coroutine will exit while this drain operation is still
1124 switch (drain_type
) {
1127 bdrv_drain(child_bs
);
1128 bdrv_unref(child_bs
);
1130 case BDRV_DRAIN_ALL
:
1131 bdrv_drain_all_begin();
1132 bdrv_drain_all_end();
1135 g_assert_not_reached();
1138 while (!dbdd
.done
) {
1139 aio_poll(qemu_get_aio_context(), true);
1142 if (detach_instead_of_delete
) {
1143 /* Here, the reference has not passed over to the coroutine,
1144 * so we have to delete the BB ourselves */
1149 static void test_delete_by_drain(void)
1151 do_test_delete_by_drain(false, BDRV_DRAIN
);
1154 static void test_detach_by_drain_all(void)
1156 do_test_delete_by_drain(true, BDRV_DRAIN_ALL
);
1159 static void test_detach_by_drain(void)
1161 do_test_delete_by_drain(true, BDRV_DRAIN
);
1165 struct detach_by_parent_data
{
1166 BlockDriverState
*parent_b
;
1168 BlockDriverState
*c
;
1171 bool detach_on_drain
;
1173 static struct detach_by_parent_data detach_by_parent_data
;
1175 static void no_coroutine_fn
detach_indirect_bh(void *opaque
)
1177 struct detach_by_parent_data
*data
= opaque
;
1179 bdrv_dec_in_flight(data
->child_b
->bs
);
1181 bdrv_graph_wrlock(NULL
);
1182 bdrv_unref_child(data
->parent_b
, data
->child_b
);
1185 data
->child_c
= bdrv_attach_child(data
->parent_b
, data
->c
, "PB-C",
1186 &child_of_bds
, BDRV_CHILD_DATA
,
1188 bdrv_graph_wrunlock();
1191 static void coroutine_mixed_fn
detach_by_parent_aio_cb(void *opaque
, int ret
)
1193 struct detach_by_parent_data
*data
= &detach_by_parent_data
;
1195 g_assert_cmpint(ret
, ==, 0);
1196 if (data
->by_parent_cb
) {
1197 bdrv_inc_in_flight(data
->child_b
->bs
);
1198 aio_bh_schedule_oneshot(qemu_get_current_aio_context(),
1199 detach_indirect_bh
, &detach_by_parent_data
);
1203 static void GRAPH_RDLOCK
detach_by_driver_cb_drained_begin(BdrvChild
*child
)
1205 struct detach_by_parent_data
*data
= &detach_by_parent_data
;
1207 if (!data
->detach_on_drain
) {
1210 data
->detach_on_drain
= false;
1212 bdrv_inc_in_flight(data
->child_b
->bs
);
1213 aio_bh_schedule_oneshot(qemu_get_current_aio_context(),
1214 detach_indirect_bh
, &detach_by_parent_data
);
1215 child_of_bds
.drained_begin(child
);
1218 static BdrvChildClass detach_by_driver_cb_class
;
1227 * by_parent_cb == true: Test that parent callbacks don't poll
1229 * PA has a pending write request whose callback changes the child nodes of
1230 * PB: It removes B and adds C instead. The subtree of PB is drained, which
1231 * will indirectly drain the write request, too.
1233 * by_parent_cb == false: Test that bdrv_drain_invoke() doesn't poll
1235 * PA's BdrvChildClass has a .drained_begin callback that schedules a BH
1236 * that does the same graph change. If bdrv_drain_invoke() calls it, the
1237 * state is messed up, but if it is only polled in the single
1238 * BDRV_POLL_WHILE() at the end of the drain, this should work fine.
1240 static void TSA_NO_TSA
test_detach_indirect(bool by_parent_cb
)
1243 BlockDriverState
*parent_a
, *parent_b
, *a
, *b
, *c
;
1244 BdrvChild
*child_a
, *child_b
;
1247 QEMUIOVector qiov
= QEMU_IOVEC_INIT_BUF(qiov
, NULL
, 0);
1249 if (!by_parent_cb
) {
1250 detach_by_driver_cb_class
= child_of_bds
;
1251 detach_by_driver_cb_class
.drained_begin
=
1252 detach_by_driver_cb_drained_begin
;
1253 detach_by_driver_cb_class
.drained_end
= NULL
;
1254 detach_by_driver_cb_class
.drained_poll
= NULL
;
1257 detach_by_parent_data
= (struct detach_by_parent_data
) {
1258 .detach_on_drain
= false,
1261 /* Create all involved nodes */
1262 parent_a
= bdrv_new_open_driver(&bdrv_test
, "parent-a", BDRV_O_RDWR
,
1264 parent_b
= bdrv_new_open_driver(&bdrv_test
, "parent-b", 0,
1267 a
= bdrv_new_open_driver(&bdrv_test
, "a", BDRV_O_RDWR
, &error_abort
);
1268 b
= bdrv_new_open_driver(&bdrv_test
, "b", BDRV_O_RDWR
, &error_abort
);
1269 c
= bdrv_new_open_driver(&bdrv_test
, "c", BDRV_O_RDWR
, &error_abort
);
1271 /* blk is a BB for parent-a */
1272 blk
= blk_new(qemu_get_aio_context(), BLK_PERM_ALL
, BLK_PERM_ALL
);
1273 blk_insert_bs(blk
, parent_a
, &error_abort
);
1274 bdrv_unref(parent_a
);
1276 /* If we want to get bdrv_drain_invoke() to call aio_poll(), the driver
1277 * callback must not return immediately. */
1278 if (!by_parent_cb
) {
1279 BDRVTestState
*s
= parent_a
->opaque
;
1280 s
->sleep_in_drain_begin
= true;
1283 /* Set child relationships */
1286 bdrv_graph_wrlock(NULL
);
1287 child_b
= bdrv_attach_child(parent_b
, b
, "PB-B", &child_of_bds
,
1288 BDRV_CHILD_DATA
, &error_abort
);
1289 child_a
= bdrv_attach_child(parent_b
, a
, "PB-A", &child_of_bds
,
1290 BDRV_CHILD_COW
, &error_abort
);
1293 bdrv_attach_child(parent_a
, a
, "PA-A",
1294 by_parent_cb
? &child_of_bds
: &detach_by_driver_cb_class
,
1295 BDRV_CHILD_DATA
, &error_abort
);
1296 bdrv_graph_wrunlock();
1298 g_assert_cmpint(parent_a
->refcnt
, ==, 1);
1299 g_assert_cmpint(parent_b
->refcnt
, ==, 1);
1300 g_assert_cmpint(a
->refcnt
, ==, 3);
1301 g_assert_cmpint(b
->refcnt
, ==, 2);
1302 g_assert_cmpint(c
->refcnt
, ==, 1);
1304 g_assert(QLIST_FIRST(&parent_b
->children
) == child_a
);
1305 g_assert(QLIST_NEXT(child_a
, next
) == child_b
);
1306 g_assert(QLIST_NEXT(child_b
, next
) == NULL
);
1308 /* Start the evil write request */
1309 detach_by_parent_data
= (struct detach_by_parent_data
) {
1310 .parent_b
= parent_b
,
1313 .by_parent_cb
= by_parent_cb
,
1314 .detach_on_drain
= true,
1316 acb
= blk_aio_preadv(blk
, 0, &qiov
, 0, detach_by_parent_aio_cb
, NULL
);
1317 g_assert(acb
!= NULL
);
1319 /* Drain and check the expected result */
1320 bdrv_drained_begin(parent_b
);
1321 bdrv_drained_begin(a
);
1322 bdrv_drained_begin(b
);
1323 bdrv_drained_begin(c
);
1325 g_assert(detach_by_parent_data
.child_c
!= NULL
);
1327 g_assert_cmpint(parent_a
->refcnt
, ==, 1);
1328 g_assert_cmpint(parent_b
->refcnt
, ==, 1);
1329 g_assert_cmpint(a
->refcnt
, ==, 3);
1330 g_assert_cmpint(b
->refcnt
, ==, 1);
1331 g_assert_cmpint(c
->refcnt
, ==, 2);
1333 g_assert(QLIST_FIRST(&parent_b
->children
) == detach_by_parent_data
.child_c
);
1334 g_assert(QLIST_NEXT(detach_by_parent_data
.child_c
, next
) == child_a
);
1335 g_assert(QLIST_NEXT(child_a
, next
) == NULL
);
1337 g_assert_cmpint(parent_a
->quiesce_counter
, ==, 1);
1338 g_assert_cmpint(parent_b
->quiesce_counter
, ==, 3);
1339 g_assert_cmpint(a
->quiesce_counter
, ==, 1);
1340 g_assert_cmpint(b
->quiesce_counter
, ==, 1);
1341 g_assert_cmpint(c
->quiesce_counter
, ==, 1);
1343 bdrv_drained_end(parent_b
);
1344 bdrv_drained_end(a
);
1345 bdrv_drained_end(b
);
1346 bdrv_drained_end(c
);
1348 bdrv_unref(parent_b
);
1351 g_assert_cmpint(a
->refcnt
, ==, 1);
1352 g_assert_cmpint(b
->refcnt
, ==, 1);
1353 g_assert_cmpint(c
->refcnt
, ==, 1);
1359 static void test_detach_by_parent_cb(void)
1361 test_detach_indirect(true);
1364 static void test_detach_by_driver_cb(void)
1366 test_detach_indirect(false);
1369 static void test_append_to_drained(void)
1372 BlockDriverState
*base
, *overlay
;
1373 BDRVTestState
*base_s
, *overlay_s
;
1375 blk
= blk_new(qemu_get_aio_context(), BLK_PERM_ALL
, BLK_PERM_ALL
);
1376 base
= bdrv_new_open_driver(&bdrv_test
, "base", BDRV_O_RDWR
, &error_abort
);
1377 base_s
= base
->opaque
;
1378 blk_insert_bs(blk
, base
, &error_abort
);
1380 overlay
= bdrv_new_open_driver(&bdrv_test
, "overlay", BDRV_O_RDWR
,
1382 overlay_s
= overlay
->opaque
;
1384 do_drain_begin(BDRV_DRAIN
, base
);
1385 g_assert_cmpint(base
->quiesce_counter
, ==, 1);
1386 g_assert_cmpint(base_s
->drain_count
, ==, 1);
1387 g_assert_cmpint(base
->in_flight
, ==, 0);
1389 aio_context_acquire(qemu_get_aio_context());
1390 bdrv_append(overlay
, base
, &error_abort
);
1391 aio_context_release(qemu_get_aio_context());
1393 g_assert_cmpint(base
->in_flight
, ==, 0);
1394 g_assert_cmpint(overlay
->in_flight
, ==, 0);
1396 g_assert_cmpint(base
->quiesce_counter
, ==, 1);
1397 g_assert_cmpint(base_s
->drain_count
, ==, 1);
1398 g_assert_cmpint(overlay
->quiesce_counter
, ==, 1);
1399 g_assert_cmpint(overlay_s
->drain_count
, ==, 1);
1401 do_drain_end(BDRV_DRAIN
, base
);
1403 g_assert_cmpint(base
->quiesce_counter
, ==, 0);
1404 g_assert_cmpint(base_s
->drain_count
, ==, 0);
1405 g_assert_cmpint(overlay
->quiesce_counter
, ==, 0);
1406 g_assert_cmpint(overlay_s
->drain_count
, ==, 0);
1408 bdrv_unref(overlay
);
1413 static void test_set_aio_context(void)
1415 BlockDriverState
*bs
;
1416 IOThread
*a
= iothread_new();
1417 IOThread
*b
= iothread_new();
1418 AioContext
*ctx_a
= iothread_get_aio_context(a
);
1419 AioContext
*ctx_b
= iothread_get_aio_context(b
);
1421 bs
= bdrv_new_open_driver(&bdrv_test
, "test-node", BDRV_O_RDWR
,
1424 bdrv_drained_begin(bs
);
1425 bdrv_try_change_aio_context(bs
, ctx_a
, NULL
, &error_abort
);
1427 aio_context_acquire(ctx_a
);
1428 bdrv_drained_end(bs
);
1430 bdrv_drained_begin(bs
);
1431 bdrv_try_change_aio_context(bs
, ctx_b
, NULL
, &error_abort
);
1432 aio_context_release(ctx_a
);
1433 aio_context_acquire(ctx_b
);
1434 bdrv_try_change_aio_context(bs
, qemu_get_aio_context(), NULL
, &error_abort
);
1435 aio_context_release(ctx_b
);
1436 bdrv_drained_end(bs
);
1444 typedef struct TestDropBackingBlockJob
{
1446 bool should_complete
;
1448 BlockDriverState
*detach_also
;
1449 BlockDriverState
*bs
;
1450 } TestDropBackingBlockJob
;
1452 static int coroutine_fn
test_drop_backing_job_run(Job
*job
, Error
**errp
)
1454 TestDropBackingBlockJob
*s
=
1455 container_of(job
, TestDropBackingBlockJob
, common
.job
);
1457 while (!s
->should_complete
) {
1458 job_sleep_ns(job
, 0);
1464 static void test_drop_backing_job_commit(Job
*job
)
1466 TestDropBackingBlockJob
*s
=
1467 container_of(job
, TestDropBackingBlockJob
, common
.job
);
1469 bdrv_set_backing_hd(s
->bs
, NULL
, &error_abort
);
1470 bdrv_set_backing_hd(s
->detach_also
, NULL
, &error_abort
);
1472 *s
->did_complete
= true;
1475 static const BlockJobDriver test_drop_backing_job_driver
= {
1477 .instance_size
= sizeof(TestDropBackingBlockJob
),
1478 .free
= block_job_free
,
1479 .user_resume
= block_job_user_resume
,
1480 .run
= test_drop_backing_job_run
,
1481 .commit
= test_drop_backing_job_commit
,
1486 * Creates a child node with three parent nodes on it, and then runs a
1487 * block job on the final one, parent-node-2.
1489 * The job is then asked to complete before a section where the child
1492 * Ending this section will undrain the child's parents, first
1493 * parent-node-2, then parent-node-1, then parent-node-0 -- the parent
1494 * list is in reverse order of how they were added. Ending the drain
1495 * on parent-node-2 will resume the job, thus completing it and
1496 * scheduling job_exit().
1498 * Ending the drain on parent-node-1 will poll the AioContext, which
1499 * lets job_exit() and thus test_drop_backing_job_commit() run. That
1500 * function first removes the child as parent-node-2's backing file.
1502 * In old (and buggy) implementations, there are two problems with
1504 * (A) bdrv_drain_invoke() polls for every node that leaves the
1505 * drained section. This means that job_exit() is scheduled
1506 * before the child has left the drained section. Its
1507 * quiesce_counter is therefore still 1 when it is removed from
1510 * (B) bdrv_replace_child_noperm() calls drained_end() on the old
1511 * child's parents as many times as the child is quiesced. This
1512 * means it will call drained_end() on parent-node-2 once.
1513 * Because parent-node-2 is no longer quiesced at this point, this
1516 * bdrv_replace_child_noperm() therefore must call drained_end() on
1517 * the parent only if it really is still drained because the child is
1520 * If removing child from parent-node-2 was successful (as it should
1521 * be), test_drop_backing_job_commit() will then also remove the child
1522 * from parent-node-0.
1524 * With an old version of our drain infrastructure ((A) above), that
1525 * resulted in the following flow:
1527 * 1. child attempts to leave its drained section. The call recurses
1530 * 2. parent-node-2 leaves the drained section. Polling in
1531 * bdrv_drain_invoke() will schedule job_exit().
1533 * 3. parent-node-1 leaves the drained section. Polling in
1534 * bdrv_drain_invoke() will run job_exit(), thus disconnecting
1535 * parent-node-0 from the child node.
1537 * 4. bdrv_parent_drained_end() uses a QLIST_FOREACH_SAFE() loop to
1538 * iterate over the parents. Thus, it now accesses the BdrvChild
1539 * object that used to connect parent-node-0 and the child node.
1540 * However, that object no longer exists, so it accesses a dangling
1543 * The solution is to only poll once when running a bdrv_drained_end()
1544 * operation, specifically at the end when all drained_end()
1545 * operations for all involved nodes have been scheduled.
1546 * Note that this also solves (A) above, thus hiding (B).
1548 static void test_blockjob_commit_by_drained_end(void)
1550 BlockDriverState
*bs_child
, *bs_parents
[3];
1551 TestDropBackingBlockJob
*job
;
1552 bool job_has_completed
= false;
1555 bs_child
= bdrv_new_open_driver(&bdrv_test
, "child-node", BDRV_O_RDWR
,
1558 for (i
= 0; i
< 3; i
++) {
1560 snprintf(name
, sizeof(name
), "parent-node-%i", i
);
1561 bs_parents
[i
] = bdrv_new_open_driver(&bdrv_test
, name
, BDRV_O_RDWR
,
1563 bdrv_set_backing_hd(bs_parents
[i
], bs_child
, &error_abort
);
1566 job
= block_job_create("job", &test_drop_backing_job_driver
, NULL
,
1567 bs_parents
[2], 0, BLK_PERM_ALL
, 0, 0, NULL
, NULL
,
1569 job
->bs
= bs_parents
[2];
1571 job
->detach_also
= bs_parents
[0];
1572 job
->did_complete
= &job_has_completed
;
1574 job_start(&job
->common
.job
);
1576 job
->should_complete
= true;
1577 bdrv_drained_begin(bs_child
);
1578 g_assert(!job_has_completed
);
1579 bdrv_drained_end(bs_child
);
1580 aio_poll(qemu_get_aio_context(), false);
1581 g_assert(job_has_completed
);
1583 bdrv_unref(bs_parents
[0]);
1584 bdrv_unref(bs_parents
[1]);
1585 bdrv_unref(bs_parents
[2]);
1586 bdrv_unref(bs_child
);
1590 typedef struct TestSimpleBlockJob
{
1592 bool should_complete
;
1594 } TestSimpleBlockJob
;
1596 static int coroutine_fn
test_simple_job_run(Job
*job
, Error
**errp
)
1598 TestSimpleBlockJob
*s
= container_of(job
, TestSimpleBlockJob
, common
.job
);
1600 while (!s
->should_complete
) {
1601 job_sleep_ns(job
, 0);
1607 static void test_simple_job_clean(Job
*job
)
1609 TestSimpleBlockJob
*s
= container_of(job
, TestSimpleBlockJob
, common
.job
);
1610 *s
->did_complete
= true;
1613 static const BlockJobDriver test_simple_job_driver
= {
1615 .instance_size
= sizeof(TestSimpleBlockJob
),
1616 .free
= block_job_free
,
1617 .user_resume
= block_job_user_resume
,
1618 .run
= test_simple_job_run
,
1619 .clean
= test_simple_job_clean
,
1623 static int drop_intermediate_poll_update_filename(BdrvChild
*child
,
1624 BlockDriverState
*new_base
,
1625 const char *filename
,
1629 * We are free to poll here, which may change the block graph, if
1630 * it is not drained.
1633 /* If the job is not drained: Complete it, schedule job_exit() */
1634 aio_poll(qemu_get_current_aio_context(), false);
1635 /* If the job is not drained: Run job_exit(), finish the job */
1636 aio_poll(qemu_get_current_aio_context(), false);
1642 * Test a poll in the midst of bdrv_drop_intermediate().
1644 * bdrv_drop_intermediate() calls BdrvChildClass.update_filename(),
1645 * which can yield or poll. This may lead to graph changes, unless
1646 * the whole subtree in question is drained.
1648 * We test this on the following graph:
1664 * node-2 --chain--> node-1 --chain--> node-0
1666 * We drop node-1 with bdrv_drop_intermediate(top=node-1, base=node-0).
1668 * This first updates node-2's backing filename by invoking
1669 * drop_intermediate_poll_update_filename(), which polls twice. This
1670 * causes the job to finish, which in turns causes the job-node to be
1673 * bdrv_drop_intermediate() uses a QLIST_FOREACH_SAFE() loop, so it
1674 * already has a pointer to the BdrvChild edge between job-node and
1675 * node-1. When it tries to handle that edge, we probably get a
1676 * segmentation fault because the object no longer exists.
1679 * The solution is for bdrv_drop_intermediate() to drain top's
1680 * subtree. This prevents graph changes from happening just because
1681 * BdrvChildClass.update_filename() yields or polls. Thus, the block
1682 * job is paused during that drained section and must finish before or
1685 * (In addition, bdrv_replace_child() must keep the job paused.)
1687 static void test_drop_intermediate_poll(void)
1689 static BdrvChildClass chain_child_class
;
1690 BlockDriverState
*chain
[3];
1691 TestSimpleBlockJob
*job
;
1692 BlockDriverState
*job_node
;
1693 bool job_has_completed
= false;
1697 chain_child_class
= child_of_bds
;
1698 chain_child_class
.update_filename
= drop_intermediate_poll_update_filename
;
1700 for (i
= 0; i
< 3; i
++) {
1702 snprintf(name
, 32, "node-%i", i
);
1704 chain
[i
] = bdrv_new_open_driver(&bdrv_test
, name
, 0, &error_abort
);
1707 job_node
= bdrv_new_open_driver(&bdrv_test
, "job-node", BDRV_O_RDWR
,
1709 bdrv_set_backing_hd(job_node
, chain
[1], &error_abort
);
1712 * Establish the chain last, so the chain links are the first
1713 * elements in the BDS.parents lists
1715 bdrv_graph_wrlock(NULL
);
1716 for (i
= 0; i
< 3; i
++) {
1718 /* Takes the reference to chain[i - 1] */
1719 bdrv_attach_child(chain
[i
], chain
[i
- 1], "chain",
1720 &chain_child_class
, BDRV_CHILD_COW
, &error_abort
);
1723 bdrv_graph_wrunlock();
1725 job
= block_job_create("job", &test_simple_job_driver
, NULL
, job_node
,
1726 0, BLK_PERM_ALL
, 0, 0, NULL
, NULL
, &error_abort
);
1728 /* The job has a reference now */
1729 bdrv_unref(job_node
);
1731 job
->did_complete
= &job_has_completed
;
1733 job_start(&job
->common
.job
);
1734 job
->should_complete
= true;
1736 g_assert(!job_has_completed
);
1737 ret
= bdrv_drop_intermediate(chain
[1], chain
[0], NULL
);
1738 aio_poll(qemu_get_aio_context(), false);
1740 g_assert(job_has_completed
);
1742 bdrv_unref(chain
[2]);
1746 typedef struct BDRVReplaceTestState
{
1747 bool setup_completed
;
1754 bool yield_before_read
;
1756 Coroutine
*drain_co
;
1757 } BDRVReplaceTestState
;
1759 static void bdrv_replace_test_close(BlockDriverState
*bs
)
1764 * If @bs has a backing file:
1765 * Yield if .yield_before_read is true (and wait for drain_begin to
1767 * Forward the read to bs->backing. Set .has_read to true.
1768 * If drain_begin has woken us, wake it in turn.
1771 * Set .has_read to true and return success.
1773 static int coroutine_fn GRAPH_RDLOCK
1774 bdrv_replace_test_co_preadv(BlockDriverState
*bs
, int64_t offset
, int64_t bytes
,
1775 QEMUIOVector
*qiov
, BdrvRequestFlags flags
)
1777 BDRVReplaceTestState
*s
= bs
->opaque
;
1782 g_assert(!s
->drain_count
);
1784 s
->io_co
= qemu_coroutine_self();
1785 if (s
->yield_before_read
) {
1786 s
->yield_before_read
= false;
1787 qemu_coroutine_yield();
1791 ret
= bdrv_co_preadv(bs
->backing
, offset
, bytes
, qiov
, 0);
1794 /* Wake up drain_co if it runs */
1796 aio_co_wake(s
->drain_co
);
1806 static void coroutine_fn
bdrv_replace_test_drain_co(void *opaque
)
1808 BlockDriverState
*bs
= opaque
;
1809 BDRVReplaceTestState
*s
= bs
->opaque
;
1811 /* Keep waking io_co up until it is done */
1813 aio_co_wake(s
->io_co
);
1815 qemu_coroutine_yield();
1818 bdrv_dec_in_flight(bs
);
1822 * If .drain_count is 0, wake up .io_co if there is one; and set
1824 * Increment .drain_count.
1826 static void bdrv_replace_test_drain_begin(BlockDriverState
*bs
)
1828 BDRVReplaceTestState
*s
= bs
->opaque
;
1830 if (!s
->setup_completed
) {
1834 if (!s
->drain_count
) {
1835 s
->drain_co
= qemu_coroutine_create(bdrv_replace_test_drain_co
, bs
);
1836 bdrv_inc_in_flight(bs
);
1837 aio_co_enter(bdrv_get_aio_context(bs
), s
->drain_co
);
1838 s
->was_drained
= true;
1843 static void coroutine_fn
bdrv_replace_test_read_entry(void *opaque
)
1845 BlockDriverState
*bs
= opaque
;
1847 QEMUIOVector qiov
= QEMU_IOVEC_INIT_BUF(qiov
, &data
, 1);
1850 /* Queue a read request post-drain */
1851 bdrv_graph_co_rdlock();
1852 ret
= bdrv_replace_test_co_preadv(bs
, 0, 1, &qiov
, 0);
1853 bdrv_graph_co_rdunlock();
1856 bdrv_dec_in_flight(bs
);
1860 * Reduce .drain_count, set .was_undrained once it reaches 0.
1861 * If .drain_count reaches 0 and the node has a backing file, issue a
1864 static void bdrv_replace_test_drain_end(BlockDriverState
*bs
)
1866 BDRVReplaceTestState
*s
= bs
->opaque
;
1868 if (!s
->setup_completed
) {
1872 g_assert(s
->drain_count
> 0);
1873 if (!--s
->drain_count
) {
1874 s
->was_undrained
= true;
1877 Coroutine
*co
= qemu_coroutine_create(bdrv_replace_test_read_entry
,
1879 bdrv_inc_in_flight(bs
);
1880 aio_co_enter(bdrv_get_aio_context(bs
), co
);
1885 static BlockDriver bdrv_replace_test
= {
1886 .format_name
= "replace_test",
1887 .instance_size
= sizeof(BDRVReplaceTestState
),
1888 .supports_backing
= true,
1890 .bdrv_close
= bdrv_replace_test_close
,
1891 .bdrv_co_preadv
= bdrv_replace_test_co_preadv
,
1893 .bdrv_drain_begin
= bdrv_replace_test_drain_begin
,
1894 .bdrv_drain_end
= bdrv_replace_test_drain_end
,
1896 .bdrv_child_perm
= bdrv_default_perms
,
1899 static void coroutine_fn
test_replace_child_mid_drain_read_co(void *opaque
)
1904 ret
= blk_co_pread(opaque
, 0, 1, &data
, 0);
1909 * We test two things:
1910 * (1) bdrv_replace_child_noperm() must not undrain the parent if both
1911 * children are drained.
1912 * (2) bdrv_replace_child_noperm() must never flush I/O requests to a
1913 * drained child. If the old child is drained, it must flush I/O
1914 * requests after the new one has been attached. If the new child
1915 * is drained, it must flush I/O requests before the old one is
1918 * To do so, we create one parent node and two child nodes; then
1919 * attach one of the children (old_child_bs) to the parent, then
1920 * drain both old_child_bs and new_child_bs according to
1921 * old_drain_count and new_drain_count, respectively, and finally
1922 * we invoke bdrv_replace_node() to replace old_child_bs by
1925 * The test block driver we use here (bdrv_replace_test) has a read
1927 * - For the parent node, can optionally yield, and then forwards the
1928 * read to bdrv_preadv(),
1929 * - For the child node, just returns immediately.
1931 * If the read yields, the drain_begin function will wake it up.
1933 * The drain_end function issues a read on the parent once it is fully
1934 * undrained (which simulates requests starting to come in again).
1936 static void do_test_replace_child_mid_drain(int old_drain_count
,
1937 int new_drain_count
)
1939 BlockBackend
*parent_blk
;
1940 BlockDriverState
*parent_bs
;
1941 BlockDriverState
*old_child_bs
, *new_child_bs
;
1942 BDRVReplaceTestState
*parent_s
;
1943 BDRVReplaceTestState
*old_child_s
, *new_child_s
;
1947 parent_bs
= bdrv_new_open_driver(&bdrv_replace_test
, "parent", 0,
1949 parent_s
= parent_bs
->opaque
;
1951 parent_blk
= blk_new(qemu_get_aio_context(),
1952 BLK_PERM_CONSISTENT_READ
, BLK_PERM_ALL
);
1953 blk_insert_bs(parent_blk
, parent_bs
, &error_abort
);
1955 old_child_bs
= bdrv_new_open_driver(&bdrv_replace_test
, "old-child", 0,
1957 new_child_bs
= bdrv_new_open_driver(&bdrv_replace_test
, "new-child", 0,
1959 old_child_s
= old_child_bs
->opaque
;
1960 new_child_s
= new_child_bs
->opaque
;
1962 /* So that we can read something */
1963 parent_bs
->total_sectors
= 1;
1964 old_child_bs
->total_sectors
= 1;
1965 new_child_bs
->total_sectors
= 1;
1967 bdrv_ref(old_child_bs
);
1968 bdrv_graph_wrlock(NULL
);
1969 bdrv_attach_child(parent_bs
, old_child_bs
, "child", &child_of_bds
,
1970 BDRV_CHILD_COW
, &error_abort
);
1971 bdrv_graph_wrunlock();
1972 parent_s
->setup_completed
= true;
1974 for (i
= 0; i
< old_drain_count
; i
++) {
1975 bdrv_drained_begin(old_child_bs
);
1977 for (i
= 0; i
< new_drain_count
; i
++) {
1978 bdrv_drained_begin(new_child_bs
);
1981 if (!old_drain_count
) {
1983 * Start a read operation that will yield, so it will not
1984 * complete before the node is drained.
1986 parent_s
->yield_before_read
= true;
1987 io_co
= qemu_coroutine_create(test_replace_child_mid_drain_read_co
,
1989 qemu_coroutine_enter(io_co
);
1992 /* If we have started a read operation, it should have yielded */
1993 g_assert(!parent_s
->has_read
);
1995 /* Reset drained status so we can see what bdrv_replace_node() does */
1996 parent_s
->was_drained
= false;
1997 parent_s
->was_undrained
= false;
1999 g_assert(parent_bs
->quiesce_counter
== old_drain_count
);
2000 bdrv_replace_node(old_child_bs
, new_child_bs
, &error_abort
);
2001 g_assert(parent_bs
->quiesce_counter
== new_drain_count
);
2003 if (!old_drain_count
&& !new_drain_count
) {
2005 * From undrained to undrained drains and undrains the parent,
2006 * because bdrv_replace_node() contains a drained section for
2009 g_assert(parent_s
->was_drained
&& parent_s
->was_undrained
);
2010 } else if (!old_drain_count
&& new_drain_count
) {
2012 * From undrained to drained should drain the parent and keep
2015 g_assert(parent_s
->was_drained
&& !parent_s
->was_undrained
);
2016 } else if (old_drain_count
&& !new_drain_count
) {
2018 * From drained to undrained should undrain the parent and
2021 g_assert(!parent_s
->was_drained
&& parent_s
->was_undrained
);
2022 } else /* if (old_drain_count && new_drain_count) */ {
2024 * From drained to drained must not undrain the parent at any
2027 g_assert(!parent_s
->was_drained
&& !parent_s
->was_undrained
);
2030 if (!old_drain_count
|| !new_drain_count
) {
2032 * If !old_drain_count, we have started a read request before
2033 * bdrv_replace_node(). If !new_drain_count, the parent must
2034 * have been undrained at some point, and
2035 * bdrv_replace_test_co_drain_end() starts a read request
2038 g_assert(parent_s
->has_read
);
2041 * If the parent was never undrained, there is no way to start
2044 g_assert(!parent_s
->has_read
);
2047 /* A drained child must have not received any request */
2048 g_assert(!(old_drain_count
&& old_child_s
->has_read
));
2049 g_assert(!(new_drain_count
&& new_child_s
->has_read
));
2051 for (i
= 0; i
< new_drain_count
; i
++) {
2052 bdrv_drained_end(new_child_bs
);
2054 for (i
= 0; i
< old_drain_count
; i
++) {
2055 bdrv_drained_end(old_child_bs
);
2059 * By now, bdrv_replace_test_co_drain_end() must have been called
2060 * at some point while the new child was attached to the parent.
2062 g_assert(parent_s
->has_read
);
2063 g_assert(new_child_s
->has_read
);
2065 blk_unref(parent_blk
);
2066 bdrv_unref(parent_bs
);
2067 bdrv_unref(old_child_bs
);
2068 bdrv_unref(new_child_bs
);
2071 static void test_replace_child_mid_drain(void)
2073 int old_drain_count
, new_drain_count
;
2075 for (old_drain_count
= 0; old_drain_count
< 2; old_drain_count
++) {
2076 for (new_drain_count
= 0; new_drain_count
< 2; new_drain_count
++) {
2077 do_test_replace_child_mid_drain(old_drain_count
, new_drain_count
);
2082 int main(int argc
, char **argv
)
2087 qemu_init_main_loop(&error_abort
);
2089 g_test_init(&argc
, &argv
, NULL
);
2090 qemu_event_init(&done_event
, false);
2092 g_test_add_func("/bdrv-drain/driver-cb/drain_all", test_drv_cb_drain_all
);
2093 g_test_add_func("/bdrv-drain/driver-cb/drain", test_drv_cb_drain
);
2095 g_test_add_func("/bdrv-drain/driver-cb/co/drain_all",
2096 test_drv_cb_co_drain_all
);
2097 g_test_add_func("/bdrv-drain/driver-cb/co/drain", test_drv_cb_co_drain
);
2099 g_test_add_func("/bdrv-drain/quiesce/drain_all", test_quiesce_drain_all
);
2100 g_test_add_func("/bdrv-drain/quiesce/drain", test_quiesce_drain
);
2102 g_test_add_func("/bdrv-drain/quiesce/co/drain_all",
2103 test_quiesce_co_drain_all
);
2104 g_test_add_func("/bdrv-drain/quiesce/co/drain", test_quiesce_co_drain
);
2106 g_test_add_func("/bdrv-drain/nested", test_nested
);
2108 g_test_add_func("/bdrv-drain/graph-change/drain_all",
2109 test_graph_change_drain_all
);
2111 g_test_add_func("/bdrv-drain/iothread/drain_all", test_iothread_drain_all
);
2112 g_test_add_func("/bdrv-drain/iothread/drain", test_iothread_drain
);
2114 g_test_add_func("/bdrv-drain/blockjob/drain_all", test_blockjob_drain_all
);
2115 g_test_add_func("/bdrv-drain/blockjob/drain", test_blockjob_drain
);
2117 g_test_add_func("/bdrv-drain/blockjob/error/drain_all",
2118 test_blockjob_error_drain_all
);
2119 g_test_add_func("/bdrv-drain/blockjob/error/drain",
2120 test_blockjob_error_drain
);
2122 g_test_add_func("/bdrv-drain/blockjob/iothread/drain_all",
2123 test_blockjob_iothread_drain_all
);
2124 g_test_add_func("/bdrv-drain/blockjob/iothread/drain",
2125 test_blockjob_iothread_drain
);
2127 g_test_add_func("/bdrv-drain/blockjob/iothread/error/drain_all",
2128 test_blockjob_iothread_error_drain_all
);
2129 g_test_add_func("/bdrv-drain/blockjob/iothread/error/drain",
2130 test_blockjob_iothread_error_drain
);
2132 g_test_add_func("/bdrv-drain/deletion/drain", test_delete_by_drain
);
2133 g_test_add_func("/bdrv-drain/detach/drain_all", test_detach_by_drain_all
);
2134 g_test_add_func("/bdrv-drain/detach/drain", test_detach_by_drain
);
2135 g_test_add_func("/bdrv-drain/detach/parent_cb", test_detach_by_parent_cb
);
2136 g_test_add_func("/bdrv-drain/detach/driver_cb", test_detach_by_driver_cb
);
2138 g_test_add_func("/bdrv-drain/attach/drain", test_append_to_drained
);
2140 g_test_add_func("/bdrv-drain/set_aio_context", test_set_aio_context
);
2142 g_test_add_func("/bdrv-drain/blockjob/commit_by_drained_end",
2143 test_blockjob_commit_by_drained_end
);
2145 g_test_add_func("/bdrv-drain/bdrv_drop_intermediate/poll",
2146 test_drop_intermediate_poll
);
2148 g_test_add_func("/bdrv-drain/replace_child/mid-drain",
2149 test_replace_child_mid_drain
);
2152 qemu_event_destroy(&done_event
);