xref: /qemu/job.c (revision 08be6fe2)
133e9e9bdSKevin Wolf /*
233e9e9bdSKevin Wolf  * Background jobs (long-running operations)
333e9e9bdSKevin Wolf  *
433e9e9bdSKevin Wolf  * Copyright (c) 2011 IBM Corp.
533e9e9bdSKevin Wolf  * Copyright (c) 2012, 2018 Red Hat, Inc.
633e9e9bdSKevin Wolf  *
733e9e9bdSKevin Wolf  * Permission is hereby granted, free of charge, to any person obtaining a copy
833e9e9bdSKevin Wolf  * of this software and associated documentation files (the "Software"), to deal
933e9e9bdSKevin Wolf  * in the Software without restriction, including without limitation the rights
1033e9e9bdSKevin Wolf  * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
1133e9e9bdSKevin Wolf  * copies of the Software, and to permit persons to whom the Software is
1233e9e9bdSKevin Wolf  * furnished to do so, subject to the following conditions:
1333e9e9bdSKevin Wolf  *
1433e9e9bdSKevin Wolf  * The above copyright notice and this permission notice shall be included in
1533e9e9bdSKevin Wolf  * all copies or substantial portions of the Software.
1633e9e9bdSKevin Wolf  *
1733e9e9bdSKevin Wolf  * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
1833e9e9bdSKevin Wolf  * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
1933e9e9bdSKevin Wolf  * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
2033e9e9bdSKevin Wolf  * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
2133e9e9bdSKevin Wolf  * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
2233e9e9bdSKevin Wolf  * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
2333e9e9bdSKevin Wolf  * THE SOFTWARE.
2433e9e9bdSKevin Wolf  */
2533e9e9bdSKevin Wolf 
2633e9e9bdSKevin Wolf #include "qemu/osdep.h"
2733e9e9bdSKevin Wolf #include "qemu-common.h"
2833e9e9bdSKevin Wolf #include "qapi/error.h"
2933e9e9bdSKevin Wolf #include "qemu/job.h"
3033e9e9bdSKevin Wolf #include "qemu/id.h"
31a50c2ab8SKevin Wolf #include "trace-root.h"
3233e9e9bdSKevin Wolf 
33e7c1d78bSKevin Wolf static QLIST_HEAD(, Job) jobs = QLIST_HEAD_INITIALIZER(jobs);
34e7c1d78bSKevin Wolf 
35a50c2ab8SKevin Wolf /* Job State Transition Table */
36a50c2ab8SKevin Wolf bool JobSTT[JOB_STATUS__MAX][JOB_STATUS__MAX] = {
37a50c2ab8SKevin Wolf                                     /* U, C, R, P, Y, S, W, D, X, E, N */
38a50c2ab8SKevin Wolf     /* U: */ [JOB_STATUS_UNDEFINED] = {0, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0},
39a50c2ab8SKevin Wolf     /* C: */ [JOB_STATUS_CREATED]   = {0, 0, 1, 0, 0, 0, 0, 0, 1, 0, 1},
40a50c2ab8SKevin Wolf     /* R: */ [JOB_STATUS_RUNNING]   = {0, 0, 0, 1, 1, 0, 1, 0, 1, 0, 0},
41a50c2ab8SKevin Wolf     /* P: */ [JOB_STATUS_PAUSED]    = {0, 0, 1, 0, 0, 0, 0, 0, 0, 0, 0},
42a50c2ab8SKevin Wolf     /* Y: */ [JOB_STATUS_READY]     = {0, 0, 0, 0, 0, 1, 1, 0, 1, 0, 0},
43a50c2ab8SKevin Wolf     /* S: */ [JOB_STATUS_STANDBY]   = {0, 0, 0, 0, 1, 0, 0, 0, 0, 0, 0},
44a50c2ab8SKevin Wolf     /* W: */ [JOB_STATUS_WAITING]   = {0, 0, 0, 0, 0, 0, 0, 1, 1, 0, 0},
45a50c2ab8SKevin Wolf     /* D: */ [JOB_STATUS_PENDING]   = {0, 0, 0, 0, 0, 0, 0, 0, 1, 1, 0},
46a50c2ab8SKevin Wolf     /* X: */ [JOB_STATUS_ABORTING]  = {0, 0, 0, 0, 0, 0, 0, 0, 1, 1, 0},
47a50c2ab8SKevin Wolf     /* E: */ [JOB_STATUS_CONCLUDED] = {0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1},
48a50c2ab8SKevin Wolf     /* N: */ [JOB_STATUS_NULL]      = {0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0},
49a50c2ab8SKevin Wolf };
50a50c2ab8SKevin Wolf 
51a50c2ab8SKevin Wolf bool JobVerbTable[JOB_VERB__MAX][JOB_STATUS__MAX] = {
52a50c2ab8SKevin Wolf                                     /* U, C, R, P, Y, S, W, D, X, E, N */
53a50c2ab8SKevin Wolf     [JOB_VERB_CANCEL]               = {0, 1, 1, 1, 1, 1, 1, 1, 0, 0, 0},
54a50c2ab8SKevin Wolf     [JOB_VERB_PAUSE]                = {0, 1, 1, 1, 1, 1, 0, 0, 0, 0, 0},
55a50c2ab8SKevin Wolf     [JOB_VERB_RESUME]               = {0, 1, 1, 1, 1, 1, 0, 0, 0, 0, 0},
56a50c2ab8SKevin Wolf     [JOB_VERB_SET_SPEED]            = {0, 1, 1, 1, 1, 1, 0, 0, 0, 0, 0},
57a50c2ab8SKevin Wolf     [JOB_VERB_COMPLETE]             = {0, 0, 0, 0, 1, 0, 0, 0, 0, 0, 0},
58a50c2ab8SKevin Wolf     [JOB_VERB_FINALIZE]             = {0, 0, 0, 0, 0, 0, 0, 1, 0, 0, 0},
59a50c2ab8SKevin Wolf     [JOB_VERB_DISMISS]              = {0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 0},
60a50c2ab8SKevin Wolf };
61a50c2ab8SKevin Wolf 
62a50c2ab8SKevin Wolf /* TODO Make static once the whole state machine is in job.c */
63a50c2ab8SKevin Wolf void job_state_transition(Job *job, JobStatus s1)
64a50c2ab8SKevin Wolf {
65a50c2ab8SKevin Wolf     JobStatus s0 = job->status;
66a50c2ab8SKevin Wolf     assert(s1 >= 0 && s1 <= JOB_STATUS__MAX);
67a50c2ab8SKevin Wolf     trace_job_state_transition(job, /* TODO re-enable: job->ret */ 0,
68a50c2ab8SKevin Wolf                                JobSTT[s0][s1] ? "allowed" : "disallowed",
69a50c2ab8SKevin Wolf                                JobStatus_str(s0), JobStatus_str(s1));
70a50c2ab8SKevin Wolf     assert(JobSTT[s0][s1]);
71a50c2ab8SKevin Wolf     job->status = s1;
72a50c2ab8SKevin Wolf }
73a50c2ab8SKevin Wolf 
74a50c2ab8SKevin Wolf int job_apply_verb(Job *job, JobVerb verb, Error **errp)
75a50c2ab8SKevin Wolf {
76a50c2ab8SKevin Wolf     JobStatus s0 = job->status;
77a50c2ab8SKevin Wolf     assert(verb >= 0 && verb <= JOB_VERB__MAX);
78a50c2ab8SKevin Wolf     trace_job_apply_verb(job, JobStatus_str(s0), JobVerb_str(verb),
79a50c2ab8SKevin Wolf                          JobVerbTable[verb][s0] ? "allowed" : "prohibited");
80a50c2ab8SKevin Wolf     if (JobVerbTable[verb][s0]) {
81a50c2ab8SKevin Wolf         return 0;
82a50c2ab8SKevin Wolf     }
83a50c2ab8SKevin Wolf     error_setg(errp, "Job '%s' in state '%s' cannot accept command verb '%s'",
84a50c2ab8SKevin Wolf                job->id, JobStatus_str(s0), JobVerb_str(verb));
85a50c2ab8SKevin Wolf     return -EPERM;
86a50c2ab8SKevin Wolf }
87a50c2ab8SKevin Wolf 
88252291eaSKevin Wolf JobType job_type(const Job *job)
89252291eaSKevin Wolf {
90252291eaSKevin Wolf     return job->driver->job_type;
91252291eaSKevin Wolf }
92252291eaSKevin Wolf 
93252291eaSKevin Wolf const char *job_type_str(const Job *job)
94252291eaSKevin Wolf {
95252291eaSKevin Wolf     return JobType_str(job_type(job));
96252291eaSKevin Wolf }
97252291eaSKevin Wolf 
98daa7f2f9SKevin Wolf bool job_is_cancelled(Job *job)
99daa7f2f9SKevin Wolf {
100daa7f2f9SKevin Wolf     return job->cancelled;
101daa7f2f9SKevin Wolf }
102daa7f2f9SKevin Wolf 
103e7c1d78bSKevin Wolf Job *job_next(Job *job)
104e7c1d78bSKevin Wolf {
105e7c1d78bSKevin Wolf     if (!job) {
106e7c1d78bSKevin Wolf         return QLIST_FIRST(&jobs);
107e7c1d78bSKevin Wolf     }
108e7c1d78bSKevin Wolf     return QLIST_NEXT(job, job_list);
109e7c1d78bSKevin Wolf }
110e7c1d78bSKevin Wolf 
111e7c1d78bSKevin Wolf Job *job_get(const char *id)
112e7c1d78bSKevin Wolf {
113e7c1d78bSKevin Wolf     Job *job;
114e7c1d78bSKevin Wolf 
115e7c1d78bSKevin Wolf     QLIST_FOREACH(job, &jobs, job_list) {
116e7c1d78bSKevin Wolf         if (job->id && !strcmp(id, job->id)) {
117e7c1d78bSKevin Wolf             return job;
118e7c1d78bSKevin Wolf         }
119e7c1d78bSKevin Wolf     }
120e7c1d78bSKevin Wolf 
121e7c1d78bSKevin Wolf     return NULL;
122e7c1d78bSKevin Wolf }
123e7c1d78bSKevin Wolf 
124*08be6fe2SKevin Wolf void *job_create(const char *job_id, const JobDriver *driver, AioContext *ctx,
125*08be6fe2SKevin Wolf                  Error **errp)
12633e9e9bdSKevin Wolf {
12733e9e9bdSKevin Wolf     Job *job;
12833e9e9bdSKevin Wolf 
12933e9e9bdSKevin Wolf     if (job_id) {
13033e9e9bdSKevin Wolf         if (!id_wellformed(job_id)) {
13133e9e9bdSKevin Wolf             error_setg(errp, "Invalid job ID '%s'", job_id);
13233e9e9bdSKevin Wolf             return NULL;
13333e9e9bdSKevin Wolf         }
134e7c1d78bSKevin Wolf         if (job_get(job_id)) {
135e7c1d78bSKevin Wolf             error_setg(errp, "Job ID '%s' already in use", job_id);
136e7c1d78bSKevin Wolf             return NULL;
137e7c1d78bSKevin Wolf         }
13833e9e9bdSKevin Wolf     }
13933e9e9bdSKevin Wolf 
14033e9e9bdSKevin Wolf     job = g_malloc0(driver->instance_size);
14133e9e9bdSKevin Wolf     job->driver        = driver;
14233e9e9bdSKevin Wolf     job->id            = g_strdup(job_id);
14380fa2c75SKevin Wolf     job->refcnt        = 1;
144*08be6fe2SKevin Wolf     job->aio_context   = ctx;
14533e9e9bdSKevin Wolf 
146a50c2ab8SKevin Wolf     job_state_transition(job, JOB_STATUS_CREATED);
147a50c2ab8SKevin Wolf 
148e7c1d78bSKevin Wolf     QLIST_INSERT_HEAD(&jobs, job, job_list);
149e7c1d78bSKevin Wolf 
15033e9e9bdSKevin Wolf     return job;
15133e9e9bdSKevin Wolf }
152fd61a701SKevin Wolf 
15380fa2c75SKevin Wolf void job_ref(Job *job)
154fd61a701SKevin Wolf {
15580fa2c75SKevin Wolf     ++job->refcnt;
15680fa2c75SKevin Wolf }
15780fa2c75SKevin Wolf 
15880fa2c75SKevin Wolf void job_unref(Job *job)
15980fa2c75SKevin Wolf {
16080fa2c75SKevin Wolf     if (--job->refcnt == 0) {
16180fa2c75SKevin Wolf         assert(job->status == JOB_STATUS_NULL);
16280fa2c75SKevin Wolf 
16380fa2c75SKevin Wolf         if (job->driver->free) {
16480fa2c75SKevin Wolf             job->driver->free(job);
16580fa2c75SKevin Wolf         }
16680fa2c75SKevin Wolf 
167e7c1d78bSKevin Wolf         QLIST_REMOVE(job, job_list);
168e7c1d78bSKevin Wolf 
169fd61a701SKevin Wolf         g_free(job->id);
170fd61a701SKevin Wolf         g_free(job);
171fd61a701SKevin Wolf     }
17280fa2c75SKevin Wolf }
173