dma/xlnx-zdma: Remove redundant statement in zdma_write_dst()
[qemu/ar7.git] / tests / test-bdrv-drain.c
blobfa0e6a648b476da0fb7e9481039a893d73405395
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 int bdrv_test_change_backing_file(BlockDriverState *bs,
105 const char *backing_file,
106 const char *backing_fmt)
108 return 0;
111 static BlockDriver bdrv_test = {
112 .format_name = "test",
113 .instance_size = sizeof(BDRVTestState),
115 .bdrv_close = bdrv_test_close,
116 .bdrv_co_preadv = bdrv_test_co_preadv,
118 .bdrv_co_drain_begin = bdrv_test_co_drain_begin,
119 .bdrv_co_drain_end = bdrv_test_co_drain_end,
121 .bdrv_child_perm = bdrv_test_child_perm,
123 .bdrv_change_backing_file = bdrv_test_change_backing_file,
126 static void aio_ret_cb(void *opaque, int ret)
128 int *aio_ret = opaque;
129 *aio_ret = ret;
132 typedef struct CallInCoroutineData {
133 void (*entry)(void);
134 bool done;
135 } CallInCoroutineData;
137 static coroutine_fn void call_in_coroutine_entry(void *opaque)
139 CallInCoroutineData *data = opaque;
141 data->entry();
142 data->done = true;
145 static void call_in_coroutine(void (*entry)(void))
147 Coroutine *co;
148 CallInCoroutineData data = {
149 .entry = entry,
150 .done = false,
153 co = qemu_coroutine_create(call_in_coroutine_entry, &data);
154 qemu_coroutine_enter(co);
155 while (!data.done) {
156 aio_poll(qemu_get_aio_context(), true);
160 enum drain_type {
161 BDRV_DRAIN_ALL,
162 BDRV_DRAIN,
163 BDRV_SUBTREE_DRAIN,
164 DRAIN_TYPE_MAX,
167 static void do_drain_begin(enum drain_type drain_type, BlockDriverState *bs)
169 switch (drain_type) {
170 case BDRV_DRAIN_ALL: bdrv_drain_all_begin(); break;
171 case BDRV_DRAIN: bdrv_drained_begin(bs); break;
172 case BDRV_SUBTREE_DRAIN: bdrv_subtree_drained_begin(bs); break;
173 default: g_assert_not_reached();
177 static void do_drain_end(enum drain_type drain_type, BlockDriverState *bs)
179 switch (drain_type) {
180 case BDRV_DRAIN_ALL: bdrv_drain_all_end(); break;
181 case BDRV_DRAIN: bdrv_drained_end(bs); break;
182 case BDRV_SUBTREE_DRAIN: bdrv_subtree_drained_end(bs); break;
183 default: g_assert_not_reached();
187 static void do_drain_begin_unlocked(enum drain_type drain_type, BlockDriverState *bs)
189 if (drain_type != BDRV_DRAIN_ALL) {
190 aio_context_acquire(bdrv_get_aio_context(bs));
192 do_drain_begin(drain_type, bs);
193 if (drain_type != BDRV_DRAIN_ALL) {
194 aio_context_release(bdrv_get_aio_context(bs));
198 static void do_drain_end_unlocked(enum drain_type drain_type, BlockDriverState *bs)
200 if (drain_type != BDRV_DRAIN_ALL) {
201 aio_context_acquire(bdrv_get_aio_context(bs));
203 do_drain_end(drain_type, bs);
204 if (drain_type != BDRV_DRAIN_ALL) {
205 aio_context_release(bdrv_get_aio_context(bs));
209 static void test_drv_cb_common(enum drain_type drain_type, bool recursive)
211 BlockBackend *blk;
212 BlockDriverState *bs, *backing;
213 BDRVTestState *s, *backing_s;
214 BlockAIOCB *acb;
215 int aio_ret;
217 QEMUIOVector qiov = QEMU_IOVEC_INIT_BUF(qiov, NULL, 0);
219 blk = blk_new(qemu_get_aio_context(), BLK_PERM_ALL, BLK_PERM_ALL);
220 bs = bdrv_new_open_driver(&bdrv_test, "test-node", BDRV_O_RDWR,
221 &error_abort);
222 s = bs->opaque;
223 blk_insert_bs(blk, bs, &error_abort);
225 backing = bdrv_new_open_driver(&bdrv_test, "backing", 0, &error_abort);
226 backing_s = backing->opaque;
227 bdrv_set_backing_hd(bs, backing, &error_abort);
229 /* Simple bdrv_drain_all_begin/end pair, check that CBs are called */
230 g_assert_cmpint(s->drain_count, ==, 0);
231 g_assert_cmpint(backing_s->drain_count, ==, 0);
233 do_drain_begin(drain_type, bs);
235 g_assert_cmpint(s->drain_count, ==, 1);
236 g_assert_cmpint(backing_s->drain_count, ==, !!recursive);
238 do_drain_end(drain_type, bs);
240 g_assert_cmpint(s->drain_count, ==, 0);
241 g_assert_cmpint(backing_s->drain_count, ==, 0);
243 /* Now do the same while a request is pending */
244 aio_ret = -EINPROGRESS;
245 acb = blk_aio_preadv(blk, 0, &qiov, 0, aio_ret_cb, &aio_ret);
246 g_assert(acb != NULL);
247 g_assert_cmpint(aio_ret, ==, -EINPROGRESS);
249 g_assert_cmpint(s->drain_count, ==, 0);
250 g_assert_cmpint(backing_s->drain_count, ==, 0);
252 do_drain_begin(drain_type, bs);
254 g_assert_cmpint(aio_ret, ==, 0);
255 g_assert_cmpint(s->drain_count, ==, 1);
256 g_assert_cmpint(backing_s->drain_count, ==, !!recursive);
258 do_drain_end(drain_type, bs);
260 g_assert_cmpint(s->drain_count, ==, 0);
261 g_assert_cmpint(backing_s->drain_count, ==, 0);
263 bdrv_unref(backing);
264 bdrv_unref(bs);
265 blk_unref(blk);
268 static void test_drv_cb_drain_all(void)
270 test_drv_cb_common(BDRV_DRAIN_ALL, true);
273 static void test_drv_cb_drain(void)
275 test_drv_cb_common(BDRV_DRAIN, false);
278 static void test_drv_cb_drain_subtree(void)
280 test_drv_cb_common(BDRV_SUBTREE_DRAIN, true);
283 static void test_drv_cb_co_drain_all(void)
285 call_in_coroutine(test_drv_cb_drain_all);
288 static void test_drv_cb_co_drain(void)
290 call_in_coroutine(test_drv_cb_drain);
293 static void test_drv_cb_co_drain_subtree(void)
295 call_in_coroutine(test_drv_cb_drain_subtree);
298 static void test_quiesce_common(enum drain_type drain_type, bool recursive)
300 BlockBackend *blk;
301 BlockDriverState *bs, *backing;
303 blk = blk_new(qemu_get_aio_context(), BLK_PERM_ALL, BLK_PERM_ALL);
304 bs = bdrv_new_open_driver(&bdrv_test, "test-node", BDRV_O_RDWR,
305 &error_abort);
306 blk_insert_bs(blk, bs, &error_abort);
308 backing = bdrv_new_open_driver(&bdrv_test, "backing", 0, &error_abort);
309 bdrv_set_backing_hd(bs, backing, &error_abort);
311 g_assert_cmpint(bs->quiesce_counter, ==, 0);
312 g_assert_cmpint(backing->quiesce_counter, ==, 0);
314 do_drain_begin(drain_type, bs);
316 g_assert_cmpint(bs->quiesce_counter, ==, 1);
317 g_assert_cmpint(backing->quiesce_counter, ==, !!recursive);
319 do_drain_end(drain_type, bs);
321 g_assert_cmpint(bs->quiesce_counter, ==, 0);
322 g_assert_cmpint(backing->quiesce_counter, ==, 0);
324 bdrv_unref(backing);
325 bdrv_unref(bs);
326 blk_unref(blk);
329 static void test_quiesce_drain_all(void)
331 test_quiesce_common(BDRV_DRAIN_ALL, true);
334 static void test_quiesce_drain(void)
336 test_quiesce_common(BDRV_DRAIN, false);
339 static void test_quiesce_drain_subtree(void)
341 test_quiesce_common(BDRV_SUBTREE_DRAIN, true);
344 static void test_quiesce_co_drain_all(void)
346 call_in_coroutine(test_quiesce_drain_all);
349 static void test_quiesce_co_drain(void)
351 call_in_coroutine(test_quiesce_drain);
354 static void test_quiesce_co_drain_subtree(void)
356 call_in_coroutine(test_quiesce_drain_subtree);
359 static void test_nested(void)
361 BlockBackend *blk;
362 BlockDriverState *bs, *backing;
363 BDRVTestState *s, *backing_s;
364 enum drain_type outer, inner;
366 blk = blk_new(qemu_get_aio_context(), BLK_PERM_ALL, BLK_PERM_ALL);
367 bs = bdrv_new_open_driver(&bdrv_test, "test-node", BDRV_O_RDWR,
368 &error_abort);
369 s = bs->opaque;
370 blk_insert_bs(blk, bs, &error_abort);
372 backing = bdrv_new_open_driver(&bdrv_test, "backing", 0, &error_abort);
373 backing_s = backing->opaque;
374 bdrv_set_backing_hd(bs, backing, &error_abort);
376 for (outer = 0; outer < DRAIN_TYPE_MAX; outer++) {
377 for (inner = 0; inner < DRAIN_TYPE_MAX; inner++) {
378 int backing_quiesce = (outer != BDRV_DRAIN) +
379 (inner != BDRV_DRAIN);
381 g_assert_cmpint(bs->quiesce_counter, ==, 0);
382 g_assert_cmpint(backing->quiesce_counter, ==, 0);
383 g_assert_cmpint(s->drain_count, ==, 0);
384 g_assert_cmpint(backing_s->drain_count, ==, 0);
386 do_drain_begin(outer, bs);
387 do_drain_begin(inner, bs);
389 g_assert_cmpint(bs->quiesce_counter, ==, 2);
390 g_assert_cmpint(backing->quiesce_counter, ==, backing_quiesce);
391 g_assert_cmpint(s->drain_count, ==, 2);
392 g_assert_cmpint(backing_s->drain_count, ==, backing_quiesce);
394 do_drain_end(inner, bs);
395 do_drain_end(outer, bs);
397 g_assert_cmpint(bs->quiesce_counter, ==, 0);
398 g_assert_cmpint(backing->quiesce_counter, ==, 0);
399 g_assert_cmpint(s->drain_count, ==, 0);
400 g_assert_cmpint(backing_s->drain_count, ==, 0);
404 bdrv_unref(backing);
405 bdrv_unref(bs);
406 blk_unref(blk);
409 static void test_multiparent(void)
411 BlockBackend *blk_a, *blk_b;
412 BlockDriverState *bs_a, *bs_b, *backing;
413 BDRVTestState *a_s, *b_s, *backing_s;
415 blk_a = blk_new(qemu_get_aio_context(), BLK_PERM_ALL, BLK_PERM_ALL);
416 bs_a = bdrv_new_open_driver(&bdrv_test, "test-node-a", BDRV_O_RDWR,
417 &error_abort);
418 a_s = bs_a->opaque;
419 blk_insert_bs(blk_a, bs_a, &error_abort);
421 blk_b = blk_new(qemu_get_aio_context(), BLK_PERM_ALL, BLK_PERM_ALL);
422 bs_b = bdrv_new_open_driver(&bdrv_test, "test-node-b", BDRV_O_RDWR,
423 &error_abort);
424 b_s = bs_b->opaque;
425 blk_insert_bs(blk_b, bs_b, &error_abort);
427 backing = bdrv_new_open_driver(&bdrv_test, "backing", 0, &error_abort);
428 backing_s = backing->opaque;
429 bdrv_set_backing_hd(bs_a, backing, &error_abort);
430 bdrv_set_backing_hd(bs_b, backing, &error_abort);
432 g_assert_cmpint(bs_a->quiesce_counter, ==, 0);
433 g_assert_cmpint(bs_b->quiesce_counter, ==, 0);
434 g_assert_cmpint(backing->quiesce_counter, ==, 0);
435 g_assert_cmpint(a_s->drain_count, ==, 0);
436 g_assert_cmpint(b_s->drain_count, ==, 0);
437 g_assert_cmpint(backing_s->drain_count, ==, 0);
439 do_drain_begin(BDRV_SUBTREE_DRAIN, bs_a);
441 g_assert_cmpint(bs_a->quiesce_counter, ==, 1);
442 g_assert_cmpint(bs_b->quiesce_counter, ==, 1);
443 g_assert_cmpint(backing->quiesce_counter, ==, 1);
444 g_assert_cmpint(a_s->drain_count, ==, 1);
445 g_assert_cmpint(b_s->drain_count, ==, 1);
446 g_assert_cmpint(backing_s->drain_count, ==, 1);
448 do_drain_begin(BDRV_SUBTREE_DRAIN, bs_b);
450 g_assert_cmpint(bs_a->quiesce_counter, ==, 2);
451 g_assert_cmpint(bs_b->quiesce_counter, ==, 2);
452 g_assert_cmpint(backing->quiesce_counter, ==, 2);
453 g_assert_cmpint(a_s->drain_count, ==, 2);
454 g_assert_cmpint(b_s->drain_count, ==, 2);
455 g_assert_cmpint(backing_s->drain_count, ==, 2);
457 do_drain_end(BDRV_SUBTREE_DRAIN, bs_b);
459 g_assert_cmpint(bs_a->quiesce_counter, ==, 1);
460 g_assert_cmpint(bs_b->quiesce_counter, ==, 1);
461 g_assert_cmpint(backing->quiesce_counter, ==, 1);
462 g_assert_cmpint(a_s->drain_count, ==, 1);
463 g_assert_cmpint(b_s->drain_count, ==, 1);
464 g_assert_cmpint(backing_s->drain_count, ==, 1);
466 do_drain_end(BDRV_SUBTREE_DRAIN, bs_a);
468 g_assert_cmpint(bs_a->quiesce_counter, ==, 0);
469 g_assert_cmpint(bs_b->quiesce_counter, ==, 0);
470 g_assert_cmpint(backing->quiesce_counter, ==, 0);
471 g_assert_cmpint(a_s->drain_count, ==, 0);
472 g_assert_cmpint(b_s->drain_count, ==, 0);
473 g_assert_cmpint(backing_s->drain_count, ==, 0);
475 bdrv_unref(backing);
476 bdrv_unref(bs_a);
477 bdrv_unref(bs_b);
478 blk_unref(blk_a);
479 blk_unref(blk_b);
482 static void test_graph_change_drain_subtree(void)
484 BlockBackend *blk_a, *blk_b;
485 BlockDriverState *bs_a, *bs_b, *backing;
486 BDRVTestState *a_s, *b_s, *backing_s;
488 blk_a = blk_new(qemu_get_aio_context(), BLK_PERM_ALL, BLK_PERM_ALL);
489 bs_a = bdrv_new_open_driver(&bdrv_test, "test-node-a", BDRV_O_RDWR,
490 &error_abort);
491 a_s = bs_a->opaque;
492 blk_insert_bs(blk_a, bs_a, &error_abort);
494 blk_b = blk_new(qemu_get_aio_context(), BLK_PERM_ALL, BLK_PERM_ALL);
495 bs_b = bdrv_new_open_driver(&bdrv_test, "test-node-b", BDRV_O_RDWR,
496 &error_abort);
497 b_s = bs_b->opaque;
498 blk_insert_bs(blk_b, bs_b, &error_abort);
500 backing = bdrv_new_open_driver(&bdrv_test, "backing", 0, &error_abort);
501 backing_s = backing->opaque;
502 bdrv_set_backing_hd(bs_a, backing, &error_abort);
504 g_assert_cmpint(bs_a->quiesce_counter, ==, 0);
505 g_assert_cmpint(bs_b->quiesce_counter, ==, 0);
506 g_assert_cmpint(backing->quiesce_counter, ==, 0);
507 g_assert_cmpint(a_s->drain_count, ==, 0);
508 g_assert_cmpint(b_s->drain_count, ==, 0);
509 g_assert_cmpint(backing_s->drain_count, ==, 0);
511 do_drain_begin(BDRV_SUBTREE_DRAIN, bs_a);
512 do_drain_begin(BDRV_SUBTREE_DRAIN, bs_a);
513 do_drain_begin(BDRV_SUBTREE_DRAIN, bs_a);
514 do_drain_begin(BDRV_SUBTREE_DRAIN, bs_b);
515 do_drain_begin(BDRV_SUBTREE_DRAIN, bs_b);
517 bdrv_set_backing_hd(bs_b, backing, &error_abort);
518 g_assert_cmpint(bs_a->quiesce_counter, ==, 5);
519 g_assert_cmpint(bs_b->quiesce_counter, ==, 5);
520 g_assert_cmpint(backing->quiesce_counter, ==, 5);
521 g_assert_cmpint(a_s->drain_count, ==, 5);
522 g_assert_cmpint(b_s->drain_count, ==, 5);
523 g_assert_cmpint(backing_s->drain_count, ==, 5);
525 bdrv_set_backing_hd(bs_b, NULL, &error_abort);
526 g_assert_cmpint(bs_a->quiesce_counter, ==, 3);
527 g_assert_cmpint(bs_b->quiesce_counter, ==, 2);
528 g_assert_cmpint(backing->quiesce_counter, ==, 3);
529 g_assert_cmpint(a_s->drain_count, ==, 3);
530 g_assert_cmpint(b_s->drain_count, ==, 2);
531 g_assert_cmpint(backing_s->drain_count, ==, 3);
533 bdrv_set_backing_hd(bs_b, backing, &error_abort);
534 g_assert_cmpint(bs_a->quiesce_counter, ==, 5);
535 g_assert_cmpint(bs_b->quiesce_counter, ==, 5);
536 g_assert_cmpint(backing->quiesce_counter, ==, 5);
537 g_assert_cmpint(a_s->drain_count, ==, 5);
538 g_assert_cmpint(b_s->drain_count, ==, 5);
539 g_assert_cmpint(backing_s->drain_count, ==, 5);
541 do_drain_end(BDRV_SUBTREE_DRAIN, bs_b);
542 do_drain_end(BDRV_SUBTREE_DRAIN, bs_b);
543 do_drain_end(BDRV_SUBTREE_DRAIN, bs_a);
544 do_drain_end(BDRV_SUBTREE_DRAIN, bs_a);
545 do_drain_end(BDRV_SUBTREE_DRAIN, bs_a);
547 g_assert_cmpint(bs_a->quiesce_counter, ==, 0);
548 g_assert_cmpint(bs_b->quiesce_counter, ==, 0);
549 g_assert_cmpint(backing->quiesce_counter, ==, 0);
550 g_assert_cmpint(a_s->drain_count, ==, 0);
551 g_assert_cmpint(b_s->drain_count, ==, 0);
552 g_assert_cmpint(backing_s->drain_count, ==, 0);
554 bdrv_unref(backing);
555 bdrv_unref(bs_a);
556 bdrv_unref(bs_b);
557 blk_unref(blk_a);
558 blk_unref(blk_b);
561 static void test_graph_change_drain_all(void)
563 BlockBackend *blk_a, *blk_b;
564 BlockDriverState *bs_a, *bs_b;
565 BDRVTestState *a_s, *b_s;
567 /* Create node A with a BlockBackend */
568 blk_a = blk_new(qemu_get_aio_context(), BLK_PERM_ALL, BLK_PERM_ALL);
569 bs_a = bdrv_new_open_driver(&bdrv_test, "test-node-a", BDRV_O_RDWR,
570 &error_abort);
571 a_s = bs_a->opaque;
572 blk_insert_bs(blk_a, bs_a, &error_abort);
574 g_assert_cmpint(bs_a->quiesce_counter, ==, 0);
575 g_assert_cmpint(a_s->drain_count, ==, 0);
577 /* Call bdrv_drain_all_begin() */
578 bdrv_drain_all_begin();
580 g_assert_cmpint(bs_a->quiesce_counter, ==, 1);
581 g_assert_cmpint(a_s->drain_count, ==, 1);
583 /* Create node B with a BlockBackend */
584 blk_b = blk_new(qemu_get_aio_context(), BLK_PERM_ALL, BLK_PERM_ALL);
585 bs_b = bdrv_new_open_driver(&bdrv_test, "test-node-b", BDRV_O_RDWR,
586 &error_abort);
587 b_s = bs_b->opaque;
588 blk_insert_bs(blk_b, bs_b, &error_abort);
590 g_assert_cmpint(bs_a->quiesce_counter, ==, 1);
591 g_assert_cmpint(bs_b->quiesce_counter, ==, 1);
592 g_assert_cmpint(a_s->drain_count, ==, 1);
593 g_assert_cmpint(b_s->drain_count, ==, 1);
595 /* Unref and finally delete node A */
596 blk_unref(blk_a);
598 g_assert_cmpint(bs_a->quiesce_counter, ==, 1);
599 g_assert_cmpint(bs_b->quiesce_counter, ==, 1);
600 g_assert_cmpint(a_s->drain_count, ==, 1);
601 g_assert_cmpint(b_s->drain_count, ==, 1);
603 bdrv_unref(bs_a);
605 g_assert_cmpint(bs_b->quiesce_counter, ==, 1);
606 g_assert_cmpint(b_s->drain_count, ==, 1);
608 /* End the drained section */
609 bdrv_drain_all_end();
611 g_assert_cmpint(bs_b->quiesce_counter, ==, 0);
612 g_assert_cmpint(b_s->drain_count, ==, 0);
614 bdrv_unref(bs_b);
615 blk_unref(blk_b);
618 struct test_iothread_data {
619 BlockDriverState *bs;
620 enum drain_type drain_type;
621 int *aio_ret;
624 static void test_iothread_drain_entry(void *opaque)
626 struct test_iothread_data *data = opaque;
628 aio_context_acquire(bdrv_get_aio_context(data->bs));
629 do_drain_begin(data->drain_type, data->bs);
630 g_assert_cmpint(*data->aio_ret, ==, 0);
631 do_drain_end(data->drain_type, data->bs);
632 aio_context_release(bdrv_get_aio_context(data->bs));
634 qemu_event_set(&done_event);
637 static void test_iothread_aio_cb(void *opaque, int ret)
639 int *aio_ret = opaque;
640 *aio_ret = ret;
641 qemu_event_set(&done_event);
644 static void test_iothread_main_thread_bh(void *opaque)
646 struct test_iothread_data *data = opaque;
648 /* Test that the AioContext is not yet locked in a random BH that is
649 * executed during drain, otherwise this would deadlock. */
650 aio_context_acquire(bdrv_get_aio_context(data->bs));
651 bdrv_flush(data->bs);
652 aio_context_release(bdrv_get_aio_context(data->bs));
656 * Starts an AIO request on a BDS that runs in the AioContext of iothread 1.
657 * The request involves a BH on iothread 2 before it can complete.
659 * @drain_thread = 0 means that do_drain_begin/end are called from the main
660 * thread, @drain_thread = 1 means that they are called from iothread 1. Drain
661 * for this BDS cannot be called from iothread 2 because only the main thread
662 * may do cross-AioContext polling.
664 static void test_iothread_common(enum drain_type drain_type, int drain_thread)
666 BlockBackend *blk;
667 BlockDriverState *bs;
668 BDRVTestState *s;
669 BlockAIOCB *acb;
670 int aio_ret;
671 struct test_iothread_data data;
673 IOThread *a = iothread_new();
674 IOThread *b = iothread_new();
675 AioContext *ctx_a = iothread_get_aio_context(a);
676 AioContext *ctx_b = iothread_get_aio_context(b);
678 QEMUIOVector qiov = QEMU_IOVEC_INIT_BUF(qiov, NULL, 0);
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(qemu_get_aio_context(), 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);
690 blk_set_disable_request_queuing(blk, true);
692 blk_set_aio_context(blk, ctx_a, &error_abort);
693 aio_context_acquire(ctx_a);
695 s->bh_indirection_ctx = ctx_b;
697 aio_ret = -EINPROGRESS;
698 qemu_event_reset(&done_event);
700 if (drain_thread == 0) {
701 acb = blk_aio_preadv(blk, 0, &qiov, 0, test_iothread_aio_cb, &aio_ret);
702 } else {
703 acb = blk_aio_preadv(blk, 0, &qiov, 0, aio_ret_cb, &aio_ret);
705 g_assert(acb != NULL);
706 g_assert_cmpint(aio_ret, ==, -EINPROGRESS);
708 aio_context_release(ctx_a);
710 data = (struct test_iothread_data) {
711 .bs = bs,
712 .drain_type = drain_type,
713 .aio_ret = &aio_ret,
716 switch (drain_thread) {
717 case 0:
718 if (drain_type != BDRV_DRAIN_ALL) {
719 aio_context_acquire(ctx_a);
722 aio_bh_schedule_oneshot(ctx_a, test_iothread_main_thread_bh, &data);
724 /* The request is running on the IOThread a. Draining its block device
725 * will make sure that it has completed as far as the BDS is concerned,
726 * but the drain in this thread can continue immediately after
727 * bdrv_dec_in_flight() and aio_ret might be assigned only slightly
728 * later. */
729 do_drain_begin(drain_type, bs);
730 g_assert_cmpint(bs->in_flight, ==, 0);
732 if (drain_type != BDRV_DRAIN_ALL) {
733 aio_context_release(ctx_a);
735 qemu_event_wait(&done_event);
736 if (drain_type != BDRV_DRAIN_ALL) {
737 aio_context_acquire(ctx_a);
740 g_assert_cmpint(aio_ret, ==, 0);
741 do_drain_end(drain_type, bs);
743 if (drain_type != BDRV_DRAIN_ALL) {
744 aio_context_release(ctx_a);
746 break;
747 case 1:
748 aio_bh_schedule_oneshot(ctx_a, test_iothread_drain_entry, &data);
749 qemu_event_wait(&done_event);
750 break;
751 default:
752 g_assert_not_reached();
755 aio_context_acquire(ctx_a);
756 blk_set_aio_context(blk, qemu_get_aio_context(), &error_abort);
757 aio_context_release(ctx_a);
759 bdrv_unref(bs);
760 blk_unref(blk);
762 out:
763 iothread_join(a);
764 iothread_join(b);
767 static void test_iothread_drain_all(void)
769 test_iothread_common(BDRV_DRAIN_ALL, 0);
770 test_iothread_common(BDRV_DRAIN_ALL, 1);
773 static void test_iothread_drain(void)
775 test_iothread_common(BDRV_DRAIN, 0);
776 test_iothread_common(BDRV_DRAIN, 1);
779 static void test_iothread_drain_subtree(void)
781 test_iothread_common(BDRV_SUBTREE_DRAIN, 0);
782 test_iothread_common(BDRV_SUBTREE_DRAIN, 1);
786 typedef struct TestBlockJob {
787 BlockJob common;
788 int run_ret;
789 int prepare_ret;
790 bool running;
791 bool should_complete;
792 } TestBlockJob;
794 static int test_job_prepare(Job *job)
796 TestBlockJob *s = container_of(job, TestBlockJob, common.job);
798 /* Provoke an AIO_WAIT_WHILE() call to verify there is no deadlock */
799 blk_flush(s->common.blk);
800 return s->prepare_ret;
803 static void test_job_commit(Job *job)
805 TestBlockJob *s = container_of(job, TestBlockJob, common.job);
807 /* Provoke an AIO_WAIT_WHILE() call to verify there is no deadlock */
808 blk_flush(s->common.blk);
811 static void test_job_abort(Job *job)
813 TestBlockJob *s = container_of(job, TestBlockJob, common.job);
815 /* Provoke an AIO_WAIT_WHILE() call to verify there is no deadlock */
816 blk_flush(s->common.blk);
819 static int coroutine_fn test_job_run(Job *job, Error **errp)
821 TestBlockJob *s = container_of(job, TestBlockJob, common.job);
823 /* We are running the actual job code past the pause point in
824 * job_co_entry(). */
825 s->running = true;
827 job_transition_to_ready(&s->common.job);
828 while (!s->should_complete) {
829 /* Avoid job_sleep_ns() because it marks the job as !busy. We want to
830 * emulate some actual activity (probably some I/O) here so that drain
831 * has to wait for this activity to stop. */
832 qemu_co_sleep_ns(QEMU_CLOCK_REALTIME, 1000000);
834 job_pause_point(&s->common.job);
837 return s->run_ret;
840 static void test_job_complete(Job *job, Error **errp)
842 TestBlockJob *s = container_of(job, TestBlockJob, common.job);
843 s->should_complete = true;
846 BlockJobDriver test_job_driver = {
847 .job_driver = {
848 .instance_size = sizeof(TestBlockJob),
849 .free = block_job_free,
850 .user_resume = block_job_user_resume,
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(qemu_get_aio_context(), 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, &error_abort);
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(qemu_get_aio_context(), BLK_PERM_ALL, BLK_PERM_ALL);
924 blk_insert_bs(blk_target, target, &error_abort);
925 blk_set_allow_aio_context_change(blk_target, true);
927 aio_context_acquire(ctx);
928 tjob = block_job_create("job0", &test_job_driver, NULL, src,
929 0, BLK_PERM_ALL,
930 0, 0, NULL, NULL, &error_abort);
931 job = &tjob->common;
932 block_job_add_bdrv(job, "target", target, 0, BLK_PERM_ALL, &error_abort);
934 switch (result) {
935 case TEST_JOB_SUCCESS:
936 break;
937 case TEST_JOB_FAIL_RUN:
938 tjob->run_ret = -EIO;
939 break;
940 case TEST_JOB_FAIL_PREPARE:
941 tjob->prepare_ret = -EIO;
942 break;
945 job_start(&job->job);
946 aio_context_release(ctx);
948 if (use_iothread) {
949 /* job_co_entry() is run in the I/O thread, wait for the actual job
950 * code to start (we don't want to catch the job in the pause point in
951 * job_co_entry(). */
952 while (!tjob->running) {
953 aio_poll(qemu_get_aio_context(), false);
957 g_assert_cmpint(job->job.pause_count, ==, 0);
958 g_assert_false(job->job.paused);
959 g_assert_true(tjob->running);
960 g_assert_true(job->job.busy); /* We're in qemu_co_sleep_ns() */
962 do_drain_begin_unlocked(drain_type, drain_bs);
964 if (drain_type == BDRV_DRAIN_ALL) {
965 /* bdrv_drain_all() drains both src and target */
966 g_assert_cmpint(job->job.pause_count, ==, 2);
967 } else {
968 g_assert_cmpint(job->job.pause_count, ==, 1);
970 g_assert_true(job->job.paused);
971 g_assert_false(job->job.busy); /* The job is paused */
973 do_drain_end_unlocked(drain_type, drain_bs);
975 if (use_iothread) {
976 /* paused is reset in the I/O thread, wait for it */
977 while (job->job.paused) {
978 aio_poll(qemu_get_aio_context(), false);
982 g_assert_cmpint(job->job.pause_count, ==, 0);
983 g_assert_false(job->job.paused);
984 g_assert_true(job->job.busy); /* We're in qemu_co_sleep_ns() */
986 do_drain_begin_unlocked(drain_type, target);
988 if (drain_type == BDRV_DRAIN_ALL) {
989 /* bdrv_drain_all() drains both src and target */
990 g_assert_cmpint(job->job.pause_count, ==, 2);
991 } else {
992 g_assert_cmpint(job->job.pause_count, ==, 1);
994 g_assert_true(job->job.paused);
995 g_assert_false(job->job.busy); /* The job is paused */
997 do_drain_end_unlocked(drain_type, target);
999 if (use_iothread) {
1000 /* paused is reset in the I/O thread, wait for it */
1001 while (job->job.paused) {
1002 aio_poll(qemu_get_aio_context(), false);
1006 g_assert_cmpint(job->job.pause_count, ==, 0);
1007 g_assert_false(job->job.paused);
1008 g_assert_true(job->job.busy); /* We're in qemu_co_sleep_ns() */
1010 aio_context_acquire(ctx);
1011 ret = job_complete_sync(&job->job, &error_abort);
1012 g_assert_cmpint(ret, ==, (result == TEST_JOB_SUCCESS ? 0 : -EIO));
1014 if (use_iothread) {
1015 blk_set_aio_context(blk_src, qemu_get_aio_context(), &error_abort);
1016 assert(blk_get_aio_context(blk_target) == qemu_get_aio_context());
1018 aio_context_release(ctx);
1020 blk_unref(blk_src);
1021 blk_unref(blk_target);
1022 bdrv_unref(src_overlay);
1023 bdrv_unref(target);
1025 if (iothread) {
1026 iothread_join(iothread);
1030 static void test_blockjob_common(enum drain_type drain_type, bool use_iothread,
1031 enum test_job_result result)
1033 test_blockjob_common_drain_node(drain_type, use_iothread, result,
1034 TEST_JOB_DRAIN_SRC);
1035 test_blockjob_common_drain_node(drain_type, use_iothread, result,
1036 TEST_JOB_DRAIN_SRC_CHILD);
1037 if (drain_type == BDRV_SUBTREE_DRAIN) {
1038 test_blockjob_common_drain_node(drain_type, use_iothread, result,
1039 TEST_JOB_DRAIN_SRC_PARENT);
1043 static void test_blockjob_drain_all(void)
1045 test_blockjob_common(BDRV_DRAIN_ALL, false, TEST_JOB_SUCCESS);
1048 static void test_blockjob_drain(void)
1050 test_blockjob_common(BDRV_DRAIN, false, TEST_JOB_SUCCESS);
1053 static void test_blockjob_drain_subtree(void)
1055 test_blockjob_common(BDRV_SUBTREE_DRAIN, false, TEST_JOB_SUCCESS);
1058 static void test_blockjob_error_drain_all(void)
1060 test_blockjob_common(BDRV_DRAIN_ALL, false, TEST_JOB_FAIL_RUN);
1061 test_blockjob_common(BDRV_DRAIN_ALL, false, TEST_JOB_FAIL_PREPARE);
1064 static void test_blockjob_error_drain(void)
1066 test_blockjob_common(BDRV_DRAIN, false, TEST_JOB_FAIL_RUN);
1067 test_blockjob_common(BDRV_DRAIN, false, TEST_JOB_FAIL_PREPARE);
1070 static void test_blockjob_error_drain_subtree(void)
1072 test_blockjob_common(BDRV_SUBTREE_DRAIN, false, TEST_JOB_FAIL_RUN);
1073 test_blockjob_common(BDRV_SUBTREE_DRAIN, false, TEST_JOB_FAIL_PREPARE);
1076 static void test_blockjob_iothread_drain_all(void)
1078 test_blockjob_common(BDRV_DRAIN_ALL, true, TEST_JOB_SUCCESS);
1081 static void test_blockjob_iothread_drain(void)
1083 test_blockjob_common(BDRV_DRAIN, true, TEST_JOB_SUCCESS);
1086 static void test_blockjob_iothread_drain_subtree(void)
1088 test_blockjob_common(BDRV_SUBTREE_DRAIN, true, TEST_JOB_SUCCESS);
1091 static void test_blockjob_iothread_error_drain_all(void)
1093 test_blockjob_common(BDRV_DRAIN_ALL, true, TEST_JOB_FAIL_RUN);
1094 test_blockjob_common(BDRV_DRAIN_ALL, true, TEST_JOB_FAIL_PREPARE);
1097 static void test_blockjob_iothread_error_drain(void)
1099 test_blockjob_common(BDRV_DRAIN, true, TEST_JOB_FAIL_RUN);
1100 test_blockjob_common(BDRV_DRAIN, true, TEST_JOB_FAIL_PREPARE);
1103 static void test_blockjob_iothread_error_drain_subtree(void)
1105 test_blockjob_common(BDRV_SUBTREE_DRAIN, true, TEST_JOB_FAIL_RUN);
1106 test_blockjob_common(BDRV_SUBTREE_DRAIN, true, TEST_JOB_FAIL_PREPARE);
1110 typedef struct BDRVTestTopState {
1111 BdrvChild *wait_child;
1112 } BDRVTestTopState;
1114 static void bdrv_test_top_close(BlockDriverState *bs)
1116 BdrvChild *c, *next_c;
1117 QLIST_FOREACH_SAFE(c, &bs->children, next, next_c) {
1118 bdrv_unref_child(bs, c);
1122 static int coroutine_fn bdrv_test_top_co_preadv(BlockDriverState *bs,
1123 uint64_t offset, uint64_t bytes,
1124 QEMUIOVector *qiov, int flags)
1126 BDRVTestTopState *tts = bs->opaque;
1127 return bdrv_co_preadv(tts->wait_child, offset, bytes, qiov, flags);
1130 static BlockDriver bdrv_test_top_driver = {
1131 .format_name = "test_top_driver",
1132 .instance_size = sizeof(BDRVTestTopState),
1134 .bdrv_close = bdrv_test_top_close,
1135 .bdrv_co_preadv = bdrv_test_top_co_preadv,
1137 .bdrv_child_perm = bdrv_format_default_perms,
1140 typedef struct TestCoDeleteByDrainData {
1141 BlockBackend *blk;
1142 bool detach_instead_of_delete;
1143 bool done;
1144 } TestCoDeleteByDrainData;
1146 static void coroutine_fn test_co_delete_by_drain(void *opaque)
1148 TestCoDeleteByDrainData *dbdd = opaque;
1149 BlockBackend *blk = dbdd->blk;
1150 BlockDriverState *bs = blk_bs(blk);
1151 BDRVTestTopState *tts = bs->opaque;
1152 void *buffer = g_malloc(65536);
1153 QEMUIOVector qiov = QEMU_IOVEC_INIT_BUF(qiov, buffer, 65536);
1155 /* Pretend some internal write operation from parent to child.
1156 * Important: We have to read from the child, not from the parent!
1157 * Draining works by first propagating it all up the tree to the
1158 * root and then waiting for drainage from root to the leaves
1159 * (protocol nodes). If we have a request waiting on the root,
1160 * everything will be drained before we go back down the tree, but
1161 * we do not want that. We want to be in the middle of draining
1162 * when this following requests returns. */
1163 bdrv_co_preadv(tts->wait_child, 0, 65536, &qiov, 0);
1165 g_assert_cmpint(bs->refcnt, ==, 1);
1167 if (!dbdd->detach_instead_of_delete) {
1168 blk_unref(blk);
1169 } else {
1170 BdrvChild *c, *next_c;
1171 QLIST_FOREACH_SAFE(c, &bs->children, next, next_c) {
1172 bdrv_unref_child(bs, c);
1176 dbdd->done = true;
1177 g_free(buffer);
1181 * Test what happens when some BDS has some children, you drain one of
1182 * them and this results in the BDS being deleted.
1184 * If @detach_instead_of_delete is set, the BDS is not going to be
1185 * deleted but will only detach all of its children.
1187 static void do_test_delete_by_drain(bool detach_instead_of_delete,
1188 enum drain_type drain_type)
1190 BlockBackend *blk;
1191 BlockDriverState *bs, *child_bs, *null_bs;
1192 BDRVTestTopState *tts;
1193 TestCoDeleteByDrainData dbdd;
1194 Coroutine *co;
1196 bs = bdrv_new_open_driver(&bdrv_test_top_driver, "top", BDRV_O_RDWR,
1197 &error_abort);
1198 bs->total_sectors = 65536 >> BDRV_SECTOR_BITS;
1199 tts = bs->opaque;
1201 null_bs = bdrv_open("null-co://", NULL, NULL, BDRV_O_RDWR | BDRV_O_PROTOCOL,
1202 &error_abort);
1203 bdrv_attach_child(bs, null_bs, "null-child", &child_file, &error_abort);
1205 /* This child will be the one to pass to requests through to, and
1206 * it will stall until a drain occurs */
1207 child_bs = bdrv_new_open_driver(&bdrv_test, "child", BDRV_O_RDWR,
1208 &error_abort);
1209 child_bs->total_sectors = 65536 >> BDRV_SECTOR_BITS;
1210 /* Takes our reference to child_bs */
1211 tts->wait_child = bdrv_attach_child(bs, child_bs, "wait-child", &child_file,
1212 &error_abort);
1214 /* This child is just there to be deleted
1215 * (for detach_instead_of_delete == true) */
1216 null_bs = bdrv_open("null-co://", NULL, NULL, BDRV_O_RDWR | BDRV_O_PROTOCOL,
1217 &error_abort);
1218 bdrv_attach_child(bs, null_bs, "null-child", &child_file, &error_abort);
1220 blk = blk_new(qemu_get_aio_context(), BLK_PERM_ALL, BLK_PERM_ALL);
1221 blk_insert_bs(blk, bs, &error_abort);
1223 /* Referenced by blk now */
1224 bdrv_unref(bs);
1226 g_assert_cmpint(bs->refcnt, ==, 1);
1227 g_assert_cmpint(child_bs->refcnt, ==, 1);
1228 g_assert_cmpint(null_bs->refcnt, ==, 1);
1231 dbdd = (TestCoDeleteByDrainData){
1232 .blk = blk,
1233 .detach_instead_of_delete = detach_instead_of_delete,
1234 .done = false,
1236 co = qemu_coroutine_create(test_co_delete_by_drain, &dbdd);
1237 qemu_coroutine_enter(co);
1239 /* Drain the child while the read operation is still pending.
1240 * This should result in the operation finishing and
1241 * test_co_delete_by_drain() resuming. Thus, @bs will be deleted
1242 * and the coroutine will exit while this drain operation is still
1243 * in progress. */
1244 switch (drain_type) {
1245 case BDRV_DRAIN:
1246 bdrv_ref(child_bs);
1247 bdrv_drain(child_bs);
1248 bdrv_unref(child_bs);
1249 break;
1250 case BDRV_SUBTREE_DRAIN:
1251 /* Would have to ref/unref bs here for !detach_instead_of_delete, but
1252 * then the whole test becomes pointless because the graph changes
1253 * don't occur during the drain any more. */
1254 assert(detach_instead_of_delete);
1255 bdrv_subtree_drained_begin(bs);
1256 bdrv_subtree_drained_end(bs);
1257 break;
1258 case BDRV_DRAIN_ALL:
1259 bdrv_drain_all_begin();
1260 bdrv_drain_all_end();
1261 break;
1262 default:
1263 g_assert_not_reached();
1266 while (!dbdd.done) {
1267 aio_poll(qemu_get_aio_context(), true);
1270 if (detach_instead_of_delete) {
1271 /* Here, the reference has not passed over to the coroutine,
1272 * so we have to delete the BB ourselves */
1273 blk_unref(blk);
1277 static void test_delete_by_drain(void)
1279 do_test_delete_by_drain(false, BDRV_DRAIN);
1282 static void test_detach_by_drain_all(void)
1284 do_test_delete_by_drain(true, BDRV_DRAIN_ALL);
1287 static void test_detach_by_drain(void)
1289 do_test_delete_by_drain(true, BDRV_DRAIN);
1292 static void test_detach_by_drain_subtree(void)
1294 do_test_delete_by_drain(true, BDRV_SUBTREE_DRAIN);
1298 struct detach_by_parent_data {
1299 BlockDriverState *parent_b;
1300 BdrvChild *child_b;
1301 BlockDriverState *c;
1302 BdrvChild *child_c;
1303 bool by_parent_cb;
1305 static struct detach_by_parent_data detach_by_parent_data;
1307 static void detach_indirect_bh(void *opaque)
1309 struct detach_by_parent_data *data = opaque;
1311 bdrv_unref_child(data->parent_b, data->child_b);
1313 bdrv_ref(data->c);
1314 data->child_c = bdrv_attach_child(data->parent_b, data->c, "PB-C",
1315 &child_file, &error_abort);
1318 static void detach_by_parent_aio_cb(void *opaque, int ret)
1320 struct detach_by_parent_data *data = &detach_by_parent_data;
1322 g_assert_cmpint(ret, ==, 0);
1323 if (data->by_parent_cb) {
1324 detach_indirect_bh(data);
1328 static void detach_by_driver_cb_drained_begin(BdrvChild *child)
1330 aio_bh_schedule_oneshot(qemu_get_current_aio_context(),
1331 detach_indirect_bh, &detach_by_parent_data);
1332 child_file.drained_begin(child);
1335 static BdrvChildRole detach_by_driver_cb_role;
1338 * Initial graph:
1340 * PA PB
1341 * \ / \
1342 * A B C
1344 * by_parent_cb == true: Test that parent callbacks don't poll
1346 * PA has a pending write request whose callback changes the child nodes of
1347 * PB: It removes B and adds C instead. The subtree of PB is drained, which
1348 * will indirectly drain the write request, too.
1350 * by_parent_cb == false: Test that bdrv_drain_invoke() doesn't poll
1352 * PA's BdrvChildRole has a .drained_begin callback that schedules a BH
1353 * that does the same graph change. If bdrv_drain_invoke() calls it, the
1354 * state is messed up, but if it is only polled in the single
1355 * BDRV_POLL_WHILE() at the end of the drain, this should work fine.
1357 static void test_detach_indirect(bool by_parent_cb)
1359 BlockBackend *blk;
1360 BlockDriverState *parent_a, *parent_b, *a, *b, *c;
1361 BdrvChild *child_a, *child_b;
1362 BlockAIOCB *acb;
1364 QEMUIOVector qiov = QEMU_IOVEC_INIT_BUF(qiov, NULL, 0);
1366 if (!by_parent_cb) {
1367 detach_by_driver_cb_role = child_file;
1368 detach_by_driver_cb_role.drained_begin =
1369 detach_by_driver_cb_drained_begin;
1372 /* Create all involved nodes */
1373 parent_a = bdrv_new_open_driver(&bdrv_test, "parent-a", BDRV_O_RDWR,
1374 &error_abort);
1375 parent_b = bdrv_new_open_driver(&bdrv_test, "parent-b", 0,
1376 &error_abort);
1378 a = bdrv_new_open_driver(&bdrv_test, "a", BDRV_O_RDWR, &error_abort);
1379 b = bdrv_new_open_driver(&bdrv_test, "b", BDRV_O_RDWR, &error_abort);
1380 c = bdrv_new_open_driver(&bdrv_test, "c", BDRV_O_RDWR, &error_abort);
1382 /* blk is a BB for parent-a */
1383 blk = blk_new(qemu_get_aio_context(), BLK_PERM_ALL, BLK_PERM_ALL);
1384 blk_insert_bs(blk, parent_a, &error_abort);
1385 bdrv_unref(parent_a);
1387 /* If we want to get bdrv_drain_invoke() to call aio_poll(), the driver
1388 * callback must not return immediately. */
1389 if (!by_parent_cb) {
1390 BDRVTestState *s = parent_a->opaque;
1391 s->sleep_in_drain_begin = true;
1394 /* Set child relationships */
1395 bdrv_ref(b);
1396 bdrv_ref(a);
1397 child_b = bdrv_attach_child(parent_b, b, "PB-B", &child_file, &error_abort);
1398 child_a = bdrv_attach_child(parent_b, a, "PB-A", &child_backing, &error_abort);
1400 bdrv_ref(a);
1401 bdrv_attach_child(parent_a, a, "PA-A",
1402 by_parent_cb ? &child_file : &detach_by_driver_cb_role,
1403 &error_abort);
1405 g_assert_cmpint(parent_a->refcnt, ==, 1);
1406 g_assert_cmpint(parent_b->refcnt, ==, 1);
1407 g_assert_cmpint(a->refcnt, ==, 3);
1408 g_assert_cmpint(b->refcnt, ==, 2);
1409 g_assert_cmpint(c->refcnt, ==, 1);
1411 g_assert(QLIST_FIRST(&parent_b->children) == child_a);
1412 g_assert(QLIST_NEXT(child_a, next) == child_b);
1413 g_assert(QLIST_NEXT(child_b, next) == NULL);
1415 /* Start the evil write request */
1416 detach_by_parent_data = (struct detach_by_parent_data) {
1417 .parent_b = parent_b,
1418 .child_b = child_b,
1419 .c = c,
1420 .by_parent_cb = by_parent_cb,
1422 acb = blk_aio_preadv(blk, 0, &qiov, 0, detach_by_parent_aio_cb, NULL);
1423 g_assert(acb != NULL);
1425 /* Drain and check the expected result */
1426 bdrv_subtree_drained_begin(parent_b);
1428 g_assert(detach_by_parent_data.child_c != NULL);
1430 g_assert_cmpint(parent_a->refcnt, ==, 1);
1431 g_assert_cmpint(parent_b->refcnt, ==, 1);
1432 g_assert_cmpint(a->refcnt, ==, 3);
1433 g_assert_cmpint(b->refcnt, ==, 1);
1434 g_assert_cmpint(c->refcnt, ==, 2);
1436 g_assert(QLIST_FIRST(&parent_b->children) == detach_by_parent_data.child_c);
1437 g_assert(QLIST_NEXT(detach_by_parent_data.child_c, next) == child_a);
1438 g_assert(QLIST_NEXT(child_a, next) == NULL);
1440 g_assert_cmpint(parent_a->quiesce_counter, ==, 1);
1441 g_assert_cmpint(parent_b->quiesce_counter, ==, 1);
1442 g_assert_cmpint(a->quiesce_counter, ==, 1);
1443 g_assert_cmpint(b->quiesce_counter, ==, 0);
1444 g_assert_cmpint(c->quiesce_counter, ==, 1);
1446 bdrv_subtree_drained_end(parent_b);
1448 bdrv_unref(parent_b);
1449 blk_unref(blk);
1451 g_assert_cmpint(a->refcnt, ==, 1);
1452 g_assert_cmpint(b->refcnt, ==, 1);
1453 g_assert_cmpint(c->refcnt, ==, 1);
1454 bdrv_unref(a);
1455 bdrv_unref(b);
1456 bdrv_unref(c);
1459 static void test_detach_by_parent_cb(void)
1461 test_detach_indirect(true);
1464 static void test_detach_by_driver_cb(void)
1466 test_detach_indirect(false);
1469 static void test_append_to_drained(void)
1471 BlockBackend *blk;
1472 BlockDriverState *base, *overlay;
1473 BDRVTestState *base_s, *overlay_s;
1475 blk = blk_new(qemu_get_aio_context(), BLK_PERM_ALL, BLK_PERM_ALL);
1476 base = bdrv_new_open_driver(&bdrv_test, "base", BDRV_O_RDWR, &error_abort);
1477 base_s = base->opaque;
1478 blk_insert_bs(blk, base, &error_abort);
1480 overlay = bdrv_new_open_driver(&bdrv_test, "overlay", BDRV_O_RDWR,
1481 &error_abort);
1482 overlay_s = overlay->opaque;
1484 do_drain_begin(BDRV_DRAIN, base);
1485 g_assert_cmpint(base->quiesce_counter, ==, 1);
1486 g_assert_cmpint(base_s->drain_count, ==, 1);
1487 g_assert_cmpint(base->in_flight, ==, 0);
1489 /* Takes ownership of overlay, so we don't have to unref it later */
1490 bdrv_append(overlay, base, &error_abort);
1491 g_assert_cmpint(base->in_flight, ==, 0);
1492 g_assert_cmpint(overlay->in_flight, ==, 0);
1494 g_assert_cmpint(base->quiesce_counter, ==, 1);
1495 g_assert_cmpint(base_s->drain_count, ==, 1);
1496 g_assert_cmpint(overlay->quiesce_counter, ==, 1);
1497 g_assert_cmpint(overlay_s->drain_count, ==, 1);
1499 do_drain_end(BDRV_DRAIN, base);
1501 g_assert_cmpint(base->quiesce_counter, ==, 0);
1502 g_assert_cmpint(base_s->drain_count, ==, 0);
1503 g_assert_cmpint(overlay->quiesce_counter, ==, 0);
1504 g_assert_cmpint(overlay_s->drain_count, ==, 0);
1506 bdrv_unref(base);
1507 blk_unref(blk);
1510 static void test_set_aio_context(void)
1512 BlockDriverState *bs;
1513 IOThread *a = iothread_new();
1514 IOThread *b = iothread_new();
1515 AioContext *ctx_a = iothread_get_aio_context(a);
1516 AioContext *ctx_b = iothread_get_aio_context(b);
1518 bs = bdrv_new_open_driver(&bdrv_test, "test-node", BDRV_O_RDWR,
1519 &error_abort);
1521 bdrv_drained_begin(bs);
1522 bdrv_try_set_aio_context(bs, ctx_a, &error_abort);
1524 aio_context_acquire(ctx_a);
1525 bdrv_drained_end(bs);
1527 bdrv_drained_begin(bs);
1528 bdrv_try_set_aio_context(bs, ctx_b, &error_abort);
1529 aio_context_release(ctx_a);
1530 aio_context_acquire(ctx_b);
1531 bdrv_try_set_aio_context(bs, qemu_get_aio_context(), &error_abort);
1532 aio_context_release(ctx_b);
1533 bdrv_drained_end(bs);
1535 bdrv_unref(bs);
1536 iothread_join(a);
1537 iothread_join(b);
1541 typedef struct TestDropBackingBlockJob {
1542 BlockJob common;
1543 bool should_complete;
1544 bool *did_complete;
1545 BlockDriverState *detach_also;
1546 } TestDropBackingBlockJob;
1548 static int coroutine_fn test_drop_backing_job_run(Job *job, Error **errp)
1550 TestDropBackingBlockJob *s =
1551 container_of(job, TestDropBackingBlockJob, common.job);
1553 while (!s->should_complete) {
1554 job_sleep_ns(job, 0);
1557 return 0;
1560 static void test_drop_backing_job_commit(Job *job)
1562 TestDropBackingBlockJob *s =
1563 container_of(job, TestDropBackingBlockJob, common.job);
1565 bdrv_set_backing_hd(blk_bs(s->common.blk), NULL, &error_abort);
1566 bdrv_set_backing_hd(s->detach_also, NULL, &error_abort);
1568 *s->did_complete = true;
1571 static const BlockJobDriver test_drop_backing_job_driver = {
1572 .job_driver = {
1573 .instance_size = sizeof(TestDropBackingBlockJob),
1574 .free = block_job_free,
1575 .user_resume = block_job_user_resume,
1576 .run = test_drop_backing_job_run,
1577 .commit = test_drop_backing_job_commit,
1582 * Creates a child node with three parent nodes on it, and then runs a
1583 * block job on the final one, parent-node-2.
1585 * The job is then asked to complete before a section where the child
1586 * is drained.
1588 * Ending this section will undrain the child's parents, first
1589 * parent-node-2, then parent-node-1, then parent-node-0 -- the parent
1590 * list is in reverse order of how they were added. Ending the drain
1591 * on parent-node-2 will resume the job, thus completing it and
1592 * scheduling job_exit().
1594 * Ending the drain on parent-node-1 will poll the AioContext, which
1595 * lets job_exit() and thus test_drop_backing_job_commit() run. That
1596 * function first removes the child as parent-node-2's backing file.
1598 * In old (and buggy) implementations, there are two problems with
1599 * that:
1600 * (A) bdrv_drain_invoke() polls for every node that leaves the
1601 * drained section. This means that job_exit() is scheduled
1602 * before the child has left the drained section. Its
1603 * quiesce_counter is therefore still 1 when it is removed from
1604 * parent-node-2.
1606 * (B) bdrv_replace_child_noperm() calls drained_end() on the old
1607 * child's parents as many times as the child is quiesced. This
1608 * means it will call drained_end() on parent-node-2 once.
1609 * Because parent-node-2 is no longer quiesced at this point, this
1610 * will fail.
1612 * bdrv_replace_child_noperm() therefore must call drained_end() on
1613 * the parent only if it really is still drained because the child is
1614 * drained.
1616 * If removing child from parent-node-2 was successful (as it should
1617 * be), test_drop_backing_job_commit() will then also remove the child
1618 * from parent-node-0.
1620 * With an old version of our drain infrastructure ((A) above), that
1621 * resulted in the following flow:
1623 * 1. child attempts to leave its drained section. The call recurses
1624 * to its parents.
1626 * 2. parent-node-2 leaves the drained section. Polling in
1627 * bdrv_drain_invoke() will schedule job_exit().
1629 * 3. parent-node-1 leaves the drained section. Polling in
1630 * bdrv_drain_invoke() will run job_exit(), thus disconnecting
1631 * parent-node-0 from the child node.
1633 * 4. bdrv_parent_drained_end() uses a QLIST_FOREACH_SAFE() loop to
1634 * iterate over the parents. Thus, it now accesses the BdrvChild
1635 * object that used to connect parent-node-0 and the child node.
1636 * However, that object no longer exists, so it accesses a dangling
1637 * pointer.
1639 * The solution is to only poll once when running a bdrv_drained_end()
1640 * operation, specifically at the end when all drained_end()
1641 * operations for all involved nodes have been scheduled.
1642 * Note that this also solves (A) above, thus hiding (B).
1644 static void test_blockjob_commit_by_drained_end(void)
1646 BlockDriverState *bs_child, *bs_parents[3];
1647 TestDropBackingBlockJob *job;
1648 bool job_has_completed = false;
1649 int i;
1651 bs_child = bdrv_new_open_driver(&bdrv_test, "child-node", BDRV_O_RDWR,
1652 &error_abort);
1654 for (i = 0; i < 3; i++) {
1655 char name[32];
1656 snprintf(name, sizeof(name), "parent-node-%i", i);
1657 bs_parents[i] = bdrv_new_open_driver(&bdrv_test, name, BDRV_O_RDWR,
1658 &error_abort);
1659 bdrv_set_backing_hd(bs_parents[i], bs_child, &error_abort);
1662 job = block_job_create("job", &test_drop_backing_job_driver, NULL,
1663 bs_parents[2], 0, BLK_PERM_ALL, 0, 0, NULL, NULL,
1664 &error_abort);
1666 job->detach_also = bs_parents[0];
1667 job->did_complete = &job_has_completed;
1669 job_start(&job->common.job);
1671 job->should_complete = true;
1672 bdrv_drained_begin(bs_child);
1673 g_assert(!job_has_completed);
1674 bdrv_drained_end(bs_child);
1675 g_assert(job_has_completed);
1677 bdrv_unref(bs_parents[0]);
1678 bdrv_unref(bs_parents[1]);
1679 bdrv_unref(bs_parents[2]);
1680 bdrv_unref(bs_child);
1684 typedef struct TestSimpleBlockJob {
1685 BlockJob common;
1686 bool should_complete;
1687 bool *did_complete;
1688 } TestSimpleBlockJob;
1690 static int coroutine_fn test_simple_job_run(Job *job, Error **errp)
1692 TestSimpleBlockJob *s = container_of(job, TestSimpleBlockJob, common.job);
1694 while (!s->should_complete) {
1695 job_sleep_ns(job, 0);
1698 return 0;
1701 static void test_simple_job_clean(Job *job)
1703 TestSimpleBlockJob *s = container_of(job, TestSimpleBlockJob, common.job);
1704 *s->did_complete = true;
1707 static const BlockJobDriver test_simple_job_driver = {
1708 .job_driver = {
1709 .instance_size = sizeof(TestSimpleBlockJob),
1710 .free = block_job_free,
1711 .user_resume = block_job_user_resume,
1712 .run = test_simple_job_run,
1713 .clean = test_simple_job_clean,
1717 static int drop_intermediate_poll_update_filename(BdrvChild *child,
1718 BlockDriverState *new_base,
1719 const char *filename,
1720 Error **errp)
1723 * We are free to poll here, which may change the block graph, if
1724 * it is not drained.
1727 /* If the job is not drained: Complete it, schedule job_exit() */
1728 aio_poll(qemu_get_current_aio_context(), false);
1729 /* If the job is not drained: Run job_exit(), finish the job */
1730 aio_poll(qemu_get_current_aio_context(), false);
1732 return 0;
1736 * Test a poll in the midst of bdrv_drop_intermediate().
1738 * bdrv_drop_intermediate() calls BdrvChildRole.update_filename(),
1739 * which can yield or poll. This may lead to graph changes, unless
1740 * the whole subtree in question is drained.
1742 * We test this on the following graph:
1744 * Job
1747 * job-node
1751 * job-node
1754 * backing
1758 * node-2 --chain--> node-1 --chain--> node-0
1760 * We drop node-1 with bdrv_drop_intermediate(top=node-1, base=node-0).
1762 * This first updates node-2's backing filename by invoking
1763 * drop_intermediate_poll_update_filename(), which polls twice. This
1764 * causes the job to finish, which in turns causes the job-node to be
1765 * deleted.
1767 * bdrv_drop_intermediate() uses a QLIST_FOREACH_SAFE() loop, so it
1768 * already has a pointer to the BdrvChild edge between job-node and
1769 * node-1. When it tries to handle that edge, we probably get a
1770 * segmentation fault because the object no longer exists.
1773 * The solution is for bdrv_drop_intermediate() to drain top's
1774 * subtree. This prevents graph changes from happening just because
1775 * BdrvChildRole.update_filename() yields or polls. Thus, the block
1776 * job is paused during that drained section and must finish before or
1777 * after.
1779 * (In addition, bdrv_replace_child() must keep the job paused.)
1781 static void test_drop_intermediate_poll(void)
1783 static BdrvChildRole chain_child_role;
1784 BlockDriverState *chain[3];
1785 TestSimpleBlockJob *job;
1786 BlockDriverState *job_node;
1787 bool job_has_completed = false;
1788 int i;
1789 int ret;
1791 chain_child_role = child_backing;
1792 chain_child_role.update_filename = drop_intermediate_poll_update_filename;
1794 for (i = 0; i < 3; i++) {
1795 char name[32];
1796 snprintf(name, 32, "node-%i", i);
1798 chain[i] = bdrv_new_open_driver(&bdrv_test, name, 0, &error_abort);
1801 job_node = bdrv_new_open_driver(&bdrv_test, "job-node", BDRV_O_RDWR,
1802 &error_abort);
1803 bdrv_set_backing_hd(job_node, chain[1], &error_abort);
1806 * Establish the chain last, so the chain links are the first
1807 * elements in the BDS.parents lists
1809 for (i = 0; i < 3; i++) {
1810 if (i) {
1811 /* Takes the reference to chain[i - 1] */
1812 chain[i]->backing = bdrv_attach_child(chain[i], chain[i - 1],
1813 "chain", &chain_child_role,
1814 &error_abort);
1818 job = block_job_create("job", &test_simple_job_driver, NULL, job_node,
1819 0, BLK_PERM_ALL, 0, 0, NULL, NULL, &error_abort);
1821 /* The job has a reference now */
1822 bdrv_unref(job_node);
1824 job->did_complete = &job_has_completed;
1826 job_start(&job->common.job);
1827 job->should_complete = true;
1829 g_assert(!job_has_completed);
1830 ret = bdrv_drop_intermediate(chain[1], chain[0], NULL);
1831 g_assert(ret == 0);
1832 g_assert(job_has_completed);
1834 bdrv_unref(chain[2]);
1838 typedef struct BDRVReplaceTestState {
1839 bool was_drained;
1840 bool was_undrained;
1841 bool has_read;
1843 int drain_count;
1845 bool yield_before_read;
1846 Coroutine *io_co;
1847 Coroutine *drain_co;
1848 } BDRVReplaceTestState;
1850 static void bdrv_replace_test_close(BlockDriverState *bs)
1855 * If @bs has a backing file:
1856 * Yield if .yield_before_read is true (and wait for drain_begin to
1857 * wake us up).
1858 * Forward the read to bs->backing. Set .has_read to true.
1859 * If drain_begin has woken us, wake it in turn.
1861 * Otherwise:
1862 * Set .has_read to true and return success.
1864 static int coroutine_fn bdrv_replace_test_co_preadv(BlockDriverState *bs,
1865 uint64_t offset,
1866 uint64_t bytes,
1867 QEMUIOVector *qiov,
1868 int flags)
1870 BDRVReplaceTestState *s = bs->opaque;
1872 if (bs->backing) {
1873 int ret;
1875 g_assert(!s->drain_count);
1877 s->io_co = qemu_coroutine_self();
1878 if (s->yield_before_read) {
1879 s->yield_before_read = false;
1880 qemu_coroutine_yield();
1882 s->io_co = NULL;
1884 ret = bdrv_preadv(bs->backing, offset, qiov);
1885 s->has_read = true;
1887 /* Wake up drain_co if it runs */
1888 if (s->drain_co) {
1889 aio_co_wake(s->drain_co);
1892 return ret;
1895 s->has_read = true;
1896 return 0;
1900 * If .drain_count is 0, wake up .io_co if there is one; and set
1901 * .was_drained.
1902 * Increment .drain_count.
1904 static void coroutine_fn bdrv_replace_test_co_drain_begin(BlockDriverState *bs)
1906 BDRVReplaceTestState *s = bs->opaque;
1908 if (!s->drain_count) {
1909 /* Keep waking io_co up until it is done */
1910 s->drain_co = qemu_coroutine_self();
1911 while (s->io_co) {
1912 aio_co_wake(s->io_co);
1913 s->io_co = NULL;
1914 qemu_coroutine_yield();
1916 s->drain_co = NULL;
1918 s->was_drained = true;
1920 s->drain_count++;
1924 * Reduce .drain_count, set .was_undrained once it reaches 0.
1925 * If .drain_count reaches 0 and the node has a backing file, issue a
1926 * read request.
1928 static void coroutine_fn bdrv_replace_test_co_drain_end(BlockDriverState *bs)
1930 BDRVReplaceTestState *s = bs->opaque;
1932 g_assert(s->drain_count > 0);
1933 if (!--s->drain_count) {
1934 int ret;
1936 s->was_undrained = true;
1938 if (bs->backing) {
1939 char data;
1940 QEMUIOVector qiov = QEMU_IOVEC_INIT_BUF(qiov, &data, 1);
1942 /* Queue a read request post-drain */
1943 ret = bdrv_replace_test_co_preadv(bs, 0, 1, &qiov, 0);
1944 g_assert(ret >= 0);
1949 static BlockDriver bdrv_replace_test = {
1950 .format_name = "replace_test",
1951 .instance_size = sizeof(BDRVReplaceTestState),
1953 .bdrv_close = bdrv_replace_test_close,
1954 .bdrv_co_preadv = bdrv_replace_test_co_preadv,
1956 .bdrv_co_drain_begin = bdrv_replace_test_co_drain_begin,
1957 .bdrv_co_drain_end = bdrv_replace_test_co_drain_end,
1959 .bdrv_child_perm = bdrv_format_default_perms,
1962 static void coroutine_fn test_replace_child_mid_drain_read_co(void *opaque)
1964 int ret;
1965 char data;
1967 ret = blk_co_pread(opaque, 0, 1, &data, 0);
1968 g_assert(ret >= 0);
1972 * We test two things:
1973 * (1) bdrv_replace_child_noperm() must not undrain the parent if both
1974 * children are drained.
1975 * (2) bdrv_replace_child_noperm() must never flush I/O requests to a
1976 * drained child. If the old child is drained, it must flush I/O
1977 * requests after the new one has been attached. If the new child
1978 * is drained, it must flush I/O requests before the old one is
1979 * detached.
1981 * To do so, we create one parent node and two child nodes; then
1982 * attach one of the children (old_child_bs) to the parent, then
1983 * drain both old_child_bs and new_child_bs according to
1984 * old_drain_count and new_drain_count, respectively, and finally
1985 * we invoke bdrv_replace_node() to replace old_child_bs by
1986 * new_child_bs.
1988 * The test block driver we use here (bdrv_replace_test) has a read
1989 * function that:
1990 * - For the parent node, can optionally yield, and then forwards the
1991 * read to bdrv_preadv(),
1992 * - For the child node, just returns immediately.
1994 * If the read yields, the drain_begin function will wake it up.
1996 * The drain_end function issues a read on the parent once it is fully
1997 * undrained (which simulates requests starting to come in again).
1999 static void do_test_replace_child_mid_drain(int old_drain_count,
2000 int new_drain_count)
2002 BlockBackend *parent_blk;
2003 BlockDriverState *parent_bs;
2004 BlockDriverState *old_child_bs, *new_child_bs;
2005 BDRVReplaceTestState *parent_s;
2006 BDRVReplaceTestState *old_child_s, *new_child_s;
2007 Coroutine *io_co;
2008 int i;
2010 parent_bs = bdrv_new_open_driver(&bdrv_replace_test, "parent", 0,
2011 &error_abort);
2012 parent_s = parent_bs->opaque;
2014 parent_blk = blk_new(qemu_get_aio_context(),
2015 BLK_PERM_CONSISTENT_READ, BLK_PERM_ALL);
2016 blk_insert_bs(parent_blk, parent_bs, &error_abort);
2018 old_child_bs = bdrv_new_open_driver(&bdrv_replace_test, "old-child", 0,
2019 &error_abort);
2020 new_child_bs = bdrv_new_open_driver(&bdrv_replace_test, "new-child", 0,
2021 &error_abort);
2022 old_child_s = old_child_bs->opaque;
2023 new_child_s = new_child_bs->opaque;
2025 /* So that we can read something */
2026 parent_bs->total_sectors = 1;
2027 old_child_bs->total_sectors = 1;
2028 new_child_bs->total_sectors = 1;
2030 bdrv_ref(old_child_bs);
2031 parent_bs->backing = bdrv_attach_child(parent_bs, old_child_bs, "child",
2032 &child_backing, &error_abort);
2034 for (i = 0; i < old_drain_count; i++) {
2035 bdrv_drained_begin(old_child_bs);
2037 for (i = 0; i < new_drain_count; i++) {
2038 bdrv_drained_begin(new_child_bs);
2041 if (!old_drain_count) {
2043 * Start a read operation that will yield, so it will not
2044 * complete before the node is drained.
2046 parent_s->yield_before_read = true;
2047 io_co = qemu_coroutine_create(test_replace_child_mid_drain_read_co,
2048 parent_blk);
2049 qemu_coroutine_enter(io_co);
2052 /* If we have started a read operation, it should have yielded */
2053 g_assert(!parent_s->has_read);
2055 /* Reset drained status so we can see what bdrv_replace_node() does */
2056 parent_s->was_drained = false;
2057 parent_s->was_undrained = false;
2059 g_assert(parent_bs->quiesce_counter == old_drain_count);
2060 bdrv_replace_node(old_child_bs, new_child_bs, &error_abort);
2061 g_assert(parent_bs->quiesce_counter == new_drain_count);
2063 if (!old_drain_count && !new_drain_count) {
2065 * From undrained to undrained drains and undrains the parent,
2066 * because bdrv_replace_node() contains a drained section for
2067 * @old_child_bs.
2069 g_assert(parent_s->was_drained && parent_s->was_undrained);
2070 } else if (!old_drain_count && new_drain_count) {
2072 * From undrained to drained should drain the parent and keep
2073 * it that way.
2075 g_assert(parent_s->was_drained && !parent_s->was_undrained);
2076 } else if (old_drain_count && !new_drain_count) {
2078 * From drained to undrained should undrain the parent and
2079 * keep it that way.
2081 g_assert(!parent_s->was_drained && parent_s->was_undrained);
2082 } else /* if (old_drain_count && new_drain_count) */ {
2084 * From drained to drained must not undrain the parent at any
2085 * point
2087 g_assert(!parent_s->was_drained && !parent_s->was_undrained);
2090 if (!old_drain_count || !new_drain_count) {
2092 * If !old_drain_count, we have started a read request before
2093 * bdrv_replace_node(). If !new_drain_count, the parent must
2094 * have been undrained at some point, and
2095 * bdrv_replace_test_co_drain_end() starts a read request
2096 * then.
2098 g_assert(parent_s->has_read);
2099 } else {
2101 * If the parent was never undrained, there is no way to start
2102 * a read request.
2104 g_assert(!parent_s->has_read);
2107 /* A drained child must have not received any request */
2108 g_assert(!(old_drain_count && old_child_s->has_read));
2109 g_assert(!(new_drain_count && new_child_s->has_read));
2111 for (i = 0; i < new_drain_count; i++) {
2112 bdrv_drained_end(new_child_bs);
2114 for (i = 0; i < old_drain_count; i++) {
2115 bdrv_drained_end(old_child_bs);
2119 * By now, bdrv_replace_test_co_drain_end() must have been called
2120 * at some point while the new child was attached to the parent.
2122 g_assert(parent_s->has_read);
2123 g_assert(new_child_s->has_read);
2125 blk_unref(parent_blk);
2126 bdrv_unref(parent_bs);
2127 bdrv_unref(old_child_bs);
2128 bdrv_unref(new_child_bs);
2131 static void test_replace_child_mid_drain(void)
2133 int old_drain_count, new_drain_count;
2135 for (old_drain_count = 0; old_drain_count < 2; old_drain_count++) {
2136 for (new_drain_count = 0; new_drain_count < 2; new_drain_count++) {
2137 do_test_replace_child_mid_drain(old_drain_count, new_drain_count);
2142 int main(int argc, char **argv)
2144 int ret;
2146 bdrv_init();
2147 qemu_init_main_loop(&error_abort);
2149 g_test_init(&argc, &argv, NULL);
2150 qemu_event_init(&done_event, false);
2152 g_test_add_func("/bdrv-drain/driver-cb/drain_all", test_drv_cb_drain_all);
2153 g_test_add_func("/bdrv-drain/driver-cb/drain", test_drv_cb_drain);
2154 g_test_add_func("/bdrv-drain/driver-cb/drain_subtree",
2155 test_drv_cb_drain_subtree);
2157 g_test_add_func("/bdrv-drain/driver-cb/co/drain_all",
2158 test_drv_cb_co_drain_all);
2159 g_test_add_func("/bdrv-drain/driver-cb/co/drain", test_drv_cb_co_drain);
2160 g_test_add_func("/bdrv-drain/driver-cb/co/drain_subtree",
2161 test_drv_cb_co_drain_subtree);
2164 g_test_add_func("/bdrv-drain/quiesce/drain_all", test_quiesce_drain_all);
2165 g_test_add_func("/bdrv-drain/quiesce/drain", test_quiesce_drain);
2166 g_test_add_func("/bdrv-drain/quiesce/drain_subtree",
2167 test_quiesce_drain_subtree);
2169 g_test_add_func("/bdrv-drain/quiesce/co/drain_all",
2170 test_quiesce_co_drain_all);
2171 g_test_add_func("/bdrv-drain/quiesce/co/drain", test_quiesce_co_drain);
2172 g_test_add_func("/bdrv-drain/quiesce/co/drain_subtree",
2173 test_quiesce_co_drain_subtree);
2175 g_test_add_func("/bdrv-drain/nested", test_nested);
2176 g_test_add_func("/bdrv-drain/multiparent", test_multiparent);
2178 g_test_add_func("/bdrv-drain/graph-change/drain_subtree",
2179 test_graph_change_drain_subtree);
2180 g_test_add_func("/bdrv-drain/graph-change/drain_all",
2181 test_graph_change_drain_all);
2183 g_test_add_func("/bdrv-drain/iothread/drain_all", test_iothread_drain_all);
2184 g_test_add_func("/bdrv-drain/iothread/drain", test_iothread_drain);
2185 g_test_add_func("/bdrv-drain/iothread/drain_subtree",
2186 test_iothread_drain_subtree);
2188 g_test_add_func("/bdrv-drain/blockjob/drain_all", test_blockjob_drain_all);
2189 g_test_add_func("/bdrv-drain/blockjob/drain", test_blockjob_drain);
2190 g_test_add_func("/bdrv-drain/blockjob/drain_subtree",
2191 test_blockjob_drain_subtree);
2193 g_test_add_func("/bdrv-drain/blockjob/error/drain_all",
2194 test_blockjob_error_drain_all);
2195 g_test_add_func("/bdrv-drain/blockjob/error/drain",
2196 test_blockjob_error_drain);
2197 g_test_add_func("/bdrv-drain/blockjob/error/drain_subtree",
2198 test_blockjob_error_drain_subtree);
2200 g_test_add_func("/bdrv-drain/blockjob/iothread/drain_all",
2201 test_blockjob_iothread_drain_all);
2202 g_test_add_func("/bdrv-drain/blockjob/iothread/drain",
2203 test_blockjob_iothread_drain);
2204 g_test_add_func("/bdrv-drain/blockjob/iothread/drain_subtree",
2205 test_blockjob_iothread_drain_subtree);
2207 g_test_add_func("/bdrv-drain/blockjob/iothread/error/drain_all",
2208 test_blockjob_iothread_error_drain_all);
2209 g_test_add_func("/bdrv-drain/blockjob/iothread/error/drain",
2210 test_blockjob_iothread_error_drain);
2211 g_test_add_func("/bdrv-drain/blockjob/iothread/error/drain_subtree",
2212 test_blockjob_iothread_error_drain_subtree);
2214 g_test_add_func("/bdrv-drain/deletion/drain", test_delete_by_drain);
2215 g_test_add_func("/bdrv-drain/detach/drain_all", test_detach_by_drain_all);
2216 g_test_add_func("/bdrv-drain/detach/drain", test_detach_by_drain);
2217 g_test_add_func("/bdrv-drain/detach/drain_subtree", test_detach_by_drain_subtree);
2218 g_test_add_func("/bdrv-drain/detach/parent_cb", test_detach_by_parent_cb);
2219 g_test_add_func("/bdrv-drain/detach/driver_cb", test_detach_by_driver_cb);
2221 g_test_add_func("/bdrv-drain/attach/drain", test_append_to_drained);
2223 g_test_add_func("/bdrv-drain/set_aio_context", test_set_aio_context);
2225 g_test_add_func("/bdrv-drain/blockjob/commit_by_drained_end",
2226 test_blockjob_commit_by_drained_end);
2228 g_test_add_func("/bdrv-drain/bdrv_drop_intermediate/poll",
2229 test_drop_intermediate_poll);
2231 g_test_add_func("/bdrv-drain/replace_child/mid-drain",
2232 test_replace_child_mid_drain);
2234 ret = g_test_run();
2235 qemu_event_destroy(&done_event);
2236 return ret;