blob: a130f6fefbae0811cbcd85f39a7ffc7349b49075 [file] [log] [blame]
Alberto Garcia9ef81122016-07-29 17:31:41 +03001/*
2 * Blockjob tests
3 *
4 * Copyright Igalia, S.L. 2016
5 *
6 * Authors:
7 * Alberto Garcia <berto@igalia.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"
John Snowc87621e2016-10-27 12:07:00 -040016#include "block/blockjob_int.h"
Alberto Garcia9ef81122016-07-29 17:31:41 +030017#include "sysemu/block-backend.h"
Andrey Shinkevichca1ef1e2019-07-29 15:46:00 +030018#include "qapi/qmp/qdict.h"
Max Reitzc2c731a2021-04-09 14:04:21 +020019#include "iothread.h"
Alberto Garcia9ef81122016-07-29 17:31:41 +030020
21static const BlockJobDriver test_block_job_driver = {
Kevin Wolf33e9e9b2018-04-12 17:29:59 +020022 .job_driver = {
23 .instance_size = sizeof(BlockJob),
Kevin Wolf80fa2c72018-04-13 18:50:05 +020024 .free = block_job_free,
Kevin Wolfb15de822018-04-18 17:10:26 +020025 .user_resume = block_job_user_resume,
Kevin Wolf33e9e9b2018-04-12 17:29:59 +020026 },
Alberto Garcia9ef81122016-07-29 17:31:41 +030027};
28
29static void block_job_cb(void *opaque, int ret)
30{
31}
32
John Snowfb367e02018-03-10 03:27:46 -050033static BlockJob *mk_job(BlockBackend *blk, const char *id,
34 const BlockJobDriver *drv, bool should_succeed,
35 int flags)
Alberto Garcia9ef81122016-07-29 17:31:41 +030036{
37 BlockJob *job;
Markus Armbruster8ca63ba2019-12-04 10:36:23 +010038 Error *err = NULL;
Alberto Garcia9ef81122016-07-29 17:31:41 +030039
John Snowfb367e02018-03-10 03:27:46 -050040 job = block_job_create(id, drv, NULL, blk_bs(blk),
41 0, BLK_PERM_ALL, 0, flags, block_job_cb,
Markus Armbruster8ca63ba2019-12-04 10:36:23 +010042 NULL, &err);
Alberto Garcia9ef81122016-07-29 17:31:41 +030043 if (should_succeed) {
Markus Armbruster8ca63ba2019-12-04 10:36:23 +010044 g_assert_null(err);
Alberto Garcia9ef81122016-07-29 17:31:41 +030045 g_assert_nonnull(job);
46 if (id) {
Kevin Wolf33e9e9b2018-04-12 17:29:59 +020047 g_assert_cmpstr(job->job.id, ==, id);
Alberto Garcia9ef81122016-07-29 17:31:41 +030048 } else {
Kevin Wolf33e9e9b2018-04-12 17:29:59 +020049 g_assert_cmpstr(job->job.id, ==, blk_name(blk));
Alberto Garcia9ef81122016-07-29 17:31:41 +030050 }
51 } else {
Markus Armbruster0cf9e2b2019-12-04 10:36:25 +010052 error_free_or_abort(&err);
Alberto Garcia9ef81122016-07-29 17:31:41 +030053 g_assert_null(job);
Alberto Garcia9ef81122016-07-29 17:31:41 +030054 }
55
56 return job;
57}
58
John Snowfb367e02018-03-10 03:27:46 -050059static BlockJob *do_test_id(BlockBackend *blk, const char *id,
60 bool should_succeed)
61{
62 return mk_job(blk, id, &test_block_job_driver,
Kevin Wolfbb02b652018-04-19 17:54:56 +020063 should_succeed, JOB_DEFAULT);
John Snowfb367e02018-03-10 03:27:46 -050064}
65
Alberto Garcia9ef81122016-07-29 17:31:41 +030066/* This creates a BlockBackend (optionally with a name) with a
67 * BlockDriverState inserted. */
68static BlockBackend *create_blk(const char *name)
69{
Kevin Wolf2807c0c2017-02-09 15:48:04 +010070 /* No I/O is performed on this device */
Kevin Wolfd861ab32019-04-25 14:25:10 +020071 BlockBackend *blk = blk_new(qemu_get_aio_context(), 0, BLK_PERM_ALL);
Kevin Wolfd185cf02017-01-16 17:17:38 +010072 BlockDriverState *bs;
73
Andrey Shinkevichca1ef1e2019-07-29 15:46:00 +030074 QDict *opt = qdict_new();
75 qdict_put_str(opt, "file.read-zeroes", "on");
76 bs = bdrv_open("null-co://", NULL, opt, 0, &error_abort);
Kevin Wolfd185cf02017-01-16 17:17:38 +010077 g_assert_nonnull(bs);
Alberto Garcia9ef81122016-07-29 17:31:41 +030078
Kevin Wolfd7086422017-01-13 19:02:32 +010079 blk_insert_bs(blk, bs, &error_abort);
Alberto Garcia9ef81122016-07-29 17:31:41 +030080 bdrv_unref(bs);
81
82 if (name) {
Markus Armbruster8ca63ba2019-12-04 10:36:23 +010083 Error *err = NULL;
84 monitor_add_blk(blk, name, &err);
85 g_assert_null(err);
Alberto Garcia9ef81122016-07-29 17:31:41 +030086 }
87
88 return blk;
89}
90
91/* This destroys the backend */
92static void destroy_blk(BlockBackend *blk)
93{
94 if (blk_name(blk)[0] != '\0') {
95 monitor_remove_blk(blk);
96 }
97
98 blk_remove_bs(blk);
99 blk_unref(blk);
100}
101
102static void test_job_ids(void)
103{
104 BlockBackend *blk[3];
105 BlockJob *job[3];
106
107 blk[0] = create_blk(NULL);
108 blk[1] = create_blk("drive1");
109 blk[2] = create_blk("drive2");
110
111 /* No job ID provided and the block backend has no name */
112 job[0] = do_test_id(blk[0], NULL, false);
113
114 /* These are all invalid job IDs */
115 job[0] = do_test_id(blk[0], "0id", false);
116 job[0] = do_test_id(blk[0], "", false);
117 job[0] = do_test_id(blk[0], " ", false);
118 job[0] = do_test_id(blk[0], "123", false);
119 job[0] = do_test_id(blk[0], "_id", 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
124 /* This one is valid */
125 job[0] = do_test_id(blk[0], "id0", true);
126
Vladimir Sementsov-Ogievskiyb23c5802019-06-06 18:41:32 +0300127 /* We can have two jobs in the same BDS */
128 job[1] = do_test_id(blk[0], "id1", true);
129 job_early_fail(&job[1]->job);
Alberto Garcia9ef81122016-07-29 17:31:41 +0300130
131 /* Duplicate job IDs are not allowed */
132 job[1] = do_test_id(blk[1], "id0", false);
133
134 /* But once job[0] finishes we can reuse its ID */
Kevin Wolf4ad35182018-04-19 17:30:16 +0200135 job_early_fail(&job[0]->job);
Alberto Garcia9ef81122016-07-29 17:31:41 +0300136 job[1] = do_test_id(blk[1], "id0", true);
137
138 /* No job ID specified, defaults to the backend name ('drive1') */
Kevin Wolf4ad35182018-04-19 17:30:16 +0200139 job_early_fail(&job[1]->job);
Alberto Garcia9ef81122016-07-29 17:31:41 +0300140 job[1] = do_test_id(blk[1], NULL, true);
141
142 /* Duplicate job ID */
143 job[2] = do_test_id(blk[2], "drive1", false);
144
145 /* The ID of job[2] would default to 'drive2' but it is already in use */
146 job[0] = do_test_id(blk[0], "drive2", true);
147 job[2] = do_test_id(blk[2], NULL, false);
148
149 /* This one is valid */
150 job[2] = do_test_id(blk[2], "id_2", true);
151
Kevin Wolf4ad35182018-04-19 17:30:16 +0200152 job_early_fail(&job[0]->job);
153 job_early_fail(&job[1]->job);
154 job_early_fail(&job[2]->job);
Alberto Garcia9ef81122016-07-29 17:31:41 +0300155
156 destroy_blk(blk[0]);
157 destroy_blk(blk[1]);
158 destroy_blk(blk[2]);
159}
160
John Snowfb367e02018-03-10 03:27:46 -0500161typedef struct CancelJob {
162 BlockJob common;
163 BlockBackend *blk;
164 bool should_converge;
165 bool should_complete;
John Snowfb367e02018-03-10 03:27:46 -0500166} CancelJob;
167
Kevin Wolf3453d972018-04-23 12:24:16 +0200168static void cancel_job_complete(Job *job, Error **errp)
John Snowfb367e02018-03-10 03:27:46 -0500169{
Kevin Wolf3453d972018-04-23 12:24:16 +0200170 CancelJob *s = container_of(job, CancelJob, common.job);
John Snowfb367e02018-03-10 03:27:46 -0500171 s->should_complete = true;
172}
173
John Snowf67432a2018-08-29 21:57:26 -0400174static int coroutine_fn cancel_job_run(Job *job, Error **errp)
John Snowfb367e02018-03-10 03:27:46 -0500175{
John Snowf67432a2018-08-29 21:57:26 -0400176 CancelJob *s = container_of(job, CancelJob, common.job);
John Snowfb367e02018-03-10 03:27:46 -0500177
178 while (!s->should_complete) {
Kevin Wolfdaa7f2f2018-04-17 12:56:07 +0200179 if (job_is_cancelled(&s->common.job)) {
John Snoweb236542018-08-29 21:57:31 -0400180 return 0;
John Snowfb367e02018-03-10 03:27:46 -0500181 }
182
Kevin Wolfdf956ae2018-04-25 15:09:58 +0200183 if (!job_is_ready(&s->common.job) && s->should_converge) {
Kevin Wolf2e1795b2018-04-25 14:56:09 +0200184 job_transition_to_ready(&s->common.job);
John Snowfb367e02018-03-10 03:27:46 -0500185 }
186
Kevin Wolf5d43e862018-04-18 16:32:20 +0200187 job_sleep_ns(&s->common.job, 100000);
John Snowfb367e02018-03-10 03:27:46 -0500188 }
189
John Snowf67432a2018-08-29 21:57:26 -0400190 return 0;
John Snowfb367e02018-03-10 03:27:46 -0500191}
192
193static const BlockJobDriver test_cancel_driver = {
Kevin Wolf33e9e9b2018-04-12 17:29:59 +0200194 .job_driver = {
195 .instance_size = sizeof(CancelJob),
Kevin Wolf80fa2c72018-04-13 18:50:05 +0200196 .free = block_job_free,
Kevin Wolfb15de822018-04-18 17:10:26 +0200197 .user_resume = block_job_user_resume,
John Snowf67432a2018-08-29 21:57:26 -0400198 .run = cancel_job_run,
Kevin Wolf3453d972018-04-23 12:24:16 +0200199 .complete = cancel_job_complete,
Kevin Wolf33e9e9b2018-04-12 17:29:59 +0200200 },
John Snowfb367e02018-03-10 03:27:46 -0500201};
202
John Snow0cc46432018-09-06 09:02:17 -0400203static CancelJob *create_common(Job **pjob)
John Snowfb367e02018-03-10 03:27:46 -0500204{
205 BlockBackend *blk;
John Snow0cc46432018-09-06 09:02:17 -0400206 Job *job;
207 BlockJob *bjob;
John Snowfb367e02018-03-10 03:27:46 -0500208 CancelJob *s;
209
210 blk = create_blk(NULL);
John Snow0cc46432018-09-06 09:02:17 -0400211 bjob = mk_job(blk, "Steve", &test_cancel_driver, true,
212 JOB_MANUAL_FINALIZE | JOB_MANUAL_DISMISS);
213 job = &bjob->job;
Emanuele Giuseppe Esposito191e7af2022-09-26 05:32:02 -0400214 WITH_JOB_LOCK_GUARD() {
215 job_ref_locked(job);
216 assert(job->status == JOB_STATUS_CREATED);
217 }
218
John Snow0cc46432018-09-06 09:02:17 -0400219 s = container_of(bjob, CancelJob, common);
John Snowfb367e02018-03-10 03:27:46 -0500220 s->blk = blk;
221
222 *pjob = job;
223 return s;
224}
225
226static void cancel_common(CancelJob *s)
227{
228 BlockJob *job = &s->common;
229 BlockBackend *blk = s->blk;
Kevin Wolfa50c2ab2018-04-13 17:19:31 +0200230 JobStatus sts = job->job.status;
Emanuele Giuseppe Esposito6f592e52022-09-26 05:32:11 -0400231 AioContext *ctx = job->job.aio_context;
John Snowfb367e02018-03-10 03:27:46 -0500232
Hanna Reitz4cfb3f02021-10-06 17:19:32 +0200233 job_cancel_sync(&job->job, true);
Emanuele Giuseppe Esposito191e7af2022-09-26 05:32:02 -0400234 WITH_JOB_LOCK_GUARD() {
235 if (sts != JOB_STATUS_CREATED && sts != JOB_STATUS_CONCLUDED) {
236 Job *dummy = &job->job;
237 job_dismiss_locked(&dummy, &error_abort);
238 }
239 assert(job->job.status == JOB_STATUS_NULL);
240 job_unref_locked(&job->job);
John Snowfb367e02018-03-10 03:27:46 -0500241 }
Kevin Wolf30c070a2018-08-17 17:29:08 +0200242
Emanuele Giuseppe Esposito6f592e52022-09-26 05:32:11 -0400243 aio_context_acquire(ctx);
244 destroy_blk(blk);
Kevin Wolf30c070a2018-08-17 17:29:08 +0200245 aio_context_release(ctx);
Emanuele Giuseppe Esposito6f592e52022-09-26 05:32:11 -0400246
John Snowfb367e02018-03-10 03:27:46 -0500247}
248
249static void test_cancel_created(void)
250{
John Snow0cc46432018-09-06 09:02:17 -0400251 Job *job;
John Snowfb367e02018-03-10 03:27:46 -0500252 CancelJob *s;
253
254 s = create_common(&job);
255 cancel_common(s);
256}
257
Emanuele Giuseppe Esposito191e7af2022-09-26 05:32:02 -0400258static void assert_job_status_is(Job *job, int status)
259{
260 WITH_JOB_LOCK_GUARD() {
261 assert(job->status == status);
262 }
263}
264
John Snowfb367e02018-03-10 03:27:46 -0500265static void test_cancel_running(void)
266{
John Snow0cc46432018-09-06 09:02:17 -0400267 Job *job;
John Snowfb367e02018-03-10 03:27:46 -0500268 CancelJob *s;
269
270 s = create_common(&job);
271
John Snow0cc46432018-09-06 09:02:17 -0400272 job_start(job);
Emanuele Giuseppe Esposito191e7af2022-09-26 05:32:02 -0400273 assert_job_status_is(job, JOB_STATUS_RUNNING);
John Snowfb367e02018-03-10 03:27:46 -0500274
275 cancel_common(s);
276}
277
278static void test_cancel_paused(void)
279{
John Snow0cc46432018-09-06 09:02:17 -0400280 Job *job;
John Snowfb367e02018-03-10 03:27:46 -0500281 CancelJob *s;
282
283 s = create_common(&job);
284
John Snow0cc46432018-09-06 09:02:17 -0400285 job_start(job);
Emanuele Giuseppe Esposito191e7af2022-09-26 05:32:02 -0400286 WITH_JOB_LOCK_GUARD() {
287 assert(job->status == JOB_STATUS_RUNNING);
288 job_user_pause_locked(job, &error_abort);
289 }
John Snow0cc46432018-09-06 09:02:17 -0400290 job_enter(job);
Emanuele Giuseppe Esposito191e7af2022-09-26 05:32:02 -0400291 assert_job_status_is(job, JOB_STATUS_PAUSED);
John Snowfb367e02018-03-10 03:27:46 -0500292
293 cancel_common(s);
294}
295
296static void test_cancel_ready(void)
297{
John Snow0cc46432018-09-06 09:02:17 -0400298 Job *job;
John Snowfb367e02018-03-10 03:27:46 -0500299 CancelJob *s;
300
301 s = create_common(&job);
302
John Snow0cc46432018-09-06 09:02:17 -0400303 job_start(job);
Emanuele Giuseppe Esposito191e7af2022-09-26 05:32:02 -0400304 assert_job_status_is(job, JOB_STATUS_RUNNING);
John Snowfb367e02018-03-10 03:27:46 -0500305
306 s->should_converge = true;
John Snow0cc46432018-09-06 09:02:17 -0400307 job_enter(job);
Emanuele Giuseppe Esposito191e7af2022-09-26 05:32:02 -0400308 assert_job_status_is(job, JOB_STATUS_READY);
John Snowfb367e02018-03-10 03:27:46 -0500309
310 cancel_common(s);
311}
312
313static void test_cancel_standby(void)
314{
John Snow0cc46432018-09-06 09:02:17 -0400315 Job *job;
John Snowfb367e02018-03-10 03:27:46 -0500316 CancelJob *s;
317
318 s = create_common(&job);
319
John Snow0cc46432018-09-06 09:02:17 -0400320 job_start(job);
Emanuele Giuseppe Esposito191e7af2022-09-26 05:32:02 -0400321 assert_job_status_is(job, JOB_STATUS_RUNNING);
John Snowfb367e02018-03-10 03:27:46 -0500322
323 s->should_converge = true;
John Snow0cc46432018-09-06 09:02:17 -0400324 job_enter(job);
Emanuele Giuseppe Esposito191e7af2022-09-26 05:32:02 -0400325 WITH_JOB_LOCK_GUARD() {
326 assert(job->status == JOB_STATUS_READY);
327 job_user_pause_locked(job, &error_abort);
328 }
John Snow0cc46432018-09-06 09:02:17 -0400329 job_enter(job);
Emanuele Giuseppe Esposito191e7af2022-09-26 05:32:02 -0400330 assert_job_status_is(job, JOB_STATUS_STANDBY);
John Snowfb367e02018-03-10 03:27:46 -0500331
332 cancel_common(s);
333}
334
335static void test_cancel_pending(void)
336{
John Snow0cc46432018-09-06 09:02:17 -0400337 Job *job;
John Snowfb367e02018-03-10 03:27:46 -0500338 CancelJob *s;
339
340 s = create_common(&job);
341
John Snow0cc46432018-09-06 09:02:17 -0400342 job_start(job);
Emanuele Giuseppe Esposito191e7af2022-09-26 05:32:02 -0400343 assert_job_status_is(job, JOB_STATUS_RUNNING);
John Snowfb367e02018-03-10 03:27:46 -0500344
345 s->should_converge = true;
John Snow0cc46432018-09-06 09:02:17 -0400346 job_enter(job);
Emanuele Giuseppe Esposito191e7af2022-09-26 05:32:02 -0400347 WITH_JOB_LOCK_GUARD() {
348 assert(job->status == JOB_STATUS_READY);
349 job_complete_locked(job, &error_abort);
350 }
John Snow0cc46432018-09-06 09:02:17 -0400351 job_enter(job);
John Snow977d26f2018-09-06 09:02:18 -0400352 while (!job->deferred_to_main_loop) {
John Snowfb367e02018-03-10 03:27:46 -0500353 aio_poll(qemu_get_aio_context(), true);
354 }
Emanuele Giuseppe Esposito191e7af2022-09-26 05:32:02 -0400355 assert_job_status_is(job, JOB_STATUS_READY);
John Snow977d26f2018-09-06 09:02:18 -0400356 aio_poll(qemu_get_aio_context(), true);
Emanuele Giuseppe Esposito191e7af2022-09-26 05:32:02 -0400357 assert_job_status_is(job, JOB_STATUS_PENDING);
John Snowfb367e02018-03-10 03:27:46 -0500358
359 cancel_common(s);
360}
361
362static void test_cancel_concluded(void)
363{
John Snow0cc46432018-09-06 09:02:17 -0400364 Job *job;
John Snowfb367e02018-03-10 03:27:46 -0500365 CancelJob *s;
366
367 s = create_common(&job);
368
John Snow0cc46432018-09-06 09:02:17 -0400369 job_start(job);
Emanuele Giuseppe Esposito191e7af2022-09-26 05:32:02 -0400370 assert_job_status_is(job, JOB_STATUS_RUNNING);
John Snowfb367e02018-03-10 03:27:46 -0500371
372 s->should_converge = true;
John Snow0cc46432018-09-06 09:02:17 -0400373 job_enter(job);
Emanuele Giuseppe Esposito191e7af2022-09-26 05:32:02 -0400374 WITH_JOB_LOCK_GUARD() {
375 assert(job->status == JOB_STATUS_READY);
376 job_complete_locked(job, &error_abort);
377 }
John Snow0cc46432018-09-06 09:02:17 -0400378 job_enter(job);
John Snow977d26f2018-09-06 09:02:18 -0400379 while (!job->deferred_to_main_loop) {
John Snowfb367e02018-03-10 03:27:46 -0500380 aio_poll(qemu_get_aio_context(), true);
381 }
Emanuele Giuseppe Esposito191e7af2022-09-26 05:32:02 -0400382 assert_job_status_is(job, JOB_STATUS_READY);
John Snow977d26f2018-09-06 09:02:18 -0400383 aio_poll(qemu_get_aio_context(), true);
Emanuele Giuseppe Esposito191e7af2022-09-26 05:32:02 -0400384 assert_job_status_is(job, JOB_STATUS_PENDING);
John Snowfb367e02018-03-10 03:27:46 -0500385
Emanuele Giuseppe Esposito191e7af2022-09-26 05:32:02 -0400386 WITH_JOB_LOCK_GUARD() {
387 job_finalize_locked(job, &error_abort);
Emanuele Giuseppe Esposito6f592e52022-09-26 05:32:11 -0400388 assert(job->status == JOB_STATUS_CONCLUDED);
Emanuele Giuseppe Esposito191e7af2022-09-26 05:32:02 -0400389 }
John Snowfb367e02018-03-10 03:27:46 -0500390
391 cancel_common(s);
392}
393
Max Reitzc2c731a2021-04-09 14:04:21 +0200394/* (See test_yielding_driver for the job description) */
395typedef struct YieldingJob {
396 BlockJob common;
397 bool should_complete;
398} YieldingJob;
399
400static void yielding_job_complete(Job *job, Error **errp)
401{
402 YieldingJob *s = container_of(job, YieldingJob, common.job);
403 s->should_complete = true;
404 job_enter(job);
405}
406
407static int coroutine_fn yielding_job_run(Job *job, Error **errp)
408{
409 YieldingJob *s = container_of(job, YieldingJob, common.job);
410
411 job_transition_to_ready(job);
412
413 while (!s->should_complete) {
414 job_yield(job);
415 }
416
417 return 0;
418}
419
420/*
421 * This job transitions immediately to the READY state, and then
422 * yields until it is to complete.
423 */
424static const BlockJobDriver test_yielding_driver = {
425 .job_driver = {
426 .instance_size = sizeof(YieldingJob),
427 .free = block_job_free,
428 .user_resume = block_job_user_resume,
429 .run = yielding_job_run,
430 .complete = yielding_job_complete,
431 },
432};
433
434/*
Emanuele Giuseppe Esposito9bd4d3c2022-09-26 05:32:14 -0400435 * Test that job_complete_locked() works even on jobs that are in a paused
Max Reitzc2c731a2021-04-09 14:04:21 +0200436 * state (i.e., STANDBY).
437 *
438 * To do this, run YieldingJob in an IO thread, get it into the READY
439 * state, then have a drained section. Before ending the section,
440 * acquire the context so the job will not be entered and will thus
441 * remain on STANDBY.
442 *
Emanuele Giuseppe Esposito9bd4d3c2022-09-26 05:32:14 -0400443 * job_complete_locked() should still work without error.
Max Reitzc2c731a2021-04-09 14:04:21 +0200444 *
445 * Note that on the QMP interface, it is impossible to lock an IO
446 * thread before a drained section ends. In practice, the
447 * bdrv_drain_all_end() and the aio_context_acquire() will be
448 * reversed. However, that makes for worse reproducibility here:
449 * Sometimes, the job would no longer be in STANDBY then but already
450 * be started. We cannot prevent that, because the IO thread runs
451 * concurrently. We can only prevent it by taking the lock before
452 * ending the drained section, so we do that.
453 *
454 * (You can reverse the order of operations and most of the time the
455 * test will pass, but sometimes the assert(status == STANDBY) will
456 * fail.)
457 */
458static void test_complete_in_standby(void)
459{
460 BlockBackend *blk;
461 IOThread *iothread;
462 AioContext *ctx;
463 Job *job;
464 BlockJob *bjob;
465
466 /* Create a test drive, move it to an IO thread */
467 blk = create_blk(NULL);
468 iothread = iothread_new();
469
470 ctx = iothread_get_aio_context(iothread);
471 blk_set_aio_context(blk, ctx, &error_abort);
472
473 /* Create our test job */
474 bjob = mk_job(blk, "job", &test_yielding_driver, true,
475 JOB_MANUAL_FINALIZE | JOB_MANUAL_DISMISS);
476 job = &bjob->job;
Emanuele Giuseppe Esposito191e7af2022-09-26 05:32:02 -0400477 assert_job_status_is(job, JOB_STATUS_CREATED);
Max Reitzc2c731a2021-04-09 14:04:21 +0200478
479 /* Wait for the job to become READY */
480 job_start(job);
Emanuele Giuseppe Esposito191e7af2022-09-26 05:32:02 -0400481 /*
482 * Here we are waiting for the status to change, so don't bother
483 * protecting the read every time.
484 */
Emanuele Giuseppe Esposito6f592e52022-09-26 05:32:11 -0400485 AIO_WAIT_WHILE_UNLOCKED(ctx, job->status != JOB_STATUS_READY);
Max Reitzc2c731a2021-04-09 14:04:21 +0200486
487 /* Begin the drained section, pausing the job */
488 bdrv_drain_all_begin();
Emanuele Giuseppe Esposito191e7af2022-09-26 05:32:02 -0400489 assert_job_status_is(job, JOB_STATUS_STANDBY);
490
Max Reitzc2c731a2021-04-09 14:04:21 +0200491 /* Lock the IO thread to prevent the job from being run */
492 aio_context_acquire(ctx);
493 /* This will schedule the job to resume it */
494 bdrv_drain_all_end();
Emanuele Giuseppe Esposito6f592e52022-09-26 05:32:11 -0400495 aio_context_release(ctx);
Max Reitzc2c731a2021-04-09 14:04:21 +0200496
Emanuele Giuseppe Esposito191e7af2022-09-26 05:32:02 -0400497 WITH_JOB_LOCK_GUARD() {
498 /* But the job cannot run, so it will remain on standby */
499 assert(job->status == JOB_STATUS_STANDBY);
Max Reitzc2c731a2021-04-09 14:04:21 +0200500
Emanuele Giuseppe Esposito191e7af2022-09-26 05:32:02 -0400501 /* Even though the job is on standby, this should work */
502 job_complete_locked(job, &error_abort);
Max Reitzc2c731a2021-04-09 14:04:21 +0200503
Emanuele Giuseppe Esposito191e7af2022-09-26 05:32:02 -0400504 /* The test is done now, clean up. */
505 job_finish_sync_locked(job, NULL, &error_abort);
506 assert(job->status == JOB_STATUS_PENDING);
Max Reitzc2c731a2021-04-09 14:04:21 +0200507
Emanuele Giuseppe Esposito191e7af2022-09-26 05:32:02 -0400508 job_finalize_locked(job, &error_abort);
509 assert(job->status == JOB_STATUS_CONCLUDED);
Max Reitzc2c731a2021-04-09 14:04:21 +0200510
Emanuele Giuseppe Esposito191e7af2022-09-26 05:32:02 -0400511 job_dismiss_locked(&job, &error_abort);
512 }
Max Reitzc2c731a2021-04-09 14:04:21 +0200513
Emanuele Giuseppe Esposito6f592e52022-09-26 05:32:11 -0400514 aio_context_acquire(ctx);
Max Reitzc2c731a2021-04-09 14:04:21 +0200515 destroy_blk(blk);
516 aio_context_release(ctx);
517 iothread_join(iothread);
518}
519
Alberto Garcia9ef81122016-07-29 17:31:41 +0300520int main(int argc, char **argv)
521{
522 qemu_init_main_loop(&error_abort);
Kevin Wolfd185cf02017-01-16 17:17:38 +0100523 bdrv_init();
Alberto Garcia9ef81122016-07-29 17:31:41 +0300524
525 g_test_init(&argc, &argv, NULL);
526 g_test_add_func("/blockjob/ids", test_job_ids);
John Snowfb367e02018-03-10 03:27:46 -0500527 g_test_add_func("/blockjob/cancel/created", test_cancel_created);
528 g_test_add_func("/blockjob/cancel/running", test_cancel_running);
529 g_test_add_func("/blockjob/cancel/paused", test_cancel_paused);
530 g_test_add_func("/blockjob/cancel/ready", test_cancel_ready);
531 g_test_add_func("/blockjob/cancel/standby", test_cancel_standby);
532 g_test_add_func("/blockjob/cancel/pending", test_cancel_pending);
533 g_test_add_func("/blockjob/cancel/concluded", test_cancel_concluded);
Peter Maydellb6903cb2023-03-17 14:35:34 +0000534
535 /*
536 * This test is flaky and sometimes fails in CI and otherwise:
537 * don't run unless user opts in via environment variable.
538 */
539 if (getenv("QEMU_TEST_FLAKY_TESTS")) {
540 g_test_add_func("/blockjob/complete_in_standby", test_complete_in_standby);
541 }
Alberto Garcia9ef81122016-07-29 17:31:41 +0300542 return g_test_run();
543}