Include hw/boards.h a bit less
[qemu/ar7.git] / tests / test-bdrv-drain.c
blob481b7508df612256f89b34c71bbb06a72b28af18
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 "qemu/main-loop.h"
31 #include "iothread.h"
33 static QemuEvent done_event;
35 typedef struct BDRVTestState {
36 int drain_count;
37 AioContext *bh_indirection_ctx;
38 bool sleep_in_drain_begin;
39 } BDRVTestState;
41 static void coroutine_fn bdrv_test_co_drain_begin(BlockDriverState *bs)
43 BDRVTestState *s = bs->opaque;
44 s->drain_count++;
45 if (s->sleep_in_drain_begin) {
46 qemu_co_sleep_ns(QEMU_CLOCK_REALTIME, 100000);
50 static void coroutine_fn bdrv_test_co_drain_end(BlockDriverState *bs)
52 BDRVTestState *s = bs->opaque;
53 s->drain_count--;
56 static void bdrv_test_close(BlockDriverState *bs)
58 BDRVTestState *s = bs->opaque;
59 g_assert_cmpint(s->drain_count, >, 0);
62 static void co_reenter_bh(void *opaque)
64 aio_co_wake(opaque);
67 static int coroutine_fn bdrv_test_co_preadv(BlockDriverState *bs,
68 uint64_t offset, uint64_t bytes,
69 QEMUIOVector *qiov, int flags)
71 BDRVTestState *s = bs->opaque;
73 /* We want this request to stay until the polling loop in drain waits for
74 * it to complete. We need to sleep a while as bdrv_drain_invoke() comes
75 * first and polls its result, too, but it shouldn't accidentally complete
76 * this request yet. */
77 qemu_co_sleep_ns(QEMU_CLOCK_REALTIME, 100000);
79 if (s->bh_indirection_ctx) {
80 aio_bh_schedule_oneshot(s->bh_indirection_ctx, co_reenter_bh,
81 qemu_coroutine_self());
82 qemu_coroutine_yield();
85 return 0;
88 static void bdrv_test_child_perm(BlockDriverState *bs, BdrvChild *c,
89 const BdrvChildRole *role,
90 BlockReopenQueue *reopen_queue,
91 uint64_t perm, uint64_t shared,
92 uint64_t *nperm, uint64_t *nshared)
94 /* bdrv_format_default_perms() accepts only these two, so disguise
95 * detach_by_driver_cb_role as one of them. */
96 if (role != &child_file && role != &child_backing) {
97 role = &child_file;
100 bdrv_format_default_perms(bs, c, role, reopen_queue, perm, shared,
101 nperm, nshared);
104 static BlockDriver bdrv_test = {
105 .format_name = "test",
106 .instance_size = sizeof(BDRVTestState),
108 .bdrv_close = bdrv_test_close,
109 .bdrv_co_preadv = bdrv_test_co_preadv,
111 .bdrv_co_drain_begin = bdrv_test_co_drain_begin,
112 .bdrv_co_drain_end = bdrv_test_co_drain_end,
114 .bdrv_child_perm = bdrv_test_child_perm,
117 static void aio_ret_cb(void *opaque, int ret)
119 int *aio_ret = opaque;
120 *aio_ret = ret;
123 typedef struct CallInCoroutineData {
124 void (*entry)(void);
125 bool done;
126 } CallInCoroutineData;
128 static coroutine_fn void call_in_coroutine_entry(void *opaque)
130 CallInCoroutineData *data = opaque;
132 data->entry();
133 data->done = true;
136 static void call_in_coroutine(void (*entry)(void))
138 Coroutine *co;
139 CallInCoroutineData data = {
140 .entry = entry,
141 .done = false,
144 co = qemu_coroutine_create(call_in_coroutine_entry, &data);
145 qemu_coroutine_enter(co);
146 while (!data.done) {
147 aio_poll(qemu_get_aio_context(), true);
151 enum drain_type {
152 BDRV_DRAIN_ALL,
153 BDRV_DRAIN,
154 BDRV_SUBTREE_DRAIN,
155 DRAIN_TYPE_MAX,
158 static void do_drain_begin(enum drain_type drain_type, BlockDriverState *bs)
160 switch (drain_type) {
161 case BDRV_DRAIN_ALL: bdrv_drain_all_begin(); break;
162 case BDRV_DRAIN: bdrv_drained_begin(bs); break;
163 case BDRV_SUBTREE_DRAIN: bdrv_subtree_drained_begin(bs); break;
164 default: g_assert_not_reached();
168 static void do_drain_end(enum drain_type drain_type, BlockDriverState *bs)
170 switch (drain_type) {
171 case BDRV_DRAIN_ALL: bdrv_drain_all_end(); break;
172 case BDRV_DRAIN: bdrv_drained_end(bs); break;
173 case BDRV_SUBTREE_DRAIN: bdrv_subtree_drained_end(bs); break;
174 default: g_assert_not_reached();
178 static void do_drain_begin_unlocked(enum drain_type drain_type, BlockDriverState *bs)
180 if (drain_type != BDRV_DRAIN_ALL) {
181 aio_context_acquire(bdrv_get_aio_context(bs));
183 do_drain_begin(drain_type, bs);
184 if (drain_type != BDRV_DRAIN_ALL) {
185 aio_context_release(bdrv_get_aio_context(bs));
189 static void do_drain_end_unlocked(enum drain_type drain_type, BlockDriverState *bs)
191 if (drain_type != BDRV_DRAIN_ALL) {
192 aio_context_acquire(bdrv_get_aio_context(bs));
194 do_drain_end(drain_type, bs);
195 if (drain_type != BDRV_DRAIN_ALL) {
196 aio_context_release(bdrv_get_aio_context(bs));
200 static void test_drv_cb_common(enum drain_type drain_type, bool recursive)
202 BlockBackend *blk;
203 BlockDriverState *bs, *backing;
204 BDRVTestState *s, *backing_s;
205 BlockAIOCB *acb;
206 int aio_ret;
208 QEMUIOVector qiov = QEMU_IOVEC_INIT_BUF(qiov, NULL, 0);
210 blk = blk_new(qemu_get_aio_context(), BLK_PERM_ALL, BLK_PERM_ALL);
211 bs = bdrv_new_open_driver(&bdrv_test, "test-node", BDRV_O_RDWR,
212 &error_abort);
213 s = bs->opaque;
214 blk_insert_bs(blk, bs, &error_abort);
216 backing = bdrv_new_open_driver(&bdrv_test, "backing", 0, &error_abort);
217 backing_s = backing->opaque;
218 bdrv_set_backing_hd(bs, backing, &error_abort);
220 /* Simple bdrv_drain_all_begin/end pair, check that CBs are called */
221 g_assert_cmpint(s->drain_count, ==, 0);
222 g_assert_cmpint(backing_s->drain_count, ==, 0);
224 do_drain_begin(drain_type, bs);
226 g_assert_cmpint(s->drain_count, ==, 1);
227 g_assert_cmpint(backing_s->drain_count, ==, !!recursive);
229 do_drain_end(drain_type, bs);
231 g_assert_cmpint(s->drain_count, ==, 0);
232 g_assert_cmpint(backing_s->drain_count, ==, 0);
234 /* Now do the same while a request is pending */
235 aio_ret = -EINPROGRESS;
236 acb = blk_aio_preadv(blk, 0, &qiov, 0, aio_ret_cb, &aio_ret);
237 g_assert(acb != NULL);
238 g_assert_cmpint(aio_ret, ==, -EINPROGRESS);
240 g_assert_cmpint(s->drain_count, ==, 0);
241 g_assert_cmpint(backing_s->drain_count, ==, 0);
243 do_drain_begin(drain_type, bs);
245 g_assert_cmpint(aio_ret, ==, 0);
246 g_assert_cmpint(s->drain_count, ==, 1);
247 g_assert_cmpint(backing_s->drain_count, ==, !!recursive);
249 do_drain_end(drain_type, bs);
251 g_assert_cmpint(s->drain_count, ==, 0);
252 g_assert_cmpint(backing_s->drain_count, ==, 0);
254 bdrv_unref(backing);
255 bdrv_unref(bs);
256 blk_unref(blk);
259 static void test_drv_cb_drain_all(void)
261 test_drv_cb_common(BDRV_DRAIN_ALL, true);
264 static void test_drv_cb_drain(void)
266 test_drv_cb_common(BDRV_DRAIN, false);
269 static void test_drv_cb_drain_subtree(void)
271 test_drv_cb_common(BDRV_SUBTREE_DRAIN, true);
274 static void test_drv_cb_co_drain_all(void)
276 call_in_coroutine(test_drv_cb_drain_all);
279 static void test_drv_cb_co_drain(void)
281 call_in_coroutine(test_drv_cb_drain);
284 static void test_drv_cb_co_drain_subtree(void)
286 call_in_coroutine(test_drv_cb_drain_subtree);
289 static void test_quiesce_common(enum drain_type drain_type, bool recursive)
291 BlockBackend *blk;
292 BlockDriverState *bs, *backing;
294 blk = blk_new(qemu_get_aio_context(), BLK_PERM_ALL, BLK_PERM_ALL);
295 bs = bdrv_new_open_driver(&bdrv_test, "test-node", BDRV_O_RDWR,
296 &error_abort);
297 blk_insert_bs(blk, bs, &error_abort);
299 backing = bdrv_new_open_driver(&bdrv_test, "backing", 0, &error_abort);
300 bdrv_set_backing_hd(bs, backing, &error_abort);
302 g_assert_cmpint(bs->quiesce_counter, ==, 0);
303 g_assert_cmpint(backing->quiesce_counter, ==, 0);
305 do_drain_begin(drain_type, bs);
307 g_assert_cmpint(bs->quiesce_counter, ==, 1);
308 g_assert_cmpint(backing->quiesce_counter, ==, !!recursive);
310 do_drain_end(drain_type, bs);
312 g_assert_cmpint(bs->quiesce_counter, ==, 0);
313 g_assert_cmpint(backing->quiesce_counter, ==, 0);
315 bdrv_unref(backing);
316 bdrv_unref(bs);
317 blk_unref(blk);
320 static void test_quiesce_drain_all(void)
322 test_quiesce_common(BDRV_DRAIN_ALL, true);
325 static void test_quiesce_drain(void)
327 test_quiesce_common(BDRV_DRAIN, false);
330 static void test_quiesce_drain_subtree(void)
332 test_quiesce_common(BDRV_SUBTREE_DRAIN, true);
335 static void test_quiesce_co_drain_all(void)
337 call_in_coroutine(test_quiesce_drain_all);
340 static void test_quiesce_co_drain(void)
342 call_in_coroutine(test_quiesce_drain);
345 static void test_quiesce_co_drain_subtree(void)
347 call_in_coroutine(test_quiesce_drain_subtree);
350 static void test_nested(void)
352 BlockBackend *blk;
353 BlockDriverState *bs, *backing;
354 BDRVTestState *s, *backing_s;
355 enum drain_type outer, inner;
357 blk = blk_new(qemu_get_aio_context(), BLK_PERM_ALL, BLK_PERM_ALL);
358 bs = bdrv_new_open_driver(&bdrv_test, "test-node", BDRV_O_RDWR,
359 &error_abort);
360 s = bs->opaque;
361 blk_insert_bs(blk, bs, &error_abort);
363 backing = bdrv_new_open_driver(&bdrv_test, "backing", 0, &error_abort);
364 backing_s = backing->opaque;
365 bdrv_set_backing_hd(bs, backing, &error_abort);
367 for (outer = 0; outer < DRAIN_TYPE_MAX; outer++) {
368 for (inner = 0; inner < DRAIN_TYPE_MAX; inner++) {
369 int backing_quiesce = (outer != BDRV_DRAIN) +
370 (inner != BDRV_DRAIN);
372 g_assert_cmpint(bs->quiesce_counter, ==, 0);
373 g_assert_cmpint(backing->quiesce_counter, ==, 0);
374 g_assert_cmpint(s->drain_count, ==, 0);
375 g_assert_cmpint(backing_s->drain_count, ==, 0);
377 do_drain_begin(outer, bs);
378 do_drain_begin(inner, bs);
380 g_assert_cmpint(bs->quiesce_counter, ==, 2);
381 g_assert_cmpint(backing->quiesce_counter, ==, backing_quiesce);
382 g_assert_cmpint(s->drain_count, ==, 2);
383 g_assert_cmpint(backing_s->drain_count, ==, backing_quiesce);
385 do_drain_end(inner, bs);
386 do_drain_end(outer, bs);
388 g_assert_cmpint(bs->quiesce_counter, ==, 0);
389 g_assert_cmpint(backing->quiesce_counter, ==, 0);
390 g_assert_cmpint(s->drain_count, ==, 0);
391 g_assert_cmpint(backing_s->drain_count, ==, 0);
395 bdrv_unref(backing);
396 bdrv_unref(bs);
397 blk_unref(blk);
400 static void test_multiparent(void)
402 BlockBackend *blk_a, *blk_b;
403 BlockDriverState *bs_a, *bs_b, *backing;
404 BDRVTestState *a_s, *b_s, *backing_s;
406 blk_a = blk_new(qemu_get_aio_context(), BLK_PERM_ALL, BLK_PERM_ALL);
407 bs_a = bdrv_new_open_driver(&bdrv_test, "test-node-a", BDRV_O_RDWR,
408 &error_abort);
409 a_s = bs_a->opaque;
410 blk_insert_bs(blk_a, bs_a, &error_abort);
412 blk_b = blk_new(qemu_get_aio_context(), BLK_PERM_ALL, BLK_PERM_ALL);
413 bs_b = bdrv_new_open_driver(&bdrv_test, "test-node-b", BDRV_O_RDWR,
414 &error_abort);
415 b_s = bs_b->opaque;
416 blk_insert_bs(blk_b, bs_b, &error_abort);
418 backing = bdrv_new_open_driver(&bdrv_test, "backing", 0, &error_abort);
419 backing_s = backing->opaque;
420 bdrv_set_backing_hd(bs_a, backing, &error_abort);
421 bdrv_set_backing_hd(bs_b, backing, &error_abort);
423 g_assert_cmpint(bs_a->quiesce_counter, ==, 0);
424 g_assert_cmpint(bs_b->quiesce_counter, ==, 0);
425 g_assert_cmpint(backing->quiesce_counter, ==, 0);
426 g_assert_cmpint(a_s->drain_count, ==, 0);
427 g_assert_cmpint(b_s->drain_count, ==, 0);
428 g_assert_cmpint(backing_s->drain_count, ==, 0);
430 do_drain_begin(BDRV_SUBTREE_DRAIN, bs_a);
432 g_assert_cmpint(bs_a->quiesce_counter, ==, 1);
433 g_assert_cmpint(bs_b->quiesce_counter, ==, 1);
434 g_assert_cmpint(backing->quiesce_counter, ==, 1);
435 g_assert_cmpint(a_s->drain_count, ==, 1);
436 g_assert_cmpint(b_s->drain_count, ==, 1);
437 g_assert_cmpint(backing_s->drain_count, ==, 1);
439 do_drain_begin(BDRV_SUBTREE_DRAIN, bs_b);
441 g_assert_cmpint(bs_a->quiesce_counter, ==, 2);
442 g_assert_cmpint(bs_b->quiesce_counter, ==, 2);
443 g_assert_cmpint(backing->quiesce_counter, ==, 2);
444 g_assert_cmpint(a_s->drain_count, ==, 2);
445 g_assert_cmpint(b_s->drain_count, ==, 2);
446 g_assert_cmpint(backing_s->drain_count, ==, 2);
448 do_drain_end(BDRV_SUBTREE_DRAIN, bs_b);
450 g_assert_cmpint(bs_a->quiesce_counter, ==, 1);
451 g_assert_cmpint(bs_b->quiesce_counter, ==, 1);
452 g_assert_cmpint(backing->quiesce_counter, ==, 1);
453 g_assert_cmpint(a_s->drain_count, ==, 1);
454 g_assert_cmpint(b_s->drain_count, ==, 1);
455 g_assert_cmpint(backing_s->drain_count, ==, 1);
457 do_drain_end(BDRV_SUBTREE_DRAIN, bs_a);
459 g_assert_cmpint(bs_a->quiesce_counter, ==, 0);
460 g_assert_cmpint(bs_b->quiesce_counter, ==, 0);
461 g_assert_cmpint(backing->quiesce_counter, ==, 0);
462 g_assert_cmpint(a_s->drain_count, ==, 0);
463 g_assert_cmpint(b_s->drain_count, ==, 0);
464 g_assert_cmpint(backing_s->drain_count, ==, 0);
466 bdrv_unref(backing);
467 bdrv_unref(bs_a);
468 bdrv_unref(bs_b);
469 blk_unref(blk_a);
470 blk_unref(blk_b);
473 static void test_graph_change_drain_subtree(void)
475 BlockBackend *blk_a, *blk_b;
476 BlockDriverState *bs_a, *bs_b, *backing;
477 BDRVTestState *a_s, *b_s, *backing_s;
479 blk_a = blk_new(qemu_get_aio_context(), BLK_PERM_ALL, BLK_PERM_ALL);
480 bs_a = bdrv_new_open_driver(&bdrv_test, "test-node-a", BDRV_O_RDWR,
481 &error_abort);
482 a_s = bs_a->opaque;
483 blk_insert_bs(blk_a, bs_a, &error_abort);
485 blk_b = blk_new(qemu_get_aio_context(), BLK_PERM_ALL, BLK_PERM_ALL);
486 bs_b = bdrv_new_open_driver(&bdrv_test, "test-node-b", BDRV_O_RDWR,
487 &error_abort);
488 b_s = bs_b->opaque;
489 blk_insert_bs(blk_b, bs_b, &error_abort);
491 backing = bdrv_new_open_driver(&bdrv_test, "backing", 0, &error_abort);
492 backing_s = backing->opaque;
493 bdrv_set_backing_hd(bs_a, backing, &error_abort);
495 g_assert_cmpint(bs_a->quiesce_counter, ==, 0);
496 g_assert_cmpint(bs_b->quiesce_counter, ==, 0);
497 g_assert_cmpint(backing->quiesce_counter, ==, 0);
498 g_assert_cmpint(a_s->drain_count, ==, 0);
499 g_assert_cmpint(b_s->drain_count, ==, 0);
500 g_assert_cmpint(backing_s->drain_count, ==, 0);
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_a);
505 do_drain_begin(BDRV_SUBTREE_DRAIN, bs_b);
506 do_drain_begin(BDRV_SUBTREE_DRAIN, bs_b);
508 bdrv_set_backing_hd(bs_b, backing, &error_abort);
509 g_assert_cmpint(bs_a->quiesce_counter, ==, 5);
510 g_assert_cmpint(bs_b->quiesce_counter, ==, 5);
511 g_assert_cmpint(backing->quiesce_counter, ==, 5);
512 g_assert_cmpint(a_s->drain_count, ==, 5);
513 g_assert_cmpint(b_s->drain_count, ==, 5);
514 g_assert_cmpint(backing_s->drain_count, ==, 5);
516 bdrv_set_backing_hd(bs_b, NULL, &error_abort);
517 g_assert_cmpint(bs_a->quiesce_counter, ==, 3);
518 g_assert_cmpint(bs_b->quiesce_counter, ==, 2);
519 g_assert_cmpint(backing->quiesce_counter, ==, 3);
520 g_assert_cmpint(a_s->drain_count, ==, 3);
521 g_assert_cmpint(b_s->drain_count, ==, 2);
522 g_assert_cmpint(backing_s->drain_count, ==, 3);
524 bdrv_set_backing_hd(bs_b, backing, &error_abort);
525 g_assert_cmpint(bs_a->quiesce_counter, ==, 5);
526 g_assert_cmpint(bs_b->quiesce_counter, ==, 5);
527 g_assert_cmpint(backing->quiesce_counter, ==, 5);
528 g_assert_cmpint(a_s->drain_count, ==, 5);
529 g_assert_cmpint(b_s->drain_count, ==, 5);
530 g_assert_cmpint(backing_s->drain_count, ==, 5);
532 do_drain_end(BDRV_SUBTREE_DRAIN, bs_b);
533 do_drain_end(BDRV_SUBTREE_DRAIN, bs_b);
534 do_drain_end(BDRV_SUBTREE_DRAIN, bs_a);
535 do_drain_end(BDRV_SUBTREE_DRAIN, bs_a);
536 do_drain_end(BDRV_SUBTREE_DRAIN, bs_a);
538 g_assert_cmpint(bs_a->quiesce_counter, ==, 0);
539 g_assert_cmpint(bs_b->quiesce_counter, ==, 0);
540 g_assert_cmpint(backing->quiesce_counter, ==, 0);
541 g_assert_cmpint(a_s->drain_count, ==, 0);
542 g_assert_cmpint(b_s->drain_count, ==, 0);
543 g_assert_cmpint(backing_s->drain_count, ==, 0);
545 bdrv_unref(backing);
546 bdrv_unref(bs_a);
547 bdrv_unref(bs_b);
548 blk_unref(blk_a);
549 blk_unref(blk_b);
552 static void test_graph_change_drain_all(void)
554 BlockBackend *blk_a, *blk_b;
555 BlockDriverState *bs_a, *bs_b;
556 BDRVTestState *a_s, *b_s;
558 /* Create node A with a BlockBackend */
559 blk_a = blk_new(qemu_get_aio_context(), BLK_PERM_ALL, BLK_PERM_ALL);
560 bs_a = bdrv_new_open_driver(&bdrv_test, "test-node-a", BDRV_O_RDWR,
561 &error_abort);
562 a_s = bs_a->opaque;
563 blk_insert_bs(blk_a, bs_a, &error_abort);
565 g_assert_cmpint(bs_a->quiesce_counter, ==, 0);
566 g_assert_cmpint(a_s->drain_count, ==, 0);
568 /* Call bdrv_drain_all_begin() */
569 bdrv_drain_all_begin();
571 g_assert_cmpint(bs_a->quiesce_counter, ==, 1);
572 g_assert_cmpint(a_s->drain_count, ==, 1);
574 /* Create node B with a BlockBackend */
575 blk_b = blk_new(qemu_get_aio_context(), BLK_PERM_ALL, BLK_PERM_ALL);
576 bs_b = bdrv_new_open_driver(&bdrv_test, "test-node-b", BDRV_O_RDWR,
577 &error_abort);
578 b_s = bs_b->opaque;
579 blk_insert_bs(blk_b, bs_b, &error_abort);
581 g_assert_cmpint(bs_a->quiesce_counter, ==, 1);
582 g_assert_cmpint(bs_b->quiesce_counter, ==, 1);
583 g_assert_cmpint(a_s->drain_count, ==, 1);
584 g_assert_cmpint(b_s->drain_count, ==, 1);
586 /* Unref and finally delete node A */
587 blk_unref(blk_a);
589 g_assert_cmpint(bs_a->quiesce_counter, ==, 1);
590 g_assert_cmpint(bs_b->quiesce_counter, ==, 1);
591 g_assert_cmpint(a_s->drain_count, ==, 1);
592 g_assert_cmpint(b_s->drain_count, ==, 1);
594 bdrv_unref(bs_a);
596 g_assert_cmpint(bs_b->quiesce_counter, ==, 1);
597 g_assert_cmpint(b_s->drain_count, ==, 1);
599 /* End the drained section */
600 bdrv_drain_all_end();
602 g_assert_cmpint(bs_b->quiesce_counter, ==, 0);
603 g_assert_cmpint(b_s->drain_count, ==, 0);
605 bdrv_unref(bs_b);
606 blk_unref(blk_b);
609 struct test_iothread_data {
610 BlockDriverState *bs;
611 enum drain_type drain_type;
612 int *aio_ret;
615 static void test_iothread_drain_entry(void *opaque)
617 struct test_iothread_data *data = opaque;
619 aio_context_acquire(bdrv_get_aio_context(data->bs));
620 do_drain_begin(data->drain_type, data->bs);
621 g_assert_cmpint(*data->aio_ret, ==, 0);
622 do_drain_end(data->drain_type, data->bs);
623 aio_context_release(bdrv_get_aio_context(data->bs));
625 qemu_event_set(&done_event);
628 static void test_iothread_aio_cb(void *opaque, int ret)
630 int *aio_ret = opaque;
631 *aio_ret = ret;
632 qemu_event_set(&done_event);
635 static void test_iothread_main_thread_bh(void *opaque)
637 struct test_iothread_data *data = opaque;
639 /* Test that the AioContext is not yet locked in a random BH that is
640 * executed during drain, otherwise this would deadlock. */
641 aio_context_acquire(bdrv_get_aio_context(data->bs));
642 bdrv_flush(data->bs);
643 aio_context_release(bdrv_get_aio_context(data->bs));
647 * Starts an AIO request on a BDS that runs in the AioContext of iothread 1.
648 * The request involves a BH on iothread 2 before it can complete.
650 * @drain_thread = 0 means that do_drain_begin/end are called from the main
651 * thread, @drain_thread = 1 means that they are called from iothread 1. Drain
652 * for this BDS cannot be called from iothread 2 because only the main thread
653 * may do cross-AioContext polling.
655 static void test_iothread_common(enum drain_type drain_type, int drain_thread)
657 BlockBackend *blk;
658 BlockDriverState *bs;
659 BDRVTestState *s;
660 BlockAIOCB *acb;
661 int aio_ret;
662 struct test_iothread_data data;
664 IOThread *a = iothread_new();
665 IOThread *b = iothread_new();
666 AioContext *ctx_a = iothread_get_aio_context(a);
667 AioContext *ctx_b = iothread_get_aio_context(b);
669 QEMUIOVector qiov = QEMU_IOVEC_INIT_BUF(qiov, NULL, 0);
671 /* bdrv_drain_all() may only be called from the main loop thread */
672 if (drain_type == BDRV_DRAIN_ALL && drain_thread != 0) {
673 goto out;
676 blk = blk_new(qemu_get_aio_context(), BLK_PERM_ALL, BLK_PERM_ALL);
677 bs = bdrv_new_open_driver(&bdrv_test, "test-node", BDRV_O_RDWR,
678 &error_abort);
679 s = bs->opaque;
680 blk_insert_bs(blk, bs, &error_abort);
682 blk_set_aio_context(blk, ctx_a, &error_abort);
683 aio_context_acquire(ctx_a);
685 s->bh_indirection_ctx = ctx_b;
687 aio_ret = -EINPROGRESS;
688 qemu_event_reset(&done_event);
690 if (drain_thread == 0) {
691 acb = blk_aio_preadv(blk, 0, &qiov, 0, test_iothread_aio_cb, &aio_ret);
692 } else {
693 acb = blk_aio_preadv(blk, 0, &qiov, 0, aio_ret_cb, &aio_ret);
695 g_assert(acb != NULL);
696 g_assert_cmpint(aio_ret, ==, -EINPROGRESS);
698 aio_context_release(ctx_a);
700 data = (struct test_iothread_data) {
701 .bs = bs,
702 .drain_type = drain_type,
703 .aio_ret = &aio_ret,
706 switch (drain_thread) {
707 case 0:
708 if (drain_type != BDRV_DRAIN_ALL) {
709 aio_context_acquire(ctx_a);
712 aio_bh_schedule_oneshot(ctx_a, test_iothread_main_thread_bh, &data);
714 /* The request is running on the IOThread a. Draining its block device
715 * will make sure that it has completed as far as the BDS is concerned,
716 * but the drain in this thread can continue immediately after
717 * bdrv_dec_in_flight() and aio_ret might be assigned only slightly
718 * later. */
719 do_drain_begin(drain_type, bs);
720 g_assert_cmpint(bs->in_flight, ==, 0);
722 if (drain_type != BDRV_DRAIN_ALL) {
723 aio_context_release(ctx_a);
725 qemu_event_wait(&done_event);
726 if (drain_type != BDRV_DRAIN_ALL) {
727 aio_context_acquire(ctx_a);
730 g_assert_cmpint(aio_ret, ==, 0);
731 do_drain_end(drain_type, bs);
733 if (drain_type != BDRV_DRAIN_ALL) {
734 aio_context_release(ctx_a);
736 break;
737 case 1:
738 aio_bh_schedule_oneshot(ctx_a, test_iothread_drain_entry, &data);
739 qemu_event_wait(&done_event);
740 break;
741 default:
742 g_assert_not_reached();
745 aio_context_acquire(ctx_a);
746 blk_set_aio_context(blk, qemu_get_aio_context(), &error_abort);
747 aio_context_release(ctx_a);
749 bdrv_unref(bs);
750 blk_unref(blk);
752 out:
753 iothread_join(a);
754 iothread_join(b);
757 static void test_iothread_drain_all(void)
759 test_iothread_common(BDRV_DRAIN_ALL, 0);
760 test_iothread_common(BDRV_DRAIN_ALL, 1);
763 static void test_iothread_drain(void)
765 test_iothread_common(BDRV_DRAIN, 0);
766 test_iothread_common(BDRV_DRAIN, 1);
769 static void test_iothread_drain_subtree(void)
771 test_iothread_common(BDRV_SUBTREE_DRAIN, 0);
772 test_iothread_common(BDRV_SUBTREE_DRAIN, 1);
776 typedef struct TestBlockJob {
777 BlockJob common;
778 int run_ret;
779 int prepare_ret;
780 bool running;
781 bool should_complete;
782 } TestBlockJob;
784 static int test_job_prepare(Job *job)
786 TestBlockJob *s = container_of(job, TestBlockJob, common.job);
788 /* Provoke an AIO_WAIT_WHILE() call to verify there is no deadlock */
789 blk_flush(s->common.blk);
790 return s->prepare_ret;
793 static void test_job_commit(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);
801 static void test_job_abort(Job *job)
803 TestBlockJob *s = container_of(job, TestBlockJob, common.job);
805 /* Provoke an AIO_WAIT_WHILE() call to verify there is no deadlock */
806 blk_flush(s->common.blk);
809 static int coroutine_fn test_job_run(Job *job, Error **errp)
811 TestBlockJob *s = container_of(job, TestBlockJob, common.job);
813 /* We are running the actual job code past the pause point in
814 * job_co_entry(). */
815 s->running = true;
817 job_transition_to_ready(&s->common.job);
818 while (!s->should_complete) {
819 /* Avoid job_sleep_ns() because it marks the job as !busy. We want to
820 * emulate some actual activity (probably some I/O) here so that drain
821 * has to wait for this activity to stop. */
822 qemu_co_sleep_ns(QEMU_CLOCK_REALTIME, 1000000);
824 job_pause_point(&s->common.job);
827 return s->run_ret;
830 static void test_job_complete(Job *job, Error **errp)
832 TestBlockJob *s = container_of(job, TestBlockJob, common.job);
833 s->should_complete = true;
836 BlockJobDriver test_job_driver = {
837 .job_driver = {
838 .instance_size = sizeof(TestBlockJob),
839 .free = block_job_free,
840 .user_resume = block_job_user_resume,
841 .drain = block_job_drain,
842 .run = test_job_run,
843 .complete = test_job_complete,
844 .prepare = test_job_prepare,
845 .commit = test_job_commit,
846 .abort = test_job_abort,
850 enum test_job_result {
851 TEST_JOB_SUCCESS,
852 TEST_JOB_FAIL_RUN,
853 TEST_JOB_FAIL_PREPARE,
856 enum test_job_drain_node {
857 TEST_JOB_DRAIN_SRC,
858 TEST_JOB_DRAIN_SRC_CHILD,
859 TEST_JOB_DRAIN_SRC_PARENT,
862 static void test_blockjob_common_drain_node(enum drain_type drain_type,
863 bool use_iothread,
864 enum test_job_result result,
865 enum test_job_drain_node drain_node)
867 BlockBackend *blk_src, *blk_target;
868 BlockDriverState *src, *src_backing, *src_overlay, *target, *drain_bs;
869 BlockJob *job;
870 TestBlockJob *tjob;
871 IOThread *iothread = NULL;
872 AioContext *ctx;
873 int ret;
875 src = bdrv_new_open_driver(&bdrv_test, "source", BDRV_O_RDWR,
876 &error_abort);
877 src_backing = bdrv_new_open_driver(&bdrv_test, "source-backing",
878 BDRV_O_RDWR, &error_abort);
879 src_overlay = bdrv_new_open_driver(&bdrv_test, "source-overlay",
880 BDRV_O_RDWR, &error_abort);
882 bdrv_set_backing_hd(src_overlay, src, &error_abort);
883 bdrv_unref(src);
884 bdrv_set_backing_hd(src, src_backing, &error_abort);
885 bdrv_unref(src_backing);
887 blk_src = blk_new(qemu_get_aio_context(), BLK_PERM_ALL, BLK_PERM_ALL);
888 blk_insert_bs(blk_src, src_overlay, &error_abort);
890 switch (drain_node) {
891 case TEST_JOB_DRAIN_SRC:
892 drain_bs = src;
893 break;
894 case TEST_JOB_DRAIN_SRC_CHILD:
895 drain_bs = src_backing;
896 break;
897 case TEST_JOB_DRAIN_SRC_PARENT:
898 drain_bs = src_overlay;
899 break;
900 default:
901 g_assert_not_reached();
904 if (use_iothread) {
905 iothread = iothread_new();
906 ctx = iothread_get_aio_context(iothread);
907 blk_set_aio_context(blk_src, ctx, &error_abort);
908 } else {
909 ctx = qemu_get_aio_context();
912 target = bdrv_new_open_driver(&bdrv_test, "target", BDRV_O_RDWR,
913 &error_abort);
914 blk_target = blk_new(qemu_get_aio_context(), BLK_PERM_ALL, BLK_PERM_ALL);
915 blk_insert_bs(blk_target, target, &error_abort);
916 blk_set_allow_aio_context_change(blk_target, true);
918 aio_context_acquire(ctx);
919 tjob = block_job_create("job0", &test_job_driver, NULL, src,
920 0, BLK_PERM_ALL,
921 0, 0, NULL, NULL, &error_abort);
922 job = &tjob->common;
923 block_job_add_bdrv(job, "target", target, 0, BLK_PERM_ALL, &error_abort);
925 switch (result) {
926 case TEST_JOB_SUCCESS:
927 break;
928 case TEST_JOB_FAIL_RUN:
929 tjob->run_ret = -EIO;
930 break;
931 case TEST_JOB_FAIL_PREPARE:
932 tjob->prepare_ret = -EIO;
933 break;
936 job_start(&job->job);
937 aio_context_release(ctx);
939 if (use_iothread) {
940 /* job_co_entry() is run in the I/O thread, wait for the actual job
941 * code to start (we don't want to catch the job in the pause point in
942 * job_co_entry(). */
943 while (!tjob->running) {
944 aio_poll(qemu_get_aio_context(), false);
948 g_assert_cmpint(job->job.pause_count, ==, 0);
949 g_assert_false(job->job.paused);
950 g_assert_true(tjob->running);
951 g_assert_true(job->job.busy); /* We're in qemu_co_sleep_ns() */
953 do_drain_begin_unlocked(drain_type, drain_bs);
955 if (drain_type == BDRV_DRAIN_ALL) {
956 /* bdrv_drain_all() drains both src and target */
957 g_assert_cmpint(job->job.pause_count, ==, 2);
958 } else {
959 g_assert_cmpint(job->job.pause_count, ==, 1);
961 g_assert_true(job->job.paused);
962 g_assert_false(job->job.busy); /* The job is paused */
964 do_drain_end_unlocked(drain_type, drain_bs);
966 if (use_iothread) {
967 /* paused is reset in the I/O thread, wait for it */
968 while (job->job.paused) {
969 aio_poll(qemu_get_aio_context(), false);
973 g_assert_cmpint(job->job.pause_count, ==, 0);
974 g_assert_false(job->job.paused);
975 g_assert_true(job->job.busy); /* We're in qemu_co_sleep_ns() */
977 do_drain_begin_unlocked(drain_type, target);
979 if (drain_type == BDRV_DRAIN_ALL) {
980 /* bdrv_drain_all() drains both src and target */
981 g_assert_cmpint(job->job.pause_count, ==, 2);
982 } else {
983 g_assert_cmpint(job->job.pause_count, ==, 1);
985 g_assert_true(job->job.paused);
986 g_assert_false(job->job.busy); /* The job is paused */
988 do_drain_end_unlocked(drain_type, target);
990 if (use_iothread) {
991 /* paused is reset in the I/O thread, wait for it */
992 while (job->job.paused) {
993 aio_poll(qemu_get_aio_context(), false);
997 g_assert_cmpint(job->job.pause_count, ==, 0);
998 g_assert_false(job->job.paused);
999 g_assert_true(job->job.busy); /* We're in qemu_co_sleep_ns() */
1001 aio_context_acquire(ctx);
1002 ret = job_complete_sync(&job->job, &error_abort);
1003 g_assert_cmpint(ret, ==, (result == TEST_JOB_SUCCESS ? 0 : -EIO));
1005 if (use_iothread) {
1006 blk_set_aio_context(blk_src, qemu_get_aio_context(), &error_abort);
1007 assert(blk_get_aio_context(blk_target) == qemu_get_aio_context());
1009 aio_context_release(ctx);
1011 blk_unref(blk_src);
1012 blk_unref(blk_target);
1013 bdrv_unref(src_overlay);
1014 bdrv_unref(target);
1016 if (iothread) {
1017 iothread_join(iothread);
1021 static void test_blockjob_common(enum drain_type drain_type, bool use_iothread,
1022 enum test_job_result result)
1024 test_blockjob_common_drain_node(drain_type, use_iothread, result,
1025 TEST_JOB_DRAIN_SRC);
1026 test_blockjob_common_drain_node(drain_type, use_iothread, result,
1027 TEST_JOB_DRAIN_SRC_CHILD);
1028 if (drain_type == BDRV_SUBTREE_DRAIN) {
1029 test_blockjob_common_drain_node(drain_type, use_iothread, result,
1030 TEST_JOB_DRAIN_SRC_PARENT);
1034 static void test_blockjob_drain_all(void)
1036 test_blockjob_common(BDRV_DRAIN_ALL, false, TEST_JOB_SUCCESS);
1039 static void test_blockjob_drain(void)
1041 test_blockjob_common(BDRV_DRAIN, false, TEST_JOB_SUCCESS);
1044 static void test_blockjob_drain_subtree(void)
1046 test_blockjob_common(BDRV_SUBTREE_DRAIN, false, TEST_JOB_SUCCESS);
1049 static void test_blockjob_error_drain_all(void)
1051 test_blockjob_common(BDRV_DRAIN_ALL, false, TEST_JOB_FAIL_RUN);
1052 test_blockjob_common(BDRV_DRAIN_ALL, false, TEST_JOB_FAIL_PREPARE);
1055 static void test_blockjob_error_drain(void)
1057 test_blockjob_common(BDRV_DRAIN, false, TEST_JOB_FAIL_RUN);
1058 test_blockjob_common(BDRV_DRAIN, false, TEST_JOB_FAIL_PREPARE);
1061 static void test_blockjob_error_drain_subtree(void)
1063 test_blockjob_common(BDRV_SUBTREE_DRAIN, false, TEST_JOB_FAIL_RUN);
1064 test_blockjob_common(BDRV_SUBTREE_DRAIN, false, TEST_JOB_FAIL_PREPARE);
1067 static void test_blockjob_iothread_drain_all(void)
1069 test_blockjob_common(BDRV_DRAIN_ALL, true, TEST_JOB_SUCCESS);
1072 static void test_blockjob_iothread_drain(void)
1074 test_blockjob_common(BDRV_DRAIN, true, TEST_JOB_SUCCESS);
1077 static void test_blockjob_iothread_drain_subtree(void)
1079 test_blockjob_common(BDRV_SUBTREE_DRAIN, true, TEST_JOB_SUCCESS);
1082 static void test_blockjob_iothread_error_drain_all(void)
1084 test_blockjob_common(BDRV_DRAIN_ALL, true, TEST_JOB_FAIL_RUN);
1085 test_blockjob_common(BDRV_DRAIN_ALL, true, TEST_JOB_FAIL_PREPARE);
1088 static void test_blockjob_iothread_error_drain(void)
1090 test_blockjob_common(BDRV_DRAIN, true, TEST_JOB_FAIL_RUN);
1091 test_blockjob_common(BDRV_DRAIN, true, TEST_JOB_FAIL_PREPARE);
1094 static void test_blockjob_iothread_error_drain_subtree(void)
1096 test_blockjob_common(BDRV_SUBTREE_DRAIN, true, TEST_JOB_FAIL_RUN);
1097 test_blockjob_common(BDRV_SUBTREE_DRAIN, true, TEST_JOB_FAIL_PREPARE);
1101 typedef struct BDRVTestTopState {
1102 BdrvChild *wait_child;
1103 } BDRVTestTopState;
1105 static void bdrv_test_top_close(BlockDriverState *bs)
1107 BdrvChild *c, *next_c;
1108 QLIST_FOREACH_SAFE(c, &bs->children, next, next_c) {
1109 bdrv_unref_child(bs, c);
1113 static int coroutine_fn bdrv_test_top_co_preadv(BlockDriverState *bs,
1114 uint64_t offset, uint64_t bytes,
1115 QEMUIOVector *qiov, int flags)
1117 BDRVTestTopState *tts = bs->opaque;
1118 return bdrv_co_preadv(tts->wait_child, offset, bytes, qiov, flags);
1121 static BlockDriver bdrv_test_top_driver = {
1122 .format_name = "test_top_driver",
1123 .instance_size = sizeof(BDRVTestTopState),
1125 .bdrv_close = bdrv_test_top_close,
1126 .bdrv_co_preadv = bdrv_test_top_co_preadv,
1128 .bdrv_child_perm = bdrv_format_default_perms,
1131 typedef struct TestCoDeleteByDrainData {
1132 BlockBackend *blk;
1133 bool detach_instead_of_delete;
1134 bool done;
1135 } TestCoDeleteByDrainData;
1137 static void coroutine_fn test_co_delete_by_drain(void *opaque)
1139 TestCoDeleteByDrainData *dbdd = opaque;
1140 BlockBackend *blk = dbdd->blk;
1141 BlockDriverState *bs = blk_bs(blk);
1142 BDRVTestTopState *tts = bs->opaque;
1143 void *buffer = g_malloc(65536);
1144 QEMUIOVector qiov = QEMU_IOVEC_INIT_BUF(qiov, buffer, 65536);
1146 /* Pretend some internal write operation from parent to child.
1147 * Important: We have to read from the child, not from the parent!
1148 * Draining works by first propagating it all up the tree to the
1149 * root and then waiting for drainage from root to the leaves
1150 * (protocol nodes). If we have a request waiting on the root,
1151 * everything will be drained before we go back down the tree, but
1152 * we do not want that. We want to be in the middle of draining
1153 * when this following requests returns. */
1154 bdrv_co_preadv(tts->wait_child, 0, 65536, &qiov, 0);
1156 g_assert_cmpint(bs->refcnt, ==, 1);
1158 if (!dbdd->detach_instead_of_delete) {
1159 blk_unref(blk);
1160 } else {
1161 BdrvChild *c, *next_c;
1162 QLIST_FOREACH_SAFE(c, &bs->children, next, next_c) {
1163 bdrv_unref_child(bs, c);
1167 dbdd->done = true;
1168 g_free(buffer);
1172 * Test what happens when some BDS has some children, you drain one of
1173 * them and this results in the BDS being deleted.
1175 * If @detach_instead_of_delete is set, the BDS is not going to be
1176 * deleted but will only detach all of its children.
1178 static void do_test_delete_by_drain(bool detach_instead_of_delete,
1179 enum drain_type drain_type)
1181 BlockBackend *blk;
1182 BlockDriverState *bs, *child_bs, *null_bs;
1183 BDRVTestTopState *tts;
1184 TestCoDeleteByDrainData dbdd;
1185 Coroutine *co;
1187 bs = bdrv_new_open_driver(&bdrv_test_top_driver, "top", BDRV_O_RDWR,
1188 &error_abort);
1189 bs->total_sectors = 65536 >> BDRV_SECTOR_BITS;
1190 tts = bs->opaque;
1192 null_bs = bdrv_open("null-co://", NULL, NULL, BDRV_O_RDWR | BDRV_O_PROTOCOL,
1193 &error_abort);
1194 bdrv_attach_child(bs, null_bs, "null-child", &child_file, &error_abort);
1196 /* This child will be the one to pass to requests through to, and
1197 * it will stall until a drain occurs */
1198 child_bs = bdrv_new_open_driver(&bdrv_test, "child", BDRV_O_RDWR,
1199 &error_abort);
1200 child_bs->total_sectors = 65536 >> BDRV_SECTOR_BITS;
1201 /* Takes our reference to child_bs */
1202 tts->wait_child = bdrv_attach_child(bs, child_bs, "wait-child", &child_file,
1203 &error_abort);
1205 /* This child is just there to be deleted
1206 * (for detach_instead_of_delete == true) */
1207 null_bs = bdrv_open("null-co://", NULL, NULL, BDRV_O_RDWR | BDRV_O_PROTOCOL,
1208 &error_abort);
1209 bdrv_attach_child(bs, null_bs, "null-child", &child_file, &error_abort);
1211 blk = blk_new(qemu_get_aio_context(), BLK_PERM_ALL, BLK_PERM_ALL);
1212 blk_insert_bs(blk, bs, &error_abort);
1214 /* Referenced by blk now */
1215 bdrv_unref(bs);
1217 g_assert_cmpint(bs->refcnt, ==, 1);
1218 g_assert_cmpint(child_bs->refcnt, ==, 1);
1219 g_assert_cmpint(null_bs->refcnt, ==, 1);
1222 dbdd = (TestCoDeleteByDrainData){
1223 .blk = blk,
1224 .detach_instead_of_delete = detach_instead_of_delete,
1225 .done = false,
1227 co = qemu_coroutine_create(test_co_delete_by_drain, &dbdd);
1228 qemu_coroutine_enter(co);
1230 /* Drain the child while the read operation is still pending.
1231 * This should result in the operation finishing and
1232 * test_co_delete_by_drain() resuming. Thus, @bs will be deleted
1233 * and the coroutine will exit while this drain operation is still
1234 * in progress. */
1235 switch (drain_type) {
1236 case BDRV_DRAIN:
1237 bdrv_ref(child_bs);
1238 bdrv_drain(child_bs);
1239 bdrv_unref(child_bs);
1240 break;
1241 case BDRV_SUBTREE_DRAIN:
1242 /* Would have to ref/unref bs here for !detach_instead_of_delete, but
1243 * then the whole test becomes pointless because the graph changes
1244 * don't occur during the drain any more. */
1245 assert(detach_instead_of_delete);
1246 bdrv_subtree_drained_begin(bs);
1247 bdrv_subtree_drained_end(bs);
1248 break;
1249 case BDRV_DRAIN_ALL:
1250 bdrv_drain_all_begin();
1251 bdrv_drain_all_end();
1252 break;
1253 default:
1254 g_assert_not_reached();
1257 while (!dbdd.done) {
1258 aio_poll(qemu_get_aio_context(), true);
1261 if (detach_instead_of_delete) {
1262 /* Here, the reference has not passed over to the coroutine,
1263 * so we have to delete the BB ourselves */
1264 blk_unref(blk);
1268 static void test_delete_by_drain(void)
1270 do_test_delete_by_drain(false, BDRV_DRAIN);
1273 static void test_detach_by_drain_all(void)
1275 do_test_delete_by_drain(true, BDRV_DRAIN_ALL);
1278 static void test_detach_by_drain(void)
1280 do_test_delete_by_drain(true, BDRV_DRAIN);
1283 static void test_detach_by_drain_subtree(void)
1285 do_test_delete_by_drain(true, BDRV_SUBTREE_DRAIN);
1289 struct detach_by_parent_data {
1290 BlockDriverState *parent_b;
1291 BdrvChild *child_b;
1292 BlockDriverState *c;
1293 BdrvChild *child_c;
1294 bool by_parent_cb;
1296 static struct detach_by_parent_data detach_by_parent_data;
1298 static void detach_indirect_bh(void *opaque)
1300 struct detach_by_parent_data *data = opaque;
1302 bdrv_unref_child(data->parent_b, data->child_b);
1304 bdrv_ref(data->c);
1305 data->child_c = bdrv_attach_child(data->parent_b, data->c, "PB-C",
1306 &child_file, &error_abort);
1309 static void detach_by_parent_aio_cb(void *opaque, int ret)
1311 struct detach_by_parent_data *data = &detach_by_parent_data;
1313 g_assert_cmpint(ret, ==, 0);
1314 if (data->by_parent_cb) {
1315 detach_indirect_bh(data);
1319 static void detach_by_driver_cb_drained_begin(BdrvChild *child)
1321 aio_bh_schedule_oneshot(qemu_get_current_aio_context(),
1322 detach_indirect_bh, &detach_by_parent_data);
1323 child_file.drained_begin(child);
1326 static BdrvChildRole detach_by_driver_cb_role;
1329 * Initial graph:
1331 * PA PB
1332 * \ / \
1333 * A B C
1335 * by_parent_cb == true: Test that parent callbacks don't poll
1337 * PA has a pending write request whose callback changes the child nodes of
1338 * PB: It removes B and adds C instead. The subtree of PB is drained, which
1339 * will indirectly drain the write request, too.
1341 * by_parent_cb == false: Test that bdrv_drain_invoke() doesn't poll
1343 * PA's BdrvChildRole has a .drained_begin callback that schedules a BH
1344 * that does the same graph change. If bdrv_drain_invoke() calls it, the
1345 * state is messed up, but if it is only polled in the single
1346 * BDRV_POLL_WHILE() at the end of the drain, this should work fine.
1348 static void test_detach_indirect(bool by_parent_cb)
1350 BlockBackend *blk;
1351 BlockDriverState *parent_a, *parent_b, *a, *b, *c;
1352 BdrvChild *child_a, *child_b;
1353 BlockAIOCB *acb;
1355 QEMUIOVector qiov = QEMU_IOVEC_INIT_BUF(qiov, NULL, 0);
1357 if (!by_parent_cb) {
1358 detach_by_driver_cb_role = child_file;
1359 detach_by_driver_cb_role.drained_begin =
1360 detach_by_driver_cb_drained_begin;
1363 /* Create all involved nodes */
1364 parent_a = bdrv_new_open_driver(&bdrv_test, "parent-a", BDRV_O_RDWR,
1365 &error_abort);
1366 parent_b = bdrv_new_open_driver(&bdrv_test, "parent-b", 0,
1367 &error_abort);
1369 a = bdrv_new_open_driver(&bdrv_test, "a", BDRV_O_RDWR, &error_abort);
1370 b = bdrv_new_open_driver(&bdrv_test, "b", BDRV_O_RDWR, &error_abort);
1371 c = bdrv_new_open_driver(&bdrv_test, "c", BDRV_O_RDWR, &error_abort);
1373 /* blk is a BB for parent-a */
1374 blk = blk_new(qemu_get_aio_context(), BLK_PERM_ALL, BLK_PERM_ALL);
1375 blk_insert_bs(blk, parent_a, &error_abort);
1376 bdrv_unref(parent_a);
1378 /* If we want to get bdrv_drain_invoke() to call aio_poll(), the driver
1379 * callback must not return immediately. */
1380 if (!by_parent_cb) {
1381 BDRVTestState *s = parent_a->opaque;
1382 s->sleep_in_drain_begin = true;
1385 /* Set child relationships */
1386 bdrv_ref(b);
1387 bdrv_ref(a);
1388 child_b = bdrv_attach_child(parent_b, b, "PB-B", &child_file, &error_abort);
1389 child_a = bdrv_attach_child(parent_b, a, "PB-A", &child_backing, &error_abort);
1391 bdrv_ref(a);
1392 bdrv_attach_child(parent_a, a, "PA-A",
1393 by_parent_cb ? &child_file : &detach_by_driver_cb_role,
1394 &error_abort);
1396 g_assert_cmpint(parent_a->refcnt, ==, 1);
1397 g_assert_cmpint(parent_b->refcnt, ==, 1);
1398 g_assert_cmpint(a->refcnt, ==, 3);
1399 g_assert_cmpint(b->refcnt, ==, 2);
1400 g_assert_cmpint(c->refcnt, ==, 1);
1402 g_assert(QLIST_FIRST(&parent_b->children) == child_a);
1403 g_assert(QLIST_NEXT(child_a, next) == child_b);
1404 g_assert(QLIST_NEXT(child_b, next) == NULL);
1406 /* Start the evil write request */
1407 detach_by_parent_data = (struct detach_by_parent_data) {
1408 .parent_b = parent_b,
1409 .child_b = child_b,
1410 .c = c,
1411 .by_parent_cb = by_parent_cb,
1413 acb = blk_aio_preadv(blk, 0, &qiov, 0, detach_by_parent_aio_cb, NULL);
1414 g_assert(acb != NULL);
1416 /* Drain and check the expected result */
1417 bdrv_subtree_drained_begin(parent_b);
1419 g_assert(detach_by_parent_data.child_c != NULL);
1421 g_assert_cmpint(parent_a->refcnt, ==, 1);
1422 g_assert_cmpint(parent_b->refcnt, ==, 1);
1423 g_assert_cmpint(a->refcnt, ==, 3);
1424 g_assert_cmpint(b->refcnt, ==, 1);
1425 g_assert_cmpint(c->refcnt, ==, 2);
1427 g_assert(QLIST_FIRST(&parent_b->children) == detach_by_parent_data.child_c);
1428 g_assert(QLIST_NEXT(detach_by_parent_data.child_c, next) == child_a);
1429 g_assert(QLIST_NEXT(child_a, next) == NULL);
1431 g_assert_cmpint(parent_a->quiesce_counter, ==, 1);
1432 g_assert_cmpint(parent_b->quiesce_counter, ==, 1);
1433 g_assert_cmpint(a->quiesce_counter, ==, 1);
1434 g_assert_cmpint(b->quiesce_counter, ==, 0);
1435 g_assert_cmpint(c->quiesce_counter, ==, 1);
1437 bdrv_subtree_drained_end(parent_b);
1439 bdrv_unref(parent_b);
1440 blk_unref(blk);
1442 g_assert_cmpint(a->refcnt, ==, 1);
1443 g_assert_cmpint(b->refcnt, ==, 1);
1444 g_assert_cmpint(c->refcnt, ==, 1);
1445 bdrv_unref(a);
1446 bdrv_unref(b);
1447 bdrv_unref(c);
1450 static void test_detach_by_parent_cb(void)
1452 test_detach_indirect(true);
1455 static void test_detach_by_driver_cb(void)
1457 test_detach_indirect(false);
1460 static void test_append_to_drained(void)
1462 BlockBackend *blk;
1463 BlockDriverState *base, *overlay;
1464 BDRVTestState *base_s, *overlay_s;
1466 blk = blk_new(qemu_get_aio_context(), BLK_PERM_ALL, BLK_PERM_ALL);
1467 base = bdrv_new_open_driver(&bdrv_test, "base", BDRV_O_RDWR, &error_abort);
1468 base_s = base->opaque;
1469 blk_insert_bs(blk, base, &error_abort);
1471 overlay = bdrv_new_open_driver(&bdrv_test, "overlay", BDRV_O_RDWR,
1472 &error_abort);
1473 overlay_s = overlay->opaque;
1475 do_drain_begin(BDRV_DRAIN, base);
1476 g_assert_cmpint(base->quiesce_counter, ==, 1);
1477 g_assert_cmpint(base_s->drain_count, ==, 1);
1478 g_assert_cmpint(base->in_flight, ==, 0);
1480 /* Takes ownership of overlay, so we don't have to unref it later */
1481 bdrv_append(overlay, base, &error_abort);
1482 g_assert_cmpint(base->in_flight, ==, 0);
1483 g_assert_cmpint(overlay->in_flight, ==, 0);
1485 g_assert_cmpint(base->quiesce_counter, ==, 1);
1486 g_assert_cmpint(base_s->drain_count, ==, 1);
1487 g_assert_cmpint(overlay->quiesce_counter, ==, 1);
1488 g_assert_cmpint(overlay_s->drain_count, ==, 1);
1490 do_drain_end(BDRV_DRAIN, base);
1492 g_assert_cmpint(base->quiesce_counter, ==, 0);
1493 g_assert_cmpint(base_s->drain_count, ==, 0);
1494 g_assert_cmpint(overlay->quiesce_counter, ==, 0);
1495 g_assert_cmpint(overlay_s->drain_count, ==, 0);
1497 bdrv_unref(base);
1498 blk_unref(blk);
1501 static void test_set_aio_context(void)
1503 BlockDriverState *bs;
1504 IOThread *a = iothread_new();
1505 IOThread *b = iothread_new();
1506 AioContext *ctx_a = iothread_get_aio_context(a);
1507 AioContext *ctx_b = iothread_get_aio_context(b);
1509 bs = bdrv_new_open_driver(&bdrv_test, "test-node", BDRV_O_RDWR,
1510 &error_abort);
1512 bdrv_drained_begin(bs);
1513 bdrv_try_set_aio_context(bs, ctx_a, &error_abort);
1515 aio_context_acquire(ctx_a);
1516 bdrv_drained_end(bs);
1518 bdrv_drained_begin(bs);
1519 bdrv_try_set_aio_context(bs, ctx_b, &error_abort);
1520 aio_context_release(ctx_a);
1521 aio_context_acquire(ctx_b);
1522 bdrv_try_set_aio_context(bs, qemu_get_aio_context(), &error_abort);
1523 aio_context_release(ctx_b);
1524 bdrv_drained_end(bs);
1526 bdrv_unref(bs);
1527 iothread_join(a);
1528 iothread_join(b);
1532 typedef struct TestDropBackingBlockJob {
1533 BlockJob common;
1534 bool should_complete;
1535 bool *did_complete;
1536 BlockDriverState *detach_also;
1537 } TestDropBackingBlockJob;
1539 static int coroutine_fn test_drop_backing_job_run(Job *job, Error **errp)
1541 TestDropBackingBlockJob *s =
1542 container_of(job, TestDropBackingBlockJob, common.job);
1544 while (!s->should_complete) {
1545 job_sleep_ns(job, 0);
1548 return 0;
1551 static void test_drop_backing_job_commit(Job *job)
1553 TestDropBackingBlockJob *s =
1554 container_of(job, TestDropBackingBlockJob, common.job);
1556 bdrv_set_backing_hd(blk_bs(s->common.blk), NULL, &error_abort);
1557 bdrv_set_backing_hd(s->detach_also, NULL, &error_abort);
1559 *s->did_complete = true;
1562 static const BlockJobDriver test_drop_backing_job_driver = {
1563 .job_driver = {
1564 .instance_size = sizeof(TestDropBackingBlockJob),
1565 .free = block_job_free,
1566 .user_resume = block_job_user_resume,
1567 .drain = block_job_drain,
1568 .run = test_drop_backing_job_run,
1569 .commit = test_drop_backing_job_commit,
1574 * Creates a child node with three parent nodes on it, and then runs a
1575 * block job on the final one, parent-node-2.
1577 * The job is then asked to complete before a section where the child
1578 * is drained.
1580 * Ending this section will undrain the child's parents, first
1581 * parent-node-2, then parent-node-1, then parent-node-0 -- the parent
1582 * list is in reverse order of how they were added. Ending the drain
1583 * on parent-node-2 will resume the job, thus completing it and
1584 * scheduling job_exit().
1586 * Ending the drain on parent-node-1 will poll the AioContext, which
1587 * lets job_exit() and thus test_drop_backing_job_commit() run. That
1588 * function first removes the child as parent-node-2's backing file.
1590 * In old (and buggy) implementations, there are two problems with
1591 * that:
1592 * (A) bdrv_drain_invoke() polls for every node that leaves the
1593 * drained section. This means that job_exit() is scheduled
1594 * before the child has left the drained section. Its
1595 * quiesce_counter is therefore still 1 when it is removed from
1596 * parent-node-2.
1598 * (B) bdrv_replace_child_noperm() calls drained_end() on the old
1599 * child's parents as many times as the child is quiesced. This
1600 * means it will call drained_end() on parent-node-2 once.
1601 * Because parent-node-2 is no longer quiesced at this point, this
1602 * will fail.
1604 * bdrv_replace_child_noperm() therefore must call drained_end() on
1605 * the parent only if it really is still drained because the child is
1606 * drained.
1608 * If removing child from parent-node-2 was successful (as it should
1609 * be), test_drop_backing_job_commit() will then also remove the child
1610 * from parent-node-0.
1612 * With an old version of our drain infrastructure ((A) above), that
1613 * resulted in the following flow:
1615 * 1. child attempts to leave its drained section. The call recurses
1616 * to its parents.
1618 * 2. parent-node-2 leaves the drained section. Polling in
1619 * bdrv_drain_invoke() will schedule job_exit().
1621 * 3. parent-node-1 leaves the drained section. Polling in
1622 * bdrv_drain_invoke() will run job_exit(), thus disconnecting
1623 * parent-node-0 from the child node.
1625 * 4. bdrv_parent_drained_end() uses a QLIST_FOREACH_SAFE() loop to
1626 * iterate over the parents. Thus, it now accesses the BdrvChild
1627 * object that used to connect parent-node-0 and the child node.
1628 * However, that object no longer exists, so it accesses a dangling
1629 * pointer.
1631 * The solution is to only poll once when running a bdrv_drained_end()
1632 * operation, specifically at the end when all drained_end()
1633 * operations for all involved nodes have been scheduled.
1634 * Note that this also solves (A) above, thus hiding (B).
1636 static void test_blockjob_commit_by_drained_end(void)
1638 BlockDriverState *bs_child, *bs_parents[3];
1639 TestDropBackingBlockJob *job;
1640 bool job_has_completed = false;
1641 int i;
1643 bs_child = bdrv_new_open_driver(&bdrv_test, "child-node", BDRV_O_RDWR,
1644 &error_abort);
1646 for (i = 0; i < 3; i++) {
1647 char name[32];
1648 snprintf(name, sizeof(name), "parent-node-%i", i);
1649 bs_parents[i] = bdrv_new_open_driver(&bdrv_test, name, BDRV_O_RDWR,
1650 &error_abort);
1651 bdrv_set_backing_hd(bs_parents[i], bs_child, &error_abort);
1654 job = block_job_create("job", &test_drop_backing_job_driver, NULL,
1655 bs_parents[2], 0, BLK_PERM_ALL, 0, 0, NULL, NULL,
1656 &error_abort);
1658 job->detach_also = bs_parents[0];
1659 job->did_complete = &job_has_completed;
1661 job_start(&job->common.job);
1663 job->should_complete = true;
1664 bdrv_drained_begin(bs_child);
1665 g_assert(!job_has_completed);
1666 bdrv_drained_end(bs_child);
1667 g_assert(job_has_completed);
1669 bdrv_unref(bs_parents[0]);
1670 bdrv_unref(bs_parents[1]);
1671 bdrv_unref(bs_parents[2]);
1672 bdrv_unref(bs_child);
1675 int main(int argc, char **argv)
1677 int ret;
1679 bdrv_init();
1680 qemu_init_main_loop(&error_abort);
1682 g_test_init(&argc, &argv, NULL);
1683 qemu_event_init(&done_event, false);
1685 g_test_add_func("/bdrv-drain/driver-cb/drain_all", test_drv_cb_drain_all);
1686 g_test_add_func("/bdrv-drain/driver-cb/drain", test_drv_cb_drain);
1687 g_test_add_func("/bdrv-drain/driver-cb/drain_subtree",
1688 test_drv_cb_drain_subtree);
1690 g_test_add_func("/bdrv-drain/driver-cb/co/drain_all",
1691 test_drv_cb_co_drain_all);
1692 g_test_add_func("/bdrv-drain/driver-cb/co/drain", test_drv_cb_co_drain);
1693 g_test_add_func("/bdrv-drain/driver-cb/co/drain_subtree",
1694 test_drv_cb_co_drain_subtree);
1697 g_test_add_func("/bdrv-drain/quiesce/drain_all", test_quiesce_drain_all);
1698 g_test_add_func("/bdrv-drain/quiesce/drain", test_quiesce_drain);
1699 g_test_add_func("/bdrv-drain/quiesce/drain_subtree",
1700 test_quiesce_drain_subtree);
1702 g_test_add_func("/bdrv-drain/quiesce/co/drain_all",
1703 test_quiesce_co_drain_all);
1704 g_test_add_func("/bdrv-drain/quiesce/co/drain", test_quiesce_co_drain);
1705 g_test_add_func("/bdrv-drain/quiesce/co/drain_subtree",
1706 test_quiesce_co_drain_subtree);
1708 g_test_add_func("/bdrv-drain/nested", test_nested);
1709 g_test_add_func("/bdrv-drain/multiparent", test_multiparent);
1711 g_test_add_func("/bdrv-drain/graph-change/drain_subtree",
1712 test_graph_change_drain_subtree);
1713 g_test_add_func("/bdrv-drain/graph-change/drain_all",
1714 test_graph_change_drain_all);
1716 g_test_add_func("/bdrv-drain/iothread/drain_all", test_iothread_drain_all);
1717 g_test_add_func("/bdrv-drain/iothread/drain", test_iothread_drain);
1718 g_test_add_func("/bdrv-drain/iothread/drain_subtree",
1719 test_iothread_drain_subtree);
1721 g_test_add_func("/bdrv-drain/blockjob/drain_all", test_blockjob_drain_all);
1722 g_test_add_func("/bdrv-drain/blockjob/drain", test_blockjob_drain);
1723 g_test_add_func("/bdrv-drain/blockjob/drain_subtree",
1724 test_blockjob_drain_subtree);
1726 g_test_add_func("/bdrv-drain/blockjob/error/drain_all",
1727 test_blockjob_error_drain_all);
1728 g_test_add_func("/bdrv-drain/blockjob/error/drain",
1729 test_blockjob_error_drain);
1730 g_test_add_func("/bdrv-drain/blockjob/error/drain_subtree",
1731 test_blockjob_error_drain_subtree);
1733 g_test_add_func("/bdrv-drain/blockjob/iothread/drain_all",
1734 test_blockjob_iothread_drain_all);
1735 g_test_add_func("/bdrv-drain/blockjob/iothread/drain",
1736 test_blockjob_iothread_drain);
1737 g_test_add_func("/bdrv-drain/blockjob/iothread/drain_subtree",
1738 test_blockjob_iothread_drain_subtree);
1740 g_test_add_func("/bdrv-drain/blockjob/iothread/error/drain_all",
1741 test_blockjob_iothread_error_drain_all);
1742 g_test_add_func("/bdrv-drain/blockjob/iothread/error/drain",
1743 test_blockjob_iothread_error_drain);
1744 g_test_add_func("/bdrv-drain/blockjob/iothread/error/drain_subtree",
1745 test_blockjob_iothread_error_drain_subtree);
1747 g_test_add_func("/bdrv-drain/deletion/drain", test_delete_by_drain);
1748 g_test_add_func("/bdrv-drain/detach/drain_all", test_detach_by_drain_all);
1749 g_test_add_func("/bdrv-drain/detach/drain", test_detach_by_drain);
1750 g_test_add_func("/bdrv-drain/detach/drain_subtree", test_detach_by_drain_subtree);
1751 g_test_add_func("/bdrv-drain/detach/parent_cb", test_detach_by_parent_cb);
1752 g_test_add_func("/bdrv-drain/detach/driver_cb", test_detach_by_driver_cb);
1754 g_test_add_func("/bdrv-drain/attach/drain", test_append_to_drained);
1756 g_test_add_func("/bdrv-drain/set_aio_context", test_set_aio_context);
1758 g_test_add_func("/bdrv-drain/blockjob/commit_by_drained_end",
1759 test_blockjob_commit_by_drained_end);
1761 ret = g_test_run();
1762 qemu_event_destroy(&done_event);
1763 return ret;