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 void do_drain_end_unlocked(enum drain_type drain_type
, BlockDriverState
*bs
)
193 if (drain_type
!= BDRV_DRAIN_ALL
) {
194 aio_context_acquire(bdrv_get_aio_context(bs
));
196 do_drain_end(drain_type
, bs
);
197 if (drain_type
!= BDRV_DRAIN_ALL
) {
198 aio_context_release(bdrv_get_aio_context(bs
));
202 static void test_drv_cb_common(enum drain_type drain_type
, bool recursive
)
205 BlockDriverState
*bs
, *backing
;
206 BDRVTestState
*s
, *backing_s
;
210 QEMUIOVector qiov
= QEMU_IOVEC_INIT_BUF(qiov
, NULL
, 0);
212 blk
= blk_new(qemu_get_aio_context(), BLK_PERM_ALL
, BLK_PERM_ALL
);
213 bs
= bdrv_new_open_driver(&bdrv_test
, "test-node", BDRV_O_RDWR
,
216 blk_insert_bs(blk
, bs
, &error_abort
);
218 backing
= bdrv_new_open_driver(&bdrv_test
, "backing", 0, &error_abort
);
219 backing_s
= backing
->opaque
;
220 bdrv_set_backing_hd(bs
, backing
, &error_abort
);
222 /* Simple bdrv_drain_all_begin/end pair, check that CBs are called */
223 g_assert_cmpint(s
->drain_count
, ==, 0);
224 g_assert_cmpint(backing_s
->drain_count
, ==, 0);
226 do_drain_begin(drain_type
, bs
);
228 g_assert_cmpint(s
->drain_count
, ==, 1);
229 g_assert_cmpint(backing_s
->drain_count
, ==, !!recursive
);
231 do_drain_end(drain_type
, bs
);
233 g_assert_cmpint(s
->drain_count
, ==, 0);
234 g_assert_cmpint(backing_s
->drain_count
, ==, 0);
236 /* Now do the same while a request is pending */
237 aio_ret
= -EINPROGRESS
;
238 acb
= blk_aio_preadv(blk
, 0, &qiov
, 0, aio_ret_cb
, &aio_ret
);
239 g_assert(acb
!= NULL
);
240 g_assert_cmpint(aio_ret
, ==, -EINPROGRESS
);
242 g_assert_cmpint(s
->drain_count
, ==, 0);
243 g_assert_cmpint(backing_s
->drain_count
, ==, 0);
245 do_drain_begin(drain_type
, bs
);
247 g_assert_cmpint(aio_ret
, ==, 0);
248 g_assert_cmpint(s
->drain_count
, ==, 1);
249 g_assert_cmpint(backing_s
->drain_count
, ==, !!recursive
);
251 do_drain_end(drain_type
, bs
);
253 g_assert_cmpint(s
->drain_count
, ==, 0);
254 g_assert_cmpint(backing_s
->drain_count
, ==, 0);
261 static void test_drv_cb_drain_all(void)
263 test_drv_cb_common(BDRV_DRAIN_ALL
, true);
266 static void test_drv_cb_drain(void)
268 test_drv_cb_common(BDRV_DRAIN
, false);
271 static void test_drv_cb_co_drain_all(void)
273 call_in_coroutine(test_drv_cb_drain_all
);
276 static void test_drv_cb_co_drain(void)
278 call_in_coroutine(test_drv_cb_drain
);
281 static void test_quiesce_common(enum drain_type drain_type
, bool recursive
)
284 BlockDriverState
*bs
, *backing
;
286 blk
= blk_new(qemu_get_aio_context(), BLK_PERM_ALL
, BLK_PERM_ALL
);
287 bs
= bdrv_new_open_driver(&bdrv_test
, "test-node", BDRV_O_RDWR
,
289 blk_insert_bs(blk
, bs
, &error_abort
);
291 backing
= bdrv_new_open_driver(&bdrv_test
, "backing", 0, &error_abort
);
292 bdrv_set_backing_hd(bs
, backing
, &error_abort
);
294 g_assert_cmpint(bs
->quiesce_counter
, ==, 0);
295 g_assert_cmpint(backing
->quiesce_counter
, ==, 0);
297 do_drain_begin(drain_type
, bs
);
299 if (drain_type
== BDRV_DRAIN_ALL
) {
300 g_assert_cmpint(bs
->quiesce_counter
, ==, 2);
302 g_assert_cmpint(bs
->quiesce_counter
, ==, 1);
304 g_assert_cmpint(backing
->quiesce_counter
, ==, !!recursive
);
306 do_drain_end(drain_type
, bs
);
308 g_assert_cmpint(bs
->quiesce_counter
, ==, 0);
309 g_assert_cmpint(backing
->quiesce_counter
, ==, 0);
316 static void test_quiesce_drain_all(void)
318 test_quiesce_common(BDRV_DRAIN_ALL
, true);
321 static void test_quiesce_drain(void)
323 test_quiesce_common(BDRV_DRAIN
, false);
326 static void test_quiesce_co_drain_all(void)
328 call_in_coroutine(test_quiesce_drain_all
);
331 static void test_quiesce_co_drain(void)
333 call_in_coroutine(test_quiesce_drain
);
336 static void test_nested(void)
339 BlockDriverState
*bs
, *backing
;
340 BDRVTestState
*s
, *backing_s
;
341 enum drain_type outer
, inner
;
343 blk
= blk_new(qemu_get_aio_context(), BLK_PERM_ALL
, BLK_PERM_ALL
);
344 bs
= bdrv_new_open_driver(&bdrv_test
, "test-node", BDRV_O_RDWR
,
347 blk_insert_bs(blk
, bs
, &error_abort
);
349 backing
= bdrv_new_open_driver(&bdrv_test
, "backing", 0, &error_abort
);
350 backing_s
= backing
->opaque
;
351 bdrv_set_backing_hd(bs
, backing
, &error_abort
);
353 for (outer
= 0; outer
< DRAIN_TYPE_MAX
; outer
++) {
354 for (inner
= 0; inner
< DRAIN_TYPE_MAX
; inner
++) {
355 int backing_quiesce
= (outer
== BDRV_DRAIN_ALL
) +
356 (inner
== BDRV_DRAIN_ALL
);
358 g_assert_cmpint(bs
->quiesce_counter
, ==, 0);
359 g_assert_cmpint(backing
->quiesce_counter
, ==, 0);
360 g_assert_cmpint(s
->drain_count
, ==, 0);
361 g_assert_cmpint(backing_s
->drain_count
, ==, 0);
363 do_drain_begin(outer
, bs
);
364 do_drain_begin(inner
, bs
);
366 g_assert_cmpint(bs
->quiesce_counter
, ==, 2 + !!backing_quiesce
);
367 g_assert_cmpint(backing
->quiesce_counter
, ==, backing_quiesce
);
368 g_assert_cmpint(s
->drain_count
, ==, 1);
369 g_assert_cmpint(backing_s
->drain_count
, ==, !!backing_quiesce
);
371 do_drain_end(inner
, bs
);
372 do_drain_end(outer
, bs
);
374 g_assert_cmpint(bs
->quiesce_counter
, ==, 0);
375 g_assert_cmpint(backing
->quiesce_counter
, ==, 0);
376 g_assert_cmpint(s
->drain_count
, ==, 0);
377 g_assert_cmpint(backing_s
->drain_count
, ==, 0);
386 static void test_graph_change_drain_all(void)
388 BlockBackend
*blk_a
, *blk_b
;
389 BlockDriverState
*bs_a
, *bs_b
;
390 BDRVTestState
*a_s
, *b_s
;
392 /* Create node A with a BlockBackend */
393 blk_a
= blk_new(qemu_get_aio_context(), BLK_PERM_ALL
, BLK_PERM_ALL
);
394 bs_a
= bdrv_new_open_driver(&bdrv_test
, "test-node-a", BDRV_O_RDWR
,
397 blk_insert_bs(blk_a
, bs_a
, &error_abort
);
399 g_assert_cmpint(bs_a
->quiesce_counter
, ==, 0);
400 g_assert_cmpint(a_s
->drain_count
, ==, 0);
402 /* Call bdrv_drain_all_begin() */
403 bdrv_drain_all_begin();
405 g_assert_cmpint(bs_a
->quiesce_counter
, ==, 1);
406 g_assert_cmpint(a_s
->drain_count
, ==, 1);
408 /* Create node B with a BlockBackend */
409 blk_b
= blk_new(qemu_get_aio_context(), BLK_PERM_ALL
, BLK_PERM_ALL
);
410 bs_b
= bdrv_new_open_driver(&bdrv_test
, "test-node-b", BDRV_O_RDWR
,
413 blk_insert_bs(blk_b
, bs_b
, &error_abort
);
415 g_assert_cmpint(bs_a
->quiesce_counter
, ==, 1);
416 g_assert_cmpint(bs_b
->quiesce_counter
, ==, 1);
417 g_assert_cmpint(a_s
->drain_count
, ==, 1);
418 g_assert_cmpint(b_s
->drain_count
, ==, 1);
420 /* Unref and finally delete node A */
423 g_assert_cmpint(bs_a
->quiesce_counter
, ==, 1);
424 g_assert_cmpint(bs_b
->quiesce_counter
, ==, 1);
425 g_assert_cmpint(a_s
->drain_count
, ==, 1);
426 g_assert_cmpint(b_s
->drain_count
, ==, 1);
430 g_assert_cmpint(bs_b
->quiesce_counter
, ==, 1);
431 g_assert_cmpint(b_s
->drain_count
, ==, 1);
433 /* End the drained section */
434 bdrv_drain_all_end();
436 g_assert_cmpint(bs_b
->quiesce_counter
, ==, 0);
437 g_assert_cmpint(b_s
->drain_count
, ==, 0);
438 g_assert_cmpint(qemu_get_aio_context()->external_disable_cnt
, ==, 0);
444 struct test_iothread_data
{
445 BlockDriverState
*bs
;
446 enum drain_type drain_type
;
450 static void test_iothread_drain_entry(void *opaque
)
452 struct test_iothread_data
*data
= opaque
;
454 aio_context_acquire(bdrv_get_aio_context(data
->bs
));
455 do_drain_begin(data
->drain_type
, data
->bs
);
456 g_assert_cmpint(*data
->aio_ret
, ==, 0);
457 do_drain_end(data
->drain_type
, data
->bs
);
458 aio_context_release(bdrv_get_aio_context(data
->bs
));
460 qemu_event_set(&done_event
);
463 static void test_iothread_aio_cb(void *opaque
, int ret
)
465 int *aio_ret
= opaque
;
467 qemu_event_set(&done_event
);
470 static void test_iothread_main_thread_bh(void *opaque
)
472 struct test_iothread_data
*data
= opaque
;
474 /* Test that the AioContext is not yet locked in a random BH that is
475 * executed during drain, otherwise this would deadlock. */
476 aio_context_acquire(bdrv_get_aio_context(data
->bs
));
477 bdrv_flush(data
->bs
);
478 aio_context_release(bdrv_get_aio_context(data
->bs
));
482 * Starts an AIO request on a BDS that runs in the AioContext of iothread 1.
483 * The request involves a BH on iothread 2 before it can complete.
485 * @drain_thread = 0 means that do_drain_begin/end are called from the main
486 * thread, @drain_thread = 1 means that they are called from iothread 1. Drain
487 * for this BDS cannot be called from iothread 2 because only the main thread
488 * may do cross-AioContext polling.
490 static void test_iothread_common(enum drain_type drain_type
, int drain_thread
)
493 BlockDriverState
*bs
;
497 struct test_iothread_data data
;
499 IOThread
*a
= iothread_new();
500 IOThread
*b
= iothread_new();
501 AioContext
*ctx_a
= iothread_get_aio_context(a
);
502 AioContext
*ctx_b
= iothread_get_aio_context(b
);
504 QEMUIOVector qiov
= QEMU_IOVEC_INIT_BUF(qiov
, NULL
, 0);
506 /* bdrv_drain_all() may only be called from the main loop thread */
507 if (drain_type
== BDRV_DRAIN_ALL
&& drain_thread
!= 0) {
511 blk
= blk_new(qemu_get_aio_context(), BLK_PERM_ALL
, BLK_PERM_ALL
);
512 bs
= bdrv_new_open_driver(&bdrv_test
, "test-node", BDRV_O_RDWR
,
515 blk_insert_bs(blk
, bs
, &error_abort
);
516 blk_set_disable_request_queuing(blk
, true);
518 blk_set_aio_context(blk
, ctx_a
, &error_abort
);
519 aio_context_acquire(ctx_a
);
521 s
->bh_indirection_ctx
= ctx_b
;
523 aio_ret
= -EINPROGRESS
;
524 qemu_event_reset(&done_event
);
526 if (drain_thread
== 0) {
527 acb
= blk_aio_preadv(blk
, 0, &qiov
, 0, test_iothread_aio_cb
, &aio_ret
);
529 acb
= blk_aio_preadv(blk
, 0, &qiov
, 0, aio_ret_cb
, &aio_ret
);
531 g_assert(acb
!= NULL
);
532 g_assert_cmpint(aio_ret
, ==, -EINPROGRESS
);
534 aio_context_release(ctx_a
);
536 data
= (struct test_iothread_data
) {
538 .drain_type
= drain_type
,
542 switch (drain_thread
) {
544 if (drain_type
!= BDRV_DRAIN_ALL
) {
545 aio_context_acquire(ctx_a
);
548 aio_bh_schedule_oneshot(ctx_a
, test_iothread_main_thread_bh
, &data
);
550 /* The request is running on the IOThread a. Draining its block device
551 * will make sure that it has completed as far as the BDS is concerned,
552 * but the drain in this thread can continue immediately after
553 * bdrv_dec_in_flight() and aio_ret might be assigned only slightly
555 do_drain_begin(drain_type
, bs
);
556 g_assert_cmpint(bs
->in_flight
, ==, 0);
558 if (drain_type
!= BDRV_DRAIN_ALL
) {
559 aio_context_release(ctx_a
);
561 qemu_event_wait(&done_event
);
562 if (drain_type
!= BDRV_DRAIN_ALL
) {
563 aio_context_acquire(ctx_a
);
566 g_assert_cmpint(aio_ret
, ==, 0);
567 do_drain_end(drain_type
, bs
);
569 if (drain_type
!= BDRV_DRAIN_ALL
) {
570 aio_context_release(ctx_a
);
574 aio_bh_schedule_oneshot(ctx_a
, test_iothread_drain_entry
, &data
);
575 qemu_event_wait(&done_event
);
578 g_assert_not_reached();
581 aio_context_acquire(ctx_a
);
582 blk_set_aio_context(blk
, qemu_get_aio_context(), &error_abort
);
583 aio_context_release(ctx_a
);
593 static void test_iothread_drain_all(void)
595 test_iothread_common(BDRV_DRAIN_ALL
, 0);
596 test_iothread_common(BDRV_DRAIN_ALL
, 1);
599 static void test_iothread_drain(void)
601 test_iothread_common(BDRV_DRAIN
, 0);
602 test_iothread_common(BDRV_DRAIN
, 1);
606 typedef struct TestBlockJob
{
608 BlockDriverState
*bs
;
612 bool should_complete
;
615 static int test_job_prepare(Job
*job
)
617 TestBlockJob
*s
= container_of(job
, TestBlockJob
, common
.job
);
619 /* Provoke an AIO_WAIT_WHILE() call to verify there is no deadlock */
621 return s
->prepare_ret
;
624 static void test_job_commit(Job
*job
)
626 TestBlockJob
*s
= container_of(job
, TestBlockJob
, common
.job
);
628 /* Provoke an AIO_WAIT_WHILE() call to verify there is no deadlock */
632 static void test_job_abort(Job
*job
)
634 TestBlockJob
*s
= container_of(job
, TestBlockJob
, common
.job
);
636 /* Provoke an AIO_WAIT_WHILE() call to verify there is no deadlock */
640 static int coroutine_fn
test_job_run(Job
*job
, Error
**errp
)
642 TestBlockJob
*s
= container_of(job
, TestBlockJob
, common
.job
);
644 /* We are running the actual job code past the pause point in
648 job_transition_to_ready(&s
->common
.job
);
649 while (!s
->should_complete
) {
650 /* Avoid job_sleep_ns() because it marks the job as !busy. We want to
651 * emulate some actual activity (probably some I/O) here so that drain
652 * has to wait for this activity to stop. */
653 qemu_co_sleep_ns(QEMU_CLOCK_REALTIME
, 1000000);
655 job_pause_point(&s
->common
.job
);
661 static void test_job_complete(Job
*job
, Error
**errp
)
663 TestBlockJob
*s
= container_of(job
, TestBlockJob
, common
.job
);
664 s
->should_complete
= true;
667 BlockJobDriver test_job_driver
= {
669 .instance_size
= sizeof(TestBlockJob
),
670 .free
= block_job_free
,
671 .user_resume
= block_job_user_resume
,
673 .complete
= test_job_complete
,
674 .prepare
= test_job_prepare
,
675 .commit
= test_job_commit
,
676 .abort
= test_job_abort
,
680 enum test_job_result
{
683 TEST_JOB_FAIL_PREPARE
,
686 enum test_job_drain_node
{
688 TEST_JOB_DRAIN_SRC_CHILD
,
691 static void test_blockjob_common_drain_node(enum drain_type drain_type
,
693 enum test_job_result result
,
694 enum test_job_drain_node drain_node
)
696 BlockBackend
*blk_src
, *blk_target
;
697 BlockDriverState
*src
, *src_backing
, *src_overlay
, *target
, *drain_bs
;
700 IOThread
*iothread
= NULL
;
704 src
= bdrv_new_open_driver(&bdrv_test
, "source", BDRV_O_RDWR
,
706 src_backing
= bdrv_new_open_driver(&bdrv_test
, "source-backing",
707 BDRV_O_RDWR
, &error_abort
);
708 src_overlay
= bdrv_new_open_driver(&bdrv_test
, "source-overlay",
709 BDRV_O_RDWR
, &error_abort
);
711 bdrv_set_backing_hd(src_overlay
, src
, &error_abort
);
713 bdrv_set_backing_hd(src
, src_backing
, &error_abort
);
714 bdrv_unref(src_backing
);
716 blk_src
= blk_new(qemu_get_aio_context(), BLK_PERM_ALL
, BLK_PERM_ALL
);
717 blk_insert_bs(blk_src
, src_overlay
, &error_abort
);
719 switch (drain_node
) {
720 case TEST_JOB_DRAIN_SRC
:
723 case TEST_JOB_DRAIN_SRC_CHILD
:
724 drain_bs
= src_backing
;
727 g_assert_not_reached();
731 iothread
= iothread_new();
732 ctx
= iothread_get_aio_context(iothread
);
733 blk_set_aio_context(blk_src
, ctx
, &error_abort
);
735 ctx
= qemu_get_aio_context();
738 target
= bdrv_new_open_driver(&bdrv_test
, "target", BDRV_O_RDWR
,
740 blk_target
= blk_new(qemu_get_aio_context(), BLK_PERM_ALL
, BLK_PERM_ALL
);
741 blk_insert_bs(blk_target
, target
, &error_abort
);
742 blk_set_allow_aio_context_change(blk_target
, true);
744 aio_context_acquire(ctx
);
745 tjob
= block_job_create("job0", &test_job_driver
, NULL
, src
,
747 0, 0, NULL
, NULL
, &error_abort
);
750 block_job_add_bdrv(job
, "target", target
, 0, BLK_PERM_ALL
, &error_abort
);
753 case TEST_JOB_SUCCESS
:
755 case TEST_JOB_FAIL_RUN
:
756 tjob
->run_ret
= -EIO
;
758 case TEST_JOB_FAIL_PREPARE
:
759 tjob
->prepare_ret
= -EIO
;
762 aio_context_release(ctx
);
764 job_start(&job
->job
);
767 /* job_co_entry() is run in the I/O thread, wait for the actual job
768 * code to start (we don't want to catch the job in the pause point in
770 while (!tjob
->running
) {
771 aio_poll(qemu_get_aio_context(), false);
775 WITH_JOB_LOCK_GUARD() {
776 g_assert_cmpint(job
->job
.pause_count
, ==, 0);
777 g_assert_false(job
->job
.paused
);
778 g_assert_true(tjob
->running
);
779 g_assert_true(job
->job
.busy
); /* We're in qemu_co_sleep_ns() */
782 do_drain_begin_unlocked(drain_type
, drain_bs
);
784 WITH_JOB_LOCK_GUARD() {
785 if (drain_type
== BDRV_DRAIN_ALL
) {
786 /* bdrv_drain_all() drains both src and target */
787 g_assert_cmpint(job
->job
.pause_count
, ==, 2);
789 g_assert_cmpint(job
->job
.pause_count
, ==, 1);
791 g_assert_true(job
->job
.paused
);
792 g_assert_false(job
->job
.busy
); /* The job is paused */
795 do_drain_end_unlocked(drain_type
, drain_bs
);
799 * Here we are waiting for the paused status to change,
800 * so don't bother protecting the read every time.
802 * paused is reset in the I/O thread, wait for it
804 while (job
->job
.paused
) {
805 aio_poll(qemu_get_aio_context(), false);
809 WITH_JOB_LOCK_GUARD() {
810 g_assert_cmpint(job
->job
.pause_count
, ==, 0);
811 g_assert_false(job
->job
.paused
);
812 g_assert_true(job
->job
.busy
); /* We're in qemu_co_sleep_ns() */
815 do_drain_begin_unlocked(drain_type
, target
);
817 WITH_JOB_LOCK_GUARD() {
818 if (drain_type
== BDRV_DRAIN_ALL
) {
819 /* bdrv_drain_all() drains both src and target */
820 g_assert_cmpint(job
->job
.pause_count
, ==, 2);
822 g_assert_cmpint(job
->job
.pause_count
, ==, 1);
824 g_assert_true(job
->job
.paused
);
825 g_assert_false(job
->job
.busy
); /* The job is paused */
828 do_drain_end_unlocked(drain_type
, target
);
832 * Here we are waiting for the paused status to change,
833 * so don't bother protecting the read every time.
835 * paused is reset in the I/O thread, wait for it
837 while (job
->job
.paused
) {
838 aio_poll(qemu_get_aio_context(), false);
842 WITH_JOB_LOCK_GUARD() {
843 g_assert_cmpint(job
->job
.pause_count
, ==, 0);
844 g_assert_false(job
->job
.paused
);
845 g_assert_true(job
->job
.busy
); /* We're in qemu_co_sleep_ns() */
848 WITH_JOB_LOCK_GUARD() {
849 ret
= job_complete_sync_locked(&job
->job
, &error_abort
);
851 g_assert_cmpint(ret
, ==, (result
== TEST_JOB_SUCCESS
? 0 : -EIO
));
853 aio_context_acquire(ctx
);
855 blk_set_aio_context(blk_src
, qemu_get_aio_context(), &error_abort
);
856 assert(blk_get_aio_context(blk_target
) == qemu_get_aio_context());
858 aio_context_release(ctx
);
861 blk_unref(blk_target
);
862 bdrv_unref(src_overlay
);
866 iothread_join(iothread
);
870 static void test_blockjob_common(enum drain_type drain_type
, bool use_iothread
,
871 enum test_job_result result
)
873 test_blockjob_common_drain_node(drain_type
, use_iothread
, result
,
875 test_blockjob_common_drain_node(drain_type
, use_iothread
, result
,
876 TEST_JOB_DRAIN_SRC_CHILD
);
879 static void test_blockjob_drain_all(void)
881 test_blockjob_common(BDRV_DRAIN_ALL
, false, TEST_JOB_SUCCESS
);
884 static void test_blockjob_drain(void)
886 test_blockjob_common(BDRV_DRAIN
, false, TEST_JOB_SUCCESS
);
889 static void test_blockjob_error_drain_all(void)
891 test_blockjob_common(BDRV_DRAIN_ALL
, false, TEST_JOB_FAIL_RUN
);
892 test_blockjob_common(BDRV_DRAIN_ALL
, false, TEST_JOB_FAIL_PREPARE
);
895 static void test_blockjob_error_drain(void)
897 test_blockjob_common(BDRV_DRAIN
, false, TEST_JOB_FAIL_RUN
);
898 test_blockjob_common(BDRV_DRAIN
, false, TEST_JOB_FAIL_PREPARE
);
901 static void test_blockjob_iothread_drain_all(void)
903 test_blockjob_common(BDRV_DRAIN_ALL
, true, TEST_JOB_SUCCESS
);
906 static void test_blockjob_iothread_drain(void)
908 test_blockjob_common(BDRV_DRAIN
, true, TEST_JOB_SUCCESS
);
911 static void test_blockjob_iothread_error_drain_all(void)
913 test_blockjob_common(BDRV_DRAIN_ALL
, true, TEST_JOB_FAIL_RUN
);
914 test_blockjob_common(BDRV_DRAIN_ALL
, true, TEST_JOB_FAIL_PREPARE
);
917 static void test_blockjob_iothread_error_drain(void)
919 test_blockjob_common(BDRV_DRAIN
, true, TEST_JOB_FAIL_RUN
);
920 test_blockjob_common(BDRV_DRAIN
, true, TEST_JOB_FAIL_PREPARE
);
924 typedef struct BDRVTestTopState
{
925 BdrvChild
*wait_child
;
928 static void bdrv_test_top_close(BlockDriverState
*bs
)
930 BdrvChild
*c
, *next_c
;
931 QLIST_FOREACH_SAFE(c
, &bs
->children
, next
, next_c
) {
932 bdrv_unref_child(bs
, c
);
936 static int coroutine_fn
bdrv_test_top_co_preadv(BlockDriverState
*bs
,
937 int64_t offset
, int64_t bytes
,
939 BdrvRequestFlags flags
)
941 BDRVTestTopState
*tts
= bs
->opaque
;
942 return bdrv_co_preadv(tts
->wait_child
, offset
, bytes
, qiov
, flags
);
945 static BlockDriver bdrv_test_top_driver
= {
946 .format_name
= "test_top_driver",
947 .instance_size
= sizeof(BDRVTestTopState
),
949 .bdrv_close
= bdrv_test_top_close
,
950 .bdrv_co_preadv
= bdrv_test_top_co_preadv
,
952 .bdrv_child_perm
= bdrv_default_perms
,
955 typedef struct TestCoDeleteByDrainData
{
957 bool detach_instead_of_delete
;
959 } TestCoDeleteByDrainData
;
961 static void coroutine_fn
test_co_delete_by_drain(void *opaque
)
963 TestCoDeleteByDrainData
*dbdd
= opaque
;
964 BlockBackend
*blk
= dbdd
->blk
;
965 BlockDriverState
*bs
= blk_bs(blk
);
966 BDRVTestTopState
*tts
= bs
->opaque
;
967 void *buffer
= g_malloc(65536);
968 QEMUIOVector qiov
= QEMU_IOVEC_INIT_BUF(qiov
, buffer
, 65536);
970 /* Pretend some internal write operation from parent to child.
971 * Important: We have to read from the child, not from the parent!
972 * Draining works by first propagating it all up the tree to the
973 * root and then waiting for drainage from root to the leaves
974 * (protocol nodes). If we have a request waiting on the root,
975 * everything will be drained before we go back down the tree, but
976 * we do not want that. We want to be in the middle of draining
977 * when this following requests returns. */
978 bdrv_co_preadv(tts
->wait_child
, 0, 65536, &qiov
, 0);
980 g_assert_cmpint(bs
->refcnt
, ==, 1);
982 if (!dbdd
->detach_instead_of_delete
) {
985 BdrvChild
*c
, *next_c
;
986 QLIST_FOREACH_SAFE(c
, &bs
->children
, next
, next_c
) {
987 bdrv_unref_child(bs
, c
);
996 * Test what happens when some BDS has some children, you drain one of
997 * them and this results in the BDS being deleted.
999 * If @detach_instead_of_delete is set, the BDS is not going to be
1000 * deleted but will only detach all of its children.
1002 static void do_test_delete_by_drain(bool detach_instead_of_delete
,
1003 enum drain_type drain_type
)
1006 BlockDriverState
*bs
, *child_bs
, *null_bs
;
1007 BDRVTestTopState
*tts
;
1008 TestCoDeleteByDrainData dbdd
;
1011 bs
= bdrv_new_open_driver(&bdrv_test_top_driver
, "top", BDRV_O_RDWR
,
1013 bs
->total_sectors
= 65536 >> BDRV_SECTOR_BITS
;
1016 null_bs
= bdrv_open("null-co://", NULL
, NULL
, BDRV_O_RDWR
| BDRV_O_PROTOCOL
,
1018 bdrv_attach_child(bs
, null_bs
, "null-child", &child_of_bds
,
1019 BDRV_CHILD_DATA
, &error_abort
);
1021 /* This child will be the one to pass to requests through to, and
1022 * it will stall until a drain occurs */
1023 child_bs
= bdrv_new_open_driver(&bdrv_test
, "child", BDRV_O_RDWR
,
1025 child_bs
->total_sectors
= 65536 >> BDRV_SECTOR_BITS
;
1026 /* Takes our reference to child_bs */
1027 tts
->wait_child
= bdrv_attach_child(bs
, child_bs
, "wait-child",
1029 BDRV_CHILD_DATA
| BDRV_CHILD_PRIMARY
,
1032 /* This child is just there to be deleted
1033 * (for detach_instead_of_delete == true) */
1034 null_bs
= bdrv_open("null-co://", NULL
, NULL
, BDRV_O_RDWR
| BDRV_O_PROTOCOL
,
1036 bdrv_attach_child(bs
, null_bs
, "null-child", &child_of_bds
, BDRV_CHILD_DATA
,
1039 blk
= blk_new(qemu_get_aio_context(), BLK_PERM_ALL
, BLK_PERM_ALL
);
1040 blk_insert_bs(blk
, bs
, &error_abort
);
1042 /* Referenced by blk now */
1045 g_assert_cmpint(bs
->refcnt
, ==, 1);
1046 g_assert_cmpint(child_bs
->refcnt
, ==, 1);
1047 g_assert_cmpint(null_bs
->refcnt
, ==, 1);
1050 dbdd
= (TestCoDeleteByDrainData
){
1052 .detach_instead_of_delete
= detach_instead_of_delete
,
1055 co
= qemu_coroutine_create(test_co_delete_by_drain
, &dbdd
);
1056 qemu_coroutine_enter(co
);
1058 /* Drain the child while the read operation is still pending.
1059 * This should result in the operation finishing and
1060 * test_co_delete_by_drain() resuming. Thus, @bs will be deleted
1061 * and the coroutine will exit while this drain operation is still
1063 switch (drain_type
) {
1066 bdrv_drain(child_bs
);
1067 bdrv_unref(child_bs
);
1069 case BDRV_DRAIN_ALL
:
1070 bdrv_drain_all_begin();
1071 bdrv_drain_all_end();
1074 g_assert_not_reached();
1077 while (!dbdd
.done
) {
1078 aio_poll(qemu_get_aio_context(), true);
1081 if (detach_instead_of_delete
) {
1082 /* Here, the reference has not passed over to the coroutine,
1083 * so we have to delete the BB ourselves */
1088 static void test_delete_by_drain(void)
1090 do_test_delete_by_drain(false, BDRV_DRAIN
);
1093 static void test_detach_by_drain_all(void)
1095 do_test_delete_by_drain(true, BDRV_DRAIN_ALL
);
1098 static void test_detach_by_drain(void)
1100 do_test_delete_by_drain(true, BDRV_DRAIN
);
1104 struct detach_by_parent_data
{
1105 BlockDriverState
*parent_b
;
1107 BlockDriverState
*c
;
1110 bool detach_on_drain
;
1112 static struct detach_by_parent_data detach_by_parent_data
;
1114 static void detach_indirect_bh(void *opaque
)
1116 struct detach_by_parent_data
*data
= opaque
;
1118 bdrv_dec_in_flight(data
->child_b
->bs
);
1119 bdrv_unref_child(data
->parent_b
, data
->child_b
);
1122 data
->child_c
= bdrv_attach_child(data
->parent_b
, data
->c
, "PB-C",
1123 &child_of_bds
, BDRV_CHILD_DATA
,
1127 static void detach_by_parent_aio_cb(void *opaque
, int ret
)
1129 struct detach_by_parent_data
*data
= &detach_by_parent_data
;
1131 g_assert_cmpint(ret
, ==, 0);
1132 if (data
->by_parent_cb
) {
1133 bdrv_inc_in_flight(data
->child_b
->bs
);
1134 detach_indirect_bh(data
);
1138 static void detach_by_driver_cb_drained_begin(BdrvChild
*child
)
1140 struct detach_by_parent_data
*data
= &detach_by_parent_data
;
1142 if (!data
->detach_on_drain
) {
1145 data
->detach_on_drain
= false;
1147 bdrv_inc_in_flight(data
->child_b
->bs
);
1148 aio_bh_schedule_oneshot(qemu_get_current_aio_context(),
1149 detach_indirect_bh
, &detach_by_parent_data
);
1150 child_of_bds
.drained_begin(child
);
1153 static BdrvChildClass detach_by_driver_cb_class
;
1162 * by_parent_cb == true: Test that parent callbacks don't poll
1164 * PA has a pending write request whose callback changes the child nodes of
1165 * PB: It removes B and adds C instead. The subtree of PB is drained, which
1166 * will indirectly drain the write request, too.
1168 * by_parent_cb == false: Test that bdrv_drain_invoke() doesn't poll
1170 * PA's BdrvChildClass has a .drained_begin callback that schedules a BH
1171 * that does the same graph change. If bdrv_drain_invoke() calls it, the
1172 * state is messed up, but if it is only polled in the single
1173 * BDRV_POLL_WHILE() at the end of the drain, this should work fine.
1175 static void test_detach_indirect(bool by_parent_cb
)
1178 BlockDriverState
*parent_a
, *parent_b
, *a
, *b
, *c
;
1179 BdrvChild
*child_a
, *child_b
;
1182 QEMUIOVector qiov
= QEMU_IOVEC_INIT_BUF(qiov
, NULL
, 0);
1184 if (!by_parent_cb
) {
1185 detach_by_driver_cb_class
= child_of_bds
;
1186 detach_by_driver_cb_class
.drained_begin
=
1187 detach_by_driver_cb_drained_begin
;
1188 detach_by_driver_cb_class
.drained_end
= NULL
;
1189 detach_by_driver_cb_class
.drained_poll
= NULL
;
1192 detach_by_parent_data
= (struct detach_by_parent_data
) {
1193 .detach_on_drain
= false,
1196 /* Create all involved nodes */
1197 parent_a
= bdrv_new_open_driver(&bdrv_test
, "parent-a", BDRV_O_RDWR
,
1199 parent_b
= bdrv_new_open_driver(&bdrv_test
, "parent-b", 0,
1202 a
= bdrv_new_open_driver(&bdrv_test
, "a", BDRV_O_RDWR
, &error_abort
);
1203 b
= bdrv_new_open_driver(&bdrv_test
, "b", BDRV_O_RDWR
, &error_abort
);
1204 c
= bdrv_new_open_driver(&bdrv_test
, "c", BDRV_O_RDWR
, &error_abort
);
1206 /* blk is a BB for parent-a */
1207 blk
= blk_new(qemu_get_aio_context(), BLK_PERM_ALL
, BLK_PERM_ALL
);
1208 blk_insert_bs(blk
, parent_a
, &error_abort
);
1209 bdrv_unref(parent_a
);
1211 /* If we want to get bdrv_drain_invoke() to call aio_poll(), the driver
1212 * callback must not return immediately. */
1213 if (!by_parent_cb
) {
1214 BDRVTestState
*s
= parent_a
->opaque
;
1215 s
->sleep_in_drain_begin
= true;
1218 /* Set child relationships */
1221 child_b
= bdrv_attach_child(parent_b
, b
, "PB-B", &child_of_bds
,
1222 BDRV_CHILD_DATA
, &error_abort
);
1223 child_a
= bdrv_attach_child(parent_b
, a
, "PB-A", &child_of_bds
,
1224 BDRV_CHILD_COW
, &error_abort
);
1227 bdrv_attach_child(parent_a
, a
, "PA-A",
1228 by_parent_cb
? &child_of_bds
: &detach_by_driver_cb_class
,
1229 BDRV_CHILD_DATA
, &error_abort
);
1231 g_assert_cmpint(parent_a
->refcnt
, ==, 1);
1232 g_assert_cmpint(parent_b
->refcnt
, ==, 1);
1233 g_assert_cmpint(a
->refcnt
, ==, 3);
1234 g_assert_cmpint(b
->refcnt
, ==, 2);
1235 g_assert_cmpint(c
->refcnt
, ==, 1);
1237 g_assert(QLIST_FIRST(&parent_b
->children
) == child_a
);
1238 g_assert(QLIST_NEXT(child_a
, next
) == child_b
);
1239 g_assert(QLIST_NEXT(child_b
, next
) == NULL
);
1241 /* Start the evil write request */
1242 detach_by_parent_data
= (struct detach_by_parent_data
) {
1243 .parent_b
= parent_b
,
1246 .by_parent_cb
= by_parent_cb
,
1247 .detach_on_drain
= true,
1249 acb
= blk_aio_preadv(blk
, 0, &qiov
, 0, detach_by_parent_aio_cb
, NULL
);
1250 g_assert(acb
!= NULL
);
1252 /* Drain and check the expected result */
1253 bdrv_drained_begin(parent_b
);
1254 bdrv_drained_begin(a
);
1255 bdrv_drained_begin(b
);
1256 bdrv_drained_begin(c
);
1258 g_assert(detach_by_parent_data
.child_c
!= NULL
);
1260 g_assert_cmpint(parent_a
->refcnt
, ==, 1);
1261 g_assert_cmpint(parent_b
->refcnt
, ==, 1);
1262 g_assert_cmpint(a
->refcnt
, ==, 3);
1263 g_assert_cmpint(b
->refcnt
, ==, 1);
1264 g_assert_cmpint(c
->refcnt
, ==, 2);
1266 g_assert(QLIST_FIRST(&parent_b
->children
) == detach_by_parent_data
.child_c
);
1267 g_assert(QLIST_NEXT(detach_by_parent_data
.child_c
, next
) == child_a
);
1268 g_assert(QLIST_NEXT(child_a
, next
) == NULL
);
1270 g_assert_cmpint(parent_a
->quiesce_counter
, ==, 1);
1271 g_assert_cmpint(parent_b
->quiesce_counter
, ==, 3);
1272 g_assert_cmpint(a
->quiesce_counter
, ==, 1);
1273 g_assert_cmpint(b
->quiesce_counter
, ==, 1);
1274 g_assert_cmpint(c
->quiesce_counter
, ==, 1);
1276 bdrv_drained_end(parent_b
);
1277 bdrv_drained_end(a
);
1278 bdrv_drained_end(b
);
1279 bdrv_drained_end(c
);
1281 bdrv_unref(parent_b
);
1284 g_assert_cmpint(a
->refcnt
, ==, 1);
1285 g_assert_cmpint(b
->refcnt
, ==, 1);
1286 g_assert_cmpint(c
->refcnt
, ==, 1);
1292 static void test_detach_by_parent_cb(void)
1294 test_detach_indirect(true);
1297 static void test_detach_by_driver_cb(void)
1299 test_detach_indirect(false);
1302 static void test_append_to_drained(void)
1305 BlockDriverState
*base
, *overlay
;
1306 BDRVTestState
*base_s
, *overlay_s
;
1308 blk
= blk_new(qemu_get_aio_context(), BLK_PERM_ALL
, BLK_PERM_ALL
);
1309 base
= bdrv_new_open_driver(&bdrv_test
, "base", BDRV_O_RDWR
, &error_abort
);
1310 base_s
= base
->opaque
;
1311 blk_insert_bs(blk
, base
, &error_abort
);
1313 overlay
= bdrv_new_open_driver(&bdrv_test
, "overlay", BDRV_O_RDWR
,
1315 overlay_s
= overlay
->opaque
;
1317 do_drain_begin(BDRV_DRAIN
, base
);
1318 g_assert_cmpint(base
->quiesce_counter
, ==, 1);
1319 g_assert_cmpint(base_s
->drain_count
, ==, 1);
1320 g_assert_cmpint(base
->in_flight
, ==, 0);
1322 bdrv_append(overlay
, base
, &error_abort
);
1323 g_assert_cmpint(base
->in_flight
, ==, 0);
1324 g_assert_cmpint(overlay
->in_flight
, ==, 0);
1326 g_assert_cmpint(base
->quiesce_counter
, ==, 1);
1327 g_assert_cmpint(base_s
->drain_count
, ==, 1);
1328 g_assert_cmpint(overlay
->quiesce_counter
, ==, 1);
1329 g_assert_cmpint(overlay_s
->drain_count
, ==, 1);
1331 do_drain_end(BDRV_DRAIN
, base
);
1333 g_assert_cmpint(base
->quiesce_counter
, ==, 0);
1334 g_assert_cmpint(base_s
->drain_count
, ==, 0);
1335 g_assert_cmpint(overlay
->quiesce_counter
, ==, 0);
1336 g_assert_cmpint(overlay_s
->drain_count
, ==, 0);
1338 bdrv_unref(overlay
);
1343 static void test_set_aio_context(void)
1345 BlockDriverState
*bs
;
1346 IOThread
*a
= iothread_new();
1347 IOThread
*b
= iothread_new();
1348 AioContext
*ctx_a
= iothread_get_aio_context(a
);
1349 AioContext
*ctx_b
= iothread_get_aio_context(b
);
1351 bs
= bdrv_new_open_driver(&bdrv_test
, "test-node", BDRV_O_RDWR
,
1354 bdrv_drained_begin(bs
);
1355 bdrv_try_change_aio_context(bs
, ctx_a
, NULL
, &error_abort
);
1357 aio_context_acquire(ctx_a
);
1358 bdrv_drained_end(bs
);
1360 bdrv_drained_begin(bs
);
1361 bdrv_try_change_aio_context(bs
, ctx_b
, NULL
, &error_abort
);
1362 aio_context_release(ctx_a
);
1363 aio_context_acquire(ctx_b
);
1364 bdrv_try_change_aio_context(bs
, qemu_get_aio_context(), NULL
, &error_abort
);
1365 aio_context_release(ctx_b
);
1366 bdrv_drained_end(bs
);
1374 typedef struct TestDropBackingBlockJob
{
1376 bool should_complete
;
1378 BlockDriverState
*detach_also
;
1379 BlockDriverState
*bs
;
1380 } TestDropBackingBlockJob
;
1382 static int coroutine_fn
test_drop_backing_job_run(Job
*job
, Error
**errp
)
1384 TestDropBackingBlockJob
*s
=
1385 container_of(job
, TestDropBackingBlockJob
, common
.job
);
1387 while (!s
->should_complete
) {
1388 job_sleep_ns(job
, 0);
1394 static void test_drop_backing_job_commit(Job
*job
)
1396 TestDropBackingBlockJob
*s
=
1397 container_of(job
, TestDropBackingBlockJob
, common
.job
);
1399 bdrv_set_backing_hd(s
->bs
, NULL
, &error_abort
);
1400 bdrv_set_backing_hd(s
->detach_also
, NULL
, &error_abort
);
1402 *s
->did_complete
= true;
1405 static const BlockJobDriver test_drop_backing_job_driver
= {
1407 .instance_size
= sizeof(TestDropBackingBlockJob
),
1408 .free
= block_job_free
,
1409 .user_resume
= block_job_user_resume
,
1410 .run
= test_drop_backing_job_run
,
1411 .commit
= test_drop_backing_job_commit
,
1416 * Creates a child node with three parent nodes on it, and then runs a
1417 * block job on the final one, parent-node-2.
1419 * The job is then asked to complete before a section where the child
1422 * Ending this section will undrain the child's parents, first
1423 * parent-node-2, then parent-node-1, then parent-node-0 -- the parent
1424 * list is in reverse order of how they were added. Ending the drain
1425 * on parent-node-2 will resume the job, thus completing it and
1426 * scheduling job_exit().
1428 * Ending the drain on parent-node-1 will poll the AioContext, which
1429 * lets job_exit() and thus test_drop_backing_job_commit() run. That
1430 * function first removes the child as parent-node-2's backing file.
1432 * In old (and buggy) implementations, there are two problems with
1434 * (A) bdrv_drain_invoke() polls for every node that leaves the
1435 * drained section. This means that job_exit() is scheduled
1436 * before the child has left the drained section. Its
1437 * quiesce_counter is therefore still 1 when it is removed from
1440 * (B) bdrv_replace_child_noperm() calls drained_end() on the old
1441 * child's parents as many times as the child is quiesced. This
1442 * means it will call drained_end() on parent-node-2 once.
1443 * Because parent-node-2 is no longer quiesced at this point, this
1446 * bdrv_replace_child_noperm() therefore must call drained_end() on
1447 * the parent only if it really is still drained because the child is
1450 * If removing child from parent-node-2 was successful (as it should
1451 * be), test_drop_backing_job_commit() will then also remove the child
1452 * from parent-node-0.
1454 * With an old version of our drain infrastructure ((A) above), that
1455 * resulted in the following flow:
1457 * 1. child attempts to leave its drained section. The call recurses
1460 * 2. parent-node-2 leaves the drained section. Polling in
1461 * bdrv_drain_invoke() will schedule job_exit().
1463 * 3. parent-node-1 leaves the drained section. Polling in
1464 * bdrv_drain_invoke() will run job_exit(), thus disconnecting
1465 * parent-node-0 from the child node.
1467 * 4. bdrv_parent_drained_end() uses a QLIST_FOREACH_SAFE() loop to
1468 * iterate over the parents. Thus, it now accesses the BdrvChild
1469 * object that used to connect parent-node-0 and the child node.
1470 * However, that object no longer exists, so it accesses a dangling
1473 * The solution is to only poll once when running a bdrv_drained_end()
1474 * operation, specifically at the end when all drained_end()
1475 * operations for all involved nodes have been scheduled.
1476 * Note that this also solves (A) above, thus hiding (B).
1478 static void test_blockjob_commit_by_drained_end(void)
1480 BlockDriverState
*bs_child
, *bs_parents
[3];
1481 TestDropBackingBlockJob
*job
;
1482 bool job_has_completed
= false;
1485 bs_child
= bdrv_new_open_driver(&bdrv_test
, "child-node", BDRV_O_RDWR
,
1488 for (i
= 0; i
< 3; i
++) {
1490 snprintf(name
, sizeof(name
), "parent-node-%i", i
);
1491 bs_parents
[i
] = bdrv_new_open_driver(&bdrv_test
, name
, BDRV_O_RDWR
,
1493 bdrv_set_backing_hd(bs_parents
[i
], bs_child
, &error_abort
);
1496 job
= block_job_create("job", &test_drop_backing_job_driver
, NULL
,
1497 bs_parents
[2], 0, BLK_PERM_ALL
, 0, 0, NULL
, NULL
,
1499 job
->bs
= bs_parents
[2];
1501 job
->detach_also
= bs_parents
[0];
1502 job
->did_complete
= &job_has_completed
;
1504 job_start(&job
->common
.job
);
1506 job
->should_complete
= true;
1507 bdrv_drained_begin(bs_child
);
1508 g_assert(!job_has_completed
);
1509 bdrv_drained_end(bs_child
);
1510 aio_poll(qemu_get_aio_context(), false);
1511 g_assert(job_has_completed
);
1513 bdrv_unref(bs_parents
[0]);
1514 bdrv_unref(bs_parents
[1]);
1515 bdrv_unref(bs_parents
[2]);
1516 bdrv_unref(bs_child
);
1520 typedef struct TestSimpleBlockJob
{
1522 bool should_complete
;
1524 } TestSimpleBlockJob
;
1526 static int coroutine_fn
test_simple_job_run(Job
*job
, Error
**errp
)
1528 TestSimpleBlockJob
*s
= container_of(job
, TestSimpleBlockJob
, common
.job
);
1530 while (!s
->should_complete
) {
1531 job_sleep_ns(job
, 0);
1537 static void test_simple_job_clean(Job
*job
)
1539 TestSimpleBlockJob
*s
= container_of(job
, TestSimpleBlockJob
, common
.job
);
1540 *s
->did_complete
= true;
1543 static const BlockJobDriver test_simple_job_driver
= {
1545 .instance_size
= sizeof(TestSimpleBlockJob
),
1546 .free
= block_job_free
,
1547 .user_resume
= block_job_user_resume
,
1548 .run
= test_simple_job_run
,
1549 .clean
= test_simple_job_clean
,
1553 static int drop_intermediate_poll_update_filename(BdrvChild
*child
,
1554 BlockDriverState
*new_base
,
1555 const char *filename
,
1559 * We are free to poll here, which may change the block graph, if
1560 * it is not drained.
1563 /* If the job is not drained: Complete it, schedule job_exit() */
1564 aio_poll(qemu_get_current_aio_context(), false);
1565 /* If the job is not drained: Run job_exit(), finish the job */
1566 aio_poll(qemu_get_current_aio_context(), false);
1572 * Test a poll in the midst of bdrv_drop_intermediate().
1574 * bdrv_drop_intermediate() calls BdrvChildClass.update_filename(),
1575 * which can yield or poll. This may lead to graph changes, unless
1576 * the whole subtree in question is drained.
1578 * We test this on the following graph:
1594 * node-2 --chain--> node-1 --chain--> node-0
1596 * We drop node-1 with bdrv_drop_intermediate(top=node-1, base=node-0).
1598 * This first updates node-2's backing filename by invoking
1599 * drop_intermediate_poll_update_filename(), which polls twice. This
1600 * causes the job to finish, which in turns causes the job-node to be
1603 * bdrv_drop_intermediate() uses a QLIST_FOREACH_SAFE() loop, so it
1604 * already has a pointer to the BdrvChild edge between job-node and
1605 * node-1. When it tries to handle that edge, we probably get a
1606 * segmentation fault because the object no longer exists.
1609 * The solution is for bdrv_drop_intermediate() to drain top's
1610 * subtree. This prevents graph changes from happening just because
1611 * BdrvChildClass.update_filename() yields or polls. Thus, the block
1612 * job is paused during that drained section and must finish before or
1615 * (In addition, bdrv_replace_child() must keep the job paused.)
1617 static void test_drop_intermediate_poll(void)
1619 static BdrvChildClass chain_child_class
;
1620 BlockDriverState
*chain
[3];
1621 TestSimpleBlockJob
*job
;
1622 BlockDriverState
*job_node
;
1623 bool job_has_completed
= false;
1627 chain_child_class
= child_of_bds
;
1628 chain_child_class
.update_filename
= drop_intermediate_poll_update_filename
;
1630 for (i
= 0; i
< 3; i
++) {
1632 snprintf(name
, 32, "node-%i", i
);
1634 chain
[i
] = bdrv_new_open_driver(&bdrv_test
, name
, 0, &error_abort
);
1637 job_node
= bdrv_new_open_driver(&bdrv_test
, "job-node", BDRV_O_RDWR
,
1639 bdrv_set_backing_hd(job_node
, chain
[1], &error_abort
);
1642 * Establish the chain last, so the chain links are the first
1643 * elements in the BDS.parents lists
1645 for (i
= 0; i
< 3; i
++) {
1647 /* Takes the reference to chain[i - 1] */
1648 bdrv_attach_child(chain
[i
], chain
[i
- 1], "chain",
1649 &chain_child_class
, BDRV_CHILD_COW
, &error_abort
);
1653 job
= block_job_create("job", &test_simple_job_driver
, NULL
, job_node
,
1654 0, BLK_PERM_ALL
, 0, 0, NULL
, NULL
, &error_abort
);
1656 /* The job has a reference now */
1657 bdrv_unref(job_node
);
1659 job
->did_complete
= &job_has_completed
;
1661 job_start(&job
->common
.job
);
1662 job
->should_complete
= true;
1664 g_assert(!job_has_completed
);
1665 ret
= bdrv_drop_intermediate(chain
[1], chain
[0], NULL
);
1666 aio_poll(qemu_get_aio_context(), false);
1668 g_assert(job_has_completed
);
1670 bdrv_unref(chain
[2]);
1674 typedef struct BDRVReplaceTestState
{
1675 bool setup_completed
;
1682 bool yield_before_read
;
1684 Coroutine
*drain_co
;
1685 } BDRVReplaceTestState
;
1687 static void bdrv_replace_test_close(BlockDriverState
*bs
)
1692 * If @bs has a backing file:
1693 * Yield if .yield_before_read is true (and wait for drain_begin to
1695 * Forward the read to bs->backing. Set .has_read to true.
1696 * If drain_begin has woken us, wake it in turn.
1699 * Set .has_read to true and return success.
1701 static int coroutine_fn
bdrv_replace_test_co_preadv(BlockDriverState
*bs
,
1705 BdrvRequestFlags flags
)
1707 BDRVReplaceTestState
*s
= bs
->opaque
;
1712 g_assert(!s
->drain_count
);
1714 s
->io_co
= qemu_coroutine_self();
1715 if (s
->yield_before_read
) {
1716 s
->yield_before_read
= false;
1717 qemu_coroutine_yield();
1721 ret
= bdrv_co_preadv(bs
->backing
, offset
, bytes
, qiov
, 0);
1724 /* Wake up drain_co if it runs */
1726 aio_co_wake(s
->drain_co
);
1736 static void coroutine_fn
bdrv_replace_test_drain_co(void *opaque
)
1738 BlockDriverState
*bs
= opaque
;
1739 BDRVReplaceTestState
*s
= bs
->opaque
;
1741 /* Keep waking io_co up until it is done */
1743 aio_co_wake(s
->io_co
);
1745 qemu_coroutine_yield();
1748 bdrv_dec_in_flight(bs
);
1752 * If .drain_count is 0, wake up .io_co if there is one; and set
1754 * Increment .drain_count.
1756 static void bdrv_replace_test_drain_begin(BlockDriverState
*bs
)
1758 BDRVReplaceTestState
*s
= bs
->opaque
;
1760 if (!s
->setup_completed
) {
1764 if (!s
->drain_count
) {
1765 s
->drain_co
= qemu_coroutine_create(bdrv_replace_test_drain_co
, bs
);
1766 bdrv_inc_in_flight(bs
);
1767 aio_co_enter(bdrv_get_aio_context(bs
), s
->drain_co
);
1768 s
->was_drained
= true;
1773 static void coroutine_fn
bdrv_replace_test_read_entry(void *opaque
)
1775 BlockDriverState
*bs
= opaque
;
1777 QEMUIOVector qiov
= QEMU_IOVEC_INIT_BUF(qiov
, &data
, 1);
1780 /* Queue a read request post-drain */
1781 ret
= bdrv_replace_test_co_preadv(bs
, 0, 1, &qiov
, 0);
1783 bdrv_dec_in_flight(bs
);
1787 * Reduce .drain_count, set .was_undrained once it reaches 0.
1788 * If .drain_count reaches 0 and the node has a backing file, issue a
1791 static void bdrv_replace_test_drain_end(BlockDriverState
*bs
)
1793 BDRVReplaceTestState
*s
= bs
->opaque
;
1795 if (!s
->setup_completed
) {
1799 g_assert(s
->drain_count
> 0);
1800 if (!--s
->drain_count
) {
1801 s
->was_undrained
= true;
1804 Coroutine
*co
= qemu_coroutine_create(bdrv_replace_test_read_entry
,
1806 bdrv_inc_in_flight(bs
);
1807 aio_co_enter(bdrv_get_aio_context(bs
), co
);
1812 static BlockDriver bdrv_replace_test
= {
1813 .format_name
= "replace_test",
1814 .instance_size
= sizeof(BDRVReplaceTestState
),
1815 .supports_backing
= true,
1817 .bdrv_close
= bdrv_replace_test_close
,
1818 .bdrv_co_preadv
= bdrv_replace_test_co_preadv
,
1820 .bdrv_drain_begin
= bdrv_replace_test_drain_begin
,
1821 .bdrv_drain_end
= bdrv_replace_test_drain_end
,
1823 .bdrv_child_perm
= bdrv_default_perms
,
1826 static void coroutine_fn
test_replace_child_mid_drain_read_co(void *opaque
)
1831 ret
= blk_co_pread(opaque
, 0, 1, &data
, 0);
1836 * We test two things:
1837 * (1) bdrv_replace_child_noperm() must not undrain the parent if both
1838 * children are drained.
1839 * (2) bdrv_replace_child_noperm() must never flush I/O requests to a
1840 * drained child. If the old child is drained, it must flush I/O
1841 * requests after the new one has been attached. If the new child
1842 * is drained, it must flush I/O requests before the old one is
1845 * To do so, we create one parent node and two child nodes; then
1846 * attach one of the children (old_child_bs) to the parent, then
1847 * drain both old_child_bs and new_child_bs according to
1848 * old_drain_count and new_drain_count, respectively, and finally
1849 * we invoke bdrv_replace_node() to replace old_child_bs by
1852 * The test block driver we use here (bdrv_replace_test) has a read
1854 * - For the parent node, can optionally yield, and then forwards the
1855 * read to bdrv_preadv(),
1856 * - For the child node, just returns immediately.
1858 * If the read yields, the drain_begin function will wake it up.
1860 * The drain_end function issues a read on the parent once it is fully
1861 * undrained (which simulates requests starting to come in again).
1863 static void do_test_replace_child_mid_drain(int old_drain_count
,
1864 int new_drain_count
)
1866 BlockBackend
*parent_blk
;
1867 BlockDriverState
*parent_bs
;
1868 BlockDriverState
*old_child_bs
, *new_child_bs
;
1869 BDRVReplaceTestState
*parent_s
;
1870 BDRVReplaceTestState
*old_child_s
, *new_child_s
;
1874 parent_bs
= bdrv_new_open_driver(&bdrv_replace_test
, "parent", 0,
1876 parent_s
= parent_bs
->opaque
;
1878 parent_blk
= blk_new(qemu_get_aio_context(),
1879 BLK_PERM_CONSISTENT_READ
, BLK_PERM_ALL
);
1880 blk_insert_bs(parent_blk
, parent_bs
, &error_abort
);
1882 old_child_bs
= bdrv_new_open_driver(&bdrv_replace_test
, "old-child", 0,
1884 new_child_bs
= bdrv_new_open_driver(&bdrv_replace_test
, "new-child", 0,
1886 old_child_s
= old_child_bs
->opaque
;
1887 new_child_s
= new_child_bs
->opaque
;
1889 /* So that we can read something */
1890 parent_bs
->total_sectors
= 1;
1891 old_child_bs
->total_sectors
= 1;
1892 new_child_bs
->total_sectors
= 1;
1894 bdrv_ref(old_child_bs
);
1895 bdrv_attach_child(parent_bs
, old_child_bs
, "child", &child_of_bds
,
1896 BDRV_CHILD_COW
, &error_abort
);
1897 parent_s
->setup_completed
= true;
1899 for (i
= 0; i
< old_drain_count
; i
++) {
1900 bdrv_drained_begin(old_child_bs
);
1902 for (i
= 0; i
< new_drain_count
; i
++) {
1903 bdrv_drained_begin(new_child_bs
);
1906 if (!old_drain_count
) {
1908 * Start a read operation that will yield, so it will not
1909 * complete before the node is drained.
1911 parent_s
->yield_before_read
= true;
1912 io_co
= qemu_coroutine_create(test_replace_child_mid_drain_read_co
,
1914 qemu_coroutine_enter(io_co
);
1917 /* If we have started a read operation, it should have yielded */
1918 g_assert(!parent_s
->has_read
);
1920 /* Reset drained status so we can see what bdrv_replace_node() does */
1921 parent_s
->was_drained
= false;
1922 parent_s
->was_undrained
= false;
1924 g_assert(parent_bs
->quiesce_counter
== old_drain_count
);
1925 bdrv_replace_node(old_child_bs
, new_child_bs
, &error_abort
);
1926 g_assert(parent_bs
->quiesce_counter
== new_drain_count
);
1928 if (!old_drain_count
&& !new_drain_count
) {
1930 * From undrained to undrained drains and undrains the parent,
1931 * because bdrv_replace_node() contains a drained section for
1934 g_assert(parent_s
->was_drained
&& parent_s
->was_undrained
);
1935 } else if (!old_drain_count
&& new_drain_count
) {
1937 * From undrained to drained should drain the parent and keep
1940 g_assert(parent_s
->was_drained
&& !parent_s
->was_undrained
);
1941 } else if (old_drain_count
&& !new_drain_count
) {
1943 * From drained to undrained should undrain the parent and
1946 g_assert(!parent_s
->was_drained
&& parent_s
->was_undrained
);
1947 } else /* if (old_drain_count && new_drain_count) */ {
1949 * From drained to drained must not undrain the parent at any
1952 g_assert(!parent_s
->was_drained
&& !parent_s
->was_undrained
);
1955 if (!old_drain_count
|| !new_drain_count
) {
1957 * If !old_drain_count, we have started a read request before
1958 * bdrv_replace_node(). If !new_drain_count, the parent must
1959 * have been undrained at some point, and
1960 * bdrv_replace_test_co_drain_end() starts a read request
1963 g_assert(parent_s
->has_read
);
1966 * If the parent was never undrained, there is no way to start
1969 g_assert(!parent_s
->has_read
);
1972 /* A drained child must have not received any request */
1973 g_assert(!(old_drain_count
&& old_child_s
->has_read
));
1974 g_assert(!(new_drain_count
&& new_child_s
->has_read
));
1976 for (i
= 0; i
< new_drain_count
; i
++) {
1977 bdrv_drained_end(new_child_bs
);
1979 for (i
= 0; i
< old_drain_count
; i
++) {
1980 bdrv_drained_end(old_child_bs
);
1984 * By now, bdrv_replace_test_co_drain_end() must have been called
1985 * at some point while the new child was attached to the parent.
1987 g_assert(parent_s
->has_read
);
1988 g_assert(new_child_s
->has_read
);
1990 blk_unref(parent_blk
);
1991 bdrv_unref(parent_bs
);
1992 bdrv_unref(old_child_bs
);
1993 bdrv_unref(new_child_bs
);
1996 static void test_replace_child_mid_drain(void)
1998 int old_drain_count
, new_drain_count
;
2000 for (old_drain_count
= 0; old_drain_count
< 2; old_drain_count
++) {
2001 for (new_drain_count
= 0; new_drain_count
< 2; new_drain_count
++) {
2002 do_test_replace_child_mid_drain(old_drain_count
, new_drain_count
);
2007 int main(int argc
, char **argv
)
2012 qemu_init_main_loop(&error_abort
);
2014 g_test_init(&argc
, &argv
, NULL
);
2015 qemu_event_init(&done_event
, false);
2017 g_test_add_func("/bdrv-drain/driver-cb/drain_all", test_drv_cb_drain_all
);
2018 g_test_add_func("/bdrv-drain/driver-cb/drain", test_drv_cb_drain
);
2020 g_test_add_func("/bdrv-drain/driver-cb/co/drain_all",
2021 test_drv_cb_co_drain_all
);
2022 g_test_add_func("/bdrv-drain/driver-cb/co/drain", test_drv_cb_co_drain
);
2024 g_test_add_func("/bdrv-drain/quiesce/drain_all", test_quiesce_drain_all
);
2025 g_test_add_func("/bdrv-drain/quiesce/drain", test_quiesce_drain
);
2027 g_test_add_func("/bdrv-drain/quiesce/co/drain_all",
2028 test_quiesce_co_drain_all
);
2029 g_test_add_func("/bdrv-drain/quiesce/co/drain", test_quiesce_co_drain
);
2031 g_test_add_func("/bdrv-drain/nested", test_nested
);
2033 g_test_add_func("/bdrv-drain/graph-change/drain_all",
2034 test_graph_change_drain_all
);
2036 g_test_add_func("/bdrv-drain/iothread/drain_all", test_iothread_drain_all
);
2037 g_test_add_func("/bdrv-drain/iothread/drain", test_iothread_drain
);
2039 g_test_add_func("/bdrv-drain/blockjob/drain_all", test_blockjob_drain_all
);
2040 g_test_add_func("/bdrv-drain/blockjob/drain", test_blockjob_drain
);
2042 g_test_add_func("/bdrv-drain/blockjob/error/drain_all",
2043 test_blockjob_error_drain_all
);
2044 g_test_add_func("/bdrv-drain/blockjob/error/drain",
2045 test_blockjob_error_drain
);
2047 g_test_add_func("/bdrv-drain/blockjob/iothread/drain_all",
2048 test_blockjob_iothread_drain_all
);
2049 g_test_add_func("/bdrv-drain/blockjob/iothread/drain",
2050 test_blockjob_iothread_drain
);
2052 g_test_add_func("/bdrv-drain/blockjob/iothread/error/drain_all",
2053 test_blockjob_iothread_error_drain_all
);
2054 g_test_add_func("/bdrv-drain/blockjob/iothread/error/drain",
2055 test_blockjob_iothread_error_drain
);
2057 g_test_add_func("/bdrv-drain/deletion/drain", test_delete_by_drain
);
2058 g_test_add_func("/bdrv-drain/detach/drain_all", test_detach_by_drain_all
);
2059 g_test_add_func("/bdrv-drain/detach/drain", test_detach_by_drain
);
2060 g_test_add_func("/bdrv-drain/detach/parent_cb", test_detach_by_parent_cb
);
2061 g_test_add_func("/bdrv-drain/detach/driver_cb", test_detach_by_driver_cb
);
2063 g_test_add_func("/bdrv-drain/attach/drain", test_append_to_drained
);
2065 g_test_add_func("/bdrv-drain/set_aio_context", test_set_aio_context
);
2067 g_test_add_func("/bdrv-drain/blockjob/commit_by_drained_end",
2068 test_blockjob_commit_by_drained_end
);
2070 g_test_add_func("/bdrv-drain/bdrv_drop_intermediate/poll",
2071 test_drop_intermediate_poll
);
2073 g_test_add_func("/bdrv-drain/replace_child/mid-drain",
2074 test_replace_child_mid_drain
);
2077 qemu_event_destroy(&done_event
);