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.
|
|
|
|
*
|
2012-01-13 17:44:23 +01:00
|
|
|
* Contributions after 2012-01-13 are licensed under the terms of the
|
|
|
|
* GNU GPL, version 2 or (at your option) any later version.
|
2008-12-12 17:41:40 +01:00
|
|
|
*/
|
|
|
|
|
2009-03-28 18:28:41 +01:00
|
|
|
#include <sys/ioctl.h>
|
2009-08-20 16:58:19 +02:00
|
|
|
#include <sys/types.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>
|
2009-08-20 16:58:19 +02:00
|
|
|
|
2009-09-12 09:36:22 +02:00
|
|
|
#include "qemu-queue.h"
|
2008-12-12 17:41:40 +01:00
|
|
|
#include "osdep.h"
|
2010-10-26 10:39:23 +02:00
|
|
|
#include "sysemu.h"
|
2009-04-07 20:43:24 +02:00
|
|
|
#include "qemu-common.h"
|
2010-05-22 19:15:08 +02:00
|
|
|
#include "trace.h"
|
2009-08-20 16:58:19 +02:00
|
|
|
#include "block_int.h"
|
|
|
|
|
|
|
|
#include "block/raw-posix-aio.h"
|
|
|
|
|
2011-08-14 06:04:49 +02:00
|
|
|
static void do_spawn_thread(void);
|
2009-08-20 16:58:19 +02:00
|
|
|
|
|
|
|
struct qemu_paiocb {
|
|
|
|
BlockDriverAIOCB common;
|
|
|
|
int aio_fildes;
|
|
|
|
union {
|
|
|
|
struct iovec *aio_iov;
|
2010-05-27 13:52:08 +02:00
|
|
|
void *aio_ioctl_buf;
|
2009-08-20 16:58:19 +02:00
|
|
|
};
|
|
|
|
int aio_niov;
|
|
|
|
size_t aio_nbytes;
|
|
|
|
#define aio_ioctl_cmd aio_nbytes /* for QEMU_AIO_IOCTL */
|
|
|
|
off_t aio_offset;
|
|
|
|
|
2009-09-12 09:36:22 +02:00
|
|
|
QTAILQ_ENTRY(qemu_paiocb) node;
|
2009-08-20 16:58:19 +02:00
|
|
|
int aio_type;
|
|
|
|
ssize_t ret;
|
|
|
|
int active;
|
|
|
|
struct qemu_paiocb *next;
|
|
|
|
};
|
|
|
|
|
|
|
|
typedef struct PosixAioState {
|
|
|
|
int rfd, wfd;
|
|
|
|
struct qemu_paiocb *first_aio;
|
|
|
|
} PosixAioState;
|
2008-12-12 17:41:40 +01:00
|
|
|
|
|
|
|
|
|
|
|
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;
|
2011-08-14 06:04:49 +02:00
|
|
|
static int new_threads = 0; /* backlog of threads we need to create */
|
|
|
|
static int pending_threads = 0; /* threads created but not running yet */
|
|
|
|
static QEMUBH *new_thread_bh;
|
2009-09-12 09:36:22 +02:00
|
|
|
static QTAILQ_HEAD(, qemu_paiocb) request_list;
|
2008-12-12 17:41:40 +01:00
|
|
|
|
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-11-18 12:15:10 +01:00
|
|
|
static ssize_t handle_aiocb_ioctl(struct qemu_paiocb *aiocb)
|
2009-04-07 20:43:24 +02:00
|
|
|
{
|
2010-05-27 13:52:08 +02:00
|
|
|
int ret;
|
|
|
|
|
|
|
|
ret = ioctl(aiocb->aio_fildes, aiocb->aio_ioctl_cmd, aiocb->aio_ioctl_buf);
|
|
|
|
if (ret == -1)
|
|
|
|
return -errno;
|
|
|
|
|
|
|
|
/*
|
2011-12-10 00:19:46 +01:00
|
|
|
* This looks weird, but the aio code only considers a request
|
|
|
|
* successful if it has written the full number of bytes.
|
2010-05-27 13:52:08 +02:00
|
|
|
*
|
|
|
|
* 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-11-18 12:15:10 +01:00
|
|
|
static ssize_t handle_aiocb_flush(struct qemu_paiocb *aiocb)
|
2009-09-04 19:01:49 +02:00
|
|
|
{
|
|
|
|
int ret;
|
|
|
|
|
2009-09-12 08:19:14 +02:00
|
|
|
ret = qemu_fdatasync(aiocb->aio_fildes);
|
2009-09-04 19:01:49 +02:00
|
|
|
if (ret == -1)
|
|
|
|
return -errno;
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
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-11-18 12:15:10 +01:00
|
|
|
static ssize_t handle_aiocb_rw_vector(struct qemu_paiocb *aiocb)
|
2009-04-07 20:43:28 +02:00
|
|
|
{
|
|
|
|
ssize_t len;
|
|
|
|
|
|
|
|
do {
|
2009-08-20 16:58:19 +02:00
|
|
|
if (aiocb->aio_type & QEMU_AIO_WRITE)
|
2009-04-07 20:43:28 +02:00
|
|
|
len = qemu_pwritev(aiocb->aio_fildes,
|
|
|
|
aiocb->aio_iov,
|
|
|
|
aiocb->aio_niov,
|
2011-09-16 13:34:46 +02:00
|
|
|
aiocb->aio_offset);
|
2009-04-07 20:43:28 +02:00
|
|
|
else
|
|
|
|
len = qemu_preadv(aiocb->aio_fildes,
|
|
|
|
aiocb->aio_iov,
|
|
|
|
aiocb->aio_niov,
|
2011-09-16 13:34:46 +02:00
|
|
|
aiocb->aio_offset);
|
2009-04-07 20:43:28 +02:00
|
|
|
} while (len == -1 && errno == EINTR);
|
|
|
|
|
|
|
|
if (len == -1)
|
|
|
|
return -errno;
|
|
|
|
return len;
|
|
|
|
}
|
|
|
|
|
2011-07-25 19:42:37 +02:00
|
|
|
/*
|
|
|
|
* Read/writes the data to/from a given linear buffer.
|
|
|
|
*
|
|
|
|
* Returns the number of bytes handles or -errno in case of an error. Short
|
|
|
|
* reads are only returned if the end of the file is reached.
|
|
|
|
*/
|
2009-11-18 12:15:10 +01:00
|
|
|
static ssize_t handle_aiocb_rw_linear(struct qemu_paiocb *aiocb, char *buf)
|
2009-03-28 18:28:41 +01:00
|
|
|
{
|
2009-11-18 12:15:10 +01:00
|
|
|
ssize_t offset = 0;
|
|
|
|
ssize_t len;
|
2009-03-28 18:28:41 +01:00
|
|
|
|
|
|
|
while (offset < aiocb->aio_nbytes) {
|
2009-08-20 16:58:19 +02:00
|
|
|
if (aiocb->aio_type & QEMU_AIO_WRITE)
|
2009-04-07 20:43:24 +02:00
|
|
|
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-11-18 12:15:10 +01:00
|
|
|
static ssize_t handle_aiocb_rw(struct qemu_paiocb *aiocb)
|
2009-03-28 18:28:41 +01:00
|
|
|
{
|
2009-11-18 12:15:10 +01:00
|
|
|
ssize_t nbytes;
|
2009-04-07 20:43:24 +02:00
|
|
|
char *buf;
|
|
|
|
|
2009-08-20 16:58:19 +02:00
|
|
|
if (!(aiocb->aio_type & QEMU_AIO_MISALIGNED)) {
|
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.
|
|
|
|
*/
|
2010-05-27 13:52:08 +02:00
|
|
|
if (preadv_present) {
|
2009-04-07 20:43:28 +02:00
|
|
|
nbytes = handle_aiocb_rw_vector(aiocb);
|
|
|
|
if (nbytes == aiocb->aio_nbytes)
|
2010-05-27 13:52:08 +02:00
|
|
|
return nbytes;
|
2009-04-07 20:43:28 +02:00
|
|
|
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.
|
|
|
|
*/
|
2010-09-12 23:42:56 +02:00
|
|
|
buf = qemu_blockalign(aiocb->common.bs, aiocb->aio_nbytes);
|
2009-08-20 16:58:19 +02:00
|
|
|
if (aiocb->aio_type & QEMU_AIO_WRITE) {
|
2009-04-07 20:43:24 +02:00
|
|
|
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);
|
2009-08-20 16:58:19 +02:00
|
|
|
if (!(aiocb->aio_type & QEMU_AIO_WRITE)) {
|
2009-04-07 20:43:24 +02:00
|
|
|
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
|
|
|
}
|
|
|
|
|
2011-09-19 16:37:13 +02:00
|
|
|
static void posix_aio_notify_event(void);
|
|
|
|
|
2008-12-12 17:41:40 +01:00
|
|
|
static void *aio_thread(void *unused)
|
|
|
|
{
|
2011-08-14 06:04:49 +02:00
|
|
|
mutex_lock(&lock);
|
|
|
|
pending_threads--;
|
|
|
|
mutex_unlock(&lock);
|
|
|
|
do_spawn_thread();
|
|
|
|
|
2008-12-12 17:41:40 +01:00
|
|
|
while (1) {
|
|
|
|
struct qemu_paiocb *aiocb;
|
2009-11-18 12:15:10 +01:00
|
|
|
ssize_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
|
|
|
|
2009-09-12 09:36:22 +02:00
|
|
|
while (QTAILQ_EMPTY(&request_list) &&
|
2008-12-12 17:41:40 +01:00
|
|
|
!(ret == ETIMEDOUT)) {
|
2011-05-02 17:32:54 +02:00
|
|
|
idle_threads++;
|
2009-02-21 06:48:11 +01:00
|
|
|
ret = cond_timedwait(&cond, &lock, &ts);
|
2011-05-02 17:32:54 +02:00
|
|
|
idle_threads--;
|
2008-12-12 17:41:40 +01:00
|
|
|
}
|
|
|
|
|
2009-09-12 09:36:22 +02:00
|
|
|
if (QTAILQ_EMPTY(&request_list))
|
2008-12-12 17:41:40 +01:00
|
|
|
break;
|
|
|
|
|
2009-09-12 09:36:22 +02:00
|
|
|
aiocb = QTAILQ_FIRST(&request_list);
|
|
|
|
QTAILQ_REMOVE(&request_list, aiocb, node);
|
2008-12-12 17:41:40 +01:00
|
|
|
aiocb->active = 1;
|
2009-02-21 06:48:11 +01:00
|
|
|
mutex_unlock(&lock);
|
2008-12-12 17:41:40 +01:00
|
|
|
|
2009-08-20 16:58:19 +02:00
|
|
|
switch (aiocb->aio_type & QEMU_AIO_TYPE_MASK) {
|
|
|
|
case QEMU_AIO_READ:
|
2011-07-25 19:42:37 +02:00
|
|
|
ret = handle_aiocb_rw(aiocb);
|
|
|
|
if (ret >= 0 && ret < aiocb->aio_nbytes && aiocb->common.bs->growable) {
|
|
|
|
/* A short read means that we have reached EOF. Pad the buffer
|
|
|
|
* with zeros for bytes after EOF. */
|
|
|
|
QEMUIOVector qiov;
|
|
|
|
|
|
|
|
qemu_iovec_init_external(&qiov, aiocb->aio_iov,
|
|
|
|
aiocb->aio_niov);
|
|
|
|
qemu_iovec_memset_skip(&qiov, 0, aiocb->aio_nbytes - ret, ret);
|
|
|
|
|
|
|
|
ret = aiocb->aio_nbytes;
|
|
|
|
}
|
|
|
|
break;
|
2009-08-20 16:58:19 +02:00
|
|
|
case QEMU_AIO_WRITE:
|
2010-05-27 13:52:08 +02:00
|
|
|
ret = handle_aiocb_rw(aiocb);
|
|
|
|
break;
|
2009-09-04 19:01:49 +02:00
|
|
|
case QEMU_AIO_FLUSH:
|
2010-05-27 13:52:08 +02:00
|
|
|
ret = handle_aiocb_flush(aiocb);
|
|
|
|
break;
|
2009-08-20 16:58:19 +02:00
|
|
|
case QEMU_AIO_IOCTL:
|
2010-05-27 13:52:08 +02:00
|
|
|
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;
|
2009-02-21 06:48:11 +01:00
|
|
|
mutex_unlock(&lock);
|
2008-12-12 17:41:40 +01:00
|
|
|
|
2011-09-19 16:37:13 +02:00
|
|
|
posix_aio_notify_event();
|
2008-12-12 17:41:40 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
cur_threads--;
|
2009-02-21 06:48:11 +01:00
|
|
|
mutex_unlock(&lock);
|
2008-12-12 17:41:40 +01:00
|
|
|
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
|
2011-08-14 06:04:49 +02:00
|
|
|
static void do_spawn_thread(void)
|
2008-12-12 17:41:40 +01:00
|
|
|
{
|
2009-09-24 22:20:44 +02:00
|
|
|
sigset_t set, oldset;
|
|
|
|
|
2011-08-14 06:04:49 +02:00
|
|
|
mutex_lock(&lock);
|
|
|
|
if (!new_threads) {
|
|
|
|
mutex_unlock(&lock);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
new_threads--;
|
|
|
|
pending_threads++;
|
|
|
|
|
|
|
|
mutex_unlock(&lock);
|
2009-09-24 22:20:44 +02:00
|
|
|
|
|
|
|
/* block all signals */
|
|
|
|
if (sigfillset(&set)) die("sigfillset");
|
|
|
|
if (sigprocmask(SIG_SETMASK, &set, &oldset)) die("sigprocmask");
|
|
|
|
|
2009-02-21 06:48:11 +01:00
|
|
|
thread_create(&thread_id, &attr, aio_thread, NULL);
|
2009-09-24 22:20:44 +02:00
|
|
|
|
|
|
|
if (sigprocmask(SIG_SETMASK, &oldset, NULL)) die("sigprocmask restore");
|
2008-12-12 17:41:40 +01:00
|
|
|
}
|
|
|
|
|
2011-08-14 06:04:49 +02:00
|
|
|
static void spawn_thread_bh_fn(void *opaque)
|
|
|
|
{
|
|
|
|
do_spawn_thread();
|
|
|
|
}
|
|
|
|
|
|
|
|
static void spawn_thread(void)
|
|
|
|
{
|
|
|
|
cur_threads++;
|
|
|
|
new_threads++;
|
|
|
|
/* If there are threads being created, they will spawn new workers, so
|
|
|
|
* we don't spend time creating many threads in a loop holding a mutex or
|
|
|
|
* starving the current vcpu.
|
|
|
|
*
|
|
|
|
* If there are no idle threads, ask the main thread to create one, so we
|
|
|
|
* inherit the correct affinity instead of the vcpu affinity.
|
|
|
|
*/
|
|
|
|
if (!pending_threads) {
|
|
|
|
qemu_bh_schedule(new_thread_bh);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2009-08-20 16:58:19 +02:00
|
|
|
static void qemu_paio_submit(struct qemu_paiocb *aiocb)
|
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();
|
2009-09-12 09:36:22 +02:00
|
|
|
QTAILQ_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
|
|
|
}
|
|
|
|
|
2009-08-20 16:58:19 +02:00
|
|
|
static ssize_t qemu_paio_return(struct qemu_paiocb *aiocb)
|
2008-12-12 17:41:40 +01:00
|
|
|
{
|
|
|
|
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;
|
|
|
|
}
|
|
|
|
|
2009-08-20 16:58:19 +02:00
|
|
|
static int qemu_paio_error(struct qemu_paiocb *aiocb)
|
2008-12-12 17:41:40 +01:00
|
|
|
{
|
|
|
|
ssize_t ret = qemu_paio_return(aiocb);
|
|
|
|
|
|
|
|
if (ret < 0)
|
|
|
|
ret = -ret;
|
|
|
|
else
|
|
|
|
ret = 0;
|
|
|
|
|
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|
2009-10-22 17:54:35 +02:00
|
|
|
static int posix_aio_process_queue(void *opaque)
|
2008-12-12 17:41:40 +01:00
|
|
|
{
|
2009-08-20 16:58:19 +02:00
|
|
|
PosixAioState *s = opaque;
|
|
|
|
struct qemu_paiocb *acb, **pacb;
|
2008-12-12 17:41:40 +01:00
|
|
|
int ret;
|
2009-10-22 17:54:35 +02:00
|
|
|
int result = 0;
|
2009-08-20 16:58:19 +02:00
|
|
|
|
|
|
|
for(;;) {
|
|
|
|
pacb = &s->first_aio;
|
|
|
|
for(;;) {
|
|
|
|
acb = *pacb;
|
|
|
|
if (!acb)
|
2009-10-22 17:54:35 +02:00
|
|
|
return result;
|
2009-10-22 17:54:40 +02:00
|
|
|
|
2009-08-20 16:58:19 +02:00
|
|
|
ret = qemu_paio_error(acb);
|
|
|
|
if (ret == ECANCELED) {
|
|
|
|
/* remove the request */
|
|
|
|
*pacb = acb->next;
|
|
|
|
qemu_aio_release(acb);
|
2009-10-22 17:54:35 +02:00
|
|
|
result = 1;
|
2009-08-20 16:58:19 +02:00
|
|
|
} else if (ret != EINPROGRESS) {
|
|
|
|
/* end of aio */
|
|
|
|
if (ret == 0) {
|
|
|
|
ret = qemu_paio_return(acb);
|
|
|
|
if (ret == acb->aio_nbytes)
|
|
|
|
ret = 0;
|
|
|
|
else
|
|
|
|
ret = -EINVAL;
|
|
|
|
} else {
|
|
|
|
ret = -ret;
|
|
|
|
}
|
2011-03-07 09:06:10 +01:00
|
|
|
|
|
|
|
trace_paio_complete(acb, acb->common.opaque, ret);
|
|
|
|
|
2009-08-20 16:58:19 +02:00
|
|
|
/* remove the request */
|
|
|
|
*pacb = acb->next;
|
|
|
|
/* call the callback */
|
|
|
|
acb->common.cb(acb->common.opaque, ret);
|
|
|
|
qemu_aio_release(acb);
|
2009-10-22 17:54:35 +02:00
|
|
|
result = 1;
|
2009-08-20 16:58:19 +02:00
|
|
|
break;
|
|
|
|
} else {
|
|
|
|
pacb = &acb->next;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2009-10-22 17:54:35 +02:00
|
|
|
|
|
|
|
return result;
|
|
|
|
}
|
|
|
|
|
|
|
|
static void posix_aio_read(void *opaque)
|
|
|
|
{
|
|
|
|
PosixAioState *s = opaque;
|
|
|
|
ssize_t len;
|
|
|
|
|
|
|
|
/* read all bytes from signal pipe */
|
|
|
|
for (;;) {
|
|
|
|
char bytes[16];
|
|
|
|
|
|
|
|
len = read(s->rfd, bytes, sizeof(bytes));
|
|
|
|
if (len == -1 && errno == EINTR)
|
|
|
|
continue; /* try again */
|
|
|
|
if (len == sizeof(bytes))
|
|
|
|
continue; /* more to read */
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
|
|
|
posix_aio_process_queue(s);
|
2009-08-20 16:58:19 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
static int posix_aio_flush(void *opaque)
|
|
|
|
{
|
|
|
|
PosixAioState *s = opaque;
|
|
|
|
return !!s->first_aio;
|
|
|
|
}
|
|
|
|
|
|
|
|
static PosixAioState *posix_aio_state;
|
|
|
|
|
2011-09-19 16:37:13 +02:00
|
|
|
static void posix_aio_notify_event(void)
|
2009-08-20 16:58:19 +02:00
|
|
|
{
|
2011-09-19 16:37:13 +02:00
|
|
|
char byte = 0;
|
|
|
|
ssize_t ret;
|
2009-08-20 16:58:19 +02:00
|
|
|
|
2011-09-19 16:37:13 +02:00
|
|
|
ret = write(posix_aio_state->wfd, &byte, sizeof(byte));
|
|
|
|
if (ret < 0 && errno != EAGAIN)
|
|
|
|
die("write()");
|
2009-08-20 16:58:19 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
static void paio_remove(struct qemu_paiocb *acb)
|
|
|
|
{
|
|
|
|
struct qemu_paiocb **pacb;
|
|
|
|
|
|
|
|
/* remove the callback from the queue */
|
|
|
|
pacb = &posix_aio_state->first_aio;
|
|
|
|
for(;;) {
|
|
|
|
if (*pacb == NULL) {
|
|
|
|
fprintf(stderr, "paio_remove: aio request not found!\n");
|
|
|
|
break;
|
|
|
|
} else if (*pacb == acb) {
|
|
|
|
*pacb = acb->next;
|
|
|
|
qemu_aio_release(acb);
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
pacb = &(*pacb)->next;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
static void paio_cancel(BlockDriverAIOCB *blockacb)
|
|
|
|
{
|
|
|
|
struct qemu_paiocb *acb = (struct qemu_paiocb *)blockacb;
|
|
|
|
int active = 0;
|
2008-12-12 17:41:40 +01:00
|
|
|
|
2011-03-07 09:06:10 +01:00
|
|
|
trace_paio_cancel(acb, acb->common.opaque);
|
|
|
|
|
2009-02-21 06:48:11 +01:00
|
|
|
mutex_lock(&lock);
|
2009-08-20 16:58:19 +02:00
|
|
|
if (!acb->active) {
|
2009-09-12 09:36:22 +02:00
|
|
|
QTAILQ_REMOVE(&request_list, acb, node);
|
2009-08-20 16:58:19 +02:00
|
|
|
acb->ret = -ECANCELED;
|
|
|
|
} else if (acb->ret == -EINPROGRESS) {
|
|
|
|
active = 1;
|
|
|
|
}
|
2009-02-21 06:48:11 +01:00
|
|
|
mutex_unlock(&lock);
|
2008-12-12 17:41:40 +01:00
|
|
|
|
2009-08-20 16:58:19 +02:00
|
|
|
if (active) {
|
|
|
|
/* fail safe: if the aio could not be canceled, we wait for
|
|
|
|
it */
|
|
|
|
while (qemu_paio_error(acb) == EINPROGRESS)
|
|
|
|
;
|
|
|
|
}
|
|
|
|
|
|
|
|
paio_remove(acb);
|
|
|
|
}
|
|
|
|
|
|
|
|
static AIOPool raw_aio_pool = {
|
|
|
|
.aiocb_size = sizeof(struct qemu_paiocb),
|
|
|
|
.cancel = paio_cancel,
|
|
|
|
};
|
|
|
|
|
2009-10-26 13:03:08 +01:00
|
|
|
BlockDriverAIOCB *paio_submit(BlockDriverState *bs, int fd,
|
2009-08-20 16:58:19 +02:00
|
|
|
int64_t sector_num, QEMUIOVector *qiov, int nb_sectors,
|
|
|
|
BlockDriverCompletionFunc *cb, void *opaque, int type)
|
|
|
|
{
|
|
|
|
struct qemu_paiocb *acb;
|
|
|
|
|
|
|
|
acb = qemu_aio_get(&raw_aio_pool, bs, cb, opaque);
|
|
|
|
acb->aio_type = type;
|
|
|
|
acb->aio_fildes = fd;
|
2009-10-22 17:54:40 +02:00
|
|
|
|
2009-09-04 19:01:49 +02:00
|
|
|
if (qiov) {
|
|
|
|
acb->aio_iov = qiov->iov;
|
|
|
|
acb->aio_niov = qiov->niov;
|
|
|
|
}
|
2009-08-20 16:58:19 +02:00
|
|
|
acb->aio_nbytes = nb_sectors * 512;
|
|
|
|
acb->aio_offset = sector_num * 512;
|
|
|
|
|
|
|
|
acb->next = posix_aio_state->first_aio;
|
|
|
|
posix_aio_state->first_aio = acb;
|
|
|
|
|
2010-05-22 19:15:08 +02:00
|
|
|
trace_paio_submit(acb, opaque, sector_num, nb_sectors, type);
|
2009-08-20 16:58:19 +02:00
|
|
|
qemu_paio_submit(acb);
|
|
|
|
return &acb->common;
|
|
|
|
}
|
|
|
|
|
|
|
|
BlockDriverAIOCB *paio_ioctl(BlockDriverState *bs, int fd,
|
|
|
|
unsigned long int req, void *buf,
|
|
|
|
BlockDriverCompletionFunc *cb, void *opaque)
|
|
|
|
{
|
|
|
|
struct qemu_paiocb *acb;
|
|
|
|
|
|
|
|
acb = qemu_aio_get(&raw_aio_pool, bs, cb, opaque);
|
|
|
|
acb->aio_type = QEMU_AIO_IOCTL;
|
|
|
|
acb->aio_fildes = fd;
|
|
|
|
acb->aio_offset = 0;
|
|
|
|
acb->aio_ioctl_buf = buf;
|
|
|
|
acb->aio_ioctl_cmd = req;
|
|
|
|
|
|
|
|
acb->next = posix_aio_state->first_aio;
|
|
|
|
posix_aio_state->first_aio = acb;
|
|
|
|
|
|
|
|
qemu_paio_submit(acb);
|
|
|
|
return &acb->common;
|
|
|
|
}
|
|
|
|
|
2009-10-26 13:03:08 +01:00
|
|
|
int paio_init(void)
|
2009-08-20 16:58:19 +02:00
|
|
|
{
|
|
|
|
PosixAioState *s;
|
|
|
|
int fds[2];
|
|
|
|
int ret;
|
|
|
|
|
|
|
|
if (posix_aio_state)
|
2009-10-26 13:03:08 +01:00
|
|
|
return 0;
|
2009-08-20 16:58:19 +02:00
|
|
|
|
2011-08-21 05:09:37 +02:00
|
|
|
s = g_malloc(sizeof(PosixAioState));
|
2009-08-20 16:58:19 +02:00
|
|
|
|
|
|
|
s->first_aio = NULL;
|
2009-12-02 12:24:42 +01:00
|
|
|
if (qemu_pipe(fds) == -1) {
|
2009-08-20 16:58:19 +02:00
|
|
|
fprintf(stderr, "failed to create pipe\n");
|
2011-11-11 10:40:08 +01:00
|
|
|
g_free(s);
|
2009-10-26 13:03:08 +01:00
|
|
|
return -1;
|
2009-08-20 16:58:19 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
s->rfd = fds[0];
|
|
|
|
s->wfd = fds[1];
|
|
|
|
|
|
|
|
fcntl(s->rfd, F_SETFL, O_NONBLOCK);
|
|
|
|
fcntl(s->wfd, F_SETFL, O_NONBLOCK);
|
|
|
|
|
2009-10-22 17:54:36 +02:00
|
|
|
qemu_aio_set_fd_handler(s->rfd, posix_aio_read, NULL, posix_aio_flush,
|
|
|
|
posix_aio_process_queue, s);
|
2009-08-20 16:58:19 +02:00
|
|
|
|
|
|
|
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");
|
|
|
|
|
2009-09-12 09:36:22 +02:00
|
|
|
QTAILQ_INIT(&request_list);
|
2011-08-14 06:04:49 +02:00
|
|
|
new_thread_bh = qemu_bh_new(spawn_thread_bh_fn, NULL);
|
2009-08-20 16:58:19 +02:00
|
|
|
|
|
|
|
posix_aio_state = s;
|
2009-10-26 13:03:08 +01:00
|
|
|
return 0;
|
2008-12-12 17:41:40 +01:00
|
|
|
}
|