| 1 | /*- |
| 2 | * SPDX-License-Identifier: BSD-2-Clause |
| 3 | * |
| 4 | * Copyright (c) 1997 John S. Dyson. All rights reserved. |
| 5 | * |
| 6 | * Redistribution and use in source and binary forms, with or without |
| 7 | * modification, are permitted provided that the following conditions |
| 8 | * are met: |
| 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. |
| 13 | * |
| 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. |
| 17 | */ |
| 18 | |
| 19 | #ifndef _SYS_AIO_H_ |
| 20 | #define	_SYS_AIO_H_ |
| 21 | |
| 22 | #include <sys/types.h> |
| 23 | #include <sys/signal.h> |
| 24 | #ifdef _KERNEL |
| 25 | #include <sys/queue.h> |
| 26 | #include <sys/event.h> |
| 27 | #include <sys/signalvar.h> |
| 28 | #include <sys/uio.h> |
| 29 | #endif |
| 30 | |
| 31 | /* |
| 32 | * Returned by aio_cancel: |
| 33 | */ |
| 34 | #define	AIO_CANCELED		0x1 |
| 35 | #define	AIO_NOTCANCELED		0x2 |
| 36 | #define	AIO_ALLDONE		0x3 |
| 37 | |
| 38 | /* |
| 39 | * LIO opcodes |
| 40 | */ |
| 41 | #define	LIO_NOP			0x0 |
| 42 | #define LIO_WRITE		0x1 |
| 43 | #define	LIO_READ		0x2 |
| 44 | #if __BSD_VISIBLE |
| 45 | #define	LIO_VECTORED		0x4 |
| 46 | #define	LIO_WRITEV		(LIO_WRITE | LIO_VECTORED) |
| 47 | #define	LIO_READV		(LIO_READ | LIO_VECTORED) |
| 48 | #endif |
| 49 | #if defined(_KERNEL) || defined(_WANT_ALL_LIO_OPCODES) |
| 50 | #define	LIO_SYNC		0x8 |
| 51 | #define	LIO_DSYNC		(0x10 | LIO_SYNC) |
| 52 | #define	LIO_MLOCK		0x20 |
| 53 | #endif |
| 54 | #if __BSD_VISIBLE |
| 55 | #define	LIO_FOFFSET		0x40 |
| 56 | #endif |
| 57 | |
| 58 | /* aio_read2/aio_write2 flags */ |
| 59 | #if __BSD_VISIBLE |
| 60 | #define	AIO_OP2_FOFFSET		0x00000001 |
| 61 | #define	AIO_OP2_VECTORED	0x00000002 |
| 62 | #endif |
| 63 | |
| 64 | /* |
| 65 | * LIO modes |
| 66 | */ |
| 67 | #define	LIO_NOWAIT		0x0 |
| 68 | #define	LIO_WAIT		0x1 |
| 69 | |
| 70 | /* |
| 71 | * Maximum number of operations in a single lio_listio call |
| 72 | */ |
| 73 | #define	AIO_LISTIO_MAX		16 |
| 74 | |
| 75 | #ifdef _KERNEL |
| 76 | |
| 77 | /* Default values of tunables for the AIO worker pool. */ |
| 78 | |
| 79 | #ifndef MAX_AIO_PROCS |
| 80 | #define MAX_AIO_PROCS		32 |
| 81 | #endif |
| 82 | |
| 83 | #ifndef TARGET_AIO_PROCS |
| 84 | #define TARGET_AIO_PROCS	4 |
| 85 | #endif |
| 86 | |
| 87 | #ifndef AIOD_LIFETIME_DEFAULT |
| 88 | #define AIOD_LIFETIME_DEFAULT	(30 * hz) |
| 89 | #endif |
| 90 | |
| 91 | #endif |
| 92 | |
| 93 | /* |
| 94 | * Private members for aiocb -- don't access |
| 95 | * directly. |
| 96 | */ |
| 97 | struct __aiocb_private { |
| 98 | 	long	status; |
| 99 | 	long	error; |
| 100 | 	void	*spare; |
| 101 | }; |
| 102 | |
| 103 | /* |
| 104 | * I/O control block |
| 105 | */ |
| 106 | typedef struct aiocb { |
| 107 | 	int	aio_fildes;		/* File descriptor */ |
| 108 | 	off_t	aio_offset;		/* File offset for I/O */ |
| 109 | 	volatile void *aio_buf;		/* I/O buffer in process space */ |
| 110 | 	size_t	aio_nbytes;		/* Number of bytes for I/O */ |
| 111 | 	int	__spare__[2]; |
| 112 | 	void	*__spare2__; |
| 113 | 	int	aio_lio_opcode;		/* LIO opcode */ |
| 114 | 	int	aio_reqprio;		/* Request priority -- ignored */ |
| 115 | 	struct	__aiocb_private	_aiocb_private; |
| 116 | 	struct	sigevent aio_sigevent;	/* Signal to deliver */ |
| 117 | } aiocb_t; |
| 118 | |
| 119 | #define	aio_iov	aio_buf			/* I/O scatter/gather list */ |
| 120 | #define	aio_iovcnt	aio_nbytes	/* Length of aio_iov */ |
| 121 | |
| 122 | #ifdef _KERNEL |
| 123 | |
| 124 | typedef void aio_cancel_fn_t(struct kaiocb *); |
| 125 | typedef void aio_handle_fn_t(struct kaiocb *); |
| 126 | |
| 127 | /* |
| 128 | * Kernel version of an I/O control block. |
| 129 | * |
| 130 | * Locking key: |
| 131 | * * - need not protected |
| 132 | * a - locked by kaioinfo lock |
| 133 | * b - locked by backend lock |
| 134 | * c - locked by aio_job_mtx |
| 135 | */ |
| 136 | struct kaiocb { |
| 137 | 	TAILQ_ENTRY(kaiocb) list;	/* (b) backend-specific list of jobs */ |
| 138 | 	TAILQ_ENTRY(kaiocb) plist;	/* (a) lists of pending / done jobs */ |
| 139 | 	TAILQ_ENTRY(kaiocb) allist;	/* (a) list of all jobs in proc */ |
| 140 | 	int	jobflags;		/* (a) job flags */ |
| 141 | 	int	ioflags;		/* (*) io flags */ |
| 142 | 	int	inblock;		/* (*) input blocks */ |
| 143 | 	int	outblock;		/* (*) output blocks */ |
| 144 | 	int	msgsnd;			/* (*) messages sent */ |
| 145 | 	int	msgrcv;			/* (*) messages received */ |
| 146 | 	struct	proc *userproc;		/* (*) user process */ |
| 147 | 	struct	ucred *cred;		/* (*) active credential when created */ |
| 148 | 	struct	file *fd_file;		/* (*) pointer to file structure */ |
| 149 | 	struct	aioliojob *lio;		/* (*) optional lio job */ |
| 150 | 	struct	aiocb *ujob;		/* (*) pointer in userspace of aiocb */ |
| 151 | 	struct	knlist klist;		/* (a) list of knotes */ |
| 152 | 	struct	aiocb uaiocb;		/* (*) copy of user I/O control block */ |
| 153 | 	struct	uio uio;		/* (*) storage for non-vectored uio */ |
| 154 | 	struct	iovec iov[1];		/* (*) storage for non-vectored uio */ |
| 155 | 	struct	uio *uiop;		/* (*) Possibly malloced uio */ |
| 156 | 	ksiginfo_t ksi;			/* (a) realtime signal info */ |
| 157 | 	uint64_t seqno;			/* (*) job number */ |
| 158 | 	aio_cancel_fn_t *cancel_fn;	/* (a) backend cancel function */ |
| 159 | 	aio_handle_fn_t *handle_fn;	/* (c) backend handle function */ |
| 160 | 	union {				/* Backend-specific data fields */ |
| 161 | 		struct {		/* BIO backend */ |
| 162 | 			volatile u_int nbio; /* Number of remaining bios */ |
| 163 | 			int	error;	/* Worst error of all bios */ |
| 164 | 			long	nbytes;	/* Bytes completed so far */ |
| 165 | 		}; |
| 166 | 		struct {		/* fsync() requests */ |
| 167 | 			int	pending; /* (a) number of pending I/O */ |
| 168 | 		}; |
| 169 | 		struct {		/* socket backend */ |
| 170 | 			void	*backend1; |
| 171 | 			long	backend3; |
| 172 | 			int	backend4; |
| 173 | 		}; |
| 174 | 	}; |
| 175 | }; |
| 176 | |
| 177 | struct socket; |
| 178 | struct sockbuf; |
| 179 | |
| 180 | /* |
| 181 | * AIO backends should permit cancellation of queued requests waiting to |
| 182 | * be serviced by installing a cancel routine while the request is |
| 183 | * queued. The cancellation routine should dequeue the request if |
| 184 | * necessary and cancel it. Care must be used to handle races between |
| 185 | * queueing and dequeueing requests and cancellation. |
| 186 | * |
| 187 | * When queueing a request somewhere such that it can be cancelled, the |
| 188 | * caller should: |
| 189 | * |
| 190 | * 1) Acquire lock that protects the associated queue. |
| 191 | * 2) Call aio_set_cancel_function() to install the cancel routine. |
| 192 | * 3) If that fails, the request has a pending cancel and should be |
| 193 | * cancelled via aio_cancel(). |
| 194 | * 4) Queue the request. |
| 195 | * |
| 196 | * When dequeueing a request to service it or hand it off to somewhere else, |
| 197 | * the caller should: |
| 198 | * |
| 199 | * 1) Acquire the lock that protects the associated queue. |
| 200 | * 2) Dequeue the request. |
| 201 | * 3) Call aio_clear_cancel_function() to clear the cancel routine. |
| 202 | * 4) If that fails, the cancel routine is about to be called. The |
| 203 | * caller should ignore the request. |
| 204 | * |
| 205 | * The cancel routine should: |
| 206 | * |
| 207 | * 1) Acquire the lock that protects the associated queue. |
| 208 | * 2) Call aio_cancel_cleared() to determine if the request is already |
| 209 | * dequeued due to a race with dequeueing thread. |
| 210 | * 3) If that fails, dequeue the request. |
| 211 | * 4) Cancel the request via aio_cancel(). |
| 212 | */ |
| 213 | |
| 214 | bool	aio_cancel_cleared(struct kaiocb *job); |
| 215 | void	aio_cancel(struct kaiocb *job); |
| 216 | bool	aio_clear_cancel_function(struct kaiocb *job); |
| 217 | void	aio_complete(struct kaiocb *job, long status, int error); |
| 218 | void	aio_schedule(struct kaiocb *job, aio_handle_fn_t *func); |
| 219 | bool	aio_set_cancel_function(struct kaiocb *job, aio_cancel_fn_t *func); |
| 220 | void	aio_switch_vmspace(struct kaiocb *job); |
| 221 | |
| 222 | #else /* !_KERNEL */ |
| 223 | |
| 224 | struct timespec; |
| 225 | |
| 226 | __BEGIN_DECLS |
| 227 | /* |
| 228 | * Asynchronously read from a file |
| 229 | */ |
| 230 | int	aio_read(struct aiocb *); |
| 231 | #if __BSD_VISIBLE |
| 232 | int	aio_readv(struct aiocb *); |
| 233 | #endif |
| 234 | |
| 235 | /* |
| 236 | * Asynchronously write to file |
| 237 | */ |
| 238 | int	aio_write(struct aiocb *); |
| 239 | #if __BSD_VISIBLE |
| 240 | int	aio_writev(struct aiocb *); |
| 241 | #endif |
| 242 | |
| 243 | /* |
| 244 | * List I/O Asynchronously/synchronously read/write to/from file |
| 245 | *	"lio_mode" specifies whether or not the I/O is synchronous. |
| 246 | *	"acb_list" is an array of "nacb_listent" I/O control blocks. |
| 247 | *	when all I/Os are complete, the optional signal "sig" is sent. |
| 248 | */ |
| 249 | int	lio_listio(int, struct aiocb *__restrict const *__restrict, int, |
| 250 | struct sigevent *); |
| 251 | |
| 252 | /* |
| 253 | * Get completion status |
| 254 | *	returns EINPROGRESS until I/O is complete. |
| 255 | *	this routine does not block. |
| 256 | */ |
| 257 | int	aio_error(const struct aiocb *); |
| 258 | |
| 259 | /* |
| 260 | * Finish up I/O, releasing I/O resources and returns the value |
| 261 | *	that would have been associated with a synchronous I/O request. |
| 262 | *	This routine must be called once and only once for each |
| 263 | *	I/O control block who has had I/O associated with it. |
| 264 | */ |
| 265 | ssize_t	aio_return(struct aiocb *); |
| 266 | |
| 267 | /* |
| 268 | * Cancel I/O |
| 269 | */ |
| 270 | int	aio_cancel(int, struct aiocb *); |
| 271 | |
| 272 | /* |
| 273 | * Suspend until all specified I/O or timeout is complete. |
| 274 | */ |
| 275 | int	aio_suspend(const struct aiocb * const[], int, const struct timespec *); |
| 276 | |
| 277 | /* |
| 278 | * Asynchronous mlock |
| 279 | */ |
| 280 | int	aio_mlock(struct aiocb *); |
| 281 | |
| 282 | #if __BSD_VISIBLE |
| 283 | ssize_t	aio_waitcomplete(struct aiocb **, struct timespec *); |
| 284 | int	aio_read2(struct aiocb *, int); |
| 285 | int	aio_write2(struct aiocb *, int); |
| 286 | #endif |
| 287 | |
| 288 | int	aio_fsync(int op, struct aiocb *aiocbp); |
| 289 | __END_DECLS |
| 290 | |
| 291 | #endif /* !_KERNEL */ |
| 292 | |
| 293 | #endif /* !_SYS_AIO_H_ */ |