]>
Commit | Line | Data |
---|---|---|
3c529d93 AL |
1 | /* |
2 | * QEMU posix-aio emulation | |
3 | * | |
4 | * Copyright IBM, Corp. 2008 | |
5 | * | |
6 | * Authors: | |
7 | * Anthony Liguori <[email protected]> | |
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 | * | |
12 | */ | |
13 | ||
221f715d | 14 | #include <sys/ioctl.h> |
9ef91a67 | 15 | #include <sys/types.h> |
3c529d93 AL |
16 | #include <pthread.h> |
17 | #include <unistd.h> | |
18 | #include <errno.h> | |
30525aff | 19 | #include <time.h> |
9ef91a67 | 20 | #include <signal.h> |
8653c015 | 21 | #include <string.h> |
22 | #include <stdlib.h> | |
23 | #include <stdio.h> | |
9ef91a67 | 24 | |
72cf2d4f | 25 | #include "qemu-queue.h" |
3c529d93 | 26 | #include "osdep.h" |
dc786bc9 | 27 | #include "sysemu.h" |
f141eafe | 28 | #include "qemu-common.h" |
6d519a5f | 29 | #include "trace.h" |
9ef91a67 CH |
30 | #include "block_int.h" |
31 | ||
32 | #include "block/raw-posix-aio.h" | |
33 | ||
34 | ||
35 | struct qemu_paiocb { | |
36 | BlockDriverAIOCB common; | |
37 | int aio_fildes; | |
38 | union { | |
39 | struct iovec *aio_iov; | |
b587a52c | 40 | void *aio_ioctl_buf; |
9ef91a67 CH |
41 | }; |
42 | int aio_niov; | |
43 | size_t aio_nbytes; | |
44 | #define aio_ioctl_cmd aio_nbytes /* for QEMU_AIO_IOCTL */ | |
45 | int ev_signo; | |
46 | off_t aio_offset; | |
47 | ||
72cf2d4f | 48 | QTAILQ_ENTRY(qemu_paiocb) node; |
9ef91a67 CH |
49 | int aio_type; |
50 | ssize_t ret; | |
51 | int active; | |
52 | struct qemu_paiocb *next; | |
e5f37649 KW |
53 | |
54 | int async_context_id; | |
9ef91a67 CH |
55 | }; |
56 | ||
57 | typedef struct PosixAioState { | |
58 | int rfd, wfd; | |
59 | struct qemu_paiocb *first_aio; | |
60 | } PosixAioState; | |
3c529d93 | 61 | |
3c529d93 AL |
62 | |
63 | static pthread_mutex_t lock = PTHREAD_MUTEX_INITIALIZER; | |
64 | static pthread_cond_t cond = PTHREAD_COND_INITIALIZER; | |
65 | static pthread_t thread_id; | |
a8227a5a | 66 | static pthread_attr_t attr; |
3c529d93 AL |
67 | static int max_threads = 64; |
68 | static int cur_threads = 0; | |
69 | static int idle_threads = 0; | |
72cf2d4f | 70 | static QTAILQ_HEAD(, qemu_paiocb) request_list; |
3c529d93 | 71 | |
2341f9a1 | 72 | #ifdef CONFIG_PREADV |
ceb42de8 AL |
73 | static int preadv_present = 1; |
74 | #else | |
75 | static int preadv_present = 0; | |
76 | #endif | |
77 | ||
8653c015 | 78 | static void die2(int err, const char *what) |
79 | { | |
80 | fprintf(stderr, "%s failed: %s\n", what, strerror(err)); | |
81 | abort(); | |
82 | } | |
83 | ||
84 | static void die(const char *what) | |
85 | { | |
86 | die2(errno, what); | |
87 | } | |
88 | ||
89 | static void mutex_lock(pthread_mutex_t *mutex) | |
90 | { | |
91 | int ret = pthread_mutex_lock(mutex); | |
92 | if (ret) die2(ret, "pthread_mutex_lock"); | |
93 | } | |
94 | ||
95 | static void mutex_unlock(pthread_mutex_t *mutex) | |
96 | { | |
97 | int ret = pthread_mutex_unlock(mutex); | |
98 | if (ret) die2(ret, "pthread_mutex_unlock"); | |
99 | } | |
100 | ||
101 | static int cond_timedwait(pthread_cond_t *cond, pthread_mutex_t *mutex, | |
102 | struct timespec *ts) | |
103 | { | |
104 | int ret = pthread_cond_timedwait(cond, mutex, ts); | |
105 | if (ret && ret != ETIMEDOUT) die2(ret, "pthread_cond_timedwait"); | |
106 | return ret; | |
107 | } | |
108 | ||
5d47e372 | 109 | static void cond_signal(pthread_cond_t *cond) |
8653c015 | 110 | { |
5d47e372 | 111 | int ret = pthread_cond_signal(cond); |
112 | if (ret) die2(ret, "pthread_cond_signal"); | |
8653c015 | 113 | } |
114 | ||
115 | static void thread_create(pthread_t *thread, pthread_attr_t *attr, | |
116 | void *(*start_routine)(void*), void *arg) | |
117 | { | |
118 | int ret = pthread_create(thread, attr, start_routine, arg); | |
119 | if (ret) die2(ret, "pthread_create"); | |
120 | } | |
121 | ||
6769da29 | 122 | static ssize_t handle_aiocb_ioctl(struct qemu_paiocb *aiocb) |
f141eafe | 123 | { |
b587a52c SH |
124 | int ret; |
125 | ||
126 | ret = ioctl(aiocb->aio_fildes, aiocb->aio_ioctl_cmd, aiocb->aio_ioctl_buf); | |
127 | if (ret == -1) | |
128 | return -errno; | |
129 | ||
130 | /* | |
131 | * This looks weird, but the aio code only consideres a request | |
b0cd712c | 132 | * successful if it has written the number full number of bytes. |
b587a52c SH |
133 | * |
134 | * Now we overload aio_nbytes as aio_ioctl_cmd for the ioctl command, | |
135 | * so in fact we return the ioctl command here to make posix_aio_read() | |
136 | * happy.. | |
137 | */ | |
138 | return aiocb->aio_nbytes; | |
f141eafe AL |
139 | } |
140 | ||
6769da29 | 141 | static ssize_t handle_aiocb_flush(struct qemu_paiocb *aiocb) |
b2e12bc6 CH |
142 | { |
143 | int ret; | |
144 | ||
47faadc6 | 145 | ret = qemu_fdatasync(aiocb->aio_fildes); |
b2e12bc6 CH |
146 | if (ret == -1) |
147 | return -errno; | |
148 | return 0; | |
149 | } | |
150 | ||
2341f9a1 | 151 | #ifdef CONFIG_PREADV |
ceb42de8 AL |
152 | |
153 | static ssize_t | |
154 | qemu_preadv(int fd, const struct iovec *iov, int nr_iov, off_t offset) | |
155 | { | |
156 | return preadv(fd, iov, nr_iov, offset); | |
157 | } | |
158 | ||
159 | static ssize_t | |
160 | qemu_pwritev(int fd, const struct iovec *iov, int nr_iov, off_t offset) | |
161 | { | |
162 | return pwritev(fd, iov, nr_iov, offset); | |
163 | } | |
164 | ||
165 | #else | |
166 | ||
167 | static ssize_t | |
168 | qemu_preadv(int fd, const struct iovec *iov, int nr_iov, off_t offset) | |
169 | { | |
170 | return -ENOSYS; | |
171 | } | |
172 | ||
173 | static ssize_t | |
174 | qemu_pwritev(int fd, const struct iovec *iov, int nr_iov, off_t offset) | |
175 | { | |
176 | return -ENOSYS; | |
177 | } | |
178 | ||
179 | #endif | |
180 | ||
6769da29 | 181 | static ssize_t handle_aiocb_rw_vector(struct qemu_paiocb *aiocb) |
ceb42de8 AL |
182 | { |
183 | size_t offset = 0; | |
184 | ssize_t len; | |
185 | ||
186 | do { | |
9ef91a67 | 187 | if (aiocb->aio_type & QEMU_AIO_WRITE) |
ceb42de8 AL |
188 | len = qemu_pwritev(aiocb->aio_fildes, |
189 | aiocb->aio_iov, | |
190 | aiocb->aio_niov, | |
191 | aiocb->aio_offset + offset); | |
192 | else | |
193 | len = qemu_preadv(aiocb->aio_fildes, | |
194 | aiocb->aio_iov, | |
195 | aiocb->aio_niov, | |
196 | aiocb->aio_offset + offset); | |
197 | } while (len == -1 && errno == EINTR); | |
198 | ||
199 | if (len == -1) | |
200 | return -errno; | |
201 | return len; | |
202 | } | |
203 | ||
6769da29 | 204 | static ssize_t handle_aiocb_rw_linear(struct qemu_paiocb *aiocb, char *buf) |
221f715d | 205 | { |
6769da29 KW |
206 | ssize_t offset = 0; |
207 | ssize_t len; | |
221f715d AL |
208 | |
209 | while (offset < aiocb->aio_nbytes) { | |
9ef91a67 | 210 | if (aiocb->aio_type & QEMU_AIO_WRITE) |
f141eafe AL |
211 | len = pwrite(aiocb->aio_fildes, |
212 | (const char *)buf + offset, | |
213 | aiocb->aio_nbytes - offset, | |
214 | aiocb->aio_offset + offset); | |
215 | else | |
216 | len = pread(aiocb->aio_fildes, | |
217 | buf + offset, | |
221f715d AL |
218 | aiocb->aio_nbytes - offset, |
219 | aiocb->aio_offset + offset); | |
221f715d | 220 | |
f141eafe AL |
221 | if (len == -1 && errno == EINTR) |
222 | continue; | |
223 | else if (len == -1) { | |
224 | offset = -errno; | |
225 | break; | |
226 | } else if (len == 0) | |
227 | break; | |
228 | ||
229 | offset += len; | |
221f715d AL |
230 | } |
231 | ||
232 | return offset; | |
233 | } | |
234 | ||
6769da29 | 235 | static ssize_t handle_aiocb_rw(struct qemu_paiocb *aiocb) |
221f715d | 236 | { |
6769da29 | 237 | ssize_t nbytes; |
f141eafe AL |
238 | char *buf; |
239 | ||
9ef91a67 | 240 | if (!(aiocb->aio_type & QEMU_AIO_MISALIGNED)) { |
f141eafe AL |
241 | /* |
242 | * If there is just a single buffer, and it is properly aligned | |
243 | * we can just use plain pread/pwrite without any problems. | |
244 | */ | |
ceb42de8 AL |
245 | if (aiocb->aio_niov == 1) |
246 | return handle_aiocb_rw_linear(aiocb, aiocb->aio_iov->iov_base); | |
247 | ||
248 | /* | |
249 | * We have more than one iovec, and all are properly aligned. | |
250 | * | |
251 | * Try preadv/pwritev first and fall back to linearizing the | |
252 | * buffer if it's not supported. | |
253 | */ | |
b587a52c | 254 | if (preadv_present) { |
ceb42de8 AL |
255 | nbytes = handle_aiocb_rw_vector(aiocb); |
256 | if (nbytes == aiocb->aio_nbytes) | |
b587a52c | 257 | return nbytes; |
ceb42de8 AL |
258 | if (nbytes < 0 && nbytes != -ENOSYS) |
259 | return nbytes; | |
260 | preadv_present = 0; | |
261 | } | |
262 | ||
263 | /* | |
264 | * XXX(hch): short read/write. no easy way to handle the reminder | |
265 | * using these interfaces. For now retry using plain | |
266 | * pread/pwrite? | |
267 | */ | |
f141eafe | 268 | } |
221f715d | 269 | |
f141eafe AL |
270 | /* |
271 | * Ok, we have to do it the hard way, copy all segments into | |
272 | * a single aligned buffer. | |
273 | */ | |
72aef731 | 274 | buf = qemu_blockalign(aiocb->common.bs, aiocb->aio_nbytes); |
9ef91a67 | 275 | if (aiocb->aio_type & QEMU_AIO_WRITE) { |
f141eafe AL |
276 | char *p = buf; |
277 | int i; | |
278 | ||
279 | for (i = 0; i < aiocb->aio_niov; ++i) { | |
280 | memcpy(p, aiocb->aio_iov[i].iov_base, aiocb->aio_iov[i].iov_len); | |
281 | p += aiocb->aio_iov[i].iov_len; | |
282 | } | |
283 | } | |
284 | ||
285 | nbytes = handle_aiocb_rw_linear(aiocb, buf); | |
9ef91a67 | 286 | if (!(aiocb->aio_type & QEMU_AIO_WRITE)) { |
f141eafe AL |
287 | char *p = buf; |
288 | size_t count = aiocb->aio_nbytes, copy; | |
289 | int i; | |
290 | ||
291 | for (i = 0; i < aiocb->aio_niov && count; ++i) { | |
292 | copy = count; | |
293 | if (copy > aiocb->aio_iov[i].iov_len) | |
294 | copy = aiocb->aio_iov[i].iov_len; | |
295 | memcpy(aiocb->aio_iov[i].iov_base, p, copy); | |
296 | p += copy; | |
297 | count -= copy; | |
298 | } | |
299 | } | |
300 | qemu_vfree(buf); | |
301 | ||
302 | return nbytes; | |
221f715d AL |
303 | } |
304 | ||
3c529d93 AL |
305 | static void *aio_thread(void *unused) |
306 | { | |
a8227a5a | 307 | pid_t pid; |
3c529d93 | 308 | |
a8227a5a | 309 | pid = getpid(); |
310 | ||
3c529d93 AL |
311 | while (1) { |
312 | struct qemu_paiocb *aiocb; | |
6769da29 | 313 | ssize_t ret = 0; |
30525aff | 314 | qemu_timeval tv; |
315 | struct timespec ts; | |
316 | ||
317 | qemu_gettimeofday(&tv); | |
318 | ts.tv_sec = tv.tv_sec + 10; | |
319 | ts.tv_nsec = 0; | |
3c529d93 | 320 | |
8653c015 | 321 | mutex_lock(&lock); |
3c529d93 | 322 | |
72cf2d4f | 323 | while (QTAILQ_EMPTY(&request_list) && |
3c529d93 | 324 | !(ret == ETIMEDOUT)) { |
5be4aab7 | 325 | idle_threads++; |
8653c015 | 326 | ret = cond_timedwait(&cond, &lock, &ts); |
5be4aab7 | 327 | idle_threads--; |
3c529d93 AL |
328 | } |
329 | ||
72cf2d4f | 330 | if (QTAILQ_EMPTY(&request_list)) |
3c529d93 AL |
331 | break; |
332 | ||
72cf2d4f BS |
333 | aiocb = QTAILQ_FIRST(&request_list); |
334 | QTAILQ_REMOVE(&request_list, aiocb, node); | |
3c529d93 | 335 | aiocb->active = 1; |
8653c015 | 336 | mutex_unlock(&lock); |
3c529d93 | 337 | |
9ef91a67 CH |
338 | switch (aiocb->aio_type & QEMU_AIO_TYPE_MASK) { |
339 | case QEMU_AIO_READ: | |
340 | case QEMU_AIO_WRITE: | |
b587a52c SH |
341 | ret = handle_aiocb_rw(aiocb); |
342 | break; | |
b2e12bc6 | 343 | case QEMU_AIO_FLUSH: |
b587a52c SH |
344 | ret = handle_aiocb_flush(aiocb); |
345 | break; | |
9ef91a67 | 346 | case QEMU_AIO_IOCTL: |
b587a52c SH |
347 | ret = handle_aiocb_ioctl(aiocb); |
348 | break; | |
349 | default: | |
350 | fprintf(stderr, "invalid aio request (0x%x)\n", aiocb->aio_type); | |
351 | ret = -EINVAL; | |
352 | break; | |
353 | } | |
3c529d93 | 354 | |
8653c015 | 355 | mutex_lock(&lock); |
221f715d | 356 | aiocb->ret = ret; |
8653c015 | 357 | mutex_unlock(&lock); |
3c529d93 | 358 | |
a8227a5a | 359 | if (kill(pid, aiocb->ev_signo)) die("kill failed"); |
3c529d93 AL |
360 | } |
361 | ||
3c529d93 | 362 | cur_threads--; |
8653c015 | 363 | mutex_unlock(&lock); |
3c529d93 AL |
364 | |
365 | return NULL; | |
366 | } | |
367 | ||
8653c015 | 368 | static void spawn_thread(void) |
3c529d93 | 369 | { |
ee399306 | 370 | sigset_t set, oldset; |
371 | ||
3c529d93 | 372 | cur_threads++; |
ee399306 | 373 | |
374 | /* block all signals */ | |
375 | if (sigfillset(&set)) die("sigfillset"); | |
376 | if (sigprocmask(SIG_SETMASK, &set, &oldset)) die("sigprocmask"); | |
377 | ||
8653c015 | 378 | thread_create(&thread_id, &attr, aio_thread, NULL); |
ee399306 | 379 | |
380 | if (sigprocmask(SIG_SETMASK, &oldset, NULL)) die("sigprocmask restore"); | |
3c529d93 AL |
381 | } |
382 | ||
9ef91a67 | 383 | static void qemu_paio_submit(struct qemu_paiocb *aiocb) |
3c529d93 | 384 | { |
3c529d93 AL |
385 | aiocb->ret = -EINPROGRESS; |
386 | aiocb->active = 0; | |
8653c015 | 387 | mutex_lock(&lock); |
3c529d93 AL |
388 | if (idle_threads == 0 && cur_threads < max_threads) |
389 | spawn_thread(); | |
72cf2d4f | 390 | QTAILQ_INSERT_TAIL(&request_list, aiocb, node); |
8653c015 | 391 | mutex_unlock(&lock); |
5d47e372 | 392 | cond_signal(&cond); |
3c529d93 AL |
393 | } |
394 | ||
9ef91a67 | 395 | static ssize_t qemu_paio_return(struct qemu_paiocb *aiocb) |
3c529d93 AL |
396 | { |
397 | ssize_t ret; | |
398 | ||
8653c015 | 399 | mutex_lock(&lock); |
3c529d93 | 400 | ret = aiocb->ret; |
8653c015 | 401 | mutex_unlock(&lock); |
3c529d93 AL |
402 | |
403 | return ret; | |
404 | } | |
405 | ||
9ef91a67 | 406 | static int qemu_paio_error(struct qemu_paiocb *aiocb) |
3c529d93 AL |
407 | { |
408 | ssize_t ret = qemu_paio_return(aiocb); | |
409 | ||
410 | if (ret < 0) | |
411 | ret = -ret; | |
412 | else | |
413 | ret = 0; | |
414 | ||
415 | return ret; | |
416 | } | |
417 | ||
59c7b155 | 418 | static int posix_aio_process_queue(void *opaque) |
3c529d93 | 419 | { |
9ef91a67 CH |
420 | PosixAioState *s = opaque; |
421 | struct qemu_paiocb *acb, **pacb; | |
3c529d93 | 422 | int ret; |
59c7b155 | 423 | int result = 0; |
e5f37649 | 424 | int async_context_id = get_async_context_id(); |
9ef91a67 CH |
425 | |
426 | for(;;) { | |
427 | pacb = &s->first_aio; | |
428 | for(;;) { | |
429 | acb = *pacb; | |
430 | if (!acb) | |
59c7b155 | 431 | return result; |
e5f37649 KW |
432 | |
433 | /* we're only interested in requests in the right context */ | |
434 | if (acb->async_context_id != async_context_id) { | |
435 | pacb = &acb->next; | |
436 | continue; | |
437 | } | |
438 | ||
9ef91a67 CH |
439 | ret = qemu_paio_error(acb); |
440 | if (ret == ECANCELED) { | |
441 | /* remove the request */ | |
442 | *pacb = acb->next; | |
443 | qemu_aio_release(acb); | |
59c7b155 | 444 | result = 1; |
9ef91a67 CH |
445 | } else if (ret != EINPROGRESS) { |
446 | /* end of aio */ | |
447 | if (ret == 0) { | |
448 | ret = qemu_paio_return(acb); | |
449 | if (ret == acb->aio_nbytes) | |
450 | ret = 0; | |
451 | else | |
452 | ret = -EINVAL; | |
453 | } else { | |
454 | ret = -ret; | |
455 | } | |
ddca9fb2 SH |
456 | |
457 | trace_paio_complete(acb, acb->common.opaque, ret); | |
458 | ||
9ef91a67 CH |
459 | /* remove the request */ |
460 | *pacb = acb->next; | |
461 | /* call the callback */ | |
462 | acb->common.cb(acb->common.opaque, ret); | |
463 | qemu_aio_release(acb); | |
59c7b155 | 464 | result = 1; |
9ef91a67 CH |
465 | break; |
466 | } else { | |
467 | pacb = &acb->next; | |
468 | } | |
469 | } | |
470 | } | |
59c7b155 KW |
471 | |
472 | return result; | |
473 | } | |
474 | ||
475 | static void posix_aio_read(void *opaque) | |
476 | { | |
477 | PosixAioState *s = opaque; | |
478 | ssize_t len; | |
479 | ||
480 | /* read all bytes from signal pipe */ | |
481 | for (;;) { | |
482 | char bytes[16]; | |
483 | ||
484 | len = read(s->rfd, bytes, sizeof(bytes)); | |
485 | if (len == -1 && errno == EINTR) | |
486 | continue; /* try again */ | |
487 | if (len == sizeof(bytes)) | |
488 | continue; /* more to read */ | |
489 | break; | |
490 | } | |
491 | ||
492 | posix_aio_process_queue(s); | |
9ef91a67 CH |
493 | } |
494 | ||
495 | static int posix_aio_flush(void *opaque) | |
496 | { | |
497 | PosixAioState *s = opaque; | |
498 | return !!s->first_aio; | |
499 | } | |
500 | ||
501 | static PosixAioState *posix_aio_state; | |
502 | ||
503 | static void aio_signal_handler(int signum) | |
504 | { | |
505 | if (posix_aio_state) { | |
506 | char byte = 0; | |
4817d327 | 507 | ssize_t ret; |
9ef91a67 | 508 | |
4817d327 KS |
509 | ret = write(posix_aio_state->wfd, &byte, sizeof(byte)); |
510 | if (ret < 0 && errno != EAGAIN) | |
511 | die("write()"); | |
9ef91a67 CH |
512 | } |
513 | ||
514 | qemu_service_io(); | |
515 | } | |
516 | ||
517 | static void paio_remove(struct qemu_paiocb *acb) | |
518 | { | |
519 | struct qemu_paiocb **pacb; | |
520 | ||
521 | /* remove the callback from the queue */ | |
522 | pacb = &posix_aio_state->first_aio; | |
523 | for(;;) { | |
524 | if (*pacb == NULL) { | |
525 | fprintf(stderr, "paio_remove: aio request not found!\n"); | |
526 | break; | |
527 | } else if (*pacb == acb) { | |
528 | *pacb = acb->next; | |
529 | qemu_aio_release(acb); | |
530 | break; | |
531 | } | |
532 | pacb = &(*pacb)->next; | |
533 | } | |
534 | } | |
535 | ||
536 | static void paio_cancel(BlockDriverAIOCB *blockacb) | |
537 | { | |
538 | struct qemu_paiocb *acb = (struct qemu_paiocb *)blockacb; | |
539 | int active = 0; | |
3c529d93 | 540 | |
ddca9fb2 SH |
541 | trace_paio_cancel(acb, acb->common.opaque); |
542 | ||
8653c015 | 543 | mutex_lock(&lock); |
9ef91a67 | 544 | if (!acb->active) { |
72cf2d4f | 545 | QTAILQ_REMOVE(&request_list, acb, node); |
9ef91a67 CH |
546 | acb->ret = -ECANCELED; |
547 | } else if (acb->ret == -EINPROGRESS) { | |
548 | active = 1; | |
549 | } | |
8653c015 | 550 | mutex_unlock(&lock); |
3c529d93 | 551 | |
9ef91a67 CH |
552 | if (active) { |
553 | /* fail safe: if the aio could not be canceled, we wait for | |
554 | it */ | |
555 | while (qemu_paio_error(acb) == EINPROGRESS) | |
556 | ; | |
557 | } | |
558 | ||
559 | paio_remove(acb); | |
560 | } | |
561 | ||
562 | static AIOPool raw_aio_pool = { | |
563 | .aiocb_size = sizeof(struct qemu_paiocb), | |
564 | .cancel = paio_cancel, | |
565 | }; | |
566 | ||
1e5b9d2f | 567 | BlockDriverAIOCB *paio_submit(BlockDriverState *bs, int fd, |
9ef91a67 CH |
568 | int64_t sector_num, QEMUIOVector *qiov, int nb_sectors, |
569 | BlockDriverCompletionFunc *cb, void *opaque, int type) | |
570 | { | |
571 | struct qemu_paiocb *acb; | |
572 | ||
573 | acb = qemu_aio_get(&raw_aio_pool, bs, cb, opaque); | |
574 | if (!acb) | |
575 | return NULL; | |
576 | acb->aio_type = type; | |
577 | acb->aio_fildes = fd; | |
578 | acb->ev_signo = SIGUSR2; | |
e5f37649 KW |
579 | acb->async_context_id = get_async_context_id(); |
580 | ||
b2e12bc6 CH |
581 | if (qiov) { |
582 | acb->aio_iov = qiov->iov; | |
583 | acb->aio_niov = qiov->niov; | |
584 | } | |
9ef91a67 CH |
585 | acb->aio_nbytes = nb_sectors * 512; |
586 | acb->aio_offset = sector_num * 512; | |
587 | ||
588 | acb->next = posix_aio_state->first_aio; | |
589 | posix_aio_state->first_aio = acb; | |
590 | ||
6d519a5f | 591 | trace_paio_submit(acb, opaque, sector_num, nb_sectors, type); |
9ef91a67 CH |
592 | qemu_paio_submit(acb); |
593 | return &acb->common; | |
594 | } | |
595 | ||
596 | BlockDriverAIOCB *paio_ioctl(BlockDriverState *bs, int fd, | |
597 | unsigned long int req, void *buf, | |
598 | BlockDriverCompletionFunc *cb, void *opaque) | |
599 | { | |
600 | struct qemu_paiocb *acb; | |
601 | ||
602 | acb = qemu_aio_get(&raw_aio_pool, bs, cb, opaque); | |
603 | if (!acb) | |
604 | return NULL; | |
605 | acb->aio_type = QEMU_AIO_IOCTL; | |
606 | acb->aio_fildes = fd; | |
607 | acb->ev_signo = SIGUSR2; | |
34cf0081 | 608 | acb->async_context_id = get_async_context_id(); |
9ef91a67 CH |
609 | acb->aio_offset = 0; |
610 | acb->aio_ioctl_buf = buf; | |
611 | acb->aio_ioctl_cmd = req; | |
612 | ||
613 | acb->next = posix_aio_state->first_aio; | |
614 | posix_aio_state->first_aio = acb; | |
615 | ||
616 | qemu_paio_submit(acb); | |
617 | return &acb->common; | |
618 | } | |
619 | ||
1e5b9d2f | 620 | int paio_init(void) |
9ef91a67 CH |
621 | { |
622 | struct sigaction act; | |
623 | PosixAioState *s; | |
624 | int fds[2]; | |
625 | int ret; | |
626 | ||
627 | if (posix_aio_state) | |
1e5b9d2f | 628 | return 0; |
9ef91a67 CH |
629 | |
630 | s = qemu_malloc(sizeof(PosixAioState)); | |
631 | ||
632 | sigfillset(&act.sa_mask); | |
633 | act.sa_flags = 0; /* do not restart syscalls to interrupt select() */ | |
634 | act.sa_handler = aio_signal_handler; | |
635 | sigaction(SIGUSR2, &act, NULL); | |
636 | ||
637 | s->first_aio = NULL; | |
40ff6d7e | 638 | if (qemu_pipe(fds) == -1) { |
9ef91a67 | 639 | fprintf(stderr, "failed to create pipe\n"); |
1e5b9d2f | 640 | return -1; |
9ef91a67 CH |
641 | } |
642 | ||
643 | s->rfd = fds[0]; | |
644 | s->wfd = fds[1]; | |
645 | ||
646 | fcntl(s->rfd, F_SETFL, O_NONBLOCK); | |
647 | fcntl(s->wfd, F_SETFL, O_NONBLOCK); | |
648 | ||
8febfa26 KW |
649 | qemu_aio_set_fd_handler(s->rfd, posix_aio_read, NULL, posix_aio_flush, |
650 | posix_aio_process_queue, s); | |
9ef91a67 CH |
651 | |
652 | ret = pthread_attr_init(&attr); | |
653 | if (ret) | |
654 | die2(ret, "pthread_attr_init"); | |
655 | ||
656 | ret = pthread_attr_setdetachstate(&attr, PTHREAD_CREATE_DETACHED); | |
657 | if (ret) | |
658 | die2(ret, "pthread_attr_setdetachstate"); | |
659 | ||
72cf2d4f | 660 | QTAILQ_INIT(&request_list); |
9ef91a67 CH |
661 | |
662 | posix_aio_state = s; | |
1e5b9d2f | 663 | return 0; |
3c529d93 | 664 | } |