]>
Commit | Line | Data |
---|---|---|
a76bab49 AL |
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 | * | |
6b620ca3 PB |
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. | |
a76bab49 AL |
14 | */ |
15 | ||
16 | #include "qemu-common.h" | |
17 | #include "block.h" | |
72cf2d4f | 18 | #include "qemu-queue.h" |
a76bab49 AL |
19 | #include "qemu_socket.h" |
20 | ||
21 | typedef struct AioHandler AioHandler; | |
22 | ||
23 | /* The list of registered AIO handlers */ | |
72cf2d4f | 24 | static QLIST_HEAD(, AioHandler) aio_handlers; |
a76bab49 AL |
25 | |
26 | /* This is a simple lock used to protect the aio_handlers list. Specifically, | |
27 | * it's used to ensure that no callbacks are removed while we're walking and | |
28 | * dispatching callbacks. | |
29 | */ | |
30 | static int walking_handlers; | |
31 | ||
32 | struct AioHandler | |
33 | { | |
34 | int fd; | |
35 | IOHandler *io_read; | |
36 | IOHandler *io_write; | |
37 | AioFlushHandler *io_flush; | |
8febfa26 | 38 | AioProcessQueue *io_process_queue; |
a76bab49 AL |
39 | int deleted; |
40 | void *opaque; | |
72cf2d4f | 41 | QLIST_ENTRY(AioHandler) node; |
a76bab49 AL |
42 | }; |
43 | ||
44 | static AioHandler *find_aio_handler(int fd) | |
45 | { | |
46 | AioHandler *node; | |
47 | ||
72cf2d4f | 48 | QLIST_FOREACH(node, &aio_handlers, node) { |
a76bab49 | 49 | if (node->fd == fd) |
79d5ca56 AG |
50 | if (!node->deleted) |
51 | return node; | |
a76bab49 AL |
52 | } |
53 | ||
54 | return NULL; | |
55 | } | |
56 | ||
57 | int qemu_aio_set_fd_handler(int fd, | |
58 | IOHandler *io_read, | |
59 | IOHandler *io_write, | |
60 | AioFlushHandler *io_flush, | |
8febfa26 | 61 | AioProcessQueue *io_process_queue, |
a76bab49 AL |
62 | void *opaque) |
63 | { | |
64 | AioHandler *node; | |
65 | ||
66 | node = find_aio_handler(fd); | |
67 | ||
68 | /* Are we deleting the fd handler? */ | |
69 | if (!io_read && !io_write) { | |
70 | if (node) { | |
71 | /* If the lock is held, just mark the node as deleted */ | |
72 | if (walking_handlers) | |
73 | node->deleted = 1; | |
74 | else { | |
75 | /* Otherwise, delete it for real. We can't just mark it as | |
76 | * deleted because deleted nodes are only cleaned up after | |
77 | * releasing the walking_handlers lock. | |
78 | */ | |
72cf2d4f | 79 | QLIST_REMOVE(node, node); |
7267c094 | 80 | g_free(node); |
a76bab49 AL |
81 | } |
82 | } | |
83 | } else { | |
84 | if (node == NULL) { | |
85 | /* Alloc and insert if it's not already there */ | |
7267c094 | 86 | node = g_malloc0(sizeof(AioHandler)); |
a76bab49 | 87 | node->fd = fd; |
72cf2d4f | 88 | QLIST_INSERT_HEAD(&aio_handlers, node, node); |
a76bab49 AL |
89 | } |
90 | /* Update handler with latest information */ | |
91 | node->io_read = io_read; | |
92 | node->io_write = io_write; | |
93 | node->io_flush = io_flush; | |
8febfa26 | 94 | node->io_process_queue = io_process_queue; |
a76bab49 AL |
95 | node->opaque = opaque; |
96 | } | |
97 | ||
98 | qemu_set_fd_handler2(fd, NULL, io_read, io_write, opaque); | |
99 | ||
100 | return 0; | |
101 | } | |
102 | ||
103 | void qemu_aio_flush(void) | |
104 | { | |
105 | AioHandler *node; | |
106 | int ret; | |
107 | ||
108 | do { | |
109 | ret = 0; | |
110 | ||
986c28d6 AA |
111 | /* |
112 | * If there are pending emulated aio start them now so flush | |
113 | * will be able to return 1. | |
114 | */ | |
115 | qemu_aio_wait(); | |
116 | ||
72cf2d4f | 117 | QLIST_FOREACH(node, &aio_handlers, node) { |
c53a7285 AK |
118 | if (node->io_flush) { |
119 | ret |= node->io_flush(node->opaque); | |
120 | } | |
a76bab49 | 121 | } |
6e5d97d0 | 122 | } while (qemu_bh_poll() || ret > 0); |
a76bab49 AL |
123 | } |
124 | ||
8febfa26 KW |
125 | int qemu_aio_process_queue(void) |
126 | { | |
127 | AioHandler *node; | |
128 | int ret = 0; | |
129 | ||
130 | walking_handlers = 1; | |
131 | ||
132 | QLIST_FOREACH(node, &aio_handlers, node) { | |
133 | if (node->io_process_queue) { | |
134 | if (node->io_process_queue(node->opaque)) { | |
135 | ret = 1; | |
136 | } | |
137 | } | |
138 | } | |
139 | ||
140 | walking_handlers = 0; | |
141 | ||
142 | return ret; | |
143 | } | |
144 | ||
a76bab49 AL |
145 | void qemu_aio_wait(void) |
146 | { | |
147 | int ret; | |
148 | ||
149 | if (qemu_bh_poll()) | |
150 | return; | |
151 | ||
8febfa26 KW |
152 | /* |
153 | * If there are callbacks left that have been queued, we need to call then. | |
154 | * Return afterwards to avoid waiting needlessly in select(). | |
155 | */ | |
156 | if (qemu_aio_process_queue()) | |
157 | return; | |
158 | ||
a76bab49 AL |
159 | do { |
160 | AioHandler *node; | |
161 | fd_set rdfds, wrfds; | |
162 | int max_fd = -1; | |
163 | ||
164 | walking_handlers = 1; | |
165 | ||
f71903d0 AL |
166 | FD_ZERO(&rdfds); |
167 | FD_ZERO(&wrfds); | |
168 | ||
a76bab49 | 169 | /* fill fd sets */ |
72cf2d4f | 170 | QLIST_FOREACH(node, &aio_handlers, node) { |
a76bab49 AL |
171 | /* If there aren't pending AIO operations, don't invoke callbacks. |
172 | * Otherwise, if there are no AIO requests, qemu_aio_wait() would | |
173 | * wait indefinitely. | |
174 | */ | |
175 | if (node->io_flush && node->io_flush(node->opaque) == 0) | |
176 | continue; | |
177 | ||
178 | if (!node->deleted && node->io_read) { | |
179 | FD_SET(node->fd, &rdfds); | |
180 | max_fd = MAX(max_fd, node->fd + 1); | |
181 | } | |
182 | if (!node->deleted && node->io_write) { | |
183 | FD_SET(node->fd, &wrfds); | |
184 | max_fd = MAX(max_fd, node->fd + 1); | |
185 | } | |
186 | } | |
187 | ||
188 | walking_handlers = 0; | |
189 | ||
190 | /* No AIO operations? Get us out of here */ | |
191 | if (max_fd == -1) | |
192 | break; | |
193 | ||
194 | /* wait until next event */ | |
195 | ret = select(max_fd, &rdfds, &wrfds, NULL, NULL); | |
196 | if (ret == -1 && errno == EINTR) | |
197 | continue; | |
198 | ||
199 | /* if we have any readable fds, dispatch event */ | |
200 | if (ret > 0) { | |
201 | walking_handlers = 1; | |
202 | ||
203 | /* we have to walk very carefully in case | |
204 | * qemu_aio_set_fd_handler is called while we're walking */ | |
72cf2d4f | 205 | node = QLIST_FIRST(&aio_handlers); |
a76bab49 AL |
206 | while (node) { |
207 | AioHandler *tmp; | |
208 | ||
209 | if (!node->deleted && | |
210 | FD_ISSET(node->fd, &rdfds) && | |
211 | node->io_read) { | |
212 | node->io_read(node->opaque); | |
213 | } | |
214 | if (!node->deleted && | |
215 | FD_ISSET(node->fd, &wrfds) && | |
216 | node->io_write) { | |
217 | node->io_write(node->opaque); | |
218 | } | |
219 | ||
220 | tmp = node; | |
72cf2d4f | 221 | node = QLIST_NEXT(node, node); |
a76bab49 AL |
222 | |
223 | if (tmp->deleted) { | |
72cf2d4f | 224 | QLIST_REMOVE(tmp, node); |
7267c094 | 225 | g_free(tmp); |
a76bab49 AL |
226 | } |
227 | } | |
228 | ||
229 | walking_handlers = 0; | |
230 | } | |
231 | } while (ret == 0); | |
232 | } |