4 * Copyright Igalia, S.L. 2016
7 * Alberto Garcia <berto@igalia.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"
20 static const BlockJobDriver test_block_job_driver
= {
22 .instance_size
= sizeof(BlockJob
),
23 .free
= block_job_free
,
24 .user_resume
= block_job_user_resume
,
25 .drain
= block_job_drain
,
29 static void block_job_cb(void *opaque
, int ret
)
33 static BlockJob
*mk_job(BlockBackend
*blk
, const char *id
,
34 const BlockJobDriver
*drv
, bool should_succeed
,
40 job
= block_job_create(id
, drv
, NULL
, blk_bs(blk
),
41 0, BLK_PERM_ALL
, 0, flags
, block_job_cb
,
45 g_assert_nonnull(job
);
47 g_assert_cmpstr(job
->job
.id
, ==, id
);
49 g_assert_cmpstr(job
->job
.id
, ==, blk_name(blk
));
52 g_assert_nonnull(errp
);
60 static BlockJob
*do_test_id(BlockBackend
*blk
, const char *id
,
63 return mk_job(blk
, id
, &test_block_job_driver
,
64 should_succeed
, JOB_DEFAULT
);
67 /* This creates a BlockBackend (optionally with a name) with a
68 * BlockDriverState inserted. */
69 static BlockBackend
*create_blk(const char *name
)
71 /* No I/O is performed on this device */
72 BlockBackend
*blk
= blk_new(qemu_get_aio_context(), 0, BLK_PERM_ALL
);
75 QDict
*opt
= qdict_new();
76 qdict_put_str(opt
, "file.read-zeroes", "on");
77 bs
= bdrv_open("null-co://", NULL
, opt
, 0, &error_abort
);
80 blk_insert_bs(blk
, bs
, &error_abort
);
85 monitor_add_blk(blk
, name
, &errp
);
92 /* This destroys the backend */
93 static void destroy_blk(BlockBackend
*blk
)
95 if (blk_name(blk
)[0] != '\0') {
96 monitor_remove_blk(blk
);
103 static void test_job_ids(void)
105 BlockBackend
*blk
[3];
108 blk
[0] = create_blk(NULL
);
109 blk
[1] = create_blk("drive1");
110 blk
[2] = create_blk("drive2");
112 /* No job ID provided and the block backend has no name */
113 job
[0] = do_test_id(blk
[0], NULL
, false);
115 /* These are all invalid job IDs */
116 job
[0] = do_test_id(blk
[0], "0id", false);
117 job
[0] = do_test_id(blk
[0], "", false);
118 job
[0] = do_test_id(blk
[0], " ", false);
119 job
[0] = do_test_id(blk
[0], "123", false);
120 job
[0] = do_test_id(blk
[0], "_id", false);
121 job
[0] = do_test_id(blk
[0], "-id", false);
122 job
[0] = do_test_id(blk
[0], ".id", false);
123 job
[0] = do_test_id(blk
[0], "#id", false);
125 /* This one is valid */
126 job
[0] = do_test_id(blk
[0], "id0", true);
128 /* We can have two jobs in the same BDS */
129 job
[1] = do_test_id(blk
[0], "id1", true);
130 job_early_fail(&job
[1]->job
);
132 /* Duplicate job IDs are not allowed */
133 job
[1] = do_test_id(blk
[1], "id0", false);
135 /* But once job[0] finishes we can reuse its ID */
136 job_early_fail(&job
[0]->job
);
137 job
[1] = do_test_id(blk
[1], "id0", true);
139 /* No job ID specified, defaults to the backend name ('drive1') */
140 job_early_fail(&job
[1]->job
);
141 job
[1] = do_test_id(blk
[1], NULL
, true);
143 /* Duplicate job ID */
144 job
[2] = do_test_id(blk
[2], "drive1", false);
146 /* The ID of job[2] would default to 'drive2' but it is already in use */
147 job
[0] = do_test_id(blk
[0], "drive2", true);
148 job
[2] = do_test_id(blk
[2], NULL
, false);
150 /* This one is valid */
151 job
[2] = do_test_id(blk
[2], "id_2", true);
153 job_early_fail(&job
[0]->job
);
154 job_early_fail(&job
[1]->job
);
155 job_early_fail(&job
[2]->job
);
162 typedef struct CancelJob
{
165 bool should_converge
;
166 bool should_complete
;
169 static void cancel_job_complete(Job
*job
, Error
**errp
)
171 CancelJob
*s
= container_of(job
, CancelJob
, common
.job
);
172 s
->should_complete
= true;
175 static int coroutine_fn
cancel_job_run(Job
*job
, Error
**errp
)
177 CancelJob
*s
= container_of(job
, CancelJob
, common
.job
);
179 while (!s
->should_complete
) {
180 if (job_is_cancelled(&s
->common
.job
)) {
184 if (!job_is_ready(&s
->common
.job
) && s
->should_converge
) {
185 job_transition_to_ready(&s
->common
.job
);
188 job_sleep_ns(&s
->common
.job
, 100000);
194 static const BlockJobDriver test_cancel_driver
= {
196 .instance_size
= sizeof(CancelJob
),
197 .free
= block_job_free
,
198 .user_resume
= block_job_user_resume
,
199 .drain
= block_job_drain
,
200 .run
= cancel_job_run
,
201 .complete
= cancel_job_complete
,
205 static CancelJob
*create_common(Job
**pjob
)
212 blk
= create_blk(NULL
);
213 bjob
= mk_job(blk
, "Steve", &test_cancel_driver
, true,
214 JOB_MANUAL_FINALIZE
| JOB_MANUAL_DISMISS
);
217 assert(job
->status
== JOB_STATUS_CREATED
);
218 s
= container_of(bjob
, CancelJob
, common
);
225 static void cancel_common(CancelJob
*s
)
227 BlockJob
*job
= &s
->common
;
228 BlockBackend
*blk
= s
->blk
;
229 JobStatus sts
= job
->job
.status
;
232 ctx
= job
->job
.aio_context
;
233 aio_context_acquire(ctx
);
235 job_cancel_sync(&job
->job
);
236 if (sts
!= JOB_STATUS_CREATED
&& sts
!= JOB_STATUS_CONCLUDED
) {
237 Job
*dummy
= &job
->job
;
238 job_dismiss(&dummy
, &error_abort
);
240 assert(job
->job
.status
== JOB_STATUS_NULL
);
241 job_unref(&job
->job
);
244 aio_context_release(ctx
);
247 static void test_cancel_created(void)
252 s
= create_common(&job
);
256 static void test_cancel_running(void)
261 s
= create_common(&job
);
264 assert(job
->status
== JOB_STATUS_RUNNING
);
269 static void test_cancel_paused(void)
274 s
= create_common(&job
);
277 assert(job
->status
== JOB_STATUS_RUNNING
);
279 job_user_pause(job
, &error_abort
);
281 assert(job
->status
== JOB_STATUS_PAUSED
);
286 static void test_cancel_ready(void)
291 s
= create_common(&job
);
294 assert(job
->status
== JOB_STATUS_RUNNING
);
296 s
->should_converge
= true;
298 assert(job
->status
== JOB_STATUS_READY
);
303 static void test_cancel_standby(void)
308 s
= create_common(&job
);
311 assert(job
->status
== JOB_STATUS_RUNNING
);
313 s
->should_converge
= true;
315 assert(job
->status
== JOB_STATUS_READY
);
317 job_user_pause(job
, &error_abort
);
319 assert(job
->status
== JOB_STATUS_STANDBY
);
324 static void test_cancel_pending(void)
329 s
= create_common(&job
);
332 assert(job
->status
== JOB_STATUS_RUNNING
);
334 s
->should_converge
= true;
336 assert(job
->status
== JOB_STATUS_READY
);
338 job_complete(job
, &error_abort
);
340 while (!job
->deferred_to_main_loop
) {
341 aio_poll(qemu_get_aio_context(), true);
343 assert(job
->status
== JOB_STATUS_READY
);
344 aio_poll(qemu_get_aio_context(), true);
345 assert(job
->status
== JOB_STATUS_PENDING
);
350 static void test_cancel_concluded(void)
355 s
= create_common(&job
);
358 assert(job
->status
== JOB_STATUS_RUNNING
);
360 s
->should_converge
= true;
362 assert(job
->status
== JOB_STATUS_READY
);
364 job_complete(job
, &error_abort
);
366 while (!job
->deferred_to_main_loop
) {
367 aio_poll(qemu_get_aio_context(), true);
369 assert(job
->status
== JOB_STATUS_READY
);
370 aio_poll(qemu_get_aio_context(), true);
371 assert(job
->status
== JOB_STATUS_PENDING
);
373 job_finalize(job
, &error_abort
);
374 assert(job
->status
== JOB_STATUS_CONCLUDED
);
379 int main(int argc
, char **argv
)
381 qemu_init_main_loop(&error_abort
);
384 g_test_init(&argc
, &argv
, NULL
);
385 g_test_add_func("/blockjob/ids", test_job_ids
);
386 g_test_add_func("/blockjob/cancel/created", test_cancel_created
);
387 g_test_add_func("/blockjob/cancel/running", test_cancel_running
);
388 g_test_add_func("/blockjob/cancel/paused", test_cancel_paused
);
389 g_test_add_func("/blockjob/cancel/ready", test_cancel_ready
);
390 g_test_add_func("/blockjob/cancel/standby", test_cancel_standby
);
391 g_test_add_func("/blockjob/cancel/pending", test_cancel_pending
);
392 g_test_add_func("/blockjob/cancel/concluded", test_cancel_concluded
);