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