Statistics
| Branch: | Revision:

root / linux-aio.c @ 85e8dab1

History | View | Annotate | Download (5.5 kB)

1 5c6c3a6c Christoph Hellwig
/*
2 5c6c3a6c Christoph Hellwig
 * Linux native AIO support.
3 5c6c3a6c Christoph Hellwig
 *
4 5c6c3a6c Christoph Hellwig
 * Copyright (C) 2009 IBM, Corp.
5 5c6c3a6c Christoph Hellwig
 * Copyright (C) 2009 Red Hat, Inc.
6 5c6c3a6c Christoph Hellwig
 *
7 5c6c3a6c Christoph Hellwig
 * This work is licensed under the terms of the GNU GPL, version 2 or later.
8 5c6c3a6c Christoph Hellwig
 * See the COPYING file in the top-level directory.
9 5c6c3a6c Christoph Hellwig
 */
10 5c6c3a6c Christoph Hellwig
#include "qemu-common.h"
11 5c6c3a6c Christoph Hellwig
#include "qemu-aio.h"
12 5c6c3a6c Christoph Hellwig
#include "block/raw-posix-aio.h"
13 5c6c3a6c Christoph Hellwig
14 5c6c3a6c Christoph Hellwig
#include <sys/eventfd.h>
15 5c6c3a6c Christoph Hellwig
#include <libaio.h>
16 5c6c3a6c Christoph Hellwig
17 5c6c3a6c Christoph Hellwig
/*
18 5c6c3a6c Christoph Hellwig
 * Queue size (per-device).
19 5c6c3a6c Christoph Hellwig
 *
20 5c6c3a6c Christoph Hellwig
 * XXX: eventually we need to communicate this to the guest and/or make it
21 5c6c3a6c Christoph Hellwig
 *      tunable by the guest.  If we get more outstanding requests at a time
22 5c6c3a6c Christoph Hellwig
 *      than this we will get EAGAIN from io_submit which is communicated to
23 5c6c3a6c Christoph Hellwig
 *      the guest as an I/O error.
24 5c6c3a6c Christoph Hellwig
 */
25 5c6c3a6c Christoph Hellwig
#define MAX_EVENTS 128
26 5c6c3a6c Christoph Hellwig
27 5c6c3a6c Christoph Hellwig
struct qemu_laiocb {
28 5c6c3a6c Christoph Hellwig
    BlockDriverAIOCB common;
29 5c6c3a6c Christoph Hellwig
    struct qemu_laio_state *ctx;
30 5c6c3a6c Christoph Hellwig
    struct iocb iocb;
31 5c6c3a6c Christoph Hellwig
    ssize_t ret;
32 5c6c3a6c Christoph Hellwig
    size_t nbytes;
33 b161e2e4 Kevin Wolf
    QEMUIOVector *qiov;
34 b161e2e4 Kevin Wolf
    bool is_read;
35 db0ffc24 Kevin Wolf
    QLIST_ENTRY(qemu_laiocb) node;
36 5c6c3a6c Christoph Hellwig
};
37 5c6c3a6c Christoph Hellwig
38 5c6c3a6c Christoph Hellwig
struct qemu_laio_state {
39 5c6c3a6c Christoph Hellwig
    io_context_t ctx;
40 5c6c3a6c Christoph Hellwig
    int efd;
41 5c6c3a6c Christoph Hellwig
    int count;
42 5c6c3a6c Christoph Hellwig
};
43 5c6c3a6c Christoph Hellwig
44 5c6c3a6c Christoph Hellwig
static inline ssize_t io_event_ret(struct io_event *ev)
45 5c6c3a6c Christoph Hellwig
{
46 5c6c3a6c Christoph Hellwig
    return (ssize_t)(((uint64_t)ev->res2 << 32) | ev->res);
47 5c6c3a6c Christoph Hellwig
}
48 5c6c3a6c Christoph Hellwig
49 db0ffc24 Kevin Wolf
/*
50 db0ffc24 Kevin Wolf
 * Completes an AIO request (calls the callback and frees the ACB).
51 db0ffc24 Kevin Wolf
 */
52 db0ffc24 Kevin Wolf
static void qemu_laio_process_completion(struct qemu_laio_state *s,
53 db0ffc24 Kevin Wolf
    struct qemu_laiocb *laiocb)
