MAINTAINERS: add Stefano Garzarella as io_uring reviewer
[qemu/rayw.git] / job-qmp.c
blob829a28aa70e3e0a880fe2007ba8ca75a9fc4cbeb
1 /*
2 * QMP interface for background jobs
4 * Copyright (c) 2011 IBM Corp.
5 * Copyright (c) 2012, 2018 Red Hat, Inc.
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:
14 * The above copyright notice and this permission notice shall be included in
15 * all copies or substantial portions of the Software.
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.
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/trace-root.h"
32 /* Get a job using its ID and acquire its AioContext */
33 static Job *find_job(const char *id, AioContext **aio_context, Error **errp)
35 Job *job;
37 *aio_context = NULL;
39 job = job_get(id);
40 if (!job) {
41 error_setg(errp, "Job not found");
42 return NULL;
45 *aio_context = job->aio_context;
46 aio_context_acquire(*aio_context);
48 return job;
51 void qmp_job_cancel(const char *id, Error **errp)
53 AioContext *aio_context;
54 Job *job = find_job(id, &aio_context, errp);
56 if (!job) {
57 return;
60 trace_qmp_job_cancel(job);
61 job_user_cancel(job, true, errp);
62 aio_context_release(aio_context);
65 void qmp_job_pause(const char *id, Error **errp)
67 AioContext *aio_context;
68 Job *job = find_job(id, &aio_context, errp);
70 if (!job) {
71 return;
74 trace_qmp_job_pause(job);
75 job_user_pause(job, errp);
76 aio_context_release(aio_context);
79 void qmp_job_resume(const char *id, Error **errp)
81 AioContext *aio_context;
82 Job *job = find_job(id, &aio_context, errp);
84 if (!job) {
85 return;
88 trace_qmp_job_resume(job);
89 job_user_resume(job, errp);
90 aio_context_release(aio_context);
93 void qmp_job_complete(const char *id, Error **errp)
95 AioContext *aio_context;
96 Job *job = find_job(id, &aio_context, errp);
98 if (!job) {
99 return;
102 trace_qmp_job_complete(job);
103 job_complete(job, errp);
104 aio_context_release(aio_context);
107 void qmp_job_finalize(const char *id, Error **errp)
109 AioContext *aio_context;
110 Job *job = find_job(id, &aio_context, errp);
112 if (!job) {
113 return;
116 trace_qmp_job_finalize(job);
117 job_ref(job);
118 job_finalize(job, errp);
121 * Job's context might have changed via job_finalize (and job_txn_apply
122 * automatically acquires the new one), so make sure we release the correct
123 * one.
125 aio_context = job->aio_context;
126 job_unref(job);
127 aio_context_release(aio_context);
130 void qmp_job_dismiss(const char *id, Error **errp)
132 AioContext *aio_context;
133 Job *job = find_job(id, &aio_context, errp);
135 if (!job) {
136 return;
139 trace_qmp_job_dismiss(job);
140 job_dismiss(&job, errp);
141 aio_context_release(aio_context);
144 static JobInfo *job_query_single(Job *job, Error **errp)
146 JobInfo *info;
147 uint64_t progress_current;
148 uint64_t progress_total;
150 assert(!job_is_internal(job));
151 progress_get_snapshot(&job->progress, &progress_current,
152 &progress_total);
154 info = g_new(JobInfo, 1);
155 *info = (JobInfo) {
156 .id = g_strdup(job->id),
157 .type = job_type(job),
158 .status = job->status,
159 .current_progress = progress_current,
160 .total_progress = progress_total,
161 .has_error = !!job->err,
162 .error = job->err ? \
163 g_strdup(error_get_pretty(job->err)) : NULL,
166 return info;
169 JobInfoList *qmp_query_jobs(Error **errp)
171 JobInfoList *head = NULL, **tail = &head;
172 Job *job;
174 for (job = job_next(NULL); job; job = job_next(job)) {
175 JobInfo *value;
176 AioContext *aio_context;
178 if (job_is_internal(job)) {
179 continue;
181 aio_context = job->aio_context;
182 aio_context_acquire(aio_context);
183 value = job_query_single(job, errp);
184 aio_context_release(aio_context);
185 if (!value) {
186 qapi_free_JobInfoList(head);
187 return NULL;
189 QAPI_LIST_APPEND(tail, value);
192 return head;