xref: /freebsd/sys/kern/vfs_aio.c (revision b1d046441de9053152c7cf03d6b60d9882687e1b)
1 /*-
2  * Copyright (c) 1997 John S. Dyson.  All rights reserved.
3  *
4  * Redistribution and use in source and binary forms, with or without
5  * modification, are permitted provided that the following conditions
6  * are met:
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.
11  *
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.
15  */
16 
17 /*
18  * This file contains support for the POSIX 1003.1B AIO/LIO facility.
19  */
20 
21 #include <sys/cdefs.h>
22 __FBSDID("$FreeBSD$");
23 
24 #include "opt_compat.h"
25 
26 #include <sys/param.h>
27 #include <sys/systm.h>
28 #include <sys/malloc.h>
29 #include <sys/bio.h>
30 #include <sys/buf.h>
31 #include <sys/capability.h>
32 #include <sys/eventhandler.h>
33 #include <sys/sysproto.h>
34 #include <sys/filedesc.h>
35 #include <sys/kernel.h>
36 #include <sys/module.h>
37 #include <sys/kthread.h>
38 #include <sys/fcntl.h>
39 #include <sys/file.h>
40 #include <sys/limits.h>
41 #include <sys/lock.h>
42 #include <sys/mutex.h>
43 #include <sys/unistd.h>
44 #include <sys/posix4.h>
45 #include <sys/proc.h>
46 #include <sys/resourcevar.h>
47 #include <sys/signalvar.h>
48 #include <sys/protosw.h>
49 #include <sys/sema.h>
50 #include <sys/socket.h>
51 #include <sys/socketvar.h>
52 #include <sys/syscall.h>
53 #include <sys/sysent.h>
54 #include <sys/sysctl.h>
55 #include <sys/sx.h>
56 #include <sys/taskqueue.h>
57 #include <sys/vnode.h>
58 #include <sys/conf.h>
59 #include <sys/event.h>
60 #include <sys/mount.h>
61 
62 #include <machine/atomic.h>
63 
64 #include <vm/vm.h>
65 #include <vm/vm_extern.h>
66 #include <vm/pmap.h>
67 #include <vm/vm_map.h>
68 #include <vm/vm_object.h>
69 #include <vm/uma.h>
70 #include <sys/aio.h>
71 
72 #include "opt_vfs_aio.h"
73 
74 /*
75  * Counter for allocating reference ids to new jobs.  Wrapped to 1 on
76  * overflow. (XXX will be removed soon.)
77  */
78 static u_long jobrefid;
79 
80 /*
81  * Counter for aio_fsync.
82  */
83 static uint64_t jobseqno;
84 
85 #define JOBST_NULL		0
86 #define JOBST_JOBQSOCK		1
87 #define JOBST_JOBQGLOBAL	2
88 #define JOBST_JOBRUNNING	3
89 #define JOBST_JOBFINISHED	4
90 #define JOBST_JOBQBUF		5
91 #define JOBST_JOBQSYNC		6
92 
93 #ifndef MAX_AIO_PER_PROC
94 #define MAX_AIO_PER_PROC	32
95 #endif
96 
97 #ifndef MAX_AIO_QUEUE_PER_PROC
98 #define MAX_AIO_QUEUE_PER_PROC	256 /* Bigger than AIO_LISTIO_MAX */
99 #endif
100 
101 #ifndef MAX_AIO_PROCS
102 #define MAX_AIO_PROCS		32
103 #endif
104 
105 #ifndef MAX_AIO_QUEUE
106 #define	MAX_AIO_QUEUE		1024 /* Bigger than AIO_LISTIO_MAX */
107 #endif
108 
109 #ifndef TARGET_AIO_PROCS
110 #define TARGET_AIO_PROCS	4
111 #endif
112 
113 #ifndef MAX_BUF_AIO
114 #define MAX_BUF_AIO		16
115 #endif
116 
117 #ifndef AIOD_TIMEOUT_DEFAULT
118 #define	AIOD_TIMEOUT_DEFAULT	(10 * hz)
119 #endif
120 
121 #ifndef AIOD_LIFETIME_DEFAULT
122 #define AIOD_LIFETIME_DEFAULT	(30 * hz)
123 #endif
124 
125 FEATURE(aio, "Asynchronous I/O");
126 
127 static MALLOC_DEFINE(M_LIO, "lio", "listio aio control block list");
128 
129 static SYSCTL_NODE(_vfs, OID_AUTO, aio, CTLFLAG_RW, 0, "Async IO management");
130 
131 static int max_aio_procs = MAX_AIO_PROCS;
132 SYSCTL_INT(_vfs_aio, OID_AUTO, max_aio_procs,
133 	CTLFLAG_RW, &max_aio_procs, 0,
134 	"Maximum number of kernel threads to use for handling async IO ");
135 
136 static int num_aio_procs = 0;
137 SYSCTL_INT(_vfs_aio, OID_AUTO, num_aio_procs,
138 	CTLFLAG_RD, &num_aio_procs, 0,
139 	"Number of presently active kernel threads for async IO");
140 
141 /*
142  * The code will adjust the actual number of AIO processes towards this
143  * number when it gets a chance.
144  */
145 static int target_aio_procs = TARGET_AIO_PROCS;
146 SYSCTL_INT(_vfs_aio, OID_AUTO, target_aio_procs, CTLFLAG_RW, &target_aio_procs,
147 	0, "Preferred number of ready kernel threads for async IO");
148 
149 static int max_queue_count = MAX_AIO_QUEUE;
150 SYSCTL_INT(_vfs_aio, OID_AUTO, max_aio_queue, CTLFLAG_RW, &max_queue_count, 0,
151     "Maximum number of aio requests to queue, globally");
152 
153 static int num_queue_count = 0;
154 SYSCTL_INT(_vfs_aio, OID_AUTO, num_queue_count, CTLFLAG_RD, &num_queue_count, 0,
155     "Number of queued aio requests");
156 
157 static int num_buf_aio = 0;
158 SYSCTL_INT(_vfs_aio, OID_AUTO, num_buf_aio, CTLFLAG_RD, &num_buf_aio, 0,
159     "Number of aio requests presently handled by the buf subsystem");
160 
161 /* Number of async I/O thread in the process of being started */
162 /* XXX This should be local to aio_aqueue() */
163 static int num_aio_resv_start = 0;
164 
165 static int aiod_timeout;
166 SYSCTL_INT(_vfs_aio, OID_AUTO, aiod_timeout, CTLFLAG_RW, &aiod_timeout, 0,
167     "Timeout value for synchronous aio operations");
168 
169 static int aiod_lifetime;
170 SYSCTL_INT(_vfs_aio, OID_AUTO, aiod_lifetime, CTLFLAG_RW, &aiod_lifetime, 0,
171     "Maximum lifetime for idle aiod");
172 
173 static int unloadable = 0;
174 SYSCTL_INT(_vfs_aio, OID_AUTO, unloadable, CTLFLAG_RW, &unloadable, 0,
175     "Allow unload of aio (not recommended)");
176 
177 
178 static int max_aio_per_proc = MAX_AIO_PER_PROC;
179 SYSCTL_INT(_vfs_aio, OID_AUTO, max_aio_per_proc, CTLFLAG_RW, &max_aio_per_proc,
180     0, "Maximum active aio requests per process (stored in the process)");
181 
182 static int max_aio_queue_per_proc = MAX_AIO_QUEUE_PER_PROC;
183 SYSCTL_INT(_vfs_aio, OID_AUTO, max_aio_queue_per_proc, CTLFLAG_RW,
184     &max_aio_queue_per_proc, 0,
185     "Maximum queued aio requests per process (stored in the process)");
186 
187 static int max_buf_aio = MAX_BUF_AIO;
188 SYSCTL_INT(_vfs_aio, OID_AUTO, max_buf_aio, CTLFLAG_RW, &max_buf_aio, 0,
189     "Maximum buf aio requests per process (stored in the process)");
190 
191 typedef struct oaiocb {
192 	int	aio_fildes;		/* File descriptor */
193 	off_t	aio_offset;		/* File offset for I/O */
194 	volatile void *aio_buf;         /* I/O buffer in process space */
195 	size_t	aio_nbytes;		/* Number of bytes for I/O */
196 	struct	osigevent aio_sigevent;	/* Signal to deliver */
197 	int	aio_lio_opcode;		/* LIO opcode */
198 	int	aio_reqprio;		/* Request priority -- ignored */
199 	struct	__aiocb_private	_aiocb_private;
200 } oaiocb_t;
201 
202 /*
203  * Below is a key of locks used to protect each member of struct aiocblist
204  * aioliojob and kaioinfo and any backends.
205  *
206  * * - need not protected
207  * a - locked by kaioinfo lock
208  * b - locked by backend lock, the backend lock can be null in some cases,
209  *     for example, BIO belongs to this type, in this case, proc lock is
210  *     reused.
211  * c - locked by aio_job_mtx, the lock for the generic file I/O backend.
212  */
213 
214 /*
215  * Current, there is only two backends: BIO and generic file I/O.
216  * socket I/O is served by generic file I/O, this is not a good idea, since
217  * disk file I/O and any other types without O_NONBLOCK flag can block daemon
218  * threads, if there is no thread to serve socket I/O, the socket I/O will be
219  * delayed too long or starved, we should create some threads dedicated to
220  * sockets to do non-blocking I/O, same for pipe and fifo, for these I/O
221  * systems we really need non-blocking interface, fiddling O_NONBLOCK in file
222  * structure is not safe because there is race between userland and aio
223  * daemons.
224  */
225 
226 struct aiocblist {
227 	TAILQ_ENTRY(aiocblist) list;	/* (b) internal list of for backend */
228 	TAILQ_ENTRY(aiocblist) plist;	/* (a) list of jobs for each backend */
229 	TAILQ_ENTRY(aiocblist) allist;  /* (a) list of all jobs in proc */
230 	int	jobflags;		/* (a) job flags */
231 	int	jobstate;		/* (b) job state */
232 	int	inputcharge;		/* (*) input blockes */
233 	int	outputcharge;		/* (*) output blockes */
234 	struct	buf *bp;		/* (*) private to BIO backend,
235 				  	 * buffer pointer
236 					 */
237 	struct	proc *userproc;		/* (*) user process */
238 	struct  ucred *cred;		/* (*) active credential when created */
239 	struct	file *fd_file;		/* (*) pointer to file structure */
240 	struct	aioliojob *lio;		/* (*) optional lio job */
241 	struct	aiocb *uuaiocb;		/* (*) pointer in userspace of aiocb */
242 	struct	knlist klist;		/* (a) list of knotes */
243 	struct	aiocb uaiocb;		/* (*) kernel I/O control block */
244 	ksiginfo_t ksi;			/* (a) realtime signal info */
245 	struct	task biotask;		/* (*) private to BIO backend */
246 	uint64_t seqno;			/* (*) job number */
247 	int	pending;		/* (a) number of pending I/O, aio_fsync only */
248 };
249 
250 /* jobflags */
251 #define AIOCBLIST_DONE		0x01
252 #define AIOCBLIST_BUFDONE	0x02
253 #define AIOCBLIST_RUNDOWN	0x04
254 #define AIOCBLIST_CHECKSYNC	0x08
255 
256 /*
257  * AIO process info
258  */
259 #define AIOP_FREE	0x1			/* proc on free queue */
260 
261 struct aiothreadlist {
262 	int aiothreadflags;			/* (c) AIO proc flags */
263 	TAILQ_ENTRY(aiothreadlist) list;	/* (c) list of processes */
264 	struct thread *aiothread;		/* (*) the AIO thread */
265 };
266 
267 /*
268  * data-structure for lio signal management
269  */
270 struct aioliojob {
271 	int	lioj_flags;			/* (a) listio flags */
272 	int	lioj_count;			/* (a) listio flags */
273 	int	lioj_finished_count;		/* (a) listio flags */
274 	struct	sigevent lioj_signal;		/* (a) signal on all I/O done */
275 	TAILQ_ENTRY(aioliojob) lioj_list;	/* (a) lio list */
276 	struct  knlist klist;			/* (a) list of knotes */
277 	ksiginfo_t lioj_ksi;			/* (a) Realtime signal info */
278 };
279 
280 #define	LIOJ_SIGNAL		0x1	/* signal on all done (lio) */
281 #define	LIOJ_SIGNAL_POSTED	0x2	/* signal has been posted */
282 #define LIOJ_KEVENT_POSTED	0x4	/* kevent triggered */
283 
284 /*
285  * per process aio data structure
286  */
287 struct kaioinfo {
288 	struct mtx	kaio_mtx;	/* the lock to protect this struct */
289 	int	kaio_flags;		/* (a) per process kaio flags */
290 	int	kaio_maxactive_count;	/* (*) maximum number of AIOs */
291 	int	kaio_active_count;	/* (c) number of currently used AIOs */
292 	int	kaio_qallowed_count;	/* (*) maxiumu size of AIO queue */
293 	int	kaio_count;		/* (a) size of AIO queue */
294 	int	kaio_ballowed_count;	/* (*) maximum number of buffers */
295 	int	kaio_buffer_count;	/* (a) number of physio buffers */
296 	TAILQ_HEAD(,aiocblist) kaio_all;	/* (a) all AIOs in the process */
297 	TAILQ_HEAD(,aiocblist) kaio_done;	/* (a) done queue for process */
298 	TAILQ_HEAD(,aioliojob) kaio_liojoblist; /* (a) list of lio jobs */
299 	TAILQ_HEAD(,aiocblist) kaio_jobqueue;	/* (a) job queue for process */
300 	TAILQ_HEAD(,aiocblist) kaio_bufqueue;	/* (a) buffer job queue for process */
301 	TAILQ_HEAD(,aiocblist) kaio_sockqueue;  /* (a) queue for aios waiting on sockets,
302 						 *  NOT USED YET.
303 						 */
304 	TAILQ_HEAD(,aiocblist) kaio_syncqueue;	/* (a) queue for aio_fsync */
305 	struct	task	kaio_task;	/* (*) task to kick aio threads */
306 };
307 
308 #define AIO_LOCK(ki)		mtx_lock(&(ki)->kaio_mtx)
309 #define AIO_UNLOCK(ki)		mtx_unlock(&(ki)->kaio_mtx)
310 #define AIO_LOCK_ASSERT(ki, f)	mtx_assert(&(ki)->kaio_mtx, (f))
311 #define AIO_MTX(ki)		(&(ki)->kaio_mtx)
312 
313 #define KAIO_RUNDOWN	0x1	/* process is being run down */
314 #define KAIO_WAKEUP	0x2	/* wakeup process when there is a significant event */
315 
316 /*
317  * Operations used to interact with userland aio control blocks.
318  * Different ABIs provide their own operations.
319  */
320 struct aiocb_ops {
321 	int	(*copyin)(struct aiocb *ujob, struct aiocb *kjob);
322 	long	(*fetch_status)(struct aiocb *ujob);
323 	long	(*fetch_error)(struct aiocb *ujob);
324 	int	(*store_status)(struct aiocb *ujob, long status);
325 	int	(*store_error)(struct aiocb *ujob, long error);
326 	int	(*store_kernelinfo)(struct aiocb *ujob, long jobref);
327 	int	(*store_aiocb)(struct aiocb **ujobp, struct aiocb *ujob);
328 };
329 
330 static TAILQ_HEAD(,aiothreadlist) aio_freeproc;		/* (c) Idle daemons */
331 static struct sema aio_newproc_sem;
332 static struct mtx aio_job_mtx;
333 static struct mtx aio_sock_mtx;
334 static TAILQ_HEAD(,aiocblist) aio_jobs;			/* (c) Async job list */
335 static struct unrhdr *aiod_unr;
336 
337 void		aio_init_aioinfo(struct proc *p);
338 static int	aio_onceonly(void);
339 static int	aio_free_entry(struct aiocblist *aiocbe);
340 static void	aio_process(struct aiocblist *aiocbe);
341 static int	aio_newproc(int *);
342 int		aio_aqueue(struct thread *td, struct aiocb *job,
343 			struct aioliojob *lio, int type, struct aiocb_ops *ops);
344 static void	aio_physwakeup(struct buf *bp);
345 static void	aio_proc_rundown(void *arg, struct proc *p);
346 static void	aio_proc_rundown_exec(void *arg, struct proc *p, struct image_params *imgp);
347 static int	aio_qphysio(struct proc *p, struct aiocblist *iocb);
348 static void	biohelper(void *, int);
349 static void	aio_daemon(void *param);
350 static void	aio_swake_cb(struct socket *, struct sockbuf *);
351 static int	aio_unload(void);
352 static void	aio_bio_done_notify(struct proc *userp, struct aiocblist *aiocbe, int type);
353 #define DONE_BUF	1
354 #define DONE_QUEUE	2
355 static int	aio_kick(struct proc *userp);
356 static void	aio_kick_nowait(struct proc *userp);
357 static void	aio_kick_helper(void *context, int pending);
358 static int	filt_aioattach(struct knote *kn);
359 static void	filt_aiodetach(struct knote *kn);
360 static int	filt_aio(struct knote *kn, long hint);
361 static int	filt_lioattach(struct knote *kn);
362 static void	filt_liodetach(struct knote *kn);
363 static int	filt_lio(struct knote *kn, long hint);
364 
365 /*
366  * Zones for:
367  * 	kaio	Per process async io info
368  *	aiop	async io thread data
369  *	aiocb	async io jobs
370  *	aiol	list io job pointer - internal to aio_suspend XXX
371  *	aiolio	list io jobs
372  */
373 static uma_zone_t kaio_zone, aiop_zone, aiocb_zone, aiol_zone, aiolio_zone;
374 
375 /* kqueue filters for aio */
376 static struct filterops aio_filtops = {
377 	.f_isfd = 0,
378 	.f_attach = filt_aioattach,
379 	.f_detach = filt_aiodetach,
380 	.f_event = filt_aio,
381 };
382 static struct filterops lio_filtops = {
383 	.f_isfd = 0,
384 	.f_attach = filt_lioattach,
385 	.f_detach = filt_liodetach,
386 	.f_event = filt_lio
387 };
388 
389 static eventhandler_tag exit_tag, exec_tag;
390 
391 TASKQUEUE_DEFINE_THREAD(aiod_bio);
392 
393 /*
394  * Main operations function for use as a kernel module.
395  */
396 static int
397 aio_modload(struct module *module, int cmd, void *arg)
398 {
399 	int error = 0;
400 
401 	switch (cmd) {
402 	case MOD_LOAD:
403 		aio_onceonly();
404 		break;
405 	case MOD_UNLOAD:
406 		error = aio_unload();
407 		break;
408 	case MOD_SHUTDOWN:
409 		break;
410 	default:
411 		error = EINVAL;
412 		break;
413 	}
414 	return (error);
415 }
416 
417 static moduledata_t aio_mod = {
418 	"aio",
419 	&aio_modload,
420 	NULL
421 };
422 
423 static struct syscall_helper_data aio_syscalls[] = {
424 	SYSCALL_INIT_HELPER(aio_cancel),
425 	SYSCALL_INIT_HELPER(aio_error),
426 	SYSCALL_INIT_HELPER(aio_fsync),
427 	SYSCALL_INIT_HELPER(aio_read),
428 	SYSCALL_INIT_HELPER(aio_return),
429 	SYSCALL_INIT_HELPER(aio_suspend),
430 	SYSCALL_INIT_HELPER(aio_waitcomplete),
431 	SYSCALL_INIT_HELPER(aio_write),
432 	SYSCALL_INIT_HELPER(lio_listio),
433 	SYSCALL_INIT_HELPER(oaio_read),
434 	SYSCALL_INIT_HELPER(oaio_write),
435 	SYSCALL_INIT_HELPER(olio_listio),
436 	SYSCALL_INIT_LAST
437 };
438 
439 #ifdef COMPAT_FREEBSD32
440 #include <sys/mount.h>
441 #include <sys/socket.h>
442 #include <compat/freebsd32/freebsd32.h>
443 #include <compat/freebsd32/freebsd32_proto.h>
444 #include <compat/freebsd32/freebsd32_signal.h>
445 #include <compat/freebsd32/freebsd32_syscall.h>
446 #include <compat/freebsd32/freebsd32_util.h>
447 
448 static struct syscall_helper_data aio32_syscalls[] = {
449 	SYSCALL32_INIT_HELPER(freebsd32_aio_return),
450 	SYSCALL32_INIT_HELPER(freebsd32_aio_suspend),
451 	SYSCALL32_INIT_HELPER(freebsd32_aio_cancel),
452 	SYSCALL32_INIT_HELPER(freebsd32_aio_error),
453 	SYSCALL32_INIT_HELPER(freebsd32_aio_fsync),
454 	SYSCALL32_INIT_HELPER(freebsd32_aio_read),
455 	SYSCALL32_INIT_HELPER(freebsd32_aio_write),
456 	SYSCALL32_INIT_HELPER(freebsd32_aio_waitcomplete),
457 	SYSCALL32_INIT_HELPER(freebsd32_lio_listio),
458 	SYSCALL32_INIT_HELPER(freebsd32_oaio_read),
459 	SYSCALL32_INIT_HELPER(freebsd32_oaio_write),
460 	SYSCALL32_INIT_HELPER(freebsd32_olio_listio),
461 	SYSCALL_INIT_LAST
462 };
463 #endif
464 
465 DECLARE_MODULE(aio, aio_mod,
466 	SI_SUB_VFS, SI_ORDER_ANY);
467 MODULE_VERSION(aio, 1);
468 
469 /*
470  * Startup initialization
471  */
472 static int
473 aio_onceonly(void)
474 {
475 	int error;
476 
477 	/* XXX: should probably just use so->callback */
478 	aio_swake = &aio_swake_cb;
479 	exit_tag = EVENTHANDLER_REGISTER(process_exit, aio_proc_rundown, NULL,
480 	    EVENTHANDLER_PRI_ANY);
481 	exec_tag = EVENTHANDLER_REGISTER(process_exec, aio_proc_rundown_exec, NULL,
482 	    EVENTHANDLER_PRI_ANY);
483 	kqueue_add_filteropts(EVFILT_AIO, &aio_filtops);
484 	kqueue_add_filteropts(EVFILT_LIO, &lio_filtops);
485 	TAILQ_INIT(&aio_freeproc);
486 	sema_init(&aio_newproc_sem, 0, "aio_new_proc");
487 	mtx_init(&aio_job_mtx, "aio_job", NULL, MTX_DEF);
488 	mtx_init(&aio_sock_mtx, "aio_sock", NULL, MTX_DEF);
489 	TAILQ_INIT(&aio_jobs);
490 	aiod_unr = new_unrhdr(1, INT_MAX, NULL);
491 	kaio_zone = uma_zcreate("AIO", sizeof(struct kaioinfo), NULL, NULL,
492 	    NULL, NULL, UMA_ALIGN_PTR, UMA_ZONE_NOFREE);
493 	aiop_zone = uma_zcreate("AIOP", sizeof(struct aiothreadlist), NULL,
494 	    NULL, NULL, NULL, UMA_ALIGN_PTR, UMA_ZONE_NOFREE);
495 	aiocb_zone = uma_zcreate("AIOCB", sizeof(struct aiocblist), NULL, NULL,
496 	    NULL, NULL, UMA_ALIGN_PTR, UMA_ZONE_NOFREE);
497 	aiol_zone = uma_zcreate("AIOL", AIO_LISTIO_MAX*sizeof(intptr_t) , NULL,
498 	    NULL, NULL, NULL, UMA_ALIGN_PTR, UMA_ZONE_NOFREE);
499 	aiolio_zone = uma_zcreate("AIOLIO", sizeof(struct aioliojob), NULL,
500 	    NULL, NULL, NULL, UMA_ALIGN_PTR, UMA_ZONE_NOFREE);
501 	aiod_timeout = AIOD_TIMEOUT_DEFAULT;
502 	aiod_lifetime = AIOD_LIFETIME_DEFAULT;
503 	jobrefid = 1;
504 	async_io_version = _POSIX_VERSION;
505 	p31b_setcfg(CTL_P1003_1B_AIO_LISTIO_MAX, AIO_LISTIO_MAX);
506 	p31b_setcfg(CTL_P1003_1B_AIO_MAX, MAX_AIO_QUEUE);
507 	p31b_setcfg(CTL_P1003_1B_AIO_PRIO_DELTA_MAX, 0);
508 
509 	error = syscall_helper_register(aio_syscalls);
510 	if (error)
511 		return (error);
512 #ifdef COMPAT_FREEBSD32
513 	error = syscall32_helper_register(aio32_syscalls);
514 	if (error)
515 		return (error);
516 #endif
517 	return (0);
518 }
519 
520 /*
521  * Callback for unload of AIO when used as a module.
522  */
523 static int
524 aio_unload(void)
525 {
526 	int error;
527 
528 	/*
529 	 * XXX: no unloads by default, it's too dangerous.
530 	 * perhaps we could do it if locked out callers and then
531 	 * did an aio_proc_rundown() on each process.
532 	 *
533 	 * jhb: aio_proc_rundown() needs to run on curproc though,
534 	 * so I don't think that would fly.
535 	 */
536 	if (!unloadable)
537 		return (EOPNOTSUPP);
538 
539 #ifdef COMPAT_FREEBSD32
540 	syscall32_helper_unregister(aio32_syscalls);
541 #endif
542 	syscall_helper_unregister(aio_syscalls);
543 
544 	error = kqueue_del_filteropts(EVFILT_AIO);
545 	if (error)
546 		return error;
547 	error = kqueue_del_filteropts(EVFILT_LIO);
548 	if (error)
549 		return error;
550 	async_io_version = 0;
551 	aio_swake = NULL;
552 	taskqueue_free(taskqueue_aiod_bio);
553 	delete_unrhdr(aiod_unr);
554 	uma_zdestroy(kaio_zone);
555 	uma_zdestroy(aiop_zone);
556 	uma_zdestroy(aiocb_zone);
557 	uma_zdestroy(aiol_zone);
558 	uma_zdestroy(aiolio_zone);
559 	EVENTHANDLER_DEREGISTER(process_exit, exit_tag);
560 	EVENTHANDLER_DEREGISTER(process_exec, exec_tag);
561 	mtx_destroy(&aio_job_mtx);
562 	mtx_destroy(&aio_sock_mtx);
563 	sema_destroy(&aio_newproc_sem);
564 	p31b_setcfg(CTL_P1003_1B_AIO_LISTIO_MAX, -1);
565 	p31b_setcfg(CTL_P1003_1B_AIO_MAX, -1);
566 	p31b_setcfg(CTL_P1003_1B_AIO_PRIO_DELTA_MAX, -1);
567 	return (0);
568 }
569 
570 /*
571  * Init the per-process aioinfo structure.  The aioinfo limits are set
572  * per-process for user limit (resource) management.
573  */
574 void
575 aio_init_aioinfo(struct proc *p)
576 {
577 	struct kaioinfo *ki;
578 
579 	ki = uma_zalloc(kaio_zone, M_WAITOK);
580 	mtx_init(&ki->kaio_mtx, "aiomtx", NULL, MTX_DEF);
581 	ki->kaio_flags = 0;
582 	ki->kaio_maxactive_count = max_aio_per_proc;
583 	ki->kaio_active_count = 0;
584 	ki->kaio_qallowed_count = max_aio_queue_per_proc;
585 	ki->kaio_count = 0;
586 	ki->kaio_ballowed_count = max_buf_aio;
587 	ki->kaio_buffer_count = 0;
588 	TAILQ_INIT(&ki->kaio_all);
589 	TAILQ_INIT(&ki->kaio_done);
590 	TAILQ_INIT(&ki->kaio_jobqueue);
591 	TAILQ_INIT(&ki->kaio_bufqueue);
592 	TAILQ_INIT(&ki->kaio_liojoblist);
593 	TAILQ_INIT(&ki->kaio_sockqueue);
594 	TAILQ_INIT(&ki->kaio_syncqueue);
595 	TASK_INIT(&ki->kaio_task, 0, aio_kick_helper, p);
596 	PROC_LOCK(p);
597 	if (p->p_aioinfo == NULL) {
598 		p->p_aioinfo = ki;
599 		PROC_UNLOCK(p);
600 	} else {
601 		PROC_UNLOCK(p);
602 		mtx_destroy(&ki->kaio_mtx);
603 		uma_zfree(kaio_zone, ki);
604 	}
605 
606 	while (num_aio_procs < MIN(target_aio_procs, max_aio_procs))
607 		aio_newproc(NULL);
608 }
609 
610 static int
611 aio_sendsig(struct proc *p, struct sigevent *sigev, ksiginfo_t *ksi)
612 {
613 	struct thread *td;
614 	int error;
615 
616 	error = sigev_findtd(p, sigev, &td);
617 	if (error)
618 		return (error);
619 	if (!KSI_ONQ(ksi)) {
620 		ksiginfo_set_sigev(ksi, sigev);
621 		ksi->ksi_code = SI_ASYNCIO;
622 		ksi->ksi_flags |= KSI_EXT | KSI_INS;
623 		tdsendsignal(p, td, ksi->ksi_signo, ksi);
624 	}
625 	PROC_UNLOCK(p);
626 	return (error);
627 }
628 
629 /*
630  * Free a job entry.  Wait for completion if it is currently active, but don't
631  * delay forever.  If we delay, we return a flag that says that we have to
632  * restart the queue scan.
633  */
634 static int
635 aio_free_entry(struct aiocblist *aiocbe)
636 {
637 	struct kaioinfo *ki;
638 	struct aioliojob *lj;
639 	struct proc *p;
640 
641 	p = aiocbe->userproc;
642 	MPASS(curproc == p);
643 	ki = p->p_aioinfo;
644 	MPASS(ki != NULL);
645 
646 	AIO_LOCK_ASSERT(ki, MA_OWNED);
647 	MPASS(aiocbe->jobstate == JOBST_JOBFINISHED);
648 
649 	atomic_subtract_int(&num_queue_count, 1);
650 
651 	ki->kaio_count--;
652 	MPASS(ki->kaio_count >= 0);
653 
654 	TAILQ_REMOVE(&ki->kaio_done, aiocbe, plist);
655 	TAILQ_REMOVE(&ki->kaio_all, aiocbe, allist);
656 
657 	lj = aiocbe->lio;
658 	if (lj) {
659 		lj->lioj_count--;
660 		lj->lioj_finished_count--;
661 
662 		if (lj->lioj_count == 0) {
663 			TAILQ_REMOVE(&ki->kaio_liojoblist, lj, lioj_list);
664 			/* lio is going away, we need to destroy any knotes */
665 			knlist_delete(&lj->klist, curthread, 1);
666 			PROC_LOCK(p);
667 			sigqueue_take(&lj->lioj_ksi);
668 			PROC_UNLOCK(p);
669 			uma_zfree(aiolio_zone, lj);
670 		}
671 	}
672 
673 	/* aiocbe is going away, we need to destroy any knotes */
674 	knlist_delete(&aiocbe->klist, curthread, 1);
675 	PROC_LOCK(p);
676 	sigqueue_take(&aiocbe->ksi);
677 	PROC_UNLOCK(p);
678 
679 	MPASS(aiocbe->bp == NULL);
680 	aiocbe->jobstate = JOBST_NULL;
681 	AIO_UNLOCK(ki);
682 
683 	/*
684 	 * The thread argument here is used to find the owning process
685 	 * and is also passed to fo_close() which may pass it to various
686 	 * places such as devsw close() routines.  Because of that, we
687 	 * need a thread pointer from the process owning the job that is
688 	 * persistent and won't disappear out from under us or move to
689 	 * another process.
690 	 *
691 	 * Currently, all the callers of this function call it to remove
692 	 * an aiocblist from the current process' job list either via a
693 	 * syscall or due to the current process calling exit() or
694 	 * execve().  Thus, we know that p == curproc.  We also know that
695 	 * curthread can't exit since we are curthread.
696 	 *
697 	 * Therefore, we use curthread as the thread to pass to
698 	 * knlist_delete().  This does mean that it is possible for the
699 	 * thread pointer at close time to differ from the thread pointer
700 	 * at open time, but this is already true of file descriptors in
701 	 * a multithreaded process.
702 	 */
703 	fdrop(aiocbe->fd_file, curthread);
704 	crfree(aiocbe->cred);
705 	uma_zfree(aiocb_zone, aiocbe);
706 	AIO_LOCK(ki);
707 
708 	return (0);
709 }
710 
711 static void
712 aio_proc_rundown_exec(void *arg, struct proc *p, struct image_params *imgp __unused)
713 {
714    	aio_proc_rundown(arg, p);
715 }
716 
717 /*
718  * Rundown the jobs for a given process.
719  */
720 static void
721 aio_proc_rundown(void *arg, struct proc *p)
722 {
723 	struct kaioinfo *ki;
724 	struct aioliojob *lj;
725 	struct aiocblist *cbe, *cbn;
726 	struct file *fp;
727 	struct socket *so;
728 	int remove;
729 
730 	KASSERT(curthread->td_proc == p,
731 	    ("%s: called on non-curproc", __func__));
732 	ki = p->p_aioinfo;
733 	if (ki == NULL)
734 		return;
735 
736 	AIO_LOCK(ki);
737 	ki->kaio_flags |= KAIO_RUNDOWN;
738 
739 restart:
740 
741 	/*
742 	 * Try to cancel all pending requests. This code simulates
743 	 * aio_cancel on all pending I/O requests.
744 	 */
745 	TAILQ_FOREACH_SAFE(cbe, &ki->kaio_jobqueue, plist, cbn) {
746 		remove = 0;
747 		mtx_lock(&aio_job_mtx);
748 		if (cbe->jobstate == JOBST_JOBQGLOBAL) {
749 			TAILQ_REMOVE(&aio_jobs, cbe, list);
750 			remove = 1;
751 		} else if (cbe->jobstate == JOBST_JOBQSOCK) {
752 			fp = cbe->fd_file;
753 			MPASS(fp->f_type == DTYPE_SOCKET);
754 			so = fp->f_data;
755 			TAILQ_REMOVE(&so->so_aiojobq, cbe, list);
756 			remove = 1;
757 		} else if (cbe->jobstate == JOBST_JOBQSYNC) {
758 			TAILQ_REMOVE(&ki->kaio_syncqueue, cbe, list);
759 			remove = 1;
760 		}
761 		mtx_unlock(&aio_job_mtx);
762 
763 		if (remove) {
764 			cbe->jobstate = JOBST_JOBFINISHED;
765 			cbe->uaiocb._aiocb_private.status = -1;
766 			cbe->uaiocb._aiocb_private.error = ECANCELED;
767 			TAILQ_REMOVE(&ki->kaio_jobqueue, cbe, plist);
768 			aio_bio_done_notify(p, cbe, DONE_QUEUE);
769 		}
770 	}
771 
772 	/* Wait for all running I/O to be finished */
773 	if (TAILQ_FIRST(&ki->kaio_bufqueue) ||
774 	    TAILQ_FIRST(&ki->kaio_jobqueue)) {
775 		ki->kaio_flags |= KAIO_WAKEUP;
776 		msleep(&p->p_aioinfo, AIO_MTX(ki), PRIBIO, "aioprn", hz);
777 		goto restart;
778 	}
779 
780 	/* Free all completed I/O requests. */
781 	while ((cbe = TAILQ_FIRST(&ki->kaio_done)) != NULL)
782 		aio_free_entry(cbe);
783 
784 	while ((lj = TAILQ_FIRST(&ki->kaio_liojoblist)) != NULL) {
785 		if (lj->lioj_count == 0) {
786 			TAILQ_REMOVE(&ki->kaio_liojoblist, lj, lioj_list);
787 			knlist_delete(&lj->klist, curthread, 1);
788 			PROC_LOCK(p);
789 			sigqueue_take(&lj->lioj_ksi);
790 			PROC_UNLOCK(p);
791 			uma_zfree(aiolio_zone, lj);
792 		} else {
793 			panic("LIO job not cleaned up: C:%d, FC:%d\n",
794 			    lj->lioj_count, lj->lioj_finished_count);
795 		}
796 	}
797 	AIO_UNLOCK(ki);
798 	taskqueue_drain(taskqueue_aiod_bio, &ki->kaio_task);
799 	mtx_destroy(&ki->kaio_mtx);
800 	uma_zfree(kaio_zone, ki);
801 	p->p_aioinfo = NULL;
802 }
803 
804 /*
805  * Select a job to run (called by an AIO daemon).
806  */
807 static struct aiocblist *
808 aio_selectjob(struct aiothreadlist *aiop)
809 {
810 	struct aiocblist *aiocbe;
811 	struct kaioinfo *ki;
812 	struct proc *userp;
813 
814 	mtx_assert(&aio_job_mtx, MA_OWNED);
815 	TAILQ_FOREACH(aiocbe, &aio_jobs, list) {
816 		userp = aiocbe->userproc;
817 		ki = userp->p_aioinfo;
818 
819 		if (ki->kaio_active_count < ki->kaio_maxactive_count) {
820 			TAILQ_REMOVE(&aio_jobs, aiocbe, list);
821 			/* Account for currently active jobs. */
822 			ki->kaio_active_count++;
823 			aiocbe->jobstate = JOBST_JOBRUNNING;
824 			break;
825 		}
826 	}
827 	return (aiocbe);
828 }
829 
830 /*
831  *  Move all data to a permanent storage device, this code
832  *  simulates fsync syscall.
833  */
834 static int
835 aio_fsync_vnode(struct thread *td, struct vnode *vp)
836 {
837 	struct mount *mp;
838 	int vfslocked;
839 	int error;
840 
841 	vfslocked = VFS_LOCK_GIANT(vp->v_mount);
842 	if ((error = vn_start_write(vp, &mp, V_WAIT | PCATCH)) != 0)
843 		goto drop;
844 	vn_lock(vp, LK_EXCLUSIVE | LK_RETRY);
845 	if (vp->v_object != NULL) {
846 		VM_OBJECT_LOCK(vp->v_object);
847 		vm_object_page_clean(vp->v_object, 0, 0, 0);
848 		VM_OBJECT_UNLOCK(vp->v_object);
849 	}
850 	error = VOP_FSYNC(vp, MNT_WAIT, td);
851 
852 	VOP_UNLOCK(vp, 0);
853 	vn_finished_write(mp);
854 drop:
855 	VFS_UNLOCK_GIANT(vfslocked);
856 	return (error);
857 }
858 
859 /*
860  * The AIO processing activity.  This is the code that does the I/O request for
861  * the non-physio version of the operations.  The normal vn operations are used,
862  * and this code should work in all instances for every type of file, including
863  * pipes, sockets, fifos, and regular files.
864  *
865  * XXX I don't think it works well for socket, pipe, and fifo.
866  */
867 static void
868 aio_process(struct aiocblist *aiocbe)
869 {
870 	struct ucred *td_savedcred;
871 	struct thread *td;
872 	struct aiocb *cb;
873 	struct file *fp;
874 	struct socket *so;
875 	struct uio auio;
876 	struct iovec aiov;
877 	int cnt;
878 	int error;
879 	int oublock_st, oublock_end;
880 	int inblock_st, inblock_end;
881 
882 	td = curthread;
883 	td_savedcred = td->td_ucred;
884 	td->td_ucred = aiocbe->cred;
885 	cb = &aiocbe->uaiocb;
886 	fp = aiocbe->fd_file;
887 
888 	if (cb->aio_lio_opcode == LIO_SYNC) {
889 		error = 0;
890 		cnt = 0;
891 		if (fp->f_vnode != NULL)
892 			error = aio_fsync_vnode(td, fp->f_vnode);
893 		cb->_aiocb_private.error = error;
894 		cb->_aiocb_private.status = 0;
895 		td->td_ucred = td_savedcred;
896 		return;
897 	}
898 
899 	aiov.iov_base = (void *)(uintptr_t)cb->aio_buf;
900 	aiov.iov_len = cb->aio_nbytes;
901 
902 	auio.uio_iov = &aiov;
903 	auio.uio_iovcnt = 1;
904 	auio.uio_offset = cb->aio_offset;
905 	auio.uio_resid = cb->aio_nbytes;
906 	cnt = cb->aio_nbytes;
907 	auio.uio_segflg = UIO_USERSPACE;
908 	auio.uio_td = td;
909 
910 	inblock_st = td->td_ru.ru_inblock;
911 	oublock_st = td->td_ru.ru_oublock;
912 	/*
913 	 * aio_aqueue() acquires a reference to the file that is
914 	 * released in aio_free_entry().
915 	 */
916 	if (cb->aio_lio_opcode == LIO_READ) {
917 		auio.uio_rw = UIO_READ;
918 		if (auio.uio_resid == 0)
919 			error = 0;
920 		else
921 			error = fo_read(fp, &auio, fp->f_cred, FOF_OFFSET, td);
922 	} else {
923 		if (fp->f_type == DTYPE_VNODE)
924 			bwillwrite();
925 		auio.uio_rw = UIO_WRITE;
926 		error = fo_write(fp, &auio, fp->f_cred, FOF_OFFSET, td);
927 	}
928 	inblock_end = td->td_ru.ru_inblock;
929 	oublock_end = td->td_ru.ru_oublock;
930 
931 	aiocbe->inputcharge = inblock_end - inblock_st;
932 	aiocbe->outputcharge = oublock_end - oublock_st;
933 
934 	if ((error) && (auio.uio_resid != cnt)) {
935 		if (error == ERESTART || error == EINTR || error == EWOULDBLOCK)
936 			error = 0;
937 		if ((error == EPIPE) && (cb->aio_lio_opcode == LIO_WRITE)) {
938 			int sigpipe = 1;
939 			if (fp->f_type == DTYPE_SOCKET) {
940 				so = fp->f_data;
941 				if (so->so_options & SO_NOSIGPIPE)
942 					sigpipe = 0;
943 			}
944 			if (sigpipe) {
945 				PROC_LOCK(aiocbe->userproc);
946 				kern_psignal(aiocbe->userproc, SIGPIPE);
947 				PROC_UNLOCK(aiocbe->userproc);
948 			}
949 		}
950 	}
951 
952 	cnt -= auio.uio_resid;
953 	cb->_aiocb_private.error = error;
954 	cb->_aiocb_private.status = cnt;
955 	td->td_ucred = td_savedcred;
956 }
957 
958 static void
959 aio_bio_done_notify(struct proc *userp, struct aiocblist *aiocbe, int type)
960 {
961 	struct aioliojob *lj;
962 	struct kaioinfo *ki;
963 	struct aiocblist *scb, *scbn;
964 	int lj_done;
965 
966 	ki = userp->p_aioinfo;
967 	AIO_LOCK_ASSERT(ki, MA_OWNED);
968 	lj = aiocbe->lio;
969 	lj_done = 0;
970 	if (lj) {
971 		lj->lioj_finished_count++;
972 		if (lj->lioj_count == lj->lioj_finished_count)
973 			lj_done = 1;
974 	}
975 	if (type == DONE_QUEUE) {
976 		aiocbe->jobflags |= AIOCBLIST_DONE;
977 	} else {
978 		aiocbe->jobflags |= AIOCBLIST_BUFDONE;
979 	}
980 	TAILQ_INSERT_TAIL(&ki->kaio_done, aiocbe, plist);
981 	aiocbe->jobstate = JOBST_JOBFINISHED;
982 
983 	if (ki->kaio_flags & KAIO_RUNDOWN)
984 		goto notification_done;
985 
986 	if (aiocbe->uaiocb.aio_sigevent.sigev_notify == SIGEV_SIGNAL ||
987 	    aiocbe->uaiocb.aio_sigevent.sigev_notify == SIGEV_THREAD_ID)
988 		aio_sendsig(userp, &aiocbe->uaiocb.aio_sigevent, &aiocbe->ksi);
989 
990 	KNOTE_LOCKED(&aiocbe->klist, 1);
991 
992 	if (lj_done) {
993 		if (lj->lioj_signal.sigev_notify == SIGEV_KEVENT) {
994 			lj->lioj_flags |= LIOJ_KEVENT_POSTED;
995 			KNOTE_LOCKED(&lj->klist, 1);
996 		}
997 		if ((lj->lioj_flags & (LIOJ_SIGNAL|LIOJ_SIGNAL_POSTED))
998 		    == LIOJ_SIGNAL
999 		    && (lj->lioj_signal.sigev_notify == SIGEV_SIGNAL ||
1000 		        lj->lioj_signal.sigev_notify == SIGEV_THREAD_ID)) {
1001 			aio_sendsig(userp, &lj->lioj_signal, &lj->lioj_ksi);
1002 			lj->lioj_flags |= LIOJ_SIGNAL_POSTED;
1003 		}
1004 	}
1005 
1006 notification_done:
1007 	if (aiocbe->jobflags & AIOCBLIST_CHECKSYNC) {
1008 		TAILQ_FOREACH_SAFE(scb, &ki->kaio_syncqueue, list, scbn) {
1009 			if (aiocbe->fd_file == scb->fd_file &&
1010 			    aiocbe->seqno < scb->seqno) {
1011 				if (--scb->pending == 0) {
1012 					mtx_lock(&aio_job_mtx);
1013 					scb->jobstate = JOBST_JOBQGLOBAL;
1014 					TAILQ_REMOVE(&ki->kaio_syncqueue, scb, list);
1015 					TAILQ_INSERT_TAIL(&aio_jobs, scb, list);
1016 					aio_kick_nowait(userp);
1017 					mtx_unlock(&aio_job_mtx);
1018 				}
1019 			}
1020 		}
1021 	}
1022 	if (ki->kaio_flags & KAIO_WAKEUP) {
1023 		ki->kaio_flags &= ~KAIO_WAKEUP;
1024 		wakeup(&userp->p_aioinfo);
1025 	}
1026 }
1027 
1028 /*
1029  * The AIO daemon, most of the actual work is done in aio_process,
1030  * but the setup (and address space mgmt) is done in this routine.
1031  */
1032 static void
1033 aio_daemon(void *_id)
1034 {
1035 	struct aiocblist *aiocbe;
1036 	struct aiothreadlist *aiop;
1037 	struct kaioinfo *ki;
1038 	struct proc *curcp, *mycp, *userp;
1039 	struct vmspace *myvm, *tmpvm;
1040 	struct thread *td = curthread;
1041 	int id = (intptr_t)_id;
1042 
1043 	/*
1044 	 * Local copies of curproc (cp) and vmspace (myvm)
1045 	 */
1046 	mycp = td->td_proc;
1047 	myvm = mycp->p_vmspace;
1048 
1049 	KASSERT(mycp->p_textvp == NULL, ("kthread has a textvp"));
1050 
1051 	/*
1052 	 * Allocate and ready the aio control info.  There is one aiop structure
1053 	 * per daemon.
1054 	 */
1055 	aiop = uma_zalloc(aiop_zone, M_WAITOK);
1056 	aiop->aiothread = td;
1057 	aiop->aiothreadflags = 0;
1058 
1059 	/* The daemon resides in its own pgrp. */
1060 	sys_setsid(td, NULL);
1061 
1062 	/*
1063 	 * Wakeup parent process.  (Parent sleeps to keep from blasting away
1064 	 * and creating too many daemons.)
1065 	 */
1066 	sema_post(&aio_newproc_sem);
1067 
1068 	mtx_lock(&aio_job_mtx);
1069 	for (;;) {
1070 		/*
1071 		 * curcp is the current daemon process context.
1072 		 * userp is the current user process context.
1073 		 */
1074 		curcp = mycp;
1075 
1076 		/*
1077 		 * Take daemon off of free queue
1078 		 */
1079 		if (aiop->aiothreadflags & AIOP_FREE) {
1080 			TAILQ_REMOVE(&aio_freeproc, aiop, list);
1081 			aiop->aiothreadflags &= ~AIOP_FREE;
1082 		}
1083 
1084 		/*
1085 		 * Check for jobs.
1086 		 */
1087 		while ((aiocbe = aio_selectjob(aiop)) != NULL) {
1088 			mtx_unlock(&aio_job_mtx);
1089 			userp = aiocbe->userproc;
1090 
1091 			/*
1092 			 * Connect to process address space for user program.
1093 			 */
1094 			if (userp != curcp) {
1095 				/*
1096 				 * Save the current address space that we are
1097 				 * connected to.
1098 				 */
1099 				tmpvm = mycp->p_vmspace;
1100 
1101 				/*
1102 				 * Point to the new user address space, and
1103 				 * refer to it.
1104 				 */
1105 				mycp->p_vmspace = userp->p_vmspace;
1106 				atomic_add_int(&mycp->p_vmspace->vm_refcnt, 1);
1107 
1108 				/* Activate the new mapping. */
1109 				pmap_activate(FIRST_THREAD_IN_PROC(mycp));
1110 
1111 				/*
1112 				 * If the old address space wasn't the daemons
1113 				 * own address space, then we need to remove the
1114 				 * daemon's reference from the other process
1115 				 * that it was acting on behalf of.
1116 				 */
1117 				if (tmpvm != myvm) {
1118 					vmspace_free(tmpvm);
1119 				}
1120 				curcp = userp;
1121 			}
1122 
1123 			ki = userp->p_aioinfo;
1124 
1125 			/* Do the I/O function. */
1126 			aio_process(aiocbe);
1127 
1128 			mtx_lock(&aio_job_mtx);
1129 			/* Decrement the active job count. */
1130 			ki->kaio_active_count--;
1131 			mtx_unlock(&aio_job_mtx);
1132 
1133 			AIO_LOCK(ki);
1134 			TAILQ_REMOVE(&ki->kaio_jobqueue, aiocbe, plist);
1135 			aio_bio_done_notify(userp, aiocbe, DONE_QUEUE);
1136 			AIO_UNLOCK(ki);
1137 
1138 			mtx_lock(&aio_job_mtx);
1139 		}
1140 
1141 		/*
1142 		 * Disconnect from user address space.
1143 		 */
1144 		if (curcp != mycp) {
1145 
1146 			mtx_unlock(&aio_job_mtx);
1147 
1148 			/* Get the user address space to disconnect from. */
1149 			tmpvm = mycp->p_vmspace;
1150 
1151 			/* Get original address space for daemon. */
1152 			mycp->p_vmspace = myvm;
1153 
1154 			/* Activate the daemon's address space. */
1155 			pmap_activate(FIRST_THREAD_IN_PROC(mycp));
1156 #ifdef DIAGNOSTIC
1157 			if (tmpvm == myvm) {
1158 				printf("AIOD: vmspace problem -- %d\n",
1159 				    mycp->p_pid);
1160 			}
1161 #endif
1162 			/* Remove our vmspace reference. */
1163 			vmspace_free(tmpvm);
1164 
1165 			curcp = mycp;
1166 
1167 			mtx_lock(&aio_job_mtx);
1168 			/*
1169 			 * We have to restart to avoid race, we only sleep if
1170 			 * no job can be selected, that should be
1171 			 * curcp == mycp.
1172 			 */
1173 			continue;
1174 		}
1175 
1176 		mtx_assert(&aio_job_mtx, MA_OWNED);
1177 
1178 		TAILQ_INSERT_HEAD(&aio_freeproc, aiop, list);
1179 		aiop->aiothreadflags |= AIOP_FREE;
1180 
1181 		/*
1182 		 * If daemon is inactive for a long time, allow it to exit,
1183 		 * thereby freeing resources.
1184 		 */
1185 		if (msleep(aiop->aiothread, &aio_job_mtx, PRIBIO, "aiordy",
1186 		    aiod_lifetime)) {
1187 			if (TAILQ_EMPTY(&aio_jobs)) {
1188 				if ((aiop->aiothreadflags & AIOP_FREE) &&
1189 				    (num_aio_procs > target_aio_procs)) {
1190 					TAILQ_REMOVE(&aio_freeproc, aiop, list);
1191 					num_aio_procs--;
1192 					mtx_unlock(&aio_job_mtx);
1193 					uma_zfree(aiop_zone, aiop);
1194 					free_unr(aiod_unr, id);
1195 #ifdef DIAGNOSTIC
1196 					if (mycp->p_vmspace->vm_refcnt <= 1) {
1197 						printf("AIOD: bad vm refcnt for"
1198 						    " exiting daemon: %d\n",
1199 						    mycp->p_vmspace->vm_refcnt);
1200 					}
1201 #endif
1202 					kproc_exit(0);
1203 				}
1204 			}
1205 		}
1206 	}
1207 	mtx_unlock(&aio_job_mtx);
1208 	panic("shouldn't be here\n");
1209 }
1210 
1211 /*
1212  * Create a new AIO daemon. This is mostly a kernel-thread fork routine. The
1213  * AIO daemon modifies its environment itself.
1214  */
1215 static int
1216 aio_newproc(int *start)
1217 {
1218 	int error;
1219 	struct proc *p;
1220 	int id;
1221 
1222 	id = alloc_unr(aiod_unr);
1223 	error = kproc_create(aio_daemon, (void *)(intptr_t)id, &p,
1224 		RFNOWAIT, 0, "aiod%d", id);
1225 	if (error == 0) {
1226 		/*
1227 		 * Wait until daemon is started.
1228 		 */
1229 		sema_wait(&aio_newproc_sem);
1230 		mtx_lock(&aio_job_mtx);
1231 		num_aio_procs++;
1232 		if (start != NULL)
1233 			(*start)--;
1234 		mtx_unlock(&aio_job_mtx);
1235 	} else {
1236 		free_unr(aiod_unr, id);
1237 	}
1238 	return (error);
1239 }
1240 
1241 /*
1242  * Try the high-performance, low-overhead physio method for eligible
1243  * VCHR devices.  This method doesn't use an aio helper thread, and
1244  * thus has very low overhead.
1245  *
1246  * Assumes that the caller, aio_aqueue(), has incremented the file
1247  * structure's reference count, preventing its deallocation for the
1248  * duration of this call.
1249  */
1250 static int
1251 aio_qphysio(struct proc *p, struct aiocblist *aiocbe)
1252 {
1253 	struct aiocb *cb;
1254 	struct file *fp;
1255 	struct buf *bp;
1256 	struct vnode *vp;
1257 	struct kaioinfo *ki;
1258 	struct aioliojob *lj;
1259 	int error;
1260 
1261 	cb = &aiocbe->uaiocb;
1262 	fp = aiocbe->fd_file;
1263 
1264 	if (fp->f_type != DTYPE_VNODE)
1265 		return (-1);
1266 
1267 	vp = fp->f_vnode;
1268 
1269 	/*
1270 	 * If its not a disk, we don't want to return a positive error.
1271 	 * It causes the aio code to not fall through to try the thread
1272 	 * way when you're talking to a regular file.
1273 	 */
1274 	if (!vn_isdisk(vp, &error)) {
1275 		if (error == ENOTBLK)
1276 			return (-1);
1277 		else
1278 			return (error);
1279 	}
1280 
1281 	if (vp->v_bufobj.bo_bsize == 0)
1282 		return (-1);
1283 
1284  	if (cb->aio_nbytes % vp->v_bufobj.bo_bsize)
1285 		return (-1);
1286 
1287 	if (cb->aio_nbytes > vp->v_rdev->si_iosize_max)
1288 		return (-1);
1289 
1290 	if (cb->aio_nbytes >
1291 	    MAXPHYS - (((vm_offset_t) cb->aio_buf) & PAGE_MASK))
1292 		return (-1);
1293 
1294 	ki = p->p_aioinfo;
1295 	if (ki->kaio_buffer_count >= ki->kaio_ballowed_count)
1296 		return (-1);
1297 
1298 	/* Create and build a buffer header for a transfer. */
1299 	bp = (struct buf *)getpbuf(NULL);
1300 	BUF_KERNPROC(bp);
1301 
1302 	AIO_LOCK(ki);
1303 	ki->kaio_count++;
1304 	ki->kaio_buffer_count++;
1305 	lj = aiocbe->lio;
1306 	if (lj)
1307 		lj->lioj_count++;
1308 	AIO_UNLOCK(ki);
1309 
1310 	/*
1311 	 * Get a copy of the kva from the physical buffer.
1312 	 */
1313 	error = 0;
1314 
1315 	bp->b_bcount = cb->aio_nbytes;
1316 	bp->b_bufsize = cb->aio_nbytes;
1317 	bp->b_iodone = aio_physwakeup;
1318 	bp->b_saveaddr = bp->b_data;
1319 	bp->b_data = (void *)(uintptr_t)cb->aio_buf;
1320 	bp->b_offset = cb->aio_offset;
1321 	bp->b_iooffset = cb->aio_offset;
1322 	bp->b_blkno = btodb(cb->aio_offset);
1323 	bp->b_iocmd = cb->aio_lio_opcode == LIO_WRITE ? BIO_WRITE : BIO_READ;
1324 
1325 	/*
1326 	 * Bring buffer into kernel space.
1327 	 */
1328 	if (vmapbuf(bp) < 0) {
1329 		error = EFAULT;
1330 		goto doerror;
1331 	}
1332 
1333 	AIO_LOCK(ki);
1334 	aiocbe->bp = bp;
1335 	bp->b_caller1 = (void *)aiocbe;
1336 	TAILQ_INSERT_TAIL(&ki->kaio_bufqueue, aiocbe, plist);
1337 	TAILQ_INSERT_TAIL(&ki->kaio_all, aiocbe, allist);
1338 	aiocbe->jobstate = JOBST_JOBQBUF;
1339 	cb->_aiocb_private.status = cb->aio_nbytes;
1340 	AIO_UNLOCK(ki);
1341 
1342 	atomic_add_int(&num_queue_count, 1);
1343 	atomic_add_int(&num_buf_aio, 1);
1344 
1345 	bp->b_error = 0;
1346 
1347 	TASK_INIT(&aiocbe->biotask, 0, biohelper, aiocbe);
1348 
1349 	/* Perform transfer. */
1350 	dev_strategy(vp->v_rdev, bp);
1351 	return (0);
1352 
1353 doerror:
1354 	AIO_LOCK(ki);
1355 	ki->kaio_count--;
1356 	ki->kaio_buffer_count--;
1357 	if (lj)
1358 		lj->lioj_count--;
1359 	aiocbe->bp = NULL;
1360 	AIO_UNLOCK(ki);
1361 	relpbuf(bp, NULL);
1362 	return (error);
1363 }
1364 
1365 /*
1366  * Wake up aio requests that may be serviceable now.
1367  */
1368 static void
1369 aio_swake_cb(struct socket *so, struct sockbuf *sb)
1370 {
1371 	struct aiocblist *cb, *cbn;
1372 	int opcode;
1373 
1374 	SOCKBUF_LOCK_ASSERT(sb);
1375 	if (sb == &so->so_snd)
1376 		opcode = LIO_WRITE;
1377 	else
1378 		opcode = LIO_READ;
1379 
1380 	sb->sb_flags &= ~SB_AIO;
1381 	mtx_lock(&aio_job_mtx);
1382 	TAILQ_FOREACH_SAFE(cb, &so->so_aiojobq, list, cbn) {
1383 		if (opcode == cb->uaiocb.aio_lio_opcode) {
1384 			if (cb->jobstate != JOBST_JOBQSOCK)
1385 				panic("invalid queue value");
1386 			/* XXX
1387 			 * We don't have actual sockets backend yet,
1388 			 * so we simply move the requests to the generic
1389 			 * file I/O backend.
1390 			 */
1391 			TAILQ_REMOVE(&so->so_aiojobq, cb, list);
1392 			TAILQ_INSERT_TAIL(&aio_jobs, cb, list);
1393 			aio_kick_nowait(cb->userproc);
1394 		}
1395 	}
1396 	mtx_unlock(&aio_job_mtx);
1397 }
1398 
1399 static int
1400 convert_old_sigevent(struct osigevent *osig, struct sigevent *nsig)
1401 {
1402 
1403 	/*
1404 	 * Only SIGEV_NONE, SIGEV_SIGNAL, and SIGEV_KEVENT are
1405 	 * supported by AIO with the old sigevent structure.
1406 	 */
1407 	nsig->sigev_notify = osig->sigev_notify;
1408 	switch (nsig->sigev_notify) {
1409 	case SIGEV_NONE:
1410 		break;
1411 	case SIGEV_SIGNAL:
1412 		nsig->sigev_signo = osig->__sigev_u.__sigev_signo;
1413 		break;
1414 	case SIGEV_KEVENT:
1415 		nsig->sigev_notify_kqueue =
1416 		    osig->__sigev_u.__sigev_notify_kqueue;
1417 		nsig->sigev_value.sival_ptr = osig->sigev_value.sival_ptr;
1418 		break;
1419 	default:
1420 		return (EINVAL);
1421 	}
1422 	return (0);
1423 }
1424 
1425 static int
1426 aiocb_copyin_old_sigevent(struct aiocb *ujob, struct aiocb *kjob)
1427 {
1428 	struct oaiocb *ojob;
1429 	int error;
1430 
1431 	bzero(kjob, sizeof(struct aiocb));
1432 	error = copyin(ujob, kjob, sizeof(struct oaiocb));
1433 	if (error)
1434 		return (error);
1435 	ojob = (struct oaiocb *)kjob;
1436 	return (convert_old_sigevent(&ojob->aio_sigevent, &kjob->aio_sigevent));
1437 }
1438 
1439 static int
1440 aiocb_copyin(struct aiocb *ujob, struct aiocb *kjob)
1441 {
1442 
1443 	return (copyin(ujob, kjob, sizeof(struct aiocb)));
1444 }
1445 
1446 static long
1447 aiocb_fetch_status(struct aiocb *ujob)
1448 {
1449 
1450 	return (fuword(&ujob->_aiocb_private.status));
1451 }
1452 
1453 static long
1454 aiocb_fetch_error(struct aiocb *ujob)
1455 {
1456 
1457 	return (fuword(&ujob->_aiocb_private.error));
1458 }
1459 
1460 static int
1461 aiocb_store_status(struct aiocb *ujob, long status)
1462 {
1463 
1464 	return (suword(&ujob->_aiocb_private.status, status));
1465 }
1466 
1467 static int
1468 aiocb_store_error(struct aiocb *ujob, long error)
1469 {
1470 
1471 	return (suword(&ujob->_aiocb_private.error, error));
1472 }
1473 
1474 static int
1475 aiocb_store_kernelinfo(struct aiocb *ujob, long jobref)
1476 {
1477 
1478 	return (suword(&ujob->_aiocb_private.kernelinfo, jobref));
1479 }
1480 
1481 static int
1482 aiocb_store_aiocb(struct aiocb **ujobp, struct aiocb *ujob)
1483 {
1484 
1485 	return (suword(ujobp, (long)ujob));
1486 }
1487 
1488 static struct aiocb_ops aiocb_ops = {
1489 	.copyin = aiocb_copyin,
1490 	.fetch_status = aiocb_fetch_status,
1491 	.fetch_error = aiocb_fetch_error,
1492 	.store_status = aiocb_store_status,
1493 	.store_error = aiocb_store_error,
1494 	.store_kernelinfo = aiocb_store_kernelinfo,
1495 	.store_aiocb = aiocb_store_aiocb,
1496 };
1497 
1498 static struct aiocb_ops aiocb_ops_osigevent = {
1499 	.copyin = aiocb_copyin_old_sigevent,
1500 	.fetch_status = aiocb_fetch_status,
1501 	.fetch_error = aiocb_fetch_error,
1502 	.store_status = aiocb_store_status,
1503 	.store_error = aiocb_store_error,
1504 	.store_kernelinfo = aiocb_store_kernelinfo,
1505 	.store_aiocb = aiocb_store_aiocb,
1506 };
1507 
1508 /*
1509  * Queue a new AIO request.  Choosing either the threaded or direct physio VCHR
1510  * technique is done in this code.
1511  */
1512 int
1513 aio_aqueue(struct thread *td, struct aiocb *job, struct aioliojob *lj,
1514 	int type, struct aiocb_ops *ops)
1515 {
1516 	struct proc *p = td->td_proc;
1517 	struct file *fp;
1518 	struct socket *so;
1519 	struct aiocblist *aiocbe, *cb;
1520 	struct kaioinfo *ki;
1521 	struct kevent kev;
1522 	struct sockbuf *sb;
1523 	int opcode;
1524 	int error;
1525 	int fd, kqfd;
1526 	int jid;
1527 
1528 	if (p->p_aioinfo == NULL)
1529 		aio_init_aioinfo(p);
1530 
1531 	ki = p->p_aioinfo;
1532 
1533 	ops->store_status(job, -1);
1534 	ops->store_error(job, 0);
1535 	ops->store_kernelinfo(job, -1);
1536 
1537 	if (num_queue_count >= max_queue_count ||
1538 	    ki->kaio_count >= ki->kaio_qallowed_count) {
1539 		ops->store_error(job, EAGAIN);
1540 		return (EAGAIN);
1541 	}
1542 
1543 	aiocbe = uma_zalloc(aiocb_zone, M_WAITOK | M_ZERO);
1544 	aiocbe->inputcharge = 0;
1545 	aiocbe->outputcharge = 0;
1546 	knlist_init_mtx(&aiocbe->klist, AIO_MTX(ki));
1547 
1548 	error = ops->copyin(job, &aiocbe->uaiocb);
1549 	if (error) {
1550 		ops->store_error(job, error);
1551 		uma_zfree(aiocb_zone, aiocbe);
1552 		return (error);
1553 	}
1554 
1555 	if (aiocbe->uaiocb.aio_sigevent.sigev_notify != SIGEV_KEVENT &&
1556 	    aiocbe->uaiocb.aio_sigevent.sigev_notify != SIGEV_SIGNAL &&
1557 	    aiocbe->uaiocb.aio_sigevent.sigev_notify != SIGEV_THREAD_ID &&
1558 	    aiocbe->uaiocb.aio_sigevent.sigev_notify != SIGEV_NONE) {
1559 		ops->store_error(job, EINVAL);
1560 		uma_zfree(aiocb_zone, aiocbe);
1561 		return (EINVAL);
1562 	}
1563 
1564 	if ((aiocbe->uaiocb.aio_sigevent.sigev_notify == SIGEV_SIGNAL ||
1565 	     aiocbe->uaiocb.aio_sigevent.sigev_notify == SIGEV_THREAD_ID) &&
1566 		!_SIG_VALID(aiocbe->uaiocb.aio_sigevent.sigev_signo)) {
1567 		uma_zfree(aiocb_zone, aiocbe);
1568 		return (EINVAL);
1569 	}
1570 
1571 	ksiginfo_init(&aiocbe->ksi);
1572 
1573 	/* Save userspace address of the job info. */
1574 	aiocbe->uuaiocb = job;
1575 
1576 	/* Get the opcode. */
1577 	if (type != LIO_NOP)
1578 		aiocbe->uaiocb.aio_lio_opcode = type;
1579 	opcode = aiocbe->uaiocb.aio_lio_opcode;
1580 
1581 	/*
1582 	 * Validate the opcode and fetch the file object for the specified
1583 	 * file descriptor.
1584 	 *
1585 	 * XXXRW: Moved the opcode validation up here so that we don't
1586 	 * retrieve a file descriptor without knowing what the capabiltity
1587 	 * should be.
1588 	 */
1589 	fd = aiocbe->uaiocb.aio_fildes;
1590 	switch (opcode) {
1591 	case LIO_WRITE:
1592 		error = fget_write(td, fd, CAP_WRITE | CAP_SEEK, &fp);
1593 		break;
1594 	case LIO_READ:
1595 		error = fget_read(td, fd, CAP_READ | CAP_SEEK, &fp);
1596 		break;
1597 	case LIO_SYNC:
1598 		error = fget(td, fd, CAP_FSYNC, &fp);
1599 		break;
1600 	case LIO_NOP:
1601 		error = fget(td, fd, 0, &fp);
1602 		break;
1603 	default:
1604 		error = EINVAL;
1605 	}
1606 	if (error) {
1607 		uma_zfree(aiocb_zone, aiocbe);
1608 		ops->store_error(job, error);
1609 		return (error);
1610 	}
1611 
1612 	if (opcode == LIO_SYNC && fp->f_vnode == NULL) {
1613 		error = EINVAL;
1614 		goto aqueue_fail;
1615 	}
1616 
1617 	if (opcode != LIO_SYNC && aiocbe->uaiocb.aio_offset == -1LL) {
1618 		error = EINVAL;
1619 		goto aqueue_fail;
1620 	}
1621 
1622 	aiocbe->fd_file = fp;
1623 
1624 	mtx_lock(&aio_job_mtx);
1625 	jid = jobrefid++;
1626 	aiocbe->seqno = jobseqno++;
1627 	mtx_unlock(&aio_job_mtx);
1628 	error = ops->store_kernelinfo(job, jid);
1629 	if (error) {
1630 		error = EINVAL;
1631 		goto aqueue_fail;
1632 	}
1633 	aiocbe->uaiocb._aiocb_private.kernelinfo = (void *)(intptr_t)jid;
1634 
1635 	if (opcode == LIO_NOP) {
1636 		fdrop(fp, td);
1637 		uma_zfree(aiocb_zone, aiocbe);
1638 		return (0);
1639 	}
1640 
1641 	if (aiocbe->uaiocb.aio_sigevent.sigev_notify != SIGEV_KEVENT)
1642 		goto no_kqueue;
1643 	kqfd = aiocbe->uaiocb.aio_sigevent.sigev_notify_kqueue;
1644 	kev.ident = (uintptr_t)aiocbe->uuaiocb;
1645 	kev.filter = EVFILT_AIO;
1646 	kev.flags = EV_ADD | EV_ENABLE | EV_FLAG1;
1647 	kev.data = (intptr_t)aiocbe;
1648 	kev.udata = aiocbe->uaiocb.aio_sigevent.sigev_value.sival_ptr;
1649 	error = kqfd_register(kqfd, &kev, td, 1);
1650 aqueue_fail:
1651 	if (error) {
1652 		fdrop(fp, td);
1653 		uma_zfree(aiocb_zone, aiocbe);
1654 		ops->store_error(job, error);
1655 		goto done;
1656 	}
1657 no_kqueue:
1658 
1659 	ops->store_error(job, EINPROGRESS);
1660 	aiocbe->uaiocb._aiocb_private.error = EINPROGRESS;
1661 	aiocbe->userproc = p;
1662 	aiocbe->cred = crhold(td->td_ucred);
1663 	aiocbe->jobflags = 0;
1664 	aiocbe->lio = lj;
1665 
1666 	if (opcode == LIO_SYNC)
1667 		goto queueit;
1668 
1669 	if (fp->f_type == DTYPE_SOCKET) {
1670 		/*
1671 		 * Alternate queueing for socket ops: Reach down into the
1672 		 * descriptor to get the socket data.  Then check to see if the
1673 		 * socket is ready to be read or written (based on the requested
1674 		 * operation).
1675 		 *
1676 		 * If it is not ready for io, then queue the aiocbe on the
1677 		 * socket, and set the flags so we get a call when sbnotify()
1678 		 * happens.
1679 		 *
1680 		 * Note if opcode is neither LIO_WRITE nor LIO_READ we lock
1681 		 * and unlock the snd sockbuf for no reason.
1682 		 */
1683 		so = fp->f_data;
1684 		sb = (opcode == LIO_READ) ? &so->so_rcv : &so->so_snd;
1685 		SOCKBUF_LOCK(sb);
1686 		if (((opcode == LIO_READ) && (!soreadable(so))) || ((opcode ==
1687 		    LIO_WRITE) && (!sowriteable(so)))) {
1688 			sb->sb_flags |= SB_AIO;
1689 
1690 			mtx_lock(&aio_job_mtx);
1691 			TAILQ_INSERT_TAIL(&so->so_aiojobq, aiocbe, list);
1692 			mtx_unlock(&aio_job_mtx);
1693 
1694 			AIO_LOCK(ki);
1695 			TAILQ_INSERT_TAIL(&ki->kaio_all, aiocbe, allist);
1696 			TAILQ_INSERT_TAIL(&ki->kaio_jobqueue, aiocbe, plist);
1697 			aiocbe->jobstate = JOBST_JOBQSOCK;
1698 			ki->kaio_count++;
1699 			if (lj)
1700 				lj->lioj_count++;
1701 			AIO_UNLOCK(ki);
1702 			SOCKBUF_UNLOCK(sb);
1703 			atomic_add_int(&num_queue_count, 1);
1704 			error = 0;
1705 			goto done;
1706 		}
1707 		SOCKBUF_UNLOCK(sb);
1708 	}
1709 
1710 	if ((error = aio_qphysio(p, aiocbe)) == 0)
1711 		goto done;
1712 #if 0
1713 	if (error > 0) {
1714 		aiocbe->uaiocb._aiocb_private.error = error;
1715 		ops->store_error(job, error);
1716 		goto done;
1717 	}
1718 #endif
1719 queueit:
1720 	/* No buffer for daemon I/O. */
1721 	aiocbe->bp = NULL;
1722 	atomic_add_int(&num_queue_count, 1);
1723 
1724 	AIO_LOCK(ki);
1725 	ki->kaio_count++;
1726 	if (lj)
1727 		lj->lioj_count++;
1728 	TAILQ_INSERT_TAIL(&ki->kaio_jobqueue, aiocbe, plist);
1729 	TAILQ_INSERT_TAIL(&ki->kaio_all, aiocbe, allist);
1730 	if (opcode == LIO_SYNC) {
1731 		TAILQ_FOREACH(cb, &ki->kaio_jobqueue, plist) {
1732 			if (cb->fd_file == aiocbe->fd_file &&
1733 			    cb->uaiocb.aio_lio_opcode != LIO_SYNC &&
1734 			    cb->seqno < aiocbe->seqno) {
1735 				cb->jobflags |= AIOCBLIST_CHECKSYNC;
1736 				aiocbe->pending++;
1737 			}
1738 		}
1739 		TAILQ_FOREACH(cb, &ki->kaio_bufqueue, plist) {
1740 			if (cb->fd_file == aiocbe->fd_file &&
1741 			    cb->uaiocb.aio_lio_opcode != LIO_SYNC &&
1742 			    cb->seqno < aiocbe->seqno) {
1743 				cb->jobflags |= AIOCBLIST_CHECKSYNC;
1744 				aiocbe->pending++;
1745 			}
1746 		}
1747 		if (aiocbe->pending != 0) {
1748 			TAILQ_INSERT_TAIL(&ki->kaio_syncqueue, aiocbe, list);
1749 			aiocbe->jobstate = JOBST_JOBQSYNC;
1750 			AIO_UNLOCK(ki);
1751 			goto done;
1752 		}
1753 	}
1754 	mtx_lock(&aio_job_mtx);
1755 	TAILQ_INSERT_TAIL(&aio_jobs, aiocbe, list);
1756 	aiocbe->jobstate = JOBST_JOBQGLOBAL;
1757 	aio_kick_nowait(p);
1758 	mtx_unlock(&aio_job_mtx);
1759 	AIO_UNLOCK(ki);
1760 	error = 0;
1761 done:
1762 	return (error);
1763 }
1764 
1765 static void
1766 aio_kick_nowait(struct proc *userp)
1767 {
1768 	struct kaioinfo *ki = userp->p_aioinfo;
1769 	struct aiothreadlist *aiop;
1770 
1771 	mtx_assert(&aio_job_mtx, MA_OWNED);
1772 	if ((aiop = TAILQ_FIRST(&aio_freeproc)) != NULL) {
1773 		TAILQ_REMOVE(&aio_freeproc, aiop, list);
1774 		aiop->aiothreadflags &= ~AIOP_FREE;
1775 		wakeup(aiop->aiothread);
1776 	} else if (((num_aio_resv_start + num_aio_procs) < max_aio_procs) &&
1777 	    ((ki->kaio_active_count + num_aio_resv_start) <
1778 	    ki->kaio_maxactive_count)) {
1779 		taskqueue_enqueue(taskqueue_aiod_bio, &ki->kaio_task);
1780 	}
1781 }
1782 
1783 static int
1784 aio_kick(struct proc *userp)
1785 {
1786 	struct kaioinfo *ki = userp->p_aioinfo;
1787 	struct aiothreadlist *aiop;
1788 	int error, ret = 0;
1789 
1790 	mtx_assert(&aio_job_mtx, MA_OWNED);
1791 retryproc:
1792 	if ((aiop = TAILQ_FIRST(&aio_freeproc)) != NULL) {
1793 		TAILQ_REMOVE(&aio_freeproc, aiop, list);
1794 		aiop->aiothreadflags &= ~AIOP_FREE;
1795 		wakeup(aiop->aiothread);
1796 	} else if (((num_aio_resv_start + num_aio_procs) < max_aio_procs) &&
1797 	    ((ki->kaio_active_count + num_aio_resv_start) <
1798 	    ki->kaio_maxactive_count)) {
1799 		num_aio_resv_start++;
1800 		mtx_unlock(&aio_job_mtx);
1801 		error = aio_newproc(&num_aio_resv_start);
1802 		mtx_lock(&aio_job_mtx);
1803 		if (error) {
1804 			num_aio_resv_start--;
1805 			goto retryproc;
1806 		}
1807 	} else {
1808 		ret = -1;
1809 	}
1810 	return (ret);
1811 }
1812 
1813 static void
1814 aio_kick_helper(void *context, int pending)
1815 {
1816 	struct proc *userp = context;
1817 
1818 	mtx_lock(&aio_job_mtx);
1819 	while (--pending >= 0) {
1820 		if (aio_kick(userp))
1821 			break;
1822 	}
1823 	mtx_unlock(&aio_job_mtx);
1824 }
1825 
1826 /*
1827  * Support the aio_return system call, as a side-effect, kernel resources are
1828  * released.
1829  */
1830 static int
1831 kern_aio_return(struct thread *td, struct aiocb *uaiocb, struct aiocb_ops *ops)
1832 {
1833 	struct proc *p = td->td_proc;
1834 	struct aiocblist *cb;
1835 	struct kaioinfo *ki;
1836 	int status, error;
1837 
1838 	ki = p->p_aioinfo;
1839 	if (ki == NULL)
1840 		return (EINVAL);
1841 	AIO_LOCK(ki);
1842 	TAILQ_FOREACH(cb, &ki->kaio_done, plist) {
1843 		if (cb->uuaiocb == uaiocb)
1844 			break;
1845 	}
1846 	if (cb != NULL) {
1847 		MPASS(cb->jobstate == JOBST_JOBFINISHED);
1848 		status = cb->uaiocb._aiocb_private.status;
1849 		error = cb->uaiocb._aiocb_private.error;
1850 		td->td_retval[0] = status;
1851 		if (cb->uaiocb.aio_lio_opcode == LIO_WRITE) {
1852 			td->td_ru.ru_oublock += cb->outputcharge;
1853 			cb->outputcharge = 0;
1854 		} else if (cb->uaiocb.aio_lio_opcode == LIO_READ) {
1855 			td->td_ru.ru_inblock += cb->inputcharge;
1856 			cb->inputcharge = 0;
1857 		}
1858 		aio_free_entry(cb);
1859 		AIO_UNLOCK(ki);
1860 		ops->store_error(uaiocb, error);
1861 		ops->store_status(uaiocb, status);
1862 	} else {
1863 		error = EINVAL;
1864 		AIO_UNLOCK(ki);
1865 	}
1866 	return (error);
1867 }
1868 
1869 int
1870 sys_aio_return(struct thread *td, struct aio_return_args *uap)
1871 {
1872 
1873 	return (kern_aio_return(td, uap->aiocbp, &aiocb_ops));
1874 }
1875 
1876 /*
1877  * Allow a process to wakeup when any of the I/O requests are completed.
1878  */
1879 static int
1880 kern_aio_suspend(struct thread *td, int njoblist, struct aiocb **ujoblist,
1881     struct timespec *ts)
1882 {
1883 	struct proc *p = td->td_proc;
1884 	struct timeval atv;
1885 	struct kaioinfo *ki;
1886 	struct aiocblist *cb, *cbfirst;
1887 	int error, i, timo;
1888 
1889 	timo = 0;
1890 	if (ts) {
1891 		if (ts->tv_nsec < 0 || ts->tv_nsec >= 1000000000)
1892 			return (EINVAL);
1893 
1894 		TIMESPEC_TO_TIMEVAL(&atv, ts);
1895 		if (itimerfix(&atv))
1896 			return (EINVAL);
1897 		timo = tvtohz(&atv);
1898 	}
1899 
1900 	ki = p->p_aioinfo;
1901 	if (ki == NULL)
1902 		return (EAGAIN);
1903 
1904 	if (njoblist == 0)
1905 		return (0);
1906 
1907 	AIO_LOCK(ki);
1908 	for (;;) {
1909 		cbfirst = NULL;
1910 		error = 0;
1911 		TAILQ_FOREACH(cb, &ki->kaio_all, allist) {
1912 			for (i = 0; i < njoblist; i++) {
1913 				if (cb->uuaiocb == ujoblist[i]) {
1914 					if (cbfirst == NULL)
1915 						cbfirst = cb;
1916 					if (cb->jobstate == JOBST_JOBFINISHED)
1917 						goto RETURN;
1918 				}
1919 			}
1920 		}
1921 		/* All tasks were finished. */
1922 		if (cbfirst == NULL)
1923 			break;
1924 
1925 		ki->kaio_flags |= KAIO_WAKEUP;
1926 		error = msleep(&p->p_aioinfo, AIO_MTX(ki), PRIBIO | PCATCH,
1927 		    "aiospn", timo);
1928 		if (error == ERESTART)
1929 			error = EINTR;
1930 		if (error)
1931 			break;
1932 	}
1933 RETURN:
1934 	AIO_UNLOCK(ki);
1935 	return (error);
1936 }
1937 
1938 int
1939 sys_aio_suspend(struct thread *td, struct aio_suspend_args *uap)
1940 {
1941 	struct timespec ts, *tsp;
1942 	struct aiocb **ujoblist;
1943 	int error;
1944 
1945 	if (uap->nent < 0 || uap->nent > AIO_LISTIO_MAX)
1946 		return (EINVAL);
1947 
1948 	if (uap->timeout) {
1949 		/* Get timespec struct. */
1950 		if ((error = copyin(uap->timeout, &ts, sizeof(ts))) != 0)
1951 			return (error);
1952 		tsp = &ts;
1953 	} else
1954 		tsp = NULL;
1955 
1956 	ujoblist = uma_zalloc(aiol_zone, M_WAITOK);
1957 	error = copyin(uap->aiocbp, ujoblist, uap->nent * sizeof(ujoblist[0]));
1958 	if (error == 0)
1959 		error = kern_aio_suspend(td, uap->nent, ujoblist, tsp);
1960 	uma_zfree(aiol_zone, ujoblist);
1961 	return (error);
1962 }
1963 
1964 /*
1965  * aio_cancel cancels any non-physio aio operations not currently in
1966  * progress.
1967  */
1968 int
1969 sys_aio_cancel(struct thread *td, struct aio_cancel_args *uap)
1970 {
1971 	struct proc *p = td->td_proc;
1972 	struct kaioinfo *ki;
1973 	struct aiocblist *cbe, *cbn;
1974 	struct file *fp;
1975 	struct socket *so;
1976 	int error;
1977 	int remove;
1978 	int cancelled = 0;
1979 	int notcancelled = 0;
1980 	struct vnode *vp;
1981 
1982 	/* Lookup file object. */
1983 	error = fget(td, uap->fd, 0, &fp);
1984 	if (error)
1985 		return (error);
1986 
1987 	ki = p->p_aioinfo;
1988 	if (ki == NULL)
1989 		goto done;
1990 
1991 	if (fp->f_type == DTYPE_VNODE) {
1992 		vp = fp->f_vnode;
1993 		if (vn_isdisk(vp, &error)) {
1994 			fdrop(fp, td);
1995 			td->td_retval[0] = AIO_NOTCANCELED;
1996 			return (0);
1997 		}
1998 	}
1999 
2000 	AIO_LOCK(ki);
2001 	TAILQ_FOREACH_SAFE(cbe, &ki->kaio_jobqueue, plist, cbn) {
2002 		if ((uap->fd == cbe->uaiocb.aio_fildes) &&
2003 		    ((uap->aiocbp == NULL) ||
2004 		     (uap->aiocbp == cbe->uuaiocb))) {
2005 			remove = 0;
2006 
2007 			mtx_lock(&aio_job_mtx);
2008 			if (cbe->jobstate == JOBST_JOBQGLOBAL) {
2009 				TAILQ_REMOVE(&aio_jobs, cbe, list);
2010 				remove = 1;
2011 			} else if (cbe->jobstate == JOBST_JOBQSOCK) {
2012 				MPASS(fp->f_type == DTYPE_SOCKET);
2013 				so = fp->f_data;
2014 				TAILQ_REMOVE(&so->so_aiojobq, cbe, list);
2015 				remove = 1;
2016 			} else if (cbe->jobstate == JOBST_JOBQSYNC) {
2017 				TAILQ_REMOVE(&ki->kaio_syncqueue, cbe, list);
2018 				remove = 1;
2019 			}
2020 			mtx_unlock(&aio_job_mtx);
2021 
2022 			if (remove) {
2023 				TAILQ_REMOVE(&ki->kaio_jobqueue, cbe, plist);
2024 				cbe->uaiocb._aiocb_private.status = -1;
2025 				cbe->uaiocb._aiocb_private.error = ECANCELED;
2026 				aio_bio_done_notify(p, cbe, DONE_QUEUE);
2027 				cancelled++;
2028 			} else {
2029 				notcancelled++;
2030 			}
2031 			if (uap->aiocbp != NULL)
2032 				break;
2033 		}
2034 	}
2035 	AIO_UNLOCK(ki);
2036 
2037 done:
2038 	fdrop(fp, td);
2039 
2040 	if (uap->aiocbp != NULL) {
2041 		if (cancelled) {
2042 			td->td_retval[0] = AIO_CANCELED;
2043 			return (0);
2044 		}
2045 	}
2046 
2047 	if (notcancelled) {
2048 		td->td_retval[0] = AIO_NOTCANCELED;
2049 		return (0);
2050 	}
2051 
2052 	if (cancelled) {
2053 		td->td_retval[0] = AIO_CANCELED;
2054 		return (0);
2055 	}
2056 
2057 	td->td_retval[0] = AIO_ALLDONE;
2058 
2059 	return (0);
2060 }
2061 
2062 /*
2063  * aio_error is implemented in the kernel level for compatibility purposes
2064  * only.  For a user mode async implementation, it would be best to do it in
2065  * a userland subroutine.
2066  */
2067 static int
2068 kern_aio_error(struct thread *td, struct aiocb *aiocbp, struct aiocb_ops *ops)
2069 {
2070 	struct proc *p = td->td_proc;
2071 	struct aiocblist *cb;
2072 	struct kaioinfo *ki;
2073 	int status;
2074 
2075 	ki = p->p_aioinfo;
2076 	if (ki == NULL) {
2077 		td->td_retval[0] = EINVAL;
2078 		return (0);
2079 	}
2080 
2081 	AIO_LOCK(ki);
2082 	TAILQ_FOREACH(cb, &ki->kaio_all, allist) {
2083 		if (cb->uuaiocb == aiocbp) {
2084 			if (cb->jobstate == JOBST_JOBFINISHED)
2085 				td->td_retval[0] =
2086 					cb->uaiocb._aiocb_private.error;
2087 			else
2088 				td->td_retval[0] = EINPROGRESS;
2089 			AIO_UNLOCK(ki);
2090 			return (0);
2091 		}
2092 	}
2093 	AIO_UNLOCK(ki);
2094 
2095 	/*
2096 	 * Hack for failure of aio_aqueue.
2097 	 */
2098 	status = ops->fetch_status(aiocbp);
2099 	if (status == -1) {
2100 		td->td_retval[0] = ops->fetch_error(aiocbp);
2101 		return (0);
2102 	}
2103 
2104 	td->td_retval[0] = EINVAL;
2105 	return (0);
2106 }
2107 
2108 int
2109 sys_aio_error(struct thread *td, struct aio_error_args *uap)
2110 {
2111 
2112 	return (kern_aio_error(td, uap->aiocbp, &aiocb_ops));
2113 }
2114 
2115 /* syscall - asynchronous read from a file (REALTIME) */
2116 int
2117 sys_oaio_read(struct thread *td, struct oaio_read_args *uap)
2118 {
2119 
2120 	return (aio_aqueue(td, (struct aiocb *)uap->aiocbp, NULL, LIO_READ,
2121 	    &aiocb_ops_osigevent));
2122 }
2123 
2124 int
2125 sys_aio_read(struct thread *td, struct aio_read_args *uap)
2126 {
2127 
2128 	return (aio_aqueue(td, uap->aiocbp, NULL, LIO_READ, &aiocb_ops));
2129 }
2130 
2131 /* syscall - asynchronous write to a file (REALTIME) */
2132 int
2133 sys_oaio_write(struct thread *td, struct oaio_write_args *uap)
2134 {
2135 
2136 	return (aio_aqueue(td, (struct aiocb *)uap->aiocbp, NULL, LIO_WRITE,
2137 	    &aiocb_ops_osigevent));
2138 }
2139 
2140 int
2141 sys_aio_write(struct thread *td, struct aio_write_args *uap)
2142 {
2143 
2144 	return (aio_aqueue(td, uap->aiocbp, NULL, LIO_WRITE, &aiocb_ops));
2145 }
2146 
2147 static int
2148 kern_lio_listio(struct thread *td, int mode, struct aiocb * const *uacb_list,
2149     struct aiocb **acb_list, int nent, struct sigevent *sig,
2150     struct aiocb_ops *ops)
2151 {
2152 	struct proc *p = td->td_proc;
2153 	struct aiocb *iocb;
2154 	struct kaioinfo *ki;
2155 	struct aioliojob *lj;
2156 	struct kevent kev;
2157 	int error;
2158 	int nerror;
2159 	int i;
2160 
2161 	if ((mode != LIO_NOWAIT) && (mode != LIO_WAIT))
2162 		return (EINVAL);
2163 
2164 	if (nent < 0 || nent > AIO_LISTIO_MAX)
2165 		return (EINVAL);
2166 
2167 	if (p->p_aioinfo == NULL)
2168 		aio_init_aioinfo(p);
2169 
2170 	ki = p->p_aioinfo;
2171 
2172 	lj = uma_zalloc(aiolio_zone, M_WAITOK);
2173 	lj->lioj_flags = 0;
2174 	lj->lioj_count = 0;
2175 	lj->lioj_finished_count = 0;
2176 	knlist_init_mtx(&lj->klist, AIO_MTX(ki));
2177 	ksiginfo_init(&lj->lioj_ksi);
2178 
2179 	/*
2180 	 * Setup signal.
2181 	 */
2182 	if (sig && (mode == LIO_NOWAIT)) {
2183 		bcopy(sig, &lj->lioj_signal, sizeof(lj->lioj_signal));
2184 		if (lj->lioj_signal.sigev_notify == SIGEV_KEVENT) {
2185 			/* Assume only new style KEVENT */
2186 			kev.filter = EVFILT_LIO;
2187 			kev.flags = EV_ADD | EV_ENABLE | EV_FLAG1;
2188 			kev.ident = (uintptr_t)uacb_list; /* something unique */
2189 			kev.data = (intptr_t)lj;
2190 			/* pass user defined sigval data */
2191 			kev.udata = lj->lioj_signal.sigev_value.sival_ptr;
2192 			error = kqfd_register(
2193 			    lj->lioj_signal.sigev_notify_kqueue, &kev, td, 1);
2194 			if (error) {
2195 				uma_zfree(aiolio_zone, lj);
2196 				return (error);
2197 			}
2198 		} else if (lj->lioj_signal.sigev_notify == SIGEV_NONE) {
2199 			;
2200 		} else if (lj->lioj_signal.sigev_notify == SIGEV_SIGNAL ||
2201 			   lj->lioj_signal.sigev_notify == SIGEV_THREAD_ID) {
2202 				if (!_SIG_VALID(lj->lioj_signal.sigev_signo)) {
2203 					uma_zfree(aiolio_zone, lj);
2204 					return EINVAL;
2205 				}
2206 				lj->lioj_flags |= LIOJ_SIGNAL;
2207 		} else {
2208 			uma_zfree(aiolio_zone, lj);
2209 			return EINVAL;
2210 		}
2211 	}
2212 
2213 	AIO_LOCK(ki);
2214 	TAILQ_INSERT_TAIL(&ki->kaio_liojoblist, lj, lioj_list);
2215 	/*
2216 	 * Add extra aiocb count to avoid the lio to be freed
2217 	 * by other threads doing aio_waitcomplete or aio_return,
2218 	 * and prevent event from being sent until we have queued
2219 	 * all tasks.
2220 	 */
2221 	lj->lioj_count = 1;
2222 	AIO_UNLOCK(ki);
2223 
2224 	/*
2225 	 * Get pointers to the list of I/O requests.
2226 	 */
2227 	nerror = 0;
2228 	for (i = 0; i < nent; i++) {
2229 		iocb = acb_list[i];
2230 		if (iocb != NULL) {
2231 			error = aio_aqueue(td, iocb, lj, LIO_NOP, ops);
2232 			if (error != 0)
2233 				nerror++;
2234 		}
2235 	}
2236 
2237 	error = 0;
2238 	AIO_LOCK(ki);
2239 	if (mode == LIO_WAIT) {
2240 		while (lj->lioj_count - 1 != lj->lioj_finished_count) {
2241 			ki->kaio_flags |= KAIO_WAKEUP;
2242 			error = msleep(&p->p_aioinfo, AIO_MTX(ki),
2243 			    PRIBIO | PCATCH, "aiospn", 0);
2244 			if (error == ERESTART)
2245 				error = EINTR;
2246 			if (error)
2247 				break;
2248 		}
2249 	} else {
2250 		if (lj->lioj_count - 1 == lj->lioj_finished_count) {
2251 			if (lj->lioj_signal.sigev_notify == SIGEV_KEVENT) {
2252 				lj->lioj_flags |= LIOJ_KEVENT_POSTED;
2253 				KNOTE_LOCKED(&lj->klist, 1);
2254 			}
2255 			if ((lj->lioj_flags & (LIOJ_SIGNAL|LIOJ_SIGNAL_POSTED))
2256 			    == LIOJ_SIGNAL
2257 			    && (lj->lioj_signal.sigev_notify == SIGEV_SIGNAL ||
2258 			    lj->lioj_signal.sigev_notify == SIGEV_THREAD_ID)) {
2259 				aio_sendsig(p, &lj->lioj_signal,
2260 					    &lj->lioj_ksi);
2261 				lj->lioj_flags |= LIOJ_SIGNAL_POSTED;
2262 			}
2263 		}
2264 	}
2265 	lj->lioj_count--;
2266 	if (lj->lioj_count == 0) {
2267 		TAILQ_REMOVE(&ki->kaio_liojoblist, lj, lioj_list);
2268 		knlist_delete(&lj->klist, curthread, 1);
2269 		PROC_LOCK(p);
2270 		sigqueue_take(&lj->lioj_ksi);
2271 		PROC_UNLOCK(p);
2272 		AIO_UNLOCK(ki);
2273 		uma_zfree(aiolio_zone, lj);
2274 	} else
2275 		AIO_UNLOCK(ki);
2276 
2277 	if (nerror)
2278 		return (EIO);
2279 	return (error);
2280 }
2281 
2282 /* syscall - list directed I/O (REALTIME) */
2283 int
2284 sys_olio_listio(struct thread *td, struct olio_listio_args *uap)
2285 {
2286 	struct aiocb **acb_list;
2287 	struct sigevent *sigp, sig;
2288 	struct osigevent osig;
2289 	int error, nent;
2290 
2291 	if ((uap->mode != LIO_NOWAIT) && (uap->mode != LIO_WAIT))
2292 		return (EINVAL);
2293 
2294 	nent = uap->nent;
2295 	if (nent < 0 || nent > AIO_LISTIO_MAX)
2296 		return (EINVAL);
2297 
2298 	if (uap->sig && (uap->mode == LIO_NOWAIT)) {
2299 		error = copyin(uap->sig, &osig, sizeof(osig));
2300 		if (error)
2301 			return (error);
2302 		error = convert_old_sigevent(&osig, &sig);
2303 		if (error)
2304 			return (error);
2305 		sigp = &sig;
2306 	} else
2307 		sigp = NULL;
2308 
2309 	acb_list = malloc(sizeof(struct aiocb *) * nent, M_LIO, M_WAITOK);
2310 	error = copyin(uap->acb_list, acb_list, nent * sizeof(acb_list[0]));
2311 	if (error == 0)
2312 		error = kern_lio_listio(td, uap->mode,
2313 		    (struct aiocb * const *)uap->acb_list, acb_list, nent, sigp,
2314 		    &aiocb_ops_osigevent);
2315 	free(acb_list, M_LIO);
2316 	return (error);
2317 }
2318 
2319 /* syscall - list directed I/O (REALTIME) */
2320 int
2321 sys_lio_listio(struct thread *td, struct lio_listio_args *uap)
2322 {
2323 	struct aiocb **acb_list;
2324 	struct sigevent *sigp, sig;
2325 	int error, nent;
2326 
2327 	if ((uap->mode != LIO_NOWAIT) && (uap->mode != LIO_WAIT))
2328 		return (EINVAL);
2329 
2330 	nent = uap->nent;
2331 	if (nent < 0 || nent > AIO_LISTIO_MAX)
2332 		return (EINVAL);
2333 
2334 	if (uap->sig && (uap->mode == LIO_NOWAIT)) {
2335 		error = copyin(uap->sig, &sig, sizeof(sig));
2336 		if (error)
2337 			return (error);
2338 		sigp = &sig;
2339 	} else
2340 		sigp = NULL;
2341 
2342 	acb_list = malloc(sizeof(struct aiocb *) * nent, M_LIO, M_WAITOK);
2343 	error = copyin(uap->acb_list, acb_list, nent * sizeof(acb_list[0]));
2344 	if (error == 0)
2345 		error = kern_lio_listio(td, uap->mode, uap->acb_list, acb_list,
2346 		    nent, sigp, &aiocb_ops);
2347 	free(acb_list, M_LIO);
2348 	return (error);
2349 }
2350 
2351 /*
2352  * Called from interrupt thread for physio, we should return as fast
2353  * as possible, so we schedule a biohelper task.
2354  */
2355 static void
2356 aio_physwakeup(struct buf *bp)
2357 {
2358 	struct aiocblist *aiocbe;
2359 
2360 	aiocbe = (struct aiocblist *)bp->b_caller1;
2361 	taskqueue_enqueue(taskqueue_aiod_bio, &aiocbe->biotask);
2362 }
2363 
2364 /*
2365  * Task routine to perform heavy tasks, process wakeup, and signals.
2366  */
2367 static void
2368 biohelper(void *context, int pending)
2369 {
2370 	struct aiocblist *aiocbe = context;
2371 	struct buf *bp;
2372 	struct proc *userp;
2373 	struct kaioinfo *ki;
2374 	int nblks;
2375 
2376 	bp = aiocbe->bp;
2377 	userp = aiocbe->userproc;
2378 	ki = userp->p_aioinfo;
2379 	AIO_LOCK(ki);
2380 	aiocbe->uaiocb._aiocb_private.status -= bp->b_resid;
2381 	aiocbe->uaiocb._aiocb_private.error = 0;
2382 	if (bp->b_ioflags & BIO_ERROR)
2383 		aiocbe->uaiocb._aiocb_private.error = bp->b_error;
2384 	nblks = btodb(aiocbe->uaiocb.aio_nbytes);
2385 	if (aiocbe->uaiocb.aio_lio_opcode == LIO_WRITE)
2386 		aiocbe->outputcharge += nblks;
2387 	else
2388 		aiocbe->inputcharge += nblks;
2389 	aiocbe->bp = NULL;
2390 	TAILQ_REMOVE(&userp->p_aioinfo->kaio_bufqueue, aiocbe, plist);
2391 	ki->kaio_buffer_count--;
2392 	aio_bio_done_notify(userp, aiocbe, DONE_BUF);
2393 	AIO_UNLOCK(ki);
2394 
2395 	/* Release mapping into kernel space. */
2396 	vunmapbuf(bp);
2397 	relpbuf(bp, NULL);
2398 	atomic_subtract_int(&num_buf_aio, 1);
2399 }
2400 
2401 /* syscall - wait for the next completion of an aio request */
2402 static int
2403 kern_aio_waitcomplete(struct thread *td, struct aiocb **aiocbp,
2404     struct timespec *ts, struct aiocb_ops *ops)
2405 {
2406 	struct proc *p = td->td_proc;
2407 	struct timeval atv;
2408 	struct kaioinfo *ki;
2409 	struct aiocblist *cb;
2410 	struct aiocb *uuaiocb;
2411 	int error, status, timo;
2412 
2413 	ops->store_aiocb(aiocbp, NULL);
2414 
2415 	timo = 0;
2416 	if (ts) {
2417 		if ((ts->tv_nsec < 0) || (ts->tv_nsec >= 1000000000))
2418 			return (EINVAL);
2419 
2420 		TIMESPEC_TO_TIMEVAL(&atv, ts);
2421 		if (itimerfix(&atv))
2422 			return (EINVAL);
2423 		timo = tvtohz(&atv);
2424 	}
2425 
2426 	if (p->p_aioinfo == NULL)
2427 		aio_init_aioinfo(p);
2428 	ki = p->p_aioinfo;
2429 
2430 	error = 0;
2431 	cb = NULL;
2432 	AIO_LOCK(ki);
2433 	while ((cb = TAILQ_FIRST(&ki->kaio_done)) == NULL) {
2434 		ki->kaio_flags |= KAIO_WAKEUP;
2435 		error = msleep(&p->p_aioinfo, AIO_MTX(ki), PRIBIO | PCATCH,
2436 		    "aiowc", timo);
2437 		if (timo && error == ERESTART)
2438 			error = EINTR;
2439 		if (error)
2440 			break;
2441 	}
2442 
2443 	if (cb != NULL) {
2444 		MPASS(cb->jobstate == JOBST_JOBFINISHED);
2445 		uuaiocb = cb->uuaiocb;
2446 		status = cb->uaiocb._aiocb_private.status;
2447 		error = cb->uaiocb._aiocb_private.error;
2448 		td->td_retval[0] = status;
2449 		if (cb->uaiocb.aio_lio_opcode == LIO_WRITE) {
2450 			td->td_ru.ru_oublock += cb->outputcharge;
2451 			cb->outputcharge = 0;
2452 		} else if (cb->uaiocb.aio_lio_opcode == LIO_READ) {
2453 			td->td_ru.ru_inblock += cb->inputcharge;
2454 			cb->inputcharge = 0;
2455 		}
2456 		aio_free_entry(cb);
2457 		AIO_UNLOCK(ki);
2458 		ops->store_aiocb(aiocbp, uuaiocb);
2459 		ops->store_error(uuaiocb, error);
2460 		ops->store_status(uuaiocb, status);
2461 	} else
2462 		AIO_UNLOCK(ki);
2463 
2464 	return (error);
2465 }
2466 
2467 int
2468 sys_aio_waitcomplete(struct thread *td, struct aio_waitcomplete_args *uap)
2469 {
2470 	struct timespec ts, *tsp;
2471 	int error;
2472 
2473 	if (uap->timeout) {
2474 		/* Get timespec struct. */
2475 		error = copyin(uap->timeout, &ts, sizeof(ts));
2476 		if (error)
2477 			return (error);
2478 		tsp = &ts;
2479 	} else
2480 		tsp = NULL;
2481 
2482 	return (kern_aio_waitcomplete(td, uap->aiocbp, tsp, &aiocb_ops));
2483 }
2484 
2485 static int
2486 kern_aio_fsync(struct thread *td, int op, struct aiocb *aiocbp,
2487     struct aiocb_ops *ops)
2488 {
2489 	struct proc *p = td->td_proc;
2490 	struct kaioinfo *ki;
2491 
2492 	if (op != O_SYNC) /* XXX lack of O_DSYNC */
2493 		return (EINVAL);
2494 	ki = p->p_aioinfo;
2495 	if (ki == NULL)
2496 		aio_init_aioinfo(p);
2497 	return (aio_aqueue(td, aiocbp, NULL, LIO_SYNC, ops));
2498 }
2499 
2500 int
2501 sys_aio_fsync(struct thread *td, struct aio_fsync_args *uap)
2502 {
2503 
2504 	return (kern_aio_fsync(td, uap->op, uap->aiocbp, &aiocb_ops));
2505 }
2506 
2507 /* kqueue attach function */
2508 static int
2509 filt_aioattach(struct knote *kn)
2510 {
2511 	struct aiocblist *aiocbe = (struct aiocblist *)kn->kn_sdata;
2512 
2513 	/*
2514 	 * The aiocbe pointer must be validated before using it, so
2515 	 * registration is restricted to the kernel; the user cannot
2516 	 * set EV_FLAG1.
2517 	 */
2518 	if ((kn->kn_flags & EV_FLAG1) == 0)
2519 		return (EPERM);
2520 	kn->kn_ptr.p_aio = aiocbe;
2521 	kn->kn_flags &= ~EV_FLAG1;
2522 
2523 	knlist_add(&aiocbe->klist, kn, 0);
2524 
2525 	return (0);
2526 }
2527 
2528 /* kqueue detach function */
2529 static void
2530 filt_aiodetach(struct knote *kn)
2531 {
2532 	struct aiocblist *aiocbe = kn->kn_ptr.p_aio;
2533 
2534 	if (!knlist_empty(&aiocbe->klist))
2535 		knlist_remove(&aiocbe->klist, kn, 0);
2536 }
2537 
2538 /* kqueue filter function */
2539 /*ARGSUSED*/
2540 static int
2541 filt_aio(struct knote *kn, long hint)
2542 {
2543 	struct aiocblist *aiocbe = kn->kn_ptr.p_aio;
2544 
2545 	kn->kn_data = aiocbe->uaiocb._aiocb_private.error;
2546 	if (aiocbe->jobstate != JOBST_JOBFINISHED)
2547 		return (0);
2548 	kn->kn_flags |= EV_EOF;
2549 	return (1);
2550 }
2551 
2552 /* kqueue attach function */
2553 static int
2554 filt_lioattach(struct knote *kn)
2555 {
2556 	struct aioliojob * lj = (struct aioliojob *)kn->kn_sdata;
2557 
2558 	/*
2559 	 * The aioliojob pointer must be validated before using it, so
2560 	 * registration is restricted to the kernel; the user cannot
2561 	 * set EV_FLAG1.
2562 	 */
2563 	if ((kn->kn_flags & EV_FLAG1) == 0)
2564 		return (EPERM);
2565 	kn->kn_ptr.p_lio = lj;
2566 	kn->kn_flags &= ~EV_FLAG1;
2567 
2568 	knlist_add(&lj->klist, kn, 0);
2569 
2570 	return (0);
2571 }
2572 
2573 /* kqueue detach function */
2574 static void
2575 filt_liodetach(struct knote *kn)
2576 {
2577 	struct aioliojob * lj = kn->kn_ptr.p_lio;
2578 
2579 	if (!knlist_empty(&lj->klist))
2580 		knlist_remove(&lj->klist, kn, 0);
2581 }
2582 
2583 /* kqueue filter function */
2584 /*ARGSUSED*/
2585 static int
2586 filt_lio(struct knote *kn, long hint)
2587 {
2588 	struct aioliojob * lj = kn->kn_ptr.p_lio;
2589 
2590 	return (lj->lioj_flags & LIOJ_KEVENT_POSTED);
2591 }
2592 
2593 #ifdef COMPAT_FREEBSD32
2594 
2595 struct __aiocb_private32 {
2596 	int32_t	status;
2597 	int32_t	error;
2598 	uint32_t kernelinfo;
2599 };
2600 
2601 typedef struct oaiocb32 {
2602 	int	aio_fildes;		/* File descriptor */
2603 	uint64_t aio_offset __packed;	/* File offset for I/O */
2604 	uint32_t aio_buf;		/* I/O buffer in process space */
2605 	uint32_t aio_nbytes;		/* Number of bytes for I/O */
2606 	struct	osigevent32 aio_sigevent; /* Signal to deliver */
2607 	int	aio_lio_opcode;		/* LIO opcode */
2608 	int	aio_reqprio;		/* Request priority -- ignored */
2609 	struct	__aiocb_private32 _aiocb_private;
2610 } oaiocb32_t;
2611 
2612 typedef struct aiocb32 {
2613 	int32_t	aio_fildes;		/* File descriptor */
2614 	uint64_t aio_offset __packed;	/* File offset for I/O */
2615 	uint32_t aio_buf;		/* I/O buffer in process space */
2616 	uint32_t aio_nbytes;		/* Number of bytes for I/O */
2617 	int	__spare__[2];
2618 	uint32_t __spare2__;
2619 	int	aio_lio_opcode;		/* LIO opcode */
2620 	int	aio_reqprio;		/* Request priority -- ignored */
2621 	struct __aiocb_private32 _aiocb_private;
2622 	struct sigevent32 aio_sigevent;	/* Signal to deliver */
2623 } aiocb32_t;
2624 
2625 static int
2626 convert_old_sigevent32(struct osigevent32 *osig, struct sigevent *nsig)
2627 {
2628 
2629 	/*
2630 	 * Only SIGEV_NONE, SIGEV_SIGNAL, and SIGEV_KEVENT are
2631 	 * supported by AIO with the old sigevent structure.
2632 	 */
2633 	CP(*osig, *nsig, sigev_notify);
2634 	switch (nsig->sigev_notify) {
2635 	case SIGEV_NONE:
2636 		break;
2637 	case SIGEV_SIGNAL:
2638 		nsig->sigev_signo = osig->__sigev_u.__sigev_signo;
2639 		break;
2640 	case SIGEV_KEVENT:
2641 		nsig->sigev_notify_kqueue =
2642 		    osig->__sigev_u.__sigev_notify_kqueue;
2643 		PTRIN_CP(*osig, *nsig, sigev_value.sival_ptr);
2644 		break;
2645 	default:
2646 		return (EINVAL);
2647 	}
2648 	return (0);
2649 }
2650 
2651 static int
2652 aiocb32_copyin_old_sigevent(struct aiocb *ujob, struct aiocb *kjob)
2653 {
2654 	struct oaiocb32 job32;
2655 	int error;
2656 
2657 	bzero(kjob, sizeof(struct aiocb));
2658 	error = copyin(ujob, &job32, sizeof(job32));
2659 	if (error)
2660 		return (error);
2661 
2662 	CP(job32, *kjob, aio_fildes);
2663 	CP(job32, *kjob, aio_offset);
2664 	PTRIN_CP(job32, *kjob, aio_buf);
2665 	CP(job32, *kjob, aio_nbytes);
2666 	CP(job32, *kjob, aio_lio_opcode);
2667 	CP(job32, *kjob, aio_reqprio);
2668 	CP(job32, *kjob, _aiocb_private.status);
2669 	CP(job32, *kjob, _aiocb_private.error);
2670 	PTRIN_CP(job32, *kjob, _aiocb_private.kernelinfo);
2671 	return (convert_old_sigevent32(&job32.aio_sigevent,
2672 	    &kjob->aio_sigevent));
2673 }
2674 
2675 static int
2676 convert_sigevent32(struct sigevent32 *sig32, struct sigevent *sig)
2677 {
2678 
2679 	CP(*sig32, *sig, sigev_notify);
2680 	switch (sig->sigev_notify) {
2681 	case SIGEV_NONE:
2682 		break;
2683 	case SIGEV_THREAD_ID:
2684 		CP(*sig32, *sig, sigev_notify_thread_id);
2685 		/* FALLTHROUGH */
2686 	case SIGEV_SIGNAL:
2687 		CP(*sig32, *sig, sigev_signo);
2688 		break;
2689 	case SIGEV_KEVENT:
2690 		CP(*sig32, *sig, sigev_notify_kqueue);
2691 		PTRIN_CP(*sig32, *sig, sigev_value.sival_ptr);
2692 		break;
2693 	default:
2694 		return (EINVAL);
2695 	}
2696 	return (0);
2697 }
2698 
2699 static int
2700 aiocb32_copyin(struct aiocb *ujob, struct aiocb *kjob)
2701 {
2702 	struct aiocb32 job32;
2703 	int error;
2704 
2705 	error = copyin(ujob, &job32, sizeof(job32));
2706 	if (error)
2707 		return (error);
2708 	CP(job32, *kjob, aio_fildes);
2709 	CP(job32, *kjob, aio_offset);
2710 	PTRIN_CP(job32, *kjob, aio_buf);
2711 	CP(job32, *kjob, aio_nbytes);
2712 	CP(job32, *kjob, aio_lio_opcode);
2713 	CP(job32, *kjob, aio_reqprio);
2714 	CP(job32, *kjob, _aiocb_private.status);
2715 	CP(job32, *kjob, _aiocb_private.error);
2716 	PTRIN_CP(job32, *kjob, _aiocb_private.kernelinfo);
2717 	return (convert_sigevent32(&job32.aio_sigevent, &kjob->aio_sigevent));
2718 }
2719 
2720 static long
2721 aiocb32_fetch_status(struct aiocb *ujob)
2722 {
2723 	struct aiocb32 *ujob32;
2724 
2725 	ujob32 = (struct aiocb32 *)ujob;
2726 	return (fuword32(&ujob32->_aiocb_private.status));
2727 }
2728 
2729 static long
2730 aiocb32_fetch_error(struct aiocb *ujob)
2731 {
2732 	struct aiocb32 *ujob32;
2733 
2734 	ujob32 = (struct aiocb32 *)ujob;
2735 	return (fuword32(&ujob32->_aiocb_private.error));
2736 }
2737 
2738 static int
2739 aiocb32_store_status(struct aiocb *ujob, long status)
2740 {
2741 	struct aiocb32 *ujob32;
2742 
2743 	ujob32 = (struct aiocb32 *)ujob;
2744 	return (suword32(&ujob32->_aiocb_private.status, status));
2745 }
2746 
2747 static int
2748 aiocb32_store_error(struct aiocb *ujob, long error)
2749 {
2750 	struct aiocb32 *ujob32;
2751 
2752 	ujob32 = (struct aiocb32 *)ujob;
2753 	return (suword32(&ujob32->_aiocb_private.error, error));
2754 }
2755 
2756 static int
2757 aiocb32_store_kernelinfo(struct aiocb *ujob, long jobref)
2758 {
2759 	struct aiocb32 *ujob32;
2760 
2761 	ujob32 = (struct aiocb32 *)ujob;
2762 	return (suword32(&ujob32->_aiocb_private.kernelinfo, jobref));
2763 }
2764 
2765 static int
2766 aiocb32_store_aiocb(struct aiocb **ujobp, struct aiocb *ujob)
2767 {
2768 
2769 	return (suword32(ujobp, (long)ujob));
2770 }
2771 
2772 static struct aiocb_ops aiocb32_ops = {
2773 	.copyin = aiocb32_copyin,
2774 	.fetch_status = aiocb32_fetch_status,
2775 	.fetch_error = aiocb32_fetch_error,
2776 	.store_status = aiocb32_store_status,
2777 	.store_error = aiocb32_store_error,
2778 	.store_kernelinfo = aiocb32_store_kernelinfo,
2779 	.store_aiocb = aiocb32_store_aiocb,
2780 };
2781 
2782 static struct aiocb_ops aiocb32_ops_osigevent = {
2783 	.copyin = aiocb32_copyin_old_sigevent,
2784 	.fetch_status = aiocb32_fetch_status,
2785 	.fetch_error = aiocb32_fetch_error,
2786 	.store_status = aiocb32_store_status,
2787 	.store_error = aiocb32_store_error,
2788 	.store_kernelinfo = aiocb32_store_kernelinfo,
2789 	.store_aiocb = aiocb32_store_aiocb,
2790 };
2791 
2792 int
2793 freebsd32_aio_return(struct thread *td, struct freebsd32_aio_return_args *uap)
2794 {
2795 
2796 	return (kern_aio_return(td, (struct aiocb *)uap->aiocbp, &aiocb32_ops));
2797 }
2798 
2799 int
2800 freebsd32_aio_suspend(struct thread *td, struct freebsd32_aio_suspend_args *uap)
2801 {
2802 	struct timespec32 ts32;
2803 	struct timespec ts, *tsp;
2804 	struct aiocb **ujoblist;
2805 	uint32_t *ujoblist32;
2806 	int error, i;
2807 
2808 	if (uap->nent < 0 || uap->nent > AIO_LISTIO_MAX)
2809 		return (EINVAL);
2810 
2811 	if (uap->timeout) {
2812 		/* Get timespec struct. */
2813 		if ((error = copyin(uap->timeout, &ts32, sizeof(ts32))) != 0)
2814 			return (error);
2815 		CP(ts32, ts, tv_sec);
2816 		CP(ts32, ts, tv_nsec);
2817 		tsp = &ts;
2818 	} else
2819 		tsp = NULL;
2820 
2821 	ujoblist = uma_zalloc(aiol_zone, M_WAITOK);
2822 	ujoblist32 = (uint32_t *)ujoblist;
2823 	error = copyin(uap->aiocbp, ujoblist32, uap->nent *
2824 	    sizeof(ujoblist32[0]));
2825 	if (error == 0) {
2826 		for (i = uap->nent; i > 0; i--)
2827 			ujoblist[i] = PTRIN(ujoblist32[i]);
2828 
2829 		error = kern_aio_suspend(td, uap->nent, ujoblist, tsp);
2830 	}
2831 	uma_zfree(aiol_zone, ujoblist);
2832 	return (error);
2833 }
2834 
2835 int
2836 freebsd32_aio_cancel(struct thread *td, struct freebsd32_aio_cancel_args *uap)
2837 {
2838 
2839 	return (sys_aio_cancel(td, (struct aio_cancel_args *)uap));
2840 }
2841 
2842 int
2843 freebsd32_aio_error(struct thread *td, struct freebsd32_aio_error_args *uap)
2844 {
2845 
2846 	return (kern_aio_error(td, (struct aiocb *)uap->aiocbp, &aiocb32_ops));
2847 }
2848 
2849 int
2850 freebsd32_oaio_read(struct thread *td, struct freebsd32_oaio_read_args *uap)
2851 {
2852 
2853 	return (aio_aqueue(td, (struct aiocb *)uap->aiocbp, NULL, LIO_READ,
2854 	    &aiocb32_ops_osigevent));
2855 }
2856 
2857 int
2858 freebsd32_aio_read(struct thread *td, struct freebsd32_aio_read_args *uap)
2859 {
2860 
2861 	return (aio_aqueue(td, (struct aiocb *)uap->aiocbp, NULL, LIO_READ,
2862 	    &aiocb32_ops));
2863 }
2864 
2865 int
2866 freebsd32_oaio_write(struct thread *td, struct freebsd32_oaio_write_args *uap)
2867 {
2868 
2869 	return (aio_aqueue(td, (struct aiocb *)uap->aiocbp, NULL, LIO_WRITE,
2870 	    &aiocb32_ops_osigevent));
2871 }
2872 
2873 int
2874 freebsd32_aio_write(struct thread *td, struct freebsd32_aio_write_args *uap)
2875 {
2876 
2877 	return (aio_aqueue(td, (struct aiocb *)uap->aiocbp, NULL, LIO_WRITE,
2878 	    &aiocb32_ops));
2879 }
2880 
2881 int
2882 freebsd32_aio_waitcomplete(struct thread *td,
2883     struct freebsd32_aio_waitcomplete_args *uap)
2884 {
2885 	struct timespec32 ts32;
2886 	struct timespec ts, *tsp;
2887 	int error;
2888 
2889 	if (uap->timeout) {
2890 		/* Get timespec struct. */
2891 		error = copyin(uap->timeout, &ts32, sizeof(ts32));
2892 		if (error)
2893 			return (error);
2894 		CP(ts32, ts, tv_sec);
2895 		CP(ts32, ts, tv_nsec);
2896 		tsp = &ts;
2897 	} else
2898 		tsp = NULL;
2899 
2900 	return (kern_aio_waitcomplete(td, (struct aiocb **)uap->aiocbp, tsp,
2901 	    &aiocb32_ops));
2902 }
2903 
2904 int
2905 freebsd32_aio_fsync(struct thread *td, struct freebsd32_aio_fsync_args *uap)
2906 {
2907 
2908 	return (kern_aio_fsync(td, uap->op, (struct aiocb *)uap->aiocbp,
2909 	    &aiocb32_ops));
2910 }
2911 
2912 int
2913 freebsd32_olio_listio(struct thread *td, struct freebsd32_olio_listio_args *uap)
2914 {
2915 	struct aiocb **acb_list;
2916 	struct sigevent *sigp, sig;
2917 	struct osigevent32 osig;
2918 	uint32_t *acb_list32;
2919 	int error, i, nent;
2920 
2921 	if ((uap->mode != LIO_NOWAIT) && (uap->mode != LIO_WAIT))
2922 		return (EINVAL);
2923 
2924 	nent = uap->nent;
2925 	if (nent < 0 || nent > AIO_LISTIO_MAX)
2926 		return (EINVAL);
2927 
2928 	if (uap->sig && (uap->mode == LIO_NOWAIT)) {
2929 		error = copyin(uap->sig, &osig, sizeof(osig));
2930 		if (error)
2931 			return (error);
2932 		error = convert_old_sigevent32(&osig, &sig);
2933 		if (error)
2934 			return (error);
2935 		sigp = &sig;
2936 	} else
2937 		sigp = NULL;
2938 
2939 	acb_list32 = malloc(sizeof(uint32_t) * nent, M_LIO, M_WAITOK);
2940 	error = copyin(uap->acb_list, acb_list32, nent * sizeof(uint32_t));
2941 	if (error) {
2942 		free(acb_list32, M_LIO);
2943 		return (error);
2944 	}
2945 	acb_list = malloc(sizeof(struct aiocb *) * nent, M_LIO, M_WAITOK);
2946 	for (i = 0; i < nent; i++)
2947 		acb_list[i] = PTRIN(acb_list32[i]);
2948 	free(acb_list32, M_LIO);
2949 
2950 	error = kern_lio_listio(td, uap->mode,
2951 	    (struct aiocb * const *)uap->acb_list, acb_list, nent, sigp,
2952 	    &aiocb32_ops_osigevent);
2953 	free(acb_list, M_LIO);
2954 	return (error);
2955 }
2956 
2957 int
2958 freebsd32_lio_listio(struct thread *td, struct freebsd32_lio_listio_args *uap)
2959 {
2960 	struct aiocb **acb_list;
2961 	struct sigevent *sigp, sig;
2962 	struct sigevent32 sig32;
2963 	uint32_t *acb_list32;
2964 	int error, i, nent;
2965 
2966 	if ((uap->mode != LIO_NOWAIT) && (uap->mode != LIO_WAIT))
2967 		return (EINVAL);
2968 
2969 	nent = uap->nent;
2970 	if (nent < 0 || nent > AIO_LISTIO_MAX)
2971 		return (EINVAL);
2972 
2973 	if (uap->sig && (uap->mode == LIO_NOWAIT)) {
2974 		error = copyin(uap->sig, &sig32, sizeof(sig32));
2975 		if (error)
2976 			return (error);
2977 		error = convert_sigevent32(&sig32, &sig);
2978 		if (error)
2979 			return (error);
2980 		sigp = &sig;
2981 	} else
2982 		sigp = NULL;
2983 
2984 	acb_list32 = malloc(sizeof(uint32_t) * nent, M_LIO, M_WAITOK);
2985 	error = copyin(uap->acb_list, acb_list32, nent * sizeof(uint32_t));
2986 	if (error) {
2987 		free(acb_list32, M_LIO);
2988 		return (error);
2989 	}
2990 	acb_list = malloc(sizeof(struct aiocb *) * nent, M_LIO, M_WAITOK);
2991 	for (i = 0; i < nent; i++)
2992 		acb_list[i] = PTRIN(acb_list32[i]);
2993 	free(acb_list32, M_LIO);
2994 
2995 	error = kern_lio_listio(td, uap->mode,
2996 	    (struct aiocb * const *)uap->acb_list, acb_list, nent, sigp,
2997 	    &aiocb32_ops);
2998 	free(acb_list, M_LIO);
2999 	return (error);
3000 }
3001 
3002 #endif
3003