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> |
Christoph Hellwig | 9ef91a6 | 2009-08-20 16:58:19 +0200 | [diff] [blame] | 20 | #include <signal.h> |
malc | 8653c01 | 2009-02-21 05:48:11 +0000 | [diff] [blame] | 21 | #include <string.h> |
| 22 | #include <stdlib.h> |
| 23 | #include <stdio.h> |
Christoph Hellwig | 9ef91a6 | 2009-08-20 16:58:19 +0200 | [diff] [blame] | 24 | |
Blue Swirl | 72cf2d4 | 2009-09-12 07:36:22 +0000 | [diff] [blame] | 25 | #include "qemu-queue.h" |
aliguori | 3c529d9 | 2008-12-12 16:41:40 +0000 | [diff] [blame] | 26 | #include "osdep.h" |
aliguori | f141eaf | 2009-04-07 18:43:24 +0000 | [diff] [blame] | 27 | #include "qemu-common.h" |
Christoph Hellwig | 9ef91a6 | 2009-08-20 16:58:19 +0200 | [diff] [blame] | 28 | #include "block_int.h" |
aliguori | 3c529d9 | 2008-12-12 16:41:40 +0000 | [diff] [blame] | 29 | |
Christoph Hellwig | 9ef91a6 | 2009-08-20 16:58:19 +0200 | [diff] [blame] | 30 | #include "block/raw-posix-aio.h" |
| 31 | |
| 32 | |
| 33 | struct 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 Swirl | 72cf2d4 | 2009-09-12 07:36:22 +0000 | [diff] [blame] | 46 | QTAILQ_ENTRY(qemu_paiocb) node; |
Christoph Hellwig | 9ef91a6 | 2009-08-20 16:58:19 +0200 | [diff] [blame] | 47 | int aio_type; |
| 48 | ssize_t ret; |
| 49 | int active; |
| 50 | struct qemu_paiocb *next; |
Kevin Wolf | e5f3764 | 2009-10-22 17:54:40 +0200 | [diff] [blame] | 51 | |
| 52 | int async_context_id; |
Christoph Hellwig | 9ef91a6 | 2009-08-20 16:58:19 +0200 | [diff] [blame] | 53 | }; |
| 54 | |
| 55 | typedef struct PosixAioState { |
| 56 | int rfd, wfd; |
| 57 | struct qemu_paiocb *first_aio; |
| 58 | } PosixAioState; |
| 59 | |
aliguori | 3c529d9 | 2008-12-12 16:41:40 +0000 | [diff] [blame] | 60 | |
| 61 | static pthread_mutex_t lock = PTHREAD_MUTEX_INITIALIZER; |
| 62 | static pthread_cond_t cond = PTHREAD_COND_INITIALIZER; |
| 63 | static pthread_t thread_id; |
malc | a8227a5 | 2009-02-21 05:48:17 +0000 | [diff] [blame] | 64 | static pthread_attr_t attr; |
aliguori | 3c529d9 | 2008-12-12 16:41:40 +0000 | [diff] [blame] | 65 | static int max_threads = 64; |
| 66 | static int cur_threads = 0; |
| 67 | static int idle_threads = 0; |
Blue Swirl | 72cf2d4 | 2009-09-12 07:36:22 +0000 | [diff] [blame] | 68 | static QTAILQ_HEAD(, qemu_paiocb) request_list; |
aliguori | 3c529d9 | 2008-12-12 16:41:40 +0000 | [diff] [blame] | 69 | |
Juan Quintela | 2341f9a | 2009-07-27 16:12:58 +0200 | [diff] [blame] | 70 | #ifdef CONFIG_PREADV |
aliguori | ceb42de | 2009-04-07 18:43:28 +0000 | [diff] [blame] | 71 | static int preadv_present = 1; |
| 72 | #else |
| 73 | static int preadv_present = 0; |
| 74 | #endif |
| 75 | |
malc | 8653c01 | 2009-02-21 05:48:11 +0000 | [diff] [blame] | 76 | static void die2(int err, const char *what) |
| 77 | { |
| 78 | fprintf(stderr, "%s failed: %s\n", what, strerror(err)); |
| 79 | abort(); |
| 80 | } |
| 81 | |
| 82 | static void die(const char *what) |
| 83 | { |
| 84 | die2(errno, what); |
| 85 | } |
| 86 | |
| 87 | static void mutex_lock(pthread_mutex_t *mutex) |
| 88 | { |
| 89 | int ret = pthread_mutex_lock(mutex); |
| 90 | if (ret) die2(ret, "pthread_mutex_lock"); |
| 91 | } |
| 92 | |
| 93 | static void mutex_unlock(pthread_mutex_t *mutex) |
| 94 | { |
| 95 | int ret = pthread_mutex_unlock(mutex); |
| 96 | if (ret) die2(ret, "pthread_mutex_unlock"); |
| 97 | } |
| 98 | |
| 99 | static int cond_timedwait(pthread_cond_t *cond, pthread_mutex_t *mutex, |
| 100 | struct timespec *ts) |
| 101 | { |
| 102 | int ret = pthread_cond_timedwait(cond, mutex, ts); |
| 103 | if (ret && ret != ETIMEDOUT) die2(ret, "pthread_cond_timedwait"); |
| 104 | return ret; |
| 105 | } |
| 106 | |
malc | 5d47e37 | 2009-02-21 05:48:15 +0000 | [diff] [blame] | 107 | static void cond_signal(pthread_cond_t *cond) |
malc | 8653c01 | 2009-02-21 05:48:11 +0000 | [diff] [blame] | 108 | { |
malc | 5d47e37 | 2009-02-21 05:48:15 +0000 | [diff] [blame] | 109 | int ret = pthread_cond_signal(cond); |
| 110 | if (ret) die2(ret, "pthread_cond_signal"); |
malc | 8653c01 | 2009-02-21 05:48:11 +0000 | [diff] [blame] | 111 | } |
| 112 | |
| 113 | static void thread_create(pthread_t *thread, pthread_attr_t *attr, |
| 114 | void *(*start_routine)(void*), void *arg) |
| 115 | { |
| 116 | int ret = pthread_create(thread, attr, start_routine, arg); |
| 117 | if (ret) die2(ret, "pthread_create"); |
| 118 | } |
| 119 | |
Kevin Wolf | 6769da2 | 2009-11-18 12:15:10 +0100 | [diff] [blame] | 120 | static ssize_t handle_aiocb_ioctl(struct qemu_paiocb *aiocb) |
aliguori | f141eaf | 2009-04-07 18:43:24 +0000 | [diff] [blame] | 121 | { |
| 122 | int ret; |
| 123 | |
| 124 | ret = ioctl(aiocb->aio_fildes, aiocb->aio_ioctl_cmd, aiocb->aio_ioctl_buf); |
| 125 | if (ret == -1) |
| 126 | return -errno; |
Christoph Hellwig | e7d54ae | 2009-04-28 11:57:02 +0200 | [diff] [blame] | 127 | |
| 128 | /* |
| 129 | * This looks weird, but the aio code only consideres a request |
| 130 | * successfull if it has written the number full number of bytes. |
| 131 | * |
| 132 | * Now we overload aio_nbytes as aio_ioctl_cmd for the ioctl command, |
| 133 | * so in fact we return the ioctl command here to make posix_aio_read() |
| 134 | * happy.. |
| 135 | */ |
| 136 | return aiocb->aio_nbytes; |
aliguori | f141eaf | 2009-04-07 18:43:24 +0000 | [diff] [blame] | 137 | } |
| 138 | |
Kevin Wolf | 6769da2 | 2009-11-18 12:15:10 +0100 | [diff] [blame] | 139 | static ssize_t handle_aiocb_flush(struct qemu_paiocb *aiocb) |
Christoph Hellwig | b2e12bc | 2009-09-04 19:01:49 +0200 | [diff] [blame] | 140 | { |
| 141 | int ret; |
| 142 | |
Blue Swirl | 47faadc | 2009-09-12 06:19:14 +0000 | [diff] [blame] | 143 | ret = qemu_fdatasync(aiocb->aio_fildes); |
Christoph Hellwig | b2e12bc | 2009-09-04 19:01:49 +0200 | [diff] [blame] | 144 | if (ret == -1) |
| 145 | return -errno; |
| 146 | return 0; |
| 147 | } |
| 148 | |
Juan Quintela | 2341f9a | 2009-07-27 16:12:58 +0200 | [diff] [blame] | 149 | #ifdef CONFIG_PREADV |
aliguori | ceb42de | 2009-04-07 18:43:28 +0000 | [diff] [blame] | 150 | |
| 151 | static ssize_t |
| 152 | qemu_preadv(int fd, const struct iovec *iov, int nr_iov, off_t offset) |
| 153 | { |
| 154 | return preadv(fd, iov, nr_iov, offset); |
| 155 | } |
| 156 | |
| 157 | static ssize_t |
| 158 | qemu_pwritev(int fd, const struct iovec *iov, int nr_iov, off_t offset) |
| 159 | { |
| 160 | return pwritev(fd, iov, nr_iov, offset); |
| 161 | } |
| 162 | |
| 163 | #else |
| 164 | |
| 165 | static ssize_t |
| 166 | qemu_preadv(int fd, const struct iovec *iov, int nr_iov, off_t offset) |
| 167 | { |
| 168 | return -ENOSYS; |
| 169 | } |
| 170 | |
| 171 | static ssize_t |
| 172 | qemu_pwritev(int fd, const struct iovec *iov, int nr_iov, off_t offset) |
| 173 | { |
| 174 | return -ENOSYS; |
| 175 | } |
| 176 | |
| 177 | #endif |
| 178 | |
Kevin Wolf | 6769da2 | 2009-11-18 12:15:10 +0100 | [diff] [blame] | 179 | static ssize_t handle_aiocb_rw_vector(struct qemu_paiocb *aiocb) |
aliguori | ceb42de | 2009-04-07 18:43:28 +0000 | [diff] [blame] | 180 | { |
| 181 | size_t offset = 0; |
| 182 | ssize_t len; |
| 183 | |
| 184 | do { |
Christoph Hellwig | 9ef91a6 | 2009-08-20 16:58:19 +0200 | [diff] [blame] | 185 | if (aiocb->aio_type & QEMU_AIO_WRITE) |
aliguori | ceb42de | 2009-04-07 18:43:28 +0000 | [diff] [blame] | 186 | len = qemu_pwritev(aiocb->aio_fildes, |
| 187 | aiocb->aio_iov, |
| 188 | aiocb->aio_niov, |
| 189 | aiocb->aio_offset + offset); |
| 190 | else |
| 191 | len = qemu_preadv(aiocb->aio_fildes, |
| 192 | aiocb->aio_iov, |
| 193 | aiocb->aio_niov, |
| 194 | aiocb->aio_offset + offset); |
| 195 | } while (len == -1 && errno == EINTR); |
| 196 | |
| 197 | if (len == -1) |
| 198 | return -errno; |
| 199 | return len; |
| 200 | } |
| 201 | |
Kevin Wolf | 6769da2 | 2009-11-18 12:15:10 +0100 | [diff] [blame] | 202 | static ssize_t handle_aiocb_rw_linear(struct qemu_paiocb *aiocb, char *buf) |
aliguori | 221f715 | 2009-03-28 17:28:41 +0000 | [diff] [blame] | 203 | { |
Kevin Wolf | 6769da2 | 2009-11-18 12:15:10 +0100 | [diff] [blame] | 204 | ssize_t offset = 0; |
| 205 | ssize_t len; |
aliguori | 221f715 | 2009-03-28 17:28:41 +0000 | [diff] [blame] | 206 | |
| 207 | while (offset < aiocb->aio_nbytes) { |
Christoph Hellwig | 9ef91a6 | 2009-08-20 16:58:19 +0200 | [diff] [blame] | 208 | if (aiocb->aio_type & QEMU_AIO_WRITE) |
aliguori | f141eaf | 2009-04-07 18:43:24 +0000 | [diff] [blame] | 209 | len = pwrite(aiocb->aio_fildes, |
| 210 | (const char *)buf + offset, |
| 211 | aiocb->aio_nbytes - offset, |
| 212 | aiocb->aio_offset + offset); |
| 213 | else |
| 214 | len = pread(aiocb->aio_fildes, |
| 215 | buf + offset, |
aliguori | 221f715 | 2009-03-28 17:28:41 +0000 | [diff] [blame] | 216 | aiocb->aio_nbytes - offset, |
| 217 | aiocb->aio_offset + offset); |
aliguori | 221f715 | 2009-03-28 17:28:41 +0000 | [diff] [blame] | 218 | |
aliguori | f141eaf | 2009-04-07 18:43:24 +0000 | [diff] [blame] | 219 | if (len == -1 && errno == EINTR) |
| 220 | continue; |
| 221 | else if (len == -1) { |
| 222 | offset = -errno; |
| 223 | break; |
| 224 | } else if (len == 0) |
| 225 | break; |
aliguori | 221f715 | 2009-03-28 17:28:41 +0000 | [diff] [blame] | 226 | |
aliguori | f141eaf | 2009-04-07 18:43:24 +0000 | [diff] [blame] | 227 | offset += len; |
aliguori | 221f715 | 2009-03-28 17:28:41 +0000 | [diff] [blame] | 228 | } |
| 229 | |
| 230 | return offset; |
| 231 | } |
| 232 | |
Kevin Wolf | 6769da2 | 2009-11-18 12:15:10 +0100 | [diff] [blame] | 233 | static ssize_t handle_aiocb_rw(struct qemu_paiocb *aiocb) |
aliguori | 221f715 | 2009-03-28 17:28:41 +0000 | [diff] [blame] | 234 | { |
Kevin Wolf | 6769da2 | 2009-11-18 12:15:10 +0100 | [diff] [blame] | 235 | ssize_t nbytes; |
aliguori | f141eaf | 2009-04-07 18:43:24 +0000 | [diff] [blame] | 236 | char *buf; |
aliguori | 221f715 | 2009-03-28 17:28:41 +0000 | [diff] [blame] | 237 | |
Christoph Hellwig | 9ef91a6 | 2009-08-20 16:58:19 +0200 | [diff] [blame] | 238 | if (!(aiocb->aio_type & QEMU_AIO_MISALIGNED)) { |
aliguori | f141eaf | 2009-04-07 18:43:24 +0000 | [diff] [blame] | 239 | /* |
| 240 | * If there is just a single buffer, and it is properly aligned |
| 241 | * we can just use plain pread/pwrite without any problems. |
| 242 | */ |
aliguori | ceb42de | 2009-04-07 18:43:28 +0000 | [diff] [blame] | 243 | if (aiocb->aio_niov == 1) |
| 244 | return handle_aiocb_rw_linear(aiocb, aiocb->aio_iov->iov_base); |
| 245 | |
| 246 | /* |
| 247 | * We have more than one iovec, and all are properly aligned. |
| 248 | * |
| 249 | * Try preadv/pwritev first and fall back to linearizing the |
| 250 | * buffer if it's not supported. |
| 251 | */ |
| 252 | if (preadv_present) { |
| 253 | nbytes = handle_aiocb_rw_vector(aiocb); |
| 254 | if (nbytes == aiocb->aio_nbytes) |
| 255 | return nbytes; |
| 256 | if (nbytes < 0 && nbytes != -ENOSYS) |
| 257 | return nbytes; |
| 258 | preadv_present = 0; |
| 259 | } |
| 260 | |
| 261 | /* |
| 262 | * XXX(hch): short read/write. no easy way to handle the reminder |
| 263 | * using these interfaces. For now retry using plain |
| 264 | * pread/pwrite? |
| 265 | */ |
aliguori | f141eaf | 2009-04-07 18:43:24 +0000 | [diff] [blame] | 266 | } |
| 267 | |
| 268 | /* |
| 269 | * Ok, we have to do it the hard way, copy all segments into |
| 270 | * a single aligned buffer. |
| 271 | */ |
| 272 | buf = qemu_memalign(512, aiocb->aio_nbytes); |
Christoph Hellwig | 9ef91a6 | 2009-08-20 16:58:19 +0200 | [diff] [blame] | 273 | if (aiocb->aio_type & QEMU_AIO_WRITE) { |
aliguori | f141eaf | 2009-04-07 18:43:24 +0000 | [diff] [blame] | 274 | char *p = buf; |
| 275 | int i; |
| 276 | |
| 277 | for (i = 0; i < aiocb->aio_niov; ++i) { |
| 278 | memcpy(p, aiocb->aio_iov[i].iov_base, aiocb->aio_iov[i].iov_len); |
| 279 | p += aiocb->aio_iov[i].iov_len; |
| 280 | } |
| 281 | } |
| 282 | |
| 283 | nbytes = handle_aiocb_rw_linear(aiocb, buf); |
Christoph Hellwig | 9ef91a6 | 2009-08-20 16:58:19 +0200 | [diff] [blame] | 284 | if (!(aiocb->aio_type & QEMU_AIO_WRITE)) { |
aliguori | f141eaf | 2009-04-07 18:43:24 +0000 | [diff] [blame] | 285 | char *p = buf; |
| 286 | size_t count = aiocb->aio_nbytes, copy; |
| 287 | int i; |
| 288 | |
| 289 | for (i = 0; i < aiocb->aio_niov && count; ++i) { |
| 290 | copy = count; |
| 291 | if (copy > aiocb->aio_iov[i].iov_len) |
| 292 | copy = aiocb->aio_iov[i].iov_len; |
| 293 | memcpy(aiocb->aio_iov[i].iov_base, p, copy); |
| 294 | p += copy; |
| 295 | count -= copy; |
| 296 | } |
| 297 | } |
| 298 | qemu_vfree(buf); |
| 299 | |
| 300 | return nbytes; |
aliguori | 221f715 | 2009-03-28 17:28:41 +0000 | [diff] [blame] | 301 | } |
| 302 | |
aliguori | 3c529d9 | 2008-12-12 16:41:40 +0000 | [diff] [blame] | 303 | static void *aio_thread(void *unused) |
| 304 | { |
malc | a8227a5 | 2009-02-21 05:48:17 +0000 | [diff] [blame] | 305 | pid_t pid; |
aliguori | 3c529d9 | 2008-12-12 16:41:40 +0000 | [diff] [blame] | 306 | |
malc | a8227a5 | 2009-02-21 05:48:17 +0000 | [diff] [blame] | 307 | pid = getpid(); |
| 308 | |
aliguori | 3c529d9 | 2008-12-12 16:41:40 +0000 | [diff] [blame] | 309 | while (1) { |
| 310 | struct qemu_paiocb *aiocb; |
Kevin Wolf | 6769da2 | 2009-11-18 12:15:10 +0100 | [diff] [blame] | 311 | ssize_t ret = 0; |
malc | 30525af | 2009-02-21 05:48:13 +0000 | [diff] [blame] | 312 | qemu_timeval tv; |
| 313 | struct timespec ts; |
| 314 | |
| 315 | qemu_gettimeofday(&tv); |
| 316 | ts.tv_sec = tv.tv_sec + 10; |
| 317 | ts.tv_nsec = 0; |
aliguori | 3c529d9 | 2008-12-12 16:41:40 +0000 | [diff] [blame] | 318 | |
malc | 8653c01 | 2009-02-21 05:48:11 +0000 | [diff] [blame] | 319 | mutex_lock(&lock); |
aliguori | 3c529d9 | 2008-12-12 16:41:40 +0000 | [diff] [blame] | 320 | |
Blue Swirl | 72cf2d4 | 2009-09-12 07:36:22 +0000 | [diff] [blame] | 321 | while (QTAILQ_EMPTY(&request_list) && |
aliguori | 3c529d9 | 2008-12-12 16:41:40 +0000 | [diff] [blame] | 322 | !(ret == ETIMEDOUT)) { |
malc | 8653c01 | 2009-02-21 05:48:11 +0000 | [diff] [blame] | 323 | ret = cond_timedwait(&cond, &lock, &ts); |
aliguori | 3c529d9 | 2008-12-12 16:41:40 +0000 | [diff] [blame] | 324 | } |
| 325 | |
Blue Swirl | 72cf2d4 | 2009-09-12 07:36:22 +0000 | [diff] [blame] | 326 | if (QTAILQ_EMPTY(&request_list)) |
aliguori | 3c529d9 | 2008-12-12 16:41:40 +0000 | [diff] [blame] | 327 | break; |
| 328 | |
Blue Swirl | 72cf2d4 | 2009-09-12 07:36:22 +0000 | [diff] [blame] | 329 | aiocb = QTAILQ_FIRST(&request_list); |
| 330 | QTAILQ_REMOVE(&request_list, aiocb, node); |
aliguori | 3c529d9 | 2008-12-12 16:41:40 +0000 | [diff] [blame] | 331 | aiocb->active = 1; |
aliguori | 3c529d9 | 2008-12-12 16:41:40 +0000 | [diff] [blame] | 332 | idle_threads--; |
malc | 8653c01 | 2009-02-21 05:48:11 +0000 | [diff] [blame] | 333 | mutex_unlock(&lock); |
aliguori | 3c529d9 | 2008-12-12 16:41:40 +0000 | [diff] [blame] | 334 | |
Christoph Hellwig | 9ef91a6 | 2009-08-20 16:58:19 +0200 | [diff] [blame] | 335 | switch (aiocb->aio_type & QEMU_AIO_TYPE_MASK) { |
| 336 | case QEMU_AIO_READ: |
| 337 | case QEMU_AIO_WRITE: |
aliguori | f141eaf | 2009-04-07 18:43:24 +0000 | [diff] [blame] | 338 | ret = handle_aiocb_rw(aiocb); |
aliguori | 221f715 | 2009-03-28 17:28:41 +0000 | [diff] [blame] | 339 | break; |
Christoph Hellwig | b2e12bc | 2009-09-04 19:01:49 +0200 | [diff] [blame] | 340 | case QEMU_AIO_FLUSH: |
| 341 | ret = handle_aiocb_flush(aiocb); |
| 342 | break; |
Christoph Hellwig | 9ef91a6 | 2009-08-20 16:58:19 +0200 | [diff] [blame] | 343 | case QEMU_AIO_IOCTL: |
aliguori | 221f715 | 2009-03-28 17:28:41 +0000 | [diff] [blame] | 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 | } |
aliguori | 3c529d9 | 2008-12-12 16:41:40 +0000 | [diff] [blame] | 351 | |
malc | 8653c01 | 2009-02-21 05:48:11 +0000 | [diff] [blame] | 352 | mutex_lock(&lock); |
aliguori | 221f715 | 2009-03-28 17:28:41 +0000 | [diff] [blame] | 353 | aiocb->ret = ret; |
aliguori | 3c529d9 | 2008-12-12 16:41:40 +0000 | [diff] [blame] | 354 | idle_threads++; |
malc | 8653c01 | 2009-02-21 05:48:11 +0000 | [diff] [blame] | 355 | mutex_unlock(&lock); |
aliguori | 3c529d9 | 2008-12-12 16:41:40 +0000 | [diff] [blame] | 356 | |
malc | a8227a5 | 2009-02-21 05:48:17 +0000 | [diff] [blame] | 357 | if (kill(pid, aiocb->ev_signo)) die("kill failed"); |
aliguori | 3c529d9 | 2008-12-12 16:41:40 +0000 | [diff] [blame] | 358 | } |
| 359 | |
| 360 | idle_threads--; |
| 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++; |
| 372 | idle_threads++; |
malc | ee39930 | 2009-09-25 00:20:44 +0400 | [diff] [blame] | 373 | |
| 374 | /* block all signals */ |
| 375 | if (sigfillset(&set)) die("sigfillset"); |
| 376 | if (sigprocmask(SIG_SETMASK, &set, &oldset)) die("sigprocmask"); |
| 377 | |
malc | 8653c01 | 2009-02-21 05:48:11 +0000 | [diff] [blame] | 378 | thread_create(&thread_id, &attr, aio_thread, NULL); |
malc | ee39930 | 2009-09-25 00:20:44 +0400 | [diff] [blame] | 379 | |
| 380 | if (sigprocmask(SIG_SETMASK, &oldset, NULL)) die("sigprocmask restore"); |
aliguori | 3c529d9 | 2008-12-12 16:41:40 +0000 | [diff] [blame] | 381 | } |
| 382 | |
Christoph Hellwig | 9ef91a6 | 2009-08-20 16:58:19 +0200 | [diff] [blame] | 383 | static void qemu_paio_submit(struct qemu_paiocb *aiocb) |
aliguori | 3c529d9 | 2008-12-12 16:41:40 +0000 | [diff] [blame] | 384 | { |
aliguori | 3c529d9 | 2008-12-12 16:41:40 +0000 | [diff] [blame] | 385 | aiocb->ret = -EINPROGRESS; |
| 386 | aiocb->active = 0; |
malc | 8653c01 | 2009-02-21 05:48:11 +0000 | [diff] [blame] | 387 | mutex_lock(&lock); |
aliguori | 3c529d9 | 2008-12-12 16:41:40 +0000 | [diff] [blame] | 388 | if (idle_threads == 0 && cur_threads < max_threads) |
| 389 | spawn_thread(); |
Blue Swirl | 72cf2d4 | 2009-09-12 07:36:22 +0000 | [diff] [blame] | 390 | QTAILQ_INSERT_TAIL(&request_list, aiocb, node); |
malc | 8653c01 | 2009-02-21 05:48:11 +0000 | [diff] [blame] | 391 | mutex_unlock(&lock); |
malc | 5d47e37 | 2009-02-21 05:48:15 +0000 | [diff] [blame] | 392 | cond_signal(&cond); |
aliguori | 3c529d9 | 2008-12-12 16:41:40 +0000 | [diff] [blame] | 393 | } |
| 394 | |
Christoph Hellwig | 9ef91a6 | 2009-08-20 16:58:19 +0200 | [diff] [blame] | 395 | static ssize_t qemu_paio_return(struct qemu_paiocb *aiocb) |
aliguori | 3c529d9 | 2008-12-12 16:41:40 +0000 | [diff] [blame] | 396 | { |
| 397 | ssize_t ret; |
| 398 | |
malc | 8653c01 | 2009-02-21 05:48:11 +0000 | [diff] [blame] | 399 | mutex_lock(&lock); |
aliguori | 3c529d9 | 2008-12-12 16:41:40 +0000 | [diff] [blame] | 400 | ret = aiocb->ret; |
malc | 8653c01 | 2009-02-21 05:48:11 +0000 | [diff] [blame] | 401 | mutex_unlock(&lock); |
aliguori | 3c529d9 | 2008-12-12 16:41:40 +0000 | [diff] [blame] | 402 | |
| 403 | return ret; |
| 404 | } |
| 405 | |
Christoph Hellwig | 9ef91a6 | 2009-08-20 16:58:19 +0200 | [diff] [blame] | 406 | static int qemu_paio_error(struct qemu_paiocb *aiocb) |
aliguori | 3c529d9 | 2008-12-12 16:41:40 +0000 | [diff] [blame] | 407 | { |
| 408 | ssize_t ret = qemu_paio_return(aiocb); |
| 409 | |
| 410 | if (ret < 0) |
| 411 | ret = -ret; |
| 412 | else |
| 413 | ret = 0; |
| 414 | |
| 415 | return ret; |
| 416 | } |
| 417 | |
Kevin Wolf | 59c7b15 | 2009-10-22 17:54:35 +0200 | [diff] [blame] | 418 | static int posix_aio_process_queue(void *opaque) |
aliguori | 3c529d9 | 2008-12-12 16:41:40 +0000 | [diff] [blame] | 419 | { |
Christoph Hellwig | 9ef91a6 | 2009-08-20 16:58:19 +0200 | [diff] [blame] | 420 | PosixAioState *s = opaque; |
| 421 | struct qemu_paiocb *acb, **pacb; |
aliguori | 3c529d9 | 2008-12-12 16:41:40 +0000 | [diff] [blame] | 422 | int ret; |
Kevin Wolf | 59c7b15 | 2009-10-22 17:54:35 +0200 | [diff] [blame] | 423 | int result = 0; |
Kevin Wolf | e5f3764 | 2009-10-22 17:54:40 +0200 | [diff] [blame] | 424 | int async_context_id = get_async_context_id(); |
Christoph Hellwig | 9ef91a6 | 2009-08-20 16:58:19 +0200 | [diff] [blame] | 425 | |
| 426 | for(;;) { |
| 427 | pacb = &s->first_aio; |
| 428 | for(;;) { |
| 429 | acb = *pacb; |
| 430 | if (!acb) |
Kevin Wolf | 59c7b15 | 2009-10-22 17:54:35 +0200 | [diff] [blame] | 431 | return result; |
Kevin Wolf | e5f3764 | 2009-10-22 17:54:40 +0200 | [diff] [blame] | 432 | |
| 433 | /* we're only interested in requests in the right context */ |
| 434 | if (acb->async_context_id != async_context_id) { |
| 435 | pacb = &acb->next; |
| 436 | continue; |
| 437 | } |
| 438 | |
Christoph Hellwig | 9ef91a6 | 2009-08-20 16:58:19 +0200 | [diff] [blame] | 439 | ret = qemu_paio_error(acb); |
| 440 | if (ret == ECANCELED) { |
| 441 | /* remove the request */ |
| 442 | *pacb = acb->next; |
| 443 | qemu_aio_release(acb); |
Kevin Wolf | 59c7b15 | 2009-10-22 17:54:35 +0200 | [diff] [blame] | 444 | result = 1; |
Christoph Hellwig | 9ef91a6 | 2009-08-20 16:58:19 +0200 | [diff] [blame] | 445 | } else if (ret != EINPROGRESS) { |
| 446 | /* end of aio */ |
| 447 | if (ret == 0) { |
| 448 | ret = qemu_paio_return(acb); |
| 449 | if (ret == acb->aio_nbytes) |
| 450 | ret = 0; |
| 451 | else |
| 452 | ret = -EINVAL; |
| 453 | } else { |
| 454 | ret = -ret; |
| 455 | } |
| 456 | /* remove the request */ |
| 457 | *pacb = acb->next; |
| 458 | /* call the callback */ |
| 459 | acb->common.cb(acb->common.opaque, ret); |
| 460 | qemu_aio_release(acb); |
Kevin Wolf | 59c7b15 | 2009-10-22 17:54:35 +0200 | [diff] [blame] | 461 | result = 1; |
Christoph Hellwig | 9ef91a6 | 2009-08-20 16:58:19 +0200 | [diff] [blame] | 462 | break; |
| 463 | } else { |
| 464 | pacb = &acb->next; |
| 465 | } |
| 466 | } |
| 467 | } |
Kevin Wolf | 59c7b15 | 2009-10-22 17:54:35 +0200 | [diff] [blame] | 468 | |
| 469 | return result; |
| 470 | } |
| 471 | |
| 472 | static void posix_aio_read(void *opaque) |
| 473 | { |
| 474 | PosixAioState *s = opaque; |
| 475 | ssize_t len; |
| 476 | |
| 477 | /* read all bytes from signal pipe */ |
| 478 | for (;;) { |
| 479 | char bytes[16]; |
| 480 | |
| 481 | len = read(s->rfd, bytes, sizeof(bytes)); |
| 482 | if (len == -1 && errno == EINTR) |
| 483 | continue; /* try again */ |
| 484 | if (len == sizeof(bytes)) |
| 485 | continue; /* more to read */ |
| 486 | break; |
| 487 | } |
| 488 | |
| 489 | posix_aio_process_queue(s); |
Christoph Hellwig | 9ef91a6 | 2009-08-20 16:58:19 +0200 | [diff] [blame] | 490 | } |
| 491 | |
| 492 | static int posix_aio_flush(void *opaque) |
| 493 | { |
| 494 | PosixAioState *s = opaque; |
| 495 | return !!s->first_aio; |
| 496 | } |
| 497 | |
| 498 | static PosixAioState *posix_aio_state; |
| 499 | |
| 500 | static void aio_signal_handler(int signum) |
| 501 | { |
| 502 | if (posix_aio_state) { |
| 503 | char byte = 0; |
Kirill A. Shutemov | 4817d32 | 2010-01-20 00:56:10 +0100 | [diff] [blame^] | 504 | ssize_t ret; |
Christoph Hellwig | 9ef91a6 | 2009-08-20 16:58:19 +0200 | [diff] [blame] | 505 | |
Kirill A. Shutemov | 4817d32 | 2010-01-20 00:56:10 +0100 | [diff] [blame^] | 506 | ret = write(posix_aio_state->wfd, &byte, sizeof(byte)); |
| 507 | if (ret < 0 && errno != EAGAIN) |
| 508 | die("write()"); |
Christoph Hellwig | 9ef91a6 | 2009-08-20 16:58:19 +0200 | [diff] [blame] | 509 | } |
| 510 | |
| 511 | qemu_service_io(); |
| 512 | } |
| 513 | |
| 514 | static void paio_remove(struct qemu_paiocb *acb) |
| 515 | { |
| 516 | struct qemu_paiocb **pacb; |
| 517 | |
| 518 | /* remove the callback from the queue */ |
| 519 | pacb = &posix_aio_state->first_aio; |
| 520 | for(;;) { |
| 521 | if (*pacb == NULL) { |
| 522 | fprintf(stderr, "paio_remove: aio request not found!\n"); |
| 523 | break; |
| 524 | } else if (*pacb == acb) { |
| 525 | *pacb = acb->next; |
| 526 | qemu_aio_release(acb); |
| 527 | break; |
| 528 | } |
| 529 | pacb = &(*pacb)->next; |
| 530 | } |
| 531 | } |
| 532 | |
| 533 | static void paio_cancel(BlockDriverAIOCB *blockacb) |
| 534 | { |
| 535 | struct qemu_paiocb *acb = (struct qemu_paiocb *)blockacb; |
| 536 | int active = 0; |
aliguori | 3c529d9 | 2008-12-12 16:41:40 +0000 | [diff] [blame] | 537 | |
malc | 8653c01 | 2009-02-21 05:48:11 +0000 | [diff] [blame] | 538 | mutex_lock(&lock); |
Christoph Hellwig | 9ef91a6 | 2009-08-20 16:58:19 +0200 | [diff] [blame] | 539 | if (!acb->active) { |
Blue Swirl | 72cf2d4 | 2009-09-12 07:36:22 +0000 | [diff] [blame] | 540 | QTAILQ_REMOVE(&request_list, acb, node); |
Christoph Hellwig | 9ef91a6 | 2009-08-20 16:58:19 +0200 | [diff] [blame] | 541 | acb->ret = -ECANCELED; |
| 542 | } else if (acb->ret == -EINPROGRESS) { |
| 543 | active = 1; |
| 544 | } |
malc | 8653c01 | 2009-02-21 05:48:11 +0000 | [diff] [blame] | 545 | mutex_unlock(&lock); |
aliguori | 3c529d9 | 2008-12-12 16:41:40 +0000 | [diff] [blame] | 546 | |
Christoph Hellwig | 9ef91a6 | 2009-08-20 16:58:19 +0200 | [diff] [blame] | 547 | if (active) { |
| 548 | /* fail safe: if the aio could not be canceled, we wait for |
| 549 | it */ |
| 550 | while (qemu_paio_error(acb) == EINPROGRESS) |
| 551 | ; |
| 552 | } |
| 553 | |
| 554 | paio_remove(acb); |
| 555 | } |
| 556 | |
| 557 | static AIOPool raw_aio_pool = { |
| 558 | .aiocb_size = sizeof(struct qemu_paiocb), |
| 559 | .cancel = paio_cancel, |
| 560 | }; |
| 561 | |
Kevin Wolf | 1e5b9d2 | 2009-10-26 13:03:08 +0100 | [diff] [blame] | 562 | BlockDriverAIOCB *paio_submit(BlockDriverState *bs, int fd, |
Christoph Hellwig | 9ef91a6 | 2009-08-20 16:58:19 +0200 | [diff] [blame] | 563 | int64_t sector_num, QEMUIOVector *qiov, int nb_sectors, |
| 564 | BlockDriverCompletionFunc *cb, void *opaque, int type) |
| 565 | { |
| 566 | struct qemu_paiocb *acb; |
| 567 | |
| 568 | acb = qemu_aio_get(&raw_aio_pool, bs, cb, opaque); |
| 569 | if (!acb) |
| 570 | return NULL; |
| 571 | acb->aio_type = type; |
| 572 | acb->aio_fildes = fd; |
| 573 | acb->ev_signo = SIGUSR2; |
Kevin Wolf | e5f3764 | 2009-10-22 17:54:40 +0200 | [diff] [blame] | 574 | acb->async_context_id = get_async_context_id(); |
| 575 | |
Christoph Hellwig | b2e12bc | 2009-09-04 19:01:49 +0200 | [diff] [blame] | 576 | if (qiov) { |
| 577 | acb->aio_iov = qiov->iov; |
| 578 | acb->aio_niov = qiov->niov; |
| 579 | } |
Christoph Hellwig | 9ef91a6 | 2009-08-20 16:58:19 +0200 | [diff] [blame] | 580 | acb->aio_nbytes = nb_sectors * 512; |
| 581 | acb->aio_offset = sector_num * 512; |
| 582 | |
| 583 | acb->next = posix_aio_state->first_aio; |
| 584 | posix_aio_state->first_aio = acb; |
| 585 | |
| 586 | qemu_paio_submit(acb); |
| 587 | return &acb->common; |
| 588 | } |
| 589 | |
| 590 | BlockDriverAIOCB *paio_ioctl(BlockDriverState *bs, int fd, |
| 591 | unsigned long int req, void *buf, |
| 592 | BlockDriverCompletionFunc *cb, void *opaque) |
| 593 | { |
| 594 | struct qemu_paiocb *acb; |
| 595 | |
| 596 | acb = qemu_aio_get(&raw_aio_pool, bs, cb, opaque); |
| 597 | if (!acb) |
| 598 | return NULL; |
| 599 | acb->aio_type = QEMU_AIO_IOCTL; |
| 600 | acb->aio_fildes = fd; |
| 601 | acb->ev_signo = SIGUSR2; |
| 602 | acb->aio_offset = 0; |
| 603 | acb->aio_ioctl_buf = buf; |
| 604 | acb->aio_ioctl_cmd = req; |
| 605 | |
| 606 | acb->next = posix_aio_state->first_aio; |
| 607 | posix_aio_state->first_aio = acb; |
| 608 | |
| 609 | qemu_paio_submit(acb); |
| 610 | return &acb->common; |
| 611 | } |
| 612 | |
Kevin Wolf | 1e5b9d2 | 2009-10-26 13:03:08 +0100 | [diff] [blame] | 613 | int paio_init(void) |
Christoph Hellwig | 9ef91a6 | 2009-08-20 16:58:19 +0200 | [diff] [blame] | 614 | { |
| 615 | struct sigaction act; |
| 616 | PosixAioState *s; |
| 617 | int fds[2]; |
| 618 | int ret; |
| 619 | |
| 620 | if (posix_aio_state) |
Kevin Wolf | 1e5b9d2 | 2009-10-26 13:03:08 +0100 | [diff] [blame] | 621 | return 0; |
Christoph Hellwig | 9ef91a6 | 2009-08-20 16:58:19 +0200 | [diff] [blame] | 622 | |
| 623 | s = qemu_malloc(sizeof(PosixAioState)); |
| 624 | |
| 625 | sigfillset(&act.sa_mask); |
| 626 | act.sa_flags = 0; /* do not restart syscalls to interrupt select() */ |
| 627 | act.sa_handler = aio_signal_handler; |
| 628 | sigaction(SIGUSR2, &act, NULL); |
| 629 | |
| 630 | s->first_aio = NULL; |
Kevin Wolf | 40ff6d7 | 2009-12-02 12:24:42 +0100 | [diff] [blame] | 631 | if (qemu_pipe(fds) == -1) { |
Christoph Hellwig | 9ef91a6 | 2009-08-20 16:58:19 +0200 | [diff] [blame] | 632 | fprintf(stderr, "failed to create pipe\n"); |
Kevin Wolf | 1e5b9d2 | 2009-10-26 13:03:08 +0100 | [diff] [blame] | 633 | return -1; |
Christoph Hellwig | 9ef91a6 | 2009-08-20 16:58:19 +0200 | [diff] [blame] | 634 | } |
| 635 | |
| 636 | s->rfd = fds[0]; |
| 637 | s->wfd = fds[1]; |
| 638 | |
| 639 | fcntl(s->rfd, F_SETFL, O_NONBLOCK); |
| 640 | fcntl(s->wfd, F_SETFL, O_NONBLOCK); |
| 641 | |
Kevin Wolf | 8febfa2 | 2009-10-22 17:54:36 +0200 | [diff] [blame] | 642 | qemu_aio_set_fd_handler(s->rfd, posix_aio_read, NULL, posix_aio_flush, |
| 643 | posix_aio_process_queue, s); |
Christoph Hellwig | 9ef91a6 | 2009-08-20 16:58:19 +0200 | [diff] [blame] | 644 | |
| 645 | ret = pthread_attr_init(&attr); |
| 646 | if (ret) |
| 647 | die2(ret, "pthread_attr_init"); |
| 648 | |
| 649 | ret = pthread_attr_setdetachstate(&attr, PTHREAD_CREATE_DETACHED); |
| 650 | if (ret) |
| 651 | die2(ret, "pthread_attr_setdetachstate"); |
| 652 | |
Blue Swirl | 72cf2d4 | 2009-09-12 07:36:22 +0000 | [diff] [blame] | 653 | QTAILQ_INIT(&request_list); |
Christoph Hellwig | 9ef91a6 | 2009-08-20 16:58:19 +0200 | [diff] [blame] | 654 | |
| 655 | posix_aio_state = s; |
Kevin Wolf | 1e5b9d2 | 2009-10-26 13:03:08 +0100 | [diff] [blame] | 656 | return 0; |
aliguori | 3c529d9 | 2008-12-12 16:41:40 +0000 | [diff] [blame] | 657 | } |