aliguori | a76bab4 | 2008-09-22 19:17:18 +0000 | [diff] [blame] | 1 | /* |
| 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 Bonzini | 6b620ca | 2012-01-13 17:44:23 +0100 | [diff] [blame] | 12 | * Contributions after 2012-01-13 are licensed under the terms of the |
| 13 | * GNU GPL, version 2 or (at your option) any later version. |
aliguori | a76bab4 | 2008-09-22 19:17:18 +0000 | [diff] [blame] | 14 | */ |
| 15 | |
| 16 | #include "qemu-common.h" |
Paolo Bonzini | 737e150 | 2012-12-17 18:19:44 +0100 | [diff] [blame] | 17 | #include "block/block.h" |
Paolo Bonzini | 1de7afc | 2012-12-17 18:20:00 +0100 | [diff] [blame] | 18 | #include "qemu/queue.h" |
| 19 | #include "qemu/sockets.h" |
aliguori | a76bab4 | 2008-09-22 19:17:18 +0000 | [diff] [blame] | 20 | |
aliguori | a76bab4 | 2008-09-22 19:17:18 +0000 | [diff] [blame] | 21 | struct AioHandler |
| 22 | { |
Paolo Bonzini | cd9ba1e | 2012-09-24 14:57:22 +0200 | [diff] [blame] | 23 | GPollFD pfd; |
aliguori | a76bab4 | 2008-09-22 19:17:18 +0000 | [diff] [blame] | 24 | IOHandler *io_read; |
| 25 | IOHandler *io_write; |
aliguori | a76bab4 | 2008-09-22 19:17:18 +0000 | [diff] [blame] | 26 | int deleted; |
Stefan Hajnoczi | 6b5f876 | 2013-02-20 11:28:32 +0100 | [diff] [blame] | 27 | int pollfds_idx; |
aliguori | a76bab4 | 2008-09-22 19:17:18 +0000 | [diff] [blame] | 28 | void *opaque; |
Blue Swirl | 72cf2d4 | 2009-09-12 07:36:22 +0000 | [diff] [blame] | 29 | QLIST_ENTRY(AioHandler) node; |
aliguori | a76bab4 | 2008-09-22 19:17:18 +0000 | [diff] [blame] | 30 | }; |
| 31 | |
Paolo Bonzini | a915f4b | 2012-09-13 12:28:51 +0200 | [diff] [blame] | 32 | static AioHandler *find_aio_handler(AioContext *ctx, int fd) |
aliguori | a76bab4 | 2008-09-22 19:17:18 +0000 | [diff] [blame] | 33 | { |
| 34 | AioHandler *node; |
| 35 | |
Paolo Bonzini | a915f4b | 2012-09-13 12:28:51 +0200 | [diff] [blame] | 36 | QLIST_FOREACH(node, &ctx->aio_handlers, node) { |
Paolo Bonzini | cd9ba1e | 2012-09-24 14:57:22 +0200 | [diff] [blame] | 37 | if (node->pfd.fd == fd) |
Alexander Graf | 79d5ca5 | 2009-05-06 02:58:48 +0200 | [diff] [blame] | 38 | if (!node->deleted) |
| 39 | return node; |
aliguori | a76bab4 | 2008-09-22 19:17:18 +0000 | [diff] [blame] | 40 | } |
| 41 | |
| 42 | return NULL; |
| 43 | } |
| 44 | |
Paolo Bonzini | a915f4b | 2012-09-13 12:28:51 +0200 | [diff] [blame] | 45 | void aio_set_fd_handler(AioContext *ctx, |
| 46 | int fd, |
| 47 | IOHandler *io_read, |
| 48 | IOHandler *io_write, |
| 49 | AioFlushHandler *io_flush, |
| 50 | void *opaque) |
aliguori | a76bab4 | 2008-09-22 19:17:18 +0000 | [diff] [blame] | 51 | { |
| 52 | AioHandler *node; |
| 53 | |
Paolo Bonzini | a915f4b | 2012-09-13 12:28:51 +0200 | [diff] [blame] | 54 | node = find_aio_handler(ctx, fd); |
aliguori | a76bab4 | 2008-09-22 19:17:18 +0000 | [diff] [blame] | 55 | |
| 56 | /* Are we deleting the fd handler? */ |
| 57 | if (!io_read && !io_write) { |
| 58 | if (node) { |
Paolo Bonzini | e3713e0 | 2012-09-24 14:57:41 +0200 | [diff] [blame] | 59 | g_source_remove_poll(&ctx->source, &node->pfd); |
| 60 | |
aliguori | a76bab4 | 2008-09-22 19:17:18 +0000 | [diff] [blame] | 61 | /* If the lock is held, just mark the node as deleted */ |
Paolo Bonzini | cd9ba1e | 2012-09-24 14:57:22 +0200 | [diff] [blame] | 62 | if (ctx->walking_handlers) { |
aliguori | a76bab4 | 2008-09-22 19:17:18 +0000 | [diff] [blame] | 63 | node->deleted = 1; |
Paolo Bonzini | cd9ba1e | 2012-09-24 14:57:22 +0200 | [diff] [blame] | 64 | node->pfd.revents = 0; |
| 65 | } else { |
aliguori | a76bab4 | 2008-09-22 19:17:18 +0000 | [diff] [blame] | 66 | /* 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 Swirl | 72cf2d4 | 2009-09-12 07:36:22 +0000 | [diff] [blame] | 70 | QLIST_REMOVE(node, node); |
Anthony Liguori | 7267c09 | 2011-08-20 22:09:37 -0500 | [diff] [blame] | 71 | g_free(node); |
aliguori | a76bab4 | 2008-09-22 19:17:18 +0000 | [diff] [blame] | 72 | } |
| 73 | } |
| 74 | } else { |
| 75 | if (node == NULL) { |
| 76 | /* Alloc and insert if it's not already there */ |
Anthony Liguori | 7267c09 | 2011-08-20 22:09:37 -0500 | [diff] [blame] | 77 | node = g_malloc0(sizeof(AioHandler)); |
Paolo Bonzini | cd9ba1e | 2012-09-24 14:57:22 +0200 | [diff] [blame] | 78 | node->pfd.fd = fd; |
Paolo Bonzini | a915f4b | 2012-09-13 12:28:51 +0200 | [diff] [blame] | 79 | QLIST_INSERT_HEAD(&ctx->aio_handlers, node, node); |
Paolo Bonzini | e3713e0 | 2012-09-24 14:57:41 +0200 | [diff] [blame] | 80 | |
| 81 | g_source_add_poll(&ctx->source, &node->pfd); |
aliguori | a76bab4 | 2008-09-22 19:17:18 +0000 | [diff] [blame] | 82 | } |
| 83 | /* Update handler with latest information */ |
| 84 | node->io_read = io_read; |
| 85 | node->io_write = io_write; |
aliguori | a76bab4 | 2008-09-22 19:17:18 +0000 | [diff] [blame] | 86 | node->opaque = opaque; |
Stefan Hajnoczi | 6b5f876 | 2013-02-20 11:28:32 +0100 | [diff] [blame] | 87 | node->pollfds_idx = -1; |
Paolo Bonzini | cd9ba1e | 2012-09-24 14:57:22 +0200 | [diff] [blame] | 88 | |
Stefan Hajnoczi | b5a01a7 | 2013-02-20 11:28:33 +0100 | [diff] [blame] | 89 | 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); |
aliguori | a76bab4 | 2008-09-22 19:17:18 +0000 | [diff] [blame] | 91 | } |
Paolo Bonzini | 7ed2b24 | 2012-09-25 10:22:39 +0200 | [diff] [blame] | 92 | |
| 93 | aio_notify(ctx); |
aliguori | a76bab4 | 2008-09-22 19:17:18 +0000 | [diff] [blame] | 94 | } |
| 95 | |
Paolo Bonzini | a915f4b | 2012-09-13 12:28:51 +0200 | [diff] [blame] | 96 | void aio_set_event_notifier(AioContext *ctx, |
| 97 | EventNotifier *notifier, |
| 98 | EventNotifierHandler *io_read, |
| 99 | AioFlushEventNotifierHandler *io_flush) |
Paolo Bonzini | 9958c35 | 2012-06-09 03:44:00 +0200 | [diff] [blame] | 100 | { |
Paolo Bonzini | a915f4b | 2012-09-13 12:28:51 +0200 | [diff] [blame] | 101 | aio_set_fd_handler(ctx, event_notifier_get_fd(notifier), |
| 102 | (IOHandler *)io_read, NULL, |
| 103 | (AioFlushHandler *)io_flush, notifier); |
Paolo Bonzini | 9958c35 | 2012-06-09 03:44:00 +0200 | [diff] [blame] | 104 | } |
| 105 | |
Paolo Bonzini | cd9ba1e | 2012-09-24 14:57:22 +0200 | [diff] [blame] | 106 | bool aio_pending(AioContext *ctx) |
| 107 | { |
| 108 | AioHandler *node; |
| 109 | |
| 110 | QLIST_FOREACH(node, &ctx->aio_handlers, node) { |
| 111 | int revents; |
| 112 | |
Paolo Bonzini | cd9ba1e | 2012-09-24 14:57:22 +0200 | [diff] [blame] | 113 | 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 Hajnoczi | d0c8d2c | 2013-02-20 11:28:31 +0100 | [diff] [blame] | 125 | static bool aio_dispatch(AioContext *ctx) |
aliguori | a76bab4 | 2008-09-22 19:17:18 +0000 | [diff] [blame] | 126 | { |
Paolo Bonzini | 9eb0bfc | 2012-04-12 14:00:56 +0200 | [diff] [blame] | 127 | AioHandler *node; |
Stefan Hajnoczi | d0c8d2c | 2013-02-20 11:28:31 +0100 | [diff] [blame] | 128 | bool progress = false; |
aliguori | a76bab4 | 2008-09-22 19:17:18 +0000 | [diff] [blame] | 129 | |
Kevin Wolf | 8febfa2 | 2009-10-22 17:54:36 +0200 | [diff] [blame] | 130 | /* |
Paolo Bonzini | cd9ba1e | 2012-09-24 14:57:22 +0200 | [diff] [blame] | 131 | * 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 Hajnoczi | d0c8d2c | 2013-02-20 11:28:31 +0100 | [diff] [blame] | 144 | if (!node->deleted && |
| 145 | (revents & (G_IO_IN | G_IO_HUP | G_IO_ERR)) && |
| 146 | node->io_read) { |
Paolo Bonzini | cd9ba1e | 2012-09-24 14:57:22 +0200 | [diff] [blame] | 147 | node->io_read(node->opaque); |
Stefan Hajnoczi | 164a101 | 2013-04-11 16:56:50 +0200 | [diff] [blame] | 148 | |
| 149 | /* aio_notify() does not count as progress */ |
| 150 | if (node->opaque != &ctx->notifier) { |
| 151 | progress = true; |
| 152 | } |
Paolo Bonzini | cd9ba1e | 2012-09-24 14:57:22 +0200 | [diff] [blame] | 153 | } |
Stefan Hajnoczi | d0c8d2c | 2013-02-20 11:28:31 +0100 | [diff] [blame] | 154 | if (!node->deleted && |
| 155 | (revents & (G_IO_OUT | G_IO_ERR)) && |
| 156 | node->io_write) { |
Paolo Bonzini | cd9ba1e | 2012-09-24 14:57:22 +0200 | [diff] [blame] | 157 | 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 Hajnoczi | d0c8d2c | 2013-02-20 11:28:31 +0100 | [diff] [blame] | 171 | return progress; |
| 172 | } |
| 173 | |
| 174 | bool aio_poll(AioContext *ctx, bool blocking) |
| 175 | { |
Stefan Hajnoczi | d0c8d2c | 2013-02-20 11:28:31 +0100 | [diff] [blame] | 176 | AioHandler *node; |
Stefan Hajnoczi | d0c8d2c | 2013-02-20 11:28:31 +0100 | [diff] [blame] | 177 | int ret; |
Stefan Hajnoczi | 164a101 | 2013-04-11 16:56:50 +0200 | [diff] [blame] | 178 | bool progress; |
Stefan Hajnoczi | d0c8d2c | 2013-02-20 11:28:31 +0100 | [diff] [blame] | 179 | |
| 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 Bonzini | cd9ba1e | 2012-09-24 14:57:22 +0200 | [diff] [blame] | 195 | |
Paolo Bonzini | 7c0628b | 2012-09-24 14:37:53 +0200 | [diff] [blame] | 196 | if (progress && !blocking) { |
Paolo Bonzini | bcdc185 | 2012-04-12 14:00:55 +0200 | [diff] [blame] | 197 | return true; |
Paolo Bonzini | bafbd6a | 2012-04-12 14:00:54 +0200 | [diff] [blame] | 198 | } |
Kevin Wolf | 8febfa2 | 2009-10-22 17:54:36 +0200 | [diff] [blame] | 199 | |
Paolo Bonzini | a915f4b | 2012-09-13 12:28:51 +0200 | [diff] [blame] | 200 | ctx->walking_handlers++; |
aliguori | a76bab4 | 2008-09-22 19:17:18 +0000 | [diff] [blame] | 201 | |
Stefan Hajnoczi | 6b5f876 | 2013-02-20 11:28:32 +0100 | [diff] [blame] | 202 | g_array_set_size(ctx->pollfds, 0); |
Paolo Bonzini | 9eb0bfc | 2012-04-12 14:00:56 +0200 | [diff] [blame] | 203 | |
Stefan Hajnoczi | 6b5f876 | 2013-02-20 11:28:32 +0100 | [diff] [blame] | 204 | /* fill pollfds */ |
Paolo Bonzini | a915f4b | 2012-09-13 12:28:51 +0200 | [diff] [blame] | 205 | QLIST_FOREACH(node, &ctx->aio_handlers, node) { |
Stefan Hajnoczi | 6b5f876 | 2013-02-20 11:28:32 +0100 | [diff] [blame] | 206 | node->pollfds_idx = -1; |
Stefan Hajnoczi | 6b5f876 | 2013-02-20 11:28:32 +0100 | [diff] [blame] | 207 | 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 Bonzini | 9eb0bfc | 2012-04-12 14:00:56 +0200 | [diff] [blame] | 214 | } |
| 215 | } |
| 216 | |
Paolo Bonzini | a915f4b | 2012-09-13 12:28:51 +0200 | [diff] [blame] | 217 | ctx->walking_handlers--; |
Paolo Bonzini | 9eb0bfc | 2012-04-12 14:00:56 +0200 | [diff] [blame] | 218 | |
Stefan Hajnoczi | 164a101 | 2013-04-11 16:56:50 +0200 | [diff] [blame] | 219 | /* early return if we only have the aio_notify() fd */ |
| 220 | if (ctx->pollfds->len == 1) { |
Paolo Bonzini | 7c0628b | 2012-09-24 14:37:53 +0200 | [diff] [blame] | 221 | return progress; |
Paolo Bonzini | 9eb0bfc | 2012-04-12 14:00:56 +0200 | [diff] [blame] | 222 | } |
| 223 | |
| 224 | /* wait until next event */ |
Stefan Hajnoczi | 6b5f876 | 2013-02-20 11:28:32 +0100 | [diff] [blame] | 225 | ret = g_poll((GPollFD *)ctx->pollfds->data, |
| 226 | ctx->pollfds->len, |
| 227 | blocking ? -1 : 0); |
Paolo Bonzini | 9eb0bfc | 2012-04-12 14:00:56 +0200 | [diff] [blame] | 228 | |
| 229 | /* if we have any readable fds, dispatch event */ |
| 230 | if (ret > 0) { |
Stefan Hajnoczi | 6b5f876 | 2013-02-20 11:28:32 +0100 | [diff] [blame] | 231 | 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 Bonzini | bcdc185 | 2012-04-12 14:00:55 +0200 | [diff] [blame] | 236 | } |
Stefan Hajnoczi | 6b5f876 | 2013-02-20 11:28:32 +0100 | [diff] [blame] | 237 | } |
| 238 | if (aio_dispatch(ctx)) { |
| 239 | progress = true; |
aliguori | a76bab4 | 2008-09-22 19:17:18 +0000 | [diff] [blame] | 240 | } |
Paolo Bonzini | 9eb0bfc | 2012-04-12 14:00:56 +0200 | [diff] [blame] | 241 | } |
Paolo Bonzini | bcdc185 | 2012-04-12 14:00:55 +0200 | [diff] [blame] | 242 | |
Stefan Hajnoczi | 164a101 | 2013-04-11 16:56:50 +0200 | [diff] [blame] | 243 | return progress; |
aliguori | a76bab4 | 2008-09-22 19:17:18 +0000 | [diff] [blame] | 244 | } |