xref: /freebsd/sys/kern/vfs_aio.c (revision 7afc53b8dfcc7d5897920ce6cc7e842fbb4ab813)
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 <sys/param.h>
25 #include <sys/systm.h>
26 #include <sys/malloc.h>
27 #include <sys/bio.h>
28 #include <sys/buf.h>
29 #include <sys/eventhandler.h>
30 #include <sys/sysproto.h>
31 #include <sys/filedesc.h>
32 #include <sys/kernel.h>
33 #include <sys/module.h>
34 #include <sys/kthread.h>
35 #include <sys/fcntl.h>
36 #include <sys/file.h>
37 #include <sys/limits.h>
38 #include <sys/lock.h>
39 #include <sys/mutex.h>
40 #include <sys/unistd.h>
41 #include <sys/proc.h>
42 #include <sys/resourcevar.h>
43 #include <sys/signalvar.h>
44 #include <sys/protosw.h>
45 #include <sys/socketvar.h>
46 #include <sys/syscall.h>
47 #include <sys/sysent.h>
48 #include <sys/sysctl.h>
49 #include <sys/sx.h>
50 #include <sys/vnode.h>
51 #include <sys/conf.h>
52 #include <sys/event.h>
53 
54 #include <posix4/posix4.h>
55 #include <vm/vm.h>
56 #include <vm/vm_extern.h>
57 #include <vm/pmap.h>
58 #include <vm/vm_map.h>
59 #include <vm/uma.h>
60 #include <sys/aio.h>
61 
62 #include "opt_vfs_aio.h"
63 
64 NET_NEEDS_GIANT("aio");
65 
66 /*
67  * Counter for allocating reference ids to new jobs.  Wrapped to 1 on
68  * overflow.
69  */
70 static	long jobrefid;
71 
72 #define JOBST_NULL		0x0
73 #define JOBST_JOBQGLOBAL	0x2
74 #define JOBST_JOBRUNNING	0x3
75 #define JOBST_JOBFINISHED	0x4
76 #define	JOBST_JOBQBUF		0x5
77 #define	JOBST_JOBBFINISHED	0x6
78 
79 #ifndef MAX_AIO_PER_PROC
80 #define MAX_AIO_PER_PROC	32
81 #endif
82 
83 #ifndef MAX_AIO_QUEUE_PER_PROC
84 #define MAX_AIO_QUEUE_PER_PROC	256 /* Bigger than AIO_LISTIO_MAX */
85 #endif
86 
87 #ifndef MAX_AIO_PROCS
88 #define MAX_AIO_PROCS		32
89 #endif
90 
91 #ifndef MAX_AIO_QUEUE
92 #define	MAX_AIO_QUEUE		1024 /* Bigger than AIO_LISTIO_MAX */
93 #endif
94 
95 #ifndef TARGET_AIO_PROCS
96 #define TARGET_AIO_PROCS	4
97 #endif
98 
99 #ifndef MAX_BUF_AIO
100 #define MAX_BUF_AIO		16
101 #endif
102 
103 #ifndef AIOD_TIMEOUT_DEFAULT
104 #define	AIOD_TIMEOUT_DEFAULT	(10 * hz)
105 #endif
106 
107 #ifndef AIOD_LIFETIME_DEFAULT
108 #define AIOD_LIFETIME_DEFAULT	(30 * hz)
109 #endif
110 
111 static SYSCTL_NODE(_vfs, OID_AUTO, aio, CTLFLAG_RW, 0, "Async IO management");
112 
113 static int max_aio_procs = MAX_AIO_PROCS;
114 SYSCTL_INT(_vfs_aio, OID_AUTO, max_aio_procs,
115 	CTLFLAG_RW, &max_aio_procs, 0,
116 	"Maximum number of kernel threads to use for handling async IO ");
117 
118 static int num_aio_procs = 0;
119 SYSCTL_INT(_vfs_aio, OID_AUTO, num_aio_procs,
120 	CTLFLAG_RD, &num_aio_procs, 0,
121 	"Number of presently active kernel threads for async IO");
122 
123 /*
124  * The code will adjust the actual number of AIO processes towards this
125  * number when it gets a chance.
126  */
127 static int target_aio_procs = TARGET_AIO_PROCS;
128 SYSCTL_INT(_vfs_aio, OID_AUTO, target_aio_procs, CTLFLAG_RW, &target_aio_procs,
129 	0, "Preferred number of ready kernel threads for async IO");
130 
131 static int max_queue_count = MAX_AIO_QUEUE;
132 SYSCTL_INT(_vfs_aio, OID_AUTO, max_aio_queue, CTLFLAG_RW, &max_queue_count, 0,
133     "Maximum number of aio requests to queue, globally");
134 
135 static int num_queue_count = 0;
136 SYSCTL_INT(_vfs_aio, OID_AUTO, num_queue_count, CTLFLAG_RD, &num_queue_count, 0,
137     "Number of queued aio requests");
138 
139 static int num_buf_aio = 0;
140 SYSCTL_INT(_vfs_aio, OID_AUTO, num_buf_aio, CTLFLAG_RD, &num_buf_aio, 0,
141     "Number of aio requests presently handled by the buf subsystem");
142 
143 /* Number of async I/O thread in the process of being started */
144 /* XXX This should be local to _aio_aqueue() */
145 static int num_aio_resv_start = 0;
146 
147 static int aiod_timeout;
148 SYSCTL_INT(_vfs_aio, OID_AUTO, aiod_timeout, CTLFLAG_RW, &aiod_timeout, 0,
149     "Timeout value for synchronous aio operations");
150 
151 static int aiod_lifetime;
152 SYSCTL_INT(_vfs_aio, OID_AUTO, aiod_lifetime, CTLFLAG_RW, &aiod_lifetime, 0,
153     "Maximum lifetime for idle aiod");
154 
155 static int unloadable = 0;
156 SYSCTL_INT(_vfs_aio, OID_AUTO, unloadable, CTLFLAG_RW, &unloadable, 0,
157     "Allow unload of aio (not recommended)");
158 
159 
160 static int max_aio_per_proc = MAX_AIO_PER_PROC;
161 SYSCTL_INT(_vfs_aio, OID_AUTO, max_aio_per_proc, CTLFLAG_RW, &max_aio_per_proc,
162     0, "Maximum active aio requests per process (stored in the process)");
163 
164 static int max_aio_queue_per_proc = MAX_AIO_QUEUE_PER_PROC;
165 SYSCTL_INT(_vfs_aio, OID_AUTO, max_aio_queue_per_proc, CTLFLAG_RW,
166     &max_aio_queue_per_proc, 0,
167     "Maximum queued aio requests per process (stored in the process)");
168 
169 static int max_buf_aio = MAX_BUF_AIO;
170 SYSCTL_INT(_vfs_aio, OID_AUTO, max_buf_aio, CTLFLAG_RW, &max_buf_aio, 0,
171     "Maximum buf aio requests per process (stored in the process)");
172 
173 struct aiocblist {
174 	TAILQ_ENTRY(aiocblist) list;	/* List of jobs */
175 	TAILQ_ENTRY(aiocblist) plist;	/* List of jobs for proc */
176 	int	jobflags;
177 	int	jobstate;
178 	int	inputcharge;
179 	int	outputcharge;
180 	struct	buf *bp;		/* Buffer pointer */
181 	struct	proc *userproc;		/* User process */ /* Not td! */
182 	struct  ucred *cred;		/* Active credential when created */
183 	struct	file *fd_file;		/* Pointer to file structure */
184 	struct	aio_liojob *lio;	/* Optional lio job */
185 	struct	aiocb *uuaiocb;		/* Pointer in userspace of aiocb */
186 	struct	knlist klist;		/* list of knotes */
187 	struct	aiocb uaiocb;		/* Kernel I/O control block */
188 };
189 
190 /* jobflags */
191 #define AIOCBLIST_RUNDOWN	0x4
192 #define AIOCBLIST_DONE		0x10
193 
194 /*
195  * AIO process info
196  */
197 #define AIOP_FREE	0x1			/* proc on free queue */
198 
199 struct aiothreadlist {
200 	int aiothreadflags;			/* AIO proc flags */
201 	TAILQ_ENTRY(aiothreadlist) list;	/* List of processes */
202 	struct thread *aiothread;		/* The AIO thread */
203 };
204 
205 /*
206  * data-structure for lio signal management
207  */
208 struct aio_liojob {
209 	int	lioj_flags;
210 	int	lioj_buffer_count;
211 	int	lioj_buffer_finished_count;
212 	int	lioj_queue_count;
213 	int	lioj_queue_finished_count;
214 	struct	sigevent lioj_signal;	/* signal on all I/O done */
215 	TAILQ_ENTRY(aio_liojob) lioj_list;
216 	struct	kaioinfo *lioj_ki;
217 };
218 #define	LIOJ_SIGNAL		0x1	/* signal on all done (lio) */
219 #define	LIOJ_SIGNAL_POSTED	0x2	/* signal has been posted */
220 
221 /*
222  * per process aio data structure
223  */
224 struct kaioinfo {
225 	int	kaio_flags;		/* per process kaio flags */
226 	int	kaio_maxactive_count;	/* maximum number of AIOs */
227 	int	kaio_active_count;	/* number of currently used AIOs */
228 	int	kaio_qallowed_count;	/* maxiumu size of AIO queue */
229 	int	kaio_queue_count;	/* size of AIO queue */
230 	int	kaio_ballowed_count;	/* maximum number of buffers */
231 	int	kaio_queue_finished_count; /* number of daemon jobs finished */
232 	int	kaio_buffer_count;	/* number of physio buffers */
233 	int	kaio_buffer_finished_count; /* count of I/O done */
234 	TAILQ_HEAD(,aio_liojob) kaio_liojoblist; /* list of lio jobs */
235 	TAILQ_HEAD(,aiocblist) kaio_jobqueue;	/* job queue for process */
236 	TAILQ_HEAD(,aiocblist) kaio_jobdone;	/* done queue for process */
237 	TAILQ_HEAD(,aiocblist) kaio_bufqueue;	/* buffer job queue for process */
238 	TAILQ_HEAD(,aiocblist) kaio_bufdone;	/* buffer done queue for process */
239 	TAILQ_HEAD(,aiocblist) kaio_sockqueue;	/* queue for aios waiting on sockets */
240 };
241 
242 #define KAIO_RUNDOWN	0x1	/* process is being run down */
243 #define KAIO_WAKEUP	0x2	/* wakeup process when there is a significant event */
244 
245 static TAILQ_HEAD(,aiothreadlist) aio_freeproc;		/* Idle daemons */
246 static struct mtx aio_freeproc_mtx;
247 
248 static TAILQ_HEAD(,aiocblist) aio_jobs;			/* Async job list */
249 
250 static void	aio_init_aioinfo(struct proc *p);
251 static void	aio_onceonly(void);
252 static int	aio_free_entry(struct aiocblist *aiocbe);
253 static void	aio_process(struct aiocblist *aiocbe);
254 static int	aio_newproc(void);
255 static int	aio_aqueue(struct thread *td, struct aiocb *job, int type);
256 static void	aio_physwakeup(struct buf *bp);
257 static void	aio_proc_rundown(void *arg, struct proc *p);
258 static int	aio_fphysio(struct aiocblist *aiocbe);
259 static int	aio_qphysio(struct proc *p, struct aiocblist *iocb);
260 static void	aio_daemon(void *uproc);
261 static void	aio_swake_cb(struct socket *, struct sockbuf *);
262 static int	aio_unload(void);
263 static int	filt_aioattach(struct knote *kn);
264 static void	filt_aiodetach(struct knote *kn);
265 static int	filt_aio(struct knote *kn, long hint);
266 
267 /*
268  * Zones for:
269  * 	kaio	Per process async io info
270  *	aiop	async io thread data
271  *	aiocb	async io jobs
272  *	aiol	list io job pointer - internal to aio_suspend XXX
273  *	aiolio	list io jobs
274  */
275 static uma_zone_t kaio_zone, aiop_zone, aiocb_zone, aiol_zone, aiolio_zone;
276 
277 /* kqueue filters for aio */
278 static struct filterops aio_filtops =
279 	{ 0, filt_aioattach, filt_aiodetach, filt_aio };
280 
281 static eventhandler_tag exit_tag, exec_tag;
282 
283 /*
284  * Main operations function for use as a kernel module.
285  */
286 static int
287 aio_modload(struct module *module, int cmd, void *arg)
288 {
289 	int error = 0;
290 
291 	switch (cmd) {
292 	case MOD_LOAD:
293 		aio_onceonly();
294 		break;
295 	case MOD_UNLOAD:
296 		error = aio_unload();
297 		break;
298 	case MOD_SHUTDOWN:
299 		break;
300 	default:
301 		error = EINVAL;
302 		break;
303 	}
304 	return (error);
305 }
306 
307 static moduledata_t aio_mod = {
308 	"aio",
309 	&aio_modload,
310 	NULL
311 };
312 
313 SYSCALL_MODULE_HELPER(aio_return);
314 SYSCALL_MODULE_HELPER(aio_suspend);
315 SYSCALL_MODULE_HELPER(aio_cancel);
316 SYSCALL_MODULE_HELPER(aio_error);
317 SYSCALL_MODULE_HELPER(aio_read);
318 SYSCALL_MODULE_HELPER(aio_write);
319 SYSCALL_MODULE_HELPER(aio_waitcomplete);
320 SYSCALL_MODULE_HELPER(lio_listio);
321 
322 DECLARE_MODULE(aio, aio_mod,
323 	SI_SUB_VFS, SI_ORDER_ANY);
324 MODULE_VERSION(aio, 1);
325 
326 /*
327  * Startup initialization
328  */
329 static void
330 aio_onceonly(void)
331 {
332 
333 	/* XXX: should probably just use so->callback */
334 	aio_swake = &aio_swake_cb;
335 	exit_tag = EVENTHANDLER_REGISTER(process_exit, aio_proc_rundown, NULL,
336 	    EVENTHANDLER_PRI_ANY);
337 	exec_tag = EVENTHANDLER_REGISTER(process_exec, aio_proc_rundown, NULL,
338 	    EVENTHANDLER_PRI_ANY);
339 	kqueue_add_filteropts(EVFILT_AIO, &aio_filtops);
340 	TAILQ_INIT(&aio_freeproc);
341 	mtx_init(&aio_freeproc_mtx, "aio_freeproc", NULL, MTX_DEF);
342 	TAILQ_INIT(&aio_jobs);
343 	kaio_zone = uma_zcreate("AIO", sizeof(struct kaioinfo), NULL, NULL,
344 	    NULL, NULL, UMA_ALIGN_PTR, UMA_ZONE_NOFREE);
345 	aiop_zone = uma_zcreate("AIOP", sizeof(struct aiothreadlist), NULL,
346 	    NULL, NULL, NULL, UMA_ALIGN_PTR, UMA_ZONE_NOFREE);
347 	aiocb_zone = uma_zcreate("AIOCB", sizeof(struct aiocblist), NULL, NULL,
348 	    NULL, NULL, UMA_ALIGN_PTR, UMA_ZONE_NOFREE);
349 	aiol_zone = uma_zcreate("AIOL", AIO_LISTIO_MAX*sizeof(intptr_t) , NULL,
350 	    NULL, NULL, NULL, UMA_ALIGN_PTR, UMA_ZONE_NOFREE);
351 	aiolio_zone = uma_zcreate("AIOLIO", sizeof(struct aio_liojob), NULL,
352 	    NULL, NULL, NULL, UMA_ALIGN_PTR, UMA_ZONE_NOFREE);
353 	aiod_timeout = AIOD_TIMEOUT_DEFAULT;
354 	aiod_lifetime = AIOD_LIFETIME_DEFAULT;
355 	jobrefid = 1;
356 	async_io_version = _POSIX_VERSION;
357 	p31b_setcfg(CTL_P1003_1B_AIO_LISTIO_MAX, AIO_LISTIO_MAX);
358 	p31b_setcfg(CTL_P1003_1B_AIO_MAX, MAX_AIO_QUEUE);
359 	p31b_setcfg(CTL_P1003_1B_AIO_PRIO_DELTA_MAX, 0);
360 }
361 
362 /*
363  * Callback for unload of AIO when used as a module.
364  */
365 static int
366 aio_unload(void)
367 {
368 	int error;
369 
370 	/*
371 	 * XXX: no unloads by default, it's too dangerous.
372 	 * perhaps we could do it if locked out callers and then
373 	 * did an aio_proc_rundown() on each process.
374 	 */
375 	if (!unloadable)
376 		return (EOPNOTSUPP);
377 
378 	error = kqueue_del_filteropts(EVFILT_AIO);
379 	if (error)
380 		return error;
381 
382 	async_io_version = 0;
383 	aio_swake = NULL;
384 	EVENTHANDLER_DEREGISTER(process_exit, exit_tag);
385 	EVENTHANDLER_DEREGISTER(process_exec, exec_tag);
386 	p31b_setcfg(CTL_P1003_1B_AIO_LISTIO_MAX, -1);
387 	p31b_setcfg(CTL_P1003_1B_AIO_MAX, -1);
388 	p31b_setcfg(CTL_P1003_1B_AIO_PRIO_DELTA_MAX, -1);
389 	return (0);
390 }
391 
392 /*
393  * Init the per-process aioinfo structure.  The aioinfo limits are set
394  * per-process for user limit (resource) management.
395  */
396 static void
397 aio_init_aioinfo(struct proc *p)
398 {
399 	struct kaioinfo *ki;
400 
401 	ki = uma_zalloc(kaio_zone, M_WAITOK);
402 	ki->kaio_flags = 0;
403 	ki->kaio_maxactive_count = max_aio_per_proc;
404 	ki->kaio_active_count = 0;
405 	ki->kaio_qallowed_count = max_aio_queue_per_proc;
406 	ki->kaio_queue_count = 0;
407 	ki->kaio_ballowed_count = max_buf_aio;
408 	ki->kaio_buffer_count = 0;
409 	ki->kaio_buffer_finished_count = 0;
410 	TAILQ_INIT(&ki->kaio_jobdone);
411 	TAILQ_INIT(&ki->kaio_jobqueue);
412 	TAILQ_INIT(&ki->kaio_bufdone);
413 	TAILQ_INIT(&ki->kaio_bufqueue);
414 	TAILQ_INIT(&ki->kaio_liojoblist);
415 	TAILQ_INIT(&ki->kaio_sockqueue);
416 	PROC_LOCK(p);
417 	if (p->p_aioinfo == NULL) {
418 		p->p_aioinfo = ki;
419 		PROC_UNLOCK(p);
420 	} else {
421 		PROC_UNLOCK(p);
422 		uma_zfree(kaio_zone, ki);
423 	}
424 
425 	while (num_aio_procs < target_aio_procs)
426 		aio_newproc();
427 }
428 
429 /*
430  * Free a job entry.  Wait for completion if it is currently active, but don't
431  * delay forever.  If we delay, we return a flag that says that we have to
432  * restart the queue scan.
433  */
434 static int
435 aio_free_entry(struct aiocblist *aiocbe)
436 {
437 	struct kaioinfo *ki;
438 	struct aio_liojob *lj;
439 	struct proc *p;
440 	int error;
441 	int s;
442 
443 	if (aiocbe->jobstate == JOBST_NULL)
444 		panic("aio_free_entry: freeing already free job");
445 
446 	p = aiocbe->userproc;
447 	ki = p->p_aioinfo;
448 	lj = aiocbe->lio;
449 	if (ki == NULL)
450 		panic("aio_free_entry: missing p->p_aioinfo");
451 
452 	while (aiocbe->jobstate == JOBST_JOBRUNNING) {
453 		aiocbe->jobflags |= AIOCBLIST_RUNDOWN;
454 		tsleep(aiocbe, PRIBIO, "jobwai", 0);
455 	}
456 	if (aiocbe->bp == NULL) {
457 		if (ki->kaio_queue_count <= 0)
458 			panic("aio_free_entry: process queue size <= 0");
459 		if (num_queue_count <= 0)
460 			panic("aio_free_entry: system wide queue size <= 0");
461 
462 		if (lj) {
463 			lj->lioj_queue_count--;
464 			if (aiocbe->jobflags & AIOCBLIST_DONE)
465 				lj->lioj_queue_finished_count--;
466 		}
467 		ki->kaio_queue_count--;
468 		if (aiocbe->jobflags & AIOCBLIST_DONE)
469 			ki->kaio_queue_finished_count--;
470 		num_queue_count--;
471 	} else {
472 		if (lj) {
473 			lj->lioj_buffer_count--;
474 			if (aiocbe->jobflags & AIOCBLIST_DONE)
475 				lj->lioj_buffer_finished_count--;
476 		}
477 		if (aiocbe->jobflags & AIOCBLIST_DONE)
478 			ki->kaio_buffer_finished_count--;
479 		ki->kaio_buffer_count--;
480 		num_buf_aio--;
481 	}
482 
483 	/* aiocbe is going away, we need to destroy any knotes */
484 	/* XXXKSE Note the thread here is used to eventually find the
485 	 * owning process again, but it is also used to do a fo_close
486 	 * and that requires the thread. (but does it require the
487 	 * OWNING thread? (or maybe the running thread?)
488 	 * There is a semantic problem here...
489 	 */
490 	knlist_delete(&aiocbe->klist, FIRST_THREAD_IN_PROC(p), 0); /* XXXKSE */
491 
492 	if ((ki->kaio_flags & KAIO_WAKEUP) || ((ki->kaio_flags & KAIO_RUNDOWN)
493 	    && ((ki->kaio_buffer_count == 0) && (ki->kaio_queue_count == 0)))) {
494 		ki->kaio_flags &= ~KAIO_WAKEUP;
495 		wakeup(p);
496 	}
497 
498 	if (aiocbe->jobstate == JOBST_JOBQBUF) {
499 		if ((error = aio_fphysio(aiocbe)) != 0)
500 			return (error);
501 		if (aiocbe->jobstate != JOBST_JOBBFINISHED)
502 			panic("aio_free_entry: invalid physio finish-up state");
503 		s = splbio();
504 		TAILQ_REMOVE(&ki->kaio_bufdone, aiocbe, plist);
505 		splx(s);
506 	} else if (aiocbe->jobstate == JOBST_JOBQGLOBAL) {
507 		s = splnet();
508 		TAILQ_REMOVE(&aio_jobs, aiocbe, list);
509 		TAILQ_REMOVE(&ki->kaio_jobqueue, aiocbe, plist);
510 		splx(s);
511 	} else if (aiocbe->jobstate == JOBST_JOBFINISHED)
512 		TAILQ_REMOVE(&ki->kaio_jobdone, aiocbe, plist);
513 	else if (aiocbe->jobstate == JOBST_JOBBFINISHED) {
514 		s = splbio();
515 		TAILQ_REMOVE(&ki->kaio_bufdone, aiocbe, plist);
516 		splx(s);
517 		if (aiocbe->bp) {
518 			vunmapbuf(aiocbe->bp);
519 			relpbuf(aiocbe->bp, NULL);
520 			aiocbe->bp = NULL;
521 		}
522 	}
523 	if (lj && (lj->lioj_buffer_count == 0) && (lj->lioj_queue_count == 0)) {
524 		TAILQ_REMOVE(&ki->kaio_liojoblist, lj, lioj_list);
525 		uma_zfree(aiolio_zone, lj);
526 	}
527 	aiocbe->jobstate = JOBST_NULL;
528 	fdrop(aiocbe->fd_file, curthread);
529 	crfree(aiocbe->cred);
530 	uma_zfree(aiocb_zone, aiocbe);
531 	return (0);
532 }
533 
534 /*
535  * Rundown the jobs for a given process.
536  */
537 static void
538 aio_proc_rundown(void *arg, struct proc *p)
539 {
540 	int s;
541 	struct kaioinfo *ki;
542 	struct aio_liojob *lj, *ljn;
543 	struct aiocblist *aiocbe, *aiocbn;
544 	struct file *fp;
545 	struct socket *so;
546 
547 	ki = p->p_aioinfo;
548 	if (ki == NULL)
549 		return;
550 
551 	mtx_lock(&Giant);
552 	ki->kaio_flags |= LIOJ_SIGNAL_POSTED;
553 	while ((ki->kaio_active_count > 0) || (ki->kaio_buffer_count >
554 	    ki->kaio_buffer_finished_count)) {
555 		ki->kaio_flags |= KAIO_RUNDOWN;
556 		if (tsleep(p, PRIBIO, "kaiowt", aiod_timeout))
557 			break;
558 	}
559 
560 	/*
561 	 * Move any aio ops that are waiting on socket I/O to the normal job
562 	 * queues so they are cleaned up with any others.
563 	 */
564 	s = splnet();
565 	for (aiocbe = TAILQ_FIRST(&ki->kaio_sockqueue); aiocbe; aiocbe =
566 	    aiocbn) {
567 		aiocbn = TAILQ_NEXT(aiocbe, plist);
568 		fp = aiocbe->fd_file;
569 		if (fp != NULL) {
570 			so = fp->f_data;
571 			TAILQ_REMOVE(&so->so_aiojobq, aiocbe, list);
572 			if (TAILQ_EMPTY(&so->so_aiojobq)) {
573 				SOCKBUF_LOCK(&so->so_snd);
574 				so->so_snd.sb_flags &= ~SB_AIO;
575 				SOCKBUF_UNLOCK(&so->so_snd);
576 				SOCKBUF_LOCK(&so->so_rcv);
577 				so->so_rcv.sb_flags &= ~SB_AIO;
578 				SOCKBUF_UNLOCK(&so->so_rcv);
579 			}
580 		}
581 		TAILQ_REMOVE(&ki->kaio_sockqueue, aiocbe, plist);
582 		TAILQ_INSERT_HEAD(&aio_jobs, aiocbe, list);
583 		TAILQ_INSERT_HEAD(&ki->kaio_jobqueue, aiocbe, plist);
584 	}
585 	splx(s);
586 
587 restart1:
588 	for (aiocbe = TAILQ_FIRST(&ki->kaio_jobdone); aiocbe; aiocbe = aiocbn) {
589 		aiocbn = TAILQ_NEXT(aiocbe, plist);
590 		if (aio_free_entry(aiocbe))
591 			goto restart1;
592 	}
593 
594 restart2:
595 	for (aiocbe = TAILQ_FIRST(&ki->kaio_jobqueue); aiocbe; aiocbe =
596 	    aiocbn) {
597 		aiocbn = TAILQ_NEXT(aiocbe, plist);
598 		if (aio_free_entry(aiocbe))
599 			goto restart2;
600 	}
601 
602 /*
603  * Note the use of lots of splbio here, trying to avoid splbio for long chains
604  * of I/O.  Probably unnecessary.
605  */
606 restart3:
607 	s = splbio();
608 	while (TAILQ_FIRST(&ki->kaio_bufqueue)) {
609 		ki->kaio_flags |= KAIO_WAKEUP;
610 		tsleep(p, PRIBIO, "aioprn", 0);
611 		splx(s);
612 		goto restart3;
613 	}
614 	splx(s);
615 
616 restart4:
617 	s = splbio();
618 	for (aiocbe = TAILQ_FIRST(&ki->kaio_bufdone); aiocbe; aiocbe = aiocbn) {
619 		aiocbn = TAILQ_NEXT(aiocbe, plist);
620 		if (aio_free_entry(aiocbe)) {
621 			splx(s);
622 			goto restart4;
623 		}
624 	}
625 	splx(s);
626 
627 	/*
628 	 * If we've slept, jobs might have moved from one queue to another.
629 	 * Retry rundown if we didn't manage to empty the queues.
630 	 */
631 	if (TAILQ_FIRST(&ki->kaio_jobdone) != NULL ||
632 	    TAILQ_FIRST(&ki->kaio_jobqueue) != NULL ||
633 	    TAILQ_FIRST(&ki->kaio_bufqueue) != NULL ||
634 	    TAILQ_FIRST(&ki->kaio_bufdone) != NULL)
635 		goto restart1;
636 
637 	for (lj = TAILQ_FIRST(&ki->kaio_liojoblist); lj; lj = ljn) {
638 		ljn = TAILQ_NEXT(lj, lioj_list);
639 		if ((lj->lioj_buffer_count == 0) && (lj->lioj_queue_count ==
640 		    0)) {
641 			TAILQ_REMOVE(&ki->kaio_liojoblist, lj, lioj_list);
642 			uma_zfree(aiolio_zone, lj);
643 		} else {
644 #ifdef DIAGNOSTIC
645 			printf("LIO job not cleaned up: B:%d, BF:%d, Q:%d, "
646 			    "QF:%d\n", lj->lioj_buffer_count,
647 			    lj->lioj_buffer_finished_count,
648 			    lj->lioj_queue_count,
649 			    lj->lioj_queue_finished_count);
650 #endif
651 		}
652 	}
653 
654 	uma_zfree(kaio_zone, ki);
655 	p->p_aioinfo = NULL;
656 	mtx_unlock(&Giant);
657 }
658 
659 /*
660  * Select a job to run (called by an AIO daemon).
661  */
662 static struct aiocblist *
663 aio_selectjob(struct aiothreadlist *aiop)
664 {
665 	int s;
666 	struct aiocblist *aiocbe;
667 	struct kaioinfo *ki;
668 	struct proc *userp;
669 
670 	s = splnet();
671 	for (aiocbe = TAILQ_FIRST(&aio_jobs); aiocbe; aiocbe =
672 	    TAILQ_NEXT(aiocbe, list)) {
673 		userp = aiocbe->userproc;
674 		ki = userp->p_aioinfo;
675 
676 		if (ki->kaio_active_count < ki->kaio_maxactive_count) {
677 			TAILQ_REMOVE(&aio_jobs, aiocbe, list);
678 			splx(s);
679 			return (aiocbe);
680 		}
681 	}
682 	splx(s);
683 
684 	return (NULL);
685 }
686 
687 /*
688  * The AIO processing activity.  This is the code that does the I/O request for
689  * the non-physio version of the operations.  The normal vn operations are used,
690  * and this code should work in all instances for every type of file, including
691  * pipes, sockets, fifos, and regular files.
692  */
693 static void
694 aio_process(struct aiocblist *aiocbe)
695 {
696 	struct ucred *td_savedcred;
697 	struct thread *td;
698 	struct proc *mycp;
699 	struct aiocb *cb;
700 	struct file *fp;
701 	struct uio auio;
702 	struct iovec aiov;
703 	int cnt;
704 	int error;
705 	int oublock_st, oublock_end;
706 	int inblock_st, inblock_end;
707 
708 	td = curthread;
709 	td_savedcred = td->td_ucred;
710 	td->td_ucred = aiocbe->cred;
711 	mycp = td->td_proc;
712 	cb = &aiocbe->uaiocb;
713 	fp = aiocbe->fd_file;
714 
715 	aiov.iov_base = (void *)(uintptr_t)cb->aio_buf;
716 	aiov.iov_len = cb->aio_nbytes;
717 
718 	auio.uio_iov = &aiov;
719 	auio.uio_iovcnt = 1;
720 	auio.uio_offset = cb->aio_offset;
721 	auio.uio_resid = cb->aio_nbytes;
722 	cnt = cb->aio_nbytes;
723 	auio.uio_segflg = UIO_USERSPACE;
724 	auio.uio_td = td;
725 
726 	inblock_st = mycp->p_stats->p_ru.ru_inblock;
727 	oublock_st = mycp->p_stats->p_ru.ru_oublock;
728 	/*
729 	 * _aio_aqueue() acquires a reference to the file that is
730 	 * released in aio_free_entry().
731 	 */
732 	if (cb->aio_lio_opcode == LIO_READ) {
733 		auio.uio_rw = UIO_READ;
734 		error = fo_read(fp, &auio, fp->f_cred, FOF_OFFSET, td);
735 	} else {
736 		auio.uio_rw = UIO_WRITE;
737 		error = fo_write(fp, &auio, fp->f_cred, FOF_OFFSET, td);
738 	}
739 	inblock_end = mycp->p_stats->p_ru.ru_inblock;
740 	oublock_end = mycp->p_stats->p_ru.ru_oublock;
741 
742 	aiocbe->inputcharge = inblock_end - inblock_st;
743 	aiocbe->outputcharge = oublock_end - oublock_st;
744 
745 	if ((error) && (auio.uio_resid != cnt)) {
746 		if (error == ERESTART || error == EINTR || error == EWOULDBLOCK)
747 			error = 0;
748 		if ((error == EPIPE) && (cb->aio_lio_opcode == LIO_WRITE)) {
749 			PROC_LOCK(aiocbe->userproc);
750 			psignal(aiocbe->userproc, SIGPIPE);
751 			PROC_UNLOCK(aiocbe->userproc);
752 		}
753 	}
754 
755 	cnt -= auio.uio_resid;
756 	cb->_aiocb_private.error = error;
757 	cb->_aiocb_private.status = cnt;
758 	td->td_ucred = td_savedcred;
759 }
760 
761 /*
762  * The AIO daemon, most of the actual work is done in aio_process,
763  * but the setup (and address space mgmt) is done in this routine.
764  */
765 static void
766 aio_daemon(void *uproc)
767 {
768 	int s;
769 	struct aio_liojob *lj;
770 	struct aiocb *cb;
771 	struct aiocblist *aiocbe;
772 	struct aiothreadlist *aiop;
773 	struct kaioinfo *ki;
774 	struct proc *curcp, *mycp, *userp;
775 	struct vmspace *myvm, *tmpvm;
776 	struct thread *td = curthread;
777 	struct pgrp *newpgrp;
778 	struct session *newsess;
779 
780 	/*
781 	 * Local copies of curproc (cp) and vmspace (myvm)
782 	 */
783 	mycp = td->td_proc;
784 	myvm = mycp->p_vmspace;
785 
786 	KASSERT(mycp->p_textvp == NULL, ("kthread has a textvp"));
787 
788 	/*
789 	 * Allocate and ready the aio control info.  There is one aiop structure
790 	 * per daemon.
791 	 */
792 	aiop = uma_zalloc(aiop_zone, M_WAITOK);
793 	aiop->aiothread = td;
794 	aiop->aiothreadflags |= AIOP_FREE;
795 
796 	/*
797 	 * Place thread (lightweight process) onto the AIO free thread list.
798 	 */
799 	mtx_lock(&aio_freeproc_mtx);
800 	TAILQ_INSERT_HEAD(&aio_freeproc, aiop, list);
801 	mtx_unlock(&aio_freeproc_mtx);
802 
803 	/*
804 	 * Get rid of our current filedescriptors.  AIOD's don't need any
805 	 * filedescriptors, except as temporarily inherited from the client.
806 	 */
807 	mtx_lock(&Giant);
808 	fdfree(td);
809 
810 	mtx_unlock(&Giant);
811 	/* The daemon resides in its own pgrp. */
812 	MALLOC(newpgrp, struct pgrp *, sizeof(struct pgrp), M_PGRP,
813 		M_WAITOK | M_ZERO);
814 	MALLOC(newsess, struct session *, sizeof(struct session), M_SESSION,
815 		M_WAITOK | M_ZERO);
816 
817 	sx_xlock(&proctree_lock);
818 	enterpgrp(mycp, mycp->p_pid, newpgrp, newsess);
819 	sx_xunlock(&proctree_lock);
820 	mtx_lock(&Giant);
821 
822 	/*
823 	 * Wakeup parent process.  (Parent sleeps to keep from blasting away
824 	 * and creating too many daemons.)
825 	 */
826 	wakeup(mycp);
827 
828 	for (;;) {
829 		/*
830 		 * curcp is the current daemon process context.
831 		 * userp is the current user process context.
832 		 */
833 		curcp = mycp;
834 
835 		/*
836 		 * Take daemon off of free queue
837 		 */
838 		mtx_lock(&aio_freeproc_mtx);
839 		if (aiop->aiothreadflags & AIOP_FREE) {
840 			TAILQ_REMOVE(&aio_freeproc, aiop, list);
841 			aiop->aiothreadflags &= ~AIOP_FREE;
842 		}
843 		mtx_unlock(&aio_freeproc_mtx);
844 
845 		/*
846 		 * Check for jobs.
847 		 */
848 		while ((aiocbe = aio_selectjob(aiop)) != NULL) {
849 			cb = &aiocbe->uaiocb;
850 			userp = aiocbe->userproc;
851 
852 			aiocbe->jobstate = JOBST_JOBRUNNING;
853 
854 			/*
855 			 * Connect to process address space for user program.
856 			 */
857 			if (userp != curcp) {
858 				/*
859 				 * Save the current address space that we are
860 				 * connected to.
861 				 */
862 				tmpvm = mycp->p_vmspace;
863 
864 				/*
865 				 * Point to the new user address space, and
866 				 * refer to it.
867 				 */
868 				mycp->p_vmspace = userp->p_vmspace;
869 				atomic_add_int(&mycp->p_vmspace->vm_refcnt, 1);
870 
871 				/* Activate the new mapping. */
872 				pmap_activate(FIRST_THREAD_IN_PROC(mycp));
873 
874 				/*
875 				 * If the old address space wasn't the daemons
876 				 * own address space, then we need to remove the
877 				 * daemon's reference from the other process
878 				 * that it was acting on behalf of.
879 				 */
880 				if (tmpvm != myvm) {
881 					vmspace_free(tmpvm);
882 				}
883 				curcp = userp;
884 			}
885 
886 			ki = userp->p_aioinfo;
887 			lj = aiocbe->lio;
888 
889 			/* Account for currently active jobs. */
890 			ki->kaio_active_count++;
891 
892 			/* Do the I/O function. */
893 			aio_process(aiocbe);
894 
895 			/* Decrement the active job count. */
896 			ki->kaio_active_count--;
897 
898 			/*
899 			 * Increment the completion count for wakeup/signal
900 			 * comparisons.
901 			 */
902 			aiocbe->jobflags |= AIOCBLIST_DONE;
903 			ki->kaio_queue_finished_count++;
904 			if (lj)
905 				lj->lioj_queue_finished_count++;
906 			if ((ki->kaio_flags & KAIO_WAKEUP) || ((ki->kaio_flags
907 			    & KAIO_RUNDOWN) && (ki->kaio_active_count == 0))) {
908 				ki->kaio_flags &= ~KAIO_WAKEUP;
909 				wakeup(userp);
910 			}
911 
912 			s = splbio();
913 			if (lj && (lj->lioj_flags &
914 			    (LIOJ_SIGNAL|LIOJ_SIGNAL_POSTED)) == LIOJ_SIGNAL) {
915 				if ((lj->lioj_queue_finished_count ==
916 				    lj->lioj_queue_count) &&
917 				    (lj->lioj_buffer_finished_count ==
918 				    lj->lioj_buffer_count)) {
919 					PROC_LOCK(userp);
920 					psignal(userp,
921 					    lj->lioj_signal.sigev_signo);
922 					PROC_UNLOCK(userp);
923 					lj->lioj_flags |= LIOJ_SIGNAL_POSTED;
924 				}
925 			}
926 			splx(s);
927 
928 			aiocbe->jobstate = JOBST_JOBFINISHED;
929 
930 			s = splnet();
931 			TAILQ_REMOVE(&ki->kaio_jobqueue, aiocbe, plist);
932 			TAILQ_INSERT_TAIL(&ki->kaio_jobdone, aiocbe, plist);
933 			splx(s);
934 			KNOTE_UNLOCKED(&aiocbe->klist, 0);
935 
936 			if (aiocbe->jobflags & AIOCBLIST_RUNDOWN) {
937 				wakeup(aiocbe);
938 				aiocbe->jobflags &= ~AIOCBLIST_RUNDOWN;
939 			}
940 
941 			if (cb->aio_sigevent.sigev_notify == SIGEV_SIGNAL) {
942 				PROC_LOCK(userp);
943 				psignal(userp, cb->aio_sigevent.sigev_signo);
944 				PROC_UNLOCK(userp);
945 			}
946 		}
947 
948 		/*
949 		 * Disconnect from user address space.
950 		 */
951 		if (curcp != mycp) {
952 			/* Get the user address space to disconnect from. */
953 			tmpvm = mycp->p_vmspace;
954 
955 			/* Get original address space for daemon. */
956 			mycp->p_vmspace = myvm;
957 
958 			/* Activate the daemon's address space. */
959 			pmap_activate(FIRST_THREAD_IN_PROC(mycp));
960 #ifdef DIAGNOSTIC
961 			if (tmpvm == myvm) {
962 				printf("AIOD: vmspace problem -- %d\n",
963 				    mycp->p_pid);
964 			}
965 #endif
966 			/* Remove our vmspace reference. */
967 			vmspace_free(tmpvm);
968 
969 			curcp = mycp;
970 		}
971 
972 		mtx_lock(&aio_freeproc_mtx);
973 		TAILQ_INSERT_HEAD(&aio_freeproc, aiop, list);
974 		aiop->aiothreadflags |= AIOP_FREE;
975 
976 		/*
977 		 * If daemon is inactive for a long time, allow it to exit,
978 		 * thereby freeing resources.
979 		 */
980 		if (msleep(aiop->aiothread, &aio_freeproc_mtx, PDROP | PRIBIO,
981 		    "aiordy", aiod_lifetime)) {
982 			s = splnet();
983 			if (TAILQ_EMPTY(&aio_jobs)) {
984 				mtx_lock(&aio_freeproc_mtx);
985 				if ((aiop->aiothreadflags & AIOP_FREE) &&
986 				    (num_aio_procs > target_aio_procs)) {
987 					TAILQ_REMOVE(&aio_freeproc, aiop, list);
988 					mtx_unlock(&aio_freeproc_mtx);
989 					splx(s);
990 					uma_zfree(aiop_zone, aiop);
991 					num_aio_procs--;
992 #ifdef DIAGNOSTIC
993 					if (mycp->p_vmspace->vm_refcnt <= 1) {
994 						printf("AIOD: bad vm refcnt for"
995 						    " exiting daemon: %d\n",
996 						    mycp->p_vmspace->vm_refcnt);
997 					}
998 #endif
999 					kthread_exit(0);
1000 				}
1001 				mtx_unlock(&aio_freeproc_mtx);
1002 			}
1003 			splx(s);
1004 		}
1005 	}
1006 }
1007 
1008 /*
1009  * Create a new AIO daemon.  This is mostly a kernel-thread fork routine.  The
1010  * AIO daemon modifies its environment itself.
1011  */
1012 static int
1013 aio_newproc(void)
1014 {
1015 	int error;
1016 	struct proc *p;
1017 
1018 	error = kthread_create(aio_daemon, curproc, &p, RFNOWAIT, 0, "aiod%d",
1019 	    num_aio_procs);
1020 	if (error)
1021 		return (error);
1022 
1023 	/*
1024 	 * Wait until daemon is started, but continue on just in case to
1025 	 * handle error conditions.
1026 	 */
1027 	error = tsleep(p, PZERO, "aiosta", aiod_timeout);
1028 
1029 	num_aio_procs++;
1030 
1031 	return (error);
1032 }
1033 
1034 /*
1035  * Try the high-performance, low-overhead physio method for eligible
1036  * VCHR devices.  This method doesn't use an aio helper thread, and
1037  * thus has very low overhead.
1038  *
1039  * Assumes that the caller, _aio_aqueue(), has incremented the file
1040  * structure's reference count, preventing its deallocation for the
1041  * duration of this call.
1042  */
1043 static int
1044 aio_qphysio(struct proc *p, struct aiocblist *aiocbe)
1045 {
1046 	int error;
1047 	struct aiocb *cb;
1048 	struct file *fp;
1049 	struct buf *bp;
1050 	struct vnode *vp;
1051 	struct kaioinfo *ki;
1052 	struct aio_liojob *lj;
1053 	int s;
1054 	int notify;
1055 
1056 	cb = &aiocbe->uaiocb;
1057 	fp = aiocbe->fd_file;
1058 
1059 	if (fp->f_type != DTYPE_VNODE)
1060 		return (-1);
1061 
1062 	vp = fp->f_vnode;
1063 
1064 	/*
1065 	 * If its not a disk, we don't want to return a positive error.
1066 	 * It causes the aio code to not fall through to try the thread
1067 	 * way when you're talking to a regular file.
1068 	 */
1069 	if (!vn_isdisk(vp, &error)) {
1070 		if (error == ENOTBLK)
1071 			return (-1);
1072 		else
1073 			return (error);
1074 	}
1075 
1076  	if (cb->aio_nbytes % vp->v_bufobj.bo_bsize)
1077 		return (-1);
1078 
1079 	if (cb->aio_nbytes >
1080 	    MAXPHYS - (((vm_offset_t) cb->aio_buf) & PAGE_MASK))
1081 		return (-1);
1082 
1083 	ki = p->p_aioinfo;
1084 	if (ki->kaio_buffer_count >= ki->kaio_ballowed_count)
1085 		return (-1);
1086 
1087 	ki->kaio_buffer_count++;
1088 
1089 	lj = aiocbe->lio;
1090 	if (lj)
1091 		lj->lioj_buffer_count++;
1092 
1093 	/* Create and build a buffer header for a transfer. */
1094 	bp = (struct buf *)getpbuf(NULL);
1095 	BUF_KERNPROC(bp);
1096 
1097 	/*
1098 	 * Get a copy of the kva from the physical buffer.
1099 	 */
1100 	error = 0;
1101 
1102 	bp->b_bcount = cb->aio_nbytes;
1103 	bp->b_bufsize = cb->aio_nbytes;
1104 	bp->b_iodone = aio_physwakeup;
1105 	bp->b_saveaddr = bp->b_data;
1106 	bp->b_data = (void *)(uintptr_t)cb->aio_buf;
1107 	bp->b_offset = cb->aio_offset;
1108 	bp->b_iooffset = cb->aio_offset;
1109 	bp->b_blkno = btodb(cb->aio_offset);
1110 	bp->b_iocmd = cb->aio_lio_opcode == LIO_WRITE ? BIO_WRITE : BIO_READ;
1111 
1112 	/*
1113 	 * Bring buffer into kernel space.
1114 	 */
1115 	if (vmapbuf(bp) < 0) {
1116 		error = EFAULT;
1117 		goto doerror;
1118 	}
1119 
1120 	s = splbio();
1121 	aiocbe->bp = bp;
1122 	bp->b_caller1 = (void *)aiocbe;
1123 	TAILQ_INSERT_TAIL(&ki->kaio_bufqueue, aiocbe, plist);
1124 	aiocbe->jobstate = JOBST_JOBQBUF;
1125 	cb->_aiocb_private.status = cb->aio_nbytes;
1126 	num_buf_aio++;
1127 	bp->b_error = 0;
1128 
1129 	splx(s);
1130 
1131 	/* Perform transfer. */
1132 	dev_strategy(vp->v_rdev, bp);
1133 
1134 	notify = 0;
1135 	s = splbio();
1136 
1137 	/*
1138 	 * If we had an error invoking the request, or an error in processing
1139 	 * the request before we have returned, we process it as an error in
1140 	 * transfer.  Note that such an I/O error is not indicated immediately,
1141 	 * but is returned using the aio_error mechanism.  In this case,
1142 	 * aio_suspend will return immediately.
1143 	 */
1144 	if (bp->b_error || (bp->b_ioflags & BIO_ERROR)) {
1145 		struct aiocb *job = aiocbe->uuaiocb;
1146 
1147 		aiocbe->uaiocb._aiocb_private.status = 0;
1148 		suword(&job->_aiocb_private.status, 0);
1149 		aiocbe->uaiocb._aiocb_private.error = bp->b_error;
1150 		suword(&job->_aiocb_private.error, bp->b_error);
1151 
1152 		ki->kaio_buffer_finished_count++;
1153 
1154 		if (aiocbe->jobstate != JOBST_JOBBFINISHED) {
1155 			aiocbe->jobstate = JOBST_JOBBFINISHED;
1156 			aiocbe->jobflags |= AIOCBLIST_DONE;
1157 			TAILQ_REMOVE(&ki->kaio_bufqueue, aiocbe, plist);
1158 			TAILQ_INSERT_TAIL(&ki->kaio_bufdone, aiocbe, plist);
1159 			notify = 1;
1160 		}
1161 	}
1162 	splx(s);
1163 	if (notify)
1164 		KNOTE_UNLOCKED(&aiocbe->klist, 0);
1165 	return (0);
1166 
1167 doerror:
1168 	ki->kaio_buffer_count--;
1169 	if (lj)
1170 		lj->lioj_buffer_count--;
1171 	aiocbe->bp = NULL;
1172 	relpbuf(bp, NULL);
1173 	return (error);
1174 }
1175 
1176 /*
1177  * This waits/tests physio completion.
1178  */
1179 static int
1180 aio_fphysio(struct aiocblist *iocb)
1181 {
1182 	int s;
1183 	struct buf *bp;
1184 	int error;
1185 
1186 	bp = iocb->bp;
1187 
1188 	s = splbio();
1189 	while ((bp->b_flags & B_DONE) == 0) {
1190 		if (tsleep(bp, PRIBIO, "physstr", aiod_timeout)) {
1191 			if ((bp->b_flags & B_DONE) == 0) {
1192 				splx(s);
1193 				return (EINPROGRESS);
1194 			} else
1195 				break;
1196 		}
1197 	}
1198 	splx(s);
1199 
1200 	/* Release mapping into kernel space. */
1201 	vunmapbuf(bp);
1202 	iocb->bp = 0;
1203 
1204 	error = 0;
1205 
1206 	/* Check for an error. */
1207 	if (bp->b_ioflags & BIO_ERROR)
1208 		error = bp->b_error;
1209 
1210 	relpbuf(bp, NULL);
1211 	return (error);
1212 }
1213 
1214 /*
1215  * Wake up aio requests that may be serviceable now.
1216  */
1217 static void
1218 aio_swake_cb(struct socket *so, struct sockbuf *sb)
1219 {
1220 	struct aiocblist *cb,*cbn;
1221 	struct proc *p;
1222 	struct kaioinfo *ki = NULL;
1223 	int opcode, wakecount = 0;
1224 	struct aiothreadlist *aiop;
1225 
1226 	if (sb == &so->so_snd) {
1227 		opcode = LIO_WRITE;
1228 		SOCKBUF_LOCK(&so->so_snd);
1229 		so->so_snd.sb_flags &= ~SB_AIO;
1230 		SOCKBUF_UNLOCK(&so->so_snd);
1231 	} else {
1232 		opcode = LIO_READ;
1233 		SOCKBUF_LOCK(&so->so_rcv);
1234 		so->so_rcv.sb_flags &= ~SB_AIO;
1235 		SOCKBUF_UNLOCK(&so->so_rcv);
1236 	}
1237 
1238 	for (cb = TAILQ_FIRST(&so->so_aiojobq); cb; cb = cbn) {
1239 		cbn = TAILQ_NEXT(cb, list);
1240 		if (opcode == cb->uaiocb.aio_lio_opcode) {
1241 			p = cb->userproc;
1242 			ki = p->p_aioinfo;
1243 			TAILQ_REMOVE(&so->so_aiojobq, cb, list);
1244 			TAILQ_REMOVE(&ki->kaio_sockqueue, cb, plist);
1245 			TAILQ_INSERT_TAIL(&aio_jobs, cb, list);
1246 			TAILQ_INSERT_TAIL(&ki->kaio_jobqueue, cb, plist);
1247 			wakecount++;
1248 			if (cb->jobstate != JOBST_JOBQGLOBAL)
1249 				panic("invalid queue value");
1250 		}
1251 	}
1252 
1253 	while (wakecount--) {
1254 		mtx_lock(&aio_freeproc_mtx);
1255 		if ((aiop = TAILQ_FIRST(&aio_freeproc)) != 0) {
1256 			TAILQ_REMOVE(&aio_freeproc, aiop, list);
1257 			aiop->aiothreadflags &= ~AIOP_FREE;
1258 			wakeup(aiop->aiothread);
1259 		}
1260 		mtx_unlock(&aio_freeproc_mtx);
1261 	}
1262 }
1263 
1264 /*
1265  * Queue a new AIO request.  Choosing either the threaded or direct physio VCHR
1266  * technique is done in this code.
1267  */
1268 static int
1269 _aio_aqueue(struct thread *td, struct aiocb *job, struct aio_liojob *lj, int type)
1270 {
1271 	struct proc *p = td->td_proc;
1272 	struct filedesc *fdp;
1273 	struct file *fp;
1274 	unsigned int fd;
1275 	struct socket *so;
1276 	int s;
1277 	int error;
1278 	int opcode;
1279 	struct aiocblist *aiocbe;
1280 	struct aiothreadlist *aiop;
1281 	struct kaioinfo *ki;
1282 	struct kevent kev;
1283 	struct kqueue *kq;
1284 	struct file *kq_fp;
1285 	struct sockbuf *sb;
1286 
1287 	aiocbe = uma_zalloc(aiocb_zone, M_WAITOK);
1288 	aiocbe->inputcharge = 0;
1289 	aiocbe->outputcharge = 0;
1290 	/* XXX - need a lock */
1291 	knlist_init(&aiocbe->klist, NULL);
1292 
1293 	suword(&job->_aiocb_private.status, -1);
1294 	suword(&job->_aiocb_private.error, 0);
1295 	suword(&job->_aiocb_private.kernelinfo, -1);
1296 
1297 	error = copyin(job, &aiocbe->uaiocb, sizeof(aiocbe->uaiocb));
1298 	if (error) {
1299 		suword(&job->_aiocb_private.error, error);
1300 		uma_zfree(aiocb_zone, aiocbe);
1301 		return (error);
1302 	}
1303 	if (aiocbe->uaiocb.aio_sigevent.sigev_notify == SIGEV_SIGNAL &&
1304 		!_SIG_VALID(aiocbe->uaiocb.aio_sigevent.sigev_signo)) {
1305 		uma_zfree(aiocb_zone, aiocbe);
1306 		return (EINVAL);
1307 	}
1308 
1309 	/* Save userspace address of the job info. */
1310 	aiocbe->uuaiocb = job;
1311 
1312 	/* Get the opcode. */
1313 	if (type != LIO_NOP)
1314 		aiocbe->uaiocb.aio_lio_opcode = type;
1315 	opcode = aiocbe->uaiocb.aio_lio_opcode;
1316 
1317 	/* Get the fd info for process. */
1318 	fdp = p->p_fd;
1319 
1320 	/*
1321 	 * Range check file descriptor.
1322 	 */
1323 	FILEDESC_LOCK(fdp);
1324 	fd = aiocbe->uaiocb.aio_fildes;
1325 	if (fd >= fdp->fd_nfiles) {
1326 		FILEDESC_UNLOCK(fdp);
1327 		uma_zfree(aiocb_zone, aiocbe);
1328 		if (type == 0)
1329 			suword(&job->_aiocb_private.error, EBADF);
1330 		return (EBADF);
1331 	}
1332 
1333 	fp = aiocbe->fd_file = fdp->fd_ofiles[fd];
1334 	if ((fp == NULL) ||
1335 	    ((opcode == LIO_WRITE) && ((fp->f_flag & FWRITE) == 0)) ||
1336 	    ((opcode == LIO_READ) && ((fp->f_flag & FREAD) == 0))) {
1337 		FILEDESC_UNLOCK(fdp);
1338 		uma_zfree(aiocb_zone, aiocbe);
1339 		if (type == 0)
1340 			suword(&job->_aiocb_private.error, EBADF);
1341 		return (EBADF);
1342 	}
1343 	fhold(fp);
1344 	FILEDESC_UNLOCK(fdp);
1345 
1346 	if (aiocbe->uaiocb.aio_offset == -1LL) {
1347 		error = EINVAL;
1348 		goto aqueue_fail;
1349 	}
1350 	error = suword(&job->_aiocb_private.kernelinfo, jobrefid);
1351 	if (error) {
1352 		error = EINVAL;
1353 		goto aqueue_fail;
1354 	}
1355 	aiocbe->uaiocb._aiocb_private.kernelinfo = (void *)(intptr_t)jobrefid;
1356 	if (jobrefid == LONG_MAX)
1357 		jobrefid = 1;
1358 	else
1359 		jobrefid++;
1360 
1361 	if (opcode == LIO_NOP) {
1362 		fdrop(fp, td);
1363 		uma_zfree(aiocb_zone, aiocbe);
1364 		if (type == 0) {
1365 			suword(&job->_aiocb_private.error, 0);
1366 			suword(&job->_aiocb_private.status, 0);
1367 			suword(&job->_aiocb_private.kernelinfo, 0);
1368 		}
1369 		return (0);
1370 	}
1371 	if ((opcode != LIO_READ) && (opcode != LIO_WRITE)) {
1372 		if (type == 0)
1373 			suword(&job->_aiocb_private.status, 0);
1374 		error = EINVAL;
1375 		goto aqueue_fail;
1376 	}
1377 
1378 	if (aiocbe->uaiocb.aio_sigevent.sigev_notify == SIGEV_KEVENT) {
1379 		kev.ident = aiocbe->uaiocb.aio_sigevent.sigev_notify_kqueue;
1380 		kev.udata = aiocbe->uaiocb.aio_sigevent.sigev_value.sigval_ptr;
1381 	} else
1382 		goto no_kqueue;
1383 	if ((u_int)kev.ident >= fdp->fd_nfiles ||
1384 	    (kq_fp = fdp->fd_ofiles[kev.ident]) == NULL ||
1385 	    (kq_fp->f_type != DTYPE_KQUEUE)) {
1386 		error = EBADF;
1387 		goto aqueue_fail;
1388 	}
1389 	kq = kq_fp->f_data;
1390 	kev.ident = (uintptr_t)aiocbe->uuaiocb;
1391 	kev.filter = EVFILT_AIO;
1392 	kev.flags = EV_ADD | EV_ENABLE | EV_FLAG1;
1393 	kev.data = (intptr_t)aiocbe;
1394 	error = kqueue_register(kq, &kev, td, 1);
1395 aqueue_fail:
1396 	if (error) {
1397 		fdrop(fp, td);
1398 		uma_zfree(aiocb_zone, aiocbe);
1399 		if (type == 0)
1400 			suword(&job->_aiocb_private.error, error);
1401 		goto done;
1402 	}
1403 no_kqueue:
1404 
1405 	suword(&job->_aiocb_private.error, EINPROGRESS);
1406 	aiocbe->uaiocb._aiocb_private.error = EINPROGRESS;
1407 	aiocbe->userproc = p;
1408 	aiocbe->cred = crhold(td->td_ucred);
1409 	aiocbe->jobflags = 0;
1410 	aiocbe->lio = lj;
1411 	ki = p->p_aioinfo;
1412 
1413 	if (fp->f_type == DTYPE_SOCKET) {
1414 		/*
1415 		 * Alternate queueing for socket ops: Reach down into the
1416 		 * descriptor to get the socket data.  Then check to see if the
1417 		 * socket is ready to be read or written (based on the requested
1418 		 * operation).
1419 		 *
1420 		 * If it is not ready for io, then queue the aiocbe on the
1421 		 * socket, and set the flags so we get a call when sbnotify()
1422 		 * happens.
1423 		 *
1424 		 * Note if opcode is neither LIO_WRITE nor LIO_READ we lock
1425 		 * and unlock the snd sockbuf for no reason.
1426 		 */
1427 		so = fp->f_data;
1428 		sb = (opcode == LIO_READ) ? &so->so_rcv : &so->so_snd;
1429 		SOCKBUF_LOCK(sb);
1430 		s = splnet();
1431 		if (((opcode == LIO_READ) && (!soreadable(so))) || ((opcode ==
1432 		    LIO_WRITE) && (!sowriteable(so)))) {
1433 			TAILQ_INSERT_TAIL(&so->so_aiojobq, aiocbe, list);
1434 			TAILQ_INSERT_TAIL(&ki->kaio_sockqueue, aiocbe, plist);
1435 			sb->sb_flags |= SB_AIO;
1436 			aiocbe->jobstate = JOBST_JOBQGLOBAL; /* XXX */
1437 			ki->kaio_queue_count++;
1438 			num_queue_count++;
1439 			SOCKBUF_UNLOCK(sb);
1440 			splx(s);
1441 			error = 0;
1442 			goto done;
1443 		}
1444 		SOCKBUF_UNLOCK(sb);
1445 		splx(s);
1446 	}
1447 
1448 	if ((error = aio_qphysio(p, aiocbe)) == 0)
1449 		goto done;
1450 	if (error > 0) {
1451 		suword(&job->_aiocb_private.status, 0);
1452 		aiocbe->uaiocb._aiocb_private.error = error;
1453 		suword(&job->_aiocb_private.error, error);
1454 		goto done;
1455 	}
1456 
1457 	/* No buffer for daemon I/O. */
1458 	aiocbe->bp = NULL;
1459 
1460 	ki->kaio_queue_count++;
1461 	if (lj)
1462 		lj->lioj_queue_count++;
1463 	s = splnet();
1464 	TAILQ_INSERT_TAIL(&ki->kaio_jobqueue, aiocbe, plist);
1465 	TAILQ_INSERT_TAIL(&aio_jobs, aiocbe, list);
1466 	splx(s);
1467 	aiocbe->jobstate = JOBST_JOBQGLOBAL;
1468 
1469 	num_queue_count++;
1470 	error = 0;
1471 
1472 	/*
1473 	 * If we don't have a free AIO process, and we are below our quota, then
1474 	 * start one.  Otherwise, depend on the subsequent I/O completions to
1475 	 * pick-up this job.  If we don't sucessfully create the new process
1476 	 * (thread) due to resource issues, we return an error for now (EAGAIN),
1477 	 * which is likely not the correct thing to do.
1478 	 */
1479 	mtx_lock(&aio_freeproc_mtx);
1480 retryproc:
1481 	if ((aiop = TAILQ_FIRST(&aio_freeproc)) != NULL) {
1482 		TAILQ_REMOVE(&aio_freeproc, aiop, list);
1483 		aiop->aiothreadflags &= ~AIOP_FREE;
1484 		wakeup(aiop->aiothread);
1485 	} else if (((num_aio_resv_start + num_aio_procs) < max_aio_procs) &&
1486 	    ((ki->kaio_active_count + num_aio_resv_start) <
1487 	    ki->kaio_maxactive_count)) {
1488 		num_aio_resv_start++;
1489 		mtx_unlock(&aio_freeproc_mtx);
1490 		if ((error = aio_newproc()) == 0) {
1491 			mtx_lock(&aio_freeproc_mtx);
1492 			num_aio_resv_start--;
1493 			goto retryproc;
1494 		}
1495 		mtx_lock(&aio_freeproc_mtx);
1496 		num_aio_resv_start--;
1497 	}
1498 	mtx_unlock(&aio_freeproc_mtx);
1499 done:
1500 	return (error);
1501 }
1502 
1503 /*
1504  * This routine queues an AIO request, checking for quotas.
1505  */
1506 static int
1507 aio_aqueue(struct thread *td, struct aiocb *job, int type)
1508 {
1509 	struct proc *p = td->td_proc;
1510 	struct kaioinfo *ki;
1511 
1512 	if (p->p_aioinfo == NULL)
1513 		aio_init_aioinfo(p);
1514 
1515 	if (num_queue_count >= max_queue_count)
1516 		return (EAGAIN);
1517 
1518 	ki = p->p_aioinfo;
1519 	if (ki->kaio_queue_count >= ki->kaio_qallowed_count)
1520 		return (EAGAIN);
1521 
1522 	return _aio_aqueue(td, job, NULL, type);
1523 }
1524 
1525 /*
1526  * Support the aio_return system call, as a side-effect, kernel resources are
1527  * released.
1528  */
1529 int
1530 aio_return(struct thread *td, struct aio_return_args *uap)
1531 {
1532 	struct proc *p = td->td_proc;
1533 	int s;
1534 	long jobref;
1535 	struct aiocblist *cb, *ncb;
1536 	struct aiocb *ujob;
1537 	struct kaioinfo *ki;
1538 
1539 	ujob = uap->aiocbp;
1540 	jobref = fuword(&ujob->_aiocb_private.kernelinfo);
1541 	if (jobref == -1 || jobref == 0)
1542 		return (EINVAL);
1543 
1544 	ki = p->p_aioinfo;
1545 	if (ki == NULL)
1546 		return (EINVAL);
1547 	PROC_LOCK(p);
1548 	TAILQ_FOREACH(cb, &ki->kaio_jobdone, plist) {
1549 		if (((intptr_t) cb->uaiocb._aiocb_private.kernelinfo) ==
1550 		    jobref) {
1551 			if (cb->uaiocb.aio_lio_opcode == LIO_WRITE) {
1552 				p->p_stats->p_ru.ru_oublock +=
1553 				    cb->outputcharge;
1554 				cb->outputcharge = 0;
1555 			} else if (cb->uaiocb.aio_lio_opcode == LIO_READ) {
1556 				p->p_stats->p_ru.ru_inblock += cb->inputcharge;
1557 				cb->inputcharge = 0;
1558 			}
1559 			goto done;
1560 		}
1561 	}
1562 	s = splbio();
1563 	for (cb = TAILQ_FIRST(&ki->kaio_bufdone); cb; cb = ncb) {
1564 		ncb = TAILQ_NEXT(cb, plist);
1565 		if (((intptr_t) cb->uaiocb._aiocb_private.kernelinfo)
1566 		    == jobref) {
1567 			break;
1568 		}
1569 	}
1570 	splx(s);
1571  done:
1572 	PROC_UNLOCK(p);
1573 	if (cb != NULL) {
1574 		if (ujob == cb->uuaiocb) {
1575 			td->td_retval[0] =
1576 			    cb->uaiocb._aiocb_private.status;
1577 		} else
1578 			td->td_retval[0] = EFAULT;
1579 		aio_free_entry(cb);
1580 		return (0);
1581 	}
1582 	return (EINVAL);
1583 }
1584 
1585 /*
1586  * Allow a process to wakeup when any of the I/O requests are completed.
1587  */
1588 int
1589 aio_suspend(struct thread *td, struct aio_suspend_args *uap)
1590 {
1591 	struct proc *p = td->td_proc;
1592 	struct timeval atv;
1593 	struct timespec ts;
1594 	struct aiocb *const *cbptr, *cbp;
1595 	struct kaioinfo *ki;
1596 	struct aiocblist *cb;
1597 	int i;
1598 	int njoblist;
1599 	int error, s, timo;
1600 	long *ijoblist;
1601 	struct aiocb **ujoblist;
1602 
1603 	if (uap->nent < 0 || uap->nent > AIO_LISTIO_MAX)
1604 		return (EINVAL);
1605 
1606 	timo = 0;
1607 	if (uap->timeout) {
1608 		/* Get timespec struct. */
1609 		if ((error = copyin(uap->timeout, &ts, sizeof(ts))) != 0)
1610 			return (error);
1611 
1612 		if (ts.tv_nsec < 0 || ts.tv_nsec >= 1000000000)
1613 			return (EINVAL);
1614 
1615 		TIMESPEC_TO_TIMEVAL(&atv, &ts);
1616 		if (itimerfix(&atv))
1617 			return (EINVAL);
1618 		timo = tvtohz(&atv);
1619 	}
1620 
1621 	ki = p->p_aioinfo;
1622 	if (ki == NULL)
1623 		return (EAGAIN);
1624 
1625 	njoblist = 0;
1626 	ijoblist = uma_zalloc(aiol_zone, M_WAITOK);
1627 	ujoblist = uma_zalloc(aiol_zone, M_WAITOK);
1628 	cbptr = uap->aiocbp;
1629 
1630 	for (i = 0; i < uap->nent; i++) {
1631 		cbp = (struct aiocb *)(intptr_t)fuword(&cbptr[i]);
1632 		if (cbp == 0)
1633 			continue;
1634 		ujoblist[njoblist] = cbp;
1635 		ijoblist[njoblist] = fuword(&cbp->_aiocb_private.kernelinfo);
1636 		njoblist++;
1637 	}
1638 
1639 	if (njoblist == 0) {
1640 		uma_zfree(aiol_zone, ijoblist);
1641 		uma_zfree(aiol_zone, ujoblist);
1642 		return (0);
1643 	}
1644 
1645 	error = 0;
1646 	for (;;) {
1647 		PROC_LOCK(p);
1648 		TAILQ_FOREACH(cb, &ki->kaio_jobdone, plist) {
1649 			for (i = 0; i < njoblist; i++) {
1650 				if (((intptr_t)
1651 				    cb->uaiocb._aiocb_private.kernelinfo) ==
1652 				    ijoblist[i]) {
1653 					PROC_UNLOCK(p);
1654 					if (ujoblist[i] != cb->uuaiocb)
1655 						error = EINVAL;
1656 					uma_zfree(aiol_zone, ijoblist);
1657 					uma_zfree(aiol_zone, ujoblist);
1658 					return (error);
1659 				}
1660 			}
1661 		}
1662 
1663 		s = splbio();
1664 		for (cb = TAILQ_FIRST(&ki->kaio_bufdone); cb; cb =
1665 		    TAILQ_NEXT(cb, plist)) {
1666 			for (i = 0; i < njoblist; i++) {
1667 				if (((intptr_t)
1668 				    cb->uaiocb._aiocb_private.kernelinfo) ==
1669 				    ijoblist[i]) {
1670 					PROC_UNLOCK(p);
1671 					splx(s);
1672 					if (ujoblist[i] != cb->uuaiocb)
1673 						error = EINVAL;
1674 					uma_zfree(aiol_zone, ijoblist);
1675 					uma_zfree(aiol_zone, ujoblist);
1676 					return (error);
1677 				}
1678 			}
1679 		}
1680 
1681 		ki->kaio_flags |= KAIO_WAKEUP;
1682 		error = msleep(p, &p->p_mtx, PDROP | PRIBIO | PCATCH, "aiospn",
1683 		    timo);
1684 		splx(s);
1685 
1686 		if (error == ERESTART || error == EINTR) {
1687 			uma_zfree(aiol_zone, ijoblist);
1688 			uma_zfree(aiol_zone, ujoblist);
1689 			return (EINTR);
1690 		} else if (error == EWOULDBLOCK) {
1691 			uma_zfree(aiol_zone, ijoblist);
1692 			uma_zfree(aiol_zone, ujoblist);
1693 			return (EAGAIN);
1694 		}
1695 	}
1696 
1697 /* NOTREACHED */
1698 	return (EINVAL);
1699 }
1700 
1701 /*
1702  * aio_cancel cancels any non-physio aio operations not currently in
1703  * progress.
1704  */
1705 int
1706 aio_cancel(struct thread *td, struct aio_cancel_args *uap)
1707 {
1708 	struct proc *p = td->td_proc;
1709 	struct kaioinfo *ki;
1710 	struct aiocblist *cbe, *cbn;
1711 	struct file *fp;
1712 	struct filedesc *fdp;
1713 	struct socket *so;
1714 	struct proc *po;
1715 	int s,error;
1716 	int cancelled=0;
1717 	int notcancelled=0;
1718 	struct vnode *vp;
1719 
1720 	fdp = p->p_fd;
1721 	if ((u_int)uap->fd >= fdp->fd_nfiles ||
1722 	    (fp = fdp->fd_ofiles[uap->fd]) == NULL)
1723 		return (EBADF);
1724 
1725 	if (fp->f_type == DTYPE_VNODE) {
1726 		vp = fp->f_vnode;
1727 
1728 		if (vn_isdisk(vp,&error)) {
1729 			td->td_retval[0] = AIO_NOTCANCELED;
1730 			return (0);
1731 		}
1732 	} else if (fp->f_type == DTYPE_SOCKET) {
1733 		so = fp->f_data;
1734 
1735 		s = splnet();
1736 
1737 		for (cbe = TAILQ_FIRST(&so->so_aiojobq); cbe; cbe = cbn) {
1738 			cbn = TAILQ_NEXT(cbe, list);
1739 			if ((uap->aiocbp == NULL) ||
1740 				(uap->aiocbp == cbe->uuaiocb) ) {
1741 				po = cbe->userproc;
1742 				ki = po->p_aioinfo;
1743 				TAILQ_REMOVE(&so->so_aiojobq, cbe, list);
1744 				TAILQ_REMOVE(&ki->kaio_sockqueue, cbe, plist);
1745 				TAILQ_INSERT_TAIL(&ki->kaio_jobdone, cbe, plist);
1746 				if (ki->kaio_flags & KAIO_WAKEUP) {
1747 					wakeup(po);
1748 				}
1749 				cbe->jobstate = JOBST_JOBFINISHED;
1750 				cbe->uaiocb._aiocb_private.status=-1;
1751 				cbe->uaiocb._aiocb_private.error=ECANCELED;
1752 				cancelled++;
1753 /* XXX cancelled, knote? */
1754 				if (cbe->uaiocb.aio_sigevent.sigev_notify ==
1755 				    SIGEV_SIGNAL) {
1756 					PROC_LOCK(cbe->userproc);
1757 					psignal(cbe->userproc, cbe->uaiocb.aio_sigevent.sigev_signo);
1758 					PROC_UNLOCK(cbe->userproc);
1759 				}
1760 				if (uap->aiocbp)
1761 					break;
1762 			}
1763 		}
1764 		splx(s);
1765 
1766 		if ((cancelled) && (uap->aiocbp)) {
1767 			td->td_retval[0] = AIO_CANCELED;
1768 			return (0);
1769 		}
1770 	}
1771 	ki=p->p_aioinfo;
1772 	if (ki == NULL)
1773 		goto done;
1774 	s = splnet();
1775 
1776 	for (cbe = TAILQ_FIRST(&ki->kaio_jobqueue); cbe; cbe = cbn) {
1777 		cbn = TAILQ_NEXT(cbe, plist);
1778 
1779 		if ((uap->fd == cbe->uaiocb.aio_fildes) &&
1780 		    ((uap->aiocbp == NULL ) ||
1781 		     (uap->aiocbp == cbe->uuaiocb))) {
1782 
1783 			if (cbe->jobstate == JOBST_JOBQGLOBAL) {
1784 				TAILQ_REMOVE(&aio_jobs, cbe, list);
1785 				TAILQ_REMOVE(&ki->kaio_jobqueue, cbe, plist);
1786 				TAILQ_INSERT_TAIL(&ki->kaio_jobdone, cbe,
1787 				    plist);
1788 				cancelled++;
1789 				ki->kaio_queue_finished_count++;
1790 				cbe->jobstate = JOBST_JOBFINISHED;
1791 				cbe->uaiocb._aiocb_private.status = -1;
1792 				cbe->uaiocb._aiocb_private.error = ECANCELED;
1793 /* XXX cancelled, knote? */
1794 				if (cbe->uaiocb.aio_sigevent.sigev_notify ==
1795 				    SIGEV_SIGNAL) {
1796 					PROC_LOCK(cbe->userproc);
1797 					psignal(cbe->userproc, cbe->uaiocb.aio_sigevent.sigev_signo);
1798 					PROC_UNLOCK(cbe->userproc);
1799 				}
1800 			} else {
1801 				notcancelled++;
1802 			}
1803 		}
1804 	}
1805 	splx(s);
1806 done:
1807 	if (notcancelled) {
1808 		td->td_retval[0] = AIO_NOTCANCELED;
1809 		return (0);
1810 	}
1811 	if (cancelled) {
1812 		td->td_retval[0] = AIO_CANCELED;
1813 		return (0);
1814 	}
1815 	td->td_retval[0] = AIO_ALLDONE;
1816 
1817 	return (0);
1818 }
1819 
1820 /*
1821  * aio_error is implemented in the kernel level for compatibility purposes only.
1822  * For a user mode async implementation, it would be best to do it in a userland
1823  * subroutine.
1824  */
1825 int
1826 aio_error(struct thread *td, struct aio_error_args *uap)
1827 {
1828 	struct proc *p = td->td_proc;
1829 	int s;
1830 	struct aiocblist *cb;
1831 	struct kaioinfo *ki;
1832 	long jobref;
1833 
1834 	ki = p->p_aioinfo;
1835 	if (ki == NULL)
1836 		return (EINVAL);
1837 
1838 	jobref = fuword(&uap->aiocbp->_aiocb_private.kernelinfo);
1839 	if ((jobref == -1) || (jobref == 0))
1840 		return (EINVAL);
1841 
1842 	PROC_LOCK(p);
1843 	TAILQ_FOREACH(cb, &ki->kaio_jobdone, plist) {
1844 		if (((intptr_t)cb->uaiocb._aiocb_private.kernelinfo) ==
1845 		    jobref) {
1846 			PROC_UNLOCK(p);
1847 			td->td_retval[0] = cb->uaiocb._aiocb_private.error;
1848 			return (0);
1849 		}
1850 	}
1851 
1852 	s = splnet();
1853 
1854 	for (cb = TAILQ_FIRST(&ki->kaio_jobqueue); cb; cb = TAILQ_NEXT(cb,
1855 	    plist)) {
1856 		if (((intptr_t)cb->uaiocb._aiocb_private.kernelinfo) ==
1857 		    jobref) {
1858 			PROC_UNLOCK(p);
1859 			td->td_retval[0] = EINPROGRESS;
1860 			splx(s);
1861 			return (0);
1862 		}
1863 	}
1864 
1865 	for (cb = TAILQ_FIRST(&ki->kaio_sockqueue); cb; cb = TAILQ_NEXT(cb,
1866 	    plist)) {
1867 		if (((intptr_t)cb->uaiocb._aiocb_private.kernelinfo) ==
1868 		    jobref) {
1869 			PROC_UNLOCK(p);
1870 			td->td_retval[0] = EINPROGRESS;
1871 			splx(s);
1872 			return (0);
1873 		}
1874 	}
1875 	splx(s);
1876 
1877 	s = splbio();
1878 	for (cb = TAILQ_FIRST(&ki->kaio_bufdone); cb; cb = TAILQ_NEXT(cb,
1879 	    plist)) {
1880 		if (((intptr_t)cb->uaiocb._aiocb_private.kernelinfo) ==
1881 		    jobref) {
1882 			PROC_UNLOCK(p);
1883 			td->td_retval[0] = cb->uaiocb._aiocb_private.error;
1884 			splx(s);
1885 			return (0);
1886 		}
1887 	}
1888 
1889 	for (cb = TAILQ_FIRST(&ki->kaio_bufqueue); cb; cb = TAILQ_NEXT(cb,
1890 	    plist)) {
1891 		if (((intptr_t)cb->uaiocb._aiocb_private.kernelinfo) ==
1892 		    jobref) {
1893 			PROC_UNLOCK(p);
1894 			td->td_retval[0] = EINPROGRESS;
1895 			splx(s);
1896 			return (0);
1897 		}
1898 	}
1899 	splx(s);
1900 	PROC_UNLOCK(p);
1901 
1902 #if (0)
1903 	/*
1904 	 * Hack for lio.
1905 	 */
1906 	status = fuword(&uap->aiocbp->_aiocb_private.status);
1907 	if (status == -1)
1908 		return fuword(&uap->aiocbp->_aiocb_private.error);
1909 #endif
1910 	return (EINVAL);
1911 }
1912 
1913 /* syscall - asynchronous read from a file (REALTIME) */
1914 int
1915 aio_read(struct thread *td, struct aio_read_args *uap)
1916 {
1917 
1918 	return aio_aqueue(td, uap->aiocbp, LIO_READ);
1919 }
1920 
1921 /* syscall - asynchronous write to a file (REALTIME) */
1922 int
1923 aio_write(struct thread *td, struct aio_write_args *uap)
1924 {
1925 
1926 	return aio_aqueue(td, uap->aiocbp, LIO_WRITE);
1927 }
1928 
1929 /* syscall - list directed I/O (REALTIME) */
1930 int
1931 lio_listio(struct thread *td, struct lio_listio_args *uap)
1932 {
1933 	struct proc *p = td->td_proc;
1934 	int nent, nentqueued;
1935 	struct aiocb *iocb, * const *cbptr;
1936 	struct aiocblist *cb;
1937 	struct kaioinfo *ki;
1938 	struct aio_liojob *lj;
1939 	int error, runningcode;
1940 	int nerror;
1941 	int i;
1942 	int s;
1943 
1944 	if ((uap->mode != LIO_NOWAIT) && (uap->mode != LIO_WAIT))
1945 		return (EINVAL);
1946 
1947 	nent = uap->nent;
1948 	if (nent < 0 || nent > AIO_LISTIO_MAX)
1949 		return (EINVAL);
1950 
1951 	if (p->p_aioinfo == NULL)
1952 		aio_init_aioinfo(p);
1953 
1954 	if ((nent + num_queue_count) > max_queue_count)
1955 		return (EAGAIN);
1956 
1957 	ki = p->p_aioinfo;
1958 	if ((nent + ki->kaio_queue_count) > ki->kaio_qallowed_count)
1959 		return (EAGAIN);
1960 
1961 	lj = uma_zalloc(aiolio_zone, M_WAITOK);
1962 	if (!lj)
1963 		return (EAGAIN);
1964 
1965 	lj->lioj_flags = 0;
1966 	lj->lioj_buffer_count = 0;
1967 	lj->lioj_buffer_finished_count = 0;
1968 	lj->lioj_queue_count = 0;
1969 	lj->lioj_queue_finished_count = 0;
1970 	lj->lioj_ki = ki;
1971 
1972 	/*
1973 	 * Setup signal.
1974 	 */
1975 	if (uap->sig && (uap->mode == LIO_NOWAIT)) {
1976 		error = copyin(uap->sig, &lj->lioj_signal,
1977 		    sizeof(lj->lioj_signal));
1978 		if (error) {
1979 			uma_zfree(aiolio_zone, lj);
1980 			return (error);
1981 		}
1982 		if (!_SIG_VALID(lj->lioj_signal.sigev_signo)) {
1983 			uma_zfree(aiolio_zone, lj);
1984 			return (EINVAL);
1985 		}
1986 		lj->lioj_flags |= LIOJ_SIGNAL;
1987 	}
1988 	TAILQ_INSERT_TAIL(&ki->kaio_liojoblist, lj, lioj_list);
1989 	/*
1990 	 * Get pointers to the list of I/O requests.
1991 	 */
1992 	nerror = 0;
1993 	nentqueued = 0;
1994 	cbptr = uap->acb_list;
1995 	for (i = 0; i < uap->nent; i++) {
1996 		iocb = (struct aiocb *)(intptr_t)fuword(&cbptr[i]);
1997 		if (((intptr_t)iocb != -1) && ((intptr_t)iocb != 0)) {
1998 			error = _aio_aqueue(td, iocb, lj, 0);
1999 			if (error == 0)
2000 				nentqueued++;
2001 			else
2002 				nerror++;
2003 		}
2004 	}
2005 
2006 	/*
2007 	 * If we haven't queued any, then just return error.
2008 	 */
2009 	if (nentqueued == 0)
2010 		return (0);
2011 
2012 	/*
2013 	 * Calculate the appropriate error return.
2014 	 */
2015 	runningcode = 0;
2016 	if (nerror)
2017 		runningcode = EIO;
2018 
2019 	if (uap->mode == LIO_WAIT) {
2020 		int command, found, jobref;
2021 
2022 		for (;;) {
2023 			found = 0;
2024 			for (i = 0; i < uap->nent; i++) {
2025 				/*
2026 				 * Fetch address of the control buf pointer in
2027 				 * user space.
2028 				 */
2029 				iocb = (struct aiocb *)
2030 				    (intptr_t)fuword(&cbptr[i]);
2031 				if (((intptr_t)iocb == -1) || ((intptr_t)iocb
2032 				    == 0))
2033 					continue;
2034 
2035 				/*
2036 				 * Fetch the associated command from user space.
2037 				 */
2038 				command = fuword(&iocb->aio_lio_opcode);
2039 				if (command == LIO_NOP) {
2040 					found++;
2041 					continue;
2042 				}
2043 
2044 				jobref =
2045 				    fuword(&iocb->_aiocb_private.kernelinfo);
2046 
2047 				TAILQ_FOREACH(cb, &ki->kaio_jobdone, plist) {
2048 					if (((intptr_t)cb->uaiocb._aiocb_private.kernelinfo)
2049 					    == jobref) {
2050 						if (cb->uaiocb.aio_lio_opcode
2051 						    == LIO_WRITE) {
2052 							p->p_stats->p_ru.ru_oublock
2053 							    +=
2054 							    cb->outputcharge;
2055 							cb->outputcharge = 0;
2056 						} else if (cb->uaiocb.aio_lio_opcode
2057 						    == LIO_READ) {
2058 							p->p_stats->p_ru.ru_inblock
2059 							    += cb->inputcharge;
2060 							cb->inputcharge = 0;
2061 						}
2062 						found++;
2063 						break;
2064 					}
2065 				}
2066 
2067 				s = splbio();
2068 				TAILQ_FOREACH(cb, &ki->kaio_bufdone, plist) {
2069 					if (((intptr_t)cb->uaiocb._aiocb_private.kernelinfo)
2070 					    == jobref) {
2071 						found++;
2072 						break;
2073 					}
2074 				}
2075 				splx(s);
2076 			}
2077 
2078 			/*
2079 			 * If all I/Os have been disposed of, then we can
2080 			 * return.
2081 			 */
2082 			if (found == nentqueued)
2083 				return (runningcode);
2084 
2085 			ki->kaio_flags |= KAIO_WAKEUP;
2086 			error = tsleep(p, PRIBIO | PCATCH, "aiospn", 0);
2087 
2088 			if (error == EINTR)
2089 				return (EINTR);
2090 			else if (error == EWOULDBLOCK)
2091 				return (EAGAIN);
2092 		}
2093 	}
2094 
2095 	return (runningcode);
2096 }
2097 
2098 /*
2099  * Interrupt handler for physio, performs the necessary process wakeups, and
2100  * signals.
2101  */
2102 static void
2103 aio_physwakeup(struct buf *bp)
2104 {
2105 	struct aiocblist *aiocbe;
2106 	struct proc *p;
2107 	struct kaioinfo *ki;
2108 	struct aio_liojob *lj;
2109 
2110 	mtx_lock(&Giant);
2111 	wakeup(bp);
2112 
2113 	aiocbe = (struct aiocblist *)bp->b_caller1;
2114 	if (aiocbe) {
2115 		p = aiocbe->userproc;
2116 
2117 		aiocbe->jobstate = JOBST_JOBBFINISHED;
2118 		aiocbe->uaiocb._aiocb_private.status -= bp->b_resid;
2119 		aiocbe->uaiocb._aiocb_private.error = 0;
2120 		aiocbe->jobflags |= AIOCBLIST_DONE;
2121 
2122 		if (bp->b_ioflags & BIO_ERROR)
2123 			aiocbe->uaiocb._aiocb_private.error = bp->b_error;
2124 
2125 		lj = aiocbe->lio;
2126 		if (lj) {
2127 			lj->lioj_buffer_finished_count++;
2128 
2129 			/*
2130 			 * wakeup/signal if all of the interrupt jobs are done.
2131 			 */
2132 			if (lj->lioj_buffer_finished_count ==
2133 			    lj->lioj_buffer_count &&
2134 			    lj->lioj_queue_finished_count ==
2135 			    lj->lioj_queue_count) {
2136 				/*
2137 				 * Post a signal if it is called for.
2138 				 */
2139 				if ((lj->lioj_flags &
2140 				    (LIOJ_SIGNAL|LIOJ_SIGNAL_POSTED)) ==
2141 				    LIOJ_SIGNAL) {
2142 					PROC_LOCK(p);
2143 					psignal(p, lj->lioj_signal.sigev_signo);
2144 					PROC_UNLOCK(p);
2145 					lj->lioj_flags |= LIOJ_SIGNAL_POSTED;
2146 				}
2147 			}
2148 		}
2149 
2150 		ki = p->p_aioinfo;
2151 		if (ki) {
2152 			ki->kaio_buffer_finished_count++;
2153 			TAILQ_REMOVE(&ki->kaio_bufqueue, aiocbe, plist);
2154 			TAILQ_INSERT_TAIL(&ki->kaio_bufdone, aiocbe, plist);
2155 
2156 			KNOTE_UNLOCKED(&aiocbe->klist, 0);
2157 			/* Do the wakeup. */
2158 			if (ki->kaio_flags & (KAIO_RUNDOWN|KAIO_WAKEUP)) {
2159 				ki->kaio_flags &= ~KAIO_WAKEUP;
2160 				wakeup(p);
2161 			}
2162 		}
2163 
2164 		if (aiocbe->uaiocb.aio_sigevent.sigev_notify == SIGEV_SIGNAL) {
2165 			PROC_LOCK(p);
2166 			psignal(p, aiocbe->uaiocb.aio_sigevent.sigev_signo);
2167 			PROC_UNLOCK(p);
2168 		}
2169 	}
2170 	mtx_unlock(&Giant);
2171 }
2172 
2173 /* syscall - wait for the next completion of an aio request */
2174 int
2175 aio_waitcomplete(struct thread *td, struct aio_waitcomplete_args *uap)
2176 {
2177 	struct proc *p = td->td_proc;
2178 	struct timeval atv;
2179 	struct timespec ts;
2180 	struct kaioinfo *ki;
2181 	struct aiocblist *cb = NULL;
2182 	int error, s, timo;
2183 
2184 	suword(uap->aiocbp, (int)NULL);
2185 
2186 	timo = 0;
2187 	if (uap->timeout) {
2188 		/* Get timespec struct. */
2189 		error = copyin(uap->timeout, &ts, sizeof(ts));
2190 		if (error)
2191 			return (error);
2192 
2193 		if ((ts.tv_nsec < 0) || (ts.tv_nsec >= 1000000000))
2194 			return (EINVAL);
2195 
2196 		TIMESPEC_TO_TIMEVAL(&atv, &ts);
2197 		if (itimerfix(&atv))
2198 			return (EINVAL);
2199 		timo = tvtohz(&atv);
2200 	}
2201 
2202 	ki = p->p_aioinfo;
2203 	if (ki == NULL)
2204 		return (EAGAIN);
2205 
2206 	for (;;) {
2207 		PROC_LOCK(p);
2208 		if ((cb = TAILQ_FIRST(&ki->kaio_jobdone)) != 0) {
2209 			PROC_UNLOCK(p);
2210 			suword(uap->aiocbp, (uintptr_t)cb->uuaiocb);
2211 			td->td_retval[0] = cb->uaiocb._aiocb_private.status;
2212 			if (cb->uaiocb.aio_lio_opcode == LIO_WRITE) {
2213 				p->p_stats->p_ru.ru_oublock +=
2214 				    cb->outputcharge;
2215 				cb->outputcharge = 0;
2216 			} else if (cb->uaiocb.aio_lio_opcode == LIO_READ) {
2217 				p->p_stats->p_ru.ru_inblock += cb->inputcharge;
2218 				cb->inputcharge = 0;
2219 			}
2220 			error = cb->uaiocb._aiocb_private.error;
2221 			aio_free_entry(cb);
2222 			return (error);
2223 		}
2224 
2225 		s = splbio();
2226  		if ((cb = TAILQ_FIRST(&ki->kaio_bufdone)) != 0 ) {
2227 			PROC_UNLOCK(p);
2228 			splx(s);
2229 			suword(uap->aiocbp, (uintptr_t)cb->uuaiocb);
2230 			error = cb->uaiocb._aiocb_private.error;
2231 			td->td_retval[0] = cb->uaiocb._aiocb_private.status;
2232 			aio_free_entry(cb);
2233 			return (error);
2234 		}
2235 
2236 		ki->kaio_flags |= KAIO_WAKEUP;
2237 		error = msleep(p, &p->p_mtx, PDROP | PRIBIO | PCATCH, "aiowc",
2238 		    timo);
2239 		splx(s);
2240 
2241 		if (error == ERESTART)
2242 			return (EINTR);
2243 		else if (error < 0)
2244 			return (error);
2245 		else if (error == EINTR)
2246 			return (EINTR);
2247 		else if (error == EWOULDBLOCK)
2248 			return (EAGAIN);
2249 	}
2250 }
2251 
2252 /* kqueue attach function */
2253 static int
2254 filt_aioattach(struct knote *kn)
2255 {
2256 	struct aiocblist *aiocbe = (struct aiocblist *)kn->kn_sdata;
2257 
2258 	/*
2259 	 * The aiocbe pointer must be validated before using it, so
2260 	 * registration is restricted to the kernel; the user cannot
2261 	 * set EV_FLAG1.
2262 	 */
2263 	if ((kn->kn_flags & EV_FLAG1) == 0)
2264 		return (EPERM);
2265 	kn->kn_flags &= ~EV_FLAG1;
2266 
2267 	knlist_add(&aiocbe->klist, kn, 0);
2268 
2269 	return (0);
2270 }
2271 
2272 /* kqueue detach function */
2273 static void
2274 filt_aiodetach(struct knote *kn)
2275 {
2276 	struct aiocblist *aiocbe = (struct aiocblist *)kn->kn_sdata;
2277 
2278 	knlist_remove(&aiocbe->klist, kn, 0);
2279 }
2280 
2281 /* kqueue filter function */
2282 /*ARGSUSED*/
2283 static int
2284 filt_aio(struct knote *kn, long hint)
2285 {
2286 	struct aiocblist *aiocbe = (struct aiocblist *)kn->kn_sdata;
2287 
2288 	kn->kn_data = aiocbe->uaiocb._aiocb_private.error;
2289 	if (aiocbe->jobstate != JOBST_JOBFINISHED &&
2290 	    aiocbe->jobstate != JOBST_JOBBFINISHED)
2291 		return (0);
2292 	kn->kn_flags |= EV_EOF;
2293 	return (1);
2294 }
2295