block: Convert block job core to BlockBackend
[qemu.git] / blockjob.c
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-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"
39
40 /* Transactional group of block jobs */
41 struct BlockJobTxn {
42
43 /* Is this txn being cancelled? */
44 bool aborting;
45
46 /* List of jobs */
47 QLIST_HEAD(, BlockJob) jobs;
48
49 /* Reference count */
50 int refcnt;
51 };
52
53 static QLIST_HEAD(, BlockJob) block_jobs = QLIST_HEAD_INITIALIZER(block_jobs);
54
55 BlockJob *block_job_next(BlockJob *job)
56 {
57 if (!job) {
58 return QLIST_FIRST(&block_jobs);
59 }
60 return QLIST_NEXT(job, job_list);
61 }
62
63 void *block_job_create(const BlockJobDriver *driver, BlockDriverState *bs,
64 int64_t speed, BlockCompletionFunc *cb,
65 void *opaque, Error **errp)
66 {
67 BlockBackend *blk;
68 BlockJob *job;
69
70 if (bs->job) {
71 error_setg(errp, QERR_DEVICE_IN_USE, bdrv_get_device_name(bs));
72 return NULL;
73 }
74
75 blk = blk_new();
76 blk_insert_bs(blk, bs);
77
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);
83
84 job->driver = driver;
85 job->id = g_strdup(bdrv_get_device_name(bs));
86 job->bs = bs;
87 job->blk = blk;
88 job->cb = cb;
89 job->opaque = opaque;
90 job->busy = true;
91 job->refcnt = 1;
92 bs->job = job;
93
94 QLIST_INSERT_HEAD(&block_jobs, job, job_list);
95
96 /* Only set speed when necessary to avoid NotSupported error */
97 if (speed != 0) {
98 Error *local_err = NULL;
99
100 block_job_set_speed(job, speed, &local_err);
101 if (local_err) {
102 block_job_unref(job);
103 error_propagate(errp, local_err);
104 return NULL;
105 }
106 }
107 return job;
108 }
109
110 void block_job_ref(BlockJob *job)
111 {
112 ++job->refcnt;
113 }
114
115 void block_job_unref(BlockJob *job)
116 {
117 if (--job->refcnt == 0) {
118 BlockDriverState *bs = blk_bs(job->blk);
119 bs->job = NULL;
120 bdrv_op_unblock_all(bs, job->blocker);
121 blk_unref(job->blk);
122 error_free(job->blocker);
123 g_free(job->id);
124 QLIST_REMOVE(job, job_list);
125 g_free(job);
126 }
127 }
128
129 static void block_job_completed_single(BlockJob *job)
130 {
131 if (!job->ret) {
132 if (job->driver->commit) {
133 job->driver->commit(job);
134 }
135 } else {
136 if (job->driver->abort) {
137 job->driver->abort(job);
138 }
139 }
140 job->cb(job->opaque, job->ret);
141 if (job->txn) {
142 block_job_txn_unref(job->txn);
143 }
144 block_job_unref(job);
145 }
146
147 static void block_job_completed_txn_abort(BlockJob *job)
148 {
149 AioContext *ctx;
150 BlockJobTxn *txn = job->txn;
151 BlockJob *other_job, *next;
152
153 if (txn->aborting) {
154 /*
155 * We are cancelled by another job, which will handle everything.
156 */
157 return;
158 }
159 txn->aborting = true;
160 /* We are the first failed job. Cancel other jobs. */
161 QLIST_FOREACH(other_job, &txn->jobs, txn_list) {
162 ctx = blk_get_aio_context(other_job->blk);
163 aio_context_acquire(ctx);
164 }
165 QLIST_FOREACH(other_job, &txn->jobs, txn_list) {
166 if (other_job == job || other_job->completed) {
167 /* Other jobs are "effectively" cancelled by us, set the status for
168 * them; this job, however, may or may not be cancelled, depending
169 * on the caller, so leave it. */
170 if (other_job != job) {
171 other_job->cancelled = true;
172 }
173 continue;
174 }
175 block_job_cancel_sync(other_job);
176 assert(other_job->completed);
177 }
178 QLIST_FOREACH_SAFE(other_job, &txn->jobs, txn_list, next) {
179 ctx = blk_get_aio_context(other_job->blk);
180 block_job_completed_single(other_job);
181 aio_context_release(ctx);
182 }
183 }
184
185 static void block_job_completed_txn_success(BlockJob *job)
186 {
187 AioContext *ctx;
188 BlockJobTxn *txn = job->txn;
189 BlockJob *other_job, *next;
190 /*
191 * Successful completion, see if there are other running jobs in this
192 * txn.
193 */
194 QLIST_FOREACH(other_job, &txn->jobs, txn_list) {
195 if (!other_job->completed) {
196 return;
197 }
198 }
199 /* We are the last completed job, commit the transaction. */
200 QLIST_FOREACH_SAFE(other_job, &txn->jobs, txn_list, next) {
201 ctx = blk_get_aio_context(other_job->blk);
202 aio_context_acquire(ctx);
203 assert(other_job->ret == 0);
204 block_job_completed_single(other_job);
205 aio_context_release(ctx);
206 }
207 }
208
209 void block_job_completed(BlockJob *job, int ret)
210 {
211 assert(blk_bs(job->blk)->job == job);
212 assert(!job->completed);
213 job->completed = true;
214 job->ret = ret;
215 if (!job->txn) {
216 block_job_completed_single(job);
217 } else if (ret < 0 || block_job_is_cancelled(job)) {
218 block_job_completed_txn_abort(job);
219 } else {
220 block_job_completed_txn_success(job);
221 }
222 }
223
224 void block_job_set_speed(BlockJob *job, int64_t speed, Error **errp)
225 {
226 Error *local_err = NULL;
227
228 if (!job->driver->set_speed) {
229 error_setg(errp, QERR_UNSUPPORTED);
230 return;
231 }
232 job->driver->set_speed(job, speed, &local_err);
233 if (local_err) {
234 error_propagate(errp, local_err);
235 return;
236 }
237
238 job->speed = speed;
239 }
240
241 void block_job_complete(BlockJob *job, Error **errp)
242 {
243 if (job->pause_count || job->cancelled || !job->driver->complete) {
244 error_setg(errp, QERR_BLOCK_JOB_NOT_READY, job->id);
245 return;
246 }
247
248 job->driver->complete(job, errp);
249 }
250
251 void block_job_pause(BlockJob *job)
252 {
253 job->pause_count++;
254 }
255
256 bool block_job_is_paused(BlockJob *job)
257 {
258 return job->pause_count > 0;
259 }
260
261 void block_job_resume(BlockJob *job)
262 {
263 assert(job->pause_count > 0);
264 job->pause_count--;
265 if (job->pause_count) {
266 return;
267 }
268 block_job_enter(job);
269 }
270
271 void block_job_enter(BlockJob *job)
272 {
273 block_job_iostatus_reset(job);
274 if (job->co && !job->busy) {
275 qemu_coroutine_enter(job->co, NULL);
276 }
277 }
278
279 void block_job_cancel(BlockJob *job)
280 {
281 job->cancelled = true;
282 block_job_enter(job);
283 }
284
285 bool block_job_is_cancelled(BlockJob *job)
286 {
287 return job->cancelled;
288 }
289
290 void block_job_iostatus_reset(BlockJob *job)
291 {
292 job->iostatus = BLOCK_DEVICE_IO_STATUS_OK;
293 if (job->driver->iostatus_reset) {
294 job->driver->iostatus_reset(job);
295 }
296 }
297
298 static int block_job_finish_sync(BlockJob *job,
299 void (*finish)(BlockJob *, Error **errp),
300 Error **errp)
301 {
302 Error *local_err = NULL;
303 int ret;
304
305 assert(blk_bs(job->blk)->job == job);
306
307 block_job_ref(job);
308 finish(job, &local_err);
309 if (local_err) {
310 error_propagate(errp, local_err);
311 block_job_unref(job);
312 return -EBUSY;
313 }
314 while (!job->completed) {
315 aio_poll(job->deferred_to_main_loop ? qemu_get_aio_context() :
316 blk_get_aio_context(job->blk),
317 true);
318 }
319 ret = (job->cancelled && job->ret == 0) ? -ECANCELED : job->ret;
320 block_job_unref(job);
321 return ret;
322 }
323
324 /* A wrapper around block_job_cancel() taking an Error ** parameter so it may be
325 * used with block_job_finish_sync() without the need for (rather nasty)
326 * function pointer casts there. */
327 static void block_job_cancel_err(BlockJob *job, Error **errp)
328 {
329 block_job_cancel(job);
330 }
331
332 int block_job_cancel_sync(BlockJob *job)
333 {
334 return block_job_finish_sync(job, &block_job_cancel_err, NULL);
335 }
336
337 void block_job_cancel_sync_all(void)
338 {
339 BlockJob *job;
340 AioContext *aio_context;
341
342 while ((job = QLIST_FIRST(&block_jobs))) {
343 aio_context = blk_get_aio_context(job->blk);
344 aio_context_acquire(aio_context);
345 block_job_cancel_sync(job);
346 aio_context_release(aio_context);
347 }
348 }
349
350 int block_job_complete_sync(BlockJob *job, Error **errp)
351 {
352 return block_job_finish_sync(job, &block_job_complete, errp);
353 }
354
355 void block_job_sleep_ns(BlockJob *job, QEMUClockType type, int64_t ns)
356 {
357 assert(job->busy);
358
359 /* Check cancellation *before* setting busy = false, too! */
360 if (block_job_is_cancelled(job)) {
361 return;
362 }
363
364 job->busy = false;
365 if (block_job_is_paused(job)) {
366 qemu_coroutine_yield();
367 } else {
368 co_aio_sleep_ns(blk_get_aio_context(job->blk), type, ns);
369 }
370 job->busy = true;
371 }
372
373 void block_job_yield(BlockJob *job)
374 {
375 assert(job->busy);
376
377 /* Check cancellation *before* setting busy = false, too! */
378 if (block_job_is_cancelled(job)) {
379 return;
380 }
381
382 job->busy = false;
383 qemu_coroutine_yield();
384 job->busy = true;
385 }
386
387 BlockJobInfo *block_job_query(BlockJob *job)
388 {
389 BlockJobInfo *info = g_new0(BlockJobInfo, 1);
390 info->type = g_strdup(BlockJobType_lookup[job->driver->job_type]);
391 info->device = g_strdup(job->id);
392 info->len = job->len;
393 info->busy = job->busy;
394 info->paused = job->pause_count > 0;
395 info->offset = job->offset;
396 info->speed = job->speed;
397 info->io_status = job->iostatus;
398 info->ready = job->ready;
399 return info;
400 }
401
402 static void block_job_iostatus_set_err(BlockJob *job, int error)
403 {
404 if (job->iostatus == BLOCK_DEVICE_IO_STATUS_OK) {
405 job->iostatus = error == ENOSPC ? BLOCK_DEVICE_IO_STATUS_NOSPACE :
406 BLOCK_DEVICE_IO_STATUS_FAILED;
407 }
408 }
409
410 void block_job_event_cancelled(BlockJob *job)
411 {
412 qapi_event_send_block_job_cancelled(job->driver->job_type,
413 job->id,
414 job->len,
415 job->offset,
416 job->speed,
417 &error_abort);
418 }
419
420 void block_job_event_completed(BlockJob *job, const char *msg)
421 {
422 qapi_event_send_block_job_completed(job->driver->job_type,
423 job->id,
424 job->len,
425 job->offset,
426 job->speed,
427 !!msg,
428 msg,
429 &error_abort);
430 }
431
432 void block_job_event_ready(BlockJob *job)
433 {
434 job->ready = true;
435
436 qapi_event_send_block_job_ready(job->driver->job_type,
437 job->id,
438 job->len,
439 job->offset,
440 job->speed, &error_abort);
441 }
442
443 BlockErrorAction block_job_error_action(BlockJob *job, BlockdevOnError on_err,
444 int is_read, int error)
445 {
446 BlockErrorAction action;
447
448 switch (on_err) {
449 case BLOCKDEV_ON_ERROR_ENOSPC:
450 action = (error == ENOSPC) ?
451 BLOCK_ERROR_ACTION_STOP : BLOCK_ERROR_ACTION_REPORT;
452 break;
453 case BLOCKDEV_ON_ERROR_STOP:
454 action = BLOCK_ERROR_ACTION_STOP;
455 break;
456 case BLOCKDEV_ON_ERROR_REPORT:
457 action = BLOCK_ERROR_ACTION_REPORT;
458 break;
459 case BLOCKDEV_ON_ERROR_IGNORE:
460 action = BLOCK_ERROR_ACTION_IGNORE;
461 break;
462 default:
463 abort();
464 }
465 qapi_event_send_block_job_error(job->id,
466 is_read ? IO_OPERATION_TYPE_READ :
467 IO_OPERATION_TYPE_WRITE,
468 action, &error_abort);
469 if (action == BLOCK_ERROR_ACTION_STOP) {
470 /* make the pause user visible, which will be resumed from QMP. */
471 job->user_paused = true;
472 block_job_pause(job);
473 block_job_iostatus_set_err(job, error);
474 }
475 return action;
476 }
477
478 typedef struct {
479 BlockJob *job;
480 QEMUBH *bh;
481 AioContext *aio_context;
482 BlockJobDeferToMainLoopFn *fn;
483 void *opaque;
484 } BlockJobDeferToMainLoopData;
485
486 static void block_job_defer_to_main_loop_bh(void *opaque)
487 {
488 BlockJobDeferToMainLoopData *data = opaque;
489 AioContext *aio_context;
490
491 qemu_bh_delete(data->bh);
492
493 /* Prevent race with block_job_defer_to_main_loop() */
494 aio_context_acquire(data->aio_context);
495
496 /* Fetch BDS AioContext again, in case it has changed */
497 aio_context = blk_get_aio_context(data->job->blk);
498 aio_context_acquire(aio_context);
499
500 data->job->deferred_to_main_loop = false;
501 data->fn(data->job, data->opaque);
502
503 aio_context_release(aio_context);
504
505 aio_context_release(data->aio_context);
506
507 g_free(data);
508 }
509
510 void block_job_defer_to_main_loop(BlockJob *job,
511 BlockJobDeferToMainLoopFn *fn,
512 void *opaque)
513 {
514 BlockJobDeferToMainLoopData *data = g_malloc(sizeof(*data));
515 data->job = job;
516 data->bh = qemu_bh_new(block_job_defer_to_main_loop_bh, data);
517 data->aio_context = blk_get_aio_context(job->blk);
518 data->fn = fn;
519 data->opaque = opaque;
520 job->deferred_to_main_loop = true;
521
522 qemu_bh_schedule(data->bh);
523 }
524
525 BlockJobTxn *block_job_txn_new(void)
526 {
527 BlockJobTxn *txn = g_new0(BlockJobTxn, 1);
528 QLIST_INIT(&txn->jobs);
529 txn->refcnt = 1;
530 return txn;
531 }
532
533 static void block_job_txn_ref(BlockJobTxn *txn)
534 {
535 txn->refcnt++;
536 }
537
538 void block_job_txn_unref(BlockJobTxn *txn)
539 {
540 if (txn && --txn->refcnt == 0) {
541 g_free(txn);
542 }
543 }
544
545 void block_job_txn_add_job(BlockJobTxn *txn, BlockJob *job)
546 {
547 if (!txn) {
548 return;
549 }
550
551 assert(!job->txn);
552 job->txn = txn;
553
554 QLIST_INSERT_HEAD(&txn->jobs, job, txn_list);
555 block_job_txn_ref(txn);
556 }