]>
Commit | Line | Data |
---|---|---|
6c6f312d SH |
1 | /* |
2 | * Blockjob transactions tests | |
3 | * | |
4 | * Copyright Red Hat, Inc. 2015 | |
5 | * | |
6 | * Authors: | |
7 | * Stefan Hajnoczi <[email protected]> | |
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 | ||
681c28a3 | 13 | #include "qemu/osdep.h" |
da34e65c | 14 | #include "qapi/error.h" |
6c6f312d | 15 | #include "qemu/main-loop.h" |
c87621ea | 16 | #include "block/blockjob_int.h" |
b75536c9 | 17 | #include "sysemu/block-backend.h" |
6c6f312d SH |
18 | |
19 | typedef struct { | |
20 | BlockJob common; | |
21 | unsigned int iterations; | |
22 | bool use_timer; | |
23 | int rc; | |
24 | int *result; | |
25 | } TestBlockJob; | |
26 | ||
e4dad427 | 27 | static void test_block_job_clean(Job *job) |
6c6f312d | 28 | { |
1908a559 KW |
29 | BlockJob *bjob = container_of(job, BlockJob, job); |
30 | BlockDriverState *bs = blk_bs(bjob->blk); | |
6c6f312d | 31 | |
6c6f312d SH |
32 | bdrv_unref(bs); |
33 | } | |
34 | ||
f67432a2 | 35 | static int coroutine_fn test_block_job_run(Job *job, Error **errp) |
6c6f312d | 36 | { |
f67432a2 | 37 | TestBlockJob *s = container_of(job, TestBlockJob, common.job); |
6c6f312d SH |
38 | |
39 | while (s->iterations--) { | |
40 | if (s->use_timer) { | |
f67432a2 | 41 | job_sleep_ns(job, 0); |
6c6f312d | 42 | } else { |
f67432a2 | 43 | job_yield(job); |
6c6f312d SH |
44 | } |
45 | ||
f67432a2 | 46 | if (job_is_cancelled(job)) { |
6c6f312d SH |
47 | break; |
48 | } | |
49 | } | |
50 | ||
f67432a2 | 51 | return s->rc; |
6c6f312d SH |
52 | } |
53 | ||
54 | typedef struct { | |
55 | TestBlockJob *job; | |
56 | int *result; | |
57 | } TestBlockJobCBData; | |
58 | ||
59 | static void test_block_job_cb(void *opaque, int ret) | |
60 | { | |
61 | TestBlockJobCBData *data = opaque; | |
daa7f2f9 | 62 | if (!ret && job_is_cancelled(&data->job->common.job)) { |
6c6f312d SH |
63 | ret = -ECANCELED; |
64 | } | |
65 | *data->result = ret; | |
66 | g_free(data); | |
67 | } | |
68 | ||
5ccac6f1 | 69 | static const BlockJobDriver test_block_job_driver = { |
33e9e9bd KW |
70 | .job_driver = { |
71 | .instance_size = sizeof(TestBlockJob), | |
80fa2c75 | 72 | .free = block_job_free, |
b15de828 | 73 | .user_resume = block_job_user_resume, |
b69f777d | 74 | .drain = block_job_drain, |
f67432a2 | 75 | .run = test_block_job_run, |
e4dad427 | 76 | .clean = test_block_job_clean, |
33e9e9bd | 77 | }, |
5ccac6f1 JS |
78 | }; |
79 | ||
6c6f312d SH |
80 | /* Create a block job that completes with a given return code after a given |
81 | * number of event loop iterations. The return code is stored in the given | |
82 | * result pointer. | |
83 | * | |
84 | * The event loop iterations can either be handled automatically with a 0 delay | |
85 | * timer, or they can be stepped manually by entering the coroutine. | |
86 | */ | |
87 | static BlockJob *test_block_job_start(unsigned int iterations, | |
88 | bool use_timer, | |
62c9e416 | 89 | int rc, int *result, JobTxn *txn) |
6c6f312d SH |
90 | { |
91 | BlockDriverState *bs; | |
92 | TestBlockJob *s; | |
93 | TestBlockJobCBData *data; | |
7f0317cf AG |
94 | static unsigned counter; |
95 | char job_id[24]; | |
6c6f312d SH |
96 | |
97 | data = g_new0(TestBlockJobCBData, 1); | |
d185cf0e KW |
98 | |
99 | bs = bdrv_open("null-co://", NULL, NULL, 0, &error_abort); | |
100 | g_assert_nonnull(bs); | |
101 | ||
7f0317cf | 102 | snprintf(job_id, sizeof(job_id), "job%u", counter++); |
75859b94 | 103 | s = block_job_create(job_id, &test_block_job_driver, txn, bs, |
bb02b65c | 104 | 0, BLK_PERM_ALL, 0, JOB_DEFAULT, |
c6cc12bf | 105 | test_block_job_cb, data, &error_abort); |
6c6f312d SH |
106 | s->iterations = iterations; |
107 | s->use_timer = use_timer; | |
108 | s->rc = rc; | |
109 | s->result = result; | |
6c6f312d SH |
110 | data->job = s; |
111 | data->result = result; | |
6c6f312d SH |
112 | return &s->common; |
113 | } | |
114 | ||
115 | static void test_single_job(int expected) | |
116 | { | |
117 | BlockJob *job; | |
62c9e416 | 118 | JobTxn *txn; |
6c6f312d SH |
119 | int result = -EINPROGRESS; |
120 | ||
7eaa8fb5 | 121 | txn = job_txn_new(); |
75859b94 | 122 | job = test_block_job_start(1, true, expected, &result, txn); |
da01ff7f | 123 | job_start(&job->job); |
6c6f312d SH |
124 | |
125 | if (expected == -ECANCELED) { | |
3d70ff53 | 126 | job_cancel(&job->job, false); |
6c6f312d SH |
127 | } |
128 | ||
129 | while (result == -EINPROGRESS) { | |
130 | aio_poll(qemu_get_aio_context(), true); | |
131 | } | |
132 | g_assert_cmpint(result, ==, expected); | |
133 | ||
7eaa8fb5 | 134 | job_txn_unref(txn); |
6c6f312d SH |
135 | } |
136 | ||
137 | static void test_single_job_success(void) | |
138 | { | |
139 | test_single_job(0); | |
140 | } | |
141 | ||
142 | static void test_single_job_failure(void) | |
143 | { | |
144 | test_single_job(-EIO); | |
145 | } | |
146 | ||
147 | static void test_single_job_cancel(void) | |
148 | { | |
149 | test_single_job(-ECANCELED); | |
150 | } | |
151 | ||
152 | static void test_pair_jobs(int expected1, int expected2) | |
153 | { | |
154 | BlockJob *job1; | |
155 | BlockJob *job2; | |
62c9e416 | 156 | JobTxn *txn; |
6c6f312d SH |
157 | int result1 = -EINPROGRESS; |
158 | int result2 = -EINPROGRESS; | |
159 | ||
7eaa8fb5 | 160 | txn = job_txn_new(); |
75859b94 JS |
161 | job1 = test_block_job_start(1, true, expected1, &result1, txn); |
162 | job2 = test_block_job_start(2, true, expected2, &result2, txn); | |
da01ff7f KW |
163 | job_start(&job1->job); |
164 | job_start(&job2->job); | |
6c6f312d | 165 | |
7e74a734 PB |
166 | /* Release our reference now to trigger as many nice |
167 | * use-after-free bugs as possible. | |
168 | */ | |
7eaa8fb5 | 169 | job_txn_unref(txn); |
7e74a734 | 170 | |
6c6f312d | 171 | if (expected1 == -ECANCELED) { |
3d70ff53 | 172 | job_cancel(&job1->job, false); |
6c6f312d SH |
173 | } |
174 | if (expected2 == -ECANCELED) { | |
3d70ff53 | 175 | job_cancel(&job2->job, false); |
6c6f312d SH |
176 | } |
177 | ||
178 | while (result1 == -EINPROGRESS || result2 == -EINPROGRESS) { | |
179 | aio_poll(qemu_get_aio_context(), true); | |
180 | } | |
181 | ||
182 | /* Failure or cancellation of one job cancels the other job */ | |
183 | if (expected1 != 0) { | |
184 | expected2 = -ECANCELED; | |
185 | } else if (expected2 != 0) { | |
186 | expected1 = -ECANCELED; | |
187 | } | |
188 | ||
189 | g_assert_cmpint(result1, ==, expected1); | |
190 | g_assert_cmpint(result2, ==, expected2); | |
6c6f312d SH |
191 | } |
192 | ||
193 | static void test_pair_jobs_success(void) | |
194 | { | |
195 | test_pair_jobs(0, 0); | |
196 | } | |
197 | ||
198 | static void test_pair_jobs_failure(void) | |
199 | { | |
200 | /* Test both orderings. The two jobs run for a different number of | |
201 | * iterations so the code path is different depending on which job fails | |
202 | * first. | |
203 | */ | |
204 | test_pair_jobs(-EIO, 0); | |
205 | test_pair_jobs(0, -EIO); | |
206 | } | |
207 | ||
208 | static void test_pair_jobs_cancel(void) | |
209 | { | |
210 | test_pair_jobs(-ECANCELED, 0); | |
211 | test_pair_jobs(0, -ECANCELED); | |
212 | } | |
213 | ||
214 | static void test_pair_jobs_fail_cancel_race(void) | |
215 | { | |
216 | BlockJob *job1; | |
217 | BlockJob *job2; | |
62c9e416 | 218 | JobTxn *txn; |
6c6f312d SH |
219 | int result1 = -EINPROGRESS; |
220 | int result2 = -EINPROGRESS; | |
221 | ||
7eaa8fb5 | 222 | txn = job_txn_new(); |
75859b94 JS |
223 | job1 = test_block_job_start(1, true, -ECANCELED, &result1, txn); |
224 | job2 = test_block_job_start(2, false, 0, &result2, txn); | |
da01ff7f KW |
225 | job_start(&job1->job); |
226 | job_start(&job2->job); | |
6c6f312d | 227 | |
3d70ff53 | 228 | job_cancel(&job1->job, false); |
6c6f312d SH |
229 | |
230 | /* Now make job2 finish before the main loop kicks jobs. This simulates | |
231 | * the race between a pending kick and another job completing. | |
232 | */ | |
3d70ff53 KW |
233 | job_enter(&job2->job); |
234 | job_enter(&job2->job); | |
6c6f312d SH |
235 | |
236 | while (result1 == -EINPROGRESS || result2 == -EINPROGRESS) { | |
237 | aio_poll(qemu_get_aio_context(), true); | |
238 | } | |
239 | ||
240 | g_assert_cmpint(result1, ==, -ECANCELED); | |
241 | g_assert_cmpint(result2, ==, -ECANCELED); | |
242 | ||
7eaa8fb5 | 243 | job_txn_unref(txn); |
6c6f312d SH |
244 | } |
245 | ||
246 | int main(int argc, char **argv) | |
247 | { | |
248 | qemu_init_main_loop(&error_abort); | |
d185cf0e | 249 | bdrv_init(); |
6c6f312d SH |
250 | |
251 | g_test_init(&argc, &argv, NULL); | |
252 | g_test_add_func("/single/success", test_single_job_success); | |
253 | g_test_add_func("/single/failure", test_single_job_failure); | |
254 | g_test_add_func("/single/cancel", test_single_job_cancel); | |
255 | g_test_add_func("/pair/success", test_pair_jobs_success); | |
256 | g_test_add_func("/pair/failure", test_pair_jobs_failure); | |
257 | g_test_add_func("/pair/cancel", test_pair_jobs_cancel); | |
258 | g_test_add_func("/pair/fail-cancel-race", test_pair_jobs_fail_cancel_race); | |
259 | return g_test_run(); | |
260 | } |