]>
Commit | Line | Data |
---|---|---|
a76bab49 AL |
1 | /* |
2 | * QEMU aio implementation | |
3 | * | |
4 | * Copyright IBM, Corp. 2008 | |
5 | * | |
6 | * Authors: | |
7 | * Anthony Liguori <aliguori@us.ibm.com> | |
8 | * | |
9 | * This work is licensed under the terms of the GNU GPL, version 2. See | |
10 | * the COPYING file in the top-level directory. | |
11 | * | |
6b620ca3 PB |
12 | * Contributions after 2012-01-13 are licensed under the terms of the |
13 | * GNU GPL, version 2 or (at your option) any later version. | |
a76bab49 AL |
14 | */ |
15 | ||
16 | #include "qemu-common.h" | |
737e150e | 17 | #include "block/block.h" |
1de7afc9 PB |
18 | #include "qemu/queue.h" |
19 | #include "qemu/sockets.h" | |
a76bab49 | 20 | |
a76bab49 AL |
21 | struct AioHandler |
22 | { | |
cd9ba1eb | 23 | GPollFD pfd; |
a76bab49 AL |
24 | IOHandler *io_read; |
25 | IOHandler *io_write; | |
26 | AioFlushHandler *io_flush; | |
27 | int deleted; | |
28 | void *opaque; | |
72cf2d4f | 29 | QLIST_ENTRY(AioHandler) node; |
a76bab49 AL |
30 | }; |
31 | ||
a915f4bc | 32 | static AioHandler *find_aio_handler(AioContext *ctx, int fd) |
a76bab49 AL |
33 | { |
34 | AioHandler *node; | |
35 | ||
a915f4bc | 36 | QLIST_FOREACH(node, &ctx->aio_handlers, node) { |
cd9ba1eb | 37 | if (node->pfd.fd == fd) |
79d5ca56 AG |
38 | if (!node->deleted) |
39 | return node; | |
a76bab49 AL |
40 | } |
41 | ||
42 | return NULL; | |
43 | } | |
44 | ||
a915f4bc PB |
45 | void aio_set_fd_handler(AioContext *ctx, |
46 | int fd, | |
47 | IOHandler *io_read, | |
48 | IOHandler *io_write, | |
49 | AioFlushHandler *io_flush, | |
50 | void *opaque) | |
a76bab49 AL |
51 | { |
52 | AioHandler *node; | |
53 | ||
a915f4bc | 54 | node = find_aio_handler(ctx, fd); |
a76bab49 AL |
55 | |
56 | /* Are we deleting the fd handler? */ | |
57 | if (!io_read && !io_write) { | |
58 | if (node) { | |
e3713e00 PB |
59 | g_source_remove_poll(&ctx->source, &node->pfd); |
60 | ||
a76bab49 | 61 | /* If the lock is held, just mark the node as deleted */ |
cd9ba1eb | 62 | if (ctx->walking_handlers) { |
a76bab49 | 63 | node->deleted = 1; |
cd9ba1eb PB |
64 | node->pfd.revents = 0; |
65 | } else { | |
a76bab49 AL |
66 | /* Otherwise, delete it for real. We can't just mark it as |
67 | * deleted because deleted nodes are only cleaned up after | |
68 | * releasing the walking_handlers lock. | |
69 | */ | |
72cf2d4f | 70 | QLIST_REMOVE(node, node); |
7267c094 | 71 | g_free(node); |
a76bab49 AL |
72 | } |
73 | } | |
74 | } else { | |
75 | if (node == NULL) { | |
76 | /* Alloc and insert if it's not already there */ | |
7267c094 | 77 | node = g_malloc0(sizeof(AioHandler)); |
cd9ba1eb | 78 | node->pfd.fd = fd; |
a915f4bc | 79 | QLIST_INSERT_HEAD(&ctx->aio_handlers, node, node); |
e3713e00 PB |
80 | |
81 | g_source_add_poll(&ctx->source, &node->pfd); | |
a76bab49 AL |
82 | } |
83 | /* Update handler with latest information */ | |
84 | node->io_read = io_read; | |
85 | node->io_write = io_write; | |
86 | node->io_flush = io_flush; | |
87 | node->opaque = opaque; | |
cd9ba1eb PB |
88 | |
89 | node->pfd.events = (io_read ? G_IO_IN | G_IO_HUP : 0); | |
90 | node->pfd.events |= (io_write ? G_IO_OUT : 0); | |
a76bab49 | 91 | } |
7ed2b24c PB |
92 | |
93 | aio_notify(ctx); | |
9958c351 PB |
94 | } |
95 | ||
a915f4bc PB |
96 | void aio_set_event_notifier(AioContext *ctx, |
97 | EventNotifier *notifier, | |
98 | EventNotifierHandler *io_read, | |
99 | AioFlushEventNotifierHandler *io_flush) | |
a76bab49 | 100 | { |
a915f4bc PB |
101 | aio_set_fd_handler(ctx, event_notifier_get_fd(notifier), |
102 | (IOHandler *)io_read, NULL, | |
103 | (AioFlushHandler *)io_flush, notifier); | |
a76bab49 AL |
104 | } |
105 | ||
cd9ba1eb PB |
106 | bool aio_pending(AioContext *ctx) |
107 | { | |
108 | AioHandler *node; | |
109 | ||
110 | QLIST_FOREACH(node, &ctx->aio_handlers, node) { | |
111 | int revents; | |
112 | ||
113 | /* | |
114 | * FIXME: right now we cannot get G_IO_HUP and G_IO_ERR because | |
115 | * main-loop.c is still select based (due to the slirp legacy). | |
116 | * If main-loop.c ever switches to poll, G_IO_ERR should be | |
117 | * tested too. Dispatching G_IO_ERR to both handlers should be | |
118 | * okay, since handlers need to be ready for spurious wakeups. | |
119 | */ | |
120 | revents = node->pfd.revents & node->pfd.events; | |
121 | if (revents & (G_IO_IN | G_IO_HUP | G_IO_ERR) && node->io_read) { | |
122 | return true; | |
123 | } | |
124 | if (revents & (G_IO_OUT | G_IO_ERR) && node->io_write) { | |
125 | return true; | |
126 | } | |
127 | } | |
128 | ||
129 | return false; | |
130 | } | |
131 | ||
d0c8d2c0 | 132 | static bool aio_dispatch(AioContext *ctx) |
a76bab49 | 133 | { |
9eb0bfca | 134 | AioHandler *node; |
d0c8d2c0 | 135 | bool progress = false; |
7c0628b2 | 136 | |
cd9ba1eb | 137 | /* |
cd9ba1eb PB |
138 | * We have to walk very carefully in case qemu_aio_set_fd_handler is |
139 | * called while we're walking. | |
140 | */ | |
141 | node = QLIST_FIRST(&ctx->aio_handlers); | |
142 | while (node) { | |
143 | AioHandler *tmp; | |
144 | int revents; | |
145 | ||
146 | ctx->walking_handlers++; | |
147 | ||
148 | revents = node->pfd.revents & node->pfd.events; | |
149 | node->pfd.revents = 0; | |
150 | ||
151 | /* See comment in aio_pending. */ | |
d0c8d2c0 SH |
152 | if (!node->deleted && |
153 | (revents & (G_IO_IN | G_IO_HUP | G_IO_ERR)) && | |
154 | node->io_read) { | |
cd9ba1eb PB |
155 | node->io_read(node->opaque); |
156 | progress = true; | |
157 | } | |
d0c8d2c0 SH |
158 | if (!node->deleted && |
159 | (revents & (G_IO_OUT | G_IO_ERR)) && | |
160 | node->io_write) { | |
cd9ba1eb PB |
161 | node->io_write(node->opaque); |
162 | progress = true; | |
163 | } | |
164 | ||
165 | tmp = node; | |
166 | node = QLIST_NEXT(node, node); | |
167 | ||
168 | ctx->walking_handlers--; | |
169 | ||
170 | if (!ctx->walking_handlers && tmp->deleted) { | |
171 | QLIST_REMOVE(tmp, node); | |
172 | g_free(tmp); | |
173 | } | |
174 | } | |
d0c8d2c0 SH |
175 | return progress; |
176 | } | |
177 | ||
178 | bool aio_poll(AioContext *ctx, bool blocking) | |
179 | { | |
180 | static struct timeval tv0; | |
181 | AioHandler *node; | |
182 | fd_set rdfds, wrfds; | |
183 | int max_fd = -1; | |
184 | int ret; | |
185 | bool busy, progress; | |
186 | ||
187 | progress = false; | |
188 | ||
189 | /* | |
190 | * If there are callbacks left that have been queued, we need to call them. | |
191 | * Do not call select in this case, because it is possible that the caller | |
192 | * does not need a complete flush (as is the case for qemu_aio_wait loops). | |
193 | */ | |
194 | if (aio_bh_poll(ctx)) { | |
195 | blocking = false; | |
196 | progress = true; | |
197 | } | |
198 | ||
199 | if (aio_dispatch(ctx)) { | |
200 | progress = true; | |
201 | } | |
cd9ba1eb | 202 | |
7c0628b2 | 203 | if (progress && !blocking) { |
bcdc1857 | 204 | return true; |
bafbd6a1 | 205 | } |
8febfa26 | 206 | |
a915f4bc | 207 | ctx->walking_handlers++; |
a76bab49 | 208 | |
9eb0bfca PB |
209 | FD_ZERO(&rdfds); |
210 | FD_ZERO(&wrfds); | |
a76bab49 | 211 | |
9eb0bfca PB |
212 | /* fill fd sets */ |
213 | busy = false; | |
a915f4bc | 214 | QLIST_FOREACH(node, &ctx->aio_handlers, node) { |
9eb0bfca PB |
215 | /* If there aren't pending AIO operations, don't invoke callbacks. |
216 | * Otherwise, if there are no AIO requests, qemu_aio_wait() would | |
217 | * wait indefinitely. | |
218 | */ | |
4231c88d | 219 | if (!node->deleted && node->io_flush) { |
9eb0bfca PB |
220 | if (node->io_flush(node->opaque) == 0) { |
221 | continue; | |
a76bab49 | 222 | } |
9eb0bfca PB |
223 | busy = true; |
224 | } | |
225 | if (!node->deleted && node->io_read) { | |
cd9ba1eb PB |
226 | FD_SET(node->pfd.fd, &rdfds); |
227 | max_fd = MAX(max_fd, node->pfd.fd + 1); | |
a76bab49 | 228 | } |
9eb0bfca | 229 | if (!node->deleted && node->io_write) { |
cd9ba1eb PB |
230 | FD_SET(node->pfd.fd, &wrfds); |
231 | max_fd = MAX(max_fd, node->pfd.fd + 1); | |
9eb0bfca PB |
232 | } |
233 | } | |
a76bab49 | 234 | |
a915f4bc | 235 | ctx->walking_handlers--; |
a76bab49 | 236 | |
9eb0bfca PB |
237 | /* No AIO operations? Get us out of here */ |
238 | if (!busy) { | |
7c0628b2 | 239 | return progress; |
9eb0bfca | 240 | } |
a76bab49 | 241 | |
9eb0bfca | 242 | /* wait until next event */ |
7c0628b2 | 243 | ret = select(max_fd, &rdfds, &wrfds, NULL, blocking ? NULL : &tv0); |
9eb0bfca PB |
244 | |
245 | /* if we have any readable fds, dispatch event */ | |
246 | if (ret > 0) { | |
9eb0bfca PB |
247 | /* we have to walk very carefully in case |
248 | * qemu_aio_set_fd_handler is called while we're walking */ | |
a915f4bc | 249 | node = QLIST_FIRST(&ctx->aio_handlers); |
9eb0bfca PB |
250 | while (node) { |
251 | AioHandler *tmp; | |
252 | ||
a915f4bc | 253 | ctx->walking_handlers++; |
2db2bfc0 | 254 | |
9eb0bfca | 255 | if (!node->deleted && |
cd9ba1eb | 256 | FD_ISSET(node->pfd.fd, &rdfds) && |
9eb0bfca PB |
257 | node->io_read) { |
258 | node->io_read(node->opaque); | |
cd9ba1eb | 259 | progress = true; |
9eb0bfca PB |
260 | } |
261 | if (!node->deleted && | |
cd9ba1eb | 262 | FD_ISSET(node->pfd.fd, &wrfds) && |
9eb0bfca PB |
263 | node->io_write) { |
264 | node->io_write(node->opaque); | |
cd9ba1eb | 265 | progress = true; |
a76bab49 AL |
266 | } |
267 | ||
9eb0bfca PB |
268 | tmp = node; |
269 | node = QLIST_NEXT(node, node); | |
270 | ||
a915f4bc | 271 | ctx->walking_handlers--; |
2db2bfc0 | 272 | |
a915f4bc | 273 | if (!ctx->walking_handlers && tmp->deleted) { |
9eb0bfca PB |
274 | QLIST_REMOVE(tmp, node); |
275 | g_free(tmp); | |
276 | } | |
a76bab49 | 277 | } |
9eb0bfca | 278 | } |
bcdc1857 | 279 | |
2ea9b58f KW |
280 | assert(progress || busy); |
281 | return true; | |
a76bab49 | 282 | } |