]> git.proxmox.com Git - mirror_qemu.git/blame - blockjob.c
nbd: BLOCK_STATUS constants
[mirror_qemu.git] / blockjob.c
CommitLineData
2f0c9fe6
PB
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
d38ea87a 26#include "qemu/osdep.h"
2f0c9fe6 27#include "qemu-common.h"
737e150e 28#include "block/block.h"
c87621ea 29#include "block/blockjob_int.h"
737e150e 30#include "block/block_int.h"
373340b2 31#include "sysemu/block-backend.h"
e688df6b 32#include "qapi/error.h"
cc7a8ea7 33#include "qapi/qmp/qerror.h"
10817bf0 34#include "qemu/coroutine.h"
7f0317cf 35#include "qemu/id.h"
1de7afc9 36#include "qemu/timer.h"
5a2d2cbd 37#include "qapi-event.h"
2f0c9fe6 38
fc24908e
PB
39/* Right now, this mutex is only needed to synchronize accesses to job->busy
40 * and job->sleep_timer, such as concurrent calls to block_job_do_yield and
41 * block_job_enter. */
42static QemuMutex block_job_mutex;
43
44static void block_job_lock(void)
45{
46 qemu_mutex_lock(&block_job_mutex);
47}
48
49static void block_job_unlock(void)
50{
51 qemu_mutex_unlock(&block_job_mutex);
52}
53
54static void __attribute__((__constructor__)) block_job_init(void)
55{
56 qemu_mutex_init(&block_job_mutex);
57}
58
8254b6d9
JS
59static void block_job_event_cancelled(BlockJob *job);
60static void block_job_event_completed(BlockJob *job, const char *msg);
aa9ef2e6 61static void block_job_enter_cond(BlockJob *job, bool(*fn)(BlockJob *job));
8254b6d9 62
c55a832f
FZ
63/* Transactional group of block jobs */
64struct BlockJobTxn {
65
66 /* Is this txn being cancelled? */
67 bool aborting;
68
69 /* List of jobs */
70 QLIST_HEAD(, BlockJob) jobs;
71
72 /* Reference count */
73 int refcnt;
74};
75
a7112795
AG
76static QLIST_HEAD(, BlockJob) block_jobs = QLIST_HEAD_INITIALIZER(block_jobs);
77
88691b37
PB
78/*
79 * The block job API is composed of two categories of functions.
80 *
81 * The first includes functions used by the monitor. The monitor is
82 * peculiar in that it accesses the block job list with block_job_get, and
83 * therefore needs consistency across block_job_get and the actual operation
84 * (e.g. block_job_set_speed). The consistency is achieved with
85 * aio_context_acquire/release. These functions are declared in blockjob.h.
86 *
87 * The second includes functions used by the block job drivers and sometimes
88 * by the core block layer. These do not care about locking, because the
89 * whole coroutine runs under the AioContext lock, and are declared in
90 * blockjob_int.h.
91 */
92
a7112795
AG
93BlockJob *block_job_next(BlockJob *job)
94{
95 if (!job) {
96 return QLIST_FIRST(&block_jobs);
97 }
98 return QLIST_NEXT(job, job_list);
99}
100
ffb1f10c
AG
101BlockJob *block_job_get(const char *id)
102{
103 BlockJob *job;
104
105 QLIST_FOREACH(job, &block_jobs, job_list) {
559b935f 106 if (job->id && !strcmp(id, job->id)) {
ffb1f10c
AG
107 return job;
108 }
109 }
110
111 return NULL;
112}
113
c8ab5c2d
PB
114BlockJobTxn *block_job_txn_new(void)
115{
116 BlockJobTxn *txn = g_new0(BlockJobTxn, 1);
117 QLIST_INIT(&txn->jobs);
118 txn->refcnt = 1;
119 return txn;
120}
121
122static void block_job_txn_ref(BlockJobTxn *txn)
123{
124 txn->refcnt++;
125}
126
127void block_job_txn_unref(BlockJobTxn *txn)
128{
129 if (txn && --txn->refcnt == 0) {
130 g_free(txn);
131 }
132}
133
134void block_job_txn_add_job(BlockJobTxn *txn, BlockJob *job)
135{
136 if (!txn) {
137 return;
138 }
139
140 assert(!job->txn);
141 job->txn = txn;
142
143 QLIST_INSERT_HEAD(&txn->jobs, job, txn_list);
144 block_job_txn_ref(txn);
145}
146
f321dcb5
PB
147static void block_job_pause(BlockJob *job)
148{
149 job->pause_count++;
150}
151
152static void block_job_resume(BlockJob *job)
153{
154 assert(job->pause_count > 0);
155 job->pause_count--;
156 if (job->pause_count) {
157 return;
158 }
159 block_job_enter(job);
160}
161
4172a003 162void block_job_ref(BlockJob *job)
05b0d8e3
PB
163{
164 ++job->refcnt;
165}
166
167static void block_job_attached_aio_context(AioContext *new_context,
168 void *opaque);
169static void block_job_detach_aio_context(void *opaque);
170
4172a003 171void block_job_unref(BlockJob *job)
05b0d8e3
PB
172{
173 if (--job->refcnt == 0) {
174 BlockDriverState *bs = blk_bs(job->blk);
0a3e155f 175 QLIST_REMOVE(job, job_list);
05b0d8e3
PB
176 bs->job = NULL;
177 block_job_remove_all_bdrv(job);
178 blk_remove_aio_context_notifier(job->blk,
179 block_job_attached_aio_context,
180 block_job_detach_aio_context, job);
181 blk_unref(job->blk);
182 error_free(job->blocker);
183 g_free(job->id);
fc24908e 184 assert(!timer_pending(&job->sleep_timer));
05b0d8e3
PB
185 g_free(job);
186 }
187}
188
463e0be1
SH
189static void block_job_attached_aio_context(AioContext *new_context,
190 void *opaque)
191{
192 BlockJob *job = opaque;
193
194 if (job->driver->attached_aio_context) {
195 job->driver->attached_aio_context(job, new_context);
196 }
197
198 block_job_resume(job);
199}
200
bae8196d
PB
201static void block_job_drain(BlockJob *job)
202{
203 /* If job is !job->busy this kicks it into the next pause point. */
204 block_job_enter(job);
205
206 blk_drain(job->blk);
207 if (job->driver->drain) {
208 job->driver->drain(job);
209 }
210}
211
463e0be1
SH
212static void block_job_detach_aio_context(void *opaque)
213{
214 BlockJob *job = opaque;
215
216 /* In case the job terminates during aio_poll()... */
217 block_job_ref(job);
218
219 block_job_pause(job);
220
463e0be1 221 while (!job->paused && !job->completed) {
bae8196d 222 block_job_drain(job);
463e0be1
SH
223 }
224
225 block_job_unref(job);
226}
227
f321dcb5
PB
228static char *child_job_get_parent_desc(BdrvChild *c)
229{
230 BlockJob *job = c->opaque;
231 return g_strdup_printf("%s job '%s'",
977c736f 232 BlockJobType_str(job->driver->job_type),
f321dcb5
PB
233 job->id);
234}
235
ad90feba 236static void child_job_drained_begin(BdrvChild *c)
f321dcb5 237{
ad90feba 238 BlockJob *job = c->opaque;
f321dcb5
PB
239 block_job_pause(job);
240}
241
ad90feba 242static void child_job_drained_end(BdrvChild *c)
f321dcb5 243{
ad90feba 244 BlockJob *job = c->opaque;
f321dcb5
PB
245 block_job_resume(job);
246}
247
ad90feba
KW
248static const BdrvChildRole child_job = {
249 .get_parent_desc = child_job_get_parent_desc,
250 .drained_begin = child_job_drained_begin,
251 .drained_end = child_job_drained_end,
252 .stay_at_node = true,
f321dcb5
PB
253};
254
bbc02b90
KW
255void block_job_remove_all_bdrv(BlockJob *job)
256{
257 GSList *l;
258 for (l = job->nodes; l; l = l->next) {
259 BdrvChild *c = l->data;
260 bdrv_op_unblock_all(c->bs, job->blocker);
261 bdrv_root_unref_child(c);
262 }
263 g_slist_free(job->nodes);
264 job->nodes = NULL;
265}
266
76d554e2
KW
267int block_job_add_bdrv(BlockJob *job, const char *name, BlockDriverState *bs,
268 uint64_t perm, uint64_t shared_perm, Error **errp)
23d402d4 269{
76d554e2
KW
270 BdrvChild *c;
271
272 c = bdrv_root_attach_child(bs, name, &child_job, perm, shared_perm,
273 job, errp);
274 if (c == NULL) {
275 return -EPERM;
276 }
277
278 job->nodes = g_slist_prepend(job->nodes, c);
23d402d4
AG
279 bdrv_ref(bs);
280 bdrv_op_block_all(bs, job->blocker);
76d554e2
KW
281
282 return 0;
23d402d4
AG
283}
284
559b935f
JS
285bool block_job_is_internal(BlockJob *job)
286{
287 return (job->id == NULL);
288}
289
5ccac6f1
JS
290static bool block_job_started(BlockJob *job)
291{
292 return job->co;
293}
294
e3796a24
JS
295/**
296 * All jobs must allow a pause point before entering their job proper. This
297 * ensures that jobs can be paused prior to being started, then resumed later.
298 */
299static void coroutine_fn block_job_co_entry(void *opaque)
300{
301 BlockJob *job = opaque;
302
303 assert(job && job->driver && job->driver->start);
304 block_job_pause_point(job);
305 job->driver->start(job);
306}
307
fc24908e
PB
308static void block_job_sleep_timer_cb(void *opaque)
309{
310 BlockJob *job = opaque;
311
312 block_job_enter(job);
313}
314
5ccac6f1
JS
315void block_job_start(BlockJob *job)
316{
317 assert(job && !block_job_started(job) && job->paused &&
e3796a24
JS
318 job->driver && job->driver->start);
319 job->co = qemu_coroutine_create(block_job_co_entry, job);
320 job->pause_count--;
321 job->busy = true;
322 job->paused = false;
aef4278c 323 bdrv_coroutine_enter(blk_bs(job->blk), job->co);
5ccac6f1
JS
324}
325
c55a832f
FZ
326static void block_job_completed_single(BlockJob *job)
327{
4fb588e9
PB
328 assert(job->completed);
329
c55a832f
FZ
330 if (!job->ret) {
331 if (job->driver->commit) {
332 job->driver->commit(job);
333 }
334 } else {
335 if (job->driver->abort) {
336 job->driver->abort(job);
337 }
338 }
e8a40bf7
JS
339 if (job->driver->clean) {
340 job->driver->clean(job);
341 }
8254b6d9
JS
342
343 if (job->cb) {
344 job->cb(job->opaque, job->ret);
345 }
5ccac6f1
JS
346
347 /* Emit events only if we actually started */
348 if (block_job_started(job)) {
349 if (block_job_is_cancelled(job)) {
350 block_job_event_cancelled(job);
351 } else {
352 const char *msg = NULL;
353 if (job->ret < 0) {
354 msg = strerror(-job->ret);
355 }
356 block_job_event_completed(job, msg);
8254b6d9 357 }
8254b6d9
JS
358 }
359
c55a832f 360 if (job->txn) {
1e93b9fb 361 QLIST_REMOVE(job, txn_list);
c55a832f
FZ
362 block_job_txn_unref(job->txn);
363 }
364 block_job_unref(job);
365}
366
4c241cf5
PB
367static void block_job_cancel_async(BlockJob *job)
368{
369 if (job->iostatus != BLOCK_DEVICE_IO_STATUS_OK) {
370 block_job_iostatus_reset(job);
371 }
372 if (job->user_paused) {
373 /* Do not call block_job_enter here, the caller will handle it. */
374 job->user_paused = false;
375 job->pause_count--;
376 }
377 job->cancelled = true;
378}
379
c8ab5c2d
PB
380static int block_job_finish_sync(BlockJob *job,
381 void (*finish)(BlockJob *, Error **errp),
382 Error **errp)
383{
384 Error *local_err = NULL;
385 int ret;
386
387 assert(blk_bs(job->blk)->job == job);
388
389 block_job_ref(job);
390
4fb588e9
PB
391 if (finish) {
392 finish(job, &local_err);
393 }
c8ab5c2d
PB
394 if (local_err) {
395 error_propagate(errp, local_err);
396 block_job_unref(job);
397 return -EBUSY;
398 }
399 /* block_job_drain calls block_job_enter, and it should be enough to
400 * induce progress until the job completes or moves to the main thread.
401 */
402 while (!job->deferred_to_main_loop && !job->completed) {
403 block_job_drain(job);
404 }
405 while (!job->completed) {
406 aio_poll(qemu_get_aio_context(), true);
407 }
408 ret = (job->cancelled && job->ret == 0) ? -ECANCELED : job->ret;
409 block_job_unref(job);
410 return ret;
411}
412
c55a832f
FZ
413static void block_job_completed_txn_abort(BlockJob *job)
414{
415 AioContext *ctx;
416 BlockJobTxn *txn = job->txn;
4fb588e9 417 BlockJob *other_job;
c55a832f
FZ
418
419 if (txn->aborting) {
420 /*
421 * We are cancelled by another job, which will handle everything.
422 */
423 return;
424 }
425 txn->aborting = true;
4fb588e9
PB
426 block_job_txn_ref(txn);
427
c55a832f
FZ
428 /* We are the first failed job. Cancel other jobs. */
429 QLIST_FOREACH(other_job, &txn->jobs, txn_list) {
b6d2e599 430 ctx = blk_get_aio_context(other_job->blk);
c55a832f
FZ
431 aio_context_acquire(ctx);
432 }
4fb588e9
PB
433
434 /* Other jobs are effectively cancelled by us, set the status for
435 * them; this job, however, may or may not be cancelled, depending
436 * on the caller, so leave it. */
c55a832f 437 QLIST_FOREACH(other_job, &txn->jobs, txn_list) {
4fb588e9
PB
438 if (other_job != job) {
439 block_job_cancel_async(other_job);
c55a832f 440 }
c55a832f 441 }
4fb588e9
PB
442 while (!QLIST_EMPTY(&txn->jobs)) {
443 other_job = QLIST_FIRST(&txn->jobs);
b6d2e599 444 ctx = blk_get_aio_context(other_job->blk);
4fb588e9
PB
445 if (!other_job->completed) {
446 assert(other_job->cancelled);
447 block_job_finish_sync(other_job, NULL, NULL);
448 }
c55a832f
FZ
449 block_job_completed_single(other_job);
450 aio_context_release(ctx);
451 }
4fb588e9
PB
452
453 block_job_txn_unref(txn);
c55a832f
FZ
454}
455
456static void block_job_completed_txn_success(BlockJob *job)
457{
458 AioContext *ctx;
459 BlockJobTxn *txn = job->txn;
460 BlockJob *other_job, *next;
461 /*
462 * Successful completion, see if there are other running jobs in this
463 * txn.
464 */
465 QLIST_FOREACH(other_job, &txn->jobs, txn_list) {
466 if (!other_job->completed) {
467 return;
468 }
469 }
470 /* We are the last completed job, commit the transaction. */
471 QLIST_FOREACH_SAFE(other_job, &txn->jobs, txn_list, next) {
b6d2e599 472 ctx = blk_get_aio_context(other_job->blk);
c55a832f
FZ
473 aio_context_acquire(ctx);
474 assert(other_job->ret == 0);
475 block_job_completed_single(other_job);
476 aio_context_release(ctx);
477 }
478}
479
aa9ef2e6
JS
480/* Assumes the block_job_mutex is held */
481static bool block_job_timer_pending(BlockJob *job)
482{
483 return timer_pending(&job->sleep_timer);
484}
485
2f0c9fe6
PB
486void block_job_set_speed(BlockJob *job, int64_t speed, Error **errp)
487{
488 Error *local_err = NULL;
aa9ef2e6 489 int64_t old_speed = job->speed;
2f0c9fe6 490
3fc4b10a 491 if (!job->driver->set_speed) {
c6bd8c70 492 error_setg(errp, QERR_UNSUPPORTED);
2f0c9fe6
PB
493 return;
494 }
3fc4b10a 495 job->driver->set_speed(job, speed, &local_err);
84d18f06 496 if (local_err) {
2f0c9fe6
PB
497 error_propagate(errp, local_err);
498 return;
499 }
500
501 job->speed = speed;
aa9ef2e6
JS
502 if (speed <= old_speed) {
503 return;
504 }
505
506 /* kick only if a timer is pending */
507 block_job_enter_cond(job, block_job_timer_pending);
2f0c9fe6
PB
508}
509
aeae883b
PB
510void block_job_complete(BlockJob *job, Error **errp)
511{
559b935f
JS
512 /* Should not be reachable via external interface for internal jobs */
513 assert(job->id);
5ccac6f1
JS
514 if (job->pause_count || job->cancelled ||
515 !block_job_started(job) || !job->driver->complete) {
9df229c3
AG
516 error_setg(errp, "The active block job '%s' cannot be completed",
517 job->id);
aeae883b
PB
518 return;
519 }
520
3fc4b10a 521 job->driver->complete(job, errp);
aeae883b
PB
522}
523
0df4ba58
JS
524void block_job_user_pause(BlockJob *job)
525{
526 job->user_paused = true;
527 block_job_pause(job);
528}
529
0df4ba58
JS
530bool block_job_user_paused(BlockJob *job)
531{
6573d9c6 532 return job->user_paused;
0df4ba58
JS
533}
534
0df4ba58
JS
535void block_job_user_resume(BlockJob *job)
536{
537 if (job && job->user_paused && job->pause_count > 0) {
2caf63a9 538 block_job_iostatus_reset(job);
4c241cf5 539 job->user_paused = false;
0df4ba58
JS
540 block_job_resume(job);
541 }
542}
543
8acc72a4
PB
544void block_job_cancel(BlockJob *job)
545{
5ccac6f1 546 if (block_job_started(job)) {
4c241cf5 547 block_job_cancel_async(job);
5ccac6f1
JS
548 block_job_enter(job);
549 } else {
550 block_job_completed(job, -ECANCELED);
551 }
8acc72a4
PB
552}
553
345f9e1b
HR
554/* A wrapper around block_job_cancel() taking an Error ** parameter so it may be
555 * used with block_job_finish_sync() without the need for (rather nasty)
556 * function pointer casts there. */
557static void block_job_cancel_err(BlockJob *job, Error **errp)
558{
559 block_job_cancel(job);
560}
561
562int block_job_cancel_sync(BlockJob *job)
563{
564 return block_job_finish_sync(job, &block_job_cancel_err, NULL);
565}
566
a1a2af07
KW
567void block_job_cancel_sync_all(void)
568{
569 BlockJob *job;
570 AioContext *aio_context;
571
572 while ((job = QLIST_FIRST(&block_jobs))) {
b6d2e599 573 aio_context = blk_get_aio_context(job->blk);
a1a2af07
KW
574 aio_context_acquire(aio_context);
575 block_job_cancel_sync(job);
576 aio_context_release(aio_context);
577 }
578}
579
345f9e1b
HR
580int block_job_complete_sync(BlockJob *job, Error **errp)
581{
582 return block_job_finish_sync(job, &block_job_complete, errp);
583}
584
559b935f 585BlockJobInfo *block_job_query(BlockJob *job, Error **errp)
30e628b7 586{
559b935f
JS
587 BlockJobInfo *info;
588
589 if (block_job_is_internal(job)) {
590 error_setg(errp, "Cannot query QEMU internal jobs");
591 return NULL;
592 }
593 info = g_new0(BlockJobInfo, 1);
977c736f 594 info->type = g_strdup(BlockJobType_str(job->driver->job_type));
8ccb9569 595 info->device = g_strdup(job->id);
32c81a4a 596 info->len = job->len;
fc24908e 597 info->busy = atomic_read(&job->busy);
751ebd76 598 info->paused = job->pause_count > 0;
32c81a4a
PB
599 info->offset = job->offset;
600 info->speed = job->speed;
601 info->io_status = job->iostatus;
ef6dbf1e 602 info->ready = job->ready;
30e628b7
PB
603 return info;
604}
32c81a4a
PB
605
606static void block_job_iostatus_set_err(BlockJob *job, int error)
607{
608 if (job->iostatus == BLOCK_DEVICE_IO_STATUS_OK) {
609 job->iostatus = error == ENOSPC ? BLOCK_DEVICE_IO_STATUS_NOSPACE :
610 BLOCK_DEVICE_IO_STATUS_FAILED;
611 }
612}
613
8254b6d9 614static void block_job_event_cancelled(BlockJob *job)
bcada37b 615{
559b935f
JS
616 if (block_job_is_internal(job)) {
617 return;
618 }
619
bcada37b 620 qapi_event_send_block_job_cancelled(job->driver->job_type,
8ccb9569 621 job->id,
bcada37b
WX
622 job->len,
623 job->offset,
624 job->speed,
625 &error_abort);
626}
32c81a4a 627
8254b6d9 628static void block_job_event_completed(BlockJob *job, const char *msg)
a66a2a36 629{
559b935f
JS
630 if (block_job_is_internal(job)) {
631 return;
632 }
633
bcada37b 634 qapi_event_send_block_job_completed(job->driver->job_type,
8ccb9569 635 job->id,
bcada37b
WX
636 job->len,
637 job->offset,
638 job->speed,
639 !!msg,
640 msg,
641 &error_abort);
a66a2a36
PB
642}
643
88691b37
PB
644/*
645 * API for block job drivers and the block layer. These functions are
646 * declared in blockjob_int.h.
647 */
648
649void *block_job_create(const char *job_id, const BlockJobDriver *driver,
650 BlockDriverState *bs, uint64_t perm,
651 uint64_t shared_perm, int64_t speed, int flags,
652 BlockCompletionFunc *cb, void *opaque, Error **errp)
653{
654 BlockBackend *blk;
655 BlockJob *job;
656 int ret;
657
658 if (bs->job) {
659 error_setg(errp, QERR_DEVICE_IN_USE, bdrv_get_device_name(bs));
660 return NULL;
661 }
662
663 if (job_id == NULL && !(flags & BLOCK_JOB_INTERNAL)) {
664 job_id = bdrv_get_device_name(bs);
665 if (!*job_id) {
666 error_setg(errp, "An explicit job ID is required for this node");
667 return NULL;
668 }
669 }
670
671 if (job_id) {
672 if (flags & BLOCK_JOB_INTERNAL) {
673 error_setg(errp, "Cannot specify job ID for internal block job");
674 return NULL;
675 }
676
677 if (!id_wellformed(job_id)) {
678 error_setg(errp, "Invalid job ID '%s'", job_id);
679 return NULL;
680 }
681
682 if (block_job_get(job_id)) {
683 error_setg(errp, "Job ID '%s' already in use", job_id);
684 return NULL;
685 }
686 }
687
688 blk = blk_new(perm, shared_perm);
689 ret = blk_insert_bs(blk, bs, errp);
690 if (ret < 0) {
691 blk_unref(blk);
692 return NULL;
693 }
694
695 job = g_malloc0(driver->instance_size);
696 job->driver = driver;
697 job->id = g_strdup(job_id);
698 job->blk = blk;
699 job->cb = cb;
700 job->opaque = opaque;
701 job->busy = false;
702 job->paused = true;
703 job->pause_count = 1;
704 job->refcnt = 1;
fc24908e
PB
705 aio_timer_init(qemu_get_aio_context(), &job->sleep_timer,
706 QEMU_CLOCK_REALTIME, SCALE_NS,
707 block_job_sleep_timer_cb, job);
88691b37
PB
708
709 error_setg(&job->blocker, "block device is in use by block job: %s",
977c736f 710 BlockJobType_str(driver->job_type));
88691b37
PB
711 block_job_add_bdrv(job, "main node", bs, 0, BLK_PERM_ALL, &error_abort);
712 bs->job = job;
713
88691b37
PB
714 bdrv_op_unblock(bs, BLOCK_OP_TYPE_DATAPLANE, job->blocker);
715
716 QLIST_INSERT_HEAD(&block_jobs, job, job_list);
717
718 blk_add_aio_context_notifier(blk, block_job_attached_aio_context,
719 block_job_detach_aio_context, job);
720
721 /* Only set speed when necessary to avoid NotSupported error */
722 if (speed != 0) {
723 Error *local_err = NULL;
724
725 block_job_set_speed(job, speed, &local_err);
726 if (local_err) {
727 block_job_unref(job);
728 error_propagate(errp, local_err);
729 return NULL;
730 }
731 }
732 return job;
733}
734
f321dcb5
PB
735void block_job_pause_all(void)
736{
737 BlockJob *job = NULL;
738 while ((job = block_job_next(job))) {
739 AioContext *aio_context = blk_get_aio_context(job->blk);
740
741 aio_context_acquire(aio_context);
3d5d319e 742 block_job_ref(job);
f321dcb5
PB
743 block_job_pause(job);
744 aio_context_release(aio_context);
745 }
746}
747
88691b37
PB
748void block_job_early_fail(BlockJob *job)
749{
750 block_job_unref(job);
751}
752
753void block_job_completed(BlockJob *job, int ret)
754{
755 assert(blk_bs(job->blk)->job == job);
756 assert(!job->completed);
757 job->completed = true;
758 job->ret = ret;
759 if (!job->txn) {
760 block_job_completed_single(job);
761 } else if (ret < 0 || block_job_is_cancelled(job)) {
762 block_job_completed_txn_abort(job);
763 } else {
764 block_job_completed_txn_success(job);
765 }
766}
767
768static bool block_job_should_pause(BlockJob *job)
769{
770 return job->pause_count > 0;
771}
772
fc24908e
PB
773/* Yield, and schedule a timer to reenter the coroutine after @ns nanoseconds.
774 * Reentering the job coroutine with block_job_enter() before the timer has
775 * expired is allowed and cancels the timer.
776 *
777 * If @ns is (uint64_t) -1, no timer is scheduled and block_job_enter() must be
778 * called explicitly. */
779static void block_job_do_yield(BlockJob *job, uint64_t ns)
356f59b8 780{
fc24908e
PB
781 block_job_lock();
782 if (ns != -1) {
783 timer_mod(&job->sleep_timer, ns);
784 }
356f59b8 785 job->busy = false;
fc24908e 786 block_job_unlock();
356f59b8
PB
787 qemu_coroutine_yield();
788
789 /* Set by block_job_enter before re-entering the coroutine. */
790 assert(job->busy);
791}
792
88691b37
PB
793void coroutine_fn block_job_pause_point(BlockJob *job)
794{
795 assert(job && block_job_started(job));
796
797 if (!block_job_should_pause(job)) {
798 return;
799 }
800 if (block_job_is_cancelled(job)) {
801 return;
802 }
803
804 if (job->driver->pause) {
805 job->driver->pause(job);
806 }
807
808 if (block_job_should_pause(job) && !block_job_is_cancelled(job)) {
809 job->paused = true;
fc24908e 810 block_job_do_yield(job, -1);
88691b37
PB
811 job->paused = false;
812 }
813
814 if (job->driver->resume) {
815 job->driver->resume(job);
816 }
817}
818
f321dcb5
PB
819void block_job_resume_all(void)
820{
3d5d319e
AG
821 BlockJob *job, *next;
822
823 QLIST_FOREACH_SAFE(job, &block_jobs, job_list, next) {
f321dcb5
PB
824 AioContext *aio_context = blk_get_aio_context(job->blk);
825
826 aio_context_acquire(aio_context);
827 block_job_resume(job);
3d5d319e 828 block_job_unref(job);
f321dcb5
PB
829 aio_context_release(aio_context);
830 }
831}
832
aa9ef2e6
JS
833/*
834 * Conditionally enter a block_job pending a call to fn() while
835 * under the block_job_lock critical section.
836 */
837static void block_job_enter_cond(BlockJob *job, bool(*fn)(BlockJob *job))
88691b37 838{
eb05e011
PB
839 if (!block_job_started(job)) {
840 return;
841 }
842 if (job->deferred_to_main_loop) {
843 return;
844 }
845
fc24908e 846 block_job_lock();
356f59b8 847 if (job->busy) {
fc24908e 848 block_job_unlock();
356f59b8 849 return;
88691b37 850 }
356f59b8 851
aa9ef2e6
JS
852 if (fn && !fn(job)) {
853 block_job_unlock();
854 return;
855 }
856
fc24908e
PB
857 assert(!job->deferred_to_main_loop);
858 timer_del(&job->sleep_timer);
356f59b8 859 job->busy = true;
fc24908e 860 block_job_unlock();
356f59b8 861 aio_co_wake(job->co);
88691b37
PB
862}
863
aa9ef2e6
JS
864void block_job_enter(BlockJob *job)
865{
866 block_job_enter_cond(job, NULL);
867}
868
88691b37
PB
869bool block_job_is_cancelled(BlockJob *job)
870{
871 return job->cancelled;
872}
873
5bf1d5a7 874void block_job_sleep_ns(BlockJob *job, int64_t ns)
88691b37
PB
875{
876 assert(job->busy);
877
878 /* Check cancellation *before* setting busy = false, too! */
879 if (block_job_is_cancelled(job)) {
880 return;
881 }
882
88691b37 883 if (!block_job_should_pause(job)) {
fc24908e 884 block_job_do_yield(job, qemu_clock_get_ns(QEMU_CLOCK_REALTIME) + ns);
88691b37 885 }
88691b37
PB
886
887 block_job_pause_point(job);
888}
889
890void block_job_yield(BlockJob *job)
891{
892 assert(job->busy);
893
894 /* Check cancellation *before* setting busy = false, too! */
895 if (block_job_is_cancelled(job)) {
896 return;
897 }
898
88691b37 899 if (!block_job_should_pause(job)) {
fc24908e 900 block_job_do_yield(job, -1);
88691b37 901 }
88691b37
PB
902
903 block_job_pause_point(job);
904}
905
2caf63a9
PB
906void block_job_iostatus_reset(BlockJob *job)
907{
4c241cf5
PB
908 if (job->iostatus == BLOCK_DEVICE_IO_STATUS_OK) {
909 return;
910 }
911 assert(job->user_paused && job->pause_count > 0);
2caf63a9
PB
912 job->iostatus = BLOCK_DEVICE_IO_STATUS_OK;
913}
914
bcada37b 915void block_job_event_ready(BlockJob *job)
a66a2a36 916{
ef6dbf1e
HR
917 job->ready = true;
918
559b935f
JS
919 if (block_job_is_internal(job)) {
920 return;
921 }
922
518848a2 923 qapi_event_send_block_job_ready(job->driver->job_type,
8ccb9569 924 job->id,
518848a2
MA
925 job->len,
926 job->offset,
927 job->speed, &error_abort);
a66a2a36
PB
928}
929
81e254dc 930BlockErrorAction block_job_error_action(BlockJob *job, BlockdevOnError on_err,
32c81a4a
PB
931 int is_read, int error)
932{
933 BlockErrorAction action;
934
935 switch (on_err) {
936 case BLOCKDEV_ON_ERROR_ENOSPC:
8c398252 937 case BLOCKDEV_ON_ERROR_AUTO:
a589569f
WX
938 action = (error == ENOSPC) ?
939 BLOCK_ERROR_ACTION_STOP : BLOCK_ERROR_ACTION_REPORT;
32c81a4a
PB
940 break;
941 case BLOCKDEV_ON_ERROR_STOP:
a589569f 942 action = BLOCK_ERROR_ACTION_STOP;
32c81a4a
PB
943 break;
944 case BLOCKDEV_ON_ERROR_REPORT:
a589569f 945 action = BLOCK_ERROR_ACTION_REPORT;
32c81a4a
PB
946 break;
947 case BLOCKDEV_ON_ERROR_IGNORE:
a589569f 948 action = BLOCK_ERROR_ACTION_IGNORE;
32c81a4a
PB
949 break;
950 default:
951 abort();
952 }
559b935f
JS
953 if (!block_job_is_internal(job)) {
954 qapi_event_send_block_job_error(job->id,
955 is_read ? IO_OPERATION_TYPE_READ :
956 IO_OPERATION_TYPE_WRITE,
957 action, &error_abort);
958 }
a589569f 959 if (action == BLOCK_ERROR_ACTION_STOP) {
751ebd76 960 /* make the pause user visible, which will be resumed from QMP. */
0df4ba58 961 block_job_user_pause(job);
32c81a4a 962 block_job_iostatus_set_err(job, error);
32c81a4a
PB
963 }
964 return action;
965}
dec7d421
SH
966
967typedef struct {
968 BlockJob *job;
dec7d421
SH
969 AioContext *aio_context;
970 BlockJobDeferToMainLoopFn *fn;
971 void *opaque;
972} BlockJobDeferToMainLoopData;
973
974static void block_job_defer_to_main_loop_bh(void *opaque)
975{
976 BlockJobDeferToMainLoopData *data = opaque;
977 AioContext *aio_context;
978
dec7d421
SH
979 /* Prevent race with block_job_defer_to_main_loop() */
980 aio_context_acquire(data->aio_context);
981
982 /* Fetch BDS AioContext again, in case it has changed */
b6d2e599 983 aio_context = blk_get_aio_context(data->job->blk);
d79df2a2
PB
984 if (aio_context != data->aio_context) {
985 aio_context_acquire(aio_context);
986 }
dec7d421
SH
987
988 data->fn(data->job, data->opaque);
989
d79df2a2
PB
990 if (aio_context != data->aio_context) {
991 aio_context_release(aio_context);
992 }
dec7d421
SH
993
994 aio_context_release(data->aio_context);
995
996 g_free(data);
997}
998
999void block_job_defer_to_main_loop(BlockJob *job,
1000 BlockJobDeferToMainLoopFn *fn,
1001 void *opaque)
1002{
1003 BlockJobDeferToMainLoopData *data = g_malloc(sizeof(*data));
1004 data->job = job;
b6d2e599 1005 data->aio_context = blk_get_aio_context(job->blk);
dec7d421
SH
1006 data->fn = fn;
1007 data->opaque = opaque;
794f0141 1008 job->deferred_to_main_loop = true;
dec7d421 1009
fffb6e12
PB
1010 aio_bh_schedule_oneshot(qemu_get_aio_context(),
1011 block_job_defer_to_main_loop_bh, data);
dec7d421 1012}