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.h"
27 #include "block/blockjob_int.h"
28 #include "sysemu/block-backend.h"
29 #include "qapi/error.h"
32 static QemuEvent done_event
;
34 typedef struct BDRVTestState
{
36 AioContext
*bh_indirection_ctx
;
39 static void coroutine_fn
bdrv_test_co_drain_begin(BlockDriverState
*bs
)
41 BDRVTestState
*s
= bs
->opaque
;
45 static void coroutine_fn
bdrv_test_co_drain_end(BlockDriverState
*bs
)
47 BDRVTestState
*s
= bs
->opaque
;
51 static void bdrv_test_close(BlockDriverState
*bs
)
53 BDRVTestState
*s
= bs
->opaque
;
54 g_assert_cmpint(s
->drain_count
, >, 0);
57 static void co_reenter_bh(void *opaque
)
62 static int coroutine_fn
bdrv_test_co_preadv(BlockDriverState
*bs
,
63 uint64_t offset
, uint64_t bytes
,
64 QEMUIOVector
*qiov
, int flags
)
66 BDRVTestState
*s
= bs
->opaque
;
68 /* We want this request to stay until the polling loop in drain waits for
69 * it to complete. We need to sleep a while as bdrv_drain_invoke() comes
70 * first and polls its result, too, but it shouldn't accidentally complete
71 * this request yet. */
72 qemu_co_sleep_ns(QEMU_CLOCK_REALTIME
, 100000);
74 if (s
->bh_indirection_ctx
) {
75 aio_bh_schedule_oneshot(s
->bh_indirection_ctx
, co_reenter_bh
,
76 qemu_coroutine_self());
77 qemu_coroutine_yield();
83 static BlockDriver bdrv_test
= {
84 .format_name
= "test",
85 .instance_size
= sizeof(BDRVTestState
),
87 .bdrv_close
= bdrv_test_close
,
88 .bdrv_co_preadv
= bdrv_test_co_preadv
,
90 .bdrv_co_drain_begin
= bdrv_test_co_drain_begin
,
91 .bdrv_co_drain_end
= bdrv_test_co_drain_end
,
93 .bdrv_child_perm
= bdrv_format_default_perms
,
96 static void aio_ret_cb(void *opaque
, int ret
)
98 int *aio_ret
= opaque
;
102 typedef struct CallInCoroutineData
{
105 } CallInCoroutineData
;
107 static coroutine_fn
void call_in_coroutine_entry(void *opaque
)
109 CallInCoroutineData
*data
= opaque
;
115 static void call_in_coroutine(void (*entry
)(void))
118 CallInCoroutineData data
= {
123 co
= qemu_coroutine_create(call_in_coroutine_entry
, &data
);
124 qemu_coroutine_enter(co
);
126 aio_poll(qemu_get_aio_context(), true);
137 static void do_drain_begin(enum drain_type drain_type
, BlockDriverState
*bs
)
139 switch (drain_type
) {
140 case BDRV_DRAIN_ALL
: bdrv_drain_all_begin(); break;
141 case BDRV_DRAIN
: bdrv_drained_begin(bs
); break;
142 case BDRV_SUBTREE_DRAIN
: bdrv_subtree_drained_begin(bs
); break;
143 default: g_assert_not_reached();
147 static void do_drain_end(enum drain_type drain_type
, BlockDriverState
*bs
)
149 switch (drain_type
) {
150 case BDRV_DRAIN_ALL
: bdrv_drain_all_end(); break;
151 case BDRV_DRAIN
: bdrv_drained_end(bs
); break;
152 case BDRV_SUBTREE_DRAIN
: bdrv_subtree_drained_end(bs
); break;
153 default: g_assert_not_reached();
157 static void test_drv_cb_common(enum drain_type drain_type
, bool recursive
)
160 BlockDriverState
*bs
, *backing
;
161 BDRVTestState
*s
, *backing_s
;
170 qemu_iovec_init_external(&qiov
, &iov
, 1);
172 blk
= blk_new(BLK_PERM_ALL
, BLK_PERM_ALL
);
173 bs
= bdrv_new_open_driver(&bdrv_test
, "test-node", BDRV_O_RDWR
,
176 blk_insert_bs(blk
, bs
, &error_abort
);
178 backing
= bdrv_new_open_driver(&bdrv_test
, "backing", 0, &error_abort
);
179 backing_s
= backing
->opaque
;
180 bdrv_set_backing_hd(bs
, backing
, &error_abort
);
182 /* Simple bdrv_drain_all_begin/end pair, check that CBs are called */
183 g_assert_cmpint(s
->drain_count
, ==, 0);
184 g_assert_cmpint(backing_s
->drain_count
, ==, 0);
186 do_drain_begin(drain_type
, bs
);
188 g_assert_cmpint(s
->drain_count
, ==, 1);
189 g_assert_cmpint(backing_s
->drain_count
, ==, !!recursive
);
191 do_drain_end(drain_type
, bs
);
193 g_assert_cmpint(s
->drain_count
, ==, 0);
194 g_assert_cmpint(backing_s
->drain_count
, ==, 0);
196 /* Now do the same while a request is pending */
197 aio_ret
= -EINPROGRESS
;
198 acb
= blk_aio_preadv(blk
, 0, &qiov
, 0, aio_ret_cb
, &aio_ret
);
199 g_assert(acb
!= NULL
);
200 g_assert_cmpint(aio_ret
, ==, -EINPROGRESS
);
202 g_assert_cmpint(s
->drain_count
, ==, 0);
203 g_assert_cmpint(backing_s
->drain_count
, ==, 0);
205 do_drain_begin(drain_type
, bs
);
207 g_assert_cmpint(aio_ret
, ==, 0);
208 g_assert_cmpint(s
->drain_count
, ==, 1);
209 g_assert_cmpint(backing_s
->drain_count
, ==, !!recursive
);
211 do_drain_end(drain_type
, bs
);
213 g_assert_cmpint(s
->drain_count
, ==, 0);
214 g_assert_cmpint(backing_s
->drain_count
, ==, 0);
221 static void test_drv_cb_drain_all(void)
223 test_drv_cb_common(BDRV_DRAIN_ALL
, true);
226 static void test_drv_cb_drain(void)
228 test_drv_cb_common(BDRV_DRAIN
, false);
231 static void test_drv_cb_drain_subtree(void)
233 test_drv_cb_common(BDRV_SUBTREE_DRAIN
, true);
236 static void test_drv_cb_co_drain_all(void)
238 call_in_coroutine(test_drv_cb_drain_all
);
241 static void test_drv_cb_co_drain(void)
243 call_in_coroutine(test_drv_cb_drain
);
246 static void test_drv_cb_co_drain_subtree(void)
248 call_in_coroutine(test_drv_cb_drain_subtree
);
251 static void test_quiesce_common(enum drain_type drain_type
, bool recursive
)
254 BlockDriverState
*bs
, *backing
;
256 blk
= blk_new(BLK_PERM_ALL
, BLK_PERM_ALL
);
257 bs
= bdrv_new_open_driver(&bdrv_test
, "test-node", BDRV_O_RDWR
,
259 blk_insert_bs(blk
, bs
, &error_abort
);
261 backing
= bdrv_new_open_driver(&bdrv_test
, "backing", 0, &error_abort
);
262 bdrv_set_backing_hd(bs
, backing
, &error_abort
);
264 g_assert_cmpint(bs
->quiesce_counter
, ==, 0);
265 g_assert_cmpint(backing
->quiesce_counter
, ==, 0);
267 do_drain_begin(drain_type
, bs
);
269 g_assert_cmpint(bs
->quiesce_counter
, ==, 1);
270 g_assert_cmpint(backing
->quiesce_counter
, ==, !!recursive
);
272 do_drain_end(drain_type
, bs
);
274 g_assert_cmpint(bs
->quiesce_counter
, ==, 0);
275 g_assert_cmpint(backing
->quiesce_counter
, ==, 0);
282 static void test_quiesce_drain_all(void)
284 test_quiesce_common(BDRV_DRAIN_ALL
, true);
287 static void test_quiesce_drain(void)
289 test_quiesce_common(BDRV_DRAIN
, false);
292 static void test_quiesce_drain_subtree(void)
294 test_quiesce_common(BDRV_SUBTREE_DRAIN
, true);
297 static void test_quiesce_co_drain_all(void)
299 call_in_coroutine(test_quiesce_drain_all
);
302 static void test_quiesce_co_drain(void)
304 call_in_coroutine(test_quiesce_drain
);
307 static void test_quiesce_co_drain_subtree(void)
309 call_in_coroutine(test_quiesce_drain_subtree
);
312 static void test_nested(void)
315 BlockDriverState
*bs
, *backing
;
316 BDRVTestState
*s
, *backing_s
;
317 enum drain_type outer
, inner
;
319 blk
= blk_new(BLK_PERM_ALL
, BLK_PERM_ALL
);
320 bs
= bdrv_new_open_driver(&bdrv_test
, "test-node", BDRV_O_RDWR
,
323 blk_insert_bs(blk
, bs
, &error_abort
);
325 backing
= bdrv_new_open_driver(&bdrv_test
, "backing", 0, &error_abort
);
326 backing_s
= backing
->opaque
;
327 bdrv_set_backing_hd(bs
, backing
, &error_abort
);
329 for (outer
= 0; outer
< DRAIN_TYPE_MAX
; outer
++) {
330 for (inner
= 0; inner
< DRAIN_TYPE_MAX
; inner
++) {
331 int backing_quiesce
= (outer
!= BDRV_DRAIN
) +
332 (inner
!= BDRV_DRAIN
);
334 g_assert_cmpint(bs
->quiesce_counter
, ==, 0);
335 g_assert_cmpint(backing
->quiesce_counter
, ==, 0);
336 g_assert_cmpint(s
->drain_count
, ==, 0);
337 g_assert_cmpint(backing_s
->drain_count
, ==, 0);
339 do_drain_begin(outer
, bs
);
340 do_drain_begin(inner
, bs
);
342 g_assert_cmpint(bs
->quiesce_counter
, ==, 2);
343 g_assert_cmpint(backing
->quiesce_counter
, ==, backing_quiesce
);
344 g_assert_cmpint(s
->drain_count
, ==, 2);
345 g_assert_cmpint(backing_s
->drain_count
, ==, backing_quiesce
);
347 do_drain_end(inner
, bs
);
348 do_drain_end(outer
, bs
);
350 g_assert_cmpint(bs
->quiesce_counter
, ==, 0);
351 g_assert_cmpint(backing
->quiesce_counter
, ==, 0);
352 g_assert_cmpint(s
->drain_count
, ==, 0);
353 g_assert_cmpint(backing_s
->drain_count
, ==, 0);
362 static void test_multiparent(void)
364 BlockBackend
*blk_a
, *blk_b
;
365 BlockDriverState
*bs_a
, *bs_b
, *backing
;
366 BDRVTestState
*a_s
, *b_s
, *backing_s
;
368 blk_a
= blk_new(BLK_PERM_ALL
, BLK_PERM_ALL
);
369 bs_a
= bdrv_new_open_driver(&bdrv_test
, "test-node-a", BDRV_O_RDWR
,
372 blk_insert_bs(blk_a
, bs_a
, &error_abort
);
374 blk_b
= blk_new(BLK_PERM_ALL
, BLK_PERM_ALL
);
375 bs_b
= bdrv_new_open_driver(&bdrv_test
, "test-node-b", BDRV_O_RDWR
,
378 blk_insert_bs(blk_b
, bs_b
, &error_abort
);
380 backing
= bdrv_new_open_driver(&bdrv_test
, "backing", 0, &error_abort
);
381 backing_s
= backing
->opaque
;
382 bdrv_set_backing_hd(bs_a
, backing
, &error_abort
);
383 bdrv_set_backing_hd(bs_b
, backing
, &error_abort
);
385 g_assert_cmpint(bs_a
->quiesce_counter
, ==, 0);
386 g_assert_cmpint(bs_b
->quiesce_counter
, ==, 0);
387 g_assert_cmpint(backing
->quiesce_counter
, ==, 0);
388 g_assert_cmpint(a_s
->drain_count
, ==, 0);
389 g_assert_cmpint(b_s
->drain_count
, ==, 0);
390 g_assert_cmpint(backing_s
->drain_count
, ==, 0);
392 do_drain_begin(BDRV_SUBTREE_DRAIN
, bs_a
);
394 g_assert_cmpint(bs_a
->quiesce_counter
, ==, 1);
395 g_assert_cmpint(bs_b
->quiesce_counter
, ==, 1);
396 g_assert_cmpint(backing
->quiesce_counter
, ==, 1);
397 g_assert_cmpint(a_s
->drain_count
, ==, 1);
398 g_assert_cmpint(b_s
->drain_count
, ==, 1);
399 g_assert_cmpint(backing_s
->drain_count
, ==, 1);
401 do_drain_begin(BDRV_SUBTREE_DRAIN
, bs_b
);
403 g_assert_cmpint(bs_a
->quiesce_counter
, ==, 2);
404 g_assert_cmpint(bs_b
->quiesce_counter
, ==, 2);
405 g_assert_cmpint(backing
->quiesce_counter
, ==, 2);
406 g_assert_cmpint(a_s
->drain_count
, ==, 2);
407 g_assert_cmpint(b_s
->drain_count
, ==, 2);
408 g_assert_cmpint(backing_s
->drain_count
, ==, 2);
410 do_drain_end(BDRV_SUBTREE_DRAIN
, bs_b
);
412 g_assert_cmpint(bs_a
->quiesce_counter
, ==, 1);
413 g_assert_cmpint(bs_b
->quiesce_counter
, ==, 1);
414 g_assert_cmpint(backing
->quiesce_counter
, ==, 1);
415 g_assert_cmpint(a_s
->drain_count
, ==, 1);
416 g_assert_cmpint(b_s
->drain_count
, ==, 1);
417 g_assert_cmpint(backing_s
->drain_count
, ==, 1);
419 do_drain_end(BDRV_SUBTREE_DRAIN
, bs_a
);
421 g_assert_cmpint(bs_a
->quiesce_counter
, ==, 0);
422 g_assert_cmpint(bs_b
->quiesce_counter
, ==, 0);
423 g_assert_cmpint(backing
->quiesce_counter
, ==, 0);
424 g_assert_cmpint(a_s
->drain_count
, ==, 0);
425 g_assert_cmpint(b_s
->drain_count
, ==, 0);
426 g_assert_cmpint(backing_s
->drain_count
, ==, 0);
435 static void test_graph_change(void)
437 BlockBackend
*blk_a
, *blk_b
;
438 BlockDriverState
*bs_a
, *bs_b
, *backing
;
439 BDRVTestState
*a_s
, *b_s
, *backing_s
;
441 blk_a
= blk_new(BLK_PERM_ALL
, BLK_PERM_ALL
);
442 bs_a
= bdrv_new_open_driver(&bdrv_test
, "test-node-a", BDRV_O_RDWR
,
445 blk_insert_bs(blk_a
, bs_a
, &error_abort
);
447 blk_b
= blk_new(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 backing
= bdrv_new_open_driver(&bdrv_test
, "backing", 0, &error_abort
);
454 backing_s
= backing
->opaque
;
455 bdrv_set_backing_hd(bs_a
, backing
, &error_abort
);
457 g_assert_cmpint(bs_a
->quiesce_counter
, ==, 0);
458 g_assert_cmpint(bs_b
->quiesce_counter
, ==, 0);
459 g_assert_cmpint(backing
->quiesce_counter
, ==, 0);
460 g_assert_cmpint(a_s
->drain_count
, ==, 0);
461 g_assert_cmpint(b_s
->drain_count
, ==, 0);
462 g_assert_cmpint(backing_s
->drain_count
, ==, 0);
464 do_drain_begin(BDRV_SUBTREE_DRAIN
, bs_a
);
465 do_drain_begin(BDRV_SUBTREE_DRAIN
, bs_a
);
466 do_drain_begin(BDRV_SUBTREE_DRAIN
, bs_a
);
467 do_drain_begin(BDRV_SUBTREE_DRAIN
, bs_b
);
468 do_drain_begin(BDRV_SUBTREE_DRAIN
, bs_b
);
470 bdrv_set_backing_hd(bs_b
, backing
, &error_abort
);
471 g_assert_cmpint(bs_a
->quiesce_counter
, ==, 5);
472 g_assert_cmpint(bs_b
->quiesce_counter
, ==, 5);
473 g_assert_cmpint(backing
->quiesce_counter
, ==, 5);
474 g_assert_cmpint(a_s
->drain_count
, ==, 5);
475 g_assert_cmpint(b_s
->drain_count
, ==, 5);
476 g_assert_cmpint(backing_s
->drain_count
, ==, 5);
478 bdrv_set_backing_hd(bs_b
, NULL
, &error_abort
);
479 g_assert_cmpint(bs_a
->quiesce_counter
, ==, 3);
480 g_assert_cmpint(bs_b
->quiesce_counter
, ==, 2);
481 g_assert_cmpint(backing
->quiesce_counter
, ==, 3);
482 g_assert_cmpint(a_s
->drain_count
, ==, 3);
483 g_assert_cmpint(b_s
->drain_count
, ==, 2);
484 g_assert_cmpint(backing_s
->drain_count
, ==, 3);
486 bdrv_set_backing_hd(bs_b
, backing
, &error_abort
);
487 g_assert_cmpint(bs_a
->quiesce_counter
, ==, 5);
488 g_assert_cmpint(bs_b
->quiesce_counter
, ==, 5);
489 g_assert_cmpint(backing
->quiesce_counter
, ==, 5);
490 g_assert_cmpint(a_s
->drain_count
, ==, 5);
491 g_assert_cmpint(b_s
->drain_count
, ==, 5);
492 g_assert_cmpint(backing_s
->drain_count
, ==, 5);
494 do_drain_end(BDRV_SUBTREE_DRAIN
, bs_b
);
495 do_drain_end(BDRV_SUBTREE_DRAIN
, bs_b
);
496 do_drain_end(BDRV_SUBTREE_DRAIN
, bs_a
);
497 do_drain_end(BDRV_SUBTREE_DRAIN
, bs_a
);
498 do_drain_end(BDRV_SUBTREE_DRAIN
, bs_a
);
500 g_assert_cmpint(bs_a
->quiesce_counter
, ==, 0);
501 g_assert_cmpint(bs_b
->quiesce_counter
, ==, 0);
502 g_assert_cmpint(backing
->quiesce_counter
, ==, 0);
503 g_assert_cmpint(a_s
->drain_count
, ==, 0);
504 g_assert_cmpint(b_s
->drain_count
, ==, 0);
505 g_assert_cmpint(backing_s
->drain_count
, ==, 0);
514 struct test_iothread_data
{
515 BlockDriverState
*bs
;
516 enum drain_type drain_type
;
520 static void test_iothread_drain_entry(void *opaque
)
522 struct test_iothread_data
*data
= opaque
;
524 aio_context_acquire(bdrv_get_aio_context(data
->bs
));
525 do_drain_begin(data
->drain_type
, data
->bs
);
526 g_assert_cmpint(*data
->aio_ret
, ==, 0);
527 do_drain_end(data
->drain_type
, data
->bs
);
528 aio_context_release(bdrv_get_aio_context(data
->bs
));
530 qemu_event_set(&done_event
);
533 static void test_iothread_aio_cb(void *opaque
, int ret
)
535 int *aio_ret
= opaque
;
537 qemu_event_set(&done_event
);
541 * Starts an AIO request on a BDS that runs in the AioContext of iothread 1.
542 * The request involves a BH on iothread 2 before it can complete.
544 * @drain_thread = 0 means that do_drain_begin/end are called from the main
545 * thread, @drain_thread = 1 means that they are called from iothread 1. Drain
546 * for this BDS cannot be called from iothread 2 because only the main thread
547 * may do cross-AioContext polling.
549 static void test_iothread_common(enum drain_type drain_type
, int drain_thread
)
552 BlockDriverState
*bs
;
556 struct test_iothread_data data
;
558 IOThread
*a
= iothread_new();
559 IOThread
*b
= iothread_new();
560 AioContext
*ctx_a
= iothread_get_aio_context(a
);
561 AioContext
*ctx_b
= iothread_get_aio_context(b
);
568 qemu_iovec_init_external(&qiov
, &iov
, 1);
570 /* bdrv_drain_all() may only be called from the main loop thread */
571 if (drain_type
== BDRV_DRAIN_ALL
&& drain_thread
!= 0) {
575 blk
= blk_new(BLK_PERM_ALL
, BLK_PERM_ALL
);
576 bs
= bdrv_new_open_driver(&bdrv_test
, "test-node", BDRV_O_RDWR
,
579 blk_insert_bs(blk
, bs
, &error_abort
);
581 blk_set_aio_context(blk
, ctx_a
);
582 aio_context_acquire(ctx_a
);
584 s
->bh_indirection_ctx
= ctx_b
;
586 aio_ret
= -EINPROGRESS
;
587 if (drain_thread
== 0) {
588 acb
= blk_aio_preadv(blk
, 0, &qiov
, 0, test_iothread_aio_cb
, &aio_ret
);
590 acb
= blk_aio_preadv(blk
, 0, &qiov
, 0, aio_ret_cb
, &aio_ret
);
592 g_assert(acb
!= NULL
);
593 g_assert_cmpint(aio_ret
, ==, -EINPROGRESS
);
595 aio_context_release(ctx_a
);
597 data
= (struct test_iothread_data
) {
599 .drain_type
= drain_type
,
603 switch (drain_thread
) {
605 if (drain_type
!= BDRV_DRAIN_ALL
) {
606 aio_context_acquire(ctx_a
);
609 /* The request is running on the IOThread a. Draining its block device
610 * will make sure that it has completed as far as the BDS is concerned,
611 * but the drain in this thread can continue immediately after
612 * bdrv_dec_in_flight() and aio_ret might be assigned only slightly
614 qemu_event_reset(&done_event
);
615 do_drain_begin(drain_type
, bs
);
616 g_assert_cmpint(bs
->in_flight
, ==, 0);
618 if (drain_type
!= BDRV_DRAIN_ALL
) {
619 aio_context_release(ctx_a
);
621 qemu_event_wait(&done_event
);
622 if (drain_type
!= BDRV_DRAIN_ALL
) {
623 aio_context_acquire(ctx_a
);
626 g_assert_cmpint(aio_ret
, ==, 0);
627 do_drain_end(drain_type
, bs
);
629 if (drain_type
!= BDRV_DRAIN_ALL
) {
630 aio_context_release(ctx_a
);
634 qemu_event_reset(&done_event
);
635 aio_bh_schedule_oneshot(ctx_a
, test_iothread_drain_entry
, &data
);
636 qemu_event_wait(&done_event
);
639 g_assert_not_reached();
642 aio_context_acquire(ctx_a
);
643 blk_set_aio_context(blk
, qemu_get_aio_context());
644 aio_context_release(ctx_a
);
654 static void test_iothread_drain_all(void)
656 test_iothread_common(BDRV_DRAIN_ALL
, 0);
657 test_iothread_common(BDRV_DRAIN_ALL
, 1);
660 static void test_iothread_drain(void)
662 test_iothread_common(BDRV_DRAIN
, 0);
663 test_iothread_common(BDRV_DRAIN
, 1);
666 static void test_iothread_drain_subtree(void)
668 test_iothread_common(BDRV_SUBTREE_DRAIN
, 0);
669 test_iothread_common(BDRV_SUBTREE_DRAIN
, 1);
673 typedef struct TestBlockJob
{
675 bool should_complete
;
678 static void test_job_completed(Job
*job
, void *opaque
)
680 job_completed(job
, 0, NULL
);
683 static void coroutine_fn
test_job_start(void *opaque
)
685 TestBlockJob
*s
= opaque
;
687 job_transition_to_ready(&s
->common
.job
);
688 while (!s
->should_complete
) {
689 /* Avoid block_job_sleep_ns() because it marks the job as !busy. We
690 * want to emulate some actual activity (probably some I/O) here so
691 * that drain has to wait for this acitivity to stop. */
692 qemu_co_sleep_ns(QEMU_CLOCK_REALTIME
, 100000);
693 job_pause_point(&s
->common
.job
);
696 job_defer_to_main_loop(&s
->common
.job
, test_job_completed
, NULL
);
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
,
710 .drain
= block_job_drain
,
711 .start
= test_job_start
,
712 .complete
= test_job_complete
,
716 static void test_blockjob_common(enum drain_type drain_type
)
718 BlockBackend
*blk_src
, *blk_target
;
719 BlockDriverState
*src
, *target
;
723 src
= bdrv_new_open_driver(&bdrv_test
, "source", BDRV_O_RDWR
,
725 blk_src
= blk_new(BLK_PERM_ALL
, BLK_PERM_ALL
);
726 blk_insert_bs(blk_src
, src
, &error_abort
);
728 target
= bdrv_new_open_driver(&bdrv_test
, "target", BDRV_O_RDWR
,
730 blk_target
= blk_new(BLK_PERM_ALL
, BLK_PERM_ALL
);
731 blk_insert_bs(blk_target
, target
, &error_abort
);
733 job
= block_job_create("job0", &test_job_driver
, NULL
, src
, 0, BLK_PERM_ALL
,
734 0, 0, NULL
, NULL
, &error_abort
);
735 block_job_add_bdrv(job
, "target", target
, 0, BLK_PERM_ALL
, &error_abort
);
736 job_start(&job
->job
);
738 g_assert_cmpint(job
->job
.pause_count
, ==, 0);
739 g_assert_false(job
->job
.paused
);
740 g_assert_true(job
->job
.busy
); /* We're in job_sleep_ns() */
742 do_drain_begin(drain_type
, src
);
744 if (drain_type
== BDRV_DRAIN_ALL
) {
745 /* bdrv_drain_all() drains both src and target */
746 g_assert_cmpint(job
->job
.pause_count
, ==, 2);
748 g_assert_cmpint(job
->job
.pause_count
, ==, 1);
750 g_assert_true(job
->job
.paused
);
751 g_assert_false(job
->job
.busy
); /* The job is paused */
753 do_drain_end(drain_type
, src
);
755 g_assert_cmpint(job
->job
.pause_count
, ==, 0);
756 g_assert_false(job
->job
.paused
);
757 g_assert_true(job
->job
.busy
); /* We're in qemu_co_sleep_ns() */
759 do_drain_begin(drain_type
, target
);
761 if (drain_type
== BDRV_DRAIN_ALL
) {
762 /* bdrv_drain_all() drains both src and target */
763 g_assert_cmpint(job
->job
.pause_count
, ==, 2);
765 g_assert_cmpint(job
->job
.pause_count
, ==, 1);
767 g_assert_true(job
->job
.paused
);
768 g_assert_false(job
->job
.busy
); /* The job is paused */
770 do_drain_end(drain_type
, target
);
772 g_assert_cmpint(job
->job
.pause_count
, ==, 0);
773 g_assert_false(job
->job
.paused
);
774 g_assert_true(job
->job
.busy
); /* We're in job_sleep_ns() */
776 ret
= job_complete_sync(&job
->job
, &error_abort
);
777 g_assert_cmpint(ret
, ==, 0);
780 blk_unref(blk_target
);
785 static void test_blockjob_drain_all(void)
787 test_blockjob_common(BDRV_DRAIN_ALL
);
790 static void test_blockjob_drain(void)
792 test_blockjob_common(BDRV_DRAIN
);
795 static void test_blockjob_drain_subtree(void)
797 test_blockjob_common(BDRV_SUBTREE_DRAIN
);
801 typedef struct BDRVTestTopState
{
802 BdrvChild
*wait_child
;
805 static void bdrv_test_top_close(BlockDriverState
*bs
)
807 BdrvChild
*c
, *next_c
;
808 QLIST_FOREACH_SAFE(c
, &bs
->children
, next
, next_c
) {
809 bdrv_unref_child(bs
, c
);
813 static int coroutine_fn
bdrv_test_top_co_preadv(BlockDriverState
*bs
,
814 uint64_t offset
, uint64_t bytes
,
815 QEMUIOVector
*qiov
, int flags
)
817 BDRVTestTopState
*tts
= bs
->opaque
;
818 return bdrv_co_preadv(tts
->wait_child
, offset
, bytes
, qiov
, flags
);
821 static BlockDriver bdrv_test_top_driver
= {
822 .format_name
= "test_top_driver",
823 .instance_size
= sizeof(BDRVTestTopState
),
825 .bdrv_close
= bdrv_test_top_close
,
826 .bdrv_co_preadv
= bdrv_test_top_co_preadv
,
828 .bdrv_child_perm
= bdrv_format_default_perms
,
831 typedef struct TestCoDeleteByDrainData
{
833 bool detach_instead_of_delete
;
835 } TestCoDeleteByDrainData
;
837 static void coroutine_fn
test_co_delete_by_drain(void *opaque
)
839 TestCoDeleteByDrainData
*dbdd
= opaque
;
840 BlockBackend
*blk
= dbdd
->blk
;
841 BlockDriverState
*bs
= blk_bs(blk
);
842 BDRVTestTopState
*tts
= bs
->opaque
;
843 void *buffer
= g_malloc(65536);
850 qemu_iovec_init_external(&qiov
, &iov
, 1);
852 /* Pretend some internal write operation from parent to child.
853 * Important: We have to read from the child, not from the parent!
854 * Draining works by first propagating it all up the tree to the
855 * root and then waiting for drainage from root to the leaves
856 * (protocol nodes). If we have a request waiting on the root,
857 * everything will be drained before we go back down the tree, but
858 * we do not want that. We want to be in the middle of draining
859 * when this following requests returns. */
860 bdrv_co_preadv(tts
->wait_child
, 0, 65536, &qiov
, 0);
862 g_assert_cmpint(bs
->refcnt
, ==, 1);
864 if (!dbdd
->detach_instead_of_delete
) {
867 BdrvChild
*c
, *next_c
;
868 QLIST_FOREACH_SAFE(c
, &bs
->children
, next
, next_c
) {
869 bdrv_unref_child(bs
, c
);
877 * Test what happens when some BDS has some children, you drain one of
878 * them and this results in the BDS being deleted.
880 * If @detach_instead_of_delete is set, the BDS is not going to be
881 * deleted but will only detach all of its children.
883 static void do_test_delete_by_drain(bool detach_instead_of_delete
,
884 enum drain_type drain_type
)
887 BlockDriverState
*bs
, *child_bs
, *null_bs
;
888 BDRVTestTopState
*tts
;
889 TestCoDeleteByDrainData dbdd
;
892 bs
= bdrv_new_open_driver(&bdrv_test_top_driver
, "top", BDRV_O_RDWR
,
894 bs
->total_sectors
= 65536 >> BDRV_SECTOR_BITS
;
897 null_bs
= bdrv_open("null-co://", NULL
, NULL
, BDRV_O_RDWR
| BDRV_O_PROTOCOL
,
899 bdrv_attach_child(bs
, null_bs
, "null-child", &child_file
, &error_abort
);
901 /* This child will be the one to pass to requests through to, and
902 * it will stall until a drain occurs */
903 child_bs
= bdrv_new_open_driver(&bdrv_test
, "child", BDRV_O_RDWR
,
905 child_bs
->total_sectors
= 65536 >> BDRV_SECTOR_BITS
;
906 /* Takes our reference to child_bs */
907 tts
->wait_child
= bdrv_attach_child(bs
, child_bs
, "wait-child", &child_file
,
910 /* This child is just there to be deleted
911 * (for detach_instead_of_delete == true) */
912 null_bs
= bdrv_open("null-co://", NULL
, NULL
, BDRV_O_RDWR
| BDRV_O_PROTOCOL
,
914 bdrv_attach_child(bs
, null_bs
, "null-child", &child_file
, &error_abort
);
916 blk
= blk_new(BLK_PERM_ALL
, BLK_PERM_ALL
);
917 blk_insert_bs(blk
, bs
, &error_abort
);
919 /* Referenced by blk now */
922 g_assert_cmpint(bs
->refcnt
, ==, 1);
923 g_assert_cmpint(child_bs
->refcnt
, ==, 1);
924 g_assert_cmpint(null_bs
->refcnt
, ==, 1);
927 dbdd
= (TestCoDeleteByDrainData
){
929 .detach_instead_of_delete
= detach_instead_of_delete
,
932 co
= qemu_coroutine_create(test_co_delete_by_drain
, &dbdd
);
933 qemu_coroutine_enter(co
);
935 /* Drain the child while the read operation is still pending.
936 * This should result in the operation finishing and
937 * test_co_delete_by_drain() resuming. Thus, @bs will be deleted
938 * and the coroutine will exit while this drain operation is still
940 switch (drain_type
) {
943 bdrv_drain(child_bs
);
944 bdrv_unref(child_bs
);
946 case BDRV_SUBTREE_DRAIN
:
947 /* Would have to ref/unref bs here for !detach_instead_of_delete, but
948 * then the whole test becomes pointless because the graph changes
949 * don't occur during the drain any more. */
950 assert(detach_instead_of_delete
);
951 bdrv_subtree_drained_begin(bs
);
952 bdrv_subtree_drained_end(bs
);
955 g_assert_not_reached();
959 aio_poll(qemu_get_aio_context(), true);
962 if (detach_instead_of_delete
) {
963 /* Here, the reference has not passed over to the coroutine,
964 * so we have to delete the BB ourselves */
969 static void test_delete_by_drain(void)
971 do_test_delete_by_drain(false, BDRV_DRAIN
);
974 static void test_detach_by_drain(void)
976 do_test_delete_by_drain(true, BDRV_DRAIN
);
979 static void test_detach_by_drain_subtree(void)
981 do_test_delete_by_drain(true, BDRV_SUBTREE_DRAIN
);
985 struct detach_by_parent_data
{
986 BlockDriverState
*parent_b
;
992 static void detach_by_parent_aio_cb(void *opaque
, int ret
)
994 struct detach_by_parent_data
*data
= opaque
;
996 g_assert_cmpint(ret
, ==, 0);
997 bdrv_unref_child(data
->parent_b
, data
->child_b
);
1000 data
->child_c
= bdrv_attach_child(data
->parent_b
, data
->c
, "PB-C",
1001 &child_file
, &error_abort
);
1011 * PA has a pending write request whose callback changes the child nodes of PB:
1012 * It removes B and adds C instead. The subtree of PB is drained, which will
1013 * indirectly drain the write request, too.
1015 static void test_detach_by_parent_cb(void)
1018 BlockDriverState
*parent_a
, *parent_b
, *a
, *b
, *c
;
1019 BdrvChild
*child_a
, *child_b
;
1021 struct detach_by_parent_data data
;
1024 struct iovec iov
= {
1028 qemu_iovec_init_external(&qiov
, &iov
, 1);
1030 /* Create all involved nodes */
1031 parent_a
= bdrv_new_open_driver(&bdrv_test
, "parent-a", BDRV_O_RDWR
,
1033 parent_b
= bdrv_new_open_driver(&bdrv_test
, "parent-b", 0,
1036 a
= bdrv_new_open_driver(&bdrv_test
, "a", BDRV_O_RDWR
, &error_abort
);
1037 b
= bdrv_new_open_driver(&bdrv_test
, "b", BDRV_O_RDWR
, &error_abort
);
1038 c
= bdrv_new_open_driver(&bdrv_test
, "c", BDRV_O_RDWR
, &error_abort
);
1040 /* blk is a BB for parent-a */
1041 blk
= blk_new(BLK_PERM_ALL
, BLK_PERM_ALL
);
1042 blk_insert_bs(blk
, parent_a
, &error_abort
);
1043 bdrv_unref(parent_a
);
1045 /* Set child relationships */
1048 child_b
= bdrv_attach_child(parent_b
, b
, "PB-B", &child_file
, &error_abort
);
1049 child_a
= bdrv_attach_child(parent_b
, a
, "PB-A", &child_backing
, &error_abort
);
1052 bdrv_attach_child(parent_a
, a
, "PA-A", &child_file
, &error_abort
);
1054 g_assert_cmpint(parent_a
->refcnt
, ==, 1);
1055 g_assert_cmpint(parent_b
->refcnt
, ==, 1);
1056 g_assert_cmpint(a
->refcnt
, ==, 3);
1057 g_assert_cmpint(b
->refcnt
, ==, 2);
1058 g_assert_cmpint(c
->refcnt
, ==, 1);
1060 g_assert(QLIST_FIRST(&parent_b
->children
) == child_a
);
1061 g_assert(QLIST_NEXT(child_a
, next
) == child_b
);
1062 g_assert(QLIST_NEXT(child_b
, next
) == NULL
);
1064 /* Start the evil write request */
1065 data
= (struct detach_by_parent_data
) {
1066 .parent_b
= parent_b
,
1070 acb
= blk_aio_preadv(blk
, 0, &qiov
, 0, detach_by_parent_aio_cb
, &data
);
1071 g_assert(acb
!= NULL
);
1073 /* Drain and check the expected result */
1074 bdrv_subtree_drained_begin(parent_b
);
1076 g_assert(data
.child_c
!= NULL
);
1078 g_assert_cmpint(parent_a
->refcnt
, ==, 1);
1079 g_assert_cmpint(parent_b
->refcnt
, ==, 1);
1080 g_assert_cmpint(a
->refcnt
, ==, 3);
1081 g_assert_cmpint(b
->refcnt
, ==, 1);
1082 g_assert_cmpint(c
->refcnt
, ==, 2);
1084 g_assert(QLIST_FIRST(&parent_b
->children
) == data
.child_c
);
1085 g_assert(QLIST_NEXT(data
.child_c
, next
) == child_a
);
1086 g_assert(QLIST_NEXT(child_a
, next
) == NULL
);
1088 g_assert_cmpint(parent_a
->quiesce_counter
, ==, 1);
1089 g_assert_cmpint(parent_b
->quiesce_counter
, ==, 1);
1090 g_assert_cmpint(a
->quiesce_counter
, ==, 1);
1091 g_assert_cmpint(b
->quiesce_counter
, ==, 0);
1092 g_assert_cmpint(c
->quiesce_counter
, ==, 1);
1094 bdrv_subtree_drained_end(parent_b
);
1096 bdrv_unref(parent_b
);
1099 /* XXX Once bdrv_close() unref's children instead of just detaching them,
1100 * this won't be necessary any more. */
1105 g_assert_cmpint(a
->refcnt
, ==, 1);
1106 g_assert_cmpint(b
->refcnt
, ==, 1);
1107 g_assert_cmpint(c
->refcnt
, ==, 1);
1114 int main(int argc
, char **argv
)
1119 qemu_init_main_loop(&error_abort
);
1121 g_test_init(&argc
, &argv
, NULL
);
1122 qemu_event_init(&done_event
, false);
1124 g_test_add_func("/bdrv-drain/driver-cb/drain_all", test_drv_cb_drain_all
);
1125 g_test_add_func("/bdrv-drain/driver-cb/drain", test_drv_cb_drain
);
1126 g_test_add_func("/bdrv-drain/driver-cb/drain_subtree",
1127 test_drv_cb_drain_subtree
);
1129 g_test_add_func("/bdrv-drain/driver-cb/co/drain_all",
1130 test_drv_cb_co_drain_all
);
1131 g_test_add_func("/bdrv-drain/driver-cb/co/drain", test_drv_cb_co_drain
);
1132 g_test_add_func("/bdrv-drain/driver-cb/co/drain_subtree",
1133 test_drv_cb_co_drain_subtree
);
1136 g_test_add_func("/bdrv-drain/quiesce/drain_all", test_quiesce_drain_all
);
1137 g_test_add_func("/bdrv-drain/quiesce/drain", test_quiesce_drain
);
1138 g_test_add_func("/bdrv-drain/quiesce/drain_subtree",
1139 test_quiesce_drain_subtree
);
1141 g_test_add_func("/bdrv-drain/quiesce/co/drain_all",
1142 test_quiesce_co_drain_all
);
1143 g_test_add_func("/bdrv-drain/quiesce/co/drain", test_quiesce_co_drain
);
1144 g_test_add_func("/bdrv-drain/quiesce/co/drain_subtree",
1145 test_quiesce_co_drain_subtree
);
1147 g_test_add_func("/bdrv-drain/nested", test_nested
);
1148 g_test_add_func("/bdrv-drain/multiparent", test_multiparent
);
1149 g_test_add_func("/bdrv-drain/graph-change", test_graph_change
);
1151 g_test_add_func("/bdrv-drain/iothread/drain_all", test_iothread_drain_all
);
1152 g_test_add_func("/bdrv-drain/iothread/drain", test_iothread_drain
);
1153 g_test_add_func("/bdrv-drain/iothread/drain_subtree",
1154 test_iothread_drain_subtree
);
1156 g_test_add_func("/bdrv-drain/blockjob/drain_all", test_blockjob_drain_all
);
1157 g_test_add_func("/bdrv-drain/blockjob/drain", test_blockjob_drain
);
1158 g_test_add_func("/bdrv-drain/blockjob/drain_subtree",
1159 test_blockjob_drain_subtree
);
1161 g_test_add_func("/bdrv-drain/deletion/drain", test_delete_by_drain
);
1162 g_test_add_func("/bdrv-drain/detach/drain", test_detach_by_drain
);
1163 g_test_add_func("/bdrv-drain/detach/drain_subtree", test_detach_by_drain_subtree
);
1164 g_test_add_func("/bdrv-drain/detach/parent_cb", test_detach_by_parent_cb
);
1167 qemu_event_destroy(&done_event
);