]> git.proxmox.com Git - mirror_qemu.git/blame - tests/test-bdrv-drain.c
Merge remote-tracking branch 'remotes/armbru/tags/pull-error-2018-08-27-v2' into...
[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
19f7a7e5 455static void test_graph_change_drain_subtree(void)
acebcf8d
KW
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
19f7a7e5
KW
534static void test_graph_change_drain_all(void)
535{
536 BlockBackend *blk_a, *blk_b;
537 BlockDriverState *bs_a, *bs_b;
538 BDRVTestState *a_s, *b_s;
539
540 /* Create node A with a BlockBackend */
541 blk_a = blk_new(BLK_PERM_ALL, BLK_PERM_ALL);
542 bs_a = bdrv_new_open_driver(&bdrv_test, "test-node-a", BDRV_O_RDWR,
543 &error_abort);
544 a_s = bs_a->opaque;
545 blk_insert_bs(blk_a, bs_a, &error_abort);
546
547 g_assert_cmpint(bs_a->quiesce_counter, ==, 0);
548 g_assert_cmpint(a_s->drain_count, ==, 0);
549
550 /* Call bdrv_drain_all_begin() */
551 bdrv_drain_all_begin();
552
553 g_assert_cmpint(bs_a->quiesce_counter, ==, 1);
554 g_assert_cmpint(a_s->drain_count, ==, 1);
555
556 /* Create node B with a BlockBackend */
557 blk_b = blk_new(BLK_PERM_ALL, BLK_PERM_ALL);
558 bs_b = bdrv_new_open_driver(&bdrv_test, "test-node-b", BDRV_O_RDWR,
559 &error_abort);
560 b_s = bs_b->opaque;
561 blk_insert_bs(blk_b, bs_b, &error_abort);
562
563 g_assert_cmpint(bs_a->quiesce_counter, ==, 1);
564 g_assert_cmpint(bs_b->quiesce_counter, ==, 1);
565 g_assert_cmpint(a_s->drain_count, ==, 1);
566 g_assert_cmpint(b_s->drain_count, ==, 1);
567
568 /* Unref and finally delete node A */
569 blk_unref(blk_a);
570
571 g_assert_cmpint(bs_a->quiesce_counter, ==, 1);
572 g_assert_cmpint(bs_b->quiesce_counter, ==, 1);
573 g_assert_cmpint(a_s->drain_count, ==, 1);
574 g_assert_cmpint(b_s->drain_count, ==, 1);
575
576 bdrv_unref(bs_a);
577
578 g_assert_cmpint(bs_b->quiesce_counter, ==, 1);
579 g_assert_cmpint(b_s->drain_count, ==, 1);
580
581 /* End the drained section */
582 bdrv_drain_all_end();
583
584 g_assert_cmpint(bs_b->quiesce_counter, ==, 0);
585 g_assert_cmpint(b_s->drain_count, ==, 0);
586
587 bdrv_unref(bs_b);
588 blk_unref(blk_b);
589}
590
bb675689
KW
591struct test_iothread_data {
592 BlockDriverState *bs;
593 enum drain_type drain_type;
594 int *aio_ret;
595};
596
597static void test_iothread_drain_entry(void *opaque)
598{
599 struct test_iothread_data *data = opaque;
600
601 aio_context_acquire(bdrv_get_aio_context(data->bs));
602 do_drain_begin(data->drain_type, data->bs);
603 g_assert_cmpint(*data->aio_ret, ==, 0);
604 do_drain_end(data->drain_type, data->bs);
605 aio_context_release(bdrv_get_aio_context(data->bs));
606
607 qemu_event_set(&done_event);
608}
609
610static void test_iothread_aio_cb(void *opaque, int ret)
611{
612 int *aio_ret = opaque;
613 *aio_ret = ret;
614 qemu_event_set(&done_event);
615}
616
617/*
618 * Starts an AIO request on a BDS that runs in the AioContext of iothread 1.
619 * The request involves a BH on iothread 2 before it can complete.
620 *
621 * @drain_thread = 0 means that do_drain_begin/end are called from the main
622 * thread, @drain_thread = 1 means that they are called from iothread 1. Drain
623 * for this BDS cannot be called from iothread 2 because only the main thread
624 * may do cross-AioContext polling.
625 */
626static void test_iothread_common(enum drain_type drain_type, int drain_thread)
627{
628 BlockBackend *blk;
629 BlockDriverState *bs;
630 BDRVTestState *s;
631 BlockAIOCB *acb;
632 int aio_ret;
633 struct test_iothread_data data;
634
635 IOThread *a = iothread_new();
636 IOThread *b = iothread_new();
637 AioContext *ctx_a = iothread_get_aio_context(a);
638 AioContext *ctx_b = iothread_get_aio_context(b);
639
640 QEMUIOVector qiov;
641 struct iovec iov = {
642 .iov_base = NULL,
643 .iov_len = 0,
644 };
645 qemu_iovec_init_external(&qiov, &iov, 1);
646
647 /* bdrv_drain_all() may only be called from the main loop thread */
648 if (drain_type == BDRV_DRAIN_ALL && drain_thread != 0) {
649 goto out;
650 }
651
652 blk = blk_new(BLK_PERM_ALL, BLK_PERM_ALL);
653 bs = bdrv_new_open_driver(&bdrv_test, "test-node", BDRV_O_RDWR,
654 &error_abort);
655 s = bs->opaque;
656 blk_insert_bs(blk, bs, &error_abort);
657
658 blk_set_aio_context(blk, ctx_a);
659 aio_context_acquire(ctx_a);
660
661 s->bh_indirection_ctx = ctx_b;
662
663 aio_ret = -EINPROGRESS;
664 if (drain_thread == 0) {
665 acb = blk_aio_preadv(blk, 0, &qiov, 0, test_iothread_aio_cb, &aio_ret);
666 } else {
667 acb = blk_aio_preadv(blk, 0, &qiov, 0, aio_ret_cb, &aio_ret);
668 }
669 g_assert(acb != NULL);
670 g_assert_cmpint(aio_ret, ==, -EINPROGRESS);
671
672 aio_context_release(ctx_a);
673
674 data = (struct test_iothread_data) {
675 .bs = bs,
676 .drain_type = drain_type,
677 .aio_ret = &aio_ret,
678 };
679
680 switch (drain_thread) {
681 case 0:
682 if (drain_type != BDRV_DRAIN_ALL) {
683 aio_context_acquire(ctx_a);
684 }
685
686 /* The request is running on the IOThread a. Draining its block device
687 * will make sure that it has completed as far as the BDS is concerned,
688 * but the drain in this thread can continue immediately after
689 * bdrv_dec_in_flight() and aio_ret might be assigned only slightly
690 * later. */
691 qemu_event_reset(&done_event);
692 do_drain_begin(drain_type, bs);
693 g_assert_cmpint(bs->in_flight, ==, 0);
694
695 if (drain_type != BDRV_DRAIN_ALL) {
696 aio_context_release(ctx_a);
697 }
698 qemu_event_wait(&done_event);
699 if (drain_type != BDRV_DRAIN_ALL) {
700 aio_context_acquire(ctx_a);
701 }
702
703 g_assert_cmpint(aio_ret, ==, 0);
704 do_drain_end(drain_type, bs);
705
706 if (drain_type != BDRV_DRAIN_ALL) {
707 aio_context_release(ctx_a);
708 }
709 break;
710 case 1:
711 qemu_event_reset(&done_event);
712 aio_bh_schedule_oneshot(ctx_a, test_iothread_drain_entry, &data);
713 qemu_event_wait(&done_event);
714 break;
715 default:
716 g_assert_not_reached();
717 }
718
719 aio_context_acquire(ctx_a);
720 blk_set_aio_context(blk, qemu_get_aio_context());
721 aio_context_release(ctx_a);
722
723 bdrv_unref(bs);
724 blk_unref(blk);
725
726out:
727 iothread_join(a);
728 iothread_join(b);
729}
730
731static void test_iothread_drain_all(void)
732{
733 test_iothread_common(BDRV_DRAIN_ALL, 0);
734 test_iothread_common(BDRV_DRAIN_ALL, 1);
735}
736
737static void test_iothread_drain(void)
738{
739 test_iothread_common(BDRV_DRAIN, 0);
740 test_iothread_common(BDRV_DRAIN, 1);
741}
742
743static void test_iothread_drain_subtree(void)
744{
745 test_iothread_common(BDRV_SUBTREE_DRAIN, 0);
746 test_iothread_common(BDRV_SUBTREE_DRAIN, 1);
747}
748
7253220d
KW
749
750typedef struct TestBlockJob {
751 BlockJob common;
752 bool should_complete;
753} TestBlockJob;
754
1908a559 755static void test_job_completed(Job *job, void *opaque)
7253220d 756{
1266c9b9 757 job_completed(job, 0, NULL);
7253220d
KW
758}
759
760static void coroutine_fn test_job_start(void *opaque)
761{
762 TestBlockJob *s = opaque;
763
2e1795b5 764 job_transition_to_ready(&s->common.job);
7253220d 765 while (!s->should_complete) {
89bd0305
KW
766 /* Avoid block_job_sleep_ns() because it marks the job as !busy. We
767 * want to emulate some actual activity (probably some I/O) here so
768 * that drain has to wait for this acitivity to stop. */
769 qemu_co_sleep_ns(QEMU_CLOCK_REALTIME, 100000);
770 job_pause_point(&s->common.job);
7253220d
KW
771 }
772
1908a559 773 job_defer_to_main_loop(&s->common.job, test_job_completed, NULL);
7253220d
KW
774}
775
3453d972 776static void test_job_complete(Job *job, Error **errp)
7253220d 777{
3453d972 778 TestBlockJob *s = container_of(job, TestBlockJob, common.job);
7253220d
KW
779 s->should_complete = true;
780}
781
782BlockJobDriver test_job_driver = {
33e9e9bd
KW
783 .job_driver = {
784 .instance_size = sizeof(TestBlockJob),
80fa2c75 785 .free = block_job_free,
b15de828 786 .user_resume = block_job_user_resume,
b69f777d 787 .drain = block_job_drain,
da01ff7f 788 .start = test_job_start,
3453d972 789 .complete = test_job_complete,
33e9e9bd 790 },
7253220d
KW
791};
792
793static void test_blockjob_common(enum drain_type drain_type)
794{
795 BlockBackend *blk_src, *blk_target;
796 BlockDriverState *src, *target;
797 BlockJob *job;
798 int ret;
799
800 src = bdrv_new_open_driver(&bdrv_test, "source", BDRV_O_RDWR,
801 &error_abort);
802 blk_src = blk_new(BLK_PERM_ALL, BLK_PERM_ALL);
803 blk_insert_bs(blk_src, src, &error_abort);
804
805 target = bdrv_new_open_driver(&bdrv_test, "target", BDRV_O_RDWR,
806 &error_abort);
807 blk_target = blk_new(BLK_PERM_ALL, BLK_PERM_ALL);
808 blk_insert_bs(blk_target, target, &error_abort);
809
75859b94
JS
810 job = block_job_create("job0", &test_job_driver, NULL, src, 0, BLK_PERM_ALL,
811 0, 0, NULL, NULL, &error_abort);
7253220d 812 block_job_add_bdrv(job, "target", target, 0, BLK_PERM_ALL, &error_abort);
da01ff7f 813 job_start(&job->job);
7253220d 814
da01ff7f
KW
815 g_assert_cmpint(job->job.pause_count, ==, 0);
816 g_assert_false(job->job.paused);
89bd0305 817 g_assert_true(job->job.busy); /* We're in job_sleep_ns() */
7253220d
KW
818
819 do_drain_begin(drain_type, src);
820
821 if (drain_type == BDRV_DRAIN_ALL) {
81193349 822 /* bdrv_drain_all() drains both src and target */
da01ff7f 823 g_assert_cmpint(job->job.pause_count, ==, 2);
7253220d 824 } else {
da01ff7f 825 g_assert_cmpint(job->job.pause_count, ==, 1);
7253220d 826 }
89bd0305 827 g_assert_true(job->job.paused);
da01ff7f 828 g_assert_false(job->job.busy); /* The job is paused */
7253220d
KW
829
830 do_drain_end(drain_type, src);
831
da01ff7f
KW
832 g_assert_cmpint(job->job.pause_count, ==, 0);
833 g_assert_false(job->job.paused);
89bd0305 834 g_assert_true(job->job.busy); /* We're in qemu_co_sleep_ns() */
7253220d
KW
835
836 do_drain_begin(drain_type, target);
837
838 if (drain_type == BDRV_DRAIN_ALL) {
81193349 839 /* bdrv_drain_all() drains both src and target */
da01ff7f 840 g_assert_cmpint(job->job.pause_count, ==, 2);
7253220d 841 } else {
da01ff7f 842 g_assert_cmpint(job->job.pause_count, ==, 1);
7253220d 843 }
89bd0305 844 g_assert_true(job->job.paused);
da01ff7f 845 g_assert_false(job->job.busy); /* The job is paused */
7253220d
KW
846
847 do_drain_end(drain_type, target);
848
da01ff7f
KW
849 g_assert_cmpint(job->job.pause_count, ==, 0);
850 g_assert_false(job->job.paused);
89bd0305 851 g_assert_true(job->job.busy); /* We're in job_sleep_ns() */
7253220d 852
3d70ff53 853 ret = job_complete_sync(&job->job, &error_abort);
7253220d
KW
854 g_assert_cmpint(ret, ==, 0);
855
856 blk_unref(blk_src);
857 blk_unref(blk_target);
858 bdrv_unref(src);
859 bdrv_unref(target);
860}
861
862static void test_blockjob_drain_all(void)
863{
864 test_blockjob_common(BDRV_DRAIN_ALL);
865}
866
867static void test_blockjob_drain(void)
868{
869 test_blockjob_common(BDRV_DRAIN);
870}
871
d2a85d0f
KW
872static void test_blockjob_drain_subtree(void)
873{
874 test_blockjob_common(BDRV_SUBTREE_DRAIN);
875}
876
4c8158e3
HR
877
878typedef struct BDRVTestTopState {
879 BdrvChild *wait_child;
880} BDRVTestTopState;
881
882static void bdrv_test_top_close(BlockDriverState *bs)
883{
884 BdrvChild *c, *next_c;
885 QLIST_FOREACH_SAFE(c, &bs->children, next, next_c) {
886 bdrv_unref_child(bs, c);
887 }
888}
889
890static int coroutine_fn bdrv_test_top_co_preadv(BlockDriverState *bs,
891 uint64_t offset, uint64_t bytes,
892 QEMUIOVector *qiov, int flags)
893{
894 BDRVTestTopState *tts = bs->opaque;
895 return bdrv_co_preadv(tts->wait_child, offset, bytes, qiov, flags);
896}
897
898static BlockDriver bdrv_test_top_driver = {
899 .format_name = "test_top_driver",
900 .instance_size = sizeof(BDRVTestTopState),
901
902 .bdrv_close = bdrv_test_top_close,
903 .bdrv_co_preadv = bdrv_test_top_co_preadv,
904
905 .bdrv_child_perm = bdrv_format_default_perms,
906};
907
908typedef struct TestCoDeleteByDrainData {
909 BlockBackend *blk;
910 bool detach_instead_of_delete;
911 bool done;
912} TestCoDeleteByDrainData;
913
914static void coroutine_fn test_co_delete_by_drain(void *opaque)
915{
916 TestCoDeleteByDrainData *dbdd = opaque;
917 BlockBackend *blk = dbdd->blk;
918 BlockDriverState *bs = blk_bs(blk);
919 BDRVTestTopState *tts = bs->opaque;
920 void *buffer = g_malloc(65536);
921 QEMUIOVector qiov;
922 struct iovec iov = {
923 .iov_base = buffer,
924 .iov_len = 65536,
925 };
926
927 qemu_iovec_init_external(&qiov, &iov, 1);
928
929 /* Pretend some internal write operation from parent to child.
930 * Important: We have to read from the child, not from the parent!
931 * Draining works by first propagating it all up the tree to the
932 * root and then waiting for drainage from root to the leaves
933 * (protocol nodes). If we have a request waiting on the root,
934 * everything will be drained before we go back down the tree, but
935 * we do not want that. We want to be in the middle of draining
936 * when this following requests returns. */
937 bdrv_co_preadv(tts->wait_child, 0, 65536, &qiov, 0);
938
939 g_assert_cmpint(bs->refcnt, ==, 1);
940
941 if (!dbdd->detach_instead_of_delete) {
942 blk_unref(blk);
943 } else {
944 BdrvChild *c, *next_c;
945 QLIST_FOREACH_SAFE(c, &bs->children, next, next_c) {
946 bdrv_unref_child(bs, c);
947 }
948 }
949
950 dbdd->done = true;
951}
952
953/**
954 * Test what happens when some BDS has some children, you drain one of
955 * them and this results in the BDS being deleted.
956 *
957 * If @detach_instead_of_delete is set, the BDS is not going to be
958 * deleted but will only detach all of its children.
959 */
ebd31837
KW
960static void do_test_delete_by_drain(bool detach_instead_of_delete,
961 enum drain_type drain_type)
4c8158e3
HR
962{
963 BlockBackend *blk;
964 BlockDriverState *bs, *child_bs, *null_bs;
965 BDRVTestTopState *tts;
966 TestCoDeleteByDrainData dbdd;
967 Coroutine *co;
968
969 bs = bdrv_new_open_driver(&bdrv_test_top_driver, "top", BDRV_O_RDWR,
970 &error_abort);
971 bs->total_sectors = 65536 >> BDRV_SECTOR_BITS;
972 tts = bs->opaque;
973
974 null_bs = bdrv_open("null-co://", NULL, NULL, BDRV_O_RDWR | BDRV_O_PROTOCOL,
975 &error_abort);
976 bdrv_attach_child(bs, null_bs, "null-child", &child_file, &error_abort);
977
978 /* This child will be the one to pass to requests through to, and
979 * it will stall until a drain occurs */
980 child_bs = bdrv_new_open_driver(&bdrv_test, "child", BDRV_O_RDWR,
981 &error_abort);
982 child_bs->total_sectors = 65536 >> BDRV_SECTOR_BITS;
983 /* Takes our reference to child_bs */
984 tts->wait_child = bdrv_attach_child(bs, child_bs, "wait-child", &child_file,
985 &error_abort);
986
987 /* This child is just there to be deleted
988 * (for detach_instead_of_delete == true) */
989 null_bs = bdrv_open("null-co://", NULL, NULL, BDRV_O_RDWR | BDRV_O_PROTOCOL,
990 &error_abort);
991 bdrv_attach_child(bs, null_bs, "null-child", &child_file, &error_abort);
992
993 blk = blk_new(BLK_PERM_ALL, BLK_PERM_ALL);
994 blk_insert_bs(blk, bs, &error_abort);
995
996 /* Referenced by blk now */
997 bdrv_unref(bs);
998
999 g_assert_cmpint(bs->refcnt, ==, 1);
1000 g_assert_cmpint(child_bs->refcnt, ==, 1);
1001 g_assert_cmpint(null_bs->refcnt, ==, 1);
1002
1003
1004 dbdd = (TestCoDeleteByDrainData){
1005 .blk = blk,
1006 .detach_instead_of_delete = detach_instead_of_delete,
1007 .done = false,
1008 };
1009 co = qemu_coroutine_create(test_co_delete_by_drain, &dbdd);
1010 qemu_coroutine_enter(co);
1011
1012 /* Drain the child while the read operation is still pending.
1013 * This should result in the operation finishing and
1014 * test_co_delete_by_drain() resuming. Thus, @bs will be deleted
1015 * and the coroutine will exit while this drain operation is still
1016 * in progress. */
ebd31837
KW
1017 switch (drain_type) {
1018 case BDRV_DRAIN:
1019 bdrv_ref(child_bs);
1020 bdrv_drain(child_bs);
1021 bdrv_unref(child_bs);
1022 break;
1023 case BDRV_SUBTREE_DRAIN:
1024 /* Would have to ref/unref bs here for !detach_instead_of_delete, but
1025 * then the whole test becomes pointless because the graph changes
1026 * don't occur during the drain any more. */
1027 assert(detach_instead_of_delete);
1028 bdrv_subtree_drained_begin(bs);
1029 bdrv_subtree_drained_end(bs);
1030 break;
19f7a7e5
KW
1031 case BDRV_DRAIN_ALL:
1032 bdrv_drain_all_begin();
1033 bdrv_drain_all_end();
1034 break;
ebd31837
KW
1035 default:
1036 g_assert_not_reached();
1037 }
4c8158e3
HR
1038
1039 while (!dbdd.done) {
1040 aio_poll(qemu_get_aio_context(), true);
1041 }
1042
1043 if (detach_instead_of_delete) {
1044 /* Here, the reference has not passed over to the coroutine,
1045 * so we have to delete the BB ourselves */
1046 blk_unref(blk);
1047 }
1048}
1049
4c8158e3
HR
1050static void test_delete_by_drain(void)
1051{
ebd31837 1052 do_test_delete_by_drain(false, BDRV_DRAIN);
4c8158e3
HR
1053}
1054
19f7a7e5
KW
1055static void test_detach_by_drain_all(void)
1056{
1057 do_test_delete_by_drain(true, BDRV_DRAIN_ALL);
1058}
1059
4c8158e3
HR
1060static void test_detach_by_drain(void)
1061{
ebd31837
KW
1062 do_test_delete_by_drain(true, BDRV_DRAIN);
1063}
1064
1065static void test_detach_by_drain_subtree(void)
1066{
1067 do_test_delete_by_drain(true, BDRV_SUBTREE_DRAIN);
4c8158e3
HR
1068}
1069
1070
231281ab
KW
1071struct detach_by_parent_data {
1072 BlockDriverState *parent_b;
1073 BdrvChild *child_b;
1074 BlockDriverState *c;
1075 BdrvChild *child_c;
57320ca9 1076 bool by_parent_cb;
231281ab 1077};
57320ca9 1078static struct detach_by_parent_data detach_by_parent_data;
231281ab 1079
57320ca9 1080static void detach_indirect_bh(void *opaque)
231281ab
KW
1081{
1082 struct detach_by_parent_data *data = opaque;
1083
231281ab
KW
1084 bdrv_unref_child(data->parent_b, data->child_b);
1085
1086 bdrv_ref(data->c);
1087 data->child_c = bdrv_attach_child(data->parent_b, data->c, "PB-C",
1088 &child_file, &error_abort);
1089}
1090
57320ca9
KW
1091static void detach_by_parent_aio_cb(void *opaque, int ret)
1092{
1093 struct detach_by_parent_data *data = &detach_by_parent_data;
1094
1095 g_assert_cmpint(ret, ==, 0);
1096 if (data->by_parent_cb) {
1097 detach_indirect_bh(data);
1098 }
1099}
1100
1101static void detach_by_driver_cb_drained_begin(BdrvChild *child)
1102{
1103 aio_bh_schedule_oneshot(qemu_get_current_aio_context(),
1104 detach_indirect_bh, &detach_by_parent_data);
1105 child_file.drained_begin(child);
1106}
1107
1108static BdrvChildRole detach_by_driver_cb_role;
1109
231281ab
KW
1110/*
1111 * Initial graph:
1112 *
1113 * PA PB
1114 * \ / \
1115 * A B C
1116 *
57320ca9
KW
1117 * by_parent_cb == true: Test that parent callbacks don't poll
1118 *
1119 * PA has a pending write request whose callback changes the child nodes of
1120 * PB: It removes B and adds C instead. The subtree of PB is drained, which
1121 * will indirectly drain the write request, too.
1122 *
1123 * by_parent_cb == false: Test that bdrv_drain_invoke() doesn't poll
1124 *
1125 * PA's BdrvChildRole has a .drained_begin callback that schedules a BH
1126 * that does the same graph change. If bdrv_drain_invoke() calls it, the
1127 * state is messed up, but if it is only polled in the single
1128 * BDRV_POLL_WHILE() at the end of the drain, this should work fine.
231281ab 1129 */
57320ca9 1130static void test_detach_indirect(bool by_parent_cb)
231281ab
KW
1131{
1132 BlockBackend *blk;
1133 BlockDriverState *parent_a, *parent_b, *a, *b, *c;
1134 BdrvChild *child_a, *child_b;
1135 BlockAIOCB *acb;
231281ab
KW
1136
1137 QEMUIOVector qiov;
1138 struct iovec iov = {
1139 .iov_base = NULL,
1140 .iov_len = 0,
1141 };
1142 qemu_iovec_init_external(&qiov, &iov, 1);
1143
57320ca9
KW
1144 if (!by_parent_cb) {
1145 detach_by_driver_cb_role = child_file;
1146 detach_by_driver_cb_role.drained_begin =
1147 detach_by_driver_cb_drained_begin;
1148 }
1149
231281ab
KW
1150 /* Create all involved nodes */
1151 parent_a = bdrv_new_open_driver(&bdrv_test, "parent-a", BDRV_O_RDWR,
1152 &error_abort);
1153 parent_b = bdrv_new_open_driver(&bdrv_test, "parent-b", 0,
1154 &error_abort);
1155
1156 a = bdrv_new_open_driver(&bdrv_test, "a", BDRV_O_RDWR, &error_abort);
1157 b = bdrv_new_open_driver(&bdrv_test, "b", BDRV_O_RDWR, &error_abort);
1158 c = bdrv_new_open_driver(&bdrv_test, "c", BDRV_O_RDWR, &error_abort);
1159
1160 /* blk is a BB for parent-a */
1161 blk = blk_new(BLK_PERM_ALL, BLK_PERM_ALL);
1162 blk_insert_bs(blk, parent_a, &error_abort);
1163 bdrv_unref(parent_a);
1164
57320ca9
KW
1165 /* If we want to get bdrv_drain_invoke() to call aio_poll(), the driver
1166 * callback must not return immediately. */
1167 if (!by_parent_cb) {
1168 BDRVTestState *s = parent_a->opaque;
1169 s->sleep_in_drain_begin = true;
1170 }
1171
231281ab
KW
1172 /* Set child relationships */
1173 bdrv_ref(b);
1174 bdrv_ref(a);
1175 child_b = bdrv_attach_child(parent_b, b, "PB-B", &child_file, &error_abort);
1176 child_a = bdrv_attach_child(parent_b, a, "PB-A", &child_backing, &error_abort);
1177
1178 bdrv_ref(a);
57320ca9
KW
1179 bdrv_attach_child(parent_a, a, "PA-A",
1180 by_parent_cb ? &child_file : &detach_by_driver_cb_role,
1181 &error_abort);
231281ab
KW
1182
1183 g_assert_cmpint(parent_a->refcnt, ==, 1);
1184 g_assert_cmpint(parent_b->refcnt, ==, 1);
1185 g_assert_cmpint(a->refcnt, ==, 3);
1186 g_assert_cmpint(b->refcnt, ==, 2);
1187 g_assert_cmpint(c->refcnt, ==, 1);
1188
1189 g_assert(QLIST_FIRST(&parent_b->children) == child_a);
1190 g_assert(QLIST_NEXT(child_a, next) == child_b);
1191 g_assert(QLIST_NEXT(child_b, next) == NULL);
1192
1193 /* Start the evil write request */
57320ca9 1194 detach_by_parent_data = (struct detach_by_parent_data) {
231281ab
KW
1195 .parent_b = parent_b,
1196 .child_b = child_b,
1197 .c = c,
57320ca9 1198 .by_parent_cb = by_parent_cb,
231281ab 1199 };
57320ca9 1200 acb = blk_aio_preadv(blk, 0, &qiov, 0, detach_by_parent_aio_cb, NULL);
231281ab
KW
1201 g_assert(acb != NULL);
1202
1203 /* Drain and check the expected result */
1204 bdrv_subtree_drained_begin(parent_b);
1205
57320ca9 1206 g_assert(detach_by_parent_data.child_c != NULL);
231281ab
KW
1207
1208 g_assert_cmpint(parent_a->refcnt, ==, 1);
1209 g_assert_cmpint(parent_b->refcnt, ==, 1);
1210 g_assert_cmpint(a->refcnt, ==, 3);
1211 g_assert_cmpint(b->refcnt, ==, 1);
1212 g_assert_cmpint(c->refcnt, ==, 2);
1213
57320ca9
KW
1214 g_assert(QLIST_FIRST(&parent_b->children) == detach_by_parent_data.child_c);
1215 g_assert(QLIST_NEXT(detach_by_parent_data.child_c, next) == child_a);
231281ab
KW
1216 g_assert(QLIST_NEXT(child_a, next) == NULL);
1217
1218 g_assert_cmpint(parent_a->quiesce_counter, ==, 1);
1219 g_assert_cmpint(parent_b->quiesce_counter, ==, 1);
1220 g_assert_cmpint(a->quiesce_counter, ==, 1);
1221 g_assert_cmpint(b->quiesce_counter, ==, 0);
1222 g_assert_cmpint(c->quiesce_counter, ==, 1);
1223
1224 bdrv_subtree_drained_end(parent_b);
1225
1226 bdrv_unref(parent_b);
1227 blk_unref(blk);
1228
1229 /* XXX Once bdrv_close() unref's children instead of just detaching them,
1230 * this won't be necessary any more. */
1231 bdrv_unref(a);
1232 bdrv_unref(a);
1233 bdrv_unref(c);
1234
1235 g_assert_cmpint(a->refcnt, ==, 1);
1236 g_assert_cmpint(b->refcnt, ==, 1);
1237 g_assert_cmpint(c->refcnt, ==, 1);
1238 bdrv_unref(a);
1239 bdrv_unref(b);
1240 bdrv_unref(c);
1241}
1242
57320ca9
KW
1243static void test_detach_by_parent_cb(void)
1244{
1245 test_detach_indirect(true);
1246}
1247
1248static void test_detach_by_driver_cb(void)
1249{
1250 test_detach_indirect(false);
1251}
231281ab 1252
b994c5bc
KW
1253static void test_append_to_drained(void)
1254{
1255 BlockBackend *blk;
1256 BlockDriverState *base, *overlay;
1257 BDRVTestState *base_s, *overlay_s;
1258
1259 blk = blk_new(BLK_PERM_ALL, BLK_PERM_ALL);
1260 base = bdrv_new_open_driver(&bdrv_test, "base", BDRV_O_RDWR, &error_abort);
1261 base_s = base->opaque;
1262 blk_insert_bs(blk, base, &error_abort);
1263
1264 overlay = bdrv_new_open_driver(&bdrv_test, "overlay", BDRV_O_RDWR,
1265 &error_abort);
1266 overlay_s = overlay->opaque;
1267
1268 do_drain_begin(BDRV_DRAIN, base);
1269 g_assert_cmpint(base->quiesce_counter, ==, 1);
1270 g_assert_cmpint(base_s->drain_count, ==, 1);
1271 g_assert_cmpint(base->in_flight, ==, 0);
1272
1273 /* Takes ownership of overlay, so we don't have to unref it later */
1274 bdrv_append(overlay, base, &error_abort);
1275 g_assert_cmpint(base->in_flight, ==, 0);
1276 g_assert_cmpint(overlay->in_flight, ==, 0);
1277
1278 g_assert_cmpint(base->quiesce_counter, ==, 1);
1279 g_assert_cmpint(base_s->drain_count, ==, 1);
1280 g_assert_cmpint(overlay->quiesce_counter, ==, 1);
1281 g_assert_cmpint(overlay_s->drain_count, ==, 1);
1282
1283 do_drain_end(BDRV_DRAIN, base);
1284
1285 g_assert_cmpint(base->quiesce_counter, ==, 0);
1286 g_assert_cmpint(base_s->drain_count, ==, 0);
1287 g_assert_cmpint(overlay->quiesce_counter, ==, 0);
1288 g_assert_cmpint(overlay_s->drain_count, ==, 0);
1289
1290 bdrv_unref(base);
1291 blk_unref(blk);
1292}
1293
881cfd17
KW
1294int main(int argc, char **argv)
1295{
bb675689
KW
1296 int ret;
1297
881cfd17
KW
1298 bdrv_init();
1299 qemu_init_main_loop(&error_abort);
1300
1301 g_test_init(&argc, &argv, NULL);
bb675689 1302 qemu_event_init(&done_event, false);
881cfd17
KW
1303
1304 g_test_add_func("/bdrv-drain/driver-cb/drain_all", test_drv_cb_drain_all);
86e1c840 1305 g_test_add_func("/bdrv-drain/driver-cb/drain", test_drv_cb_drain);
d2a85d0f
KW
1306 g_test_add_func("/bdrv-drain/driver-cb/drain_subtree",
1307 test_drv_cb_drain_subtree);
881cfd17 1308
6d0252f2
KW
1309 g_test_add_func("/bdrv-drain/driver-cb/co/drain_all",
1310 test_drv_cb_co_drain_all);
0582eb10
KW
1311 g_test_add_func("/bdrv-drain/driver-cb/co/drain", test_drv_cb_co_drain);
1312 g_test_add_func("/bdrv-drain/driver-cb/co/drain_subtree",
1313 test_drv_cb_co_drain_subtree);
1314
1315
89a6ceab
KW
1316 g_test_add_func("/bdrv-drain/quiesce/drain_all", test_quiesce_drain_all);
1317 g_test_add_func("/bdrv-drain/quiesce/drain", test_quiesce_drain);
d2a85d0f
KW
1318 g_test_add_func("/bdrv-drain/quiesce/drain_subtree",
1319 test_quiesce_drain_subtree);
89a6ceab 1320
6d0252f2
KW
1321 g_test_add_func("/bdrv-drain/quiesce/co/drain_all",
1322 test_quiesce_co_drain_all);
0582eb10
KW
1323 g_test_add_func("/bdrv-drain/quiesce/co/drain", test_quiesce_co_drain);
1324 g_test_add_func("/bdrv-drain/quiesce/co/drain_subtree",
1325 test_quiesce_co_drain_subtree);
1326
6c429a6a 1327 g_test_add_func("/bdrv-drain/nested", test_nested);
27e64474 1328 g_test_add_func("/bdrv-drain/multiparent", test_multiparent);
19f7a7e5
KW
1329
1330 g_test_add_func("/bdrv-drain/graph-change/drain_subtree",
1331 test_graph_change_drain_subtree);
1332 g_test_add_func("/bdrv-drain/graph-change/drain_all",
1333 test_graph_change_drain_all);
6c429a6a 1334
bb675689
KW
1335 g_test_add_func("/bdrv-drain/iothread/drain_all", test_iothread_drain_all);
1336 g_test_add_func("/bdrv-drain/iothread/drain", test_iothread_drain);
1337 g_test_add_func("/bdrv-drain/iothread/drain_subtree",
1338 test_iothread_drain_subtree);
1339
7253220d
KW
1340 g_test_add_func("/bdrv-drain/blockjob/drain_all", test_blockjob_drain_all);
1341 g_test_add_func("/bdrv-drain/blockjob/drain", test_blockjob_drain);
d2a85d0f
KW
1342 g_test_add_func("/bdrv-drain/blockjob/drain_subtree",
1343 test_blockjob_drain_subtree);
7253220d 1344
ebd31837 1345 g_test_add_func("/bdrv-drain/deletion/drain", test_delete_by_drain);
19f7a7e5 1346 g_test_add_func("/bdrv-drain/detach/drain_all", test_detach_by_drain_all);
ebd31837
KW
1347 g_test_add_func("/bdrv-drain/detach/drain", test_detach_by_drain);
1348 g_test_add_func("/bdrv-drain/detach/drain_subtree", test_detach_by_drain_subtree);
231281ab 1349 g_test_add_func("/bdrv-drain/detach/parent_cb", test_detach_by_parent_cb);
57320ca9 1350 g_test_add_func("/bdrv-drain/detach/driver_cb", test_detach_by_driver_cb);
4c8158e3 1351
b994c5bc
KW
1352 g_test_add_func("/bdrv-drain/attach/drain", test_append_to_drained);
1353
bb675689
KW
1354 ret = g_test_run();
1355 qemu_event_destroy(&done_event);
1356 return ret;
881cfd17 1357}