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 BlockDriver bdrv_test
= {
104 .format_name
= "test",
105 .instance_size
= sizeof(BDRVTestState
),
107 .bdrv_close
= bdrv_test_close
,
108 .bdrv_co_preadv
= bdrv_test_co_preadv
,
110 .bdrv_co_drain_begin
= bdrv_test_co_drain_begin
,
111 .bdrv_co_drain_end
= bdrv_test_co_drain_end
,
113 .bdrv_child_perm
= bdrv_test_child_perm
,
116 static void aio_ret_cb(void *opaque
, int ret
)
118 int *aio_ret
= opaque
;
122 typedef struct CallInCoroutineData
{
125 } CallInCoroutineData
;
127 static coroutine_fn
void call_in_coroutine_entry(void *opaque
)
129 CallInCoroutineData
*data
= opaque
;
135 static void call_in_coroutine(void (*entry
)(void))
138 CallInCoroutineData data
= {
143 co
= qemu_coroutine_create(call_in_coroutine_entry
, &data
);
144 qemu_coroutine_enter(co
);
146 aio_poll(qemu_get_aio_context(), true);
157 static void do_drain_begin(enum drain_type drain_type
, BlockDriverState
*bs
)
159 switch (drain_type
) {
160 case BDRV_DRAIN_ALL
: bdrv_drain_all_begin(); break;
161 case BDRV_DRAIN
: bdrv_drained_begin(bs
); break;
162 case BDRV_SUBTREE_DRAIN
: bdrv_subtree_drained_begin(bs
); break;
163 default: g_assert_not_reached();
167 static void do_drain_end(enum drain_type drain_type
, BlockDriverState
*bs
)
169 switch (drain_type
) {
170 case BDRV_DRAIN_ALL
: bdrv_drain_all_end(); break;
171 case BDRV_DRAIN
: bdrv_drained_end(bs
); break;
172 case BDRV_SUBTREE_DRAIN
: bdrv_subtree_drained_end(bs
); break;
173 default: g_assert_not_reached();
177 static void do_drain_begin_unlocked(enum drain_type drain_type
, BlockDriverState
*bs
)
179 if (drain_type
!= BDRV_DRAIN_ALL
) {
180 aio_context_acquire(bdrv_get_aio_context(bs
));
182 do_drain_begin(drain_type
, bs
);
183 if (drain_type
!= BDRV_DRAIN_ALL
) {
184 aio_context_release(bdrv_get_aio_context(bs
));
188 static void do_drain_end_unlocked(enum drain_type drain_type
, BlockDriverState
*bs
)
190 if (drain_type
!= BDRV_DRAIN_ALL
) {
191 aio_context_acquire(bdrv_get_aio_context(bs
));
193 do_drain_end(drain_type
, bs
);
194 if (drain_type
!= BDRV_DRAIN_ALL
) {
195 aio_context_release(bdrv_get_aio_context(bs
));
199 static void test_drv_cb_common(enum drain_type drain_type
, bool recursive
)
202 BlockDriverState
*bs
, *backing
;
203 BDRVTestState
*s
, *backing_s
;
207 QEMUIOVector qiov
= QEMU_IOVEC_INIT_BUF(qiov
, NULL
, 0);
209 blk
= blk_new(qemu_get_aio_context(), BLK_PERM_ALL
, BLK_PERM_ALL
);
210 bs
= bdrv_new_open_driver(&bdrv_test
, "test-node", BDRV_O_RDWR
,
213 blk_insert_bs(blk
, bs
, &error_abort
);
215 backing
= bdrv_new_open_driver(&bdrv_test
, "backing", 0, &error_abort
);
216 backing_s
= backing
->opaque
;
217 bdrv_set_backing_hd(bs
, backing
, &error_abort
);
219 /* Simple bdrv_drain_all_begin/end pair, check that CBs are called */
220 g_assert_cmpint(s
->drain_count
, ==, 0);
221 g_assert_cmpint(backing_s
->drain_count
, ==, 0);
223 do_drain_begin(drain_type
, bs
);
225 g_assert_cmpint(s
->drain_count
, ==, 1);
226 g_assert_cmpint(backing_s
->drain_count
, ==, !!recursive
);
228 do_drain_end(drain_type
, bs
);
230 g_assert_cmpint(s
->drain_count
, ==, 0);
231 g_assert_cmpint(backing_s
->drain_count
, ==, 0);
233 /* Now do the same while a request is pending */
234 aio_ret
= -EINPROGRESS
;
235 acb
= blk_aio_preadv(blk
, 0, &qiov
, 0, aio_ret_cb
, &aio_ret
);
236 g_assert(acb
!= NULL
);
237 g_assert_cmpint(aio_ret
, ==, -EINPROGRESS
);
239 g_assert_cmpint(s
->drain_count
, ==, 0);
240 g_assert_cmpint(backing_s
->drain_count
, ==, 0);
242 do_drain_begin(drain_type
, bs
);
244 g_assert_cmpint(aio_ret
, ==, 0);
245 g_assert_cmpint(s
->drain_count
, ==, 1);
246 g_assert_cmpint(backing_s
->drain_count
, ==, !!recursive
);
248 do_drain_end(drain_type
, bs
);
250 g_assert_cmpint(s
->drain_count
, ==, 0);
251 g_assert_cmpint(backing_s
->drain_count
, ==, 0);
258 static void test_drv_cb_drain_all(void)
260 test_drv_cb_common(BDRV_DRAIN_ALL
, true);
263 static void test_drv_cb_drain(void)
265 test_drv_cb_common(BDRV_DRAIN
, false);
268 static void test_drv_cb_drain_subtree(void)
270 test_drv_cb_common(BDRV_SUBTREE_DRAIN
, true);
273 static void test_drv_cb_co_drain_all(void)
275 call_in_coroutine(test_drv_cb_drain_all
);
278 static void test_drv_cb_co_drain(void)
280 call_in_coroutine(test_drv_cb_drain
);
283 static void test_drv_cb_co_drain_subtree(void)
285 call_in_coroutine(test_drv_cb_drain_subtree
);
288 static void test_quiesce_common(enum drain_type drain_type
, bool recursive
)
291 BlockDriverState
*bs
, *backing
;
293 blk
= blk_new(qemu_get_aio_context(), BLK_PERM_ALL
, BLK_PERM_ALL
);
294 bs
= bdrv_new_open_driver(&bdrv_test
, "test-node", BDRV_O_RDWR
,
296 blk_insert_bs(blk
, bs
, &error_abort
);
298 backing
= bdrv_new_open_driver(&bdrv_test
, "backing", 0, &error_abort
);
299 bdrv_set_backing_hd(bs
, backing
, &error_abort
);
301 g_assert_cmpint(bs
->quiesce_counter
, ==, 0);
302 g_assert_cmpint(backing
->quiesce_counter
, ==, 0);
304 do_drain_begin(drain_type
, bs
);
306 g_assert_cmpint(bs
->quiesce_counter
, ==, 1);
307 g_assert_cmpint(backing
->quiesce_counter
, ==, !!recursive
);
309 do_drain_end(drain_type
, bs
);
311 g_assert_cmpint(bs
->quiesce_counter
, ==, 0);
312 g_assert_cmpint(backing
->quiesce_counter
, ==, 0);
319 static void test_quiesce_drain_all(void)
321 test_quiesce_common(BDRV_DRAIN_ALL
, true);
324 static void test_quiesce_drain(void)
326 test_quiesce_common(BDRV_DRAIN
, false);
329 static void test_quiesce_drain_subtree(void)
331 test_quiesce_common(BDRV_SUBTREE_DRAIN
, true);
334 static void test_quiesce_co_drain_all(void)
336 call_in_coroutine(test_quiesce_drain_all
);
339 static void test_quiesce_co_drain(void)
341 call_in_coroutine(test_quiesce_drain
);
344 static void test_quiesce_co_drain_subtree(void)
346 call_in_coroutine(test_quiesce_drain_subtree
);
349 static void test_nested(void)
352 BlockDriverState
*bs
, *backing
;
353 BDRVTestState
*s
, *backing_s
;
354 enum drain_type outer
, inner
;
356 blk
= blk_new(qemu_get_aio_context(), BLK_PERM_ALL
, BLK_PERM_ALL
);
357 bs
= bdrv_new_open_driver(&bdrv_test
, "test-node", BDRV_O_RDWR
,
360 blk_insert_bs(blk
, bs
, &error_abort
);
362 backing
= bdrv_new_open_driver(&bdrv_test
, "backing", 0, &error_abort
);
363 backing_s
= backing
->opaque
;
364 bdrv_set_backing_hd(bs
, backing
, &error_abort
);
366 for (outer
= 0; outer
< DRAIN_TYPE_MAX
; outer
++) {
367 for (inner
= 0; inner
< DRAIN_TYPE_MAX
; inner
++) {
368 int backing_quiesce
= (outer
!= BDRV_DRAIN
) +
369 (inner
!= BDRV_DRAIN
);
371 g_assert_cmpint(bs
->quiesce_counter
, ==, 0);
372 g_assert_cmpint(backing
->quiesce_counter
, ==, 0);
373 g_assert_cmpint(s
->drain_count
, ==, 0);
374 g_assert_cmpint(backing_s
->drain_count
, ==, 0);
376 do_drain_begin(outer
, bs
);
377 do_drain_begin(inner
, bs
);
379 g_assert_cmpint(bs
->quiesce_counter
, ==, 2);
380 g_assert_cmpint(backing
->quiesce_counter
, ==, backing_quiesce
);
381 g_assert_cmpint(s
->drain_count
, ==, 2);
382 g_assert_cmpint(backing_s
->drain_count
, ==, backing_quiesce
);
384 do_drain_end(inner
, bs
);
385 do_drain_end(outer
, bs
);
387 g_assert_cmpint(bs
->quiesce_counter
, ==, 0);
388 g_assert_cmpint(backing
->quiesce_counter
, ==, 0);
389 g_assert_cmpint(s
->drain_count
, ==, 0);
390 g_assert_cmpint(backing_s
->drain_count
, ==, 0);
399 static void test_multiparent(void)
401 BlockBackend
*blk_a
, *blk_b
;
402 BlockDriverState
*bs_a
, *bs_b
, *backing
;
403 BDRVTestState
*a_s
, *b_s
, *backing_s
;
405 blk_a
= blk_new(qemu_get_aio_context(), BLK_PERM_ALL
, BLK_PERM_ALL
);
406 bs_a
= bdrv_new_open_driver(&bdrv_test
, "test-node-a", BDRV_O_RDWR
,
409 blk_insert_bs(blk_a
, bs_a
, &error_abort
);
411 blk_b
= blk_new(qemu_get_aio_context(), BLK_PERM_ALL
, BLK_PERM_ALL
);
412 bs_b
= bdrv_new_open_driver(&bdrv_test
, "test-node-b", BDRV_O_RDWR
,
415 blk_insert_bs(blk_b
, bs_b
, &error_abort
);
417 backing
= bdrv_new_open_driver(&bdrv_test
, "backing", 0, &error_abort
);
418 backing_s
= backing
->opaque
;
419 bdrv_set_backing_hd(bs_a
, backing
, &error_abort
);
420 bdrv_set_backing_hd(bs_b
, backing
, &error_abort
);
422 g_assert_cmpint(bs_a
->quiesce_counter
, ==, 0);
423 g_assert_cmpint(bs_b
->quiesce_counter
, ==, 0);
424 g_assert_cmpint(backing
->quiesce_counter
, ==, 0);
425 g_assert_cmpint(a_s
->drain_count
, ==, 0);
426 g_assert_cmpint(b_s
->drain_count
, ==, 0);
427 g_assert_cmpint(backing_s
->drain_count
, ==, 0);
429 do_drain_begin(BDRV_SUBTREE_DRAIN
, bs_a
);
431 g_assert_cmpint(bs_a
->quiesce_counter
, ==, 1);
432 g_assert_cmpint(bs_b
->quiesce_counter
, ==, 1);
433 g_assert_cmpint(backing
->quiesce_counter
, ==, 1);
434 g_assert_cmpint(a_s
->drain_count
, ==, 1);
435 g_assert_cmpint(b_s
->drain_count
, ==, 1);
436 g_assert_cmpint(backing_s
->drain_count
, ==, 1);
438 do_drain_begin(BDRV_SUBTREE_DRAIN
, bs_b
);
440 g_assert_cmpint(bs_a
->quiesce_counter
, ==, 2);
441 g_assert_cmpint(bs_b
->quiesce_counter
, ==, 2);
442 g_assert_cmpint(backing
->quiesce_counter
, ==, 2);
443 g_assert_cmpint(a_s
->drain_count
, ==, 2);
444 g_assert_cmpint(b_s
->drain_count
, ==, 2);
445 g_assert_cmpint(backing_s
->drain_count
, ==, 2);
447 do_drain_end(BDRV_SUBTREE_DRAIN
, bs_b
);
449 g_assert_cmpint(bs_a
->quiesce_counter
, ==, 1);
450 g_assert_cmpint(bs_b
->quiesce_counter
, ==, 1);
451 g_assert_cmpint(backing
->quiesce_counter
, ==, 1);
452 g_assert_cmpint(a_s
->drain_count
, ==, 1);
453 g_assert_cmpint(b_s
->drain_count
, ==, 1);
454 g_assert_cmpint(backing_s
->drain_count
, ==, 1);
456 do_drain_end(BDRV_SUBTREE_DRAIN
, bs_a
);
458 g_assert_cmpint(bs_a
->quiesce_counter
, ==, 0);
459 g_assert_cmpint(bs_b
->quiesce_counter
, ==, 0);
460 g_assert_cmpint(backing
->quiesce_counter
, ==, 0);
461 g_assert_cmpint(a_s
->drain_count
, ==, 0);
462 g_assert_cmpint(b_s
->drain_count
, ==, 0);
463 g_assert_cmpint(backing_s
->drain_count
, ==, 0);
472 static void test_graph_change_drain_subtree(void)
474 BlockBackend
*blk_a
, *blk_b
;
475 BlockDriverState
*bs_a
, *bs_b
, *backing
;
476 BDRVTestState
*a_s
, *b_s
, *backing_s
;
478 blk_a
= blk_new(qemu_get_aio_context(), BLK_PERM_ALL
, BLK_PERM_ALL
);
479 bs_a
= bdrv_new_open_driver(&bdrv_test
, "test-node-a", BDRV_O_RDWR
,
482 blk_insert_bs(blk_a
, bs_a
, &error_abort
);
484 blk_b
= blk_new(qemu_get_aio_context(), BLK_PERM_ALL
, BLK_PERM_ALL
);
485 bs_b
= bdrv_new_open_driver(&bdrv_test
, "test-node-b", BDRV_O_RDWR
,
488 blk_insert_bs(blk_b
, bs_b
, &error_abort
);
490 backing
= bdrv_new_open_driver(&bdrv_test
, "backing", 0, &error_abort
);
491 backing_s
= backing
->opaque
;
492 bdrv_set_backing_hd(bs_a
, backing
, &error_abort
);
494 g_assert_cmpint(bs_a
->quiesce_counter
, ==, 0);
495 g_assert_cmpint(bs_b
->quiesce_counter
, ==, 0);
496 g_assert_cmpint(backing
->quiesce_counter
, ==, 0);
497 g_assert_cmpint(a_s
->drain_count
, ==, 0);
498 g_assert_cmpint(b_s
->drain_count
, ==, 0);
499 g_assert_cmpint(backing_s
->drain_count
, ==, 0);
501 do_drain_begin(BDRV_SUBTREE_DRAIN
, bs_a
);
502 do_drain_begin(BDRV_SUBTREE_DRAIN
, bs_a
);
503 do_drain_begin(BDRV_SUBTREE_DRAIN
, bs_a
);
504 do_drain_begin(BDRV_SUBTREE_DRAIN
, bs_b
);
505 do_drain_begin(BDRV_SUBTREE_DRAIN
, bs_b
);
507 bdrv_set_backing_hd(bs_b
, backing
, &error_abort
);
508 g_assert_cmpint(bs_a
->quiesce_counter
, ==, 5);
509 g_assert_cmpint(bs_b
->quiesce_counter
, ==, 5);
510 g_assert_cmpint(backing
->quiesce_counter
, ==, 5);
511 g_assert_cmpint(a_s
->drain_count
, ==, 5);
512 g_assert_cmpint(b_s
->drain_count
, ==, 5);
513 g_assert_cmpint(backing_s
->drain_count
, ==, 5);
515 bdrv_set_backing_hd(bs_b
, NULL
, &error_abort
);
516 g_assert_cmpint(bs_a
->quiesce_counter
, ==, 3);
517 g_assert_cmpint(bs_b
->quiesce_counter
, ==, 2);
518 g_assert_cmpint(backing
->quiesce_counter
, ==, 3);
519 g_assert_cmpint(a_s
->drain_count
, ==, 3);
520 g_assert_cmpint(b_s
->drain_count
, ==, 2);
521 g_assert_cmpint(backing_s
->drain_count
, ==, 3);
523 bdrv_set_backing_hd(bs_b
, backing
, &error_abort
);
524 g_assert_cmpint(bs_a
->quiesce_counter
, ==, 5);
525 g_assert_cmpint(bs_b
->quiesce_counter
, ==, 5);
526 g_assert_cmpint(backing
->quiesce_counter
, ==, 5);
527 g_assert_cmpint(a_s
->drain_count
, ==, 5);
528 g_assert_cmpint(b_s
->drain_count
, ==, 5);
529 g_assert_cmpint(backing_s
->drain_count
, ==, 5);
531 do_drain_end(BDRV_SUBTREE_DRAIN
, bs_b
);
532 do_drain_end(BDRV_SUBTREE_DRAIN
, bs_b
);
533 do_drain_end(BDRV_SUBTREE_DRAIN
, bs_a
);
534 do_drain_end(BDRV_SUBTREE_DRAIN
, bs_a
);
535 do_drain_end(BDRV_SUBTREE_DRAIN
, bs_a
);
537 g_assert_cmpint(bs_a
->quiesce_counter
, ==, 0);
538 g_assert_cmpint(bs_b
->quiesce_counter
, ==, 0);
539 g_assert_cmpint(backing
->quiesce_counter
, ==, 0);
540 g_assert_cmpint(a_s
->drain_count
, ==, 0);
541 g_assert_cmpint(b_s
->drain_count
, ==, 0);
542 g_assert_cmpint(backing_s
->drain_count
, ==, 0);
551 static void test_graph_change_drain_all(void)
553 BlockBackend
*blk_a
, *blk_b
;
554 BlockDriverState
*bs_a
, *bs_b
;
555 BDRVTestState
*a_s
, *b_s
;
557 /* Create node A with a BlockBackend */
558 blk_a
= blk_new(qemu_get_aio_context(), BLK_PERM_ALL
, BLK_PERM_ALL
);
559 bs_a
= bdrv_new_open_driver(&bdrv_test
, "test-node-a", BDRV_O_RDWR
,
562 blk_insert_bs(blk_a
, bs_a
, &error_abort
);
564 g_assert_cmpint(bs_a
->quiesce_counter
, ==, 0);
565 g_assert_cmpint(a_s
->drain_count
, ==, 0);
567 /* Call bdrv_drain_all_begin() */
568 bdrv_drain_all_begin();
570 g_assert_cmpint(bs_a
->quiesce_counter
, ==, 1);
571 g_assert_cmpint(a_s
->drain_count
, ==, 1);
573 /* Create node B with a BlockBackend */
574 blk_b
= blk_new(qemu_get_aio_context(), BLK_PERM_ALL
, BLK_PERM_ALL
);
575 bs_b
= bdrv_new_open_driver(&bdrv_test
, "test-node-b", BDRV_O_RDWR
,
578 blk_insert_bs(blk_b
, bs_b
, &error_abort
);
580 g_assert_cmpint(bs_a
->quiesce_counter
, ==, 1);
581 g_assert_cmpint(bs_b
->quiesce_counter
, ==, 1);
582 g_assert_cmpint(a_s
->drain_count
, ==, 1);
583 g_assert_cmpint(b_s
->drain_count
, ==, 1);
585 /* Unref and finally delete node A */
588 g_assert_cmpint(bs_a
->quiesce_counter
, ==, 1);
589 g_assert_cmpint(bs_b
->quiesce_counter
, ==, 1);
590 g_assert_cmpint(a_s
->drain_count
, ==, 1);
591 g_assert_cmpint(b_s
->drain_count
, ==, 1);
595 g_assert_cmpint(bs_b
->quiesce_counter
, ==, 1);
596 g_assert_cmpint(b_s
->drain_count
, ==, 1);
598 /* End the drained section */
599 bdrv_drain_all_end();
601 g_assert_cmpint(bs_b
->quiesce_counter
, ==, 0);
602 g_assert_cmpint(b_s
->drain_count
, ==, 0);
608 struct test_iothread_data
{
609 BlockDriverState
*bs
;
610 enum drain_type drain_type
;
614 static void test_iothread_drain_entry(void *opaque
)
616 struct test_iothread_data
*data
= opaque
;
618 aio_context_acquire(bdrv_get_aio_context(data
->bs
));
619 do_drain_begin(data
->drain_type
, data
->bs
);
620 g_assert_cmpint(*data
->aio_ret
, ==, 0);
621 do_drain_end(data
->drain_type
, data
->bs
);
622 aio_context_release(bdrv_get_aio_context(data
->bs
));
624 qemu_event_set(&done_event
);
627 static void test_iothread_aio_cb(void *opaque
, int ret
)
629 int *aio_ret
= opaque
;
631 qemu_event_set(&done_event
);
634 static void test_iothread_main_thread_bh(void *opaque
)
636 struct test_iothread_data
*data
= opaque
;
638 /* Test that the AioContext is not yet locked in a random BH that is
639 * executed during drain, otherwise this would deadlock. */
640 aio_context_acquire(bdrv_get_aio_context(data
->bs
));
641 bdrv_flush(data
->bs
);
642 aio_context_release(bdrv_get_aio_context(data
->bs
));
646 * Starts an AIO request on a BDS that runs in the AioContext of iothread 1.
647 * The request involves a BH on iothread 2 before it can complete.
649 * @drain_thread = 0 means that do_drain_begin/end are called from the main
650 * thread, @drain_thread = 1 means that they are called from iothread 1. Drain
651 * for this BDS cannot be called from iothread 2 because only the main thread
652 * may do cross-AioContext polling.
654 static void test_iothread_common(enum drain_type drain_type
, int drain_thread
)
657 BlockDriverState
*bs
;
661 struct test_iothread_data data
;
663 IOThread
*a
= iothread_new();
664 IOThread
*b
= iothread_new();
665 AioContext
*ctx_a
= iothread_get_aio_context(a
);
666 AioContext
*ctx_b
= iothread_get_aio_context(b
);
668 QEMUIOVector qiov
= QEMU_IOVEC_INIT_BUF(qiov
, NULL
, 0);
670 /* bdrv_drain_all() may only be called from the main loop thread */
671 if (drain_type
== BDRV_DRAIN_ALL
&& drain_thread
!= 0) {
675 blk
= blk_new(qemu_get_aio_context(), BLK_PERM_ALL
, BLK_PERM_ALL
);
676 bs
= bdrv_new_open_driver(&bdrv_test
, "test-node", BDRV_O_RDWR
,
679 blk_insert_bs(blk
, bs
, &error_abort
);
681 blk_set_aio_context(blk
, ctx_a
, &error_abort
);
682 aio_context_acquire(ctx_a
);
684 s
->bh_indirection_ctx
= ctx_b
;
686 aio_ret
= -EINPROGRESS
;
687 qemu_event_reset(&done_event
);
689 if (drain_thread
== 0) {
690 acb
= blk_aio_preadv(blk
, 0, &qiov
, 0, test_iothread_aio_cb
, &aio_ret
);
692 acb
= blk_aio_preadv(blk
, 0, &qiov
, 0, aio_ret_cb
, &aio_ret
);
694 g_assert(acb
!= NULL
);
695 g_assert_cmpint(aio_ret
, ==, -EINPROGRESS
);
697 aio_context_release(ctx_a
);
699 data
= (struct test_iothread_data
) {
701 .drain_type
= drain_type
,
705 switch (drain_thread
) {
707 if (drain_type
!= BDRV_DRAIN_ALL
) {
708 aio_context_acquire(ctx_a
);
711 aio_bh_schedule_oneshot(ctx_a
, test_iothread_main_thread_bh
, &data
);
713 /* The request is running on the IOThread a. Draining its block device
714 * will make sure that it has completed as far as the BDS is concerned,
715 * but the drain in this thread can continue immediately after
716 * bdrv_dec_in_flight() and aio_ret might be assigned only slightly
718 do_drain_begin(drain_type
, bs
);
719 g_assert_cmpint(bs
->in_flight
, ==, 0);
721 if (drain_type
!= BDRV_DRAIN_ALL
) {
722 aio_context_release(ctx_a
);
724 qemu_event_wait(&done_event
);
725 if (drain_type
!= BDRV_DRAIN_ALL
) {
726 aio_context_acquire(ctx_a
);
729 g_assert_cmpint(aio_ret
, ==, 0);
730 do_drain_end(drain_type
, bs
);
732 if (drain_type
!= BDRV_DRAIN_ALL
) {
733 aio_context_release(ctx_a
);
737 aio_bh_schedule_oneshot(ctx_a
, test_iothread_drain_entry
, &data
);
738 qemu_event_wait(&done_event
);
741 g_assert_not_reached();
744 aio_context_acquire(ctx_a
);
745 blk_set_aio_context(blk
, qemu_get_aio_context(), &error_abort
);
746 aio_context_release(ctx_a
);
756 static void test_iothread_drain_all(void)
758 test_iothread_common(BDRV_DRAIN_ALL
, 0);
759 test_iothread_common(BDRV_DRAIN_ALL
, 1);
762 static void test_iothread_drain(void)
764 test_iothread_common(BDRV_DRAIN
, 0);
765 test_iothread_common(BDRV_DRAIN
, 1);
768 static void test_iothread_drain_subtree(void)
770 test_iothread_common(BDRV_SUBTREE_DRAIN
, 0);
771 test_iothread_common(BDRV_SUBTREE_DRAIN
, 1);
775 typedef struct TestBlockJob
{
780 bool should_complete
;
783 static int test_job_prepare(Job
*job
)
785 TestBlockJob
*s
= container_of(job
, TestBlockJob
, common
.job
);
787 /* Provoke an AIO_WAIT_WHILE() call to verify there is no deadlock */
788 blk_flush(s
->common
.blk
);
789 return s
->prepare_ret
;
792 static void test_job_commit(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
);
800 static void test_job_abort(Job
*job
)
802 TestBlockJob
*s
= container_of(job
, TestBlockJob
, common
.job
);
804 /* Provoke an AIO_WAIT_WHILE() call to verify there is no deadlock */
805 blk_flush(s
->common
.blk
);
808 static int coroutine_fn
test_job_run(Job
*job
, Error
**errp
)
810 TestBlockJob
*s
= container_of(job
, TestBlockJob
, common
.job
);
812 /* We are running the actual job code past the pause point in
816 job_transition_to_ready(&s
->common
.job
);
817 while (!s
->should_complete
) {
818 /* Avoid job_sleep_ns() because it marks the job as !busy. We want to
819 * emulate some actual activity (probably some I/O) here so that drain
820 * has to wait for this activity to stop. */
821 qemu_co_sleep_ns(QEMU_CLOCK_REALTIME
, 1000000);
823 job_pause_point(&s
->common
.job
);
829 static void test_job_complete(Job
*job
, Error
**errp
)
831 TestBlockJob
*s
= container_of(job
, TestBlockJob
, common
.job
);
832 s
->should_complete
= true;
835 BlockJobDriver test_job_driver
= {
837 .instance_size
= sizeof(TestBlockJob
),
838 .free
= block_job_free
,
839 .user_resume
= block_job_user_resume
,
840 .drain
= block_job_drain
,
842 .complete
= test_job_complete
,
843 .prepare
= test_job_prepare
,
844 .commit
= test_job_commit
,
845 .abort
= test_job_abort
,
849 enum test_job_result
{
852 TEST_JOB_FAIL_PREPARE
,
855 enum test_job_drain_node
{
857 TEST_JOB_DRAIN_SRC_CHILD
,
858 TEST_JOB_DRAIN_SRC_PARENT
,
861 static void test_blockjob_common_drain_node(enum drain_type drain_type
,
863 enum test_job_result result
,
864 enum test_job_drain_node drain_node
)
866 BlockBackend
*blk_src
, *blk_target
;
867 BlockDriverState
*src
, *src_backing
, *src_overlay
, *target
, *drain_bs
;
870 IOThread
*iothread
= NULL
;
874 src
= bdrv_new_open_driver(&bdrv_test
, "source", BDRV_O_RDWR
,
876 src_backing
= bdrv_new_open_driver(&bdrv_test
, "source-backing",
877 BDRV_O_RDWR
, &error_abort
);
878 src_overlay
= bdrv_new_open_driver(&bdrv_test
, "source-overlay",
879 BDRV_O_RDWR
, &error_abort
);
881 bdrv_set_backing_hd(src_overlay
, src
, &error_abort
);
883 bdrv_set_backing_hd(src
, src_backing
, &error_abort
);
884 bdrv_unref(src_backing
);
886 blk_src
= blk_new(qemu_get_aio_context(), BLK_PERM_ALL
, BLK_PERM_ALL
);
887 blk_insert_bs(blk_src
, src_overlay
, &error_abort
);
889 switch (drain_node
) {
890 case TEST_JOB_DRAIN_SRC
:
893 case TEST_JOB_DRAIN_SRC_CHILD
:
894 drain_bs
= src_backing
;
896 case TEST_JOB_DRAIN_SRC_PARENT
:
897 drain_bs
= src_overlay
;
900 g_assert_not_reached();
904 iothread
= iothread_new();
905 ctx
= iothread_get_aio_context(iothread
);
906 blk_set_aio_context(blk_src
, ctx
, &error_abort
);
908 ctx
= qemu_get_aio_context();
911 target
= bdrv_new_open_driver(&bdrv_test
, "target", BDRV_O_RDWR
,
913 blk_target
= blk_new(qemu_get_aio_context(), BLK_PERM_ALL
, BLK_PERM_ALL
);
914 blk_insert_bs(blk_target
, target
, &error_abort
);
915 blk_set_allow_aio_context_change(blk_target
, true);
917 aio_context_acquire(ctx
);
918 tjob
= block_job_create("job0", &test_job_driver
, NULL
, src
,
920 0, 0, NULL
, NULL
, &error_abort
);
922 block_job_add_bdrv(job
, "target", target
, 0, BLK_PERM_ALL
, &error_abort
);
925 case TEST_JOB_SUCCESS
:
927 case TEST_JOB_FAIL_RUN
:
928 tjob
->run_ret
= -EIO
;
930 case TEST_JOB_FAIL_PREPARE
:
931 tjob
->prepare_ret
= -EIO
;
935 job_start(&job
->job
);
936 aio_context_release(ctx
);
939 /* job_co_entry() is run in the I/O thread, wait for the actual job
940 * code to start (we don't want to catch the job in the pause point in
942 while (!tjob
->running
) {
943 aio_poll(qemu_get_aio_context(), false);
947 g_assert_cmpint(job
->job
.pause_count
, ==, 0);
948 g_assert_false(job
->job
.paused
);
949 g_assert_true(tjob
->running
);
950 g_assert_true(job
->job
.busy
); /* We're in qemu_co_sleep_ns() */
952 do_drain_begin_unlocked(drain_type
, drain_bs
);
954 if (drain_type
== BDRV_DRAIN_ALL
) {
955 /* bdrv_drain_all() drains both src and target */
956 g_assert_cmpint(job
->job
.pause_count
, ==, 2);
958 g_assert_cmpint(job
->job
.pause_count
, ==, 1);
960 g_assert_true(job
->job
.paused
);
961 g_assert_false(job
->job
.busy
); /* The job is paused */
963 do_drain_end_unlocked(drain_type
, drain_bs
);
966 /* paused is reset in the I/O thread, wait for it */
967 while (job
->job
.paused
) {
968 aio_poll(qemu_get_aio_context(), false);
972 g_assert_cmpint(job
->job
.pause_count
, ==, 0);
973 g_assert_false(job
->job
.paused
);
974 g_assert_true(job
->job
.busy
); /* We're in qemu_co_sleep_ns() */
976 do_drain_begin_unlocked(drain_type
, target
);
978 if (drain_type
== BDRV_DRAIN_ALL
) {
979 /* bdrv_drain_all() drains both src and target */
980 g_assert_cmpint(job
->job
.pause_count
, ==, 2);
982 g_assert_cmpint(job
->job
.pause_count
, ==, 1);
984 g_assert_true(job
->job
.paused
);
985 g_assert_false(job
->job
.busy
); /* The job is paused */
987 do_drain_end_unlocked(drain_type
, target
);
990 /* paused is reset in the I/O thread, wait for it */
991 while (job
->job
.paused
) {
992 aio_poll(qemu_get_aio_context(), false);
996 g_assert_cmpint(job
->job
.pause_count
, ==, 0);
997 g_assert_false(job
->job
.paused
);
998 g_assert_true(job
->job
.busy
); /* We're in qemu_co_sleep_ns() */
1000 aio_context_acquire(ctx
);
1001 ret
= job_complete_sync(&job
->job
, &error_abort
);
1002 g_assert_cmpint(ret
, ==, (result
== TEST_JOB_SUCCESS
? 0 : -EIO
));
1005 blk_set_aio_context(blk_src
, qemu_get_aio_context(), &error_abort
);
1006 assert(blk_get_aio_context(blk_target
) == qemu_get_aio_context());
1008 aio_context_release(ctx
);
1011 blk_unref(blk_target
);
1012 bdrv_unref(src_overlay
);
1016 iothread_join(iothread
);
1020 static void test_blockjob_common(enum drain_type drain_type
, bool use_iothread
,
1021 enum test_job_result result
)
1023 test_blockjob_common_drain_node(drain_type
, use_iothread
, result
,
1024 TEST_JOB_DRAIN_SRC
);
1025 test_blockjob_common_drain_node(drain_type
, use_iothread
, result
,
1026 TEST_JOB_DRAIN_SRC_CHILD
);
1027 if (drain_type
== BDRV_SUBTREE_DRAIN
) {
1028 test_blockjob_common_drain_node(drain_type
, use_iothread
, result
,
1029 TEST_JOB_DRAIN_SRC_PARENT
);
1033 static void test_blockjob_drain_all(void)
1035 test_blockjob_common(BDRV_DRAIN_ALL
, false, TEST_JOB_SUCCESS
);
1038 static void test_blockjob_drain(void)
1040 test_blockjob_common(BDRV_DRAIN
, false, TEST_JOB_SUCCESS
);
1043 static void test_blockjob_drain_subtree(void)
1045 test_blockjob_common(BDRV_SUBTREE_DRAIN
, false, TEST_JOB_SUCCESS
);
1048 static void test_blockjob_error_drain_all(void)
1050 test_blockjob_common(BDRV_DRAIN_ALL
, false, TEST_JOB_FAIL_RUN
);
1051 test_blockjob_common(BDRV_DRAIN_ALL
, false, TEST_JOB_FAIL_PREPARE
);
1054 static void test_blockjob_error_drain(void)
1056 test_blockjob_common(BDRV_DRAIN
, false, TEST_JOB_FAIL_RUN
);
1057 test_blockjob_common(BDRV_DRAIN
, false, TEST_JOB_FAIL_PREPARE
);
1060 static void test_blockjob_error_drain_subtree(void)
1062 test_blockjob_common(BDRV_SUBTREE_DRAIN
, false, TEST_JOB_FAIL_RUN
);
1063 test_blockjob_common(BDRV_SUBTREE_DRAIN
, false, TEST_JOB_FAIL_PREPARE
);
1066 static void test_blockjob_iothread_drain_all(void)
1068 test_blockjob_common(BDRV_DRAIN_ALL
, true, TEST_JOB_SUCCESS
);
1071 static void test_blockjob_iothread_drain(void)
1073 test_blockjob_common(BDRV_DRAIN
, true, TEST_JOB_SUCCESS
);
1076 static void test_blockjob_iothread_drain_subtree(void)
1078 test_blockjob_common(BDRV_SUBTREE_DRAIN
, true, TEST_JOB_SUCCESS
);
1081 static void test_blockjob_iothread_error_drain_all(void)
1083 test_blockjob_common(BDRV_DRAIN_ALL
, true, TEST_JOB_FAIL_RUN
);
1084 test_blockjob_common(BDRV_DRAIN_ALL
, true, TEST_JOB_FAIL_PREPARE
);
1087 static void test_blockjob_iothread_error_drain(void)
1089 test_blockjob_common(BDRV_DRAIN
, true, TEST_JOB_FAIL_RUN
);
1090 test_blockjob_common(BDRV_DRAIN
, true, TEST_JOB_FAIL_PREPARE
);
1093 static void test_blockjob_iothread_error_drain_subtree(void)
1095 test_blockjob_common(BDRV_SUBTREE_DRAIN
, true, TEST_JOB_FAIL_RUN
);
1096 test_blockjob_common(BDRV_SUBTREE_DRAIN
, true, TEST_JOB_FAIL_PREPARE
);
1100 typedef struct BDRVTestTopState
{
1101 BdrvChild
*wait_child
;
1104 static void bdrv_test_top_close(BlockDriverState
*bs
)
1106 BdrvChild
*c
, *next_c
;
1107 QLIST_FOREACH_SAFE(c
, &bs
->children
, next
, next_c
) {
1108 bdrv_unref_child(bs
, c
);
1112 static int coroutine_fn
bdrv_test_top_co_preadv(BlockDriverState
*bs
,
1113 uint64_t offset
, uint64_t bytes
,
1114 QEMUIOVector
*qiov
, int flags
)
1116 BDRVTestTopState
*tts
= bs
->opaque
;
1117 return bdrv_co_preadv(tts
->wait_child
, offset
, bytes
, qiov
, flags
);
1120 static BlockDriver bdrv_test_top_driver
= {
1121 .format_name
= "test_top_driver",
1122 .instance_size
= sizeof(BDRVTestTopState
),
1124 .bdrv_close
= bdrv_test_top_close
,
1125 .bdrv_co_preadv
= bdrv_test_top_co_preadv
,
1127 .bdrv_child_perm
= bdrv_format_default_perms
,
1130 typedef struct TestCoDeleteByDrainData
{
1132 bool detach_instead_of_delete
;
1134 } TestCoDeleteByDrainData
;
1136 static void coroutine_fn
test_co_delete_by_drain(void *opaque
)
1138 TestCoDeleteByDrainData
*dbdd
= opaque
;
1139 BlockBackend
*blk
= dbdd
->blk
;
1140 BlockDriverState
*bs
= blk_bs(blk
);
1141 BDRVTestTopState
*tts
= bs
->opaque
;
1142 void *buffer
= g_malloc(65536);
1143 QEMUIOVector qiov
= QEMU_IOVEC_INIT_BUF(qiov
, buffer
, 65536);
1145 /* Pretend some internal write operation from parent to child.
1146 * Important: We have to read from the child, not from the parent!
1147 * Draining works by first propagating it all up the tree to the
1148 * root and then waiting for drainage from root to the leaves
1149 * (protocol nodes). If we have a request waiting on the root,
1150 * everything will be drained before we go back down the tree, but
1151 * we do not want that. We want to be in the middle of draining
1152 * when this following requests returns. */
1153 bdrv_co_preadv(tts
->wait_child
, 0, 65536, &qiov
, 0);
1155 g_assert_cmpint(bs
->refcnt
, ==, 1);
1157 if (!dbdd
->detach_instead_of_delete
) {
1160 BdrvChild
*c
, *next_c
;
1161 QLIST_FOREACH_SAFE(c
, &bs
->children
, next
, next_c
) {
1162 bdrv_unref_child(bs
, c
);
1171 * Test what happens when some BDS has some children, you drain one of
1172 * them and this results in the BDS being deleted.
1174 * If @detach_instead_of_delete is set, the BDS is not going to be
1175 * deleted but will only detach all of its children.
1177 static void do_test_delete_by_drain(bool detach_instead_of_delete
,
1178 enum drain_type drain_type
)
1181 BlockDriverState
*bs
, *child_bs
, *null_bs
;
1182 BDRVTestTopState
*tts
;
1183 TestCoDeleteByDrainData dbdd
;
1186 bs
= bdrv_new_open_driver(&bdrv_test_top_driver
, "top", BDRV_O_RDWR
,
1188 bs
->total_sectors
= 65536 >> BDRV_SECTOR_BITS
;
1191 null_bs
= bdrv_open("null-co://", NULL
, NULL
, BDRV_O_RDWR
| BDRV_O_PROTOCOL
,
1193 bdrv_attach_child(bs
, null_bs
, "null-child", &child_file
, &error_abort
);
1195 /* This child will be the one to pass to requests through to, and
1196 * it will stall until a drain occurs */
1197 child_bs
= bdrv_new_open_driver(&bdrv_test
, "child", BDRV_O_RDWR
,
1199 child_bs
->total_sectors
= 65536 >> BDRV_SECTOR_BITS
;
1200 /* Takes our reference to child_bs */
1201 tts
->wait_child
= bdrv_attach_child(bs
, child_bs
, "wait-child", &child_file
,
1204 /* This child is just there to be deleted
1205 * (for detach_instead_of_delete == true) */
1206 null_bs
= bdrv_open("null-co://", NULL
, NULL
, BDRV_O_RDWR
| BDRV_O_PROTOCOL
,
1208 bdrv_attach_child(bs
, null_bs
, "null-child", &child_file
, &error_abort
);
1210 blk
= blk_new(qemu_get_aio_context(), BLK_PERM_ALL
, BLK_PERM_ALL
);
1211 blk_insert_bs(blk
, bs
, &error_abort
);
1213 /* Referenced by blk now */
1216 g_assert_cmpint(bs
->refcnt
, ==, 1);
1217 g_assert_cmpint(child_bs
->refcnt
, ==, 1);
1218 g_assert_cmpint(null_bs
->refcnt
, ==, 1);
1221 dbdd
= (TestCoDeleteByDrainData
){
1223 .detach_instead_of_delete
= detach_instead_of_delete
,
1226 co
= qemu_coroutine_create(test_co_delete_by_drain
, &dbdd
);
1227 qemu_coroutine_enter(co
);
1229 /* Drain the child while the read operation is still pending.
1230 * This should result in the operation finishing and
1231 * test_co_delete_by_drain() resuming. Thus, @bs will be deleted
1232 * and the coroutine will exit while this drain operation is still
1234 switch (drain_type
) {
1237 bdrv_drain(child_bs
);
1238 bdrv_unref(child_bs
);
1240 case BDRV_SUBTREE_DRAIN
:
1241 /* Would have to ref/unref bs here for !detach_instead_of_delete, but
1242 * then the whole test becomes pointless because the graph changes
1243 * don't occur during the drain any more. */
1244 assert(detach_instead_of_delete
);
1245 bdrv_subtree_drained_begin(bs
);
1246 bdrv_subtree_drained_end(bs
);
1248 case BDRV_DRAIN_ALL
:
1249 bdrv_drain_all_begin();
1250 bdrv_drain_all_end();
1253 g_assert_not_reached();
1256 while (!dbdd
.done
) {
1257 aio_poll(qemu_get_aio_context(), true);
1260 if (detach_instead_of_delete
) {
1261 /* Here, the reference has not passed over to the coroutine,
1262 * so we have to delete the BB ourselves */
1267 static void test_delete_by_drain(void)
1269 do_test_delete_by_drain(false, BDRV_DRAIN
);
1272 static void test_detach_by_drain_all(void)
1274 do_test_delete_by_drain(true, BDRV_DRAIN_ALL
);
1277 static void test_detach_by_drain(void)
1279 do_test_delete_by_drain(true, BDRV_DRAIN
);
1282 static void test_detach_by_drain_subtree(void)
1284 do_test_delete_by_drain(true, BDRV_SUBTREE_DRAIN
);
1288 struct detach_by_parent_data
{
1289 BlockDriverState
*parent_b
;
1291 BlockDriverState
*c
;
1295 static struct detach_by_parent_data detach_by_parent_data
;
1297 static void detach_indirect_bh(void *opaque
)
1299 struct detach_by_parent_data
*data
= opaque
;
1301 bdrv_unref_child(data
->parent_b
, data
->child_b
);
1304 data
->child_c
= bdrv_attach_child(data
->parent_b
, data
->c
, "PB-C",
1305 &child_file
, &error_abort
);
1308 static void detach_by_parent_aio_cb(void *opaque
, int ret
)
1310 struct detach_by_parent_data
*data
= &detach_by_parent_data
;
1312 g_assert_cmpint(ret
, ==, 0);
1313 if (data
->by_parent_cb
) {
1314 detach_indirect_bh(data
);
1318 static void detach_by_driver_cb_drained_begin(BdrvChild
*child
)
1320 aio_bh_schedule_oneshot(qemu_get_current_aio_context(),
1321 detach_indirect_bh
, &detach_by_parent_data
);
1322 child_file
.drained_begin(child
);
1325 static BdrvChildRole detach_by_driver_cb_role
;
1334 * by_parent_cb == true: Test that parent callbacks don't poll
1336 * PA has a pending write request whose callback changes the child nodes of
1337 * PB: It removes B and adds C instead. The subtree of PB is drained, which
1338 * will indirectly drain the write request, too.
1340 * by_parent_cb == false: Test that bdrv_drain_invoke() doesn't poll
1342 * PA's BdrvChildRole has a .drained_begin callback that schedules a BH
1343 * that does the same graph change. If bdrv_drain_invoke() calls it, the
1344 * state is messed up, but if it is only polled in the single
1345 * BDRV_POLL_WHILE() at the end of the drain, this should work fine.
1347 static void test_detach_indirect(bool by_parent_cb
)
1350 BlockDriverState
*parent_a
, *parent_b
, *a
, *b
, *c
;
1351 BdrvChild
*child_a
, *child_b
;
1354 QEMUIOVector qiov
= QEMU_IOVEC_INIT_BUF(qiov
, NULL
, 0);
1356 if (!by_parent_cb
) {
1357 detach_by_driver_cb_role
= child_file
;
1358 detach_by_driver_cb_role
.drained_begin
=
1359 detach_by_driver_cb_drained_begin
;
1362 /* Create all involved nodes */
1363 parent_a
= bdrv_new_open_driver(&bdrv_test
, "parent-a", BDRV_O_RDWR
,
1365 parent_b
= bdrv_new_open_driver(&bdrv_test
, "parent-b", 0,
1368 a
= bdrv_new_open_driver(&bdrv_test
, "a", BDRV_O_RDWR
, &error_abort
);
1369 b
= bdrv_new_open_driver(&bdrv_test
, "b", BDRV_O_RDWR
, &error_abort
);
1370 c
= bdrv_new_open_driver(&bdrv_test
, "c", BDRV_O_RDWR
, &error_abort
);
1372 /* blk is a BB for parent-a */
1373 blk
= blk_new(qemu_get_aio_context(), BLK_PERM_ALL
, BLK_PERM_ALL
);
1374 blk_insert_bs(blk
, parent_a
, &error_abort
);
1375 bdrv_unref(parent_a
);
1377 /* If we want to get bdrv_drain_invoke() to call aio_poll(), the driver
1378 * callback must not return immediately. */
1379 if (!by_parent_cb
) {
1380 BDRVTestState
*s
= parent_a
->opaque
;
1381 s
->sleep_in_drain_begin
= true;
1384 /* Set child relationships */
1387 child_b
= bdrv_attach_child(parent_b
, b
, "PB-B", &child_file
, &error_abort
);
1388 child_a
= bdrv_attach_child(parent_b
, a
, "PB-A", &child_backing
, &error_abort
);
1391 bdrv_attach_child(parent_a
, a
, "PA-A",
1392 by_parent_cb
? &child_file
: &detach_by_driver_cb_role
,
1395 g_assert_cmpint(parent_a
->refcnt
, ==, 1);
1396 g_assert_cmpint(parent_b
->refcnt
, ==, 1);
1397 g_assert_cmpint(a
->refcnt
, ==, 3);
1398 g_assert_cmpint(b
->refcnt
, ==, 2);
1399 g_assert_cmpint(c
->refcnt
, ==, 1);
1401 g_assert(QLIST_FIRST(&parent_b
->children
) == child_a
);
1402 g_assert(QLIST_NEXT(child_a
, next
) == child_b
);
1403 g_assert(QLIST_NEXT(child_b
, next
) == NULL
);
1405 /* Start the evil write request */
1406 detach_by_parent_data
= (struct detach_by_parent_data
) {
1407 .parent_b
= parent_b
,
1410 .by_parent_cb
= by_parent_cb
,
1412 acb
= blk_aio_preadv(blk
, 0, &qiov
, 0, detach_by_parent_aio_cb
, NULL
);
1413 g_assert(acb
!= NULL
);
1415 /* Drain and check the expected result */
1416 bdrv_subtree_drained_begin(parent_b
);
1418 g_assert(detach_by_parent_data
.child_c
!= NULL
);
1420 g_assert_cmpint(parent_a
->refcnt
, ==, 1);
1421 g_assert_cmpint(parent_b
->refcnt
, ==, 1);
1422 g_assert_cmpint(a
->refcnt
, ==, 3);
1423 g_assert_cmpint(b
->refcnt
, ==, 1);
1424 g_assert_cmpint(c
->refcnt
, ==, 2);
1426 g_assert(QLIST_FIRST(&parent_b
->children
) == detach_by_parent_data
.child_c
);
1427 g_assert(QLIST_NEXT(detach_by_parent_data
.child_c
, next
) == child_a
);
1428 g_assert(QLIST_NEXT(child_a
, next
) == NULL
);
1430 g_assert_cmpint(parent_a
->quiesce_counter
, ==, 1);
1431 g_assert_cmpint(parent_b
->quiesce_counter
, ==, 1);
1432 g_assert_cmpint(a
->quiesce_counter
, ==, 1);
1433 g_assert_cmpint(b
->quiesce_counter
, ==, 0);
1434 g_assert_cmpint(c
->quiesce_counter
, ==, 1);
1436 bdrv_subtree_drained_end(parent_b
);
1438 bdrv_unref(parent_b
);
1441 g_assert_cmpint(a
->refcnt
, ==, 1);
1442 g_assert_cmpint(b
->refcnt
, ==, 1);
1443 g_assert_cmpint(c
->refcnt
, ==, 1);
1449 static void test_detach_by_parent_cb(void)
1451 test_detach_indirect(true);
1454 static void test_detach_by_driver_cb(void)
1456 test_detach_indirect(false);
1459 static void test_append_to_drained(void)
1462 BlockDriverState
*base
, *overlay
;
1463 BDRVTestState
*base_s
, *overlay_s
;
1465 blk
= blk_new(qemu_get_aio_context(), BLK_PERM_ALL
, BLK_PERM_ALL
);
1466 base
= bdrv_new_open_driver(&bdrv_test
, "base", BDRV_O_RDWR
, &error_abort
);
1467 base_s
= base
->opaque
;
1468 blk_insert_bs(blk
, base
, &error_abort
);
1470 overlay
= bdrv_new_open_driver(&bdrv_test
, "overlay", BDRV_O_RDWR
,
1472 overlay_s
= overlay
->opaque
;
1474 do_drain_begin(BDRV_DRAIN
, base
);
1475 g_assert_cmpint(base
->quiesce_counter
, ==, 1);
1476 g_assert_cmpint(base_s
->drain_count
, ==, 1);
1477 g_assert_cmpint(base
->in_flight
, ==, 0);
1479 /* Takes ownership of overlay, so we don't have to unref it later */
1480 bdrv_append(overlay
, base
, &error_abort
);
1481 g_assert_cmpint(base
->in_flight
, ==, 0);
1482 g_assert_cmpint(overlay
->in_flight
, ==, 0);
1484 g_assert_cmpint(base
->quiesce_counter
, ==, 1);
1485 g_assert_cmpint(base_s
->drain_count
, ==, 1);
1486 g_assert_cmpint(overlay
->quiesce_counter
, ==, 1);
1487 g_assert_cmpint(overlay_s
->drain_count
, ==, 1);
1489 do_drain_end(BDRV_DRAIN
, base
);
1491 g_assert_cmpint(base
->quiesce_counter
, ==, 0);
1492 g_assert_cmpint(base_s
->drain_count
, ==, 0);
1493 g_assert_cmpint(overlay
->quiesce_counter
, ==, 0);
1494 g_assert_cmpint(overlay_s
->drain_count
, ==, 0);
1500 static void test_set_aio_context(void)
1502 BlockDriverState
*bs
;
1503 IOThread
*a
= iothread_new();
1504 IOThread
*b
= iothread_new();
1505 AioContext
*ctx_a
= iothread_get_aio_context(a
);
1506 AioContext
*ctx_b
= iothread_get_aio_context(b
);
1508 bs
= bdrv_new_open_driver(&bdrv_test
, "test-node", BDRV_O_RDWR
,
1511 bdrv_drained_begin(bs
);
1512 bdrv_try_set_aio_context(bs
, ctx_a
, &error_abort
);
1514 aio_context_acquire(ctx_a
);
1515 bdrv_drained_end(bs
);
1517 bdrv_drained_begin(bs
);
1518 bdrv_try_set_aio_context(bs
, ctx_b
, &error_abort
);
1519 aio_context_release(ctx_a
);
1520 aio_context_acquire(ctx_b
);
1521 bdrv_try_set_aio_context(bs
, qemu_get_aio_context(), &error_abort
);
1522 aio_context_release(ctx_b
);
1523 bdrv_drained_end(bs
);
1530 int main(int argc
, char **argv
)
1535 qemu_init_main_loop(&error_abort
);
1537 g_test_init(&argc
, &argv
, NULL
);
1538 qemu_event_init(&done_event
, false);
1540 g_test_add_func("/bdrv-drain/driver-cb/drain_all", test_drv_cb_drain_all
);
1541 g_test_add_func("/bdrv-drain/driver-cb/drain", test_drv_cb_drain
);
1542 g_test_add_func("/bdrv-drain/driver-cb/drain_subtree",
1543 test_drv_cb_drain_subtree
);
1545 g_test_add_func("/bdrv-drain/driver-cb/co/drain_all",
1546 test_drv_cb_co_drain_all
);
1547 g_test_add_func("/bdrv-drain/driver-cb/co/drain", test_drv_cb_co_drain
);
1548 g_test_add_func("/bdrv-drain/driver-cb/co/drain_subtree",
1549 test_drv_cb_co_drain_subtree
);
1552 g_test_add_func("/bdrv-drain/quiesce/drain_all", test_quiesce_drain_all
);
1553 g_test_add_func("/bdrv-drain/quiesce/drain", test_quiesce_drain
);
1554 g_test_add_func("/bdrv-drain/quiesce/drain_subtree",
1555 test_quiesce_drain_subtree
);
1557 g_test_add_func("/bdrv-drain/quiesce/co/drain_all",
1558 test_quiesce_co_drain_all
);
1559 g_test_add_func("/bdrv-drain/quiesce/co/drain", test_quiesce_co_drain
);
1560 g_test_add_func("/bdrv-drain/quiesce/co/drain_subtree",
1561 test_quiesce_co_drain_subtree
);
1563 g_test_add_func("/bdrv-drain/nested", test_nested
);
1564 g_test_add_func("/bdrv-drain/multiparent", test_multiparent
);
1566 g_test_add_func("/bdrv-drain/graph-change/drain_subtree",
1567 test_graph_change_drain_subtree
);
1568 g_test_add_func("/bdrv-drain/graph-change/drain_all",
1569 test_graph_change_drain_all
);
1571 g_test_add_func("/bdrv-drain/iothread/drain_all", test_iothread_drain_all
);
1572 g_test_add_func("/bdrv-drain/iothread/drain", test_iothread_drain
);
1573 g_test_add_func("/bdrv-drain/iothread/drain_subtree",
1574 test_iothread_drain_subtree
);
1576 g_test_add_func("/bdrv-drain/blockjob/drain_all", test_blockjob_drain_all
);
1577 g_test_add_func("/bdrv-drain/blockjob/drain", test_blockjob_drain
);
1578 g_test_add_func("/bdrv-drain/blockjob/drain_subtree",
1579 test_blockjob_drain_subtree
);
1581 g_test_add_func("/bdrv-drain/blockjob/error/drain_all",
1582 test_blockjob_error_drain_all
);
1583 g_test_add_func("/bdrv-drain/blockjob/error/drain",
1584 test_blockjob_error_drain
);
1585 g_test_add_func("/bdrv-drain/blockjob/error/drain_subtree",
1586 test_blockjob_error_drain_subtree
);
1588 g_test_add_func("/bdrv-drain/blockjob/iothread/drain_all",
1589 test_blockjob_iothread_drain_all
);
1590 g_test_add_func("/bdrv-drain/blockjob/iothread/drain",
1591 test_blockjob_iothread_drain
);
1592 g_test_add_func("/bdrv-drain/blockjob/iothread/drain_subtree",
1593 test_blockjob_iothread_drain_subtree
);
1595 g_test_add_func("/bdrv-drain/blockjob/iothread/error/drain_all",
1596 test_blockjob_iothread_error_drain_all
);
1597 g_test_add_func("/bdrv-drain/blockjob/iothread/error/drain",
1598 test_blockjob_iothread_error_drain
);
1599 g_test_add_func("/bdrv-drain/blockjob/iothread/error/drain_subtree",
1600 test_blockjob_iothread_error_drain_subtree
);
1602 g_test_add_func("/bdrv-drain/deletion/drain", test_delete_by_drain
);
1603 g_test_add_func("/bdrv-drain/detach/drain_all", test_detach_by_drain_all
);
1604 g_test_add_func("/bdrv-drain/detach/drain", test_detach_by_drain
);
1605 g_test_add_func("/bdrv-drain/detach/drain_subtree", test_detach_by_drain_subtree
);
1606 g_test_add_func("/bdrv-drain/detach/parent_cb", test_detach_by_parent_cb
);
1607 g_test_add_func("/bdrv-drain/detach/driver_cb", test_detach_by_driver_cb
);
1609 g_test_add_func("/bdrv-drain/attach/drain", test_append_to_drained
);
1611 g_test_add_func("/bdrv-drain/set_aio_context", test_set_aio_context
);
1614 qemu_event_destroy(&done_event
);