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