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