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
;
37 bool sleep_in_drain_begin
;
40 static void coroutine_fn
bdrv_test_co_drain_begin(BlockDriverState
*bs
)
42 BDRVTestState
*s
= bs
->opaque
;
44 if (s
->sleep_in_drain_begin
) {
45 qemu_co_sleep_ns(QEMU_CLOCK_REALTIME
, 100000);
49 static void coroutine_fn
bdrv_test_co_drain_end(BlockDriverState
*bs
)
51 BDRVTestState
*s
= bs
->opaque
;
55 static void bdrv_test_close(BlockDriverState
*bs
)
57 BDRVTestState
*s
= bs
->opaque
;
58 g_assert_cmpint(s
->drain_count
, >, 0);
61 static void co_reenter_bh(void *opaque
)
66 static int coroutine_fn
bdrv_test_co_preadv(BlockDriverState
*bs
,
67 uint64_t offset
, uint64_t bytes
,
68 QEMUIOVector
*qiov
, int flags
)
70 BDRVTestState
*s
= bs
->opaque
;
72 /* We want this request to stay until the polling loop in drain waits for
73 * it to complete. We need to sleep a while as bdrv_drain_invoke() comes
74 * first and polls its result, too, but it shouldn't accidentally complete
75 * this request yet. */
76 qemu_co_sleep_ns(QEMU_CLOCK_REALTIME
, 100000);
78 if (s
->bh_indirection_ctx
) {
79 aio_bh_schedule_oneshot(s
->bh_indirection_ctx
, co_reenter_bh
,
80 qemu_coroutine_self());
81 qemu_coroutine_yield();
87 static void bdrv_test_child_perm(BlockDriverState
*bs
, BdrvChild
*c
,
88 const BdrvChildRole
*role
,
89 BlockReopenQueue
*reopen_queue
,
90 uint64_t perm
, uint64_t shared
,
91 uint64_t *nperm
, uint64_t *nshared
)
93 /* bdrv_format_default_perms() accepts only these two, so disguise
94 * detach_by_driver_cb_role as one of them. */
95 if (role
!= &child_file
&& role
!= &child_backing
) {
99 bdrv_format_default_perms(bs
, c
, role
, reopen_queue
, perm
, shared
,
103 static int bdrv_test_change_backing_file(BlockDriverState
*bs
,
104 const char *backing_file
,
105 const char *backing_fmt
)
110 static BlockDriver bdrv_test
= {
111 .format_name
= "test",
112 .instance_size
= sizeof(BDRVTestState
),
114 .bdrv_close
= bdrv_test_close
,
115 .bdrv_co_preadv
= bdrv_test_co_preadv
,
117 .bdrv_co_drain_begin
= bdrv_test_co_drain_begin
,
118 .bdrv_co_drain_end
= bdrv_test_co_drain_end
,
120 .bdrv_child_perm
= bdrv_test_child_perm
,
122 .bdrv_change_backing_file
= bdrv_test_change_backing_file
,
125 static void aio_ret_cb(void *opaque
, int ret
)
127 int *aio_ret
= opaque
;
131 typedef struct CallInCoroutineData
{
134 } CallInCoroutineData
;
136 static coroutine_fn
void call_in_coroutine_entry(void *opaque
)
138 CallInCoroutineData
*data
= opaque
;
144 static void call_in_coroutine(void (*entry
)(void))
147 CallInCoroutineData data
= {
152 co
= qemu_coroutine_create(call_in_coroutine_entry
, &data
);
153 qemu_coroutine_enter(co
);
155 aio_poll(qemu_get_aio_context(), true);
166 static void do_drain_begin(enum drain_type drain_type
, BlockDriverState
*bs
)
168 switch (drain_type
) {
169 case BDRV_DRAIN_ALL
: bdrv_drain_all_begin(); break;
170 case BDRV_DRAIN
: bdrv_drained_begin(bs
); break;
171 case BDRV_SUBTREE_DRAIN
: bdrv_subtree_drained_begin(bs
); break;
172 default: g_assert_not_reached();
176 static void do_drain_end(enum drain_type drain_type
, BlockDriverState
*bs
)
178 switch (drain_type
) {
179 case BDRV_DRAIN_ALL
: bdrv_drain_all_end(); break;
180 case BDRV_DRAIN
: bdrv_drained_end(bs
); break;
181 case BDRV_SUBTREE_DRAIN
: bdrv_subtree_drained_end(bs
); break;
182 default: g_assert_not_reached();
186 static void do_drain_begin_unlocked(enum drain_type drain_type
, BlockDriverState
*bs
)
188 if (drain_type
!= BDRV_DRAIN_ALL
) {
189 aio_context_acquire(bdrv_get_aio_context(bs
));
191 do_drain_begin(drain_type
, bs
);
192 if (drain_type
!= BDRV_DRAIN_ALL
) {
193 aio_context_release(bdrv_get_aio_context(bs
));
197 static void do_drain_end_unlocked(enum drain_type drain_type
, BlockDriverState
*bs
)
199 if (drain_type
!= BDRV_DRAIN_ALL
) {
200 aio_context_acquire(bdrv_get_aio_context(bs
));
202 do_drain_end(drain_type
, bs
);
203 if (drain_type
!= BDRV_DRAIN_ALL
) {
204 aio_context_release(bdrv_get_aio_context(bs
));
208 static void test_drv_cb_common(enum drain_type drain_type
, bool recursive
)
211 BlockDriverState
*bs
, *backing
;
212 BDRVTestState
*s
, *backing_s
;
216 QEMUIOVector qiov
= QEMU_IOVEC_INIT_BUF(qiov
, NULL
, 0);
218 blk
= blk_new(qemu_get_aio_context(), BLK_PERM_ALL
, BLK_PERM_ALL
);
219 bs
= bdrv_new_open_driver(&bdrv_test
, "test-node", BDRV_O_RDWR
,
222 blk_insert_bs(blk
, bs
, &error_abort
);
224 backing
= bdrv_new_open_driver(&bdrv_test
, "backing", 0, &error_abort
);
225 backing_s
= backing
->opaque
;
226 bdrv_set_backing_hd(bs
, backing
, &error_abort
);
228 /* Simple bdrv_drain_all_begin/end pair, check that CBs are called */
229 g_assert_cmpint(s
->drain_count
, ==, 0);
230 g_assert_cmpint(backing_s
->drain_count
, ==, 0);
232 do_drain_begin(drain_type
, bs
);
234 g_assert_cmpint(s
->drain_count
, ==, 1);
235 g_assert_cmpint(backing_s
->drain_count
, ==, !!recursive
);
237 do_drain_end(drain_type
, bs
);
239 g_assert_cmpint(s
->drain_count
, ==, 0);
240 g_assert_cmpint(backing_s
->drain_count
, ==, 0);
242 /* Now do the same while a request is pending */
243 aio_ret
= -EINPROGRESS
;
244 acb
= blk_aio_preadv(blk
, 0, &qiov
, 0, aio_ret_cb
, &aio_ret
);
245 g_assert(acb
!= NULL
);
246 g_assert_cmpint(aio_ret
, ==, -EINPROGRESS
);
248 g_assert_cmpint(s
->drain_count
, ==, 0);
249 g_assert_cmpint(backing_s
->drain_count
, ==, 0);
251 do_drain_begin(drain_type
, bs
);
253 g_assert_cmpint(aio_ret
, ==, 0);
254 g_assert_cmpint(s
->drain_count
, ==, 1);
255 g_assert_cmpint(backing_s
->drain_count
, ==, !!recursive
);
257 do_drain_end(drain_type
, bs
);
259 g_assert_cmpint(s
->drain_count
, ==, 0);
260 g_assert_cmpint(backing_s
->drain_count
, ==, 0);
267 static void test_drv_cb_drain_all(void)
269 test_drv_cb_common(BDRV_DRAIN_ALL
, true);
272 static void test_drv_cb_drain(void)
274 test_drv_cb_common(BDRV_DRAIN
, false);
277 static void test_drv_cb_drain_subtree(void)
279 test_drv_cb_common(BDRV_SUBTREE_DRAIN
, true);
282 static void test_drv_cb_co_drain_all(void)
284 call_in_coroutine(test_drv_cb_drain_all
);
287 static void test_drv_cb_co_drain(void)
289 call_in_coroutine(test_drv_cb_drain
);
292 static void test_drv_cb_co_drain_subtree(void)
294 call_in_coroutine(test_drv_cb_drain_subtree
);
297 static void test_quiesce_common(enum drain_type drain_type
, bool recursive
)
300 BlockDriverState
*bs
, *backing
;
302 blk
= blk_new(qemu_get_aio_context(), BLK_PERM_ALL
, BLK_PERM_ALL
);
303 bs
= bdrv_new_open_driver(&bdrv_test
, "test-node", BDRV_O_RDWR
,
305 blk_insert_bs(blk
, bs
, &error_abort
);
307 backing
= bdrv_new_open_driver(&bdrv_test
, "backing", 0, &error_abort
);
308 bdrv_set_backing_hd(bs
, backing
, &error_abort
);
310 g_assert_cmpint(bs
->quiesce_counter
, ==, 0);
311 g_assert_cmpint(backing
->quiesce_counter
, ==, 0);
313 do_drain_begin(drain_type
, bs
);
315 g_assert_cmpint(bs
->quiesce_counter
, ==, 1);
316 g_assert_cmpint(backing
->quiesce_counter
, ==, !!recursive
);
318 do_drain_end(drain_type
, bs
);
320 g_assert_cmpint(bs
->quiesce_counter
, ==, 0);
321 g_assert_cmpint(backing
->quiesce_counter
, ==, 0);
328 static void test_quiesce_drain_all(void)
330 test_quiesce_common(BDRV_DRAIN_ALL
, true);
333 static void test_quiesce_drain(void)
335 test_quiesce_common(BDRV_DRAIN
, false);
338 static void test_quiesce_drain_subtree(void)
340 test_quiesce_common(BDRV_SUBTREE_DRAIN
, true);
343 static void test_quiesce_co_drain_all(void)
345 call_in_coroutine(test_quiesce_drain_all
);
348 static void test_quiesce_co_drain(void)
350 call_in_coroutine(test_quiesce_drain
);
353 static void test_quiesce_co_drain_subtree(void)
355 call_in_coroutine(test_quiesce_drain_subtree
);
358 static void test_nested(void)
361 BlockDriverState
*bs
, *backing
;
362 BDRVTestState
*s
, *backing_s
;
363 enum drain_type outer
, inner
;
365 blk
= blk_new(qemu_get_aio_context(), BLK_PERM_ALL
, BLK_PERM_ALL
);
366 bs
= bdrv_new_open_driver(&bdrv_test
, "test-node", BDRV_O_RDWR
,
369 blk_insert_bs(blk
, bs
, &error_abort
);
371 backing
= bdrv_new_open_driver(&bdrv_test
, "backing", 0, &error_abort
);
372 backing_s
= backing
->opaque
;
373 bdrv_set_backing_hd(bs
, backing
, &error_abort
);
375 for (outer
= 0; outer
< DRAIN_TYPE_MAX
; outer
++) {
376 for (inner
= 0; inner
< DRAIN_TYPE_MAX
; inner
++) {
377 int backing_quiesce
= (outer
!= BDRV_DRAIN
) +
378 (inner
!= BDRV_DRAIN
);
380 g_assert_cmpint(bs
->quiesce_counter
, ==, 0);
381 g_assert_cmpint(backing
->quiesce_counter
, ==, 0);
382 g_assert_cmpint(s
->drain_count
, ==, 0);
383 g_assert_cmpint(backing_s
->drain_count
, ==, 0);
385 do_drain_begin(outer
, bs
);
386 do_drain_begin(inner
, bs
);
388 g_assert_cmpint(bs
->quiesce_counter
, ==, 2);
389 g_assert_cmpint(backing
->quiesce_counter
, ==, backing_quiesce
);
390 g_assert_cmpint(s
->drain_count
, ==, 2);
391 g_assert_cmpint(backing_s
->drain_count
, ==, backing_quiesce
);
393 do_drain_end(inner
, bs
);
394 do_drain_end(outer
, bs
);
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);
408 static void test_multiparent(void)
410 BlockBackend
*blk_a
, *blk_b
;
411 BlockDriverState
*bs_a
, *bs_b
, *backing
;
412 BDRVTestState
*a_s
, *b_s
, *backing_s
;
414 blk_a
= blk_new(qemu_get_aio_context(), BLK_PERM_ALL
, BLK_PERM_ALL
);
415 bs_a
= bdrv_new_open_driver(&bdrv_test
, "test-node-a", BDRV_O_RDWR
,
418 blk_insert_bs(blk_a
, bs_a
, &error_abort
);
420 blk_b
= blk_new(qemu_get_aio_context(), BLK_PERM_ALL
, BLK_PERM_ALL
);
421 bs_b
= bdrv_new_open_driver(&bdrv_test
, "test-node-b", BDRV_O_RDWR
,
424 blk_insert_bs(blk_b
, bs_b
, &error_abort
);
426 backing
= bdrv_new_open_driver(&bdrv_test
, "backing", 0, &error_abort
);
427 backing_s
= backing
->opaque
;
428 bdrv_set_backing_hd(bs_a
, backing
, &error_abort
);
429 bdrv_set_backing_hd(bs_b
, backing
, &error_abort
);
431 g_assert_cmpint(bs_a
->quiesce_counter
, ==, 0);
432 g_assert_cmpint(bs_b
->quiesce_counter
, ==, 0);
433 g_assert_cmpint(backing
->quiesce_counter
, ==, 0);
434 g_assert_cmpint(a_s
->drain_count
, ==, 0);
435 g_assert_cmpint(b_s
->drain_count
, ==, 0);
436 g_assert_cmpint(backing_s
->drain_count
, ==, 0);
438 do_drain_begin(BDRV_SUBTREE_DRAIN
, bs_a
);
440 g_assert_cmpint(bs_a
->quiesce_counter
, ==, 1);
441 g_assert_cmpint(bs_b
->quiesce_counter
, ==, 1);
442 g_assert_cmpint(backing
->quiesce_counter
, ==, 1);
443 g_assert_cmpint(a_s
->drain_count
, ==, 1);
444 g_assert_cmpint(b_s
->drain_count
, ==, 1);
445 g_assert_cmpint(backing_s
->drain_count
, ==, 1);
447 do_drain_begin(BDRV_SUBTREE_DRAIN
, bs_b
);
449 g_assert_cmpint(bs_a
->quiesce_counter
, ==, 2);
450 g_assert_cmpint(bs_b
->quiesce_counter
, ==, 2);
451 g_assert_cmpint(backing
->quiesce_counter
, ==, 2);
452 g_assert_cmpint(a_s
->drain_count
, ==, 2);
453 g_assert_cmpint(b_s
->drain_count
, ==, 2);
454 g_assert_cmpint(backing_s
->drain_count
, ==, 2);
456 do_drain_end(BDRV_SUBTREE_DRAIN
, bs_b
);
458 g_assert_cmpint(bs_a
->quiesce_counter
, ==, 1);
459 g_assert_cmpint(bs_b
->quiesce_counter
, ==, 1);
460 g_assert_cmpint(backing
->quiesce_counter
, ==, 1);
461 g_assert_cmpint(a_s
->drain_count
, ==, 1);
462 g_assert_cmpint(b_s
->drain_count
, ==, 1);
463 g_assert_cmpint(backing_s
->drain_count
, ==, 1);
465 do_drain_end(BDRV_SUBTREE_DRAIN
, bs_a
);
467 g_assert_cmpint(bs_a
->quiesce_counter
, ==, 0);
468 g_assert_cmpint(bs_b
->quiesce_counter
, ==, 0);
469 g_assert_cmpint(backing
->quiesce_counter
, ==, 0);
470 g_assert_cmpint(a_s
->drain_count
, ==, 0);
471 g_assert_cmpint(b_s
->drain_count
, ==, 0);
472 g_assert_cmpint(backing_s
->drain_count
, ==, 0);
481 static void test_graph_change_drain_subtree(void)
483 BlockBackend
*blk_a
, *blk_b
;
484 BlockDriverState
*bs_a
, *bs_b
, *backing
;
485 BDRVTestState
*a_s
, *b_s
, *backing_s
;
487 blk_a
= blk_new(qemu_get_aio_context(), BLK_PERM_ALL
, BLK_PERM_ALL
);
488 bs_a
= bdrv_new_open_driver(&bdrv_test
, "test-node-a", BDRV_O_RDWR
,
491 blk_insert_bs(blk_a
, bs_a
, &error_abort
);
493 blk_b
= blk_new(qemu_get_aio_context(), BLK_PERM_ALL
, BLK_PERM_ALL
);
494 bs_b
= bdrv_new_open_driver(&bdrv_test
, "test-node-b", BDRV_O_RDWR
,
497 blk_insert_bs(blk_b
, bs_b
, &error_abort
);
499 backing
= bdrv_new_open_driver(&bdrv_test
, "backing", 0, &error_abort
);
500 backing_s
= backing
->opaque
;
501 bdrv_set_backing_hd(bs_a
, backing
, &error_abort
);
503 g_assert_cmpint(bs_a
->quiesce_counter
, ==, 0);
504 g_assert_cmpint(bs_b
->quiesce_counter
, ==, 0);
505 g_assert_cmpint(backing
->quiesce_counter
, ==, 0);
506 g_assert_cmpint(a_s
->drain_count
, ==, 0);
507 g_assert_cmpint(b_s
->drain_count
, ==, 0);
508 g_assert_cmpint(backing_s
->drain_count
, ==, 0);
510 do_drain_begin(BDRV_SUBTREE_DRAIN
, bs_a
);
511 do_drain_begin(BDRV_SUBTREE_DRAIN
, bs_a
);
512 do_drain_begin(BDRV_SUBTREE_DRAIN
, bs_a
);
513 do_drain_begin(BDRV_SUBTREE_DRAIN
, bs_b
);
514 do_drain_begin(BDRV_SUBTREE_DRAIN
, bs_b
);
516 bdrv_set_backing_hd(bs_b
, backing
, &error_abort
);
517 g_assert_cmpint(bs_a
->quiesce_counter
, ==, 5);
518 g_assert_cmpint(bs_b
->quiesce_counter
, ==, 5);
519 g_assert_cmpint(backing
->quiesce_counter
, ==, 5);
520 g_assert_cmpint(a_s
->drain_count
, ==, 5);
521 g_assert_cmpint(b_s
->drain_count
, ==, 5);
522 g_assert_cmpint(backing_s
->drain_count
, ==, 5);
524 bdrv_set_backing_hd(bs_b
, NULL
, &error_abort
);
525 g_assert_cmpint(bs_a
->quiesce_counter
, ==, 3);
526 g_assert_cmpint(bs_b
->quiesce_counter
, ==, 2);
527 g_assert_cmpint(backing
->quiesce_counter
, ==, 3);
528 g_assert_cmpint(a_s
->drain_count
, ==, 3);
529 g_assert_cmpint(b_s
->drain_count
, ==, 2);
530 g_assert_cmpint(backing_s
->drain_count
, ==, 3);
532 bdrv_set_backing_hd(bs_b
, backing
, &error_abort
);
533 g_assert_cmpint(bs_a
->quiesce_counter
, ==, 5);
534 g_assert_cmpint(bs_b
->quiesce_counter
, ==, 5);
535 g_assert_cmpint(backing
->quiesce_counter
, ==, 5);
536 g_assert_cmpint(a_s
->drain_count
, ==, 5);
537 g_assert_cmpint(b_s
->drain_count
, ==, 5);
538 g_assert_cmpint(backing_s
->drain_count
, ==, 5);
540 do_drain_end(BDRV_SUBTREE_DRAIN
, bs_b
);
541 do_drain_end(BDRV_SUBTREE_DRAIN
, bs_b
);
542 do_drain_end(BDRV_SUBTREE_DRAIN
, bs_a
);
543 do_drain_end(BDRV_SUBTREE_DRAIN
, bs_a
);
544 do_drain_end(BDRV_SUBTREE_DRAIN
, bs_a
);
546 g_assert_cmpint(bs_a
->quiesce_counter
, ==, 0);
547 g_assert_cmpint(bs_b
->quiesce_counter
, ==, 0);
548 g_assert_cmpint(backing
->quiesce_counter
, ==, 0);
549 g_assert_cmpint(a_s
->drain_count
, ==, 0);
550 g_assert_cmpint(b_s
->drain_count
, ==, 0);
551 g_assert_cmpint(backing_s
->drain_count
, ==, 0);
560 static void test_graph_change_drain_all(void)
562 BlockBackend
*blk_a
, *blk_b
;
563 BlockDriverState
*bs_a
, *bs_b
;
564 BDRVTestState
*a_s
, *b_s
;
566 /* Create node A with a BlockBackend */
567 blk_a
= blk_new(qemu_get_aio_context(), BLK_PERM_ALL
, BLK_PERM_ALL
);
568 bs_a
= bdrv_new_open_driver(&bdrv_test
, "test-node-a", BDRV_O_RDWR
,
571 blk_insert_bs(blk_a
, bs_a
, &error_abort
);
573 g_assert_cmpint(bs_a
->quiesce_counter
, ==, 0);
574 g_assert_cmpint(a_s
->drain_count
, ==, 0);
576 /* Call bdrv_drain_all_begin() */
577 bdrv_drain_all_begin();
579 g_assert_cmpint(bs_a
->quiesce_counter
, ==, 1);
580 g_assert_cmpint(a_s
->drain_count
, ==, 1);
582 /* Create node B with a BlockBackend */
583 blk_b
= blk_new(qemu_get_aio_context(), BLK_PERM_ALL
, BLK_PERM_ALL
);
584 bs_b
= bdrv_new_open_driver(&bdrv_test
, "test-node-b", BDRV_O_RDWR
,
587 blk_insert_bs(blk_b
, bs_b
, &error_abort
);
589 g_assert_cmpint(bs_a
->quiesce_counter
, ==, 1);
590 g_assert_cmpint(bs_b
->quiesce_counter
, ==, 1);
591 g_assert_cmpint(a_s
->drain_count
, ==, 1);
592 g_assert_cmpint(b_s
->drain_count
, ==, 1);
594 /* Unref and finally delete node A */
597 g_assert_cmpint(bs_a
->quiesce_counter
, ==, 1);
598 g_assert_cmpint(bs_b
->quiesce_counter
, ==, 1);
599 g_assert_cmpint(a_s
->drain_count
, ==, 1);
600 g_assert_cmpint(b_s
->drain_count
, ==, 1);
604 g_assert_cmpint(bs_b
->quiesce_counter
, ==, 1);
605 g_assert_cmpint(b_s
->drain_count
, ==, 1);
607 /* End the drained section */
608 bdrv_drain_all_end();
610 g_assert_cmpint(bs_b
->quiesce_counter
, ==, 0);
611 g_assert_cmpint(b_s
->drain_count
, ==, 0);
617 struct test_iothread_data
{
618 BlockDriverState
*bs
;
619 enum drain_type drain_type
;
623 static void test_iothread_drain_entry(void *opaque
)
625 struct test_iothread_data
*data
= opaque
;
627 aio_context_acquire(bdrv_get_aio_context(data
->bs
));
628 do_drain_begin(data
->drain_type
, data
->bs
);
629 g_assert_cmpint(*data
->aio_ret
, ==, 0);
630 do_drain_end(data
->drain_type
, data
->bs
);
631 aio_context_release(bdrv_get_aio_context(data
->bs
));
633 qemu_event_set(&done_event
);
636 static void test_iothread_aio_cb(void *opaque
, int ret
)
638 int *aio_ret
= opaque
;
640 qemu_event_set(&done_event
);
643 static void test_iothread_main_thread_bh(void *opaque
)
645 struct test_iothread_data
*data
= opaque
;
647 /* Test that the AioContext is not yet locked in a random BH that is
648 * executed during drain, otherwise this would deadlock. */
649 aio_context_acquire(bdrv_get_aio_context(data
->bs
));
650 bdrv_flush(data
->bs
);
651 aio_context_release(bdrv_get_aio_context(data
->bs
));
655 * Starts an AIO request on a BDS that runs in the AioContext of iothread 1.
656 * The request involves a BH on iothread 2 before it can complete.
658 * @drain_thread = 0 means that do_drain_begin/end are called from the main
659 * thread, @drain_thread = 1 means that they are called from iothread 1. Drain
660 * for this BDS cannot be called from iothread 2 because only the main thread
661 * may do cross-AioContext polling.
663 static void test_iothread_common(enum drain_type drain_type
, int drain_thread
)
666 BlockDriverState
*bs
;
670 struct test_iothread_data data
;
672 IOThread
*a
= iothread_new();
673 IOThread
*b
= iothread_new();
674 AioContext
*ctx_a
= iothread_get_aio_context(a
);
675 AioContext
*ctx_b
= iothread_get_aio_context(b
);
677 QEMUIOVector qiov
= QEMU_IOVEC_INIT_BUF(qiov
, NULL
, 0);
679 /* bdrv_drain_all() may only be called from the main loop thread */
680 if (drain_type
== BDRV_DRAIN_ALL
&& drain_thread
!= 0) {
684 blk
= blk_new(qemu_get_aio_context(), BLK_PERM_ALL
, BLK_PERM_ALL
);
685 bs
= bdrv_new_open_driver(&bdrv_test
, "test-node", BDRV_O_RDWR
,
688 blk_insert_bs(blk
, bs
, &error_abort
);
690 blk_set_aio_context(blk
, ctx_a
, &error_abort
);
691 aio_context_acquire(ctx_a
);
693 s
->bh_indirection_ctx
= ctx_b
;
695 aio_ret
= -EINPROGRESS
;
696 qemu_event_reset(&done_event
);
698 if (drain_thread
== 0) {
699 acb
= blk_aio_preadv(blk
, 0, &qiov
, 0, test_iothread_aio_cb
, &aio_ret
);
701 acb
= blk_aio_preadv(blk
, 0, &qiov
, 0, aio_ret_cb
, &aio_ret
);
703 g_assert(acb
!= NULL
);
704 g_assert_cmpint(aio_ret
, ==, -EINPROGRESS
);
706 aio_context_release(ctx_a
);
708 data
= (struct test_iothread_data
) {
710 .drain_type
= drain_type
,
714 switch (drain_thread
) {
716 if (drain_type
!= BDRV_DRAIN_ALL
) {
717 aio_context_acquire(ctx_a
);
720 aio_bh_schedule_oneshot(ctx_a
, test_iothread_main_thread_bh
, &data
);
722 /* The request is running on the IOThread a. Draining its block device
723 * will make sure that it has completed as far as the BDS is concerned,
724 * but the drain in this thread can continue immediately after
725 * bdrv_dec_in_flight() and aio_ret might be assigned only slightly
727 do_drain_begin(drain_type
, bs
);
728 g_assert_cmpint(bs
->in_flight
, ==, 0);
730 if (drain_type
!= BDRV_DRAIN_ALL
) {
731 aio_context_release(ctx_a
);
733 qemu_event_wait(&done_event
);
734 if (drain_type
!= BDRV_DRAIN_ALL
) {
735 aio_context_acquire(ctx_a
);
738 g_assert_cmpint(aio_ret
, ==, 0);
739 do_drain_end(drain_type
, bs
);
741 if (drain_type
!= BDRV_DRAIN_ALL
) {
742 aio_context_release(ctx_a
);
746 aio_bh_schedule_oneshot(ctx_a
, test_iothread_drain_entry
, &data
);
747 qemu_event_wait(&done_event
);
750 g_assert_not_reached();
753 aio_context_acquire(ctx_a
);
754 blk_set_aio_context(blk
, qemu_get_aio_context(), &error_abort
);
755 aio_context_release(ctx_a
);
765 static void test_iothread_drain_all(void)
767 test_iothread_common(BDRV_DRAIN_ALL
, 0);
768 test_iothread_common(BDRV_DRAIN_ALL
, 1);
771 static void test_iothread_drain(void)
773 test_iothread_common(BDRV_DRAIN
, 0);
774 test_iothread_common(BDRV_DRAIN
, 1);
777 static void test_iothread_drain_subtree(void)
779 test_iothread_common(BDRV_SUBTREE_DRAIN
, 0);
780 test_iothread_common(BDRV_SUBTREE_DRAIN
, 1);
784 typedef struct TestBlockJob
{
789 bool should_complete
;
792 static int test_job_prepare(Job
*job
)
794 TestBlockJob
*s
= container_of(job
, TestBlockJob
, common
.job
);
796 /* Provoke an AIO_WAIT_WHILE() call to verify there is no deadlock */
797 blk_flush(s
->common
.blk
);
798 return s
->prepare_ret
;
801 static void test_job_commit(Job
*job
)
803 TestBlockJob
*s
= container_of(job
, TestBlockJob
, common
.job
);
805 /* Provoke an AIO_WAIT_WHILE() call to verify there is no deadlock */
806 blk_flush(s
->common
.blk
);
809 static void test_job_abort(Job
*job
)
811 TestBlockJob
*s
= container_of(job
, TestBlockJob
, common
.job
);
813 /* Provoke an AIO_WAIT_WHILE() call to verify there is no deadlock */
814 blk_flush(s
->common
.blk
);
817 static int coroutine_fn
test_job_run(Job
*job
, Error
**errp
)
819 TestBlockJob
*s
= container_of(job
, TestBlockJob
, common
.job
);
821 /* We are running the actual job code past the pause point in
825 job_transition_to_ready(&s
->common
.job
);
826 while (!s
->should_complete
) {
827 /* Avoid job_sleep_ns() because it marks the job as !busy. We want to
828 * emulate some actual activity (probably some I/O) here so that drain
829 * has to wait for this activity to stop. */
830 qemu_co_sleep_ns(QEMU_CLOCK_REALTIME
, 1000000);
832 job_pause_point(&s
->common
.job
);
838 static void test_job_complete(Job
*job
, Error
**errp
)
840 TestBlockJob
*s
= container_of(job
, TestBlockJob
, common
.job
);
841 s
->should_complete
= true;
844 BlockJobDriver test_job_driver
= {
846 .instance_size
= sizeof(TestBlockJob
),
847 .free
= block_job_free
,
848 .user_resume
= block_job_user_resume
,
849 .drain
= block_job_drain
,
851 .complete
= test_job_complete
,
852 .prepare
= test_job_prepare
,
853 .commit
= test_job_commit
,
854 .abort
= test_job_abort
,
858 enum test_job_result
{
861 TEST_JOB_FAIL_PREPARE
,
864 enum test_job_drain_node
{
866 TEST_JOB_DRAIN_SRC_CHILD
,
867 TEST_JOB_DRAIN_SRC_PARENT
,
870 static void test_blockjob_common_drain_node(enum drain_type drain_type
,
872 enum test_job_result result
,
873 enum test_job_drain_node drain_node
)
875 BlockBackend
*blk_src
, *blk_target
;
876 BlockDriverState
*src
, *src_backing
, *src_overlay
, *target
, *drain_bs
;
879 IOThread
*iothread
= NULL
;
883 src
= bdrv_new_open_driver(&bdrv_test
, "source", BDRV_O_RDWR
,
885 src_backing
= bdrv_new_open_driver(&bdrv_test
, "source-backing",
886 BDRV_O_RDWR
, &error_abort
);
887 src_overlay
= bdrv_new_open_driver(&bdrv_test
, "source-overlay",
888 BDRV_O_RDWR
, &error_abort
);
890 bdrv_set_backing_hd(src_overlay
, src
, &error_abort
);
892 bdrv_set_backing_hd(src
, src_backing
, &error_abort
);
893 bdrv_unref(src_backing
);
895 blk_src
= blk_new(qemu_get_aio_context(), BLK_PERM_ALL
, BLK_PERM_ALL
);
896 blk_insert_bs(blk_src
, src_overlay
, &error_abort
);
898 switch (drain_node
) {
899 case TEST_JOB_DRAIN_SRC
:
902 case TEST_JOB_DRAIN_SRC_CHILD
:
903 drain_bs
= src_backing
;
905 case TEST_JOB_DRAIN_SRC_PARENT
:
906 drain_bs
= src_overlay
;
909 g_assert_not_reached();
913 iothread
= iothread_new();
914 ctx
= iothread_get_aio_context(iothread
);
915 blk_set_aio_context(blk_src
, ctx
, &error_abort
);
917 ctx
= qemu_get_aio_context();
920 target
= bdrv_new_open_driver(&bdrv_test
, "target", BDRV_O_RDWR
,
922 blk_target
= blk_new(qemu_get_aio_context(), BLK_PERM_ALL
, BLK_PERM_ALL
);
923 blk_insert_bs(blk_target
, target
, &error_abort
);
924 blk_set_allow_aio_context_change(blk_target
, true);
926 aio_context_acquire(ctx
);
927 tjob
= block_job_create("job0", &test_job_driver
, NULL
, src
,
929 0, 0, NULL
, NULL
, &error_abort
);
931 block_job_add_bdrv(job
, "target", target
, 0, BLK_PERM_ALL
, &error_abort
);
934 case TEST_JOB_SUCCESS
:
936 case TEST_JOB_FAIL_RUN
:
937 tjob
->run_ret
= -EIO
;
939 case TEST_JOB_FAIL_PREPARE
:
940 tjob
->prepare_ret
= -EIO
;
944 job_start(&job
->job
);
945 aio_context_release(ctx
);
948 /* job_co_entry() is run in the I/O thread, wait for the actual job
949 * code to start (we don't want to catch the job in the pause point in
951 while (!tjob
->running
) {
952 aio_poll(qemu_get_aio_context(), false);
956 g_assert_cmpint(job
->job
.pause_count
, ==, 0);
957 g_assert_false(job
->job
.paused
);
958 g_assert_true(tjob
->running
);
959 g_assert_true(job
->job
.busy
); /* We're in qemu_co_sleep_ns() */
961 do_drain_begin_unlocked(drain_type
, drain_bs
);
963 if (drain_type
== BDRV_DRAIN_ALL
) {
964 /* bdrv_drain_all() drains both src and target */
965 g_assert_cmpint(job
->job
.pause_count
, ==, 2);
967 g_assert_cmpint(job
->job
.pause_count
, ==, 1);
969 g_assert_true(job
->job
.paused
);
970 g_assert_false(job
->job
.busy
); /* The job is paused */
972 do_drain_end_unlocked(drain_type
, drain_bs
);
975 /* paused is reset in the I/O thread, wait for it */
976 while (job
->job
.paused
) {
977 aio_poll(qemu_get_aio_context(), false);
981 g_assert_cmpint(job
->job
.pause_count
, ==, 0);
982 g_assert_false(job
->job
.paused
);
983 g_assert_true(job
->job
.busy
); /* We're in qemu_co_sleep_ns() */
985 do_drain_begin_unlocked(drain_type
, target
);
987 if (drain_type
== BDRV_DRAIN_ALL
) {
988 /* bdrv_drain_all() drains both src and target */
989 g_assert_cmpint(job
->job
.pause_count
, ==, 2);
991 g_assert_cmpint(job
->job
.pause_count
, ==, 1);
993 g_assert_true(job
->job
.paused
);
994 g_assert_false(job
->job
.busy
); /* The job is paused */
996 do_drain_end_unlocked(drain_type
, target
);
999 /* paused is reset in the I/O thread, wait for it */
1000 while (job
->job
.paused
) {
1001 aio_poll(qemu_get_aio_context(), false);
1005 g_assert_cmpint(job
->job
.pause_count
, ==, 0);
1006 g_assert_false(job
->job
.paused
);
1007 g_assert_true(job
->job
.busy
); /* We're in qemu_co_sleep_ns() */
1009 aio_context_acquire(ctx
);
1010 ret
= job_complete_sync(&job
->job
, &error_abort
);
1011 g_assert_cmpint(ret
, ==, (result
== TEST_JOB_SUCCESS
? 0 : -EIO
));
1014 blk_set_aio_context(blk_src
, qemu_get_aio_context(), &error_abort
);
1015 assert(blk_get_aio_context(blk_target
) == qemu_get_aio_context());
1017 aio_context_release(ctx
);
1020 blk_unref(blk_target
);
1021 bdrv_unref(src_overlay
);
1025 iothread_join(iothread
);
1029 static void test_blockjob_common(enum drain_type drain_type
, bool use_iothread
,
1030 enum test_job_result result
)
1032 test_blockjob_common_drain_node(drain_type
, use_iothread
, result
,
1033 TEST_JOB_DRAIN_SRC
);
1034 test_blockjob_common_drain_node(drain_type
, use_iothread
, result
,
1035 TEST_JOB_DRAIN_SRC_CHILD
);
1036 if (drain_type
== BDRV_SUBTREE_DRAIN
) {
1037 test_blockjob_common_drain_node(drain_type
, use_iothread
, result
,
1038 TEST_JOB_DRAIN_SRC_PARENT
);
1042 static void test_blockjob_drain_all(void)
1044 test_blockjob_common(BDRV_DRAIN_ALL
, false, TEST_JOB_SUCCESS
);
1047 static void test_blockjob_drain(void)
1049 test_blockjob_common(BDRV_DRAIN
, false, TEST_JOB_SUCCESS
);
1052 static void test_blockjob_drain_subtree(void)
1054 test_blockjob_common(BDRV_SUBTREE_DRAIN
, false, TEST_JOB_SUCCESS
);
1057 static void test_blockjob_error_drain_all(void)
1059 test_blockjob_common(BDRV_DRAIN_ALL
, false, TEST_JOB_FAIL_RUN
);
1060 test_blockjob_common(BDRV_DRAIN_ALL
, false, TEST_JOB_FAIL_PREPARE
);
1063 static void test_blockjob_error_drain(void)
1065 test_blockjob_common(BDRV_DRAIN
, false, TEST_JOB_FAIL_RUN
);
1066 test_blockjob_common(BDRV_DRAIN
, false, TEST_JOB_FAIL_PREPARE
);
1069 static void test_blockjob_error_drain_subtree(void)
1071 test_blockjob_common(BDRV_SUBTREE_DRAIN
, false, TEST_JOB_FAIL_RUN
);
1072 test_blockjob_common(BDRV_SUBTREE_DRAIN
, false, TEST_JOB_FAIL_PREPARE
);
1075 static void test_blockjob_iothread_drain_all(void)
1077 test_blockjob_common(BDRV_DRAIN_ALL
, true, TEST_JOB_SUCCESS
);
1080 static void test_blockjob_iothread_drain(void)
1082 test_blockjob_common(BDRV_DRAIN
, true, TEST_JOB_SUCCESS
);
1085 static void test_blockjob_iothread_drain_subtree(void)
1087 test_blockjob_common(BDRV_SUBTREE_DRAIN
, true, TEST_JOB_SUCCESS
);
1090 static void test_blockjob_iothread_error_drain_all(void)
1092 test_blockjob_common(BDRV_DRAIN_ALL
, true, TEST_JOB_FAIL_RUN
);
1093 test_blockjob_common(BDRV_DRAIN_ALL
, true, TEST_JOB_FAIL_PREPARE
);
1096 static void test_blockjob_iothread_error_drain(void)
1098 test_blockjob_common(BDRV_DRAIN
, true, TEST_JOB_FAIL_RUN
);
1099 test_blockjob_common(BDRV_DRAIN
, true, TEST_JOB_FAIL_PREPARE
);
1102 static void test_blockjob_iothread_error_drain_subtree(void)
1104 test_blockjob_common(BDRV_SUBTREE_DRAIN
, true, TEST_JOB_FAIL_RUN
);
1105 test_blockjob_common(BDRV_SUBTREE_DRAIN
, true, TEST_JOB_FAIL_PREPARE
);
1109 typedef struct BDRVTestTopState
{
1110 BdrvChild
*wait_child
;
1113 static void bdrv_test_top_close(BlockDriverState
*bs
)
1115 BdrvChild
*c
, *next_c
;
1116 QLIST_FOREACH_SAFE(c
, &bs
->children
, next
, next_c
) {
1117 bdrv_unref_child(bs
, c
);
1121 static int coroutine_fn
bdrv_test_top_co_preadv(BlockDriverState
*bs
,
1122 uint64_t offset
, uint64_t bytes
,
1123 QEMUIOVector
*qiov
, int flags
)
1125 BDRVTestTopState
*tts
= bs
->opaque
;
1126 return bdrv_co_preadv(tts
->wait_child
, offset
, bytes
, qiov
, flags
);
1129 static BlockDriver bdrv_test_top_driver
= {
1130 .format_name
= "test_top_driver",
1131 .instance_size
= sizeof(BDRVTestTopState
),
1133 .bdrv_close
= bdrv_test_top_close
,
1134 .bdrv_co_preadv
= bdrv_test_top_co_preadv
,
1136 .bdrv_child_perm
= bdrv_format_default_perms
,
1139 typedef struct TestCoDeleteByDrainData
{
1141 bool detach_instead_of_delete
;
1143 } TestCoDeleteByDrainData
;
1145 static void coroutine_fn
test_co_delete_by_drain(void *opaque
)
1147 TestCoDeleteByDrainData
*dbdd
= opaque
;
1148 BlockBackend
*blk
= dbdd
->blk
;
1149 BlockDriverState
*bs
= blk_bs(blk
);
1150 BDRVTestTopState
*tts
= bs
->opaque
;
1151 void *buffer
= g_malloc(65536);
1152 QEMUIOVector qiov
= QEMU_IOVEC_INIT_BUF(qiov
, buffer
, 65536);
1154 /* Pretend some internal write operation from parent to child.
1155 * Important: We have to read from the child, not from the parent!
1156 * Draining works by first propagating it all up the tree to the
1157 * root and then waiting for drainage from root to the leaves
1158 * (protocol nodes). If we have a request waiting on the root,
1159 * everything will be drained before we go back down the tree, but
1160 * we do not want that. We want to be in the middle of draining
1161 * when this following requests returns. */
1162 bdrv_co_preadv(tts
->wait_child
, 0, 65536, &qiov
, 0);
1164 g_assert_cmpint(bs
->refcnt
, ==, 1);
1166 if (!dbdd
->detach_instead_of_delete
) {
1169 BdrvChild
*c
, *next_c
;
1170 QLIST_FOREACH_SAFE(c
, &bs
->children
, next
, next_c
) {
1171 bdrv_unref_child(bs
, c
);
1180 * Test what happens when some BDS has some children, you drain one of
1181 * them and this results in the BDS being deleted.
1183 * If @detach_instead_of_delete is set, the BDS is not going to be
1184 * deleted but will only detach all of its children.
1186 static void do_test_delete_by_drain(bool detach_instead_of_delete
,
1187 enum drain_type drain_type
)
1190 BlockDriverState
*bs
, *child_bs
, *null_bs
;
1191 BDRVTestTopState
*tts
;
1192 TestCoDeleteByDrainData dbdd
;
1195 bs
= bdrv_new_open_driver(&bdrv_test_top_driver
, "top", BDRV_O_RDWR
,
1197 bs
->total_sectors
= 65536 >> BDRV_SECTOR_BITS
;
1200 null_bs
= bdrv_open("null-co://", NULL
, NULL
, BDRV_O_RDWR
| BDRV_O_PROTOCOL
,
1202 bdrv_attach_child(bs
, null_bs
, "null-child", &child_file
, &error_abort
);
1204 /* This child will be the one to pass to requests through to, and
1205 * it will stall until a drain occurs */
1206 child_bs
= bdrv_new_open_driver(&bdrv_test
, "child", BDRV_O_RDWR
,
1208 child_bs
->total_sectors
= 65536 >> BDRV_SECTOR_BITS
;
1209 /* Takes our reference to child_bs */
1210 tts
->wait_child
= bdrv_attach_child(bs
, child_bs
, "wait-child", &child_file
,
1213 /* This child is just there to be deleted
1214 * (for detach_instead_of_delete == true) */
1215 null_bs
= bdrv_open("null-co://", NULL
, NULL
, BDRV_O_RDWR
| BDRV_O_PROTOCOL
,
1217 bdrv_attach_child(bs
, null_bs
, "null-child", &child_file
, &error_abort
);
1219 blk
= blk_new(qemu_get_aio_context(), BLK_PERM_ALL
, BLK_PERM_ALL
);
1220 blk_insert_bs(blk
, bs
, &error_abort
);
1222 /* Referenced by blk now */
1225 g_assert_cmpint(bs
->refcnt
, ==, 1);
1226 g_assert_cmpint(child_bs
->refcnt
, ==, 1);
1227 g_assert_cmpint(null_bs
->refcnt
, ==, 1);
1230 dbdd
= (TestCoDeleteByDrainData
){
1232 .detach_instead_of_delete
= detach_instead_of_delete
,
1235 co
= qemu_coroutine_create(test_co_delete_by_drain
, &dbdd
);
1236 qemu_coroutine_enter(co
);
1238 /* Drain the child while the read operation is still pending.
1239 * This should result in the operation finishing and
1240 * test_co_delete_by_drain() resuming. Thus, @bs will be deleted
1241 * and the coroutine will exit while this drain operation is still
1243 switch (drain_type
) {
1246 bdrv_drain(child_bs
);
1247 bdrv_unref(child_bs
);
1249 case BDRV_SUBTREE_DRAIN
:
1250 /* Would have to ref/unref bs here for !detach_instead_of_delete, but
1251 * then the whole test becomes pointless because the graph changes
1252 * don't occur during the drain any more. */
1253 assert(detach_instead_of_delete
);
1254 bdrv_subtree_drained_begin(bs
);
1255 bdrv_subtree_drained_end(bs
);
1257 case BDRV_DRAIN_ALL
:
1258 bdrv_drain_all_begin();
1259 bdrv_drain_all_end();
1262 g_assert_not_reached();
1265 while (!dbdd
.done
) {
1266 aio_poll(qemu_get_aio_context(), true);
1269 if (detach_instead_of_delete
) {
1270 /* Here, the reference has not passed over to the coroutine,
1271 * so we have to delete the BB ourselves */
1276 static void test_delete_by_drain(void)
1278 do_test_delete_by_drain(false, BDRV_DRAIN
);
1281 static void test_detach_by_drain_all(void)
1283 do_test_delete_by_drain(true, BDRV_DRAIN_ALL
);
1286 static void test_detach_by_drain(void)
1288 do_test_delete_by_drain(true, BDRV_DRAIN
);
1291 static void test_detach_by_drain_subtree(void)
1293 do_test_delete_by_drain(true, BDRV_SUBTREE_DRAIN
);
1297 struct detach_by_parent_data
{
1298 BlockDriverState
*parent_b
;
1300 BlockDriverState
*c
;
1304 static struct detach_by_parent_data detach_by_parent_data
;
1306 static void detach_indirect_bh(void *opaque
)
1308 struct detach_by_parent_data
*data
= opaque
;
1310 bdrv_unref_child(data
->parent_b
, data
->child_b
);
1313 data
->child_c
= bdrv_attach_child(data
->parent_b
, data
->c
, "PB-C",
1314 &child_file
, &error_abort
);
1317 static void detach_by_parent_aio_cb(void *opaque
, int ret
)
1319 struct detach_by_parent_data
*data
= &detach_by_parent_data
;
1321 g_assert_cmpint(ret
, ==, 0);
1322 if (data
->by_parent_cb
) {
1323 detach_indirect_bh(data
);
1327 static void detach_by_driver_cb_drained_begin(BdrvChild
*child
)
1329 aio_bh_schedule_oneshot(qemu_get_current_aio_context(),
1330 detach_indirect_bh
, &detach_by_parent_data
);
1331 child_file
.drained_begin(child
);
1334 static BdrvChildRole detach_by_driver_cb_role
;
1343 * by_parent_cb == true: Test that parent callbacks don't poll
1345 * PA has a pending write request whose callback changes the child nodes of
1346 * PB: It removes B and adds C instead. The subtree of PB is drained, which
1347 * will indirectly drain the write request, too.
1349 * by_parent_cb == false: Test that bdrv_drain_invoke() doesn't poll
1351 * PA's BdrvChildRole has a .drained_begin callback that schedules a BH
1352 * that does the same graph change. If bdrv_drain_invoke() calls it, the
1353 * state is messed up, but if it is only polled in the single
1354 * BDRV_POLL_WHILE() at the end of the drain, this should work fine.
1356 static void test_detach_indirect(bool by_parent_cb
)
1359 BlockDriverState
*parent_a
, *parent_b
, *a
, *b
, *c
;
1360 BdrvChild
*child_a
, *child_b
;
1363 QEMUIOVector qiov
= QEMU_IOVEC_INIT_BUF(qiov
, NULL
, 0);
1365 if (!by_parent_cb
) {
1366 detach_by_driver_cb_role
= child_file
;
1367 detach_by_driver_cb_role
.drained_begin
=
1368 detach_by_driver_cb_drained_begin
;
1371 /* Create all involved nodes */
1372 parent_a
= bdrv_new_open_driver(&bdrv_test
, "parent-a", BDRV_O_RDWR
,
1374 parent_b
= bdrv_new_open_driver(&bdrv_test
, "parent-b", 0,
1377 a
= bdrv_new_open_driver(&bdrv_test
, "a", BDRV_O_RDWR
, &error_abort
);
1378 b
= bdrv_new_open_driver(&bdrv_test
, "b", BDRV_O_RDWR
, &error_abort
);
1379 c
= bdrv_new_open_driver(&bdrv_test
, "c", BDRV_O_RDWR
, &error_abort
);
1381 /* blk is a BB for parent-a */
1382 blk
= blk_new(qemu_get_aio_context(), BLK_PERM_ALL
, BLK_PERM_ALL
);
1383 blk_insert_bs(blk
, parent_a
, &error_abort
);
1384 bdrv_unref(parent_a
);
1386 /* If we want to get bdrv_drain_invoke() to call aio_poll(), the driver
1387 * callback must not return immediately. */
1388 if (!by_parent_cb
) {
1389 BDRVTestState
*s
= parent_a
->opaque
;
1390 s
->sleep_in_drain_begin
= true;
1393 /* Set child relationships */
1396 child_b
= bdrv_attach_child(parent_b
, b
, "PB-B", &child_file
, &error_abort
);
1397 child_a
= bdrv_attach_child(parent_b
, a
, "PB-A", &child_backing
, &error_abort
);
1400 bdrv_attach_child(parent_a
, a
, "PA-A",
1401 by_parent_cb
? &child_file
: &detach_by_driver_cb_role
,
1404 g_assert_cmpint(parent_a
->refcnt
, ==, 1);
1405 g_assert_cmpint(parent_b
->refcnt
, ==, 1);
1406 g_assert_cmpint(a
->refcnt
, ==, 3);
1407 g_assert_cmpint(b
->refcnt
, ==, 2);
1408 g_assert_cmpint(c
->refcnt
, ==, 1);
1410 g_assert(QLIST_FIRST(&parent_b
->children
) == child_a
);
1411 g_assert(QLIST_NEXT(child_a
, next
) == child_b
);
1412 g_assert(QLIST_NEXT(child_b
, next
) == NULL
);
1414 /* Start the evil write request */
1415 detach_by_parent_data
= (struct detach_by_parent_data
) {
1416 .parent_b
= parent_b
,
1419 .by_parent_cb
= by_parent_cb
,
1421 acb
= blk_aio_preadv(blk
, 0, &qiov
, 0, detach_by_parent_aio_cb
, NULL
);
1422 g_assert(acb
!= NULL
);
1424 /* Drain and check the expected result */
1425 bdrv_subtree_drained_begin(parent_b
);
1427 g_assert(detach_by_parent_data
.child_c
!= NULL
);
1429 g_assert_cmpint(parent_a
->refcnt
, ==, 1);
1430 g_assert_cmpint(parent_b
->refcnt
, ==, 1);
1431 g_assert_cmpint(a
->refcnt
, ==, 3);
1432 g_assert_cmpint(b
->refcnt
, ==, 1);
1433 g_assert_cmpint(c
->refcnt
, ==, 2);
1435 g_assert(QLIST_FIRST(&parent_b
->children
) == detach_by_parent_data
.child_c
);
1436 g_assert(QLIST_NEXT(detach_by_parent_data
.child_c
, next
) == child_a
);
1437 g_assert(QLIST_NEXT(child_a
, next
) == NULL
);
1439 g_assert_cmpint(parent_a
->quiesce_counter
, ==, 1);
1440 g_assert_cmpint(parent_b
->quiesce_counter
, ==, 1);
1441 g_assert_cmpint(a
->quiesce_counter
, ==, 1);
1442 g_assert_cmpint(b
->quiesce_counter
, ==, 0);
1443 g_assert_cmpint(c
->quiesce_counter
, ==, 1);
1445 bdrv_subtree_drained_end(parent_b
);
1447 bdrv_unref(parent_b
);
1450 g_assert_cmpint(a
->refcnt
, ==, 1);
1451 g_assert_cmpint(b
->refcnt
, ==, 1);
1452 g_assert_cmpint(c
->refcnt
, ==, 1);
1458 static void test_detach_by_parent_cb(void)
1460 test_detach_indirect(true);
1463 static void test_detach_by_driver_cb(void)
1465 test_detach_indirect(false);
1468 static void test_append_to_drained(void)
1471 BlockDriverState
*base
, *overlay
;
1472 BDRVTestState
*base_s
, *overlay_s
;
1474 blk
= blk_new(qemu_get_aio_context(), BLK_PERM_ALL
, BLK_PERM_ALL
);
1475 base
= bdrv_new_open_driver(&bdrv_test
, "base", BDRV_O_RDWR
, &error_abort
);
1476 base_s
= base
->opaque
;
1477 blk_insert_bs(blk
, base
, &error_abort
);
1479 overlay
= bdrv_new_open_driver(&bdrv_test
, "overlay", BDRV_O_RDWR
,
1481 overlay_s
= overlay
->opaque
;
1483 do_drain_begin(BDRV_DRAIN
, base
);
1484 g_assert_cmpint(base
->quiesce_counter
, ==, 1);
1485 g_assert_cmpint(base_s
->drain_count
, ==, 1);
1486 g_assert_cmpint(base
->in_flight
, ==, 0);
1488 /* Takes ownership of overlay, so we don't have to unref it later */
1489 bdrv_append(overlay
, base
, &error_abort
);
1490 g_assert_cmpint(base
->in_flight
, ==, 0);
1491 g_assert_cmpint(overlay
->in_flight
, ==, 0);
1493 g_assert_cmpint(base
->quiesce_counter
, ==, 1);
1494 g_assert_cmpint(base_s
->drain_count
, ==, 1);
1495 g_assert_cmpint(overlay
->quiesce_counter
, ==, 1);
1496 g_assert_cmpint(overlay_s
->drain_count
, ==, 1);
1498 do_drain_end(BDRV_DRAIN
, base
);
1500 g_assert_cmpint(base
->quiesce_counter
, ==, 0);
1501 g_assert_cmpint(base_s
->drain_count
, ==, 0);
1502 g_assert_cmpint(overlay
->quiesce_counter
, ==, 0);
1503 g_assert_cmpint(overlay_s
->drain_count
, ==, 0);
1509 static void test_set_aio_context(void)
1511 BlockDriverState
*bs
;
1512 IOThread
*a
= iothread_new();
1513 IOThread
*b
= iothread_new();
1514 AioContext
*ctx_a
= iothread_get_aio_context(a
);
1515 AioContext
*ctx_b
= iothread_get_aio_context(b
);
1517 bs
= bdrv_new_open_driver(&bdrv_test
, "test-node", BDRV_O_RDWR
,
1520 bdrv_drained_begin(bs
);
1521 bdrv_try_set_aio_context(bs
, ctx_a
, &error_abort
);
1523 aio_context_acquire(ctx_a
);
1524 bdrv_drained_end(bs
);
1526 bdrv_drained_begin(bs
);
1527 bdrv_try_set_aio_context(bs
, ctx_b
, &error_abort
);
1528 aio_context_release(ctx_a
);
1529 aio_context_acquire(ctx_b
);
1530 bdrv_try_set_aio_context(bs
, qemu_get_aio_context(), &error_abort
);
1531 aio_context_release(ctx_b
);
1532 bdrv_drained_end(bs
);
1540 typedef struct TestDropBackingBlockJob
{
1542 bool should_complete
;
1544 BlockDriverState
*detach_also
;
1545 } TestDropBackingBlockJob
;
1547 static int coroutine_fn
test_drop_backing_job_run(Job
*job
, Error
**errp
)
1549 TestDropBackingBlockJob
*s
=
1550 container_of(job
, TestDropBackingBlockJob
, common
.job
);
1552 while (!s
->should_complete
) {
1553 job_sleep_ns(job
, 0);
1559 static void test_drop_backing_job_commit(Job
*job
)
1561 TestDropBackingBlockJob
*s
=
1562 container_of(job
, TestDropBackingBlockJob
, common
.job
);
1564 bdrv_set_backing_hd(blk_bs(s
->common
.blk
), NULL
, &error_abort
);
1565 bdrv_set_backing_hd(s
->detach_also
, NULL
, &error_abort
);
1567 *s
->did_complete
= true;
1570 static const BlockJobDriver test_drop_backing_job_driver
= {
1572 .instance_size
= sizeof(TestDropBackingBlockJob
),
1573 .free
= block_job_free
,
1574 .user_resume
= block_job_user_resume
,
1575 .drain
= block_job_drain
,
1576 .run
= test_drop_backing_job_run
,
1577 .commit
= test_drop_backing_job_commit
,
1582 * Creates a child node with three parent nodes on it, and then runs a
1583 * block job on the final one, parent-node-2.
1585 * The job is then asked to complete before a section where the child
1588 * Ending this section will undrain the child's parents, first
1589 * parent-node-2, then parent-node-1, then parent-node-0 -- the parent
1590 * list is in reverse order of how they were added. Ending the drain
1591 * on parent-node-2 will resume the job, thus completing it and
1592 * scheduling job_exit().
1594 * Ending the drain on parent-node-1 will poll the AioContext, which
1595 * lets job_exit() and thus test_drop_backing_job_commit() run. That
1596 * function first removes the child as parent-node-2's backing file.
1598 * In old (and buggy) implementations, there are two problems with
1600 * (A) bdrv_drain_invoke() polls for every node that leaves the
1601 * drained section. This means that job_exit() is scheduled
1602 * before the child has left the drained section. Its
1603 * quiesce_counter is therefore still 1 when it is removed from
1606 * (B) bdrv_replace_child_noperm() calls drained_end() on the old
1607 * child's parents as many times as the child is quiesced. This
1608 * means it will call drained_end() on parent-node-2 once.
1609 * Because parent-node-2 is no longer quiesced at this point, this
1612 * bdrv_replace_child_noperm() therefore must call drained_end() on
1613 * the parent only if it really is still drained because the child is
1616 * If removing child from parent-node-2 was successful (as it should
1617 * be), test_drop_backing_job_commit() will then also remove the child
1618 * from parent-node-0.
1620 * With an old version of our drain infrastructure ((A) above), that
1621 * resulted in the following flow:
1623 * 1. child attempts to leave its drained section. The call recurses
1626 * 2. parent-node-2 leaves the drained section. Polling in
1627 * bdrv_drain_invoke() will schedule job_exit().
1629 * 3. parent-node-1 leaves the drained section. Polling in
1630 * bdrv_drain_invoke() will run job_exit(), thus disconnecting
1631 * parent-node-0 from the child node.
1633 * 4. bdrv_parent_drained_end() uses a QLIST_FOREACH_SAFE() loop to
1634 * iterate over the parents. Thus, it now accesses the BdrvChild
1635 * object that used to connect parent-node-0 and the child node.
1636 * However, that object no longer exists, so it accesses a dangling
1639 * The solution is to only poll once when running a bdrv_drained_end()
1640 * operation, specifically at the end when all drained_end()
1641 * operations for all involved nodes have been scheduled.
1642 * Note that this also solves (A) above, thus hiding (B).
1644 static void test_blockjob_commit_by_drained_end(void)
1646 BlockDriverState
*bs_child
, *bs_parents
[3];
1647 TestDropBackingBlockJob
*job
;
1648 bool job_has_completed
= false;
1651 bs_child
= bdrv_new_open_driver(&bdrv_test
, "child-node", BDRV_O_RDWR
,
1654 for (i
= 0; i
< 3; i
++) {
1656 snprintf(name
, sizeof(name
), "parent-node-%i", i
);
1657 bs_parents
[i
] = bdrv_new_open_driver(&bdrv_test
, name
, BDRV_O_RDWR
,
1659 bdrv_set_backing_hd(bs_parents
[i
], bs_child
, &error_abort
);
1662 job
= block_job_create("job", &test_drop_backing_job_driver
, NULL
,
1663 bs_parents
[2], 0, BLK_PERM_ALL
, 0, 0, NULL
, NULL
,
1666 job
->detach_also
= bs_parents
[0];
1667 job
->did_complete
= &job_has_completed
;
1669 job_start(&job
->common
.job
);
1671 job
->should_complete
= true;
1672 bdrv_drained_begin(bs_child
);
1673 g_assert(!job_has_completed
);
1674 bdrv_drained_end(bs_child
);
1675 g_assert(job_has_completed
);
1677 bdrv_unref(bs_parents
[0]);
1678 bdrv_unref(bs_parents
[1]);
1679 bdrv_unref(bs_parents
[2]);
1680 bdrv_unref(bs_child
);
1684 typedef struct TestSimpleBlockJob
{
1686 bool should_complete
;
1688 } TestSimpleBlockJob
;
1690 static int coroutine_fn
test_simple_job_run(Job
*job
, Error
**errp
)
1692 TestSimpleBlockJob
*s
= container_of(job
, TestSimpleBlockJob
, common
.job
);
1694 while (!s
->should_complete
) {
1695 job_sleep_ns(job
, 0);
1701 static void test_simple_job_clean(Job
*job
)
1703 TestSimpleBlockJob
*s
= container_of(job
, TestSimpleBlockJob
, common
.job
);
1704 *s
->did_complete
= true;
1707 static const BlockJobDriver test_simple_job_driver
= {
1709 .instance_size
= sizeof(TestSimpleBlockJob
),
1710 .free
= block_job_free
,
1711 .user_resume
= block_job_user_resume
,
1712 .drain
= block_job_drain
,
1713 .run
= test_simple_job_run
,
1714 .clean
= test_simple_job_clean
,
1718 static int drop_intermediate_poll_update_filename(BdrvChild
*child
,
1719 BlockDriverState
*new_base
,
1720 const char *filename
,
1724 * We are free to poll here, which may change the block graph, if
1725 * it is not drained.
1728 /* If the job is not drained: Complete it, schedule job_exit() */
1729 aio_poll(qemu_get_current_aio_context(), false);
1730 /* If the job is not drained: Run job_exit(), finish the job */
1731 aio_poll(qemu_get_current_aio_context(), false);
1737 * Test a poll in the midst of bdrv_drop_intermediate().
1739 * bdrv_drop_intermediate() calls BdrvChildRole.update_filename(),
1740 * which can yield or poll. This may lead to graph changes, unless
1741 * the whole subtree in question is drained.
1743 * We test this on the following graph:
1759 * node-2 --chain--> node-1 --chain--> node-0
1761 * We drop node-1 with bdrv_drop_intermediate(top=node-1, base=node-0).
1763 * This first updates node-2's backing filename by invoking
1764 * drop_intermediate_poll_update_filename(), which polls twice. This
1765 * causes the job to finish, which in turns causes the job-node to be
1768 * bdrv_drop_intermediate() uses a QLIST_FOREACH_SAFE() loop, so it
1769 * already has a pointer to the BdrvChild edge between job-node and
1770 * node-1. When it tries to handle that edge, we probably get a
1771 * segmentation fault because the object no longer exists.
1774 * The solution is for bdrv_drop_intermediate() to drain top's
1775 * subtree. This prevents graph changes from happening just because
1776 * BdrvChildRole.update_filename() yields or polls. Thus, the block
1777 * job is paused during that drained section and must finish before or
1780 * (In addition, bdrv_replace_child() must keep the job paused.)
1782 static void test_drop_intermediate_poll(void)
1784 static BdrvChildRole chain_child_role
;
1785 BlockDriverState
*chain
[3];
1786 TestSimpleBlockJob
*job
;
1787 BlockDriverState
*job_node
;
1788 bool job_has_completed
= false;
1792 chain_child_role
= child_backing
;
1793 chain_child_role
.update_filename
= drop_intermediate_poll_update_filename
;
1795 for (i
= 0; i
< 3; i
++) {
1797 snprintf(name
, 32, "node-%i", i
);
1799 chain
[i
] = bdrv_new_open_driver(&bdrv_test
, name
, 0, &error_abort
);
1802 job_node
= bdrv_new_open_driver(&bdrv_test
, "job-node", BDRV_O_RDWR
,
1804 bdrv_set_backing_hd(job_node
, chain
[1], &error_abort
);
1807 * Establish the chain last, so the chain links are the first
1808 * elements in the BDS.parents lists
1810 for (i
= 0; i
< 3; i
++) {
1812 /* Takes the reference to chain[i - 1] */
1813 chain
[i
]->backing
= bdrv_attach_child(chain
[i
], chain
[i
- 1],
1814 "chain", &chain_child_role
,
1819 job
= block_job_create("job", &test_simple_job_driver
, NULL
, job_node
,
1820 0, BLK_PERM_ALL
, 0, 0, NULL
, NULL
, &error_abort
);
1822 /* The job has a reference now */
1823 bdrv_unref(job_node
);
1825 job
->did_complete
= &job_has_completed
;
1827 job_start(&job
->common
.job
);
1828 job
->should_complete
= true;
1830 g_assert(!job_has_completed
);
1831 ret
= bdrv_drop_intermediate(chain
[1], chain
[0], NULL
);
1833 g_assert(job_has_completed
);
1835 bdrv_unref(chain
[2]);
1839 typedef struct BDRVReplaceTestState
{
1846 bool yield_before_read
;
1848 Coroutine
*drain_co
;
1849 } BDRVReplaceTestState
;
1851 static void bdrv_replace_test_close(BlockDriverState
*bs
)
1856 * If @bs has a backing file:
1857 * Yield if .yield_before_read is true (and wait for drain_begin to
1859 * Forward the read to bs->backing. Set .has_read to true.
1860 * If drain_begin has woken us, wake it in turn.
1863 * Set .has_read to true and return success.
1865 static int coroutine_fn
bdrv_replace_test_co_preadv(BlockDriverState
*bs
,
1871 BDRVReplaceTestState
*s
= bs
->opaque
;
1876 g_assert(!s
->drain_count
);
1878 s
->io_co
= qemu_coroutine_self();
1879 if (s
->yield_before_read
) {
1880 s
->yield_before_read
= false;
1881 qemu_coroutine_yield();
1885 ret
= bdrv_preadv(bs
->backing
, offset
, qiov
);
1888 /* Wake up drain_co if it runs */
1890 aio_co_wake(s
->drain_co
);
1901 * If .drain_count is 0, wake up .io_co if there is one; and set
1903 * Increment .drain_count.
1905 static void coroutine_fn
bdrv_replace_test_co_drain_begin(BlockDriverState
*bs
)
1907 BDRVReplaceTestState
*s
= bs
->opaque
;
1909 if (!s
->drain_count
) {
1910 /* Keep waking io_co up until it is done */
1911 s
->drain_co
= qemu_coroutine_self();
1913 aio_co_wake(s
->io_co
);
1915 qemu_coroutine_yield();
1919 s
->was_drained
= true;
1925 * Reduce .drain_count, set .was_undrained once it reaches 0.
1926 * If .drain_count reaches 0 and the node has a backing file, issue a
1929 static void coroutine_fn
bdrv_replace_test_co_drain_end(BlockDriverState
*bs
)
1931 BDRVReplaceTestState
*s
= bs
->opaque
;
1933 g_assert(s
->drain_count
> 0);
1934 if (!--s
->drain_count
) {
1937 s
->was_undrained
= true;
1941 QEMUIOVector qiov
= QEMU_IOVEC_INIT_BUF(qiov
, &data
, 1);
1943 /* Queue a read request post-drain */
1944 ret
= bdrv_replace_test_co_preadv(bs
, 0, 1, &qiov
, 0);
1950 static BlockDriver bdrv_replace_test
= {
1951 .format_name
= "replace_test",
1952 .instance_size
= sizeof(BDRVReplaceTestState
),
1954 .bdrv_close
= bdrv_replace_test_close
,
1955 .bdrv_co_preadv
= bdrv_replace_test_co_preadv
,
1957 .bdrv_co_drain_begin
= bdrv_replace_test_co_drain_begin
,
1958 .bdrv_co_drain_end
= bdrv_replace_test_co_drain_end
,
1960 .bdrv_child_perm
= bdrv_format_default_perms
,
1963 static void coroutine_fn
test_replace_child_mid_drain_read_co(void *opaque
)
1968 ret
= blk_co_pread(opaque
, 0, 1, &data
, 0);
1973 * We test two things:
1974 * (1) bdrv_replace_child_noperm() must not undrain the parent if both
1975 * children are drained.
1976 * (2) bdrv_replace_child_noperm() must never flush I/O requests to a
1977 * drained child. If the old child is drained, it must flush I/O
1978 * requests after the new one has been attached. If the new child
1979 * is drained, it must flush I/O requests before the old one is
1982 * To do so, we create one parent node and two child nodes; then
1983 * attach one of the children (old_child_bs) to the parent, then
1984 * drain both old_child_bs and new_child_bs according to
1985 * old_drain_count and new_drain_count, respectively, and finally
1986 * we invoke bdrv_replace_node() to replace old_child_bs by
1989 * The test block driver we use here (bdrv_replace_test) has a read
1991 * - For the parent node, can optionally yield, and then forwards the
1992 * read to bdrv_preadv(),
1993 * - For the child node, just returns immediately.
1995 * If the read yields, the drain_begin function will wake it up.
1997 * The drain_end function issues a read on the parent once it is fully
1998 * undrained (which simulates requests starting to come in again).
2000 static void do_test_replace_child_mid_drain(int old_drain_count
,
2001 int new_drain_count
)
2003 BlockBackend
*parent_blk
;
2004 BlockDriverState
*parent_bs
;
2005 BlockDriverState
*old_child_bs
, *new_child_bs
;
2006 BDRVReplaceTestState
*parent_s
;
2007 BDRVReplaceTestState
*old_child_s
, *new_child_s
;
2011 parent_bs
= bdrv_new_open_driver(&bdrv_replace_test
, "parent", 0,
2013 parent_s
= parent_bs
->opaque
;
2015 parent_blk
= blk_new(qemu_get_aio_context(),
2016 BLK_PERM_CONSISTENT_READ
, BLK_PERM_ALL
);
2017 blk_insert_bs(parent_blk
, parent_bs
, &error_abort
);
2019 old_child_bs
= bdrv_new_open_driver(&bdrv_replace_test
, "old-child", 0,
2021 new_child_bs
= bdrv_new_open_driver(&bdrv_replace_test
, "new-child", 0,
2023 old_child_s
= old_child_bs
->opaque
;
2024 new_child_s
= new_child_bs
->opaque
;
2026 /* So that we can read something */
2027 parent_bs
->total_sectors
= 1;
2028 old_child_bs
->total_sectors
= 1;
2029 new_child_bs
->total_sectors
= 1;
2031 bdrv_ref(old_child_bs
);
2032 parent_bs
->backing
= bdrv_attach_child(parent_bs
, old_child_bs
, "child",
2033 &child_backing
, &error_abort
);
2035 for (i
= 0; i
< old_drain_count
; i
++) {
2036 bdrv_drained_begin(old_child_bs
);
2038 for (i
= 0; i
< new_drain_count
; i
++) {
2039 bdrv_drained_begin(new_child_bs
);
2042 if (!old_drain_count
) {
2044 * Start a read operation that will yield, so it will not
2045 * complete before the node is drained.
2047 parent_s
->yield_before_read
= true;
2048 io_co
= qemu_coroutine_create(test_replace_child_mid_drain_read_co
,
2050 qemu_coroutine_enter(io_co
);
2053 /* If we have started a read operation, it should have yielded */
2054 g_assert(!parent_s
->has_read
);
2056 /* Reset drained status so we can see what bdrv_replace_node() does */
2057 parent_s
->was_drained
= false;
2058 parent_s
->was_undrained
= false;
2060 g_assert(parent_bs
->quiesce_counter
== old_drain_count
);
2061 bdrv_replace_node(old_child_bs
, new_child_bs
, &error_abort
);
2062 g_assert(parent_bs
->quiesce_counter
== new_drain_count
);
2064 if (!old_drain_count
&& !new_drain_count
) {
2066 * From undrained to undrained drains and undrains the parent,
2067 * because bdrv_replace_node() contains a drained section for
2070 g_assert(parent_s
->was_drained
&& parent_s
->was_undrained
);
2071 } else if (!old_drain_count
&& new_drain_count
) {
2073 * From undrained to drained should drain the parent and keep
2076 g_assert(parent_s
->was_drained
&& !parent_s
->was_undrained
);
2077 } else if (old_drain_count
&& !new_drain_count
) {
2079 * From drained to undrained should undrain the parent and
2082 g_assert(!parent_s
->was_drained
&& parent_s
->was_undrained
);
2083 } else /* if (old_drain_count && new_drain_count) */ {
2085 * From drained to drained must not undrain the parent at any
2088 g_assert(!parent_s
->was_drained
&& !parent_s
->was_undrained
);
2091 if (!old_drain_count
|| !new_drain_count
) {
2093 * If !old_drain_count, we have started a read request before
2094 * bdrv_replace_node(). If !new_drain_count, the parent must
2095 * have been undrained at some point, and
2096 * bdrv_replace_test_co_drain_end() starts a read request
2099 g_assert(parent_s
->has_read
);
2102 * If the parent was never undrained, there is no way to start
2105 g_assert(!parent_s
->has_read
);
2108 /* A drained child must have not received any request */
2109 g_assert(!(old_drain_count
&& old_child_s
->has_read
));
2110 g_assert(!(new_drain_count
&& new_child_s
->has_read
));
2112 for (i
= 0; i
< new_drain_count
; i
++) {
2113 bdrv_drained_end(new_child_bs
);
2115 for (i
= 0; i
< old_drain_count
; i
++) {
2116 bdrv_drained_end(old_child_bs
);
2120 * By now, bdrv_replace_test_co_drain_end() must have been called
2121 * at some point while the new child was attached to the parent.
2123 g_assert(parent_s
->has_read
);
2124 g_assert(new_child_s
->has_read
);
2126 blk_unref(parent_blk
);
2127 bdrv_unref(parent_bs
);
2128 bdrv_unref(old_child_bs
);
2129 bdrv_unref(new_child_bs
);
2132 static void test_replace_child_mid_drain(void)
2134 int old_drain_count
, new_drain_count
;
2136 for (old_drain_count
= 0; old_drain_count
< 2; old_drain_count
++) {
2137 for (new_drain_count
= 0; new_drain_count
< 2; new_drain_count
++) {
2138 do_test_replace_child_mid_drain(old_drain_count
, new_drain_count
);
2143 int main(int argc
, char **argv
)
2148 qemu_init_main_loop(&error_abort
);
2150 g_test_init(&argc
, &argv
, NULL
);
2151 qemu_event_init(&done_event
, false);
2153 g_test_add_func("/bdrv-drain/driver-cb/drain_all", test_drv_cb_drain_all
);
2154 g_test_add_func("/bdrv-drain/driver-cb/drain", test_drv_cb_drain
);
2155 g_test_add_func("/bdrv-drain/driver-cb/drain_subtree",
2156 test_drv_cb_drain_subtree
);
2158 g_test_add_func("/bdrv-drain/driver-cb/co/drain_all",
2159 test_drv_cb_co_drain_all
);
2160 g_test_add_func("/bdrv-drain/driver-cb/co/drain", test_drv_cb_co_drain
);
2161 g_test_add_func("/bdrv-drain/driver-cb/co/drain_subtree",
2162 test_drv_cb_co_drain_subtree
);
2165 g_test_add_func("/bdrv-drain/quiesce/drain_all", test_quiesce_drain_all
);
2166 g_test_add_func("/bdrv-drain/quiesce/drain", test_quiesce_drain
);
2167 g_test_add_func("/bdrv-drain/quiesce/drain_subtree",
2168 test_quiesce_drain_subtree
);
2170 g_test_add_func("/bdrv-drain/quiesce/co/drain_all",
2171 test_quiesce_co_drain_all
);
2172 g_test_add_func("/bdrv-drain/quiesce/co/drain", test_quiesce_co_drain
);
2173 g_test_add_func("/bdrv-drain/quiesce/co/drain_subtree",
2174 test_quiesce_co_drain_subtree
);
2176 g_test_add_func("/bdrv-drain/nested", test_nested
);
2177 g_test_add_func("/bdrv-drain/multiparent", test_multiparent
);
2179 g_test_add_func("/bdrv-drain/graph-change/drain_subtree",
2180 test_graph_change_drain_subtree
);
2181 g_test_add_func("/bdrv-drain/graph-change/drain_all",
2182 test_graph_change_drain_all
);
2184 g_test_add_func("/bdrv-drain/iothread/drain_all", test_iothread_drain_all
);
2185 g_test_add_func("/bdrv-drain/iothread/drain", test_iothread_drain
);
2186 g_test_add_func("/bdrv-drain/iothread/drain_subtree",
2187 test_iothread_drain_subtree
);
2189 g_test_add_func("/bdrv-drain/blockjob/drain_all", test_blockjob_drain_all
);
2190 g_test_add_func("/bdrv-drain/blockjob/drain", test_blockjob_drain
);
2191 g_test_add_func("/bdrv-drain/blockjob/drain_subtree",
2192 test_blockjob_drain_subtree
);
2194 g_test_add_func("/bdrv-drain/blockjob/error/drain_all",
2195 test_blockjob_error_drain_all
);
2196 g_test_add_func("/bdrv-drain/blockjob/error/drain",
2197 test_blockjob_error_drain
);
2198 g_test_add_func("/bdrv-drain/blockjob/error/drain_subtree",
2199 test_blockjob_error_drain_subtree
);
2201 g_test_add_func("/bdrv-drain/blockjob/iothread/drain_all",
2202 test_blockjob_iothread_drain_all
);
2203 g_test_add_func("/bdrv-drain/blockjob/iothread/drain",
2204 test_blockjob_iothread_drain
);
2205 g_test_add_func("/bdrv-drain/blockjob/iothread/drain_subtree",
2206 test_blockjob_iothread_drain_subtree
);
2208 g_test_add_func("/bdrv-drain/blockjob/iothread/error/drain_all",
2209 test_blockjob_iothread_error_drain_all
);
2210 g_test_add_func("/bdrv-drain/blockjob/iothread/error/drain",
2211 test_blockjob_iothread_error_drain
);
2212 g_test_add_func("/bdrv-drain/blockjob/iothread/error/drain_subtree",
2213 test_blockjob_iothread_error_drain_subtree
);
2215 g_test_add_func("/bdrv-drain/deletion/drain", test_delete_by_drain
);
2216 g_test_add_func("/bdrv-drain/detach/drain_all", test_detach_by_drain_all
);
2217 g_test_add_func("/bdrv-drain/detach/drain", test_detach_by_drain
);
2218 g_test_add_func("/bdrv-drain/detach/drain_subtree", test_detach_by_drain_subtree
);
2219 g_test_add_func("/bdrv-drain/detach/parent_cb", test_detach_by_parent_cb
);
2220 g_test_add_func("/bdrv-drain/detach/driver_cb", test_detach_by_driver_cb
);
2222 g_test_add_func("/bdrv-drain/attach/drain", test_append_to_drained
);
2224 g_test_add_func("/bdrv-drain/set_aio_context", test_set_aio_context
);
2226 g_test_add_func("/bdrv-drain/blockjob/commit_by_drained_end",
2227 test_blockjob_commit_by_drained_end
);
2229 g_test_add_func("/bdrv-drain/bdrv_drop_intermediate/poll",
2230 test_drop_intermediate_poll
);
2232 g_test_add_func("/bdrv-drain/replace_child/mid-drain",
2233 test_replace_child_mid_drain
);
2236 qemu_event_destroy(&done_event
);