Home | History | Annotate | Line # | Download | only in kern
sys_aio.c revision 1.17
      1 /*	$NetBSD: sys_aio.c,v 1.17 2008/04/24 15:35:29 ad Exp $	*/
      2 
      3 /*
      4  * Copyright (c) 2007, Mindaugas Rasiukevicius <rmind at NetBSD org>
      5  * All rights reserved.
      6  *
      7  * Redistribution and use in source and binary forms, with or without
      8  * modification, are permitted provided that the following conditions
      9  * are met:
     10  * 1. Redistributions of source code must retain the above copyright
     11  *    notice, this list of conditions and the following disclaimer.
     12  * 2. Redistributions in binary form must reproduce the above copyright
     13  *    notice, this list of conditions and the following disclaimer in the
     14  *    documentation and/or other materials provided with the distribution.
     15  *
     16  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
     17  * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
     18  * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
     19  * PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
     20  * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
     21  * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
     22  * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
     23  * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
     24  * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
     25  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
     26  * POSSIBILITY OF SUCH DAMAGE.
     27  */
     28 
     29 /*
     30  * TODO:
     31  *   1. Additional work for VCHR and maybe VBLK devices.
     32  *   2. Consider making the job-finding O(n) per one file descriptor.
     33  */
     34 
     35 #include <sys/cdefs.h>
     36 __KERNEL_RCSID(0, "$NetBSD: sys_aio.c,v 1.17 2008/04/24 15:35:29 ad Exp $");
     37 
     38 #include "opt_ddb.h"
     39 
     40 #include <sys/param.h>
     41 #include <sys/condvar.h>
     42 #include <sys/file.h>
     43 #include <sys/filedesc.h>
     44 #include <sys/kernel.h>
     45 #include <sys/kmem.h>
     46 #include <sys/lwp.h>
     47 #include <sys/mutex.h>
     48 #include <sys/pool.h>
     49 #include <sys/proc.h>
     50 #include <sys/queue.h>
     51 #include <sys/signal.h>
     52 #include <sys/signalvar.h>
     53 #include <sys/syscallargs.h>
     54 #include <sys/sysctl.h>
     55 #include <sys/systm.h>
     56 #include <sys/types.h>
     57 #include <sys/vnode.h>
     58 #include <sys/atomic.h>
     59 
     60 #include <uvm/uvm_extern.h>
     61 
     62 /*
     63  * System-wide limits and counter of AIO operations.
     64  */
     65 static u_int aio_listio_max = AIO_LISTIO_MAX;
     66 static u_int aio_max = AIO_MAX;
     67 static u_int aio_jobs_count;
     68 
     69 static struct pool aio_job_pool;
     70 static struct pool aio_lio_pool;
     71 
     72 /* Prototypes */
     73 void aio_worker(void *);
     74 static void aio_process(struct aio_job *);
     75 static void aio_sendsig(struct proc *, struct sigevent *);
     76 static int aio_enqueue_job(int, void *, struct lio_req *);
     77 
     78 /*
     79  * Initialize the AIO system.
     80  */
     81 void
     82 aio_sysinit(void)
     83 {
     84 
     85 	pool_init(&aio_job_pool, sizeof(struct aio_job), 0, 0, 0,
     86 	    "aio_jobs_pool", &pool_allocator_nointr, IPL_NONE);
     87 	pool_init(&aio_lio_pool, sizeof(struct lio_req), 0, 0, 0,
     88 	    "aio_lio_pool", &pool_allocator_nointr, IPL_NONE);
     89 }
     90 
     91 /*
     92  * Initialize Asynchronous I/O data structures for the process.
     93  */
     94 int
     95 aio_init(struct proc *p)
     96 {
     97 	struct aioproc *aio;
     98 	struct lwp *l;
     99 	int error;
    100 	bool inmem;
    101 	vaddr_t uaddr;
    102 
    103 	/* Allocate and initialize AIO structure */
    104 	aio = kmem_zalloc(sizeof(struct aioproc), KM_SLEEP);
    105 	if (aio == NULL)
    106 		return EAGAIN;
    107 
    108 	/* Initialize queue and their synchronization structures */
    109 	mutex_init(&aio->aio_mtx, MUTEX_DEFAULT, IPL_NONE);
    110 	cv_init(&aio->aio_worker_cv, "aiowork");
    111 	cv_init(&aio->done_cv, "aiodone");
    112 	TAILQ_INIT(&aio->jobs_queue);
    113 
    114 	/*
    115 	 * Create an AIO worker thread.
    116 	 * XXX: Currently, AIO thread is not protected against user's actions.
    117 	 */
    118 	inmem = uvm_uarea_alloc(&uaddr);
    119 	if (uaddr == 0) {
    120 		aio_exit(p, aio);
    121 		return EAGAIN;
    122 	}
    123 	error = lwp_create(curlwp, p, uaddr, inmem, 0, NULL, 0, aio_worker,
    124 	    NULL, &l, curlwp->l_class);
    125 	if (error != 0) {
    126 		uvm_uarea_free(uaddr, curcpu());
    127 		aio_exit(p, aio);
    128 		return error;
    129 	}
    130 
    131 	/* Recheck if we are really first */
    132 	mutex_enter(&p->p_mutex);
    133 	if (p->p_aio) {
    134 		mutex_exit(&p->p_mutex);
    135 		aio_exit(p, aio);
    136 		lwp_exit(l);
    137 		return 0;
    138 	}
    139 	p->p_aio = aio;
    140 	mutex_exit(&p->p_mutex);
    141 
    142 	/* Complete the initialization of thread, and run it */
    143 	mutex_enter(&p->p_smutex);
    144 	aio->aio_worker = l;
    145 	p->p_nrlwps++;
    146 	lwp_lock(l);
    147 	l->l_stat = LSRUN;
    148 	l->l_priority = MAXPRI_USER;
    149 	sched_enqueue(l, false);
    150 	lwp_unlock(l);
    151 	mutex_exit(&p->p_smutex);
    152 
    153 	return 0;
    154 }
    155 
    156 /*
    157  * Exit of Asynchronous I/O subsystem of process.
    158  */
    159 void
    160 aio_exit(struct proc *p, struct aioproc *aio)
    161 {
    162 	struct aio_job *a_job;
    163 
    164 	if (aio == NULL)
    165 		return;
    166 
    167 	/* Free AIO queue */
    168 	while (!TAILQ_EMPTY(&aio->jobs_queue)) {
    169 		a_job = TAILQ_FIRST(&aio->jobs_queue);
    170 		TAILQ_REMOVE(&aio->jobs_queue, a_job, list);
    171 		pool_put(&aio_job_pool, a_job);
    172 		atomic_dec_uint(&aio_jobs_count);
    173 	}
    174 
    175 	/* Destroy and free the entire AIO data structure */
    176 	cv_destroy(&aio->aio_worker_cv);
    177 	cv_destroy(&aio->done_cv);
    178 	mutex_destroy(&aio->aio_mtx);
    179 	kmem_free(aio, sizeof(struct aioproc));
    180 }
    181 
    182 /*
    183  * AIO worker thread and processor.
    184  */
    185 void
    186 aio_worker(void *arg)
    187 {
    188 	struct proc *p = curlwp->l_proc;
    189 	struct aioproc *aio = p->p_aio;
    190 	struct aio_job *a_job;
    191 	struct lio_req *lio;
    192 	sigset_t oss, nss;
    193 	int error, refcnt;
    194 
    195 	/*
    196 	 * Make an empty signal mask, so it
    197 	 * handles only SIGKILL and SIGSTOP.
    198 	 */
    199 	sigfillset(&nss);
    200 	mutex_enter(&p->p_smutex);
    201 	error = sigprocmask1(curlwp, SIG_SETMASK, &nss, &oss);
    202 	mutex_exit(&p->p_smutex);
    203 	KASSERT(error == 0);
    204 
    205 	for (;;) {
    206 		/*
    207 		 * Loop for each job in the queue.  If there
    208 		 * are no jobs then sleep.
    209 		 */
    210 		mutex_enter(&aio->aio_mtx);
    211 		while ((a_job = TAILQ_FIRST(&aio->jobs_queue)) == NULL) {
    212 			if (cv_wait_sig(&aio->aio_worker_cv, &aio->aio_mtx)) {
    213 				/*
    214 				 * Thread was interrupted - check for
    215 				 * pending exit or suspend.
    216 				 */
    217 				mutex_exit(&aio->aio_mtx);
    218 				lwp_userret(curlwp);
    219 				mutex_enter(&aio->aio_mtx);
    220 			}
    221 		}
    222 
    223 		/* Take the job from the queue */
    224 		aio->curjob = a_job;
    225 		TAILQ_REMOVE(&aio->jobs_queue, a_job, list);
    226 
    227 		atomic_dec_uint(&aio_jobs_count);
    228 		aio->jobs_count--;
    229 
    230 		mutex_exit(&aio->aio_mtx);
    231 
    232 		/* Process an AIO operation */
    233 		aio_process(a_job);
    234 
    235 		/* Copy data structure back to the user-space */
    236 		(void)copyout(&a_job->aiocbp, a_job->aiocb_uptr,
    237 		    sizeof(struct aiocb));
    238 
    239 		mutex_enter(&aio->aio_mtx);
    240 		aio->curjob = NULL;
    241 
    242 		/* Decrease a reference counter, if there is a LIO structure */
    243 		lio = a_job->lio;
    244 		refcnt = (lio != NULL ? --lio->refcnt : -1);
    245 
    246 		/* Notify all suspenders */
    247 		cv_broadcast(&aio->done_cv);
    248 		mutex_exit(&aio->aio_mtx);
    249 
    250 		/* Send a signal, if any */
    251 		aio_sendsig(p, &a_job->aiocbp.aio_sigevent);
    252 
    253 		/* Destroy the LIO structure */
    254 		if (refcnt == 0) {
    255 			aio_sendsig(p, &lio->sig);
    256 			pool_put(&aio_lio_pool, lio);
    257 		}
    258 
    259 		/* Destroy the the job */
    260 		pool_put(&aio_job_pool, a_job);
    261 	}
    262 
    263 	/* NOTREACHED */
    264 }
    265 
    266 static void
    267 aio_process(struct aio_job *a_job)
    268 {
    269 	struct proc *p = curlwp->l_proc;
    270 	struct aiocb *aiocbp = &a_job->aiocbp;
    271 	struct file *fp;
    272 	int fd = aiocbp->aio_fildes;
    273 	int error = 0;
    274 
    275 	KASSERT(a_job->aio_op != 0);
    276 
    277 	if ((a_job->aio_op & (AIO_READ | AIO_WRITE)) != 0) {
    278 		struct iovec aiov;
    279 		struct uio auio;
    280 
    281 		if (aiocbp->aio_nbytes > SSIZE_MAX) {
    282 			error = EINVAL;
    283 			goto done;
    284 		}
    285 
    286 		fp = fd_getfile(fd);
    287 		if (fp == NULL) {
    288 			error = EBADF;
    289 			goto done;
    290 		}
    291 
    292 		aiov.iov_base = (void *)(uintptr_t)aiocbp->aio_buf;
    293 		aiov.iov_len = aiocbp->aio_nbytes;
    294 		auio.uio_iov = &aiov;
    295 		auio.uio_iovcnt = 1;
    296 		auio.uio_resid = aiocbp->aio_nbytes;
    297 		auio.uio_vmspace = p->p_vmspace;
    298 
    299 		if (a_job->aio_op & AIO_READ) {
    300 			/*
    301 			 * Perform a Read operation
    302 			 */
    303 			KASSERT((a_job->aio_op & AIO_WRITE) == 0);
    304 
    305 			if ((fp->f_flag & FREAD) == 0) {
    306 				fd_putfile(fd);
    307 				error = EBADF;
    308 				goto done;
    309 			}
    310 			auio.uio_rw = UIO_READ;
    311 			error = (*fp->f_ops->fo_read)(fp, &aiocbp->aio_offset,
    312 			    &auio, fp->f_cred, FOF_UPDATE_OFFSET);
    313 		} else {
    314 			/*
    315 			 * Perform a Write operation
    316 			 */
    317 			KASSERT(a_job->aio_op & AIO_WRITE);
    318 
    319 			if ((fp->f_flag & FWRITE) == 0) {
    320 				fd_putfile(fd);
    321 				error = EBADF;
    322 				goto done;
    323 			}
    324 			auio.uio_rw = UIO_WRITE;
    325 			error = (*fp->f_ops->fo_write)(fp, &aiocbp->aio_offset,
    326 			    &auio, fp->f_cred, FOF_UPDATE_OFFSET);
    327 		}
    328 		fd_putfile(fd);
    329 
    330 		/* Store the result value */
    331 		a_job->aiocbp.aio_nbytes -= auio.uio_resid;
    332 		a_job->aiocbp._retval = (error == 0) ?
    333 		    a_job->aiocbp.aio_nbytes : -1;
    334 
    335 	} else if ((a_job->aio_op & (AIO_SYNC | AIO_DSYNC)) != 0) {
    336 		/*
    337 		 * Perform a file Sync operation
    338 		 */
    339 		struct vnode *vp;
    340 
    341 		if ((error = fd_getvnode(fd, &fp)) != 0)
    342 			goto done;
    343 
    344 		if ((fp->f_flag & FWRITE) == 0) {
    345 			fd_putfile(fd);
    346 			error = EBADF;
    347 			goto done;
    348 		}
    349 
    350 		vp = (struct vnode *)fp->f_data;
    351 		vn_lock(vp, LK_EXCLUSIVE | LK_RETRY);
    352 		if (a_job->aio_op & AIO_DSYNC) {
    353 			error = VOP_FSYNC(vp, fp->f_cred,
    354 			    FSYNC_WAIT | FSYNC_DATAONLY, 0, 0);
    355 		} else if (a_job->aio_op & AIO_SYNC) {
    356 			error = VOP_FSYNC(vp, fp->f_cred,
    357 			    FSYNC_WAIT, 0, 0);
    358 			if (error == 0 && bioopsp != NULL &&
    359 			    vp->v_mount &&
    360 			    (vp->v_mount->mnt_flag & MNT_SOFTDEP))
    361 			    bioopsp->io_fsync(vp, 0);
    362 		}
    363 		VOP_UNLOCK(vp, 0);
    364 		fd_putfile(fd);
    365 
    366 		/* Store the result value */
    367 		a_job->aiocbp._retval = (error == 0) ? 0 : -1;
    368 
    369 	} else
    370 		panic("aio_process: invalid operation code\n");
    371 
    372 done:
    373 	/* Job is done, set the error, if any */
    374 	a_job->aiocbp._errno = error;
    375 	a_job->aiocbp._state = JOB_DONE;
    376 }
    377 
    378 /*
    379  * Send AIO signal.
    380  */
    381 static void
    382 aio_sendsig(struct proc *p, struct sigevent *sig)
    383 {
    384 	ksiginfo_t ksi;
    385 
    386 	if (sig->sigev_signo == 0 || sig->sigev_notify == SIGEV_NONE)
    387 		return;
    388 
    389 	KSI_INIT(&ksi);
    390 	ksi.ksi_signo = sig->sigev_signo;
    391 	ksi.ksi_code = SI_ASYNCIO;
    392 	ksi.ksi_value = sig->sigev_value;
    393 	mutex_enter(proc_lock);
    394 	kpsignal(p, &ksi, NULL);
    395 	mutex_exit(proc_lock);
    396 }
    397 
    398 /*
    399  * Enqueue the job.
    400  */
    401 static int
    402 aio_enqueue_job(int op, void *aiocb_uptr, struct lio_req *lio)
    403 {
    404 	struct proc *p = curlwp->l_proc;
    405 	struct aioproc *aio;
    406 	struct aio_job *a_job;
    407 	struct aiocb aiocbp;
    408 	struct sigevent *sig;
    409 	int error;
    410 
    411 	/* Non-accurate check for the limit */
    412 	if (aio_jobs_count + 1 > aio_max)
    413 		return EAGAIN;
    414 
    415 	/* Get the data structure from user-space */
    416 	error = copyin(aiocb_uptr, &aiocbp, sizeof(struct aiocb));
    417 	if (error)
    418 		return error;
    419 
    420 	/* Check if signal is set, and validate it */
    421 	sig = &aiocbp.aio_sigevent;
    422 	if (sig->sigev_signo < 0 || sig->sigev_signo >= NSIG ||
    423 	    sig->sigev_notify < SIGEV_NONE || sig->sigev_notify > SIGEV_SA)
    424 		return EINVAL;
    425 
    426 	/* Buffer and byte count */
    427 	if (((AIO_SYNC | AIO_DSYNC) & op) == 0)
    428 		if (aiocbp.aio_buf == NULL || aiocbp.aio_nbytes > SSIZE_MAX)
    429 			return EINVAL;
    430 
    431 	/* Check the opcode, if LIO_NOP - simply ignore */
    432 	if (op == AIO_LIO) {
    433 		KASSERT(lio != NULL);
    434 		if (aiocbp.aio_lio_opcode == LIO_WRITE)
    435 			op = AIO_WRITE;
    436 		else if (aiocbp.aio_lio_opcode == LIO_READ)
    437 			op = AIO_READ;
    438 		else
    439 			return (aiocbp.aio_lio_opcode == LIO_NOP) ? 0 : EINVAL;
    440 	} else {
    441 		KASSERT(lio == NULL);
    442 	}
    443 
    444 	/*
    445 	 * Look for already existing job.  If found - the job is in-progress.
    446 	 * According to POSIX this is invalid, so return the error.
    447 	 */
    448 	aio = p->p_aio;
    449 	if (aio) {
    450 		mutex_enter(&aio->aio_mtx);
    451 		if (aio->curjob) {
    452 			a_job = aio->curjob;
    453 			if (a_job->aiocb_uptr == aiocb_uptr) {
    454 				mutex_exit(&aio->aio_mtx);
    455 				return EINVAL;
    456 			}
    457 		}
    458 		TAILQ_FOREACH(a_job, &aio->jobs_queue, list) {
    459 			if (a_job->aiocb_uptr != aiocb_uptr)
    460 				continue;
    461 			mutex_exit(&aio->aio_mtx);
    462 			return EINVAL;
    463 		}
    464 		mutex_exit(&aio->aio_mtx);
    465 	}
    466 
    467 	/*
    468 	 * Check if AIO structure is initialized, if not - initialize it.
    469 	 * In LIO case, we did that already.  We will recheck this with
    470 	 * the lock in aio_init().
    471 	 */
    472 	if (lio == NULL && p->p_aio == NULL)
    473 		if (aio_init(p))
    474 			return EAGAIN;
    475 	aio = p->p_aio;
    476 
    477 	/*
    478 	 * Set the state with errno, and copy data
    479 	 * structure back to the user-space.
    480 	 */
    481 	aiocbp._state = JOB_WIP;
    482 	aiocbp._errno = EINPROGRESS;
    483 	aiocbp._retval = -1;
    484 	error = copyout(&aiocbp, aiocb_uptr, sizeof(struct aiocb));
    485 	if (error)
    486 		return error;
    487 
    488 	/* Allocate and initialize a new AIO job */
    489 	a_job = pool_get(&aio_job_pool, PR_WAITOK);
    490 	memset(a_job, 0, sizeof(struct aio_job));
    491 
    492 	/*
    493 	 * Set the data.
    494 	 * Store the user-space pointer for searching.  Since we
    495 	 * are storing only per proc pointers - it is safe.
    496 	 */
    497 	memcpy(&a_job->aiocbp, &aiocbp, sizeof(struct aiocb));
    498 	a_job->aiocb_uptr = aiocb_uptr;
    499 	a_job->aio_op |= op;
    500 	a_job->lio = lio;
    501 
    502 	/*
    503 	 * Add the job to the queue, update the counters, and
    504 	 * notify the AIO worker thread to handle the job.
    505 	 */
    506 	mutex_enter(&aio->aio_mtx);
    507 
    508 	/* Fail, if the limit was reached */
    509 	if (atomic_inc_uint_nv(&aio_jobs_count) > aio_max ||
    510 	    aio->jobs_count >= aio_listio_max) {
    511 		atomic_dec_uint(&aio_jobs_count);
    512 		mutex_exit(&aio->aio_mtx);
    513 		pool_put(&aio_job_pool, a_job);
    514 		return EAGAIN;
    515 	}
    516 
    517 	TAILQ_INSERT_TAIL(&aio->jobs_queue, a_job, list);
    518 	aio->jobs_count++;
    519 	if (lio)
    520 		lio->refcnt++;
    521 	cv_signal(&aio->aio_worker_cv);
    522 
    523 	mutex_exit(&aio->aio_mtx);
    524 
    525 	/*
    526 	 * One would handle the errors only with aio_error() function.
    527 	 * This way is appropriate according to POSIX.
    528 	 */
    529 	return 0;
    530 }
    531 
    532 /*
    533  * Syscall functions.
    534  */
    535 
    536 int
    537 sys_aio_cancel(struct lwp *l, const struct sys_aio_cancel_args *uap, register_t *retval)
    538 {
    539 	/* {
    540 		syscallarg(int) fildes;
    541 		syscallarg(struct aiocb *) aiocbp;
    542 	} */
    543 	struct proc *p = l->l_proc;
    544 	struct aioproc *aio;
    545 	struct aio_job *a_job;
    546 	struct aiocb *aiocbp_ptr;
    547 	struct lio_req *lio;
    548 	struct filedesc	*fdp = p->p_fd;
    549 	unsigned int cn, errcnt, fildes;
    550 
    551 	TAILQ_HEAD(, aio_job) tmp_jobs_list;
    552 
    553 	/* Check for invalid file descriptor */
    554 	fildes = (unsigned int)SCARG(uap, fildes);
    555 	if (fildes >= fdp->fd_nfiles)
    556 		return EBADF;
    557 	membar_consumer();
    558 	if (fdp->fd_ofiles[fildes] == NULL || fdp->fd_ofiles[fildes]->ff_file == NULL)
    559 		return EBADF;
    560 
    561 	/* Check if AIO structure is initialized */
    562 	if (p->p_aio == NULL) {
    563 		*retval = AIO_NOTCANCELED;
    564 		return 0;
    565 	}
    566 
    567 	aio = p->p_aio;
    568 	aiocbp_ptr = (struct aiocb *)SCARG(uap, aiocbp);
    569 
    570 	mutex_enter(&aio->aio_mtx);
    571 
    572 	/* Cancel the jobs, and remove them from the queue */
    573 	cn = 0;
    574 	TAILQ_INIT(&tmp_jobs_list);
    575 	TAILQ_FOREACH(a_job, &aio->jobs_queue, list) {
    576 		if (aiocbp_ptr) {
    577 			if (aiocbp_ptr != a_job->aiocb_uptr)
    578 				continue;
    579 			if (fildes != a_job->aiocbp.aio_fildes) {
    580 				mutex_exit(&aio->aio_mtx);
    581 				return EBADF;
    582 			}
    583 		} else if (a_job->aiocbp.aio_fildes != fildes)
    584 			continue;
    585 
    586 		TAILQ_REMOVE(&aio->jobs_queue, a_job, list);
    587 		TAILQ_INSERT_TAIL(&tmp_jobs_list, a_job, list);
    588 
    589 		/* Decrease the counters */
    590 		atomic_dec_uint(&aio_jobs_count);
    591 		aio->jobs_count--;
    592 		lio = a_job->lio;
    593 		if (lio != NULL && --lio->refcnt != 0)
    594 			a_job->lio = NULL;
    595 
    596 		cn++;
    597 		if (aiocbp_ptr)
    598 			break;
    599 	}
    600 
    601 	/* There are canceled jobs */
    602 	if (cn)
    603 		*retval = AIO_CANCELED;
    604 
    605 	/* We cannot cancel current job */
    606 	a_job = aio->curjob;
    607 	if (a_job && ((a_job->aiocbp.aio_fildes == fildes) ||
    608 	    (a_job->aiocb_uptr == aiocbp_ptr)))
    609 		*retval = AIO_NOTCANCELED;
    610 
    611 	mutex_exit(&aio->aio_mtx);
    612 
    613 	/* Free the jobs after the lock */
    614 	errcnt = 0;
    615 	while (!TAILQ_EMPTY(&tmp_jobs_list)) {
    616 		a_job = TAILQ_FIRST(&tmp_jobs_list);
    617 		TAILQ_REMOVE(&tmp_jobs_list, a_job, list);
    618 		/* Set the errno and copy structures back to the user-space */
    619 		a_job->aiocbp._errno = ECANCELED;
    620 		a_job->aiocbp._state = JOB_DONE;
    621 		if (copyout(&a_job->aiocbp, a_job->aiocb_uptr,
    622 		    sizeof(struct aiocb)))
    623 			errcnt++;
    624 		/* Send a signal if any */
    625 		aio_sendsig(p, &a_job->aiocbp.aio_sigevent);
    626 		if (a_job->lio) {
    627 			lio = a_job->lio;
    628 			aio_sendsig(p, &lio->sig);
    629 			pool_put(&aio_lio_pool, lio);
    630 		}
    631 		pool_put(&aio_job_pool, a_job);
    632 	}
    633 
    634 	if (errcnt)
    635 		return EFAULT;
    636 
    637 	/* Set a correct return value */
    638 	if (*retval == 0)
    639 		*retval = AIO_ALLDONE;
    640 
    641 	return 0;
    642 }
    643 
    644 int
    645 sys_aio_error(struct lwp *l, const struct sys_aio_error_args *uap, register_t *retval)
    646 {
    647 	/* {
    648 		syscallarg(const struct aiocb *) aiocbp;
    649 	} */
    650 	struct proc *p = l->l_proc;
    651 	struct aioproc *aio = p->p_aio;
    652 	struct aiocb aiocbp;
    653 	int error;
    654 
    655 	if (aio == NULL)
    656 		return EINVAL;
    657 
    658 	error = copyin(SCARG(uap, aiocbp), &aiocbp, sizeof(struct aiocb));
    659 	if (error)
    660 		return error;
    661 
    662 	if (aiocbp._state == JOB_NONE)
    663 		return EINVAL;
    664 
    665 	*retval = aiocbp._errno;
    666 
    667 	return 0;
    668 }
    669 
    670 int
    671 sys_aio_fsync(struct lwp *l, const struct sys_aio_fsync_args *uap, register_t *retval)
    672 {
    673 	/* {
    674 		syscallarg(int) op;
    675 		syscallarg(struct aiocb *) aiocbp;
    676 	} */
    677 	int op = SCARG(uap, op);
    678 
    679 	if ((op != O_DSYNC) && (op != O_SYNC))
    680 		return EINVAL;
    681 
    682 	op = O_DSYNC ? AIO_DSYNC : AIO_SYNC;
    683 
    684 	return aio_enqueue_job(op, SCARG(uap, aiocbp), NULL);
    685 }
    686 
    687 int
    688 sys_aio_read(struct lwp *l, const struct sys_aio_read_args *uap, register_t *retval)
    689 {
    690 	/* {
    691 		syscallarg(struct aiocb *) aiocbp;
    692 	} */
    693 
    694 	return aio_enqueue_job(AIO_READ, SCARG(uap, aiocbp), NULL);
    695 }
    696 
    697 int
    698 sys_aio_return(struct lwp *l, const struct sys_aio_return_args *uap, register_t *retval)
    699 {
    700 	/* {
    701 		syscallarg(struct aiocb *) aiocbp;
    702 	} */
    703 	struct proc *p = l->l_proc;
    704 	struct aioproc *aio = p->p_aio;
    705 	struct aiocb aiocbp;
    706 	int error;
    707 
    708 	if (aio == NULL)
    709 		return EINVAL;
    710 
    711 	error = copyin(SCARG(uap, aiocbp), &aiocbp, sizeof(struct aiocb));
    712 	if (error)
    713 		return error;
    714 
    715 	if (aiocbp._errno == EINPROGRESS || aiocbp._state != JOB_DONE)
    716 		return EINVAL;
    717 
    718 	*retval = aiocbp._retval;
    719 
    720 	/* Reset the internal variables */
    721 	aiocbp._errno = 0;
    722 	aiocbp._retval = -1;
    723 	aiocbp._state = JOB_NONE;
    724 	error = copyout(&aiocbp, SCARG(uap, aiocbp), sizeof(struct aiocb));
    725 
    726 	return error;
    727 }
    728 
    729 int
    730 sys_aio_suspend(struct lwp *l, const struct sys_aio_suspend_args *uap, register_t *retval)
    731 {
    732 	/* {
    733 		syscallarg(const struct aiocb *const[]) list;
    734 		syscallarg(int) nent;
    735 		syscallarg(const struct timespec *) timeout;
    736 	} */
    737 	struct proc *p = l->l_proc;
    738 	struct aioproc *aio;
    739 	struct aio_job *a_job;
    740 	struct aiocb **aiocbp_list;
    741 	struct timespec ts;
    742 	int i, error, nent, timo;
    743 
    744 	if (p->p_aio == NULL)
    745 		return EAGAIN;
    746 	aio = p->p_aio;
    747 
    748 	nent = SCARG(uap, nent);
    749 	if (nent <= 0 || nent > aio_listio_max)
    750 		return EAGAIN;
    751 
    752 	if (SCARG(uap, timeout)) {
    753 		/* Convert timespec to ticks */
    754 		error = copyin(SCARG(uap, timeout), &ts,
    755 		    sizeof(struct timespec));
    756 		if (error)
    757 			return error;
    758 		timo = mstohz((ts.tv_sec * 1000) + (ts.tv_nsec / 1000000));
    759 		if (timo == 0 && ts.tv_sec == 0 && ts.tv_nsec > 0)
    760 			timo = 1;
    761 		if (timo <= 0)
    762 			return EAGAIN;
    763 	} else
    764 		timo = 0;
    765 
    766 	/* Get the list from user-space */
    767 	aiocbp_list = kmem_zalloc(nent * sizeof(struct aio_job), KM_SLEEP);
    768 	error = copyin(SCARG(uap, list), aiocbp_list,
    769 	    nent * sizeof(struct aiocb));
    770 	if (error) {
    771 		kmem_free(aiocbp_list, nent * sizeof(struct aio_job));
    772 		return error;
    773 	}
    774 
    775 	mutex_enter(&aio->aio_mtx);
    776 	for (;;) {
    777 
    778 		for (i = 0; i < nent; i++) {
    779 
    780 			/* Skip NULL entries */
    781 			if (aiocbp_list[i] == NULL)
    782 				continue;
    783 
    784 			/* Skip current job */
    785 			if (aio->curjob) {
    786 				a_job = aio->curjob;
    787 				if (a_job->aiocb_uptr == aiocbp_list[i])
    788 					continue;
    789 			}
    790 
    791 			/* Look for a job in the queue */
    792 			TAILQ_FOREACH(a_job, &aio->jobs_queue, list)
    793 				if (a_job->aiocb_uptr == aiocbp_list[i])
    794 					break;
    795 
    796 			if (a_job == NULL) {
    797 				struct aiocb aiocbp;
    798 
    799 				mutex_exit(&aio->aio_mtx);
    800 
    801 				error = copyin(aiocbp_list[i], &aiocbp,
    802 				    sizeof(struct aiocb));
    803 				if (error == 0 && aiocbp._state != JOB_DONE) {
    804 					mutex_enter(&aio->aio_mtx);
    805 					continue;
    806 				}
    807 
    808 				kmem_free(aiocbp_list,
    809 				    nent * sizeof(struct aio_job));
    810 				return error;
    811 			}
    812 		}
    813 
    814 		/* Wait for a signal or when timeout occurs */
    815 		error = cv_timedwait_sig(&aio->done_cv, &aio->aio_mtx, timo);
    816 		if (error) {
    817 			if (error == EWOULDBLOCK)
    818 				error = EAGAIN;
    819 			break;
    820 		}
    821 	}
    822 	mutex_exit(&aio->aio_mtx);
    823 
    824 	kmem_free(aiocbp_list, nent * sizeof(struct aio_job));
    825 	return error;
    826 }
    827 
    828 int
    829 sys_aio_write(struct lwp *l, const struct sys_aio_write_args *uap, register_t *retval)
    830 {
    831 	/* {
    832 		syscallarg(struct aiocb *) aiocbp;
    833 	} */
    834 
    835 	return aio_enqueue_job(AIO_WRITE, SCARG(uap, aiocbp), NULL);
    836 }
    837 
    838 int
    839 sys_lio_listio(struct lwp *l, const struct sys_lio_listio_args *uap, register_t *retval)
    840 {
    841 	/* {
    842 		syscallarg(int) mode;
    843 		syscallarg(struct aiocb *const[]) list;
    844 		syscallarg(int) nent;
    845 		syscallarg(struct sigevent *) sig;
    846 	} */
    847 	struct proc *p = l->l_proc;
    848 	struct aioproc *aio;
    849 	struct aiocb **aiocbp_list;
    850 	struct lio_req *lio;
    851 	int i, error, errcnt, mode, nent;
    852 
    853 	mode = SCARG(uap, mode);
    854 	nent = SCARG(uap, nent);
    855 
    856 	/* Non-accurate checks for the limit and invalid values */
    857 	if (nent < 1 || nent > aio_listio_max)
    858 		return EINVAL;
    859 	if (aio_jobs_count + nent > aio_max)
    860 		return EAGAIN;
    861 
    862 	/* Check if AIO structure is initialized, if not - initialize it */
    863 	if (p->p_aio == NULL)
    864 		if (aio_init(p))
    865 			return EAGAIN;
    866 	aio = p->p_aio;
    867 
    868 	/* Create a LIO structure */
    869 	lio = pool_get(&aio_lio_pool, PR_WAITOK);
    870 	lio->refcnt = 1;
    871 	error = 0;
    872 
    873 	switch (mode) {
    874 	case LIO_WAIT:
    875 		memset(&lio->sig, 0, sizeof(struct sigevent));
    876 		break;
    877 	case LIO_NOWAIT:
    878 		/* Check for signal, validate it */
    879 		if (SCARG(uap, sig)) {
    880 			struct sigevent *sig = &lio->sig;
    881 
    882 			error = copyin(SCARG(uap, sig), &lio->sig,
    883 			    sizeof(struct sigevent));
    884 			if (error == 0 &&
    885 			    (sig->sigev_signo < 0 ||
    886 			    sig->sigev_signo >= NSIG ||
    887 			    sig->sigev_notify < SIGEV_NONE ||
    888 			    sig->sigev_notify > SIGEV_SA))
    889 				error = EINVAL;
    890 		} else
    891 			memset(&lio->sig, 0, sizeof(struct sigevent));
    892 		break;
    893 	default:
    894 		error = EINVAL;
    895 		break;
    896 	}
    897 
    898 	if (error != 0) {
    899 		pool_put(&aio_lio_pool, lio);
    900 		return error;
    901 	}
    902 
    903 	/* Get the list from user-space */
    904 	aiocbp_list = kmem_zalloc(nent * sizeof(struct aio_job), KM_SLEEP);
    905 	error = copyin(SCARG(uap, list), aiocbp_list,
    906 	    nent * sizeof(struct aiocb));
    907 	if (error) {
    908 		mutex_enter(&aio->aio_mtx);
    909 		goto err;
    910 	}
    911 
    912 	/* Enqueue all jobs */
    913 	errcnt = 0;
    914 	for (i = 0; i < nent; i++) {
    915 		error = aio_enqueue_job(AIO_LIO, aiocbp_list[i], lio);
    916 		/*
    917 		 * According to POSIX, in such error case it may
    918 		 * fail with other I/O operations initiated.
    919 		 */
    920 		if (error)
    921 			errcnt++;
    922 	}
    923 
    924 	mutex_enter(&aio->aio_mtx);
    925 
    926 	/* Return an error, if any */
    927 	if (errcnt) {
    928 		error = EIO;
    929 		goto err;
    930 	}
    931 
    932 	if (mode == LIO_WAIT) {
    933 		/*
    934 		 * Wait for AIO completion.  In such case,
    935 		 * the LIO structure will be freed here.
    936 		 */
    937 		while (lio->refcnt > 1 && error == 0)
    938 			error = cv_wait_sig(&aio->done_cv, &aio->aio_mtx);
    939 		if (error)
    940 			error = EINTR;
    941 	}
    942 
    943 err:
    944 	if (--lio->refcnt != 0)
    945 		lio = NULL;
    946 	mutex_exit(&aio->aio_mtx);
    947 	if (lio != NULL) {
    948 		aio_sendsig(p, &lio->sig);
    949 		pool_put(&aio_lio_pool, lio);
    950 	}
    951 	kmem_free(aiocbp_list, nent * sizeof(struct aio_job));
    952 	return error;
    953 }
    954 
    955 /*
    956  * SysCtl
    957  */
    958 
    959 static int
    960 sysctl_aio_listio_max(SYSCTLFN_ARGS)
    961 {
    962 	struct sysctlnode node;
    963 	int error, newsize;
    964 
    965 	node = *rnode;
    966 	node.sysctl_data = &newsize;
    967 
    968 	newsize = aio_listio_max;
    969 	error = sysctl_lookup(SYSCTLFN_CALL(&node));
    970 	if (error || newp == NULL)
    971 		return error;
    972 
    973 	if (newsize < 1 || newsize > aio_max)
    974 		return EINVAL;
    975 	aio_listio_max = newsize;
    976 
    977 	return 0;
    978 }
    979 
    980 static int
    981 sysctl_aio_max(SYSCTLFN_ARGS)
    982 {
    983 	struct sysctlnode node;
    984 	int error, newsize;
    985 
    986 	node = *rnode;
    987 	node.sysctl_data = &newsize;
    988 
    989 	newsize = aio_max;
    990 	error = sysctl_lookup(SYSCTLFN_CALL(&node));
    991 	if (error || newp == NULL)
    992 		return error;
    993 
    994 	if (newsize < 1 || newsize < aio_listio_max)
    995 		return EINVAL;
    996 	aio_max = newsize;
    997 
    998 	return 0;
    999 }
   1000 
   1001 SYSCTL_SETUP(sysctl_aio_setup, "sysctl aio setup")
   1002 {
   1003 
   1004 	sysctl_createv(clog, 0, NULL, NULL,
   1005 		CTLFLAG_PERMANENT,
   1006 		CTLTYPE_NODE, "kern", NULL,
   1007 		NULL, 0, NULL, 0,
   1008 		CTL_KERN, CTL_EOL);
   1009 	sysctl_createv(clog, 0, NULL, NULL,
   1010 		CTLFLAG_PERMANENT | CTLFLAG_IMMEDIATE,
   1011 		CTLTYPE_INT, "posix_aio",
   1012 		SYSCTL_DESCR("Version of IEEE Std 1003.1 and its "
   1013 			     "Asynchronous I/O option to which the "
   1014 			     "system attempts to conform"),
   1015 		NULL, _POSIX_ASYNCHRONOUS_IO, NULL, 0,
   1016 		CTL_KERN, CTL_CREATE, CTL_EOL);
   1017 	sysctl_createv(clog, 0, NULL, NULL,
   1018 		CTLFLAG_PERMANENT | CTLFLAG_READWRITE,
   1019 		CTLTYPE_INT, "aio_listio_max",
   1020 		SYSCTL_DESCR("Maximum number of asynchronous I/O "
   1021 			     "operations in a single list I/O call"),
   1022 		sysctl_aio_listio_max, 0, &aio_listio_max, 0,
   1023 		CTL_KERN, CTL_CREATE, CTL_EOL);
   1024 	sysctl_createv(clog, 0, NULL, NULL,
   1025 		CTLFLAG_PERMANENT | CTLFLAG_READWRITE,
   1026 		CTLTYPE_INT, "aio_max",
   1027 		SYSCTL_DESCR("Maximum number of asynchronous I/O "
   1028 			     "operations"),
   1029 		sysctl_aio_max, 0, &aio_max, 0,
   1030 		CTL_KERN, CTL_CREATE, CTL_EOL);
   1031 }
   1032 
   1033 /*
   1034  * Debugging
   1035  */
   1036 #if defined(DDB)
   1037 void
   1038 aio_print_jobs(void (*pr)(const char *, ...))
   1039 {
   1040 	struct proc *p = (curlwp == NULL ? NULL : curlwp->l_proc);
   1041 	struct aioproc *aio;
   1042 	struct aio_job *a_job;
   1043 	struct aiocb *aiocbp;
   1044 
   1045 	if (p == NULL) {
   1046 		(*pr)("AIO: We are not in the processes right now.\n");
   1047 		return;
   1048 	}
   1049 
   1050 	aio = p->p_aio;
   1051 	if (aio == NULL) {
   1052 		(*pr)("AIO data is not initialized (PID = %d).\n", p->p_pid);
   1053 		return;
   1054 	}
   1055 
   1056 	(*pr)("AIO: PID = %d\n", p->p_pid);
   1057 	(*pr)("AIO: Global count of the jobs = %u\n", aio_jobs_count);
   1058 	(*pr)("AIO: Count of the jobs = %u\n", aio->jobs_count);
   1059 
   1060 	if (aio->curjob) {
   1061 		a_job = aio->curjob;
   1062 		(*pr)("\nAIO current job:\n");
   1063 		(*pr)(" opcode = %d, errno = %d, state = %d, aiocb_ptr = %p\n",
   1064 		    a_job->aio_op, a_job->aiocbp._errno,
   1065 		    a_job->aiocbp._state, a_job->aiocb_uptr);
   1066 		aiocbp = &a_job->aiocbp;
   1067 		(*pr)("   fd = %d, offset = %u, buf = %p, nbytes = %u\n",
   1068 		    aiocbp->aio_fildes, aiocbp->aio_offset,
   1069 		    aiocbp->aio_buf, aiocbp->aio_nbytes);
   1070 	}
   1071 
   1072 	(*pr)("\nAIO queue:\n");
   1073 	TAILQ_FOREACH(a_job, &aio->jobs_queue, list) {
   1074 		(*pr)(" opcode = %d, errno = %d, state = %d, aiocb_ptr = %p\n",
   1075 		    a_job->aio_op, a_job->aiocbp._errno,
   1076 		    a_job->aiocbp._state, a_job->aiocb_uptr);
   1077 		aiocbp = &a_job->aiocbp;
   1078 		(*pr)("   fd = %d, offset = %u, buf = %p, nbytes = %u\n",
   1079 		    aiocbp->aio_fildes, aiocbp->aio_offset,
   1080 		    aiocbp->aio_buf, aiocbp->aio_nbytes);
   1081 	}
   1082 }
   1083 #endif /* defined(DDB) */
   1084