]> git.proxmox.com Git - mirror_qemu.git/blob - tests/test-bdrv-drain.c
test-bdrv-drain: Test nested drain sections
[mirror_qemu.git] / tests / test-bdrv-drain.c
1 /*
2 * Block node draining tests
3 *
4 * Copyright (c) 2017 Kevin Wolf <kwolf@redhat.com>
5 *
6 * Permission is hereby granted, free of charge, to any person obtaining a copy
7 * of this software and associated documentation files (the "Software"), to deal
8 * in the Software without restriction, including without limitation the rights
9 * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
10 * copies of the Software, and to permit persons to whom the Software is
11 * furnished to do so, subject to the following conditions:
12 *
13 * The above copyright notice and this permission notice shall be included in
14 * all copies or substantial portions of the Software.
15 *
16 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
17 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
18 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
19 * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
20 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
21 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
22 * THE SOFTWARE.
23 */
24
25 #include "qemu/osdep.h"
26 #include "block/block.h"
27 #include "block/blockjob_int.h"
28 #include "sysemu/block-backend.h"
29 #include "qapi/error.h"
30
31 typedef struct BDRVTestState {
32 int drain_count;
33 } BDRVTestState;
34
35 static void coroutine_fn bdrv_test_co_drain_begin(BlockDriverState *bs)
36 {
37 BDRVTestState *s = bs->opaque;
38 s->drain_count++;
39 }
40
41 static void coroutine_fn bdrv_test_co_drain_end(BlockDriverState *bs)
42 {
43 BDRVTestState *s = bs->opaque;
44 s->drain_count--;
45 }
46
47 static void bdrv_test_close(BlockDriverState *bs)
48 {
49 BDRVTestState *s = bs->opaque;
50 g_assert_cmpint(s->drain_count, >, 0);
51 }
52
53 static int coroutine_fn bdrv_test_co_preadv(BlockDriverState *bs,
54 uint64_t offset, uint64_t bytes,
55 QEMUIOVector *qiov, int flags)
56 {
57 /* We want this request to stay until the polling loop in drain waits for
58 * it to complete. We need to sleep a while as bdrv_drain_invoke() comes
59 * first and polls its result, too, but it shouldn't accidentally complete
60 * this request yet. */
61 qemu_co_sleep_ns(QEMU_CLOCK_REALTIME, 100000);
62
63 return 0;
64 }
65
66 static BlockDriver bdrv_test = {
67 .format_name = "test",
68 .instance_size = sizeof(BDRVTestState),
69
70 .bdrv_close = bdrv_test_close,
71 .bdrv_co_preadv = bdrv_test_co_preadv,
72
73 .bdrv_co_drain_begin = bdrv_test_co_drain_begin,
74 .bdrv_co_drain_end = bdrv_test_co_drain_end,
75
76 .bdrv_child_perm = bdrv_format_default_perms,
77 };
78
79 static void aio_ret_cb(void *opaque, int ret)
80 {
81 int *aio_ret = opaque;
82 *aio_ret = ret;
83 }
84
85 enum drain_type {
86 BDRV_DRAIN_ALL,
87 BDRV_DRAIN,
88 DRAIN_TYPE_MAX,
89 };
90
91 static void do_drain_begin(enum drain_type drain_type, BlockDriverState *bs)
92 {
93 switch (drain_type) {
94 case BDRV_DRAIN_ALL: bdrv_drain_all_begin(); break;
95 case BDRV_DRAIN: bdrv_drained_begin(bs); break;
96 default: g_assert_not_reached();
97 }
98 }
99
100 static void do_drain_end(enum drain_type drain_type, BlockDriverState *bs)
101 {
102 switch (drain_type) {
103 case BDRV_DRAIN_ALL: bdrv_drain_all_end(); break;
104 case BDRV_DRAIN: bdrv_drained_end(bs); break;
105 default: g_assert_not_reached();
106 }
107 }
108
109 static void test_drv_cb_common(enum drain_type drain_type, bool recursive)
110 {
111 BlockBackend *blk;
112 BlockDriverState *bs, *backing;
113 BDRVTestState *s, *backing_s;
114 BlockAIOCB *acb;
115 int aio_ret;
116
117 QEMUIOVector qiov;
118 struct iovec iov = {
119 .iov_base = NULL,
120 .iov_len = 0,
121 };
122 qemu_iovec_init_external(&qiov, &iov, 1);
123
124 blk = blk_new(BLK_PERM_ALL, BLK_PERM_ALL);
125 bs = bdrv_new_open_driver(&bdrv_test, "test-node", BDRV_O_RDWR,
126 &error_abort);
127 s = bs->opaque;
128 blk_insert_bs(blk, bs, &error_abort);
129
130 backing = bdrv_new_open_driver(&bdrv_test, "backing", 0, &error_abort);
131 backing_s = backing->opaque;
132 bdrv_set_backing_hd(bs, backing, &error_abort);
133
134 /* Simple bdrv_drain_all_begin/end pair, check that CBs are called */
135 g_assert_cmpint(s->drain_count, ==, 0);
136 g_assert_cmpint(backing_s->drain_count, ==, 0);
137
138 do_drain_begin(drain_type, bs);
139
140 g_assert_cmpint(s->drain_count, ==, 1);
141 g_assert_cmpint(backing_s->drain_count, ==, !!recursive);
142
143 do_drain_end(drain_type, bs);
144
145 g_assert_cmpint(s->drain_count, ==, 0);
146 g_assert_cmpint(backing_s->drain_count, ==, 0);
147
148 /* Now do the same while a request is pending */
149 aio_ret = -EINPROGRESS;
150 acb = blk_aio_preadv(blk, 0, &qiov, 0, aio_ret_cb, &aio_ret);
151 g_assert(acb != NULL);
152 g_assert_cmpint(aio_ret, ==, -EINPROGRESS);
153
154 g_assert_cmpint(s->drain_count, ==, 0);
155 g_assert_cmpint(backing_s->drain_count, ==, 0);
156
157 do_drain_begin(drain_type, bs);
158
159 g_assert_cmpint(aio_ret, ==, 0);
160 g_assert_cmpint(s->drain_count, ==, 1);
161 g_assert_cmpint(backing_s->drain_count, ==, !!recursive);
162
163 do_drain_end(drain_type, bs);
164
165 g_assert_cmpint(s->drain_count, ==, 0);
166 g_assert_cmpint(backing_s->drain_count, ==, 0);
167
168 bdrv_unref(backing);
169 bdrv_unref(bs);
170 blk_unref(blk);
171 }
172
173 static void test_drv_cb_drain_all(void)
174 {
175 test_drv_cb_common(BDRV_DRAIN_ALL, true);
176 }
177
178 static void test_drv_cb_drain(void)
179 {
180 test_drv_cb_common(BDRV_DRAIN, false);
181 }
182
183 static void test_quiesce_common(enum drain_type drain_type, bool recursive)
184 {
185 BlockBackend *blk;
186 BlockDriverState *bs, *backing;
187
188 blk = blk_new(BLK_PERM_ALL, BLK_PERM_ALL);
189 bs = bdrv_new_open_driver(&bdrv_test, "test-node", BDRV_O_RDWR,
190 &error_abort);
191 blk_insert_bs(blk, bs, &error_abort);
192
193 backing = bdrv_new_open_driver(&bdrv_test, "backing", 0, &error_abort);
194 bdrv_set_backing_hd(bs, backing, &error_abort);
195
196 g_assert_cmpint(bs->quiesce_counter, ==, 0);
197 g_assert_cmpint(backing->quiesce_counter, ==, 0);
198
199 do_drain_begin(drain_type, bs);
200
201 g_assert_cmpint(bs->quiesce_counter, ==, 1);
202 g_assert_cmpint(backing->quiesce_counter, ==, !!recursive);
203
204 do_drain_end(drain_type, bs);
205
206 g_assert_cmpint(bs->quiesce_counter, ==, 0);
207 g_assert_cmpint(backing->quiesce_counter, ==, 0);
208
209 bdrv_unref(backing);
210 bdrv_unref(bs);
211 blk_unref(blk);
212 }
213
214 static void test_quiesce_drain_all(void)
215 {
216 // XXX drain_all doesn't quiesce
217 //test_quiesce_common(BDRV_DRAIN_ALL, true);
218 }
219
220 static void test_quiesce_drain(void)
221 {
222 test_quiesce_common(BDRV_DRAIN, false);
223 }
224
225 static void test_nested(void)
226 {
227 BlockBackend *blk;
228 BlockDriverState *bs, *backing;
229 BDRVTestState *s, *backing_s;
230 enum drain_type outer, inner;
231
232 blk = blk_new(BLK_PERM_ALL, BLK_PERM_ALL);
233 bs = bdrv_new_open_driver(&bdrv_test, "test-node", BDRV_O_RDWR,
234 &error_abort);
235 s = bs->opaque;
236 blk_insert_bs(blk, bs, &error_abort);
237
238 backing = bdrv_new_open_driver(&bdrv_test, "backing", 0, &error_abort);
239 backing_s = backing->opaque;
240 bdrv_set_backing_hd(bs, backing, &error_abort);
241
242 for (outer = 0; outer < DRAIN_TYPE_MAX; outer++) {
243 for (inner = 0; inner < DRAIN_TYPE_MAX; inner++) {
244 /* XXX bdrv_drain_all() doesn't increase the quiesce_counter */
245 int bs_quiesce = (outer != BDRV_DRAIN_ALL) +
246 (inner != BDRV_DRAIN_ALL);
247 int backing_quiesce = 0;
248 int backing_cb_cnt = (outer != BDRV_DRAIN) +
249 (inner != BDRV_DRAIN);
250
251 g_assert_cmpint(bs->quiesce_counter, ==, 0);
252 g_assert_cmpint(backing->quiesce_counter, ==, 0);
253 g_assert_cmpint(s->drain_count, ==, 0);
254 g_assert_cmpint(backing_s->drain_count, ==, 0);
255
256 do_drain_begin(outer, bs);
257 do_drain_begin(inner, bs);
258
259 g_assert_cmpint(bs->quiesce_counter, ==, bs_quiesce);
260 g_assert_cmpint(backing->quiesce_counter, ==, backing_quiesce);
261 g_assert_cmpint(s->drain_count, ==, 2);
262 g_assert_cmpint(backing_s->drain_count, ==, backing_cb_cnt);
263
264 do_drain_end(inner, bs);
265 do_drain_end(outer, bs);
266
267 g_assert_cmpint(bs->quiesce_counter, ==, 0);
268 g_assert_cmpint(backing->quiesce_counter, ==, 0);
269 g_assert_cmpint(s->drain_count, ==, 0);
270 g_assert_cmpint(backing_s->drain_count, ==, 0);
271 }
272 }
273
274 bdrv_unref(backing);
275 bdrv_unref(bs);
276 blk_unref(blk);
277 }
278
279
280 typedef struct TestBlockJob {
281 BlockJob common;
282 bool should_complete;
283 } TestBlockJob;
284
285 static void test_job_completed(BlockJob *job, void *opaque)
286 {
287 block_job_completed(job, 0);
288 }
289
290 static void coroutine_fn test_job_start(void *opaque)
291 {
292 TestBlockJob *s = opaque;
293
294 while (!s->should_complete) {
295 block_job_sleep_ns(&s->common, 100000);
296 }
297
298 block_job_defer_to_main_loop(&s->common, test_job_completed, NULL);
299 }
300
301 static void test_job_complete(BlockJob *job, Error **errp)
302 {
303 TestBlockJob *s = container_of(job, TestBlockJob, common);
304 s->should_complete = true;
305 }
306
307 BlockJobDriver test_job_driver = {
308 .instance_size = sizeof(TestBlockJob),
309 .start = test_job_start,
310 .complete = test_job_complete,
311 };
312
313 static void test_blockjob_common(enum drain_type drain_type)
314 {
315 BlockBackend *blk_src, *blk_target;
316 BlockDriverState *src, *target;
317 BlockJob *job;
318 int ret;
319
320 src = bdrv_new_open_driver(&bdrv_test, "source", BDRV_O_RDWR,
321 &error_abort);
322 blk_src = blk_new(BLK_PERM_ALL, BLK_PERM_ALL);
323 blk_insert_bs(blk_src, src, &error_abort);
324
325 target = bdrv_new_open_driver(&bdrv_test, "target", BDRV_O_RDWR,
326 &error_abort);
327 blk_target = blk_new(BLK_PERM_ALL, BLK_PERM_ALL);
328 blk_insert_bs(blk_target, target, &error_abort);
329
330 job = block_job_create("job0", &test_job_driver, src, 0, BLK_PERM_ALL, 0,
331 0, NULL, NULL, &error_abort);
332 block_job_add_bdrv(job, "target", target, 0, BLK_PERM_ALL, &error_abort);
333 block_job_start(job);
334
335 g_assert_cmpint(job->pause_count, ==, 0);
336 g_assert_false(job->paused);
337 g_assert_false(job->busy); /* We're in block_job_sleep_ns() */
338
339 do_drain_begin(drain_type, src);
340
341 if (drain_type == BDRV_DRAIN_ALL) {
342 /* bdrv_drain_all() drains both src and target */
343 g_assert_cmpint(job->pause_count, ==, 2);
344 } else {
345 g_assert_cmpint(job->pause_count, ==, 1);
346 }
347 /* XXX We don't wait until the job is actually paused. Is this okay? */
348 /* g_assert_true(job->paused); */
349 g_assert_false(job->busy); /* The job is paused */
350
351 do_drain_end(drain_type, src);
352
353 g_assert_cmpint(job->pause_count, ==, 0);
354 g_assert_false(job->paused);
355 g_assert_false(job->busy); /* We're in block_job_sleep_ns() */
356
357 do_drain_begin(drain_type, target);
358
359 if (drain_type == BDRV_DRAIN_ALL) {
360 /* bdrv_drain_all() drains both src and target */
361 g_assert_cmpint(job->pause_count, ==, 2);
362 } else {
363 g_assert_cmpint(job->pause_count, ==, 1);
364 }
365 /* XXX We don't wait until the job is actually paused. Is this okay? */
366 /* g_assert_true(job->paused); */
367 g_assert_false(job->busy); /* The job is paused */
368
369 do_drain_end(drain_type, target);
370
371 g_assert_cmpint(job->pause_count, ==, 0);
372 g_assert_false(job->paused);
373 g_assert_false(job->busy); /* We're in block_job_sleep_ns() */
374
375 ret = block_job_complete_sync(job, &error_abort);
376 g_assert_cmpint(ret, ==, 0);
377
378 blk_unref(blk_src);
379 blk_unref(blk_target);
380 bdrv_unref(src);
381 bdrv_unref(target);
382 }
383
384 static void test_blockjob_drain_all(void)
385 {
386 test_blockjob_common(BDRV_DRAIN_ALL);
387 }
388
389 static void test_blockjob_drain(void)
390 {
391 test_blockjob_common(BDRV_DRAIN);
392 }
393
394 int main(int argc, char **argv)
395 {
396 bdrv_init();
397 qemu_init_main_loop(&error_abort);
398
399 g_test_init(&argc, &argv, NULL);
400
401 g_test_add_func("/bdrv-drain/driver-cb/drain_all", test_drv_cb_drain_all);
402 g_test_add_func("/bdrv-drain/driver-cb/drain", test_drv_cb_drain);
403
404 g_test_add_func("/bdrv-drain/quiesce/drain_all", test_quiesce_drain_all);
405 g_test_add_func("/bdrv-drain/quiesce/drain", test_quiesce_drain);
406
407 g_test_add_func("/bdrv-drain/nested", test_nested);
408
409 g_test_add_func("/bdrv-drain/blockjob/drain_all", test_blockjob_drain_all);
410 g_test_add_func("/bdrv-drain/blockjob/drain", test_blockjob_drain);
411
412 return g_test_run();
413 }