blob: d3ac06e238b565716f155a10f4827a2e66c14c14 [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 Bonzinia3462c62014-07-09 11:53:08 +0200103bool aio_prepare(AioContext *ctx)
104{
105 return false;
106}
107
Paolo Bonzinicd9ba1e2012-09-24 14:57:22 +0200108bool aio_pending(AioContext *ctx)
109{
110 AioHandler *node;
111
112 QLIST_FOREACH(node, &ctx->aio_handlers, node) {
113 int revents;
114
Paolo Bonzinicd9ba1e2012-09-24 14:57:22 +0200115 revents = node->pfd.revents & node->pfd.events;
116 if (revents & (G_IO_IN | G_IO_HUP | G_IO_ERR) && node->io_read) {
117 return true;
118 }
119 if (revents & (G_IO_OUT | G_IO_ERR) && node->io_write) {
120 return true;
121 }
122 }
123
124 return false;
125}
126
Paolo Bonzinie4c7e2d2014-07-09 11:53:05 +0200127bool aio_dispatch(AioContext *ctx)
aliguoria76bab42008-09-22 19:17:18 +0000128{
Paolo Bonzini9eb0bfc2012-04-12 14:00:56 +0200129 AioHandler *node;
Stefan Hajnoczid0c8d2c2013-02-20 11:28:31 +0100130 bool progress = false;
aliguoria76bab42008-09-22 19:17:18 +0000131
Kevin Wolf8febfa22009-10-22 17:54:36 +0200132 /*
Paolo Bonzinie4c7e2d2014-07-09 11:53:05 +0200133 * If there are callbacks left that have been queued, we need to call them.
134 * Do not call select in this case, because it is possible that the caller
135 * does not need a complete flush (as is the case for aio_poll loops).
136 */
137 if (aio_bh_poll(ctx)) {
138 progress = true;
139 }
140
141 /*
Paolo Bonzini87f68d32014-07-07 15:18:02 +0200142 * We have to walk very carefully in case aio_set_fd_handler is
Paolo Bonzinicd9ba1e2012-09-24 14:57:22 +0200143 * called while we're walking.
144 */
145 node = QLIST_FIRST(&ctx->aio_handlers);
146 while (node) {
147 AioHandler *tmp;
148 int revents;
149
150 ctx->walking_handlers++;
151
152 revents = node->pfd.revents & node->pfd.events;
153 node->pfd.revents = 0;
154
Stefan Hajnoczid0c8d2c2013-02-20 11:28:31 +0100155 if (!node->deleted &&
156 (revents & (G_IO_IN | G_IO_HUP | G_IO_ERR)) &&
157 node->io_read) {
Paolo Bonzinicd9ba1e2012-09-24 14:57:22 +0200158 node->io_read(node->opaque);
Stefan Hajnoczi164a1012013-04-11 16:56:50 +0200159
160 /* aio_notify() does not count as progress */
161 if (node->opaque != &ctx->notifier) {
162 progress = true;
163 }
Paolo Bonzinicd9ba1e2012-09-24 14:57:22 +0200164 }
Stefan Hajnoczid0c8d2c2013-02-20 11:28:31 +0100165 if (!node->deleted &&
166 (revents & (G_IO_OUT | G_IO_ERR)) &&
167 node->io_write) {
Paolo Bonzinicd9ba1e2012-09-24 14:57:22 +0200168 node->io_write(node->opaque);
169 progress = true;
170 }
171
172 tmp = node;
173 node = QLIST_NEXT(node, node);
174
175 ctx->walking_handlers--;
176
177 if (!ctx->walking_handlers && tmp->deleted) {
178 QLIST_REMOVE(tmp, node);
179 g_free(tmp);
180 }
181 }
Alex Bligh438e1f42013-08-21 16:02:53 +0100182
183 /* Run our timers */
184 progress |= timerlistgroup_run_timers(&ctx->tlg);
185
Stefan Hajnoczid0c8d2c2013-02-20 11:28:31 +0100186 return progress;
187}
188
189bool aio_poll(AioContext *ctx, bool blocking)
190{
Stefan Hajnoczid0c8d2c2013-02-20 11:28:31 +0100191 AioHandler *node;
Paolo Bonzini0ceb8492014-07-07 15:18:04 +0200192 bool was_dispatching;
Stefan Hajnoczid0c8d2c2013-02-20 11:28:31 +0100193 int ret;
Stefan Hajnoczi164a1012013-04-11 16:56:50 +0200194 bool progress;
Stefan Hajnoczid0c8d2c2013-02-20 11:28:31 +0100195
Paolo Bonzini0ceb8492014-07-07 15:18:04 +0200196 was_dispatching = ctx->dispatching;
Stefan Hajnoczid0c8d2c2013-02-20 11:28:31 +0100197 progress = false;
198
Paolo Bonzini0ceb8492014-07-07 15:18:04 +0200199 /* aio_notify can avoid the expensive event_notifier_set if
200 * everything (file descriptors, bottom halves, timers) will
Paolo Bonzinie4c7e2d2014-07-09 11:53:05 +0200201 * be re-evaluated before the next blocking poll(). This is
202 * already true when aio_poll is called with blocking == false;
203 * if blocking == true, it is only true after poll() returns.
Paolo Bonzini0ceb8492014-07-07 15:18:04 +0200204 *
205 * If we're in a nested event loop, ctx->dispatching might be true.
206 * In that case we can restore it just before returning, but we
207 * have to clear it now.
208 */
209 aio_set_dispatching(ctx, !blocking);
210
Paolo Bonzinia915f4b2012-09-13 12:28:51 +0200211 ctx->walking_handlers++;
aliguoria76bab42008-09-22 19:17:18 +0000212
Stefan Hajnoczi6b5f8762013-02-20 11:28:32 +0100213 g_array_set_size(ctx->pollfds, 0);
Paolo Bonzini9eb0bfc2012-04-12 14:00:56 +0200214
Stefan Hajnoczi6b5f8762013-02-20 11:28:32 +0100215 /* fill pollfds */
Paolo Bonzinia915f4b2012-09-13 12:28:51 +0200216 QLIST_FOREACH(node, &ctx->aio_handlers, node) {
Stefan Hajnoczi6b5f8762013-02-20 11:28:32 +0100217 node->pollfds_idx = -1;
Stefan Hajnoczi6b5f8762013-02-20 11:28:32 +0100218 if (!node->deleted && node->pfd.events) {
219 GPollFD pfd = {
220 .fd = node->pfd.fd,
221 .events = node->pfd.events,
222 };
223 node->pollfds_idx = ctx->pollfds->len;
224 g_array_append_val(ctx->pollfds, pfd);
Paolo Bonzini9eb0bfc2012-04-12 14:00:56 +0200225 }
226 }
227
Paolo Bonzinia915f4b2012-09-13 12:28:51 +0200228 ctx->walking_handlers--;
Paolo Bonzini9eb0bfc2012-04-12 14:00:56 +0200229
Paolo Bonzini9eb0bfc2012-04-12 14:00:56 +0200230 /* wait until next event */
Alex Bligh438e1f42013-08-21 16:02:53 +0100231 ret = qemu_poll_ns((GPollFD *)ctx->pollfds->data,
232 ctx->pollfds->len,
Paolo Bonzini845ca102014-07-09 11:53:01 +0200233 blocking ? aio_compute_timeout(ctx) : 0);
Paolo Bonzini9eb0bfc2012-04-12 14:00:56 +0200234
235 /* if we have any readable fds, dispatch event */
236 if (ret > 0) {
Stefan Hajnoczi6b5f8762013-02-20 11:28:32 +0100237 QLIST_FOREACH(node, &ctx->aio_handlers, node) {
238 if (node->pollfds_idx != -1) {
239 GPollFD *pfd = &g_array_index(ctx->pollfds, GPollFD,
240 node->pollfds_idx);
241 node->pfd.revents = pfd->revents;
Paolo Bonzinibcdc1852012-04-12 14:00:55 +0200242 }
Stefan Hajnoczi6b5f8762013-02-20 11:28:32 +0100243 }
Alex Bligh438e1f42013-08-21 16:02:53 +0100244 }
245
246 /* Run dispatch even if there were no readable fds to run timers */
Paolo Bonzini0ceb8492014-07-07 15:18:04 +0200247 aio_set_dispatching(ctx, true);
Alex Bligh438e1f42013-08-21 16:02:53 +0100248 if (aio_dispatch(ctx)) {
249 progress = true;
Paolo Bonzini9eb0bfc2012-04-12 14:00:56 +0200250 }
Paolo Bonzinibcdc1852012-04-12 14:00:55 +0200251
Paolo Bonzini0ceb8492014-07-07 15:18:04 +0200252 aio_set_dispatching(ctx, was_dispatching);
Stefan Hajnoczi164a1012013-04-11 16:56:50 +0200253 return progress;
aliguoria76bab42008-09-22 19:17:18 +0000254}