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"
32 #include "block/raw-posix-aio.h"
34 static void do_spawn_thread(void);
37 BlockDriverAIOCB common
;
40 struct iovec
*aio_iov
;
45 #define aio_ioctl_cmd aio_nbytes /* for QEMU_AIO_IOCTL */
49 QTAILQ_ENTRY(qemu_paiocb
) node
;
53 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 int new_threads
= 0; /* backlog of threads we need to create */
70 static int pending_threads
= 0; /* threads created but not running yet */
71 static QEMUBH
*new_thread_bh
;
72 static QTAILQ_HEAD(, qemu_paiocb
) request_list
;
75 static int preadv_present
= 1;
77 static int preadv_present
= 0;
80 static void die2(int err
, const char *what
)
82 fprintf(stderr
, "%s failed: %s\n", what
, strerror(err
));
86 static void die(const char *what
)
91 static void mutex_lock(pthread_mutex_t
*mutex
)
93 int ret
= pthread_mutex_lock(mutex
);
94 if (ret
) die2(ret
, "pthread_mutex_lock");
97 static void mutex_unlock(pthread_mutex_t
*mutex
)
99 int ret
= pthread_mutex_unlock(mutex
);
100 if (ret
) die2(ret
, "pthread_mutex_unlock");
103 static int cond_timedwait(pthread_cond_t
*cond
, pthread_mutex_t
*mutex
,
106 int ret
= pthread_cond_timedwait(cond
, mutex
, ts
);
107 if (ret
&& ret
!= ETIMEDOUT
) die2(ret
, "pthread_cond_timedwait");
111 static void cond_signal(pthread_cond_t
*cond
)
113 int ret
= pthread_cond_signal(cond
);
114 if (ret
) die2(ret
, "pthread_cond_signal");
117 static void thread_create(pthread_t
*thread
, pthread_attr_t
*attr
,
118 void *(*start_routine
)(void*), void *arg
)
120 int ret
= pthread_create(thread
, attr
, start_routine
, arg
);
121 if (ret
) die2(ret
, "pthread_create");
124 static ssize_t
handle_aiocb_ioctl(struct qemu_paiocb
*aiocb
)
128 ret
= ioctl(aiocb
->aio_fildes
, aiocb
->aio_ioctl_cmd
, aiocb
->aio_ioctl_buf
);
133 * This looks weird, but the aio code only consideres a request
134 * successful if it has written the number full number of bytes.
136 * Now we overload aio_nbytes as aio_ioctl_cmd for the ioctl command,
137 * so in fact we return the ioctl command here to make posix_aio_read()
140 return aiocb
->aio_nbytes
;
143 static ssize_t
handle_aiocb_flush(struct qemu_paiocb
*aiocb
)
147 ret
= qemu_fdatasync(aiocb
->aio_fildes
);
156 qemu_preadv(int fd
, const struct iovec
*iov
, int nr_iov
, off_t offset
)
158 return preadv(fd
, iov
, nr_iov
, offset
);
162 qemu_pwritev(int fd
, const struct iovec
*iov
, int nr_iov
, off_t offset
)
164 return pwritev(fd
, iov
, nr_iov
, offset
);
170 qemu_preadv(int fd
, const struct iovec
*iov
, int nr_iov
, off_t offset
)
176 qemu_pwritev(int fd
, const struct iovec
*iov
, int nr_iov
, off_t offset
)
183 static ssize_t
handle_aiocb_rw_vector(struct qemu_paiocb
*aiocb
)
189 if (aiocb
->aio_type
& QEMU_AIO_WRITE
)
190 len
= qemu_pwritev(aiocb
->aio_fildes
,
193 aiocb
->aio_offset
+ offset
);
195 len
= qemu_preadv(aiocb
->aio_fildes
,
198 aiocb
->aio_offset
+ offset
);
199 } while (len
== -1 && errno
== EINTR
);
207 * Read/writes the data to/from a given linear buffer.
209 * Returns the number of bytes handles or -errno in case of an error. Short
210 * reads are only returned if the end of the file is reached.
212 static ssize_t
handle_aiocb_rw_linear(struct qemu_paiocb
*aiocb
, char *buf
)
217 while (offset
< aiocb
->aio_nbytes
) {
218 if (aiocb
->aio_type
& QEMU_AIO_WRITE
)
219 len
= pwrite(aiocb
->aio_fildes
,
220 (const char *)buf
+ offset
,
221 aiocb
->aio_nbytes
- offset
,
222 aiocb
->aio_offset
+ offset
);
224 len
= pread(aiocb
->aio_fildes
,
226 aiocb
->aio_nbytes
- offset
,
227 aiocb
->aio_offset
+ offset
);
229 if (len
== -1 && errno
== EINTR
)
231 else if (len
== -1) {
243 static ssize_t
handle_aiocb_rw(struct qemu_paiocb
*aiocb
)
248 if (!(aiocb
->aio_type
& QEMU_AIO_MISALIGNED
)) {
250 * If there is just a single buffer, and it is properly aligned
251 * we can just use plain pread/pwrite without any problems.
253 if (aiocb
->aio_niov
== 1)
254 return handle_aiocb_rw_linear(aiocb
, aiocb
->aio_iov
->iov_base
);
257 * We have more than one iovec, and all are properly aligned.
259 * Try preadv/pwritev first and fall back to linearizing the
260 * buffer if it's not supported.
262 if (preadv_present
) {
263 nbytes
= handle_aiocb_rw_vector(aiocb
);
264 if (nbytes
== aiocb
->aio_nbytes
)
266 if (nbytes
< 0 && nbytes
!= -ENOSYS
)
272 * XXX(hch): short read/write. no easy way to handle the reminder
273 * using these interfaces. For now retry using plain
279 * Ok, we have to do it the hard way, copy all segments into
280 * a single aligned buffer.
282 buf
= qemu_blockalign(aiocb
->common
.bs
, aiocb
->aio_nbytes
);
283 if (aiocb
->aio_type
& QEMU_AIO_WRITE
) {
287 for (i
= 0; i
< aiocb
->aio_niov
; ++i
) {
288 memcpy(p
, aiocb
->aio_iov
[i
].iov_base
, aiocb
->aio_iov
[i
].iov_len
);
289 p
+= aiocb
->aio_iov
[i
].iov_len
;
293 nbytes
= handle_aiocb_rw_linear(aiocb
, buf
);
294 if (!(aiocb
->aio_type
& QEMU_AIO_WRITE
)) {
296 size_t count
= aiocb
->aio_nbytes
, copy
;
299 for (i
= 0; i
< aiocb
->aio_niov
&& count
; ++i
) {
301 if (copy
> aiocb
->aio_iov
[i
].iov_len
)
302 copy
= aiocb
->aio_iov
[i
].iov_len
;
303 memcpy(aiocb
->aio_iov
[i
].iov_base
, p
, copy
);
313 static void *aio_thread(void *unused
)
325 struct qemu_paiocb
*aiocb
;
330 qemu_gettimeofday(&tv
);
331 ts
.tv_sec
= tv
.tv_sec
+ 10;
336 while (QTAILQ_EMPTY(&request_list
) &&
337 !(ret
== ETIMEDOUT
)) {
339 ret
= cond_timedwait(&cond
, &lock
, &ts
);
343 if (QTAILQ_EMPTY(&request_list
))
346 aiocb
= QTAILQ_FIRST(&request_list
);
347 QTAILQ_REMOVE(&request_list
, aiocb
, node
);
351 switch (aiocb
->aio_type
& QEMU_AIO_TYPE_MASK
) {
353 ret
= handle_aiocb_rw(aiocb
);
354 if (ret
>= 0 && ret
< aiocb
->aio_nbytes
&& aiocb
->common
.bs
->growable
) {
355 /* A short read means that we have reached EOF. Pad the buffer
356 * with zeros for bytes after EOF. */
359 qemu_iovec_init_external(&qiov
, aiocb
->aio_iov
,
361 qemu_iovec_memset_skip(&qiov
, 0, aiocb
->aio_nbytes
- ret
, ret
);
363 ret
= aiocb
->aio_nbytes
;
367 ret
= handle_aiocb_rw(aiocb
);
370 ret
= handle_aiocb_flush(aiocb
);
373 ret
= handle_aiocb_ioctl(aiocb
);
376 fprintf(stderr
, "invalid aio request (0x%x)\n", aiocb
->aio_type
);
385 if (kill(pid
, aiocb
->ev_signo
)) die("kill failed");
394 static void do_spawn_thread(void)
396 sigset_t set
, oldset
;
409 /* block all signals */
410 if (sigfillset(&set
)) die("sigfillset");
411 if (sigprocmask(SIG_SETMASK
, &set
, &oldset
)) die("sigprocmask");
413 thread_create(&thread_id
, &attr
, aio_thread
, NULL
);
415 if (sigprocmask(SIG_SETMASK
, &oldset
, NULL
)) die("sigprocmask restore");
418 static void spawn_thread_bh_fn(void *opaque
)
423 static void spawn_thread(void)
427 /* If there are threads being created, they will spawn new workers, so
428 * we don't spend time creating many threads in a loop holding a mutex or
429 * starving the current vcpu.
431 * If there are no idle threads, ask the main thread to create one, so we
432 * inherit the correct affinity instead of the vcpu affinity.
434 if (!pending_threads
) {
435 qemu_bh_schedule(new_thread_bh
);
439 static void qemu_paio_submit(struct qemu_paiocb
*aiocb
)
441 aiocb
->ret
= -EINPROGRESS
;
444 if (idle_threads
== 0 && cur_threads
< max_threads
)
446 QTAILQ_INSERT_TAIL(&request_list
, aiocb
, node
);
451 static ssize_t
qemu_paio_return(struct qemu_paiocb
*aiocb
)
462 static int qemu_paio_error(struct qemu_paiocb
*aiocb
)
464 ssize_t ret
= qemu_paio_return(aiocb
);
474 static int posix_aio_process_queue(void *opaque
)
476 PosixAioState
*s
= opaque
;
477 struct qemu_paiocb
*acb
, **pacb
;
482 pacb
= &s
->first_aio
;
488 ret
= qemu_paio_error(acb
);
489 if (ret
== ECANCELED
) {
490 /* remove the request */
492 qemu_aio_release(acb
);
494 } else if (ret
!= EINPROGRESS
) {
497 ret
= qemu_paio_return(acb
);
498 if (ret
== acb
->aio_nbytes
)
506 trace_paio_complete(acb
, acb
->common
.opaque
, ret
);
508 /* remove the request */
510 /* call the callback */
511 acb
->common
.cb(acb
->common
.opaque
, ret
);
512 qemu_aio_release(acb
);
524 static void posix_aio_read(void *opaque
)
526 PosixAioState
*s
= opaque
;
528 struct qemu_signalfd_siginfo siginfo
;
533 /* try to read from signalfd, don't freak out if we can't read anything */
535 while (offset
< 128) {
538 len
= read(s
->fd
, sig
.buf
+ offset
, 128 - offset
);
539 if (len
== -1 && errno
== EINTR
)
541 if (len
== -1 && errno
== EAGAIN
) {
542 /* there is no natural reason for this to happen,
543 * so we'll spin hard until we get everything just
544 * to be on the safe side. */
552 posix_aio_process_queue(s
);
555 static int posix_aio_flush(void *opaque
)
557 PosixAioState
*s
= opaque
;
558 return !!s
->first_aio
;
561 static PosixAioState
*posix_aio_state
;
563 static void paio_remove(struct qemu_paiocb
*acb
)
565 struct qemu_paiocb
**pacb
;
567 /* remove the callback from the queue */
568 pacb
= &posix_aio_state
->first_aio
;
571 fprintf(stderr
, "paio_remove: aio request not found!\n");
573 } else if (*pacb
== acb
) {
575 qemu_aio_release(acb
);
578 pacb
= &(*pacb
)->next
;
582 static void paio_cancel(BlockDriverAIOCB
*blockacb
)
584 struct qemu_paiocb
*acb
= (struct qemu_paiocb
*)blockacb
;
587 trace_paio_cancel(acb
, acb
->common
.opaque
);
591 QTAILQ_REMOVE(&request_list
, acb
, node
);
592 acb
->ret
= -ECANCELED
;
593 } else if (acb
->ret
== -EINPROGRESS
) {
599 /* fail safe: if the aio could not be canceled, we wait for
601 while (qemu_paio_error(acb
) == EINPROGRESS
)
608 static AIOPool raw_aio_pool
= {
609 .aiocb_size
= sizeof(struct qemu_paiocb
),
610 .cancel
= paio_cancel
,
613 BlockDriverAIOCB
*paio_submit(BlockDriverState
*bs
, int fd
,
614 int64_t sector_num
, QEMUIOVector
*qiov
, int nb_sectors
,
615 BlockDriverCompletionFunc
*cb
, void *opaque
, int type
)
617 struct qemu_paiocb
*acb
;
619 acb
= qemu_aio_get(&raw_aio_pool
, bs
, cb
, opaque
);
622 acb
->aio_type
= type
;
623 acb
->aio_fildes
= fd
;
624 acb
->ev_signo
= SIGUSR2
;
627 acb
->aio_iov
= qiov
->iov
;
628 acb
->aio_niov
= qiov
->niov
;
630 acb
->aio_nbytes
= nb_sectors
* 512;
631 acb
->aio_offset
= sector_num
* 512;
633 acb
->next
= posix_aio_state
->first_aio
;
634 posix_aio_state
->first_aio
= acb
;
636 trace_paio_submit(acb
, opaque
, sector_num
, nb_sectors
, type
);
637 qemu_paio_submit(acb
);
641 BlockDriverAIOCB
*paio_ioctl(BlockDriverState
*bs
, int fd
,
642 unsigned long int req
, void *buf
,
643 BlockDriverCompletionFunc
*cb
, void *opaque
)
645 struct qemu_paiocb
*acb
;
647 acb
= qemu_aio_get(&raw_aio_pool
, bs
, cb
, opaque
);
650 acb
->aio_type
= QEMU_AIO_IOCTL
;
651 acb
->aio_fildes
= fd
;
652 acb
->ev_signo
= SIGUSR2
;
654 acb
->aio_ioctl_buf
= buf
;
655 acb
->aio_ioctl_cmd
= req
;
657 acb
->next
= posix_aio_state
->first_aio
;
658 posix_aio_state
->first_aio
= acb
;
660 qemu_paio_submit(acb
);
673 s
= g_malloc(sizeof(PosixAioState
));
675 /* Make sure to block AIO signal */
677 sigaddset(&mask
, SIGUSR2
);
678 sigprocmask(SIG_BLOCK
, &mask
, NULL
);
681 s
->fd
= qemu_signalfd(&mask
);
683 fprintf(stderr
, "failed to create signalfd\n");
687 fcntl(s
->fd
, F_SETFL
, O_NONBLOCK
);
689 qemu_aio_set_fd_handler(s
->fd
, posix_aio_read
, NULL
, posix_aio_flush
,
690 posix_aio_process_queue
, s
);
692 ret
= pthread_attr_init(&attr
);
694 die2(ret
, "pthread_attr_init");
696 ret
= pthread_attr_setdetachstate(&attr
, PTHREAD_CREATE_DETACHED
);
698 die2(ret
, "pthread_attr_setdetachstate");
700 QTAILQ_INIT(&request_list
);
701 new_thread_bh
= qemu_bh_new(spawn_thread_bh_fn
, NULL
);