]> git.proxmox.com Git - mirror_qemu.git/blame - tests/test-bdrv-drain.c
block: Pass BdrvChildRole in remaining cases
[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"
db725815 30#include "qemu/main-loop.h"
bb675689
KW
31#include "iothread.h"
32
33static QemuEvent done_event;
881cfd17
KW
34
35typedef struct BDRVTestState {
36 int drain_count;
bb675689 37 AioContext *bh_indirection_ctx;
57320ca9 38 bool sleep_in_drain_begin;
881cfd17
KW
39} BDRVTestState;
40
41static void coroutine_fn bdrv_test_co_drain_begin(BlockDriverState *bs)
42{
43 BDRVTestState *s = bs->opaque;
44 s->drain_count++;
57320ca9
KW
45 if (s->sleep_in_drain_begin) {
46 qemu_co_sleep_ns(QEMU_CLOCK_REALTIME, 100000);
47 }
881cfd17
KW
48}
49
50static void coroutine_fn bdrv_test_co_drain_end(BlockDriverState *bs)
51{
52 BDRVTestState *s = bs->opaque;
53 s->drain_count--;
54}
55
56static void bdrv_test_close(BlockDriverState *bs)
57{
58 BDRVTestState *s = bs->opaque;
59 g_assert_cmpint(s->drain_count, >, 0);
60}
61
bb675689
KW
62static void co_reenter_bh(void *opaque)
63{
64 aio_co_wake(opaque);
65}
66
881cfd17
KW
67static int coroutine_fn bdrv_test_co_preadv(BlockDriverState *bs,
68 uint64_t offset, uint64_t bytes,
69 QEMUIOVector *qiov, int flags)
70{
bb675689
KW
71 BDRVTestState *s = bs->opaque;
72
881cfd17
KW
73 /* We want this request to stay until the polling loop in drain waits for
74 * it to complete. We need to sleep a while as bdrv_drain_invoke() comes
75 * first and polls its result, too, but it shouldn't accidentally complete
76 * this request yet. */
77 qemu_co_sleep_ns(QEMU_CLOCK_REALTIME, 100000);
78
bb675689
KW
79 if (s->bh_indirection_ctx) {
80 aio_bh_schedule_oneshot(s->bh_indirection_ctx, co_reenter_bh,
81 qemu_coroutine_self());
82 qemu_coroutine_yield();
83 }
84
881cfd17
KW
85 return 0;
86}
87
57320ca9 88static void bdrv_test_child_perm(BlockDriverState *bs, BdrvChild *c,
bd86fb99 89 const BdrvChildClass *child_class,
bf8e925e 90 BdrvChildRole role,
57320ca9
KW
91 BlockReopenQueue *reopen_queue,
92 uint64_t perm, uint64_t shared,
93 uint64_t *nperm, uint64_t *nshared)
94{
bd86fb99 95 /*
f6de853f
HR
96 * bdrv_default_perms() accepts nothing else, so disguise
97 * detach_by_driver_cb_parent.
bd86fb99 98 */
f6de853f 99 child_class = &child_of_bds;
57320ca9 100
69dca43d
HR
101 bdrv_default_perms(bs, c, child_class, role, reopen_queue,
102 perm, shared, nperm, nshared);
57320ca9
KW
103}
104
9746b35c
HR
105static int bdrv_test_change_backing_file(BlockDriverState *bs,
106 const char *backing_file,
107 const char *backing_fmt)
108{
109 return 0;
110}
111
881cfd17
KW
112static BlockDriver bdrv_test = {
113 .format_name = "test",
114 .instance_size = sizeof(BDRVTestState),
115
116 .bdrv_close = bdrv_test_close,
117 .bdrv_co_preadv = bdrv_test_co_preadv,
118
119 .bdrv_co_drain_begin = bdrv_test_co_drain_begin,
120 .bdrv_co_drain_end = bdrv_test_co_drain_end,
86e1c840 121
57320ca9 122 .bdrv_child_perm = bdrv_test_child_perm,
9746b35c
HR
123
124 .bdrv_change_backing_file = bdrv_test_change_backing_file,
881cfd17
KW
125};
126
127static void aio_ret_cb(void *opaque, int ret)
128{
129 int *aio_ret = opaque;
130 *aio_ret = ret;
131}
132
0582eb10
KW
133typedef struct CallInCoroutineData {
134 void (*entry)(void);
135 bool done;
136} CallInCoroutineData;
137
138static coroutine_fn void call_in_coroutine_entry(void *opaque)
139{
140 CallInCoroutineData *data = opaque;
141
142 data->entry();
143 data->done = true;
144}
145
146static void call_in_coroutine(void (*entry)(void))
147{
148 Coroutine *co;
149 CallInCoroutineData data = {
150 .entry = entry,
151 .done = false,
152 };
153
154 co = qemu_coroutine_create(call_in_coroutine_entry, &data);
155 qemu_coroutine_enter(co);
156 while (!data.done) {
157 aio_poll(qemu_get_aio_context(), true);
158 }
159}
160
86e1c840
KW
161enum drain_type {
162 BDRV_DRAIN_ALL,
163 BDRV_DRAIN,
d2a85d0f 164 BDRV_SUBTREE_DRAIN,
6c429a6a 165 DRAIN_TYPE_MAX,
86e1c840
KW
166};
167
168static void do_drain_begin(enum drain_type drain_type, BlockDriverState *bs)
169{
170 switch (drain_type) {
171 case BDRV_DRAIN_ALL: bdrv_drain_all_begin(); break;
172 case BDRV_DRAIN: bdrv_drained_begin(bs); break;
d2a85d0f 173 case BDRV_SUBTREE_DRAIN: bdrv_subtree_drained_begin(bs); break;
86e1c840
KW
174 default: g_assert_not_reached();
175 }
176}
177
178static void do_drain_end(enum drain_type drain_type, BlockDriverState *bs)
179{
180 switch (drain_type) {
181 case BDRV_DRAIN_ALL: bdrv_drain_all_end(); break;
182 case BDRV_DRAIN: bdrv_drained_end(bs); break;
d2a85d0f 183 case BDRV_SUBTREE_DRAIN: bdrv_subtree_drained_end(bs); break;
86e1c840
KW
184 default: g_assert_not_reached();
185 }
186}
187
f62c1729
KW
188static void do_drain_begin_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_begin(drain_type, bs);
194 if (drain_type != BDRV_DRAIN_ALL) {
195 aio_context_release(bdrv_get_aio_context(bs));
196 }
197}
198
199static void do_drain_end_unlocked(enum drain_type drain_type, BlockDriverState *bs)
200{
201 if (drain_type != BDRV_DRAIN_ALL) {
202 aio_context_acquire(bdrv_get_aio_context(bs));
203 }
204 do_drain_end(drain_type, bs);
205 if (drain_type != BDRV_DRAIN_ALL) {
206 aio_context_release(bdrv_get_aio_context(bs));
207 }
208}
209
86e1c840 210static void test_drv_cb_common(enum drain_type drain_type, bool recursive)
881cfd17
KW
211{
212 BlockBackend *blk;
86e1c840
KW
213 BlockDriverState *bs, *backing;
214 BDRVTestState *s, *backing_s;
881cfd17
KW
215 BlockAIOCB *acb;
216 int aio_ret;
217
405d8fe0 218 QEMUIOVector qiov = QEMU_IOVEC_INIT_BUF(qiov, NULL, 0);
881cfd17 219
d861ab3a 220 blk = blk_new(qemu_get_aio_context(), BLK_PERM_ALL, BLK_PERM_ALL);
881cfd17
KW
221 bs = bdrv_new_open_driver(&bdrv_test, "test-node", BDRV_O_RDWR,
222 &error_abort);
223 s = bs->opaque;
224 blk_insert_bs(blk, bs, &error_abort);
225
86e1c840
KW
226 backing = bdrv_new_open_driver(&bdrv_test, "backing", 0, &error_abort);
227 backing_s = backing->opaque;
228 bdrv_set_backing_hd(bs, backing, &error_abort);
229
881cfd17
KW
230 /* Simple bdrv_drain_all_begin/end pair, check that CBs are called */
231 g_assert_cmpint(s->drain_count, ==, 0);
86e1c840
KW
232 g_assert_cmpint(backing_s->drain_count, ==, 0);
233
234 do_drain_begin(drain_type, bs);
235
881cfd17 236 g_assert_cmpint(s->drain_count, ==, 1);
86e1c840
KW
237 g_assert_cmpint(backing_s->drain_count, ==, !!recursive);
238
239 do_drain_end(drain_type, bs);
240
881cfd17 241 g_assert_cmpint(s->drain_count, ==, 0);
86e1c840 242 g_assert_cmpint(backing_s->drain_count, ==, 0);
881cfd17
KW
243
244 /* Now do the same while a request is pending */
245 aio_ret = -EINPROGRESS;
246 acb = blk_aio_preadv(blk, 0, &qiov, 0, aio_ret_cb, &aio_ret);
247 g_assert(acb != NULL);
248 g_assert_cmpint(aio_ret, ==, -EINPROGRESS);
249
250 g_assert_cmpint(s->drain_count, ==, 0);
86e1c840
KW
251 g_assert_cmpint(backing_s->drain_count, ==, 0);
252
253 do_drain_begin(drain_type, bs);
254
881cfd17
KW
255 g_assert_cmpint(aio_ret, ==, 0);
256 g_assert_cmpint(s->drain_count, ==, 1);
86e1c840
KW
257 g_assert_cmpint(backing_s->drain_count, ==, !!recursive);
258
259 do_drain_end(drain_type, bs);
260
881cfd17 261 g_assert_cmpint(s->drain_count, ==, 0);
86e1c840 262 g_assert_cmpint(backing_s->drain_count, ==, 0);
881cfd17 263
86e1c840 264 bdrv_unref(backing);
881cfd17
KW
265 bdrv_unref(bs);
266 blk_unref(blk);
267}
268
86e1c840
KW
269static void test_drv_cb_drain_all(void)
270{
271 test_drv_cb_common(BDRV_DRAIN_ALL, true);
272}
273
274static void test_drv_cb_drain(void)
275{
276 test_drv_cb_common(BDRV_DRAIN, false);
277}
278
d2a85d0f
KW
279static void test_drv_cb_drain_subtree(void)
280{
281 test_drv_cb_common(BDRV_SUBTREE_DRAIN, true);
282}
283
6d0252f2
KW
284static void test_drv_cb_co_drain_all(void)
285{
286 call_in_coroutine(test_drv_cb_drain_all);
287}
288
0582eb10
KW
289static void test_drv_cb_co_drain(void)
290{
291 call_in_coroutine(test_drv_cb_drain);
292}
293
294static void test_drv_cb_co_drain_subtree(void)
295{
296 call_in_coroutine(test_drv_cb_drain_subtree);
297}
298
89a6ceab
KW
299static void test_quiesce_common(enum drain_type drain_type, bool recursive)
300{
301 BlockBackend *blk;
302 BlockDriverState *bs, *backing;
303
d861ab3a 304 blk = blk_new(qemu_get_aio_context(), BLK_PERM_ALL, BLK_PERM_ALL);
89a6ceab
KW
305 bs = bdrv_new_open_driver(&bdrv_test, "test-node", BDRV_O_RDWR,
306 &error_abort);
307 blk_insert_bs(blk, bs, &error_abort);
308
309 backing = bdrv_new_open_driver(&bdrv_test, "backing", 0, &error_abort);
310 bdrv_set_backing_hd(bs, backing, &error_abort);
311
312 g_assert_cmpint(bs->quiesce_counter, ==, 0);
313 g_assert_cmpint(backing->quiesce_counter, ==, 0);
314
315 do_drain_begin(drain_type, bs);
316
317 g_assert_cmpint(bs->quiesce_counter, ==, 1);
318 g_assert_cmpint(backing->quiesce_counter, ==, !!recursive);
319
320 do_drain_end(drain_type, bs);
321
322 g_assert_cmpint(bs->quiesce_counter, ==, 0);
323 g_assert_cmpint(backing->quiesce_counter, ==, 0);
324
325 bdrv_unref(backing);
326 bdrv_unref(bs);
327 blk_unref(blk);
328}
329
330static void test_quiesce_drain_all(void)
331{
79ab8b21 332 test_quiesce_common(BDRV_DRAIN_ALL, true);
89a6ceab
KW
333}
334
335static void test_quiesce_drain(void)
336{
337 test_quiesce_common(BDRV_DRAIN, false);
338}
339
d2a85d0f
KW
340static void test_quiesce_drain_subtree(void)
341{
342 test_quiesce_common(BDRV_SUBTREE_DRAIN, true);
343}
344
6d0252f2
KW
345static void test_quiesce_co_drain_all(void)
346{
347 call_in_coroutine(test_quiesce_drain_all);
348}
349
0582eb10
KW
350static void test_quiesce_co_drain(void)
351{
352 call_in_coroutine(test_quiesce_drain);
353}
354
355static void test_quiesce_co_drain_subtree(void)
356{
357 call_in_coroutine(test_quiesce_drain_subtree);
358}
359
6c429a6a
KW
360static void test_nested(void)
361{
362 BlockBackend *blk;
363 BlockDriverState *bs, *backing;
364 BDRVTestState *s, *backing_s;
365 enum drain_type outer, inner;
366
d861ab3a 367 blk = blk_new(qemu_get_aio_context(), BLK_PERM_ALL, BLK_PERM_ALL);
6c429a6a
KW
368 bs = bdrv_new_open_driver(&bdrv_test, "test-node", BDRV_O_RDWR,
369 &error_abort);
370 s = bs->opaque;
371 blk_insert_bs(blk, bs, &error_abort);
372
373 backing = bdrv_new_open_driver(&bdrv_test, "backing", 0, &error_abort);
374 backing_s = backing->opaque;
375 bdrv_set_backing_hd(bs, backing, &error_abort);
376
377 for (outer = 0; outer < DRAIN_TYPE_MAX; outer++) {
378 for (inner = 0; inner < DRAIN_TYPE_MAX; inner++) {
79ab8b21 379 int backing_quiesce = (outer != BDRV_DRAIN) +
6c429a6a
KW
380 (inner != BDRV_DRAIN);
381
382 g_assert_cmpint(bs->quiesce_counter, ==, 0);
383 g_assert_cmpint(backing->quiesce_counter, ==, 0);
384 g_assert_cmpint(s->drain_count, ==, 0);
385 g_assert_cmpint(backing_s->drain_count, ==, 0);
386
387 do_drain_begin(outer, bs);
388 do_drain_begin(inner, bs);
389
79ab8b21 390 g_assert_cmpint(bs->quiesce_counter, ==, 2);
6c429a6a
KW
391 g_assert_cmpint(backing->quiesce_counter, ==, backing_quiesce);
392 g_assert_cmpint(s->drain_count, ==, 2);
79ab8b21 393 g_assert_cmpint(backing_s->drain_count, ==, backing_quiesce);
6c429a6a
KW
394
395 do_drain_end(inner, bs);
396 do_drain_end(outer, bs);
397
398 g_assert_cmpint(bs->quiesce_counter, ==, 0);
399 g_assert_cmpint(backing->quiesce_counter, ==, 0);
400 g_assert_cmpint(s->drain_count, ==, 0);
401 g_assert_cmpint(backing_s->drain_count, ==, 0);
402 }
403 }
404
405 bdrv_unref(backing);
406 bdrv_unref(bs);
407 blk_unref(blk);
408}
409
27e64474
KW
410static void test_multiparent(void)
411{
412 BlockBackend *blk_a, *blk_b;
413 BlockDriverState *bs_a, *bs_b, *backing;
414 BDRVTestState *a_s, *b_s, *backing_s;
415
d861ab3a 416 blk_a = blk_new(qemu_get_aio_context(), BLK_PERM_ALL, BLK_PERM_ALL);
27e64474
KW
417 bs_a = bdrv_new_open_driver(&bdrv_test, "test-node-a", BDRV_O_RDWR,
418 &error_abort);
419 a_s = bs_a->opaque;
420 blk_insert_bs(blk_a, bs_a, &error_abort);
421
d861ab3a 422 blk_b = blk_new(qemu_get_aio_context(), BLK_PERM_ALL, BLK_PERM_ALL);
27e64474
KW
423 bs_b = bdrv_new_open_driver(&bdrv_test, "test-node-b", BDRV_O_RDWR,
424 &error_abort);
425 b_s = bs_b->opaque;
426 blk_insert_bs(blk_b, bs_b, &error_abort);
427
428 backing = bdrv_new_open_driver(&bdrv_test, "backing", 0, &error_abort);
429 backing_s = backing->opaque;
430 bdrv_set_backing_hd(bs_a, backing, &error_abort);
431 bdrv_set_backing_hd(bs_b, backing, &error_abort);
432
433 g_assert_cmpint(bs_a->quiesce_counter, ==, 0);
434 g_assert_cmpint(bs_b->quiesce_counter, ==, 0);
435 g_assert_cmpint(backing->quiesce_counter, ==, 0);
436 g_assert_cmpint(a_s->drain_count, ==, 0);
437 g_assert_cmpint(b_s->drain_count, ==, 0);
438 g_assert_cmpint(backing_s->drain_count, ==, 0);
439
440 do_drain_begin(BDRV_SUBTREE_DRAIN, bs_a);
441
442 g_assert_cmpint(bs_a->quiesce_counter, ==, 1);
443 g_assert_cmpint(bs_b->quiesce_counter, ==, 1);
444 g_assert_cmpint(backing->quiesce_counter, ==, 1);
445 g_assert_cmpint(a_s->drain_count, ==, 1);
446 g_assert_cmpint(b_s->drain_count, ==, 1);
447 g_assert_cmpint(backing_s->drain_count, ==, 1);
448
449 do_drain_begin(BDRV_SUBTREE_DRAIN, bs_b);
450
451 g_assert_cmpint(bs_a->quiesce_counter, ==, 2);
452 g_assert_cmpint(bs_b->quiesce_counter, ==, 2);
453 g_assert_cmpint(backing->quiesce_counter, ==, 2);
454 g_assert_cmpint(a_s->drain_count, ==, 2);
455 g_assert_cmpint(b_s->drain_count, ==, 2);
456 g_assert_cmpint(backing_s->drain_count, ==, 2);
457
458 do_drain_end(BDRV_SUBTREE_DRAIN, bs_b);
459
460 g_assert_cmpint(bs_a->quiesce_counter, ==, 1);
461 g_assert_cmpint(bs_b->quiesce_counter, ==, 1);
462 g_assert_cmpint(backing->quiesce_counter, ==, 1);
463 g_assert_cmpint(a_s->drain_count, ==, 1);
464 g_assert_cmpint(b_s->drain_count, ==, 1);
465 g_assert_cmpint(backing_s->drain_count, ==, 1);
466
467 do_drain_end(BDRV_SUBTREE_DRAIN, bs_a);
468
469 g_assert_cmpint(bs_a->quiesce_counter, ==, 0);
470 g_assert_cmpint(bs_b->quiesce_counter, ==, 0);
471 g_assert_cmpint(backing->quiesce_counter, ==, 0);
472 g_assert_cmpint(a_s->drain_count, ==, 0);
473 g_assert_cmpint(b_s->drain_count, ==, 0);
474 g_assert_cmpint(backing_s->drain_count, ==, 0);
475
476 bdrv_unref(backing);
477 bdrv_unref(bs_a);
478 bdrv_unref(bs_b);
479 blk_unref(blk_a);
480 blk_unref(blk_b);
481}
482
19f7a7e5 483static void test_graph_change_drain_subtree(void)
acebcf8d
KW
484{
485 BlockBackend *blk_a, *blk_b;
486 BlockDriverState *bs_a, *bs_b, *backing;
487 BDRVTestState *a_s, *b_s, *backing_s;
488
d861ab3a 489 blk_a = blk_new(qemu_get_aio_context(), BLK_PERM_ALL, BLK_PERM_ALL);
acebcf8d
KW
490 bs_a = bdrv_new_open_driver(&bdrv_test, "test-node-a", BDRV_O_RDWR,
491 &error_abort);
492 a_s = bs_a->opaque;
493 blk_insert_bs(blk_a, bs_a, &error_abort);
494
d861ab3a 495 blk_b = blk_new(qemu_get_aio_context(), BLK_PERM_ALL, BLK_PERM_ALL);
acebcf8d
KW
496 bs_b = bdrv_new_open_driver(&bdrv_test, "test-node-b", BDRV_O_RDWR,
497 &error_abort);
498 b_s = bs_b->opaque;
499 blk_insert_bs(blk_b, bs_b, &error_abort);
500
501 backing = bdrv_new_open_driver(&bdrv_test, "backing", 0, &error_abort);
502 backing_s = backing->opaque;
503 bdrv_set_backing_hd(bs_a, backing, &error_abort);
504
505 g_assert_cmpint(bs_a->quiesce_counter, ==, 0);
506 g_assert_cmpint(bs_b->quiesce_counter, ==, 0);
507 g_assert_cmpint(backing->quiesce_counter, ==, 0);
508 g_assert_cmpint(a_s->drain_count, ==, 0);
509 g_assert_cmpint(b_s->drain_count, ==, 0);
510 g_assert_cmpint(backing_s->drain_count, ==, 0);
511
512 do_drain_begin(BDRV_SUBTREE_DRAIN, bs_a);
513 do_drain_begin(BDRV_SUBTREE_DRAIN, bs_a);
514 do_drain_begin(BDRV_SUBTREE_DRAIN, bs_a);
515 do_drain_begin(BDRV_SUBTREE_DRAIN, bs_b);
516 do_drain_begin(BDRV_SUBTREE_DRAIN, bs_b);
517
518 bdrv_set_backing_hd(bs_b, backing, &error_abort);
519 g_assert_cmpint(bs_a->quiesce_counter, ==, 5);
520 g_assert_cmpint(bs_b->quiesce_counter, ==, 5);
521 g_assert_cmpint(backing->quiesce_counter, ==, 5);
522 g_assert_cmpint(a_s->drain_count, ==, 5);
523 g_assert_cmpint(b_s->drain_count, ==, 5);
524 g_assert_cmpint(backing_s->drain_count, ==, 5);
525
526 bdrv_set_backing_hd(bs_b, NULL, &error_abort);
527 g_assert_cmpint(bs_a->quiesce_counter, ==, 3);
528 g_assert_cmpint(bs_b->quiesce_counter, ==, 2);
529 g_assert_cmpint(backing->quiesce_counter, ==, 3);
530 g_assert_cmpint(a_s->drain_count, ==, 3);
531 g_assert_cmpint(b_s->drain_count, ==, 2);
532 g_assert_cmpint(backing_s->drain_count, ==, 3);
533
534 bdrv_set_backing_hd(bs_b, backing, &error_abort);
535 g_assert_cmpint(bs_a->quiesce_counter, ==, 5);
536 g_assert_cmpint(bs_b->quiesce_counter, ==, 5);
537 g_assert_cmpint(backing->quiesce_counter, ==, 5);
538 g_assert_cmpint(a_s->drain_count, ==, 5);
539 g_assert_cmpint(b_s->drain_count, ==, 5);
540 g_assert_cmpint(backing_s->drain_count, ==, 5);
541
542 do_drain_end(BDRV_SUBTREE_DRAIN, bs_b);
543 do_drain_end(BDRV_SUBTREE_DRAIN, bs_b);
544 do_drain_end(BDRV_SUBTREE_DRAIN, bs_a);
545 do_drain_end(BDRV_SUBTREE_DRAIN, bs_a);
546 do_drain_end(BDRV_SUBTREE_DRAIN, bs_a);
547
548 g_assert_cmpint(bs_a->quiesce_counter, ==, 0);
549 g_assert_cmpint(bs_b->quiesce_counter, ==, 0);
550 g_assert_cmpint(backing->quiesce_counter, ==, 0);
551 g_assert_cmpint(a_s->drain_count, ==, 0);
552 g_assert_cmpint(b_s->drain_count, ==, 0);
553 g_assert_cmpint(backing_s->drain_count, ==, 0);
554
555 bdrv_unref(backing);
556 bdrv_unref(bs_a);
557 bdrv_unref(bs_b);
558 blk_unref(blk_a);
559 blk_unref(blk_b);
560}
561
19f7a7e5
KW
562static void test_graph_change_drain_all(void)
563{
564 BlockBackend *blk_a, *blk_b;
565 BlockDriverState *bs_a, *bs_b;
566 BDRVTestState *a_s, *b_s;
567
568 /* Create node A with a BlockBackend */
d861ab3a 569 blk_a = blk_new(qemu_get_aio_context(), BLK_PERM_ALL, BLK_PERM_ALL);
19f7a7e5
KW
570 bs_a = bdrv_new_open_driver(&bdrv_test, "test-node-a", BDRV_O_RDWR,
571 &error_abort);
572 a_s = bs_a->opaque;
573 blk_insert_bs(blk_a, bs_a, &error_abort);
574
575 g_assert_cmpint(bs_a->quiesce_counter, ==, 0);
576 g_assert_cmpint(a_s->drain_count, ==, 0);
577
578 /* Call bdrv_drain_all_begin() */
579 bdrv_drain_all_begin();
580
581 g_assert_cmpint(bs_a->quiesce_counter, ==, 1);
582 g_assert_cmpint(a_s->drain_count, ==, 1);
583
584 /* Create node B with a BlockBackend */
d861ab3a 585 blk_b = blk_new(qemu_get_aio_context(), BLK_PERM_ALL, BLK_PERM_ALL);
19f7a7e5
KW
586 bs_b = bdrv_new_open_driver(&bdrv_test, "test-node-b", BDRV_O_RDWR,
587 &error_abort);
588 b_s = bs_b->opaque;
589 blk_insert_bs(blk_b, bs_b, &error_abort);
590
591 g_assert_cmpint(bs_a->quiesce_counter, ==, 1);
592 g_assert_cmpint(bs_b->quiesce_counter, ==, 1);
593 g_assert_cmpint(a_s->drain_count, ==, 1);
594 g_assert_cmpint(b_s->drain_count, ==, 1);
595
596 /* Unref and finally delete node A */
597 blk_unref(blk_a);
598
599 g_assert_cmpint(bs_a->quiesce_counter, ==, 1);
600 g_assert_cmpint(bs_b->quiesce_counter, ==, 1);
601 g_assert_cmpint(a_s->drain_count, ==, 1);
602 g_assert_cmpint(b_s->drain_count, ==, 1);
603
604 bdrv_unref(bs_a);
605
606 g_assert_cmpint(bs_b->quiesce_counter, ==, 1);
607 g_assert_cmpint(b_s->drain_count, ==, 1);
608
609 /* End the drained section */
610 bdrv_drain_all_end();
611
612 g_assert_cmpint(bs_b->quiesce_counter, ==, 0);
613 g_assert_cmpint(b_s->drain_count, ==, 0);
614
615 bdrv_unref(bs_b);
616 blk_unref(blk_b);
617}
618
bb675689
KW
619struct test_iothread_data {
620 BlockDriverState *bs;
621 enum drain_type drain_type;
622 int *aio_ret;
623};
624
625static void test_iothread_drain_entry(void *opaque)
626{
627 struct test_iothread_data *data = opaque;
628
629 aio_context_acquire(bdrv_get_aio_context(data->bs));
630 do_drain_begin(data->drain_type, data->bs);
631 g_assert_cmpint(*data->aio_ret, ==, 0);
632 do_drain_end(data->drain_type, data->bs);
633 aio_context_release(bdrv_get_aio_context(data->bs));
634
635 qemu_event_set(&done_event);
636}
637
638static void test_iothread_aio_cb(void *opaque, int ret)
639{
640 int *aio_ret = opaque;
641 *aio_ret = ret;
642 qemu_event_set(&done_event);
643}
644
ecc1a5c7
KW
645static void test_iothread_main_thread_bh(void *opaque)
646{
647 struct test_iothread_data *data = opaque;
648
649 /* Test that the AioContext is not yet locked in a random BH that is
650 * executed during drain, otherwise this would deadlock. */
651 aio_context_acquire(bdrv_get_aio_context(data->bs));
652 bdrv_flush(data->bs);
653 aio_context_release(bdrv_get_aio_context(data->bs));
654}
655
bb675689
KW
656/*
657 * Starts an AIO request on a BDS that runs in the AioContext of iothread 1.
658 * The request involves a BH on iothread 2 before it can complete.
659 *
660 * @drain_thread = 0 means that do_drain_begin/end are called from the main
661 * thread, @drain_thread = 1 means that they are called from iothread 1. Drain
662 * for this BDS cannot be called from iothread 2 because only the main thread
663 * may do cross-AioContext polling.
664 */
665static void test_iothread_common(enum drain_type drain_type, int drain_thread)
666{
667 BlockBackend *blk;
668 BlockDriverState *bs;
669 BDRVTestState *s;
670 BlockAIOCB *acb;
671 int aio_ret;
672 struct test_iothread_data data;
673
674 IOThread *a = iothread_new();
675 IOThread *b = iothread_new();
676 AioContext *ctx_a = iothread_get_aio_context(a);
677 AioContext *ctx_b = iothread_get_aio_context(b);
678
405d8fe0 679 QEMUIOVector qiov = QEMU_IOVEC_INIT_BUF(qiov, NULL, 0);
bb675689
KW
680
681 /* bdrv_drain_all() may only be called from the main loop thread */
682 if (drain_type == BDRV_DRAIN_ALL && drain_thread != 0) {
683 goto out;
684 }
685
d861ab3a 686 blk = blk_new(qemu_get_aio_context(), BLK_PERM_ALL, BLK_PERM_ALL);
bb675689
KW
687 bs = bdrv_new_open_driver(&bdrv_test, "test-node", BDRV_O_RDWR,
688 &error_abort);
689 s = bs->opaque;
690 blk_insert_bs(blk, bs, &error_abort);
cf312932 691 blk_set_disable_request_queuing(blk, true);
bb675689 692
97896a48 693 blk_set_aio_context(blk, ctx_a, &error_abort);
bb675689
KW
694 aio_context_acquire(ctx_a);
695
696 s->bh_indirection_ctx = ctx_b;
697
698 aio_ret = -EINPROGRESS;
dd353157
KW
699 qemu_event_reset(&done_event);
700
bb675689
KW
701 if (drain_thread == 0) {
702 acb = blk_aio_preadv(blk, 0, &qiov, 0, test_iothread_aio_cb, &aio_ret);
703 } else {
704 acb = blk_aio_preadv(blk, 0, &qiov, 0, aio_ret_cb, &aio_ret);
705 }
706 g_assert(acb != NULL);
707 g_assert_cmpint(aio_ret, ==, -EINPROGRESS);
708
709 aio_context_release(ctx_a);
710
711 data = (struct test_iothread_data) {
712 .bs = bs,
713 .drain_type = drain_type,
714 .aio_ret = &aio_ret,
715 };
716
717 switch (drain_thread) {
718 case 0:
719 if (drain_type != BDRV_DRAIN_ALL) {
720 aio_context_acquire(ctx_a);
721 }
722
ecc1a5c7
KW
723 aio_bh_schedule_oneshot(ctx_a, test_iothread_main_thread_bh, &data);
724
bb675689
KW
725 /* The request is running on the IOThread a. Draining its block device
726 * will make sure that it has completed as far as the BDS is concerned,
727 * but the drain in this thread can continue immediately after
728 * bdrv_dec_in_flight() and aio_ret might be assigned only slightly
729 * later. */
bb675689
KW
730 do_drain_begin(drain_type, bs);
731 g_assert_cmpint(bs->in_flight, ==, 0);
732
733 if (drain_type != BDRV_DRAIN_ALL) {
734 aio_context_release(ctx_a);
735 }
736 qemu_event_wait(&done_event);
737 if (drain_type != BDRV_DRAIN_ALL) {
738 aio_context_acquire(ctx_a);
739 }
740
741 g_assert_cmpint(aio_ret, ==, 0);
742 do_drain_end(drain_type, bs);
743
744 if (drain_type != BDRV_DRAIN_ALL) {
745 aio_context_release(ctx_a);
746 }
747 break;
748 case 1:
bb675689
KW
749 aio_bh_schedule_oneshot(ctx_a, test_iothread_drain_entry, &data);
750 qemu_event_wait(&done_event);
751 break;
752 default:
753 g_assert_not_reached();
754 }
755
756 aio_context_acquire(ctx_a);
97896a48 757 blk_set_aio_context(blk, qemu_get_aio_context(), &error_abort);
bb675689
KW
758 aio_context_release(ctx_a);
759
760 bdrv_unref(bs);
761 blk_unref(blk);
762
763out:
764 iothread_join(a);
765 iothread_join(b);
766}
767
768static void test_iothread_drain_all(void)
769{
770 test_iothread_common(BDRV_DRAIN_ALL, 0);
771 test_iothread_common(BDRV_DRAIN_ALL, 1);
772}
773
774static void test_iothread_drain(void)
775{
776 test_iothread_common(BDRV_DRAIN, 0);
777 test_iothread_common(BDRV_DRAIN, 1);
778}
779
780static void test_iothread_drain_subtree(void)
781{
782 test_iothread_common(BDRV_SUBTREE_DRAIN, 0);
783 test_iothread_common(BDRV_SUBTREE_DRAIN, 1);
784}
785
7253220d
KW
786
787typedef struct TestBlockJob {
788 BlockJob common;
d49725af
KW
789 int run_ret;
790 int prepare_ret;
d8b3afd5 791 bool running;
7253220d
KW
792 bool should_complete;
793} TestBlockJob;
794
ae23dde9
KW
795static int test_job_prepare(Job *job)
796{
797 TestBlockJob *s = container_of(job, TestBlockJob, common.job);
798
799 /* Provoke an AIO_WAIT_WHILE() call to verify there is no deadlock */
800 blk_flush(s->common.blk);
d49725af
KW
801 return s->prepare_ret;
802}
803
804static void test_job_commit(Job *job)
805{
806 TestBlockJob *s = container_of(job, TestBlockJob, common.job);
807
808 /* Provoke an AIO_WAIT_WHILE() call to verify there is no deadlock */
809 blk_flush(s->common.blk);
810}
811
812static void test_job_abort(Job *job)
813{
814 TestBlockJob *s = container_of(job, TestBlockJob, common.job);
815
816 /* Provoke an AIO_WAIT_WHILE() call to verify there is no deadlock */
817 blk_flush(s->common.blk);
ae23dde9
KW
818}
819
f67432a2 820static int coroutine_fn test_job_run(Job *job, Error **errp)
7253220d 821{
f67432a2 822 TestBlockJob *s = container_of(job, TestBlockJob, common.job);
7253220d 823
d8b3afd5
KW
824 /* We are running the actual job code past the pause point in
825 * job_co_entry(). */
826 s->running = true;
827
2e1795b5 828 job_transition_to_ready(&s->common.job);
7253220d 829 while (!s->should_complete) {
5599c162
KW
830 /* Avoid job_sleep_ns() because it marks the job as !busy. We want to
831 * emulate some actual activity (probably some I/O) here so that drain
832 * has to wait for this activity to stop. */
d8b3afd5
KW
833 qemu_co_sleep_ns(QEMU_CLOCK_REALTIME, 1000000);
834
89bd0305 835 job_pause_point(&s->common.job);
7253220d
KW
836 }
837
d49725af 838 return s->run_ret;
7253220d
KW
839}
840
3453d972 841static void test_job_complete(Job *job, Error **errp)
7253220d 842{
3453d972 843 TestBlockJob *s = container_of(job, TestBlockJob, common.job);
7253220d
KW
844 s->should_complete = true;
845}
846
847BlockJobDriver test_job_driver = {
33e9e9bd
KW
848 .job_driver = {
849 .instance_size = sizeof(TestBlockJob),
80fa2c75 850 .free = block_job_free,
b15de828 851 .user_resume = block_job_user_resume,
f67432a2 852 .run = test_job_run,
3453d972 853 .complete = test_job_complete,
ae23dde9 854 .prepare = test_job_prepare,
d49725af
KW
855 .commit = test_job_commit,
856 .abort = test_job_abort,
33e9e9bd 857 },
7253220d
KW
858};
859
d49725af
KW
860enum test_job_result {
861 TEST_JOB_SUCCESS,
862 TEST_JOB_FAIL_RUN,
863 TEST_JOB_FAIL_PREPARE,
864};
865
d8b3afd5
KW
866enum test_job_drain_node {
867 TEST_JOB_DRAIN_SRC,
868 TEST_JOB_DRAIN_SRC_CHILD,
869 TEST_JOB_DRAIN_SRC_PARENT,
870};
871
872static void test_blockjob_common_drain_node(enum drain_type drain_type,
873 bool use_iothread,
874 enum test_job_result result,
875 enum test_job_drain_node drain_node)
7253220d
KW
876{
877 BlockBackend *blk_src, *blk_target;
d8b3afd5 878 BlockDriverState *src, *src_backing, *src_overlay, *target, *drain_bs;
7253220d 879 BlockJob *job;
d49725af 880 TestBlockJob *tjob;
f62c1729
KW
881 IOThread *iothread = NULL;
882 AioContext *ctx;
7253220d
KW
883 int ret;
884
885 src = bdrv_new_open_driver(&bdrv_test, "source", BDRV_O_RDWR,
886 &error_abort);
d8b3afd5
KW
887 src_backing = bdrv_new_open_driver(&bdrv_test, "source-backing",
888 BDRV_O_RDWR, &error_abort);
889 src_overlay = bdrv_new_open_driver(&bdrv_test, "source-overlay",
890 BDRV_O_RDWR, &error_abort);
891
892 bdrv_set_backing_hd(src_overlay, src, &error_abort);
893 bdrv_unref(src);
894 bdrv_set_backing_hd(src, src_backing, &error_abort);
895 bdrv_unref(src_backing);
896
d861ab3a 897 blk_src = blk_new(qemu_get_aio_context(), BLK_PERM_ALL, BLK_PERM_ALL);
d8b3afd5
KW
898 blk_insert_bs(blk_src, src_overlay, &error_abort);
899
900 switch (drain_node) {
901 case TEST_JOB_DRAIN_SRC:
902 drain_bs = src;
903 break;
904 case TEST_JOB_DRAIN_SRC_CHILD:
905 drain_bs = src_backing;
906 break;
907 case TEST_JOB_DRAIN_SRC_PARENT:
908 drain_bs = src_overlay;
909 break;
910 default:
911 g_assert_not_reached();
912 }
7253220d 913
f62c1729
KW
914 if (use_iothread) {
915 iothread = iothread_new();
916 ctx = iothread_get_aio_context(iothread);
97896a48 917 blk_set_aio_context(blk_src, ctx, &error_abort);
f62c1729
KW
918 } else {
919 ctx = qemu_get_aio_context();
920 }
921
7253220d
KW
922 target = bdrv_new_open_driver(&bdrv_test, "target", BDRV_O_RDWR,
923 &error_abort);
d861ab3a 924 blk_target = blk_new(qemu_get_aio_context(), BLK_PERM_ALL, BLK_PERM_ALL);
7253220d 925 blk_insert_bs(blk_target, target, &error_abort);
132ada80 926 blk_set_allow_aio_context_change(blk_target, true);
7253220d 927
f62c1729 928 aio_context_acquire(ctx);
d49725af
KW
929 tjob = block_job_create("job0", &test_job_driver, NULL, src,
930 0, BLK_PERM_ALL,
931 0, 0, NULL, NULL, &error_abort);
932 job = &tjob->common;
7253220d 933 block_job_add_bdrv(job, "target", target, 0, BLK_PERM_ALL, &error_abort);
d49725af
KW
934
935 switch (result) {
936 case TEST_JOB_SUCCESS:
937 break;
938 case TEST_JOB_FAIL_RUN:
939 tjob->run_ret = -EIO;
940 break;
941 case TEST_JOB_FAIL_PREPARE:
942 tjob->prepare_ret = -EIO;
943 break;
944 }
945
da01ff7f 946 job_start(&job->job);
f62c1729 947 aio_context_release(ctx);
7253220d 948
d8b3afd5
KW
949 if (use_iothread) {
950 /* job_co_entry() is run in the I/O thread, wait for the actual job
951 * code to start (we don't want to catch the job in the pause point in
952 * job_co_entry(). */
953 while (!tjob->running) {
954 aio_poll(qemu_get_aio_context(), false);
955 }
956 }
957
da01ff7f
KW
958 g_assert_cmpint(job->job.pause_count, ==, 0);
959 g_assert_false(job->job.paused);
d8b3afd5 960 g_assert_true(tjob->running);
5599c162 961 g_assert_true(job->job.busy); /* We're in qemu_co_sleep_ns() */
7253220d 962
d8b3afd5 963 do_drain_begin_unlocked(drain_type, drain_bs);
7253220d
KW
964
965 if (drain_type == BDRV_DRAIN_ALL) {
81193349 966 /* bdrv_drain_all() drains both src and target */
da01ff7f 967 g_assert_cmpint(job->job.pause_count, ==, 2);
7253220d 968 } else {
da01ff7f 969 g_assert_cmpint(job->job.pause_count, ==, 1);
7253220d 970 }
89bd0305 971 g_assert_true(job->job.paused);
da01ff7f 972 g_assert_false(job->job.busy); /* The job is paused */
7253220d 973
d8b3afd5 974 do_drain_end_unlocked(drain_type, drain_bs);
f62c1729
KW
975
976 if (use_iothread) {
977 /* paused is reset in the I/O thread, wait for it */
978 while (job->job.paused) {
979 aio_poll(qemu_get_aio_context(), false);
980 }
981 }
7253220d 982
da01ff7f
KW
983 g_assert_cmpint(job->job.pause_count, ==, 0);
984 g_assert_false(job->job.paused);
89bd0305 985 g_assert_true(job->job.busy); /* We're in qemu_co_sleep_ns() */
7253220d 986
132ada80 987 do_drain_begin_unlocked(drain_type, target);
7253220d
KW
988
989 if (drain_type == BDRV_DRAIN_ALL) {
81193349 990 /* bdrv_drain_all() drains both src and target */
da01ff7f 991 g_assert_cmpint(job->job.pause_count, ==, 2);
7253220d 992 } else {
da01ff7f 993 g_assert_cmpint(job->job.pause_count, ==, 1);
7253220d 994 }
89bd0305 995 g_assert_true(job->job.paused);
da01ff7f 996 g_assert_false(job->job.busy); /* The job is paused */
7253220d 997
132ada80 998 do_drain_end_unlocked(drain_type, target);
7253220d 999
f62c1729
KW
1000 if (use_iothread) {
1001 /* paused is reset in the I/O thread, wait for it */
1002 while (job->job.paused) {
1003 aio_poll(qemu_get_aio_context(), false);
1004 }
1005 }
1006
da01ff7f
KW
1007 g_assert_cmpint(job->job.pause_count, ==, 0);
1008 g_assert_false(job->job.paused);
5599c162 1009 g_assert_true(job->job.busy); /* We're in qemu_co_sleep_ns() */
7253220d 1010
f62c1729 1011 aio_context_acquire(ctx);
3d70ff53 1012 ret = job_complete_sync(&job->job, &error_abort);
d49725af 1013 g_assert_cmpint(ret, ==, (result == TEST_JOB_SUCCESS ? 0 : -EIO));
7253220d 1014
f62c1729 1015 if (use_iothread) {
97896a48 1016 blk_set_aio_context(blk_src, qemu_get_aio_context(), &error_abort);
ad943dcb 1017 assert(blk_get_aio_context(blk_target) == qemu_get_aio_context());
f62c1729
KW
1018 }
1019 aio_context_release(ctx);
1020
7253220d
KW
1021 blk_unref(blk_src);
1022 blk_unref(blk_target);
d8b3afd5 1023 bdrv_unref(src_overlay);
7253220d 1024 bdrv_unref(target);
f62c1729
KW
1025
1026 if (iothread) {
1027 iothread_join(iothread);
1028 }
7253220d
KW
1029}
1030
d8b3afd5
KW
1031static void test_blockjob_common(enum drain_type drain_type, bool use_iothread,
1032 enum test_job_result result)
1033{
1034 test_blockjob_common_drain_node(drain_type, use_iothread, result,
1035 TEST_JOB_DRAIN_SRC);
1036 test_blockjob_common_drain_node(drain_type, use_iothread, result,
1037 TEST_JOB_DRAIN_SRC_CHILD);
1038 if (drain_type == BDRV_SUBTREE_DRAIN) {
1039 test_blockjob_common_drain_node(drain_type, use_iothread, result,
1040 TEST_JOB_DRAIN_SRC_PARENT);
1041 }
1042}
1043
7253220d
KW
1044static void test_blockjob_drain_all(void)
1045{
d49725af 1046 test_blockjob_common(BDRV_DRAIN_ALL, false, TEST_JOB_SUCCESS);
7253220d
KW
1047}
1048
1049static void test_blockjob_drain(void)
1050{
d49725af 1051 test_blockjob_common(BDRV_DRAIN, false, TEST_JOB_SUCCESS);
7253220d
KW
1052}
1053
d2a85d0f
KW
1054static void test_blockjob_drain_subtree(void)
1055{
d49725af
KW
1056 test_blockjob_common(BDRV_SUBTREE_DRAIN, false, TEST_JOB_SUCCESS);
1057}
1058
1059static void test_blockjob_error_drain_all(void)
1060{
1061 test_blockjob_common(BDRV_DRAIN_ALL, false, TEST_JOB_FAIL_RUN);
1062 test_blockjob_common(BDRV_DRAIN_ALL, false, TEST_JOB_FAIL_PREPARE);
1063}
1064
1065static void test_blockjob_error_drain(void)
1066{
1067 test_blockjob_common(BDRV_DRAIN, false, TEST_JOB_FAIL_RUN);
1068 test_blockjob_common(BDRV_DRAIN, false, TEST_JOB_FAIL_PREPARE);
1069}
1070
1071static void test_blockjob_error_drain_subtree(void)
1072{
1073 test_blockjob_common(BDRV_SUBTREE_DRAIN, false, TEST_JOB_FAIL_RUN);
1074 test_blockjob_common(BDRV_SUBTREE_DRAIN, false, TEST_JOB_FAIL_PREPARE);
f62c1729
KW
1075}
1076
1077static void test_blockjob_iothread_drain_all(void)
1078{
d49725af 1079 test_blockjob_common(BDRV_DRAIN_ALL, true, TEST_JOB_SUCCESS);
f62c1729
KW
1080}
1081
1082static void test_blockjob_iothread_drain(void)
1083{
d49725af 1084 test_blockjob_common(BDRV_DRAIN, true, TEST_JOB_SUCCESS);
f62c1729
KW
1085}
1086
1087static void test_blockjob_iothread_drain_subtree(void)
1088{
d49725af
KW
1089 test_blockjob_common(BDRV_SUBTREE_DRAIN, true, TEST_JOB_SUCCESS);
1090}
1091
1092static void test_blockjob_iothread_error_drain_all(void)
1093{
1094 test_blockjob_common(BDRV_DRAIN_ALL, true, TEST_JOB_FAIL_RUN);
1095 test_blockjob_common(BDRV_DRAIN_ALL, true, TEST_JOB_FAIL_PREPARE);
1096}
1097
1098static void test_blockjob_iothread_error_drain(void)
1099{
1100 test_blockjob_common(BDRV_DRAIN, true, TEST_JOB_FAIL_RUN);
1101 test_blockjob_common(BDRV_DRAIN, true, TEST_JOB_FAIL_PREPARE);
1102}
1103
1104static void test_blockjob_iothread_error_drain_subtree(void)
1105{
1106 test_blockjob_common(BDRV_SUBTREE_DRAIN, true, TEST_JOB_FAIL_RUN);
1107 test_blockjob_common(BDRV_SUBTREE_DRAIN, true, TEST_JOB_FAIL_PREPARE);
d2a85d0f
KW
1108}
1109
4c8158e3
HR
1110
1111typedef struct BDRVTestTopState {
1112 BdrvChild *wait_child;
1113} BDRVTestTopState;
1114
1115static void bdrv_test_top_close(BlockDriverState *bs)
1116{
1117 BdrvChild *c, *next_c;
1118 QLIST_FOREACH_SAFE(c, &bs->children, next, next_c) {
1119 bdrv_unref_child(bs, c);
1120 }
1121}
1122
1123static int coroutine_fn bdrv_test_top_co_preadv(BlockDriverState *bs,
1124 uint64_t offset, uint64_t bytes,
1125 QEMUIOVector *qiov, int flags)
1126{
1127 BDRVTestTopState *tts = bs->opaque;
1128 return bdrv_co_preadv(tts->wait_child, offset, bytes, qiov, flags);
1129}
1130
1131static BlockDriver bdrv_test_top_driver = {
1132 .format_name = "test_top_driver",
1133 .instance_size = sizeof(BDRVTestTopState),
1134
1135 .bdrv_close = bdrv_test_top_close,
1136 .bdrv_co_preadv = bdrv_test_top_co_preadv,
1137
69dca43d 1138 .bdrv_child_perm = bdrv_default_perms,
4c8158e3
HR
1139};
1140
1141typedef struct TestCoDeleteByDrainData {
1142 BlockBackend *blk;
1143 bool detach_instead_of_delete;
1144 bool done;
1145} TestCoDeleteByDrainData;
1146
1147static void coroutine_fn test_co_delete_by_drain(void *opaque)
1148{
1149 TestCoDeleteByDrainData *dbdd = opaque;
1150 BlockBackend *blk = dbdd->blk;
1151 BlockDriverState *bs = blk_bs(blk);
1152 BDRVTestTopState *tts = bs->opaque;
1153 void *buffer = g_malloc(65536);
405d8fe0 1154 QEMUIOVector qiov = QEMU_IOVEC_INIT_BUF(qiov, buffer, 65536);
4c8158e3
HR
1155
1156 /* Pretend some internal write operation from parent to child.
1157 * Important: We have to read from the child, not from the parent!
1158 * Draining works by first propagating it all up the tree to the
1159 * root and then waiting for drainage from root to the leaves
1160 * (protocol nodes). If we have a request waiting on the root,
1161 * everything will be drained before we go back down the tree, but
1162 * we do not want that. We want to be in the middle of draining
1163 * when this following requests returns. */
1164 bdrv_co_preadv(tts->wait_child, 0, 65536, &qiov, 0);
1165
1166 g_assert_cmpint(bs->refcnt, ==, 1);
1167
1168 if (!dbdd->detach_instead_of_delete) {
1169 blk_unref(blk);
1170 } else {
1171 BdrvChild *c, *next_c;
1172 QLIST_FOREACH_SAFE(c, &bs->children, next, next_c) {
1173 bdrv_unref_child(bs, c);
1174 }
1175 }
1176
1177 dbdd->done = true;
7b43db3c 1178 g_free(buffer);
4c8158e3
HR
1179}
1180
1181/**
1182 * Test what happens when some BDS has some children, you drain one of
1183 * them and this results in the BDS being deleted.
1184 *
1185 * If @detach_instead_of_delete is set, the BDS is not going to be
1186 * deleted but will only detach all of its children.
1187 */
ebd31837
KW
1188static void do_test_delete_by_drain(bool detach_instead_of_delete,
1189 enum drain_type drain_type)
4c8158e3
HR
1190{
1191 BlockBackend *blk;
1192 BlockDriverState *bs, *child_bs, *null_bs;
1193 BDRVTestTopState *tts;
1194 TestCoDeleteByDrainData dbdd;
1195 Coroutine *co;
1196
1197 bs = bdrv_new_open_driver(&bdrv_test_top_driver, "top", BDRV_O_RDWR,
1198 &error_abort);
1199 bs->total_sectors = 65536 >> BDRV_SECTOR_BITS;
1200 tts = bs->opaque;
1201
1202 null_bs = bdrv_open("null-co://", NULL, NULL, BDRV_O_RDWR | BDRV_O_PROTOCOL,
1203 &error_abort);
a16be3cd
HR
1204 bdrv_attach_child(bs, null_bs, "null-child", &child_of_bds,
1205 BDRV_CHILD_DATA, &error_abort);
4c8158e3
HR
1206
1207 /* This child will be the one to pass to requests through to, and
1208 * it will stall until a drain occurs */
1209 child_bs = bdrv_new_open_driver(&bdrv_test, "child", BDRV_O_RDWR,
1210 &error_abort);
1211 child_bs->total_sectors = 65536 >> BDRV_SECTOR_BITS;
1212 /* Takes our reference to child_bs */
a16be3cd
HR
1213 tts->wait_child = bdrv_attach_child(bs, child_bs, "wait-child",
1214 &child_of_bds,
1215 BDRV_CHILD_DATA | BDRV_CHILD_PRIMARY,
1216 &error_abort);
4c8158e3
HR
1217
1218 /* This child is just there to be deleted
1219 * (for detach_instead_of_delete == true) */
1220 null_bs = bdrv_open("null-co://", NULL, NULL, BDRV_O_RDWR | BDRV_O_PROTOCOL,
1221 &error_abort);
a16be3cd
HR
1222 bdrv_attach_child(bs, null_bs, "null-child", &child_of_bds, BDRV_CHILD_DATA,
1223 &error_abort);
4c8158e3 1224
d861ab3a 1225 blk = blk_new(qemu_get_aio_context(), BLK_PERM_ALL, BLK_PERM_ALL);
4c8158e3
HR
1226 blk_insert_bs(blk, bs, &error_abort);
1227
1228 /* Referenced by blk now */
1229 bdrv_unref(bs);
1230
1231 g_assert_cmpint(bs->refcnt, ==, 1);
1232 g_assert_cmpint(child_bs->refcnt, ==, 1);
1233 g_assert_cmpint(null_bs->refcnt, ==, 1);
1234
1235
1236 dbdd = (TestCoDeleteByDrainData){
1237 .blk = blk,
1238 .detach_instead_of_delete = detach_instead_of_delete,
1239 .done = false,
1240 };
1241 co = qemu_coroutine_create(test_co_delete_by_drain, &dbdd);
1242 qemu_coroutine_enter(co);
1243
1244 /* Drain the child while the read operation is still pending.
1245 * This should result in the operation finishing and
1246 * test_co_delete_by_drain() resuming. Thus, @bs will be deleted
1247 * and the coroutine will exit while this drain operation is still
1248 * in progress. */
ebd31837
KW
1249 switch (drain_type) {
1250 case BDRV_DRAIN:
1251 bdrv_ref(child_bs);
1252 bdrv_drain(child_bs);
1253 bdrv_unref(child_bs);
1254 break;
1255 case BDRV_SUBTREE_DRAIN:
1256 /* Would have to ref/unref bs here for !detach_instead_of_delete, but
1257 * then the whole test becomes pointless because the graph changes
1258 * don't occur during the drain any more. */
1259 assert(detach_instead_of_delete);
1260 bdrv_subtree_drained_begin(bs);
1261 bdrv_subtree_drained_end(bs);
1262 break;
19f7a7e5
KW
1263 case BDRV_DRAIN_ALL:
1264 bdrv_drain_all_begin();
1265 bdrv_drain_all_end();
1266 break;
ebd31837
KW
1267 default:
1268 g_assert_not_reached();
1269 }
4c8158e3
HR
1270
1271 while (!dbdd.done) {
1272 aio_poll(qemu_get_aio_context(), true);
1273 }
1274
1275 if (detach_instead_of_delete) {
1276 /* Here, the reference has not passed over to the coroutine,
1277 * so we have to delete the BB ourselves */
1278 blk_unref(blk);
1279 }
1280}
1281
4c8158e3
HR
1282static void test_delete_by_drain(void)
1283{
ebd31837 1284 do_test_delete_by_drain(false, BDRV_DRAIN);
4c8158e3
HR
1285}
1286
19f7a7e5
KW
1287static void test_detach_by_drain_all(void)
1288{
1289 do_test_delete_by_drain(true, BDRV_DRAIN_ALL);
1290}
1291
4c8158e3
HR
1292static void test_detach_by_drain(void)
1293{
ebd31837
KW
1294 do_test_delete_by_drain(true, BDRV_DRAIN);
1295}
1296
1297static void test_detach_by_drain_subtree(void)
1298{
1299 do_test_delete_by_drain(true, BDRV_SUBTREE_DRAIN);
4c8158e3
HR
1300}
1301
1302
231281ab
KW
1303struct detach_by_parent_data {
1304 BlockDriverState *parent_b;
1305 BdrvChild *child_b;
1306 BlockDriverState *c;
1307 BdrvChild *child_c;
57320ca9 1308 bool by_parent_cb;
231281ab 1309};
57320ca9 1310static struct detach_by_parent_data detach_by_parent_data;
231281ab 1311
57320ca9 1312static void detach_indirect_bh(void *opaque)
231281ab
KW
1313{
1314 struct detach_by_parent_data *data = opaque;
1315
231281ab
KW
1316 bdrv_unref_child(data->parent_b, data->child_b);
1317
1318 bdrv_ref(data->c);
1319 data->child_c = bdrv_attach_child(data->parent_b, data->c, "PB-C",
a16be3cd
HR
1320 &child_of_bds, BDRV_CHILD_DATA,
1321 &error_abort);
231281ab
KW
1322}
1323
57320ca9
KW
1324static void detach_by_parent_aio_cb(void *opaque, int ret)
1325{
1326 struct detach_by_parent_data *data = &detach_by_parent_data;
1327
1328 g_assert_cmpint(ret, ==, 0);
1329 if (data->by_parent_cb) {
1330 detach_indirect_bh(data);
1331 }
1332}
1333
1334static void detach_by_driver_cb_drained_begin(BdrvChild *child)
1335{
1336 aio_bh_schedule_oneshot(qemu_get_current_aio_context(),
1337 detach_indirect_bh, &detach_by_parent_data);
a16be3cd 1338 child_of_bds.drained_begin(child);
57320ca9
KW
1339}
1340
bd86fb99 1341static BdrvChildClass detach_by_driver_cb_class;
57320ca9 1342
231281ab
KW
1343/*
1344 * Initial graph:
1345 *
1346 * PA PB
1347 * \ / \
1348 * A B C
1349 *
57320ca9
KW
1350 * by_parent_cb == true: Test that parent callbacks don't poll
1351 *
1352 * PA has a pending write request whose callback changes the child nodes of
1353 * PB: It removes B and adds C instead. The subtree of PB is drained, which
1354 * will indirectly drain the write request, too.
1355 *
1356 * by_parent_cb == false: Test that bdrv_drain_invoke() doesn't poll
1357 *
bd86fb99 1358 * PA's BdrvChildClass has a .drained_begin callback that schedules a BH
57320ca9
KW
1359 * that does the same graph change. If bdrv_drain_invoke() calls it, the
1360 * state is messed up, but if it is only polled in the single
1361 * BDRV_POLL_WHILE() at the end of the drain, this should work fine.
231281ab 1362 */
57320ca9 1363static void test_detach_indirect(bool by_parent_cb)
231281ab
KW
1364{
1365 BlockBackend *blk;
1366 BlockDriverState *parent_a, *parent_b, *a, *b, *c;
1367 BdrvChild *child_a, *child_b;
1368 BlockAIOCB *acb;
231281ab 1369
405d8fe0 1370 QEMUIOVector qiov = QEMU_IOVEC_INIT_BUF(qiov, NULL, 0);
231281ab 1371
57320ca9 1372 if (!by_parent_cb) {
a16be3cd 1373 detach_by_driver_cb_class = child_of_bds;
bd86fb99 1374 detach_by_driver_cb_class.drained_begin =
57320ca9
KW
1375 detach_by_driver_cb_drained_begin;
1376 }
1377
231281ab
KW
1378 /* Create all involved nodes */
1379 parent_a = bdrv_new_open_driver(&bdrv_test, "parent-a", BDRV_O_RDWR,
1380 &error_abort);
1381 parent_b = bdrv_new_open_driver(&bdrv_test, "parent-b", 0,
1382 &error_abort);
1383
1384 a = bdrv_new_open_driver(&bdrv_test, "a", BDRV_O_RDWR, &error_abort);
1385 b = bdrv_new_open_driver(&bdrv_test, "b", BDRV_O_RDWR, &error_abort);
1386 c = bdrv_new_open_driver(&bdrv_test, "c", BDRV_O_RDWR, &error_abort);
1387
1388 /* blk is a BB for parent-a */
d861ab3a 1389 blk = blk_new(qemu_get_aio_context(), BLK_PERM_ALL, BLK_PERM_ALL);
231281ab
KW
1390 blk_insert_bs(blk, parent_a, &error_abort);
1391 bdrv_unref(parent_a);
1392
57320ca9
KW
1393 /* If we want to get bdrv_drain_invoke() to call aio_poll(), the driver
1394 * callback must not return immediately. */
1395 if (!by_parent_cb) {
1396 BDRVTestState *s = parent_a->opaque;
1397 s->sleep_in_drain_begin = true;
1398 }
1399
231281ab
KW
1400 /* Set child relationships */
1401 bdrv_ref(b);
1402 bdrv_ref(a);
a16be3cd
HR
1403 child_b = bdrv_attach_child(parent_b, b, "PB-B", &child_of_bds,
1404 BDRV_CHILD_DATA, &error_abort);
25191e5f
HR
1405 child_a = bdrv_attach_child(parent_b, a, "PB-A", &child_of_bds,
1406 BDRV_CHILD_COW, &error_abort);
231281ab
KW
1407
1408 bdrv_ref(a);
57320ca9 1409 bdrv_attach_child(parent_a, a, "PA-A",
a16be3cd
HR
1410 by_parent_cb ? &child_of_bds : &detach_by_driver_cb_class,
1411 BDRV_CHILD_DATA, &error_abort);
231281ab
KW
1412
1413 g_assert_cmpint(parent_a->refcnt, ==, 1);
1414 g_assert_cmpint(parent_b->refcnt, ==, 1);
1415 g_assert_cmpint(a->refcnt, ==, 3);
1416 g_assert_cmpint(b->refcnt, ==, 2);
1417 g_assert_cmpint(c->refcnt, ==, 1);
1418
1419 g_assert(QLIST_FIRST(&parent_b->children) == child_a);
1420 g_assert(QLIST_NEXT(child_a, next) == child_b);
1421 g_assert(QLIST_NEXT(child_b, next) == NULL);
1422
1423 /* Start the evil write request */
57320ca9 1424 detach_by_parent_data = (struct detach_by_parent_data) {
231281ab
KW
1425 .parent_b = parent_b,
1426 .child_b = child_b,
1427 .c = c,
57320ca9 1428 .by_parent_cb = by_parent_cb,
231281ab 1429 };
57320ca9 1430 acb = blk_aio_preadv(blk, 0, &qiov, 0, detach_by_parent_aio_cb, NULL);
231281ab
KW
1431 g_assert(acb != NULL);
1432
1433 /* Drain and check the expected result */
1434 bdrv_subtree_drained_begin(parent_b);
1435
57320ca9 1436 g_assert(detach_by_parent_data.child_c != NULL);
231281ab
KW
1437
1438 g_assert_cmpint(parent_a->refcnt, ==, 1);
1439 g_assert_cmpint(parent_b->refcnt, ==, 1);
1440 g_assert_cmpint(a->refcnt, ==, 3);
1441 g_assert_cmpint(b->refcnt, ==, 1);
1442 g_assert_cmpint(c->refcnt, ==, 2);
1443
57320ca9
KW
1444 g_assert(QLIST_FIRST(&parent_b->children) == detach_by_parent_data.child_c);
1445 g_assert(QLIST_NEXT(detach_by_parent_data.child_c, next) == child_a);
231281ab
KW
1446 g_assert(QLIST_NEXT(child_a, next) == NULL);
1447
1448 g_assert_cmpint(parent_a->quiesce_counter, ==, 1);
1449 g_assert_cmpint(parent_b->quiesce_counter, ==, 1);
1450 g_assert_cmpint(a->quiesce_counter, ==, 1);
1451 g_assert_cmpint(b->quiesce_counter, ==, 0);
1452 g_assert_cmpint(c->quiesce_counter, ==, 1);
1453
1454 bdrv_subtree_drained_end(parent_b);
1455
1456 bdrv_unref(parent_b);
1457 blk_unref(blk);
1458
231281ab
KW
1459 g_assert_cmpint(a->refcnt, ==, 1);
1460 g_assert_cmpint(b->refcnt, ==, 1);
1461 g_assert_cmpint(c->refcnt, ==, 1);
1462 bdrv_unref(a);
1463 bdrv_unref(b);
1464 bdrv_unref(c);
1465}
1466
57320ca9
KW
1467static void test_detach_by_parent_cb(void)
1468{
1469 test_detach_indirect(true);
1470}
1471
1472static void test_detach_by_driver_cb(void)
1473{
1474 test_detach_indirect(false);
1475}
231281ab 1476
b994c5bc
KW
1477static void test_append_to_drained(void)
1478{
1479 BlockBackend *blk;
1480 BlockDriverState *base, *overlay;
1481 BDRVTestState *base_s, *overlay_s;
1482
d861ab3a 1483 blk = blk_new(qemu_get_aio_context(), BLK_PERM_ALL, BLK_PERM_ALL);
b994c5bc
KW
1484 base = bdrv_new_open_driver(&bdrv_test, "base", BDRV_O_RDWR, &error_abort);
1485 base_s = base->opaque;
1486 blk_insert_bs(blk, base, &error_abort);
1487
1488 overlay = bdrv_new_open_driver(&bdrv_test, "overlay", BDRV_O_RDWR,
1489 &error_abort);
1490 overlay_s = overlay->opaque;
1491
1492 do_drain_begin(BDRV_DRAIN, base);
1493 g_assert_cmpint(base->quiesce_counter, ==, 1);
1494 g_assert_cmpint(base_s->drain_count, ==, 1);
1495 g_assert_cmpint(base->in_flight, ==, 0);
1496
1497 /* Takes ownership of overlay, so we don't have to unref it later */
1498 bdrv_append(overlay, base, &error_abort);
1499 g_assert_cmpint(base->in_flight, ==, 0);
1500 g_assert_cmpint(overlay->in_flight, ==, 0);
1501
1502 g_assert_cmpint(base->quiesce_counter, ==, 1);
1503 g_assert_cmpint(base_s->drain_count, ==, 1);
1504 g_assert_cmpint(overlay->quiesce_counter, ==, 1);
1505 g_assert_cmpint(overlay_s->drain_count, ==, 1);
1506
1507 do_drain_end(BDRV_DRAIN, base);
1508
1509 g_assert_cmpint(base->quiesce_counter, ==, 0);
1510 g_assert_cmpint(base_s->drain_count, ==, 0);
1511 g_assert_cmpint(overlay->quiesce_counter, ==, 0);
1512 g_assert_cmpint(overlay_s->drain_count, ==, 0);
1513
1514 bdrv_unref(base);
1515 blk_unref(blk);
1516}
1517
247d2737
KW
1518static void test_set_aio_context(void)
1519{
1520 BlockDriverState *bs;
1521 IOThread *a = iothread_new();
1522 IOThread *b = iothread_new();
1523 AioContext *ctx_a = iothread_get_aio_context(a);
1524 AioContext *ctx_b = iothread_get_aio_context(b);
1525
1526 bs = bdrv_new_open_driver(&bdrv_test, "test-node", BDRV_O_RDWR,
1527 &error_abort);
1528
1529 bdrv_drained_begin(bs);
26bf15e4 1530 bdrv_try_set_aio_context(bs, ctx_a, &error_abort);
247d2737
KW
1531
1532 aio_context_acquire(ctx_a);
1533 bdrv_drained_end(bs);
1534
1535 bdrv_drained_begin(bs);
26bf15e4 1536 bdrv_try_set_aio_context(bs, ctx_b, &error_abort);
247d2737
KW
1537 aio_context_release(ctx_a);
1538 aio_context_acquire(ctx_b);
26bf15e4 1539 bdrv_try_set_aio_context(bs, qemu_get_aio_context(), &error_abort);
247d2737
KW
1540 aio_context_release(ctx_b);
1541 bdrv_drained_end(bs);
1542
1543 bdrv_unref(bs);
1544 iothread_join(a);
1545 iothread_join(b);
1546}
1547
8e442810
HR
1548
1549typedef struct TestDropBackingBlockJob {
1550 BlockJob common;
1551 bool should_complete;
1552 bool *did_complete;
2afdc790 1553 BlockDriverState *detach_also;
8e442810
HR
1554} TestDropBackingBlockJob;
1555
1556static int coroutine_fn test_drop_backing_job_run(Job *job, Error **errp)
1557{
1558 TestDropBackingBlockJob *s =
1559 container_of(job, TestDropBackingBlockJob, common.job);
1560
1561 while (!s->should_complete) {
1562 job_sleep_ns(job, 0);
1563 }
1564
1565 return 0;
1566}
1567
1568static void test_drop_backing_job_commit(Job *job)
1569{
1570 TestDropBackingBlockJob *s =
1571 container_of(job, TestDropBackingBlockJob, common.job);
1572
1573 bdrv_set_backing_hd(blk_bs(s->common.blk), NULL, &error_abort);
2afdc790 1574 bdrv_set_backing_hd(s->detach_also, NULL, &error_abort);
8e442810
HR
1575
1576 *s->did_complete = true;
1577}
1578
1579static const BlockJobDriver test_drop_backing_job_driver = {
1580 .job_driver = {
1581 .instance_size = sizeof(TestDropBackingBlockJob),
1582 .free = block_job_free,
1583 .user_resume = block_job_user_resume,
8e442810
HR
1584 .run = test_drop_backing_job_run,
1585 .commit = test_drop_backing_job_commit,
1586 }
1587};
1588
1589/**
1590 * Creates a child node with three parent nodes on it, and then runs a
1591 * block job on the final one, parent-node-2.
1592 *
8e442810
HR
1593 * The job is then asked to complete before a section where the child
1594 * is drained.
1595 *
1596 * Ending this section will undrain the child's parents, first
1597 * parent-node-2, then parent-node-1, then parent-node-0 -- the parent
1598 * list is in reverse order of how they were added. Ending the drain
1599 * on parent-node-2 will resume the job, thus completing it and
1600 * scheduling job_exit().
1601 *
1602 * Ending the drain on parent-node-1 will poll the AioContext, which
1603 * lets job_exit() and thus test_drop_backing_job_commit() run. That
2afdc790 1604 * function first removes the child as parent-node-2's backing file.
8e442810
HR
1605 *
1606 * In old (and buggy) implementations, there are two problems with
1607 * that:
1608 * (A) bdrv_drain_invoke() polls for every node that leaves the
1609 * drained section. This means that job_exit() is scheduled
1610 * before the child has left the drained section. Its
1611 * quiesce_counter is therefore still 1 when it is removed from
1612 * parent-node-2.
1613 *
1614 * (B) bdrv_replace_child_noperm() calls drained_end() on the old
1615 * child's parents as many times as the child is quiesced. This
1616 * means it will call drained_end() on parent-node-2 once.
1617 * Because parent-node-2 is no longer quiesced at this point, this
1618 * will fail.
1619 *
1620 * bdrv_replace_child_noperm() therefore must call drained_end() on
1621 * the parent only if it really is still drained because the child is
1622 * drained.
2afdc790
HR
1623 *
1624 * If removing child from parent-node-2 was successful (as it should
1625 * be), test_drop_backing_job_commit() will then also remove the child
1626 * from parent-node-0.
1627 *
1628 * With an old version of our drain infrastructure ((A) above), that
1629 * resulted in the following flow:
1630 *
1631 * 1. child attempts to leave its drained section. The call recurses
1632 * to its parents.
1633 *
1634 * 2. parent-node-2 leaves the drained section. Polling in
1635 * bdrv_drain_invoke() will schedule job_exit().
1636 *
1637 * 3. parent-node-1 leaves the drained section. Polling in
1638 * bdrv_drain_invoke() will run job_exit(), thus disconnecting
1639 * parent-node-0 from the child node.
1640 *
1641 * 4. bdrv_parent_drained_end() uses a QLIST_FOREACH_SAFE() loop to
1642 * iterate over the parents. Thus, it now accesses the BdrvChild
1643 * object that used to connect parent-node-0 and the child node.
1644 * However, that object no longer exists, so it accesses a dangling
1645 * pointer.
1646 *
1647 * The solution is to only poll once when running a bdrv_drained_end()
1648 * operation, specifically at the end when all drained_end()
1649 * operations for all involved nodes have been scheduled.
1650 * Note that this also solves (A) above, thus hiding (B).
8e442810
HR
1651 */
1652static void test_blockjob_commit_by_drained_end(void)
1653{
1654 BlockDriverState *bs_child, *bs_parents[3];
1655 TestDropBackingBlockJob *job;
1656 bool job_has_completed = false;
1657 int i;
1658
1659 bs_child = bdrv_new_open_driver(&bdrv_test, "child-node", BDRV_O_RDWR,
1660 &error_abort);
1661
1662 for (i = 0; i < 3; i++) {
1663 char name[32];
1664 snprintf(name, sizeof(name), "parent-node-%i", i);
1665 bs_parents[i] = bdrv_new_open_driver(&bdrv_test, name, BDRV_O_RDWR,
1666 &error_abort);
1667 bdrv_set_backing_hd(bs_parents[i], bs_child, &error_abort);
1668 }
1669
1670 job = block_job_create("job", &test_drop_backing_job_driver, NULL,
1671 bs_parents[2], 0, BLK_PERM_ALL, 0, 0, NULL, NULL,
1672 &error_abort);
1673
2afdc790 1674 job->detach_also = bs_parents[0];
8e442810
HR
1675 job->did_complete = &job_has_completed;
1676
1677 job_start(&job->common.job);
1678
1679 job->should_complete = true;
1680 bdrv_drained_begin(bs_child);
1681 g_assert(!job_has_completed);
1682 bdrv_drained_end(bs_child);
1683 g_assert(job_has_completed);
1684
1685 bdrv_unref(bs_parents[0]);
1686 bdrv_unref(bs_parents[1]);
1687 bdrv_unref(bs_parents[2]);
1688 bdrv_unref(bs_child);
1689}
1690
9746b35c
HR
1691
1692typedef struct TestSimpleBlockJob {
1693 BlockJob common;
1694 bool should_complete;
1695 bool *did_complete;
1696} TestSimpleBlockJob;
1697
1698static int coroutine_fn test_simple_job_run(Job *job, Error **errp)
1699{
1700 TestSimpleBlockJob *s = container_of(job, TestSimpleBlockJob, common.job);
1701
1702 while (!s->should_complete) {
1703 job_sleep_ns(job, 0);
1704 }
1705
1706 return 0;
1707}
1708
1709static void test_simple_job_clean(Job *job)
1710{
1711 TestSimpleBlockJob *s = container_of(job, TestSimpleBlockJob, common.job);
1712 *s->did_complete = true;
1713}
1714
1715static const BlockJobDriver test_simple_job_driver = {
1716 .job_driver = {
1717 .instance_size = sizeof(TestSimpleBlockJob),
1718 .free = block_job_free,
1719 .user_resume = block_job_user_resume,
9746b35c
HR
1720 .run = test_simple_job_run,
1721 .clean = test_simple_job_clean,
1722 },
1723};
1724
1725static int drop_intermediate_poll_update_filename(BdrvChild *child,
1726 BlockDriverState *new_base,
1727 const char *filename,
1728 Error **errp)
1729{
1730 /*
1731 * We are free to poll here, which may change the block graph, if
1732 * it is not drained.
1733 */
1734
1735 /* If the job is not drained: Complete it, schedule job_exit() */
1736 aio_poll(qemu_get_current_aio_context(), false);
1737 /* If the job is not drained: Run job_exit(), finish the job */
1738 aio_poll(qemu_get_current_aio_context(), false);
1739
1740 return 0;
1741}
1742
1743/**
1744 * Test a poll in the midst of bdrv_drop_intermediate().
1745 *
bd86fb99 1746 * bdrv_drop_intermediate() calls BdrvChildClass.update_filename(),
9746b35c
HR
1747 * which can yield or poll. This may lead to graph changes, unless
1748 * the whole subtree in question is drained.
1749 *
1750 * We test this on the following graph:
1751 *
1752 * Job
1753 *
1754 * |
1755 * job-node
1756 * |
1757 * v
1758 *
1759 * job-node
1760 *
1761 * |
1762 * backing
1763 * |
1764 * v
1765 *
1766 * node-2 --chain--> node-1 --chain--> node-0
1767 *
1768 * We drop node-1 with bdrv_drop_intermediate(top=node-1, base=node-0).
1769 *
1770 * This first updates node-2's backing filename by invoking
1771 * drop_intermediate_poll_update_filename(), which polls twice. This
1772 * causes the job to finish, which in turns causes the job-node to be
1773 * deleted.
1774 *
1775 * bdrv_drop_intermediate() uses a QLIST_FOREACH_SAFE() loop, so it
1776 * already has a pointer to the BdrvChild edge between job-node and
1777 * node-1. When it tries to handle that edge, we probably get a
1778 * segmentation fault because the object no longer exists.
1779 *
1780 *
1781 * The solution is for bdrv_drop_intermediate() to drain top's
1782 * subtree. This prevents graph changes from happening just because
bd86fb99 1783 * BdrvChildClass.update_filename() yields or polls. Thus, the block
9746b35c
HR
1784 * job is paused during that drained section and must finish before or
1785 * after.
1786 *
1787 * (In addition, bdrv_replace_child() must keep the job paused.)
1788 */
1789static void test_drop_intermediate_poll(void)
1790{
bd86fb99 1791 static BdrvChildClass chain_child_class;
9746b35c
HR
1792 BlockDriverState *chain[3];
1793 TestSimpleBlockJob *job;
1794 BlockDriverState *job_node;
1795 bool job_has_completed = false;
1796 int i;
1797 int ret;
1798
25191e5f 1799 chain_child_class = child_of_bds;
bd86fb99 1800 chain_child_class.update_filename = drop_intermediate_poll_update_filename;
9746b35c
HR
1801
1802 for (i = 0; i < 3; i++) {
1803 char name[32];
1804 snprintf(name, 32, "node-%i", i);
1805
1806 chain[i] = bdrv_new_open_driver(&bdrv_test, name, 0, &error_abort);
1807 }
1808
1809 job_node = bdrv_new_open_driver(&bdrv_test, "job-node", BDRV_O_RDWR,
1810 &error_abort);
1811 bdrv_set_backing_hd(job_node, chain[1], &error_abort);
1812
1813 /*
1814 * Establish the chain last, so the chain links are the first
1815 * elements in the BDS.parents lists
1816 */
1817 for (i = 0; i < 3; i++) {
1818 if (i) {
1819 /* Takes the reference to chain[i - 1] */
1820 chain[i]->backing = bdrv_attach_child(chain[i], chain[i - 1],
bd86fb99 1821 "chain", &chain_child_class,
25191e5f 1822 BDRV_CHILD_COW, &error_abort);
9746b35c
HR
1823 }
1824 }
1825
1826 job = block_job_create("job", &test_simple_job_driver, NULL, job_node,
1827 0, BLK_PERM_ALL, 0, 0, NULL, NULL, &error_abort);
1828
1829 /* The job has a reference now */
1830 bdrv_unref(job_node);
1831
1832 job->did_complete = &job_has_completed;
1833
1834 job_start(&job->common.job);
1835 job->should_complete = true;
1836
1837 g_assert(!job_has_completed);
1838 ret = bdrv_drop_intermediate(chain[1], chain[0], NULL);
1839 g_assert(ret == 0);
1840 g_assert(job_has_completed);
1841
1842 bdrv_unref(chain[2]);
1843}
1844
0513f984
HR
1845
1846typedef struct BDRVReplaceTestState {
1847 bool was_drained;
1848 bool was_undrained;
1849 bool has_read;
1850
1851 int drain_count;
1852
1853 bool yield_before_read;
1854 Coroutine *io_co;
1855 Coroutine *drain_co;
1856} BDRVReplaceTestState;
1857
1858static void bdrv_replace_test_close(BlockDriverState *bs)
1859{
1860}
1861
1862/**
1863 * If @bs has a backing file:
1864 * Yield if .yield_before_read is true (and wait for drain_begin to
1865 * wake us up).
1866 * Forward the read to bs->backing. Set .has_read to true.
1867 * If drain_begin has woken us, wake it in turn.
1868 *
1869 * Otherwise:
1870 * Set .has_read to true and return success.
1871 */
1872static int coroutine_fn bdrv_replace_test_co_preadv(BlockDriverState *bs,
1873 uint64_t offset,
1874 uint64_t bytes,
1875 QEMUIOVector *qiov,
1876 int flags)
1877{
1878 BDRVReplaceTestState *s = bs->opaque;
1879
1880 if (bs->backing) {
1881 int ret;
1882
1883 g_assert(!s->drain_count);
1884
1885 s->io_co = qemu_coroutine_self();
1886 if (s->yield_before_read) {
1887 s->yield_before_read = false;
1888 qemu_coroutine_yield();
1889 }
1890 s->io_co = NULL;
1891
1892 ret = bdrv_preadv(bs->backing, offset, qiov);
1893 s->has_read = true;
1894
1895 /* Wake up drain_co if it runs */
1896 if (s->drain_co) {
1897 aio_co_wake(s->drain_co);
1898 }
1899
1900 return ret;
1901 }
1902
1903 s->has_read = true;
1904 return 0;
1905}
1906
1907/**
1908 * If .drain_count is 0, wake up .io_co if there is one; and set
1909 * .was_drained.
1910 * Increment .drain_count.
1911 */
1912static void coroutine_fn bdrv_replace_test_co_drain_begin(BlockDriverState *bs)
1913{
1914 BDRVReplaceTestState *s = bs->opaque;
1915
1916 if (!s->drain_count) {
1917 /* Keep waking io_co up until it is done */
1918 s->drain_co = qemu_coroutine_self();
1919 while (s->io_co) {
1920 aio_co_wake(s->io_co);
1921 s->io_co = NULL;
1922 qemu_coroutine_yield();
1923 }
1924 s->drain_co = NULL;
1925
1926 s->was_drained = true;
1927 }
1928 s->drain_count++;
1929}
1930
1931/**
1932 * Reduce .drain_count, set .was_undrained once it reaches 0.
1933 * If .drain_count reaches 0 and the node has a backing file, issue a
1934 * read request.
1935 */
1936static void coroutine_fn bdrv_replace_test_co_drain_end(BlockDriverState *bs)
1937{
1938 BDRVReplaceTestState *s = bs->opaque;
1939
1940 g_assert(s->drain_count > 0);
1941 if (!--s->drain_count) {
1942 int ret;
1943
1944 s->was_undrained = true;
1945
1946 if (bs->backing) {
1947 char data;
1948 QEMUIOVector qiov = QEMU_IOVEC_INIT_BUF(qiov, &data, 1);
1949
1950 /* Queue a read request post-drain */
1951 ret = bdrv_replace_test_co_preadv(bs, 0, 1, &qiov, 0);
1952 g_assert(ret >= 0);
1953 }
1954 }
1955}
1956
1957static BlockDriver bdrv_replace_test = {
1958 .format_name = "replace_test",
1959 .instance_size = sizeof(BDRVReplaceTestState),
1960
1961 .bdrv_close = bdrv_replace_test_close,
1962 .bdrv_co_preadv = bdrv_replace_test_co_preadv,
1963
1964 .bdrv_co_drain_begin = bdrv_replace_test_co_drain_begin,
1965 .bdrv_co_drain_end = bdrv_replace_test_co_drain_end,
1966
69dca43d 1967 .bdrv_child_perm = bdrv_default_perms,
0513f984
HR
1968};
1969
1970static void coroutine_fn test_replace_child_mid_drain_read_co(void *opaque)
1971{
1972 int ret;
1973 char data;
1974
1975 ret = blk_co_pread(opaque, 0, 1, &data, 0);
1976 g_assert(ret >= 0);
1977}
1978
1979/**
1980 * We test two things:
1981 * (1) bdrv_replace_child_noperm() must not undrain the parent if both
1982 * children are drained.
1983 * (2) bdrv_replace_child_noperm() must never flush I/O requests to a
1984 * drained child. If the old child is drained, it must flush I/O
1985 * requests after the new one has been attached. If the new child
1986 * is drained, it must flush I/O requests before the old one is
1987 * detached.
1988 *
1989 * To do so, we create one parent node and two child nodes; then
1990 * attach one of the children (old_child_bs) to the parent, then
1991 * drain both old_child_bs and new_child_bs according to
1992 * old_drain_count and new_drain_count, respectively, and finally
1993 * we invoke bdrv_replace_node() to replace old_child_bs by
1994 * new_child_bs.
1995 *
1996 * The test block driver we use here (bdrv_replace_test) has a read
1997 * function that:
1998 * - For the parent node, can optionally yield, and then forwards the
1999 * read to bdrv_preadv(),
2000 * - For the child node, just returns immediately.
2001 *
2002 * If the read yields, the drain_begin function will wake it up.
2003 *
2004 * The drain_end function issues a read on the parent once it is fully
2005 * undrained (which simulates requests starting to come in again).
2006 */
2007static void do_test_replace_child_mid_drain(int old_drain_count,
2008 int new_drain_count)
2009{
2010 BlockBackend *parent_blk;
2011 BlockDriverState *parent_bs;
2012 BlockDriverState *old_child_bs, *new_child_bs;
2013 BDRVReplaceTestState *parent_s;
2014 BDRVReplaceTestState *old_child_s, *new_child_s;
2015 Coroutine *io_co;
2016 int i;
2017
2018 parent_bs = bdrv_new_open_driver(&bdrv_replace_test, "parent", 0,
2019 &error_abort);
2020 parent_s = parent_bs->opaque;
2021
2022 parent_blk = blk_new(qemu_get_aio_context(),
2023 BLK_PERM_CONSISTENT_READ, BLK_PERM_ALL);
2024 blk_insert_bs(parent_blk, parent_bs, &error_abort);
2025
2026 old_child_bs = bdrv_new_open_driver(&bdrv_replace_test, "old-child", 0,
2027 &error_abort);
2028 new_child_bs = bdrv_new_open_driver(&bdrv_replace_test, "new-child", 0,
2029 &error_abort);
2030 old_child_s = old_child_bs->opaque;
2031 new_child_s = new_child_bs->opaque;
2032
2033 /* So that we can read something */
2034 parent_bs->total_sectors = 1;
2035 old_child_bs->total_sectors = 1;
2036 new_child_bs->total_sectors = 1;
2037
2038 bdrv_ref(old_child_bs);
2039 parent_bs->backing = bdrv_attach_child(parent_bs, old_child_bs, "child",
25191e5f
HR
2040 &child_of_bds, BDRV_CHILD_COW,
2041 &error_abort);
0513f984
HR
2042
2043 for (i = 0; i < old_drain_count; i++) {
2044 bdrv_drained_begin(old_child_bs);
2045 }
2046 for (i = 0; i < new_drain_count; i++) {
2047 bdrv_drained_begin(new_child_bs);
2048 }
2049
2050 if (!old_drain_count) {
2051 /*
2052 * Start a read operation that will yield, so it will not
2053 * complete before the node is drained.
2054 */
2055 parent_s->yield_before_read = true;
2056 io_co = qemu_coroutine_create(test_replace_child_mid_drain_read_co,
2057 parent_blk);
2058 qemu_coroutine_enter(io_co);
2059 }
2060
2061 /* If we have started a read operation, it should have yielded */
2062 g_assert(!parent_s->has_read);
2063
2064 /* Reset drained status so we can see what bdrv_replace_node() does */
2065 parent_s->was_drained = false;
2066 parent_s->was_undrained = false;
2067
2068 g_assert(parent_bs->quiesce_counter == old_drain_count);
2069 bdrv_replace_node(old_child_bs, new_child_bs, &error_abort);
2070 g_assert(parent_bs->quiesce_counter == new_drain_count);
2071
2072 if (!old_drain_count && !new_drain_count) {
2073 /*
2074 * From undrained to undrained drains and undrains the parent,
2075 * because bdrv_replace_node() contains a drained section for
2076 * @old_child_bs.
2077 */
2078 g_assert(parent_s->was_drained && parent_s->was_undrained);
2079 } else if (!old_drain_count && new_drain_count) {
2080 /*
2081 * From undrained to drained should drain the parent and keep
2082 * it that way.
2083 */
2084 g_assert(parent_s->was_drained && !parent_s->was_undrained);
2085 } else if (old_drain_count && !new_drain_count) {
2086 /*
2087 * From drained to undrained should undrain the parent and
2088 * keep it that way.
2089 */
2090 g_assert(!parent_s->was_drained && parent_s->was_undrained);
2091 } else /* if (old_drain_count && new_drain_count) */ {
2092 /*
2093 * From drained to drained must not undrain the parent at any
2094 * point
2095 */
2096 g_assert(!parent_s->was_drained && !parent_s->was_undrained);
2097 }
2098
2099 if (!old_drain_count || !new_drain_count) {
2100 /*
2101 * If !old_drain_count, we have started a read request before
2102 * bdrv_replace_node(). If !new_drain_count, the parent must
2103 * have been undrained at some point, and
2104 * bdrv_replace_test_co_drain_end() starts a read request
2105 * then.
2106 */
2107 g_assert(parent_s->has_read);
2108 } else {
2109 /*
2110 * If the parent was never undrained, there is no way to start
2111 * a read request.
2112 */
2113 g_assert(!parent_s->has_read);
2114 }
2115
2116 /* A drained child must have not received any request */
2117 g_assert(!(old_drain_count && old_child_s->has_read));
2118 g_assert(!(new_drain_count && new_child_s->has_read));
2119
2120 for (i = 0; i < new_drain_count; i++) {
2121 bdrv_drained_end(new_child_bs);
2122 }
2123 for (i = 0; i < old_drain_count; i++) {
2124 bdrv_drained_end(old_child_bs);
2125 }
2126
2127 /*
2128 * By now, bdrv_replace_test_co_drain_end() must have been called
2129 * at some point while the new child was attached to the parent.
2130 */
2131 g_assert(parent_s->has_read);
2132 g_assert(new_child_s->has_read);
2133
2134 blk_unref(parent_blk);
2135 bdrv_unref(parent_bs);
2136 bdrv_unref(old_child_bs);
2137 bdrv_unref(new_child_bs);
2138}
2139
2140static void test_replace_child_mid_drain(void)
2141{
2142 int old_drain_count, new_drain_count;
2143
2144 for (old_drain_count = 0; old_drain_count < 2; old_drain_count++) {
2145 for (new_drain_count = 0; new_drain_count < 2; new_drain_count++) {
2146 do_test_replace_child_mid_drain(old_drain_count, new_drain_count);
2147 }
2148 }
2149}
2150
881cfd17
KW
2151int main(int argc, char **argv)
2152{
bb675689
KW
2153 int ret;
2154
881cfd17
KW
2155 bdrv_init();
2156 qemu_init_main_loop(&error_abort);
2157
2158 g_test_init(&argc, &argv, NULL);
bb675689 2159 qemu_event_init(&done_event, false);
881cfd17
KW
2160
2161 g_test_add_func("/bdrv-drain/driver-cb/drain_all", test_drv_cb_drain_all);
86e1c840 2162 g_test_add_func("/bdrv-drain/driver-cb/drain", test_drv_cb_drain);
d2a85d0f
KW
2163 g_test_add_func("/bdrv-drain/driver-cb/drain_subtree",
2164 test_drv_cb_drain_subtree);
881cfd17 2165
6d0252f2
KW
2166 g_test_add_func("/bdrv-drain/driver-cb/co/drain_all",
2167 test_drv_cb_co_drain_all);
0582eb10
KW
2168 g_test_add_func("/bdrv-drain/driver-cb/co/drain", test_drv_cb_co_drain);
2169 g_test_add_func("/bdrv-drain/driver-cb/co/drain_subtree",
2170 test_drv_cb_co_drain_subtree);
2171
2172
89a6ceab
KW
2173 g_test_add_func("/bdrv-drain/quiesce/drain_all", test_quiesce_drain_all);
2174 g_test_add_func("/bdrv-drain/quiesce/drain", test_quiesce_drain);
d2a85d0f
KW
2175 g_test_add_func("/bdrv-drain/quiesce/drain_subtree",
2176 test_quiesce_drain_subtree);
89a6ceab 2177
6d0252f2
KW
2178 g_test_add_func("/bdrv-drain/quiesce/co/drain_all",
2179 test_quiesce_co_drain_all);
0582eb10
KW
2180 g_test_add_func("/bdrv-drain/quiesce/co/drain", test_quiesce_co_drain);
2181 g_test_add_func("/bdrv-drain/quiesce/co/drain_subtree",
2182 test_quiesce_co_drain_subtree);
2183
6c429a6a 2184 g_test_add_func("/bdrv-drain/nested", test_nested);
27e64474 2185 g_test_add_func("/bdrv-drain/multiparent", test_multiparent);
19f7a7e5
KW
2186
2187 g_test_add_func("/bdrv-drain/graph-change/drain_subtree",
2188 test_graph_change_drain_subtree);
2189 g_test_add_func("/bdrv-drain/graph-change/drain_all",
2190 test_graph_change_drain_all);
6c429a6a 2191
bb675689
KW
2192 g_test_add_func("/bdrv-drain/iothread/drain_all", test_iothread_drain_all);
2193 g_test_add_func("/bdrv-drain/iothread/drain", test_iothread_drain);
2194 g_test_add_func("/bdrv-drain/iothread/drain_subtree",
2195 test_iothread_drain_subtree);
2196
7253220d
KW
2197 g_test_add_func("/bdrv-drain/blockjob/drain_all", test_blockjob_drain_all);
2198 g_test_add_func("/bdrv-drain/blockjob/drain", test_blockjob_drain);
d2a85d0f
KW
2199 g_test_add_func("/bdrv-drain/blockjob/drain_subtree",
2200 test_blockjob_drain_subtree);
7253220d 2201
d49725af
KW
2202 g_test_add_func("/bdrv-drain/blockjob/error/drain_all",
2203 test_blockjob_error_drain_all);
2204 g_test_add_func("/bdrv-drain/blockjob/error/drain",
2205 test_blockjob_error_drain);
2206 g_test_add_func("/bdrv-drain/blockjob/error/drain_subtree",
2207 test_blockjob_error_drain_subtree);
2208
f62c1729
KW
2209 g_test_add_func("/bdrv-drain/blockjob/iothread/drain_all",
2210 test_blockjob_iothread_drain_all);
2211 g_test_add_func("/bdrv-drain/blockjob/iothread/drain",
2212 test_blockjob_iothread_drain);
2213 g_test_add_func("/bdrv-drain/blockjob/iothread/drain_subtree",
2214 test_blockjob_iothread_drain_subtree);
2215
d49725af
KW
2216 g_test_add_func("/bdrv-drain/blockjob/iothread/error/drain_all",
2217 test_blockjob_iothread_error_drain_all);
2218 g_test_add_func("/bdrv-drain/blockjob/iothread/error/drain",
2219 test_blockjob_iothread_error_drain);
2220 g_test_add_func("/bdrv-drain/blockjob/iothread/error/drain_subtree",
2221 test_blockjob_iothread_error_drain_subtree);
2222
ebd31837 2223 g_test_add_func("/bdrv-drain/deletion/drain", test_delete_by_drain);
19f7a7e5 2224 g_test_add_func("/bdrv-drain/detach/drain_all", test_detach_by_drain_all);
ebd31837
KW
2225 g_test_add_func("/bdrv-drain/detach/drain", test_detach_by_drain);
2226 g_test_add_func("/bdrv-drain/detach/drain_subtree", test_detach_by_drain_subtree);
231281ab 2227 g_test_add_func("/bdrv-drain/detach/parent_cb", test_detach_by_parent_cb);
57320ca9 2228 g_test_add_func("/bdrv-drain/detach/driver_cb", test_detach_by_driver_cb);
4c8158e3 2229
b994c5bc
KW
2230 g_test_add_func("/bdrv-drain/attach/drain", test_append_to_drained);
2231
247d2737
KW
2232 g_test_add_func("/bdrv-drain/set_aio_context", test_set_aio_context);
2233
8e442810
HR
2234 g_test_add_func("/bdrv-drain/blockjob/commit_by_drained_end",
2235 test_blockjob_commit_by_drained_end);
2236
9746b35c
HR
2237 g_test_add_func("/bdrv-drain/bdrv_drop_intermediate/poll",
2238 test_drop_intermediate_poll);
2239
0513f984
HR
2240 g_test_add_func("/bdrv-drain/replace_child/mid-drain",
2241 test_replace_child_mid_drain);
2242
bb675689
KW
2243 ret = g_test_run();
2244 qemu_event_destroy(&done_event);
2245 return ret;
881cfd17 2246}