]>
Commit | Line | Data |
---|---|---|
33e9e9bd KW |
1 | /* |
2 | * Background jobs (long-running operations) | |
3 | * | |
4 | * Copyright (c) 2011 IBM Corp. | |
5 | * Copyright (c) 2012, 2018 Red Hat, Inc. | |
6 | * | |
7 | * Permission is hereby granted, free of charge, to any person obtaining a copy | |
8 | * of this software and associated documentation files (the "Software"), to deal | |
9 | * in the Software without restriction, including without limitation the rights | |
10 | * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell | |
11 | * copies of the Software, and to permit persons to whom the Software is | |
12 | * furnished to do so, subject to the following conditions: | |
13 | * | |
14 | * The above copyright notice and this permission notice shall be included in | |
15 | * all copies or substantial portions of the Software. | |
16 | * | |
17 | * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR | |
18 | * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, | |
19 | * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL | |
20 | * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER | |
21 | * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, | |
22 | * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN | |
23 | * THE SOFTWARE. | |
24 | */ | |
25 | ||
26 | #include "qemu/osdep.h" | |
27 | #include "qemu-common.h" | |
28 | #include "qapi/error.h" | |
29 | #include "qemu/job.h" | |
30 | #include "qemu/id.h" | |
a50c2ab8 | 31 | #include "trace-root.h" |
33e9e9bd | 32 | |
e7c1d78b KW |
33 | static QLIST_HEAD(, Job) jobs = QLIST_HEAD_INITIALIZER(jobs); |
34 | ||
a50c2ab8 KW |
35 | /* Job State Transition Table */ |
36 | bool JobSTT[JOB_STATUS__MAX][JOB_STATUS__MAX] = { | |
37 | /* U, C, R, P, Y, S, W, D, X, E, N */ | |
38 | /* U: */ [JOB_STATUS_UNDEFINED] = {0, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0}, | |
39 | /* C: */ [JOB_STATUS_CREATED] = {0, 0, 1, 0, 0, 0, 0, 0, 1, 0, 1}, | |
40 | /* R: */ [JOB_STATUS_RUNNING] = {0, 0, 0, 1, 1, 0, 1, 0, 1, 0, 0}, | |
41 | /* P: */ [JOB_STATUS_PAUSED] = {0, 0, 1, 0, 0, 0, 0, 0, 0, 0, 0}, | |
42 | /* Y: */ [JOB_STATUS_READY] = {0, 0, 0, 0, 0, 1, 1, 0, 1, 0, 0}, | |
43 | /* S: */ [JOB_STATUS_STANDBY] = {0, 0, 0, 0, 1, 0, 0, 0, 0, 0, 0}, | |
44 | /* W: */ [JOB_STATUS_WAITING] = {0, 0, 0, 0, 0, 0, 0, 1, 1, 0, 0}, | |
45 | /* D: */ [JOB_STATUS_PENDING] = {0, 0, 0, 0, 0, 0, 0, 0, 1, 1, 0}, | |
46 | /* X: */ [JOB_STATUS_ABORTING] = {0, 0, 0, 0, 0, 0, 0, 0, 1, 1, 0}, | |
47 | /* E: */ [JOB_STATUS_CONCLUDED] = {0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1}, | |
48 | /* N: */ [JOB_STATUS_NULL] = {0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0}, | |
49 | }; | |
50 | ||
51 | bool JobVerbTable[JOB_VERB__MAX][JOB_STATUS__MAX] = { | |
52 | /* U, C, R, P, Y, S, W, D, X, E, N */ | |
53 | [JOB_VERB_CANCEL] = {0, 1, 1, 1, 1, 1, 1, 1, 0, 0, 0}, | |
54 | [JOB_VERB_PAUSE] = {0, 1, 1, 1, 1, 1, 0, 0, 0, 0, 0}, | |
55 | [JOB_VERB_RESUME] = {0, 1, 1, 1, 1, 1, 0, 0, 0, 0, 0}, | |
56 | [JOB_VERB_SET_SPEED] = {0, 1, 1, 1, 1, 1, 0, 0, 0, 0, 0}, | |
57 | [JOB_VERB_COMPLETE] = {0, 0, 0, 0, 1, 0, 0, 0, 0, 0, 0}, | |
58 | [JOB_VERB_FINALIZE] = {0, 0, 0, 0, 0, 0, 0, 1, 0, 0, 0}, | |
59 | [JOB_VERB_DISMISS] = {0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 0}, | |
60 | }; | |
61 | ||
62 | /* TODO Make static once the whole state machine is in job.c */ | |
63 | void job_state_transition(Job *job, JobStatus s1) | |
64 | { | |
65 | JobStatus s0 = job->status; | |
66 | assert(s1 >= 0 && s1 <= JOB_STATUS__MAX); | |
67 | trace_job_state_transition(job, /* TODO re-enable: job->ret */ 0, | |
68 | JobSTT[s0][s1] ? "allowed" : "disallowed", | |
69 | JobStatus_str(s0), JobStatus_str(s1)); | |
70 | assert(JobSTT[s0][s1]); | |
71 | job->status = s1; | |
72 | } | |
73 | ||
74 | int job_apply_verb(Job *job, JobVerb verb, Error **errp) | |
75 | { | |
76 | JobStatus s0 = job->status; | |
77 | assert(verb >= 0 && verb <= JOB_VERB__MAX); | |
78 | trace_job_apply_verb(job, JobStatus_str(s0), JobVerb_str(verb), | |
79 | JobVerbTable[verb][s0] ? "allowed" : "prohibited"); | |
80 | if (JobVerbTable[verb][s0]) { | |
81 | return 0; | |
82 | } | |
83 | error_setg(errp, "Job '%s' in state '%s' cannot accept command verb '%s'", | |
84 | job->id, JobStatus_str(s0), JobVerb_str(verb)); | |
85 | return -EPERM; | |
86 | } | |
87 | ||
252291ea KW |
88 | JobType job_type(const Job *job) |
89 | { | |
90 | return job->driver->job_type; | |
91 | } | |
92 | ||
93 | const char *job_type_str(const Job *job) | |
94 | { | |
95 | return JobType_str(job_type(job)); | |
96 | } | |
97 | ||
e7c1d78b KW |
98 | Job *job_next(Job *job) |
99 | { | |
100 | if (!job) { | |
101 | return QLIST_FIRST(&jobs); | |
102 | } | |
103 | return QLIST_NEXT(job, job_list); | |
104 | } | |
105 | ||
106 | Job *job_get(const char *id) | |
107 | { | |
108 | Job *job; | |
109 | ||
110 | QLIST_FOREACH(job, &jobs, job_list) { | |
111 | if (job->id && !strcmp(id, job->id)) { | |
112 | return job; | |
113 | } | |
114 | } | |
115 | ||
116 | return NULL; | |
117 | } | |
118 | ||
33e9e9bd KW |
119 | void *job_create(const char *job_id, const JobDriver *driver, Error **errp) |
120 | { | |
121 | Job *job; | |
122 | ||
123 | if (job_id) { | |
124 | if (!id_wellformed(job_id)) { | |
125 | error_setg(errp, "Invalid job ID '%s'", job_id); | |
126 | return NULL; | |
127 | } | |
e7c1d78b KW |
128 | if (job_get(job_id)) { |
129 | error_setg(errp, "Job ID '%s' already in use", job_id); | |
130 | return NULL; | |
131 | } | |
33e9e9bd KW |
132 | } |
133 | ||
134 | job = g_malloc0(driver->instance_size); | |
135 | job->driver = driver; | |
136 | job->id = g_strdup(job_id); | |
80fa2c75 | 137 | job->refcnt = 1; |
33e9e9bd | 138 | |
a50c2ab8 KW |
139 | job_state_transition(job, JOB_STATUS_CREATED); |
140 | ||
e7c1d78b KW |
141 | QLIST_INSERT_HEAD(&jobs, job, job_list); |
142 | ||
33e9e9bd KW |
143 | return job; |
144 | } | |
fd61a701 | 145 | |
80fa2c75 | 146 | void job_ref(Job *job) |
fd61a701 | 147 | { |
80fa2c75 KW |
148 | ++job->refcnt; |
149 | } | |
150 | ||
151 | void job_unref(Job *job) | |
152 | { | |
153 | if (--job->refcnt == 0) { | |
154 | assert(job->status == JOB_STATUS_NULL); | |
e7c1d78b | 155 | |
80fa2c75 KW |
156 | if (job->driver->free) { |
157 | job->driver->free(job); | |
158 | } | |
159 | ||
160 | QLIST_REMOVE(job, job_list); | |
161 | ||
162 | g_free(job->id); | |
163 | g_free(job); | |
164 | } | |
fd61a701 | 165 | } |