2 * SPDX-License-Identifier: BSD-2-Clause-FreeBSD
4 * Copyright (c) 1997 John S. Dyson. All rights reserved.
6 * Redistribution and use in source and binary forms, with or without
7 * modification, are permitted provided that the following conditions
9 * 1. Redistributions of source code must retain the above copyright
10 * notice, this list of conditions and the following disclaimer.
11 * 2. John S. Dyson's name may not be used to endorse or promote products
12 * derived from this software without specific prior written permission.
14 * DISCLAIMER: This code isn't warranted to do anything useful. Anything
15 * bad that happens because of using this software isn't the responsibility
16 * of the author. This software is distributed AS-IS.
24 #include <sys/types.h>
25 #include <sys/signal.h>
27 #include <sys/queue.h>
28 #include <sys/event.h>
29 #include <sys/signalvar.h>
34 * Returned by aio_cancel:
36 #define AIO_CANCELED 0x1
37 #define AIO_NOTCANCELED 0x2
38 #define AIO_ALLDONE 0x3
46 #if defined(_KERNEL) || defined(_WANT_ALL_LIO_OPCODES)
47 #define LIO_VECTORED 0x4
48 #define LIO_WRITEV (LIO_WRITE | LIO_VECTORED)
49 #define LIO_READV (LIO_READ | LIO_VECTORED)
51 #define LIO_DSYNC (0x10 | LIO_SYNC)
52 #define LIO_MLOCK 0x20
58 #define LIO_NOWAIT 0x0
62 * Maximum number of operations in a single lio_listio call
64 #define AIO_LISTIO_MAX 16
68 /* Default values of tunables for the AIO worker pool. */
71 #define MAX_AIO_PROCS 32
74 #ifndef TARGET_AIO_PROCS
75 #define TARGET_AIO_PROCS 4
78 #ifndef AIOD_LIFETIME_DEFAULT
79 #define AIOD_LIFETIME_DEFAULT (30 * hz)
85 * Private members for aiocb -- don't access
88 struct __aiocb_private {
97 typedef struct aiocb {
98 int aio_fildes; /* File descriptor */
99 off_t aio_offset; /* File offset for I/O */
100 volatile void *aio_buf; /* I/O buffer in process space */
101 size_t aio_nbytes; /* Number of bytes for I/O */
104 int aio_lio_opcode; /* LIO opcode */
105 int aio_reqprio; /* Request priority -- ignored */
106 struct __aiocb_private _aiocb_private;
107 struct sigevent aio_sigevent; /* Signal to deliver */
110 #define aio_iov aio_buf /* I/O scatter/gather list */
111 #define aio_iovcnt aio_nbytes /* Length of aio_iov */
115 typedef void aio_cancel_fn_t(struct kaiocb *);
116 typedef void aio_handle_fn_t(struct kaiocb *);
119 * Kernel version of an I/O control block.
122 * * - need not protected
123 * a - locked by kaioinfo lock
124 * b - locked by backend lock
125 * c - locked by aio_job_mtx
128 TAILQ_ENTRY(kaiocb) list; /* (b) backend-specific list of jobs */
129 TAILQ_ENTRY(kaiocb) plist; /* (a) lists of pending / done jobs */
130 TAILQ_ENTRY(kaiocb) allist; /* (a) list of all jobs in proc */
131 int jobflags; /* (a) job flags */
132 int inblock; /* (*) input blocks */
133 int outblock; /* (*) output blocks */
134 int msgsnd; /* (*) messages sent */
135 int msgrcv; /* (*) messages received */
136 struct proc *userproc; /* (*) user process */
137 struct ucred *cred; /* (*) active credential when created */
138 struct file *fd_file; /* (*) pointer to file structure */
139 struct aioliojob *lio; /* (*) optional lio job */
140 struct aiocb *ujob; /* (*) pointer in userspace of aiocb */
141 struct knlist klist; /* (a) list of knotes */
142 struct aiocb uaiocb; /* (*) copy of user I/O control block */
143 struct uio uio; /* (*) storage for non-vectored uio */
144 struct iovec iov[1]; /* (*) storage for non-vectored uio */
145 struct uio *uiop; /* (*) Possibly malloced uio */
146 ksiginfo_t ksi; /* (a) realtime signal info */
147 uint64_t seqno; /* (*) job number */
148 aio_cancel_fn_t *cancel_fn; /* (a) backend cancel function */
149 aio_handle_fn_t *handle_fn; /* (c) backend handle function */
150 union { /* Backend-specific data fields */
151 struct { /* BIO backend */
152 int nbio; /* Number of remaining bios */
153 int error; /* Worst error of all bios */
154 long nbytes; /* Bytes completed so far */
156 struct { /* fsync() requests */
157 int pending; /* (a) number of pending I/O */
159 struct { /* socket backend */
171 * AIO backends should permit cancellation of queued requests waiting to
172 * be serviced by installing a cancel routine while the request is
173 * queued. The cancellation routine should dequeue the request if
174 * necessary and cancel it. Care must be used to handle races between
175 * queueing and dequeueing requests and cancellation.
177 * When queueing a request somewhere such that it can be cancelled, the
180 * 1) Acquire lock that protects the associated queue.
181 * 2) Call aio_set_cancel_function() to install the cancel routine.
182 * 3) If that fails, the request has a pending cancel and should be
183 * cancelled via aio_cancel().
184 * 4) Queue the request.
186 * When dequeueing a request to service it or hand it off to somewhere else,
189 * 1) Acquire the lock that protects the associated queue.
190 * 2) Dequeue the request.
191 * 3) Call aio_clear_cancel_function() to clear the cancel routine.
192 * 4) If that fails, the cancel routine is about to be called. The
193 * caller should ignore the request.
195 * The cancel routine should:
197 * 1) Acquire the lock that protects the associated queue.
198 * 2) Call aio_cancel_cleared() to determine if the request is already
199 * dequeued due to a race with dequeueing thread.
200 * 3) If that fails, dequeue the request.
201 * 4) Cancel the request via aio_cancel().
204 bool aio_cancel_cleared(struct kaiocb *job);
205 void aio_cancel(struct kaiocb *job);
206 bool aio_clear_cancel_function(struct kaiocb *job);
207 void aio_complete(struct kaiocb *job, long status, int error);
208 void aio_schedule(struct kaiocb *job, aio_handle_fn_t *func);
209 bool aio_set_cancel_function(struct kaiocb *job, aio_cancel_fn_t *func);
210 void aio_switch_vmspace(struct kaiocb *job);
218 * Asynchronously read from a file
220 int aio_read(struct aiocb *);
222 int aio_readv(struct aiocb *);
226 * Asynchronously write to file
228 int aio_write(struct aiocb *);
230 int aio_writev(struct aiocb *);
234 * List I/O Asynchronously/synchronously read/write to/from file
235 * "lio_mode" specifies whether or not the I/O is synchronous.
236 * "acb_list" is an array of "nacb_listent" I/O control blocks.
237 * when all I/Os are complete, the optional signal "sig" is sent.
239 int lio_listio(int, struct aiocb *__restrict const *__restrict, int,
243 * Get completion status
244 * returns EINPROGRESS until I/O is complete.
245 * this routine does not block.
247 int aio_error(const struct aiocb *);
250 * Finish up I/O, releasing I/O resources and returns the value
251 * that would have been associated with a synchronous I/O request.
252 * This routine must be called once and only once for each
253 * I/O control block who has had I/O associated with it.
255 ssize_t aio_return(struct aiocb *);
260 int aio_cancel(int, struct aiocb *);
263 * Suspend until all specified I/O or timeout is complete.
265 int aio_suspend(const struct aiocb * const[], int, const struct timespec *);
270 int aio_mlock(struct aiocb *);
273 ssize_t aio_waitcomplete(struct aiocb **, struct timespec *);
276 int aio_fsync(int op, struct aiocb *aiocbp);
279 #endif /* !_KERNEL */
281 #endif /* !_SYS_AIO_H_ */