aliguori | 3c529d9 | 2008-12-12 16:41:40 +0000 | [diff] [blame] | 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 | |
aliguori | 221f715 | 2009-03-28 17:28:41 +0000 | [diff] [blame] | 14 | #include <sys/ioctl.h> |
Christoph Hellwig | 9ef91a6 | 2009-08-20 16:58:19 +0200 | [diff] [blame] | 15 | #include <sys/types.h> |
aliguori | 3c529d9 | 2008-12-12 16:41:40 +0000 | [diff] [blame] | 16 | #include <pthread.h> |
| 17 | #include <unistd.h> |
| 18 | #include <errno.h> |
malc | 30525af | 2009-02-21 05:48:13 +0000 | [diff] [blame] | 19 | #include <time.h> |
malc | 8653c01 | 2009-02-21 05:48:11 +0000 | [diff] [blame] | 20 | #include <string.h> |
| 21 | #include <stdlib.h> |
| 22 | #include <stdio.h> |
Christoph Hellwig | 9ef91a6 | 2009-08-20 16:58:19 +0200 | [diff] [blame] | 23 | |
Blue Swirl | 72cf2d4 | 2009-09-12 07:36:22 +0000 | [diff] [blame] | 24 | #include "qemu-queue.h" |
aliguori | 3c529d9 | 2008-12-12 16:41:40 +0000 | [diff] [blame] | 25 | #include "osdep.h" |
Jes Sorensen | dc786bc | 2010-10-26 10:39:23 +0200 | [diff] [blame] | 26 | #include "sysemu.h" |
aliguori | f141eaf | 2009-04-07 18:43:24 +0000 | [diff] [blame] | 27 | #include "qemu-common.h" |
Stefan Hajnoczi | 6d519a5 | 2010-05-22 18:15:08 +0100 | [diff] [blame] | 28 | #include "trace.h" |
Christoph Hellwig | 9ef91a6 | 2009-08-20 16:58:19 +0200 | [diff] [blame] | 29 | #include "block_int.h" |
aliguori | 3c529d9 | 2008-12-12 16:41:40 +0000 | [diff] [blame] | 30 | |
Christoph Hellwig | 9ef91a6 | 2009-08-20 16:58:19 +0200 | [diff] [blame] | 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; |
Stefan Hajnoczi | b587a52 | 2010-05-27 12:52:08 +0100 | [diff] [blame] | 39 | void *aio_ioctl_buf; |
Christoph Hellwig | 9ef91a6 | 2009-08-20 16:58:19 +0200 | [diff] [blame] | 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 | |
Blue Swirl | 72cf2d4 | 2009-09-12 07:36:22 +0000 | [diff] [blame] | 47 | QTAILQ_ENTRY(qemu_paiocb) node; |
Christoph Hellwig | 9ef91a6 | 2009-08-20 16:58:19 +0200 | [diff] [blame] | 48 | int aio_type; |
| 49 | ssize_t ret; |
| 50 | int active; |
| 51 | struct qemu_paiocb *next; |
Kevin Wolf | e5f3764 | 2009-10-22 17:54:40 +0200 | [diff] [blame] | 52 | |
| 53 | int async_context_id; |
Christoph Hellwig | 9ef91a6 | 2009-08-20 16:58:19 +0200 | [diff] [blame] | 54 | }; |
| 55 | |
| 56 | typedef struct PosixAioState { |
| 57 | int rfd, wfd; |
| 58 | struct qemu_paiocb *first_aio; |
| 59 | } PosixAioState; |
| 60 | |
aliguori | 3c529d9 | 2008-12-12 16:41:40 +0000 | [diff] [blame] | 61 | |
| 62 | static pthread_mutex_t lock = PTHREAD_MUTEX_INITIALIZER; |
| 63 | static pthread_cond_t cond = PTHREAD_COND_INITIALIZER; |
| 64 | static pthread_t thread_id; |
malc | a8227a5 | 2009-02-21 05:48:17 +0000 | [diff] [blame] | 65 | static pthread_attr_t attr; |
aliguori | 3c529d9 | 2008-12-12 16:41:40 +0000 | [diff] [blame] | 66 | static int max_threads = 64; |
| 67 | static int cur_threads = 0; |
| 68 | static int idle_threads = 0; |
Blue Swirl | 72cf2d4 | 2009-09-12 07:36:22 +0000 | [diff] [blame] | 69 | static QTAILQ_HEAD(, qemu_paiocb) request_list; |
aliguori | 3c529d9 | 2008-12-12 16:41:40 +0000 | [diff] [blame] | 70 | |
Juan Quintela | 2341f9a | 2009-07-27 16:12:58 +0200 | [diff] [blame] | 71 | #ifdef CONFIG_PREADV |
aliguori | ceb42de | 2009-04-07 18:43:28 +0000 | [diff] [blame] | 72 | static int preadv_present = 1; |
| 73 | #else |
| 74 | static int preadv_present = 0; |
| 75 | #endif |
| 76 | |
malc | 8653c01 | 2009-02-21 05:48:11 +0000 | [diff] [blame] | 77 | static void die2(int err, const char *what) |
| 78 | { |
| 79 | fprintf(stderr, "%s failed: %s\n", what, strerror(err)); |
| 80 | abort(); |
| 81 | } |
| 82 | |
| 83 | static void die(const char *what) |
| 84 | { |
| 85 | die2(errno, what); |
| 86 | } |
| 87 | |
| 88 | static 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 | |
| 94 | static 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 | |
| 100 | static 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 | |
malc | 5d47e37 | 2009-02-21 05:48:15 +0000 | [diff] [blame] | 108 | static void cond_signal(pthread_cond_t *cond) |
malc | 8653c01 | 2009-02-21 05:48:11 +0000 | [diff] [blame] | 109 | { |
malc | 5d47e37 | 2009-02-21 05:48:15 +0000 | [diff] [blame] | 110 | int ret = pthread_cond_signal(cond); |
| 111 | if (ret) die2(ret, "pthread_cond_signal"); |
malc | 8653c01 | 2009-02-21 05:48:11 +0000 | [diff] [blame] | 112 | } |
| 113 | |
| 114 | static 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 Wolf | 6769da2 | 2009-11-18 12:15:10 +0100 | [diff] [blame] | 121 | static ssize_t handle_aiocb_ioctl(struct qemu_paiocb *aiocb) |
aliguori | f141eaf | 2009-04-07 18:43:24 +0000 | [diff] [blame] | 122 | { |
Stefan Hajnoczi | b587a52 | 2010-05-27 12:52:08 +0100 | [diff] [blame] | 123 | int ret; |
aliguori | f141eaf | 2009-04-07 18:43:24 +0000 | [diff] [blame] | 124 | |
Stefan Hajnoczi | b587a52 | 2010-05-27 12:52:08 +0100 | [diff] [blame] | 125 | ret = ioctl(aiocb->aio_fildes, aiocb->aio_ioctl_cmd, aiocb->aio_ioctl_buf); |
| 126 | if (ret == -1) |
| 127 | return -errno; |
Christoph Hellwig | e7d54ae | 2009-04-28 11:57:02 +0200 | [diff] [blame] | 128 | |
Stefan Hajnoczi | b587a52 | 2010-05-27 12:52:08 +0100 | [diff] [blame] | 129 | /* |
| 130 | * This looks weird, but the aio code only consideres a request |
Stefan Weil | b0cd712 | 2010-08-06 21:53:45 +0200 | [diff] [blame] | 131 | * successful if it has written the number full number of bytes. |
Stefan Hajnoczi | b587a52 | 2010-05-27 12:52:08 +0100 | [diff] [blame] | 132 | * |
| 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; |
aliguori | f141eaf | 2009-04-07 18:43:24 +0000 | [diff] [blame] | 138 | } |
| 139 | |
Kevin Wolf | 6769da2 | 2009-11-18 12:15:10 +0100 | [diff] [blame] | 140 | static ssize_t handle_aiocb_flush(struct qemu_paiocb *aiocb) |
Christoph Hellwig | b2e12bc | 2009-09-04 19:01:49 +0200 | [diff] [blame] | 141 | { |
| 142 | int ret; |
| 143 | |
Blue Swirl | 47faadc | 2009-09-12 06:19:14 +0000 | [diff] [blame] | 144 | ret = qemu_fdatasync(aiocb->aio_fildes); |
Christoph Hellwig | b2e12bc | 2009-09-04 19:01:49 +0200 | [diff] [blame] | 145 | if (ret == -1) |
| 146 | return -errno; |
| 147 | return 0; |
| 148 | } |
| 149 | |
Juan Quintela | 2341f9a | 2009-07-27 16:12:58 +0200 | [diff] [blame] | 150 | #ifdef CONFIG_PREADV |
aliguori | ceb42de | 2009-04-07 18:43:28 +0000 | [diff] [blame] | 151 | |
| 152 | static ssize_t |
| 153 | qemu_preadv(int fd, const struct iovec *iov, int nr_iov, off_t offset) |
| 154 | { |
| 155 | return preadv(fd, iov, nr_iov, offset); |
| 156 | } |
| 157 | |
| 158 | static ssize_t |
| 159 | qemu_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 | |
| 166 | static ssize_t |
| 167 | qemu_preadv(int fd, const struct iovec *iov, int nr_iov, off_t offset) |
| 168 | { |
| 169 | return -ENOSYS; |
| 170 | } |
| 171 | |
| 172 | static ssize_t |
| 173 | qemu_pwritev(int fd, const struct iovec *iov, int nr_iov, off_t offset) |
| 174 | { |
| 175 | return -ENOSYS; |
| 176 | } |
| 177 | |
| 178 | #endif |
| 179 | |
Kevin Wolf | 6769da2 | 2009-11-18 12:15:10 +0100 | [diff] [blame] | 180 | static ssize_t handle_aiocb_rw_vector(struct qemu_paiocb *aiocb) |
aliguori | ceb42de | 2009-04-07 18:43:28 +0000 | [diff] [blame] | 181 | { |
| 182 | size_t offset = 0; |
| 183 | ssize_t len; |
| 184 | |
| 185 | do { |
Christoph Hellwig | 9ef91a6 | 2009-08-20 16:58:19 +0200 | [diff] [blame] | 186 | if (aiocb->aio_type & QEMU_AIO_WRITE) |
aliguori | ceb42de | 2009-04-07 18:43:28 +0000 | [diff] [blame] | 187 | 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 Wolf | 6769da2 | 2009-11-18 12:15:10 +0100 | [diff] [blame] | 203 | static ssize_t handle_aiocb_rw_linear(struct qemu_paiocb *aiocb, char *buf) |
aliguori | 221f715 | 2009-03-28 17:28:41 +0000 | [diff] [blame] | 204 | { |
Kevin Wolf | 6769da2 | 2009-11-18 12:15:10 +0100 | [diff] [blame] | 205 | ssize_t offset = 0; |
| 206 | ssize_t len; |
aliguori | 221f715 | 2009-03-28 17:28:41 +0000 | [diff] [blame] | 207 | |
| 208 | while (offset < aiocb->aio_nbytes) { |
Christoph Hellwig | 9ef91a6 | 2009-08-20 16:58:19 +0200 | [diff] [blame] | 209 | if (aiocb->aio_type & QEMU_AIO_WRITE) |
aliguori | f141eaf | 2009-04-07 18:43:24 +0000 | [diff] [blame] | 210 | 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, |
aliguori | 221f715 | 2009-03-28 17:28:41 +0000 | [diff] [blame] | 217 | aiocb->aio_nbytes - offset, |
| 218 | aiocb->aio_offset + offset); |
aliguori | 221f715 | 2009-03-28 17:28:41 +0000 | [diff] [blame] | 219 | |
aliguori | f141eaf | 2009-04-07 18:43:24 +0000 | [diff] [blame] | 220 | if (len == -1 && errno == EINTR) |
| 221 | continue; |
| 222 | else if (len == -1) { |
| 223 | offset = -errno; |
| 224 | break; |
| 225 | } else if (len == 0) |
| 226 | break; |
aliguori | 221f715 | 2009-03-28 17:28:41 +0000 | [diff] [blame] | 227 | |
aliguori | f141eaf | 2009-04-07 18:43:24 +0000 | [diff] [blame] | 228 | offset += len; |
aliguori | 221f715 | 2009-03-28 17:28:41 +0000 | [diff] [blame] | 229 | } |
| 230 | |
| 231 | return offset; |
| 232 | } |
| 233 | |
Kevin Wolf | 6769da2 | 2009-11-18 12:15:10 +0100 | [diff] [blame] | 234 | static ssize_t handle_aiocb_rw(struct qemu_paiocb *aiocb) |
aliguori | 221f715 | 2009-03-28 17:28:41 +0000 | [diff] [blame] | 235 | { |
Kevin Wolf | 6769da2 | 2009-11-18 12:15:10 +0100 | [diff] [blame] | 236 | ssize_t nbytes; |
aliguori | f141eaf | 2009-04-07 18:43:24 +0000 | [diff] [blame] | 237 | char *buf; |
aliguori | 221f715 | 2009-03-28 17:28:41 +0000 | [diff] [blame] | 238 | |
Christoph Hellwig | 9ef91a6 | 2009-08-20 16:58:19 +0200 | [diff] [blame] | 239 | if (!(aiocb->aio_type & QEMU_AIO_MISALIGNED)) { |
aliguori | f141eaf | 2009-04-07 18:43:24 +0000 | [diff] [blame] | 240 | /* |
| 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 | */ |
aliguori | ceb42de | 2009-04-07 18:43:28 +0000 | [diff] [blame] | 244 | 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 Hajnoczi | b587a52 | 2010-05-27 12:52:08 +0100 | [diff] [blame] | 253 | if (preadv_present) { |
aliguori | ceb42de | 2009-04-07 18:43:28 +0000 | [diff] [blame] | 254 | nbytes = handle_aiocb_rw_vector(aiocb); |
| 255 | if (nbytes == aiocb->aio_nbytes) |
Stefan Hajnoczi | b587a52 | 2010-05-27 12:52:08 +0100 | [diff] [blame] | 256 | return nbytes; |
aliguori | ceb42de | 2009-04-07 18:43:28 +0000 | [diff] [blame] | 257 | 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 | */ |
aliguori | f141eaf | 2009-04-07 18:43:24 +0000 | [diff] [blame] | 267 | } |
| 268 | |
| 269 | /* |
| 270 | * Ok, we have to do it the hard way, copy all segments into |
| 271 | * a single aligned buffer. |
| 272 | */ |
Christoph Hellwig | 72aef73 | 2010-09-12 23:42:56 +0200 | [diff] [blame] | 273 | buf = qemu_blockalign(aiocb->common.bs, aiocb->aio_nbytes); |
Christoph Hellwig | 9ef91a6 | 2009-08-20 16:58:19 +0200 | [diff] [blame] | 274 | if (aiocb->aio_type & QEMU_AIO_WRITE) { |
aliguori | f141eaf | 2009-04-07 18:43:24 +0000 | [diff] [blame] | 275 | 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 Hellwig | 9ef91a6 | 2009-08-20 16:58:19 +0200 | [diff] [blame] | 285 | if (!(aiocb->aio_type & QEMU_AIO_WRITE)) { |
aliguori | f141eaf | 2009-04-07 18:43:24 +0000 | [diff] [blame] | 286 | 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; |
aliguori | 221f715 | 2009-03-28 17:28:41 +0000 | [diff] [blame] | 302 | } |
| 303 | |
aliguori | 3c529d9 | 2008-12-12 16:41:40 +0000 | [diff] [blame] | 304 | static void *aio_thread(void *unused) |
| 305 | { |
malc | a8227a5 | 2009-02-21 05:48:17 +0000 | [diff] [blame] | 306 | pid_t pid; |
aliguori | 3c529d9 | 2008-12-12 16:41:40 +0000 | [diff] [blame] | 307 | |
malc | a8227a5 | 2009-02-21 05:48:17 +0000 | [diff] [blame] | 308 | pid = getpid(); |
| 309 | |
aliguori | 3c529d9 | 2008-12-12 16:41:40 +0000 | [diff] [blame] | 310 | while (1) { |
| 311 | struct qemu_paiocb *aiocb; |
Kevin Wolf | 6769da2 | 2009-11-18 12:15:10 +0100 | [diff] [blame] | 312 | ssize_t ret = 0; |
malc | 30525af | 2009-02-21 05:48:13 +0000 | [diff] [blame] | 313 | 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; |
aliguori | 3c529d9 | 2008-12-12 16:41:40 +0000 | [diff] [blame] | 319 | |
malc | 8653c01 | 2009-02-21 05:48:11 +0000 | [diff] [blame] | 320 | mutex_lock(&lock); |
aliguori | 3c529d9 | 2008-12-12 16:41:40 +0000 | [diff] [blame] | 321 | |
Blue Swirl | 72cf2d4 | 2009-09-12 07:36:22 +0000 | [diff] [blame] | 322 | while (QTAILQ_EMPTY(&request_list) && |
aliguori | 3c529d9 | 2008-12-12 16:41:40 +0000 | [diff] [blame] | 323 | !(ret == ETIMEDOUT)) { |
Kevin Wolf | 5be4aab | 2011-05-02 17:32:54 +0200 | [diff] [blame] | 324 | idle_threads++; |
malc | 8653c01 | 2009-02-21 05:48:11 +0000 | [diff] [blame] | 325 | ret = cond_timedwait(&cond, &lock, &ts); |
Kevin Wolf | 5be4aab | 2011-05-02 17:32:54 +0200 | [diff] [blame] | 326 | idle_threads--; |
aliguori | 3c529d9 | 2008-12-12 16:41:40 +0000 | [diff] [blame] | 327 | } |
| 328 | |
Blue Swirl | 72cf2d4 | 2009-09-12 07:36:22 +0000 | [diff] [blame] | 329 | if (QTAILQ_EMPTY(&request_list)) |
aliguori | 3c529d9 | 2008-12-12 16:41:40 +0000 | [diff] [blame] | 330 | break; |
| 331 | |
Blue Swirl | 72cf2d4 | 2009-09-12 07:36:22 +0000 | [diff] [blame] | 332 | aiocb = QTAILQ_FIRST(&request_list); |
| 333 | QTAILQ_REMOVE(&request_list, aiocb, node); |
aliguori | 3c529d9 | 2008-12-12 16:41:40 +0000 | [diff] [blame] | 334 | aiocb->active = 1; |
malc | 8653c01 | 2009-02-21 05:48:11 +0000 | [diff] [blame] | 335 | mutex_unlock(&lock); |
aliguori | 3c529d9 | 2008-12-12 16:41:40 +0000 | [diff] [blame] | 336 | |
Christoph Hellwig | 9ef91a6 | 2009-08-20 16:58:19 +0200 | [diff] [blame] | 337 | switch (aiocb->aio_type & QEMU_AIO_TYPE_MASK) { |
| 338 | case QEMU_AIO_READ: |
| 339 | case QEMU_AIO_WRITE: |
Stefan Hajnoczi | b587a52 | 2010-05-27 12:52:08 +0100 | [diff] [blame] | 340 | ret = handle_aiocb_rw(aiocb); |
| 341 | break; |
Christoph Hellwig | b2e12bc | 2009-09-04 19:01:49 +0200 | [diff] [blame] | 342 | case QEMU_AIO_FLUSH: |
Stefan Hajnoczi | b587a52 | 2010-05-27 12:52:08 +0100 | [diff] [blame] | 343 | ret = handle_aiocb_flush(aiocb); |
| 344 | break; |
Christoph Hellwig | 9ef91a6 | 2009-08-20 16:58:19 +0200 | [diff] [blame] | 345 | case QEMU_AIO_IOCTL: |
Stefan Hajnoczi | b587a52 | 2010-05-27 12:52:08 +0100 | [diff] [blame] | 346 | ret = handle_aiocb_ioctl(aiocb); |
| 347 | break; |
| 348 | default: |
| 349 | fprintf(stderr, "invalid aio request (0x%x)\n", aiocb->aio_type); |
| 350 | ret = -EINVAL; |
| 351 | break; |
| 352 | } |
aliguori | 3c529d9 | 2008-12-12 16:41:40 +0000 | [diff] [blame] | 353 | |
malc | 8653c01 | 2009-02-21 05:48:11 +0000 | [diff] [blame] | 354 | mutex_lock(&lock); |
aliguori | 221f715 | 2009-03-28 17:28:41 +0000 | [diff] [blame] | 355 | aiocb->ret = ret; |
malc | 8653c01 | 2009-02-21 05:48:11 +0000 | [diff] [blame] | 356 | mutex_unlock(&lock); |
aliguori | 3c529d9 | 2008-12-12 16:41:40 +0000 | [diff] [blame] | 357 | |
malc | a8227a5 | 2009-02-21 05:48:17 +0000 | [diff] [blame] | 358 | if (kill(pid, aiocb->ev_signo)) die("kill failed"); |
aliguori | 3c529d9 | 2008-12-12 16:41:40 +0000 | [diff] [blame] | 359 | } |
| 360 | |
aliguori | 3c529d9 | 2008-12-12 16:41:40 +0000 | [diff] [blame] | 361 | cur_threads--; |
malc | 8653c01 | 2009-02-21 05:48:11 +0000 | [diff] [blame] | 362 | mutex_unlock(&lock); |
aliguori | 3c529d9 | 2008-12-12 16:41:40 +0000 | [diff] [blame] | 363 | |
| 364 | return NULL; |
| 365 | } |
| 366 | |
malc | 8653c01 | 2009-02-21 05:48:11 +0000 | [diff] [blame] | 367 | static void spawn_thread(void) |
aliguori | 3c529d9 | 2008-12-12 16:41:40 +0000 | [diff] [blame] | 368 | { |
malc | ee39930 | 2009-09-25 00:20:44 +0400 | [diff] [blame] | 369 | sigset_t set, oldset; |
| 370 | |
aliguori | 3c529d9 | 2008-12-12 16:41:40 +0000 | [diff] [blame] | 371 | cur_threads++; |
malc | ee39930 | 2009-09-25 00:20:44 +0400 | [diff] [blame] | 372 | |
| 373 | /* block all signals */ |
| 374 | if (sigfillset(&set)) die("sigfillset"); |
| 375 | if (sigprocmask(SIG_SETMASK, &set, &oldset)) die("sigprocmask"); |
| 376 | |
malc | 8653c01 | 2009-02-21 05:48:11 +0000 | [diff] [blame] | 377 | thread_create(&thread_id, &attr, aio_thread, NULL); |
malc | ee39930 | 2009-09-25 00:20:44 +0400 | [diff] [blame] | 378 | |
| 379 | if (sigprocmask(SIG_SETMASK, &oldset, NULL)) die("sigprocmask restore"); |
aliguori | 3c529d9 | 2008-12-12 16:41:40 +0000 | [diff] [blame] | 380 | } |
| 381 | |
Christoph Hellwig | 9ef91a6 | 2009-08-20 16:58:19 +0200 | [diff] [blame] | 382 | static void qemu_paio_submit(struct qemu_paiocb *aiocb) |
aliguori | 3c529d9 | 2008-12-12 16:41:40 +0000 | [diff] [blame] | 383 | { |
aliguori | 3c529d9 | 2008-12-12 16:41:40 +0000 | [diff] [blame] | 384 | aiocb->ret = -EINPROGRESS; |
| 385 | aiocb->active = 0; |
malc | 8653c01 | 2009-02-21 05:48:11 +0000 | [diff] [blame] | 386 | mutex_lock(&lock); |
aliguori | 3c529d9 | 2008-12-12 16:41:40 +0000 | [diff] [blame] | 387 | if (idle_threads == 0 && cur_threads < max_threads) |
| 388 | spawn_thread(); |
Blue Swirl | 72cf2d4 | 2009-09-12 07:36:22 +0000 | [diff] [blame] | 389 | QTAILQ_INSERT_TAIL(&request_list, aiocb, node); |
malc | 8653c01 | 2009-02-21 05:48:11 +0000 | [diff] [blame] | 390 | mutex_unlock(&lock); |
malc | 5d47e37 | 2009-02-21 05:48:15 +0000 | [diff] [blame] | 391 | cond_signal(&cond); |
aliguori | 3c529d9 | 2008-12-12 16:41:40 +0000 | [diff] [blame] | 392 | } |
| 393 | |
Christoph Hellwig | 9ef91a6 | 2009-08-20 16:58:19 +0200 | [diff] [blame] | 394 | static ssize_t qemu_paio_return(struct qemu_paiocb *aiocb) |
aliguori | 3c529d9 | 2008-12-12 16:41:40 +0000 | [diff] [blame] | 395 | { |
| 396 | ssize_t ret; |
| 397 | |
malc | 8653c01 | 2009-02-21 05:48:11 +0000 | [diff] [blame] | 398 | mutex_lock(&lock); |
aliguori | 3c529d9 | 2008-12-12 16:41:40 +0000 | [diff] [blame] | 399 | ret = aiocb->ret; |
malc | 8653c01 | 2009-02-21 05:48:11 +0000 | [diff] [blame] | 400 | mutex_unlock(&lock); |
aliguori | 3c529d9 | 2008-12-12 16:41:40 +0000 | [diff] [blame] | 401 | |
| 402 | return ret; |
| 403 | } |
| 404 | |
Christoph Hellwig | 9ef91a6 | 2009-08-20 16:58:19 +0200 | [diff] [blame] | 405 | static int qemu_paio_error(struct qemu_paiocb *aiocb) |
aliguori | 3c529d9 | 2008-12-12 16:41:40 +0000 | [diff] [blame] | 406 | { |
| 407 | ssize_t ret = qemu_paio_return(aiocb); |
| 408 | |
| 409 | if (ret < 0) |
| 410 | ret = -ret; |
| 411 | else |
| 412 | ret = 0; |
| 413 | |
| 414 | return ret; |
| 415 | } |
| 416 | |
Kevin Wolf | 59c7b15 | 2009-10-22 17:54:35 +0200 | [diff] [blame] | 417 | static int posix_aio_process_queue(void *opaque) |
aliguori | 3c529d9 | 2008-12-12 16:41:40 +0000 | [diff] [blame] | 418 | { |
Christoph Hellwig | 9ef91a6 | 2009-08-20 16:58:19 +0200 | [diff] [blame] | 419 | PosixAioState *s = opaque; |
| 420 | struct qemu_paiocb *acb, **pacb; |
aliguori | 3c529d9 | 2008-12-12 16:41:40 +0000 | [diff] [blame] | 421 | int ret; |
Kevin Wolf | 59c7b15 | 2009-10-22 17:54:35 +0200 | [diff] [blame] | 422 | int result = 0; |
Kevin Wolf | e5f3764 | 2009-10-22 17:54:40 +0200 | [diff] [blame] | 423 | int async_context_id = get_async_context_id(); |
Christoph Hellwig | 9ef91a6 | 2009-08-20 16:58:19 +0200 | [diff] [blame] | 424 | |
| 425 | for(;;) { |
| 426 | pacb = &s->first_aio; |
| 427 | for(;;) { |
| 428 | acb = *pacb; |
| 429 | if (!acb) |
Kevin Wolf | 59c7b15 | 2009-10-22 17:54:35 +0200 | [diff] [blame] | 430 | return result; |
Kevin Wolf | e5f3764 | 2009-10-22 17:54:40 +0200 | [diff] [blame] | 431 | |
| 432 | /* we're only interested in requests in the right context */ |
| 433 | if (acb->async_context_id != async_context_id) { |
| 434 | pacb = &acb->next; |
| 435 | continue; |
| 436 | } |
| 437 | |
Christoph Hellwig | 9ef91a6 | 2009-08-20 16:58:19 +0200 | [diff] [blame] | 438 | ret = qemu_paio_error(acb); |
| 439 | if (ret == ECANCELED) { |
| 440 | /* remove the request */ |
| 441 | *pacb = acb->next; |
| 442 | qemu_aio_release(acb); |
Kevin Wolf | 59c7b15 | 2009-10-22 17:54:35 +0200 | [diff] [blame] | 443 | result = 1; |
Christoph Hellwig | 9ef91a6 | 2009-08-20 16:58:19 +0200 | [diff] [blame] | 444 | } else if (ret != EINPROGRESS) { |
| 445 | /* end of aio */ |
| 446 | if (ret == 0) { |
| 447 | ret = qemu_paio_return(acb); |
| 448 | if (ret == acb->aio_nbytes) |
| 449 | ret = 0; |
| 450 | else |
| 451 | ret = -EINVAL; |
| 452 | } else { |
| 453 | ret = -ret; |
| 454 | } |
Stefan Hajnoczi | ddca9fb | 2011-03-07 08:06:10 +0000 | [diff] [blame] | 455 | |
| 456 | trace_paio_complete(acb, acb->common.opaque, ret); |
| 457 | |
Christoph Hellwig | 9ef91a6 | 2009-08-20 16:58:19 +0200 | [diff] [blame] | 458 | /* remove the request */ |
| 459 | *pacb = acb->next; |
| 460 | /* call the callback */ |
| 461 | acb->common.cb(acb->common.opaque, ret); |
| 462 | qemu_aio_release(acb); |
Kevin Wolf | 59c7b15 | 2009-10-22 17:54:35 +0200 | [diff] [blame] | 463 | result = 1; |
Christoph Hellwig | 9ef91a6 | 2009-08-20 16:58:19 +0200 | [diff] [blame] | 464 | break; |
| 465 | } else { |
| 466 | pacb = &acb->next; |
| 467 | } |
| 468 | } |
| 469 | } |
Kevin Wolf | 59c7b15 | 2009-10-22 17:54:35 +0200 | [diff] [blame] | 470 | |
| 471 | return result; |
| 472 | } |
| 473 | |
| 474 | static void posix_aio_read(void *opaque) |
| 475 | { |
| 476 | PosixAioState *s = opaque; |
| 477 | ssize_t len; |
| 478 | |
| 479 | /* read all bytes from signal pipe */ |
| 480 | for (;;) { |
| 481 | char bytes[16]; |
| 482 | |
| 483 | len = read(s->rfd, bytes, sizeof(bytes)); |
| 484 | if (len == -1 && errno == EINTR) |
| 485 | continue; /* try again */ |
| 486 | if (len == sizeof(bytes)) |
| 487 | continue; /* more to read */ |
| 488 | break; |
| 489 | } |
| 490 | |
| 491 | posix_aio_process_queue(s); |
Christoph Hellwig | 9ef91a6 | 2009-08-20 16:58:19 +0200 | [diff] [blame] | 492 | } |
| 493 | |
| 494 | static int posix_aio_flush(void *opaque) |
| 495 | { |
| 496 | PosixAioState *s = opaque; |
| 497 | return !!s->first_aio; |
| 498 | } |
| 499 | |
| 500 | static PosixAioState *posix_aio_state; |
| 501 | |
| 502 | static void aio_signal_handler(int signum) |
| 503 | { |
| 504 | if (posix_aio_state) { |
| 505 | char byte = 0; |
Kirill A. Shutemov | 4817d32 | 2010-01-20 00:56:10 +0100 | [diff] [blame] | 506 | ssize_t ret; |
Christoph Hellwig | 9ef91a6 | 2009-08-20 16:58:19 +0200 | [diff] [blame] | 507 | |
Kirill A. Shutemov | 4817d32 | 2010-01-20 00:56:10 +0100 | [diff] [blame] | 508 | ret = write(posix_aio_state->wfd, &byte, sizeof(byte)); |
| 509 | if (ret < 0 && errno != EAGAIN) |
| 510 | die("write()"); |
Christoph Hellwig | 9ef91a6 | 2009-08-20 16:58:19 +0200 | [diff] [blame] | 511 | } |
| 512 | |
| 513 | qemu_service_io(); |
| 514 | } |
| 515 | |
| 516 | static void paio_remove(struct qemu_paiocb *acb) |
| 517 | { |
| 518 | struct qemu_paiocb **pacb; |
| 519 | |
| 520 | /* remove the callback from the queue */ |
| 521 | pacb = &posix_aio_state->first_aio; |
| 522 | for(;;) { |
| 523 | if (*pacb == NULL) { |
| 524 | fprintf(stderr, "paio_remove: aio request not found!\n"); |
| 525 | break; |
| 526 | } else if (*pacb == acb) { |
| 527 | *pacb = acb->next; |
| 528 | qemu_aio_release(acb); |
| 529 | break; |
| 530 | } |
| 531 | pacb = &(*pacb)->next; |
| 532 | } |
| 533 | } |
| 534 | |
| 535 | static void paio_cancel(BlockDriverAIOCB *blockacb) |
| 536 | { |
| 537 | struct qemu_paiocb *acb = (struct qemu_paiocb *)blockacb; |
| 538 | int active = 0; |
aliguori | 3c529d9 | 2008-12-12 16:41:40 +0000 | [diff] [blame] | 539 | |
Stefan Hajnoczi | ddca9fb | 2011-03-07 08:06:10 +0000 | [diff] [blame] | 540 | trace_paio_cancel(acb, acb->common.opaque); |
| 541 | |
malc | 8653c01 | 2009-02-21 05:48:11 +0000 | [diff] [blame] | 542 | mutex_lock(&lock); |
Christoph Hellwig | 9ef91a6 | 2009-08-20 16:58:19 +0200 | [diff] [blame] | 543 | if (!acb->active) { |
Blue Swirl | 72cf2d4 | 2009-09-12 07:36:22 +0000 | [diff] [blame] | 544 | QTAILQ_REMOVE(&request_list, acb, node); |
Christoph Hellwig | 9ef91a6 | 2009-08-20 16:58:19 +0200 | [diff] [blame] | 545 | acb->ret = -ECANCELED; |
| 546 | } else if (acb->ret == -EINPROGRESS) { |
| 547 | active = 1; |
| 548 | } |
malc | 8653c01 | 2009-02-21 05:48:11 +0000 | [diff] [blame] | 549 | mutex_unlock(&lock); |
aliguori | 3c529d9 | 2008-12-12 16:41:40 +0000 | [diff] [blame] | 550 | |
Christoph Hellwig | 9ef91a6 | 2009-08-20 16:58:19 +0200 | [diff] [blame] | 551 | if (active) { |
| 552 | /* fail safe: if the aio could not be canceled, we wait for |
| 553 | it */ |
| 554 | while (qemu_paio_error(acb) == EINPROGRESS) |
| 555 | ; |
| 556 | } |
| 557 | |
| 558 | paio_remove(acb); |
| 559 | } |
| 560 | |
| 561 | static AIOPool raw_aio_pool = { |
| 562 | .aiocb_size = sizeof(struct qemu_paiocb), |
| 563 | .cancel = paio_cancel, |
| 564 | }; |
| 565 | |
Kevin Wolf | 1e5b9d2 | 2009-10-26 13:03:08 +0100 | [diff] [blame] | 566 | BlockDriverAIOCB *paio_submit(BlockDriverState *bs, int fd, |
Christoph Hellwig | 9ef91a6 | 2009-08-20 16:58:19 +0200 | [diff] [blame] | 567 | int64_t sector_num, QEMUIOVector *qiov, int nb_sectors, |
| 568 | BlockDriverCompletionFunc *cb, void *opaque, int type) |
| 569 | { |
| 570 | struct qemu_paiocb *acb; |
| 571 | |
| 572 | acb = qemu_aio_get(&raw_aio_pool, bs, cb, opaque); |
| 573 | if (!acb) |
| 574 | return NULL; |
| 575 | acb->aio_type = type; |
| 576 | acb->aio_fildes = fd; |
| 577 | acb->ev_signo = SIGUSR2; |
Kevin Wolf | e5f3764 | 2009-10-22 17:54:40 +0200 | [diff] [blame] | 578 | acb->async_context_id = get_async_context_id(); |
| 579 | |
Christoph Hellwig | b2e12bc | 2009-09-04 19:01:49 +0200 | [diff] [blame] | 580 | if (qiov) { |
| 581 | acb->aio_iov = qiov->iov; |
| 582 | acb->aio_niov = qiov->niov; |
| 583 | } |
Christoph Hellwig | 9ef91a6 | 2009-08-20 16:58:19 +0200 | [diff] [blame] | 584 | acb->aio_nbytes = nb_sectors * 512; |
| 585 | acb->aio_offset = sector_num * 512; |
| 586 | |
| 587 | acb->next = posix_aio_state->first_aio; |
| 588 | posix_aio_state->first_aio = acb; |
| 589 | |
Stefan Hajnoczi | 6d519a5 | 2010-05-22 18:15:08 +0100 | [diff] [blame] | 590 | trace_paio_submit(acb, opaque, sector_num, nb_sectors, type); |
Christoph Hellwig | 9ef91a6 | 2009-08-20 16:58:19 +0200 | [diff] [blame] | 591 | qemu_paio_submit(acb); |
| 592 | return &acb->common; |
| 593 | } |
| 594 | |
| 595 | BlockDriverAIOCB *paio_ioctl(BlockDriverState *bs, int fd, |
| 596 | unsigned long int req, void *buf, |
| 597 | BlockDriverCompletionFunc *cb, void *opaque) |
| 598 | { |
| 599 | struct qemu_paiocb *acb; |
| 600 | |
| 601 | acb = qemu_aio_get(&raw_aio_pool, bs, cb, opaque); |
| 602 | if (!acb) |
| 603 | return NULL; |
| 604 | acb->aio_type = QEMU_AIO_IOCTL; |
| 605 | acb->aio_fildes = fd; |
| 606 | acb->ev_signo = SIGUSR2; |
Andrew de Quincey | 34cf008 | 2010-08-08 21:04:50 +0100 | [diff] [blame] | 607 | acb->async_context_id = get_async_context_id(); |
Christoph Hellwig | 9ef91a6 | 2009-08-20 16:58:19 +0200 | [diff] [blame] | 608 | acb->aio_offset = 0; |
| 609 | acb->aio_ioctl_buf = buf; |
| 610 | acb->aio_ioctl_cmd = req; |
| 611 | |
| 612 | acb->next = posix_aio_state->first_aio; |
| 613 | posix_aio_state->first_aio = acb; |
| 614 | |
| 615 | qemu_paio_submit(acb); |
| 616 | return &acb->common; |
| 617 | } |
| 618 | |
Kevin Wolf | 1e5b9d2 | 2009-10-26 13:03:08 +0100 | [diff] [blame] | 619 | int paio_init(void) |
Christoph Hellwig | 9ef91a6 | 2009-08-20 16:58:19 +0200 | [diff] [blame] | 620 | { |
| 621 | struct sigaction act; |
| 622 | PosixAioState *s; |
| 623 | int fds[2]; |
| 624 | int ret; |
| 625 | |
| 626 | if (posix_aio_state) |
Kevin Wolf | 1e5b9d2 | 2009-10-26 13:03:08 +0100 | [diff] [blame] | 627 | return 0; |
Christoph Hellwig | 9ef91a6 | 2009-08-20 16:58:19 +0200 | [diff] [blame] | 628 | |
| 629 | s = qemu_malloc(sizeof(PosixAioState)); |
| 630 | |
| 631 | sigfillset(&act.sa_mask); |
| 632 | act.sa_flags = 0; /* do not restart syscalls to interrupt select() */ |
| 633 | act.sa_handler = aio_signal_handler; |
| 634 | sigaction(SIGUSR2, &act, NULL); |
| 635 | |
| 636 | s->first_aio = NULL; |
Kevin Wolf | 40ff6d7 | 2009-12-02 12:24:42 +0100 | [diff] [blame] | 637 | if (qemu_pipe(fds) == -1) { |
Christoph Hellwig | 9ef91a6 | 2009-08-20 16:58:19 +0200 | [diff] [blame] | 638 | fprintf(stderr, "failed to create pipe\n"); |
Kevin Wolf | 1e5b9d2 | 2009-10-26 13:03:08 +0100 | [diff] [blame] | 639 | return -1; |
Christoph Hellwig | 9ef91a6 | 2009-08-20 16:58:19 +0200 | [diff] [blame] | 640 | } |
| 641 | |
| 642 | s->rfd = fds[0]; |
| 643 | s->wfd = fds[1]; |
| 644 | |
| 645 | fcntl(s->rfd, F_SETFL, O_NONBLOCK); |
| 646 | fcntl(s->wfd, F_SETFL, O_NONBLOCK); |
| 647 | |
Kevin Wolf | 8febfa2 | 2009-10-22 17:54:36 +0200 | [diff] [blame] | 648 | qemu_aio_set_fd_handler(s->rfd, posix_aio_read, NULL, posix_aio_flush, |
| 649 | posix_aio_process_queue, s); |
Christoph Hellwig | 9ef91a6 | 2009-08-20 16:58:19 +0200 | [diff] [blame] | 650 | |
| 651 | ret = pthread_attr_init(&attr); |
| 652 | if (ret) |
| 653 | die2(ret, "pthread_attr_init"); |
| 654 | |
| 655 | ret = pthread_attr_setdetachstate(&attr, PTHREAD_CREATE_DETACHED); |
| 656 | if (ret) |
| 657 | die2(ret, "pthread_attr_setdetachstate"); |
| 658 | |
Blue Swirl | 72cf2d4 | 2009-09-12 07:36:22 +0000 | [diff] [blame] | 659 | QTAILQ_INIT(&request_list); |
Christoph Hellwig | 9ef91a6 | 2009-08-20 16:58:19 +0200 | [diff] [blame] | 660 | |
| 661 | posix_aio_state = s; |
Kevin Wolf | 1e5b9d2 | 2009-10-26 13:03:08 +0100 | [diff] [blame] | 662 | return 0; |
aliguori | 3c529d9 | 2008-12-12 16:41:40 +0000 | [diff] [blame] | 663 | } |