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 int64_t offset
, int64_t bytes
,
70 BdrvRequestFlags flags
)
72 BDRVTestState
*s
= bs
->opaque
;
74 /* We want this request to stay until the polling loop in drain waits for
75 * it to complete. We need to sleep a while as bdrv_drain_invoke() comes
76 * first and polls its result, too, but it shouldn't accidentally complete
77 * this request yet. */
78 qemu_co_sleep_ns(QEMU_CLOCK_REALTIME
, 100000);
80 if (s
->bh_indirection_ctx
) {
81 aio_bh_schedule_oneshot(s
->bh_indirection_ctx
, co_reenter_bh
,
82 qemu_coroutine_self());
83 qemu_coroutine_yield();
89 static int bdrv_test_change_backing_file(BlockDriverState
*bs
,
90 const char *backing_file
,
91 const char *backing_fmt
)
96 static BlockDriver bdrv_test
= {
97 .format_name
= "test",
98 .instance_size
= sizeof(BDRVTestState
),
99 .supports_backing
= true,
101 .bdrv_close
= bdrv_test_close
,
102 .bdrv_co_preadv
= bdrv_test_co_preadv
,
104 .bdrv_co_drain_begin
= bdrv_test_co_drain_begin
,
105 .bdrv_co_drain_end
= bdrv_test_co_drain_end
,
107 .bdrv_child_perm
= bdrv_default_perms
,
109 .bdrv_change_backing_file
= bdrv_test_change_backing_file
,
112 static void aio_ret_cb(void *opaque
, int ret
)
114 int *aio_ret
= opaque
;
118 typedef struct CallInCoroutineData
{
121 } CallInCoroutineData
;
123 static coroutine_fn
void call_in_coroutine_entry(void *opaque
)
125 CallInCoroutineData
*data
= opaque
;
131 static void call_in_coroutine(void (*entry
)(void))
134 CallInCoroutineData data
= {
139 co
= qemu_coroutine_create(call_in_coroutine_entry
, &data
);
140 qemu_coroutine_enter(co
);
142 aio_poll(qemu_get_aio_context(), true);
153 static void do_drain_begin(enum drain_type drain_type
, BlockDriverState
*bs
)
155 switch (drain_type
) {
156 case BDRV_DRAIN_ALL
: bdrv_drain_all_begin(); break;
157 case BDRV_DRAIN
: bdrv_drained_begin(bs
); break;
158 case BDRV_SUBTREE_DRAIN
: bdrv_subtree_drained_begin(bs
); break;
159 default: g_assert_not_reached();
163 static void do_drain_end(enum drain_type drain_type
, BlockDriverState
*bs
)
165 switch (drain_type
) {
166 case BDRV_DRAIN_ALL
: bdrv_drain_all_end(); break;
167 case BDRV_DRAIN
: bdrv_drained_end(bs
); break;
168 case BDRV_SUBTREE_DRAIN
: bdrv_subtree_drained_end(bs
); break;
169 default: g_assert_not_reached();
173 static void do_drain_begin_unlocked(enum drain_type drain_type
, BlockDriverState
*bs
)
175 if (drain_type
!= BDRV_DRAIN_ALL
) {
176 aio_context_acquire(bdrv_get_aio_context(bs
));
178 do_drain_begin(drain_type
, bs
);
179 if (drain_type
!= BDRV_DRAIN_ALL
) {
180 aio_context_release(bdrv_get_aio_context(bs
));
184 static void do_drain_end_unlocked(enum drain_type drain_type
, BlockDriverState
*bs
)
186 if (drain_type
!= BDRV_DRAIN_ALL
) {
187 aio_context_acquire(bdrv_get_aio_context(bs
));
189 do_drain_end(drain_type
, bs
);
190 if (drain_type
!= BDRV_DRAIN_ALL
) {
191 aio_context_release(bdrv_get_aio_context(bs
));
195 static void test_drv_cb_common(enum drain_type drain_type
, bool recursive
)
198 BlockDriverState
*bs
, *backing
;
199 BDRVTestState
*s
, *backing_s
;
203 QEMUIOVector qiov
= QEMU_IOVEC_INIT_BUF(qiov
, NULL
, 0);
205 blk
= blk_new(qemu_get_aio_context(), BLK_PERM_ALL
, BLK_PERM_ALL
);
206 bs
= bdrv_new_open_driver(&bdrv_test
, "test-node", BDRV_O_RDWR
,
209 blk_insert_bs(blk
, bs
, &error_abort
);
211 backing
= bdrv_new_open_driver(&bdrv_test
, "backing", 0, &error_abort
);
212 backing_s
= backing
->opaque
;
213 bdrv_set_backing_hd(bs
, backing
, &error_abort
);
215 /* Simple bdrv_drain_all_begin/end pair, check that CBs are called */
216 g_assert_cmpint(s
->drain_count
, ==, 0);
217 g_assert_cmpint(backing_s
->drain_count
, ==, 0);
219 do_drain_begin(drain_type
, bs
);
221 g_assert_cmpint(s
->drain_count
, ==, 1);
222 g_assert_cmpint(backing_s
->drain_count
, ==, !!recursive
);
224 do_drain_end(drain_type
, bs
);
226 g_assert_cmpint(s
->drain_count
, ==, 0);
227 g_assert_cmpint(backing_s
->drain_count
, ==, 0);
229 /* Now do the same while a request is pending */
230 aio_ret
= -EINPROGRESS
;
231 acb
= blk_aio_preadv(blk
, 0, &qiov
, 0, aio_ret_cb
, &aio_ret
);
232 g_assert(acb
!= NULL
);
233 g_assert_cmpint(aio_ret
, ==, -EINPROGRESS
);
235 g_assert_cmpint(s
->drain_count
, ==, 0);
236 g_assert_cmpint(backing_s
->drain_count
, ==, 0);
238 do_drain_begin(drain_type
, bs
);
240 g_assert_cmpint(aio_ret
, ==, 0);
241 g_assert_cmpint(s
->drain_count
, ==, 1);
242 g_assert_cmpint(backing_s
->drain_count
, ==, !!recursive
);
244 do_drain_end(drain_type
, bs
);
246 g_assert_cmpint(s
->drain_count
, ==, 0);
247 g_assert_cmpint(backing_s
->drain_count
, ==, 0);
254 static void test_drv_cb_drain_all(void)
256 test_drv_cb_common(BDRV_DRAIN_ALL
, true);
259 static void test_drv_cb_drain(void)
261 test_drv_cb_common(BDRV_DRAIN
, false);
264 static void test_drv_cb_drain_subtree(void)
266 test_drv_cb_common(BDRV_SUBTREE_DRAIN
, true);
269 static void test_drv_cb_co_drain_all(void)
271 call_in_coroutine(test_drv_cb_drain_all
);
274 static void test_drv_cb_co_drain(void)
276 call_in_coroutine(test_drv_cb_drain
);
279 static void test_drv_cb_co_drain_subtree(void)
281 call_in_coroutine(test_drv_cb_drain_subtree
);
284 static void test_quiesce_common(enum drain_type drain_type
, bool recursive
)
287 BlockDriverState
*bs
, *backing
;
289 blk
= blk_new(qemu_get_aio_context(), BLK_PERM_ALL
, BLK_PERM_ALL
);
290 bs
= bdrv_new_open_driver(&bdrv_test
, "test-node", BDRV_O_RDWR
,
292 blk_insert_bs(blk
, bs
, &error_abort
);
294 backing
= bdrv_new_open_driver(&bdrv_test
, "backing", 0, &error_abort
);
295 bdrv_set_backing_hd(bs
, backing
, &error_abort
);
297 g_assert_cmpint(bs
->quiesce_counter
, ==, 0);
298 g_assert_cmpint(backing
->quiesce_counter
, ==, 0);
300 do_drain_begin(drain_type
, bs
);
302 g_assert_cmpint(bs
->quiesce_counter
, ==, 1);
303 g_assert_cmpint(backing
->quiesce_counter
, ==, !!recursive
);
305 do_drain_end(drain_type
, bs
);
307 g_assert_cmpint(bs
->quiesce_counter
, ==, 0);
308 g_assert_cmpint(backing
->quiesce_counter
, ==, 0);
315 static void test_quiesce_drain_all(void)
317 test_quiesce_common(BDRV_DRAIN_ALL
, true);
320 static void test_quiesce_drain(void)
322 test_quiesce_common(BDRV_DRAIN
, false);
325 static void test_quiesce_drain_subtree(void)
327 test_quiesce_common(BDRV_SUBTREE_DRAIN
, true);
330 static void test_quiesce_co_drain_all(void)
332 call_in_coroutine(test_quiesce_drain_all
);
335 static void test_quiesce_co_drain(void)
337 call_in_coroutine(test_quiesce_drain
);
340 static void test_quiesce_co_drain_subtree(void)
342 call_in_coroutine(test_quiesce_drain_subtree
);
345 static void test_nested(void)
348 BlockDriverState
*bs
, *backing
;
349 BDRVTestState
*s
, *backing_s
;
350 enum drain_type outer
, inner
;
352 blk
= blk_new(qemu_get_aio_context(), BLK_PERM_ALL
, BLK_PERM_ALL
);
353 bs
= bdrv_new_open_driver(&bdrv_test
, "test-node", BDRV_O_RDWR
,
356 blk_insert_bs(blk
, bs
, &error_abort
);
358 backing
= bdrv_new_open_driver(&bdrv_test
, "backing", 0, &error_abort
);
359 backing_s
= backing
->opaque
;
360 bdrv_set_backing_hd(bs
, backing
, &error_abort
);
362 for (outer
= 0; outer
< DRAIN_TYPE_MAX
; outer
++) {
363 for (inner
= 0; inner
< DRAIN_TYPE_MAX
; inner
++) {
364 int backing_quiesce
= (outer
!= BDRV_DRAIN
) +
365 (inner
!= BDRV_DRAIN
);
367 g_assert_cmpint(bs
->quiesce_counter
, ==, 0);
368 g_assert_cmpint(backing
->quiesce_counter
, ==, 0);
369 g_assert_cmpint(s
->drain_count
, ==, 0);
370 g_assert_cmpint(backing_s
->drain_count
, ==, 0);
372 do_drain_begin(outer
, bs
);
373 do_drain_begin(inner
, bs
);
375 g_assert_cmpint(bs
->quiesce_counter
, ==, 2);
376 g_assert_cmpint(backing
->quiesce_counter
, ==, backing_quiesce
);
377 g_assert_cmpint(s
->drain_count
, ==, 2);
378 g_assert_cmpint(backing_s
->drain_count
, ==, backing_quiesce
);
380 do_drain_end(inner
, bs
);
381 do_drain_end(outer
, bs
);
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);
395 static void test_multiparent(void)
397 BlockBackend
*blk_a
, *blk_b
;
398 BlockDriverState
*bs_a
, *bs_b
, *backing
;
399 BDRVTestState
*a_s
, *b_s
, *backing_s
;
401 blk_a
= blk_new(qemu_get_aio_context(), BLK_PERM_ALL
, BLK_PERM_ALL
);
402 bs_a
= bdrv_new_open_driver(&bdrv_test
, "test-node-a", BDRV_O_RDWR
,
405 blk_insert_bs(blk_a
, bs_a
, &error_abort
);
407 blk_b
= blk_new(qemu_get_aio_context(), BLK_PERM_ALL
, BLK_PERM_ALL
);
408 bs_b
= bdrv_new_open_driver(&bdrv_test
, "test-node-b", BDRV_O_RDWR
,
411 blk_insert_bs(blk_b
, bs_b
, &error_abort
);
413 backing
= bdrv_new_open_driver(&bdrv_test
, "backing", 0, &error_abort
);
414 backing_s
= backing
->opaque
;
415 bdrv_set_backing_hd(bs_a
, backing
, &error_abort
);
416 bdrv_set_backing_hd(bs_b
, backing
, &error_abort
);
418 g_assert_cmpint(bs_a
->quiesce_counter
, ==, 0);
419 g_assert_cmpint(bs_b
->quiesce_counter
, ==, 0);
420 g_assert_cmpint(backing
->quiesce_counter
, ==, 0);
421 g_assert_cmpint(a_s
->drain_count
, ==, 0);
422 g_assert_cmpint(b_s
->drain_count
, ==, 0);
423 g_assert_cmpint(backing_s
->drain_count
, ==, 0);
425 do_drain_begin(BDRV_SUBTREE_DRAIN
, bs_a
);
427 g_assert_cmpint(bs_a
->quiesce_counter
, ==, 1);
428 g_assert_cmpint(bs_b
->quiesce_counter
, ==, 1);
429 g_assert_cmpint(backing
->quiesce_counter
, ==, 1);
430 g_assert_cmpint(a_s
->drain_count
, ==, 1);
431 g_assert_cmpint(b_s
->drain_count
, ==, 1);
432 g_assert_cmpint(backing_s
->drain_count
, ==, 1);
434 do_drain_begin(BDRV_SUBTREE_DRAIN
, bs_b
);
436 g_assert_cmpint(bs_a
->quiesce_counter
, ==, 2);
437 g_assert_cmpint(bs_b
->quiesce_counter
, ==, 2);
438 g_assert_cmpint(backing
->quiesce_counter
, ==, 2);
439 g_assert_cmpint(a_s
->drain_count
, ==, 2);
440 g_assert_cmpint(b_s
->drain_count
, ==, 2);
441 g_assert_cmpint(backing_s
->drain_count
, ==, 2);
443 do_drain_end(BDRV_SUBTREE_DRAIN
, bs_b
);
445 g_assert_cmpint(bs_a
->quiesce_counter
, ==, 1);
446 g_assert_cmpint(bs_b
->quiesce_counter
, ==, 1);
447 g_assert_cmpint(backing
->quiesce_counter
, ==, 1);
448 g_assert_cmpint(a_s
->drain_count
, ==, 1);
449 g_assert_cmpint(b_s
->drain_count
, ==, 1);
450 g_assert_cmpint(backing_s
->drain_count
, ==, 1);
452 do_drain_end(BDRV_SUBTREE_DRAIN
, bs_a
);
454 g_assert_cmpint(bs_a
->quiesce_counter
, ==, 0);
455 g_assert_cmpint(bs_b
->quiesce_counter
, ==, 0);
456 g_assert_cmpint(backing
->quiesce_counter
, ==, 0);
457 g_assert_cmpint(a_s
->drain_count
, ==, 0);
458 g_assert_cmpint(b_s
->drain_count
, ==, 0);
459 g_assert_cmpint(backing_s
->drain_count
, ==, 0);
468 static void test_graph_change_drain_subtree(void)
470 BlockBackend
*blk_a
, *blk_b
;
471 BlockDriverState
*bs_a
, *bs_b
, *backing
;
472 BDRVTestState
*a_s
, *b_s
, *backing_s
;
474 blk_a
= blk_new(qemu_get_aio_context(), BLK_PERM_ALL
, BLK_PERM_ALL
);
475 bs_a
= bdrv_new_open_driver(&bdrv_test
, "test-node-a", BDRV_O_RDWR
,
478 blk_insert_bs(blk_a
, bs_a
, &error_abort
);
480 blk_b
= blk_new(qemu_get_aio_context(), BLK_PERM_ALL
, BLK_PERM_ALL
);
481 bs_b
= bdrv_new_open_driver(&bdrv_test
, "test-node-b", BDRV_O_RDWR
,
484 blk_insert_bs(blk_b
, bs_b
, &error_abort
);
486 backing
= bdrv_new_open_driver(&bdrv_test
, "backing", 0, &error_abort
);
487 backing_s
= backing
->opaque
;
488 bdrv_set_backing_hd(bs_a
, backing
, &error_abort
);
490 g_assert_cmpint(bs_a
->quiesce_counter
, ==, 0);
491 g_assert_cmpint(bs_b
->quiesce_counter
, ==, 0);
492 g_assert_cmpint(backing
->quiesce_counter
, ==, 0);
493 g_assert_cmpint(a_s
->drain_count
, ==, 0);
494 g_assert_cmpint(b_s
->drain_count
, ==, 0);
495 g_assert_cmpint(backing_s
->drain_count
, ==, 0);
497 do_drain_begin(BDRV_SUBTREE_DRAIN
, bs_a
);
498 do_drain_begin(BDRV_SUBTREE_DRAIN
, bs_a
);
499 do_drain_begin(BDRV_SUBTREE_DRAIN
, bs_a
);
500 do_drain_begin(BDRV_SUBTREE_DRAIN
, bs_b
);
501 do_drain_begin(BDRV_SUBTREE_DRAIN
, bs_b
);
503 bdrv_set_backing_hd(bs_b
, backing
, &error_abort
);
504 g_assert_cmpint(bs_a
->quiesce_counter
, ==, 5);
505 g_assert_cmpint(bs_b
->quiesce_counter
, ==, 5);
506 g_assert_cmpint(backing
->quiesce_counter
, ==, 5);
507 g_assert_cmpint(a_s
->drain_count
, ==, 5);
508 g_assert_cmpint(b_s
->drain_count
, ==, 5);
509 g_assert_cmpint(backing_s
->drain_count
, ==, 5);
511 bdrv_set_backing_hd(bs_b
, NULL
, &error_abort
);
512 g_assert_cmpint(bs_a
->quiesce_counter
, ==, 3);
513 g_assert_cmpint(bs_b
->quiesce_counter
, ==, 2);
514 g_assert_cmpint(backing
->quiesce_counter
, ==, 3);
515 g_assert_cmpint(a_s
->drain_count
, ==, 3);
516 g_assert_cmpint(b_s
->drain_count
, ==, 2);
517 g_assert_cmpint(backing_s
->drain_count
, ==, 3);
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 do_drain_end(BDRV_SUBTREE_DRAIN
, bs_b
);
528 do_drain_end(BDRV_SUBTREE_DRAIN
, bs_b
);
529 do_drain_end(BDRV_SUBTREE_DRAIN
, bs_a
);
530 do_drain_end(BDRV_SUBTREE_DRAIN
, bs_a
);
531 do_drain_end(BDRV_SUBTREE_DRAIN
, bs_a
);
533 g_assert_cmpint(bs_a
->quiesce_counter
, ==, 0);
534 g_assert_cmpint(bs_b
->quiesce_counter
, ==, 0);
535 g_assert_cmpint(backing
->quiesce_counter
, ==, 0);
536 g_assert_cmpint(a_s
->drain_count
, ==, 0);
537 g_assert_cmpint(b_s
->drain_count
, ==, 0);
538 g_assert_cmpint(backing_s
->drain_count
, ==, 0);
547 static void test_graph_change_drain_all(void)
549 BlockBackend
*blk_a
, *blk_b
;
550 BlockDriverState
*bs_a
, *bs_b
;
551 BDRVTestState
*a_s
, *b_s
;
553 /* Create node A with a BlockBackend */
554 blk_a
= blk_new(qemu_get_aio_context(), BLK_PERM_ALL
, BLK_PERM_ALL
);
555 bs_a
= bdrv_new_open_driver(&bdrv_test
, "test-node-a", BDRV_O_RDWR
,
558 blk_insert_bs(blk_a
, bs_a
, &error_abort
);
560 g_assert_cmpint(bs_a
->quiesce_counter
, ==, 0);
561 g_assert_cmpint(a_s
->drain_count
, ==, 0);
563 /* Call bdrv_drain_all_begin() */
564 bdrv_drain_all_begin();
566 g_assert_cmpint(bs_a
->quiesce_counter
, ==, 1);
567 g_assert_cmpint(a_s
->drain_count
, ==, 1);
569 /* Create node B with a BlockBackend */
570 blk_b
= blk_new(qemu_get_aio_context(), BLK_PERM_ALL
, BLK_PERM_ALL
);
571 bs_b
= bdrv_new_open_driver(&bdrv_test
, "test-node-b", BDRV_O_RDWR
,
574 blk_insert_bs(blk_b
, bs_b
, &error_abort
);
576 g_assert_cmpint(bs_a
->quiesce_counter
, ==, 1);
577 g_assert_cmpint(bs_b
->quiesce_counter
, ==, 1);
578 g_assert_cmpint(a_s
->drain_count
, ==, 1);
579 g_assert_cmpint(b_s
->drain_count
, ==, 1);
581 /* Unref and finally delete node A */
584 g_assert_cmpint(bs_a
->quiesce_counter
, ==, 1);
585 g_assert_cmpint(bs_b
->quiesce_counter
, ==, 1);
586 g_assert_cmpint(a_s
->drain_count
, ==, 1);
587 g_assert_cmpint(b_s
->drain_count
, ==, 1);
591 g_assert_cmpint(bs_b
->quiesce_counter
, ==, 1);
592 g_assert_cmpint(b_s
->drain_count
, ==, 1);
594 /* End the drained section */
595 bdrv_drain_all_end();
597 g_assert_cmpint(bs_b
->quiesce_counter
, ==, 0);
598 g_assert_cmpint(b_s
->drain_count
, ==, 0);
599 g_assert_cmpint(qemu_get_aio_context()->external_disable_cnt
, ==, 0);
605 struct test_iothread_data
{
606 BlockDriverState
*bs
;
607 enum drain_type drain_type
;
611 static void test_iothread_drain_entry(void *opaque
)
613 struct test_iothread_data
*data
= opaque
;
615 aio_context_acquire(bdrv_get_aio_context(data
->bs
));
616 do_drain_begin(data
->drain_type
, data
->bs
);
617 g_assert_cmpint(*data
->aio_ret
, ==, 0);
618 do_drain_end(data
->drain_type
, data
->bs
);
619 aio_context_release(bdrv_get_aio_context(data
->bs
));
621 qemu_event_set(&done_event
);
624 static void test_iothread_aio_cb(void *opaque
, int ret
)
626 int *aio_ret
= opaque
;
628 qemu_event_set(&done_event
);
631 static void test_iothread_main_thread_bh(void *opaque
)
633 struct test_iothread_data
*data
= opaque
;
635 /* Test that the AioContext is not yet locked in a random BH that is
636 * executed during drain, otherwise this would deadlock. */
637 aio_context_acquire(bdrv_get_aio_context(data
->bs
));
638 bdrv_flush(data
->bs
);
639 aio_context_release(bdrv_get_aio_context(data
->bs
));
643 * Starts an AIO request on a BDS that runs in the AioContext of iothread 1.
644 * The request involves a BH on iothread 2 before it can complete.
646 * @drain_thread = 0 means that do_drain_begin/end are called from the main
647 * thread, @drain_thread = 1 means that they are called from iothread 1. Drain
648 * for this BDS cannot be called from iothread 2 because only the main thread
649 * may do cross-AioContext polling.
651 static void test_iothread_common(enum drain_type drain_type
, int drain_thread
)
654 BlockDriverState
*bs
;
658 struct test_iothread_data data
;
660 IOThread
*a
= iothread_new();
661 IOThread
*b
= iothread_new();
662 AioContext
*ctx_a
= iothread_get_aio_context(a
);
663 AioContext
*ctx_b
= iothread_get_aio_context(b
);
665 QEMUIOVector qiov
= QEMU_IOVEC_INIT_BUF(qiov
, NULL
, 0);
667 /* bdrv_drain_all() may only be called from the main loop thread */
668 if (drain_type
== BDRV_DRAIN_ALL
&& drain_thread
!= 0) {
672 blk
= blk_new(qemu_get_aio_context(), BLK_PERM_ALL
, BLK_PERM_ALL
);
673 bs
= bdrv_new_open_driver(&bdrv_test
, "test-node", BDRV_O_RDWR
,
676 blk_insert_bs(blk
, bs
, &error_abort
);
677 blk_set_disable_request_queuing(blk
, true);
679 blk_set_aio_context(blk
, ctx_a
, &error_abort
);
680 aio_context_acquire(ctx_a
);
682 s
->bh_indirection_ctx
= ctx_b
;
684 aio_ret
= -EINPROGRESS
;
685 qemu_event_reset(&done_event
);
687 if (drain_thread
== 0) {
688 acb
= blk_aio_preadv(blk
, 0, &qiov
, 0, test_iothread_aio_cb
, &aio_ret
);
690 acb
= blk_aio_preadv(blk
, 0, &qiov
, 0, aio_ret_cb
, &aio_ret
);
692 g_assert(acb
!= NULL
);
693 g_assert_cmpint(aio_ret
, ==, -EINPROGRESS
);
695 aio_context_release(ctx_a
);
697 data
= (struct test_iothread_data
) {
699 .drain_type
= drain_type
,
703 switch (drain_thread
) {
705 if (drain_type
!= BDRV_DRAIN_ALL
) {
706 aio_context_acquire(ctx_a
);
709 aio_bh_schedule_oneshot(ctx_a
, test_iothread_main_thread_bh
, &data
);
711 /* The request is running on the IOThread a. Draining its block device
712 * will make sure that it has completed as far as the BDS is concerned,
713 * but the drain in this thread can continue immediately after
714 * bdrv_dec_in_flight() and aio_ret might be assigned only slightly
716 do_drain_begin(drain_type
, bs
);
717 g_assert_cmpint(bs
->in_flight
, ==, 0);
719 if (drain_type
!= BDRV_DRAIN_ALL
) {
720 aio_context_release(ctx_a
);
722 qemu_event_wait(&done_event
);
723 if (drain_type
!= BDRV_DRAIN_ALL
) {
724 aio_context_acquire(ctx_a
);
727 g_assert_cmpint(aio_ret
, ==, 0);
728 do_drain_end(drain_type
, bs
);
730 if (drain_type
!= BDRV_DRAIN_ALL
) {
731 aio_context_release(ctx_a
);
735 aio_bh_schedule_oneshot(ctx_a
, test_iothread_drain_entry
, &data
);
736 qemu_event_wait(&done_event
);
739 g_assert_not_reached();
742 aio_context_acquire(ctx_a
);
743 blk_set_aio_context(blk
, qemu_get_aio_context(), &error_abort
);
744 aio_context_release(ctx_a
);
754 static void test_iothread_drain_all(void)
756 test_iothread_common(BDRV_DRAIN_ALL
, 0);
757 test_iothread_common(BDRV_DRAIN_ALL
, 1);
760 static void test_iothread_drain(void)
762 test_iothread_common(BDRV_DRAIN
, 0);
763 test_iothread_common(BDRV_DRAIN
, 1);
766 static void test_iothread_drain_subtree(void)
768 test_iothread_common(BDRV_SUBTREE_DRAIN
, 0);
769 test_iothread_common(BDRV_SUBTREE_DRAIN
, 1);
773 typedef struct TestBlockJob
{
775 BlockDriverState
*bs
;
779 bool should_complete
;
782 static int test_job_prepare(Job
*job
)
784 TestBlockJob
*s
= container_of(job
, TestBlockJob
, common
.job
);
786 /* Provoke an AIO_WAIT_WHILE() call to verify there is no deadlock */
788 return s
->prepare_ret
;
791 static void test_job_commit(Job
*job
)
793 TestBlockJob
*s
= container_of(job
, TestBlockJob
, common
.job
);
795 /* Provoke an AIO_WAIT_WHILE() call to verify there is no deadlock */
799 static void test_job_abort(Job
*job
)
801 TestBlockJob
*s
= container_of(job
, TestBlockJob
, common
.job
);
803 /* Provoke an AIO_WAIT_WHILE() call to verify there is no deadlock */
807 static int coroutine_fn
test_job_run(Job
*job
, Error
**errp
)
809 TestBlockJob
*s
= container_of(job
, TestBlockJob
, common
.job
);
811 /* We are running the actual job code past the pause point in
815 job_transition_to_ready(&s
->common
.job
);
816 while (!s
->should_complete
) {
817 /* Avoid job_sleep_ns() because it marks the job as !busy. We want to
818 * emulate some actual activity (probably some I/O) here so that drain
819 * has to wait for this activity to stop. */
820 qemu_co_sleep_ns(QEMU_CLOCK_REALTIME
, 1000000);
822 job_pause_point(&s
->common
.job
);
828 static void test_job_complete(Job
*job
, Error
**errp
)
830 TestBlockJob
*s
= container_of(job
, TestBlockJob
, common
.job
);
831 s
->should_complete
= true;
834 BlockJobDriver test_job_driver
= {
836 .instance_size
= sizeof(TestBlockJob
),
837 .free
= block_job_free
,
838 .user_resume
= block_job_user_resume
,
840 .complete
= test_job_complete
,
841 .prepare
= test_job_prepare
,
842 .commit
= test_job_commit
,
843 .abort
= test_job_abort
,
847 enum test_job_result
{
850 TEST_JOB_FAIL_PREPARE
,
853 enum test_job_drain_node
{
855 TEST_JOB_DRAIN_SRC_CHILD
,
856 TEST_JOB_DRAIN_SRC_PARENT
,
859 static void test_blockjob_common_drain_node(enum drain_type drain_type
,
861 enum test_job_result result
,
862 enum test_job_drain_node drain_node
)
864 BlockBackend
*blk_src
, *blk_target
;
865 BlockDriverState
*src
, *src_backing
, *src_overlay
, *target
, *drain_bs
;
868 IOThread
*iothread
= NULL
;
872 src
= bdrv_new_open_driver(&bdrv_test
, "source", BDRV_O_RDWR
,
874 src_backing
= bdrv_new_open_driver(&bdrv_test
, "source-backing",
875 BDRV_O_RDWR
, &error_abort
);
876 src_overlay
= bdrv_new_open_driver(&bdrv_test
, "source-overlay",
877 BDRV_O_RDWR
, &error_abort
);
879 bdrv_set_backing_hd(src_overlay
, src
, &error_abort
);
881 bdrv_set_backing_hd(src
, src_backing
, &error_abort
);
882 bdrv_unref(src_backing
);
884 blk_src
= blk_new(qemu_get_aio_context(), BLK_PERM_ALL
, BLK_PERM_ALL
);
885 blk_insert_bs(blk_src
, src_overlay
, &error_abort
);
887 switch (drain_node
) {
888 case TEST_JOB_DRAIN_SRC
:
891 case TEST_JOB_DRAIN_SRC_CHILD
:
892 drain_bs
= src_backing
;
894 case TEST_JOB_DRAIN_SRC_PARENT
:
895 drain_bs
= src_overlay
;
898 g_assert_not_reached();
902 iothread
= iothread_new();
903 ctx
= iothread_get_aio_context(iothread
);
904 blk_set_aio_context(blk_src
, ctx
, &error_abort
);
906 ctx
= qemu_get_aio_context();
909 target
= bdrv_new_open_driver(&bdrv_test
, "target", BDRV_O_RDWR
,
911 blk_target
= blk_new(qemu_get_aio_context(), BLK_PERM_ALL
, BLK_PERM_ALL
);
912 blk_insert_bs(blk_target
, target
, &error_abort
);
913 blk_set_allow_aio_context_change(blk_target
, true);
915 aio_context_acquire(ctx
);
916 tjob
= block_job_create("job0", &test_job_driver
, NULL
, src
,
918 0, 0, NULL
, NULL
, &error_abort
);
921 block_job_add_bdrv(job
, "target", target
, 0, BLK_PERM_ALL
, &error_abort
);
924 case TEST_JOB_SUCCESS
:
926 case TEST_JOB_FAIL_RUN
:
927 tjob
->run_ret
= -EIO
;
929 case TEST_JOB_FAIL_PREPARE
:
930 tjob
->prepare_ret
= -EIO
;
934 job_start(&job
->job
);
935 aio_context_release(ctx
);
938 /* job_co_entry() is run in the I/O thread, wait for the actual job
939 * code to start (we don't want to catch the job in the pause point in
941 while (!tjob
->running
) {
942 aio_poll(qemu_get_aio_context(), false);
946 g_assert_cmpint(job
->job
.pause_count
, ==, 0);
947 g_assert_false(job
->job
.paused
);
948 g_assert_true(tjob
->running
);
949 g_assert_true(job
->job
.busy
); /* We're in qemu_co_sleep_ns() */
951 do_drain_begin_unlocked(drain_type
, drain_bs
);
953 if (drain_type
== BDRV_DRAIN_ALL
) {
954 /* bdrv_drain_all() drains both src and target */
955 g_assert_cmpint(job
->job
.pause_count
, ==, 2);
957 g_assert_cmpint(job
->job
.pause_count
, ==, 1);
959 g_assert_true(job
->job
.paused
);
960 g_assert_false(job
->job
.busy
); /* The job is paused */
962 do_drain_end_unlocked(drain_type
, drain_bs
);
965 /* paused is reset in the I/O thread, wait for it */
966 while (job
->job
.paused
) {
967 aio_poll(qemu_get_aio_context(), false);
971 g_assert_cmpint(job
->job
.pause_count
, ==, 0);
972 g_assert_false(job
->job
.paused
);
973 g_assert_true(job
->job
.busy
); /* We're in qemu_co_sleep_ns() */
975 do_drain_begin_unlocked(drain_type
, target
);
977 if (drain_type
== BDRV_DRAIN_ALL
) {
978 /* bdrv_drain_all() drains both src and target */
979 g_assert_cmpint(job
->job
.pause_count
, ==, 2);
981 g_assert_cmpint(job
->job
.pause_count
, ==, 1);
983 g_assert_true(job
->job
.paused
);
984 g_assert_false(job
->job
.busy
); /* The job is paused */
986 do_drain_end_unlocked(drain_type
, target
);
989 /* paused is reset in the I/O thread, wait for it */
990 while (job
->job
.paused
) {
991 aio_poll(qemu_get_aio_context(), false);
995 g_assert_cmpint(job
->job
.pause_count
, ==, 0);
996 g_assert_false(job
->job
.paused
);
997 g_assert_true(job
->job
.busy
); /* We're in qemu_co_sleep_ns() */
999 aio_context_acquire(ctx
);
1000 ret
= job_complete_sync(&job
->job
, &error_abort
);
1001 g_assert_cmpint(ret
, ==, (result
== TEST_JOB_SUCCESS
? 0 : -EIO
));
1004 blk_set_aio_context(blk_src
, qemu_get_aio_context(), &error_abort
);
1005 assert(blk_get_aio_context(blk_target
) == qemu_get_aio_context());
1007 aio_context_release(ctx
);
1010 blk_unref(blk_target
);
1011 bdrv_unref(src_overlay
);
1015 iothread_join(iothread
);
1019 static void test_blockjob_common(enum drain_type drain_type
, bool use_iothread
,
1020 enum test_job_result result
)
1022 test_blockjob_common_drain_node(drain_type
, use_iothread
, result
,
1023 TEST_JOB_DRAIN_SRC
);
1024 test_blockjob_common_drain_node(drain_type
, use_iothread
, result
,
1025 TEST_JOB_DRAIN_SRC_CHILD
);
1026 if (drain_type
== BDRV_SUBTREE_DRAIN
) {
1027 test_blockjob_common_drain_node(drain_type
, use_iothread
, result
,
1028 TEST_JOB_DRAIN_SRC_PARENT
);
1032 static void test_blockjob_drain_all(void)
1034 test_blockjob_common(BDRV_DRAIN_ALL
, false, TEST_JOB_SUCCESS
);
1037 static void test_blockjob_drain(void)
1039 test_blockjob_common(BDRV_DRAIN
, false, TEST_JOB_SUCCESS
);
1042 static void test_blockjob_drain_subtree(void)
1044 test_blockjob_common(BDRV_SUBTREE_DRAIN
, false, TEST_JOB_SUCCESS
);
1047 static void test_blockjob_error_drain_all(void)
1049 test_blockjob_common(BDRV_DRAIN_ALL
, false, TEST_JOB_FAIL_RUN
);
1050 test_blockjob_common(BDRV_DRAIN_ALL
, false, TEST_JOB_FAIL_PREPARE
);
1053 static void test_blockjob_error_drain(void)
1055 test_blockjob_common(BDRV_DRAIN
, false, TEST_JOB_FAIL_RUN
);
1056 test_blockjob_common(BDRV_DRAIN
, false, TEST_JOB_FAIL_PREPARE
);
1059 static void test_blockjob_error_drain_subtree(void)
1061 test_blockjob_common(BDRV_SUBTREE_DRAIN
, false, TEST_JOB_FAIL_RUN
);
1062 test_blockjob_common(BDRV_SUBTREE_DRAIN
, false, TEST_JOB_FAIL_PREPARE
);
1065 static void test_blockjob_iothread_drain_all(void)
1067 test_blockjob_common(BDRV_DRAIN_ALL
, true, TEST_JOB_SUCCESS
);
1070 static void test_blockjob_iothread_drain(void)
1072 test_blockjob_common(BDRV_DRAIN
, true, TEST_JOB_SUCCESS
);
1075 static void test_blockjob_iothread_drain_subtree(void)
1077 test_blockjob_common(BDRV_SUBTREE_DRAIN
, true, TEST_JOB_SUCCESS
);
1080 static void test_blockjob_iothread_error_drain_all(void)
1082 test_blockjob_common(BDRV_DRAIN_ALL
, true, TEST_JOB_FAIL_RUN
);
1083 test_blockjob_common(BDRV_DRAIN_ALL
, true, TEST_JOB_FAIL_PREPARE
);
1086 static void test_blockjob_iothread_error_drain(void)
1088 test_blockjob_common(BDRV_DRAIN
, true, TEST_JOB_FAIL_RUN
);
1089 test_blockjob_common(BDRV_DRAIN
, true, TEST_JOB_FAIL_PREPARE
);
1092 static void test_blockjob_iothread_error_drain_subtree(void)
1094 test_blockjob_common(BDRV_SUBTREE_DRAIN
, true, TEST_JOB_FAIL_RUN
);
1095 test_blockjob_common(BDRV_SUBTREE_DRAIN
, true, TEST_JOB_FAIL_PREPARE
);
1099 typedef struct BDRVTestTopState
{
1100 BdrvChild
*wait_child
;
1103 static void bdrv_test_top_close(BlockDriverState
*bs
)
1105 BdrvChild
*c
, *next_c
;
1106 QLIST_FOREACH_SAFE(c
, &bs
->children
, next
, next_c
) {
1107 bdrv_unref_child(bs
, c
);
1111 static int coroutine_fn
bdrv_test_top_co_preadv(BlockDriverState
*bs
,
1112 int64_t offset
, int64_t bytes
,
1114 BdrvRequestFlags 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_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_of_bds
,
1194 BDRV_CHILD_DATA
, &error_abort
);
1196 /* This child will be the one to pass to requests through to, and
1197 * it will stall until a drain occurs */
1198 child_bs
= bdrv_new_open_driver(&bdrv_test
, "child", BDRV_O_RDWR
,
1200 child_bs
->total_sectors
= 65536 >> BDRV_SECTOR_BITS
;
1201 /* Takes our reference to child_bs */
1202 tts
->wait_child
= bdrv_attach_child(bs
, child_bs
, "wait-child",
1204 BDRV_CHILD_DATA
| BDRV_CHILD_PRIMARY
,
1207 /* This child is just there to be deleted
1208 * (for detach_instead_of_delete == true) */
1209 null_bs
= bdrv_open("null-co://", NULL
, NULL
, BDRV_O_RDWR
| BDRV_O_PROTOCOL
,
1211 bdrv_attach_child(bs
, null_bs
, "null-child", &child_of_bds
, BDRV_CHILD_DATA
,
1214 blk
= blk_new(qemu_get_aio_context(), BLK_PERM_ALL
, BLK_PERM_ALL
);
1215 blk_insert_bs(blk
, bs
, &error_abort
);
1217 /* Referenced by blk now */
1220 g_assert_cmpint(bs
->refcnt
, ==, 1);
1221 g_assert_cmpint(child_bs
->refcnt
, ==, 1);
1222 g_assert_cmpint(null_bs
->refcnt
, ==, 1);
1225 dbdd
= (TestCoDeleteByDrainData
){
1227 .detach_instead_of_delete
= detach_instead_of_delete
,
1230 co
= qemu_coroutine_create(test_co_delete_by_drain
, &dbdd
);
1231 qemu_coroutine_enter(co
);
1233 /* Drain the child while the read operation is still pending.
1234 * This should result in the operation finishing and
1235 * test_co_delete_by_drain() resuming. Thus, @bs will be deleted
1236 * and the coroutine will exit while this drain operation is still
1238 switch (drain_type
) {
1241 bdrv_drain(child_bs
);
1242 bdrv_unref(child_bs
);
1244 case BDRV_SUBTREE_DRAIN
:
1245 /* Would have to ref/unref bs here for !detach_instead_of_delete, but
1246 * then the whole test becomes pointless because the graph changes
1247 * don't occur during the drain any more. */
1248 assert(detach_instead_of_delete
);
1249 bdrv_subtree_drained_begin(bs
);
1250 bdrv_subtree_drained_end(bs
);
1252 case BDRV_DRAIN_ALL
:
1253 bdrv_drain_all_begin();
1254 bdrv_drain_all_end();
1257 g_assert_not_reached();
1260 while (!dbdd
.done
) {
1261 aio_poll(qemu_get_aio_context(), true);
1264 if (detach_instead_of_delete
) {
1265 /* Here, the reference has not passed over to the coroutine,
1266 * so we have to delete the BB ourselves */
1271 static void test_delete_by_drain(void)
1273 do_test_delete_by_drain(false, BDRV_DRAIN
);
1276 static void test_detach_by_drain_all(void)
1278 do_test_delete_by_drain(true, BDRV_DRAIN_ALL
);
1281 static void test_detach_by_drain(void)
1283 do_test_delete_by_drain(true, BDRV_DRAIN
);
1286 static void test_detach_by_drain_subtree(void)
1288 do_test_delete_by_drain(true, BDRV_SUBTREE_DRAIN
);
1292 struct detach_by_parent_data
{
1293 BlockDriverState
*parent_b
;
1295 BlockDriverState
*c
;
1299 static struct detach_by_parent_data detach_by_parent_data
;
1301 static void detach_indirect_bh(void *opaque
)
1303 struct detach_by_parent_data
*data
= opaque
;
1305 bdrv_unref_child(data
->parent_b
, data
->child_b
);
1308 data
->child_c
= bdrv_attach_child(data
->parent_b
, data
->c
, "PB-C",
1309 &child_of_bds
, BDRV_CHILD_DATA
,
1313 static void detach_by_parent_aio_cb(void *opaque
, int ret
)
1315 struct detach_by_parent_data
*data
= &detach_by_parent_data
;
1317 g_assert_cmpint(ret
, ==, 0);
1318 if (data
->by_parent_cb
) {
1319 detach_indirect_bh(data
);
1323 static void detach_by_driver_cb_drained_begin(BdrvChild
*child
)
1325 aio_bh_schedule_oneshot(qemu_get_current_aio_context(),
1326 detach_indirect_bh
, &detach_by_parent_data
);
1327 child_of_bds
.drained_begin(child
);
1330 static BdrvChildClass detach_by_driver_cb_class
;
1339 * by_parent_cb == true: Test that parent callbacks don't poll
1341 * PA has a pending write request whose callback changes the child nodes of
1342 * PB: It removes B and adds C instead. The subtree of PB is drained, which
1343 * will indirectly drain the write request, too.
1345 * by_parent_cb == false: Test that bdrv_drain_invoke() doesn't poll
1347 * PA's BdrvChildClass has a .drained_begin callback that schedules a BH
1348 * that does the same graph change. If bdrv_drain_invoke() calls it, the
1349 * state is messed up, but if it is only polled in the single
1350 * BDRV_POLL_WHILE() at the end of the drain, this should work fine.
1352 static void test_detach_indirect(bool by_parent_cb
)
1355 BlockDriverState
*parent_a
, *parent_b
, *a
, *b
, *c
;
1356 BdrvChild
*child_a
, *child_b
;
1359 QEMUIOVector qiov
= QEMU_IOVEC_INIT_BUF(qiov
, NULL
, 0);
1361 if (!by_parent_cb
) {
1362 detach_by_driver_cb_class
= child_of_bds
;
1363 detach_by_driver_cb_class
.drained_begin
=
1364 detach_by_driver_cb_drained_begin
;
1367 /* Create all involved nodes */
1368 parent_a
= bdrv_new_open_driver(&bdrv_test
, "parent-a", BDRV_O_RDWR
,
1370 parent_b
= bdrv_new_open_driver(&bdrv_test
, "parent-b", 0,
1373 a
= bdrv_new_open_driver(&bdrv_test
, "a", BDRV_O_RDWR
, &error_abort
);
1374 b
= bdrv_new_open_driver(&bdrv_test
, "b", BDRV_O_RDWR
, &error_abort
);
1375 c
= bdrv_new_open_driver(&bdrv_test
, "c", BDRV_O_RDWR
, &error_abort
);
1377 /* blk is a BB for parent-a */
1378 blk
= blk_new(qemu_get_aio_context(), BLK_PERM_ALL
, BLK_PERM_ALL
);
1379 blk_insert_bs(blk
, parent_a
, &error_abort
);
1380 bdrv_unref(parent_a
);
1382 /* If we want to get bdrv_drain_invoke() to call aio_poll(), the driver
1383 * callback must not return immediately. */
1384 if (!by_parent_cb
) {
1385 BDRVTestState
*s
= parent_a
->opaque
;
1386 s
->sleep_in_drain_begin
= true;
1389 /* Set child relationships */
1392 child_b
= bdrv_attach_child(parent_b
, b
, "PB-B", &child_of_bds
,
1393 BDRV_CHILD_DATA
, &error_abort
);
1394 child_a
= bdrv_attach_child(parent_b
, a
, "PB-A", &child_of_bds
,
1395 BDRV_CHILD_COW
, &error_abort
);
1398 bdrv_attach_child(parent_a
, a
, "PA-A",
1399 by_parent_cb
? &child_of_bds
: &detach_by_driver_cb_class
,
1400 BDRV_CHILD_DATA
, &error_abort
);
1402 g_assert_cmpint(parent_a
->refcnt
, ==, 1);
1403 g_assert_cmpint(parent_b
->refcnt
, ==, 1);
1404 g_assert_cmpint(a
->refcnt
, ==, 3);
1405 g_assert_cmpint(b
->refcnt
, ==, 2);
1406 g_assert_cmpint(c
->refcnt
, ==, 1);
1408 g_assert(QLIST_FIRST(&parent_b
->children
) == child_a
);
1409 g_assert(QLIST_NEXT(child_a
, next
) == child_b
);
1410 g_assert(QLIST_NEXT(child_b
, next
) == NULL
);
1412 /* Start the evil write request */
1413 detach_by_parent_data
= (struct detach_by_parent_data
) {
1414 .parent_b
= parent_b
,
1417 .by_parent_cb
= by_parent_cb
,
1419 acb
= blk_aio_preadv(blk
, 0, &qiov
, 0, detach_by_parent_aio_cb
, NULL
);
1420 g_assert(acb
!= NULL
);
1422 /* Drain and check the expected result */
1423 bdrv_subtree_drained_begin(parent_b
);
1425 g_assert(detach_by_parent_data
.child_c
!= NULL
);
1427 g_assert_cmpint(parent_a
->refcnt
, ==, 1);
1428 g_assert_cmpint(parent_b
->refcnt
, ==, 1);
1429 g_assert_cmpint(a
->refcnt
, ==, 3);
1430 g_assert_cmpint(b
->refcnt
, ==, 1);
1431 g_assert_cmpint(c
->refcnt
, ==, 2);
1433 g_assert(QLIST_FIRST(&parent_b
->children
) == detach_by_parent_data
.child_c
);
1434 g_assert(QLIST_NEXT(detach_by_parent_data
.child_c
, next
) == child_a
);
1435 g_assert(QLIST_NEXT(child_a
, next
) == NULL
);
1437 g_assert_cmpint(parent_a
->quiesce_counter
, ==, 1);
1438 g_assert_cmpint(parent_b
->quiesce_counter
, ==, 1);
1439 g_assert_cmpint(a
->quiesce_counter
, ==, 1);
1440 g_assert_cmpint(b
->quiesce_counter
, ==, 0);
1441 g_assert_cmpint(c
->quiesce_counter
, ==, 1);
1443 bdrv_subtree_drained_end(parent_b
);
1445 bdrv_unref(parent_b
);
1448 g_assert_cmpint(a
->refcnt
, ==, 1);
1449 g_assert_cmpint(b
->refcnt
, ==, 1);
1450 g_assert_cmpint(c
->refcnt
, ==, 1);
1456 static void test_detach_by_parent_cb(void)
1458 test_detach_indirect(true);
1461 static void test_detach_by_driver_cb(void)
1463 test_detach_indirect(false);
1466 static void test_append_to_drained(void)
1469 BlockDriverState
*base
, *overlay
;
1470 BDRVTestState
*base_s
, *overlay_s
;
1472 blk
= blk_new(qemu_get_aio_context(), BLK_PERM_ALL
, BLK_PERM_ALL
);
1473 base
= bdrv_new_open_driver(&bdrv_test
, "base", BDRV_O_RDWR
, &error_abort
);
1474 base_s
= base
->opaque
;
1475 blk_insert_bs(blk
, base
, &error_abort
);
1477 overlay
= bdrv_new_open_driver(&bdrv_test
, "overlay", BDRV_O_RDWR
,
1479 overlay_s
= overlay
->opaque
;
1481 do_drain_begin(BDRV_DRAIN
, base
);
1482 g_assert_cmpint(base
->quiesce_counter
, ==, 1);
1483 g_assert_cmpint(base_s
->drain_count
, ==, 1);
1484 g_assert_cmpint(base
->in_flight
, ==, 0);
1486 bdrv_append(overlay
, base
, &error_abort
);
1487 g_assert_cmpint(base
->in_flight
, ==, 0);
1488 g_assert_cmpint(overlay
->in_flight
, ==, 0);
1490 g_assert_cmpint(base
->quiesce_counter
, ==, 1);
1491 g_assert_cmpint(base_s
->drain_count
, ==, 1);
1492 g_assert_cmpint(overlay
->quiesce_counter
, ==, 1);
1493 g_assert_cmpint(overlay_s
->drain_count
, ==, 1);
1495 do_drain_end(BDRV_DRAIN
, base
);
1497 g_assert_cmpint(base
->quiesce_counter
, ==, 0);
1498 g_assert_cmpint(base_s
->drain_count
, ==, 0);
1499 g_assert_cmpint(overlay
->quiesce_counter
, ==, 0);
1500 g_assert_cmpint(overlay_s
->drain_count
, ==, 0);
1502 bdrv_unref(overlay
);
1507 static void test_set_aio_context(void)
1509 BlockDriverState
*bs
;
1510 IOThread
*a
= iothread_new();
1511 IOThread
*b
= iothread_new();
1512 AioContext
*ctx_a
= iothread_get_aio_context(a
);
1513 AioContext
*ctx_b
= iothread_get_aio_context(b
);
1515 bs
= bdrv_new_open_driver(&bdrv_test
, "test-node", BDRV_O_RDWR
,
1518 bdrv_drained_begin(bs
);
1519 bdrv_try_set_aio_context(bs
, ctx_a
, &error_abort
);
1521 aio_context_acquire(ctx_a
);
1522 bdrv_drained_end(bs
);
1524 bdrv_drained_begin(bs
);
1525 bdrv_try_set_aio_context(bs
, ctx_b
, &error_abort
);
1526 aio_context_release(ctx_a
);
1527 aio_context_acquire(ctx_b
);
1528 bdrv_try_set_aio_context(bs
, qemu_get_aio_context(), &error_abort
);
1529 aio_context_release(ctx_b
);
1530 bdrv_drained_end(bs
);
1538 typedef struct TestDropBackingBlockJob
{
1540 bool should_complete
;
1542 BlockDriverState
*detach_also
;
1543 BlockDriverState
*bs
;
1544 } TestDropBackingBlockJob
;
1546 static int coroutine_fn
test_drop_backing_job_run(Job
*job
, Error
**errp
)
1548 TestDropBackingBlockJob
*s
=
1549 container_of(job
, TestDropBackingBlockJob
, common
.job
);
1551 while (!s
->should_complete
) {
1552 job_sleep_ns(job
, 0);
1558 static void test_drop_backing_job_commit(Job
*job
)
1560 TestDropBackingBlockJob
*s
=
1561 container_of(job
, TestDropBackingBlockJob
, common
.job
);
1563 bdrv_set_backing_hd(s
->bs
, NULL
, &error_abort
);
1564 bdrv_set_backing_hd(s
->detach_also
, NULL
, &error_abort
);
1566 *s
->did_complete
= true;
1569 static const BlockJobDriver test_drop_backing_job_driver
= {
1571 .instance_size
= sizeof(TestDropBackingBlockJob
),
1572 .free
= block_job_free
,
1573 .user_resume
= block_job_user_resume
,
1574 .run
= test_drop_backing_job_run
,
1575 .commit
= test_drop_backing_job_commit
,
1580 * Creates a child node with three parent nodes on it, and then runs a
1581 * block job on the final one, parent-node-2.
1583 * The job is then asked to complete before a section where the child
1586 * Ending this section will undrain the child's parents, first
1587 * parent-node-2, then parent-node-1, then parent-node-0 -- the parent
1588 * list is in reverse order of how they were added. Ending the drain
1589 * on parent-node-2 will resume the job, thus completing it and
1590 * scheduling job_exit().
1592 * Ending the drain on parent-node-1 will poll the AioContext, which
1593 * lets job_exit() and thus test_drop_backing_job_commit() run. That
1594 * function first removes the child as parent-node-2's backing file.
1596 * In old (and buggy) implementations, there are two problems with
1598 * (A) bdrv_drain_invoke() polls for every node that leaves the
1599 * drained section. This means that job_exit() is scheduled
1600 * before the child has left the drained section. Its
1601 * quiesce_counter is therefore still 1 when it is removed from
1604 * (B) bdrv_replace_child_noperm() calls drained_end() on the old
1605 * child's parents as many times as the child is quiesced. This
1606 * means it will call drained_end() on parent-node-2 once.
1607 * Because parent-node-2 is no longer quiesced at this point, this
1610 * bdrv_replace_child_noperm() therefore must call drained_end() on
1611 * the parent only if it really is still drained because the child is
1614 * If removing child from parent-node-2 was successful (as it should
1615 * be), test_drop_backing_job_commit() will then also remove the child
1616 * from parent-node-0.
1618 * With an old version of our drain infrastructure ((A) above), that
1619 * resulted in the following flow:
1621 * 1. child attempts to leave its drained section. The call recurses
1624 * 2. parent-node-2 leaves the drained section. Polling in
1625 * bdrv_drain_invoke() will schedule job_exit().
1627 * 3. parent-node-1 leaves the drained section. Polling in
1628 * bdrv_drain_invoke() will run job_exit(), thus disconnecting
1629 * parent-node-0 from the child node.
1631 * 4. bdrv_parent_drained_end() uses a QLIST_FOREACH_SAFE() loop to
1632 * iterate over the parents. Thus, it now accesses the BdrvChild
1633 * object that used to connect parent-node-0 and the child node.
1634 * However, that object no longer exists, so it accesses a dangling
1637 * The solution is to only poll once when running a bdrv_drained_end()
1638 * operation, specifically at the end when all drained_end()
1639 * operations for all involved nodes have been scheduled.
1640 * Note that this also solves (A) above, thus hiding (B).
1642 static void test_blockjob_commit_by_drained_end(void)
1644 BlockDriverState
*bs_child
, *bs_parents
[3];
1645 TestDropBackingBlockJob
*job
;
1646 bool job_has_completed
= false;
1649 bs_child
= bdrv_new_open_driver(&bdrv_test
, "child-node", BDRV_O_RDWR
,
1652 for (i
= 0; i
< 3; i
++) {
1654 snprintf(name
, sizeof(name
), "parent-node-%i", i
);
1655 bs_parents
[i
] = bdrv_new_open_driver(&bdrv_test
, name
, BDRV_O_RDWR
,
1657 bdrv_set_backing_hd(bs_parents
[i
], bs_child
, &error_abort
);
1660 job
= block_job_create("job", &test_drop_backing_job_driver
, NULL
,
1661 bs_parents
[2], 0, BLK_PERM_ALL
, 0, 0, NULL
, NULL
,
1663 job
->bs
= bs_parents
[2];
1665 job
->detach_also
= bs_parents
[0];
1666 job
->did_complete
= &job_has_completed
;
1668 job_start(&job
->common
.job
);
1670 job
->should_complete
= true;
1671 bdrv_drained_begin(bs_child
);
1672 g_assert(!job_has_completed
);
1673 bdrv_drained_end(bs_child
);
1674 g_assert(job_has_completed
);
1676 bdrv_unref(bs_parents
[0]);
1677 bdrv_unref(bs_parents
[1]);
1678 bdrv_unref(bs_parents
[2]);
1679 bdrv_unref(bs_child
);
1683 typedef struct TestSimpleBlockJob
{
1685 bool should_complete
;
1687 } TestSimpleBlockJob
;
1689 static int coroutine_fn
test_simple_job_run(Job
*job
, Error
**errp
)
1691 TestSimpleBlockJob
*s
= container_of(job
, TestSimpleBlockJob
, common
.job
);
1693 while (!s
->should_complete
) {
1694 job_sleep_ns(job
, 0);
1700 static void test_simple_job_clean(Job
*job
)
1702 TestSimpleBlockJob
*s
= container_of(job
, TestSimpleBlockJob
, common
.job
);
1703 *s
->did_complete
= true;
1706 static const BlockJobDriver test_simple_job_driver
= {
1708 .instance_size
= sizeof(TestSimpleBlockJob
),
1709 .free
= block_job_free
,
1710 .user_resume
= block_job_user_resume
,
1711 .run
= test_simple_job_run
,
1712 .clean
= test_simple_job_clean
,
1716 static int drop_intermediate_poll_update_filename(BdrvChild
*child
,
1717 BlockDriverState
*new_base
,
1718 const char *filename
,
1722 * We are free to poll here, which may change the block graph, if
1723 * it is not drained.
1726 /* If the job is not drained: Complete it, schedule job_exit() */
1727 aio_poll(qemu_get_current_aio_context(), false);
1728 /* If the job is not drained: Run job_exit(), finish the job */
1729 aio_poll(qemu_get_current_aio_context(), false);
1735 * Test a poll in the midst of bdrv_drop_intermediate().
1737 * bdrv_drop_intermediate() calls BdrvChildClass.update_filename(),
1738 * which can yield or poll. This may lead to graph changes, unless
1739 * the whole subtree in question is drained.
1741 * We test this on the following graph:
1757 * node-2 --chain--> node-1 --chain--> node-0
1759 * We drop node-1 with bdrv_drop_intermediate(top=node-1, base=node-0).
1761 * This first updates node-2's backing filename by invoking
1762 * drop_intermediate_poll_update_filename(), which polls twice. This
1763 * causes the job to finish, which in turns causes the job-node to be
1766 * bdrv_drop_intermediate() uses a QLIST_FOREACH_SAFE() loop, so it
1767 * already has a pointer to the BdrvChild edge between job-node and
1768 * node-1. When it tries to handle that edge, we probably get a
1769 * segmentation fault because the object no longer exists.
1772 * The solution is for bdrv_drop_intermediate() to drain top's
1773 * subtree. This prevents graph changes from happening just because
1774 * BdrvChildClass.update_filename() yields or polls. Thus, the block
1775 * job is paused during that drained section and must finish before or
1778 * (In addition, bdrv_replace_child() must keep the job paused.)
1780 static void test_drop_intermediate_poll(void)
1782 static BdrvChildClass chain_child_class
;
1783 BlockDriverState
*chain
[3];
1784 TestSimpleBlockJob
*job
;
1785 BlockDriverState
*job_node
;
1786 bool job_has_completed
= false;
1790 chain_child_class
= child_of_bds
;
1791 chain_child_class
.update_filename
= drop_intermediate_poll_update_filename
;
1793 for (i
= 0; i
< 3; i
++) {
1795 snprintf(name
, 32, "node-%i", i
);
1797 chain
[i
] = bdrv_new_open_driver(&bdrv_test
, name
, 0, &error_abort
);
1800 job_node
= bdrv_new_open_driver(&bdrv_test
, "job-node", BDRV_O_RDWR
,
1802 bdrv_set_backing_hd(job_node
, chain
[1], &error_abort
);
1805 * Establish the chain last, so the chain links are the first
1806 * elements in the BDS.parents lists
1808 for (i
= 0; i
< 3; i
++) {
1810 /* Takes the reference to chain[i - 1] */
1811 chain
[i
]->backing
= bdrv_attach_child(chain
[i
], chain
[i
- 1],
1812 "chain", &chain_child_class
,
1813 BDRV_CHILD_COW
, &error_abort
);
1817 job
= block_job_create("job", &test_simple_job_driver
, NULL
, job_node
,
1818 0, BLK_PERM_ALL
, 0, 0, NULL
, NULL
, &error_abort
);
1820 /* The job has a reference now */
1821 bdrv_unref(job_node
);
1823 job
->did_complete
= &job_has_completed
;
1825 job_start(&job
->common
.job
);
1826 job
->should_complete
= true;
1828 g_assert(!job_has_completed
);
1829 ret
= bdrv_drop_intermediate(chain
[1], chain
[0], NULL
);
1831 g_assert(job_has_completed
);
1833 bdrv_unref(chain
[2]);
1837 typedef struct BDRVReplaceTestState
{
1844 bool yield_before_read
;
1846 Coroutine
*drain_co
;
1847 } BDRVReplaceTestState
;
1849 static void bdrv_replace_test_close(BlockDriverState
*bs
)
1854 * If @bs has a backing file:
1855 * Yield if .yield_before_read is true (and wait for drain_begin to
1857 * Forward the read to bs->backing. Set .has_read to true.
1858 * If drain_begin has woken us, wake it in turn.
1861 * Set .has_read to true and return success.
1863 static int coroutine_fn
bdrv_replace_test_co_preadv(BlockDriverState
*bs
,
1867 BdrvRequestFlags flags
)
1869 BDRVReplaceTestState
*s
= bs
->opaque
;
1874 g_assert(!s
->drain_count
);
1876 s
->io_co
= qemu_coroutine_self();
1877 if (s
->yield_before_read
) {
1878 s
->yield_before_read
= false;
1879 qemu_coroutine_yield();
1883 ret
= bdrv_co_preadv(bs
->backing
, offset
, bytes
, qiov
, 0);
1886 /* Wake up drain_co if it runs */
1888 aio_co_wake(s
->drain_co
);
1899 * If .drain_count is 0, wake up .io_co if there is one; and set
1901 * Increment .drain_count.
1903 static void coroutine_fn
bdrv_replace_test_co_drain_begin(BlockDriverState
*bs
)
1905 BDRVReplaceTestState
*s
= bs
->opaque
;
1907 if (!s
->drain_count
) {
1908 /* Keep waking io_co up until it is done */
1909 s
->drain_co
= qemu_coroutine_self();
1911 aio_co_wake(s
->io_co
);
1913 qemu_coroutine_yield();
1917 s
->was_drained
= true;
1923 * Reduce .drain_count, set .was_undrained once it reaches 0.
1924 * If .drain_count reaches 0 and the node has a backing file, issue a
1927 static void coroutine_fn
bdrv_replace_test_co_drain_end(BlockDriverState
*bs
)
1929 BDRVReplaceTestState
*s
= bs
->opaque
;
1931 g_assert(s
->drain_count
> 0);
1932 if (!--s
->drain_count
) {
1935 s
->was_undrained
= true;
1939 QEMUIOVector qiov
= QEMU_IOVEC_INIT_BUF(qiov
, &data
, 1);
1941 /* Queue a read request post-drain */
1942 ret
= bdrv_replace_test_co_preadv(bs
, 0, 1, &qiov
, 0);
1948 static BlockDriver bdrv_replace_test
= {
1949 .format_name
= "replace_test",
1950 .instance_size
= sizeof(BDRVReplaceTestState
),
1952 .bdrv_close
= bdrv_replace_test_close
,
1953 .bdrv_co_preadv
= bdrv_replace_test_co_preadv
,
1955 .bdrv_co_drain_begin
= bdrv_replace_test_co_drain_begin
,
1956 .bdrv_co_drain_end
= bdrv_replace_test_co_drain_end
,
1958 .bdrv_child_perm
= bdrv_default_perms
,
1961 static void coroutine_fn
test_replace_child_mid_drain_read_co(void *opaque
)
1966 ret
= blk_co_pread(opaque
, 0, 1, &data
, 0);
1971 * We test two things:
1972 * (1) bdrv_replace_child_noperm() must not undrain the parent if both
1973 * children are drained.
1974 * (2) bdrv_replace_child_noperm() must never flush I/O requests to a
1975 * drained child. If the old child is drained, it must flush I/O
1976 * requests after the new one has been attached. If the new child
1977 * is drained, it must flush I/O requests before the old one is
1980 * To do so, we create one parent node and two child nodes; then
1981 * attach one of the children (old_child_bs) to the parent, then
1982 * drain both old_child_bs and new_child_bs according to
1983 * old_drain_count and new_drain_count, respectively, and finally
1984 * we invoke bdrv_replace_node() to replace old_child_bs by
1987 * The test block driver we use here (bdrv_replace_test) has a read
1989 * - For the parent node, can optionally yield, and then forwards the
1990 * read to bdrv_preadv(),
1991 * - For the child node, just returns immediately.
1993 * If the read yields, the drain_begin function will wake it up.
1995 * The drain_end function issues a read on the parent once it is fully
1996 * undrained (which simulates requests starting to come in again).
1998 static void do_test_replace_child_mid_drain(int old_drain_count
,
1999 int new_drain_count
)
2001 BlockBackend
*parent_blk
;
2002 BlockDriverState
*parent_bs
;
2003 BlockDriverState
*old_child_bs
, *new_child_bs
;
2004 BDRVReplaceTestState
*parent_s
;
2005 BDRVReplaceTestState
*old_child_s
, *new_child_s
;
2009 parent_bs
= bdrv_new_open_driver(&bdrv_replace_test
, "parent", 0,
2011 parent_s
= parent_bs
->opaque
;
2013 parent_blk
= blk_new(qemu_get_aio_context(),
2014 BLK_PERM_CONSISTENT_READ
, BLK_PERM_ALL
);
2015 blk_insert_bs(parent_blk
, parent_bs
, &error_abort
);
2017 old_child_bs
= bdrv_new_open_driver(&bdrv_replace_test
, "old-child", 0,
2019 new_child_bs
= bdrv_new_open_driver(&bdrv_replace_test
, "new-child", 0,
2021 old_child_s
= old_child_bs
->opaque
;
2022 new_child_s
= new_child_bs
->opaque
;
2024 /* So that we can read something */
2025 parent_bs
->total_sectors
= 1;
2026 old_child_bs
->total_sectors
= 1;
2027 new_child_bs
->total_sectors
= 1;
2029 bdrv_ref(old_child_bs
);
2030 parent_bs
->backing
= bdrv_attach_child(parent_bs
, old_child_bs
, "child",
2031 &child_of_bds
, BDRV_CHILD_COW
,
2034 for (i
= 0; i
< old_drain_count
; i
++) {
2035 bdrv_drained_begin(old_child_bs
);
2037 for (i
= 0; i
< new_drain_count
; i
++) {
2038 bdrv_drained_begin(new_child_bs
);
2041 if (!old_drain_count
) {
2043 * Start a read operation that will yield, so it will not
2044 * complete before the node is drained.
2046 parent_s
->yield_before_read
= true;
2047 io_co
= qemu_coroutine_create(test_replace_child_mid_drain_read_co
,
2049 qemu_coroutine_enter(io_co
);
2052 /* If we have started a read operation, it should have yielded */
2053 g_assert(!parent_s
->has_read
);
2055 /* Reset drained status so we can see what bdrv_replace_node() does */
2056 parent_s
->was_drained
= false;
2057 parent_s
->was_undrained
= false;
2059 g_assert(parent_bs
->quiesce_counter
== old_drain_count
);
2060 bdrv_replace_node(old_child_bs
, new_child_bs
, &error_abort
);
2061 g_assert(parent_bs
->quiesce_counter
== new_drain_count
);
2063 if (!old_drain_count
&& !new_drain_count
) {
2065 * From undrained to undrained drains and undrains the parent,
2066 * because bdrv_replace_node() contains a drained section for
2069 g_assert(parent_s
->was_drained
&& parent_s
->was_undrained
);
2070 } else if (!old_drain_count
&& new_drain_count
) {
2072 * From undrained to drained should drain the parent and keep
2075 g_assert(parent_s
->was_drained
&& !parent_s
->was_undrained
);
2076 } else if (old_drain_count
&& !new_drain_count
) {
2078 * From drained to undrained should undrain the parent and
2081 g_assert(!parent_s
->was_drained
&& parent_s
->was_undrained
);
2082 } else /* if (old_drain_count && new_drain_count) */ {
2084 * From drained to drained must not undrain the parent at any
2087 g_assert(!parent_s
->was_drained
&& !parent_s
->was_undrained
);
2090 if (!old_drain_count
|| !new_drain_count
) {
2092 * If !old_drain_count, we have started a read request before
2093 * bdrv_replace_node(). If !new_drain_count, the parent must
2094 * have been undrained at some point, and
2095 * bdrv_replace_test_co_drain_end() starts a read request
2098 g_assert(parent_s
->has_read
);
2101 * If the parent was never undrained, there is no way to start
2104 g_assert(!parent_s
->has_read
);
2107 /* A drained child must have not received any request */
2108 g_assert(!(old_drain_count
&& old_child_s
->has_read
));
2109 g_assert(!(new_drain_count
&& new_child_s
->has_read
));
2111 for (i
= 0; i
< new_drain_count
; i
++) {
2112 bdrv_drained_end(new_child_bs
);
2114 for (i
= 0; i
< old_drain_count
; i
++) {
2115 bdrv_drained_end(old_child_bs
);
2119 * By now, bdrv_replace_test_co_drain_end() must have been called
2120 * at some point while the new child was attached to the parent.
2122 g_assert(parent_s
->has_read
);
2123 g_assert(new_child_s
->has_read
);
2125 blk_unref(parent_blk
);
2126 bdrv_unref(parent_bs
);
2127 bdrv_unref(old_child_bs
);
2128 bdrv_unref(new_child_bs
);
2131 static void test_replace_child_mid_drain(void)
2133 int old_drain_count
, new_drain_count
;
2135 for (old_drain_count
= 0; old_drain_count
< 2; old_drain_count
++) {
2136 for (new_drain_count
= 0; new_drain_count
< 2; new_drain_count
++) {
2137 do_test_replace_child_mid_drain(old_drain_count
, new_drain_count
);
2142 int main(int argc
, char **argv
)
2147 qemu_init_main_loop(&error_abort
);
2149 g_test_init(&argc
, &argv
, NULL
);
2150 qemu_event_init(&done_event
, false);
2152 g_test_add_func("/bdrv-drain/driver-cb/drain_all", test_drv_cb_drain_all
);
2153 g_test_add_func("/bdrv-drain/driver-cb/drain", test_drv_cb_drain
);
2154 g_test_add_func("/bdrv-drain/driver-cb/drain_subtree",
2155 test_drv_cb_drain_subtree
);
2157 g_test_add_func("/bdrv-drain/driver-cb/co/drain_all",
2158 test_drv_cb_co_drain_all
);
2159 g_test_add_func("/bdrv-drain/driver-cb/co/drain", test_drv_cb_co_drain
);
2160 g_test_add_func("/bdrv-drain/driver-cb/co/drain_subtree",
2161 test_drv_cb_co_drain_subtree
);
2164 g_test_add_func("/bdrv-drain/quiesce/drain_all", test_quiesce_drain_all
);
2165 g_test_add_func("/bdrv-drain/quiesce/drain", test_quiesce_drain
);
2166 g_test_add_func("/bdrv-drain/quiesce/drain_subtree",
2167 test_quiesce_drain_subtree
);
2169 g_test_add_func("/bdrv-drain/quiesce/co/drain_all",
2170 test_quiesce_co_drain_all
);
2171 g_test_add_func("/bdrv-drain/quiesce/co/drain", test_quiesce_co_drain
);
2172 g_test_add_func("/bdrv-drain/quiesce/co/drain_subtree",
2173 test_quiesce_co_drain_subtree
);
2175 g_test_add_func("/bdrv-drain/nested", test_nested
);
2176 g_test_add_func("/bdrv-drain/multiparent", test_multiparent
);
2178 g_test_add_func("/bdrv-drain/graph-change/drain_subtree",
2179 test_graph_change_drain_subtree
);
2180 g_test_add_func("/bdrv-drain/graph-change/drain_all",
2181 test_graph_change_drain_all
);
2183 g_test_add_func("/bdrv-drain/iothread/drain_all", test_iothread_drain_all
);
2184 g_test_add_func("/bdrv-drain/iothread/drain", test_iothread_drain
);
2185 g_test_add_func("/bdrv-drain/iothread/drain_subtree",
2186 test_iothread_drain_subtree
);
2188 g_test_add_func("/bdrv-drain/blockjob/drain_all", test_blockjob_drain_all
);
2189 g_test_add_func("/bdrv-drain/blockjob/drain", test_blockjob_drain
);
2190 g_test_add_func("/bdrv-drain/blockjob/drain_subtree",
2191 test_blockjob_drain_subtree
);
2193 g_test_add_func("/bdrv-drain/blockjob/error/drain_all",
2194 test_blockjob_error_drain_all
);
2195 g_test_add_func("/bdrv-drain/blockjob/error/drain",
2196 test_blockjob_error_drain
);
2197 g_test_add_func("/bdrv-drain/blockjob/error/drain_subtree",
2198 test_blockjob_error_drain_subtree
);
2200 g_test_add_func("/bdrv-drain/blockjob/iothread/drain_all",
2201 test_blockjob_iothread_drain_all
);
2202 g_test_add_func("/bdrv-drain/blockjob/iothread/drain",
2203 test_blockjob_iothread_drain
);
2204 g_test_add_func("/bdrv-drain/blockjob/iothread/drain_subtree",
2205 test_blockjob_iothread_drain_subtree
);
2207 g_test_add_func("/bdrv-drain/blockjob/iothread/error/drain_all",
2208 test_blockjob_iothread_error_drain_all
);
2209 g_test_add_func("/bdrv-drain/blockjob/iothread/error/drain",
2210 test_blockjob_iothread_error_drain
);
2211 g_test_add_func("/bdrv-drain/blockjob/iothread/error/drain_subtree",
2212 test_blockjob_iothread_error_drain_subtree
);
2214 g_test_add_func("/bdrv-drain/deletion/drain", test_delete_by_drain
);
2215 g_test_add_func("/bdrv-drain/detach/drain_all", test_detach_by_drain_all
);
2216 g_test_add_func("/bdrv-drain/detach/drain", test_detach_by_drain
);
2217 g_test_add_func("/bdrv-drain/detach/drain_subtree", test_detach_by_drain_subtree
);
2218 g_test_add_func("/bdrv-drain/detach/parent_cb", test_detach_by_parent_cb
);
2219 g_test_add_func("/bdrv-drain/detach/driver_cb", test_detach_by_driver_cb
);
2221 g_test_add_func("/bdrv-drain/attach/drain", test_append_to_drained
);
2223 g_test_add_func("/bdrv-drain/set_aio_context", test_set_aio_context
);
2225 g_test_add_func("/bdrv-drain/blockjob/commit_by_drained_end",
2226 test_blockjob_commit_by_drained_end
);
2228 g_test_add_func("/bdrv-drain/bdrv_drop_intermediate/poll",
2229 test_drop_intermediate_poll
);
2231 g_test_add_func("/bdrv-drain/replace_child/mid-drain",
2232 test_replace_child_mid_drain
);
2235 qemu_event_destroy(&done_event
);