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"
12 #include "qemu-queue.h"
13 #include "block/raw-aio.h"
14 #include "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
{
45 static inline ssize_t
io_event_ret(struct io_event
*ev
)
47 return (ssize_t
)(((uint64_t)ev
->res2
<< 32) | ev
->res
);
51 * Completes an AIO request (calls the callback and frees the ACB).
53 static void qemu_laio_process_completion(struct qemu_laio_state
*s
,
54 struct qemu_laiocb
*laiocb
)
61 if (ret
!= -ECANCELED
) {
62 if (ret
== laiocb
->nbytes
) {
64 } else if (ret
>= 0) {
65 /* Short reads mean EOF, pad with zeros. */
66 if (laiocb
->is_read
) {
67 qemu_iovec_memset(laiocb
->qiov
, ret
, 0,
68 laiocb
->qiov
->size
- ret
);
74 laiocb
->common
.cb(laiocb
->common
.opaque
, ret
);
77 qemu_aio_release(laiocb
);
80 static void qemu_laio_completion_cb(EventNotifier
*e
)
82 struct qemu_laio_state
*s
= container_of(e
, struct qemu_laio_state
, e
);
84 while (event_notifier_test_and_clear(&s
->e
)) {
85 struct io_event events
[MAX_EVENTS
];
86 struct timespec ts
= { 0 };
90 nevents
= io_getevents(s
->ctx
, MAX_EVENTS
, MAX_EVENTS
, events
, &ts
);
91 } while (nevents
== -EINTR
);
93 for (i
= 0; i
< nevents
; i
++) {
94 struct iocb
*iocb
= events
[i
].obj
;
95 struct qemu_laiocb
*laiocb
=
96 container_of(iocb
, struct qemu_laiocb
, iocb
);
98 laiocb
->ret
= io_event_ret(&events
[i
]);
99 qemu_laio_process_completion(s
, laiocb
);
104 static int qemu_laio_flush_cb(EventNotifier
*e
)
106 struct qemu_laio_state
*s
= container_of(e
, struct qemu_laio_state
, e
);
108 return (s
->count
> 0) ? 1 : 0;
111 static void laio_cancel(BlockDriverAIOCB
*blockacb
)
113 struct qemu_laiocb
*laiocb
= (struct qemu_laiocb
*)blockacb
;
114 struct io_event event
;
117 if (laiocb
->ret
!= -EINPROGRESS
)
121 * Note that as of Linux 2.6.31 neither the block device code nor any
122 * filesystem implements cancellation of AIO request.
123 * Thus the polling loop below is the normal code path.
125 ret
= io_cancel(laiocb
->ctx
->ctx
, &laiocb
->iocb
, &event
);
127 laiocb
->ret
= -ECANCELED
;
132 * We have to wait for the iocb to finish.
134 * The only way to get the iocb status update is by polling the io context.
135 * We might be able to do this slightly more optimal by removing the
138 while (laiocb
->ret
== -EINPROGRESS
) {
139 qemu_laio_completion_cb(&laiocb
->ctx
->e
);
143 static const AIOCBInfo laio_aiocb_info
= {
144 .aiocb_size
= sizeof(struct qemu_laiocb
),
145 .cancel
= laio_cancel
,
148 BlockDriverAIOCB
*laio_submit(BlockDriverState
*bs
, void *aio_ctx
, int fd
,
149 int64_t sector_num
, QEMUIOVector
*qiov
, int nb_sectors
,
150 BlockDriverCompletionFunc
*cb
, void *opaque
, int type
)
152 struct qemu_laio_state
*s
= aio_ctx
;
153 struct qemu_laiocb
*laiocb
;
155 off_t offset
= sector_num
* 512;
157 laiocb
= qemu_aio_get(&laio_aiocb_info
, bs
, cb
, opaque
);
158 laiocb
->nbytes
= nb_sectors
* 512;
160 laiocb
->ret
= -EINPROGRESS
;
161 laiocb
->is_read
= (type
== QEMU_AIO_READ
);
164 iocbs
= &laiocb
->iocb
;
168 io_prep_pwritev(iocbs
, fd
, qiov
->iov
, qiov
->niov
, offset
);
171 io_prep_preadv(iocbs
, fd
, qiov
->iov
, qiov
->niov
, offset
);
173 /* Currently Linux kernel does not support other operations */
175 fprintf(stderr
, "%s: invalid AIO request type 0x%x.\n",
179 io_set_eventfd(&laiocb
->iocb
, event_notifier_get_fd(&s
->e
));
182 if (io_submit(s
->ctx
, 1, &iocbs
) < 0)
184 return &laiocb
->common
;
189 qemu_aio_release(laiocb
);
193 void *laio_init(void)
195 struct qemu_laio_state
*s
;
197 s
= g_malloc0(sizeof(*s
));
198 if (event_notifier_init(&s
->e
, false) < 0) {
202 if (io_setup(MAX_EVENTS
, &s
->ctx
) != 0) {
206 qemu_aio_set_event_notifier(&s
->e
, qemu_laio_completion_cb
,
212 event_notifier_cleanup(&s
->e
);