]> git.proxmox.com Git - mirror_qemu.git/blame - tests/test-bdrv-drain.c
block: Nested drain_end must still call callbacks
[mirror_qemu.git] / tests / test-bdrv-drain.c
CommitLineData
881cfd17
KW
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"
7253220d 27#include "block/blockjob_int.h"
881cfd17
KW
28#include "sysemu/block-backend.h"
29#include "qapi/error.h"
30
31typedef struct BDRVTestState {
32 int drain_count;
33} BDRVTestState;
34
35static void coroutine_fn bdrv_test_co_drain_begin(BlockDriverState *bs)
36{
37 BDRVTestState *s = bs->opaque;
38 s->drain_count++;
39}
40
41static void coroutine_fn bdrv_test_co_drain_end(BlockDriverState *bs)
42{
43 BDRVTestState *s = bs->opaque;
44 s->drain_count--;
45}
46
47static void bdrv_test_close(BlockDriverState *bs)
48{
49 BDRVTestState *s = bs->opaque;
50 g_assert_cmpint(s->drain_count, >, 0);
51}
52
53static 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
66static 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,
86e1c840
KW
75
76 .bdrv_child_perm = bdrv_format_default_perms,
881cfd17
KW
77};
78
79static void aio_ret_cb(void *opaque, int ret)
80{
81 int *aio_ret = opaque;
82 *aio_ret = ret;
83}
84
86e1c840
KW
85enum drain_type {
86 BDRV_DRAIN_ALL,
87 BDRV_DRAIN,
88};
89
90static void do_drain_begin(enum drain_type drain_type, BlockDriverState *bs)
91{
92 switch (drain_type) {
93 case BDRV_DRAIN_ALL: bdrv_drain_all_begin(); break;
94 case BDRV_DRAIN: bdrv_drained_begin(bs); break;
95 default: g_assert_not_reached();
96 }
97}
98
99static void do_drain_end(enum drain_type drain_type, BlockDriverState *bs)
100{
101 switch (drain_type) {
102 case BDRV_DRAIN_ALL: bdrv_drain_all_end(); break;
103 case BDRV_DRAIN: bdrv_drained_end(bs); break;
104 default: g_assert_not_reached();
105 }
106}
107
108static void test_drv_cb_common(enum drain_type drain_type, bool recursive)
881cfd17
KW
109{
110 BlockBackend *blk;
86e1c840
KW
111 BlockDriverState *bs, *backing;
112 BDRVTestState *s, *backing_s;
881cfd17
KW
113 BlockAIOCB *acb;
114 int aio_ret;
115
116 QEMUIOVector qiov;
117 struct iovec iov = {
118 .iov_base = NULL,
119 .iov_len = 0,
120 };
121 qemu_iovec_init_external(&qiov, &iov, 1);
122
123 blk = blk_new(BLK_PERM_ALL, BLK_PERM_ALL);
124 bs = bdrv_new_open_driver(&bdrv_test, "test-node", BDRV_O_RDWR,
125 &error_abort);
126 s = bs->opaque;
127 blk_insert_bs(blk, bs, &error_abort);
128
86e1c840
KW
129 backing = bdrv_new_open_driver(&bdrv_test, "backing", 0, &error_abort);
130 backing_s = backing->opaque;
131 bdrv_set_backing_hd(bs, backing, &error_abort);
132
881cfd17
KW
133 /* Simple bdrv_drain_all_begin/end pair, check that CBs are called */
134 g_assert_cmpint(s->drain_count, ==, 0);
86e1c840
KW
135 g_assert_cmpint(backing_s->drain_count, ==, 0);
136
137 do_drain_begin(drain_type, bs);
138
881cfd17 139 g_assert_cmpint(s->drain_count, ==, 1);
86e1c840
KW
140 g_assert_cmpint(backing_s->drain_count, ==, !!recursive);
141
142 do_drain_end(drain_type, bs);
143
881cfd17 144 g_assert_cmpint(s->drain_count, ==, 0);
86e1c840 145 g_assert_cmpint(backing_s->drain_count, ==, 0);
881cfd17
KW
146
147 /* Now do the same while a request is pending */
148 aio_ret = -EINPROGRESS;
149 acb = blk_aio_preadv(blk, 0, &qiov, 0, aio_ret_cb, &aio_ret);
150 g_assert(acb != NULL);
151 g_assert_cmpint(aio_ret, ==, -EINPROGRESS);
152
153 g_assert_cmpint(s->drain_count, ==, 0);
86e1c840
KW
154 g_assert_cmpint(backing_s->drain_count, ==, 0);
155
156 do_drain_begin(drain_type, bs);
157
881cfd17
KW
158 g_assert_cmpint(aio_ret, ==, 0);
159 g_assert_cmpint(s->drain_count, ==, 1);
86e1c840
KW
160 g_assert_cmpint(backing_s->drain_count, ==, !!recursive);
161
162 do_drain_end(drain_type, bs);
163
881cfd17 164 g_assert_cmpint(s->drain_count, ==, 0);
86e1c840 165 g_assert_cmpint(backing_s->drain_count, ==, 0);
881cfd17 166
86e1c840 167 bdrv_unref(backing);
881cfd17
KW
168 bdrv_unref(bs);
169 blk_unref(blk);
170}
171
86e1c840
KW
172static void test_drv_cb_drain_all(void)
173{
174 test_drv_cb_common(BDRV_DRAIN_ALL, true);
175}
176
177static void test_drv_cb_drain(void)
178{
179 test_drv_cb_common(BDRV_DRAIN, false);
180}
181
89a6ceab
KW
182static void test_quiesce_common(enum drain_type drain_type, bool recursive)
183{
184 BlockBackend *blk;
185 BlockDriverState *bs, *backing;
186
187 blk = blk_new(BLK_PERM_ALL, BLK_PERM_ALL);
188 bs = bdrv_new_open_driver(&bdrv_test, "test-node", BDRV_O_RDWR,
189 &error_abort);
190 blk_insert_bs(blk, bs, &error_abort);
191
192 backing = bdrv_new_open_driver(&bdrv_test, "backing", 0, &error_abort);
193 bdrv_set_backing_hd(bs, backing, &error_abort);
194
195 g_assert_cmpint(bs->quiesce_counter, ==, 0);
196 g_assert_cmpint(backing->quiesce_counter, ==, 0);
197
198 do_drain_begin(drain_type, bs);
199
200 g_assert_cmpint(bs->quiesce_counter, ==, 1);
201 g_assert_cmpint(backing->quiesce_counter, ==, !!recursive);
202
203 do_drain_end(drain_type, bs);
204
205 g_assert_cmpint(bs->quiesce_counter, ==, 0);
206 g_assert_cmpint(backing->quiesce_counter, ==, 0);
207
208 bdrv_unref(backing);
209 bdrv_unref(bs);
210 blk_unref(blk);
211}
212
213static void test_quiesce_drain_all(void)
214{
215 // XXX drain_all doesn't quiesce
216 //test_quiesce_common(BDRV_DRAIN_ALL, true);
217}
218
219static void test_quiesce_drain(void)
220{
221 test_quiesce_common(BDRV_DRAIN, false);
222}
223
7253220d
KW
224
225typedef struct TestBlockJob {
226 BlockJob common;
227 bool should_complete;
228} TestBlockJob;
229
230static void test_job_completed(BlockJob *job, void *opaque)
231{
232 block_job_completed(job, 0);
233}
234
235static void coroutine_fn test_job_start(void *opaque)
236{
237 TestBlockJob *s = opaque;
238
239 while (!s->should_complete) {
240 block_job_sleep_ns(&s->common, 100000);
241 }
242
243 block_job_defer_to_main_loop(&s->common, test_job_completed, NULL);
244}
245
246static void test_job_complete(BlockJob *job, Error **errp)
247{
248 TestBlockJob *s = container_of(job, TestBlockJob, common);
249 s->should_complete = true;
250}
251
252BlockJobDriver test_job_driver = {
253 .instance_size = sizeof(TestBlockJob),
254 .start = test_job_start,
255 .complete = test_job_complete,
256};
257
258static void test_blockjob_common(enum drain_type drain_type)
259{
260 BlockBackend *blk_src, *blk_target;
261 BlockDriverState *src, *target;
262 BlockJob *job;
263 int ret;
264
265 src = bdrv_new_open_driver(&bdrv_test, "source", BDRV_O_RDWR,
266 &error_abort);
267 blk_src = blk_new(BLK_PERM_ALL, BLK_PERM_ALL);
268 blk_insert_bs(blk_src, src, &error_abort);
269
270 target = bdrv_new_open_driver(&bdrv_test, "target", BDRV_O_RDWR,
271 &error_abort);
272 blk_target = blk_new(BLK_PERM_ALL, BLK_PERM_ALL);
273 blk_insert_bs(blk_target, target, &error_abort);
274
275 job = block_job_create("job0", &test_job_driver, src, 0, BLK_PERM_ALL, 0,
276 0, NULL, NULL, &error_abort);
277 block_job_add_bdrv(job, "target", target, 0, BLK_PERM_ALL, &error_abort);
278 block_job_start(job);
279
280 g_assert_cmpint(job->pause_count, ==, 0);
281 g_assert_false(job->paused);
282 g_assert_false(job->busy); /* We're in block_job_sleep_ns() */
283
284 do_drain_begin(drain_type, src);
285
286 if (drain_type == BDRV_DRAIN_ALL) {
81193349
KW
287 /* bdrv_drain_all() drains both src and target */
288 g_assert_cmpint(job->pause_count, ==, 2);
7253220d
KW
289 } else {
290 g_assert_cmpint(job->pause_count, ==, 1);
291 }
292 /* XXX We don't wait until the job is actually paused. Is this okay? */
293 /* g_assert_true(job->paused); */
294 g_assert_false(job->busy); /* The job is paused */
295
296 do_drain_end(drain_type, src);
297
298 g_assert_cmpint(job->pause_count, ==, 0);
299 g_assert_false(job->paused);
300 g_assert_false(job->busy); /* We're in block_job_sleep_ns() */
301
302 do_drain_begin(drain_type, target);
303
304 if (drain_type == BDRV_DRAIN_ALL) {
81193349
KW
305 /* bdrv_drain_all() drains both src and target */
306 g_assert_cmpint(job->pause_count, ==, 2);
7253220d
KW
307 } else {
308 g_assert_cmpint(job->pause_count, ==, 1);
309 }
310 /* XXX We don't wait until the job is actually paused. Is this okay? */
311 /* g_assert_true(job->paused); */
312 g_assert_false(job->busy); /* The job is paused */
313
314 do_drain_end(drain_type, target);
315
316 g_assert_cmpint(job->pause_count, ==, 0);
317 g_assert_false(job->paused);
318 g_assert_false(job->busy); /* We're in block_job_sleep_ns() */
319
320 ret = block_job_complete_sync(job, &error_abort);
321 g_assert_cmpint(ret, ==, 0);
322
323 blk_unref(blk_src);
324 blk_unref(blk_target);
325 bdrv_unref(src);
326 bdrv_unref(target);
327}
328
329static void test_blockjob_drain_all(void)
330{
331 test_blockjob_common(BDRV_DRAIN_ALL);
332}
333
334static void test_blockjob_drain(void)
335{
336 test_blockjob_common(BDRV_DRAIN);
337}
338
881cfd17
KW
339int main(int argc, char **argv)
340{
341 bdrv_init();
342 qemu_init_main_loop(&error_abort);
343
344 g_test_init(&argc, &argv, NULL);
345
346 g_test_add_func("/bdrv-drain/driver-cb/drain_all", test_drv_cb_drain_all);
86e1c840 347 g_test_add_func("/bdrv-drain/driver-cb/drain", test_drv_cb_drain);
881cfd17 348
89a6ceab
KW
349 g_test_add_func("/bdrv-drain/quiesce/drain_all", test_quiesce_drain_all);
350 g_test_add_func("/bdrv-drain/quiesce/drain", test_quiesce_drain);
351
7253220d
KW
352 g_test_add_func("/bdrv-drain/blockjob/drain_all", test_blockjob_drain_all);
353 g_test_add_func("/bdrv-drain/blockjob/drain", test_blockjob_drain);
354
881cfd17
KW
355 return g_test_run();
356}