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