blob: 0936b4ff9d5f8df58ec6df0d305af3f1d207e2ab [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
Paolo Bonzinie4c7e2d2014-07-09 11:53:05 +0200122bool 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 Bonzinie4c7e2d2014-07-09 11:53:05 +0200128 * If there are callbacks left that have been queued, we need to call them.
129 * Do not call select in this case, because it is possible that the caller
130 * does not need a complete flush (as is the case for aio_poll loops).
131 */
132 if (aio_bh_poll(ctx)) {
133 progress = true;
134 }
135
136 /*
Paolo Bonzini87f68d32014-07-07 15:18:02 +0200137 * We have to walk very carefully in case aio_set_fd_handler is
Paolo Bonzinicd9ba1e2012-09-24 14:57:22 +0200138 * called while we're walking.
139 */
140 node = QLIST_FIRST(&ctx->aio_handlers);
141 while (node) {
142 AioHandler *tmp;
143 int revents;
144
145 ctx->walking_handlers++;
146
147 revents = node->pfd.revents & node->pfd.events;
148 node->pfd.revents = 0;
149
Stefan Hajnoczid0c8d2c2013-02-20 11:28:31 +0100150 if (!node->deleted &&
151 (revents & (G_IO_IN | G_IO_HUP | G_IO_ERR)) &&
152 node->io_read) {
Paolo Bonzinicd9ba1e2012-09-24 14:57:22 +0200153 node->io_read(node->opaque);
Stefan Hajnoczi164a1012013-04-11 16:56:50 +0200154
155 /* aio_notify() does not count as progress */
156 if (node->opaque != &ctx->notifier) {
157 progress = true;
158 }
Paolo Bonzinicd9ba1e2012-09-24 14:57:22 +0200159 }
Stefan Hajnoczid0c8d2c2013-02-20 11:28:31 +0100160 if (!node->deleted &&
161 (revents & (G_IO_OUT | G_IO_ERR)) &&
162 node->io_write) {
Paolo Bonzinicd9ba1e2012-09-24 14:57:22 +0200163 node->io_write(node->opaque);
164 progress = true;
165 }
166
167 tmp = node;
168 node = QLIST_NEXT(node, node);
169
170 ctx->walking_handlers--;
171
172 if (!ctx->walking_handlers && tmp->deleted) {
173 QLIST_REMOVE(tmp, node);
174 g_free(tmp);
175 }
176 }
Alex Bligh438e1f42013-08-21 16:02:53 +0100177
178 /* Run our timers */
179 progress |= timerlistgroup_run_timers(&ctx->tlg);
180
Stefan Hajnoczid0c8d2c2013-02-20 11:28:31 +0100181 return progress;
182}
183
184bool aio_poll(AioContext *ctx, bool blocking)
185{
Stefan Hajnoczid0c8d2c2013-02-20 11:28:31 +0100186 AioHandler *node;
Paolo Bonzini0ceb8492014-07-07 15:18:04 +0200187 bool was_dispatching;
Stefan Hajnoczid0c8d2c2013-02-20 11:28:31 +0100188 int ret;
Stefan Hajnoczi164a1012013-04-11 16:56:50 +0200189 bool progress;
Stefan Hajnoczid0c8d2c2013-02-20 11:28:31 +0100190
Paolo Bonzini0ceb8492014-07-07 15:18:04 +0200191 was_dispatching = ctx->dispatching;
Stefan Hajnoczid0c8d2c2013-02-20 11:28:31 +0100192 progress = false;
193
Paolo Bonzini0ceb8492014-07-07 15:18:04 +0200194 /* aio_notify can avoid the expensive event_notifier_set if
195 * everything (file descriptors, bottom halves, timers) will
Paolo Bonzinie4c7e2d2014-07-09 11:53:05 +0200196 * be re-evaluated before the next blocking poll(). This is
197 * already true when aio_poll is called with blocking == false;
198 * if blocking == true, it is only true after poll() returns.
Paolo Bonzini0ceb8492014-07-07 15:18:04 +0200199 *
200 * If we're in a nested event loop, ctx->dispatching might be true.
201 * In that case we can restore it just before returning, but we
202 * have to clear it now.
203 */
204 aio_set_dispatching(ctx, !blocking);
205
Paolo Bonzinia915f4b2012-09-13 12:28:51 +0200206 ctx->walking_handlers++;
aliguoria76bab42008-09-22 19:17:18 +0000207
Stefan Hajnoczi6b5f8762013-02-20 11:28:32 +0100208 g_array_set_size(ctx->pollfds, 0);
Paolo Bonzini9eb0bfc2012-04-12 14:00:56 +0200209
Stefan Hajnoczi6b5f8762013-02-20 11:28:32 +0100210 /* fill pollfds */
Paolo Bonzinia915f4b2012-09-13 12:28:51 +0200211 QLIST_FOREACH(node, &ctx->aio_handlers, node) {
Stefan Hajnoczi6b5f8762013-02-20 11:28:32 +0100212 node->pollfds_idx = -1;
Stefan Hajnoczi6b5f8762013-02-20 11:28:32 +0100213 if (!node->deleted && node->pfd.events) {
214 GPollFD pfd = {
215 .fd = node->pfd.fd,
216 .events = node->pfd.events,
217 };
218 node->pollfds_idx = ctx->pollfds->len;
219 g_array_append_val(ctx->pollfds, pfd);
Paolo Bonzini9eb0bfc2012-04-12 14:00:56 +0200220 }
221 }
222
Paolo Bonzinia915f4b2012-09-13 12:28:51 +0200223 ctx->walking_handlers--;
Paolo Bonzini9eb0bfc2012-04-12 14:00:56 +0200224
Paolo Bonzini9eb0bfc2012-04-12 14:00:56 +0200225 /* wait until next event */
Alex Bligh438e1f42013-08-21 16:02:53 +0100226 ret = qemu_poll_ns((GPollFD *)ctx->pollfds->data,
227 ctx->pollfds->len,
Paolo Bonzini845ca102014-07-09 11:53:01 +0200228 blocking ? aio_compute_timeout(ctx) : 0);
Paolo Bonzini9eb0bfc2012-04-12 14:00:56 +0200229
230 /* if we have any readable fds, dispatch event */
231 if (ret > 0) {
Stefan Hajnoczi6b5f8762013-02-20 11:28:32 +0100232 QLIST_FOREACH(node, &ctx->aio_handlers, node) {
233 if (node->pollfds_idx != -1) {
234 GPollFD *pfd = &g_array_index(ctx->pollfds, GPollFD,
235 node->pollfds_idx);
236 node->pfd.revents = pfd->revents;
Paolo Bonzinibcdc1852012-04-12 14:00:55 +0200237 }
Stefan Hajnoczi6b5f8762013-02-20 11:28:32 +0100238 }
Alex Bligh438e1f42013-08-21 16:02:53 +0100239 }
240
241 /* Run dispatch even if there were no readable fds to run timers */
Paolo Bonzini0ceb8492014-07-07 15:18:04 +0200242 aio_set_dispatching(ctx, true);
Alex Bligh438e1f42013-08-21 16:02:53 +0100243 if (aio_dispatch(ctx)) {
244 progress = true;
Paolo Bonzini9eb0bfc2012-04-12 14:00:56 +0200245 }
Paolo Bonzinibcdc1852012-04-12 14:00:55 +0200246
Paolo Bonzini0ceb8492014-07-07 15:18:04 +0200247 aio_set_dispatching(ctx, was_dispatching);
Stefan Hajnoczi164a1012013-04-11 16:56:50 +0200248 return progress;
aliguoria76bab42008-09-22 19:17:18 +0000249}