]> git.proxmox.com Git - mirror_qemu.git/blob - tests/test-blockjob-txn.c
job: Move BlockJobCreateFlags to Job
[mirror_qemu.git] / tests / test-blockjob-txn.c
1 /*
2 * Blockjob transactions tests
3 *
4 * Copyright Red Hat, Inc. 2015
5 *
6 * Authors:
7 * Stefan Hajnoczi <stefanha@redhat.com>
8 *
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.
11 */
12
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
19 typedef struct {
20 BlockJob common;
21 unsigned int iterations;
22 bool use_timer;
23 int rc;
24 int *result;
25 } TestBlockJob;
26
27 static void test_block_job_complete(Job *job, void *opaque)
28 {
29 BlockJob *bjob = container_of(job, BlockJob, job);
30 BlockDriverState *bs = blk_bs(bjob->blk);
31 int rc = (intptr_t)opaque;
32
33 if (job_is_cancelled(job)) {
34 rc = -ECANCELED;
35 }
36
37 block_job_completed(bjob, rc);
38 bdrv_unref(bs);
39 }
40
41 static void coroutine_fn test_block_job_run(void *opaque)
42 {
43 TestBlockJob *s = opaque;
44 BlockJob *job = &s->common;
45
46 while (s->iterations--) {
47 if (s->use_timer) {
48 job_sleep_ns(&job->job, 0);
49 } else {
50 block_job_yield(job);
51 }
52
53 if (job_is_cancelled(&job->job)) {
54 break;
55 }
56 }
57
58 job_defer_to_main_loop(&job->job, test_block_job_complete,
59 (void *)(intptr_t)s->rc);
60 }
61
62 typedef struct {
63 TestBlockJob *job;
64 int *result;
65 } TestBlockJobCBData;
66
67 static void test_block_job_cb(void *opaque, int ret)
68 {
69 TestBlockJobCBData *data = opaque;
70 if (!ret && job_is_cancelled(&data->job->common.job)) {
71 ret = -ECANCELED;
72 }
73 *data->result = ret;
74 g_free(data);
75 }
76
77 static const BlockJobDriver test_block_job_driver = {
78 .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,
83 },
84 };
85
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
88 * result pointer.
89 *
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.
92 */
93 static BlockJob *test_block_job_start(unsigned int iterations,
94 bool use_timer,
95 int rc, int *result, BlockJobTxn *txn)
96 {
97 BlockDriverState *bs;
98 TestBlockJob *s;
99 TestBlockJobCBData *data;
100 static unsigned counter;
101 char job_id[24];
102
103 data = g_new0(TestBlockJobCBData, 1);
104
105 bs = bdrv_open("null-co://", NULL, NULL, 0, &error_abort);
106 g_assert_nonnull(bs);
107
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;
114 s->rc = rc;
115 s->result = result;
116 data->job = s;
117 data->result = result;
118 return &s->common;
119 }
120
121 static void test_single_job(int expected)
122 {
123 BlockJob *job;
124 BlockJobTxn *txn;
125 int result = -EINPROGRESS;
126
127 txn = block_job_txn_new();
128 job = test_block_job_start(1, true, expected, &result, txn);
129 job_start(&job->job);
130
131 if (expected == -ECANCELED) {
132 block_job_cancel(job, false);
133 }
134
135 while (result == -EINPROGRESS) {
136 aio_poll(qemu_get_aio_context(), true);
137 }
138 g_assert_cmpint(result, ==, expected);
139
140 block_job_txn_unref(txn);
141 }
142
143 static void test_single_job_success(void)
144 {
145 test_single_job(0);
146 }
147
148 static void test_single_job_failure(void)
149 {
150 test_single_job(-EIO);
151 }
152
153 static void test_single_job_cancel(void)
154 {
155 test_single_job(-ECANCELED);
156 }
157
158 static void test_pair_jobs(int expected1, int expected2)
159 {
160 BlockJob *job1;
161 BlockJob *job2;
162 BlockJobTxn *txn;
163 int result1 = -EINPROGRESS;
164 int result2 = -EINPROGRESS;
165
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);
171
172 /* Release our reference now to trigger as many nice
173 * use-after-free bugs as possible.
174 */
175 block_job_txn_unref(txn);
176
177 if (expected1 == -ECANCELED) {
178 block_job_cancel(job1, false);
179 }
180 if (expected2 == -ECANCELED) {
181 block_job_cancel(job2, false);
182 }
183
184 while (result1 == -EINPROGRESS || result2 == -EINPROGRESS) {
185 aio_poll(qemu_get_aio_context(), true);
186 }
187
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;
193 }
194
195 g_assert_cmpint(result1, ==, expected1);
196 g_assert_cmpint(result2, ==, expected2);
197 }
198
199 static void test_pair_jobs_success(void)
200 {
201 test_pair_jobs(0, 0);
202 }
203
204 static void test_pair_jobs_failure(void)
205 {
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
208 * first.
209 */
210 test_pair_jobs(-EIO, 0);
211 test_pair_jobs(0, -EIO);
212 }
213
214 static void test_pair_jobs_cancel(void)
215 {
216 test_pair_jobs(-ECANCELED, 0);
217 test_pair_jobs(0, -ECANCELED);
218 }
219
220 static void test_pair_jobs_fail_cancel_race(void)
221 {
222 BlockJob *job1;
223 BlockJob *job2;
224 BlockJobTxn *txn;
225 int result1 = -EINPROGRESS;
226 int result2 = -EINPROGRESS;
227
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);
233
234 block_job_cancel(job1, false);
235
236 /* Now make job2 finish before the main loop kicks jobs. This simulates
237 * the race between a pending kick and another job completing.
238 */
239 block_job_enter(job2);
240 block_job_enter(job2);
241
242 while (result1 == -EINPROGRESS || result2 == -EINPROGRESS) {
243 aio_poll(qemu_get_aio_context(), true);
244 }
245
246 g_assert_cmpint(result1, ==, -ECANCELED);
247 g_assert_cmpint(result2, ==, -ECANCELED);
248
249 block_job_txn_unref(txn);
250 }
251
252 int main(int argc, char **argv)
253 {
254 qemu_init_main_loop(&error_abort);
255 bdrv_init();
256
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);
265 return g_test_run();
266 }