]> Git Repo - qemu.git/blob - tests/test-bdrv-drain.c
test-bdrv-drain: Graph change through parent callback
[qemu.git] / tests / test-bdrv-drain.c
1 /*
2  * Block node draining tests
3  *
4  * Copyright (c) 2017 Kevin Wolf <[email protected]>
5  *
6  * Permission is hereby granted, free of charge, to any person obtaining a copy
7  * of this software and associated documentation files (the "Software"), to deal
8  * in the Software without restriction, including without limitation the rights
9  * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
10  * copies of the Software, and to permit persons to whom the Software is
11  * furnished to do so, subject to the following conditions:
12  *
13  * The above copyright notice and this permission notice shall be included in
14  * all copies or substantial portions of the Software.
15  *
16  * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
17  * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
18  * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
19  * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
20  * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
21  * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
22  * THE SOFTWARE.
23  */
24
25 #include "qemu/osdep.h"
26 #include "block/block.h"
27 #include "block/blockjob_int.h"
28 #include "sysemu/block-backend.h"
29 #include "qapi/error.h"
30 #include "iothread.h"
31
32 static QemuEvent done_event;
33
34 typedef struct BDRVTestState {
35     int drain_count;
36     AioContext *bh_indirection_ctx;
37 } BDRVTestState;
38
39 static void coroutine_fn bdrv_test_co_drain_begin(BlockDriverState *bs)
40 {
41     BDRVTestState *s = bs->opaque;
42     s->drain_count++;
43 }
44
45 static void coroutine_fn bdrv_test_co_drain_end(BlockDriverState *bs)
46 {
47     BDRVTestState *s = bs->opaque;
48     s->drain_count--;
49 }
50
51 static void bdrv_test_close(BlockDriverState *bs)
52 {
53     BDRVTestState *s = bs->opaque;
54     g_assert_cmpint(s->drain_count, >, 0);
55 }
56
57 static void co_reenter_bh(void *opaque)
58 {
59     aio_co_wake(opaque);
60 }
61
62 static int coroutine_fn bdrv_test_co_preadv(BlockDriverState *bs,
63                                             uint64_t offset, uint64_t bytes,
64                                             QEMUIOVector *qiov, int flags)
65 {
66     BDRVTestState *s = bs->opaque;
67
68     /* We want this request to stay until the polling loop in drain waits for
69      * it to complete. We need to sleep a while as bdrv_drain_invoke() comes
70      * first and polls its result, too, but it shouldn't accidentally complete
71      * this request yet. */
72     qemu_co_sleep_ns(QEMU_CLOCK_REALTIME, 100000);
73
74     if (s->bh_indirection_ctx) {
75         aio_bh_schedule_oneshot(s->bh_indirection_ctx, co_reenter_bh,
76                                 qemu_coroutine_self());
77         qemu_coroutine_yield();
78     }
79
80     return 0;
81 }
82
83 static BlockDriver bdrv_test = {
84     .format_name            = "test",
85     .instance_size          = sizeof(BDRVTestState),
86
87     .bdrv_close             = bdrv_test_close,
88     .bdrv_co_preadv         = bdrv_test_co_preadv,
89
90     .bdrv_co_drain_begin    = bdrv_test_co_drain_begin,
91     .bdrv_co_drain_end      = bdrv_test_co_drain_end,
92
93     .bdrv_child_perm        = bdrv_format_default_perms,
94 };
95
96 static void aio_ret_cb(void *opaque, int ret)
97 {
98     int *aio_ret = opaque;
99     *aio_ret = ret;
100 }
101
102 typedef struct CallInCoroutineData {
103     void (*entry)(void);
104     bool done;
105 } CallInCoroutineData;
106
107 static coroutine_fn void call_in_coroutine_entry(void *opaque)
108 {
109     CallInCoroutineData *data = opaque;
110
111     data->entry();
112     data->done = true;
113 }
114
115 static void call_in_coroutine(void (*entry)(void))
116 {
117     Coroutine *co;
118     CallInCoroutineData data = {
119         .entry  = entry,
120         .done   = false,
121     };
122
123     co = qemu_coroutine_create(call_in_coroutine_entry, &data);
124     qemu_coroutine_enter(co);
125     while (!data.done) {
126         aio_poll(qemu_get_aio_context(), true);
127     }
128 }
129
130 enum drain_type {
131     BDRV_DRAIN_ALL,
132     BDRV_DRAIN,
133     BDRV_SUBTREE_DRAIN,
134     DRAIN_TYPE_MAX,
135 };
136
137 static void do_drain_begin(enum drain_type drain_type, BlockDriverState *bs)
138 {
139     switch (drain_type) {
140     case BDRV_DRAIN_ALL:        bdrv_drain_all_begin(); break;
141     case BDRV_DRAIN:            bdrv_drained_begin(bs); break;
142     case BDRV_SUBTREE_DRAIN:    bdrv_subtree_drained_begin(bs); break;
143     default:                    g_assert_not_reached();
144     }
145 }
146
147 static void do_drain_end(enum drain_type drain_type, BlockDriverState *bs)
148 {
149     switch (drain_type) {
150     case BDRV_DRAIN_ALL:        bdrv_drain_all_end(); break;
151     case BDRV_DRAIN:            bdrv_drained_end(bs); break;
152     case BDRV_SUBTREE_DRAIN:    bdrv_subtree_drained_end(bs); break;
153     default:                    g_assert_not_reached();
154     }
155 }
156
157 static void test_drv_cb_common(enum drain_type drain_type, bool recursive)
158 {
159     BlockBackend *blk;
160     BlockDriverState *bs, *backing;
161     BDRVTestState *s, *backing_s;
162     BlockAIOCB *acb;
163     int aio_ret;
164
165     QEMUIOVector qiov;
166     struct iovec iov = {
167         .iov_base = NULL,
168         .iov_len = 0,
169     };
170     qemu_iovec_init_external(&qiov, &iov, 1);
171
172     blk = blk_new(BLK_PERM_ALL, BLK_PERM_ALL);
173     bs = bdrv_new_open_driver(&bdrv_test, "test-node", BDRV_O_RDWR,
174                               &error_abort);
175     s = bs->opaque;
176     blk_insert_bs(blk, bs, &error_abort);
177
178     backing = bdrv_new_open_driver(&bdrv_test, "backing", 0, &error_abort);
179     backing_s = backing->opaque;
180     bdrv_set_backing_hd(bs, backing, &error_abort);
181
182     /* Simple bdrv_drain_all_begin/end pair, check that CBs are called */
183     g_assert_cmpint(s->drain_count, ==, 0);
184     g_assert_cmpint(backing_s->drain_count, ==, 0);
185
186     do_drain_begin(drain_type, bs);
187
188     g_assert_cmpint(s->drain_count, ==, 1);
189     g_assert_cmpint(backing_s->drain_count, ==, !!recursive);
190
191     do_drain_end(drain_type, bs);
192
193     g_assert_cmpint(s->drain_count, ==, 0);
194     g_assert_cmpint(backing_s->drain_count, ==, 0);
195
196     /* Now do the same while a request is pending */
197     aio_ret = -EINPROGRESS;
198     acb = blk_aio_preadv(blk, 0, &qiov, 0, aio_ret_cb, &aio_ret);
199     g_assert(acb != NULL);
200     g_assert_cmpint(aio_ret, ==, -EINPROGRESS);
201
202     g_assert_cmpint(s->drain_count, ==, 0);
203     g_assert_cmpint(backing_s->drain_count, ==, 0);
204
205     do_drain_begin(drain_type, bs);
206
207     g_assert_cmpint(aio_ret, ==, 0);
208     g_assert_cmpint(s->drain_count, ==, 1);
209     g_assert_cmpint(backing_s->drain_count, ==, !!recursive);
210
211     do_drain_end(drain_type, bs);
212
213     g_assert_cmpint(s->drain_count, ==, 0);
214     g_assert_cmpint(backing_s->drain_count, ==, 0);
215
216     bdrv_unref(backing);
217     bdrv_unref(bs);
218     blk_unref(blk);
219 }
220
221 static void test_drv_cb_drain_all(void)
222 {
223     test_drv_cb_common(BDRV_DRAIN_ALL, true);
224 }
225
226 static void test_drv_cb_drain(void)
227 {
228     test_drv_cb_common(BDRV_DRAIN, false);
229 }
230
231 static void test_drv_cb_drain_subtree(void)
232 {
233     test_drv_cb_common(BDRV_SUBTREE_DRAIN, true);
234 }
235
236 static void test_drv_cb_co_drain_all(void)
237 {
238     call_in_coroutine(test_drv_cb_drain_all);
239 }
240
241 static void test_drv_cb_co_drain(void)
242 {
243     call_in_coroutine(test_drv_cb_drain);
244 }
245
246 static void test_drv_cb_co_drain_subtree(void)
247 {
248     call_in_coroutine(test_drv_cb_drain_subtree);
249 }
250
251 static void test_quiesce_common(enum drain_type drain_type, bool recursive)
252 {
253     BlockBackend *blk;
254     BlockDriverState *bs, *backing;
255
256     blk = blk_new(BLK_PERM_ALL, BLK_PERM_ALL);
257     bs = bdrv_new_open_driver(&bdrv_test, "test-node", BDRV_O_RDWR,
258                               &error_abort);
259     blk_insert_bs(blk, bs, &error_abort);
260
261     backing = bdrv_new_open_driver(&bdrv_test, "backing", 0, &error_abort);
262     bdrv_set_backing_hd(bs, backing, &error_abort);
263
264     g_assert_cmpint(bs->quiesce_counter, ==, 0);
265     g_assert_cmpint(backing->quiesce_counter, ==, 0);
266
267     do_drain_begin(drain_type, bs);
268
269     g_assert_cmpint(bs->quiesce_counter, ==, 1);
270     g_assert_cmpint(backing->quiesce_counter, ==, !!recursive);
271
272     do_drain_end(drain_type, bs);
273
274     g_assert_cmpint(bs->quiesce_counter, ==, 0);
275     g_assert_cmpint(backing->quiesce_counter, ==, 0);
276
277     bdrv_unref(backing);
278     bdrv_unref(bs);
279     blk_unref(blk);
280 }
281
282 static void test_quiesce_drain_all(void)
283 {
284     test_quiesce_common(BDRV_DRAIN_ALL, true);
285 }
286
287 static void test_quiesce_drain(void)
288 {
289     test_quiesce_common(BDRV_DRAIN, false);
290 }
291
292 static void test_quiesce_drain_subtree(void)
293 {
294     test_quiesce_common(BDRV_SUBTREE_DRAIN, true);
295 }
296
297 static void test_quiesce_co_drain_all(void)
298 {
299     call_in_coroutine(test_quiesce_drain_all);
300 }
301
302 static void test_quiesce_co_drain(void)
303 {
304     call_in_coroutine(test_quiesce_drain);
305 }
306
307 static void test_quiesce_co_drain_subtree(void)
308 {
309     call_in_coroutine(test_quiesce_drain_subtree);
310 }
311
312 static void test_nested(void)
313 {
314     BlockBackend *blk;
315     BlockDriverState *bs, *backing;
316     BDRVTestState *s, *backing_s;
317     enum drain_type outer, inner;
318
319     blk = blk_new(BLK_PERM_ALL, BLK_PERM_ALL);
320     bs = bdrv_new_open_driver(&bdrv_test, "test-node", BDRV_O_RDWR,
321                               &error_abort);
322     s = bs->opaque;
323     blk_insert_bs(blk, bs, &error_abort);
324
325     backing = bdrv_new_open_driver(&bdrv_test, "backing", 0, &error_abort);
326     backing_s = backing->opaque;
327     bdrv_set_backing_hd(bs, backing, &error_abort);
328
329     for (outer = 0; outer < DRAIN_TYPE_MAX; outer++) {
330         for (inner = 0; inner < DRAIN_TYPE_MAX; inner++) {
331             int backing_quiesce = (outer != BDRV_DRAIN) +
332                                   (inner != BDRV_DRAIN);
333
334             g_assert_cmpint(bs->quiesce_counter, ==, 0);
335             g_assert_cmpint(backing->quiesce_counter, ==, 0);
336             g_assert_cmpint(s->drain_count, ==, 0);
337             g_assert_cmpint(backing_s->drain_count, ==, 0);
338
339             do_drain_begin(outer, bs);
340             do_drain_begin(inner, bs);
341
342             g_assert_cmpint(bs->quiesce_counter, ==, 2);
343             g_assert_cmpint(backing->quiesce_counter, ==, backing_quiesce);
344             g_assert_cmpint(s->drain_count, ==, 2);
345             g_assert_cmpint(backing_s->drain_count, ==, backing_quiesce);
346
347             do_drain_end(inner, bs);
348             do_drain_end(outer, bs);
349
350             g_assert_cmpint(bs->quiesce_counter, ==, 0);
351             g_assert_cmpint(backing->quiesce_counter, ==, 0);
352             g_assert_cmpint(s->drain_count, ==, 0);
353             g_assert_cmpint(backing_s->drain_count, ==, 0);
354         }
355     }
356
357     bdrv_unref(backing);
358     bdrv_unref(bs);
359     blk_unref(blk);
360 }
361
362 static void test_multiparent(void)
363 {
364     BlockBackend *blk_a, *blk_b;
365     BlockDriverState *bs_a, *bs_b, *backing;
366     BDRVTestState *a_s, *b_s, *backing_s;
367
368     blk_a = blk_new(BLK_PERM_ALL, BLK_PERM_ALL);
369     bs_a = bdrv_new_open_driver(&bdrv_test, "test-node-a", BDRV_O_RDWR,
370                                 &error_abort);
371     a_s = bs_a->opaque;
372     blk_insert_bs(blk_a, bs_a, &error_abort);
373
374     blk_b = blk_new(BLK_PERM_ALL, BLK_PERM_ALL);
375     bs_b = bdrv_new_open_driver(&bdrv_test, "test-node-b", BDRV_O_RDWR,
376                                 &error_abort);
377     b_s = bs_b->opaque;
378     blk_insert_bs(blk_b, bs_b, &error_abort);
379
380     backing = bdrv_new_open_driver(&bdrv_test, "backing", 0, &error_abort);
381     backing_s = backing->opaque;
382     bdrv_set_backing_hd(bs_a, backing, &error_abort);
383     bdrv_set_backing_hd(bs_b, backing, &error_abort);
384
385     g_assert_cmpint(bs_a->quiesce_counter, ==, 0);
386     g_assert_cmpint(bs_b->quiesce_counter, ==, 0);
387     g_assert_cmpint(backing->quiesce_counter, ==, 0);
388     g_assert_cmpint(a_s->drain_count, ==, 0);
389     g_assert_cmpint(b_s->drain_count, ==, 0);
390     g_assert_cmpint(backing_s->drain_count, ==, 0);
391
392     do_drain_begin(BDRV_SUBTREE_DRAIN, bs_a);
393
394     g_assert_cmpint(bs_a->quiesce_counter, ==, 1);
395     g_assert_cmpint(bs_b->quiesce_counter, ==, 1);
396     g_assert_cmpint(backing->quiesce_counter, ==, 1);
397     g_assert_cmpint(a_s->drain_count, ==, 1);
398     g_assert_cmpint(b_s->drain_count, ==, 1);
399     g_assert_cmpint(backing_s->drain_count, ==, 1);
400
401     do_drain_begin(BDRV_SUBTREE_DRAIN, bs_b);
402
403     g_assert_cmpint(bs_a->quiesce_counter, ==, 2);
404     g_assert_cmpint(bs_b->quiesce_counter, ==, 2);
405     g_assert_cmpint(backing->quiesce_counter, ==, 2);
406     g_assert_cmpint(a_s->drain_count, ==, 2);
407     g_assert_cmpint(b_s->drain_count, ==, 2);
408     g_assert_cmpint(backing_s->drain_count, ==, 2);
409
410     do_drain_end(BDRV_SUBTREE_DRAIN, bs_b);
411
412     g_assert_cmpint(bs_a->quiesce_counter, ==, 1);
413     g_assert_cmpint(bs_b->quiesce_counter, ==, 1);
414     g_assert_cmpint(backing->quiesce_counter, ==, 1);
415     g_assert_cmpint(a_s->drain_count, ==, 1);
416     g_assert_cmpint(b_s->drain_count, ==, 1);
417     g_assert_cmpint(backing_s->drain_count, ==, 1);
418
419     do_drain_end(BDRV_SUBTREE_DRAIN, bs_a);
420
421     g_assert_cmpint(bs_a->quiesce_counter, ==, 0);
422     g_assert_cmpint(bs_b->quiesce_counter, ==, 0);
423     g_assert_cmpint(backing->quiesce_counter, ==, 0);
424     g_assert_cmpint(a_s->drain_count, ==, 0);
425     g_assert_cmpint(b_s->drain_count, ==, 0);
426     g_assert_cmpint(backing_s->drain_count, ==, 0);
427
428     bdrv_unref(backing);
429     bdrv_unref(bs_a);
430     bdrv_unref(bs_b);
431     blk_unref(blk_a);
432     blk_unref(blk_b);
433 }
434
435 static void test_graph_change(void)
436 {
437     BlockBackend *blk_a, *blk_b;
438     BlockDriverState *bs_a, *bs_b, *backing;
439     BDRVTestState *a_s, *b_s, *backing_s;
440
441     blk_a = blk_new(BLK_PERM_ALL, BLK_PERM_ALL);
442     bs_a = bdrv_new_open_driver(&bdrv_test, "test-node-a", BDRV_O_RDWR,
443                                 &error_abort);
444     a_s = bs_a->opaque;
445     blk_insert_bs(blk_a, bs_a, &error_abort);
446
447     blk_b = blk_new(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     backing = bdrv_new_open_driver(&bdrv_test, "backing", 0, &error_abort);
454     backing_s = backing->opaque;
455     bdrv_set_backing_hd(bs_a, backing, &error_abort);
456
457     g_assert_cmpint(bs_a->quiesce_counter, ==, 0);
458     g_assert_cmpint(bs_b->quiesce_counter, ==, 0);
459     g_assert_cmpint(backing->quiesce_counter, ==, 0);
460     g_assert_cmpint(a_s->drain_count, ==, 0);
461     g_assert_cmpint(b_s->drain_count, ==, 0);
462     g_assert_cmpint(backing_s->drain_count, ==, 0);
463
464     do_drain_begin(BDRV_SUBTREE_DRAIN, bs_a);
465     do_drain_begin(BDRV_SUBTREE_DRAIN, bs_a);
466     do_drain_begin(BDRV_SUBTREE_DRAIN, bs_a);
467     do_drain_begin(BDRV_SUBTREE_DRAIN, bs_b);
468     do_drain_begin(BDRV_SUBTREE_DRAIN, bs_b);
469
470     bdrv_set_backing_hd(bs_b, backing, &error_abort);
471     g_assert_cmpint(bs_a->quiesce_counter, ==, 5);
472     g_assert_cmpint(bs_b->quiesce_counter, ==, 5);
473     g_assert_cmpint(backing->quiesce_counter, ==, 5);
474     g_assert_cmpint(a_s->drain_count, ==, 5);
475     g_assert_cmpint(b_s->drain_count, ==, 5);
476     g_assert_cmpint(backing_s->drain_count, ==, 5);
477
478     bdrv_set_backing_hd(bs_b, NULL, &error_abort);
479     g_assert_cmpint(bs_a->quiesce_counter, ==, 3);
480     g_assert_cmpint(bs_b->quiesce_counter, ==, 2);
481     g_assert_cmpint(backing->quiesce_counter, ==, 3);
482     g_assert_cmpint(a_s->drain_count, ==, 3);
483     g_assert_cmpint(b_s->drain_count, ==, 2);
484     g_assert_cmpint(backing_s->drain_count, ==, 3);
485
486     bdrv_set_backing_hd(bs_b, backing, &error_abort);
487     g_assert_cmpint(bs_a->quiesce_counter, ==, 5);
488     g_assert_cmpint(bs_b->quiesce_counter, ==, 5);
489     g_assert_cmpint(backing->quiesce_counter, ==, 5);
490     g_assert_cmpint(a_s->drain_count, ==, 5);
491     g_assert_cmpint(b_s->drain_count, ==, 5);
492     g_assert_cmpint(backing_s->drain_count, ==, 5);
493
494     do_drain_end(BDRV_SUBTREE_DRAIN, bs_b);
495     do_drain_end(BDRV_SUBTREE_DRAIN, bs_b);
496     do_drain_end(BDRV_SUBTREE_DRAIN, bs_a);
497     do_drain_end(BDRV_SUBTREE_DRAIN, bs_a);
498     do_drain_end(BDRV_SUBTREE_DRAIN, bs_a);
499
500     g_assert_cmpint(bs_a->quiesce_counter, ==, 0);
501     g_assert_cmpint(bs_b->quiesce_counter, ==, 0);
502     g_assert_cmpint(backing->quiesce_counter, ==, 0);
503     g_assert_cmpint(a_s->drain_count, ==, 0);
504     g_assert_cmpint(b_s->drain_count, ==, 0);
505     g_assert_cmpint(backing_s->drain_count, ==, 0);
506
507     bdrv_unref(backing);
508     bdrv_unref(bs_a);
509     bdrv_unref(bs_b);
510     blk_unref(blk_a);
511     blk_unref(blk_b);
512 }
513
514 struct test_iothread_data {
515     BlockDriverState *bs;
516     enum drain_type drain_type;
517     int *aio_ret;
518 };
519
520 static void test_iothread_drain_entry(void *opaque)
521 {
522     struct test_iothread_data *data = opaque;
523
524     aio_context_acquire(bdrv_get_aio_context(data->bs));
525     do_drain_begin(data->drain_type, data->bs);
526     g_assert_cmpint(*data->aio_ret, ==, 0);
527     do_drain_end(data->drain_type, data->bs);
528     aio_context_release(bdrv_get_aio_context(data->bs));
529
530     qemu_event_set(&done_event);
531 }
532
533 static void test_iothread_aio_cb(void *opaque, int ret)
534 {
535     int *aio_ret = opaque;
536     *aio_ret = ret;
537     qemu_event_set(&done_event);
538 }
539
540 /*
541  * Starts an AIO request on a BDS that runs in the AioContext of iothread 1.
542  * The request involves a BH on iothread 2 before it can complete.
543  *
544  * @drain_thread = 0 means that do_drain_begin/end are called from the main
545  * thread, @drain_thread = 1 means that they are called from iothread 1. Drain
546  * for this BDS cannot be called from iothread 2 because only the main thread
547  * may do cross-AioContext polling.
548  */
549 static void test_iothread_common(enum drain_type drain_type, int drain_thread)
550 {
551     BlockBackend *blk;
552     BlockDriverState *bs;
553     BDRVTestState *s;
554     BlockAIOCB *acb;
555     int aio_ret;
556     struct test_iothread_data data;
557
558     IOThread *a = iothread_new();
559     IOThread *b = iothread_new();
560     AioContext *ctx_a = iothread_get_aio_context(a);
561     AioContext *ctx_b = iothread_get_aio_context(b);
562
563     QEMUIOVector qiov;
564     struct iovec iov = {
565         .iov_base = NULL,
566         .iov_len = 0,
567     };
568     qemu_iovec_init_external(&qiov, &iov, 1);
569
570     /* bdrv_drain_all() may only be called from the main loop thread */
571     if (drain_type == BDRV_DRAIN_ALL && drain_thread != 0) {
572         goto out;
573     }
574
575     blk = blk_new(BLK_PERM_ALL, BLK_PERM_ALL);
576     bs = bdrv_new_open_driver(&bdrv_test, "test-node", BDRV_O_RDWR,
577                               &error_abort);
578     s = bs->opaque;
579     blk_insert_bs(blk, bs, &error_abort);
580
581     blk_set_aio_context(blk, ctx_a);
582     aio_context_acquire(ctx_a);
583
584     s->bh_indirection_ctx = ctx_b;
585
586     aio_ret = -EINPROGRESS;
587     if (drain_thread == 0) {
588         acb = blk_aio_preadv(blk, 0, &qiov, 0, test_iothread_aio_cb, &aio_ret);
589     } else {
590         acb = blk_aio_preadv(blk, 0, &qiov, 0, aio_ret_cb, &aio_ret);
591     }
592     g_assert(acb != NULL);
593     g_assert_cmpint(aio_ret, ==, -EINPROGRESS);
594
595     aio_context_release(ctx_a);
596
597     data = (struct test_iothread_data) {
598         .bs         = bs,
599         .drain_type = drain_type,
600         .aio_ret    = &aio_ret,
601     };
602
603     switch (drain_thread) {
604     case 0:
605         if (drain_type != BDRV_DRAIN_ALL) {
606             aio_context_acquire(ctx_a);
607         }
608
609         /* The request is running on the IOThread a. Draining its block device
610          * will make sure that it has completed as far as the BDS is concerned,
611          * but the drain in this thread can continue immediately after
612          * bdrv_dec_in_flight() and aio_ret might be assigned only slightly
613          * later. */
614         qemu_event_reset(&done_event);
615         do_drain_begin(drain_type, bs);
616         g_assert_cmpint(bs->in_flight, ==, 0);
617
618         if (drain_type != BDRV_DRAIN_ALL) {
619             aio_context_release(ctx_a);
620         }
621         qemu_event_wait(&done_event);
622         if (drain_type != BDRV_DRAIN_ALL) {
623             aio_context_acquire(ctx_a);
624         }
625
626         g_assert_cmpint(aio_ret, ==, 0);
627         do_drain_end(drain_type, bs);
628
629         if (drain_type != BDRV_DRAIN_ALL) {
630             aio_context_release(ctx_a);
631         }
632         break;
633     case 1:
634         qemu_event_reset(&done_event);
635         aio_bh_schedule_oneshot(ctx_a, test_iothread_drain_entry, &data);
636         qemu_event_wait(&done_event);
637         break;
638     default:
639         g_assert_not_reached();
640     }
641
642     aio_context_acquire(ctx_a);
643     blk_set_aio_context(blk, qemu_get_aio_context());
644     aio_context_release(ctx_a);
645
646     bdrv_unref(bs);
647     blk_unref(blk);
648
649 out:
650     iothread_join(a);
651     iothread_join(b);
652 }
653
654 static void test_iothread_drain_all(void)
655 {
656     test_iothread_common(BDRV_DRAIN_ALL, 0);
657     test_iothread_common(BDRV_DRAIN_ALL, 1);
658 }
659
660 static void test_iothread_drain(void)
661 {
662     test_iothread_common(BDRV_DRAIN, 0);
663     test_iothread_common(BDRV_DRAIN, 1);
664 }
665
666 static void test_iothread_drain_subtree(void)
667 {
668     test_iothread_common(BDRV_SUBTREE_DRAIN, 0);
669     test_iothread_common(BDRV_SUBTREE_DRAIN, 1);
670 }
671
672
673 typedef struct TestBlockJob {
674     BlockJob common;
675     bool should_complete;
676 } TestBlockJob;
677
678 static void test_job_completed(Job *job, void *opaque)
679 {
680     job_completed(job, 0, NULL);
681 }
682
683 static void coroutine_fn test_job_start(void *opaque)
684 {
685     TestBlockJob *s = opaque;
686
687     job_transition_to_ready(&s->common.job);
688     while (!s->should_complete) {
689         /* Avoid block_job_sleep_ns() because it marks the job as !busy. We
690          * want to emulate some actual activity (probably some I/O) here so
691          * that drain has to wait for this acitivity to stop. */
692         qemu_co_sleep_ns(QEMU_CLOCK_REALTIME, 100000);
693         job_pause_point(&s->common.job);
694     }
695
696     job_defer_to_main_loop(&s->common.job, test_job_completed, NULL);
697 }
698
699 static void test_job_complete(Job *job, Error **errp)
700 {
701     TestBlockJob *s = container_of(job, TestBlockJob, common.job);
702     s->should_complete = true;
703 }
704
705 BlockJobDriver test_job_driver = {
706     .job_driver = {
707         .instance_size  = sizeof(TestBlockJob),
708         .free           = block_job_free,
709         .user_resume    = block_job_user_resume,
710         .drain          = block_job_drain,
711         .start          = test_job_start,
712         .complete       = test_job_complete,
713     },
714 };
715
716 static void test_blockjob_common(enum drain_type drain_type)
717 {
718     BlockBackend *blk_src, *blk_target;
719     BlockDriverState *src, *target;
720     BlockJob *job;
721     int ret;
722
723     src = bdrv_new_open_driver(&bdrv_test, "source", BDRV_O_RDWR,
724                                &error_abort);
725     blk_src = blk_new(BLK_PERM_ALL, BLK_PERM_ALL);
726     blk_insert_bs(blk_src, src, &error_abort);
727
728     target = bdrv_new_open_driver(&bdrv_test, "target", BDRV_O_RDWR,
729                                   &error_abort);
730     blk_target = blk_new(BLK_PERM_ALL, BLK_PERM_ALL);
731     blk_insert_bs(blk_target, target, &error_abort);
732
733     job = block_job_create("job0", &test_job_driver, NULL, src, 0, BLK_PERM_ALL,
734                            0, 0, NULL, NULL, &error_abort);
735     block_job_add_bdrv(job, "target", target, 0, BLK_PERM_ALL, &error_abort);
736     job_start(&job->job);
737
738     g_assert_cmpint(job->job.pause_count, ==, 0);
739     g_assert_false(job->job.paused);
740     g_assert_true(job->job.busy); /* We're in job_sleep_ns() */
741
742     do_drain_begin(drain_type, src);
743
744     if (drain_type == BDRV_DRAIN_ALL) {
745         /* bdrv_drain_all() drains both src and target */
746         g_assert_cmpint(job->job.pause_count, ==, 2);
747     } else {
748         g_assert_cmpint(job->job.pause_count, ==, 1);
749     }
750     g_assert_true(job->job.paused);
751     g_assert_false(job->job.busy); /* The job is paused */
752
753     do_drain_end(drain_type, src);
754
755     g_assert_cmpint(job->job.pause_count, ==, 0);
756     g_assert_false(job->job.paused);
757     g_assert_true(job->job.busy); /* We're in qemu_co_sleep_ns() */
758
759     do_drain_begin(drain_type, target);
760
761     if (drain_type == BDRV_DRAIN_ALL) {
762         /* bdrv_drain_all() drains both src and target */
763         g_assert_cmpint(job->job.pause_count, ==, 2);
764     } else {
765         g_assert_cmpint(job->job.pause_count, ==, 1);
766     }
767     g_assert_true(job->job.paused);
768     g_assert_false(job->job.busy); /* The job is paused */
769
770     do_drain_end(drain_type, target);
771
772     g_assert_cmpint(job->job.pause_count, ==, 0);
773     g_assert_false(job->job.paused);
774     g_assert_true(job->job.busy); /* We're in job_sleep_ns() */
775
776     ret = job_complete_sync(&job->job, &error_abort);
777     g_assert_cmpint(ret, ==, 0);
778
779     blk_unref(blk_src);
780     blk_unref(blk_target);
781     bdrv_unref(src);
782     bdrv_unref(target);
783 }
784
785 static void test_blockjob_drain_all(void)
786 {
787     test_blockjob_common(BDRV_DRAIN_ALL);
788 }
789
790 static void test_blockjob_drain(void)
791 {
792     test_blockjob_common(BDRV_DRAIN);
793 }
794
795 static void test_blockjob_drain_subtree(void)
796 {
797     test_blockjob_common(BDRV_SUBTREE_DRAIN);
798 }
799
800
801 typedef struct BDRVTestTopState {
802     BdrvChild *wait_child;
803 } BDRVTestTopState;
804
805 static void bdrv_test_top_close(BlockDriverState *bs)
806 {
807     BdrvChild *c, *next_c;
808     QLIST_FOREACH_SAFE(c, &bs->children, next, next_c) {
809         bdrv_unref_child(bs, c);
810     }
811 }
812
813 static int coroutine_fn bdrv_test_top_co_preadv(BlockDriverState *bs,
814                                                 uint64_t offset, uint64_t bytes,
815                                                 QEMUIOVector *qiov, int flags)
816 {
817     BDRVTestTopState *tts = bs->opaque;
818     return bdrv_co_preadv(tts->wait_child, offset, bytes, qiov, flags);
819 }
820
821 static BlockDriver bdrv_test_top_driver = {
822     .format_name            = "test_top_driver",
823     .instance_size          = sizeof(BDRVTestTopState),
824
825     .bdrv_close             = bdrv_test_top_close,
826     .bdrv_co_preadv         = bdrv_test_top_co_preadv,
827
828     .bdrv_child_perm        = bdrv_format_default_perms,
829 };
830
831 typedef struct TestCoDeleteByDrainData {
832     BlockBackend *blk;
833     bool detach_instead_of_delete;
834     bool done;
835 } TestCoDeleteByDrainData;
836
837 static void coroutine_fn test_co_delete_by_drain(void *opaque)
838 {
839     TestCoDeleteByDrainData *dbdd = opaque;
840     BlockBackend *blk = dbdd->blk;
841     BlockDriverState *bs = blk_bs(blk);
842     BDRVTestTopState *tts = bs->opaque;
843     void *buffer = g_malloc(65536);
844     QEMUIOVector qiov;
845     struct iovec iov = {
846         .iov_base = buffer,
847         .iov_len  = 65536,
848     };
849
850     qemu_iovec_init_external(&qiov, &iov, 1);
851
852     /* Pretend some internal write operation from parent to child.
853      * Important: We have to read from the child, not from the parent!
854      * Draining works by first propagating it all up the tree to the
855      * root and then waiting for drainage from root to the leaves
856      * (protocol nodes).  If we have a request waiting on the root,
857      * everything will be drained before we go back down the tree, but
858      * we do not want that.  We want to be in the middle of draining
859      * when this following requests returns. */
860     bdrv_co_preadv(tts->wait_child, 0, 65536, &qiov, 0);
861
862     g_assert_cmpint(bs->refcnt, ==, 1);
863
864     if (!dbdd->detach_instead_of_delete) {
865         blk_unref(blk);
866     } else {
867         BdrvChild *c, *next_c;
868         QLIST_FOREACH_SAFE(c, &bs->children, next, next_c) {
869             bdrv_unref_child(bs, c);
870         }
871     }
872
873     dbdd->done = true;
874 }
875
876 /**
877  * Test what happens when some BDS has some children, you drain one of
878  * them and this results in the BDS being deleted.
879  *
880  * If @detach_instead_of_delete is set, the BDS is not going to be
881  * deleted but will only detach all of its children.
882  */
883 static void do_test_delete_by_drain(bool detach_instead_of_delete,
884                                     enum drain_type drain_type)
885 {
886     BlockBackend *blk;
887     BlockDriverState *bs, *child_bs, *null_bs;
888     BDRVTestTopState *tts;
889     TestCoDeleteByDrainData dbdd;
890     Coroutine *co;
891
892     bs = bdrv_new_open_driver(&bdrv_test_top_driver, "top", BDRV_O_RDWR,
893                               &error_abort);
894     bs->total_sectors = 65536 >> BDRV_SECTOR_BITS;
895     tts = bs->opaque;
896
897     null_bs = bdrv_open("null-co://", NULL, NULL, BDRV_O_RDWR | BDRV_O_PROTOCOL,
898                         &error_abort);
899     bdrv_attach_child(bs, null_bs, "null-child", &child_file, &error_abort);
900
901     /* This child will be the one to pass to requests through to, and
902      * it will stall until a drain occurs */
903     child_bs = bdrv_new_open_driver(&bdrv_test, "child", BDRV_O_RDWR,
904                                     &error_abort);
905     child_bs->total_sectors = 65536 >> BDRV_SECTOR_BITS;
906     /* Takes our reference to child_bs */
907     tts->wait_child = bdrv_attach_child(bs, child_bs, "wait-child", &child_file,
908                                         &error_abort);
909
910     /* This child is just there to be deleted
911      * (for detach_instead_of_delete == true) */
912     null_bs = bdrv_open("null-co://", NULL, NULL, BDRV_O_RDWR | BDRV_O_PROTOCOL,
913                         &error_abort);
914     bdrv_attach_child(bs, null_bs, "null-child", &child_file, &error_abort);
915
916     blk = blk_new(BLK_PERM_ALL, BLK_PERM_ALL);
917     blk_insert_bs(blk, bs, &error_abort);
918
919     /* Referenced by blk now */
920     bdrv_unref(bs);
921
922     g_assert_cmpint(bs->refcnt, ==, 1);
923     g_assert_cmpint(child_bs->refcnt, ==, 1);
924     g_assert_cmpint(null_bs->refcnt, ==, 1);
925
926
927     dbdd = (TestCoDeleteByDrainData){
928         .blk = blk,
929         .detach_instead_of_delete = detach_instead_of_delete,
930         .done = false,
931     };
932     co = qemu_coroutine_create(test_co_delete_by_drain, &dbdd);
933     qemu_coroutine_enter(co);
934
935     /* Drain the child while the read operation is still pending.
936      * This should result in the operation finishing and
937      * test_co_delete_by_drain() resuming.  Thus, @bs will be deleted
938      * and the coroutine will exit while this drain operation is still
939      * in progress. */
940     switch (drain_type) {
941     case BDRV_DRAIN:
942         bdrv_ref(child_bs);
943         bdrv_drain(child_bs);
944         bdrv_unref(child_bs);
945         break;
946     case BDRV_SUBTREE_DRAIN:
947         /* Would have to ref/unref bs here for !detach_instead_of_delete, but
948          * then the whole test becomes pointless because the graph changes
949          * don't occur during the drain any more. */
950         assert(detach_instead_of_delete);
951         bdrv_subtree_drained_begin(bs);
952         bdrv_subtree_drained_end(bs);
953         break;
954     default:
955         g_assert_not_reached();
956     }
957
958     while (!dbdd.done) {
959         aio_poll(qemu_get_aio_context(), true);
960     }
961
962     if (detach_instead_of_delete) {
963         /* Here, the reference has not passed over to the coroutine,
964          * so we have to delete the BB ourselves */
965         blk_unref(blk);
966     }
967 }
968
969 static void test_delete_by_drain(void)
970 {
971     do_test_delete_by_drain(false, BDRV_DRAIN);
972 }
973
974 static void test_detach_by_drain(void)
975 {
976     do_test_delete_by_drain(true, BDRV_DRAIN);
977 }
978
979 static void test_detach_by_drain_subtree(void)
980 {
981     do_test_delete_by_drain(true, BDRV_SUBTREE_DRAIN);
982 }
983
984
985 struct detach_by_parent_data {
986     BlockDriverState *parent_b;
987     BdrvChild *child_b;
988     BlockDriverState *c;
989     BdrvChild *child_c;
990 };
991
992 static void detach_by_parent_aio_cb(void *opaque, int ret)
993 {
994     struct detach_by_parent_data *data = opaque;
995
996     g_assert_cmpint(ret, ==, 0);
997     bdrv_unref_child(data->parent_b, data->child_b);
998
999     bdrv_ref(data->c);
1000     data->child_c = bdrv_attach_child(data->parent_b, data->c, "PB-C",
1001                                       &child_file, &error_abort);
1002 }
1003
1004 /*
1005  * Initial graph:
1006  *
1007  * PA     PB
1008  *    \ /   \
1009  *     A     B     C
1010  *
1011  * PA has a pending write request whose callback changes the child nodes of PB:
1012  * It removes B and adds C instead. The subtree of PB is drained, which will
1013  * indirectly drain the write request, too.
1014  */
1015 static void test_detach_by_parent_cb(void)
1016 {
1017     BlockBackend *blk;
1018     BlockDriverState *parent_a, *parent_b, *a, *b, *c;
1019     BdrvChild *child_a, *child_b;
1020     BlockAIOCB *acb;
1021     struct detach_by_parent_data data;
1022
1023     QEMUIOVector qiov;
1024     struct iovec iov = {
1025         .iov_base = NULL,
1026         .iov_len = 0,
1027     };
1028     qemu_iovec_init_external(&qiov, &iov, 1);
1029
1030     /* Create all involved nodes */
1031     parent_a = bdrv_new_open_driver(&bdrv_test, "parent-a", BDRV_O_RDWR,
1032                                     &error_abort);
1033     parent_b = bdrv_new_open_driver(&bdrv_test, "parent-b", 0,
1034                                     &error_abort);
1035
1036     a = bdrv_new_open_driver(&bdrv_test, "a", BDRV_O_RDWR, &error_abort);
1037     b = bdrv_new_open_driver(&bdrv_test, "b", BDRV_O_RDWR, &error_abort);
1038     c = bdrv_new_open_driver(&bdrv_test, "c", BDRV_O_RDWR, &error_abort);
1039
1040     /* blk is a BB for parent-a */
1041     blk = blk_new(BLK_PERM_ALL, BLK_PERM_ALL);
1042     blk_insert_bs(blk, parent_a, &error_abort);
1043     bdrv_unref(parent_a);
1044
1045     /* Set child relationships */
1046     bdrv_ref(b);
1047     bdrv_ref(a);
1048     child_b = bdrv_attach_child(parent_b, b, "PB-B", &child_file, &error_abort);
1049     child_a = bdrv_attach_child(parent_b, a, "PB-A", &child_backing, &error_abort);
1050
1051     bdrv_ref(a);
1052     bdrv_attach_child(parent_a, a, "PA-A", &child_file, &error_abort);
1053
1054     g_assert_cmpint(parent_a->refcnt, ==, 1);
1055     g_assert_cmpint(parent_b->refcnt, ==, 1);
1056     g_assert_cmpint(a->refcnt, ==, 3);
1057     g_assert_cmpint(b->refcnt, ==, 2);
1058     g_assert_cmpint(c->refcnt, ==, 1);
1059
1060     g_assert(QLIST_FIRST(&parent_b->children) == child_a);
1061     g_assert(QLIST_NEXT(child_a, next) == child_b);
1062     g_assert(QLIST_NEXT(child_b, next) == NULL);
1063
1064     /* Start the evil write request */
1065     data = (struct detach_by_parent_data) {
1066         .parent_b = parent_b,
1067         .child_b = child_b,
1068         .c = c,
1069     };
1070     acb = blk_aio_preadv(blk, 0, &qiov, 0, detach_by_parent_aio_cb, &data);
1071     g_assert(acb != NULL);
1072
1073     /* Drain and check the expected result */
1074     bdrv_subtree_drained_begin(parent_b);
1075
1076     g_assert(data.child_c != NULL);
1077
1078     g_assert_cmpint(parent_a->refcnt, ==, 1);
1079     g_assert_cmpint(parent_b->refcnt, ==, 1);
1080     g_assert_cmpint(a->refcnt, ==, 3);
1081     g_assert_cmpint(b->refcnt, ==, 1);
1082     g_assert_cmpint(c->refcnt, ==, 2);
1083
1084     g_assert(QLIST_FIRST(&parent_b->children) == data.child_c);
1085     g_assert(QLIST_NEXT(data.child_c, next) == child_a);
1086     g_assert(QLIST_NEXT(child_a, next) == NULL);
1087
1088     g_assert_cmpint(parent_a->quiesce_counter, ==, 1);
1089     g_assert_cmpint(parent_b->quiesce_counter, ==, 1);
1090     g_assert_cmpint(a->quiesce_counter, ==, 1);
1091     g_assert_cmpint(b->quiesce_counter, ==, 0);
1092     g_assert_cmpint(c->quiesce_counter, ==, 1);
1093
1094     bdrv_subtree_drained_end(parent_b);
1095
1096     bdrv_unref(parent_b);
1097     blk_unref(blk);
1098
1099     /* XXX Once bdrv_close() unref's children instead of just detaching them,
1100      * this won't be necessary any more. */
1101     bdrv_unref(a);
1102     bdrv_unref(a);
1103     bdrv_unref(c);
1104
1105     g_assert_cmpint(a->refcnt, ==, 1);
1106     g_assert_cmpint(b->refcnt, ==, 1);
1107     g_assert_cmpint(c->refcnt, ==, 1);
1108     bdrv_unref(a);
1109     bdrv_unref(b);
1110     bdrv_unref(c);
1111 }
1112
1113
1114 int main(int argc, char **argv)
1115 {
1116     int ret;
1117
1118     bdrv_init();
1119     qemu_init_main_loop(&error_abort);
1120
1121     g_test_init(&argc, &argv, NULL);
1122     qemu_event_init(&done_event, false);
1123
1124     g_test_add_func("/bdrv-drain/driver-cb/drain_all", test_drv_cb_drain_all);
1125     g_test_add_func("/bdrv-drain/driver-cb/drain", test_drv_cb_drain);
1126     g_test_add_func("/bdrv-drain/driver-cb/drain_subtree",
1127                     test_drv_cb_drain_subtree);
1128
1129     g_test_add_func("/bdrv-drain/driver-cb/co/drain_all",
1130                     test_drv_cb_co_drain_all);
1131     g_test_add_func("/bdrv-drain/driver-cb/co/drain", test_drv_cb_co_drain);
1132     g_test_add_func("/bdrv-drain/driver-cb/co/drain_subtree",
1133                     test_drv_cb_co_drain_subtree);
1134
1135
1136     g_test_add_func("/bdrv-drain/quiesce/drain_all", test_quiesce_drain_all);
1137     g_test_add_func("/bdrv-drain/quiesce/drain", test_quiesce_drain);
1138     g_test_add_func("/bdrv-drain/quiesce/drain_subtree",
1139                     test_quiesce_drain_subtree);
1140
1141     g_test_add_func("/bdrv-drain/quiesce/co/drain_all",
1142                     test_quiesce_co_drain_all);
1143     g_test_add_func("/bdrv-drain/quiesce/co/drain", test_quiesce_co_drain);
1144     g_test_add_func("/bdrv-drain/quiesce/co/drain_subtree",
1145                     test_quiesce_co_drain_subtree);
1146
1147     g_test_add_func("/bdrv-drain/nested", test_nested);
1148     g_test_add_func("/bdrv-drain/multiparent", test_multiparent);
1149     g_test_add_func("/bdrv-drain/graph-change", test_graph_change);
1150
1151     g_test_add_func("/bdrv-drain/iothread/drain_all", test_iothread_drain_all);
1152     g_test_add_func("/bdrv-drain/iothread/drain", test_iothread_drain);
1153     g_test_add_func("/bdrv-drain/iothread/drain_subtree",
1154                     test_iothread_drain_subtree);
1155
1156     g_test_add_func("/bdrv-drain/blockjob/drain_all", test_blockjob_drain_all);
1157     g_test_add_func("/bdrv-drain/blockjob/drain", test_blockjob_drain);
1158     g_test_add_func("/bdrv-drain/blockjob/drain_subtree",
1159                     test_blockjob_drain_subtree);
1160
1161     g_test_add_func("/bdrv-drain/deletion/drain", test_delete_by_drain);
1162     g_test_add_func("/bdrv-drain/detach/drain", test_detach_by_drain);
1163     g_test_add_func("/bdrv-drain/detach/drain_subtree", test_detach_by_drain_subtree);
1164     g_test_add_func("/bdrv-drain/detach/parent_cb", test_detach_by_parent_cb);
1165
1166     ret = g_test_run();
1167     qemu_event_destroy(&done_event);
1168     return ret;
1169 }
This page took 0.086163 seconds and 4 git commands to generate.