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"
28 #include "block_int.h"
30 #include "block/raw-posix-aio.h"
34 BlockDriverAIOCB common
;
37 struct iovec
*aio_iov
;
42 #define aio_ioctl_cmd aio_nbytes /* for QEMU_AIO_IOCTL */
46 QTAILQ_ENTRY(qemu_paiocb
) node
;
50 struct qemu_paiocb
*next
;
55 typedef struct PosixAioState
{
57 struct qemu_paiocb
*first_aio
;
61 static pthread_mutex_t lock
= PTHREAD_MUTEX_INITIALIZER
;
62 static pthread_cond_t cond
= PTHREAD_COND_INITIALIZER
;
63 static pthread_t thread_id
;
64 static pthread_attr_t attr
;
65 static int max_threads
= 64;
66 static int cur_threads
= 0;
67 static int idle_threads
= 0;
68 static QTAILQ_HEAD(, qemu_paiocb
) request_list
;
71 static int preadv_present
= 1;
73 static int preadv_present
= 0;
76 static void die2(int err
, const char *what
)
78 fprintf(stderr
, "%s failed: %s\n", what
, strerror(err
));
82 static void die(const char *what
)
87 static void mutex_lock(pthread_mutex_t
*mutex
)
89 int ret
= pthread_mutex_lock(mutex
);
90 if (ret
) die2(ret
, "pthread_mutex_lock");
93 static void mutex_unlock(pthread_mutex_t
*mutex
)
95 int ret
= pthread_mutex_unlock(mutex
);
96 if (ret
) die2(ret
, "pthread_mutex_unlock");
99 static int cond_timedwait(pthread_cond_t
*cond
, pthread_mutex_t
*mutex
,
102 int ret
= pthread_cond_timedwait(cond
, mutex
, ts
);
103 if (ret
&& ret
!= ETIMEDOUT
) die2(ret
, "pthread_cond_timedwait");
107 static void cond_signal(pthread_cond_t
*cond
)
109 int ret
= pthread_cond_signal(cond
);
110 if (ret
) die2(ret
, "pthread_cond_signal");
113 static void thread_create(pthread_t
*thread
, pthread_attr_t
*attr
,
114 void *(*start_routine
)(void*), void *arg
)
116 int ret
= pthread_create(thread
, attr
, start_routine
, arg
);
117 if (ret
) die2(ret
, "pthread_create");
120 static size_t handle_aiocb_ioctl(struct qemu_paiocb
*aiocb
)
124 ret
= ioctl(aiocb
->aio_fildes
, aiocb
->aio_ioctl_cmd
, aiocb
->aio_ioctl_buf
);
129 * This looks weird, but the aio code only consideres a request
130 * successfull if it has written the number full number of bytes.
132 * Now we overload aio_nbytes as aio_ioctl_cmd for the ioctl command,
133 * so in fact we return the ioctl command here to make posix_aio_read()
136 return aiocb
->aio_nbytes
;
139 static size_t handle_aiocb_flush(struct qemu_paiocb
*aiocb
)
143 ret
= qemu_fdatasync(aiocb
->aio_fildes
);
152 qemu_preadv(int fd
, const struct iovec
*iov
, int nr_iov
, off_t offset
)
154 return preadv(fd
, iov
, nr_iov
, offset
);
158 qemu_pwritev(int fd
, const struct iovec
*iov
, int nr_iov
, off_t offset
)
160 return pwritev(fd
, iov
, nr_iov
, offset
);
166 qemu_preadv(int fd
, const struct iovec
*iov
, int nr_iov
, off_t offset
)
172 qemu_pwritev(int fd
, const struct iovec
*iov
, int nr_iov
, off_t offset
)
179 static size_t handle_aiocb_rw_vector(struct qemu_paiocb
*aiocb
)
185 if (aiocb
->aio_type
& QEMU_AIO_WRITE
)
186 len
= qemu_pwritev(aiocb
->aio_fildes
,
189 aiocb
->aio_offset
+ offset
);
191 len
= qemu_preadv(aiocb
->aio_fildes
,
194 aiocb
->aio_offset
+ offset
);
195 } while (len
== -1 && errno
== EINTR
);
202 static size_t handle_aiocb_rw_linear(struct qemu_paiocb
*aiocb
, char *buf
)
207 while (offset
< aiocb
->aio_nbytes
) {
208 if (aiocb
->aio_type
& QEMU_AIO_WRITE
)
209 len
= pwrite(aiocb
->aio_fildes
,
210 (const char *)buf
+ offset
,
211 aiocb
->aio_nbytes
- offset
,
212 aiocb
->aio_offset
+ offset
);
214 len
= pread(aiocb
->aio_fildes
,
216 aiocb
->aio_nbytes
- offset
,
217 aiocb
->aio_offset
+ offset
);
219 if (len
== -1 && errno
== EINTR
)
221 else if (len
== -1) {
233 static size_t handle_aiocb_rw(struct qemu_paiocb
*aiocb
)
238 if (!(aiocb
->aio_type
& QEMU_AIO_MISALIGNED
)) {
240 * If there is just a single buffer, and it is properly aligned
241 * we can just use plain pread/pwrite without any problems.
243 if (aiocb
->aio_niov
== 1)
244 return handle_aiocb_rw_linear(aiocb
, aiocb
->aio_iov
->iov_base
);
247 * We have more than one iovec, and all are properly aligned.
249 * Try preadv/pwritev first and fall back to linearizing the
250 * buffer if it's not supported.
252 if (preadv_present
) {
253 nbytes
= handle_aiocb_rw_vector(aiocb
);
254 if (nbytes
== aiocb
->aio_nbytes
)
256 if (nbytes
< 0 && nbytes
!= -ENOSYS
)
262 * XXX(hch): short read/write. no easy way to handle the reminder
263 * using these interfaces. For now retry using plain
269 * Ok, we have to do it the hard way, copy all segments into
270 * a single aligned buffer.
272 buf
= qemu_memalign(512, aiocb
->aio_nbytes
);
273 if (aiocb
->aio_type
& QEMU_AIO_WRITE
) {
277 for (i
= 0; i
< aiocb
->aio_niov
; ++i
) {
278 memcpy(p
, aiocb
->aio_iov
[i
].iov_base
, aiocb
->aio_iov
[i
].iov_len
);
279 p
+= aiocb
->aio_iov
[i
].iov_len
;
283 nbytes
= handle_aiocb_rw_linear(aiocb
, buf
);
284 if (!(aiocb
->aio_type
& QEMU_AIO_WRITE
)) {
286 size_t count
= aiocb
->aio_nbytes
, copy
;
289 for (i
= 0; i
< aiocb
->aio_niov
&& count
; ++i
) {
291 if (copy
> aiocb
->aio_iov
[i
].iov_len
)
292 copy
= aiocb
->aio_iov
[i
].iov_len
;
293 memcpy(aiocb
->aio_iov
[i
].iov_base
, p
, copy
);
303 static void *aio_thread(void *unused
)
310 struct qemu_paiocb
*aiocb
;
315 qemu_gettimeofday(&tv
);
316 ts
.tv_sec
= tv
.tv_sec
+ 10;
321 while (QTAILQ_EMPTY(&request_list
) &&
322 !(ret
== ETIMEDOUT
)) {
323 ret
= cond_timedwait(&cond
, &lock
, &ts
);
326 if (QTAILQ_EMPTY(&request_list
))
329 aiocb
= QTAILQ_FIRST(&request_list
);
330 QTAILQ_REMOVE(&request_list
, aiocb
, node
);
335 switch (aiocb
->aio_type
& QEMU_AIO_TYPE_MASK
) {
338 ret
= handle_aiocb_rw(aiocb
);
341 ret
= handle_aiocb_flush(aiocb
);
344 ret
= handle_aiocb_ioctl(aiocb
);
347 fprintf(stderr
, "invalid aio request (0x%x)\n", aiocb
->aio_type
);
357 if (kill(pid
, aiocb
->ev_signo
)) die("kill failed");
367 static void spawn_thread(void)
369 sigset_t set
, oldset
;
374 /* block all signals */
375 if (sigfillset(&set
)) die("sigfillset");
376 if (sigprocmask(SIG_SETMASK
, &set
, &oldset
)) die("sigprocmask");
378 thread_create(&thread_id
, &attr
, aio_thread
, NULL
);
380 if (sigprocmask(SIG_SETMASK
, &oldset
, NULL
)) die("sigprocmask restore");
383 static void qemu_paio_submit(struct qemu_paiocb
*aiocb
)
385 aiocb
->ret
= -EINPROGRESS
;
388 if (idle_threads
== 0 && cur_threads
< max_threads
)
390 QTAILQ_INSERT_TAIL(&request_list
, aiocb
, node
);
395 static ssize_t
qemu_paio_return(struct qemu_paiocb
*aiocb
)
406 static int qemu_paio_error(struct qemu_paiocb
*aiocb
)
408 ssize_t ret
= qemu_paio_return(aiocb
);
418 static int posix_aio_process_queue(void *opaque
)
420 PosixAioState
*s
= opaque
;
421 struct qemu_paiocb
*acb
, **pacb
;
424 int async_context_id
= get_async_context_id();
427 pacb
= &s
->first_aio
;
433 /* we're only interested in requests in the right context */
434 if (acb
->async_context_id
!= async_context_id
) {
439 ret
= qemu_paio_error(acb
);
440 if (ret
== ECANCELED
) {
441 /* remove the request */
443 qemu_aio_release(acb
);
445 } else if (ret
!= EINPROGRESS
) {
448 ret
= qemu_paio_return(acb
);
449 if (ret
== acb
->aio_nbytes
)
456 /* remove the request */
458 /* call the callback */
459 acb
->common
.cb(acb
->common
.opaque
, ret
);
460 qemu_aio_release(acb
);
472 static void posix_aio_read(void *opaque
)
474 PosixAioState
*s
= opaque
;
477 /* read all bytes from signal pipe */
481 len
= read(s
->rfd
, bytes
, sizeof(bytes
));
482 if (len
== -1 && errno
== EINTR
)
483 continue; /* try again */
484 if (len
== sizeof(bytes
))
485 continue; /* more to read */
489 posix_aio_process_queue(s
);
492 static int posix_aio_flush(void *opaque
)
494 PosixAioState
*s
= opaque
;
495 return !!s
->first_aio
;
498 static PosixAioState
*posix_aio_state
;
500 static void aio_signal_handler(int signum
)
502 if (posix_aio_state
) {
505 write(posix_aio_state
->wfd
, &byte
, sizeof(byte
));
511 static void paio_remove(struct qemu_paiocb
*acb
)
513 struct qemu_paiocb
**pacb
;
515 /* remove the callback from the queue */
516 pacb
= &posix_aio_state
->first_aio
;
519 fprintf(stderr
, "paio_remove: aio request not found!\n");
521 } else if (*pacb
== acb
) {
523 qemu_aio_release(acb
);
526 pacb
= &(*pacb
)->next
;
530 static void paio_cancel(BlockDriverAIOCB
*blockacb
)
532 struct qemu_paiocb
*acb
= (struct qemu_paiocb
*)blockacb
;
537 QTAILQ_REMOVE(&request_list
, acb
, node
);
538 acb
->ret
= -ECANCELED
;
539 } else if (acb
->ret
== -EINPROGRESS
) {
545 /* fail safe: if the aio could not be canceled, we wait for
547 while (qemu_paio_error(acb
) == EINPROGRESS
)
554 static AIOPool raw_aio_pool
= {
555 .aiocb_size
= sizeof(struct qemu_paiocb
),
556 .cancel
= paio_cancel
,
559 BlockDriverAIOCB
*paio_submit(BlockDriverState
*bs
, int fd
,
560 int64_t sector_num
, QEMUIOVector
*qiov
, int nb_sectors
,
561 BlockDriverCompletionFunc
*cb
, void *opaque
, int type
)
563 struct qemu_paiocb
*acb
;
565 acb
= qemu_aio_get(&raw_aio_pool
, bs
, cb
, opaque
);
568 acb
->aio_type
= type
;
569 acb
->aio_fildes
= fd
;
570 acb
->ev_signo
= SIGUSR2
;
571 acb
->async_context_id
= get_async_context_id();
574 acb
->aio_iov
= qiov
->iov
;
575 acb
->aio_niov
= qiov
->niov
;
577 acb
->aio_nbytes
= nb_sectors
* 512;
578 acb
->aio_offset
= sector_num
* 512;
580 acb
->next
= posix_aio_state
->first_aio
;
581 posix_aio_state
->first_aio
= acb
;
583 qemu_paio_submit(acb
);
587 BlockDriverAIOCB
*paio_ioctl(BlockDriverState
*bs
, int fd
,
588 unsigned long int req
, void *buf
,
589 BlockDriverCompletionFunc
*cb
, void *opaque
)
591 struct qemu_paiocb
*acb
;
593 acb
= qemu_aio_get(&raw_aio_pool
, bs
, cb
, opaque
);
596 acb
->aio_type
= QEMU_AIO_IOCTL
;
597 acb
->aio_fildes
= fd
;
598 acb
->ev_signo
= SIGUSR2
;
600 acb
->aio_ioctl_buf
= buf
;
601 acb
->aio_ioctl_cmd
= req
;
603 acb
->next
= posix_aio_state
->first_aio
;
604 posix_aio_state
->first_aio
= acb
;
606 qemu_paio_submit(acb
);
612 struct sigaction act
;
620 s
= qemu_malloc(sizeof(PosixAioState
));
622 sigfillset(&act
.sa_mask
);
623 act
.sa_flags
= 0; /* do not restart syscalls to interrupt select() */
624 act
.sa_handler
= aio_signal_handler
;
625 sigaction(SIGUSR2
, &act
, NULL
);
628 if (pipe(fds
) == -1) {
629 fprintf(stderr
, "failed to create pipe\n");
636 fcntl(s
->rfd
, F_SETFL
, O_NONBLOCK
);
637 fcntl(s
->wfd
, F_SETFL
, O_NONBLOCK
);
639 qemu_aio_set_fd_handler(s
->rfd
, posix_aio_read
, NULL
, posix_aio_flush
,
640 posix_aio_process_queue
, s
);
642 ret
= pthread_attr_init(&attr
);
644 die2(ret
, "pthread_attr_init");
646 ret
= pthread_attr_setdetachstate(&attr
, PTHREAD_CREATE_DETACHED
);
648 die2(ret
, "pthread_attr_setdetachstate");
650 QTAILQ_INIT(&request_list
);