2 * Blockjob transactions tests
4 * Copyright Red Hat, Inc. 2015
7 * Stefan Hajnoczi <stefanha@redhat.com>
9 * This work is licensed under the terms of the GNU LGPL, version 2 or later.
10 * See the COPYING.LIB file in the top-level directory.
13 #include "qemu/osdep.h"
14 #include "qapi/error.h"
15 #include "qemu/main-loop.h"
16 #include "block/blockjob_int.h"
17 #include "sysemu/block-backend.h"
21 unsigned int iterations
;
27 static void test_block_job_complete(Job
*job
, void *opaque
)
29 BlockJob
*bjob
= container_of(job
, BlockJob
, job
);
30 BlockDriverState
*bs
= blk_bs(bjob
->blk
);
31 int rc
= (intptr_t)opaque
;
33 if (job_is_cancelled(job
)) {
37 block_job_completed(bjob
, rc
);
41 static void coroutine_fn
test_block_job_run(void *opaque
)
43 TestBlockJob
*s
= opaque
;
44 BlockJob
*job
= &s
->common
;
46 while (s
->iterations
--) {
48 job_sleep_ns(&job
->job
, 0);
53 if (job_is_cancelled(&job
->job
)) {
58 job_defer_to_main_loop(&job
->job
, test_block_job_complete
,
59 (void *)(intptr_t)s
->rc
);
67 static void test_block_job_cb(void *opaque
, int ret
)
69 TestBlockJobCBData
*data
= opaque
;
70 if (!ret
&& job_is_cancelled(&data
->job
->common
.job
)) {
77 static const BlockJobDriver test_block_job_driver
= {
79 .instance_size
= sizeof(TestBlockJob
),
80 .free
= block_job_free
,
81 .user_resume
= block_job_user_resume
,
82 .start
= test_block_job_run
,
86 /* Create a block job that completes with a given return code after a given
87 * number of event loop iterations. The return code is stored in the given
90 * The event loop iterations can either be handled automatically with a 0 delay
91 * timer, or they can be stepped manually by entering the coroutine.
93 static BlockJob
*test_block_job_start(unsigned int iterations
,
95 int rc
, int *result
, BlockJobTxn
*txn
)
99 TestBlockJobCBData
*data
;
100 static unsigned counter
;
103 data
= g_new0(TestBlockJobCBData
, 1);
105 bs
= bdrv_open("null-co://", NULL
, NULL
, 0, &error_abort
);
106 g_assert_nonnull(bs
);
108 snprintf(job_id
, sizeof(job_id
), "job%u", counter
++);
109 s
= block_job_create(job_id
, &test_block_job_driver
, txn
, bs
,
110 0, BLK_PERM_ALL
, 0, JOB_DEFAULT
,
111 test_block_job_cb
, data
, &error_abort
);
112 s
->iterations
= iterations
;
113 s
->use_timer
= use_timer
;
117 data
->result
= result
;
121 static void test_single_job(int expected
)
125 int result
= -EINPROGRESS
;
127 txn
= block_job_txn_new();
128 job
= test_block_job_start(1, true, expected
, &result
, txn
);
129 job_start(&job
->job
);
131 if (expected
== -ECANCELED
) {
132 block_job_cancel(job
, false);
135 while (result
== -EINPROGRESS
) {
136 aio_poll(qemu_get_aio_context(), true);
138 g_assert_cmpint(result
, ==, expected
);
140 block_job_txn_unref(txn
);
143 static void test_single_job_success(void)
148 static void test_single_job_failure(void)
150 test_single_job(-EIO
);
153 static void test_single_job_cancel(void)
155 test_single_job(-ECANCELED
);
158 static void test_pair_jobs(int expected1
, int expected2
)
163 int result1
= -EINPROGRESS
;
164 int result2
= -EINPROGRESS
;
166 txn
= block_job_txn_new();
167 job1
= test_block_job_start(1, true, expected1
, &result1
, txn
);
168 job2
= test_block_job_start(2, true, expected2
, &result2
, txn
);
169 job_start(&job1
->job
);
170 job_start(&job2
->job
);
172 /* Release our reference now to trigger as many nice
173 * use-after-free bugs as possible.
175 block_job_txn_unref(txn
);
177 if (expected1
== -ECANCELED
) {
178 block_job_cancel(job1
, false);
180 if (expected2
== -ECANCELED
) {
181 block_job_cancel(job2
, false);
184 while (result1
== -EINPROGRESS
|| result2
== -EINPROGRESS
) {
185 aio_poll(qemu_get_aio_context(), true);
188 /* Failure or cancellation of one job cancels the other job */
189 if (expected1
!= 0) {
190 expected2
= -ECANCELED
;
191 } else if (expected2
!= 0) {
192 expected1
= -ECANCELED
;
195 g_assert_cmpint(result1
, ==, expected1
);
196 g_assert_cmpint(result2
, ==, expected2
);
199 static void test_pair_jobs_success(void)
201 test_pair_jobs(0, 0);
204 static void test_pair_jobs_failure(void)
206 /* Test both orderings. The two jobs run for a different number of
207 * iterations so the code path is different depending on which job fails
210 test_pair_jobs(-EIO
, 0);
211 test_pair_jobs(0, -EIO
);
214 static void test_pair_jobs_cancel(void)
216 test_pair_jobs(-ECANCELED
, 0);
217 test_pair_jobs(0, -ECANCELED
);
220 static void test_pair_jobs_fail_cancel_race(void)
225 int result1
= -EINPROGRESS
;
226 int result2
= -EINPROGRESS
;
228 txn
= block_job_txn_new();
229 job1
= test_block_job_start(1, true, -ECANCELED
, &result1
, txn
);
230 job2
= test_block_job_start(2, false, 0, &result2
, txn
);
231 job_start(&job1
->job
);
232 job_start(&job2
->job
);
234 block_job_cancel(job1
, false);
236 /* Now make job2 finish before the main loop kicks jobs. This simulates
237 * the race between a pending kick and another job completing.
239 block_job_enter(job2
);
240 block_job_enter(job2
);
242 while (result1
== -EINPROGRESS
|| result2
== -EINPROGRESS
) {
243 aio_poll(qemu_get_aio_context(), true);
246 g_assert_cmpint(result1
, ==, -ECANCELED
);
247 g_assert_cmpint(result2
, ==, -ECANCELED
);
249 block_job_txn_unref(txn
);
252 int main(int argc
, char **argv
)
254 qemu_init_main_loop(&error_abort
);
257 g_test_init(&argc
, &argv
, NULL
);
258 g_test_add_func("/single/success", test_single_job_success
);
259 g_test_add_func("/single/failure", test_single_job_failure
);
260 g_test_add_func("/single/cancel", test_single_job_cancel
);
261 g_test_add_func("/pair/success", test_pair_jobs_success
);
262 g_test_add_func("/pair/failure", test_pair_jobs_failure
);
263 g_test_add_func("/pair/cancel", test_pair_jobs_cancel
);
264 g_test_add_func("/pair/fail-cancel-race", test_pair_jobs_fail_cancel_race
);