Statistics
| Branch: | Revision:

root / posix-aio-compat.c @ 212b6008

History | View | Annotate | Download (15.8 kB)

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