2 * Copyright (c) 1997 John S. Dyson. All rights reserved.
4 * Redistribution and use in source and binary forms, with or without
5 * modification, are permitted provided that the following conditions
7 * 1. Redistributions of source code must retain the above copyright
8 * notice, this list of conditions and the following disclaimer.
9 * 2. John S. Dyson's name may not be used to endorse or promote products
10 * derived from this software without specific prior written permission.
12 * DISCLAIMER: This code isn't warranted to do anything useful. Anything
13 * bad that happens because of using this software isn't the responsibility
14 * of the author. This software is distributed AS-IS.
16 * $FreeBSD: src/sys/kern/vfs_aio.c,v 1.70.2.28 2003/05/29 06:15:35 alc Exp $
17 * $DragonFly: src/sys/kern/vfs_aio.c,v 1.33 2006/12/23 23:47:54 swildner Exp $
21 * This file contains support for the POSIX 1003.1B AIO/LIO facility.
24 #include <sys/param.h>
25 #include <sys/systm.h>
27 #include <sys/sysproto.h>
28 #include <sys/filedesc.h>
29 #include <sys/kernel.h>
30 #include <sys/fcntl.h>
33 #include <sys/unistd.h>
35 #include <sys/resourcevar.h>
36 #include <sys/signalvar.h>
37 #include <sys/protosw.h>
38 #include <sys/socketvar.h>
39 #include <sys/sysctl.h>
40 #include <sys/vnode.h>
42 #include <sys/event.h>
45 #include <vm/vm_extern.h>
47 #include <vm/vm_map.h>
48 #include <vm/vm_zone.h>
50 #include <sys/file2.h>
52 #include <sys/thread2.h>
54 #include <machine/limits.h>
55 #include "opt_vfs_aio.h"
60 * Counter for allocating reference ids to new jobs. Wrapped to 1 on
65 #define JOBST_NULL 0x0
66 #define JOBST_JOBQGLOBAL 0x2
67 #define JOBST_JOBRUNNING 0x3
68 #define JOBST_JOBFINISHED 0x4
69 #define JOBST_JOBQBUF 0x5
70 #define JOBST_JOBBFINISHED 0x6
72 #ifndef MAX_AIO_PER_PROC
73 #define MAX_AIO_PER_PROC 32
76 #ifndef MAX_AIO_QUEUE_PER_PROC
77 #define MAX_AIO_QUEUE_PER_PROC 256 /* Bigger than AIO_LISTIO_MAX */
81 #define MAX_AIO_PROCS 32
85 #define MAX_AIO_QUEUE 1024 /* Bigger than AIO_LISTIO_MAX */
88 #ifndef TARGET_AIO_PROCS
89 #define TARGET_AIO_PROCS 4
93 #define MAX_BUF_AIO 16
96 #ifndef AIOD_TIMEOUT_DEFAULT
97 #define AIOD_TIMEOUT_DEFAULT (10 * hz)
100 #ifndef AIOD_LIFETIME_DEFAULT
101 #define AIOD_LIFETIME_DEFAULT (30 * hz)
104 SYSCTL_NODE(_vfs, OID_AUTO, aio, CTLFLAG_RW, 0, "Async IO management");
106 static int max_aio_procs = MAX_AIO_PROCS;
107 SYSCTL_INT(_vfs_aio, OID_AUTO, max_aio_procs,
108 CTLFLAG_RW, &max_aio_procs, 0,
109 "Maximum number of kernel threads to use for handling async IO");
111 static int num_aio_procs = 0;
112 SYSCTL_INT(_vfs_aio, OID_AUTO, num_aio_procs,
113 CTLFLAG_RD, &num_aio_procs, 0,
114 "Number of presently active kernel threads for async IO");
117 * The code will adjust the actual number of AIO processes towards this
118 * number when it gets a chance.
120 static int target_aio_procs = TARGET_AIO_PROCS;
121 SYSCTL_INT(_vfs_aio, OID_AUTO, target_aio_procs, CTLFLAG_RW, &target_aio_procs,
122 0, "Preferred number of ready kernel threads for async IO");
124 static int max_queue_count = MAX_AIO_QUEUE;
125 SYSCTL_INT(_vfs_aio, OID_AUTO, max_aio_queue, CTLFLAG_RW, &max_queue_count, 0,
126 "Maximum number of aio requests to queue, globally");
128 static int num_queue_count = 0;
129 SYSCTL_INT(_vfs_aio, OID_AUTO, num_queue_count, CTLFLAG_RD, &num_queue_count, 0,
130 "Number of queued aio requests");
132 static int num_buf_aio = 0;
133 SYSCTL_INT(_vfs_aio, OID_AUTO, num_buf_aio, CTLFLAG_RD, &num_buf_aio, 0,
134 "Number of aio requests presently handled by the buf subsystem");
136 /* Number of async I/O thread in the process of being started */
137 /* XXX This should be local to _aio_aqueue() */
138 static int num_aio_resv_start = 0;
140 static int aiod_timeout;
141 SYSCTL_INT(_vfs_aio, OID_AUTO, aiod_timeout, CTLFLAG_RW, &aiod_timeout, 0,
142 "Timeout value for synchronous aio operations");
144 static int aiod_lifetime;
145 SYSCTL_INT(_vfs_aio, OID_AUTO, aiod_lifetime, CTLFLAG_RW, &aiod_lifetime, 0,
146 "Maximum lifetime for idle aiod");
148 static int max_aio_per_proc = MAX_AIO_PER_PROC;
149 SYSCTL_INT(_vfs_aio, OID_AUTO, max_aio_per_proc, CTLFLAG_RW, &max_aio_per_proc,
150 0, "Maximum active aio requests per process (stored in the process)");
152 static int max_aio_queue_per_proc = MAX_AIO_QUEUE_PER_PROC;
153 SYSCTL_INT(_vfs_aio, OID_AUTO, max_aio_queue_per_proc, CTLFLAG_RW,
154 &max_aio_queue_per_proc, 0,
155 "Maximum queued aio requests per process (stored in the process)");
157 static int max_buf_aio = MAX_BUF_AIO;
158 SYSCTL_INT(_vfs_aio, OID_AUTO, max_buf_aio, CTLFLAG_RW, &max_buf_aio, 0,
159 "Maximum buf aio requests per process (stored in the process)");
164 #define AIOP_FREE 0x1 /* proc on free queue */
165 #define AIOP_SCHED 0x2 /* proc explicitly scheduled */
168 int aioprocflags; /* AIO proc flags */
169 TAILQ_ENTRY(aioproclist) list; /* List of processes */
170 struct proc *aioproc; /* The AIO thread */
174 * data-structure for lio signal management
178 int lioj_buffer_count;
179 int lioj_buffer_finished_count;
180 int lioj_queue_count;
181 int lioj_queue_finished_count;
182 struct sigevent lioj_signal; /* signal on all I/O done */
183 TAILQ_ENTRY(aio_liojob) lioj_list;
184 struct kaioinfo *lioj_ki;
186 #define LIOJ_SIGNAL 0x1 /* signal on all done (lio) */
187 #define LIOJ_SIGNAL_POSTED 0x2 /* signal has been posted */
190 * per process aio data structure
193 int kaio_flags; /* per process kaio flags */
194 int kaio_maxactive_count; /* maximum number of AIOs */
195 int kaio_active_count; /* number of currently used AIOs */
196 int kaio_qallowed_count; /* maxiumu size of AIO queue */
197 int kaio_queue_count; /* size of AIO queue */
198 int kaio_ballowed_count; /* maximum number of buffers */
199 int kaio_queue_finished_count; /* number of daemon jobs finished */
200 int kaio_buffer_count; /* number of physio buffers */
201 int kaio_buffer_finished_count; /* count of I/O done */
202 struct proc *kaio_p; /* process that uses this kaio block */
203 TAILQ_HEAD(,aio_liojob) kaio_liojoblist; /* list of lio jobs */
204 TAILQ_HEAD(,aiocblist) kaio_jobqueue; /* job queue for process */
205 TAILQ_HEAD(,aiocblist) kaio_jobdone; /* done queue for process */
206 TAILQ_HEAD(,aiocblist) kaio_bufqueue; /* buffer job queue for process */
207 TAILQ_HEAD(,aiocblist) kaio_bufdone; /* buffer done queue for process */
208 TAILQ_HEAD(,aiocblist) kaio_sockqueue; /* queue for aios waiting on sockets */
211 #define KAIO_RUNDOWN 0x1 /* process is being run down */
212 #define KAIO_WAKEUP 0x2 /* wakeup process when there is a significant event */
214 static TAILQ_HEAD(,aioproclist) aio_freeproc, aio_activeproc;
215 static TAILQ_HEAD(,aiocblist) aio_jobs; /* Async job list */
216 static TAILQ_HEAD(,aiocblist) aio_bufjobs; /* Phys I/O job list */
217 static TAILQ_HEAD(,aiocblist) aio_freejobs; /* Pool of free jobs */
219 static void aio_init_aioinfo(struct proc *p);
220 static void aio_onceonly(void *);
221 static int aio_free_entry(struct aiocblist *aiocbe);
222 static void aio_process(struct aiocblist *aiocbe);
223 static int aio_newproc(void);
224 static int aio_aqueue(struct aiocb *job, int type);
225 static void aio_physwakeup(struct bio *bio);
226 static int aio_fphysio(struct aiocblist *aiocbe);
227 static int aio_qphysio(struct proc *p, struct aiocblist *iocb);
228 static void aio_daemon(void *uproc);
229 static void process_signal(void *aioj);
231 SYSINIT(aio, SI_SUB_VFS, SI_ORDER_ANY, aio_onceonly, NULL);
235 * kaio Per process async io info
236 * aiop async io thread data
237 * aiocb async io jobs
238 * aiol list io job pointer - internal to aio_suspend XXX
239 * aiolio list io jobs
241 static vm_zone_t kaio_zone, aiop_zone, aiocb_zone, aiol_zone, aiolio_zone;
244 * Startup initialization
247 aio_onceonly(void *na)
249 TAILQ_INIT(&aio_freeproc);
250 TAILQ_INIT(&aio_activeproc);
251 TAILQ_INIT(&aio_jobs);
252 TAILQ_INIT(&aio_bufjobs);
253 TAILQ_INIT(&aio_freejobs);
254 kaio_zone = zinit("AIO", sizeof(struct kaioinfo), 0, 0, 1);
255 aiop_zone = zinit("AIOP", sizeof(struct aioproclist), 0, 0, 1);
256 aiocb_zone = zinit("AIOCB", sizeof(struct aiocblist), 0, 0, 1);
257 aiol_zone = zinit("AIOL", AIO_LISTIO_MAX*sizeof(intptr_t), 0, 0, 1);
258 aiolio_zone = zinit("AIOLIO", sizeof(struct aio_liojob), 0, 0, 1);
259 aiod_timeout = AIOD_TIMEOUT_DEFAULT;
260 aiod_lifetime = AIOD_LIFETIME_DEFAULT;
265 * Init the per-process aioinfo structure. The aioinfo limits are set
266 * per-process for user limit (resource) management.
269 aio_init_aioinfo(struct proc *p)
272 if (p->p_aioinfo == NULL) {
273 ki = zalloc(kaio_zone);
276 ki->kaio_maxactive_count = max_aio_per_proc;
277 ki->kaio_active_count = 0;
278 ki->kaio_qallowed_count = max_aio_queue_per_proc;
279 ki->kaio_queue_count = 0;
280 ki->kaio_ballowed_count = max_buf_aio;
281 ki->kaio_buffer_count = 0;
282 ki->kaio_buffer_finished_count = 0;
284 TAILQ_INIT(&ki->kaio_jobdone);
285 TAILQ_INIT(&ki->kaio_jobqueue);
286 TAILQ_INIT(&ki->kaio_bufdone);
287 TAILQ_INIT(&ki->kaio_bufqueue);
288 TAILQ_INIT(&ki->kaio_liojoblist);
289 TAILQ_INIT(&ki->kaio_sockqueue);
292 while (num_aio_procs < target_aio_procs)
297 * Free a job entry. Wait for completion if it is currently active, but don't
298 * delay forever. If we delay, we return a flag that says that we have to
299 * restart the queue scan.
302 aio_free_entry(struct aiocblist *aiocbe)
305 struct aio_liojob *lj;
309 if (aiocbe->jobstate == JOBST_NULL)
310 panic("aio_free_entry: freeing already free job");
312 p = aiocbe->userproc;
316 panic("aio_free_entry: missing p->p_aioinfo");
318 while (aiocbe->jobstate == JOBST_JOBRUNNING) {
319 aiocbe->jobflags |= AIOCBLIST_RUNDOWN;
320 tsleep(aiocbe, 0, "jobwai", 0);
322 if (aiocbe->bp == NULL) {
323 if (ki->kaio_queue_count <= 0)
324 panic("aio_free_entry: process queue size <= 0");
325 if (num_queue_count <= 0)
326 panic("aio_free_entry: system wide queue size <= 0");
329 lj->lioj_queue_count--;
330 if (aiocbe->jobflags & AIOCBLIST_DONE)
331 lj->lioj_queue_finished_count--;
333 ki->kaio_queue_count--;
334 if (aiocbe->jobflags & AIOCBLIST_DONE)
335 ki->kaio_queue_finished_count--;
339 lj->lioj_buffer_count--;
340 if (aiocbe->jobflags & AIOCBLIST_DONE)
341 lj->lioj_buffer_finished_count--;
343 if (aiocbe->jobflags & AIOCBLIST_DONE)
344 ki->kaio_buffer_finished_count--;
345 ki->kaio_buffer_count--;
349 /* aiocbe is going away, we need to destroy any knotes */
350 knote_remove(p->p_thread, &aiocbe->klist);
352 if ((ki->kaio_flags & KAIO_WAKEUP) || ((ki->kaio_flags & KAIO_RUNDOWN)
353 && ((ki->kaio_buffer_count == 0) && (ki->kaio_queue_count == 0)))) {
354 ki->kaio_flags &= ~KAIO_WAKEUP;
358 if (aiocbe->jobstate == JOBST_JOBQBUF) {
359 if ((error = aio_fphysio(aiocbe)) != 0)
361 if (aiocbe->jobstate != JOBST_JOBBFINISHED)
362 panic("aio_free_entry: invalid physio finish-up state");
364 TAILQ_REMOVE(&ki->kaio_bufdone, aiocbe, plist);
366 } else if (aiocbe->jobstate == JOBST_JOBQGLOBAL) {
368 TAILQ_REMOVE(&aio_jobs, aiocbe, list);
369 TAILQ_REMOVE(&ki->kaio_jobqueue, aiocbe, plist);
371 } else if (aiocbe->jobstate == JOBST_JOBFINISHED)
372 TAILQ_REMOVE(&ki->kaio_jobdone, aiocbe, plist);
373 else if (aiocbe->jobstate == JOBST_JOBBFINISHED) {
375 TAILQ_REMOVE(&ki->kaio_bufdone, aiocbe, plist);
378 vunmapbuf(aiocbe->bp);
379 relpbuf(aiocbe->bp, NULL);
383 if (lj && (lj->lioj_buffer_count == 0) && (lj->lioj_queue_count == 0)) {
384 TAILQ_REMOVE(&ki->kaio_liojoblist, lj, lioj_list);
385 zfree(aiolio_zone, lj);
387 aiocbe->jobstate = JOBST_NULL;
388 callout_stop(&aiocbe->timeout);
389 fdrop(aiocbe->fd_file);
390 TAILQ_INSERT_HEAD(&aio_freejobs, aiocbe, list);
396 * Rundown the jobs for a given process.
399 aio_proc_rundown(struct proc *p)
405 struct aio_liojob *lj, *ljn;
406 struct aiocblist *aiocbe, *aiocbn;
414 ki->kaio_flags |= LIOJ_SIGNAL_POSTED;
415 while ((ki->kaio_active_count > 0) || (ki->kaio_buffer_count >
416 ki->kaio_buffer_finished_count)) {
417 ki->kaio_flags |= KAIO_RUNDOWN;
418 if (tsleep(p, 0, "kaiowt", aiod_timeout))
423 * Move any aio ops that are waiting on socket I/O to the normal job
424 * queues so they are cleaned up with any others.
427 for (aiocbe = TAILQ_FIRST(&ki->kaio_sockqueue); aiocbe; aiocbe =
429 aiocbn = TAILQ_NEXT(aiocbe, plist);
430 fp = aiocbe->fd_file;
432 so = (struct socket *)fp->f_data;
433 TAILQ_REMOVE(&so->so_aiojobq, aiocbe, list);
434 if (TAILQ_EMPTY(&so->so_aiojobq)) {
435 so->so_snd.sb_flags &= ~SB_AIO;
436 so->so_rcv.sb_flags &= ~SB_AIO;
439 TAILQ_REMOVE(&ki->kaio_sockqueue, aiocbe, plist);
440 TAILQ_INSERT_HEAD(&aio_jobs, aiocbe, list);
441 TAILQ_INSERT_HEAD(&ki->kaio_jobqueue, aiocbe, plist);
446 for (aiocbe = TAILQ_FIRST(&ki->kaio_jobdone); aiocbe; aiocbe = aiocbn) {
447 aiocbn = TAILQ_NEXT(aiocbe, plist);
448 if (aio_free_entry(aiocbe))
453 for (aiocbe = TAILQ_FIRST(&ki->kaio_jobqueue); aiocbe; aiocbe =
455 aiocbn = TAILQ_NEXT(aiocbe, plist);
456 if (aio_free_entry(aiocbe))
462 while (TAILQ_FIRST(&ki->kaio_bufqueue)) {
463 ki->kaio_flags |= KAIO_WAKEUP;
464 tsleep(p, 0, "aioprn", 0);
472 for (aiocbe = TAILQ_FIRST(&ki->kaio_bufdone); aiocbe; aiocbe = aiocbn) {
473 aiocbn = TAILQ_NEXT(aiocbe, plist);
474 if (aio_free_entry(aiocbe)) {
482 * If we've slept, jobs might have moved from one queue to another.
483 * Retry rundown if we didn't manage to empty the queues.
485 if (TAILQ_FIRST(&ki->kaio_jobdone) != NULL ||
486 TAILQ_FIRST(&ki->kaio_jobqueue) != NULL ||
487 TAILQ_FIRST(&ki->kaio_bufqueue) != NULL ||
488 TAILQ_FIRST(&ki->kaio_bufdone) != NULL)
491 for (lj = TAILQ_FIRST(&ki->kaio_liojoblist); lj; lj = ljn) {
492 ljn = TAILQ_NEXT(lj, lioj_list);
493 if ((lj->lioj_buffer_count == 0) && (lj->lioj_queue_count ==
495 TAILQ_REMOVE(&ki->kaio_liojoblist, lj, lioj_list);
496 zfree(aiolio_zone, lj);
499 kprintf("LIO job not cleaned up: B:%d, BF:%d, Q:%d, "
500 "QF:%d\n", lj->lioj_buffer_count,
501 lj->lioj_buffer_finished_count,
502 lj->lioj_queue_count,
503 lj->lioj_queue_finished_count);
508 zfree(kaio_zone, ki);
515 * Select a job to run (called by an AIO daemon).
517 static struct aiocblist *
518 aio_selectjob(struct aioproclist *aiop)
520 struct aiocblist *aiocbe;
525 for (aiocbe = TAILQ_FIRST(&aio_jobs); aiocbe; aiocbe =
526 TAILQ_NEXT(aiocbe, list)) {
527 userp = aiocbe->userproc;
528 ki = userp->p_aioinfo;
530 if (ki->kaio_active_count < ki->kaio_maxactive_count) {
531 TAILQ_REMOVE(&aio_jobs, aiocbe, list);
542 * The AIO processing activity. This is the code that does the I/O request for
543 * the non-physio version of the operations. The normal vn operations are used,
544 * and this code should work in all instances for every type of file, including
545 * pipes, sockets, fifos, and regular files.
548 aio_process(struct aiocblist *aiocbe)
557 int oublock_st, oublock_end;
558 int inblock_st, inblock_end;
561 cb = &aiocbe->uaiocb;
562 fp = aiocbe->fd_file;
564 aiov.iov_base = (void *)(uintptr_t)cb->aio_buf;
565 aiov.iov_len = cb->aio_nbytes;
567 auio.uio_iov = &aiov;
569 auio.uio_offset = cb->aio_offset;
570 auio.uio_resid = cb->aio_nbytes;
571 cnt = cb->aio_nbytes;
572 auio.uio_segflg = UIO_USERSPACE;
575 inblock_st = mytd->td_proc->p_stats->p_ru.ru_inblock;
576 oublock_st = mytd->td_proc->p_stats->p_ru.ru_oublock;
578 * _aio_aqueue() acquires a reference to the file that is
579 * released in aio_free_entry().
581 if (cb->aio_lio_opcode == LIO_READ) {
582 auio.uio_rw = UIO_READ;
583 error = fo_read(fp, &auio, fp->f_cred, O_FOFFSET);
585 auio.uio_rw = UIO_WRITE;
586 error = fo_write(fp, &auio, fp->f_cred, O_FOFFSET);
588 inblock_end = mytd->td_proc->p_stats->p_ru.ru_inblock;
589 oublock_end = mytd->td_proc->p_stats->p_ru.ru_oublock;
591 aiocbe->inputcharge = inblock_end - inblock_st;
592 aiocbe->outputcharge = oublock_end - oublock_st;
594 if ((error) && (auio.uio_resid != cnt)) {
595 if (error == ERESTART || error == EINTR || error == EWOULDBLOCK)
597 if ((error == EPIPE) && (cb->aio_lio_opcode == LIO_WRITE))
598 ksignal(aiocbe->userproc, SIGPIPE);
601 cnt -= auio.uio_resid;
602 cb->_aiocb_private.error = error;
603 cb->_aiocb_private.status = cnt;
607 * The AIO daemon, most of the actual work is done in aio_process,
608 * but the setup (and address space mgmt) is done in this routine.
610 * The MP lock is held on entry.
613 aio_daemon(void *uproc)
615 struct aio_liojob *lj;
617 struct aiocblist *aiocbe;
618 struct aioproclist *aiop;
620 struct proc *curcp, *mycp, *userp;
621 struct vmspace *myvm, *tmpvm;
625 * Local copies of curproc (cp) and vmspace (myvm)
628 myvm = mycp->p_vmspace;
630 if (mycp->p_textvp) {
631 vrele(mycp->p_textvp);
632 mycp->p_textvp = NULL;
636 * Allocate and ready the aio control info. There is one aiop structure
639 aiop = zalloc(aiop_zone);
640 aiop->aioproc = mycp;
641 aiop->aioprocflags |= AIOP_FREE;
646 * Place thread (lightweight process) onto the AIO free thread list.
648 if (TAILQ_EMPTY(&aio_freeproc))
649 wakeup(&aio_freeproc);
650 TAILQ_INSERT_HEAD(&aio_freeproc, aiop, list);
654 /* Make up a name for the daemon. */
655 strcpy(mycp->p_comm, "aiod");
658 * Get rid of our current filedescriptors. AIOD's don't need any
659 * filedescriptors, except as temporarily inherited from the client.
660 * Credentials are also cloned, and made equivalent to "root".
664 cr = cratom(&mycp->p_ucred);
666 uireplace(&cr->cr_uidinfo, uifind(0));
668 cr->cr_groups[0] = 1;
670 /* The daemon resides in its own pgrp. */
671 enterpgrp(mycp, mycp->p_pid, 1);
673 /* Mark special process type. */
674 mycp->p_flag |= P_SYSTEM | P_KTHREADP;
677 * Wakeup parent process. (Parent sleeps to keep from blasting away
678 * and creating too many daemons.)
684 * curcp is the current daemon process context.
685 * userp is the current user process context.
690 * Take daemon off of free queue
692 if (aiop->aioprocflags & AIOP_FREE) {
694 TAILQ_REMOVE(&aio_freeproc, aiop, list);
695 TAILQ_INSERT_TAIL(&aio_activeproc, aiop, list);
696 aiop->aioprocflags &= ~AIOP_FREE;
699 aiop->aioprocflags &= ~AIOP_SCHED;
704 while ((aiocbe = aio_selectjob(aiop)) != NULL) {
705 cb = &aiocbe->uaiocb;
706 userp = aiocbe->userproc;
708 aiocbe->jobstate = JOBST_JOBRUNNING;
711 * Connect to process address space for user program.
713 if (userp != curcp) {
715 * Save the current address space that we are
718 tmpvm = mycp->p_vmspace;
721 * Point to the new user address space, and
724 mycp->p_vmspace = userp->p_vmspace;
725 mycp->p_vmspace->vm_refcnt++;
727 /* Activate the new mapping. */
731 * If the old address space wasn't the daemons
732 * own address space, then we need to remove the
733 * daemon's reference from the other process
734 * that it was acting on behalf of.
742 ki = userp->p_aioinfo;
745 /* Account for currently active jobs. */
746 ki->kaio_active_count++;
748 /* Do the I/O function. */
751 /* Decrement the active job count. */
752 ki->kaio_active_count--;
755 * Increment the completion count for wakeup/signal
758 aiocbe->jobflags |= AIOCBLIST_DONE;
759 ki->kaio_queue_finished_count++;
761 lj->lioj_queue_finished_count++;
762 if ((ki->kaio_flags & KAIO_WAKEUP) || ((ki->kaio_flags
763 & KAIO_RUNDOWN) && (ki->kaio_active_count == 0))) {
764 ki->kaio_flags &= ~KAIO_WAKEUP;
769 if (lj && (lj->lioj_flags &
770 (LIOJ_SIGNAL|LIOJ_SIGNAL_POSTED)) == LIOJ_SIGNAL) {
771 if ((lj->lioj_queue_finished_count ==
772 lj->lioj_queue_count) &&
773 (lj->lioj_buffer_finished_count ==
774 lj->lioj_buffer_count)) {
776 lj->lioj_signal.sigev_signo);
783 aiocbe->jobstate = JOBST_JOBFINISHED;
786 TAILQ_REMOVE(&ki->kaio_jobqueue, aiocbe, plist);
787 TAILQ_INSERT_TAIL(&ki->kaio_jobdone, aiocbe, plist);
789 KNOTE(&aiocbe->klist, 0);
791 if (aiocbe->jobflags & AIOCBLIST_RUNDOWN) {
793 aiocbe->jobflags &= ~AIOCBLIST_RUNDOWN;
796 if (cb->aio_sigevent.sigev_notify == SIGEV_SIGNAL) {
797 ksignal(userp, cb->aio_sigevent.sigev_signo);
802 * Disconnect from user address space.
805 /* Get the user address space to disconnect from. */
806 tmpvm = mycp->p_vmspace;
808 /* Get original address space for daemon. */
809 mycp->p_vmspace = myvm;
811 /* Activate the daemon's address space. */
815 kprintf("AIOD: vmspace problem -- %d\n",
819 /* Remove our vmspace reference. */
826 * If we are the first to be put onto the free queue, wakeup
827 * anyone waiting for a daemon.
830 TAILQ_REMOVE(&aio_activeproc, aiop, list);
831 if (TAILQ_EMPTY(&aio_freeproc))
832 wakeup(&aio_freeproc);
833 TAILQ_INSERT_HEAD(&aio_freeproc, aiop, list);
834 aiop->aioprocflags |= AIOP_FREE;
838 * If daemon is inactive for a long time, allow it to exit,
839 * thereby freeing resources.
841 if (((aiop->aioprocflags & AIOP_SCHED) == 0) && tsleep(mycp,
842 0, "aiordy", aiod_lifetime)) {
844 if (TAILQ_EMPTY(&aio_jobs)) {
845 if ((aiop->aioprocflags & AIOP_FREE) &&
846 (num_aio_procs > target_aio_procs)) {
847 TAILQ_REMOVE(&aio_freeproc, aiop, list);
849 zfree(aiop_zone, aiop);
852 if (mycp->p_vmspace->vm_refcnt <= 1) {
853 kprintf("AIOD: bad vm refcnt for"
854 " exiting daemon: %d\n",
855 mycp->p_vmspace->vm_refcnt);
867 * Create a new AIO daemon. This is mostly a kernel-thread fork routine. The
868 * AIO daemon modifies its environment itself.
874 struct lwp *lp, *nlp;
878 error = fork1(lp, RFPROC|RFMEM|RFNOWAIT, &np);
881 nlp = LIST_FIRST(&np->p_lwps);
882 cpu_set_fork_handler(nlp, aio_daemon, curproc);
883 start_forked_proc(lp, np);
886 * Wait until daemon is started, but continue on just in case to
887 * handle error conditions.
889 error = tsleep(np, 0, "aiosta", aiod_timeout);
896 * Try the high-performance, low-overhead physio method for eligible
897 * VCHR devices. This method doesn't use an aio helper thread, and
898 * thus has very low overhead.
900 * Assumes that the caller, _aio_aqueue(), has incremented the file
901 * structure's reference count, preventing its deallocation for the
902 * duration of this call.
905 aio_qphysio(struct proc *p, struct aiocblist *aiocbe)
913 struct aio_liojob *lj;
916 cb = &aiocbe->uaiocb;
917 fp = aiocbe->fd_file;
919 if (fp->f_type != DTYPE_VNODE)
922 vp = (struct vnode *)fp->f_data;
925 * If its not a disk, we don't want to return a positive error.
926 * It causes the aio code to not fall through to try the thread
927 * way when you're talking to a regular file.
929 if (!vn_isdisk(vp, &error)) {
930 if (error == ENOTBLK)
936 if (cb->aio_nbytes % vp->v_rdev->si_bsize_phys)
940 MAXPHYS - (((vm_offset_t) cb->aio_buf) & PAGE_MASK))
944 if (ki->kaio_buffer_count >= ki->kaio_ballowed_count)
947 ki->kaio_buffer_count++;
951 lj->lioj_buffer_count++;
953 /* Create and build a buffer header for a transfer. */
958 * Get a copy of the kva from the physical buffer.
960 bp->b_bio1.bio_caller_info1.ptr = p;
963 bp->b_cmd = (cb->aio_lio_opcode == LIO_WRITE) ?
964 BUF_CMD_WRITE : BUF_CMD_READ;
965 bp->b_bio1.bio_done = aio_physwakeup;
966 bp->b_bio1.bio_offset = cb->aio_offset;
968 /* Bring buffer into kernel space. */
969 if (vmapbuf(bp, __DEVOLATILE(char *, cb->aio_buf), cb->aio_nbytes) < 0) {
977 bp->b_bio1.bio_caller_info2.ptr = aiocbe;
978 TAILQ_INSERT_TAIL(&aio_bufjobs, aiocbe, list);
979 TAILQ_INSERT_TAIL(&ki->kaio_bufqueue, aiocbe, plist);
980 aiocbe->jobstate = JOBST_JOBQBUF;
981 cb->_aiocb_private.status = cb->aio_nbytes;
987 /* Perform transfer. */
988 dev_dstrategy(vp->v_rdev, &bp->b_bio1);
994 * If we had an error invoking the request, or an error in processing
995 * the request before we have returned, we process it as an error in
996 * transfer. Note that such an I/O error is not indicated immediately,
997 * but is returned using the aio_error mechanism. In this case,
998 * aio_suspend will return immediately.
1000 if (bp->b_error || (bp->b_flags & B_ERROR)) {
1001 struct aiocb *job = aiocbe->uuaiocb;
1003 aiocbe->uaiocb._aiocb_private.status = 0;
1004 suword(&job->_aiocb_private.status, 0);
1005 aiocbe->uaiocb._aiocb_private.error = bp->b_error;
1006 suword(&job->_aiocb_private.error, bp->b_error);
1008 ki->kaio_buffer_finished_count++;
1010 if (aiocbe->jobstate != JOBST_JOBBFINISHED) {
1011 aiocbe->jobstate = JOBST_JOBBFINISHED;
1012 aiocbe->jobflags |= AIOCBLIST_DONE;
1013 TAILQ_REMOVE(&aio_bufjobs, aiocbe, list);
1014 TAILQ_REMOVE(&ki->kaio_bufqueue, aiocbe, plist);
1015 TAILQ_INSERT_TAIL(&ki->kaio_bufdone, aiocbe, plist);
1021 KNOTE(&aiocbe->klist, 0);
1025 ki->kaio_buffer_count--;
1027 lj->lioj_buffer_count--;
1034 * This waits/tests physio completion.
1037 aio_fphysio(struct aiocblist *iocb)
1045 while (bp->b_cmd != BUF_CMD_DONE) {
1046 if (tsleep(bp, 0, "physstr", aiod_timeout)) {
1047 if (bp->b_cmd != BUF_CMD_DONE) {
1057 /* Release mapping into kernel space. */
1063 /* Check for an error. */
1064 if (bp->b_flags & B_ERROR)
1065 error = bp->b_error;
1070 #endif /* VFS_AIO */
1073 * Wake up aio requests that may be serviceable now.
1076 aio_swake(struct socket *so, struct sockbuf *sb)
1081 struct aiocblist *cb,*cbn;
1083 struct kaioinfo *ki = NULL;
1084 int opcode, wakecount = 0;
1085 struct aioproclist *aiop;
1087 if (sb == &so->so_snd) {
1089 so->so_snd.sb_flags &= ~SB_AIO;
1092 so->so_rcv.sb_flags &= ~SB_AIO;
1095 for (cb = TAILQ_FIRST(&so->so_aiojobq); cb; cb = cbn) {
1096 cbn = TAILQ_NEXT(cb, list);
1097 if (opcode == cb->uaiocb.aio_lio_opcode) {
1100 TAILQ_REMOVE(&so->so_aiojobq, cb, list);
1101 TAILQ_REMOVE(&ki->kaio_sockqueue, cb, plist);
1102 TAILQ_INSERT_TAIL(&aio_jobs, cb, list);
1103 TAILQ_INSERT_TAIL(&ki->kaio_jobqueue, cb, plist);
1105 if (cb->jobstate != JOBST_JOBQGLOBAL)
1106 panic("invalid queue value");
1110 while (wakecount--) {
1111 if ((aiop = TAILQ_FIRST(&aio_freeproc)) != 0) {
1112 TAILQ_REMOVE(&aio_freeproc, aiop, list);
1113 TAILQ_INSERT_TAIL(&aio_activeproc, aiop, list);
1114 aiop->aioprocflags &= ~AIOP_FREE;
1115 wakeup(aiop->aioproc);
1118 #endif /* VFS_AIO */
1123 * Queue a new AIO request. Choosing either the threaded or direct physio VCHR
1124 * technique is done in this code.
1127 _aio_aqueue(struct aiocb *job, struct aio_liojob *lj, int type)
1129 struct proc *p = curproc;
1130 struct filedesc *fdp;
1135 int opcode, user_opcode;
1136 struct aiocblist *aiocbe;
1137 struct aioproclist *aiop;
1138 struct kaioinfo *ki;
1143 if ((aiocbe = TAILQ_FIRST(&aio_freejobs)) != NULL)
1144 TAILQ_REMOVE(&aio_freejobs, aiocbe, list);
1146 aiocbe = zalloc (aiocb_zone);
1148 aiocbe->inputcharge = 0;
1149 aiocbe->outputcharge = 0;
1150 callout_init(&aiocbe->timeout);
1151 SLIST_INIT(&aiocbe->klist);
1153 suword(&job->_aiocb_private.status, -1);
1154 suword(&job->_aiocb_private.error, 0);
1155 suword(&job->_aiocb_private.kernelinfo, -1);
1157 error = copyin(job, &aiocbe->uaiocb, sizeof(aiocbe->uaiocb));
1159 suword(&job->_aiocb_private.error, error);
1160 TAILQ_INSERT_HEAD(&aio_freejobs, aiocbe, list);
1163 if (aiocbe->uaiocb.aio_sigevent.sigev_notify == SIGEV_SIGNAL &&
1164 !_SIG_VALID(aiocbe->uaiocb.aio_sigevent.sigev_signo)) {
1165 TAILQ_INSERT_HEAD(&aio_freejobs, aiocbe, list);
1169 /* Save userspace address of the job info. */
1170 aiocbe->uuaiocb = job;
1172 /* Get the opcode. */
1173 user_opcode = aiocbe->uaiocb.aio_lio_opcode;
1174 if (type != LIO_NOP)
1175 aiocbe->uaiocb.aio_lio_opcode = type;
1176 opcode = aiocbe->uaiocb.aio_lio_opcode;
1178 /* Get the fd info for process. */
1182 * Range check file descriptor.
1184 fd = aiocbe->uaiocb.aio_fildes;
1185 if (fd >= fdp->fd_nfiles) {
1186 TAILQ_INSERT_HEAD(&aio_freejobs, aiocbe, list);
1188 suword(&job->_aiocb_private.error, EBADF);
1192 fp = aiocbe->fd_file = fdp->fd_files[fd].fp;
1193 if ((fp == NULL) || ((opcode == LIO_WRITE) && ((fp->f_flag & FWRITE) ==
1195 TAILQ_INSERT_HEAD(&aio_freejobs, aiocbe, list);
1197 suword(&job->_aiocb_private.error, EBADF);
1202 if (aiocbe->uaiocb.aio_offset == -1LL) {
1206 error = suword(&job->_aiocb_private.kernelinfo, jobrefid);
1211 aiocbe->uaiocb._aiocb_private.kernelinfo = (void *)(intptr_t)jobrefid;
1212 if (jobrefid == LONG_MAX)
1217 if (opcode == LIO_NOP) {
1219 TAILQ_INSERT_HEAD(&aio_freejobs, aiocbe, list);
1221 suword(&job->_aiocb_private.error, 0);
1222 suword(&job->_aiocb_private.status, 0);
1223 suword(&job->_aiocb_private.kernelinfo, 0);
1227 if ((opcode != LIO_READ) && (opcode != LIO_WRITE)) {
1229 suword(&job->_aiocb_private.status, 0);
1234 if (aiocbe->uaiocb.aio_sigevent.sigev_notify == SIGEV_KEVENT) {
1235 kev.ident = aiocbe->uaiocb.aio_sigevent.sigev_notify_kqueue;
1236 kev.udata = aiocbe->uaiocb.aio_sigevent.sigev_value.sigval_ptr;
1240 * This method for requesting kevent-based notification won't
1241 * work on the alpha, since we're passing in a pointer
1242 * via aio_lio_opcode, which is an int. Use the SIGEV_KEVENT-
1243 * based method instead.
1245 if (user_opcode == LIO_NOP || user_opcode == LIO_READ ||
1246 user_opcode == LIO_WRITE)
1249 error = copyin((struct kevent *)(uintptr_t)user_opcode,
1254 if ((u_int)kev.ident >= fdp->fd_nfiles ||
1255 (kq_fp = fdp->fd_files[kev.ident].fp) == NULL ||
1256 (kq_fp->f_type != DTYPE_KQUEUE)) {
1260 kq = (struct kqueue *)kq_fp->f_data;
1261 kev.ident = (uintptr_t)aiocbe->uuaiocb;
1262 kev.filter = EVFILT_AIO;
1263 kev.flags = EV_ADD | EV_ENABLE | EV_FLAG1;
1264 kev.data = (intptr_t)aiocbe;
1265 error = kqueue_register(kq, &kev, p->p_thread);
1269 TAILQ_INSERT_HEAD(&aio_freejobs, aiocbe, list);
1271 suword(&job->_aiocb_private.error, error);
1276 suword(&job->_aiocb_private.error, EINPROGRESS);
1277 aiocbe->uaiocb._aiocb_private.error = EINPROGRESS;
1278 aiocbe->userproc = p;
1279 aiocbe->jobflags = 0;
1283 if (fp->f_type == DTYPE_SOCKET) {
1285 * Alternate queueing for socket ops: Reach down into the
1286 * descriptor to get the socket data. Then check to see if the
1287 * socket is ready to be read or written (based on the requested
1290 * If it is not ready for io, then queue the aiocbe on the
1291 * socket, and set the flags so we get a call when sbnotify()
1294 so = (struct socket *)fp->f_data;
1296 if (((opcode == LIO_READ) && (!soreadable(so))) || ((opcode ==
1297 LIO_WRITE) && (!sowriteable(so)))) {
1298 TAILQ_INSERT_TAIL(&so->so_aiojobq, aiocbe, list);
1299 TAILQ_INSERT_TAIL(&ki->kaio_sockqueue, aiocbe, plist);
1300 if (opcode == LIO_READ)
1301 so->so_rcv.sb_flags |= SB_AIO;
1303 so->so_snd.sb_flags |= SB_AIO;
1304 aiocbe->jobstate = JOBST_JOBQGLOBAL; /* XXX */
1305 ki->kaio_queue_count++;
1314 if ((error = aio_qphysio(p, aiocbe)) == 0)
1317 suword(&job->_aiocb_private.status, 0);
1318 aiocbe->uaiocb._aiocb_private.error = error;
1319 suword(&job->_aiocb_private.error, error);
1323 /* No buffer for daemon I/O. */
1326 ki->kaio_queue_count++;
1328 lj->lioj_queue_count++;
1330 TAILQ_INSERT_TAIL(&ki->kaio_jobqueue, aiocbe, plist);
1331 TAILQ_INSERT_TAIL(&aio_jobs, aiocbe, list);
1333 aiocbe->jobstate = JOBST_JOBQGLOBAL;
1339 * If we don't have a free AIO process, and we are below our quota, then
1340 * start one. Otherwise, depend on the subsequent I/O completions to
1341 * pick-up this job. If we don't successfully create the new process
1342 * (thread) due to resource issues, we return an error for now (EAGAIN),
1343 * which is likely not the correct thing to do.
1347 if ((aiop = TAILQ_FIRST(&aio_freeproc)) != NULL) {
1348 TAILQ_REMOVE(&aio_freeproc, aiop, list);
1349 TAILQ_INSERT_TAIL(&aio_activeproc, aiop, list);
1350 aiop->aioprocflags &= ~AIOP_FREE;
1351 wakeup(aiop->aioproc);
1352 } else if (((num_aio_resv_start + num_aio_procs) < max_aio_procs) &&
1353 ((ki->kaio_active_count + num_aio_resv_start) <
1354 ki->kaio_maxactive_count)) {
1355 num_aio_resv_start++;
1356 if ((error = aio_newproc()) == 0) {
1357 num_aio_resv_start--;
1360 num_aio_resv_start--;
1368 * This routine queues an AIO request, checking for quotas.
1371 aio_aqueue(struct aiocb *job, int type)
1373 struct proc *p = curproc;
1374 struct kaioinfo *ki;
1376 if (p->p_aioinfo == NULL)
1377 aio_init_aioinfo(p);
1379 if (num_queue_count >= max_queue_count)
1383 if (ki->kaio_queue_count >= ki->kaio_qallowed_count)
1386 return _aio_aqueue(job, NULL, type);
1388 #endif /* VFS_AIO */
1391 * Support the aio_return system call, as a side-effect, kernel resources are
1395 sys_aio_return(struct aio_return_args *uap)
1400 struct proc *p = curproc;
1402 struct aiocblist *cb, *ncb;
1404 struct kaioinfo *ki;
1412 jobref = fuword(&ujob->_aiocb_private.kernelinfo);
1413 if (jobref == -1 || jobref == 0)
1416 TAILQ_FOREACH(cb, &ki->kaio_jobdone, plist) {
1417 if (((intptr_t) cb->uaiocb._aiocb_private.kernelinfo) ==
1419 if (ujob == cb->uuaiocb) {
1420 uap->sysmsg_result =
1421 cb->uaiocb._aiocb_private.status;
1423 uap->sysmsg_result = EFAULT;
1424 if (cb->uaiocb.aio_lio_opcode == LIO_WRITE) {
1425 p->p_stats->p_ru.ru_oublock +=
1427 cb->outputcharge = 0;
1428 } else if (cb->uaiocb.aio_lio_opcode == LIO_READ) {
1429 p->p_stats->p_ru.ru_inblock += cb->inputcharge;
1430 cb->inputcharge = 0;
1437 for (cb = TAILQ_FIRST(&ki->kaio_bufdone); cb; cb = ncb) {
1438 ncb = TAILQ_NEXT(cb, plist);
1439 if (((intptr_t) cb->uaiocb._aiocb_private.kernelinfo)
1442 if (ujob == cb->uuaiocb) {
1443 uap->sysmsg_result =
1444 cb->uaiocb._aiocb_private.status;
1446 uap->sysmsg_result = EFAULT;
1454 #endif /* VFS_AIO */
1458 * Allow a process to wakeup when any of the I/O requests are completed.
1461 sys_aio_suspend(struct aio_suspend_args *uap)
1466 struct proc *p = curproc;
1469 struct aiocb *const *cbptr, *cbp;
1470 struct kaioinfo *ki;
1471 struct aiocblist *cb;
1476 struct aiocb **ujoblist;
1478 if (uap->nent > AIO_LISTIO_MAX)
1483 /* Get timespec struct. */
1484 if ((error = copyin(uap->timeout, &ts, sizeof(ts))) != 0)
1487 if (ts.tv_nsec < 0 || ts.tv_nsec >= 1000000000)
1490 TIMESPEC_TO_TIMEVAL(&atv, &ts);
1491 if (itimerfix(&atv))
1493 timo = tvtohz_high(&atv);
1501 ijoblist = zalloc(aiol_zone);
1502 ujoblist = zalloc(aiol_zone);
1503 cbptr = uap->aiocbp;
1505 for (i = 0; i < uap->nent; i++) {
1506 cbp = (struct aiocb *)(intptr_t)fuword(&cbptr[i]);
1509 ujoblist[njoblist] = cbp;
1510 ijoblist[njoblist] = fuword(&cbp->_aiocb_private.kernelinfo);
1514 if (njoblist == 0) {
1515 zfree(aiol_zone, ijoblist);
1516 zfree(aiol_zone, ujoblist);
1522 TAILQ_FOREACH(cb, &ki->kaio_jobdone, plist) {
1523 for (i = 0; i < njoblist; i++) {
1525 cb->uaiocb._aiocb_private.kernelinfo) ==
1527 if (ujoblist[i] != cb->uuaiocb)
1529 zfree(aiol_zone, ijoblist);
1530 zfree(aiol_zone, ujoblist);
1537 for (cb = TAILQ_FIRST(&ki->kaio_bufdone); cb; cb =
1538 TAILQ_NEXT(cb, plist)) {
1539 for (i = 0; i < njoblist; i++) {
1541 cb->uaiocb._aiocb_private.kernelinfo) ==
1544 if (ujoblist[i] != cb->uuaiocb)
1546 zfree(aiol_zone, ijoblist);
1547 zfree(aiol_zone, ujoblist);
1553 ki->kaio_flags |= KAIO_WAKEUP;
1554 error = tsleep(p, PCATCH, "aiospn", timo);
1557 if (error == ERESTART || error == EINTR) {
1558 zfree(aiol_zone, ijoblist);
1559 zfree(aiol_zone, ujoblist);
1561 } else if (error == EWOULDBLOCK) {
1562 zfree(aiol_zone, ijoblist);
1563 zfree(aiol_zone, ujoblist);
1570 #endif /* VFS_AIO */
1574 * aio_cancel cancels any non-physio aio operations not currently in
1578 sys_aio_cancel(struct aio_cancel_args *uap)
1583 struct proc *p = curproc;
1584 struct kaioinfo *ki;
1585 struct aiocblist *cbe, *cbn;
1587 struct filedesc *fdp;
1596 if ((u_int)uap->fd >= fdp->fd_nfiles ||
1597 (fp = fdp->fd_files[uap->fd].fp) == NULL)
1600 if (fp->f_type == DTYPE_VNODE) {
1601 vp = (struct vnode *)fp->f_data;
1603 if (vn_isdisk(vp,&error)) {
1604 uap->sysmsg_result = AIO_NOTCANCELED;
1607 } else if (fp->f_type == DTYPE_SOCKET) {
1608 so = (struct socket *)fp->f_data;
1612 for (cbe = TAILQ_FIRST(&so->so_aiojobq); cbe; cbe = cbn) {
1613 cbn = TAILQ_NEXT(cbe, list);
1614 if ((uap->aiocbp == NULL) ||
1615 (uap->aiocbp == cbe->uuaiocb) ) {
1618 TAILQ_REMOVE(&so->so_aiojobq, cbe, list);
1619 TAILQ_REMOVE(&ki->kaio_sockqueue, cbe, plist);
1620 TAILQ_INSERT_TAIL(&ki->kaio_jobdone, cbe, plist);
1621 if (ki->kaio_flags & KAIO_WAKEUP) {
1624 cbe->jobstate = JOBST_JOBFINISHED;
1625 cbe->uaiocb._aiocb_private.status=-1;
1626 cbe->uaiocb._aiocb_private.error=ECANCELED;
1628 /* XXX cancelled, knote? */
1629 if (cbe->uaiocb.aio_sigevent.sigev_notify ==
1631 ksignal(cbe->userproc, cbe->uaiocb.aio_sigevent.sigev_signo);
1638 if ((cancelled) && (uap->aiocbp)) {
1639 uap->sysmsg_result = AIO_CANCELED;
1648 for (cbe = TAILQ_FIRST(&ki->kaio_jobqueue); cbe; cbe = cbn) {
1649 cbn = TAILQ_NEXT(cbe, plist);
1651 if ((uap->fd == cbe->uaiocb.aio_fildes) &&
1652 ((uap->aiocbp == NULL ) ||
1653 (uap->aiocbp == cbe->uuaiocb))) {
1655 if (cbe->jobstate == JOBST_JOBQGLOBAL) {
1656 TAILQ_REMOVE(&aio_jobs, cbe, list);
1657 TAILQ_REMOVE(&ki->kaio_jobqueue, cbe, plist);
1658 TAILQ_INSERT_TAIL(&ki->kaio_jobdone, cbe,
1661 ki->kaio_queue_finished_count++;
1662 cbe->jobstate = JOBST_JOBFINISHED;
1663 cbe->uaiocb._aiocb_private.status = -1;
1664 cbe->uaiocb._aiocb_private.error = ECANCELED;
1665 /* XXX cancelled, knote? */
1666 if (cbe->uaiocb.aio_sigevent.sigev_notify ==
1668 ksignal(cbe->userproc, cbe->uaiocb.aio_sigevent.sigev_signo);
1677 uap->sysmsg_result = AIO_NOTCANCELED;
1681 uap->sysmsg_result = AIO_CANCELED;
1684 uap->sysmsg_result = AIO_ALLDONE;
1687 #endif /* VFS_AIO */
1691 * aio_error is implemented in the kernel level for compatibility purposes only.
1692 * For a user mode async implementation, it would be best to do it in a userland
1696 sys_aio_error(struct aio_error_args *uap)
1701 struct proc *p = curproc;
1702 struct aiocblist *cb;
1703 struct kaioinfo *ki;
1710 jobref = fuword(&uap->aiocbp->_aiocb_private.kernelinfo);
1711 if ((jobref == -1) || (jobref == 0))
1714 TAILQ_FOREACH(cb, &ki->kaio_jobdone, plist) {
1715 if (((intptr_t)cb->uaiocb._aiocb_private.kernelinfo) ==
1717 uap->sysmsg_result = cb->uaiocb._aiocb_private.error;
1724 for (cb = TAILQ_FIRST(&ki->kaio_jobqueue); cb; cb = TAILQ_NEXT(cb,
1726 if (((intptr_t)cb->uaiocb._aiocb_private.kernelinfo) ==
1728 uap->sysmsg_result = EINPROGRESS;
1734 for (cb = TAILQ_FIRST(&ki->kaio_sockqueue); cb; cb = TAILQ_NEXT(cb,
1736 if (((intptr_t)cb->uaiocb._aiocb_private.kernelinfo) ==
1738 uap->sysmsg_result = EINPROGRESS;
1746 for (cb = TAILQ_FIRST(&ki->kaio_bufdone); cb; cb = TAILQ_NEXT(cb,
1748 if (((intptr_t)cb->uaiocb._aiocb_private.kernelinfo) ==
1750 uap->sysmsg_result = cb->uaiocb._aiocb_private.error;
1756 for (cb = TAILQ_FIRST(&ki->kaio_bufqueue); cb; cb = TAILQ_NEXT(cb,
1758 if (((intptr_t)cb->uaiocb._aiocb_private.kernelinfo) ==
1760 uap->sysmsg_result = EINPROGRESS;
1771 status = fuword(&uap->aiocbp->_aiocb_private.status);
1773 return fuword(&uap->aiocbp->_aiocb_private.error);
1776 #endif /* VFS_AIO */
1779 /* syscall - asynchronous read from a file (REALTIME) */
1781 sys_aio_read(struct aio_read_args *uap)
1786 return aio_aqueue(uap->aiocbp, LIO_READ);
1787 #endif /* VFS_AIO */
1790 /* syscall - asynchronous write to a file (REALTIME) */
1792 sys_aio_write(struct aio_write_args *uap)
1797 return aio_aqueue(uap->aiocbp, LIO_WRITE);
1798 #endif /* VFS_AIO */
1801 /* syscall - XXX undocumented */
1803 sys_lio_listio(struct lio_listio_args *uap)
1808 struct proc *p = curproc;
1809 int nent, nentqueued;
1810 struct aiocb *iocb, * const *cbptr;
1811 struct aiocblist *cb;
1812 struct kaioinfo *ki;
1813 struct aio_liojob *lj;
1814 int error, runningcode;
1818 if ((uap->mode != LIO_NOWAIT) && (uap->mode != LIO_WAIT))
1822 if (nent > AIO_LISTIO_MAX)
1825 if (p->p_aioinfo == NULL)
1826 aio_init_aioinfo(p);
1828 if ((nent + num_queue_count) > max_queue_count)
1832 if ((nent + ki->kaio_queue_count) > ki->kaio_qallowed_count)
1835 lj = zalloc(aiolio_zone);
1840 lj->lioj_buffer_count = 0;
1841 lj->lioj_buffer_finished_count = 0;
1842 lj->lioj_queue_count = 0;
1843 lj->lioj_queue_finished_count = 0;
1849 if (uap->sig && (uap->mode == LIO_NOWAIT)) {
1850 error = copyin(uap->sig, &lj->lioj_signal,
1851 sizeof(lj->lioj_signal));
1853 zfree(aiolio_zone, lj);
1856 if (!_SIG_VALID(lj->lioj_signal.sigev_signo)) {
1857 zfree(aiolio_zone, lj);
1860 lj->lioj_flags |= LIOJ_SIGNAL;
1861 lj->lioj_flags &= ~LIOJ_SIGNAL_POSTED;
1863 lj->lioj_flags &= ~LIOJ_SIGNAL;
1865 TAILQ_INSERT_TAIL(&ki->kaio_liojoblist, lj, lioj_list);
1867 * Get pointers to the list of I/O requests.
1871 cbptr = uap->acb_list;
1872 for (i = 0; i < uap->nent; i++) {
1873 iocb = (struct aiocb *)(intptr_t)fuword(&cbptr[i]);
1874 if (((intptr_t)iocb != -1) && ((intptr_t)iocb != 0)) {
1875 error = _aio_aqueue(iocb, lj, 0);
1884 * If we haven't queued any, then just return error.
1886 if (nentqueued == 0)
1890 * Calculate the appropriate error return.
1896 if (uap->mode == LIO_WAIT) {
1897 int command, found, jobref;
1901 for (i = 0; i < uap->nent; i++) {
1903 * Fetch address of the control buf pointer in
1906 iocb = (struct aiocb *)
1907 (intptr_t)fuword(&cbptr[i]);
1908 if (((intptr_t)iocb == -1) || ((intptr_t)iocb
1913 * Fetch the associated command from user space.
1915 command = fuword(&iocb->aio_lio_opcode);
1916 if (command == LIO_NOP) {
1921 jobref = fuword(&iocb->_aiocb_private.kernelinfo);
1923 TAILQ_FOREACH(cb, &ki->kaio_jobdone, plist) {
1924 if (((intptr_t)cb->uaiocb._aiocb_private.kernelinfo)
1926 if (cb->uaiocb.aio_lio_opcode
1928 p->p_stats->p_ru.ru_oublock
1931 cb->outputcharge = 0;
1932 } else if (cb->uaiocb.aio_lio_opcode
1934 p->p_stats->p_ru.ru_inblock
1936 cb->inputcharge = 0;
1944 TAILQ_FOREACH(cb, &ki->kaio_bufdone, plist) {
1945 if (((intptr_t)cb->uaiocb._aiocb_private.kernelinfo)
1955 * If all I/Os have been disposed of, then we can
1958 if (found == nentqueued)
1961 ki->kaio_flags |= KAIO_WAKEUP;
1962 error = tsleep(p, PCATCH, "aiospn", 0);
1966 else if (error == EWOULDBLOCK)
1972 #endif /* VFS_AIO */
1977 * This is a weird hack so that we can post a signal. It is safe to do so from
1978 * a timeout routine, but *not* from an interrupt routine.
1981 process_signal(void *aioj)
1983 struct aiocblist *aiocbe = aioj;
1984 struct aio_liojob *lj = aiocbe->lio;
1985 struct aiocb *cb = &aiocbe->uaiocb;
1987 if ((lj) && (lj->lioj_signal.sigev_notify == SIGEV_SIGNAL) &&
1988 (lj->lioj_queue_count == lj->lioj_queue_finished_count)) {
1989 ksignal(lj->lioj_ki->kaio_p, lj->lioj_signal.sigev_signo);
1990 lj->lioj_flags |= LIOJ_SIGNAL_POSTED;
1993 if (cb->aio_sigevent.sigev_notify == SIGEV_SIGNAL)
1994 ksignal(aiocbe->userproc, cb->aio_sigevent.sigev_signo);
1998 * Interrupt handler for physio, performs the necessary process wakeups, and
2002 aio_physwakeup(struct bio *bio)
2004 struct buf *bp = bio->bio_buf;
2005 struct aiocblist *aiocbe;
2007 struct kaioinfo *ki;
2008 struct aio_liojob *lj;
2010 aiocbe = bio->bio_caller_info2.ptr;
2013 p = bio->bio_caller_info1.ptr;
2015 aiocbe->jobstate = JOBST_JOBBFINISHED;
2016 aiocbe->uaiocb._aiocb_private.status -= bp->b_resid;
2017 aiocbe->uaiocb._aiocb_private.error = 0;
2018 aiocbe->jobflags |= AIOCBLIST_DONE;
2020 if (bp->b_flags & B_ERROR)
2021 aiocbe->uaiocb._aiocb_private.error = bp->b_error;
2025 lj->lioj_buffer_finished_count++;
2028 * wakeup/signal if all of the interrupt jobs are done.
2030 if (lj->lioj_buffer_finished_count ==
2031 lj->lioj_buffer_count) {
2033 * Post a signal if it is called for.
2035 if ((lj->lioj_flags &
2036 (LIOJ_SIGNAL|LIOJ_SIGNAL_POSTED)) ==
2038 lj->lioj_flags |= LIOJ_SIGNAL_POSTED;
2039 callout_reset(&aiocbe->timeout, 0,
2040 process_signal, aiocbe);
2047 ki->kaio_buffer_finished_count++;
2048 TAILQ_REMOVE(&aio_bufjobs, aiocbe, list);
2049 TAILQ_REMOVE(&ki->kaio_bufqueue, aiocbe, plist);
2050 TAILQ_INSERT_TAIL(&ki->kaio_bufdone, aiocbe, plist);
2052 KNOTE(&aiocbe->klist, 0);
2053 /* Do the wakeup. */
2054 if (ki->kaio_flags & (KAIO_RUNDOWN|KAIO_WAKEUP)) {
2055 ki->kaio_flags &= ~KAIO_WAKEUP;
2060 if (aiocbe->uaiocb.aio_sigevent.sigev_notify == SIGEV_SIGNAL) {
2061 callout_reset(&aiocbe->timeout, 0,
2062 process_signal, aiocbe);
2065 bp->b_cmd = BUF_CMD_DONE;
2068 #endif /* VFS_AIO */
2070 /* syscall - wait for the next completion of an aio request */
2072 sys_aio_waitcomplete(struct aio_waitcomplete_args *uap)
2077 struct proc *p = curproc;
2080 struct kaioinfo *ki;
2081 struct aiocblist *cb = NULL;
2084 suword(uap->aiocbp, (int)NULL);
2088 /* Get timespec struct. */
2089 error = copyin(uap->timeout, &ts, sizeof(ts));
2093 if ((ts.tv_nsec < 0) || (ts.tv_nsec >= 1000000000))
2096 TIMESPEC_TO_TIMEVAL(&atv, &ts);
2097 if (itimerfix(&atv))
2099 timo = tvtohz_high(&atv);
2107 if ((cb = TAILQ_FIRST(&ki->kaio_jobdone)) != 0) {
2108 suword(uap->aiocbp, (uintptr_t)cb->uuaiocb);
2109 uap->sysmsg_result = cb->uaiocb._aiocb_private.status;
2110 if (cb->uaiocb.aio_lio_opcode == LIO_WRITE) {
2111 p->p_stats->p_ru.ru_oublock +=
2113 cb->outputcharge = 0;
2114 } else if (cb->uaiocb.aio_lio_opcode == LIO_READ) {
2115 p->p_stats->p_ru.ru_inblock += cb->inputcharge;
2116 cb->inputcharge = 0;
2119 return cb->uaiocb._aiocb_private.error;
2123 if ((cb = TAILQ_FIRST(&ki->kaio_bufdone)) != 0 ) {
2125 suword(uap->aiocbp, (uintptr_t)cb->uuaiocb);
2126 uap->sysmsg_result = cb->uaiocb._aiocb_private.status;
2128 return cb->uaiocb._aiocb_private.error;
2131 ki->kaio_flags |= KAIO_WAKEUP;
2132 error = tsleep(p, PCATCH, "aiowc", timo);
2135 if (error == ERESTART)
2139 else if (error == EINTR)
2141 else if (error == EWOULDBLOCK)
2144 #endif /* VFS_AIO */
2149 filt_aioattach(struct knote *kn)
2155 struct filterops aio_filtops =
2156 { 0, filt_aioattach, NULL, NULL };
2159 /* kqueue attach function */
2161 filt_aioattach(struct knote *kn)
2163 struct aiocblist *aiocbe = (struct aiocblist *)kn->kn_sdata;
2166 * The aiocbe pointer must be validated before using it, so
2167 * registration is restricted to the kernel; the user cannot
2170 if ((kn->kn_flags & EV_FLAG1) == 0)
2172 kn->kn_flags &= ~EV_FLAG1;
2174 SLIST_INSERT_HEAD(&aiocbe->klist, kn, kn_selnext);
2179 /* kqueue detach function */
2181 filt_aiodetach(struct knote *kn)
2183 struct aiocblist *aiocbe = (struct aiocblist *)kn->kn_sdata;
2185 SLIST_REMOVE(&aiocbe->klist, kn, knote, kn_selnext);
2188 /* kqueue filter function */
2191 filt_aio(struct knote *kn, long hint)
2193 struct aiocblist *aiocbe = (struct aiocblist *)kn->kn_sdata;
2195 kn->kn_data = aiocbe->uaiocb._aiocb_private.error;
2196 if (aiocbe->jobstate != JOBST_JOBFINISHED &&
2197 aiocbe->jobstate != JOBST_JOBBFINISHED)
2199 kn->kn_flags |= EV_EOF;
2203 struct filterops aio_filtops =
2204 { 0, filt_aioattach, filt_aiodetach, filt_aio };
2205 #endif /* VFS_AIO */