Line | Count | Source |
1 | | /* |
2 | | * QEMU System Emulator block driver |
3 | | * |
4 | | * Copyright (c) 2011 IBM Corp. |
5 | | * Copyright (c) 2012 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" |
27 | | #include "qemu/aio-wait.h" |
28 | | #include "block/block.h" |
29 | | #include "block/blockjob_int.h" |
30 | | #include "block/block_int.h" |
31 | | #include "block/trace.h" |
32 | | #include "system/block-backend.h" |
33 | | #include "qapi/error.h" |
34 | | #include "qapi/qapi-events-block-core.h" |
35 | | #include "qapi/qmp/qerror.h" |
36 | | #include "qemu/main-loop.h" |
37 | | #include "qemu/timer.h" |
38 | | |
39 | | static bool is_block_job(Job *job) |
40 | 0 | { |
41 | 0 | return job_type(job) == JOB_TYPE_BACKUP || |
42 | 0 | job_type(job) == JOB_TYPE_COMMIT || |
43 | 0 | job_type(job) == JOB_TYPE_MIRROR || |
44 | 0 | job_type(job) == JOB_TYPE_STREAM; |
45 | 0 | } |
46 | | |
47 | | BlockJob *block_job_next_locked(BlockJob *bjob) |
48 | 61 | { |
49 | 61 | Job *job = bjob ? &bjob->job : NULL; |
50 | 61 | GLOBAL_STATE_CODE(); |
51 | | |
52 | 61 | do { |
53 | 61 | job = job_next_locked(job); |
54 | 61 | } while (job && !is_block_job(job)); |
55 | | |
56 | 61 | return job ? container_of(job, BlockJob, job) : NULL; |
57 | 61 | } |
58 | | |
59 | | BlockJob *block_job_get_locked(const char *id) |
60 | 0 | { |
61 | 0 | Job *job = job_get_locked(id); |
62 | 0 | GLOBAL_STATE_CODE(); |
63 | |
|
64 | 0 | if (job && is_block_job(job)) { |
65 | 0 | return container_of(job, BlockJob, job); |
66 | 0 | } else { |
67 | 0 | return NULL; |
68 | 0 | } |
69 | 0 | } |
70 | | |
71 | | BlockJob *block_job_get(const char *id) |
72 | 0 | { |
73 | 0 | JOB_LOCK_GUARD(); |
74 | 0 | return block_job_get_locked(id); |
75 | 0 | } |
76 | | |
77 | | void block_job_free(Job *job) |
78 | 0 | { |
79 | 0 | BlockJob *bjob = container_of(job, BlockJob, job); |
80 | 0 | GLOBAL_STATE_CODE(); |
81 | |
|
82 | 0 | block_job_remove_all_bdrv(bjob); |
83 | 0 | ratelimit_destroy(&bjob->limit); |
84 | 0 | error_free(bjob->blocker); |
85 | 0 | } |
86 | | |
87 | | static char *child_job_get_parent_desc(BdrvChild *c) |
88 | 0 | { |
89 | 0 | BlockJob *job = c->opaque; |
90 | 0 | return g_strdup_printf("%s job '%s'", job_type_str(&job->job), job->job.id); |
91 | 0 | } |
92 | | |
93 | | static void child_job_drained_begin(BdrvChild *c) |
94 | 0 | { |
95 | 0 | BlockJob *job = c->opaque; |
96 | 0 | job_pause(&job->job); |
97 | 0 | } |
98 | | |
99 | | static bool child_job_drained_poll(BdrvChild *c) |
100 | 0 | { |
101 | 0 | BlockJob *bjob = c->opaque; |
102 | 0 | Job *job = &bjob->job; |
103 | 0 | const BlockJobDriver *drv = block_job_driver(bjob); |
104 | | |
105 | | /* An inactive or completed job doesn't have any pending requests. Jobs |
106 | | * with !job->busy are either already paused or have a pause point after |
107 | | * being reentered, so no job driver code will run before they pause. */ |
108 | 0 | WITH_JOB_LOCK_GUARD() { |
109 | 0 | if (!job->busy || job_is_completed_locked(job)) { |
110 | 0 | return false; |
111 | 0 | } |
112 | 0 | } |
113 | | |
114 | | /* Otherwise, assume that it isn't fully stopped yet, but allow the job to |
115 | | * override this assumption. */ |
116 | 0 | if (drv->drained_poll) { |
117 | 0 | return drv->drained_poll(bjob); |
118 | 0 | } else { |
119 | 0 | return true; |
120 | 0 | } |
121 | 0 | } |
122 | | |
123 | | static void child_job_drained_end(BdrvChild *c) |
124 | 0 | { |
125 | 0 | BlockJob *job = c->opaque; |
126 | 0 | job_resume(&job->job); |
127 | 0 | } |
128 | | |
129 | | typedef struct BdrvStateChildJobContext { |
130 | | AioContext *new_ctx; |
131 | | BlockJob *job; |
132 | | } BdrvStateChildJobContext; |
133 | | |
134 | | static void child_job_set_aio_ctx_commit(void *opaque) |
135 | 0 | { |
136 | 0 | BdrvStateChildJobContext *s = opaque; |
137 | 0 | BlockJob *job = s->job; |
138 | |
|
139 | 0 | job_set_aio_context(&job->job, s->new_ctx); |
140 | 0 | } |
141 | | |
142 | | static TransactionActionDrv change_child_job_context = { |
143 | | .commit = child_job_set_aio_ctx_commit, |
144 | | .clean = g_free, |
145 | | }; |
146 | | |
147 | | static bool GRAPH_RDLOCK |
148 | | child_job_change_aio_ctx(BdrvChild *c, AioContext *ctx, GHashTable *visited, |
149 | | Transaction *tran, Error **errp) |
150 | 0 | { |
151 | 0 | BlockJob *job = c->opaque; |
152 | 0 | BdrvStateChildJobContext *s; |
153 | 0 | GSList *l; |
154 | |
|
155 | 0 | for (l = job->nodes; l; l = l->next) { |
156 | 0 | BdrvChild *sibling = l->data; |
157 | 0 | if (!bdrv_child_change_aio_context(sibling, ctx, visited, |
158 | 0 | tran, errp)) { |
159 | 0 | return false; |
160 | 0 | } |
161 | 0 | } |
162 | | |
163 | 0 | s = g_new(BdrvStateChildJobContext, 1); |
164 | 0 | *s = (BdrvStateChildJobContext) { |
165 | 0 | .new_ctx = ctx, |
166 | 0 | .job = job, |
167 | 0 | }; |
168 | |
|
169 | 0 | tran_add(tran, &change_child_job_context, s); |
170 | 0 | return true; |
171 | 0 | } |
172 | | |
173 | | static AioContext *child_job_get_parent_aio_context(BdrvChild *c) |
174 | 0 | { |
175 | 0 | BlockJob *job = c->opaque; |
176 | 0 | IO_CODE(); |
177 | 0 | JOB_LOCK_GUARD(); |
178 | |
|
179 | 0 | return job->job.aio_context; |
180 | 0 | } |
181 | | |
182 | | static const BdrvChildClass child_job = { |
183 | | .get_parent_desc = child_job_get_parent_desc, |
184 | | .drained_begin = child_job_drained_begin, |
185 | | .drained_poll = child_job_drained_poll, |
186 | | .drained_end = child_job_drained_end, |
187 | | .change_aio_ctx = child_job_change_aio_ctx, |
188 | | .stay_at_node = true, |
189 | | .get_parent_aio_context = child_job_get_parent_aio_context, |
190 | | }; |
191 | | |
192 | | void block_job_remove_all_bdrv(BlockJob *job) |
193 | 0 | { |
194 | 0 | GLOBAL_STATE_CODE(); |
195 | | /* |
196 | | * bdrv_root_unref_child() may reach child_job_[can_]set_aio_ctx(), |
197 | | * which will also traverse job->nodes, so consume the list one by |
198 | | * one to make sure that such a concurrent access does not attempt |
199 | | * to process an already freed BdrvChild. |
200 | | */ |
201 | 0 | bdrv_graph_wrlock_drained(); |
202 | 0 | while (job->nodes) { |
203 | 0 | GSList *l = job->nodes; |
204 | 0 | BdrvChild *c = l->data; |
205 | |
|
206 | 0 | job->nodes = l->next; |
207 | |
|
208 | 0 | bdrv_op_unblock_all(c->bs, job->blocker); |
209 | 0 | bdrv_root_unref_child(c); |
210 | |
|
211 | 0 | g_slist_free_1(l); |
212 | 0 | } |
213 | 0 | bdrv_graph_wrunlock(); |
214 | 0 | } |
215 | | |
216 | | bool block_job_has_bdrv(BlockJob *job, BlockDriverState *bs) |
217 | 0 | { |
218 | 0 | GSList *el; |
219 | 0 | GLOBAL_STATE_CODE(); |
220 | |
|
221 | 0 | for (el = job->nodes; el; el = el->next) { |
222 | 0 | BdrvChild *c = el->data; |
223 | 0 | if (c->bs == bs) { |
224 | 0 | return true; |
225 | 0 | } |
226 | 0 | } |
227 | | |
228 | 0 | return false; |
229 | 0 | } |
230 | | |
231 | | int block_job_add_bdrv(BlockJob *job, const char *name, BlockDriverState *bs, |
232 | | uint64_t perm, uint64_t shared_perm, Error **errp) |
233 | 0 | { |
234 | 0 | BdrvChild *c; |
235 | 0 | GLOBAL_STATE_CODE(); |
236 | |
|
237 | 0 | bdrv_ref(bs); |
238 | |
|
239 | 0 | c = bdrv_root_attach_child(bs, name, &child_job, 0, perm, shared_perm, job, |
240 | 0 | errp); |
241 | 0 | if (c == NULL) { |
242 | 0 | return -EPERM; |
243 | 0 | } |
244 | | |
245 | 0 | job->nodes = g_slist_prepend(job->nodes, c); |
246 | 0 | bdrv_op_block_all(bs, job->blocker); |
247 | |
|
248 | 0 | return 0; |
249 | 0 | } |
250 | | |
251 | | /* Called with job_mutex lock held. */ |
252 | | static void block_job_on_idle_locked(Notifier *n, void *opaque) |
253 | 0 | { |
254 | 0 | aio_wait_kick(); |
255 | 0 | } |
256 | | |
257 | | bool block_job_is_internal(BlockJob *job) |
258 | 0 | { |
259 | 0 | return (job->job.id == NULL); |
260 | 0 | } |
261 | | |
262 | | const BlockJobDriver *block_job_driver(BlockJob *job) |
263 | 0 | { |
264 | 0 | return container_of(job->job.driver, BlockJobDriver, job_driver); |
265 | 0 | } |
266 | | |
267 | | /* Assumes the job_mutex is held */ |
268 | | static bool job_timer_pending(Job *job) |
269 | 0 | { |
270 | 0 | return timer_pending(&job->sleep_timer); |
271 | 0 | } |
272 | | |
273 | | bool block_job_set_speed_locked(BlockJob *job, int64_t speed, Error **errp) |
274 | 0 | { |
275 | 0 | const BlockJobDriver *drv = block_job_driver(job); |
276 | 0 | int64_t old_speed = job->speed; |
277 | |
|
278 | 0 | GLOBAL_STATE_CODE(); |
279 | |
|
280 | 0 | if (job_apply_verb_locked(&job->job, JOB_VERB_SET_SPEED, errp) < 0) { |
281 | 0 | return false; |
282 | 0 | } |
283 | 0 | if (speed < 0) { |
284 | 0 | error_setg(errp, QERR_INVALID_PARAMETER_VALUE, "speed", |
285 | 0 | "a non-negative value"); |
286 | 0 | return false; |
287 | 0 | } |
288 | | |
289 | 0 | ratelimit_set_speed(&job->limit, speed, BLOCK_JOB_SLICE_TIME); |
290 | |
|
291 | 0 | job->speed = speed; |
292 | |
|
293 | 0 | if (drv->set_speed) { |
294 | 0 | job_unlock(); |
295 | 0 | drv->set_speed(job, speed); |
296 | 0 | job_lock(); |
297 | 0 | } |
298 | |
|
299 | 0 | if (speed && speed <= old_speed) { |
300 | 0 | return true; |
301 | 0 | } |
302 | | |
303 | | /* kick only if a timer is pending */ |
304 | 0 | job_enter_cond_locked(&job->job, job_timer_pending); |
305 | |
|
306 | 0 | return true; |
307 | 0 | } |
308 | | |
309 | | static bool block_job_set_speed(BlockJob *job, int64_t speed, Error **errp) |
310 | 0 | { |
311 | 0 | JOB_LOCK_GUARD(); |
312 | 0 | return block_job_set_speed_locked(job, speed, errp); |
313 | 0 | } |
314 | | |
315 | | void block_job_change_locked(BlockJob *job, BlockJobChangeOptions *opts, |
316 | | Error **errp) |
317 | 0 | { |
318 | 0 | const BlockJobDriver *drv = block_job_driver(job); |
319 | |
|
320 | 0 | GLOBAL_STATE_CODE(); |
321 | |
|
322 | 0 | if (job_apply_verb_locked(&job->job, JOB_VERB_CHANGE, errp)) { |
323 | 0 | return; |
324 | 0 | } |
325 | | |
326 | 0 | if (drv->change) { |
327 | 0 | job_unlock(); |
328 | 0 | drv->change(job, opts, errp); |
329 | 0 | job_lock(); |
330 | 0 | } else { |
331 | 0 | error_setg(errp, "Job type does not support change"); |
332 | 0 | } |
333 | 0 | } |
334 | | |
335 | | void block_job_ratelimit_processed_bytes(BlockJob *job, uint64_t n) |
336 | 0 | { |
337 | 0 | IO_CODE(); |
338 | 0 | ratelimit_calculate_delay(&job->limit, n); |
339 | 0 | } |
340 | | |
341 | | void block_job_ratelimit_sleep(BlockJob *job) |
342 | 0 | { |
343 | 0 | uint64_t delay_ns; |
344 | | |
345 | | /* |
346 | | * Sleep at least once. If the job is reentered early, keep waiting until |
347 | | * we've waited for the full time that is necessary to keep the job at the |
348 | | * right speed. |
349 | | * |
350 | | * Make sure to recalculate the delay after each (possibly interrupted) |
351 | | * sleep because the speed can change while the job has yielded. |
352 | | */ |
353 | 0 | do { |
354 | 0 | delay_ns = ratelimit_calculate_delay(&job->limit, 0); |
355 | 0 | job_sleep_ns(&job->job, delay_ns); |
356 | 0 | } while (delay_ns && !job_is_cancelled(&job->job)); |
357 | 0 | } |
358 | | |
359 | | BlockJobInfo *block_job_query_locked(BlockJob *job, Error **errp) |
360 | 0 | { |
361 | 0 | BlockJobInfo *info; |
362 | 0 | uint64_t progress_current, progress_total; |
363 | 0 | const BlockJobDriver *drv = block_job_driver(job); |
364 | |
|
365 | 0 | GLOBAL_STATE_CODE(); |
366 | |
|
367 | 0 | if (block_job_is_internal(job)) { |
368 | 0 | error_setg(errp, "Cannot query QEMU internal jobs"); |
369 | 0 | return NULL; |
370 | 0 | } |
371 | | |
372 | 0 | progress_get_snapshot(&job->job.progress, &progress_current, |
373 | 0 | &progress_total); |
374 | |
|
375 | 0 | info = g_new0(BlockJobInfo, 1); |
376 | 0 | info->type = job_type(&job->job); |
377 | 0 | info->device = g_strdup(job->job.id); |
378 | 0 | info->busy = job->job.busy; |
379 | 0 | info->paused = job->job.pause_count > 0; |
380 | 0 | info->offset = progress_current; |
381 | 0 | info->len = progress_total; |
382 | 0 | info->speed = job->speed; |
383 | 0 | info->io_status = job->iostatus; |
384 | 0 | info->ready = job_is_ready_locked(&job->job), |
385 | 0 | info->status = job->job.status; |
386 | 0 | info->auto_finalize = job->job.auto_finalize; |
387 | 0 | info->auto_dismiss = job->job.auto_dismiss; |
388 | 0 | if (job->job.ret) { |
389 | 0 | info->error = job->job.err ? |
390 | 0 | g_strdup(error_get_pretty(job->job.err)) : |
391 | 0 | g_strdup(strerror(-job->job.ret)); |
392 | 0 | } |
393 | 0 | if (drv->query) { |
394 | 0 | job_unlock(); |
395 | 0 | drv->query(job, info); |
396 | 0 | job_lock(); |
397 | 0 | } |
398 | 0 | return info; |
399 | 0 | } |
400 | | |
401 | | /* Called with job lock held */ |
402 | | static void block_job_iostatus_set_err_locked(BlockJob *job, int error) |
403 | 0 | { |
404 | 0 | if (job->iostatus == BLOCK_DEVICE_IO_STATUS_OK) { |
405 | 0 | job->iostatus = error == ENOSPC ? BLOCK_DEVICE_IO_STATUS_NOSPACE : |
406 | 0 | BLOCK_DEVICE_IO_STATUS_FAILED; |
407 | 0 | } |
408 | 0 | } |
409 | | |
410 | | /* Called with job_mutex lock held. */ |
411 | | static void block_job_event_cancelled_locked(Notifier *n, void *opaque) |
412 | 0 | { |
413 | 0 | BlockJob *job = opaque; |
414 | 0 | uint64_t progress_current, progress_total; |
415 | |
|
416 | 0 | if (block_job_is_internal(job)) { |
417 | 0 | return; |
418 | 0 | } |
419 | | |
420 | 0 | progress_get_snapshot(&job->job.progress, &progress_current, |
421 | 0 | &progress_total); |
422 | |
|
423 | 0 | qapi_event_send_block_job_cancelled(job_type(&job->job), |
424 | 0 | job->job.id, |
425 | 0 | progress_total, |
426 | 0 | progress_current, |
427 | 0 | job->speed); |
428 | 0 | } |
429 | | |
430 | | /* Called with job_mutex lock held. */ |
431 | | static void block_job_event_completed_locked(Notifier *n, void *opaque) |
432 | 0 | { |
433 | 0 | BlockJob *job = opaque; |
434 | 0 | const char *msg = NULL; |
435 | 0 | uint64_t progress_current, progress_total; |
436 | |
|
437 | 0 | if (block_job_is_internal(job)) { |
438 | 0 | return; |
439 | 0 | } |
440 | | |
441 | 0 | if (job->job.ret < 0) { |
442 | 0 | msg = error_get_pretty(job->job.err); |
443 | 0 | } |
444 | |
|
445 | 0 | progress_get_snapshot(&job->job.progress, &progress_current, |
446 | 0 | &progress_total); |
447 | |
|
448 | 0 | qapi_event_send_block_job_completed(job_type(&job->job), |
449 | 0 | job->job.id, |
450 | 0 | progress_total, |
451 | 0 | progress_current, |
452 | 0 | job->speed, |
453 | 0 | msg); |
454 | 0 | } |
455 | | |
456 | | /* Called with job_mutex lock held. */ |
457 | | static void block_job_event_pending_locked(Notifier *n, void *opaque) |
458 | 0 | { |
459 | 0 | BlockJob *job = opaque; |
460 | |
|
461 | 0 | if (block_job_is_internal(job)) { |
462 | 0 | return; |
463 | 0 | } |
464 | | |
465 | 0 | qapi_event_send_block_job_pending(job_type(&job->job), |
466 | 0 | job->job.id); |
467 | 0 | } |
468 | | |
469 | | /* Called with job_mutex lock held. */ |
470 | | static void block_job_event_ready_locked(Notifier *n, void *opaque) |
471 | 0 | { |
472 | 0 | BlockJob *job = opaque; |
473 | 0 | uint64_t progress_current, progress_total; |
474 | |
|
475 | 0 | if (block_job_is_internal(job)) { |
476 | 0 | return; |
477 | 0 | } |
478 | | |
479 | 0 | progress_get_snapshot(&job->job.progress, &progress_current, |
480 | 0 | &progress_total); |
481 | |
|
482 | 0 | qapi_event_send_block_job_ready(job_type(&job->job), |
483 | 0 | job->job.id, |
484 | 0 | progress_total, |
485 | 0 | progress_current, |
486 | 0 | job->speed); |
487 | 0 | } |
488 | | |
489 | | |
490 | | void *block_job_create(const char *job_id, const BlockJobDriver *driver, |
491 | | JobTxn *txn, BlockDriverState *bs, uint64_t perm, |
492 | | uint64_t shared_perm, int64_t speed, int flags, |
493 | | BlockCompletionFunc *cb, void *opaque, Error **errp) |
494 | 0 | { |
495 | 0 | BlockJob *job; |
496 | 0 | int ret; |
497 | 0 | GLOBAL_STATE_CODE(); |
498 | |
|
499 | 0 | bdrv_graph_wrlock_drained(); |
500 | |
|
501 | 0 | if (job_id == NULL && !(flags & JOB_INTERNAL)) { |
502 | 0 | job_id = bdrv_get_device_name(bs); |
503 | 0 | } |
504 | |
|
505 | 0 | job = job_create(job_id, &driver->job_driver, txn, bdrv_get_aio_context(bs), |
506 | 0 | flags, cb, opaque, errp); |
507 | 0 | if (job == NULL) { |
508 | 0 | bdrv_graph_wrunlock(); |
509 | 0 | return NULL; |
510 | 0 | } |
511 | | |
512 | 0 | assert(is_block_job(&job->job)); |
513 | 0 | assert(job->job.driver->free == &block_job_free); |
514 | 0 | assert(job->job.driver->user_resume == &block_job_user_resume); |
515 | |
|
516 | 0 | ratelimit_init(&job->limit); |
517 | |
|
518 | 0 | job->finalize_cancelled_notifier.notify = block_job_event_cancelled_locked; |
519 | 0 | job->finalize_completed_notifier.notify = block_job_event_completed_locked; |
520 | 0 | job->pending_notifier.notify = block_job_event_pending_locked; |
521 | 0 | job->ready_notifier.notify = block_job_event_ready_locked; |
522 | 0 | job->idle_notifier.notify = block_job_on_idle_locked; |
523 | |
|
524 | 0 | WITH_JOB_LOCK_GUARD() { |
525 | 0 | notifier_list_add(&job->job.on_finalize_cancelled, |
526 | 0 | &job->finalize_cancelled_notifier); |
527 | 0 | notifier_list_add(&job->job.on_finalize_completed, |
528 | 0 | &job->finalize_completed_notifier); |
529 | 0 | notifier_list_add(&job->job.on_pending, &job->pending_notifier); |
530 | 0 | notifier_list_add(&job->job.on_ready, &job->ready_notifier); |
531 | 0 | notifier_list_add(&job->job.on_idle, &job->idle_notifier); |
532 | 0 | } |
533 | |
|
534 | 0 | error_setg(&job->blocker, "block device is in use by block job: %s", |
535 | 0 | job_type_str(&job->job)); |
536 | |
|
537 | 0 | ret = block_job_add_bdrv(job, "main node", bs, perm, shared_perm, errp); |
538 | 0 | if (ret < 0) { |
539 | 0 | goto fail; |
540 | 0 | } |
541 | | |
542 | 0 | if (!block_job_set_speed(job, speed, errp)) { |
543 | 0 | goto fail; |
544 | 0 | } |
545 | | |
546 | 0 | bdrv_graph_wrunlock(); |
547 | 0 | return job; |
548 | | |
549 | 0 | fail: |
550 | 0 | bdrv_graph_wrunlock(); |
551 | 0 | job_early_fail(&job->job); |
552 | 0 | return NULL; |
553 | 0 | } |
554 | | |
555 | | void block_job_iostatus_reset_locked(BlockJob *job) |
556 | 0 | { |
557 | 0 | GLOBAL_STATE_CODE(); |
558 | 0 | if (job->iostatus == BLOCK_DEVICE_IO_STATUS_OK) { |
559 | 0 | return; |
560 | 0 | } |
561 | 0 | assert(job->job.user_paused && job->job.pause_count > 0); |
562 | 0 | job->iostatus = BLOCK_DEVICE_IO_STATUS_OK; |
563 | 0 | } |
564 | | |
565 | | static void block_job_iostatus_reset(BlockJob *job) |
566 | 0 | { |
567 | 0 | JOB_LOCK_GUARD(); |
568 | 0 | block_job_iostatus_reset_locked(job); |
569 | 0 | } |
570 | | |
571 | | void block_job_user_resume(Job *job) |
572 | 0 | { |
573 | 0 | BlockJob *bjob = container_of(job, BlockJob, job); |
574 | 0 | GLOBAL_STATE_CODE(); |
575 | 0 | block_job_iostatus_reset(bjob); |
576 | 0 | } |
577 | | |
578 | | BlockErrorAction block_job_error_action(BlockJob *job, BlockdevOnError on_err, |
579 | | int is_read, int error) |
580 | 0 | { |
581 | 0 | BlockErrorAction action; |
582 | 0 | IO_CODE(); |
583 | |
|
584 | 0 | switch (on_err) { |
585 | 0 | case BLOCKDEV_ON_ERROR_ENOSPC: |
586 | 0 | case BLOCKDEV_ON_ERROR_AUTO: |
587 | 0 | action = (error == ENOSPC) ? |
588 | 0 | BLOCK_ERROR_ACTION_STOP : BLOCK_ERROR_ACTION_REPORT; |
589 | 0 | break; |
590 | 0 | case BLOCKDEV_ON_ERROR_STOP: |
591 | 0 | action = BLOCK_ERROR_ACTION_STOP; |
592 | 0 | break; |
593 | 0 | case BLOCKDEV_ON_ERROR_REPORT: |
594 | 0 | action = BLOCK_ERROR_ACTION_REPORT; |
595 | 0 | break; |
596 | 0 | case BLOCKDEV_ON_ERROR_IGNORE: |
597 | 0 | action = BLOCK_ERROR_ACTION_IGNORE; |
598 | 0 | break; |
599 | 0 | default: |
600 | 0 | abort(); |
601 | 0 | } |
602 | 0 | if (!block_job_is_internal(job)) { |
603 | 0 | qapi_event_send_block_job_error(job->job.id, |
604 | 0 | is_read ? IO_OPERATION_TYPE_READ : |
605 | 0 | IO_OPERATION_TYPE_WRITE, |
606 | 0 | action); |
607 | 0 | } |
608 | 0 | if (action == BLOCK_ERROR_ACTION_STOP) { |
609 | 0 | WITH_JOB_LOCK_GUARD() { |
610 | 0 | if (!job->job.user_paused) { |
611 | 0 | job_pause_locked(&job->job); |
612 | | /* |
613 | | * make the pause user visible, which will be |
614 | | * resumed from QMP. |
615 | | */ |
616 | 0 | job->job.user_paused = true; |
617 | 0 | } |
618 | 0 | block_job_iostatus_set_err_locked(job, error); |
619 | 0 | } |
620 | 0 | } |
621 | 0 | return action; |
622 | 0 | } |
623 | | |
624 | | AioContext *block_job_get_aio_context(BlockJob *job) |
625 | 0 | { |
626 | 0 | GLOBAL_STATE_CODE(); |
627 | 0 | return job->job.aio_context; |
628 | 0 | } |