ppc/pnv: add a PSI bridge model for POWER9
[qemu.git] / tests / test-bdrv-drain.c
blobeda90750eb3ca2bdbd9ab4742823fad1c6c26b13
1 /*
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
22 * THE SOFTWARE.
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 "iothread.h"
32 static QemuEvent done_event;
34 typedef struct BDRVTestState {
35 int drain_count;
36 AioContext *bh_indirection_ctx;
37 bool sleep_in_drain_begin;
38 } BDRVTestState;
40 static void coroutine_fn bdrv_test_co_drain_begin(BlockDriverState *bs)
42 BDRVTestState *s = bs->opaque;
43 s->drain_count++;
44 if (s->sleep_in_drain_begin) {
45 qemu_co_sleep_ns(QEMU_CLOCK_REALTIME, 100000);
49 static void coroutine_fn bdrv_test_co_drain_end(BlockDriverState *bs)
51 BDRVTestState *s = bs->opaque;
52 s->drain_count--;
55 static void bdrv_test_close(BlockDriverState *bs)
57 BDRVTestState *s = bs->opaque;
58 g_assert_cmpint(s->drain_count, >, 0);
61 static void co_reenter_bh(void *opaque)
63 aio_co_wake(opaque);
66 static int coroutine_fn bdrv_test_co_preadv(BlockDriverState *bs,
67 uint64_t offset, uint64_t bytes,
68 QEMUIOVector *qiov, int flags)
70 BDRVTestState *s = bs->opaque;
72 /* We want this request to stay until the polling loop in drain waits for
73 * it to complete. We need to sleep a while as bdrv_drain_invoke() comes
74 * first and polls its result, too, but it shouldn't accidentally complete
75 * this request yet. */
76 qemu_co_sleep_ns(QEMU_CLOCK_REALTIME, 100000);
78 if (s->bh_indirection_ctx) {
79 aio_bh_schedule_oneshot(s->bh_indirection_ctx, co_reenter_bh,
80 qemu_coroutine_self());
81 qemu_coroutine_yield();
84 return 0;
87 static void bdrv_test_child_perm(BlockDriverState *bs, BdrvChild *c,
88 const BdrvChildRole *role,
89 BlockReopenQueue *reopen_queue,
90 uint64_t perm, uint64_t shared,
91 uint64_t *nperm, uint64_t *nshared)
93 /* bdrv_format_default_perms() accepts only these two, so disguise
94 * detach_by_driver_cb_role as one of them. */
95 if (role != &child_file && role != &child_backing) {
96 role = &child_file;
99 bdrv_format_default_perms(bs, c, role, reopen_queue, perm, shared,
100 nperm, nshared);
103 static BlockDriver bdrv_test = {
104 .format_name = "test",
105 .instance_size = sizeof(BDRVTestState),
107 .bdrv_close = bdrv_test_close,
108 .bdrv_co_preadv = bdrv_test_co_preadv,
110 .bdrv_co_drain_begin = bdrv_test_co_drain_begin,
111 .bdrv_co_drain_end = bdrv_test_co_drain_end,
113 .bdrv_child_perm = bdrv_test_child_perm,
116 static void aio_ret_cb(void *opaque, int ret)
118 int *aio_ret = opaque;
119 *aio_ret = ret;
122 typedef struct CallInCoroutineData {
123 void (*entry)(void);
124 bool done;
125 } CallInCoroutineData;
127 static coroutine_fn void call_in_coroutine_entry(void *opaque)
129 CallInCoroutineData *data = opaque;
131 data->entry();
132 data->done = true;
135 static void call_in_coroutine(void (*entry)(void))
137 Coroutine *co;
138 CallInCoroutineData data = {
139 .entry = entry,
140 .done = false,
143 co = qemu_coroutine_create(call_in_coroutine_entry, &data);
144 qemu_coroutine_enter(co);
145 while (!data.done) {
146 aio_poll(qemu_get_aio_context(), true);
150 enum drain_type {
151 BDRV_DRAIN_ALL,
152 BDRV_DRAIN,
153 BDRV_SUBTREE_DRAIN,
154 DRAIN_TYPE_MAX,
157 static void do_drain_begin(enum drain_type drain_type, BlockDriverState *bs)
159 switch (drain_type) {
160 case BDRV_DRAIN_ALL: bdrv_drain_all_begin(); break;
161 case BDRV_DRAIN: bdrv_drained_begin(bs); break;
162 case BDRV_SUBTREE_DRAIN: bdrv_subtree_drained_begin(bs); break;
163 default: g_assert_not_reached();
167 static void do_drain_end(enum drain_type drain_type, BlockDriverState *bs)
169 switch (drain_type) {
170 case BDRV_DRAIN_ALL: bdrv_drain_all_end(); break;
171 case BDRV_DRAIN: bdrv_drained_end(bs); break;
172 case BDRV_SUBTREE_DRAIN: bdrv_subtree_drained_end(bs); break;
173 default: g_assert_not_reached();
177 static void do_drain_begin_unlocked(enum drain_type drain_type, BlockDriverState *bs)
179 if (drain_type != BDRV_DRAIN_ALL) {
180 aio_context_acquire(bdrv_get_aio_context(bs));
182 do_drain_begin(drain_type, bs);
183 if (drain_type != BDRV_DRAIN_ALL) {
184 aio_context_release(bdrv_get_aio_context(bs));
188 static void do_drain_end_unlocked(enum drain_type drain_type, BlockDriverState *bs)
190 if (drain_type != BDRV_DRAIN_ALL) {
191 aio_context_acquire(bdrv_get_aio_context(bs));
193 do_drain_end(drain_type, bs);
194 if (drain_type != BDRV_DRAIN_ALL) {
195 aio_context_release(bdrv_get_aio_context(bs));
199 static void test_drv_cb_common(enum drain_type drain_type, bool recursive)
201 BlockBackend *blk;
202 BlockDriverState *bs, *backing;
203 BDRVTestState *s, *backing_s;
204 BlockAIOCB *acb;
205 int aio_ret;
207 QEMUIOVector qiov = QEMU_IOVEC_INIT_BUF(qiov, NULL, 0);
209 blk = blk_new(BLK_PERM_ALL, BLK_PERM_ALL);
210 bs = bdrv_new_open_driver(&bdrv_test, "test-node", BDRV_O_RDWR,
211 &error_abort);
212 s = bs->opaque;
213 blk_insert_bs(blk, bs, &error_abort);
215 backing = bdrv_new_open_driver(&bdrv_test, "backing", 0, &error_abort);
216 backing_s = backing->opaque;
217 bdrv_set_backing_hd(bs, backing, &error_abort);
219 /* Simple bdrv_drain_all_begin/end pair, check that CBs are called */
220 g_assert_cmpint(s->drain_count, ==, 0);
221 g_assert_cmpint(backing_s->drain_count, ==, 0);
223 do_drain_begin(drain_type, bs);
225 g_assert_cmpint(s->drain_count, ==, 1);
226 g_assert_cmpint(backing_s->drain_count, ==, !!recursive);
228 do_drain_end(drain_type, bs);
230 g_assert_cmpint(s->drain_count, ==, 0);
231 g_assert_cmpint(backing_s->drain_count, ==, 0);
233 /* Now do the same while a request is pending */
234 aio_ret = -EINPROGRESS;
235 acb = blk_aio_preadv(blk, 0, &qiov, 0, aio_ret_cb, &aio_ret);
236 g_assert(acb != NULL);
237 g_assert_cmpint(aio_ret, ==, -EINPROGRESS);
239 g_assert_cmpint(s->drain_count, ==, 0);
240 g_assert_cmpint(backing_s->drain_count, ==, 0);
242 do_drain_begin(drain_type, bs);
244 g_assert_cmpint(aio_ret, ==, 0);
245 g_assert_cmpint(s->drain_count, ==, 1);
246 g_assert_cmpint(backing_s->drain_count, ==, !!recursive);
248 do_drain_end(drain_type, bs);
250 g_assert_cmpint(s->drain_count, ==, 0);
251 g_assert_cmpint(backing_s->drain_count, ==, 0);
253 bdrv_unref(backing);
254 bdrv_unref(bs);
255 blk_unref(blk);
258 static void test_drv_cb_drain_all(void)
260 test_drv_cb_common(BDRV_DRAIN_ALL, true);
263 static void test_drv_cb_drain(void)
265 test_drv_cb_common(BDRV_DRAIN, false);
268 static void test_drv_cb_drain_subtree(void)
270 test_drv_cb_common(BDRV_SUBTREE_DRAIN, true);
273 static void test_drv_cb_co_drain_all(void)
275 call_in_coroutine(test_drv_cb_drain_all);
278 static void test_drv_cb_co_drain(void)
280 call_in_coroutine(test_drv_cb_drain);
283 static void test_drv_cb_co_drain_subtree(void)
285 call_in_coroutine(test_drv_cb_drain_subtree);
288 static void test_quiesce_common(enum drain_type drain_type, bool recursive)
290 BlockBackend *blk;
291 BlockDriverState *bs, *backing;
293 blk = blk_new(BLK_PERM_ALL, BLK_PERM_ALL);
294 bs = bdrv_new_open_driver(&bdrv_test, "test-node", BDRV_O_RDWR,
295 &error_abort);
296 blk_insert_bs(blk, bs, &error_abort);
298 backing = bdrv_new_open_driver(&bdrv_test, "backing", 0, &error_abort);
299 bdrv_set_backing_hd(bs, backing, &error_abort);
301 g_assert_cmpint(bs->quiesce_counter, ==, 0);
302 g_assert_cmpint(backing->quiesce_counter, ==, 0);
304 do_drain_begin(drain_type, bs);
306 g_assert_cmpint(bs->quiesce_counter, ==, 1);
307 g_assert_cmpint(backing->quiesce_counter, ==, !!recursive);
309 do_drain_end(drain_type, bs);
311 g_assert_cmpint(bs->quiesce_counter, ==, 0);
312 g_assert_cmpint(backing->quiesce_counter, ==, 0);
314 bdrv_unref(backing);
315 bdrv_unref(bs);
316 blk_unref(blk);
319 static void test_quiesce_drain_all(void)
321 test_quiesce_common(BDRV_DRAIN_ALL, true);
324 static void test_quiesce_drain(void)
326 test_quiesce_common(BDRV_DRAIN, false);
329 static void test_quiesce_drain_subtree(void)
331 test_quiesce_common(BDRV_SUBTREE_DRAIN, true);
334 static void test_quiesce_co_drain_all(void)
336 call_in_coroutine(test_quiesce_drain_all);
339 static void test_quiesce_co_drain(void)
341 call_in_coroutine(test_quiesce_drain);
344 static void test_quiesce_co_drain_subtree(void)
346 call_in_coroutine(test_quiesce_drain_subtree);
349 static void test_nested(void)
351 BlockBackend *blk;
352 BlockDriverState *bs, *backing;
353 BDRVTestState *s, *backing_s;
354 enum drain_type outer, inner;
356 blk = blk_new(BLK_PERM_ALL, BLK_PERM_ALL);
357 bs = bdrv_new_open_driver(&bdrv_test, "test-node", BDRV_O_RDWR,
358 &error_abort);
359 s = bs->opaque;
360 blk_insert_bs(blk, bs, &error_abort);
362 backing = bdrv_new_open_driver(&bdrv_test, "backing", 0, &error_abort);
363 backing_s = backing->opaque;
364 bdrv_set_backing_hd(bs, backing, &error_abort);
366 for (outer = 0; outer < DRAIN_TYPE_MAX; outer++) {
367 for (inner = 0; inner < DRAIN_TYPE_MAX; inner++) {
368 int backing_quiesce = (outer != BDRV_DRAIN) +
369 (inner != BDRV_DRAIN);
371 g_assert_cmpint(bs->quiesce_counter, ==, 0);
372 g_assert_cmpint(backing->quiesce_counter, ==, 0);
373 g_assert_cmpint(s->drain_count, ==, 0);
374 g_assert_cmpint(backing_s->drain_count, ==, 0);
376 do_drain_begin(outer, bs);
377 do_drain_begin(inner, bs);
379 g_assert_cmpint(bs->quiesce_counter, ==, 2);
380 g_assert_cmpint(backing->quiesce_counter, ==, backing_quiesce);
381 g_assert_cmpint(s->drain_count, ==, 2);
382 g_assert_cmpint(backing_s->drain_count, ==, backing_quiesce);
384 do_drain_end(inner, bs);
385 do_drain_end(outer, bs);
387 g_assert_cmpint(bs->quiesce_counter, ==, 0);
388 g_assert_cmpint(backing->quiesce_counter, ==, 0);
389 g_assert_cmpint(s->drain_count, ==, 0);
390 g_assert_cmpint(backing_s->drain_count, ==, 0);
394 bdrv_unref(backing);
395 bdrv_unref(bs);
396 blk_unref(blk);
399 static void test_multiparent(void)
401 BlockBackend *blk_a, *blk_b;
402 BlockDriverState *bs_a, *bs_b, *backing;
403 BDRVTestState *a_s, *b_s, *backing_s;
405 blk_a = blk_new(BLK_PERM_ALL, BLK_PERM_ALL);
406 bs_a = bdrv_new_open_driver(&bdrv_test, "test-node-a", BDRV_O_RDWR,
407 &error_abort);
408 a_s = bs_a->opaque;
409 blk_insert_bs(blk_a, bs_a, &error_abort);
411 blk_b = blk_new(BLK_PERM_ALL, BLK_PERM_ALL);
412 bs_b = bdrv_new_open_driver(&bdrv_test, "test-node-b", BDRV_O_RDWR,
413 &error_abort);
414 b_s = bs_b->opaque;
415 blk_insert_bs(blk_b, bs_b, &error_abort);
417 backing = bdrv_new_open_driver(&bdrv_test, "backing", 0, &error_abort);
418 backing_s = backing->opaque;
419 bdrv_set_backing_hd(bs_a, backing, &error_abort);
420 bdrv_set_backing_hd(bs_b, backing, &error_abort);
422 g_assert_cmpint(bs_a->quiesce_counter, ==, 0);
423 g_assert_cmpint(bs_b->quiesce_counter, ==, 0);
424 g_assert_cmpint(backing->quiesce_counter, ==, 0);
425 g_assert_cmpint(a_s->drain_count, ==, 0);
426 g_assert_cmpint(b_s->drain_count, ==, 0);
427 g_assert_cmpint(backing_s->drain_count, ==, 0);
429 do_drain_begin(BDRV_SUBTREE_DRAIN, bs_a);
431 g_assert_cmpint(bs_a->quiesce_counter, ==, 1);
432 g_assert_cmpint(bs_b->quiesce_counter, ==, 1);
433 g_assert_cmpint(backing->quiesce_counter, ==, 1);
434 g_assert_cmpint(a_s->drain_count, ==, 1);
435 g_assert_cmpint(b_s->drain_count, ==, 1);
436 g_assert_cmpint(backing_s->drain_count, ==, 1);
438 do_drain_begin(BDRV_SUBTREE_DRAIN, bs_b);
440 g_assert_cmpint(bs_a->quiesce_counter, ==, 2);
441 g_assert_cmpint(bs_b->quiesce_counter, ==, 2);
442 g_assert_cmpint(backing->quiesce_counter, ==, 2);
443 g_assert_cmpint(a_s->drain_count, ==, 2);
444 g_assert_cmpint(b_s->drain_count, ==, 2);
445 g_assert_cmpint(backing_s->drain_count, ==, 2);
447 do_drain_end(BDRV_SUBTREE_DRAIN, bs_b);
449 g_assert_cmpint(bs_a->quiesce_counter, ==, 1);
450 g_assert_cmpint(bs_b->quiesce_counter, ==, 1);
451 g_assert_cmpint(backing->quiesce_counter, ==, 1);
452 g_assert_cmpint(a_s->drain_count, ==, 1);
453 g_assert_cmpint(b_s->drain_count, ==, 1);
454 g_assert_cmpint(backing_s->drain_count, ==, 1);
456 do_drain_end(BDRV_SUBTREE_DRAIN, bs_a);
458 g_assert_cmpint(bs_a->quiesce_counter, ==, 0);
459 g_assert_cmpint(bs_b->quiesce_counter, ==, 0);
460 g_assert_cmpint(backing->quiesce_counter, ==, 0);
461 g_assert_cmpint(a_s->drain_count, ==, 0);
462 g_assert_cmpint(b_s->drain_count, ==, 0);
463 g_assert_cmpint(backing_s->drain_count, ==, 0);
465 bdrv_unref(backing);
466 bdrv_unref(bs_a);
467 bdrv_unref(bs_b);
468 blk_unref(blk_a);
469 blk_unref(blk_b);
472 static void test_graph_change_drain_subtree(void)
474 BlockBackend *blk_a, *blk_b;
475 BlockDriverState *bs_a, *bs_b, *backing;
476 BDRVTestState *a_s, *b_s, *backing_s;
478 blk_a = blk_new(BLK_PERM_ALL, BLK_PERM_ALL);
479 bs_a = bdrv_new_open_driver(&bdrv_test, "test-node-a", BDRV_O_RDWR,
480 &error_abort);
481 a_s = bs_a->opaque;
482 blk_insert_bs(blk_a, bs_a, &error_abort);
484 blk_b = blk_new(BLK_PERM_ALL, BLK_PERM_ALL);
485 bs_b = bdrv_new_open_driver(&bdrv_test, "test-node-b", BDRV_O_RDWR,
486 &error_abort);
487 b_s = bs_b->opaque;
488 blk_insert_bs(blk_b, bs_b, &error_abort);
490 backing = bdrv_new_open_driver(&bdrv_test, "backing", 0, &error_abort);
491 backing_s = backing->opaque;
492 bdrv_set_backing_hd(bs_a, backing, &error_abort);
494 g_assert_cmpint(bs_a->quiesce_counter, ==, 0);
495 g_assert_cmpint(bs_b->quiesce_counter, ==, 0);
496 g_assert_cmpint(backing->quiesce_counter, ==, 0);
497 g_assert_cmpint(a_s->drain_count, ==, 0);
498 g_assert_cmpint(b_s->drain_count, ==, 0);
499 g_assert_cmpint(backing_s->drain_count, ==, 0);
501 do_drain_begin(BDRV_SUBTREE_DRAIN, bs_a);
502 do_drain_begin(BDRV_SUBTREE_DRAIN, bs_a);
503 do_drain_begin(BDRV_SUBTREE_DRAIN, bs_a);
504 do_drain_begin(BDRV_SUBTREE_DRAIN, bs_b);
505 do_drain_begin(BDRV_SUBTREE_DRAIN, bs_b);
507 bdrv_set_backing_hd(bs_b, backing, &error_abort);
508 g_assert_cmpint(bs_a->quiesce_counter, ==, 5);
509 g_assert_cmpint(bs_b->quiesce_counter, ==, 5);
510 g_assert_cmpint(backing->quiesce_counter, ==, 5);
511 g_assert_cmpint(a_s->drain_count, ==, 5);
512 g_assert_cmpint(b_s->drain_count, ==, 5);
513 g_assert_cmpint(backing_s->drain_count, ==, 5);
515 bdrv_set_backing_hd(bs_b, NULL, &error_abort);
516 g_assert_cmpint(bs_a->quiesce_counter, ==, 3);
517 g_assert_cmpint(bs_b->quiesce_counter, ==, 2);
518 g_assert_cmpint(backing->quiesce_counter, ==, 3);
519 g_assert_cmpint(a_s->drain_count, ==, 3);
520 g_assert_cmpint(b_s->drain_count, ==, 2);
521 g_assert_cmpint(backing_s->drain_count, ==, 3);
523 bdrv_set_backing_hd(bs_b, backing, &error_abort);
524 g_assert_cmpint(bs_a->quiesce_counter, ==, 5);
525 g_assert_cmpint(bs_b->quiesce_counter, ==, 5);
526 g_assert_cmpint(backing->quiesce_counter, ==, 5);
527 g_assert_cmpint(a_s->drain_count, ==, 5);
528 g_assert_cmpint(b_s->drain_count, ==, 5);
529 g_assert_cmpint(backing_s->drain_count, ==, 5);
531 do_drain_end(BDRV_SUBTREE_DRAIN, bs_b);
532 do_drain_end(BDRV_SUBTREE_DRAIN, bs_b);
533 do_drain_end(BDRV_SUBTREE_DRAIN, bs_a);
534 do_drain_end(BDRV_SUBTREE_DRAIN, bs_a);
535 do_drain_end(BDRV_SUBTREE_DRAIN, bs_a);
537 g_assert_cmpint(bs_a->quiesce_counter, ==, 0);
538 g_assert_cmpint(bs_b->quiesce_counter, ==, 0);
539 g_assert_cmpint(backing->quiesce_counter, ==, 0);
540 g_assert_cmpint(a_s->drain_count, ==, 0);
541 g_assert_cmpint(b_s->drain_count, ==, 0);
542 g_assert_cmpint(backing_s->drain_count, ==, 0);
544 bdrv_unref(backing);
545 bdrv_unref(bs_a);
546 bdrv_unref(bs_b);
547 blk_unref(blk_a);
548 blk_unref(blk_b);
551 static void test_graph_change_drain_all(void)
553 BlockBackend *blk_a, *blk_b;
554 BlockDriverState *bs_a, *bs_b;
555 BDRVTestState *a_s, *b_s;
557 /* Create node A with a BlockBackend */
558 blk_a = blk_new(BLK_PERM_ALL, BLK_PERM_ALL);
559 bs_a = bdrv_new_open_driver(&bdrv_test, "test-node-a", BDRV_O_RDWR,
560 &error_abort);
561 a_s = bs_a->opaque;
562 blk_insert_bs(blk_a, bs_a, &error_abort);
564 g_assert_cmpint(bs_a->quiesce_counter, ==, 0);
565 g_assert_cmpint(a_s->drain_count, ==, 0);
567 /* Call bdrv_drain_all_begin() */
568 bdrv_drain_all_begin();
570 g_assert_cmpint(bs_a->quiesce_counter, ==, 1);
571 g_assert_cmpint(a_s->drain_count, ==, 1);
573 /* Create node B with a BlockBackend */
574 blk_b = blk_new(BLK_PERM_ALL, BLK_PERM_ALL);
575 bs_b = bdrv_new_open_driver(&bdrv_test, "test-node-b", BDRV_O_RDWR,
576 &error_abort);
577 b_s = bs_b->opaque;
578 blk_insert_bs(blk_b, bs_b, &error_abort);
580 g_assert_cmpint(bs_a->quiesce_counter, ==, 1);
581 g_assert_cmpint(bs_b->quiesce_counter, ==, 1);
582 g_assert_cmpint(a_s->drain_count, ==, 1);
583 g_assert_cmpint(b_s->drain_count, ==, 1);
585 /* Unref and finally delete node A */
586 blk_unref(blk_a);
588 g_assert_cmpint(bs_a->quiesce_counter, ==, 1);
589 g_assert_cmpint(bs_b->quiesce_counter, ==, 1);
590 g_assert_cmpint(a_s->drain_count, ==, 1);
591 g_assert_cmpint(b_s->drain_count, ==, 1);
593 bdrv_unref(bs_a);
595 g_assert_cmpint(bs_b->quiesce_counter, ==, 1);
596 g_assert_cmpint(b_s->drain_count, ==, 1);
598 /* End the drained section */
599 bdrv_drain_all_end();
601 g_assert_cmpint(bs_b->quiesce_counter, ==, 0);
602 g_assert_cmpint(b_s->drain_count, ==, 0);
604 bdrv_unref(bs_b);
605 blk_unref(blk_b);
608 struct test_iothread_data {
609 BlockDriverState *bs;
610 enum drain_type drain_type;
611 int *aio_ret;
614 static void test_iothread_drain_entry(void *opaque)
616 struct test_iothread_data *data = opaque;
618 aio_context_acquire(bdrv_get_aio_context(data->bs));
619 do_drain_begin(data->drain_type, data->bs);
620 g_assert_cmpint(*data->aio_ret, ==, 0);
621 do_drain_end(data->drain_type, data->bs);
622 aio_context_release(bdrv_get_aio_context(data->bs));
624 qemu_event_set(&done_event);
627 static void test_iothread_aio_cb(void *opaque, int ret)
629 int *aio_ret = opaque;
630 *aio_ret = ret;
631 qemu_event_set(&done_event);
634 static void test_iothread_main_thread_bh(void *opaque)
636 struct test_iothread_data *data = opaque;
638 /* Test that the AioContext is not yet locked in a random BH that is
639 * executed during drain, otherwise this would deadlock. */
640 aio_context_acquire(bdrv_get_aio_context(data->bs));
641 bdrv_flush(data->bs);
642 aio_context_release(bdrv_get_aio_context(data->bs));
646 * Starts an AIO request on a BDS that runs in the AioContext of iothread 1.
647 * The request involves a BH on iothread 2 before it can complete.
649 * @drain_thread = 0 means that do_drain_begin/end are called from the main
650 * thread, @drain_thread = 1 means that they are called from iothread 1. Drain
651 * for this BDS cannot be called from iothread 2 because only the main thread
652 * may do cross-AioContext polling.
654 static void test_iothread_common(enum drain_type drain_type, int drain_thread)
656 BlockBackend *blk;
657 BlockDriverState *bs;
658 BDRVTestState *s;
659 BlockAIOCB *acb;
660 int aio_ret;
661 struct test_iothread_data data;
663 IOThread *a = iothread_new();
664 IOThread *b = iothread_new();
665 AioContext *ctx_a = iothread_get_aio_context(a);
666 AioContext *ctx_b = iothread_get_aio_context(b);
668 QEMUIOVector qiov = QEMU_IOVEC_INIT_BUF(qiov, NULL, 0);
670 /* bdrv_drain_all() may only be called from the main loop thread */
671 if (drain_type == BDRV_DRAIN_ALL && drain_thread != 0) {
672 goto out;
675 blk = blk_new(BLK_PERM_ALL, BLK_PERM_ALL);
676 bs = bdrv_new_open_driver(&bdrv_test, "test-node", BDRV_O_RDWR,
677 &error_abort);
678 s = bs->opaque;
679 blk_insert_bs(blk, bs, &error_abort);
681 blk_set_aio_context(blk, ctx_a);
682 aio_context_acquire(ctx_a);
684 s->bh_indirection_ctx = ctx_b;
686 aio_ret = -EINPROGRESS;
687 qemu_event_reset(&done_event);
689 if (drain_thread == 0) {
690 acb = blk_aio_preadv(blk, 0, &qiov, 0, test_iothread_aio_cb, &aio_ret);
691 } else {
692 acb = blk_aio_preadv(blk, 0, &qiov, 0, aio_ret_cb, &aio_ret);
694 g_assert(acb != NULL);
695 g_assert_cmpint(aio_ret, ==, -EINPROGRESS);
697 aio_context_release(ctx_a);
699 data = (struct test_iothread_data) {
700 .bs = bs,
701 .drain_type = drain_type,
702 .aio_ret = &aio_ret,
705 switch (drain_thread) {
706 case 0:
707 if (drain_type != BDRV_DRAIN_ALL) {
708 aio_context_acquire(ctx_a);
711 aio_bh_schedule_oneshot(ctx_a, test_iothread_main_thread_bh, &data);
713 /* The request is running on the IOThread a. Draining its block device
714 * will make sure that it has completed as far as the BDS is concerned,
715 * but the drain in this thread can continue immediately after
716 * bdrv_dec_in_flight() and aio_ret might be assigned only slightly
717 * later. */
718 do_drain_begin(drain_type, bs);
719 g_assert_cmpint(bs->in_flight, ==, 0);
721 if (drain_type != BDRV_DRAIN_ALL) {
722 aio_context_release(ctx_a);
724 qemu_event_wait(&done_event);
725 if (drain_type != BDRV_DRAIN_ALL) {
726 aio_context_acquire(ctx_a);
729 g_assert_cmpint(aio_ret, ==, 0);
730 do_drain_end(drain_type, bs);
732 if (drain_type != BDRV_DRAIN_ALL) {
733 aio_context_release(ctx_a);
735 break;
736 case 1:
737 aio_bh_schedule_oneshot(ctx_a, test_iothread_drain_entry, &data);
738 qemu_event_wait(&done_event);
739 break;
740 default:
741 g_assert_not_reached();
744 aio_context_acquire(ctx_a);
745 blk_set_aio_context(blk, qemu_get_aio_context());
746 aio_context_release(ctx_a);
748 bdrv_unref(bs);
749 blk_unref(blk);
751 out:
752 iothread_join(a);
753 iothread_join(b);
756 static void test_iothread_drain_all(void)
758 test_iothread_common(BDRV_DRAIN_ALL, 0);
759 test_iothread_common(BDRV_DRAIN_ALL, 1);
762 static void test_iothread_drain(void)
764 test_iothread_common(BDRV_DRAIN, 0);
765 test_iothread_common(BDRV_DRAIN, 1);
768 static void test_iothread_drain_subtree(void)
770 test_iothread_common(BDRV_SUBTREE_DRAIN, 0);
771 test_iothread_common(BDRV_SUBTREE_DRAIN, 1);
775 typedef struct TestBlockJob {
776 BlockJob common;
777 int run_ret;
778 int prepare_ret;
779 bool running;
780 bool should_complete;
781 } TestBlockJob;
783 static int test_job_prepare(Job *job)
785 TestBlockJob *s = container_of(job, TestBlockJob, common.job);
787 /* Provoke an AIO_WAIT_WHILE() call to verify there is no deadlock */
788 blk_flush(s->common.blk);
789 return s->prepare_ret;
792 static void test_job_commit(Job *job)
794 TestBlockJob *s = container_of(job, TestBlockJob, common.job);
796 /* Provoke an AIO_WAIT_WHILE() call to verify there is no deadlock */
797 blk_flush(s->common.blk);
800 static void test_job_abort(Job *job)
802 TestBlockJob *s = container_of(job, TestBlockJob, common.job);
804 /* Provoke an AIO_WAIT_WHILE() call to verify there is no deadlock */
805 blk_flush(s->common.blk);
808 static int coroutine_fn test_job_run(Job *job, Error **errp)
810 TestBlockJob *s = container_of(job, TestBlockJob, common.job);
812 /* We are running the actual job code past the pause point in
813 * job_co_entry(). */
814 s->running = true;
816 job_transition_to_ready(&s->common.job);
817 while (!s->should_complete) {
818 /* Avoid job_sleep_ns() because it marks the job as !busy. We want to
819 * emulate some actual activity (probably some I/O) here so that drain
820 * has to wait for this activity to stop. */
821 qemu_co_sleep_ns(QEMU_CLOCK_REALTIME, 1000000);
823 job_pause_point(&s->common.job);
826 return s->run_ret;
829 static void test_job_complete(Job *job, Error **errp)
831 TestBlockJob *s = container_of(job, TestBlockJob, common.job);
832 s->should_complete = true;
835 BlockJobDriver test_job_driver = {
836 .job_driver = {
837 .instance_size = sizeof(TestBlockJob),
838 .free = block_job_free,
839 .user_resume = block_job_user_resume,
840 .drain = block_job_drain,
841 .run = test_job_run,
842 .complete = test_job_complete,
843 .prepare = test_job_prepare,
844 .commit = test_job_commit,
845 .abort = test_job_abort,
849 enum test_job_result {
850 TEST_JOB_SUCCESS,
851 TEST_JOB_FAIL_RUN,
852 TEST_JOB_FAIL_PREPARE,
855 enum test_job_drain_node {
856 TEST_JOB_DRAIN_SRC,
857 TEST_JOB_DRAIN_SRC_CHILD,
858 TEST_JOB_DRAIN_SRC_PARENT,
861 static void test_blockjob_common_drain_node(enum drain_type drain_type,
862 bool use_iothread,
863 enum test_job_result result,
864 enum test_job_drain_node drain_node)
866 BlockBackend *blk_src, *blk_target;
867 BlockDriverState *src, *src_backing, *src_overlay, *target, *drain_bs;
868 BlockJob *job;
869 TestBlockJob *tjob;
870 IOThread *iothread = NULL;
871 AioContext *ctx;
872 int ret;
874 src = bdrv_new_open_driver(&bdrv_test, "source", BDRV_O_RDWR,
875 &error_abort);
876 src_backing = bdrv_new_open_driver(&bdrv_test, "source-backing",
877 BDRV_O_RDWR, &error_abort);
878 src_overlay = bdrv_new_open_driver(&bdrv_test, "source-overlay",
879 BDRV_O_RDWR, &error_abort);
881 bdrv_set_backing_hd(src_overlay, src, &error_abort);
882 bdrv_unref(src);
883 bdrv_set_backing_hd(src, src_backing, &error_abort);
884 bdrv_unref(src_backing);
886 blk_src = blk_new(BLK_PERM_ALL, BLK_PERM_ALL);
887 blk_insert_bs(blk_src, src_overlay, &error_abort);
889 switch (drain_node) {
890 case TEST_JOB_DRAIN_SRC:
891 drain_bs = src;
892 break;
893 case TEST_JOB_DRAIN_SRC_CHILD:
894 drain_bs = src_backing;
895 break;
896 case TEST_JOB_DRAIN_SRC_PARENT:
897 drain_bs = src_overlay;
898 break;
899 default:
900 g_assert_not_reached();
903 if (use_iothread) {
904 iothread = iothread_new();
905 ctx = iothread_get_aio_context(iothread);
906 blk_set_aio_context(blk_src, ctx);
907 } else {
908 ctx = qemu_get_aio_context();
911 target = bdrv_new_open_driver(&bdrv_test, "target", BDRV_O_RDWR,
912 &error_abort);
913 blk_target = blk_new(BLK_PERM_ALL, BLK_PERM_ALL);
914 blk_insert_bs(blk_target, target, &error_abort);
916 aio_context_acquire(ctx);
917 tjob = block_job_create("job0", &test_job_driver, NULL, src,
918 0, BLK_PERM_ALL,
919 0, 0, NULL, NULL, &error_abort);
920 job = &tjob->common;
921 block_job_add_bdrv(job, "target", target, 0, BLK_PERM_ALL, &error_abort);
923 switch (result) {
924 case TEST_JOB_SUCCESS:
925 break;
926 case TEST_JOB_FAIL_RUN:
927 tjob->run_ret = -EIO;
928 break;
929 case TEST_JOB_FAIL_PREPARE:
930 tjob->prepare_ret = -EIO;
931 break;
934 job_start(&job->job);
935 aio_context_release(ctx);
937 if (use_iothread) {
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
940 * job_co_entry(). */
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);
956 } else {
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);
964 if (use_iothread) {
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(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);
980 } else {
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(drain_type, target);
988 if (use_iothread) {
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));
1003 if (use_iothread) {
1004 blk_set_aio_context(blk_src, qemu_get_aio_context());
1006 aio_context_release(ctx);
1008 blk_unref(blk_src);
1009 blk_unref(blk_target);
1010 bdrv_unref(src_overlay);
1011 bdrv_unref(target);
1013 if (iothread) {
1014 iothread_join(iothread);
1018 static void test_blockjob_common(enum drain_type drain_type, bool use_iothread,
1019 enum test_job_result result)
1021 test_blockjob_common_drain_node(drain_type, use_iothread, result,
1022 TEST_JOB_DRAIN_SRC);
1023 test_blockjob_common_drain_node(drain_type, use_iothread, result,
1024 TEST_JOB_DRAIN_SRC_CHILD);
1025 if (drain_type == BDRV_SUBTREE_DRAIN) {
1026 test_blockjob_common_drain_node(drain_type, use_iothread, result,
1027 TEST_JOB_DRAIN_SRC_PARENT);
1031 static void test_blockjob_drain_all(void)
1033 test_blockjob_common(BDRV_DRAIN_ALL, false, TEST_JOB_SUCCESS);
1036 static void test_blockjob_drain(void)
1038 test_blockjob_common(BDRV_DRAIN, false, TEST_JOB_SUCCESS);
1041 static void test_blockjob_drain_subtree(void)
1043 test_blockjob_common(BDRV_SUBTREE_DRAIN, false, TEST_JOB_SUCCESS);
1046 static void test_blockjob_error_drain_all(void)
1048 test_blockjob_common(BDRV_DRAIN_ALL, false, TEST_JOB_FAIL_RUN);
1049 test_blockjob_common(BDRV_DRAIN_ALL, false, TEST_JOB_FAIL_PREPARE);
1052 static void test_blockjob_error_drain(void)
1054 test_blockjob_common(BDRV_DRAIN, false, TEST_JOB_FAIL_RUN);
1055 test_blockjob_common(BDRV_DRAIN, false, TEST_JOB_FAIL_PREPARE);
1058 static void test_blockjob_error_drain_subtree(void)
1060 test_blockjob_common(BDRV_SUBTREE_DRAIN, false, TEST_JOB_FAIL_RUN);
1061 test_blockjob_common(BDRV_SUBTREE_DRAIN, false, TEST_JOB_FAIL_PREPARE);
1064 static void test_blockjob_iothread_drain_all(void)
1066 test_blockjob_common(BDRV_DRAIN_ALL, true, TEST_JOB_SUCCESS);
1069 static void test_blockjob_iothread_drain(void)
1071 test_blockjob_common(BDRV_DRAIN, true, TEST_JOB_SUCCESS);
1074 static void test_blockjob_iothread_drain_subtree(void)
1076 test_blockjob_common(BDRV_SUBTREE_DRAIN, true, TEST_JOB_SUCCESS);
1079 static void test_blockjob_iothread_error_drain_all(void)
1081 test_blockjob_common(BDRV_DRAIN_ALL, true, TEST_JOB_FAIL_RUN);
1082 test_blockjob_common(BDRV_DRAIN_ALL, true, TEST_JOB_FAIL_PREPARE);
1085 static void test_blockjob_iothread_error_drain(void)
1087 test_blockjob_common(BDRV_DRAIN, true, TEST_JOB_FAIL_RUN);
1088 test_blockjob_common(BDRV_DRAIN, true, TEST_JOB_FAIL_PREPARE);
1091 static void test_blockjob_iothread_error_drain_subtree(void)
1093 test_blockjob_common(BDRV_SUBTREE_DRAIN, true, TEST_JOB_FAIL_RUN);
1094 test_blockjob_common(BDRV_SUBTREE_DRAIN, true, TEST_JOB_FAIL_PREPARE);
1098 typedef struct BDRVTestTopState {
1099 BdrvChild *wait_child;
1100 } BDRVTestTopState;
1102 static void bdrv_test_top_close(BlockDriverState *bs)
1104 BdrvChild *c, *next_c;
1105 QLIST_FOREACH_SAFE(c, &bs->children, next, next_c) {
1106 bdrv_unref_child(bs, c);
1110 static int coroutine_fn bdrv_test_top_co_preadv(BlockDriverState *bs,
1111 uint64_t offset, uint64_t bytes,
1112 QEMUIOVector *qiov, int flags)
1114 BDRVTestTopState *tts = bs->opaque;
1115 return bdrv_co_preadv(tts->wait_child, offset, bytes, qiov, flags);
1118 static BlockDriver bdrv_test_top_driver = {
1119 .format_name = "test_top_driver",
1120 .instance_size = sizeof(BDRVTestTopState),
1122 .bdrv_close = bdrv_test_top_close,
1123 .bdrv_co_preadv = bdrv_test_top_co_preadv,
1125 .bdrv_child_perm = bdrv_format_default_perms,
1128 typedef struct TestCoDeleteByDrainData {
1129 BlockBackend *blk;
1130 bool detach_instead_of_delete;
1131 bool done;
1132 } TestCoDeleteByDrainData;
1134 static void coroutine_fn test_co_delete_by_drain(void *opaque)
1136 TestCoDeleteByDrainData *dbdd = opaque;
1137 BlockBackend *blk = dbdd->blk;
1138 BlockDriverState *bs = blk_bs(blk);
1139 BDRVTestTopState *tts = bs->opaque;
1140 void *buffer = g_malloc(65536);
1141 QEMUIOVector qiov = QEMU_IOVEC_INIT_BUF(qiov, buffer, 65536);
1143 /* Pretend some internal write operation from parent to child.
1144 * Important: We have to read from the child, not from the parent!
1145 * Draining works by first propagating it all up the tree to the
1146 * root and then waiting for drainage from root to the leaves
1147 * (protocol nodes). If we have a request waiting on the root,
1148 * everything will be drained before we go back down the tree, but
1149 * we do not want that. We want to be in the middle of draining
1150 * when this following requests returns. */
1151 bdrv_co_preadv(tts->wait_child, 0, 65536, &qiov, 0);
1153 g_assert_cmpint(bs->refcnt, ==, 1);
1155 if (!dbdd->detach_instead_of_delete) {
1156 blk_unref(blk);
1157 } else {
1158 BdrvChild *c, *next_c;
1159 QLIST_FOREACH_SAFE(c, &bs->children, next, next_c) {
1160 bdrv_unref_child(bs, c);
1164 dbdd->done = true;
1165 g_free(buffer);
1169 * Test what happens when some BDS has some children, you drain one of
1170 * them and this results in the BDS being deleted.
1172 * If @detach_instead_of_delete is set, the BDS is not going to be
1173 * deleted but will only detach all of its children.
1175 static void do_test_delete_by_drain(bool detach_instead_of_delete,
1176 enum drain_type drain_type)
1178 BlockBackend *blk;
1179 BlockDriverState *bs, *child_bs, *null_bs;
1180 BDRVTestTopState *tts;
1181 TestCoDeleteByDrainData dbdd;
1182 Coroutine *co;
1184 bs = bdrv_new_open_driver(&bdrv_test_top_driver, "top", BDRV_O_RDWR,
1185 &error_abort);
1186 bs->total_sectors = 65536 >> BDRV_SECTOR_BITS;
1187 tts = bs->opaque;
1189 null_bs = bdrv_open("null-co://", NULL, NULL, BDRV_O_RDWR | BDRV_O_PROTOCOL,
1190 &error_abort);
1191 bdrv_attach_child(bs, null_bs, "null-child", &child_file, &error_abort);
1193 /* This child will be the one to pass to requests through to, and
1194 * it will stall until a drain occurs */
1195 child_bs = bdrv_new_open_driver(&bdrv_test, "child", BDRV_O_RDWR,
1196 &error_abort);
1197 child_bs->total_sectors = 65536 >> BDRV_SECTOR_BITS;
1198 /* Takes our reference to child_bs */
1199 tts->wait_child = bdrv_attach_child(bs, child_bs, "wait-child", &child_file,
1200 &error_abort);
1202 /* This child is just there to be deleted
1203 * (for detach_instead_of_delete == true) */
1204 null_bs = bdrv_open("null-co://", NULL, NULL, BDRV_O_RDWR | BDRV_O_PROTOCOL,
1205 &error_abort);
1206 bdrv_attach_child(bs, null_bs, "null-child", &child_file, &error_abort);
1208 blk = blk_new(BLK_PERM_ALL, BLK_PERM_ALL);
1209 blk_insert_bs(blk, bs, &error_abort);
1211 /* Referenced by blk now */
1212 bdrv_unref(bs);
1214 g_assert_cmpint(bs->refcnt, ==, 1);
1215 g_assert_cmpint(child_bs->refcnt, ==, 1);
1216 g_assert_cmpint(null_bs->refcnt, ==, 1);
1219 dbdd = (TestCoDeleteByDrainData){
1220 .blk = blk,
1221 .detach_instead_of_delete = detach_instead_of_delete,
1222 .done = false,
1224 co = qemu_coroutine_create(test_co_delete_by_drain, &dbdd);
1225 qemu_coroutine_enter(co);
1227 /* Drain the child while the read operation is still pending.
1228 * This should result in the operation finishing and
1229 * test_co_delete_by_drain() resuming. Thus, @bs will be deleted
1230 * and the coroutine will exit while this drain operation is still
1231 * in progress. */
1232 switch (drain_type) {
1233 case BDRV_DRAIN:
1234 bdrv_ref(child_bs);
1235 bdrv_drain(child_bs);
1236 bdrv_unref(child_bs);
1237 break;
1238 case BDRV_SUBTREE_DRAIN:
1239 /* Would have to ref/unref bs here for !detach_instead_of_delete, but
1240 * then the whole test becomes pointless because the graph changes
1241 * don't occur during the drain any more. */
1242 assert(detach_instead_of_delete);
1243 bdrv_subtree_drained_begin(bs);
1244 bdrv_subtree_drained_end(bs);
1245 break;
1246 case BDRV_DRAIN_ALL:
1247 bdrv_drain_all_begin();
1248 bdrv_drain_all_end();
1249 break;
1250 default:
1251 g_assert_not_reached();
1254 while (!dbdd.done) {
1255 aio_poll(qemu_get_aio_context(), true);
1258 if (detach_instead_of_delete) {
1259 /* Here, the reference has not passed over to the coroutine,
1260 * so we have to delete the BB ourselves */
1261 blk_unref(blk);
1265 static void test_delete_by_drain(void)
1267 do_test_delete_by_drain(false, BDRV_DRAIN);
1270 static void test_detach_by_drain_all(void)
1272 do_test_delete_by_drain(true, BDRV_DRAIN_ALL);
1275 static void test_detach_by_drain(void)
1277 do_test_delete_by_drain(true, BDRV_DRAIN);
1280 static void test_detach_by_drain_subtree(void)
1282 do_test_delete_by_drain(true, BDRV_SUBTREE_DRAIN);
1286 struct detach_by_parent_data {
1287 BlockDriverState *parent_b;
1288 BdrvChild *child_b;
1289 BlockDriverState *c;
1290 BdrvChild *child_c;
1291 bool by_parent_cb;
1293 static struct detach_by_parent_data detach_by_parent_data;
1295 static void detach_indirect_bh(void *opaque)
1297 struct detach_by_parent_data *data = opaque;
1299 bdrv_unref_child(data->parent_b, data->child_b);
1301 bdrv_ref(data->c);
1302 data->child_c = bdrv_attach_child(data->parent_b, data->c, "PB-C",
1303 &child_file, &error_abort);
1306 static void detach_by_parent_aio_cb(void *opaque, int ret)
1308 struct detach_by_parent_data *data = &detach_by_parent_data;
1310 g_assert_cmpint(ret, ==, 0);
1311 if (data->by_parent_cb) {
1312 detach_indirect_bh(data);
1316 static void detach_by_driver_cb_drained_begin(BdrvChild *child)
1318 aio_bh_schedule_oneshot(qemu_get_current_aio_context(),
1319 detach_indirect_bh, &detach_by_parent_data);
1320 child_file.drained_begin(child);
1323 static BdrvChildRole detach_by_driver_cb_role;
1326 * Initial graph:
1328 * PA PB
1329 * \ / \
1330 * A B C
1332 * by_parent_cb == true: Test that parent callbacks don't poll
1334 * PA has a pending write request whose callback changes the child nodes of
1335 * PB: It removes B and adds C instead. The subtree of PB is drained, which
1336 * will indirectly drain the write request, too.
1338 * by_parent_cb == false: Test that bdrv_drain_invoke() doesn't poll
1340 * PA's BdrvChildRole has a .drained_begin callback that schedules a BH
1341 * that does the same graph change. If bdrv_drain_invoke() calls it, the
1342 * state is messed up, but if it is only polled in the single
1343 * BDRV_POLL_WHILE() at the end of the drain, this should work fine.
1345 static void test_detach_indirect(bool by_parent_cb)
1347 BlockBackend *blk;
1348 BlockDriverState *parent_a, *parent_b, *a, *b, *c;
1349 BdrvChild *child_a, *child_b;
1350 BlockAIOCB *acb;
1352 QEMUIOVector qiov = QEMU_IOVEC_INIT_BUF(qiov, NULL, 0);
1354 if (!by_parent_cb) {
1355 detach_by_driver_cb_role = child_file;
1356 detach_by_driver_cb_role.drained_begin =
1357 detach_by_driver_cb_drained_begin;
1360 /* Create all involved nodes */
1361 parent_a = bdrv_new_open_driver(&bdrv_test, "parent-a", BDRV_O_RDWR,
1362 &error_abort);
1363 parent_b = bdrv_new_open_driver(&bdrv_test, "parent-b", 0,
1364 &error_abort);
1366 a = bdrv_new_open_driver(&bdrv_test, "a", BDRV_O_RDWR, &error_abort);
1367 b = bdrv_new_open_driver(&bdrv_test, "b", BDRV_O_RDWR, &error_abort);
1368 c = bdrv_new_open_driver(&bdrv_test, "c", BDRV_O_RDWR, &error_abort);
1370 /* blk is a BB for parent-a */
1371 blk = blk_new(BLK_PERM_ALL, BLK_PERM_ALL);
1372 blk_insert_bs(blk, parent_a, &error_abort);
1373 bdrv_unref(parent_a);
1375 /* If we want to get bdrv_drain_invoke() to call aio_poll(), the driver
1376 * callback must not return immediately. */
1377 if (!by_parent_cb) {
1378 BDRVTestState *s = parent_a->opaque;
1379 s->sleep_in_drain_begin = true;
1382 /* Set child relationships */
1383 bdrv_ref(b);
1384 bdrv_ref(a);
1385 child_b = bdrv_attach_child(parent_b, b, "PB-B", &child_file, &error_abort);
1386 child_a = bdrv_attach_child(parent_b, a, "PB-A", &child_backing, &error_abort);
1388 bdrv_ref(a);
1389 bdrv_attach_child(parent_a, a, "PA-A",
1390 by_parent_cb ? &child_file : &detach_by_driver_cb_role,
1391 &error_abort);
1393 g_assert_cmpint(parent_a->refcnt, ==, 1);
1394 g_assert_cmpint(parent_b->refcnt, ==, 1);
1395 g_assert_cmpint(a->refcnt, ==, 3);
1396 g_assert_cmpint(b->refcnt, ==, 2);
1397 g_assert_cmpint(c->refcnt, ==, 1);
1399 g_assert(QLIST_FIRST(&parent_b->children) == child_a);
1400 g_assert(QLIST_NEXT(child_a, next) == child_b);
1401 g_assert(QLIST_NEXT(child_b, next) == NULL);
1403 /* Start the evil write request */
1404 detach_by_parent_data = (struct detach_by_parent_data) {
1405 .parent_b = parent_b,
1406 .child_b = child_b,
1407 .c = c,
1408 .by_parent_cb = by_parent_cb,
1410 acb = blk_aio_preadv(blk, 0, &qiov, 0, detach_by_parent_aio_cb, NULL);
1411 g_assert(acb != NULL);
1413 /* Drain and check the expected result */
1414 bdrv_subtree_drained_begin(parent_b);
1416 g_assert(detach_by_parent_data.child_c != NULL);
1418 g_assert_cmpint(parent_a->refcnt, ==, 1);
1419 g_assert_cmpint(parent_b->refcnt, ==, 1);
1420 g_assert_cmpint(a->refcnt, ==, 3);
1421 g_assert_cmpint(b->refcnt, ==, 1);
1422 g_assert_cmpint(c->refcnt, ==, 2);
1424 g_assert(QLIST_FIRST(&parent_b->children) == detach_by_parent_data.child_c);
1425 g_assert(QLIST_NEXT(detach_by_parent_data.child_c, next) == child_a);
1426 g_assert(QLIST_NEXT(child_a, next) == NULL);
1428 g_assert_cmpint(parent_a->quiesce_counter, ==, 1);
1429 g_assert_cmpint(parent_b->quiesce_counter, ==, 1);
1430 g_assert_cmpint(a->quiesce_counter, ==, 1);
1431 g_assert_cmpint(b->quiesce_counter, ==, 0);
1432 g_assert_cmpint(c->quiesce_counter, ==, 1);
1434 bdrv_subtree_drained_end(parent_b);
1436 bdrv_unref(parent_b);
1437 blk_unref(blk);
1439 /* XXX Once bdrv_close() unref's children instead of just detaching them,
1440 * this won't be necessary any more. */
1441 bdrv_unref(a);
1442 bdrv_unref(a);
1443 bdrv_unref(c);
1445 g_assert_cmpint(a->refcnt, ==, 1);
1446 g_assert_cmpint(b->refcnt, ==, 1);
1447 g_assert_cmpint(c->refcnt, ==, 1);
1448 bdrv_unref(a);
1449 bdrv_unref(b);
1450 bdrv_unref(c);
1453 static void test_detach_by_parent_cb(void)
1455 test_detach_indirect(true);
1458 static void test_detach_by_driver_cb(void)
1460 test_detach_indirect(false);
1463 static void test_append_to_drained(void)
1465 BlockBackend *blk;
1466 BlockDriverState *base, *overlay;
1467 BDRVTestState *base_s, *overlay_s;
1469 blk = blk_new(BLK_PERM_ALL, BLK_PERM_ALL);
1470 base = bdrv_new_open_driver(&bdrv_test, "base", BDRV_O_RDWR, &error_abort);
1471 base_s = base->opaque;
1472 blk_insert_bs(blk, base, &error_abort);
1474 overlay = bdrv_new_open_driver(&bdrv_test, "overlay", BDRV_O_RDWR,
1475 &error_abort);
1476 overlay_s = overlay->opaque;
1478 do_drain_begin(BDRV_DRAIN, base);
1479 g_assert_cmpint(base->quiesce_counter, ==, 1);
1480 g_assert_cmpint(base_s->drain_count, ==, 1);
1481 g_assert_cmpint(base->in_flight, ==, 0);
1483 /* Takes ownership of overlay, so we don't have to unref it later */
1484 bdrv_append(overlay, base, &error_abort);
1485 g_assert_cmpint(base->in_flight, ==, 0);
1486 g_assert_cmpint(overlay->in_flight, ==, 0);
1488 g_assert_cmpint(base->quiesce_counter, ==, 1);
1489 g_assert_cmpint(base_s->drain_count, ==, 1);
1490 g_assert_cmpint(overlay->quiesce_counter, ==, 1);
1491 g_assert_cmpint(overlay_s->drain_count, ==, 1);
1493 do_drain_end(BDRV_DRAIN, base);
1495 g_assert_cmpint(base->quiesce_counter, ==, 0);
1496 g_assert_cmpint(base_s->drain_count, ==, 0);
1497 g_assert_cmpint(overlay->quiesce_counter, ==, 0);
1498 g_assert_cmpint(overlay_s->drain_count, ==, 0);
1500 bdrv_unref(base);
1501 blk_unref(blk);
1504 static void test_set_aio_context(void)
1506 BlockDriverState *bs;
1507 IOThread *a = iothread_new();
1508 IOThread *b = iothread_new();
1509 AioContext *ctx_a = iothread_get_aio_context(a);
1510 AioContext *ctx_b = iothread_get_aio_context(b);
1512 bs = bdrv_new_open_driver(&bdrv_test, "test-node", BDRV_O_RDWR,
1513 &error_abort);
1515 bdrv_drained_begin(bs);
1516 bdrv_set_aio_context(bs, ctx_a);
1518 aio_context_acquire(ctx_a);
1519 bdrv_drained_end(bs);
1521 bdrv_drained_begin(bs);
1522 bdrv_set_aio_context(bs, ctx_b);
1523 aio_context_release(ctx_a);
1524 aio_context_acquire(ctx_b);
1525 bdrv_set_aio_context(bs, qemu_get_aio_context());
1526 aio_context_release(ctx_b);
1527 bdrv_drained_end(bs);
1529 bdrv_unref(bs);
1530 iothread_join(a);
1531 iothread_join(b);
1534 int main(int argc, char **argv)
1536 int ret;
1538 bdrv_init();
1539 qemu_init_main_loop(&error_abort);
1541 g_test_init(&argc, &argv, NULL);
1542 qemu_event_init(&done_event, false);
1544 g_test_add_func("/bdrv-drain/driver-cb/drain_all", test_drv_cb_drain_all);
1545 g_test_add_func("/bdrv-drain/driver-cb/drain", test_drv_cb_drain);
1546 g_test_add_func("/bdrv-drain/driver-cb/drain_subtree",
1547 test_drv_cb_drain_subtree);
1549 g_test_add_func("/bdrv-drain/driver-cb/co/drain_all",
1550 test_drv_cb_co_drain_all);
1551 g_test_add_func("/bdrv-drain/driver-cb/co/drain", test_drv_cb_co_drain);
1552 g_test_add_func("/bdrv-drain/driver-cb/co/drain_subtree",
1553 test_drv_cb_co_drain_subtree);
1556 g_test_add_func("/bdrv-drain/quiesce/drain_all", test_quiesce_drain_all);
1557 g_test_add_func("/bdrv-drain/quiesce/drain", test_quiesce_drain);
1558 g_test_add_func("/bdrv-drain/quiesce/drain_subtree",
1559 test_quiesce_drain_subtree);
1561 g_test_add_func("/bdrv-drain/quiesce/co/drain_all",
1562 test_quiesce_co_drain_all);
1563 g_test_add_func("/bdrv-drain/quiesce/co/drain", test_quiesce_co_drain);
1564 g_test_add_func("/bdrv-drain/quiesce/co/drain_subtree",
1565 test_quiesce_co_drain_subtree);
1567 g_test_add_func("/bdrv-drain/nested", test_nested);
1568 g_test_add_func("/bdrv-drain/multiparent", test_multiparent);
1570 g_test_add_func("/bdrv-drain/graph-change/drain_subtree",
1571 test_graph_change_drain_subtree);
1572 g_test_add_func("/bdrv-drain/graph-change/drain_all",
1573 test_graph_change_drain_all);
1575 g_test_add_func("/bdrv-drain/iothread/drain_all", test_iothread_drain_all);
1576 g_test_add_func("/bdrv-drain/iothread/drain", test_iothread_drain);
1577 g_test_add_func("/bdrv-drain/iothread/drain_subtree",
1578 test_iothread_drain_subtree);
1580 g_test_add_func("/bdrv-drain/blockjob/drain_all", test_blockjob_drain_all);
1581 g_test_add_func("/bdrv-drain/blockjob/drain", test_blockjob_drain);
1582 g_test_add_func("/bdrv-drain/blockjob/drain_subtree",
1583 test_blockjob_drain_subtree);
1585 g_test_add_func("/bdrv-drain/blockjob/error/drain_all",
1586 test_blockjob_error_drain_all);
1587 g_test_add_func("/bdrv-drain/blockjob/error/drain",
1588 test_blockjob_error_drain);
1589 g_test_add_func("/bdrv-drain/blockjob/error/drain_subtree",
1590 test_blockjob_error_drain_subtree);
1592 g_test_add_func("/bdrv-drain/blockjob/iothread/drain_all",
1593 test_blockjob_iothread_drain_all);
1594 g_test_add_func("/bdrv-drain/blockjob/iothread/drain",
1595 test_blockjob_iothread_drain);
1596 g_test_add_func("/bdrv-drain/blockjob/iothread/drain_subtree",
1597 test_blockjob_iothread_drain_subtree);
1599 g_test_add_func("/bdrv-drain/blockjob/iothread/error/drain_all",
1600 test_blockjob_iothread_error_drain_all);
1601 g_test_add_func("/bdrv-drain/blockjob/iothread/error/drain",
1602 test_blockjob_iothread_error_drain);
1603 g_test_add_func("/bdrv-drain/blockjob/iothread/error/drain_subtree",
1604 test_blockjob_iothread_error_drain_subtree);
1606 g_test_add_func("/bdrv-drain/deletion/drain", test_delete_by_drain);
1607 g_test_add_func("/bdrv-drain/detach/drain_all", test_detach_by_drain_all);
1608 g_test_add_func("/bdrv-drain/detach/drain", test_detach_by_drain);
1609 g_test_add_func("/bdrv-drain/detach/drain_subtree", test_detach_by_drain_subtree);
1610 g_test_add_func("/bdrv-drain/detach/parent_cb", test_detach_by_parent_cb);
1611 g_test_add_func("/bdrv-drain/detach/driver_cb", test_detach_by_driver_cb);
1613 g_test_add_func("/bdrv-drain/attach/drain", test_append_to_drained);
1615 g_test_add_func("/bdrv-drain/set_aio_context", test_set_aio_context);
1617 ret = g_test_run();
1618 qemu_event_destroy(&done_event);
1619 return ret;