xref: /freebsd/sys/sys/aio.h (revision 2247f48941743cab420394b5ff0fc65ec8c69a99)
160727d8bSWarner Losh /*-
2c4e20cadSPedro F. Giffuni  * SPDX-License-Identifier: BSD-2-Clause-FreeBSD
3c4e20cadSPedro F. Giffuni  *
40f606585SJohn Dyson  * Copyright (c) 1997 John S. Dyson.  All rights reserved.
50f606585SJohn Dyson  *
60f606585SJohn Dyson  * Redistribution and use in source and binary forms, with or without
70f606585SJohn Dyson  * modification, are permitted provided that the following conditions
80f606585SJohn Dyson  * are met:
90f606585SJohn Dyson  * 1. Redistributions of source code must retain the above copyright
100f606585SJohn Dyson  *    notice, this list of conditions and the following disclaimer.
110f606585SJohn Dyson  * 2. John S. Dyson's name may not be used to endorse or promote products
120f606585SJohn Dyson  *    derived from this software without specific prior written permission.
130f606585SJohn Dyson  *
140f606585SJohn Dyson  * DISCLAIMER:  This code isn't warranted to do anything useful.  Anything
150f606585SJohn Dyson  * bad that happens because of using this software isn't the responsibility
160f606585SJohn Dyson  * of the author.  This software is distributed AS-IS.
170f606585SJohn Dyson  *
18c3aac50fSPeter Wemm  * $FreeBSD$
190f606585SJohn Dyson  */
200f606585SJohn Dyson 
21f0231545SDmitrij Tejblum #ifndef _SYS_AIO_H_
22f0231545SDmitrij Tejblum #define	_SYS_AIO_H_
23f0231545SDmitrij Tejblum 
240f606585SJohn Dyson #include <sys/types.h>
258a6472b7SPeter Dufault #include <sys/signal.h>
26f3215338SJohn Baldwin #ifdef _KERNEL
27f3215338SJohn Baldwin #include <sys/queue.h>
28f3215338SJohn Baldwin #include <sys/event.h>
29f3215338SJohn Baldwin #include <sys/signalvar.h>
30022ca2fcSAlan Somers #include <sys/uio.h>
31f3215338SJohn Baldwin #endif
32f0231545SDmitrij Tejblum 
330f606585SJohn Dyson /*
340f606585SJohn Dyson  * Returned by aio_cancel:
350f606585SJohn Dyson  */
360f606585SJohn Dyson #define	AIO_CANCELED		0x1
3778922e41SJohn Dyson #define	AIO_NOTCANCELED		0x2
380f606585SJohn Dyson #define	AIO_ALLDONE		0x3
390f606585SJohn Dyson 
400f606585SJohn Dyson /*
410f606585SJohn Dyson  * LIO opcodes
420f606585SJohn Dyson  */
430f606585SJohn Dyson #define	LIO_NOP			0x0
440f606585SJohn Dyson #define LIO_WRITE		0x1
450f606585SJohn Dyson #define	LIO_READ		0x2
46ff1a3078SAlan Somers #if defined(_KERNEL) || defined(_WANT_ALL_LIO_OPCODES)
47*2247f489SAlan Somers #define	LIO_VECTORED		0x4
48*2247f489SAlan Somers #define	LIO_WRITEV		(LIO_WRITE | LIO_VECTORED)
49*2247f489SAlan Somers #define	LIO_READV		(LIO_READ | LIO_VECTORED)
50*2247f489SAlan Somers #define	LIO_SYNC		0x8
51*2247f489SAlan Somers #define	LIO_DSYNC		(0x10 | LIO_SYNC)
52*2247f489SAlan Somers #define	LIO_MLOCK		0x20
5353fcc63cSDavid Xu #endif
540f606585SJohn Dyson 
550f606585SJohn Dyson /*
560f606585SJohn Dyson  * LIO modes
570f606585SJohn Dyson  */
580f606585SJohn Dyson #define	LIO_NOWAIT		0x0
590f606585SJohn Dyson #define	LIO_WAIT		0x1
600f606585SJohn Dyson 
610f606585SJohn Dyson /*
6221b8a7a6SAlan Somers  * Maximum number of operations in a single lio_listio call
637a2ac24cSPeter Dufault  */
647a2ac24cSPeter Dufault #define	AIO_LISTIO_MAX		16
657a2ac24cSPeter Dufault 
66f3215338SJohn Baldwin #ifdef _KERNEL
67f3215338SJohn Baldwin 
68f3215338SJohn Baldwin /* Default values of tunables for the AIO worker pool. */
69f3215338SJohn Baldwin 
70f3215338SJohn Baldwin #ifndef MAX_AIO_PROCS
71f3215338SJohn Baldwin #define MAX_AIO_PROCS		32
72f3215338SJohn Baldwin #endif
73f3215338SJohn Baldwin 
74f3215338SJohn Baldwin #ifndef TARGET_AIO_PROCS
75f3215338SJohn Baldwin #define TARGET_AIO_PROCS	4
76f3215338SJohn Baldwin #endif
77f3215338SJohn Baldwin 
78f3215338SJohn Baldwin #ifndef AIOD_LIFETIME_DEFAULT
79f3215338SJohn Baldwin #define AIOD_LIFETIME_DEFAULT	(30 * hz)
80f3215338SJohn Baldwin #endif
81f3215338SJohn Baldwin 
82f3215338SJohn Baldwin #endif
83f3215338SJohn Baldwin 
847a2ac24cSPeter Dufault /*
850f606585SJohn Dyson  * Private members for aiocb -- don't access
860f606585SJohn Dyson  * directly.
870f606585SJohn Dyson  */
880f606585SJohn Dyson struct __aiocb_private {
8996b8882aSAlan Cox 	long	status;
9096b8882aSAlan Cox 	long	error;
910f606585SJohn Dyson 	void	*kernelinfo;
920f606585SJohn Dyson };
930f606585SJohn Dyson 
940f606585SJohn Dyson /*
950f606585SJohn Dyson  * I/O control block
960f606585SJohn Dyson  */
970f606585SJohn Dyson typedef struct aiocb {
980f606585SJohn Dyson 	int	aio_fildes;		/* File descriptor */
990f606585SJohn Dyson 	off_t	aio_offset;		/* File offset for I/O */
10078922e41SJohn Dyson 	volatile void *aio_buf;		/* I/O buffer in process space */
1010f606585SJohn Dyson 	size_t	aio_nbytes;		/* Number of bytes for I/O */
102c4592cbcSJohn Baldwin 	int	__spare__[2];
103c4592cbcSJohn Baldwin 	void	*__spare2__;
1040f606585SJohn Dyson 	int	aio_lio_opcode;		/* LIO opcode */
1050f606585SJohn Dyson 	int	aio_reqprio;		/* Request priority -- ignored */
1060f606585SJohn Dyson 	struct	__aiocb_private	_aiocb_private;
1070972628aSDavid Xu 	struct	sigevent aio_sigevent;	/* Signal to deliver */
1080f606585SJohn Dyson } aiocb_t;
1090f606585SJohn Dyson 
110022ca2fcSAlan Somers #define	aio_iov	aio_buf			/* I/O scatter/gather list */
111022ca2fcSAlan Somers #define	aio_iovcnt	aio_nbytes	/* Length of aio_iov */
112022ca2fcSAlan Somers 
113f3215338SJohn Baldwin #ifdef _KERNEL
114f3215338SJohn Baldwin 
115f3215338SJohn Baldwin typedef void aio_cancel_fn_t(struct kaiocb *);
116f3215338SJohn Baldwin typedef void aio_handle_fn_t(struct kaiocb *);
117f3215338SJohn Baldwin 
118f3215338SJohn Baldwin /*
119f3215338SJohn Baldwin  * Kernel version of an I/O control block.
120f3215338SJohn Baldwin  *
121f3215338SJohn Baldwin  * Locking key:
122f3215338SJohn Baldwin  * * - need not protected
123f3215338SJohn Baldwin  * a - locked by kaioinfo lock
124f3215338SJohn Baldwin  * b - locked by backend lock
125f3215338SJohn Baldwin  * c - locked by aio_job_mtx
126f3215338SJohn Baldwin  */
127f3215338SJohn Baldwin struct kaiocb {
128f3215338SJohn Baldwin 	TAILQ_ENTRY(kaiocb) list;	/* (b) backend-specific list of jobs */
129f3215338SJohn Baldwin 	TAILQ_ENTRY(kaiocb) plist;	/* (a) lists of pending / done jobs */
130f3215338SJohn Baldwin 	TAILQ_ENTRY(kaiocb) allist;	/* (a) list of all jobs in proc */
131f3215338SJohn Baldwin 	int	jobflags;		/* (a) job flags */
132b1012d80SJohn Baldwin 	int	inblock;		/* (*) input blocks */
133b1012d80SJohn Baldwin 	int	outblock;		/* (*) output blocks */
134b1012d80SJohn Baldwin 	int	msgsnd;			/* (*) messages sent */
135b1012d80SJohn Baldwin 	int	msgrcv;			/* (*) messages received */
136f3215338SJohn Baldwin 	struct	proc *userproc;		/* (*) user process */
137f3215338SJohn Baldwin 	struct	ucred *cred;		/* (*) active credential when created */
138f3215338SJohn Baldwin 	struct	file *fd_file;		/* (*) pointer to file structure */
139f3215338SJohn Baldwin 	struct	aioliojob *lio;		/* (*) optional lio job */
140f3215338SJohn Baldwin 	struct	aiocb *ujob;		/* (*) pointer in userspace of aiocb */
141f3215338SJohn Baldwin 	struct	knlist klist;		/* (a) list of knotes */
142f3215338SJohn Baldwin 	struct	aiocb uaiocb;		/* (*) copy of user I/O control block */
143022ca2fcSAlan Somers 	struct	uio uio;		/* (*) storage for non-vectored uio */
144022ca2fcSAlan Somers 	struct	iovec iov[1];		/* (*) storage for non-vectored uio */
145022ca2fcSAlan Somers 	struct	uio *uiop;		/* (*) Possibly malloced uio */
146f3215338SJohn Baldwin 	ksiginfo_t ksi;			/* (a) realtime signal info */
147f3215338SJohn Baldwin 	uint64_t seqno;			/* (*) job number */
148f3215338SJohn Baldwin 	aio_cancel_fn_t *cancel_fn;	/* (a) backend cancel function */
149f3215338SJohn Baldwin 	aio_handle_fn_t *handle_fn;	/* (c) backend handle function */
150fe0bdd1dSJohn Baldwin 	union {				/* Backend-specific data fields */
151022ca2fcSAlan Somers 		struct {		/* BIO backend */
152022ca2fcSAlan Somers 			int	nbio;	/* Number of remaining bios */
153022ca2fcSAlan Somers 			int	error;	/* Worst error of all bios */
154022ca2fcSAlan Somers 			long	nbytes;	/* Bytes completed so far */
155022ca2fcSAlan Somers 		};
156fe0bdd1dSJohn Baldwin 		struct {		/* fsync() requests */
157fe0bdd1dSJohn Baldwin 			int	pending; /* (a) number of pending I/O */
158fe0bdd1dSJohn Baldwin 		};
15901206038SAlan Somers 		struct {		/* socket backend */
160fe0bdd1dSJohn Baldwin 			void	*backend1;
161fe0bdd1dSJohn Baldwin 			long	backend3;
162fe0bdd1dSJohn Baldwin 			int	backend4;
163fe0bdd1dSJohn Baldwin 		};
164fe0bdd1dSJohn Baldwin 	};
165f3215338SJohn Baldwin };
166f3215338SJohn Baldwin 
167f3215338SJohn Baldwin struct socket;
168f3215338SJohn Baldwin struct sockbuf;
169f3215338SJohn Baldwin 
170f3215338SJohn Baldwin /*
171f3215338SJohn Baldwin  * AIO backends should permit cancellation of queued requests waiting to
172f3215338SJohn Baldwin  * be serviced by installing a cancel routine while the request is
173f3215338SJohn Baldwin  * queued.  The cancellation routine should dequeue the request if
174f3215338SJohn Baldwin  * necessary and cancel it.  Care must be used to handle races between
175f3215338SJohn Baldwin  * queueing and dequeueing requests and cancellation.
176f3215338SJohn Baldwin  *
177f3215338SJohn Baldwin  * When queueing a request somewhere such that it can be cancelled, the
178f3215338SJohn Baldwin  * caller should:
179f3215338SJohn Baldwin  *
180f3215338SJohn Baldwin  *  1) Acquire lock that protects the associated queue.
181f3215338SJohn Baldwin  *  2) Call aio_set_cancel_function() to install the cancel routine.
182f3215338SJohn Baldwin  *  3) If that fails, the request has a pending cancel and should be
183f3215338SJohn Baldwin  *     cancelled via aio_cancel().
184f3215338SJohn Baldwin  *  4) Queue the request.
185f3215338SJohn Baldwin  *
186f3215338SJohn Baldwin  * When dequeueing a request to service it or hand it off to somewhere else,
187f3215338SJohn Baldwin  * the caller should:
188f3215338SJohn Baldwin  *
189f3215338SJohn Baldwin  *  1) Acquire the lock that protects the associated queue.
190f3215338SJohn Baldwin  *  2) Dequeue the request.
191f3215338SJohn Baldwin  *  3) Call aio_clear_cancel_function() to clear the cancel routine.
192f3215338SJohn Baldwin  *  4) If that fails, the cancel routine is about to be called.  The
193f3215338SJohn Baldwin  *     caller should ignore the request.
194f3215338SJohn Baldwin  *
195f3215338SJohn Baldwin  * The cancel routine should:
196f3215338SJohn Baldwin  *
197f3215338SJohn Baldwin  *  1) Acquire the lock that protects the associated queue.
198f3215338SJohn Baldwin  *  2) Call aio_cancel_cleared() to determine if the request is already
199f3215338SJohn Baldwin  *     dequeued due to a race with dequeueing thread.
200f3215338SJohn Baldwin  *  3) If that fails, dequeue the request.
201f3215338SJohn Baldwin  *  4) Cancel the request via aio_cancel().
202f3215338SJohn Baldwin  */
203f3215338SJohn Baldwin 
204f3215338SJohn Baldwin bool	aio_cancel_cleared(struct kaiocb *job);
205f3215338SJohn Baldwin void	aio_cancel(struct kaiocb *job);
206f3215338SJohn Baldwin bool	aio_clear_cancel_function(struct kaiocb *job);
207f3215338SJohn Baldwin void	aio_complete(struct kaiocb *job, long status, int error);
208f3215338SJohn Baldwin void	aio_schedule(struct kaiocb *job, aio_handle_fn_t *func);
209f3215338SJohn Baldwin bool	aio_set_cancel_function(struct kaiocb *job, aio_cancel_fn_t *func);
210f3215338SJohn Baldwin void	aio_switch_vmspace(struct kaiocb *job);
211f3215338SJohn Baldwin 
212f3215338SJohn Baldwin #else /* !_KERNEL */
213f0231545SDmitrij Tejblum 
21434d3ac59SDavid Schultz struct timespec;
21534d3ac59SDavid Schultz 
216f0231545SDmitrij Tejblum __BEGIN_DECLS
2170f606585SJohn Dyson /*
2180f606585SJohn Dyson  * Asynchronously read from a file
2190f606585SJohn Dyson  */
220f0231545SDmitrij Tejblum int	aio_read(struct aiocb *);
221022ca2fcSAlan Somers #if __BSD_VISIBLE
222022ca2fcSAlan Somers int	aio_readv(struct aiocb *);
223022ca2fcSAlan Somers #endif
2240f606585SJohn Dyson 
2250f606585SJohn Dyson /*
2260f606585SJohn Dyson  * Asynchronously write to file
2270f606585SJohn Dyson  */
228f0231545SDmitrij Tejblum int	aio_write(struct aiocb *);
229022ca2fcSAlan Somers #if __BSD_VISIBLE
230022ca2fcSAlan Somers int	aio_writev(struct aiocb *);
231022ca2fcSAlan Somers #endif
2320f606585SJohn Dyson 
2330f606585SJohn Dyson /*
2340f606585SJohn Dyson  * List I/O Asynchronously/synchronously read/write to/from file
2350f606585SJohn Dyson  *	"lio_mode" specifies whether or not the I/O is synchronous.
2360f606585SJohn Dyson  *	"acb_list" is an array of "nacb_listent" I/O control blocks.
2370f606585SJohn Dyson  *	when all I/Os are complete, the optional signal "sig" is sent.
2380f606585SJohn Dyson  */
239049342a9SEd Schouten int	lio_listio(int, struct aiocb *__restrict const *__restrict, int,
240049342a9SEd Schouten     struct sigevent *);
2410f606585SJohn Dyson 
2420f606585SJohn Dyson /*
2430f606585SJohn Dyson  * Get completion status
2440f606585SJohn Dyson  *	returns EINPROGRESS until I/O is complete.
2450f606585SJohn Dyson  *	this routine does not block.
2460f606585SJohn Dyson  */
247f0231545SDmitrij Tejblum int	aio_error(const struct aiocb *);
2480f606585SJohn Dyson 
2490f606585SJohn Dyson /*
2500f606585SJohn Dyson  * Finish up I/O, releasing I/O resources and returns the value
2510f606585SJohn Dyson  *	that would have been associated with a synchronous I/O request.
2520f606585SJohn Dyson  *	This routine must be called once and only once for each
2530f606585SJohn Dyson  *	I/O control block who has had I/O associated with it.
2540f606585SJohn Dyson  */
255f0231545SDmitrij Tejblum ssize_t	aio_return(struct aiocb *);
2560f606585SJohn Dyson 
2570f606585SJohn Dyson /*
2584270aed7SAlan Cox  * Cancel I/O
2590f606585SJohn Dyson  */
260f0231545SDmitrij Tejblum int	aio_cancel(int, struct aiocb *);
2610f606585SJohn Dyson 
2620f606585SJohn Dyson /*
2630f606585SJohn Dyson  * Suspend until all specified I/O or timeout is complete.
2640f606585SJohn Dyson  */
265f0231545SDmitrij Tejblum int	aio_suspend(const struct aiocb * const[], int, const struct timespec *);
2660f606585SJohn Dyson 
2676160e12cSGleb Smirnoff /*
2686160e12cSGleb Smirnoff  * Asynchronous mlock
2696160e12cSGleb Smirnoff  */
2706160e12cSGleb Smirnoff int	aio_mlock(struct aiocb *);
2716160e12cSGleb Smirnoff 
272b8db1c9fSJohn Baldwin #if __BSD_VISIBLE
273bb430bc7SJohn Baldwin ssize_t	aio_waitcomplete(struct aiocb **, struct timespec *);
27434d3ac59SDavid Schultz #endif
275bfbbc4aaSJason Evans 
27653fcc63cSDavid Xu int	aio_fsync(int op, struct aiocb *aiocbp);
277f0231545SDmitrij Tejblum __END_DECLS
27871af8fbaSJohn Dyson 
279f3215338SJohn Baldwin #endif /* !_KERNEL */
2805aaef07cSJohn Dyson 
281f3215338SJohn Baldwin #endif /* !_SYS_AIO_H_ */
282