blob: 9e26fa899f6b64fd20cd6486571cd0650d243c81 [file] [log] [blame]
Kevin Wolf1a90bc82018-05-03 19:01:14 +02001/*
2 * QMP interface for background jobs
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"
Kevin Wolf1a90bc82018-05-03 19:01:14 +020027#include "qemu/job.h"
28#include "qapi/qapi-commands-job.h"
29#include "qapi/error.h"
Paolo Bonzini243af022020-02-04 12:20:10 +010030#include "trace/trace-root.h"
Kevin Wolf1a90bc82018-05-03 19:01:14 +020031
Emanuele Giuseppe Esposito96241122022-09-26 05:32:01 -040032/*
Emanuele Giuseppe Esposito6f592e52022-09-26 05:32:11 -040033 * Get a job using its ID. Called with job_mutex held.
Emanuele Giuseppe Esposito96241122022-09-26 05:32:01 -040034 */
Emanuele Giuseppe Esposito6f592e52022-09-26 05:32:11 -040035static Job *find_job_locked(const char *id, Error **errp)
Kevin Wolf1a90bc82018-05-03 19:01:14 +020036{
37 Job *job;
38
Emanuele Giuseppe Esposito96241122022-09-26 05:32:01 -040039 job = job_get_locked(id);
Kevin Wolf1a90bc82018-05-03 19:01:14 +020040 if (!job) {
41 error_setg(errp, "Job not found");
42 return NULL;
43 }
44
Kevin Wolf1a90bc82018-05-03 19:01:14 +020045 return job;
46}
47
48void qmp_job_cancel(const char *id, Error **errp)
49{
Emanuele Giuseppe Esposito96241122022-09-26 05:32:01 -040050 Job *job;
51
52 JOB_LOCK_GUARD();
Emanuele Giuseppe Esposito6f592e52022-09-26 05:32:11 -040053 job = find_job_locked(id, errp);
Kevin Wolf1a90bc82018-05-03 19:01:14 +020054
55 if (!job) {
56 return;
57 }
58
59 trace_qmp_job_cancel(job);
Emanuele Giuseppe Esposito96241122022-09-26 05:32:01 -040060 job_user_cancel_locked(job, true, errp);
Kevin Wolf1a90bc82018-05-03 19:01:14 +020061}
62
63void qmp_job_pause(const char *id, Error **errp)
64{
Emanuele Giuseppe Esposito96241122022-09-26 05:32:01 -040065 Job *job;
66
67 JOB_LOCK_GUARD();
Emanuele Giuseppe Esposito6f592e52022-09-26 05:32:11 -040068 job = find_job_locked(id, errp);
Kevin Wolf1a90bc82018-05-03 19:01:14 +020069
70 if (!job) {
71 return;
72 }
73
74 trace_qmp_job_pause(job);
Emanuele Giuseppe Esposito96241122022-09-26 05:32:01 -040075 job_user_pause_locked(job, errp);
Kevin Wolf1a90bc82018-05-03 19:01:14 +020076}
77
78void qmp_job_resume(const char *id, Error **errp)
79{
Emanuele Giuseppe Esposito96241122022-09-26 05:32:01 -040080 Job *job;
81
82 JOB_LOCK_GUARD();
Emanuele Giuseppe Esposito6f592e52022-09-26 05:32:11 -040083 job = find_job_locked(id, errp);
Kevin Wolf1a90bc82018-05-03 19:01:14 +020084
85 if (!job) {
86 return;
87 }
88
89 trace_qmp_job_resume(job);
Emanuele Giuseppe Esposito96241122022-09-26 05:32:01 -040090 job_user_resume_locked(job, errp);
Kevin Wolf1a90bc82018-05-03 19:01:14 +020091}
92
93void qmp_job_complete(const char *id, Error **errp)
94{
Emanuele Giuseppe Esposito96241122022-09-26 05:32:01 -040095 Job *job;
96
97 JOB_LOCK_GUARD();
Emanuele Giuseppe Esposito6f592e52022-09-26 05:32:11 -040098 job = find_job_locked(id, errp);
Kevin Wolf1a90bc82018-05-03 19:01:14 +020099
100 if (!job) {
101 return;
102 }
103
104 trace_qmp_job_complete(job);
Emanuele Giuseppe Esposito96241122022-09-26 05:32:01 -0400105 job_complete_locked(job, errp);
Kevin Wolf1a90bc82018-05-03 19:01:14 +0200106}
107
108void qmp_job_finalize(const char *id, Error **errp)
109{
Emanuele Giuseppe Esposito96241122022-09-26 05:32:01 -0400110 Job *job;
111
112 JOB_LOCK_GUARD();
Emanuele Giuseppe Esposito6f592e52022-09-26 05:32:11 -0400113 job = find_job_locked(id, errp);
Kevin Wolf1a90bc82018-05-03 19:01:14 +0200114
115 if (!job) {
116 return;
117 }
118
119 trace_qmp_job_finalize(job);
Emanuele Giuseppe Esposito96241122022-09-26 05:32:01 -0400120 job_ref_locked(job);
121 job_finalize_locked(job, errp);
Stefan Reiterb660a842020-04-07 13:56:49 +0200122
Emanuele Giuseppe Esposito96241122022-09-26 05:32:01 -0400123 job_unref_locked(job);
Kevin Wolf1a90bc82018-05-03 19:01:14 +0200124}
125
126void qmp_job_dismiss(const char *id, Error **errp)
127{
Emanuele Giuseppe Esposito96241122022-09-26 05:32:01 -0400128 Job *job;
129
130 JOB_LOCK_GUARD();
Emanuele Giuseppe Esposito6f592e52022-09-26 05:32:11 -0400131 job = find_job_locked(id, errp);
Kevin Wolf1a90bc82018-05-03 19:01:14 +0200132
133 if (!job) {
134 return;
135 }
136
137 trace_qmp_job_dismiss(job);
Emanuele Giuseppe Esposito96241122022-09-26 05:32:01 -0400138 job_dismiss_locked(&job, errp);
Kevin Wolf1a90bc82018-05-03 19:01:14 +0200139}
Kevin Wolf456273b2018-05-04 16:25:43 +0200140
Emanuele Giuseppe Esposito880eeec2022-09-26 05:32:04 -0400141/* Called with job_mutex held. */
142static JobInfo *job_query_single_locked(Job *job, Error **errp)
Kevin Wolf456273b2018-05-04 16:25:43 +0200143{
144 JobInfo *info;
Emanuele Giuseppe Espositoa7b4f8f2021-06-14 10:11:29 +0200145 uint64_t progress_current;
146 uint64_t progress_total;
Kevin Wolf456273b2018-05-04 16:25:43 +0200147
148 assert(!job_is_internal(job));
Emanuele Giuseppe Espositoa7b4f8f2021-06-14 10:11:29 +0200149 progress_get_snapshot(&job->progress, &progress_current,
150 &progress_total);
Kevin Wolf456273b2018-05-04 16:25:43 +0200151
Kevin Wolf456273b2018-05-04 16:25:43 +0200152 info = g_new(JobInfo, 1);
153 *info = (JobInfo) {
154 .id = g_strdup(job->id),
155 .type = job_type(job),
156 .status = job->status,
Emanuele Giuseppe Espositoa7b4f8f2021-06-14 10:11:29 +0200157 .current_progress = progress_current,
158 .total_progress = progress_total,
Markus Armbruster107111b2022-11-04 17:06:56 +0100159 .error = job->err ?
John Snow3d1f8b02018-08-29 21:57:27 -0400160 g_strdup(error_get_pretty(job->err)) : NULL,
Kevin Wolf456273b2018-05-04 16:25:43 +0200161 };
162
163 return info;
164}
165
166JobInfoList *qmp_query_jobs(Error **errp)
167{
Eric Blakec3033fd2021-01-13 16:10:12 -0600168 JobInfoList *head = NULL, **tail = &head;
Kevin Wolf456273b2018-05-04 16:25:43 +0200169 Job *job;
170
Emanuele Giuseppe Esposito880eeec2022-09-26 05:32:04 -0400171 JOB_LOCK_GUARD();
172
173 for (job = job_next_locked(NULL); job; job = job_next_locked(job)) {
Eric Blakec3033fd2021-01-13 16:10:12 -0600174 JobInfo *value;
Kevin Wolf456273b2018-05-04 16:25:43 +0200175
176 if (job_is_internal(job)) {
177 continue;
178 }
Emanuele Giuseppe Esposito880eeec2022-09-26 05:32:04 -0400179 value = job_query_single_locked(job, errp);
Eric Blakec3033fd2021-01-13 16:10:12 -0600180 if (!value) {
Kevin Wolf456273b2018-05-04 16:25:43 +0200181 qapi_free_JobInfoList(head);
182 return NULL;
183 }
Eric Blakec3033fd2021-01-13 16:10:12 -0600184 QAPI_LIST_APPEND(tail, value);
Kevin Wolf456273b2018-05-04 16:25:43 +0200185 }
186
187 return head;
188}