]> git.proxmox.com Git - mirror_qemu.git/blob - posix-aio-compat.c
async: Remove AsyncContext
[mirror_qemu.git] / posix-aio-compat.c
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
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 <string.h>
21 #include <stdlib.h>
22 #include <stdio.h>
23
24 #include "qemu-queue.h"
25 #include "osdep.h"
26 #include "sysemu.h"
27 #include "qemu-common.h"
28 #include "trace.h"
29 #include "block_int.h"
30
31 #include "block/raw-posix-aio.h"
32
33
34 struct qemu_paiocb {
35 BlockDriverAIOCB common;
36 int aio_fildes;
37 union {
38 struct iovec *aio_iov;
39 void *aio_ioctl_buf;
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
47 QTAILQ_ENTRY(qemu_paiocb) node;
48 int aio_type;
49 ssize_t ret;
50 int active;
51 struct qemu_paiocb *next;
52 };
53
54 typedef struct PosixAioState {
55 int rfd, wfd;
56 struct qemu_paiocb *first_aio;
57 } PosixAioState;
58
59
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;
68
69 #ifdef CONFIG_PREADV
70 static int preadv_present = 1;
71 #else
72 static int preadv_present = 0;
73 #endif
74
75 static void die2(int err, const char *what)
76 {
77 fprintf(stderr, "%s failed: %s\n", what, strerror(err));
78 abort();
79 }
80
81 static void die(const char *what)
82 {
83 die2(errno, what);
84 }
85
86 static 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
92 static 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
98 static 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
106 static void cond_signal(pthread_cond_t *cond)
107 {
108 int ret = pthread_cond_signal(cond);
109 if (ret) die2(ret, "pthread_cond_signal");
110 }
111
112 static 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
119 static ssize_t handle_aiocb_ioctl(struct qemu_paiocb *aiocb)
120 {
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
129 * successful if it has written the number full number of bytes.
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;
136 }
137
138 static ssize_t handle_aiocb_flush(struct qemu_paiocb *aiocb)
139 {
140 int ret;
141
142 ret = qemu_fdatasync(aiocb->aio_fildes);
143 if (ret == -1)
144 return -errno;
145 return 0;
146 }
147
148 #ifdef CONFIG_PREADV
149
150 static ssize_t
151 qemu_preadv(int fd, const struct iovec *iov, int nr_iov, off_t offset)
152 {
153 return preadv(fd, iov, nr_iov, offset);
154 }
155
156 static ssize_t
157 qemu_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
164 static ssize_t
165 qemu_preadv(int fd, const struct iovec *iov, int nr_iov, off_t offset)
166 {
167 return -ENOSYS;
168 }
169
170 static ssize_t
171 qemu_pwritev(int fd, const struct iovec *iov, int nr_iov, off_t offset)
172 {
173 return -ENOSYS;
174 }
175
176 #endif
177
178 static ssize_t handle_aiocb_rw_vector(struct qemu_paiocb *aiocb)
179 {
180 size_t offset = 0;
181 ssize_t len;
182
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);
195
196 if (len == -1)
197 return -errno;
198 return len;
199 }
200
201 static ssize_t handle_aiocb_rw_linear(struct qemu_paiocb *aiocb, char *buf)
202 {
203 ssize_t offset = 0;
204 ssize_t len;
205
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);
217
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;
225
226 offset += len;
227 }
228
229 return offset;
230 }
231
232 static ssize_t handle_aiocb_rw(struct qemu_paiocb *aiocb)
233 {
234 ssize_t nbytes;
235 char *buf;
236
237 if (!(aiocb->aio_type & QEMU_AIO_MISALIGNED)) {
238 /*
239 * If there is just a single buffer, and it is properly aligned
240 * we can just use plain pread/pwrite without any problems.
241 */
242 if (aiocb->aio_niov == 1)
243 return handle_aiocb_rw_linear(aiocb, aiocb->aio_iov->iov_base);
244
245 /*
246 * We have more than one iovec, and all are properly aligned.
247 *
248 * Try preadv/pwritev first and fall back to linearizing the
249 * buffer if it's not supported.
250 */
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;
258 }
259
260 /*
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?
264 */
265 }
266
267 /*
268 * Ok, we have to do it the hard way, copy all segments into
269 * a single aligned buffer.
270 */
271 buf = qemu_blockalign(aiocb->common.bs, aiocb->aio_nbytes);
272 if (aiocb->aio_type & QEMU_AIO_WRITE) {
273 char *p = buf;
274 int i;
275
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;
279 }
280 }
281
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;
287
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;
295 }
296 }
297 qemu_vfree(buf);
298
299 return nbytes;
300 }
301
302 static void *aio_thread(void *unused)
303 {
304 pid_t pid;
305
306 pid = getpid();
307
308 while (1) {
309 struct qemu_paiocb *aiocb;
310 ssize_t ret = 0;
311 qemu_timeval tv;
312 struct timespec ts;
313
314 qemu_gettimeofday(&tv);
315 ts.tv_sec = tv.tv_sec + 10;
316 ts.tv_nsec = 0;
317
318 mutex_lock(&lock);
319
320 while (QTAILQ_EMPTY(&request_list) &&
321 !(ret == ETIMEDOUT)) {
322 idle_threads++;
323 ret = cond_timedwait(&cond, &lock, &ts);
324 idle_threads--;
325 }
326
327 if (QTAILQ_EMPTY(&request_list))
328 break;
329
330 aiocb = QTAILQ_FIRST(&request_list);
331 QTAILQ_REMOVE(&request_list, aiocb, node);
332 aiocb->active = 1;
333 mutex_unlock(&lock);
334
335 switch (aiocb->aio_type & QEMU_AIO_TYPE_MASK) {
336 case QEMU_AIO_READ:
337 case QEMU_AIO_WRITE:
338 ret = handle_aiocb_rw(aiocb);
339 break;
340 case QEMU_AIO_FLUSH:
341 ret = handle_aiocb_flush(aiocb);
342 break;
343 case QEMU_AIO_IOCTL:
344 ret = handle_aiocb_ioctl(aiocb);
345 break;
346 default:
347 fprintf(stderr, "invalid aio request (0x%x)\n", aiocb->aio_type);
348 ret = -EINVAL;
349 break;
350 }
351
352 mutex_lock(&lock);
353 aiocb->ret = ret;
354 mutex_unlock(&lock);
355
356 if (kill(pid, aiocb->ev_signo)) die("kill failed");
357 }
358
359 cur_threads--;
360 mutex_unlock(&lock);
361
362 return NULL;
363 }
364
365 static void spawn_thread(void)
366 {
367 sigset_t set, oldset;
368
369 cur_threads++;
370
371 /* block all signals */
372 if (sigfillset(&set)) die("sigfillset");
373 if (sigprocmask(SIG_SETMASK, &set, &oldset)) die("sigprocmask");
374
375 thread_create(&thread_id, &attr, aio_thread, NULL);
376
377 if (sigprocmask(SIG_SETMASK, &oldset, NULL)) die("sigprocmask restore");
378 }
379
380 static void qemu_paio_submit(struct qemu_paiocb *aiocb)
381 {
382 aiocb->ret = -EINPROGRESS;
383 aiocb->active = 0;
384 mutex_lock(&lock);
385 if (idle_threads == 0 && cur_threads < max_threads)
386 spawn_thread();
387 QTAILQ_INSERT_TAIL(&request_list, aiocb, node);
388 mutex_unlock(&lock);
389 cond_signal(&cond);
390 }
391
392 static ssize_t qemu_paio_return(struct qemu_paiocb *aiocb)
393 {
394 ssize_t ret;
395
396 mutex_lock(&lock);
397 ret = aiocb->ret;
398 mutex_unlock(&lock);
399
400 return ret;
401 }
402
403 static int qemu_paio_error(struct qemu_paiocb *aiocb)
404 {
405 ssize_t ret = qemu_paio_return(aiocb);
406
407 if (ret < 0)
408 ret = -ret;
409 else
410 ret = 0;
411
412 return ret;
413 }
414
415 static int posix_aio_process_queue(void *opaque)
416 {
417 PosixAioState *s = opaque;
418 struct qemu_paiocb *acb, **pacb;
419 int ret;
420 int result = 0;
421
422 for(;;) {
423 pacb = &s->first_aio;
424 for(;;) {
425 acb = *pacb;
426 if (!acb)
427 return result;
428
429 ret = qemu_paio_error(acb);
430 if (ret == ECANCELED) {
431 /* remove the request */
432 *pacb = acb->next;
433 qemu_aio_release(acb);
434 result = 1;
435 } else if (ret != EINPROGRESS) {
436 /* end of aio */
437 if (ret == 0) {
438 ret = qemu_paio_return(acb);
439 if (ret == acb->aio_nbytes)
440 ret = 0;
441 else
442 ret = -EINVAL;
443 } else {
444 ret = -ret;
445 }
446
447 trace_paio_complete(acb, acb->common.opaque, ret);
448
449 /* remove the request */
450 *pacb = acb->next;
451 /* call the callback */
452 acb->common.cb(acb->common.opaque, ret);
453 qemu_aio_release(acb);
454 result = 1;
455 break;
456 } else {
457 pacb = &acb->next;
458 }
459 }
460 }
461
462 return result;
463 }
464
465 static void posix_aio_read(void *opaque)
466 {
467 PosixAioState *s = opaque;
468 ssize_t len;
469
470 /* read all bytes from signal pipe */
471 for (;;) {
472 char bytes[16];
473
474 len = read(s->rfd, bytes, sizeof(bytes));
475 if (len == -1 && errno == EINTR)
476 continue; /* try again */
477 if (len == sizeof(bytes))
478 continue; /* more to read */
479 break;
480 }
481
482 posix_aio_process_queue(s);
483 }
484
485 static int posix_aio_flush(void *opaque)
486 {
487 PosixAioState *s = opaque;
488 return !!s->first_aio;
489 }
490
491 static PosixAioState *posix_aio_state;
492
493 static void aio_signal_handler(int signum)
494 {
495 if (posix_aio_state) {
496 char byte = 0;
497 ssize_t ret;
498
499 ret = write(posix_aio_state->wfd, &byte, sizeof(byte));
500 if (ret < 0 && errno != EAGAIN)
501 die("write()");
502 }
503
504 qemu_service_io();
505 }
506
507 static void paio_remove(struct qemu_paiocb *acb)
508 {
509 struct qemu_paiocb **pacb;
510
511 /* remove the callback from the queue */
512 pacb = &posix_aio_state->first_aio;
513 for(;;) {
514 if (*pacb == NULL) {
515 fprintf(stderr, "paio_remove: aio request not found!\n");
516 break;
517 } else if (*pacb == acb) {
518 *pacb = acb->next;
519 qemu_aio_release(acb);
520 break;
521 }
522 pacb = &(*pacb)->next;
523 }
524 }
525
526 static void paio_cancel(BlockDriverAIOCB *blockacb)
527 {
528 struct qemu_paiocb *acb = (struct qemu_paiocb *)blockacb;
529 int active = 0;
530
531 trace_paio_cancel(acb, acb->common.opaque);
532
533 mutex_lock(&lock);
534 if (!acb->active) {
535 QTAILQ_REMOVE(&request_list, acb, node);
536 acb->ret = -ECANCELED;
537 } else if (acb->ret == -EINPROGRESS) {
538 active = 1;
539 }
540 mutex_unlock(&lock);
541
542 if (active) {
543 /* fail safe: if the aio could not be canceled, we wait for
544 it */
545 while (qemu_paio_error(acb) == EINPROGRESS)
546 ;
547 }
548
549 paio_remove(acb);
550 }
551
552 static AIOPool raw_aio_pool = {
553 .aiocb_size = sizeof(struct qemu_paiocb),
554 .cancel = paio_cancel,
555 };
556
557 BlockDriverAIOCB *paio_submit(BlockDriverState *bs, int fd,
558 int64_t sector_num, QEMUIOVector *qiov, int nb_sectors,
559 BlockDriverCompletionFunc *cb, void *opaque, int type)
560 {
561 struct qemu_paiocb *acb;
562
563 acb = qemu_aio_get(&raw_aio_pool, bs, cb, opaque);
564 if (!acb)
565 return NULL;
566 acb->aio_type = type;
567 acb->aio_fildes = fd;
568 acb->ev_signo = SIGUSR2;
569
570 if (qiov) {
571 acb->aio_iov = qiov->iov;
572 acb->aio_niov = qiov->niov;
573 }
574 acb->aio_nbytes = nb_sectors * 512;
575 acb->aio_offset = sector_num * 512;
576
577 acb->next = posix_aio_state->first_aio;
578 posix_aio_state->first_aio = acb;
579
580 trace_paio_submit(acb, opaque, sector_num, nb_sectors, type);
581 qemu_paio_submit(acb);
582 return &acb->common;
583 }
584
585 BlockDriverAIOCB *paio_ioctl(BlockDriverState *bs, int fd,
586 unsigned long int req, void *buf,
587 BlockDriverCompletionFunc *cb, void *opaque)
588 {
589 struct qemu_paiocb *acb;
590
591 acb = qemu_aio_get(&raw_aio_pool, bs, cb, opaque);
592 if (!acb)
593 return NULL;
594 acb->aio_type = QEMU_AIO_IOCTL;
595 acb->aio_fildes = fd;
596 acb->ev_signo = SIGUSR2;
597 acb->aio_offset = 0;
598 acb->aio_ioctl_buf = buf;
599 acb->aio_ioctl_cmd = req;
600
601 acb->next = posix_aio_state->first_aio;
602 posix_aio_state->first_aio = acb;
603
604 qemu_paio_submit(acb);
605 return &acb->common;
606 }
607
608 int paio_init(void)
609 {
610 struct sigaction act;
611 PosixAioState *s;
612 int fds[2];
613 int ret;
614
615 if (posix_aio_state)
616 return 0;
617
618 s = qemu_malloc(sizeof(PosixAioState));
619
620 sigfillset(&act.sa_mask);
621 act.sa_flags = 0; /* do not restart syscalls to interrupt select() */
622 act.sa_handler = aio_signal_handler;
623 sigaction(SIGUSR2, &act, NULL);
624
625 s->first_aio = NULL;
626 if (qemu_pipe(fds) == -1) {
627 fprintf(stderr, "failed to create pipe\n");
628 return -1;
629 }
630
631 s->rfd = fds[0];
632 s->wfd = fds[1];
633
634 fcntl(s->rfd, F_SETFL, O_NONBLOCK);
635 fcntl(s->wfd, F_SETFL, O_NONBLOCK);
636
637 qemu_aio_set_fd_handler(s->rfd, posix_aio_read, NULL, posix_aio_flush,
638 posix_aio_process_queue, s);
639
640 ret = pthread_attr_init(&attr);
641 if (ret)
642 die2(ret, "pthread_attr_init");
643
644 ret = pthread_attr_setdetachstate(&attr, PTHREAD_CREATE_DETACHED);
645 if (ret)
646 die2(ret, "pthread_attr_setdetachstate");
647
648 QTAILQ_INIT(&request_list);
649
650 posix_aio_state = s;
651 return 0;
652 }