]> git.proxmox.com Git - qemu.git/blame - posix-aio-compat.c
Use glib memory allocation and free functions
[qemu.git] / posix-aio-compat.c
CommitLineData
3c529d93
AL
1/*
2 * QEMU posix-aio emulation
3 *
4 * Copyright IBM, Corp. 2008
5 *
6 * Authors:
7 * Anthony Liguori <aliguori@us.ibm.com>
8 *
9 * This work is licensed under the terms of the GNU GPL, version 2. See
10 * the COPYING file in the top-level directory.
11 *
12 */
13
221f715d 14#include <sys/ioctl.h>
9ef91a67 15#include <sys/types.h>
3c529d93
AL
16#include <pthread.h>
17#include <unistd.h>
18#include <errno.h>
30525aff 19#include <time.h>
8653c015 20#include <string.h>
21#include <stdlib.h>
22#include <stdio.h>
9ef91a67 23
72cf2d4f 24#include "qemu-queue.h"
3c529d93 25#include "osdep.h"
dc786bc9 26#include "sysemu.h"
f141eafe 27#include "qemu-common.h"
6d519a5f 28#include "trace.h"
9ef91a67
CH
29#include "block_int.h"
30
31#include "block/raw-posix-aio.h"
32
33
34struct qemu_paiocb {
35 BlockDriverAIOCB common;
36 int aio_fildes;
37 union {
38 struct iovec *aio_iov;
b587a52c 39 void *aio_ioctl_buf;
9ef91a67
CH
40 };
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;
46
72cf2d4f 47 QTAILQ_ENTRY(qemu_paiocb) node;
9ef91a67
CH
48 int aio_type;
49 ssize_t ret;
50 int active;
51 struct qemu_paiocb *next;
52};
53
54typedef struct PosixAioState {
55 int rfd, wfd;
56 struct qemu_paiocb *first_aio;
57} PosixAioState;
3c529d93 58
3c529d93
AL
59
60static pthread_mutex_t lock = PTHREAD_MUTEX_INITIALIZER;
61static pthread_cond_t cond = PTHREAD_COND_INITIALIZER;
62static pthread_t thread_id;
a8227a5a 63static pthread_attr_t attr;
3c529d93
AL
64static int max_threads = 64;
65static int cur_threads = 0;
66static int idle_threads = 0;
72cf2d4f 67static QTAILQ_HEAD(, qemu_paiocb) request_list;
3c529d93 68
2341f9a1 69#ifdef CONFIG_PREADV
ceb42de8
AL
70static int preadv_present = 1;
71#else
72static int preadv_present = 0;
73#endif
74
8653c015 75static void die2(int err, const char *what)
76{
77 fprintf(stderr, "%s failed: %s\n", what, strerror(err));
78 abort();
79}
80
81static void die(const char *what)
82{
83 die2(errno, what);
84}
85
86static void mutex_lock(pthread_mutex_t *mutex)
87{
88 int ret = pthread_mutex_lock(mutex);
89 if (ret) die2(ret, "pthread_mutex_lock");
90}
91
92static void mutex_unlock(pthread_mutex_t *mutex)
93{
94 int ret = pthread_mutex_unlock(mutex);
95 if (ret) die2(ret, "pthread_mutex_unlock");
96}
97
98static int cond_timedwait(pthread_cond_t *cond, pthread_mutex_t *mutex,
99 struct timespec *ts)
100{
101 int ret = pthread_cond_timedwait(cond, mutex, ts);
102 if (ret && ret != ETIMEDOUT) die2(ret, "pthread_cond_timedwait");
103 return ret;
104}
105
5d47e372 106static void cond_signal(pthread_cond_t *cond)
8653c015 107{
5d47e372 108 int ret = pthread_cond_signal(cond);
109 if (ret) die2(ret, "pthread_cond_signal");
8653c015 110}
111
112static void thread_create(pthread_t *thread, pthread_attr_t *attr,
113 void *(*start_routine)(void*), void *arg)
114{
115 int ret = pthread_create(thread, attr, start_routine, arg);
116 if (ret) die2(ret, "pthread_create");
117}
118
6769da29 119static ssize_t handle_aiocb_ioctl(struct qemu_paiocb *aiocb)
f141eafe 120{
b587a52c
SH
121 int ret;
122
123 ret = ioctl(aiocb->aio_fildes, aiocb->aio_ioctl_cmd, aiocb->aio_ioctl_buf);
124 if (ret == -1)
125 return -errno;
126
127 /*
128 * This looks weird, but the aio code only consideres a request
b0cd712c 129 * successful if it has written the number full number of bytes.
b587a52c
SH
130 *
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..
134 */
135 return aiocb->aio_nbytes;
f141eafe
AL
136}
137
6769da29 138static ssize_t handle_aiocb_flush(struct qemu_paiocb *aiocb)
b2e12bc6
CH
139{
140 int ret;
141
47faadc6 142 ret = qemu_fdatasync(aiocb->aio_fildes);
b2e12bc6
CH
143 if (ret == -1)
144 return -errno;
145 return 0;
146}
147
2341f9a1 148#ifdef CONFIG_PREADV
ceb42de8
AL
149
150static ssize_t
151qemu_preadv(int fd, const struct iovec *iov, int nr_iov, off_t offset)
152{
153 return preadv(fd, iov, nr_iov, offset);
154}
155
156static ssize_t
157qemu_pwritev(int fd, const struct iovec *iov, int nr_iov, off_t offset)
158{
159 return pwritev(fd, iov, nr_iov, offset);
160}
161
162#else
163
164static ssize_t
165qemu_preadv(int fd, const struct iovec *iov, int nr_iov, off_t offset)
166{
167 return -ENOSYS;
168}
169
170static ssize_t
171qemu_pwritev(int fd, const struct iovec *iov, int nr_iov, off_t offset)
172{
173 return -ENOSYS;
174}
175
176#endif
177
6769da29 178static ssize_t handle_aiocb_rw_vector(struct qemu_paiocb *aiocb)
ceb42de8
AL
179{
180 size_t offset = 0;
181 ssize_t len;
182
183 do {
9ef91a67 184 if (aiocb->aio_type & QEMU_AIO_WRITE)
ceb42de8
AL
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);
195
196 if (len == -1)
197 return -errno;
198 return len;
199}
200
ba1d1afd
KW
201/*
202 * Read/writes the data to/from a given linear buffer.
203 *
204 * Returns the number of bytes handles or -errno in case of an error. Short
205 * reads are only returned if the end of the file is reached.
206 */
6769da29 207static ssize_t handle_aiocb_rw_linear(struct qemu_paiocb *aiocb, char *buf)
221f715d 208{
6769da29
KW
209 ssize_t offset = 0;
210 ssize_t len;
221f715d
AL
211
212 while (offset < aiocb->aio_nbytes) {
9ef91a67 213 if (aiocb->aio_type & QEMU_AIO_WRITE)
f141eafe
AL
214 len = pwrite(aiocb->aio_fildes,
215 (const char *)buf + offset,
216 aiocb->aio_nbytes - offset,
217 aiocb->aio_offset + offset);
218 else
219 len = pread(aiocb->aio_fildes,
220 buf + offset,
221f715d
AL
221 aiocb->aio_nbytes - offset,
222 aiocb->aio_offset + offset);
221f715d 223
f141eafe
AL
224 if (len == -1 && errno == EINTR)
225 continue;
226 else if (len == -1) {
227 offset = -errno;
228 break;
229 } else if (len == 0)
230 break;
231
232 offset += len;
221f715d
AL
233 }
234
235 return offset;
236}
237
6769da29 238static ssize_t handle_aiocb_rw(struct qemu_paiocb *aiocb)
221f715d 239{
6769da29 240 ssize_t nbytes;
f141eafe
AL
241 char *buf;
242
9ef91a67 243 if (!(aiocb->aio_type & QEMU_AIO_MISALIGNED)) {
f141eafe
AL
244 /*
245 * If there is just a single buffer, and it is properly aligned
246 * we can just use plain pread/pwrite without any problems.
247 */
ceb42de8
AL
248 if (aiocb->aio_niov == 1)
249 return handle_aiocb_rw_linear(aiocb, aiocb->aio_iov->iov_base);
250
251 /*
252 * We have more than one iovec, and all are properly aligned.
253 *
254 * Try preadv/pwritev first and fall back to linearizing the
255 * buffer if it's not supported.
256 */
b587a52c 257 if (preadv_present) {
ceb42de8
AL
258 nbytes = handle_aiocb_rw_vector(aiocb);
259 if (nbytes == aiocb->aio_nbytes)
b587a52c 260 return nbytes;
ceb42de8
AL
261 if (nbytes < 0 && nbytes != -ENOSYS)
262 return nbytes;
263 preadv_present = 0;
264 }
265
266 /*
267 * XXX(hch): short read/write. no easy way to handle the reminder
268 * using these interfaces. For now retry using plain
269 * pread/pwrite?
270 */
f141eafe 271 }
221f715d 272
f141eafe
AL
273 /*
274 * Ok, we have to do it the hard way, copy all segments into
275 * a single aligned buffer.
276 */
72aef731 277 buf = qemu_blockalign(aiocb->common.bs, aiocb->aio_nbytes);
9ef91a67 278 if (aiocb->aio_type & QEMU_AIO_WRITE) {
f141eafe
AL
279 char *p = buf;
280 int i;
281
282 for (i = 0; i < aiocb->aio_niov; ++i) {
283 memcpy(p, aiocb->aio_iov[i].iov_base, aiocb->aio_iov[i].iov_len);
284 p += aiocb->aio_iov[i].iov_len;
285 }
286 }
287
288 nbytes = handle_aiocb_rw_linear(aiocb, buf);
9ef91a67 289 if (!(aiocb->aio_type & QEMU_AIO_WRITE)) {
f141eafe
AL
290 char *p = buf;
291 size_t count = aiocb->aio_nbytes, copy;
292 int i;
293
294 for (i = 0; i < aiocb->aio_niov && count; ++i) {
295 copy = count;
296 if (copy > aiocb->aio_iov[i].iov_len)
297 copy = aiocb->aio_iov[i].iov_len;
298 memcpy(aiocb->aio_iov[i].iov_base, p, copy);
299 p += copy;
300 count -= copy;
301 }
302 }
303 qemu_vfree(buf);
304
305 return nbytes;
221f715d
AL
306}
307
3c529d93
AL
308static void *aio_thread(void *unused)
309{
a8227a5a 310 pid_t pid;
3c529d93 311
a8227a5a 312 pid = getpid();
313
3c529d93
AL
314 while (1) {
315 struct qemu_paiocb *aiocb;
6769da29 316 ssize_t ret = 0;
30525aff 317 qemu_timeval tv;
318 struct timespec ts;
319
320 qemu_gettimeofday(&tv);
321 ts.tv_sec = tv.tv_sec + 10;
322 ts.tv_nsec = 0;
3c529d93 323
8653c015 324 mutex_lock(&lock);
3c529d93 325
72cf2d4f 326 while (QTAILQ_EMPTY(&request_list) &&
3c529d93 327 !(ret == ETIMEDOUT)) {
5be4aab7 328 idle_threads++;
8653c015 329 ret = cond_timedwait(&cond, &lock, &ts);
5be4aab7 330 idle_threads--;
3c529d93
AL
331 }
332
72cf2d4f 333 if (QTAILQ_EMPTY(&request_list))
3c529d93
AL
334 break;
335
72cf2d4f
BS
336 aiocb = QTAILQ_FIRST(&request_list);
337 QTAILQ_REMOVE(&request_list, aiocb, node);
3c529d93 338 aiocb->active = 1;
8653c015 339 mutex_unlock(&lock);
3c529d93 340
9ef91a67
CH
341 switch (aiocb->aio_type & QEMU_AIO_TYPE_MASK) {
342 case QEMU_AIO_READ:
ba1d1afd
KW
343 ret = handle_aiocb_rw(aiocb);
344 if (ret >= 0 && ret < aiocb->aio_nbytes && aiocb->common.bs->growable) {
345 /* A short read means that we have reached EOF. Pad the buffer
346 * with zeros for bytes after EOF. */
347 QEMUIOVector qiov;
348
349 qemu_iovec_init_external(&qiov, aiocb->aio_iov,
350 aiocb->aio_niov);
351 qemu_iovec_memset_skip(&qiov, 0, aiocb->aio_nbytes - ret, ret);
352
353 ret = aiocb->aio_nbytes;
354 }
355 break;
9ef91a67 356 case QEMU_AIO_WRITE:
b587a52c
SH
357 ret = handle_aiocb_rw(aiocb);
358 break;
b2e12bc6 359 case QEMU_AIO_FLUSH:
b587a52c
SH
360 ret = handle_aiocb_flush(aiocb);
361 break;
9ef91a67 362 case QEMU_AIO_IOCTL:
b587a52c
SH
363 ret = handle_aiocb_ioctl(aiocb);
364 break;
365 default:
366 fprintf(stderr, "invalid aio request (0x%x)\n", aiocb->aio_type);
367 ret = -EINVAL;
368 break;
369 }
3c529d93 370
8653c015 371 mutex_lock(&lock);
221f715d 372 aiocb->ret = ret;
8653c015 373 mutex_unlock(&lock);
3c529d93 374
a8227a5a 375 if (kill(pid, aiocb->ev_signo)) die("kill failed");
3c529d93
AL
376 }
377
3c529d93 378 cur_threads--;
8653c015 379 mutex_unlock(&lock);
3c529d93
AL
380
381 return NULL;
382}
383
8653c015 384static void spawn_thread(void)
3c529d93 385{
ee399306 386 sigset_t set, oldset;
387
3c529d93 388 cur_threads++;
ee399306 389
390 /* block all signals */
391 if (sigfillset(&set)) die("sigfillset");
392 if (sigprocmask(SIG_SETMASK, &set, &oldset)) die("sigprocmask");
393
8653c015 394 thread_create(&thread_id, &attr, aio_thread, NULL);
ee399306 395
396 if (sigprocmask(SIG_SETMASK, &oldset, NULL)) die("sigprocmask restore");
3c529d93
AL
397}
398
9ef91a67 399static void qemu_paio_submit(struct qemu_paiocb *aiocb)
3c529d93 400{
3c529d93
AL
401 aiocb->ret = -EINPROGRESS;
402 aiocb->active = 0;
8653c015 403 mutex_lock(&lock);
3c529d93
AL
404 if (idle_threads == 0 && cur_threads < max_threads)
405 spawn_thread();
72cf2d4f 406 QTAILQ_INSERT_TAIL(&request_list, aiocb, node);
8653c015 407 mutex_unlock(&lock);
5d47e372 408 cond_signal(&cond);
3c529d93
AL
409}
410
9ef91a67 411static ssize_t qemu_paio_return(struct qemu_paiocb *aiocb)
3c529d93
AL
412{
413 ssize_t ret;
414
8653c015 415 mutex_lock(&lock);
3c529d93 416 ret = aiocb->ret;
8653c015 417 mutex_unlock(&lock);
3c529d93
AL
418
419 return ret;
420}
421
9ef91a67 422static int qemu_paio_error(struct qemu_paiocb *aiocb)
3c529d93
AL
423{
424 ssize_t ret = qemu_paio_return(aiocb);
425
426 if (ret < 0)
427 ret = -ret;
428 else
429 ret = 0;
430
431 return ret;
432}
433
59c7b155 434static int posix_aio_process_queue(void *opaque)
3c529d93 435{
9ef91a67
CH
436 PosixAioState *s = opaque;
437 struct qemu_paiocb *acb, **pacb;
3c529d93 438 int ret;
59c7b155 439 int result = 0;
9ef91a67
CH
440
441 for(;;) {
442 pacb = &s->first_aio;
443 for(;;) {
444 acb = *pacb;
445 if (!acb)
59c7b155 446 return result;
e5f37649 447
9ef91a67
CH
448 ret = qemu_paio_error(acb);
449 if (ret == ECANCELED) {
450 /* remove the request */
451 *pacb = acb->next;
452 qemu_aio_release(acb);
59c7b155 453 result = 1;
9ef91a67
CH
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;
464 }
ddca9fb2
SH
465
466 trace_paio_complete(acb, acb->common.opaque, ret);
467
9ef91a67
CH
468 /* remove the request */
469 *pacb = acb->next;
470 /* call the callback */
471 acb->common.cb(acb->common.opaque, ret);
472 qemu_aio_release(acb);
59c7b155 473 result = 1;
9ef91a67
CH
474 break;
475 } else {
476 pacb = &acb->next;
477 }
478 }
479 }
59c7b155
KW
480
481 return result;
482}
483
484static void posix_aio_read(void *opaque)
485{
486 PosixAioState *s = opaque;
487 ssize_t len;
488
489 /* read all bytes from signal pipe */
490 for (;;) {
491 char bytes[16];
492
493 len = read(s->rfd, bytes, sizeof(bytes));
494 if (len == -1 && errno == EINTR)
495 continue; /* try again */
496 if (len == sizeof(bytes))
497 continue; /* more to read */
498 break;
499 }
500
501 posix_aio_process_queue(s);
9ef91a67
CH
502}
503
504static int posix_aio_flush(void *opaque)
505{
506 PosixAioState *s = opaque;
507 return !!s->first_aio;
508}
509
510static PosixAioState *posix_aio_state;
511
512static void aio_signal_handler(int signum)
513{
514 if (posix_aio_state) {
515 char byte = 0;
4817d327 516 ssize_t ret;
9ef91a67 517
4817d327
KS
518 ret = write(posix_aio_state->wfd, &byte, sizeof(byte));
519 if (ret < 0 && errno != EAGAIN)
520 die("write()");
9ef91a67
CH
521 }
522
523 qemu_service_io();
524}
525
526static void paio_remove(struct qemu_paiocb *acb)
527{
528 struct qemu_paiocb **pacb;
529
530 /* remove the callback from the queue */
531 pacb = &posix_aio_state->first_aio;
532 for(;;) {
533 if (*pacb == NULL) {
534 fprintf(stderr, "paio_remove: aio request not found!\n");
535 break;
536 } else if (*pacb == acb) {
537 *pacb = acb->next;
538 qemu_aio_release(acb);
539 break;
540 }
541 pacb = &(*pacb)->next;
542 }
543}
544
545static void paio_cancel(BlockDriverAIOCB *blockacb)
546{
547 struct qemu_paiocb *acb = (struct qemu_paiocb *)blockacb;
548 int active = 0;
3c529d93 549
ddca9fb2
SH
550 trace_paio_cancel(acb, acb->common.opaque);
551
8653c015 552 mutex_lock(&lock);
9ef91a67 553 if (!acb->active) {
72cf2d4f 554 QTAILQ_REMOVE(&request_list, acb, node);
9ef91a67
CH
555 acb->ret = -ECANCELED;
556 } else if (acb->ret == -EINPROGRESS) {
557 active = 1;
558 }
8653c015 559 mutex_unlock(&lock);
3c529d93 560
9ef91a67
CH
561 if (active) {
562 /* fail safe: if the aio could not be canceled, we wait for
563 it */
564 while (qemu_paio_error(acb) == EINPROGRESS)
565 ;
566 }
567
568 paio_remove(acb);
569}
570
571static AIOPool raw_aio_pool = {
572 .aiocb_size = sizeof(struct qemu_paiocb),
573 .cancel = paio_cancel,
574};
575
1e5b9d2f 576BlockDriverAIOCB *paio_submit(BlockDriverState *bs, int fd,
9ef91a67
CH
577 int64_t sector_num, QEMUIOVector *qiov, int nb_sectors,
578 BlockDriverCompletionFunc *cb, void *opaque, int type)
579{
580 struct qemu_paiocb *acb;
581
582 acb = qemu_aio_get(&raw_aio_pool, bs, cb, opaque);
583 if (!acb)
584 return NULL;
585 acb->aio_type = type;
586 acb->aio_fildes = fd;
587 acb->ev_signo = SIGUSR2;
e5f37649 588
b2e12bc6
CH
589 if (qiov) {
590 acb->aio_iov = qiov->iov;
591 acb->aio_niov = qiov->niov;
592 }
9ef91a67
CH
593 acb->aio_nbytes = nb_sectors * 512;
594 acb->aio_offset = sector_num * 512;
595
596 acb->next = posix_aio_state->first_aio;
597 posix_aio_state->first_aio = acb;
598
6d519a5f 599 trace_paio_submit(acb, opaque, sector_num, nb_sectors, type);
9ef91a67
CH
600 qemu_paio_submit(acb);
601 return &acb->common;
602}
603
604BlockDriverAIOCB *paio_ioctl(BlockDriverState *bs, int fd,
605 unsigned long int req, void *buf,
606 BlockDriverCompletionFunc *cb, void *opaque)
607{
608 struct qemu_paiocb *acb;
609
610 acb = qemu_aio_get(&raw_aio_pool, bs, cb, opaque);
611 if (!acb)
612 return NULL;
613 acb->aio_type = QEMU_AIO_IOCTL;
614 acb->aio_fildes = fd;
615 acb->ev_signo = SIGUSR2;
616 acb->aio_offset = 0;
617 acb->aio_ioctl_buf = buf;
618 acb->aio_ioctl_cmd = req;
619
620 acb->next = posix_aio_state->first_aio;
621 posix_aio_state->first_aio = acb;
622
623 qemu_paio_submit(acb);
624 return &acb->common;
625}
626
1e5b9d2f 627int paio_init(void)
9ef91a67
CH
628{
629 struct sigaction act;
630 PosixAioState *s;
631 int fds[2];
632 int ret;
633
634 if (posix_aio_state)
1e5b9d2f 635 return 0;
9ef91a67 636
7267c094 637 s = g_malloc(sizeof(PosixAioState));
9ef91a67
CH
638
639 sigfillset(&act.sa_mask);
640 act.sa_flags = 0; /* do not restart syscalls to interrupt select() */
641 act.sa_handler = aio_signal_handler;
642 sigaction(SIGUSR2, &act, NULL);
643
644 s->first_aio = NULL;
40ff6d7e 645 if (qemu_pipe(fds) == -1) {
9ef91a67 646 fprintf(stderr, "failed to create pipe\n");
1e5b9d2f 647 return -1;
9ef91a67
CH
648 }
649
650 s->rfd = fds[0];
651 s->wfd = fds[1];
652
653 fcntl(s->rfd, F_SETFL, O_NONBLOCK);
654 fcntl(s->wfd, F_SETFL, O_NONBLOCK);
655
8febfa26
KW
656 qemu_aio_set_fd_handler(s->rfd, posix_aio_read, NULL, posix_aio_flush,
657 posix_aio_process_queue, s);
9ef91a67
CH
658
659 ret = pthread_attr_init(&attr);
660 if (ret)
661 die2(ret, "pthread_attr_init");
662
663 ret = pthread_attr_setdetachstate(&attr, PTHREAD_CREATE_DETACHED);
664 if (ret)
665 die2(ret, "pthread_attr_setdetachstate");
666
72cf2d4f 667 QTAILQ_INIT(&request_list);
9ef91a67
CH
668
669 posix_aio_state = s;
1e5b9d2f 670 return 0;
3c529d93 671}