]>
Commit | Line | Data |
---|---|---|
a76bab49 AL |
1 | /* |
2 | * QEMU aio implementation | |
3 | * | |
4 | * Copyright IBM, Corp. 2008 | |
5 | * | |
6 | * Authors: | |
7 | * Anthony Liguori <[email protected]> | |
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" | |
17 | #include "block.h" | |
72cf2d4f | 18 | #include "qemu-queue.h" |
a76bab49 AL |
19 | #include "qemu_socket.h" |
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) { | |
59 | /* If the lock is held, just mark the node as deleted */ | |
cd9ba1eb | 60 | if (ctx->walking_handlers) { |
a76bab49 | 61 | node->deleted = 1; |
cd9ba1eb PB |
62 | node->pfd.revents = 0; |
63 | } else { | |
a76bab49 AL |
64 | /* Otherwise, delete it for real. We can't just mark it as |
65 | * deleted because deleted nodes are only cleaned up after | |
66 | * releasing the walking_handlers lock. | |
67 | */ | |
72cf2d4f | 68 | QLIST_REMOVE(node, node); |
7267c094 | 69 | g_free(node); |
a76bab49 AL |
70 | } |
71 | } | |
72 | } else { | |
73 | if (node == NULL) { | |
74 | /* Alloc and insert if it's not already there */ | |
7267c094 | 75 | node = g_malloc0(sizeof(AioHandler)); |
cd9ba1eb | 76 | node->pfd.fd = fd; |
a915f4bc | 77 | QLIST_INSERT_HEAD(&ctx->aio_handlers, node, node); |
a76bab49 AL |
78 | } |
79 | /* Update handler with latest information */ | |
80 | node->io_read = io_read; | |
81 | node->io_write = io_write; | |
82 | node->io_flush = io_flush; | |
83 | node->opaque = opaque; | |
cd9ba1eb PB |
84 | |
85 | node->pfd.events = (io_read ? G_IO_IN | G_IO_HUP : 0); | |
86 | node->pfd.events |= (io_write ? G_IO_OUT : 0); | |
a76bab49 | 87 | } |
9958c351 PB |
88 | } |
89 | ||
a915f4bc PB |
90 | void aio_set_event_notifier(AioContext *ctx, |
91 | EventNotifier *notifier, | |
92 | EventNotifierHandler *io_read, | |
93 | AioFlushEventNotifierHandler *io_flush) | |
a76bab49 | 94 | { |
a915f4bc PB |
95 | aio_set_fd_handler(ctx, event_notifier_get_fd(notifier), |
96 | (IOHandler *)io_read, NULL, | |
97 | (AioFlushHandler *)io_flush, notifier); | |
a76bab49 AL |
98 | } |
99 | ||
cd9ba1eb PB |
100 | bool aio_pending(AioContext *ctx) |
101 | { | |
102 | AioHandler *node; | |
103 | ||
104 | QLIST_FOREACH(node, &ctx->aio_handlers, node) { | |
105 | int revents; | |
106 | ||
107 | /* | |
108 | * FIXME: right now we cannot get G_IO_HUP and G_IO_ERR because | |
109 | * main-loop.c is still select based (due to the slirp legacy). | |
110 | * If main-loop.c ever switches to poll, G_IO_ERR should be | |
111 | * tested too. Dispatching G_IO_ERR to both handlers should be | |
112 | * okay, since handlers need to be ready for spurious wakeups. | |
113 | */ | |
114 | revents = node->pfd.revents & node->pfd.events; | |
115 | if (revents & (G_IO_IN | G_IO_HUP | G_IO_ERR) && node->io_read) { | |
116 | return true; | |
117 | } | |
118 | if (revents & (G_IO_OUT | G_IO_ERR) && node->io_write) { | |
119 | return true; | |
120 | } | |
121 | } | |
122 | ||
123 | return false; | |
124 | } | |
125 | ||
7c0628b2 | 126 | bool aio_poll(AioContext *ctx, bool blocking) |
a76bab49 | 127 | { |
7c0628b2 | 128 | static struct timeval tv0; |
9eb0bfca PB |
129 | AioHandler *node; |
130 | fd_set rdfds, wrfds; | |
131 | int max_fd = -1; | |
a76bab49 | 132 | int ret; |
7c0628b2 PB |
133 | bool busy, progress; |
134 | ||
135 | progress = false; | |
a76bab49 | 136 | |
8febfa26 KW |
137 | /* |
138 | * If there are callbacks left that have been queued, we need to call then. | |
bcdc1857 PB |
139 | * Do not call select in this case, because it is possible that the caller |
140 | * does not need a complete flush (as is the case for qemu_aio_wait loops). | |
8febfa26 | 141 | */ |
a915f4bc | 142 | if (aio_bh_poll(ctx)) { |
7c0628b2 PB |
143 | blocking = false; |
144 | progress = true; | |
145 | } | |
146 | ||
cd9ba1eb PB |
147 | /* |
148 | * Then dispatch any pending callbacks from the GSource. | |
149 | * | |
150 | * We have to walk very carefully in case qemu_aio_set_fd_handler is | |
151 | * called while we're walking. | |
152 | */ | |
153 | node = QLIST_FIRST(&ctx->aio_handlers); | |
154 | while (node) { | |
155 | AioHandler *tmp; | |
156 | int revents; | |
157 | ||
158 | ctx->walking_handlers++; | |
159 | ||
160 | revents = node->pfd.revents & node->pfd.events; | |
161 | node->pfd.revents = 0; | |
162 | ||
163 | /* See comment in aio_pending. */ | |
164 | if (revents & (G_IO_IN | G_IO_HUP | G_IO_ERR) && node->io_read) { | |
165 | node->io_read(node->opaque); | |
166 | progress = true; | |
167 | } | |
168 | if (revents & (G_IO_OUT | G_IO_ERR) && node->io_write) { | |
169 | node->io_write(node->opaque); | |
170 | progress = true; | |
171 | } | |
172 | ||
173 | tmp = node; | |
174 | node = QLIST_NEXT(node, node); | |
175 | ||
176 | ctx->walking_handlers--; | |
177 | ||
178 | if (!ctx->walking_handlers && tmp->deleted) { | |
179 | QLIST_REMOVE(tmp, node); | |
180 | g_free(tmp); | |
181 | } | |
182 | } | |
183 | ||
7c0628b2 | 184 | if (progress && !blocking) { |
bcdc1857 | 185 | return true; |
bafbd6a1 | 186 | } |
8febfa26 | 187 | |
a915f4bc | 188 | ctx->walking_handlers++; |
a76bab49 | 189 | |
9eb0bfca PB |
190 | FD_ZERO(&rdfds); |
191 | FD_ZERO(&wrfds); | |
a76bab49 | 192 | |
9eb0bfca PB |
193 | /* fill fd sets */ |
194 | busy = false; | |
a915f4bc | 195 | QLIST_FOREACH(node, &ctx->aio_handlers, node) { |
9eb0bfca PB |
196 | /* If there aren't pending AIO operations, don't invoke callbacks. |
197 | * Otherwise, if there are no AIO requests, qemu_aio_wait() would | |
198 | * wait indefinitely. | |
199 | */ | |
4231c88d | 200 | if (!node->deleted && node->io_flush) { |
9eb0bfca PB |
201 | if (node->io_flush(node->opaque) == 0) { |
202 | continue; | |
a76bab49 | 203 | } |
9eb0bfca PB |
204 | busy = true; |
205 | } | |
206 | if (!node->deleted && node->io_read) { | |
cd9ba1eb PB |
207 | FD_SET(node->pfd.fd, &rdfds); |
208 | max_fd = MAX(max_fd, node->pfd.fd + 1); | |
a76bab49 | 209 | } |
9eb0bfca | 210 | if (!node->deleted && node->io_write) { |
cd9ba1eb PB |
211 | FD_SET(node->pfd.fd, &wrfds); |
212 | max_fd = MAX(max_fd, node->pfd.fd + 1); | |
9eb0bfca PB |
213 | } |
214 | } | |
a76bab49 | 215 | |
a915f4bc | 216 | ctx->walking_handlers--; |
a76bab49 | 217 | |
9eb0bfca PB |
218 | /* No AIO operations? Get us out of here */ |
219 | if (!busy) { | |
7c0628b2 | 220 | return progress; |
9eb0bfca | 221 | } |
a76bab49 | 222 | |
9eb0bfca | 223 | /* wait until next event */ |
7c0628b2 | 224 | ret = select(max_fd, &rdfds, &wrfds, NULL, blocking ? NULL : &tv0); |
9eb0bfca PB |
225 | |
226 | /* if we have any readable fds, dispatch event */ | |
227 | if (ret > 0) { | |
9eb0bfca PB |
228 | /* we have to walk very carefully in case |
229 | * qemu_aio_set_fd_handler is called while we're walking */ | |
a915f4bc | 230 | node = QLIST_FIRST(&ctx->aio_handlers); |
9eb0bfca PB |
231 | while (node) { |
232 | AioHandler *tmp; | |
233 | ||
a915f4bc | 234 | ctx->walking_handlers++; |
2db2bfc0 | 235 | |
9eb0bfca | 236 | if (!node->deleted && |
cd9ba1eb | 237 | FD_ISSET(node->pfd.fd, &rdfds) && |
9eb0bfca PB |
238 | node->io_read) { |
239 | node->io_read(node->opaque); | |
cd9ba1eb | 240 | progress = true; |
9eb0bfca PB |
241 | } |
242 | if (!node->deleted && | |
cd9ba1eb | 243 | FD_ISSET(node->pfd.fd, &wrfds) && |
9eb0bfca PB |
244 | node->io_write) { |
245 | node->io_write(node->opaque); | |
cd9ba1eb | 246 | progress = true; |
a76bab49 AL |
247 | } |
248 | ||
9eb0bfca PB |
249 | tmp = node; |
250 | node = QLIST_NEXT(node, node); | |
251 | ||
a915f4bc | 252 | ctx->walking_handlers--; |
2db2bfc0 | 253 | |
a915f4bc | 254 | if (!ctx->walking_handlers && tmp->deleted) { |
9eb0bfca PB |
255 | QLIST_REMOVE(tmp, node); |
256 | g_free(tmp); | |
257 | } | |
a76bab49 | 258 | } |
9eb0bfca | 259 | } |
bcdc1857 | 260 | |
7c0628b2 | 261 | return progress; |
a76bab49 | 262 | } |