2 * Linux native AIO support.
4 * Copyright (C) 2009 IBM, Corp.
5 * Copyright (C) 2009 Red Hat, Inc.
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.
10 #include "qemu-common.h"
11 #include "block/aio.h"
12 #include "qemu/queue.h"
13 #include "block/raw-aio.h"
14 #include "qemu/event_notifier.h"
19 * Queue size (per-device).
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.
26 #define MAX_EVENTS 128
29 BlockDriverAIOCB common
;
30 struct qemu_laio_state
*ctx
;
36 QLIST_ENTRY(qemu_laiocb
) node
;
39 struct qemu_laio_state
{
44 static inline ssize_t
io_event_ret(struct io_event
*ev
)
46 return (ssize_t
)(((uint64_t)ev
->res2
<< 32) | ev
->res
);
50 * Completes an AIO request (calls the callback and frees the ACB).
52 static void qemu_laio_process_completion(struct qemu_laio_state
*s
,
53 struct qemu_laiocb
*laiocb
)
58 if (ret
!= -ECANCELED
) {
59 if (ret
== laiocb
->nbytes
) {
61 } else if (ret
>= 0) {
62 /* Short reads mean EOF, pad with zeros. */
63 if (laiocb
->is_read
) {
64 qemu_iovec_memset(laiocb
->qiov
, ret
, 0,
65 laiocb
->qiov
->size
- ret
);
71 laiocb
->common
.cb(laiocb
->common
.opaque
, ret
);
74 qemu_aio_release(laiocb
);
77 static void qemu_laio_completion_cb(EventNotifier
*e
)
79 struct qemu_laio_state
*s
= container_of(e
, struct qemu_laio_state
, e
);
81 while (event_notifier_test_and_clear(&s
->e
)) {
82 struct io_event events
[MAX_EVENTS
];
83 struct timespec ts
= { 0 };
87 nevents
= io_getevents(s
->ctx
, MAX_EVENTS
, MAX_EVENTS
, events
, &ts
);
88 } while (nevents
== -EINTR
);
90 for (i
= 0; i
< nevents
; i
++) {
91 struct iocb
*iocb
= events
[i
].obj
;
92 struct qemu_laiocb
*laiocb
=
93 container_of(iocb
, struct qemu_laiocb
, iocb
);
95 laiocb
->ret
= io_event_ret(&events
[i
]);
96 qemu_laio_process_completion(s
, laiocb
);
101 static void laio_cancel(BlockDriverAIOCB
*blockacb
)
103 struct qemu_laiocb
*laiocb
= (struct qemu_laiocb
*)blockacb
;
104 struct io_event event
;
107 if (laiocb
->ret
!= -EINPROGRESS
)
111 * Note that as of Linux 2.6.31 neither the block device code nor any
112 * filesystem implements cancellation of AIO request.
113 * Thus the polling loop below is the normal code path.
115 ret
= io_cancel(laiocb
->ctx
->ctx
, &laiocb
->iocb
, &event
);
117 laiocb
->ret
= -ECANCELED
;
122 * We have to wait for the iocb to finish.
124 * The only way to get the iocb status update is by polling the io context.
125 * We might be able to do this slightly more optimal by removing the
128 while (laiocb
->ret
== -EINPROGRESS
) {
129 qemu_laio_completion_cb(&laiocb
->ctx
->e
);
133 static const AIOCBInfo laio_aiocb_info
= {
134 .aiocb_size
= sizeof(struct qemu_laiocb
),
135 .cancel
= laio_cancel
,
138 BlockDriverAIOCB
*laio_submit(BlockDriverState
*bs
, void *aio_ctx
, int fd
,
139 int64_t sector_num
, QEMUIOVector
*qiov
, int nb_sectors
,
140 BlockDriverCompletionFunc
*cb
, void *opaque
, int type
)
142 struct qemu_laio_state
*s
= aio_ctx
;
143 struct qemu_laiocb
*laiocb
;
145 off_t offset
= sector_num
* 512;
147 laiocb
= qemu_aio_get(&laio_aiocb_info
, bs
, cb
, opaque
);
148 laiocb
->nbytes
= nb_sectors
* 512;
150 laiocb
->ret
= -EINPROGRESS
;
151 laiocb
->is_read
= (type
== QEMU_AIO_READ
);
154 iocbs
= &laiocb
->iocb
;
158 io_prep_pwritev(iocbs
, fd
, qiov
->iov
, qiov
->niov
, offset
);
161 io_prep_preadv(iocbs
, fd
, qiov
->iov
, qiov
->niov
, offset
);
163 /* Currently Linux kernel does not support other operations */
165 fprintf(stderr
, "%s: invalid AIO request type 0x%x.\n",
169 io_set_eventfd(&laiocb
->iocb
, event_notifier_get_fd(&s
->e
));
171 if (io_submit(s
->ctx
, 1, &iocbs
) < 0)
173 return &laiocb
->common
;
176 qemu_aio_release(laiocb
);
180 void laio_detach_aio_context(void *s_
, AioContext
*old_context
)
182 struct qemu_laio_state
*s
= s_
;
184 aio_set_event_notifier(old_context
, &s
->e
, NULL
);
187 void laio_attach_aio_context(void *s_
, AioContext
*new_context
)
189 struct qemu_laio_state
*s
= s_
;
191 aio_set_event_notifier(new_context
, &s
->e
, qemu_laio_completion_cb
);
194 void *laio_init(void)
196 struct qemu_laio_state
*s
;
198 s
= g_malloc0(sizeof(*s
));
199 if (event_notifier_init(&s
->e
, false) < 0) {
203 if (io_setup(MAX_EVENTS
, &s
->ctx
) != 0) {
210 event_notifier_cleanup(&s
->e
);
216 void laio_cleanup(void *s_
)
218 struct qemu_laio_state
*s
= s_
;
220 event_notifier_cleanup(&s
->e
);