2008-12-12 17:41:40 +01:00
|
|
|
/*
|
|
|
|
* QEMU posix-aio emulation
|
|
|
|
*
|
|
|
|
* Copyright IBM, Corp. 2008
|
|
|
|
*
|
|
|
|
* Authors:
|
|
|
|
* Anthony Liguori <aliguori@us.ibm.com>
|
|
|
|
*
|
|
|
|
* This work is licensed under the terms of the GNU GPL, version 2. See
|
|
|
|
* the COPYING file in the top-level directory.
|
|
|
|
*
|
|
|
|
*/
|
|
|
|
|
2009-03-28 18:28:41 +01:00
|
|
|
#include <sys/ioctl.h>
|
2008-12-12 17:41:40 +01:00
|
|
|
#include <pthread.h>
|
|
|
|
#include <unistd.h>
|
|
|
|
#include <errno.h>
|
2009-02-21 06:48:13 +01:00
|
|
|
#include <time.h>
|
2009-02-21 06:48:11 +01:00
|
|
|
#include <string.h>
|
|
|
|
#include <stdlib.h>
|
|
|
|
#include <stdio.h>
|
2008-12-12 17:41:40 +01:00
|
|
|
#include "osdep.h"
|
2009-04-07 20:43:24 +02:00
|
|
|
#include "qemu-common.h"
|
2008-12-12 17:41:40 +01:00
|
|
|
|
|
|
|
#include "posix-aio-compat.h"
|
|
|
|
|
|
|
|
static pthread_mutex_t lock = PTHREAD_MUTEX_INITIALIZER;
|
|
|
|
static pthread_cond_t cond = PTHREAD_COND_INITIALIZER;
|
|
|
|
static pthread_t thread_id;
|
2009-02-21 06:48:17 +01:00
|
|
|
static pthread_attr_t attr;
|
2008-12-12 17:41:40 +01:00
|
|
|
static int max_threads = 64;
|
|
|
|
static int cur_threads = 0;
|
|
|
|
static int idle_threads = 0;
|
|
|
|
static TAILQ_HEAD(, qemu_paiocb) request_list;
|
|
|
|
|
2009-07-27 16:12:58 +02:00
|
|
|
#ifdef CONFIG_PREADV
|
2009-04-07 20:43:28 +02:00
|
|
|
static int preadv_present = 1;
|
|
|
|
#else
|
|
|
|
static int preadv_present = 0;
|
|
|
|
#endif
|
|
|
|
|
2009-02-21 06:48:11 +01:00
|
|
|
static void die2(int err, const char *what)
|
|
|
|
{
|
|
|
|
fprintf(stderr, "%s failed: %s\n", what, strerror(err));
|
|
|
|
abort();
|
|
|
|
}
|
|
|
|
|
|
|
|
static void die(const char *what)
|
|
|
|
{
|
|
|
|
die2(errno, what);
|
|
|
|
}
|
|
|
|
|
|
|
|
static void mutex_lock(pthread_mutex_t *mutex)
|
|
|
|
{
|
|
|
|
int ret = pthread_mutex_lock(mutex);
|
|
|
|
if (ret) die2(ret, "pthread_mutex_lock");
|
|
|
|
}
|
|
|
|
|
|
|
|
static void mutex_unlock(pthread_mutex_t *mutex)
|
|
|
|
{
|
|
|
|
int ret = pthread_mutex_unlock(mutex);
|
|
|
|
if (ret) die2(ret, "pthread_mutex_unlock");
|
|
|
|
}
|
|
|
|
|
|
|
|
static int cond_timedwait(pthread_cond_t *cond, pthread_mutex_t *mutex,
|
|
|
|
struct timespec *ts)
|
|
|
|
{
|
|
|
|
int ret = pthread_cond_timedwait(cond, mutex, ts);
|
|
|
|
if (ret && ret != ETIMEDOUT) die2(ret, "pthread_cond_timedwait");
|
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|
2009-02-21 06:48:15 +01:00
|
|
|
static void cond_signal(pthread_cond_t *cond)
|
2009-02-21 06:48:11 +01:00
|
|
|
{
|
2009-02-21 06:48:15 +01:00
|
|
|
int ret = pthread_cond_signal(cond);
|
|
|
|
if (ret) die2(ret, "pthread_cond_signal");
|
2009-02-21 06:48:11 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
static void thread_create(pthread_t *thread, pthread_attr_t *attr,
|
|
|
|
void *(*start_routine)(void*), void *arg)
|
|
|
|
{
|
|
|
|
int ret = pthread_create(thread, attr, start_routine, arg);
|
|
|
|
if (ret) die2(ret, "pthread_create");
|
|
|
|
}
|
|
|
|
|
2009-04-07 20:43:24 +02:00
|
|
|
static size_t handle_aiocb_ioctl(struct qemu_paiocb *aiocb)
|
|
|
|
{
|
|
|
|
int ret;
|
|
|
|
|
|
|
|
ret = ioctl(aiocb->aio_fildes, aiocb->aio_ioctl_cmd, aiocb->aio_ioctl_buf);
|
|
|
|
if (ret == -1)
|
|
|
|
return -errno;
|
2009-04-28 11:57:02 +02:00
|
|
|
|
|
|
|
/*
|
|
|
|
* This looks weird, but the aio code only consideres a request
|
|
|
|
* successfull if it has written the number full number of bytes.
|
|
|
|
*
|
|
|
|
* Now we overload aio_nbytes as aio_ioctl_cmd for the ioctl command,
|
|
|
|
* so in fact we return the ioctl command here to make posix_aio_read()
|
|
|
|
* happy..
|
|
|
|
*/
|
|
|
|
return aiocb->aio_nbytes;
|
2009-04-07 20:43:24 +02:00
|
|
|
}
|
|
|
|
|
2009-07-27 16:12:58 +02:00
|
|
|
#ifdef CONFIG_PREADV
|
2009-04-07 20:43:28 +02:00
|
|
|
|
|
|
|
static ssize_t
|
|
|
|
qemu_preadv(int fd, const struct iovec *iov, int nr_iov, off_t offset)
|
|
|
|
{
|
|
|
|
return preadv(fd, iov, nr_iov, offset);
|
|
|
|
}
|
|
|
|
|
|
|
|
static ssize_t
|
|
|
|
qemu_pwritev(int fd, const struct iovec *iov, int nr_iov, off_t offset)
|
|
|
|
{
|
|
|
|
return pwritev(fd, iov, nr_iov, offset);
|
|
|
|
}
|
|
|
|
|
|
|
|
#else
|
|
|
|
|
|
|
|
static ssize_t
|
|
|
|
qemu_preadv(int fd, const struct iovec *iov, int nr_iov, off_t offset)
|
|
|
|
{
|
|
|
|
return -ENOSYS;
|
|
|
|
}
|
|
|
|
|
|
|
|
static ssize_t
|
|
|
|
qemu_pwritev(int fd, const struct iovec *iov, int nr_iov, off_t offset)
|
|
|
|
{
|
|
|
|
return -ENOSYS;
|
|
|
|
}
|
|
|
|
|
|
|
|
#endif
|
|
|
|
|
2009-04-07 20:43:24 +02:00
|
|
|
/*
|
|
|
|
* Check if we need to copy the data in the aiocb into a new
|
|
|
|
* properly aligned buffer.
|
|
|
|
*/
|
|
|
|
static int aiocb_needs_copy(struct qemu_paiocb *aiocb)
|
|
|
|
{
|
|
|
|
if (aiocb->aio_flags & QEMU_AIO_SECTOR_ALIGNED) {
|
|
|
|
int i;
|
|
|
|
|
|
|
|
for (i = 0; i < aiocb->aio_niov; i++)
|
|
|
|
if ((uintptr_t) aiocb->aio_iov[i].iov_base % 512)
|
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2009-04-07 20:43:28 +02:00
|
|
|
static size_t handle_aiocb_rw_vector(struct qemu_paiocb *aiocb)
|
|
|
|
{
|
|
|
|
size_t offset = 0;
|
|
|
|
ssize_t len;
|
|
|
|
|
|
|
|
do {
|
|
|
|
if (aiocb->aio_type == QEMU_PAIO_WRITE)
|
|
|
|
len = qemu_pwritev(aiocb->aio_fildes,
|
|
|
|
aiocb->aio_iov,
|
|
|
|
aiocb->aio_niov,
|
|
|
|
aiocb->aio_offset + offset);
|
|
|
|
else
|
|
|
|
len = qemu_preadv(aiocb->aio_fildes,
|
|
|
|
aiocb->aio_iov,
|
|
|
|
aiocb->aio_niov,
|
|
|
|
aiocb->aio_offset + offset);
|
|
|
|
} while (len == -1 && errno == EINTR);
|
|
|
|
|
|
|
|
if (len == -1)
|
|
|
|
return -errno;
|
|
|
|
return len;
|
|
|
|
}
|
|
|
|
|
2009-04-07 20:43:24 +02:00
|
|
|
static size_t handle_aiocb_rw_linear(struct qemu_paiocb *aiocb, char *buf)
|
2009-03-28 18:28:41 +01:00
|
|
|
{
|
|
|
|
size_t offset = 0;
|
2009-04-07 20:43:24 +02:00
|
|
|
size_t len;
|
2009-03-28 18:28:41 +01:00
|
|
|
|
|
|
|
while (offset < aiocb->aio_nbytes) {
|
2009-04-07 20:43:24 +02:00
|
|
|
if (aiocb->aio_type == QEMU_PAIO_WRITE)
|
|
|
|
len = pwrite(aiocb->aio_fildes,
|
|
|
|
(const char *)buf + offset,
|
|
|
|
aiocb->aio_nbytes - offset,
|
|
|
|
aiocb->aio_offset + offset);
|
|
|
|
else
|
|
|
|
len = pread(aiocb->aio_fildes,
|
|
|
|
buf + offset,
|
2009-03-28 18:28:41 +01:00
|
|
|
aiocb->aio_nbytes - offset,
|
|
|
|
aiocb->aio_offset + offset);
|
|
|
|
|
2009-04-07 20:43:24 +02:00
|
|
|
if (len == -1 && errno == EINTR)
|
|
|
|
continue;
|
|
|
|
else if (len == -1) {
|
|
|
|
offset = -errno;
|
|
|
|
break;
|
|
|
|
} else if (len == 0)
|
|
|
|
break;
|
|
|
|
|
|
|
|
offset += len;
|
2009-03-28 18:28:41 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
return offset;
|
|
|
|
}
|
|
|
|
|
2009-04-07 20:43:24 +02:00
|
|
|
static size_t handle_aiocb_rw(struct qemu_paiocb *aiocb)
|
2009-03-28 18:28:41 +01:00
|
|
|
{
|
2009-04-07 20:43:24 +02:00
|
|
|
size_t nbytes;
|
|
|
|
char *buf;
|
|
|
|
|
2009-04-07 20:43:28 +02:00
|
|
|
if (!aiocb_needs_copy(aiocb)) {
|
2009-04-07 20:43:24 +02:00
|
|
|
/*
|
|
|
|
* If there is just a single buffer, and it is properly aligned
|
|
|
|
* we can just use plain pread/pwrite without any problems.
|
|
|
|
*/
|
2009-04-07 20:43:28 +02:00
|
|
|
if (aiocb->aio_niov == 1)
|
|
|
|
return handle_aiocb_rw_linear(aiocb, aiocb->aio_iov->iov_base);
|
|
|
|
|
|
|
|
/*
|
|
|
|
* We have more than one iovec, and all are properly aligned.
|
|
|
|
*
|
|
|
|
* Try preadv/pwritev first and fall back to linearizing the
|
|
|
|
* buffer if it's not supported.
|
|
|
|
*/
|
|
|
|
if (preadv_present) {
|
|
|
|
nbytes = handle_aiocb_rw_vector(aiocb);
|
|
|
|
if (nbytes == aiocb->aio_nbytes)
|
|
|
|
return nbytes;
|
|
|
|
if (nbytes < 0 && nbytes != -ENOSYS)
|
|
|
|
return nbytes;
|
|
|
|
preadv_present = 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
* XXX(hch): short read/write. no easy way to handle the reminder
|
|
|
|
* using these interfaces. For now retry using plain
|
|
|
|
* pread/pwrite?
|
|
|
|
*/
|
2009-04-07 20:43:24 +02:00
|
|
|
}
|
2009-03-28 18:28:41 +01:00
|
|
|
|
2009-04-07 20:43:24 +02:00
|
|
|
/*
|
|
|
|
* Ok, we have to do it the hard way, copy all segments into
|
|
|
|
* a single aligned buffer.
|
|
|
|
*/
|
|
|
|
buf = qemu_memalign(512, aiocb->aio_nbytes);
|
|
|
|
if (aiocb->aio_type == QEMU_PAIO_WRITE) {
|
|
|
|
char *p = buf;
|
|
|
|
int i;
|
|
|
|
|
|
|
|
for (i = 0; i < aiocb->aio_niov; ++i) {
|
|
|
|
memcpy(p, aiocb->aio_iov[i].iov_base, aiocb->aio_iov[i].iov_len);
|
|
|
|
p += aiocb->aio_iov[i].iov_len;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
nbytes = handle_aiocb_rw_linear(aiocb, buf);
|
|
|
|
if (aiocb->aio_type != QEMU_PAIO_WRITE) {
|
|
|
|
char *p = buf;
|
|
|
|
size_t count = aiocb->aio_nbytes, copy;
|
|
|
|
int i;
|
|
|
|
|
|
|
|
for (i = 0; i < aiocb->aio_niov && count; ++i) {
|
|
|
|
copy = count;
|
|
|
|
if (copy > aiocb->aio_iov[i].iov_len)
|
|
|
|
copy = aiocb->aio_iov[i].iov_len;
|
|
|
|
memcpy(aiocb->aio_iov[i].iov_base, p, copy);
|
|
|
|
p += copy;
|
|
|
|
count -= copy;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
qemu_vfree(buf);
|
|
|
|
|
|
|
|
return nbytes;
|
2009-03-28 18:28:41 +01:00
|
|
|
}
|
|
|
|
|
2008-12-12 17:41:40 +01:00
|
|
|
static void *aio_thread(void *unused)
|
|
|
|
{
|
2009-02-21 06:48:17 +01:00
|
|
|
pid_t pid;
|
2008-12-12 17:41:40 +01:00
|
|
|
sigset_t set;
|
|
|
|
|
2009-02-21 06:48:17 +01:00
|
|
|
pid = getpid();
|
|
|
|
|
2008-12-12 17:41:40 +01:00
|
|
|
/* block all signals */
|
2009-02-21 06:48:11 +01:00
|
|
|
if (sigfillset(&set)) die("sigfillset");
|
|
|
|
if (sigprocmask(SIG_BLOCK, &set, NULL)) die("sigprocmask");
|
2008-12-12 17:41:40 +01:00
|
|
|
|
|
|
|
while (1) {
|
|
|
|
struct qemu_paiocb *aiocb;
|
2009-03-28 18:28:41 +01:00
|
|
|
size_t ret = 0;
|
2009-02-21 06:48:13 +01:00
|
|
|
qemu_timeval tv;
|
|
|
|
struct timespec ts;
|
|
|
|
|
|
|
|
qemu_gettimeofday(&tv);
|
|
|
|
ts.tv_sec = tv.tv_sec + 10;
|
|
|
|
ts.tv_nsec = 0;
|
2008-12-12 17:41:40 +01:00
|
|
|
|
2009-02-21 06:48:11 +01:00
|
|
|
mutex_lock(&lock);
|
2008-12-12 17:41:40 +01:00
|
|
|
|
|
|
|
while (TAILQ_EMPTY(&request_list) &&
|
|
|
|
!(ret == ETIMEDOUT)) {
|
2009-02-21 06:48:11 +01:00
|
|
|
ret = cond_timedwait(&cond, &lock, &ts);
|
2008-12-12 17:41:40 +01:00
|
|
|
}
|
|
|
|
|
2009-02-21 06:48:19 +01:00
|
|
|
if (TAILQ_EMPTY(&request_list))
|
2008-12-12 17:41:40 +01:00
|
|
|
break;
|
|
|
|
|
|
|
|
aiocb = TAILQ_FIRST(&request_list);
|
|
|
|
TAILQ_REMOVE(&request_list, aiocb, node);
|
|
|
|
aiocb->active = 1;
|
|
|
|
idle_threads--;
|
2009-02-21 06:48:11 +01:00
|
|
|
mutex_unlock(&lock);
|
2008-12-12 17:41:40 +01:00
|
|
|
|
2009-03-28 18:28:41 +01:00
|
|
|
switch (aiocb->aio_type) {
|
|
|
|
case QEMU_PAIO_READ:
|
|
|
|
case QEMU_PAIO_WRITE:
|
2009-04-07 20:43:24 +02:00
|
|
|
ret = handle_aiocb_rw(aiocb);
|
2009-03-28 18:28:41 +01:00
|
|
|
break;
|
|
|
|
case QEMU_PAIO_IOCTL:
|
|
|
|
ret = handle_aiocb_ioctl(aiocb);
|
|
|
|
break;
|
|
|
|
default:
|
|
|
|
fprintf(stderr, "invalid aio request (0x%x)\n", aiocb->aio_type);
|
|
|
|
ret = -EINVAL;
|
|
|
|
break;
|
|
|
|
}
|
2008-12-12 17:41:40 +01:00
|
|
|
|
2009-02-21 06:48:11 +01:00
|
|
|
mutex_lock(&lock);
|
2009-03-28 18:28:41 +01:00
|
|
|
aiocb->ret = ret;
|
2008-12-12 17:41:40 +01:00
|
|
|
idle_threads++;
|
2009-02-21 06:48:11 +01:00
|
|
|
mutex_unlock(&lock);
|
2008-12-12 17:41:40 +01:00
|
|
|
|
2009-02-21 06:48:17 +01:00
|
|
|
if (kill(pid, aiocb->ev_signo)) die("kill failed");
|
2008-12-12 17:41:40 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
idle_threads--;
|
|
|
|
cur_threads--;
|
2009-02-21 06:48:11 +01:00
|
|
|
mutex_unlock(&lock);
|
2008-12-12 17:41:40 +01:00
|
|
|
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
|
2009-02-21 06:48:11 +01:00
|
|
|
static void spawn_thread(void)
|
2008-12-12 17:41:40 +01:00
|
|
|
{
|
|
|
|
cur_threads++;
|
|
|
|
idle_threads++;
|
2009-02-21 06:48:11 +01:00
|
|
|
thread_create(&thread_id, &attr, aio_thread, NULL);
|
2008-12-12 17:41:40 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
int qemu_paio_init(struct qemu_paioinit *aioinit)
|
|
|
|
{
|
2009-02-21 06:48:17 +01:00
|
|
|
int ret;
|
|
|
|
|
|
|
|
ret = pthread_attr_init(&attr);
|
|
|
|
if (ret) die2(ret, "pthread_attr_init");
|
|
|
|
|
|
|
|
ret = pthread_attr_setdetachstate(&attr, PTHREAD_CREATE_DETACHED);
|
|
|
|
if (ret) die2(ret, "pthread_attr_setdetachstate");
|
|
|
|
|
2008-12-12 17:41:40 +01:00
|
|
|
TAILQ_INIT(&request_list);
|
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2009-03-28 18:28:41 +01:00
|
|
|
static int qemu_paio_submit(struct qemu_paiocb *aiocb, int type)
|
2008-12-12 17:41:40 +01:00
|
|
|
{
|
2009-03-28 18:28:41 +01:00
|
|
|
aiocb->aio_type = type;
|
2008-12-12 17:41:40 +01:00
|
|
|
aiocb->ret = -EINPROGRESS;
|
|
|
|
aiocb->active = 0;
|
2009-02-21 06:48:11 +01:00
|
|
|
mutex_lock(&lock);
|
2008-12-12 17:41:40 +01:00
|
|
|
if (idle_threads == 0 && cur_threads < max_threads)
|
|
|
|
spawn_thread();
|
|
|
|
TAILQ_INSERT_TAIL(&request_list, aiocb, node);
|
2009-02-21 06:48:11 +01:00
|
|
|
mutex_unlock(&lock);
|
2009-02-21 06:48:15 +01:00
|
|
|
cond_signal(&cond);
|
2008-12-12 17:41:40 +01:00
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
int qemu_paio_read(struct qemu_paiocb *aiocb)
|
|
|
|
{
|
2009-03-28 18:28:41 +01:00
|
|
|
return qemu_paio_submit(aiocb, QEMU_PAIO_READ);
|
2008-12-12 17:41:40 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
int qemu_paio_write(struct qemu_paiocb *aiocb)
|
|
|
|
{
|
2009-03-28 18:28:41 +01:00
|
|
|
return qemu_paio_submit(aiocb, QEMU_PAIO_WRITE);
|
|
|
|
}
|
|
|
|
|
|
|
|
int qemu_paio_ioctl(struct qemu_paiocb *aiocb)
|
|
|
|
{
|
|
|
|
return qemu_paio_submit(aiocb, QEMU_PAIO_IOCTL);
|
2008-12-12 17:41:40 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
ssize_t qemu_paio_return(struct qemu_paiocb *aiocb)
|
|
|
|
{
|
|
|
|
ssize_t ret;
|
|
|
|
|
2009-02-21 06:48:11 +01:00
|
|
|
mutex_lock(&lock);
|
2008-12-12 17:41:40 +01:00
|
|
|
ret = aiocb->ret;
|
2009-02-21 06:48:11 +01:00
|
|
|
mutex_unlock(&lock);
|
2008-12-12 17:41:40 +01:00
|
|
|
|
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|
|
|
|
int qemu_paio_error(struct qemu_paiocb *aiocb)
|
|
|
|
{
|
|
|
|
ssize_t ret = qemu_paio_return(aiocb);
|
|
|
|
|
|
|
|
if (ret < 0)
|
|
|
|
ret = -ret;
|
|
|
|
else
|
|
|
|
ret = 0;
|
|
|
|
|
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|
|
|
|
int qemu_paio_cancel(int fd, struct qemu_paiocb *aiocb)
|
|
|
|
{
|
|
|
|
int ret;
|
|
|
|
|
2009-02-21 06:48:11 +01:00
|
|
|
mutex_lock(&lock);
|
2008-12-12 17:41:40 +01:00
|
|
|
if (!aiocb->active) {
|
|
|
|
TAILQ_REMOVE(&request_list, aiocb, node);
|
|
|
|
aiocb->ret = -ECANCELED;
|
|
|
|
ret = QEMU_PAIO_CANCELED;
|
|
|
|
} else if (aiocb->ret == -EINPROGRESS)
|
|
|
|
ret = QEMU_PAIO_NOTCANCELED;
|
|
|
|
else
|
|
|
|
ret = QEMU_PAIO_ALLDONE;
|
2009-02-21 06:48:11 +01:00
|
|
|
mutex_unlock(&lock);
|
2008-12-12 17:41:40 +01:00
|
|
|
|
|
|
|
return ret;
|
|
|
|
}
|