1 | /* /usr/include/libaio.h |
---|
2 | * |
---|
3 | * Copyright 2000,2001,2002 Red Hat, Inc. |
---|
4 | * |
---|
5 | * Written by Benjamin LaHaise <bcrl@redhat.com> |
---|
6 | * |
---|
7 | * libaio Linux async I/O interface |
---|
8 | * |
---|
9 | * This library is free software; you can redistribute it and/or |
---|
10 | * modify it under the terms of the GNU Lesser General Public |
---|
11 | * License as published by the Free Software Foundation; either |
---|
12 | * version 2 of the License, or (at your option) any later version. |
---|
13 | * |
---|
14 | * This library is distributed in the hope that it will be useful, |
---|
15 | * but WITHOUT ANY WARRANTY; without even the implied warranty of |
---|
16 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU |
---|
17 | * Lesser General Public License for more details. |
---|
18 | * |
---|
19 | * You should have received a copy of the GNU Lesser General Public |
---|
20 | * License along with this library; if not, write to the Free Software |
---|
21 | * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA |
---|
22 | */ |
---|
23 | #ifndef __LIBAIO_H |
---|
24 | #define __LIBAIO_H |
---|
25 | |
---|
26 | #ifdef __cplusplus |
---|
27 | extern "C" { |
---|
28 | #endif |
---|
29 | |
---|
30 | #include <sys/types.h> |
---|
31 | #include <string.h> |
---|
32 | |
---|
33 | struct timespec; |
---|
34 | struct sockaddr; |
---|
35 | struct iovec; |
---|
36 | struct iocb; |
---|
37 | |
---|
38 | typedef struct io_context *io_context_t; |
---|
39 | |
---|
40 | typedef enum io_iocb_cmd { |
---|
41 | IO_CMD_PREAD = 0, |
---|
42 | IO_CMD_PWRITE = 1, |
---|
43 | |
---|
44 | IO_CMD_FSYNC = 2, |
---|
45 | IO_CMD_FDSYNC = 3, |
---|
46 | |
---|
47 | IO_CMD_POLL = 5, |
---|
48 | IO_CMD_NOOP = 6, |
---|
49 | } io_iocb_cmd_t; |
---|
50 | |
---|
51 | #if defined(__i386__) /* little endian, 32 bits */ |
---|
52 | #define PADDED(x, y) x; unsigned y |
---|
53 | #define PADDEDptr(x, y) x; unsigned y |
---|
54 | #define PADDEDul(x, y) unsigned long x; unsigned y |
---|
55 | #elif defined(__ia64__) || defined(__x86_64__) || defined(__alpha__) |
---|
56 | #define PADDED(x, y) x, y |
---|
57 | #define PADDEDptr(x, y) x |
---|
58 | #define PADDEDul(x, y) unsigned long x |
---|
59 | #elif defined(__powerpc64__) /* big endian, 64 bits */ |
---|
60 | #define PADDED(x, y) unsigned y; x |
---|
61 | #define PADDEDptr(x,y) x |
---|
62 | #define PADDEDul(x, y) unsigned long x |
---|
63 | #elif defined(__PPC__) /* big endian, 32 bits */ |
---|
64 | #define PADDED(x, y) unsigned y; x |
---|
65 | #define PADDEDptr(x, y) unsigned y; x |
---|
66 | #define PADDEDul(x, y) unsigned y; unsigned long x |
---|
67 | #elif defined(__s390x__) /* big endian, 64 bits */ |
---|
68 | #define PADDED(x, y) unsigned y; x |
---|
69 | #define PADDEDptr(x,y) x |
---|
70 | #define PADDEDul(x, y) unsigned long x |
---|
71 | #elif defined(__s390__) /* big endian, 32 bits */ |
---|
72 | #define PADDED(x, y) unsigned y; x |
---|
73 | #define PADDEDptr(x, y) unsigned y; x |
---|
74 | #define PADDEDul(x, y) unsigned y; unsigned long x |
---|
75 | #else |
---|
76 | #error endian? |
---|
77 | #endif |
---|
78 | |
---|
79 | struct io_iocb_poll { |
---|
80 | PADDED(int events, __pad1); |
---|
81 | }; /* result code is the set of result flags or -'ve errno */ |
---|
82 | |
---|
83 | struct io_iocb_sockaddr { |
---|
84 | struct sockaddr *addr; |
---|
85 | int len; |
---|
86 | }; /* result code is the length of the sockaddr, or -'ve errno */ |
---|
87 | |
---|
88 | struct io_iocb_common { |
---|
89 | PADDEDptr(void *buf, __pad1); |
---|
90 | PADDEDul(nbytes, __pad2); |
---|
91 | long long offset; |
---|
92 | long long __pad3, __pad4; |
---|
93 | }; /* result code is the amount read or -'ve errno */ |
---|
94 | |
---|
95 | struct io_iocb_vector { |
---|
96 | const struct iovec *vec; |
---|
97 | int nr; |
---|
98 | long long offset; |
---|
99 | }; /* result code is the amount read or -'ve errno */ |
---|
100 | |
---|
101 | struct iocb { |
---|
102 | PADDEDptr(void *data, __pad1); /* Return in the io completion event */ |
---|
103 | PADDED(unsigned key, __pad2); /* For use in identifying io requests */ |
---|
104 | |
---|
105 | short aio_lio_opcode; |
---|
106 | short aio_reqprio; |
---|
107 | int aio_fildes; |
---|
108 | |
---|
109 | union { |
---|
110 | struct io_iocb_common c; |
---|
111 | struct io_iocb_vector v; |
---|
112 | struct io_iocb_poll poll; |
---|
113 | struct io_iocb_sockaddr saddr; |
---|
114 | } u; |
---|
115 | }; |
---|
116 | |
---|
117 | struct io_event { |
---|
118 | PADDEDptr(void *data, __pad1); |
---|
119 | PADDEDptr(struct iocb *obj, __pad2); |
---|
120 | PADDEDul(res, __pad3); |
---|
121 | PADDEDul(res2, __pad4); |
---|
122 | }; |
---|
123 | |
---|
124 | #undef PADDED |
---|
125 | #undef PADDEDptr |
---|
126 | #undef PADDEDul |
---|
127 | |
---|
128 | typedef void (*io_callback_t)(io_context_t ctx, struct iocb *iocb, long res, long res2); |
---|
129 | |
---|
130 | /* library wrappers */ |
---|
131 | extern int io_queue_init(int maxevents, io_context_t *ctxp); |
---|
132 | /*extern int io_queue_grow(io_context_t ctx, int new_maxevents);*/ |
---|
133 | extern int io_queue_release(io_context_t ctx); |
---|
134 | /*extern int io_queue_wait(io_context_t ctx, struct timespec *timeout);*/ |
---|
135 | extern int io_queue_run(io_context_t ctx); |
---|
136 | |
---|
137 | /* Actual syscalls */ |
---|
138 | extern int io_setup(int maxevents, io_context_t *ctxp); |
---|
139 | extern int io_destroy(io_context_t ctx); |
---|
140 | extern int io_submit(io_context_t ctx, long nr, struct iocb *ios[]); |
---|
141 | extern int io_cancel(io_context_t ctx, struct iocb *iocb, struct io_event *evt); |
---|
142 | extern int io_getevents(io_context_t ctx_id, long min_nr, long nr, struct io_event *events, struct timespec *timeout); |
---|
143 | |
---|
144 | |
---|
145 | static inline void io_set_callback(struct iocb *iocb, io_callback_t cb) |
---|
146 | { |
---|
147 | iocb->data = (void *)cb; |
---|
148 | } |
---|
149 | |
---|
150 | static inline void io_prep_pread(struct iocb *iocb, int fd, void *buf, size_t count, long long offset) |
---|
151 | { |
---|
152 | memset(iocb, 0, sizeof(*iocb)); |
---|
153 | iocb->aio_fildes = fd; |
---|
154 | iocb->aio_lio_opcode = IO_CMD_PREAD; |
---|
155 | iocb->aio_reqprio = 0; |
---|
156 | iocb->u.c.buf = buf; |
---|
157 | iocb->u.c.nbytes = count; |
---|
158 | iocb->u.c.offset = offset; |
---|
159 | } |
---|
160 | |
---|
161 | static inline void io_prep_pwrite(struct iocb *iocb, int fd, void *buf, size_t count, long long offset) |
---|
162 | { |
---|
163 | memset(iocb, 0, sizeof(*iocb)); |
---|
164 | iocb->aio_fildes = fd; |
---|
165 | iocb->aio_lio_opcode = IO_CMD_PWRITE; |
---|
166 | iocb->aio_reqprio = 0; |
---|
167 | iocb->u.c.buf = buf; |
---|
168 | iocb->u.c.nbytes = count; |
---|
169 | iocb->u.c.offset = offset; |
---|
170 | } |
---|
171 | |
---|
172 | static inline void io_prep_poll(struct iocb *iocb, int fd, int events) |
---|
173 | { |
---|
174 | memset(iocb, 0, sizeof(*iocb)); |
---|
175 | iocb->aio_fildes = fd; |
---|
176 | iocb->aio_lio_opcode = IO_CMD_POLL; |
---|
177 | iocb->aio_reqprio = 0; |
---|
178 | iocb->u.poll.events = events; |
---|
179 | } |
---|
180 | |
---|
181 | static inline int io_poll(io_context_t ctx, struct iocb *iocb, io_callback_t cb, int fd, int events) |
---|
182 | { |
---|
183 | io_prep_poll(iocb, fd, events); |
---|
184 | io_set_callback(iocb, cb); |
---|
185 | return io_submit(ctx, 1, &iocb); |
---|
186 | } |
---|
187 | |
---|
188 | static inline void io_prep_fsync(struct iocb *iocb, int fd) |
---|
189 | { |
---|
190 | memset(iocb, 0, sizeof(*iocb)); |
---|
191 | iocb->aio_fildes = fd; |
---|
192 | iocb->aio_lio_opcode = IO_CMD_FSYNC; |
---|
193 | iocb->aio_reqprio = 0; |
---|
194 | } |
---|
195 | |
---|
196 | static inline int io_fsync(io_context_t ctx, struct iocb *iocb, io_callback_t cb, int fd) |
---|
197 | { |
---|
198 | io_prep_fsync(iocb, fd); |
---|
199 | io_set_callback(iocb, cb); |
---|
200 | return io_submit(ctx, 1, &iocb); |
---|
201 | } |
---|
202 | |
---|
203 | static inline void io_prep_fdsync(struct iocb *iocb, int fd) |
---|
204 | { |
---|
205 | memset(iocb, 0, sizeof(*iocb)); |
---|
206 | iocb->aio_fildes = fd; |
---|
207 | iocb->aio_lio_opcode = IO_CMD_FDSYNC; |
---|
208 | iocb->aio_reqprio = 0; |
---|
209 | } |
---|
210 | |
---|
211 | static inline int io_fdsync(io_context_t ctx, struct iocb *iocb, io_callback_t cb, int fd) |
---|
212 | { |
---|
213 | io_prep_fdsync(iocb, fd); |
---|
214 | io_set_callback(iocb, cb); |
---|
215 | return io_submit(ctx, 1, &iocb); |
---|
216 | } |
---|
217 | |
---|
218 | #ifdef __cplusplus |
---|
219 | } |
---|
220 | #endif |
---|
221 | |
---|
222 | #endif /* __LIBAIO_H */ |
---|