54 db0ffc24 Kevin Wolf
{
55 db0ffc24 Kevin Wolf
    int ret;
56 db0ffc24 Kevin Wolf
57 db0ffc24 Kevin Wolf
    s->count--;
58 db0ffc24 Kevin Wolf
59 db0ffc24 Kevin Wolf
    ret = laiocb->ret;
60 db0ffc24 Kevin Wolf
    if (ret != -ECANCELED) {
61 b161e2e4 Kevin Wolf
        if (ret == laiocb->nbytes) {
62 db0ffc24 Kevin Wolf
            ret = 0;
63 b161e2e4 Kevin Wolf
        } else if (ret >= 0) {
64 b161e2e4 Kevin Wolf
            /* Short reads mean EOF, pad with zeros. */
65 b161e2e4 Kevin Wolf
            if (laiocb->is_read) {
66 b161e2e4 Kevin Wolf
                qemu_iovec_memset_skip(laiocb->qiov, 0,
67 b161e2e4 Kevin Wolf
                    laiocb->qiov->size - ret, ret);
68 b161e2e4 Kevin Wolf
            } else {
69 b161e2e4 Kevin Wolf
                ret = -EINVAL;
70 b161e2e4 Kevin Wolf
            }
71 b161e2e4 Kevin Wolf
        }
72 db0ffc24 Kevin Wolf
73 db0ffc24 Kevin Wolf
        laiocb->common.cb(laiocb->common.opaque, ret);
74 db0ffc24 Kevin Wolf
    }
75 db0ffc24 Kevin Wolf
76 db0ffc24 Kevin Wolf
    qemu_aio_release(laiocb);
77 db0ffc24 Kevin Wolf
}
78 db0ffc24 Kevin Wolf
79 5c6c3a6c Christoph Hellwig
static void qemu_laio_completion_cb(void *opaque)
80 5c6c3a6c Christoph Hellwig
{
81 5c6c3a6c Christoph Hellwig
    struct qemu_laio_state *s = opaque;
82 5c6c3a6c Christoph Hellwig
83 5c6c3a6c Christoph Hellwig
    while (1) {
84 5c6c3a6c Christoph Hellwig
        struct io_event events[MAX_EVENTS];
85 5c6c3a6c Christoph Hellwig
        uint64_t val;
86 5c6c3a6c Christoph Hellwig
        ssize_t ret;
87 5c6c3a6c Christoph Hellwig
        struct timespec ts = { 0 };
88 5c6c3a6c Christoph Hellwig
        int nevents, i;
89 5c6c3a6c Christoph Hellwig
90 5c6c3a6c Christoph Hellwig
        do {
91 5c6c3a6c Christoph Hellwig
            ret = read(s->efd, &val, sizeof(val));
92 2be50649 Stefan Hajnoczi
        } while (ret == -1 && errno == EINTR);
93 5c6c3a6c Christoph Hellwig
94 5c6c3a6c Christoph Hellwig
        if (ret == -1 && errno == EAGAIN)
95 5c6c3a6c Christoph Hellwig
            break;
96 5c6c3a6c Christoph Hellwig
97 5c6c3a6c Christoph Hellwig
        if (ret != 8)
98 5c6c3a6c Christoph Hellwig
            break;
99 5c6c3a6c Christoph Hellwig
100 5c6c3a6c Christoph Hellwig
        do {
101 5c6c3a6c Christoph Hellwig
            nevents = io_getevents(s->ctx, val, MAX_EVENTS, events, &ts);
102 5c6c3a6c Christoph Hellwig
        } while (nevents == -EINTR);
103 5c6c3a6c Christoph Hellwig
104 5c6c3a6c Christoph Hellwig
        for (i = 0; i < nevents; i++) {
105 5c6c3a6c Christoph Hellwig
            struct iocb *iocb = events[i].obj;
106 5c6c3a6c Christoph Hellwig
            struct qemu_laiocb *laiocb =
107 5c6c3a6c Christoph Hellwig
                    container_of(iocb, struct qemu_laiocb, iocb);
108 5c6c3a6c Christoph Hellwig
109 db0ffc24 Kevin Wolf
            laiocb->ret = io_event_ret(&events[i]);
110 384acbf4 Kevin Wolf
            qemu_laio_process_completion(s, laiocb);
111 5c6c3a6c Christoph Hellwig
        }
112 5c6c3a6c Christoph Hellwig
    }
113 5c6c3a6c Christoph Hellwig
}
114 5c6c3a6c Christoph Hellwig
115 5c6c3a6c Christoph Hellwig
static int qemu_laio_flush_cb(void *opaque)
116 5c6c3a6c Christoph Hellwig
{
117 5c6c3a6c Christoph Hellwig
    struct qemu_laio_state *s = opaque;
118 5c6c3a6c Christoph Hellwig
119 5c6c3a6c Christoph Hellwig
    return (s->count > 0) ? 1 : 0;
120 5c6c3a6c Christoph Hellwig
}
121 5c6c3a6c Christoph Hellwig
122 5c6c3a6c Christoph Hellwig
static void laio_cancel(BlockDriverAIOCB *blockacb)
123 5c6c3a6c Christoph Hellwig
{
124 5c6c3a6c Christoph Hellwig
    struct qemu_laiocb *laiocb = (struct qemu_laiocb *)blockacb;
125 5c6c3a6c Christoph Hellwig
    struct io_event event;
126 5c6c3a6c Christoph Hellwig
    int ret;
127 5c6c3a6c Christoph Hellwig
128 5c6c3a6c Christoph Hellwig
    if (laiocb->ret != -EINPROGRESS)
129 5c6c3a6c Christoph Hellwig
        return;
130 5c6c3a6c Christoph Hellwig
131 5c6c3a6c Christoph Hellwig
    /*
132 5c6c3a6c Christoph Hellwig
     * Note that as of Linux 2.6.31 neither the block device code nor any
133 5c6c3a6c Christoph Hellwig
     * filesystem implements cancellation of AIO request.
134 5c6c3a6c Christoph Hellwig
     * Thus the polling loop below is the normal code path.
135 5c6c3a6c Christoph Hellwig
     */
136 5c6c3a6c Christoph Hellwig
    ret = io_cancel(laiocb->ctx->ctx, &laiocb->iocb, &event);
137 5c6c3a6c Christoph Hellwig
    if (ret == 0) {
138 5c6c3a6c Christoph Hellwig
        laiocb->ret = -ECANCELED;
139 5c6c3a6c Christoph Hellwig
        return;
140 5c6c3a6c Christoph Hellwig
    }
141 5c6c3a6c Christoph Hellwig
142 5c6c3a6c Christoph Hellwig
    /*
143 5c6c3a6c Christoph Hellwig
     * We have to wait for the iocb to finish.
144 5c6c3a6c Christoph Hellwig
     *
145 5c6c3a6c Christoph Hellwig
     * The only way to get the iocb status update is by polling the io context.
146 5c6c3a6c Christoph Hellwig
     * We might be able to do this slightly more optimal by removing the
147 5c6c3a6c Christoph Hellwig
     * O_NONBLOCK flag.
148 5c6c3a6c Christoph Hellwig
     */
149 5c6c3a6c Christoph Hellwig
    while (laiocb->ret == -EINPROGRESS)
150 5c6c3a6c Christoph Hellwig
        qemu_laio_completion_cb(laiocb->ctx);
151 5c6c3a6c Christoph Hellwig
}
152 5c6c3a6c Christoph Hellwig
153 5c6c3a6c Christoph Hellwig
static AIOPool laio_pool = {
154 5c6c3a6c Christoph Hellwig
    .aiocb_size         = sizeof(struct qemu_laiocb),
155 5c6c3a6c Christoph Hellwig
    .cancel             = laio_cancel,
156 5c6c3a6c Christoph Hellwig
};
157 5c6c3a6c Christoph Hellwig
158 5c6c3a6c Christoph Hellwig
BlockDriverAIOCB *laio_submit(BlockDriverState *bs, void *aio_ctx, int fd,
159 5c6c3a6c Christoph Hellwig
        int64_t sector_num, QEMUIOVector *qiov, int nb_sectors,
160 5c6c3a6c Christoph Hellwig
        BlockDriverCompletionFunc *cb, void *opaque, int type)
