2 * QEMU posix-aio emulation
4 * Copyright IBM, Corp. 2008
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"
33 static void do_spawn_thread(void);
36 BlockDriverAIOCB common;
39 struct iovec *aio_iov;
44 #define aio_ioctl_cmd aio_nbytes /* for QEMU_AIO_IOCTL */
48 QTAILQ_ENTRY(qemu_paiocb) node;
52 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 int new_threads = 0; /* backlog of threads we need to create */
69 static int pending_threads = 0; /* threads created but not running yet */
70 static QEMUBH *new_thread_bh;
71 static QTAILQ_HEAD(, qemu_paiocb) request_list;
74 static int preadv_present = 1;
76 static int preadv_present = 0;
79 static void die2(int err, const char *what)
81 fprintf(stderr, "%s failed: %s\n", what, strerror(err));
85 static void die(const char *what)
90 static void mutex_lock(pthread_mutex_t *mutex)
92 int ret = pthread_mutex_lock(mutex);
93 if (ret) die2(ret, "pthread_mutex_lock");
96 static void mutex_unlock(pthread_mutex_t *mutex)
98 int ret = pthread_mutex_unlock(mutex);
99 if (ret) die2(ret, "pthread_mutex_unlock");
102 static int cond_timedwait(pthread_cond_t *cond, pthread_mutex_t *mutex,
105 int ret = pthread_cond_timedwait(cond, mutex, ts);
106 if (ret && ret != ETIMEDOUT) die2(ret, "pthread_cond_timedwait");
110 static void cond_signal(pthread_cond_t *cond)
112 int ret = pthread_cond_signal(cond);
113 if (ret) die2(ret, "pthread_cond_signal");
116 static void thread_create(pthread_t *thread, pthread_attr_t *attr,
117 void *(*start_routine)(void*), void *arg)
119 int ret = pthread_create(thread, attr, start_routine, arg);
120 if (ret) die2(ret, "pthread_create");
123 static ssize_t handle_aiocb_ioctl(struct qemu_paiocb *aiocb)
127 ret = ioctl(aiocb->aio_fildes, aiocb->aio_ioctl_cmd, aiocb->aio_ioctl_buf);
132 * This looks weird, but the aio code only consideres a request
133 * successful if it has written the number full number of bytes.
135 * Now we overload aio_nbytes as aio_ioctl_cmd for the ioctl command,
136 * so in fact we return the ioctl command here to make posix_aio_read()
139 return aiocb->aio_nbytes;
142 static ssize_t handle_aiocb_flush(struct qemu_paiocb *aiocb)
146 ret = qemu_fdatasync(aiocb->aio_fildes);
155 qemu_preadv(int fd, const struct iovec *iov, int nr_iov, off_t offset)
157 return preadv(fd, iov, nr_iov, offset);
161 qemu_pwritev(int fd, const struct iovec *iov, int nr_iov, off_t offset)
163 return pwritev(fd, iov, nr_iov, offset);
169 qemu_preadv(int fd, const struct iovec *iov, int nr_iov, off_t offset)
175 qemu_pwritev(int fd, const struct iovec *iov, int nr_iov, off_t offset)
182 static ssize_t handle_aiocb_rw_vector(struct qemu_paiocb *aiocb)
187 if (aiocb->aio_type & QEMU_AIO_WRITE)
188 len = qemu_pwritev(aiocb->aio_fildes,
193 len = qemu_preadv(aiocb->aio_fildes,
197 } while (len == -1 && errno == EINTR);
205 * Read/writes the data to/from a given linear buffer.
207 * Returns the number of bytes handles or -errno in case of an error. Short
208 * reads are only returned if the end of the file is reached.
210 static ssize_t handle_aiocb_rw_linear(struct qemu_paiocb *aiocb, char *buf)
215 while (offset < aiocb->aio_nbytes) {
216 if (aiocb->aio_type & QEMU_AIO_WRITE)
217 len = pwrite(aiocb->aio_fildes,
218 (const char *)buf + offset,
219 aiocb->aio_nbytes - offset,
220 aiocb->aio_offset + offset);
222 len = pread(aiocb->aio_fildes,
224 aiocb->aio_nbytes - offset,
225 aiocb->aio_offset + offset);
227 if (len == -1 && errno == EINTR)
229 else if (len == -1) {
241 static ssize_t handle_aiocb_rw(struct qemu_paiocb *aiocb)
246 if (!(aiocb->aio_type & QEMU_AIO_MISALIGNED)) {
248 * If there is just a single buffer, and it is properly aligned
249 * we can just use plain pread/pwrite without any problems.
251 if (aiocb->aio_niov == 1)
252 return handle_aiocb_rw_linear(aiocb, aiocb->aio_iov->iov_base);
255 * We have more than one iovec, and all are properly aligned.
257 * Try preadv/pwritev first and fall back to linearizing the
258 * buffer if it's not supported.
260 if (preadv_present) {
261 nbytes = handle_aiocb_rw_vector(aiocb);
262 if (nbytes == aiocb->aio_nbytes)
264 if (nbytes < 0 && nbytes != -ENOSYS)
270 * XXX(hch): short read/write. no easy way to handle the reminder
271 * using these interfaces. For now retry using plain
277 * Ok, we have to do it the hard way, copy all segments into
278 * a single aligned buffer.
280 buf = qemu_blockalign(aiocb->common.bs, aiocb->aio_nbytes);
281 if (aiocb->aio_type & QEMU_AIO_WRITE) {
285 for (i = 0; i < aiocb->aio_niov; ++i) {
286 memcpy(p, aiocb->aio_iov[i].iov_base, aiocb->aio_iov[i].iov_len);
287 p += aiocb->aio_iov[i].iov_len;
291 nbytes = handle_aiocb_rw_linear(aiocb, buf);
292 if (!(aiocb->aio_type & QEMU_AIO_WRITE)) {
294 size_t count = aiocb->aio_nbytes, copy;
297 for (i = 0; i < aiocb->aio_niov && count; ++i) {
299 if (copy > aiocb->aio_iov[i].iov_len)
300 copy = aiocb->aio_iov[i].iov_len;
301 memcpy(aiocb->aio_iov[i].iov_base, p, copy);
311 static void *aio_thread(void *unused)
323 struct qemu_paiocb *aiocb;
328 qemu_gettimeofday(&tv);
329 ts.tv_sec = tv.tv_sec + 10;
334 while (QTAILQ_EMPTY(&request_list) &&
335 !(ret == ETIMEDOUT)) {
337 ret = cond_timedwait(&cond, &lock, &ts);
341 if (QTAILQ_EMPTY(&request_list))
344 aiocb = QTAILQ_FIRST(&request_list);
345 QTAILQ_REMOVE(&request_list, aiocb, node);
349 switch (aiocb->aio_type & QEMU_AIO_TYPE_MASK) {
351 ret = handle_aiocb_rw(aiocb);
352 if (ret >= 0 && ret < aiocb->aio_nbytes && aiocb->common.bs->growable) {
353 /* A short read means that we have reached EOF. Pad the buffer
354 * with zeros for bytes after EOF. */
357 qemu_iovec_init_external(&qiov, aiocb->aio_iov,
359 qemu_iovec_memset_skip(&qiov, 0, aiocb->aio_nbytes - ret, ret);
361 ret = aiocb->aio_nbytes;
365 ret = handle_aiocb_rw(aiocb);
368 ret = handle_aiocb_flush(aiocb);
371 ret = handle_aiocb_ioctl(aiocb);
374 fprintf(stderr, "invalid aio request (0x%x)\n", aiocb->aio_type);
383 if (kill(pid, aiocb->ev_signo)) die("kill failed");
392 static void do_spawn_thread(void)
394 sigset_t set, oldset;
407 /* block all signals */
408 if (sigfillset(&set)) die("sigfillset");
409 if (sigprocmask(SIG_SETMASK, &set, &oldset)) die("sigprocmask");
411 thread_create(&thread_id, &attr, aio_thread, NULL);
413 if (sigprocmask(SIG_SETMASK, &oldset, NULL)) die("sigprocmask restore");
416 static void spawn_thread_bh_fn(void *opaque)
421 static void spawn_thread(void)
425 /* If there are threads being created, they will spawn new workers, so
426 * we don't spend time creating many threads in a loop holding a mutex or
427 * starving the current vcpu.
429 * If there are no idle threads, ask the main thread to create one, so we
430 * inherit the correct affinity instead of the vcpu affinity.
432 if (!pending_threads) {
433 qemu_bh_schedule(new_thread_bh);
437 static void qemu_paio_submit(struct qemu_paiocb *aiocb)
439 aiocb->ret = -EINPROGRESS;
442 if (idle_threads == 0 && cur_threads < max_threads)
444 QTAILQ_INSERT_TAIL(&request_list, aiocb, node);
449 static ssize_t qemu_paio_return(struct qemu_paiocb *aiocb)
460 static int qemu_paio_error(struct qemu_paiocb *aiocb)
462 ssize_t ret = qemu_paio_return(aiocb);
472 static int posix_aio_process_queue(void *opaque)
474 PosixAioState *s = opaque;
475 struct qemu_paiocb *acb, **pacb;
480 pacb = &s->first_aio;
486 ret = qemu_paio_error(acb);
487 if (ret == ECANCELED) {
488 /* remove the request */
490 qemu_aio_release(acb);
492 } else if (ret != EINPROGRESS) {
495 ret = qemu_paio_return(acb);
496 if (ret == acb->aio_nbytes)
504 trace_paio_complete(acb, acb->common.opaque, ret);
506 /* remove the request */
508 /* call the callback */
509 acb->common.cb(acb->common.opaque, ret);
510 qemu_aio_release(acb);
522 static void posix_aio_read(void *opaque)
524 PosixAioState *s = opaque;
527 /* read all bytes from signal pipe */
531 len = read(s->rfd, bytes, sizeof(bytes));
532 if (len == -1 && errno == EINTR)
533 continue; /* try again */
534 if (len == sizeof(bytes))
535 continue; /* more to read */
539 posix_aio_process_queue(s);
542 static int posix_aio_flush(void *opaque)
544 PosixAioState *s = opaque;
545 return !!s->first_aio;
548 static PosixAioState *posix_aio_state;
550 static void aio_signal_handler(int signum)
552 if (posix_aio_state) {
556 ret = write(posix_aio_state->wfd, &byte, sizeof(byte));
557 if (ret < 0 && errno != EAGAIN)
564 static void paio_remove(struct qemu_paiocb *acb)
566 struct qemu_paiocb **pacb;
568 /* remove the callback from the queue */
569 pacb = &posix_aio_state->first_aio;
572 fprintf(stderr, "paio_remove: aio request not found!\n");
574 } else if (*pacb == acb) {
576 qemu_aio_release(acb);
579 pacb = &(*pacb)->next;
583 static void paio_cancel(BlockDriverAIOCB *blockacb)
585 struct qemu_paiocb *acb = (struct qemu_paiocb *)blockacb;
588 trace_paio_cancel(acb, acb->common.opaque);
592 QTAILQ_REMOVE(&request_list, acb, node);
593 acb->ret = -ECANCELED;
594 } else if (acb->ret == -EINPROGRESS) {
600 /* fail safe: if the aio could not be canceled, we wait for
602 while (qemu_paio_error(acb) == EINPROGRESS)
609 static AIOPool raw_aio_pool = {
610 .aiocb_size = sizeof(struct qemu_paiocb),
611 .cancel = paio_cancel,
614 BlockDriverAIOCB *paio_submit(BlockDriverState *bs, int fd,
615 int64_t sector_num, QEMUIOVector *qiov, int nb_sectors,
616 BlockDriverCompletionFunc *cb, void *opaque, int type)
618 struct qemu_paiocb *acb;
620 acb = qemu_aio_get(&raw_aio_pool, bs, cb, opaque);
623 acb->aio_type = type;
624 acb->aio_fildes = fd;
625 acb->ev_signo = SIGUSR2;
628 acb->aio_iov = qiov->iov;
629 acb->aio_niov = qiov->niov;
631 acb->aio_nbytes = nb_sectors * 512;
632 acb->aio_offset = sector_num * 512;
634 acb->next = posix_aio_state->first_aio;
635 posix_aio_state->first_aio = acb;
637 trace_paio_submit(acb, opaque, sector_num, nb_sectors, type);
638 qemu_paio_submit(acb);
642 BlockDriverAIOCB *paio_ioctl(BlockDriverState *bs, int fd,
643 unsigned long int req, void *buf,
644 BlockDriverCompletionFunc *cb, void *opaque)
646 struct qemu_paiocb *acb;
648 acb = qemu_aio_get(&raw_aio_pool, bs, cb, opaque);
651 acb->aio_type = QEMU_AIO_IOCTL;
652 acb->aio_fildes = fd;
653 acb->ev_signo = SIGUSR2;
655 acb->aio_ioctl_buf = buf;
656 acb->aio_ioctl_cmd = req;
658 acb->next = posix_aio_state->first_aio;
659 posix_aio_state->first_aio = acb;
661 qemu_paio_submit(acb);
667 struct sigaction act;
675 s = g_malloc(sizeof(PosixAioState));
677 sigfillset(&act.sa_mask);
678 act.sa_flags = 0; /* do not restart syscalls to interrupt select() */
679 act.sa_handler = aio_signal_handler;
680 sigaction(SIGUSR2, &act, NULL);
683 if (qemu_pipe(fds) == -1) {
684 fprintf(stderr, "failed to create pipe\n");
691 fcntl(s->rfd, F_SETFL, O_NONBLOCK);
692 fcntl(s->wfd, F_SETFL, O_NONBLOCK);
694 qemu_aio_set_fd_handler(s->rfd, posix_aio_read, NULL, posix_aio_flush,
695 posix_aio_process_queue, s);
697 ret = pthread_attr_init(&attr);
699 die2(ret, "pthread_attr_init");
701 ret = pthread_attr_setdetachstate(&attr, PTHREAD_CREATE_DETACHED);
703 die2(ret, "pthread_attr_setdetachstate");
705 QTAILQ_INIT(&request_list);
706 new_thread_bh = qemu_bh_new(spawn_thread_bh_fn, NULL);