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"
30 #include "qemu/main-loop.h"
33 static QemuEvent done_event
;
35 typedef struct BDRVTestState
{
37 AioContext
*bh_indirection_ctx
;
38 bool sleep_in_drain_begin
;
41 static void coroutine_fn
bdrv_test_co_drain_begin(BlockDriverState
*bs
)
43 BDRVTestState
*s
= bs
->opaque
;
45 if (s
->sleep_in_drain_begin
) {
46 qemu_co_sleep_ns(QEMU_CLOCK_REALTIME
, 100000);
50 static void coroutine_fn
bdrv_test_co_drain_end(BlockDriverState
*bs
)
52 BDRVTestState
*s
= bs
->opaque
;
56 static void bdrv_test_close(BlockDriverState
*bs
)
58 BDRVTestState
*s
= bs
->opaque
;
59 g_assert_cmpint(s
->drain_count
, >, 0);
62 static void co_reenter_bh(void *opaque
)
67 static int coroutine_fn
bdrv_test_co_preadv(BlockDriverState
*bs
,
68 uint64_t offset
, uint64_t bytes
,
69 QEMUIOVector
*qiov
, int flags
)
71 BDRVTestState
*s
= bs
->opaque
;
73 /* We want this request to stay until the polling loop in drain waits for
74 * it to complete. We need to sleep a while as bdrv_drain_invoke() comes
75 * first and polls its result, too, but it shouldn't accidentally complete
76 * this request yet. */
77 qemu_co_sleep_ns(QEMU_CLOCK_REALTIME
, 100000);
79 if (s
->bh_indirection_ctx
) {
80 aio_bh_schedule_oneshot(s
->bh_indirection_ctx
, co_reenter_bh
,
81 qemu_coroutine_self());
82 qemu_coroutine_yield();
88 static void bdrv_test_child_perm(BlockDriverState
*bs
, BdrvChild
*c
,
89 const BdrvChildRole
*role
,
90 BlockReopenQueue
*reopen_queue
,
91 uint64_t perm
, uint64_t shared
,
92 uint64_t *nperm
, uint64_t *nshared
)
94 /* bdrv_format_default_perms() accepts only these two, so disguise
95 * detach_by_driver_cb_role as one of them. */
96 if (role
!= &child_file
&& role
!= &child_backing
) {
100 bdrv_format_default_perms(bs
, c
, role
, reopen_queue
, perm
, shared
,
104 static int bdrv_test_change_backing_file(BlockDriverState
*bs
,
105 const char *backing_file
,
106 const char *backing_fmt
)
111 static BlockDriver bdrv_test
= {
112 .format_name
= "test",
113 .instance_size
= sizeof(BDRVTestState
),
115 .bdrv_close
= bdrv_test_close
,
116 .bdrv_co_preadv
= bdrv_test_co_preadv
,
118 .bdrv_co_drain_begin
= bdrv_test_co_drain_begin
,
119 .bdrv_co_drain_end
= bdrv_test_co_drain_end
,
121 .bdrv_child_perm
= bdrv_test_child_perm
,
123 .bdrv_change_backing_file
= bdrv_test_change_backing_file
,
126 static void aio_ret_cb(void *opaque
, int ret
)
128 int *aio_ret
= opaque
;
132 typedef struct CallInCoroutineData
{
135 } CallInCoroutineData
;
137 static coroutine_fn
void call_in_coroutine_entry(void *opaque
)
139 CallInCoroutineData
*data
= opaque
;
145 static void call_in_coroutine(void (*entry
)(void))
148 CallInCoroutineData data
= {
153 co
= qemu_coroutine_create(call_in_coroutine_entry
, &data
);
154 qemu_coroutine_enter(co
);
156 aio_poll(qemu_get_aio_context(), true);
167 static void do_drain_begin(enum drain_type drain_type
, BlockDriverState
*bs
)
169 switch (drain_type
) {
170 case BDRV_DRAIN_ALL
: bdrv_drain_all_begin(); break;
171 case BDRV_DRAIN
: bdrv_drained_begin(bs
); break;
172 case BDRV_SUBTREE_DRAIN
: bdrv_subtree_drained_begin(bs
); break;
173 default: g_assert_not_reached();
177 static void do_drain_end(enum drain_type drain_type
, BlockDriverState
*bs
)
179 switch (drain_type
) {
180 case BDRV_DRAIN_ALL
: bdrv_drain_all_end(); break;
181 case BDRV_DRAIN
: bdrv_drained_end(bs
); break;
182 case BDRV_SUBTREE_DRAIN
: bdrv_subtree_drained_end(bs
); break;
183 default: g_assert_not_reached();
187 static void do_drain_begin_unlocked(enum drain_type drain_type
, BlockDriverState
*bs
)
189 if (drain_type
!= BDRV_DRAIN_ALL
) {
190 aio_context_acquire(bdrv_get_aio_context(bs
));
192 do_drain_begin(drain_type
, bs
);
193 if (drain_type
!= BDRV_DRAIN_ALL
) {
194 aio_context_release(bdrv_get_aio_context(bs
));
198 static void do_drain_end_unlocked(enum drain_type drain_type
, BlockDriverState
*bs
)
200 if (drain_type
!= BDRV_DRAIN_ALL
) {
201 aio_context_acquire(bdrv_get_aio_context(bs
));
203 do_drain_end(drain_type
, bs
);
204 if (drain_type
!= BDRV_DRAIN_ALL
) {
205 aio_context_release(bdrv_get_aio_context(bs
));
209 static void test_drv_cb_common(enum drain_type drain_type
, bool recursive
)
212 BlockDriverState
*bs
, *backing
;
213 BDRVTestState
*s
, *backing_s
;
217 QEMUIOVector qiov
= QEMU_IOVEC_INIT_BUF(qiov
, NULL
, 0);
219 blk
= blk_new(qemu_get_aio_context(), BLK_PERM_ALL
, BLK_PERM_ALL
);
220 bs
= bdrv_new_open_driver(&bdrv_test
, "test-node", BDRV_O_RDWR
,
223 blk_insert_bs(blk
, bs
, &error_abort
);
225 backing
= bdrv_new_open_driver(&bdrv_test
, "backing", 0, &error_abort
);
226 backing_s
= backing
->opaque
;
227 bdrv_set_backing_hd(bs
, backing
, &error_abort
);
229 /* Simple bdrv_drain_all_begin/end pair, check that CBs are called */
230 g_assert_cmpint(s
->drain_count
, ==, 0);
231 g_assert_cmpint(backing_s
->drain_count
, ==, 0);
233 do_drain_begin(drain_type
, bs
);
235 g_assert_cmpint(s
->drain_count
, ==, 1);
236 g_assert_cmpint(backing_s
->drain_count
, ==, !!recursive
);
238 do_drain_end(drain_type
, bs
);
240 g_assert_cmpint(s
->drain_count
, ==, 0);
241 g_assert_cmpint(backing_s
->drain_count
, ==, 0);
243 /* Now do the same while a request is pending */
244 aio_ret
= -EINPROGRESS
;
245 acb
= blk_aio_preadv(blk
, 0, &qiov
, 0, aio_ret_cb
, &aio_ret
);
246 g_assert(acb
!= NULL
);
247 g_assert_cmpint(aio_ret
, ==, -EINPROGRESS
);
249 g_assert_cmpint(s
->drain_count
, ==, 0);
250 g_assert_cmpint(backing_s
->drain_count
, ==, 0);
252 do_drain_begin(drain_type
, bs
);
254 g_assert_cmpint(aio_ret
, ==, 0);
255 g_assert_cmpint(s
->drain_count
, ==, 1);
256 g_assert_cmpint(backing_s
->drain_count
, ==, !!recursive
);
258 do_drain_end(drain_type
, bs
);
260 g_assert_cmpint(s
->drain_count
, ==, 0);
261 g_assert_cmpint(backing_s
->drain_count
, ==, 0);
268 static void test_drv_cb_drain_all(void)
270 test_drv_cb_common(BDRV_DRAIN_ALL
, true);
273 static void test_drv_cb_drain(void)
275 test_drv_cb_common(BDRV_DRAIN
, false);
278 static void test_drv_cb_drain_subtree(void)
280 test_drv_cb_common(BDRV_SUBTREE_DRAIN
, true);
283 static void test_drv_cb_co_drain_all(void)
285 call_in_coroutine(test_drv_cb_drain_all
);
288 static void test_drv_cb_co_drain(void)
290 call_in_coroutine(test_drv_cb_drain
);
293 static void test_drv_cb_co_drain_subtree(void)
295 call_in_coroutine(test_drv_cb_drain_subtree
);
298 static void test_quiesce_common(enum drain_type drain_type
, bool recursive
)
301 BlockDriverState
*bs
, *backing
;
303 blk
= blk_new(qemu_get_aio_context(), BLK_PERM_ALL
, BLK_PERM_ALL
);
304 bs
= bdrv_new_open_driver(&bdrv_test
, "test-node", BDRV_O_RDWR
,
306 blk_insert_bs(blk
, bs
, &error_abort
);
308 backing
= bdrv_new_open_driver(&bdrv_test
, "backing", 0, &error_abort
);
309 bdrv_set_backing_hd(bs
, backing
, &error_abort
);
311 g_assert_cmpint(bs
->quiesce_counter
, ==, 0);
312 g_assert_cmpint(backing
->quiesce_counter
, ==, 0);
314 do_drain_begin(drain_type
, bs
);
316 g_assert_cmpint(bs
->quiesce_counter
, ==, 1);
317 g_assert_cmpint(backing
->quiesce_counter
, ==, !!recursive
);
319 do_drain_end(drain_type
, bs
);
321 g_assert_cmpint(bs
->quiesce_counter
, ==, 0);
322 g_assert_cmpint(backing
->quiesce_counter
, ==, 0);
329 static void test_quiesce_drain_all(void)
331 test_quiesce_common(BDRV_DRAIN_ALL
, true);
334 static void test_quiesce_drain(void)
336 test_quiesce_common(BDRV_DRAIN
, false);
339 static void test_quiesce_drain_subtree(void)
341 test_quiesce_common(BDRV_SUBTREE_DRAIN
, true);
344 static void test_quiesce_co_drain_all(void)
346 call_in_coroutine(test_quiesce_drain_all
);
349 static void test_quiesce_co_drain(void)
351 call_in_coroutine(test_quiesce_drain
);
354 static void test_quiesce_co_drain_subtree(void)
356 call_in_coroutine(test_quiesce_drain_subtree
);
359 static void test_nested(void)
362 BlockDriverState
*bs
, *backing
;
363 BDRVTestState
*s
, *backing_s
;
364 enum drain_type outer
, inner
;
366 blk
= blk_new(qemu_get_aio_context(), BLK_PERM_ALL
, BLK_PERM_ALL
);
367 bs
= bdrv_new_open_driver(&bdrv_test
, "test-node", BDRV_O_RDWR
,
370 blk_insert_bs(blk
, bs
, &error_abort
);
372 backing
= bdrv_new_open_driver(&bdrv_test
, "backing", 0, &error_abort
);
373 backing_s
= backing
->opaque
;
374 bdrv_set_backing_hd(bs
, backing
, &error_abort
);
376 for (outer
= 0; outer
< DRAIN_TYPE_MAX
; outer
++) {
377 for (inner
= 0; inner
< DRAIN_TYPE_MAX
; inner
++) {
378 int backing_quiesce
= (outer
!= BDRV_DRAIN
) +
379 (inner
!= BDRV_DRAIN
);
381 g_assert_cmpint(bs
->quiesce_counter
, ==, 0);
382 g_assert_cmpint(backing
->quiesce_counter
, ==, 0);
383 g_assert_cmpint(s
->drain_count
, ==, 0);
384 g_assert_cmpint(backing_s
->drain_count
, ==, 0);
386 do_drain_begin(outer
, bs
);
387 do_drain_begin(inner
, bs
);
389 g_assert_cmpint(bs
->quiesce_counter
, ==, 2);
390 g_assert_cmpint(backing
->quiesce_counter
, ==, backing_quiesce
);
391 g_assert_cmpint(s
->drain_count
, ==, 2);
392 g_assert_cmpint(backing_s
->drain_count
, ==, backing_quiesce
);
394 do_drain_end(inner
, bs
);
395 do_drain_end(outer
, bs
);
397 g_assert_cmpint(bs
->quiesce_counter
, ==, 0);
398 g_assert_cmpint(backing
->quiesce_counter
, ==, 0);
399 g_assert_cmpint(s
->drain_count
, ==, 0);
400 g_assert_cmpint(backing_s
->drain_count
, ==, 0);
409 static void test_multiparent(void)
411 BlockBackend
*blk_a
, *blk_b
;
412 BlockDriverState
*bs_a
, *bs_b
, *backing
;
413 BDRVTestState
*a_s
, *b_s
, *backing_s
;
415 blk_a
= blk_new(qemu_get_aio_context(), BLK_PERM_ALL
, BLK_PERM_ALL
);
416 bs_a
= bdrv_new_open_driver(&bdrv_test
, "test-node-a", BDRV_O_RDWR
,
419 blk_insert_bs(blk_a
, bs_a
, &error_abort
);
421 blk_b
= blk_new(qemu_get_aio_context(), BLK_PERM_ALL
, BLK_PERM_ALL
);
422 bs_b
= bdrv_new_open_driver(&bdrv_test
, "test-node-b", BDRV_O_RDWR
,
425 blk_insert_bs(blk_b
, bs_b
, &error_abort
);
427 backing
= bdrv_new_open_driver(&bdrv_test
, "backing", 0, &error_abort
);
428 backing_s
= backing
->opaque
;
429 bdrv_set_backing_hd(bs_a
, backing
, &error_abort
);
430 bdrv_set_backing_hd(bs_b
, backing
, &error_abort
);
432 g_assert_cmpint(bs_a
->quiesce_counter
, ==, 0);
433 g_assert_cmpint(bs_b
->quiesce_counter
, ==, 0);
434 g_assert_cmpint(backing
->quiesce_counter
, ==, 0);
435 g_assert_cmpint(a_s
->drain_count
, ==, 0);
436 g_assert_cmpint(b_s
->drain_count
, ==, 0);
437 g_assert_cmpint(backing_s
->drain_count
, ==, 0);
439 do_drain_begin(BDRV_SUBTREE_DRAIN
, bs_a
);
441 g_assert_cmpint(bs_a
->quiesce_counter
, ==, 1);
442 g_assert_cmpint(bs_b
->quiesce_counter
, ==, 1);
443 g_assert_cmpint(backing
->quiesce_counter
, ==, 1);
444 g_assert_cmpint(a_s
->drain_count
, ==, 1);
445 g_assert_cmpint(b_s
->drain_count
, ==, 1);
446 g_assert_cmpint(backing_s
->drain_count
, ==, 1);
448 do_drain_begin(BDRV_SUBTREE_DRAIN
, bs_b
);
450 g_assert_cmpint(bs_a
->quiesce_counter
, ==, 2);
451 g_assert_cmpint(bs_b
->quiesce_counter
, ==, 2);
452 g_assert_cmpint(backing
->quiesce_counter
, ==, 2);
453 g_assert_cmpint(a_s
->drain_count
, ==, 2);
454 g_assert_cmpint(b_s
->drain_count
, ==, 2);
455 g_assert_cmpint(backing_s
->drain_count
, ==, 2);
457 do_drain_end(BDRV_SUBTREE_DRAIN
, bs_b
);
459 g_assert_cmpint(bs_a
->quiesce_counter
, ==, 1);
460 g_assert_cmpint(bs_b
->quiesce_counter
, ==, 1);
461 g_assert_cmpint(backing
->quiesce_counter
, ==, 1);
462 g_assert_cmpint(a_s
->drain_count
, ==, 1);
463 g_assert_cmpint(b_s
->drain_count
, ==, 1);
464 g_assert_cmpint(backing_s
->drain_count
, ==, 1);
466 do_drain_end(BDRV_SUBTREE_DRAIN
, bs_a
);
468 g_assert_cmpint(bs_a
->quiesce_counter
, ==, 0);
469 g_assert_cmpint(bs_b
->quiesce_counter
, ==, 0);
470 g_assert_cmpint(backing
->quiesce_counter
, ==, 0);
471 g_assert_cmpint(a_s
->drain_count
, ==, 0);
472 g_assert_cmpint(b_s
->drain_count
, ==, 0);
473 g_assert_cmpint(backing_s
->drain_count
, ==, 0);
482 static void test_graph_change_drain_subtree(void)
484 BlockBackend
*blk_a
, *blk_b
;
485 BlockDriverState
*bs_a
, *bs_b
, *backing
;
486 BDRVTestState
*a_s
, *b_s
, *backing_s
;
488 blk_a
= blk_new(qemu_get_aio_context(), BLK_PERM_ALL
, BLK_PERM_ALL
);
489 bs_a
= bdrv_new_open_driver(&bdrv_test
, "test-node-a", BDRV_O_RDWR
,
492 blk_insert_bs(blk_a
, bs_a
, &error_abort
);
494 blk_b
= blk_new(qemu_get_aio_context(), BLK_PERM_ALL
, BLK_PERM_ALL
);
495 bs_b
= bdrv_new_open_driver(&bdrv_test
, "test-node-b", BDRV_O_RDWR
,
498 blk_insert_bs(blk_b
, bs_b
, &error_abort
);
500 backing
= bdrv_new_open_driver(&bdrv_test
, "backing", 0, &error_abort
);
501 backing_s
= backing
->opaque
;
502 bdrv_set_backing_hd(bs_a
, backing
, &error_abort
);
504 g_assert_cmpint(bs_a
->quiesce_counter
, ==, 0);
505 g_assert_cmpint(bs_b
->quiesce_counter
, ==, 0);
506 g_assert_cmpint(backing
->quiesce_counter
, ==, 0);
507 g_assert_cmpint(a_s
->drain_count
, ==, 0);
508 g_assert_cmpint(b_s
->drain_count
, ==, 0);
509 g_assert_cmpint(backing_s
->drain_count
, ==, 0);
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_a
);
514 do_drain_begin(BDRV_SUBTREE_DRAIN
, bs_b
);
515 do_drain_begin(BDRV_SUBTREE_DRAIN
, bs_b
);
517 bdrv_set_backing_hd(bs_b
, backing
, &error_abort
);
518 g_assert_cmpint(bs_a
->quiesce_counter
, ==, 5);
519 g_assert_cmpint(bs_b
->quiesce_counter
, ==, 5);
520 g_assert_cmpint(backing
->quiesce_counter
, ==, 5);
521 g_assert_cmpint(a_s
->drain_count
, ==, 5);
522 g_assert_cmpint(b_s
->drain_count
, ==, 5);
523 g_assert_cmpint(backing_s
->drain_count
, ==, 5);
525 bdrv_set_backing_hd(bs_b
, NULL
, &error_abort
);
526 g_assert_cmpint(bs_a
->quiesce_counter
, ==, 3);
527 g_assert_cmpint(bs_b
->quiesce_counter
, ==, 2);
528 g_assert_cmpint(backing
->quiesce_counter
, ==, 3);
529 g_assert_cmpint(a_s
->drain_count
, ==, 3);
530 g_assert_cmpint(b_s
->drain_count
, ==, 2);
531 g_assert_cmpint(backing_s
->drain_count
, ==, 3);
533 bdrv_set_backing_hd(bs_b
, backing
, &error_abort
);
534 g_assert_cmpint(bs_a
->quiesce_counter
, ==, 5);
535 g_assert_cmpint(bs_b
->quiesce_counter
, ==, 5);
536 g_assert_cmpint(backing
->quiesce_counter
, ==, 5);
537 g_assert_cmpint(a_s
->drain_count
, ==, 5);
538 g_assert_cmpint(b_s
->drain_count
, ==, 5);
539 g_assert_cmpint(backing_s
->drain_count
, ==, 5);
541 do_drain_end(BDRV_SUBTREE_DRAIN
, bs_b
);
542 do_drain_end(BDRV_SUBTREE_DRAIN
, bs_b
);
543 do_drain_end(BDRV_SUBTREE_DRAIN
, bs_a
);
544 do_drain_end(BDRV_SUBTREE_DRAIN
, bs_a
);
545 do_drain_end(BDRV_SUBTREE_DRAIN
, bs_a
);
547 g_assert_cmpint(bs_a
->quiesce_counter
, ==, 0);
548 g_assert_cmpint(bs_b
->quiesce_counter
, ==, 0);
549 g_assert_cmpint(backing
->quiesce_counter
, ==, 0);
550 g_assert_cmpint(a_s
->drain_count
, ==, 0);
551 g_assert_cmpint(b_s
->drain_count
, ==, 0);
552 g_assert_cmpint(backing_s
->drain_count
, ==, 0);
561 static void test_graph_change_drain_all(void)
563 BlockBackend
*blk_a
, *blk_b
;
564 BlockDriverState
*bs_a
, *bs_b
;
565 BDRVTestState
*a_s
, *b_s
;
567 /* Create node A with a BlockBackend */
568 blk_a
= blk_new(qemu_get_aio_context(), BLK_PERM_ALL
, BLK_PERM_ALL
);
569 bs_a
= bdrv_new_open_driver(&bdrv_test
, "test-node-a", BDRV_O_RDWR
,
572 blk_insert_bs(blk_a
, bs_a
, &error_abort
);
574 g_assert_cmpint(bs_a
->quiesce_counter
, ==, 0);
575 g_assert_cmpint(a_s
->drain_count
, ==, 0);
577 /* Call bdrv_drain_all_begin() */
578 bdrv_drain_all_begin();
580 g_assert_cmpint(bs_a
->quiesce_counter
, ==, 1);
581 g_assert_cmpint(a_s
->drain_count
, ==, 1);
583 /* Create node B with a BlockBackend */
584 blk_b
= blk_new(qemu_get_aio_context(), BLK_PERM_ALL
, BLK_PERM_ALL
);
585 bs_b
= bdrv_new_open_driver(&bdrv_test
, "test-node-b", BDRV_O_RDWR
,
588 blk_insert_bs(blk_b
, bs_b
, &error_abort
);
590 g_assert_cmpint(bs_a
->quiesce_counter
, ==, 1);
591 g_assert_cmpint(bs_b
->quiesce_counter
, ==, 1);
592 g_assert_cmpint(a_s
->drain_count
, ==, 1);
593 g_assert_cmpint(b_s
->drain_count
, ==, 1);
595 /* Unref and finally delete node A */
598 g_assert_cmpint(bs_a
->quiesce_counter
, ==, 1);
599 g_assert_cmpint(bs_b
->quiesce_counter
, ==, 1);
600 g_assert_cmpint(a_s
->drain_count
, ==, 1);
601 g_assert_cmpint(b_s
->drain_count
, ==, 1);
605 g_assert_cmpint(bs_b
->quiesce_counter
, ==, 1);
606 g_assert_cmpint(b_s
->drain_count
, ==, 1);
608 /* End the drained section */
609 bdrv_drain_all_end();
611 g_assert_cmpint(bs_b
->quiesce_counter
, ==, 0);
612 g_assert_cmpint(b_s
->drain_count
, ==, 0);
618 struct test_iothread_data
{
619 BlockDriverState
*bs
;
620 enum drain_type drain_type
;
624 static void test_iothread_drain_entry(void *opaque
)
626 struct test_iothread_data
*data
= opaque
;
628 aio_context_acquire(bdrv_get_aio_context(data
->bs
));
629 do_drain_begin(data
->drain_type
, data
->bs
);
630 g_assert_cmpint(*data
->aio_ret
, ==, 0);
631 do_drain_end(data
->drain_type
, data
->bs
);
632 aio_context_release(bdrv_get_aio_context(data
->bs
));
634 qemu_event_set(&done_event
);
637 static void test_iothread_aio_cb(void *opaque
, int ret
)
639 int *aio_ret
= opaque
;
641 qemu_event_set(&done_event
);
644 static void test_iothread_main_thread_bh(void *opaque
)
646 struct test_iothread_data
*data
= opaque
;
648 /* Test that the AioContext is not yet locked in a random BH that is
649 * executed during drain, otherwise this would deadlock. */
650 aio_context_acquire(bdrv_get_aio_context(data
->bs
));
651 bdrv_flush(data
->bs
);
652 aio_context_release(bdrv_get_aio_context(data
->bs
));
656 * Starts an AIO request on a BDS that runs in the AioContext of iothread 1.
657 * The request involves a BH on iothread 2 before it can complete.
659 * @drain_thread = 0 means that do_drain_begin/end are called from the main
660 * thread, @drain_thread = 1 means that they are called from iothread 1. Drain
661 * for this BDS cannot be called from iothread 2 because only the main thread
662 * may do cross-AioContext polling.
664 static void test_iothread_common(enum drain_type drain_type
, int drain_thread
)
667 BlockDriverState
*bs
;
671 struct test_iothread_data data
;
673 IOThread
*a
= iothread_new();
674 IOThread
*b
= iothread_new();
675 AioContext
*ctx_a
= iothread_get_aio_context(a
);
676 AioContext
*ctx_b
= iothread_get_aio_context(b
);
678 QEMUIOVector qiov
= QEMU_IOVEC_INIT_BUF(qiov
, NULL
, 0);
680 /* bdrv_drain_all() may only be called from the main loop thread */
681 if (drain_type
== BDRV_DRAIN_ALL
&& drain_thread
!= 0) {
685 blk
= blk_new(qemu_get_aio_context(), BLK_PERM_ALL
, BLK_PERM_ALL
);
686 bs
= bdrv_new_open_driver(&bdrv_test
, "test-node", BDRV_O_RDWR
,
689 blk_insert_bs(blk
, bs
, &error_abort
);
690 blk_set_disable_request_queuing(blk
, true);
692 blk_set_aio_context(blk
, ctx_a
, &error_abort
);
693 aio_context_acquire(ctx_a
);
695 s
->bh_indirection_ctx
= ctx_b
;
697 aio_ret
= -EINPROGRESS
;
698 qemu_event_reset(&done_event
);
700 if (drain_thread
== 0) {
701 acb
= blk_aio_preadv(blk
, 0, &qiov
, 0, test_iothread_aio_cb
, &aio_ret
);
703 acb
= blk_aio_preadv(blk
, 0, &qiov
, 0, aio_ret_cb
, &aio_ret
);
705 g_assert(acb
!= NULL
);
706 g_assert_cmpint(aio_ret
, ==, -EINPROGRESS
);
708 aio_context_release(ctx_a
);
710 data
= (struct test_iothread_data
) {
712 .drain_type
= drain_type
,
716 switch (drain_thread
) {
718 if (drain_type
!= BDRV_DRAIN_ALL
) {
719 aio_context_acquire(ctx_a
);
722 aio_bh_schedule_oneshot(ctx_a
, test_iothread_main_thread_bh
, &data
);
724 /* The request is running on the IOThread a. Draining its block device
725 * will make sure that it has completed as far as the BDS is concerned,
726 * but the drain in this thread can continue immediately after
727 * bdrv_dec_in_flight() and aio_ret might be assigned only slightly
729 do_drain_begin(drain_type
, bs
);
730 g_assert_cmpint(bs
->in_flight
, ==, 0);
732 if (drain_type
!= BDRV_DRAIN_ALL
) {
733 aio_context_release(ctx_a
);
735 qemu_event_wait(&done_event
);
736 if (drain_type
!= BDRV_DRAIN_ALL
) {
737 aio_context_acquire(ctx_a
);
740 g_assert_cmpint(aio_ret
, ==, 0);
741 do_drain_end(drain_type
, bs
);
743 if (drain_type
!= BDRV_DRAIN_ALL
) {
744 aio_context_release(ctx_a
);
748 aio_bh_schedule_oneshot(ctx_a
, test_iothread_drain_entry
, &data
);
749 qemu_event_wait(&done_event
);
752 g_assert_not_reached();
755 aio_context_acquire(ctx_a
);
756 blk_set_aio_context(blk
, qemu_get_aio_context(), &error_abort
);
757 aio_context_release(ctx_a
);
767 static void test_iothread_drain_all(void)
769 test_iothread_common(BDRV_DRAIN_ALL
, 0);
770 test_iothread_common(BDRV_DRAIN_ALL
, 1);
773 static void test_iothread_drain(void)
775 test_iothread_common(BDRV_DRAIN
, 0);
776 test_iothread_common(BDRV_DRAIN
, 1);
779 static void test_iothread_drain_subtree(void)
781 test_iothread_common(BDRV_SUBTREE_DRAIN
, 0);
782 test_iothread_common(BDRV_SUBTREE_DRAIN
, 1);
786 typedef struct TestBlockJob
{
791 bool should_complete
;
794 static int test_job_prepare(Job
*job
)
796 TestBlockJob
*s
= container_of(job
, TestBlockJob
, common
.job
);
798 /* Provoke an AIO_WAIT_WHILE() call to verify there is no deadlock */
799 blk_flush(s
->common
.blk
);
800 return s
->prepare_ret
;
803 static void test_job_commit(Job
*job
)
805 TestBlockJob
*s
= container_of(job
, TestBlockJob
, common
.job
);
807 /* Provoke an AIO_WAIT_WHILE() call to verify there is no deadlock */
808 blk_flush(s
->common
.blk
);
811 static void test_job_abort(Job
*job
)
813 TestBlockJob
*s
= container_of(job
, TestBlockJob
, common
.job
);
815 /* Provoke an AIO_WAIT_WHILE() call to verify there is no deadlock */
816 blk_flush(s
->common
.blk
);
819 static int coroutine_fn
test_job_run(Job
*job
, Error
**errp
)
821 TestBlockJob
*s
= container_of(job
, TestBlockJob
, common
.job
);
823 /* We are running the actual job code past the pause point in
827 job_transition_to_ready(&s
->common
.job
);
828 while (!s
->should_complete
) {
829 /* Avoid job_sleep_ns() because it marks the job as !busy. We want to
830 * emulate some actual activity (probably some I/O) here so that drain
831 * has to wait for this activity to stop. */
832 qemu_co_sleep_ns(QEMU_CLOCK_REALTIME
, 1000000);
834 job_pause_point(&s
->common
.job
);
840 static void test_job_complete(Job
*job
, Error
**errp
)
842 TestBlockJob
*s
= container_of(job
, TestBlockJob
, common
.job
);
843 s
->should_complete
= true;
846 BlockJobDriver test_job_driver
= {
848 .instance_size
= sizeof(TestBlockJob
),
849 .free
= block_job_free
,
850 .user_resume
= block_job_user_resume
,
851 .drain
= block_job_drain
,
853 .complete
= test_job_complete
,
854 .prepare
= test_job_prepare
,
855 .commit
= test_job_commit
,
856 .abort
= test_job_abort
,
860 enum test_job_result
{
863 TEST_JOB_FAIL_PREPARE
,
866 enum test_job_drain_node
{
868 TEST_JOB_DRAIN_SRC_CHILD
,
869 TEST_JOB_DRAIN_SRC_PARENT
,
872 static void test_blockjob_common_drain_node(enum drain_type drain_type
,
874 enum test_job_result result
,
875 enum test_job_drain_node drain_node
)
877 BlockBackend
*blk_src
, *blk_target
;
878 BlockDriverState
*src
, *src_backing
, *src_overlay
, *target
, *drain_bs
;
881 IOThread
*iothread
= NULL
;
885 src
= bdrv_new_open_driver(&bdrv_test
, "source", BDRV_O_RDWR
,
887 src_backing
= bdrv_new_open_driver(&bdrv_test
, "source-backing",
888 BDRV_O_RDWR
, &error_abort
);
889 src_overlay
= bdrv_new_open_driver(&bdrv_test
, "source-overlay",
890 BDRV_O_RDWR
, &error_abort
);
892 bdrv_set_backing_hd(src_overlay
, src
, &error_abort
);
894 bdrv_set_backing_hd(src
, src_backing
, &error_abort
);
895 bdrv_unref(src_backing
);
897 blk_src
= blk_new(qemu_get_aio_context(), BLK_PERM_ALL
, BLK_PERM_ALL
);
898 blk_insert_bs(blk_src
, src_overlay
, &error_abort
);
900 switch (drain_node
) {
901 case TEST_JOB_DRAIN_SRC
:
904 case TEST_JOB_DRAIN_SRC_CHILD
:
905 drain_bs
= src_backing
;
907 case TEST_JOB_DRAIN_SRC_PARENT
:
908 drain_bs
= src_overlay
;
911 g_assert_not_reached();
915 iothread
= iothread_new();
916 ctx
= iothread_get_aio_context(iothread
);
917 blk_set_aio_context(blk_src
, ctx
, &error_abort
);
919 ctx
= qemu_get_aio_context();
922 target
= bdrv_new_open_driver(&bdrv_test
, "target", BDRV_O_RDWR
,
924 blk_target
= blk_new(qemu_get_aio_context(), BLK_PERM_ALL
, BLK_PERM_ALL
);
925 blk_insert_bs(blk_target
, target
, &error_abort
);
926 blk_set_allow_aio_context_change(blk_target
, true);
928 aio_context_acquire(ctx
);
929 tjob
= block_job_create("job0", &test_job_driver
, NULL
, src
,
931 0, 0, NULL
, NULL
, &error_abort
);
933 block_job_add_bdrv(job
, "target", target
, 0, BLK_PERM_ALL
, &error_abort
);
936 case TEST_JOB_SUCCESS
:
938 case TEST_JOB_FAIL_RUN
:
939 tjob
->run_ret
= -EIO
;
941 case TEST_JOB_FAIL_PREPARE
:
942 tjob
->prepare_ret
= -EIO
;
946 job_start(&job
->job
);
947 aio_context_release(ctx
);
950 /* job_co_entry() is run in the I/O thread, wait for the actual job
951 * code to start (we don't want to catch the job in the pause point in
953 while (!tjob
->running
) {
954 aio_poll(qemu_get_aio_context(), false);
958 g_assert_cmpint(job
->job
.pause_count
, ==, 0);
959 g_assert_false(job
->job
.paused
);
960 g_assert_true(tjob
->running
);
961 g_assert_true(job
->job
.busy
); /* We're in qemu_co_sleep_ns() */
963 do_drain_begin_unlocked(drain_type
, drain_bs
);
965 if (drain_type
== BDRV_DRAIN_ALL
) {
966 /* bdrv_drain_all() drains both src and target */
967 g_assert_cmpint(job
->job
.pause_count
, ==, 2);
969 g_assert_cmpint(job
->job
.pause_count
, ==, 1);
971 g_assert_true(job
->job
.paused
);
972 g_assert_false(job
->job
.busy
); /* The job is paused */
974 do_drain_end_unlocked(drain_type
, drain_bs
);
977 /* paused is reset in the I/O thread, wait for it */
978 while (job
->job
.paused
) {
979 aio_poll(qemu_get_aio_context(), false);
983 g_assert_cmpint(job
->job
.pause_count
, ==, 0);
984 g_assert_false(job
->job
.paused
);
985 g_assert_true(job
->job
.busy
); /* We're in qemu_co_sleep_ns() */
987 do_drain_begin_unlocked(drain_type
, target
);
989 if (drain_type
== BDRV_DRAIN_ALL
) {
990 /* bdrv_drain_all() drains both src and target */
991 g_assert_cmpint(job
->job
.pause_count
, ==, 2);
993 g_assert_cmpint(job
->job
.pause_count
, ==, 1);
995 g_assert_true(job
->job
.paused
);
996 g_assert_false(job
->job
.busy
); /* The job is paused */
998 do_drain_end_unlocked(drain_type
, target
);
1001 /* paused is reset in the I/O thread, wait for it */
1002 while (job
->job
.paused
) {
1003 aio_poll(qemu_get_aio_context(), false);
1007 g_assert_cmpint(job
->job
.pause_count
, ==, 0);
1008 g_assert_false(job
->job
.paused
);
1009 g_assert_true(job
->job
.busy
); /* We're in qemu_co_sleep_ns() */
1011 aio_context_acquire(ctx
);
1012 ret
= job_complete_sync(&job
->job
, &error_abort
);
1013 g_assert_cmpint(ret
, ==, (result
== TEST_JOB_SUCCESS
? 0 : -EIO
));
1016 blk_set_aio_context(blk_src
, qemu_get_aio_context(), &error_abort
);
1017 assert(blk_get_aio_context(blk_target
) == qemu_get_aio_context());
1019 aio_context_release(ctx
);
1022 blk_unref(blk_target
);
1023 bdrv_unref(src_overlay
);
1027 iothread_join(iothread
);
1031 static void test_blockjob_common(enum drain_type drain_type
, bool use_iothread
,
1032 enum test_job_result result
)
1034 test_blockjob_common_drain_node(drain_type
, use_iothread
, result
,
1035 TEST_JOB_DRAIN_SRC
);
1036 test_blockjob_common_drain_node(drain_type
, use_iothread
, result
,
1037 TEST_JOB_DRAIN_SRC_CHILD
);
1038 if (drain_type
== BDRV_SUBTREE_DRAIN
) {
1039 test_blockjob_common_drain_node(drain_type
, use_iothread
, result
,
1040 TEST_JOB_DRAIN_SRC_PARENT
);
1044 static void test_blockjob_drain_all(void)
1046 test_blockjob_common(BDRV_DRAIN_ALL
, false, TEST_JOB_SUCCESS
);
1049 static void test_blockjob_drain(void)
1051 test_blockjob_common(BDRV_DRAIN
, false, TEST_JOB_SUCCESS
);
1054 static void test_blockjob_drain_subtree(void)
1056 test_blockjob_common(BDRV_SUBTREE_DRAIN
, false, TEST_JOB_SUCCESS
);
1059 static void test_blockjob_error_drain_all(void)
1061 test_blockjob_common(BDRV_DRAIN_ALL
, false, TEST_JOB_FAIL_RUN
);
1062 test_blockjob_common(BDRV_DRAIN_ALL
, false, TEST_JOB_FAIL_PREPARE
);
1065 static void test_blockjob_error_drain(void)
1067 test_blockjob_common(BDRV_DRAIN
, false, TEST_JOB_FAIL_RUN
);
1068 test_blockjob_common(BDRV_DRAIN
, false, TEST_JOB_FAIL_PREPARE
);
1071 static void test_blockjob_error_drain_subtree(void)
1073 test_blockjob_common(BDRV_SUBTREE_DRAIN
, false, TEST_JOB_FAIL_RUN
);
1074 test_blockjob_common(BDRV_SUBTREE_DRAIN
, false, TEST_JOB_FAIL_PREPARE
);
1077 static void test_blockjob_iothread_drain_all(void)
1079 test_blockjob_common(BDRV_DRAIN_ALL
, true, TEST_JOB_SUCCESS
);
1082 static void test_blockjob_iothread_drain(void)
1084 test_blockjob_common(BDRV_DRAIN
, true, TEST_JOB_SUCCESS
);
1087 static void test_blockjob_iothread_drain_subtree(void)
1089 test_blockjob_common(BDRV_SUBTREE_DRAIN
, true, TEST_JOB_SUCCESS
);
1092 static void test_blockjob_iothread_error_drain_all(void)
1094 test_blockjob_common(BDRV_DRAIN_ALL
, true, TEST_JOB_FAIL_RUN
);
1095 test_blockjob_common(BDRV_DRAIN_ALL
, true, TEST_JOB_FAIL_PREPARE
);
1098 static void test_blockjob_iothread_error_drain(void)
1100 test_blockjob_common(BDRV_DRAIN
, true, TEST_JOB_FAIL_RUN
);
1101 test_blockjob_common(BDRV_DRAIN
, true, TEST_JOB_FAIL_PREPARE
);
1104 static void test_blockjob_iothread_error_drain_subtree(void)
1106 test_blockjob_common(BDRV_SUBTREE_DRAIN
, true, TEST_JOB_FAIL_RUN
);
1107 test_blockjob_common(BDRV_SUBTREE_DRAIN
, true, TEST_JOB_FAIL_PREPARE
);
1111 typedef struct BDRVTestTopState
{
1112 BdrvChild
*wait_child
;
1115 static void bdrv_test_top_close(BlockDriverState
*bs
)
1117 BdrvChild
*c
, *next_c
;
1118 QLIST_FOREACH_SAFE(c
, &bs
->children
, next
, next_c
) {
1119 bdrv_unref_child(bs
, c
);
1123 static int coroutine_fn
bdrv_test_top_co_preadv(BlockDriverState
*bs
,
1124 uint64_t offset
, uint64_t bytes
,
1125 QEMUIOVector
*qiov
, int flags
)
1127 BDRVTestTopState
*tts
= bs
->opaque
;
1128 return bdrv_co_preadv(tts
->wait_child
, offset
, bytes
, qiov
, flags
);
1131 static BlockDriver bdrv_test_top_driver
= {
1132 .format_name
= "test_top_driver",
1133 .instance_size
= sizeof(BDRVTestTopState
),
1135 .bdrv_close
= bdrv_test_top_close
,
1136 .bdrv_co_preadv
= bdrv_test_top_co_preadv
,
1138 .bdrv_child_perm
= bdrv_format_default_perms
,
1141 typedef struct TestCoDeleteByDrainData
{
1143 bool detach_instead_of_delete
;
1145 } TestCoDeleteByDrainData
;
1147 static void coroutine_fn
test_co_delete_by_drain(void *opaque
)
1149 TestCoDeleteByDrainData
*dbdd
= opaque
;
1150 BlockBackend
*blk
= dbdd
->blk
;
1151 BlockDriverState
*bs
= blk_bs(blk
);
1152 BDRVTestTopState
*tts
= bs
->opaque
;
1153 void *buffer
= g_malloc(65536);
1154 QEMUIOVector qiov
= QEMU_IOVEC_INIT_BUF(qiov
, buffer
, 65536);
1156 /* Pretend some internal write operation from parent to child.
1157 * Important: We have to read from the child, not from the parent!
1158 * Draining works by first propagating it all up the tree to the
1159 * root and then waiting for drainage from root to the leaves
1160 * (protocol nodes). If we have a request waiting on the root,
1161 * everything will be drained before we go back down the tree, but
1162 * we do not want that. We want to be in the middle of draining
1163 * when this following requests returns. */
1164 bdrv_co_preadv(tts
->wait_child
, 0, 65536, &qiov
, 0);
1166 g_assert_cmpint(bs
->refcnt
, ==, 1);
1168 if (!dbdd
->detach_instead_of_delete
) {
1171 BdrvChild
*c
, *next_c
;
1172 QLIST_FOREACH_SAFE(c
, &bs
->children
, next
, next_c
) {
1173 bdrv_unref_child(bs
, c
);
1182 * Test what happens when some BDS has some children, you drain one of
1183 * them and this results in the BDS being deleted.
1185 * If @detach_instead_of_delete is set, the BDS is not going to be
1186 * deleted but will only detach all of its children.
1188 static void do_test_delete_by_drain(bool detach_instead_of_delete
,
1189 enum drain_type drain_type
)
1192 BlockDriverState
*bs
, *child_bs
, *null_bs
;
1193 BDRVTestTopState
*tts
;
1194 TestCoDeleteByDrainData dbdd
;
1197 bs
= bdrv_new_open_driver(&bdrv_test_top_driver
, "top", BDRV_O_RDWR
,
1199 bs
->total_sectors
= 65536 >> BDRV_SECTOR_BITS
;
1202 null_bs
= bdrv_open("null-co://", NULL
, NULL
, BDRV_O_RDWR
| BDRV_O_PROTOCOL
,
1204 bdrv_attach_child(bs
, null_bs
, "null-child", &child_file
, &error_abort
);
1206 /* This child will be the one to pass to requests through to, and
1207 * it will stall until a drain occurs */
1208 child_bs
= bdrv_new_open_driver(&bdrv_test
, "child", BDRV_O_RDWR
,
1210 child_bs
->total_sectors
= 65536 >> BDRV_SECTOR_BITS
;
1211 /* Takes our reference to child_bs */
1212 tts
->wait_child
= bdrv_attach_child(bs
, child_bs
, "wait-child", &child_file
,
1215 /* This child is just there to be deleted
1216 * (for detach_instead_of_delete == true) */
1217 null_bs
= bdrv_open("null-co://", NULL
, NULL
, BDRV_O_RDWR
| BDRV_O_PROTOCOL
,
1219 bdrv_attach_child(bs
, null_bs
, "null-child", &child_file
, &error_abort
);
1221 blk
= blk_new(qemu_get_aio_context(), BLK_PERM_ALL
, BLK_PERM_ALL
);
1222 blk_insert_bs(blk
, bs
, &error_abort
);
1224 /* Referenced by blk now */
1227 g_assert_cmpint(bs
->refcnt
, ==, 1);
1228 g_assert_cmpint(child_bs
->refcnt
, ==, 1);
1229 g_assert_cmpint(null_bs
->refcnt
, ==, 1);
1232 dbdd
= (TestCoDeleteByDrainData
){
1234 .detach_instead_of_delete
= detach_instead_of_delete
,
1237 co
= qemu_coroutine_create(test_co_delete_by_drain
, &dbdd
);
1238 qemu_coroutine_enter(co
);
1240 /* Drain the child while the read operation is still pending.
1241 * This should result in the operation finishing and
1242 * test_co_delete_by_drain() resuming. Thus, @bs will be deleted
1243 * and the coroutine will exit while this drain operation is still
1245 switch (drain_type
) {
1248 bdrv_drain(child_bs
);
1249 bdrv_unref(child_bs
);
1251 case BDRV_SUBTREE_DRAIN
:
1252 /* Would have to ref/unref bs here for !detach_instead_of_delete, but
1253 * then the whole test becomes pointless because the graph changes
1254 * don't occur during the drain any more. */
1255 assert(detach_instead_of_delete
);
1256 bdrv_subtree_drained_begin(bs
);
1257 bdrv_subtree_drained_end(bs
);
1259 case BDRV_DRAIN_ALL
:
1260 bdrv_drain_all_begin();
1261 bdrv_drain_all_end();
1264 g_assert_not_reached();
1267 while (!dbdd
.done
) {
1268 aio_poll(qemu_get_aio_context(), true);
1271 if (detach_instead_of_delete
) {
1272 /* Here, the reference has not passed over to the coroutine,
1273 * so we have to delete the BB ourselves */
1278 static void test_delete_by_drain(void)
1280 do_test_delete_by_drain(false, BDRV_DRAIN
);
1283 static void test_detach_by_drain_all(void)
1285 do_test_delete_by_drain(true, BDRV_DRAIN_ALL
);
1288 static void test_detach_by_drain(void)
1290 do_test_delete_by_drain(true, BDRV_DRAIN
);
1293 static void test_detach_by_drain_subtree(void)
1295 do_test_delete_by_drain(true, BDRV_SUBTREE_DRAIN
);
1299 struct detach_by_parent_data
{
1300 BlockDriverState
*parent_b
;
1302 BlockDriverState
*c
;
1306 static struct detach_by_parent_data detach_by_parent_data
;
1308 static void detach_indirect_bh(void *opaque
)
1310 struct detach_by_parent_data
*data
= opaque
;
1312 bdrv_unref_child(data
->parent_b
, data
->child_b
);
1315 data
->child_c
= bdrv_attach_child(data
->parent_b
, data
->c
, "PB-C",
1316 &child_file
, &error_abort
);
1319 static void detach_by_parent_aio_cb(void *opaque
, int ret
)
1321 struct detach_by_parent_data
*data
= &detach_by_parent_data
;
1323 g_assert_cmpint(ret
, ==, 0);
1324 if (data
->by_parent_cb
) {
1325 detach_indirect_bh(data
);
1329 static void detach_by_driver_cb_drained_begin(BdrvChild
*child
)
1331 aio_bh_schedule_oneshot(qemu_get_current_aio_context(),
1332 detach_indirect_bh
, &detach_by_parent_data
);
1333 child_file
.drained_begin(child
);
1336 static BdrvChildRole detach_by_driver_cb_role
;
1345 * by_parent_cb == true: Test that parent callbacks don't poll
1347 * PA has a pending write request whose callback changes the child nodes of
1348 * PB: It removes B and adds C instead. The subtree of PB is drained, which
1349 * will indirectly drain the write request, too.
1351 * by_parent_cb == false: Test that bdrv_drain_invoke() doesn't poll
1353 * PA's BdrvChildRole has a .drained_begin callback that schedules a BH
1354 * that does the same graph change. If bdrv_drain_invoke() calls it, the
1355 * state is messed up, but if it is only polled in the single
1356 * BDRV_POLL_WHILE() at the end of the drain, this should work fine.
1358 static void test_detach_indirect(bool by_parent_cb
)
1361 BlockDriverState
*parent_a
, *parent_b
, *a
, *b
, *c
;
1362 BdrvChild
*child_a
, *child_b
;
1365 QEMUIOVector qiov
= QEMU_IOVEC_INIT_BUF(qiov
, NULL
, 0);
1367 if (!by_parent_cb
) {
1368 detach_by_driver_cb_role
= child_file
;
1369 detach_by_driver_cb_role
.drained_begin
=
1370 detach_by_driver_cb_drained_begin
;
1373 /* Create all involved nodes */
1374 parent_a
= bdrv_new_open_driver(&bdrv_test
, "parent-a", BDRV_O_RDWR
,
1376 parent_b
= bdrv_new_open_driver(&bdrv_test
, "parent-b", 0,
1379 a
= bdrv_new_open_driver(&bdrv_test
, "a", BDRV_O_RDWR
, &error_abort
);
1380 b
= bdrv_new_open_driver(&bdrv_test
, "b", BDRV_O_RDWR
, &error_abort
);
1381 c
= bdrv_new_open_driver(&bdrv_test
, "c", BDRV_O_RDWR
, &error_abort
);
1383 /* blk is a BB for parent-a */
1384 blk
= blk_new(qemu_get_aio_context(), BLK_PERM_ALL
, BLK_PERM_ALL
);
1385 blk_insert_bs(blk
, parent_a
, &error_abort
);
1386 bdrv_unref(parent_a
);
1388 /* If we want to get bdrv_drain_invoke() to call aio_poll(), the driver
1389 * callback must not return immediately. */
1390 if (!by_parent_cb
) {
1391 BDRVTestState
*s
= parent_a
->opaque
;
1392 s
->sleep_in_drain_begin
= true;
1395 /* Set child relationships */
1398 child_b
= bdrv_attach_child(parent_b
, b
, "PB-B", &child_file
, &error_abort
);
1399 child_a
= bdrv_attach_child(parent_b
, a
, "PB-A", &child_backing
, &error_abort
);
1402 bdrv_attach_child(parent_a
, a
, "PA-A",
1403 by_parent_cb
? &child_file
: &detach_by_driver_cb_role
,
1406 g_assert_cmpint(parent_a
->refcnt
, ==, 1);
1407 g_assert_cmpint(parent_b
->refcnt
, ==, 1);
1408 g_assert_cmpint(a
->refcnt
, ==, 3);
1409 g_assert_cmpint(b
->refcnt
, ==, 2);
1410 g_assert_cmpint(c
->refcnt
, ==, 1);
1412 g_assert(QLIST_FIRST(&parent_b
->children
) == child_a
);
1413 g_assert(QLIST_NEXT(child_a
, next
) == child_b
);
1414 g_assert(QLIST_NEXT(child_b
, next
) == NULL
);
1416 /* Start the evil write request */
1417 detach_by_parent_data
= (struct detach_by_parent_data
) {
1418 .parent_b
= parent_b
,
1421 .by_parent_cb
= by_parent_cb
,
1423 acb
= blk_aio_preadv(blk
, 0, &qiov
, 0, detach_by_parent_aio_cb
, NULL
);
1424 g_assert(acb
!= NULL
);
1426 /* Drain and check the expected result */
1427 bdrv_subtree_drained_begin(parent_b
);
1429 g_assert(detach_by_parent_data
.child_c
!= NULL
);
1431 g_assert_cmpint(parent_a
->refcnt
, ==, 1);
1432 g_assert_cmpint(parent_b
->refcnt
, ==, 1);
1433 g_assert_cmpint(a
->refcnt
, ==, 3);
1434 g_assert_cmpint(b
->refcnt
, ==, 1);
1435 g_assert_cmpint(c
->refcnt
, ==, 2);
1437 g_assert(QLIST_FIRST(&parent_b
->children
) == detach_by_parent_data
.child_c
);
1438 g_assert(QLIST_NEXT(detach_by_parent_data
.child_c
, next
) == child_a
);
1439 g_assert(QLIST_NEXT(child_a
, next
) == NULL
);
1441 g_assert_cmpint(parent_a
->quiesce_counter
, ==, 1);
1442 g_assert_cmpint(parent_b
->quiesce_counter
, ==, 1);
1443 g_assert_cmpint(a
->quiesce_counter
, ==, 1);
1444 g_assert_cmpint(b
->quiesce_counter
, ==, 0);
1445 g_assert_cmpint(c
->quiesce_counter
, ==, 1);
1447 bdrv_subtree_drained_end(parent_b
);
1449 bdrv_unref(parent_b
);
1452 g_assert_cmpint(a
->refcnt
, ==, 1);
1453 g_assert_cmpint(b
->refcnt
, ==, 1);
1454 g_assert_cmpint(c
->refcnt
, ==, 1);
1460 static void test_detach_by_parent_cb(void)
1462 test_detach_indirect(true);
1465 static void test_detach_by_driver_cb(void)
1467 test_detach_indirect(false);
1470 static void test_append_to_drained(void)
1473 BlockDriverState
*base
, *overlay
;
1474 BDRVTestState
*base_s
, *overlay_s
;
1476 blk
= blk_new(qemu_get_aio_context(), BLK_PERM_ALL
, BLK_PERM_ALL
);
1477 base
= bdrv_new_open_driver(&bdrv_test
, "base", BDRV_O_RDWR
, &error_abort
);
1478 base_s
= base
->opaque
;
1479 blk_insert_bs(blk
, base
, &error_abort
);
1481 overlay
= bdrv_new_open_driver(&bdrv_test
, "overlay", BDRV_O_RDWR
,
1483 overlay_s
= overlay
->opaque
;
1485 do_drain_begin(BDRV_DRAIN
, base
);
1486 g_assert_cmpint(base
->quiesce_counter
, ==, 1);
1487 g_assert_cmpint(base_s
->drain_count
, ==, 1);
1488 g_assert_cmpint(base
->in_flight
, ==, 0);
1490 /* Takes ownership of overlay, so we don't have to unref it later */
1491 bdrv_append(overlay
, base
, &error_abort
);
1492 g_assert_cmpint(base
->in_flight
, ==, 0);
1493 g_assert_cmpint(overlay
->in_flight
, ==, 0);
1495 g_assert_cmpint(base
->quiesce_counter
, ==, 1);
1496 g_assert_cmpint(base_s
->drain_count
, ==, 1);
1497 g_assert_cmpint(overlay
->quiesce_counter
, ==, 1);
1498 g_assert_cmpint(overlay_s
->drain_count
, ==, 1);
1500 do_drain_end(BDRV_DRAIN
, base
);
1502 g_assert_cmpint(base
->quiesce_counter
, ==, 0);
1503 g_assert_cmpint(base_s
->drain_count
, ==, 0);
1504 g_assert_cmpint(overlay
->quiesce_counter
, ==, 0);
1505 g_assert_cmpint(overlay_s
->drain_count
, ==, 0);
1511 static void test_set_aio_context(void)
1513 BlockDriverState
*bs
;
1514 IOThread
*a
= iothread_new();
1515 IOThread
*b
= iothread_new();
1516 AioContext
*ctx_a
= iothread_get_aio_context(a
);
1517 AioContext
*ctx_b
= iothread_get_aio_context(b
);
1519 bs
= bdrv_new_open_driver(&bdrv_test
, "test-node", BDRV_O_RDWR
,
1522 bdrv_drained_begin(bs
);
1523 bdrv_try_set_aio_context(bs
, ctx_a
, &error_abort
);
1525 aio_context_acquire(ctx_a
);
1526 bdrv_drained_end(bs
);
1528 bdrv_drained_begin(bs
);
1529 bdrv_try_set_aio_context(bs
, ctx_b
, &error_abort
);
1530 aio_context_release(ctx_a
);
1531 aio_context_acquire(ctx_b
);
1532 bdrv_try_set_aio_context(bs
, qemu_get_aio_context(), &error_abort
);
1533 aio_context_release(ctx_b
);
1534 bdrv_drained_end(bs
);
1542 typedef struct TestDropBackingBlockJob
{
1544 bool should_complete
;
1546 BlockDriverState
*detach_also
;
1547 } TestDropBackingBlockJob
;
1549 static int coroutine_fn
test_drop_backing_job_run(Job
*job
, Error
**errp
)
1551 TestDropBackingBlockJob
*s
=
1552 container_of(job
, TestDropBackingBlockJob
, common
.job
);
1554 while (!s
->should_complete
) {
1555 job_sleep_ns(job
, 0);
1561 static void test_drop_backing_job_commit(Job
*job
)
1563 TestDropBackingBlockJob
*s
=
1564 container_of(job
, TestDropBackingBlockJob
, common
.job
);
1566 bdrv_set_backing_hd(blk_bs(s
->common
.blk
), NULL
, &error_abort
);
1567 bdrv_set_backing_hd(s
->detach_also
, NULL
, &error_abort
);
1569 *s
->did_complete
= true;
1572 static const BlockJobDriver test_drop_backing_job_driver
= {
1574 .instance_size
= sizeof(TestDropBackingBlockJob
),
1575 .free
= block_job_free
,
1576 .user_resume
= block_job_user_resume
,
1577 .drain
= block_job_drain
,
1578 .run
= test_drop_backing_job_run
,
1579 .commit
= test_drop_backing_job_commit
,
1584 * Creates a child node with three parent nodes on it, and then runs a
1585 * block job on the final one, parent-node-2.
1587 * The job is then asked to complete before a section where the child
1590 * Ending this section will undrain the child's parents, first
1591 * parent-node-2, then parent-node-1, then parent-node-0 -- the parent
1592 * list is in reverse order of how they were added. Ending the drain
1593 * on parent-node-2 will resume the job, thus completing it and
1594 * scheduling job_exit().
1596 * Ending the drain on parent-node-1 will poll the AioContext, which
1597 * lets job_exit() and thus test_drop_backing_job_commit() run. That
1598 * function first removes the child as parent-node-2's backing file.
1600 * In old (and buggy) implementations, there are two problems with
1602 * (A) bdrv_drain_invoke() polls for every node that leaves the
1603 * drained section. This means that job_exit() is scheduled
1604 * before the child has left the drained section. Its
1605 * quiesce_counter is therefore still 1 when it is removed from
1608 * (B) bdrv_replace_child_noperm() calls drained_end() on the old
1609 * child's parents as many times as the child is quiesced. This
1610 * means it will call drained_end() on parent-node-2 once.
1611 * Because parent-node-2 is no longer quiesced at this point, this
1614 * bdrv_replace_child_noperm() therefore must call drained_end() on
1615 * the parent only if it really is still drained because the child is
1618 * If removing child from parent-node-2 was successful (as it should
1619 * be), test_drop_backing_job_commit() will then also remove the child
1620 * from parent-node-0.
1622 * With an old version of our drain infrastructure ((A) above), that
1623 * resulted in the following flow:
1625 * 1. child attempts to leave its drained section. The call recurses
1628 * 2. parent-node-2 leaves the drained section. Polling in
1629 * bdrv_drain_invoke() will schedule job_exit().
1631 * 3. parent-node-1 leaves the drained section. Polling in
1632 * bdrv_drain_invoke() will run job_exit(), thus disconnecting
1633 * parent-node-0 from the child node.
1635 * 4. bdrv_parent_drained_end() uses a QLIST_FOREACH_SAFE() loop to
1636 * iterate over the parents. Thus, it now accesses the BdrvChild
1637 * object that used to connect parent-node-0 and the child node.
1638 * However, that object no longer exists, so it accesses a dangling
1641 * The solution is to only poll once when running a bdrv_drained_end()
1642 * operation, specifically at the end when all drained_end()
1643 * operations for all involved nodes have been scheduled.
1644 * Note that this also solves (A) above, thus hiding (B).
1646 static void test_blockjob_commit_by_drained_end(void)
1648 BlockDriverState
*bs_child
, *bs_parents
[3];
1649 TestDropBackingBlockJob
*job
;
1650 bool job_has_completed
= false;
1653 bs_child
= bdrv_new_open_driver(&bdrv_test
, "child-node", BDRV_O_RDWR
,
1656 for (i
= 0; i
< 3; i
++) {
1658 snprintf(name
, sizeof(name
), "parent-node-%i", i
);
1659 bs_parents
[i
] = bdrv_new_open_driver(&bdrv_test
, name
, BDRV_O_RDWR
,
1661 bdrv_set_backing_hd(bs_parents
[i
], bs_child
, &error_abort
);
1664 job
= block_job_create("job", &test_drop_backing_job_driver
, NULL
,
1665 bs_parents
[2], 0, BLK_PERM_ALL
, 0, 0, NULL
, NULL
,
1668 job
->detach_also
= bs_parents
[0];
1669 job
->did_complete
= &job_has_completed
;
1671 job_start(&job
->common
.job
);
1673 job
->should_complete
= true;
1674 bdrv_drained_begin(bs_child
);
1675 g_assert(!job_has_completed
);
1676 bdrv_drained_end(bs_child
);
1677 g_assert(job_has_completed
);
1679 bdrv_unref(bs_parents
[0]);
1680 bdrv_unref(bs_parents
[1]);
1681 bdrv_unref(bs_parents
[2]);
1682 bdrv_unref(bs_child
);
1686 typedef struct TestSimpleBlockJob
{
1688 bool should_complete
;
1690 } TestSimpleBlockJob
;
1692 static int coroutine_fn
test_simple_job_run(Job
*job
, Error
**errp
)
1694 TestSimpleBlockJob
*s
= container_of(job
, TestSimpleBlockJob
, common
.job
);
1696 while (!s
->should_complete
) {
1697 job_sleep_ns(job
, 0);
1703 static void test_simple_job_clean(Job
*job
)
1705 TestSimpleBlockJob
*s
= container_of(job
, TestSimpleBlockJob
, common
.job
);
1706 *s
->did_complete
= true;
1709 static const BlockJobDriver test_simple_job_driver
= {
1711 .instance_size
= sizeof(TestSimpleBlockJob
),
1712 .free
= block_job_free
,
1713 .user_resume
= block_job_user_resume
,
1714 .drain
= block_job_drain
,
1715 .run
= test_simple_job_run
,
1716 .clean
= test_simple_job_clean
,
1720 static int drop_intermediate_poll_update_filename(BdrvChild
*child
,
1721 BlockDriverState
*new_base
,
1722 const char *filename
,
1726 * We are free to poll here, which may change the block graph, if
1727 * it is not drained.
1730 /* If the job is not drained: Complete it, schedule job_exit() */
1731 aio_poll(qemu_get_current_aio_context(), false);
1732 /* If the job is not drained: Run job_exit(), finish the job */
1733 aio_poll(qemu_get_current_aio_context(), false);
1739 * Test a poll in the midst of bdrv_drop_intermediate().
1741 * bdrv_drop_intermediate() calls BdrvChildRole.update_filename(),
1742 * which can yield or poll. This may lead to graph changes, unless
1743 * the whole subtree in question is drained.
1745 * We test this on the following graph:
1761 * node-2 --chain--> node-1 --chain--> node-0
1763 * We drop node-1 with bdrv_drop_intermediate(top=node-1, base=node-0).
1765 * This first updates node-2's backing filename by invoking
1766 * drop_intermediate_poll_update_filename(), which polls twice. This
1767 * causes the job to finish, which in turns causes the job-node to be
1770 * bdrv_drop_intermediate() uses a QLIST_FOREACH_SAFE() loop, so it
1771 * already has a pointer to the BdrvChild edge between job-node and
1772 * node-1. When it tries to handle that edge, we probably get a
1773 * segmentation fault because the object no longer exists.
1776 * The solution is for bdrv_drop_intermediate() to drain top's
1777 * subtree. This prevents graph changes from happening just because
1778 * BdrvChildRole.update_filename() yields or polls. Thus, the block
1779 * job is paused during that drained section and must finish before or
1782 * (In addition, bdrv_replace_child() must keep the job paused.)
1784 static void test_drop_intermediate_poll(void)
1786 static BdrvChildRole chain_child_role
;
1787 BlockDriverState
*chain
[3];
1788 TestSimpleBlockJob
*job
;
1789 BlockDriverState
*job_node
;
1790 bool job_has_completed
= false;
1794 chain_child_role
= child_backing
;
1795 chain_child_role
.update_filename
= drop_intermediate_poll_update_filename
;
1797 for (i
= 0; i
< 3; i
++) {
1799 snprintf(name
, 32, "node-%i", i
);
1801 chain
[i
] = bdrv_new_open_driver(&bdrv_test
, name
, 0, &error_abort
);
1804 job_node
= bdrv_new_open_driver(&bdrv_test
, "job-node", BDRV_O_RDWR
,
1806 bdrv_set_backing_hd(job_node
, chain
[1], &error_abort
);
1809 * Establish the chain last, so the chain links are the first
1810 * elements in the BDS.parents lists
1812 for (i
= 0; i
< 3; i
++) {
1814 /* Takes the reference to chain[i - 1] */
1815 chain
[i
]->backing
= bdrv_attach_child(chain
[i
], chain
[i
- 1],
1816 "chain", &chain_child_role
,
1821 job
= block_job_create("job", &test_simple_job_driver
, NULL
, job_node
,
1822 0, BLK_PERM_ALL
, 0, 0, NULL
, NULL
, &error_abort
);
1824 /* The job has a reference now */
1825 bdrv_unref(job_node
);
1827 job
->did_complete
= &job_has_completed
;
1829 job_start(&job
->common
.job
);
1830 job
->should_complete
= true;
1832 g_assert(!job_has_completed
);
1833 ret
= bdrv_drop_intermediate(chain
[1], chain
[0], NULL
);
1835 g_assert(job_has_completed
);
1837 bdrv_unref(chain
[2]);
1841 typedef struct BDRVReplaceTestState
{
1848 bool yield_before_read
;
1850 Coroutine
*drain_co
;
1851 } BDRVReplaceTestState
;
1853 static void bdrv_replace_test_close(BlockDriverState
*bs
)
1858 * If @bs has a backing file:
1859 * Yield if .yield_before_read is true (and wait for drain_begin to
1861 * Forward the read to bs->backing. Set .has_read to true.
1862 * If drain_begin has woken us, wake it in turn.
1865 * Set .has_read to true and return success.
1867 static int coroutine_fn
bdrv_replace_test_co_preadv(BlockDriverState
*bs
,
1873 BDRVReplaceTestState
*s
= bs
->opaque
;
1878 g_assert(!s
->drain_count
);
1880 s
->io_co
= qemu_coroutine_self();
1881 if (s
->yield_before_read
) {
1882 s
->yield_before_read
= false;
1883 qemu_coroutine_yield();
1887 ret
= bdrv_preadv(bs
->backing
, offset
, qiov
);
1890 /* Wake up drain_co if it runs */
1892 aio_co_wake(s
->drain_co
);
1903 * If .drain_count is 0, wake up .io_co if there is one; and set
1905 * Increment .drain_count.
1907 static void coroutine_fn
bdrv_replace_test_co_drain_begin(BlockDriverState
*bs
)
1909 BDRVReplaceTestState
*s
= bs
->opaque
;
1911 if (!s
->drain_count
) {
1912 /* Keep waking io_co up until it is done */
1913 s
->drain_co
= qemu_coroutine_self();
1915 aio_co_wake(s
->io_co
);
1917 qemu_coroutine_yield();
1921 s
->was_drained
= true;
1927 * Reduce .drain_count, set .was_undrained once it reaches 0.
1928 * If .drain_count reaches 0 and the node has a backing file, issue a
1931 static void coroutine_fn
bdrv_replace_test_co_drain_end(BlockDriverState
*bs
)
1933 BDRVReplaceTestState
*s
= bs
->opaque
;
1935 g_assert(s
->drain_count
> 0);
1936 if (!--s
->drain_count
) {
1939 s
->was_undrained
= true;
1943 QEMUIOVector qiov
= QEMU_IOVEC_INIT_BUF(qiov
, &data
, 1);
1945 /* Queue a read request post-drain */
1946 ret
= bdrv_replace_test_co_preadv(bs
, 0, 1, &qiov
, 0);
1952 static BlockDriver bdrv_replace_test
= {
1953 .format_name
= "replace_test",
1954 .instance_size
= sizeof(BDRVReplaceTestState
),
1956 .bdrv_close
= bdrv_replace_test_close
,
1957 .bdrv_co_preadv
= bdrv_replace_test_co_preadv
,
1959 .bdrv_co_drain_begin
= bdrv_replace_test_co_drain_begin
,
1960 .bdrv_co_drain_end
= bdrv_replace_test_co_drain_end
,
1962 .bdrv_child_perm
= bdrv_format_default_perms
,
1965 static void coroutine_fn
test_replace_child_mid_drain_read_co(void *opaque
)
1970 ret
= blk_co_pread(opaque
, 0, 1, &data
, 0);
1975 * We test two things:
1976 * (1) bdrv_replace_child_noperm() must not undrain the parent if both
1977 * children are drained.
1978 * (2) bdrv_replace_child_noperm() must never flush I/O requests to a
1979 * drained child. If the old child is drained, it must flush I/O
1980 * requests after the new one has been attached. If the new child
1981 * is drained, it must flush I/O requests before the old one is
1984 * To do so, we create one parent node and two child nodes; then
1985 * attach one of the children (old_child_bs) to the parent, then
1986 * drain both old_child_bs and new_child_bs according to
1987 * old_drain_count and new_drain_count, respectively, and finally
1988 * we invoke bdrv_replace_node() to replace old_child_bs by
1991 * The test block driver we use here (bdrv_replace_test) has a read
1993 * - For the parent node, can optionally yield, and then forwards the
1994 * read to bdrv_preadv(),
1995 * - For the child node, just returns immediately.
1997 * If the read yields, the drain_begin function will wake it up.
1999 * The drain_end function issues a read on the parent once it is fully
2000 * undrained (which simulates requests starting to come in again).
2002 static void do_test_replace_child_mid_drain(int old_drain_count
,
2003 int new_drain_count
)
2005 BlockBackend
*parent_blk
;
2006 BlockDriverState
*parent_bs
;
2007 BlockDriverState
*old_child_bs
, *new_child_bs
;
2008 BDRVReplaceTestState
*parent_s
;
2009 BDRVReplaceTestState
*old_child_s
, *new_child_s
;
2013 parent_bs
= bdrv_new_open_driver(&bdrv_replace_test
, "parent", 0,
2015 parent_s
= parent_bs
->opaque
;
2017 parent_blk
= blk_new(qemu_get_aio_context(),
2018 BLK_PERM_CONSISTENT_READ
, BLK_PERM_ALL
);
2019 blk_insert_bs(parent_blk
, parent_bs
, &error_abort
);
2021 old_child_bs
= bdrv_new_open_driver(&bdrv_replace_test
, "old-child", 0,
2023 new_child_bs
= bdrv_new_open_driver(&bdrv_replace_test
, "new-child", 0,
2025 old_child_s
= old_child_bs
->opaque
;
2026 new_child_s
= new_child_bs
->opaque
;
2028 /* So that we can read something */
2029 parent_bs
->total_sectors
= 1;
2030 old_child_bs
->total_sectors
= 1;
2031 new_child_bs
->total_sectors
= 1;
2033 bdrv_ref(old_child_bs
);
2034 parent_bs
->backing
= bdrv_attach_child(parent_bs
, old_child_bs
, "child",
2035 &child_backing
, &error_abort
);
2037 for (i
= 0; i
< old_drain_count
; i
++) {
2038 bdrv_drained_begin(old_child_bs
);
2040 for (i
= 0; i
< new_drain_count
; i
++) {
2041 bdrv_drained_begin(new_child_bs
);
2044 if (!old_drain_count
) {
2046 * Start a read operation that will yield, so it will not
2047 * complete before the node is drained.
2049 parent_s
->yield_before_read
= true;
2050 io_co
= qemu_coroutine_create(test_replace_child_mid_drain_read_co
,
2052 qemu_coroutine_enter(io_co
);
2055 /* If we have started a read operation, it should have yielded */
2056 g_assert(!parent_s
->has_read
);
2058 /* Reset drained status so we can see what bdrv_replace_node() does */
2059 parent_s
->was_drained
= false;
2060 parent_s
->was_undrained
= false;
2062 g_assert(parent_bs
->quiesce_counter
== old_drain_count
);
2063 bdrv_replace_node(old_child_bs
, new_child_bs
, &error_abort
);
2064 g_assert(parent_bs
->quiesce_counter
== new_drain_count
);
2066 if (!old_drain_count
&& !new_drain_count
) {
2068 * From undrained to undrained drains and undrains the parent,
2069 * because bdrv_replace_node() contains a drained section for
2072 g_assert(parent_s
->was_drained
&& parent_s
->was_undrained
);
2073 } else if (!old_drain_count
&& new_drain_count
) {
2075 * From undrained to drained should drain the parent and keep
2078 g_assert(parent_s
->was_drained
&& !parent_s
->was_undrained
);
2079 } else if (old_drain_count
&& !new_drain_count
) {
2081 * From drained to undrained should undrain the parent and
2084 g_assert(!parent_s
->was_drained
&& parent_s
->was_undrained
);
2085 } else /* if (old_drain_count && new_drain_count) */ {
2087 * From drained to drained must not undrain the parent at any
2090 g_assert(!parent_s
->was_drained
&& !parent_s
->was_undrained
);
2093 if (!old_drain_count
|| !new_drain_count
) {
2095 * If !old_drain_count, we have started a read request before
2096 * bdrv_replace_node(). If !new_drain_count, the parent must
2097 * have been undrained at some point, and
2098 * bdrv_replace_test_co_drain_end() starts a read request
2101 g_assert(parent_s
->has_read
);
2104 * If the parent was never undrained, there is no way to start
2107 g_assert(!parent_s
->has_read
);
2110 /* A drained child must have not received any request */
2111 g_assert(!(old_drain_count
&& old_child_s
->has_read
));
2112 g_assert(!(new_drain_count
&& new_child_s
->has_read
));
2114 for (i
= 0; i
< new_drain_count
; i
++) {
2115 bdrv_drained_end(new_child_bs
);
2117 for (i
= 0; i
< old_drain_count
; i
++) {
2118 bdrv_drained_end(old_child_bs
);
2122 * By now, bdrv_replace_test_co_drain_end() must have been called
2123 * at some point while the new child was attached to the parent.
2125 g_assert(parent_s
->has_read
);
2126 g_assert(new_child_s
->has_read
);
2128 blk_unref(parent_blk
);
2129 bdrv_unref(parent_bs
);
2130 bdrv_unref(old_child_bs
);
2131 bdrv_unref(new_child_bs
);
2134 static void test_replace_child_mid_drain(void)
2136 int old_drain_count
, new_drain_count
;
2138 for (old_drain_count
= 0; old_drain_count
< 2; old_drain_count
++) {
2139 for (new_drain_count
= 0; new_drain_count
< 2; new_drain_count
++) {
2140 do_test_replace_child_mid_drain(old_drain_count
, new_drain_count
);
2145 int main(int argc
, char **argv
)
2150 qemu_init_main_loop(&error_abort
);
2152 g_test_init(&argc
, &argv
, NULL
);
2153 qemu_event_init(&done_event
, false);
2155 g_test_add_func("/bdrv-drain/driver-cb/drain_all", test_drv_cb_drain_all
);
2156 g_test_add_func("/bdrv-drain/driver-cb/drain", test_drv_cb_drain
);
2157 g_test_add_func("/bdrv-drain/driver-cb/drain_subtree",
2158 test_drv_cb_drain_subtree
);
2160 g_test_add_func("/bdrv-drain/driver-cb/co/drain_all",
2161 test_drv_cb_co_drain_all
);
2162 g_test_add_func("/bdrv-drain/driver-cb/co/drain", test_drv_cb_co_drain
);
2163 g_test_add_func("/bdrv-drain/driver-cb/co/drain_subtree",
2164 test_drv_cb_co_drain_subtree
);
2167 g_test_add_func("/bdrv-drain/quiesce/drain_all", test_quiesce_drain_all
);
2168 g_test_add_func("/bdrv-drain/quiesce/drain", test_quiesce_drain
);
2169 g_test_add_func("/bdrv-drain/quiesce/drain_subtree",
2170 test_quiesce_drain_subtree
);
2172 g_test_add_func("/bdrv-drain/quiesce/co/drain_all",
2173 test_quiesce_co_drain_all
);
2174 g_test_add_func("/bdrv-drain/quiesce/co/drain", test_quiesce_co_drain
);
2175 g_test_add_func("/bdrv-drain/quiesce/co/drain_subtree",
2176 test_quiesce_co_drain_subtree
);
2178 g_test_add_func("/bdrv-drain/nested", test_nested
);
2179 g_test_add_func("/bdrv-drain/multiparent", test_multiparent
);
2181 g_test_add_func("/bdrv-drain/graph-change/drain_subtree",
2182 test_graph_change_drain_subtree
);
2183 g_test_add_func("/bdrv-drain/graph-change/drain_all",
2184 test_graph_change_drain_all
);
2186 g_test_add_func("/bdrv-drain/iothread/drain_all", test_iothread_drain_all
);
2187 g_test_add_func("/bdrv-drain/iothread/drain", test_iothread_drain
);
2188 g_test_add_func("/bdrv-drain/iothread/drain_subtree",
2189 test_iothread_drain_subtree
);
2191 g_test_add_func("/bdrv-drain/blockjob/drain_all", test_blockjob_drain_all
);
2192 g_test_add_func("/bdrv-drain/blockjob/drain", test_blockjob_drain
);
2193 g_test_add_func("/bdrv-drain/blockjob/drain_subtree",
2194 test_blockjob_drain_subtree
);
2196 g_test_add_func("/bdrv-drain/blockjob/error/drain_all",
2197 test_blockjob_error_drain_all
);
2198 g_test_add_func("/bdrv-drain/blockjob/error/drain",
2199 test_blockjob_error_drain
);
2200 g_test_add_func("/bdrv-drain/blockjob/error/drain_subtree",
2201 test_blockjob_error_drain_subtree
);
2203 g_test_add_func("/bdrv-drain/blockjob/iothread/drain_all",
2204 test_blockjob_iothread_drain_all
);
2205 g_test_add_func("/bdrv-drain/blockjob/iothread/drain",
2206 test_blockjob_iothread_drain
);
2207 g_test_add_func("/bdrv-drain/blockjob/iothread/drain_subtree",
2208 test_blockjob_iothread_drain_subtree
);
2210 g_test_add_func("/bdrv-drain/blockjob/iothread/error/drain_all",
2211 test_blockjob_iothread_error_drain_all
);
2212 g_test_add_func("/bdrv-drain/blockjob/iothread/error/drain",
2213 test_blockjob_iothread_error_drain
);
2214 g_test_add_func("/bdrv-drain/blockjob/iothread/error/drain_subtree",
2215 test_blockjob_iothread_error_drain_subtree
);
2217 g_test_add_func("/bdrv-drain/deletion/drain", test_delete_by_drain
);
2218 g_test_add_func("/bdrv-drain/detach/drain_all", test_detach_by_drain_all
);
2219 g_test_add_func("/bdrv-drain/detach/drain", test_detach_by_drain
);
2220 g_test_add_func("/bdrv-drain/detach/drain_subtree", test_detach_by_drain_subtree
);
2221 g_test_add_func("/bdrv-drain/detach/parent_cb", test_detach_by_parent_cb
);
2222 g_test_add_func("/bdrv-drain/detach/driver_cb", test_detach_by_driver_cb
);
2224 g_test_add_func("/bdrv-drain/attach/drain", test_append_to_drained
);
2226 g_test_add_func("/bdrv-drain/set_aio_context", test_set_aio_context
);
2228 g_test_add_func("/bdrv-drain/blockjob/commit_by_drained_end",
2229 test_blockjob_commit_by_drained_end
);
2231 g_test_add_func("/bdrv-drain/bdrv_drop_intermediate/poll",
2232 test_drop_intermediate_poll
);
2234 g_test_add_func("/bdrv-drain/replace_child/mid-drain",
2235 test_replace_child_mid_drain
);
2238 qemu_event_destroy(&done_event
);