]> git.proxmox.com Git - mirror_qemu.git/blame - util/aio-win32.c
qemu-img: assert block_job_get() does not return NULL in img_commit()
[mirror_qemu.git] / util / aio-win32.c
CommitLineData
a76bab49
AL
1/*
2 * QEMU aio implementation
3 *
f42b2207
PB
4 * Copyright IBM Corp., 2008
5 * Copyright Red Hat Inc., 2012
a76bab49
AL
6 *
7 * Authors:
8 * Anthony Liguori <aliguori@us.ibm.com>
f42b2207 9 * Paolo Bonzini <pbonzini@redhat.com>
a76bab49
AL
10 *
11 * This work is licensed under the terms of the GNU GPL, version 2. See
12 * the COPYING file in the top-level directory.
13 *
6b620ca3
PB
14 * Contributions after 2012-01-13 are licensed under the terms of the
15 * GNU GPL, version 2 or (at your option) any later version.
a76bab49
AL
16 */
17
d38ea87a 18#include "qemu/osdep.h"
a76bab49 19#include "qemu-common.h"
737e150e 20#include "block/block.h"
1de7afc9
PB
21#include "qemu/queue.h"
22#include "qemu/sockets.h"
4a1cba38 23#include "qapi/error.h"
b92d9a91 24#include "qemu/rcu_queue.h"
a76bab49 25
f42b2207
PB
26struct AioHandler {
27 EventNotifier *e;
b493317d
PB
28 IOHandler *io_read;
29 IOHandler *io_write;
f42b2207 30 EventNotifierHandler *io_notify;
cd9ba1eb 31 GPollFD pfd;
a76bab49 32 int deleted;
b493317d 33 void *opaque;
dca21ef2 34 bool is_external;
72cf2d4f 35 QLIST_ENTRY(AioHandler) node;
a76bab49
AL
36};
37
b493317d
PB
38void aio_set_fd_handler(AioContext *ctx,
39 int fd,
dca21ef2 40 bool is_external,
b493317d
PB
41 IOHandler *io_read,
42 IOHandler *io_write,
4a1cba38 43 AioPollFn *io_poll,
b493317d
PB
44 void *opaque)
45{
46 /* fd is a SOCKET in our case */
47 AioHandler *node;
48
b92d9a91 49 qemu_lockcnt_lock(&ctx->list_lock);
b493317d
PB
50 QLIST_FOREACH(node, &ctx->aio_handlers, node) {
51 if (node->pfd.fd == fd && !node->deleted) {
52 break;
53 }
54 }
55
56 /* Are we deleting the fd handler? */
57 if (!io_read && !io_write) {
58 if (node) {
b92d9a91
PB
59 /* If aio_poll is in progress, just mark the node as deleted */
60 if (qemu_lockcnt_count(&ctx->list_lock)) {
b493317d
PB
61 node->deleted = 1;
62 node->pfd.revents = 0;
63 } else {
64 /* Otherwise, delete it for real. We can't just mark it as
65 * deleted because deleted nodes are only cleaned up after
b92d9a91 66 * releasing the list_lock.
b493317d
PB
67 */
68 QLIST_REMOVE(node, node);
69 g_free(node);
70 }
71 }
72 } else {
73 HANDLE event;
55d41b16 74 long bitmask = 0;
b493317d
PB
75
76 if (node == NULL) {
77 /* Alloc and insert if it's not already there */
3ba235a0 78 node = g_new0(AioHandler, 1);
b493317d 79 node->pfd.fd = fd;
b92d9a91 80 QLIST_INSERT_HEAD_RCU(&ctx->aio_handlers, node, node);
b493317d
PB
81 }
82
83 node->pfd.events = 0;
84 if (node->io_read) {
85 node->pfd.events |= G_IO_IN;
86 }
87 if (node->io_write) {
88 node->pfd.events |= G_IO_OUT;
89 }
90
91 node->e = &ctx->notifier;
92
93 /* Update handler with latest information */
94 node->opaque = opaque;
95 node->io_read = io_read;
96 node->io_write = io_write;
dca21ef2 97 node->is_external = is_external;
b493317d 98
55d41b16
AF
99 if (io_read) {
100 bitmask |= FD_READ | FD_ACCEPT | FD_CLOSE;
101 }
102
103 if (io_write) {
104 bitmask |= FD_WRITE | FD_CONNECT;
105 }
106
b493317d 107 event = event_notifier_get_handle(&ctx->notifier);
55d41b16 108 WSAEventSelect(node->pfd.fd, event, bitmask);
b493317d
PB
109 }
110
b92d9a91 111 qemu_lockcnt_unlock(&ctx->list_lock);
b493317d
PB
112 aio_notify(ctx);
113}
114
684e508c
SH
115void aio_set_fd_poll(AioContext *ctx, int fd,
116 IOHandler *io_poll_begin,
117 IOHandler *io_poll_end)
118{
119 /* Not implemented */
120}
121
f42b2207
PB
122void aio_set_event_notifier(AioContext *ctx,
123 EventNotifier *e,
dca21ef2 124 bool is_external,
4a1cba38
SH
125 EventNotifierHandler *io_notify,
126 AioPollFn *io_poll)
a76bab49
AL
127{
128 AioHandler *node;
129
b92d9a91 130 qemu_lockcnt_lock(&ctx->list_lock);
a915f4bc 131 QLIST_FOREACH(node, &ctx->aio_handlers, node) {
f42b2207
PB
132 if (node->e == e && !node->deleted) {
133 break;
134 }
a76bab49
AL
135 }
136
a76bab49 137 /* Are we deleting the fd handler? */
f42b2207 138 if (!io_notify) {
a76bab49 139 if (node) {
e3713e00
PB
140 g_source_remove_poll(&ctx->source, &node->pfd);
141
b92d9a91
PB
142 /* aio_poll is in progress, just mark the node as deleted */
143 if (qemu_lockcnt_count(&ctx->list_lock)) {
a76bab49 144 node->deleted = 1;
cd9ba1eb
PB
145 node->pfd.revents = 0;
146 } else {
a76bab49
AL
147 /* Otherwise, delete it for real. We can't just mark it as
148 * deleted because deleted nodes are only cleaned up after
b92d9a91 149 * releasing the list_lock.
a76bab49 150 */
72cf2d4f 151 QLIST_REMOVE(node, node);
7267c094 152 g_free(node);
a76bab49
AL
153 }
154 }
155 } else {
156 if (node == NULL) {
157 /* Alloc and insert if it's not already there */
3ba235a0 158 node = g_new0(AioHandler, 1);
f42b2207
PB
159 node->e = e;
160 node->pfd.fd = (uintptr_t)event_notifier_get_handle(e);
161 node->pfd.events = G_IO_IN;
dca21ef2 162 node->is_external = is_external;
b92d9a91 163 QLIST_INSERT_HEAD_RCU(&ctx->aio_handlers, node, node);
e3713e00
PB
164
165 g_source_add_poll(&ctx->source, &node->pfd);
a76bab49
AL
166 }
167 /* Update handler with latest information */
f42b2207 168 node->io_notify = io_notify;
a76bab49 169 }
7ed2b24c 170
b92d9a91 171 qemu_lockcnt_unlock(&ctx->list_lock);
7ed2b24c 172 aio_notify(ctx);
9958c351
PB
173}
174
684e508c
SH
175void aio_set_event_notifier_poll(AioContext *ctx,
176 EventNotifier *notifier,
177 EventNotifierHandler *io_poll_begin,
178 EventNotifierHandler *io_poll_end)
179{
180 /* Not implemented */
181}
182
a3462c65
PB
183bool aio_prepare(AioContext *ctx)
184{
b493317d
PB
185 static struct timeval tv0;
186 AioHandler *node;
187 bool have_select_revents = false;
188 fd_set rfds, wfds;
189
b92d9a91
PB
190 /*
191 * We have to walk very carefully in case aio_set_fd_handler is
192 * called while we're walking.
193 */
194 qemu_lockcnt_inc(&ctx->list_lock);
195
b493317d
PB
196 /* fill fd sets */
197 FD_ZERO(&rfds);
198 FD_ZERO(&wfds);
b92d9a91 199 QLIST_FOREACH_RCU(node, &ctx->aio_handlers, node) {
b493317d
PB
200 if (node->io_read) {
201 FD_SET ((SOCKET)node->pfd.fd, &rfds);
202 }
203 if (node->io_write) {
204 FD_SET ((SOCKET)node->pfd.fd, &wfds);
205 }
206 }
207
208 if (select(0, &rfds, &wfds, NULL, &tv0) > 0) {
b92d9a91 209 QLIST_FOREACH_RCU(node, &ctx->aio_handlers, node) {
b493317d
PB
210 node->pfd.revents = 0;
211 if (FD_ISSET(node->pfd.fd, &rfds)) {
212 node->pfd.revents |= G_IO_IN;
213 have_select_revents = true;
214 }
215
216 if (FD_ISSET(node->pfd.fd, &wfds)) {
217 node->pfd.revents |= G_IO_OUT;
218 have_select_revents = true;
219 }
220 }
221 }
222
b92d9a91 223 qemu_lockcnt_dec(&ctx->list_lock);
b493317d 224 return have_select_revents;
a3462c65
PB
225}
226
cd9ba1eb
PB
227bool aio_pending(AioContext *ctx)
228{
229 AioHandler *node;
b92d9a91 230 bool result = false;
cd9ba1eb 231
b92d9a91
PB
232 /*
233 * We have to walk very carefully in case aio_set_fd_handler is
234 * called while we're walking.
235 */
236 qemu_lockcnt_inc(&ctx->list_lock);
237 QLIST_FOREACH_RCU(node, &ctx->aio_handlers, node) {
f42b2207 238 if (node->pfd.revents && node->io_notify) {
b92d9a91
PB
239 result = true;
240 break;
cd9ba1eb 241 }
b493317d
PB
242
243 if ((node->pfd.revents & G_IO_IN) && node->io_read) {
b92d9a91
PB
244 result = true;
245 break;
b493317d
PB
246 }
247 if ((node->pfd.revents & G_IO_OUT) && node->io_write) {
b92d9a91
PB
248 result = true;
249 break;
b493317d 250 }
cd9ba1eb
PB
251 }
252
b92d9a91
PB
253 qemu_lockcnt_dec(&ctx->list_lock);
254 return result;
cd9ba1eb
PB
255}
256
a398dea3 257static bool aio_dispatch_handlers(AioContext *ctx, HANDLE event)
a76bab49 258{
b92d9a91 259 AioHandler *node;
a398dea3 260 bool progress = false;
b92d9a91 261 AioHandler *tmp;
7c0628b2 262
cd9ba1eb 263 /*
87f68d31 264 * We have to walk very carefully in case aio_set_fd_handler is
cd9ba1eb
PB
265 * called while we're walking.
266 */
b92d9a91 267 QLIST_FOREACH_SAFE_RCU(node, &ctx->aio_handlers, node, tmp) {
b493317d 268 int revents = node->pfd.revents;
cd9ba1eb 269
a398dea3 270 if (!node->deleted &&
b493317d 271 (revents || event_notifier_get_handle(node->e) == event) &&
a398dea3 272 node->io_notify) {
f42b2207
PB
273 node->pfd.revents = 0;
274 node->io_notify(node->e);
164a101f
SH
275
276 /* aio_notify() does not count as progress */
8b2d42d2 277 if (node->e != &ctx->notifier) {
164a101f
SH
278 progress = true;
279 }
cd9ba1eb
PB
280 }
281
b493317d
PB
282 if (!node->deleted &&
283 (node->io_read || node->io_write)) {
284 node->pfd.revents = 0;
285 if ((revents & G_IO_IN) && node->io_read) {
286 node->io_read(node->opaque);
287 progress = true;
288 }
289 if ((revents & G_IO_OUT) && node->io_write) {
290 node->io_write(node->opaque);
291 progress = true;
292 }
293
294 /* if the next select() will return an event, we have progressed */
295 if (event == event_notifier_get_handle(&ctx->notifier)) {
296 WSANETWORKEVENTS ev;
297 WSAEnumNetworkEvents(node->pfd.fd, event, &ev);
298 if (ev.lNetworkEvents) {
299 progress = true;
300 }
301 }
302 }
303
abf90d39 304 if (node->deleted) {
b92d9a91 305 if (qemu_lockcnt_dec_if_lock(&ctx->list_lock)) {
abf90d39
PB
306 QLIST_REMOVE(node, node);
307 g_free(node);
b92d9a91 308 qemu_lockcnt_inc_and_unlock(&ctx->list_lock);
abf90d39 309 }
cd9ba1eb
PB
310 }
311 }
312
a398dea3
PB
313 return progress;
314}
315
a153bf52 316void aio_dispatch(AioContext *ctx)
a398dea3 317{
bd451435 318 qemu_lockcnt_inc(&ctx->list_lock);
a153bf52
PB
319 aio_bh_poll(ctx);
320 aio_dispatch_handlers(ctx, INVALID_HANDLE_VALUE);
bd451435 321 qemu_lockcnt_dec(&ctx->list_lock);
a153bf52 322 timerlistgroup_run_timers(&ctx->tlg);
a398dea3
PB
323}
324
325bool aio_poll(AioContext *ctx, bool blocking)
326{
327 AioHandler *node;
328 HANDLE events[MAXIMUM_WAIT_OBJECTS + 1];
eabc9779 329 bool progress, have_select_revents, first;
a398dea3
PB
330 int count;
331 int timeout;
332
333 progress = false;
334
0a9dd166
PB
335 /* aio_notify can avoid the expensive event_notifier_set if
336 * everything (file descriptors, bottom halves, timers) will
337 * be re-evaluated before the next blocking poll(). This is
338 * already true when aio_poll is called with blocking == false;
eabc9779
PB
339 * if blocking == true, it is only true after poll() returns,
340 * so disable the optimization now.
0a9dd166 341 */
eabc9779
PB
342 if (blocking) {
343 atomic_add(&ctx->notify_me, 2);
344 }
0a9dd166 345
b92d9a91 346 qemu_lockcnt_inc(&ctx->list_lock);
6493c975
PB
347 have_select_revents = aio_prepare(ctx);
348
9eb0bfca 349 /* fill fd sets */
f42b2207 350 count = 0;
b92d9a91 351 QLIST_FOREACH_RCU(node, &ctx->aio_handlers, node) {
c1e1e5fa
FZ
352 if (!node->deleted && node->io_notify
353 && aio_node_check(ctx, node->is_external)) {
f42b2207 354 events[count++] = event_notifier_get_handle(node->e);
9eb0bfca
PB
355 }
356 }
a76bab49 357
3672fa50 358 first = true;
a76bab49 359
6493c975
PB
360 /* ctx->notifier is always registered. */
361 assert(count > 0);
362
363 /* Multiple iterations, all of them non-blocking except the first,
364 * may be necessary to process all pending events. After the first
365 * WaitForMultipleObjects call ctx->notify_me will be decremented.
366 */
367 do {
b493317d 368 HANDLE event;
438e1f47
AB
369 int ret;
370
6493c975 371 timeout = blocking && !have_select_revents
845ca10d 372 ? qemu_timeout_ns_to_ms(aio_compute_timeout(ctx)) : 0;
438e1f47 373 ret = WaitForMultipleObjects(count, events, FALSE, timeout);
eabc9779
PB
374 if (blocking) {
375 assert(first);
b37548fc 376 assert(in_aio_context_home_thread(ctx));
eabc9779 377 atomic_sub(&ctx->notify_me, 2);
b37548fc 378 aio_notify_accept(ctx);
eabc9779 379 }
f42b2207 380
21a03d17 381 if (first) {
21a03d17
PB
382 progress |= aio_bh_poll(ctx);
383 first = false;
3672fa50 384 }
3672fa50 385
f42b2207 386 /* if we have any signaled events, dispatch event */
b493317d
PB
387 event = NULL;
388 if ((DWORD) (ret - WAIT_OBJECT_0) < count) {
389 event = events[ret - WAIT_OBJECT_0];
a90d411e 390 events[ret - WAIT_OBJECT_0] = events[--count];
b493317d 391 } else if (!have_select_revents) {
f42b2207
PB
392 break;
393 }
394
b493317d 395 have_select_revents = false;
f42b2207 396 blocking = false;
9eb0bfca 397
b493317d 398 progress |= aio_dispatch_handlers(ctx, event);
6493c975 399 } while (count > 0);
bcdc1857 400
bd451435
PB
401 qemu_lockcnt_dec(&ctx->list_lock);
402
e4c7e2d1 403 progress |= timerlistgroup_run_timers(&ctx->tlg);
164a101f 404 return progress;
a76bab49 405}
37fcee5d 406
7e003465 407void aio_context_setup(AioContext *ctx)
37fcee5d
FZ
408{
409}
4a1cba38 410
cd0a6d2b
JW
411void aio_context_destroy(AioContext *ctx)
412{
413}
414
82a41186
SH
415void aio_context_set_poll_params(AioContext *ctx, int64_t max_ns,
416 int64_t grow, int64_t shrink, Error **errp)
4a1cba38 417{
90c558be
PX
418 if (max_ns) {
419 error_setg(errp, "AioContext polling is not implemented on Windows");
420 }
4a1cba38 421}