blob: 4901539b9281c85effae336094fcf61bec59e1e1 [file] [log] [blame]
aliguori3c529d92008-12-12 16:41:40 +00001/*
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
aliguori221f7152009-03-28 17:28:41 +000014#include <sys/ioctl.h>
Christoph Hellwig9ef91a62009-08-20 16:58:19 +020015#include <sys/types.h>
aliguori3c529d92008-12-12 16:41:40 +000016#include <pthread.h>
17#include <unistd.h>
18#include <errno.h>
malc30525af2009-02-21 05:48:13 +000019#include <time.h>
Christoph Hellwig9ef91a62009-08-20 16:58:19 +020020#include <signal.h>
malc8653c012009-02-21 05:48:11 +000021#include <string.h>
22#include <stdlib.h>
23#include <stdio.h>
Christoph Hellwig9ef91a62009-08-20 16:58:19 +020024
Blue Swirl72cf2d42009-09-12 07:36:22 +000025#include "qemu-queue.h"
aliguori3c529d92008-12-12 16:41:40 +000026#include "osdep.h"
aliguorif141eaf2009-04-07 18:43:24 +000027#include "qemu-common.h"
Christoph Hellwig9ef91a62009-08-20 16:58:19 +020028#include "block_int.h"
aliguori3c529d92008-12-12 16:41:40 +000029
Christoph Hellwig9ef91a62009-08-20 16:58:19 +020030#include "block/raw-posix-aio.h"
31
32
33struct qemu_paiocb {
34 BlockDriverAIOCB common;
35 int aio_fildes;
36 union {
37 struct iovec *aio_iov;
38 void *aio_ioctl_buf;
39 };
40 int aio_niov;
41 size_t aio_nbytes;
42#define aio_ioctl_cmd aio_nbytes /* for QEMU_AIO_IOCTL */
43 int ev_signo;
44 off_t aio_offset;
45
Blue Swirl72cf2d42009-09-12 07:36:22 +000046 QTAILQ_ENTRY(qemu_paiocb) node;
Christoph Hellwig9ef91a62009-08-20 16:58:19 +020047 int aio_type;
48 ssize_t ret;
49 int active;
50 struct qemu_paiocb *next;
51};
52
53typedef struct PosixAioState {
54 int rfd, wfd;
55 struct qemu_paiocb *first_aio;
56} PosixAioState;
57
aliguori3c529d92008-12-12 16:41:40 +000058
59static pthread_mutex_t lock = PTHREAD_MUTEX_INITIALIZER;
60static pthread_cond_t cond = PTHREAD_COND_INITIALIZER;
61static pthread_t thread_id;
malca8227a52009-02-21 05:48:17 +000062static pthread_attr_t attr;
aliguori3c529d92008-12-12 16:41:40 +000063static int max_threads = 64;
64static int cur_threads = 0;
65static int idle_threads = 0;
Blue Swirl72cf2d42009-09-12 07:36:22 +000066static QTAILQ_HEAD(, qemu_paiocb) request_list;
aliguori3c529d92008-12-12 16:41:40 +000067
Juan Quintela2341f9a2009-07-27 16:12:58 +020068#ifdef CONFIG_PREADV
aliguoriceb42de2009-04-07 18:43:28 +000069static int preadv_present = 1;
70#else
71static int preadv_present = 0;
72#endif
73
malc8653c012009-02-21 05:48:11 +000074static void die2(int err, const char *what)
75{
76 fprintf(stderr, "%s failed: %s\n", what, strerror(err));
77 abort();
78}
79
80static void die(const char *what)
81{
82 die2(errno, what);
83}
84
85static void mutex_lock(pthread_mutex_t *mutex)
86{
87 int ret = pthread_mutex_lock(mutex);
88 if (ret) die2(ret, "pthread_mutex_lock");
89}
90
91static void mutex_unlock(pthread_mutex_t *mutex)
92{
93 int ret = pthread_mutex_unlock(mutex);
94 if (ret) die2(ret, "pthread_mutex_unlock");
95}
96
97static int cond_timedwait(pthread_cond_t *cond, pthread_mutex_t *mutex,
98 struct timespec *ts)
99{
100 int ret = pthread_cond_timedwait(cond, mutex, ts);
101 if (ret && ret != ETIMEDOUT) die2(ret, "pthread_cond_timedwait");
102 return ret;
103}
104
malc5d47e372009-02-21 05:48:15 +0000105static void cond_signal(pthread_cond_t *cond)
malc8653c012009-02-21 05:48:11 +0000106{
malc5d47e372009-02-21 05:48:15 +0000107 int ret = pthread_cond_signal(cond);
108 if (ret) die2(ret, "pthread_cond_signal");
malc8653c012009-02-21 05:48:11 +0000109}
110
111static void thread_create(pthread_t *thread, pthread_attr_t *attr,
112 void *(*start_routine)(void*), void *arg)
113{
114 int ret = pthread_create(thread, attr, start_routine, arg);
115 if (ret) die2(ret, "pthread_create");
116}
117
aliguorif141eaf2009-04-07 18:43:24 +0000118static size_t handle_aiocb_ioctl(struct qemu_paiocb *aiocb)
119{
120 int ret;
121
122 ret = ioctl(aiocb->aio_fildes, aiocb->aio_ioctl_cmd, aiocb->aio_ioctl_buf);
123 if (ret == -1)
124 return -errno;
Christoph Hellwige7d54ae2009-04-28 11:57:02 +0200125
126 /*
127 * This looks weird, but the aio code only consideres a request
128 * successfull if it has written the number full number of bytes.
129 *
130 * Now we overload aio_nbytes as aio_ioctl_cmd for the ioctl command,
131 * so in fact we return the ioctl command here to make posix_aio_read()
132 * happy..
133 */
134 return aiocb->aio_nbytes;
aliguorif141eaf2009-04-07 18:43:24 +0000135}
136
Christoph Hellwigb2e12bc2009-09-04 19:01:49 +0200137static size_t handle_aiocb_flush(struct qemu_paiocb *aiocb)
138{
139 int ret;
140
Blue Swirl47faadc2009-09-12 06:19:14 +0000141 ret = qemu_fdatasync(aiocb->aio_fildes);
Christoph Hellwigb2e12bc2009-09-04 19:01:49 +0200142 if (ret == -1)
143 return -errno;
144 return 0;
145}
146
Juan Quintela2341f9a2009-07-27 16:12:58 +0200147#ifdef CONFIG_PREADV
aliguoriceb42de2009-04-07 18:43:28 +0000148
149static ssize_t
150qemu_preadv(int fd, const struct iovec *iov, int nr_iov, off_t offset)
151{
152 return preadv(fd, iov, nr_iov, offset);
153}
154
155static ssize_t
156qemu_pwritev(int fd, const struct iovec *iov, int nr_iov, off_t offset)
157{
158 return pwritev(fd, iov, nr_iov, offset);
159}
160
161#else
162
163static ssize_t
164qemu_preadv(int fd, const struct iovec *iov, int nr_iov, off_t offset)
165{
166 return -ENOSYS;
167}
168
169static ssize_t
170qemu_pwritev(int fd, const struct iovec *iov, int nr_iov, off_t offset)
171{
172 return -ENOSYS;
173}
174
175#endif
176
aliguoriceb42de2009-04-07 18:43:28 +0000177static size_t handle_aiocb_rw_vector(struct qemu_paiocb *aiocb)
178{
179 size_t offset = 0;
180 ssize_t len;
181
182 do {
Christoph Hellwig9ef91a62009-08-20 16:58:19 +0200183 if (aiocb->aio_type & QEMU_AIO_WRITE)
aliguoriceb42de2009-04-07 18:43:28 +0000184 len = qemu_pwritev(aiocb->aio_fildes,
185 aiocb->aio_iov,
186 aiocb->aio_niov,
187 aiocb->aio_offset + offset);
188 else
189 len = qemu_preadv(aiocb->aio_fildes,
190 aiocb->aio_iov,
191 aiocb->aio_niov,
192 aiocb->aio_offset + offset);
193 } while (len == -1 && errno == EINTR);
194
195 if (len == -1)
196 return -errno;
197 return len;
198}
199
aliguorif141eaf2009-04-07 18:43:24 +0000200static size_t handle_aiocb_rw_linear(struct qemu_paiocb *aiocb, char *buf)
aliguori221f7152009-03-28 17:28:41 +0000201{
202 size_t offset = 0;
aliguorif141eaf2009-04-07 18:43:24 +0000203 size_t len;
aliguori221f7152009-03-28 17:28:41 +0000204
205 while (offset < aiocb->aio_nbytes) {
Christoph Hellwig9ef91a62009-08-20 16:58:19 +0200206 if (aiocb->aio_type & QEMU_AIO_WRITE)
aliguorif141eaf2009-04-07 18:43:24 +0000207 len = pwrite(aiocb->aio_fildes,
208 (const char *)buf + offset,
209 aiocb->aio_nbytes - offset,
210 aiocb->aio_offset + offset);
211 else
212 len = pread(aiocb->aio_fildes,
213 buf + offset,
aliguori221f7152009-03-28 17:28:41 +0000214 aiocb->aio_nbytes - offset,
215 aiocb->aio_offset + offset);
aliguori221f7152009-03-28 17:28:41 +0000216
aliguorif141eaf2009-04-07 18:43:24 +0000217 if (len == -1 && errno == EINTR)
218 continue;
219 else if (len == -1) {
220 offset = -errno;
221 break;
222 } else if (len == 0)
223 break;
aliguori221f7152009-03-28 17:28:41 +0000224
aliguorif141eaf2009-04-07 18:43:24 +0000225 offset += len;
aliguori221f7152009-03-28 17:28:41 +0000226 }
227
228 return offset;
229}
230
aliguorif141eaf2009-04-07 18:43:24 +0000231static size_t handle_aiocb_rw(struct qemu_paiocb *aiocb)
aliguori221f7152009-03-28 17:28:41 +0000232{
aliguorif141eaf2009-04-07 18:43:24 +0000233 size_t nbytes;
234 char *buf;
aliguori221f7152009-03-28 17:28:41 +0000235
Christoph Hellwig9ef91a62009-08-20 16:58:19 +0200236 if (!(aiocb->aio_type & QEMU_AIO_MISALIGNED)) {
aliguorif141eaf2009-04-07 18:43:24 +0000237 /*
238 * If there is just a single buffer, and it is properly aligned
239 * we can just use plain pread/pwrite without any problems.
240 */
aliguoriceb42de2009-04-07 18:43:28 +0000241 if (aiocb->aio_niov == 1)
242 return handle_aiocb_rw_linear(aiocb, aiocb->aio_iov->iov_base);
243
244 /*
245 * We have more than one iovec, and all are properly aligned.
246 *
247 * Try preadv/pwritev first and fall back to linearizing the
248 * buffer if it's not supported.
249 */
250 if (preadv_present) {
251 nbytes = handle_aiocb_rw_vector(aiocb);
252 if (nbytes == aiocb->aio_nbytes)
253 return nbytes;
254 if (nbytes < 0 && nbytes != -ENOSYS)
255 return nbytes;
256 preadv_present = 0;
257 }
258
259 /*
260 * XXX(hch): short read/write. no easy way to handle the reminder
261 * using these interfaces. For now retry using plain
262 * pread/pwrite?
263 */
aliguorif141eaf2009-04-07 18:43:24 +0000264 }
265
266 /*
267 * Ok, we have to do it the hard way, copy all segments into
268 * a single aligned buffer.
269 */
270 buf = qemu_memalign(512, aiocb->aio_nbytes);
Christoph Hellwig9ef91a62009-08-20 16:58:19 +0200271 if (aiocb->aio_type & QEMU_AIO_WRITE) {
aliguorif141eaf2009-04-07 18:43:24 +0000272 char *p = buf;
273 int i;
274
275 for (i = 0; i < aiocb->aio_niov; ++i) {
276 memcpy(p, aiocb->aio_iov[i].iov_base, aiocb->aio_iov[i].iov_len);
277 p += aiocb->aio_iov[i].iov_len;
278 }
279 }
280
281 nbytes = handle_aiocb_rw_linear(aiocb, buf);
Christoph Hellwig9ef91a62009-08-20 16:58:19 +0200282 if (!(aiocb->aio_type & QEMU_AIO_WRITE)) {
aliguorif141eaf2009-04-07 18:43:24 +0000283 char *p = buf;
284 size_t count = aiocb->aio_nbytes, copy;
285 int i;
286
287 for (i = 0; i < aiocb->aio_niov && count; ++i) {
288 copy = count;
289 if (copy > aiocb->aio_iov[i].iov_len)
290 copy = aiocb->aio_iov[i].iov_len;
291 memcpy(aiocb->aio_iov[i].iov_base, p, copy);
292 p += copy;
293 count -= copy;
294 }
295 }
296 qemu_vfree(buf);
297
298 return nbytes;
aliguori221f7152009-03-28 17:28:41 +0000299}
300
aliguori3c529d92008-12-12 16:41:40 +0000301static void *aio_thread(void *unused)
302{
malca8227a52009-02-21 05:48:17 +0000303 pid_t pid;
aliguori3c529d92008-12-12 16:41:40 +0000304
malca8227a52009-02-21 05:48:17 +0000305 pid = getpid();
306
aliguori3c529d92008-12-12 16:41:40 +0000307 while (1) {
308 struct qemu_paiocb *aiocb;
aliguori221f7152009-03-28 17:28:41 +0000309 size_t ret = 0;
malc30525af2009-02-21 05:48:13 +0000310 qemu_timeval tv;
311 struct timespec ts;
312
313 qemu_gettimeofday(&tv);
314 ts.tv_sec = tv.tv_sec + 10;
315 ts.tv_nsec = 0;
aliguori3c529d92008-12-12 16:41:40 +0000316
malc8653c012009-02-21 05:48:11 +0000317 mutex_lock(&lock);
aliguori3c529d92008-12-12 16:41:40 +0000318
Blue Swirl72cf2d42009-09-12 07:36:22 +0000319 while (QTAILQ_EMPTY(&request_list) &&
aliguori3c529d92008-12-12 16:41:40 +0000320 !(ret == ETIMEDOUT)) {
malc8653c012009-02-21 05:48:11 +0000321 ret = cond_timedwait(&cond, &lock, &ts);
aliguori3c529d92008-12-12 16:41:40 +0000322 }
323
Blue Swirl72cf2d42009-09-12 07:36:22 +0000324 if (QTAILQ_EMPTY(&request_list))
aliguori3c529d92008-12-12 16:41:40 +0000325 break;
326
Blue Swirl72cf2d42009-09-12 07:36:22 +0000327 aiocb = QTAILQ_FIRST(&request_list);
328 QTAILQ_REMOVE(&request_list, aiocb, node);
aliguori3c529d92008-12-12 16:41:40 +0000329 aiocb->active = 1;
aliguori3c529d92008-12-12 16:41:40 +0000330 idle_threads--;
malc8653c012009-02-21 05:48:11 +0000331 mutex_unlock(&lock);
aliguori3c529d92008-12-12 16:41:40 +0000332
Christoph Hellwig9ef91a62009-08-20 16:58:19 +0200333 switch (aiocb->aio_type & QEMU_AIO_TYPE_MASK) {
334 case QEMU_AIO_READ:
335 case QEMU_AIO_WRITE:
aliguorif141eaf2009-04-07 18:43:24 +0000336 ret = handle_aiocb_rw(aiocb);
aliguori221f7152009-03-28 17:28:41 +0000337 break;
Christoph Hellwigb2e12bc2009-09-04 19:01:49 +0200338 case QEMU_AIO_FLUSH:
339 ret = handle_aiocb_flush(aiocb);
340 break;
Christoph Hellwig9ef91a62009-08-20 16:58:19 +0200341 case QEMU_AIO_IOCTL:
aliguori221f7152009-03-28 17:28:41 +0000342 ret = handle_aiocb_ioctl(aiocb);
343 break;
344 default:
345 fprintf(stderr, "invalid aio request (0x%x)\n", aiocb->aio_type);
346 ret = -EINVAL;
347 break;
348 }
aliguori3c529d92008-12-12 16:41:40 +0000349
malc8653c012009-02-21 05:48:11 +0000350 mutex_lock(&lock);
aliguori221f7152009-03-28 17:28:41 +0000351 aiocb->ret = ret;
aliguori3c529d92008-12-12 16:41:40 +0000352 idle_threads++;
malc8653c012009-02-21 05:48:11 +0000353 mutex_unlock(&lock);
aliguori3c529d92008-12-12 16:41:40 +0000354
malca8227a52009-02-21 05:48:17 +0000355 if (kill(pid, aiocb->ev_signo)) die("kill failed");
aliguori3c529d92008-12-12 16:41:40 +0000356 }
357
358 idle_threads--;
359 cur_threads--;
malc8653c012009-02-21 05:48:11 +0000360 mutex_unlock(&lock);
aliguori3c529d92008-12-12 16:41:40 +0000361
362 return NULL;
363}
364
malc8653c012009-02-21 05:48:11 +0000365static void spawn_thread(void)
aliguori3c529d92008-12-12 16:41:40 +0000366{
malcee399302009-09-25 00:20:44 +0400367 sigset_t set, oldset;
368
aliguori3c529d92008-12-12 16:41:40 +0000369 cur_threads++;
370 idle_threads++;
malcee399302009-09-25 00:20:44 +0400371
372 /* block all signals */
373 if (sigfillset(&set)) die("sigfillset");
374 if (sigprocmask(SIG_SETMASK, &set, &oldset)) die("sigprocmask");
375
malc8653c012009-02-21 05:48:11 +0000376 thread_create(&thread_id, &attr, aio_thread, NULL);
malcee399302009-09-25 00:20:44 +0400377
378 if (sigprocmask(SIG_SETMASK, &oldset, NULL)) die("sigprocmask restore");
aliguori3c529d92008-12-12 16:41:40 +0000379}
380
Christoph Hellwig9ef91a62009-08-20 16:58:19 +0200381static void qemu_paio_submit(struct qemu_paiocb *aiocb)
aliguori3c529d92008-12-12 16:41:40 +0000382{
aliguori3c529d92008-12-12 16:41:40 +0000383 aiocb->ret = -EINPROGRESS;
384 aiocb->active = 0;
malc8653c012009-02-21 05:48:11 +0000385 mutex_lock(&lock);
aliguori3c529d92008-12-12 16:41:40 +0000386 if (idle_threads == 0 && cur_threads < max_threads)
387 spawn_thread();
Blue Swirl72cf2d42009-09-12 07:36:22 +0000388 QTAILQ_INSERT_TAIL(&request_list, aiocb, node);
malc8653c012009-02-21 05:48:11 +0000389 mutex_unlock(&lock);
malc5d47e372009-02-21 05:48:15 +0000390 cond_signal(&cond);
aliguori3c529d92008-12-12 16:41:40 +0000391}
392
Christoph Hellwig9ef91a62009-08-20 16:58:19 +0200393static ssize_t qemu_paio_return(struct qemu_paiocb *aiocb)
aliguori3c529d92008-12-12 16:41:40 +0000394{
395 ssize_t ret;
396
malc8653c012009-02-21 05:48:11 +0000397 mutex_lock(&lock);
aliguori3c529d92008-12-12 16:41:40 +0000398 ret = aiocb->ret;
malc8653c012009-02-21 05:48:11 +0000399 mutex_unlock(&lock);
aliguori3c529d92008-12-12 16:41:40 +0000400
401 return ret;
402}
403
Christoph Hellwig9ef91a62009-08-20 16:58:19 +0200404static int qemu_paio_error(struct qemu_paiocb *aiocb)
aliguori3c529d92008-12-12 16:41:40 +0000405{
406 ssize_t ret = qemu_paio_return(aiocb);
407
408 if (ret < 0)
409 ret = -ret;
410 else
411 ret = 0;
412
413 return ret;
414}
415
Kevin Wolf59c7b152009-10-22 17:54:35 +0200416static int posix_aio_process_queue(void *opaque)
aliguori3c529d92008-12-12 16:41:40 +0000417{
Christoph Hellwig9ef91a62009-08-20 16:58:19 +0200418 PosixAioState *s = opaque;
419 struct qemu_paiocb *acb, **pacb;
aliguori3c529d92008-12-12 16:41:40 +0000420 int ret;
Kevin Wolf59c7b152009-10-22 17:54:35 +0200421 int result = 0;
Christoph Hellwig9ef91a62009-08-20 16:58:19 +0200422
423 for(;;) {
424 pacb = &s->first_aio;
425 for(;;) {
426 acb = *pacb;
427 if (!acb)
Kevin Wolf59c7b152009-10-22 17:54:35 +0200428 return result;
Christoph Hellwig9ef91a62009-08-20 16:58:19 +0200429 ret = qemu_paio_error(acb);
430 if (ret == ECANCELED) {
431 /* remove the request */
432 *pacb = acb->next;
433 qemu_aio_release(acb);
Kevin Wolf59c7b152009-10-22 17:54:35 +0200434 result = 1;
Christoph Hellwig9ef91a62009-08-20 16:58:19 +0200435 } 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 /* remove the request */
447 *pacb = acb->next;
448 /* call the callback */
449 acb->common.cb(acb->common.opaque, ret);
450 qemu_aio_release(acb);
Kevin Wolf59c7b152009-10-22 17:54:35 +0200451 result = 1;
Christoph Hellwig9ef91a62009-08-20 16:58:19 +0200452 break;
453 } else {
454 pacb = &acb->next;
455 }
456 }
457 }
Kevin Wolf59c7b152009-10-22 17:54:35 +0200458
459 return result;
460}
461
462static void posix_aio_read(void *opaque)
463{
464 PosixAioState *s = opaque;
465 ssize_t len;
466
467 /* read all bytes from signal pipe */
468 for (;;) {
469 char bytes[16];
470
471 len = read(s->rfd, bytes, sizeof(bytes));
472 if (len == -1 && errno == EINTR)
473 continue; /* try again */
474 if (len == sizeof(bytes))
475 continue; /* more to read */
476 break;
477 }
478
479 posix_aio_process_queue(s);
Christoph Hellwig9ef91a62009-08-20 16:58:19 +0200480}
481
482static int posix_aio_flush(void *opaque)
483{
484 PosixAioState *s = opaque;
485 return !!s->first_aio;
486}
487
488static PosixAioState *posix_aio_state;
489
490static void aio_signal_handler(int signum)
491{
492 if (posix_aio_state) {
493 char byte = 0;
494
495 write(posix_aio_state->wfd, &byte, sizeof(byte));
496 }
497
498 qemu_service_io();
499}
500
501static void paio_remove(struct qemu_paiocb *acb)
502{
503 struct qemu_paiocb **pacb;
504
505 /* remove the callback from the queue */
506 pacb = &posix_aio_state->first_aio;
507 for(;;) {
508 if (*pacb == NULL) {
509 fprintf(stderr, "paio_remove: aio request not found!\n");
510 break;
511 } else if (*pacb == acb) {
512 *pacb = acb->next;
513 qemu_aio_release(acb);
514 break;
515 }
516 pacb = &(*pacb)->next;
517 }
518}
519
520static void paio_cancel(BlockDriverAIOCB *blockacb)
521{
522 struct qemu_paiocb *acb = (struct qemu_paiocb *)blockacb;
523 int active = 0;
aliguori3c529d92008-12-12 16:41:40 +0000524
malc8653c012009-02-21 05:48:11 +0000525 mutex_lock(&lock);
Christoph Hellwig9ef91a62009-08-20 16:58:19 +0200526 if (!acb->active) {
Blue Swirl72cf2d42009-09-12 07:36:22 +0000527 QTAILQ_REMOVE(&request_list, acb, node);
Christoph Hellwig9ef91a62009-08-20 16:58:19 +0200528 acb->ret = -ECANCELED;
529 } else if (acb->ret == -EINPROGRESS) {
530 active = 1;
531 }
malc8653c012009-02-21 05:48:11 +0000532 mutex_unlock(&lock);
aliguori3c529d92008-12-12 16:41:40 +0000533
Christoph Hellwig9ef91a62009-08-20 16:58:19 +0200534 if (active) {
535 /* fail safe: if the aio could not be canceled, we wait for
536 it */
537 while (qemu_paio_error(acb) == EINPROGRESS)
538 ;
539 }
540
541 paio_remove(acb);
542}
543
544static AIOPool raw_aio_pool = {
545 .aiocb_size = sizeof(struct qemu_paiocb),
546 .cancel = paio_cancel,
547};
548
549BlockDriverAIOCB *paio_submit(BlockDriverState *bs, void *aio_ctx, int fd,
550 int64_t sector_num, QEMUIOVector *qiov, int nb_sectors,
551 BlockDriverCompletionFunc *cb, void *opaque, int type)
552{
553 struct qemu_paiocb *acb;
554
555 acb = qemu_aio_get(&raw_aio_pool, bs, cb, opaque);
556 if (!acb)
557 return NULL;
558 acb->aio_type = type;
559 acb->aio_fildes = fd;
560 acb->ev_signo = SIGUSR2;
Christoph Hellwigb2e12bc2009-09-04 19:01:49 +0200561 if (qiov) {
562 acb->aio_iov = qiov->iov;
563 acb->aio_niov = qiov->niov;
564 }
Christoph Hellwig9ef91a62009-08-20 16:58:19 +0200565 acb->aio_nbytes = nb_sectors * 512;
566 acb->aio_offset = sector_num * 512;
567
568 acb->next = posix_aio_state->first_aio;
569 posix_aio_state->first_aio = acb;
570
571 qemu_paio_submit(acb);
572 return &acb->common;
573}
574
575BlockDriverAIOCB *paio_ioctl(BlockDriverState *bs, int fd,
576 unsigned long int req, void *buf,
577 BlockDriverCompletionFunc *cb, void *opaque)
578{
579 struct qemu_paiocb *acb;
580
581 acb = qemu_aio_get(&raw_aio_pool, bs, cb, opaque);
582 if (!acb)
583 return NULL;
584 acb->aio_type = QEMU_AIO_IOCTL;
585 acb->aio_fildes = fd;
586 acb->ev_signo = SIGUSR2;
587 acb->aio_offset = 0;
588 acb->aio_ioctl_buf = buf;
589 acb->aio_ioctl_cmd = req;
590
591 acb->next = posix_aio_state->first_aio;
592 posix_aio_state->first_aio = acb;
593
594 qemu_paio_submit(acb);
595 return &acb->common;
596}
597
598void *paio_init(void)
599{
600 struct sigaction act;
601 PosixAioState *s;
602 int fds[2];
603 int ret;
604
605 if (posix_aio_state)
606 return posix_aio_state;
607
608 s = qemu_malloc(sizeof(PosixAioState));
609
610 sigfillset(&act.sa_mask);
611 act.sa_flags = 0; /* do not restart syscalls to interrupt select() */
612 act.sa_handler = aio_signal_handler;
613 sigaction(SIGUSR2, &act, NULL);
614
615 s->first_aio = NULL;
616 if (pipe(fds) == -1) {
617 fprintf(stderr, "failed to create pipe\n");
618 return NULL;
619 }
620
621 s->rfd = fds[0];
622 s->wfd = fds[1];
623
624 fcntl(s->rfd, F_SETFL, O_NONBLOCK);
625 fcntl(s->wfd, F_SETFL, O_NONBLOCK);
626
Kevin Wolf8febfa22009-10-22 17:54:36 +0200627 qemu_aio_set_fd_handler(s->rfd, posix_aio_read, NULL, posix_aio_flush,
628 posix_aio_process_queue, s);
Christoph Hellwig9ef91a62009-08-20 16:58:19 +0200629
630 ret = pthread_attr_init(&attr);
631 if (ret)
632 die2(ret, "pthread_attr_init");
633
634 ret = pthread_attr_setdetachstate(&attr, PTHREAD_CREATE_DETACHED);
635 if (ret)
636 die2(ret, "pthread_attr_setdetachstate");
637
Blue Swirl72cf2d42009-09-12 07:36:22 +0000638 QTAILQ_INIT(&request_list);
Christoph Hellwig9ef91a62009-08-20 16:58:19 +0200639
640 posix_aio_state = s;
641
642 return posix_aio_state;
aliguori3c529d92008-12-12 16:41:40 +0000643}