2 * Copyright (c) 1997 John S. Dyson. All rights reserved.
4 * Redistribution and use in source and binary forms, with or without
5 * modification, are permitted provided that the following conditions
7 * 1. Redistributions of source code must retain the above copyright
8 * notice, this list of conditions and the following disclaimer.
9 * 2. John S. Dyson's name may not be used to endorse or promote products
10 * derived from this software without specific prior written permission.
12 * DISCLAIMER: This code isn't warranted to do anything useful. Anything
13 * bad that happens because of using this software isn't the responsibility
14 * of the author. This software is distributed AS-IS.
22 #include <sys/types.h>
23 #include <sys/signal.h>
25 #include <sys/queue.h>
26 #include <sys/event.h>
27 #include <sys/signalvar.h>
31 * Returned by aio_cancel:
33 #define AIO_CANCELED 0x1
34 #define AIO_NOTCANCELED 0x2
35 #define AIO_ALLDONE 0x3
51 #define LIO_NOWAIT 0x0
55 * Maximum number of allowed LIO operations
57 #define AIO_LISTIO_MAX 16
61 /* Default values of tunables for the AIO worker pool. */
64 #define MAX_AIO_PROCS 32
67 #ifndef TARGET_AIO_PROCS
68 #define TARGET_AIO_PROCS 4
71 #ifndef AIOD_LIFETIME_DEFAULT
72 #define AIOD_LIFETIME_DEFAULT (30 * hz)
78 * Private members for aiocb -- don't access
81 struct __aiocb_private {
90 typedef struct aiocb {
91 int aio_fildes; /* File descriptor */
92 off_t aio_offset; /* File offset for I/O */
93 volatile void *aio_buf; /* I/O buffer in process space */
94 size_t aio_nbytes; /* Number of bytes for I/O */
97 int aio_lio_opcode; /* LIO opcode */
98 int aio_reqprio; /* Request priority -- ignored */
99 struct __aiocb_private _aiocb_private;
100 struct sigevent aio_sigevent; /* Signal to deliver */
105 typedef void aio_cancel_fn_t(struct kaiocb *);
106 typedef void aio_handle_fn_t(struct kaiocb *);
109 * Kernel version of an I/O control block.
112 * * - need not protected
113 * a - locked by kaioinfo lock
114 * b - locked by backend lock
115 * c - locked by aio_job_mtx
118 TAILQ_ENTRY(kaiocb) list; /* (b) backend-specific list of jobs */
119 TAILQ_ENTRY(kaiocb) plist; /* (a) lists of pending / done jobs */
120 TAILQ_ENTRY(kaiocb) allist; /* (a) list of all jobs in proc */
121 int jobflags; /* (a) job flags */
122 int inblock; /* (*) input blocks */
123 int outblock; /* (*) output blocks */
124 int msgsnd; /* (*) messages sent */
125 int msgrcv; /* (*) messages received */
126 struct proc *userproc; /* (*) user process */
127 struct ucred *cred; /* (*) active credential when created */
128 struct file *fd_file; /* (*) pointer to file structure */
129 struct aioliojob *lio; /* (*) optional lio job */
130 struct aiocb *ujob; /* (*) pointer in userspace of aiocb */
131 struct knlist klist; /* (a) list of knotes */
132 struct aiocb uaiocb; /* (*) copy of user I/O control block */
133 ksiginfo_t ksi; /* (a) realtime signal info */
134 uint64_t seqno; /* (*) job number */
135 aio_cancel_fn_t *cancel_fn; /* (a) backend cancel function */
136 aio_handle_fn_t *handle_fn; /* (c) backend handle function */
137 union { /* Backend-specific data fields */
138 struct { /* BIO backend */
139 struct bio *bp; /* (*) BIO pointer */
140 struct buf *pbuf; /* (*) buffer pointer */
141 struct vm_page *pages[btoc(MAXPHYS)+1]; /* (*) */
142 int npages; /* (*) number of pages */
144 struct { /* fsync() requests */
145 int pending; /* (a) number of pending I/O */
160 * AIO backends should permit cancellation of queued requests waiting to
161 * be serviced by installing a cancel routine while the request is
162 * queued. The cancellation routine should dequeue the request if
163 * necessary and cancel it. Care must be used to handle races between
164 * queueing and dequeueing requests and cancellation.
166 * When queueing a request somewhere such that it can be cancelled, the
169 * 1) Acquire lock that protects the associated queue.
170 * 2) Call aio_set_cancel_function() to install the cancel routine.
171 * 3) If that fails, the request has a pending cancel and should be
172 * cancelled via aio_cancel().
173 * 4) Queue the request.
175 * When dequeueing a request to service it or hand it off to somewhere else,
178 * 1) Acquire the lock that protects the associated queue.
179 * 2) Dequeue the request.
180 * 3) Call aio_clear_cancel_function() to clear the cancel routine.
181 * 4) If that fails, the cancel routine is about to be called. The
182 * caller should ignore the request.
184 * The cancel routine should:
186 * 1) Acquire the lock that protects the associated queue.
187 * 2) Call aio_cancel_cleared() to determine if the request is already
188 * dequeued due to a race with dequeueing thread.
189 * 3) If that fails, dequeue the request.
190 * 4) Cancel the request via aio_cancel().
193 bool aio_cancel_cleared(struct kaiocb *job);
194 void aio_cancel(struct kaiocb *job);
195 bool aio_clear_cancel_function(struct kaiocb *job);
196 void aio_complete(struct kaiocb *job, long status, int error);
197 void aio_schedule(struct kaiocb *job, aio_handle_fn_t *func);
198 bool aio_set_cancel_function(struct kaiocb *job, aio_cancel_fn_t *func);
199 void aio_switch_vmspace(struct kaiocb *job);
207 * Asynchronously read from a file
209 int aio_read(struct aiocb *);
212 * Asynchronously write to file
214 int aio_write(struct aiocb *);
217 * List I/O Asynchronously/synchronously read/write to/from file
218 * "lio_mode" specifies whether or not the I/O is synchronous.
219 * "acb_list" is an array of "nacb_listent" I/O control blocks.
220 * when all I/Os are complete, the optional signal "sig" is sent.
222 int lio_listio(int, struct aiocb *__restrict const *__restrict, int,
226 * Get completion status
227 * returns EINPROGRESS until I/O is complete.
228 * this routine does not block.
230 int aio_error(const struct aiocb *);
233 * Finish up I/O, releasing I/O resources and returns the value
234 * that would have been associated with a synchronous I/O request.
235 * This routine must be called once and only once for each
236 * I/O control block who has had I/O associated with it.
238 ssize_t aio_return(struct aiocb *);
243 int aio_cancel(int, struct aiocb *);
246 * Suspend until all specified I/O or timeout is complete.
248 int aio_suspend(const struct aiocb * const[], int, const struct timespec *);
253 int aio_mlock(struct aiocb *);
256 ssize_t aio_waitcomplete(struct aiocb **, struct timespec *);
259 int aio_fsync(int op, struct aiocb *aiocbp);
262 #endif /* !_KERNEL */
264 #endif /* !_SYS_AIO_H_ */