* Copyright 2000,2001,2002 Red Hat, Inc.
* Written by Benjamin LaHaise <bcrl@redhat.com>
* libaio Linux async I/O interface
* This library is free software; you can redistribute it and/or
* modify it under the terms of the GNU Lesser General Public
* License as published by the Free Software Foundation; either
* version 2 of the License, or (at your option) any later version.
* This library is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
* Lesser General Public License for more details.
* You should have received a copy of the GNU Lesser General Public
* License along with this library; if not, write to the Free Software
* Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
typedef struct io_context *io_context_t;
typedef enum io_iocb_cmd {
/* little endian, 32 bits */
#if defined(__i386__) || (defined(__arm__) && !defined(__ARMEB__)) || \
defined(__sh__) || defined(__bfin__) || defined(__MIPSEL__) || \
defined(__cris__) || (defined(__riscv) && __riscv_xlen == 32) || \
(defined(__GNUC__) && defined(__BYTE_ORDER__) && \
__BYTE_ORDER__ == __ORDER_LITTLE_ENDIAN__ && __SIZEOF_LONG__ == 4)
#define PADDED(x, y) x; unsigned y
#define PADDEDptr(x, y) x; unsigned y
#define PADDEDul(x, y) unsigned long x; unsigned y
/* little endian, 64 bits */
#elif defined(__ia64__) || defined(__x86_64__) || defined(__alpha__) || \
(defined(__aarch64__) && defined(__AARCH64EL__)) || \
(defined(__riscv) && __riscv_xlen == 64) || \
(defined(__GNUC__) && defined(__BYTE_ORDER__) && \
__BYTE_ORDER__ == __ORDER_LITTLE_ENDIAN__ && __SIZEOF_LONG__ == 8)
#define PADDED(x, y) x, y
#define PADDEDptr(x, y) x
#define PADDEDul(x, y) unsigned long x
/* big endian, 64 bits */
#elif defined(__powerpc64__) || defined(__s390x__) || \
(defined(__sparc__) && defined(__arch64__)) || \
(defined(__aarch64__) && defined(__AARCH64EB__)) || \
(defined(__GNUC__) && defined(__BYTE_ORDER__) && \
__BYTE_ORDER__ == __ORDER_BIG_ENDIAN__ && __SIZEOF_LONG__ == 8)
#define PADDED(x, y) unsigned y; x
#define PADDEDul(x, y) unsigned long x
/* big endian, 32 bits */
#elif defined(__PPC__) || defined(__s390__) || \
(defined(__arm__) && defined(__ARMEB__)) || \
defined(__sparc__) || defined(__MIPSEB__) || defined(__m68k__) || \
defined(__hppa__) || defined(__frv__) || defined(__avr32__) || \
(defined(__GNUC__) && defined(__BYTE_ORDER__) && \
__BYTE_ORDER__ == __ORDER_BIG_ENDIAN__ && __SIZEOF_LONG__ == 4)
#define PADDED(x, y) unsigned y; x
#define PADDEDptr(x, y) unsigned y; x
#define PADDEDul(x, y) unsigned y; unsigned long x
PADDED(int events, __pad1);
}; /* result code is the set of result flags or -'ve errno */
struct io_iocb_sockaddr {
}; /* result code is the length of the sockaddr, or -'ve errno */
PADDEDptr(void *buf, __pad1);
PADDEDul(nbytes, __pad2);
}; /* result code is the amount read or -'ve errno */
}; /* result code is the amount read or -'ve errno */
PADDEDptr(void *data, __pad1); /* Return in the io completion event */
/* key: For use in identifying io requests */
/* aio_rw_flags: RWF_* flags (such as RWF_NOWAIT) */
PADDED(unsigned key, aio_rw_flags);
struct io_iocb_poll poll;
struct io_iocb_sockaddr saddr;
PADDEDptr(void *data, __pad1);
PADDEDptr(struct iocb *obj, __pad2);
typedef void (*io_callback_t)(io_context_t ctx, struct iocb *iocb, long res, long res2);
extern int io_queue_init(int maxevents, io_context_t *ctxp);
/*extern int io_queue_grow(io_context_t ctx, int new_maxevents);*/
extern int io_queue_release(io_context_t ctx);
/*extern int io_queue_wait(io_context_t ctx, struct timespec *timeout);*/
extern int io_queue_run(io_context_t ctx);
extern int io_setup(int maxevents, io_context_t *ctxp);
extern int io_destroy(io_context_t ctx);
extern int io_submit(io_context_t ctx, long nr, struct iocb *ios[]);
extern int io_cancel(io_context_t ctx, struct iocb *iocb, struct io_event *evt);
extern int io_getevents(io_context_t ctx_id, long min_nr, long nr, struct io_event *events, struct timespec *timeout);
extern int io_pgetevents(io_context_t ctx_id, long min_nr, long nr,
struct io_event *events, struct timespec *timeout,
static inline void io_set_callback(struct iocb *iocb, io_callback_t cb)
static inline void io_prep_pread(struct iocb *iocb, int fd, void *buf, size_t count, long long offset)
memset(iocb, 0, sizeof(*iocb));
iocb->aio_lio_opcode = IO_CMD_PREAD;
iocb->u.c.nbytes = count;
iocb->u.c.offset = offset;
static inline void io_prep_pwrite(struct iocb *iocb, int fd, void *buf, size_t count, long long offset)
memset(iocb, 0, sizeof(*iocb));
iocb->aio_lio_opcode = IO_CMD_PWRITE;
iocb->u.c.nbytes = count;
iocb->u.c.offset = offset;
static inline void io_prep_preadv(struct iocb *iocb, int fd, const struct iovec *iov, int iovcnt, long long offset)
memset(iocb, 0, sizeof(*iocb));
iocb->aio_lio_opcode = IO_CMD_PREADV;
iocb->u.c.buf = (void *)iov;
iocb->u.c.nbytes = iovcnt;
iocb->u.c.offset = offset;
static inline void io_prep_pwritev(struct iocb *iocb, int fd, const struct iovec *iov, int iovcnt, long long offset)
memset(iocb, 0, sizeof(*iocb));
iocb->aio_lio_opcode = IO_CMD_PWRITEV;
iocb->u.c.buf = (void *)iov;
iocb->u.c.nbytes = iovcnt;
iocb->u.c.offset = offset;
static inline void io_prep_preadv2(struct iocb *iocb, int fd, const struct iovec *iov, int iovcnt, long long offset, int flags)
memset(iocb, 0, sizeof(*iocb));
iocb->aio_lio_opcode = IO_CMD_PREADV;
iocb->aio_rw_flags = flags;
iocb->u.c.buf = (void *)iov;
iocb->u.c.nbytes = iovcnt;
iocb->u.c.offset = offset;
static inline void io_prep_pwritev2(struct iocb *iocb, int fd, const struct iovec *iov, int iovcnt, long long offset, int flags)
memset(iocb, 0, sizeof(*iocb));
iocb->aio_lio_opcode = IO_CMD_PWRITEV;
iocb->aio_rw_flags = flags;
iocb->u.c.buf = (void *)iov;
iocb->u.c.nbytes = iovcnt;
iocb->u.c.offset = offset;
static inline void io_prep_poll(struct iocb *iocb, int fd, int events)
memset(iocb, 0, sizeof(*iocb));
iocb->aio_lio_opcode = IO_CMD_POLL;
iocb->u.poll.events = events;
static inline int io_poll(io_context_t ctx, struct iocb *iocb, io_callback_t cb, int fd, int events)
io_prep_poll(iocb, fd, events);
io_set_callback(iocb, cb);
return io_submit(ctx, 1, &iocb);
static inline void io_prep_fsync(struct iocb *iocb, int fd)
memset(iocb, 0, sizeof(*iocb));
iocb->aio_lio_opcode = IO_CMD_FSYNC;
static inline int io_fsync(io_context_t ctx, struct iocb *iocb, io_callback_t cb, int fd)
io_set_callback(iocb, cb);
return io_submit(ctx, 1, &iocb);
static inline void io_prep_fdsync(struct iocb *iocb, int fd)
memset(iocb, 0, sizeof(*iocb));
iocb->aio_lio_opcode = IO_CMD_FDSYNC;
static inline int io_fdsync(io_context_t ctx, struct iocb *iocb, io_callback_t cb, int fd)
io_prep_fdsync(iocb, fd);
io_set_callback(iocb, cb);
return io_submit(ctx, 1, &iocb);
static inline void io_set_eventfd(struct iocb *iocb, int eventfd)
iocb->u.c.flags |= (1 << 0) /* IOCB_FLAG_RESFD */;
iocb->u.c.resfd = eventfd;