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
26 #include "qemu/osdep.h"
28 #include "qapi/qapi-commands-job.h"
29 #include "qapi/error.h"
30 #include "trace/trace-root.h"
33 * Get a job using its ID and acquire its AioContext.
34 * Called with job_mutex held.
36 static Job
*find_job_locked(const char *id
,
37 AioContext
**aio_context
,
44 job
= job_get_locked(id
);
46 error_setg(errp
, "Job not found");
50 *aio_context
= job
->aio_context
;
51 aio_context_acquire(*aio_context
);
56 void qmp_job_cancel(const char *id
, Error
**errp
)
58 AioContext
*aio_context
;
62 job
= find_job_locked(id
, &aio_context
, errp
);
68 trace_qmp_job_cancel(job
);
69 job_user_cancel_locked(job
, true, errp
);
70 aio_context_release(aio_context
);
73 void qmp_job_pause(const char *id
, Error
**errp
)
75 AioContext
*aio_context
;
79 job
= find_job_locked(id
, &aio_context
, errp
);
85 trace_qmp_job_pause(job
);
86 job_user_pause_locked(job
, errp
);
87 aio_context_release(aio_context
);
90 void qmp_job_resume(const char *id
, Error
**errp
)
92 AioContext
*aio_context
;
96 job
= find_job_locked(id
, &aio_context
, errp
);
102 trace_qmp_job_resume(job
);
103 job_user_resume_locked(job
, errp
);
104 aio_context_release(aio_context
);
107 void qmp_job_complete(const char *id
, Error
**errp
)
109 AioContext
*aio_context
;
113 job
= find_job_locked(id
, &aio_context
, errp
);
119 trace_qmp_job_complete(job
);
120 job_complete_locked(job
, errp
);
121 aio_context_release(aio_context
);
124 void qmp_job_finalize(const char *id
, Error
**errp
)
126 AioContext
*aio_context
;
130 job
= find_job_locked(id
, &aio_context
, errp
);
136 trace_qmp_job_finalize(job
);
138 job_finalize_locked(job
, errp
);
141 * Job's context might have changed via job_finalize (and job_txn_apply
142 * automatically acquires the new one), so make sure we release the correct
145 aio_context
= job
->aio_context
;
146 job_unref_locked(job
);
147 aio_context_release(aio_context
);
150 void qmp_job_dismiss(const char *id
, Error
**errp
)
152 AioContext
*aio_context
;
156 job
= find_job_locked(id
, &aio_context
, errp
);
162 trace_qmp_job_dismiss(job
);
163 job_dismiss_locked(&job
, errp
);
164 aio_context_release(aio_context
);
167 /* Called with job_mutex held. */
168 static JobInfo
*job_query_single_locked(Job
*job
, Error
**errp
)
171 uint64_t progress_current
;
172 uint64_t progress_total
;
174 assert(!job_is_internal(job
));
175 progress_get_snapshot(&job
->progress
, &progress_current
,
178 info
= g_new(JobInfo
, 1);
180 .id
= g_strdup(job
->id
),
181 .type
= job_type(job
),
182 .status
= job
->status
,
183 .current_progress
= progress_current
,
184 .total_progress
= progress_total
,
185 .has_error
= !!job
->err
,
186 .error
= job
->err
? \
187 g_strdup(error_get_pretty(job
->err
)) : NULL
,
193 JobInfoList
*qmp_query_jobs(Error
**errp
)
195 JobInfoList
*head
= NULL
, **tail
= &head
;
200 for (job
= job_next_locked(NULL
); job
; job
= job_next_locked(job
)) {
202 AioContext
*aio_context
;
204 if (job_is_internal(job
)) {
207 aio_context
= job
->aio_context
;
208 aio_context_acquire(aio_context
);
209 value
= job_query_single_locked(job
, errp
);
210 aio_context_release(aio_context
);
212 qapi_free_JobInfoList(head
);
215 QAPI_LIST_APPEND(tail
, value
);