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"
34 #include "block/raw-posix-aio.h"
36 static void do_spawn_thread(void);
39 BlockDriverAIOCB common;
42 struct iovec *aio_iov;
47 #define aio_ioctl_cmd aio_nbytes /* for QEMU_AIO_IOCTL */
50 QTAILQ_ENTRY(qemu_paiocb) node;
54 struct qemu_paiocb *next;
57 typedef struct PosixAioState {
59 struct qemu_paiocb *first_aio;
63 static pthread_mutex_t lock = PTHREAD_MUTEX_INITIALIZER;
64 static pthread_cond_t cond = PTHREAD_COND_INITIALIZER;
65 static pthread_t thread_id;
66 static pthread_attr_t attr;
67 static int max_threads = 64;
68 static int cur_threads = 0;
69 static int idle_threads = 0;
70 static int new_threads = 0; /* backlog of threads we need to create */
71 static int pending_threads = 0; /* threads created but not running yet */
72 static QEMUBH *new_thread_bh;
73 static QTAILQ_HEAD(, qemu_paiocb) request_list;
76 static int preadv_present = 1;
78 static int preadv_present = 0;
81 static void die2(int err, const char *what)
83 fprintf(stderr, "%s failed: %s\n", what, strerror(err));
87 static void die(const char *what)
92 static void mutex_lock(pthread_mutex_t *mutex)
94 int ret = pthread_mutex_lock(mutex);
95 if (ret) die2(ret, "pthread_mutex_lock");
98 static void mutex_unlock(pthread_mutex_t *mutex)
100 int ret = pthread_mutex_unlock(mutex);
101 if (ret) die2(ret, "pthread_mutex_unlock");
104 static int cond_timedwait(pthread_cond_t *cond, pthread_mutex_t *mutex,
107 int ret = pthread_cond_timedwait(cond, mutex, ts);
108 if (ret && ret != ETIMEDOUT) die2(ret, "pthread_cond_timedwait");
112 static void cond_signal(pthread_cond_t *cond)
114 int ret = pthread_cond_signal(cond);
115 if (ret) die2(ret, "pthread_cond_signal");
118 static void thread_create(pthread_t *thread, pthread_attr_t *attr,
119 void *(*start_routine)(void*), void *arg)
121 int ret = pthread_create(thread, attr, start_routine, arg);
122 if (ret) die2(ret, "pthread_create");
125 static ssize_t handle_aiocb_ioctl(struct qemu_paiocb *aiocb)
129 ret = ioctl(aiocb->aio_fildes, aiocb->aio_ioctl_cmd, aiocb->aio_ioctl_buf);
134 * This looks weird, but the aio code only considers a request
135 * successful if it has written the full number of bytes.
137 * Now we overload aio_nbytes as aio_ioctl_cmd for the ioctl command,
138 * so in fact we return the ioctl command here to make posix_aio_read()
141 return aiocb->aio_nbytes;
144 static ssize_t handle_aiocb_flush(struct qemu_paiocb *aiocb)
148 ret = qemu_fdatasync(aiocb->aio_fildes);
157 qemu_preadv(int fd, const struct iovec *iov, int nr_iov, off_t offset)
159 return preadv(fd, iov, nr_iov, offset);
163 qemu_pwritev(int fd, const struct iovec *iov, int nr_iov, off_t offset)
165 return pwritev(fd, iov, nr_iov, offset);
171 qemu_preadv(int fd, const struct iovec *iov, int nr_iov, off_t offset)
177 qemu_pwritev(int fd, const struct iovec *iov, int nr_iov, off_t offset)
184 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,
195 len = qemu_preadv(aiocb->aio_fildes,
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 posix_aio_notify_event(void);
315 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. */
355 iov_memset(aiocb->aio_iov, aiocb->aio_niov, ret,
356 0, aiocb->aio_nbytes - ret);
358 ret = aiocb->aio_nbytes;
362 ret = handle_aiocb_rw(aiocb);
365 ret = handle_aiocb_flush(aiocb);
368 ret = handle_aiocb_ioctl(aiocb);
371 fprintf(stderr, "invalid aio request (0x%x)\n", aiocb->aio_type);
380 posix_aio_notify_event();
389 static void do_spawn_thread(void)
391 sigset_t set, oldset;
404 /* block all signals */
405 if (sigfillset(&set)) die("sigfillset");
406 if (sigprocmask(SIG_SETMASK, &set, &oldset)) die("sigprocmask");
408 thread_create(&thread_id, &attr, aio_thread, NULL);
410 if (sigprocmask(SIG_SETMASK, &oldset, NULL)) die("sigprocmask restore");
413 static void spawn_thread_bh_fn(void *opaque)
418 static void spawn_thread(void)
422 /* If there are threads being created, they will spawn new workers, so
423 * we don't spend time creating many threads in a loop holding a mutex or
424 * starving the current vcpu.
426 * If there are no idle threads, ask the main thread to create one, so we
427 * inherit the correct affinity instead of the vcpu affinity.
429 if (!pending_threads) {
430 qemu_bh_schedule(new_thread_bh);
434 static void qemu_paio_submit(struct qemu_paiocb *aiocb)
436 aiocb->ret = -EINPROGRESS;
439 if (idle_threads == 0 && cur_threads < max_threads)
441 QTAILQ_INSERT_TAIL(&request_list, aiocb, node);
446 static ssize_t qemu_paio_return(struct qemu_paiocb *aiocb)
457 static int qemu_paio_error(struct qemu_paiocb *aiocb)
459 ssize_t ret = qemu_paio_return(aiocb);
469 static void posix_aio_read(void *opaque)
471 PosixAioState *s = opaque;
472 struct qemu_paiocb *acb, **pacb;
476 /* read all bytes from signal pipe */
480 len = read(s->rfd, bytes, sizeof(bytes));
481 if (len == -1 && errno == EINTR)
482 continue; /* try again */
483 if (len == sizeof(bytes))
484 continue; /* more to read */
489 pacb = &s->first_aio;
495 ret = qemu_paio_error(acb);
496 if (ret == ECANCELED) {
497 /* remove the request */
499 qemu_aio_release(acb);
500 } else if (ret != EINPROGRESS) {
503 ret = qemu_paio_return(acb);
504 if (ret == acb->aio_nbytes)
512 trace_paio_complete(acb, acb->common.opaque, ret);
514 /* remove the request */
516 /* call the callback */
517 acb->common.cb(acb->common.opaque, ret);
518 qemu_aio_release(acb);
527 static int posix_aio_flush(void *opaque)
529 PosixAioState *s = opaque;
530 return !!s->first_aio;
533 static PosixAioState *posix_aio_state;
535 static void posix_aio_notify_event(void)
540 ret = write(posix_aio_state->wfd, &byte, sizeof(byte));
541 if (ret < 0 && errno != EAGAIN)
545 static void paio_remove(struct qemu_paiocb *acb)
547 struct qemu_paiocb **pacb;
549 /* remove the callback from the queue */
550 pacb = &posix_aio_state->first_aio;
553 fprintf(stderr, "paio_remove: aio request not found!\n");
555 } else if (*pacb == acb) {
557 qemu_aio_release(acb);
560 pacb = &(*pacb)->next;
564 static void paio_cancel(BlockDriverAIOCB *blockacb)
566 struct qemu_paiocb *acb = (struct qemu_paiocb *)blockacb;
569 trace_paio_cancel(acb, acb->common.opaque);
573 QTAILQ_REMOVE(&request_list, acb, node);
574 acb->ret = -ECANCELED;
575 } else if (acb->ret == -EINPROGRESS) {
581 /* fail safe: if the aio could not be canceled, we wait for
583 while (qemu_paio_error(acb) == EINPROGRESS)
590 static AIOPool raw_aio_pool = {
591 .aiocb_size = sizeof(struct qemu_paiocb),
592 .cancel = paio_cancel,
595 BlockDriverAIOCB *paio_submit(BlockDriverState *bs, int fd,
596 int64_t sector_num, QEMUIOVector *qiov, int nb_sectors,
597 BlockDriverCompletionFunc *cb, void *opaque, int type)
599 struct qemu_paiocb *acb;
601 acb = qemu_aio_get(&raw_aio_pool, bs, cb, opaque);
602 acb->aio_type = type;
603 acb->aio_fildes = fd;
606 acb->aio_iov = qiov->iov;
607 acb->aio_niov = qiov->niov;
609 acb->aio_nbytes = nb_sectors * 512;
610 acb->aio_offset = sector_num * 512;
612 acb->next = posix_aio_state->first_aio;
613 posix_aio_state->first_aio = acb;
615 trace_paio_submit(acb, opaque, sector_num, nb_sectors, type);
616 qemu_paio_submit(acb);
620 BlockDriverAIOCB *paio_ioctl(BlockDriverState *bs, int fd,
621 unsigned long int req, void *buf,
622 BlockDriverCompletionFunc *cb, void *opaque)
624 struct qemu_paiocb *acb;
626 acb = qemu_aio_get(&raw_aio_pool, bs, cb, opaque);
627 acb->aio_type = QEMU_AIO_IOCTL;
628 acb->aio_fildes = fd;
630 acb->aio_ioctl_buf = buf;
631 acb->aio_ioctl_cmd = req;
633 acb->next = posix_aio_state->first_aio;
634 posix_aio_state->first_aio = acb;
636 qemu_paio_submit(acb);
649 s = g_malloc(sizeof(PosixAioState));
652 if (qemu_pipe(fds) == -1) {
653 fprintf(stderr, "failed to create pipe\n");
661 fcntl(s->rfd, F_SETFL, O_NONBLOCK);
662 fcntl(s->wfd, F_SETFL, O_NONBLOCK);
664 qemu_aio_set_fd_handler(s->rfd, posix_aio_read, NULL, posix_aio_flush, s);
666 ret = pthread_attr_init(&attr);
668 die2(ret, "pthread_attr_init");
670 ret = pthread_attr_setdetachstate(&attr, PTHREAD_CREATE_DETACHED);
672 die2(ret, "pthread_attr_setdetachstate");
674 QTAILQ_INIT(&request_list);
675 new_thread_bh = qemu_bh_new(spawn_thread_bh_fn, NULL);