Merge commit '70783b9c9be31e98421f17327a1127021abae672' into upstream-merge
[qemu-kvm/markmc.git] / posix-aio-compat.c
blobd7dbab3e499177166f67860d5bb6cc13dfa17fd2
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 "block_int.h"
29 #include "compatfd.h"
31 #include "block/raw-posix-aio.h"
34 struct qemu_paiocb {
35 BlockDriverAIOCB common;
36 int aio_fildes;
37 union {
38 struct iovec *aio_iov;
39 void *aio_ioctl_buf;
41 int aio_niov;
42 size_t aio_nbytes;
43 #define aio_ioctl_cmd aio_nbytes /* for QEMU_AIO_IOCTL */
44 int ev_signo;
45 off_t aio_offset;
47 QTAILQ_ENTRY(qemu_paiocb) node;
48 int aio_type;
49 ssize_t ret;
50 int active;
51 struct qemu_paiocb *next;
53 int async_context_id;
56 typedef struct PosixAioState {
57 int fd;
58 struct qemu_paiocb *first_aio;
59 } PosixAioState;
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 QTAILQ_HEAD(, qemu_paiocb) request_list;
71 #ifdef CONFIG_PREADV
72 static int preadv_present = 1;
73 #else
74 static int preadv_present = 0;
75 #endif
77 static void die2(int err, const char *what)
79 fprintf(stderr, "%s failed: %s\n", what, strerror(err));
80 abort();
83 static void die(const char *what)
85 die2(errno, what);
88 static void mutex_lock(pthread_mutex_t *mutex)
90 int ret = pthread_mutex_lock(mutex);
91 if (ret) die2(ret, "pthread_mutex_lock");
94 static void mutex_unlock(pthread_mutex_t *mutex)
96 int ret = pthread_mutex_unlock(mutex);
97 if (ret) die2(ret, "pthread_mutex_unlock");
100 static int cond_timedwait(pthread_cond_t *cond, pthread_mutex_t *mutex,
101 struct timespec *ts)
103 int ret = pthread_cond_timedwait(cond, mutex, ts);
104 if (ret && ret != ETIMEDOUT) die2(ret, "pthread_cond_timedwait");
105 return ret;
108 static void cond_signal(pthread_cond_t *cond)
110 int ret = pthread_cond_signal(cond);
111 if (ret) die2(ret, "pthread_cond_signal");
114 static void thread_create(pthread_t *thread, pthread_attr_t *attr,
115 void *(*start_routine)(void*), void *arg)
117 int ret = pthread_create(thread, attr, start_routine, arg);
118 if (ret) die2(ret, "pthread_create");
121 static size_t handle_aiocb_ioctl(struct qemu_paiocb *aiocb)
123 int ret;
125 ret = ioctl(aiocb->aio_fildes, aiocb->aio_ioctl_cmd, aiocb->aio_ioctl_buf);
126 if (ret == -1)
127 return -errno;
130 * This looks weird, but the aio code only consideres a request
131 * successfull if it has written the number full number of bytes.
133 * Now we overload aio_nbytes as aio_ioctl_cmd for the ioctl command,
134 * so in fact we return the ioctl command here to make posix_aio_read()
135 * happy..
137 return aiocb->aio_nbytes;
140 static size_t handle_aiocb_flush(struct qemu_paiocb *aiocb)
142 int ret;
144 ret = qemu_fdatasync(aiocb->aio_fildes);
145 if (ret == -1)
146 return -errno;
147 return 0;
150 #ifdef CONFIG_PREADV
152 static ssize_t
153 qemu_preadv(int fd, const struct iovec *iov, int nr_iov, off_t offset)
155 return preadv(fd, iov, nr_iov, offset);
158 static ssize_t
159 qemu_pwritev(int fd, const struct iovec *iov, int nr_iov, off_t offset)
161 return pwritev(fd, iov, nr_iov, offset);
164 #else
166 static ssize_t
167 qemu_preadv(int fd, const struct iovec *iov, int nr_iov, off_t offset)
169 return -ENOSYS;
172 static ssize_t
173 qemu_pwritev(int fd, const struct iovec *iov, int nr_iov, off_t offset)
175 return -ENOSYS;
178 #endif
180 static size_t handle_aiocb_rw_vector(struct qemu_paiocb *aiocb)
182 size_t offset = 0;
183 ssize_t len;
185 do {
186 if (aiocb->aio_type & QEMU_AIO_WRITE)
187 len = qemu_pwritev(aiocb->aio_fildes,
188 aiocb->aio_iov,
189 aiocb->aio_niov,
190 aiocb->aio_offset + offset);
191 else
192 len = qemu_preadv(aiocb->aio_fildes,
193 aiocb->aio_iov,
194 aiocb->aio_niov,
195 aiocb->aio_offset + offset);
196 } while (len == -1 && errno == EINTR);
198 if (len == -1)
199 return -errno;
200 return len;
203 static size_t handle_aiocb_rw_linear(struct qemu_paiocb *aiocb, char *buf)
205 size_t offset = 0;
206 size_t len;
208 while (offset < aiocb->aio_nbytes) {
209 if (aiocb->aio_type & QEMU_AIO_WRITE)
210 len = pwrite(aiocb->aio_fildes,
211 (const char *)buf + offset,
212 aiocb->aio_nbytes - offset,
213 aiocb->aio_offset + offset);
214 else
215 len = pread(aiocb->aio_fildes,
216 buf + offset,
217 aiocb->aio_nbytes - offset,
218 aiocb->aio_offset + offset);
220 if (len == -1 && errno == EINTR)
221 continue;
222 else if (len == -1) {
223 offset = -errno;
224 break;
225 } else if (len == 0)
226 break;
228 offset += len;
231 return offset;
234 static size_t handle_aiocb_rw(struct qemu_paiocb *aiocb)
236 size_t nbytes;
237 char *buf;
239 if (!(aiocb->aio_type & QEMU_AIO_MISALIGNED)) {
241 * If there is just a single buffer, and it is properly aligned
242 * we can just use plain pread/pwrite without any problems.
244 if (aiocb->aio_niov == 1)
245 return handle_aiocb_rw_linear(aiocb, aiocb->aio_iov->iov_base);
248 * We have more than one iovec, and all are properly aligned.
250 * Try preadv/pwritev first and fall back to linearizing the
251 * buffer if it's not supported.
253 if (preadv_present) {
254 nbytes = handle_aiocb_rw_vector(aiocb);
255 if (nbytes == aiocb->aio_nbytes)
256 return nbytes;
257 if (nbytes < 0 && nbytes != -ENOSYS)
258 return nbytes;
259 preadv_present = 0;
263 * XXX(hch): short read/write. no easy way to handle the reminder
264 * using these interfaces. For now retry using plain
265 * pread/pwrite?
270 * Ok, we have to do it the hard way, copy all segments into
271 * a single aligned buffer.
273 buf = qemu_memalign(512, aiocb->aio_nbytes);
274 if (aiocb->aio_type & QEMU_AIO_WRITE) {
275 char *p = buf;
276 int i;
278 for (i = 0; i < aiocb->aio_niov; ++i) {
279 memcpy(p, aiocb->aio_iov[i].iov_base, aiocb->aio_iov[i].iov_len);
280 p += aiocb->aio_iov[i].iov_len;
284 nbytes = handle_aiocb_rw_linear(aiocb, buf);
285 if (!(aiocb->aio_type & QEMU_AIO_WRITE)) {
286 char *p = buf;
287 size_t count = aiocb->aio_nbytes, copy;
288 int i;
290 for (i = 0; i < aiocb->aio_niov && count; ++i) {
291 copy = count;
292 if (copy > aiocb->aio_iov[i].iov_len)
293 copy = aiocb->aio_iov[i].iov_len;
294 memcpy(aiocb->aio_iov[i].iov_base, p, copy);
295 p += copy;
296 count -= copy;
299 qemu_vfree(buf);
301 return nbytes;
304 static void *aio_thread(void *unused)
306 pid_t pid;
308 pid = getpid();
310 while (1) {
311 struct qemu_paiocb *aiocb;
312 size_t ret = 0;
313 qemu_timeval tv;
314 struct timespec ts;
316 qemu_gettimeofday(&tv);
317 ts.tv_sec = tv.tv_sec + 10;
318 ts.tv_nsec = 0;
320 mutex_lock(&lock);
322 while (QTAILQ_EMPTY(&request_list) &&
323 !(ret == ETIMEDOUT)) {
324 ret = cond_timedwait(&cond, &lock, &ts);
327 if (QTAILQ_EMPTY(&request_list))
328 break;
330 aiocb = QTAILQ_FIRST(&request_list);
331 QTAILQ_REMOVE(&request_list, aiocb, node);
332 aiocb->active = 1;
333 idle_threads--;
334 mutex_unlock(&lock);
336 switch (aiocb->aio_type & QEMU_AIO_TYPE_MASK) {
337 case QEMU_AIO_READ:
338 case QEMU_AIO_WRITE:
339 ret = handle_aiocb_rw(aiocb);
340 break;
341 case QEMU_AIO_FLUSH:
342 ret = handle_aiocb_flush(aiocb);
343 break;
344 case QEMU_AIO_IOCTL:
345 ret = handle_aiocb_ioctl(aiocb);
346 break;
347 default:
348 fprintf(stderr, "invalid aio request (0x%x)\n", aiocb->aio_type);
349 ret = -EINVAL;
350 break;
353 mutex_lock(&lock);
354 aiocb->ret = ret;
355 idle_threads++;
356 mutex_unlock(&lock);
358 if (kill(pid, aiocb->ev_signo)) die("kill failed");
361 idle_threads--;
362 cur_threads--;
363 mutex_unlock(&lock);
365 return NULL;
368 static void spawn_thread(void)
370 sigset_t set, oldset;
372 cur_threads++;
373 idle_threads++;
375 /* block all signals */
376 if (sigfillset(&set)) die("sigfillset");
377 if (sigprocmask(SIG_SETMASK, &set, &oldset)) die("sigprocmask");
379 thread_create(&thread_id, &attr, aio_thread, NULL);
381 if (sigprocmask(SIG_SETMASK, &oldset, NULL)) die("sigprocmask restore");
384 static void qemu_paio_submit(struct qemu_paiocb *aiocb)
386 aiocb->ret = -EINPROGRESS;
387 aiocb->active = 0;
388 mutex_lock(&lock);
389 if (idle_threads == 0 && cur_threads < max_threads)
390 spawn_thread();
391 QTAILQ_INSERT_TAIL(&request_list, aiocb, node);
392 mutex_unlock(&lock);
393 cond_signal(&cond);
396 static ssize_t qemu_paio_return(struct qemu_paiocb *aiocb)
398 ssize_t ret;
400 mutex_lock(&lock);
401 ret = aiocb->ret;
402 mutex_unlock(&lock);
404 return ret;
407 static int qemu_paio_error(struct qemu_paiocb *aiocb)
409 ssize_t ret = qemu_paio_return(aiocb);
411 if (ret < 0)
412 ret = -ret;
413 else
414 ret = 0;
416 return ret;
419 static int posix_aio_process_queue(void *opaque)
421 PosixAioState *s = opaque;
422 struct qemu_paiocb *acb, **pacb;
423 int ret;
424 int result = 0;
425 int async_context_id = get_async_context_id();
427 for(;;) {
428 pacb = &s->first_aio;
429 for(;;) {
430 acb = *pacb;
431 if (!acb)
432 return result;
434 /* we're only interested in requests in the right context */
435 if (acb->async_context_id != async_context_id) {
436 pacb = &acb->next;
437 continue;
440 ret = qemu_paio_error(acb);
441 if (ret == ECANCELED) {
442 /* remove the request */
443 *pacb = acb->next;
444 qemu_aio_release(acb);
445 result = 1;
446 } else if (ret != EINPROGRESS) {
447 /* end of aio */
448 if (ret == 0) {
449 ret = qemu_paio_return(acb);
450 if (ret == acb->aio_nbytes)
451 ret = 0;
452 else
453 ret = -EINVAL;
454 } else {
455 ret = -ret;
457 /* remove the request */
458 *pacb = acb->next;
459 /* call the callback */
460 acb->common.cb(acb->common.opaque, ret);
461 qemu_aio_release(acb);
462 result = 1;
463 break;
464 } else {
465 pacb = &acb->next;
470 return result;
473 static void posix_aio_read(void *opaque)
475 PosixAioState *s = opaque;
476 union {
477 struct qemu_signalfd_siginfo siginfo;
478 char buf[128];
479 } sig;
480 size_t offset;
482 /* try to read from signalfd, don't freak out if we can't read anything */
483 offset = 0;
484 while (offset < 128) {
485 ssize_t len;
487 len = read(s->fd, sig.buf + offset, 128 - offset);
488 if (len == -1 && errno == EINTR)
489 continue;
490 if (len == -1 && errno == EAGAIN) {
491 /* there is no natural reason for this to happen,
492 * so we'll spin hard until we get everything just
493 * to be on the safe side. */
494 if (offset > 0)
495 continue;
498 offset += len;
501 posix_aio_process_queue(s);
504 static int posix_aio_flush(void *opaque)
506 PosixAioState *s = opaque;
507 return !!s->first_aio;
510 static PosixAioState *posix_aio_state;
512 static void paio_remove(struct qemu_paiocb *acb)
514 struct qemu_paiocb **pacb;
516 /* remove the callback from the queue */
517 pacb = &posix_aio_state->first_aio;
518 for(;;) {
519 if (*pacb == NULL) {
520 fprintf(stderr, "paio_remove: aio request not found!\n");
521 break;
522 } else if (*pacb == acb) {
523 *pacb = acb->next;
524 qemu_aio_release(acb);
525 break;
527 pacb = &(*pacb)->next;
531 static void paio_cancel(BlockDriverAIOCB *blockacb)
533 struct qemu_paiocb *acb = (struct qemu_paiocb *)blockacb;
534 int active = 0;
536 mutex_lock(&lock);
537 if (!acb->active) {
538 QTAILQ_REMOVE(&request_list, acb, node);
539 acb->ret = -ECANCELED;
540 } else if (acb->ret == -EINPROGRESS) {
541 active = 1;
543 mutex_unlock(&lock);
545 if (active) {
546 /* fail safe: if the aio could not be canceled, we wait for
547 it */
548 while (qemu_paio_error(acb) == EINPROGRESS)
552 paio_remove(acb);
555 static AIOPool raw_aio_pool = {
556 .aiocb_size = sizeof(struct qemu_paiocb),
557 .cancel = paio_cancel,
560 BlockDriverAIOCB *paio_submit(BlockDriverState *bs, void *aio_ctx, int fd,
561 int64_t sector_num, QEMUIOVector *qiov, int nb_sectors,
562 BlockDriverCompletionFunc *cb, void *opaque, int type)
564 struct qemu_paiocb *acb;
566 acb = qemu_aio_get(&raw_aio_pool, bs, cb, opaque);
567 if (!acb)
568 return NULL;
569 acb->aio_type = type;
570 acb->aio_fildes = fd;
571 acb->ev_signo = SIGUSR2;
572 acb->async_context_id = get_async_context_id();
574 if (qiov) {
575 acb->aio_iov = qiov->iov;
576 acb->aio_niov = qiov->niov;
578 acb->aio_nbytes = nb_sectors * 512;
579 acb->aio_offset = sector_num * 512;
581 acb->next = posix_aio_state->first_aio;
582 posix_aio_state->first_aio = acb;
584 qemu_paio_submit(acb);
585 return &acb->common;
588 BlockDriverAIOCB *paio_ioctl(BlockDriverState *bs, int fd,
589 unsigned long int req, void *buf,
590 BlockDriverCompletionFunc *cb, void *opaque)
592 struct qemu_paiocb *acb;
594 acb = qemu_aio_get(&raw_aio_pool, bs, cb, opaque);
595 if (!acb)
596 return NULL;
597 acb->aio_type = QEMU_AIO_IOCTL;
598 acb->aio_fildes = fd;
599 acb->ev_signo = SIGUSR2;
600 acb->aio_offset = 0;
601 acb->aio_ioctl_buf = buf;
602 acb->aio_ioctl_cmd = req;
604 acb->next = posix_aio_state->first_aio;
605 posix_aio_state->first_aio = acb;
607 qemu_paio_submit(acb);
608 return &acb->common;
611 void *paio_init(void)
613 sigset_t mask;
614 PosixAioState *s;
615 int ret;
617 if (posix_aio_state)
618 return posix_aio_state;
620 s = qemu_malloc(sizeof(PosixAioState));
622 /* Make sure to block AIO signal */
623 sigemptyset(&mask);
624 sigaddset(&mask, SIGUSR2);
625 sigprocmask(SIG_BLOCK, &mask, NULL);
627 s->first_aio = NULL;
628 s->fd = qemu_signalfd(&mask);
629 if (s->fd == -1) {
630 fprintf(stderr, "failed to create signalfd\n");
631 return NULL;
634 fcntl(s->fd, F_SETFL, O_NONBLOCK);
636 qemu_aio_set_fd_handler(s->fd, posix_aio_read, NULL, posix_aio_flush,
637 posix_aio_process_queue, s);
639 ret = pthread_attr_init(&attr);
640 if (ret)
641 die2(ret, "pthread_attr_init");
643 ret = pthread_attr_setdetachstate(&attr, PTHREAD_CREATE_DETACHED);
644 if (ret)
645 die2(ret, "pthread_attr_setdetachstate");
647 QTAILQ_INIT(&request_list);
649 posix_aio_state = s;
651 return posix_aio_state;