Statistics
| Branch: | Revision:

root / block / linux-aio.c @ 7371d56f

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