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