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
;
933 aio_context_release(ctx
);
935 job_start(&job
->job
);
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 WITH_JOB_LOCK_GUARD() {
947 g_assert_cmpint(job
->job
.pause_count
, ==, 0);
948 g_assert_false(job
->job
.paused
);
949 g_assert_true(tjob
->running
);
950 g_assert_true(job
->job
.busy
); /* We're in qemu_co_sleep_ns() */
953 do_drain_begin_unlocked(drain_type
, drain_bs
);
955 WITH_JOB_LOCK_GUARD() {
956 if (drain_type
== BDRV_DRAIN_ALL
) {
957 /* bdrv_drain_all() drains both src and target */
958 g_assert_cmpint(job
->job
.pause_count
, ==, 2);
960 g_assert_cmpint(job
->job
.pause_count
, ==, 1);
962 g_assert_true(job
->job
.paused
);
963 g_assert_false(job
->job
.busy
); /* The job is paused */
966 do_drain_end_unlocked(drain_type
, drain_bs
);
970 * Here we are waiting for the paused status to change,
971 * so don't bother protecting the read every time.
973 * paused is reset in the I/O thread, wait for it
975 while (job
->job
.paused
) {
976 aio_poll(qemu_get_aio_context(), false);
980 WITH_JOB_LOCK_GUARD() {
981 g_assert_cmpint(job
->job
.pause_count
, ==, 0);
982 g_assert_false(job
->job
.paused
);
983 g_assert_true(job
->job
.busy
); /* We're in qemu_co_sleep_ns() */
986 do_drain_begin_unlocked(drain_type
, target
);
988 WITH_JOB_LOCK_GUARD() {
989 if (drain_type
== BDRV_DRAIN_ALL
) {
990 /* bdrv_drain_all() drains both src and target */
991 g_assert_cmpint(job
->job
.pause_count
, ==, 2);
993 g_assert_cmpint(job
->job
.pause_count
, ==, 1);
995 g_assert_true(job
->job
.paused
);
996 g_assert_false(job
->job
.busy
); /* The job is paused */
999 do_drain_end_unlocked(drain_type
, target
);
1003 * Here we are waiting for the paused status to change,
1004 * so don't bother protecting the read every time.
1006 * paused is reset in the I/O thread, wait for it
1008 while (job
->job
.paused
) {
1009 aio_poll(qemu_get_aio_context(), false);
1013 WITH_JOB_LOCK_GUARD() {
1014 g_assert_cmpint(job
->job
.pause_count
, ==, 0);
1015 g_assert_false(job
->job
.paused
);
1016 g_assert_true(job
->job
.busy
); /* We're in qemu_co_sleep_ns() */
1019 WITH_JOB_LOCK_GUARD() {
1020 ret
= job_complete_sync_locked(&job
->job
, &error_abort
);
1022 g_assert_cmpint(ret
, ==, (result
== TEST_JOB_SUCCESS
? 0 : -EIO
));
1024 aio_context_acquire(ctx
);
1026 blk_set_aio_context(blk_src
, qemu_get_aio_context(), &error_abort
);
1027 assert(blk_get_aio_context(blk_target
) == qemu_get_aio_context());
1029 aio_context_release(ctx
);
1032 blk_unref(blk_target
);
1033 bdrv_unref(src_overlay
);
1037 iothread_join(iothread
);
1041 static void test_blockjob_common(enum drain_type drain_type
, bool use_iothread
,
1042 enum test_job_result result
)
1044 test_blockjob_common_drain_node(drain_type
, use_iothread
, result
,
1045 TEST_JOB_DRAIN_SRC
);
1046 test_blockjob_common_drain_node(drain_type
, use_iothread
, result
,
1047 TEST_JOB_DRAIN_SRC_CHILD
);
1048 if (drain_type
== BDRV_SUBTREE_DRAIN
) {
1049 test_blockjob_common_drain_node(drain_type
, use_iothread
, result
,
1050 TEST_JOB_DRAIN_SRC_PARENT
);
1054 static void test_blockjob_drain_all(void)
1056 test_blockjob_common(BDRV_DRAIN_ALL
, false, TEST_JOB_SUCCESS
);
1059 static void test_blockjob_drain(void)
1061 test_blockjob_common(BDRV_DRAIN
, false, TEST_JOB_SUCCESS
);
1064 static void test_blockjob_drain_subtree(void)
1066 test_blockjob_common(BDRV_SUBTREE_DRAIN
, false, TEST_JOB_SUCCESS
);
1069 static void test_blockjob_error_drain_all(void)
1071 test_blockjob_common(BDRV_DRAIN_ALL
, false, TEST_JOB_FAIL_RUN
);
1072 test_blockjob_common(BDRV_DRAIN_ALL
, false, TEST_JOB_FAIL_PREPARE
);
1075 static void test_blockjob_error_drain(void)
1077 test_blockjob_common(BDRV_DRAIN
, false, TEST_JOB_FAIL_RUN
);
1078 test_blockjob_common(BDRV_DRAIN
, false, TEST_JOB_FAIL_PREPARE
);
1081 static void test_blockjob_error_drain_subtree(void)
1083 test_blockjob_common(BDRV_SUBTREE_DRAIN
, false, TEST_JOB_FAIL_RUN
);
1084 test_blockjob_common(BDRV_SUBTREE_DRAIN
, false, TEST_JOB_FAIL_PREPARE
);
1087 static void test_blockjob_iothread_drain_all(void)
1089 test_blockjob_common(BDRV_DRAIN_ALL
, true, TEST_JOB_SUCCESS
);
1092 static void test_blockjob_iothread_drain(void)
1094 test_blockjob_common(BDRV_DRAIN
, true, TEST_JOB_SUCCESS
);
1097 static void test_blockjob_iothread_drain_subtree(void)
1099 test_blockjob_common(BDRV_SUBTREE_DRAIN
, true, TEST_JOB_SUCCESS
);
1102 static void test_blockjob_iothread_error_drain_all(void)
1104 test_blockjob_common(BDRV_DRAIN_ALL
, true, TEST_JOB_FAIL_RUN
);
1105 test_blockjob_common(BDRV_DRAIN_ALL
, true, TEST_JOB_FAIL_PREPARE
);
1108 static void test_blockjob_iothread_error_drain(void)
1110 test_blockjob_common(BDRV_DRAIN
, true, TEST_JOB_FAIL_RUN
);
1111 test_blockjob_common(BDRV_DRAIN
, true, TEST_JOB_FAIL_PREPARE
);
1114 static void test_blockjob_iothread_error_drain_subtree(void)
1116 test_blockjob_common(BDRV_SUBTREE_DRAIN
, true, TEST_JOB_FAIL_RUN
);
1117 test_blockjob_common(BDRV_SUBTREE_DRAIN
, true, TEST_JOB_FAIL_PREPARE
);
1121 typedef struct BDRVTestTopState
{
1122 BdrvChild
*wait_child
;
1125 static void bdrv_test_top_close(BlockDriverState
*bs
)
1127 BdrvChild
*c
, *next_c
;
1128 QLIST_FOREACH_SAFE(c
, &bs
->children
, next
, next_c
) {
1129 bdrv_unref_child(bs
, c
);
1133 static int coroutine_fn
bdrv_test_top_co_preadv(BlockDriverState
*bs
,
1134 int64_t offset
, int64_t bytes
,
1136 BdrvRequestFlags flags
)
1138 BDRVTestTopState
*tts
= bs
->opaque
;
1139 return bdrv_co_preadv(tts
->wait_child
, offset
, bytes
, qiov
, flags
);
1142 static BlockDriver bdrv_test_top_driver
= {
1143 .format_name
= "test_top_driver",
1144 .instance_size
= sizeof(BDRVTestTopState
),
1146 .bdrv_close
= bdrv_test_top_close
,
1147 .bdrv_co_preadv
= bdrv_test_top_co_preadv
,
1149 .bdrv_child_perm
= bdrv_default_perms
,
1152 typedef struct TestCoDeleteByDrainData
{
1154 bool detach_instead_of_delete
;
1156 } TestCoDeleteByDrainData
;
1158 static void coroutine_fn
test_co_delete_by_drain(void *opaque
)
1160 TestCoDeleteByDrainData
*dbdd
= opaque
;
1161 BlockBackend
*blk
= dbdd
->blk
;
1162 BlockDriverState
*bs
= blk_bs(blk
);
1163 BDRVTestTopState
*tts
= bs
->opaque
;
1164 void *buffer
= g_malloc(65536);
1165 QEMUIOVector qiov
= QEMU_IOVEC_INIT_BUF(qiov
, buffer
, 65536);
1167 /* Pretend some internal write operation from parent to child.
1168 * Important: We have to read from the child, not from the parent!
1169 * Draining works by first propagating it all up the tree to the
1170 * root and then waiting for drainage from root to the leaves
1171 * (protocol nodes). If we have a request waiting on the root,
1172 * everything will be drained before we go back down the tree, but
1173 * we do not want that. We want to be in the middle of draining
1174 * when this following requests returns. */
1175 bdrv_co_preadv(tts
->wait_child
, 0, 65536, &qiov
, 0);
1177 g_assert_cmpint(bs
->refcnt
, ==, 1);
1179 if (!dbdd
->detach_instead_of_delete
) {
1182 BdrvChild
*c
, *next_c
;
1183 QLIST_FOREACH_SAFE(c
, &bs
->children
, next
, next_c
) {
1184 bdrv_unref_child(bs
, c
);
1193 * Test what happens when some BDS has some children, you drain one of
1194 * them and this results in the BDS being deleted.
1196 * If @detach_instead_of_delete is set, the BDS is not going to be
1197 * deleted but will only detach all of its children.
1199 static void do_test_delete_by_drain(bool detach_instead_of_delete
,
1200 enum drain_type drain_type
)
1203 BlockDriverState
*bs
, *child_bs
, *null_bs
;
1204 BDRVTestTopState
*tts
;
1205 TestCoDeleteByDrainData dbdd
;
1208 bs
= bdrv_new_open_driver(&bdrv_test_top_driver
, "top", BDRV_O_RDWR
,
1210 bs
->total_sectors
= 65536 >> BDRV_SECTOR_BITS
;
1213 null_bs
= bdrv_open("null-co://", NULL
, NULL
, BDRV_O_RDWR
| BDRV_O_PROTOCOL
,
1215 bdrv_attach_child(bs
, null_bs
, "null-child", &child_of_bds
,
1216 BDRV_CHILD_DATA
, &error_abort
);
1218 /* This child will be the one to pass to requests through to, and
1219 * it will stall until a drain occurs */
1220 child_bs
= bdrv_new_open_driver(&bdrv_test
, "child", BDRV_O_RDWR
,
1222 child_bs
->total_sectors
= 65536 >> BDRV_SECTOR_BITS
;
1223 /* Takes our reference to child_bs */
1224 tts
->wait_child
= bdrv_attach_child(bs
, child_bs
, "wait-child",
1226 BDRV_CHILD_DATA
| BDRV_CHILD_PRIMARY
,
1229 /* This child is just there to be deleted
1230 * (for detach_instead_of_delete == true) */
1231 null_bs
= bdrv_open("null-co://", NULL
, NULL
, BDRV_O_RDWR
| BDRV_O_PROTOCOL
,
1233 bdrv_attach_child(bs
, null_bs
, "null-child", &child_of_bds
, BDRV_CHILD_DATA
,
1236 blk
= blk_new(qemu_get_aio_context(), BLK_PERM_ALL
, BLK_PERM_ALL
);
1237 blk_insert_bs(blk
, bs
, &error_abort
);
1239 /* Referenced by blk now */
1242 g_assert_cmpint(bs
->refcnt
, ==, 1);
1243 g_assert_cmpint(child_bs
->refcnt
, ==, 1);
1244 g_assert_cmpint(null_bs
->refcnt
, ==, 1);
1247 dbdd
= (TestCoDeleteByDrainData
){
1249 .detach_instead_of_delete
= detach_instead_of_delete
,
1252 co
= qemu_coroutine_create(test_co_delete_by_drain
, &dbdd
);
1253 qemu_coroutine_enter(co
);
1255 /* Drain the child while the read operation is still pending.
1256 * This should result in the operation finishing and
1257 * test_co_delete_by_drain() resuming. Thus, @bs will be deleted
1258 * and the coroutine will exit while this drain operation is still
1260 switch (drain_type
) {
1263 bdrv_drain(child_bs
);
1264 bdrv_unref(child_bs
);
1266 case BDRV_SUBTREE_DRAIN
:
1267 /* Would have to ref/unref bs here for !detach_instead_of_delete, but
1268 * then the whole test becomes pointless because the graph changes
1269 * don't occur during the drain any more. */
1270 assert(detach_instead_of_delete
);
1271 bdrv_subtree_drained_begin(bs
);
1272 bdrv_subtree_drained_end(bs
);
1274 case BDRV_DRAIN_ALL
:
1275 bdrv_drain_all_begin();
1276 bdrv_drain_all_end();
1279 g_assert_not_reached();
1282 while (!dbdd
.done
) {
1283 aio_poll(qemu_get_aio_context(), true);
1286 if (detach_instead_of_delete
) {
1287 /* Here, the reference has not passed over to the coroutine,
1288 * so we have to delete the BB ourselves */
1293 static void test_delete_by_drain(void)
1295 do_test_delete_by_drain(false, BDRV_DRAIN
);
1298 static void test_detach_by_drain_all(void)
1300 do_test_delete_by_drain(true, BDRV_DRAIN_ALL
);
1303 static void test_detach_by_drain(void)
1305 do_test_delete_by_drain(true, BDRV_DRAIN
);
1308 static void test_detach_by_drain_subtree(void)
1310 do_test_delete_by_drain(true, BDRV_SUBTREE_DRAIN
);
1314 struct detach_by_parent_data
{
1315 BlockDriverState
*parent_b
;
1317 BlockDriverState
*c
;
1321 static struct detach_by_parent_data detach_by_parent_data
;
1323 static void detach_indirect_bh(void *opaque
)
1325 struct detach_by_parent_data
*data
= opaque
;
1327 bdrv_unref_child(data
->parent_b
, data
->child_b
);
1330 data
->child_c
= bdrv_attach_child(data
->parent_b
, data
->c
, "PB-C",
1331 &child_of_bds
, BDRV_CHILD_DATA
,
1335 static void detach_by_parent_aio_cb(void *opaque
, int ret
)
1337 struct detach_by_parent_data
*data
= &detach_by_parent_data
;
1339 g_assert_cmpint(ret
, ==, 0);
1340 if (data
->by_parent_cb
) {
1341 detach_indirect_bh(data
);
1345 static void detach_by_driver_cb_drained_begin(BdrvChild
*child
)
1347 aio_bh_schedule_oneshot(qemu_get_current_aio_context(),
1348 detach_indirect_bh
, &detach_by_parent_data
);
1349 child_of_bds
.drained_begin(child
);
1352 static BdrvChildClass detach_by_driver_cb_class
;
1361 * by_parent_cb == true: Test that parent callbacks don't poll
1363 * PA has a pending write request whose callback changes the child nodes of
1364 * PB: It removes B and adds C instead. The subtree of PB is drained, which
1365 * will indirectly drain the write request, too.
1367 * by_parent_cb == false: Test that bdrv_drain_invoke() doesn't poll
1369 * PA's BdrvChildClass has a .drained_begin callback that schedules a BH
1370 * that does the same graph change. If bdrv_drain_invoke() calls it, the
1371 * state is messed up, but if it is only polled in the single
1372 * BDRV_POLL_WHILE() at the end of the drain, this should work fine.
1374 static void test_detach_indirect(bool by_parent_cb
)
1377 BlockDriverState
*parent_a
, *parent_b
, *a
, *b
, *c
;
1378 BdrvChild
*child_a
, *child_b
;
1381 QEMUIOVector qiov
= QEMU_IOVEC_INIT_BUF(qiov
, NULL
, 0);
1383 if (!by_parent_cb
) {
1384 detach_by_driver_cb_class
= child_of_bds
;
1385 detach_by_driver_cb_class
.drained_begin
=
1386 detach_by_driver_cb_drained_begin
;
1389 /* Create all involved nodes */
1390 parent_a
= bdrv_new_open_driver(&bdrv_test
, "parent-a", BDRV_O_RDWR
,
1392 parent_b
= bdrv_new_open_driver(&bdrv_test
, "parent-b", 0,
1395 a
= bdrv_new_open_driver(&bdrv_test
, "a", BDRV_O_RDWR
, &error_abort
);
1396 b
= bdrv_new_open_driver(&bdrv_test
, "b", BDRV_O_RDWR
, &error_abort
);
1397 c
= bdrv_new_open_driver(&bdrv_test
, "c", BDRV_O_RDWR
, &error_abort
);
1399 /* blk is a BB for parent-a */
1400 blk
= blk_new(qemu_get_aio_context(), BLK_PERM_ALL
, BLK_PERM_ALL
);
1401 blk_insert_bs(blk
, parent_a
, &error_abort
);
1402 bdrv_unref(parent_a
);
1404 /* If we want to get bdrv_drain_invoke() to call aio_poll(), the driver
1405 * callback must not return immediately. */
1406 if (!by_parent_cb
) {
1407 BDRVTestState
*s
= parent_a
->opaque
;
1408 s
->sleep_in_drain_begin
= true;
1411 /* Set child relationships */
1414 child_b
= bdrv_attach_child(parent_b
, b
, "PB-B", &child_of_bds
,
1415 BDRV_CHILD_DATA
, &error_abort
);
1416 child_a
= bdrv_attach_child(parent_b
, a
, "PB-A", &child_of_bds
,
1417 BDRV_CHILD_COW
, &error_abort
);
1420 bdrv_attach_child(parent_a
, a
, "PA-A",
1421 by_parent_cb
? &child_of_bds
: &detach_by_driver_cb_class
,
1422 BDRV_CHILD_DATA
, &error_abort
);
1424 g_assert_cmpint(parent_a
->refcnt
, ==, 1);
1425 g_assert_cmpint(parent_b
->refcnt
, ==, 1);
1426 g_assert_cmpint(a
->refcnt
, ==, 3);
1427 g_assert_cmpint(b
->refcnt
, ==, 2);
1428 g_assert_cmpint(c
->refcnt
, ==, 1);
1430 g_assert(QLIST_FIRST(&parent_b
->children
) == child_a
);
1431 g_assert(QLIST_NEXT(child_a
, next
) == child_b
);
1432 g_assert(QLIST_NEXT(child_b
, next
) == NULL
);
1434 /* Start the evil write request */
1435 detach_by_parent_data
= (struct detach_by_parent_data
) {
1436 .parent_b
= parent_b
,
1439 .by_parent_cb
= by_parent_cb
,
1441 acb
= blk_aio_preadv(blk
, 0, &qiov
, 0, detach_by_parent_aio_cb
, NULL
);
1442 g_assert(acb
!= NULL
);
1444 /* Drain and check the expected result */
1445 bdrv_subtree_drained_begin(parent_b
);
1447 g_assert(detach_by_parent_data
.child_c
!= NULL
);
1449 g_assert_cmpint(parent_a
->refcnt
, ==, 1);
1450 g_assert_cmpint(parent_b
->refcnt
, ==, 1);
1451 g_assert_cmpint(a
->refcnt
, ==, 3);
1452 g_assert_cmpint(b
->refcnt
, ==, 1);
1453 g_assert_cmpint(c
->refcnt
, ==, 2);
1455 g_assert(QLIST_FIRST(&parent_b
->children
) == detach_by_parent_data
.child_c
);
1456 g_assert(QLIST_NEXT(detach_by_parent_data
.child_c
, next
) == child_a
);
1457 g_assert(QLIST_NEXT(child_a
, next
) == NULL
);
1459 g_assert_cmpint(parent_a
->quiesce_counter
, ==, 1);
1460 g_assert_cmpint(parent_b
->quiesce_counter
, ==, 1);
1461 g_assert_cmpint(a
->quiesce_counter
, ==, 1);
1462 g_assert_cmpint(b
->quiesce_counter
, ==, 0);
1463 g_assert_cmpint(c
->quiesce_counter
, ==, 1);
1465 bdrv_subtree_drained_end(parent_b
);
1467 bdrv_unref(parent_b
);
1470 g_assert_cmpint(a
->refcnt
, ==, 1);
1471 g_assert_cmpint(b
->refcnt
, ==, 1);
1472 g_assert_cmpint(c
->refcnt
, ==, 1);
1478 static void test_detach_by_parent_cb(void)
1480 test_detach_indirect(true);
1483 static void test_detach_by_driver_cb(void)
1485 test_detach_indirect(false);
1488 static void test_append_to_drained(void)
1491 BlockDriverState
*base
, *overlay
;
1492 BDRVTestState
*base_s
, *overlay_s
;
1494 blk
= blk_new(qemu_get_aio_context(), BLK_PERM_ALL
, BLK_PERM_ALL
);
1495 base
= bdrv_new_open_driver(&bdrv_test
, "base", BDRV_O_RDWR
, &error_abort
);
1496 base_s
= base
->opaque
;
1497 blk_insert_bs(blk
, base
, &error_abort
);
1499 overlay
= bdrv_new_open_driver(&bdrv_test
, "overlay", BDRV_O_RDWR
,
1501 overlay_s
= overlay
->opaque
;
1503 do_drain_begin(BDRV_DRAIN
, base
);
1504 g_assert_cmpint(base
->quiesce_counter
, ==, 1);
1505 g_assert_cmpint(base_s
->drain_count
, ==, 1);
1506 g_assert_cmpint(base
->in_flight
, ==, 0);
1508 bdrv_append(overlay
, base
, &error_abort
);
1509 g_assert_cmpint(base
->in_flight
, ==, 0);
1510 g_assert_cmpint(overlay
->in_flight
, ==, 0);
1512 g_assert_cmpint(base
->quiesce_counter
, ==, 1);
1513 g_assert_cmpint(base_s
->drain_count
, ==, 1);
1514 g_assert_cmpint(overlay
->quiesce_counter
, ==, 1);
1515 g_assert_cmpint(overlay_s
->drain_count
, ==, 1);
1517 do_drain_end(BDRV_DRAIN
, base
);
1519 g_assert_cmpint(base
->quiesce_counter
, ==, 0);
1520 g_assert_cmpint(base_s
->drain_count
, ==, 0);
1521 g_assert_cmpint(overlay
->quiesce_counter
, ==, 0);
1522 g_assert_cmpint(overlay_s
->drain_count
, ==, 0);
1524 bdrv_unref(overlay
);
1529 static void test_set_aio_context(void)
1531 BlockDriverState
*bs
;
1532 IOThread
*a
= iothread_new();
1533 IOThread
*b
= iothread_new();
1534 AioContext
*ctx_a
= iothread_get_aio_context(a
);
1535 AioContext
*ctx_b
= iothread_get_aio_context(b
);
1537 bs
= bdrv_new_open_driver(&bdrv_test
, "test-node", BDRV_O_RDWR
,
1540 bdrv_drained_begin(bs
);
1541 bdrv_try_change_aio_context(bs
, ctx_a
, NULL
, &error_abort
);
1543 aio_context_acquire(ctx_a
);
1544 bdrv_drained_end(bs
);
1546 bdrv_drained_begin(bs
);
1547 bdrv_try_change_aio_context(bs
, ctx_b
, NULL
, &error_abort
);
1548 aio_context_release(ctx_a
);
1549 aio_context_acquire(ctx_b
);
1550 bdrv_try_change_aio_context(bs
, qemu_get_aio_context(), NULL
, &error_abort
);
1551 aio_context_release(ctx_b
);
1552 bdrv_drained_end(bs
);
1560 typedef struct TestDropBackingBlockJob
{
1562 bool should_complete
;
1564 BlockDriverState
*detach_also
;
1565 BlockDriverState
*bs
;
1566 } TestDropBackingBlockJob
;
1568 static int coroutine_fn
test_drop_backing_job_run(Job
*job
, Error
**errp
)
1570 TestDropBackingBlockJob
*s
=
1571 container_of(job
, TestDropBackingBlockJob
, common
.job
);
1573 while (!s
->should_complete
) {
1574 job_sleep_ns(job
, 0);
1580 static void test_drop_backing_job_commit(Job
*job
)
1582 TestDropBackingBlockJob
*s
=
1583 container_of(job
, TestDropBackingBlockJob
, common
.job
);
1585 bdrv_set_backing_hd(s
->bs
, NULL
, &error_abort
);
1586 bdrv_set_backing_hd(s
->detach_also
, NULL
, &error_abort
);
1588 *s
->did_complete
= true;
1591 static const BlockJobDriver test_drop_backing_job_driver
= {
1593 .instance_size
= sizeof(TestDropBackingBlockJob
),
1594 .free
= block_job_free
,
1595 .user_resume
= block_job_user_resume
,
1596 .run
= test_drop_backing_job_run
,
1597 .commit
= test_drop_backing_job_commit
,
1602 * Creates a child node with three parent nodes on it, and then runs a
1603 * block job on the final one, parent-node-2.
1605 * The job is then asked to complete before a section where the child
1608 * Ending this section will undrain the child's parents, first
1609 * parent-node-2, then parent-node-1, then parent-node-0 -- the parent
1610 * list is in reverse order of how they were added. Ending the drain
1611 * on parent-node-2 will resume the job, thus completing it and
1612 * scheduling job_exit().
1614 * Ending the drain on parent-node-1 will poll the AioContext, which
1615 * lets job_exit() and thus test_drop_backing_job_commit() run. That
1616 * function first removes the child as parent-node-2's backing file.
1618 * In old (and buggy) implementations, there are two problems with
1620 * (A) bdrv_drain_invoke() polls for every node that leaves the
1621 * drained section. This means that job_exit() is scheduled
1622 * before the child has left the drained section. Its
1623 * quiesce_counter is therefore still 1 when it is removed from
1626 * (B) bdrv_replace_child_noperm() calls drained_end() on the old
1627 * child's parents as many times as the child is quiesced. This
1628 * means it will call drained_end() on parent-node-2 once.
1629 * Because parent-node-2 is no longer quiesced at this point, this
1632 * bdrv_replace_child_noperm() therefore must call drained_end() on
1633 * the parent only if it really is still drained because the child is
1636 * If removing child from parent-node-2 was successful (as it should
1637 * be), test_drop_backing_job_commit() will then also remove the child
1638 * from parent-node-0.
1640 * With an old version of our drain infrastructure ((A) above), that
1641 * resulted in the following flow:
1643 * 1. child attempts to leave its drained section. The call recurses
1646 * 2. parent-node-2 leaves the drained section. Polling in
1647 * bdrv_drain_invoke() will schedule job_exit().
1649 * 3. parent-node-1 leaves the drained section. Polling in
1650 * bdrv_drain_invoke() will run job_exit(), thus disconnecting
1651 * parent-node-0 from the child node.
1653 * 4. bdrv_parent_drained_end() uses a QLIST_FOREACH_SAFE() loop to
1654 * iterate over the parents. Thus, it now accesses the BdrvChild
1655 * object that used to connect parent-node-0 and the child node.
1656 * However, that object no longer exists, so it accesses a dangling
1659 * The solution is to only poll once when running a bdrv_drained_end()
1660 * operation, specifically at the end when all drained_end()
1661 * operations for all involved nodes have been scheduled.
1662 * Note that this also solves (A) above, thus hiding (B).
1664 static void test_blockjob_commit_by_drained_end(void)
1666 BlockDriverState
*bs_child
, *bs_parents
[3];
1667 TestDropBackingBlockJob
*job
;
1668 bool job_has_completed
= false;
1671 bs_child
= bdrv_new_open_driver(&bdrv_test
, "child-node", BDRV_O_RDWR
,
1674 for (i
= 0; i
< 3; i
++) {
1676 snprintf(name
, sizeof(name
), "parent-node-%i", i
);
1677 bs_parents
[i
] = bdrv_new_open_driver(&bdrv_test
, name
, BDRV_O_RDWR
,
1679 bdrv_set_backing_hd(bs_parents
[i
], bs_child
, &error_abort
);
1682 job
= block_job_create("job", &test_drop_backing_job_driver
, NULL
,
1683 bs_parents
[2], 0, BLK_PERM_ALL
, 0, 0, NULL
, NULL
,
1685 job
->bs
= bs_parents
[2];
1687 job
->detach_also
= bs_parents
[0];
1688 job
->did_complete
= &job_has_completed
;
1690 job_start(&job
->common
.job
);
1692 job
->should_complete
= true;
1693 bdrv_drained_begin(bs_child
);
1694 g_assert(!job_has_completed
);
1695 bdrv_drained_end(bs_child
);
1696 g_assert(job_has_completed
);
1698 bdrv_unref(bs_parents
[0]);
1699 bdrv_unref(bs_parents
[1]);
1700 bdrv_unref(bs_parents
[2]);
1701 bdrv_unref(bs_child
);
1705 typedef struct TestSimpleBlockJob
{
1707 bool should_complete
;
1709 } TestSimpleBlockJob
;
1711 static int coroutine_fn
test_simple_job_run(Job
*job
, Error
**errp
)
1713 TestSimpleBlockJob
*s
= container_of(job
, TestSimpleBlockJob
, common
.job
);
1715 while (!s
->should_complete
) {
1716 job_sleep_ns(job
, 0);
1722 static void test_simple_job_clean(Job
*job
)
1724 TestSimpleBlockJob
*s
= container_of(job
, TestSimpleBlockJob
, common
.job
);
1725 *s
->did_complete
= true;
1728 static const BlockJobDriver test_simple_job_driver
= {
1730 .instance_size
= sizeof(TestSimpleBlockJob
),
1731 .free
= block_job_free
,
1732 .user_resume
= block_job_user_resume
,
1733 .run
= test_simple_job_run
,
1734 .clean
= test_simple_job_clean
,
1738 static int drop_intermediate_poll_update_filename(BdrvChild
*child
,
1739 BlockDriverState
*new_base
,
1740 const char *filename
,
1744 * We are free to poll here, which may change the block graph, if
1745 * it is not drained.
1748 /* If the job is not drained: Complete it, schedule job_exit() */
1749 aio_poll(qemu_get_current_aio_context(), false);
1750 /* If the job is not drained: Run job_exit(), finish the job */
1751 aio_poll(qemu_get_current_aio_context(), false);
1757 * Test a poll in the midst of bdrv_drop_intermediate().
1759 * bdrv_drop_intermediate() calls BdrvChildClass.update_filename(),
1760 * which can yield or poll. This may lead to graph changes, unless
1761 * the whole subtree in question is drained.
1763 * We test this on the following graph:
1779 * node-2 --chain--> node-1 --chain--> node-0
1781 * We drop node-1 with bdrv_drop_intermediate(top=node-1, base=node-0).
1783 * This first updates node-2's backing filename by invoking
1784 * drop_intermediate_poll_update_filename(), which polls twice. This
1785 * causes the job to finish, which in turns causes the job-node to be
1788 * bdrv_drop_intermediate() uses a QLIST_FOREACH_SAFE() loop, so it
1789 * already has a pointer to the BdrvChild edge between job-node and
1790 * node-1. When it tries to handle that edge, we probably get a
1791 * segmentation fault because the object no longer exists.
1794 * The solution is for bdrv_drop_intermediate() to drain top's
1795 * subtree. This prevents graph changes from happening just because
1796 * BdrvChildClass.update_filename() yields or polls. Thus, the block
1797 * job is paused during that drained section and must finish before or
1800 * (In addition, bdrv_replace_child() must keep the job paused.)
1802 static void test_drop_intermediate_poll(void)
1804 static BdrvChildClass chain_child_class
;
1805 BlockDriverState
*chain
[3];
1806 TestSimpleBlockJob
*job
;
1807 BlockDriverState
*job_node
;
1808 bool job_has_completed
= false;
1812 chain_child_class
= child_of_bds
;
1813 chain_child_class
.update_filename
= drop_intermediate_poll_update_filename
;
1815 for (i
= 0; i
< 3; i
++) {
1817 snprintf(name
, 32, "node-%i", i
);
1819 chain
[i
] = bdrv_new_open_driver(&bdrv_test
, name
, 0, &error_abort
);
1822 job_node
= bdrv_new_open_driver(&bdrv_test
, "job-node", BDRV_O_RDWR
,
1824 bdrv_set_backing_hd(job_node
, chain
[1], &error_abort
);
1827 * Establish the chain last, so the chain links are the first
1828 * elements in the BDS.parents lists
1830 for (i
= 0; i
< 3; i
++) {
1832 /* Takes the reference to chain[i - 1] */
1833 bdrv_attach_child(chain
[i
], chain
[i
- 1], "chain",
1834 &chain_child_class
, BDRV_CHILD_COW
, &error_abort
);
1838 job
= block_job_create("job", &test_simple_job_driver
, NULL
, job_node
,
1839 0, BLK_PERM_ALL
, 0, 0, NULL
, NULL
, &error_abort
);
1841 /* The job has a reference now */
1842 bdrv_unref(job_node
);
1844 job
->did_complete
= &job_has_completed
;
1846 job_start(&job
->common
.job
);
1847 job
->should_complete
= true;
1849 g_assert(!job_has_completed
);
1850 ret
= bdrv_drop_intermediate(chain
[1], chain
[0], NULL
);
1852 g_assert(job_has_completed
);
1854 bdrv_unref(chain
[2]);
1858 typedef struct BDRVReplaceTestState
{
1865 bool yield_before_read
;
1867 Coroutine
*drain_co
;
1868 } BDRVReplaceTestState
;
1870 static void bdrv_replace_test_close(BlockDriverState
*bs
)
1875 * If @bs has a backing file:
1876 * Yield if .yield_before_read is true (and wait for drain_begin to
1878 * Forward the read to bs->backing. Set .has_read to true.
1879 * If drain_begin has woken us, wake it in turn.
1882 * Set .has_read to true and return success.
1884 static int coroutine_fn
bdrv_replace_test_co_preadv(BlockDriverState
*bs
,
1888 BdrvRequestFlags flags
)
1890 BDRVReplaceTestState
*s
= bs
->opaque
;
1895 g_assert(!s
->drain_count
);
1897 s
->io_co
= qemu_coroutine_self();
1898 if (s
->yield_before_read
) {
1899 s
->yield_before_read
= false;
1900 qemu_coroutine_yield();
1904 ret
= bdrv_co_preadv(bs
->backing
, offset
, bytes
, qiov
, 0);
1907 /* Wake up drain_co if it runs */
1909 aio_co_wake(s
->drain_co
);
1920 * If .drain_count is 0, wake up .io_co if there is one; and set
1922 * Increment .drain_count.
1924 static void coroutine_fn
bdrv_replace_test_co_drain_begin(BlockDriverState
*bs
)
1926 BDRVReplaceTestState
*s
= bs
->opaque
;
1928 if (!s
->drain_count
) {
1929 /* Keep waking io_co up until it is done */
1930 s
->drain_co
= qemu_coroutine_self();
1932 aio_co_wake(s
->io_co
);
1934 qemu_coroutine_yield();
1938 s
->was_drained
= true;
1944 * Reduce .drain_count, set .was_undrained once it reaches 0.
1945 * If .drain_count reaches 0 and the node has a backing file, issue a
1948 static void coroutine_fn
bdrv_replace_test_co_drain_end(BlockDriverState
*bs
)
1950 BDRVReplaceTestState
*s
= bs
->opaque
;
1952 g_assert(s
->drain_count
> 0);
1953 if (!--s
->drain_count
) {
1956 s
->was_undrained
= true;
1960 QEMUIOVector qiov
= QEMU_IOVEC_INIT_BUF(qiov
, &data
, 1);
1962 /* Queue a read request post-drain */
1963 ret
= bdrv_replace_test_co_preadv(bs
, 0, 1, &qiov
, 0);
1969 static BlockDriver bdrv_replace_test
= {
1970 .format_name
= "replace_test",
1971 .instance_size
= sizeof(BDRVReplaceTestState
),
1972 .supports_backing
= true,
1974 .bdrv_close
= bdrv_replace_test_close
,
1975 .bdrv_co_preadv
= bdrv_replace_test_co_preadv
,
1977 .bdrv_co_drain_begin
= bdrv_replace_test_co_drain_begin
,
1978 .bdrv_co_drain_end
= bdrv_replace_test_co_drain_end
,
1980 .bdrv_child_perm
= bdrv_default_perms
,
1983 static void coroutine_fn
test_replace_child_mid_drain_read_co(void *opaque
)
1988 ret
= blk_co_pread(opaque
, 0, 1, &data
, 0);
1993 * We test two things:
1994 * (1) bdrv_replace_child_noperm() must not undrain the parent if both
1995 * children are drained.
1996 * (2) bdrv_replace_child_noperm() must never flush I/O requests to a
1997 * drained child. If the old child is drained, it must flush I/O
1998 * requests after the new one has been attached. If the new child
1999 * is drained, it must flush I/O requests before the old one is
2002 * To do so, we create one parent node and two child nodes; then
2003 * attach one of the children (old_child_bs) to the parent, then
2004 * drain both old_child_bs and new_child_bs according to
2005 * old_drain_count and new_drain_count, respectively, and finally
2006 * we invoke bdrv_replace_node() to replace old_child_bs by
2009 * The test block driver we use here (bdrv_replace_test) has a read
2011 * - For the parent node, can optionally yield, and then forwards the
2012 * read to bdrv_preadv(),
2013 * - For the child node, just returns immediately.
2015 * If the read yields, the drain_begin function will wake it up.
2017 * The drain_end function issues a read on the parent once it is fully
2018 * undrained (which simulates requests starting to come in again).
2020 static void do_test_replace_child_mid_drain(int old_drain_count
,
2021 int new_drain_count
)
2023 BlockBackend
*parent_blk
;
2024 BlockDriverState
*parent_bs
;
2025 BlockDriverState
*old_child_bs
, *new_child_bs
;
2026 BDRVReplaceTestState
*parent_s
;
2027 BDRVReplaceTestState
*old_child_s
, *new_child_s
;
2031 parent_bs
= bdrv_new_open_driver(&bdrv_replace_test
, "parent", 0,
2033 parent_s
= parent_bs
->opaque
;
2035 parent_blk
= blk_new(qemu_get_aio_context(),
2036 BLK_PERM_CONSISTENT_READ
, BLK_PERM_ALL
);
2037 blk_insert_bs(parent_blk
, parent_bs
, &error_abort
);
2039 old_child_bs
= bdrv_new_open_driver(&bdrv_replace_test
, "old-child", 0,
2041 new_child_bs
= bdrv_new_open_driver(&bdrv_replace_test
, "new-child", 0,
2043 old_child_s
= old_child_bs
->opaque
;
2044 new_child_s
= new_child_bs
->opaque
;
2046 /* So that we can read something */
2047 parent_bs
->total_sectors
= 1;
2048 old_child_bs
->total_sectors
= 1;
2049 new_child_bs
->total_sectors
= 1;
2051 bdrv_ref(old_child_bs
);
2052 bdrv_attach_child(parent_bs
, old_child_bs
, "child", &child_of_bds
,
2053 BDRV_CHILD_COW
, &error_abort
);
2055 for (i
= 0; i
< old_drain_count
; i
++) {
2056 bdrv_drained_begin(old_child_bs
);
2058 for (i
= 0; i
< new_drain_count
; i
++) {
2059 bdrv_drained_begin(new_child_bs
);
2062 if (!old_drain_count
) {
2064 * Start a read operation that will yield, so it will not
2065 * complete before the node is drained.
2067 parent_s
->yield_before_read
= true;
2068 io_co
= qemu_coroutine_create(test_replace_child_mid_drain_read_co
,
2070 qemu_coroutine_enter(io_co
);
2073 /* If we have started a read operation, it should have yielded */
2074 g_assert(!parent_s
->has_read
);
2076 /* Reset drained status so we can see what bdrv_replace_node() does */
2077 parent_s
->was_drained
= false;
2078 parent_s
->was_undrained
= false;
2080 g_assert(parent_bs
->quiesce_counter
== old_drain_count
);
2081 bdrv_replace_node(old_child_bs
, new_child_bs
, &error_abort
);
2082 g_assert(parent_bs
->quiesce_counter
== new_drain_count
);
2084 if (!old_drain_count
&& !new_drain_count
) {
2086 * From undrained to undrained drains and undrains the parent,
2087 * because bdrv_replace_node() contains a drained section for
2090 g_assert(parent_s
->was_drained
&& parent_s
->was_undrained
);
2091 } else if (!old_drain_count
&& new_drain_count
) {
2093 * From undrained to drained should drain the parent and keep
2096 g_assert(parent_s
->was_drained
&& !parent_s
->was_undrained
);
2097 } else if (old_drain_count
&& !new_drain_count
) {
2099 * From drained to undrained should undrain the parent and
2102 g_assert(!parent_s
->was_drained
&& parent_s
->was_undrained
);
2103 } else /* if (old_drain_count && new_drain_count) */ {
2105 * From drained to drained must not undrain the parent at any
2108 g_assert(!parent_s
->was_drained
&& !parent_s
->was_undrained
);
2111 if (!old_drain_count
|| !new_drain_count
) {
2113 * If !old_drain_count, we have started a read request before
2114 * bdrv_replace_node(). If !new_drain_count, the parent must
2115 * have been undrained at some point, and
2116 * bdrv_replace_test_co_drain_end() starts a read request
2119 g_assert(parent_s
->has_read
);
2122 * If the parent was never undrained, there is no way to start
2125 g_assert(!parent_s
->has_read
);
2128 /* A drained child must have not received any request */
2129 g_assert(!(old_drain_count
&& old_child_s
->has_read
));
2130 g_assert(!(new_drain_count
&& new_child_s
->has_read
));
2132 for (i
= 0; i
< new_drain_count
; i
++) {
2133 bdrv_drained_end(new_child_bs
);
2135 for (i
= 0; i
< old_drain_count
; i
++) {
2136 bdrv_drained_end(old_child_bs
);
2140 * By now, bdrv_replace_test_co_drain_end() must have been called
2141 * at some point while the new child was attached to the parent.
2143 g_assert(parent_s
->has_read
);
2144 g_assert(new_child_s
->has_read
);
2146 blk_unref(parent_blk
);
2147 bdrv_unref(parent_bs
);
2148 bdrv_unref(old_child_bs
);
2149 bdrv_unref(new_child_bs
);
2152 static void test_replace_child_mid_drain(void)
2154 int old_drain_count
, new_drain_count
;
2156 for (old_drain_count
= 0; old_drain_count
< 2; old_drain_count
++) {
2157 for (new_drain_count
= 0; new_drain_count
< 2; new_drain_count
++) {
2158 do_test_replace_child_mid_drain(old_drain_count
, new_drain_count
);
2163 int main(int argc
, char **argv
)
2168 qemu_init_main_loop(&error_abort
);
2170 g_test_init(&argc
, &argv
, NULL
);
2171 qemu_event_init(&done_event
, false);
2173 g_test_add_func("/bdrv-drain/driver-cb/drain_all", test_drv_cb_drain_all
);
2174 g_test_add_func("/bdrv-drain/driver-cb/drain", test_drv_cb_drain
);
2175 g_test_add_func("/bdrv-drain/driver-cb/drain_subtree",
2176 test_drv_cb_drain_subtree
);
2178 g_test_add_func("/bdrv-drain/driver-cb/co/drain_all",
2179 test_drv_cb_co_drain_all
);
2180 g_test_add_func("/bdrv-drain/driver-cb/co/drain", test_drv_cb_co_drain
);
2181 g_test_add_func("/bdrv-drain/driver-cb/co/drain_subtree",
2182 test_drv_cb_co_drain_subtree
);
2185 g_test_add_func("/bdrv-drain/quiesce/drain_all", test_quiesce_drain_all
);
2186 g_test_add_func("/bdrv-drain/quiesce/drain", test_quiesce_drain
);
2187 g_test_add_func("/bdrv-drain/quiesce/drain_subtree",
2188 test_quiesce_drain_subtree
);
2190 g_test_add_func("/bdrv-drain/quiesce/co/drain_all",
2191 test_quiesce_co_drain_all
);
2192 g_test_add_func("/bdrv-drain/quiesce/co/drain", test_quiesce_co_drain
);
2193 g_test_add_func("/bdrv-drain/quiesce/co/drain_subtree",
2194 test_quiesce_co_drain_subtree
);
2196 g_test_add_func("/bdrv-drain/nested", test_nested
);
2197 g_test_add_func("/bdrv-drain/multiparent", test_multiparent
);
2199 g_test_add_func("/bdrv-drain/graph-change/drain_subtree",
2200 test_graph_change_drain_subtree
);
2201 g_test_add_func("/bdrv-drain/graph-change/drain_all",
2202 test_graph_change_drain_all
);
2204 g_test_add_func("/bdrv-drain/iothread/drain_all", test_iothread_drain_all
);
2205 g_test_add_func("/bdrv-drain/iothread/drain", test_iothread_drain
);
2206 g_test_add_func("/bdrv-drain/iothread/drain_subtree",
2207 test_iothread_drain_subtree
);
2209 g_test_add_func("/bdrv-drain/blockjob/drain_all", test_blockjob_drain_all
);
2210 g_test_add_func("/bdrv-drain/blockjob/drain", test_blockjob_drain
);
2211 g_test_add_func("/bdrv-drain/blockjob/drain_subtree",
2212 test_blockjob_drain_subtree
);
2214 g_test_add_func("/bdrv-drain/blockjob/error/drain_all",
2215 test_blockjob_error_drain_all
);
2216 g_test_add_func("/bdrv-drain/blockjob/error/drain",
2217 test_blockjob_error_drain
);
2218 g_test_add_func("/bdrv-drain/blockjob/error/drain_subtree",
2219 test_blockjob_error_drain_subtree
);
2221 g_test_add_func("/bdrv-drain/blockjob/iothread/drain_all",
2222 test_blockjob_iothread_drain_all
);
2223 g_test_add_func("/bdrv-drain/blockjob/iothread/drain",
2224 test_blockjob_iothread_drain
);
2225 g_test_add_func("/bdrv-drain/blockjob/iothread/drain_subtree",
2226 test_blockjob_iothread_drain_subtree
);
2228 g_test_add_func("/bdrv-drain/blockjob/iothread/error/drain_all",
2229 test_blockjob_iothread_error_drain_all
);
2230 g_test_add_func("/bdrv-drain/blockjob/iothread/error/drain",
2231 test_blockjob_iothread_error_drain
);
2232 g_test_add_func("/bdrv-drain/blockjob/iothread/error/drain_subtree",
2233 test_blockjob_iothread_error_drain_subtree
);
2235 g_test_add_func("/bdrv-drain/deletion/drain", test_delete_by_drain
);
2236 g_test_add_func("/bdrv-drain/detach/drain_all", test_detach_by_drain_all
);
2237 g_test_add_func("/bdrv-drain/detach/drain", test_detach_by_drain
);
2238 g_test_add_func("/bdrv-drain/detach/drain_subtree", test_detach_by_drain_subtree
);
2239 g_test_add_func("/bdrv-drain/detach/parent_cb", test_detach_by_parent_cb
);
2240 g_test_add_func("/bdrv-drain/detach/driver_cb", test_detach_by_driver_cb
);
2242 g_test_add_func("/bdrv-drain/attach/drain", test_append_to_drained
);
2244 g_test_add_func("/bdrv-drain/set_aio_context", test_set_aio_context
);
2246 g_test_add_func("/bdrv-drain/blockjob/commit_by_drained_end",
2247 test_blockjob_commit_by_drained_end
);
2249 g_test_add_func("/bdrv-drain/bdrv_drop_intermediate/poll",
2250 test_drop_intermediate_poll
);
2252 g_test_add_func("/bdrv-drain/replace_child/mid-drain",
2253 test_replace_child_mid_drain
);
2256 qemu_event_destroy(&done_event
);