xref: /qemu/tests/unit/test-blockjob.c (revision 6f592e5aca1a27fe1c1f661cfe68b35b90850acf)
19ef8112aSAlberto Garcia /*
29ef8112aSAlberto Garcia  * Blockjob tests
39ef8112aSAlberto Garcia  *
49ef8112aSAlberto Garcia  * Copyright Igalia, S.L. 2016
59ef8112aSAlberto Garcia  *
69ef8112aSAlberto Garcia  * Authors:
79ef8112aSAlberto Garcia  *  Alberto Garcia   <berto@igalia.com>
89ef8112aSAlberto Garcia  *
99ef8112aSAlberto Garcia  * This work is licensed under the terms of the GNU LGPL, version 2 or later.
109ef8112aSAlberto Garcia  * See the COPYING.LIB file in the top-level directory.
119ef8112aSAlberto Garcia  */
129ef8112aSAlberto Garcia 
139ef8112aSAlberto Garcia #include "qemu/osdep.h"
149ef8112aSAlberto Garcia #include "qapi/error.h"
159ef8112aSAlberto Garcia #include "qemu/main-loop.h"
16c87621eaSJohn Snow #include "block/blockjob_int.h"
179ef8112aSAlberto Garcia #include "sysemu/block-backend.h"
18ca1ef1e6SAndrey Shinkevich #include "qapi/qmp/qdict.h"
19c2c731a4SMax Reitz #include "iothread.h"
209ef8112aSAlberto Garcia 
219ef8112aSAlberto Garcia static const BlockJobDriver test_block_job_driver = {
2233e9e9bdSKevin Wolf     .job_driver = {
239ef8112aSAlberto Garcia         .instance_size = sizeof(BlockJob),
2480fa2c75SKevin Wolf         .free          = block_job_free,
25b15de828SKevin Wolf         .user_resume   = block_job_user_resume,
2633e9e9bdSKevin Wolf     },
279ef8112aSAlberto Garcia };
289ef8112aSAlberto Garcia 
299ef8112aSAlberto Garcia static void block_job_cb(void *opaque, int ret)
309ef8112aSAlberto Garcia {
319ef8112aSAlberto Garcia }
329ef8112aSAlberto Garcia 
33fb367e03SJohn Snow static BlockJob *mk_job(BlockBackend *blk, const char *id,
34fb367e03SJohn Snow                         const BlockJobDriver *drv, bool should_succeed,
35fb367e03SJohn Snow                         int flags)
369ef8112aSAlberto Garcia {
379ef8112aSAlberto Garcia     BlockJob *job;
388ca63ba8SMarkus Armbruster     Error *err = NULL;
399ef8112aSAlberto Garcia 
40fb367e03SJohn Snow     job = block_job_create(id, drv, NULL, blk_bs(blk),
41fb367e03SJohn Snow                            0, BLK_PERM_ALL, 0, flags, block_job_cb,
428ca63ba8SMarkus Armbruster                            NULL, &err);
439ef8112aSAlberto Garcia     if (should_succeed) {
448ca63ba8SMarkus Armbruster         g_assert_null(err);
459ef8112aSAlberto Garcia         g_assert_nonnull(job);
469ef8112aSAlberto Garcia         if (id) {
4733e9e9bdSKevin Wolf             g_assert_cmpstr(job->job.id, ==, id);
489ef8112aSAlberto Garcia         } else {
4933e9e9bdSKevin Wolf             g_assert_cmpstr(job->job.id, ==, blk_name(blk));
509ef8112aSAlberto Garcia         }
519ef8112aSAlberto Garcia     } else {
520cf9e2b4SMarkus Armbruster         error_free_or_abort(&err);
539ef8112aSAlberto Garcia         g_assert_null(job);
549ef8112aSAlberto Garcia     }
559ef8112aSAlberto Garcia 
569ef8112aSAlberto Garcia     return job;
579ef8112aSAlberto Garcia }
589ef8112aSAlberto Garcia 
59fb367e03SJohn Snow static BlockJob *do_test_id(BlockBackend *blk, const char *id,
60fb367e03SJohn Snow                             bool should_succeed)
61fb367e03SJohn Snow {
62fb367e03SJohn Snow     return mk_job(blk, id, &test_block_job_driver,
63bb02b65cSKevin Wolf                   should_succeed, JOB_DEFAULT);
64fb367e03SJohn Snow }
65fb367e03SJohn Snow 
669ef8112aSAlberto Garcia /* This creates a BlockBackend (optionally with a name) with a
679ef8112aSAlberto Garcia  * BlockDriverState inserted. */
689ef8112aSAlberto Garcia static BlockBackend *create_blk(const char *name)
699ef8112aSAlberto Garcia {
702807c0cdSKevin Wolf     /* No I/O is performed on this device */
71d861ab3aSKevin Wolf     BlockBackend *blk = blk_new(qemu_get_aio_context(), 0, BLK_PERM_ALL);
72d185cf0eSKevin Wolf     BlockDriverState *bs;
73d185cf0eSKevin Wolf 
74ca1ef1e6SAndrey Shinkevich     QDict *opt = qdict_new();
75ca1ef1e6SAndrey Shinkevich     qdict_put_str(opt, "file.read-zeroes", "on");
76ca1ef1e6SAndrey Shinkevich     bs = bdrv_open("null-co://", NULL, opt, 0, &error_abort);
77d185cf0eSKevin Wolf     g_assert_nonnull(bs);
789ef8112aSAlberto Garcia 
79d7086422SKevin Wolf     blk_insert_bs(blk, bs, &error_abort);
809ef8112aSAlberto Garcia     bdrv_unref(bs);
819ef8112aSAlberto Garcia 
829ef8112aSAlberto Garcia     if (name) {
838ca63ba8SMarkus Armbruster         Error *err = NULL;
848ca63ba8SMarkus Armbruster         monitor_add_blk(blk, name, &err);
858ca63ba8SMarkus Armbruster         g_assert_null(err);
869ef8112aSAlberto Garcia     }
879ef8112aSAlberto Garcia 
889ef8112aSAlberto Garcia     return blk;
899ef8112aSAlberto Garcia }
909ef8112aSAlberto Garcia 
919ef8112aSAlberto Garcia /* This destroys the backend */
929ef8112aSAlberto Garcia static void destroy_blk(BlockBackend *blk)
939ef8112aSAlberto Garcia {
949ef8112aSAlberto Garcia     if (blk_name(blk)[0] != '\0') {
959ef8112aSAlberto Garcia         monitor_remove_blk(blk);
969ef8112aSAlberto Garcia     }
979ef8112aSAlberto Garcia 
989ef8112aSAlberto Garcia     blk_remove_bs(blk);
999ef8112aSAlberto Garcia     blk_unref(blk);
1009ef8112aSAlberto Garcia }
1019ef8112aSAlberto Garcia 
1029ef8112aSAlberto Garcia static void test_job_ids(void)
1039ef8112aSAlberto Garcia {
1049ef8112aSAlberto Garcia     BlockBackend *blk[3];
1059ef8112aSAlberto Garcia     BlockJob *job[3];
1069ef8112aSAlberto Garcia 
1079ef8112aSAlberto Garcia     blk[0] = create_blk(NULL);
1089ef8112aSAlberto Garcia     blk[1] = create_blk("drive1");
1099ef8112aSAlberto Garcia     blk[2] = create_blk("drive2");
1109ef8112aSAlberto Garcia 
1119ef8112aSAlberto Garcia     /* No job ID provided and the block backend has no name */
1129ef8112aSAlberto Garcia     job[0] = do_test_id(blk[0], NULL, false);
1139ef8112aSAlberto Garcia 
1149ef8112aSAlberto Garcia     /* These are all invalid job IDs */
1159ef8112aSAlberto Garcia     job[0] = do_test_id(blk[0], "0id", false);
1169ef8112aSAlberto Garcia     job[0] = do_test_id(blk[0], "",    false);
1179ef8112aSAlberto Garcia     job[0] = do_test_id(blk[0], "   ", false);
1189ef8112aSAlberto Garcia     job[0] = do_test_id(blk[0], "123", false);
1199ef8112aSAlberto Garcia     job[0] = do_test_id(blk[0], "_id", false);
1209ef8112aSAlberto Garcia     job[0] = do_test_id(blk[0], "-id", false);
1219ef8112aSAlberto Garcia     job[0] = do_test_id(blk[0], ".id", false);
1229ef8112aSAlberto Garcia     job[0] = do_test_id(blk[0], "#id", false);
1239ef8112aSAlberto Garcia 
1249ef8112aSAlberto Garcia     /* This one is valid */
1259ef8112aSAlberto Garcia     job[0] = do_test_id(blk[0], "id0", true);
1269ef8112aSAlberto Garcia 
127b23c580cSVladimir Sementsov-Ogievskiy     /* We can have two jobs in the same BDS */
128b23c580cSVladimir Sementsov-Ogievskiy     job[1] = do_test_id(blk[0], "id1", true);
129b23c580cSVladimir Sementsov-Ogievskiy     job_early_fail(&job[1]->job);
1309ef8112aSAlberto Garcia 
1319ef8112aSAlberto Garcia     /* Duplicate job IDs are not allowed */
1329ef8112aSAlberto Garcia     job[1] = do_test_id(blk[1], "id0", false);
1339ef8112aSAlberto Garcia 
1349ef8112aSAlberto Garcia     /* But once job[0] finishes we can reuse its ID */
1354ad35181SKevin Wolf     job_early_fail(&job[0]->job);
1369ef8112aSAlberto Garcia     job[1] = do_test_id(blk[1], "id0", true);
1379ef8112aSAlberto Garcia 
1389ef8112aSAlberto Garcia     /* No job ID specified, defaults to the backend name ('drive1') */
1394ad35181SKevin Wolf     job_early_fail(&job[1]->job);
1409ef8112aSAlberto Garcia     job[1] = do_test_id(blk[1], NULL, true);
1419ef8112aSAlberto Garcia 
1429ef8112aSAlberto Garcia     /* Duplicate job ID */
1439ef8112aSAlberto Garcia     job[2] = do_test_id(blk[2], "drive1", false);
1449ef8112aSAlberto Garcia 
1459ef8112aSAlberto Garcia     /* The ID of job[2] would default to 'drive2' but it is already in use */
1469ef8112aSAlberto Garcia     job[0] = do_test_id(blk[0], "drive2", true);
1479ef8112aSAlberto Garcia     job[2] = do_test_id(blk[2], NULL, false);
1489ef8112aSAlberto Garcia 
1499ef8112aSAlberto Garcia     /* This one is valid */
1509ef8112aSAlberto Garcia     job[2] = do_test_id(blk[2], "id_2", true);
1519ef8112aSAlberto Garcia 
1524ad35181SKevin Wolf     job_early_fail(&job[0]->job);
1534ad35181SKevin Wolf     job_early_fail(&job[1]->job);
1544ad35181SKevin Wolf     job_early_fail(&job[2]->job);
1559ef8112aSAlberto Garcia 
1569ef8112aSAlberto Garcia     destroy_blk(blk[0]);
1579ef8112aSAlberto Garcia     destroy_blk(blk[1]);
1589ef8112aSAlberto Garcia     destroy_blk(blk[2]);
1599ef8112aSAlberto Garcia }
1609ef8112aSAlberto Garcia 
161fb367e03SJohn Snow typedef struct CancelJob {
162fb367e03SJohn Snow     BlockJob common;
163fb367e03SJohn Snow     BlockBackend *blk;
164fb367e03SJohn Snow     bool should_converge;
165fb367e03SJohn Snow     bool should_complete;
166fb367e03SJohn Snow } CancelJob;
167fb367e03SJohn Snow 
1683453d972SKevin Wolf static void cancel_job_complete(Job *job, Error **errp)
169fb367e03SJohn Snow {
1703453d972SKevin Wolf     CancelJob *s = container_of(job, CancelJob, common.job);
171fb367e03SJohn Snow     s->should_complete = true;
172fb367e03SJohn Snow }
173fb367e03SJohn Snow 
174f67432a2SJohn Snow static int coroutine_fn cancel_job_run(Job *job, Error **errp)
175fb367e03SJohn Snow {
176f67432a2SJohn Snow     CancelJob *s = container_of(job, CancelJob, common.job);
177fb367e03SJohn Snow 
178fb367e03SJohn Snow     while (!s->should_complete) {
179daa7f2f9SKevin Wolf         if (job_is_cancelled(&s->common.job)) {
180eb23654dSJohn Snow             return 0;
181fb367e03SJohn Snow         }
182fb367e03SJohn Snow 
183df956ae2SKevin Wolf         if (!job_is_ready(&s->common.job) && s->should_converge) {
1842e1795b5SKevin Wolf             job_transition_to_ready(&s->common.job);
185fb367e03SJohn Snow         }
186fb367e03SJohn Snow 
1875d43e86eSKevin Wolf         job_sleep_ns(&s->common.job, 100000);
188fb367e03SJohn Snow     }
189fb367e03SJohn Snow 
190f67432a2SJohn Snow     return 0;
191fb367e03SJohn Snow }
192fb367e03SJohn Snow 
193fb367e03SJohn Snow static const BlockJobDriver test_cancel_driver = {
19433e9e9bdSKevin Wolf     .job_driver = {
195fb367e03SJohn Snow         .instance_size = sizeof(CancelJob),
19680fa2c75SKevin Wolf         .free          = block_job_free,
197b15de828SKevin Wolf         .user_resume   = block_job_user_resume,
198f67432a2SJohn Snow         .run           = cancel_job_run,
199fb367e03SJohn Snow         .complete      = cancel_job_complete,
2003453d972SKevin Wolf     },
201fb367e03SJohn Snow };
202fb367e03SJohn Snow 
2030cc4643bSJohn Snow static CancelJob *create_common(Job **pjob)
204fb367e03SJohn Snow {
205fb367e03SJohn Snow     BlockBackend *blk;
2060cc4643bSJohn Snow     Job *job;
2070cc4643bSJohn Snow     BlockJob *bjob;
208fb367e03SJohn Snow     CancelJob *s;
209fb367e03SJohn Snow 
210fb367e03SJohn Snow     blk = create_blk(NULL);
2110cc4643bSJohn Snow     bjob = mk_job(blk, "Steve", &test_cancel_driver, true,
212bb02b65cSKevin Wolf                   JOB_MANUAL_FINALIZE | JOB_MANUAL_DISMISS);
2130cc4643bSJohn Snow     job = &bjob->job;
214191e7af3SEmanuele Giuseppe Esposito     WITH_JOB_LOCK_GUARD() {
215191e7af3SEmanuele Giuseppe Esposito         job_ref_locked(job);
2160cc4643bSJohn Snow         assert(job->status == JOB_STATUS_CREATED);
217191e7af3SEmanuele Giuseppe Esposito     }
218191e7af3SEmanuele Giuseppe Esposito 
2190cc4643bSJohn Snow     s = container_of(bjob, CancelJob, common);
220fb367e03SJohn Snow     s->blk = blk;
221fb367e03SJohn Snow 
222fb367e03SJohn Snow     *pjob = job;
223fb367e03SJohn Snow     return s;
224fb367e03SJohn Snow }
225fb367e03SJohn Snow 
226fb367e03SJohn Snow static void cancel_common(CancelJob *s)
227fb367e03SJohn Snow {
228fb367e03SJohn Snow     BlockJob *job = &s->common;
229fb367e03SJohn Snow     BlockBackend *blk = s->blk;
230a50c2ab8SKevin Wolf     JobStatus sts = job->job.status;
231*6f592e5aSEmanuele Giuseppe Esposito     AioContext *ctx = job->job.aio_context;
232fb367e03SJohn Snow 
2334cfb3f05SHanna Reitz     job_cancel_sync(&job->job, true);
234191e7af3SEmanuele Giuseppe Esposito     WITH_JOB_LOCK_GUARD() {
235a50c2ab8SKevin Wolf         if (sts != JOB_STATUS_CREATED && sts != JOB_STATUS_CONCLUDED) {
2365f9a6a08SKevin Wolf             Job *dummy = &job->job;
237191e7af3SEmanuele Giuseppe Esposito             job_dismiss_locked(&dummy, &error_abort);
238fb367e03SJohn Snow         }
239a50c2ab8SKevin Wolf         assert(job->job.status == JOB_STATUS_NULL);
240191e7af3SEmanuele Giuseppe Esposito         job_unref_locked(&job->job);
241191e7af3SEmanuele Giuseppe Esposito     }
24230c070a5SKevin Wolf 
243*6f592e5aSEmanuele Giuseppe Esposito     aio_context_acquire(ctx);
244*6f592e5aSEmanuele Giuseppe Esposito     destroy_blk(blk);
24530c070a5SKevin Wolf     aio_context_release(ctx);
246*6f592e5aSEmanuele Giuseppe Esposito 
247fb367e03SJohn Snow }
248fb367e03SJohn Snow 
249fb367e03SJohn Snow static void test_cancel_created(void)
250fb367e03SJohn Snow {
2510cc4643bSJohn Snow     Job *job;
252fb367e03SJohn Snow     CancelJob *s;
253fb367e03SJohn Snow 
254fb367e03SJohn Snow     s = create_common(&job);
255fb367e03SJohn Snow     cancel_common(s);
256fb367e03SJohn Snow }
257fb367e03SJohn Snow 
258191e7af3SEmanuele Giuseppe Esposito static void assert_job_status_is(Job *job, int status)
259191e7af3SEmanuele Giuseppe Esposito {
260191e7af3SEmanuele Giuseppe Esposito     WITH_JOB_LOCK_GUARD() {
261191e7af3SEmanuele Giuseppe Esposito         assert(job->status == status);
262191e7af3SEmanuele Giuseppe Esposito     }
263191e7af3SEmanuele Giuseppe Esposito }
264191e7af3SEmanuele Giuseppe Esposito 
265fb367e03SJohn Snow static void test_cancel_running(void)
266fb367e03SJohn Snow {
2670cc4643bSJohn Snow     Job *job;
268fb367e03SJohn Snow     CancelJob *s;
269fb367e03SJohn Snow 
270fb367e03SJohn Snow     s = create_common(&job);
271fb367e03SJohn Snow 
2720cc4643bSJohn Snow     job_start(job);
273191e7af3SEmanuele Giuseppe Esposito     assert_job_status_is(job, JOB_STATUS_RUNNING);
274fb367e03SJohn Snow 
275fb367e03SJohn Snow     cancel_common(s);
276fb367e03SJohn Snow }
277fb367e03SJohn Snow 
278fb367e03SJohn Snow static void test_cancel_paused(void)
279fb367e03SJohn Snow {
2800cc4643bSJohn Snow     Job *job;
281fb367e03SJohn Snow     CancelJob *s;
282fb367e03SJohn Snow 
283fb367e03SJohn Snow     s = create_common(&job);
284fb367e03SJohn Snow 
2850cc4643bSJohn Snow     job_start(job);
286191e7af3SEmanuele Giuseppe Esposito     WITH_JOB_LOCK_GUARD() {
2870cc4643bSJohn Snow         assert(job->status == JOB_STATUS_RUNNING);
288191e7af3SEmanuele Giuseppe Esposito         job_user_pause_locked(job, &error_abort);
289191e7af3SEmanuele Giuseppe Esposito     }
2900cc4643bSJohn Snow     job_enter(job);
291191e7af3SEmanuele Giuseppe Esposito     assert_job_status_is(job, JOB_STATUS_PAUSED);
292fb367e03SJohn Snow 
293fb367e03SJohn Snow     cancel_common(s);
294fb367e03SJohn Snow }
295fb367e03SJohn Snow 
296fb367e03SJohn Snow static void test_cancel_ready(void)
297fb367e03SJohn Snow {
2980cc4643bSJohn Snow     Job *job;
299fb367e03SJohn Snow     CancelJob *s;
300fb367e03SJohn Snow 
301fb367e03SJohn Snow     s = create_common(&job);
302fb367e03SJohn Snow 
3030cc4643bSJohn Snow     job_start(job);
304191e7af3SEmanuele Giuseppe Esposito     assert_job_status_is(job, JOB_STATUS_RUNNING);
305fb367e03SJohn Snow 
306fb367e03SJohn Snow     s->should_converge = true;
3070cc4643bSJohn Snow     job_enter(job);
308191e7af3SEmanuele Giuseppe Esposito     assert_job_status_is(job, JOB_STATUS_READY);
309fb367e03SJohn Snow 
310fb367e03SJohn Snow     cancel_common(s);
311fb367e03SJohn Snow }
312fb367e03SJohn Snow 
313fb367e03SJohn Snow static void test_cancel_standby(void)
314fb367e03SJohn Snow {
3150cc4643bSJohn Snow     Job *job;
316fb367e03SJohn Snow     CancelJob *s;
317fb367e03SJohn Snow 
318fb367e03SJohn Snow     s = create_common(&job);
319fb367e03SJohn Snow 
3200cc4643bSJohn Snow     job_start(job);
321191e7af3SEmanuele Giuseppe Esposito     assert_job_status_is(job, JOB_STATUS_RUNNING);
322fb367e03SJohn Snow 
323fb367e03SJohn Snow     s->should_converge = true;
3240cc4643bSJohn Snow     job_enter(job);
325191e7af3SEmanuele Giuseppe Esposito     WITH_JOB_LOCK_GUARD() {
3260cc4643bSJohn Snow         assert(job->status == JOB_STATUS_READY);
327191e7af3SEmanuele Giuseppe Esposito         job_user_pause_locked(job, &error_abort);
328191e7af3SEmanuele Giuseppe Esposito     }
3290cc4643bSJohn Snow     job_enter(job);
330191e7af3SEmanuele Giuseppe Esposito     assert_job_status_is(job, JOB_STATUS_STANDBY);
331fb367e03SJohn Snow 
332fb367e03SJohn Snow     cancel_common(s);
333fb367e03SJohn Snow }
334fb367e03SJohn Snow 
335fb367e03SJohn Snow static void test_cancel_pending(void)
336fb367e03SJohn Snow {
3370cc4643bSJohn Snow     Job *job;
338fb367e03SJohn Snow     CancelJob *s;
339fb367e03SJohn Snow 
340fb367e03SJohn Snow     s = create_common(&job);
341fb367e03SJohn Snow 
3420cc4643bSJohn Snow     job_start(job);
343191e7af3SEmanuele Giuseppe Esposito     assert_job_status_is(job, JOB_STATUS_RUNNING);
344fb367e03SJohn Snow 
345fb367e03SJohn Snow     s->should_converge = true;
3460cc4643bSJohn Snow     job_enter(job);
347191e7af3SEmanuele Giuseppe Esposito     WITH_JOB_LOCK_GUARD() {
3480cc4643bSJohn Snow         assert(job->status == JOB_STATUS_READY);
349191e7af3SEmanuele Giuseppe Esposito         job_complete_locked(job, &error_abort);
350191e7af3SEmanuele Giuseppe Esposito     }
3510cc4643bSJohn Snow     job_enter(job);
352977d26fdSJohn Snow     while (!job->deferred_to_main_loop) {
353fb367e03SJohn Snow         aio_poll(qemu_get_aio_context(), true);
354fb367e03SJohn Snow     }
355191e7af3SEmanuele Giuseppe Esposito     assert_job_status_is(job, JOB_STATUS_READY);
356977d26fdSJohn Snow     aio_poll(qemu_get_aio_context(), true);
357191e7af3SEmanuele Giuseppe Esposito     assert_job_status_is(job, JOB_STATUS_PENDING);
358fb367e03SJohn Snow 
359fb367e03SJohn Snow     cancel_common(s);
360fb367e03SJohn Snow }
361fb367e03SJohn Snow 
362fb367e03SJohn Snow static void test_cancel_concluded(void)
363fb367e03SJohn Snow {
3640cc4643bSJohn Snow     Job *job;
365fb367e03SJohn Snow     CancelJob *s;
366fb367e03SJohn Snow 
367fb367e03SJohn Snow     s = create_common(&job);
368fb367e03SJohn Snow 
3690cc4643bSJohn Snow     job_start(job);
370191e7af3SEmanuele Giuseppe Esposito     assert_job_status_is(job, JOB_STATUS_RUNNING);
371fb367e03SJohn Snow 
372fb367e03SJohn Snow     s->should_converge = true;
3730cc4643bSJohn Snow     job_enter(job);
374191e7af3SEmanuele Giuseppe Esposito     WITH_JOB_LOCK_GUARD() {
3750cc4643bSJohn Snow         assert(job->status == JOB_STATUS_READY);
376191e7af3SEmanuele Giuseppe Esposito         job_complete_locked(job, &error_abort);
377191e7af3SEmanuele Giuseppe Esposito     }
3780cc4643bSJohn Snow     job_enter(job);
379977d26fdSJohn Snow     while (!job->deferred_to_main_loop) {
380fb367e03SJohn Snow         aio_poll(qemu_get_aio_context(), true);
381fb367e03SJohn Snow     }
382191e7af3SEmanuele Giuseppe Esposito     assert_job_status_is(job, JOB_STATUS_READY);
383977d26fdSJohn Snow     aio_poll(qemu_get_aio_context(), true);
384191e7af3SEmanuele Giuseppe Esposito     assert_job_status_is(job, JOB_STATUS_PENDING);
385fb367e03SJohn Snow 
386191e7af3SEmanuele Giuseppe Esposito     WITH_JOB_LOCK_GUARD() {
387191e7af3SEmanuele Giuseppe Esposito         job_finalize_locked(job, &error_abort);
388*6f592e5aSEmanuele Giuseppe Esposito         assert(job->status == JOB_STATUS_CONCLUDED);
389191e7af3SEmanuele Giuseppe Esposito     }
390fb367e03SJohn Snow 
391fb367e03SJohn Snow     cancel_common(s);
392fb367e03SJohn Snow }
393fb367e03SJohn Snow 
394c2c731a4SMax Reitz /* (See test_yielding_driver for the job description) */
395c2c731a4SMax Reitz typedef struct YieldingJob {
396c2c731a4SMax Reitz     BlockJob common;
397c2c731a4SMax Reitz     bool should_complete;
398c2c731a4SMax Reitz } YieldingJob;
399c2c731a4SMax Reitz 
400c2c731a4SMax Reitz static void yielding_job_complete(Job *job, Error **errp)
401c2c731a4SMax Reitz {
402c2c731a4SMax Reitz     YieldingJob *s = container_of(job, YieldingJob, common.job);
403c2c731a4SMax Reitz     s->should_complete = true;
404c2c731a4SMax Reitz     job_enter(job);
405c2c731a4SMax Reitz }
406c2c731a4SMax Reitz 
407c2c731a4SMax Reitz static int coroutine_fn yielding_job_run(Job *job, Error **errp)
408c2c731a4SMax Reitz {
409c2c731a4SMax Reitz     YieldingJob *s = container_of(job, YieldingJob, common.job);
410c2c731a4SMax Reitz 
411c2c731a4SMax Reitz     job_transition_to_ready(job);
412c2c731a4SMax Reitz 
413c2c731a4SMax Reitz     while (!s->should_complete) {
414c2c731a4SMax Reitz         job_yield(job);
415c2c731a4SMax Reitz     }
416c2c731a4SMax Reitz 
417c2c731a4SMax Reitz     return 0;
418c2c731a4SMax Reitz }
419c2c731a4SMax Reitz 
420c2c731a4SMax Reitz /*
421c2c731a4SMax Reitz  * This job transitions immediately to the READY state, and then
422c2c731a4SMax Reitz  * yields until it is to complete.
423c2c731a4SMax Reitz  */
424c2c731a4SMax Reitz static const BlockJobDriver test_yielding_driver = {
425c2c731a4SMax Reitz     .job_driver = {
426c2c731a4SMax Reitz         .instance_size  = sizeof(YieldingJob),
427c2c731a4SMax Reitz         .free           = block_job_free,
428c2c731a4SMax Reitz         .user_resume    = block_job_user_resume,
429c2c731a4SMax Reitz         .run            = yielding_job_run,
430c2c731a4SMax Reitz         .complete       = yielding_job_complete,
431c2c731a4SMax Reitz     },
432c2c731a4SMax Reitz };
433c2c731a4SMax Reitz 
434c2c731a4SMax Reitz /*
435c2c731a4SMax Reitz  * Test that job_complete() works even on jobs that are in a paused
436c2c731a4SMax Reitz  * state (i.e., STANDBY).
437c2c731a4SMax Reitz  *
438c2c731a4SMax Reitz  * To do this, run YieldingJob in an IO thread, get it into the READY
439c2c731a4SMax Reitz  * state, then have a drained section.  Before ending the section,
440c2c731a4SMax Reitz  * acquire the context so the job will not be entered and will thus
441c2c731a4SMax Reitz  * remain on STANDBY.
442c2c731a4SMax Reitz  *
443c2c731a4SMax Reitz  * job_complete() should still work without error.
444c2c731a4SMax Reitz  *
445c2c731a4SMax Reitz  * Note that on the QMP interface, it is impossible to lock an IO
446c2c731a4SMax Reitz  * thread before a drained section ends.  In practice, the
447c2c731a4SMax Reitz  * bdrv_drain_all_end() and the aio_context_acquire() will be
448c2c731a4SMax Reitz  * reversed.  However, that makes for worse reproducibility here:
449c2c731a4SMax Reitz  * Sometimes, the job would no longer be in STANDBY then but already
450c2c731a4SMax Reitz  * be started.  We cannot prevent that, because the IO thread runs
451c2c731a4SMax Reitz  * concurrently.  We can only prevent it by taking the lock before
452c2c731a4SMax Reitz  * ending the drained section, so we do that.
453c2c731a4SMax Reitz  *
454c2c731a4SMax Reitz  * (You can reverse the order of operations and most of the time the
455c2c731a4SMax Reitz  * test will pass, but sometimes the assert(status == STANDBY) will
456c2c731a4SMax Reitz  * fail.)
457c2c731a4SMax Reitz  */
458c2c731a4SMax Reitz static void test_complete_in_standby(void)
459c2c731a4SMax Reitz {
460c2c731a4SMax Reitz     BlockBackend *blk;
461c2c731a4SMax Reitz     IOThread *iothread;
462c2c731a4SMax Reitz     AioContext *ctx;
463c2c731a4SMax Reitz     Job *job;
464c2c731a4SMax Reitz     BlockJob *bjob;
465c2c731a4SMax Reitz 
466c2c731a4SMax Reitz     /* Create a test drive, move it to an IO thread */
467c2c731a4SMax Reitz     blk = create_blk(NULL);
468c2c731a4SMax Reitz     iothread = iothread_new();
469c2c731a4SMax Reitz 
470c2c731a4SMax Reitz     ctx = iothread_get_aio_context(iothread);
471c2c731a4SMax Reitz     blk_set_aio_context(blk, ctx, &error_abort);
472c2c731a4SMax Reitz 
473c2c731a4SMax Reitz     /* Create our test job */
474c2c731a4SMax Reitz     bjob = mk_job(blk, "job", &test_yielding_driver, true,
475c2c731a4SMax Reitz                   JOB_MANUAL_FINALIZE | JOB_MANUAL_DISMISS);
476c2c731a4SMax Reitz     job = &bjob->job;
477191e7af3SEmanuele Giuseppe Esposito     assert_job_status_is(job, JOB_STATUS_CREATED);
478c2c731a4SMax Reitz 
479c2c731a4SMax Reitz     /* Wait for the job to become READY */
480c2c731a4SMax Reitz     job_start(job);
481191e7af3SEmanuele Giuseppe Esposito     /*
482191e7af3SEmanuele Giuseppe Esposito      * Here we are waiting for the status to change, so don't bother
483191e7af3SEmanuele Giuseppe Esposito      * protecting the read every time.
484191e7af3SEmanuele Giuseppe Esposito      */
485*6f592e5aSEmanuele Giuseppe Esposito     AIO_WAIT_WHILE_UNLOCKED(ctx, job->status != JOB_STATUS_READY);
486c2c731a4SMax Reitz 
487c2c731a4SMax Reitz     /* Begin the drained section, pausing the job */
488c2c731a4SMax Reitz     bdrv_drain_all_begin();
489191e7af3SEmanuele Giuseppe Esposito     assert_job_status_is(job, JOB_STATUS_STANDBY);
490191e7af3SEmanuele Giuseppe Esposito 
491c2c731a4SMax Reitz     /* Lock the IO thread to prevent the job from being run */
492c2c731a4SMax Reitz     aio_context_acquire(ctx);
493c2c731a4SMax Reitz     /* This will schedule the job to resume it */
494c2c731a4SMax Reitz     bdrv_drain_all_end();
495*6f592e5aSEmanuele Giuseppe Esposito     aio_context_release(ctx);
496c2c731a4SMax Reitz 
497191e7af3SEmanuele Giuseppe Esposito     WITH_JOB_LOCK_GUARD() {
498c2c731a4SMax Reitz         /* But the job cannot run, so it will remain on standby */
499c2c731a4SMax Reitz         assert(job->status == JOB_STATUS_STANDBY);
500c2c731a4SMax Reitz 
501c2c731a4SMax Reitz         /* Even though the job is on standby, this should work */
502191e7af3SEmanuele Giuseppe Esposito         job_complete_locked(job, &error_abort);
503c2c731a4SMax Reitz 
504c2c731a4SMax Reitz         /* The test is done now, clean up. */
505191e7af3SEmanuele Giuseppe Esposito         job_finish_sync_locked(job, NULL, &error_abort);
506c2c731a4SMax Reitz         assert(job->status == JOB_STATUS_PENDING);
507c2c731a4SMax Reitz 
508191e7af3SEmanuele Giuseppe Esposito         job_finalize_locked(job, &error_abort);
509c2c731a4SMax Reitz         assert(job->status == JOB_STATUS_CONCLUDED);
510c2c731a4SMax Reitz 
511191e7af3SEmanuele Giuseppe Esposito         job_dismiss_locked(&job, &error_abort);
512191e7af3SEmanuele Giuseppe Esposito     }
513c2c731a4SMax Reitz 
514*6f592e5aSEmanuele Giuseppe Esposito     aio_context_acquire(ctx);
515c2c731a4SMax Reitz     destroy_blk(blk);
516c2c731a4SMax Reitz     aio_context_release(ctx);
517c2c731a4SMax Reitz     iothread_join(iothread);
518c2c731a4SMax Reitz }
519c2c731a4SMax Reitz 
5209ef8112aSAlberto Garcia int main(int argc, char **argv)
5219ef8112aSAlberto Garcia {
5229ef8112aSAlberto Garcia     qemu_init_main_loop(&error_abort);
523d185cf0eSKevin Wolf     bdrv_init();
5249ef8112aSAlberto Garcia 
5259ef8112aSAlberto Garcia     g_test_init(&argc, &argv, NULL);
5269ef8112aSAlberto Garcia     g_test_add_func("/blockjob/ids", test_job_ids);
527fb367e03SJohn Snow     g_test_add_func("/blockjob/cancel/created", test_cancel_created);
528fb367e03SJohn Snow     g_test_add_func("/blockjob/cancel/running", test_cancel_running);
529fb367e03SJohn Snow     g_test_add_func("/blockjob/cancel/paused", test_cancel_paused);
530fb367e03SJohn Snow     g_test_add_func("/blockjob/cancel/ready", test_cancel_ready);
531fb367e03SJohn Snow     g_test_add_func("/blockjob/cancel/standby", test_cancel_standby);
532fb367e03SJohn Snow     g_test_add_func("/blockjob/cancel/pending", test_cancel_pending);
533fb367e03SJohn Snow     g_test_add_func("/blockjob/cancel/concluded", test_cancel_concluded);
534c2c731a4SMax Reitz     g_test_add_func("/blockjob/complete_in_standby", test_complete_in_standby);
5359ef8112aSAlberto Garcia     return g_test_run();
5369ef8112aSAlberto Garcia }
537