blob: f921d4f538337c30e58abd7f1b3565c79c155ff4 [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"
Paolo Bonzini737e1502012-12-17 18:19:44 +010017#include "block/block.h"
Paolo Bonzini1de7afc2012-12-17 18:20:00 +010018#include "qemu/queue.h"
19#include "qemu/sockets.h"
aliguoria76bab42008-09-22 19:17:18 +000020
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;
aliguoria76bab42008-09-22 19:17:18 +000026 int deleted;
Stefan Hajnoczi6b5f8762013-02-20 11:28:32 +010027 int pollfds_idx;
aliguoria76bab42008-09-22 19:17:18 +000028 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,
Paolo Bonzinia915f4b2012-09-13 12:28:51 +020049 void *opaque)
aliguoria76bab42008-09-22 19:17:18 +000050{
51 AioHandler *node;
52
Paolo Bonzinia915f4b2012-09-13 12:28:51 +020053 node = find_aio_handler(ctx, fd);
aliguoria76bab42008-09-22 19:17:18 +000054
55 /* Are we deleting the fd handler? */
56 if (!io_read && !io_write) {
57 if (node) {
Paolo Bonzinie3713e02012-09-24 14:57:41 +020058 g_source_remove_poll(&ctx->source, &node->pfd);
59
aliguoria76bab42008-09-22 19:17:18 +000060 /* If the lock is held, just mark the node as deleted */
Paolo Bonzinicd9ba1e2012-09-24 14:57:22 +020061 if (ctx->walking_handlers) {
aliguoria76bab42008-09-22 19:17:18 +000062 node->deleted = 1;
Paolo Bonzinicd9ba1e2012-09-24 14:57:22 +020063 node->pfd.revents = 0;
64 } else {
aliguoria76bab42008-09-22 19:17:18 +000065 /* Otherwise, delete it for real. We can't just mark it as
66 * deleted because deleted nodes are only cleaned up after
67 * releasing the walking_handlers lock.
68 */
Blue Swirl72cf2d42009-09-12 07:36:22 +000069 QLIST_REMOVE(node, node);
Anthony Liguori7267c092011-08-20 22:09:37 -050070 g_free(node);
aliguoria76bab42008-09-22 19:17:18 +000071 }
72 }
73 } else {
74 if (node == NULL) {
75 /* Alloc and insert if it's not already there */
Anthony Liguori7267c092011-08-20 22:09:37 -050076 node = g_malloc0(sizeof(AioHandler));
Paolo Bonzinicd9ba1e2012-09-24 14:57:22 +020077 node->pfd.fd = fd;
Paolo Bonzinia915f4b2012-09-13 12:28:51 +020078 QLIST_INSERT_HEAD(&ctx->aio_handlers, node, node);
Paolo Bonzinie3713e02012-09-24 14:57:41 +020079
80 g_source_add_poll(&ctx->source, &node->pfd);
aliguoria76bab42008-09-22 19:17:18 +000081 }
82 /* Update handler with latest information */
83 node->io_read = io_read;
84 node->io_write = io_write;
aliguoria76bab42008-09-22 19:17:18 +000085 node->opaque = opaque;
Stefan Hajnoczi6b5f8762013-02-20 11:28:32 +010086 node->pollfds_idx = -1;
Paolo Bonzinicd9ba1e2012-09-24 14:57:22 +020087
Stefan Hajnoczib5a01a72013-02-20 11:28:33 +010088 node->pfd.events = (io_read ? G_IO_IN | G_IO_HUP | G_IO_ERR : 0);
89 node->pfd.events |= (io_write ? G_IO_OUT | G_IO_ERR : 0);
aliguoria76bab42008-09-22 19:17:18 +000090 }
Paolo Bonzini7ed2b242012-09-25 10:22:39 +020091
92 aio_notify(ctx);
aliguoria76bab42008-09-22 19:17:18 +000093}
94
Paolo Bonzinia915f4b2012-09-13 12:28:51 +020095void aio_set_event_notifier(AioContext *ctx,
96 EventNotifier *notifier,
Stefan Hajnoczif2e5dca2013-04-11 17:26:25 +020097 EventNotifierHandler *io_read)
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),
Stefan Hajnoczif2e5dca2013-04-11 17:26:25 +0200100 (IOHandler *)io_read, NULL, notifier);
Paolo Bonzini9958c352012-06-09 03:44:00 +0200101}
102
Paolo Bonzinicd9ba1e2012-09-24 14:57:22 +0200103bool aio_pending(AioContext *ctx)
104{
105 AioHandler *node;
106
107 QLIST_FOREACH(node, &ctx->aio_handlers, node) {
108 int revents;
109
Paolo Bonzinicd9ba1e2012-09-24 14:57:22 +0200110 revents = node->pfd.revents & node->pfd.events;
111 if (revents & (G_IO_IN | G_IO_HUP | G_IO_ERR) && node->io_read) {
112 return true;
113 }
114 if (revents & (G_IO_OUT | G_IO_ERR) && node->io_write) {
115 return true;
116 }
117 }
118
119 return false;
120}
121
Stefan Hajnoczid0c8d2c2013-02-20 11:28:31 +0100122static bool aio_dispatch(AioContext *ctx)
aliguoria76bab42008-09-22 19:17:18 +0000123{
Paolo Bonzini9eb0bfc2012-04-12 14:00:56 +0200124 AioHandler *node;
Stefan Hajnoczid0c8d2c2013-02-20 11:28:31 +0100125 bool progress = false;
aliguoria76bab42008-09-22 19:17:18 +0000126
Kevin Wolf8febfa22009-10-22 17:54:36 +0200127 /*
Paolo Bonzinicd9ba1e2012-09-24 14:57:22 +0200128 * We have to walk very carefully in case qemu_aio_set_fd_handler is
129 * called while we're walking.
130 */
131 node = QLIST_FIRST(&ctx->aio_handlers);
132 while (node) {
133 AioHandler *tmp;
134 int revents;
135
136 ctx->walking_handlers++;
137
138 revents = node->pfd.revents & node->pfd.events;
139 node->pfd.revents = 0;
140
Stefan Hajnoczid0c8d2c2013-02-20 11:28:31 +0100141 if (!node->deleted &&
142 (revents & (G_IO_IN | G_IO_HUP | G_IO_ERR)) &&
143 node->io_read) {
Paolo Bonzinicd9ba1e2012-09-24 14:57:22 +0200144 node->io_read(node->opaque);
Stefan Hajnoczi164a1012013-04-11 16:56:50 +0200145
146 /* aio_notify() does not count as progress */
147 if (node->opaque != &ctx->notifier) {
148 progress = true;
149 }
Paolo Bonzinicd9ba1e2012-09-24 14:57:22 +0200150 }
Stefan Hajnoczid0c8d2c2013-02-20 11:28:31 +0100151 if (!node->deleted &&
152 (revents & (G_IO_OUT | G_IO_ERR)) &&
153 node->io_write) {
Paolo Bonzinicd9ba1e2012-09-24 14:57:22 +0200154 node->io_write(node->opaque);
155 progress = true;
156 }
157
158 tmp = node;
159 node = QLIST_NEXT(node, node);
160
161 ctx->walking_handlers--;
162
163 if (!ctx->walking_handlers && tmp->deleted) {
164 QLIST_REMOVE(tmp, node);
165 g_free(tmp);
166 }
167 }
Alex Bligh438e1f42013-08-21 16:02:53 +0100168
169 /* Run our timers */
170 progress |= timerlistgroup_run_timers(&ctx->tlg);
171
Stefan Hajnoczid0c8d2c2013-02-20 11:28:31 +0100172 return progress;
173}
174
175bool aio_poll(AioContext *ctx, bool blocking)
176{
Stefan Hajnoczid0c8d2c2013-02-20 11:28:31 +0100177 AioHandler *node;
Stefan Hajnoczid0c8d2c2013-02-20 11:28:31 +0100178 int ret;
Stefan Hajnoczi164a1012013-04-11 16:56:50 +0200179 bool progress;
Stefan Hajnoczid0c8d2c2013-02-20 11:28:31 +0100180
181 progress = false;
182
183 /*
184 * If there are callbacks left that have been queued, we need to call them.
185 * Do not call select in this case, because it is possible that the caller
186 * does not need a complete flush (as is the case for qemu_aio_wait loops).
187 */
188 if (aio_bh_poll(ctx)) {
189 blocking = false;
190 progress = true;
191 }
192
193 if (aio_dispatch(ctx)) {
194 progress = true;
195 }
Paolo Bonzinicd9ba1e2012-09-24 14:57:22 +0200196
Paolo Bonzini7c0628b2012-09-24 14:37:53 +0200197 if (progress && !blocking) {
Paolo Bonzinibcdc1852012-04-12 14:00:55 +0200198 return true;
Paolo Bonzinibafbd6a2012-04-12 14:00:54 +0200199 }
Kevin Wolf8febfa22009-10-22 17:54:36 +0200200
Paolo Bonzinia915f4b2012-09-13 12:28:51 +0200201 ctx->walking_handlers++;
aliguoria76bab42008-09-22 19:17:18 +0000202
Stefan Hajnoczi6b5f8762013-02-20 11:28:32 +0100203 g_array_set_size(ctx->pollfds, 0);
Paolo Bonzini9eb0bfc2012-04-12 14:00:56 +0200204
Stefan Hajnoczi6b5f8762013-02-20 11:28:32 +0100205 /* fill pollfds */
Paolo Bonzinia915f4b2012-09-13 12:28:51 +0200206 QLIST_FOREACH(node, &ctx->aio_handlers, node) {
Stefan Hajnoczi6b5f8762013-02-20 11:28:32 +0100207 node->pollfds_idx = -1;
Stefan Hajnoczi6b5f8762013-02-20 11:28:32 +0100208 if (!node->deleted && node->pfd.events) {
209 GPollFD pfd = {
210 .fd = node->pfd.fd,
211 .events = node->pfd.events,
212 };
213 node->pollfds_idx = ctx->pollfds->len;
214 g_array_append_val(ctx->pollfds, pfd);
Paolo Bonzini9eb0bfc2012-04-12 14:00:56 +0200215 }
216 }
217
Paolo Bonzinia915f4b2012-09-13 12:28:51 +0200218 ctx->walking_handlers--;
Paolo Bonzini9eb0bfc2012-04-12 14:00:56 +0200219
Paolo Bonzini9eb0bfc2012-04-12 14:00:56 +0200220 /* wait until next event */
Alex Bligh438e1f42013-08-21 16:02:53 +0100221 ret = qemu_poll_ns((GPollFD *)ctx->pollfds->data,
222 ctx->pollfds->len,
223 blocking ? timerlistgroup_deadline_ns(&ctx->tlg) : 0);
Paolo Bonzini9eb0bfc2012-04-12 14:00:56 +0200224
225 /* if we have any readable fds, dispatch event */
226 if (ret > 0) {
Stefan Hajnoczi6b5f8762013-02-20 11:28:32 +0100227 QLIST_FOREACH(node, &ctx->aio_handlers, node) {
228 if (node->pollfds_idx != -1) {
229 GPollFD *pfd = &g_array_index(ctx->pollfds, GPollFD,
230 node->pollfds_idx);
231 node->pfd.revents = pfd->revents;
Paolo Bonzinibcdc1852012-04-12 14:00:55 +0200232 }
Stefan Hajnoczi6b5f8762013-02-20 11:28:32 +0100233 }
Alex Bligh438e1f42013-08-21 16:02:53 +0100234 }
235
236 /* Run dispatch even if there were no readable fds to run timers */
237 if (aio_dispatch(ctx)) {
238 progress = true;
Paolo Bonzini9eb0bfc2012-04-12 14:00:56 +0200239 }
Paolo Bonzinibcdc1852012-04-12 14:00:55 +0200240
Stefan Hajnoczi164a1012013-04-11 16:56:50 +0200241 return progress;
aliguoria76bab42008-09-22 19:17:18 +0000242}