Replace all setjmp()/longjmp() with sigsetjmp()/siglongjmp()
[qemu/pbrook.git] / block / linux-aio.c
blobee0f8d10c924f08efc269ccfcc53258b829351c8
1 /*
2 * Linux native AIO support.
4 * Copyright (C) 2009 IBM, Corp.
5 * Copyright (C) 2009 Red Hat, Inc.
7 * This work is licensed under the terms of the GNU GPL, version 2 or later.
8 * See the COPYING file in the top-level directory.
9 */
10 #include "qemu-common.h"
11 #include "block/aio.h"
12 #include "qemu/queue.h"
13 #include "block/raw-aio.h"
14 #include "qemu/event_notifier.h"
16 #include <libaio.h>
19 * Queue size (per-device).
21 * XXX: eventually we need to communicate this to the guest and/or make it
22 * tunable by the guest. If we get more outstanding requests at a time
23 * than this we will get EAGAIN from io_submit which is communicated to
24 * the guest as an I/O error.
26 #define MAX_EVENTS 128
28 struct qemu_laiocb {
29 BlockDriverAIOCB common;
30 struct qemu_laio_state *ctx;
31 struct iocb iocb;
32 ssize_t ret;
33 size_t nbytes;
34 QEMUIOVector *qiov;
35 bool is_read;
36 QLIST_ENTRY(qemu_laiocb) node;
39 struct qemu_laio_state {
40 io_context_t ctx;
41 EventNotifier e;
42 int count;
45 static inline ssize_t io_event_ret(struct io_event *ev)
47 return (ssize_t)(((uint64_t)ev->res2 << 32) | ev->res);
51 * Completes an AIO request (calls the callback and frees the ACB).
53 static void qemu_laio_process_completion(struct qemu_laio_state *s,
54 struct qemu_laiocb *laiocb)
56 int ret;
58 s->count--;
60 ret = laiocb->ret;
61 if (ret != -ECANCELED) {
62 if (ret == laiocb->nbytes) {
63 ret = 0;
64 } else if (ret >= 0) {
65 /* Short reads mean EOF, pad with zeros. */
66 if (laiocb->is_read) {
67 qemu_iovec_memset(laiocb->qiov, ret, 0,
68 laiocb->qiov->size - ret);
69 } else {
70 ret = -EINVAL;
74 laiocb->common.cb(laiocb->common.opaque, ret);
77 qemu_aio_release(laiocb);
80 static void qemu_laio_completion_cb(EventNotifier *e)
82 struct qemu_laio_state *s = container_of(e, struct qemu_laio_state, e);
84 while (event_notifier_test_and_clear(&s->e)) {
85 struct io_event events[MAX_EVENTS];
86 struct timespec ts = { 0 };
87 int nevents, i;
89 do {
90 nevents = io_getevents(s->ctx, MAX_EVENTS, MAX_EVENTS, events, &ts);
91 } while (nevents == -EINTR);
93 for (i = 0; i < nevents; i++) {
94 struct iocb *iocb = events[i].obj;
95 struct qemu_laiocb *laiocb =
96 container_of(iocb, struct qemu_laiocb, iocb);
98 laiocb->ret = io_event_ret(&events[i]);
99 qemu_laio_process_completion(s, laiocb);
104 static int qemu_laio_flush_cb(EventNotifier *e)
106 struct qemu_laio_state *s = container_of(e, struct qemu_laio_state, e);
108 return (s->count > 0) ? 1 : 0;
111 static void laio_cancel(BlockDriverAIOCB *blockacb)
113 struct qemu_laiocb *laiocb = (struct qemu_laiocb *)blockacb;
114 struct io_event event;
115 int ret;
117 if (laiocb->ret != -EINPROGRESS)
118 return;
121 * Note that as of Linux 2.6.31 neither the block device code nor any
122 * filesystem implements cancellation of AIO request.
123 * Thus the polling loop below is the normal code path.
125 ret = io_cancel(laiocb->ctx->ctx, &laiocb->iocb, &event);
126 if (ret == 0) {
127 laiocb->ret = -ECANCELED;
128 return;
132 * We have to wait for the iocb to finish.
134 * The only way to get the iocb status update is by polling the io context.
135 * We might be able to do this slightly more optimal by removing the
136 * O_NONBLOCK flag.
138 while (laiocb->ret == -EINPROGRESS) {
139 qemu_laio_completion_cb(&laiocb->ctx->e);
143 static const AIOCBInfo laio_aiocb_info = {
144 .aiocb_size = sizeof(struct qemu_laiocb),
145 .cancel = laio_cancel,
148 BlockDriverAIOCB *laio_submit(BlockDriverState *bs, void *aio_ctx, int fd,
149 int64_t sector_num, QEMUIOVector *qiov, int nb_sectors,
150 BlockDriverCompletionFunc *cb, void *opaque, int type)
152 struct qemu_laio_state *s = aio_ctx;
153 struct qemu_laiocb *laiocb;
154 struct iocb *iocbs;
155 off_t offset = sector_num * 512;
157 laiocb = qemu_aio_get(&laio_aiocb_info, bs, cb, opaque);
158 laiocb->nbytes = nb_sectors * 512;
159 laiocb->ctx = s;
160 laiocb->ret = -EINPROGRESS;
161 laiocb->is_read = (type == QEMU_AIO_READ);
162 laiocb->qiov = qiov;
164 iocbs = &laiocb->iocb;
166 switch (type) {
167 case QEMU_AIO_WRITE:
168 io_prep_pwritev(iocbs, fd, qiov->iov, qiov->niov, offset);
169 break;
170 case QEMU_AIO_READ:
171 io_prep_preadv(iocbs, fd, qiov->iov, qiov->niov, offset);
172 break;
173 /* Currently Linux kernel does not support other operations */
174 default:
175 fprintf(stderr, "%s: invalid AIO request type 0x%x.\n",
176 __func__, type);
177 goto out_free_aiocb;
179 io_set_eventfd(&laiocb->iocb, event_notifier_get_fd(&s->e));
180 s->count++;
182 if (io_submit(s->ctx, 1, &iocbs) < 0)
183 goto out_dec_count;
184 return &laiocb->common;
186 out_dec_count:
187 s->count--;
188 out_free_aiocb:
189 qemu_aio_release(laiocb);
190 return NULL;
193 void *laio_init(void)
195 struct qemu_laio_state *s;
197 s = g_malloc0(sizeof(*s));
198 if (event_notifier_init(&s->e, false) < 0) {
199 goto out_free_state;
202 if (io_setup(MAX_EVENTS, &s->ctx) != 0) {
203 goto out_close_efd;
206 qemu_aio_set_event_notifier(&s->e, qemu_laio_completion_cb,
207 qemu_laio_flush_cb);
209 return s;
211 out_close_efd:
212 event_notifier_cleanup(&s->e);
213 out_free_state:
214 g_free(s);
215 return NULL;