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.
12 * Contributions after 2012-01-13 are licensed under the terms of the
13 * GNU GPL, version 2 or (at your option) any later version.
16 #include <sys/ioctl.h>
17 #include <sys/types.h>
26 #include "qemu-queue.h"
29 #include "qemu-common.h"
31 #include "block_int.h"
33 #include "block/raw-posix-aio.h"
35 static void do_spawn_thread(void);
38 BlockDriverAIOCB common;
41 struct iovec *aio_iov;
46 #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 considers a request
134 * successful if it has written the 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)
188 if (aiocb->aio_type & QEMU_AIO_WRITE)
189 len = qemu_pwritev(aiocb->aio_fildes,
194 len = qemu_preadv(aiocb->aio_fildes,
198 } while (len == -1 && errno == EINTR);
206 * Read/writes the data to/from a given linear buffer.
208 * Returns the number of bytes handles or -errno in case of an error. Short
209 * reads are only returned if the end of the file is reached.
211 static ssize_t handle_aiocb_rw_linear(struct qemu_paiocb *aiocb, char *buf)
216 while (offset < aiocb->aio_nbytes) {
217 if (aiocb->aio_type & QEMU_AIO_WRITE)
218 len = pwrite(aiocb->aio_fildes,
219 (const char *)buf + offset,
220 aiocb->aio_nbytes - offset,
221 aiocb->aio_offset + offset);
223 len = pread(aiocb->aio_fildes,
225 aiocb->aio_nbytes - offset,
226 aiocb->aio_offset + offset);
228 if (len == -1 && errno == EINTR)
230 else if (len == -1) {
242 static ssize_t handle_aiocb_rw(struct qemu_paiocb *aiocb)
247 if (!(aiocb->aio_type & QEMU_AIO_MISALIGNED)) {
249 * If there is just a single buffer, and it is properly aligned
250 * we can just use plain pread/pwrite without any problems.
252 if (aiocb->aio_niov == 1)
253 return handle_aiocb_rw_linear(aiocb, aiocb->aio_iov->iov_base);
256 * We have more than one iovec, and all are properly aligned.
258 * Try preadv/pwritev first and fall back to linearizing the
259 * buffer if it's not supported.
261 if (preadv_present) {
262 nbytes = handle_aiocb_rw_vector(aiocb);
263 if (nbytes == aiocb->aio_nbytes)
265 if (nbytes < 0 && nbytes != -ENOSYS)
271 * XXX(hch): short read/write. no easy way to handle the reminder
272 * using these interfaces. For now retry using plain
278 * Ok, we have to do it the hard way, copy all segments into
279 * a single aligned buffer.
281 buf = qemu_blockalign(aiocb->common.bs, aiocb->aio_nbytes);
282 if (aiocb->aio_type & QEMU_AIO_WRITE) {
286 for (i = 0; i < aiocb->aio_niov; ++i) {
287 memcpy(p, aiocb->aio_iov[i].iov_base, aiocb->aio_iov[i].iov_len);
288 p += aiocb->aio_iov[i].iov_len;
292 nbytes = handle_aiocb_rw_linear(aiocb, buf);
293 if (!(aiocb->aio_type & QEMU_AIO_WRITE)) {
295 size_t count = aiocb->aio_nbytes, copy;
298 for (i = 0; i < aiocb->aio_niov && count; ++i) {
300 if (copy > aiocb->aio_iov[i].iov_len)
301 copy = aiocb->aio_iov[i].iov_len;
302 memcpy(aiocb->aio_iov[i].iov_base, p, copy);
312 static void posix_aio_notify_event(void);
314 static void *aio_thread(void *unused)
322 struct qemu_paiocb *aiocb;
327 qemu_gettimeofday(&tv);
328 ts.tv_sec = tv.tv_sec + 10;
333 while (QTAILQ_EMPTY(&request_list) &&
334 !(ret == ETIMEDOUT)) {
336 ret = cond_timedwait(&cond, &lock, &ts);
340 if (QTAILQ_EMPTY(&request_list))
343 aiocb = QTAILQ_FIRST(&request_list);
344 QTAILQ_REMOVE(&request_list, aiocb, node);
348 switch (aiocb->aio_type & QEMU_AIO_TYPE_MASK) {
350 ret = handle_aiocb_rw(aiocb);
351 if (ret >= 0 && ret < aiocb->aio_nbytes && aiocb->common.bs->growable) {
352 /* A short read means that we have reached EOF. Pad the buffer
353 * with zeros for bytes after EOF. */
356 qemu_iovec_init_external(&qiov, aiocb->aio_iov,
358 qemu_iovec_memset_skip(&qiov, 0, aiocb->aio_nbytes - ret, ret);
360 ret = aiocb->aio_nbytes;
364 ret = handle_aiocb_rw(aiocb);
367 ret = handle_aiocb_flush(aiocb);
370 ret = handle_aiocb_ioctl(aiocb);
373 fprintf(stderr, "invalid aio request (0x%x)\n", aiocb->aio_type);
382 posix_aio_notify_event();
391 static void do_spawn_thread(void)
393 sigset_t set, oldset;
406 /* block all signals */
407 if (sigfillset(&set)) die("sigfillset");
408 if (sigprocmask(SIG_SETMASK, &set, &oldset)) die("sigprocmask");
410 thread_create(&thread_id, &attr, aio_thread, NULL);
412 if (sigprocmask(SIG_SETMASK, &oldset, NULL)) die("sigprocmask restore");
415 static void spawn_thread_bh_fn(void *opaque)
420 static void spawn_thread(void)
424 /* If there are threads being created, they will spawn new workers, so
425 * we don't spend time creating many threads in a loop holding a mutex or
426 * starving the current vcpu.
428 * If there are no idle threads, ask the main thread to create one, so we
429 * inherit the correct affinity instead of the vcpu affinity.
431 if (!pending_threads) {
432 qemu_bh_schedule(new_thread_bh);
436 static void qemu_paio_submit(struct qemu_paiocb *aiocb)
438 aiocb->ret = -EINPROGRESS;
441 if (idle_threads == 0 && cur_threads < max_threads)
443 QTAILQ_INSERT_TAIL(&request_list, aiocb, node);
448 static ssize_t qemu_paio_return(struct qemu_paiocb *aiocb)
459 static int qemu_paio_error(struct qemu_paiocb *aiocb)
461 ssize_t ret = qemu_paio_return(aiocb);
471 static void posix_aio_read(void *opaque)
473 PosixAioState *s = opaque;
474 struct qemu_paiocb *acb, **pacb;
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 */
491 pacb = &s->first_aio;
497 ret = qemu_paio_error(acb);
498 if (ret == ECANCELED) {
499 /* remove the request */
501 qemu_aio_release(acb);
502 } else if (ret != EINPROGRESS) {
505 ret = qemu_paio_return(acb);
506 if (ret == acb->aio_nbytes)
514 trace_paio_complete(acb, acb->common.opaque, ret);
516 /* remove the request */
518 /* call the callback */
519 acb->common.cb(acb->common.opaque, ret);
520 qemu_aio_release(acb);
529 static int posix_aio_flush(void *opaque)
531 PosixAioState *s = opaque;
532 return !!s->first_aio;
535 static PosixAioState *posix_aio_state;
537 static void posix_aio_notify_event(void)
542 ret = write(posix_aio_state->wfd, &byte, sizeof(byte));
543 if (ret < 0 && errno != EAGAIN)
547 static void paio_remove(struct qemu_paiocb *acb)
549 struct qemu_paiocb **pacb;
551 /* remove the callback from the queue */
552 pacb = &posix_aio_state->first_aio;
555 fprintf(stderr, "paio_remove: aio request not found!\n");
557 } else if (*pacb == acb) {
559 qemu_aio_release(acb);
562 pacb = &(*pacb)->next;
566 static void paio_cancel(BlockDriverAIOCB *blockacb)
568 struct qemu_paiocb *acb = (struct qemu_paiocb *)blockacb;
571 trace_paio_cancel(acb, acb->common.opaque);
575 QTAILQ_REMOVE(&request_list, acb, node);
576 acb->ret = -ECANCELED;
577 } else if (acb->ret == -EINPROGRESS) {
583 /* fail safe: if the aio could not be canceled, we wait for
585 while (qemu_paio_error(acb) == EINPROGRESS)
592 static AIOPool raw_aio_pool = {
593 .aiocb_size = sizeof(struct qemu_paiocb),
594 .cancel = paio_cancel,
597 BlockDriverAIOCB *paio_submit(BlockDriverState *bs, int fd,
598 int64_t sector_num, QEMUIOVector *qiov, int nb_sectors,
599 BlockDriverCompletionFunc *cb, void *opaque, int type)
601 struct qemu_paiocb *acb;
603 acb = qemu_aio_get(&raw_aio_pool, bs, cb, opaque);
604 acb->aio_type = type;
605 acb->aio_fildes = fd;
608 acb->aio_iov = qiov->iov;
609 acb->aio_niov = qiov->niov;
611 acb->aio_nbytes = nb_sectors * 512;
612 acb->aio_offset = sector_num * 512;
614 acb->next = posix_aio_state->first_aio;
615 posix_aio_state->first_aio = acb;
617 trace_paio_submit(acb, opaque, sector_num, nb_sectors, type);
618 qemu_paio_submit(acb);
622 BlockDriverAIOCB *paio_ioctl(BlockDriverState *bs, int fd,
623 unsigned long int req, void *buf,
624 BlockDriverCompletionFunc *cb, void *opaque)
626 struct qemu_paiocb *acb;
628 acb = qemu_aio_get(&raw_aio_pool, bs, cb, opaque);
629 acb->aio_type = QEMU_AIO_IOCTL;
630 acb->aio_fildes = fd;
632 acb->aio_ioctl_buf = buf;
633 acb->aio_ioctl_cmd = req;
635 acb->next = posix_aio_state->first_aio;
636 posix_aio_state->first_aio = acb;
638 qemu_paio_submit(acb);
651 s = g_malloc(sizeof(PosixAioState));
654 if (qemu_pipe(fds) == -1) {
655 fprintf(stderr, "failed to create pipe\n");
663 fcntl(s->rfd, F_SETFL, O_NONBLOCK);
664 fcntl(s->wfd, F_SETFL, O_NONBLOCK);
666 qemu_aio_set_fd_handler(s->rfd, posix_aio_read, NULL, posix_aio_flush, s);
668 ret = pthread_attr_init(&attr);
670 die2(ret, "pthread_attr_init");
672 ret = pthread_attr_setdetachstate(&attr, PTHREAD_CREATE_DETACHED);
674 die2(ret, "pthread_attr_setdetachstate");
676 QTAILQ_INIT(&request_list);
677 new_thread_bh = qemu_bh_new(spawn_thread_bh_fn, NULL);