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));
222 * Locking the block graph would be a bit cumbersome here because this function
223 * is called both in coroutine and non-coroutine context. We know this is a test
224 * and nothing else is running, so don't bother with TSA.
226 static void coroutine_mixed_fn TSA_NO_TSA
227 test_drv_cb_common(BlockBackend *blk, enum drain_type drain_type,
230 BlockDriverState *bs = blk_bs(blk);
231 BlockDriverState *backing = bs->backing->bs;
232 BDRVTestState *s, *backing_s;
236 QEMUIOVector qiov = QEMU_IOVEC_INIT_BUF(qiov, NULL, 0);
239 backing_s = backing->opaque;
241 /* Simple bdrv_drain_all_begin/end pair, check that CBs are called */
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(s->drain_count, ==, 1);
248 g_assert_cmpint(backing_s->drain_count, ==, !!recursive);
250 do_drain_end(drain_type, bs);
252 g_assert_cmpint(s->drain_count, ==, 0);
253 g_assert_cmpint(backing_s->drain_count, ==, 0);
255 /* Now do the same while a request is pending */
256 aio_ret = -EINPROGRESS;
257 acb = blk_aio_preadv(blk, 0, &qiov, 0, aio_ret_cb, &aio_ret);
258 g_assert(acb != NULL);
259 g_assert_cmpint(aio_ret, ==, -EINPROGRESS);
261 g_assert_cmpint(s->drain_count, ==, 0);
262 g_assert_cmpint(backing_s->drain_count, ==, 0);
264 do_drain_begin(drain_type, bs);
266 g_assert_cmpint(aio_ret, ==, 0);
267 g_assert_cmpint(s->drain_count, ==, 1);
268 g_assert_cmpint(backing_s->drain_count, ==, !!recursive);
270 do_drain_end(drain_type, bs);
272 g_assert_cmpint(s->drain_count, ==, 0);
273 g_assert_cmpint(backing_s->drain_count, ==, 0);
276 static void test_drv_cb_drain_all(void)
278 BlockBackend *blk = test_setup();
279 test_drv_cb_common(blk, BDRV_DRAIN_ALL, true);
283 static void test_drv_cb_drain(void)
285 BlockBackend *blk = test_setup();
286 test_drv_cb_common(blk, BDRV_DRAIN, false);
290 static void coroutine_fn test_drv_cb_co_drain_all_entry(void)
292 BlockBackend *blk = blk_all_next(NULL);
293 test_drv_cb_common(blk, BDRV_DRAIN_ALL, true);
296 static void test_drv_cb_co_drain_all(void)
298 BlockBackend *blk = test_setup();
299 call_in_coroutine(test_drv_cb_co_drain_all_entry);
303 static void coroutine_fn test_drv_cb_co_drain_entry(void)
305 BlockBackend *blk = blk_all_next(NULL);
306 test_drv_cb_common(blk, BDRV_DRAIN, false);
309 static void test_drv_cb_co_drain(void)
311 BlockBackend *blk = test_setup();
312 call_in_coroutine(test_drv_cb_co_drain_entry);
317 * Locking the block graph would be a bit cumbersome here because this function
318 * is called both in coroutine and non-coroutine context. We know this is a test
319 * and nothing else is running, so don't bother with TSA.
321 static void coroutine_mixed_fn TSA_NO_TSA
322 test_quiesce_common(BlockBackend *blk, enum drain_type drain_type,
325 BlockDriverState *bs = blk_bs(blk);
326 BlockDriverState *backing = bs->backing->bs;
328 g_assert_cmpint(bs->quiesce_counter, ==, 0);
329 g_assert_cmpint(backing->quiesce_counter, ==, 0);
331 do_drain_begin(drain_type, bs);
333 if (drain_type == BDRV_DRAIN_ALL) {
334 g_assert_cmpint(bs->quiesce_counter, ==, 2);
336 g_assert_cmpint(bs->quiesce_counter, ==, 1);
338 g_assert_cmpint(backing->quiesce_counter, ==, !!recursive);
340 do_drain_end(drain_type, bs);
342 g_assert_cmpint(bs->quiesce_counter, ==, 0);
343 g_assert_cmpint(backing->quiesce_counter, ==, 0);
346 static void test_quiesce_drain_all(void)
348 BlockBackend *blk = test_setup();
349 test_quiesce_common(blk, BDRV_DRAIN_ALL, true);
353 static void test_quiesce_drain(void)
355 BlockBackend *blk = test_setup();
356 test_quiesce_common(blk, BDRV_DRAIN, false);
360 static void coroutine_fn test_quiesce_co_drain_all_entry(void)
362 BlockBackend *blk = blk_all_next(NULL);
363 test_quiesce_common(blk, BDRV_DRAIN_ALL, true);
366 static void test_quiesce_co_drain_all(void)
368 BlockBackend *blk = test_setup();
369 call_in_coroutine(test_quiesce_co_drain_all_entry);
373 static void coroutine_fn test_quiesce_co_drain_entry(void)
375 BlockBackend *blk = blk_all_next(NULL);
376 test_quiesce_common(blk, BDRV_DRAIN, false);
379 static void test_quiesce_co_drain(void)
381 BlockBackend *blk = test_setup();
382 call_in_coroutine(test_quiesce_co_drain_entry);
386 static void test_nested(void)
389 BlockDriverState *bs, *backing;
390 BDRVTestState *s, *backing_s;
391 enum drain_type outer, inner;
393 blk = blk_new(qemu_get_aio_context(), BLK_PERM_ALL, BLK_PERM_ALL);
394 bs = bdrv_new_open_driver(&bdrv_test, "test-node", BDRV_O_RDWR,
397 blk_insert_bs(blk, bs, &error_abort);
399 backing = bdrv_new_open_driver(&bdrv_test, "backing", 0, &error_abort);
400 backing_s = backing->opaque;
401 bdrv_set_backing_hd(bs, backing, &error_abort);
403 for (outer = 0; outer < DRAIN_TYPE_MAX; outer++) {
404 for (inner = 0; inner < DRAIN_TYPE_MAX; inner++) {
405 int backing_quiesce = (outer == BDRV_DRAIN_ALL) +
406 (inner == BDRV_DRAIN_ALL);
408 g_assert_cmpint(bs->quiesce_counter, ==, 0);
409 g_assert_cmpint(backing->quiesce_counter, ==, 0);
410 g_assert_cmpint(s->drain_count, ==, 0);
411 g_assert_cmpint(backing_s->drain_count, ==, 0);
413 do_drain_begin(outer, bs);
414 do_drain_begin(inner, bs);
416 g_assert_cmpint(bs->quiesce_counter, ==, 2 + !!backing_quiesce);
417 g_assert_cmpint(backing->quiesce_counter, ==, backing_quiesce);
418 g_assert_cmpint(s->drain_count, ==, 1);
419 g_assert_cmpint(backing_s->drain_count, ==, !!backing_quiesce);
421 do_drain_end(inner, bs);
422 do_drain_end(outer, bs);
424 g_assert_cmpint(bs->quiesce_counter, ==, 0);
425 g_assert_cmpint(backing->quiesce_counter, ==, 0);
426 g_assert_cmpint(s->drain_count, ==, 0);
427 g_assert_cmpint(backing_s->drain_count, ==, 0);
436 static void test_graph_change_drain_all(void)
438 BlockBackend *blk_a, *blk_b;
439 BlockDriverState *bs_a, *bs_b;
440 BDRVTestState *a_s, *b_s;
442 /* Create node A with a BlockBackend */
443 blk_a = blk_new(qemu_get_aio_context(), BLK_PERM_ALL, BLK_PERM_ALL);
444 bs_a = bdrv_new_open_driver(&bdrv_test, "test-node-a", BDRV_O_RDWR,
447 blk_insert_bs(blk_a, bs_a, &error_abort);
449 g_assert_cmpint(bs_a->quiesce_counter, ==, 0);
450 g_assert_cmpint(a_s->drain_count, ==, 0);
452 /* Call bdrv_drain_all_begin() */
453 bdrv_drain_all_begin();
455 g_assert_cmpint(bs_a->quiesce_counter, ==, 1);
456 g_assert_cmpint(a_s->drain_count, ==, 1);
458 /* Create node B with a BlockBackend */
459 blk_b = blk_new(qemu_get_aio_context(), BLK_PERM_ALL, BLK_PERM_ALL);
460 bs_b = bdrv_new_open_driver(&bdrv_test, "test-node-b", BDRV_O_RDWR,
463 blk_insert_bs(blk_b, bs_b, &error_abort);
465 g_assert_cmpint(bs_a->quiesce_counter, ==, 1);
466 g_assert_cmpint(bs_b->quiesce_counter, ==, 1);
467 g_assert_cmpint(a_s->drain_count, ==, 1);
468 g_assert_cmpint(b_s->drain_count, ==, 1);
470 /* Unref and finally delete node A */
473 g_assert_cmpint(bs_a->quiesce_counter, ==, 1);
474 g_assert_cmpint(bs_b->quiesce_counter, ==, 1);
475 g_assert_cmpint(a_s->drain_count, ==, 1);
476 g_assert_cmpint(b_s->drain_count, ==, 1);
480 g_assert_cmpint(bs_b->quiesce_counter, ==, 1);
481 g_assert_cmpint(b_s->drain_count, ==, 1);
483 /* End the drained section */
484 bdrv_drain_all_end();
486 g_assert_cmpint(bs_b->quiesce_counter, ==, 0);
487 g_assert_cmpint(b_s->drain_count, ==, 0);
493 struct test_iothread_data {
494 BlockDriverState *bs;
495 enum drain_type drain_type;
500 static void coroutine_fn test_iothread_drain_co_entry(void *opaque)
502 struct test_iothread_data *data = opaque;
504 do_drain_begin(data->drain_type, data->bs);
505 g_assert_cmpint(*data->aio_ret, ==, 0);
506 do_drain_end(data->drain_type, data->bs);
508 data->co_done = true;
512 static void test_iothread_aio_cb(void *opaque, int ret)
514 int *aio_ret = opaque;
516 qemu_event_set(&done_event);
519 static void test_iothread_main_thread_bh(void *opaque)
521 struct test_iothread_data *data = opaque;
523 /* Test that the AioContext is not yet locked in a random BH that is
524 * executed during drain, otherwise this would deadlock. */
525 aio_context_acquire(bdrv_get_aio_context(data->bs));
526 bdrv_flush(data->bs);
527 bdrv_dec_in_flight(data->bs); /* incremented by test_iothread_common() */
528 aio_context_release(bdrv_get_aio_context(data->bs));
532 * Starts an AIO request on a BDS that runs in the AioContext of iothread 1.
533 * The request involves a BH on iothread 2 before it can complete.
535 * @drain_thread = 0 means that do_drain_begin/end are called from the main
536 * thread, @drain_thread = 1 means that they are called from iothread 1. Drain
537 * for this BDS cannot be called from iothread 2 because only the main thread
538 * may do cross-AioContext polling.
540 static void test_iothread_common(enum drain_type drain_type, int drain_thread)
543 BlockDriverState *bs;
548 struct test_iothread_data data;
550 IOThread *a = iothread_new();
551 IOThread *b = iothread_new();
552 AioContext *ctx_a = iothread_get_aio_context(a);
553 AioContext *ctx_b = iothread_get_aio_context(b);
555 QEMUIOVector qiov = QEMU_IOVEC_INIT_BUF(qiov, NULL, 0);
557 /* bdrv_drain_all() may only be called from the main loop thread */
558 if (drain_type == BDRV_DRAIN_ALL && drain_thread != 0) {
562 blk = blk_new(qemu_get_aio_context(), BLK_PERM_ALL, BLK_PERM_ALL);
563 bs = bdrv_new_open_driver(&bdrv_test, "test-node", BDRV_O_RDWR,
566 blk_insert_bs(blk, bs, &error_abort);
567 blk_set_disable_request_queuing(blk, true);
569 blk_set_aio_context(blk, ctx_a, &error_abort);
570 aio_context_acquire(ctx_a);
572 s->bh_indirection_ctx = ctx_b;
574 aio_ret = -EINPROGRESS;
575 qemu_event_reset(&done_event);
577 if (drain_thread == 0) {
578 acb = blk_aio_preadv(blk, 0, &qiov, 0, test_iothread_aio_cb, &aio_ret);
580 acb = blk_aio_preadv(blk, 0, &qiov, 0, aio_ret_cb, &aio_ret);
582 g_assert(acb != NULL);
583 g_assert_cmpint(aio_ret, ==, -EINPROGRESS);
585 aio_context_release(ctx_a);
587 data = (struct test_iothread_data) {
589 .drain_type = drain_type,
593 switch (drain_thread) {
595 if (drain_type != BDRV_DRAIN_ALL) {
596 aio_context_acquire(ctx_a);
600 * Increment in_flight so that do_drain_begin() waits for
601 * test_iothread_main_thread_bh(). This prevents the race between
602 * test_iothread_main_thread_bh() in IOThread a and do_drain_begin() in
603 * this thread. test_iothread_main_thread_bh() decrements in_flight.
605 bdrv_inc_in_flight(bs);
606 aio_bh_schedule_oneshot(ctx_a, test_iothread_main_thread_bh, &data);
608 /* The request is running on the IOThread a. Draining its block device
609 * will make sure that it has completed as far as the BDS is concerned,
610 * but the drain in this thread can continue immediately after
611 * bdrv_dec_in_flight() and aio_ret might be assigned only slightly
613 do_drain_begin(drain_type, bs);
614 g_assert_cmpint(bs->in_flight, ==, 0);
616 if (drain_type != BDRV_DRAIN_ALL) {
617 aio_context_release(ctx_a);
619 qemu_event_wait(&done_event);
620 if (drain_type != BDRV_DRAIN_ALL) {
621 aio_context_acquire(ctx_a);
624 g_assert_cmpint(aio_ret, ==, 0);
625 do_drain_end(drain_type, bs);
627 if (drain_type != BDRV_DRAIN_ALL) {
628 aio_context_release(ctx_a);
632 co = qemu_coroutine_create(test_iothread_drain_co_entry, &data);
633 aio_co_enter(ctx_a, co);
634 AIO_WAIT_WHILE_UNLOCKED(NULL, !data.co_done);
637 g_assert_not_reached();
640 aio_context_acquire(ctx_a);
641 blk_set_aio_context(blk, qemu_get_aio_context(), &error_abort);
642 aio_context_release(ctx_a);
652 static void test_iothread_drain_all(void)
654 test_iothread_common(BDRV_DRAIN_ALL, 0);
655 test_iothread_common(BDRV_DRAIN_ALL, 1);
658 static void test_iothread_drain(void)
660 test_iothread_common(BDRV_DRAIN, 0);
661 test_iothread_common(BDRV_DRAIN, 1);
665 typedef struct TestBlockJob {
667 BlockDriverState *bs;
671 bool should_complete;
674 static int test_job_prepare(Job *job)
676 TestBlockJob *s = container_of(job, TestBlockJob, common.job);
678 /* Provoke an AIO_WAIT_WHILE() call to verify there is no deadlock */
680 return s->prepare_ret;
683 static void test_job_commit(Job *job)
685 TestBlockJob *s = container_of(job, TestBlockJob, common.job);
687 /* Provoke an AIO_WAIT_WHILE() call to verify there is no deadlock */
691 static void test_job_abort(Job *job)
693 TestBlockJob *s = container_of(job, TestBlockJob, common.job);
695 /* Provoke an AIO_WAIT_WHILE() call to verify there is no deadlock */
699 static int coroutine_fn test_job_run(Job *job, Error **errp)
701 TestBlockJob *s = container_of(job, TestBlockJob, common.job);
703 /* We are running the actual job code past the pause point in
707 job_transition_to_ready(&s->common.job);
708 while (!s->should_complete) {
709 /* Avoid job_sleep_ns() because it marks the job as !busy. We want to
710 * emulate some actual activity (probably some I/O) here so that drain
711 * has to wait for this activity to stop. */
712 qemu_co_sleep_ns(QEMU_CLOCK_REALTIME, 1000000);
714 job_pause_point(&s->common.job);
720 static void test_job_complete(Job *job, Error **errp)
722 TestBlockJob *s = container_of(job, TestBlockJob, common.job);
723 s->should_complete = true;
726 BlockJobDriver test_job_driver = {
728 .instance_size = sizeof(TestBlockJob),
729 .free = block_job_free,
730 .user_resume = block_job_user_resume,
732 .complete = test_job_complete,
733 .prepare = test_job_prepare,
734 .commit = test_job_commit,
735 .abort = test_job_abort,
739 enum test_job_result {
742 TEST_JOB_FAIL_PREPARE,
745 enum test_job_drain_node {
747 TEST_JOB_DRAIN_SRC_CHILD,
750 static void test_blockjob_common_drain_node(enum drain_type drain_type,
752 enum test_job_result result,
753 enum test_job_drain_node drain_node)
755 BlockBackend *blk_src, *blk_target;
756 BlockDriverState *src, *src_backing, *src_overlay, *target, *drain_bs;
759 IOThread *iothread = NULL;
763 src = bdrv_new_open_driver(&bdrv_test, "source", BDRV_O_RDWR,
765 src_backing = bdrv_new_open_driver(&bdrv_test, "source-backing",
766 BDRV_O_RDWR, &error_abort);
767 src_overlay = bdrv_new_open_driver(&bdrv_test, "source-overlay",
768 BDRV_O_RDWR, &error_abort);
770 bdrv_set_backing_hd(src_overlay, src, &error_abort);
772 bdrv_set_backing_hd(src, src_backing, &error_abort);
773 bdrv_unref(src_backing);
775 blk_src = blk_new(qemu_get_aio_context(), BLK_PERM_ALL, BLK_PERM_ALL);
776 blk_insert_bs(blk_src, src_overlay, &error_abort);
778 switch (drain_node) {
779 case TEST_JOB_DRAIN_SRC:
782 case TEST_JOB_DRAIN_SRC_CHILD:
783 drain_bs = src_backing;
786 g_assert_not_reached();
790 iothread = iothread_new();
791 ctx = iothread_get_aio_context(iothread);
792 blk_set_aio_context(blk_src, ctx, &error_abort);
794 ctx = qemu_get_aio_context();
797 target = bdrv_new_open_driver(&bdrv_test, "target", BDRV_O_RDWR,
799 blk_target = blk_new(qemu_get_aio_context(), BLK_PERM_ALL, BLK_PERM_ALL);
800 blk_insert_bs(blk_target, target, &error_abort);
801 blk_set_allow_aio_context_change(blk_target, true);
803 aio_context_acquire(ctx);
804 tjob = block_job_create("job0", &test_job_driver, NULL, src,
806 0, 0, NULL, NULL, &error_abort);
810 bdrv_graph_wrlock(target);
811 block_job_add_bdrv(job, "target", target, 0, BLK_PERM_ALL, &error_abort);
812 bdrv_graph_wrunlock();
815 case TEST_JOB_SUCCESS:
817 case TEST_JOB_FAIL_RUN:
818 tjob->run_ret = -EIO;
820 case TEST_JOB_FAIL_PREPARE:
821 tjob->prepare_ret = -EIO;
824 aio_context_release(ctx);
826 job_start(&job->job);
829 /* job_co_entry() is run in the I/O thread, wait for the actual job
830 * code to start (we don't want to catch the job in the pause point in
832 while (!tjob->running) {
833 aio_poll(qemu_get_aio_context(), false);
837 WITH_JOB_LOCK_GUARD() {
838 g_assert_cmpint(job->job.pause_count, ==, 0);
839 g_assert_false(job->job.paused);
840 g_assert_true(tjob->running);
841 g_assert_true(job->job.busy); /* We're in qemu_co_sleep_ns() */
844 do_drain_begin_unlocked(drain_type, drain_bs);
846 WITH_JOB_LOCK_GUARD() {
847 if (drain_type == BDRV_DRAIN_ALL) {
848 /* bdrv_drain_all() drains both src and target */
849 g_assert_cmpint(job->job.pause_count, ==, 2);
851 g_assert_cmpint(job->job.pause_count, ==, 1);
853 g_assert_true(job->job.paused);
854 g_assert_false(job->job.busy); /* The job is paused */
857 do_drain_end_unlocked(drain_type, drain_bs);
861 * Here we are waiting for the paused status to change,
862 * so don't bother protecting the read every time.
864 * paused is reset in the I/O thread, wait for it
866 while (job->job.paused) {
867 aio_poll(qemu_get_aio_context(), false);
871 WITH_JOB_LOCK_GUARD() {
872 g_assert_cmpint(job->job.pause_count, ==, 0);
873 g_assert_false(job->job.paused);
874 g_assert_true(job->job.busy); /* We're in qemu_co_sleep_ns() */
877 do_drain_begin_unlocked(drain_type, target);
879 WITH_JOB_LOCK_GUARD() {
880 if (drain_type == BDRV_DRAIN_ALL) {
881 /* bdrv_drain_all() drains both src and target */
882 g_assert_cmpint(job->job.pause_count, ==, 2);
884 g_assert_cmpint(job->job.pause_count, ==, 1);
886 g_assert_true(job->job.paused);
887 g_assert_false(job->job.busy); /* The job is paused */
890 do_drain_end_unlocked(drain_type, target);
894 * Here we are waiting for the paused status to change,
895 * so don't bother protecting the read every time.
897 * paused is reset in the I/O thread, wait for it
899 while (job->job.paused) {
900 aio_poll(qemu_get_aio_context(), false);
904 WITH_JOB_LOCK_GUARD() {
905 g_assert_cmpint(job->job.pause_count, ==, 0);
906 g_assert_false(job->job.paused);
907 g_assert_true(job->job.busy); /* We're in qemu_co_sleep_ns() */
910 WITH_JOB_LOCK_GUARD() {
911 ret = job_complete_sync_locked(&job->job, &error_abort);
913 g_assert_cmpint(ret, ==, (result == TEST_JOB_SUCCESS ? 0 : -EIO));
915 aio_context_acquire(ctx);
917 blk_set_aio_context(blk_src, qemu_get_aio_context(), &error_abort);
918 assert(blk_get_aio_context(blk_target) == qemu_get_aio_context());
920 aio_context_release(ctx);
923 blk_unref(blk_target);
924 bdrv_unref(src_overlay);
928 iothread_join(iothread);
932 static void test_blockjob_common(enum drain_type drain_type, bool use_iothread,
933 enum test_job_result result)
935 test_blockjob_common_drain_node(drain_type, use_iothread, result,
937 test_blockjob_common_drain_node(drain_type, use_iothread, result,
938 TEST_JOB_DRAIN_SRC_CHILD);
941 static void test_blockjob_drain_all(void)
943 test_blockjob_common(BDRV_DRAIN_ALL, false, TEST_JOB_SUCCESS);
946 static void test_blockjob_drain(void)
948 test_blockjob_common(BDRV_DRAIN, false, TEST_JOB_SUCCESS);
951 static void test_blockjob_error_drain_all(void)
953 test_blockjob_common(BDRV_DRAIN_ALL, false, TEST_JOB_FAIL_RUN);
954 test_blockjob_common(BDRV_DRAIN_ALL, false, TEST_JOB_FAIL_PREPARE);
957 static void test_blockjob_error_drain(void)
959 test_blockjob_common(BDRV_DRAIN, false, TEST_JOB_FAIL_RUN);
960 test_blockjob_common(BDRV_DRAIN, false, TEST_JOB_FAIL_PREPARE);
963 static void test_blockjob_iothread_drain_all(void)
965 test_blockjob_common(BDRV_DRAIN_ALL, true, TEST_JOB_SUCCESS);
968 static void test_blockjob_iothread_drain(void)
970 test_blockjob_common(BDRV_DRAIN, true, TEST_JOB_SUCCESS);
973 static void test_blockjob_iothread_error_drain_all(void)
975 test_blockjob_common(BDRV_DRAIN_ALL, true, TEST_JOB_FAIL_RUN);
976 test_blockjob_common(BDRV_DRAIN_ALL, true, TEST_JOB_FAIL_PREPARE);
979 static void test_blockjob_iothread_error_drain(void)
981 test_blockjob_common(BDRV_DRAIN, true, TEST_JOB_FAIL_RUN);
982 test_blockjob_common(BDRV_DRAIN, true, TEST_JOB_FAIL_PREPARE);
986 typedef struct BDRVTestTopState {
987 BdrvChild *wait_child;
990 static void bdrv_test_top_close(BlockDriverState *bs)
992 BdrvChild *c, *next_c;
994 bdrv_graph_wrlock(NULL);
995 QLIST_FOREACH_SAFE(c, &bs->children, next, next_c) {
996 bdrv_unref_child(bs, c);
998 bdrv_graph_wrunlock();
1001 static int coroutine_fn GRAPH_RDLOCK
1002 bdrv_test_top_co_preadv(BlockDriverState *bs, int64_t offset, int64_t bytes,
1003 QEMUIOVector *qiov, BdrvRequestFlags flags)
1005 BDRVTestTopState *tts = bs->opaque;
1006 return bdrv_co_preadv(tts->wait_child, offset, bytes, qiov, flags);
1009 static BlockDriver bdrv_test_top_driver = {
1010 .format_name = "test_top_driver",
1011 .instance_size = sizeof(BDRVTestTopState),
1013 .bdrv_close = bdrv_test_top_close,
1014 .bdrv_co_preadv = bdrv_test_top_co_preadv,
1016 .bdrv_child_perm = bdrv_default_perms,
1019 typedef struct TestCoDeleteByDrainData {
1021 bool detach_instead_of_delete;
1023 } TestCoDeleteByDrainData;
1025 static void coroutine_fn test_co_delete_by_drain(void *opaque)
1027 TestCoDeleteByDrainData *dbdd = opaque;
1028 BlockBackend *blk = dbdd->blk;
1029 BlockDriverState *bs = blk_bs(blk);
1030 BDRVTestTopState *tts = bs->opaque;
1031 void *buffer = g_malloc(65536);
1032 QEMUIOVector qiov = QEMU_IOVEC_INIT_BUF(qiov, buffer, 65536);
1034 /* Pretend some internal write operation from parent to child.
1035 * Important: We have to read from the child, not from the parent!
1036 * Draining works by first propagating it all up the tree to the
1037 * root and then waiting for drainage from root to the leaves
1038 * (protocol nodes). If we have a request waiting on the root,
1039 * everything will be drained before we go back down the tree, but
1040 * we do not want that. We want to be in the middle of draining
1041 * when this following requests returns. */
1042 bdrv_graph_co_rdlock();
1043 bdrv_co_preadv(tts->wait_child, 0, 65536, &qiov, 0);
1044 bdrv_graph_co_rdunlock();
1046 g_assert_cmpint(bs->refcnt, ==, 1);
1048 if (!dbdd->detach_instead_of_delete) {
1051 BdrvChild *c, *next_c;
1052 bdrv_graph_co_rdlock();
1053 QLIST_FOREACH_SAFE(c, &bs->children, next, next_c) {
1054 bdrv_graph_co_rdunlock();
1055 bdrv_co_unref_child(bs, c);
1056 bdrv_graph_co_rdlock();
1058 bdrv_graph_co_rdunlock();
1066 * Test what happens when some BDS has some children, you drain one of
1067 * them and this results in the BDS being deleted.
1069 * If @detach_instead_of_delete is set, the BDS is not going to be
1070 * deleted but will only detach all of its children.
1072 static void do_test_delete_by_drain(bool detach_instead_of_delete,
1073 enum drain_type drain_type)
1076 BlockDriverState *bs, *child_bs, *null_bs;
1077 BDRVTestTopState *tts;
1078 TestCoDeleteByDrainData dbdd;
1081 bs = bdrv_new_open_driver(&bdrv_test_top_driver, "top", BDRV_O_RDWR,
1083 bs->total_sectors = 65536 >> BDRV_SECTOR_BITS;
1086 null_bs = bdrv_open("null-co://", NULL, NULL, BDRV_O_RDWR | BDRV_O_PROTOCOL,
1088 bdrv_graph_wrlock(NULL);
1089 bdrv_attach_child(bs, null_bs, "null-child", &child_of_bds,
1090 BDRV_CHILD_DATA, &error_abort);
1091 bdrv_graph_wrunlock();
1093 /* This child will be the one to pass to requests through to, and
1094 * it will stall until a drain occurs */
1095 child_bs = bdrv_new_open_driver(&bdrv_test, "child", BDRV_O_RDWR,
1097 child_bs->total_sectors = 65536 >> BDRV_SECTOR_BITS;
1098 /* Takes our reference to child_bs */
1099 bdrv_graph_wrlock(NULL);
1100 tts->wait_child = bdrv_attach_child(bs, child_bs, "wait-child",
1102 BDRV_CHILD_DATA | BDRV_CHILD_PRIMARY,
1104 bdrv_graph_wrunlock();
1106 /* This child is just there to be deleted
1107 * (for detach_instead_of_delete == true) */
1108 null_bs = bdrv_open("null-co://", NULL, NULL, BDRV_O_RDWR | BDRV_O_PROTOCOL,
1110 bdrv_graph_wrlock(NULL);
1111 bdrv_attach_child(bs, null_bs, "null-child", &child_of_bds, BDRV_CHILD_DATA,
1113 bdrv_graph_wrunlock();
1115 blk = blk_new(qemu_get_aio_context(), BLK_PERM_ALL, BLK_PERM_ALL);
1116 blk_insert_bs(blk, bs, &error_abort);
1118 /* Referenced by blk now */
1121 g_assert_cmpint(bs->refcnt, ==, 1);
1122 g_assert_cmpint(child_bs->refcnt, ==, 1);
1123 g_assert_cmpint(null_bs->refcnt, ==, 1);
1126 dbdd = (TestCoDeleteByDrainData){
1128 .detach_instead_of_delete = detach_instead_of_delete,
1131 co = qemu_coroutine_create(test_co_delete_by_drain, &dbdd);
1132 qemu_coroutine_enter(co);
1134 /* Drain the child while the read operation is still pending.
1135 * This should result in the operation finishing and
1136 * test_co_delete_by_drain() resuming. Thus, @bs will be deleted
1137 * and the coroutine will exit while this drain operation is still
1139 switch (drain_type) {
1142 bdrv_drain(child_bs);
1143 bdrv_unref(child_bs);
1145 case BDRV_DRAIN_ALL:
1146 bdrv_drain_all_begin();
1147 bdrv_drain_all_end();
1150 g_assert_not_reached();
1153 while (!dbdd.done) {
1154 aio_poll(qemu_get_aio_context(), true);
1157 if (detach_instead_of_delete) {
1158 /* Here, the reference has not passed over to the coroutine,
1159 * so we have to delete the BB ourselves */
1164 static void test_delete_by_drain(void)
1166 do_test_delete_by_drain(false, BDRV_DRAIN);
1169 static void test_detach_by_drain_all(void)
1171 do_test_delete_by_drain(true, BDRV_DRAIN_ALL);
1174 static void test_detach_by_drain(void)
1176 do_test_delete_by_drain(true, BDRV_DRAIN);
1180 struct detach_by_parent_data {
1181 BlockDriverState *parent_b;
1183 BlockDriverState *c;
1186 bool detach_on_drain;
1188 static struct detach_by_parent_data detach_by_parent_data;
1190 static void no_coroutine_fn detach_indirect_bh(void *opaque)
1192 struct detach_by_parent_data *data = opaque;
1194 bdrv_dec_in_flight(data->child_b->bs);
1196 bdrv_graph_wrlock(NULL);
1197 bdrv_unref_child(data->parent_b, data->child_b);
1200 data->child_c = bdrv_attach_child(data->parent_b, data->c, "PB-C",
1201 &child_of_bds, BDRV_CHILD_DATA,
1203 bdrv_graph_wrunlock();
1206 static void coroutine_mixed_fn detach_by_parent_aio_cb(void *opaque, int ret)
1208 struct detach_by_parent_data *data = &detach_by_parent_data;
1210 g_assert_cmpint(ret, ==, 0);
1211 if (data->by_parent_cb) {
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);
1218 static void GRAPH_RDLOCK detach_by_driver_cb_drained_begin(BdrvChild *child)
1220 struct detach_by_parent_data *data = &detach_by_parent_data;
1222 if (!data->detach_on_drain) {
1225 data->detach_on_drain = false;
1227 bdrv_inc_in_flight(data->child_b->bs);
1228 aio_bh_schedule_oneshot(qemu_get_current_aio_context(),
1229 detach_indirect_bh, &detach_by_parent_data);
1230 child_of_bds.drained_begin(child);
1233 static BdrvChildClass detach_by_driver_cb_class;
1242 * by_parent_cb == true: Test that parent callbacks don't poll
1244 * PA has a pending write request whose callback changes the child nodes of
1245 * PB: It removes B and adds C instead. The subtree of PB is drained, which
1246 * will indirectly drain the write request, too.
1248 * by_parent_cb == false: Test that bdrv_drain_invoke() doesn't poll
1250 * PA's BdrvChildClass has a .drained_begin callback that schedules a BH
1251 * that does the same graph change. If bdrv_drain_invoke() calls it, the
1252 * state is messed up, but if it is only polled in the single
1253 * BDRV_POLL_WHILE() at the end of the drain, this should work fine.
1255 static void TSA_NO_TSA test_detach_indirect(bool by_parent_cb)
1258 BlockDriverState *parent_a, *parent_b, *a, *b, *c;
1259 BdrvChild *child_a, *child_b;
1262 QEMUIOVector qiov = QEMU_IOVEC_INIT_BUF(qiov, NULL, 0);
1264 if (!by_parent_cb) {
1265 detach_by_driver_cb_class = child_of_bds;
1266 detach_by_driver_cb_class.drained_begin =
1267 detach_by_driver_cb_drained_begin;
1268 detach_by_driver_cb_class.drained_end = NULL;
1269 detach_by_driver_cb_class.drained_poll = NULL;
1272 detach_by_parent_data = (struct detach_by_parent_data) {
1273 .detach_on_drain = false,
1276 /* Create all involved nodes */
1277 parent_a = bdrv_new_open_driver(&bdrv_test, "parent-a", BDRV_O_RDWR,
1279 parent_b = bdrv_new_open_driver(&bdrv_test, "parent-b", 0,
1282 a = bdrv_new_open_driver(&bdrv_test, "a", BDRV_O_RDWR, &error_abort);
1283 b = bdrv_new_open_driver(&bdrv_test, "b", BDRV_O_RDWR, &error_abort);
1284 c = bdrv_new_open_driver(&bdrv_test, "c", BDRV_O_RDWR, &error_abort);
1286 /* blk is a BB for parent-a */
1287 blk = blk_new(qemu_get_aio_context(), BLK_PERM_ALL, BLK_PERM_ALL);
1288 blk_insert_bs(blk, parent_a, &error_abort);
1289 bdrv_unref(parent_a);
1291 /* If we want to get bdrv_drain_invoke() to call aio_poll(), the driver
1292 * callback must not return immediately. */
1293 if (!by_parent_cb) {
1294 BDRVTestState *s = parent_a->opaque;
1295 s->sleep_in_drain_begin = true;
1298 /* Set child relationships */
1301 bdrv_graph_wrlock(NULL);
1302 child_b = bdrv_attach_child(parent_b, b, "PB-B", &child_of_bds,
1303 BDRV_CHILD_DATA, &error_abort);
1304 child_a = bdrv_attach_child(parent_b, a, "PB-A", &child_of_bds,
1305 BDRV_CHILD_COW, &error_abort);
1308 bdrv_attach_child(parent_a, a, "PA-A",
1309 by_parent_cb ? &child_of_bds : &detach_by_driver_cb_class,
1310 BDRV_CHILD_DATA, &error_abort);
1311 bdrv_graph_wrunlock();
1313 g_assert_cmpint(parent_a->refcnt, ==, 1);
1314 g_assert_cmpint(parent_b->refcnt, ==, 1);
1315 g_assert_cmpint(a->refcnt, ==, 3);
1316 g_assert_cmpint(b->refcnt, ==, 2);
1317 g_assert_cmpint(c->refcnt, ==, 1);
1319 g_assert(QLIST_FIRST(&parent_b->children) == child_a);
1320 g_assert(QLIST_NEXT(child_a, next) == child_b);
1321 g_assert(QLIST_NEXT(child_b, next) == NULL);
1323 /* Start the evil write request */
1324 detach_by_parent_data = (struct detach_by_parent_data) {
1325 .parent_b = parent_b,
1328 .by_parent_cb = by_parent_cb,
1329 .detach_on_drain = true,
1331 acb = blk_aio_preadv(blk, 0, &qiov, 0, detach_by_parent_aio_cb, NULL);
1332 g_assert(acb != NULL);
1334 /* Drain and check the expected result */
1335 bdrv_drained_begin(parent_b);
1336 bdrv_drained_begin(a);
1337 bdrv_drained_begin(b);
1338 bdrv_drained_begin(c);
1340 g_assert(detach_by_parent_data.child_c != NULL);
1342 g_assert_cmpint(parent_a->refcnt, ==, 1);
1343 g_assert_cmpint(parent_b->refcnt, ==, 1);
1344 g_assert_cmpint(a->refcnt, ==, 3);
1345 g_assert_cmpint(b->refcnt, ==, 1);
1346 g_assert_cmpint(c->refcnt, ==, 2);
1348 g_assert(QLIST_FIRST(&parent_b->children) == detach_by_parent_data.child_c);
1349 g_assert(QLIST_NEXT(detach_by_parent_data.child_c, next) == child_a);
1350 g_assert(QLIST_NEXT(child_a, next) == NULL);
1352 g_assert_cmpint(parent_a->quiesce_counter, ==, 1);
1353 g_assert_cmpint(parent_b->quiesce_counter, ==, 3);
1354 g_assert_cmpint(a->quiesce_counter, ==, 1);
1355 g_assert_cmpint(b->quiesce_counter, ==, 1);
1356 g_assert_cmpint(c->quiesce_counter, ==, 1);
1358 bdrv_drained_end(parent_b);
1359 bdrv_drained_end(a);
1360 bdrv_drained_end(b);
1361 bdrv_drained_end(c);
1363 bdrv_unref(parent_b);
1366 g_assert_cmpint(a->refcnt, ==, 1);
1367 g_assert_cmpint(b->refcnt, ==, 1);
1368 g_assert_cmpint(c->refcnt, ==, 1);
1374 static void test_detach_by_parent_cb(void)
1376 test_detach_indirect(true);
1379 static void test_detach_by_driver_cb(void)
1381 test_detach_indirect(false);
1384 static void test_append_to_drained(void)
1387 BlockDriverState *base, *overlay;
1388 BDRVTestState *base_s, *overlay_s;
1390 blk = blk_new(qemu_get_aio_context(), BLK_PERM_ALL, BLK_PERM_ALL);
1391 base = bdrv_new_open_driver(&bdrv_test, "base", BDRV_O_RDWR, &error_abort);
1392 base_s = base->opaque;
1393 blk_insert_bs(blk, base, &error_abort);
1395 overlay = bdrv_new_open_driver(&bdrv_test, "overlay", BDRV_O_RDWR,
1397 overlay_s = overlay->opaque;
1399 do_drain_begin(BDRV_DRAIN, base);
1400 g_assert_cmpint(base->quiesce_counter, ==, 1);
1401 g_assert_cmpint(base_s->drain_count, ==, 1);
1402 g_assert_cmpint(base->in_flight, ==, 0);
1404 aio_context_acquire(qemu_get_aio_context());
1405 bdrv_append(overlay, base, &error_abort);
1406 aio_context_release(qemu_get_aio_context());
1408 g_assert_cmpint(base->in_flight, ==, 0);
1409 g_assert_cmpint(overlay->in_flight, ==, 0);
1411 g_assert_cmpint(base->quiesce_counter, ==, 1);
1412 g_assert_cmpint(base_s->drain_count, ==, 1);
1413 g_assert_cmpint(overlay->quiesce_counter, ==, 1);
1414 g_assert_cmpint(overlay_s->drain_count, ==, 1);
1416 do_drain_end(BDRV_DRAIN, base);
1418 g_assert_cmpint(base->quiesce_counter, ==, 0);
1419 g_assert_cmpint(base_s->drain_count, ==, 0);
1420 g_assert_cmpint(overlay->quiesce_counter, ==, 0);
1421 g_assert_cmpint(overlay_s->drain_count, ==, 0);
1423 bdrv_unref(overlay);
1428 static void test_set_aio_context(void)
1430 BlockDriverState *bs;
1431 IOThread *a = iothread_new();
1432 IOThread *b = iothread_new();
1433 AioContext *ctx_a = iothread_get_aio_context(a);
1434 AioContext *ctx_b = iothread_get_aio_context(b);
1436 bs = bdrv_new_open_driver(&bdrv_test, "test-node", BDRV_O_RDWR,
1439 bdrv_drained_begin(bs);
1440 bdrv_try_change_aio_context(bs, ctx_a, NULL, &error_abort);
1442 aio_context_acquire(ctx_a);
1443 bdrv_drained_end(bs);
1445 bdrv_drained_begin(bs);
1446 bdrv_try_change_aio_context(bs, ctx_b, NULL, &error_abort);
1447 aio_context_release(ctx_a);
1448 aio_context_acquire(ctx_b);
1449 bdrv_try_change_aio_context(bs, qemu_get_aio_context(), NULL, &error_abort);
1450 aio_context_release(ctx_b);
1451 bdrv_drained_end(bs);
1459 typedef struct TestDropBackingBlockJob {
1461 bool should_complete;
1463 BlockDriverState *detach_also;
1464 BlockDriverState *bs;
1465 } TestDropBackingBlockJob;
1467 static int coroutine_fn test_drop_backing_job_run(Job *job, Error **errp)
1469 TestDropBackingBlockJob *s =
1470 container_of(job, TestDropBackingBlockJob, common.job);
1472 while (!s->should_complete) {
1473 job_sleep_ns(job, 0);
1479 static void test_drop_backing_job_commit(Job *job)
1481 TestDropBackingBlockJob *s =
1482 container_of(job, TestDropBackingBlockJob, common.job);
1484 bdrv_set_backing_hd(s->bs, NULL, &error_abort);
1485 bdrv_set_backing_hd(s->detach_also, NULL, &error_abort);
1487 *s->did_complete = true;
1490 static const BlockJobDriver test_drop_backing_job_driver = {
1492 .instance_size = sizeof(TestDropBackingBlockJob),
1493 .free = block_job_free,
1494 .user_resume = block_job_user_resume,
1495 .run = test_drop_backing_job_run,
1496 .commit = test_drop_backing_job_commit,
1501 * Creates a child node with three parent nodes on it, and then runs a
1502 * block job on the final one, parent-node-2.
1504 * The job is then asked to complete before a section where the child
1507 * Ending this section will undrain the child's parents, first
1508 * parent-node-2, then parent-node-1, then parent-node-0 -- the parent
1509 * list is in reverse order of how they were added. Ending the drain
1510 * on parent-node-2 will resume the job, thus completing it and
1511 * scheduling job_exit().
1513 * Ending the drain on parent-node-1 will poll the AioContext, which
1514 * lets job_exit() and thus test_drop_backing_job_commit() run. That
1515 * function first removes the child as parent-node-2's backing file.
1517 * In old (and buggy) implementations, there are two problems with
1519 * (A) bdrv_drain_invoke() polls for every node that leaves the
1520 * drained section. This means that job_exit() is scheduled
1521 * before the child has left the drained section. Its
1522 * quiesce_counter is therefore still 1 when it is removed from
1525 * (B) bdrv_replace_child_noperm() calls drained_end() on the old
1526 * child's parents as many times as the child is quiesced. This
1527 * means it will call drained_end() on parent-node-2 once.
1528 * Because parent-node-2 is no longer quiesced at this point, this
1531 * bdrv_replace_child_noperm() therefore must call drained_end() on
1532 * the parent only if it really is still drained because the child is
1535 * If removing child from parent-node-2 was successful (as it should
1536 * be), test_drop_backing_job_commit() will then also remove the child
1537 * from parent-node-0.
1539 * With an old version of our drain infrastructure ((A) above), that
1540 * resulted in the following flow:
1542 * 1. child attempts to leave its drained section. The call recurses
1545 * 2. parent-node-2 leaves the drained section. Polling in
1546 * bdrv_drain_invoke() will schedule job_exit().
1548 * 3. parent-node-1 leaves the drained section. Polling in
1549 * bdrv_drain_invoke() will run job_exit(), thus disconnecting
1550 * parent-node-0 from the child node.
1552 * 4. bdrv_parent_drained_end() uses a QLIST_FOREACH_SAFE() loop to
1553 * iterate over the parents. Thus, it now accesses the BdrvChild
1554 * object that used to connect parent-node-0 and the child node.
1555 * However, that object no longer exists, so it accesses a dangling
1558 * The solution is to only poll once when running a bdrv_drained_end()
1559 * operation, specifically at the end when all drained_end()
1560 * operations for all involved nodes have been scheduled.
1561 * Note that this also solves (A) above, thus hiding (B).
1563 static void test_blockjob_commit_by_drained_end(void)
1565 BlockDriverState *bs_child, *bs_parents[3];
1566 TestDropBackingBlockJob *job;
1567 bool job_has_completed = false;
1570 bs_child = bdrv_new_open_driver(&bdrv_test, "child-node", BDRV_O_RDWR,
1573 for (i = 0; i < 3; i++) {
1575 snprintf(name, sizeof(name), "parent-node-%i", i);
1576 bs_parents[i] = bdrv_new_open_driver(&bdrv_test, name, BDRV_O_RDWR,
1578 bdrv_set_backing_hd(bs_parents[i], bs_child, &error_abort);
1581 job = block_job_create("job", &test_drop_backing_job_driver, NULL,
1582 bs_parents[2], 0, BLK_PERM_ALL, 0, 0, NULL, NULL,
1584 job->bs = bs_parents[2];
1586 job->detach_also = bs_parents[0];
1587 job->did_complete = &job_has_completed;
1589 job_start(&job->common.job);
1591 job->should_complete = true;
1592 bdrv_drained_begin(bs_child);
1593 g_assert(!job_has_completed);
1594 bdrv_drained_end(bs_child);
1595 aio_poll(qemu_get_aio_context(), false);
1596 g_assert(job_has_completed);
1598 bdrv_unref(bs_parents[0]);
1599 bdrv_unref(bs_parents[1]);
1600 bdrv_unref(bs_parents[2]);
1601 bdrv_unref(bs_child);
1605 typedef struct TestSimpleBlockJob {
1607 bool should_complete;
1609 } TestSimpleBlockJob;
1611 static int coroutine_fn test_simple_job_run(Job *job, Error **errp)
1613 TestSimpleBlockJob *s = container_of(job, TestSimpleBlockJob, common.job);
1615 while (!s->should_complete) {
1616 job_sleep_ns(job, 0);
1622 static void test_simple_job_clean(Job *job)
1624 TestSimpleBlockJob *s = container_of(job, TestSimpleBlockJob, common.job);
1625 *s->did_complete = true;
1628 static const BlockJobDriver test_simple_job_driver = {
1630 .instance_size = sizeof(TestSimpleBlockJob),
1631 .free = block_job_free,
1632 .user_resume = block_job_user_resume,
1633 .run = test_simple_job_run,
1634 .clean = test_simple_job_clean,
1638 static int drop_intermediate_poll_update_filename(BdrvChild *child,
1639 BlockDriverState *new_base,
1640 const char *filename,
1644 * We are free to poll here, which may change the block graph, if
1645 * it is not drained.
1648 /* If the job is not drained: Complete it, schedule job_exit() */
1649 aio_poll(qemu_get_current_aio_context(), false);
1650 /* If the job is not drained: Run job_exit(), finish the job */
1651 aio_poll(qemu_get_current_aio_context(), false);
1657 * Test a poll in the midst of bdrv_drop_intermediate().
1659 * bdrv_drop_intermediate() calls BdrvChildClass.update_filename(),
1660 * which can yield or poll. This may lead to graph changes, unless
1661 * the whole subtree in question is drained.
1663 * We test this on the following graph:
1679 * node-2 --chain--> node-1 --chain--> node-0
1681 * We drop node-1 with bdrv_drop_intermediate(top=node-1, base=node-0).
1683 * This first updates node-2's backing filename by invoking
1684 * drop_intermediate_poll_update_filename(), which polls twice. This
1685 * causes the job to finish, which in turns causes the job-node to be
1688 * bdrv_drop_intermediate() uses a QLIST_FOREACH_SAFE() loop, so it
1689 * already has a pointer to the BdrvChild edge between job-node and
1690 * node-1. When it tries to handle that edge, we probably get a
1691 * segmentation fault because the object no longer exists.
1694 * The solution is for bdrv_drop_intermediate() to drain top's
1695 * subtree. This prevents graph changes from happening just because
1696 * BdrvChildClass.update_filename() yields or polls. Thus, the block
1697 * job is paused during that drained section and must finish before or
1700 * (In addition, bdrv_replace_child() must keep the job paused.)
1702 static void test_drop_intermediate_poll(void)
1704 static BdrvChildClass chain_child_class;
1705 BlockDriverState *chain[3];
1706 TestSimpleBlockJob *job;
1707 BlockDriverState *job_node;
1708 bool job_has_completed = false;
1712 chain_child_class = child_of_bds;
1713 chain_child_class.update_filename = drop_intermediate_poll_update_filename;
1715 for (i = 0; i < 3; i++) {
1717 snprintf(name, 32, "node-%i", i);
1719 chain[i] = bdrv_new_open_driver(&bdrv_test, name, 0, &error_abort);
1722 job_node = bdrv_new_open_driver(&bdrv_test, "job-node", BDRV_O_RDWR,
1724 bdrv_set_backing_hd(job_node, chain[1], &error_abort);
1727 * Establish the chain last, so the chain links are the first
1728 * elements in the BDS.parents lists
1730 bdrv_graph_wrlock(NULL);
1731 for (i = 0; i < 3; i++) {
1733 /* Takes the reference to chain[i - 1] */
1734 bdrv_attach_child(chain[i], chain[i - 1], "chain",
1735 &chain_child_class, BDRV_CHILD_COW, &error_abort);
1738 bdrv_graph_wrunlock();
1740 job = block_job_create("job", &test_simple_job_driver, NULL, job_node,
1741 0, BLK_PERM_ALL, 0, 0, NULL, NULL, &error_abort);
1743 /* The job has a reference now */
1744 bdrv_unref(job_node);
1746 job->did_complete = &job_has_completed;
1748 job_start(&job->common.job);
1749 job->should_complete = true;
1751 g_assert(!job_has_completed);
1752 ret = bdrv_drop_intermediate(chain[1], chain[0], NULL);
1753 aio_poll(qemu_get_aio_context(), false);
1755 g_assert(job_has_completed);
1757 bdrv_unref(chain[2]);
1761 typedef struct BDRVReplaceTestState {
1762 bool setup_completed;
1769 bool yield_before_read;
1771 Coroutine *drain_co;
1772 } BDRVReplaceTestState;
1774 static void bdrv_replace_test_close(BlockDriverState *bs)
1779 * If @bs has a backing file:
1780 * Yield if .yield_before_read is true (and wait for drain_begin to
1782 * Forward the read to bs->backing. Set .has_read to true.
1783 * If drain_begin has woken us, wake it in turn.
1786 * Set .has_read to true and return success.
1788 static int coroutine_fn GRAPH_RDLOCK
1789 bdrv_replace_test_co_preadv(BlockDriverState *bs, int64_t offset, int64_t bytes,
1790 QEMUIOVector *qiov, BdrvRequestFlags flags)
1792 BDRVReplaceTestState *s = bs->opaque;
1797 g_assert(!s->drain_count);
1799 s->io_co = qemu_coroutine_self();
1800 if (s->yield_before_read) {
1801 s->yield_before_read = false;
1802 qemu_coroutine_yield();
1806 ret = bdrv_co_preadv(bs->backing, offset, bytes, qiov, 0);
1809 /* Wake up drain_co if it runs */
1811 aio_co_wake(s->drain_co);
1821 static void coroutine_fn bdrv_replace_test_drain_co(void *opaque)
1823 BlockDriverState *bs = opaque;
1824 BDRVReplaceTestState *s = bs->opaque;
1826 /* Keep waking io_co up until it is done */
1828 aio_co_wake(s->io_co);
1830 qemu_coroutine_yield();
1833 bdrv_dec_in_flight(bs);
1837 * If .drain_count is 0, wake up .io_co if there is one; and set
1839 * Increment .drain_count.
1841 static void bdrv_replace_test_drain_begin(BlockDriverState *bs)
1843 BDRVReplaceTestState *s = bs->opaque;
1845 if (!s->setup_completed) {
1849 if (!s->drain_count) {
1850 s->drain_co = qemu_coroutine_create(bdrv_replace_test_drain_co, bs);
1851 bdrv_inc_in_flight(bs);
1852 aio_co_enter(bdrv_get_aio_context(bs), s->drain_co);
1853 s->was_drained = true;
1858 static void coroutine_fn bdrv_replace_test_read_entry(void *opaque)
1860 BlockDriverState *bs = opaque;
1862 QEMUIOVector qiov = QEMU_IOVEC_INIT_BUF(qiov, &data, 1);
1865 /* Queue a read request post-drain */
1866 bdrv_graph_co_rdlock();
1867 ret = bdrv_replace_test_co_preadv(bs, 0, 1, &qiov, 0);
1868 bdrv_graph_co_rdunlock();
1871 bdrv_dec_in_flight(bs);
1875 * Reduce .drain_count, set .was_undrained once it reaches 0.
1876 * If .drain_count reaches 0 and the node has a backing file, issue a
1879 static void bdrv_replace_test_drain_end(BlockDriverState *bs)
1881 BDRVReplaceTestState *s = bs->opaque;
1883 GRAPH_RDLOCK_GUARD_MAINLOOP();
1885 if (!s->setup_completed) {
1889 g_assert(s->drain_count > 0);
1890 if (!--s->drain_count) {
1891 s->was_undrained = true;
1894 Coroutine *co = qemu_coroutine_create(bdrv_replace_test_read_entry,
1896 bdrv_inc_in_flight(bs);
1897 aio_co_enter(bdrv_get_aio_context(bs), co);
1902 static BlockDriver bdrv_replace_test = {
1903 .format_name = "replace_test",
1904 .instance_size = sizeof(BDRVReplaceTestState),
1905 .supports_backing = true,
1907 .bdrv_close = bdrv_replace_test_close,
1908 .bdrv_co_preadv = bdrv_replace_test_co_preadv,
1910 .bdrv_drain_begin = bdrv_replace_test_drain_begin,
1911 .bdrv_drain_end = bdrv_replace_test_drain_end,
1913 .bdrv_child_perm = bdrv_default_perms,
1916 static void coroutine_fn test_replace_child_mid_drain_read_co(void *opaque)
1921 ret = blk_co_pread(opaque, 0, 1, &data, 0);
1926 * We test two things:
1927 * (1) bdrv_replace_child_noperm() must not undrain the parent if both
1928 * children are drained.
1929 * (2) bdrv_replace_child_noperm() must never flush I/O requests to a
1930 * drained child. If the old child is drained, it must flush I/O
1931 * requests after the new one has been attached. If the new child
1932 * is drained, it must flush I/O requests before the old one is
1935 * To do so, we create one parent node and two child nodes; then
1936 * attach one of the children (old_child_bs) to the parent, then
1937 * drain both old_child_bs and new_child_bs according to
1938 * old_drain_count and new_drain_count, respectively, and finally
1939 * we invoke bdrv_replace_node() to replace old_child_bs by
1942 * The test block driver we use here (bdrv_replace_test) has a read
1944 * - For the parent node, can optionally yield, and then forwards the
1945 * read to bdrv_preadv(),
1946 * - For the child node, just returns immediately.
1948 * If the read yields, the drain_begin function will wake it up.
1950 * The drain_end function issues a read on the parent once it is fully
1951 * undrained (which simulates requests starting to come in again).
1953 static void do_test_replace_child_mid_drain(int old_drain_count,
1954 int new_drain_count)
1956 BlockBackend *parent_blk;
1957 BlockDriverState *parent_bs;
1958 BlockDriverState *old_child_bs, *new_child_bs;
1959 BDRVReplaceTestState *parent_s;
1960 BDRVReplaceTestState *old_child_s, *new_child_s;
1964 parent_bs = bdrv_new_open_driver(&bdrv_replace_test, "parent", 0,
1966 parent_s = parent_bs->opaque;
1968 parent_blk = blk_new(qemu_get_aio_context(),
1969 BLK_PERM_CONSISTENT_READ, BLK_PERM_ALL);
1970 blk_insert_bs(parent_blk, parent_bs, &error_abort);
1972 old_child_bs = bdrv_new_open_driver(&bdrv_replace_test, "old-child", 0,
1974 new_child_bs = bdrv_new_open_driver(&bdrv_replace_test, "new-child", 0,
1976 old_child_s = old_child_bs->opaque;
1977 new_child_s = new_child_bs->opaque;
1979 /* So that we can read something */
1980 parent_bs->total_sectors = 1;
1981 old_child_bs->total_sectors = 1;
1982 new_child_bs->total_sectors = 1;
1984 bdrv_ref(old_child_bs);
1985 bdrv_graph_wrlock(NULL);
1986 bdrv_attach_child(parent_bs, old_child_bs, "child", &child_of_bds,
1987 BDRV_CHILD_COW, &error_abort);
1988 bdrv_graph_wrunlock();
1989 parent_s->setup_completed = true;
1991 for (i = 0; i < old_drain_count; i++) {
1992 bdrv_drained_begin(old_child_bs);
1994 for (i = 0; i < new_drain_count; i++) {
1995 bdrv_drained_begin(new_child_bs);
1998 if (!old_drain_count) {
2000 * Start a read operation that will yield, so it will not
2001 * complete before the node is drained.
2003 parent_s->yield_before_read = true;
2004 io_co = qemu_coroutine_create(test_replace_child_mid_drain_read_co,
2006 qemu_coroutine_enter(io_co);
2009 /* If we have started a read operation, it should have yielded */
2010 g_assert(!parent_s->has_read);
2012 /* Reset drained status so we can see what bdrv_replace_node() does */
2013 parent_s->was_drained = false;
2014 parent_s->was_undrained = false;
2016 g_assert(parent_bs->quiesce_counter == old_drain_count);
2017 bdrv_drained_begin(old_child_bs);
2018 bdrv_drained_begin(new_child_bs);
2019 bdrv_graph_wrlock(NULL);
2020 bdrv_replace_node(old_child_bs, new_child_bs, &error_abort);
2021 bdrv_graph_wrunlock();
2022 bdrv_drained_end(new_child_bs);
2023 bdrv_drained_end(old_child_bs);
2024 g_assert(parent_bs->quiesce_counter == new_drain_count);
2026 if (!old_drain_count && !new_drain_count) {
2028 * From undrained to undrained drains and undrains the parent,
2029 * because bdrv_replace_node() contains a drained section for
2032 g_assert(parent_s->was_drained && parent_s->was_undrained);
2033 } else if (!old_drain_count && new_drain_count) {
2035 * From undrained to drained should drain the parent and keep
2038 g_assert(parent_s->was_drained && !parent_s->was_undrained);
2039 } else if (old_drain_count && !new_drain_count) {
2041 * From drained to undrained should undrain the parent and
2044 g_assert(!parent_s->was_drained && parent_s->was_undrained);
2045 } else /* if (old_drain_count && new_drain_count) */ {
2047 * From drained to drained must not undrain the parent at any
2050 g_assert(!parent_s->was_drained && !parent_s->was_undrained);
2053 if (!old_drain_count || !new_drain_count) {
2055 * If !old_drain_count, we have started a read request before
2056 * bdrv_replace_node(). If !new_drain_count, the parent must
2057 * have been undrained at some point, and
2058 * bdrv_replace_test_co_drain_end() starts a read request
2061 g_assert(parent_s->has_read);
2064 * If the parent was never undrained, there is no way to start
2067 g_assert(!parent_s->has_read);
2070 /* A drained child must have not received any request */
2071 g_assert(!(old_drain_count && old_child_s->has_read));
2072 g_assert(!(new_drain_count && new_child_s->has_read));
2074 for (i = 0; i < new_drain_count; i++) {
2075 bdrv_drained_end(new_child_bs);
2077 for (i = 0; i < old_drain_count; i++) {
2078 bdrv_drained_end(old_child_bs);
2082 * By now, bdrv_replace_test_co_drain_end() must have been called
2083 * at some point while the new child was attached to the parent.
2085 g_assert(parent_s->has_read);
2086 g_assert(new_child_s->has_read);
2088 blk_unref(parent_blk);
2089 bdrv_unref(parent_bs);
2090 bdrv_unref(old_child_bs);
2091 bdrv_unref(new_child_bs);
2094 static void test_replace_child_mid_drain(void)
2096 int old_drain_count, new_drain_count;
2098 for (old_drain_count = 0; old_drain_count < 2; old_drain_count++) {
2099 for (new_drain_count = 0; new_drain_count < 2; new_drain_count++) {
2100 do_test_replace_child_mid_drain(old_drain_count, new_drain_count);
2105 int main(int argc, char **argv)
2110 qemu_init_main_loop(&error_abort);
2112 g_test_init(&argc, &argv, NULL);
2113 qemu_event_init(&done_event, false);
2115 g_test_add_func("/bdrv-drain/driver-cb/drain_all", test_drv_cb_drain_all);
2116 g_test_add_func("/bdrv-drain/driver-cb/drain", test_drv_cb_drain);
2118 g_test_add_func("/bdrv-drain/driver-cb/co/drain_all",
2119 test_drv_cb_co_drain_all);
2120 g_test_add_func("/bdrv-drain/driver-cb/co/drain", test_drv_cb_co_drain);
2122 g_test_add_func("/bdrv-drain/quiesce/drain_all", test_quiesce_drain_all);
2123 g_test_add_func("/bdrv-drain/quiesce/drain", test_quiesce_drain);
2125 g_test_add_func("/bdrv-drain/quiesce/co/drain_all",
2126 test_quiesce_co_drain_all);
2127 g_test_add_func("/bdrv-drain/quiesce/co/drain", test_quiesce_co_drain);
2129 g_test_add_func("/bdrv-drain/nested", test_nested);
2131 g_test_add_func("/bdrv-drain/graph-change/drain_all",
2132 test_graph_change_drain_all);
2134 g_test_add_func("/bdrv-drain/iothread/drain_all", test_iothread_drain_all);
2135 g_test_add_func("/bdrv-drain/iothread/drain", test_iothread_drain);
2137 g_test_add_func("/bdrv-drain/blockjob/drain_all", test_blockjob_drain_all);
2138 g_test_add_func("/bdrv-drain/blockjob/drain", test_blockjob_drain);
2140 g_test_add_func("/bdrv-drain/blockjob/error/drain_all",
2141 test_blockjob_error_drain_all);
2142 g_test_add_func("/bdrv-drain/blockjob/error/drain",
2143 test_blockjob_error_drain);
2145 g_test_add_func("/bdrv-drain/blockjob/iothread/drain_all",
2146 test_blockjob_iothread_drain_all);
2147 g_test_add_func("/bdrv-drain/blockjob/iothread/drain",
2148 test_blockjob_iothread_drain);
2150 g_test_add_func("/bdrv-drain/blockjob/iothread/error/drain_all",
2151 test_blockjob_iothread_error_drain_all);
2152 g_test_add_func("/bdrv-drain/blockjob/iothread/error/drain",
2153 test_blockjob_iothread_error_drain);
2155 g_test_add_func("/bdrv-drain/deletion/drain", test_delete_by_drain);
2156 g_test_add_func("/bdrv-drain/detach/drain_all", test_detach_by_drain_all);
2157 g_test_add_func("/bdrv-drain/detach/drain", test_detach_by_drain);
2158 g_test_add_func("/bdrv-drain/detach/parent_cb", test_detach_by_parent_cb);
2159 g_test_add_func("/bdrv-drain/detach/driver_cb", test_detach_by_driver_cb);
2161 g_test_add_func("/bdrv-drain/attach/drain", test_append_to_drained);
2163 g_test_add_func("/bdrv-drain/set_aio_context", test_set_aio_context);
2165 g_test_add_func("/bdrv-drain/blockjob/commit_by_drained_end",
2166 test_blockjob_commit_by_drained_end);
2168 g_test_add_func("/bdrv-drain/bdrv_drop_intermediate/poll",
2169 test_drop_intermediate_poll);
2171 g_test_add_func("/bdrv-drain/replace_child/mid-drain",
2172 test_replace_child_mid_drain);
2175 qemu_event_destroy(&done_event);