test-bdrv-drain: Graph change through parent callback
[qemu/ar7.git] / tests / test-bdrv-drain.c
blobabb287e59751fd3311a782731f2177d621d7994e
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 } BDRVTestState;
39 static void coroutine_fn bdrv_test_co_drain_begin(BlockDriverState *bs)
41 BDRVTestState *s = bs->opaque;
42 s->drain_count++;
45 static void coroutine_fn bdrv_test_co_drain_end(BlockDriverState *bs)
47 BDRVTestState *s = bs->opaque;
48 s->drain_count--;
51 static void bdrv_test_close(BlockDriverState *bs)
53 BDRVTestState *s = bs->opaque;
54 g_assert_cmpint(s->drain_count, >, 0);
57 static void co_reenter_bh(void *opaque)
59 aio_co_wake(opaque);
62 static int coroutine_fn bdrv_test_co_preadv(BlockDriverState *bs,
63 uint64_t offset, uint64_t bytes,
64 QEMUIOVector *qiov, int flags)
66 BDRVTestState *s = bs->opaque;
68 /* We want this request to stay until the polling loop in drain waits for
69 * it to complete. We need to sleep a while as bdrv_drain_invoke() comes
70 * first and polls its result, too, but it shouldn't accidentally complete
71 * this request yet. */
72 qemu_co_sleep_ns(QEMU_CLOCK_REALTIME, 100000);
74 if (s->bh_indirection_ctx) {
75 aio_bh_schedule_oneshot(s->bh_indirection_ctx, co_reenter_bh,
76 qemu_coroutine_self());
77 qemu_coroutine_yield();
80 return 0;
83 static BlockDriver bdrv_test = {
84 .format_name = "test",
85 .instance_size = sizeof(BDRVTestState),
87 .bdrv_close = bdrv_test_close,
88 .bdrv_co_preadv = bdrv_test_co_preadv,
90 .bdrv_co_drain_begin = bdrv_test_co_drain_begin,
91 .bdrv_co_drain_end = bdrv_test_co_drain_end,
93 .bdrv_child_perm = bdrv_format_default_perms,
96 static void aio_ret_cb(void *opaque, int ret)
98 int *aio_ret = opaque;
99 *aio_ret = ret;
102 typedef struct CallInCoroutineData {
103 void (*entry)(void);
104 bool done;
105 } CallInCoroutineData;
107 static coroutine_fn void call_in_coroutine_entry(void *opaque)
109 CallInCoroutineData *data = opaque;
111 data->entry();
112 data->done = true;
115 static void call_in_coroutine(void (*entry)(void))
117 Coroutine *co;
118 CallInCoroutineData data = {
119 .entry = entry,
120 .done = false,
123 co = qemu_coroutine_create(call_in_coroutine_entry, &data);
124 qemu_coroutine_enter(co);
125 while (!data.done) {
126 aio_poll(qemu_get_aio_context(), true);
130 enum drain_type {
131 BDRV_DRAIN_ALL,
132 BDRV_DRAIN,
133 BDRV_SUBTREE_DRAIN,
134 DRAIN_TYPE_MAX,
137 static void do_drain_begin(enum drain_type drain_type, BlockDriverState *bs)
139 switch (drain_type) {
140 case BDRV_DRAIN_ALL: bdrv_drain_all_begin(); break;
141 case BDRV_DRAIN: bdrv_drained_begin(bs); break;
142 case BDRV_SUBTREE_DRAIN: bdrv_subtree_drained_begin(bs); break;
143 default: g_assert_not_reached();
147 static void do_drain_end(enum drain_type drain_type, BlockDriverState *bs)
149 switch (drain_type) {
150 case BDRV_DRAIN_ALL: bdrv_drain_all_end(); break;
151 case BDRV_DRAIN: bdrv_drained_end(bs); break;
152 case BDRV_SUBTREE_DRAIN: bdrv_subtree_drained_end(bs); break;
153 default: g_assert_not_reached();
157 static void test_drv_cb_common(enum drain_type drain_type, bool recursive)
159 BlockBackend *blk;
160 BlockDriverState *bs, *backing;
161 BDRVTestState *s, *backing_s;
162 BlockAIOCB *acb;
163 int aio_ret;
165 QEMUIOVector qiov;
166 struct iovec iov = {
167 .iov_base = NULL,
168 .iov_len = 0,
170 qemu_iovec_init_external(&qiov, &iov, 1);
172 blk = blk_new(BLK_PERM_ALL, BLK_PERM_ALL);
173 bs = bdrv_new_open_driver(&bdrv_test, "test-node", BDRV_O_RDWR,
174 &error_abort);
175 s = bs->opaque;
176 blk_insert_bs(blk, bs, &error_abort);
178 backing = bdrv_new_open_driver(&bdrv_test, "backing", 0, &error_abort);
179 backing_s = backing->opaque;
180 bdrv_set_backing_hd(bs, backing, &error_abort);
182 /* Simple bdrv_drain_all_begin/end pair, check that CBs are called */
183 g_assert_cmpint(s->drain_count, ==, 0);
184 g_assert_cmpint(backing_s->drain_count, ==, 0);
186 do_drain_begin(drain_type, bs);
188 g_assert_cmpint(s->drain_count, ==, 1);
189 g_assert_cmpint(backing_s->drain_count, ==, !!recursive);
191 do_drain_end(drain_type, bs);
193 g_assert_cmpint(s->drain_count, ==, 0);
194 g_assert_cmpint(backing_s->drain_count, ==, 0);
196 /* Now do the same while a request is pending */
197 aio_ret = -EINPROGRESS;
198 acb = blk_aio_preadv(blk, 0, &qiov, 0, aio_ret_cb, &aio_ret);
199 g_assert(acb != NULL);
200 g_assert_cmpint(aio_ret, ==, -EINPROGRESS);
202 g_assert_cmpint(s->drain_count, ==, 0);
203 g_assert_cmpint(backing_s->drain_count, ==, 0);
205 do_drain_begin(drain_type, bs);
207 g_assert_cmpint(aio_ret, ==, 0);
208 g_assert_cmpint(s->drain_count, ==, 1);
209 g_assert_cmpint(backing_s->drain_count, ==, !!recursive);
211 do_drain_end(drain_type, bs);
213 g_assert_cmpint(s->drain_count, ==, 0);
214 g_assert_cmpint(backing_s->drain_count, ==, 0);
216 bdrv_unref(backing);
217 bdrv_unref(bs);
218 blk_unref(blk);
221 static void test_drv_cb_drain_all(void)
223 test_drv_cb_common(BDRV_DRAIN_ALL, true);
226 static void test_drv_cb_drain(void)
228 test_drv_cb_common(BDRV_DRAIN, false);
231 static void test_drv_cb_drain_subtree(void)
233 test_drv_cb_common(BDRV_SUBTREE_DRAIN, true);
236 static void test_drv_cb_co_drain_all(void)
238 call_in_coroutine(test_drv_cb_drain_all);
241 static void test_drv_cb_co_drain(void)
243 call_in_coroutine(test_drv_cb_drain);
246 static void test_drv_cb_co_drain_subtree(void)
248 call_in_coroutine(test_drv_cb_drain_subtree);
251 static void test_quiesce_common(enum drain_type drain_type, bool recursive)
253 BlockBackend *blk;
254 BlockDriverState *bs, *backing;
256 blk = blk_new(BLK_PERM_ALL, BLK_PERM_ALL);
257 bs = bdrv_new_open_driver(&bdrv_test, "test-node", BDRV_O_RDWR,
258 &error_abort);
259 blk_insert_bs(blk, bs, &error_abort);
261 backing = bdrv_new_open_driver(&bdrv_test, "backing", 0, &error_abort);
262 bdrv_set_backing_hd(bs, backing, &error_abort);
264 g_assert_cmpint(bs->quiesce_counter, ==, 0);
265 g_assert_cmpint(backing->quiesce_counter, ==, 0);
267 do_drain_begin(drain_type, bs);
269 g_assert_cmpint(bs->quiesce_counter, ==, 1);
270 g_assert_cmpint(backing->quiesce_counter, ==, !!recursive);
272 do_drain_end(drain_type, bs);
274 g_assert_cmpint(bs->quiesce_counter, ==, 0);
275 g_assert_cmpint(backing->quiesce_counter, ==, 0);
277 bdrv_unref(backing);
278 bdrv_unref(bs);
279 blk_unref(blk);
282 static void test_quiesce_drain_all(void)
284 test_quiesce_common(BDRV_DRAIN_ALL, true);
287 static void test_quiesce_drain(void)
289 test_quiesce_common(BDRV_DRAIN, false);
292 static void test_quiesce_drain_subtree(void)
294 test_quiesce_common(BDRV_SUBTREE_DRAIN, true);
297 static void test_quiesce_co_drain_all(void)
299 call_in_coroutine(test_quiesce_drain_all);
302 static void test_quiesce_co_drain(void)
304 call_in_coroutine(test_quiesce_drain);
307 static void test_quiesce_co_drain_subtree(void)
309 call_in_coroutine(test_quiesce_drain_subtree);
312 static void test_nested(void)
314 BlockBackend *blk;
315 BlockDriverState *bs, *backing;
316 BDRVTestState *s, *backing_s;
317 enum drain_type outer, inner;
319 blk = blk_new(BLK_PERM_ALL, BLK_PERM_ALL);
320 bs = bdrv_new_open_driver(&bdrv_test, "test-node", BDRV_O_RDWR,
321 &error_abort);
322 s = bs->opaque;
323 blk_insert_bs(blk, bs, &error_abort);
325 backing = bdrv_new_open_driver(&bdrv_test, "backing", 0, &error_abort);
326 backing_s = backing->opaque;
327 bdrv_set_backing_hd(bs, backing, &error_abort);
329 for (outer = 0; outer < DRAIN_TYPE_MAX; outer++) {
330 for (inner = 0; inner < DRAIN_TYPE_MAX; inner++) {
331 int backing_quiesce = (outer != BDRV_DRAIN) +
332 (inner != BDRV_DRAIN);
334 g_assert_cmpint(bs->quiesce_counter, ==, 0);
335 g_assert_cmpint(backing->quiesce_counter, ==, 0);
336 g_assert_cmpint(s->drain_count, ==, 0);
337 g_assert_cmpint(backing_s->drain_count, ==, 0);
339 do_drain_begin(outer, bs);
340 do_drain_begin(inner, bs);
342 g_assert_cmpint(bs->quiesce_counter, ==, 2);
343 g_assert_cmpint(backing->quiesce_counter, ==, backing_quiesce);
344 g_assert_cmpint(s->drain_count, ==, 2);
345 g_assert_cmpint(backing_s->drain_count, ==, backing_quiesce);
347 do_drain_end(inner, bs);
348 do_drain_end(outer, bs);
350 g_assert_cmpint(bs->quiesce_counter, ==, 0);
351 g_assert_cmpint(backing->quiesce_counter, ==, 0);
352 g_assert_cmpint(s->drain_count, ==, 0);
353 g_assert_cmpint(backing_s->drain_count, ==, 0);
357 bdrv_unref(backing);
358 bdrv_unref(bs);
359 blk_unref(blk);
362 static void test_multiparent(void)
364 BlockBackend *blk_a, *blk_b;
365 BlockDriverState *bs_a, *bs_b, *backing;
366 BDRVTestState *a_s, *b_s, *backing_s;
368 blk_a = blk_new(BLK_PERM_ALL, BLK_PERM_ALL);
369 bs_a = bdrv_new_open_driver(&bdrv_test, "test-node-a", BDRV_O_RDWR,
370 &error_abort);
371 a_s = bs_a->opaque;
372 blk_insert_bs(blk_a, bs_a, &error_abort);
374 blk_b = blk_new(BLK_PERM_ALL, BLK_PERM_ALL);
375 bs_b = bdrv_new_open_driver(&bdrv_test, "test-node-b", BDRV_O_RDWR,
376 &error_abort);
377 b_s = bs_b->opaque;
378 blk_insert_bs(blk_b, bs_b, &error_abort);
380 backing = bdrv_new_open_driver(&bdrv_test, "backing", 0, &error_abort);
381 backing_s = backing->opaque;
382 bdrv_set_backing_hd(bs_a, backing, &error_abort);
383 bdrv_set_backing_hd(bs_b, backing, &error_abort);
385 g_assert_cmpint(bs_a->quiesce_counter, ==, 0);
386 g_assert_cmpint(bs_b->quiesce_counter, ==, 0);
387 g_assert_cmpint(backing->quiesce_counter, ==, 0);
388 g_assert_cmpint(a_s->drain_count, ==, 0);
389 g_assert_cmpint(b_s->drain_count, ==, 0);
390 g_assert_cmpint(backing_s->drain_count, ==, 0);
392 do_drain_begin(BDRV_SUBTREE_DRAIN, bs_a);
394 g_assert_cmpint(bs_a->quiesce_counter, ==, 1);
395 g_assert_cmpint(bs_b->quiesce_counter, ==, 1);
396 g_assert_cmpint(backing->quiesce_counter, ==, 1);
397 g_assert_cmpint(a_s->drain_count, ==, 1);
398 g_assert_cmpint(b_s->drain_count, ==, 1);
399 g_assert_cmpint(backing_s->drain_count, ==, 1);
401 do_drain_begin(BDRV_SUBTREE_DRAIN, bs_b);
403 g_assert_cmpint(bs_a->quiesce_counter, ==, 2);
404 g_assert_cmpint(bs_b->quiesce_counter, ==, 2);
405 g_assert_cmpint(backing->quiesce_counter, ==, 2);
406 g_assert_cmpint(a_s->drain_count, ==, 2);
407 g_assert_cmpint(b_s->drain_count, ==, 2);
408 g_assert_cmpint(backing_s->drain_count, ==, 2);
410 do_drain_end(BDRV_SUBTREE_DRAIN, bs_b);
412 g_assert_cmpint(bs_a->quiesce_counter, ==, 1);
413 g_assert_cmpint(bs_b->quiesce_counter, ==, 1);
414 g_assert_cmpint(backing->quiesce_counter, ==, 1);
415 g_assert_cmpint(a_s->drain_count, ==, 1);
416 g_assert_cmpint(b_s->drain_count, ==, 1);
417 g_assert_cmpint(backing_s->drain_count, ==, 1);
419 do_drain_end(BDRV_SUBTREE_DRAIN, bs_a);
421 g_assert_cmpint(bs_a->quiesce_counter, ==, 0);
422 g_assert_cmpint(bs_b->quiesce_counter, ==, 0);
423 g_assert_cmpint(backing->quiesce_counter, ==, 0);
424 g_assert_cmpint(a_s->drain_count, ==, 0);
425 g_assert_cmpint(b_s->drain_count, ==, 0);
426 g_assert_cmpint(backing_s->drain_count, ==, 0);
428 bdrv_unref(backing);
429 bdrv_unref(bs_a);
430 bdrv_unref(bs_b);
431 blk_unref(blk_a);
432 blk_unref(blk_b);
435 static void test_graph_change(void)
437 BlockBackend *blk_a, *blk_b;
438 BlockDriverState *bs_a, *bs_b, *backing;
439 BDRVTestState *a_s, *b_s, *backing_s;
441 blk_a = blk_new(BLK_PERM_ALL, BLK_PERM_ALL);
442 bs_a = bdrv_new_open_driver(&bdrv_test, "test-node-a", BDRV_O_RDWR,
443 &error_abort);
444 a_s = bs_a->opaque;
445 blk_insert_bs(blk_a, bs_a, &error_abort);
447 blk_b = blk_new(BLK_PERM_ALL, BLK_PERM_ALL);
448 bs_b = bdrv_new_open_driver(&bdrv_test, "test-node-b", BDRV_O_RDWR,
449 &error_abort);
450 b_s = bs_b->opaque;
451 blk_insert_bs(blk_b, bs_b, &error_abort);
453 backing = bdrv_new_open_driver(&bdrv_test, "backing", 0, &error_abort);
454 backing_s = backing->opaque;
455 bdrv_set_backing_hd(bs_a, backing, &error_abort);
457 g_assert_cmpint(bs_a->quiesce_counter, ==, 0);
458 g_assert_cmpint(bs_b->quiesce_counter, ==, 0);
459 g_assert_cmpint(backing->quiesce_counter, ==, 0);
460 g_assert_cmpint(a_s->drain_count, ==, 0);
461 g_assert_cmpint(b_s->drain_count, ==, 0);
462 g_assert_cmpint(backing_s->drain_count, ==, 0);
464 do_drain_begin(BDRV_SUBTREE_DRAIN, bs_a);
465 do_drain_begin(BDRV_SUBTREE_DRAIN, bs_a);
466 do_drain_begin(BDRV_SUBTREE_DRAIN, bs_a);
467 do_drain_begin(BDRV_SUBTREE_DRAIN, bs_b);
468 do_drain_begin(BDRV_SUBTREE_DRAIN, bs_b);
470 bdrv_set_backing_hd(bs_b, backing, &error_abort);
471 g_assert_cmpint(bs_a->quiesce_counter, ==, 5);
472 g_assert_cmpint(bs_b->quiesce_counter, ==, 5);
473 g_assert_cmpint(backing->quiesce_counter, ==, 5);
474 g_assert_cmpint(a_s->drain_count, ==, 5);
475 g_assert_cmpint(b_s->drain_count, ==, 5);
476 g_assert_cmpint(backing_s->drain_count, ==, 5);
478 bdrv_set_backing_hd(bs_b, NULL, &error_abort);
479 g_assert_cmpint(bs_a->quiesce_counter, ==, 3);
480 g_assert_cmpint(bs_b->quiesce_counter, ==, 2);
481 g_assert_cmpint(backing->quiesce_counter, ==, 3);
482 g_assert_cmpint(a_s->drain_count, ==, 3);
483 g_assert_cmpint(b_s->drain_count, ==, 2);
484 g_assert_cmpint(backing_s->drain_count, ==, 3);
486 bdrv_set_backing_hd(bs_b, backing, &error_abort);
487 g_assert_cmpint(bs_a->quiesce_counter, ==, 5);
488 g_assert_cmpint(bs_b->quiesce_counter, ==, 5);
489 g_assert_cmpint(backing->quiesce_counter, ==, 5);
490 g_assert_cmpint(a_s->drain_count, ==, 5);
491 g_assert_cmpint(b_s->drain_count, ==, 5);
492 g_assert_cmpint(backing_s->drain_count, ==, 5);
494 do_drain_end(BDRV_SUBTREE_DRAIN, bs_b);
495 do_drain_end(BDRV_SUBTREE_DRAIN, bs_b);
496 do_drain_end(BDRV_SUBTREE_DRAIN, bs_a);
497 do_drain_end(BDRV_SUBTREE_DRAIN, bs_a);
498 do_drain_end(BDRV_SUBTREE_DRAIN, bs_a);
500 g_assert_cmpint(bs_a->quiesce_counter, ==, 0);
501 g_assert_cmpint(bs_b->quiesce_counter, ==, 0);
502 g_assert_cmpint(backing->quiesce_counter, ==, 0);
503 g_assert_cmpint(a_s->drain_count, ==, 0);
504 g_assert_cmpint(b_s->drain_count, ==, 0);
505 g_assert_cmpint(backing_s->drain_count, ==, 0);
507 bdrv_unref(backing);
508 bdrv_unref(bs_a);
509 bdrv_unref(bs_b);
510 blk_unref(blk_a);
511 blk_unref(blk_b);
514 struct test_iothread_data {
515 BlockDriverState *bs;
516 enum drain_type drain_type;
517 int *aio_ret;
520 static void test_iothread_drain_entry(void *opaque)
522 struct test_iothread_data *data = opaque;
524 aio_context_acquire(bdrv_get_aio_context(data->bs));
525 do_drain_begin(data->drain_type, data->bs);
526 g_assert_cmpint(*data->aio_ret, ==, 0);
527 do_drain_end(data->drain_type, data->bs);
528 aio_context_release(bdrv_get_aio_context(data->bs));
530 qemu_event_set(&done_event);
533 static void test_iothread_aio_cb(void *opaque, int ret)
535 int *aio_ret = opaque;
536 *aio_ret = ret;
537 qemu_event_set(&done_event);
541 * Starts an AIO request on a BDS that runs in the AioContext of iothread 1.
542 * The request involves a BH on iothread 2 before it can complete.
544 * @drain_thread = 0 means that do_drain_begin/end are called from the main
545 * thread, @drain_thread = 1 means that they are called from iothread 1. Drain
546 * for this BDS cannot be called from iothread 2 because only the main thread
547 * may do cross-AioContext polling.
549 static void test_iothread_common(enum drain_type drain_type, int drain_thread)
551 BlockBackend *blk;
552 BlockDriverState *bs;
553 BDRVTestState *s;
554 BlockAIOCB *acb;
555 int aio_ret;
556 struct test_iothread_data data;
558 IOThread *a = iothread_new();
559 IOThread *b = iothread_new();
560 AioContext *ctx_a = iothread_get_aio_context(a);
561 AioContext *ctx_b = iothread_get_aio_context(b);
563 QEMUIOVector qiov;
564 struct iovec iov = {
565 .iov_base = NULL,
566 .iov_len = 0,
568 qemu_iovec_init_external(&qiov, &iov, 1);
570 /* bdrv_drain_all() may only be called from the main loop thread */
571 if (drain_type == BDRV_DRAIN_ALL && drain_thread != 0) {
572 goto out;
575 blk = blk_new(BLK_PERM_ALL, BLK_PERM_ALL);
576 bs = bdrv_new_open_driver(&bdrv_test, "test-node", BDRV_O_RDWR,
577 &error_abort);
578 s = bs->opaque;
579 blk_insert_bs(blk, bs, &error_abort);
581 blk_set_aio_context(blk, ctx_a);
582 aio_context_acquire(ctx_a);
584 s->bh_indirection_ctx = ctx_b;
586 aio_ret = -EINPROGRESS;
587 if (drain_thread == 0) {
588 acb = blk_aio_preadv(blk, 0, &qiov, 0, test_iothread_aio_cb, &aio_ret);
589 } else {
590 acb = blk_aio_preadv(blk, 0, &qiov, 0, aio_ret_cb, &aio_ret);
592 g_assert(acb != NULL);
593 g_assert_cmpint(aio_ret, ==, -EINPROGRESS);
595 aio_context_release(ctx_a);
597 data = (struct test_iothread_data) {
598 .bs = bs,
599 .drain_type = drain_type,
600 .aio_ret = &aio_ret,
603 switch (drain_thread) {
604 case 0:
605 if (drain_type != BDRV_DRAIN_ALL) {
606 aio_context_acquire(ctx_a);
609 /* The request is running on the IOThread a. Draining its block device
610 * will make sure that it has completed as far as the BDS is concerned,
611 * but the drain in this thread can continue immediately after
612 * bdrv_dec_in_flight() and aio_ret might be assigned only slightly
613 * later. */
614 qemu_event_reset(&done_event);
615 do_drain_begin(drain_type, bs);
616 g_assert_cmpint(bs->in_flight, ==, 0);
618 if (drain_type != BDRV_DRAIN_ALL) {
619 aio_context_release(ctx_a);
621 qemu_event_wait(&done_event);
622 if (drain_type != BDRV_DRAIN_ALL) {
623 aio_context_acquire(ctx_a);
626 g_assert_cmpint(aio_ret, ==, 0);
627 do_drain_end(drain_type, bs);
629 if (drain_type != BDRV_DRAIN_ALL) {
630 aio_context_release(ctx_a);
632 break;
633 case 1:
634 qemu_event_reset(&done_event);
635 aio_bh_schedule_oneshot(ctx_a, test_iothread_drain_entry, &data);
636 qemu_event_wait(&done_event);
637 break;
638 default:
639 g_assert_not_reached();
642 aio_context_acquire(ctx_a);
643 blk_set_aio_context(blk, qemu_get_aio_context());
644 aio_context_release(ctx_a);
646 bdrv_unref(bs);
647 blk_unref(blk);
649 out:
650 iothread_join(a);
651 iothread_join(b);
654 static void test_iothread_drain_all(void)
656 test_iothread_common(BDRV_DRAIN_ALL, 0);
657 test_iothread_common(BDRV_DRAIN_ALL, 1);
660 static void test_iothread_drain(void)
662 test_iothread_common(BDRV_DRAIN, 0);
663 test_iothread_common(BDRV_DRAIN, 1);
666 static void test_iothread_drain_subtree(void)
668 test_iothread_common(BDRV_SUBTREE_DRAIN, 0);
669 test_iothread_common(BDRV_SUBTREE_DRAIN, 1);
673 typedef struct TestBlockJob {
674 BlockJob common;
675 bool should_complete;
676 } TestBlockJob;
678 static void test_job_completed(Job *job, void *opaque)
680 job_completed(job, 0, NULL);
683 static void coroutine_fn test_job_start(void *opaque)
685 TestBlockJob *s = opaque;
687 job_transition_to_ready(&s->common.job);
688 while (!s->should_complete) {
689 /* Avoid block_job_sleep_ns() because it marks the job as !busy. We
690 * want to emulate some actual activity (probably some I/O) here so
691 * that drain has to wait for this acitivity to stop. */
692 qemu_co_sleep_ns(QEMU_CLOCK_REALTIME, 100000);
693 job_pause_point(&s->common.job);
696 job_defer_to_main_loop(&s->common.job, test_job_completed, NULL);
699 static void test_job_complete(Job *job, Error **errp)
701 TestBlockJob *s = container_of(job, TestBlockJob, common.job);
702 s->should_complete = true;
705 BlockJobDriver test_job_driver = {
706 .job_driver = {
707 .instance_size = sizeof(TestBlockJob),
708 .free = block_job_free,
709 .user_resume = block_job_user_resume,
710 .drain = block_job_drain,
711 .start = test_job_start,
712 .complete = test_job_complete,
716 static void test_blockjob_common(enum drain_type drain_type)
718 BlockBackend *blk_src, *blk_target;
719 BlockDriverState *src, *target;
720 BlockJob *job;
721 int ret;
723 src = bdrv_new_open_driver(&bdrv_test, "source", BDRV_O_RDWR,
724 &error_abort);
725 blk_src = blk_new(BLK_PERM_ALL, BLK_PERM_ALL);
726 blk_insert_bs(blk_src, src, &error_abort);
728 target = bdrv_new_open_driver(&bdrv_test, "target", BDRV_O_RDWR,
729 &error_abort);
730 blk_target = blk_new(BLK_PERM_ALL, BLK_PERM_ALL);
731 blk_insert_bs(blk_target, target, &error_abort);
733 job = block_job_create("job0", &test_job_driver, NULL, src, 0, BLK_PERM_ALL,
734 0, 0, NULL, NULL, &error_abort);
735 block_job_add_bdrv(job, "target", target, 0, BLK_PERM_ALL, &error_abort);
736 job_start(&job->job);
738 g_assert_cmpint(job->job.pause_count, ==, 0);
739 g_assert_false(job->job.paused);
740 g_assert_true(job->job.busy); /* We're in job_sleep_ns() */
742 do_drain_begin(drain_type, src);
744 if (drain_type == BDRV_DRAIN_ALL) {
745 /* bdrv_drain_all() drains both src and target */
746 g_assert_cmpint(job->job.pause_count, ==, 2);
747 } else {
748 g_assert_cmpint(job->job.pause_count, ==, 1);
750 g_assert_true(job->job.paused);
751 g_assert_false(job->job.busy); /* The job is paused */
753 do_drain_end(drain_type, src);
755 g_assert_cmpint(job->job.pause_count, ==, 0);
756 g_assert_false(job->job.paused);
757 g_assert_true(job->job.busy); /* We're in qemu_co_sleep_ns() */
759 do_drain_begin(drain_type, target);
761 if (drain_type == BDRV_DRAIN_ALL) {
762 /* bdrv_drain_all() drains both src and target */
763 g_assert_cmpint(job->job.pause_count, ==, 2);
764 } else {
765 g_assert_cmpint(job->job.pause_count, ==, 1);
767 g_assert_true(job->job.paused);
768 g_assert_false(job->job.busy); /* The job is paused */
770 do_drain_end(drain_type, target);
772 g_assert_cmpint(job->job.pause_count, ==, 0);
773 g_assert_false(job->job.paused);
774 g_assert_true(job->job.busy); /* We're in job_sleep_ns() */
776 ret = job_complete_sync(&job->job, &error_abort);
777 g_assert_cmpint(ret, ==, 0);
779 blk_unref(blk_src);
780 blk_unref(blk_target);
781 bdrv_unref(src);
782 bdrv_unref(target);
785 static void test_blockjob_drain_all(void)
787 test_blockjob_common(BDRV_DRAIN_ALL);
790 static void test_blockjob_drain(void)
792 test_blockjob_common(BDRV_DRAIN);
795 static void test_blockjob_drain_subtree(void)
797 test_blockjob_common(BDRV_SUBTREE_DRAIN);
801 typedef struct BDRVTestTopState {
802 BdrvChild *wait_child;
803 } BDRVTestTopState;
805 static void bdrv_test_top_close(BlockDriverState *bs)
807 BdrvChild *c, *next_c;
808 QLIST_FOREACH_SAFE(c, &bs->children, next, next_c) {
809 bdrv_unref_child(bs, c);
813 static int coroutine_fn bdrv_test_top_co_preadv(BlockDriverState *bs,
814 uint64_t offset, uint64_t bytes,
815 QEMUIOVector *qiov, int flags)
817 BDRVTestTopState *tts = bs->opaque;
818 return bdrv_co_preadv(tts->wait_child, offset, bytes, qiov, flags);
821 static BlockDriver bdrv_test_top_driver = {
822 .format_name = "test_top_driver",
823 .instance_size = sizeof(BDRVTestTopState),
825 .bdrv_close = bdrv_test_top_close,
826 .bdrv_co_preadv = bdrv_test_top_co_preadv,
828 .bdrv_child_perm = bdrv_format_default_perms,
831 typedef struct TestCoDeleteByDrainData {
832 BlockBackend *blk;
833 bool detach_instead_of_delete;
834 bool done;
835 } TestCoDeleteByDrainData;
837 static void coroutine_fn test_co_delete_by_drain(void *opaque)
839 TestCoDeleteByDrainData *dbdd = opaque;
840 BlockBackend *blk = dbdd->blk;
841 BlockDriverState *bs = blk_bs(blk);
842 BDRVTestTopState *tts = bs->opaque;
843 void *buffer = g_malloc(65536);
844 QEMUIOVector qiov;
845 struct iovec iov = {
846 .iov_base = buffer,
847 .iov_len = 65536,
850 qemu_iovec_init_external(&qiov, &iov, 1);
852 /* Pretend some internal write operation from parent to child.
853 * Important: We have to read from the child, not from the parent!
854 * Draining works by first propagating it all up the tree to the
855 * root and then waiting for drainage from root to the leaves
856 * (protocol nodes). If we have a request waiting on the root,
857 * everything will be drained before we go back down the tree, but
858 * we do not want that. We want to be in the middle of draining
859 * when this following requests returns. */
860 bdrv_co_preadv(tts->wait_child, 0, 65536, &qiov, 0);
862 g_assert_cmpint(bs->refcnt, ==, 1);
864 if (!dbdd->detach_instead_of_delete) {
865 blk_unref(blk);
866 } else {
867 BdrvChild *c, *next_c;
868 QLIST_FOREACH_SAFE(c, &bs->children, next, next_c) {
869 bdrv_unref_child(bs, c);
873 dbdd->done = true;
877 * Test what happens when some BDS has some children, you drain one of
878 * them and this results in the BDS being deleted.
880 * If @detach_instead_of_delete is set, the BDS is not going to be
881 * deleted but will only detach all of its children.
883 static void do_test_delete_by_drain(bool detach_instead_of_delete,
884 enum drain_type drain_type)
886 BlockBackend *blk;
887 BlockDriverState *bs, *child_bs, *null_bs;
888 BDRVTestTopState *tts;
889 TestCoDeleteByDrainData dbdd;
890 Coroutine *co;
892 bs = bdrv_new_open_driver(&bdrv_test_top_driver, "top", BDRV_O_RDWR,
893 &error_abort);
894 bs->total_sectors = 65536 >> BDRV_SECTOR_BITS;
895 tts = bs->opaque;
897 null_bs = bdrv_open("null-co://", NULL, NULL, BDRV_O_RDWR | BDRV_O_PROTOCOL,
898 &error_abort);
899 bdrv_attach_child(bs, null_bs, "null-child", &child_file, &error_abort);
901 /* This child will be the one to pass to requests through to, and
902 * it will stall until a drain occurs */
903 child_bs = bdrv_new_open_driver(&bdrv_test, "child", BDRV_O_RDWR,
904 &error_abort);
905 child_bs->total_sectors = 65536 >> BDRV_SECTOR_BITS;
906 /* Takes our reference to child_bs */
907 tts->wait_child = bdrv_attach_child(bs, child_bs, "wait-child", &child_file,
908 &error_abort);
910 /* This child is just there to be deleted
911 * (for detach_instead_of_delete == true) */
912 null_bs = bdrv_open("null-co://", NULL, NULL, BDRV_O_RDWR | BDRV_O_PROTOCOL,
913 &error_abort);
914 bdrv_attach_child(bs, null_bs, "null-child", &child_file, &error_abort);
916 blk = blk_new(BLK_PERM_ALL, BLK_PERM_ALL);
917 blk_insert_bs(blk, bs, &error_abort);
919 /* Referenced by blk now */
920 bdrv_unref(bs);
922 g_assert_cmpint(bs->refcnt, ==, 1);
923 g_assert_cmpint(child_bs->refcnt, ==, 1);
924 g_assert_cmpint(null_bs->refcnt, ==, 1);
927 dbdd = (TestCoDeleteByDrainData){
928 .blk = blk,
929 .detach_instead_of_delete = detach_instead_of_delete,
930 .done = false,
932 co = qemu_coroutine_create(test_co_delete_by_drain, &dbdd);
933 qemu_coroutine_enter(co);
935 /* Drain the child while the read operation is still pending.
936 * This should result in the operation finishing and
937 * test_co_delete_by_drain() resuming. Thus, @bs will be deleted
938 * and the coroutine will exit while this drain operation is still
939 * in progress. */
940 switch (drain_type) {
941 case BDRV_DRAIN:
942 bdrv_ref(child_bs);
943 bdrv_drain(child_bs);
944 bdrv_unref(child_bs);
945 break;
946 case BDRV_SUBTREE_DRAIN:
947 /* Would have to ref/unref bs here for !detach_instead_of_delete, but
948 * then the whole test becomes pointless because the graph changes
949 * don't occur during the drain any more. */
950 assert(detach_instead_of_delete);
951 bdrv_subtree_drained_begin(bs);
952 bdrv_subtree_drained_end(bs);
953 break;
954 default:
955 g_assert_not_reached();
958 while (!dbdd.done) {
959 aio_poll(qemu_get_aio_context(), true);
962 if (detach_instead_of_delete) {
963 /* Here, the reference has not passed over to the coroutine,
964 * so we have to delete the BB ourselves */
965 blk_unref(blk);
969 static void test_delete_by_drain(void)
971 do_test_delete_by_drain(false, BDRV_DRAIN);
974 static void test_detach_by_drain(void)
976 do_test_delete_by_drain(true, BDRV_DRAIN);
979 static void test_detach_by_drain_subtree(void)
981 do_test_delete_by_drain(true, BDRV_SUBTREE_DRAIN);
985 struct detach_by_parent_data {
986 BlockDriverState *parent_b;
987 BdrvChild *child_b;
988 BlockDriverState *c;
989 BdrvChild *child_c;
992 static void detach_by_parent_aio_cb(void *opaque, int ret)
994 struct detach_by_parent_data *data = opaque;
996 g_assert_cmpint(ret, ==, 0);
997 bdrv_unref_child(data->parent_b, data->child_b);
999 bdrv_ref(data->c);
1000 data->child_c = bdrv_attach_child(data->parent_b, data->c, "PB-C",
1001 &child_file, &error_abort);
1005 * Initial graph:
1007 * PA PB
1008 * \ / \
1009 * A B C
1011 * PA has a pending write request whose callback changes the child nodes of PB:
1012 * It removes B and adds C instead. The subtree of PB is drained, which will
1013 * indirectly drain the write request, too.
1015 static void test_detach_by_parent_cb(void)
1017 BlockBackend *blk;
1018 BlockDriverState *parent_a, *parent_b, *a, *b, *c;
1019 BdrvChild *child_a, *child_b;
1020 BlockAIOCB *acb;
1021 struct detach_by_parent_data data;
1023 QEMUIOVector qiov;
1024 struct iovec iov = {
1025 .iov_base = NULL,
1026 .iov_len = 0,
1028 qemu_iovec_init_external(&qiov, &iov, 1);
1030 /* Create all involved nodes */
1031 parent_a = bdrv_new_open_driver(&bdrv_test, "parent-a", BDRV_O_RDWR,
1032 &error_abort);
1033 parent_b = bdrv_new_open_driver(&bdrv_test, "parent-b", 0,
1034 &error_abort);
1036 a = bdrv_new_open_driver(&bdrv_test, "a", BDRV_O_RDWR, &error_abort);
1037 b = bdrv_new_open_driver(&bdrv_test, "b", BDRV_O_RDWR, &error_abort);
1038 c = bdrv_new_open_driver(&bdrv_test, "c", BDRV_O_RDWR, &error_abort);
1040 /* blk is a BB for parent-a */
1041 blk = blk_new(BLK_PERM_ALL, BLK_PERM_ALL);
1042 blk_insert_bs(blk, parent_a, &error_abort);
1043 bdrv_unref(parent_a);
1045 /* Set child relationships */
1046 bdrv_ref(b);
1047 bdrv_ref(a);
1048 child_b = bdrv_attach_child(parent_b, b, "PB-B", &child_file, &error_abort);
1049 child_a = bdrv_attach_child(parent_b, a, "PB-A", &child_backing, &error_abort);
1051 bdrv_ref(a);
1052 bdrv_attach_child(parent_a, a, "PA-A", &child_file, &error_abort);
1054 g_assert_cmpint(parent_a->refcnt, ==, 1);
1055 g_assert_cmpint(parent_b->refcnt, ==, 1);
1056 g_assert_cmpint(a->refcnt, ==, 3);
1057 g_assert_cmpint(b->refcnt, ==, 2);
1058 g_assert_cmpint(c->refcnt, ==, 1);
1060 g_assert(QLIST_FIRST(&parent_b->children) == child_a);
1061 g_assert(QLIST_NEXT(child_a, next) == child_b);
1062 g_assert(QLIST_NEXT(child_b, next) == NULL);
1064 /* Start the evil write request */
1065 data = (struct detach_by_parent_data) {
1066 .parent_b = parent_b,
1067 .child_b = child_b,
1068 .c = c,
1070 acb = blk_aio_preadv(blk, 0, &qiov, 0, detach_by_parent_aio_cb, &data);
1071 g_assert(acb != NULL);
1073 /* Drain and check the expected result */
1074 bdrv_subtree_drained_begin(parent_b);
1076 g_assert(data.child_c != NULL);
1078 g_assert_cmpint(parent_a->refcnt, ==, 1);
1079 g_assert_cmpint(parent_b->refcnt, ==, 1);
1080 g_assert_cmpint(a->refcnt, ==, 3);
1081 g_assert_cmpint(b->refcnt, ==, 1);
1082 g_assert_cmpint(c->refcnt, ==, 2);
1084 g_assert(QLIST_FIRST(&parent_b->children) == data.child_c);
1085 g_assert(QLIST_NEXT(data.child_c, next) == child_a);
1086 g_assert(QLIST_NEXT(child_a, next) == NULL);
1088 g_assert_cmpint(parent_a->quiesce_counter, ==, 1);
1089 g_assert_cmpint(parent_b->quiesce_counter, ==, 1);
1090 g_assert_cmpint(a->quiesce_counter, ==, 1);
1091 g_assert_cmpint(b->quiesce_counter, ==, 0);
1092 g_assert_cmpint(c->quiesce_counter, ==, 1);
1094 bdrv_subtree_drained_end(parent_b);
1096 bdrv_unref(parent_b);
1097 blk_unref(blk);
1099 /* XXX Once bdrv_close() unref's children instead of just detaching them,
1100 * this won't be necessary any more. */
1101 bdrv_unref(a);
1102 bdrv_unref(a);
1103 bdrv_unref(c);
1105 g_assert_cmpint(a->refcnt, ==, 1);
1106 g_assert_cmpint(b->refcnt, ==, 1);
1107 g_assert_cmpint(c->refcnt, ==, 1);
1108 bdrv_unref(a);
1109 bdrv_unref(b);
1110 bdrv_unref(c);
1114 int main(int argc, char **argv)
1116 int ret;
1118 bdrv_init();
1119 qemu_init_main_loop(&error_abort);
1121 g_test_init(&argc, &argv, NULL);
1122 qemu_event_init(&done_event, false);
1124 g_test_add_func("/bdrv-drain/driver-cb/drain_all", test_drv_cb_drain_all);
1125 g_test_add_func("/bdrv-drain/driver-cb/drain", test_drv_cb_drain);
1126 g_test_add_func("/bdrv-drain/driver-cb/drain_subtree",
1127 test_drv_cb_drain_subtree);
1129 g_test_add_func("/bdrv-drain/driver-cb/co/drain_all",
1130 test_drv_cb_co_drain_all);
1131 g_test_add_func("/bdrv-drain/driver-cb/co/drain", test_drv_cb_co_drain);
1132 g_test_add_func("/bdrv-drain/driver-cb/co/drain_subtree",
1133 test_drv_cb_co_drain_subtree);
1136 g_test_add_func("/bdrv-drain/quiesce/drain_all", test_quiesce_drain_all);
1137 g_test_add_func("/bdrv-drain/quiesce/drain", test_quiesce_drain);
1138 g_test_add_func("/bdrv-drain/quiesce/drain_subtree",
1139 test_quiesce_drain_subtree);
1141 g_test_add_func("/bdrv-drain/quiesce/co/drain_all",
1142 test_quiesce_co_drain_all);
1143 g_test_add_func("/bdrv-drain/quiesce/co/drain", test_quiesce_co_drain);
1144 g_test_add_func("/bdrv-drain/quiesce/co/drain_subtree",
1145 test_quiesce_co_drain_subtree);
1147 g_test_add_func("/bdrv-drain/nested", test_nested);
1148 g_test_add_func("/bdrv-drain/multiparent", test_multiparent);
1149 g_test_add_func("/bdrv-drain/graph-change", test_graph_change);
1151 g_test_add_func("/bdrv-drain/iothread/drain_all", test_iothread_drain_all);
1152 g_test_add_func("/bdrv-drain/iothread/drain", test_iothread_drain);
1153 g_test_add_func("/bdrv-drain/iothread/drain_subtree",
1154 test_iothread_drain_subtree);
1156 g_test_add_func("/bdrv-drain/blockjob/drain_all", test_blockjob_drain_all);
1157 g_test_add_func("/bdrv-drain/blockjob/drain", test_blockjob_drain);
1158 g_test_add_func("/bdrv-drain/blockjob/drain_subtree",
1159 test_blockjob_drain_subtree);
1161 g_test_add_func("/bdrv-drain/deletion/drain", test_delete_by_drain);
1162 g_test_add_func("/bdrv-drain/detach/drain", test_detach_by_drain);
1163 g_test_add_func("/bdrv-drain/detach/drain_subtree", test_detach_by_drain_subtree);
1164 g_test_add_func("/bdrv-drain/detach/parent_cb", test_detach_by_parent_cb);
1166 ret = g_test_run();
1167 qemu_event_destroy(&done_event);
1168 return ret;