blob: 7b862b5400377b932c374de0fc9eb049e7520330 [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"
Stefan Hajnoczi6d519a52010-05-22 18:15:08 +010028#include "trace.h"
Christoph Hellwig9ef91a62009-08-20 16:58:19 +020029#include "block_int.h"
aliguori3c529d92008-12-12 16:41:40 +000030
Christoph Hellwig9ef91a62009-08-20 16:58:19 +020031#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;
Stefan Hajnoczib587a522010-05-27 12:52:08 +010039 void *aio_ioctl_buf;
Christoph Hellwig9ef91a62009-08-20 16:58:19 +020040 };
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
Blue Swirl72cf2d42009-09-12 07:36:22 +000047 QTAILQ_ENTRY(qemu_paiocb) node;
Christoph Hellwig9ef91a62009-08-20 16:58:19 +020048 int aio_type;
49 ssize_t ret;
50 int active;
51 struct qemu_paiocb *next;
Kevin Wolfe5f37642009-10-22 17:54:40 +020052
53 int async_context_id;
Christoph Hellwig9ef91a62009-08-20 16:58:19 +020054};
55
56typedef struct PosixAioState {
57 int rfd, wfd;
58 struct qemu_paiocb *first_aio;
59} PosixAioState;
60
aliguori3c529d92008-12-12 16:41:40 +000061
62static pthread_mutex_t lock = PTHREAD_MUTEX_INITIALIZER;
63static pthread_cond_t cond = PTHREAD_COND_INITIALIZER;
64static pthread_t thread_id;
malca8227a52009-02-21 05:48:17 +000065static pthread_attr_t attr;
aliguori3c529d92008-12-12 16:41:40 +000066static int max_threads = 64;
67static int cur_threads = 0;
68static int idle_threads = 0;
Blue Swirl72cf2d42009-09-12 07:36:22 +000069static QTAILQ_HEAD(, qemu_paiocb) request_list;
aliguori3c529d92008-12-12 16:41:40 +000070
Juan Quintela2341f9a2009-07-27 16:12:58 +020071#ifdef CONFIG_PREADV
aliguoriceb42de2009-04-07 18:43:28 +000072static int preadv_present = 1;
73#else
74static int preadv_present = 0;
75#endif
76
malc8653c012009-02-21 05:48:11 +000077static void die2(int err, const char *what)
78{
79 fprintf(stderr, "%s failed: %s\n", what, strerror(err));
80 abort();
81}
82
83static void die(const char *what)
84{
85 die2(errno, what);
86}
87
88static void mutex_lock(pthread_mutex_t *mutex)
89{
90 int ret = pthread_mutex_lock(mutex);
91 if (ret) die2(ret, "pthread_mutex_lock");
92}
93
94static void mutex_unlock(pthread_mutex_t *mutex)
95{
96 int ret = pthread_mutex_unlock(mutex);
97 if (ret) die2(ret, "pthread_mutex_unlock");
98}
99
100static int cond_timedwait(pthread_cond_t *cond, pthread_mutex_t *mutex,
101 struct timespec *ts)
102{
103 int ret = pthread_cond_timedwait(cond, mutex, ts);
104 if (ret && ret != ETIMEDOUT) die2(ret, "pthread_cond_timedwait");
105 return ret;
106}
107
malc5d47e372009-02-21 05:48:15 +0000108static void cond_signal(pthread_cond_t *cond)
malc8653c012009-02-21 05:48:11 +0000109{
malc5d47e372009-02-21 05:48:15 +0000110 int ret = pthread_cond_signal(cond);
111 if (ret) die2(ret, "pthread_cond_signal");
malc8653c012009-02-21 05:48:11 +0000112}
113
114static void thread_create(pthread_t *thread, pthread_attr_t *attr,
115 void *(*start_routine)(void*), void *arg)
116{
117 int ret = pthread_create(thread, attr, start_routine, arg);
118 if (ret) die2(ret, "pthread_create");
119}
120
Kevin Wolf6769da22009-11-18 12:15:10 +0100121static ssize_t handle_aiocb_ioctl(struct qemu_paiocb *aiocb)
aliguorif141eaf2009-04-07 18:43:24 +0000122{
Stefan Hajnoczib587a522010-05-27 12:52:08 +0100123 int ret;
aliguorif141eaf2009-04-07 18:43:24 +0000124
Stefan Hajnoczib587a522010-05-27 12:52:08 +0100125 ret = ioctl(aiocb->aio_fildes, aiocb->aio_ioctl_cmd, aiocb->aio_ioctl_buf);
126 if (ret == -1)
127 return -errno;
Christoph Hellwige7d54ae2009-04-28 11:57:02 +0200128
Stefan Hajnoczib587a522010-05-27 12:52:08 +0100129 /*
130 * This looks weird, but the aio code only consideres a request
Stefan Weilb0cd7122010-08-06 21:53:45 +0200131 * successful if it has written the number full number of bytes.
Stefan Hajnoczib587a522010-05-27 12:52:08 +0100132 *
133 * Now we overload aio_nbytes as aio_ioctl_cmd for the ioctl command,
134 * so in fact we return the ioctl command here to make posix_aio_read()
135 * happy..
136 */
137 return aiocb->aio_nbytes;
aliguorif141eaf2009-04-07 18:43:24 +0000138}
139
Kevin Wolf6769da22009-11-18 12:15:10 +0100140static ssize_t handle_aiocb_flush(struct qemu_paiocb *aiocb)
Christoph Hellwigb2e12bc2009-09-04 19:01:49 +0200141{
142 int ret;
143
Blue Swirl47faadc2009-09-12 06:19:14 +0000144 ret = qemu_fdatasync(aiocb->aio_fildes);
Christoph Hellwigb2e12bc2009-09-04 19:01:49 +0200145 if (ret == -1)
146 return -errno;
147 return 0;
148}
149
Juan Quintela2341f9a2009-07-27 16:12:58 +0200150#ifdef CONFIG_PREADV
aliguoriceb42de2009-04-07 18:43:28 +0000151
152static ssize_t
153qemu_preadv(int fd, const struct iovec *iov, int nr_iov, off_t offset)
154{
155 return preadv(fd, iov, nr_iov, offset);
156}
157
158static ssize_t
159qemu_pwritev(int fd, const struct iovec *iov, int nr_iov, off_t offset)
160{
161 return pwritev(fd, iov, nr_iov, offset);
162}
163
164#else
165
166static ssize_t
167qemu_preadv(int fd, const struct iovec *iov, int nr_iov, off_t offset)
168{
169 return -ENOSYS;
170}
171
172static ssize_t
173qemu_pwritev(int fd, const struct iovec *iov, int nr_iov, off_t offset)
174{
175 return -ENOSYS;
176}
177
178#endif
179
Kevin Wolf6769da22009-11-18 12:15:10 +0100180static ssize_t handle_aiocb_rw_vector(struct qemu_paiocb *aiocb)
aliguoriceb42de2009-04-07 18:43:28 +0000181{
182 size_t offset = 0;
183 ssize_t len;
184
185 do {
Christoph Hellwig9ef91a62009-08-20 16:58:19 +0200186 if (aiocb->aio_type & QEMU_AIO_WRITE)
aliguoriceb42de2009-04-07 18:43:28 +0000187 len = qemu_pwritev(aiocb->aio_fildes,
188 aiocb->aio_iov,
189 aiocb->aio_niov,
190 aiocb->aio_offset + offset);
191 else
192 len = qemu_preadv(aiocb->aio_fildes,
193 aiocb->aio_iov,
194 aiocb->aio_niov,
195 aiocb->aio_offset + offset);
196 } while (len == -1 && errno == EINTR);
197
198 if (len == -1)
199 return -errno;
200 return len;
201}
202
Kevin Wolf6769da22009-11-18 12:15:10 +0100203static ssize_t handle_aiocb_rw_linear(struct qemu_paiocb *aiocb, char *buf)
aliguori221f7152009-03-28 17:28:41 +0000204{
Kevin Wolf6769da22009-11-18 12:15:10 +0100205 ssize_t offset = 0;
206 ssize_t len;
aliguori221f7152009-03-28 17:28:41 +0000207
208 while (offset < aiocb->aio_nbytes) {
Christoph Hellwig9ef91a62009-08-20 16:58:19 +0200209 if (aiocb->aio_type & QEMU_AIO_WRITE)
aliguorif141eaf2009-04-07 18:43:24 +0000210 len = pwrite(aiocb->aio_fildes,
211 (const char *)buf + offset,
212 aiocb->aio_nbytes - offset,
213 aiocb->aio_offset + offset);
214 else
215 len = pread(aiocb->aio_fildes,
216 buf + offset,
aliguori221f7152009-03-28 17:28:41 +0000217 aiocb->aio_nbytes - offset,
218 aiocb->aio_offset + offset);
aliguori221f7152009-03-28 17:28:41 +0000219
aliguorif141eaf2009-04-07 18:43:24 +0000220 if (len == -1 && errno == EINTR)
221 continue;
222 else if (len == -1) {
223 offset = -errno;
224 break;
225 } else if (len == 0)
226 break;
aliguori221f7152009-03-28 17:28:41 +0000227
aliguorif141eaf2009-04-07 18:43:24 +0000228 offset += len;
aliguori221f7152009-03-28 17:28:41 +0000229 }
230
231 return offset;
232}
233
Kevin Wolf6769da22009-11-18 12:15:10 +0100234static ssize_t handle_aiocb_rw(struct qemu_paiocb *aiocb)
aliguori221f7152009-03-28 17:28:41 +0000235{
Kevin Wolf6769da22009-11-18 12:15:10 +0100236 ssize_t nbytes;
aliguorif141eaf2009-04-07 18:43:24 +0000237 char *buf;
aliguori221f7152009-03-28 17:28:41 +0000238
Christoph Hellwig9ef91a62009-08-20 16:58:19 +0200239 if (!(aiocb->aio_type & QEMU_AIO_MISALIGNED)) {
aliguorif141eaf2009-04-07 18:43:24 +0000240 /*
241 * If there is just a single buffer, and it is properly aligned
242 * we can just use plain pread/pwrite without any problems.
243 */
aliguoriceb42de2009-04-07 18:43:28 +0000244 if (aiocb->aio_niov == 1)
245 return handle_aiocb_rw_linear(aiocb, aiocb->aio_iov->iov_base);
246
247 /*
248 * We have more than one iovec, and all are properly aligned.
249 *
250 * Try preadv/pwritev first and fall back to linearizing the
251 * buffer if it's not supported.
252 */
Stefan Hajnoczib587a522010-05-27 12:52:08 +0100253 if (preadv_present) {
aliguoriceb42de2009-04-07 18:43:28 +0000254 nbytes = handle_aiocb_rw_vector(aiocb);
255 if (nbytes == aiocb->aio_nbytes)
Stefan Hajnoczib587a522010-05-27 12:52:08 +0100256 return nbytes;
aliguoriceb42de2009-04-07 18:43:28 +0000257 if (nbytes < 0 && nbytes != -ENOSYS)
258 return nbytes;
259 preadv_present = 0;
260 }
261
262 /*
263 * XXX(hch): short read/write. no easy way to handle the reminder
264 * using these interfaces. For now retry using plain
265 * pread/pwrite?
266 */
aliguorif141eaf2009-04-07 18:43:24 +0000267 }
268
269 /*
270 * Ok, we have to do it the hard way, copy all segments into
271 * a single aligned buffer.
272 */
Christoph Hellwig72aef732010-09-12 23:42:56 +0200273 buf = qemu_blockalign(aiocb->common.bs, aiocb->aio_nbytes);
Christoph Hellwig9ef91a62009-08-20 16:58:19 +0200274 if (aiocb->aio_type & QEMU_AIO_WRITE) {
aliguorif141eaf2009-04-07 18:43:24 +0000275 char *p = buf;
276 int i;
277
278 for (i = 0; i < aiocb->aio_niov; ++i) {
279 memcpy(p, aiocb->aio_iov[i].iov_base, aiocb->aio_iov[i].iov_len);
280 p += aiocb->aio_iov[i].iov_len;
281 }
282 }
283
284 nbytes = handle_aiocb_rw_linear(aiocb, buf);
Christoph Hellwig9ef91a62009-08-20 16:58:19 +0200285 if (!(aiocb->aio_type & QEMU_AIO_WRITE)) {
aliguorif141eaf2009-04-07 18:43:24 +0000286 char *p = buf;
287 size_t count = aiocb->aio_nbytes, copy;
288 int i;
289
290 for (i = 0; i < aiocb->aio_niov && count; ++i) {
291 copy = count;
292 if (copy > aiocb->aio_iov[i].iov_len)
293 copy = aiocb->aio_iov[i].iov_len;
294 memcpy(aiocb->aio_iov[i].iov_base, p, copy);
295 p += copy;
296 count -= copy;
297 }
298 }
299 qemu_vfree(buf);
300
301 return nbytes;
aliguori221f7152009-03-28 17:28:41 +0000302}
303
aliguori3c529d92008-12-12 16:41:40 +0000304static void *aio_thread(void *unused)
305{
malca8227a52009-02-21 05:48:17 +0000306 pid_t pid;
aliguori3c529d92008-12-12 16:41:40 +0000307
malca8227a52009-02-21 05:48:17 +0000308 pid = getpid();
309
aliguori3c529d92008-12-12 16:41:40 +0000310 while (1) {
311 struct qemu_paiocb *aiocb;
Kevin Wolf6769da22009-11-18 12:15:10 +0100312 ssize_t ret = 0;
malc30525af2009-02-21 05:48:13 +0000313 qemu_timeval tv;
314 struct timespec ts;
315
316 qemu_gettimeofday(&tv);
317 ts.tv_sec = tv.tv_sec + 10;
318 ts.tv_nsec = 0;
aliguori3c529d92008-12-12 16:41:40 +0000319
malc8653c012009-02-21 05:48:11 +0000320 mutex_lock(&lock);
aliguori3c529d92008-12-12 16:41:40 +0000321
Blue Swirl72cf2d42009-09-12 07:36:22 +0000322 while (QTAILQ_EMPTY(&request_list) &&
aliguori3c529d92008-12-12 16:41:40 +0000323 !(ret == ETIMEDOUT)) {
malc8653c012009-02-21 05:48:11 +0000324 ret = cond_timedwait(&cond, &lock, &ts);
aliguori3c529d92008-12-12 16:41:40 +0000325 }
326
Blue Swirl72cf2d42009-09-12 07:36:22 +0000327 if (QTAILQ_EMPTY(&request_list))
aliguori3c529d92008-12-12 16:41:40 +0000328 break;
329
Blue Swirl72cf2d42009-09-12 07:36:22 +0000330 aiocb = QTAILQ_FIRST(&request_list);
331 QTAILQ_REMOVE(&request_list, aiocb, node);
aliguori3c529d92008-12-12 16:41:40 +0000332 aiocb->active = 1;
aliguori3c529d92008-12-12 16:41:40 +0000333 idle_threads--;
malc8653c012009-02-21 05:48:11 +0000334 mutex_unlock(&lock);
aliguori3c529d92008-12-12 16:41:40 +0000335
Christoph Hellwig9ef91a62009-08-20 16:58:19 +0200336 switch (aiocb->aio_type & QEMU_AIO_TYPE_MASK) {
337 case QEMU_AIO_READ:
338 case QEMU_AIO_WRITE:
Stefan Hajnoczib587a522010-05-27 12:52:08 +0100339 ret = handle_aiocb_rw(aiocb);
340 break;
Christoph Hellwigb2e12bc2009-09-04 19:01:49 +0200341 case QEMU_AIO_FLUSH:
Stefan Hajnoczib587a522010-05-27 12:52:08 +0100342 ret = handle_aiocb_flush(aiocb);
343 break;
Christoph Hellwig9ef91a62009-08-20 16:58:19 +0200344 case QEMU_AIO_IOCTL:
Stefan Hajnoczib587a522010-05-27 12:52:08 +0100345 ret = handle_aiocb_ioctl(aiocb);
346 break;
347 default:
348 fprintf(stderr, "invalid aio request (0x%x)\n", aiocb->aio_type);
349 ret = -EINVAL;
350 break;
351 }
aliguori3c529d92008-12-12 16:41:40 +0000352
malc8653c012009-02-21 05:48:11 +0000353 mutex_lock(&lock);
aliguori221f7152009-03-28 17:28:41 +0000354 aiocb->ret = ret;
aliguori3c529d92008-12-12 16:41:40 +0000355 idle_threads++;
malc8653c012009-02-21 05:48:11 +0000356 mutex_unlock(&lock);
aliguori3c529d92008-12-12 16:41:40 +0000357
malca8227a52009-02-21 05:48:17 +0000358 if (kill(pid, aiocb->ev_signo)) die("kill failed");
aliguori3c529d92008-12-12 16:41:40 +0000359 }
360
361 idle_threads--;
362 cur_threads--;
malc8653c012009-02-21 05:48:11 +0000363 mutex_unlock(&lock);
aliguori3c529d92008-12-12 16:41:40 +0000364
365 return NULL;
366}
367
malc8653c012009-02-21 05:48:11 +0000368static void spawn_thread(void)
aliguori3c529d92008-12-12 16:41:40 +0000369{
malcee399302009-09-25 00:20:44 +0400370 sigset_t set, oldset;
371
aliguori3c529d92008-12-12 16:41:40 +0000372 cur_threads++;
373 idle_threads++;
malcee399302009-09-25 00:20:44 +0400374
375 /* block all signals */
376 if (sigfillset(&set)) die("sigfillset");
377 if (sigprocmask(SIG_SETMASK, &set, &oldset)) die("sigprocmask");
378
malc8653c012009-02-21 05:48:11 +0000379 thread_create(&thread_id, &attr, aio_thread, NULL);
malcee399302009-09-25 00:20:44 +0400380
381 if (sigprocmask(SIG_SETMASK, &oldset, NULL)) die("sigprocmask restore");
aliguori3c529d92008-12-12 16:41:40 +0000382}
383
Christoph Hellwig9ef91a62009-08-20 16:58:19 +0200384static void qemu_paio_submit(struct qemu_paiocb *aiocb)
aliguori3c529d92008-12-12 16:41:40 +0000385{
aliguori3c529d92008-12-12 16:41:40 +0000386 aiocb->ret = -EINPROGRESS;
387 aiocb->active = 0;
malc8653c012009-02-21 05:48:11 +0000388 mutex_lock(&lock);
aliguori3c529d92008-12-12 16:41:40 +0000389 if (idle_threads == 0 && cur_threads < max_threads)
390 spawn_thread();
Blue Swirl72cf2d42009-09-12 07:36:22 +0000391 QTAILQ_INSERT_TAIL(&request_list, aiocb, node);
malc8653c012009-02-21 05:48:11 +0000392 mutex_unlock(&lock);
malc5d47e372009-02-21 05:48:15 +0000393 cond_signal(&cond);
aliguori3c529d92008-12-12 16:41:40 +0000394}
395
Christoph Hellwig9ef91a62009-08-20 16:58:19 +0200396static ssize_t qemu_paio_return(struct qemu_paiocb *aiocb)
aliguori3c529d92008-12-12 16:41:40 +0000397{
398 ssize_t ret;
399
malc8653c012009-02-21 05:48:11 +0000400 mutex_lock(&lock);
aliguori3c529d92008-12-12 16:41:40 +0000401 ret = aiocb->ret;
malc8653c012009-02-21 05:48:11 +0000402 mutex_unlock(&lock);
aliguori3c529d92008-12-12 16:41:40 +0000403
404 return ret;
405}
406
Christoph Hellwig9ef91a62009-08-20 16:58:19 +0200407static int qemu_paio_error(struct qemu_paiocb *aiocb)
aliguori3c529d92008-12-12 16:41:40 +0000408{
409 ssize_t ret = qemu_paio_return(aiocb);
410
411 if (ret < 0)
412 ret = -ret;
413 else
414 ret = 0;
415
416 return ret;
417}
418
Kevin Wolf59c7b152009-10-22 17:54:35 +0200419static int posix_aio_process_queue(void *opaque)
aliguori3c529d92008-12-12 16:41:40 +0000420{
Christoph Hellwig9ef91a62009-08-20 16:58:19 +0200421 PosixAioState *s = opaque;
422 struct qemu_paiocb *acb, **pacb;
aliguori3c529d92008-12-12 16:41:40 +0000423 int ret;
Kevin Wolf59c7b152009-10-22 17:54:35 +0200424 int result = 0;
Kevin Wolfe5f37642009-10-22 17:54:40 +0200425 int async_context_id = get_async_context_id();
Christoph Hellwig9ef91a62009-08-20 16:58:19 +0200426
427 for(;;) {
428 pacb = &s->first_aio;
429 for(;;) {
430 acb = *pacb;
431 if (!acb)
Kevin Wolf59c7b152009-10-22 17:54:35 +0200432 return result;
Kevin Wolfe5f37642009-10-22 17:54:40 +0200433
434 /* we're only interested in requests in the right context */
435 if (acb->async_context_id != async_context_id) {
436 pacb = &acb->next;
437 continue;
438 }
439
Christoph Hellwig9ef91a62009-08-20 16:58:19 +0200440 ret = qemu_paio_error(acb);
441 if (ret == ECANCELED) {
442 /* remove the request */
443 *pacb = acb->next;
444 qemu_aio_release(acb);
Kevin Wolf59c7b152009-10-22 17:54:35 +0200445 result = 1;
Christoph Hellwig9ef91a62009-08-20 16:58:19 +0200446 } else if (ret != EINPROGRESS) {
447 /* end of aio */
448 if (ret == 0) {
449 ret = qemu_paio_return(acb);
450 if (ret == acb->aio_nbytes)
451 ret = 0;
452 else
453 ret = -EINVAL;
454 } else {
455 ret = -ret;
456 }
457 /* remove the request */
458 *pacb = acb->next;
459 /* call the callback */
460 acb->common.cb(acb->common.opaque, ret);
461 qemu_aio_release(acb);
Kevin Wolf59c7b152009-10-22 17:54:35 +0200462 result = 1;
Christoph Hellwig9ef91a62009-08-20 16:58:19 +0200463 break;
464 } else {
465 pacb = &acb->next;
466 }
467 }
468 }
Kevin Wolf59c7b152009-10-22 17:54:35 +0200469
470 return result;
471}
472
473static void posix_aio_read(void *opaque)
474{
475 PosixAioState *s = opaque;
476 ssize_t len;
477
478 /* read all bytes from signal pipe */
479 for (;;) {
480 char bytes[16];
481
482 len = read(s->rfd, bytes, sizeof(bytes));
483 if (len == -1 && errno == EINTR)
484 continue; /* try again */
485 if (len == sizeof(bytes))
486 continue; /* more to read */
487 break;
488 }
489
490 posix_aio_process_queue(s);
Christoph Hellwig9ef91a62009-08-20 16:58:19 +0200491}
492
493static int posix_aio_flush(void *opaque)
494{
495 PosixAioState *s = opaque;
496 return !!s->first_aio;
497}
498
499static PosixAioState *posix_aio_state;
500
501static void aio_signal_handler(int signum)
502{
503 if (posix_aio_state) {
504 char byte = 0;
Kirill A. Shutemov4817d322010-01-20 00:56:10 +0100505 ssize_t ret;
Christoph Hellwig9ef91a62009-08-20 16:58:19 +0200506
Kirill A. Shutemov4817d322010-01-20 00:56:10 +0100507 ret = write(posix_aio_state->wfd, &byte, sizeof(byte));
508 if (ret < 0 && errno != EAGAIN)
509 die("write()");
Christoph Hellwig9ef91a62009-08-20 16:58:19 +0200510 }
511
512 qemu_service_io();
513}
514
515static void paio_remove(struct qemu_paiocb *acb)
516{
517 struct qemu_paiocb **pacb;
518
519 /* remove the callback from the queue */
520 pacb = &posix_aio_state->first_aio;
521 for(;;) {
522 if (*pacb == NULL) {
523 fprintf(stderr, "paio_remove: aio request not found!\n");
524 break;
525 } else if (*pacb == acb) {
526 *pacb = acb->next;
527 qemu_aio_release(acb);
528 break;
529 }
530 pacb = &(*pacb)->next;
531 }
532}
533
534static void paio_cancel(BlockDriverAIOCB *blockacb)
535{
536 struct qemu_paiocb *acb = (struct qemu_paiocb *)blockacb;
537 int active = 0;
aliguori3c529d92008-12-12 16:41:40 +0000538
malc8653c012009-02-21 05:48:11 +0000539 mutex_lock(&lock);
Christoph Hellwig9ef91a62009-08-20 16:58:19 +0200540 if (!acb->active) {
Blue Swirl72cf2d42009-09-12 07:36:22 +0000541 QTAILQ_REMOVE(&request_list, acb, node);
Christoph Hellwig9ef91a62009-08-20 16:58:19 +0200542 acb->ret = -ECANCELED;
543 } else if (acb->ret == -EINPROGRESS) {
544 active = 1;
545 }
malc8653c012009-02-21 05:48:11 +0000546 mutex_unlock(&lock);
aliguori3c529d92008-12-12 16:41:40 +0000547
Christoph Hellwig9ef91a62009-08-20 16:58:19 +0200548 if (active) {
549 /* fail safe: if the aio could not be canceled, we wait for
550 it */
551 while (qemu_paio_error(acb) == EINPROGRESS)
552 ;
553 }
554
555 paio_remove(acb);
556}
557
558static AIOPool raw_aio_pool = {
559 .aiocb_size = sizeof(struct qemu_paiocb),
560 .cancel = paio_cancel,
561};
562
Kevin Wolf1e5b9d22009-10-26 13:03:08 +0100563BlockDriverAIOCB *paio_submit(BlockDriverState *bs, int fd,
Christoph Hellwig9ef91a62009-08-20 16:58:19 +0200564 int64_t sector_num, QEMUIOVector *qiov, int nb_sectors,
565 BlockDriverCompletionFunc *cb, void *opaque, int type)
566{
567 struct qemu_paiocb *acb;
568
569 acb = qemu_aio_get(&raw_aio_pool, bs, cb, opaque);
570 if (!acb)
571 return NULL;
572 acb->aio_type = type;
573 acb->aio_fildes = fd;
574 acb->ev_signo = SIGUSR2;
Kevin Wolfe5f37642009-10-22 17:54:40 +0200575 acb->async_context_id = get_async_context_id();
576
Christoph Hellwigb2e12bc2009-09-04 19:01:49 +0200577 if (qiov) {
578 acb->aio_iov = qiov->iov;
579 acb->aio_niov = qiov->niov;
580 }
Christoph Hellwig9ef91a62009-08-20 16:58:19 +0200581 acb->aio_nbytes = nb_sectors * 512;
582 acb->aio_offset = sector_num * 512;
583
584 acb->next = posix_aio_state->first_aio;
585 posix_aio_state->first_aio = acb;
586
Stefan Hajnoczi6d519a52010-05-22 18:15:08 +0100587 trace_paio_submit(acb, opaque, sector_num, nb_sectors, type);
Christoph Hellwig9ef91a62009-08-20 16:58:19 +0200588 qemu_paio_submit(acb);
589 return &acb->common;
590}
591
592BlockDriverAIOCB *paio_ioctl(BlockDriverState *bs, int fd,
593 unsigned long int req, void *buf,
594 BlockDriverCompletionFunc *cb, void *opaque)
595{
596 struct qemu_paiocb *acb;
597
598 acb = qemu_aio_get(&raw_aio_pool, bs, cb, opaque);
599 if (!acb)
600 return NULL;
601 acb->aio_type = QEMU_AIO_IOCTL;
602 acb->aio_fildes = fd;
603 acb->ev_signo = SIGUSR2;
Andrew de Quincey34cf0082010-08-08 21:04:50 +0100604 acb->async_context_id = get_async_context_id();
Christoph Hellwig9ef91a62009-08-20 16:58:19 +0200605 acb->aio_offset = 0;
606 acb->aio_ioctl_buf = buf;
607 acb->aio_ioctl_cmd = req;
608
609 acb->next = posix_aio_state->first_aio;
610 posix_aio_state->first_aio = acb;
611
612 qemu_paio_submit(acb);
613 return &acb->common;
614}
615
Kevin Wolf1e5b9d22009-10-26 13:03:08 +0100616int paio_init(void)
Christoph Hellwig9ef91a62009-08-20 16:58:19 +0200617{
618 struct sigaction act;
619 PosixAioState *s;
620 int fds[2];
621 int ret;
622
623 if (posix_aio_state)
Kevin Wolf1e5b9d22009-10-26 13:03:08 +0100624 return 0;
Christoph Hellwig9ef91a62009-08-20 16:58:19 +0200625
626 s = qemu_malloc(sizeof(PosixAioState));
627
628 sigfillset(&act.sa_mask);
629 act.sa_flags = 0; /* do not restart syscalls to interrupt select() */
630 act.sa_handler = aio_signal_handler;
631 sigaction(SIGUSR2, &act, NULL);
632
633 s->first_aio = NULL;
Kevin Wolf40ff6d72009-12-02 12:24:42 +0100634 if (qemu_pipe(fds) == -1) {
Christoph Hellwig9ef91a62009-08-20 16:58:19 +0200635 fprintf(stderr, "failed to create pipe\n");
Kevin Wolf1e5b9d22009-10-26 13:03:08 +0100636 return -1;
Christoph Hellwig9ef91a62009-08-20 16:58:19 +0200637 }
638
639 s->rfd = fds[0];
640 s->wfd = fds[1];
641
642 fcntl(s->rfd, F_SETFL, O_NONBLOCK);
643 fcntl(s->wfd, F_SETFL, O_NONBLOCK);
644
Kevin Wolf8febfa22009-10-22 17:54:36 +0200645 qemu_aio_set_fd_handler(s->rfd, posix_aio_read, NULL, posix_aio_flush,
646 posix_aio_process_queue, s);
Christoph Hellwig9ef91a62009-08-20 16:58:19 +0200647
648 ret = pthread_attr_init(&attr);
649 if (ret)
650 die2(ret, "pthread_attr_init");
651
652 ret = pthread_attr_setdetachstate(&attr, PTHREAD_CREATE_DETACHED);
653 if (ret)
654 die2(ret, "pthread_attr_setdetachstate");
655
Blue Swirl72cf2d42009-09-12 07:36:22 +0000656 QTAILQ_INIT(&request_list);
Christoph Hellwig9ef91a62009-08-20 16:58:19 +0200657
658 posix_aio_state = s;
Kevin Wolf1e5b9d22009-10-26 13:03:08 +0100659 return 0;
aliguori3c529d92008-12-12 16:41:40 +0000660}