xen: duplicate xen_disk.c as basis of dataplane/xen-block.c
[qemu.git] / tests / test-bdrv-drain.c
blobee1740ff06df9f28a1767254259ad06f3d05a6da
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;
208 struct iovec iov = {
209 .iov_base = NULL,
210 .iov_len = 0,
212 qemu_iovec_init_external(&qiov, &iov, 1);
214 blk = blk_new(BLK_PERM_ALL, BLK_PERM_ALL);
215 bs = bdrv_new_open_driver(&bdrv_test, "test-node", BDRV_O_RDWR,
216 &error_abort);
217 s = bs->opaque;
218 blk_insert_bs(blk, bs, &error_abort);
220 backing = bdrv_new_open_driver(&bdrv_test, "backing", 0, &error_abort);
221 backing_s = backing->opaque;
222 bdrv_set_backing_hd(bs, backing, &error_abort);
224 /* Simple bdrv_drain_all_begin/end pair, check that CBs are called */
225 g_assert_cmpint(s->drain_count, ==, 0);
226 g_assert_cmpint(backing_s->drain_count, ==, 0);
228 do_drain_begin(drain_type, bs);
230 g_assert_cmpint(s->drain_count, ==, 1);
231 g_assert_cmpint(backing_s->drain_count, ==, !!recursive);
233 do_drain_end(drain_type, bs);
235 g_assert_cmpint(s->drain_count, ==, 0);
236 g_assert_cmpint(backing_s->drain_count, ==, 0);
238 /* Now do the same while a request is pending */
239 aio_ret = -EINPROGRESS;
240 acb = blk_aio_preadv(blk, 0, &qiov, 0, aio_ret_cb, &aio_ret);
241 g_assert(acb != NULL);
242 g_assert_cmpint(aio_ret, ==, -EINPROGRESS);
244 g_assert_cmpint(s->drain_count, ==, 0);
245 g_assert_cmpint(backing_s->drain_count, ==, 0);
247 do_drain_begin(drain_type, bs);
249 g_assert_cmpint(aio_ret, ==, 0);
250 g_assert_cmpint(s->drain_count, ==, 1);
251 g_assert_cmpint(backing_s->drain_count, ==, !!recursive);
253 do_drain_end(drain_type, bs);
255 g_assert_cmpint(s->drain_count, ==, 0);
256 g_assert_cmpint(backing_s->drain_count, ==, 0);
258 bdrv_unref(backing);
259 bdrv_unref(bs);
260 blk_unref(blk);
263 static void test_drv_cb_drain_all(void)
265 test_drv_cb_common(BDRV_DRAIN_ALL, true);
268 static void test_drv_cb_drain(void)
270 test_drv_cb_common(BDRV_DRAIN, false);
273 static void test_drv_cb_drain_subtree(void)
275 test_drv_cb_common(BDRV_SUBTREE_DRAIN, true);
278 static void test_drv_cb_co_drain_all(void)
280 call_in_coroutine(test_drv_cb_drain_all);
283 static void test_drv_cb_co_drain(void)
285 call_in_coroutine(test_drv_cb_drain);
288 static void test_drv_cb_co_drain_subtree(void)
290 call_in_coroutine(test_drv_cb_drain_subtree);
293 static void test_quiesce_common(enum drain_type drain_type, bool recursive)
295 BlockBackend *blk;
296 BlockDriverState *bs, *backing;
298 blk = blk_new(BLK_PERM_ALL, BLK_PERM_ALL);
299 bs = bdrv_new_open_driver(&bdrv_test, "test-node", BDRV_O_RDWR,
300 &error_abort);
301 blk_insert_bs(blk, bs, &error_abort);
303 backing = bdrv_new_open_driver(&bdrv_test, "backing", 0, &error_abort);
304 bdrv_set_backing_hd(bs, backing, &error_abort);
306 g_assert_cmpint(bs->quiesce_counter, ==, 0);
307 g_assert_cmpint(backing->quiesce_counter, ==, 0);
309 do_drain_begin(drain_type, bs);
311 g_assert_cmpint(bs->quiesce_counter, ==, 1);
312 g_assert_cmpint(backing->quiesce_counter, ==, !!recursive);
314 do_drain_end(drain_type, bs);
316 g_assert_cmpint(bs->quiesce_counter, ==, 0);
317 g_assert_cmpint(backing->quiesce_counter, ==, 0);
319 bdrv_unref(backing);
320 bdrv_unref(bs);
321 blk_unref(blk);
324 static void test_quiesce_drain_all(void)
326 test_quiesce_common(BDRV_DRAIN_ALL, true);
329 static void test_quiesce_drain(void)
331 test_quiesce_common(BDRV_DRAIN, false);
334 static void test_quiesce_drain_subtree(void)
336 test_quiesce_common(BDRV_SUBTREE_DRAIN, true);
339 static void test_quiesce_co_drain_all(void)
341 call_in_coroutine(test_quiesce_drain_all);
344 static void test_quiesce_co_drain(void)
346 call_in_coroutine(test_quiesce_drain);
349 static void test_quiesce_co_drain_subtree(void)
351 call_in_coroutine(test_quiesce_drain_subtree);
354 static void test_nested(void)
356 BlockBackend *blk;
357 BlockDriverState *bs, *backing;
358 BDRVTestState *s, *backing_s;
359 enum drain_type outer, inner;
361 blk = blk_new(BLK_PERM_ALL, BLK_PERM_ALL);
362 bs = bdrv_new_open_driver(&bdrv_test, "test-node", BDRV_O_RDWR,
363 &error_abort);
364 s = bs->opaque;
365 blk_insert_bs(blk, bs, &error_abort);
367 backing = bdrv_new_open_driver(&bdrv_test, "backing", 0, &error_abort);
368 backing_s = backing->opaque;
369 bdrv_set_backing_hd(bs, backing, &error_abort);
371 for (outer = 0; outer < DRAIN_TYPE_MAX; outer++) {
372 for (inner = 0; inner < DRAIN_TYPE_MAX; inner++) {
373 int backing_quiesce = (outer != BDRV_DRAIN) +
374 (inner != BDRV_DRAIN);
376 g_assert_cmpint(bs->quiesce_counter, ==, 0);
377 g_assert_cmpint(backing->quiesce_counter, ==, 0);
378 g_assert_cmpint(s->drain_count, ==, 0);
379 g_assert_cmpint(backing_s->drain_count, ==, 0);
381 do_drain_begin(outer, bs);
382 do_drain_begin(inner, bs);
384 g_assert_cmpint(bs->quiesce_counter, ==, 2);
385 g_assert_cmpint(backing->quiesce_counter, ==, backing_quiesce);
386 g_assert_cmpint(s->drain_count, ==, 2);
387 g_assert_cmpint(backing_s->drain_count, ==, backing_quiesce);
389 do_drain_end(inner, bs);
390 do_drain_end(outer, bs);
392 g_assert_cmpint(bs->quiesce_counter, ==, 0);
393 g_assert_cmpint(backing->quiesce_counter, ==, 0);
394 g_assert_cmpint(s->drain_count, ==, 0);
395 g_assert_cmpint(backing_s->drain_count, ==, 0);
399 bdrv_unref(backing);
400 bdrv_unref(bs);
401 blk_unref(blk);
404 static void test_multiparent(void)
406 BlockBackend *blk_a, *blk_b;
407 BlockDriverState *bs_a, *bs_b, *backing;
408 BDRVTestState *a_s, *b_s, *backing_s;
410 blk_a = blk_new(BLK_PERM_ALL, BLK_PERM_ALL);
411 bs_a = bdrv_new_open_driver(&bdrv_test, "test-node-a", BDRV_O_RDWR,
412 &error_abort);
413 a_s = bs_a->opaque;
414 blk_insert_bs(blk_a, bs_a, &error_abort);
416 blk_b = blk_new(BLK_PERM_ALL, BLK_PERM_ALL);
417 bs_b = bdrv_new_open_driver(&bdrv_test, "test-node-b", BDRV_O_RDWR,
418 &error_abort);
419 b_s = bs_b->opaque;
420 blk_insert_bs(blk_b, bs_b, &error_abort);
422 backing = bdrv_new_open_driver(&bdrv_test, "backing", 0, &error_abort);
423 backing_s = backing->opaque;
424 bdrv_set_backing_hd(bs_a, backing, &error_abort);
425 bdrv_set_backing_hd(bs_b, backing, &error_abort);
427 g_assert_cmpint(bs_a->quiesce_counter, ==, 0);
428 g_assert_cmpint(bs_b->quiesce_counter, ==, 0);
429 g_assert_cmpint(backing->quiesce_counter, ==, 0);
430 g_assert_cmpint(a_s->drain_count, ==, 0);
431 g_assert_cmpint(b_s->drain_count, ==, 0);
432 g_assert_cmpint(backing_s->drain_count, ==, 0);
434 do_drain_begin(BDRV_SUBTREE_DRAIN, bs_a);
436 g_assert_cmpint(bs_a->quiesce_counter, ==, 1);
437 g_assert_cmpint(bs_b->quiesce_counter, ==, 1);
438 g_assert_cmpint(backing->quiesce_counter, ==, 1);
439 g_assert_cmpint(a_s->drain_count, ==, 1);
440 g_assert_cmpint(b_s->drain_count, ==, 1);
441 g_assert_cmpint(backing_s->drain_count, ==, 1);
443 do_drain_begin(BDRV_SUBTREE_DRAIN, bs_b);
445 g_assert_cmpint(bs_a->quiesce_counter, ==, 2);
446 g_assert_cmpint(bs_b->quiesce_counter, ==, 2);
447 g_assert_cmpint(backing->quiesce_counter, ==, 2);
448 g_assert_cmpint(a_s->drain_count, ==, 2);
449 g_assert_cmpint(b_s->drain_count, ==, 2);
450 g_assert_cmpint(backing_s->drain_count, ==, 2);
452 do_drain_end(BDRV_SUBTREE_DRAIN, bs_b);
454 g_assert_cmpint(bs_a->quiesce_counter, ==, 1);
455 g_assert_cmpint(bs_b->quiesce_counter, ==, 1);
456 g_assert_cmpint(backing->quiesce_counter, ==, 1);
457 g_assert_cmpint(a_s->drain_count, ==, 1);
458 g_assert_cmpint(b_s->drain_count, ==, 1);
459 g_assert_cmpint(backing_s->drain_count, ==, 1);
461 do_drain_end(BDRV_SUBTREE_DRAIN, bs_a);
463 g_assert_cmpint(bs_a->quiesce_counter, ==, 0);
464 g_assert_cmpint(bs_b->quiesce_counter, ==, 0);
465 g_assert_cmpint(backing->quiesce_counter, ==, 0);
466 g_assert_cmpint(a_s->drain_count, ==, 0);
467 g_assert_cmpint(b_s->drain_count, ==, 0);
468 g_assert_cmpint(backing_s->drain_count, ==, 0);
470 bdrv_unref(backing);
471 bdrv_unref(bs_a);
472 bdrv_unref(bs_b);
473 blk_unref(blk_a);
474 blk_unref(blk_b);
477 static void test_graph_change_drain_subtree(void)
479 BlockBackend *blk_a, *blk_b;
480 BlockDriverState *bs_a, *bs_b, *backing;
481 BDRVTestState *a_s, *b_s, *backing_s;
483 blk_a = blk_new(BLK_PERM_ALL, BLK_PERM_ALL);
484 bs_a = bdrv_new_open_driver(&bdrv_test, "test-node-a", BDRV_O_RDWR,
485 &error_abort);
486 a_s = bs_a->opaque;
487 blk_insert_bs(blk_a, bs_a, &error_abort);
489 blk_b = blk_new(BLK_PERM_ALL, BLK_PERM_ALL);
490 bs_b = bdrv_new_open_driver(&bdrv_test, "test-node-b", BDRV_O_RDWR,
491 &error_abort);
492 b_s = bs_b->opaque;
493 blk_insert_bs(blk_b, bs_b, &error_abort);
495 backing = bdrv_new_open_driver(&bdrv_test, "backing", 0, &error_abort);
496 backing_s = backing->opaque;
497 bdrv_set_backing_hd(bs_a, backing, &error_abort);
499 g_assert_cmpint(bs_a->quiesce_counter, ==, 0);
500 g_assert_cmpint(bs_b->quiesce_counter, ==, 0);
501 g_assert_cmpint(backing->quiesce_counter, ==, 0);
502 g_assert_cmpint(a_s->drain_count, ==, 0);
503 g_assert_cmpint(b_s->drain_count, ==, 0);
504 g_assert_cmpint(backing_s->drain_count, ==, 0);
506 do_drain_begin(BDRV_SUBTREE_DRAIN, bs_a);
507 do_drain_begin(BDRV_SUBTREE_DRAIN, bs_a);
508 do_drain_begin(BDRV_SUBTREE_DRAIN, bs_a);
509 do_drain_begin(BDRV_SUBTREE_DRAIN, bs_b);
510 do_drain_begin(BDRV_SUBTREE_DRAIN, bs_b);
512 bdrv_set_backing_hd(bs_b, backing, &error_abort);
513 g_assert_cmpint(bs_a->quiesce_counter, ==, 5);
514 g_assert_cmpint(bs_b->quiesce_counter, ==, 5);
515 g_assert_cmpint(backing->quiesce_counter, ==, 5);
516 g_assert_cmpint(a_s->drain_count, ==, 5);
517 g_assert_cmpint(b_s->drain_count, ==, 5);
518 g_assert_cmpint(backing_s->drain_count, ==, 5);
520 bdrv_set_backing_hd(bs_b, NULL, &error_abort);
521 g_assert_cmpint(bs_a->quiesce_counter, ==, 3);
522 g_assert_cmpint(bs_b->quiesce_counter, ==, 2);
523 g_assert_cmpint(backing->quiesce_counter, ==, 3);
524 g_assert_cmpint(a_s->drain_count, ==, 3);
525 g_assert_cmpint(b_s->drain_count, ==, 2);
526 g_assert_cmpint(backing_s->drain_count, ==, 3);
528 bdrv_set_backing_hd(bs_b, backing, &error_abort);
529 g_assert_cmpint(bs_a->quiesce_counter, ==, 5);
530 g_assert_cmpint(bs_b->quiesce_counter, ==, 5);
531 g_assert_cmpint(backing->quiesce_counter, ==, 5);
532 g_assert_cmpint(a_s->drain_count, ==, 5);
533 g_assert_cmpint(b_s->drain_count, ==, 5);
534 g_assert_cmpint(backing_s->drain_count, ==, 5);
536 do_drain_end(BDRV_SUBTREE_DRAIN, bs_b);
537 do_drain_end(BDRV_SUBTREE_DRAIN, bs_b);
538 do_drain_end(BDRV_SUBTREE_DRAIN, bs_a);
539 do_drain_end(BDRV_SUBTREE_DRAIN, bs_a);
540 do_drain_end(BDRV_SUBTREE_DRAIN, bs_a);
542 g_assert_cmpint(bs_a->quiesce_counter, ==, 0);
543 g_assert_cmpint(bs_b->quiesce_counter, ==, 0);
544 g_assert_cmpint(backing->quiesce_counter, ==, 0);
545 g_assert_cmpint(a_s->drain_count, ==, 0);
546 g_assert_cmpint(b_s->drain_count, ==, 0);
547 g_assert_cmpint(backing_s->drain_count, ==, 0);
549 bdrv_unref(backing);
550 bdrv_unref(bs_a);
551 bdrv_unref(bs_b);
552 blk_unref(blk_a);
553 blk_unref(blk_b);
556 static void test_graph_change_drain_all(void)
558 BlockBackend *blk_a, *blk_b;
559 BlockDriverState *bs_a, *bs_b;
560 BDRVTestState *a_s, *b_s;
562 /* Create node A with a BlockBackend */
563 blk_a = blk_new(BLK_PERM_ALL, BLK_PERM_ALL);
564 bs_a = bdrv_new_open_driver(&bdrv_test, "test-node-a", BDRV_O_RDWR,
565 &error_abort);
566 a_s = bs_a->opaque;
567 blk_insert_bs(blk_a, bs_a, &error_abort);
569 g_assert_cmpint(bs_a->quiesce_counter, ==, 0);
570 g_assert_cmpint(a_s->drain_count, ==, 0);
572 /* Call bdrv_drain_all_begin() */
573 bdrv_drain_all_begin();
575 g_assert_cmpint(bs_a->quiesce_counter, ==, 1);
576 g_assert_cmpint(a_s->drain_count, ==, 1);
578 /* Create node B with a BlockBackend */
579 blk_b = blk_new(BLK_PERM_ALL, BLK_PERM_ALL);
580 bs_b = bdrv_new_open_driver(&bdrv_test, "test-node-b", BDRV_O_RDWR,
581 &error_abort);
582 b_s = bs_b->opaque;
583 blk_insert_bs(blk_b, bs_b, &error_abort);
585 g_assert_cmpint(bs_a->quiesce_counter, ==, 1);
586 g_assert_cmpint(bs_b->quiesce_counter, ==, 1);
587 g_assert_cmpint(a_s->drain_count, ==, 1);
588 g_assert_cmpint(b_s->drain_count, ==, 1);
590 /* Unref and finally delete node A */
591 blk_unref(blk_a);
593 g_assert_cmpint(bs_a->quiesce_counter, ==, 1);
594 g_assert_cmpint(bs_b->quiesce_counter, ==, 1);
595 g_assert_cmpint(a_s->drain_count, ==, 1);
596 g_assert_cmpint(b_s->drain_count, ==, 1);
598 bdrv_unref(bs_a);
600 g_assert_cmpint(bs_b->quiesce_counter, ==, 1);
601 g_assert_cmpint(b_s->drain_count, ==, 1);
603 /* End the drained section */
604 bdrv_drain_all_end();
606 g_assert_cmpint(bs_b->quiesce_counter, ==, 0);
607 g_assert_cmpint(b_s->drain_count, ==, 0);
609 bdrv_unref(bs_b);
610 blk_unref(blk_b);
613 struct test_iothread_data {
614 BlockDriverState *bs;
615 enum drain_type drain_type;
616 int *aio_ret;
619 static void test_iothread_drain_entry(void *opaque)
621 struct test_iothread_data *data = opaque;
623 aio_context_acquire(bdrv_get_aio_context(data->bs));
624 do_drain_begin(data->drain_type, data->bs);
625 g_assert_cmpint(*data->aio_ret, ==, 0);
626 do_drain_end(data->drain_type, data->bs);
627 aio_context_release(bdrv_get_aio_context(data->bs));
629 qemu_event_set(&done_event);
632 static void test_iothread_aio_cb(void *opaque, int ret)
634 int *aio_ret = opaque;
635 *aio_ret = ret;
636 qemu_event_set(&done_event);
639 static void test_iothread_main_thread_bh(void *opaque)
641 struct test_iothread_data *data = opaque;
643 /* Test that the AioContext is not yet locked in a random BH that is
644 * executed during drain, otherwise this would deadlock. */
645 aio_context_acquire(bdrv_get_aio_context(data->bs));
646 bdrv_flush(data->bs);
647 aio_context_release(bdrv_get_aio_context(data->bs));
651 * Starts an AIO request on a BDS that runs in the AioContext of iothread 1.
652 * The request involves a BH on iothread 2 before it can complete.
654 * @drain_thread = 0 means that do_drain_begin/end are called from the main
655 * thread, @drain_thread = 1 means that they are called from iothread 1. Drain
656 * for this BDS cannot be called from iothread 2 because only the main thread
657 * may do cross-AioContext polling.
659 static void test_iothread_common(enum drain_type drain_type, int drain_thread)
661 BlockBackend *blk;
662 BlockDriverState *bs;
663 BDRVTestState *s;
664 BlockAIOCB *acb;
665 int aio_ret;
666 struct test_iothread_data data;
668 IOThread *a = iothread_new();
669 IOThread *b = iothread_new();
670 AioContext *ctx_a = iothread_get_aio_context(a);
671 AioContext *ctx_b = iothread_get_aio_context(b);
673 QEMUIOVector qiov;
674 struct iovec iov = {
675 .iov_base = NULL,
676 .iov_len = 0,
678 qemu_iovec_init_external(&qiov, &iov, 1);
680 /* bdrv_drain_all() may only be called from the main loop thread */
681 if (drain_type == BDRV_DRAIN_ALL && drain_thread != 0) {
682 goto out;
685 blk = blk_new(BLK_PERM_ALL, BLK_PERM_ALL);
686 bs = bdrv_new_open_driver(&bdrv_test, "test-node", BDRV_O_RDWR,
687 &error_abort);
688 s = bs->opaque;
689 blk_insert_bs(blk, bs, &error_abort);
691 blk_set_aio_context(blk, ctx_a);
692 aio_context_acquire(ctx_a);
694 s->bh_indirection_ctx = ctx_b;
696 aio_ret = -EINPROGRESS;
697 qemu_event_reset(&done_event);
699 if (drain_thread == 0) {
700 acb = blk_aio_preadv(blk, 0, &qiov, 0, test_iothread_aio_cb, &aio_ret);
701 } else {
702 acb = blk_aio_preadv(blk, 0, &qiov, 0, aio_ret_cb, &aio_ret);
704 g_assert(acb != NULL);
705 g_assert_cmpint(aio_ret, ==, -EINPROGRESS);
707 aio_context_release(ctx_a);
709 data = (struct test_iothread_data) {
710 .bs = bs,
711 .drain_type = drain_type,
712 .aio_ret = &aio_ret,
715 switch (drain_thread) {
716 case 0:
717 if (drain_type != BDRV_DRAIN_ALL) {
718 aio_context_acquire(ctx_a);
721 aio_bh_schedule_oneshot(ctx_a, test_iothread_main_thread_bh, &data);
723 /* The request is running on the IOThread a. Draining its block device
724 * will make sure that it has completed as far as the BDS is concerned,
725 * but the drain in this thread can continue immediately after
726 * bdrv_dec_in_flight() and aio_ret might be assigned only slightly
727 * later. */
728 do_drain_begin(drain_type, bs);
729 g_assert_cmpint(bs->in_flight, ==, 0);
731 if (drain_type != BDRV_DRAIN_ALL) {
732 aio_context_release(ctx_a);
734 qemu_event_wait(&done_event);
735 if (drain_type != BDRV_DRAIN_ALL) {
736 aio_context_acquire(ctx_a);
739 g_assert_cmpint(aio_ret, ==, 0);
740 do_drain_end(drain_type, bs);
742 if (drain_type != BDRV_DRAIN_ALL) {
743 aio_context_release(ctx_a);
745 break;
746 case 1:
747 aio_bh_schedule_oneshot(ctx_a, test_iothread_drain_entry, &data);
748 qemu_event_wait(&done_event);
749 break;
750 default:
751 g_assert_not_reached();
754 aio_context_acquire(ctx_a);
755 blk_set_aio_context(blk, qemu_get_aio_context());
756 aio_context_release(ctx_a);
758 bdrv_unref(bs);
759 blk_unref(blk);
761 out:
762 iothread_join(a);
763 iothread_join(b);
766 static void test_iothread_drain_all(void)
768 test_iothread_common(BDRV_DRAIN_ALL, 0);
769 test_iothread_common(BDRV_DRAIN_ALL, 1);
772 static void test_iothread_drain(void)
774 test_iothread_common(BDRV_DRAIN, 0);
775 test_iothread_common(BDRV_DRAIN, 1);
778 static void test_iothread_drain_subtree(void)
780 test_iothread_common(BDRV_SUBTREE_DRAIN, 0);
781 test_iothread_common(BDRV_SUBTREE_DRAIN, 1);
785 typedef struct TestBlockJob {
786 BlockJob common;
787 int run_ret;
788 int prepare_ret;
789 bool running;
790 bool should_complete;
791 } TestBlockJob;
793 static int test_job_prepare(Job *job)
795 TestBlockJob *s = container_of(job, TestBlockJob, common.job);
797 /* Provoke an AIO_WAIT_WHILE() call to verify there is no deadlock */
798 blk_flush(s->common.blk);
799 return s->prepare_ret;
802 static void test_job_commit(Job *job)
804 TestBlockJob *s = container_of(job, TestBlockJob, common.job);
806 /* Provoke an AIO_WAIT_WHILE() call to verify there is no deadlock */
807 blk_flush(s->common.blk);
810 static void test_job_abort(Job *job)
812 TestBlockJob *s = container_of(job, TestBlockJob, common.job);
814 /* Provoke an AIO_WAIT_WHILE() call to verify there is no deadlock */
815 blk_flush(s->common.blk);
818 static int coroutine_fn test_job_run(Job *job, Error **errp)
820 TestBlockJob *s = container_of(job, TestBlockJob, common.job);
822 /* We are running the actual job code past the pause point in
823 * job_co_entry(). */
824 s->running = true;
826 job_transition_to_ready(&s->common.job);
827 while (!s->should_complete) {
828 /* Avoid job_sleep_ns() because it marks the job as !busy. We want to
829 * emulate some actual activity (probably some I/O) here so that drain
830 * has to wait for this activity to stop. */
831 qemu_co_sleep_ns(QEMU_CLOCK_REALTIME, 1000000);
833 job_pause_point(&s->common.job);
836 return s->run_ret;
839 static void test_job_complete(Job *job, Error **errp)
841 TestBlockJob *s = container_of(job, TestBlockJob, common.job);
842 s->should_complete = true;
845 BlockJobDriver test_job_driver = {
846 .job_driver = {
847 .instance_size = sizeof(TestBlockJob),
848 .free = block_job_free,
849 .user_resume = block_job_user_resume,
850 .drain = block_job_drain,
851 .run = test_job_run,
852 .complete = test_job_complete,
853 .prepare = test_job_prepare,
854 .commit = test_job_commit,
855 .abort = test_job_abort,
859 enum test_job_result {
860 TEST_JOB_SUCCESS,
861 TEST_JOB_FAIL_RUN,
862 TEST_JOB_FAIL_PREPARE,
865 enum test_job_drain_node {
866 TEST_JOB_DRAIN_SRC,
867 TEST_JOB_DRAIN_SRC_CHILD,
868 TEST_JOB_DRAIN_SRC_PARENT,
871 static void test_blockjob_common_drain_node(enum drain_type drain_type,
872 bool use_iothread,
873 enum test_job_result result,
874 enum test_job_drain_node drain_node)
876 BlockBackend *blk_src, *blk_target;
877 BlockDriverState *src, *src_backing, *src_overlay, *target, *drain_bs;
878 BlockJob *job;
879 TestBlockJob *tjob;
880 IOThread *iothread = NULL;
881 AioContext *ctx;
882 int ret;
884 src = bdrv_new_open_driver(&bdrv_test, "source", BDRV_O_RDWR,
885 &error_abort);
886 src_backing = bdrv_new_open_driver(&bdrv_test, "source-backing",
887 BDRV_O_RDWR, &error_abort);
888 src_overlay = bdrv_new_open_driver(&bdrv_test, "source-overlay",
889 BDRV_O_RDWR, &error_abort);
891 bdrv_set_backing_hd(src_overlay, src, &error_abort);
892 bdrv_unref(src);
893 bdrv_set_backing_hd(src, src_backing, &error_abort);
894 bdrv_unref(src_backing);
896 blk_src = blk_new(BLK_PERM_ALL, BLK_PERM_ALL);
897 blk_insert_bs(blk_src, src_overlay, &error_abort);
899 switch (drain_node) {
900 case TEST_JOB_DRAIN_SRC:
901 drain_bs = src;
902 break;
903 case TEST_JOB_DRAIN_SRC_CHILD:
904 drain_bs = src_backing;
905 break;
906 case TEST_JOB_DRAIN_SRC_PARENT:
907 drain_bs = src_overlay;
908 break;
909 default:
910 g_assert_not_reached();
913 if (use_iothread) {
914 iothread = iothread_new();
915 ctx = iothread_get_aio_context(iothread);
916 blk_set_aio_context(blk_src, ctx);
917 } else {
918 ctx = qemu_get_aio_context();
921 target = bdrv_new_open_driver(&bdrv_test, "target", BDRV_O_RDWR,
922 &error_abort);
923 blk_target = blk_new(BLK_PERM_ALL, BLK_PERM_ALL);
924 blk_insert_bs(blk_target, target, &error_abort);
926 aio_context_acquire(ctx);
927 tjob = block_job_create("job0", &test_job_driver, NULL, src,
928 0, BLK_PERM_ALL,
929 0, 0, NULL, NULL, &error_abort);
930 job = &tjob->common;
931 block_job_add_bdrv(job, "target", target, 0, BLK_PERM_ALL, &error_abort);
933 switch (result) {
934 case TEST_JOB_SUCCESS:
935 break;
936 case TEST_JOB_FAIL_RUN:
937 tjob->run_ret = -EIO;
938 break;
939 case TEST_JOB_FAIL_PREPARE:
940 tjob->prepare_ret = -EIO;
941 break;
944 job_start(&job->job);
945 aio_context_release(ctx);
947 if (use_iothread) {
948 /* job_co_entry() is run in the I/O thread, wait for the actual job
949 * code to start (we don't want to catch the job in the pause point in
950 * job_co_entry(). */
951 while (!tjob->running) {
952 aio_poll(qemu_get_aio_context(), false);
956 g_assert_cmpint(job->job.pause_count, ==, 0);
957 g_assert_false(job->job.paused);
958 g_assert_true(tjob->running);
959 g_assert_true(job->job.busy); /* We're in qemu_co_sleep_ns() */
961 do_drain_begin_unlocked(drain_type, drain_bs);
963 if (drain_type == BDRV_DRAIN_ALL) {
964 /* bdrv_drain_all() drains both src and target */
965 g_assert_cmpint(job->job.pause_count, ==, 2);
966 } else {
967 g_assert_cmpint(job->job.pause_count, ==, 1);
969 g_assert_true(job->job.paused);
970 g_assert_false(job->job.busy); /* The job is paused */
972 do_drain_end_unlocked(drain_type, drain_bs);
974 if (use_iothread) {
975 /* paused is reset in the I/O thread, wait for it */
976 while (job->job.paused) {
977 aio_poll(qemu_get_aio_context(), false);
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() */
985 do_drain_begin(drain_type, target);
987 if (drain_type == BDRV_DRAIN_ALL) {
988 /* bdrv_drain_all() drains both src and target */
989 g_assert_cmpint(job->job.pause_count, ==, 2);
990 } else {
991 g_assert_cmpint(job->job.pause_count, ==, 1);
993 g_assert_true(job->job.paused);
994 g_assert_false(job->job.busy); /* The job is paused */
996 do_drain_end(drain_type, target);
998 if (use_iothread) {
999 /* paused is reset in the I/O thread, wait for it */
1000 while (job->job.paused) {
1001 aio_poll(qemu_get_aio_context(), false);
1005 g_assert_cmpint(job->job.pause_count, ==, 0);
1006 g_assert_false(job->job.paused);
1007 g_assert_true(job->job.busy); /* We're in qemu_co_sleep_ns() */
1009 aio_context_acquire(ctx);
1010 ret = job_complete_sync(&job->job, &error_abort);
1011 g_assert_cmpint(ret, ==, (result == TEST_JOB_SUCCESS ? 0 : -EIO));
1013 if (use_iothread) {
1014 blk_set_aio_context(blk_src, qemu_get_aio_context());
1016 aio_context_release(ctx);
1018 blk_unref(blk_src);
1019 blk_unref(blk_target);
1020 bdrv_unref(src_overlay);
1021 bdrv_unref(target);
1023 if (iothread) {
1024 iothread_join(iothread);
1028 static void test_blockjob_common(enum drain_type drain_type, bool use_iothread,
1029 enum test_job_result result)
1031 test_blockjob_common_drain_node(drain_type, use_iothread, result,
1032 TEST_JOB_DRAIN_SRC);
1033 test_blockjob_common_drain_node(drain_type, use_iothread, result,
1034 TEST_JOB_DRAIN_SRC_CHILD);
1035 if (drain_type == BDRV_SUBTREE_DRAIN) {
1036 test_blockjob_common_drain_node(drain_type, use_iothread, result,
1037 TEST_JOB_DRAIN_SRC_PARENT);
1041 static void test_blockjob_drain_all(void)
1043 test_blockjob_common(BDRV_DRAIN_ALL, false, TEST_JOB_SUCCESS);
1046 static void test_blockjob_drain(void)
1048 test_blockjob_common(BDRV_DRAIN, false, TEST_JOB_SUCCESS);
1051 static void test_blockjob_drain_subtree(void)
1053 test_blockjob_common(BDRV_SUBTREE_DRAIN, false, TEST_JOB_SUCCESS);
1056 static void test_blockjob_error_drain_all(void)
1058 test_blockjob_common(BDRV_DRAIN_ALL, false, TEST_JOB_FAIL_RUN);
1059 test_blockjob_common(BDRV_DRAIN_ALL, false, TEST_JOB_FAIL_PREPARE);
1062 static void test_blockjob_error_drain(void)
1064 test_blockjob_common(BDRV_DRAIN, false, TEST_JOB_FAIL_RUN);
1065 test_blockjob_common(BDRV_DRAIN, false, TEST_JOB_FAIL_PREPARE);
1068 static void test_blockjob_error_drain_subtree(void)
1070 test_blockjob_common(BDRV_SUBTREE_DRAIN, false, TEST_JOB_FAIL_RUN);
1071 test_blockjob_common(BDRV_SUBTREE_DRAIN, false, TEST_JOB_FAIL_PREPARE);
1074 static void test_blockjob_iothread_drain_all(void)
1076 test_blockjob_common(BDRV_DRAIN_ALL, true, TEST_JOB_SUCCESS);
1079 static void test_blockjob_iothread_drain(void)
1081 test_blockjob_common(BDRV_DRAIN, true, TEST_JOB_SUCCESS);
1084 static void test_blockjob_iothread_drain_subtree(void)
1086 test_blockjob_common(BDRV_SUBTREE_DRAIN, true, TEST_JOB_SUCCESS);
1089 static void test_blockjob_iothread_error_drain_all(void)
1091 test_blockjob_common(BDRV_DRAIN_ALL, true, TEST_JOB_FAIL_RUN);
1092 test_blockjob_common(BDRV_DRAIN_ALL, true, TEST_JOB_FAIL_PREPARE);
1095 static void test_blockjob_iothread_error_drain(void)
1097 test_blockjob_common(BDRV_DRAIN, true, TEST_JOB_FAIL_RUN);
1098 test_blockjob_common(BDRV_DRAIN, true, TEST_JOB_FAIL_PREPARE);
1101 static void test_blockjob_iothread_error_drain_subtree(void)
1103 test_blockjob_common(BDRV_SUBTREE_DRAIN, true, TEST_JOB_FAIL_RUN);
1104 test_blockjob_common(BDRV_SUBTREE_DRAIN, true, TEST_JOB_FAIL_PREPARE);
1108 typedef struct BDRVTestTopState {
1109 BdrvChild *wait_child;
1110 } BDRVTestTopState;
1112 static void bdrv_test_top_close(BlockDriverState *bs)
1114 BdrvChild *c, *next_c;
1115 QLIST_FOREACH_SAFE(c, &bs->children, next, next_c) {
1116 bdrv_unref_child(bs, c);
1120 static int coroutine_fn bdrv_test_top_co_preadv(BlockDriverState *bs,
1121 uint64_t offset, uint64_t bytes,
1122 QEMUIOVector *qiov, int flags)
1124 BDRVTestTopState *tts = bs->opaque;
1125 return bdrv_co_preadv(tts->wait_child, offset, bytes, qiov, flags);
1128 static BlockDriver bdrv_test_top_driver = {
1129 .format_name = "test_top_driver",
1130 .instance_size = sizeof(BDRVTestTopState),
1132 .bdrv_close = bdrv_test_top_close,
1133 .bdrv_co_preadv = bdrv_test_top_co_preadv,
1135 .bdrv_child_perm = bdrv_format_default_perms,
1138 typedef struct TestCoDeleteByDrainData {
1139 BlockBackend *blk;
1140 bool detach_instead_of_delete;
1141 bool done;
1142 } TestCoDeleteByDrainData;
1144 static void coroutine_fn test_co_delete_by_drain(void *opaque)
1146 TestCoDeleteByDrainData *dbdd = opaque;
1147 BlockBackend *blk = dbdd->blk;
1148 BlockDriverState *bs = blk_bs(blk);
1149 BDRVTestTopState *tts = bs->opaque;
1150 void *buffer = g_malloc(65536);
1151 QEMUIOVector qiov;
1152 struct iovec iov = {
1153 .iov_base = buffer,
1154 .iov_len = 65536,
1157 qemu_iovec_init_external(&qiov, &iov, 1);
1159 /* Pretend some internal write operation from parent to child.
1160 * Important: We have to read from the child, not from the parent!
1161 * Draining works by first propagating it all up the tree to the
1162 * root and then waiting for drainage from root to the leaves
1163 * (protocol nodes). If we have a request waiting on the root,
1164 * everything will be drained before we go back down the tree, but
1165 * we do not want that. We want to be in the middle of draining
1166 * when this following requests returns. */
1167 bdrv_co_preadv(tts->wait_child, 0, 65536, &qiov, 0);
1169 g_assert_cmpint(bs->refcnt, ==, 1);
1171 if (!dbdd->detach_instead_of_delete) {
1172 blk_unref(blk);
1173 } else {
1174 BdrvChild *c, *next_c;
1175 QLIST_FOREACH_SAFE(c, &bs->children, next, next_c) {
1176 bdrv_unref_child(bs, c);
1180 dbdd->done = true;
1181 g_free(buffer);
1185 * Test what happens when some BDS has some children, you drain one of
1186 * them and this results in the BDS being deleted.
1188 * If @detach_instead_of_delete is set, the BDS is not going to be
1189 * deleted but will only detach all of its children.
1191 static void do_test_delete_by_drain(bool detach_instead_of_delete,
1192 enum drain_type drain_type)
1194 BlockBackend *blk;
1195 BlockDriverState *bs, *child_bs, *null_bs;
1196 BDRVTestTopState *tts;
1197 TestCoDeleteByDrainData dbdd;
1198 Coroutine *co;
1200 bs = bdrv_new_open_driver(&bdrv_test_top_driver, "top", BDRV_O_RDWR,
1201 &error_abort);
1202 bs->total_sectors = 65536 >> BDRV_SECTOR_BITS;
1203 tts = bs->opaque;
1205 null_bs = bdrv_open("null-co://", NULL, NULL, BDRV_O_RDWR | BDRV_O_PROTOCOL,
1206 &error_abort);
1207 bdrv_attach_child(bs, null_bs, "null-child", &child_file, &error_abort);
1209 /* This child will be the one to pass to requests through to, and
1210 * it will stall until a drain occurs */
1211 child_bs = bdrv_new_open_driver(&bdrv_test, "child", BDRV_O_RDWR,
1212 &error_abort);
1213 child_bs->total_sectors = 65536 >> BDRV_SECTOR_BITS;
1214 /* Takes our reference to child_bs */
1215 tts->wait_child = bdrv_attach_child(bs, child_bs, "wait-child", &child_file,
1216 &error_abort);
1218 /* This child is just there to be deleted
1219 * (for detach_instead_of_delete == true) */
1220 null_bs = bdrv_open("null-co://", NULL, NULL, BDRV_O_RDWR | BDRV_O_PROTOCOL,
1221 &error_abort);
1222 bdrv_attach_child(bs, null_bs, "null-child", &child_file, &error_abort);
1224 blk = blk_new(BLK_PERM_ALL, BLK_PERM_ALL);
1225 blk_insert_bs(blk, bs, &error_abort);
1227 /* Referenced by blk now */
1228 bdrv_unref(bs);
1230 g_assert_cmpint(bs->refcnt, ==, 1);
1231 g_assert_cmpint(child_bs->refcnt, ==, 1);
1232 g_assert_cmpint(null_bs->refcnt, ==, 1);
1235 dbdd = (TestCoDeleteByDrainData){
1236 .blk = blk,
1237 .detach_instead_of_delete = detach_instead_of_delete,
1238 .done = false,
1240 co = qemu_coroutine_create(test_co_delete_by_drain, &dbdd);
1241 qemu_coroutine_enter(co);
1243 /* Drain the child while the read operation is still pending.
1244 * This should result in the operation finishing and
1245 * test_co_delete_by_drain() resuming. Thus, @bs will be deleted
1246 * and the coroutine will exit while this drain operation is still
1247 * in progress. */
1248 switch (drain_type) {
1249 case BDRV_DRAIN:
1250 bdrv_ref(child_bs);
1251 bdrv_drain(child_bs);
1252 bdrv_unref(child_bs);
1253 break;
1254 case BDRV_SUBTREE_DRAIN:
1255 /* Would have to ref/unref bs here for !detach_instead_of_delete, but
1256 * then the whole test becomes pointless because the graph changes
1257 * don't occur during the drain any more. */
1258 assert(detach_instead_of_delete);
1259 bdrv_subtree_drained_begin(bs);
1260 bdrv_subtree_drained_end(bs);
1261 break;
1262 case BDRV_DRAIN_ALL:
1263 bdrv_drain_all_begin();
1264 bdrv_drain_all_end();
1265 break;
1266 default:
1267 g_assert_not_reached();
1270 while (!dbdd.done) {
1271 aio_poll(qemu_get_aio_context(), true);
1274 if (detach_instead_of_delete) {
1275 /* Here, the reference has not passed over to the coroutine,
1276 * so we have to delete the BB ourselves */
1277 blk_unref(blk);
1281 static void test_delete_by_drain(void)
1283 do_test_delete_by_drain(false, BDRV_DRAIN);
1286 static void test_detach_by_drain_all(void)
1288 do_test_delete_by_drain(true, BDRV_DRAIN_ALL);
1291 static void test_detach_by_drain(void)
1293 do_test_delete_by_drain(true, BDRV_DRAIN);
1296 static void test_detach_by_drain_subtree(void)
1298 do_test_delete_by_drain(true, BDRV_SUBTREE_DRAIN);
1302 struct detach_by_parent_data {
1303 BlockDriverState *parent_b;
1304 BdrvChild *child_b;
1305 BlockDriverState *c;
1306 BdrvChild *child_c;
1307 bool by_parent_cb;
1309 static struct detach_by_parent_data detach_by_parent_data;
1311 static void detach_indirect_bh(void *opaque)
1313 struct detach_by_parent_data *data = opaque;
1315 bdrv_unref_child(data->parent_b, data->child_b);
1317 bdrv_ref(data->c);
1318 data->child_c = bdrv_attach_child(data->parent_b, data->c, "PB-C",
1319 &child_file, &error_abort);
1322 static void detach_by_parent_aio_cb(void *opaque, int ret)
1324 struct detach_by_parent_data *data = &detach_by_parent_data;
1326 g_assert_cmpint(ret, ==, 0);
1327 if (data->by_parent_cb) {
1328 detach_indirect_bh(data);
1332 static void detach_by_driver_cb_drained_begin(BdrvChild *child)
1334 aio_bh_schedule_oneshot(qemu_get_current_aio_context(),
1335 detach_indirect_bh, &detach_by_parent_data);
1336 child_file.drained_begin(child);
1339 static BdrvChildRole detach_by_driver_cb_role;
1342 * Initial graph:
1344 * PA PB
1345 * \ / \
1346 * A B C
1348 * by_parent_cb == true: Test that parent callbacks don't poll
1350 * PA has a pending write request whose callback changes the child nodes of
1351 * PB: It removes B and adds C instead. The subtree of PB is drained, which
1352 * will indirectly drain the write request, too.
1354 * by_parent_cb == false: Test that bdrv_drain_invoke() doesn't poll
1356 * PA's BdrvChildRole has a .drained_begin callback that schedules a BH
1357 * that does the same graph change. If bdrv_drain_invoke() calls it, the
1358 * state is messed up, but if it is only polled in the single
1359 * BDRV_POLL_WHILE() at the end of the drain, this should work fine.
1361 static void test_detach_indirect(bool by_parent_cb)
1363 BlockBackend *blk;
1364 BlockDriverState *parent_a, *parent_b, *a, *b, *c;
1365 BdrvChild *child_a, *child_b;
1366 BlockAIOCB *acb;
1368 QEMUIOVector qiov;
1369 struct iovec iov = {
1370 .iov_base = NULL,
1371 .iov_len = 0,
1373 qemu_iovec_init_external(&qiov, &iov, 1);
1375 if (!by_parent_cb) {
1376 detach_by_driver_cb_role = child_file;
1377 detach_by_driver_cb_role.drained_begin =
1378 detach_by_driver_cb_drained_begin;
1381 /* Create all involved nodes */
1382 parent_a = bdrv_new_open_driver(&bdrv_test, "parent-a", BDRV_O_RDWR,
1383 &error_abort);
1384 parent_b = bdrv_new_open_driver(&bdrv_test, "parent-b", 0,
1385 &error_abort);
1387 a = bdrv_new_open_driver(&bdrv_test, "a", BDRV_O_RDWR, &error_abort);
1388 b = bdrv_new_open_driver(&bdrv_test, "b", BDRV_O_RDWR, &error_abort);
1389 c = bdrv_new_open_driver(&bdrv_test, "c", BDRV_O_RDWR, &error_abort);
1391 /* blk is a BB for parent-a */
1392 blk = blk_new(BLK_PERM_ALL, BLK_PERM_ALL);
1393 blk_insert_bs(blk, parent_a, &error_abort);
1394 bdrv_unref(parent_a);
1396 /* If we want to get bdrv_drain_invoke() to call aio_poll(), the driver
1397 * callback must not return immediately. */
1398 if (!by_parent_cb) {
1399 BDRVTestState *s = parent_a->opaque;
1400 s->sleep_in_drain_begin = true;
1403 /* Set child relationships */
1404 bdrv_ref(b);
1405 bdrv_ref(a);
1406 child_b = bdrv_attach_child(parent_b, b, "PB-B", &child_file, &error_abort);
1407 child_a = bdrv_attach_child(parent_b, a, "PB-A", &child_backing, &error_abort);
1409 bdrv_ref(a);
1410 bdrv_attach_child(parent_a, a, "PA-A",
1411 by_parent_cb ? &child_file : &detach_by_driver_cb_role,
1412 &error_abort);
1414 g_assert_cmpint(parent_a->refcnt, ==, 1);
1415 g_assert_cmpint(parent_b->refcnt, ==, 1);
1416 g_assert_cmpint(a->refcnt, ==, 3);
1417 g_assert_cmpint(b->refcnt, ==, 2);
1418 g_assert_cmpint(c->refcnt, ==, 1);
1420 g_assert(QLIST_FIRST(&parent_b->children) == child_a);
1421 g_assert(QLIST_NEXT(child_a, next) == child_b);
1422 g_assert(QLIST_NEXT(child_b, next) == NULL);
1424 /* Start the evil write request */
1425 detach_by_parent_data = (struct detach_by_parent_data) {
1426 .parent_b = parent_b,
1427 .child_b = child_b,
1428 .c = c,
1429 .by_parent_cb = by_parent_cb,
1431 acb = blk_aio_preadv(blk, 0, &qiov, 0, detach_by_parent_aio_cb, NULL);
1432 g_assert(acb != NULL);
1434 /* Drain and check the expected result */
1435 bdrv_subtree_drained_begin(parent_b);
1437 g_assert(detach_by_parent_data.child_c != NULL);
1439 g_assert_cmpint(parent_a->refcnt, ==, 1);
1440 g_assert_cmpint(parent_b->refcnt, ==, 1);
1441 g_assert_cmpint(a->refcnt, ==, 3);
1442 g_assert_cmpint(b->refcnt, ==, 1);
1443 g_assert_cmpint(c->refcnt, ==, 2);
1445 g_assert(QLIST_FIRST(&parent_b->children) == detach_by_parent_data.child_c);
1446 g_assert(QLIST_NEXT(detach_by_parent_data.child_c, next) == child_a);
1447 g_assert(QLIST_NEXT(child_a, next) == NULL);
1449 g_assert_cmpint(parent_a->quiesce_counter, ==, 1);
1450 g_assert_cmpint(parent_b->quiesce_counter, ==, 1);
1451 g_assert_cmpint(a->quiesce_counter, ==, 1);
1452 g_assert_cmpint(b->quiesce_counter, ==, 0);
1453 g_assert_cmpint(c->quiesce_counter, ==, 1);
1455 bdrv_subtree_drained_end(parent_b);
1457 bdrv_unref(parent_b);
1458 blk_unref(blk);
1460 /* XXX Once bdrv_close() unref's children instead of just detaching them,
1461 * this won't be necessary any more. */
1462 bdrv_unref(a);
1463 bdrv_unref(a);
1464 bdrv_unref(c);
1466 g_assert_cmpint(a->refcnt, ==, 1);
1467 g_assert_cmpint(b->refcnt, ==, 1);
1468 g_assert_cmpint(c->refcnt, ==, 1);
1469 bdrv_unref(a);
1470 bdrv_unref(b);
1471 bdrv_unref(c);
1474 static void test_detach_by_parent_cb(void)
1476 test_detach_indirect(true);
1479 static void test_detach_by_driver_cb(void)
1481 test_detach_indirect(false);
1484 static void test_append_to_drained(void)
1486 BlockBackend *blk;
1487 BlockDriverState *base, *overlay;
1488 BDRVTestState *base_s, *overlay_s;
1490 blk = blk_new(BLK_PERM_ALL, BLK_PERM_ALL);
1491 base = bdrv_new_open_driver(&bdrv_test, "base", BDRV_O_RDWR, &error_abort);
1492 base_s = base->opaque;
1493 blk_insert_bs(blk, base, &error_abort);
1495 overlay = bdrv_new_open_driver(&bdrv_test, "overlay", BDRV_O_RDWR,
1496 &error_abort);
1497 overlay_s = overlay->opaque;
1499 do_drain_begin(BDRV_DRAIN, base);
1500 g_assert_cmpint(base->quiesce_counter, ==, 1);
1501 g_assert_cmpint(base_s->drain_count, ==, 1);
1502 g_assert_cmpint(base->in_flight, ==, 0);
1504 /* Takes ownership of overlay, so we don't have to unref it later */
1505 bdrv_append(overlay, base, &error_abort);
1506 g_assert_cmpint(base->in_flight, ==, 0);
1507 g_assert_cmpint(overlay->in_flight, ==, 0);
1509 g_assert_cmpint(base->quiesce_counter, ==, 1);
1510 g_assert_cmpint(base_s->drain_count, ==, 1);
1511 g_assert_cmpint(overlay->quiesce_counter, ==, 1);
1512 g_assert_cmpint(overlay_s->drain_count, ==, 1);
1514 do_drain_end(BDRV_DRAIN, base);
1516 g_assert_cmpint(base->quiesce_counter, ==, 0);
1517 g_assert_cmpint(base_s->drain_count, ==, 0);
1518 g_assert_cmpint(overlay->quiesce_counter, ==, 0);
1519 g_assert_cmpint(overlay_s->drain_count, ==, 0);
1521 bdrv_unref(base);
1522 blk_unref(blk);
1525 int main(int argc, char **argv)
1527 int ret;
1529 bdrv_init();
1530 qemu_init_main_loop(&error_abort);
1532 g_test_init(&argc, &argv, NULL);
1533 qemu_event_init(&done_event, false);
1535 g_test_add_func("/bdrv-drain/driver-cb/drain_all", test_drv_cb_drain_all);
1536 g_test_add_func("/bdrv-drain/driver-cb/drain", test_drv_cb_drain);
1537 g_test_add_func("/bdrv-drain/driver-cb/drain_subtree",
1538 test_drv_cb_drain_subtree);
1540 g_test_add_func("/bdrv-drain/driver-cb/co/drain_all",
1541 test_drv_cb_co_drain_all);
1542 g_test_add_func("/bdrv-drain/driver-cb/co/drain", test_drv_cb_co_drain);
1543 g_test_add_func("/bdrv-drain/driver-cb/co/drain_subtree",
1544 test_drv_cb_co_drain_subtree);
1547 g_test_add_func("/bdrv-drain/quiesce/drain_all", test_quiesce_drain_all);
1548 g_test_add_func("/bdrv-drain/quiesce/drain", test_quiesce_drain);
1549 g_test_add_func("/bdrv-drain/quiesce/drain_subtree",
1550 test_quiesce_drain_subtree);
1552 g_test_add_func("/bdrv-drain/quiesce/co/drain_all",
1553 test_quiesce_co_drain_all);
1554 g_test_add_func("/bdrv-drain/quiesce/co/drain", test_quiesce_co_drain);
1555 g_test_add_func("/bdrv-drain/quiesce/co/drain_subtree",
1556 test_quiesce_co_drain_subtree);
1558 g_test_add_func("/bdrv-drain/nested", test_nested);
1559 g_test_add_func("/bdrv-drain/multiparent", test_multiparent);
1561 g_test_add_func("/bdrv-drain/graph-change/drain_subtree",
1562 test_graph_change_drain_subtree);
1563 g_test_add_func("/bdrv-drain/graph-change/drain_all",
1564 test_graph_change_drain_all);
1566 g_test_add_func("/bdrv-drain/iothread/drain_all", test_iothread_drain_all);
1567 g_test_add_func("/bdrv-drain/iothread/drain", test_iothread_drain);
1568 g_test_add_func("/bdrv-drain/iothread/drain_subtree",
1569 test_iothread_drain_subtree);
1571 g_test_add_func("/bdrv-drain/blockjob/drain_all", test_blockjob_drain_all);
1572 g_test_add_func("/bdrv-drain/blockjob/drain", test_blockjob_drain);
1573 g_test_add_func("/bdrv-drain/blockjob/drain_subtree",
1574 test_blockjob_drain_subtree);
1576 g_test_add_func("/bdrv-drain/blockjob/error/drain_all",
1577 test_blockjob_error_drain_all);
1578 g_test_add_func("/bdrv-drain/blockjob/error/drain",
1579 test_blockjob_error_drain);
1580 g_test_add_func("/bdrv-drain/blockjob/error/drain_subtree",
1581 test_blockjob_error_drain_subtree);
1583 g_test_add_func("/bdrv-drain/blockjob/iothread/drain_all",
1584 test_blockjob_iothread_drain_all);
1585 g_test_add_func("/bdrv-drain/blockjob/iothread/drain",
1586 test_blockjob_iothread_drain);
1587 g_test_add_func("/bdrv-drain/blockjob/iothread/drain_subtree",
1588 test_blockjob_iothread_drain_subtree);
1590 g_test_add_func("/bdrv-drain/blockjob/iothread/error/drain_all",
1591 test_blockjob_iothread_error_drain_all);
1592 g_test_add_func("/bdrv-drain/blockjob/iothread/error/drain",
1593 test_blockjob_iothread_error_drain);
1594 g_test_add_func("/bdrv-drain/blockjob/iothread/error/drain_subtree",
1595 test_blockjob_iothread_error_drain_subtree);
1597 g_test_add_func("/bdrv-drain/deletion/drain", test_delete_by_drain);
1598 g_test_add_func("/bdrv-drain/detach/drain_all", test_detach_by_drain_all);
1599 g_test_add_func("/bdrv-drain/detach/drain", test_detach_by_drain);
1600 g_test_add_func("/bdrv-drain/detach/drain_subtree", test_detach_by_drain_subtree);
1601 g_test_add_func("/bdrv-drain/detach/parent_cb", test_detach_by_parent_cb);
1602 g_test_add_func("/bdrv-drain/detach/driver_cb", test_detach_by_driver_cb);
1604 g_test_add_func("/bdrv-drain/attach/drain", test_append_to_drained);
1606 ret = g_test_run();
1607 qemu_event_destroy(&done_event);
1608 return ret;