hw/intc/arm_gicv3: Implement gicv3_cpuif_update()
[qemu/kevin.git] / blockjob.c
blob01b896b7e75626a8c185c8819edc4919af8d56e7
1 /*
2 * QEMU System Emulator block driver
4 * Copyright (c) 2011 IBM Corp.
5 * Copyright (c) 2012 Red Hat, Inc.
7 * Permission is hereby granted, free of charge, to any person obtaining a copy
8 * of this software and associated documentation files (the "Software"), to deal
9 * in the Software without restriction, including without limitation the rights
10 * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
11 * copies of the Software, and to permit persons to whom the Software is
12 * furnished to do so, subject to the following conditions:
14 * The above copyright notice and this permission notice shall be included in
15 * all copies or substantial portions of the Software.
17 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
18 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
19 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
20 * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
21 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
22 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
23 * THE SOFTWARE.
26 #include "qemu/osdep.h"
27 #include "qemu-common.h"
28 #include "trace.h"
29 #include "block/block.h"
30 #include "block/blockjob.h"
31 #include "block/block_int.h"
32 #include "sysemu/block-backend.h"
33 #include "qapi/qmp/qerror.h"
34 #include "qapi/qmp/qjson.h"
35 #include "qemu/coroutine.h"
36 #include "qmp-commands.h"
37 #include "qemu/timer.h"
38 #include "qapi-event.h"
40 /* Transactional group of block jobs */
41 struct BlockJobTxn {
43 /* Is this txn being cancelled? */
44 bool aborting;
46 /* List of jobs */
47 QLIST_HEAD(, BlockJob) jobs;
49 /* Reference count */
50 int refcnt;
53 static QLIST_HEAD(, BlockJob) block_jobs = QLIST_HEAD_INITIALIZER(block_jobs);
55 BlockJob *block_job_next(BlockJob *job)
57 if (!job) {
58 return QLIST_FIRST(&block_jobs);
60 return QLIST_NEXT(job, job_list);
63 void *block_job_create(const BlockJobDriver *driver, BlockDriverState *bs,
64 int64_t speed, BlockCompletionFunc *cb,
65 void *opaque, Error **errp)
67 BlockBackend *blk;
68 BlockJob *job;
70 if (bs->job) {
71 error_setg(errp, QERR_DEVICE_IN_USE, bdrv_get_device_name(bs));
72 return NULL;
75 blk = blk_new();
76 blk_insert_bs(blk, bs);
78 job = g_malloc0(driver->instance_size);
79 error_setg(&job->blocker, "block device is in use by block job: %s",
80 BlockJobType_lookup[driver->job_type]);
81 bdrv_op_block_all(bs, job->blocker);
82 bdrv_op_unblock(bs, BLOCK_OP_TYPE_DATAPLANE, job->blocker);
84 job->driver = driver;
85 job->id = g_strdup(bdrv_get_device_name(bs));
86 job->blk = blk;
87 job->cb = cb;
88 job->opaque = opaque;
89 job->busy = true;
90 job->refcnt = 1;
91 bs->job = job;
93 QLIST_INSERT_HEAD(&block_jobs, job, job_list);
95 /* Only set speed when necessary to avoid NotSupported error */
96 if (speed != 0) {
97 Error *local_err = NULL;
99 block_job_set_speed(job, speed, &local_err);
100 if (local_err) {
101 block_job_unref(job);
102 error_propagate(errp, local_err);
103 return NULL;
106 return job;
109 void block_job_ref(BlockJob *job)
111 ++job->refcnt;
114 void block_job_unref(BlockJob *job)
116 if (--job->refcnt == 0) {
117 BlockDriverState *bs = blk_bs(job->blk);
118 bs->job = NULL;
119 bdrv_op_unblock_all(bs, job->blocker);
120 blk_unref(job->blk);
121 error_free(job->blocker);
122 g_free(job->id);
123 QLIST_REMOVE(job, job_list);
124 g_free(job);
128 static void block_job_completed_single(BlockJob *job)
130 if (!job->ret) {
131 if (job->driver->commit) {
132 job->driver->commit(job);
134 } else {
135 if (job->driver->abort) {
136 job->driver->abort(job);
139 job->cb(job->opaque, job->ret);
140 if (job->txn) {
141 block_job_txn_unref(job->txn);
143 block_job_unref(job);
146 static void block_job_completed_txn_abort(BlockJob *job)
148 AioContext *ctx;
149 BlockJobTxn *txn = job->txn;
150 BlockJob *other_job, *next;
152 if (txn->aborting) {
154 * We are cancelled by another job, which will handle everything.
156 return;
158 txn->aborting = true;
159 /* We are the first failed job. Cancel other jobs. */
160 QLIST_FOREACH(other_job, &txn->jobs, txn_list) {
161 ctx = blk_get_aio_context(other_job->blk);
162 aio_context_acquire(ctx);
164 QLIST_FOREACH(other_job, &txn->jobs, txn_list) {
165 if (other_job == job || other_job->completed) {
166 /* Other jobs are "effectively" cancelled by us, set the status for
167 * them; this job, however, may or may not be cancelled, depending
168 * on the caller, so leave it. */
169 if (other_job != job) {
170 other_job->cancelled = true;
172 continue;
174 block_job_cancel_sync(other_job);
175 assert(other_job->completed);
177 QLIST_FOREACH_SAFE(other_job, &txn->jobs, txn_list, next) {
178 ctx = blk_get_aio_context(other_job->blk);
179 block_job_completed_single(other_job);
180 aio_context_release(ctx);
184 static void block_job_completed_txn_success(BlockJob *job)
186 AioContext *ctx;
187 BlockJobTxn *txn = job->txn;
188 BlockJob *other_job, *next;
190 * Successful completion, see if there are other running jobs in this
191 * txn.
193 QLIST_FOREACH(other_job, &txn->jobs, txn_list) {
194 if (!other_job->completed) {
195 return;
198 /* We are the last completed job, commit the transaction. */
199 QLIST_FOREACH_SAFE(other_job, &txn->jobs, txn_list, next) {
200 ctx = blk_get_aio_context(other_job->blk);
201 aio_context_acquire(ctx);
202 assert(other_job->ret == 0);
203 block_job_completed_single(other_job);
204 aio_context_release(ctx);
208 void block_job_completed(BlockJob *job, int ret)
210 assert(blk_bs(job->blk)->job == job);
211 assert(!job->completed);
212 job->completed = true;
213 job->ret = ret;
214 if (!job->txn) {
215 block_job_completed_single(job);
216 } else if (ret < 0 || block_job_is_cancelled(job)) {
217 block_job_completed_txn_abort(job);
218 } else {
219 block_job_completed_txn_success(job);
223 void block_job_set_speed(BlockJob *job, int64_t speed, Error **errp)
225 Error *local_err = NULL;
227 if (!job->driver->set_speed) {
228 error_setg(errp, QERR_UNSUPPORTED);
229 return;
231 job->driver->set_speed(job, speed, &local_err);
232 if (local_err) {
233 error_propagate(errp, local_err);
234 return;
237 job->speed = speed;
240 void block_job_complete(BlockJob *job, Error **errp)
242 if (job->pause_count || job->cancelled || !job->driver->complete) {
243 error_setg(errp, QERR_BLOCK_JOB_NOT_READY, job->id);
244 return;
247 job->driver->complete(job, errp);
250 void block_job_pause(BlockJob *job)
252 job->pause_count++;
255 bool block_job_is_paused(BlockJob *job)
257 return job->pause_count > 0;
260 void block_job_resume(BlockJob *job)
262 assert(job->pause_count > 0);
263 job->pause_count--;
264 if (job->pause_count) {
265 return;
267 block_job_enter(job);
270 void block_job_enter(BlockJob *job)
272 block_job_iostatus_reset(job);
273 if (job->co && !job->busy) {
274 qemu_coroutine_enter(job->co, NULL);
278 void block_job_cancel(BlockJob *job)
280 job->cancelled = true;
281 block_job_enter(job);
284 bool block_job_is_cancelled(BlockJob *job)
286 return job->cancelled;
289 void block_job_iostatus_reset(BlockJob *job)
291 job->iostatus = BLOCK_DEVICE_IO_STATUS_OK;
292 if (job->driver->iostatus_reset) {
293 job->driver->iostatus_reset(job);
297 static int block_job_finish_sync(BlockJob *job,
298 void (*finish)(BlockJob *, Error **errp),
299 Error **errp)
301 Error *local_err = NULL;
302 int ret;
304 assert(blk_bs(job->blk)->job == job);
306 block_job_ref(job);
307 finish(job, &local_err);
308 if (local_err) {
309 error_propagate(errp, local_err);
310 block_job_unref(job);
311 return -EBUSY;
313 while (!job->completed) {
314 aio_poll(job->deferred_to_main_loop ? qemu_get_aio_context() :
315 blk_get_aio_context(job->blk),
316 true);
318 ret = (job->cancelled && job->ret == 0) ? -ECANCELED : job->ret;
319 block_job_unref(job);
320 return ret;
323 /* A wrapper around block_job_cancel() taking an Error ** parameter so it may be
324 * used with block_job_finish_sync() without the need for (rather nasty)
325 * function pointer casts there. */
326 static void block_job_cancel_err(BlockJob *job, Error **errp)
328 block_job_cancel(job);
331 int block_job_cancel_sync(BlockJob *job)
333 return block_job_finish_sync(job, &block_job_cancel_err, NULL);
336 void block_job_cancel_sync_all(void)
338 BlockJob *job;
339 AioContext *aio_context;
341 while ((job = QLIST_FIRST(&block_jobs))) {
342 aio_context = blk_get_aio_context(job->blk);
343 aio_context_acquire(aio_context);
344 block_job_cancel_sync(job);
345 aio_context_release(aio_context);
349 int block_job_complete_sync(BlockJob *job, Error **errp)
351 return block_job_finish_sync(job, &block_job_complete, errp);
354 void block_job_sleep_ns(BlockJob *job, QEMUClockType type, int64_t ns)
356 assert(job->busy);
358 /* Check cancellation *before* setting busy = false, too! */
359 if (block_job_is_cancelled(job)) {
360 return;
363 job->busy = false;
364 if (!block_job_is_paused(job)) {
365 co_aio_sleep_ns(blk_get_aio_context(job->blk), type, ns);
367 /* The job can be paused while sleeping, so check this again */
368 if (block_job_is_paused(job)) {
369 qemu_coroutine_yield();
371 job->busy = true;
374 void block_job_yield(BlockJob *job)
376 assert(job->busy);
378 /* Check cancellation *before* setting busy = false, too! */
379 if (block_job_is_cancelled(job)) {
380 return;
383 job->busy = false;
384 qemu_coroutine_yield();
385 job->busy = true;
388 BlockJobInfo *block_job_query(BlockJob *job)
390 BlockJobInfo *info = g_new0(BlockJobInfo, 1);
391 info->type = g_strdup(BlockJobType_lookup[job->driver->job_type]);
392 info->device = g_strdup(job->id);
393 info->len = job->len;
394 info->busy = job->busy;
395 info->paused = job->pause_count > 0;
396 info->offset = job->offset;
397 info->speed = job->speed;
398 info->io_status = job->iostatus;
399 info->ready = job->ready;
400 return info;
403 static void block_job_iostatus_set_err(BlockJob *job, int error)
405 if (job->iostatus == BLOCK_DEVICE_IO_STATUS_OK) {
406 job->iostatus = error == ENOSPC ? BLOCK_DEVICE_IO_STATUS_NOSPACE :
407 BLOCK_DEVICE_IO_STATUS_FAILED;
411 void block_job_event_cancelled(BlockJob *job)
413 qapi_event_send_block_job_cancelled(job->driver->job_type,
414 job->id,
415 job->len,
416 job->offset,
417 job->speed,
418 &error_abort);
421 void block_job_event_completed(BlockJob *job, const char *msg)
423 qapi_event_send_block_job_completed(job->driver->job_type,
424 job->id,
425 job->len,
426 job->offset,
427 job->speed,
428 !!msg,
429 msg,
430 &error_abort);
433 void block_job_event_ready(BlockJob *job)
435 job->ready = true;
437 qapi_event_send_block_job_ready(job->driver->job_type,
438 job->id,
439 job->len,
440 job->offset,
441 job->speed, &error_abort);
444 BlockErrorAction block_job_error_action(BlockJob *job, BlockdevOnError on_err,
445 int is_read, int error)
447 BlockErrorAction action;
449 switch (on_err) {
450 case BLOCKDEV_ON_ERROR_ENOSPC:
451 action = (error == ENOSPC) ?
452 BLOCK_ERROR_ACTION_STOP : BLOCK_ERROR_ACTION_REPORT;
453 break;
454 case BLOCKDEV_ON_ERROR_STOP:
455 action = BLOCK_ERROR_ACTION_STOP;
456 break;
457 case BLOCKDEV_ON_ERROR_REPORT:
458 action = BLOCK_ERROR_ACTION_REPORT;
459 break;
460 case BLOCKDEV_ON_ERROR_IGNORE:
461 action = BLOCK_ERROR_ACTION_IGNORE;
462 break;
463 default:
464 abort();
466 qapi_event_send_block_job_error(job->id,
467 is_read ? IO_OPERATION_TYPE_READ :
468 IO_OPERATION_TYPE_WRITE,
469 action, &error_abort);
470 if (action == BLOCK_ERROR_ACTION_STOP) {
471 /* make the pause user visible, which will be resumed from QMP. */
472 job->user_paused = true;
473 block_job_pause(job);
474 block_job_iostatus_set_err(job, error);
476 return action;
479 typedef struct {
480 BlockJob *job;
481 QEMUBH *bh;
482 AioContext *aio_context;
483 BlockJobDeferToMainLoopFn *fn;
484 void *opaque;
485 } BlockJobDeferToMainLoopData;
487 static void block_job_defer_to_main_loop_bh(void *opaque)
489 BlockJobDeferToMainLoopData *data = opaque;
490 AioContext *aio_context;
492 qemu_bh_delete(data->bh);
494 /* Prevent race with block_job_defer_to_main_loop() */
495 aio_context_acquire(data->aio_context);
497 /* Fetch BDS AioContext again, in case it has changed */
498 aio_context = blk_get_aio_context(data->job->blk);
499 aio_context_acquire(aio_context);
501 data->job->deferred_to_main_loop = false;
502 data->fn(data->job, data->opaque);
504 aio_context_release(aio_context);
506 aio_context_release(data->aio_context);
508 g_free(data);
511 void block_job_defer_to_main_loop(BlockJob *job,
512 BlockJobDeferToMainLoopFn *fn,
513 void *opaque)
515 BlockJobDeferToMainLoopData *data = g_malloc(sizeof(*data));
516 data->job = job;
517 data->bh = qemu_bh_new(block_job_defer_to_main_loop_bh, data);
518 data->aio_context = blk_get_aio_context(job->blk);
519 data->fn = fn;
520 data->opaque = opaque;
521 job->deferred_to_main_loop = true;
523 qemu_bh_schedule(data->bh);
526 BlockJobTxn *block_job_txn_new(void)
528 BlockJobTxn *txn = g_new0(BlockJobTxn, 1);
529 QLIST_INIT(&txn->jobs);
530 txn->refcnt = 1;
531 return txn;
534 static void block_job_txn_ref(BlockJobTxn *txn)
536 txn->refcnt++;
539 void block_job_txn_unref(BlockJobTxn *txn)
541 if (txn && --txn->refcnt == 0) {
542 g_free(txn);
546 void block_job_txn_add_job(BlockJobTxn *txn, BlockJob *job)
548 if (!txn) {
549 return;
552 assert(!job->txn);
553 job->txn = txn;
555 QLIST_INSERT_HEAD(&txn->jobs, job, txn_list);
556 block_job_txn_ref(txn);