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>
25 #include <sys/malloc.h>
28 #include <sys/sysproto.h>
29 #include <sys/filedesc.h>
30 #include <sys/kernel.h>
31 #include <sys/kthread.h>
32 #include <sys/fcntl.h>
35 #include <sys/mutex.h>
36 #include <sys/unistd.h>
38 #include <sys/resourcevar.h>
39 #include <sys/signalvar.h>
40 #include <sys/protosw.h>
41 #include <sys/socketvar.h>
42 #include <sys/syscall.h>
43 #include <sys/sysent.h>
44 #include <sys/sysctl.h>
46 #include <sys/vnode.h>
48 #include <sys/event.h>
51 #include <vm/vm_extern.h>
53 #include <vm/vm_map.h>
57 #include <machine/limits.h>
59 #include "opt_vfs_aio.h"
62 * Counter for allocating reference ids to new jobs. Wrapped to 1 on
67 #define JOBST_NULL 0x0
68 #define JOBST_JOBQGLOBAL 0x2
69 #define JOBST_JOBRUNNING 0x3
70 #define JOBST_JOBFINISHED 0x4
71 #define JOBST_JOBQBUF 0x5
72 #define JOBST_JOBBFINISHED 0x6
74 #ifndef MAX_AIO_PER_PROC
75 #define MAX_AIO_PER_PROC 32
78 #ifndef MAX_AIO_QUEUE_PER_PROC
79 #define MAX_AIO_QUEUE_PER_PROC 256 /* Bigger than AIO_LISTIO_MAX */
83 #define MAX_AIO_PROCS 32
87 #define MAX_AIO_QUEUE 1024 /* Bigger than AIO_LISTIO_MAX */
90 #ifndef TARGET_AIO_PROCS
91 #define TARGET_AIO_PROCS 4
95 #define MAX_BUF_AIO 16
98 #ifndef AIOD_TIMEOUT_DEFAULT
99 #define AIOD_TIMEOUT_DEFAULT (10 * hz)
102 #ifndef AIOD_LIFETIME_DEFAULT
103 #define AIOD_LIFETIME_DEFAULT (30 * hz)
106 SYSCTL_NODE(_vfs, OID_AUTO, aio, CTLFLAG_RW, 0, "Async IO management");
108 static int max_aio_procs = MAX_AIO_PROCS;
109 SYSCTL_INT(_vfs_aio, OID_AUTO, max_aio_procs,
110 CTLFLAG_RW, &max_aio_procs, 0,
111 "Maximum number of kernel threads to use for handling async IO ");
113 static int num_aio_procs = 0;
114 SYSCTL_INT(_vfs_aio, OID_AUTO, num_aio_procs,
115 CTLFLAG_RD, &num_aio_procs, 0,
116 "Number of presently active kernel threads for async IO");
119 * The code will adjust the actual number of AIO processes towards this
120 * number when it gets a chance.
122 static int target_aio_procs = TARGET_AIO_PROCS;
123 SYSCTL_INT(_vfs_aio, OID_AUTO, target_aio_procs, CTLFLAG_RW, &target_aio_procs,
124 0, "Preferred number of ready kernel threads for async IO");
126 static int max_queue_count = MAX_AIO_QUEUE;
127 SYSCTL_INT(_vfs_aio, OID_AUTO, max_aio_queue, CTLFLAG_RW, &max_queue_count, 0,
128 "Maximum number of aio requests to queue, globally");
130 static int num_queue_count = 0;
131 SYSCTL_INT(_vfs_aio, OID_AUTO, num_queue_count, CTLFLAG_RD, &num_queue_count, 0,
132 "Number of queued aio requests");
134 static int num_buf_aio = 0;
135 SYSCTL_INT(_vfs_aio, OID_AUTO, num_buf_aio, CTLFLAG_RD, &num_buf_aio, 0,
136 "Number of aio requests presently handled by the buf subsystem");
138 /* Number of async I/O thread in the process of being started */
139 /* XXX This should be local to _aio_aqueue() */
140 static int num_aio_resv_start = 0;
142 static int aiod_timeout;
143 SYSCTL_INT(_vfs_aio, OID_AUTO, aiod_timeout, CTLFLAG_RW, &aiod_timeout, 0,
144 "Timeout value for synchronous aio operations");
146 static int aiod_lifetime;
147 SYSCTL_INT(_vfs_aio, OID_AUTO, aiod_lifetime, CTLFLAG_RW, &aiod_lifetime, 0,
148 "Maximum lifetime for idle aiod");
150 static int unloadable = 0;
151 SYSCTL_INT(_vfs_aio, OID_AUTO, unloadable, CTLFLAG_RW, &unloadable, 0,
152 "Allow unload of aio (not recommended)");
155 static int max_aio_per_proc = MAX_AIO_PER_PROC;
156 SYSCTL_INT(_vfs_aio, OID_AUTO, max_aio_per_proc, CTLFLAG_RW, &max_aio_per_proc,
157 0, "Maximum active aio requests per process (stored in the process)");
159 static int max_aio_queue_per_proc = MAX_AIO_QUEUE_PER_PROC;
160 SYSCTL_INT(_vfs_aio, OID_AUTO, max_aio_queue_per_proc, CTLFLAG_RW,
161 &max_aio_queue_per_proc, 0,
162 "Maximum queued aio requests per process (stored in the process)");
164 static int max_buf_aio = MAX_BUF_AIO;
165 SYSCTL_INT(_vfs_aio, OID_AUTO, max_buf_aio, CTLFLAG_RW, &max_buf_aio, 0,
166 "Maximum buf aio requests per process (stored in the process)");
169 TAILQ_ENTRY(aiocblist) list; /* List of jobs */
170 TAILQ_ENTRY(aiocblist) plist; /* List of jobs for proc */
175 struct callout_handle timeouthandle;
176 struct buf *bp; /* Buffer pointer */
177 struct proc *userproc; /* User process */ /* Not td! */
178 struct file *fd_file; /* Pointer to file structure */
179 struct aio_liojob *lio; /* Optional lio job */
180 struct aiocb *uuaiocb; /* Pointer in userspace of aiocb */
181 struct klist klist; /* list of knotes */
182 struct aiocb uaiocb; /* Kernel I/O control block */
186 #define AIOCBLIST_RUNDOWN 0x4
187 #define AIOCBLIST_DONE 0x10
192 #define AIOP_FREE 0x1 /* proc on free queue */
193 #define AIOP_SCHED 0x2 /* proc explicitly scheduled */
195 struct aiothreadlist {
196 int aiothreadflags; /* AIO proc flags */
197 TAILQ_ENTRY(aiothreadlist) list; /* List of processes */
198 struct thread *aiothread; /* The AIO thread */
202 * data-structure for lio signal management
206 int lioj_buffer_count;
207 int lioj_buffer_finished_count;
208 int lioj_queue_count;
209 int lioj_queue_finished_count;
210 struct sigevent lioj_signal; /* signal on all I/O done */
211 TAILQ_ENTRY(aio_liojob) lioj_list;
212 struct kaioinfo *lioj_ki;
214 #define LIOJ_SIGNAL 0x1 /* signal on all done (lio) */
215 #define LIOJ_SIGNAL_POSTED 0x2 /* signal has been posted */
218 * per process aio data structure
221 int kaio_flags; /* per process kaio flags */
222 int kaio_maxactive_count; /* maximum number of AIOs */
223 int kaio_active_count; /* number of currently used AIOs */
224 int kaio_qallowed_count; /* maxiumu size of AIO queue */
225 int kaio_queue_count; /* size of AIO queue */
226 int kaio_ballowed_count; /* maximum number of buffers */
227 int kaio_queue_finished_count; /* number of daemon jobs finished */
228 int kaio_buffer_count; /* number of physio buffers */
229 int kaio_buffer_finished_count; /* count of I/O done */
230 struct proc *kaio_p; /* process that uses this kaio block */
231 TAILQ_HEAD(,aio_liojob) kaio_liojoblist; /* list of lio jobs */
232 TAILQ_HEAD(,aiocblist) kaio_jobqueue; /* job queue for process */
233 TAILQ_HEAD(,aiocblist) kaio_jobdone; /* done queue for process */
234 TAILQ_HEAD(,aiocblist) kaio_bufqueue; /* buffer job queue for process */
235 TAILQ_HEAD(,aiocblist) kaio_bufdone; /* buffer done queue for process */
236 TAILQ_HEAD(,aiocblist) kaio_sockqueue; /* queue for aios waiting on sockets */
239 #define KAIO_RUNDOWN 0x1 /* process is being run down */
240 #define KAIO_WAKEUP 0x2 /* wakeup process when there is a significant event */
242 static TAILQ_HEAD(,aiothreadlist) aio_activeproc; /* Active daemons */
243 static TAILQ_HEAD(,aiothreadlist) aio_freeproc; /* Idle daemons */
244 static TAILQ_HEAD(,aiocblist) aio_jobs; /* Async job list */
245 static TAILQ_HEAD(,aiocblist) aio_bufjobs; /* Phys I/O job list */
247 static void aio_init_aioinfo(struct proc *p);
248 static void aio_onceonly(void);
249 static int aio_free_entry(struct aiocblist *aiocbe);
250 static void aio_process(struct aiocblist *aiocbe);
251 static int aio_newproc(void);
252 static int aio_aqueue(struct thread *td, struct aiocb *job, int type);
253 static void aio_physwakeup(struct buf *bp);
254 static void aio_proc_rundown(struct proc *p);
255 static int aio_fphysio(struct aiocblist *aiocbe);
256 static int aio_qphysio(struct proc *p, struct aiocblist *iocb);
257 static void aio_daemon(void *uproc);
258 static void aio_swake_cb(struct socket *, struct sockbuf *);
259 static int aio_unload(void);
260 static void process_signal(void *aioj);
261 static int filt_aioattach(struct knote *kn);
262 static void filt_aiodetach(struct knote *kn);
263 static int filt_aio(struct knote *kn, long hint);
267 * kaio Per process async io info
268 * aiop async io thread data
269 * aiocb async io jobs
270 * aiol list io job pointer - internal to aio_suspend XXX
271 * aiolio list io jobs
273 static uma_zone_t kaio_zone, aiop_zone, aiocb_zone, aiol_zone, aiolio_zone;
275 /* kqueue filters for aio */
276 static struct filterops aio_filtops =
277 { 0, filt_aioattach, filt_aiodetach, filt_aio };
280 * Main operations function for use as a kernel module.
283 aio_modload(struct module *module, int cmd, void *arg)
292 error = aio_unload();
303 static moduledata_t aio_mod = {
309 SYSCALL_MODULE_HELPER(aio_return);
310 SYSCALL_MODULE_HELPER(aio_suspend);
311 SYSCALL_MODULE_HELPER(aio_cancel);
312 SYSCALL_MODULE_HELPER(aio_error);
313 SYSCALL_MODULE_HELPER(aio_read);
314 SYSCALL_MODULE_HELPER(aio_write);
315 SYSCALL_MODULE_HELPER(aio_waitcomplete);
316 SYSCALL_MODULE_HELPER(lio_listio);
318 DECLARE_MODULE(aio, aio_mod,
319 SI_SUB_VFS, SI_ORDER_ANY);
320 MODULE_VERSION(aio, 1);
323 * Startup initialization
329 /* XXX: should probably just use so->callback */
330 aio_swake = &aio_swake_cb;
331 at_exit(aio_proc_rundown);
332 at_exec(aio_proc_rundown);
333 kqueue_add_filteropts(EVFILT_AIO, &aio_filtops);
334 TAILQ_INIT(&aio_freeproc);
335 TAILQ_INIT(&aio_activeproc);
336 TAILQ_INIT(&aio_jobs);
337 TAILQ_INIT(&aio_bufjobs);
338 kaio_zone = uma_zcreate("AIO", sizeof(struct kaioinfo), NULL, NULL,
339 NULL, NULL, UMA_ALIGN_PTR, UMA_ZONE_NOFREE);
340 aiop_zone = uma_zcreate("AIOP", sizeof(struct aiothreadlist), NULL,
341 NULL, NULL, NULL, UMA_ALIGN_PTR, UMA_ZONE_NOFREE);
342 aiocb_zone = uma_zcreate("AIOCB", sizeof(struct aiocblist), NULL, NULL,
343 NULL, NULL, UMA_ALIGN_PTR, UMA_ZONE_NOFREE);
344 aiol_zone = uma_zcreate("AIOL", AIO_LISTIO_MAX*sizeof(intptr_t) , NULL,
345 NULL, NULL, NULL, UMA_ALIGN_PTR, UMA_ZONE_NOFREE);
346 aiolio_zone = uma_zcreate("AIOLIO", sizeof(struct aio_liojob), NULL,
347 NULL, NULL, NULL, UMA_ALIGN_PTR, UMA_ZONE_NOFREE);
348 aiod_timeout = AIOD_TIMEOUT_DEFAULT;
349 aiod_lifetime = AIOD_LIFETIME_DEFAULT;
351 async_io_version = _POSIX_VERSION;
355 * Callback for unload of AIO when used as a module.
362 * XXX: no unloads by default, it's too dangerous.
363 * perhaps we could do it if locked out callers and then
364 * did an aio_proc_rundown() on each process.
369 async_io_version = 0;
371 rm_at_exit(aio_proc_rundown);
372 rm_at_exec(aio_proc_rundown);
373 kqueue_del_filteropts(EVFILT_AIO);
378 * Init the per-process aioinfo structure. The aioinfo limits are set
379 * per-process for user limit (resource) management.
382 aio_init_aioinfo(struct proc *p)
385 if (p->p_aioinfo == NULL) {
386 ki = uma_zalloc(kaio_zone, M_WAITOK);
389 ki->kaio_maxactive_count = max_aio_per_proc;
390 ki->kaio_active_count = 0;
391 ki->kaio_qallowed_count = max_aio_queue_per_proc;
392 ki->kaio_queue_count = 0;
393 ki->kaio_ballowed_count = max_buf_aio;
394 ki->kaio_buffer_count = 0;
395 ki->kaio_buffer_finished_count = 0;
397 TAILQ_INIT(&ki->kaio_jobdone);
398 TAILQ_INIT(&ki->kaio_jobqueue);
399 TAILQ_INIT(&ki->kaio_bufdone);
400 TAILQ_INIT(&ki->kaio_bufqueue);
401 TAILQ_INIT(&ki->kaio_liojoblist);
402 TAILQ_INIT(&ki->kaio_sockqueue);
405 while (num_aio_procs < target_aio_procs)
410 * Free a job entry. Wait for completion if it is currently active, but don't
411 * delay forever. If we delay, we return a flag that says that we have to
412 * restart the queue scan.
415 aio_free_entry(struct aiocblist *aiocbe)
418 struct aio_liojob *lj;
423 if (aiocbe->jobstate == JOBST_NULL)
424 panic("aio_free_entry: freeing already free job");
426 p = aiocbe->userproc;
430 panic("aio_free_entry: missing p->p_aioinfo");
432 while (aiocbe->jobstate == JOBST_JOBRUNNING) {
433 aiocbe->jobflags |= AIOCBLIST_RUNDOWN;
434 tsleep(aiocbe, PRIBIO, "jobwai", 0);
436 if (aiocbe->bp == NULL) {
437 if (ki->kaio_queue_count <= 0)
438 panic("aio_free_entry: process queue size <= 0");
439 if (num_queue_count <= 0)
440 panic("aio_free_entry: system wide queue size <= 0");
443 lj->lioj_queue_count--;
444 if (aiocbe->jobflags & AIOCBLIST_DONE)
445 lj->lioj_queue_finished_count--;
447 ki->kaio_queue_count--;
448 if (aiocbe->jobflags & AIOCBLIST_DONE)
449 ki->kaio_queue_finished_count--;
453 lj->lioj_buffer_count--;
454 if (aiocbe->jobflags & AIOCBLIST_DONE)
455 lj->lioj_buffer_finished_count--;
457 if (aiocbe->jobflags & AIOCBLIST_DONE)
458 ki->kaio_buffer_finished_count--;
459 ki->kaio_buffer_count--;
463 /* aiocbe is going away, we need to destroy any knotes */
464 /* XXXKSE Note the thread here is used to eventually find the
465 * owning process again, but it is also used to do a fo_close
466 * and that requires the thread. (but does it require the
467 * OWNING thread? (or maybe the running thread?)
468 * There is a semantic problem here...
470 knote_remove(FIRST_THREAD_IN_PROC(p), &aiocbe->klist); /* XXXKSE */
472 if ((ki->kaio_flags & KAIO_WAKEUP) || ((ki->kaio_flags & KAIO_RUNDOWN)
473 && ((ki->kaio_buffer_count == 0) && (ki->kaio_queue_count == 0)))) {
474 ki->kaio_flags &= ~KAIO_WAKEUP;
478 if (aiocbe->jobstate == JOBST_JOBQBUF) {
479 if ((error = aio_fphysio(aiocbe)) != 0)
481 if (aiocbe->jobstate != JOBST_JOBBFINISHED)
482 panic("aio_free_entry: invalid physio finish-up state");
484 TAILQ_REMOVE(&ki->kaio_bufdone, aiocbe, plist);
486 } else if (aiocbe->jobstate == JOBST_JOBQGLOBAL) {
488 TAILQ_REMOVE(&aio_jobs, aiocbe, list);
489 TAILQ_REMOVE(&ki->kaio_jobqueue, aiocbe, plist);
491 } else if (aiocbe->jobstate == JOBST_JOBFINISHED)
492 TAILQ_REMOVE(&ki->kaio_jobdone, aiocbe, plist);
493 else if (aiocbe->jobstate == JOBST_JOBBFINISHED) {
495 TAILQ_REMOVE(&ki->kaio_bufdone, aiocbe, plist);
498 vunmapbuf(aiocbe->bp);
499 relpbuf(aiocbe->bp, NULL);
503 if (lj && (lj->lioj_buffer_count == 0) && (lj->lioj_queue_count == 0)) {
504 TAILQ_REMOVE(&ki->kaio_liojoblist, lj, lioj_list);
505 uma_zfree(aiolio_zone, lj);
507 aiocbe->jobstate = JOBST_NULL;
508 untimeout(process_signal, aiocbe, aiocbe->timeouthandle);
509 fdrop(aiocbe->fd_file, curthread);
510 uma_zfree(aiocb_zone, aiocbe);
515 * Rundown the jobs for a given process.
518 aio_proc_rundown(struct proc *p)
522 struct aio_liojob *lj, *ljn;
523 struct aiocblist *aiocbe, *aiocbn;
531 ki->kaio_flags |= LIOJ_SIGNAL_POSTED;
532 while ((ki->kaio_active_count > 0) || (ki->kaio_buffer_count >
533 ki->kaio_buffer_finished_count)) {
534 ki->kaio_flags |= KAIO_RUNDOWN;
535 if (tsleep(p, PRIBIO, "kaiowt", aiod_timeout))
540 * Move any aio ops that are waiting on socket I/O to the normal job
541 * queues so they are cleaned up with any others.
544 for (aiocbe = TAILQ_FIRST(&ki->kaio_sockqueue); aiocbe; aiocbe =
546 aiocbn = TAILQ_NEXT(aiocbe, plist);
547 fp = aiocbe->fd_file;
549 so = (struct socket *)fp->f_data;
550 TAILQ_REMOVE(&so->so_aiojobq, aiocbe, list);
551 if (TAILQ_EMPTY(&so->so_aiojobq)) {
552 so->so_snd.sb_flags &= ~SB_AIO;
553 so->so_rcv.sb_flags &= ~SB_AIO;
556 TAILQ_REMOVE(&ki->kaio_sockqueue, aiocbe, plist);
557 TAILQ_INSERT_HEAD(&aio_jobs, aiocbe, list);
558 TAILQ_INSERT_HEAD(&ki->kaio_jobqueue, aiocbe, plist);
563 for (aiocbe = TAILQ_FIRST(&ki->kaio_jobdone); aiocbe; aiocbe = aiocbn) {
564 aiocbn = TAILQ_NEXT(aiocbe, plist);
565 if (aio_free_entry(aiocbe))
570 for (aiocbe = TAILQ_FIRST(&ki->kaio_jobqueue); aiocbe; aiocbe =
572 aiocbn = TAILQ_NEXT(aiocbe, plist);
573 if (aio_free_entry(aiocbe))
578 * Note the use of lots of splbio here, trying to avoid splbio for long chains
579 * of I/O. Probably unnecessary.
583 while (TAILQ_FIRST(&ki->kaio_bufqueue)) {
584 ki->kaio_flags |= KAIO_WAKEUP;
585 tsleep(p, PRIBIO, "aioprn", 0);
593 for (aiocbe = TAILQ_FIRST(&ki->kaio_bufdone); aiocbe; aiocbe = aiocbn) {
594 aiocbn = TAILQ_NEXT(aiocbe, plist);
595 if (aio_free_entry(aiocbe)) {
603 * If we've slept, jobs might have moved from one queue to another.
604 * Retry rundown if we didn't manage to empty the queues.
606 if (TAILQ_FIRST(&ki->kaio_jobdone) != NULL ||
607 TAILQ_FIRST(&ki->kaio_jobqueue) != NULL ||
608 TAILQ_FIRST(&ki->kaio_bufqueue) != NULL ||
609 TAILQ_FIRST(&ki->kaio_bufdone) != NULL)
612 for (lj = TAILQ_FIRST(&ki->kaio_liojoblist); lj; lj = ljn) {
613 ljn = TAILQ_NEXT(lj, lioj_list);
614 if ((lj->lioj_buffer_count == 0) && (lj->lioj_queue_count ==
616 TAILQ_REMOVE(&ki->kaio_liojoblist, lj, lioj_list);
617 uma_zfree(aiolio_zone, lj);
620 printf("LIO job not cleaned up: B:%d, BF:%d, Q:%d, "
621 "QF:%d\n", lj->lioj_buffer_count,
622 lj->lioj_buffer_finished_count,
623 lj->lioj_queue_count,
624 lj->lioj_queue_finished_count);
629 uma_zfree(kaio_zone, ki);
634 * Select a job to run (called by an AIO daemon).
636 static struct aiocblist *
637 aio_selectjob(struct aiothreadlist *aiop)
640 struct aiocblist *aiocbe;
645 for (aiocbe = TAILQ_FIRST(&aio_jobs); aiocbe; aiocbe =
646 TAILQ_NEXT(aiocbe, list)) {
647 userp = aiocbe->userproc;
648 ki = userp->p_aioinfo;
650 if (ki->kaio_active_count < ki->kaio_maxactive_count) {
651 TAILQ_REMOVE(&aio_jobs, aiocbe, list);
662 * The AIO processing activity. This is the code that does the I/O request for
663 * the non-physio version of the operations. The normal vn operations are used,
664 * and this code should work in all instances for every type of file, including
665 * pipes, sockets, fifos, and regular files.
668 aio_process(struct aiocblist *aiocbe)
678 int oublock_st, oublock_end;
679 int inblock_st, inblock_end;
683 cb = &aiocbe->uaiocb;
684 fp = aiocbe->fd_file;
686 aiov.iov_base = (void *)(uintptr_t)cb->aio_buf;
687 aiov.iov_len = cb->aio_nbytes;
689 auio.uio_iov = &aiov;
691 auio.uio_offset = cb->aio_offset;
692 auio.uio_resid = cb->aio_nbytes;
693 cnt = cb->aio_nbytes;
694 auio.uio_segflg = UIO_USERSPACE;
697 inblock_st = mycp->p_stats->p_ru.ru_inblock;
698 oublock_st = mycp->p_stats->p_ru.ru_oublock;
700 * _aio_aqueue() acquires a reference to the file that is
701 * released in aio_free_entry().
703 if (cb->aio_lio_opcode == LIO_READ) {
704 auio.uio_rw = UIO_READ;
705 error = fo_read(fp, &auio, fp->f_cred, FOF_OFFSET, td);
707 auio.uio_rw = UIO_WRITE;
708 error = fo_write(fp, &auio, fp->f_cred, FOF_OFFSET, td);
710 inblock_end = mycp->p_stats->p_ru.ru_inblock;
711 oublock_end = mycp->p_stats->p_ru.ru_oublock;
713 aiocbe->inputcharge = inblock_end - inblock_st;
714 aiocbe->outputcharge = oublock_end - oublock_st;
716 if ((error) && (auio.uio_resid != cnt)) {
717 if (error == ERESTART || error == EINTR || error == EWOULDBLOCK)
719 if ((error == EPIPE) && (cb->aio_lio_opcode == LIO_WRITE)) {
720 PROC_LOCK(aiocbe->userproc);
721 psignal(aiocbe->userproc, SIGPIPE);
722 PROC_UNLOCK(aiocbe->userproc);
726 cnt -= auio.uio_resid;
727 cb->_aiocb_private.error = error;
728 cb->_aiocb_private.status = cnt;
732 * The AIO daemon, most of the actual work is done in aio_process,
733 * but the setup (and address space mgmt) is done in this routine.
736 aio_daemon(void *uproc)
739 struct aio_liojob *lj;
741 struct aiocblist *aiocbe;
742 struct aiothreadlist *aiop;
744 struct proc *curcp, *mycp, *userp;
745 struct vmspace *myvm, *tmpvm;
746 struct thread *td = curthread;
747 struct pgrp *newpgrp;
748 struct session *newsess;
752 * Local copies of curproc (cp) and vmspace (myvm)
755 myvm = mycp->p_vmspace;
757 if (mycp->p_textvp) {
758 vrele(mycp->p_textvp);
759 mycp->p_textvp = NULL;
763 * Allocate and ready the aio control info. There is one aiop structure
766 aiop = uma_zalloc(aiop_zone, M_WAITOK);
767 aiop->aiothread = td;
768 aiop->aiothreadflags |= AIOP_FREE;
773 * Place thread (lightweight process) onto the AIO free thread list.
775 if (TAILQ_EMPTY(&aio_freeproc))
776 wakeup(&aio_freeproc);
777 TAILQ_INSERT_HEAD(&aio_freeproc, aiop, list);
782 * Get rid of our current filedescriptors. AIOD's don't need any
783 * filedescriptors, except as temporarily inherited from the client.
788 /* The daemon resides in its own pgrp. */
789 MALLOC(newpgrp, struct pgrp *, sizeof(struct pgrp), M_PGRP,
791 MALLOC(newsess, struct session *, sizeof(struct session), M_SESSION,
794 sx_xlock(&proctree_lock);
795 enterpgrp(mycp, mycp->p_pid, newpgrp, newsess);
796 sx_xunlock(&proctree_lock);
799 /* Mark special process type. */
800 mycp->p_flag |= P_SYSTEM;
803 * Wakeup parent process. (Parent sleeps to keep from blasting away
804 * and creating too many daemons.)
810 * curcp is the current daemon process context.
811 * userp is the current user process context.
816 * Take daemon off of free queue
818 if (aiop->aiothreadflags & AIOP_FREE) {
820 TAILQ_REMOVE(&aio_freeproc, aiop, list);
821 TAILQ_INSERT_TAIL(&aio_activeproc, aiop, list);
822 aiop->aiothreadflags &= ~AIOP_FREE;
825 aiop->aiothreadflags &= ~AIOP_SCHED;
830 while ((aiocbe = aio_selectjob(aiop)) != NULL) {
831 cb = &aiocbe->uaiocb;
832 userp = aiocbe->userproc;
834 aiocbe->jobstate = JOBST_JOBRUNNING;
837 * Connect to process address space for user program.
839 if (userp != curcp) {
841 * Save the current address space that we are
844 tmpvm = mycp->p_vmspace;
847 * Point to the new user address space, and
850 mycp->p_vmspace = userp->p_vmspace;
851 mycp->p_vmspace->vm_refcnt++;
853 /* Activate the new mapping. */
854 pmap_activate(FIRST_THREAD_IN_PROC(mycp));
857 * If the old address space wasn't the daemons
858 * own address space, then we need to remove the
859 * daemon's reference from the other process
860 * that it was acting on behalf of.
868 ki = userp->p_aioinfo;
871 /* Account for currently active jobs. */
872 ki->kaio_active_count++;
874 /* Do the I/O function. */
877 /* Decrement the active job count. */
878 ki->kaio_active_count--;
881 * Increment the completion count for wakeup/signal
884 aiocbe->jobflags |= AIOCBLIST_DONE;
885 ki->kaio_queue_finished_count++;
887 lj->lioj_queue_finished_count++;
888 if ((ki->kaio_flags & KAIO_WAKEUP) || ((ki->kaio_flags
889 & KAIO_RUNDOWN) && (ki->kaio_active_count == 0))) {
890 ki->kaio_flags &= ~KAIO_WAKEUP;
895 if (lj && (lj->lioj_flags &
896 (LIOJ_SIGNAL|LIOJ_SIGNAL_POSTED)) == LIOJ_SIGNAL) {
897 if ((lj->lioj_queue_finished_count ==
898 lj->lioj_queue_count) &&
899 (lj->lioj_buffer_finished_count ==
900 lj->lioj_buffer_count)) {
903 lj->lioj_signal.sigev_signo);
905 lj->lioj_flags |= LIOJ_SIGNAL_POSTED;
910 aiocbe->jobstate = JOBST_JOBFINISHED;
913 TAILQ_REMOVE(&ki->kaio_jobqueue, aiocbe, plist);
914 TAILQ_INSERT_TAIL(&ki->kaio_jobdone, aiocbe, plist);
916 KNOTE(&aiocbe->klist, 0);
918 if (aiocbe->jobflags & AIOCBLIST_RUNDOWN) {
920 aiocbe->jobflags &= ~AIOCBLIST_RUNDOWN;
923 if (cb->aio_sigevent.sigev_notify == SIGEV_SIGNAL) {
925 psignal(userp, cb->aio_sigevent.sigev_signo);
931 * Disconnect from user address space.
934 /* Get the user address space to disconnect from. */
935 tmpvm = mycp->p_vmspace;
937 /* Get original address space for daemon. */
938 mycp->p_vmspace = myvm;
940 /* Activate the daemon's address space. */
941 pmap_activate(FIRST_THREAD_IN_PROC(mycp));
944 printf("AIOD: vmspace problem -- %d\n",
948 /* Remove our vmspace reference. */
955 * If we are the first to be put onto the free queue, wakeup
956 * anyone waiting for a daemon.
959 TAILQ_REMOVE(&aio_activeproc, aiop, list);
960 if (TAILQ_EMPTY(&aio_freeproc))
961 wakeup(&aio_freeproc);
962 TAILQ_INSERT_HEAD(&aio_freeproc, aiop, list);
963 aiop->aiothreadflags |= AIOP_FREE;
967 * If daemon is inactive for a long time, allow it to exit,
968 * thereby freeing resources.
970 if ((aiop->aiothreadflags & AIOP_SCHED) == 0 &&
971 tsleep(aiop->aiothread, PRIBIO, "aiordy", aiod_lifetime)) {
973 if (TAILQ_EMPTY(&aio_jobs)) {
974 if ((aiop->aiothreadflags & AIOP_FREE) &&
975 (num_aio_procs > target_aio_procs)) {
976 TAILQ_REMOVE(&aio_freeproc, aiop, list);
978 uma_zfree(aiop_zone, aiop);
981 if (mycp->p_vmspace->vm_refcnt <= 1) {
982 printf("AIOD: bad vm refcnt for"
983 " exiting daemon: %d\n",
984 mycp->p_vmspace->vm_refcnt);
996 * Create a new AIO daemon. This is mostly a kernel-thread fork routine. The
997 * AIO daemon modifies its environment itself.
1005 error = kthread_create(aio_daemon, curproc, &p, RFNOWAIT, 0, "aiod%d",
1011 * Wait until daemon is started, but continue on just in case to
1012 * handle error conditions.
1014 error = tsleep(p, PZERO, "aiosta", aiod_timeout);
1022 * Try the high-performance, low-overhead physio method for eligible
1023 * VCHR devices. This method doesn't use an aio helper thread, and
1024 * thus has very low overhead.
1026 * Assumes that the caller, _aio_aqueue(), has incremented the file
1027 * structure's reference count, preventing its deallocation for the
1028 * duration of this call.
1031 aio_qphysio(struct proc *p, struct aiocblist *aiocbe)
1038 struct kaioinfo *ki;
1039 struct aio_liojob *lj;
1043 cb = &aiocbe->uaiocb;
1044 fp = aiocbe->fd_file;
1046 if (fp->f_type != DTYPE_VNODE)
1049 vp = (struct vnode *)fp->f_data;
1052 * If its not a disk, we don't want to return a positive error.
1053 * It causes the aio code to not fall through to try the thread
1054 * way when you're talking to a regular file.
1056 if (!vn_isdisk(vp, &error)) {
1057 if (error == ENOTBLK)
1063 if (cb->aio_nbytes % vp->v_rdev->si_bsize_phys)
1066 if (cb->aio_nbytes >
1067 MAXPHYS - (((vm_offset_t) cb->aio_buf) & PAGE_MASK))
1071 if (ki->kaio_buffer_count >= ki->kaio_ballowed_count)
1074 ki->kaio_buffer_count++;
1078 lj->lioj_buffer_count++;
1080 /* Create and build a buffer header for a transfer. */
1081 bp = (struct buf *)getpbuf(NULL);
1085 * Get a copy of the kva from the physical buffer.
1088 bp->b_dev = vp->v_rdev;
1089 error = bp->b_error = 0;
1091 bp->b_bcount = cb->aio_nbytes;
1092 bp->b_bufsize = cb->aio_nbytes;
1093 bp->b_flags = B_PHYS;
1094 bp->b_iodone = aio_physwakeup;
1095 bp->b_saveaddr = bp->b_data;
1096 bp->b_data = (void *)(uintptr_t)cb->aio_buf;
1097 bp->b_blkno = btodb(cb->aio_offset);
1099 if (cb->aio_lio_opcode == LIO_WRITE) {
1100 bp->b_iocmd = BIO_WRITE;
1101 if (!useracc(bp->b_data, bp->b_bufsize, VM_PROT_READ)) {
1106 bp->b_iocmd = BIO_READ;
1107 if (!useracc(bp->b_data, bp->b_bufsize, VM_PROT_WRITE)) {
1113 /* Bring buffer into kernel space. */
1118 bp->b_spc = (void *)aiocbe;
1119 TAILQ_INSERT_TAIL(&aio_bufjobs, aiocbe, list);
1120 TAILQ_INSERT_TAIL(&ki->kaio_bufqueue, aiocbe, plist);
1121 aiocbe->jobstate = JOBST_JOBQBUF;
1122 cb->_aiocb_private.status = cb->aio_nbytes;
1128 /* Perform transfer. */
1129 DEV_STRATEGY(bp, 0);
1135 * If we had an error invoking the request, or an error in processing
1136 * the request before we have returned, we process it as an error in
1137 * transfer. Note that such an I/O error is not indicated immediately,
1138 * but is returned using the aio_error mechanism. In this case,
1139 * aio_suspend will return immediately.
1141 if (bp->b_error || (bp->b_ioflags & BIO_ERROR)) {
1142 struct aiocb *job = aiocbe->uuaiocb;
1144 aiocbe->uaiocb._aiocb_private.status = 0;
1145 suword(&job->_aiocb_private.status, 0);
1146 aiocbe->uaiocb._aiocb_private.error = bp->b_error;
1147 suword(&job->_aiocb_private.error, bp->b_error);
1149 ki->kaio_buffer_finished_count++;
1151 if (aiocbe->jobstate != JOBST_JOBBFINISHED) {
1152 aiocbe->jobstate = JOBST_JOBBFINISHED;
1153 aiocbe->jobflags |= AIOCBLIST_DONE;
1154 TAILQ_REMOVE(&aio_bufjobs, aiocbe, list);
1155 TAILQ_REMOVE(&ki->kaio_bufqueue, aiocbe, plist);
1156 TAILQ_INSERT_TAIL(&ki->kaio_bufdone, aiocbe, plist);
1162 KNOTE(&aiocbe->klist, 0);
1166 ki->kaio_buffer_count--;
1168 lj->lioj_buffer_count--;
1175 * This waits/tests physio completion.
1178 aio_fphysio(struct aiocblist *iocb)
1187 while ((bp->b_flags & B_DONE) == 0) {
1188 if (tsleep(bp, PRIBIO, "physstr", aiod_timeout)) {
1189 if ((bp->b_flags & B_DONE) == 0) {
1198 /* Release mapping into kernel space. */
1204 /* Check for an error. */
1205 if (bp->b_ioflags & BIO_ERROR)
1206 error = bp->b_error;
1213 * Wake up aio requests that may be serviceable now.
1216 aio_swake_cb(struct socket *so, struct sockbuf *sb)
1218 struct aiocblist *cb,*cbn;
1220 struct kaioinfo *ki = NULL;
1221 int opcode, wakecount = 0;
1222 struct aiothreadlist *aiop;
1224 if (sb == &so->so_snd) {
1226 so->so_snd.sb_flags &= ~SB_AIO;
1229 so->so_rcv.sb_flags &= ~SB_AIO;
1232 for (cb = TAILQ_FIRST(&so->so_aiojobq); cb; cb = cbn) {
1233 cbn = TAILQ_NEXT(cb, list);
1234 if (opcode == cb->uaiocb.aio_lio_opcode) {
1237 TAILQ_REMOVE(&so->so_aiojobq, cb, list);
1238 TAILQ_REMOVE(&ki->kaio_sockqueue, cb, plist);
1239 TAILQ_INSERT_TAIL(&aio_jobs, cb, list);
1240 TAILQ_INSERT_TAIL(&ki->kaio_jobqueue, cb, plist);
1242 if (cb->jobstate != JOBST_JOBQGLOBAL)
1243 panic("invalid queue value");
1247 while (wakecount--) {
1248 if ((aiop = TAILQ_FIRST(&aio_freeproc)) != 0) {
1249 TAILQ_REMOVE(&aio_freeproc, aiop, list);
1250 TAILQ_INSERT_TAIL(&aio_activeproc, aiop, list);
1251 aiop->aiothreadflags &= ~AIOP_FREE;
1252 wakeup(aiop->aiothread);
1258 * Queue a new AIO request. Choosing either the threaded or direct physio VCHR
1259 * technique is done in this code.
1262 _aio_aqueue(struct thread *td, struct aiocb *job, struct aio_liojob *lj, int type)
1264 struct proc *p = td->td_proc;
1265 struct filedesc *fdp;
1271 int opcode, user_opcode;
1272 struct aiocblist *aiocbe;
1273 struct aiothreadlist *aiop;
1274 struct kaioinfo *ki;
1279 aiocbe = uma_zalloc(aiocb_zone, M_WAITOK);
1280 aiocbe->inputcharge = 0;
1281 aiocbe->outputcharge = 0;
1282 callout_handle_init(&aiocbe->timeouthandle);
1283 SLIST_INIT(&aiocbe->klist);
1285 suword(&job->_aiocb_private.status, -1);
1286 suword(&job->_aiocb_private.error, 0);
1287 suword(&job->_aiocb_private.kernelinfo, -1);
1289 error = copyin(job, &aiocbe->uaiocb, sizeof(aiocbe->uaiocb));
1291 suword(&job->_aiocb_private.error, error);
1292 uma_zfree(aiocb_zone, aiocbe);
1295 if (aiocbe->uaiocb.aio_sigevent.sigev_notify == SIGEV_SIGNAL &&
1296 !_SIG_VALID(aiocbe->uaiocb.aio_sigevent.sigev_signo)) {
1297 uma_zfree(aiocb_zone, aiocbe);
1301 /* Save userspace address of the job info. */
1302 aiocbe->uuaiocb = job;
1304 /* Get the opcode. */
1305 user_opcode = aiocbe->uaiocb.aio_lio_opcode;
1306 if (type != LIO_NOP)
1307 aiocbe->uaiocb.aio_lio_opcode = type;
1308 opcode = aiocbe->uaiocb.aio_lio_opcode;
1310 /* Get the fd info for process. */
1314 * Range check file descriptor.
1316 fd = aiocbe->uaiocb.aio_fildes;
1317 if (fd >= fdp->fd_nfiles) {
1318 uma_zfree(aiocb_zone, aiocbe);
1320 suword(&job->_aiocb_private.error, EBADF);
1324 fp = aiocbe->fd_file = fdp->fd_ofiles[fd];
1325 if ((fp == NULL) || ((opcode == LIO_WRITE) && ((fp->f_flag & FWRITE) ==
1327 uma_zfree(aiocb_zone, aiocbe);
1329 suword(&job->_aiocb_private.error, EBADF);
1334 if (aiocbe->uaiocb.aio_offset == -1LL) {
1338 error = suword(&job->_aiocb_private.kernelinfo, jobrefid);
1343 aiocbe->uaiocb._aiocb_private.kernelinfo = (void *)(intptr_t)jobrefid;
1344 if (jobrefid == LONG_MAX)
1349 if (opcode == LIO_NOP) {
1351 uma_zfree(aiocb_zone, aiocbe);
1353 suword(&job->_aiocb_private.error, 0);
1354 suword(&job->_aiocb_private.status, 0);
1355 suword(&job->_aiocb_private.kernelinfo, 0);
1359 if ((opcode != LIO_READ) && (opcode != LIO_WRITE)) {
1361 suword(&job->_aiocb_private.status, 0);
1366 if (aiocbe->uaiocb.aio_sigevent.sigev_notify == SIGEV_KEVENT) {
1367 kev.ident = aiocbe->uaiocb.aio_sigevent.sigev_notify_kqueue;
1368 kev.udata = aiocbe->uaiocb.aio_sigevent.sigev_value.sigval_ptr;
1372 * This method for requesting kevent-based notification won't
1373 * work on the alpha, since we're passing in a pointer
1374 * via aio_lio_opcode, which is an int. Use the SIGEV_KEVENT-
1375 * based method instead.
1377 if (user_opcode == LIO_NOP || user_opcode == LIO_READ ||
1378 user_opcode == LIO_WRITE)
1381 error = copyin((struct kevent *)(uintptr_t)user_opcode,
1386 if ((u_int)kev.ident >= fdp->fd_nfiles ||
1387 (kq_fp = fdp->fd_ofiles[kev.ident]) == NULL ||
1388 (kq_fp->f_type != DTYPE_KQUEUE)) {
1392 kq = (struct kqueue *)kq_fp->f_data;
1393 kev.ident = (uintptr_t)aiocbe->uuaiocb;
1394 kev.filter = EVFILT_AIO;
1395 kev.flags = EV_ADD | EV_ENABLE | EV_FLAG1;
1396 kev.data = (intptr_t)aiocbe;
1397 error = kqueue_register(kq, &kev, td);
1401 uma_zfree(aiocb_zone, aiocbe);
1403 suword(&job->_aiocb_private.error, error);
1408 suword(&job->_aiocb_private.error, EINPROGRESS);
1409 aiocbe->uaiocb._aiocb_private.error = EINPROGRESS;
1410 aiocbe->userproc = p;
1411 aiocbe->jobflags = 0;
1415 if (fp->f_type == DTYPE_SOCKET) {
1417 * Alternate queueing for socket ops: Reach down into the
1418 * descriptor to get the socket data. Then check to see if the
1419 * socket is ready to be read or written (based on the requested
1422 * If it is not ready for io, then queue the aiocbe on the
1423 * socket, and set the flags so we get a call when sbnotify()
1426 so = (struct socket *)fp->f_data;
1428 if (((opcode == LIO_READ) && (!soreadable(so))) || ((opcode ==
1429 LIO_WRITE) && (!sowriteable(so)))) {
1430 TAILQ_INSERT_TAIL(&so->so_aiojobq, aiocbe, list);
1431 TAILQ_INSERT_TAIL(&ki->kaio_sockqueue, aiocbe, plist);
1432 if (opcode == LIO_READ)
1433 so->so_rcv.sb_flags |= SB_AIO;
1435 so->so_snd.sb_flags |= SB_AIO;
1436 aiocbe->jobstate = JOBST_JOBQGLOBAL; /* XXX */
1437 ki->kaio_queue_count++;
1446 if ((error = aio_qphysio(p, aiocbe)) == 0)
1449 suword(&job->_aiocb_private.status, 0);
1450 aiocbe->uaiocb._aiocb_private.error = error;
1451 suword(&job->_aiocb_private.error, error);
1455 /* No buffer for daemon I/O. */
1458 ki->kaio_queue_count++;
1460 lj->lioj_queue_count++;
1462 TAILQ_INSERT_TAIL(&ki->kaio_jobqueue, aiocbe, plist);
1463 TAILQ_INSERT_TAIL(&aio_jobs, aiocbe, list);
1465 aiocbe->jobstate = JOBST_JOBQGLOBAL;
1471 * If we don't have a free AIO process, and we are below our quota, then
1472 * start one. Otherwise, depend on the subsequent I/O completions to
1473 * pick-up this job. If we don't sucessfully create the new process
1474 * (thread) due to resource issues, we return an error for now (EAGAIN),
1475 * which is likely not the correct thing to do.
1479 if ((aiop = TAILQ_FIRST(&aio_freeproc)) != NULL) {
1480 TAILQ_REMOVE(&aio_freeproc, aiop, list);
1481 TAILQ_INSERT_TAIL(&aio_activeproc, aiop, list);
1482 aiop->aiothreadflags &= ~AIOP_FREE;
1483 wakeup(aiop->aiothread);
1484 } else if (((num_aio_resv_start + num_aio_procs) < max_aio_procs) &&
1485 ((ki->kaio_active_count + num_aio_resv_start) <
1486 ki->kaio_maxactive_count)) {
1487 num_aio_resv_start++;
1488 if ((error = aio_newproc()) == 0) {
1489 num_aio_resv_start--;
1492 num_aio_resv_start--;
1500 * This routine queues an AIO request, checking for quotas.
1503 aio_aqueue(struct thread *td, struct aiocb *job, int type)
1505 struct proc *p = td->td_proc;
1506 struct kaioinfo *ki;
1508 if (p->p_aioinfo == NULL)
1509 aio_init_aioinfo(p);
1511 if (num_queue_count >= max_queue_count)
1515 if (ki->kaio_queue_count >= ki->kaio_qallowed_count)
1518 return _aio_aqueue(td, job, NULL, type);
1522 * Support the aio_return system call, as a side-effect, kernel resources are
1526 aio_return(struct thread *td, struct aio_return_args *uap)
1528 struct proc *p = td->td_proc;
1531 struct aiocblist *cb, *ncb;
1533 struct kaioinfo *ki;
1536 jobref = fuword(&ujob->_aiocb_private.kernelinfo);
1537 if (jobref == -1 || jobref == 0)
1543 TAILQ_FOREACH(cb, &ki->kaio_jobdone, plist) {
1544 if (((intptr_t) cb->uaiocb._aiocb_private.kernelinfo) ==
1546 if (cb->uaiocb.aio_lio_opcode == LIO_WRITE) {
1547 p->p_stats->p_ru.ru_oublock +=
1549 cb->outputcharge = 0;
1550 } else if (cb->uaiocb.aio_lio_opcode == LIO_READ) {
1551 p->p_stats->p_ru.ru_inblock += cb->inputcharge;
1552 cb->inputcharge = 0;
1558 for (cb = TAILQ_FIRST(&ki->kaio_bufdone); cb; cb = ncb) {
1559 ncb = TAILQ_NEXT(cb, plist);
1560 if (((intptr_t) cb->uaiocb._aiocb_private.kernelinfo)
1568 if (ujob == cb->uuaiocb) {
1570 cb->uaiocb._aiocb_private.status;
1572 td->td_retval[0] = EFAULT;
1580 * Allow a process to wakeup when any of the I/O requests are completed.
1583 aio_suspend(struct thread *td, struct aio_suspend_args *uap)
1585 struct proc *p = td->td_proc;
1588 struct aiocb *const *cbptr, *cbp;
1589 struct kaioinfo *ki;
1590 struct aiocblist *cb;
1595 struct aiocb **ujoblist;
1597 if (uap->nent > AIO_LISTIO_MAX)
1602 /* Get timespec struct. */
1603 if ((error = copyin(uap->timeout, &ts, sizeof(ts))) != 0)
1606 if (ts.tv_nsec < 0 || ts.tv_nsec >= 1000000000)
1609 TIMESPEC_TO_TIMEVAL(&atv, &ts);
1610 if (itimerfix(&atv))
1612 timo = tvtohz(&atv);
1620 ijoblist = uma_zalloc(aiol_zone, M_WAITOK);
1621 ujoblist = uma_zalloc(aiol_zone, M_WAITOK);
1622 cbptr = uap->aiocbp;
1624 for (i = 0; i < uap->nent; i++) {
1625 cbp = (struct aiocb *)(intptr_t)fuword(&cbptr[i]);
1628 ujoblist[njoblist] = cbp;
1629 ijoblist[njoblist] = fuword(&cbp->_aiocb_private.kernelinfo);
1633 if (njoblist == 0) {
1634 uma_zfree(aiol_zone, ijoblist);
1635 uma_zfree(aiol_zone, ujoblist);
1641 TAILQ_FOREACH(cb, &ki->kaio_jobdone, plist) {
1642 for (i = 0; i < njoblist; i++) {
1644 cb->uaiocb._aiocb_private.kernelinfo) ==
1646 if (ujoblist[i] != cb->uuaiocb)
1648 uma_zfree(aiol_zone, ijoblist);
1649 uma_zfree(aiol_zone, ujoblist);
1656 for (cb = TAILQ_FIRST(&ki->kaio_bufdone); cb; cb =
1657 TAILQ_NEXT(cb, plist)) {
1658 for (i = 0; i < njoblist; i++) {
1660 cb->uaiocb._aiocb_private.kernelinfo) ==
1663 if (ujoblist[i] != cb->uuaiocb)
1665 uma_zfree(aiol_zone, ijoblist);
1666 uma_zfree(aiol_zone, ujoblist);
1672 ki->kaio_flags |= KAIO_WAKEUP;
1673 error = tsleep(p, PRIBIO | PCATCH, "aiospn", timo);
1676 if (error == ERESTART || error == EINTR) {
1677 uma_zfree(aiol_zone, ijoblist);
1678 uma_zfree(aiol_zone, ujoblist);
1680 } else if (error == EWOULDBLOCK) {
1681 uma_zfree(aiol_zone, ijoblist);
1682 uma_zfree(aiol_zone, ujoblist);
1692 * aio_cancel cancels any non-physio aio operations not currently in
1696 aio_cancel(struct thread *td, struct aio_cancel_args *uap)
1698 struct proc *p = td->td_proc;
1699 struct kaioinfo *ki;
1700 struct aiocblist *cbe, *cbn;
1702 struct filedesc *fdp;
1711 if ((u_int)uap->fd >= fdp->fd_nfiles ||
1712 (fp = fdp->fd_ofiles[uap->fd]) == NULL)
1715 if (fp->f_type == DTYPE_VNODE) {
1716 vp = (struct vnode *)fp->f_data;
1718 if (vn_isdisk(vp,&error)) {
1719 td->td_retval[0] = AIO_NOTCANCELED;
1722 } else if (fp->f_type == DTYPE_SOCKET) {
1723 so = (struct socket *)fp->f_data;
1727 for (cbe = TAILQ_FIRST(&so->so_aiojobq); cbe; cbe = cbn) {
1728 cbn = TAILQ_NEXT(cbe, list);
1729 if ((uap->aiocbp == NULL) ||
1730 (uap->aiocbp == cbe->uuaiocb) ) {
1733 TAILQ_REMOVE(&so->so_aiojobq, cbe, list);
1734 TAILQ_REMOVE(&ki->kaio_sockqueue, cbe, plist);
1735 TAILQ_INSERT_TAIL(&ki->kaio_jobdone, cbe, plist);
1736 if (ki->kaio_flags & KAIO_WAKEUP) {
1739 cbe->jobstate = JOBST_JOBFINISHED;
1740 cbe->uaiocb._aiocb_private.status=-1;
1741 cbe->uaiocb._aiocb_private.error=ECANCELED;
1743 /* XXX cancelled, knote? */
1744 if (cbe->uaiocb.aio_sigevent.sigev_notify ==
1746 PROC_LOCK(cbe->userproc);
1747 psignal(cbe->userproc, cbe->uaiocb.aio_sigevent.sigev_signo);
1748 PROC_UNLOCK(cbe->userproc);
1756 if ((cancelled) && (uap->aiocbp)) {
1757 td->td_retval[0] = AIO_CANCELED;
1766 for (cbe = TAILQ_FIRST(&ki->kaio_jobqueue); cbe; cbe = cbn) {
1767 cbn = TAILQ_NEXT(cbe, plist);
1769 if ((uap->fd == cbe->uaiocb.aio_fildes) &&
1770 ((uap->aiocbp == NULL ) ||
1771 (uap->aiocbp == cbe->uuaiocb))) {
1773 if (cbe->jobstate == JOBST_JOBQGLOBAL) {
1774 TAILQ_REMOVE(&aio_jobs, cbe, list);
1775 TAILQ_REMOVE(&ki->kaio_jobqueue, cbe, plist);
1776 TAILQ_INSERT_TAIL(&ki->kaio_jobdone, cbe,
1779 ki->kaio_queue_finished_count++;
1780 cbe->jobstate = JOBST_JOBFINISHED;
1781 cbe->uaiocb._aiocb_private.status = -1;
1782 cbe->uaiocb._aiocb_private.error = ECANCELED;
1783 /* XXX cancelled, knote? */
1784 if (cbe->uaiocb.aio_sigevent.sigev_notify ==
1786 PROC_LOCK(cbe->userproc);
1787 psignal(cbe->userproc, cbe->uaiocb.aio_sigevent.sigev_signo);
1788 PROC_UNLOCK(cbe->userproc);
1798 td->td_retval[0] = AIO_NOTCANCELED;
1802 td->td_retval[0] = AIO_CANCELED;
1805 td->td_retval[0] = AIO_ALLDONE;
1811 * aio_error is implemented in the kernel level for compatibility purposes only.
1812 * For a user mode async implementation, it would be best to do it in a userland
1816 aio_error(struct thread *td, struct aio_error_args *uap)
1818 struct proc *p = td->td_proc;
1820 struct aiocblist *cb;
1821 struct kaioinfo *ki;
1828 jobref = fuword(&uap->aiocbp->_aiocb_private.kernelinfo);
1829 if ((jobref == -1) || (jobref == 0))
1832 TAILQ_FOREACH(cb, &ki->kaio_jobdone, plist) {
1833 if (((intptr_t)cb->uaiocb._aiocb_private.kernelinfo) ==
1835 td->td_retval[0] = cb->uaiocb._aiocb_private.error;
1842 for (cb = TAILQ_FIRST(&ki->kaio_jobqueue); cb; cb = TAILQ_NEXT(cb,
1844 if (((intptr_t)cb->uaiocb._aiocb_private.kernelinfo) ==
1846 td->td_retval[0] = EINPROGRESS;
1852 for (cb = TAILQ_FIRST(&ki->kaio_sockqueue); cb; cb = TAILQ_NEXT(cb,
1854 if (((intptr_t)cb->uaiocb._aiocb_private.kernelinfo) ==
1856 td->td_retval[0] = EINPROGRESS;
1864 for (cb = TAILQ_FIRST(&ki->kaio_bufdone); cb; cb = TAILQ_NEXT(cb,
1866 if (((intptr_t)cb->uaiocb._aiocb_private.kernelinfo) ==
1868 td->td_retval[0] = cb->uaiocb._aiocb_private.error;
1874 for (cb = TAILQ_FIRST(&ki->kaio_bufqueue); cb; cb = TAILQ_NEXT(cb,
1876 if (((intptr_t)cb->uaiocb._aiocb_private.kernelinfo) ==
1878 td->td_retval[0] = EINPROGRESS;
1889 status = fuword(&uap->aiocbp->_aiocb_private.status);
1891 return fuword(&uap->aiocbp->_aiocb_private.error);
1896 /* syscall - asynchronous read from a file (REALTIME) */
1898 aio_read(struct thread *td, struct aio_read_args *uap)
1901 return aio_aqueue(td, uap->aiocbp, LIO_READ);
1904 /* syscall - asynchronous write to a file (REALTIME) */
1906 aio_write(struct thread *td, struct aio_write_args *uap)
1909 return aio_aqueue(td, uap->aiocbp, LIO_WRITE);
1912 /* syscall - XXX undocumented */
1914 lio_listio(struct thread *td, struct lio_listio_args *uap)
1916 struct proc *p = td->td_proc;
1917 int nent, nentqueued;
1918 struct aiocb *iocb, * const *cbptr;
1919 struct aiocblist *cb;
1920 struct kaioinfo *ki;
1921 struct aio_liojob *lj;
1922 int error, runningcode;
1927 if ((uap->mode != LIO_NOWAIT) && (uap->mode != LIO_WAIT))
1931 if (nent > AIO_LISTIO_MAX)
1934 if (p->p_aioinfo == NULL)
1935 aio_init_aioinfo(p);
1937 if ((nent + num_queue_count) > max_queue_count)
1941 if ((nent + ki->kaio_queue_count) > ki->kaio_qallowed_count)
1944 lj = uma_zalloc(aiolio_zone, M_WAITOK);
1949 lj->lioj_buffer_count = 0;
1950 lj->lioj_buffer_finished_count = 0;
1951 lj->lioj_queue_count = 0;
1952 lj->lioj_queue_finished_count = 0;
1958 if (uap->sig && (uap->mode == LIO_NOWAIT)) {
1959 error = copyin(uap->sig, &lj->lioj_signal,
1960 sizeof(lj->lioj_signal));
1962 uma_zfree(aiolio_zone, lj);
1965 if (!_SIG_VALID(lj->lioj_signal.sigev_signo)) {
1966 uma_zfree(aiolio_zone, lj);
1969 lj->lioj_flags |= LIOJ_SIGNAL;
1971 TAILQ_INSERT_TAIL(&ki->kaio_liojoblist, lj, lioj_list);
1973 * Get pointers to the list of I/O requests.
1977 cbptr = uap->acb_list;
1978 for (i = 0; i < uap->nent; i++) {
1979 iocb = (struct aiocb *)(intptr_t)fuword(&cbptr[i]);
1980 if (((intptr_t)iocb != -1) && ((intptr_t)iocb != 0)) {
1981 error = _aio_aqueue(td, iocb, lj, 0);
1990 * If we haven't queued any, then just return error.
1992 if (nentqueued == 0)
1996 * Calculate the appropriate error return.
2002 if (uap->mode == LIO_WAIT) {
2003 int command, found, jobref;
2007 for (i = 0; i < uap->nent; i++) {
2009 * Fetch address of the control buf pointer in
2012 iocb = (struct aiocb *)
2013 (intptr_t)fuword(&cbptr[i]);
2014 if (((intptr_t)iocb == -1) || ((intptr_t)iocb
2019 * Fetch the associated command from user space.
2021 command = fuword(&iocb->aio_lio_opcode);
2022 if (command == LIO_NOP) {
2027 jobref = fuword(&iocb->_aiocb_private.kernelinfo);
2029 TAILQ_FOREACH(cb, &ki->kaio_jobdone, plist) {
2030 if (((intptr_t)cb->uaiocb._aiocb_private.kernelinfo)
2032 if (cb->uaiocb.aio_lio_opcode
2034 p->p_stats->p_ru.ru_oublock
2037 cb->outputcharge = 0;
2038 } else if (cb->uaiocb.aio_lio_opcode
2040 p->p_stats->p_ru.ru_inblock
2042 cb->inputcharge = 0;
2050 TAILQ_FOREACH(cb, &ki->kaio_bufdone, plist) {
2051 if (((intptr_t)cb->uaiocb._aiocb_private.kernelinfo)
2061 * If all I/Os have been disposed of, then we can
2064 if (found == nentqueued)
2067 ki->kaio_flags |= KAIO_WAKEUP;
2068 error = tsleep(p, PRIBIO | PCATCH, "aiospn", 0);
2072 else if (error == EWOULDBLOCK)
2081 * This is a weird hack so that we can post a signal. It is safe to do so from
2082 * a timeout routine, but *not* from an interrupt routine.
2085 process_signal(void *aioj)
2087 struct aiocblist *aiocbe = aioj;
2088 struct aio_liojob *lj = aiocbe->lio;
2089 struct aiocb *cb = &aiocbe->uaiocb;
2091 if ((lj) && (lj->lioj_signal.sigev_notify == SIGEV_SIGNAL) &&
2092 (lj->lioj_queue_count == lj->lioj_queue_finished_count)) {
2093 PROC_LOCK(lj->lioj_ki->kaio_p);
2094 psignal(lj->lioj_ki->kaio_p, lj->lioj_signal.sigev_signo);
2095 PROC_UNLOCK(lj->lioj_ki->kaio_p);
2096 lj->lioj_flags |= LIOJ_SIGNAL_POSTED;
2099 if (cb->aio_sigevent.sigev_notify == SIGEV_SIGNAL) {
2100 PROC_LOCK(aiocbe->userproc);
2101 psignal(aiocbe->userproc, cb->aio_sigevent.sigev_signo);
2102 PROC_UNLOCK(aiocbe->userproc);
2107 * Interrupt handler for physio, performs the necessary process wakeups, and
2111 aio_physwakeup(struct buf *bp)
2113 struct aiocblist *aiocbe;
2115 struct kaioinfo *ki;
2116 struct aio_liojob *lj;
2120 aiocbe = (struct aiocblist *)bp->b_spc;
2124 aiocbe->jobstate = JOBST_JOBBFINISHED;
2125 aiocbe->uaiocb._aiocb_private.status -= bp->b_resid;
2126 aiocbe->uaiocb._aiocb_private.error = 0;
2127 aiocbe->jobflags |= AIOCBLIST_DONE;
2129 if (bp->b_ioflags & BIO_ERROR)
2130 aiocbe->uaiocb._aiocb_private.error = bp->b_error;
2134 lj->lioj_buffer_finished_count++;
2137 * wakeup/signal if all of the interrupt jobs are done.
2139 if (lj->lioj_buffer_finished_count ==
2140 lj->lioj_buffer_count) {
2142 * Post a signal if it is called for.
2144 if ((lj->lioj_flags &
2145 (LIOJ_SIGNAL|LIOJ_SIGNAL_POSTED)) ==
2147 lj->lioj_flags |= LIOJ_SIGNAL_POSTED;
2148 aiocbe->timeouthandle =
2149 timeout(process_signal,
2157 ki->kaio_buffer_finished_count++;
2158 TAILQ_REMOVE(&aio_bufjobs, aiocbe, list);
2159 TAILQ_REMOVE(&ki->kaio_bufqueue, aiocbe, plist);
2160 TAILQ_INSERT_TAIL(&ki->kaio_bufdone, aiocbe, plist);
2162 KNOTE(&aiocbe->klist, 0);
2163 /* Do the wakeup. */
2164 if (ki->kaio_flags & (KAIO_RUNDOWN|KAIO_WAKEUP)) {
2165 ki->kaio_flags &= ~KAIO_WAKEUP;
2170 if (aiocbe->uaiocb.aio_sigevent.sigev_notify == SIGEV_SIGNAL)
2171 aiocbe->timeouthandle =
2172 timeout(process_signal, aiocbe, 0);
2176 /* syscall - wait for the next completion of an aio request */
2178 aio_waitcomplete(struct thread *td, struct aio_waitcomplete_args *uap)
2180 struct proc *p = td->td_proc;
2183 struct kaioinfo *ki;
2184 struct aiocblist *cb = NULL;
2187 suword(uap->aiocbp, (int)NULL);
2191 /* Get timespec struct. */
2192 error = copyin(uap->timeout, &ts, sizeof(ts));
2196 if ((ts.tv_nsec < 0) || (ts.tv_nsec >= 1000000000))
2199 TIMESPEC_TO_TIMEVAL(&atv, &ts);
2200 if (itimerfix(&atv))
2202 timo = tvtohz(&atv);
2210 if ((cb = TAILQ_FIRST(&ki->kaio_jobdone)) != 0) {
2211 suword(uap->aiocbp, (uintptr_t)cb->uuaiocb);
2212 td->td_retval[0] = cb->uaiocb._aiocb_private.status;
2213 if (cb->uaiocb.aio_lio_opcode == LIO_WRITE) {
2214 p->p_stats->p_ru.ru_oublock +=
2216 cb->outputcharge = 0;
2217 } else if (cb->uaiocb.aio_lio_opcode == LIO_READ) {
2218 p->p_stats->p_ru.ru_inblock += cb->inputcharge;
2219 cb->inputcharge = 0;
2222 return cb->uaiocb._aiocb_private.error;
2226 if ((cb = TAILQ_FIRST(&ki->kaio_bufdone)) != 0 ) {
2228 suword(uap->aiocbp, (uintptr_t)cb->uuaiocb);
2229 td->td_retval[0] = cb->uaiocb._aiocb_private.status;
2231 return cb->uaiocb._aiocb_private.error;
2234 ki->kaio_flags |= KAIO_WAKEUP;
2235 error = tsleep(p, PRIBIO | PCATCH, "aiowc", timo);
2238 if (error == ERESTART)
2242 else if (error == EINTR)
2244 else if (error == EWOULDBLOCK)
2249 /* kqueue attach function */
2251 filt_aioattach(struct knote *kn)
2253 struct aiocblist *aiocbe = (struct aiocblist *)kn->kn_sdata;
2256 * The aiocbe pointer must be validated before using it, so
2257 * registration is restricted to the kernel; the user cannot
2260 if ((kn->kn_flags & EV_FLAG1) == 0)
2262 kn->kn_flags &= ~EV_FLAG1;
2264 SLIST_INSERT_HEAD(&aiocbe->klist, kn, kn_selnext);
2269 /* kqueue detach function */
2271 filt_aiodetach(struct knote *kn)
2273 struct aiocblist *aiocbe = (struct aiocblist *)kn->kn_sdata;
2275 SLIST_REMOVE(&aiocbe->klist, kn, knote, kn_selnext);
2278 /* kqueue filter function */
2281 filt_aio(struct knote *kn, long hint)
2283 struct aiocblist *aiocbe = (struct aiocblist *)kn->kn_sdata;
2285 kn->kn_data = aiocbe->uaiocb._aiocb_private.error;
2286 if (aiocbe->jobstate != JOBST_JOBFINISHED &&
2287 aiocbe->jobstate != JOBST_JOBBFINISHED)
2289 kn->kn_flags |= EV_EOF;