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"
18 #include "qapi/qmp/qdict.h"
22 unsigned int iterations
;
28 static void test_block_job_clean(Job
*job
)
30 BlockJob
*bjob
= container_of(job
, BlockJob
, job
);
31 BlockDriverState
*bs
= blk_bs(bjob
->blk
);
36 static int coroutine_fn
test_block_job_run(Job
*job
, Error
**errp
)
38 TestBlockJob
*s
= container_of(job
, TestBlockJob
, common
.job
);
40 while (s
->iterations
--) {
47 if (job_is_cancelled(job
)) {
60 static void test_block_job_cb(void *opaque
, int ret
)
62 TestBlockJobCBData
*data
= opaque
;
63 if (!ret
&& job_is_cancelled(&data
->job
->common
.job
)) {
70 static const BlockJobDriver test_block_job_driver
= {
72 .instance_size
= sizeof(TestBlockJob
),
73 .free
= block_job_free
,
74 .user_resume
= block_job_user_resume
,
75 .drain
= block_job_drain
,
76 .run
= test_block_job_run
,
77 .clean
= test_block_job_clean
,
81 /* Create a block job that completes with a given return code after a given
82 * number of event loop iterations. The return code is stored in the given
85 * The event loop iterations can either be handled automatically with a 0 delay
86 * timer, or they can be stepped manually by entering the coroutine.
88 static BlockJob
*test_block_job_start(unsigned int iterations
,
90 int rc
, int *result
, JobTxn
*txn
)
94 TestBlockJobCBData
*data
;
95 static unsigned counter
;
98 data
= g_new0(TestBlockJobCBData
, 1);
100 QDict
*opt
= qdict_new();
101 qdict_put_str(opt
, "file.read-zeroes", "on");
102 bs
= bdrv_open("null-co://", NULL
, opt
, 0, &error_abort
);
103 g_assert_nonnull(bs
);
105 snprintf(job_id
, sizeof(job_id
), "job%u", counter
++);
106 s
= block_job_create(job_id
, &test_block_job_driver
, txn
, bs
,
107 0, BLK_PERM_ALL
, 0, JOB_DEFAULT
,
108 test_block_job_cb
, data
, &error_abort
);
109 s
->iterations
= iterations
;
110 s
->use_timer
= use_timer
;
114 data
->result
= result
;
118 static void test_single_job(int expected
)
122 int result
= -EINPROGRESS
;
125 job
= test_block_job_start(1, true, expected
, &result
, txn
);
126 job_start(&job
->job
);
128 if (expected
== -ECANCELED
) {
129 job_cancel(&job
->job
, false);
132 while (result
== -EINPROGRESS
) {
133 aio_poll(qemu_get_aio_context(), true);
135 g_assert_cmpint(result
, ==, expected
);
140 static void test_single_job_success(void)
145 static void test_single_job_failure(void)
147 test_single_job(-EIO
);
150 static void test_single_job_cancel(void)
152 test_single_job(-ECANCELED
);
155 static void test_pair_jobs(int expected1
, int expected2
)
160 int result1
= -EINPROGRESS
;
161 int result2
= -EINPROGRESS
;
164 job1
= test_block_job_start(1, true, expected1
, &result1
, txn
);
165 job2
= test_block_job_start(2, true, expected2
, &result2
, txn
);
166 job_start(&job1
->job
);
167 job_start(&job2
->job
);
169 /* Release our reference now to trigger as many nice
170 * use-after-free bugs as possible.
174 if (expected1
== -ECANCELED
) {
175 job_cancel(&job1
->job
, false);
177 if (expected2
== -ECANCELED
) {
178 job_cancel(&job2
->job
, false);
181 while (result1
== -EINPROGRESS
|| result2
== -EINPROGRESS
) {
182 aio_poll(qemu_get_aio_context(), true);
185 /* Failure or cancellation of one job cancels the other job */
186 if (expected1
!= 0) {
187 expected2
= -ECANCELED
;
188 } else if (expected2
!= 0) {
189 expected1
= -ECANCELED
;
192 g_assert_cmpint(result1
, ==, expected1
);
193 g_assert_cmpint(result2
, ==, expected2
);
196 static void test_pair_jobs_success(void)
198 test_pair_jobs(0, 0);
201 static void test_pair_jobs_failure(void)
203 /* Test both orderings. The two jobs run for a different number of
204 * iterations so the code path is different depending on which job fails
207 test_pair_jobs(-EIO
, 0);
208 test_pair_jobs(0, -EIO
);
211 static void test_pair_jobs_cancel(void)
213 test_pair_jobs(-ECANCELED
, 0);
214 test_pair_jobs(0, -ECANCELED
);
217 static void test_pair_jobs_fail_cancel_race(void)
222 int result1
= -EINPROGRESS
;
223 int result2
= -EINPROGRESS
;
226 job1
= test_block_job_start(1, true, -ECANCELED
, &result1
, txn
);
227 job2
= test_block_job_start(2, false, 0, &result2
, txn
);
228 job_start(&job1
->job
);
229 job_start(&job2
->job
);
231 job_cancel(&job1
->job
, false);
233 /* Now make job2 finish before the main loop kicks jobs. This simulates
234 * the race between a pending kick and another job completing.
236 job_enter(&job2
->job
);
237 job_enter(&job2
->job
);
239 while (result1
== -EINPROGRESS
|| result2
== -EINPROGRESS
) {
240 aio_poll(qemu_get_aio_context(), true);
243 g_assert_cmpint(result1
, ==, -ECANCELED
);
244 g_assert_cmpint(result2
, ==, -ECANCELED
);
249 int main(int argc
, char **argv
)
251 qemu_init_main_loop(&error_abort
);
254 g_test_init(&argc
, &argv
, NULL
);
255 g_test_add_func("/single/success", test_single_job_success
);
256 g_test_add_func("/single/failure", test_single_job_failure
);
257 g_test_add_func("/single/cancel", test_single_job_cancel
);
258 g_test_add_func("/pair/success", test_pair_jobs_success
);
259 g_test_add_func("/pair/failure", test_pair_jobs_failure
);
260 g_test_add_func("/pair/cancel", test_pair_jobs_cancel
);
261 g_test_add_func("/pair/fail-cancel-race", test_pair_jobs_fail_cancel_race
);