m25p80: Make a table for JEDEC ID.
[qemu/ar7.git] / blockjob.c
blob90c4e262b090f693ad048a4586755f003e65b3c6
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 /* Normally the job runs in its BlockBackend's AioContext. The exception is
64 * block_job_defer_to_main_loop() where it runs in the QEMU main loop. Code
65 * that supports both cases uses this helper function.
67 static AioContext *block_job_get_aio_context(BlockJob *job)
69 return job->deferred_to_main_loop ?
70 qemu_get_aio_context() :
71 blk_get_aio_context(job->blk);
74 static void block_job_attached_aio_context(AioContext *new_context,
75 void *opaque)
77 BlockJob *job = opaque;
79 if (job->driver->attached_aio_context) {
80 job->driver->attached_aio_context(job, new_context);
83 block_job_resume(job);
86 static void block_job_detach_aio_context(void *opaque)
88 BlockJob *job = opaque;
90 /* In case the job terminates during aio_poll()... */
91 block_job_ref(job);
93 block_job_pause(job);
95 if (!job->paused) {
96 /* If job is !job->busy this kicks it into the next pause point. */
97 block_job_enter(job);
99 while (!job->paused && !job->completed) {
100 aio_poll(block_job_get_aio_context(job), true);
103 block_job_unref(job);
106 void *block_job_create(const BlockJobDriver *driver, BlockDriverState *bs,
107 int64_t speed, BlockCompletionFunc *cb,
108 void *opaque, Error **errp)
110 BlockBackend *blk;
111 BlockJob *job;
113 if (bs->job) {
114 error_setg(errp, QERR_DEVICE_IN_USE, bdrv_get_device_name(bs));
115 return NULL;
118 blk = blk_new();
119 blk_insert_bs(blk, bs);
121 job = g_malloc0(driver->instance_size);
122 error_setg(&job->blocker, "block device is in use by block job: %s",
123 BlockJobType_lookup[driver->job_type]);
124 bdrv_op_block_all(bs, job->blocker);
125 bdrv_op_unblock(bs, BLOCK_OP_TYPE_DATAPLANE, job->blocker);
127 job->driver = driver;
128 job->id = g_strdup(bdrv_get_device_name(bs));
129 job->blk = blk;
130 job->cb = cb;
131 job->opaque = opaque;
132 job->busy = true;
133 job->refcnt = 1;
134 bs->job = job;
136 QLIST_INSERT_HEAD(&block_jobs, job, job_list);
138 blk_add_aio_context_notifier(blk, block_job_attached_aio_context,
139 block_job_detach_aio_context, job);
141 /* Only set speed when necessary to avoid NotSupported error */
142 if (speed != 0) {
143 Error *local_err = NULL;
145 block_job_set_speed(job, speed, &local_err);
146 if (local_err) {
147 block_job_unref(job);
148 error_propagate(errp, local_err);
149 return NULL;
152 return job;
155 void block_job_ref(BlockJob *job)
157 ++job->refcnt;
160 void block_job_unref(BlockJob *job)
162 if (--job->refcnt == 0) {
163 BlockDriverState *bs = blk_bs(job->blk);
164 bs->job = NULL;
165 bdrv_op_unblock_all(bs, job->blocker);
166 blk_remove_aio_context_notifier(job->blk,
167 block_job_attached_aio_context,
168 block_job_detach_aio_context, job);
169 blk_unref(job->blk);
170 error_free(job->blocker);
171 g_free(job->id);
172 QLIST_REMOVE(job, job_list);
173 g_free(job);
177 static void block_job_completed_single(BlockJob *job)
179 if (!job->ret) {
180 if (job->driver->commit) {
181 job->driver->commit(job);
183 } else {
184 if (job->driver->abort) {
185 job->driver->abort(job);
188 job->cb(job->opaque, job->ret);
189 if (job->txn) {
190 block_job_txn_unref(job->txn);
192 block_job_unref(job);
195 static void block_job_completed_txn_abort(BlockJob *job)
197 AioContext *ctx;
198 BlockJobTxn *txn = job->txn;
199 BlockJob *other_job, *next;
201 if (txn->aborting) {
203 * We are cancelled by another job, which will handle everything.
205 return;
207 txn->aborting = true;
208 /* We are the first failed job. Cancel other jobs. */
209 QLIST_FOREACH(other_job, &txn->jobs, txn_list) {
210 ctx = blk_get_aio_context(other_job->blk);
211 aio_context_acquire(ctx);
213 QLIST_FOREACH(other_job, &txn->jobs, txn_list) {
214 if (other_job == job || other_job->completed) {
215 /* Other jobs are "effectively" cancelled by us, set the status for
216 * them; this job, however, may or may not be cancelled, depending
217 * on the caller, so leave it. */
218 if (other_job != job) {
219 other_job->cancelled = true;
221 continue;
223 block_job_cancel_sync(other_job);
224 assert(other_job->completed);
226 QLIST_FOREACH_SAFE(other_job, &txn->jobs, txn_list, next) {
227 ctx = blk_get_aio_context(other_job->blk);
228 block_job_completed_single(other_job);
229 aio_context_release(ctx);
233 static void block_job_completed_txn_success(BlockJob *job)
235 AioContext *ctx;
236 BlockJobTxn *txn = job->txn;
237 BlockJob *other_job, *next;
239 * Successful completion, see if there are other running jobs in this
240 * txn.
242 QLIST_FOREACH(other_job, &txn->jobs, txn_list) {
243 if (!other_job->completed) {
244 return;
247 /* We are the last completed job, commit the transaction. */
248 QLIST_FOREACH_SAFE(other_job, &txn->jobs, txn_list, next) {
249 ctx = blk_get_aio_context(other_job->blk);
250 aio_context_acquire(ctx);
251 assert(other_job->ret == 0);
252 block_job_completed_single(other_job);
253 aio_context_release(ctx);
257 void block_job_completed(BlockJob *job, int ret)
259 assert(blk_bs(job->blk)->job == job);
260 assert(!job->completed);
261 job->completed = true;
262 job->ret = ret;
263 if (!job->txn) {
264 block_job_completed_single(job);
265 } else if (ret < 0 || block_job_is_cancelled(job)) {
266 block_job_completed_txn_abort(job);
267 } else {
268 block_job_completed_txn_success(job);
272 void block_job_set_speed(BlockJob *job, int64_t speed, Error **errp)
274 Error *local_err = NULL;
276 if (!job->driver->set_speed) {
277 error_setg(errp, QERR_UNSUPPORTED);
278 return;
280 job->driver->set_speed(job, speed, &local_err);
281 if (local_err) {
282 error_propagate(errp, local_err);
283 return;
286 job->speed = speed;
289 void block_job_complete(BlockJob *job, Error **errp)
291 if (job->pause_count || job->cancelled || !job->driver->complete) {
292 error_setg(errp, QERR_BLOCK_JOB_NOT_READY, job->id);
293 return;
296 job->driver->complete(job, errp);
299 void block_job_pause(BlockJob *job)
301 job->pause_count++;
304 static bool block_job_should_pause(BlockJob *job)
306 return job->pause_count > 0;
309 void coroutine_fn block_job_pause_point(BlockJob *job)
311 if (!block_job_should_pause(job)) {
312 return;
314 if (block_job_is_cancelled(job)) {
315 return;
318 if (job->driver->pause) {
319 job->driver->pause(job);
322 if (block_job_should_pause(job) && !block_job_is_cancelled(job)) {
323 job->paused = true;
324 job->busy = false;
325 qemu_coroutine_yield(); /* wait for block_job_resume() */
326 job->busy = true;
327 job->paused = false;
330 if (job->driver->resume) {
331 job->driver->resume(job);
335 void block_job_resume(BlockJob *job)
337 assert(job->pause_count > 0);
338 job->pause_count--;
339 if (job->pause_count) {
340 return;
342 block_job_enter(job);
345 void block_job_enter(BlockJob *job)
347 if (job->co && !job->busy) {
348 qemu_coroutine_enter(job->co, NULL);
352 void block_job_cancel(BlockJob *job)
354 job->cancelled = true;
355 block_job_iostatus_reset(job);
356 block_job_enter(job);
359 bool block_job_is_cancelled(BlockJob *job)
361 return job->cancelled;
364 void block_job_iostatus_reset(BlockJob *job)
366 job->iostatus = BLOCK_DEVICE_IO_STATUS_OK;
367 if (job->driver->iostatus_reset) {
368 job->driver->iostatus_reset(job);
372 static int block_job_finish_sync(BlockJob *job,
373 void (*finish)(BlockJob *, Error **errp),
374 Error **errp)
376 Error *local_err = NULL;
377 int ret;
379 assert(blk_bs(job->blk)->job == job);
381 block_job_ref(job);
382 finish(job, &local_err);
383 if (local_err) {
384 error_propagate(errp, local_err);
385 block_job_unref(job);
386 return -EBUSY;
388 while (!job->completed) {
389 aio_poll(block_job_get_aio_context(job), true);
391 ret = (job->cancelled && job->ret == 0) ? -ECANCELED : job->ret;
392 block_job_unref(job);
393 return ret;
396 /* A wrapper around block_job_cancel() taking an Error ** parameter so it may be
397 * used with block_job_finish_sync() without the need for (rather nasty)
398 * function pointer casts there. */
399 static void block_job_cancel_err(BlockJob *job, Error **errp)
401 block_job_cancel(job);
404 int block_job_cancel_sync(BlockJob *job)
406 return block_job_finish_sync(job, &block_job_cancel_err, NULL);
409 void block_job_cancel_sync_all(void)
411 BlockJob *job;
412 AioContext *aio_context;
414 while ((job = QLIST_FIRST(&block_jobs))) {
415 aio_context = blk_get_aio_context(job->blk);
416 aio_context_acquire(aio_context);
417 block_job_cancel_sync(job);
418 aio_context_release(aio_context);
422 int block_job_complete_sync(BlockJob *job, Error **errp)
424 return block_job_finish_sync(job, &block_job_complete, errp);
427 void block_job_sleep_ns(BlockJob *job, QEMUClockType type, int64_t ns)
429 assert(job->busy);
431 /* Check cancellation *before* setting busy = false, too! */
432 if (block_job_is_cancelled(job)) {
433 return;
436 job->busy = false;
437 if (!block_job_should_pause(job)) {
438 co_aio_sleep_ns(blk_get_aio_context(job->blk), type, ns);
440 job->busy = true;
442 block_job_pause_point(job);
445 void block_job_yield(BlockJob *job)
447 assert(job->busy);
449 /* Check cancellation *before* setting busy = false, too! */
450 if (block_job_is_cancelled(job)) {
451 return;
454 job->busy = false;
455 if (!block_job_should_pause(job)) {
456 qemu_coroutine_yield();
458 job->busy = true;
460 block_job_pause_point(job);
463 BlockJobInfo *block_job_query(BlockJob *job)
465 BlockJobInfo *info = g_new0(BlockJobInfo, 1);
466 info->type = g_strdup(BlockJobType_lookup[job->driver->job_type]);
467 info->device = g_strdup(job->id);
468 info->len = job->len;
469 info->busy = job->busy;
470 info->paused = job->pause_count > 0;
471 info->offset = job->offset;
472 info->speed = job->speed;
473 info->io_status = job->iostatus;
474 info->ready = job->ready;
475 return info;
478 static void block_job_iostatus_set_err(BlockJob *job, int error)
480 if (job->iostatus == BLOCK_DEVICE_IO_STATUS_OK) {
481 job->iostatus = error == ENOSPC ? BLOCK_DEVICE_IO_STATUS_NOSPACE :
482 BLOCK_DEVICE_IO_STATUS_FAILED;
486 void block_job_event_cancelled(BlockJob *job)
488 qapi_event_send_block_job_cancelled(job->driver->job_type,
489 job->id,
490 job->len,
491 job->offset,
492 job->speed,
493 &error_abort);
496 void block_job_event_completed(BlockJob *job, const char *msg)
498 qapi_event_send_block_job_completed(job->driver->job_type,
499 job->id,
500 job->len,
501 job->offset,
502 job->speed,
503 !!msg,
504 msg,
505 &error_abort);
508 void block_job_event_ready(BlockJob *job)
510 job->ready = true;
512 qapi_event_send_block_job_ready(job->driver->job_type,
513 job->id,
514 job->len,
515 job->offset,
516 job->speed, &error_abort);
519 BlockErrorAction block_job_error_action(BlockJob *job, BlockdevOnError on_err,
520 int is_read, int error)
522 BlockErrorAction action;
524 switch (on_err) {
525 case BLOCKDEV_ON_ERROR_ENOSPC:
526 action = (error == ENOSPC) ?
527 BLOCK_ERROR_ACTION_STOP : BLOCK_ERROR_ACTION_REPORT;
528 break;
529 case BLOCKDEV_ON_ERROR_STOP:
530 action = BLOCK_ERROR_ACTION_STOP;
531 break;
532 case BLOCKDEV_ON_ERROR_REPORT:
533 action = BLOCK_ERROR_ACTION_REPORT;
534 break;
535 case BLOCKDEV_ON_ERROR_IGNORE:
536 action = BLOCK_ERROR_ACTION_IGNORE;
537 break;
538 default:
539 abort();
541 qapi_event_send_block_job_error(job->id,
542 is_read ? IO_OPERATION_TYPE_READ :
543 IO_OPERATION_TYPE_WRITE,
544 action, &error_abort);
545 if (action == BLOCK_ERROR_ACTION_STOP) {
546 /* make the pause user visible, which will be resumed from QMP. */
547 job->user_paused = true;
548 block_job_pause(job);
549 block_job_iostatus_set_err(job, error);
551 return action;
554 typedef struct {
555 BlockJob *job;
556 QEMUBH *bh;
557 AioContext *aio_context;
558 BlockJobDeferToMainLoopFn *fn;
559 void *opaque;
560 } BlockJobDeferToMainLoopData;
562 static void block_job_defer_to_main_loop_bh(void *opaque)
564 BlockJobDeferToMainLoopData *data = opaque;
565 AioContext *aio_context;
567 qemu_bh_delete(data->bh);
569 /* Prevent race with block_job_defer_to_main_loop() */
570 aio_context_acquire(data->aio_context);
572 /* Fetch BDS AioContext again, in case it has changed */
573 aio_context = blk_get_aio_context(data->job->blk);
574 aio_context_acquire(aio_context);
576 data->job->deferred_to_main_loop = false;
577 data->fn(data->job, data->opaque);
579 aio_context_release(aio_context);
581 aio_context_release(data->aio_context);
583 g_free(data);
586 void block_job_defer_to_main_loop(BlockJob *job,
587 BlockJobDeferToMainLoopFn *fn,
588 void *opaque)
590 BlockJobDeferToMainLoopData *data = g_malloc(sizeof(*data));
591 data->job = job;
592 data->bh = qemu_bh_new(block_job_defer_to_main_loop_bh, data);
593 data->aio_context = blk_get_aio_context(job->blk);
594 data->fn = fn;
595 data->opaque = opaque;
596 job->deferred_to_main_loop = true;
598 qemu_bh_schedule(data->bh);
601 BlockJobTxn *block_job_txn_new(void)
603 BlockJobTxn *txn = g_new0(BlockJobTxn, 1);
604 QLIST_INIT(&txn->jobs);
605 txn->refcnt = 1;
606 return txn;
609 static void block_job_txn_ref(BlockJobTxn *txn)
611 txn->refcnt++;
614 void block_job_txn_unref(BlockJobTxn *txn)
616 if (txn && --txn->refcnt == 0) {
617 g_free(txn);
621 void block_job_txn_add_job(BlockJobTxn *txn, BlockJob *job)
623 if (!txn) {
624 return;
627 assert(!job->txn);
628 job->txn = txn;
630 QLIST_INSERT_HEAD(&txn->jobs, job, txn_list);
631 block_job_txn_ref(txn);