migration: Use migrate_max_postcopy_bandwidth()
[qemu/armbru.git] / tests / unit / test-bdrv-drain.c
blobd9d38070621af9a7734890ba35761ebf4d615eff
1 /*
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
22 * THE SOFTWARE.
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"
31 #include "iothread.h"
33 static QemuEvent done_event;
35 typedef struct BDRVTestState {
36 int drain_count;
37 AioContext *bh_indirection_ctx;
38 bool sleep_in_drain_begin;
39 } BDRVTestState;
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;
52 s->drain_count++;
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;
63 s->drain_count--;
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)
74 aio_co_wake(opaque);
77 static int coroutine_fn bdrv_test_co_preadv(BlockDriverState *bs,
78 int64_t offset, int64_t bytes,
79 QEMUIOVector *qiov,
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();
96 return 0;
99 static int bdrv_test_change_backing_file(BlockDriverState *bs,
100 const char *backing_file,
101 const char *backing_fmt)
103 return 0;
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;
125 *aio_ret = ret;
128 typedef struct CallInCoroutineData {
129 void (*entry)(void);
130 bool done;
131 } CallInCoroutineData;
133 static coroutine_fn void call_in_coroutine_entry(void *opaque)
135 CallInCoroutineData *data = opaque;
137 data->entry();
138 data->done = true;
141 static void call_in_coroutine(void (*entry)(void))
143 Coroutine *co;
144 CallInCoroutineData data = {
145 .entry = entry,
146 .done = false,
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);
156 enum drain_type {
157 BDRV_DRAIN_ALL,
158 BDRV_DRAIN,
159 DRAIN_TYPE_MAX,
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 void do_drain_end_unlocked(enum drain_type drain_type, BlockDriverState *bs)
193 if (drain_type != BDRV_DRAIN_ALL) {
194 aio_context_acquire(bdrv_get_aio_context(bs));
196 do_drain_end(drain_type, bs);
197 if (drain_type != BDRV_DRAIN_ALL) {
198 aio_context_release(bdrv_get_aio_context(bs));
202 static void test_drv_cb_common(enum drain_type drain_type, bool recursive)
204 BlockBackend *blk;
205 BlockDriverState *bs, *backing;
206 BDRVTestState *s, *backing_s;
207 BlockAIOCB *acb;
208 int aio_ret;
210 QEMUIOVector qiov = QEMU_IOVEC_INIT_BUF(qiov, NULL, 0);
212 blk = blk_new(qemu_get_aio_context(), BLK_PERM_ALL, BLK_PERM_ALL);
213 bs = bdrv_new_open_driver(&bdrv_test, "test-node", BDRV_O_RDWR,
214 &error_abort);
215 s = bs->opaque;
216 blk_insert_bs(blk, bs, &error_abort);
218 backing = bdrv_new_open_driver(&bdrv_test, "backing", 0, &error_abort);
219 backing_s = backing->opaque;
220 bdrv_set_backing_hd(bs, backing, &error_abort);
222 /* Simple bdrv_drain_all_begin/end pair, check that CBs are called */
223 g_assert_cmpint(s->drain_count, ==, 0);
224 g_assert_cmpint(backing_s->drain_count, ==, 0);
226 do_drain_begin(drain_type, bs);
228 g_assert_cmpint(s->drain_count, ==, 1);
229 g_assert_cmpint(backing_s->drain_count, ==, !!recursive);
231 do_drain_end(drain_type, bs);
233 g_assert_cmpint(s->drain_count, ==, 0);
234 g_assert_cmpint(backing_s->drain_count, ==, 0);
236 /* Now do the same while a request is pending */
237 aio_ret = -EINPROGRESS;
238 acb = blk_aio_preadv(blk, 0, &qiov, 0, aio_ret_cb, &aio_ret);
239 g_assert(acb != NULL);
240 g_assert_cmpint(aio_ret, ==, -EINPROGRESS);
242 g_assert_cmpint(s->drain_count, ==, 0);
243 g_assert_cmpint(backing_s->drain_count, ==, 0);
245 do_drain_begin(drain_type, bs);
247 g_assert_cmpint(aio_ret, ==, 0);
248 g_assert_cmpint(s->drain_count, ==, 1);
249 g_assert_cmpint(backing_s->drain_count, ==, !!recursive);
251 do_drain_end(drain_type, bs);
253 g_assert_cmpint(s->drain_count, ==, 0);
254 g_assert_cmpint(backing_s->drain_count, ==, 0);
256 bdrv_unref(backing);
257 bdrv_unref(bs);
258 blk_unref(blk);
261 static void test_drv_cb_drain_all(void)
263 test_drv_cb_common(BDRV_DRAIN_ALL, true);
266 static void test_drv_cb_drain(void)
268 test_drv_cb_common(BDRV_DRAIN, false);
271 static void test_drv_cb_co_drain_all(void)
273 call_in_coroutine(test_drv_cb_drain_all);
276 static void test_drv_cb_co_drain(void)
278 call_in_coroutine(test_drv_cb_drain);
281 static void test_quiesce_common(enum drain_type drain_type, bool recursive)
283 BlockBackend *blk;
284 BlockDriverState *bs, *backing;
286 blk = blk_new(qemu_get_aio_context(), BLK_PERM_ALL, BLK_PERM_ALL);
287 bs = bdrv_new_open_driver(&bdrv_test, "test-node", BDRV_O_RDWR,
288 &error_abort);
289 blk_insert_bs(blk, bs, &error_abort);
291 backing = bdrv_new_open_driver(&bdrv_test, "backing", 0, &error_abort);
292 bdrv_set_backing_hd(bs, backing, &error_abort);
294 g_assert_cmpint(bs->quiesce_counter, ==, 0);
295 g_assert_cmpint(backing->quiesce_counter, ==, 0);
297 do_drain_begin(drain_type, bs);
299 if (drain_type == BDRV_DRAIN_ALL) {
300 g_assert_cmpint(bs->quiesce_counter, ==, 2);
301 } else {
302 g_assert_cmpint(bs->quiesce_counter, ==, 1);
304 g_assert_cmpint(backing->quiesce_counter, ==, !!recursive);
306 do_drain_end(drain_type, bs);
308 g_assert_cmpint(bs->quiesce_counter, ==, 0);
309 g_assert_cmpint(backing->quiesce_counter, ==, 0);
311 bdrv_unref(backing);
312 bdrv_unref(bs);
313 blk_unref(blk);
316 static void test_quiesce_drain_all(void)
318 test_quiesce_common(BDRV_DRAIN_ALL, true);
321 static void test_quiesce_drain(void)
323 test_quiesce_common(BDRV_DRAIN, false);
326 static void test_quiesce_co_drain_all(void)
328 call_in_coroutine(test_quiesce_drain_all);
331 static void test_quiesce_co_drain(void)
333 call_in_coroutine(test_quiesce_drain);
336 static void test_nested(void)
338 BlockBackend *blk;
339 BlockDriverState *bs, *backing;
340 BDRVTestState *s, *backing_s;
341 enum drain_type outer, inner;
343 blk = blk_new(qemu_get_aio_context(), BLK_PERM_ALL, BLK_PERM_ALL);
344 bs = bdrv_new_open_driver(&bdrv_test, "test-node", BDRV_O_RDWR,
345 &error_abort);
346 s = bs->opaque;
347 blk_insert_bs(blk, bs, &error_abort);
349 backing = bdrv_new_open_driver(&bdrv_test, "backing", 0, &error_abort);
350 backing_s = backing->opaque;
351 bdrv_set_backing_hd(bs, backing, &error_abort);
353 for (outer = 0; outer < DRAIN_TYPE_MAX; outer++) {
354 for (inner = 0; inner < DRAIN_TYPE_MAX; inner++) {
355 int backing_quiesce = (outer == BDRV_DRAIN_ALL) +
356 (inner == BDRV_DRAIN_ALL);
358 g_assert_cmpint(bs->quiesce_counter, ==, 0);
359 g_assert_cmpint(backing->quiesce_counter, ==, 0);
360 g_assert_cmpint(s->drain_count, ==, 0);
361 g_assert_cmpint(backing_s->drain_count, ==, 0);
363 do_drain_begin(outer, bs);
364 do_drain_begin(inner, bs);
366 g_assert_cmpint(bs->quiesce_counter, ==, 2 + !!backing_quiesce);
367 g_assert_cmpint(backing->quiesce_counter, ==, backing_quiesce);
368 g_assert_cmpint(s->drain_count, ==, 1);
369 g_assert_cmpint(backing_s->drain_count, ==, !!backing_quiesce);
371 do_drain_end(inner, bs);
372 do_drain_end(outer, bs);
374 g_assert_cmpint(bs->quiesce_counter, ==, 0);
375 g_assert_cmpint(backing->quiesce_counter, ==, 0);
376 g_assert_cmpint(s->drain_count, ==, 0);
377 g_assert_cmpint(backing_s->drain_count, ==, 0);
381 bdrv_unref(backing);
382 bdrv_unref(bs);
383 blk_unref(blk);
386 static void test_graph_change_drain_all(void)
388 BlockBackend *blk_a, *blk_b;
389 BlockDriverState *bs_a, *bs_b;
390 BDRVTestState *a_s, *b_s;
392 /* Create node A with a BlockBackend */
393 blk_a = blk_new(qemu_get_aio_context(), BLK_PERM_ALL, BLK_PERM_ALL);
394 bs_a = bdrv_new_open_driver(&bdrv_test, "test-node-a", BDRV_O_RDWR,
395 &error_abort);
396 a_s = bs_a->opaque;
397 blk_insert_bs(blk_a, bs_a, &error_abort);
399 g_assert_cmpint(bs_a->quiesce_counter, ==, 0);
400 g_assert_cmpint(a_s->drain_count, ==, 0);
402 /* Call bdrv_drain_all_begin() */
403 bdrv_drain_all_begin();
405 g_assert_cmpint(bs_a->quiesce_counter, ==, 1);
406 g_assert_cmpint(a_s->drain_count, ==, 1);
408 /* Create node B with a BlockBackend */
409 blk_b = blk_new(qemu_get_aio_context(), BLK_PERM_ALL, BLK_PERM_ALL);
410 bs_b = bdrv_new_open_driver(&bdrv_test, "test-node-b", BDRV_O_RDWR,
411 &error_abort);
412 b_s = bs_b->opaque;
413 blk_insert_bs(blk_b, bs_b, &error_abort);
415 g_assert_cmpint(bs_a->quiesce_counter, ==, 1);
416 g_assert_cmpint(bs_b->quiesce_counter, ==, 1);
417 g_assert_cmpint(a_s->drain_count, ==, 1);
418 g_assert_cmpint(b_s->drain_count, ==, 1);
420 /* Unref and finally delete node A */
421 blk_unref(blk_a);
423 g_assert_cmpint(bs_a->quiesce_counter, ==, 1);
424 g_assert_cmpint(bs_b->quiesce_counter, ==, 1);
425 g_assert_cmpint(a_s->drain_count, ==, 1);
426 g_assert_cmpint(b_s->drain_count, ==, 1);
428 bdrv_unref(bs_a);
430 g_assert_cmpint(bs_b->quiesce_counter, ==, 1);
431 g_assert_cmpint(b_s->drain_count, ==, 1);
433 /* End the drained section */
434 bdrv_drain_all_end();
436 g_assert_cmpint(bs_b->quiesce_counter, ==, 0);
437 g_assert_cmpint(b_s->drain_count, ==, 0);
438 g_assert_cmpint(qemu_get_aio_context()->external_disable_cnt, ==, 0);
440 bdrv_unref(bs_b);
441 blk_unref(blk_b);
444 struct test_iothread_data {
445 BlockDriverState *bs;
446 enum drain_type drain_type;
447 int *aio_ret;
450 static void test_iothread_drain_entry(void *opaque)
452 struct test_iothread_data *data = opaque;
454 aio_context_acquire(bdrv_get_aio_context(data->bs));
455 do_drain_begin(data->drain_type, data->bs);
456 g_assert_cmpint(*data->aio_ret, ==, 0);
457 do_drain_end(data->drain_type, data->bs);
458 aio_context_release(bdrv_get_aio_context(data->bs));
460 qemu_event_set(&done_event);
463 static void test_iothread_aio_cb(void *opaque, int ret)
465 int *aio_ret = opaque;
466 *aio_ret = ret;
467 qemu_event_set(&done_event);
470 static void test_iothread_main_thread_bh(void *opaque)
472 struct test_iothread_data *data = opaque;
474 /* Test that the AioContext is not yet locked in a random BH that is
475 * executed during drain, otherwise this would deadlock. */
476 aio_context_acquire(bdrv_get_aio_context(data->bs));
477 bdrv_flush(data->bs);
478 aio_context_release(bdrv_get_aio_context(data->bs));
482 * Starts an AIO request on a BDS that runs in the AioContext of iothread 1.
483 * The request involves a BH on iothread 2 before it can complete.
485 * @drain_thread = 0 means that do_drain_begin/end are called from the main
486 * thread, @drain_thread = 1 means that they are called from iothread 1. Drain
487 * for this BDS cannot be called from iothread 2 because only the main thread
488 * may do cross-AioContext polling.
490 static void test_iothread_common(enum drain_type drain_type, int drain_thread)
492 BlockBackend *blk;
493 BlockDriverState *bs;
494 BDRVTestState *s;
495 BlockAIOCB *acb;
496 int aio_ret;
497 struct test_iothread_data data;
499 IOThread *a = iothread_new();
500 IOThread *b = iothread_new();
501 AioContext *ctx_a = iothread_get_aio_context(a);
502 AioContext *ctx_b = iothread_get_aio_context(b);
504 QEMUIOVector qiov = QEMU_IOVEC_INIT_BUF(qiov, NULL, 0);
506 /* bdrv_drain_all() may only be called from the main loop thread */
507 if (drain_type == BDRV_DRAIN_ALL && drain_thread != 0) {
508 goto out;
511 blk = blk_new(qemu_get_aio_context(), BLK_PERM_ALL, BLK_PERM_ALL);
512 bs = bdrv_new_open_driver(&bdrv_test, "test-node", BDRV_O_RDWR,
513 &error_abort);
514 s = bs->opaque;
515 blk_insert_bs(blk, bs, &error_abort);
516 blk_set_disable_request_queuing(blk, true);
518 blk_set_aio_context(blk, ctx_a, &error_abort);
519 aio_context_acquire(ctx_a);
521 s->bh_indirection_ctx = ctx_b;
523 aio_ret = -EINPROGRESS;
524 qemu_event_reset(&done_event);
526 if (drain_thread == 0) {
527 acb = blk_aio_preadv(blk, 0, &qiov, 0, test_iothread_aio_cb, &aio_ret);
528 } else {
529 acb = blk_aio_preadv(blk, 0, &qiov, 0, aio_ret_cb, &aio_ret);
531 g_assert(acb != NULL);
532 g_assert_cmpint(aio_ret, ==, -EINPROGRESS);
534 aio_context_release(ctx_a);
536 data = (struct test_iothread_data) {
537 .bs = bs,
538 .drain_type = drain_type,
539 .aio_ret = &aio_ret,
542 switch (drain_thread) {
543 case 0:
544 if (drain_type != BDRV_DRAIN_ALL) {
545 aio_context_acquire(ctx_a);
548 aio_bh_schedule_oneshot(ctx_a, test_iothread_main_thread_bh, &data);
550 /* The request is running on the IOThread a. Draining its block device
551 * will make sure that it has completed as far as the BDS is concerned,
552 * but the drain in this thread can continue immediately after
553 * bdrv_dec_in_flight() and aio_ret might be assigned only slightly
554 * later. */
555 do_drain_begin(drain_type, bs);
556 g_assert_cmpint(bs->in_flight, ==, 0);
558 if (drain_type != BDRV_DRAIN_ALL) {
559 aio_context_release(ctx_a);
561 qemu_event_wait(&done_event);
562 if (drain_type != BDRV_DRAIN_ALL) {
563 aio_context_acquire(ctx_a);
566 g_assert_cmpint(aio_ret, ==, 0);
567 do_drain_end(drain_type, bs);
569 if (drain_type != BDRV_DRAIN_ALL) {
570 aio_context_release(ctx_a);
572 break;
573 case 1:
574 aio_bh_schedule_oneshot(ctx_a, test_iothread_drain_entry, &data);
575 qemu_event_wait(&done_event);
576 break;
577 default:
578 g_assert_not_reached();
581 aio_context_acquire(ctx_a);
582 blk_set_aio_context(blk, qemu_get_aio_context(), &error_abort);
583 aio_context_release(ctx_a);
585 bdrv_unref(bs);
586 blk_unref(blk);
588 out:
589 iothread_join(a);
590 iothread_join(b);
593 static void test_iothread_drain_all(void)
595 test_iothread_common(BDRV_DRAIN_ALL, 0);
596 test_iothread_common(BDRV_DRAIN_ALL, 1);
599 static void test_iothread_drain(void)
601 test_iothread_common(BDRV_DRAIN, 0);
602 test_iothread_common(BDRV_DRAIN, 1);
606 typedef struct TestBlockJob {
607 BlockJob common;
608 BlockDriverState *bs;
609 int run_ret;
610 int prepare_ret;
611 bool running;
612 bool should_complete;
613 } TestBlockJob;
615 static int test_job_prepare(Job *job)
617 TestBlockJob *s = container_of(job, TestBlockJob, common.job);
619 /* Provoke an AIO_WAIT_WHILE() call to verify there is no deadlock */
620 bdrv_flush(s->bs);
621 return s->prepare_ret;
624 static void test_job_commit(Job *job)
626 TestBlockJob *s = container_of(job, TestBlockJob, common.job);
628 /* Provoke an AIO_WAIT_WHILE() call to verify there is no deadlock */
629 bdrv_flush(s->bs);
632 static void test_job_abort(Job *job)
634 TestBlockJob *s = container_of(job, TestBlockJob, common.job);
636 /* Provoke an AIO_WAIT_WHILE() call to verify there is no deadlock */
637 bdrv_flush(s->bs);
640 static int coroutine_fn test_job_run(Job *job, Error **errp)
642 TestBlockJob *s = container_of(job, TestBlockJob, common.job);
644 /* We are running the actual job code past the pause point in
645 * job_co_entry(). */
646 s->running = true;
648 job_transition_to_ready(&s->common.job);
649 while (!s->should_complete) {
650 /* Avoid job_sleep_ns() because it marks the job as !busy. We want to
651 * emulate some actual activity (probably some I/O) here so that drain
652 * has to wait for this activity to stop. */
653 qemu_co_sleep_ns(QEMU_CLOCK_REALTIME, 1000000);
655 job_pause_point(&s->common.job);
658 return s->run_ret;
661 static void test_job_complete(Job *job, Error **errp)
663 TestBlockJob *s = container_of(job, TestBlockJob, common.job);
664 s->should_complete = true;
667 BlockJobDriver test_job_driver = {
668 .job_driver = {
669 .instance_size = sizeof(TestBlockJob),
670 .free = block_job_free,
671 .user_resume = block_job_user_resume,
672 .run = test_job_run,
673 .complete = test_job_complete,
674 .prepare = test_job_prepare,
675 .commit = test_job_commit,
676 .abort = test_job_abort,
680 enum test_job_result {
681 TEST_JOB_SUCCESS,
682 TEST_JOB_FAIL_RUN,
683 TEST_JOB_FAIL_PREPARE,
686 enum test_job_drain_node {
687 TEST_JOB_DRAIN_SRC,
688 TEST_JOB_DRAIN_SRC_CHILD,
691 static void test_blockjob_common_drain_node(enum drain_type drain_type,
692 bool use_iothread,
693 enum test_job_result result,
694 enum test_job_drain_node drain_node)
696 BlockBackend *blk_src, *blk_target;
697 BlockDriverState *src, *src_backing, *src_overlay, *target, *drain_bs;
698 BlockJob *job;
699 TestBlockJob *tjob;
700 IOThread *iothread = NULL;
701 AioContext *ctx;
702 int ret;
704 src = bdrv_new_open_driver(&bdrv_test, "source", BDRV_O_RDWR,
705 &error_abort);
706 src_backing = bdrv_new_open_driver(&bdrv_test, "source-backing",
707 BDRV_O_RDWR, &error_abort);
708 src_overlay = bdrv_new_open_driver(&bdrv_test, "source-overlay",
709 BDRV_O_RDWR, &error_abort);
711 bdrv_set_backing_hd(src_overlay, src, &error_abort);
712 bdrv_unref(src);
713 bdrv_set_backing_hd(src, src_backing, &error_abort);
714 bdrv_unref(src_backing);
716 blk_src = blk_new(qemu_get_aio_context(), BLK_PERM_ALL, BLK_PERM_ALL);
717 blk_insert_bs(blk_src, src_overlay, &error_abort);
719 switch (drain_node) {
720 case TEST_JOB_DRAIN_SRC:
721 drain_bs = src;
722 break;
723 case TEST_JOB_DRAIN_SRC_CHILD:
724 drain_bs = src_backing;
725 break;
726 default:
727 g_assert_not_reached();
730 if (use_iothread) {
731 iothread = iothread_new();
732 ctx = iothread_get_aio_context(iothread);
733 blk_set_aio_context(blk_src, ctx, &error_abort);
734 } else {
735 ctx = qemu_get_aio_context();
738 target = bdrv_new_open_driver(&bdrv_test, "target", BDRV_O_RDWR,
739 &error_abort);
740 blk_target = blk_new(qemu_get_aio_context(), BLK_PERM_ALL, BLK_PERM_ALL);
741 blk_insert_bs(blk_target, target, &error_abort);
742 blk_set_allow_aio_context_change(blk_target, true);
744 aio_context_acquire(ctx);
745 tjob = block_job_create("job0", &test_job_driver, NULL, src,
746 0, BLK_PERM_ALL,
747 0, 0, NULL, NULL, &error_abort);
748 tjob->bs = src;
749 job = &tjob->common;
750 block_job_add_bdrv(job, "target", target, 0, BLK_PERM_ALL, &error_abort);
752 switch (result) {
753 case TEST_JOB_SUCCESS:
754 break;
755 case TEST_JOB_FAIL_RUN:
756 tjob->run_ret = -EIO;
757 break;
758 case TEST_JOB_FAIL_PREPARE:
759 tjob->prepare_ret = -EIO;
760 break;
762 aio_context_release(ctx);
764 job_start(&job->job);
766 if (use_iothread) {
767 /* job_co_entry() is run in the I/O thread, wait for the actual job
768 * code to start (we don't want to catch the job in the pause point in
769 * job_co_entry(). */
770 while (!tjob->running) {
771 aio_poll(qemu_get_aio_context(), false);
775 WITH_JOB_LOCK_GUARD() {
776 g_assert_cmpint(job->job.pause_count, ==, 0);
777 g_assert_false(job->job.paused);
778 g_assert_true(tjob->running);
779 g_assert_true(job->job.busy); /* We're in qemu_co_sleep_ns() */
782 do_drain_begin_unlocked(drain_type, drain_bs);
784 WITH_JOB_LOCK_GUARD() {
785 if (drain_type == BDRV_DRAIN_ALL) {
786 /* bdrv_drain_all() drains both src and target */
787 g_assert_cmpint(job->job.pause_count, ==, 2);
788 } else {
789 g_assert_cmpint(job->job.pause_count, ==, 1);
791 g_assert_true(job->job.paused);
792 g_assert_false(job->job.busy); /* The job is paused */
795 do_drain_end_unlocked(drain_type, drain_bs);
797 if (use_iothread) {
799 * Here we are waiting for the paused status to change,
800 * so don't bother protecting the read every time.
802 * paused is reset in the I/O thread, wait for it
804 while (job->job.paused) {
805 aio_poll(qemu_get_aio_context(), false);
809 WITH_JOB_LOCK_GUARD() {
810 g_assert_cmpint(job->job.pause_count, ==, 0);
811 g_assert_false(job->job.paused);
812 g_assert_true(job->job.busy); /* We're in qemu_co_sleep_ns() */
815 do_drain_begin_unlocked(drain_type, target);
817 WITH_JOB_LOCK_GUARD() {
818 if (drain_type == BDRV_DRAIN_ALL) {
819 /* bdrv_drain_all() drains both src and target */
820 g_assert_cmpint(job->job.pause_count, ==, 2);
821 } else {
822 g_assert_cmpint(job->job.pause_count, ==, 1);
824 g_assert_true(job->job.paused);
825 g_assert_false(job->job.busy); /* The job is paused */
828 do_drain_end_unlocked(drain_type, target);
830 if (use_iothread) {
832 * Here we are waiting for the paused status to change,
833 * so don't bother protecting the read every time.
835 * paused is reset in the I/O thread, wait for it
837 while (job->job.paused) {
838 aio_poll(qemu_get_aio_context(), false);
842 WITH_JOB_LOCK_GUARD() {
843 g_assert_cmpint(job->job.pause_count, ==, 0);
844 g_assert_false(job->job.paused);
845 g_assert_true(job->job.busy); /* We're in qemu_co_sleep_ns() */
848 WITH_JOB_LOCK_GUARD() {
849 ret = job_complete_sync_locked(&job->job, &error_abort);
851 g_assert_cmpint(ret, ==, (result == TEST_JOB_SUCCESS ? 0 : -EIO));
853 aio_context_acquire(ctx);
854 if (use_iothread) {
855 blk_set_aio_context(blk_src, qemu_get_aio_context(), &error_abort);
856 assert(blk_get_aio_context(blk_target) == qemu_get_aio_context());
858 aio_context_release(ctx);
860 blk_unref(blk_src);
861 blk_unref(blk_target);
862 bdrv_unref(src_overlay);
863 bdrv_unref(target);
865 if (iothread) {
866 iothread_join(iothread);
870 static void test_blockjob_common(enum drain_type drain_type, bool use_iothread,
871 enum test_job_result result)
873 test_blockjob_common_drain_node(drain_type, use_iothread, result,
874 TEST_JOB_DRAIN_SRC);
875 test_blockjob_common_drain_node(drain_type, use_iothread, result,
876 TEST_JOB_DRAIN_SRC_CHILD);
879 static void test_blockjob_drain_all(void)
881 test_blockjob_common(BDRV_DRAIN_ALL, false, TEST_JOB_SUCCESS);
884 static void test_blockjob_drain(void)
886 test_blockjob_common(BDRV_DRAIN, false, TEST_JOB_SUCCESS);
889 static void test_blockjob_error_drain_all(void)
891 test_blockjob_common(BDRV_DRAIN_ALL, false, TEST_JOB_FAIL_RUN);
892 test_blockjob_common(BDRV_DRAIN_ALL, false, TEST_JOB_FAIL_PREPARE);
895 static void test_blockjob_error_drain(void)
897 test_blockjob_common(BDRV_DRAIN, false, TEST_JOB_FAIL_RUN);
898 test_blockjob_common(BDRV_DRAIN, false, TEST_JOB_FAIL_PREPARE);
901 static void test_blockjob_iothread_drain_all(void)
903 test_blockjob_common(BDRV_DRAIN_ALL, true, TEST_JOB_SUCCESS);
906 static void test_blockjob_iothread_drain(void)
908 test_blockjob_common(BDRV_DRAIN, true, TEST_JOB_SUCCESS);
911 static void test_blockjob_iothread_error_drain_all(void)
913 test_blockjob_common(BDRV_DRAIN_ALL, true, TEST_JOB_FAIL_RUN);
914 test_blockjob_common(BDRV_DRAIN_ALL, true, TEST_JOB_FAIL_PREPARE);
917 static void test_blockjob_iothread_error_drain(void)
919 test_blockjob_common(BDRV_DRAIN, true, TEST_JOB_FAIL_RUN);
920 test_blockjob_common(BDRV_DRAIN, true, TEST_JOB_FAIL_PREPARE);
924 typedef struct BDRVTestTopState {
925 BdrvChild *wait_child;
926 } BDRVTestTopState;
928 static void bdrv_test_top_close(BlockDriverState *bs)
930 BdrvChild *c, *next_c;
931 QLIST_FOREACH_SAFE(c, &bs->children, next, next_c) {
932 bdrv_unref_child(bs, c);
936 static int coroutine_fn GRAPH_RDLOCK
937 bdrv_test_top_co_preadv(BlockDriverState *bs, int64_t offset, int64_t bytes,
938 QEMUIOVector *qiov, BdrvRequestFlags flags)
940 BDRVTestTopState *tts = bs->opaque;
941 return bdrv_co_preadv(tts->wait_child, offset, bytes, qiov, flags);
944 static BlockDriver bdrv_test_top_driver = {
945 .format_name = "test_top_driver",
946 .instance_size = sizeof(BDRVTestTopState),
948 .bdrv_close = bdrv_test_top_close,
949 .bdrv_co_preadv = bdrv_test_top_co_preadv,
951 .bdrv_child_perm = bdrv_default_perms,
954 typedef struct TestCoDeleteByDrainData {
955 BlockBackend *blk;
956 bool detach_instead_of_delete;
957 bool done;
958 } TestCoDeleteByDrainData;
960 static void coroutine_fn test_co_delete_by_drain(void *opaque)
962 TestCoDeleteByDrainData *dbdd = opaque;
963 BlockBackend *blk = dbdd->blk;
964 BlockDriverState *bs = blk_bs(blk);
965 BDRVTestTopState *tts = bs->opaque;
966 void *buffer = g_malloc(65536);
967 QEMUIOVector qiov = QEMU_IOVEC_INIT_BUF(qiov, buffer, 65536);
969 GRAPH_RDLOCK_GUARD();
971 /* Pretend some internal write operation from parent to child.
972 * Important: We have to read from the child, not from the parent!
973 * Draining works by first propagating it all up the tree to the
974 * root and then waiting for drainage from root to the leaves
975 * (protocol nodes). If we have a request waiting on the root,
976 * everything will be drained before we go back down the tree, but
977 * we do not want that. We want to be in the middle of draining
978 * when this following requests returns. */
979 bdrv_co_preadv(tts->wait_child, 0, 65536, &qiov, 0);
981 g_assert_cmpint(bs->refcnt, ==, 1);
983 if (!dbdd->detach_instead_of_delete) {
984 blk_unref(blk);
985 } else {
986 BdrvChild *c, *next_c;
987 QLIST_FOREACH_SAFE(c, &bs->children, next, next_c) {
988 bdrv_unref_child(bs, c);
992 dbdd->done = true;
993 g_free(buffer);
997 * Test what happens when some BDS has some children, you drain one of
998 * them and this results in the BDS being deleted.
1000 * If @detach_instead_of_delete is set, the BDS is not going to be
1001 * deleted but will only detach all of its children.
1003 static void do_test_delete_by_drain(bool detach_instead_of_delete,
1004 enum drain_type drain_type)
1006 BlockBackend *blk;
1007 BlockDriverState *bs, *child_bs, *null_bs;
1008 BDRVTestTopState *tts;
1009 TestCoDeleteByDrainData dbdd;
1010 Coroutine *co;
1012 bs = bdrv_new_open_driver(&bdrv_test_top_driver, "top", BDRV_O_RDWR,
1013 &error_abort);
1014 bs->total_sectors = 65536 >> BDRV_SECTOR_BITS;
1015 tts = bs->opaque;
1017 null_bs = bdrv_open("null-co://", NULL, NULL, BDRV_O_RDWR | BDRV_O_PROTOCOL,
1018 &error_abort);
1019 bdrv_attach_child(bs, null_bs, "null-child", &child_of_bds,
1020 BDRV_CHILD_DATA, &error_abort);
1022 /* This child will be the one to pass to requests through to, and
1023 * it will stall until a drain occurs */
1024 child_bs = bdrv_new_open_driver(&bdrv_test, "child", BDRV_O_RDWR,
1025 &error_abort);
1026 child_bs->total_sectors = 65536 >> BDRV_SECTOR_BITS;
1027 /* Takes our reference to child_bs */
1028 tts->wait_child = bdrv_attach_child(bs, child_bs, "wait-child",
1029 &child_of_bds,
1030 BDRV_CHILD_DATA | BDRV_CHILD_PRIMARY,
1031 &error_abort);
1033 /* This child is just there to be deleted
1034 * (for detach_instead_of_delete == true) */
1035 null_bs = bdrv_open("null-co://", NULL, NULL, BDRV_O_RDWR | BDRV_O_PROTOCOL,
1036 &error_abort);
1037 bdrv_attach_child(bs, null_bs, "null-child", &child_of_bds, BDRV_CHILD_DATA,
1038 &error_abort);
1040 blk = blk_new(qemu_get_aio_context(), BLK_PERM_ALL, BLK_PERM_ALL);
1041 blk_insert_bs(blk, bs, &error_abort);
1043 /* Referenced by blk now */
1044 bdrv_unref(bs);
1046 g_assert_cmpint(bs->refcnt, ==, 1);
1047 g_assert_cmpint(child_bs->refcnt, ==, 1);
1048 g_assert_cmpint(null_bs->refcnt, ==, 1);
1051 dbdd = (TestCoDeleteByDrainData){
1052 .blk = blk,
1053 .detach_instead_of_delete = detach_instead_of_delete,
1054 .done = false,
1056 co = qemu_coroutine_create(test_co_delete_by_drain, &dbdd);
1057 qemu_coroutine_enter(co);
1059 /* Drain the child while the read operation is still pending.
1060 * This should result in the operation finishing and
1061 * test_co_delete_by_drain() resuming. Thus, @bs will be deleted
1062 * and the coroutine will exit while this drain operation is still
1063 * in progress. */
1064 switch (drain_type) {
1065 case BDRV_DRAIN:
1066 bdrv_ref(child_bs);
1067 bdrv_drain(child_bs);
1068 bdrv_unref(child_bs);
1069 break;
1070 case BDRV_DRAIN_ALL:
1071 bdrv_drain_all_begin();
1072 bdrv_drain_all_end();
1073 break;
1074 default:
1075 g_assert_not_reached();
1078 while (!dbdd.done) {
1079 aio_poll(qemu_get_aio_context(), true);
1082 if (detach_instead_of_delete) {
1083 /* Here, the reference has not passed over to the coroutine,
1084 * so we have to delete the BB ourselves */
1085 blk_unref(blk);
1089 static void test_delete_by_drain(void)
1091 do_test_delete_by_drain(false, BDRV_DRAIN);
1094 static void test_detach_by_drain_all(void)
1096 do_test_delete_by_drain(true, BDRV_DRAIN_ALL);
1099 static void test_detach_by_drain(void)
1101 do_test_delete_by_drain(true, BDRV_DRAIN);
1105 struct detach_by_parent_data {
1106 BlockDriverState *parent_b;
1107 BdrvChild *child_b;
1108 BlockDriverState *c;
1109 BdrvChild *child_c;
1110 bool by_parent_cb;
1111 bool detach_on_drain;
1113 static struct detach_by_parent_data detach_by_parent_data;
1115 static void detach_indirect_bh(void *opaque)
1117 struct detach_by_parent_data *data = opaque;
1119 bdrv_dec_in_flight(data->child_b->bs);
1120 bdrv_unref_child(data->parent_b, data->child_b);
1122 bdrv_ref(data->c);
1123 data->child_c = bdrv_attach_child(data->parent_b, data->c, "PB-C",
1124 &child_of_bds, BDRV_CHILD_DATA,
1125 &error_abort);
1128 static void detach_by_parent_aio_cb(void *opaque, int ret)
1130 struct detach_by_parent_data *data = &detach_by_parent_data;
1132 g_assert_cmpint(ret, ==, 0);
1133 if (data->by_parent_cb) {
1134 bdrv_inc_in_flight(data->child_b->bs);
1135 detach_indirect_bh(data);
1139 static void detach_by_driver_cb_drained_begin(BdrvChild *child)
1141 struct detach_by_parent_data *data = &detach_by_parent_data;
1143 if (!data->detach_on_drain) {
1144 return;
1146 data->detach_on_drain = false;
1148 bdrv_inc_in_flight(data->child_b->bs);
1149 aio_bh_schedule_oneshot(qemu_get_current_aio_context(),
1150 detach_indirect_bh, &detach_by_parent_data);
1151 child_of_bds.drained_begin(child);
1154 static BdrvChildClass detach_by_driver_cb_class;
1157 * Initial graph:
1159 * PA PB
1160 * \ / \
1161 * A B C
1163 * by_parent_cb == true: Test that parent callbacks don't poll
1165 * PA has a pending write request whose callback changes the child nodes of
1166 * PB: It removes B and adds C instead. The subtree of PB is drained, which
1167 * will indirectly drain the write request, too.
1169 * by_parent_cb == false: Test that bdrv_drain_invoke() doesn't poll
1171 * PA's BdrvChildClass has a .drained_begin callback that schedules a BH
1172 * that does the same graph change. If bdrv_drain_invoke() calls it, the
1173 * state is messed up, but if it is only polled in the single
1174 * BDRV_POLL_WHILE() at the end of the drain, this should work fine.
1176 static void test_detach_indirect(bool by_parent_cb)
1178 BlockBackend *blk;
1179 BlockDriverState *parent_a, *parent_b, *a, *b, *c;
1180 BdrvChild *child_a, *child_b;
1181 BlockAIOCB *acb;
1183 QEMUIOVector qiov = QEMU_IOVEC_INIT_BUF(qiov, NULL, 0);
1185 if (!by_parent_cb) {
1186 detach_by_driver_cb_class = child_of_bds;
1187 detach_by_driver_cb_class.drained_begin =
1188 detach_by_driver_cb_drained_begin;
1189 detach_by_driver_cb_class.drained_end = NULL;
1190 detach_by_driver_cb_class.drained_poll = NULL;
1193 detach_by_parent_data = (struct detach_by_parent_data) {
1194 .detach_on_drain = false,
1197 /* Create all involved nodes */
1198 parent_a = bdrv_new_open_driver(&bdrv_test, "parent-a", BDRV_O_RDWR,
1199 &error_abort);
1200 parent_b = bdrv_new_open_driver(&bdrv_test, "parent-b", 0,
1201 &error_abort);
1203 a = bdrv_new_open_driver(&bdrv_test, "a", BDRV_O_RDWR, &error_abort);
1204 b = bdrv_new_open_driver(&bdrv_test, "b", BDRV_O_RDWR, &error_abort);
1205 c = bdrv_new_open_driver(&bdrv_test, "c", BDRV_O_RDWR, &error_abort);
1207 /* blk is a BB for parent-a */
1208 blk = blk_new(qemu_get_aio_context(), BLK_PERM_ALL, BLK_PERM_ALL);
1209 blk_insert_bs(blk, parent_a, &error_abort);
1210 bdrv_unref(parent_a);
1212 /* If we want to get bdrv_drain_invoke() to call aio_poll(), the driver
1213 * callback must not return immediately. */
1214 if (!by_parent_cb) {
1215 BDRVTestState *s = parent_a->opaque;
1216 s->sleep_in_drain_begin = true;
1219 /* Set child relationships */
1220 bdrv_ref(b);
1221 bdrv_ref(a);
1222 child_b = bdrv_attach_child(parent_b, b, "PB-B", &child_of_bds,
1223 BDRV_CHILD_DATA, &error_abort);
1224 child_a = bdrv_attach_child(parent_b, a, "PB-A", &child_of_bds,
1225 BDRV_CHILD_COW, &error_abort);
1227 bdrv_ref(a);
1228 bdrv_attach_child(parent_a, a, "PA-A",
1229 by_parent_cb ? &child_of_bds : &detach_by_driver_cb_class,
1230 BDRV_CHILD_DATA, &error_abort);
1232 g_assert_cmpint(parent_a->refcnt, ==, 1);
1233 g_assert_cmpint(parent_b->refcnt, ==, 1);
1234 g_assert_cmpint(a->refcnt, ==, 3);
1235 g_assert_cmpint(b->refcnt, ==, 2);
1236 g_assert_cmpint(c->refcnt, ==, 1);
1238 g_assert(QLIST_FIRST(&parent_b->children) == child_a);
1239 g_assert(QLIST_NEXT(child_a, next) == child_b);
1240 g_assert(QLIST_NEXT(child_b, next) == NULL);
1242 /* Start the evil write request */
1243 detach_by_parent_data = (struct detach_by_parent_data) {
1244 .parent_b = parent_b,
1245 .child_b = child_b,
1246 .c = c,
1247 .by_parent_cb = by_parent_cb,
1248 .detach_on_drain = true,
1250 acb = blk_aio_preadv(blk, 0, &qiov, 0, detach_by_parent_aio_cb, NULL);
1251 g_assert(acb != NULL);
1253 /* Drain and check the expected result */
1254 bdrv_drained_begin(parent_b);
1255 bdrv_drained_begin(a);
1256 bdrv_drained_begin(b);
1257 bdrv_drained_begin(c);
1259 g_assert(detach_by_parent_data.child_c != NULL);
1261 g_assert_cmpint(parent_a->refcnt, ==, 1);
1262 g_assert_cmpint(parent_b->refcnt, ==, 1);
1263 g_assert_cmpint(a->refcnt, ==, 3);
1264 g_assert_cmpint(b->refcnt, ==, 1);
1265 g_assert_cmpint(c->refcnt, ==, 2);
1267 g_assert(QLIST_FIRST(&parent_b->children) == detach_by_parent_data.child_c);
1268 g_assert(QLIST_NEXT(detach_by_parent_data.child_c, next) == child_a);
1269 g_assert(QLIST_NEXT(child_a, next) == NULL);
1271 g_assert_cmpint(parent_a->quiesce_counter, ==, 1);
1272 g_assert_cmpint(parent_b->quiesce_counter, ==, 3);
1273 g_assert_cmpint(a->quiesce_counter, ==, 1);
1274 g_assert_cmpint(b->quiesce_counter, ==, 1);
1275 g_assert_cmpint(c->quiesce_counter, ==, 1);
1277 bdrv_drained_end(parent_b);
1278 bdrv_drained_end(a);
1279 bdrv_drained_end(b);
1280 bdrv_drained_end(c);
1282 bdrv_unref(parent_b);
1283 blk_unref(blk);
1285 g_assert_cmpint(a->refcnt, ==, 1);
1286 g_assert_cmpint(b->refcnt, ==, 1);
1287 g_assert_cmpint(c->refcnt, ==, 1);
1288 bdrv_unref(a);
1289 bdrv_unref(b);
1290 bdrv_unref(c);
1293 static void test_detach_by_parent_cb(void)
1295 test_detach_indirect(true);
1298 static void test_detach_by_driver_cb(void)
1300 test_detach_indirect(false);
1303 static void test_append_to_drained(void)
1305 BlockBackend *blk;
1306 BlockDriverState *base, *overlay;
1307 BDRVTestState *base_s, *overlay_s;
1309 blk = blk_new(qemu_get_aio_context(), BLK_PERM_ALL, BLK_PERM_ALL);
1310 base = bdrv_new_open_driver(&bdrv_test, "base", BDRV_O_RDWR, &error_abort);
1311 base_s = base->opaque;
1312 blk_insert_bs(blk, base, &error_abort);
1314 overlay = bdrv_new_open_driver(&bdrv_test, "overlay", BDRV_O_RDWR,
1315 &error_abort);
1316 overlay_s = overlay->opaque;
1318 do_drain_begin(BDRV_DRAIN, base);
1319 g_assert_cmpint(base->quiesce_counter, ==, 1);
1320 g_assert_cmpint(base_s->drain_count, ==, 1);
1321 g_assert_cmpint(base->in_flight, ==, 0);
1323 bdrv_append(overlay, base, &error_abort);
1324 g_assert_cmpint(base->in_flight, ==, 0);
1325 g_assert_cmpint(overlay->in_flight, ==, 0);
1327 g_assert_cmpint(base->quiesce_counter, ==, 1);
1328 g_assert_cmpint(base_s->drain_count, ==, 1);
1329 g_assert_cmpint(overlay->quiesce_counter, ==, 1);
1330 g_assert_cmpint(overlay_s->drain_count, ==, 1);
1332 do_drain_end(BDRV_DRAIN, base);
1334 g_assert_cmpint(base->quiesce_counter, ==, 0);
1335 g_assert_cmpint(base_s->drain_count, ==, 0);
1336 g_assert_cmpint(overlay->quiesce_counter, ==, 0);
1337 g_assert_cmpint(overlay_s->drain_count, ==, 0);
1339 bdrv_unref(overlay);
1340 bdrv_unref(base);
1341 blk_unref(blk);
1344 static void test_set_aio_context(void)
1346 BlockDriverState *bs;
1347 IOThread *a = iothread_new();
1348 IOThread *b = iothread_new();
1349 AioContext *ctx_a = iothread_get_aio_context(a);
1350 AioContext *ctx_b = iothread_get_aio_context(b);
1352 bs = bdrv_new_open_driver(&bdrv_test, "test-node", BDRV_O_RDWR,
1353 &error_abort);
1355 bdrv_drained_begin(bs);
1356 bdrv_try_change_aio_context(bs, ctx_a, NULL, &error_abort);
1358 aio_context_acquire(ctx_a);
1359 bdrv_drained_end(bs);
1361 bdrv_drained_begin(bs);
1362 bdrv_try_change_aio_context(bs, ctx_b, NULL, &error_abort);
1363 aio_context_release(ctx_a);
1364 aio_context_acquire(ctx_b);
1365 bdrv_try_change_aio_context(bs, qemu_get_aio_context(), NULL, &error_abort);
1366 aio_context_release(ctx_b);
1367 bdrv_drained_end(bs);
1369 bdrv_unref(bs);
1370 iothread_join(a);
1371 iothread_join(b);
1375 typedef struct TestDropBackingBlockJob {
1376 BlockJob common;
1377 bool should_complete;
1378 bool *did_complete;
1379 BlockDriverState *detach_also;
1380 BlockDriverState *bs;
1381 } TestDropBackingBlockJob;
1383 static int coroutine_fn test_drop_backing_job_run(Job *job, Error **errp)
1385 TestDropBackingBlockJob *s =
1386 container_of(job, TestDropBackingBlockJob, common.job);
1388 while (!s->should_complete) {
1389 job_sleep_ns(job, 0);
1392 return 0;
1395 static void test_drop_backing_job_commit(Job *job)
1397 TestDropBackingBlockJob *s =
1398 container_of(job, TestDropBackingBlockJob, common.job);
1400 bdrv_set_backing_hd(s->bs, NULL, &error_abort);
1401 bdrv_set_backing_hd(s->detach_also, NULL, &error_abort);
1403 *s->did_complete = true;
1406 static const BlockJobDriver test_drop_backing_job_driver = {
1407 .job_driver = {
1408 .instance_size = sizeof(TestDropBackingBlockJob),
1409 .free = block_job_free,
1410 .user_resume = block_job_user_resume,
1411 .run = test_drop_backing_job_run,
1412 .commit = test_drop_backing_job_commit,
1417 * Creates a child node with three parent nodes on it, and then runs a
1418 * block job on the final one, parent-node-2.
1420 * The job is then asked to complete before a section where the child
1421 * is drained.
1423 * Ending this section will undrain the child's parents, first
1424 * parent-node-2, then parent-node-1, then parent-node-0 -- the parent
1425 * list is in reverse order of how they were added. Ending the drain
1426 * on parent-node-2 will resume the job, thus completing it and
1427 * scheduling job_exit().
1429 * Ending the drain on parent-node-1 will poll the AioContext, which
1430 * lets job_exit() and thus test_drop_backing_job_commit() run. That
1431 * function first removes the child as parent-node-2's backing file.
1433 * In old (and buggy) implementations, there are two problems with
1434 * that:
1435 * (A) bdrv_drain_invoke() polls for every node that leaves the
1436 * drained section. This means that job_exit() is scheduled
1437 * before the child has left the drained section. Its
1438 * quiesce_counter is therefore still 1 when it is removed from
1439 * parent-node-2.
1441 * (B) bdrv_replace_child_noperm() calls drained_end() on the old
1442 * child's parents as many times as the child is quiesced. This
1443 * means it will call drained_end() on parent-node-2 once.
1444 * Because parent-node-2 is no longer quiesced at this point, this
1445 * will fail.
1447 * bdrv_replace_child_noperm() therefore must call drained_end() on
1448 * the parent only if it really is still drained because the child is
1449 * drained.
1451 * If removing child from parent-node-2 was successful (as it should
1452 * be), test_drop_backing_job_commit() will then also remove the child
1453 * from parent-node-0.
1455 * With an old version of our drain infrastructure ((A) above), that
1456 * resulted in the following flow:
1458 * 1. child attempts to leave its drained section. The call recurses
1459 * to its parents.
1461 * 2. parent-node-2 leaves the drained section. Polling in
1462 * bdrv_drain_invoke() will schedule job_exit().
1464 * 3. parent-node-1 leaves the drained section. Polling in
1465 * bdrv_drain_invoke() will run job_exit(), thus disconnecting
1466 * parent-node-0 from the child node.
1468 * 4. bdrv_parent_drained_end() uses a QLIST_FOREACH_SAFE() loop to
1469 * iterate over the parents. Thus, it now accesses the BdrvChild
1470 * object that used to connect parent-node-0 and the child node.
1471 * However, that object no longer exists, so it accesses a dangling
1472 * pointer.
1474 * The solution is to only poll once when running a bdrv_drained_end()
1475 * operation, specifically at the end when all drained_end()
1476 * operations for all involved nodes have been scheduled.
1477 * Note that this also solves (A) above, thus hiding (B).
1479 static void test_blockjob_commit_by_drained_end(void)
1481 BlockDriverState *bs_child, *bs_parents[3];
1482 TestDropBackingBlockJob *job;
1483 bool job_has_completed = false;
1484 int i;
1486 bs_child = bdrv_new_open_driver(&bdrv_test, "child-node", BDRV_O_RDWR,
1487 &error_abort);
1489 for (i = 0; i < 3; i++) {
1490 char name[32];
1491 snprintf(name, sizeof(name), "parent-node-%i", i);
1492 bs_parents[i] = bdrv_new_open_driver(&bdrv_test, name, BDRV_O_RDWR,
1493 &error_abort);
1494 bdrv_set_backing_hd(bs_parents[i], bs_child, &error_abort);
1497 job = block_job_create("job", &test_drop_backing_job_driver, NULL,
1498 bs_parents[2], 0, BLK_PERM_ALL, 0, 0, NULL, NULL,
1499 &error_abort);
1500 job->bs = bs_parents[2];
1502 job->detach_also = bs_parents[0];
1503 job->did_complete = &job_has_completed;
1505 job_start(&job->common.job);
1507 job->should_complete = true;
1508 bdrv_drained_begin(bs_child);
1509 g_assert(!job_has_completed);
1510 bdrv_drained_end(bs_child);
1511 aio_poll(qemu_get_aio_context(), false);
1512 g_assert(job_has_completed);
1514 bdrv_unref(bs_parents[0]);
1515 bdrv_unref(bs_parents[1]);
1516 bdrv_unref(bs_parents[2]);
1517 bdrv_unref(bs_child);
1521 typedef struct TestSimpleBlockJob {
1522 BlockJob common;
1523 bool should_complete;
1524 bool *did_complete;
1525 } TestSimpleBlockJob;
1527 static int coroutine_fn test_simple_job_run(Job *job, Error **errp)
1529 TestSimpleBlockJob *s = container_of(job, TestSimpleBlockJob, common.job);
1531 while (!s->should_complete) {
1532 job_sleep_ns(job, 0);
1535 return 0;
1538 static void test_simple_job_clean(Job *job)
1540 TestSimpleBlockJob *s = container_of(job, TestSimpleBlockJob, common.job);
1541 *s->did_complete = true;
1544 static const BlockJobDriver test_simple_job_driver = {
1545 .job_driver = {
1546 .instance_size = sizeof(TestSimpleBlockJob),
1547 .free = block_job_free,
1548 .user_resume = block_job_user_resume,
1549 .run = test_simple_job_run,
1550 .clean = test_simple_job_clean,
1554 static int drop_intermediate_poll_update_filename(BdrvChild *child,
1555 BlockDriverState *new_base,
1556 const char *filename,
1557 Error **errp)
1560 * We are free to poll here, which may change the block graph, if
1561 * it is not drained.
1564 /* If the job is not drained: Complete it, schedule job_exit() */
1565 aio_poll(qemu_get_current_aio_context(), false);
1566 /* If the job is not drained: Run job_exit(), finish the job */
1567 aio_poll(qemu_get_current_aio_context(), false);
1569 return 0;
1573 * Test a poll in the midst of bdrv_drop_intermediate().
1575 * bdrv_drop_intermediate() calls BdrvChildClass.update_filename(),
1576 * which can yield or poll. This may lead to graph changes, unless
1577 * the whole subtree in question is drained.
1579 * We test this on the following graph:
1581 * Job
1584 * job-node
1588 * job-node
1591 * backing
1595 * node-2 --chain--> node-1 --chain--> node-0
1597 * We drop node-1 with bdrv_drop_intermediate(top=node-1, base=node-0).
1599 * This first updates node-2's backing filename by invoking
1600 * drop_intermediate_poll_update_filename(), which polls twice. This
1601 * causes the job to finish, which in turns causes the job-node to be
1602 * deleted.
1604 * bdrv_drop_intermediate() uses a QLIST_FOREACH_SAFE() loop, so it
1605 * already has a pointer to the BdrvChild edge between job-node and
1606 * node-1. When it tries to handle that edge, we probably get a
1607 * segmentation fault because the object no longer exists.
1610 * The solution is for bdrv_drop_intermediate() to drain top's
1611 * subtree. This prevents graph changes from happening just because
1612 * BdrvChildClass.update_filename() yields or polls. Thus, the block
1613 * job is paused during that drained section and must finish before or
1614 * after.
1616 * (In addition, bdrv_replace_child() must keep the job paused.)
1618 static void test_drop_intermediate_poll(void)
1620 static BdrvChildClass chain_child_class;
1621 BlockDriverState *chain[3];
1622 TestSimpleBlockJob *job;
1623 BlockDriverState *job_node;
1624 bool job_has_completed = false;
1625 int i;
1626 int ret;
1628 chain_child_class = child_of_bds;
1629 chain_child_class.update_filename = drop_intermediate_poll_update_filename;
1631 for (i = 0; i < 3; i++) {
1632 char name[32];
1633 snprintf(name, 32, "node-%i", i);
1635 chain[i] = bdrv_new_open_driver(&bdrv_test, name, 0, &error_abort);
1638 job_node = bdrv_new_open_driver(&bdrv_test, "job-node", BDRV_O_RDWR,
1639 &error_abort);
1640 bdrv_set_backing_hd(job_node, chain[1], &error_abort);
1643 * Establish the chain last, so the chain links are the first
1644 * elements in the BDS.parents lists
1646 for (i = 0; i < 3; i++) {
1647 if (i) {
1648 /* Takes the reference to chain[i - 1] */
1649 bdrv_attach_child(chain[i], chain[i - 1], "chain",
1650 &chain_child_class, BDRV_CHILD_COW, &error_abort);
1654 job = block_job_create("job", &test_simple_job_driver, NULL, job_node,
1655 0, BLK_PERM_ALL, 0, 0, NULL, NULL, &error_abort);
1657 /* The job has a reference now */
1658 bdrv_unref(job_node);
1660 job->did_complete = &job_has_completed;
1662 job_start(&job->common.job);
1663 job->should_complete = true;
1665 g_assert(!job_has_completed);
1666 ret = bdrv_drop_intermediate(chain[1], chain[0], NULL);
1667 aio_poll(qemu_get_aio_context(), false);
1668 g_assert(ret == 0);
1669 g_assert(job_has_completed);
1671 bdrv_unref(chain[2]);
1675 typedef struct BDRVReplaceTestState {
1676 bool setup_completed;
1677 bool was_drained;
1678 bool was_undrained;
1679 bool has_read;
1681 int drain_count;
1683 bool yield_before_read;
1684 Coroutine *io_co;
1685 Coroutine *drain_co;
1686 } BDRVReplaceTestState;
1688 static void bdrv_replace_test_close(BlockDriverState *bs)
1693 * If @bs has a backing file:
1694 * Yield if .yield_before_read is true (and wait for drain_begin to
1695 * wake us up).
1696 * Forward the read to bs->backing. Set .has_read to true.
1697 * If drain_begin has woken us, wake it in turn.
1699 * Otherwise:
1700 * Set .has_read to true and return success.
1702 static int coroutine_fn GRAPH_RDLOCK
1703 bdrv_replace_test_co_preadv(BlockDriverState *bs, int64_t offset, int64_t bytes,
1704 QEMUIOVector *qiov, BdrvRequestFlags flags)
1706 BDRVReplaceTestState *s = bs->opaque;
1708 if (bs->backing) {
1709 int ret;
1711 g_assert(!s->drain_count);
1713 s->io_co = qemu_coroutine_self();
1714 if (s->yield_before_read) {
1715 s->yield_before_read = false;
1716 qemu_coroutine_yield();
1718 s->io_co = NULL;
1720 ret = bdrv_co_preadv(bs->backing, offset, bytes, qiov, 0);
1721 s->has_read = true;
1723 /* Wake up drain_co if it runs */
1724 if (s->drain_co) {
1725 aio_co_wake(s->drain_co);
1728 return ret;
1731 s->has_read = true;
1732 return 0;
1735 static void coroutine_fn bdrv_replace_test_drain_co(void *opaque)
1737 BlockDriverState *bs = opaque;
1738 BDRVReplaceTestState *s = bs->opaque;
1740 /* Keep waking io_co up until it is done */
1741 while (s->io_co) {
1742 aio_co_wake(s->io_co);
1743 s->io_co = NULL;
1744 qemu_coroutine_yield();
1746 s->drain_co = NULL;
1747 bdrv_dec_in_flight(bs);
1751 * If .drain_count is 0, wake up .io_co if there is one; and set
1752 * .was_drained.
1753 * Increment .drain_count.
1755 static void bdrv_replace_test_drain_begin(BlockDriverState *bs)
1757 BDRVReplaceTestState *s = bs->opaque;
1759 if (!s->setup_completed) {
1760 return;
1763 if (!s->drain_count) {
1764 s->drain_co = qemu_coroutine_create(bdrv_replace_test_drain_co, bs);
1765 bdrv_inc_in_flight(bs);
1766 aio_co_enter(bdrv_get_aio_context(bs), s->drain_co);
1767 s->was_drained = true;
1769 s->drain_count++;
1772 static void coroutine_fn bdrv_replace_test_read_entry(void *opaque)
1774 BlockDriverState *bs = opaque;
1775 char data;
1776 QEMUIOVector qiov = QEMU_IOVEC_INIT_BUF(qiov, &data, 1);
1777 int ret;
1779 /* Queue a read request post-drain */
1780 bdrv_graph_co_rdlock();
1781 ret = bdrv_replace_test_co_preadv(bs, 0, 1, &qiov, 0);
1782 bdrv_graph_co_rdunlock();
1784 g_assert(ret >= 0);
1785 bdrv_dec_in_flight(bs);
1789 * Reduce .drain_count, set .was_undrained once it reaches 0.
1790 * If .drain_count reaches 0 and the node has a backing file, issue a
1791 * read request.
1793 static void bdrv_replace_test_drain_end(BlockDriverState *bs)
1795 BDRVReplaceTestState *s = bs->opaque;
1797 if (!s->setup_completed) {
1798 return;
1801 g_assert(s->drain_count > 0);
1802 if (!--s->drain_count) {
1803 s->was_undrained = true;
1805 if (bs->backing) {
1806 Coroutine *co = qemu_coroutine_create(bdrv_replace_test_read_entry,
1807 bs);
1808 bdrv_inc_in_flight(bs);
1809 aio_co_enter(bdrv_get_aio_context(bs), co);
1814 static BlockDriver bdrv_replace_test = {
1815 .format_name = "replace_test",
1816 .instance_size = sizeof(BDRVReplaceTestState),
1817 .supports_backing = true,
1819 .bdrv_close = bdrv_replace_test_close,
1820 .bdrv_co_preadv = bdrv_replace_test_co_preadv,
1822 .bdrv_drain_begin = bdrv_replace_test_drain_begin,
1823 .bdrv_drain_end = bdrv_replace_test_drain_end,
1825 .bdrv_child_perm = bdrv_default_perms,
1828 static void coroutine_fn test_replace_child_mid_drain_read_co(void *opaque)
1830 int ret;
1831 char data;
1833 ret = blk_co_pread(opaque, 0, 1, &data, 0);
1834 g_assert(ret >= 0);
1838 * We test two things:
1839 * (1) bdrv_replace_child_noperm() must not undrain the parent if both
1840 * children are drained.
1841 * (2) bdrv_replace_child_noperm() must never flush I/O requests to a
1842 * drained child. If the old child is drained, it must flush I/O
1843 * requests after the new one has been attached. If the new child
1844 * is drained, it must flush I/O requests before the old one is
1845 * detached.
1847 * To do so, we create one parent node and two child nodes; then
1848 * attach one of the children (old_child_bs) to the parent, then
1849 * drain both old_child_bs and new_child_bs according to
1850 * old_drain_count and new_drain_count, respectively, and finally
1851 * we invoke bdrv_replace_node() to replace old_child_bs by
1852 * new_child_bs.
1854 * The test block driver we use here (bdrv_replace_test) has a read
1855 * function that:
1856 * - For the parent node, can optionally yield, and then forwards the
1857 * read to bdrv_preadv(),
1858 * - For the child node, just returns immediately.
1860 * If the read yields, the drain_begin function will wake it up.
1862 * The drain_end function issues a read on the parent once it is fully
1863 * undrained (which simulates requests starting to come in again).
1865 static void do_test_replace_child_mid_drain(int old_drain_count,
1866 int new_drain_count)
1868 BlockBackend *parent_blk;
1869 BlockDriverState *parent_bs;
1870 BlockDriverState *old_child_bs, *new_child_bs;
1871 BDRVReplaceTestState *parent_s;
1872 BDRVReplaceTestState *old_child_s, *new_child_s;
1873 Coroutine *io_co;
1874 int i;
1876 parent_bs = bdrv_new_open_driver(&bdrv_replace_test, "parent", 0,
1877 &error_abort);
1878 parent_s = parent_bs->opaque;
1880 parent_blk = blk_new(qemu_get_aio_context(),
1881 BLK_PERM_CONSISTENT_READ, BLK_PERM_ALL);
1882 blk_insert_bs(parent_blk, parent_bs, &error_abort);
1884 old_child_bs = bdrv_new_open_driver(&bdrv_replace_test, "old-child", 0,
1885 &error_abort);
1886 new_child_bs = bdrv_new_open_driver(&bdrv_replace_test, "new-child", 0,
1887 &error_abort);
1888 old_child_s = old_child_bs->opaque;
1889 new_child_s = new_child_bs->opaque;
1891 /* So that we can read something */
1892 parent_bs->total_sectors = 1;
1893 old_child_bs->total_sectors = 1;
1894 new_child_bs->total_sectors = 1;
1896 bdrv_ref(old_child_bs);
1897 bdrv_attach_child(parent_bs, old_child_bs, "child", &child_of_bds,
1898 BDRV_CHILD_COW, &error_abort);
1899 parent_s->setup_completed = true;
1901 for (i = 0; i < old_drain_count; i++) {
1902 bdrv_drained_begin(old_child_bs);
1904 for (i = 0; i < new_drain_count; i++) {
1905 bdrv_drained_begin(new_child_bs);
1908 if (!old_drain_count) {
1910 * Start a read operation that will yield, so it will not
1911 * complete before the node is drained.
1913 parent_s->yield_before_read = true;
1914 io_co = qemu_coroutine_create(test_replace_child_mid_drain_read_co,
1915 parent_blk);
1916 qemu_coroutine_enter(io_co);
1919 /* If we have started a read operation, it should have yielded */
1920 g_assert(!parent_s->has_read);
1922 /* Reset drained status so we can see what bdrv_replace_node() does */
1923 parent_s->was_drained = false;
1924 parent_s->was_undrained = false;
1926 g_assert(parent_bs->quiesce_counter == old_drain_count);
1927 bdrv_replace_node(old_child_bs, new_child_bs, &error_abort);
1928 g_assert(parent_bs->quiesce_counter == new_drain_count);
1930 if (!old_drain_count && !new_drain_count) {
1932 * From undrained to undrained drains and undrains the parent,
1933 * because bdrv_replace_node() contains a drained section for
1934 * @old_child_bs.
1936 g_assert(parent_s->was_drained && parent_s->was_undrained);
1937 } else if (!old_drain_count && new_drain_count) {
1939 * From undrained to drained should drain the parent and keep
1940 * it that way.
1942 g_assert(parent_s->was_drained && !parent_s->was_undrained);
1943 } else if (old_drain_count && !new_drain_count) {
1945 * From drained to undrained should undrain the parent and
1946 * keep it that way.
1948 g_assert(!parent_s->was_drained && parent_s->was_undrained);
1949 } else /* if (old_drain_count && new_drain_count) */ {
1951 * From drained to drained must not undrain the parent at any
1952 * point
1954 g_assert(!parent_s->was_drained && !parent_s->was_undrained);
1957 if (!old_drain_count || !new_drain_count) {
1959 * If !old_drain_count, we have started a read request before
1960 * bdrv_replace_node(). If !new_drain_count, the parent must
1961 * have been undrained at some point, and
1962 * bdrv_replace_test_co_drain_end() starts a read request
1963 * then.
1965 g_assert(parent_s->has_read);
1966 } else {
1968 * If the parent was never undrained, there is no way to start
1969 * a read request.
1971 g_assert(!parent_s->has_read);
1974 /* A drained child must have not received any request */
1975 g_assert(!(old_drain_count && old_child_s->has_read));
1976 g_assert(!(new_drain_count && new_child_s->has_read));
1978 for (i = 0; i < new_drain_count; i++) {
1979 bdrv_drained_end(new_child_bs);
1981 for (i = 0; i < old_drain_count; i++) {
1982 bdrv_drained_end(old_child_bs);
1986 * By now, bdrv_replace_test_co_drain_end() must have been called
1987 * at some point while the new child was attached to the parent.
1989 g_assert(parent_s->has_read);
1990 g_assert(new_child_s->has_read);
1992 blk_unref(parent_blk);
1993 bdrv_unref(parent_bs);
1994 bdrv_unref(old_child_bs);
1995 bdrv_unref(new_child_bs);
1998 static void test_replace_child_mid_drain(void)
2000 int old_drain_count, new_drain_count;
2002 for (old_drain_count = 0; old_drain_count < 2; old_drain_count++) {
2003 for (new_drain_count = 0; new_drain_count < 2; new_drain_count++) {
2004 do_test_replace_child_mid_drain(old_drain_count, new_drain_count);
2009 int main(int argc, char **argv)
2011 int ret;
2013 bdrv_init();
2014 qemu_init_main_loop(&error_abort);
2016 g_test_init(&argc, &argv, NULL);
2017 qemu_event_init(&done_event, false);
2019 g_test_add_func("/bdrv-drain/driver-cb/drain_all", test_drv_cb_drain_all);
2020 g_test_add_func("/bdrv-drain/driver-cb/drain", test_drv_cb_drain);
2022 g_test_add_func("/bdrv-drain/driver-cb/co/drain_all",
2023 test_drv_cb_co_drain_all);
2024 g_test_add_func("/bdrv-drain/driver-cb/co/drain", test_drv_cb_co_drain);
2026 g_test_add_func("/bdrv-drain/quiesce/drain_all", test_quiesce_drain_all);
2027 g_test_add_func("/bdrv-drain/quiesce/drain", test_quiesce_drain);
2029 g_test_add_func("/bdrv-drain/quiesce/co/drain_all",
2030 test_quiesce_co_drain_all);
2031 g_test_add_func("/bdrv-drain/quiesce/co/drain", test_quiesce_co_drain);
2033 g_test_add_func("/bdrv-drain/nested", test_nested);
2035 g_test_add_func("/bdrv-drain/graph-change/drain_all",
2036 test_graph_change_drain_all);
2038 g_test_add_func("/bdrv-drain/iothread/drain_all", test_iothread_drain_all);
2039 g_test_add_func("/bdrv-drain/iothread/drain", test_iothread_drain);
2041 g_test_add_func("/bdrv-drain/blockjob/drain_all", test_blockjob_drain_all);
2042 g_test_add_func("/bdrv-drain/blockjob/drain", test_blockjob_drain);
2044 g_test_add_func("/bdrv-drain/blockjob/error/drain_all",
2045 test_blockjob_error_drain_all);
2046 g_test_add_func("/bdrv-drain/blockjob/error/drain",
2047 test_blockjob_error_drain);
2049 g_test_add_func("/bdrv-drain/blockjob/iothread/drain_all",
2050 test_blockjob_iothread_drain_all);
2051 g_test_add_func("/bdrv-drain/blockjob/iothread/drain",
2052 test_blockjob_iothread_drain);
2054 g_test_add_func("/bdrv-drain/blockjob/iothread/error/drain_all",
2055 test_blockjob_iothread_error_drain_all);
2056 g_test_add_func("/bdrv-drain/blockjob/iothread/error/drain",
2057 test_blockjob_iothread_error_drain);
2059 g_test_add_func("/bdrv-drain/deletion/drain", test_delete_by_drain);
2060 g_test_add_func("/bdrv-drain/detach/drain_all", test_detach_by_drain_all);
2061 g_test_add_func("/bdrv-drain/detach/drain", test_detach_by_drain);
2062 g_test_add_func("/bdrv-drain/detach/parent_cb", test_detach_by_parent_cb);
2063 g_test_add_func("/bdrv-drain/detach/driver_cb", test_detach_by_driver_cb);
2065 g_test_add_func("/bdrv-drain/attach/drain", test_append_to_drained);
2067 g_test_add_func("/bdrv-drain/set_aio_context", test_set_aio_context);
2069 g_test_add_func("/bdrv-drain/blockjob/commit_by_drained_end",
2070 test_blockjob_commit_by_drained_end);
2072 g_test_add_func("/bdrv-drain/bdrv_drop_intermediate/poll",
2073 test_drop_intermediate_poll);
2075 g_test_add_func("/bdrv-drain/replace_child/mid-drain",
2076 test_replace_child_mid_drain);
2078 ret = g_test_run();
2079 qemu_event_destroy(&done_event);
2080 return ret;