xref: /freebsd/sys/kern/vfs_aio.c (revision ca27f0cef04fe67c812aae3568211798f52f28ee)
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 	u_short evflags;
1528 
1529 	if (p->p_aioinfo == NULL)
1530 		aio_init_aioinfo(p);
1531 
1532 	ki = p->p_aioinfo;
1533 
1534 	ops->store_status(job, -1);
1535 	ops->store_error(job, 0);
1536 	ops->store_kernelinfo(job, -1);
1537 
1538 	if (num_queue_count >= max_queue_count ||
1539 	    ki->kaio_count >= ki->kaio_qallowed_count) {
1540 		ops->store_error(job, EAGAIN);
1541 		return (EAGAIN);
1542 	}
1543 
1544 	aiocbe = uma_zalloc(aiocb_zone, M_WAITOK | M_ZERO);
1545 	aiocbe->inputcharge = 0;
1546 	aiocbe->outputcharge = 0;
1547 	knlist_init_mtx(&aiocbe->klist, AIO_MTX(ki));
1548 
1549 	error = ops->copyin(job, &aiocbe->uaiocb);
1550 	if (error) {
1551 		ops->store_error(job, error);
1552 		uma_zfree(aiocb_zone, aiocbe);
1553 		return (error);
1554 	}
1555 
1556 	/* XXX: aio_nbytes is later casted to signed types. */
1557 	if (aiocbe->uaiocb.aio_nbytes > INT_MAX) {
1558 		uma_zfree(aiocb_zone, aiocbe);
1559 		return (EINVAL);
1560 	}
1561 
1562 	if (aiocbe->uaiocb.aio_sigevent.sigev_notify != SIGEV_KEVENT &&
1563 	    aiocbe->uaiocb.aio_sigevent.sigev_notify != SIGEV_SIGNAL &&
1564 	    aiocbe->uaiocb.aio_sigevent.sigev_notify != SIGEV_THREAD_ID &&
1565 	    aiocbe->uaiocb.aio_sigevent.sigev_notify != SIGEV_NONE) {
1566 		ops->store_error(job, EINVAL);
1567 		uma_zfree(aiocb_zone, aiocbe);
1568 		return (EINVAL);
1569 	}
1570 
1571 	if ((aiocbe->uaiocb.aio_sigevent.sigev_notify == SIGEV_SIGNAL ||
1572 	     aiocbe->uaiocb.aio_sigevent.sigev_notify == SIGEV_THREAD_ID) &&
1573 		!_SIG_VALID(aiocbe->uaiocb.aio_sigevent.sigev_signo)) {
1574 		uma_zfree(aiocb_zone, aiocbe);
1575 		return (EINVAL);
1576 	}
1577 
1578 	ksiginfo_init(&aiocbe->ksi);
1579 
1580 	/* Save userspace address of the job info. */
1581 	aiocbe->uuaiocb = job;
1582 
1583 	/* Get the opcode. */
1584 	if (type != LIO_NOP)
1585 		aiocbe->uaiocb.aio_lio_opcode = type;
1586 	opcode = aiocbe->uaiocb.aio_lio_opcode;
1587 
1588 	/*
1589 	 * Validate the opcode and fetch the file object for the specified
1590 	 * file descriptor.
1591 	 *
1592 	 * XXXRW: Moved the opcode validation up here so that we don't
1593 	 * retrieve a file descriptor without knowing what the capabiltity
1594 	 * should be.
1595 	 */
1596 	fd = aiocbe->uaiocb.aio_fildes;
1597 	switch (opcode) {
1598 	case LIO_WRITE:
1599 		error = fget_write(td, fd, CAP_WRITE | CAP_SEEK, &fp);
1600 		break;
1601 	case LIO_READ:
1602 		error = fget_read(td, fd, CAP_READ | CAP_SEEK, &fp);
1603 		break;
1604 	case LIO_SYNC:
1605 		error = fget(td, fd, CAP_FSYNC, &fp);
1606 		break;
1607 	case LIO_NOP:
1608 		error = fget(td, fd, 0, &fp);
1609 		break;
1610 	default:
1611 		error = EINVAL;
1612 	}
1613 	if (error) {
1614 		uma_zfree(aiocb_zone, aiocbe);
1615 		ops->store_error(job, error);
1616 		return (error);
1617 	}
1618 
1619 	if (opcode == LIO_SYNC && fp->f_vnode == NULL) {
1620 		error = EINVAL;
1621 		goto aqueue_fail;
1622 	}
1623 
1624 	if (opcode != LIO_SYNC && aiocbe->uaiocb.aio_offset == -1LL) {
1625 		error = EINVAL;
1626 		goto aqueue_fail;
1627 	}
1628 
1629 	aiocbe->fd_file = fp;
1630 
1631 	mtx_lock(&aio_job_mtx);
1632 	jid = jobrefid++;
1633 	aiocbe->seqno = jobseqno++;
1634 	mtx_unlock(&aio_job_mtx);
1635 	error = ops->store_kernelinfo(job, jid);
1636 	if (error) {
1637 		error = EINVAL;
1638 		goto aqueue_fail;
1639 	}
1640 	aiocbe->uaiocb._aiocb_private.kernelinfo = (void *)(intptr_t)jid;
1641 
1642 	if (opcode == LIO_NOP) {
1643 		fdrop(fp, td);
1644 		uma_zfree(aiocb_zone, aiocbe);
1645 		return (0);
1646 	}
1647 
1648 	if (aiocbe->uaiocb.aio_sigevent.sigev_notify != SIGEV_KEVENT)
1649 		goto no_kqueue;
1650 	evflags = aiocbe->uaiocb.aio_sigevent.sigev_notify_kevent_flags;
1651 	if ((evflags & ~(EV_CLEAR | EV_DISPATCH | EV_ONESHOT)) != 0) {
1652 		error = EINVAL;
1653 		goto aqueue_fail;
1654 	}
1655 	kqfd = aiocbe->uaiocb.aio_sigevent.sigev_notify_kqueue;
1656 	kev.ident = (uintptr_t)aiocbe->uuaiocb;
1657 	kev.filter = EVFILT_AIO;
1658 	kev.flags = EV_ADD | EV_ENABLE | EV_FLAG1 | evflags;
1659 	kev.data = (intptr_t)aiocbe;
1660 	kev.udata = aiocbe->uaiocb.aio_sigevent.sigev_value.sival_ptr;
1661 	error = kqfd_register(kqfd, &kev, td, 1);
1662 aqueue_fail:
1663 	if (error) {
1664 		fdrop(fp, td);
1665 		uma_zfree(aiocb_zone, aiocbe);
1666 		ops->store_error(job, error);
1667 		goto done;
1668 	}
1669 no_kqueue:
1670 
1671 	ops->store_error(job, EINPROGRESS);
1672 	aiocbe->uaiocb._aiocb_private.error = EINPROGRESS;
1673 	aiocbe->userproc = p;
1674 	aiocbe->cred = crhold(td->td_ucred);
1675 	aiocbe->jobflags = 0;
1676 	aiocbe->lio = lj;
1677 
1678 	if (opcode == LIO_SYNC)
1679 		goto queueit;
1680 
1681 	if (fp->f_type == DTYPE_SOCKET) {
1682 		/*
1683 		 * Alternate queueing for socket ops: Reach down into the
1684 		 * descriptor to get the socket data.  Then check to see if the
1685 		 * socket is ready to be read or written (based on the requested
1686 		 * operation).
1687 		 *
1688 		 * If it is not ready for io, then queue the aiocbe on the
1689 		 * socket, and set the flags so we get a call when sbnotify()
1690 		 * happens.
1691 		 *
1692 		 * Note if opcode is neither LIO_WRITE nor LIO_READ we lock
1693 		 * and unlock the snd sockbuf for no reason.
1694 		 */
1695 		so = fp->f_data;
1696 		sb = (opcode == LIO_READ) ? &so->so_rcv : &so->so_snd;
1697 		SOCKBUF_LOCK(sb);
1698 		if (((opcode == LIO_READ) && (!soreadable(so))) || ((opcode ==
1699 		    LIO_WRITE) && (!sowriteable(so)))) {
1700 			sb->sb_flags |= SB_AIO;
1701 
1702 			mtx_lock(&aio_job_mtx);
1703 			TAILQ_INSERT_TAIL(&so->so_aiojobq, aiocbe, list);
1704 			mtx_unlock(&aio_job_mtx);
1705 
1706 			AIO_LOCK(ki);
1707 			TAILQ_INSERT_TAIL(&ki->kaio_all, aiocbe, allist);
1708 			TAILQ_INSERT_TAIL(&ki->kaio_jobqueue, aiocbe, plist);
1709 			aiocbe->jobstate = JOBST_JOBQSOCK;
1710 			ki->kaio_count++;
1711 			if (lj)
1712 				lj->lioj_count++;
1713 			AIO_UNLOCK(ki);
1714 			SOCKBUF_UNLOCK(sb);
1715 			atomic_add_int(&num_queue_count, 1);
1716 			error = 0;
1717 			goto done;
1718 		}
1719 		SOCKBUF_UNLOCK(sb);
1720 	}
1721 
1722 	if ((error = aio_qphysio(p, aiocbe)) == 0)
1723 		goto done;
1724 #if 0
1725 	if (error > 0) {
1726 		aiocbe->uaiocb._aiocb_private.error = error;
1727 		ops->store_error(job, error);
1728 		goto done;
1729 	}
1730 #endif
1731 queueit:
1732 	/* No buffer for daemon I/O. */
1733 	aiocbe->bp = NULL;
1734 	atomic_add_int(&num_queue_count, 1);
1735 
1736 	AIO_LOCK(ki);
1737 	ki->kaio_count++;
1738 	if (lj)
1739 		lj->lioj_count++;
1740 	TAILQ_INSERT_TAIL(&ki->kaio_jobqueue, aiocbe, plist);
1741 	TAILQ_INSERT_TAIL(&ki->kaio_all, aiocbe, allist);
1742 	if (opcode == LIO_SYNC) {
1743 		TAILQ_FOREACH(cb, &ki->kaio_jobqueue, plist) {
1744 			if (cb->fd_file == aiocbe->fd_file &&
1745 			    cb->uaiocb.aio_lio_opcode != LIO_SYNC &&
1746 			    cb->seqno < aiocbe->seqno) {
1747 				cb->jobflags |= AIOCBLIST_CHECKSYNC;
1748 				aiocbe->pending++;
1749 			}
1750 		}
1751 		TAILQ_FOREACH(cb, &ki->kaio_bufqueue, plist) {
1752 			if (cb->fd_file == aiocbe->fd_file &&
1753 			    cb->uaiocb.aio_lio_opcode != LIO_SYNC &&
1754 			    cb->seqno < aiocbe->seqno) {
1755 				cb->jobflags |= AIOCBLIST_CHECKSYNC;
1756 				aiocbe->pending++;
1757 			}
1758 		}
1759 		if (aiocbe->pending != 0) {
1760 			TAILQ_INSERT_TAIL(&ki->kaio_syncqueue, aiocbe, list);
1761 			aiocbe->jobstate = JOBST_JOBQSYNC;
1762 			AIO_UNLOCK(ki);
1763 			goto done;
1764 		}
1765 	}
1766 	mtx_lock(&aio_job_mtx);
1767 	TAILQ_INSERT_TAIL(&aio_jobs, aiocbe, list);
1768 	aiocbe->jobstate = JOBST_JOBQGLOBAL;
1769 	aio_kick_nowait(p);
1770 	mtx_unlock(&aio_job_mtx);
1771 	AIO_UNLOCK(ki);
1772 	error = 0;
1773 done:
1774 	return (error);
1775 }
1776 
1777 static void
1778 aio_kick_nowait(struct proc *userp)
1779 {
1780 	struct kaioinfo *ki = userp->p_aioinfo;
1781 	struct aiothreadlist *aiop;
1782 
1783 	mtx_assert(&aio_job_mtx, MA_OWNED);
1784 	if ((aiop = TAILQ_FIRST(&aio_freeproc)) != NULL) {
1785 		TAILQ_REMOVE(&aio_freeproc, aiop, list);
1786 		aiop->aiothreadflags &= ~AIOP_FREE;
1787 		wakeup(aiop->aiothread);
1788 	} else if (((num_aio_resv_start + num_aio_procs) < max_aio_procs) &&
1789 	    ((ki->kaio_active_count + num_aio_resv_start) <
1790 	    ki->kaio_maxactive_count)) {
1791 		taskqueue_enqueue(taskqueue_aiod_bio, &ki->kaio_task);
1792 	}
1793 }
1794 
1795 static int
1796 aio_kick(struct proc *userp)
1797 {
1798 	struct kaioinfo *ki = userp->p_aioinfo;
1799 	struct aiothreadlist *aiop;
1800 	int error, ret = 0;
1801 
1802 	mtx_assert(&aio_job_mtx, MA_OWNED);
1803 retryproc:
1804 	if ((aiop = TAILQ_FIRST(&aio_freeproc)) != NULL) {
1805 		TAILQ_REMOVE(&aio_freeproc, aiop, list);
1806 		aiop->aiothreadflags &= ~AIOP_FREE;
1807 		wakeup(aiop->aiothread);
1808 	} else if (((num_aio_resv_start + num_aio_procs) < max_aio_procs) &&
1809 	    ((ki->kaio_active_count + num_aio_resv_start) <
1810 	    ki->kaio_maxactive_count)) {
1811 		num_aio_resv_start++;
1812 		mtx_unlock(&aio_job_mtx);
1813 		error = aio_newproc(&num_aio_resv_start);
1814 		mtx_lock(&aio_job_mtx);
1815 		if (error) {
1816 			num_aio_resv_start--;
1817 			goto retryproc;
1818 		}
1819 	} else {
1820 		ret = -1;
1821 	}
1822 	return (ret);
1823 }
1824 
1825 static void
1826 aio_kick_helper(void *context, int pending)
1827 {
1828 	struct proc *userp = context;
1829 
1830 	mtx_lock(&aio_job_mtx);
1831 	while (--pending >= 0) {
1832 		if (aio_kick(userp))
1833 			break;
1834 	}
1835 	mtx_unlock(&aio_job_mtx);
1836 }
1837 
1838 /*
1839  * Support the aio_return system call, as a side-effect, kernel resources are
1840  * released.
1841  */
1842 static int
1843 kern_aio_return(struct thread *td, struct aiocb *uaiocb, struct aiocb_ops *ops)
1844 {
1845 	struct proc *p = td->td_proc;
1846 	struct aiocblist *cb;
1847 	struct kaioinfo *ki;
1848 	int status, error;
1849 
1850 	ki = p->p_aioinfo;
1851 	if (ki == NULL)
1852 		return (EINVAL);
1853 	AIO_LOCK(ki);
1854 	TAILQ_FOREACH(cb, &ki->kaio_done, plist) {
1855 		if (cb->uuaiocb == uaiocb)
1856 			break;
1857 	}
1858 	if (cb != NULL) {
1859 		MPASS(cb->jobstate == JOBST_JOBFINISHED);
1860 		status = cb->uaiocb._aiocb_private.status;
1861 		error = cb->uaiocb._aiocb_private.error;
1862 		td->td_retval[0] = status;
1863 		if (cb->uaiocb.aio_lio_opcode == LIO_WRITE) {
1864 			td->td_ru.ru_oublock += cb->outputcharge;
1865 			cb->outputcharge = 0;
1866 		} else if (cb->uaiocb.aio_lio_opcode == LIO_READ) {
1867 			td->td_ru.ru_inblock += cb->inputcharge;
1868 			cb->inputcharge = 0;
1869 		}
1870 		aio_free_entry(cb);
1871 		AIO_UNLOCK(ki);
1872 		ops->store_error(uaiocb, error);
1873 		ops->store_status(uaiocb, status);
1874 	} else {
1875 		error = EINVAL;
1876 		AIO_UNLOCK(ki);
1877 	}
1878 	return (error);
1879 }
1880 
1881 int
1882 sys_aio_return(struct thread *td, struct aio_return_args *uap)
1883 {
1884 
1885 	return (kern_aio_return(td, uap->aiocbp, &aiocb_ops));
1886 }
1887 
1888 /*
1889  * Allow a process to wakeup when any of the I/O requests are completed.
1890  */
1891 static int
1892 kern_aio_suspend(struct thread *td, int njoblist, struct aiocb **ujoblist,
1893     struct timespec *ts)
1894 {
1895 	struct proc *p = td->td_proc;
1896 	struct timeval atv;
1897 	struct kaioinfo *ki;
1898 	struct aiocblist *cb, *cbfirst;
1899 	int error, i, timo;
1900 
1901 	timo = 0;
1902 	if (ts) {
1903 		if (ts->tv_nsec < 0 || ts->tv_nsec >= 1000000000)
1904 			return (EINVAL);
1905 
1906 		TIMESPEC_TO_TIMEVAL(&atv, ts);
1907 		if (itimerfix(&atv))
1908 			return (EINVAL);
1909 		timo = tvtohz(&atv);
1910 	}
1911 
1912 	ki = p->p_aioinfo;
1913 	if (ki == NULL)
1914 		return (EAGAIN);
1915 
1916 	if (njoblist == 0)
1917 		return (0);
1918 
1919 	AIO_LOCK(ki);
1920 	for (;;) {
1921 		cbfirst = NULL;
1922 		error = 0;
1923 		TAILQ_FOREACH(cb, &ki->kaio_all, allist) {
1924 			for (i = 0; i < njoblist; i++) {
1925 				if (cb->uuaiocb == ujoblist[i]) {
1926 					if (cbfirst == NULL)
1927 						cbfirst = cb;
1928 					if (cb->jobstate == JOBST_JOBFINISHED)
1929 						goto RETURN;
1930 				}
1931 			}
1932 		}
1933 		/* All tasks were finished. */
1934 		if (cbfirst == NULL)
1935 			break;
1936 
1937 		ki->kaio_flags |= KAIO_WAKEUP;
1938 		error = msleep(&p->p_aioinfo, AIO_MTX(ki), PRIBIO | PCATCH,
1939 		    "aiospn", timo);
1940 		if (error == ERESTART)
1941 			error = EINTR;
1942 		if (error)
1943 			break;
1944 	}
1945 RETURN:
1946 	AIO_UNLOCK(ki);
1947 	return (error);
1948 }
1949 
1950 int
1951 sys_aio_suspend(struct thread *td, struct aio_suspend_args *uap)
1952 {
1953 	struct timespec ts, *tsp;
1954 	struct aiocb **ujoblist;
1955 	int error;
1956 
1957 	if (uap->nent < 0 || uap->nent > AIO_LISTIO_MAX)
1958 		return (EINVAL);
1959 
1960 	if (uap->timeout) {
1961 		/* Get timespec struct. */
1962 		if ((error = copyin(uap->timeout, &ts, sizeof(ts))) != 0)
1963 			return (error);
1964 		tsp = &ts;
1965 	} else
1966 		tsp = NULL;
1967 
1968 	ujoblist = uma_zalloc(aiol_zone, M_WAITOK);
1969 	error = copyin(uap->aiocbp, ujoblist, uap->nent * sizeof(ujoblist[0]));
1970 	if (error == 0)
1971 		error = kern_aio_suspend(td, uap->nent, ujoblist, tsp);
1972 	uma_zfree(aiol_zone, ujoblist);
1973 	return (error);
1974 }
1975 
1976 /*
1977  * aio_cancel cancels any non-physio aio operations not currently in
1978  * progress.
1979  */
1980 int
1981 sys_aio_cancel(struct thread *td, struct aio_cancel_args *uap)
1982 {
1983 	struct proc *p = td->td_proc;
1984 	struct kaioinfo *ki;
1985 	struct aiocblist *cbe, *cbn;
1986 	struct file *fp;
1987 	struct socket *so;
1988 	int error;
1989 	int remove;
1990 	int cancelled = 0;
1991 	int notcancelled = 0;
1992 	struct vnode *vp;
1993 
1994 	/* Lookup file object. */
1995 	error = fget(td, uap->fd, 0, &fp);
1996 	if (error)
1997 		return (error);
1998 
1999 	ki = p->p_aioinfo;
2000 	if (ki == NULL)
2001 		goto done;
2002 
2003 	if (fp->f_type == DTYPE_VNODE) {
2004 		vp = fp->f_vnode;
2005 		if (vn_isdisk(vp, &error)) {
2006 			fdrop(fp, td);
2007 			td->td_retval[0] = AIO_NOTCANCELED;
2008 			return (0);
2009 		}
2010 	}
2011 
2012 	AIO_LOCK(ki);
2013 	TAILQ_FOREACH_SAFE(cbe, &ki->kaio_jobqueue, plist, cbn) {
2014 		if ((uap->fd == cbe->uaiocb.aio_fildes) &&
2015 		    ((uap->aiocbp == NULL) ||
2016 		     (uap->aiocbp == cbe->uuaiocb))) {
2017 			remove = 0;
2018 
2019 			mtx_lock(&aio_job_mtx);
2020 			if (cbe->jobstate == JOBST_JOBQGLOBAL) {
2021 				TAILQ_REMOVE(&aio_jobs, cbe, list);
2022 				remove = 1;
2023 			} else if (cbe->jobstate == JOBST_JOBQSOCK) {
2024 				MPASS(fp->f_type == DTYPE_SOCKET);
2025 				so = fp->f_data;
2026 				TAILQ_REMOVE(&so->so_aiojobq, cbe, list);
2027 				remove = 1;
2028 			} else if (cbe->jobstate == JOBST_JOBQSYNC) {
2029 				TAILQ_REMOVE(&ki->kaio_syncqueue, cbe, list);
2030 				remove = 1;
2031 			}
2032 			mtx_unlock(&aio_job_mtx);
2033 
2034 			if (remove) {
2035 				TAILQ_REMOVE(&ki->kaio_jobqueue, cbe, plist);
2036 				cbe->uaiocb._aiocb_private.status = -1;
2037 				cbe->uaiocb._aiocb_private.error = ECANCELED;
2038 				aio_bio_done_notify(p, cbe, DONE_QUEUE);
2039 				cancelled++;
2040 			} else {
2041 				notcancelled++;
2042 			}
2043 			if (uap->aiocbp != NULL)
2044 				break;
2045 		}
2046 	}
2047 	AIO_UNLOCK(ki);
2048 
2049 done:
2050 	fdrop(fp, td);
2051 
2052 	if (uap->aiocbp != NULL) {
2053 		if (cancelled) {
2054 			td->td_retval[0] = AIO_CANCELED;
2055 			return (0);
2056 		}
2057 	}
2058 
2059 	if (notcancelled) {
2060 		td->td_retval[0] = AIO_NOTCANCELED;
2061 		return (0);
2062 	}
2063 
2064 	if (cancelled) {
2065 		td->td_retval[0] = AIO_CANCELED;
2066 		return (0);
2067 	}
2068 
2069 	td->td_retval[0] = AIO_ALLDONE;
2070 
2071 	return (0);
2072 }
2073 
2074 /*
2075  * aio_error is implemented in the kernel level for compatibility purposes
2076  * only.  For a user mode async implementation, it would be best to do it in
2077  * a userland subroutine.
2078  */
2079 static int
2080 kern_aio_error(struct thread *td, struct aiocb *aiocbp, struct aiocb_ops *ops)
2081 {
2082 	struct proc *p = td->td_proc;
2083 	struct aiocblist *cb;
2084 	struct kaioinfo *ki;
2085 	int status;
2086 
2087 	ki = p->p_aioinfo;
2088 	if (ki == NULL) {
2089 		td->td_retval[0] = EINVAL;
2090 		return (0);
2091 	}
2092 
2093 	AIO_LOCK(ki);
2094 	TAILQ_FOREACH(cb, &ki->kaio_all, allist) {
2095 		if (cb->uuaiocb == aiocbp) {
2096 			if (cb->jobstate == JOBST_JOBFINISHED)
2097 				td->td_retval[0] =
2098 					cb->uaiocb._aiocb_private.error;
2099 			else
2100 				td->td_retval[0] = EINPROGRESS;
2101 			AIO_UNLOCK(ki);
2102 			return (0);
2103 		}
2104 	}
2105 	AIO_UNLOCK(ki);
2106 
2107 	/*
2108 	 * Hack for failure of aio_aqueue.
2109 	 */
2110 	status = ops->fetch_status(aiocbp);
2111 	if (status == -1) {
2112 		td->td_retval[0] = ops->fetch_error(aiocbp);
2113 		return (0);
2114 	}
2115 
2116 	td->td_retval[0] = EINVAL;
2117 	return (0);
2118 }
2119 
2120 int
2121 sys_aio_error(struct thread *td, struct aio_error_args *uap)
2122 {
2123 
2124 	return (kern_aio_error(td, uap->aiocbp, &aiocb_ops));
2125 }
2126 
2127 /* syscall - asynchronous read from a file (REALTIME) */
2128 int
2129 sys_oaio_read(struct thread *td, struct oaio_read_args *uap)
2130 {
2131 
2132 	return (aio_aqueue(td, (struct aiocb *)uap->aiocbp, NULL, LIO_READ,
2133 	    &aiocb_ops_osigevent));
2134 }
2135 
2136 int
2137 sys_aio_read(struct thread *td, struct aio_read_args *uap)
2138 {
2139 
2140 	return (aio_aqueue(td, uap->aiocbp, NULL, LIO_READ, &aiocb_ops));
2141 }
2142 
2143 /* syscall - asynchronous write to a file (REALTIME) */
2144 int
2145 sys_oaio_write(struct thread *td, struct oaio_write_args *uap)
2146 {
2147 
2148 	return (aio_aqueue(td, (struct aiocb *)uap->aiocbp, NULL, LIO_WRITE,
2149 	    &aiocb_ops_osigevent));
2150 }
2151 
2152 int
2153 sys_aio_write(struct thread *td, struct aio_write_args *uap)
2154 {
2155 
2156 	return (aio_aqueue(td, uap->aiocbp, NULL, LIO_WRITE, &aiocb_ops));
2157 }
2158 
2159 static int
2160 kern_lio_listio(struct thread *td, int mode, struct aiocb * const *uacb_list,
2161     struct aiocb **acb_list, int nent, struct sigevent *sig,
2162     struct aiocb_ops *ops)
2163 {
2164 	struct proc *p = td->td_proc;
2165 	struct aiocb *iocb;
2166 	struct kaioinfo *ki;
2167 	struct aioliojob *lj;
2168 	struct kevent kev;
2169 	int error;
2170 	int nerror;
2171 	int i;
2172 
2173 	if ((mode != LIO_NOWAIT) && (mode != LIO_WAIT))
2174 		return (EINVAL);
2175 
2176 	if (nent < 0 || nent > AIO_LISTIO_MAX)
2177 		return (EINVAL);
2178 
2179 	if (p->p_aioinfo == NULL)
2180 		aio_init_aioinfo(p);
2181 
2182 	ki = p->p_aioinfo;
2183 
2184 	lj = uma_zalloc(aiolio_zone, M_WAITOK);
2185 	lj->lioj_flags = 0;
2186 	lj->lioj_count = 0;
2187 	lj->lioj_finished_count = 0;
2188 	knlist_init_mtx(&lj->klist, AIO_MTX(ki));
2189 	ksiginfo_init(&lj->lioj_ksi);
2190 
2191 	/*
2192 	 * Setup signal.
2193 	 */
2194 	if (sig && (mode == LIO_NOWAIT)) {
2195 		bcopy(sig, &lj->lioj_signal, sizeof(lj->lioj_signal));
2196 		if (lj->lioj_signal.sigev_notify == SIGEV_KEVENT) {
2197 			/* Assume only new style KEVENT */
2198 			kev.filter = EVFILT_LIO;
2199 			kev.flags = EV_ADD | EV_ENABLE | EV_FLAG1;
2200 			kev.ident = (uintptr_t)uacb_list; /* something unique */
2201 			kev.data = (intptr_t)lj;
2202 			/* pass user defined sigval data */
2203 			kev.udata = lj->lioj_signal.sigev_value.sival_ptr;
2204 			error = kqfd_register(
2205 			    lj->lioj_signal.sigev_notify_kqueue, &kev, td, 1);
2206 			if (error) {
2207 				uma_zfree(aiolio_zone, lj);
2208 				return (error);
2209 			}
2210 		} else if (lj->lioj_signal.sigev_notify == SIGEV_NONE) {
2211 			;
2212 		} else if (lj->lioj_signal.sigev_notify == SIGEV_SIGNAL ||
2213 			   lj->lioj_signal.sigev_notify == SIGEV_THREAD_ID) {
2214 				if (!_SIG_VALID(lj->lioj_signal.sigev_signo)) {
2215 					uma_zfree(aiolio_zone, lj);
2216 					return EINVAL;
2217 				}
2218 				lj->lioj_flags |= LIOJ_SIGNAL;
2219 		} else {
2220 			uma_zfree(aiolio_zone, lj);
2221 			return EINVAL;
2222 		}
2223 	}
2224 
2225 	AIO_LOCK(ki);
2226 	TAILQ_INSERT_TAIL(&ki->kaio_liojoblist, lj, lioj_list);
2227 	/*
2228 	 * Add extra aiocb count to avoid the lio to be freed
2229 	 * by other threads doing aio_waitcomplete or aio_return,
2230 	 * and prevent event from being sent until we have queued
2231 	 * all tasks.
2232 	 */
2233 	lj->lioj_count = 1;
2234 	AIO_UNLOCK(ki);
2235 
2236 	/*
2237 	 * Get pointers to the list of I/O requests.
2238 	 */
2239 	nerror = 0;
2240 	for (i = 0; i < nent; i++) {
2241 		iocb = acb_list[i];
2242 		if (iocb != NULL) {
2243 			error = aio_aqueue(td, iocb, lj, LIO_NOP, ops);
2244 			if (error != 0)
2245 				nerror++;
2246 		}
2247 	}
2248 
2249 	error = 0;
2250 	AIO_LOCK(ki);
2251 	if (mode == LIO_WAIT) {
2252 		while (lj->lioj_count - 1 != lj->lioj_finished_count) {
2253 			ki->kaio_flags |= KAIO_WAKEUP;
2254 			error = msleep(&p->p_aioinfo, AIO_MTX(ki),
2255 			    PRIBIO | PCATCH, "aiospn", 0);
2256 			if (error == ERESTART)
2257 				error = EINTR;
2258 			if (error)
2259 				break;
2260 		}
2261 	} else {
2262 		if (lj->lioj_count - 1 == lj->lioj_finished_count) {
2263 			if (lj->lioj_signal.sigev_notify == SIGEV_KEVENT) {
2264 				lj->lioj_flags |= LIOJ_KEVENT_POSTED;
2265 				KNOTE_LOCKED(&lj->klist, 1);
2266 			}
2267 			if ((lj->lioj_flags & (LIOJ_SIGNAL|LIOJ_SIGNAL_POSTED))
2268 			    == LIOJ_SIGNAL
2269 			    && (lj->lioj_signal.sigev_notify == SIGEV_SIGNAL ||
2270 			    lj->lioj_signal.sigev_notify == SIGEV_THREAD_ID)) {
2271 				aio_sendsig(p, &lj->lioj_signal,
2272 					    &lj->lioj_ksi);
2273 				lj->lioj_flags |= LIOJ_SIGNAL_POSTED;
2274 			}
2275 		}
2276 	}
2277 	lj->lioj_count--;
2278 	if (lj->lioj_count == 0) {
2279 		TAILQ_REMOVE(&ki->kaio_liojoblist, lj, lioj_list);
2280 		knlist_delete(&lj->klist, curthread, 1);
2281 		PROC_LOCK(p);
2282 		sigqueue_take(&lj->lioj_ksi);
2283 		PROC_UNLOCK(p);
2284 		AIO_UNLOCK(ki);
2285 		uma_zfree(aiolio_zone, lj);
2286 	} else
2287 		AIO_UNLOCK(ki);
2288 
2289 	if (nerror)
2290 		return (EIO);
2291 	return (error);
2292 }
2293 
2294 /* syscall - list directed I/O (REALTIME) */
2295 int
2296 sys_olio_listio(struct thread *td, struct olio_listio_args *uap)
2297 {
2298 	struct aiocb **acb_list;
2299 	struct sigevent *sigp, sig;
2300 	struct osigevent osig;
2301 	int error, nent;
2302 
2303 	if ((uap->mode != LIO_NOWAIT) && (uap->mode != LIO_WAIT))
2304 		return (EINVAL);
2305 
2306 	nent = uap->nent;
2307 	if (nent < 0 || nent > AIO_LISTIO_MAX)
2308 		return (EINVAL);
2309 
2310 	if (uap->sig && (uap->mode == LIO_NOWAIT)) {
2311 		error = copyin(uap->sig, &osig, sizeof(osig));
2312 		if (error)
2313 			return (error);
2314 		error = convert_old_sigevent(&osig, &sig);
2315 		if (error)
2316 			return (error);
2317 		sigp = &sig;
2318 	} else
2319 		sigp = NULL;
2320 
2321 	acb_list = malloc(sizeof(struct aiocb *) * nent, M_LIO, M_WAITOK);
2322 	error = copyin(uap->acb_list, acb_list, nent * sizeof(acb_list[0]));
2323 	if (error == 0)
2324 		error = kern_lio_listio(td, uap->mode,
2325 		    (struct aiocb * const *)uap->acb_list, acb_list, nent, sigp,
2326 		    &aiocb_ops_osigevent);
2327 	free(acb_list, M_LIO);
2328 	return (error);
2329 }
2330 
2331 /* syscall - list directed I/O (REALTIME) */
2332 int
2333 sys_lio_listio(struct thread *td, struct lio_listio_args *uap)
2334 {
2335 	struct aiocb **acb_list;
2336 	struct sigevent *sigp, sig;
2337 	int error, nent;
2338 
2339 	if ((uap->mode != LIO_NOWAIT) && (uap->mode != LIO_WAIT))
2340 		return (EINVAL);
2341 
2342 	nent = uap->nent;
2343 	if (nent < 0 || nent > AIO_LISTIO_MAX)
2344 		return (EINVAL);
2345 
2346 	if (uap->sig && (uap->mode == LIO_NOWAIT)) {
2347 		error = copyin(uap->sig, &sig, sizeof(sig));
2348 		if (error)
2349 			return (error);
2350 		sigp = &sig;
2351 	} else
2352 		sigp = NULL;
2353 
2354 	acb_list = malloc(sizeof(struct aiocb *) * nent, M_LIO, M_WAITOK);
2355 	error = copyin(uap->acb_list, acb_list, nent * sizeof(acb_list[0]));
2356 	if (error == 0)
2357 		error = kern_lio_listio(td, uap->mode, uap->acb_list, acb_list,
2358 		    nent, sigp, &aiocb_ops);
2359 	free(acb_list, M_LIO);
2360 	return (error);
2361 }
2362 
2363 /*
2364  * Called from interrupt thread for physio, we should return as fast
2365  * as possible, so we schedule a biohelper task.
2366  */
2367 static void
2368 aio_physwakeup(struct buf *bp)
2369 {
2370 	struct aiocblist *aiocbe;
2371 
2372 	aiocbe = (struct aiocblist *)bp->b_caller1;
2373 	taskqueue_enqueue(taskqueue_aiod_bio, &aiocbe->biotask);
2374 }
2375 
2376 /*
2377  * Task routine to perform heavy tasks, process wakeup, and signals.
2378  */
2379 static void
2380 biohelper(void *context, int pending)
2381 {
2382 	struct aiocblist *aiocbe = context;
2383 	struct buf *bp;
2384 	struct proc *userp;
2385 	struct kaioinfo *ki;
2386 	int nblks;
2387 
2388 	bp = aiocbe->bp;
2389 	userp = aiocbe->userproc;
2390 	ki = userp->p_aioinfo;
2391 	AIO_LOCK(ki);
2392 	aiocbe->uaiocb._aiocb_private.status -= bp->b_resid;
2393 	aiocbe->uaiocb._aiocb_private.error = 0;
2394 	if (bp->b_ioflags & BIO_ERROR)
2395 		aiocbe->uaiocb._aiocb_private.error = bp->b_error;
2396 	nblks = btodb(aiocbe->uaiocb.aio_nbytes);
2397 	if (aiocbe->uaiocb.aio_lio_opcode == LIO_WRITE)
2398 		aiocbe->outputcharge += nblks;
2399 	else
2400 		aiocbe->inputcharge += nblks;
2401 	aiocbe->bp = NULL;
2402 	TAILQ_REMOVE(&userp->p_aioinfo->kaio_bufqueue, aiocbe, plist);
2403 	ki->kaio_buffer_count--;
2404 	aio_bio_done_notify(userp, aiocbe, DONE_BUF);
2405 	AIO_UNLOCK(ki);
2406 
2407 	/* Release mapping into kernel space. */
2408 	vunmapbuf(bp);
2409 	relpbuf(bp, NULL);
2410 	atomic_subtract_int(&num_buf_aio, 1);
2411 }
2412 
2413 /* syscall - wait for the next completion of an aio request */
2414 static int
2415 kern_aio_waitcomplete(struct thread *td, struct aiocb **aiocbp,
2416     struct timespec *ts, struct aiocb_ops *ops)
2417 {
2418 	struct proc *p = td->td_proc;
2419 	struct timeval atv;
2420 	struct kaioinfo *ki;
2421 	struct aiocblist *cb;
2422 	struct aiocb *uuaiocb;
2423 	int error, status, timo;
2424 
2425 	ops->store_aiocb(aiocbp, NULL);
2426 
2427 	timo = 0;
2428 	if (ts) {
2429 		if ((ts->tv_nsec < 0) || (ts->tv_nsec >= 1000000000))
2430 			return (EINVAL);
2431 
2432 		TIMESPEC_TO_TIMEVAL(&atv, ts);
2433 		if (itimerfix(&atv))
2434 			return (EINVAL);
2435 		timo = tvtohz(&atv);
2436 	}
2437 
2438 	if (p->p_aioinfo == NULL)
2439 		aio_init_aioinfo(p);
2440 	ki = p->p_aioinfo;
2441 
2442 	error = 0;
2443 	cb = NULL;
2444 	AIO_LOCK(ki);
2445 	while ((cb = TAILQ_FIRST(&ki->kaio_done)) == NULL) {
2446 		ki->kaio_flags |= KAIO_WAKEUP;
2447 		error = msleep(&p->p_aioinfo, AIO_MTX(ki), PRIBIO | PCATCH,
2448 		    "aiowc", timo);
2449 		if (timo && error == ERESTART)
2450 			error = EINTR;
2451 		if (error)
2452 			break;
2453 	}
2454 
2455 	if (cb != NULL) {
2456 		MPASS(cb->jobstate == JOBST_JOBFINISHED);
2457 		uuaiocb = cb->uuaiocb;
2458 		status = cb->uaiocb._aiocb_private.status;
2459 		error = cb->uaiocb._aiocb_private.error;
2460 		td->td_retval[0] = status;
2461 		if (cb->uaiocb.aio_lio_opcode == LIO_WRITE) {
2462 			td->td_ru.ru_oublock += cb->outputcharge;
2463 			cb->outputcharge = 0;
2464 		} else if (cb->uaiocb.aio_lio_opcode == LIO_READ) {
2465 			td->td_ru.ru_inblock += cb->inputcharge;
2466 			cb->inputcharge = 0;
2467 		}
2468 		aio_free_entry(cb);
2469 		AIO_UNLOCK(ki);
2470 		ops->store_aiocb(aiocbp, uuaiocb);
2471 		ops->store_error(uuaiocb, error);
2472 		ops->store_status(uuaiocb, status);
2473 	} else
2474 		AIO_UNLOCK(ki);
2475 
2476 	return (error);
2477 }
2478 
2479 int
2480 sys_aio_waitcomplete(struct thread *td, struct aio_waitcomplete_args *uap)
2481 {
2482 	struct timespec ts, *tsp;
2483 	int error;
2484 
2485 	if (uap->timeout) {
2486 		/* Get timespec struct. */
2487 		error = copyin(uap->timeout, &ts, sizeof(ts));
2488 		if (error)
2489 			return (error);
2490 		tsp = &ts;
2491 	} else
2492 		tsp = NULL;
2493 
2494 	return (kern_aio_waitcomplete(td, uap->aiocbp, tsp, &aiocb_ops));
2495 }
2496 
2497 static int
2498 kern_aio_fsync(struct thread *td, int op, struct aiocb *aiocbp,
2499     struct aiocb_ops *ops)
2500 {
2501 	struct proc *p = td->td_proc;
2502 	struct kaioinfo *ki;
2503 
2504 	if (op != O_SYNC) /* XXX lack of O_DSYNC */
2505 		return (EINVAL);
2506 	ki = p->p_aioinfo;
2507 	if (ki == NULL)
2508 		aio_init_aioinfo(p);
2509 	return (aio_aqueue(td, aiocbp, NULL, LIO_SYNC, ops));
2510 }
2511 
2512 int
2513 sys_aio_fsync(struct thread *td, struct aio_fsync_args *uap)
2514 {
2515 
2516 	return (kern_aio_fsync(td, uap->op, uap->aiocbp, &aiocb_ops));
2517 }
2518 
2519 /* kqueue attach function */
2520 static int
2521 filt_aioattach(struct knote *kn)
2522 {
2523 	struct aiocblist *aiocbe = (struct aiocblist *)kn->kn_sdata;
2524 
2525 	/*
2526 	 * The aiocbe pointer must be validated before using it, so
2527 	 * registration is restricted to the kernel; the user cannot
2528 	 * set EV_FLAG1.
2529 	 */
2530 	if ((kn->kn_flags & EV_FLAG1) == 0)
2531 		return (EPERM);
2532 	kn->kn_ptr.p_aio = aiocbe;
2533 	kn->kn_flags &= ~EV_FLAG1;
2534 
2535 	knlist_add(&aiocbe->klist, kn, 0);
2536 
2537 	return (0);
2538 }
2539 
2540 /* kqueue detach function */
2541 static void
2542 filt_aiodetach(struct knote *kn)
2543 {
2544 	struct knlist *knl;
2545 
2546 	knl = &kn->kn_ptr.p_aio->klist;
2547 	knl->kl_lock(knl->kl_lockarg);
2548 	if (!knlist_empty(knl))
2549 		knlist_remove(knl, kn, 1);
2550 	knl->kl_unlock(knl->kl_lockarg);
2551 }
2552 
2553 /* kqueue filter function */
2554 /*ARGSUSED*/
2555 static int
2556 filt_aio(struct knote *kn, long hint)
2557 {
2558 	struct aiocblist *aiocbe = kn->kn_ptr.p_aio;
2559 
2560 	kn->kn_data = aiocbe->uaiocb._aiocb_private.error;
2561 	if (aiocbe->jobstate != JOBST_JOBFINISHED)
2562 		return (0);
2563 	kn->kn_flags |= EV_EOF;
2564 	return (1);
2565 }
2566 
2567 /* kqueue attach function */
2568 static int
2569 filt_lioattach(struct knote *kn)
2570 {
2571 	struct aioliojob * lj = (struct aioliojob *)kn->kn_sdata;
2572 
2573 	/*
2574 	 * The aioliojob pointer must be validated before using it, so
2575 	 * registration is restricted to the kernel; the user cannot
2576 	 * set EV_FLAG1.
2577 	 */
2578 	if ((kn->kn_flags & EV_FLAG1) == 0)
2579 		return (EPERM);
2580 	kn->kn_ptr.p_lio = lj;
2581 	kn->kn_flags &= ~EV_FLAG1;
2582 
2583 	knlist_add(&lj->klist, kn, 0);
2584 
2585 	return (0);
2586 }
2587 
2588 /* kqueue detach function */
2589 static void
2590 filt_liodetach(struct knote *kn)
2591 {
2592 	struct knlist *knl;
2593 
2594 	knl = &kn->kn_ptr.p_lio->klist;
2595 	knl->kl_lock(knl->kl_lockarg);
2596 	if (!knlist_empty(knl))
2597 		knlist_remove(knl, kn, 1);
2598 	knl->kl_unlock(knl->kl_lockarg);
2599 }
2600 
2601 /* kqueue filter function */
2602 /*ARGSUSED*/
2603 static int
2604 filt_lio(struct knote *kn, long hint)
2605 {
2606 	struct aioliojob * lj = kn->kn_ptr.p_lio;
2607 
2608 	return (lj->lioj_flags & LIOJ_KEVENT_POSTED);
2609 }
2610 
2611 #ifdef COMPAT_FREEBSD32
2612 
2613 struct __aiocb_private32 {
2614 	int32_t	status;
2615 	int32_t	error;
2616 	uint32_t kernelinfo;
2617 };
2618 
2619 typedef struct oaiocb32 {
2620 	int	aio_fildes;		/* File descriptor */
2621 	uint64_t aio_offset __packed;	/* File offset for I/O */
2622 	uint32_t aio_buf;		/* I/O buffer in process space */
2623 	uint32_t aio_nbytes;		/* Number of bytes for I/O */
2624 	struct	osigevent32 aio_sigevent; /* Signal to deliver */
2625 	int	aio_lio_opcode;		/* LIO opcode */
2626 	int	aio_reqprio;		/* Request priority -- ignored */
2627 	struct	__aiocb_private32 _aiocb_private;
2628 } oaiocb32_t;
2629 
2630 typedef struct aiocb32 {
2631 	int32_t	aio_fildes;		/* File descriptor */
2632 	uint64_t aio_offset __packed;	/* File offset for I/O */
2633 	uint32_t aio_buf;		/* I/O buffer in process space */
2634 	uint32_t aio_nbytes;		/* Number of bytes for I/O */
2635 	int	__spare__[2];
2636 	uint32_t __spare2__;
2637 	int	aio_lio_opcode;		/* LIO opcode */
2638 	int	aio_reqprio;		/* Request priority -- ignored */
2639 	struct __aiocb_private32 _aiocb_private;
2640 	struct sigevent32 aio_sigevent;	/* Signal to deliver */
2641 } aiocb32_t;
2642 
2643 static int
2644 convert_old_sigevent32(struct osigevent32 *osig, struct sigevent *nsig)
2645 {
2646 
2647 	/*
2648 	 * Only SIGEV_NONE, SIGEV_SIGNAL, and SIGEV_KEVENT are
2649 	 * supported by AIO with the old sigevent structure.
2650 	 */
2651 	CP(*osig, *nsig, sigev_notify);
2652 	switch (nsig->sigev_notify) {
2653 	case SIGEV_NONE:
2654 		break;
2655 	case SIGEV_SIGNAL:
2656 		nsig->sigev_signo = osig->__sigev_u.__sigev_signo;
2657 		break;
2658 	case SIGEV_KEVENT:
2659 		nsig->sigev_notify_kqueue =
2660 		    osig->__sigev_u.__sigev_notify_kqueue;
2661 		PTRIN_CP(*osig, *nsig, sigev_value.sival_ptr);
2662 		break;
2663 	default:
2664 		return (EINVAL);
2665 	}
2666 	return (0);
2667 }
2668 
2669 static int
2670 aiocb32_copyin_old_sigevent(struct aiocb *ujob, struct aiocb *kjob)
2671 {
2672 	struct oaiocb32 job32;
2673 	int error;
2674 
2675 	bzero(kjob, sizeof(struct aiocb));
2676 	error = copyin(ujob, &job32, sizeof(job32));
2677 	if (error)
2678 		return (error);
2679 
2680 	CP(job32, *kjob, aio_fildes);
2681 	CP(job32, *kjob, aio_offset);
2682 	PTRIN_CP(job32, *kjob, aio_buf);
2683 	CP(job32, *kjob, aio_nbytes);
2684 	CP(job32, *kjob, aio_lio_opcode);
2685 	CP(job32, *kjob, aio_reqprio);
2686 	CP(job32, *kjob, _aiocb_private.status);
2687 	CP(job32, *kjob, _aiocb_private.error);
2688 	PTRIN_CP(job32, *kjob, _aiocb_private.kernelinfo);
2689 	return (convert_old_sigevent32(&job32.aio_sigevent,
2690 	    &kjob->aio_sigevent));
2691 }
2692 
2693 static int
2694 convert_sigevent32(struct sigevent32 *sig32, struct sigevent *sig)
2695 {
2696 
2697 	CP(*sig32, *sig, sigev_notify);
2698 	switch (sig->sigev_notify) {
2699 	case SIGEV_NONE:
2700 		break;
2701 	case SIGEV_THREAD_ID:
2702 		CP(*sig32, *sig, sigev_notify_thread_id);
2703 		/* FALLTHROUGH */
2704 	case SIGEV_SIGNAL:
2705 		CP(*sig32, *sig, sigev_signo);
2706 		break;
2707 	case SIGEV_KEVENT:
2708 		CP(*sig32, *sig, sigev_notify_kqueue);
2709 		CP(*sig32, *sig, sigev_notify_kevent_flags);
2710 		PTRIN_CP(*sig32, *sig, sigev_value.sival_ptr);
2711 		break;
2712 	default:
2713 		return (EINVAL);
2714 	}
2715 	return (0);
2716 }
2717 
2718 static int
2719 aiocb32_copyin(struct aiocb *ujob, struct aiocb *kjob)
2720 {
2721 	struct aiocb32 job32;
2722 	int error;
2723 
2724 	error = copyin(ujob, &job32, sizeof(job32));
2725 	if (error)
2726 		return (error);
2727 	CP(job32, *kjob, aio_fildes);
2728 	CP(job32, *kjob, aio_offset);
2729 	PTRIN_CP(job32, *kjob, aio_buf);
2730 	CP(job32, *kjob, aio_nbytes);
2731 	CP(job32, *kjob, aio_lio_opcode);
2732 	CP(job32, *kjob, aio_reqprio);
2733 	CP(job32, *kjob, _aiocb_private.status);
2734 	CP(job32, *kjob, _aiocb_private.error);
2735 	PTRIN_CP(job32, *kjob, _aiocb_private.kernelinfo);
2736 	return (convert_sigevent32(&job32.aio_sigevent, &kjob->aio_sigevent));
2737 }
2738 
2739 static long
2740 aiocb32_fetch_status(struct aiocb *ujob)
2741 {
2742 	struct aiocb32 *ujob32;
2743 
2744 	ujob32 = (struct aiocb32 *)ujob;
2745 	return (fuword32(&ujob32->_aiocb_private.status));
2746 }
2747 
2748 static long
2749 aiocb32_fetch_error(struct aiocb *ujob)
2750 {
2751 	struct aiocb32 *ujob32;
2752 
2753 	ujob32 = (struct aiocb32 *)ujob;
2754 	return (fuword32(&ujob32->_aiocb_private.error));
2755 }
2756 
2757 static int
2758 aiocb32_store_status(struct aiocb *ujob, long status)
2759 {
2760 	struct aiocb32 *ujob32;
2761 
2762 	ujob32 = (struct aiocb32 *)ujob;
2763 	return (suword32(&ujob32->_aiocb_private.status, status));
2764 }
2765 
2766 static int
2767 aiocb32_store_error(struct aiocb *ujob, long error)
2768 {
2769 	struct aiocb32 *ujob32;
2770 
2771 	ujob32 = (struct aiocb32 *)ujob;
2772 	return (suword32(&ujob32->_aiocb_private.error, error));
2773 }
2774 
2775 static int
2776 aiocb32_store_kernelinfo(struct aiocb *ujob, long jobref)
2777 {
2778 	struct aiocb32 *ujob32;
2779 
2780 	ujob32 = (struct aiocb32 *)ujob;
2781 	return (suword32(&ujob32->_aiocb_private.kernelinfo, jobref));
2782 }
2783 
2784 static int
2785 aiocb32_store_aiocb(struct aiocb **ujobp, struct aiocb *ujob)
2786 {
2787 
2788 	return (suword32(ujobp, (long)ujob));
2789 }
2790 
2791 static struct aiocb_ops aiocb32_ops = {
2792 	.copyin = aiocb32_copyin,
2793 	.fetch_status = aiocb32_fetch_status,
2794 	.fetch_error = aiocb32_fetch_error,
2795 	.store_status = aiocb32_store_status,
2796 	.store_error = aiocb32_store_error,
2797 	.store_kernelinfo = aiocb32_store_kernelinfo,
2798 	.store_aiocb = aiocb32_store_aiocb,
2799 };
2800 
2801 static struct aiocb_ops aiocb32_ops_osigevent = {
2802 	.copyin = aiocb32_copyin_old_sigevent,
2803 	.fetch_status = aiocb32_fetch_status,
2804 	.fetch_error = aiocb32_fetch_error,
2805 	.store_status = aiocb32_store_status,
2806 	.store_error = aiocb32_store_error,
2807 	.store_kernelinfo = aiocb32_store_kernelinfo,
2808 	.store_aiocb = aiocb32_store_aiocb,
2809 };
2810 
2811 int
2812 freebsd32_aio_return(struct thread *td, struct freebsd32_aio_return_args *uap)
2813 {
2814 
2815 	return (kern_aio_return(td, (struct aiocb *)uap->aiocbp, &aiocb32_ops));
2816 }
2817 
2818 int
2819 freebsd32_aio_suspend(struct thread *td, struct freebsd32_aio_suspend_args *uap)
2820 {
2821 	struct timespec32 ts32;
2822 	struct timespec ts, *tsp;
2823 	struct aiocb **ujoblist;
2824 	uint32_t *ujoblist32;
2825 	int error, i;
2826 
2827 	if (uap->nent < 0 || uap->nent > AIO_LISTIO_MAX)
2828 		return (EINVAL);
2829 
2830 	if (uap->timeout) {
2831 		/* Get timespec struct. */
2832 		if ((error = copyin(uap->timeout, &ts32, sizeof(ts32))) != 0)
2833 			return (error);
2834 		CP(ts32, ts, tv_sec);
2835 		CP(ts32, ts, tv_nsec);
2836 		tsp = &ts;
2837 	} else
2838 		tsp = NULL;
2839 
2840 	ujoblist = uma_zalloc(aiol_zone, M_WAITOK);
2841 	ujoblist32 = (uint32_t *)ujoblist;
2842 	error = copyin(uap->aiocbp, ujoblist32, uap->nent *
2843 	    sizeof(ujoblist32[0]));
2844 	if (error == 0) {
2845 		for (i = uap->nent; i > 0; i--)
2846 			ujoblist[i] = PTRIN(ujoblist32[i]);
2847 
2848 		error = kern_aio_suspend(td, uap->nent, ujoblist, tsp);
2849 	}
2850 	uma_zfree(aiol_zone, ujoblist);
2851 	return (error);
2852 }
2853 
2854 int
2855 freebsd32_aio_cancel(struct thread *td, struct freebsd32_aio_cancel_args *uap)
2856 {
2857 
2858 	return (sys_aio_cancel(td, (struct aio_cancel_args *)uap));
2859 }
2860 
2861 int
2862 freebsd32_aio_error(struct thread *td, struct freebsd32_aio_error_args *uap)
2863 {
2864 
2865 	return (kern_aio_error(td, (struct aiocb *)uap->aiocbp, &aiocb32_ops));
2866 }
2867 
2868 int
2869 freebsd32_oaio_read(struct thread *td, struct freebsd32_oaio_read_args *uap)
2870 {
2871 
2872 	return (aio_aqueue(td, (struct aiocb *)uap->aiocbp, NULL, LIO_READ,
2873 	    &aiocb32_ops_osigevent));
2874 }
2875 
2876 int
2877 freebsd32_aio_read(struct thread *td, struct freebsd32_aio_read_args *uap)
2878 {
2879 
2880 	return (aio_aqueue(td, (struct aiocb *)uap->aiocbp, NULL, LIO_READ,
2881 	    &aiocb32_ops));
2882 }
2883 
2884 int
2885 freebsd32_oaio_write(struct thread *td, struct freebsd32_oaio_write_args *uap)
2886 {
2887 
2888 	return (aio_aqueue(td, (struct aiocb *)uap->aiocbp, NULL, LIO_WRITE,
2889 	    &aiocb32_ops_osigevent));
2890 }
2891 
2892 int
2893 freebsd32_aio_write(struct thread *td, struct freebsd32_aio_write_args *uap)
2894 {
2895 
2896 	return (aio_aqueue(td, (struct aiocb *)uap->aiocbp, NULL, LIO_WRITE,
2897 	    &aiocb32_ops));
2898 }
2899 
2900 int
2901 freebsd32_aio_waitcomplete(struct thread *td,
2902     struct freebsd32_aio_waitcomplete_args *uap)
2903 {
2904 	struct timespec32 ts32;
2905 	struct timespec ts, *tsp;
2906 	int error;
2907 
2908 	if (uap->timeout) {
2909 		/* Get timespec struct. */
2910 		error = copyin(uap->timeout, &ts32, sizeof(ts32));
2911 		if (error)
2912 			return (error);
2913 		CP(ts32, ts, tv_sec);
2914 		CP(ts32, ts, tv_nsec);
2915 		tsp = &ts;
2916 	} else
2917 		tsp = NULL;
2918 
2919 	return (kern_aio_waitcomplete(td, (struct aiocb **)uap->aiocbp, tsp,
2920 	    &aiocb32_ops));
2921 }
2922 
2923 int
2924 freebsd32_aio_fsync(struct thread *td, struct freebsd32_aio_fsync_args *uap)
2925 {
2926 
2927 	return (kern_aio_fsync(td, uap->op, (struct aiocb *)uap->aiocbp,
2928 	    &aiocb32_ops));
2929 }
2930 
2931 int
2932 freebsd32_olio_listio(struct thread *td, struct freebsd32_olio_listio_args *uap)
2933 {
2934 	struct aiocb **acb_list;
2935 	struct sigevent *sigp, sig;
2936 	struct osigevent32 osig;
2937 	uint32_t *acb_list32;
2938 	int error, i, nent;
2939 
2940 	if ((uap->mode != LIO_NOWAIT) && (uap->mode != LIO_WAIT))
2941 		return (EINVAL);
2942 
2943 	nent = uap->nent;
2944 	if (nent < 0 || nent > AIO_LISTIO_MAX)
2945 		return (EINVAL);
2946 
2947 	if (uap->sig && (uap->mode == LIO_NOWAIT)) {
2948 		error = copyin(uap->sig, &osig, sizeof(osig));
2949 		if (error)
2950 			return (error);
2951 		error = convert_old_sigevent32(&osig, &sig);
2952 		if (error)
2953 			return (error);
2954 		sigp = &sig;
2955 	} else
2956 		sigp = NULL;
2957 
2958 	acb_list32 = malloc(sizeof(uint32_t) * nent, M_LIO, M_WAITOK);
2959 	error = copyin(uap->acb_list, acb_list32, nent * sizeof(uint32_t));
2960 	if (error) {
2961 		free(acb_list32, M_LIO);
2962 		return (error);
2963 	}
2964 	acb_list = malloc(sizeof(struct aiocb *) * nent, M_LIO, M_WAITOK);
2965 	for (i = 0; i < nent; i++)
2966 		acb_list[i] = PTRIN(acb_list32[i]);
2967 	free(acb_list32, M_LIO);
2968 
2969 	error = kern_lio_listio(td, uap->mode,
2970 	    (struct aiocb * const *)uap->acb_list, acb_list, nent, sigp,
2971 	    &aiocb32_ops_osigevent);
2972 	free(acb_list, M_LIO);
2973 	return (error);
2974 }
2975 
2976 int
2977 freebsd32_lio_listio(struct thread *td, struct freebsd32_lio_listio_args *uap)
2978 {
2979 	struct aiocb **acb_list;
2980 	struct sigevent *sigp, sig;
2981 	struct sigevent32 sig32;
2982 	uint32_t *acb_list32;
2983 	int error, i, nent;
2984 
2985 	if ((uap->mode != LIO_NOWAIT) && (uap->mode != LIO_WAIT))
2986 		return (EINVAL);
2987 
2988 	nent = uap->nent;
2989 	if (nent < 0 || nent > AIO_LISTIO_MAX)
2990 		return (EINVAL);
2991 
2992 	if (uap->sig && (uap->mode == LIO_NOWAIT)) {
2993 		error = copyin(uap->sig, &sig32, sizeof(sig32));
2994 		if (error)
2995 			return (error);
2996 		error = convert_sigevent32(&sig32, &sig);
2997 		if (error)
2998 			return (error);
2999 		sigp = &sig;
3000 	} else
3001 		sigp = NULL;
3002 
3003 	acb_list32 = malloc(sizeof(uint32_t) * nent, M_LIO, M_WAITOK);
3004 	error = copyin(uap->acb_list, acb_list32, nent * sizeof(uint32_t));
3005 	if (error) {
3006 		free(acb_list32, M_LIO);
3007 		return (error);
3008 	}
3009 	acb_list = malloc(sizeof(struct aiocb *) * nent, M_LIO, M_WAITOK);
3010 	for (i = 0; i < nent; i++)
3011 		acb_list[i] = PTRIN(acb_list32[i]);
3012 	free(acb_list32, M_LIO);
3013 
3014 	error = kern_lio_listio(td, uap->mode,
3015 	    (struct aiocb * const *)uap->acb_list, acb_list, nent, sigp,
3016 	    &aiocb32_ops);
3017 	free(acb_list, M_LIO);
3018 	return (error);
3019 }
3020 
3021 #endif
3022