Kevin Wolf | 33e9e9b | 2018-04-12 17:29:59 +0200 | [diff] [blame] | 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" |
Kevin Wolf | 33e9e9b | 2018-04-12 17:29:59 +0200 | [diff] [blame] | 27 | #include "qapi/error.h" |
| 28 | #include "qemu/job.h" |
| 29 | #include "qemu/id.h" |
Kevin Wolf | 1908a55 | 2018-04-17 16:41:17 +0200 | [diff] [blame] | 30 | #include "qemu/main-loop.h" |
Kevin Wolf | de0fbe6 | 2018-08-17 14:58:49 +0200 | [diff] [blame] | 31 | #include "block/aio-wait.h" |
Paolo Bonzini | 243af02 | 2020-02-04 12:20:10 +0100 | [diff] [blame] | 32 | #include "trace/trace-root.h" |
Kevin Wolf | 1dac83f | 2018-04-30 19:09:46 +0200 | [diff] [blame] | 33 | #include "qapi/qapi-events-job.h" |
Kevin Wolf | 33e9e9b | 2018-04-12 17:29:59 +0200 | [diff] [blame] | 34 | |
Emanuele Giuseppe Esposito | 55c5a25 | 2022-09-26 05:31:54 -0400 | [diff] [blame] | 35 | /* |
Emanuele Giuseppe Esposito | bf61c58 | 2022-09-26 05:31:59 -0400 | [diff] [blame] | 36 | * The job API is composed of two categories of functions. |
| 37 | * |
| 38 | * The first includes functions used by the monitor. The monitor is |
| 39 | * peculiar in that it accesses the job list with job_get, and |
| 40 | * therefore needs consistency across job_get and the actual operation |
| 41 | * (e.g. job_user_cancel). To achieve this consistency, the caller |
| 42 | * calls job_lock/job_unlock itself around the whole operation. |
| 43 | * |
| 44 | * |
| 45 | * The second includes functions used by the job drivers and sometimes |
| 46 | * by the core block layer. These delegate the locking to the callee instead. |
| 47 | * |
| 48 | * TODO Actually make this true |
| 49 | */ |
| 50 | |
| 51 | /* |
Emanuele Giuseppe Esposito | 55c5a25 | 2022-09-26 05:31:54 -0400 | [diff] [blame] | 52 | * job_mutex protects the jobs list, but also makes the |
| 53 | * struct job fields thread-safe. |
| 54 | */ |
| 55 | QemuMutex job_mutex; |
| 56 | |
Emanuele Giuseppe Esposito | afe1e8a | 2022-09-26 05:31:58 -0400 | [diff] [blame] | 57 | /* Protected by job_mutex */ |
Kevin Wolf | e7c1d78 | 2018-04-12 17:54:37 +0200 | [diff] [blame] | 58 | static QLIST_HEAD(, Job) jobs = QLIST_HEAD_INITIALIZER(jobs); |
| 59 | |
Kevin Wolf | a50c2ab | 2018-04-13 17:19:31 +0200 | [diff] [blame] | 60 | /* Job State Transition Table */ |
| 61 | bool JobSTT[JOB_STATUS__MAX][JOB_STATUS__MAX] = { |
| 62 | /* U, C, R, P, Y, S, W, D, X, E, N */ |
| 63 | /* U: */ [JOB_STATUS_UNDEFINED] = {0, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0}, |
| 64 | /* C: */ [JOB_STATUS_CREATED] = {0, 0, 1, 0, 0, 0, 0, 0, 1, 0, 1}, |
| 65 | /* R: */ [JOB_STATUS_RUNNING] = {0, 0, 0, 1, 1, 0, 1, 0, 1, 0, 0}, |
| 66 | /* P: */ [JOB_STATUS_PAUSED] = {0, 0, 1, 0, 0, 0, 0, 0, 0, 0, 0}, |
| 67 | /* Y: */ [JOB_STATUS_READY] = {0, 0, 0, 0, 0, 1, 1, 0, 1, 0, 0}, |
| 68 | /* S: */ [JOB_STATUS_STANDBY] = {0, 0, 0, 0, 1, 0, 0, 0, 0, 0, 0}, |
| 69 | /* W: */ [JOB_STATUS_WAITING] = {0, 0, 0, 0, 0, 0, 0, 1, 1, 0, 0}, |
| 70 | /* D: */ [JOB_STATUS_PENDING] = {0, 0, 0, 0, 0, 0, 0, 0, 1, 1, 0}, |
| 71 | /* X: */ [JOB_STATUS_ABORTING] = {0, 0, 0, 0, 0, 0, 0, 0, 1, 1, 0}, |
| 72 | /* E: */ [JOB_STATUS_CONCLUDED] = {0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1}, |
| 73 | /* N: */ [JOB_STATUS_NULL] = {0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0}, |
| 74 | }; |
| 75 | |
| 76 | bool JobVerbTable[JOB_VERB__MAX][JOB_STATUS__MAX] = { |
| 77 | /* U, C, R, P, Y, S, W, D, X, E, N */ |
| 78 | [JOB_VERB_CANCEL] = {0, 1, 1, 1, 1, 1, 1, 1, 0, 0, 0}, |
| 79 | [JOB_VERB_PAUSE] = {0, 1, 1, 1, 1, 1, 0, 0, 0, 0, 0}, |
| 80 | [JOB_VERB_RESUME] = {0, 1, 1, 1, 1, 1, 0, 0, 0, 0, 0}, |
| 81 | [JOB_VERB_SET_SPEED] = {0, 1, 1, 1, 1, 1, 0, 0, 0, 0, 0}, |
Max Reitz | 53ddb9c | 2021-04-09 14:04:20 +0200 | [diff] [blame] | 82 | [JOB_VERB_COMPLETE] = {0, 0, 0, 0, 1, 1, 0, 0, 0, 0, 0}, |
Kevin Wolf | a50c2ab | 2018-04-13 17:19:31 +0200 | [diff] [blame] | 83 | [JOB_VERB_FINALIZE] = {0, 0, 0, 0, 0, 0, 0, 1, 0, 0, 0}, |
| 84 | [JOB_VERB_DISMISS] = {0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 0}, |
| 85 | }; |
| 86 | |
Kevin Wolf | 7eaa8fb | 2018-04-23 16:06:26 +0200 | [diff] [blame] | 87 | /* Transactional group of jobs */ |
| 88 | struct JobTxn { |
| 89 | |
| 90 | /* Is this txn being cancelled? */ |
| 91 | bool aborting; |
| 92 | |
| 93 | /* List of jobs */ |
| 94 | QLIST_HEAD(, Job) jobs; |
| 95 | |
| 96 | /* Reference count */ |
| 97 | int refcnt; |
| 98 | }; |
| 99 | |
Emanuele Giuseppe Esposito | 55c5a25 | 2022-09-26 05:31:54 -0400 | [diff] [blame] | 100 | void job_lock(void) |
| 101 | { |
| 102 | /* nop */ |
| 103 | } |
Kevin Wolf | da01ff7 | 2018-04-13 17:31:02 +0200 | [diff] [blame] | 104 | |
Emanuele Giuseppe Esposito | 55c5a25 | 2022-09-26 05:31:54 -0400 | [diff] [blame] | 105 | void job_unlock(void) |
| 106 | { |
| 107 | /* nop */ |
| 108 | } |
| 109 | |
| 110 | static void real_job_lock(void) |
Kevin Wolf | da01ff7 | 2018-04-13 17:31:02 +0200 | [diff] [blame] | 111 | { |
| 112 | qemu_mutex_lock(&job_mutex); |
| 113 | } |
| 114 | |
Emanuele Giuseppe Esposito | 55c5a25 | 2022-09-26 05:31:54 -0400 | [diff] [blame] | 115 | static void real_job_unlock(void) |
Kevin Wolf | da01ff7 | 2018-04-13 17:31:02 +0200 | [diff] [blame] | 116 | { |
| 117 | qemu_mutex_unlock(&job_mutex); |
| 118 | } |
| 119 | |
| 120 | static void __attribute__((__constructor__)) job_init(void) |
| 121 | { |
| 122 | qemu_mutex_init(&job_mutex); |
| 123 | } |
| 124 | |
Kevin Wolf | 7eaa8fb | 2018-04-23 16:06:26 +0200 | [diff] [blame] | 125 | JobTxn *job_txn_new(void) |
| 126 | { |
| 127 | JobTxn *txn = g_new0(JobTxn, 1); |
| 128 | QLIST_INIT(&txn->jobs); |
| 129 | txn->refcnt = 1; |
| 130 | return txn; |
| 131 | } |
| 132 | |
Emanuele Giuseppe Esposito | afe1e8a | 2022-09-26 05:31:58 -0400 | [diff] [blame] | 133 | /* Called with job_mutex held. */ |
| 134 | static void job_txn_ref_locked(JobTxn *txn) |
Kevin Wolf | 7eaa8fb | 2018-04-23 16:06:26 +0200 | [diff] [blame] | 135 | { |
| 136 | txn->refcnt++; |
| 137 | } |
| 138 | |
Emanuele Giuseppe Esposito | afe1e8a | 2022-09-26 05:31:58 -0400 | [diff] [blame] | 139 | void job_txn_unref_locked(JobTxn *txn) |
Kevin Wolf | 7eaa8fb | 2018-04-23 16:06:26 +0200 | [diff] [blame] | 140 | { |
| 141 | if (txn && --txn->refcnt == 0) { |
| 142 | g_free(txn); |
| 143 | } |
| 144 | } |
| 145 | |
Emanuele Giuseppe Esposito | afe1e8a | 2022-09-26 05:31:58 -0400 | [diff] [blame] | 146 | void job_txn_unref(JobTxn *txn) |
| 147 | { |
| 148 | JOB_LOCK_GUARD(); |
| 149 | job_txn_unref_locked(txn); |
| 150 | } |
| 151 | |
Emanuele Giuseppe Esposito | 544f4d5 | 2022-09-26 05:31:56 -0400 | [diff] [blame] | 152 | /** |
| 153 | * @txn: The transaction (may be NULL) |
| 154 | * @job: Job to add to the transaction |
| 155 | * |
| 156 | * Add @job to the transaction. The @job must not already be in a transaction. |
| 157 | * The caller must call either job_txn_unref() or job_completed() to release |
| 158 | * the reference that is automatically grabbed here. |
| 159 | * |
| 160 | * If @txn is NULL, the function does nothing. |
Emanuele Giuseppe Esposito | afe1e8a | 2022-09-26 05:31:58 -0400 | [diff] [blame] | 161 | * |
| 162 | * Called with job_mutex held. |
Emanuele Giuseppe Esposito | 544f4d5 | 2022-09-26 05:31:56 -0400 | [diff] [blame] | 163 | */ |
Emanuele Giuseppe Esposito | afe1e8a | 2022-09-26 05:31:58 -0400 | [diff] [blame] | 164 | static void job_txn_add_job_locked(JobTxn *txn, Job *job) |
Kevin Wolf | 7eaa8fb | 2018-04-23 16:06:26 +0200 | [diff] [blame] | 165 | { |
| 166 | if (!txn) { |
| 167 | return; |
| 168 | } |
| 169 | |
| 170 | assert(!job->txn); |
| 171 | job->txn = txn; |
| 172 | |
| 173 | QLIST_INSERT_HEAD(&txn->jobs, job, txn_list); |
Emanuele Giuseppe Esposito | afe1e8a | 2022-09-26 05:31:58 -0400 | [diff] [blame] | 174 | job_txn_ref_locked(txn); |
Kevin Wolf | 7eaa8fb | 2018-04-23 16:06:26 +0200 | [diff] [blame] | 175 | } |
| 176 | |
Emanuele Giuseppe Esposito | afe1e8a | 2022-09-26 05:31:58 -0400 | [diff] [blame] | 177 | /* Called with job_mutex held. */ |
| 178 | static void job_txn_del_job_locked(Job *job) |
Kevin Wolf | 7eaa8fb | 2018-04-23 16:06:26 +0200 | [diff] [blame] | 179 | { |
| 180 | if (job->txn) { |
| 181 | QLIST_REMOVE(job, txn_list); |
Emanuele Giuseppe Esposito | afe1e8a | 2022-09-26 05:31:58 -0400 | [diff] [blame] | 182 | job_txn_unref_locked(job->txn); |
Kevin Wolf | 7eaa8fb | 2018-04-23 16:06:26 +0200 | [diff] [blame] | 183 | job->txn = NULL; |
| 184 | } |
| 185 | } |
| 186 | |
Emanuele Giuseppe Esposito | afe1e8a | 2022-09-26 05:31:58 -0400 | [diff] [blame] | 187 | /* Called with job_mutex held, but releases it temporarily. */ |
| 188 | static int job_txn_apply_locked(Job *job, int fn(Job *)) |
Kevin Wolf | 7eaa8fb | 2018-04-23 16:06:26 +0200 | [diff] [blame] | 189 | { |
Stefan Reiter | b660a84 | 2020-04-07 13:56:49 +0200 | [diff] [blame] | 190 | AioContext *inner_ctx; |
| 191 | Job *other_job, *next; |
| 192 | JobTxn *txn = job->txn; |
Kevin Wolf | 7eaa8fb | 2018-04-23 16:06:26 +0200 | [diff] [blame] | 193 | int rc = 0; |
| 194 | |
Stefan Reiter | b660a84 | 2020-04-07 13:56:49 +0200 | [diff] [blame] | 195 | /* |
| 196 | * Similar to job_completed_txn_abort, we take each job's lock before |
| 197 | * applying fn, but since we assume that outer_ctx is held by the caller, |
| 198 | * we need to release it here to avoid holding the lock twice - which would |
| 199 | * break AIO_WAIT_WHILE from within fn. |
| 200 | */ |
Emanuele Giuseppe Esposito | afe1e8a | 2022-09-26 05:31:58 -0400 | [diff] [blame] | 201 | job_ref_locked(job); |
Stefan Reiter | b660a84 | 2020-04-07 13:56:49 +0200 | [diff] [blame] | 202 | aio_context_release(job->aio_context); |
| 203 | |
| 204 | QLIST_FOREACH_SAFE(other_job, &txn->jobs, txn_list, next) { |
| 205 | inner_ctx = other_job->aio_context; |
| 206 | aio_context_acquire(inner_ctx); |
| 207 | rc = fn(other_job); |
| 208 | aio_context_release(inner_ctx); |
Kevin Wolf | 7eaa8fb | 2018-04-23 16:06:26 +0200 | [diff] [blame] | 209 | if (rc) { |
| 210 | break; |
| 211 | } |
| 212 | } |
Stefan Reiter | b660a84 | 2020-04-07 13:56:49 +0200 | [diff] [blame] | 213 | |
| 214 | /* |
| 215 | * Note that job->aio_context might have been changed by calling fn, so we |
| 216 | * can't use a local variable to cache it. |
| 217 | */ |
| 218 | aio_context_acquire(job->aio_context); |
Emanuele Giuseppe Esposito | afe1e8a | 2022-09-26 05:31:58 -0400 | [diff] [blame] | 219 | job_unref_locked(job); |
Kevin Wolf | 7eaa8fb | 2018-04-23 16:06:26 +0200 | [diff] [blame] | 220 | return rc; |
| 221 | } |
| 222 | |
Kevin Wolf | 456273b | 2018-05-04 16:25:43 +0200 | [diff] [blame] | 223 | bool job_is_internal(Job *job) |
Kevin Wolf | 1dac83f | 2018-04-30 19:09:46 +0200 | [diff] [blame] | 224 | { |
| 225 | return (job->id == NULL); |
| 226 | } |
| 227 | |
Emanuele Giuseppe Esposito | afe1e8a | 2022-09-26 05:31:58 -0400 | [diff] [blame] | 228 | /* Called with job_mutex held. */ |
| 229 | static void job_state_transition_locked(Job *job, JobStatus s1) |
Kevin Wolf | a50c2ab | 2018-04-13 17:19:31 +0200 | [diff] [blame] | 230 | { |
| 231 | JobStatus s0 = job->status; |
Liam Merwick | c203228 | 2018-11-05 21:38:35 +0000 | [diff] [blame] | 232 | assert(s1 >= 0 && s1 < JOB_STATUS__MAX); |
Kevin Wolf | 4ad3518 | 2018-04-19 17:30:16 +0200 | [diff] [blame] | 233 | trace_job_state_transition(job, job->ret, |
Kevin Wolf | a50c2ab | 2018-04-13 17:19:31 +0200 | [diff] [blame] | 234 | JobSTT[s0][s1] ? "allowed" : "disallowed", |
| 235 | JobStatus_str(s0), JobStatus_str(s1)); |
| 236 | assert(JobSTT[s0][s1]); |
| 237 | job->status = s1; |
Kevin Wolf | 1dac83f | 2018-04-30 19:09:46 +0200 | [diff] [blame] | 238 | |
| 239 | if (!job_is_internal(job) && s1 != s0) { |
Peter Xu | 3ab7238 | 2018-08-15 21:37:37 +0800 | [diff] [blame] | 240 | qapi_event_send_job_status_change(job->id, job->status); |
Kevin Wolf | 1dac83f | 2018-04-30 19:09:46 +0200 | [diff] [blame] | 241 | } |
Kevin Wolf | a50c2ab | 2018-04-13 17:19:31 +0200 | [diff] [blame] | 242 | } |
| 243 | |
Emanuele Giuseppe Esposito | afe1e8a | 2022-09-26 05:31:58 -0400 | [diff] [blame] | 244 | int job_apply_verb_locked(Job *job, JobVerb verb, Error **errp) |
Kevin Wolf | a50c2ab | 2018-04-13 17:19:31 +0200 | [diff] [blame] | 245 | { |
| 246 | JobStatus s0 = job->status; |
Liam Merwick | c203228 | 2018-11-05 21:38:35 +0000 | [diff] [blame] | 247 | assert(verb >= 0 && verb < JOB_VERB__MAX); |
Kevin Wolf | a50c2ab | 2018-04-13 17:19:31 +0200 | [diff] [blame] | 248 | trace_job_apply_verb(job, JobStatus_str(s0), JobVerb_str(verb), |
| 249 | JobVerbTable[verb][s0] ? "allowed" : "prohibited"); |
| 250 | if (JobVerbTable[verb][s0]) { |
| 251 | return 0; |
| 252 | } |
| 253 | error_setg(errp, "Job '%s' in state '%s' cannot accept command verb '%s'", |
| 254 | job->id, JobStatus_str(s0), JobVerb_str(verb)); |
| 255 | return -EPERM; |
| 256 | } |
| 257 | |
Emanuele Giuseppe Esposito | afe1e8a | 2022-09-26 05:31:58 -0400 | [diff] [blame] | 258 | int job_apply_verb(Job *job, JobVerb verb, Error **errp) |
| 259 | { |
| 260 | JOB_LOCK_GUARD(); |
| 261 | return job_apply_verb_locked(job, verb, errp); |
| 262 | } |
| 263 | |
Kevin Wolf | 252291e | 2018-04-12 17:57:08 +0200 | [diff] [blame] | 264 | JobType job_type(const Job *job) |
| 265 | { |
| 266 | return job->driver->job_type; |
| 267 | } |
| 268 | |
| 269 | const char *job_type_str(const Job *job) |
| 270 | { |
| 271 | return JobType_str(job_type(job)); |
| 272 | } |
| 273 | |
Emanuele Giuseppe Esposito | afe1e8a | 2022-09-26 05:31:58 -0400 | [diff] [blame] | 274 | bool job_is_cancelled_locked(Job *job) |
Kevin Wolf | daa7f2f | 2018-04-17 12:56:07 +0200 | [diff] [blame] | 275 | { |
Hanna Reitz | a640fa0 | 2021-10-06 17:19:39 +0200 | [diff] [blame] | 276 | /* force_cancel may be true only if cancelled is true, too */ |
| 277 | assert(job->cancelled || !job->force_cancel); |
| 278 | return job->force_cancel; |
Hanna Reitz | 08b83bf | 2021-10-06 17:19:35 +0200 | [diff] [blame] | 279 | } |
| 280 | |
Emanuele Giuseppe Esposito | afe1e8a | 2022-09-26 05:31:58 -0400 | [diff] [blame] | 281 | bool job_is_cancelled(Job *job) |
| 282 | { |
| 283 | JOB_LOCK_GUARD(); |
| 284 | return job_is_cancelled_locked(job); |
| 285 | } |
| 286 | |
| 287 | /* Called with job_mutex held. */ |
| 288 | static bool job_cancel_requested_locked(Job *job) |
Hanna Reitz | 08b83bf | 2021-10-06 17:19:35 +0200 | [diff] [blame] | 289 | { |
Kevin Wolf | daa7f2f | 2018-04-17 12:56:07 +0200 | [diff] [blame] | 290 | return job->cancelled; |
| 291 | } |
| 292 | |
Emanuele Giuseppe Esposito | afe1e8a | 2022-09-26 05:31:58 -0400 | [diff] [blame] | 293 | bool job_cancel_requested(Job *job) |
| 294 | { |
| 295 | JOB_LOCK_GUARD(); |
| 296 | return job_cancel_requested_locked(job); |
| 297 | } |
| 298 | |
| 299 | bool job_is_ready_locked(Job *job) |
Kevin Wolf | df956ae | 2018-04-25 15:09:58 +0200 | [diff] [blame] | 300 | { |
| 301 | switch (job->status) { |
| 302 | case JOB_STATUS_UNDEFINED: |
| 303 | case JOB_STATUS_CREATED: |
| 304 | case JOB_STATUS_RUNNING: |
| 305 | case JOB_STATUS_PAUSED: |
| 306 | case JOB_STATUS_WAITING: |
| 307 | case JOB_STATUS_PENDING: |
| 308 | case JOB_STATUS_ABORTING: |
| 309 | case JOB_STATUS_CONCLUDED: |
| 310 | case JOB_STATUS_NULL: |
| 311 | return false; |
| 312 | case JOB_STATUS_READY: |
| 313 | case JOB_STATUS_STANDBY: |
| 314 | return true; |
| 315 | default: |
| 316 | g_assert_not_reached(); |
| 317 | } |
| 318 | return false; |
| 319 | } |
| 320 | |
Emanuele Giuseppe Esposito | afe1e8a | 2022-09-26 05:31:58 -0400 | [diff] [blame] | 321 | bool job_is_ready(Job *job) |
| 322 | { |
| 323 | JOB_LOCK_GUARD(); |
| 324 | return job_is_ready_locked(job); |
| 325 | } |
| 326 | |
| 327 | bool job_is_completed_locked(Job *job) |
| 328 | { |
| 329 | switch (job->status) { |
| 330 | case JOB_STATUS_UNDEFINED: |
| 331 | case JOB_STATUS_CREATED: |
| 332 | case JOB_STATUS_RUNNING: |
| 333 | case JOB_STATUS_PAUSED: |
| 334 | case JOB_STATUS_READY: |
| 335 | case JOB_STATUS_STANDBY: |
| 336 | return false; |
| 337 | case JOB_STATUS_WAITING: |
| 338 | case JOB_STATUS_PENDING: |
| 339 | case JOB_STATUS_ABORTING: |
| 340 | case JOB_STATUS_CONCLUDED: |
| 341 | case JOB_STATUS_NULL: |
| 342 | return true; |
| 343 | default: |
| 344 | g_assert_not_reached(); |
| 345 | } |
| 346 | return false; |
| 347 | } |
| 348 | |
Kevin Wolf | dbe5e6c | 2018-04-19 13:04:01 +0200 | [diff] [blame] | 349 | bool job_is_completed(Job *job) |
| 350 | { |
Emanuele Giuseppe Esposito | afe1e8a | 2022-09-26 05:31:58 -0400 | [diff] [blame] | 351 | JOB_LOCK_GUARD(); |
| 352 | return job_is_completed_locked(job); |
Kevin Wolf | dbe5e6c | 2018-04-19 13:04:01 +0200 | [diff] [blame] | 353 | } |
| 354 | |
Emanuele Giuseppe Esposito | afe1e8a | 2022-09-26 05:31:58 -0400 | [diff] [blame] | 355 | static bool job_started_locked(Job *job) |
Kevin Wolf | da01ff7 | 2018-04-13 17:31:02 +0200 | [diff] [blame] | 356 | { |
| 357 | return job->co; |
| 358 | } |
| 359 | |
Emanuele Giuseppe Esposito | afe1e8a | 2022-09-26 05:31:58 -0400 | [diff] [blame] | 360 | /* Called with job_mutex held. */ |
| 361 | static bool job_should_pause_locked(Job *job) |
Kevin Wolf | da01ff7 | 2018-04-13 17:31:02 +0200 | [diff] [blame] | 362 | { |
| 363 | return job->pause_count > 0; |
| 364 | } |
| 365 | |
Emanuele Giuseppe Esposito | afe1e8a | 2022-09-26 05:31:58 -0400 | [diff] [blame] | 366 | Job *job_next_locked(Job *job) |
Kevin Wolf | e7c1d78 | 2018-04-12 17:54:37 +0200 | [diff] [blame] | 367 | { |
| 368 | if (!job) { |
| 369 | return QLIST_FIRST(&jobs); |
| 370 | } |
| 371 | return QLIST_NEXT(job, job_list); |
| 372 | } |
| 373 | |
Emanuele Giuseppe Esposito | afe1e8a | 2022-09-26 05:31:58 -0400 | [diff] [blame] | 374 | Job *job_next(Job *job) |
| 375 | { |
| 376 | JOB_LOCK_GUARD(); |
| 377 | return job_next_locked(job); |
| 378 | } |
| 379 | |
| 380 | Job *job_get_locked(const char *id) |
Kevin Wolf | e7c1d78 | 2018-04-12 17:54:37 +0200 | [diff] [blame] | 381 | { |
| 382 | Job *job; |
| 383 | |
| 384 | QLIST_FOREACH(job, &jobs, job_list) { |
| 385 | if (job->id && !strcmp(id, job->id)) { |
| 386 | return job; |
| 387 | } |
| 388 | } |
| 389 | |
| 390 | return NULL; |
| 391 | } |
| 392 | |
Emanuele Giuseppe Esposito | afe1e8a | 2022-09-26 05:31:58 -0400 | [diff] [blame] | 393 | Job *job_get(const char *id) |
| 394 | { |
| 395 | JOB_LOCK_GUARD(); |
| 396 | return job_get_locked(id); |
| 397 | } |
| 398 | |
| 399 | /* Called with job_mutex *not* held. */ |
Kevin Wolf | 5d43e86 | 2018-04-18 16:32:20 +0200 | [diff] [blame] | 400 | static void job_sleep_timer_cb(void *opaque) |
| 401 | { |
| 402 | Job *job = opaque; |
| 403 | |
| 404 | job_enter(job); |
| 405 | } |
| 406 | |
Kevin Wolf | 7eaa8fb | 2018-04-23 16:06:26 +0200 | [diff] [blame] | 407 | void *job_create(const char *job_id, const JobDriver *driver, JobTxn *txn, |
| 408 | AioContext *ctx, int flags, BlockCompletionFunc *cb, |
| 409 | void *opaque, Error **errp) |
Kevin Wolf | 33e9e9b | 2018-04-12 17:29:59 +0200 | [diff] [blame] | 410 | { |
| 411 | Job *job; |
| 412 | |
Emanuele Giuseppe Esposito | afe1e8a | 2022-09-26 05:31:58 -0400 | [diff] [blame] | 413 | JOB_LOCK_GUARD(); |
| 414 | |
Kevin Wolf | 33e9e9b | 2018-04-12 17:29:59 +0200 | [diff] [blame] | 415 | if (job_id) { |
Kevin Wolf | bb02b65 | 2018-04-19 17:54:56 +0200 | [diff] [blame] | 416 | if (flags & JOB_INTERNAL) { |
| 417 | error_setg(errp, "Cannot specify job ID for internal job"); |
| 418 | return NULL; |
| 419 | } |
Kevin Wolf | 33e9e9b | 2018-04-12 17:29:59 +0200 | [diff] [blame] | 420 | if (!id_wellformed(job_id)) { |
| 421 | error_setg(errp, "Invalid job ID '%s'", job_id); |
| 422 | return NULL; |
| 423 | } |
Emanuele Giuseppe Esposito | afe1e8a | 2022-09-26 05:31:58 -0400 | [diff] [blame] | 424 | if (job_get_locked(job_id)) { |
Kevin Wolf | e7c1d78 | 2018-04-12 17:54:37 +0200 | [diff] [blame] | 425 | error_setg(errp, "Job ID '%s' already in use", job_id); |
| 426 | return NULL; |
| 427 | } |
Kevin Wolf | bb02b65 | 2018-04-19 17:54:56 +0200 | [diff] [blame] | 428 | } else if (!(flags & JOB_INTERNAL)) { |
| 429 | error_setg(errp, "An explicit job ID is required"); |
| 430 | return NULL; |
Kevin Wolf | 33e9e9b | 2018-04-12 17:29:59 +0200 | [diff] [blame] | 431 | } |
| 432 | |
| 433 | job = g_malloc0(driver->instance_size); |
| 434 | job->driver = driver; |
| 435 | job->id = g_strdup(job_id); |
Kevin Wolf | 80fa2c7 | 2018-04-13 18:50:05 +0200 | [diff] [blame] | 436 | job->refcnt = 1; |
Kevin Wolf | 08be6fe | 2018-04-17 13:49:33 +0200 | [diff] [blame] | 437 | job->aio_context = ctx; |
Kevin Wolf | da01ff7 | 2018-04-13 17:31:02 +0200 | [diff] [blame] | 438 | job->busy = false; |
| 439 | job->paused = true; |
| 440 | job->pause_count = 1; |
Kevin Wolf | bb02b65 | 2018-04-19 17:54:56 +0200 | [diff] [blame] | 441 | job->auto_finalize = !(flags & JOB_MANUAL_FINALIZE); |
| 442 | job->auto_dismiss = !(flags & JOB_MANUAL_DISMISS); |
Kevin Wolf | 4ad3518 | 2018-04-19 17:30:16 +0200 | [diff] [blame] | 443 | job->cb = cb; |
| 444 | job->opaque = opaque; |
Kevin Wolf | 33e9e9b | 2018-04-12 17:29:59 +0200 | [diff] [blame] | 445 | |
Emanuele Giuseppe Esposito | a7b4f8f | 2021-06-14 10:11:29 +0200 | [diff] [blame] | 446 | progress_init(&job->progress); |
| 447 | |
Kevin Wolf | 139a9f0 | 2018-04-23 18:04:57 +0200 | [diff] [blame] | 448 | notifier_list_init(&job->on_finalize_cancelled); |
| 449 | notifier_list_init(&job->on_finalize_completed); |
| 450 | notifier_list_init(&job->on_pending); |
Kevin Wolf | 2e1795b | 2018-04-25 14:56:09 +0200 | [diff] [blame] | 451 | notifier_list_init(&job->on_ready); |
Emanuele Giuseppe Esposito | 252f409 | 2021-11-03 12:21:55 -0400 | [diff] [blame] | 452 | notifier_list_init(&job->on_idle); |
Kevin Wolf | 139a9f0 | 2018-04-23 18:04:57 +0200 | [diff] [blame] | 453 | |
Emanuele Giuseppe Esposito | afe1e8a | 2022-09-26 05:31:58 -0400 | [diff] [blame] | 454 | job_state_transition_locked(job, JOB_STATUS_CREATED); |
Kevin Wolf | 5d43e86 | 2018-04-18 16:32:20 +0200 | [diff] [blame] | 455 | aio_timer_init(qemu_get_aio_context(), &job->sleep_timer, |
| 456 | QEMU_CLOCK_REALTIME, SCALE_NS, |
| 457 | job_sleep_timer_cb, job); |
Kevin Wolf | a50c2ab | 2018-04-13 17:19:31 +0200 | [diff] [blame] | 458 | |
Kevin Wolf | e7c1d78 | 2018-04-12 17:54:37 +0200 | [diff] [blame] | 459 | QLIST_INSERT_HEAD(&jobs, job, job_list); |
| 460 | |
Kevin Wolf | 7eaa8fb | 2018-04-23 16:06:26 +0200 | [diff] [blame] | 461 | /* Single jobs are modeled as single-job transactions for sake of |
| 462 | * consolidating the job management logic */ |
| 463 | if (!txn) { |
| 464 | txn = job_txn_new(); |
Emanuele Giuseppe Esposito | afe1e8a | 2022-09-26 05:31:58 -0400 | [diff] [blame] | 465 | job_txn_add_job_locked(txn, job); |
| 466 | job_txn_unref_locked(txn); |
Kevin Wolf | 7eaa8fb | 2018-04-23 16:06:26 +0200 | [diff] [blame] | 467 | } else { |
Emanuele Giuseppe Esposito | afe1e8a | 2022-09-26 05:31:58 -0400 | [diff] [blame] | 468 | job_txn_add_job_locked(txn, job); |
Kevin Wolf | 7eaa8fb | 2018-04-23 16:06:26 +0200 | [diff] [blame] | 469 | } |
| 470 | |
Kevin Wolf | 33e9e9b | 2018-04-12 17:29:59 +0200 | [diff] [blame] | 471 | return job; |
| 472 | } |
Kevin Wolf | fd61a70 | 2018-04-12 19:06:53 +0200 | [diff] [blame] | 473 | |
Emanuele Giuseppe Esposito | afe1e8a | 2022-09-26 05:31:58 -0400 | [diff] [blame] | 474 | void job_ref_locked(Job *job) |
Kevin Wolf | fd61a70 | 2018-04-12 19:06:53 +0200 | [diff] [blame] | 475 | { |
Kevin Wolf | 80fa2c7 | 2018-04-13 18:50:05 +0200 | [diff] [blame] | 476 | ++job->refcnt; |
| 477 | } |
Kevin Wolf | e7c1d78 | 2018-04-12 17:54:37 +0200 | [diff] [blame] | 478 | |
Emanuele Giuseppe Esposito | afe1e8a | 2022-09-26 05:31:58 -0400 | [diff] [blame] | 479 | void job_ref(Job *job) |
| 480 | { |
| 481 | JOB_LOCK_GUARD(); |
| 482 | job_ref_locked(job); |
| 483 | } |
| 484 | |
| 485 | void job_unref_locked(Job *job) |
Kevin Wolf | 80fa2c7 | 2018-04-13 18:50:05 +0200 | [diff] [blame] | 486 | { |
Emanuele Giuseppe Esposito | c70b803 | 2022-03-03 10:16:16 -0500 | [diff] [blame] | 487 | GLOBAL_STATE_CODE(); |
| 488 | |
Kevin Wolf | 80fa2c7 | 2018-04-13 18:50:05 +0200 | [diff] [blame] | 489 | if (--job->refcnt == 0) { |
| 490 | assert(job->status == JOB_STATUS_NULL); |
Kevin Wolf | 5d43e86 | 2018-04-18 16:32:20 +0200 | [diff] [blame] | 491 | assert(!timer_pending(&job->sleep_timer)); |
Kevin Wolf | 7eaa8fb | 2018-04-23 16:06:26 +0200 | [diff] [blame] | 492 | assert(!job->txn); |
Kevin Wolf | 80fa2c7 | 2018-04-13 18:50:05 +0200 | [diff] [blame] | 493 | |
| 494 | if (job->driver->free) { |
Emanuele Giuseppe Esposito | afe1e8a | 2022-09-26 05:31:58 -0400 | [diff] [blame] | 495 | job_unlock(); |
Kevin Wolf | 80fa2c7 | 2018-04-13 18:50:05 +0200 | [diff] [blame] | 496 | job->driver->free(job); |
Emanuele Giuseppe Esposito | afe1e8a | 2022-09-26 05:31:58 -0400 | [diff] [blame] | 497 | job_lock(); |
Kevin Wolf | 80fa2c7 | 2018-04-13 18:50:05 +0200 | [diff] [blame] | 498 | } |
| 499 | |
| 500 | QLIST_REMOVE(job, job_list); |
| 501 | |
Emanuele Giuseppe Esposito | a7b4f8f | 2021-06-14 10:11:29 +0200 | [diff] [blame] | 502 | progress_destroy(&job->progress); |
John Snow | 3d1f8b0 | 2018-08-29 21:57:27 -0400 | [diff] [blame] | 503 | error_free(job->err); |
Kevin Wolf | 80fa2c7 | 2018-04-13 18:50:05 +0200 | [diff] [blame] | 504 | g_free(job->id); |
| 505 | g_free(job); |
| 506 | } |
Kevin Wolf | fd61a70 | 2018-04-12 19:06:53 +0200 | [diff] [blame] | 507 | } |
Kevin Wolf | 1908a55 | 2018-04-17 16:41:17 +0200 | [diff] [blame] | 508 | |
Emanuele Giuseppe Esposito | afe1e8a | 2022-09-26 05:31:58 -0400 | [diff] [blame] | 509 | void job_unref(Job *job) |
| 510 | { |
| 511 | JOB_LOCK_GUARD(); |
| 512 | job_unref_locked(job); |
| 513 | } |
| 514 | |
Kevin Wolf | 30a5c88 | 2018-05-04 12:17:20 +0200 | [diff] [blame] | 515 | void job_progress_update(Job *job, uint64_t done) |
| 516 | { |
Vladimir Sementsov-Ogievskiy | 01fe1ca | 2020-03-11 13:29:56 +0300 | [diff] [blame] | 517 | progress_work_done(&job->progress, done); |
Kevin Wolf | 30a5c88 | 2018-05-04 12:17:20 +0200 | [diff] [blame] | 518 | } |
| 519 | |
| 520 | void job_progress_set_remaining(Job *job, uint64_t remaining) |
| 521 | { |
Vladimir Sementsov-Ogievskiy | 01fe1ca | 2020-03-11 13:29:56 +0300 | [diff] [blame] | 522 | progress_set_remaining(&job->progress, remaining); |
Kevin Wolf | 30a5c88 | 2018-05-04 12:17:20 +0200 | [diff] [blame] | 523 | } |
| 524 | |
Max Reitz | 62f1360 | 2018-06-13 20:18:20 +0200 | [diff] [blame] | 525 | void job_progress_increase_remaining(Job *job, uint64_t delta) |
| 526 | { |
Vladimir Sementsov-Ogievskiy | 01fe1ca | 2020-03-11 13:29:56 +0300 | [diff] [blame] | 527 | progress_increase_remaining(&job->progress, delta); |
Max Reitz | 62f1360 | 2018-06-13 20:18:20 +0200 | [diff] [blame] | 528 | } |
| 529 | |
Emanuele Giuseppe Esposito | 544f4d5 | 2022-09-26 05:31:56 -0400 | [diff] [blame] | 530 | /** |
| 531 | * To be called when a cancelled job is finalised. |
Emanuele Giuseppe Esposito | afe1e8a | 2022-09-26 05:31:58 -0400 | [diff] [blame] | 532 | * Called with job_mutex held. |
Emanuele Giuseppe Esposito | 544f4d5 | 2022-09-26 05:31:56 -0400 | [diff] [blame] | 533 | */ |
Emanuele Giuseppe Esposito | afe1e8a | 2022-09-26 05:31:58 -0400 | [diff] [blame] | 534 | static void job_event_cancelled_locked(Job *job) |
Kevin Wolf | 139a9f0 | 2018-04-23 18:04:57 +0200 | [diff] [blame] | 535 | { |
| 536 | notifier_list_notify(&job->on_finalize_cancelled, job); |
| 537 | } |
| 538 | |
Emanuele Giuseppe Esposito | 544f4d5 | 2022-09-26 05:31:56 -0400 | [diff] [blame] | 539 | /** |
| 540 | * To be called when a successfully completed job is finalised. |
Emanuele Giuseppe Esposito | afe1e8a | 2022-09-26 05:31:58 -0400 | [diff] [blame] | 541 | * Called with job_mutex held. |
Emanuele Giuseppe Esposito | 544f4d5 | 2022-09-26 05:31:56 -0400 | [diff] [blame] | 542 | */ |
Emanuele Giuseppe Esposito | afe1e8a | 2022-09-26 05:31:58 -0400 | [diff] [blame] | 543 | static void job_event_completed_locked(Job *job) |
Kevin Wolf | 139a9f0 | 2018-04-23 18:04:57 +0200 | [diff] [blame] | 544 | { |
| 545 | notifier_list_notify(&job->on_finalize_completed, job); |
| 546 | } |
| 547 | |
Emanuele Giuseppe Esposito | afe1e8a | 2022-09-26 05:31:58 -0400 | [diff] [blame] | 548 | /* Called with job_mutex held. */ |
| 549 | static void job_event_pending_locked(Job *job) |
Kevin Wolf | 139a9f0 | 2018-04-23 18:04:57 +0200 | [diff] [blame] | 550 | { |
| 551 | notifier_list_notify(&job->on_pending, job); |
| 552 | } |
| 553 | |
Emanuele Giuseppe Esposito | afe1e8a | 2022-09-26 05:31:58 -0400 | [diff] [blame] | 554 | /* Called with job_mutex held. */ |
| 555 | static void job_event_ready_locked(Job *job) |
Kevin Wolf | 2e1795b | 2018-04-25 14:56:09 +0200 | [diff] [blame] | 556 | { |
| 557 | notifier_list_notify(&job->on_ready, job); |
| 558 | } |
| 559 | |
Emanuele Giuseppe Esposito | afe1e8a | 2022-09-26 05:31:58 -0400 | [diff] [blame] | 560 | /* Called with job_mutex held. */ |
| 561 | static void job_event_idle_locked(Job *job) |
Kevin Wolf | 34dc97b | 2018-08-17 14:53:05 +0200 | [diff] [blame] | 562 | { |
| 563 | notifier_list_notify(&job->on_idle, job); |
| 564 | } |
| 565 | |
Emanuele Giuseppe Esposito | afe1e8a | 2022-09-26 05:31:58 -0400 | [diff] [blame] | 566 | void job_enter_cond_locked(Job *job, bool(*fn)(Job *job)) |
Kevin Wolf | da01ff7 | 2018-04-13 17:31:02 +0200 | [diff] [blame] | 567 | { |
Emanuele Giuseppe Esposito | afe1e8a | 2022-09-26 05:31:58 -0400 | [diff] [blame] | 568 | if (!job_started_locked(job)) { |
Kevin Wolf | da01ff7 | 2018-04-13 17:31:02 +0200 | [diff] [blame] | 569 | return; |
| 570 | } |
| 571 | if (job->deferred_to_main_loop) { |
| 572 | return; |
| 573 | } |
| 574 | |
Emanuele Giuseppe Esposito | 55c5a25 | 2022-09-26 05:31:54 -0400 | [diff] [blame] | 575 | real_job_lock(); |
Kevin Wolf | da01ff7 | 2018-04-13 17:31:02 +0200 | [diff] [blame] | 576 | if (job->busy) { |
Emanuele Giuseppe Esposito | 55c5a25 | 2022-09-26 05:31:54 -0400 | [diff] [blame] | 577 | real_job_unlock(); |
Kevin Wolf | da01ff7 | 2018-04-13 17:31:02 +0200 | [diff] [blame] | 578 | return; |
| 579 | } |
| 580 | |
| 581 | if (fn && !fn(job)) { |
Emanuele Giuseppe Esposito | 55c5a25 | 2022-09-26 05:31:54 -0400 | [diff] [blame] | 582 | real_job_unlock(); |
Kevin Wolf | da01ff7 | 2018-04-13 17:31:02 +0200 | [diff] [blame] | 583 | return; |
| 584 | } |
| 585 | |
| 586 | assert(!job->deferred_to_main_loop); |
| 587 | timer_del(&job->sleep_timer); |
| 588 | job->busy = true; |
Emanuele Giuseppe Esposito | 55c5a25 | 2022-09-26 05:31:54 -0400 | [diff] [blame] | 589 | real_job_unlock(); |
Emanuele Giuseppe Esposito | afe1e8a | 2022-09-26 05:31:58 -0400 | [diff] [blame] | 590 | job_unlock(); |
Paolo Bonzini | ef02dac | 2022-09-26 05:32:05 -0400 | [diff] [blame^] | 591 | aio_co_wake(job->co); |
Emanuele Giuseppe Esposito | afe1e8a | 2022-09-26 05:31:58 -0400 | [diff] [blame] | 592 | job_lock(); |
| 593 | } |
| 594 | |
| 595 | void job_enter_cond(Job *job, bool(*fn)(Job *job)) |
| 596 | { |
| 597 | JOB_LOCK_GUARD(); |
| 598 | job_enter_cond_locked(job, fn); |
Kevin Wolf | da01ff7 | 2018-04-13 17:31:02 +0200 | [diff] [blame] | 599 | } |
| 600 | |
Kevin Wolf | 5d43e86 | 2018-04-18 16:32:20 +0200 | [diff] [blame] | 601 | void job_enter(Job *job) |
| 602 | { |
Emanuele Giuseppe Esposito | afe1e8a | 2022-09-26 05:31:58 -0400 | [diff] [blame] | 603 | JOB_LOCK_GUARD(); |
| 604 | job_enter_cond_locked(job, NULL); |
Kevin Wolf | 5d43e86 | 2018-04-18 16:32:20 +0200 | [diff] [blame] | 605 | } |
| 606 | |
Kevin Wolf | da01ff7 | 2018-04-13 17:31:02 +0200 | [diff] [blame] | 607 | /* Yield, and schedule a timer to reenter the coroutine after @ns nanoseconds. |
Kevin Wolf | 3d70ff5 | 2018-04-24 16:13:52 +0200 | [diff] [blame] | 608 | * Reentering the job coroutine with job_enter() before the timer has expired |
| 609 | * is allowed and cancels the timer. |
Kevin Wolf | da01ff7 | 2018-04-13 17:31:02 +0200 | [diff] [blame] | 610 | * |
Kevin Wolf | 3d70ff5 | 2018-04-24 16:13:52 +0200 | [diff] [blame] | 611 | * If @ns is (uint64_t) -1, no timer is scheduled and job_enter() must be |
Emanuele Giuseppe Esposito | afe1e8a | 2022-09-26 05:31:58 -0400 | [diff] [blame] | 612 | * called explicitly. |
| 613 | * |
| 614 | * Called with job_mutex held, but releases it temporarily. |
| 615 | */ |
| 616 | static void coroutine_fn job_do_yield_locked(Job *job, uint64_t ns) |
Kevin Wolf | da01ff7 | 2018-04-13 17:31:02 +0200 | [diff] [blame] | 617 | { |
Paolo Bonzini | ef02dac | 2022-09-26 05:32:05 -0400 | [diff] [blame^] | 618 | AioContext *next_aio_context; |
| 619 | |
Emanuele Giuseppe Esposito | 55c5a25 | 2022-09-26 05:31:54 -0400 | [diff] [blame] | 620 | real_job_lock(); |
Kevin Wolf | da01ff7 | 2018-04-13 17:31:02 +0200 | [diff] [blame] | 621 | if (ns != -1) { |
| 622 | timer_mod(&job->sleep_timer, ns); |
| 623 | } |
| 624 | job->busy = false; |
Emanuele Giuseppe Esposito | afe1e8a | 2022-09-26 05:31:58 -0400 | [diff] [blame] | 625 | job_event_idle_locked(job); |
Emanuele Giuseppe Esposito | 55c5a25 | 2022-09-26 05:31:54 -0400 | [diff] [blame] | 626 | real_job_unlock(); |
Emanuele Giuseppe Esposito | afe1e8a | 2022-09-26 05:31:58 -0400 | [diff] [blame] | 627 | job_unlock(); |
Kevin Wolf | da01ff7 | 2018-04-13 17:31:02 +0200 | [diff] [blame] | 628 | qemu_coroutine_yield(); |
Emanuele Giuseppe Esposito | afe1e8a | 2022-09-26 05:31:58 -0400 | [diff] [blame] | 629 | job_lock(); |
Kevin Wolf | da01ff7 | 2018-04-13 17:31:02 +0200 | [diff] [blame] | 630 | |
Paolo Bonzini | ef02dac | 2022-09-26 05:32:05 -0400 | [diff] [blame^] | 631 | next_aio_context = job->aio_context; |
| 632 | /* |
| 633 | * Coroutine has resumed, but in the meanwhile the job AioContext |
| 634 | * might have changed via bdrv_try_set_aio_context(), so we need to move |
| 635 | * the coroutine too in the new aiocontext. |
| 636 | */ |
| 637 | while (qemu_get_current_aio_context() != next_aio_context) { |
| 638 | job_unlock(); |
| 639 | aio_co_reschedule_self(next_aio_context); |
| 640 | job_lock(); |
| 641 | next_aio_context = job->aio_context; |
| 642 | } |
| 643 | |
| 644 | /* Set by job_enter_cond_locked() before re-entering the coroutine. */ |
Kevin Wolf | da01ff7 | 2018-04-13 17:31:02 +0200 | [diff] [blame] | 645 | assert(job->busy); |
| 646 | } |
| 647 | |
Emanuele Giuseppe Esposito | afe1e8a | 2022-09-26 05:31:58 -0400 | [diff] [blame] | 648 | /* Called with job_mutex held, but releases it temporarily. */ |
| 649 | static void coroutine_fn job_pause_point_locked(Job *job) |
Kevin Wolf | da01ff7 | 2018-04-13 17:31:02 +0200 | [diff] [blame] | 650 | { |
Emanuele Giuseppe Esposito | afe1e8a | 2022-09-26 05:31:58 -0400 | [diff] [blame] | 651 | assert(job && job_started_locked(job)); |
Kevin Wolf | da01ff7 | 2018-04-13 17:31:02 +0200 | [diff] [blame] | 652 | |
Emanuele Giuseppe Esposito | afe1e8a | 2022-09-26 05:31:58 -0400 | [diff] [blame] | 653 | if (!job_should_pause_locked(job)) { |
Kevin Wolf | da01ff7 | 2018-04-13 17:31:02 +0200 | [diff] [blame] | 654 | return; |
| 655 | } |
Emanuele Giuseppe Esposito | afe1e8a | 2022-09-26 05:31:58 -0400 | [diff] [blame] | 656 | if (job_is_cancelled_locked(job)) { |
Kevin Wolf | da01ff7 | 2018-04-13 17:31:02 +0200 | [diff] [blame] | 657 | return; |
| 658 | } |
| 659 | |
| 660 | if (job->driver->pause) { |
Emanuele Giuseppe Esposito | afe1e8a | 2022-09-26 05:31:58 -0400 | [diff] [blame] | 661 | job_unlock(); |
Kevin Wolf | da01ff7 | 2018-04-13 17:31:02 +0200 | [diff] [blame] | 662 | job->driver->pause(job); |
Emanuele Giuseppe Esposito | afe1e8a | 2022-09-26 05:31:58 -0400 | [diff] [blame] | 663 | job_lock(); |
Kevin Wolf | da01ff7 | 2018-04-13 17:31:02 +0200 | [diff] [blame] | 664 | } |
| 665 | |
Emanuele Giuseppe Esposito | afe1e8a | 2022-09-26 05:31:58 -0400 | [diff] [blame] | 666 | if (job_should_pause_locked(job) && !job_is_cancelled_locked(job)) { |
Kevin Wolf | da01ff7 | 2018-04-13 17:31:02 +0200 | [diff] [blame] | 667 | JobStatus status = job->status; |
Emanuele Giuseppe Esposito | afe1e8a | 2022-09-26 05:31:58 -0400 | [diff] [blame] | 668 | job_state_transition_locked(job, status == JOB_STATUS_READY |
| 669 | ? JOB_STATUS_STANDBY |
| 670 | : JOB_STATUS_PAUSED); |
Kevin Wolf | da01ff7 | 2018-04-13 17:31:02 +0200 | [diff] [blame] | 671 | job->paused = true; |
Emanuele Giuseppe Esposito | afe1e8a | 2022-09-26 05:31:58 -0400 | [diff] [blame] | 672 | job_do_yield_locked(job, -1); |
Kevin Wolf | da01ff7 | 2018-04-13 17:31:02 +0200 | [diff] [blame] | 673 | job->paused = false; |
Emanuele Giuseppe Esposito | afe1e8a | 2022-09-26 05:31:58 -0400 | [diff] [blame] | 674 | job_state_transition_locked(job, status); |
Kevin Wolf | da01ff7 | 2018-04-13 17:31:02 +0200 | [diff] [blame] | 675 | } |
| 676 | |
| 677 | if (job->driver->resume) { |
Emanuele Giuseppe Esposito | afe1e8a | 2022-09-26 05:31:58 -0400 | [diff] [blame] | 678 | job_unlock(); |
Kevin Wolf | da01ff7 | 2018-04-13 17:31:02 +0200 | [diff] [blame] | 679 | job->driver->resume(job); |
Emanuele Giuseppe Esposito | afe1e8a | 2022-09-26 05:31:58 -0400 | [diff] [blame] | 680 | job_lock(); |
Kevin Wolf | da01ff7 | 2018-04-13 17:31:02 +0200 | [diff] [blame] | 681 | } |
| 682 | } |
| 683 | |
Emanuele Giuseppe Esposito | afe1e8a | 2022-09-26 05:31:58 -0400 | [diff] [blame] | 684 | void coroutine_fn job_pause_point(Job *job) |
| 685 | { |
| 686 | JOB_LOCK_GUARD(); |
| 687 | job_pause_point_locked(job); |
| 688 | } |
| 689 | |
| 690 | static void coroutine_fn job_yield_locked(Job *job) |
| 691 | { |
| 692 | assert(job->busy); |
| 693 | |
| 694 | /* Check cancellation *before* setting busy = false, too! */ |
| 695 | if (job_is_cancelled_locked(job)) { |
| 696 | return; |
| 697 | } |
| 698 | |
| 699 | if (!job_should_pause_locked(job)) { |
| 700 | job_do_yield_locked(job, -1); |
| 701 | } |
| 702 | |
| 703 | job_pause_point_locked(job); |
| 704 | } |
| 705 | |
Paolo Bonzini | 06753a0 | 2022-09-22 10:49:19 +0200 | [diff] [blame] | 706 | void coroutine_fn job_yield(Job *job) |
Kevin Wolf | 198c49c | 2018-04-24 16:55:04 +0200 | [diff] [blame] | 707 | { |
Emanuele Giuseppe Esposito | afe1e8a | 2022-09-26 05:31:58 -0400 | [diff] [blame] | 708 | JOB_LOCK_GUARD(); |
| 709 | job_yield_locked(job); |
Kevin Wolf | 198c49c | 2018-04-24 16:55:04 +0200 | [diff] [blame] | 710 | } |
| 711 | |
Kevin Wolf | 5d43e86 | 2018-04-18 16:32:20 +0200 | [diff] [blame] | 712 | void coroutine_fn job_sleep_ns(Job *job, int64_t ns) |
| 713 | { |
Emanuele Giuseppe Esposito | afe1e8a | 2022-09-26 05:31:58 -0400 | [diff] [blame] | 714 | JOB_LOCK_GUARD(); |
Kevin Wolf | 5d43e86 | 2018-04-18 16:32:20 +0200 | [diff] [blame] | 715 | assert(job->busy); |
| 716 | |
| 717 | /* Check cancellation *before* setting busy = false, too! */ |
Emanuele Giuseppe Esposito | afe1e8a | 2022-09-26 05:31:58 -0400 | [diff] [blame] | 718 | if (job_is_cancelled_locked(job)) { |
Kevin Wolf | 5d43e86 | 2018-04-18 16:32:20 +0200 | [diff] [blame] | 719 | return; |
| 720 | } |
| 721 | |
Emanuele Giuseppe Esposito | afe1e8a | 2022-09-26 05:31:58 -0400 | [diff] [blame] | 722 | if (!job_should_pause_locked(job)) { |
| 723 | job_do_yield_locked(job, qemu_clock_get_ns(QEMU_CLOCK_REALTIME) + ns); |
Kevin Wolf | 5d43e86 | 2018-04-18 16:32:20 +0200 | [diff] [blame] | 724 | } |
| 725 | |
Emanuele Giuseppe Esposito | afe1e8a | 2022-09-26 05:31:58 -0400 | [diff] [blame] | 726 | job_pause_point_locked(job); |
Kevin Wolf | 5d43e86 | 2018-04-18 16:32:20 +0200 | [diff] [blame] | 727 | } |
| 728 | |
Emanuele Giuseppe Esposito | afe1e8a | 2022-09-26 05:31:58 -0400 | [diff] [blame] | 729 | /* Assumes the job_mutex is held */ |
| 730 | static bool job_timer_not_pending_locked(Job *job) |
Kevin Wolf | b15de82 | 2018-04-18 17:10:26 +0200 | [diff] [blame] | 731 | { |
| 732 | return !timer_pending(&job->sleep_timer); |
| 733 | } |
| 734 | |
Emanuele Giuseppe Esposito | afe1e8a | 2022-09-26 05:31:58 -0400 | [diff] [blame] | 735 | void job_pause_locked(Job *job) |
Kevin Wolf | b15de82 | 2018-04-18 17:10:26 +0200 | [diff] [blame] | 736 | { |
| 737 | job->pause_count++; |
Vladimir Sementsov-Ogievskiy | 3ee1483 | 2021-01-17 00:46:51 +0300 | [diff] [blame] | 738 | if (!job->paused) { |
Emanuele Giuseppe Esposito | afe1e8a | 2022-09-26 05:31:58 -0400 | [diff] [blame] | 739 | job_enter_cond_locked(job, NULL); |
Vladimir Sementsov-Ogievskiy | 3ee1483 | 2021-01-17 00:46:51 +0300 | [diff] [blame] | 740 | } |
Kevin Wolf | b15de82 | 2018-04-18 17:10:26 +0200 | [diff] [blame] | 741 | } |
| 742 | |
Emanuele Giuseppe Esposito | afe1e8a | 2022-09-26 05:31:58 -0400 | [diff] [blame] | 743 | void job_pause(Job *job) |
| 744 | { |
| 745 | JOB_LOCK_GUARD(); |
| 746 | job_pause_locked(job); |
| 747 | } |
| 748 | |
| 749 | void job_resume_locked(Job *job) |
Kevin Wolf | b15de82 | 2018-04-18 17:10:26 +0200 | [diff] [blame] | 750 | { |
| 751 | assert(job->pause_count > 0); |
| 752 | job->pause_count--; |
| 753 | if (job->pause_count) { |
| 754 | return; |
| 755 | } |
| 756 | |
| 757 | /* kick only if no timer is pending */ |
Emanuele Giuseppe Esposito | afe1e8a | 2022-09-26 05:31:58 -0400 | [diff] [blame] | 758 | job_enter_cond_locked(job, job_timer_not_pending_locked); |
Kevin Wolf | b15de82 | 2018-04-18 17:10:26 +0200 | [diff] [blame] | 759 | } |
| 760 | |
Emanuele Giuseppe Esposito | afe1e8a | 2022-09-26 05:31:58 -0400 | [diff] [blame] | 761 | void job_resume(Job *job) |
Kevin Wolf | b15de82 | 2018-04-18 17:10:26 +0200 | [diff] [blame] | 762 | { |
Emanuele Giuseppe Esposito | afe1e8a | 2022-09-26 05:31:58 -0400 | [diff] [blame] | 763 | JOB_LOCK_GUARD(); |
| 764 | job_resume_locked(job); |
| 765 | } |
| 766 | |
| 767 | void job_user_pause_locked(Job *job, Error **errp) |
| 768 | { |
| 769 | if (job_apply_verb_locked(job, JOB_VERB_PAUSE, errp)) { |
Kevin Wolf | b15de82 | 2018-04-18 17:10:26 +0200 | [diff] [blame] | 770 | return; |
| 771 | } |
| 772 | if (job->user_paused) { |
| 773 | error_setg(errp, "Job is already paused"); |
| 774 | return; |
| 775 | } |
| 776 | job->user_paused = true; |
Emanuele Giuseppe Esposito | afe1e8a | 2022-09-26 05:31:58 -0400 | [diff] [blame] | 777 | job_pause_locked(job); |
Kevin Wolf | b15de82 | 2018-04-18 17:10:26 +0200 | [diff] [blame] | 778 | } |
| 779 | |
Emanuele Giuseppe Esposito | afe1e8a | 2022-09-26 05:31:58 -0400 | [diff] [blame] | 780 | void job_user_pause(Job *job, Error **errp) |
| 781 | { |
| 782 | JOB_LOCK_GUARD(); |
| 783 | job_user_pause_locked(job, errp); |
| 784 | } |
| 785 | |
| 786 | bool job_user_paused_locked(Job *job) |
Kevin Wolf | b15de82 | 2018-04-18 17:10:26 +0200 | [diff] [blame] | 787 | { |
| 788 | return job->user_paused; |
| 789 | } |
| 790 | |
Emanuele Giuseppe Esposito | afe1e8a | 2022-09-26 05:31:58 -0400 | [diff] [blame] | 791 | bool job_user_paused(Job *job) |
| 792 | { |
| 793 | JOB_LOCK_GUARD(); |
| 794 | return job_user_paused_locked(job); |
| 795 | } |
| 796 | |
| 797 | void job_user_resume_locked(Job *job, Error **errp) |
Kevin Wolf | b15de82 | 2018-04-18 17:10:26 +0200 | [diff] [blame] | 798 | { |
| 799 | assert(job); |
Emanuele Giuseppe Esposito | c70b803 | 2022-03-03 10:16:16 -0500 | [diff] [blame] | 800 | GLOBAL_STATE_CODE(); |
Kevin Wolf | b15de82 | 2018-04-18 17:10:26 +0200 | [diff] [blame] | 801 | if (!job->user_paused || job->pause_count <= 0) { |
| 802 | error_setg(errp, "Can't resume a job that was not paused"); |
| 803 | return; |
| 804 | } |
Emanuele Giuseppe Esposito | afe1e8a | 2022-09-26 05:31:58 -0400 | [diff] [blame] | 805 | if (job_apply_verb_locked(job, JOB_VERB_RESUME, errp)) { |
Kevin Wolf | b15de82 | 2018-04-18 17:10:26 +0200 | [diff] [blame] | 806 | return; |
| 807 | } |
| 808 | if (job->driver->user_resume) { |
Emanuele Giuseppe Esposito | afe1e8a | 2022-09-26 05:31:58 -0400 | [diff] [blame] | 809 | job_unlock(); |
Kevin Wolf | b15de82 | 2018-04-18 17:10:26 +0200 | [diff] [blame] | 810 | job->driver->user_resume(job); |
Emanuele Giuseppe Esposito | afe1e8a | 2022-09-26 05:31:58 -0400 | [diff] [blame] | 811 | job_lock(); |
Kevin Wolf | b15de82 | 2018-04-18 17:10:26 +0200 | [diff] [blame] | 812 | } |
| 813 | job->user_paused = false; |
Emanuele Giuseppe Esposito | afe1e8a | 2022-09-26 05:31:58 -0400 | [diff] [blame] | 814 | job_resume_locked(job); |
Kevin Wolf | b15de82 | 2018-04-18 17:10:26 +0200 | [diff] [blame] | 815 | } |
| 816 | |
Emanuele Giuseppe Esposito | afe1e8a | 2022-09-26 05:31:58 -0400 | [diff] [blame] | 817 | void job_user_resume(Job *job, Error **errp) |
| 818 | { |
| 819 | JOB_LOCK_GUARD(); |
| 820 | job_user_resume_locked(job, errp); |
| 821 | } |
| 822 | |
| 823 | /* Called with job_mutex held, but releases it temporarily. */ |
| 824 | static void job_do_dismiss_locked(Job *job) |
Kevin Wolf | 4ad3518 | 2018-04-19 17:30:16 +0200 | [diff] [blame] | 825 | { |
| 826 | assert(job); |
| 827 | job->busy = false; |
| 828 | job->paused = false; |
| 829 | job->deferred_to_main_loop = true; |
| 830 | |
Emanuele Giuseppe Esposito | afe1e8a | 2022-09-26 05:31:58 -0400 | [diff] [blame] | 831 | job_txn_del_job_locked(job); |
Kevin Wolf | 4ad3518 | 2018-04-19 17:30:16 +0200 | [diff] [blame] | 832 | |
Emanuele Giuseppe Esposito | afe1e8a | 2022-09-26 05:31:58 -0400 | [diff] [blame] | 833 | job_state_transition_locked(job, JOB_STATUS_NULL); |
| 834 | job_unref_locked(job); |
Kevin Wolf | 4ad3518 | 2018-04-19 17:30:16 +0200 | [diff] [blame] | 835 | } |
| 836 | |
Emanuele Giuseppe Esposito | afe1e8a | 2022-09-26 05:31:58 -0400 | [diff] [blame] | 837 | void job_dismiss_locked(Job **jobptr, Error **errp) |
Kevin Wolf | 5f9a6a0 | 2018-04-24 17:10:12 +0200 | [diff] [blame] | 838 | { |
| 839 | Job *job = *jobptr; |
| 840 | /* similarly to _complete, this is QMP-interface only. */ |
| 841 | assert(job->id); |
Emanuele Giuseppe Esposito | afe1e8a | 2022-09-26 05:31:58 -0400 | [diff] [blame] | 842 | if (job_apply_verb_locked(job, JOB_VERB_DISMISS, errp)) { |
Kevin Wolf | 5f9a6a0 | 2018-04-24 17:10:12 +0200 | [diff] [blame] | 843 | return; |
| 844 | } |
| 845 | |
Emanuele Giuseppe Esposito | afe1e8a | 2022-09-26 05:31:58 -0400 | [diff] [blame] | 846 | job_do_dismiss_locked(job); |
Kevin Wolf | 5f9a6a0 | 2018-04-24 17:10:12 +0200 | [diff] [blame] | 847 | *jobptr = NULL; |
| 848 | } |
| 849 | |
Emanuele Giuseppe Esposito | afe1e8a | 2022-09-26 05:31:58 -0400 | [diff] [blame] | 850 | void job_dismiss(Job **jobptr, Error **errp) |
| 851 | { |
| 852 | JOB_LOCK_GUARD(); |
| 853 | job_dismiss_locked(jobptr, errp); |
| 854 | } |
| 855 | |
Kevin Wolf | 4ad3518 | 2018-04-19 17:30:16 +0200 | [diff] [blame] | 856 | void job_early_fail(Job *job) |
| 857 | { |
Emanuele Giuseppe Esposito | afe1e8a | 2022-09-26 05:31:58 -0400 | [diff] [blame] | 858 | JOB_LOCK_GUARD(); |
Kevin Wolf | 4ad3518 | 2018-04-19 17:30:16 +0200 | [diff] [blame] | 859 | assert(job->status == JOB_STATUS_CREATED); |
Emanuele Giuseppe Esposito | afe1e8a | 2022-09-26 05:31:58 -0400 | [diff] [blame] | 860 | job_do_dismiss_locked(job); |
Kevin Wolf | 4ad3518 | 2018-04-19 17:30:16 +0200 | [diff] [blame] | 861 | } |
| 862 | |
Emanuele Giuseppe Esposito | afe1e8a | 2022-09-26 05:31:58 -0400 | [diff] [blame] | 863 | /* Called with job_mutex held. */ |
| 864 | static void job_conclude_locked(Job *job) |
Kevin Wolf | 4ad3518 | 2018-04-19 17:30:16 +0200 | [diff] [blame] | 865 | { |
Emanuele Giuseppe Esposito | afe1e8a | 2022-09-26 05:31:58 -0400 | [diff] [blame] | 866 | job_state_transition_locked(job, JOB_STATUS_CONCLUDED); |
| 867 | if (job->auto_dismiss || !job_started_locked(job)) { |
| 868 | job_do_dismiss_locked(job); |
Kevin Wolf | 4ad3518 | 2018-04-19 17:30:16 +0200 | [diff] [blame] | 869 | } |
| 870 | } |
| 871 | |
Emanuele Giuseppe Esposito | afe1e8a | 2022-09-26 05:31:58 -0400 | [diff] [blame] | 872 | /* Called with job_mutex held. */ |
| 873 | static void job_update_rc_locked(Job *job) |
Kevin Wolf | 4ad3518 | 2018-04-19 17:30:16 +0200 | [diff] [blame] | 874 | { |
Emanuele Giuseppe Esposito | afe1e8a | 2022-09-26 05:31:58 -0400 | [diff] [blame] | 875 | if (!job->ret && job_is_cancelled_locked(job)) { |
Kevin Wolf | 4ad3518 | 2018-04-19 17:30:16 +0200 | [diff] [blame] | 876 | job->ret = -ECANCELED; |
| 877 | } |
| 878 | if (job->ret) { |
John Snow | 3d1f8b0 | 2018-08-29 21:57:27 -0400 | [diff] [blame] | 879 | if (!job->err) { |
| 880 | error_setg(&job->err, "%s", strerror(-job->ret)); |
Kevin Wolf | 1266c9b | 2018-05-24 15:26:10 +0200 | [diff] [blame] | 881 | } |
Emanuele Giuseppe Esposito | afe1e8a | 2022-09-26 05:31:58 -0400 | [diff] [blame] | 882 | job_state_transition_locked(job, JOB_STATUS_ABORTING); |
Kevin Wolf | 4ad3518 | 2018-04-19 17:30:16 +0200 | [diff] [blame] | 883 | } |
| 884 | } |
| 885 | |
| 886 | static void job_commit(Job *job) |
| 887 | { |
| 888 | assert(!job->ret); |
Emanuele Giuseppe Esposito | c70b803 | 2022-03-03 10:16:16 -0500 | [diff] [blame] | 889 | GLOBAL_STATE_CODE(); |
Kevin Wolf | 4ad3518 | 2018-04-19 17:30:16 +0200 | [diff] [blame] | 890 | if (job->driver->commit) { |
| 891 | job->driver->commit(job); |
| 892 | } |
| 893 | } |
| 894 | |
| 895 | static void job_abort(Job *job) |
| 896 | { |
| 897 | assert(job->ret); |
Emanuele Giuseppe Esposito | c70b803 | 2022-03-03 10:16:16 -0500 | [diff] [blame] | 898 | GLOBAL_STATE_CODE(); |
Kevin Wolf | 4ad3518 | 2018-04-19 17:30:16 +0200 | [diff] [blame] | 899 | if (job->driver->abort) { |
| 900 | job->driver->abort(job); |
| 901 | } |
| 902 | } |
| 903 | |
| 904 | static void job_clean(Job *job) |
| 905 | { |
Emanuele Giuseppe Esposito | c70b803 | 2022-03-03 10:16:16 -0500 | [diff] [blame] | 906 | GLOBAL_STATE_CODE(); |
Kevin Wolf | 4ad3518 | 2018-04-19 17:30:16 +0200 | [diff] [blame] | 907 | if (job->driver->clean) { |
| 908 | job->driver->clean(job); |
| 909 | } |
| 910 | } |
| 911 | |
Emanuele Giuseppe Esposito | afe1e8a | 2022-09-26 05:31:58 -0400 | [diff] [blame] | 912 | /* Called with job_mutex held, but releases it temporarily */ |
| 913 | static int job_finalize_single_locked(Job *job) |
Kevin Wolf | 4ad3518 | 2018-04-19 17:30:16 +0200 | [diff] [blame] | 914 | { |
Emanuele Giuseppe Esposito | afe1e8a | 2022-09-26 05:31:58 -0400 | [diff] [blame] | 915 | int job_ret; |
| 916 | |
| 917 | assert(job_is_completed_locked(job)); |
Kevin Wolf | 4ad3518 | 2018-04-19 17:30:16 +0200 | [diff] [blame] | 918 | |
| 919 | /* Ensure abort is called for late-transactional failures */ |
Emanuele Giuseppe Esposito | afe1e8a | 2022-09-26 05:31:58 -0400 | [diff] [blame] | 920 | job_update_rc_locked(job); |
Kevin Wolf | 4ad3518 | 2018-04-19 17:30:16 +0200 | [diff] [blame] | 921 | |
Emanuele Giuseppe Esposito | afe1e8a | 2022-09-26 05:31:58 -0400 | [diff] [blame] | 922 | job_ret = job->ret; |
| 923 | job_unlock(); |
| 924 | |
| 925 | if (!job_ret) { |
Kevin Wolf | 4ad3518 | 2018-04-19 17:30:16 +0200 | [diff] [blame] | 926 | job_commit(job); |
| 927 | } else { |
| 928 | job_abort(job); |
| 929 | } |
| 930 | job_clean(job); |
| 931 | |
Emanuele Giuseppe Esposito | afe1e8a | 2022-09-26 05:31:58 -0400 | [diff] [blame] | 932 | job_lock(); |
| 933 | |
Kevin Wolf | 4ad3518 | 2018-04-19 17:30:16 +0200 | [diff] [blame] | 934 | if (job->cb) { |
Emanuele Giuseppe Esposito | afe1e8a | 2022-09-26 05:31:58 -0400 | [diff] [blame] | 935 | job_ret = job->ret; |
| 936 | job_unlock(); |
| 937 | job->cb(job->opaque, job_ret); |
| 938 | job_lock(); |
Kevin Wolf | 4ad3518 | 2018-04-19 17:30:16 +0200 | [diff] [blame] | 939 | } |
| 940 | |
| 941 | /* Emit events only if we actually started */ |
Emanuele Giuseppe Esposito | afe1e8a | 2022-09-26 05:31:58 -0400 | [diff] [blame] | 942 | if (job_started_locked(job)) { |
| 943 | if (job_is_cancelled_locked(job)) { |
| 944 | job_event_cancelled_locked(job); |
Kevin Wolf | 4ad3518 | 2018-04-19 17:30:16 +0200 | [diff] [blame] | 945 | } else { |
Emanuele Giuseppe Esposito | afe1e8a | 2022-09-26 05:31:58 -0400 | [diff] [blame] | 946 | job_event_completed_locked(job); |
Kevin Wolf | 4ad3518 | 2018-04-19 17:30:16 +0200 | [diff] [blame] | 947 | } |
| 948 | } |
| 949 | |
Emanuele Giuseppe Esposito | afe1e8a | 2022-09-26 05:31:58 -0400 | [diff] [blame] | 950 | job_txn_del_job_locked(job); |
| 951 | job_conclude_locked(job); |
Kevin Wolf | 4ad3518 | 2018-04-19 17:30:16 +0200 | [diff] [blame] | 952 | return 0; |
| 953 | } |
| 954 | |
Emanuele Giuseppe Esposito | afe1e8a | 2022-09-26 05:31:58 -0400 | [diff] [blame] | 955 | /* Called with job_mutex held, but releases it temporarily */ |
| 956 | static void job_cancel_async_locked(Job *job, bool force) |
Kevin Wolf | 7eaa8fb | 2018-04-23 16:06:26 +0200 | [diff] [blame] | 957 | { |
Emanuele Giuseppe Esposito | c70b803 | 2022-03-03 10:16:16 -0500 | [diff] [blame] | 958 | GLOBAL_STATE_CODE(); |
Vladimir Sementsov-Ogievskiy | 9820933 | 2021-02-05 19:37:14 +0300 | [diff] [blame] | 959 | if (job->driver->cancel) { |
Emanuele Giuseppe Esposito | afe1e8a | 2022-09-26 05:31:58 -0400 | [diff] [blame] | 960 | job_unlock(); |
Hanna Reitz | 73895f3 | 2021-10-06 17:19:33 +0200 | [diff] [blame] | 961 | force = job->driver->cancel(job, force); |
Emanuele Giuseppe Esposito | afe1e8a | 2022-09-26 05:31:58 -0400 | [diff] [blame] | 962 | job_lock(); |
Hanna Reitz | 73895f3 | 2021-10-06 17:19:33 +0200 | [diff] [blame] | 963 | } else { |
| 964 | /* No .cancel() means the job will behave as if force-cancelled */ |
| 965 | force = true; |
Vladimir Sementsov-Ogievskiy | 9820933 | 2021-02-05 19:37:14 +0300 | [diff] [blame] | 966 | } |
Hanna Reitz | 73895f3 | 2021-10-06 17:19:33 +0200 | [diff] [blame] | 967 | |
Kevin Wolf | 7eaa8fb | 2018-04-23 16:06:26 +0200 | [diff] [blame] | 968 | if (job->user_paused) { |
| 969 | /* Do not call job_enter here, the caller will handle it. */ |
Kevin Wolf | 7eaa8fb | 2018-04-23 16:06:26 +0200 | [diff] [blame] | 970 | if (job->driver->user_resume) { |
Emanuele Giuseppe Esposito | afe1e8a | 2022-09-26 05:31:58 -0400 | [diff] [blame] | 971 | job_unlock(); |
Kevin Wolf | 7eaa8fb | 2018-04-23 16:06:26 +0200 | [diff] [blame] | 972 | job->driver->user_resume(job); |
Emanuele Giuseppe Esposito | afe1e8a | 2022-09-26 05:31:58 -0400 | [diff] [blame] | 973 | job_lock(); |
Kevin Wolf | 7eaa8fb | 2018-04-23 16:06:26 +0200 | [diff] [blame] | 974 | } |
Jeff Cody | e321c05 | 2018-08-21 12:26:19 -0400 | [diff] [blame] | 975 | job->user_paused = false; |
Kevin Wolf | 7eaa8fb | 2018-04-23 16:06:26 +0200 | [diff] [blame] | 976 | assert(job->pause_count > 0); |
| 977 | job->pause_count--; |
| 978 | } |
Hanna Reitz | 401dd09 | 2021-10-06 17:19:34 +0200 | [diff] [blame] | 979 | |
| 980 | /* |
| 981 | * Ignore soft cancel requests after the job is already done |
| 982 | * (We will still invoke job->driver->cancel() above, but if the |
| 983 | * job driver supports soft cancelling and the job is done, that |
| 984 | * should be a no-op, too. We still call it so it can override |
| 985 | * @force.) |
| 986 | */ |
| 987 | if (force || !job->deferred_to_main_loop) { |
| 988 | job->cancelled = true; |
| 989 | /* To prevent 'force == false' overriding a previous 'force == true' */ |
| 990 | job->force_cancel |= force; |
| 991 | } |
Kevin Wolf | 7eaa8fb | 2018-04-23 16:06:26 +0200 | [diff] [blame] | 992 | } |
| 993 | |
Emanuele Giuseppe Esposito | afe1e8a | 2022-09-26 05:31:58 -0400 | [diff] [blame] | 994 | /* Called with job_mutex held, but releases it temporarily. */ |
| 995 | static void job_completed_txn_abort_locked(Job *job) |
Kevin Wolf | 7eaa8fb | 2018-04-23 16:06:26 +0200 | [diff] [blame] | 996 | { |
| 997 | AioContext *ctx; |
| 998 | JobTxn *txn = job->txn; |
| 999 | Job *other_job; |
| 1000 | |
| 1001 | if (txn->aborting) { |
| 1002 | /* |
| 1003 | * We are cancelled by another job, which will handle everything. |
| 1004 | */ |
| 1005 | return; |
| 1006 | } |
| 1007 | txn->aborting = true; |
Emanuele Giuseppe Esposito | afe1e8a | 2022-09-26 05:31:58 -0400 | [diff] [blame] | 1008 | job_txn_ref_locked(txn); |
Kevin Wolf | 7eaa8fb | 2018-04-23 16:06:26 +0200 | [diff] [blame] | 1009 | |
Hanna Reitz | d431131 | 2021-10-06 17:19:28 +0200 | [diff] [blame] | 1010 | /* |
| 1011 | * We can only hold the single job's AioContext lock while calling |
Kevin Wolf | 644f3a2 | 2018-09-13 14:35:27 +0200 | [diff] [blame] | 1012 | * job_finalize_single() because the finalization callbacks can involve |
Hanna Reitz | d431131 | 2021-10-06 17:19:28 +0200 | [diff] [blame] | 1013 | * calls of AIO_WAIT_WHILE(), which could deadlock otherwise. |
| 1014 | * Note that the job's AioContext may change when it is finalized. |
| 1015 | */ |
Emanuele Giuseppe Esposito | afe1e8a | 2022-09-26 05:31:58 -0400 | [diff] [blame] | 1016 | job_ref_locked(job); |
Hanna Reitz | d431131 | 2021-10-06 17:19:28 +0200 | [diff] [blame] | 1017 | aio_context_release(job->aio_context); |
Kevin Wolf | 7eaa8fb | 2018-04-23 16:06:26 +0200 | [diff] [blame] | 1018 | |
| 1019 | /* Other jobs are effectively cancelled by us, set the status for |
| 1020 | * them; this job, however, may or may not be cancelled, depending |
| 1021 | * on the caller, so leave it. */ |
| 1022 | QLIST_FOREACH(other_job, &txn->jobs, txn_list) { |
| 1023 | if (other_job != job) { |
Kevin Wolf | 644f3a2 | 2018-09-13 14:35:27 +0200 | [diff] [blame] | 1024 | ctx = other_job->aio_context; |
| 1025 | aio_context_acquire(ctx); |
Hanna Reitz | 1d4a43e | 2021-10-06 17:19:31 +0200 | [diff] [blame] | 1026 | /* |
| 1027 | * This is a transaction: If one job failed, no result will matter. |
| 1028 | * Therefore, pass force=true to terminate all other jobs as quickly |
| 1029 | * as possible. |
| 1030 | */ |
Emanuele Giuseppe Esposito | afe1e8a | 2022-09-26 05:31:58 -0400 | [diff] [blame] | 1031 | job_cancel_async_locked(other_job, true); |
Kevin Wolf | 644f3a2 | 2018-09-13 14:35:27 +0200 | [diff] [blame] | 1032 | aio_context_release(ctx); |
Kevin Wolf | 7eaa8fb | 2018-04-23 16:06:26 +0200 | [diff] [blame] | 1033 | } |
| 1034 | } |
| 1035 | while (!QLIST_EMPTY(&txn->jobs)) { |
| 1036 | other_job = QLIST_FIRST(&txn->jobs); |
Hanna Reitz | d431131 | 2021-10-06 17:19:28 +0200 | [diff] [blame] | 1037 | /* |
| 1038 | * The job's AioContext may change, so store it in @ctx so we |
| 1039 | * release the same context that we have acquired before. |
| 1040 | */ |
Kevin Wolf | 7eaa8fb | 2018-04-23 16:06:26 +0200 | [diff] [blame] | 1041 | ctx = other_job->aio_context; |
Kevin Wolf | 644f3a2 | 2018-09-13 14:35:27 +0200 | [diff] [blame] | 1042 | aio_context_acquire(ctx); |
Emanuele Giuseppe Esposito | afe1e8a | 2022-09-26 05:31:58 -0400 | [diff] [blame] | 1043 | if (!job_is_completed_locked(other_job)) { |
| 1044 | assert(job_cancel_requested_locked(other_job)); |
| 1045 | job_finish_sync_locked(other_job, NULL, NULL); |
Kevin Wolf | 7eaa8fb | 2018-04-23 16:06:26 +0200 | [diff] [blame] | 1046 | } |
Emanuele Giuseppe Esposito | afe1e8a | 2022-09-26 05:31:58 -0400 | [diff] [blame] | 1047 | job_finalize_single_locked(other_job); |
Kevin Wolf | 7eaa8fb | 2018-04-23 16:06:26 +0200 | [diff] [blame] | 1048 | aio_context_release(ctx); |
| 1049 | } |
| 1050 | |
Hanna Reitz | d431131 | 2021-10-06 17:19:28 +0200 | [diff] [blame] | 1051 | /* |
| 1052 | * Use job_ref()/job_unref() so we can read the AioContext here |
| 1053 | * even if the job went away during job_finalize_single(). |
| 1054 | */ |
| 1055 | aio_context_acquire(job->aio_context); |
Emanuele Giuseppe Esposito | afe1e8a | 2022-09-26 05:31:58 -0400 | [diff] [blame] | 1056 | job_unref_locked(job); |
Kevin Wolf | 644f3a2 | 2018-09-13 14:35:27 +0200 | [diff] [blame] | 1057 | |
Emanuele Giuseppe Esposito | afe1e8a | 2022-09-26 05:31:58 -0400 | [diff] [blame] | 1058 | job_txn_unref_locked(txn); |
Kevin Wolf | 7eaa8fb | 2018-04-23 16:06:26 +0200 | [diff] [blame] | 1059 | } |
| 1060 | |
Emanuele Giuseppe Esposito | afe1e8a | 2022-09-26 05:31:58 -0400 | [diff] [blame] | 1061 | /* Called with job_mutex held, but releases it temporarily */ |
| 1062 | static int job_prepare_locked(Job *job) |
Kevin Wolf | 7eaa8fb | 2018-04-23 16:06:26 +0200 | [diff] [blame] | 1063 | { |
Emanuele Giuseppe Esposito | afe1e8a | 2022-09-26 05:31:58 -0400 | [diff] [blame] | 1064 | int ret; |
| 1065 | |
Emanuele Giuseppe Esposito | c70b803 | 2022-03-03 10:16:16 -0500 | [diff] [blame] | 1066 | GLOBAL_STATE_CODE(); |
Kevin Wolf | 7eaa8fb | 2018-04-23 16:06:26 +0200 | [diff] [blame] | 1067 | if (job->ret == 0 && job->driver->prepare) { |
Emanuele Giuseppe Esposito | afe1e8a | 2022-09-26 05:31:58 -0400 | [diff] [blame] | 1068 | job_unlock(); |
| 1069 | ret = job->driver->prepare(job); |
| 1070 | job_lock(); |
| 1071 | job->ret = ret; |
| 1072 | job_update_rc_locked(job); |
Kevin Wolf | 7eaa8fb | 2018-04-23 16:06:26 +0200 | [diff] [blame] | 1073 | } |
| 1074 | return job->ret; |
| 1075 | } |
| 1076 | |
Emanuele Giuseppe Esposito | afe1e8a | 2022-09-26 05:31:58 -0400 | [diff] [blame] | 1077 | /* Called with job_mutex held */ |
| 1078 | static int job_needs_finalize_locked(Job *job) |
Kevin Wolf | 7eaa8fb | 2018-04-23 16:06:26 +0200 | [diff] [blame] | 1079 | { |
| 1080 | return !job->auto_finalize; |
| 1081 | } |
| 1082 | |
Emanuele Giuseppe Esposito | afe1e8a | 2022-09-26 05:31:58 -0400 | [diff] [blame] | 1083 | /* Called with job_mutex held */ |
| 1084 | static void job_do_finalize_locked(Job *job) |
Kevin Wolf | 7eaa8fb | 2018-04-23 16:06:26 +0200 | [diff] [blame] | 1085 | { |
| 1086 | int rc; |
| 1087 | assert(job && job->txn); |
| 1088 | |
| 1089 | /* prepare the transaction to complete */ |
Emanuele Giuseppe Esposito | afe1e8a | 2022-09-26 05:31:58 -0400 | [diff] [blame] | 1090 | rc = job_txn_apply_locked(job, job_prepare_locked); |
Kevin Wolf | 7eaa8fb | 2018-04-23 16:06:26 +0200 | [diff] [blame] | 1091 | if (rc) { |
Emanuele Giuseppe Esposito | afe1e8a | 2022-09-26 05:31:58 -0400 | [diff] [blame] | 1092 | job_completed_txn_abort_locked(job); |
Kevin Wolf | 7eaa8fb | 2018-04-23 16:06:26 +0200 | [diff] [blame] | 1093 | } else { |
Emanuele Giuseppe Esposito | afe1e8a | 2022-09-26 05:31:58 -0400 | [diff] [blame] | 1094 | job_txn_apply_locked(job, job_finalize_single_locked); |
Kevin Wolf | 7eaa8fb | 2018-04-23 16:06:26 +0200 | [diff] [blame] | 1095 | } |
| 1096 | } |
| 1097 | |
Emanuele Giuseppe Esposito | afe1e8a | 2022-09-26 05:31:58 -0400 | [diff] [blame] | 1098 | void job_finalize_locked(Job *job, Error **errp) |
| 1099 | { |
| 1100 | assert(job && job->id); |
| 1101 | if (job_apply_verb_locked(job, JOB_VERB_FINALIZE, errp)) { |
| 1102 | return; |
| 1103 | } |
| 1104 | job_do_finalize_locked(job); |
| 1105 | } |
| 1106 | |
Kevin Wolf | 7eaa8fb | 2018-04-23 16:06:26 +0200 | [diff] [blame] | 1107 | void job_finalize(Job *job, Error **errp) |
| 1108 | { |
Emanuele Giuseppe Esposito | afe1e8a | 2022-09-26 05:31:58 -0400 | [diff] [blame] | 1109 | JOB_LOCK_GUARD(); |
| 1110 | job_finalize_locked(job, errp); |
Kevin Wolf | 7eaa8fb | 2018-04-23 16:06:26 +0200 | [diff] [blame] | 1111 | } |
| 1112 | |
Emanuele Giuseppe Esposito | afe1e8a | 2022-09-26 05:31:58 -0400 | [diff] [blame] | 1113 | /* Called with job_mutex held. */ |
| 1114 | static int job_transition_to_pending_locked(Job *job) |
Kevin Wolf | 7eaa8fb | 2018-04-23 16:06:26 +0200 | [diff] [blame] | 1115 | { |
Emanuele Giuseppe Esposito | afe1e8a | 2022-09-26 05:31:58 -0400 | [diff] [blame] | 1116 | job_state_transition_locked(job, JOB_STATUS_PENDING); |
Kevin Wolf | 7eaa8fb | 2018-04-23 16:06:26 +0200 | [diff] [blame] | 1117 | if (!job->auto_finalize) { |
Emanuele Giuseppe Esposito | afe1e8a | 2022-09-26 05:31:58 -0400 | [diff] [blame] | 1118 | job_event_pending_locked(job); |
Kevin Wolf | 7eaa8fb | 2018-04-23 16:06:26 +0200 | [diff] [blame] | 1119 | } |
| 1120 | return 0; |
| 1121 | } |
| 1122 | |
Kevin Wolf | 2e1795b | 2018-04-25 14:56:09 +0200 | [diff] [blame] | 1123 | void job_transition_to_ready(Job *job) |
| 1124 | { |
Emanuele Giuseppe Esposito | afe1e8a | 2022-09-26 05:31:58 -0400 | [diff] [blame] | 1125 | JOB_LOCK_GUARD(); |
| 1126 | job_state_transition_locked(job, JOB_STATUS_READY); |
| 1127 | job_event_ready_locked(job); |
Kevin Wolf | 2e1795b | 2018-04-25 14:56:09 +0200 | [diff] [blame] | 1128 | } |
| 1129 | |
Emanuele Giuseppe Esposito | afe1e8a | 2022-09-26 05:31:58 -0400 | [diff] [blame] | 1130 | /* Called with job_mutex held. */ |
| 1131 | static void job_completed_txn_success_locked(Job *job) |
Kevin Wolf | 7eaa8fb | 2018-04-23 16:06:26 +0200 | [diff] [blame] | 1132 | { |
| 1133 | JobTxn *txn = job->txn; |
| 1134 | Job *other_job; |
| 1135 | |
Emanuele Giuseppe Esposito | afe1e8a | 2022-09-26 05:31:58 -0400 | [diff] [blame] | 1136 | job_state_transition_locked(job, JOB_STATUS_WAITING); |
Kevin Wolf | 7eaa8fb | 2018-04-23 16:06:26 +0200 | [diff] [blame] | 1137 | |
| 1138 | /* |
| 1139 | * Successful completion, see if there are other running jobs in this |
| 1140 | * txn. |
| 1141 | */ |
| 1142 | QLIST_FOREACH(other_job, &txn->jobs, txn_list) { |
Emanuele Giuseppe Esposito | afe1e8a | 2022-09-26 05:31:58 -0400 | [diff] [blame] | 1143 | if (!job_is_completed_locked(other_job)) { |
Kevin Wolf | 7eaa8fb | 2018-04-23 16:06:26 +0200 | [diff] [blame] | 1144 | return; |
| 1145 | } |
| 1146 | assert(other_job->ret == 0); |
| 1147 | } |
| 1148 | |
Emanuele Giuseppe Esposito | afe1e8a | 2022-09-26 05:31:58 -0400 | [diff] [blame] | 1149 | job_txn_apply_locked(job, job_transition_to_pending_locked); |
Kevin Wolf | 7eaa8fb | 2018-04-23 16:06:26 +0200 | [diff] [blame] | 1150 | |
| 1151 | /* If no jobs need manual finalization, automatically do so */ |
Emanuele Giuseppe Esposito | afe1e8a | 2022-09-26 05:31:58 -0400 | [diff] [blame] | 1152 | if (job_txn_apply_locked(job, job_needs_finalize_locked) == 0) { |
| 1153 | job_do_finalize_locked(job); |
Kevin Wolf | 7eaa8fb | 2018-04-23 16:06:26 +0200 | [diff] [blame] | 1154 | } |
| 1155 | } |
| 1156 | |
Emanuele Giuseppe Esposito | afe1e8a | 2022-09-26 05:31:58 -0400 | [diff] [blame] | 1157 | /* Called with job_mutex held. */ |
| 1158 | static void job_completed_locked(Job *job) |
Kevin Wolf | 3d70ff5 | 2018-04-24 16:13:52 +0200 | [diff] [blame] | 1159 | { |
Emanuele Giuseppe Esposito | afe1e8a | 2022-09-26 05:31:58 -0400 | [diff] [blame] | 1160 | assert(job && job->txn && !job_is_completed_locked(job)); |
Kevin Wolf | 1266c9b | 2018-05-24 15:26:10 +0200 | [diff] [blame] | 1161 | |
Emanuele Giuseppe Esposito | afe1e8a | 2022-09-26 05:31:58 -0400 | [diff] [blame] | 1162 | job_update_rc_locked(job); |
John Snow | 404ff28 | 2018-08-29 21:57:33 -0400 | [diff] [blame] | 1163 | trace_job_completed(job, job->ret); |
Kevin Wolf | 3d70ff5 | 2018-04-24 16:13:52 +0200 | [diff] [blame] | 1164 | if (job->ret) { |
Emanuele Giuseppe Esposito | afe1e8a | 2022-09-26 05:31:58 -0400 | [diff] [blame] | 1165 | job_completed_txn_abort_locked(job); |
Kevin Wolf | 3d70ff5 | 2018-04-24 16:13:52 +0200 | [diff] [blame] | 1166 | } else { |
Emanuele Giuseppe Esposito | afe1e8a | 2022-09-26 05:31:58 -0400 | [diff] [blame] | 1167 | job_completed_txn_success_locked(job); |
Kevin Wolf | 3d70ff5 | 2018-04-24 16:13:52 +0200 | [diff] [blame] | 1168 | } |
| 1169 | } |
| 1170 | |
Emanuele Giuseppe Esposito | afe1e8a | 2022-09-26 05:31:58 -0400 | [diff] [blame] | 1171 | /** |
| 1172 | * Useful only as a type shim for aio_bh_schedule_oneshot. |
| 1173 | * Called with job_mutex *not* held. |
| 1174 | */ |
John Snow | ccbfb33 | 2018-09-06 09:02:20 -0400 | [diff] [blame] | 1175 | static void job_exit(void *opaque) |
| 1176 | { |
| 1177 | Job *job = (Job *)opaque; |
Stefan Reiter | b660a84 | 2020-04-07 13:56:49 +0200 | [diff] [blame] | 1178 | AioContext *ctx; |
Emanuele Giuseppe Esposito | afe1e8a | 2022-09-26 05:31:58 -0400 | [diff] [blame] | 1179 | JOB_LOCK_GUARD(); |
Kevin Wolf | d1756c7 | 2018-09-12 13:50:38 +0200 | [diff] [blame] | 1180 | |
Emanuele Giuseppe Esposito | afe1e8a | 2022-09-26 05:31:58 -0400 | [diff] [blame] | 1181 | job_ref_locked(job); |
Stefan Reiter | b660a84 | 2020-04-07 13:56:49 +0200 | [diff] [blame] | 1182 | aio_context_acquire(job->aio_context); |
Kevin Wolf | b5a7a05 | 2018-09-07 15:31:22 +0200 | [diff] [blame] | 1183 | |
| 1184 | /* This is a lie, we're not quiescent, but still doing the completion |
| 1185 | * callbacks. However, completion callbacks tend to involve operations that |
| 1186 | * drain block nodes, and if .drained_poll still returned true, we would |
| 1187 | * deadlock. */ |
| 1188 | job->busy = false; |
Emanuele Giuseppe Esposito | afe1e8a | 2022-09-26 05:31:58 -0400 | [diff] [blame] | 1189 | job_event_idle_locked(job); |
Kevin Wolf | b5a7a05 | 2018-09-07 15:31:22 +0200 | [diff] [blame] | 1190 | |
Emanuele Giuseppe Esposito | afe1e8a | 2022-09-26 05:31:58 -0400 | [diff] [blame] | 1191 | job_completed_locked(job); |
Kevin Wolf | b5a7a05 | 2018-09-07 15:31:22 +0200 | [diff] [blame] | 1192 | |
Stefan Reiter | b660a84 | 2020-04-07 13:56:49 +0200 | [diff] [blame] | 1193 | /* |
| 1194 | * Note that calling job_completed can move the job to a different |
| 1195 | * aio_context, so we cannot cache from above. job_txn_apply takes care of |
| 1196 | * acquiring the new lock, and we ref/unref to avoid job_completed freeing |
| 1197 | * the job underneath us. |
| 1198 | */ |
| 1199 | ctx = job->aio_context; |
Emanuele Giuseppe Esposito | afe1e8a | 2022-09-26 05:31:58 -0400 | [diff] [blame] | 1200 | job_unref_locked(job); |
Kevin Wolf | d1756c7 | 2018-09-12 13:50:38 +0200 | [diff] [blame] | 1201 | aio_context_release(ctx); |
John Snow | ccbfb33 | 2018-09-06 09:02:20 -0400 | [diff] [blame] | 1202 | } |
| 1203 | |
| 1204 | /** |
| 1205 | * All jobs must allow a pause point before entering their job proper. This |
| 1206 | * ensures that jobs can be paused prior to being started, then resumed later. |
| 1207 | */ |
| 1208 | static void coroutine_fn job_co_entry(void *opaque) |
| 1209 | { |
| 1210 | Job *job = opaque; |
Emanuele Giuseppe Esposito | afe1e8a | 2022-09-26 05:31:58 -0400 | [diff] [blame] | 1211 | int ret; |
John Snow | ccbfb33 | 2018-09-06 09:02:20 -0400 | [diff] [blame] | 1212 | |
| 1213 | assert(job && job->driver && job->driver->run); |
Emanuele Giuseppe Esposito | afe1e8a | 2022-09-26 05:31:58 -0400 | [diff] [blame] | 1214 | WITH_JOB_LOCK_GUARD() { |
| 1215 | assert(job->aio_context == qemu_get_current_aio_context()); |
| 1216 | job_pause_point_locked(job); |
| 1217 | } |
| 1218 | ret = job->driver->run(job, &job->err); |
| 1219 | WITH_JOB_LOCK_GUARD() { |
| 1220 | job->ret = ret; |
| 1221 | job->deferred_to_main_loop = true; |
| 1222 | job->busy = true; |
| 1223 | } |
John Snow | ccbfb33 | 2018-09-06 09:02:20 -0400 | [diff] [blame] | 1224 | aio_bh_schedule_oneshot(qemu_get_aio_context(), job_exit, job); |
| 1225 | } |
| 1226 | |
| 1227 | void job_start(Job *job) |
| 1228 | { |
Emanuele Giuseppe Esposito | afe1e8a | 2022-09-26 05:31:58 -0400 | [diff] [blame] | 1229 | assert(qemu_in_main_thread()); |
| 1230 | |
| 1231 | WITH_JOB_LOCK_GUARD() { |
| 1232 | assert(job && !job_started_locked(job) && job->paused && |
| 1233 | job->driver && job->driver->run); |
| 1234 | job->co = qemu_coroutine_create(job_co_entry, job); |
| 1235 | job->pause_count--; |
| 1236 | job->busy = true; |
| 1237 | job->paused = false; |
| 1238 | job_state_transition_locked(job, JOB_STATUS_RUNNING); |
| 1239 | } |
John Snow | ccbfb33 | 2018-09-06 09:02:20 -0400 | [diff] [blame] | 1240 | aio_co_enter(job->aio_context, job->co); |
| 1241 | } |
| 1242 | |
Emanuele Giuseppe Esposito | afe1e8a | 2022-09-26 05:31:58 -0400 | [diff] [blame] | 1243 | void job_cancel_locked(Job *job, bool force) |
Kevin Wolf | 3d70ff5 | 2018-04-24 16:13:52 +0200 | [diff] [blame] | 1244 | { |
| 1245 | if (job->status == JOB_STATUS_CONCLUDED) { |
Emanuele Giuseppe Esposito | afe1e8a | 2022-09-26 05:31:58 -0400 | [diff] [blame] | 1246 | job_do_dismiss_locked(job); |
Kevin Wolf | 3d70ff5 | 2018-04-24 16:13:52 +0200 | [diff] [blame] | 1247 | return; |
| 1248 | } |
Emanuele Giuseppe Esposito | afe1e8a | 2022-09-26 05:31:58 -0400 | [diff] [blame] | 1249 | job_cancel_async_locked(job, force); |
| 1250 | if (!job_started_locked(job)) { |
| 1251 | job_completed_locked(job); |
Kevin Wolf | 3d70ff5 | 2018-04-24 16:13:52 +0200 | [diff] [blame] | 1252 | } else if (job->deferred_to_main_loop) { |
Hanna Reitz | 401dd09 | 2021-10-06 17:19:34 +0200 | [diff] [blame] | 1253 | /* |
| 1254 | * job_cancel_async() ignores soft-cancel requests for jobs |
| 1255 | * that are already done (i.e. deferred to the main loop). We |
| 1256 | * have to check again whether the job is really cancelled. |
Hanna Reitz | 08b83bf | 2021-10-06 17:19:35 +0200 | [diff] [blame] | 1257 | * (job_cancel_requested() and job_is_cancelled() are equivalent |
| 1258 | * here, because job_cancel_async() will make soft-cancel |
| 1259 | * requests no-ops when deferred_to_main_loop is true. We |
| 1260 | * choose to call job_is_cancelled() to show that we invoke |
| 1261 | * job_completed_txn_abort() only for force-cancelled jobs.) |
Hanna Reitz | 401dd09 | 2021-10-06 17:19:34 +0200 | [diff] [blame] | 1262 | */ |
Emanuele Giuseppe Esposito | afe1e8a | 2022-09-26 05:31:58 -0400 | [diff] [blame] | 1263 | if (job_is_cancelled_locked(job)) { |
| 1264 | job_completed_txn_abort_locked(job); |
Hanna Reitz | 401dd09 | 2021-10-06 17:19:34 +0200 | [diff] [blame] | 1265 | } |
Kevin Wolf | 3d70ff5 | 2018-04-24 16:13:52 +0200 | [diff] [blame] | 1266 | } else { |
Emanuele Giuseppe Esposito | afe1e8a | 2022-09-26 05:31:58 -0400 | [diff] [blame] | 1267 | job_enter_cond_locked(job, NULL); |
Kevin Wolf | 3d70ff5 | 2018-04-24 16:13:52 +0200 | [diff] [blame] | 1268 | } |
| 1269 | } |
| 1270 | |
Emanuele Giuseppe Esposito | afe1e8a | 2022-09-26 05:31:58 -0400 | [diff] [blame] | 1271 | void job_cancel(Job *job, bool force) |
| 1272 | { |
| 1273 | JOB_LOCK_GUARD(); |
| 1274 | job_cancel_locked(job, force); |
| 1275 | } |
| 1276 | |
| 1277 | void job_user_cancel_locked(Job *job, bool force, Error **errp) |
| 1278 | { |
| 1279 | if (job_apply_verb_locked(job, JOB_VERB_CANCEL, errp)) { |
| 1280 | return; |
| 1281 | } |
| 1282 | job_cancel_locked(job, force); |
| 1283 | } |
| 1284 | |
Kevin Wolf | 3d70ff5 | 2018-04-24 16:13:52 +0200 | [diff] [blame] | 1285 | void job_user_cancel(Job *job, bool force, Error **errp) |
| 1286 | { |
Emanuele Giuseppe Esposito | afe1e8a | 2022-09-26 05:31:58 -0400 | [diff] [blame] | 1287 | JOB_LOCK_GUARD(); |
| 1288 | job_user_cancel_locked(job, force, errp); |
Kevin Wolf | 3d70ff5 | 2018-04-24 16:13:52 +0200 | [diff] [blame] | 1289 | } |
| 1290 | |
| 1291 | /* A wrapper around job_cancel() taking an Error ** parameter so it may be |
| 1292 | * used with job_finish_sync() without the need for (rather nasty) function |
Emanuele Giuseppe Esposito | afe1e8a | 2022-09-26 05:31:58 -0400 | [diff] [blame] | 1293 | * pointer casts there. |
| 1294 | * |
| 1295 | * Called with job_mutex held. |
| 1296 | */ |
| 1297 | static void job_cancel_err_locked(Job *job, Error **errp) |
Kevin Wolf | 3d70ff5 | 2018-04-24 16:13:52 +0200 | [diff] [blame] | 1298 | { |
Emanuele Giuseppe Esposito | afe1e8a | 2022-09-26 05:31:58 -0400 | [diff] [blame] | 1299 | job_cancel_locked(job, false); |
Kevin Wolf | 3d70ff5 | 2018-04-24 16:13:52 +0200 | [diff] [blame] | 1300 | } |
| 1301 | |
Hanna Reitz | 4cfb3f0 | 2021-10-06 17:19:32 +0200 | [diff] [blame] | 1302 | /** |
| 1303 | * Same as job_cancel_err(), but force-cancel. |
Emanuele Giuseppe Esposito | afe1e8a | 2022-09-26 05:31:58 -0400 | [diff] [blame] | 1304 | * Called with job_mutex held. |
Hanna Reitz | 4cfb3f0 | 2021-10-06 17:19:32 +0200 | [diff] [blame] | 1305 | */ |
Emanuele Giuseppe Esposito | afe1e8a | 2022-09-26 05:31:58 -0400 | [diff] [blame] | 1306 | static void job_force_cancel_err_locked(Job *job, Error **errp) |
Kevin Wolf | 3d70ff5 | 2018-04-24 16:13:52 +0200 | [diff] [blame] | 1307 | { |
Emanuele Giuseppe Esposito | afe1e8a | 2022-09-26 05:31:58 -0400 | [diff] [blame] | 1308 | job_cancel_locked(job, true); |
| 1309 | } |
| 1310 | |
| 1311 | int job_cancel_sync_locked(Job *job, bool force) |
| 1312 | { |
| 1313 | if (force) { |
| 1314 | return job_finish_sync_locked(job, &job_force_cancel_err_locked, NULL); |
| 1315 | } else { |
| 1316 | return job_finish_sync_locked(job, &job_cancel_err_locked, NULL); |
| 1317 | } |
Hanna Reitz | 4cfb3f0 | 2021-10-06 17:19:32 +0200 | [diff] [blame] | 1318 | } |
| 1319 | |
| 1320 | int job_cancel_sync(Job *job, bool force) |
| 1321 | { |
Emanuele Giuseppe Esposito | afe1e8a | 2022-09-26 05:31:58 -0400 | [diff] [blame] | 1322 | JOB_LOCK_GUARD(); |
| 1323 | return job_cancel_sync_locked(job, force); |
Kevin Wolf | 3d70ff5 | 2018-04-24 16:13:52 +0200 | [diff] [blame] | 1324 | } |
| 1325 | |
| 1326 | void job_cancel_sync_all(void) |
| 1327 | { |
| 1328 | Job *job; |
| 1329 | AioContext *aio_context; |
Emanuele Giuseppe Esposito | afe1e8a | 2022-09-26 05:31:58 -0400 | [diff] [blame] | 1330 | JOB_LOCK_GUARD(); |
Kevin Wolf | 3d70ff5 | 2018-04-24 16:13:52 +0200 | [diff] [blame] | 1331 | |
Emanuele Giuseppe Esposito | afe1e8a | 2022-09-26 05:31:58 -0400 | [diff] [blame] | 1332 | while ((job = job_next_locked(NULL))) { |
Kevin Wolf | 3d70ff5 | 2018-04-24 16:13:52 +0200 | [diff] [blame] | 1333 | aio_context = job->aio_context; |
| 1334 | aio_context_acquire(aio_context); |
Emanuele Giuseppe Esposito | afe1e8a | 2022-09-26 05:31:58 -0400 | [diff] [blame] | 1335 | job_cancel_sync_locked(job, true); |
Kevin Wolf | 3d70ff5 | 2018-04-24 16:13:52 +0200 | [diff] [blame] | 1336 | aio_context_release(aio_context); |
| 1337 | } |
| 1338 | } |
| 1339 | |
Emanuele Giuseppe Esposito | afe1e8a | 2022-09-26 05:31:58 -0400 | [diff] [blame] | 1340 | int job_complete_sync_locked(Job *job, Error **errp) |
Kevin Wolf | 3d70ff5 | 2018-04-24 16:13:52 +0200 | [diff] [blame] | 1341 | { |
Emanuele Giuseppe Esposito | afe1e8a | 2022-09-26 05:31:58 -0400 | [diff] [blame] | 1342 | return job_finish_sync_locked(job, job_complete_locked, errp); |
Kevin Wolf | 3d70ff5 | 2018-04-24 16:13:52 +0200 | [diff] [blame] | 1343 | } |
| 1344 | |
Emanuele Giuseppe Esposito | afe1e8a | 2022-09-26 05:31:58 -0400 | [diff] [blame] | 1345 | int job_complete_sync(Job *job, Error **errp) |
| 1346 | { |
| 1347 | JOB_LOCK_GUARD(); |
| 1348 | return job_complete_sync_locked(job, errp); |
| 1349 | } |
| 1350 | |
| 1351 | void job_complete_locked(Job *job, Error **errp) |
Kevin Wolf | 3453d97 | 2018-04-23 12:24:16 +0200 | [diff] [blame] | 1352 | { |
| 1353 | /* Should not be reachable via external interface for internal jobs */ |
| 1354 | assert(job->id); |
Emanuele Giuseppe Esposito | c70b803 | 2022-03-03 10:16:16 -0500 | [diff] [blame] | 1355 | GLOBAL_STATE_CODE(); |
Emanuele Giuseppe Esposito | afe1e8a | 2022-09-26 05:31:58 -0400 | [diff] [blame] | 1356 | if (job_apply_verb_locked(job, JOB_VERB_COMPLETE, errp)) { |
Kevin Wolf | 3453d97 | 2018-04-23 12:24:16 +0200 | [diff] [blame] | 1357 | return; |
| 1358 | } |
Emanuele Giuseppe Esposito | afe1e8a | 2022-09-26 05:31:58 -0400 | [diff] [blame] | 1359 | if (job_cancel_requested_locked(job) || !job->driver->complete) { |
Kevin Wolf | 3453d97 | 2018-04-23 12:24:16 +0200 | [diff] [blame] | 1360 | error_setg(errp, "The active block job '%s' cannot be completed", |
| 1361 | job->id); |
| 1362 | return; |
| 1363 | } |
| 1364 | |
Emanuele Giuseppe Esposito | afe1e8a | 2022-09-26 05:31:58 -0400 | [diff] [blame] | 1365 | job_unlock(); |
Kevin Wolf | 3453d97 | 2018-04-23 12:24:16 +0200 | [diff] [blame] | 1366 | job->driver->complete(job, errp); |
Emanuele Giuseppe Esposito | afe1e8a | 2022-09-26 05:31:58 -0400 | [diff] [blame] | 1367 | job_lock(); |
Kevin Wolf | 3453d97 | 2018-04-23 12:24:16 +0200 | [diff] [blame] | 1368 | } |
| 1369 | |
Emanuele Giuseppe Esposito | afe1e8a | 2022-09-26 05:31:58 -0400 | [diff] [blame] | 1370 | void job_complete(Job *job, Error **errp) |
| 1371 | { |
| 1372 | JOB_LOCK_GUARD(); |
| 1373 | job_complete_locked(job, errp); |
| 1374 | } |
| 1375 | |
| 1376 | int job_finish_sync_locked(Job *job, |
| 1377 | void (*finish)(Job *, Error **errp), |
| 1378 | Error **errp) |
Kevin Wolf | 6a74c07 | 2018-04-20 15:33:57 +0200 | [diff] [blame] | 1379 | { |
| 1380 | Error *local_err = NULL; |
| 1381 | int ret; |
| 1382 | |
Emanuele Giuseppe Esposito | afe1e8a | 2022-09-26 05:31:58 -0400 | [diff] [blame] | 1383 | job_ref_locked(job); |
Kevin Wolf | 6a74c07 | 2018-04-20 15:33:57 +0200 | [diff] [blame] | 1384 | |
| 1385 | if (finish) { |
| 1386 | finish(job, &local_err); |
| 1387 | } |
| 1388 | if (local_err) { |
| 1389 | error_propagate(errp, local_err); |
Emanuele Giuseppe Esposito | afe1e8a | 2022-09-26 05:31:58 -0400 | [diff] [blame] | 1390 | job_unref_locked(job); |
Kevin Wolf | 6a74c07 | 2018-04-20 15:33:57 +0200 | [diff] [blame] | 1391 | return -EBUSY; |
| 1392 | } |
Kevin Wolf | de0fbe6 | 2018-08-17 14:58:49 +0200 | [diff] [blame] | 1393 | |
Emanuele Giuseppe Esposito | afe1e8a | 2022-09-26 05:31:58 -0400 | [diff] [blame] | 1394 | job_unlock(); |
Kevin Wolf | cfe29d8 | 2018-09-18 17:09:16 +0200 | [diff] [blame] | 1395 | AIO_WAIT_WHILE(job->aio_context, |
Vladimir Sementsov-Ogievskiy | bb0c940 | 2019-08-29 12:09:53 +0300 | [diff] [blame] | 1396 | (job_enter(job), !job_is_completed(job))); |
Emanuele Giuseppe Esposito | afe1e8a | 2022-09-26 05:31:58 -0400 | [diff] [blame] | 1397 | job_lock(); |
Kevin Wolf | de0fbe6 | 2018-08-17 14:58:49 +0200 | [diff] [blame] | 1398 | |
Emanuele Giuseppe Esposito | afe1e8a | 2022-09-26 05:31:58 -0400 | [diff] [blame] | 1399 | ret = (job_is_cancelled_locked(job) && job->ret == 0) |
| 1400 | ? -ECANCELED : job->ret; |
| 1401 | job_unref_locked(job); |
Kevin Wolf | 6a74c07 | 2018-04-20 15:33:57 +0200 | [diff] [blame] | 1402 | return ret; |
| 1403 | } |
Emanuele Giuseppe Esposito | afe1e8a | 2022-09-26 05:31:58 -0400 | [diff] [blame] | 1404 | |
| 1405 | int job_finish_sync(Job *job, void (*finish)(Job *, Error **errp), Error **errp) |
| 1406 | { |
| 1407 | JOB_LOCK_GUARD(); |
| 1408 | return job_finish_sync_locked(job, finish, errp); |
| 1409 | } |