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