1*49cdfc7eSAndroid Build Coastguard Worker #ifndef _LIBAIO_H
2*49cdfc7eSAndroid Build Coastguard Worker #define _LIBAIO_H
3*49cdfc7eSAndroid Build Coastguard Worker
4*49cdfc7eSAndroid Build Coastguard Worker // Don't include the actual header uapi/aio_abi.h
5*49cdfc7eSAndroid Build Coastguard Worker // since libaio redefines the structs for some reason.
6*49cdfc7eSAndroid Build Coastguard Worker // Instead override those definitions with the ones below.
7*49cdfc7eSAndroid Build Coastguard Worker #define __LINUX__AIO_ABI_H
8*49cdfc7eSAndroid Build Coastguard Worker
9*49cdfc7eSAndroid Build Coastguard Worker #include <errno.h>
10*49cdfc7eSAndroid Build Coastguard Worker #include <stdint.h>
11*49cdfc7eSAndroid Build Coastguard Worker
12*49cdfc7eSAndroid Build Coastguard Worker #if __UINTPTR_MAX__ == UINT64_MAX
13*49cdfc7eSAndroid Build Coastguard Worker #define PADDED_PTR(x, y) x
14*49cdfc7eSAndroid Build Coastguard Worker #elif __UINTPTR_MAX__ == UINT32_MAX
15*49cdfc7eSAndroid Build Coastguard Worker #define PADDED_PTR(x, y) x; unsigned y
16*49cdfc7eSAndroid Build Coastguard Worker #endif
17*49cdfc7eSAndroid Build Coastguard Worker
18*49cdfc7eSAndroid Build Coastguard Worker struct io_iocb_common {
19*49cdfc7eSAndroid Build Coastguard Worker PADDED_PTR(void *buf, __pad1);
20*49cdfc7eSAndroid Build Coastguard Worker __u64 nbytes;
21*49cdfc7eSAndroid Build Coastguard Worker __s64 offset;
22*49cdfc7eSAndroid Build Coastguard Worker __u64 reserved2;
23*49cdfc7eSAndroid Build Coastguard Worker __u32 flags;
24*49cdfc7eSAndroid Build Coastguard Worker __u32 resfd;
25*49cdfc7eSAndroid Build Coastguard Worker };
26*49cdfc7eSAndroid Build Coastguard Worker
27*49cdfc7eSAndroid Build Coastguard Worker struct iocb {
28*49cdfc7eSAndroid Build Coastguard Worker PADDED_PTR(void *data, __pad1);
29*49cdfc7eSAndroid Build Coastguard Worker __u32 key;
30*49cdfc7eSAndroid Build Coastguard Worker __u32 aio_rw_flags;
31*49cdfc7eSAndroid Build Coastguard Worker __u16 aio_lio_opcode;
32*49cdfc7eSAndroid Build Coastguard Worker __s16 aio_reqprio;
33*49cdfc7eSAndroid Build Coastguard Worker __u32 aio_fildes;
34*49cdfc7eSAndroid Build Coastguard Worker union {
35*49cdfc7eSAndroid Build Coastguard Worker struct io_iocb_common c;
36*49cdfc7eSAndroid Build Coastguard Worker } u;
37*49cdfc7eSAndroid Build Coastguard Worker };
38*49cdfc7eSAndroid Build Coastguard Worker
39*49cdfc7eSAndroid Build Coastguard Worker struct io_event {
40*49cdfc7eSAndroid Build Coastguard Worker PADDED_PTR(void *data, __pad1);
41*49cdfc7eSAndroid Build Coastguard Worker PADDED_PTR(struct iocb *obj, __pad2);
42*49cdfc7eSAndroid Build Coastguard Worker __s64 res;
43*49cdfc7eSAndroid Build Coastguard Worker __s64 res2;
44*49cdfc7eSAndroid Build Coastguard Worker };
45*49cdfc7eSAndroid Build Coastguard Worker
46*49cdfc7eSAndroid Build Coastguard Worker typedef unsigned long io_context_t;
47*49cdfc7eSAndroid Build Coastguard Worker typedef io_context_t aio_context_t;
48*49cdfc7eSAndroid Build Coastguard Worker
49*49cdfc7eSAndroid Build Coastguard Worker #include <asyncio/AsyncIO.h>
50*49cdfc7eSAndroid Build Coastguard Worker
51*49cdfc7eSAndroid Build Coastguard Worker #define IO_CMD_PREAD 0
52*49cdfc7eSAndroid Build Coastguard Worker #define IO_CMD_PWRITE 1
53*49cdfc7eSAndroid Build Coastguard Worker #define IO_CMD_FSYNC 2
54*49cdfc7eSAndroid Build Coastguard Worker #define IO_CMD_FDSYNC 3
55*49cdfc7eSAndroid Build Coastguard Worker #define IO_CMD_POLL 5
56*49cdfc7eSAndroid Build Coastguard Worker #define IO_CMD_NOOP 6
57*49cdfc7eSAndroid Build Coastguard Worker #define IO_CMD_PREADV 7
58*49cdfc7eSAndroid Build Coastguard Worker #define IO_CMD_PWRITEV 8
59*49cdfc7eSAndroid Build Coastguard Worker
60*49cdfc7eSAndroid Build Coastguard Worker typedef void (*io_callback_t)(io_context_t ctx, struct iocb *iocb, long res, long res2);
61*49cdfc7eSAndroid Build Coastguard Worker
redirect_error(int ret)62*49cdfc7eSAndroid Build Coastguard Worker static inline int redirect_error(int ret) {
63*49cdfc7eSAndroid Build Coastguard Worker return ret == -1 ? -errno : ret;
64*49cdfc7eSAndroid Build Coastguard Worker }
65*49cdfc7eSAndroid Build Coastguard Worker
66*49cdfc7eSAndroid Build Coastguard Worker // libaio doesn't follow syscall convention, so errors are returned
67*49cdfc7eSAndroid Build Coastguard Worker // as negative values and errno isn't used.
68*49cdfc7eSAndroid Build Coastguard Worker
libaio_setup(int maxevents,io_context_t * ctxp)69*49cdfc7eSAndroid Build Coastguard Worker static inline int libaio_setup(int maxevents, io_context_t *ctxp) {
70*49cdfc7eSAndroid Build Coastguard Worker int ret = io_setup(maxevents, ctxp);
71*49cdfc7eSAndroid Build Coastguard Worker return redirect_error(ret);
72*49cdfc7eSAndroid Build Coastguard Worker }
73*49cdfc7eSAndroid Build Coastguard Worker
libaio_destroy(io_context_t ctx)74*49cdfc7eSAndroid Build Coastguard Worker static inline int libaio_destroy(io_context_t ctx) {
75*49cdfc7eSAndroid Build Coastguard Worker int ret = io_destroy(ctx);
76*49cdfc7eSAndroid Build Coastguard Worker return redirect_error(ret);
77*49cdfc7eSAndroid Build Coastguard Worker }
78*49cdfc7eSAndroid Build Coastguard Worker
libaio_submit(io_context_t ctx,long nr,struct iocb * ios[])79*49cdfc7eSAndroid Build Coastguard Worker static inline int libaio_submit(io_context_t ctx, long nr, struct iocb *ios[]) {
80*49cdfc7eSAndroid Build Coastguard Worker int ret = io_submit(ctx, nr, ios);
81*49cdfc7eSAndroid Build Coastguard Worker return redirect_error(ret);
82*49cdfc7eSAndroid Build Coastguard Worker }
83*49cdfc7eSAndroid Build Coastguard Worker
libaio_cancel(io_context_t ctx,struct iocb * iocb,struct io_event * evt)84*49cdfc7eSAndroid Build Coastguard Worker static inline int libaio_cancel(io_context_t ctx, struct iocb *iocb, struct io_event *evt) {
85*49cdfc7eSAndroid Build Coastguard Worker int ret = io_cancel(ctx, iocb, evt);
86*49cdfc7eSAndroid Build Coastguard Worker return redirect_error(ret);
87*49cdfc7eSAndroid Build Coastguard Worker }
88*49cdfc7eSAndroid Build Coastguard Worker
libaio_getevents(io_context_t ctx_id,long min_nr,long nr,struct io_event * events,struct timespec * timeout)89*49cdfc7eSAndroid Build Coastguard Worker static inline int libaio_getevents(io_context_t ctx_id, long min_nr, long nr, struct io_event *events, struct timespec *timeout) {
90*49cdfc7eSAndroid Build Coastguard Worker int ret = io_getevents(ctx_id, min_nr, nr, events, timeout);
91*49cdfc7eSAndroid Build Coastguard Worker return redirect_error(ret);
92*49cdfc7eSAndroid Build Coastguard Worker }
93*49cdfc7eSAndroid Build Coastguard Worker
io_set_callback(struct iocb * iocb,io_callback_t cb)94*49cdfc7eSAndroid Build Coastguard Worker static inline void io_set_callback(struct iocb *iocb, io_callback_t cb)
95*49cdfc7eSAndroid Build Coastguard Worker {
96*49cdfc7eSAndroid Build Coastguard Worker iocb->data = (void *)cb;
97*49cdfc7eSAndroid Build Coastguard Worker }
98*49cdfc7eSAndroid Build Coastguard Worker
io_queue_init(int maxevents,io_context_t * ctxp)99*49cdfc7eSAndroid Build Coastguard Worker static inline int io_queue_init(int maxevents, io_context_t *ctxp) {
100*49cdfc7eSAndroid Build Coastguard Worker memset(ctxp, 0, sizeof(*ctxp));
101*49cdfc7eSAndroid Build Coastguard Worker return libaio_setup(maxevents, ctxp);
102*49cdfc7eSAndroid Build Coastguard Worker }
103*49cdfc7eSAndroid Build Coastguard Worker
104*49cdfc7eSAndroid Build Coastguard Worker // Override the system calls with their libaio versions.
105*49cdfc7eSAndroid Build Coastguard Worker
106*49cdfc7eSAndroid Build Coastguard Worker #define io_setup(a, b) libaio_setup(a, b)
107*49cdfc7eSAndroid Build Coastguard Worker #define io_destroy(a) libaio_destroy(a)
108*49cdfc7eSAndroid Build Coastguard Worker #define io_submit(a, b, c) libaio_submit(a, b, c)
109*49cdfc7eSAndroid Build Coastguard Worker #define io_cancel(a, b, c) libaio_cancel(a, b, c)
110*49cdfc7eSAndroid Build Coastguard Worker #define io_getevents(a, b, c, d, e) libaio_getevents(a, b, c, d, e)
111*49cdfc7eSAndroid Build Coastguard Worker
112*49cdfc7eSAndroid Build Coastguard Worker #define io_queue_release(a) io_destroy(a)
113*49cdfc7eSAndroid Build Coastguard Worker
114*49cdfc7eSAndroid Build Coastguard Worker #endif
115