]>
Commit | Line | Data |
---|---|---|
9ef8112a AG |
1 | /* |
2 | * Blockjob tests | |
3 | * | |
4 | * Copyright Igalia, S.L. 2016 | |
5 | * | |
6 | * Authors: | |
7 | * Alberto Garcia <[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 | ||
13 | #include "qemu/osdep.h" | |
14 | #include "qapi/error.h" | |
15 | #include "qemu/main-loop.h" | |
c87621ea | 16 | #include "block/blockjob_int.h" |
9ef8112a AG |
17 | #include "sysemu/block-backend.h" |
18 | ||
19 | static const BlockJobDriver test_block_job_driver = { | |
33e9e9bd KW |
20 | .job_driver = { |
21 | .instance_size = sizeof(BlockJob), | |
80fa2c75 | 22 | .free = block_job_free, |
b15de828 | 23 | .user_resume = block_job_user_resume, |
b69f777d | 24 | .drain = block_job_drain, |
33e9e9bd | 25 | }, |
9ef8112a AG |
26 | }; |
27 | ||
28 | static void block_job_cb(void *opaque, int ret) | |
29 | { | |
30 | } | |
31 | ||
fb367e03 JS |
32 | static BlockJob *mk_job(BlockBackend *blk, const char *id, |
33 | const BlockJobDriver *drv, bool should_succeed, | |
34 | int flags) | |
9ef8112a AG |
35 | { |
36 | BlockJob *job; | |
37 | Error *errp = NULL; | |
38 | ||
fb367e03 JS |
39 | job = block_job_create(id, drv, NULL, blk_bs(blk), |
40 | 0, BLK_PERM_ALL, 0, flags, block_job_cb, | |
c6cc12bf | 41 | NULL, &errp); |
9ef8112a AG |
42 | if (should_succeed) { |
43 | g_assert_null(errp); | |
44 | g_assert_nonnull(job); | |
45 | if (id) { | |
33e9e9bd | 46 | g_assert_cmpstr(job->job.id, ==, id); |
9ef8112a | 47 | } else { |
33e9e9bd | 48 | g_assert_cmpstr(job->job.id, ==, blk_name(blk)); |
9ef8112a AG |
49 | } |
50 | } else { | |
51 | g_assert_nonnull(errp); | |
52 | g_assert_null(job); | |
53 | error_free(errp); | |
54 | } | |
55 | ||
56 | return job; | |
57 | } | |
58 | ||
fb367e03 JS |
59 | static BlockJob *do_test_id(BlockBackend *blk, const char *id, |
60 | bool should_succeed) | |
61 | { | |
62 | return mk_job(blk, id, &test_block_job_driver, | |
bb02b65c | 63 | should_succeed, JOB_DEFAULT); |
fb367e03 JS |
64 | } |
65 | ||
9ef8112a AG |
66 | /* This creates a BlockBackend (optionally with a name) with a |
67 | * BlockDriverState inserted. */ | |
68 | static BlockBackend *create_blk(const char *name) | |
69 | { | |
2807c0cd | 70 | /* No I/O is performed on this device */ |
d861ab3a | 71 | BlockBackend *blk = blk_new(qemu_get_aio_context(), 0, BLK_PERM_ALL); |
d185cf0e KW |
72 | BlockDriverState *bs; |
73 | ||
74 | bs = bdrv_open("null-co://", NULL, NULL, 0, &error_abort); | |
75 | g_assert_nonnull(bs); | |
9ef8112a | 76 | |
d7086422 | 77 | blk_insert_bs(blk, bs, &error_abort); |
9ef8112a AG |
78 | bdrv_unref(bs); |
79 | ||
80 | if (name) { | |
81 | Error *errp = NULL; | |
82 | monitor_add_blk(blk, name, &errp); | |
83 | g_assert_null(errp); | |
84 | } | |
85 | ||
86 | return blk; | |
87 | } | |
88 | ||
89 | /* This destroys the backend */ | |
90 | static void destroy_blk(BlockBackend *blk) | |
91 | { | |
92 | if (blk_name(blk)[0] != '\0') { | |
93 | monitor_remove_blk(blk); | |
94 | } | |
95 | ||
96 | blk_remove_bs(blk); | |
97 | blk_unref(blk); | |
98 | } | |
99 | ||
100 | static void test_job_ids(void) | |
101 | { | |
102 | BlockBackend *blk[3]; | |
103 | BlockJob *job[3]; | |
104 | ||
105 | blk[0] = create_blk(NULL); | |
106 | blk[1] = create_blk("drive1"); | |
107 | blk[2] = create_blk("drive2"); | |
108 | ||
109 | /* No job ID provided and the block backend has no name */ | |
110 | job[0] = do_test_id(blk[0], NULL, false); | |
111 | ||
112 | /* These are all invalid job IDs */ | |
113 | job[0] = do_test_id(blk[0], "0id", false); | |
114 | job[0] = do_test_id(blk[0], "", false); | |
115 | job[0] = do_test_id(blk[0], " ", false); | |
116 | job[0] = do_test_id(blk[0], "123", false); | |
117 | job[0] = do_test_id(blk[0], "_id", false); | |
118 | job[0] = do_test_id(blk[0], "-id", false); | |
119 | job[0] = do_test_id(blk[0], ".id", false); | |
120 | job[0] = do_test_id(blk[0], "#id", false); | |
121 | ||
122 | /* This one is valid */ | |
123 | job[0] = do_test_id(blk[0], "id0", true); | |
124 | ||
b23c580c VSO |
125 | /* We can have two jobs in the same BDS */ |
126 | job[1] = do_test_id(blk[0], "id1", true); | |
127 | job_early_fail(&job[1]->job); | |
9ef8112a AG |
128 | |
129 | /* Duplicate job IDs are not allowed */ | |
130 | job[1] = do_test_id(blk[1], "id0", false); | |
131 | ||
132 | /* But once job[0] finishes we can reuse its ID */ | |
4ad35181 | 133 | job_early_fail(&job[0]->job); |
9ef8112a AG |
134 | job[1] = do_test_id(blk[1], "id0", true); |
135 | ||
136 | /* No job ID specified, defaults to the backend name ('drive1') */ | |
4ad35181 | 137 | job_early_fail(&job[1]->job); |
9ef8112a AG |
138 | job[1] = do_test_id(blk[1], NULL, true); |
139 | ||
140 | /* Duplicate job ID */ | |
141 | job[2] = do_test_id(blk[2], "drive1", false); | |
142 | ||
143 | /* The ID of job[2] would default to 'drive2' but it is already in use */ | |
144 | job[0] = do_test_id(blk[0], "drive2", true); | |
145 | job[2] = do_test_id(blk[2], NULL, false); | |
146 | ||
147 | /* This one is valid */ | |
148 | job[2] = do_test_id(blk[2], "id_2", true); | |
149 | ||
4ad35181 KW |
150 | job_early_fail(&job[0]->job); |
151 | job_early_fail(&job[1]->job); | |
152 | job_early_fail(&job[2]->job); | |
9ef8112a AG |
153 | |
154 | destroy_blk(blk[0]); | |
155 | destroy_blk(blk[1]); | |
156 | destroy_blk(blk[2]); | |
157 | } | |
158 | ||
fb367e03 JS |
159 | typedef struct CancelJob { |
160 | BlockJob common; | |
161 | BlockBackend *blk; | |
162 | bool should_converge; | |
163 | bool should_complete; | |
fb367e03 JS |
164 | } CancelJob; |
165 | ||
3453d972 | 166 | static void cancel_job_complete(Job *job, Error **errp) |
fb367e03 | 167 | { |
3453d972 | 168 | CancelJob *s = container_of(job, CancelJob, common.job); |
fb367e03 JS |
169 | s->should_complete = true; |
170 | } | |
171 | ||
f67432a2 | 172 | static int coroutine_fn cancel_job_run(Job *job, Error **errp) |
fb367e03 | 173 | { |
f67432a2 | 174 | CancelJob *s = container_of(job, CancelJob, common.job); |
fb367e03 JS |
175 | |
176 | while (!s->should_complete) { | |
daa7f2f9 | 177 | if (job_is_cancelled(&s->common.job)) { |
eb23654d | 178 | return 0; |
fb367e03 JS |
179 | } |
180 | ||
df956ae2 | 181 | if (!job_is_ready(&s->common.job) && s->should_converge) { |
2e1795b5 | 182 | job_transition_to_ready(&s->common.job); |
fb367e03 JS |
183 | } |
184 | ||
5d43e86e | 185 | job_sleep_ns(&s->common.job, 100000); |
fb367e03 JS |
186 | } |
187 | ||
f67432a2 | 188 | return 0; |
fb367e03 JS |
189 | } |
190 | ||
191 | static const BlockJobDriver test_cancel_driver = { | |
33e9e9bd KW |
192 | .job_driver = { |
193 | .instance_size = sizeof(CancelJob), | |
80fa2c75 | 194 | .free = block_job_free, |
b15de828 | 195 | .user_resume = block_job_user_resume, |
b69f777d | 196 | .drain = block_job_drain, |
f67432a2 | 197 | .run = cancel_job_run, |
3453d972 | 198 | .complete = cancel_job_complete, |
33e9e9bd | 199 | }, |
fb367e03 JS |
200 | }; |
201 | ||
0cc4643b | 202 | static CancelJob *create_common(Job **pjob) |
fb367e03 JS |
203 | { |
204 | BlockBackend *blk; | |
0cc4643b JS |
205 | Job *job; |
206 | BlockJob *bjob; | |
fb367e03 JS |
207 | CancelJob *s; |
208 | ||
209 | blk = create_blk(NULL); | |
0cc4643b JS |
210 | bjob = mk_job(blk, "Steve", &test_cancel_driver, true, |
211 | JOB_MANUAL_FINALIZE | JOB_MANUAL_DISMISS); | |
212 | job = &bjob->job; | |
213 | job_ref(job); | |
214 | assert(job->status == JOB_STATUS_CREATED); | |
215 | s = container_of(bjob, CancelJob, common); | |
fb367e03 JS |
216 | s->blk = blk; |
217 | ||
218 | *pjob = job; | |
219 | return s; | |
220 | } | |
221 | ||
222 | static void cancel_common(CancelJob *s) | |
223 | { | |
224 | BlockJob *job = &s->common; | |
225 | BlockBackend *blk = s->blk; | |
a50c2ab8 | 226 | JobStatus sts = job->job.status; |
30c070a5 KW |
227 | AioContext *ctx; |
228 | ||
229 | ctx = job->job.aio_context; | |
230 | aio_context_acquire(ctx); | |
fb367e03 | 231 | |
3d70ff53 | 232 | job_cancel_sync(&job->job); |
a50c2ab8 | 233 | if (sts != JOB_STATUS_CREATED && sts != JOB_STATUS_CONCLUDED) { |
5f9a6a08 KW |
234 | Job *dummy = &job->job; |
235 | job_dismiss(&dummy, &error_abort); | |
fb367e03 | 236 | } |
a50c2ab8 | 237 | assert(job->job.status == JOB_STATUS_NULL); |
80fa2c75 | 238 | job_unref(&job->job); |
fb367e03 | 239 | destroy_blk(blk); |
30c070a5 KW |
240 | |
241 | aio_context_release(ctx); | |
fb367e03 JS |
242 | } |
243 | ||
244 | static void test_cancel_created(void) | |
245 | { | |
0cc4643b | 246 | Job *job; |
fb367e03 JS |
247 | CancelJob *s; |
248 | ||
249 | s = create_common(&job); | |
250 | cancel_common(s); | |
251 | } | |
252 | ||
253 | static void test_cancel_running(void) | |
254 | { | |
0cc4643b | 255 | Job *job; |
fb367e03 JS |
256 | CancelJob *s; |
257 | ||
258 | s = create_common(&job); | |
259 | ||
0cc4643b JS |
260 | job_start(job); |
261 | assert(job->status == JOB_STATUS_RUNNING); | |
fb367e03 JS |
262 | |
263 | cancel_common(s); | |
264 | } | |
265 | ||
266 | static void test_cancel_paused(void) | |
267 | { | |
0cc4643b | 268 | Job *job; |
fb367e03 JS |
269 | CancelJob *s; |
270 | ||
271 | s = create_common(&job); | |
272 | ||
0cc4643b JS |
273 | job_start(job); |
274 | assert(job->status == JOB_STATUS_RUNNING); | |
fb367e03 | 275 | |
0cc4643b JS |
276 | job_user_pause(job, &error_abort); |
277 | job_enter(job); | |
278 | assert(job->status == JOB_STATUS_PAUSED); | |
fb367e03 JS |
279 | |
280 | cancel_common(s); | |
281 | } | |
282 | ||
283 | static void test_cancel_ready(void) | |
284 | { | |
0cc4643b | 285 | Job *job; |
fb367e03 JS |
286 | CancelJob *s; |
287 | ||
288 | s = create_common(&job); | |
289 | ||
0cc4643b JS |
290 | job_start(job); |
291 | assert(job->status == JOB_STATUS_RUNNING); | |
fb367e03 JS |
292 | |
293 | s->should_converge = true; | |
0cc4643b JS |
294 | job_enter(job); |
295 | assert(job->status == JOB_STATUS_READY); | |
fb367e03 JS |
296 | |
297 | cancel_common(s); | |
298 | } | |
299 | ||
300 | static void test_cancel_standby(void) | |
301 | { | |
0cc4643b | 302 | Job *job; |
fb367e03 JS |
303 | CancelJob *s; |
304 | ||
305 | s = create_common(&job); | |
306 | ||
0cc4643b JS |
307 | job_start(job); |
308 | assert(job->status == JOB_STATUS_RUNNING); | |
fb367e03 JS |
309 | |
310 | s->should_converge = true; | |
0cc4643b JS |
311 | job_enter(job); |
312 | assert(job->status == JOB_STATUS_READY); | |
fb367e03 | 313 | |
0cc4643b JS |
314 | job_user_pause(job, &error_abort); |
315 | job_enter(job); | |
316 | assert(job->status == JOB_STATUS_STANDBY); | |
fb367e03 JS |
317 | |
318 | cancel_common(s); | |
319 | } | |
320 | ||
321 | static void test_cancel_pending(void) | |
322 | { | |
0cc4643b | 323 | Job *job; |
fb367e03 JS |
324 | CancelJob *s; |
325 | ||
326 | s = create_common(&job); | |
327 | ||
0cc4643b JS |
328 | job_start(job); |
329 | assert(job->status == JOB_STATUS_RUNNING); | |
fb367e03 JS |
330 | |
331 | s->should_converge = true; | |
0cc4643b JS |
332 | job_enter(job); |
333 | assert(job->status == JOB_STATUS_READY); | |
fb367e03 | 334 | |
0cc4643b JS |
335 | job_complete(job, &error_abort); |
336 | job_enter(job); | |
977d26fd | 337 | while (!job->deferred_to_main_loop) { |
fb367e03 JS |
338 | aio_poll(qemu_get_aio_context(), true); |
339 | } | |
977d26fd JS |
340 | assert(job->status == JOB_STATUS_READY); |
341 | aio_poll(qemu_get_aio_context(), true); | |
0cc4643b | 342 | assert(job->status == JOB_STATUS_PENDING); |
fb367e03 JS |
343 | |
344 | cancel_common(s); | |
345 | } | |
346 | ||
347 | static void test_cancel_concluded(void) | |
348 | { | |
0cc4643b | 349 | Job *job; |
fb367e03 JS |
350 | CancelJob *s; |
351 | ||
352 | s = create_common(&job); | |
353 | ||
0cc4643b JS |
354 | job_start(job); |
355 | assert(job->status == JOB_STATUS_RUNNING); | |
fb367e03 JS |
356 | |
357 | s->should_converge = true; | |
0cc4643b JS |
358 | job_enter(job); |
359 | assert(job->status == JOB_STATUS_READY); | |
fb367e03 | 360 | |
0cc4643b JS |
361 | job_complete(job, &error_abort); |
362 | job_enter(job); | |
977d26fd | 363 | while (!job->deferred_to_main_loop) { |
fb367e03 JS |
364 | aio_poll(qemu_get_aio_context(), true); |
365 | } | |
977d26fd JS |
366 | assert(job->status == JOB_STATUS_READY); |
367 | aio_poll(qemu_get_aio_context(), true); | |
0cc4643b | 368 | assert(job->status == JOB_STATUS_PENDING); |
fb367e03 | 369 | |
0cc4643b JS |
370 | job_finalize(job, &error_abort); |
371 | assert(job->status == JOB_STATUS_CONCLUDED); | |
fb367e03 JS |
372 | |
373 | cancel_common(s); | |
374 | } | |
375 | ||
9ef8112a AG |
376 | int main(int argc, char **argv) |
377 | { | |
378 | qemu_init_main_loop(&error_abort); | |
d185cf0e | 379 | bdrv_init(); |
9ef8112a AG |
380 | |
381 | g_test_init(&argc, &argv, NULL); | |
382 | g_test_add_func("/blockjob/ids", test_job_ids); | |
fb367e03 JS |
383 | g_test_add_func("/blockjob/cancel/created", test_cancel_created); |
384 | g_test_add_func("/blockjob/cancel/running", test_cancel_running); | |
385 | g_test_add_func("/blockjob/cancel/paused", test_cancel_paused); | |
386 | g_test_add_func("/blockjob/cancel/ready", test_cancel_ready); | |
387 | g_test_add_func("/blockjob/cancel/standby", test_cancel_standby); | |
388 | g_test_add_func("/blockjob/cancel/pending", test_cancel_pending); | |
389 | g_test_add_func("/blockjob/cancel/concluded", test_cancel_concluded); | |
9ef8112a AG |
390 | return g_test_run(); |
391 | } |