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