root / aio.c @ 50132156
History | View | Annotate | Download (5 kB)
1 | a76bab49 | aliguori | /*
|
---|---|---|---|
2 | a76bab49 | aliguori | * QEMU aio implementation
|
3 | a76bab49 | aliguori | *
|
4 | a76bab49 | aliguori | * Copyright IBM, Corp. 2008
|
5 | a76bab49 | aliguori | *
|
6 | a76bab49 | aliguori | * Authors:
|
7 | a76bab49 | aliguori | * Anthony Liguori <aliguori@us.ibm.com>
|
8 | a76bab49 | aliguori | *
|
9 | a76bab49 | aliguori | * This work is licensed under the terms of the GNU GPL, version 2. See
|
10 | a76bab49 | aliguori | * the COPYING file in the top-level directory.
|
11 | a76bab49 | aliguori | *
|
12 | a76bab49 | aliguori | */
|
13 | a76bab49 | aliguori | |
14 | a76bab49 | aliguori | #include "qemu-common.h" |
15 | a76bab49 | aliguori | #include "block.h" |
16 | 72cf2d4f | Blue Swirl | #include "qemu-queue.h" |
17 | a76bab49 | aliguori | #include "qemu_socket.h" |
18 | a76bab49 | aliguori | |
19 | a76bab49 | aliguori | typedef struct AioHandler AioHandler; |
20 | a76bab49 | aliguori | |
21 | a76bab49 | aliguori | /* The list of registered AIO handlers */
|
22 | 72cf2d4f | Blue Swirl | static QLIST_HEAD(, AioHandler) aio_handlers;
|
23 | a76bab49 | aliguori | |
24 | a76bab49 | aliguori | /* This is a simple lock used to protect the aio_handlers list. Specifically,
|
25 | a76bab49 | aliguori | * it's used to ensure that no callbacks are removed while we're walking and
|
26 | a76bab49 | aliguori | * dispatching callbacks.
|
27 | a76bab49 | aliguori | */
|
28 | a76bab49 | aliguori | static int walking_handlers; |
29 | a76bab49 | aliguori | |
30 | a76bab49 | aliguori | struct AioHandler
|
31 | a76bab49 | aliguori | { |
32 | a76bab49 | aliguori | int fd;
|
33 | a76bab49 | aliguori | IOHandler *io_read; |
34 | a76bab49 | aliguori | IOHandler *io_write; |
35 | a76bab49 | aliguori | AioFlushHandler *io_flush; |
36 | a76bab49 | aliguori | int deleted;
|
37 | a76bab49 | aliguori | void *opaque;
|
38 | 72cf2d4f | Blue Swirl | QLIST_ENTRY(AioHandler) node; |
39 | a76bab49 | aliguori | }; |
40 | a76bab49 | aliguori | |
41 | a76bab49 | aliguori | static AioHandler *find_aio_handler(int fd) |
42 | a76bab49 | aliguori | { |
43 | a76bab49 | aliguori | AioHandler *node; |
44 | a76bab49 | aliguori | |
45 | 72cf2d4f | Blue Swirl | QLIST_FOREACH(node, &aio_handlers, node) { |
46 | a76bab49 | aliguori | if (node->fd == fd)
|
47 | 79d5ca56 | Alexander Graf | if (!node->deleted)
|
48 | 79d5ca56 | Alexander Graf | return node;
|
49 | a76bab49 | aliguori | } |
50 | a76bab49 | aliguori | |
51 | a76bab49 | aliguori | return NULL; |
52 | a76bab49 | aliguori | } |
53 | a76bab49 | aliguori | |
54 | a76bab49 | aliguori | int qemu_aio_set_fd_handler(int fd, |
55 | a76bab49 | aliguori | IOHandler *io_read, |
56 | a76bab49 | aliguori | IOHandler *io_write, |
57 | a76bab49 | aliguori | AioFlushHandler *io_flush, |
58 | a76bab49 | aliguori | void *opaque)
|
59 | a76bab49 | aliguori | { |
60 | a76bab49 | aliguori | AioHandler *node; |
61 | a76bab49 | aliguori | |
62 | a76bab49 | aliguori | node = find_aio_handler(fd); |
63 | a76bab49 | aliguori | |
64 | a76bab49 | aliguori | /* Are we deleting the fd handler? */
|
65 | a76bab49 | aliguori | if (!io_read && !io_write) {
|
66 | a76bab49 | aliguori | if (node) {
|
67 | a76bab49 | aliguori | /* If the lock is held, just mark the node as deleted */
|
68 | a76bab49 | aliguori | if (walking_handlers)
|
69 | a76bab49 | aliguori | node->deleted = 1;
|
70 | a76bab49 | aliguori | else {
|
71 | a76bab49 | aliguori | /* Otherwise, delete it for real. We can't just mark it as
|
72 | a76bab49 | aliguori | * deleted because deleted nodes are only cleaned up after
|
73 | a76bab49 | aliguori | * releasing the walking_handlers lock.
|
74 | a76bab49 | aliguori | */
|
75 | 72cf2d4f | Blue Swirl | QLIST_REMOVE(node, node); |
76 | a76bab49 | aliguori | qemu_free(node); |
77 | a76bab49 | aliguori | } |
78 | a76bab49 | aliguori | } |
79 | a76bab49 | aliguori | } else {
|
80 | a76bab49 | aliguori | if (node == NULL) { |
81 | a76bab49 | aliguori | /* Alloc and insert if it's not already there */
|
82 | a76bab49 | aliguori | node = qemu_mallocz(sizeof(AioHandler));
|
83 | a76bab49 | aliguori | node->fd = fd; |
84 | 72cf2d4f | Blue Swirl | QLIST_INSERT_HEAD(&aio_handlers, node, node); |
85 | a76bab49 | aliguori | } |
86 | a76bab49 | aliguori | /* Update handler with latest information */
|
87 | a76bab49 | aliguori | node->io_read = io_read; |
88 | a76bab49 | aliguori | node->io_write = io_write; |
89 | a76bab49 | aliguori | node->io_flush = io_flush; |
90 | a76bab49 | aliguori | node->opaque = opaque; |
91 | a76bab49 | aliguori | } |
92 | a76bab49 | aliguori | |
93 | a76bab49 | aliguori | qemu_set_fd_handler2(fd, NULL, io_read, io_write, opaque);
|
94 | a76bab49 | aliguori | |
95 | a76bab49 | aliguori | return 0; |
96 | a76bab49 | aliguori | } |
97 | a76bab49 | aliguori | |
98 | a76bab49 | aliguori | void qemu_aio_flush(void) |
99 | a76bab49 | aliguori | { |
100 | a76bab49 | aliguori | AioHandler *node; |
101 | a76bab49 | aliguori | int ret;
|
102 | a76bab49 | aliguori | |
103 | a76bab49 | aliguori | do {
|
104 | a76bab49 | aliguori | ret = 0;
|
105 | a76bab49 | aliguori | |
106 | 986c28d6 | Andrea Arcangeli | /*
|
107 | 986c28d6 | Andrea Arcangeli | * If there are pending emulated aio start them now so flush
|
108 | 986c28d6 | Andrea Arcangeli | * will be able to return 1.
|
109 | 986c28d6 | Andrea Arcangeli | */
|
110 | 986c28d6 | Andrea Arcangeli | qemu_aio_wait(); |
111 | 986c28d6 | Andrea Arcangeli | |
112 | 72cf2d4f | Blue Swirl | QLIST_FOREACH(node, &aio_handlers, node) { |
113 | a76bab49 | aliguori | ret |= node->io_flush(node->opaque); |
114 | a76bab49 | aliguori | } |
115 | 6e5d97d0 | Nolan | } while (qemu_bh_poll() || ret > 0); |
116 | a76bab49 | aliguori | } |
117 | a76bab49 | aliguori | |
118 | a76bab49 | aliguori | void qemu_aio_wait(void) |
119 | a76bab49 | aliguori | { |
120 | a76bab49 | aliguori | int ret;
|
121 | a76bab49 | aliguori | |
122 | a76bab49 | aliguori | if (qemu_bh_poll())
|
123 | a76bab49 | aliguori | return;
|
124 | a76bab49 | aliguori | |
125 | a76bab49 | aliguori | do {
|
126 | a76bab49 | aliguori | AioHandler *node; |
127 | a76bab49 | aliguori | fd_set rdfds, wrfds; |
128 | a76bab49 | aliguori | int max_fd = -1; |
129 | a76bab49 | aliguori | |
130 | a76bab49 | aliguori | walking_handlers = 1;
|
131 | a76bab49 | aliguori | |
132 | f71903d0 | aliguori | FD_ZERO(&rdfds); |
133 | f71903d0 | aliguori | FD_ZERO(&wrfds); |
134 | f71903d0 | aliguori | |
135 | a76bab49 | aliguori | /* fill fd sets */
|
136 | 72cf2d4f | Blue Swirl | QLIST_FOREACH(node, &aio_handlers, node) { |
137 | a76bab49 | aliguori | /* If there aren't pending AIO operations, don't invoke callbacks.
|
138 | a76bab49 | aliguori | * Otherwise, if there are no AIO requests, qemu_aio_wait() would
|
139 | a76bab49 | aliguori | * wait indefinitely.
|
140 | a76bab49 | aliguori | */
|
141 | a76bab49 | aliguori | if (node->io_flush && node->io_flush(node->opaque) == 0) |
142 | a76bab49 | aliguori | continue;
|
143 | a76bab49 | aliguori | |
144 | a76bab49 | aliguori | if (!node->deleted && node->io_read) {
|
145 | a76bab49 | aliguori | FD_SET(node->fd, &rdfds); |
146 | a76bab49 | aliguori | max_fd = MAX(max_fd, node->fd + 1);
|
147 | a76bab49 | aliguori | } |
148 | a76bab49 | aliguori | if (!node->deleted && node->io_write) {
|
149 | a76bab49 | aliguori | FD_SET(node->fd, &wrfds); |
150 | a76bab49 | aliguori | max_fd = MAX(max_fd, node->fd + 1);
|
151 | a76bab49 | aliguori | } |
152 | a76bab49 | aliguori | } |
153 | a76bab49 | aliguori | |
154 | a76bab49 | aliguori | walking_handlers = 0;
|
155 | a76bab49 | aliguori | |
156 | a76bab49 | aliguori | /* No AIO operations? Get us out of here */
|
157 | a76bab49 | aliguori | if (max_fd == -1) |
158 | a76bab49 | aliguori | break;
|
159 | a76bab49 | aliguori | |
160 | a76bab49 | aliguori | /* wait until next event */
|
161 | a76bab49 | aliguori | ret = select(max_fd, &rdfds, &wrfds, NULL, NULL); |
162 | a76bab49 | aliguori | if (ret == -1 && errno == EINTR) |
163 | a76bab49 | aliguori | continue;
|
164 | a76bab49 | aliguori | |
165 | a76bab49 | aliguori | /* if we have any readable fds, dispatch event */
|
166 | a76bab49 | aliguori | if (ret > 0) { |
167 | a76bab49 | aliguori | walking_handlers = 1;
|
168 | a76bab49 | aliguori | |
169 | a76bab49 | aliguori | /* we have to walk very carefully in case
|
170 | a76bab49 | aliguori | * qemu_aio_set_fd_handler is called while we're walking */
|
171 | 72cf2d4f | Blue Swirl | node = QLIST_FIRST(&aio_handlers); |
172 | a76bab49 | aliguori | while (node) {
|
173 | a76bab49 | aliguori | AioHandler *tmp; |
174 | a76bab49 | aliguori | |
175 | a76bab49 | aliguori | if (!node->deleted &&
|
176 | a76bab49 | aliguori | FD_ISSET(node->fd, &rdfds) && |
177 | a76bab49 | aliguori | node->io_read) { |
178 | a76bab49 | aliguori | node->io_read(node->opaque); |
179 | a76bab49 | aliguori | } |
180 | a76bab49 | aliguori | if (!node->deleted &&
|
181 | a76bab49 | aliguori | FD_ISSET(node->fd, &wrfds) && |
182 | a76bab49 | aliguori | node->io_write) { |
183 | a76bab49 | aliguori | node->io_write(node->opaque); |
184 | a76bab49 | aliguori | } |
185 | a76bab49 | aliguori | |
186 | a76bab49 | aliguori | tmp = node; |
187 | 72cf2d4f | Blue Swirl | node = QLIST_NEXT(node, node); |
188 | a76bab49 | aliguori | |
189 | a76bab49 | aliguori | if (tmp->deleted) {
|
190 | 72cf2d4f | Blue Swirl | QLIST_REMOVE(tmp, node); |
191 | a76bab49 | aliguori | qemu_free(tmp); |
192 | a76bab49 | aliguori | } |
193 | a76bab49 | aliguori | } |
194 | a76bab49 | aliguori | |
195 | a76bab49 | aliguori | walking_handlers = 0;
|
196 | a76bab49 | aliguori | } |
197 | a76bab49 | aliguori | } while (ret == 0); |
198 | a76bab49 | aliguori | } |