2 * Block node draining tests
4 * Copyright (c) 2017 Kevin Wolf <kwolf@redhat.com>
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:
13 * The above copyright notice and this permission notice shall be included in
14 * all copies or substantial portions of the Software.
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
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"
31 typedef struct BDRVTestState
{
35 static void coroutine_fn
bdrv_test_co_drain_begin(BlockDriverState
*bs
)
37 BDRVTestState
*s
= bs
->opaque
;
41 static void coroutine_fn
bdrv_test_co_drain_end(BlockDriverState
*bs
)
43 BDRVTestState
*s
= bs
->opaque
;
47 static void bdrv_test_close(BlockDriverState
*bs
)
49 BDRVTestState
*s
= bs
->opaque
;
50 g_assert_cmpint(s
->drain_count
, >, 0);
53 static int coroutine_fn
bdrv_test_co_preadv(BlockDriverState
*bs
,
54 uint64_t offset
, uint64_t bytes
,
55 QEMUIOVector
*qiov
, int flags
)
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);
66 static BlockDriver bdrv_test
= {
67 .format_name
= "test",
68 .instance_size
= sizeof(BDRVTestState
),
70 .bdrv_close
= bdrv_test_close
,
71 .bdrv_co_preadv
= bdrv_test_co_preadv
,
73 .bdrv_co_drain_begin
= bdrv_test_co_drain_begin
,
74 .bdrv_co_drain_end
= bdrv_test_co_drain_end
,
76 .bdrv_child_perm
= bdrv_format_default_perms
,
79 static void aio_ret_cb(void *opaque
, int ret
)
81 int *aio_ret
= opaque
;
91 static void do_drain_begin(enum drain_type drain_type
, BlockDriverState
*bs
)
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();
100 static void do_drain_end(enum drain_type drain_type
, BlockDriverState
*bs
)
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();
109 static void test_drv_cb_common(enum drain_type drain_type
, bool recursive
)
112 BlockDriverState
*bs
, *backing
;
113 BDRVTestState
*s
, *backing_s
;
122 qemu_iovec_init_external(&qiov
, &iov
, 1);
124 blk
= blk_new(BLK_PERM_ALL
, BLK_PERM_ALL
);
125 bs
= bdrv_new_open_driver(&bdrv_test
, "test-node", BDRV_O_RDWR
,
128 blk_insert_bs(blk
, bs
, &error_abort
);
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
);
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);
138 do_drain_begin(drain_type
, bs
);
140 g_assert_cmpint(s
->drain_count
, ==, 1);
141 g_assert_cmpint(backing_s
->drain_count
, ==, !!recursive
);
143 do_drain_end(drain_type
, bs
);
145 g_assert_cmpint(s
->drain_count
, ==, 0);
146 g_assert_cmpint(backing_s
->drain_count
, ==, 0);
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
);
154 g_assert_cmpint(s
->drain_count
, ==, 0);
155 g_assert_cmpint(backing_s
->drain_count
, ==, 0);
157 do_drain_begin(drain_type
, bs
);
159 g_assert_cmpint(aio_ret
, ==, 0);
160 g_assert_cmpint(s
->drain_count
, ==, 1);
161 g_assert_cmpint(backing_s
->drain_count
, ==, !!recursive
);
163 do_drain_end(drain_type
, bs
);
165 g_assert_cmpint(s
->drain_count
, ==, 0);
166 g_assert_cmpint(backing_s
->drain_count
, ==, 0);
173 static void test_drv_cb_drain_all(void)
175 test_drv_cb_common(BDRV_DRAIN_ALL
, true);
178 static void test_drv_cb_drain(void)
180 test_drv_cb_common(BDRV_DRAIN
, false);
183 static void test_quiesce_common(enum drain_type drain_type
, bool recursive
)
186 BlockDriverState
*bs
, *backing
;
188 blk
= blk_new(BLK_PERM_ALL
, BLK_PERM_ALL
);
189 bs
= bdrv_new_open_driver(&bdrv_test
, "test-node", BDRV_O_RDWR
,
191 blk_insert_bs(blk
, bs
, &error_abort
);
193 backing
= bdrv_new_open_driver(&bdrv_test
, "backing", 0, &error_abort
);
194 bdrv_set_backing_hd(bs
, backing
, &error_abort
);
196 g_assert_cmpint(bs
->quiesce_counter
, ==, 0);
197 g_assert_cmpint(backing
->quiesce_counter
, ==, 0);
199 do_drain_begin(drain_type
, bs
);
201 g_assert_cmpint(bs
->quiesce_counter
, ==, 1);
202 g_assert_cmpint(backing
->quiesce_counter
, ==, !!recursive
);
204 do_drain_end(drain_type
, bs
);
206 g_assert_cmpint(bs
->quiesce_counter
, ==, 0);
207 g_assert_cmpint(backing
->quiesce_counter
, ==, 0);
214 static void test_quiesce_drain_all(void)
216 // XXX drain_all doesn't quiesce
217 //test_quiesce_common(BDRV_DRAIN_ALL, true);
220 static void test_quiesce_drain(void)
222 test_quiesce_common(BDRV_DRAIN
, false);
225 static void test_nested(void)
228 BlockDriverState
*bs
, *backing
;
229 BDRVTestState
*s
, *backing_s
;
230 enum drain_type outer
, inner
;
232 blk
= blk_new(BLK_PERM_ALL
, BLK_PERM_ALL
);
233 bs
= bdrv_new_open_driver(&bdrv_test
, "test-node", BDRV_O_RDWR
,
236 blk_insert_bs(blk
, bs
, &error_abort
);
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
);
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
);
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);
256 do_drain_begin(outer
, bs
);
257 do_drain_begin(inner
, bs
);
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
);
264 do_drain_end(inner
, bs
);
265 do_drain_end(outer
, bs
);
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);
280 typedef struct TestBlockJob
{
282 bool should_complete
;
285 static void test_job_completed(BlockJob
*job
, void *opaque
)
287 block_job_completed(job
, 0);
290 static void coroutine_fn
test_job_start(void *opaque
)
292 TestBlockJob
*s
= opaque
;
294 while (!s
->should_complete
) {
295 block_job_sleep_ns(&s
->common
, 100000);
298 block_job_defer_to_main_loop(&s
->common
, test_job_completed
, NULL
);
301 static void test_job_complete(BlockJob
*job
, Error
**errp
)
303 TestBlockJob
*s
= container_of(job
, TestBlockJob
, common
);
304 s
->should_complete
= true;
307 BlockJobDriver test_job_driver
= {
308 .instance_size
= sizeof(TestBlockJob
),
309 .start
= test_job_start
,
310 .complete
= test_job_complete
,
313 static void test_blockjob_common(enum drain_type drain_type
)
315 BlockBackend
*blk_src
, *blk_target
;
316 BlockDriverState
*src
, *target
;
320 src
= bdrv_new_open_driver(&bdrv_test
, "source", BDRV_O_RDWR
,
322 blk_src
= blk_new(BLK_PERM_ALL
, BLK_PERM_ALL
);
323 blk_insert_bs(blk_src
, src
, &error_abort
);
325 target
= bdrv_new_open_driver(&bdrv_test
, "target", BDRV_O_RDWR
,
327 blk_target
= blk_new(BLK_PERM_ALL
, BLK_PERM_ALL
);
328 blk_insert_bs(blk_target
, target
, &error_abort
);
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
);
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() */
339 do_drain_begin(drain_type
, src
);
341 if (drain_type
== BDRV_DRAIN_ALL
) {
342 /* bdrv_drain_all() drains both src and target */
343 g_assert_cmpint(job
->pause_count
, ==, 2);
345 g_assert_cmpint(job
->pause_count
, ==, 1);
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 */
351 do_drain_end(drain_type
, src
);
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() */
357 do_drain_begin(drain_type
, target
);
359 if (drain_type
== BDRV_DRAIN_ALL
) {
360 /* bdrv_drain_all() drains both src and target */
361 g_assert_cmpint(job
->pause_count
, ==, 2);
363 g_assert_cmpint(job
->pause_count
, ==, 1);
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 */
369 do_drain_end(drain_type
, target
);
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() */
375 ret
= block_job_complete_sync(job
, &error_abort
);
376 g_assert_cmpint(ret
, ==, 0);
379 blk_unref(blk_target
);
384 static void test_blockjob_drain_all(void)
386 test_blockjob_common(BDRV_DRAIN_ALL
);
389 static void test_blockjob_drain(void)
391 test_blockjob_common(BDRV_DRAIN
);
394 int main(int argc
, char **argv
)
397 qemu_init_main_loop(&error_abort
);
399 g_test_init(&argc
, &argv
, NULL
);
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
);
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
);
407 g_test_add_func("/bdrv-drain/nested", test_nested
);
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
);