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