blob: 7d66048b01840325ee2738777de097b9670e0c7c [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,
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) {
Paolo Bonzinie3713e02012-09-24 14:57:41 +020059 g_source_remove_poll(&ctx->source, &node->pfd);
60
aliguoria76bab42008-09-22 19:17:18 +000061 /* If the lock is held, just mark the node as deleted */
Paolo Bonzinicd9ba1e2012-09-24 14:57:22 +020062 if (ctx->walking_handlers) {
aliguoria76bab42008-09-22 19:17:18 +000063 node->deleted = 1;
Paolo Bonzinicd9ba1e2012-09-24 14:57:22 +020064 node->pfd.revents = 0;
65 } else {
aliguoria76bab42008-09-22 19:17:18 +000066 /* Otherwise, delete it for real. We can't just mark it as
67 * deleted because deleted nodes are only cleaned up after
68 * releasing the walking_handlers lock.
69 */
Blue Swirl72cf2d42009-09-12 07:36:22 +000070 QLIST_REMOVE(node, node);
Anthony Liguori7267c092011-08-20 22:09:37 -050071 g_free(node);
aliguoria76bab42008-09-22 19:17:18 +000072 }
73 }
74 } else {
75 if (node == NULL) {
76 /* Alloc and insert if it's not already there */
Anthony Liguori7267c092011-08-20 22:09:37 -050077 node = g_malloc0(sizeof(AioHandler));
Paolo Bonzinicd9ba1e2012-09-24 14:57:22 +020078 node->pfd.fd = fd;
Paolo Bonzinia915f4b2012-09-13 12:28:51 +020079 QLIST_INSERT_HEAD(&ctx->aio_handlers, node, node);
Paolo Bonzinie3713e02012-09-24 14:57:41 +020080
81 g_source_add_poll(&ctx->source, &node->pfd);
aliguoria76bab42008-09-22 19:17:18 +000082 }
83 /* Update handler with latest information */
84 node->io_read = io_read;
85 node->io_write = io_write;
aliguoria76bab42008-09-22 19:17:18 +000086 node->opaque = opaque;
Stefan Hajnoczi6b5f8762013-02-20 11:28:32 +010087 node->pollfds_idx = -1;
Paolo Bonzinicd9ba1e2012-09-24 14:57:22 +020088
Stefan Hajnoczib5a01a72013-02-20 11:28:33 +010089 node->pfd.events = (io_read ? G_IO_IN | G_IO_HUP | G_IO_ERR : 0);
90 node->pfd.events |= (io_write ? G_IO_OUT | G_IO_ERR : 0);
aliguoria76bab42008-09-22 19:17:18 +000091 }
Paolo Bonzini7ed2b242012-09-25 10:22:39 +020092
93 aio_notify(ctx);
aliguoria76bab42008-09-22 19:17:18 +000094}
95
Paolo Bonzinia915f4b2012-09-13 12:28:51 +020096void aio_set_event_notifier(AioContext *ctx,
97 EventNotifier *notifier,
98 EventNotifierHandler *io_read,
99 AioFlushEventNotifierHandler *io_flush)
Paolo Bonzini9958c352012-06-09 03:44:00 +0200100{
Paolo Bonzinia915f4b2012-09-13 12:28:51 +0200101 aio_set_fd_handler(ctx, event_notifier_get_fd(notifier),
102 (IOHandler *)io_read, NULL,
103 (AioFlushHandler *)io_flush, notifier);
Paolo Bonzini9958c352012-06-09 03:44:00 +0200104}
105
Paolo Bonzinicd9ba1e2012-09-24 14:57:22 +0200106bool aio_pending(AioContext *ctx)
107{
108 AioHandler *node;
109
110 QLIST_FOREACH(node, &ctx->aio_handlers, node) {
111 int revents;
112
Paolo Bonzinicd9ba1e2012-09-24 14:57:22 +0200113 revents = node->pfd.revents & node->pfd.events;
114 if (revents & (G_IO_IN | G_IO_HUP | G_IO_ERR) && node->io_read) {
115 return true;
116 }
117 if (revents & (G_IO_OUT | G_IO_ERR) && node->io_write) {
118 return true;
119 }
120 }
121
122 return false;
123}
124
Stefan Hajnoczid0c8d2c2013-02-20 11:28:31 +0100125static bool aio_dispatch(AioContext *ctx)
aliguoria76bab42008-09-22 19:17:18 +0000126{
Paolo Bonzini9eb0bfc2012-04-12 14:00:56 +0200127 AioHandler *node;
Stefan Hajnoczid0c8d2c2013-02-20 11:28:31 +0100128 bool progress = false;
aliguoria76bab42008-09-22 19:17:18 +0000129
Kevin Wolf8febfa22009-10-22 17:54:36 +0200130 /*
Paolo Bonzinicd9ba1e2012-09-24 14:57:22 +0200131 * We have to walk very carefully in case qemu_aio_set_fd_handler is
132 * called while we're walking.
133 */
134 node = QLIST_FIRST(&ctx->aio_handlers);
135 while (node) {
136 AioHandler *tmp;
137 int revents;
138
139 ctx->walking_handlers++;
140
141 revents = node->pfd.revents & node->pfd.events;
142 node->pfd.revents = 0;
143
Stefan Hajnoczid0c8d2c2013-02-20 11:28:31 +0100144 if (!node->deleted &&
145 (revents & (G_IO_IN | G_IO_HUP | G_IO_ERR)) &&
146 node->io_read) {
Paolo Bonzinicd9ba1e2012-09-24 14:57:22 +0200147 node->io_read(node->opaque);
Stefan Hajnoczi164a1012013-04-11 16:56:50 +0200148
149 /* aio_notify() does not count as progress */
150 if (node->opaque != &ctx->notifier) {
151 progress = true;
152 }
Paolo Bonzinicd9ba1e2012-09-24 14:57:22 +0200153 }
Stefan Hajnoczid0c8d2c2013-02-20 11:28:31 +0100154 if (!node->deleted &&
155 (revents & (G_IO_OUT | G_IO_ERR)) &&
156 node->io_write) {
Paolo Bonzinicd9ba1e2012-09-24 14:57:22 +0200157 node->io_write(node->opaque);
158 progress = true;
159 }
160
161 tmp = node;
162 node = QLIST_NEXT(node, node);
163
164 ctx->walking_handlers--;
165
166 if (!ctx->walking_handlers && tmp->deleted) {
167 QLIST_REMOVE(tmp, node);
168 g_free(tmp);
169 }
170 }
Stefan Hajnoczid0c8d2c2013-02-20 11:28:31 +0100171 return progress;
172}
173
174bool aio_poll(AioContext *ctx, bool blocking)
175{
Stefan Hajnoczid0c8d2c2013-02-20 11:28:31 +0100176 AioHandler *node;
Stefan Hajnoczid0c8d2c2013-02-20 11:28:31 +0100177 int ret;
Stefan Hajnoczi164a1012013-04-11 16:56:50 +0200178 bool progress;
Stefan Hajnoczid0c8d2c2013-02-20 11:28:31 +0100179
180 progress = false;
181
182 /*
183 * If there are callbacks left that have been queued, we need to call them.
184 * Do not call select in this case, because it is possible that the caller
185 * does not need a complete flush (as is the case for qemu_aio_wait loops).
186 */
187 if (aio_bh_poll(ctx)) {
188 blocking = false;
189 progress = true;
190 }
191
192 if (aio_dispatch(ctx)) {
193 progress = true;
194 }
Paolo Bonzinicd9ba1e2012-09-24 14:57:22 +0200195
Paolo Bonzini7c0628b2012-09-24 14:37:53 +0200196 if (progress && !blocking) {
Paolo Bonzinibcdc1852012-04-12 14:00:55 +0200197 return true;
Paolo Bonzinibafbd6a2012-04-12 14:00:54 +0200198 }
Kevin Wolf8febfa22009-10-22 17:54:36 +0200199
Paolo Bonzinia915f4b2012-09-13 12:28:51 +0200200 ctx->walking_handlers++;
aliguoria76bab42008-09-22 19:17:18 +0000201
Stefan Hajnoczi6b5f8762013-02-20 11:28:32 +0100202 g_array_set_size(ctx->pollfds, 0);
Paolo Bonzini9eb0bfc2012-04-12 14:00:56 +0200203
Stefan Hajnoczi6b5f8762013-02-20 11:28:32 +0100204 /* fill pollfds */
Paolo Bonzinia915f4b2012-09-13 12:28:51 +0200205 QLIST_FOREACH(node, &ctx->aio_handlers, node) {
Stefan Hajnoczi6b5f8762013-02-20 11:28:32 +0100206 node->pollfds_idx = -1;
Stefan Hajnoczi6b5f8762013-02-20 11:28:32 +0100207 if (!node->deleted && node->pfd.events) {
208 GPollFD pfd = {
209 .fd = node->pfd.fd,
210 .events = node->pfd.events,
211 };
212 node->pollfds_idx = ctx->pollfds->len;
213 g_array_append_val(ctx->pollfds, pfd);
Paolo Bonzini9eb0bfc2012-04-12 14:00:56 +0200214 }
215 }
216
Paolo Bonzinia915f4b2012-09-13 12:28:51 +0200217 ctx->walking_handlers--;
Paolo Bonzini9eb0bfc2012-04-12 14:00:56 +0200218
Stefan Hajnoczi164a1012013-04-11 16:56:50 +0200219 /* early return if we only have the aio_notify() fd */
220 if (ctx->pollfds->len == 1) {
Paolo Bonzini7c0628b2012-09-24 14:37:53 +0200221 return progress;
Paolo Bonzini9eb0bfc2012-04-12 14:00:56 +0200222 }
223
224 /* wait until next event */
Stefan Hajnoczi6b5f8762013-02-20 11:28:32 +0100225 ret = g_poll((GPollFD *)ctx->pollfds->data,
226 ctx->pollfds->len,
227 blocking ? -1 : 0);
Paolo Bonzini9eb0bfc2012-04-12 14:00:56 +0200228
229 /* if we have any readable fds, dispatch event */
230 if (ret > 0) {
Stefan Hajnoczi6b5f8762013-02-20 11:28:32 +0100231 QLIST_FOREACH(node, &ctx->aio_handlers, node) {
232 if (node->pollfds_idx != -1) {
233 GPollFD *pfd = &g_array_index(ctx->pollfds, GPollFD,
234 node->pollfds_idx);
235 node->pfd.revents = pfd->revents;
Paolo Bonzinibcdc1852012-04-12 14:00:55 +0200236 }
Stefan Hajnoczi6b5f8762013-02-20 11:28:32 +0100237 }
238 if (aio_dispatch(ctx)) {
239 progress = true;
aliguoria76bab42008-09-22 19:17:18 +0000240 }
Paolo Bonzini9eb0bfc2012-04-12 14:00:56 +0200241 }
Paolo Bonzinibcdc1852012-04-12 14:00:55 +0200242
Stefan Hajnoczi164a1012013-04-11 16:56:50 +0200243 return progress;
aliguoria76bab42008-09-22 19:17:18 +0000244}