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