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