blob: 44247224e22fe8589e1295d3d2f29c8ca6faa7c6 [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) {
59 /* If the lock is held, just mark the node as deleted */
Paolo Bonzinicd9ba1e2012-09-24 14:57:22 +020060 if (ctx->walking_handlers) {
aliguoria76bab42008-09-22 19:17:18 +000061 node->deleted = 1;
Paolo Bonzinicd9ba1e2012-09-24 14:57:22 +020062 node->pfd.revents = 0;
63 } else {
aliguoria76bab42008-09-22 19:17:18 +000064 /* 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 */
Blue Swirl72cf2d42009-09-12 07:36:22 +000068 QLIST_REMOVE(node, node);
Anthony Liguori7267c092011-08-20 22:09:37 -050069 g_free(node);
aliguoria76bab42008-09-22 19:17:18 +000070 }
71 }
72 } else {
73 if (node == NULL) {
74 /* Alloc and insert if it's not already there */
Anthony Liguori7267c092011-08-20 22:09:37 -050075 node = g_malloc0(sizeof(AioHandler));
Paolo Bonzinicd9ba1e2012-09-24 14:57:22 +020076 node->pfd.fd = fd;
Paolo Bonzinia915f4b2012-09-13 12:28:51 +020077 QLIST_INSERT_HEAD(&ctx->aio_handlers, node, node);
aliguoria76bab42008-09-22 19:17:18 +000078 }
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;
Paolo Bonzinicd9ba1e2012-09-24 14:57:22 +020084
85 node->pfd.events = (io_read ? G_IO_IN | G_IO_HUP : 0);
86 node->pfd.events |= (io_write ? G_IO_OUT : 0);
aliguoria76bab42008-09-22 19:17:18 +000087 }
aliguoria76bab42008-09-22 19:17:18 +000088}
89
Paolo Bonzinia915f4b2012-09-13 12:28:51 +020090void aio_set_event_notifier(AioContext *ctx,
91 EventNotifier *notifier,
92 EventNotifierHandler *io_read,
93 AioFlushEventNotifierHandler *io_flush)
Paolo Bonzini9958c352012-06-09 03:44:00 +020094{
Paolo Bonzinia915f4b2012-09-13 12:28:51 +020095 aio_set_fd_handler(ctx, event_notifier_get_fd(notifier),
96 (IOHandler *)io_read, NULL,
97 (AioFlushHandler *)io_flush, notifier);
Paolo Bonzini9958c352012-06-09 03:44:00 +020098}
99
Paolo Bonzinicd9ba1e2012-09-24 14:57:22 +0200100bool 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
Paolo Bonzini7c0628b2012-09-24 14:37:53 +0200126bool aio_poll(AioContext *ctx, bool blocking)
aliguoria76bab42008-09-22 19:17:18 +0000127{
Paolo Bonzini7c0628b2012-09-24 14:37:53 +0200128 static struct timeval tv0;
Paolo Bonzini9eb0bfc2012-04-12 14:00:56 +0200129 AioHandler *node;
130 fd_set rdfds, wrfds;
131 int max_fd = -1;
aliguoria76bab42008-09-22 19:17:18 +0000132 int ret;
Paolo Bonzini7c0628b2012-09-24 14:37:53 +0200133 bool busy, progress;
134
135 progress = false;
aliguoria76bab42008-09-22 19:17:18 +0000136
Kevin Wolf8febfa22009-10-22 17:54:36 +0200137 /*
138 * If there are callbacks left that have been queued, we need to call then.
Paolo Bonzinibcdc1852012-04-12 14:00:55 +0200139 * 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).
Kevin Wolf8febfa22009-10-22 17:54:36 +0200141 */
Paolo Bonzinia915f4b2012-09-13 12:28:51 +0200142 if (aio_bh_poll(ctx)) {
Paolo Bonzini7c0628b2012-09-24 14:37:53 +0200143 blocking = false;
144 progress = true;
145 }
146
Paolo Bonzinicd9ba1e2012-09-24 14:57:22 +0200147 /*
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
Paolo Bonzini7c0628b2012-09-24 14:37:53 +0200184 if (progress && !blocking) {
Paolo Bonzinibcdc1852012-04-12 14:00:55 +0200185 return true;
Paolo Bonzinibafbd6a2012-04-12 14:00:54 +0200186 }
Kevin Wolf8febfa22009-10-22 17:54:36 +0200187
Paolo Bonzinia915f4b2012-09-13 12:28:51 +0200188 ctx->walking_handlers++;
aliguoria76bab42008-09-22 19:17:18 +0000189
Paolo Bonzini9eb0bfc2012-04-12 14:00:56 +0200190 FD_ZERO(&rdfds);
191 FD_ZERO(&wrfds);
192
193 /* fill fd sets */
194 busy = false;
Paolo Bonzinia915f4b2012-09-13 12:28:51 +0200195 QLIST_FOREACH(node, &ctx->aio_handlers, node) {
Paolo Bonzini9eb0bfc2012-04-12 14:00:56 +0200196 /* 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 */
Paolo Bonzini4231c882012-09-26 15:21:36 +0200200 if (!node->deleted && node->io_flush) {
Paolo Bonzini9eb0bfc2012-04-12 14:00:56 +0200201 if (node->io_flush(node->opaque) == 0) {
202 continue;
203 }
204 busy = true;
205 }
206 if (!node->deleted && node->io_read) {
Paolo Bonzinicd9ba1e2012-09-24 14:57:22 +0200207 FD_SET(node->pfd.fd, &rdfds);
208 max_fd = MAX(max_fd, node->pfd.fd + 1);
Paolo Bonzini9eb0bfc2012-04-12 14:00:56 +0200209 }
210 if (!node->deleted && node->io_write) {
Paolo Bonzinicd9ba1e2012-09-24 14:57:22 +0200211 FD_SET(node->pfd.fd, &wrfds);
212 max_fd = MAX(max_fd, node->pfd.fd + 1);
Paolo Bonzini9eb0bfc2012-04-12 14:00:56 +0200213 }
214 }
215
Paolo Bonzinia915f4b2012-09-13 12:28:51 +0200216 ctx->walking_handlers--;
Paolo Bonzini9eb0bfc2012-04-12 14:00:56 +0200217
218 /* No AIO operations? Get us out of here */
219 if (!busy) {
Paolo Bonzini7c0628b2012-09-24 14:37:53 +0200220 return progress;
Paolo Bonzini9eb0bfc2012-04-12 14:00:56 +0200221 }
222
223 /* wait until next event */
Paolo Bonzini7c0628b2012-09-24 14:37:53 +0200224 ret = select(max_fd, &rdfds, &wrfds, NULL, blocking ? NULL : &tv0);
Paolo Bonzini9eb0bfc2012-04-12 14:00:56 +0200225
226 /* if we have any readable fds, dispatch event */
227 if (ret > 0) {
Paolo Bonzini9eb0bfc2012-04-12 14:00:56 +0200228 /* we have to walk very carefully in case
229 * qemu_aio_set_fd_handler is called while we're walking */
Paolo Bonzinia915f4b2012-09-13 12:28:51 +0200230 node = QLIST_FIRST(&ctx->aio_handlers);
Paolo Bonzini9eb0bfc2012-04-12 14:00:56 +0200231 while (node) {
232 AioHandler *tmp;
aliguorif71903d2008-10-12 21:19:57 +0000233
Paolo Bonzinia915f4b2012-09-13 12:28:51 +0200234 ctx->walking_handlers++;
Paolo Bonzini2db2bfc2012-09-27 19:27:43 +0530235
Paolo Bonzini9eb0bfc2012-04-12 14:00:56 +0200236 if (!node->deleted &&
Paolo Bonzinicd9ba1e2012-09-24 14:57:22 +0200237 FD_ISSET(node->pfd.fd, &rdfds) &&
Paolo Bonzini9eb0bfc2012-04-12 14:00:56 +0200238 node->io_read) {
239 node->io_read(node->opaque);
Paolo Bonzinicd9ba1e2012-09-24 14:57:22 +0200240 progress = true;
Paolo Bonzinibcdc1852012-04-12 14:00:55 +0200241 }
Paolo Bonzini9eb0bfc2012-04-12 14:00:56 +0200242 if (!node->deleted &&
Paolo Bonzinicd9ba1e2012-09-24 14:57:22 +0200243 FD_ISSET(node->pfd.fd, &wrfds) &&
Paolo Bonzini9eb0bfc2012-04-12 14:00:56 +0200244 node->io_write) {
245 node->io_write(node->opaque);
Paolo Bonzinicd9ba1e2012-09-24 14:57:22 +0200246 progress = true;
aliguoria76bab42008-09-22 19:17:18 +0000247 }
Paolo Bonzini9eb0bfc2012-04-12 14:00:56 +0200248
249 tmp = node;
250 node = QLIST_NEXT(node, node);
251
Paolo Bonzinia915f4b2012-09-13 12:28:51 +0200252 ctx->walking_handlers--;
Paolo Bonzini2db2bfc2012-09-27 19:27:43 +0530253
Paolo Bonzinia915f4b2012-09-13 12:28:51 +0200254 if (!ctx->walking_handlers && tmp->deleted) {
Paolo Bonzini9eb0bfc2012-04-12 14:00:56 +0200255 QLIST_REMOVE(tmp, node);
256 g_free(tmp);
aliguoria76bab42008-09-22 19:17:18 +0000257 }
258 }
Paolo Bonzini9eb0bfc2012-04-12 14:00:56 +0200259 }
Paolo Bonzinibcdc1852012-04-12 14:00:55 +0200260
Paolo Bonzini7c0628b2012-09-24 14:37:53 +0200261 return progress;
aliguoria76bab42008-09-22 19:17:18 +0000262}