Merge branch 'vhost' of git://git.kernel.org/pub/scm/linux/kernel/git/mst/qemu-kvm
[qemu-kvm/stefanha.git] / posix-aio-compat.c
blob09306ddaffe049d531822cc0595f4b68eb37e053
1 /*
2 * QEMU posix-aio emulation
4 * Copyright IBM, Corp. 2008
6 * Authors:
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.
14 #include <sys/ioctl.h>
15 #include <sys/types.h>
16 #include <pthread.h>
17 #include <unistd.h>
18 #include <errno.h>
19 #include <time.h>
20 #include <signal.h>
21 #include <string.h>
22 #include <stdlib.h>
23 #include <stdio.h>
25 #include "qemu-queue.h"
26 #include "osdep.h"
27 #include "qemu-common.h"
28 #include "trace.h"
29 #include "block_int.h"
30 #include "compatfd.h"
32 #include "block/raw-posix-aio.h"
35 struct qemu_paiocb {
36 BlockDriverAIOCB common;
37 int aio_fildes;
38 union {
39 struct iovec *aio_iov;
40 void *aio_ioctl_buf;
42 int aio_niov;
43 size_t aio_nbytes;
44 #define aio_ioctl_cmd aio_nbytes /* for QEMU_AIO_IOCTL */
45 int ev_signo;
46 off_t aio_offset;
48 QTAILQ_ENTRY(qemu_paiocb) node;
49 int aio_type;
50 ssize_t ret;
51 int active;
52 struct qemu_paiocb *next;
54 int async_context_id;
57 typedef struct PosixAioState {
58 int fd;
59 struct qemu_paiocb *first_aio;
60 } PosixAioState;
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 QTAILQ_HEAD(, qemu_paiocb) request_list;
72 #ifdef CONFIG_PREADV
73 static int preadv_present = 1;
74 #else
75 static int preadv_present = 0;
76 #endif
78 static void die2(int err, const char *what)
80 fprintf(stderr, "%s failed: %s\n", what, strerror(err));
81 abort();
84 static void die(const char *what)
86 die2(errno, what);
89 static void mutex_lock(pthread_mutex_t *mutex)
91 int ret = pthread_mutex_lock(mutex);
92 if (ret) die2(ret, "pthread_mutex_lock");
95 static void mutex_unlock(pthread_mutex_t *mutex)
97 int ret = pthread_mutex_unlock(mutex);
98 if (ret) die2(ret, "pthread_mutex_unlock");
101 static int cond_timedwait(pthread_cond_t *cond, pthread_mutex_t *mutex,
102 struct timespec *ts)
104 int ret = pthread_cond_timedwait(cond, mutex, ts);
105 if (ret && ret != ETIMEDOUT) die2(ret, "pthread_cond_timedwait");
106 return ret;
109 static void cond_signal(pthread_cond_t *cond)
111 int ret = pthread_cond_signal(cond);
112 if (ret) die2(ret, "pthread_cond_signal");
115 static void thread_create(pthread_t *thread, pthread_attr_t *attr,
116 void *(*start_routine)(void*), void *arg)
118 int ret = pthread_create(thread, attr, start_routine, arg);
119 if (ret) die2(ret, "pthread_create");
122 static ssize_t handle_aiocb_ioctl(struct qemu_paiocb *aiocb)
124 int ret;
126 ret = ioctl(aiocb->aio_fildes, aiocb->aio_ioctl_cmd, aiocb->aio_ioctl_buf);
127 if (ret == -1)
128 return -errno;
131 * This looks weird, but the aio code only consideres a request
132 * successfull if it has written the number full number of bytes.
134 * Now we overload aio_nbytes as aio_ioctl_cmd for the ioctl command,
135 * so in fact we return the ioctl command here to make posix_aio_read()
136 * happy..
138 return aiocb->aio_nbytes;
141 static ssize_t handle_aiocb_flush(struct qemu_paiocb *aiocb)
143 int ret;
145 ret = qemu_fdatasync(aiocb->aio_fildes);
146 if (ret == -1)
147 return -errno;
148 return 0;
151 #ifdef CONFIG_PREADV
153 static ssize_t
154 qemu_preadv(int fd, const struct iovec *iov, int nr_iov, off_t offset)
156 return preadv(fd, iov, nr_iov, offset);
159 static ssize_t
160 qemu_pwritev(int fd, const struct iovec *iov, int nr_iov, off_t offset)
162 return pwritev(fd, iov, nr_iov, offset);
165 #else
167 static ssize_t
168 qemu_preadv(int fd, const struct iovec *iov, int nr_iov, off_t offset)
170 return -ENOSYS;
173 static ssize_t
174 qemu_pwritev(int fd, const struct iovec *iov, int nr_iov, off_t offset)
176 return -ENOSYS;
179 #endif
181 static ssize_t handle_aiocb_rw_vector(struct qemu_paiocb *aiocb)
183 size_t offset = 0;
184 ssize_t len;
186 do {
187 if (aiocb->aio_type & QEMU_AIO_WRITE)
188 len = qemu_pwritev(aiocb->aio_fildes,
189 aiocb->aio_iov,
190 aiocb->aio_niov,
191 aiocb->aio_offset + offset);
192 else
193 len = qemu_preadv(aiocb->aio_fildes,
194 aiocb->aio_iov,
195 aiocb->aio_niov,
196 aiocb->aio_offset + offset);
197 } while (len == -1 && errno == EINTR);
199 if (len == -1)
200 return -errno;
201 return len;
204 static ssize_t handle_aiocb_rw_linear(struct qemu_paiocb *aiocb, char *buf)
206 ssize_t offset = 0;
207 ssize_t len;
209 while (offset < aiocb->aio_nbytes) {
210 if (aiocb->aio_type & QEMU_AIO_WRITE)
211 len = pwrite(aiocb->aio_fildes,
212 (const char *)buf + offset,
213 aiocb->aio_nbytes - offset,
214 aiocb->aio_offset + offset);
215 else
216 len = pread(aiocb->aio_fildes,
217 buf + offset,
218 aiocb->aio_nbytes - offset,
219 aiocb->aio_offset + offset);
221 if (len == -1 && errno == EINTR)
222 continue;
223 else if (len == -1) {
224 offset = -errno;
225 break;
226 } else if (len == 0)
227 break;
229 offset += len;
232 return offset;
235 static ssize_t handle_aiocb_rw(struct qemu_paiocb *aiocb)
237 ssize_t nbytes;
238 char *buf;
240 if (!(aiocb->aio_type & QEMU_AIO_MISALIGNED)) {
242 * If there is just a single buffer, and it is properly aligned
243 * we can just use plain pread/pwrite without any problems.
245 if (aiocb->aio_niov == 1)
246 return handle_aiocb_rw_linear(aiocb, aiocb->aio_iov->iov_base);
249 * We have more than one iovec, and all are properly aligned.
251 * Try preadv/pwritev first and fall back to linearizing the
252 * buffer if it's not supported.
254 if (preadv_present) {
255 nbytes = handle_aiocb_rw_vector(aiocb);
256 if (nbytes == aiocb->aio_nbytes)
257 return nbytes;
258 if (nbytes < 0 && nbytes != -ENOSYS)
259 return nbytes;
260 preadv_present = 0;
264 * XXX(hch): short read/write. no easy way to handle the reminder
265 * using these interfaces. For now retry using plain
266 * pread/pwrite?
271 * Ok, we have to do it the hard way, copy all segments into
272 * a single aligned buffer.
274 buf = qemu_blockalign(aiocb->common.bs, aiocb->aio_nbytes);
275 if (aiocb->aio_type & QEMU_AIO_WRITE) {
276 char *p = buf;
277 int i;
279 for (i = 0; i < aiocb->aio_niov; ++i) {
280 memcpy(p, aiocb->aio_iov[i].iov_base, aiocb->aio_iov[i].iov_len);
281 p += aiocb->aio_iov[i].iov_len;
285 nbytes = handle_aiocb_rw_linear(aiocb, buf);
286 if (!(aiocb->aio_type & QEMU_AIO_WRITE)) {
287 char *p = buf;
288 size_t count = aiocb->aio_nbytes, copy;
289 int i;
291 for (i = 0; i < aiocb->aio_niov && count; ++i) {
292 copy = count;
293 if (copy > aiocb->aio_iov[i].iov_len)
294 copy = aiocb->aio_iov[i].iov_len;
295 memcpy(aiocb->aio_iov[i].iov_base, p, copy);
296 p += copy;
297 count -= copy;
300 qemu_vfree(buf);
302 return nbytes;
305 static void *aio_thread(void *unused)
307 pid_t pid;
309 pid = getpid();
311 while (1) {
312 struct qemu_paiocb *aiocb;
313 ssize_t ret = 0;
314 qemu_timeval tv;
315 struct timespec ts;
317 qemu_gettimeofday(&tv);
318 ts.tv_sec = tv.tv_sec + 10;
319 ts.tv_nsec = 0;
321 mutex_lock(&lock);
323 while (QTAILQ_EMPTY(&request_list) &&
324 !(ret == ETIMEDOUT)) {
325 ret = cond_timedwait(&cond, &lock, &ts);
328 if (QTAILQ_EMPTY(&request_list))
329 break;
331 aiocb = QTAILQ_FIRST(&request_list);
332 QTAILQ_REMOVE(&request_list, aiocb, node);
333 aiocb->active = 1;
334 idle_threads--;
335 mutex_unlock(&lock);
337 switch (aiocb->aio_type & QEMU_AIO_TYPE_MASK) {
338 case QEMU_AIO_READ:
339 case QEMU_AIO_WRITE:
340 ret = handle_aiocb_rw(aiocb);
341 break;
342 case QEMU_AIO_FLUSH:
343 ret = handle_aiocb_flush(aiocb);
344 break;
345 case QEMU_AIO_IOCTL:
346 ret = handle_aiocb_ioctl(aiocb);
347 break;
348 default:
349 fprintf(stderr, "invalid aio request (0x%x)\n", aiocb->aio_type);
350 ret = -EINVAL;
351 break;
354 mutex_lock(&lock);
355 aiocb->ret = ret;
356 idle_threads++;
357 mutex_unlock(&lock);
359 if (kill(pid, aiocb->ev_signo)) die("kill failed");
362 idle_threads--;
363 cur_threads--;
364 mutex_unlock(&lock);
366 return NULL;
369 static void spawn_thread(void)
371 sigset_t set, oldset;
373 cur_threads++;
374 idle_threads++;
376 /* block all signals */
377 if (sigfillset(&set)) die("sigfillset");
378 if (sigprocmask(SIG_SETMASK, &set, &oldset)) die("sigprocmask");
380 thread_create(&thread_id, &attr, aio_thread, NULL);
382 if (sigprocmask(SIG_SETMASK, &oldset, NULL)) die("sigprocmask restore");
385 static void qemu_paio_submit(struct qemu_paiocb *aiocb)
387 aiocb->ret = -EINPROGRESS;
388 aiocb->active = 0;
389 mutex_lock(&lock);
390 if (idle_threads == 0 && cur_threads < max_threads)
391 spawn_thread();
392 QTAILQ_INSERT_TAIL(&request_list, aiocb, node);
393 mutex_unlock(&lock);
394 cond_signal(&cond);
397 static ssize_t qemu_paio_return(struct qemu_paiocb *aiocb)
399 ssize_t ret;
401 mutex_lock(&lock);
402 ret = aiocb->ret;
403 mutex_unlock(&lock);
405 return ret;
408 static int qemu_paio_error(struct qemu_paiocb *aiocb)
410 ssize_t ret = qemu_paio_return(aiocb);
412 if (ret < 0)
413 ret = -ret;
414 else
415 ret = 0;
417 return ret;
420 static int posix_aio_process_queue(void *opaque)
422 PosixAioState *s = opaque;
423 struct qemu_paiocb *acb, **pacb;
424 int ret;
425 int result = 0;
426 int async_context_id = get_async_context_id();
428 for(;;) {
429 pacb = &s->first_aio;
430 for(;;) {
431 acb = *pacb;
432 if (!acb)
433 return result;
435 /* we're only interested in requests in the right context */
436 if (acb->async_context_id != async_context_id) {
437 pacb = &acb->next;
438 continue;
441 ret = qemu_paio_error(acb);
442 if (ret == ECANCELED) {
443 /* remove the request */
444 *pacb = acb->next;
445 qemu_aio_release(acb);
446 result = 1;
447 } else if (ret != EINPROGRESS) {
448 /* end of aio */
449 if (ret == 0) {
450 ret = qemu_paio_return(acb);
451 if (ret == acb->aio_nbytes)
452 ret = 0;
453 else
454 ret = -EINVAL;
455 } else {
456 ret = -ret;
458 /* remove the request */
459 *pacb = acb->next;
460 /* call the callback */
461 acb->common.cb(acb->common.opaque, ret);
462 qemu_aio_release(acb);
463 result = 1;
464 break;
465 } else {
466 pacb = &acb->next;
471 return result;
474 static void posix_aio_read(void *opaque)
476 PosixAioState *s = opaque;
477 union {
478 struct qemu_signalfd_siginfo siginfo;
479 char buf[128];
480 } sig;
481 size_t offset;
483 /* try to read from signalfd, don't freak out if we can't read anything */
484 offset = 0;
485 while (offset < 128) {
486 ssize_t len;
488 len = read(s->fd, sig.buf + offset, 128 - offset);
489 if (len == -1 && errno == EINTR)
490 continue;
491 if (len == -1 && errno == EAGAIN) {
492 /* there is no natural reason for this to happen,
493 * so we'll spin hard until we get everything just
494 * to be on the safe side. */
495 if (offset > 0)
496 continue;
499 offset += len;
502 posix_aio_process_queue(s);
505 static int posix_aio_flush(void *opaque)
507 PosixAioState *s = opaque;
508 return !!s->first_aio;
511 static PosixAioState *posix_aio_state;
513 static void paio_remove(struct qemu_paiocb *acb)
515 struct qemu_paiocb **pacb;
517 /* remove the callback from the queue */
518 pacb = &posix_aio_state->first_aio;
519 for(;;) {
520 if (*pacb == NULL) {
521 fprintf(stderr, "paio_remove: aio request not found!\n");
522 break;
523 } else if (*pacb == acb) {
524 *pacb = acb->next;
525 qemu_aio_release(acb);
526 break;
528 pacb = &(*pacb)->next;
532 static void paio_cancel(BlockDriverAIOCB *blockacb)
534 struct qemu_paiocb *acb = (struct qemu_paiocb *)blockacb;
535 int active = 0;
537 mutex_lock(&lock);
538 if (!acb->active) {
539 QTAILQ_REMOVE(&request_list, acb, node);
540 acb->ret = -ECANCELED;
541 } else if (acb->ret == -EINPROGRESS) {
542 active = 1;
544 mutex_unlock(&lock);
546 if (active) {
547 /* fail safe: if the aio could not be canceled, we wait for
548 it */
549 while (qemu_paio_error(acb) == EINPROGRESS)
553 paio_remove(acb);
556 static AIOPool raw_aio_pool = {
557 .aiocb_size = sizeof(struct qemu_paiocb),
558 .cancel = paio_cancel,
561 BlockDriverAIOCB *paio_submit(BlockDriverState *bs, int fd,
562 int64_t sector_num, QEMUIOVector *qiov, int nb_sectors,
563 BlockDriverCompletionFunc *cb, void *opaque, int type)
565 struct qemu_paiocb *acb;
567 acb = qemu_aio_get(&raw_aio_pool, bs, cb, opaque);
568 if (!acb)
569 return NULL;
570 acb->aio_type = type;
571 acb->aio_fildes = fd;
572 acb->ev_signo = SIGUSR2;
573 acb->async_context_id = get_async_context_id();
575 if (qiov) {
576 acb->aio_iov = qiov->iov;
577 acb->aio_niov = qiov->niov;
579 acb->aio_nbytes = nb_sectors * 512;
580 acb->aio_offset = sector_num * 512;
582 acb->next = posix_aio_state->first_aio;
583 posix_aio_state->first_aio = acb;
585 trace_paio_submit(acb, opaque, sector_num, nb_sectors, type);
586 qemu_paio_submit(acb);
587 return &acb->common;
590 BlockDriverAIOCB *paio_ioctl(BlockDriverState *bs, int fd,
591 unsigned long int req, void *buf,
592 BlockDriverCompletionFunc *cb, void *opaque)
594 struct qemu_paiocb *acb;
596 acb = qemu_aio_get(&raw_aio_pool, bs, cb, opaque);
597 if (!acb)
598 return NULL;
599 acb->aio_type = QEMU_AIO_IOCTL;
600 acb->aio_fildes = fd;
601 acb->ev_signo = SIGUSR2;
602 acb->async_context_id = get_async_context_id();
603 acb->aio_offset = 0;
604 acb->aio_ioctl_buf = buf;
605 acb->aio_ioctl_cmd = req;
607 acb->next = posix_aio_state->first_aio;
608 posix_aio_state->first_aio = acb;
610 qemu_paio_submit(acb);
611 return &acb->common;
614 int paio_init(void)
616 sigset_t mask;
617 PosixAioState *s;
618 int ret;
620 if (posix_aio_state)
621 return 0;
623 s = qemu_malloc(sizeof(PosixAioState));
625 /* Make sure to block AIO signal */
626 sigemptyset(&mask);
627 sigaddset(&mask, SIGUSR2);
628 sigprocmask(SIG_BLOCK, &mask, NULL);
630 s->first_aio = NULL;
631 s->fd = qemu_signalfd(&mask);
632 if (s->fd == -1) {
633 fprintf(stderr, "failed to create signalfd\n");
634 return -1;
637 fcntl(s->fd, F_SETFL, O_NONBLOCK);
639 qemu_aio_set_fd_handler(s->fd, posix_aio_read, NULL, posix_aio_flush,
640 posix_aio_process_queue, s);
642 ret = pthread_attr_init(&attr);
643 if (ret)
644 die2(ret, "pthread_attr_init");
646 ret = pthread_attr_setdetachstate(&attr, PTHREAD_CREATE_DETACHED);
647 if (ret)
648 die2(ret, "pthread_attr_setdetachstate");
650 QTAILQ_INIT(&request_list);
652 posix_aio_state = s;
653 return 0;