]> git.proxmox.com Git - mirror_qemu.git/blame - job.c
job: Move defer_to_main_loop to Job
[mirror_qemu.git] / job.c
CommitLineData
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"
1908a559 31#include "qemu/main-loop.h"
a50c2ab8 32#include "trace-root.h"
33e9e9bd 33
e7c1d78b
KW
34static QLIST_HEAD(, Job) jobs = QLIST_HEAD_INITIALIZER(jobs);
35
a50c2ab8
KW
36/* Job State Transition Table */
37bool JobSTT[JOB_STATUS__MAX][JOB_STATUS__MAX] = {
38 /* U, C, R, P, Y, S, W, D, X, E, N */
39 /* U: */ [JOB_STATUS_UNDEFINED] = {0, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0},
40 /* C: */ [JOB_STATUS_CREATED] = {0, 0, 1, 0, 0, 0, 0, 0, 1, 0, 1},
41 /* R: */ [JOB_STATUS_RUNNING] = {0, 0, 0, 1, 1, 0, 1, 0, 1, 0, 0},
42 /* P: */ [JOB_STATUS_PAUSED] = {0, 0, 1, 0, 0, 0, 0, 0, 0, 0, 0},
43 /* Y: */ [JOB_STATUS_READY] = {0, 0, 0, 0, 0, 1, 1, 0, 1, 0, 0},
44 /* S: */ [JOB_STATUS_STANDBY] = {0, 0, 0, 0, 1, 0, 0, 0, 0, 0, 0},
45 /* W: */ [JOB_STATUS_WAITING] = {0, 0, 0, 0, 0, 0, 0, 1, 1, 0, 0},
46 /* D: */ [JOB_STATUS_PENDING] = {0, 0, 0, 0, 0, 0, 0, 0, 1, 1, 0},
47 /* X: */ [JOB_STATUS_ABORTING] = {0, 0, 0, 0, 0, 0, 0, 0, 1, 1, 0},
48 /* E: */ [JOB_STATUS_CONCLUDED] = {0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1},
49 /* N: */ [JOB_STATUS_NULL] = {0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0},
50};
51
52bool JobVerbTable[JOB_VERB__MAX][JOB_STATUS__MAX] = {
53 /* U, C, R, P, Y, S, W, D, X, E, N */
54 [JOB_VERB_CANCEL] = {0, 1, 1, 1, 1, 1, 1, 1, 0, 0, 0},
55 [JOB_VERB_PAUSE] = {0, 1, 1, 1, 1, 1, 0, 0, 0, 0, 0},
56 [JOB_VERB_RESUME] = {0, 1, 1, 1, 1, 1, 0, 0, 0, 0, 0},
57 [JOB_VERB_SET_SPEED] = {0, 1, 1, 1, 1, 1, 0, 0, 0, 0, 0},
58 [JOB_VERB_COMPLETE] = {0, 0, 0, 0, 1, 0, 0, 0, 0, 0, 0},
59 [JOB_VERB_FINALIZE] = {0, 0, 0, 0, 0, 0, 0, 1, 0, 0, 0},
60 [JOB_VERB_DISMISS] = {0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 0},
61};
62
63/* TODO Make static once the whole state machine is in job.c */
64void job_state_transition(Job *job, JobStatus s1)
65{
66 JobStatus s0 = job->status;
67 assert(s1 >= 0 && s1 <= JOB_STATUS__MAX);
68 trace_job_state_transition(job, /* TODO re-enable: job->ret */ 0,
69 JobSTT[s0][s1] ? "allowed" : "disallowed",
70 JobStatus_str(s0), JobStatus_str(s1));
71 assert(JobSTT[s0][s1]);
72 job->status = s1;
73}
74
75int job_apply_verb(Job *job, JobVerb verb, Error **errp)
76{
77 JobStatus s0 = job->status;
78 assert(verb >= 0 && verb <= JOB_VERB__MAX);
79 trace_job_apply_verb(job, JobStatus_str(s0), JobVerb_str(verb),
80 JobVerbTable[verb][s0] ? "allowed" : "prohibited");
81 if (JobVerbTable[verb][s0]) {
82 return 0;
83 }
84 error_setg(errp, "Job '%s' in state '%s' cannot accept command verb '%s'",
85 job->id, JobStatus_str(s0), JobVerb_str(verb));
86 return -EPERM;
87}
88
252291ea
KW
89JobType job_type(const Job *job)
90{
91 return job->driver->job_type;
92}
93
94const char *job_type_str(const Job *job)
95{
96 return JobType_str(job_type(job));
97}
98
daa7f2f9
KW
99bool job_is_cancelled(Job *job)
100{
101 return job->cancelled;
102}
103
e7c1d78b
KW
104Job *job_next(Job *job)
105{
106 if (!job) {
107 return QLIST_FIRST(&jobs);
108 }
109 return QLIST_NEXT(job, job_list);
110}
111
112Job *job_get(const char *id)
113{
114 Job *job;
115
116 QLIST_FOREACH(job, &jobs, job_list) {
117 if (job->id && !strcmp(id, job->id)) {
118 return job;
119 }
120 }
121
122 return NULL;
123}
124
08be6fe2
KW
125void *job_create(const char *job_id, const JobDriver *driver, AioContext *ctx,
126 Error **errp)
33e9e9bd
KW
127{
128 Job *job;
129
130 if (job_id) {
131 if (!id_wellformed(job_id)) {
132 error_setg(errp, "Invalid job ID '%s'", job_id);
133 return NULL;
134 }
e7c1d78b
KW
135 if (job_get(job_id)) {
136 error_setg(errp, "Job ID '%s' already in use", job_id);
137 return NULL;
138 }
33e9e9bd
KW
139 }
140
141 job = g_malloc0(driver->instance_size);
142 job->driver = driver;
143 job->id = g_strdup(job_id);
80fa2c75 144 job->refcnt = 1;
08be6fe2 145 job->aio_context = ctx;
33e9e9bd 146
a50c2ab8
KW
147 job_state_transition(job, JOB_STATUS_CREATED);
148
e7c1d78b
KW
149 QLIST_INSERT_HEAD(&jobs, job, job_list);
150
33e9e9bd
KW
151 return job;
152}
fd61a701 153
80fa2c75 154void job_ref(Job *job)
fd61a701 155{
80fa2c75
KW
156 ++job->refcnt;
157}
158
159void job_unref(Job *job)
160{
161 if (--job->refcnt == 0) {
162 assert(job->status == JOB_STATUS_NULL);
e7c1d78b 163
80fa2c75
KW
164 if (job->driver->free) {
165 job->driver->free(job);
166 }
167
168 QLIST_REMOVE(job, job_list);
169
170 g_free(job->id);
171 g_free(job);
172 }
fd61a701 173}
1908a559
KW
174
175typedef struct {
176 Job *job;
177 JobDeferToMainLoopFn *fn;
178 void *opaque;
179} JobDeferToMainLoopData;
180
181static void job_defer_to_main_loop_bh(void *opaque)
182{
183 JobDeferToMainLoopData *data = opaque;
184 Job *job = data->job;
185 AioContext *aio_context = job->aio_context;
186
187 aio_context_acquire(aio_context);
188 data->fn(data->job, data->opaque);
189 aio_context_release(aio_context);
190
191 g_free(data);
192}
193
194void job_defer_to_main_loop(Job *job, JobDeferToMainLoopFn *fn, void *opaque)
195{
196 JobDeferToMainLoopData *data = g_malloc(sizeof(*data));
197 data->job = job;
198 data->fn = fn;
199 data->opaque = opaque;
200 job->deferred_to_main_loop = true;
201
202 aio_bh_schedule_oneshot(qemu_get_aio_context(),
203 job_defer_to_main_loop_bh, data);
204}