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