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>
24 #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
;
54 typedef struct PosixAioState
{
56 struct qemu_paiocb
*first_aio
;
60 static pthread_mutex_t lock
= PTHREAD_MUTEX_INITIALIZER
;
61 static pthread_cond_t cond
= PTHREAD_COND_INITIALIZER
;
62 static pthread_t thread_id
;
63 static pthread_attr_t attr
;
64 static int max_threads
= 64;
65 static int cur_threads
= 0;
66 static int idle_threads
= 0;
67 static QTAILQ_HEAD(, qemu_paiocb
) request_list
;
70 static int preadv_present
= 1;
72 static int preadv_present
= 0;
75 static void die2(int err
, const char *what
)
77 fprintf(stderr
, "%s failed: %s\n", what
, strerror(err
));
81 static void die(const char *what
)
86 static void mutex_lock(pthread_mutex_t
*mutex
)
88 int ret
= pthread_mutex_lock(mutex
);
89 if (ret
) die2(ret
, "pthread_mutex_lock");
92 static void mutex_unlock(pthread_mutex_t
*mutex
)
94 int ret
= pthread_mutex_unlock(mutex
);
95 if (ret
) die2(ret
, "pthread_mutex_unlock");
98 static int cond_timedwait(pthread_cond_t
*cond
, pthread_mutex_t
*mutex
,
101 int ret
= pthread_cond_timedwait(cond
, mutex
, ts
);
102 if (ret
&& ret
!= ETIMEDOUT
) die2(ret
, "pthread_cond_timedwait");
106 static void cond_signal(pthread_cond_t
*cond
)
108 int ret
= pthread_cond_signal(cond
);
109 if (ret
) die2(ret
, "pthread_cond_signal");
112 static void thread_create(pthread_t
*thread
, pthread_attr_t
*attr
,
113 void *(*start_routine
)(void*), void *arg
)
115 int ret
= pthread_create(thread
, attr
, start_routine
, arg
);
116 if (ret
) die2(ret
, "pthread_create");
119 static ssize_t
handle_aiocb_ioctl(struct qemu_paiocb
*aiocb
)
123 ret
= ioctl(aiocb
->aio_fildes
, aiocb
->aio_ioctl_cmd
, aiocb
->aio_ioctl_buf
);
128 * This looks weird, but the aio code only consideres a request
129 * successful if it has written the number full number of bytes.
131 * Now we overload aio_nbytes as aio_ioctl_cmd for the ioctl command,
132 * so in fact we return the ioctl command here to make posix_aio_read()
135 return aiocb
->aio_nbytes
;
138 static ssize_t
handle_aiocb_flush(struct qemu_paiocb
*aiocb
)
142 ret
= qemu_fdatasync(aiocb
->aio_fildes
);
151 qemu_preadv(int fd
, const struct iovec
*iov
, int nr_iov
, off_t offset
)
153 return preadv(fd
, iov
, nr_iov
, offset
);
157 qemu_pwritev(int fd
, const struct iovec
*iov
, int nr_iov
, off_t offset
)
159 return pwritev(fd
, iov
, nr_iov
, offset
);
165 qemu_preadv(int fd
, const struct iovec
*iov
, int nr_iov
, off_t offset
)
171 qemu_pwritev(int fd
, const struct iovec
*iov
, int nr_iov
, off_t offset
)
178 static ssize_t
handle_aiocb_rw_vector(struct qemu_paiocb
*aiocb
)
184 if (aiocb
->aio_type
& QEMU_AIO_WRITE
)
185 len
= qemu_pwritev(aiocb
->aio_fildes
,
188 aiocb
->aio_offset
+ offset
);
190 len
= qemu_preadv(aiocb
->aio_fildes
,
193 aiocb
->aio_offset
+ offset
);
194 } while (len
== -1 && errno
== EINTR
);
202 * Read/writes the data to/from a given linear buffer.
204 * Returns the number of bytes handles or -errno in case of an error. Short
205 * reads are only returned if the end of the file is reached.
207 static ssize_t
handle_aiocb_rw_linear(struct qemu_paiocb
*aiocb
, char *buf
)
212 while (offset
< aiocb
->aio_nbytes
) {
213 if (aiocb
->aio_type
& QEMU_AIO_WRITE
)
214 len
= pwrite(aiocb
->aio_fildes
,
215 (const char *)buf
+ offset
,
216 aiocb
->aio_nbytes
- offset
,
217 aiocb
->aio_offset
+ offset
);
219 len
= pread(aiocb
->aio_fildes
,
221 aiocb
->aio_nbytes
- offset
,
222 aiocb
->aio_offset
+ offset
);
224 if (len
== -1 && errno
== EINTR
)
226 else if (len
== -1) {
238 static ssize_t
handle_aiocb_rw(struct qemu_paiocb
*aiocb
)
243 if (!(aiocb
->aio_type
& QEMU_AIO_MISALIGNED
)) {
245 * If there is just a single buffer, and it is properly aligned
246 * we can just use plain pread/pwrite without any problems.
248 if (aiocb
->aio_niov
== 1)
249 return handle_aiocb_rw_linear(aiocb
, aiocb
->aio_iov
->iov_base
);
252 * We have more than one iovec, and all are properly aligned.
254 * Try preadv/pwritev first and fall back to linearizing the
255 * buffer if it's not supported.
257 if (preadv_present
) {
258 nbytes
= handle_aiocb_rw_vector(aiocb
);
259 if (nbytes
== aiocb
->aio_nbytes
)
261 if (nbytes
< 0 && nbytes
!= -ENOSYS
)
267 * XXX(hch): short read/write. no easy way to handle the reminder
268 * using these interfaces. For now retry using plain
274 * Ok, we have to do it the hard way, copy all segments into
275 * a single aligned buffer.
277 buf
= qemu_blockalign(aiocb
->common
.bs
, aiocb
->aio_nbytes
);
278 if (aiocb
->aio_type
& QEMU_AIO_WRITE
) {
282 for (i
= 0; i
< aiocb
->aio_niov
; ++i
) {
283 memcpy(p
, aiocb
->aio_iov
[i
].iov_base
, aiocb
->aio_iov
[i
].iov_len
);
284 p
+= aiocb
->aio_iov
[i
].iov_len
;
288 nbytes
= handle_aiocb_rw_linear(aiocb
, buf
);
289 if (!(aiocb
->aio_type
& QEMU_AIO_WRITE
)) {
291 size_t count
= aiocb
->aio_nbytes
, copy
;
294 for (i
= 0; i
< aiocb
->aio_niov
&& count
; ++i
) {
296 if (copy
> aiocb
->aio_iov
[i
].iov_len
)
297 copy
= aiocb
->aio_iov
[i
].iov_len
;
298 memcpy(aiocb
->aio_iov
[i
].iov_base
, p
, copy
);
308 static void *aio_thread(void *unused
)
315 struct qemu_paiocb
*aiocb
;
320 qemu_gettimeofday(&tv
);
321 ts
.tv_sec
= tv
.tv_sec
+ 10;
326 while (QTAILQ_EMPTY(&request_list
) &&
327 !(ret
== ETIMEDOUT
)) {
329 ret
= cond_timedwait(&cond
, &lock
, &ts
);
333 if (QTAILQ_EMPTY(&request_list
))
336 aiocb
= QTAILQ_FIRST(&request_list
);
337 QTAILQ_REMOVE(&request_list
, aiocb
, node
);
341 switch (aiocb
->aio_type
& QEMU_AIO_TYPE_MASK
) {
343 ret
= handle_aiocb_rw(aiocb
);
344 if (ret
>= 0 && ret
< aiocb
->aio_nbytes
&& aiocb
->common
.bs
->growable
) {
345 /* A short read means that we have reached EOF. Pad the buffer
346 * with zeros for bytes after EOF. */
349 qemu_iovec_init_external(&qiov
, aiocb
->aio_iov
,
351 qemu_iovec_memset_skip(&qiov
, 0, aiocb
->aio_nbytes
- ret
, ret
);
353 ret
= aiocb
->aio_nbytes
;
357 ret
= handle_aiocb_rw(aiocb
);
360 ret
= handle_aiocb_flush(aiocb
);
363 ret
= handle_aiocb_ioctl(aiocb
);
366 fprintf(stderr
, "invalid aio request (0x%x)\n", aiocb
->aio_type
);
375 if (kill(pid
, aiocb
->ev_signo
)) die("kill failed");
384 static void spawn_thread(void)
386 sigset_t set
, oldset
;
390 /* block all signals */
391 if (sigfillset(&set
)) die("sigfillset");
392 if (sigprocmask(SIG_SETMASK
, &set
, &oldset
)) die("sigprocmask");
394 thread_create(&thread_id
, &attr
, aio_thread
, NULL
);
396 if (sigprocmask(SIG_SETMASK
, &oldset
, NULL
)) die("sigprocmask restore");
399 static void qemu_paio_submit(struct qemu_paiocb
*aiocb
)
401 aiocb
->ret
= -EINPROGRESS
;
404 if (idle_threads
== 0 && cur_threads
< max_threads
)
406 QTAILQ_INSERT_TAIL(&request_list
, aiocb
, node
);
411 static ssize_t
qemu_paio_return(struct qemu_paiocb
*aiocb
)
422 static int qemu_paio_error(struct qemu_paiocb
*aiocb
)
424 ssize_t ret
= qemu_paio_return(aiocb
);
434 static int posix_aio_process_queue(void *opaque
)
436 PosixAioState
*s
= opaque
;
437 struct qemu_paiocb
*acb
, **pacb
;
442 pacb
= &s
->first_aio
;
448 ret
= qemu_paio_error(acb
);
449 if (ret
== ECANCELED
) {
450 /* remove the request */
452 qemu_aio_release(acb
);
454 } else if (ret
!= EINPROGRESS
) {
457 ret
= qemu_paio_return(acb
);
458 if (ret
== acb
->aio_nbytes
)
466 trace_paio_complete(acb
, acb
->common
.opaque
, ret
);
468 /* remove the request */
470 /* call the callback */
471 acb
->common
.cb(acb
->common
.opaque
, ret
);
472 qemu_aio_release(acb
);
484 static void posix_aio_read(void *opaque
)
486 PosixAioState
*s
= opaque
;
489 /* read all bytes from signal pipe */
493 len
= read(s
->rfd
, bytes
, sizeof(bytes
));
494 if (len
== -1 && errno
== EINTR
)
495 continue; /* try again */
496 if (len
== sizeof(bytes
))
497 continue; /* more to read */
501 posix_aio_process_queue(s
);
504 static int posix_aio_flush(void *opaque
)
506 PosixAioState
*s
= opaque
;
507 return !!s
->first_aio
;
510 static PosixAioState
*posix_aio_state
;
512 static void aio_signal_handler(int signum
)
514 if (posix_aio_state
) {
518 ret
= write(posix_aio_state
->wfd
, &byte
, sizeof(byte
));
519 if (ret
< 0 && errno
!= EAGAIN
)
526 static void paio_remove(struct qemu_paiocb
*acb
)
528 struct qemu_paiocb
**pacb
;
530 /* remove the callback from the queue */
531 pacb
= &posix_aio_state
->first_aio
;
534 fprintf(stderr
, "paio_remove: aio request not found!\n");
536 } else if (*pacb
== acb
) {
538 qemu_aio_release(acb
);
541 pacb
= &(*pacb
)->next
;
545 static void paio_cancel(BlockDriverAIOCB
*blockacb
)
547 struct qemu_paiocb
*acb
= (struct qemu_paiocb
*)blockacb
;
550 trace_paio_cancel(acb
, acb
->common
.opaque
);
554 QTAILQ_REMOVE(&request_list
, acb
, node
);
555 acb
->ret
= -ECANCELED
;
556 } else if (acb
->ret
== -EINPROGRESS
) {
562 /* fail safe: if the aio could not be canceled, we wait for
564 while (qemu_paio_error(acb
) == EINPROGRESS
)
571 static AIOPool raw_aio_pool
= {
572 .aiocb_size
= sizeof(struct qemu_paiocb
),
573 .cancel
= paio_cancel
,
576 BlockDriverAIOCB
*paio_submit(BlockDriverState
*bs
, int fd
,
577 int64_t sector_num
, QEMUIOVector
*qiov
, int nb_sectors
,
578 BlockDriverCompletionFunc
*cb
, void *opaque
, int type
)
580 struct qemu_paiocb
*acb
;
582 acb
= qemu_aio_get(&raw_aio_pool
, bs
, cb
, opaque
);
585 acb
->aio_type
= type
;
586 acb
->aio_fildes
= fd
;
587 acb
->ev_signo
= SIGUSR2
;
590 acb
->aio_iov
= qiov
->iov
;
591 acb
->aio_niov
= qiov
->niov
;
593 acb
->aio_nbytes
= nb_sectors
* 512;
594 acb
->aio_offset
= sector_num
* 512;
596 acb
->next
= posix_aio_state
->first_aio
;
597 posix_aio_state
->first_aio
= acb
;
599 trace_paio_submit(acb
, opaque
, sector_num
, nb_sectors
, type
);
600 qemu_paio_submit(acb
);
604 BlockDriverAIOCB
*paio_ioctl(BlockDriverState
*bs
, int fd
,
605 unsigned long int req
, void *buf
,
606 BlockDriverCompletionFunc
*cb
, void *opaque
)
608 struct qemu_paiocb
*acb
;
610 acb
= qemu_aio_get(&raw_aio_pool
, bs
, cb
, opaque
);
613 acb
->aio_type
= QEMU_AIO_IOCTL
;
614 acb
->aio_fildes
= fd
;
615 acb
->ev_signo
= SIGUSR2
;
617 acb
->aio_ioctl_buf
= buf
;
618 acb
->aio_ioctl_cmd
= req
;
620 acb
->next
= posix_aio_state
->first_aio
;
621 posix_aio_state
->first_aio
= acb
;
623 qemu_paio_submit(acb
);
629 struct sigaction act
;
637 s
= qemu_malloc(sizeof(PosixAioState
));
639 sigfillset(&act
.sa_mask
);
640 act
.sa_flags
= 0; /* do not restart syscalls to interrupt select() */
641 act
.sa_handler
= aio_signal_handler
;
642 sigaction(SIGUSR2
, &act
, NULL
);
645 if (qemu_pipe(fds
) == -1) {
646 fprintf(stderr
, "failed to create pipe\n");
653 fcntl(s
->rfd
, F_SETFL
, O_NONBLOCK
);
654 fcntl(s
->wfd
, F_SETFL
, O_NONBLOCK
);
656 qemu_aio_set_fd_handler(s
->rfd
, posix_aio_read
, NULL
, posix_aio_flush
,
657 posix_aio_process_queue
, s
);
659 ret
= pthread_attr_init(&attr
);
661 die2(ret
, "pthread_attr_init");
663 ret
= pthread_attr_setdetachstate(&attr
, PTHREAD_CREATE_DETACHED
);
665 die2(ret
, "pthread_attr_setdetachstate");
667 QTAILQ_INIT(&request_list
);