blob: 88b0520a8b73f5f1317fb5dd72995b203f95648a [file] [log] [blame]
Christoph Hellwig5c6c3a62009-08-20 16:58:35 +02001/*
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"
Paolo Bonzini737e1502012-12-17 18:19:44 +010011#include "block/aio.h"
Paolo Bonzini1de7afc2012-12-17 18:20:00 +010012#include "qemu/queue.h"
Paolo Bonzini9f8540e2012-06-09 10:57:37 +020013#include "block/raw-aio.h"
Paolo Bonzini1de7afc2012-12-17 18:20:00 +010014#include "qemu/event_notifier.h"
Christoph Hellwig5c6c3a62009-08-20 16:58:35 +020015
Christoph Hellwig5c6c3a62009-08-20 16:58:35 +020016#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
Ming Lei1b3abdc2014-07-04 18:04:34 +080028#define MAX_QUEUED_IO 128
29
Christoph Hellwig5c6c3a62009-08-20 16:58:35 +020030struct qemu_laiocb {
Markus Armbruster7c84b1b2014-10-07 13:59:14 +020031 BlockAIOCB common;
Christoph Hellwig5c6c3a62009-08-20 16:58:35 +020032 struct qemu_laio_state *ctx;
33 struct iocb iocb;
34 ssize_t ret;
35 size_t nbytes;
Kevin Wolfb161e2e2011-10-13 15:42:52 +020036 QEMUIOVector *qiov;
37 bool is_read;
Paolo Bonzini28b24082014-12-11 14:52:26 +010038 QSIMPLEQ_ENTRY(qemu_laiocb) next;
Christoph Hellwig5c6c3a62009-08-20 16:58:35 +020039};
40
Ming Lei1b3abdc2014-07-04 18:04:34 +080041typedef struct {
Ming Lei1b3abdc2014-07-04 18:04:34 +080042 int plugged;
Paolo Bonzini8455ce02014-12-11 14:52:28 +010043 unsigned int n;
Paolo Bonzini43f23762014-12-11 14:52:27 +010044 bool blocked;
Paolo Bonzini28b24082014-12-11 14:52:26 +010045 QSIMPLEQ_HEAD(, qemu_laiocb) pending;
Ming Lei1b3abdc2014-07-04 18:04:34 +080046} LaioQueue;
47
Christoph Hellwig5c6c3a62009-08-20 16:58:35 +020048struct qemu_laio_state {
49 io_context_t ctx;
Paolo Bonzinic90caf22012-02-24 08:39:02 +010050 EventNotifier e;
Ming Lei1b3abdc2014-07-04 18:04:34 +080051
52 /* io queue for submit at batch */
53 LaioQueue io_q;
Stefan Hajnoczi2cdff7f2014-08-04 16:56:33 +010054
55 /* I/O completion processing */
56 QEMUBH *completion_bh;
57 struct io_event events[MAX_EVENTS];
58 int event_idx;
59 int event_max;
Christoph Hellwig5c6c3a62009-08-20 16:58:35 +020060};
61
Paolo Bonzinide354642014-12-11 14:52:29 +010062static void ioq_submit(struct qemu_laio_state *s);
Paolo Bonzini28b24082014-12-11 14:52:26 +010063
Christoph Hellwig5c6c3a62009-08-20 16:58:35 +020064static inline ssize_t io_event_ret(struct io_event *ev)
65{
66 return (ssize_t)(((uint64_t)ev->res2 << 32) | ev->res);
67}
68
Kevin Wolfdb0ffc22009-10-22 17:54:41 +020069/*
70 * Completes an AIO request (calls the callback and frees the ACB).
Kevin Wolfdb0ffc22009-10-22 17:54:41 +020071 */
72static void qemu_laio_process_completion(struct qemu_laio_state *s,
73 struct qemu_laiocb *laiocb)
74{
75 int ret;
76
Kevin Wolfdb0ffc22009-10-22 17:54:41 +020077 ret = laiocb->ret;
78 if (ret != -ECANCELED) {
Kevin Wolfb161e2e2011-10-13 15:42:52 +020079 if (ret == laiocb->nbytes) {
Kevin Wolfdb0ffc22009-10-22 17:54:41 +020080 ret = 0;
Kevin Wolfb161e2e2011-10-13 15:42:52 +020081 } else if (ret >= 0) {
82 /* Short reads mean EOF, pad with zeros. */
83 if (laiocb->is_read) {
Michael Tokarev3d9b4922012-03-10 16:54:23 +040084 qemu_iovec_memset(laiocb->qiov, ret, 0,
85 laiocb->qiov->size - ret);
Kevin Wolfb161e2e2011-10-13 15:42:52 +020086 } else {
87 ret = -EINVAL;
88 }
89 }
Kevin Wolfdb0ffc22009-10-22 17:54:41 +020090 }
Fam Zheng771b64d2014-09-11 13:41:13 +080091 laiocb->common.cb(laiocb->common.opaque, ret);
Kevin Wolfdb0ffc22009-10-22 17:54:41 +020092
Fam Zheng80074292014-09-11 13:41:28 +080093 qemu_aio_unref(laiocb);
Kevin Wolfdb0ffc22009-10-22 17:54:41 +020094}
95
Stefan Hajnoczi2cdff7f2014-08-04 16:56:33 +010096/* The completion BH fetches completed I/O requests and invokes their
97 * callbacks.
98 *
99 * The function is somewhat tricky because it supports nested event loops, for
100 * example when a request callback invokes aio_poll(). In order to do this,
101 * the completion events array and index are kept in qemu_laio_state. The BH
102 * reschedules itself as long as there are completions pending so it will
103 * either be called again in a nested event loop or will be called after all
104 * events have been completed. When there are no events left to complete, the
105 * BH returns without rescheduling.
106 */
107static void qemu_laio_completion_bh(void *opaque)
108{
109 struct qemu_laio_state *s = opaque;
110
111 /* Fetch more completion events when empty */
112 if (s->event_idx == s->event_max) {
113 do {
114 struct timespec ts = { 0 };
115 s->event_max = io_getevents(s->ctx, MAX_EVENTS, MAX_EVENTS,
116 s->events, &ts);
117 } while (s->event_max == -EINTR);
118
119 s->event_idx = 0;
120 if (s->event_max <= 0) {
121 s->event_max = 0;
122 return; /* no more events */
123 }
124 }
125
126 /* Reschedule so nested event loops see currently pending completions */
127 qemu_bh_schedule(s->completion_bh);
128
129 /* Process completion events */
130 while (s->event_idx < s->event_max) {
131 struct iocb *iocb = s->events[s->event_idx].obj;
132 struct qemu_laiocb *laiocb =
133 container_of(iocb, struct qemu_laiocb, iocb);
134
135 laiocb->ret = io_event_ret(&s->events[s->event_idx]);
136 s->event_idx++;
137
138 qemu_laio_process_completion(s, laiocb);
139 }
Paolo Bonzini28b24082014-12-11 14:52:26 +0100140
141 if (!s->io_q.plugged && !QSIMPLEQ_EMPTY(&s->io_q.pending)) {
142 ioq_submit(s);
143 }
Stefan Hajnoczi2cdff7f2014-08-04 16:56:33 +0100144}
145
Paolo Bonzinic90caf22012-02-24 08:39:02 +0100146static void qemu_laio_completion_cb(EventNotifier *e)
Christoph Hellwig5c6c3a62009-08-20 16:58:35 +0200147{
Paolo Bonzinic90caf22012-02-24 08:39:02 +0100148 struct qemu_laio_state *s = container_of(e, struct qemu_laio_state, e);
Christoph Hellwig5c6c3a62009-08-20 16:58:35 +0200149
Stefan Hajnoczi2cdff7f2014-08-04 16:56:33 +0100150 if (event_notifier_test_and_clear(&s->e)) {
151 qemu_bh_schedule(s->completion_bh);
Christoph Hellwig5c6c3a62009-08-20 16:58:35 +0200152 }
153}
154
Markus Armbruster7c84b1b2014-10-07 13:59:14 +0200155static void laio_cancel(BlockAIOCB *blockacb)
Christoph Hellwig5c6c3a62009-08-20 16:58:35 +0200156{
157 struct qemu_laiocb *laiocb = (struct qemu_laiocb *)blockacb;
158 struct io_event event;
159 int ret;
160
Fam Zheng771b64d2014-09-11 13:41:13 +0800161 if (laiocb->ret != -EINPROGRESS) {
Christoph Hellwig5c6c3a62009-08-20 16:58:35 +0200162 return;
Fam Zheng771b64d2014-09-11 13:41:13 +0800163 }
Christoph Hellwig5c6c3a62009-08-20 16:58:35 +0200164 ret = io_cancel(laiocb->ctx->ctx, &laiocb->iocb, &event);
Fam Zheng771b64d2014-09-11 13:41:13 +0800165 laiocb->ret = -ECANCELED;
166 if (ret != 0) {
167 /* iocb is not cancelled, cb will be called by the event loop later */
Christoph Hellwig5c6c3a62009-08-20 16:58:35 +0200168 return;
169 }
170
Fam Zheng771b64d2014-09-11 13:41:13 +0800171 laiocb->common.cb(laiocb->common.opaque, laiocb->ret);
Christoph Hellwig5c6c3a62009-08-20 16:58:35 +0200172}
173
Stefan Hajnoczid7331be2012-10-31 16:34:37 +0100174static const AIOCBInfo laio_aiocb_info = {
Christoph Hellwig5c6c3a62009-08-20 16:58:35 +0200175 .aiocb_size = sizeof(struct qemu_laiocb),
Fam Zheng771b64d2014-09-11 13:41:13 +0800176 .cancel_async = laio_cancel,
Christoph Hellwig5c6c3a62009-08-20 16:58:35 +0200177};
178
Ming Lei1b3abdc2014-07-04 18:04:34 +0800179static void ioq_init(LaioQueue *io_q)
180{
Paolo Bonzini28b24082014-12-11 14:52:26 +0100181 QSIMPLEQ_INIT(&io_q->pending);
Ming Lei1b3abdc2014-07-04 18:04:34 +0800182 io_q->plugged = 0;
Paolo Bonzini8455ce02014-12-11 14:52:28 +0100183 io_q->n = 0;
Paolo Bonzini43f23762014-12-11 14:52:27 +0100184 io_q->blocked = false;
Ming Lei1b3abdc2014-07-04 18:04:34 +0800185}
186
Paolo Bonzinide354642014-12-11 14:52:29 +0100187static void ioq_submit(struct qemu_laio_state *s)
Ming Lei1b3abdc2014-07-04 18:04:34 +0800188{
Paolo Bonzini82595da2014-12-11 14:52:30 +0100189 int ret, len;
Paolo Bonzini28b24082014-12-11 14:52:26 +0100190 struct qemu_laiocb *aiocb;
191 struct iocb *iocbs[MAX_QUEUED_IO];
Paolo Bonzini82595da2014-12-11 14:52:30 +0100192 QSIMPLEQ_HEAD(, qemu_laiocb) completed;
Ming Lei1b3abdc2014-07-04 18:04:34 +0800193
Paolo Bonzini43f23762014-12-11 14:52:27 +0100194 do {
195 len = 0;
196 QSIMPLEQ_FOREACH(aiocb, &s->io_q.pending, next) {
197 iocbs[len++] = &aiocb->iocb;
198 if (len == MAX_QUEUED_IO) {
199 break;
200 }
Paolo Bonzini28b24082014-12-11 14:52:26 +0100201 }
Ming Lei1b3abdc2014-07-04 18:04:34 +0800202
Paolo Bonzini43f23762014-12-11 14:52:27 +0100203 ret = io_submit(s->ctx, len, iocbs);
204 if (ret == -EAGAIN) {
Paolo Bonzini82595da2014-12-11 14:52:30 +0100205 break;
Paolo Bonzini43f23762014-12-11 14:52:27 +0100206 }
207 if (ret < 0) {
208 abort();
209 }
Ming Lei1b3abdc2014-07-04 18:04:34 +0800210
Paolo Bonzini82595da2014-12-11 14:52:30 +0100211 s->io_q.n -= ret;
212 aiocb = container_of(iocbs[ret - 1], struct qemu_laiocb, iocb);
213 QSIMPLEQ_SPLIT_AFTER(&s->io_q.pending, aiocb, next, &completed);
Paolo Bonzini43f23762014-12-11 14:52:27 +0100214 } while (ret == len && !QSIMPLEQ_EMPTY(&s->io_q.pending));
Paolo Bonzini8455ce02014-12-11 14:52:28 +0100215 s->io_q.blocked = (s->io_q.n > 0);
Ming Lei1b3abdc2014-07-04 18:04:34 +0800216}
217
Ming Lei1b3abdc2014-07-04 18:04:34 +0800218void laio_io_plug(BlockDriverState *bs, void *aio_ctx)
219{
220 struct qemu_laio_state *s = aio_ctx;
221
222 s->io_q.plugged++;
223}
224
Paolo Bonzinide354642014-12-11 14:52:29 +0100225void laio_io_unplug(BlockDriverState *bs, void *aio_ctx, bool unplug)
Ming Lei1b3abdc2014-07-04 18:04:34 +0800226{
227 struct qemu_laio_state *s = aio_ctx;
Ming Lei1b3abdc2014-07-04 18:04:34 +0800228
229 assert(s->io_q.plugged > 0 || !unplug);
230
231 if (unplug && --s->io_q.plugged > 0) {
Paolo Bonzinide354642014-12-11 14:52:29 +0100232 return;
Ming Lei1b3abdc2014-07-04 18:04:34 +0800233 }
234
Paolo Bonzini43f23762014-12-11 14:52:27 +0100235 if (!s->io_q.blocked && !QSIMPLEQ_EMPTY(&s->io_q.pending)) {
Paolo Bonzinide354642014-12-11 14:52:29 +0100236 ioq_submit(s);
Ming Lei1b3abdc2014-07-04 18:04:34 +0800237 }
Ming Lei1b3abdc2014-07-04 18:04:34 +0800238}
239
Markus Armbruster7c84b1b2014-10-07 13:59:14 +0200240BlockAIOCB *laio_submit(BlockDriverState *bs, void *aio_ctx, int fd,
Christoph Hellwig5c6c3a62009-08-20 16:58:35 +0200241 int64_t sector_num, QEMUIOVector *qiov, int nb_sectors,
Markus Armbruster097310b2014-10-07 13:59:15 +0200242 BlockCompletionFunc *cb, void *opaque, int type)
Christoph Hellwig5c6c3a62009-08-20 16:58:35 +0200243{
244 struct qemu_laio_state *s = aio_ctx;
245 struct qemu_laiocb *laiocb;
246 struct iocb *iocbs;
247 off_t offset = sector_num * 512;
248
Stefan Hajnoczid7331be2012-10-31 16:34:37 +0100249 laiocb = qemu_aio_get(&laio_aiocb_info, bs, cb, opaque);
Christoph Hellwig5c6c3a62009-08-20 16:58:35 +0200250 laiocb->nbytes = nb_sectors * 512;
251 laiocb->ctx = s;
252 laiocb->ret = -EINPROGRESS;
Kevin Wolfb161e2e2011-10-13 15:42:52 +0200253 laiocb->is_read = (type == QEMU_AIO_READ);
254 laiocb->qiov = qiov;
Christoph Hellwig5c6c3a62009-08-20 16:58:35 +0200255
256 iocbs = &laiocb->iocb;
257
258 switch (type) {
259 case QEMU_AIO_WRITE:
260 io_prep_pwritev(iocbs, fd, qiov->iov, qiov->niov, offset);
261 break;
262 case QEMU_AIO_READ:
263 io_prep_preadv(iocbs, fd, qiov->iov, qiov->niov, offset);
264 break;
Frediano Ziglioc30e6242011-08-30 09:46:11 +0200265 /* Currently Linux kernel does not support other operations */
Christoph Hellwig5c6c3a62009-08-20 16:58:35 +0200266 default:
267 fprintf(stderr, "%s: invalid AIO request type 0x%x.\n",
268 __func__, type);
269 goto out_free_aiocb;
270 }
Paolo Bonzinic90caf22012-02-24 08:39:02 +0100271 io_set_eventfd(&laiocb->iocb, event_notifier_get_fd(&s->e));
Christoph Hellwig5c6c3a62009-08-20 16:58:35 +0200272
Paolo Bonzini28b24082014-12-11 14:52:26 +0100273 QSIMPLEQ_INSERT_TAIL(&s->io_q.pending, laiocb, next);
Paolo Bonzini8455ce02014-12-11 14:52:28 +0100274 s->io_q.n++;
Paolo Bonzini43f23762014-12-11 14:52:27 +0100275 if (!s->io_q.blocked &&
Paolo Bonzini8455ce02014-12-11 14:52:28 +0100276 (!s->io_q.plugged || s->io_q.n >= MAX_QUEUED_IO)) {
Paolo Bonzini28b24082014-12-11 14:52:26 +0100277 ioq_submit(s);
Ming Lei1b3abdc2014-07-04 18:04:34 +0800278 }
Christoph Hellwig5c6c3a62009-08-20 16:58:35 +0200279 return &laiocb->common;
280
Kevin Wolf449c1842011-09-22 14:21:30 +0200281out_free_aiocb:
Fam Zheng80074292014-09-11 13:41:28 +0800282 qemu_aio_unref(laiocb);
Christoph Hellwig5c6c3a62009-08-20 16:58:35 +0200283 return NULL;
284}
285
Stefan Hajnoczic2f34262014-05-08 16:34:47 +0200286void laio_detach_aio_context(void *s_, AioContext *old_context)
287{
288 struct qemu_laio_state *s = s_;
289
Fam Zhengdca21ef2015-10-23 11:08:05 +0800290 aio_set_event_notifier(old_context, &s->e, false, NULL);
Stefan Hajnoczi2cdff7f2014-08-04 16:56:33 +0100291 qemu_bh_delete(s->completion_bh);
Stefan Hajnoczic2f34262014-05-08 16:34:47 +0200292}
293
294void laio_attach_aio_context(void *s_, AioContext *new_context)
295{
296 struct qemu_laio_state *s = s_;
297
Stefan Hajnoczi2cdff7f2014-08-04 16:56:33 +0100298 s->completion_bh = aio_bh_new(new_context, qemu_laio_completion_bh, s);
Fam Zhengdca21ef2015-10-23 11:08:05 +0800299 aio_set_event_notifier(new_context, &s->e, false,
300 qemu_laio_completion_cb);
Stefan Hajnoczic2f34262014-05-08 16:34:47 +0200301}
302
Christoph Hellwig5c6c3a62009-08-20 16:58:35 +0200303void *laio_init(void)
304{
305 struct qemu_laio_state *s;
306
Anthony Liguori7267c092011-08-20 22:09:37 -0500307 s = g_malloc0(sizeof(*s));
Paolo Bonzinic90caf22012-02-24 08:39:02 +0100308 if (event_notifier_init(&s->e, false) < 0) {
Christoph Hellwig5c6c3a62009-08-20 16:58:35 +0200309 goto out_free_state;
Paolo Bonzinic90caf22012-02-24 08:39:02 +0100310 }
Christoph Hellwig5c6c3a62009-08-20 16:58:35 +0200311
Paolo Bonzinic90caf22012-02-24 08:39:02 +0100312 if (io_setup(MAX_EVENTS, &s->ctx) != 0) {
Christoph Hellwig5c6c3a62009-08-20 16:58:35 +0200313 goto out_close_efd;
Paolo Bonzinic90caf22012-02-24 08:39:02 +0100314 }
Christoph Hellwig5c6c3a62009-08-20 16:58:35 +0200315
Ming Lei1b3abdc2014-07-04 18:04:34 +0800316 ioq_init(&s->io_q);
317
Christoph Hellwig5c6c3a62009-08-20 16:58:35 +0200318 return s;
319
320out_close_efd:
Paolo Bonzinic90caf22012-02-24 08:39:02 +0100321 event_notifier_cleanup(&s->e);
Christoph Hellwig5c6c3a62009-08-20 16:58:35 +0200322out_free_state:
Anthony Liguori7267c092011-08-20 22:09:37 -0500323 g_free(s);
Christoph Hellwig5c6c3a62009-08-20 16:58:35 +0200324 return NULL;
325}
Stefan Hajnocziabd269b2014-05-08 16:34:48 +0200326
327void laio_cleanup(void *s_)
328{
329 struct qemu_laio_state *s = s_;
330
331 event_notifier_cleanup(&s->e);
Gongleia1abf402014-07-12 11:43:37 +0800332
333 if (io_destroy(s->ctx) != 0) {
334 fprintf(stderr, "%s: destroy AIO context %p failed\n",
335 __func__, &s->ctx);
336 }
Stefan Hajnocziabd269b2014-05-08 16:34:48 +0200337 g_free(s);
338}