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