test: Fix compilation rule for assembly files wrt autodependencies
[qemu-kvm/amd-iommu.git] / posix-aio-compat.c
blobca528766a7f57c9dd14967489595b940620da68b
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;
305 sigset_t set;
307 pid = getpid();
309 /* block all signals */
310 if (sigfillset(&set)) die("sigfillset");
311 if (sigprocmask(SIG_BLOCK, &set, NULL)) die("sigprocmask");
313 while (1) {
314 struct qemu_paiocb *aiocb;
315 size_t ret = 0;
316 qemu_timeval tv;
317 struct timespec ts;
319 qemu_gettimeofday(&tv);
320 ts.tv_sec = tv.tv_sec + 10;
321 ts.tv_nsec = 0;
323 mutex_lock(&lock);
325 while (QTAILQ_EMPTY(&request_list) &&
326 !(ret == ETIMEDOUT)) {
327 ret = cond_timedwait(&cond, &lock, &ts);
330 if (QTAILQ_EMPTY(&request_list))
331 break;
333 aiocb = QTAILQ_FIRST(&request_list);
334 QTAILQ_REMOVE(&request_list, aiocb, node);
335 aiocb->active = 1;
336 idle_threads--;
337 mutex_unlock(&lock);
339 switch (aiocb->aio_type & QEMU_AIO_TYPE_MASK) {
340 case QEMU_AIO_READ:
341 case QEMU_AIO_WRITE:
342 ret = handle_aiocb_rw(aiocb);
343 break;
344 case QEMU_AIO_FLUSH:
345 ret = handle_aiocb_flush(aiocb);
346 break;
347 case QEMU_AIO_IOCTL:
348 ret = handle_aiocb_ioctl(aiocb);
349 break;
350 default:
351 fprintf(stderr, "invalid aio request (0x%x)\n", aiocb->aio_type);
352 ret = -EINVAL;
353 break;
356 mutex_lock(&lock);
357 aiocb->ret = ret;
358 idle_threads++;
359 mutex_unlock(&lock);
361 if (kill(pid, aiocb->ev_signo)) die("kill failed");
364 idle_threads--;
365 cur_threads--;
366 mutex_unlock(&lock);
368 return NULL;
371 static void spawn_thread(void)
373 cur_threads++;
374 idle_threads++;
375 thread_create(&thread_id, &attr, aio_thread, NULL);
378 static void qemu_paio_submit(struct qemu_paiocb *aiocb)
380 aiocb->ret = -EINPROGRESS;
381 aiocb->active = 0;
382 mutex_lock(&lock);
383 if (idle_threads == 0 && cur_threads < max_threads)
384 spawn_thread();
385 QTAILQ_INSERT_TAIL(&request_list, aiocb, node);
386 mutex_unlock(&lock);
387 cond_signal(&cond);
390 static ssize_t qemu_paio_return(struct qemu_paiocb *aiocb)
392 ssize_t ret;
394 mutex_lock(&lock);
395 ret = aiocb->ret;
396 mutex_unlock(&lock);
398 return ret;
401 static int qemu_paio_error(struct qemu_paiocb *aiocb)
403 ssize_t ret = qemu_paio_return(aiocb);
405 if (ret < 0)
406 ret = -ret;
407 else
408 ret = 0;
410 return ret;
413 static void posix_aio_read(void *opaque)
415 PosixAioState *s = opaque;
416 struct qemu_paiocb *acb, **pacb;
417 int ret;
418 union {
419 struct qemu_signalfd_siginfo siginfo;
420 char buf[128];
421 } sig;
422 size_t offset;
424 /* try to read from signalfd, don't freak out if we can't read anything */
425 offset = 0;
426 while (offset < 128) {
427 ssize_t len;
429 len = read(s->fd, sig.buf + offset, 128 - offset);
430 if (len == -1 && errno == EINTR)
431 continue;
432 if (len == -1 && errno == EAGAIN) {
433 /* there is no natural reason for this to happen,
434 * so we'll spin hard until we get everything just
435 * to be on the safe side. */
436 if (offset > 0)
437 continue;
440 offset += len;
443 for(;;) {
444 pacb = &s->first_aio;
445 for(;;) {
446 acb = *pacb;
447 if (!acb)
448 goto the_end;
449 ret = qemu_paio_error(acb);
450 if (ret == ECANCELED) {
451 /* remove the request */
452 *pacb = acb->next;
453 qemu_aio_release(acb);
454 } else if (ret != EINPROGRESS) {
455 /* end of aio */
456 if (ret == 0) {
457 ret = qemu_paio_return(acb);
458 if (ret == acb->aio_nbytes)
459 ret = 0;
460 else
461 ret = -EINVAL;
462 } else {
463 ret = -ret;
465 /* remove the request */
466 *pacb = acb->next;
467 /* call the callback */
468 acb->common.cb(acb->common.opaque, ret);
469 qemu_aio_release(acb);
470 break;
471 } else {
472 pacb = &acb->next;
476 the_end: ;
479 static int posix_aio_flush(void *opaque)
481 PosixAioState *s = opaque;
482 return !!s->first_aio;
485 static PosixAioState *posix_aio_state;
487 static void paio_remove(struct qemu_paiocb *acb)
489 struct qemu_paiocb **pacb;
491 /* remove the callback from the queue */
492 pacb = &posix_aio_state->first_aio;
493 for(;;) {
494 if (*pacb == NULL) {
495 fprintf(stderr, "paio_remove: aio request not found!\n");
496 break;
497 } else if (*pacb == acb) {
498 *pacb = acb->next;
499 qemu_aio_release(acb);
500 break;
502 pacb = &(*pacb)->next;
506 static void paio_cancel(BlockDriverAIOCB *blockacb)
508 struct qemu_paiocb *acb = (struct qemu_paiocb *)blockacb;
509 int active = 0;
511 mutex_lock(&lock);
512 if (!acb->active) {
513 QTAILQ_REMOVE(&request_list, acb, node);
514 acb->ret = -ECANCELED;
515 } else if (acb->ret == -EINPROGRESS) {
516 active = 1;
518 mutex_unlock(&lock);
520 if (active) {
521 /* fail safe: if the aio could not be canceled, we wait for
522 it */
523 while (qemu_paio_error(acb) == EINPROGRESS)
527 paio_remove(acb);
530 static AIOPool raw_aio_pool = {
531 .aiocb_size = sizeof(struct qemu_paiocb),
532 .cancel = paio_cancel,
535 BlockDriverAIOCB *paio_submit(BlockDriverState *bs, void *aio_ctx, int fd,
536 int64_t sector_num, QEMUIOVector *qiov, int nb_sectors,
537 BlockDriverCompletionFunc *cb, void *opaque, int type)
539 struct qemu_paiocb *acb;
541 acb = qemu_aio_get(&raw_aio_pool, bs, cb, opaque);
542 if (!acb)
543 return NULL;
544 acb->aio_type = type;
545 acb->aio_fildes = fd;
546 acb->ev_signo = SIGUSR2;
547 if (qiov) {
548 acb->aio_iov = qiov->iov;
549 acb->aio_niov = qiov->niov;
551 acb->aio_nbytes = nb_sectors * 512;
552 acb->aio_offset = sector_num * 512;
554 acb->next = posix_aio_state->first_aio;
555 posix_aio_state->first_aio = acb;
557 qemu_paio_submit(acb);
558 return &acb->common;
561 BlockDriverAIOCB *paio_ioctl(BlockDriverState *bs, int fd,
562 unsigned long int req, void *buf,
563 BlockDriverCompletionFunc *cb, void *opaque)
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 = QEMU_AIO_IOCTL;
571 acb->aio_fildes = fd;
572 acb->ev_signo = SIGUSR2;
573 acb->aio_offset = 0;
574 acb->aio_ioctl_buf = buf;
575 acb->aio_ioctl_cmd = req;
577 acb->next = posix_aio_state->first_aio;
578 posix_aio_state->first_aio = acb;
580 qemu_paio_submit(acb);
581 return &acb->common;
584 void *paio_init(void)
586 sigset_t mask;
587 PosixAioState *s;
588 int ret;
590 if (posix_aio_state)
591 return posix_aio_state;
593 s = qemu_malloc(sizeof(PosixAioState));
595 /* Make sure to block AIO signal */
596 sigemptyset(&mask);
597 sigaddset(&mask, SIGUSR2);
598 sigprocmask(SIG_BLOCK, &mask, NULL);
600 s->first_aio = NULL;
601 s->fd = qemu_signalfd(&mask);
602 if (s->fd == -1) {
603 fprintf(stderr, "failed to create signalfd\n");
604 return NULL;
607 fcntl(s->fd, F_SETFL, O_NONBLOCK);
609 qemu_aio_set_fd_handler(s->fd, posix_aio_read, NULL, posix_aio_flush, s);
611 ret = pthread_attr_init(&attr);
612 if (ret)
613 die2(ret, "pthread_attr_init");
615 ret = pthread_attr_setdetachstate(&attr, PTHREAD_CREATE_DETACHED);
616 if (ret)
617 die2(ret, "pthread_attr_setdetachstate");
619 QTAILQ_INIT(&request_list);
621 posix_aio_state = s;
623 return posix_aio_state;