161 5c6c3a6c Christoph Hellwig
{
162 5c6c3a6c Christoph Hellwig
    struct qemu_laio_state *s = aio_ctx;
163 5c6c3a6c Christoph Hellwig
    struct qemu_laiocb *laiocb;
164 5c6c3a6c Christoph Hellwig
    struct iocb *iocbs;
165 5c6c3a6c Christoph Hellwig
    off_t offset = sector_num * 512;
166 5c6c3a6c Christoph Hellwig
167 5c6c3a6c Christoph Hellwig
    laiocb = qemu_aio_get(&laio_pool, bs, cb, opaque);
168 5c6c3a6c Christoph Hellwig
    laiocb->nbytes = nb_sectors * 512;
169 5c6c3a6c Christoph Hellwig
    laiocb->ctx = s;
170 5c6c3a6c Christoph Hellwig
    laiocb->ret = -EINPROGRESS;
171 b161e2e4 Kevin Wolf
    laiocb->is_read = (type == QEMU_AIO_READ);
172 b161e2e4 Kevin Wolf
    laiocb->qiov = qiov;
173 5c6c3a6c Christoph Hellwig
174 5c6c3a6c Christoph Hellwig
    iocbs = &laiocb->iocb;
175 5c6c3a6c Christoph Hellwig
176 5c6c3a6c Christoph Hellwig
    switch (type) {
177 5c6c3a6c Christoph Hellwig
    case QEMU_AIO_WRITE:
178 5c6c3a6c Christoph Hellwig
        io_prep_pwritev(iocbs, fd, qiov->iov, qiov->niov, offset);
179 5c6c3a6c Christoph Hellwig
        break;
180 5c6c3a6c Christoph Hellwig
    case QEMU_AIO_READ:
181 5c6c3a6c Christoph Hellwig
        io_prep_preadv(iocbs, fd, qiov->iov, qiov->niov, offset);
182 5c6c3a6c Christoph Hellwig
        break;
183 c30e624d Frediano Ziglio
    /* Currently Linux kernel does not support other operations */
184 5c6c3a6c Christoph Hellwig
    default:
185 5c6c3a6c Christoph Hellwig
        fprintf(stderr, "%s: invalid AIO request type 0x%x.\n",
186 5c6c3a6c Christoph Hellwig
                        __func__, type);
187 5c6c3a6c Christoph Hellwig
        goto out_free_aiocb;
188 5c6c3a6c Christoph Hellwig
    }
189 5c6c3a6c Christoph Hellwig
    io_set_eventfd(&laiocb->iocb, s->efd);
190 5c6c3a6c Christoph Hellwig
    s->count++;
191 5c6c3a6c Christoph Hellwig
192 5c6c3a6c Christoph Hellwig
    if (io_submit(s->ctx, 1, &iocbs) < 0)
193 5c6c3a6c Christoph Hellwig
        goto out_dec_count;
194 5c6c3a6c Christoph Hellwig
    return &laiocb->common;
195 5c6c3a6c Christoph Hellwig
196 5c6c3a6c Christoph Hellwig
out_dec_count:
197 5c6c3a6c Christoph Hellwig
    s->count--;
198 449c184e Kevin Wolf
out_free_aiocb:
199 449c184e Kevin Wolf
    qemu_aio_release(laiocb);
200 5c6c3a6c Christoph Hellwig
    return NULL;
201 5c6c3a6c Christoph Hellwig
}
202 5c6c3a6c Christoph Hellwig
203 5c6c3a6c Christoph Hellwig
void *laio_init(void)
204 5c6c3a6c Christoph Hellwig
{
205 5c6c3a6c Christoph Hellwig
    struct qemu_laio_state *s;
206 5c6c3a6c Christoph Hellwig
207 7267c094 Anthony Liguori
    s = g_malloc0(sizeof(*s));
208 5c6c3a6c Christoph Hellwig
    s->efd = eventfd(0, 0);
209 5c6c3a6c Christoph Hellwig
    if (s->efd == -1)
210 5c6c3a6c Christoph Hellwig
        goto out_free_state;
211 5c6c3a6c Christoph Hellwig
    fcntl(s->efd, F_SETFL, O_NONBLOCK);
212 5c6c3a6c Christoph Hellwig
213 5c6c3a6c Christoph Hellwig
    if (io_setup(MAX_EVENTS, &s->ctx) != 0)
214 5c6c3a6c Christoph Hellwig
        goto out_close_efd;
215 5c6c3a6c Christoph Hellwig
216 db0ffc24 Kevin Wolf
    qemu_aio_set_fd_handler(s->efd, qemu_laio_completion_cb, NULL,
217 cf26a4e6 Paolo Bonzini
        qemu_laio_flush_cb, NULL, s);
218 5c6c3a6c Christoph Hellwig
219 5c6c3a6c Christoph Hellwig
    return s;
220 5c6c3a6c Christoph Hellwig
221 5c6c3a6c Christoph Hellwig
out_close_efd:
222 5c6c3a6c Christoph Hellwig
    close(s->efd);
223 5c6c3a6c Christoph Hellwig
out_free_state:
224 7267c094 Anthony Liguori
    g_free(s);
225 5c6c3a6c Christoph Hellwig
    return NULL;
226 5c6c3a6c Christoph Hellwig
}