]>
Commit | Line | Data |
---|---|---|
3c529d93 AL |
1 | /* |
2 | * QEMU posix-aio emulation | |
3 | * | |
4 | * Copyright IBM, Corp. 2008 | |
5 | * | |
6 | * Authors: | |
7 | * Anthony Liguori <[email protected]> | |
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 | ||
221f715d | 14 | #include <sys/ioctl.h> |
9ef91a67 | 15 | #include <sys/types.h> |
3c529d93 AL |
16 | #include <pthread.h> |
17 | #include <unistd.h> | |
18 | #include <errno.h> | |
30525aff | 19 | #include <time.h> |
8653c015 | 20 | #include <string.h> |
21 | #include <stdlib.h> | |
22 | #include <stdio.h> | |
9ef91a67 | 23 | |
72cf2d4f | 24 | #include "qemu-queue.h" |
3c529d93 | 25 | #include "osdep.h" |
dc786bc9 | 26 | #include "sysemu.h" |
f141eafe | 27 | #include "qemu-common.h" |
6d519a5f | 28 | #include "trace.h" |
9ef91a67 CH |
29 | #include "block_int.h" |
30 | ||
31 | #include "block/raw-posix-aio.h" | |
32 | ||
e4ea78ee | 33 | static void do_spawn_thread(void); |
9ef91a67 CH |
34 | |
35 | struct qemu_paiocb { | |
36 | BlockDriverAIOCB common; | |
37 | int aio_fildes; | |
38 | union { | |
39 | struct iovec *aio_iov; | |
b587a52c | 40 | void *aio_ioctl_buf; |
9ef91a67 CH |
41 | }; |
42 | int aio_niov; | |
43 | size_t aio_nbytes; | |
44 | #define aio_ioctl_cmd aio_nbytes /* for QEMU_AIO_IOCTL */ | |
9ef91a67 CH |
45 | off_t aio_offset; |
46 | ||
72cf2d4f | 47 | QTAILQ_ENTRY(qemu_paiocb) node; |
9ef91a67 CH |
48 | int aio_type; |
49 | ssize_t ret; | |
50 | int active; | |
51 | struct qemu_paiocb *next; | |
52 | }; | |
53 | ||
54 | typedef struct PosixAioState { | |
55 | int rfd, wfd; | |
56 | struct qemu_paiocb *first_aio; | |
57 | } PosixAioState; | |
3c529d93 | 58 | |
3c529d93 AL |
59 | |
60 | static pthread_mutex_t lock = PTHREAD_MUTEX_INITIALIZER; | |
61 | static pthread_cond_t cond = PTHREAD_COND_INITIALIZER; | |
62 | static pthread_t thread_id; | |
a8227a5a | 63 | static pthread_attr_t attr; |
3c529d93 AL |
64 | static int max_threads = 64; |
65 | static int cur_threads = 0; | |
66 | static int idle_threads = 0; | |
e4ea78ee AK |
67 | static int new_threads = 0; /* backlog of threads we need to create */ |
68 | static int pending_threads = 0; /* threads created but not running yet */ | |
69 | static QEMUBH *new_thread_bh; | |
72cf2d4f | 70 | static QTAILQ_HEAD(, qemu_paiocb) request_list; |
3c529d93 | 71 | |
2341f9a1 | 72 | #ifdef CONFIG_PREADV |
ceb42de8 AL |
73 | static int preadv_present = 1; |
74 | #else | |
75 | static int preadv_present = 0; | |
76 | #endif | |
77 | ||
8653c015 | 78 | static void die2(int err, const char *what) |
79 | { | |
80 | fprintf(stderr, "%s failed: %s\n", what, strerror(err)); | |
81 | abort(); | |
82 | } | |
83 | ||
84 | static void die(const char *what) | |
85 | { | |
86 | die2(errno, what); | |
87 | } | |
88 | ||
89 | static void mutex_lock(pthread_mutex_t *mutex) | |
90 | { | |
91 | int ret = pthread_mutex_lock(mutex); | |
92 | if (ret) die2(ret, "pthread_mutex_lock"); | |
93 | } | |
94 | ||
95 | static void mutex_unlock(pthread_mutex_t *mutex) | |
96 | { | |
97 | int ret = pthread_mutex_unlock(mutex); | |
98 | if (ret) die2(ret, "pthread_mutex_unlock"); | |
99 | } | |
100 | ||
101 | static int cond_timedwait(pthread_cond_t *cond, pthread_mutex_t *mutex, | |
102 | struct timespec *ts) | |
103 | { | |
104 | int ret = pthread_cond_timedwait(cond, mutex, ts); | |
105 | if (ret && ret != ETIMEDOUT) die2(ret, "pthread_cond_timedwait"); | |
106 | return ret; | |
107 | } | |
108 | ||
5d47e372 | 109 | static void cond_signal(pthread_cond_t *cond) |
8653c015 | 110 | { |
5d47e372 | 111 | int ret = pthread_cond_signal(cond); |
112 | if (ret) die2(ret, "pthread_cond_signal"); | |
8653c015 | 113 | } |
114 | ||
115 | static void thread_create(pthread_t *thread, pthread_attr_t *attr, | |
116 | void *(*start_routine)(void*), void *arg) | |
117 | { | |
118 | int ret = pthread_create(thread, attr, start_routine, arg); | |
119 | if (ret) die2(ret, "pthread_create"); | |
120 | } | |
121 | ||
6769da29 | 122 | static ssize_t handle_aiocb_ioctl(struct qemu_paiocb *aiocb) |
f141eafe | 123 | { |
b587a52c SH |
124 | int ret; |
125 | ||
126 | ret = ioctl(aiocb->aio_fildes, aiocb->aio_ioctl_cmd, aiocb->aio_ioctl_buf); | |
127 | if (ret == -1) | |
128 | return -errno; | |
129 | ||
130 | /* | |
131 | * This looks weird, but the aio code only consideres a request | |
b0cd712c | 132 | * successful if it has written the number full number of bytes. |
b587a52c SH |
133 | * |
134 | * Now we overload aio_nbytes as aio_ioctl_cmd for the ioctl command, | |
135 | * so in fact we return the ioctl command here to make posix_aio_read() | |
136 | * happy.. | |
137 | */ | |
138 | return aiocb->aio_nbytes; | |
f141eafe AL |
139 | } |
140 | ||
6769da29 | 141 | static ssize_t handle_aiocb_flush(struct qemu_paiocb *aiocb) |
b2e12bc6 CH |
142 | { |
143 | int ret; | |
144 | ||
47faadc6 | 145 | ret = qemu_fdatasync(aiocb->aio_fildes); |
b2e12bc6 CH |
146 | if (ret == -1) |
147 | return -errno; | |
148 | return 0; | |
149 | } | |
150 | ||
2341f9a1 | 151 | #ifdef CONFIG_PREADV |
ceb42de8 AL |
152 | |
153 | static ssize_t | |
154 | qemu_preadv(int fd, const struct iovec *iov, int nr_iov, off_t offset) | |
155 | { | |
156 | return preadv(fd, iov, nr_iov, offset); | |
157 | } | |
158 | ||
159 | static ssize_t | |
160 | qemu_pwritev(int fd, const struct iovec *iov, int nr_iov, off_t offset) | |
161 | { | |
162 | return pwritev(fd, iov, nr_iov, offset); | |
163 | } | |
164 | ||
165 | #else | |
166 | ||
167 | static ssize_t | |
168 | qemu_preadv(int fd, const struct iovec *iov, int nr_iov, off_t offset) | |
169 | { | |
170 | return -ENOSYS; | |
171 | } | |
172 | ||
173 | static ssize_t | |
174 | qemu_pwritev(int fd, const struct iovec *iov, int nr_iov, off_t offset) | |
175 | { | |
176 | return -ENOSYS; | |
177 | } | |
178 | ||
179 | #endif | |
180 | ||
6769da29 | 181 | static ssize_t handle_aiocb_rw_vector(struct qemu_paiocb *aiocb) |
ceb42de8 | 182 | { |
ceb42de8 AL |
183 | ssize_t len; |
184 | ||
185 | do { | |
9ef91a67 | 186 | if (aiocb->aio_type & QEMU_AIO_WRITE) |
ceb42de8 AL |
187 | len = qemu_pwritev(aiocb->aio_fildes, |
188 | aiocb->aio_iov, | |
189 | aiocb->aio_niov, | |
21cfa41e | 190 | aiocb->aio_offset); |
ceb42de8 AL |
191 | else |
192 | len = qemu_preadv(aiocb->aio_fildes, | |
193 | aiocb->aio_iov, | |
194 | aiocb->aio_niov, | |
21cfa41e | 195 | aiocb->aio_offset); |
ceb42de8 AL |
196 | } while (len == -1 && errno == EINTR); |
197 | ||
198 | if (len == -1) | |
199 | return -errno; | |
200 | return len; | |
201 | } | |
202 | ||
ba1d1afd KW |
203 | /* |
204 | * Read/writes the data to/from a given linear buffer. | |
205 | * | |
206 | * Returns the number of bytes handles or -errno in case of an error. Short | |
207 | * reads are only returned if the end of the file is reached. | |
208 | */ | |
6769da29 | 209 | static ssize_t handle_aiocb_rw_linear(struct qemu_paiocb *aiocb, char *buf) |
221f715d | 210 | { |
6769da29 KW |
211 | ssize_t offset = 0; |
212 | ssize_t len; | |
221f715d AL |
213 | |
214 | while (offset < aiocb->aio_nbytes) { | |
9ef91a67 | 215 | if (aiocb->aio_type & QEMU_AIO_WRITE) |
f141eafe AL |
216 | len = pwrite(aiocb->aio_fildes, |
217 | (const char *)buf + offset, | |
218 | aiocb->aio_nbytes - offset, | |
219 | aiocb->aio_offset + offset); | |
220 | else | |
221 | len = pread(aiocb->aio_fildes, | |
222 | buf + offset, | |
221f715d AL |
223 | aiocb->aio_nbytes - offset, |
224 | aiocb->aio_offset + offset); | |
221f715d | 225 | |
f141eafe AL |
226 | if (len == -1 && errno == EINTR) |
227 | continue; | |
228 | else if (len == -1) { | |
229 | offset = -errno; | |
230 | break; | |
231 | } else if (len == 0) | |
232 | break; | |
233 | ||
234 | offset += len; | |
221f715d AL |
235 | } |
236 | ||
237 | return offset; | |
238 | } | |
239 | ||
6769da29 | 240 | static ssize_t handle_aiocb_rw(struct qemu_paiocb *aiocb) |
221f715d | 241 | { |
6769da29 | 242 | ssize_t nbytes; |
f141eafe AL |
243 | char *buf; |
244 | ||
9ef91a67 | 245 | if (!(aiocb->aio_type & QEMU_AIO_MISALIGNED)) { |
f141eafe AL |
246 | /* |
247 | * If there is just a single buffer, and it is properly aligned | |
248 | * we can just use plain pread/pwrite without any problems. | |
249 | */ | |
ceb42de8 AL |
250 | if (aiocb->aio_niov == 1) |
251 | return handle_aiocb_rw_linear(aiocb, aiocb->aio_iov->iov_base); | |
252 | ||
253 | /* | |
254 | * We have more than one iovec, and all are properly aligned. | |
255 | * | |
256 | * Try preadv/pwritev first and fall back to linearizing the | |
257 | * buffer if it's not supported. | |
258 | */ | |
b587a52c | 259 | if (preadv_present) { |
ceb42de8 AL |
260 | nbytes = handle_aiocb_rw_vector(aiocb); |
261 | if (nbytes == aiocb->aio_nbytes) | |
b587a52c | 262 | return nbytes; |
ceb42de8 AL |
263 | if (nbytes < 0 && nbytes != -ENOSYS) |
264 | return nbytes; | |
265 | preadv_present = 0; | |
266 | } | |
267 | ||
268 | /* | |
269 | * XXX(hch): short read/write. no easy way to handle the reminder | |
270 | * using these interfaces. For now retry using plain | |
271 | * pread/pwrite? | |
272 | */ | |
f141eafe | 273 | } |
221f715d | 274 | |
f141eafe AL |
275 | /* |
276 | * Ok, we have to do it the hard way, copy all segments into | |
277 | * a single aligned buffer. | |
278 | */ | |
72aef731 | 279 | buf = qemu_blockalign(aiocb->common.bs, aiocb->aio_nbytes); |
9ef91a67 | 280 | if (aiocb->aio_type & QEMU_AIO_WRITE) { |
f141eafe AL |
281 | char *p = buf; |
282 | int i; | |
283 | ||
284 | for (i = 0; i < aiocb->aio_niov; ++i) { | |
285 | memcpy(p, aiocb->aio_iov[i].iov_base, aiocb->aio_iov[i].iov_len); | |
286 | p += aiocb->aio_iov[i].iov_len; | |
287 | } | |
288 | } | |
289 | ||
290 | nbytes = handle_aiocb_rw_linear(aiocb, buf); | |
9ef91a67 | 291 | if (!(aiocb->aio_type & QEMU_AIO_WRITE)) { |
f141eafe AL |
292 | char *p = buf; |
293 | size_t count = aiocb->aio_nbytes, copy; | |
294 | int i; | |
295 | ||
296 | for (i = 0; i < aiocb->aio_niov && count; ++i) { | |
297 | copy = count; | |
298 | if (copy > aiocb->aio_iov[i].iov_len) | |
299 | copy = aiocb->aio_iov[i].iov_len; | |
300 | memcpy(aiocb->aio_iov[i].iov_base, p, copy); | |
301 | p += copy; | |
302 | count -= copy; | |
303 | } | |
304 | } | |
305 | qemu_vfree(buf); | |
306 | ||
307 | return nbytes; | |
221f715d AL |
308 | } |
309 | ||
e1d3b254 FZ |
310 | static void posix_aio_notify_event(void); |
311 | ||
3c529d93 AL |
312 | static void *aio_thread(void *unused) |
313 | { | |
e4ea78ee AK |
314 | mutex_lock(&lock); |
315 | pending_threads--; | |
316 | mutex_unlock(&lock); | |
317 | do_spawn_thread(); | |
318 | ||
3c529d93 AL |
319 | while (1) { |
320 | struct qemu_paiocb *aiocb; | |
6769da29 | 321 | ssize_t ret = 0; |
30525aff | 322 | qemu_timeval tv; |
323 | struct timespec ts; | |
324 | ||
325 | qemu_gettimeofday(&tv); | |
326 | ts.tv_sec = tv.tv_sec + 10; | |
327 | ts.tv_nsec = 0; | |
3c529d93 | 328 | |
8653c015 | 329 | mutex_lock(&lock); |
3c529d93 | 330 | |
72cf2d4f | 331 | while (QTAILQ_EMPTY(&request_list) && |
3c529d93 | 332 | !(ret == ETIMEDOUT)) { |
5be4aab7 | 333 | idle_threads++; |
8653c015 | 334 | ret = cond_timedwait(&cond, &lock, &ts); |
5be4aab7 | 335 | idle_threads--; |
3c529d93 AL |
336 | } |
337 | ||
72cf2d4f | 338 | if (QTAILQ_EMPTY(&request_list)) |
3c529d93 AL |
339 | break; |
340 | ||
72cf2d4f BS |
341 | aiocb = QTAILQ_FIRST(&request_list); |
342 | QTAILQ_REMOVE(&request_list, aiocb, node); | |
3c529d93 | 343 | aiocb->active = 1; |
8653c015 | 344 | mutex_unlock(&lock); |
3c529d93 | 345 | |
9ef91a67 CH |
346 | switch (aiocb->aio_type & QEMU_AIO_TYPE_MASK) { |
347 | case QEMU_AIO_READ: | |
ba1d1afd KW |
348 | ret = handle_aiocb_rw(aiocb); |
349 | if (ret >= 0 && ret < aiocb->aio_nbytes && aiocb->common.bs->growable) { | |
350 | /* A short read means that we have reached EOF. Pad the buffer | |
351 | * with zeros for bytes after EOF. */ | |
352 | QEMUIOVector qiov; | |
353 | ||
354 | qemu_iovec_init_external(&qiov, aiocb->aio_iov, | |
355 | aiocb->aio_niov); | |
356 | qemu_iovec_memset_skip(&qiov, 0, aiocb->aio_nbytes - ret, ret); | |
357 | ||
358 | ret = aiocb->aio_nbytes; | |
359 | } | |
360 | break; | |
9ef91a67 | 361 | case QEMU_AIO_WRITE: |
b587a52c SH |
362 | ret = handle_aiocb_rw(aiocb); |
363 | break; | |
b2e12bc6 | 364 | case QEMU_AIO_FLUSH: |
b587a52c SH |
365 | ret = handle_aiocb_flush(aiocb); |
366 | break; | |
9ef91a67 | 367 | case QEMU_AIO_IOCTL: |
b587a52c SH |
368 | ret = handle_aiocb_ioctl(aiocb); |
369 | break; | |
370 | default: | |
371 | fprintf(stderr, "invalid aio request (0x%x)\n", aiocb->aio_type); | |
372 | ret = -EINVAL; | |
373 | break; | |
374 | } | |
3c529d93 | 375 | |
8653c015 | 376 | mutex_lock(&lock); |
221f715d | 377 | aiocb->ret = ret; |
8653c015 | 378 | mutex_unlock(&lock); |
3c529d93 | 379 | |
e1d3b254 | 380 | posix_aio_notify_event(); |
3c529d93 AL |
381 | } |
382 | ||
3c529d93 | 383 | cur_threads--; |
8653c015 | 384 | mutex_unlock(&lock); |
3c529d93 AL |
385 | |
386 | return NULL; | |
387 | } | |
388 | ||
e4ea78ee | 389 | static void do_spawn_thread(void) |
3c529d93 | 390 | { |
ee399306 | 391 | sigset_t set, oldset; |
392 | ||
e4ea78ee AK |
393 | mutex_lock(&lock); |
394 | if (!new_threads) { | |
395 | mutex_unlock(&lock); | |
396 | return; | |
397 | } | |
398 | ||
399 | new_threads--; | |
400 | pending_threads++; | |
401 | ||
402 | mutex_unlock(&lock); | |
ee399306 | 403 | |
404 | /* block all signals */ | |
405 | if (sigfillset(&set)) die("sigfillset"); | |
406 | if (sigprocmask(SIG_SETMASK, &set, &oldset)) die("sigprocmask"); | |
407 | ||
8653c015 | 408 | thread_create(&thread_id, &attr, aio_thread, NULL); |
ee399306 | 409 | |
410 | if (sigprocmask(SIG_SETMASK, &oldset, NULL)) die("sigprocmask restore"); | |
3c529d93 AL |
411 | } |
412 | ||
e4ea78ee AK |
413 | static void spawn_thread_bh_fn(void *opaque) |
414 | { | |
415 | do_spawn_thread(); | |
416 | } | |
417 | ||
418 | static void spawn_thread(void) | |
419 | { | |
420 | cur_threads++; | |
421 | new_threads++; | |
422 | /* If there are threads being created, they will spawn new workers, so | |
423 | * we don't spend time creating many threads in a loop holding a mutex or | |
424 | * starving the current vcpu. | |
425 | * | |
426 | * If there are no idle threads, ask the main thread to create one, so we | |
427 | * inherit the correct affinity instead of the vcpu affinity. | |
428 | */ | |
429 | if (!pending_threads) { | |
430 | qemu_bh_schedule(new_thread_bh); | |
431 | } | |
432 | } | |
433 | ||
9ef91a67 | 434 | static void qemu_paio_submit(struct qemu_paiocb *aiocb) |
3c529d93 | 435 | { |
3c529d93 AL |
436 | aiocb->ret = -EINPROGRESS; |
437 | aiocb->active = 0; | |
8653c015 | 438 | mutex_lock(&lock); |
3c529d93 AL |
439 | if (idle_threads == 0 && cur_threads < max_threads) |
440 | spawn_thread(); | |
72cf2d4f | 441 | QTAILQ_INSERT_TAIL(&request_list, aiocb, node); |
8653c015 | 442 | mutex_unlock(&lock); |
5d47e372 | 443 | cond_signal(&cond); |
3c529d93 AL |
444 | } |
445 | ||
9ef91a67 | 446 | static ssize_t qemu_paio_return(struct qemu_paiocb *aiocb) |
3c529d93 AL |
447 | { |
448 | ssize_t ret; | |
449 | ||
8653c015 | 450 | mutex_lock(&lock); |
3c529d93 | 451 | ret = aiocb->ret; |
8653c015 | 452 | mutex_unlock(&lock); |
3c529d93 AL |
453 | |
454 | return ret; | |
455 | } | |
456 | ||
9ef91a67 | 457 | static int qemu_paio_error(struct qemu_paiocb *aiocb) |
3c529d93 AL |
458 | { |
459 | ssize_t ret = qemu_paio_return(aiocb); | |
460 | ||
461 | if (ret < 0) | |
462 | ret = -ret; | |
463 | else | |
464 | ret = 0; | |
465 | ||
466 | return ret; | |
467 | } | |
468 | ||
59c7b155 | 469 | static int posix_aio_process_queue(void *opaque) |
3c529d93 | 470 | { |
9ef91a67 CH |
471 | PosixAioState *s = opaque; |
472 | struct qemu_paiocb *acb, **pacb; | |
3c529d93 | 473 | int ret; |
59c7b155 | 474 | int result = 0; |
9ef91a67 CH |
475 | |
476 | for(;;) { | |
477 | pacb = &s->first_aio; | |
478 | for(;;) { | |
479 | acb = *pacb; | |
480 | if (!acb) | |
59c7b155 | 481 | return result; |
e5f37649 | 482 | |
9ef91a67 CH |
483 | ret = qemu_paio_error(acb); |
484 | if (ret == ECANCELED) { | |
485 | /* remove the request */ | |
486 | *pacb = acb->next; | |
487 | qemu_aio_release(acb); | |
59c7b155 | 488 | result = 1; |
9ef91a67 CH |
489 | } else if (ret != EINPROGRESS) { |
490 | /* end of aio */ | |
491 | if (ret == 0) { | |
492 | ret = qemu_paio_return(acb); | |
493 | if (ret == acb->aio_nbytes) | |
494 | ret = 0; | |
495 | else | |
496 | ret = -EINVAL; | |
497 | } else { | |
498 | ret = -ret; | |
499 | } | |
ddca9fb2 SH |
500 | |
501 | trace_paio_complete(acb, acb->common.opaque, ret); | |
502 | ||
9ef91a67 CH |
503 | /* remove the request */ |
504 | *pacb = acb->next; | |
505 | /* call the callback */ | |
506 | acb->common.cb(acb->common.opaque, ret); | |
507 | qemu_aio_release(acb); | |
59c7b155 | 508 | result = 1; |
9ef91a67 CH |
509 | break; |
510 | } else { | |
511 | pacb = &acb->next; | |
512 | } | |
513 | } | |
514 | } | |
59c7b155 KW |
515 | |
516 | return result; | |
517 | } | |
518 | ||
519 | static void posix_aio_read(void *opaque) | |
520 | { | |
521 | PosixAioState *s = opaque; | |
522 | ssize_t len; | |
523 | ||
524 | /* read all bytes from signal pipe */ | |
525 | for (;;) { | |
526 | char bytes[16]; | |
527 | ||
528 | len = read(s->rfd, bytes, sizeof(bytes)); | |
529 | if (len == -1 && errno == EINTR) | |
530 | continue; /* try again */ | |
531 | if (len == sizeof(bytes)) | |
532 | continue; /* more to read */ | |
533 | break; | |
534 | } | |
535 | ||
536 | posix_aio_process_queue(s); | |
9ef91a67 CH |
537 | } |
538 | ||
539 | static int posix_aio_flush(void *opaque) | |
540 | { | |
541 | PosixAioState *s = opaque; | |
542 | return !!s->first_aio; | |
543 | } | |
544 | ||
545 | static PosixAioState *posix_aio_state; | |
546 | ||
e1d3b254 | 547 | static void posix_aio_notify_event(void) |
9ef91a67 | 548 | { |
e1d3b254 FZ |
549 | char byte = 0; |
550 | ssize_t ret; | |
9ef91a67 | 551 | |
e1d3b254 FZ |
552 | ret = write(posix_aio_state->wfd, &byte, sizeof(byte)); |
553 | if (ret < 0 && errno != EAGAIN) | |
554 | die("write()"); | |
9ef91a67 CH |
555 | } |
556 | ||
557 | static void paio_remove(struct qemu_paiocb *acb) | |
558 | { | |
559 | struct qemu_paiocb **pacb; | |
560 | ||
561 | /* remove the callback from the queue */ | |
562 | pacb = &posix_aio_state->first_aio; | |
563 | for(;;) { | |
564 | if (*pacb == NULL) { | |
565 | fprintf(stderr, "paio_remove: aio request not found!\n"); | |
566 | break; | |
567 | } else if (*pacb == acb) { | |
568 | *pacb = acb->next; | |
569 | qemu_aio_release(acb); | |
570 | break; | |
571 | } | |
572 | pacb = &(*pacb)->next; | |
573 | } | |
574 | } | |
575 | ||
576 | static void paio_cancel(BlockDriverAIOCB *blockacb) | |
577 | { | |
578 | struct qemu_paiocb *acb = (struct qemu_paiocb *)blockacb; | |
579 | int active = 0; | |
3c529d93 | 580 | |
ddca9fb2 SH |
581 | trace_paio_cancel(acb, acb->common.opaque); |
582 | ||
8653c015 | 583 | mutex_lock(&lock); |
9ef91a67 | 584 | if (!acb->active) { |
72cf2d4f | 585 | QTAILQ_REMOVE(&request_list, acb, node); |
9ef91a67 CH |
586 | acb->ret = -ECANCELED; |
587 | } else if (acb->ret == -EINPROGRESS) { | |
588 | active = 1; | |
589 | } | |
8653c015 | 590 | mutex_unlock(&lock); |
3c529d93 | 591 | |
9ef91a67 CH |
592 | if (active) { |
593 | /* fail safe: if the aio could not be canceled, we wait for | |
594 | it */ | |
595 | while (qemu_paio_error(acb) == EINPROGRESS) | |
596 | ; | |
597 | } | |
598 | ||
599 | paio_remove(acb); | |
600 | } | |
601 | ||
602 | static AIOPool raw_aio_pool = { | |
603 | .aiocb_size = sizeof(struct qemu_paiocb), | |
604 | .cancel = paio_cancel, | |
605 | }; | |
606 | ||
1e5b9d2f | 607 | BlockDriverAIOCB *paio_submit(BlockDriverState *bs, int fd, |
9ef91a67 CH |
608 | int64_t sector_num, QEMUIOVector *qiov, int nb_sectors, |
609 | BlockDriverCompletionFunc *cb, void *opaque, int type) | |
610 | { | |
611 | struct qemu_paiocb *acb; | |
612 | ||
613 | acb = qemu_aio_get(&raw_aio_pool, bs, cb, opaque); | |
614 | if (!acb) | |
615 | return NULL; | |
616 | acb->aio_type = type; | |
617 | acb->aio_fildes = fd; | |
e5f37649 | 618 | |
b2e12bc6 CH |
619 | if (qiov) { |
620 | acb->aio_iov = qiov->iov; | |
621 | acb->aio_niov = qiov->niov; | |
622 | } | |
9ef91a67 CH |
623 | acb->aio_nbytes = nb_sectors * 512; |
624 | acb->aio_offset = sector_num * 512; | |
625 | ||
626 | acb->next = posix_aio_state->first_aio; | |
627 | posix_aio_state->first_aio = acb; | |
628 | ||
6d519a5f | 629 | trace_paio_submit(acb, opaque, sector_num, nb_sectors, type); |
9ef91a67 CH |
630 | qemu_paio_submit(acb); |
631 | return &acb->common; | |
632 | } | |
633 | ||
634 | BlockDriverAIOCB *paio_ioctl(BlockDriverState *bs, int fd, | |
635 | unsigned long int req, void *buf, | |
636 | BlockDriverCompletionFunc *cb, void *opaque) | |
637 | { | |
638 | struct qemu_paiocb *acb; | |
639 | ||
640 | acb = qemu_aio_get(&raw_aio_pool, bs, cb, opaque); | |
641 | if (!acb) | |
642 | return NULL; | |
643 | acb->aio_type = QEMU_AIO_IOCTL; | |
644 | acb->aio_fildes = fd; | |
9ef91a67 CH |
645 | acb->aio_offset = 0; |
646 | acb->aio_ioctl_buf = buf; | |
647 | acb->aio_ioctl_cmd = req; | |
648 | ||
649 | acb->next = posix_aio_state->first_aio; | |
650 | posix_aio_state->first_aio = acb; | |
651 | ||
652 | qemu_paio_submit(acb); | |
653 | return &acb->common; | |
654 | } | |
655 | ||
1e5b9d2f | 656 | int paio_init(void) |
9ef91a67 | 657 | { |
9ef91a67 CH |
658 | PosixAioState *s; |
659 | int fds[2]; | |
660 | int ret; | |
661 | ||
662 | if (posix_aio_state) | |
1e5b9d2f | 663 | return 0; |
9ef91a67 | 664 | |
7267c094 | 665 | s = g_malloc(sizeof(PosixAioState)); |
9ef91a67 | 666 | |
9ef91a67 | 667 | s->first_aio = NULL; |
40ff6d7e | 668 | if (qemu_pipe(fds) == -1) { |
9ef91a67 | 669 | fprintf(stderr, "failed to create pipe\n"); |
1e5b9d2f | 670 | return -1; |
9ef91a67 CH |
671 | } |
672 | ||
673 | s->rfd = fds[0]; | |
674 | s->wfd = fds[1]; | |
675 | ||
676 | fcntl(s->rfd, F_SETFL, O_NONBLOCK); | |
677 | fcntl(s->wfd, F_SETFL, O_NONBLOCK); | |
678 | ||
8febfa26 KW |
679 | qemu_aio_set_fd_handler(s->rfd, posix_aio_read, NULL, posix_aio_flush, |
680 | posix_aio_process_queue, s); | |
9ef91a67 CH |
681 | |
682 | ret = pthread_attr_init(&attr); | |
683 | if (ret) | |
684 | die2(ret, "pthread_attr_init"); | |
685 | ||
686 | ret = pthread_attr_setdetachstate(&attr, PTHREAD_CREATE_DETACHED); | |
687 | if (ret) | |
688 | die2(ret, "pthread_attr_setdetachstate"); | |
689 | ||
72cf2d4f | 690 | QTAILQ_INIT(&request_list); |
e4ea78ee | 691 | new_thread_bh = qemu_bh_new(spawn_thread_bh_fn, NULL); |
9ef91a67 CH |
692 | |
693 | posix_aio_state = s; | |
1e5b9d2f | 694 | return 0; |
3c529d93 | 695 | } |