2 * Block node draining tests
4 * Copyright (c) 2017 Kevin Wolf <kwolf@redhat.com>
6 * Permission is hereby granted, free of charge, to any person obtaining a copy
7 * of this software and associated documentation files (the "Software"), to deal
8 * in the Software without restriction, including without limitation the rights
9 * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
10 * copies of the Software, and to permit persons to whom the Software is
11 * furnished to do so, subject to the following conditions:
13 * The above copyright notice and this permission notice shall be included in
14 * all copies or substantial portions of the Software.
16 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
17 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
18 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
19 * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
20 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
21 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
25 #include "qemu/osdep.h"
26 #include "block/block_int.h"
27 #include "block/blockjob_int.h"
28 #include "sysemu/block-backend.h"
29 #include "qapi/error.h"
30 #include "qemu/main-loop.h"
33 static QemuEvent done_event
;
35 typedef struct BDRVTestState
{
37 AioContext
*bh_indirection_ctx
;
38 bool sleep_in_drain_begin
;
41 static void coroutine_fn
sleep_in_drain_begin(void *opaque
)
43 BlockDriverState
*bs
= opaque
;
45 qemu_co_sleep_ns(QEMU_CLOCK_REALTIME
, 100000);
46 bdrv_dec_in_flight(bs
);
49 static void bdrv_test_drain_begin(BlockDriverState
*bs
)
51 BDRVTestState
*s
= bs
->opaque
;
53 if (s
->sleep_in_drain_begin
) {
54 Coroutine
*co
= qemu_coroutine_create(sleep_in_drain_begin
, bs
);
55 bdrv_inc_in_flight(bs
);
56 aio_co_enter(bdrv_get_aio_context(bs
), co
);
60 static void bdrv_test_drain_end(BlockDriverState
*bs
)
62 BDRVTestState
*s
= bs
->opaque
;
66 static void bdrv_test_close(BlockDriverState
*bs
)
68 BDRVTestState
*s
= bs
->opaque
;
69 g_assert_cmpint(s
->drain_count
, >, 0);
72 static void co_reenter_bh(void *opaque
)
77 static int coroutine_fn
bdrv_test_co_preadv(BlockDriverState
*bs
,
78 int64_t offset
, int64_t bytes
,
80 BdrvRequestFlags flags
)
82 BDRVTestState
*s
= bs
->opaque
;
84 /* We want this request to stay until the polling loop in drain waits for
85 * it to complete. We need to sleep a while as bdrv_drain_invoke() comes
86 * first and polls its result, too, but it shouldn't accidentally complete
87 * this request yet. */
88 qemu_co_sleep_ns(QEMU_CLOCK_REALTIME
, 100000);
90 if (s
->bh_indirection_ctx
) {
91 aio_bh_schedule_oneshot(s
->bh_indirection_ctx
, co_reenter_bh
,
92 qemu_coroutine_self());
93 qemu_coroutine_yield();
99 static int bdrv_test_change_backing_file(BlockDriverState
*bs
,
100 const char *backing_file
,
101 const char *backing_fmt
)
106 static BlockDriver bdrv_test
= {
107 .format_name
= "test",
108 .instance_size
= sizeof(BDRVTestState
),
109 .supports_backing
= true,
111 .bdrv_close
= bdrv_test_close
,
112 .bdrv_co_preadv
= bdrv_test_co_preadv
,
114 .bdrv_drain_begin
= bdrv_test_drain_begin
,
115 .bdrv_drain_end
= bdrv_test_drain_end
,
117 .bdrv_child_perm
= bdrv_default_perms
,
119 .bdrv_change_backing_file
= bdrv_test_change_backing_file
,
122 static void aio_ret_cb(void *opaque
, int ret
)
124 int *aio_ret
= opaque
;
128 typedef struct CallInCoroutineData
{
131 } CallInCoroutineData
;
133 static coroutine_fn
void call_in_coroutine_entry(void *opaque
)
135 CallInCoroutineData
*data
= opaque
;
141 static void call_in_coroutine(void (*entry
)(void))
144 CallInCoroutineData data
= {
149 co
= qemu_coroutine_create(call_in_coroutine_entry
, &data
);
150 qemu_coroutine_enter(co
);
152 aio_poll(qemu_get_aio_context(), true);
162 static void do_drain_begin(enum drain_type drain_type
, BlockDriverState
*bs
)
164 switch (drain_type
) {
165 case BDRV_DRAIN_ALL
: bdrv_drain_all_begin(); break;
166 case BDRV_DRAIN
: bdrv_drained_begin(bs
); break;
167 default: g_assert_not_reached();
171 static void do_drain_end(enum drain_type drain_type
, BlockDriverState
*bs
)
173 switch (drain_type
) {
174 case BDRV_DRAIN_ALL
: bdrv_drain_all_end(); break;
175 case BDRV_DRAIN
: bdrv_drained_end(bs
); break;
176 default: g_assert_not_reached();
180 static void do_drain_begin_unlocked(enum drain_type drain_type
, BlockDriverState
*bs
)
182 if (drain_type
!= BDRV_DRAIN_ALL
) {
183 aio_context_acquire(bdrv_get_aio_context(bs
));
185 do_drain_begin(drain_type
, bs
);
186 if (drain_type
!= BDRV_DRAIN_ALL
) {
187 aio_context_release(bdrv_get_aio_context(bs
));
191 static BlockBackend
* no_coroutine_fn
test_setup(void)
194 BlockDriverState
*bs
, *backing
;
196 blk
= blk_new(qemu_get_aio_context(), BLK_PERM_ALL
, BLK_PERM_ALL
);
197 bs
= bdrv_new_open_driver(&bdrv_test
, "test-node", BDRV_O_RDWR
,
199 blk_insert_bs(blk
, bs
, &error_abort
);
201 backing
= bdrv_new_open_driver(&bdrv_test
, "backing", 0, &error_abort
);
202 bdrv_set_backing_hd(bs
, backing
, &error_abort
);
210 static void do_drain_end_unlocked(enum drain_type drain_type
, BlockDriverState
*bs
)
212 if (drain_type
!= BDRV_DRAIN_ALL
) {
213 aio_context_acquire(bdrv_get_aio_context(bs
));
215 do_drain_end(drain_type
, bs
);
216 if (drain_type
!= BDRV_DRAIN_ALL
) {
217 aio_context_release(bdrv_get_aio_context(bs
));
221 static void test_drv_cb_common(BlockBackend
*blk
, enum drain_type drain_type
,
224 BlockDriverState
*bs
= blk_bs(blk
);
225 BlockDriverState
*backing
= bs
->backing
->bs
;
226 BDRVTestState
*s
, *backing_s
;
230 QEMUIOVector qiov
= QEMU_IOVEC_INIT_BUF(qiov
, NULL
, 0);
233 backing_s
= backing
->opaque
;
235 /* Simple bdrv_drain_all_begin/end pair, check that CBs are called */
236 g_assert_cmpint(s
->drain_count
, ==, 0);
237 g_assert_cmpint(backing_s
->drain_count
, ==, 0);
239 do_drain_begin(drain_type
, bs
);
241 g_assert_cmpint(s
->drain_count
, ==, 1);
242 g_assert_cmpint(backing_s
->drain_count
, ==, !!recursive
);
244 do_drain_end(drain_type
, bs
);
246 g_assert_cmpint(s
->drain_count
, ==, 0);
247 g_assert_cmpint(backing_s
->drain_count
, ==, 0);
249 /* Now do the same while a request is pending */
250 aio_ret
= -EINPROGRESS
;
251 acb
= blk_aio_preadv(blk
, 0, &qiov
, 0, aio_ret_cb
, &aio_ret
);
252 g_assert(acb
!= NULL
);
253 g_assert_cmpint(aio_ret
, ==, -EINPROGRESS
);
255 g_assert_cmpint(s
->drain_count
, ==, 0);
256 g_assert_cmpint(backing_s
->drain_count
, ==, 0);
258 do_drain_begin(drain_type
, bs
);
260 g_assert_cmpint(aio_ret
, ==, 0);
261 g_assert_cmpint(s
->drain_count
, ==, 1);
262 g_assert_cmpint(backing_s
->drain_count
, ==, !!recursive
);
264 do_drain_end(drain_type
, bs
);
266 g_assert_cmpint(s
->drain_count
, ==, 0);
267 g_assert_cmpint(backing_s
->drain_count
, ==, 0);
270 static void test_drv_cb_drain_all(void)
272 BlockBackend
*blk
= test_setup();
273 test_drv_cb_common(blk
, BDRV_DRAIN_ALL
, true);
277 static void test_drv_cb_drain(void)
279 BlockBackend
*blk
= test_setup();
280 test_drv_cb_common(blk
, BDRV_DRAIN
, false);
284 static void coroutine_fn
test_drv_cb_co_drain_all_entry(void)
286 BlockBackend
*blk
= blk_all_next(NULL
);
287 test_drv_cb_common(blk
, BDRV_DRAIN_ALL
, true);
290 static void test_drv_cb_co_drain_all(void)
292 BlockBackend
*blk
= test_setup();
293 call_in_coroutine(test_drv_cb_co_drain_all_entry
);
297 static void coroutine_fn
test_drv_cb_co_drain_entry(void)
299 BlockBackend
*blk
= blk_all_next(NULL
);
300 test_drv_cb_common(blk
, BDRV_DRAIN
, false);
303 static void test_drv_cb_co_drain(void)
305 BlockBackend
*blk
= test_setup();
306 call_in_coroutine(test_drv_cb_co_drain_entry
);
310 static void test_quiesce_common(BlockBackend
*blk
, enum drain_type drain_type
,
313 BlockDriverState
*bs
= blk_bs(blk
);
314 BlockDriverState
*backing
= bs
->backing
->bs
;
316 g_assert_cmpint(bs
->quiesce_counter
, ==, 0);
317 g_assert_cmpint(backing
->quiesce_counter
, ==, 0);
319 do_drain_begin(drain_type
, bs
);
321 if (drain_type
== BDRV_DRAIN_ALL
) {
322 g_assert_cmpint(bs
->quiesce_counter
, ==, 2);
324 g_assert_cmpint(bs
->quiesce_counter
, ==, 1);
326 g_assert_cmpint(backing
->quiesce_counter
, ==, !!recursive
);
328 do_drain_end(drain_type
, bs
);
330 g_assert_cmpint(bs
->quiesce_counter
, ==, 0);
331 g_assert_cmpint(backing
->quiesce_counter
, ==, 0);
334 static void test_quiesce_drain_all(void)
336 BlockBackend
*blk
= test_setup();
337 test_quiesce_common(blk
, BDRV_DRAIN_ALL
, true);
341 static void test_quiesce_drain(void)
343 BlockBackend
*blk
= test_setup();
344 test_quiesce_common(blk
, BDRV_DRAIN
, false);
348 static void coroutine_fn
test_quiesce_co_drain_all_entry(void)
350 BlockBackend
*blk
= blk_all_next(NULL
);
351 test_quiesce_common(blk
, BDRV_DRAIN_ALL
, true);
354 static void test_quiesce_co_drain_all(void)
356 BlockBackend
*blk
= test_setup();
357 call_in_coroutine(test_quiesce_co_drain_all_entry
);
361 static void coroutine_fn
test_quiesce_co_drain_entry(void)
363 BlockBackend
*blk
= blk_all_next(NULL
);
364 test_quiesce_common(blk
, BDRV_DRAIN
, false);
367 static void test_quiesce_co_drain(void)
369 BlockBackend
*blk
= test_setup();
370 call_in_coroutine(test_quiesce_co_drain_entry
);
374 static void test_nested(void)
377 BlockDriverState
*bs
, *backing
;
378 BDRVTestState
*s
, *backing_s
;
379 enum drain_type outer
, inner
;
381 blk
= blk_new(qemu_get_aio_context(), BLK_PERM_ALL
, BLK_PERM_ALL
);
382 bs
= bdrv_new_open_driver(&bdrv_test
, "test-node", BDRV_O_RDWR
,
385 blk_insert_bs(blk
, bs
, &error_abort
);
387 backing
= bdrv_new_open_driver(&bdrv_test
, "backing", 0, &error_abort
);
388 backing_s
= backing
->opaque
;
389 bdrv_set_backing_hd(bs
, backing
, &error_abort
);
391 for (outer
= 0; outer
< DRAIN_TYPE_MAX
; outer
++) {
392 for (inner
= 0; inner
< DRAIN_TYPE_MAX
; inner
++) {
393 int backing_quiesce
= (outer
== BDRV_DRAIN_ALL
) +
394 (inner
== BDRV_DRAIN_ALL
);
396 g_assert_cmpint(bs
->quiesce_counter
, ==, 0);
397 g_assert_cmpint(backing
->quiesce_counter
, ==, 0);
398 g_assert_cmpint(s
->drain_count
, ==, 0);
399 g_assert_cmpint(backing_s
->drain_count
, ==, 0);
401 do_drain_begin(outer
, bs
);
402 do_drain_begin(inner
, bs
);
404 g_assert_cmpint(bs
->quiesce_counter
, ==, 2 + !!backing_quiesce
);
405 g_assert_cmpint(backing
->quiesce_counter
, ==, backing_quiesce
);
406 g_assert_cmpint(s
->drain_count
, ==, 1);
407 g_assert_cmpint(backing_s
->drain_count
, ==, !!backing_quiesce
);
409 do_drain_end(inner
, bs
);
410 do_drain_end(outer
, bs
);
412 g_assert_cmpint(bs
->quiesce_counter
, ==, 0);
413 g_assert_cmpint(backing
->quiesce_counter
, ==, 0);
414 g_assert_cmpint(s
->drain_count
, ==, 0);
415 g_assert_cmpint(backing_s
->drain_count
, ==, 0);
424 static void test_graph_change_drain_all(void)
426 BlockBackend
*blk_a
, *blk_b
;
427 BlockDriverState
*bs_a
, *bs_b
;
428 BDRVTestState
*a_s
, *b_s
;
430 /* Create node A with a BlockBackend */
431 blk_a
= blk_new(qemu_get_aio_context(), BLK_PERM_ALL
, BLK_PERM_ALL
);
432 bs_a
= bdrv_new_open_driver(&bdrv_test
, "test-node-a", BDRV_O_RDWR
,
435 blk_insert_bs(blk_a
, bs_a
, &error_abort
);
437 g_assert_cmpint(bs_a
->quiesce_counter
, ==, 0);
438 g_assert_cmpint(a_s
->drain_count
, ==, 0);
440 /* Call bdrv_drain_all_begin() */
441 bdrv_drain_all_begin();
443 g_assert_cmpint(bs_a
->quiesce_counter
, ==, 1);
444 g_assert_cmpint(a_s
->drain_count
, ==, 1);
446 /* Create node B with a BlockBackend */
447 blk_b
= blk_new(qemu_get_aio_context(), BLK_PERM_ALL
, BLK_PERM_ALL
);
448 bs_b
= bdrv_new_open_driver(&bdrv_test
, "test-node-b", BDRV_O_RDWR
,
451 blk_insert_bs(blk_b
, bs_b
, &error_abort
);
453 g_assert_cmpint(bs_a
->quiesce_counter
, ==, 1);
454 g_assert_cmpint(bs_b
->quiesce_counter
, ==, 1);
455 g_assert_cmpint(a_s
->drain_count
, ==, 1);
456 g_assert_cmpint(b_s
->drain_count
, ==, 1);
458 /* Unref and finally delete node A */
461 g_assert_cmpint(bs_a
->quiesce_counter
, ==, 1);
462 g_assert_cmpint(bs_b
->quiesce_counter
, ==, 1);
463 g_assert_cmpint(a_s
->drain_count
, ==, 1);
464 g_assert_cmpint(b_s
->drain_count
, ==, 1);
468 g_assert_cmpint(bs_b
->quiesce_counter
, ==, 1);
469 g_assert_cmpint(b_s
->drain_count
, ==, 1);
471 /* End the drained section */
472 bdrv_drain_all_end();
474 g_assert_cmpint(bs_b
->quiesce_counter
, ==, 0);
475 g_assert_cmpint(b_s
->drain_count
, ==, 0);
476 g_assert_cmpint(qemu_get_aio_context()->external_disable_cnt
, ==, 0);
482 struct test_iothread_data
{
483 BlockDriverState
*bs
;
484 enum drain_type drain_type
;
488 static void test_iothread_drain_entry(void *opaque
)
490 struct test_iothread_data
*data
= opaque
;
492 aio_context_acquire(bdrv_get_aio_context(data
->bs
));
493 do_drain_begin(data
->drain_type
, data
->bs
);
494 g_assert_cmpint(*data
->aio_ret
, ==, 0);
495 do_drain_end(data
->drain_type
, data
->bs
);
496 aio_context_release(bdrv_get_aio_context(data
->bs
));
498 qemu_event_set(&done_event
);
501 static void test_iothread_aio_cb(void *opaque
, int ret
)
503 int *aio_ret
= opaque
;
505 qemu_event_set(&done_event
);
508 static void test_iothread_main_thread_bh(void *opaque
)
510 struct test_iothread_data
*data
= opaque
;
512 /* Test that the AioContext is not yet locked in a random BH that is
513 * executed during drain, otherwise this would deadlock. */
514 aio_context_acquire(bdrv_get_aio_context(data
->bs
));
515 bdrv_flush(data
->bs
);
516 aio_context_release(bdrv_get_aio_context(data
->bs
));
520 * Starts an AIO request on a BDS that runs in the AioContext of iothread 1.
521 * The request involves a BH on iothread 2 before it can complete.
523 * @drain_thread = 0 means that do_drain_begin/end are called from the main
524 * thread, @drain_thread = 1 means that they are called from iothread 1. Drain
525 * for this BDS cannot be called from iothread 2 because only the main thread
526 * may do cross-AioContext polling.
528 static void test_iothread_common(enum drain_type drain_type
, int drain_thread
)
531 BlockDriverState
*bs
;
535 struct test_iothread_data data
;
537 IOThread
*a
= iothread_new();
538 IOThread
*b
= iothread_new();
539 AioContext
*ctx_a
= iothread_get_aio_context(a
);
540 AioContext
*ctx_b
= iothread_get_aio_context(b
);
542 QEMUIOVector qiov
= QEMU_IOVEC_INIT_BUF(qiov
, NULL
, 0);
544 /* bdrv_drain_all() may only be called from the main loop thread */
545 if (drain_type
== BDRV_DRAIN_ALL
&& drain_thread
!= 0) {
549 blk
= blk_new(qemu_get_aio_context(), BLK_PERM_ALL
, BLK_PERM_ALL
);
550 bs
= bdrv_new_open_driver(&bdrv_test
, "test-node", BDRV_O_RDWR
,
553 blk_insert_bs(blk
, bs
, &error_abort
);
554 blk_set_disable_request_queuing(blk
, true);
556 blk_set_aio_context(blk
, ctx_a
, &error_abort
);
557 aio_context_acquire(ctx_a
);
559 s
->bh_indirection_ctx
= ctx_b
;
561 aio_ret
= -EINPROGRESS
;
562 qemu_event_reset(&done_event
);
564 if (drain_thread
== 0) {
565 acb
= blk_aio_preadv(blk
, 0, &qiov
, 0, test_iothread_aio_cb
, &aio_ret
);
567 acb
= blk_aio_preadv(blk
, 0, &qiov
, 0, aio_ret_cb
, &aio_ret
);
569 g_assert(acb
!= NULL
);
570 g_assert_cmpint(aio_ret
, ==, -EINPROGRESS
);
572 aio_context_release(ctx_a
);
574 data
= (struct test_iothread_data
) {
576 .drain_type
= drain_type
,
580 switch (drain_thread
) {
582 if (drain_type
!= BDRV_DRAIN_ALL
) {
583 aio_context_acquire(ctx_a
);
586 aio_bh_schedule_oneshot(ctx_a
, test_iothread_main_thread_bh
, &data
);
588 /* The request is running on the IOThread a. Draining its block device
589 * will make sure that it has completed as far as the BDS is concerned,
590 * but the drain in this thread can continue immediately after
591 * bdrv_dec_in_flight() and aio_ret might be assigned only slightly
593 do_drain_begin(drain_type
, bs
);
594 g_assert_cmpint(bs
->in_flight
, ==, 0);
596 if (drain_type
!= BDRV_DRAIN_ALL
) {
597 aio_context_release(ctx_a
);
599 qemu_event_wait(&done_event
);
600 if (drain_type
!= BDRV_DRAIN_ALL
) {
601 aio_context_acquire(ctx_a
);
604 g_assert_cmpint(aio_ret
, ==, 0);
605 do_drain_end(drain_type
, bs
);
607 if (drain_type
!= BDRV_DRAIN_ALL
) {
608 aio_context_release(ctx_a
);
612 aio_bh_schedule_oneshot(ctx_a
, test_iothread_drain_entry
, &data
);
613 qemu_event_wait(&done_event
);
616 g_assert_not_reached();
619 aio_context_acquire(ctx_a
);
620 blk_set_aio_context(blk
, qemu_get_aio_context(), &error_abort
);
621 aio_context_release(ctx_a
);
631 static void test_iothread_drain_all(void)
633 test_iothread_common(BDRV_DRAIN_ALL
, 0);
634 test_iothread_common(BDRV_DRAIN_ALL
, 1);
637 static void test_iothread_drain(void)
639 test_iothread_common(BDRV_DRAIN
, 0);
640 test_iothread_common(BDRV_DRAIN
, 1);
644 typedef struct TestBlockJob
{
646 BlockDriverState
*bs
;
650 bool should_complete
;
653 static int test_job_prepare(Job
*job
)
655 TestBlockJob
*s
= container_of(job
, TestBlockJob
, common
.job
);
657 /* Provoke an AIO_WAIT_WHILE() call to verify there is no deadlock */
659 return s
->prepare_ret
;
662 static void test_job_commit(Job
*job
)
664 TestBlockJob
*s
= container_of(job
, TestBlockJob
, common
.job
);
666 /* Provoke an AIO_WAIT_WHILE() call to verify there is no deadlock */
670 static void test_job_abort(Job
*job
)
672 TestBlockJob
*s
= container_of(job
, TestBlockJob
, common
.job
);
674 /* Provoke an AIO_WAIT_WHILE() call to verify there is no deadlock */
678 static int coroutine_fn
test_job_run(Job
*job
, Error
**errp
)
680 TestBlockJob
*s
= container_of(job
, TestBlockJob
, common
.job
);
682 /* We are running the actual job code past the pause point in
686 job_transition_to_ready(&s
->common
.job
);
687 while (!s
->should_complete
) {
688 /* Avoid job_sleep_ns() because it marks the job as !busy. We want to
689 * emulate some actual activity (probably some I/O) here so that drain
690 * has to wait for this activity to stop. */
691 qemu_co_sleep_ns(QEMU_CLOCK_REALTIME
, 1000000);
693 job_pause_point(&s
->common
.job
);
699 static void test_job_complete(Job
*job
, Error
**errp
)
701 TestBlockJob
*s
= container_of(job
, TestBlockJob
, common
.job
);
702 s
->should_complete
= true;
705 BlockJobDriver test_job_driver
= {
707 .instance_size
= sizeof(TestBlockJob
),
708 .free
= block_job_free
,
709 .user_resume
= block_job_user_resume
,
711 .complete
= test_job_complete
,
712 .prepare
= test_job_prepare
,
713 .commit
= test_job_commit
,
714 .abort
= test_job_abort
,
718 enum test_job_result
{
721 TEST_JOB_FAIL_PREPARE
,
724 enum test_job_drain_node
{
726 TEST_JOB_DRAIN_SRC_CHILD
,
729 static void test_blockjob_common_drain_node(enum drain_type drain_type
,
731 enum test_job_result result
,
732 enum test_job_drain_node drain_node
)
734 BlockBackend
*blk_src
, *blk_target
;
735 BlockDriverState
*src
, *src_backing
, *src_overlay
, *target
, *drain_bs
;
738 IOThread
*iothread
= NULL
;
742 src
= bdrv_new_open_driver(&bdrv_test
, "source", BDRV_O_RDWR
,
744 src_backing
= bdrv_new_open_driver(&bdrv_test
, "source-backing",
745 BDRV_O_RDWR
, &error_abort
);
746 src_overlay
= bdrv_new_open_driver(&bdrv_test
, "source-overlay",
747 BDRV_O_RDWR
, &error_abort
);
749 bdrv_set_backing_hd(src_overlay
, src
, &error_abort
);
751 bdrv_set_backing_hd(src
, src_backing
, &error_abort
);
752 bdrv_unref(src_backing
);
754 blk_src
= blk_new(qemu_get_aio_context(), BLK_PERM_ALL
, BLK_PERM_ALL
);
755 blk_insert_bs(blk_src
, src_overlay
, &error_abort
);
757 switch (drain_node
) {
758 case TEST_JOB_DRAIN_SRC
:
761 case TEST_JOB_DRAIN_SRC_CHILD
:
762 drain_bs
= src_backing
;
765 g_assert_not_reached();
769 iothread
= iothread_new();
770 ctx
= iothread_get_aio_context(iothread
);
771 blk_set_aio_context(blk_src
, ctx
, &error_abort
);
773 ctx
= qemu_get_aio_context();
776 target
= bdrv_new_open_driver(&bdrv_test
, "target", BDRV_O_RDWR
,
778 blk_target
= blk_new(qemu_get_aio_context(), BLK_PERM_ALL
, BLK_PERM_ALL
);
779 blk_insert_bs(blk_target
, target
, &error_abort
);
780 blk_set_allow_aio_context_change(blk_target
, true);
782 aio_context_acquire(ctx
);
783 tjob
= block_job_create("job0", &test_job_driver
, NULL
, src
,
785 0, 0, NULL
, NULL
, &error_abort
);
788 block_job_add_bdrv(job
, "target", target
, 0, BLK_PERM_ALL
, &error_abort
);
791 case TEST_JOB_SUCCESS
:
793 case TEST_JOB_FAIL_RUN
:
794 tjob
->run_ret
= -EIO
;
796 case TEST_JOB_FAIL_PREPARE
:
797 tjob
->prepare_ret
= -EIO
;
800 aio_context_release(ctx
);
802 job_start(&job
->job
);
805 /* job_co_entry() is run in the I/O thread, wait for the actual job
806 * code to start (we don't want to catch the job in the pause point in
808 while (!tjob
->running
) {
809 aio_poll(qemu_get_aio_context(), false);
813 WITH_JOB_LOCK_GUARD() {
814 g_assert_cmpint(job
->job
.pause_count
, ==, 0);
815 g_assert_false(job
->job
.paused
);
816 g_assert_true(tjob
->running
);
817 g_assert_true(job
->job
.busy
); /* We're in qemu_co_sleep_ns() */
820 do_drain_begin_unlocked(drain_type
, drain_bs
);
822 WITH_JOB_LOCK_GUARD() {
823 if (drain_type
== BDRV_DRAIN_ALL
) {
824 /* bdrv_drain_all() drains both src and target */
825 g_assert_cmpint(job
->job
.pause_count
, ==, 2);
827 g_assert_cmpint(job
->job
.pause_count
, ==, 1);
829 g_assert_true(job
->job
.paused
);
830 g_assert_false(job
->job
.busy
); /* The job is paused */
833 do_drain_end_unlocked(drain_type
, drain_bs
);
837 * Here we are waiting for the paused status to change,
838 * so don't bother protecting the read every time.
840 * paused is reset in the I/O thread, wait for it
842 while (job
->job
.paused
) {
843 aio_poll(qemu_get_aio_context(), false);
847 WITH_JOB_LOCK_GUARD() {
848 g_assert_cmpint(job
->job
.pause_count
, ==, 0);
849 g_assert_false(job
->job
.paused
);
850 g_assert_true(job
->job
.busy
); /* We're in qemu_co_sleep_ns() */
853 do_drain_begin_unlocked(drain_type
, target
);
855 WITH_JOB_LOCK_GUARD() {
856 if (drain_type
== BDRV_DRAIN_ALL
) {
857 /* bdrv_drain_all() drains both src and target */
858 g_assert_cmpint(job
->job
.pause_count
, ==, 2);
860 g_assert_cmpint(job
->job
.pause_count
, ==, 1);
862 g_assert_true(job
->job
.paused
);
863 g_assert_false(job
->job
.busy
); /* The job is paused */
866 do_drain_end_unlocked(drain_type
, target
);
870 * Here we are waiting for the paused status to change,
871 * so don't bother protecting the read every time.
873 * paused is reset in the I/O thread, wait for it
875 while (job
->job
.paused
) {
876 aio_poll(qemu_get_aio_context(), false);
880 WITH_JOB_LOCK_GUARD() {
881 g_assert_cmpint(job
->job
.pause_count
, ==, 0);
882 g_assert_false(job
->job
.paused
);
883 g_assert_true(job
->job
.busy
); /* We're in qemu_co_sleep_ns() */
886 WITH_JOB_LOCK_GUARD() {
887 ret
= job_complete_sync_locked(&job
->job
, &error_abort
);
889 g_assert_cmpint(ret
, ==, (result
== TEST_JOB_SUCCESS
? 0 : -EIO
));
891 aio_context_acquire(ctx
);
893 blk_set_aio_context(blk_src
, qemu_get_aio_context(), &error_abort
);
894 assert(blk_get_aio_context(blk_target
) == qemu_get_aio_context());
896 aio_context_release(ctx
);
899 blk_unref(blk_target
);
900 bdrv_unref(src_overlay
);
904 iothread_join(iothread
);
908 static void test_blockjob_common(enum drain_type drain_type
, bool use_iothread
,
909 enum test_job_result result
)
911 test_blockjob_common_drain_node(drain_type
, use_iothread
, result
,
913 test_blockjob_common_drain_node(drain_type
, use_iothread
, result
,
914 TEST_JOB_DRAIN_SRC_CHILD
);
917 static void test_blockjob_drain_all(void)
919 test_blockjob_common(BDRV_DRAIN_ALL
, false, TEST_JOB_SUCCESS
);
922 static void test_blockjob_drain(void)
924 test_blockjob_common(BDRV_DRAIN
, false, TEST_JOB_SUCCESS
);
927 static void test_blockjob_error_drain_all(void)
929 test_blockjob_common(BDRV_DRAIN_ALL
, false, TEST_JOB_FAIL_RUN
);
930 test_blockjob_common(BDRV_DRAIN_ALL
, false, TEST_JOB_FAIL_PREPARE
);
933 static void test_blockjob_error_drain(void)
935 test_blockjob_common(BDRV_DRAIN
, false, TEST_JOB_FAIL_RUN
);
936 test_blockjob_common(BDRV_DRAIN
, false, TEST_JOB_FAIL_PREPARE
);
939 static void test_blockjob_iothread_drain_all(void)
941 test_blockjob_common(BDRV_DRAIN_ALL
, true, TEST_JOB_SUCCESS
);
944 static void test_blockjob_iothread_drain(void)
946 test_blockjob_common(BDRV_DRAIN
, true, TEST_JOB_SUCCESS
);
949 static void test_blockjob_iothread_error_drain_all(void)
951 test_blockjob_common(BDRV_DRAIN_ALL
, true, TEST_JOB_FAIL_RUN
);
952 test_blockjob_common(BDRV_DRAIN_ALL
, true, TEST_JOB_FAIL_PREPARE
);
955 static void test_blockjob_iothread_error_drain(void)
957 test_blockjob_common(BDRV_DRAIN
, true, TEST_JOB_FAIL_RUN
);
958 test_blockjob_common(BDRV_DRAIN
, true, TEST_JOB_FAIL_PREPARE
);
962 typedef struct BDRVTestTopState
{
963 BdrvChild
*wait_child
;
966 static void bdrv_test_top_close(BlockDriverState
*bs
)
968 BdrvChild
*c
, *next_c
;
969 QLIST_FOREACH_SAFE(c
, &bs
->children
, next
, next_c
) {
970 bdrv_unref_child(bs
, c
);
974 static int coroutine_fn GRAPH_RDLOCK
975 bdrv_test_top_co_preadv(BlockDriverState
*bs
, int64_t offset
, int64_t bytes
,
976 QEMUIOVector
*qiov
, BdrvRequestFlags flags
)
978 BDRVTestTopState
*tts
= bs
->opaque
;
979 return bdrv_co_preadv(tts
->wait_child
, offset
, bytes
, qiov
, flags
);
982 static BlockDriver bdrv_test_top_driver
= {
983 .format_name
= "test_top_driver",
984 .instance_size
= sizeof(BDRVTestTopState
),
986 .bdrv_close
= bdrv_test_top_close
,
987 .bdrv_co_preadv
= bdrv_test_top_co_preadv
,
989 .bdrv_child_perm
= bdrv_default_perms
,
992 typedef struct TestCoDeleteByDrainData
{
994 bool detach_instead_of_delete
;
996 } TestCoDeleteByDrainData
;
998 static void coroutine_fn
test_co_delete_by_drain(void *opaque
)
1000 TestCoDeleteByDrainData
*dbdd
= opaque
;
1001 BlockBackend
*blk
= dbdd
->blk
;
1002 BlockDriverState
*bs
= blk_bs(blk
);
1003 BDRVTestTopState
*tts
= bs
->opaque
;
1004 void *buffer
= g_malloc(65536);
1005 QEMUIOVector qiov
= QEMU_IOVEC_INIT_BUF(qiov
, buffer
, 65536);
1007 /* Pretend some internal write operation from parent to child.
1008 * Important: We have to read from the child, not from the parent!
1009 * Draining works by first propagating it all up the tree to the
1010 * root and then waiting for drainage from root to the leaves
1011 * (protocol nodes). If we have a request waiting on the root,
1012 * everything will be drained before we go back down the tree, but
1013 * we do not want that. We want to be in the middle of draining
1014 * when this following requests returns. */
1015 bdrv_graph_co_rdlock();
1016 bdrv_co_preadv(tts
->wait_child
, 0, 65536, &qiov
, 0);
1017 bdrv_graph_co_rdunlock();
1019 g_assert_cmpint(bs
->refcnt
, ==, 1);
1021 if (!dbdd
->detach_instead_of_delete
) {
1024 BdrvChild
*c
, *next_c
;
1025 QLIST_FOREACH_SAFE(c
, &bs
->children
, next
, next_c
) {
1026 bdrv_unref_child(bs
, c
);
1035 * Test what happens when some BDS has some children, you drain one of
1036 * them and this results in the BDS being deleted.
1038 * If @detach_instead_of_delete is set, the BDS is not going to be
1039 * deleted but will only detach all of its children.
1041 static void do_test_delete_by_drain(bool detach_instead_of_delete
,
1042 enum drain_type drain_type
)
1045 BlockDriverState
*bs
, *child_bs
, *null_bs
;
1046 BDRVTestTopState
*tts
;
1047 TestCoDeleteByDrainData dbdd
;
1050 bs
= bdrv_new_open_driver(&bdrv_test_top_driver
, "top", BDRV_O_RDWR
,
1052 bs
->total_sectors
= 65536 >> BDRV_SECTOR_BITS
;
1055 null_bs
= bdrv_open("null-co://", NULL
, NULL
, BDRV_O_RDWR
| BDRV_O_PROTOCOL
,
1057 bdrv_attach_child(bs
, null_bs
, "null-child", &child_of_bds
,
1058 BDRV_CHILD_DATA
, &error_abort
);
1060 /* This child will be the one to pass to requests through to, and
1061 * it will stall until a drain occurs */
1062 child_bs
= bdrv_new_open_driver(&bdrv_test
, "child", BDRV_O_RDWR
,
1064 child_bs
->total_sectors
= 65536 >> BDRV_SECTOR_BITS
;
1065 /* Takes our reference to child_bs */
1066 tts
->wait_child
= bdrv_attach_child(bs
, child_bs
, "wait-child",
1068 BDRV_CHILD_DATA
| BDRV_CHILD_PRIMARY
,
1071 /* This child is just there to be deleted
1072 * (for detach_instead_of_delete == true) */
1073 null_bs
= bdrv_open("null-co://", NULL
, NULL
, BDRV_O_RDWR
| BDRV_O_PROTOCOL
,
1075 bdrv_attach_child(bs
, null_bs
, "null-child", &child_of_bds
, BDRV_CHILD_DATA
,
1078 blk
= blk_new(qemu_get_aio_context(), BLK_PERM_ALL
, BLK_PERM_ALL
);
1079 blk_insert_bs(blk
, bs
, &error_abort
);
1081 /* Referenced by blk now */
1084 g_assert_cmpint(bs
->refcnt
, ==, 1);
1085 g_assert_cmpint(child_bs
->refcnt
, ==, 1);
1086 g_assert_cmpint(null_bs
->refcnt
, ==, 1);
1089 dbdd
= (TestCoDeleteByDrainData
){
1091 .detach_instead_of_delete
= detach_instead_of_delete
,
1094 co
= qemu_coroutine_create(test_co_delete_by_drain
, &dbdd
);
1095 qemu_coroutine_enter(co
);
1097 /* Drain the child while the read operation is still pending.
1098 * This should result in the operation finishing and
1099 * test_co_delete_by_drain() resuming. Thus, @bs will be deleted
1100 * and the coroutine will exit while this drain operation is still
1102 switch (drain_type
) {
1105 bdrv_drain(child_bs
);
1106 bdrv_unref(child_bs
);
1108 case BDRV_DRAIN_ALL
:
1109 bdrv_drain_all_begin();
1110 bdrv_drain_all_end();
1113 g_assert_not_reached();
1116 while (!dbdd
.done
) {
1117 aio_poll(qemu_get_aio_context(), true);
1120 if (detach_instead_of_delete
) {
1121 /* Here, the reference has not passed over to the coroutine,
1122 * so we have to delete the BB ourselves */
1127 static void test_delete_by_drain(void)
1129 do_test_delete_by_drain(false, BDRV_DRAIN
);
1132 static void test_detach_by_drain_all(void)
1134 do_test_delete_by_drain(true, BDRV_DRAIN_ALL
);
1137 static void test_detach_by_drain(void)
1139 do_test_delete_by_drain(true, BDRV_DRAIN
);
1143 struct detach_by_parent_data
{
1144 BlockDriverState
*parent_b
;
1146 BlockDriverState
*c
;
1149 bool detach_on_drain
;
1151 static struct detach_by_parent_data detach_by_parent_data
;
1153 static void detach_indirect_bh(void *opaque
)
1155 struct detach_by_parent_data
*data
= opaque
;
1157 bdrv_dec_in_flight(data
->child_b
->bs
);
1158 bdrv_unref_child(data
->parent_b
, data
->child_b
);
1161 data
->child_c
= bdrv_attach_child(data
->parent_b
, data
->c
, "PB-C",
1162 &child_of_bds
, BDRV_CHILD_DATA
,
1166 static void detach_by_parent_aio_cb(void *opaque
, int ret
)
1168 struct detach_by_parent_data
*data
= &detach_by_parent_data
;
1170 g_assert_cmpint(ret
, ==, 0);
1171 if (data
->by_parent_cb
) {
1172 bdrv_inc_in_flight(data
->child_b
->bs
);
1173 detach_indirect_bh(data
);
1177 static void detach_by_driver_cb_drained_begin(BdrvChild
*child
)
1179 struct detach_by_parent_data
*data
= &detach_by_parent_data
;
1181 if (!data
->detach_on_drain
) {
1184 data
->detach_on_drain
= false;
1186 bdrv_inc_in_flight(data
->child_b
->bs
);
1187 aio_bh_schedule_oneshot(qemu_get_current_aio_context(),
1188 detach_indirect_bh
, &detach_by_parent_data
);
1189 child_of_bds
.drained_begin(child
);
1192 static BdrvChildClass detach_by_driver_cb_class
;
1201 * by_parent_cb == true: Test that parent callbacks don't poll
1203 * PA has a pending write request whose callback changes the child nodes of
1204 * PB: It removes B and adds C instead. The subtree of PB is drained, which
1205 * will indirectly drain the write request, too.
1207 * by_parent_cb == false: Test that bdrv_drain_invoke() doesn't poll
1209 * PA's BdrvChildClass has a .drained_begin callback that schedules a BH
1210 * that does the same graph change. If bdrv_drain_invoke() calls it, the
1211 * state is messed up, but if it is only polled in the single
1212 * BDRV_POLL_WHILE() at the end of the drain, this should work fine.
1214 static void test_detach_indirect(bool by_parent_cb
)
1217 BlockDriverState
*parent_a
, *parent_b
, *a
, *b
, *c
;
1218 BdrvChild
*child_a
, *child_b
;
1221 QEMUIOVector qiov
= QEMU_IOVEC_INIT_BUF(qiov
, NULL
, 0);
1223 if (!by_parent_cb
) {
1224 detach_by_driver_cb_class
= child_of_bds
;
1225 detach_by_driver_cb_class
.drained_begin
=
1226 detach_by_driver_cb_drained_begin
;
1227 detach_by_driver_cb_class
.drained_end
= NULL
;
1228 detach_by_driver_cb_class
.drained_poll
= NULL
;
1231 detach_by_parent_data
= (struct detach_by_parent_data
) {
1232 .detach_on_drain
= false,
1235 /* Create all involved nodes */
1236 parent_a
= bdrv_new_open_driver(&bdrv_test
, "parent-a", BDRV_O_RDWR
,
1238 parent_b
= bdrv_new_open_driver(&bdrv_test
, "parent-b", 0,
1241 a
= bdrv_new_open_driver(&bdrv_test
, "a", BDRV_O_RDWR
, &error_abort
);
1242 b
= bdrv_new_open_driver(&bdrv_test
, "b", BDRV_O_RDWR
, &error_abort
);
1243 c
= bdrv_new_open_driver(&bdrv_test
, "c", BDRV_O_RDWR
, &error_abort
);
1245 /* blk is a BB for parent-a */
1246 blk
= blk_new(qemu_get_aio_context(), BLK_PERM_ALL
, BLK_PERM_ALL
);
1247 blk_insert_bs(blk
, parent_a
, &error_abort
);
1248 bdrv_unref(parent_a
);
1250 /* If we want to get bdrv_drain_invoke() to call aio_poll(), the driver
1251 * callback must not return immediately. */
1252 if (!by_parent_cb
) {
1253 BDRVTestState
*s
= parent_a
->opaque
;
1254 s
->sleep_in_drain_begin
= true;
1257 /* Set child relationships */
1260 child_b
= bdrv_attach_child(parent_b
, b
, "PB-B", &child_of_bds
,
1261 BDRV_CHILD_DATA
, &error_abort
);
1262 child_a
= bdrv_attach_child(parent_b
, a
, "PB-A", &child_of_bds
,
1263 BDRV_CHILD_COW
, &error_abort
);
1266 bdrv_attach_child(parent_a
, a
, "PA-A",
1267 by_parent_cb
? &child_of_bds
: &detach_by_driver_cb_class
,
1268 BDRV_CHILD_DATA
, &error_abort
);
1270 g_assert_cmpint(parent_a
->refcnt
, ==, 1);
1271 g_assert_cmpint(parent_b
->refcnt
, ==, 1);
1272 g_assert_cmpint(a
->refcnt
, ==, 3);
1273 g_assert_cmpint(b
->refcnt
, ==, 2);
1274 g_assert_cmpint(c
->refcnt
, ==, 1);
1276 g_assert(QLIST_FIRST(&parent_b
->children
) == child_a
);
1277 g_assert(QLIST_NEXT(child_a
, next
) == child_b
);
1278 g_assert(QLIST_NEXT(child_b
, next
) == NULL
);
1280 /* Start the evil write request */
1281 detach_by_parent_data
= (struct detach_by_parent_data
) {
1282 .parent_b
= parent_b
,
1285 .by_parent_cb
= by_parent_cb
,
1286 .detach_on_drain
= true,
1288 acb
= blk_aio_preadv(blk
, 0, &qiov
, 0, detach_by_parent_aio_cb
, NULL
);
1289 g_assert(acb
!= NULL
);
1291 /* Drain and check the expected result */
1292 bdrv_drained_begin(parent_b
);
1293 bdrv_drained_begin(a
);
1294 bdrv_drained_begin(b
);
1295 bdrv_drained_begin(c
);
1297 g_assert(detach_by_parent_data
.child_c
!= NULL
);
1299 g_assert_cmpint(parent_a
->refcnt
, ==, 1);
1300 g_assert_cmpint(parent_b
->refcnt
, ==, 1);
1301 g_assert_cmpint(a
->refcnt
, ==, 3);
1302 g_assert_cmpint(b
->refcnt
, ==, 1);
1303 g_assert_cmpint(c
->refcnt
, ==, 2);
1305 g_assert(QLIST_FIRST(&parent_b
->children
) == detach_by_parent_data
.child_c
);
1306 g_assert(QLIST_NEXT(detach_by_parent_data
.child_c
, next
) == child_a
);
1307 g_assert(QLIST_NEXT(child_a
, next
) == NULL
);
1309 g_assert_cmpint(parent_a
->quiesce_counter
, ==, 1);
1310 g_assert_cmpint(parent_b
->quiesce_counter
, ==, 3);
1311 g_assert_cmpint(a
->quiesce_counter
, ==, 1);
1312 g_assert_cmpint(b
->quiesce_counter
, ==, 1);
1313 g_assert_cmpint(c
->quiesce_counter
, ==, 1);
1315 bdrv_drained_end(parent_b
);
1316 bdrv_drained_end(a
);
1317 bdrv_drained_end(b
);
1318 bdrv_drained_end(c
);
1320 bdrv_unref(parent_b
);
1323 g_assert_cmpint(a
->refcnt
, ==, 1);
1324 g_assert_cmpint(b
->refcnt
, ==, 1);
1325 g_assert_cmpint(c
->refcnt
, ==, 1);
1331 static void test_detach_by_parent_cb(void)
1333 test_detach_indirect(true);
1336 static void test_detach_by_driver_cb(void)
1338 test_detach_indirect(false);
1341 static void test_append_to_drained(void)
1344 BlockDriverState
*base
, *overlay
;
1345 BDRVTestState
*base_s
, *overlay_s
;
1347 blk
= blk_new(qemu_get_aio_context(), BLK_PERM_ALL
, BLK_PERM_ALL
);
1348 base
= bdrv_new_open_driver(&bdrv_test
, "base", BDRV_O_RDWR
, &error_abort
);
1349 base_s
= base
->opaque
;
1350 blk_insert_bs(blk
, base
, &error_abort
);
1352 overlay
= bdrv_new_open_driver(&bdrv_test
, "overlay", BDRV_O_RDWR
,
1354 overlay_s
= overlay
->opaque
;
1356 do_drain_begin(BDRV_DRAIN
, base
);
1357 g_assert_cmpint(base
->quiesce_counter
, ==, 1);
1358 g_assert_cmpint(base_s
->drain_count
, ==, 1);
1359 g_assert_cmpint(base
->in_flight
, ==, 0);
1361 bdrv_append(overlay
, base
, &error_abort
);
1362 g_assert_cmpint(base
->in_flight
, ==, 0);
1363 g_assert_cmpint(overlay
->in_flight
, ==, 0);
1365 g_assert_cmpint(base
->quiesce_counter
, ==, 1);
1366 g_assert_cmpint(base_s
->drain_count
, ==, 1);
1367 g_assert_cmpint(overlay
->quiesce_counter
, ==, 1);
1368 g_assert_cmpint(overlay_s
->drain_count
, ==, 1);
1370 do_drain_end(BDRV_DRAIN
, base
);
1372 g_assert_cmpint(base
->quiesce_counter
, ==, 0);
1373 g_assert_cmpint(base_s
->drain_count
, ==, 0);
1374 g_assert_cmpint(overlay
->quiesce_counter
, ==, 0);
1375 g_assert_cmpint(overlay_s
->drain_count
, ==, 0);
1377 bdrv_unref(overlay
);
1382 static void test_set_aio_context(void)
1384 BlockDriverState
*bs
;
1385 IOThread
*a
= iothread_new();
1386 IOThread
*b
= iothread_new();
1387 AioContext
*ctx_a
= iothread_get_aio_context(a
);
1388 AioContext
*ctx_b
= iothread_get_aio_context(b
);
1390 bs
= bdrv_new_open_driver(&bdrv_test
, "test-node", BDRV_O_RDWR
,
1393 bdrv_drained_begin(bs
);
1394 bdrv_try_change_aio_context(bs
, ctx_a
, NULL
, &error_abort
);
1396 aio_context_acquire(ctx_a
);
1397 bdrv_drained_end(bs
);
1399 bdrv_drained_begin(bs
);
1400 bdrv_try_change_aio_context(bs
, ctx_b
, NULL
, &error_abort
);
1401 aio_context_release(ctx_a
);
1402 aio_context_acquire(ctx_b
);
1403 bdrv_try_change_aio_context(bs
, qemu_get_aio_context(), NULL
, &error_abort
);
1404 aio_context_release(ctx_b
);
1405 bdrv_drained_end(bs
);
1413 typedef struct TestDropBackingBlockJob
{
1415 bool should_complete
;
1417 BlockDriverState
*detach_also
;
1418 BlockDriverState
*bs
;
1419 } TestDropBackingBlockJob
;
1421 static int coroutine_fn
test_drop_backing_job_run(Job
*job
, Error
**errp
)
1423 TestDropBackingBlockJob
*s
=
1424 container_of(job
, TestDropBackingBlockJob
, common
.job
);
1426 while (!s
->should_complete
) {
1427 job_sleep_ns(job
, 0);
1433 static void test_drop_backing_job_commit(Job
*job
)
1435 TestDropBackingBlockJob
*s
=
1436 container_of(job
, TestDropBackingBlockJob
, common
.job
);
1438 bdrv_set_backing_hd(s
->bs
, NULL
, &error_abort
);
1439 bdrv_set_backing_hd(s
->detach_also
, NULL
, &error_abort
);
1441 *s
->did_complete
= true;
1444 static const BlockJobDriver test_drop_backing_job_driver
= {
1446 .instance_size
= sizeof(TestDropBackingBlockJob
),
1447 .free
= block_job_free
,
1448 .user_resume
= block_job_user_resume
,
1449 .run
= test_drop_backing_job_run
,
1450 .commit
= test_drop_backing_job_commit
,
1455 * Creates a child node with three parent nodes on it, and then runs a
1456 * block job on the final one, parent-node-2.
1458 * The job is then asked to complete before a section where the child
1461 * Ending this section will undrain the child's parents, first
1462 * parent-node-2, then parent-node-1, then parent-node-0 -- the parent
1463 * list is in reverse order of how they were added. Ending the drain
1464 * on parent-node-2 will resume the job, thus completing it and
1465 * scheduling job_exit().
1467 * Ending the drain on parent-node-1 will poll the AioContext, which
1468 * lets job_exit() and thus test_drop_backing_job_commit() run. That
1469 * function first removes the child as parent-node-2's backing file.
1471 * In old (and buggy) implementations, there are two problems with
1473 * (A) bdrv_drain_invoke() polls for every node that leaves the
1474 * drained section. This means that job_exit() is scheduled
1475 * before the child has left the drained section. Its
1476 * quiesce_counter is therefore still 1 when it is removed from
1479 * (B) bdrv_replace_child_noperm() calls drained_end() on the old
1480 * child's parents as many times as the child is quiesced. This
1481 * means it will call drained_end() on parent-node-2 once.
1482 * Because parent-node-2 is no longer quiesced at this point, this
1485 * bdrv_replace_child_noperm() therefore must call drained_end() on
1486 * the parent only if it really is still drained because the child is
1489 * If removing child from parent-node-2 was successful (as it should
1490 * be), test_drop_backing_job_commit() will then also remove the child
1491 * from parent-node-0.
1493 * With an old version of our drain infrastructure ((A) above), that
1494 * resulted in the following flow:
1496 * 1. child attempts to leave its drained section. The call recurses
1499 * 2. parent-node-2 leaves the drained section. Polling in
1500 * bdrv_drain_invoke() will schedule job_exit().
1502 * 3. parent-node-1 leaves the drained section. Polling in
1503 * bdrv_drain_invoke() will run job_exit(), thus disconnecting
1504 * parent-node-0 from the child node.
1506 * 4. bdrv_parent_drained_end() uses a QLIST_FOREACH_SAFE() loop to
1507 * iterate over the parents. Thus, it now accesses the BdrvChild
1508 * object that used to connect parent-node-0 and the child node.
1509 * However, that object no longer exists, so it accesses a dangling
1512 * The solution is to only poll once when running a bdrv_drained_end()
1513 * operation, specifically at the end when all drained_end()
1514 * operations for all involved nodes have been scheduled.
1515 * Note that this also solves (A) above, thus hiding (B).
1517 static void test_blockjob_commit_by_drained_end(void)
1519 BlockDriverState
*bs_child
, *bs_parents
[3];
1520 TestDropBackingBlockJob
*job
;
1521 bool job_has_completed
= false;
1524 bs_child
= bdrv_new_open_driver(&bdrv_test
, "child-node", BDRV_O_RDWR
,
1527 for (i
= 0; i
< 3; i
++) {
1529 snprintf(name
, sizeof(name
), "parent-node-%i", i
);
1530 bs_parents
[i
] = bdrv_new_open_driver(&bdrv_test
, name
, BDRV_O_RDWR
,
1532 bdrv_set_backing_hd(bs_parents
[i
], bs_child
, &error_abort
);
1535 job
= block_job_create("job", &test_drop_backing_job_driver
, NULL
,
1536 bs_parents
[2], 0, BLK_PERM_ALL
, 0, 0, NULL
, NULL
,
1538 job
->bs
= bs_parents
[2];
1540 job
->detach_also
= bs_parents
[0];
1541 job
->did_complete
= &job_has_completed
;
1543 job_start(&job
->common
.job
);
1545 job
->should_complete
= true;
1546 bdrv_drained_begin(bs_child
);
1547 g_assert(!job_has_completed
);
1548 bdrv_drained_end(bs_child
);
1549 aio_poll(qemu_get_aio_context(), false);
1550 g_assert(job_has_completed
);
1552 bdrv_unref(bs_parents
[0]);
1553 bdrv_unref(bs_parents
[1]);
1554 bdrv_unref(bs_parents
[2]);
1555 bdrv_unref(bs_child
);
1559 typedef struct TestSimpleBlockJob
{
1561 bool should_complete
;
1563 } TestSimpleBlockJob
;
1565 static int coroutine_fn
test_simple_job_run(Job
*job
, Error
**errp
)
1567 TestSimpleBlockJob
*s
= container_of(job
, TestSimpleBlockJob
, common
.job
);
1569 while (!s
->should_complete
) {
1570 job_sleep_ns(job
, 0);
1576 static void test_simple_job_clean(Job
*job
)
1578 TestSimpleBlockJob
*s
= container_of(job
, TestSimpleBlockJob
, common
.job
);
1579 *s
->did_complete
= true;
1582 static const BlockJobDriver test_simple_job_driver
= {
1584 .instance_size
= sizeof(TestSimpleBlockJob
),
1585 .free
= block_job_free
,
1586 .user_resume
= block_job_user_resume
,
1587 .run
= test_simple_job_run
,
1588 .clean
= test_simple_job_clean
,
1592 static int drop_intermediate_poll_update_filename(BdrvChild
*child
,
1593 BlockDriverState
*new_base
,
1594 const char *filename
,
1598 * We are free to poll here, which may change the block graph, if
1599 * it is not drained.
1602 /* If the job is not drained: Complete it, schedule job_exit() */
1603 aio_poll(qemu_get_current_aio_context(), false);
1604 /* If the job is not drained: Run job_exit(), finish the job */
1605 aio_poll(qemu_get_current_aio_context(), false);
1611 * Test a poll in the midst of bdrv_drop_intermediate().
1613 * bdrv_drop_intermediate() calls BdrvChildClass.update_filename(),
1614 * which can yield or poll. This may lead to graph changes, unless
1615 * the whole subtree in question is drained.
1617 * We test this on the following graph:
1633 * node-2 --chain--> node-1 --chain--> node-0
1635 * We drop node-1 with bdrv_drop_intermediate(top=node-1, base=node-0).
1637 * This first updates node-2's backing filename by invoking
1638 * drop_intermediate_poll_update_filename(), which polls twice. This
1639 * causes the job to finish, which in turns causes the job-node to be
1642 * bdrv_drop_intermediate() uses a QLIST_FOREACH_SAFE() loop, so it
1643 * already has a pointer to the BdrvChild edge between job-node and
1644 * node-1. When it tries to handle that edge, we probably get a
1645 * segmentation fault because the object no longer exists.
1648 * The solution is for bdrv_drop_intermediate() to drain top's
1649 * subtree. This prevents graph changes from happening just because
1650 * BdrvChildClass.update_filename() yields or polls. Thus, the block
1651 * job is paused during that drained section and must finish before or
1654 * (In addition, bdrv_replace_child() must keep the job paused.)
1656 static void test_drop_intermediate_poll(void)
1658 static BdrvChildClass chain_child_class
;
1659 BlockDriverState
*chain
[3];
1660 TestSimpleBlockJob
*job
;
1661 BlockDriverState
*job_node
;
1662 bool job_has_completed
= false;
1666 chain_child_class
= child_of_bds
;
1667 chain_child_class
.update_filename
= drop_intermediate_poll_update_filename
;
1669 for (i
= 0; i
< 3; i
++) {
1671 snprintf(name
, 32, "node-%i", i
);
1673 chain
[i
] = bdrv_new_open_driver(&bdrv_test
, name
, 0, &error_abort
);
1676 job_node
= bdrv_new_open_driver(&bdrv_test
, "job-node", BDRV_O_RDWR
,
1678 bdrv_set_backing_hd(job_node
, chain
[1], &error_abort
);
1681 * Establish the chain last, so the chain links are the first
1682 * elements in the BDS.parents lists
1684 for (i
= 0; i
< 3; i
++) {
1686 /* Takes the reference to chain[i - 1] */
1687 bdrv_attach_child(chain
[i
], chain
[i
- 1], "chain",
1688 &chain_child_class
, BDRV_CHILD_COW
, &error_abort
);
1692 job
= block_job_create("job", &test_simple_job_driver
, NULL
, job_node
,
1693 0, BLK_PERM_ALL
, 0, 0, NULL
, NULL
, &error_abort
);
1695 /* The job has a reference now */
1696 bdrv_unref(job_node
);
1698 job
->did_complete
= &job_has_completed
;
1700 job_start(&job
->common
.job
);
1701 job
->should_complete
= true;
1703 g_assert(!job_has_completed
);
1704 ret
= bdrv_drop_intermediate(chain
[1], chain
[0], NULL
);
1705 aio_poll(qemu_get_aio_context(), false);
1707 g_assert(job_has_completed
);
1709 bdrv_unref(chain
[2]);
1713 typedef struct BDRVReplaceTestState
{
1714 bool setup_completed
;
1721 bool yield_before_read
;
1723 Coroutine
*drain_co
;
1724 } BDRVReplaceTestState
;
1726 static void bdrv_replace_test_close(BlockDriverState
*bs
)
1731 * If @bs has a backing file:
1732 * Yield if .yield_before_read is true (and wait for drain_begin to
1734 * Forward the read to bs->backing. Set .has_read to true.
1735 * If drain_begin has woken us, wake it in turn.
1738 * Set .has_read to true and return success.
1740 static int coroutine_fn GRAPH_RDLOCK
1741 bdrv_replace_test_co_preadv(BlockDriverState
*bs
, int64_t offset
, int64_t bytes
,
1742 QEMUIOVector
*qiov
, BdrvRequestFlags flags
)
1744 BDRVReplaceTestState
*s
= bs
->opaque
;
1749 g_assert(!s
->drain_count
);
1751 s
->io_co
= qemu_coroutine_self();
1752 if (s
->yield_before_read
) {
1753 s
->yield_before_read
= false;
1754 qemu_coroutine_yield();
1758 ret
= bdrv_co_preadv(bs
->backing
, offset
, bytes
, qiov
, 0);
1761 /* Wake up drain_co if it runs */
1763 aio_co_wake(s
->drain_co
);
1773 static void coroutine_fn
bdrv_replace_test_drain_co(void *opaque
)
1775 BlockDriverState
*bs
= opaque
;
1776 BDRVReplaceTestState
*s
= bs
->opaque
;
1778 /* Keep waking io_co up until it is done */
1780 aio_co_wake(s
->io_co
);
1782 qemu_coroutine_yield();
1785 bdrv_dec_in_flight(bs
);
1789 * If .drain_count is 0, wake up .io_co if there is one; and set
1791 * Increment .drain_count.
1793 static void bdrv_replace_test_drain_begin(BlockDriverState
*bs
)
1795 BDRVReplaceTestState
*s
= bs
->opaque
;
1797 if (!s
->setup_completed
) {
1801 if (!s
->drain_count
) {
1802 s
->drain_co
= qemu_coroutine_create(bdrv_replace_test_drain_co
, bs
);
1803 bdrv_inc_in_flight(bs
);
1804 aio_co_enter(bdrv_get_aio_context(bs
), s
->drain_co
);
1805 s
->was_drained
= true;
1810 static void coroutine_fn
bdrv_replace_test_read_entry(void *opaque
)
1812 BlockDriverState
*bs
= opaque
;
1814 QEMUIOVector qiov
= QEMU_IOVEC_INIT_BUF(qiov
, &data
, 1);
1817 /* Queue a read request post-drain */
1818 bdrv_graph_co_rdlock();
1819 ret
= bdrv_replace_test_co_preadv(bs
, 0, 1, &qiov
, 0);
1820 bdrv_graph_co_rdunlock();
1823 bdrv_dec_in_flight(bs
);
1827 * Reduce .drain_count, set .was_undrained once it reaches 0.
1828 * If .drain_count reaches 0 and the node has a backing file, issue a
1831 static void bdrv_replace_test_drain_end(BlockDriverState
*bs
)
1833 BDRVReplaceTestState
*s
= bs
->opaque
;
1835 if (!s
->setup_completed
) {
1839 g_assert(s
->drain_count
> 0);
1840 if (!--s
->drain_count
) {
1841 s
->was_undrained
= true;
1844 Coroutine
*co
= qemu_coroutine_create(bdrv_replace_test_read_entry
,
1846 bdrv_inc_in_flight(bs
);
1847 aio_co_enter(bdrv_get_aio_context(bs
), co
);
1852 static BlockDriver bdrv_replace_test
= {
1853 .format_name
= "replace_test",
1854 .instance_size
= sizeof(BDRVReplaceTestState
),
1855 .supports_backing
= true,
1857 .bdrv_close
= bdrv_replace_test_close
,
1858 .bdrv_co_preadv
= bdrv_replace_test_co_preadv
,
1860 .bdrv_drain_begin
= bdrv_replace_test_drain_begin
,
1861 .bdrv_drain_end
= bdrv_replace_test_drain_end
,
1863 .bdrv_child_perm
= bdrv_default_perms
,
1866 static void coroutine_fn
test_replace_child_mid_drain_read_co(void *opaque
)
1871 ret
= blk_co_pread(opaque
, 0, 1, &data
, 0);
1876 * We test two things:
1877 * (1) bdrv_replace_child_noperm() must not undrain the parent if both
1878 * children are drained.
1879 * (2) bdrv_replace_child_noperm() must never flush I/O requests to a
1880 * drained child. If the old child is drained, it must flush I/O
1881 * requests after the new one has been attached. If the new child
1882 * is drained, it must flush I/O requests before the old one is
1885 * To do so, we create one parent node and two child nodes; then
1886 * attach one of the children (old_child_bs) to the parent, then
1887 * drain both old_child_bs and new_child_bs according to
1888 * old_drain_count and new_drain_count, respectively, and finally
1889 * we invoke bdrv_replace_node() to replace old_child_bs by
1892 * The test block driver we use here (bdrv_replace_test) has a read
1894 * - For the parent node, can optionally yield, and then forwards the
1895 * read to bdrv_preadv(),
1896 * - For the child node, just returns immediately.
1898 * If the read yields, the drain_begin function will wake it up.
1900 * The drain_end function issues a read on the parent once it is fully
1901 * undrained (which simulates requests starting to come in again).
1903 static void do_test_replace_child_mid_drain(int old_drain_count
,
1904 int new_drain_count
)
1906 BlockBackend
*parent_blk
;
1907 BlockDriverState
*parent_bs
;
1908 BlockDriverState
*old_child_bs
, *new_child_bs
;
1909 BDRVReplaceTestState
*parent_s
;
1910 BDRVReplaceTestState
*old_child_s
, *new_child_s
;
1914 parent_bs
= bdrv_new_open_driver(&bdrv_replace_test
, "parent", 0,
1916 parent_s
= parent_bs
->opaque
;
1918 parent_blk
= blk_new(qemu_get_aio_context(),
1919 BLK_PERM_CONSISTENT_READ
, BLK_PERM_ALL
);
1920 blk_insert_bs(parent_blk
, parent_bs
, &error_abort
);
1922 old_child_bs
= bdrv_new_open_driver(&bdrv_replace_test
, "old-child", 0,
1924 new_child_bs
= bdrv_new_open_driver(&bdrv_replace_test
, "new-child", 0,
1926 old_child_s
= old_child_bs
->opaque
;
1927 new_child_s
= new_child_bs
->opaque
;
1929 /* So that we can read something */
1930 parent_bs
->total_sectors
= 1;
1931 old_child_bs
->total_sectors
= 1;
1932 new_child_bs
->total_sectors
= 1;
1934 bdrv_ref(old_child_bs
);
1935 bdrv_attach_child(parent_bs
, old_child_bs
, "child", &child_of_bds
,
1936 BDRV_CHILD_COW
, &error_abort
);
1937 parent_s
->setup_completed
= true;
1939 for (i
= 0; i
< old_drain_count
; i
++) {
1940 bdrv_drained_begin(old_child_bs
);
1942 for (i
= 0; i
< new_drain_count
; i
++) {
1943 bdrv_drained_begin(new_child_bs
);
1946 if (!old_drain_count
) {
1948 * Start a read operation that will yield, so it will not
1949 * complete before the node is drained.
1951 parent_s
->yield_before_read
= true;
1952 io_co
= qemu_coroutine_create(test_replace_child_mid_drain_read_co
,
1954 qemu_coroutine_enter(io_co
);
1957 /* If we have started a read operation, it should have yielded */
1958 g_assert(!parent_s
->has_read
);
1960 /* Reset drained status so we can see what bdrv_replace_node() does */
1961 parent_s
->was_drained
= false;
1962 parent_s
->was_undrained
= false;
1964 g_assert(parent_bs
->quiesce_counter
== old_drain_count
);
1965 bdrv_replace_node(old_child_bs
, new_child_bs
, &error_abort
);
1966 g_assert(parent_bs
->quiesce_counter
== new_drain_count
);
1968 if (!old_drain_count
&& !new_drain_count
) {
1970 * From undrained to undrained drains and undrains the parent,
1971 * because bdrv_replace_node() contains a drained section for
1974 g_assert(parent_s
->was_drained
&& parent_s
->was_undrained
);
1975 } else if (!old_drain_count
&& new_drain_count
) {
1977 * From undrained to drained should drain the parent and keep
1980 g_assert(parent_s
->was_drained
&& !parent_s
->was_undrained
);
1981 } else if (old_drain_count
&& !new_drain_count
) {
1983 * From drained to undrained should undrain the parent and
1986 g_assert(!parent_s
->was_drained
&& parent_s
->was_undrained
);
1987 } else /* if (old_drain_count && new_drain_count) */ {
1989 * From drained to drained must not undrain the parent at any
1992 g_assert(!parent_s
->was_drained
&& !parent_s
->was_undrained
);
1995 if (!old_drain_count
|| !new_drain_count
) {
1997 * If !old_drain_count, we have started a read request before
1998 * bdrv_replace_node(). If !new_drain_count, the parent must
1999 * have been undrained at some point, and
2000 * bdrv_replace_test_co_drain_end() starts a read request
2003 g_assert(parent_s
->has_read
);
2006 * If the parent was never undrained, there is no way to start
2009 g_assert(!parent_s
->has_read
);
2012 /* A drained child must have not received any request */
2013 g_assert(!(old_drain_count
&& old_child_s
->has_read
));
2014 g_assert(!(new_drain_count
&& new_child_s
->has_read
));
2016 for (i
= 0; i
< new_drain_count
; i
++) {
2017 bdrv_drained_end(new_child_bs
);
2019 for (i
= 0; i
< old_drain_count
; i
++) {
2020 bdrv_drained_end(old_child_bs
);
2024 * By now, bdrv_replace_test_co_drain_end() must have been called
2025 * at some point while the new child was attached to the parent.
2027 g_assert(parent_s
->has_read
);
2028 g_assert(new_child_s
->has_read
);
2030 blk_unref(parent_blk
);
2031 bdrv_unref(parent_bs
);
2032 bdrv_unref(old_child_bs
);
2033 bdrv_unref(new_child_bs
);
2036 static void test_replace_child_mid_drain(void)
2038 int old_drain_count
, new_drain_count
;
2040 for (old_drain_count
= 0; old_drain_count
< 2; old_drain_count
++) {
2041 for (new_drain_count
= 0; new_drain_count
< 2; new_drain_count
++) {
2042 do_test_replace_child_mid_drain(old_drain_count
, new_drain_count
);
2047 int main(int argc
, char **argv
)
2052 qemu_init_main_loop(&error_abort
);
2054 g_test_init(&argc
, &argv
, NULL
);
2055 qemu_event_init(&done_event
, false);
2057 g_test_add_func("/bdrv-drain/driver-cb/drain_all", test_drv_cb_drain_all
);
2058 g_test_add_func("/bdrv-drain/driver-cb/drain", test_drv_cb_drain
);
2060 g_test_add_func("/bdrv-drain/driver-cb/co/drain_all",
2061 test_drv_cb_co_drain_all
);
2062 g_test_add_func("/bdrv-drain/driver-cb/co/drain", test_drv_cb_co_drain
);
2064 g_test_add_func("/bdrv-drain/quiesce/drain_all", test_quiesce_drain_all
);
2065 g_test_add_func("/bdrv-drain/quiesce/drain", test_quiesce_drain
);
2067 g_test_add_func("/bdrv-drain/quiesce/co/drain_all",
2068 test_quiesce_co_drain_all
);
2069 g_test_add_func("/bdrv-drain/quiesce/co/drain", test_quiesce_co_drain
);
2071 g_test_add_func("/bdrv-drain/nested", test_nested
);
2073 g_test_add_func("/bdrv-drain/graph-change/drain_all",
2074 test_graph_change_drain_all
);
2076 g_test_add_func("/bdrv-drain/iothread/drain_all", test_iothread_drain_all
);
2077 g_test_add_func("/bdrv-drain/iothread/drain", test_iothread_drain
);
2079 g_test_add_func("/bdrv-drain/blockjob/drain_all", test_blockjob_drain_all
);
2080 g_test_add_func("/bdrv-drain/blockjob/drain", test_blockjob_drain
);
2082 g_test_add_func("/bdrv-drain/blockjob/error/drain_all",
2083 test_blockjob_error_drain_all
);
2084 g_test_add_func("/bdrv-drain/blockjob/error/drain",
2085 test_blockjob_error_drain
);
2087 g_test_add_func("/bdrv-drain/blockjob/iothread/drain_all",
2088 test_blockjob_iothread_drain_all
);
2089 g_test_add_func("/bdrv-drain/blockjob/iothread/drain",
2090 test_blockjob_iothread_drain
);
2092 g_test_add_func("/bdrv-drain/blockjob/iothread/error/drain_all",
2093 test_blockjob_iothread_error_drain_all
);
2094 g_test_add_func("/bdrv-drain/blockjob/iothread/error/drain",
2095 test_blockjob_iothread_error_drain
);
2097 g_test_add_func("/bdrv-drain/deletion/drain", test_delete_by_drain
);
2098 g_test_add_func("/bdrv-drain/detach/drain_all", test_detach_by_drain_all
);
2099 g_test_add_func("/bdrv-drain/detach/drain", test_detach_by_drain
);
2100 g_test_add_func("/bdrv-drain/detach/parent_cb", test_detach_by_parent_cb
);
2101 g_test_add_func("/bdrv-drain/detach/driver_cb", test_detach_by_driver_cb
);
2103 g_test_add_func("/bdrv-drain/attach/drain", test_append_to_drained
);
2105 g_test_add_func("/bdrv-drain/set_aio_context", test_set_aio_context
);
2107 g_test_add_func("/bdrv-drain/blockjob/commit_by_drained_end",
2108 test_blockjob_commit_by_drained_end
);
2110 g_test_add_func("/bdrv-drain/bdrv_drop_intermediate/poll",
2111 test_drop_intermediate_poll
);
2113 g_test_add_func("/bdrv-drain/replace_child/mid-drain",
2114 test_replace_child_mid_drain
);
2117 qemu_event_destroy(&done_event
);