libaio.h 7.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256
  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. #ifdef __cplusplus
  26. extern "C" {
  27. #endif
  28. #include <sys/types.h>
  29. #include <string.h>
  30. struct timespec;
  31. struct sockaddr;
  32. struct iovec;
  33. typedef struct io_context *io_context_t;
  34. typedef enum io_iocb_cmd {
  35. IO_CMD_PREAD = 0,
  36. IO_CMD_PWRITE = 1,
  37. IO_CMD_FSYNC = 2,
  38. IO_CMD_FDSYNC = 3,
  39. IO_CMD_POLL = 5, /* Never implemented in mainline, see io_prep_poll */
  40. IO_CMD_NOOP = 6,
  41. IO_CMD_PREADV = 7,
  42. IO_CMD_PWRITEV = 8,
  43. } io_iocb_cmd_t;
  44. #if defined(__i386__) /* little endian, 32 bits */
  45. #define PADDED(x, y) x; unsigned y
  46. #define PADDEDptr(x, y) x; unsigned y
  47. #define PADDEDul(x, y) unsigned long x; unsigned y
  48. #elif defined(__ia64__) || defined(__x86_64__) || defined(__alpha__)
  49. #define PADDED(x, y) x, y
  50. #define PADDEDptr(x, y) x
  51. #define PADDEDul(x, y) unsigned long x
  52. #elif defined(__powerpc64__) /* big endian, 64 bits */
  53. #define PADDED(x, y) unsigned y; x
  54. #define PADDEDptr(x,y) x
  55. #define PADDEDul(x, y) unsigned long x
  56. #elif defined(__PPC__) /* big endian, 32 bits */
  57. #define PADDED(x, y) unsigned y; x
  58. #define PADDEDptr(x, y) unsigned y; x
  59. #define PADDEDul(x, y) unsigned y; unsigned long x
  60. #elif defined(__s390x__) /* big endian, 64 bits */
  61. #define PADDED(x, y) unsigned y; x
  62. #define PADDEDptr(x,y) x
  63. #define PADDEDul(x, y) unsigned long x
  64. #elif defined(__s390__) /* big endian, 32 bits */
  65. #define PADDED(x, y) unsigned y; x
  66. #define PADDEDptr(x, y) unsigned y; x
  67. #define PADDEDul(x, y) unsigned y; unsigned long x
  68. #else
  69. #error endian?
  70. #endif
  71. struct io_iocb_poll {
  72. PADDED(int events, __pad1);
  73. }; /* result code is the set of result flags or -'ve errno */
  74. struct io_iocb_sockaddr {
  75. struct sockaddr *addr;
  76. int len;
  77. }; /* result code is the length of the sockaddr, or -'ve errno */
  78. struct io_iocb_common {
  79. PADDEDptr(void *buf, __pad1);
  80. PADDEDul(nbytes, __pad2);
  81. long long offset;
  82. long long __pad3;
  83. unsigned flags;
  84. unsigned resfd;
  85. }; /* result code is the amount read or -'ve errno */
  86. struct io_iocb_vector {
  87. const struct iovec *vec;
  88. int nr;
  89. long long offset;
  90. }; /* result code is the amount read or -'ve errno */
  91. struct iocb {
  92. PADDEDptr(void *data, __pad1); /* Return in the io completion event */
  93. PADDED(unsigned key, __pad2); /* For use in identifying io requests */
  94. short aio_lio_opcode;
  95. short aio_reqprio;
  96. int aio_fildes;
  97. union {
  98. struct io_iocb_common c;
  99. struct io_iocb_vector v;
  100. struct io_iocb_poll poll;
  101. struct io_iocb_sockaddr saddr;
  102. } u;
  103. };
  104. struct io_event {
  105. PADDEDptr(void *data, __pad1);
  106. PADDEDptr(struct iocb *obj, __pad2);
  107. PADDEDul(res, __pad3);
  108. PADDEDul(res2, __pad4);
  109. };
  110. #undef PADDED
  111. #undef PADDEDptr
  112. #undef PADDEDul
  113. typedef void (*io_callback_t)(io_context_t ctx, struct iocb *iocb, long res, long res2);
  114. /* library wrappers */
  115. extern int io_queue_init(int maxevents, io_context_t *ctxp);
  116. /*extern int io_queue_grow(io_context_t ctx, int new_maxevents);*/
  117. extern int io_queue_release(io_context_t ctx);
  118. /*extern int io_queue_wait(io_context_t ctx, struct timespec *timeout);*/
  119. extern int io_queue_run(io_context_t ctx);
  120. /* Actual syscalls */
  121. extern int io_setup(int maxevents, io_context_t *ctxp);
  122. extern int io_destroy(io_context_t ctx);
  123. extern int io_submit(io_context_t ctx, long nr, struct iocb *ios[]);
  124. extern int io_cancel(io_context_t ctx, struct iocb *iocb, struct io_event *evt);
  125. extern int io_getevents(io_context_t ctx_id, long min_nr, long nr, struct io_event *events, struct timespec *timeout);
  126. static inline void io_set_callback(struct iocb *iocb, io_callback_t cb)
  127. {
  128. iocb->data = (void *)cb;
  129. }
  130. static inline void io_prep_pread(struct iocb *iocb, int fd, void *buf, size_t count, long long offset)
  131. {
  132. memset(iocb, 0, sizeof(*iocb));
  133. iocb->aio_fildes = fd;
  134. iocb->aio_lio_opcode = IO_CMD_PREAD;
  135. iocb->aio_reqprio = 0;
  136. iocb->u.c.buf = buf;
  137. iocb->u.c.nbytes = count;
  138. iocb->u.c.offset = offset;
  139. }
  140. static inline void io_prep_pwrite(struct iocb *iocb, int fd, void *buf, size_t count, long long offset)
  141. {
  142. memset(iocb, 0, sizeof(*iocb));
  143. iocb->aio_fildes = fd;
  144. iocb->aio_lio_opcode = IO_CMD_PWRITE;
  145. iocb->aio_reqprio = 0;
  146. iocb->u.c.buf = buf;
  147. iocb->u.c.nbytes = count;
  148. iocb->u.c.offset = offset;
  149. }
  150. static inline void io_prep_preadv(struct iocb *iocb, int fd, const struct iovec *iov, int iovcnt, long long offset)
  151. {
  152. memset(iocb, 0, sizeof(*iocb));
  153. iocb->aio_fildes = fd;
  154. iocb->aio_lio_opcode = IO_CMD_PREADV;
  155. iocb->aio_reqprio = 0;
  156. iocb->u.c.buf = (void *)iov;
  157. iocb->u.c.nbytes = iovcnt;
  158. iocb->u.c.offset = offset;
  159. }
  160. static inline void io_prep_pwritev(struct iocb *iocb, int fd, const struct iovec *iov, int iovcnt, long long offset)
  161. {
  162. memset(iocb, 0, sizeof(*iocb));
  163. iocb->aio_fildes = fd;
  164. iocb->aio_lio_opcode = IO_CMD_PWRITEV;
  165. iocb->aio_reqprio = 0;
  166. iocb->u.c.buf = (void *)iov;
  167. iocb->u.c.nbytes = iovcnt;
  168. iocb->u.c.offset = offset;
  169. }
  170. /* Jeff Moyer says this was implemented in Red Hat AS2.1 and RHEL3.
  171. * AFAICT, it was never in mainline, and should not be used. --RR */
  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. static inline int io_poll(io_context_t ctx, struct iocb *iocb, io_callback_t cb, int fd, int events)
  181. {
  182. io_prep_poll(iocb, fd, events);
  183. io_set_callback(iocb, cb);
  184. return io_submit(ctx, 1, &iocb);
  185. }
  186. static inline void io_prep_fsync(struct iocb *iocb, int fd)
  187. {
  188. memset(iocb, 0, sizeof(*iocb));
  189. iocb->aio_fildes = fd;
  190. iocb->aio_lio_opcode = IO_CMD_FSYNC;
  191. iocb->aio_reqprio = 0;
  192. }
  193. static inline int io_fsync(io_context_t ctx, struct iocb *iocb, io_callback_t cb, int fd)
  194. {
  195. io_prep_fsync(iocb, fd);
  196. io_set_callback(iocb, cb);
  197. return io_submit(ctx, 1, &iocb);
  198. }
  199. static inline void io_prep_fdsync(struct iocb *iocb, int fd)
  200. {
  201. memset(iocb, 0, sizeof(*iocb));
  202. iocb->aio_fildes = fd;
  203. iocb->aio_lio_opcode = IO_CMD_FDSYNC;
  204. iocb->aio_reqprio = 0;
  205. }
  206. static inline int io_fdsync(io_context_t ctx, struct iocb *iocb, io_callback_t cb, int fd)
  207. {
  208. io_prep_fdsync(iocb, fd);
  209. io_set_callback(iocb, cb);
  210. return io_submit(ctx, 1, &iocb);
  211. }
  212. static inline void io_set_eventfd(struct iocb *iocb, int eventfd)
  213. {
  214. iocb->u.c.flags |= (1 << 0) /* IOCB_FLAG_RESFD */;
  215. iocb->u.c.resfd = eventfd;
  216. }
  217. #ifdef __cplusplus
  218. }
  219. #endif
  220. #endif /* __LIBAIO_H */