1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26#include "qemu/osdep.h"
27#include "qemu/job.h"
28#include "qapi/qapi-commands-job.h"
29#include "qapi/error.h"
30#include "trace-root.h"
31
32
33static Job *find_job(const char *id, AioContext **aio_context, Error **errp)
34{
35 Job *job;
36
37 *aio_context = NULL;
38
39 job = job_get(id);
40 if (!job) {
41 error_setg(errp, "Job not found");
42 return NULL;
43 }
44
45 *aio_context = job->aio_context;
46 aio_context_acquire(*aio_context);
47
48 return job;
49}
50
51void qmp_job_cancel(const char *id, Error **errp)
52{
53 AioContext *aio_context;
54 Job *job = find_job(id, &aio_context, errp);
55
56 if (!job) {
57 return;
58 }
59
60 trace_qmp_job_cancel(job);
61 job_user_cancel(job, true, errp);
62 aio_context_release(aio_context);
63}
64
65void qmp_job_pause(const char *id, Error **errp)
66{
67 AioContext *aio_context;
68 Job *job = find_job(id, &aio_context, errp);
69
70 if (!job) {
71 return;
72 }
73
74 trace_qmp_job_pause(job);
75 job_user_pause(job, errp);
76 aio_context_release(aio_context);
77}
78
79void qmp_job_resume(const char *id, Error **errp)
80{
81 AioContext *aio_context;
82 Job *job = find_job(id, &aio_context, errp);
83
84 if (!job) {
85 return;
86 }
87
88 trace_qmp_job_resume(job);
89 job_user_resume(job, errp);
90 aio_context_release(aio_context);
91}
92
93void qmp_job_complete(const char *id, Error **errp)
94{
95 AioContext *aio_context;
96 Job *job = find_job(id, &aio_context, errp);
97
98 if (!job) {
99 return;
100 }
101
102 trace_qmp_job_complete(job);
103 job_complete(job, errp);
104 aio_context_release(aio_context);
105}
106
107void qmp_job_finalize(const char *id, Error **errp)
108{
109 AioContext *aio_context;
110 Job *job = find_job(id, &aio_context, errp);
111
112 if (!job) {
113 return;
114 }
115
116 trace_qmp_job_finalize(job);
117 job_ref(job);
118 job_finalize(job, errp);
119
120
121
122
123
124
125 aio_context = job->aio_context;
126 job_unref(job);
127 aio_context_release(aio_context);
128}
129
130void qmp_job_dismiss(const char *id, Error **errp)
131{
132 AioContext *aio_context;
133 Job *job = find_job(id, &aio_context, errp);
134
135 if (!job) {
136 return;
137 }
138
139 trace_qmp_job_dismiss(job);
140 job_dismiss(&job, errp);
141 aio_context_release(aio_context);
142}
143
144static JobInfo *job_query_single(Job *job, Error **errp)
145{
146 JobInfo *info;
147
148 assert(!job_is_internal(job));
149
150 info = g_new(JobInfo, 1);
151 *info = (JobInfo) {
152 .id = g_strdup(job->id),
153 .type = job_type(job),
154 .status = job->status,
155 .current_progress = job->progress.current,
156 .total_progress = job->progress.total,
157 .has_error = !!job->err,
158 .error = job->err ? \
159 g_strdup(error_get_pretty(job->err)) : NULL,
160 };
161
162 return info;
163}
164
165JobInfoList *qmp_query_jobs(Error **errp)
166{
167 JobInfoList *head = NULL, **p_next = &head;
168 Job *job;
169
170 for (job = job_next(NULL); job; job = job_next(job)) {
171 JobInfoList *elem;
172 AioContext *aio_context;
173
174 if (job_is_internal(job)) {
175 continue;
176 }
177 elem = g_new0(JobInfoList, 1);
178 aio_context = job->aio_context;
179 aio_context_acquire(aio_context);
180 elem->value = job_query_single(job, errp);
181 aio_context_release(aio_context);
182 if (!elem->value) {
183 g_free(elem);
184 qapi_free_JobInfoList(head);
185 return NULL;
186 }
187 *p_next = elem;
188 p_next = &elem->next;
189 }
190
191 return head;
192}
193