blob: b68eccd40ccc6d4c2af25349f61694f3cddb4d16 [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;
26 AioFlushHandler *io_flush;
27 int deleted;
Stefan Hajnoczi6b5f8762013-02-20 11:28:32 +010028 int pollfds_idx;
aliguoria76bab42008-09-22 19:17:18 +000029 void *opaque;
Blue Swirl72cf2d42009-09-12 07:36:22 +000030 QLIST_ENTRY(AioHandler) node;
aliguoria76bab42008-09-22 19:17:18 +000031};
32
Paolo Bonzinia915f4b2012-09-13 12:28:51 +020033static AioHandler *find_aio_handler(AioContext *ctx, int fd)
aliguoria76bab42008-09-22 19:17:18 +000034{
35 AioHandler *node;
36
Paolo Bonzinia915f4b2012-09-13 12:28:51 +020037 QLIST_FOREACH(node, &ctx->aio_handlers, node) {
Paolo Bonzinicd9ba1e2012-09-24 14:57:22 +020038 if (node->pfd.fd == fd)
Alexander Graf79d5ca52009-05-06 02:58:48 +020039 if (!node->deleted)
40 return node;
aliguoria76bab42008-09-22 19:17:18 +000041 }
42
43 return NULL;
44}
45
Paolo Bonzinia915f4b2012-09-13 12:28:51 +020046void aio_set_fd_handler(AioContext *ctx,
47 int fd,
48 IOHandler *io_read,
49 IOHandler *io_write,
50 AioFlushHandler *io_flush,
51 void *opaque)
aliguoria76bab42008-09-22 19:17:18 +000052{
53 AioHandler *node;
54
Paolo Bonzinia915f4b2012-09-13 12:28:51 +020055 node = find_aio_handler(ctx, fd);
aliguoria76bab42008-09-22 19:17:18 +000056
57 /* Are we deleting the fd handler? */
58 if (!io_read && !io_write) {
59 if (node) {
Paolo Bonzinie3713e02012-09-24 14:57:41 +020060 g_source_remove_poll(&ctx->source, &node->pfd);
61
aliguoria76bab42008-09-22 19:17:18 +000062 /* If the lock is held, just mark the node as deleted */
Paolo Bonzinicd9ba1e2012-09-24 14:57:22 +020063 if (ctx->walking_handlers) {
aliguoria76bab42008-09-22 19:17:18 +000064 node->deleted = 1;
Paolo Bonzinicd9ba1e2012-09-24 14:57:22 +020065 node->pfd.revents = 0;
66 } else {
aliguoria76bab42008-09-22 19:17:18 +000067 /* Otherwise, delete it for real. We can't just mark it as
68 * deleted because deleted nodes are only cleaned up after
69 * releasing the walking_handlers lock.
70 */
Blue Swirl72cf2d42009-09-12 07:36:22 +000071 QLIST_REMOVE(node, node);
Anthony Liguori7267c092011-08-20 22:09:37 -050072 g_free(node);
aliguoria76bab42008-09-22 19:17:18 +000073 }
74 }
75 } else {
76 if (node == NULL) {
77 /* Alloc and insert if it's not already there */
Anthony Liguori7267c092011-08-20 22:09:37 -050078 node = g_malloc0(sizeof(AioHandler));
Paolo Bonzinicd9ba1e2012-09-24 14:57:22 +020079 node->pfd.fd = fd;
Paolo Bonzinia915f4b2012-09-13 12:28:51 +020080 QLIST_INSERT_HEAD(&ctx->aio_handlers, node, node);
Paolo Bonzinie3713e02012-09-24 14:57:41 +020081
82 g_source_add_poll(&ctx->source, &node->pfd);
aliguoria76bab42008-09-22 19:17:18 +000083 }
84 /* Update handler with latest information */
85 node->io_read = io_read;
86 node->io_write = io_write;
87 node->io_flush = io_flush;
88 node->opaque = opaque;
Stefan Hajnoczi6b5f8762013-02-20 11:28:32 +010089 node->pollfds_idx = -1;
Paolo Bonzinicd9ba1e2012-09-24 14:57:22 +020090
Stefan Hajnoczib5a01a72013-02-20 11:28:33 +010091 node->pfd.events = (io_read ? G_IO_IN | G_IO_HUP | G_IO_ERR : 0);
92 node->pfd.events |= (io_write ? G_IO_OUT | G_IO_ERR : 0);
aliguoria76bab42008-09-22 19:17:18 +000093 }
Paolo Bonzini7ed2b242012-09-25 10:22:39 +020094
95 aio_notify(ctx);
aliguoria76bab42008-09-22 19:17:18 +000096}
97
Paolo Bonzinia915f4b2012-09-13 12:28:51 +020098void aio_set_event_notifier(AioContext *ctx,
99 EventNotifier *notifier,
100 EventNotifierHandler *io_read,
101 AioFlushEventNotifierHandler *io_flush)
Paolo Bonzini9958c352012-06-09 03:44:00 +0200102{
Paolo Bonzinia915f4b2012-09-13 12:28:51 +0200103 aio_set_fd_handler(ctx, event_notifier_get_fd(notifier),
104 (IOHandler *)io_read, NULL,
105 (AioFlushHandler *)io_flush, notifier);
Paolo Bonzini9958c352012-06-09 03:44:00 +0200106}
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
Stefan Hajnoczid0c8d2c2013-02-20 11:28:31 +0100127static bool 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 Bonzinicd9ba1e2012-09-24 14:57:22 +0200133 * We have to walk very carefully in case qemu_aio_set_fd_handler is
134 * called while we're walking.
135 */
136 node = QLIST_FIRST(&ctx->aio_handlers);
137 while (node) {
138 AioHandler *tmp;
139 int revents;
140
141 ctx->walking_handlers++;
142
143 revents = node->pfd.revents & node->pfd.events;
144 node->pfd.revents = 0;
145
Stefan Hajnoczid0c8d2c2013-02-20 11:28:31 +0100146 if (!node->deleted &&
147 (revents & (G_IO_IN | G_IO_HUP | G_IO_ERR)) &&
148 node->io_read) {
Paolo Bonzinicd9ba1e2012-09-24 14:57:22 +0200149 node->io_read(node->opaque);
150 progress = true;
151 }
Stefan Hajnoczid0c8d2c2013-02-20 11:28:31 +0100152 if (!node->deleted &&
153 (revents & (G_IO_OUT | G_IO_ERR)) &&
154 node->io_write) {
Paolo Bonzinicd9ba1e2012-09-24 14:57:22 +0200155 node->io_write(node->opaque);
156 progress = true;
157 }
158
159 tmp = node;
160 node = QLIST_NEXT(node, node);
161
162 ctx->walking_handlers--;
163
164 if (!ctx->walking_handlers && tmp->deleted) {
165 QLIST_REMOVE(tmp, node);
166 g_free(tmp);
167 }
168 }
Stefan Hajnoczid0c8d2c2013-02-20 11:28:31 +0100169 return progress;
170}
171
172bool aio_poll(AioContext *ctx, bool blocking)
173{
Stefan Hajnoczid0c8d2c2013-02-20 11:28:31 +0100174 AioHandler *node;
Stefan Hajnoczid0c8d2c2013-02-20 11:28:31 +0100175 int ret;
176 bool busy, progress;
177
178 progress = false;
179
180 /*
181 * If there are callbacks left that have been queued, we need to call them.
182 * Do not call select in this case, because it is possible that the caller
183 * does not need a complete flush (as is the case for qemu_aio_wait loops).
184 */
185 if (aio_bh_poll(ctx)) {
186 blocking = false;
187 progress = true;
188 }
189
190 if (aio_dispatch(ctx)) {
191 progress = true;
192 }
Paolo Bonzinicd9ba1e2012-09-24 14:57:22 +0200193
Paolo Bonzini7c0628b2012-09-24 14:37:53 +0200194 if (progress && !blocking) {
Paolo Bonzinibcdc1852012-04-12 14:00:55 +0200195 return true;
Paolo Bonzinibafbd6a2012-04-12 14:00:54 +0200196 }
Kevin Wolf8febfa22009-10-22 17:54:36 +0200197
Paolo Bonzinia915f4b2012-09-13 12:28:51 +0200198 ctx->walking_handlers++;
aliguoria76bab42008-09-22 19:17:18 +0000199
Stefan Hajnoczi6b5f8762013-02-20 11:28:32 +0100200 g_array_set_size(ctx->pollfds, 0);
Paolo Bonzini9eb0bfc2012-04-12 14:00:56 +0200201
Stefan Hajnoczi6b5f8762013-02-20 11:28:32 +0100202 /* fill pollfds */
Paolo Bonzini9eb0bfc2012-04-12 14:00:56 +0200203 busy = false;
Paolo Bonzinia915f4b2012-09-13 12:28:51 +0200204 QLIST_FOREACH(node, &ctx->aio_handlers, node) {
Stefan Hajnoczi6b5f8762013-02-20 11:28:32 +0100205 node->pollfds_idx = -1;
206
Paolo Bonzini9eb0bfc2012-04-12 14:00:56 +0200207 /* If there aren't pending AIO operations, don't invoke callbacks.
208 * Otherwise, if there are no AIO requests, qemu_aio_wait() would
209 * wait indefinitely.
210 */
Paolo Bonzini4231c882012-09-26 15:21:36 +0200211 if (!node->deleted && node->io_flush) {
Paolo Bonzini9eb0bfc2012-04-12 14:00:56 +0200212 if (node->io_flush(node->opaque) == 0) {
213 continue;
214 }
215 busy = true;
216 }
Stefan Hajnoczi6b5f8762013-02-20 11:28:32 +0100217 if (!node->deleted && node->pfd.events) {
218 GPollFD pfd = {
219 .fd = node->pfd.fd,
220 .events = node->pfd.events,
221 };
222 node->pollfds_idx = ctx->pollfds->len;
223 g_array_append_val(ctx->pollfds, pfd);
Paolo Bonzini9eb0bfc2012-04-12 14:00:56 +0200224 }
225 }
226
Paolo Bonzinia915f4b2012-09-13 12:28:51 +0200227 ctx->walking_handlers--;
Paolo Bonzini9eb0bfc2012-04-12 14:00:56 +0200228
229 /* No AIO operations? Get us out of here */
230 if (!busy) {
Paolo Bonzini7c0628b2012-09-24 14:37:53 +0200231 return progress;
Paolo Bonzini9eb0bfc2012-04-12 14:00:56 +0200232 }
233
234 /* wait until next event */
Stefan Hajnoczi6b5f8762013-02-20 11:28:32 +0100235 ret = g_poll((GPollFD *)ctx->pollfds->data,
236 ctx->pollfds->len,
237 blocking ? -1 : 0);
Paolo Bonzini9eb0bfc2012-04-12 14:00:56 +0200238
239 /* if we have any readable fds, dispatch event */
240 if (ret > 0) {
Stefan Hajnoczi6b5f8762013-02-20 11:28:32 +0100241 QLIST_FOREACH(node, &ctx->aio_handlers, node) {
242 if (node->pollfds_idx != -1) {
243 GPollFD *pfd = &g_array_index(ctx->pollfds, GPollFD,
244 node->pollfds_idx);
245 node->pfd.revents = pfd->revents;
Paolo Bonzinibcdc1852012-04-12 14:00:55 +0200246 }
Stefan Hajnoczi6b5f8762013-02-20 11:28:32 +0100247 }
248 if (aio_dispatch(ctx)) {
249 progress = true;
aliguoria76bab42008-09-22 19:17:18 +0000250 }
Paolo Bonzini9eb0bfc2012-04-12 14:00:56 +0200251 }
Paolo Bonzinibcdc1852012-04-12 14:00:55 +0200252
Kevin Wolf2ea9b582013-01-16 19:25:51 +0100253 assert(progress || busy);
254 return true;
aliguoria76bab42008-09-22 19:17:18 +0000255}