]> git.proxmox.com Git - mirror_qemu.git/blame - tests/test-bdrv-drain.c
block: Use a single global AioWait
[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
f62c1729
KW
177static void do_drain_begin_unlocked(enum drain_type drain_type, BlockDriverState *bs)
178{
179 if (drain_type != BDRV_DRAIN_ALL) {
180 aio_context_acquire(bdrv_get_aio_context(bs));
181 }
182 do_drain_begin(drain_type, bs);
183 if (drain_type != BDRV_DRAIN_ALL) {
184 aio_context_release(bdrv_get_aio_context(bs));
185 }
186}
187
188static void do_drain_end_unlocked(enum drain_type drain_type, BlockDriverState *bs)
189{
190 if (drain_type != BDRV_DRAIN_ALL) {
191 aio_context_acquire(bdrv_get_aio_context(bs));
192 }
193 do_drain_end(drain_type, bs);
194 if (drain_type != BDRV_DRAIN_ALL) {
195 aio_context_release(bdrv_get_aio_context(bs));
196 }
197}
198
86e1c840 199static void test_drv_cb_common(enum drain_type drain_type, bool recursive)
881cfd17
KW
200{
201 BlockBackend *blk;
86e1c840
KW
202 BlockDriverState *bs, *backing;
203 BDRVTestState *s, *backing_s;
881cfd17
KW
204 BlockAIOCB *acb;
205 int aio_ret;
206
207 QEMUIOVector qiov;
208 struct iovec iov = {
209 .iov_base = NULL,
210 .iov_len = 0,
211 };
212 qemu_iovec_init_external(&qiov, &iov, 1);
213
214 blk = blk_new(BLK_PERM_ALL, BLK_PERM_ALL);
215 bs = bdrv_new_open_driver(&bdrv_test, "test-node", BDRV_O_RDWR,
216 &error_abort);
217 s = bs->opaque;
218 blk_insert_bs(blk, bs, &error_abort);
219
86e1c840
KW
220 backing = bdrv_new_open_driver(&bdrv_test, "backing", 0, &error_abort);
221 backing_s = backing->opaque;
222 bdrv_set_backing_hd(bs, backing, &error_abort);
223
881cfd17
KW
224 /* Simple bdrv_drain_all_begin/end pair, check that CBs are called */
225 g_assert_cmpint(s->drain_count, ==, 0);
86e1c840
KW
226 g_assert_cmpint(backing_s->drain_count, ==, 0);
227
228 do_drain_begin(drain_type, bs);
229
881cfd17 230 g_assert_cmpint(s->drain_count, ==, 1);
86e1c840
KW
231 g_assert_cmpint(backing_s->drain_count, ==, !!recursive);
232
233 do_drain_end(drain_type, bs);
234
881cfd17 235 g_assert_cmpint(s->drain_count, ==, 0);
86e1c840 236 g_assert_cmpint(backing_s->drain_count, ==, 0);
881cfd17
KW
237
238 /* Now do the same while a request is pending */
239 aio_ret = -EINPROGRESS;
240 acb = blk_aio_preadv(blk, 0, &qiov, 0, aio_ret_cb, &aio_ret);
241 g_assert(acb != NULL);
242 g_assert_cmpint(aio_ret, ==, -EINPROGRESS);
243
244 g_assert_cmpint(s->drain_count, ==, 0);
86e1c840
KW
245 g_assert_cmpint(backing_s->drain_count, ==, 0);
246
247 do_drain_begin(drain_type, bs);
248
881cfd17
KW
249 g_assert_cmpint(aio_ret, ==, 0);
250 g_assert_cmpint(s->drain_count, ==, 1);
86e1c840
KW
251 g_assert_cmpint(backing_s->drain_count, ==, !!recursive);
252
253 do_drain_end(drain_type, bs);
254
881cfd17 255 g_assert_cmpint(s->drain_count, ==, 0);
86e1c840 256 g_assert_cmpint(backing_s->drain_count, ==, 0);
881cfd17 257
86e1c840 258 bdrv_unref(backing);
881cfd17
KW
259 bdrv_unref(bs);
260 blk_unref(blk);
261}
262
86e1c840
KW
263static void test_drv_cb_drain_all(void)
264{
265 test_drv_cb_common(BDRV_DRAIN_ALL, true);
266}
267
268static void test_drv_cb_drain(void)
269{
270 test_drv_cb_common(BDRV_DRAIN, false);
271}
272
d2a85d0f
KW
273static void test_drv_cb_drain_subtree(void)
274{
275 test_drv_cb_common(BDRV_SUBTREE_DRAIN, true);
276}
277
6d0252f2
KW
278static void test_drv_cb_co_drain_all(void)
279{
280 call_in_coroutine(test_drv_cb_drain_all);
281}
282
0582eb10
KW
283static void test_drv_cb_co_drain(void)
284{
285 call_in_coroutine(test_drv_cb_drain);
286}
287
288static void test_drv_cb_co_drain_subtree(void)
289{
290 call_in_coroutine(test_drv_cb_drain_subtree);
291}
292
89a6ceab
KW
293static void test_quiesce_common(enum drain_type drain_type, bool recursive)
294{
295 BlockBackend *blk;
296 BlockDriverState *bs, *backing;
297
298 blk = blk_new(BLK_PERM_ALL, BLK_PERM_ALL);
299 bs = bdrv_new_open_driver(&bdrv_test, "test-node", BDRV_O_RDWR,
300 &error_abort);
301 blk_insert_bs(blk, bs, &error_abort);
302
303 backing = bdrv_new_open_driver(&bdrv_test, "backing", 0, &error_abort);
304 bdrv_set_backing_hd(bs, backing, &error_abort);
305
306 g_assert_cmpint(bs->quiesce_counter, ==, 0);
307 g_assert_cmpint(backing->quiesce_counter, ==, 0);
308
309 do_drain_begin(drain_type, bs);
310
311 g_assert_cmpint(bs->quiesce_counter, ==, 1);
312 g_assert_cmpint(backing->quiesce_counter, ==, !!recursive);
313
314 do_drain_end(drain_type, bs);
315
316 g_assert_cmpint(bs->quiesce_counter, ==, 0);
317 g_assert_cmpint(backing->quiesce_counter, ==, 0);
318
319 bdrv_unref(backing);
320 bdrv_unref(bs);
321 blk_unref(blk);
322}
323
324static void test_quiesce_drain_all(void)
325{
79ab8b21 326 test_quiesce_common(BDRV_DRAIN_ALL, true);
89a6ceab
KW
327}
328
329static void test_quiesce_drain(void)
330{
331 test_quiesce_common(BDRV_DRAIN, false);
332}
333
d2a85d0f
KW
334static void test_quiesce_drain_subtree(void)
335{
336 test_quiesce_common(BDRV_SUBTREE_DRAIN, true);
337}
338
6d0252f2
KW
339static void test_quiesce_co_drain_all(void)
340{
341 call_in_coroutine(test_quiesce_drain_all);
342}
343
0582eb10
KW
344static void test_quiesce_co_drain(void)
345{
346 call_in_coroutine(test_quiesce_drain);
347}
348
349static void test_quiesce_co_drain_subtree(void)
350{
351 call_in_coroutine(test_quiesce_drain_subtree);
352}
353
6c429a6a
KW
354static void test_nested(void)
355{
356 BlockBackend *blk;
357 BlockDriverState *bs, *backing;
358 BDRVTestState *s, *backing_s;
359 enum drain_type outer, inner;
360
361 blk = blk_new(BLK_PERM_ALL, BLK_PERM_ALL);
362 bs = bdrv_new_open_driver(&bdrv_test, "test-node", BDRV_O_RDWR,
363 &error_abort);
364 s = bs->opaque;
365 blk_insert_bs(blk, bs, &error_abort);
366
367 backing = bdrv_new_open_driver(&bdrv_test, "backing", 0, &error_abort);
368 backing_s = backing->opaque;
369 bdrv_set_backing_hd(bs, backing, &error_abort);
370
371 for (outer = 0; outer < DRAIN_TYPE_MAX; outer++) {
372 for (inner = 0; inner < DRAIN_TYPE_MAX; inner++) {
79ab8b21 373 int backing_quiesce = (outer != BDRV_DRAIN) +
6c429a6a
KW
374 (inner != BDRV_DRAIN);
375
376 g_assert_cmpint(bs->quiesce_counter, ==, 0);
377 g_assert_cmpint(backing->quiesce_counter, ==, 0);
378 g_assert_cmpint(s->drain_count, ==, 0);
379 g_assert_cmpint(backing_s->drain_count, ==, 0);
380
381 do_drain_begin(outer, bs);
382 do_drain_begin(inner, bs);
383
79ab8b21 384 g_assert_cmpint(bs->quiesce_counter, ==, 2);
6c429a6a
KW
385 g_assert_cmpint(backing->quiesce_counter, ==, backing_quiesce);
386 g_assert_cmpint(s->drain_count, ==, 2);
79ab8b21 387 g_assert_cmpint(backing_s->drain_count, ==, backing_quiesce);
6c429a6a
KW
388
389 do_drain_end(inner, bs);
390 do_drain_end(outer, bs);
391
392 g_assert_cmpint(bs->quiesce_counter, ==, 0);
393 g_assert_cmpint(backing->quiesce_counter, ==, 0);
394 g_assert_cmpint(s->drain_count, ==, 0);
395 g_assert_cmpint(backing_s->drain_count, ==, 0);
396 }
397 }
398
399 bdrv_unref(backing);
400 bdrv_unref(bs);
401 blk_unref(blk);
402}
403
27e64474
KW
404static void test_multiparent(void)
405{
406 BlockBackend *blk_a, *blk_b;
407 BlockDriverState *bs_a, *bs_b, *backing;
408 BDRVTestState *a_s, *b_s, *backing_s;
409
410 blk_a = blk_new(BLK_PERM_ALL, BLK_PERM_ALL);
411 bs_a = bdrv_new_open_driver(&bdrv_test, "test-node-a", BDRV_O_RDWR,
412 &error_abort);
413 a_s = bs_a->opaque;
414 blk_insert_bs(blk_a, bs_a, &error_abort);
415
416 blk_b = blk_new(BLK_PERM_ALL, BLK_PERM_ALL);
417 bs_b = bdrv_new_open_driver(&bdrv_test, "test-node-b", BDRV_O_RDWR,
418 &error_abort);
419 b_s = bs_b->opaque;
420 blk_insert_bs(blk_b, bs_b, &error_abort);
421
422 backing = bdrv_new_open_driver(&bdrv_test, "backing", 0, &error_abort);
423 backing_s = backing->opaque;
424 bdrv_set_backing_hd(bs_a, backing, &error_abort);
425 bdrv_set_backing_hd(bs_b, backing, &error_abort);
426
427 g_assert_cmpint(bs_a->quiesce_counter, ==, 0);
428 g_assert_cmpint(bs_b->quiesce_counter, ==, 0);
429 g_assert_cmpint(backing->quiesce_counter, ==, 0);
430 g_assert_cmpint(a_s->drain_count, ==, 0);
431 g_assert_cmpint(b_s->drain_count, ==, 0);
432 g_assert_cmpint(backing_s->drain_count, ==, 0);
433
434 do_drain_begin(BDRV_SUBTREE_DRAIN, bs_a);
435
436 g_assert_cmpint(bs_a->quiesce_counter, ==, 1);
437 g_assert_cmpint(bs_b->quiesce_counter, ==, 1);
438 g_assert_cmpint(backing->quiesce_counter, ==, 1);
439 g_assert_cmpint(a_s->drain_count, ==, 1);
440 g_assert_cmpint(b_s->drain_count, ==, 1);
441 g_assert_cmpint(backing_s->drain_count, ==, 1);
442
443 do_drain_begin(BDRV_SUBTREE_DRAIN, bs_b);
444
445 g_assert_cmpint(bs_a->quiesce_counter, ==, 2);
446 g_assert_cmpint(bs_b->quiesce_counter, ==, 2);
447 g_assert_cmpint(backing->quiesce_counter, ==, 2);
448 g_assert_cmpint(a_s->drain_count, ==, 2);
449 g_assert_cmpint(b_s->drain_count, ==, 2);
450 g_assert_cmpint(backing_s->drain_count, ==, 2);
451
452 do_drain_end(BDRV_SUBTREE_DRAIN, bs_b);
453
454 g_assert_cmpint(bs_a->quiesce_counter, ==, 1);
455 g_assert_cmpint(bs_b->quiesce_counter, ==, 1);
456 g_assert_cmpint(backing->quiesce_counter, ==, 1);
457 g_assert_cmpint(a_s->drain_count, ==, 1);
458 g_assert_cmpint(b_s->drain_count, ==, 1);
459 g_assert_cmpint(backing_s->drain_count, ==, 1);
460
461 do_drain_end(BDRV_SUBTREE_DRAIN, bs_a);
462
463 g_assert_cmpint(bs_a->quiesce_counter, ==, 0);
464 g_assert_cmpint(bs_b->quiesce_counter, ==, 0);
465 g_assert_cmpint(backing->quiesce_counter, ==, 0);
466 g_assert_cmpint(a_s->drain_count, ==, 0);
467 g_assert_cmpint(b_s->drain_count, ==, 0);
468 g_assert_cmpint(backing_s->drain_count, ==, 0);
469
470 bdrv_unref(backing);
471 bdrv_unref(bs_a);
472 bdrv_unref(bs_b);
473 blk_unref(blk_a);
474 blk_unref(blk_b);
475}
476
19f7a7e5 477static void test_graph_change_drain_subtree(void)
acebcf8d
KW
478{
479 BlockBackend *blk_a, *blk_b;
480 BlockDriverState *bs_a, *bs_b, *backing;
481 BDRVTestState *a_s, *b_s, *backing_s;
482
483 blk_a = blk_new(BLK_PERM_ALL, BLK_PERM_ALL);
484 bs_a = bdrv_new_open_driver(&bdrv_test, "test-node-a", BDRV_O_RDWR,
485 &error_abort);
486 a_s = bs_a->opaque;
487 blk_insert_bs(blk_a, bs_a, &error_abort);
488
489 blk_b = blk_new(BLK_PERM_ALL, BLK_PERM_ALL);
490 bs_b = bdrv_new_open_driver(&bdrv_test, "test-node-b", BDRV_O_RDWR,
491 &error_abort);
492 b_s = bs_b->opaque;
493 blk_insert_bs(blk_b, bs_b, &error_abort);
494
495 backing = bdrv_new_open_driver(&bdrv_test, "backing", 0, &error_abort);
496 backing_s = backing->opaque;
497 bdrv_set_backing_hd(bs_a, backing, &error_abort);
498
499 g_assert_cmpint(bs_a->quiesce_counter, ==, 0);
500 g_assert_cmpint(bs_b->quiesce_counter, ==, 0);
501 g_assert_cmpint(backing->quiesce_counter, ==, 0);
502 g_assert_cmpint(a_s->drain_count, ==, 0);
503 g_assert_cmpint(b_s->drain_count, ==, 0);
504 g_assert_cmpint(backing_s->drain_count, ==, 0);
505
506 do_drain_begin(BDRV_SUBTREE_DRAIN, bs_a);
507 do_drain_begin(BDRV_SUBTREE_DRAIN, bs_a);
508 do_drain_begin(BDRV_SUBTREE_DRAIN, bs_a);
509 do_drain_begin(BDRV_SUBTREE_DRAIN, bs_b);
510 do_drain_begin(BDRV_SUBTREE_DRAIN, bs_b);
511
512 bdrv_set_backing_hd(bs_b, backing, &error_abort);
513 g_assert_cmpint(bs_a->quiesce_counter, ==, 5);
514 g_assert_cmpint(bs_b->quiesce_counter, ==, 5);
515 g_assert_cmpint(backing->quiesce_counter, ==, 5);
516 g_assert_cmpint(a_s->drain_count, ==, 5);
517 g_assert_cmpint(b_s->drain_count, ==, 5);
518 g_assert_cmpint(backing_s->drain_count, ==, 5);
519
520 bdrv_set_backing_hd(bs_b, NULL, &error_abort);
521 g_assert_cmpint(bs_a->quiesce_counter, ==, 3);
522 g_assert_cmpint(bs_b->quiesce_counter, ==, 2);
523 g_assert_cmpint(backing->quiesce_counter, ==, 3);
524 g_assert_cmpint(a_s->drain_count, ==, 3);
525 g_assert_cmpint(b_s->drain_count, ==, 2);
526 g_assert_cmpint(backing_s->drain_count, ==, 3);
527
528 bdrv_set_backing_hd(bs_b, backing, &error_abort);
529 g_assert_cmpint(bs_a->quiesce_counter, ==, 5);
530 g_assert_cmpint(bs_b->quiesce_counter, ==, 5);
531 g_assert_cmpint(backing->quiesce_counter, ==, 5);
532 g_assert_cmpint(a_s->drain_count, ==, 5);
533 g_assert_cmpint(b_s->drain_count, ==, 5);
534 g_assert_cmpint(backing_s->drain_count, ==, 5);
535
536 do_drain_end(BDRV_SUBTREE_DRAIN, bs_b);
537 do_drain_end(BDRV_SUBTREE_DRAIN, bs_b);
538 do_drain_end(BDRV_SUBTREE_DRAIN, bs_a);
539 do_drain_end(BDRV_SUBTREE_DRAIN, bs_a);
540 do_drain_end(BDRV_SUBTREE_DRAIN, bs_a);
541
542 g_assert_cmpint(bs_a->quiesce_counter, ==, 0);
543 g_assert_cmpint(bs_b->quiesce_counter, ==, 0);
544 g_assert_cmpint(backing->quiesce_counter, ==, 0);
545 g_assert_cmpint(a_s->drain_count, ==, 0);
546 g_assert_cmpint(b_s->drain_count, ==, 0);
547 g_assert_cmpint(backing_s->drain_count, ==, 0);
548
549 bdrv_unref(backing);
550 bdrv_unref(bs_a);
551 bdrv_unref(bs_b);
552 blk_unref(blk_a);
553 blk_unref(blk_b);
554}
555
19f7a7e5
KW
556static void test_graph_change_drain_all(void)
557{
558 BlockBackend *blk_a, *blk_b;
559 BlockDriverState *bs_a, *bs_b;
560 BDRVTestState *a_s, *b_s;
561
562 /* Create node A with a BlockBackend */
563 blk_a = blk_new(BLK_PERM_ALL, BLK_PERM_ALL);
564 bs_a = bdrv_new_open_driver(&bdrv_test, "test-node-a", BDRV_O_RDWR,
565 &error_abort);
566 a_s = bs_a->opaque;
567 blk_insert_bs(blk_a, bs_a, &error_abort);
568
569 g_assert_cmpint(bs_a->quiesce_counter, ==, 0);
570 g_assert_cmpint(a_s->drain_count, ==, 0);
571
572 /* Call bdrv_drain_all_begin() */
573 bdrv_drain_all_begin();
574
575 g_assert_cmpint(bs_a->quiesce_counter, ==, 1);
576 g_assert_cmpint(a_s->drain_count, ==, 1);
577
578 /* Create node B with a BlockBackend */
579 blk_b = blk_new(BLK_PERM_ALL, BLK_PERM_ALL);
580 bs_b = bdrv_new_open_driver(&bdrv_test, "test-node-b", BDRV_O_RDWR,
581 &error_abort);
582 b_s = bs_b->opaque;
583 blk_insert_bs(blk_b, bs_b, &error_abort);
584
585 g_assert_cmpint(bs_a->quiesce_counter, ==, 1);
586 g_assert_cmpint(bs_b->quiesce_counter, ==, 1);
587 g_assert_cmpint(a_s->drain_count, ==, 1);
588 g_assert_cmpint(b_s->drain_count, ==, 1);
589
590 /* Unref and finally delete node A */
591 blk_unref(blk_a);
592
593 g_assert_cmpint(bs_a->quiesce_counter, ==, 1);
594 g_assert_cmpint(bs_b->quiesce_counter, ==, 1);
595 g_assert_cmpint(a_s->drain_count, ==, 1);
596 g_assert_cmpint(b_s->drain_count, ==, 1);
597
598 bdrv_unref(bs_a);
599
600 g_assert_cmpint(bs_b->quiesce_counter, ==, 1);
601 g_assert_cmpint(b_s->drain_count, ==, 1);
602
603 /* End the drained section */
604 bdrv_drain_all_end();
605
606 g_assert_cmpint(bs_b->quiesce_counter, ==, 0);
607 g_assert_cmpint(b_s->drain_count, ==, 0);
608
609 bdrv_unref(bs_b);
610 blk_unref(blk_b);
611}
612
bb675689
KW
613struct test_iothread_data {
614 BlockDriverState *bs;
615 enum drain_type drain_type;
616 int *aio_ret;
617};
618
619static void test_iothread_drain_entry(void *opaque)
620{
621 struct test_iothread_data *data = opaque;
622
623 aio_context_acquire(bdrv_get_aio_context(data->bs));
624 do_drain_begin(data->drain_type, data->bs);
625 g_assert_cmpint(*data->aio_ret, ==, 0);
626 do_drain_end(data->drain_type, data->bs);
627 aio_context_release(bdrv_get_aio_context(data->bs));
628
629 qemu_event_set(&done_event);
630}
631
632static void test_iothread_aio_cb(void *opaque, int ret)
633{
634 int *aio_ret = opaque;
635 *aio_ret = ret;
636 qemu_event_set(&done_event);
637}
638
ecc1a5c7
KW
639static void test_iothread_main_thread_bh(void *opaque)
640{
641 struct test_iothread_data *data = opaque;
642
643 /* Test that the AioContext is not yet locked in a random BH that is
644 * executed during drain, otherwise this would deadlock. */
645 aio_context_acquire(bdrv_get_aio_context(data->bs));
646 bdrv_flush(data->bs);
647 aio_context_release(bdrv_get_aio_context(data->bs));
648}
649
bb675689
KW
650/*
651 * Starts an AIO request on a BDS that runs in the AioContext of iothread 1.
652 * The request involves a BH on iothread 2 before it can complete.
653 *
654 * @drain_thread = 0 means that do_drain_begin/end are called from the main
655 * thread, @drain_thread = 1 means that they are called from iothread 1. Drain
656 * for this BDS cannot be called from iothread 2 because only the main thread
657 * may do cross-AioContext polling.
658 */
659static void test_iothread_common(enum drain_type drain_type, int drain_thread)
660{
661 BlockBackend *blk;
662 BlockDriverState *bs;
663 BDRVTestState *s;
664 BlockAIOCB *acb;
665 int aio_ret;
666 struct test_iothread_data data;
667
668 IOThread *a = iothread_new();
669 IOThread *b = iothread_new();
670 AioContext *ctx_a = iothread_get_aio_context(a);
671 AioContext *ctx_b = iothread_get_aio_context(b);
672
673 QEMUIOVector qiov;
674 struct iovec iov = {
675 .iov_base = NULL,
676 .iov_len = 0,
677 };
678 qemu_iovec_init_external(&qiov, &iov, 1);
679
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;
683 }
684
685 blk = blk_new(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
691 blk_set_aio_context(blk, ctx_a);
692 aio_context_acquire(ctx_a);
693
694 s->bh_indirection_ctx = ctx_b;
695
696 aio_ret = -EINPROGRESS;
697 if (drain_thread == 0) {
698 acb = blk_aio_preadv(blk, 0, &qiov, 0, test_iothread_aio_cb, &aio_ret);
699 } else {
700 acb = blk_aio_preadv(blk, 0, &qiov, 0, aio_ret_cb, &aio_ret);
701 }
702 g_assert(acb != NULL);
703 g_assert_cmpint(aio_ret, ==, -EINPROGRESS);
704
705 aio_context_release(ctx_a);
706
707 data = (struct test_iothread_data) {
708 .bs = bs,
709 .drain_type = drain_type,
710 .aio_ret = &aio_ret,
711 };
712
713 switch (drain_thread) {
714 case 0:
715 if (drain_type != BDRV_DRAIN_ALL) {
716 aio_context_acquire(ctx_a);
717 }
718
ecc1a5c7
KW
719 aio_bh_schedule_oneshot(ctx_a, test_iothread_main_thread_bh, &data);
720
bb675689
KW
721 /* The request is running on the IOThread a. Draining its block device
722 * will make sure that it has completed as far as the BDS is concerned,
723 * but the drain in this thread can continue immediately after
724 * bdrv_dec_in_flight() and aio_ret might be assigned only slightly
725 * later. */
726 qemu_event_reset(&done_event);
727 do_drain_begin(drain_type, bs);
728 g_assert_cmpint(bs->in_flight, ==, 0);
729
730 if (drain_type != BDRV_DRAIN_ALL) {
731 aio_context_release(ctx_a);
732 }
733 qemu_event_wait(&done_event);
734 if (drain_type != BDRV_DRAIN_ALL) {
735 aio_context_acquire(ctx_a);
736 }
737
738 g_assert_cmpint(aio_ret, ==, 0);
739 do_drain_end(drain_type, bs);
740
741 if (drain_type != BDRV_DRAIN_ALL) {
742 aio_context_release(ctx_a);
743 }
744 break;
745 case 1:
746 qemu_event_reset(&done_event);
747 aio_bh_schedule_oneshot(ctx_a, test_iothread_drain_entry, &data);
748 qemu_event_wait(&done_event);
749 break;
750 default:
751 g_assert_not_reached();
752 }
753
754 aio_context_acquire(ctx_a);
755 blk_set_aio_context(blk, qemu_get_aio_context());
756 aio_context_release(ctx_a);
757
758 bdrv_unref(bs);
759 blk_unref(blk);
760
761out:
762 iothread_join(a);
763 iothread_join(b);
764}
765
766static void test_iothread_drain_all(void)
767{
768 test_iothread_common(BDRV_DRAIN_ALL, 0);
769 test_iothread_common(BDRV_DRAIN_ALL, 1);
770}
771
772static void test_iothread_drain(void)
773{
774 test_iothread_common(BDRV_DRAIN, 0);
775 test_iothread_common(BDRV_DRAIN, 1);
776}
777
778static void test_iothread_drain_subtree(void)
779{
780 test_iothread_common(BDRV_SUBTREE_DRAIN, 0);
781 test_iothread_common(BDRV_SUBTREE_DRAIN, 1);
782}
783
7253220d
KW
784
785typedef struct TestBlockJob {
786 BlockJob common;
d49725af
KW
787 int run_ret;
788 int prepare_ret;
7253220d
KW
789 bool should_complete;
790} TestBlockJob;
791
ae23dde9
KW
792static int test_job_prepare(Job *job)
793{
794 TestBlockJob *s = container_of(job, TestBlockJob, common.job);
795
796 /* Provoke an AIO_WAIT_WHILE() call to verify there is no deadlock */
797 blk_flush(s->common.blk);
d49725af
KW
798 return s->prepare_ret;
799}
800
801static void test_job_commit(Job *job)
802{
803 TestBlockJob *s = container_of(job, TestBlockJob, common.job);
804
805 /* Provoke an AIO_WAIT_WHILE() call to verify there is no deadlock */
806 blk_flush(s->common.blk);
807}
808
809static void test_job_abort(Job *job)
810{
811 TestBlockJob *s = container_of(job, TestBlockJob, common.job);
812
813 /* Provoke an AIO_WAIT_WHILE() call to verify there is no deadlock */
814 blk_flush(s->common.blk);
ae23dde9
KW
815}
816
f67432a2 817static int coroutine_fn test_job_run(Job *job, Error **errp)
7253220d 818{
f67432a2 819 TestBlockJob *s = container_of(job, TestBlockJob, common.job);
7253220d 820
2e1795b5 821 job_transition_to_ready(&s->common.job);
7253220d 822 while (!s->should_complete) {
5599c162
KW
823 /* Avoid job_sleep_ns() because it marks the job as !busy. We want to
824 * emulate some actual activity (probably some I/O) here so that drain
825 * has to wait for this activity to stop. */
89bd0305
KW
826 qemu_co_sleep_ns(QEMU_CLOCK_REALTIME, 100000);
827 job_pause_point(&s->common.job);
7253220d
KW
828 }
829
d49725af 830 return s->run_ret;
7253220d
KW
831}
832
3453d972 833static void test_job_complete(Job *job, Error **errp)
7253220d 834{
3453d972 835 TestBlockJob *s = container_of(job, TestBlockJob, common.job);
7253220d
KW
836 s->should_complete = true;
837}
838
839BlockJobDriver test_job_driver = {
33e9e9bd
KW
840 .job_driver = {
841 .instance_size = sizeof(TestBlockJob),
80fa2c75 842 .free = block_job_free,
b15de828 843 .user_resume = block_job_user_resume,
b69f777d 844 .drain = block_job_drain,
f67432a2 845 .run = test_job_run,
3453d972 846 .complete = test_job_complete,
ae23dde9 847 .prepare = test_job_prepare,
d49725af
KW
848 .commit = test_job_commit,
849 .abort = test_job_abort,
33e9e9bd 850 },
7253220d
KW
851};
852
d49725af
KW
853enum test_job_result {
854 TEST_JOB_SUCCESS,
855 TEST_JOB_FAIL_RUN,
856 TEST_JOB_FAIL_PREPARE,
857};
858
859static void test_blockjob_common(enum drain_type drain_type, bool use_iothread,
860 enum test_job_result result)
7253220d
KW
861{
862 BlockBackend *blk_src, *blk_target;
863 BlockDriverState *src, *target;
864 BlockJob *job;
d49725af 865 TestBlockJob *tjob;
f62c1729
KW
866 IOThread *iothread = NULL;
867 AioContext *ctx;
7253220d
KW
868 int ret;
869
870 src = bdrv_new_open_driver(&bdrv_test, "source", BDRV_O_RDWR,
871 &error_abort);
872 blk_src = blk_new(BLK_PERM_ALL, BLK_PERM_ALL);
873 blk_insert_bs(blk_src, src, &error_abort);
874
f62c1729
KW
875 if (use_iothread) {
876 iothread = iothread_new();
877 ctx = iothread_get_aio_context(iothread);
878 blk_set_aio_context(blk_src, ctx);
879 } else {
880 ctx = qemu_get_aio_context();
881 }
882
7253220d
KW
883 target = bdrv_new_open_driver(&bdrv_test, "target", BDRV_O_RDWR,
884 &error_abort);
885 blk_target = blk_new(BLK_PERM_ALL, BLK_PERM_ALL);
886 blk_insert_bs(blk_target, target, &error_abort);
887
f62c1729 888 aio_context_acquire(ctx);
d49725af
KW
889 tjob = block_job_create("job0", &test_job_driver, NULL, src,
890 0, BLK_PERM_ALL,
891 0, 0, NULL, NULL, &error_abort);
892 job = &tjob->common;
7253220d 893 block_job_add_bdrv(job, "target", target, 0, BLK_PERM_ALL, &error_abort);
d49725af
KW
894
895 switch (result) {
896 case TEST_JOB_SUCCESS:
897 break;
898 case TEST_JOB_FAIL_RUN:
899 tjob->run_ret = -EIO;
900 break;
901 case TEST_JOB_FAIL_PREPARE:
902 tjob->prepare_ret = -EIO;
903 break;
904 }
905
da01ff7f 906 job_start(&job->job);
f62c1729 907 aio_context_release(ctx);
7253220d 908
da01ff7f
KW
909 g_assert_cmpint(job->job.pause_count, ==, 0);
910 g_assert_false(job->job.paused);
5599c162 911 g_assert_true(job->job.busy); /* We're in qemu_co_sleep_ns() */
7253220d 912
f62c1729 913 do_drain_begin_unlocked(drain_type, src);
7253220d
KW
914
915 if (drain_type == BDRV_DRAIN_ALL) {
81193349 916 /* bdrv_drain_all() drains both src and target */
da01ff7f 917 g_assert_cmpint(job->job.pause_count, ==, 2);
7253220d 918 } else {
da01ff7f 919 g_assert_cmpint(job->job.pause_count, ==, 1);
7253220d 920 }
89bd0305 921 g_assert_true(job->job.paused);
da01ff7f 922 g_assert_false(job->job.busy); /* The job is paused */
7253220d 923
f62c1729
KW
924 do_drain_end_unlocked(drain_type, src);
925
926 if (use_iothread) {
927 /* paused is reset in the I/O thread, wait for it */
928 while (job->job.paused) {
929 aio_poll(qemu_get_aio_context(), false);
930 }
931 }
7253220d 932
da01ff7f
KW
933 g_assert_cmpint(job->job.pause_count, ==, 0);
934 g_assert_false(job->job.paused);
89bd0305 935 g_assert_true(job->job.busy); /* We're in qemu_co_sleep_ns() */
7253220d
KW
936
937 do_drain_begin(drain_type, target);
938
939 if (drain_type == BDRV_DRAIN_ALL) {
81193349 940 /* bdrv_drain_all() drains both src and target */
da01ff7f 941 g_assert_cmpint(job->job.pause_count, ==, 2);
7253220d 942 } else {
da01ff7f 943 g_assert_cmpint(job->job.pause_count, ==, 1);
7253220d 944 }
89bd0305 945 g_assert_true(job->job.paused);
da01ff7f 946 g_assert_false(job->job.busy); /* The job is paused */
7253220d
KW
947
948 do_drain_end(drain_type, target);
949
f62c1729
KW
950 if (use_iothread) {
951 /* paused is reset in the I/O thread, wait for it */
952 while (job->job.paused) {
953 aio_poll(qemu_get_aio_context(), false);
954 }
955 }
956
da01ff7f
KW
957 g_assert_cmpint(job->job.pause_count, ==, 0);
958 g_assert_false(job->job.paused);
5599c162 959 g_assert_true(job->job.busy); /* We're in qemu_co_sleep_ns() */
7253220d 960
f62c1729 961 aio_context_acquire(ctx);
3d70ff53 962 ret = job_complete_sync(&job->job, &error_abort);
d49725af 963 g_assert_cmpint(ret, ==, (result == TEST_JOB_SUCCESS ? 0 : -EIO));
7253220d 964
f62c1729
KW
965 if (use_iothread) {
966 blk_set_aio_context(blk_src, qemu_get_aio_context());
967 }
968 aio_context_release(ctx);
969
7253220d
KW
970 blk_unref(blk_src);
971 blk_unref(blk_target);
972 bdrv_unref(src);
973 bdrv_unref(target);
f62c1729
KW
974
975 if (iothread) {
976 iothread_join(iothread);
977 }
7253220d
KW
978}
979
980static void test_blockjob_drain_all(void)
981{
d49725af 982 test_blockjob_common(BDRV_DRAIN_ALL, false, TEST_JOB_SUCCESS);
7253220d
KW
983}
984
985static void test_blockjob_drain(void)
986{
d49725af 987 test_blockjob_common(BDRV_DRAIN, false, TEST_JOB_SUCCESS);
7253220d
KW
988}
989
d2a85d0f
KW
990static void test_blockjob_drain_subtree(void)
991{
d49725af
KW
992 test_blockjob_common(BDRV_SUBTREE_DRAIN, false, TEST_JOB_SUCCESS);
993}
994
995static void test_blockjob_error_drain_all(void)
996{
997 test_blockjob_common(BDRV_DRAIN_ALL, false, TEST_JOB_FAIL_RUN);
998 test_blockjob_common(BDRV_DRAIN_ALL, false, TEST_JOB_FAIL_PREPARE);
999}
1000
1001static void test_blockjob_error_drain(void)
1002{
1003 test_blockjob_common(BDRV_DRAIN, false, TEST_JOB_FAIL_RUN);
1004 test_blockjob_common(BDRV_DRAIN, false, TEST_JOB_FAIL_PREPARE);
1005}
1006
1007static void test_blockjob_error_drain_subtree(void)
1008{
1009 test_blockjob_common(BDRV_SUBTREE_DRAIN, false, TEST_JOB_FAIL_RUN);
1010 test_blockjob_common(BDRV_SUBTREE_DRAIN, false, TEST_JOB_FAIL_PREPARE);
f62c1729
KW
1011}
1012
1013static void test_blockjob_iothread_drain_all(void)
1014{
d49725af 1015 test_blockjob_common(BDRV_DRAIN_ALL, true, TEST_JOB_SUCCESS);
f62c1729
KW
1016}
1017
1018static void test_blockjob_iothread_drain(void)
1019{
d49725af 1020 test_blockjob_common(BDRV_DRAIN, true, TEST_JOB_SUCCESS);
f62c1729
KW
1021}
1022
1023static void test_blockjob_iothread_drain_subtree(void)
1024{
d49725af
KW
1025 test_blockjob_common(BDRV_SUBTREE_DRAIN, true, TEST_JOB_SUCCESS);
1026}
1027
1028static void test_blockjob_iothread_error_drain_all(void)
1029{
1030 test_blockjob_common(BDRV_DRAIN_ALL, true, TEST_JOB_FAIL_RUN);
1031 test_blockjob_common(BDRV_DRAIN_ALL, true, TEST_JOB_FAIL_PREPARE);
1032}
1033
1034static void test_blockjob_iothread_error_drain(void)
1035{
1036 test_blockjob_common(BDRV_DRAIN, true, TEST_JOB_FAIL_RUN);
1037 test_blockjob_common(BDRV_DRAIN, true, TEST_JOB_FAIL_PREPARE);
1038}
1039
1040static void test_blockjob_iothread_error_drain_subtree(void)
1041{
1042 test_blockjob_common(BDRV_SUBTREE_DRAIN, true, TEST_JOB_FAIL_RUN);
1043 test_blockjob_common(BDRV_SUBTREE_DRAIN, true, TEST_JOB_FAIL_PREPARE);
d2a85d0f
KW
1044}
1045
4c8158e3
HR
1046
1047typedef struct BDRVTestTopState {
1048 BdrvChild *wait_child;
1049} BDRVTestTopState;
1050
1051static void bdrv_test_top_close(BlockDriverState *bs)
1052{
1053 BdrvChild *c, *next_c;
1054 QLIST_FOREACH_SAFE(c, &bs->children, next, next_c) {
1055 bdrv_unref_child(bs, c);
1056 }
1057}
1058
1059static int coroutine_fn bdrv_test_top_co_preadv(BlockDriverState *bs,
1060 uint64_t offset, uint64_t bytes,
1061 QEMUIOVector *qiov, int flags)
1062{
1063 BDRVTestTopState *tts = bs->opaque;
1064 return bdrv_co_preadv(tts->wait_child, offset, bytes, qiov, flags);
1065}
1066
1067static BlockDriver bdrv_test_top_driver = {
1068 .format_name = "test_top_driver",
1069 .instance_size = sizeof(BDRVTestTopState),
1070
1071 .bdrv_close = bdrv_test_top_close,
1072 .bdrv_co_preadv = bdrv_test_top_co_preadv,
1073
1074 .bdrv_child_perm = bdrv_format_default_perms,
1075};
1076
1077typedef struct TestCoDeleteByDrainData {
1078 BlockBackend *blk;
1079 bool detach_instead_of_delete;
1080 bool done;
1081} TestCoDeleteByDrainData;
1082
1083static void coroutine_fn test_co_delete_by_drain(void *opaque)
1084{
1085 TestCoDeleteByDrainData *dbdd = opaque;
1086 BlockBackend *blk = dbdd->blk;
1087 BlockDriverState *bs = blk_bs(blk);
1088 BDRVTestTopState *tts = bs->opaque;
1089 void *buffer = g_malloc(65536);
1090 QEMUIOVector qiov;
1091 struct iovec iov = {
1092 .iov_base = buffer,
1093 .iov_len = 65536,
1094 };
1095
1096 qemu_iovec_init_external(&qiov, &iov, 1);
1097
1098 /* Pretend some internal write operation from parent to child.
1099 * Important: We have to read from the child, not from the parent!
1100 * Draining works by first propagating it all up the tree to the
1101 * root and then waiting for drainage from root to the leaves
1102 * (protocol nodes). If we have a request waiting on the root,
1103 * everything will be drained before we go back down the tree, but
1104 * we do not want that. We want to be in the middle of draining
1105 * when this following requests returns. */
1106 bdrv_co_preadv(tts->wait_child, 0, 65536, &qiov, 0);
1107
1108 g_assert_cmpint(bs->refcnt, ==, 1);
1109
1110 if (!dbdd->detach_instead_of_delete) {
1111 blk_unref(blk);
1112 } else {
1113 BdrvChild *c, *next_c;
1114 QLIST_FOREACH_SAFE(c, &bs->children, next, next_c) {
1115 bdrv_unref_child(bs, c);
1116 }
1117 }
1118
1119 dbdd->done = true;
7b43db3c 1120 g_free(buffer);
4c8158e3
HR
1121}
1122
1123/**
1124 * Test what happens when some BDS has some children, you drain one of
1125 * them and this results in the BDS being deleted.
1126 *
1127 * If @detach_instead_of_delete is set, the BDS is not going to be
1128 * deleted but will only detach all of its children.
1129 */
ebd31837
KW
1130static void do_test_delete_by_drain(bool detach_instead_of_delete,
1131 enum drain_type drain_type)
4c8158e3
HR
1132{
1133 BlockBackend *blk;
1134 BlockDriverState *bs, *child_bs, *null_bs;
1135 BDRVTestTopState *tts;
1136 TestCoDeleteByDrainData dbdd;
1137 Coroutine *co;
1138
1139 bs = bdrv_new_open_driver(&bdrv_test_top_driver, "top", BDRV_O_RDWR,
1140 &error_abort);
1141 bs->total_sectors = 65536 >> BDRV_SECTOR_BITS;
1142 tts = bs->opaque;
1143
1144 null_bs = bdrv_open("null-co://", NULL, NULL, BDRV_O_RDWR | BDRV_O_PROTOCOL,
1145 &error_abort);
1146 bdrv_attach_child(bs, null_bs, "null-child", &child_file, &error_abort);
1147
1148 /* This child will be the one to pass to requests through to, and
1149 * it will stall until a drain occurs */
1150 child_bs = bdrv_new_open_driver(&bdrv_test, "child", BDRV_O_RDWR,
1151 &error_abort);
1152 child_bs->total_sectors = 65536 >> BDRV_SECTOR_BITS;
1153 /* Takes our reference to child_bs */
1154 tts->wait_child = bdrv_attach_child(bs, child_bs, "wait-child", &child_file,
1155 &error_abort);
1156
1157 /* This child is just there to be deleted
1158 * (for detach_instead_of_delete == true) */
1159 null_bs = bdrv_open("null-co://", NULL, NULL, BDRV_O_RDWR | BDRV_O_PROTOCOL,
1160 &error_abort);
1161 bdrv_attach_child(bs, null_bs, "null-child", &child_file, &error_abort);
1162
1163 blk = blk_new(BLK_PERM_ALL, BLK_PERM_ALL);
1164 blk_insert_bs(blk, bs, &error_abort);
1165
1166 /* Referenced by blk now */
1167 bdrv_unref(bs);
1168
1169 g_assert_cmpint(bs->refcnt, ==, 1);
1170 g_assert_cmpint(child_bs->refcnt, ==, 1);
1171 g_assert_cmpint(null_bs->refcnt, ==, 1);
1172
1173
1174 dbdd = (TestCoDeleteByDrainData){
1175 .blk = blk,
1176 .detach_instead_of_delete = detach_instead_of_delete,
1177 .done = false,
1178 };
1179 co = qemu_coroutine_create(test_co_delete_by_drain, &dbdd);
1180 qemu_coroutine_enter(co);
1181
1182 /* Drain the child while the read operation is still pending.
1183 * This should result in the operation finishing and
1184 * test_co_delete_by_drain() resuming. Thus, @bs will be deleted
1185 * and the coroutine will exit while this drain operation is still
1186 * in progress. */
ebd31837
KW
1187 switch (drain_type) {
1188 case BDRV_DRAIN:
1189 bdrv_ref(child_bs);
1190 bdrv_drain(child_bs);
1191 bdrv_unref(child_bs);
1192 break;
1193 case BDRV_SUBTREE_DRAIN:
1194 /* Would have to ref/unref bs here for !detach_instead_of_delete, but
1195 * then the whole test becomes pointless because the graph changes
1196 * don't occur during the drain any more. */
1197 assert(detach_instead_of_delete);
1198 bdrv_subtree_drained_begin(bs);
1199 bdrv_subtree_drained_end(bs);
1200 break;
19f7a7e5
KW
1201 case BDRV_DRAIN_ALL:
1202 bdrv_drain_all_begin();
1203 bdrv_drain_all_end();
1204 break;
ebd31837
KW
1205 default:
1206 g_assert_not_reached();
1207 }
4c8158e3
HR
1208
1209 while (!dbdd.done) {
1210 aio_poll(qemu_get_aio_context(), true);
1211 }
1212
1213 if (detach_instead_of_delete) {
1214 /* Here, the reference has not passed over to the coroutine,
1215 * so we have to delete the BB ourselves */
1216 blk_unref(blk);
1217 }
1218}
1219
4c8158e3
HR
1220static void test_delete_by_drain(void)
1221{
ebd31837 1222 do_test_delete_by_drain(false, BDRV_DRAIN);
4c8158e3
HR
1223}
1224
19f7a7e5
KW
1225static void test_detach_by_drain_all(void)
1226{
1227 do_test_delete_by_drain(true, BDRV_DRAIN_ALL);
1228}
1229
4c8158e3
HR
1230static void test_detach_by_drain(void)
1231{
ebd31837
KW
1232 do_test_delete_by_drain(true, BDRV_DRAIN);
1233}
1234
1235static void test_detach_by_drain_subtree(void)
1236{
1237 do_test_delete_by_drain(true, BDRV_SUBTREE_DRAIN);
4c8158e3
HR
1238}
1239
1240
231281ab
KW
1241struct detach_by_parent_data {
1242 BlockDriverState *parent_b;
1243 BdrvChild *child_b;
1244 BlockDriverState *c;
1245 BdrvChild *child_c;
57320ca9 1246 bool by_parent_cb;
231281ab 1247};
57320ca9 1248static struct detach_by_parent_data detach_by_parent_data;
231281ab 1249
57320ca9 1250static void detach_indirect_bh(void *opaque)
231281ab
KW
1251{
1252 struct detach_by_parent_data *data = opaque;
1253
231281ab
KW
1254 bdrv_unref_child(data->parent_b, data->child_b);
1255
1256 bdrv_ref(data->c);
1257 data->child_c = bdrv_attach_child(data->parent_b, data->c, "PB-C",
1258 &child_file, &error_abort);
1259}
1260
57320ca9
KW
1261static void detach_by_parent_aio_cb(void *opaque, int ret)
1262{
1263 struct detach_by_parent_data *data = &detach_by_parent_data;
1264
1265 g_assert_cmpint(ret, ==, 0);
1266 if (data->by_parent_cb) {
1267 detach_indirect_bh(data);
1268 }
1269}
1270
1271static void detach_by_driver_cb_drained_begin(BdrvChild *child)
1272{
1273 aio_bh_schedule_oneshot(qemu_get_current_aio_context(),
1274 detach_indirect_bh, &detach_by_parent_data);
1275 child_file.drained_begin(child);
1276}
1277
1278static BdrvChildRole detach_by_driver_cb_role;
1279
231281ab
KW
1280/*
1281 * Initial graph:
1282 *
1283 * PA PB
1284 * \ / \
1285 * A B C
1286 *
57320ca9
KW
1287 * by_parent_cb == true: Test that parent callbacks don't poll
1288 *
1289 * PA has a pending write request whose callback changes the child nodes of
1290 * PB: It removes B and adds C instead. The subtree of PB is drained, which
1291 * will indirectly drain the write request, too.
1292 *
1293 * by_parent_cb == false: Test that bdrv_drain_invoke() doesn't poll
1294 *
1295 * PA's BdrvChildRole has a .drained_begin callback that schedules a BH
1296 * that does the same graph change. If bdrv_drain_invoke() calls it, the
1297 * state is messed up, but if it is only polled in the single
1298 * BDRV_POLL_WHILE() at the end of the drain, this should work fine.
231281ab 1299 */
57320ca9 1300static void test_detach_indirect(bool by_parent_cb)
231281ab
KW
1301{
1302 BlockBackend *blk;
1303 BlockDriverState *parent_a, *parent_b, *a, *b, *c;
1304 BdrvChild *child_a, *child_b;
1305 BlockAIOCB *acb;
231281ab
KW
1306
1307 QEMUIOVector qiov;
1308 struct iovec iov = {
1309 .iov_base = NULL,
1310 .iov_len = 0,
1311 };
1312 qemu_iovec_init_external(&qiov, &iov, 1);
1313
57320ca9
KW
1314 if (!by_parent_cb) {
1315 detach_by_driver_cb_role = child_file;
1316 detach_by_driver_cb_role.drained_begin =
1317 detach_by_driver_cb_drained_begin;
1318 }
1319
231281ab
KW
1320 /* Create all involved nodes */
1321 parent_a = bdrv_new_open_driver(&bdrv_test, "parent-a", BDRV_O_RDWR,
1322 &error_abort);
1323 parent_b = bdrv_new_open_driver(&bdrv_test, "parent-b", 0,
1324 &error_abort);
1325
1326 a = bdrv_new_open_driver(&bdrv_test, "a", BDRV_O_RDWR, &error_abort);
1327 b = bdrv_new_open_driver(&bdrv_test, "b", BDRV_O_RDWR, &error_abort);
1328 c = bdrv_new_open_driver(&bdrv_test, "c", BDRV_O_RDWR, &error_abort);
1329
1330 /* blk is a BB for parent-a */
1331 blk = blk_new(BLK_PERM_ALL, BLK_PERM_ALL);
1332 blk_insert_bs(blk, parent_a, &error_abort);
1333 bdrv_unref(parent_a);
1334
57320ca9
KW
1335 /* If we want to get bdrv_drain_invoke() to call aio_poll(), the driver
1336 * callback must not return immediately. */
1337 if (!by_parent_cb) {
1338 BDRVTestState *s = parent_a->opaque;
1339 s->sleep_in_drain_begin = true;
1340 }
1341
231281ab
KW
1342 /* Set child relationships */
1343 bdrv_ref(b);
1344 bdrv_ref(a);
1345 child_b = bdrv_attach_child(parent_b, b, "PB-B", &child_file, &error_abort);
1346 child_a = bdrv_attach_child(parent_b, a, "PB-A", &child_backing, &error_abort);
1347
1348 bdrv_ref(a);
57320ca9
KW
1349 bdrv_attach_child(parent_a, a, "PA-A",
1350 by_parent_cb ? &child_file : &detach_by_driver_cb_role,
1351 &error_abort);
231281ab
KW
1352
1353 g_assert_cmpint(parent_a->refcnt, ==, 1);
1354 g_assert_cmpint(parent_b->refcnt, ==, 1);
1355 g_assert_cmpint(a->refcnt, ==, 3);
1356 g_assert_cmpint(b->refcnt, ==, 2);
1357 g_assert_cmpint(c->refcnt, ==, 1);
1358
1359 g_assert(QLIST_FIRST(&parent_b->children) == child_a);
1360 g_assert(QLIST_NEXT(child_a, next) == child_b);
1361 g_assert(QLIST_NEXT(child_b, next) == NULL);
1362
1363 /* Start the evil write request */
57320ca9 1364 detach_by_parent_data = (struct detach_by_parent_data) {
231281ab
KW
1365 .parent_b = parent_b,
1366 .child_b = child_b,
1367 .c = c,
57320ca9 1368 .by_parent_cb = by_parent_cb,
231281ab 1369 };
57320ca9 1370 acb = blk_aio_preadv(blk, 0, &qiov, 0, detach_by_parent_aio_cb, NULL);
231281ab
KW
1371 g_assert(acb != NULL);
1372
1373 /* Drain and check the expected result */
1374 bdrv_subtree_drained_begin(parent_b);
1375
57320ca9 1376 g_assert(detach_by_parent_data.child_c != NULL);
231281ab
KW
1377
1378 g_assert_cmpint(parent_a->refcnt, ==, 1);
1379 g_assert_cmpint(parent_b->refcnt, ==, 1);
1380 g_assert_cmpint(a->refcnt, ==, 3);
1381 g_assert_cmpint(b->refcnt, ==, 1);
1382 g_assert_cmpint(c->refcnt, ==, 2);
1383
57320ca9
KW
1384 g_assert(QLIST_FIRST(&parent_b->children) == detach_by_parent_data.child_c);
1385 g_assert(QLIST_NEXT(detach_by_parent_data.child_c, next) == child_a);
231281ab
KW
1386 g_assert(QLIST_NEXT(child_a, next) == NULL);
1387
1388 g_assert_cmpint(parent_a->quiesce_counter, ==, 1);
1389 g_assert_cmpint(parent_b->quiesce_counter, ==, 1);
1390 g_assert_cmpint(a->quiesce_counter, ==, 1);
1391 g_assert_cmpint(b->quiesce_counter, ==, 0);
1392 g_assert_cmpint(c->quiesce_counter, ==, 1);
1393
1394 bdrv_subtree_drained_end(parent_b);
1395
1396 bdrv_unref(parent_b);
1397 blk_unref(blk);
1398
1399 /* XXX Once bdrv_close() unref's children instead of just detaching them,
1400 * this won't be necessary any more. */
1401 bdrv_unref(a);
1402 bdrv_unref(a);
1403 bdrv_unref(c);
1404
1405 g_assert_cmpint(a->refcnt, ==, 1);
1406 g_assert_cmpint(b->refcnt, ==, 1);
1407 g_assert_cmpint(c->refcnt, ==, 1);
1408 bdrv_unref(a);
1409 bdrv_unref(b);
1410 bdrv_unref(c);
1411}
1412
57320ca9
KW
1413static void test_detach_by_parent_cb(void)
1414{
1415 test_detach_indirect(true);
1416}
1417
1418static void test_detach_by_driver_cb(void)
1419{
1420 test_detach_indirect(false);
1421}
231281ab 1422
b994c5bc
KW
1423static void test_append_to_drained(void)
1424{
1425 BlockBackend *blk;
1426 BlockDriverState *base, *overlay;
1427 BDRVTestState *base_s, *overlay_s;
1428
1429 blk = blk_new(BLK_PERM_ALL, BLK_PERM_ALL);
1430 base = bdrv_new_open_driver(&bdrv_test, "base", BDRV_O_RDWR, &error_abort);
1431 base_s = base->opaque;
1432 blk_insert_bs(blk, base, &error_abort);
1433
1434 overlay = bdrv_new_open_driver(&bdrv_test, "overlay", BDRV_O_RDWR,
1435 &error_abort);
1436 overlay_s = overlay->opaque;
1437
1438 do_drain_begin(BDRV_DRAIN, base);
1439 g_assert_cmpint(base->quiesce_counter, ==, 1);
1440 g_assert_cmpint(base_s->drain_count, ==, 1);
1441 g_assert_cmpint(base->in_flight, ==, 0);
1442
1443 /* Takes ownership of overlay, so we don't have to unref it later */
1444 bdrv_append(overlay, base, &error_abort);
1445 g_assert_cmpint(base->in_flight, ==, 0);
1446 g_assert_cmpint(overlay->in_flight, ==, 0);
1447
1448 g_assert_cmpint(base->quiesce_counter, ==, 1);
1449 g_assert_cmpint(base_s->drain_count, ==, 1);
1450 g_assert_cmpint(overlay->quiesce_counter, ==, 1);
1451 g_assert_cmpint(overlay_s->drain_count, ==, 1);
1452
1453 do_drain_end(BDRV_DRAIN, base);
1454
1455 g_assert_cmpint(base->quiesce_counter, ==, 0);
1456 g_assert_cmpint(base_s->drain_count, ==, 0);
1457 g_assert_cmpint(overlay->quiesce_counter, ==, 0);
1458 g_assert_cmpint(overlay_s->drain_count, ==, 0);
1459
1460 bdrv_unref(base);
1461 blk_unref(blk);
1462}
1463
881cfd17
KW
1464int main(int argc, char **argv)
1465{
bb675689
KW
1466 int ret;
1467
881cfd17
KW
1468 bdrv_init();
1469 qemu_init_main_loop(&error_abort);
1470
1471 g_test_init(&argc, &argv, NULL);
bb675689 1472 qemu_event_init(&done_event, false);
881cfd17
KW
1473
1474 g_test_add_func("/bdrv-drain/driver-cb/drain_all", test_drv_cb_drain_all);
86e1c840 1475 g_test_add_func("/bdrv-drain/driver-cb/drain", test_drv_cb_drain);
d2a85d0f
KW
1476 g_test_add_func("/bdrv-drain/driver-cb/drain_subtree",
1477 test_drv_cb_drain_subtree);
881cfd17 1478
6d0252f2
KW
1479 g_test_add_func("/bdrv-drain/driver-cb/co/drain_all",
1480 test_drv_cb_co_drain_all);
0582eb10
KW
1481 g_test_add_func("/bdrv-drain/driver-cb/co/drain", test_drv_cb_co_drain);
1482 g_test_add_func("/bdrv-drain/driver-cb/co/drain_subtree",
1483 test_drv_cb_co_drain_subtree);
1484
1485
89a6ceab
KW
1486 g_test_add_func("/bdrv-drain/quiesce/drain_all", test_quiesce_drain_all);
1487 g_test_add_func("/bdrv-drain/quiesce/drain", test_quiesce_drain);
d2a85d0f
KW
1488 g_test_add_func("/bdrv-drain/quiesce/drain_subtree",
1489 test_quiesce_drain_subtree);
89a6ceab 1490
6d0252f2
KW
1491 g_test_add_func("/bdrv-drain/quiesce/co/drain_all",
1492 test_quiesce_co_drain_all);
0582eb10
KW
1493 g_test_add_func("/bdrv-drain/quiesce/co/drain", test_quiesce_co_drain);
1494 g_test_add_func("/bdrv-drain/quiesce/co/drain_subtree",
1495 test_quiesce_co_drain_subtree);
1496
6c429a6a 1497 g_test_add_func("/bdrv-drain/nested", test_nested);
27e64474 1498 g_test_add_func("/bdrv-drain/multiparent", test_multiparent);
19f7a7e5
KW
1499
1500 g_test_add_func("/bdrv-drain/graph-change/drain_subtree",
1501 test_graph_change_drain_subtree);
1502 g_test_add_func("/bdrv-drain/graph-change/drain_all",
1503 test_graph_change_drain_all);
6c429a6a 1504
bb675689
KW
1505 g_test_add_func("/bdrv-drain/iothread/drain_all", test_iothread_drain_all);
1506 g_test_add_func("/bdrv-drain/iothread/drain", test_iothread_drain);
1507 g_test_add_func("/bdrv-drain/iothread/drain_subtree",
1508 test_iothread_drain_subtree);
1509
7253220d
KW
1510 g_test_add_func("/bdrv-drain/blockjob/drain_all", test_blockjob_drain_all);
1511 g_test_add_func("/bdrv-drain/blockjob/drain", test_blockjob_drain);
d2a85d0f
KW
1512 g_test_add_func("/bdrv-drain/blockjob/drain_subtree",
1513 test_blockjob_drain_subtree);
7253220d 1514
d49725af
KW
1515 g_test_add_func("/bdrv-drain/blockjob/error/drain_all",
1516 test_blockjob_error_drain_all);
1517 g_test_add_func("/bdrv-drain/blockjob/error/drain",
1518 test_blockjob_error_drain);
1519 g_test_add_func("/bdrv-drain/blockjob/error/drain_subtree",
1520 test_blockjob_error_drain_subtree);
1521
f62c1729
KW
1522 g_test_add_func("/bdrv-drain/blockjob/iothread/drain_all",
1523 test_blockjob_iothread_drain_all);
1524 g_test_add_func("/bdrv-drain/blockjob/iothread/drain",
1525 test_blockjob_iothread_drain);
1526 g_test_add_func("/bdrv-drain/blockjob/iothread/drain_subtree",
1527 test_blockjob_iothread_drain_subtree);
1528
d49725af
KW
1529 g_test_add_func("/bdrv-drain/blockjob/iothread/error/drain_all",
1530 test_blockjob_iothread_error_drain_all);
1531 g_test_add_func("/bdrv-drain/blockjob/iothread/error/drain",
1532 test_blockjob_iothread_error_drain);
1533 g_test_add_func("/bdrv-drain/blockjob/iothread/error/drain_subtree",
1534 test_blockjob_iothread_error_drain_subtree);
1535
ebd31837 1536 g_test_add_func("/bdrv-drain/deletion/drain", test_delete_by_drain);
19f7a7e5 1537 g_test_add_func("/bdrv-drain/detach/drain_all", test_detach_by_drain_all);
ebd31837
KW
1538 g_test_add_func("/bdrv-drain/detach/drain", test_detach_by_drain);
1539 g_test_add_func("/bdrv-drain/detach/drain_subtree", test_detach_by_drain_subtree);
231281ab 1540 g_test_add_func("/bdrv-drain/detach/parent_cb", test_detach_by_parent_cb);
57320ca9 1541 g_test_add_func("/bdrv-drain/detach/driver_cb", test_detach_by_driver_cb);
4c8158e3 1542
b994c5bc
KW
1543 g_test_add_func("/bdrv-drain/attach/drain", test_append_to_drained);
1544
bb675689
KW
1545 ret = g_test_run();
1546 qemu_event_destroy(&done_event);
1547 return ret;
881cfd17 1548}