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 BdrvChildClass
*child_class
,
90 BlockReopenQueue
*reopen_queue
,
91 uint64_t perm
, uint64_t shared
,
92 uint64_t *nperm
, uint64_t *nshared
)
95 * bdrv_format_default_perms() accepts only these two, so disguise
96 * detach_by_driver_cb_parent as one of them.
98 if (child_class
!= &child_file
&& child_class
!= &child_backing
) {
99 child_class
= &child_file
;
102 bdrv_format_default_perms(bs
, c
, child_class
, reopen_queue
, perm
, shared
,
106 static int bdrv_test_change_backing_file(BlockDriverState
*bs
,
107 const char *backing_file
,
108 const char *backing_fmt
)
113 static BlockDriver bdrv_test
= {
114 .format_name
= "test",
115 .instance_size
= sizeof(BDRVTestState
),
117 .bdrv_close
= bdrv_test_close
,
118 .bdrv_co_preadv
= bdrv_test_co_preadv
,
120 .bdrv_co_drain_begin
= bdrv_test_co_drain_begin
,
121 .bdrv_co_drain_end
= bdrv_test_co_drain_end
,
123 .bdrv_child_perm
= bdrv_test_child_perm
,
125 .bdrv_change_backing_file
= bdrv_test_change_backing_file
,
128 static void aio_ret_cb(void *opaque
, int ret
)
130 int *aio_ret
= opaque
;
134 typedef struct CallInCoroutineData
{
137 } CallInCoroutineData
;
139 static coroutine_fn
void call_in_coroutine_entry(void *opaque
)
141 CallInCoroutineData
*data
= opaque
;
147 static void call_in_coroutine(void (*entry
)(void))
150 CallInCoroutineData data
= {
155 co
= qemu_coroutine_create(call_in_coroutine_entry
, &data
);
156 qemu_coroutine_enter(co
);
158 aio_poll(qemu_get_aio_context(), true);
169 static void do_drain_begin(enum drain_type drain_type
, BlockDriverState
*bs
)
171 switch (drain_type
) {
172 case BDRV_DRAIN_ALL
: bdrv_drain_all_begin(); break;
173 case BDRV_DRAIN
: bdrv_drained_begin(bs
); break;
174 case BDRV_SUBTREE_DRAIN
: bdrv_subtree_drained_begin(bs
); break;
175 default: g_assert_not_reached();
179 static void do_drain_end(enum drain_type drain_type
, BlockDriverState
*bs
)
181 switch (drain_type
) {
182 case BDRV_DRAIN_ALL
: bdrv_drain_all_end(); break;
183 case BDRV_DRAIN
: bdrv_drained_end(bs
); break;
184 case BDRV_SUBTREE_DRAIN
: bdrv_subtree_drained_end(bs
); break;
185 default: g_assert_not_reached();
189 static void do_drain_begin_unlocked(enum drain_type drain_type
, BlockDriverState
*bs
)
191 if (drain_type
!= BDRV_DRAIN_ALL
) {
192 aio_context_acquire(bdrv_get_aio_context(bs
));
194 do_drain_begin(drain_type
, bs
);
195 if (drain_type
!= BDRV_DRAIN_ALL
) {
196 aio_context_release(bdrv_get_aio_context(bs
));
200 static void do_drain_end_unlocked(enum drain_type drain_type
, BlockDriverState
*bs
)
202 if (drain_type
!= BDRV_DRAIN_ALL
) {
203 aio_context_acquire(bdrv_get_aio_context(bs
));
205 do_drain_end(drain_type
, bs
);
206 if (drain_type
!= BDRV_DRAIN_ALL
) {
207 aio_context_release(bdrv_get_aio_context(bs
));
211 static void test_drv_cb_common(enum drain_type drain_type
, bool recursive
)
214 BlockDriverState
*bs
, *backing
;
215 BDRVTestState
*s
, *backing_s
;
219 QEMUIOVector qiov
= QEMU_IOVEC_INIT_BUF(qiov
, NULL
, 0);
221 blk
= blk_new(qemu_get_aio_context(), BLK_PERM_ALL
, BLK_PERM_ALL
);
222 bs
= bdrv_new_open_driver(&bdrv_test
, "test-node", BDRV_O_RDWR
,
225 blk_insert_bs(blk
, bs
, &error_abort
);
227 backing
= bdrv_new_open_driver(&bdrv_test
, "backing", 0, &error_abort
);
228 backing_s
= backing
->opaque
;
229 bdrv_set_backing_hd(bs
, backing
, &error_abort
);
231 /* Simple bdrv_drain_all_begin/end pair, check that CBs are called */
232 g_assert_cmpint(s
->drain_count
, ==, 0);
233 g_assert_cmpint(backing_s
->drain_count
, ==, 0);
235 do_drain_begin(drain_type
, bs
);
237 g_assert_cmpint(s
->drain_count
, ==, 1);
238 g_assert_cmpint(backing_s
->drain_count
, ==, !!recursive
);
240 do_drain_end(drain_type
, bs
);
242 g_assert_cmpint(s
->drain_count
, ==, 0);
243 g_assert_cmpint(backing_s
->drain_count
, ==, 0);
245 /* Now do the same while a request is pending */
246 aio_ret
= -EINPROGRESS
;
247 acb
= blk_aio_preadv(blk
, 0, &qiov
, 0, aio_ret_cb
, &aio_ret
);
248 g_assert(acb
!= NULL
);
249 g_assert_cmpint(aio_ret
, ==, -EINPROGRESS
);
251 g_assert_cmpint(s
->drain_count
, ==, 0);
252 g_assert_cmpint(backing_s
->drain_count
, ==, 0);
254 do_drain_begin(drain_type
, bs
);
256 g_assert_cmpint(aio_ret
, ==, 0);
257 g_assert_cmpint(s
->drain_count
, ==, 1);
258 g_assert_cmpint(backing_s
->drain_count
, ==, !!recursive
);
260 do_drain_end(drain_type
, bs
);
262 g_assert_cmpint(s
->drain_count
, ==, 0);
263 g_assert_cmpint(backing_s
->drain_count
, ==, 0);
270 static void test_drv_cb_drain_all(void)
272 test_drv_cb_common(BDRV_DRAIN_ALL
, true);
275 static void test_drv_cb_drain(void)
277 test_drv_cb_common(BDRV_DRAIN
, false);
280 static void test_drv_cb_drain_subtree(void)
282 test_drv_cb_common(BDRV_SUBTREE_DRAIN
, true);
285 static void test_drv_cb_co_drain_all(void)
287 call_in_coroutine(test_drv_cb_drain_all
);
290 static void test_drv_cb_co_drain(void)
292 call_in_coroutine(test_drv_cb_drain
);
295 static void test_drv_cb_co_drain_subtree(void)
297 call_in_coroutine(test_drv_cb_drain_subtree
);
300 static void test_quiesce_common(enum drain_type drain_type
, bool recursive
)
303 BlockDriverState
*bs
, *backing
;
305 blk
= blk_new(qemu_get_aio_context(), BLK_PERM_ALL
, BLK_PERM_ALL
);
306 bs
= bdrv_new_open_driver(&bdrv_test
, "test-node", BDRV_O_RDWR
,
308 blk_insert_bs(blk
, bs
, &error_abort
);
310 backing
= bdrv_new_open_driver(&bdrv_test
, "backing", 0, &error_abort
);
311 bdrv_set_backing_hd(bs
, backing
, &error_abort
);
313 g_assert_cmpint(bs
->quiesce_counter
, ==, 0);
314 g_assert_cmpint(backing
->quiesce_counter
, ==, 0);
316 do_drain_begin(drain_type
, bs
);
318 g_assert_cmpint(bs
->quiesce_counter
, ==, 1);
319 g_assert_cmpint(backing
->quiesce_counter
, ==, !!recursive
);
321 do_drain_end(drain_type
, bs
);
323 g_assert_cmpint(bs
->quiesce_counter
, ==, 0);
324 g_assert_cmpint(backing
->quiesce_counter
, ==, 0);
331 static void test_quiesce_drain_all(void)
333 test_quiesce_common(BDRV_DRAIN_ALL
, true);
336 static void test_quiesce_drain(void)
338 test_quiesce_common(BDRV_DRAIN
, false);
341 static void test_quiesce_drain_subtree(void)
343 test_quiesce_common(BDRV_SUBTREE_DRAIN
, true);
346 static void test_quiesce_co_drain_all(void)
348 call_in_coroutine(test_quiesce_drain_all
);
351 static void test_quiesce_co_drain(void)
353 call_in_coroutine(test_quiesce_drain
);
356 static void test_quiesce_co_drain_subtree(void)
358 call_in_coroutine(test_quiesce_drain_subtree
);
361 static void test_nested(void)
364 BlockDriverState
*bs
, *backing
;
365 BDRVTestState
*s
, *backing_s
;
366 enum drain_type outer
, inner
;
368 blk
= blk_new(qemu_get_aio_context(), BLK_PERM_ALL
, BLK_PERM_ALL
);
369 bs
= bdrv_new_open_driver(&bdrv_test
, "test-node", BDRV_O_RDWR
,
372 blk_insert_bs(blk
, bs
, &error_abort
);
374 backing
= bdrv_new_open_driver(&bdrv_test
, "backing", 0, &error_abort
);
375 backing_s
= backing
->opaque
;
376 bdrv_set_backing_hd(bs
, backing
, &error_abort
);
378 for (outer
= 0; outer
< DRAIN_TYPE_MAX
; outer
++) {
379 for (inner
= 0; inner
< DRAIN_TYPE_MAX
; inner
++) {
380 int backing_quiesce
= (outer
!= BDRV_DRAIN
) +
381 (inner
!= BDRV_DRAIN
);
383 g_assert_cmpint(bs
->quiesce_counter
, ==, 0);
384 g_assert_cmpint(backing
->quiesce_counter
, ==, 0);
385 g_assert_cmpint(s
->drain_count
, ==, 0);
386 g_assert_cmpint(backing_s
->drain_count
, ==, 0);
388 do_drain_begin(outer
, bs
);
389 do_drain_begin(inner
, bs
);
391 g_assert_cmpint(bs
->quiesce_counter
, ==, 2);
392 g_assert_cmpint(backing
->quiesce_counter
, ==, backing_quiesce
);
393 g_assert_cmpint(s
->drain_count
, ==, 2);
394 g_assert_cmpint(backing_s
->drain_count
, ==, backing_quiesce
);
396 do_drain_end(inner
, bs
);
397 do_drain_end(outer
, bs
);
399 g_assert_cmpint(bs
->quiesce_counter
, ==, 0);
400 g_assert_cmpint(backing
->quiesce_counter
, ==, 0);
401 g_assert_cmpint(s
->drain_count
, ==, 0);
402 g_assert_cmpint(backing_s
->drain_count
, ==, 0);
411 static void test_multiparent(void)
413 BlockBackend
*blk_a
, *blk_b
;
414 BlockDriverState
*bs_a
, *bs_b
, *backing
;
415 BDRVTestState
*a_s
, *b_s
, *backing_s
;
417 blk_a
= blk_new(qemu_get_aio_context(), BLK_PERM_ALL
, BLK_PERM_ALL
);
418 bs_a
= bdrv_new_open_driver(&bdrv_test
, "test-node-a", BDRV_O_RDWR
,
421 blk_insert_bs(blk_a
, bs_a
, &error_abort
);
423 blk_b
= blk_new(qemu_get_aio_context(), BLK_PERM_ALL
, BLK_PERM_ALL
);
424 bs_b
= bdrv_new_open_driver(&bdrv_test
, "test-node-b", BDRV_O_RDWR
,
427 blk_insert_bs(blk_b
, bs_b
, &error_abort
);
429 backing
= bdrv_new_open_driver(&bdrv_test
, "backing", 0, &error_abort
);
430 backing_s
= backing
->opaque
;
431 bdrv_set_backing_hd(bs_a
, backing
, &error_abort
);
432 bdrv_set_backing_hd(bs_b
, backing
, &error_abort
);
434 g_assert_cmpint(bs_a
->quiesce_counter
, ==, 0);
435 g_assert_cmpint(bs_b
->quiesce_counter
, ==, 0);
436 g_assert_cmpint(backing
->quiesce_counter
, ==, 0);
437 g_assert_cmpint(a_s
->drain_count
, ==, 0);
438 g_assert_cmpint(b_s
->drain_count
, ==, 0);
439 g_assert_cmpint(backing_s
->drain_count
, ==, 0);
441 do_drain_begin(BDRV_SUBTREE_DRAIN
, bs_a
);
443 g_assert_cmpint(bs_a
->quiesce_counter
, ==, 1);
444 g_assert_cmpint(bs_b
->quiesce_counter
, ==, 1);
445 g_assert_cmpint(backing
->quiesce_counter
, ==, 1);
446 g_assert_cmpint(a_s
->drain_count
, ==, 1);
447 g_assert_cmpint(b_s
->drain_count
, ==, 1);
448 g_assert_cmpint(backing_s
->drain_count
, ==, 1);
450 do_drain_begin(BDRV_SUBTREE_DRAIN
, bs_b
);
452 g_assert_cmpint(bs_a
->quiesce_counter
, ==, 2);
453 g_assert_cmpint(bs_b
->quiesce_counter
, ==, 2);
454 g_assert_cmpint(backing
->quiesce_counter
, ==, 2);
455 g_assert_cmpint(a_s
->drain_count
, ==, 2);
456 g_assert_cmpint(b_s
->drain_count
, ==, 2);
457 g_assert_cmpint(backing_s
->drain_count
, ==, 2);
459 do_drain_end(BDRV_SUBTREE_DRAIN
, bs_b
);
461 g_assert_cmpint(bs_a
->quiesce_counter
, ==, 1);
462 g_assert_cmpint(bs_b
->quiesce_counter
, ==, 1);
463 g_assert_cmpint(backing
->quiesce_counter
, ==, 1);
464 g_assert_cmpint(a_s
->drain_count
, ==, 1);
465 g_assert_cmpint(b_s
->drain_count
, ==, 1);
466 g_assert_cmpint(backing_s
->drain_count
, ==, 1);
468 do_drain_end(BDRV_SUBTREE_DRAIN
, bs_a
);
470 g_assert_cmpint(bs_a
->quiesce_counter
, ==, 0);
471 g_assert_cmpint(bs_b
->quiesce_counter
, ==, 0);
472 g_assert_cmpint(backing
->quiesce_counter
, ==, 0);
473 g_assert_cmpint(a_s
->drain_count
, ==, 0);
474 g_assert_cmpint(b_s
->drain_count
, ==, 0);
475 g_assert_cmpint(backing_s
->drain_count
, ==, 0);
484 static void test_graph_change_drain_subtree(void)
486 BlockBackend
*blk_a
, *blk_b
;
487 BlockDriverState
*bs_a
, *bs_b
, *backing
;
488 BDRVTestState
*a_s
, *b_s
, *backing_s
;
490 blk_a
= blk_new(qemu_get_aio_context(), BLK_PERM_ALL
, BLK_PERM_ALL
);
491 bs_a
= bdrv_new_open_driver(&bdrv_test
, "test-node-a", BDRV_O_RDWR
,
494 blk_insert_bs(blk_a
, bs_a
, &error_abort
);
496 blk_b
= blk_new(qemu_get_aio_context(), BLK_PERM_ALL
, BLK_PERM_ALL
);
497 bs_b
= bdrv_new_open_driver(&bdrv_test
, "test-node-b", BDRV_O_RDWR
,
500 blk_insert_bs(blk_b
, bs_b
, &error_abort
);
502 backing
= bdrv_new_open_driver(&bdrv_test
, "backing", 0, &error_abort
);
503 backing_s
= backing
->opaque
;
504 bdrv_set_backing_hd(bs_a
, backing
, &error_abort
);
506 g_assert_cmpint(bs_a
->quiesce_counter
, ==, 0);
507 g_assert_cmpint(bs_b
->quiesce_counter
, ==, 0);
508 g_assert_cmpint(backing
->quiesce_counter
, ==, 0);
509 g_assert_cmpint(a_s
->drain_count
, ==, 0);
510 g_assert_cmpint(b_s
->drain_count
, ==, 0);
511 g_assert_cmpint(backing_s
->drain_count
, ==, 0);
513 do_drain_begin(BDRV_SUBTREE_DRAIN
, bs_a
);
514 do_drain_begin(BDRV_SUBTREE_DRAIN
, bs_a
);
515 do_drain_begin(BDRV_SUBTREE_DRAIN
, bs_a
);
516 do_drain_begin(BDRV_SUBTREE_DRAIN
, bs_b
);
517 do_drain_begin(BDRV_SUBTREE_DRAIN
, bs_b
);
519 bdrv_set_backing_hd(bs_b
, backing
, &error_abort
);
520 g_assert_cmpint(bs_a
->quiesce_counter
, ==, 5);
521 g_assert_cmpint(bs_b
->quiesce_counter
, ==, 5);
522 g_assert_cmpint(backing
->quiesce_counter
, ==, 5);
523 g_assert_cmpint(a_s
->drain_count
, ==, 5);
524 g_assert_cmpint(b_s
->drain_count
, ==, 5);
525 g_assert_cmpint(backing_s
->drain_count
, ==, 5);
527 bdrv_set_backing_hd(bs_b
, NULL
, &error_abort
);
528 g_assert_cmpint(bs_a
->quiesce_counter
, ==, 3);
529 g_assert_cmpint(bs_b
->quiesce_counter
, ==, 2);
530 g_assert_cmpint(backing
->quiesce_counter
, ==, 3);
531 g_assert_cmpint(a_s
->drain_count
, ==, 3);
532 g_assert_cmpint(b_s
->drain_count
, ==, 2);
533 g_assert_cmpint(backing_s
->drain_count
, ==, 3);
535 bdrv_set_backing_hd(bs_b
, backing
, &error_abort
);
536 g_assert_cmpint(bs_a
->quiesce_counter
, ==, 5);
537 g_assert_cmpint(bs_b
->quiesce_counter
, ==, 5);
538 g_assert_cmpint(backing
->quiesce_counter
, ==, 5);
539 g_assert_cmpint(a_s
->drain_count
, ==, 5);
540 g_assert_cmpint(b_s
->drain_count
, ==, 5);
541 g_assert_cmpint(backing_s
->drain_count
, ==, 5);
543 do_drain_end(BDRV_SUBTREE_DRAIN
, bs_b
);
544 do_drain_end(BDRV_SUBTREE_DRAIN
, bs_b
);
545 do_drain_end(BDRV_SUBTREE_DRAIN
, bs_a
);
546 do_drain_end(BDRV_SUBTREE_DRAIN
, bs_a
);
547 do_drain_end(BDRV_SUBTREE_DRAIN
, bs_a
);
549 g_assert_cmpint(bs_a
->quiesce_counter
, ==, 0);
550 g_assert_cmpint(bs_b
->quiesce_counter
, ==, 0);
551 g_assert_cmpint(backing
->quiesce_counter
, ==, 0);
552 g_assert_cmpint(a_s
->drain_count
, ==, 0);
553 g_assert_cmpint(b_s
->drain_count
, ==, 0);
554 g_assert_cmpint(backing_s
->drain_count
, ==, 0);
563 static void test_graph_change_drain_all(void)
565 BlockBackend
*blk_a
, *blk_b
;
566 BlockDriverState
*bs_a
, *bs_b
;
567 BDRVTestState
*a_s
, *b_s
;
569 /* Create node A with a BlockBackend */
570 blk_a
= blk_new(qemu_get_aio_context(), BLK_PERM_ALL
, BLK_PERM_ALL
);
571 bs_a
= bdrv_new_open_driver(&bdrv_test
, "test-node-a", BDRV_O_RDWR
,
574 blk_insert_bs(blk_a
, bs_a
, &error_abort
);
576 g_assert_cmpint(bs_a
->quiesce_counter
, ==, 0);
577 g_assert_cmpint(a_s
->drain_count
, ==, 0);
579 /* Call bdrv_drain_all_begin() */
580 bdrv_drain_all_begin();
582 g_assert_cmpint(bs_a
->quiesce_counter
, ==, 1);
583 g_assert_cmpint(a_s
->drain_count
, ==, 1);
585 /* Create node B with a BlockBackend */
586 blk_b
= blk_new(qemu_get_aio_context(), BLK_PERM_ALL
, BLK_PERM_ALL
);
587 bs_b
= bdrv_new_open_driver(&bdrv_test
, "test-node-b", BDRV_O_RDWR
,
590 blk_insert_bs(blk_b
, bs_b
, &error_abort
);
592 g_assert_cmpint(bs_a
->quiesce_counter
, ==, 1);
593 g_assert_cmpint(bs_b
->quiesce_counter
, ==, 1);
594 g_assert_cmpint(a_s
->drain_count
, ==, 1);
595 g_assert_cmpint(b_s
->drain_count
, ==, 1);
597 /* Unref and finally delete node A */
600 g_assert_cmpint(bs_a
->quiesce_counter
, ==, 1);
601 g_assert_cmpint(bs_b
->quiesce_counter
, ==, 1);
602 g_assert_cmpint(a_s
->drain_count
, ==, 1);
603 g_assert_cmpint(b_s
->drain_count
, ==, 1);
607 g_assert_cmpint(bs_b
->quiesce_counter
, ==, 1);
608 g_assert_cmpint(b_s
->drain_count
, ==, 1);
610 /* End the drained section */
611 bdrv_drain_all_end();
613 g_assert_cmpint(bs_b
->quiesce_counter
, ==, 0);
614 g_assert_cmpint(b_s
->drain_count
, ==, 0);
620 struct test_iothread_data
{
621 BlockDriverState
*bs
;
622 enum drain_type drain_type
;
626 static void test_iothread_drain_entry(void *opaque
)
628 struct test_iothread_data
*data
= opaque
;
630 aio_context_acquire(bdrv_get_aio_context(data
->bs
));
631 do_drain_begin(data
->drain_type
, data
->bs
);
632 g_assert_cmpint(*data
->aio_ret
, ==, 0);
633 do_drain_end(data
->drain_type
, data
->bs
);
634 aio_context_release(bdrv_get_aio_context(data
->bs
));
636 qemu_event_set(&done_event
);
639 static void test_iothread_aio_cb(void *opaque
, int ret
)
641 int *aio_ret
= opaque
;
643 qemu_event_set(&done_event
);
646 static void test_iothread_main_thread_bh(void *opaque
)
648 struct test_iothread_data
*data
= opaque
;
650 /* Test that the AioContext is not yet locked in a random BH that is
651 * executed during drain, otherwise this would deadlock. */
652 aio_context_acquire(bdrv_get_aio_context(data
->bs
));
653 bdrv_flush(data
->bs
);
654 aio_context_release(bdrv_get_aio_context(data
->bs
));
658 * Starts an AIO request on a BDS that runs in the AioContext of iothread 1.
659 * The request involves a BH on iothread 2 before it can complete.
661 * @drain_thread = 0 means that do_drain_begin/end are called from the main
662 * thread, @drain_thread = 1 means that they are called from iothread 1. Drain
663 * for this BDS cannot be called from iothread 2 because only the main thread
664 * may do cross-AioContext polling.
666 static void test_iothread_common(enum drain_type drain_type
, int drain_thread
)
669 BlockDriverState
*bs
;
673 struct test_iothread_data data
;
675 IOThread
*a
= iothread_new();
676 IOThread
*b
= iothread_new();
677 AioContext
*ctx_a
= iothread_get_aio_context(a
);
678 AioContext
*ctx_b
= iothread_get_aio_context(b
);
680 QEMUIOVector qiov
= QEMU_IOVEC_INIT_BUF(qiov
, NULL
, 0);
682 /* bdrv_drain_all() may only be called from the main loop thread */
683 if (drain_type
== BDRV_DRAIN_ALL
&& drain_thread
!= 0) {
687 blk
= blk_new(qemu_get_aio_context(), BLK_PERM_ALL
, BLK_PERM_ALL
);
688 bs
= bdrv_new_open_driver(&bdrv_test
, "test-node", BDRV_O_RDWR
,
691 blk_insert_bs(blk
, bs
, &error_abort
);
692 blk_set_disable_request_queuing(blk
, true);
694 blk_set_aio_context(blk
, ctx_a
, &error_abort
);
695 aio_context_acquire(ctx_a
);
697 s
->bh_indirection_ctx
= ctx_b
;
699 aio_ret
= -EINPROGRESS
;
700 qemu_event_reset(&done_event
);
702 if (drain_thread
== 0) {
703 acb
= blk_aio_preadv(blk
, 0, &qiov
, 0, test_iothread_aio_cb
, &aio_ret
);
705 acb
= blk_aio_preadv(blk
, 0, &qiov
, 0, aio_ret_cb
, &aio_ret
);
707 g_assert(acb
!= NULL
);
708 g_assert_cmpint(aio_ret
, ==, -EINPROGRESS
);
710 aio_context_release(ctx_a
);
712 data
= (struct test_iothread_data
) {
714 .drain_type
= drain_type
,
718 switch (drain_thread
) {
720 if (drain_type
!= BDRV_DRAIN_ALL
) {
721 aio_context_acquire(ctx_a
);
724 aio_bh_schedule_oneshot(ctx_a
, test_iothread_main_thread_bh
, &data
);
726 /* The request is running on the IOThread a. Draining its block device
727 * will make sure that it has completed as far as the BDS is concerned,
728 * but the drain in this thread can continue immediately after
729 * bdrv_dec_in_flight() and aio_ret might be assigned only slightly
731 do_drain_begin(drain_type
, bs
);
732 g_assert_cmpint(bs
->in_flight
, ==, 0);
734 if (drain_type
!= BDRV_DRAIN_ALL
) {
735 aio_context_release(ctx_a
);
737 qemu_event_wait(&done_event
);
738 if (drain_type
!= BDRV_DRAIN_ALL
) {
739 aio_context_acquire(ctx_a
);
742 g_assert_cmpint(aio_ret
, ==, 0);
743 do_drain_end(drain_type
, bs
);
745 if (drain_type
!= BDRV_DRAIN_ALL
) {
746 aio_context_release(ctx_a
);
750 aio_bh_schedule_oneshot(ctx_a
, test_iothread_drain_entry
, &data
);
751 qemu_event_wait(&done_event
);
754 g_assert_not_reached();
757 aio_context_acquire(ctx_a
);
758 blk_set_aio_context(blk
, qemu_get_aio_context(), &error_abort
);
759 aio_context_release(ctx_a
);
769 static void test_iothread_drain_all(void)
771 test_iothread_common(BDRV_DRAIN_ALL
, 0);
772 test_iothread_common(BDRV_DRAIN_ALL
, 1);
775 static void test_iothread_drain(void)
777 test_iothread_common(BDRV_DRAIN
, 0);
778 test_iothread_common(BDRV_DRAIN
, 1);
781 static void test_iothread_drain_subtree(void)
783 test_iothread_common(BDRV_SUBTREE_DRAIN
, 0);
784 test_iothread_common(BDRV_SUBTREE_DRAIN
, 1);
788 typedef struct TestBlockJob
{
793 bool should_complete
;
796 static int test_job_prepare(Job
*job
)
798 TestBlockJob
*s
= container_of(job
, TestBlockJob
, common
.job
);
800 /* Provoke an AIO_WAIT_WHILE() call to verify there is no deadlock */
801 blk_flush(s
->common
.blk
);
802 return s
->prepare_ret
;
805 static void test_job_commit(Job
*job
)
807 TestBlockJob
*s
= container_of(job
, TestBlockJob
, common
.job
);
809 /* Provoke an AIO_WAIT_WHILE() call to verify there is no deadlock */
810 blk_flush(s
->common
.blk
);
813 static void test_job_abort(Job
*job
)
815 TestBlockJob
*s
= container_of(job
, TestBlockJob
, common
.job
);
817 /* Provoke an AIO_WAIT_WHILE() call to verify there is no deadlock */
818 blk_flush(s
->common
.blk
);
821 static int coroutine_fn
test_job_run(Job
*job
, Error
**errp
)
823 TestBlockJob
*s
= container_of(job
, TestBlockJob
, common
.job
);
825 /* We are running the actual job code past the pause point in
829 job_transition_to_ready(&s
->common
.job
);
830 while (!s
->should_complete
) {
831 /* Avoid job_sleep_ns() because it marks the job as !busy. We want to
832 * emulate some actual activity (probably some I/O) here so that drain
833 * has to wait for this activity to stop. */
834 qemu_co_sleep_ns(QEMU_CLOCK_REALTIME
, 1000000);
836 job_pause_point(&s
->common
.job
);
842 static void test_job_complete(Job
*job
, Error
**errp
)
844 TestBlockJob
*s
= container_of(job
, TestBlockJob
, common
.job
);
845 s
->should_complete
= true;
848 BlockJobDriver test_job_driver
= {
850 .instance_size
= sizeof(TestBlockJob
),
851 .free
= block_job_free
,
852 .user_resume
= block_job_user_resume
,
854 .complete
= test_job_complete
,
855 .prepare
= test_job_prepare
,
856 .commit
= test_job_commit
,
857 .abort
= test_job_abort
,
861 enum test_job_result
{
864 TEST_JOB_FAIL_PREPARE
,
867 enum test_job_drain_node
{
869 TEST_JOB_DRAIN_SRC_CHILD
,
870 TEST_JOB_DRAIN_SRC_PARENT
,
873 static void test_blockjob_common_drain_node(enum drain_type drain_type
,
875 enum test_job_result result
,
876 enum test_job_drain_node drain_node
)
878 BlockBackend
*blk_src
, *blk_target
;
879 BlockDriverState
*src
, *src_backing
, *src_overlay
, *target
, *drain_bs
;
882 IOThread
*iothread
= NULL
;
886 src
= bdrv_new_open_driver(&bdrv_test
, "source", BDRV_O_RDWR
,
888 src_backing
= bdrv_new_open_driver(&bdrv_test
, "source-backing",
889 BDRV_O_RDWR
, &error_abort
);
890 src_overlay
= bdrv_new_open_driver(&bdrv_test
, "source-overlay",
891 BDRV_O_RDWR
, &error_abort
);
893 bdrv_set_backing_hd(src_overlay
, src
, &error_abort
);
895 bdrv_set_backing_hd(src
, src_backing
, &error_abort
);
896 bdrv_unref(src_backing
);
898 blk_src
= blk_new(qemu_get_aio_context(), BLK_PERM_ALL
, BLK_PERM_ALL
);
899 blk_insert_bs(blk_src
, src_overlay
, &error_abort
);
901 switch (drain_node
) {
902 case TEST_JOB_DRAIN_SRC
:
905 case TEST_JOB_DRAIN_SRC_CHILD
:
906 drain_bs
= src_backing
;
908 case TEST_JOB_DRAIN_SRC_PARENT
:
909 drain_bs
= src_overlay
;
912 g_assert_not_reached();
916 iothread
= iothread_new();
917 ctx
= iothread_get_aio_context(iothread
);
918 blk_set_aio_context(blk_src
, ctx
, &error_abort
);
920 ctx
= qemu_get_aio_context();
923 target
= bdrv_new_open_driver(&bdrv_test
, "target", BDRV_O_RDWR
,
925 blk_target
= blk_new(qemu_get_aio_context(), BLK_PERM_ALL
, BLK_PERM_ALL
);
926 blk_insert_bs(blk_target
, target
, &error_abort
);
927 blk_set_allow_aio_context_change(blk_target
, true);
929 aio_context_acquire(ctx
);
930 tjob
= block_job_create("job0", &test_job_driver
, NULL
, src
,
932 0, 0, NULL
, NULL
, &error_abort
);
934 block_job_add_bdrv(job
, "target", target
, 0, BLK_PERM_ALL
, &error_abort
);
937 case TEST_JOB_SUCCESS
:
939 case TEST_JOB_FAIL_RUN
:
940 tjob
->run_ret
= -EIO
;
942 case TEST_JOB_FAIL_PREPARE
:
943 tjob
->prepare_ret
= -EIO
;
947 job_start(&job
->job
);
948 aio_context_release(ctx
);
951 /* job_co_entry() is run in the I/O thread, wait for the actual job
952 * code to start (we don't want to catch the job in the pause point in
954 while (!tjob
->running
) {
955 aio_poll(qemu_get_aio_context(), false);
959 g_assert_cmpint(job
->job
.pause_count
, ==, 0);
960 g_assert_false(job
->job
.paused
);
961 g_assert_true(tjob
->running
);
962 g_assert_true(job
->job
.busy
); /* We're in qemu_co_sleep_ns() */
964 do_drain_begin_unlocked(drain_type
, drain_bs
);
966 if (drain_type
== BDRV_DRAIN_ALL
) {
967 /* bdrv_drain_all() drains both src and target */
968 g_assert_cmpint(job
->job
.pause_count
, ==, 2);
970 g_assert_cmpint(job
->job
.pause_count
, ==, 1);
972 g_assert_true(job
->job
.paused
);
973 g_assert_false(job
->job
.busy
); /* The job is paused */
975 do_drain_end_unlocked(drain_type
, drain_bs
);
978 /* paused is reset in the I/O thread, wait for it */
979 while (job
->job
.paused
) {
980 aio_poll(qemu_get_aio_context(), false);
984 g_assert_cmpint(job
->job
.pause_count
, ==, 0);
985 g_assert_false(job
->job
.paused
);
986 g_assert_true(job
->job
.busy
); /* We're in qemu_co_sleep_ns() */
988 do_drain_begin_unlocked(drain_type
, target
);
990 if (drain_type
== BDRV_DRAIN_ALL
) {
991 /* bdrv_drain_all() drains both src and target */
992 g_assert_cmpint(job
->job
.pause_count
, ==, 2);
994 g_assert_cmpint(job
->job
.pause_count
, ==, 1);
996 g_assert_true(job
->job
.paused
);
997 g_assert_false(job
->job
.busy
); /* The job is paused */
999 do_drain_end_unlocked(drain_type
, target
);
1002 /* paused is reset in the I/O thread, wait for it */
1003 while (job
->job
.paused
) {
1004 aio_poll(qemu_get_aio_context(), false);
1008 g_assert_cmpint(job
->job
.pause_count
, ==, 0);
1009 g_assert_false(job
->job
.paused
);
1010 g_assert_true(job
->job
.busy
); /* We're in qemu_co_sleep_ns() */
1012 aio_context_acquire(ctx
);
1013 ret
= job_complete_sync(&job
->job
, &error_abort
);
1014 g_assert_cmpint(ret
, ==, (result
== TEST_JOB_SUCCESS
? 0 : -EIO
));
1017 blk_set_aio_context(blk_src
, qemu_get_aio_context(), &error_abort
);
1018 assert(blk_get_aio_context(blk_target
) == qemu_get_aio_context());
1020 aio_context_release(ctx
);
1023 blk_unref(blk_target
);
1024 bdrv_unref(src_overlay
);
1028 iothread_join(iothread
);
1032 static void test_blockjob_common(enum drain_type drain_type
, bool use_iothread
,
1033 enum test_job_result result
)
1035 test_blockjob_common_drain_node(drain_type
, use_iothread
, result
,
1036 TEST_JOB_DRAIN_SRC
);
1037 test_blockjob_common_drain_node(drain_type
, use_iothread
, result
,
1038 TEST_JOB_DRAIN_SRC_CHILD
);
1039 if (drain_type
== BDRV_SUBTREE_DRAIN
) {
1040 test_blockjob_common_drain_node(drain_type
, use_iothread
, result
,
1041 TEST_JOB_DRAIN_SRC_PARENT
);
1045 static void test_blockjob_drain_all(void)
1047 test_blockjob_common(BDRV_DRAIN_ALL
, false, TEST_JOB_SUCCESS
);
1050 static void test_blockjob_drain(void)
1052 test_blockjob_common(BDRV_DRAIN
, false, TEST_JOB_SUCCESS
);
1055 static void test_blockjob_drain_subtree(void)
1057 test_blockjob_common(BDRV_SUBTREE_DRAIN
, false, TEST_JOB_SUCCESS
);
1060 static void test_blockjob_error_drain_all(void)
1062 test_blockjob_common(BDRV_DRAIN_ALL
, false, TEST_JOB_FAIL_RUN
);
1063 test_blockjob_common(BDRV_DRAIN_ALL
, false, TEST_JOB_FAIL_PREPARE
);
1066 static void test_blockjob_error_drain(void)
1068 test_blockjob_common(BDRV_DRAIN
, false, TEST_JOB_FAIL_RUN
);
1069 test_blockjob_common(BDRV_DRAIN
, false, TEST_JOB_FAIL_PREPARE
);
1072 static void test_blockjob_error_drain_subtree(void)
1074 test_blockjob_common(BDRV_SUBTREE_DRAIN
, false, TEST_JOB_FAIL_RUN
);
1075 test_blockjob_common(BDRV_SUBTREE_DRAIN
, false, TEST_JOB_FAIL_PREPARE
);
1078 static void test_blockjob_iothread_drain_all(void)
1080 test_blockjob_common(BDRV_DRAIN_ALL
, true, TEST_JOB_SUCCESS
);
1083 static void test_blockjob_iothread_drain(void)
1085 test_blockjob_common(BDRV_DRAIN
, true, TEST_JOB_SUCCESS
);
1088 static void test_blockjob_iothread_drain_subtree(void)
1090 test_blockjob_common(BDRV_SUBTREE_DRAIN
, true, TEST_JOB_SUCCESS
);
1093 static void test_blockjob_iothread_error_drain_all(void)
1095 test_blockjob_common(BDRV_DRAIN_ALL
, true, TEST_JOB_FAIL_RUN
);
1096 test_blockjob_common(BDRV_DRAIN_ALL
, true, TEST_JOB_FAIL_PREPARE
);
1099 static void test_blockjob_iothread_error_drain(void)
1101 test_blockjob_common(BDRV_DRAIN
, true, TEST_JOB_FAIL_RUN
);
1102 test_blockjob_common(BDRV_DRAIN
, true, TEST_JOB_FAIL_PREPARE
);
1105 static void test_blockjob_iothread_error_drain_subtree(void)
1107 test_blockjob_common(BDRV_SUBTREE_DRAIN
, true, TEST_JOB_FAIL_RUN
);
1108 test_blockjob_common(BDRV_SUBTREE_DRAIN
, true, TEST_JOB_FAIL_PREPARE
);
1112 typedef struct BDRVTestTopState
{
1113 BdrvChild
*wait_child
;
1116 static void bdrv_test_top_close(BlockDriverState
*bs
)
1118 BdrvChild
*c
, *next_c
;
1119 QLIST_FOREACH_SAFE(c
, &bs
->children
, next
, next_c
) {
1120 bdrv_unref_child(bs
, c
);
1124 static int coroutine_fn
bdrv_test_top_co_preadv(BlockDriverState
*bs
,
1125 uint64_t offset
, uint64_t bytes
,
1126 QEMUIOVector
*qiov
, int flags
)
1128 BDRVTestTopState
*tts
= bs
->opaque
;
1129 return bdrv_co_preadv(tts
->wait_child
, offset
, bytes
, qiov
, flags
);
1132 static BlockDriver bdrv_test_top_driver
= {
1133 .format_name
= "test_top_driver",
1134 .instance_size
= sizeof(BDRVTestTopState
),
1136 .bdrv_close
= bdrv_test_top_close
,
1137 .bdrv_co_preadv
= bdrv_test_top_co_preadv
,
1139 .bdrv_child_perm
= bdrv_format_default_perms
,
1142 typedef struct TestCoDeleteByDrainData
{
1144 bool detach_instead_of_delete
;
1146 } TestCoDeleteByDrainData
;
1148 static void coroutine_fn
test_co_delete_by_drain(void *opaque
)
1150 TestCoDeleteByDrainData
*dbdd
= opaque
;
1151 BlockBackend
*blk
= dbdd
->blk
;
1152 BlockDriverState
*bs
= blk_bs(blk
);
1153 BDRVTestTopState
*tts
= bs
->opaque
;
1154 void *buffer
= g_malloc(65536);
1155 QEMUIOVector qiov
= QEMU_IOVEC_INIT_BUF(qiov
, buffer
, 65536);
1157 /* Pretend some internal write operation from parent to child.
1158 * Important: We have to read from the child, not from the parent!
1159 * Draining works by first propagating it all up the tree to the
1160 * root and then waiting for drainage from root to the leaves
1161 * (protocol nodes). If we have a request waiting on the root,
1162 * everything will be drained before we go back down the tree, but
1163 * we do not want that. We want to be in the middle of draining
1164 * when this following requests returns. */
1165 bdrv_co_preadv(tts
->wait_child
, 0, 65536, &qiov
, 0);
1167 g_assert_cmpint(bs
->refcnt
, ==, 1);
1169 if (!dbdd
->detach_instead_of_delete
) {
1172 BdrvChild
*c
, *next_c
;
1173 QLIST_FOREACH_SAFE(c
, &bs
->children
, next
, next_c
) {
1174 bdrv_unref_child(bs
, c
);
1183 * Test what happens when some BDS has some children, you drain one of
1184 * them and this results in the BDS being deleted.
1186 * If @detach_instead_of_delete is set, the BDS is not going to be
1187 * deleted but will only detach all of its children.
1189 static void do_test_delete_by_drain(bool detach_instead_of_delete
,
1190 enum drain_type drain_type
)
1193 BlockDriverState
*bs
, *child_bs
, *null_bs
;
1194 BDRVTestTopState
*tts
;
1195 TestCoDeleteByDrainData dbdd
;
1198 bs
= bdrv_new_open_driver(&bdrv_test_top_driver
, "top", BDRV_O_RDWR
,
1200 bs
->total_sectors
= 65536 >> BDRV_SECTOR_BITS
;
1203 null_bs
= bdrv_open("null-co://", NULL
, NULL
, BDRV_O_RDWR
| BDRV_O_PROTOCOL
,
1205 bdrv_attach_child(bs
, null_bs
, "null-child", &child_file
, 0, &error_abort
);
1207 /* This child will be the one to pass to requests through to, and
1208 * it will stall until a drain occurs */
1209 child_bs
= bdrv_new_open_driver(&bdrv_test
, "child", BDRV_O_RDWR
,
1211 child_bs
->total_sectors
= 65536 >> BDRV_SECTOR_BITS
;
1212 /* Takes our reference to child_bs */
1213 tts
->wait_child
= bdrv_attach_child(bs
, child_bs
, "wait-child", &child_file
,
1216 /* This child is just there to be deleted
1217 * (for detach_instead_of_delete == true) */
1218 null_bs
= bdrv_open("null-co://", NULL
, NULL
, BDRV_O_RDWR
| BDRV_O_PROTOCOL
,
1220 bdrv_attach_child(bs
, null_bs
, "null-child", &child_file
, 0, &error_abort
);
1222 blk
= blk_new(qemu_get_aio_context(), BLK_PERM_ALL
, BLK_PERM_ALL
);
1223 blk_insert_bs(blk
, bs
, &error_abort
);
1225 /* Referenced by blk now */
1228 g_assert_cmpint(bs
->refcnt
, ==, 1);
1229 g_assert_cmpint(child_bs
->refcnt
, ==, 1);
1230 g_assert_cmpint(null_bs
->refcnt
, ==, 1);
1233 dbdd
= (TestCoDeleteByDrainData
){
1235 .detach_instead_of_delete
= detach_instead_of_delete
,
1238 co
= qemu_coroutine_create(test_co_delete_by_drain
, &dbdd
);
1239 qemu_coroutine_enter(co
);
1241 /* Drain the child while the read operation is still pending.
1242 * This should result in the operation finishing and
1243 * test_co_delete_by_drain() resuming. Thus, @bs will be deleted
1244 * and the coroutine will exit while this drain operation is still
1246 switch (drain_type
) {
1249 bdrv_drain(child_bs
);
1250 bdrv_unref(child_bs
);
1252 case BDRV_SUBTREE_DRAIN
:
1253 /* Would have to ref/unref bs here for !detach_instead_of_delete, but
1254 * then the whole test becomes pointless because the graph changes
1255 * don't occur during the drain any more. */
1256 assert(detach_instead_of_delete
);
1257 bdrv_subtree_drained_begin(bs
);
1258 bdrv_subtree_drained_end(bs
);
1260 case BDRV_DRAIN_ALL
:
1261 bdrv_drain_all_begin();
1262 bdrv_drain_all_end();
1265 g_assert_not_reached();
1268 while (!dbdd
.done
) {
1269 aio_poll(qemu_get_aio_context(), true);
1272 if (detach_instead_of_delete
) {
1273 /* Here, the reference has not passed over to the coroutine,
1274 * so we have to delete the BB ourselves */
1279 static void test_delete_by_drain(void)
1281 do_test_delete_by_drain(false, BDRV_DRAIN
);
1284 static void test_detach_by_drain_all(void)
1286 do_test_delete_by_drain(true, BDRV_DRAIN_ALL
);
1289 static void test_detach_by_drain(void)
1291 do_test_delete_by_drain(true, BDRV_DRAIN
);
1294 static void test_detach_by_drain_subtree(void)
1296 do_test_delete_by_drain(true, BDRV_SUBTREE_DRAIN
);
1300 struct detach_by_parent_data
{
1301 BlockDriverState
*parent_b
;
1303 BlockDriverState
*c
;
1307 static struct detach_by_parent_data detach_by_parent_data
;
1309 static void detach_indirect_bh(void *opaque
)
1311 struct detach_by_parent_data
*data
= opaque
;
1313 bdrv_unref_child(data
->parent_b
, data
->child_b
);
1316 data
->child_c
= bdrv_attach_child(data
->parent_b
, data
->c
, "PB-C",
1317 &child_file
, 0, &error_abort
);
1320 static void detach_by_parent_aio_cb(void *opaque
, int ret
)
1322 struct detach_by_parent_data
*data
= &detach_by_parent_data
;
1324 g_assert_cmpint(ret
, ==, 0);
1325 if (data
->by_parent_cb
) {
1326 detach_indirect_bh(data
);
1330 static void detach_by_driver_cb_drained_begin(BdrvChild
*child
)
1332 aio_bh_schedule_oneshot(qemu_get_current_aio_context(),
1333 detach_indirect_bh
, &detach_by_parent_data
);
1334 child_file
.drained_begin(child
);
1337 static BdrvChildClass detach_by_driver_cb_class
;
1346 * by_parent_cb == true: Test that parent callbacks don't poll
1348 * PA has a pending write request whose callback changes the child nodes of
1349 * PB: It removes B and adds C instead. The subtree of PB is drained, which
1350 * will indirectly drain the write request, too.
1352 * by_parent_cb == false: Test that bdrv_drain_invoke() doesn't poll
1354 * PA's BdrvChildClass has a .drained_begin callback that schedules a BH
1355 * that does the same graph change. If bdrv_drain_invoke() calls it, the
1356 * state is messed up, but if it is only polled in the single
1357 * BDRV_POLL_WHILE() at the end of the drain, this should work fine.
1359 static void test_detach_indirect(bool by_parent_cb
)
1362 BlockDriverState
*parent_a
, *parent_b
, *a
, *b
, *c
;
1363 BdrvChild
*child_a
, *child_b
;
1366 QEMUIOVector qiov
= QEMU_IOVEC_INIT_BUF(qiov
, NULL
, 0);
1368 if (!by_parent_cb
) {
1369 detach_by_driver_cb_class
= child_file
;
1370 detach_by_driver_cb_class
.drained_begin
=
1371 detach_by_driver_cb_drained_begin
;
1374 /* Create all involved nodes */
1375 parent_a
= bdrv_new_open_driver(&bdrv_test
, "parent-a", BDRV_O_RDWR
,
1377 parent_b
= bdrv_new_open_driver(&bdrv_test
, "parent-b", 0,
1380 a
= bdrv_new_open_driver(&bdrv_test
, "a", BDRV_O_RDWR
, &error_abort
);
1381 b
= bdrv_new_open_driver(&bdrv_test
, "b", BDRV_O_RDWR
, &error_abort
);
1382 c
= bdrv_new_open_driver(&bdrv_test
, "c", BDRV_O_RDWR
, &error_abort
);
1384 /* blk is a BB for parent-a */
1385 blk
= blk_new(qemu_get_aio_context(), BLK_PERM_ALL
, BLK_PERM_ALL
);
1386 blk_insert_bs(blk
, parent_a
, &error_abort
);
1387 bdrv_unref(parent_a
);
1389 /* If we want to get bdrv_drain_invoke() to call aio_poll(), the driver
1390 * callback must not return immediately. */
1391 if (!by_parent_cb
) {
1392 BDRVTestState
*s
= parent_a
->opaque
;
1393 s
->sleep_in_drain_begin
= true;
1396 /* Set child relationships */
1399 child_b
= bdrv_attach_child(parent_b
, b
, "PB-B", &child_file
, 0,
1401 child_a
= bdrv_attach_child(parent_b
, a
, "PB-A", &child_backing
, 0,
1405 bdrv_attach_child(parent_a
, a
, "PA-A",
1406 by_parent_cb
? &child_file
: &detach_by_driver_cb_class
,
1409 g_assert_cmpint(parent_a
->refcnt
, ==, 1);
1410 g_assert_cmpint(parent_b
->refcnt
, ==, 1);
1411 g_assert_cmpint(a
->refcnt
, ==, 3);
1412 g_assert_cmpint(b
->refcnt
, ==, 2);
1413 g_assert_cmpint(c
->refcnt
, ==, 1);
1415 g_assert(QLIST_FIRST(&parent_b
->children
) == child_a
);
1416 g_assert(QLIST_NEXT(child_a
, next
) == child_b
);
1417 g_assert(QLIST_NEXT(child_b
, next
) == NULL
);
1419 /* Start the evil write request */
1420 detach_by_parent_data
= (struct detach_by_parent_data
) {
1421 .parent_b
= parent_b
,
1424 .by_parent_cb
= by_parent_cb
,
1426 acb
= blk_aio_preadv(blk
, 0, &qiov
, 0, detach_by_parent_aio_cb
, NULL
);
1427 g_assert(acb
!= NULL
);
1429 /* Drain and check the expected result */
1430 bdrv_subtree_drained_begin(parent_b
);
1432 g_assert(detach_by_parent_data
.child_c
!= NULL
);
1434 g_assert_cmpint(parent_a
->refcnt
, ==, 1);
1435 g_assert_cmpint(parent_b
->refcnt
, ==, 1);
1436 g_assert_cmpint(a
->refcnt
, ==, 3);
1437 g_assert_cmpint(b
->refcnt
, ==, 1);
1438 g_assert_cmpint(c
->refcnt
, ==, 2);
1440 g_assert(QLIST_FIRST(&parent_b
->children
) == detach_by_parent_data
.child_c
);
1441 g_assert(QLIST_NEXT(detach_by_parent_data
.child_c
, next
) == child_a
);
1442 g_assert(QLIST_NEXT(child_a
, next
) == NULL
);
1444 g_assert_cmpint(parent_a
->quiesce_counter
, ==, 1);
1445 g_assert_cmpint(parent_b
->quiesce_counter
, ==, 1);
1446 g_assert_cmpint(a
->quiesce_counter
, ==, 1);
1447 g_assert_cmpint(b
->quiesce_counter
, ==, 0);
1448 g_assert_cmpint(c
->quiesce_counter
, ==, 1);
1450 bdrv_subtree_drained_end(parent_b
);
1452 bdrv_unref(parent_b
);
1455 g_assert_cmpint(a
->refcnt
, ==, 1);
1456 g_assert_cmpint(b
->refcnt
, ==, 1);
1457 g_assert_cmpint(c
->refcnt
, ==, 1);
1463 static void test_detach_by_parent_cb(void)
1465 test_detach_indirect(true);
1468 static void test_detach_by_driver_cb(void)
1470 test_detach_indirect(false);
1473 static void test_append_to_drained(void)
1476 BlockDriverState
*base
, *overlay
;
1477 BDRVTestState
*base_s
, *overlay_s
;
1479 blk
= blk_new(qemu_get_aio_context(), BLK_PERM_ALL
, BLK_PERM_ALL
);
1480 base
= bdrv_new_open_driver(&bdrv_test
, "base", BDRV_O_RDWR
, &error_abort
);
1481 base_s
= base
->opaque
;
1482 blk_insert_bs(blk
, base
, &error_abort
);
1484 overlay
= bdrv_new_open_driver(&bdrv_test
, "overlay", BDRV_O_RDWR
,
1486 overlay_s
= overlay
->opaque
;
1488 do_drain_begin(BDRV_DRAIN
, base
);
1489 g_assert_cmpint(base
->quiesce_counter
, ==, 1);
1490 g_assert_cmpint(base_s
->drain_count
, ==, 1);
1491 g_assert_cmpint(base
->in_flight
, ==, 0);
1493 /* Takes ownership of overlay, so we don't have to unref it later */
1494 bdrv_append(overlay
, base
, &error_abort
);
1495 g_assert_cmpint(base
->in_flight
, ==, 0);
1496 g_assert_cmpint(overlay
->in_flight
, ==, 0);
1498 g_assert_cmpint(base
->quiesce_counter
, ==, 1);
1499 g_assert_cmpint(base_s
->drain_count
, ==, 1);
1500 g_assert_cmpint(overlay
->quiesce_counter
, ==, 1);
1501 g_assert_cmpint(overlay_s
->drain_count
, ==, 1);
1503 do_drain_end(BDRV_DRAIN
, base
);
1505 g_assert_cmpint(base
->quiesce_counter
, ==, 0);
1506 g_assert_cmpint(base_s
->drain_count
, ==, 0);
1507 g_assert_cmpint(overlay
->quiesce_counter
, ==, 0);
1508 g_assert_cmpint(overlay_s
->drain_count
, ==, 0);
1514 static void test_set_aio_context(void)
1516 BlockDriverState
*bs
;
1517 IOThread
*a
= iothread_new();
1518 IOThread
*b
= iothread_new();
1519 AioContext
*ctx_a
= iothread_get_aio_context(a
);
1520 AioContext
*ctx_b
= iothread_get_aio_context(b
);
1522 bs
= bdrv_new_open_driver(&bdrv_test
, "test-node", BDRV_O_RDWR
,
1525 bdrv_drained_begin(bs
);
1526 bdrv_try_set_aio_context(bs
, ctx_a
, &error_abort
);
1528 aio_context_acquire(ctx_a
);
1529 bdrv_drained_end(bs
);
1531 bdrv_drained_begin(bs
);
1532 bdrv_try_set_aio_context(bs
, ctx_b
, &error_abort
);
1533 aio_context_release(ctx_a
);
1534 aio_context_acquire(ctx_b
);
1535 bdrv_try_set_aio_context(bs
, qemu_get_aio_context(), &error_abort
);
1536 aio_context_release(ctx_b
);
1537 bdrv_drained_end(bs
);
1545 typedef struct TestDropBackingBlockJob
{
1547 bool should_complete
;
1549 BlockDriverState
*detach_also
;
1550 } TestDropBackingBlockJob
;
1552 static int coroutine_fn
test_drop_backing_job_run(Job
*job
, Error
**errp
)
1554 TestDropBackingBlockJob
*s
=
1555 container_of(job
, TestDropBackingBlockJob
, common
.job
);
1557 while (!s
->should_complete
) {
1558 job_sleep_ns(job
, 0);
1564 static void test_drop_backing_job_commit(Job
*job
)
1566 TestDropBackingBlockJob
*s
=
1567 container_of(job
, TestDropBackingBlockJob
, common
.job
);
1569 bdrv_set_backing_hd(blk_bs(s
->common
.blk
), NULL
, &error_abort
);
1570 bdrv_set_backing_hd(s
->detach_also
, NULL
, &error_abort
);
1572 *s
->did_complete
= true;
1575 static const BlockJobDriver test_drop_backing_job_driver
= {
1577 .instance_size
= sizeof(TestDropBackingBlockJob
),
1578 .free
= block_job_free
,
1579 .user_resume
= block_job_user_resume
,
1580 .run
= test_drop_backing_job_run
,
1581 .commit
= test_drop_backing_job_commit
,
1586 * Creates a child node with three parent nodes on it, and then runs a
1587 * block job on the final one, parent-node-2.
1589 * The job is then asked to complete before a section where the child
1592 * Ending this section will undrain the child's parents, first
1593 * parent-node-2, then parent-node-1, then parent-node-0 -- the parent
1594 * list is in reverse order of how they were added. Ending the drain
1595 * on parent-node-2 will resume the job, thus completing it and
1596 * scheduling job_exit().
1598 * Ending the drain on parent-node-1 will poll the AioContext, which
1599 * lets job_exit() and thus test_drop_backing_job_commit() run. That
1600 * function first removes the child as parent-node-2's backing file.
1602 * In old (and buggy) implementations, there are two problems with
1604 * (A) bdrv_drain_invoke() polls for every node that leaves the
1605 * drained section. This means that job_exit() is scheduled
1606 * before the child has left the drained section. Its
1607 * quiesce_counter is therefore still 1 when it is removed from
1610 * (B) bdrv_replace_child_noperm() calls drained_end() on the old
1611 * child's parents as many times as the child is quiesced. This
1612 * means it will call drained_end() on parent-node-2 once.
1613 * Because parent-node-2 is no longer quiesced at this point, this
1616 * bdrv_replace_child_noperm() therefore must call drained_end() on
1617 * the parent only if it really is still drained because the child is
1620 * If removing child from parent-node-2 was successful (as it should
1621 * be), test_drop_backing_job_commit() will then also remove the child
1622 * from parent-node-0.
1624 * With an old version of our drain infrastructure ((A) above), that
1625 * resulted in the following flow:
1627 * 1. child attempts to leave its drained section. The call recurses
1630 * 2. parent-node-2 leaves the drained section. Polling in
1631 * bdrv_drain_invoke() will schedule job_exit().
1633 * 3. parent-node-1 leaves the drained section. Polling in
1634 * bdrv_drain_invoke() will run job_exit(), thus disconnecting
1635 * parent-node-0 from the child node.
1637 * 4. bdrv_parent_drained_end() uses a QLIST_FOREACH_SAFE() loop to
1638 * iterate over the parents. Thus, it now accesses the BdrvChild
1639 * object that used to connect parent-node-0 and the child node.
1640 * However, that object no longer exists, so it accesses a dangling
1643 * The solution is to only poll once when running a bdrv_drained_end()
1644 * operation, specifically at the end when all drained_end()
1645 * operations for all involved nodes have been scheduled.
1646 * Note that this also solves (A) above, thus hiding (B).
1648 static void test_blockjob_commit_by_drained_end(void)
1650 BlockDriverState
*bs_child
, *bs_parents
[3];
1651 TestDropBackingBlockJob
*job
;
1652 bool job_has_completed
= false;
1655 bs_child
= bdrv_new_open_driver(&bdrv_test
, "child-node", BDRV_O_RDWR
,
1658 for (i
= 0; i
< 3; i
++) {
1660 snprintf(name
, sizeof(name
), "parent-node-%i", i
);
1661 bs_parents
[i
] = bdrv_new_open_driver(&bdrv_test
, name
, BDRV_O_RDWR
,
1663 bdrv_set_backing_hd(bs_parents
[i
], bs_child
, &error_abort
);
1666 job
= block_job_create("job", &test_drop_backing_job_driver
, NULL
,
1667 bs_parents
[2], 0, BLK_PERM_ALL
, 0, 0, NULL
, NULL
,
1670 job
->detach_also
= bs_parents
[0];
1671 job
->did_complete
= &job_has_completed
;
1673 job_start(&job
->common
.job
);
1675 job
->should_complete
= true;
1676 bdrv_drained_begin(bs_child
);
1677 g_assert(!job_has_completed
);
1678 bdrv_drained_end(bs_child
);
1679 g_assert(job_has_completed
);
1681 bdrv_unref(bs_parents
[0]);
1682 bdrv_unref(bs_parents
[1]);
1683 bdrv_unref(bs_parents
[2]);
1684 bdrv_unref(bs_child
);
1688 typedef struct TestSimpleBlockJob
{
1690 bool should_complete
;
1692 } TestSimpleBlockJob
;
1694 static int coroutine_fn
test_simple_job_run(Job
*job
, Error
**errp
)
1696 TestSimpleBlockJob
*s
= container_of(job
, TestSimpleBlockJob
, common
.job
);
1698 while (!s
->should_complete
) {
1699 job_sleep_ns(job
, 0);
1705 static void test_simple_job_clean(Job
*job
)
1707 TestSimpleBlockJob
*s
= container_of(job
, TestSimpleBlockJob
, common
.job
);
1708 *s
->did_complete
= true;
1711 static const BlockJobDriver test_simple_job_driver
= {
1713 .instance_size
= sizeof(TestSimpleBlockJob
),
1714 .free
= block_job_free
,
1715 .user_resume
= block_job_user_resume
,
1716 .run
= test_simple_job_run
,
1717 .clean
= test_simple_job_clean
,
1721 static int drop_intermediate_poll_update_filename(BdrvChild
*child
,
1722 BlockDriverState
*new_base
,
1723 const char *filename
,
1727 * We are free to poll here, which may change the block graph, if
1728 * it is not drained.
1731 /* If the job is not drained: Complete it, schedule job_exit() */
1732 aio_poll(qemu_get_current_aio_context(), false);
1733 /* If the job is not drained: Run job_exit(), finish the job */
1734 aio_poll(qemu_get_current_aio_context(), false);
1740 * Test a poll in the midst of bdrv_drop_intermediate().
1742 * bdrv_drop_intermediate() calls BdrvChildClass.update_filename(),
1743 * which can yield or poll. This may lead to graph changes, unless
1744 * the whole subtree in question is drained.
1746 * We test this on the following graph:
1762 * node-2 --chain--> node-1 --chain--> node-0
1764 * We drop node-1 with bdrv_drop_intermediate(top=node-1, base=node-0).
1766 * This first updates node-2's backing filename by invoking
1767 * drop_intermediate_poll_update_filename(), which polls twice. This
1768 * causes the job to finish, which in turns causes the job-node to be
1771 * bdrv_drop_intermediate() uses a QLIST_FOREACH_SAFE() loop, so it
1772 * already has a pointer to the BdrvChild edge between job-node and
1773 * node-1. When it tries to handle that edge, we probably get a
1774 * segmentation fault because the object no longer exists.
1777 * The solution is for bdrv_drop_intermediate() to drain top's
1778 * subtree. This prevents graph changes from happening just because
1779 * BdrvChildClass.update_filename() yields or polls. Thus, the block
1780 * job is paused during that drained section and must finish before or
1783 * (In addition, bdrv_replace_child() must keep the job paused.)
1785 static void test_drop_intermediate_poll(void)
1787 static BdrvChildClass chain_child_class
;
1788 BlockDriverState
*chain
[3];
1789 TestSimpleBlockJob
*job
;
1790 BlockDriverState
*job_node
;
1791 bool job_has_completed
= false;
1795 chain_child_class
= child_backing
;
1796 chain_child_class
.update_filename
= drop_intermediate_poll_update_filename
;
1798 for (i
= 0; i
< 3; i
++) {
1800 snprintf(name
, 32, "node-%i", i
);
1802 chain
[i
] = bdrv_new_open_driver(&bdrv_test
, name
, 0, &error_abort
);
1805 job_node
= bdrv_new_open_driver(&bdrv_test
, "job-node", BDRV_O_RDWR
,
1807 bdrv_set_backing_hd(job_node
, chain
[1], &error_abort
);
1810 * Establish the chain last, so the chain links are the first
1811 * elements in the BDS.parents lists
1813 for (i
= 0; i
< 3; i
++) {
1815 /* Takes the reference to chain[i - 1] */
1816 chain
[i
]->backing
= bdrv_attach_child(chain
[i
], chain
[i
- 1],
1817 "chain", &chain_child_class
,
1822 job
= block_job_create("job", &test_simple_job_driver
, NULL
, job_node
,
1823 0, BLK_PERM_ALL
, 0, 0, NULL
, NULL
, &error_abort
);
1825 /* The job has a reference now */
1826 bdrv_unref(job_node
);
1828 job
->did_complete
= &job_has_completed
;
1830 job_start(&job
->common
.job
);
1831 job
->should_complete
= true;
1833 g_assert(!job_has_completed
);
1834 ret
= bdrv_drop_intermediate(chain
[1], chain
[0], NULL
);
1836 g_assert(job_has_completed
);
1838 bdrv_unref(chain
[2]);
1842 typedef struct BDRVReplaceTestState
{
1849 bool yield_before_read
;
1851 Coroutine
*drain_co
;
1852 } BDRVReplaceTestState
;
1854 static void bdrv_replace_test_close(BlockDriverState
*bs
)
1859 * If @bs has a backing file:
1860 * Yield if .yield_before_read is true (and wait for drain_begin to
1862 * Forward the read to bs->backing. Set .has_read to true.
1863 * If drain_begin has woken us, wake it in turn.
1866 * Set .has_read to true and return success.
1868 static int coroutine_fn
bdrv_replace_test_co_preadv(BlockDriverState
*bs
,
1874 BDRVReplaceTestState
*s
= bs
->opaque
;
1879 g_assert(!s
->drain_count
);
1881 s
->io_co
= qemu_coroutine_self();
1882 if (s
->yield_before_read
) {
1883 s
->yield_before_read
= false;
1884 qemu_coroutine_yield();
1888 ret
= bdrv_preadv(bs
->backing
, offset
, qiov
);
1891 /* Wake up drain_co if it runs */
1893 aio_co_wake(s
->drain_co
);
1904 * If .drain_count is 0, wake up .io_co if there is one; and set
1906 * Increment .drain_count.
1908 static void coroutine_fn
bdrv_replace_test_co_drain_begin(BlockDriverState
*bs
)
1910 BDRVReplaceTestState
*s
= bs
->opaque
;
1912 if (!s
->drain_count
) {
1913 /* Keep waking io_co up until it is done */
1914 s
->drain_co
= qemu_coroutine_self();
1916 aio_co_wake(s
->io_co
);
1918 qemu_coroutine_yield();
1922 s
->was_drained
= true;
1928 * Reduce .drain_count, set .was_undrained once it reaches 0.
1929 * If .drain_count reaches 0 and the node has a backing file, issue a
1932 static void coroutine_fn
bdrv_replace_test_co_drain_end(BlockDriverState
*bs
)
1934 BDRVReplaceTestState
*s
= bs
->opaque
;
1936 g_assert(s
->drain_count
> 0);
1937 if (!--s
->drain_count
) {
1940 s
->was_undrained
= true;
1944 QEMUIOVector qiov
= QEMU_IOVEC_INIT_BUF(qiov
, &data
, 1);
1946 /* Queue a read request post-drain */
1947 ret
= bdrv_replace_test_co_preadv(bs
, 0, 1, &qiov
, 0);
1953 static BlockDriver bdrv_replace_test
= {
1954 .format_name
= "replace_test",
1955 .instance_size
= sizeof(BDRVReplaceTestState
),
1957 .bdrv_close
= bdrv_replace_test_close
,
1958 .bdrv_co_preadv
= bdrv_replace_test_co_preadv
,
1960 .bdrv_co_drain_begin
= bdrv_replace_test_co_drain_begin
,
1961 .bdrv_co_drain_end
= bdrv_replace_test_co_drain_end
,
1963 .bdrv_child_perm
= bdrv_format_default_perms
,
1966 static void coroutine_fn
test_replace_child_mid_drain_read_co(void *opaque
)
1971 ret
= blk_co_pread(opaque
, 0, 1, &data
, 0);
1976 * We test two things:
1977 * (1) bdrv_replace_child_noperm() must not undrain the parent if both
1978 * children are drained.
1979 * (2) bdrv_replace_child_noperm() must never flush I/O requests to a
1980 * drained child. If the old child is drained, it must flush I/O
1981 * requests after the new one has been attached. If the new child
1982 * is drained, it must flush I/O requests before the old one is
1985 * To do so, we create one parent node and two child nodes; then
1986 * attach one of the children (old_child_bs) to the parent, then
1987 * drain both old_child_bs and new_child_bs according to
1988 * old_drain_count and new_drain_count, respectively, and finally
1989 * we invoke bdrv_replace_node() to replace old_child_bs by
1992 * The test block driver we use here (bdrv_replace_test) has a read
1994 * - For the parent node, can optionally yield, and then forwards the
1995 * read to bdrv_preadv(),
1996 * - For the child node, just returns immediately.
1998 * If the read yields, the drain_begin function will wake it up.
2000 * The drain_end function issues a read on the parent once it is fully
2001 * undrained (which simulates requests starting to come in again).
2003 static void do_test_replace_child_mid_drain(int old_drain_count
,
2004 int new_drain_count
)
2006 BlockBackend
*parent_blk
;
2007 BlockDriverState
*parent_bs
;
2008 BlockDriverState
*old_child_bs
, *new_child_bs
;
2009 BDRVReplaceTestState
*parent_s
;
2010 BDRVReplaceTestState
*old_child_s
, *new_child_s
;
2014 parent_bs
= bdrv_new_open_driver(&bdrv_replace_test
, "parent", 0,
2016 parent_s
= parent_bs
->opaque
;
2018 parent_blk
= blk_new(qemu_get_aio_context(),
2019 BLK_PERM_CONSISTENT_READ
, BLK_PERM_ALL
);
2020 blk_insert_bs(parent_blk
, parent_bs
, &error_abort
);
2022 old_child_bs
= bdrv_new_open_driver(&bdrv_replace_test
, "old-child", 0,
2024 new_child_bs
= bdrv_new_open_driver(&bdrv_replace_test
, "new-child", 0,
2026 old_child_s
= old_child_bs
->opaque
;
2027 new_child_s
= new_child_bs
->opaque
;
2029 /* So that we can read something */
2030 parent_bs
->total_sectors
= 1;
2031 old_child_bs
->total_sectors
= 1;
2032 new_child_bs
->total_sectors
= 1;
2034 bdrv_ref(old_child_bs
);
2035 parent_bs
->backing
= bdrv_attach_child(parent_bs
, old_child_bs
, "child",
2036 &child_backing
, 0, &error_abort
);
2038 for (i
= 0; i
< old_drain_count
; i
++) {
2039 bdrv_drained_begin(old_child_bs
);
2041 for (i
= 0; i
< new_drain_count
; i
++) {
2042 bdrv_drained_begin(new_child_bs
);
2045 if (!old_drain_count
) {
2047 * Start a read operation that will yield, so it will not
2048 * complete before the node is drained.
2050 parent_s
->yield_before_read
= true;
2051 io_co
= qemu_coroutine_create(test_replace_child_mid_drain_read_co
,
2053 qemu_coroutine_enter(io_co
);
2056 /* If we have started a read operation, it should have yielded */
2057 g_assert(!parent_s
->has_read
);
2059 /* Reset drained status so we can see what bdrv_replace_node() does */
2060 parent_s
->was_drained
= false;
2061 parent_s
->was_undrained
= false;
2063 g_assert(parent_bs
->quiesce_counter
== old_drain_count
);
2064 bdrv_replace_node(old_child_bs
, new_child_bs
, &error_abort
);
2065 g_assert(parent_bs
->quiesce_counter
== new_drain_count
);
2067 if (!old_drain_count
&& !new_drain_count
) {
2069 * From undrained to undrained drains and undrains the parent,
2070 * because bdrv_replace_node() contains a drained section for
2073 g_assert(parent_s
->was_drained
&& parent_s
->was_undrained
);
2074 } else if (!old_drain_count
&& new_drain_count
) {
2076 * From undrained to drained should drain the parent and keep
2079 g_assert(parent_s
->was_drained
&& !parent_s
->was_undrained
);
2080 } else if (old_drain_count
&& !new_drain_count
) {
2082 * From drained to undrained should undrain the parent and
2085 g_assert(!parent_s
->was_drained
&& parent_s
->was_undrained
);
2086 } else /* if (old_drain_count && new_drain_count) */ {
2088 * From drained to drained must not undrain the parent at any
2091 g_assert(!parent_s
->was_drained
&& !parent_s
->was_undrained
);
2094 if (!old_drain_count
|| !new_drain_count
) {
2096 * If !old_drain_count, we have started a read request before
2097 * bdrv_replace_node(). If !new_drain_count, the parent must
2098 * have been undrained at some point, and
2099 * bdrv_replace_test_co_drain_end() starts a read request
2102 g_assert(parent_s
->has_read
);
2105 * If the parent was never undrained, there is no way to start
2108 g_assert(!parent_s
->has_read
);
2111 /* A drained child must have not received any request */
2112 g_assert(!(old_drain_count
&& old_child_s
->has_read
));
2113 g_assert(!(new_drain_count
&& new_child_s
->has_read
));
2115 for (i
= 0; i
< new_drain_count
; i
++) {
2116 bdrv_drained_end(new_child_bs
);
2118 for (i
= 0; i
< old_drain_count
; i
++) {
2119 bdrv_drained_end(old_child_bs
);
2123 * By now, bdrv_replace_test_co_drain_end() must have been called
2124 * at some point while the new child was attached to the parent.
2126 g_assert(parent_s
->has_read
);
2127 g_assert(new_child_s
->has_read
);
2129 blk_unref(parent_blk
);
2130 bdrv_unref(parent_bs
);
2131 bdrv_unref(old_child_bs
);
2132 bdrv_unref(new_child_bs
);
2135 static void test_replace_child_mid_drain(void)
2137 int old_drain_count
, new_drain_count
;
2139 for (old_drain_count
= 0; old_drain_count
< 2; old_drain_count
++) {
2140 for (new_drain_count
= 0; new_drain_count
< 2; new_drain_count
++) {
2141 do_test_replace_child_mid_drain(old_drain_count
, new_drain_count
);
2146 int main(int argc
, char **argv
)
2151 qemu_init_main_loop(&error_abort
);
2153 g_test_init(&argc
, &argv
, NULL
);
2154 qemu_event_init(&done_event
, false);
2156 g_test_add_func("/bdrv-drain/driver-cb/drain_all", test_drv_cb_drain_all
);
2157 g_test_add_func("/bdrv-drain/driver-cb/drain", test_drv_cb_drain
);
2158 g_test_add_func("/bdrv-drain/driver-cb/drain_subtree",
2159 test_drv_cb_drain_subtree
);
2161 g_test_add_func("/bdrv-drain/driver-cb/co/drain_all",
2162 test_drv_cb_co_drain_all
);
2163 g_test_add_func("/bdrv-drain/driver-cb/co/drain", test_drv_cb_co_drain
);
2164 g_test_add_func("/bdrv-drain/driver-cb/co/drain_subtree",
2165 test_drv_cb_co_drain_subtree
);
2168 g_test_add_func("/bdrv-drain/quiesce/drain_all", test_quiesce_drain_all
);
2169 g_test_add_func("/bdrv-drain/quiesce/drain", test_quiesce_drain
);
2170 g_test_add_func("/bdrv-drain/quiesce/drain_subtree",
2171 test_quiesce_drain_subtree
);
2173 g_test_add_func("/bdrv-drain/quiesce/co/drain_all",
2174 test_quiesce_co_drain_all
);
2175 g_test_add_func("/bdrv-drain/quiesce/co/drain", test_quiesce_co_drain
);
2176 g_test_add_func("/bdrv-drain/quiesce/co/drain_subtree",
2177 test_quiesce_co_drain_subtree
);
2179 g_test_add_func("/bdrv-drain/nested", test_nested
);
2180 g_test_add_func("/bdrv-drain/multiparent", test_multiparent
);
2182 g_test_add_func("/bdrv-drain/graph-change/drain_subtree",
2183 test_graph_change_drain_subtree
);
2184 g_test_add_func("/bdrv-drain/graph-change/drain_all",
2185 test_graph_change_drain_all
);
2187 g_test_add_func("/bdrv-drain/iothread/drain_all", test_iothread_drain_all
);
2188 g_test_add_func("/bdrv-drain/iothread/drain", test_iothread_drain
);
2189 g_test_add_func("/bdrv-drain/iothread/drain_subtree",
2190 test_iothread_drain_subtree
);
2192 g_test_add_func("/bdrv-drain/blockjob/drain_all", test_blockjob_drain_all
);
2193 g_test_add_func("/bdrv-drain/blockjob/drain", test_blockjob_drain
);
2194 g_test_add_func("/bdrv-drain/blockjob/drain_subtree",
2195 test_blockjob_drain_subtree
);
2197 g_test_add_func("/bdrv-drain/blockjob/error/drain_all",
2198 test_blockjob_error_drain_all
);
2199 g_test_add_func("/bdrv-drain/blockjob/error/drain",
2200 test_blockjob_error_drain
);
2201 g_test_add_func("/bdrv-drain/blockjob/error/drain_subtree",
2202 test_blockjob_error_drain_subtree
);
2204 g_test_add_func("/bdrv-drain/blockjob/iothread/drain_all",
2205 test_blockjob_iothread_drain_all
);
2206 g_test_add_func("/bdrv-drain/blockjob/iothread/drain",
2207 test_blockjob_iothread_drain
);
2208 g_test_add_func("/bdrv-drain/blockjob/iothread/drain_subtree",
2209 test_blockjob_iothread_drain_subtree
);
2211 g_test_add_func("/bdrv-drain/blockjob/iothread/error/drain_all",
2212 test_blockjob_iothread_error_drain_all
);
2213 g_test_add_func("/bdrv-drain/blockjob/iothread/error/drain",
2214 test_blockjob_iothread_error_drain
);
2215 g_test_add_func("/bdrv-drain/blockjob/iothread/error/drain_subtree",
2216 test_blockjob_iothread_error_drain_subtree
);
2218 g_test_add_func("/bdrv-drain/deletion/drain", test_delete_by_drain
);
2219 g_test_add_func("/bdrv-drain/detach/drain_all", test_detach_by_drain_all
);
2220 g_test_add_func("/bdrv-drain/detach/drain", test_detach_by_drain
);
2221 g_test_add_func("/bdrv-drain/detach/drain_subtree", test_detach_by_drain_subtree
);
2222 g_test_add_func("/bdrv-drain/detach/parent_cb", test_detach_by_parent_cb
);
2223 g_test_add_func("/bdrv-drain/detach/driver_cb", test_detach_by_driver_cb
);
2225 g_test_add_func("/bdrv-drain/attach/drain", test_append_to_drained
);
2227 g_test_add_func("/bdrv-drain/set_aio_context", test_set_aio_context
);
2229 g_test_add_func("/bdrv-drain/blockjob/commit_by_drained_end",
2230 test_blockjob_commit_by_drained_end
);
2232 g_test_add_func("/bdrv-drain/bdrv_drop_intermediate/poll",
2233 test_drop_intermediate_poll
);
2235 g_test_add_func("/bdrv-drain/replace_child/mid-drain",
2236 test_replace_child_mid_drain
);
2239 qemu_event_destroy(&done_event
);