blob: 65b26073f023b8a9d016c0e0ee67ca511dcc4e4a [file] [log] [blame]
aliguoria76bab42008-09-22 19:17:18 +00001/*
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 *
Paolo Bonzini6b620ca2012-01-13 17:44:23 +010012 * Contributions after 2012-01-13 are licensed under the terms of the
13 * GNU GPL, version 2 or (at your option) any later version.
aliguoria76bab42008-09-22 19:17:18 +000014 */
15
16#include "qemu-common.h"
17#include "block.h"
Blue Swirl72cf2d42009-09-12 07:36:22 +000018#include "qemu-queue.h"
aliguoria76bab42008-09-22 19:17:18 +000019#include "qemu_socket.h"
20
aliguoria76bab42008-09-22 19:17:18 +000021struct AioHandler
22{
Paolo Bonzinicd9ba1e2012-09-24 14:57:22 +020023 GPollFD pfd;
aliguoria76bab42008-09-22 19:17:18 +000024 IOHandler *io_read;
25 IOHandler *io_write;
26 AioFlushHandler *io_flush;
27 int deleted;
28 void *opaque;
Blue Swirl72cf2d42009-09-12 07:36:22 +000029 QLIST_ENTRY(AioHandler) node;
aliguoria76bab42008-09-22 19:17:18 +000030};
31
Paolo Bonzinia915f4b2012-09-13 12:28:51 +020032static AioHandler *find_aio_handler(AioContext *ctx, int fd)
aliguoria76bab42008-09-22 19:17:18 +000033{
34 AioHandler *node;
35
Paolo Bonzinia915f4b2012-09-13 12:28:51 +020036 QLIST_FOREACH(node, &ctx->aio_handlers, node) {
Paolo Bonzinicd9ba1e2012-09-24 14:57:22 +020037 if (node->pfd.fd == fd)
Alexander Graf79d5ca52009-05-06 02:58:48 +020038 if (!node->deleted)
39 return node;
aliguoria76bab42008-09-22 19:17:18 +000040 }
41
42 return NULL;
43}
44
Paolo Bonzinia915f4b2012-09-13 12:28:51 +020045void aio_set_fd_handler(AioContext *ctx,
46 int fd,
47 IOHandler *io_read,
48 IOHandler *io_write,
49 AioFlushHandler *io_flush,
50 void *opaque)
aliguoria76bab42008-09-22 19:17:18 +000051{
52 AioHandler *node;
53
Paolo Bonzinia915f4b2012-09-13 12:28:51 +020054 node = find_aio_handler(ctx, fd);
aliguoria76bab42008-09-22 19:17:18 +000055
56 /* Are we deleting the fd handler? */
57 if (!io_read && !io_write) {
58 if (node) {
Paolo Bonzinie3713e02012-09-24 14:57:41 +020059 g_source_remove_poll(&ctx->source, &node->pfd);
60
aliguoria76bab42008-09-22 19:17:18 +000061 /* If the lock is held, just mark the node as deleted */
Paolo Bonzinicd9ba1e2012-09-24 14:57:22 +020062 if (ctx->walking_handlers) {
aliguoria76bab42008-09-22 19:17:18 +000063 node->deleted = 1;
Paolo Bonzinicd9ba1e2012-09-24 14:57:22 +020064 node->pfd.revents = 0;
65 } else {
aliguoria76bab42008-09-22 19:17:18 +000066 /* 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 */
Blue Swirl72cf2d42009-09-12 07:36:22 +000070 QLIST_REMOVE(node, node);
Anthony Liguori7267c092011-08-20 22:09:37 -050071 g_free(node);
aliguoria76bab42008-09-22 19:17:18 +000072 }
73 }
74 } else {
75 if (node == NULL) {
76 /* Alloc and insert if it's not already there */
Anthony Liguori7267c092011-08-20 22:09:37 -050077 node = g_malloc0(sizeof(AioHandler));
Paolo Bonzinicd9ba1e2012-09-24 14:57:22 +020078 node->pfd.fd = fd;
Paolo Bonzinia915f4b2012-09-13 12:28:51 +020079 QLIST_INSERT_HEAD(&ctx->aio_handlers, node, node);
Paolo Bonzinie3713e02012-09-24 14:57:41 +020080
81 g_source_add_poll(&ctx->source, &node->pfd);
aliguoria76bab42008-09-22 19:17:18 +000082 }
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;
Paolo Bonzinicd9ba1e2012-09-24 14:57:22 +020088
89 node->pfd.events = (io_read ? G_IO_IN | G_IO_HUP : 0);
90 node->pfd.events |= (io_write ? G_IO_OUT : 0);
aliguoria76bab42008-09-22 19:17:18 +000091 }
aliguoria76bab42008-09-22 19:17:18 +000092}
93
Paolo Bonzinia915f4b2012-09-13 12:28:51 +020094void aio_set_event_notifier(AioContext *ctx,
95 EventNotifier *notifier,
96 EventNotifierHandler *io_read,
97 AioFlushEventNotifierHandler *io_flush)
Paolo Bonzini9958c352012-06-09 03:44:00 +020098{
Paolo Bonzinia915f4b2012-09-13 12:28:51 +020099 aio_set_fd_handler(ctx, event_notifier_get_fd(notifier),
100 (IOHandler *)io_read, NULL,
101 (AioFlushHandler *)io_flush, notifier);
Paolo Bonzini9958c352012-06-09 03:44:00 +0200102}
103
Paolo Bonzinicd9ba1e2012-09-24 14:57:22 +0200104bool aio_pending(AioContext *ctx)
105{
106 AioHandler *node;
107
108 QLIST_FOREACH(node, &ctx->aio_handlers, node) {
109 int revents;
110
111 /*
112 * FIXME: right now we cannot get G_IO_HUP and G_IO_ERR because
113 * main-loop.c is still select based (due to the slirp legacy).
114 * If main-loop.c ever switches to poll, G_IO_ERR should be
115 * tested too. Dispatching G_IO_ERR to both handlers should be
116 * okay, since handlers need to be ready for spurious wakeups.
117 */
118 revents = node->pfd.revents & node->pfd.events;
119 if (revents & (G_IO_IN | G_IO_HUP | G_IO_ERR) && node->io_read) {
120 return true;
121 }
122 if (revents & (G_IO_OUT | G_IO_ERR) && node->io_write) {
123 return true;
124 }
125 }
126
127 return false;
128}
129
Paolo Bonzini7c0628b2012-09-24 14:37:53 +0200130bool aio_poll(AioContext *ctx, bool blocking)
aliguoria76bab42008-09-22 19:17:18 +0000131{
Paolo Bonzini7c0628b2012-09-24 14:37:53 +0200132 static struct timeval tv0;
Paolo Bonzini9eb0bfc2012-04-12 14:00:56 +0200133 AioHandler *node;
134 fd_set rdfds, wrfds;
135 int max_fd = -1;
aliguoria76bab42008-09-22 19:17:18 +0000136 int ret;
Paolo Bonzini7c0628b2012-09-24 14:37:53 +0200137 bool busy, progress;
138
139 progress = false;
aliguoria76bab42008-09-22 19:17:18 +0000140
Kevin Wolf8febfa22009-10-22 17:54:36 +0200141 /*
142 * If there are callbacks left that have been queued, we need to call then.
Paolo Bonzinibcdc1852012-04-12 14:00:55 +0200143 * Do not call select in this case, because it is possible that the caller
144 * does not need a complete flush (as is the case for qemu_aio_wait loops).
Kevin Wolf8febfa22009-10-22 17:54:36 +0200145 */
Paolo Bonzinia915f4b2012-09-13 12:28:51 +0200146 if (aio_bh_poll(ctx)) {
Paolo Bonzini7c0628b2012-09-24 14:37:53 +0200147 blocking = false;
148 progress = true;
149 }
150
Paolo Bonzinicd9ba1e2012-09-24 14:57:22 +0200151 /*
152 * Then dispatch any pending callbacks from the GSource.
153 *
154 * We have to walk very carefully in case qemu_aio_set_fd_handler is
155 * called while we're walking.
156 */
157 node = QLIST_FIRST(&ctx->aio_handlers);
158 while (node) {
159 AioHandler *tmp;
160 int revents;
161
162 ctx->walking_handlers++;
163
164 revents = node->pfd.revents & node->pfd.events;
165 node->pfd.revents = 0;
166
167 /* See comment in aio_pending. */
168 if (revents & (G_IO_IN | G_IO_HUP | G_IO_ERR) && node->io_read) {
169 node->io_read(node->opaque);
170 progress = true;
171 }
172 if (revents & (G_IO_OUT | G_IO_ERR) && node->io_write) {
173 node->io_write(node->opaque);
174 progress = true;
175 }
176
177 tmp = node;
178 node = QLIST_NEXT(node, node);
179
180 ctx->walking_handlers--;
181
182 if (!ctx->walking_handlers && tmp->deleted) {
183 QLIST_REMOVE(tmp, node);
184 g_free(tmp);
185 }
186 }
187
Paolo Bonzini7c0628b2012-09-24 14:37:53 +0200188 if (progress && !blocking) {
Paolo Bonzinibcdc1852012-04-12 14:00:55 +0200189 return true;
Paolo Bonzinibafbd6a2012-04-12 14:00:54 +0200190 }
Kevin Wolf8febfa22009-10-22 17:54:36 +0200191
Paolo Bonzinia915f4b2012-09-13 12:28:51 +0200192 ctx->walking_handlers++;
aliguoria76bab42008-09-22 19:17:18 +0000193
Paolo Bonzini9eb0bfc2012-04-12 14:00:56 +0200194 FD_ZERO(&rdfds);
195 FD_ZERO(&wrfds);
196
197 /* fill fd sets */
198 busy = false;
Paolo Bonzinia915f4b2012-09-13 12:28:51 +0200199 QLIST_FOREACH(node, &ctx->aio_handlers, node) {
Paolo Bonzini9eb0bfc2012-04-12 14:00:56 +0200200 /* If there aren't pending AIO operations, don't invoke callbacks.
201 * Otherwise, if there are no AIO requests, qemu_aio_wait() would
202 * wait indefinitely.
203 */
Paolo Bonzini4231c882012-09-26 15:21:36 +0200204 if (!node->deleted && node->io_flush) {
Paolo Bonzini9eb0bfc2012-04-12 14:00:56 +0200205 if (node->io_flush(node->opaque) == 0) {
206 continue;
207 }
208 busy = true;
209 }
210 if (!node->deleted && node->io_read) {
Paolo Bonzinicd9ba1e2012-09-24 14:57:22 +0200211 FD_SET(node->pfd.fd, &rdfds);
212 max_fd = MAX(max_fd, node->pfd.fd + 1);
Paolo Bonzini9eb0bfc2012-04-12 14:00:56 +0200213 }
214 if (!node->deleted && node->io_write) {
Paolo Bonzinicd9ba1e2012-09-24 14:57:22 +0200215 FD_SET(node->pfd.fd, &wrfds);
216 max_fd = MAX(max_fd, node->pfd.fd + 1);
Paolo Bonzini9eb0bfc2012-04-12 14:00:56 +0200217 }
218 }
219
Paolo Bonzinia915f4b2012-09-13 12:28:51 +0200220 ctx->walking_handlers--;
Paolo Bonzini9eb0bfc2012-04-12 14:00:56 +0200221
222 /* No AIO operations? Get us out of here */
223 if (!busy) {
Paolo Bonzini7c0628b2012-09-24 14:37:53 +0200224 return progress;
Paolo Bonzini9eb0bfc2012-04-12 14:00:56 +0200225 }
226
227 /* wait until next event */
Paolo Bonzini7c0628b2012-09-24 14:37:53 +0200228 ret = select(max_fd, &rdfds, &wrfds, NULL, blocking ? NULL : &tv0);
Paolo Bonzini9eb0bfc2012-04-12 14:00:56 +0200229
230 /* if we have any readable fds, dispatch event */
231 if (ret > 0) {
Paolo Bonzini9eb0bfc2012-04-12 14:00:56 +0200232 /* we have to walk very carefully in case
233 * qemu_aio_set_fd_handler is called while we're walking */
Paolo Bonzinia915f4b2012-09-13 12:28:51 +0200234 node = QLIST_FIRST(&ctx->aio_handlers);
Paolo Bonzini9eb0bfc2012-04-12 14:00:56 +0200235 while (node) {
236 AioHandler *tmp;
aliguorif71903d2008-10-12 21:19:57 +0000237
Paolo Bonzinia915f4b2012-09-13 12:28:51 +0200238 ctx->walking_handlers++;
Paolo Bonzini2db2bfc2012-09-27 19:27:43 +0530239
Paolo Bonzini9eb0bfc2012-04-12 14:00:56 +0200240 if (!node->deleted &&
Paolo Bonzinicd9ba1e2012-09-24 14:57:22 +0200241 FD_ISSET(node->pfd.fd, &rdfds) &&
Paolo Bonzini9eb0bfc2012-04-12 14:00:56 +0200242 node->io_read) {
243 node->io_read(node->opaque);
Paolo Bonzinicd9ba1e2012-09-24 14:57:22 +0200244 progress = true;
Paolo Bonzinibcdc1852012-04-12 14:00:55 +0200245 }
Paolo Bonzini9eb0bfc2012-04-12 14:00:56 +0200246 if (!node->deleted &&
Paolo Bonzinicd9ba1e2012-09-24 14:57:22 +0200247 FD_ISSET(node->pfd.fd, &wrfds) &&
Paolo Bonzini9eb0bfc2012-04-12 14:00:56 +0200248 node->io_write) {
249 node->io_write(node->opaque);
Paolo Bonzinicd9ba1e2012-09-24 14:57:22 +0200250 progress = true;
aliguoria76bab42008-09-22 19:17:18 +0000251 }
Paolo Bonzini9eb0bfc2012-04-12 14:00:56 +0200252
253 tmp = node;
254 node = QLIST_NEXT(node, node);
255
Paolo Bonzinia915f4b2012-09-13 12:28:51 +0200256 ctx->walking_handlers--;
Paolo Bonzini2db2bfc2012-09-27 19:27:43 +0530257
Paolo Bonzinia915f4b2012-09-13 12:28:51 +0200258 if (!ctx->walking_handlers && tmp->deleted) {
Paolo Bonzini9eb0bfc2012-04-12 14:00:56 +0200259 QLIST_REMOVE(tmp, node);
260 g_free(tmp);
aliguoria76bab42008-09-22 19:17:18 +0000261 }
262 }
Paolo Bonzini9eb0bfc2012-04-12 14:00:56 +0200263 }
Paolo Bonzinibcdc1852012-04-12 14:00:55 +0200264
Paolo Bonzini7c0628b2012-09-24 14:37:53 +0200265 return progress;
aliguoria76bab42008-09-22 19:17:18 +0000266}