1
2
3
4
5
6
7
8
9
10#include "qemu-common.h"
11#include "qemu-aio.h"
12#include "qemu-queue.h"
13#include "block/raw-aio.h"
14#include "event_notifier.h"
15
16#include <libaio.h>
17
18
19
20
21
22
23
24
25
26#define MAX_EVENTS 128
27
28struct 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;
37};
38
39struct qemu_laio_state {
40 io_context_t ctx;
41 EventNotifier e;
42 int count;
43};
44
45static inline ssize_t io_event_ret(struct io_event *ev)
46{
47 return (ssize_t)(((uint64_t)ev->res2 << 32) | ev->res);
48}
49
50
51
52
53static void qemu_laio_process_completion(struct qemu_laio_state *s,
54 struct qemu_laiocb *laiocb)
55{
56 int ret;
57
58 s->count--;
59
60 ret = laiocb->ret;
61 if (ret != -ECANCELED) {
62 if (ret == laiocb->nbytes) {
63 ret = 0;
64 } else if (ret >= 0) {
65
66 if (laiocb->is_read) {
67 qemu_iovec_memset(laiocb->qiov, ret, 0,
68 laiocb->qiov->size - ret);
69 } else {
70 ret = -EINVAL;
71 }
72 }
73
74 laiocb->common.cb(laiocb->common.opaque, ret);
75 }
76
77 qemu_aio_release(laiocb);
78}
79
80static void qemu_laio_completion_cb(EventNotifier *e)
81{
82 struct qemu_laio_state *s = container_of(e, struct qemu_laio_state, e);
83
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;
88
89 do {
90 nevents = io_getevents(s->ctx, MAX_EVENTS, MAX_EVENTS, events, &ts);
91 } while (nevents == -EINTR);
92
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);
97
98 laiocb->ret = io_event_ret(&events[i]);
99 qemu_laio_process_completion(s, laiocb);
100 }
101 }
102}
103
104static int qemu_laio_flush_cb(EventNotifier *e)
105{
106 struct qemu_laio_state *s = container_of(e, struct qemu_laio_state, e);
107
108 return (s->count > 0) ? 1 : 0;
109}
110
111static void laio_cancel(BlockDriverAIOCB *blockacb)
112{
113 struct qemu_laiocb *laiocb = (struct qemu_laiocb *)blockacb;
114 struct io_event event;
115 int ret;
116
117 if (laiocb->ret != -EINPROGRESS)
118 return;
119
120
121
122
123
124
125 ret = io_cancel(laiocb->ctx->ctx, &laiocb->iocb, &event);
126 if (ret == 0) {
127 laiocb->ret = -ECANCELED;
128 return;
129 }
130
131
132
133
134
135
136
137
138 while (laiocb->ret == -EINPROGRESS) {
139 qemu_laio_completion_cb(&laiocb->ctx->e);
140 }
141}
142
143static const AIOCBInfo laio_aiocb_info = {
144 .aiocb_size = sizeof(struct qemu_laiocb),
145 .cancel = laio_cancel,
146};
147
148BlockDriverAIOCB *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)
151{
152 struct qemu_laio_state *s = aio_ctx;
153 struct qemu_laiocb *laiocb;
154 struct iocb *iocbs;
155 off_t offset = sector_num * 512;
156
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;
163
164 iocbs = &laiocb->iocb;
165
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
174 default:
175 fprintf(stderr, "%s: invalid AIO request type 0x%x.\n",
176 __func__, type);
177 goto out_free_aiocb;
178 }
179 io_set_eventfd(&laiocb->iocb, event_notifier_get_fd(&s->e));
180 s->count++;
181
182 if (io_submit(s->ctx, 1, &iocbs) < 0)
183 goto out_dec_count;
184 return &laiocb->common;
185
186out_dec_count:
187 s->count--;
188out_free_aiocb:
189 qemu_aio_release(laiocb);
190 return NULL;
191}
192
193void *laio_init(void)
194{
195 struct qemu_laio_state *s;
196
197 s = g_malloc0(sizeof(*s));
198 if (event_notifier_init(&s->e, false) < 0) {
199 goto out_free_state;
200 }
201
202 if (io_setup(MAX_EVENTS, &s->ctx) != 0) {
203 goto out_close_efd;
204 }
205
206 qemu_aio_set_event_notifier(&s->e, qemu_laio_completion_cb,
207 qemu_laio_flush_cb);
208
209 return s;
210
211out_close_efd:
212 event_notifier_cleanup(&s->e);
213out_free_state:
214 g_free(s);
215 return NULL;
216}
217