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