]> git.proxmox.com Git - mirror_qemu.git/blame - tests/test-bdrv-drain.c
block: Allow graph changes in bdrv_drain_all_begin/end sections
[mirror_qemu.git] / tests / test-bdrv-drain.c
CommitLineData
881cfd17
KW
1/*
2 * Block node draining tests
3 *
4 * Copyright (c) 2017 Kevin Wolf <kwolf@redhat.com>
5 *
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:
12 *
13 * The above copyright notice and this permission notice shall be included in
14 * all copies or substantial portions of the Software.
15 *
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.
23 */
24
25#include "qemu/osdep.h"
26#include "block/block.h"
7253220d 27#include "block/blockjob_int.h"
881cfd17
KW
28#include "sysemu/block-backend.h"
29#include "qapi/error.h"
bb675689
KW
30#include "iothread.h"
31
32static QemuEvent done_event;
881cfd17
KW
33
34typedef struct BDRVTestState {
35 int drain_count;
bb675689 36 AioContext *bh_indirection_ctx;
57320ca9 37 bool sleep_in_drain_begin;
881cfd17
KW
38} BDRVTestState;
39
40static void coroutine_fn bdrv_test_co_drain_begin(BlockDriverState *bs)
41{
42 BDRVTestState *s = bs->opaque;
43 s->drain_count++;
57320ca9
KW
44 if (s->sleep_in_drain_begin) {
45 qemu_co_sleep_ns(QEMU_CLOCK_REALTIME, 100000);
46 }
881cfd17
KW
47}
48
49static void coroutine_fn bdrv_test_co_drain_end(BlockDriverState *bs)
50{
51 BDRVTestState *s = bs->opaque;
52 s->drain_count--;
53}
54
55static void bdrv_test_close(BlockDriverState *bs)
56{
57 BDRVTestState *s = bs->opaque;
58 g_assert_cmpint(s->drain_count, >, 0);
59}
60
bb675689
KW
61static void co_reenter_bh(void *opaque)
62{
63 aio_co_wake(opaque);
64}
65
881cfd17
KW
66static int coroutine_fn bdrv_test_co_preadv(BlockDriverState *bs,
67 uint64_t offset, uint64_t bytes,
68 QEMUIOVector *qiov, int flags)
69{
bb675689
KW
70 BDRVTestState *s = bs->opaque;
71
881cfd17
KW
72 /* We want this request to stay until the polling loop in drain waits for
73 * it to complete. We need to sleep a while as bdrv_drain_invoke() comes
74 * first and polls its result, too, but it shouldn't accidentally complete
75 * this request yet. */
76 qemu_co_sleep_ns(QEMU_CLOCK_REALTIME, 100000);
77
bb675689
KW
78 if (s->bh_indirection_ctx) {
79 aio_bh_schedule_oneshot(s->bh_indirection_ctx, co_reenter_bh,
80 qemu_coroutine_self());
81 qemu_coroutine_yield();
82 }
83
881cfd17
KW
84 return 0;
85}
86
57320ca9
KW
87static void bdrv_test_child_perm(BlockDriverState *bs, BdrvChild *c,
88 const BdrvChildRole *role,
89 BlockReopenQueue *reopen_queue,
90 uint64_t perm, uint64_t shared,
91 uint64_t *nperm, uint64_t *nshared)
92{
93 /* bdrv_format_default_perms() accepts only these two, so disguise
94 * detach_by_driver_cb_role as one of them. */
95 if (role != &child_file && role != &child_backing) {
96 role = &child_file;
97 }
98
99 bdrv_format_default_perms(bs, c, role, reopen_queue, perm, shared,
100 nperm, nshared);
101}
102
881cfd17
KW
103static BlockDriver bdrv_test = {
104 .format_name = "test",
105 .instance_size = sizeof(BDRVTestState),
106
107 .bdrv_close = bdrv_test_close,
108 .bdrv_co_preadv = bdrv_test_co_preadv,
109
110 .bdrv_co_drain_begin = bdrv_test_co_drain_begin,
111 .bdrv_co_drain_end = bdrv_test_co_drain_end,
86e1c840 112
57320ca9 113 .bdrv_child_perm = bdrv_test_child_perm,
881cfd17
KW
114};
115
116static void aio_ret_cb(void *opaque, int ret)
117{
118 int *aio_ret = opaque;
119 *aio_ret = ret;
120}
121
0582eb10
KW
122typedef struct CallInCoroutineData {
123 void (*entry)(void);
124 bool done;
125} CallInCoroutineData;
126
127static coroutine_fn void call_in_coroutine_entry(void *opaque)
128{
129 CallInCoroutineData *data = opaque;
130
131 data->entry();
132 data->done = true;
133}
134
135static void call_in_coroutine(void (*entry)(void))
136{
137 Coroutine *co;
138 CallInCoroutineData data = {
139 .entry = entry,
140 .done = false,
141 };
142
143 co = qemu_coroutine_create(call_in_coroutine_entry, &data);
144 qemu_coroutine_enter(co);
145 while (!data.done) {
146 aio_poll(qemu_get_aio_context(), true);
147 }
148}
149
86e1c840
KW
150enum drain_type {
151 BDRV_DRAIN_ALL,
152 BDRV_DRAIN,
d2a85d0f 153 BDRV_SUBTREE_DRAIN,
6c429a6a 154 DRAIN_TYPE_MAX,
86e1c840
KW
155};
156
157static void do_drain_begin(enum drain_type drain_type, BlockDriverState *bs)
158{
159 switch (drain_type) {
160 case BDRV_DRAIN_ALL: bdrv_drain_all_begin(); break;
161 case BDRV_DRAIN: bdrv_drained_begin(bs); break;
d2a85d0f 162 case BDRV_SUBTREE_DRAIN: bdrv_subtree_drained_begin(bs); break;
86e1c840
KW
163 default: g_assert_not_reached();
164 }
165}
166
167static void do_drain_end(enum drain_type drain_type, BlockDriverState *bs)
168{
169 switch (drain_type) {
170 case BDRV_DRAIN_ALL: bdrv_drain_all_end(); break;
171 case BDRV_DRAIN: bdrv_drained_end(bs); break;
d2a85d0f 172 case BDRV_SUBTREE_DRAIN: bdrv_subtree_drained_end(bs); break;
86e1c840
KW
173 default: g_assert_not_reached();
174 }
175}
176
177static void test_drv_cb_common(enum drain_type drain_type, bool recursive)
881cfd17
KW
178{
179 BlockBackend *blk;
86e1c840
KW
180 BlockDriverState *bs, *backing;
181 BDRVTestState *s, *backing_s;
881cfd17
KW
182 BlockAIOCB *acb;
183 int aio_ret;
184
185 QEMUIOVector qiov;
186 struct iovec iov = {
187 .iov_base = NULL,
188 .iov_len = 0,
189 };
190 qemu_iovec_init_external(&qiov, &iov, 1);
191
192 blk = blk_new(BLK_PERM_ALL, BLK_PERM_ALL);
193 bs = bdrv_new_open_driver(&bdrv_test, "test-node", BDRV_O_RDWR,
194 &error_abort);
195 s = bs->opaque;
196 blk_insert_bs(blk, bs, &error_abort);
197
86e1c840
KW
198 backing = bdrv_new_open_driver(&bdrv_test, "backing", 0, &error_abort);
199 backing_s = backing->opaque;
200 bdrv_set_backing_hd(bs, backing, &error_abort);
201
881cfd17
KW
202 /* Simple bdrv_drain_all_begin/end pair, check that CBs are called */
203 g_assert_cmpint(s->drain_count, ==, 0);
86e1c840
KW
204 g_assert_cmpint(backing_s->drain_count, ==, 0);
205
206 do_drain_begin(drain_type, bs);
207
881cfd17 208 g_assert_cmpint(s->drain_count, ==, 1);
86e1c840
KW
209 g_assert_cmpint(backing_s->drain_count, ==, !!recursive);
210
211 do_drain_end(drain_type, bs);
212
881cfd17 213 g_assert_cmpint(s->drain_count, ==, 0);
86e1c840 214 g_assert_cmpint(backing_s->drain_count, ==, 0);
881cfd17
KW
215
216 /* Now do the same while a request is pending */
217 aio_ret = -EINPROGRESS;
218 acb = blk_aio_preadv(blk, 0, &qiov, 0, aio_ret_cb, &aio_ret);
219 g_assert(acb != NULL);
220 g_assert_cmpint(aio_ret, ==, -EINPROGRESS);
221
222 g_assert_cmpint(s->drain_count, ==, 0);
86e1c840
KW
223 g_assert_cmpint(backing_s->drain_count, ==, 0);
224
225 do_drain_begin(drain_type, bs);
226
881cfd17
KW
227 g_assert_cmpint(aio_ret, ==, 0);
228 g_assert_cmpint(s->drain_count, ==, 1);
86e1c840
KW
229 g_assert_cmpint(backing_s->drain_count, ==, !!recursive);
230
231 do_drain_end(drain_type, bs);
232
881cfd17 233 g_assert_cmpint(s->drain_count, ==, 0);
86e1c840 234 g_assert_cmpint(backing_s->drain_count, ==, 0);
881cfd17 235
86e1c840 236 bdrv_unref(backing);
881cfd17
KW
237 bdrv_unref(bs);
238 blk_unref(blk);
239}
240
86e1c840
KW
241static void test_drv_cb_drain_all(void)
242{
243 test_drv_cb_common(BDRV_DRAIN_ALL, true);
244}
245
246static void test_drv_cb_drain(void)
247{
248 test_drv_cb_common(BDRV_DRAIN, false);
249}
250
d2a85d0f
KW
251static void test_drv_cb_drain_subtree(void)
252{
253 test_drv_cb_common(BDRV_SUBTREE_DRAIN, true);
254}
255
6d0252f2
KW
256static void test_drv_cb_co_drain_all(void)
257{
258 call_in_coroutine(test_drv_cb_drain_all);
259}
260
0582eb10
KW
261static void test_drv_cb_co_drain(void)
262{
263 call_in_coroutine(test_drv_cb_drain);
264}
265
266static void test_drv_cb_co_drain_subtree(void)
267{
268 call_in_coroutine(test_drv_cb_drain_subtree);
269}
270
89a6ceab
KW
271static void test_quiesce_common(enum drain_type drain_type, bool recursive)
272{
273 BlockBackend *blk;
274 BlockDriverState *bs, *backing;
275
276 blk = blk_new(BLK_PERM_ALL, BLK_PERM_ALL);
277 bs = bdrv_new_open_driver(&bdrv_test, "test-node", BDRV_O_RDWR,
278 &error_abort);
279 blk_insert_bs(blk, bs, &error_abort);
280
281 backing = bdrv_new_open_driver(&bdrv_test, "backing", 0, &error_abort);
282 bdrv_set_backing_hd(bs, backing, &error_abort);
283
284 g_assert_cmpint(bs->quiesce_counter, ==, 0);
285 g_assert_cmpint(backing->quiesce_counter, ==, 0);
286
287 do_drain_begin(drain_type, bs);
288
289 g_assert_cmpint(bs->quiesce_counter, ==, 1);
290 g_assert_cmpint(backing->quiesce_counter, ==, !!recursive);
291
292 do_drain_end(drain_type, bs);
293
294 g_assert_cmpint(bs->quiesce_counter, ==, 0);
295 g_assert_cmpint(backing->quiesce_counter, ==, 0);
296
297 bdrv_unref(backing);
298 bdrv_unref(bs);
299 blk_unref(blk);
300}
301
302static void test_quiesce_drain_all(void)
303{
79ab8b21 304 test_quiesce_common(BDRV_DRAIN_ALL, true);
89a6ceab
KW
305}
306
307static void test_quiesce_drain(void)
308{
309 test_quiesce_common(BDRV_DRAIN, false);
310}
311
d2a85d0f
KW
312static void test_quiesce_drain_subtree(void)
313{
314 test_quiesce_common(BDRV_SUBTREE_DRAIN, true);
315}
316
6d0252f2
KW
317static void test_quiesce_co_drain_all(void)
318{
319 call_in_coroutine(test_quiesce_drain_all);
320}
321
0582eb10
KW
322static void test_quiesce_co_drain(void)
323{
324 call_in_coroutine(test_quiesce_drain);
325}
326
327static void test_quiesce_co_drain_subtree(void)
328{
329 call_in_coroutine(test_quiesce_drain_subtree);
330}
331
6c429a6a
KW
332static void test_nested(void)
333{
334 BlockBackend *blk;
335 BlockDriverState *bs, *backing;
336 BDRVTestState *s, *backing_s;
337 enum drain_type outer, inner;
338
339 blk = blk_new(BLK_PERM_ALL, BLK_PERM_ALL);
340 bs = bdrv_new_open_driver(&bdrv_test, "test-node", BDRV_O_RDWR,
341 &error_abort);
342 s = bs->opaque;
343 blk_insert_bs(blk, bs, &error_abort);
344
345 backing = bdrv_new_open_driver(&bdrv_test, "backing", 0, &error_abort);
346 backing_s = backing->opaque;
347 bdrv_set_backing_hd(bs, backing, &error_abort);
348
349 for (outer = 0; outer < DRAIN_TYPE_MAX; outer++) {
350 for (inner = 0; inner < DRAIN_TYPE_MAX; inner++) {
79ab8b21 351 int backing_quiesce = (outer != BDRV_DRAIN) +
6c429a6a
KW
352 (inner != BDRV_DRAIN);
353
354 g_assert_cmpint(bs->quiesce_counter, ==, 0);
355 g_assert_cmpint(backing->quiesce_counter, ==, 0);
356 g_assert_cmpint(s->drain_count, ==, 0);
357 g_assert_cmpint(backing_s->drain_count, ==, 0);
358
359 do_drain_begin(outer, bs);
360 do_drain_begin(inner, bs);
361
79ab8b21 362 g_assert_cmpint(bs->quiesce_counter, ==, 2);
6c429a6a
KW
363 g_assert_cmpint(backing->quiesce_counter, ==, backing_quiesce);
364 g_assert_cmpint(s->drain_count, ==, 2);
79ab8b21 365 g_assert_cmpint(backing_s->drain_count, ==, backing_quiesce);
6c429a6a
KW
366
367 do_drain_end(inner, bs);
368 do_drain_end(outer, bs);
369
370 g_assert_cmpint(bs->quiesce_counter, ==, 0);
371 g_assert_cmpint(backing->quiesce_counter, ==, 0);
372 g_assert_cmpint(s->drain_count, ==, 0);
373 g_assert_cmpint(backing_s->drain_count, ==, 0);
374 }
375 }
376
377 bdrv_unref(backing);
378 bdrv_unref(bs);
379 blk_unref(blk);
380}
381
27e64474
KW
382static void test_multiparent(void)
383{
384 BlockBackend *blk_a, *blk_b;
385 BlockDriverState *bs_a, *bs_b, *backing;
386 BDRVTestState *a_s, *b_s, *backing_s;
387
388 blk_a = blk_new(BLK_PERM_ALL, BLK_PERM_ALL);
389 bs_a = bdrv_new_open_driver(&bdrv_test, "test-node-a", BDRV_O_RDWR,
390 &error_abort);
391 a_s = bs_a->opaque;
392 blk_insert_bs(blk_a, bs_a, &error_abort);
393
394 blk_b = blk_new(BLK_PERM_ALL, BLK_PERM_ALL);
395 bs_b = bdrv_new_open_driver(&bdrv_test, "test-node-b", BDRV_O_RDWR,
396 &error_abort);
397 b_s = bs_b->opaque;
398 blk_insert_bs(blk_b, bs_b, &error_abort);
399
400 backing = bdrv_new_open_driver(&bdrv_test, "backing", 0, &error_abort);
401 backing_s = backing->opaque;
402 bdrv_set_backing_hd(bs_a, backing, &error_abort);
403 bdrv_set_backing_hd(bs_b, backing, &error_abort);
404
405 g_assert_cmpint(bs_a->quiesce_counter, ==, 0);
406 g_assert_cmpint(bs_b->quiesce_counter, ==, 0);
407 g_assert_cmpint(backing->quiesce_counter, ==, 0);
408 g_assert_cmpint(a_s->drain_count, ==, 0);
409 g_assert_cmpint(b_s->drain_count, ==, 0);
410 g_assert_cmpint(backing_s->drain_count, ==, 0);
411
412 do_drain_begin(BDRV_SUBTREE_DRAIN, bs_a);
413
414 g_assert_cmpint(bs_a->quiesce_counter, ==, 1);
415 g_assert_cmpint(bs_b->quiesce_counter, ==, 1);
416 g_assert_cmpint(backing->quiesce_counter, ==, 1);
417 g_assert_cmpint(a_s->drain_count, ==, 1);
418 g_assert_cmpint(b_s->drain_count, ==, 1);
419 g_assert_cmpint(backing_s->drain_count, ==, 1);
420
421 do_drain_begin(BDRV_SUBTREE_DRAIN, bs_b);
422
423 g_assert_cmpint(bs_a->quiesce_counter, ==, 2);
424 g_assert_cmpint(bs_b->quiesce_counter, ==, 2);
425 g_assert_cmpint(backing->quiesce_counter, ==, 2);
426 g_assert_cmpint(a_s->drain_count, ==, 2);
427 g_assert_cmpint(b_s->drain_count, ==, 2);
428 g_assert_cmpint(backing_s->drain_count, ==, 2);
429
430 do_drain_end(BDRV_SUBTREE_DRAIN, bs_b);
431
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);
438
439 do_drain_end(BDRV_SUBTREE_DRAIN, bs_a);
440
441 g_assert_cmpint(bs_a->quiesce_counter, ==, 0);
442 g_assert_cmpint(bs_b->quiesce_counter, ==, 0);
443 g_assert_cmpint(backing->quiesce_counter, ==, 0);
444 g_assert_cmpint(a_s->drain_count, ==, 0);
445 g_assert_cmpint(b_s->drain_count, ==, 0);
446 g_assert_cmpint(backing_s->drain_count, ==, 0);
447
448 bdrv_unref(backing);
449 bdrv_unref(bs_a);
450 bdrv_unref(bs_b);
451 blk_unref(blk_a);
452 blk_unref(blk_b);
453}
454
acebcf8d
KW
455static void test_graph_change(void)
456{
457 BlockBackend *blk_a, *blk_b;
458 BlockDriverState *bs_a, *bs_b, *backing;
459 BDRVTestState *a_s, *b_s, *backing_s;
460
461 blk_a = blk_new(BLK_PERM_ALL, BLK_PERM_ALL);
462 bs_a = bdrv_new_open_driver(&bdrv_test, "test-node-a", BDRV_O_RDWR,
463 &error_abort);
464 a_s = bs_a->opaque;
465 blk_insert_bs(blk_a, bs_a, &error_abort);
466
467 blk_b = blk_new(BLK_PERM_ALL, BLK_PERM_ALL);
468 bs_b = bdrv_new_open_driver(&bdrv_test, "test-node-b", BDRV_O_RDWR,
469 &error_abort);
470 b_s = bs_b->opaque;
471 blk_insert_bs(blk_b, bs_b, &error_abort);
472
473 backing = bdrv_new_open_driver(&bdrv_test, "backing", 0, &error_abort);
474 backing_s = backing->opaque;
475 bdrv_set_backing_hd(bs_a, backing, &error_abort);
476
477 g_assert_cmpint(bs_a->quiesce_counter, ==, 0);
478 g_assert_cmpint(bs_b->quiesce_counter, ==, 0);
479 g_assert_cmpint(backing->quiesce_counter, ==, 0);
480 g_assert_cmpint(a_s->drain_count, ==, 0);
481 g_assert_cmpint(b_s->drain_count, ==, 0);
482 g_assert_cmpint(backing_s->drain_count, ==, 0);
483
484 do_drain_begin(BDRV_SUBTREE_DRAIN, bs_a);
485 do_drain_begin(BDRV_SUBTREE_DRAIN, bs_a);
486 do_drain_begin(BDRV_SUBTREE_DRAIN, bs_a);
487 do_drain_begin(BDRV_SUBTREE_DRAIN, bs_b);
488 do_drain_begin(BDRV_SUBTREE_DRAIN, bs_b);
489
490 bdrv_set_backing_hd(bs_b, backing, &error_abort);
491 g_assert_cmpint(bs_a->quiesce_counter, ==, 5);
492 g_assert_cmpint(bs_b->quiesce_counter, ==, 5);
493 g_assert_cmpint(backing->quiesce_counter, ==, 5);
494 g_assert_cmpint(a_s->drain_count, ==, 5);
495 g_assert_cmpint(b_s->drain_count, ==, 5);
496 g_assert_cmpint(backing_s->drain_count, ==, 5);
497
498 bdrv_set_backing_hd(bs_b, NULL, &error_abort);
499 g_assert_cmpint(bs_a->quiesce_counter, ==, 3);
500 g_assert_cmpint(bs_b->quiesce_counter, ==, 2);
501 g_assert_cmpint(backing->quiesce_counter, ==, 3);
502 g_assert_cmpint(a_s->drain_count, ==, 3);
503 g_assert_cmpint(b_s->drain_count, ==, 2);
504 g_assert_cmpint(backing_s->drain_count, ==, 3);
505
506 bdrv_set_backing_hd(bs_b, backing, &error_abort);
507 g_assert_cmpint(bs_a->quiesce_counter, ==, 5);
508 g_assert_cmpint(bs_b->quiesce_counter, ==, 5);
509 g_assert_cmpint(backing->quiesce_counter, ==, 5);
510 g_assert_cmpint(a_s->drain_count, ==, 5);
511 g_assert_cmpint(b_s->drain_count, ==, 5);
512 g_assert_cmpint(backing_s->drain_count, ==, 5);
513
514 do_drain_end(BDRV_SUBTREE_DRAIN, bs_b);
515 do_drain_end(BDRV_SUBTREE_DRAIN, bs_b);
516 do_drain_end(BDRV_SUBTREE_DRAIN, bs_a);
517 do_drain_end(BDRV_SUBTREE_DRAIN, bs_a);
518 do_drain_end(BDRV_SUBTREE_DRAIN, bs_a);
519
520 g_assert_cmpint(bs_a->quiesce_counter, ==, 0);
521 g_assert_cmpint(bs_b->quiesce_counter, ==, 0);
522 g_assert_cmpint(backing->quiesce_counter, ==, 0);
523 g_assert_cmpint(a_s->drain_count, ==, 0);
524 g_assert_cmpint(b_s->drain_count, ==, 0);
525 g_assert_cmpint(backing_s->drain_count, ==, 0);
526
527 bdrv_unref(backing);
528 bdrv_unref(bs_a);
529 bdrv_unref(bs_b);
530 blk_unref(blk_a);
531 blk_unref(blk_b);
532}
533
bb675689
KW
534struct test_iothread_data {
535 BlockDriverState *bs;
536 enum drain_type drain_type;
537 int *aio_ret;
538};
539
540static void test_iothread_drain_entry(void *opaque)
541{
542 struct test_iothread_data *data = opaque;
543
544 aio_context_acquire(bdrv_get_aio_context(data->bs));
545 do_drain_begin(data->drain_type, data->bs);
546 g_assert_cmpint(*data->aio_ret, ==, 0);
547 do_drain_end(data->drain_type, data->bs);
548 aio_context_release(bdrv_get_aio_context(data->bs));
549
550 qemu_event_set(&done_event);
551}
552
553static void test_iothread_aio_cb(void *opaque, int ret)
554{
555 int *aio_ret = opaque;
556 *aio_ret = ret;
557 qemu_event_set(&done_event);
558}
559
560/*
561 * Starts an AIO request on a BDS that runs in the AioContext of iothread 1.
562 * The request involves a BH on iothread 2 before it can complete.
563 *
564 * @drain_thread = 0 means that do_drain_begin/end are called from the main
565 * thread, @drain_thread = 1 means that they are called from iothread 1. Drain
566 * for this BDS cannot be called from iothread 2 because only the main thread
567 * may do cross-AioContext polling.
568 */
569static void test_iothread_common(enum drain_type drain_type, int drain_thread)
570{
571 BlockBackend *blk;
572 BlockDriverState *bs;
573 BDRVTestState *s;
574 BlockAIOCB *acb;
575 int aio_ret;
576 struct test_iothread_data data;
577
578 IOThread *a = iothread_new();
579 IOThread *b = iothread_new();
580 AioContext *ctx_a = iothread_get_aio_context(a);
581 AioContext *ctx_b = iothread_get_aio_context(b);
582
583 QEMUIOVector qiov;
584 struct iovec iov = {
585 .iov_base = NULL,
586 .iov_len = 0,
587 };
588 qemu_iovec_init_external(&qiov, &iov, 1);
589
590 /* bdrv_drain_all() may only be called from the main loop thread */
591 if (drain_type == BDRV_DRAIN_ALL && drain_thread != 0) {
592 goto out;
593 }
594
595 blk = blk_new(BLK_PERM_ALL, BLK_PERM_ALL);
596 bs = bdrv_new_open_driver(&bdrv_test, "test-node", BDRV_O_RDWR,
597 &error_abort);
598 s = bs->opaque;
599 blk_insert_bs(blk, bs, &error_abort);
600
601 blk_set_aio_context(blk, ctx_a);
602 aio_context_acquire(ctx_a);
603
604 s->bh_indirection_ctx = ctx_b;
605
606 aio_ret = -EINPROGRESS;
607 if (drain_thread == 0) {
608 acb = blk_aio_preadv(blk, 0, &qiov, 0, test_iothread_aio_cb, &aio_ret);
609 } else {
610 acb = blk_aio_preadv(blk, 0, &qiov, 0, aio_ret_cb, &aio_ret);
611 }
612 g_assert(acb != NULL);
613 g_assert_cmpint(aio_ret, ==, -EINPROGRESS);
614
615 aio_context_release(ctx_a);
616
617 data = (struct test_iothread_data) {
618 .bs = bs,
619 .drain_type = drain_type,
620 .aio_ret = &aio_ret,
621 };
622
623 switch (drain_thread) {
624 case 0:
625 if (drain_type != BDRV_DRAIN_ALL) {
626 aio_context_acquire(ctx_a);
627 }
628
629 /* The request is running on the IOThread a. Draining its block device
630 * will make sure that it has completed as far as the BDS is concerned,
631 * but the drain in this thread can continue immediately after
632 * bdrv_dec_in_flight() and aio_ret might be assigned only slightly
633 * later. */
634 qemu_event_reset(&done_event);
635 do_drain_begin(drain_type, bs);
636 g_assert_cmpint(bs->in_flight, ==, 0);
637
638 if (drain_type != BDRV_DRAIN_ALL) {
639 aio_context_release(ctx_a);
640 }
641 qemu_event_wait(&done_event);
642 if (drain_type != BDRV_DRAIN_ALL) {
643 aio_context_acquire(ctx_a);
644 }
645
646 g_assert_cmpint(aio_ret, ==, 0);
647 do_drain_end(drain_type, bs);
648
649 if (drain_type != BDRV_DRAIN_ALL) {
650 aio_context_release(ctx_a);
651 }
652 break;
653 case 1:
654 qemu_event_reset(&done_event);
655 aio_bh_schedule_oneshot(ctx_a, test_iothread_drain_entry, &data);
656 qemu_event_wait(&done_event);
657 break;
658 default:
659 g_assert_not_reached();
660 }
661
662 aio_context_acquire(ctx_a);
663 blk_set_aio_context(blk, qemu_get_aio_context());
664 aio_context_release(ctx_a);
665
666 bdrv_unref(bs);
667 blk_unref(blk);
668
669out:
670 iothread_join(a);
671 iothread_join(b);
672}
673
674static void test_iothread_drain_all(void)
675{
676 test_iothread_common(BDRV_DRAIN_ALL, 0);
677 test_iothread_common(BDRV_DRAIN_ALL, 1);
678}
679
680static void test_iothread_drain(void)
681{
682 test_iothread_common(BDRV_DRAIN, 0);
683 test_iothread_common(BDRV_DRAIN, 1);
684}
685
686static void test_iothread_drain_subtree(void)
687{
688 test_iothread_common(BDRV_SUBTREE_DRAIN, 0);
689 test_iothread_common(BDRV_SUBTREE_DRAIN, 1);
690}
691
7253220d
KW
692
693typedef struct TestBlockJob {
694 BlockJob common;
695 bool should_complete;
696} TestBlockJob;
697
1908a559 698static void test_job_completed(Job *job, void *opaque)
7253220d 699{
1266c9b9 700 job_completed(job, 0, NULL);
7253220d
KW
701}
702
703static void coroutine_fn test_job_start(void *opaque)
704{
705 TestBlockJob *s = opaque;
706
2e1795b5 707 job_transition_to_ready(&s->common.job);
7253220d 708 while (!s->should_complete) {
89bd0305
KW
709 /* Avoid block_job_sleep_ns() because it marks the job as !busy. We
710 * want to emulate some actual activity (probably some I/O) here so
711 * that drain has to wait for this acitivity to stop. */
712 qemu_co_sleep_ns(QEMU_CLOCK_REALTIME, 100000);
713 job_pause_point(&s->common.job);
7253220d
KW
714 }
715
1908a559 716 job_defer_to_main_loop(&s->common.job, test_job_completed, NULL);
7253220d
KW
717}
718
3453d972 719static void test_job_complete(Job *job, Error **errp)
7253220d 720{
3453d972 721 TestBlockJob *s = container_of(job, TestBlockJob, common.job);
7253220d
KW
722 s->should_complete = true;
723}
724
725BlockJobDriver test_job_driver = {
33e9e9bd
KW
726 .job_driver = {
727 .instance_size = sizeof(TestBlockJob),
80fa2c75 728 .free = block_job_free,
b15de828 729 .user_resume = block_job_user_resume,
b69f777d 730 .drain = block_job_drain,
da01ff7f 731 .start = test_job_start,
3453d972 732 .complete = test_job_complete,
33e9e9bd 733 },
7253220d
KW
734};
735
736static void test_blockjob_common(enum drain_type drain_type)
737{
738 BlockBackend *blk_src, *blk_target;
739 BlockDriverState *src, *target;
740 BlockJob *job;
741 int ret;
742
743 src = bdrv_new_open_driver(&bdrv_test, "source", BDRV_O_RDWR,
744 &error_abort);
745 blk_src = blk_new(BLK_PERM_ALL, BLK_PERM_ALL);
746 blk_insert_bs(blk_src, src, &error_abort);
747
748 target = bdrv_new_open_driver(&bdrv_test, "target", BDRV_O_RDWR,
749 &error_abort);
750 blk_target = blk_new(BLK_PERM_ALL, BLK_PERM_ALL);
751 blk_insert_bs(blk_target, target, &error_abort);
752
75859b94
JS
753 job = block_job_create("job0", &test_job_driver, NULL, src, 0, BLK_PERM_ALL,
754 0, 0, NULL, NULL, &error_abort);
7253220d 755 block_job_add_bdrv(job, "target", target, 0, BLK_PERM_ALL, &error_abort);
da01ff7f 756 job_start(&job->job);
7253220d 757
da01ff7f
KW
758 g_assert_cmpint(job->job.pause_count, ==, 0);
759 g_assert_false(job->job.paused);
89bd0305 760 g_assert_true(job->job.busy); /* We're in job_sleep_ns() */
7253220d
KW
761
762 do_drain_begin(drain_type, src);
763
764 if (drain_type == BDRV_DRAIN_ALL) {
81193349 765 /* bdrv_drain_all() drains both src and target */
da01ff7f 766 g_assert_cmpint(job->job.pause_count, ==, 2);
7253220d 767 } else {
da01ff7f 768 g_assert_cmpint(job->job.pause_count, ==, 1);
7253220d 769 }
89bd0305 770 g_assert_true(job->job.paused);
da01ff7f 771 g_assert_false(job->job.busy); /* The job is paused */
7253220d
KW
772
773 do_drain_end(drain_type, src);
774
da01ff7f
KW
775 g_assert_cmpint(job->job.pause_count, ==, 0);
776 g_assert_false(job->job.paused);
89bd0305 777 g_assert_true(job->job.busy); /* We're in qemu_co_sleep_ns() */
7253220d
KW
778
779 do_drain_begin(drain_type, target);
780
781 if (drain_type == BDRV_DRAIN_ALL) {
81193349 782 /* bdrv_drain_all() drains both src and target */
da01ff7f 783 g_assert_cmpint(job->job.pause_count, ==, 2);
7253220d 784 } else {
da01ff7f 785 g_assert_cmpint(job->job.pause_count, ==, 1);
7253220d 786 }
89bd0305 787 g_assert_true(job->job.paused);
da01ff7f 788 g_assert_false(job->job.busy); /* The job is paused */
7253220d
KW
789
790 do_drain_end(drain_type, target);
791
da01ff7f
KW
792 g_assert_cmpint(job->job.pause_count, ==, 0);
793 g_assert_false(job->job.paused);
89bd0305 794 g_assert_true(job->job.busy); /* We're in job_sleep_ns() */
7253220d 795
3d70ff53 796 ret = job_complete_sync(&job->job, &error_abort);
7253220d
KW
797 g_assert_cmpint(ret, ==, 0);
798
799 blk_unref(blk_src);
800 blk_unref(blk_target);
801 bdrv_unref(src);
802 bdrv_unref(target);
803}
804
805static void test_blockjob_drain_all(void)
806{
807 test_blockjob_common(BDRV_DRAIN_ALL);
808}
809
810static void test_blockjob_drain(void)
811{
812 test_blockjob_common(BDRV_DRAIN);
813}
814
d2a85d0f
KW
815static void test_blockjob_drain_subtree(void)
816{
817 test_blockjob_common(BDRV_SUBTREE_DRAIN);
818}
819
4c8158e3
HR
820
821typedef struct BDRVTestTopState {
822 BdrvChild *wait_child;
823} BDRVTestTopState;
824
825static void bdrv_test_top_close(BlockDriverState *bs)
826{
827 BdrvChild *c, *next_c;
828 QLIST_FOREACH_SAFE(c, &bs->children, next, next_c) {
829 bdrv_unref_child(bs, c);
830 }
831}
832
833static int coroutine_fn bdrv_test_top_co_preadv(BlockDriverState *bs,
834 uint64_t offset, uint64_t bytes,
835 QEMUIOVector *qiov, int flags)
836{
837 BDRVTestTopState *tts = bs->opaque;
838 return bdrv_co_preadv(tts->wait_child, offset, bytes, qiov, flags);
839}
840
841static BlockDriver bdrv_test_top_driver = {
842 .format_name = "test_top_driver",
843 .instance_size = sizeof(BDRVTestTopState),
844
845 .bdrv_close = bdrv_test_top_close,
846 .bdrv_co_preadv = bdrv_test_top_co_preadv,
847
848 .bdrv_child_perm = bdrv_format_default_perms,
849};
850
851typedef struct TestCoDeleteByDrainData {
852 BlockBackend *blk;
853 bool detach_instead_of_delete;
854 bool done;
855} TestCoDeleteByDrainData;
856
857static void coroutine_fn test_co_delete_by_drain(void *opaque)
858{
859 TestCoDeleteByDrainData *dbdd = opaque;
860 BlockBackend *blk = dbdd->blk;
861 BlockDriverState *bs = blk_bs(blk);
862 BDRVTestTopState *tts = bs->opaque;
863 void *buffer = g_malloc(65536);
864 QEMUIOVector qiov;
865 struct iovec iov = {
866 .iov_base = buffer,
867 .iov_len = 65536,
868 };
869
870 qemu_iovec_init_external(&qiov, &iov, 1);
871
872 /* Pretend some internal write operation from parent to child.
873 * Important: We have to read from the child, not from the parent!
874 * Draining works by first propagating it all up the tree to the
875 * root and then waiting for drainage from root to the leaves
876 * (protocol nodes). If we have a request waiting on the root,
877 * everything will be drained before we go back down the tree, but
878 * we do not want that. We want to be in the middle of draining
879 * when this following requests returns. */
880 bdrv_co_preadv(tts->wait_child, 0, 65536, &qiov, 0);
881
882 g_assert_cmpint(bs->refcnt, ==, 1);
883
884 if (!dbdd->detach_instead_of_delete) {
885 blk_unref(blk);
886 } else {
887 BdrvChild *c, *next_c;
888 QLIST_FOREACH_SAFE(c, &bs->children, next, next_c) {
889 bdrv_unref_child(bs, c);
890 }
891 }
892
893 dbdd->done = true;
894}
895
896/**
897 * Test what happens when some BDS has some children, you drain one of
898 * them and this results in the BDS being deleted.
899 *
900 * If @detach_instead_of_delete is set, the BDS is not going to be
901 * deleted but will only detach all of its children.
902 */
ebd31837
KW
903static void do_test_delete_by_drain(bool detach_instead_of_delete,
904 enum drain_type drain_type)
4c8158e3
HR
905{
906 BlockBackend *blk;
907 BlockDriverState *bs, *child_bs, *null_bs;
908 BDRVTestTopState *tts;
909 TestCoDeleteByDrainData dbdd;
910 Coroutine *co;
911
912 bs = bdrv_new_open_driver(&bdrv_test_top_driver, "top", BDRV_O_RDWR,
913 &error_abort);
914 bs->total_sectors = 65536 >> BDRV_SECTOR_BITS;
915 tts = bs->opaque;
916
917 null_bs = bdrv_open("null-co://", NULL, NULL, BDRV_O_RDWR | BDRV_O_PROTOCOL,
918 &error_abort);
919 bdrv_attach_child(bs, null_bs, "null-child", &child_file, &error_abort);
920
921 /* This child will be the one to pass to requests through to, and
922 * it will stall until a drain occurs */
923 child_bs = bdrv_new_open_driver(&bdrv_test, "child", BDRV_O_RDWR,
924 &error_abort);
925 child_bs->total_sectors = 65536 >> BDRV_SECTOR_BITS;
926 /* Takes our reference to child_bs */
927 tts->wait_child = bdrv_attach_child(bs, child_bs, "wait-child", &child_file,
928 &error_abort);
929
930 /* This child is just there to be deleted
931 * (for detach_instead_of_delete == true) */
932 null_bs = bdrv_open("null-co://", NULL, NULL, BDRV_O_RDWR | BDRV_O_PROTOCOL,
933 &error_abort);
934 bdrv_attach_child(bs, null_bs, "null-child", &child_file, &error_abort);
935
936 blk = blk_new(BLK_PERM_ALL, BLK_PERM_ALL);
937 blk_insert_bs(blk, bs, &error_abort);
938
939 /* Referenced by blk now */
940 bdrv_unref(bs);
941
942 g_assert_cmpint(bs->refcnt, ==, 1);
943 g_assert_cmpint(child_bs->refcnt, ==, 1);
944 g_assert_cmpint(null_bs->refcnt, ==, 1);
945
946
947 dbdd = (TestCoDeleteByDrainData){
948 .blk = blk,
949 .detach_instead_of_delete = detach_instead_of_delete,
950 .done = false,
951 };
952 co = qemu_coroutine_create(test_co_delete_by_drain, &dbdd);
953 qemu_coroutine_enter(co);
954
955 /* Drain the child while the read operation is still pending.
956 * This should result in the operation finishing and
957 * test_co_delete_by_drain() resuming. Thus, @bs will be deleted
958 * and the coroutine will exit while this drain operation is still
959 * in progress. */
ebd31837
KW
960 switch (drain_type) {
961 case BDRV_DRAIN:
962 bdrv_ref(child_bs);
963 bdrv_drain(child_bs);
964 bdrv_unref(child_bs);
965 break;
966 case BDRV_SUBTREE_DRAIN:
967 /* Would have to ref/unref bs here for !detach_instead_of_delete, but
968 * then the whole test becomes pointless because the graph changes
969 * don't occur during the drain any more. */
970 assert(detach_instead_of_delete);
971 bdrv_subtree_drained_begin(bs);
972 bdrv_subtree_drained_end(bs);
973 break;
974 default:
975 g_assert_not_reached();
976 }
4c8158e3
HR
977
978 while (!dbdd.done) {
979 aio_poll(qemu_get_aio_context(), true);
980 }
981
982 if (detach_instead_of_delete) {
983 /* Here, the reference has not passed over to the coroutine,
984 * so we have to delete the BB ourselves */
985 blk_unref(blk);
986 }
987}
988
4c8158e3
HR
989static void test_delete_by_drain(void)
990{
ebd31837 991 do_test_delete_by_drain(false, BDRV_DRAIN);
4c8158e3
HR
992}
993
994static void test_detach_by_drain(void)
995{
ebd31837
KW
996 do_test_delete_by_drain(true, BDRV_DRAIN);
997}
998
999static void test_detach_by_drain_subtree(void)
1000{
1001 do_test_delete_by_drain(true, BDRV_SUBTREE_DRAIN);
4c8158e3
HR
1002}
1003
1004
231281ab
KW
1005struct detach_by_parent_data {
1006 BlockDriverState *parent_b;
1007 BdrvChild *child_b;
1008 BlockDriverState *c;
1009 BdrvChild *child_c;
57320ca9 1010 bool by_parent_cb;
231281ab 1011};
57320ca9 1012static struct detach_by_parent_data detach_by_parent_data;
231281ab 1013
57320ca9 1014static void detach_indirect_bh(void *opaque)
231281ab
KW
1015{
1016 struct detach_by_parent_data *data = opaque;
1017
231281ab
KW
1018 bdrv_unref_child(data->parent_b, data->child_b);
1019
1020 bdrv_ref(data->c);
1021 data->child_c = bdrv_attach_child(data->parent_b, data->c, "PB-C",
1022 &child_file, &error_abort);
1023}
1024
57320ca9
KW
1025static void detach_by_parent_aio_cb(void *opaque, int ret)
1026{
1027 struct detach_by_parent_data *data = &detach_by_parent_data;
1028
1029 g_assert_cmpint(ret, ==, 0);
1030 if (data->by_parent_cb) {
1031 detach_indirect_bh(data);
1032 }
1033}
1034
1035static void detach_by_driver_cb_drained_begin(BdrvChild *child)
1036{
1037 aio_bh_schedule_oneshot(qemu_get_current_aio_context(),
1038 detach_indirect_bh, &detach_by_parent_data);
1039 child_file.drained_begin(child);
1040}
1041
1042static BdrvChildRole detach_by_driver_cb_role;
1043
231281ab
KW
1044/*
1045 * Initial graph:
1046 *
1047 * PA PB
1048 * \ / \
1049 * A B C
1050 *
57320ca9
KW
1051 * by_parent_cb == true: Test that parent callbacks don't poll
1052 *
1053 * PA has a pending write request whose callback changes the child nodes of
1054 * PB: It removes B and adds C instead. The subtree of PB is drained, which
1055 * will indirectly drain the write request, too.
1056 *
1057 * by_parent_cb == false: Test that bdrv_drain_invoke() doesn't poll
1058 *
1059 * PA's BdrvChildRole has a .drained_begin callback that schedules a BH
1060 * that does the same graph change. If bdrv_drain_invoke() calls it, the
1061 * state is messed up, but if it is only polled in the single
1062 * BDRV_POLL_WHILE() at the end of the drain, this should work fine.
231281ab 1063 */
57320ca9 1064static void test_detach_indirect(bool by_parent_cb)
231281ab
KW
1065{
1066 BlockBackend *blk;
1067 BlockDriverState *parent_a, *parent_b, *a, *b, *c;
1068 BdrvChild *child_a, *child_b;
1069 BlockAIOCB *acb;
231281ab
KW
1070
1071 QEMUIOVector qiov;
1072 struct iovec iov = {
1073 .iov_base = NULL,
1074 .iov_len = 0,
1075 };
1076 qemu_iovec_init_external(&qiov, &iov, 1);
1077
57320ca9
KW
1078 if (!by_parent_cb) {
1079 detach_by_driver_cb_role = child_file;
1080 detach_by_driver_cb_role.drained_begin =
1081 detach_by_driver_cb_drained_begin;
1082 }
1083
231281ab
KW
1084 /* Create all involved nodes */
1085 parent_a = bdrv_new_open_driver(&bdrv_test, "parent-a", BDRV_O_RDWR,
1086 &error_abort);
1087 parent_b = bdrv_new_open_driver(&bdrv_test, "parent-b", 0,
1088 &error_abort);
1089
1090 a = bdrv_new_open_driver(&bdrv_test, "a", BDRV_O_RDWR, &error_abort);
1091 b = bdrv_new_open_driver(&bdrv_test, "b", BDRV_O_RDWR, &error_abort);
1092 c = bdrv_new_open_driver(&bdrv_test, "c", BDRV_O_RDWR, &error_abort);
1093
1094 /* blk is a BB for parent-a */
1095 blk = blk_new(BLK_PERM_ALL, BLK_PERM_ALL);
1096 blk_insert_bs(blk, parent_a, &error_abort);
1097 bdrv_unref(parent_a);
1098
57320ca9
KW
1099 /* If we want to get bdrv_drain_invoke() to call aio_poll(), the driver
1100 * callback must not return immediately. */
1101 if (!by_parent_cb) {
1102 BDRVTestState *s = parent_a->opaque;
1103 s->sleep_in_drain_begin = true;
1104 }
1105
231281ab
KW
1106 /* Set child relationships */
1107 bdrv_ref(b);
1108 bdrv_ref(a);
1109 child_b = bdrv_attach_child(parent_b, b, "PB-B", &child_file, &error_abort);
1110 child_a = bdrv_attach_child(parent_b, a, "PB-A", &child_backing, &error_abort);
1111
1112 bdrv_ref(a);
57320ca9
KW
1113 bdrv_attach_child(parent_a, a, "PA-A",
1114 by_parent_cb ? &child_file : &detach_by_driver_cb_role,
1115 &error_abort);
231281ab
KW
1116
1117 g_assert_cmpint(parent_a->refcnt, ==, 1);
1118 g_assert_cmpint(parent_b->refcnt, ==, 1);
1119 g_assert_cmpint(a->refcnt, ==, 3);
1120 g_assert_cmpint(b->refcnt, ==, 2);
1121 g_assert_cmpint(c->refcnt, ==, 1);
1122
1123 g_assert(QLIST_FIRST(&parent_b->children) == child_a);
1124 g_assert(QLIST_NEXT(child_a, next) == child_b);
1125 g_assert(QLIST_NEXT(child_b, next) == NULL);
1126
1127 /* Start the evil write request */
57320ca9 1128 detach_by_parent_data = (struct detach_by_parent_data) {
231281ab
KW
1129 .parent_b = parent_b,
1130 .child_b = child_b,
1131 .c = c,
57320ca9 1132 .by_parent_cb = by_parent_cb,
231281ab 1133 };
57320ca9 1134 acb = blk_aio_preadv(blk, 0, &qiov, 0, detach_by_parent_aio_cb, NULL);
231281ab
KW
1135 g_assert(acb != NULL);
1136
1137 /* Drain and check the expected result */
1138 bdrv_subtree_drained_begin(parent_b);
1139
57320ca9 1140 g_assert(detach_by_parent_data.child_c != NULL);
231281ab
KW
1141
1142 g_assert_cmpint(parent_a->refcnt, ==, 1);
1143 g_assert_cmpint(parent_b->refcnt, ==, 1);
1144 g_assert_cmpint(a->refcnt, ==, 3);
1145 g_assert_cmpint(b->refcnt, ==, 1);
1146 g_assert_cmpint(c->refcnt, ==, 2);
1147
57320ca9
KW
1148 g_assert(QLIST_FIRST(&parent_b->children) == detach_by_parent_data.child_c);
1149 g_assert(QLIST_NEXT(detach_by_parent_data.child_c, next) == child_a);
231281ab
KW
1150 g_assert(QLIST_NEXT(child_a, next) == NULL);
1151
1152 g_assert_cmpint(parent_a->quiesce_counter, ==, 1);
1153 g_assert_cmpint(parent_b->quiesce_counter, ==, 1);
1154 g_assert_cmpint(a->quiesce_counter, ==, 1);
1155 g_assert_cmpint(b->quiesce_counter, ==, 0);
1156 g_assert_cmpint(c->quiesce_counter, ==, 1);
1157
1158 bdrv_subtree_drained_end(parent_b);
1159
1160 bdrv_unref(parent_b);
1161 blk_unref(blk);
1162
1163 /* XXX Once bdrv_close() unref's children instead of just detaching them,
1164 * this won't be necessary any more. */
1165 bdrv_unref(a);
1166 bdrv_unref(a);
1167 bdrv_unref(c);
1168
1169 g_assert_cmpint(a->refcnt, ==, 1);
1170 g_assert_cmpint(b->refcnt, ==, 1);
1171 g_assert_cmpint(c->refcnt, ==, 1);
1172 bdrv_unref(a);
1173 bdrv_unref(b);
1174 bdrv_unref(c);
1175}
1176
57320ca9
KW
1177static void test_detach_by_parent_cb(void)
1178{
1179 test_detach_indirect(true);
1180}
1181
1182static void test_detach_by_driver_cb(void)
1183{
1184 test_detach_indirect(false);
1185}
231281ab 1186
881cfd17
KW
1187int main(int argc, char **argv)
1188{
bb675689
KW
1189 int ret;
1190
881cfd17
KW
1191 bdrv_init();
1192 qemu_init_main_loop(&error_abort);
1193
1194 g_test_init(&argc, &argv, NULL);
bb675689 1195 qemu_event_init(&done_event, false);
881cfd17
KW
1196
1197 g_test_add_func("/bdrv-drain/driver-cb/drain_all", test_drv_cb_drain_all);
86e1c840 1198 g_test_add_func("/bdrv-drain/driver-cb/drain", test_drv_cb_drain);
d2a85d0f
KW
1199 g_test_add_func("/bdrv-drain/driver-cb/drain_subtree",
1200 test_drv_cb_drain_subtree);
881cfd17 1201
6d0252f2
KW
1202 g_test_add_func("/bdrv-drain/driver-cb/co/drain_all",
1203 test_drv_cb_co_drain_all);
0582eb10
KW
1204 g_test_add_func("/bdrv-drain/driver-cb/co/drain", test_drv_cb_co_drain);
1205 g_test_add_func("/bdrv-drain/driver-cb/co/drain_subtree",
1206 test_drv_cb_co_drain_subtree);
1207
1208
89a6ceab
KW
1209 g_test_add_func("/bdrv-drain/quiesce/drain_all", test_quiesce_drain_all);
1210 g_test_add_func("/bdrv-drain/quiesce/drain", test_quiesce_drain);
d2a85d0f
KW
1211 g_test_add_func("/bdrv-drain/quiesce/drain_subtree",
1212 test_quiesce_drain_subtree);
89a6ceab 1213
6d0252f2
KW
1214 g_test_add_func("/bdrv-drain/quiesce/co/drain_all",
1215 test_quiesce_co_drain_all);
0582eb10
KW
1216 g_test_add_func("/bdrv-drain/quiesce/co/drain", test_quiesce_co_drain);
1217 g_test_add_func("/bdrv-drain/quiesce/co/drain_subtree",
1218 test_quiesce_co_drain_subtree);
1219
6c429a6a 1220 g_test_add_func("/bdrv-drain/nested", test_nested);
27e64474 1221 g_test_add_func("/bdrv-drain/multiparent", test_multiparent);
acebcf8d 1222 g_test_add_func("/bdrv-drain/graph-change", test_graph_change);
6c429a6a 1223
bb675689
KW
1224 g_test_add_func("/bdrv-drain/iothread/drain_all", test_iothread_drain_all);
1225 g_test_add_func("/bdrv-drain/iothread/drain", test_iothread_drain);
1226 g_test_add_func("/bdrv-drain/iothread/drain_subtree",
1227 test_iothread_drain_subtree);
1228
7253220d
KW
1229 g_test_add_func("/bdrv-drain/blockjob/drain_all", test_blockjob_drain_all);
1230 g_test_add_func("/bdrv-drain/blockjob/drain", test_blockjob_drain);
d2a85d0f
KW
1231 g_test_add_func("/bdrv-drain/blockjob/drain_subtree",
1232 test_blockjob_drain_subtree);
7253220d 1233
ebd31837
KW
1234 g_test_add_func("/bdrv-drain/deletion/drain", test_delete_by_drain);
1235 g_test_add_func("/bdrv-drain/detach/drain", test_detach_by_drain);
1236 g_test_add_func("/bdrv-drain/detach/drain_subtree", test_detach_by_drain_subtree);
231281ab 1237 g_test_add_func("/bdrv-drain/detach/parent_cb", test_detach_by_parent_cb);
57320ca9 1238 g_test_add_func("/bdrv-drain/detach/driver_cb", test_detach_by_driver_cb);
4c8158e3 1239
bb675689
KW
1240 ret = g_test_run();
1241 qemu_event_destroy(&done_event);
1242 return ret;
881cfd17 1243}