Merge commit '25be210f69543faae4f22bfa2c7446c4f1be62ef' into upstream-merge
[qemu-kvm/amd-iommu.git] / posix-aio-compat.c
blob65f2bfdefef94aa810d3fba5d4b36839bee5818f
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;
54 typedef struct PosixAioState {
55 int fd;
56 struct qemu_paiocb *first_aio;
57 } PosixAioState;
60 static pthread_mutex_t lock = PTHREAD_MUTEX_INITIALIZER;
61 static pthread_cond_t cond = PTHREAD_COND_INITIALIZER;
62 static pthread_t thread_id;
63 static pthread_attr_t attr;
64 static int max_threads = 64;
65 static int cur_threads = 0;
66 static int idle_threads = 0;
67 static QTAILQ_HEAD(, qemu_paiocb) request_list;
69 #ifdef CONFIG_PREADV
70 static int preadv_present = 1;
71 #else
72 static int preadv_present = 0;
73 #endif
75 static void die2(int err, const char *what)
77 fprintf(stderr, "%s failed: %s\n", what, strerror(err));
78 abort();
81 static void die(const char *what)
83 die2(errno, what);
86 static void mutex_lock(pthread_mutex_t *mutex)
88 int ret = pthread_mutex_lock(mutex);
89 if (ret) die2(ret, "pthread_mutex_lock");
92 static void mutex_unlock(pthread_mutex_t *mutex)
94 int ret = pthread_mutex_unlock(mutex);
95 if (ret) die2(ret, "pthread_mutex_unlock");
98 static int cond_timedwait(pthread_cond_t *cond, pthread_mutex_t *mutex,
99 struct timespec *ts)
101 int ret = pthread_cond_timedwait(cond, mutex, ts);
102 if (ret && ret != ETIMEDOUT) die2(ret, "pthread_cond_timedwait");
103 return ret;
106 static void cond_signal(pthread_cond_t *cond)
108 int ret = pthread_cond_signal(cond);
109 if (ret) die2(ret, "pthread_cond_signal");
112 static void thread_create(pthread_t *thread, pthread_attr_t *attr,
113 void *(*start_routine)(void*), void *arg)
115 int ret = pthread_create(thread, attr, start_routine, arg);
116 if (ret) die2(ret, "pthread_create");
119 static size_t handle_aiocb_ioctl(struct qemu_paiocb *aiocb)
121 int ret;
123 ret = ioctl(aiocb->aio_fildes, aiocb->aio_ioctl_cmd, aiocb->aio_ioctl_buf);
124 if (ret == -1)
125 return -errno;
128 * This looks weird, but the aio code only consideres a request
129 * successfull if it has written the number full number of bytes.
131 * Now we overload aio_nbytes as aio_ioctl_cmd for the ioctl command,
132 * so in fact we return the ioctl command here to make posix_aio_read()
133 * happy..
135 return aiocb->aio_nbytes;
138 static size_t handle_aiocb_flush(struct qemu_paiocb *aiocb)
140 int ret;
142 ret = qemu_fdatasync(aiocb->aio_fildes);
143 if (ret == -1)
144 return -errno;
145 return 0;
148 #ifdef CONFIG_PREADV
150 static ssize_t
151 qemu_preadv(int fd, const struct iovec *iov, int nr_iov, off_t offset)
153 return preadv(fd, iov, nr_iov, offset);
156 static ssize_t
157 qemu_pwritev(int fd, const struct iovec *iov, int nr_iov, off_t offset)
159 return pwritev(fd, iov, nr_iov, offset);
162 #else
164 static ssize_t
165 qemu_preadv(int fd, const struct iovec *iov, int nr_iov, off_t offset)
167 return -ENOSYS;
170 static ssize_t
171 qemu_pwritev(int fd, const struct iovec *iov, int nr_iov, off_t offset)
173 return -ENOSYS;
176 #endif
178 static size_t handle_aiocb_rw_vector(struct qemu_paiocb *aiocb)
180 size_t offset = 0;
181 ssize_t len;
183 do {
184 if (aiocb->aio_type & QEMU_AIO_WRITE)
185 len = qemu_pwritev(aiocb->aio_fildes,
186 aiocb->aio_iov,
187 aiocb->aio_niov,
188 aiocb->aio_offset + offset);
189 else
190 len = qemu_preadv(aiocb->aio_fildes,
191 aiocb->aio_iov,
192 aiocb->aio_niov,
193 aiocb->aio_offset + offset);
194 } while (len == -1 && errno == EINTR);
196 if (len == -1)
197 return -errno;
198 return len;
201 static size_t handle_aiocb_rw_linear(struct qemu_paiocb *aiocb, char *buf)
203 size_t offset = 0;
204 size_t len;
206 while (offset < aiocb->aio_nbytes) {
207 if (aiocb->aio_type & QEMU_AIO_WRITE)
208 len = pwrite(aiocb->aio_fildes,
209 (const char *)buf + offset,
210 aiocb->aio_nbytes - offset,
211 aiocb->aio_offset + offset);
212 else
213 len = pread(aiocb->aio_fildes,
214 buf + offset,
215 aiocb->aio_nbytes - offset,
216 aiocb->aio_offset + offset);
218 if (len == -1 && errno == EINTR)
219 continue;
220 else if (len == -1) {
221 offset = -errno;
222 break;
223 } else if (len == 0)
224 break;
226 offset += len;
229 return offset;
232 static size_t handle_aiocb_rw(struct qemu_paiocb *aiocb)
234 size_t nbytes;
235 char *buf;
237 if (!(aiocb->aio_type & QEMU_AIO_MISALIGNED)) {
239 * If there is just a single buffer, and it is properly aligned
240 * we can just use plain pread/pwrite without any problems.
242 if (aiocb->aio_niov == 1)
243 return handle_aiocb_rw_linear(aiocb, aiocb->aio_iov->iov_base);
246 * We have more than one iovec, and all are properly aligned.
248 * Try preadv/pwritev first and fall back to linearizing the
249 * buffer if it's not supported.
251 if (preadv_present) {
252 nbytes = handle_aiocb_rw_vector(aiocb);
253 if (nbytes == aiocb->aio_nbytes)
254 return nbytes;
255 if (nbytes < 0 && nbytes != -ENOSYS)
256 return nbytes;
257 preadv_present = 0;
261 * XXX(hch): short read/write. no easy way to handle the reminder
262 * using these interfaces. For now retry using plain
263 * pread/pwrite?
268 * Ok, we have to do it the hard way, copy all segments into
269 * a single aligned buffer.
271 buf = qemu_memalign(512, aiocb->aio_nbytes);
272 if (aiocb->aio_type & QEMU_AIO_WRITE) {
273 char *p = buf;
274 int i;
276 for (i = 0; i < aiocb->aio_niov; ++i) {
277 memcpy(p, aiocb->aio_iov[i].iov_base, aiocb->aio_iov[i].iov_len);
278 p += aiocb->aio_iov[i].iov_len;
282 nbytes = handle_aiocb_rw_linear(aiocb, buf);
283 if (!(aiocb->aio_type & QEMU_AIO_WRITE)) {
284 char *p = buf;
285 size_t count = aiocb->aio_nbytes, copy;
286 int i;
288 for (i = 0; i < aiocb->aio_niov && count; ++i) {
289 copy = count;
290 if (copy > aiocb->aio_iov[i].iov_len)
291 copy = aiocb->aio_iov[i].iov_len;
292 memcpy(aiocb->aio_iov[i].iov_base, p, copy);
293 p += copy;
294 count -= copy;
297 qemu_vfree(buf);
299 return nbytes;
302 static void *aio_thread(void *unused)
304 pid_t pid;
306 pid = getpid();
308 while (1) {
309 struct qemu_paiocb *aiocb;
310 size_t ret = 0;
311 qemu_timeval tv;
312 struct timespec ts;
314 qemu_gettimeofday(&tv);
315 ts.tv_sec = tv.tv_sec + 10;
316 ts.tv_nsec = 0;
318 mutex_lock(&lock);
320 while (QTAILQ_EMPTY(&request_list) &&
321 !(ret == ETIMEDOUT)) {
322 ret = cond_timedwait(&cond, &lock, &ts);
325 if (QTAILQ_EMPTY(&request_list))
326 break;
328 aiocb = QTAILQ_FIRST(&request_list);
329 QTAILQ_REMOVE(&request_list, aiocb, node);
330 aiocb->active = 1;
331 idle_threads--;
332 mutex_unlock(&lock);
334 switch (aiocb->aio_type & QEMU_AIO_TYPE_MASK) {
335 case QEMU_AIO_READ:
336 case QEMU_AIO_WRITE:
337 ret = handle_aiocb_rw(aiocb);
338 break;
339 case QEMU_AIO_FLUSH:
340 ret = handle_aiocb_flush(aiocb);
341 break;
342 case QEMU_AIO_IOCTL:
343 ret = handle_aiocb_ioctl(aiocb);
344 break;
345 default:
346 fprintf(stderr, "invalid aio request (0x%x)\n", aiocb->aio_type);
347 ret = -EINVAL;
348 break;
351 mutex_lock(&lock);
352 aiocb->ret = ret;
353 idle_threads++;
354 mutex_unlock(&lock);
356 if (kill(pid, aiocb->ev_signo)) die("kill failed");
359 idle_threads--;
360 cur_threads--;
361 mutex_unlock(&lock);
363 return NULL;
366 static void spawn_thread(void)
368 sigset_t set, oldset;
370 cur_threads++;
371 idle_threads++;
373 /* block all signals */
374 if (sigfillset(&set)) die("sigfillset");
375 if (sigprocmask(SIG_SETMASK, &set, &oldset)) die("sigprocmask");
377 thread_create(&thread_id, &attr, aio_thread, NULL);
379 if (sigprocmask(SIG_SETMASK, &oldset, NULL)) die("sigprocmask restore");
382 static void qemu_paio_submit(struct qemu_paiocb *aiocb)
384 aiocb->ret = -EINPROGRESS;
385 aiocb->active = 0;
386 mutex_lock(&lock);
387 if (idle_threads == 0 && cur_threads < max_threads)
388 spawn_thread();
389 QTAILQ_INSERT_TAIL(&request_list, aiocb, node);
390 mutex_unlock(&lock);
391 cond_signal(&cond);
394 static ssize_t qemu_paio_return(struct qemu_paiocb *aiocb)
396 ssize_t ret;
398 mutex_lock(&lock);
399 ret = aiocb->ret;
400 mutex_unlock(&lock);
402 return ret;
405 static int qemu_paio_error(struct qemu_paiocb *aiocb)
407 ssize_t ret = qemu_paio_return(aiocb);
409 if (ret < 0)
410 ret = -ret;
411 else
412 ret = 0;
414 return ret;
417 static void posix_aio_read(void *opaque)
419 PosixAioState *s = opaque;
420 struct qemu_paiocb *acb, **pacb;
421 int ret;
422 union {
423 struct qemu_signalfd_siginfo siginfo;
424 char buf[128];
425 } sig;
426 size_t offset;
428 /* try to read from signalfd, don't freak out if we can't read anything */
429 offset = 0;
430 while (offset < 128) {
431 ssize_t len;
433 len = read(s->fd, sig.buf + offset, 128 - offset);
434 if (len == -1 && errno == EINTR)
435 continue;
436 if (len == -1 && errno == EAGAIN) {
437 /* there is no natural reason for this to happen,
438 * so we'll spin hard until we get everything just
439 * to be on the safe side. */
440 if (offset > 0)
441 continue;
444 offset += len;
447 for(;;) {
448 pacb = &s->first_aio;
449 for(;;) {
450 acb = *pacb;
451 if (!acb)
452 goto the_end;
453 ret = qemu_paio_error(acb);
454 if (ret == ECANCELED) {
455 /* remove the request */
456 *pacb = acb->next;
457 qemu_aio_release(acb);
458 } else if (ret != EINPROGRESS) {
459 /* end of aio */
460 if (ret == 0) {
461 ret = qemu_paio_return(acb);
462 if (ret == acb->aio_nbytes)
463 ret = 0;
464 else
465 ret = -EINVAL;
466 } else {
467 ret = -ret;
469 /* remove the request */
470 *pacb = acb->next;
471 /* call the callback */
472 acb->common.cb(acb->common.opaque, ret);
473 qemu_aio_release(acb);
474 break;
475 } else {
476 pacb = &acb->next;
480 the_end: ;
483 static int posix_aio_flush(void *opaque)
485 PosixAioState *s = opaque;
486 return !!s->first_aio;
489 static PosixAioState *posix_aio_state;
491 static void paio_remove(struct qemu_paiocb *acb)
493 struct qemu_paiocb **pacb;
495 /* remove the callback from the queue */
496 pacb = &posix_aio_state->first_aio;
497 for(;;) {
498 if (*pacb == NULL) {
499 fprintf(stderr, "paio_remove: aio request not found!\n");
500 break;
501 } else if (*pacb == acb) {
502 *pacb = acb->next;
503 qemu_aio_release(acb);
504 break;
506 pacb = &(*pacb)->next;
510 static void paio_cancel(BlockDriverAIOCB *blockacb)
512 struct qemu_paiocb *acb = (struct qemu_paiocb *)blockacb;
513 int active = 0;
515 mutex_lock(&lock);
516 if (!acb->active) {
517 QTAILQ_REMOVE(&request_list, acb, node);
518 acb->ret = -ECANCELED;
519 } else if (acb->ret == -EINPROGRESS) {
520 active = 1;
522 mutex_unlock(&lock);
524 if (active) {
525 /* fail safe: if the aio could not be canceled, we wait for
526 it */
527 while (qemu_paio_error(acb) == EINPROGRESS)
531 paio_remove(acb);
534 static AIOPool raw_aio_pool = {
535 .aiocb_size = sizeof(struct qemu_paiocb),
536 .cancel = paio_cancel,
539 BlockDriverAIOCB *paio_submit(BlockDriverState *bs, void *aio_ctx, int fd,
540 int64_t sector_num, QEMUIOVector *qiov, int nb_sectors,
541 BlockDriverCompletionFunc *cb, void *opaque, int type)
543 struct qemu_paiocb *acb;
545 acb = qemu_aio_get(&raw_aio_pool, bs, cb, opaque);
546 if (!acb)
547 return NULL;
548 acb->aio_type = type;
549 acb->aio_fildes = fd;
550 acb->ev_signo = SIGUSR2;
551 if (qiov) {
552 acb->aio_iov = qiov->iov;
553 acb->aio_niov = qiov->niov;
555 acb->aio_nbytes = nb_sectors * 512;
556 acb->aio_offset = sector_num * 512;
558 acb->next = posix_aio_state->first_aio;
559 posix_aio_state->first_aio = acb;
561 qemu_paio_submit(acb);
562 return &acb->common;
565 BlockDriverAIOCB *paio_ioctl(BlockDriverState *bs, int fd,
566 unsigned long int req, void *buf,
567 BlockDriverCompletionFunc *cb, void *opaque)
569 struct qemu_paiocb *acb;
571 acb = qemu_aio_get(&raw_aio_pool, bs, cb, opaque);
572 if (!acb)
573 return NULL;
574 acb->aio_type = QEMU_AIO_IOCTL;
575 acb->aio_fildes = fd;
576 acb->ev_signo = SIGUSR2;
577 acb->aio_offset = 0;
578 acb->aio_ioctl_buf = buf;
579 acb->aio_ioctl_cmd = req;
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 void *paio_init(void)
590 sigset_t mask;
591 PosixAioState *s;
592 int ret;
594 if (posix_aio_state)
595 return posix_aio_state;
597 s = qemu_malloc(sizeof(PosixAioState));
599 /* Make sure to block AIO signal */
600 sigemptyset(&mask);
601 sigaddset(&mask, SIGUSR2);
602 sigprocmask(SIG_BLOCK, &mask, NULL);
604 s->first_aio = NULL;
605 s->fd = qemu_signalfd(&mask);
606 if (s->fd == -1) {
607 fprintf(stderr, "failed to create signalfd\n");
608 return NULL;
611 fcntl(s->fd, F_SETFL, O_NONBLOCK);
613 qemu_aio_set_fd_handler(s->fd, posix_aio_read, NULL, posix_aio_flush, s);
615 ret = pthread_attr_init(&attr);
616 if (ret)
617 die2(ret, "pthread_attr_init");
619 ret = pthread_attr_setdetachstate(&attr, PTHREAD_CREATE_DETACHED);
620 if (ret)
621 die2(ret, "pthread_attr_setdetachstate");
623 QTAILQ_INIT(&request_list);
625 posix_aio_state = s;
627 return posix_aio_state;