1 /* $NetBSD: sys_aio.c,v 1.29 2009/10/21 21:12:06 rmind Exp $ */
4 * Copyright (c) 2007, Mindaugas Rasiukevicius <rmind at NetBSD org>
7 * Redistribution and use in source and binary forms, with or without
8 * modification, are permitted provided that the following conditions
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.
16 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
17 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
18 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
19 * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
20 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
21 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
22 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
23 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
24 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
25 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
30 * Implementation of POSIX asynchronous I/O.
31 * Defined in the Base Definitions volume of IEEE Std 1003.1-2001.
34 #include <sys/cdefs.h>
35 __KERNEL_RCSID(0, "$NetBSD: sys_aio.c,v 1.29 2009/10/21 21:12:06 rmind Exp $");
41 #include <sys/param.h>
42 #include <sys/condvar.h>
44 #include <sys/filedesc.h>
45 #include <sys/kernel.h>
48 #include <sys/mutex.h>
51 #include <sys/queue.h>
52 #include <sys/signal.h>
53 #include <sys/signalvar.h>
54 #include <sys/syscall.h>
55 #include <sys/syscallargs.h>
56 #include <sys/syscallvar.h>
57 #include <sys/sysctl.h>
58 #include <sys/systm.h>
59 #include <sys/types.h>
60 #include <sys/vnode.h>
61 #include <sys/atomic.h>
62 #include <sys/module.h>
65 #include <uvm/uvm_extern.h>
67 MODULE(MODULE_CLASS_MISC
, aio
, NULL
);
70 * System-wide limits and counter of AIO operations.
72 u_int aio_listio_max
= AIO_LISTIO_MAX
;
73 static u_int aio_max
= AIO_MAX
;
74 static u_int aio_jobs_count
;
76 static struct pool aio_job_pool
;
77 static struct pool aio_lio_pool
;
78 static void *aio_ehook
;
81 static void aio_worker(void *);
82 static void aio_process(struct aio_job
*);
83 static void aio_sendsig(struct proc
*, struct sigevent
*);
84 static int aio_enqueue_job(int, void *, struct lio_req
*);
85 static void aio_exit(proc_t
*, void *);
87 static const struct syscall_package aio_syscalls
[] = {
88 { SYS_aio_cancel
, 0, (sy_call_t
*)sys_aio_cancel
},
89 { SYS_aio_error
, 0, (sy_call_t
*)sys_aio_error
},
90 { SYS_aio_fsync
, 0, (sy_call_t
*)sys_aio_fsync
},
91 { SYS_aio_read
, 0, (sy_call_t
*)sys_aio_read
},
92 { SYS_aio_return
, 0, (sy_call_t
*)sys_aio_return
},
93 { SYS___aio_suspend50
, 0, (sy_call_t
*)sys___aio_suspend50
},
94 { SYS_aio_write
, 0, (sy_call_t
*)sys_aio_write
},
95 { SYS_lio_listio
, 0, (sy_call_t
*)sys_lio_listio
},
100 * Tear down all AIO state.
103 aio_fini(bool interface
)
109 /* Stop syscall activity. */
110 error
= syscall_disestablish(NULL
, aio_syscalls
);
113 /* Abort if any processes are using AIO. */
114 mutex_enter(proc_lock
);
115 PROCLIST_FOREACH(p
, &allproc
) {
116 if (p
->p_aio
!= NULL
)
119 mutex_exit(proc_lock
);
121 error
= syscall_establish(NULL
, aio_syscalls
);
126 KASSERT(aio_jobs_count
== 0);
127 exithook_disestablish(aio_ehook
);
128 pool_destroy(&aio_job_pool
);
129 pool_destroy(&aio_lio_pool
);
134 * Initialize global AIO state.
141 pool_init(&aio_job_pool
, sizeof(struct aio_job
), 0, 0, 0,
142 "aio_jobs_pool", &pool_allocator_nointr
, IPL_NONE
);
143 pool_init(&aio_lio_pool
, sizeof(struct lio_req
), 0, 0, 0,
144 "aio_lio_pool", &pool_allocator_nointr
, IPL_NONE
);
145 aio_ehook
= exithook_establish(aio_exit
, NULL
);
146 error
= syscall_establish(NULL
, aio_syscalls
);
156 aio_modcmd(modcmd_t cmd
, void *arg
)
160 case MODULE_CMD_INIT
:
162 case MODULE_CMD_FINI
:
163 return aio_fini(true);
170 * Initialize Asynchronous I/O data structures for the process.
173 aio_procinit(struct proc
*p
)
180 /* Allocate and initialize AIO structure */
181 aio
= kmem_zalloc(sizeof(struct aioproc
), KM_SLEEP
);
185 /* Initialize queue and their synchronization structures */
186 mutex_init(&aio
->aio_mtx
, MUTEX_DEFAULT
, IPL_NONE
);
187 cv_init(&aio
->aio_worker_cv
, "aiowork");
188 cv_init(&aio
->done_cv
, "aiodone");
189 TAILQ_INIT(&aio
->jobs_queue
);
192 * Create an AIO worker thread.
193 * XXX: Currently, AIO thread is not protected against user's actions.
195 uaddr
= uvm_uarea_alloc();
200 error
= lwp_create(curlwp
, p
, uaddr
, 0, NULL
, 0, aio_worker
,
201 NULL
, &l
, curlwp
->l_class
);
203 uvm_uarea_free(uaddr
);
208 /* Recheck if we are really first */
209 mutex_enter(p
->p_lock
);
211 mutex_exit(p
->p_lock
);
218 /* Complete the initialization of thread, and run it */
223 l
->l_priority
= MAXPRI_USER
;
224 sched_enqueue(l
, false);
226 mutex_exit(p
->p_lock
);
232 * Exit of Asynchronous I/O subsystem of process.
235 aio_exit(struct proc
*p
, void *cookie
)
237 struct aio_job
*a_job
;
242 else if ((aio
= p
->p_aio
) == NULL
)
246 while (!TAILQ_EMPTY(&aio
->jobs_queue
)) {
247 a_job
= TAILQ_FIRST(&aio
->jobs_queue
);
248 TAILQ_REMOVE(&aio
->jobs_queue
, a_job
, list
);
249 pool_put(&aio_job_pool
, a_job
);
250 atomic_dec_uint(&aio_jobs_count
);
253 /* Destroy and free the entire AIO data structure */
254 cv_destroy(&aio
->aio_worker_cv
);
255 cv_destroy(&aio
->done_cv
);
256 mutex_destroy(&aio
->aio_mtx
);
257 kmem_free(aio
, sizeof(struct aioproc
));
261 * AIO worker thread and processor.
264 aio_worker(void *arg
)
266 struct proc
*p
= curlwp
->l_proc
;
267 struct aioproc
*aio
= p
->p_aio
;
268 struct aio_job
*a_job
;
274 * Make an empty signal mask, so it
275 * handles only SIGKILL and SIGSTOP.
278 mutex_enter(p
->p_lock
);
279 error
= sigprocmask1(curlwp
, SIG_SETMASK
, &nss
, &oss
);
280 mutex_exit(p
->p_lock
);
285 * Loop for each job in the queue. If there
286 * are no jobs then sleep.
288 mutex_enter(&aio
->aio_mtx
);
289 while ((a_job
= TAILQ_FIRST(&aio
->jobs_queue
)) == NULL
) {
290 if (cv_wait_sig(&aio
->aio_worker_cv
, &aio
->aio_mtx
)) {
292 * Thread was interrupted - check for
293 * pending exit or suspend.
295 mutex_exit(&aio
->aio_mtx
);
297 mutex_enter(&aio
->aio_mtx
);
301 /* Take the job from the queue */
303 TAILQ_REMOVE(&aio
->jobs_queue
, a_job
, list
);
305 atomic_dec_uint(&aio_jobs_count
);
308 mutex_exit(&aio
->aio_mtx
);
310 /* Process an AIO operation */
313 /* Copy data structure back to the user-space */
314 (void)copyout(&a_job
->aiocbp
, a_job
->aiocb_uptr
,
315 sizeof(struct aiocb
));
317 mutex_enter(&aio
->aio_mtx
);
320 /* Decrease a reference counter, if there is a LIO structure */
322 refcnt
= (lio
!= NULL
? --lio
->refcnt
: -1);
324 /* Notify all suspenders */
325 cv_broadcast(&aio
->done_cv
);
326 mutex_exit(&aio
->aio_mtx
);
328 /* Send a signal, if any */
329 aio_sendsig(p
, &a_job
->aiocbp
.aio_sigevent
);
331 /* Destroy the LIO structure */
333 aio_sendsig(p
, &lio
->sig
);
334 pool_put(&aio_lio_pool
, lio
);
337 /* Destroy the job */
338 pool_put(&aio_job_pool
, a_job
);
345 aio_process(struct aio_job
*a_job
)
347 struct proc
*p
= curlwp
->l_proc
;
348 struct aiocb
*aiocbp
= &a_job
->aiocbp
;
350 int fd
= aiocbp
->aio_fildes
;
353 KASSERT(a_job
->aio_op
!= 0);
355 if ((a_job
->aio_op
& (AIO_READ
| AIO_WRITE
)) != 0) {
359 if (aiocbp
->aio_nbytes
> SSIZE_MAX
) {
370 aiov
.iov_base
= (void *)(uintptr_t)aiocbp
->aio_buf
;
371 aiov
.iov_len
= aiocbp
->aio_nbytes
;
372 auio
.uio_iov
= &aiov
;
374 auio
.uio_resid
= aiocbp
->aio_nbytes
;
375 auio
.uio_vmspace
= p
->p_vmspace
;
377 if (a_job
->aio_op
& AIO_READ
) {
379 * Perform a Read operation
381 KASSERT((a_job
->aio_op
& AIO_WRITE
) == 0);
383 if ((fp
->f_flag
& FREAD
) == 0) {
388 auio
.uio_rw
= UIO_READ
;
389 error
= (*fp
->f_ops
->fo_read
)(fp
, &aiocbp
->aio_offset
,
390 &auio
, fp
->f_cred
, FOF_UPDATE_OFFSET
);
393 * Perform a Write operation
395 KASSERT(a_job
->aio_op
& AIO_WRITE
);
397 if ((fp
->f_flag
& FWRITE
) == 0) {
402 auio
.uio_rw
= UIO_WRITE
;
403 error
= (*fp
->f_ops
->fo_write
)(fp
, &aiocbp
->aio_offset
,
404 &auio
, fp
->f_cred
, FOF_UPDATE_OFFSET
);
408 /* Store the result value */
409 a_job
->aiocbp
.aio_nbytes
-= auio
.uio_resid
;
410 a_job
->aiocbp
._retval
= (error
== 0) ?
411 a_job
->aiocbp
.aio_nbytes
: -1;
413 } else if ((a_job
->aio_op
& (AIO_SYNC
| AIO_DSYNC
)) != 0) {
415 * Perform a file Sync operation
419 if ((error
= fd_getvnode(fd
, &fp
)) != 0)
422 if ((fp
->f_flag
& FWRITE
) == 0) {
428 vp
= (struct vnode
*)fp
->f_data
;
429 vn_lock(vp
, LK_EXCLUSIVE
| LK_RETRY
);
430 if (a_job
->aio_op
& AIO_DSYNC
) {
431 error
= VOP_FSYNC(vp
, fp
->f_cred
,
432 FSYNC_WAIT
| FSYNC_DATAONLY
, 0, 0);
433 } else if (a_job
->aio_op
& AIO_SYNC
) {
434 error
= VOP_FSYNC(vp
, fp
->f_cred
,
440 /* Store the result value */
441 a_job
->aiocbp
._retval
= (error
== 0) ? 0 : -1;
444 panic("aio_process: invalid operation code\n");
447 /* Job is done, set the error, if any */
448 a_job
->aiocbp
._errno
= error
;
449 a_job
->aiocbp
._state
= JOB_DONE
;
456 aio_sendsig(struct proc
*p
, struct sigevent
*sig
)
460 if (sig
->sigev_signo
== 0 || sig
->sigev_notify
== SIGEV_NONE
)
464 ksi
.ksi_signo
= sig
->sigev_signo
;
465 ksi
.ksi_code
= SI_ASYNCIO
;
466 ksi
.ksi_value
= sig
->sigev_value
;
467 mutex_enter(proc_lock
);
468 kpsignal(p
, &ksi
, NULL
);
469 mutex_exit(proc_lock
);
476 aio_enqueue_job(int op
, void *aiocb_uptr
, struct lio_req
*lio
)
478 struct proc
*p
= curlwp
->l_proc
;
480 struct aio_job
*a_job
;
482 struct sigevent
*sig
;
485 /* Non-accurate check for the limit */
486 if (aio_jobs_count
+ 1 > aio_max
)
489 /* Get the data structure from user-space */
490 error
= copyin(aiocb_uptr
, &aiocbp
, sizeof(struct aiocb
));
494 /* Check if signal is set, and validate it */
495 sig
= &aiocbp
.aio_sigevent
;
496 if (sig
->sigev_signo
< 0 || sig
->sigev_signo
>= NSIG
||
497 sig
->sigev_notify
< SIGEV_NONE
|| sig
->sigev_notify
> SIGEV_SA
)
500 /* Buffer and byte count */
501 if (((AIO_SYNC
| AIO_DSYNC
) & op
) == 0)
502 if (aiocbp
.aio_buf
== NULL
|| aiocbp
.aio_nbytes
> SSIZE_MAX
)
505 /* Check the opcode, if LIO_NOP - simply ignore */
507 KASSERT(lio
!= NULL
);
508 if (aiocbp
.aio_lio_opcode
== LIO_WRITE
)
510 else if (aiocbp
.aio_lio_opcode
== LIO_READ
)
513 return (aiocbp
.aio_lio_opcode
== LIO_NOP
) ? 0 : EINVAL
;
515 KASSERT(lio
== NULL
);
519 * Look for already existing job. If found - the job is in-progress.
520 * According to POSIX this is invalid, so return the error.
524 mutex_enter(&aio
->aio_mtx
);
527 if (a_job
->aiocb_uptr
== aiocb_uptr
) {
528 mutex_exit(&aio
->aio_mtx
);
532 TAILQ_FOREACH(a_job
, &aio
->jobs_queue
, list
) {
533 if (a_job
->aiocb_uptr
!= aiocb_uptr
)
535 mutex_exit(&aio
->aio_mtx
);
538 mutex_exit(&aio
->aio_mtx
);
542 * Check if AIO structure is initialized, if not - initialize it.
543 * In LIO case, we did that already. We will recheck this with
544 * the lock in aio_procinit().
546 if (lio
== NULL
&& p
->p_aio
== NULL
)
552 * Set the state with errno, and copy data
553 * structure back to the user-space.
555 aiocbp
._state
= JOB_WIP
;
556 aiocbp
._errno
= EINPROGRESS
;
558 error
= copyout(&aiocbp
, aiocb_uptr
, sizeof(struct aiocb
));
562 /* Allocate and initialize a new AIO job */
563 a_job
= pool_get(&aio_job_pool
, PR_WAITOK
);
564 memset(a_job
, 0, sizeof(struct aio_job
));
568 * Store the user-space pointer for searching. Since we
569 * are storing only per proc pointers - it is safe.
571 memcpy(&a_job
->aiocbp
, &aiocbp
, sizeof(struct aiocb
));
572 a_job
->aiocb_uptr
= aiocb_uptr
;
577 * Add the job to the queue, update the counters, and
578 * notify the AIO worker thread to handle the job.
580 mutex_enter(&aio
->aio_mtx
);
582 /* Fail, if the limit was reached */
583 if (atomic_inc_uint_nv(&aio_jobs_count
) > aio_max
||
584 aio
->jobs_count
>= aio_listio_max
) {
585 atomic_dec_uint(&aio_jobs_count
);
586 mutex_exit(&aio
->aio_mtx
);
587 pool_put(&aio_job_pool
, a_job
);
591 TAILQ_INSERT_TAIL(&aio
->jobs_queue
, a_job
, list
);
595 cv_signal(&aio
->aio_worker_cv
);
597 mutex_exit(&aio
->aio_mtx
);
600 * One would handle the errors only with aio_error() function.
601 * This way is appropriate according to POSIX.
611 sys_aio_cancel(struct lwp
*l
, const struct sys_aio_cancel_args
*uap
,
615 syscallarg(int) fildes;
616 syscallarg(struct aiocb *) aiocbp;
618 struct proc
*p
= l
->l_proc
;
620 struct aio_job
*a_job
;
621 struct aiocb
*aiocbp_ptr
;
623 struct filedesc
*fdp
= p
->p_fd
;
624 unsigned int cn
, errcnt
, fildes
;
627 TAILQ_HEAD(, aio_job
) tmp_jobs_list
;
629 /* Check for invalid file descriptor */
630 fildes
= (unsigned int)SCARG(uap
, fildes
);
632 if (fildes
>= dt
->dt_nfiles
)
634 if (dt
->dt_ff
[fildes
] == NULL
|| dt
->dt_ff
[fildes
]->ff_file
== NULL
)
637 /* Check if AIO structure is initialized */
638 if (p
->p_aio
== NULL
) {
639 *retval
= AIO_NOTCANCELED
;
644 aiocbp_ptr
= (struct aiocb
*)SCARG(uap
, aiocbp
);
646 mutex_enter(&aio
->aio_mtx
);
648 /* Cancel the jobs, and remove them from the queue */
650 TAILQ_INIT(&tmp_jobs_list
);
651 TAILQ_FOREACH(a_job
, &aio
->jobs_queue
, list
) {
653 if (aiocbp_ptr
!= a_job
->aiocb_uptr
)
655 if (fildes
!= a_job
->aiocbp
.aio_fildes
) {
656 mutex_exit(&aio
->aio_mtx
);
659 } else if (a_job
->aiocbp
.aio_fildes
!= fildes
)
662 TAILQ_REMOVE(&aio
->jobs_queue
, a_job
, list
);
663 TAILQ_INSERT_TAIL(&tmp_jobs_list
, a_job
, list
);
665 /* Decrease the counters */
666 atomic_dec_uint(&aio_jobs_count
);
669 if (lio
!= NULL
&& --lio
->refcnt
!= 0)
677 /* There are canceled jobs */
679 *retval
= AIO_CANCELED
;
681 /* We cannot cancel current job */
683 if (a_job
&& ((a_job
->aiocbp
.aio_fildes
== fildes
) ||
684 (a_job
->aiocb_uptr
== aiocbp_ptr
)))
685 *retval
= AIO_NOTCANCELED
;
687 mutex_exit(&aio
->aio_mtx
);
689 /* Free the jobs after the lock */
691 while (!TAILQ_EMPTY(&tmp_jobs_list
)) {
692 a_job
= TAILQ_FIRST(&tmp_jobs_list
);
693 TAILQ_REMOVE(&tmp_jobs_list
, a_job
, list
);
694 /* Set the errno and copy structures back to the user-space */
695 a_job
->aiocbp
._errno
= ECANCELED
;
696 a_job
->aiocbp
._state
= JOB_DONE
;
697 if (copyout(&a_job
->aiocbp
, a_job
->aiocb_uptr
,
698 sizeof(struct aiocb
)))
700 /* Send a signal if any */
701 aio_sendsig(p
, &a_job
->aiocbp
.aio_sigevent
);
704 aio_sendsig(p
, &lio
->sig
);
705 pool_put(&aio_lio_pool
, lio
);
707 pool_put(&aio_job_pool
, a_job
);
713 /* Set a correct return value */
715 *retval
= AIO_ALLDONE
;
721 sys_aio_error(struct lwp
*l
, const struct sys_aio_error_args
*uap
,
725 syscallarg(const struct aiocb *) aiocbp;
727 struct proc
*p
= l
->l_proc
;
728 struct aioproc
*aio
= p
->p_aio
;
735 error
= copyin(SCARG(uap
, aiocbp
), &aiocbp
, sizeof(struct aiocb
));
739 if (aiocbp
._state
== JOB_NONE
)
742 *retval
= aiocbp
._errno
;
748 sys_aio_fsync(struct lwp
*l
, const struct sys_aio_fsync_args
*uap
,
753 syscallarg(struct aiocb *) aiocbp;
755 int op
= SCARG(uap
, op
);
757 if ((op
!= O_DSYNC
) && (op
!= O_SYNC
))
760 op
= O_DSYNC
? AIO_DSYNC
: AIO_SYNC
;
762 return aio_enqueue_job(op
, SCARG(uap
, aiocbp
), NULL
);
766 sys_aio_read(struct lwp
*l
, const struct sys_aio_read_args
*uap
,
770 syscallarg(struct aiocb *) aiocbp;
773 return aio_enqueue_job(AIO_READ
, SCARG(uap
, aiocbp
), NULL
);
777 sys_aio_return(struct lwp
*l
, const struct sys_aio_return_args
*uap
,
781 syscallarg(struct aiocb *) aiocbp;
783 struct proc
*p
= l
->l_proc
;
784 struct aioproc
*aio
= p
->p_aio
;
791 error
= copyin(SCARG(uap
, aiocbp
), &aiocbp
, sizeof(struct aiocb
));
795 if (aiocbp
._errno
== EINPROGRESS
|| aiocbp
._state
!= JOB_DONE
)
798 *retval
= aiocbp
._retval
;
800 /* Reset the internal variables */
803 aiocbp
._state
= JOB_NONE
;
804 error
= copyout(&aiocbp
, SCARG(uap
, aiocbp
), sizeof(struct aiocb
));
810 sys___aio_suspend50(struct lwp
*l
, const struct sys___aio_suspend50_args
*uap
,
814 syscallarg(const struct aiocb *const[]) list;
815 syscallarg(int) nent;
816 syscallarg(const struct timespec *) timeout;
822 nent
= SCARG(uap
, nent
);
823 if (nent
<= 0 || nent
> aio_listio_max
)
826 if (SCARG(uap
, timeout
)) {
827 /* Convert timespec to ticks */
828 error
= copyin(SCARG(uap
, timeout
), &ts
,
829 sizeof(struct timespec
));
833 list
= kmem_alloc(nent
* sizeof(*list
), KM_SLEEP
);
834 error
= copyin(SCARG(uap
, list
), list
, nent
* sizeof(*list
));
837 error
= aio_suspend1(l
, list
, nent
, SCARG(uap
, timeout
) ? &ts
: NULL
);
839 kmem_free(list
, nent
* sizeof(*list
));
844 aio_suspend1(struct lwp
*l
, struct aiocb
**aiocbp_list
, int nent
,
847 struct proc
*p
= l
->l_proc
;
849 struct aio_job
*a_job
;
852 if (p
->p_aio
== NULL
)
857 timo
= mstohz((ts
->tv_sec
* 1000) + (ts
->tv_nsec
/ 1000000));
858 if (timo
== 0 && ts
->tv_sec
== 0 && ts
->tv_nsec
> 0)
865 /* Get the list from user-space */
867 mutex_enter(&aio
->aio_mtx
);
870 for (i
= 0; i
< nent
; i
++) {
872 /* Skip NULL entries */
873 if (aiocbp_list
[i
] == NULL
)
876 /* Skip current job */
879 if (a_job
->aiocb_uptr
== aiocbp_list
[i
])
883 /* Look for a job in the queue */
884 TAILQ_FOREACH(a_job
, &aio
->jobs_queue
, list
)
885 if (a_job
->aiocb_uptr
== aiocbp_list
[i
])
891 mutex_exit(&aio
->aio_mtx
);
893 error
= copyin(aiocbp_list
[i
], &aiocbp
,
894 sizeof(struct aiocb
));
895 if (error
== 0 && aiocbp
._state
!= JOB_DONE
) {
896 mutex_enter(&aio
->aio_mtx
);
903 /* Wait for a signal or when timeout occurs */
904 error
= cv_timedwait_sig(&aio
->done_cv
, &aio
->aio_mtx
, timo
);
906 if (error
== EWOULDBLOCK
)
911 mutex_exit(&aio
->aio_mtx
);
916 sys_aio_write(struct lwp
*l
, const struct sys_aio_write_args
*uap
,
920 syscallarg(struct aiocb *) aiocbp;
923 return aio_enqueue_job(AIO_WRITE
, SCARG(uap
, aiocbp
), NULL
);
927 sys_lio_listio(struct lwp
*l
, const struct sys_lio_listio_args
*uap
,
931 syscallarg(int) mode;
932 syscallarg(struct aiocb *const[]) list;
933 syscallarg(int) nent;
934 syscallarg(struct sigevent *) sig;
936 struct proc
*p
= l
->l_proc
;
938 struct aiocb
**aiocbp_list
;
940 int i
, error
, errcnt
, mode
, nent
;
942 mode
= SCARG(uap
, mode
);
943 nent
= SCARG(uap
, nent
);
945 /* Non-accurate checks for the limit and invalid values */
946 if (nent
< 1 || nent
> aio_listio_max
)
948 if (aio_jobs_count
+ nent
> aio_max
)
951 /* Check if AIO structure is initialized, if not - initialize it */
952 if (p
->p_aio
== NULL
)
957 /* Create a LIO structure */
958 lio
= pool_get(&aio_lio_pool
, PR_WAITOK
);
964 memset(&lio
->sig
, 0, sizeof(struct sigevent
));
967 /* Check for signal, validate it */
968 if (SCARG(uap
, sig
)) {
969 struct sigevent
*sig
= &lio
->sig
;
971 error
= copyin(SCARG(uap
, sig
), &lio
->sig
,
972 sizeof(struct sigevent
));
974 (sig
->sigev_signo
< 0 ||
975 sig
->sigev_signo
>= NSIG
||
976 sig
->sigev_notify
< SIGEV_NONE
||
977 sig
->sigev_notify
> SIGEV_SA
))
980 memset(&lio
->sig
, 0, sizeof(struct sigevent
));
988 pool_put(&aio_lio_pool
, lio
);
992 /* Get the list from user-space */
993 aiocbp_list
= kmem_alloc(nent
* sizeof(*aiocbp_list
), KM_SLEEP
);
994 error
= copyin(SCARG(uap
, list
), aiocbp_list
,
995 nent
* sizeof(*aiocbp_list
));
997 mutex_enter(&aio
->aio_mtx
);
1001 /* Enqueue all jobs */
1003 for (i
= 0; i
< nent
; i
++) {
1004 error
= aio_enqueue_job(AIO_LIO
, aiocbp_list
[i
], lio
);
1006 * According to POSIX, in such error case it may
1007 * fail with other I/O operations initiated.
1013 mutex_enter(&aio
->aio_mtx
);
1015 /* Return an error, if any */
1021 if (mode
== LIO_WAIT
) {
1023 * Wait for AIO completion. In such case,
1024 * the LIO structure will be freed here.
1026 while (lio
->refcnt
> 1 && error
== 0)
1027 error
= cv_wait_sig(&aio
->done_cv
, &aio
->aio_mtx
);
1033 if (--lio
->refcnt
!= 0)
1035 mutex_exit(&aio
->aio_mtx
);
1037 aio_sendsig(p
, &lio
->sig
);
1038 pool_put(&aio_lio_pool
, lio
);
1040 kmem_free(aiocbp_list
, nent
* sizeof(*aiocbp_list
));
1049 sysctl_aio_listio_max(SYSCTLFN_ARGS
)
1051 struct sysctlnode node
;
1055 node
.sysctl_data
= &newsize
;
1057 newsize
= aio_listio_max
;
1058 error
= sysctl_lookup(SYSCTLFN_CALL(&node
));
1059 if (error
|| newp
== NULL
)
1062 if (newsize
< 1 || newsize
> aio_max
)
1064 aio_listio_max
= newsize
;
1070 sysctl_aio_max(SYSCTLFN_ARGS
)
1072 struct sysctlnode node
;
1076 node
.sysctl_data
= &newsize
;
1079 error
= sysctl_lookup(SYSCTLFN_CALL(&node
));
1080 if (error
|| newp
== NULL
)
1083 if (newsize
< 1 || newsize
< aio_listio_max
)
1090 SYSCTL_SETUP(sysctl_aio_setup
, "sysctl aio setup")
1093 sysctl_createv(clog
, 0, NULL
, NULL
,
1095 CTLTYPE_NODE
, "kern", NULL
,
1098 sysctl_createv(clog
, 0, NULL
, NULL
,
1099 CTLFLAG_PERMANENT
| CTLFLAG_IMMEDIATE
,
1100 CTLTYPE_INT
, "posix_aio",
1101 SYSCTL_DESCR("Version of IEEE Std 1003.1 and its "
1102 "Asynchronous I/O option to which the "
1103 "system attempts to conform"),
1104 NULL
, _POSIX_ASYNCHRONOUS_IO
, NULL
, 0,
1105 CTL_KERN
, CTL_CREATE
, CTL_EOL
);
1106 sysctl_createv(clog
, 0, NULL
, NULL
,
1107 CTLFLAG_PERMANENT
| CTLFLAG_READWRITE
,
1108 CTLTYPE_INT
, "aio_listio_max",
1109 SYSCTL_DESCR("Maximum number of asynchronous I/O "
1110 "operations in a single list I/O call"),
1111 sysctl_aio_listio_max
, 0, &aio_listio_max
, 0,
1112 CTL_KERN
, CTL_CREATE
, CTL_EOL
);
1113 sysctl_createv(clog
, 0, NULL
, NULL
,
1114 CTLFLAG_PERMANENT
| CTLFLAG_READWRITE
,
1115 CTLTYPE_INT
, "aio_max",
1116 SYSCTL_DESCR("Maximum number of asynchronous I/O "
1118 sysctl_aio_max
, 0, &aio_max
, 0,
1119 CTL_KERN
, CTL_CREATE
, CTL_EOL
);
1127 aio_print_jobs(void (*pr
)(const char *, ...))
1129 struct proc
*p
= (curlwp
== NULL
? NULL
: curlwp
->l_proc
);
1130 struct aioproc
*aio
;
1131 struct aio_job
*a_job
;
1132 struct aiocb
*aiocbp
;
1135 (*pr
)("AIO: We are not in the processes right now.\n");
1141 (*pr
)("AIO data is not initialized (PID = %d).\n", p
->p_pid
);
1145 (*pr
)("AIO: PID = %d\n", p
->p_pid
);
1146 (*pr
)("AIO: Global count of the jobs = %u\n", aio_jobs_count
);
1147 (*pr
)("AIO: Count of the jobs = %u\n", aio
->jobs_count
);
1150 a_job
= aio
->curjob
;
1151 (*pr
)("\nAIO current job:\n");
1152 (*pr
)(" opcode = %d, errno = %d, state = %d, aiocb_ptr = %p\n",
1153 a_job
->aio_op
, a_job
->aiocbp
._errno
,
1154 a_job
->aiocbp
._state
, a_job
->aiocb_uptr
);
1155 aiocbp
= &a_job
->aiocbp
;
1156 (*pr
)(" fd = %d, offset = %u, buf = %p, nbytes = %u\n",
1157 aiocbp
->aio_fildes
, aiocbp
->aio_offset
,
1158 aiocbp
->aio_buf
, aiocbp
->aio_nbytes
);
1161 (*pr
)("\nAIO queue:\n");
1162 TAILQ_FOREACH(a_job
, &aio
->jobs_queue
, list
) {
1163 (*pr
)(" opcode = %d, errno = %d, state = %d, aiocb_ptr = %p\n",
1164 a_job
->aio_op
, a_job
->aiocbp
._errno
,
1165 a_job
->aiocbp
._state
, a_job
->aiocb_uptr
);
1166 aiocbp
= &a_job
->aiocbp
;
1167 (*pr
)(" fd = %d, offset = %u, buf = %p, nbytes = %u\n",
1168 aiocbp
->aio_fildes
, aiocbp
->aio_offset
,
1169 aiocbp
->aio_buf
, aiocbp
->aio_nbytes
);
1172 #endif /* defined(DDB) */