sys_aio.c revision 1.12 1 /* $NetBSD: sys_aio.c,v 1.12 2007/11/29 18:17:01 rmind 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.12 2007/11/29 18:17:01 rmind 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_NOSLEEP);
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 struct filedesc *fdp = p->p_fd;
273 int fd = aiocbp->aio_fildes;
274 int error = 0;
275
276 KASSERT(fdp != NULL);
277 KASSERT(a_job->aio_op != 0);
278
279 if ((a_job->aio_op & (AIO_READ | AIO_WRITE)) != 0) {
280 struct iovec aiov;
281 struct uio auio;
282
283 if (aiocbp->aio_nbytes > SSIZE_MAX) {
284 error = EINVAL;
285 goto done;
286 }
287
288 fp = fd_getfile(fdp, fd);
289 if (fp == NULL) {
290 error = EBADF;
291 goto done;
292 }
293
294 aiov.iov_base = (void *)(uintptr_t)aiocbp->aio_buf;
295 aiov.iov_len = aiocbp->aio_nbytes;
296 auio.uio_iov = &aiov;
297 auio.uio_iovcnt = 1;
298 auio.uio_resid = aiocbp->aio_nbytes;
299 auio.uio_vmspace = p->p_vmspace;
300
301 FILE_USE(fp);
302 if (a_job->aio_op & AIO_READ) {
303 /*
304 * Perform a Read operation
305 */
306 KASSERT((a_job->aio_op & AIO_WRITE) == 0);
307
308 if ((fp->f_flag & FREAD) == 0) {
309 FILE_UNUSE(fp, curlwp);
310 error = EBADF;
311 goto done;
312 }
313 auio.uio_rw = UIO_READ;
314 error = (*fp->f_ops->fo_read)(fp, &aiocbp->aio_offset,
315 &auio, fp->f_cred, FOF_UPDATE_OFFSET);
316 } else {
317 /*
318 * Perform a Write operation
319 */
320 KASSERT(a_job->aio_op & AIO_WRITE);
321
322 if ((fp->f_flag & FWRITE) == 0) {
323 FILE_UNUSE(fp, curlwp);
324 error = EBADF;
325 goto done;
326 }
327 auio.uio_rw = UIO_WRITE;
328 error = (*fp->f_ops->fo_write)(fp, &aiocbp->aio_offset,
329 &auio, fp->f_cred, FOF_UPDATE_OFFSET);
330 }
331 FILE_UNUSE(fp, curlwp);
332
333 /* Store the result value */
334 a_job->aiocbp.aio_nbytes -= auio.uio_resid;
335 a_job->aiocbp._retval = (error == 0) ?
336 a_job->aiocbp.aio_nbytes : -1;
337
338 } else if ((a_job->aio_op & (AIO_SYNC | AIO_DSYNC)) != 0) {
339 /*
340 * Perform a file Sync operation
341 */
342 struct vnode *vp;
343
344 if ((error = getvnode(fdp, fd, &fp)) != 0)
345 goto done;
346
347 if ((fp->f_flag & FWRITE) == 0) {
348 FILE_UNUSE(fp, curlwp);
349 error = EBADF;
350 goto done;
351 }
352
353 vp = (struct vnode *)fp->f_data;
354 vn_lock(vp, LK_EXCLUSIVE | LK_RETRY);
355 if (a_job->aio_op & AIO_DSYNC) {
356 error = VOP_FSYNC(vp, fp->f_cred,
357 FSYNC_WAIT | FSYNC_DATAONLY, 0, 0);
358 } else if (a_job->aio_op & AIO_SYNC) {
359 error = VOP_FSYNC(vp, fp->f_cred,
360 FSYNC_WAIT, 0, 0);
361 if (error == 0 && bioopsp != NULL &&
362 vp->v_mount &&
363 (vp->v_mount->mnt_flag & MNT_SOFTDEP))
364 bioopsp->io_fsync(vp, 0);
365 }
366 VOP_UNLOCK(vp, 0);
367 FILE_UNUSE(fp, curlwp);
368
369 /* Store the result value */
370 a_job->aiocbp._retval = (error == 0) ? 0 : -1;
371
372 } else
373 panic("aio_process: invalid operation code\n");
374
375 done:
376 /* Job is done, set the error, if any */
377 a_job->aiocbp._errno = error;
378 a_job->aiocbp._state = JOB_DONE;
379 }
380
381 /*
382 * Send AIO signal.
383 */
384 static void
385 aio_sendsig(struct proc *p, struct sigevent *sig)
386 {
387 ksiginfo_t ksi;
388
389 if (sig->sigev_signo == 0 || sig->sigev_notify == SIGEV_NONE)
390 return;
391
392 KSI_INIT(&ksi);
393 ksi.ksi_signo = sig->sigev_signo;
394 ksi.ksi_code = SI_ASYNCIO;
395 ksi.ksi_value = sig->sigev_value;
396 mutex_enter(&proclist_mutex);
397 kpsignal(p, &ksi, NULL);
398 mutex_exit(&proclist_mutex);
399 }
400
401 /*
402 * Enqueue the job.
403 */
404 static int
405 aio_enqueue_job(int op, void *aiocb_uptr, struct lio_req *lio)
406 {
407 struct proc *p = curlwp->l_proc;
408 struct aioproc *aio;
409 struct aio_job *a_job;
410 struct aiocb aiocbp;
411 struct sigevent *sig;
412 int error;
413
414 /* Non-accurate check for the limit */
415 if (aio_jobs_count + 1 > aio_max)
416 return EAGAIN;
417
418 /* Get the data structure from user-space */
419 error = copyin(aiocb_uptr, &aiocbp, sizeof(struct aiocb));
420 if (error)
421 return error;
422
423 /* Check if signal is set, and validate it */
424 sig = &aiocbp.aio_sigevent;
425 if (sig->sigev_signo < 0 || sig->sigev_signo >= NSIG ||
426 sig->sigev_notify < SIGEV_NONE || sig->sigev_notify > SIGEV_SA)
427 return EINVAL;
428
429 /* Buffer and byte count */
430 if (((AIO_SYNC | AIO_DSYNC) & op) == 0)
431 if (aiocbp.aio_buf == NULL || aiocbp.aio_nbytes > SSIZE_MAX)
432 return EINVAL;
433
434 /* Check the opcode, if LIO_NOP - simply ignore */
435 if (op == AIO_LIO) {
436 KASSERT(lio != NULL);
437 if (aiocbp.aio_lio_opcode == LIO_WRITE)
438 op = AIO_WRITE;
439 else if (aiocbp.aio_lio_opcode == LIO_READ)
440 op = AIO_READ;
441 else
442 return (aiocbp.aio_lio_opcode == LIO_NOP) ? 0 : EINVAL;
443 } else {
444 KASSERT(lio == NULL);
445 }
446
447 /*
448 * Look for already existing job. If found - the job is in-progress.
449 * According to POSIX this is invalid, so return the error.
450 */
451 aio = p->p_aio;
452 if (aio) {
453 mutex_enter(&aio->aio_mtx);
454 if (aio->curjob) {
455 a_job = aio->curjob;
456 if (a_job->aiocb_uptr == aiocb_uptr) {
457 mutex_exit(&aio->aio_mtx);
458 return EINVAL;
459 }
460 }
461 TAILQ_FOREACH(a_job, &aio->jobs_queue, list) {
462 if (a_job->aiocb_uptr != aiocb_uptr)
463 continue;
464 mutex_exit(&aio->aio_mtx);
465 return EINVAL;
466 }
467 mutex_exit(&aio->aio_mtx);
468 }
469
470 /*
471 * Check if AIO structure is initialized, if not - initialize it.
472 * In LIO case, we did that already. We will recheck this with
473 * the lock in aio_init().
474 */
475 if (lio == NULL && p->p_aio == NULL)
476 if (aio_init(p))
477 return EAGAIN;
478 aio = p->p_aio;
479
480 /*
481 * Set the state with errno, and copy data
482 * structure back to the user-space.
483 */
484 aiocbp._state = JOB_WIP;
485 aiocbp._errno = EINPROGRESS;
486 aiocbp._retval = -1;
487 error = copyout(&aiocbp, aiocb_uptr, sizeof(struct aiocb));
488 if (error)
489 return error;
490
491 /* Allocate and initialize a new AIO job */
492 a_job = pool_get(&aio_job_pool, PR_WAITOK);
493 memset(a_job, 0, sizeof(struct aio_job));
494
495 /*
496 * Set the data.
497 * Store the user-space pointer for searching. Since we
498 * are storing only per proc pointers - it is safe.
499 */
500 memcpy(&a_job->aiocbp, &aiocbp, sizeof(struct aiocb));
501 a_job->aiocb_uptr = aiocb_uptr;
502 a_job->aio_op |= op;
503 a_job->lio = lio;
504
505 /*
506 * Add the job to the queue, update the counters, and
507 * notify the AIO worker thread to handle the job.
508 */
509 mutex_enter(&aio->aio_mtx);
510
511 /* Fail, if the limit was reached */
512 if (atomic_inc_uint_nv(&aio_jobs_count) > aio_max) {
513 atomic_dec_uint(&aio_jobs_count);
514 error = EAGAIN;
515 }
516 if (error || aio->jobs_count >= aio_listio_max) {
517 mutex_exit(&aio->aio_mtx);
518 pool_put(&aio_job_pool, a_job);
519 return EAGAIN;
520 }
521
522 TAILQ_INSERT_TAIL(&aio->jobs_queue, a_job, list);
523 atomic_inc_uint(&aio_jobs_count);
524 aio->jobs_count++;
525 if (lio)
526 lio->refcnt++;
527 cv_signal(&aio->aio_worker_cv);
528
529 mutex_exit(&aio->aio_mtx);
530
531 /*
532 * One would handle the errors only with aio_error() function.
533 * This way is appropriate according to POSIX.
534 */
535 return 0;
536 }
537
538 /*
539 * Syscall functions.
540 */
541
542 int
543 sys_aio_cancel(struct lwp *l, void *v, register_t *retval)
544 {
545 struct sys_aio_cancel_args /* {
546 syscallarg(int) fildes;
547 syscallarg(struct aiocb *) aiocbp;
548 } */ *uap = v;
549 struct proc *p = l->l_proc;
550 struct aioproc *aio;
551 struct aio_job *a_job;
552 struct aiocb *aiocbp_ptr;
553 struct lio_req *lio;
554 struct filedesc *fdp = p->p_fd;
555 unsigned int cn, errcnt, fildes;
556
557 TAILQ_HEAD(, aio_job) tmp_jobs_list;
558
559 /* Check for invalid file descriptor */
560 fildes = (unsigned int)SCARG(uap, fildes);
561 if (fildes >= fdp->fd_nfiles || fdp->fd_ofiles[fildes] == NULL)
562 return EBADF;
563
564 /* Check if AIO structure is initialized */
565 if (p->p_aio == NULL) {
566 *retval = AIO_NOTCANCELED;
567 return 0;
568 }
569
570 aio = p->p_aio;
571 aiocbp_ptr = (struct aiocb *)SCARG(uap, aiocbp);
572
573 mutex_enter(&aio->aio_mtx);
574
575 /* Cancel the jobs, and remove them from the queue */
576 cn = 0;
577 TAILQ_INIT(&tmp_jobs_list);
578 TAILQ_FOREACH(a_job, &aio->jobs_queue, list) {
579 if (aiocbp_ptr) {
580 if (aiocbp_ptr != a_job->aiocb_uptr)
581 continue;
582 if (fildes != a_job->aiocbp.aio_fildes) {
583 mutex_exit(&aio->aio_mtx);
584 return EBADF;
585 }
586 } else if (a_job->aiocbp.aio_fildes != fildes)
587 continue;
588
589 TAILQ_REMOVE(&aio->jobs_queue, a_job, list);
590 TAILQ_INSERT_TAIL(&tmp_jobs_list, a_job, list);
591
592 /* Decrease the counters */
593 atomic_dec_uint(&aio_jobs_count);
594 aio->jobs_count--;
595 lio = a_job->lio;
596 if (lio != NULL && --lio->refcnt != 0)
597 a_job->lio = NULL;
598
599 cn++;
600 if (aiocbp_ptr)
601 break;
602 }
603
604 /* There are canceled jobs */
605 if (cn)
606 *retval = AIO_CANCELED;
607
608 /* We cannot cancel current job */
609 a_job = aio->curjob;
610 if (a_job && ((a_job->aiocbp.aio_fildes == fildes) ||
611 (a_job->aiocb_uptr == aiocbp_ptr)))
612 *retval = AIO_NOTCANCELED;
613
614 mutex_exit(&aio->aio_mtx);
615
616 /* Free the jobs after the lock */
617 errcnt = 0;
618 while (!TAILQ_EMPTY(&tmp_jobs_list)) {
619 a_job = TAILQ_FIRST(&tmp_jobs_list);
620 TAILQ_REMOVE(&tmp_jobs_list, a_job, list);
621 /* Set the errno and copy structures back to the user-space */
622 a_job->aiocbp._errno = ECANCELED;
623 a_job->aiocbp._state = JOB_DONE;
624 if (copyout(&a_job->aiocbp, a_job->aiocb_uptr,
625 sizeof(struct aiocb)))
626 errcnt++;
627 /* Send a signal if any */
628 aio_sendsig(p, &a_job->aiocbp.aio_sigevent);
629 if (a_job->lio) {
630 lio = a_job->lio;
631 aio_sendsig(p, &lio->sig);
632 pool_put(&aio_lio_pool, lio);
633 }
634 pool_put(&aio_job_pool, a_job);
635 }
636
637 if (errcnt)
638 return EFAULT;
639
640 /* Set a correct return value */
641 if (*retval == 0)
642 *retval = AIO_ALLDONE;
643
644 return 0;
645 }
646
647 int
648 sys_aio_error(struct lwp *l, void *v, register_t *retval)
649 {
650 struct sys_aio_error_args /* {
651 syscallarg(const struct aiocb *) aiocbp;
652 } */ *uap = v;
653 struct proc *p = l->l_proc;
654 struct aioproc *aio = p->p_aio;
655 struct aiocb aiocbp;
656 int error;
657
658 if (aio == NULL)
659 return EINVAL;
660
661 error = copyin(SCARG(uap, aiocbp), &aiocbp, sizeof(struct aiocb));
662 if (error)
663 return error;
664
665 if (aiocbp._state == JOB_NONE)
666 return EINVAL;
667
668 *retval = aiocbp._errno;
669
670 return 0;
671 }
672
673 int
674 sys_aio_fsync(struct lwp *l, void *v, register_t *retval)
675 {
676 struct sys_aio_fsync_args /* {
677 syscallarg(int) op;
678 syscallarg(struct aiocb *) aiocbp;
679 } */ *uap = v;
680 int op = SCARG(uap, op);
681
682 if ((op != O_DSYNC) && (op != O_SYNC))
683 return EINVAL;
684
685 op = O_DSYNC ? AIO_DSYNC : AIO_SYNC;
686
687 return aio_enqueue_job(op, SCARG(uap, aiocbp), NULL);
688 }
689
690 int
691 sys_aio_read(struct lwp *l, void *v, register_t *retval)
692 {
693 struct sys_aio_read_args /* {
694 syscallarg(struct aiocb *) aiocbp;
695 } */ *uap = v;
696
697 return aio_enqueue_job(AIO_READ, SCARG(uap, aiocbp), NULL);
698 }
699
700 int
701 sys_aio_return(struct lwp *l, void *v, register_t *retval)
702 {
703 struct sys_aio_return_args /* {
704 syscallarg(struct aiocb *) aiocbp;
705 } */ *uap = v;
706 struct proc *p = l->l_proc;
707 struct aioproc *aio = p->p_aio;
708 struct aiocb aiocbp;
709 int error;
710
711 if (aio == NULL)
712 return EINVAL;
713
714 error = copyin(SCARG(uap, aiocbp), &aiocbp, sizeof(struct aiocb));
715 if (error)
716 return error;
717
718 if (aiocbp._errno == EINPROGRESS || aiocbp._state != JOB_DONE)
719 return EINVAL;
720
721 *retval = aiocbp._retval;
722
723 /* Reset the internal variables */
724 aiocbp._errno = 0;
725 aiocbp._retval = -1;
726 aiocbp._state = JOB_NONE;
727 error = copyout(&aiocbp, SCARG(uap, aiocbp), sizeof(struct aiocb));
728
729 return error;
730 }
731
732 int
733 sys_aio_suspend(struct lwp *l, void *v, register_t *retval)
734 {
735 struct sys_aio_suspend_args /* {
736 syscallarg(const struct aiocb *const[]) list;
737 syscallarg(int) nent;
738 syscallarg(const struct timespec *) timeout;
739 } */ *uap = v;
740 struct proc *p = l->l_proc;
741 struct aioproc *aio;
742 struct aio_job *a_job;
743 struct aiocb **aiocbp_list;
744 struct timespec ts;
745 int i, error, nent, timo;
746
747 if (p->p_aio == NULL)
748 return EAGAIN;
749 aio = p->p_aio;
750
751 nent = SCARG(uap, nent);
752 if (nent <= 0 || nent > aio_listio_max)
753 return EAGAIN;
754
755 if (SCARG(uap, timeout)) {
756 /* Convert timespec to ticks */
757 error = copyin(SCARG(uap, timeout), &ts,
758 sizeof(struct timespec));
759 if (error)
760 return error;
761 timo = mstohz((ts.tv_sec * 1000) + (ts.tv_nsec / 1000000));
762 if (timo == 0 && ts.tv_sec == 0 && ts.tv_nsec > 0)
763 timo = 1;
764 if (timo <= 0)
765 return EAGAIN;
766 } else
767 timo = 0;
768
769 /* Get the list from user-space */
770 aiocbp_list = kmem_zalloc(nent * sizeof(struct aio_job), KM_SLEEP);
771 error = copyin(SCARG(uap, list), aiocbp_list,
772 nent * sizeof(struct aiocb));
773 if (error) {
774 kmem_free(aiocbp_list, nent * sizeof(struct aio_job));
775 return error;
776 }
777
778 mutex_enter(&aio->aio_mtx);
779 for (;;) {
780
781 for (i = 0; i < nent; i++) {
782
783 /* Skip NULL entries */
784 if (aiocbp_list[i] == NULL)
785 continue;
786
787 /* Skip current job */
788 if (aio->curjob) {
789 a_job = aio->curjob;
790 if (a_job->aiocb_uptr == aiocbp_list[i])
791 continue;
792 }
793
794 /* Look for a job in the queue */
795 TAILQ_FOREACH(a_job, &aio->jobs_queue, list)
796 if (a_job->aiocb_uptr == aiocbp_list[i])
797 break;
798
799 if (a_job == NULL) {
800 struct aiocb aiocbp;
801
802 mutex_exit(&aio->aio_mtx);
803
804 error = copyin(aiocbp_list[i], &aiocbp,
805 sizeof(struct aiocb));
806 if (error == 0 && aiocbp._state != JOB_DONE) {
807 mutex_enter(&aio->aio_mtx);
808 continue;
809 }
810
811 kmem_free(aiocbp_list,
812 nent * sizeof(struct aio_job));
813 return error;
814 }
815 }
816
817 /* Wait for a signal or when timeout occurs */
818 error = cv_timedwait_sig(&aio->done_cv, &aio->aio_mtx, timo);
819 if (error) {
820 if (error == EWOULDBLOCK)
821 error = EAGAIN;
822 break;
823 }
824 }
825 mutex_exit(&aio->aio_mtx);
826
827 kmem_free(aiocbp_list, nent * sizeof(struct aio_job));
828 return error;
829 }
830
831 int
832 sys_aio_write(struct lwp *l, void *v, register_t *retval)
833 {
834 struct sys_aio_write_args /* {
835 syscallarg(struct aiocb *) aiocbp;
836 } */ *uap = v;
837
838 return aio_enqueue_job(AIO_WRITE, SCARG(uap, aiocbp), NULL);
839 }
840
841 int
842 sys_lio_listio(struct lwp *l, void *v, register_t *retval)
843 {
844 struct sys_lio_listio_args /* {
845 syscallarg(int) mode;
846 syscallarg(struct aiocb *const[]) list;
847 syscallarg(int) nent;
848 syscallarg(struct sigevent *) sig;
849 } */ *uap = v;
850 struct proc *p = l->l_proc;
851 struct aioproc *aio;
852 struct aiocb **aiocbp_list;
853 struct lio_req *lio;
854 int i, error, errcnt, mode, nent;
855
856 mode = SCARG(uap, mode);
857 nent = SCARG(uap, nent);
858
859 /* Non-accurate checks for the limit and invalid values */
860 if (nent < 1 || nent > aio_listio_max)
861 return EINVAL;
862 if (aio_jobs_count + nent > aio_max)
863 return EAGAIN;
864
865 /* Check if AIO structure is initialized, if not - initialize it */
866 if (p->p_aio == NULL)
867 if (aio_init(p))
868 return EAGAIN;
869 aio = p->p_aio;
870
871 /* Create a LIO structure */
872 lio = pool_get(&aio_lio_pool, PR_WAITOK);
873 lio->refcnt = 1;
874 error = 0;
875
876 switch (mode) {
877 case LIO_WAIT:
878 memset(&lio->sig, 0, sizeof(struct sigevent));
879 break;
880 case LIO_NOWAIT:
881 /* Check for signal, validate it */
882 if (SCARG(uap, sig)) {
883 struct sigevent *sig = &lio->sig;
884
885 error = copyin(SCARG(uap, sig), &lio->sig,
886 sizeof(struct sigevent));
887 if (error == 0 &&
888 (sig->sigev_signo < 0 ||
889 sig->sigev_signo >= NSIG ||
890 sig->sigev_notify < SIGEV_NONE ||
891 sig->sigev_notify > SIGEV_SA))
892 error = EINVAL;
893 } else
894 memset(&lio->sig, 0, sizeof(struct sigevent));
895 break;
896 default:
897 error = EINVAL;
898 break;
899 }
900
901 if (error != 0) {
902 pool_put(&aio_lio_pool, lio);
903 return error;
904 }
905
906 /* Get the list from user-space */
907 aiocbp_list = kmem_zalloc(nent * sizeof(struct aio_job), KM_SLEEP);
908 error = copyin(SCARG(uap, list), aiocbp_list,
909 nent * sizeof(struct aiocb));
910 if (error) {
911 mutex_enter(&aio->aio_mtx);
912 goto err;
913 }
914
915 /* Enqueue all jobs */
916 errcnt = 0;
917 for (i = 0; i < nent; i++) {
918 error = aio_enqueue_job(AIO_LIO, aiocbp_list[i], lio);
919 /*
920 * According to POSIX, in such error case it may
921 * fail with other I/O operations initiated.
922 */
923 if (error)
924 errcnt++;
925 }
926
927 mutex_enter(&aio->aio_mtx);
928
929 /* Return an error, if any */
930 if (errcnt) {
931 error = EIO;
932 goto err;
933 }
934
935 if (mode == LIO_WAIT) {
936 /*
937 * Wait for AIO completion. In such case,
938 * the LIO structure will be freed here.
939 */
940 while (lio->refcnt > 1 && error == 0)
941 error = cv_wait_sig(&aio->done_cv, &aio->aio_mtx);
942 if (error)
943 error = EINTR;
944 }
945
946 err:
947 if (--lio->refcnt != 0)
948 lio = NULL;
949 mutex_exit(&aio->aio_mtx);
950 if (lio != NULL) {
951 aio_sendsig(p, &lio->sig);
952 pool_put(&aio_lio_pool, lio);
953 }
954 kmem_free(aiocbp_list, nent * sizeof(struct aio_job));
955 return error;
956 }
957
958 /*
959 * SysCtl
960 */
961
962 static int
963 sysctl_aio_listio_max(SYSCTLFN_ARGS)
964 {
965 struct sysctlnode node;
966 int error, newsize;
967
968 node = *rnode;
969 node.sysctl_data = &newsize;
970
971 newsize = aio_listio_max;
972 error = sysctl_lookup(SYSCTLFN_CALL(&node));
973 if (error || newp == NULL)
974 return error;
975
976 if (newsize < 1 || newsize > aio_max)
977 return EINVAL;
978 aio_listio_max = newsize;
979
980 return 0;
981 }
982
983 static int
984 sysctl_aio_max(SYSCTLFN_ARGS)
985 {
986 struct sysctlnode node;
987 int error, newsize;
988
989 node = *rnode;
990 node.sysctl_data = &newsize;
991
992 newsize = aio_max;
993 error = sysctl_lookup(SYSCTLFN_CALL(&node));
994 if (error || newp == NULL)
995 return error;
996
997 if (newsize < 1 || newsize < aio_listio_max)
998 return EINVAL;
999 aio_max = newsize;
1000
1001 return 0;
1002 }
1003
1004 SYSCTL_SETUP(sysctl_aio_setup, "sysctl aio setup")
1005 {
1006
1007 sysctl_createv(clog, 0, NULL, NULL,
1008 CTLFLAG_PERMANENT,
1009 CTLTYPE_NODE, "kern", NULL,
1010 NULL, 0, NULL, 0,
1011 CTL_KERN, CTL_EOL);
1012 sysctl_createv(clog, 0, NULL, NULL,
1013 CTLFLAG_PERMANENT | CTLFLAG_IMMEDIATE,
1014 CTLTYPE_INT, "posix_aio",
1015 SYSCTL_DESCR("Version of IEEE Std 1003.1 and its "
1016 "Asynchronous I/O option to which the "
1017 "system attempts to conform"),
1018 NULL, _POSIX_ASYNCHRONOUS_IO, NULL, 0,
1019 CTL_KERN, CTL_CREATE, CTL_EOL);
1020 sysctl_createv(clog, 0, NULL, NULL,
1021 CTLFLAG_PERMANENT | CTLFLAG_READWRITE,
1022 CTLTYPE_INT, "aio_listio_max",
1023 SYSCTL_DESCR("Maximum number of asynchronous I/O "
1024 "operations in a single list I/O call"),
1025 sysctl_aio_listio_max, 0, &aio_listio_max, 0,
1026 CTL_KERN, CTL_CREATE, CTL_EOL);
1027 sysctl_createv(clog, 0, NULL, NULL,
1028 CTLFLAG_PERMANENT | CTLFLAG_READWRITE,
1029 CTLTYPE_INT, "aio_max",
1030 SYSCTL_DESCR("Maximum number of asynchronous I/O "
1031 "operations"),
1032 sysctl_aio_max, 0, &aio_max, 0,
1033 CTL_KERN, CTL_CREATE, CTL_EOL);
1034 }
1035
1036 /*
1037 * Debugging
1038 */
1039 #if defined(DDB)
1040 void
1041 aio_print_jobs(void (*pr)(const char *, ...))
1042 {
1043 struct proc *p = (curlwp == NULL ? NULL : curlwp->l_proc);
1044 struct aioproc *aio;
1045 struct aio_job *a_job;
1046 struct aiocb *aiocbp;
1047
1048 if (p == NULL) {
1049 (*pr)("AIO: We are not in the processes right now.\n");
1050 return;
1051 }
1052
1053 aio = p->p_aio;
1054 if (aio == NULL) {
1055 (*pr)("AIO data is not initialized (PID = %d).\n", p->p_pid);
1056 return;
1057 }
1058
1059 (*pr)("AIO: PID = %d\n", p->p_pid);
1060 (*pr)("AIO: Global count of the jobs = %u\n", aio_jobs_count);
1061 (*pr)("AIO: Count of the jobs = %u\n", aio->jobs_count);
1062
1063 if (aio->curjob) {
1064 a_job = aio->curjob;
1065 (*pr)("\nAIO current job:\n");
1066 (*pr)(" opcode = %d, errno = %d, state = %d, aiocb_ptr = %p\n",
1067 a_job->aio_op, a_job->aiocbp._errno,
1068 a_job->aiocbp._state, a_job->aiocb_uptr);
1069 aiocbp = &a_job->aiocbp;
1070 (*pr)(" fd = %d, offset = %u, buf = %p, nbytes = %u\n",
1071 aiocbp->aio_fildes, aiocbp->aio_offset,
1072 aiocbp->aio_buf, aiocbp->aio_nbytes);
1073 }
1074
1075 (*pr)("\nAIO queue:\n");
1076 TAILQ_FOREACH(a_job, &aio->jobs_queue, list) {
1077 (*pr)(" opcode = %d, errno = %d, state = %d, aiocb_ptr = %p\n",
1078 a_job->aio_op, a_job->aiocbp._errno,
1079 a_job->aiocbp._state, a_job->aiocb_uptr);
1080 aiocbp = &a_job->aiocbp;
1081 (*pr)(" fd = %d, offset = %u, buf = %p, nbytes = %u\n",
1082 aiocbp->aio_fildes, aiocbp->aio_offset,
1083 aiocbp->aio_buf, aiocbp->aio_nbytes);
1084 }
1085 }
1086 #endif /* defined(DDB) */
1087