]>
Commit | Line | Data |
---|---|---|
5c6c3a6c CH |
1 | /* |
2 | * Linux native AIO support. | |
3 | * | |
4 | * Copyright (C) 2009 IBM, Corp. | |
5 | * Copyright (C) 2009 Red Hat, Inc. | |
6 | * | |
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" | |
737e150e | 11 | #include "block/aio.h" |
1de7afc9 | 12 | #include "qemu/queue.h" |
9f8540ec | 13 | #include "block/raw-aio.h" |
1de7afc9 | 14 | #include "qemu/event_notifier.h" |
5c6c3a6c | 15 | |
5c6c3a6c CH |
16 | #include <libaio.h> |
17 | ||
18 | /* | |
19 | * Queue size (per-device). | |
20 | * | |
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. | |
25 | */ | |
26 | #define MAX_EVENTS 128 | |
27 | ||
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; | |
b161e2e4 KW |
34 | QEMUIOVector *qiov; |
35 | bool is_read; | |
db0ffc24 | 36 | QLIST_ENTRY(qemu_laiocb) node; |
5c6c3a6c CH |
37 | }; |
38 | ||
39 | struct qemu_laio_state { | |
40 | io_context_t ctx; | |
c90caf25 | 41 | EventNotifier e; |
5c6c3a6c CH |
42 | int count; |
43 | }; | |
44 | ||
45 | static inline ssize_t io_event_ret(struct io_event *ev) | |
46 | { | |
47 | return (ssize_t)(((uint64_t)ev->res2 << 32) | ev->res); | |
48 | } | |
49 | ||
db0ffc24 KW |
50 | /* |
51 | * Completes an AIO request (calls the callback and frees the ACB). | |
db0ffc24 KW |
52 | */ |
53 | static 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) { | |
b161e2e4 | 62 | if (ret == laiocb->nbytes) { |
db0ffc24 | 63 | ret = 0; |
b161e2e4 KW |
64 | } else if (ret >= 0) { |
65 | /* Short reads mean EOF, pad with zeros. */ | |
66 | if (laiocb->is_read) { | |
3d9b4925 MT |
67 | qemu_iovec_memset(laiocb->qiov, ret, 0, |
68 | laiocb->qiov->size - ret); | |
b161e2e4 KW |
69 | } else { |
70 | ret = -EINVAL; | |
71 | } | |
72 | } | |
db0ffc24 KW |
73 | |
74 | laiocb->common.cb(laiocb->common.opaque, ret); | |
75 | } | |
76 | ||
77 | qemu_aio_release(laiocb); | |
78 | } | |
79 | ||
c90caf25 | 80 | static void qemu_laio_completion_cb(EventNotifier *e) |
5c6c3a6c | 81 | { |
c90caf25 | 82 | struct qemu_laio_state *s = container_of(e, struct qemu_laio_state, e); |
5c6c3a6c | 83 | |
c90caf25 | 84 | while (event_notifier_test_and_clear(&s->e)) { |
5c6c3a6c | 85 | struct io_event events[MAX_EVENTS]; |
5c6c3a6c CH |
86 | struct timespec ts = { 0 }; |
87 | int nevents, i; | |
88 | ||
89 | do { | |
c90caf25 | 90 | nevents = io_getevents(s->ctx, MAX_EVENTS, MAX_EVENTS, events, &ts); |
5c6c3a6c CH |
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 | ||
db0ffc24 | 98 | laiocb->ret = io_event_ret(&events[i]); |
384acbf4 | 99 | qemu_laio_process_completion(s, laiocb); |
5c6c3a6c CH |
100 | } |
101 | } | |
102 | } | |
103 | ||
c90caf25 | 104 | static int qemu_laio_flush_cb(EventNotifier *e) |
5c6c3a6c | 105 | { |
c90caf25 | 106 | struct qemu_laio_state *s = container_of(e, struct qemu_laio_state, e); |
5c6c3a6c CH |
107 | |
108 | return (s->count > 0) ? 1 : 0; | |
109 | } | |
110 | ||
111 | static 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 | * 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. | |
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 | * We have to wait for the iocb to finish. | |
133 | * | |
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. | |
137 | */ | |
c90caf25 PB |
138 | while (laiocb->ret == -EINPROGRESS) { |
139 | qemu_laio_completion_cb(&laiocb->ctx->e); | |
140 | } | |
5c6c3a6c CH |
141 | } |
142 | ||
d7331bed | 143 | static const AIOCBInfo laio_aiocb_info = { |
5c6c3a6c CH |
144 | .aiocb_size = sizeof(struct qemu_laiocb), |
145 | .cancel = laio_cancel, | |
146 | }; | |
147 | ||
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) | |
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 | ||
d7331bed | 157 | laiocb = qemu_aio_get(&laio_aiocb_info, bs, cb, opaque); |
5c6c3a6c CH |
158 | laiocb->nbytes = nb_sectors * 512; |
159 | laiocb->ctx = s; | |
160 | laiocb->ret = -EINPROGRESS; | |
b161e2e4 KW |
161 | laiocb->is_read = (type == QEMU_AIO_READ); |
162 | laiocb->qiov = qiov; | |
5c6c3a6c CH |
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; | |
c30e624d | 173 | /* Currently Linux kernel does not support other operations */ |
5c6c3a6c CH |
174 | default: |
175 | fprintf(stderr, "%s: invalid AIO request type 0x%x.\n", | |
176 | __func__, type); | |
177 | goto out_free_aiocb; | |
178 | } | |
c90caf25 | 179 | io_set_eventfd(&laiocb->iocb, event_notifier_get_fd(&s->e)); |
5c6c3a6c CH |
180 | s->count++; |
181 | ||
182 | if (io_submit(s->ctx, 1, &iocbs) < 0) | |
183 | goto out_dec_count; | |
184 | return &laiocb->common; | |
185 | ||
5c6c3a6c CH |
186 | out_dec_count: |
187 | s->count--; | |
449c184e KW |
188 | out_free_aiocb: |
189 | qemu_aio_release(laiocb); | |
5c6c3a6c CH |
190 | return NULL; |
191 | } | |
192 | ||
193 | void *laio_init(void) | |
194 | { | |
195 | struct qemu_laio_state *s; | |
196 | ||
7267c094 | 197 | s = g_malloc0(sizeof(*s)); |
c90caf25 | 198 | if (event_notifier_init(&s->e, false) < 0) { |
5c6c3a6c | 199 | goto out_free_state; |
c90caf25 | 200 | } |
5c6c3a6c | 201 | |
c90caf25 | 202 | if (io_setup(MAX_EVENTS, &s->ctx) != 0) { |
5c6c3a6c | 203 | goto out_close_efd; |
c90caf25 | 204 | } |
5c6c3a6c | 205 | |
c90caf25 PB |
206 | qemu_aio_set_event_notifier(&s->e, qemu_laio_completion_cb, |
207 | qemu_laio_flush_cb); | |
5c6c3a6c CH |
208 | |
209 | return s; | |
210 | ||
211 | out_close_efd: | |
c90caf25 | 212 | event_notifier_cleanup(&s->e); |
5c6c3a6c | 213 | out_free_state: |
7267c094 | 214 | g_free(s); |
5c6c3a6c CH |
215 | return NULL; |
216 | } |