2 * QEMU posix-aio emulation
4 * Copyright IBM, Corp. 2008
7 * Anthony Liguori <aliguori@us.ibm.com>
9 * This work is licensed under the terms of the GNU GPL, version 2. See
10 * the COPYING file in the top-level directory.
14 #include <sys/ioctl.h>
15 #include <sys/types.h>
25 #include "qemu-queue.h"
27 #include "qemu-common.h"
29 #include "block_int.h"
31 #include "block/raw-posix-aio.h"
35 BlockDriverAIOCB common
;
38 struct iovec
*aio_iov
;
43 #define aio_ioctl_cmd aio_nbytes /* for QEMU_AIO_IOCTL */
47 QTAILQ_ENTRY(qemu_paiocb
) node
;
51 struct qemu_paiocb
*next
;
56 typedef struct PosixAioState
{
58 struct qemu_paiocb
*first_aio
;
62 static pthread_mutex_t lock
= PTHREAD_MUTEX_INITIALIZER
;
63 static pthread_cond_t cond
= PTHREAD_COND_INITIALIZER
;
64 static pthread_t thread_id
;
65 static pthread_attr_t attr
;
66 static int max_threads
= 64;
67 static int cur_threads
= 0;
68 static int idle_threads
= 0;
69 static QTAILQ_HEAD(, qemu_paiocb
) request_list
;
72 static int preadv_present
= 1;
74 static int preadv_present
= 0;
77 static void die2(int err
, const char *what
)
79 fprintf(stderr
, "%s failed: %s\n", what
, strerror(err
));
83 static void die(const char *what
)
88 static void mutex_lock(pthread_mutex_t
*mutex
)
90 int ret
= pthread_mutex_lock(mutex
);
91 if (ret
) die2(ret
, "pthread_mutex_lock");
94 static void mutex_unlock(pthread_mutex_t
*mutex
)
96 int ret
= pthread_mutex_unlock(mutex
);
97 if (ret
) die2(ret
, "pthread_mutex_unlock");
100 static int cond_timedwait(pthread_cond_t
*cond
, pthread_mutex_t
*mutex
,
103 int ret
= pthread_cond_timedwait(cond
, mutex
, ts
);
104 if (ret
&& ret
!= ETIMEDOUT
) die2(ret
, "pthread_cond_timedwait");
108 static void cond_signal(pthread_cond_t
*cond
)
110 int ret
= pthread_cond_signal(cond
);
111 if (ret
) die2(ret
, "pthread_cond_signal");
114 static void thread_create(pthread_t
*thread
, pthread_attr_t
*attr
,
115 void *(*start_routine
)(void*), void *arg
)
117 int ret
= pthread_create(thread
, attr
, start_routine
, arg
);
118 if (ret
) die2(ret
, "pthread_create");
121 static ssize_t
handle_aiocb_ioctl(struct qemu_paiocb
*aiocb
)
125 ret
= ioctl(aiocb
->aio_fildes
, aiocb
->aio_ioctl_cmd
, aiocb
->aio_ioctl_buf
);
130 * This looks weird, but the aio code only consideres a request
131 * successfull if it has written the number full number of bytes.
133 * Now we overload aio_nbytes as aio_ioctl_cmd for the ioctl command,
134 * so in fact we return the ioctl command here to make posix_aio_read()
137 return aiocb
->aio_nbytes
;
140 static ssize_t
handle_aiocb_flush(struct qemu_paiocb
*aiocb
)
144 ret
= qemu_fdatasync(aiocb
->aio_fildes
);
153 qemu_preadv(int fd
, const struct iovec
*iov
, int nr_iov
, off_t offset
)
155 return preadv(fd
, iov
, nr_iov
, offset
);
159 qemu_pwritev(int fd
, const struct iovec
*iov
, int nr_iov
, off_t offset
)
161 return pwritev(fd
, iov
, nr_iov
, offset
);
167 qemu_preadv(int fd
, const struct iovec
*iov
, int nr_iov
, off_t offset
)
173 qemu_pwritev(int fd
, const struct iovec
*iov
, int nr_iov
, off_t offset
)
180 static ssize_t
handle_aiocb_rw_vector(struct qemu_paiocb
*aiocb
)
186 if (aiocb
->aio_type
& QEMU_AIO_WRITE
)
187 len
= qemu_pwritev(aiocb
->aio_fildes
,
190 aiocb
->aio_offset
+ offset
);
192 len
= qemu_preadv(aiocb
->aio_fildes
,
195 aiocb
->aio_offset
+ offset
);
196 } while (len
== -1 && errno
== EINTR
);
203 static ssize_t
handle_aiocb_rw_linear(struct qemu_paiocb
*aiocb
, char *buf
)
208 while (offset
< aiocb
->aio_nbytes
) {
209 if (aiocb
->aio_type
& QEMU_AIO_WRITE
)
210 len
= pwrite(aiocb
->aio_fildes
,
211 (const char *)buf
+ offset
,
212 aiocb
->aio_nbytes
- offset
,
213 aiocb
->aio_offset
+ offset
);
215 len
= pread(aiocb
->aio_fildes
,
217 aiocb
->aio_nbytes
- offset
,
218 aiocb
->aio_offset
+ offset
);
220 if (len
== -1 && errno
== EINTR
)
222 else if (len
== -1) {
234 static ssize_t
handle_aiocb_rw(struct qemu_paiocb
*aiocb
)
239 if (!(aiocb
->aio_type
& QEMU_AIO_MISALIGNED
)) {
241 * If there is just a single buffer, and it is properly aligned
242 * we can just use plain pread/pwrite without any problems.
244 if (aiocb
->aio_niov
== 1)
245 return handle_aiocb_rw_linear(aiocb
, aiocb
->aio_iov
->iov_base
);
248 * We have more than one iovec, and all are properly aligned.
250 * Try preadv/pwritev first and fall back to linearizing the
251 * buffer if it's not supported.
253 if (preadv_present
) {
254 nbytes
= handle_aiocb_rw_vector(aiocb
);
255 if (nbytes
== aiocb
->aio_nbytes
)
257 if (nbytes
< 0 && nbytes
!= -ENOSYS
)
263 * XXX(hch): short read/write. no easy way to handle the reminder
264 * using these interfaces. For now retry using plain
270 * Ok, we have to do it the hard way, copy all segments into
271 * a single aligned buffer.
273 buf
= qemu_blockalign(aiocb
->common
.bs
, aiocb
->aio_nbytes
);
274 if (aiocb
->aio_type
& QEMU_AIO_WRITE
) {
278 for (i
= 0; i
< aiocb
->aio_niov
; ++i
) {
279 memcpy(p
, aiocb
->aio_iov
[i
].iov_base
, aiocb
->aio_iov
[i
].iov_len
);
280 p
+= aiocb
->aio_iov
[i
].iov_len
;
284 nbytes
= handle_aiocb_rw_linear(aiocb
, buf
);
285 if (!(aiocb
->aio_type
& QEMU_AIO_WRITE
)) {
287 size_t count
= aiocb
->aio_nbytes
, copy
;
290 for (i
= 0; i
< aiocb
->aio_niov
&& count
; ++i
) {
292 if (copy
> aiocb
->aio_iov
[i
].iov_len
)
293 copy
= aiocb
->aio_iov
[i
].iov_len
;
294 memcpy(aiocb
->aio_iov
[i
].iov_base
, p
, copy
);
304 static void *aio_thread(void *unused
)
311 struct qemu_paiocb
*aiocb
;
316 qemu_gettimeofday(&tv
);
317 ts
.tv_sec
= tv
.tv_sec
+ 10;
322 while (QTAILQ_EMPTY(&request_list
) &&
323 !(ret
== ETIMEDOUT
)) {
324 ret
= cond_timedwait(&cond
, &lock
, &ts
);
327 if (QTAILQ_EMPTY(&request_list
))
330 aiocb
= QTAILQ_FIRST(&request_list
);
331 QTAILQ_REMOVE(&request_list
, aiocb
, node
);
336 switch (aiocb
->aio_type
& QEMU_AIO_TYPE_MASK
) {
339 ret
= handle_aiocb_rw(aiocb
);
342 ret
= handle_aiocb_flush(aiocb
);
345 ret
= handle_aiocb_ioctl(aiocb
);
348 fprintf(stderr
, "invalid aio request (0x%x)\n", aiocb
->aio_type
);
358 if (kill(pid
, aiocb
->ev_signo
)) die("kill failed");
368 static void spawn_thread(void)
370 sigset_t set
, oldset
;
375 /* block all signals */
376 if (sigfillset(&set
)) die("sigfillset");
377 if (sigprocmask(SIG_SETMASK
, &set
, &oldset
)) die("sigprocmask");
379 thread_create(&thread_id
, &attr
, aio_thread
, NULL
);
381 if (sigprocmask(SIG_SETMASK
, &oldset
, NULL
)) die("sigprocmask restore");
384 static void qemu_paio_submit(struct qemu_paiocb
*aiocb
)
386 aiocb
->ret
= -EINPROGRESS
;
389 if (idle_threads
== 0 && cur_threads
< max_threads
)
391 QTAILQ_INSERT_TAIL(&request_list
, aiocb
, node
);
396 static ssize_t
qemu_paio_return(struct qemu_paiocb
*aiocb
)
407 static int qemu_paio_error(struct qemu_paiocb
*aiocb
)
409 ssize_t ret
= qemu_paio_return(aiocb
);
419 static int posix_aio_process_queue(void *opaque
)
421 PosixAioState
*s
= opaque
;
422 struct qemu_paiocb
*acb
, **pacb
;
425 int async_context_id
= get_async_context_id();
428 pacb
= &s
->first_aio
;
434 /* we're only interested in requests in the right context */
435 if (acb
->async_context_id
!= async_context_id
) {
440 ret
= qemu_paio_error(acb
);
441 if (ret
== ECANCELED
) {
442 /* remove the request */
444 qemu_aio_release(acb
);
446 } else if (ret
!= EINPROGRESS
) {
449 ret
= qemu_paio_return(acb
);
450 if (ret
== acb
->aio_nbytes
)
457 /* remove the request */
459 /* call the callback */
460 acb
->common
.cb(acb
->common
.opaque
, ret
);
461 qemu_aio_release(acb
);
473 static void posix_aio_read(void *opaque
)
475 PosixAioState
*s
= opaque
;
478 /* read all bytes from signal pipe */
482 len
= read(s
->rfd
, bytes
, sizeof(bytes
));
483 if (len
== -1 && errno
== EINTR
)
484 continue; /* try again */
485 if (len
== sizeof(bytes
))
486 continue; /* more to read */
490 posix_aio_process_queue(s
);
493 static int posix_aio_flush(void *opaque
)
495 PosixAioState
*s
= opaque
;
496 return !!s
->first_aio
;
499 static PosixAioState
*posix_aio_state
;
501 static void aio_signal_handler(int signum
)
503 if (posix_aio_state
) {
507 ret
= write(posix_aio_state
->wfd
, &byte
, sizeof(byte
));
508 if (ret
< 0 && errno
!= EAGAIN
)
515 static void paio_remove(struct qemu_paiocb
*acb
)
517 struct qemu_paiocb
**pacb
;
519 /* remove the callback from the queue */
520 pacb
= &posix_aio_state
->first_aio
;
523 fprintf(stderr
, "paio_remove: aio request not found!\n");
525 } else if (*pacb
== acb
) {
527 qemu_aio_release(acb
);
530 pacb
= &(*pacb
)->next
;
534 static void paio_cancel(BlockDriverAIOCB
*blockacb
)
536 struct qemu_paiocb
*acb
= (struct qemu_paiocb
*)blockacb
;
541 QTAILQ_REMOVE(&request_list
, acb
, node
);
542 acb
->ret
= -ECANCELED
;
543 } else if (acb
->ret
== -EINPROGRESS
) {
549 /* fail safe: if the aio could not be canceled, we wait for
551 while (qemu_paio_error(acb
) == EINPROGRESS
)
558 static AIOPool raw_aio_pool
= {
559 .aiocb_size
= sizeof(struct qemu_paiocb
),
560 .cancel
= paio_cancel
,
563 BlockDriverAIOCB
*paio_submit(BlockDriverState
*bs
, int fd
,
564 int64_t sector_num
, QEMUIOVector
*qiov
, int nb_sectors
,
565 BlockDriverCompletionFunc
*cb
, void *opaque
, int type
)
567 struct qemu_paiocb
*acb
;
569 acb
= qemu_aio_get(&raw_aio_pool
, bs
, cb
, opaque
);
572 acb
->aio_type
= type
;
573 acb
->aio_fildes
= fd
;
574 acb
->ev_signo
= SIGUSR2
;
575 acb
->async_context_id
= get_async_context_id();
578 acb
->aio_iov
= qiov
->iov
;
579 acb
->aio_niov
= qiov
->niov
;
581 acb
->aio_nbytes
= nb_sectors
* 512;
582 acb
->aio_offset
= sector_num
* 512;
584 acb
->next
= posix_aio_state
->first_aio
;
585 posix_aio_state
->first_aio
= acb
;
587 trace_paio_submit(acb
, opaque
, sector_num
, nb_sectors
, type
);
588 qemu_paio_submit(acb
);
592 BlockDriverAIOCB
*paio_ioctl(BlockDriverState
*bs
, int fd
,
593 unsigned long int req
, void *buf
,
594 BlockDriverCompletionFunc
*cb
, void *opaque
)
596 struct qemu_paiocb
*acb
;
598 acb
= qemu_aio_get(&raw_aio_pool
, bs
, cb
, opaque
);
601 acb
->aio_type
= QEMU_AIO_IOCTL
;
602 acb
->aio_fildes
= fd
;
603 acb
->ev_signo
= SIGUSR2
;
604 acb
->async_context_id
= get_async_context_id();
606 acb
->aio_ioctl_buf
= buf
;
607 acb
->aio_ioctl_cmd
= req
;
609 acb
->next
= posix_aio_state
->first_aio
;
610 posix_aio_state
->first_aio
= acb
;
612 qemu_paio_submit(acb
);
618 struct sigaction act
;
626 s
= qemu_malloc(sizeof(PosixAioState
));
628 sigfillset(&act
.sa_mask
);
629 act
.sa_flags
= 0; /* do not restart syscalls to interrupt select() */
630 act
.sa_handler
= aio_signal_handler
;
631 sigaction(SIGUSR2
, &act
, NULL
);
634 if (qemu_pipe(fds
) == -1) {
635 fprintf(stderr
, "failed to create pipe\n");
642 fcntl(s
->rfd
, F_SETFL
, O_NONBLOCK
);
643 fcntl(s
->wfd
, F_SETFL
, O_NONBLOCK
);
645 qemu_aio_set_fd_handler(s
->rfd
, posix_aio_read
, NULL
, posix_aio_flush
,
646 posix_aio_process_queue
, s
);
648 ret
= pthread_attr_init(&attr
);
650 die2(ret
, "pthread_attr_init");
652 ret
= pthread_attr_setdetachstate(&attr
, PTHREAD_CREATE_DETACHED
);
654 die2(ret
, "pthread_attr_setdetachstate");
656 QTAILQ_INIT(&request_list
);