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.
20 * This file contains support for the POSIX 1003.1B AIO/LIO facility.
23 #include <sys/param.h>
24 #include <sys/systm.h>
27 #include <sys/sysproto.h>
28 #include <sys/filedesc.h>
29 #include <sys/kernel.h>
30 #include <sys/kthread.h>
31 #include <sys/fcntl.h>
34 #include <sys/mutex.h>
35 #include <sys/unistd.h>
37 #include <sys/resourcevar.h>
38 #include <sys/signalvar.h>
39 #include <sys/protosw.h>
40 #include <sys/socketvar.h>
41 #include <sys/sysctl.h>
42 #include <sys/vnode.h>
44 #include <sys/event.h>
47 #include <vm/vm_extern.h>
49 #include <vm/vm_map.h>
50 #include <vm/vm_zone.h>
53 #include <machine/limits.h>
55 #include "opt_vfs_aio.h"
61 #define JOBST_NULL 0x0
62 #define JOBST_JOBQPROC 0x1
63 #define JOBST_JOBQGLOBAL 0x2
64 #define JOBST_JOBRUNNING 0x3
65 #define JOBST_JOBFINISHED 0x4
66 #define JOBST_JOBQBUF 0x5
67 #define JOBST_JOBBFINISHED 0x6
69 #ifndef MAX_AIO_PER_PROC
70 #define MAX_AIO_PER_PROC 32
73 #ifndef MAX_AIO_QUEUE_PER_PROC
74 #define MAX_AIO_QUEUE_PER_PROC 256 /* Bigger than AIO_LISTIO_MAX */
78 #define MAX_AIO_PROCS 32
82 #define MAX_AIO_QUEUE 1024 /* Bigger than AIO_LISTIO_MAX */
85 #ifndef TARGET_AIO_PROCS
86 #define TARGET_AIO_PROCS 4
90 #define MAX_BUF_AIO 16
93 #ifndef AIOD_TIMEOUT_DEFAULT
94 #define AIOD_TIMEOUT_DEFAULT (10 * hz)
97 #ifndef AIOD_LIFETIME_DEFAULT
98 #define AIOD_LIFETIME_DEFAULT (30 * hz)
101 static int max_aio_procs = MAX_AIO_PROCS;
102 static int num_aio_procs = 0;
103 static int target_aio_procs = TARGET_AIO_PROCS;
104 static int max_queue_count = MAX_AIO_QUEUE;
105 static int num_queue_count = 0;
106 static int num_buf_aio = 0;
107 static int num_aio_resv_start = 0;
108 static int aiod_timeout;
109 static int aiod_lifetime;
111 static int max_aio_per_proc = MAX_AIO_PER_PROC;
112 static int max_aio_queue_per_proc = MAX_AIO_QUEUE_PER_PROC;
113 static int max_buf_aio = MAX_BUF_AIO;
115 SYSCTL_NODE(_vfs, OID_AUTO, aio, CTLFLAG_RW, 0, "AIO mgmt");
117 SYSCTL_INT(_vfs_aio, OID_AUTO, max_aio_per_proc,
118 CTLFLAG_RW, &max_aio_per_proc, 0, "");
120 SYSCTL_INT(_vfs_aio, OID_AUTO, max_aio_queue_per_proc,
121 CTLFLAG_RW, &max_aio_queue_per_proc, 0, "");
123 SYSCTL_INT(_vfs_aio, OID_AUTO, max_aio_procs,
124 CTLFLAG_RW, &max_aio_procs, 0, "");
126 SYSCTL_INT(_vfs_aio, OID_AUTO, num_aio_procs,
127 CTLFLAG_RD, &num_aio_procs, 0, "");
129 SYSCTL_INT(_vfs_aio, OID_AUTO, num_queue_count,
130 CTLFLAG_RD, &num_queue_count, 0, "");
132 SYSCTL_INT(_vfs_aio, OID_AUTO, max_aio_queue,
133 CTLFLAG_RW, &max_queue_count, 0, "");
135 SYSCTL_INT(_vfs_aio, OID_AUTO, target_aio_procs,
136 CTLFLAG_RW, &target_aio_procs, 0, "");
138 SYSCTL_INT(_vfs_aio, OID_AUTO, max_buf_aio,
139 CTLFLAG_RW, &max_buf_aio, 0, "");
141 SYSCTL_INT(_vfs_aio, OID_AUTO, num_buf_aio,
142 CTLFLAG_RD, &num_buf_aio, 0, "");
144 SYSCTL_INT(_vfs_aio, OID_AUTO, aiod_lifetime,
145 CTLFLAG_RW, &aiod_lifetime, 0, "");
147 SYSCTL_INT(_vfs_aio, OID_AUTO, aiod_timeout,
148 CTLFLAG_RW, &aiod_timeout, 0, "");
153 #define AIOP_FREE 0x1 /* proc on free queue */
154 #define AIOP_SCHED 0x2 /* proc explicitly scheduled */
157 int aioprocflags; /* AIO proc flags */
158 TAILQ_ENTRY(aioproclist) list; /* List of processes */
159 struct proc *aioproc; /* The AIO thread */
160 TAILQ_HEAD (,aiocblist) jobtorun; /* suggested job to run */
164 * data-structure for lio signal management
168 int lioj_buffer_count;
169 int lioj_buffer_finished_count;
170 int lioj_queue_count;
171 int lioj_queue_finished_count;
172 struct sigevent lioj_signal; /* signal on all I/O done */
173 TAILQ_ENTRY (aio_liojob) lioj_list;
174 struct kaioinfo *lioj_ki;
176 #define LIOJ_SIGNAL 0x1 /* signal on all done (lio) */
177 #define LIOJ_SIGNAL_POSTED 0x2 /* signal has been posted */
180 * per process aio data structure
183 int kaio_flags; /* per process kaio flags */
184 int kaio_maxactive_count; /* maximum number of AIOs */
185 int kaio_active_count; /* number of currently used AIOs */
186 int kaio_qallowed_count; /* maxiumu size of AIO queue */
187 int kaio_queue_count; /* size of AIO queue */
188 int kaio_ballowed_count; /* maximum number of buffers */
189 int kaio_queue_finished_count; /* number of daemon jobs finished */
190 int kaio_buffer_count; /* number of physio buffers */
191 int kaio_buffer_finished_count; /* count of I/O done */
192 struct proc *kaio_p; /* process that uses this kaio block */
193 TAILQ_HEAD (,aio_liojob) kaio_liojoblist; /* list of lio jobs */
194 TAILQ_HEAD (,aiocblist) kaio_jobqueue; /* job queue for process */
195 TAILQ_HEAD (,aiocblist) kaio_jobdone; /* done queue for process */
196 TAILQ_HEAD (,aiocblist) kaio_bufqueue; /* buffer job queue for process */
197 TAILQ_HEAD (,aiocblist) kaio_bufdone; /* buffer done queue for process */
198 TAILQ_HEAD (,aiocblist) kaio_sockqueue; /* queue for aios waiting on sockets */
201 #define KAIO_RUNDOWN 0x1 /* process is being run down */
202 #define KAIO_WAKEUP 0x2 /* wakeup process when there is a significant event */
204 static TAILQ_HEAD(,aioproclist) aio_freeproc, aio_activeproc;
205 static TAILQ_HEAD(,aiocblist) aio_jobs; /* Async job list */
206 static TAILQ_HEAD(,aiocblist) aio_bufjobs; /* Phys I/O job list */
208 static void aio_init_aioinfo(struct proc *p);
209 static void aio_onceonly(void *);
210 static int aio_free_entry(struct aiocblist *aiocbe);
211 static void aio_process(struct aiocblist *aiocbe);
212 static int aio_newproc(void);
213 static int aio_aqueue(struct proc *p, struct aiocb *job, int type);
214 static void aio_physwakeup(struct buf *bp);
215 static int aio_fphysio(struct proc *p, struct aiocblist *aiocbe);
216 static int aio_qphysio(struct proc *p, struct aiocblist *iocb);
217 static void aio_daemon(void *uproc);
218 static void process_signal(void *aioj);
220 SYSINIT(aio, SI_SUB_VFS, SI_ORDER_ANY, aio_onceonly, NULL);
222 static vm_zone_t kaio_zone = 0, aiop_zone = 0, aiocb_zone = 0, aiol_zone = 0;
223 static vm_zone_t aiolio_zone = 0;
226 * Startup initialization
229 aio_onceonly(void *na)
231 TAILQ_INIT(&aio_freeproc);
232 TAILQ_INIT(&aio_activeproc);
233 TAILQ_INIT(&aio_jobs);
234 TAILQ_INIT(&aio_bufjobs);
235 kaio_zone = zinit("AIO", sizeof (struct kaioinfo), 0, 0, 1);
236 aiop_zone = zinit("AIOP", sizeof (struct aioproclist), 0, 0, 1);
237 aiocb_zone = zinit("AIOCB", sizeof (struct aiocblist), 0, 0, 1);
238 aiol_zone = zinit("AIOL", AIO_LISTIO_MAX * sizeof (int), 0, 0, 1);
239 aiolio_zone = zinit("AIOLIO", AIO_LISTIO_MAX * sizeof (struct
240 aio_liojob), 0, 0, 1);
241 aiod_timeout = AIOD_TIMEOUT_DEFAULT;
242 aiod_lifetime = AIOD_LIFETIME_DEFAULT;
247 * Init the per-process aioinfo structure. The aioinfo limits are set
248 * per-process for user limit (resource) management.
251 aio_init_aioinfo(struct proc *p)
254 if (p->p_aioinfo == NULL) {
255 ki = zalloc(kaio_zone);
258 ki->kaio_maxactive_count = max_aio_per_proc;
259 ki->kaio_active_count = 0;
260 ki->kaio_qallowed_count = max_aio_queue_per_proc;
261 ki->kaio_queue_count = 0;
262 ki->kaio_ballowed_count = max_buf_aio;
263 ki->kaio_buffer_count = 0;
264 ki->kaio_buffer_finished_count = 0;
266 TAILQ_INIT(&ki->kaio_jobdone);
267 TAILQ_INIT(&ki->kaio_jobqueue);
268 TAILQ_INIT(&ki->kaio_bufdone);
269 TAILQ_INIT(&ki->kaio_bufqueue);
270 TAILQ_INIT(&ki->kaio_liojoblist);
271 TAILQ_INIT(&ki->kaio_sockqueue);
274 while (num_aio_procs < target_aio_procs)
279 * Free a job entry. Wait for completion if it is currently active, but don't
280 * delay forever. If we delay, we return a flag that says that we have to
281 * restart the queue scan.
284 aio_free_entry(struct aiocblist *aiocbe)
287 struct aioproclist *aiop;
288 struct aio_liojob *lj;
293 if (aiocbe->jobstate == JOBST_NULL)
294 panic("aio_free_entry: freeing already free job");
296 p = aiocbe->userproc;
300 panic("aio_free_entry: missing p->p_aioinfo");
302 while (aiocbe->jobstate == JOBST_JOBRUNNING) {
303 if (aiocbe->jobflags & AIOCBLIST_ASYNCFREE)
305 aiocbe->jobflags |= AIOCBLIST_RUNDOWN;
306 tsleep(aiocbe, PRIBIO, "jobwai", 0);
308 aiocbe->jobflags &= ~AIOCBLIST_ASYNCFREE;
310 if (aiocbe->bp == NULL) {
311 if (ki->kaio_queue_count <= 0)
312 panic("aio_free_entry: process queue size <= 0");
313 if (num_queue_count <= 0)
314 panic("aio_free_entry: system wide queue size <= 0");
317 lj->lioj_queue_count--;
318 if (aiocbe->jobflags & AIOCBLIST_DONE)
319 lj->lioj_queue_finished_count--;
321 ki->kaio_queue_count--;
322 if (aiocbe->jobflags & AIOCBLIST_DONE)
323 ki->kaio_queue_finished_count--;
327 lj->lioj_buffer_count--;
328 if (aiocbe->jobflags & AIOCBLIST_DONE)
329 lj->lioj_buffer_finished_count--;
331 if (aiocbe->jobflags & AIOCBLIST_DONE)
332 ki->kaio_buffer_finished_count--;
333 ki->kaio_buffer_count--;
337 /* aiocbe is going away, we need to destroy any knotes */
338 knote_remove(p, &aiocbe->klist);
340 if ((ki->kaio_flags & KAIO_WAKEUP) || ((ki->kaio_flags & KAIO_RUNDOWN)
341 && ((ki->kaio_buffer_count == 0) && (ki->kaio_queue_count == 0)))) {
342 ki->kaio_flags &= ~KAIO_WAKEUP;
346 if (aiocbe->jobstate == JOBST_JOBQBUF) {
347 if ((error = aio_fphysio(p, aiocbe)) != 0)
349 if (aiocbe->jobstate != JOBST_JOBBFINISHED)
350 panic("aio_free_entry: invalid physio finish-up state");
352 TAILQ_REMOVE(&ki->kaio_bufdone, aiocbe, plist);
354 } else if (aiocbe->jobstate == JOBST_JOBQPROC) {
355 aiop = aiocbe->jobaioproc;
356 TAILQ_REMOVE(&aiop->jobtorun, aiocbe, list);
357 } else if (aiocbe->jobstate == JOBST_JOBQGLOBAL) {
358 TAILQ_REMOVE(&aio_jobs, aiocbe, list);
359 TAILQ_REMOVE(&ki->kaio_jobqueue, aiocbe, plist);
360 } else if (aiocbe->jobstate == JOBST_JOBFINISHED)
361 TAILQ_REMOVE(&ki->kaio_jobdone, aiocbe, plist);
362 else if (aiocbe->jobstate == JOBST_JOBBFINISHED) {
364 TAILQ_REMOVE(&ki->kaio_bufdone, aiocbe, plist);
367 vunmapbuf(aiocbe->bp);
368 relpbuf(aiocbe->bp, NULL);
372 if (lj && (lj->lioj_buffer_count == 0) && (lj->lioj_queue_count == 0)) {
373 TAILQ_REMOVE(&ki->kaio_liojoblist, lj, lioj_list);
374 zfree(aiolio_zone, lj);
376 aiocbe->jobstate = JOBST_NULL;
377 untimeout(process_signal, aiocbe, aiocbe->timeouthandle);
378 zfree(aiocb_zone, aiocbe);
384 * Rundown the jobs for a given process.
387 aio_proc_rundown(struct proc *p)
394 struct aio_liojob *lj, *ljn;
395 struct aiocblist *aiocbe, *aiocbn;
397 struct filedesc *fdp;
404 ki->kaio_flags |= LIOJ_SIGNAL_POSTED;
405 while ((ki->kaio_active_count > 0) || (ki->kaio_buffer_count >
406 ki->kaio_buffer_finished_count)) {
407 ki->kaio_flags |= KAIO_RUNDOWN;
408 if (tsleep(p, PRIBIO, "kaiowt", aiod_timeout))
413 * Move any aio ops that are waiting on socket I/O to the normal job
414 * queues so they are cleaned up with any others.
419 for (aiocbe = TAILQ_FIRST(&ki->kaio_sockqueue); aiocbe; aiocbe =
421 aiocbn = TAILQ_NEXT(aiocbe, plist);
422 fp = fdp->fd_ofiles[aiocbe->uaiocb.aio_fildes];
425 * Under some circumstances, the aio_fildes and the file
426 * structure don't match. This would leave aiocbe's in the
427 * TAILQ associated with the socket and cause a panic later.
431 if ((fp == NULL) || (fp != aiocbe->fd_file))
432 fp = aiocbe->fd_file;
434 so = (struct socket *)fp->f_data;
435 TAILQ_REMOVE(&so->so_aiojobq, aiocbe, list);
436 if (TAILQ_EMPTY(&so->so_aiojobq)) {
437 so->so_snd.sb_flags &= ~SB_AIO;
438 so->so_rcv.sb_flags &= ~SB_AIO;
441 TAILQ_REMOVE(&ki->kaio_sockqueue, aiocbe, plist);
442 TAILQ_INSERT_HEAD(&aio_jobs, aiocbe, list);
443 TAILQ_INSERT_HEAD(&ki->kaio_jobqueue, aiocbe, plist);
448 for (aiocbe = TAILQ_FIRST(&ki->kaio_jobdone); aiocbe; aiocbe = aiocbn) {
449 aiocbn = TAILQ_NEXT(aiocbe, plist);
450 if (aio_free_entry(aiocbe))
455 for (aiocbe = TAILQ_FIRST(&ki->kaio_jobqueue); aiocbe; aiocbe =
457 aiocbn = TAILQ_NEXT(aiocbe, plist);
458 if (aio_free_entry(aiocbe))
463 * Note the use of lots of splbio here, trying to avoid splbio for long chains
464 * of I/O. Probably unnecessary.
468 while (TAILQ_FIRST(&ki->kaio_bufqueue)) {
469 ki->kaio_flags |= KAIO_WAKEUP;
470 tsleep(p, PRIBIO, "aioprn", 0);
478 for (aiocbe = TAILQ_FIRST(&ki->kaio_bufdone); aiocbe; aiocbe = aiocbn) {
479 aiocbn = TAILQ_NEXT(aiocbe, plist);
480 if (aio_free_entry(aiocbe)) {
488 * If we've slept, jobs might have moved from one queue to another.
489 * Retry rundown if we didn't manage to empty the queues.
491 if (TAILQ_FIRST(&ki->kaio_jobdone) != NULL ||
492 TAILQ_FIRST(&ki->kaio_jobqueue) != NULL ||
493 TAILQ_FIRST(&ki->kaio_bufqueue) != NULL ||
494 TAILQ_FIRST(&ki->kaio_bufdone) != NULL)
497 for (lj = TAILQ_FIRST(&ki->kaio_liojoblist); lj; lj = ljn) {
498 ljn = TAILQ_NEXT(lj, lioj_list);
499 if ((lj->lioj_buffer_count == 0) && (lj->lioj_queue_count ==
501 TAILQ_REMOVE(&ki->kaio_liojoblist, lj, lioj_list);
502 zfree(aiolio_zone, lj);
505 printf("LIO job not cleaned up: B:%d, BF:%d, Q:%d, "
506 "QF:%d\n", lj->lioj_buffer_count,
507 lj->lioj_buffer_finished_count,
508 lj->lioj_queue_count,
509 lj->lioj_queue_finished_count);
514 zfree(kaio_zone, ki);
521 * Select a job to run (called by an AIO daemon).
523 static struct aiocblist *
524 aio_selectjob(struct aioproclist *aiop)
527 struct aiocblist *aiocbe;
531 aiocbe = TAILQ_FIRST(&aiop->jobtorun);
533 TAILQ_REMOVE(&aiop->jobtorun, aiocbe, list);
538 for (aiocbe = TAILQ_FIRST(&aio_jobs); aiocbe; aiocbe =
539 TAILQ_NEXT(aiocbe, list)) {
540 userp = aiocbe->userproc;
541 ki = userp->p_aioinfo;
543 if (ki->kaio_active_count < ki->kaio_maxactive_count) {
544 TAILQ_REMOVE(&aio_jobs, aiocbe, list);
555 * The AIO processing activity. This is the code that does the I/O request for
556 * the non-physio version of the operations. The normal vn operations are used,
557 * and this code should work in all instances for every type of file, including
558 * pipes, sockets, fifos, and regular files.
561 aio_process(struct aiocblist *aiocbe)
563 struct filedesc *fdp;
564 struct proc *userp, *mycp;
573 int oublock_st, oublock_end;
574 int inblock_st, inblock_end;
576 userp = aiocbe->userproc;
577 cb = &aiocbe->uaiocb;
583 fp = fdp->fd_ofiles[fd];
585 if ((fp == NULL) || (fp != aiocbe->fd_file)) {
586 cb->_aiocb_private.error = EBADF;
587 cb->_aiocb_private.status = -1;
591 aiov.iov_base = (void *)cb->aio_buf;
592 aiov.iov_len = cb->aio_nbytes;
594 auio.uio_iov = &aiov;
596 auio.uio_offset = offset = cb->aio_offset;
597 auio.uio_resid = cb->aio_nbytes;
598 cnt = cb->aio_nbytes;
599 auio.uio_segflg = UIO_USERSPACE;
600 auio.uio_procp = mycp;
602 inblock_st = mycp->p_stats->p_ru.ru_inblock;
603 oublock_st = mycp->p_stats->p_ru.ru_oublock;
605 * Temporarily bump the ref count while reading to avoid the
606 * descriptor being ripped out from under us.
609 if (cb->aio_lio_opcode == LIO_READ) {
610 auio.uio_rw = UIO_READ;
611 error = fo_read(fp, &auio, fp->f_cred, FOF_OFFSET, mycp);
613 auio.uio_rw = UIO_WRITE;
614 error = fo_write(fp, &auio, fp->f_cred, FOF_OFFSET, mycp);
617 inblock_end = mycp->p_stats->p_ru.ru_inblock;
618 oublock_end = mycp->p_stats->p_ru.ru_oublock;
620 aiocbe->inputcharge = inblock_end - inblock_st;
621 aiocbe->outputcharge = oublock_end - oublock_st;
623 if ((error) && (auio.uio_resid != cnt)) {
624 if (error == ERESTART || error == EINTR || error == EWOULDBLOCK)
626 if ((error == EPIPE) && (cb->aio_lio_opcode == LIO_WRITE)) {
628 psignal(userp, SIGPIPE);
633 cnt -= auio.uio_resid;
634 cb->_aiocb_private.error = error;
635 cb->_aiocb_private.status = cnt;
639 * The AIO daemon, most of the actual work is done in aio_process,
640 * but the setup (and address space mgmt) is done in this routine.
643 aio_daemon(void *uproc)
646 struct aio_liojob *lj;
648 struct aiocblist *aiocbe;
649 struct aioproclist *aiop;
651 struct proc *curcp, *mycp, *userp;
652 struct vmspace *myvm, *tmpvm;
656 * Local copies of curproc (cp) and vmspace (myvm)
659 myvm = mycp->p_vmspace;
661 if (mycp->p_textvp) {
662 vrele(mycp->p_textvp);
663 mycp->p_textvp = NULL;
667 * Allocate and ready the aio control info. There is one aiop structure
670 aiop = zalloc(aiop_zone);
671 aiop->aioproc = mycp;
672 aiop->aioprocflags |= AIOP_FREE;
673 TAILQ_INIT(&aiop->jobtorun);
678 * Place thread (lightweight process) onto the AIO free thread list.
680 if (TAILQ_EMPTY(&aio_freeproc))
681 wakeup(&aio_freeproc);
682 TAILQ_INSERT_HEAD(&aio_freeproc, aiop, list);
687 * Get rid of our current filedescriptors. AIOD's don't need any
688 * filedescriptors, except as temporarily inherited from the client.
689 * Credentials are also cloned, and made equivalent to "root".
693 mycp->p_ucred = crcopy(mycp->p_ucred);
694 mycp->p_ucred->cr_uid = 0;
695 uifree(mycp->p_ucred->cr_uidinfo);
696 mycp->p_ucred->cr_uidinfo = uifind(0);
697 mycp->p_ucred->cr_ngroups = 1;
698 mycp->p_ucred->cr_groups[0] = 1;
700 /* The daemon resides in its own pgrp. */
701 enterpgrp(mycp, mycp->p_pid, 1);
703 /* Mark special process type. */
704 mycp->p_flag |= P_SYSTEM;
707 * Wakeup parent process. (Parent sleeps to keep from blasting away
708 * creating to many daemons.)
714 * curcp is the current daemon process context.
715 * userp is the current user process context.
720 * Take daemon off of free queue
722 if (aiop->aioprocflags & AIOP_FREE) {
724 TAILQ_REMOVE(&aio_freeproc, aiop, list);
725 TAILQ_INSERT_TAIL(&aio_activeproc, aiop, list);
726 aiop->aioprocflags &= ~AIOP_FREE;
729 aiop->aioprocflags &= ~AIOP_SCHED;
734 while ((aiocbe = aio_selectjob(aiop)) != NULL) {
735 cb = &aiocbe->uaiocb;
736 userp = aiocbe->userproc;
738 aiocbe->jobstate = JOBST_JOBRUNNING;
741 * Connect to process address space for user program.
743 if (userp != curcp) {
745 * Save the current address space that we are
748 tmpvm = mycp->p_vmspace;
751 * Point to the new user address space, and
754 mycp->p_vmspace = userp->p_vmspace;
755 mycp->p_vmspace->vm_refcnt++;
757 /* Activate the new mapping. */
761 * If the old address space wasn't the daemons
762 * own address space, then we need to remove the
763 * daemon's reference from the other process
764 * that it was acting on behalf of.
771 * Disassociate from previous clients file
772 * descriptors, and associate to the new clients
773 * descriptors. Note that the daemon doesn't
774 * need to worry about its orginal descriptors,
775 * because they were originally freed.
779 mycp->p_fd = fdshare(userp);
783 ki = userp->p_aioinfo;
786 /* Account for currently active jobs. */
787 ki->kaio_active_count++;
789 /* Do the I/O function. */
790 aiocbe->jobaioproc = aiop;
793 /* Decrement the active job count. */
794 ki->kaio_active_count--;
797 * Increment the completion count for wakeup/signal
800 aiocbe->jobflags |= AIOCBLIST_DONE;
801 ki->kaio_queue_finished_count++;
803 lj->lioj_queue_finished_count++;
804 if ((ki->kaio_flags & KAIO_WAKEUP) || ((ki->kaio_flags
805 & KAIO_RUNDOWN) && (ki->kaio_active_count == 0))) {
806 ki->kaio_flags &= ~KAIO_WAKEUP;
811 if (lj && (lj->lioj_flags &
812 (LIOJ_SIGNAL|LIOJ_SIGNAL_POSTED)) == LIOJ_SIGNAL) {
813 if ((lj->lioj_queue_finished_count ==
814 lj->lioj_queue_count) &&
815 (lj->lioj_buffer_finished_count ==
816 lj->lioj_buffer_count)) {
819 lj->lioj_signal.sigev_signo);
821 lj->lioj_flags |= LIOJ_SIGNAL_POSTED;
826 aiocbe->jobstate = JOBST_JOBFINISHED;
829 * If the I/O request should be automatically rundown,
830 * do the needed cleanup. Otherwise, place the queue
831 * entry for the just finished I/O request into the done
832 * queue for the associated client.
835 if (aiocbe->jobflags & AIOCBLIST_ASYNCFREE) {
836 aiocbe->jobflags &= ~AIOCBLIST_ASYNCFREE;
837 zfree(aiocb_zone, aiocbe);
839 TAILQ_REMOVE(&ki->kaio_jobqueue, aiocbe, plist);
840 TAILQ_INSERT_TAIL(&ki->kaio_jobdone, aiocbe,
844 KNOTE(&aiocbe->klist, 0);
846 if (aiocbe->jobflags & AIOCBLIST_RUNDOWN) {
848 aiocbe->jobflags &= ~AIOCBLIST_RUNDOWN;
851 if (cb->aio_sigevent.sigev_notify == SIGEV_SIGNAL) {
853 psignal(userp, cb->aio_sigevent.sigev_signo);
859 * Disconnect from user address space.
862 /* Get the user address space to disconnect from. */
863 tmpvm = mycp->p_vmspace;
865 /* Get original address space for daemon. */
866 mycp->p_vmspace = myvm;
868 /* Activate the daemon's address space. */
872 printf("AIOD: vmspace problem -- %d\n",
876 /* Remove our vmspace reference. */
880 * Disassociate from the user process's file
890 * If we are the first to be put onto the free queue, wakeup
891 * anyone waiting for a daemon.
894 TAILQ_REMOVE(&aio_activeproc, aiop, list);
895 if (TAILQ_EMPTY(&aio_freeproc))
896 wakeup(&aio_freeproc);
897 TAILQ_INSERT_HEAD(&aio_freeproc, aiop, list);
898 aiop->aioprocflags |= AIOP_FREE;
902 * If daemon is inactive for a long time, allow it to exit,
903 * thereby freeing resources.
905 if (((aiop->aioprocflags & AIOP_SCHED) == 0) && tsleep(mycp,
906 PRIBIO, "aiordy", aiod_lifetime)) {
908 if ((TAILQ_FIRST(&aio_jobs) == NULL) &&
909 (TAILQ_FIRST(&aiop->jobtorun) == NULL)) {
910 if ((aiop->aioprocflags & AIOP_FREE) &&
911 (num_aio_procs > target_aio_procs)) {
912 TAILQ_REMOVE(&aio_freeproc, aiop, list);
914 zfree(aiop_zone, aiop);
917 if (mycp->p_vmspace->vm_refcnt <= 1) {
918 printf("AIOD: bad vm refcnt for"
919 " exiting daemon: %d\n",
920 mycp->p_vmspace->vm_refcnt);
932 * Create a new AIO daemon. This is mostly a kernel-thread fork routine. The
933 * AIO daemon modifies its environment itself.
941 error = kthread_create(aio_daemon, curproc, &p, RFNOWAIT, "aiod%d",
947 * Wait until daemon is started, but continue on just in case to
948 * handle error conditions.
950 error = tsleep(p, PZERO, "aiosta", aiod_timeout);
958 * Try the high-performance, low-overhead physio method for eligible
959 * VCHR devices. This method doesn't use an aio helper thread, and
960 * thus has very low overhead.
962 * Assumes that the caller, _aio_aqueue(), has incremented the file
963 * structure's reference count, preventing its deallocation for the
964 * duration of this call.
967 aio_qphysio(struct proc *p, struct aiocblist *aiocbe)
975 struct filedesc *fdp;
976 struct aio_liojob *lj;
981 cb = &aiocbe->uaiocb;
984 fp = fdp->fd_ofiles[fd];
986 if (fp->f_type != DTYPE_VNODE)
989 vp = (struct vnode *)fp->f_data;
992 * If its not a disk, we don't want to return a positive error.
993 * It causes the aio code to not fall through to try the thread
994 * way when you're talking to a regular file.
996 if (!vn_isdisk(vp, &error)) {
997 if (error == ENOTBLK)
1003 if (cb->aio_nbytes % vp->v_rdev->si_bsize_phys)
1006 if (cb->aio_nbytes >
1007 MAXPHYS - (((vm_offset_t) cb->aio_buf) & PAGE_MASK))
1011 if (ki->kaio_buffer_count >= ki->kaio_ballowed_count)
1014 ki->kaio_buffer_count++;
1018 lj->lioj_buffer_count++;
1020 /* Create and build a buffer header for a transfer. */
1021 bp = (struct buf *)getpbuf(NULL);
1025 * Get a copy of the kva from the physical buffer.
1028 bp->b_dev = vp->v_rdev;
1029 error = bp->b_error = 0;
1031 bp->b_bcount = cb->aio_nbytes;
1032 bp->b_bufsize = cb->aio_nbytes;
1033 bp->b_flags = B_PHYS;
1034 bp->b_iodone = aio_physwakeup;
1035 bp->b_saveaddr = bp->b_data;
1036 bp->b_data = (void *)cb->aio_buf;
1037 bp->b_blkno = btodb(cb->aio_offset);
1039 if (cb->aio_lio_opcode == LIO_WRITE) {
1040 bp->b_iocmd = BIO_WRITE;
1041 if (!useracc(bp->b_data, bp->b_bufsize, VM_PROT_READ)) {
1046 bp->b_iocmd = BIO_READ;
1047 if (!useracc(bp->b_data, bp->b_bufsize, VM_PROT_WRITE)) {
1053 /* Bring buffer into kernel space. */
1058 bp->b_spc = (void *)aiocbe;
1059 TAILQ_INSERT_TAIL(&aio_bufjobs, aiocbe, list);
1060 TAILQ_INSERT_TAIL(&ki->kaio_bufqueue, aiocbe, plist);
1061 aiocbe->jobstate = JOBST_JOBQBUF;
1062 cb->_aiocb_private.status = cb->aio_nbytes;
1068 /* Perform transfer. */
1069 DEV_STRATEGY(bp, 0);
1075 * If we had an error invoking the request, or an error in processing
1076 * the request before we have returned, we process it as an error in
1077 * transfer. Note that such an I/O error is not indicated immediately,
1078 * but is returned using the aio_error mechanism. In this case,
1079 * aio_suspend will return immediately.
1081 if (bp->b_error || (bp->b_ioflags & BIO_ERROR)) {
1082 struct aiocb *job = aiocbe->uuaiocb;
1084 aiocbe->uaiocb._aiocb_private.status = 0;
1085 suword(&job->_aiocb_private.status, 0);
1086 aiocbe->uaiocb._aiocb_private.error = bp->b_error;
1087 suword(&job->_aiocb_private.error, bp->b_error);
1089 ki->kaio_buffer_finished_count++;
1091 if (aiocbe->jobstate != JOBST_JOBBFINISHED) {
1092 aiocbe->jobstate = JOBST_JOBBFINISHED;
1093 aiocbe->jobflags |= AIOCBLIST_DONE;
1094 TAILQ_REMOVE(&aio_bufjobs, aiocbe, list);
1095 TAILQ_REMOVE(&ki->kaio_bufqueue, aiocbe, plist);
1096 TAILQ_INSERT_TAIL(&ki->kaio_bufdone, aiocbe, plist);
1102 KNOTE(&aiocbe->klist, 0);
1106 ki->kaio_buffer_count--;
1108 lj->lioj_buffer_count--;
1115 * This waits/tests physio completion.
1118 aio_fphysio(struct proc *p, struct aiocblist *iocb)
1127 while ((bp->b_flags & B_DONE) == 0) {
1128 if (tsleep(bp, PRIBIO, "physstr", aiod_timeout)) {
1129 if ((bp->b_flags & B_DONE) == 0) {
1138 /* Release mapping into kernel space. */
1144 /* Check for an error. */
1145 if (bp->b_ioflags & BIO_ERROR)
1146 error = bp->b_error;
1151 #endif /* VFS_AIO */
1154 * Wake up aio requests that may be serviceable now.
1157 aio_swake(struct socket *so, struct sockbuf *sb)
1162 struct aiocblist *cb,*cbn;
1164 struct kaioinfo *ki = NULL;
1165 int opcode, wakecount = 0;
1166 struct aioproclist *aiop;
1168 if (sb == &so->so_snd) {
1170 so->so_snd.sb_flags &= ~SB_AIO;
1173 so->so_rcv.sb_flags &= ~SB_AIO;
1176 for (cb = TAILQ_FIRST(&so->so_aiojobq); cb; cb = cbn) {
1177 cbn = TAILQ_NEXT(cb, list);
1178 if (opcode == cb->uaiocb.aio_lio_opcode) {
1181 TAILQ_REMOVE(&so->so_aiojobq, cb, list);
1182 TAILQ_REMOVE(&ki->kaio_sockqueue, cb, plist);
1183 TAILQ_INSERT_TAIL(&aio_jobs, cb, list);
1184 TAILQ_INSERT_TAIL(&ki->kaio_jobqueue, cb, plist);
1186 if (cb->jobstate != JOBST_JOBQGLOBAL)
1187 panic("invalid queue value");
1191 while (wakecount--) {
1192 if ((aiop = TAILQ_FIRST(&aio_freeproc)) != 0) {
1193 TAILQ_REMOVE(&aio_freeproc, aiop, list);
1194 TAILQ_INSERT_TAIL(&aio_activeproc, aiop, list);
1195 aiop->aioprocflags &= ~AIOP_FREE;
1196 wakeup(aiop->aioproc);
1199 #endif /* VFS_AIO */
1204 * Queue a new AIO request. Choosing either the threaded or direct physio VCHR
1205 * technique is done in this code.
1208 _aio_aqueue(struct proc *p, struct aiocb *job, struct aio_liojob *lj, int type)
1210 struct filedesc *fdp;
1217 struct aiocblist *aiocbe;
1218 struct aioproclist *aiop;
1219 struct kaioinfo *ki;
1224 aiocbe = zalloc(aiocb_zone);
1225 aiocbe->inputcharge = 0;
1226 aiocbe->outputcharge = 0;
1227 callout_handle_init(&aiocbe->timeouthandle);
1228 SLIST_INIT(&aiocbe->klist);
1230 suword(&job->_aiocb_private.status, -1);
1231 suword(&job->_aiocb_private.error, 0);
1232 suword(&job->_aiocb_private.kernelinfo, -1);
1234 error = copyin(job, &aiocbe->uaiocb, sizeof(aiocbe->uaiocb));
1236 suword(&job->_aiocb_private.error, error);
1237 zfree(aiocb_zone, aiocbe);
1240 if (aiocbe->uaiocb.aio_sigevent.sigev_notify == SIGEV_SIGNAL &&
1241 !_SIG_VALID(aiocbe->uaiocb.aio_sigevent.sigev_signo)) {
1242 zfree(aiocb_zone, aiocbe);
1246 /* Save userspace address of the job info. */
1247 aiocbe->uuaiocb = job;
1249 /* Get the opcode. */
1250 if (type != LIO_NOP)
1251 aiocbe->uaiocb.aio_lio_opcode = type;
1252 opcode = aiocbe->uaiocb.aio_lio_opcode;
1254 /* Get the fd info for process. */
1258 * Range check file descriptor.
1260 fd = aiocbe->uaiocb.aio_fildes;
1261 if (fd >= fdp->fd_nfiles) {
1262 zfree(aiocb_zone, aiocbe);
1264 suword(&job->_aiocb_private.error, EBADF);
1268 fp = aiocbe->fd_file = fdp->fd_ofiles[fd];
1269 if ((fp == NULL) || ((opcode == LIO_WRITE) && ((fp->f_flag & FWRITE) ==
1271 zfree(aiocb_zone, aiocbe);
1273 suword(&job->_aiocb_private.error, EBADF);
1277 if (aiocbe->uaiocb.aio_offset == -1LL) {
1278 zfree(aiocb_zone, aiocbe);
1280 suword(&job->_aiocb_private.error, EINVAL);
1284 error = suword(&job->_aiocb_private.kernelinfo, jobrefid);
1286 zfree(aiocb_zone, aiocbe);
1288 suword(&job->_aiocb_private.error, EINVAL);
1292 aiocbe->uaiocb._aiocb_private.kernelinfo = (void *)(intptr_t)jobrefid;
1293 if (jobrefid == LONG_MAX)
1298 if (opcode == LIO_NOP) {
1299 zfree(aiocb_zone, aiocbe);
1301 suword(&job->_aiocb_private.error, 0);
1302 suword(&job->_aiocb_private.status, 0);
1303 suword(&job->_aiocb_private.kernelinfo, 0);
1308 if ((opcode != LIO_READ) && (opcode != LIO_WRITE)) {
1309 zfree(aiocb_zone, aiocbe);
1311 suword(&job->_aiocb_private.status, 0);
1312 suword(&job->_aiocb_private.error, EINVAL);
1319 if (aiocbe->uaiocb.aio_sigevent.sigev_notify == SIGEV_KEVENT) {
1320 kev.ident = aiocbe->uaiocb.aio_sigevent.sigev_notify_kqueue;
1321 kev.udata = aiocbe->uaiocb.aio_sigevent.sigev_value.sigval_ptr;
1325 * This method for requesting kevent-based notification won't
1326 * work on the alpha, since we're passing in a pointer
1327 * via aio_lio_opcode, which is an int. Use the SIGEV_KEVENT-
1328 * based method instead.
1330 struct kevent *kevp;
1332 kevp = (struct kevent *)job->aio_lio_opcode;
1336 error = copyin(kevp, &kev, sizeof(kev));
1340 if ((u_int)kev.ident >= fdp->fd_nfiles ||
1341 (kq_fp = fdp->fd_ofiles[kev.ident]) == NULL ||
1342 (kq_fp->f_type != DTYPE_KQUEUE)) {
1346 kq = (struct kqueue *)kq_fp->f_data;
1347 kev.ident = (uintptr_t)aiocbe;
1348 kev.filter = EVFILT_AIO;
1349 kev.flags = EV_ADD | EV_ENABLE | EV_FLAG1;
1350 error = kqueue_register(kq, &kev, p);
1353 zfree(aiocb_zone, aiocbe);
1355 suword(&job->_aiocb_private.error, error);
1360 suword(&job->_aiocb_private.error, EINPROGRESS);
1361 aiocbe->uaiocb._aiocb_private.error = EINPROGRESS;
1362 aiocbe->userproc = p;
1363 aiocbe->jobflags = 0;
1367 if (fp->f_type == DTYPE_SOCKET) {
1369 * Alternate queueing for socket ops: Reach down into the
1370 * descriptor to get the socket data. Then check to see if the
1371 * socket is ready to be read or written (based on the requested
1374 * If it is not ready for io, then queue the aiocbe on the
1375 * socket, and set the flags so we get a call when sbnotify()
1378 so = (struct socket *)fp->f_data;
1380 if (((opcode == LIO_READ) && (!soreadable(so))) || ((opcode ==
1381 LIO_WRITE) && (!sowriteable(so)))) {
1382 TAILQ_INSERT_TAIL(&so->so_aiojobq, aiocbe, list);
1383 TAILQ_INSERT_TAIL(&ki->kaio_sockqueue, aiocbe, plist);
1384 if (opcode == LIO_READ)
1385 so->so_rcv.sb_flags |= SB_AIO;
1387 so->so_snd.sb_flags |= SB_AIO;
1388 aiocbe->jobstate = JOBST_JOBQGLOBAL; /* XXX */
1389 ki->kaio_queue_count++;
1398 if ((error = aio_qphysio(p, aiocbe)) == 0)
1401 suword(&job->_aiocb_private.status, 0);
1402 aiocbe->uaiocb._aiocb_private.error = error;
1403 suword(&job->_aiocb_private.error, error);
1407 /* No buffer for daemon I/O. */
1410 ki->kaio_queue_count++;
1412 lj->lioj_queue_count++;
1414 TAILQ_INSERT_TAIL(&ki->kaio_jobqueue, aiocbe, plist);
1415 TAILQ_INSERT_TAIL(&aio_jobs, aiocbe, list);
1417 aiocbe->jobstate = JOBST_JOBQGLOBAL;
1423 * If we don't have a free AIO process, and we are below our quota, then
1424 * start one. Otherwise, depend on the subsequent I/O completions to
1425 * pick-up this job. If we don't sucessfully create the new process
1426 * (thread) due to resource issues, we return an error for now (EAGAIN),
1427 * which is likely not the correct thing to do.
1431 if ((aiop = TAILQ_FIRST(&aio_freeproc)) != NULL) {
1432 TAILQ_REMOVE(&aio_freeproc, aiop, list);
1433 TAILQ_INSERT_TAIL(&aio_activeproc, aiop, list);
1434 aiop->aioprocflags &= ~AIOP_FREE;
1435 wakeup(aiop->aioproc);
1436 } else if (((num_aio_resv_start + num_aio_procs) < max_aio_procs) &&
1437 ((ki->kaio_active_count + num_aio_resv_start) <
1438 ki->kaio_maxactive_count)) {
1439 num_aio_resv_start++;
1440 if ((error = aio_newproc()) == 0) {
1441 num_aio_resv_start--;
1445 num_aio_resv_start--;
1454 * This routine queues an AIO request, checking for quotas.
1457 aio_aqueue(struct proc *p, struct aiocb *job, int type)
1459 struct kaioinfo *ki;
1461 if (p->p_aioinfo == NULL)
1462 aio_init_aioinfo(p);
1464 if (num_queue_count >= max_queue_count)
1468 if (ki->kaio_queue_count >= ki->kaio_qallowed_count)
1471 return _aio_aqueue(p, job, NULL, type);
1473 #endif /* VFS_AIO */
1476 * Support the aio_return system call, as a side-effect, kernel resources are
1480 aio_return(struct proc *p, struct aio_return_args *uap)
1487 struct aiocblist *cb, *ncb;
1489 struct kaioinfo *ki;
1497 jobref = fuword(&ujob->_aiocb_private.kernelinfo);
1498 if (jobref == -1 || jobref == 0)
1502 for (cb = TAILQ_FIRST(&ki->kaio_jobdone); cb; cb = TAILQ_NEXT(cb,
1504 if (((intptr_t) cb->uaiocb._aiocb_private.kernelinfo) ==
1507 if (ujob == cb->uuaiocb) {
1509 cb->uaiocb._aiocb_private.status;
1511 p->p_retval[0] = EFAULT;
1512 if (cb->uaiocb.aio_lio_opcode == LIO_WRITE) {
1513 curproc->p_stats->p_ru.ru_oublock +=
1515 cb->outputcharge = 0;
1516 } else if (cb->uaiocb.aio_lio_opcode == LIO_READ) {
1517 curproc->p_stats->p_ru.ru_inblock +=
1519 cb->inputcharge = 0;
1528 for (cb = TAILQ_FIRST(&ki->kaio_bufdone); cb; cb = ncb) {
1529 ncb = TAILQ_NEXT(cb, plist);
1530 if (((intptr_t) cb->uaiocb._aiocb_private.kernelinfo)
1533 if (ujob == cb->uuaiocb) {
1535 cb->uaiocb._aiocb_private.status;
1537 p->p_retval[0] = EFAULT;
1545 #endif /* VFS_AIO */
1549 * Allow a process to wakeup when any of the I/O requests are completed.
1552 aio_suspend(struct proc *p, struct aio_suspend_args *uap)
1559 struct aiocb *const *cbptr, *cbp;
1560 struct kaioinfo *ki;
1561 struct aiocblist *cb;
1566 struct aiocb **ujoblist;
1568 if (uap->nent >= AIO_LISTIO_MAX)
1573 /* Get timespec struct. */
1574 if ((error = copyin(uap->timeout, &ts, sizeof(ts))) != 0)
1577 if (ts.tv_nsec < 0 || ts.tv_nsec >= 1000000000)
1580 TIMESPEC_TO_TIMEVAL(&atv, &ts);
1581 if (itimerfix(&atv))
1583 timo = tvtohz(&atv);
1591 ijoblist = zalloc(aiol_zone);
1592 ujoblist = zalloc(aiol_zone);
1593 cbptr = uap->aiocbp;
1595 for (i = 0; i < uap->nent; i++) {
1596 cbp = (struct aiocb *)(intptr_t)fuword((caddr_t)&cbptr[i]);
1599 ujoblist[njoblist] = cbp;
1600 ijoblist[njoblist] = fuword(&cbp->_aiocb_private.kernelinfo);
1604 if (njoblist == 0) {
1605 zfree(aiol_zone, ijoblist);
1606 zfree(aiol_zone, ujoblist);
1612 for (cb = TAILQ_FIRST(&ki->kaio_jobdone); cb; cb =
1613 TAILQ_NEXT(cb, plist)) {
1614 for (i = 0; i < njoblist; i++) {
1616 cb->uaiocb._aiocb_private.kernelinfo) ==
1618 if (ujoblist[i] != cb->uuaiocb)
1620 zfree(aiol_zone, ijoblist);
1621 zfree(aiol_zone, ujoblist);
1628 for (cb = TAILQ_FIRST(&ki->kaio_bufdone); cb; cb =
1629 TAILQ_NEXT(cb, plist)) {
1630 for (i = 0; i < njoblist; i++) {
1632 cb->uaiocb._aiocb_private.kernelinfo) ==
1635 if (ujoblist[i] != cb->uuaiocb)
1637 zfree(aiol_zone, ijoblist);
1638 zfree(aiol_zone, ujoblist);
1644 ki->kaio_flags |= KAIO_WAKEUP;
1645 error = tsleep(p, PRIBIO | PCATCH, "aiospn", timo);
1648 if (error == ERESTART || error == EINTR) {
1649 zfree(aiol_zone, ijoblist);
1650 zfree(aiol_zone, ujoblist);
1652 } else if (error == EWOULDBLOCK) {
1653 zfree(aiol_zone, ijoblist);
1654 zfree(aiol_zone, ujoblist);
1661 #endif /* VFS_AIO */
1665 * aio_cancel cancels any non-physio aio operations not currently in
1669 aio_cancel(struct proc *p, struct aio_cancel_args *uap)
1674 struct kaioinfo *ki;
1675 struct aiocblist *cbe, *cbn;
1677 struct filedesc *fdp;
1687 fp = fdp->fd_ofiles[uap->fd];
1693 if (fp->f_type == DTYPE_VNODE) {
1694 vp = (struct vnode *)fp->f_data;
1696 if (vn_isdisk(vp,&error)) {
1697 p->p_retval[0] = AIO_NOTCANCELED;
1700 } else if (fp->f_type == DTYPE_SOCKET) {
1701 so = (struct socket *)fp->f_data;
1705 for (cbe = TAILQ_FIRST(&so->so_aiojobq); cbe; cbe = cbn) {
1706 cbn = TAILQ_NEXT(cbe, list);
1707 if ((uap->aiocbp == NULL) ||
1708 (uap->aiocbp == cbe->uuaiocb) ) {
1711 TAILQ_REMOVE(&so->so_aiojobq, cbe, list);
1712 TAILQ_REMOVE(&ki->kaio_sockqueue, cbe, plist);
1713 TAILQ_INSERT_TAIL(&ki->kaio_jobdone, cbe, plist);
1714 if (ki->kaio_flags & KAIO_WAKEUP) {
1717 cbe->jobstate = JOBST_JOBFINISHED;
1718 cbe->uaiocb._aiocb_private.status=-1;
1719 cbe->uaiocb._aiocb_private.error=ECANCELED;
1721 /* XXX cancelled, knote? */
1722 if (cbe->uaiocb.aio_sigevent.sigev_notify ==
1724 PROC_LOCK(cbe->userproc);
1725 psignal(cbe->userproc, cbe->uaiocb.aio_sigevent.sigev_signo);
1726 PROC_UNLOCK(cbe->userproc);
1735 if ((cancelled) && (uap->aiocbp)) {
1736 p->p_retval[0] = AIO_CANCELED;
1746 for (cbe = TAILQ_FIRST(&ki->kaio_jobqueue); cbe; cbe = cbn) {
1747 cbn = TAILQ_NEXT(cbe, plist);
1749 if ((uap->fd == cbe->uaiocb.aio_fildes) &&
1750 ((uap->aiocbp == NULL ) ||
1751 (uap->aiocbp == cbe->uuaiocb))) {
1753 if (cbe->jobstate == JOBST_JOBQGLOBAL) {
1754 TAILQ_REMOVE(&aio_jobs, cbe, list);
1755 TAILQ_REMOVE(&ki->kaio_jobqueue, cbe, plist);
1756 TAILQ_INSERT_TAIL(&ki->kaio_jobdone, cbe,
1759 ki->kaio_queue_finished_count++;
1760 cbe->jobstate = JOBST_JOBFINISHED;
1761 cbe->uaiocb._aiocb_private.status = -1;
1762 cbe->uaiocb._aiocb_private.error = ECANCELED;
1763 /* XXX cancelled, knote? */
1764 if (cbe->uaiocb.aio_sigevent.sigev_notify ==
1766 PROC_LOCK(cbe->userproc);
1767 psignal(cbe->userproc, cbe->uaiocb.aio_sigevent.sigev_signo);
1768 PROC_UNLOCK(cbe->userproc);
1780 p->p_retval[0] = AIO_NOTCANCELED;
1785 p->p_retval[0] = AIO_CANCELED;
1789 p->p_retval[0] = AIO_ALLDONE;
1792 #endif /* VFS_AIO */
1796 * aio_error is implemented in the kernel level for compatibility purposes only.
1797 * For a user mode async implementation, it would be best to do it in a userland
1801 aio_error(struct proc *p, struct aio_error_args *uap)
1807 struct aiocblist *cb;
1808 struct kaioinfo *ki;
1815 jobref = fuword(&uap->aiocbp->_aiocb_private.kernelinfo);
1816 if ((jobref == -1) || (jobref == 0))
1819 for (cb = TAILQ_FIRST(&ki->kaio_jobdone); cb; cb = TAILQ_NEXT(cb,
1821 if (((intptr_t)cb->uaiocb._aiocb_private.kernelinfo) ==
1823 p->p_retval[0] = cb->uaiocb._aiocb_private.error;
1830 for (cb = TAILQ_FIRST(&ki->kaio_jobqueue); cb; cb = TAILQ_NEXT(cb,
1832 if (((intptr_t)cb->uaiocb._aiocb_private.kernelinfo) ==
1834 p->p_retval[0] = EINPROGRESS;
1840 for (cb = TAILQ_FIRST(&ki->kaio_sockqueue); cb; cb = TAILQ_NEXT(cb,
1842 if (((intptr_t)cb->uaiocb._aiocb_private.kernelinfo) ==
1844 p->p_retval[0] = EINPROGRESS;
1852 for (cb = TAILQ_FIRST(&ki->kaio_bufdone); cb; cb = TAILQ_NEXT(cb,
1854 if (((intptr_t)cb->uaiocb._aiocb_private.kernelinfo) ==
1856 p->p_retval[0] = cb->uaiocb._aiocb_private.error;
1862 for (cb = TAILQ_FIRST(&ki->kaio_bufqueue); cb; cb = TAILQ_NEXT(cb,
1864 if (((intptr_t)cb->uaiocb._aiocb_private.kernelinfo) ==
1866 p->p_retval[0] = EINPROGRESS;
1877 status = fuword(&uap->aiocbp->_aiocb_private.status);
1879 return fuword(&uap->aiocbp->_aiocb_private.error);
1882 #endif /* VFS_AIO */
1886 aio_read(struct proc *p, struct aio_read_args *uap)
1891 return aio_aqueue(p, uap->aiocbp, LIO_READ);
1892 #endif /* VFS_AIO */
1896 aio_write(struct proc *p, struct aio_write_args *uap)
1901 return aio_aqueue(p, uap->aiocbp, LIO_WRITE);
1902 #endif /* VFS_AIO */
1906 lio_listio(struct proc *p, struct lio_listio_args *uap)
1911 int nent, nentqueued;
1912 struct aiocb *iocb, * const *cbptr;
1913 struct aiocblist *cb;
1914 struct kaioinfo *ki;
1915 struct aio_liojob *lj;
1916 int error, runningcode;
1921 if ((uap->mode != LIO_NOWAIT) && (uap->mode != LIO_WAIT))
1925 if (nent > AIO_LISTIO_MAX)
1928 if (p->p_aioinfo == NULL)
1929 aio_init_aioinfo(p);
1931 if ((nent + num_queue_count) > max_queue_count)
1935 if ((nent + ki->kaio_queue_count) > ki->kaio_qallowed_count)
1938 lj = zalloc(aiolio_zone);
1943 lj->lioj_buffer_count = 0;
1944 lj->lioj_buffer_finished_count = 0;
1945 lj->lioj_queue_count = 0;
1946 lj->lioj_queue_finished_count = 0;
1952 if (uap->sig && (uap->mode == LIO_NOWAIT)) {
1953 error = copyin(uap->sig, &lj->lioj_signal,
1954 sizeof(lj->lioj_signal));
1956 zfree(aiolio_zone, lj);
1959 if (!_SIG_VALID(lj->lioj_signal.sigev_signo)) {
1960 zfree(aiolio_zone, lj);
1963 lj->lioj_flags |= LIOJ_SIGNAL;
1964 lj->lioj_flags &= ~LIOJ_SIGNAL_POSTED;
1966 lj->lioj_flags &= ~LIOJ_SIGNAL;
1968 TAILQ_INSERT_TAIL(&ki->kaio_liojoblist, lj, lioj_list);
1970 * Get pointers to the list of I/O requests.
1974 cbptr = uap->acb_list;
1975 for (i = 0; i < uap->nent; i++) {
1976 iocb = (struct aiocb *)(intptr_t)fuword((caddr_t)&cbptr[i]);
1977 if (((intptr_t)iocb != -1) && ((intptr_t)iocb != NULL)) {
1978 error = _aio_aqueue(p, iocb, lj, 0);
1987 * If we haven't queued any, then just return error.
1989 if (nentqueued == 0)
1993 * Calculate the appropriate error return.
1999 if (uap->mode == LIO_WAIT) {
2000 int command, found, jobref;
2004 for (i = 0; i < uap->nent; i++) {
2006 * Fetch address of the control buf pointer in
2009 iocb = (struct aiocb *)(intptr_t)fuword((caddr_t)&cbptr[i]);
2010 if (((intptr_t)iocb == -1) || ((intptr_t)iocb
2015 * Fetch the associated command from user space.
2017 command = fuword(&iocb->aio_lio_opcode);
2018 if (command == LIO_NOP) {
2023 jobref = fuword(&iocb->_aiocb_private.kernelinfo);
2025 TAILQ_FOREACH(cb, &ki->kaio_jobdone, plist) {
2026 if (((intptr_t)cb->uaiocb._aiocb_private.kernelinfo)
2028 if (cb->uaiocb.aio_lio_opcode
2030 curproc->p_stats->p_ru.ru_oublock
2033 cb->outputcharge = 0;
2034 } else if (cb->uaiocb.aio_lio_opcode
2036 curproc->p_stats->p_ru.ru_inblock
2038 cb->inputcharge = 0;
2046 TAILQ_FOREACH(cb, &ki->kaio_bufdone, plist) {
2047 if (((intptr_t)cb->uaiocb._aiocb_private.kernelinfo)
2057 * If all I/Os have been disposed of, then we can
2060 if (found == nentqueued)
2063 ki->kaio_flags |= KAIO_WAKEUP;
2064 error = tsleep(p, PRIBIO | PCATCH, "aiospn", 0);
2068 else if (error == EWOULDBLOCK)
2074 #endif /* VFS_AIO */
2079 * This is a weird hack so that we can post a signal. It is safe to do so from
2080 * a timeout routine, but *not* from an interrupt routine.
2083 process_signal(void *aioj)
2085 struct aiocblist *aiocbe = aioj;
2086 struct aio_liojob *lj = aiocbe->lio;
2087 struct aiocb *cb = &aiocbe->uaiocb;
2089 if ((lj) && (lj->lioj_signal.sigev_notify == SIGEV_SIGNAL) &&
2090 (lj->lioj_queue_count == lj->lioj_queue_finished_count)) {
2091 PROC_LOCK(lj->lioj_ki->kaio_p);
2092 psignal(lj->lioj_ki->kaio_p, lj->lioj_signal.sigev_signo);
2093 PROC_UNLOCK(lj->lioj_ki->kaio_p);
2094 lj->lioj_flags |= LIOJ_SIGNAL_POSTED;
2097 if (cb->aio_sigevent.sigev_notify == SIGEV_SIGNAL) {
2098 PROC_LOCK(aiocbe->userproc);
2099 psignal(aiocbe->userproc, cb->aio_sigevent.sigev_signo);
2100 PROC_UNLOCK(aiocbe->userproc);
2105 * Interrupt handler for physio, performs the necessary process wakeups, and
2109 aio_physwakeup(struct buf *bp)
2111 struct aiocblist *aiocbe;
2113 struct kaioinfo *ki;
2114 struct aio_liojob *lj;
2118 aiocbe = (struct aiocblist *)bp->b_spc;
2122 aiocbe->jobstate = JOBST_JOBBFINISHED;
2123 aiocbe->uaiocb._aiocb_private.status -= bp->b_resid;
2124 aiocbe->uaiocb._aiocb_private.error = 0;
2125 aiocbe->jobflags |= AIOCBLIST_DONE;
2127 if (bp->b_ioflags & BIO_ERROR)
2128 aiocbe->uaiocb._aiocb_private.error = bp->b_error;
2132 lj->lioj_buffer_finished_count++;
2135 * wakeup/signal if all of the interrupt jobs are done.
2137 if (lj->lioj_buffer_finished_count ==
2138 lj->lioj_buffer_count) {
2140 * Post a signal if it is called for.
2142 if ((lj->lioj_flags &
2143 (LIOJ_SIGNAL|LIOJ_SIGNAL_POSTED)) ==
2145 lj->lioj_flags |= LIOJ_SIGNAL_POSTED;
2146 aiocbe->timeouthandle =
2147 timeout(process_signal,
2155 ki->kaio_buffer_finished_count++;
2156 TAILQ_REMOVE(&aio_bufjobs, aiocbe, list);
2157 TAILQ_REMOVE(&ki->kaio_bufqueue, aiocbe, plist);
2158 TAILQ_INSERT_TAIL(&ki->kaio_bufdone, aiocbe, plist);
2160 KNOTE(&aiocbe->klist, 0);
2161 /* Do the wakeup. */
2162 if (ki->kaio_flags & (KAIO_RUNDOWN|KAIO_WAKEUP)) {
2163 ki->kaio_flags &= ~KAIO_WAKEUP;
2168 if (aiocbe->uaiocb.aio_sigevent.sigev_notify == SIGEV_SIGNAL)
2169 aiocbe->timeouthandle =
2170 timeout(process_signal, aiocbe, 0);
2173 #endif /* VFS_AIO */
2176 aio_waitcomplete(struct proc *p, struct aio_waitcomplete_args *uap)
2183 struct aiocb **cbptr;
2184 struct kaioinfo *ki;
2185 struct aiocblist *cb = NULL;
2188 suword(uap->aiocbp, (int)NULL);
2192 /* Get timespec struct. */
2193 error = copyin(uap->timeout, &ts, sizeof(ts));
2197 if ((ts.tv_nsec < 0) || (ts.tv_nsec >= 1000000000))
2200 TIMESPEC_TO_TIMEVAL(&atv, &ts);
2201 if (itimerfix(&atv))
2203 timo = tvtohz(&atv);
2210 cbptr = uap->aiocbp;
2213 if ((cb = TAILQ_FIRST(&ki->kaio_jobdone)) != 0) {
2214 suword(uap->aiocbp, (int)cb->uuaiocb);
2215 p->p_retval[0] = cb->uaiocb._aiocb_private.status;
2216 if (cb->uaiocb.aio_lio_opcode == LIO_WRITE) {
2217 curproc->p_stats->p_ru.ru_oublock +=
2219 cb->outputcharge = 0;
2220 } else if (cb->uaiocb.aio_lio_opcode == LIO_READ) {
2221 curproc->p_stats->p_ru.ru_inblock +=
2223 cb->inputcharge = 0;
2226 return cb->uaiocb._aiocb_private.error;
2230 if ((cb = TAILQ_FIRST(&ki->kaio_bufdone)) != 0 ) {
2232 suword(uap->aiocbp, (int)cb->uuaiocb);
2233 p->p_retval[0] = cb->uaiocb._aiocb_private.status;
2235 return cb->uaiocb._aiocb_private.error;
2238 ki->kaio_flags |= KAIO_WAKEUP;
2239 error = tsleep(p, PRIBIO | PCATCH, "aiowc", timo);
2242 if (error == ERESTART)
2246 else if (error == EINTR)
2248 else if (error == EWOULDBLOCK)
2251 #endif /* VFS_AIO */
2257 filt_aioattach(struct knote *kn)
2263 struct filterops aio_filtops =
2264 { 0, filt_aioattach, NULL, NULL };
2268 filt_aioattach(struct knote *kn)
2270 struct aiocblist *aiocbe = (struct aiocblist *)kn->kn_id;
2273 * The aiocbe pointer must be validated before using it, so
2274 * registration is restricted to the kernel; the user cannot
2277 if ((kn->kn_flags & EV_FLAG1) == 0)
2279 kn->kn_flags &= ~EV_FLAG1;
2281 SLIST_INSERT_HEAD(&aiocbe->klist, kn, kn_selnext);
2287 filt_aiodetach(struct knote *kn)
2289 struct aiocblist *aiocbe = (struct aiocblist *)kn->kn_id;
2290 int s = splhigh(); /* XXX no clue, so overkill */
2292 SLIST_REMOVE(&aiocbe->klist, kn, knote, kn_selnext);
2298 filt_aio(struct knote *kn, long hint)
2300 struct aiocblist *aiocbe = (struct aiocblist *)kn->kn_id;
2302 kn->kn_data = 0; /* XXX data returned? */
2303 if (aiocbe->jobstate != JOBST_JOBFINISHED &&
2304 aiocbe->jobstate != JOBST_JOBBFINISHED)
2306 kn->kn_flags |= EV_EOF;
2310 struct filterops aio_filtops =
2311 { 0, filt_aioattach, filt_aiodetach, filt_aio };
2312 #endif /* VFS_AIO */