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.
18 * This file contains support for the POSIX 1003.1B AIO/LIO facility.
21 #include <sys/cdefs.h>
22 __FBSDID("$FreeBSD$");
24 #include <sys/param.h>
25 #include <sys/systm.h>
26 #include <sys/malloc.h>
29 #include <sys/eventhandler.h>
30 #include <sys/sysproto.h>
31 #include <sys/filedesc.h>
32 #include <sys/kernel.h>
33 #include <sys/module.h>
34 #include <sys/kthread.h>
35 #include <sys/fcntl.h>
37 #include <sys/limits.h>
39 #include <sys/mutex.h>
40 #include <sys/unistd.h>
42 #include <sys/resourcevar.h>
43 #include <sys/signalvar.h>
44 #include <sys/protosw.h>
45 #include <sys/socketvar.h>
46 #include <sys/syscall.h>
47 #include <sys/sysent.h>
48 #include <sys/sysctl.h>
50 #include <sys/vnode.h>
52 #include <sys/event.h>
54 #include <posix4/posix4.h>
56 #include <vm/vm_extern.h>
58 #include <vm/vm_map.h>
62 #include "opt_vfs_aio.h"
64 NET_NEEDS_GIANT("aio");
67 * Counter for allocating reference ids to new jobs. Wrapped to 1 on
72 #define JOBST_NULL 0x0
73 #define JOBST_JOBQGLOBAL 0x2
74 #define JOBST_JOBRUNNING 0x3
75 #define JOBST_JOBFINISHED 0x4
76 #define JOBST_JOBQBUF 0x5
77 #define JOBST_JOBBFINISHED 0x6
79 #ifndef MAX_AIO_PER_PROC
80 #define MAX_AIO_PER_PROC 32
83 #ifndef MAX_AIO_QUEUE_PER_PROC
84 #define MAX_AIO_QUEUE_PER_PROC 256 /* Bigger than AIO_LISTIO_MAX */
88 #define MAX_AIO_PROCS 32
92 #define MAX_AIO_QUEUE 1024 /* Bigger than AIO_LISTIO_MAX */
95 #ifndef TARGET_AIO_PROCS
96 #define TARGET_AIO_PROCS 4
100 #define MAX_BUF_AIO 16
103 #ifndef AIOD_TIMEOUT_DEFAULT
104 #define AIOD_TIMEOUT_DEFAULT (10 * hz)
107 #ifndef AIOD_LIFETIME_DEFAULT
108 #define AIOD_LIFETIME_DEFAULT (30 * hz)
111 static SYSCTL_NODE(_vfs, OID_AUTO, aio, CTLFLAG_RW, 0, "Async IO management");
113 static int max_aio_procs = MAX_AIO_PROCS;
114 SYSCTL_INT(_vfs_aio, OID_AUTO, max_aio_procs,
115 CTLFLAG_RW, &max_aio_procs, 0,
116 "Maximum number of kernel threads to use for handling async IO ");
118 static int num_aio_procs = 0;
119 SYSCTL_INT(_vfs_aio, OID_AUTO, num_aio_procs,
120 CTLFLAG_RD, &num_aio_procs, 0,
121 "Number of presently active kernel threads for async IO");
124 * The code will adjust the actual number of AIO processes towards this
125 * number when it gets a chance.
127 static int target_aio_procs = TARGET_AIO_PROCS;
128 SYSCTL_INT(_vfs_aio, OID_AUTO, target_aio_procs, CTLFLAG_RW, &target_aio_procs,
129 0, "Preferred number of ready kernel threads for async IO");
131 static int max_queue_count = MAX_AIO_QUEUE;
132 SYSCTL_INT(_vfs_aio, OID_AUTO, max_aio_queue, CTLFLAG_RW, &max_queue_count, 0,
133 "Maximum number of aio requests to queue, globally");
135 static int num_queue_count = 0;
136 SYSCTL_INT(_vfs_aio, OID_AUTO, num_queue_count, CTLFLAG_RD, &num_queue_count, 0,
137 "Number of queued aio requests");
139 static int num_buf_aio = 0;
140 SYSCTL_INT(_vfs_aio, OID_AUTO, num_buf_aio, CTLFLAG_RD, &num_buf_aio, 0,
141 "Number of aio requests presently handled by the buf subsystem");
143 /* Number of async I/O thread in the process of being started */
144 /* XXX This should be local to _aio_aqueue() */
145 static int num_aio_resv_start = 0;
147 static int aiod_timeout;
148 SYSCTL_INT(_vfs_aio, OID_AUTO, aiod_timeout, CTLFLAG_RW, &aiod_timeout, 0,
149 "Timeout value for synchronous aio operations");
151 static int aiod_lifetime;
152 SYSCTL_INT(_vfs_aio, OID_AUTO, aiod_lifetime, CTLFLAG_RW, &aiod_lifetime, 0,
153 "Maximum lifetime for idle aiod");
155 static int unloadable = 0;
156 SYSCTL_INT(_vfs_aio, OID_AUTO, unloadable, CTLFLAG_RW, &unloadable, 0,
157 "Allow unload of aio (not recommended)");
160 static int max_aio_per_proc = MAX_AIO_PER_PROC;
161 SYSCTL_INT(_vfs_aio, OID_AUTO, max_aio_per_proc, CTLFLAG_RW, &max_aio_per_proc,
162 0, "Maximum active aio requests per process (stored in the process)");
164 static int max_aio_queue_per_proc = MAX_AIO_QUEUE_PER_PROC;
165 SYSCTL_INT(_vfs_aio, OID_AUTO, max_aio_queue_per_proc, CTLFLAG_RW,
166 &max_aio_queue_per_proc, 0,
167 "Maximum queued aio requests per process (stored in the process)");
169 static int max_buf_aio = MAX_BUF_AIO;
170 SYSCTL_INT(_vfs_aio, OID_AUTO, max_buf_aio, CTLFLAG_RW, &max_buf_aio, 0,
171 "Maximum buf aio requests per process (stored in the process)");
174 TAILQ_ENTRY(aiocblist) list; /* List of jobs */
175 TAILQ_ENTRY(aiocblist) plist; /* List of jobs for proc */
180 struct buf *bp; /* Buffer pointer */
181 struct proc *userproc; /* User process */ /* Not td! */
182 struct ucred *cred; /* Active credential when created */
183 struct file *fd_file; /* Pointer to file structure */
184 struct aio_liojob *lio; /* Optional lio job */
185 struct aiocb *uuaiocb; /* Pointer in userspace of aiocb */
186 struct knlist klist; /* list of knotes */
187 struct aiocb uaiocb; /* Kernel I/O control block */
191 #define AIOCBLIST_RUNDOWN 0x4
192 #define AIOCBLIST_DONE 0x10
197 #define AIOP_FREE 0x1 /* proc on free queue */
199 struct aiothreadlist {
200 int aiothreadflags; /* AIO proc flags */
201 TAILQ_ENTRY(aiothreadlist) list; /* List of processes */
202 struct thread *aiothread; /* The AIO thread */
206 * data-structure for lio signal management
210 int lioj_buffer_count;
211 int lioj_buffer_finished_count;
212 int lioj_queue_count;
213 int lioj_queue_finished_count;
214 struct sigevent lioj_signal; /* signal on all I/O done */
215 TAILQ_ENTRY(aio_liojob) lioj_list;
217 #define LIOJ_SIGNAL 0x1 /* signal on all done (lio) */
218 #define LIOJ_SIGNAL_POSTED 0x2 /* signal has been posted */
221 * per process aio data structure
224 int kaio_flags; /* per process kaio flags */
225 int kaio_maxactive_count; /* maximum number of AIOs */
226 int kaio_active_count; /* number of currently used AIOs */
227 int kaio_qallowed_count; /* maxiumu size of AIO queue */
228 int kaio_queue_count; /* size of AIO queue */
229 int kaio_ballowed_count; /* maximum number of buffers */
230 int kaio_queue_finished_count; /* number of daemon jobs finished */
231 int kaio_buffer_count; /* number of physio buffers */
232 int kaio_buffer_finished_count; /* count of I/O done */
233 TAILQ_HEAD(,aio_liojob) kaio_liojoblist; /* list of lio jobs */
234 TAILQ_HEAD(,aiocblist) kaio_jobqueue; /* job queue for process */
235 TAILQ_HEAD(,aiocblist) kaio_jobdone; /* done queue for process */
236 TAILQ_HEAD(,aiocblist) kaio_bufqueue; /* buffer job queue for process */
237 TAILQ_HEAD(,aiocblist) kaio_bufdone; /* buffer done queue for process */
238 TAILQ_HEAD(,aiocblist) kaio_sockqueue; /* queue for aios waiting on sockets */
241 #define KAIO_RUNDOWN 0x1 /* process is being run down */
242 #define KAIO_WAKEUP 0x2 /* wakeup process when there is a significant event */
244 static TAILQ_HEAD(,aiothreadlist) aio_freeproc; /* Idle daemons */
245 static struct mtx aio_freeproc_mtx;
247 static TAILQ_HEAD(,aiocblist) aio_jobs; /* Async job list */
249 static void aio_init_aioinfo(struct proc *p);
250 static void aio_onceonly(void);
251 static int aio_free_entry(struct aiocblist *aiocbe);
252 static void aio_process(struct aiocblist *aiocbe);
253 static int aio_newproc(void);
254 static int aio_aqueue(struct thread *td, struct aiocb *job, int type);
255 static void aio_physwakeup(struct buf *bp);
256 static void aio_proc_rundown(void *arg, struct proc *p);
257 static int aio_fphysio(struct aiocblist *aiocbe);
258 static int aio_qphysio(struct proc *p, struct aiocblist *iocb);
259 static void aio_daemon(void *uproc);
260 static void aio_swake_cb(struct socket *, struct sockbuf *);
261 static int aio_unload(void);
262 static int filt_aioattach(struct knote *kn);
263 static void filt_aiodetach(struct knote *kn);
264 static int filt_aio(struct knote *kn, long hint);
268 * kaio Per process async io info
269 * aiop async io thread data
270 * aiocb async io jobs
271 * aiol list io job pointer - internal to aio_suspend XXX
272 * aiolio list io jobs
274 static uma_zone_t kaio_zone, aiop_zone, aiocb_zone, aiol_zone, aiolio_zone;
276 /* kqueue filters for aio */
277 static struct filterops aio_filtops =
278 { 0, filt_aioattach, filt_aiodetach, filt_aio };
280 static eventhandler_tag exit_tag, exec_tag;
283 * Main operations function for use as a kernel module.
286 aio_modload(struct module *module, int cmd, void *arg)
295 error = aio_unload();
306 static moduledata_t aio_mod = {
312 SYSCALL_MODULE_HELPER(aio_return);
313 SYSCALL_MODULE_HELPER(aio_suspend);
314 SYSCALL_MODULE_HELPER(aio_cancel);
315 SYSCALL_MODULE_HELPER(aio_error);
316 SYSCALL_MODULE_HELPER(aio_read);
317 SYSCALL_MODULE_HELPER(aio_write);
318 SYSCALL_MODULE_HELPER(aio_waitcomplete);
319 SYSCALL_MODULE_HELPER(lio_listio);
321 DECLARE_MODULE(aio, aio_mod,
322 SI_SUB_VFS, SI_ORDER_ANY);
323 MODULE_VERSION(aio, 1);
326 * Startup initialization
332 /* XXX: should probably just use so->callback */
333 aio_swake = &aio_swake_cb;
334 exit_tag = EVENTHANDLER_REGISTER(process_exit, aio_proc_rundown, NULL,
335 EVENTHANDLER_PRI_ANY);
336 exec_tag = EVENTHANDLER_REGISTER(process_exec, aio_proc_rundown, NULL,
337 EVENTHANDLER_PRI_ANY);
338 kqueue_add_filteropts(EVFILT_AIO, &aio_filtops);
339 TAILQ_INIT(&aio_freeproc);
340 mtx_init(&aio_freeproc_mtx, "aio_freeproc", NULL, MTX_DEF);
341 TAILQ_INIT(&aio_jobs);
342 kaio_zone = uma_zcreate("AIO", sizeof(struct kaioinfo), NULL, NULL,
343 NULL, NULL, UMA_ALIGN_PTR, UMA_ZONE_NOFREE);
344 aiop_zone = uma_zcreate("AIOP", sizeof(struct aiothreadlist), NULL,
345 NULL, NULL, NULL, UMA_ALIGN_PTR, UMA_ZONE_NOFREE);
346 aiocb_zone = uma_zcreate("AIOCB", sizeof(struct aiocblist), NULL, NULL,
347 NULL, NULL, UMA_ALIGN_PTR, UMA_ZONE_NOFREE);
348 aiol_zone = uma_zcreate("AIOL", AIO_LISTIO_MAX*sizeof(intptr_t) , NULL,
349 NULL, NULL, NULL, UMA_ALIGN_PTR, UMA_ZONE_NOFREE);
350 aiolio_zone = uma_zcreate("AIOLIO", sizeof(struct aio_liojob), NULL,
351 NULL, NULL, NULL, UMA_ALIGN_PTR, UMA_ZONE_NOFREE);
352 aiod_timeout = AIOD_TIMEOUT_DEFAULT;
353 aiod_lifetime = AIOD_LIFETIME_DEFAULT;
355 async_io_version = _POSIX_VERSION;
356 p31b_setcfg(CTL_P1003_1B_AIO_LISTIO_MAX, AIO_LISTIO_MAX);
357 p31b_setcfg(CTL_P1003_1B_AIO_MAX, MAX_AIO_QUEUE);
358 p31b_setcfg(CTL_P1003_1B_AIO_PRIO_DELTA_MAX, 0);
362 * Callback for unload of AIO when used as a module.
370 * XXX: no unloads by default, it's too dangerous.
371 * perhaps we could do it if locked out callers and then
372 * did an aio_proc_rundown() on each process.
377 error = kqueue_del_filteropts(EVFILT_AIO);
381 async_io_version = 0;
383 EVENTHANDLER_DEREGISTER(process_exit, exit_tag);
384 EVENTHANDLER_DEREGISTER(process_exec, exec_tag);
385 p31b_setcfg(CTL_P1003_1B_AIO_LISTIO_MAX, -1);
386 p31b_setcfg(CTL_P1003_1B_AIO_MAX, -1);
387 p31b_setcfg(CTL_P1003_1B_AIO_PRIO_DELTA_MAX, -1);
392 * Init the per-process aioinfo structure. The aioinfo limits are set
393 * per-process for user limit (resource) management.
396 aio_init_aioinfo(struct proc *p)
400 ki = uma_zalloc(kaio_zone, M_WAITOK);
402 ki->kaio_maxactive_count = max_aio_per_proc;
403 ki->kaio_active_count = 0;
404 ki->kaio_qallowed_count = max_aio_queue_per_proc;
405 ki->kaio_queue_count = 0;
406 ki->kaio_ballowed_count = max_buf_aio;
407 ki->kaio_buffer_count = 0;
408 ki->kaio_buffer_finished_count = 0;
409 TAILQ_INIT(&ki->kaio_jobdone);
410 TAILQ_INIT(&ki->kaio_jobqueue);
411 TAILQ_INIT(&ki->kaio_bufdone);
412 TAILQ_INIT(&ki->kaio_bufqueue);
413 TAILQ_INIT(&ki->kaio_liojoblist);
414 TAILQ_INIT(&ki->kaio_sockqueue);
416 if (p->p_aioinfo == NULL) {
421 uma_zfree(kaio_zone, ki);
424 while (num_aio_procs < target_aio_procs)
429 * Free a job entry. Wait for completion if it is currently active, but don't
430 * delay forever. If we delay, we return a flag that says that we have to
431 * restart the queue scan.
434 aio_free_entry(struct aiocblist *aiocbe)
437 struct aio_liojob *lj;
442 if (aiocbe->jobstate == JOBST_NULL)
443 panic("aio_free_entry: freeing already free job");
445 p = aiocbe->userproc;
449 panic("aio_free_entry: missing p->p_aioinfo");
451 while (aiocbe->jobstate == JOBST_JOBRUNNING) {
452 aiocbe->jobflags |= AIOCBLIST_RUNDOWN;
453 tsleep(aiocbe, PRIBIO, "jobwai", 0);
455 if (aiocbe->bp == NULL) {
456 if (ki->kaio_queue_count <= 0)
457 panic("aio_free_entry: process queue size <= 0");
458 if (num_queue_count <= 0)
459 panic("aio_free_entry: system wide queue size <= 0");
462 lj->lioj_queue_count--;
463 if (aiocbe->jobflags & AIOCBLIST_DONE)
464 lj->lioj_queue_finished_count--;
466 ki->kaio_queue_count--;
467 if (aiocbe->jobflags & AIOCBLIST_DONE)
468 ki->kaio_queue_finished_count--;
472 lj->lioj_buffer_count--;
473 if (aiocbe->jobflags & AIOCBLIST_DONE)
474 lj->lioj_buffer_finished_count--;
476 if (aiocbe->jobflags & AIOCBLIST_DONE)
477 ki->kaio_buffer_finished_count--;
478 ki->kaio_buffer_count--;
482 /* aiocbe is going away, we need to destroy any knotes */
483 /* XXXKSE Note the thread here is used to eventually find the
484 * owning process again, but it is also used to do a fo_close
485 * and that requires the thread. (but does it require the
486 * OWNING thread? (or maybe the running thread?)
487 * There is a semantic problem here...
489 knlist_delete(&aiocbe->klist, FIRST_THREAD_IN_PROC(p), 0); /* XXXKSE */
491 if ((ki->kaio_flags & KAIO_WAKEUP) || ((ki->kaio_flags & KAIO_RUNDOWN)
492 && ((ki->kaio_buffer_count == 0) && (ki->kaio_queue_count == 0)))) {
493 ki->kaio_flags &= ~KAIO_WAKEUP;
497 if (aiocbe->jobstate == JOBST_JOBQBUF) {
498 if ((error = aio_fphysio(aiocbe)) != 0)
500 if (aiocbe->jobstate != JOBST_JOBBFINISHED)
501 panic("aio_free_entry: invalid physio finish-up state");
503 TAILQ_REMOVE(&ki->kaio_bufdone, aiocbe, plist);
505 } else if (aiocbe->jobstate == JOBST_JOBQGLOBAL) {
507 TAILQ_REMOVE(&aio_jobs, aiocbe, list);
508 TAILQ_REMOVE(&ki->kaio_jobqueue, aiocbe, plist);
510 } else if (aiocbe->jobstate == JOBST_JOBFINISHED)
511 TAILQ_REMOVE(&ki->kaio_jobdone, aiocbe, plist);
512 else if (aiocbe->jobstate == JOBST_JOBBFINISHED) {
514 TAILQ_REMOVE(&ki->kaio_bufdone, aiocbe, plist);
517 vunmapbuf(aiocbe->bp);
518 relpbuf(aiocbe->bp, NULL);
522 if (lj && (lj->lioj_buffer_count == 0) && (lj->lioj_queue_count == 0)) {
523 TAILQ_REMOVE(&ki->kaio_liojoblist, lj, lioj_list);
524 uma_zfree(aiolio_zone, lj);
526 aiocbe->jobstate = JOBST_NULL;
527 fdrop(aiocbe->fd_file, curthread);
528 crfree(aiocbe->cred);
529 uma_zfree(aiocb_zone, aiocbe);
534 * Rundown the jobs for a given process.
537 aio_proc_rundown(void *arg, struct proc *p)
541 struct aio_liojob *lj, *ljn;
542 struct aiocblist *aiocbe, *aiocbn;
551 ki->kaio_flags |= LIOJ_SIGNAL_POSTED;
552 while ((ki->kaio_active_count > 0) || (ki->kaio_buffer_count >
553 ki->kaio_buffer_finished_count)) {
554 ki->kaio_flags |= KAIO_RUNDOWN;
555 if (tsleep(p, PRIBIO, "kaiowt", aiod_timeout))
560 * Move any aio ops that are waiting on socket I/O to the normal job
561 * queues so they are cleaned up with any others.
564 for (aiocbe = TAILQ_FIRST(&ki->kaio_sockqueue); aiocbe; aiocbe =
566 aiocbn = TAILQ_NEXT(aiocbe, plist);
567 fp = aiocbe->fd_file;
570 TAILQ_REMOVE(&so->so_aiojobq, aiocbe, list);
571 if (TAILQ_EMPTY(&so->so_aiojobq)) {
572 SOCKBUF_LOCK(&so->so_snd);
573 so->so_snd.sb_flags &= ~SB_AIO;
574 SOCKBUF_UNLOCK(&so->so_snd);
575 SOCKBUF_LOCK(&so->so_rcv);
576 so->so_rcv.sb_flags &= ~SB_AIO;
577 SOCKBUF_UNLOCK(&so->so_rcv);
580 TAILQ_REMOVE(&ki->kaio_sockqueue, aiocbe, plist);
581 TAILQ_INSERT_HEAD(&aio_jobs, aiocbe, list);
582 TAILQ_INSERT_HEAD(&ki->kaio_jobqueue, aiocbe, plist);
587 for (aiocbe = TAILQ_FIRST(&ki->kaio_jobdone); aiocbe; aiocbe = aiocbn) {
588 aiocbn = TAILQ_NEXT(aiocbe, plist);
589 if (aio_free_entry(aiocbe))
594 for (aiocbe = TAILQ_FIRST(&ki->kaio_jobqueue); aiocbe; aiocbe =
596 aiocbn = TAILQ_NEXT(aiocbe, plist);
597 if (aio_free_entry(aiocbe))
602 * Note the use of lots of splbio here, trying to avoid splbio for long chains
603 * of I/O. Probably unnecessary.
607 while (TAILQ_FIRST(&ki->kaio_bufqueue)) {
608 ki->kaio_flags |= KAIO_WAKEUP;
609 tsleep(p, PRIBIO, "aioprn", 0);
617 for (aiocbe = TAILQ_FIRST(&ki->kaio_bufdone); aiocbe; aiocbe = aiocbn) {
618 aiocbn = TAILQ_NEXT(aiocbe, plist);
619 if (aio_free_entry(aiocbe)) {
627 * If we've slept, jobs might have moved from one queue to another.
628 * Retry rundown if we didn't manage to empty the queues.
630 if (TAILQ_FIRST(&ki->kaio_jobdone) != NULL ||
631 TAILQ_FIRST(&ki->kaio_jobqueue) != NULL ||
632 TAILQ_FIRST(&ki->kaio_bufqueue) != NULL ||
633 TAILQ_FIRST(&ki->kaio_bufdone) != NULL)
636 for (lj = TAILQ_FIRST(&ki->kaio_liojoblist); lj; lj = ljn) {
637 ljn = TAILQ_NEXT(lj, lioj_list);
638 if ((lj->lioj_buffer_count == 0) && (lj->lioj_queue_count ==
640 TAILQ_REMOVE(&ki->kaio_liojoblist, lj, lioj_list);
641 uma_zfree(aiolio_zone, lj);
644 printf("LIO job not cleaned up: B:%d, BF:%d, Q:%d, "
645 "QF:%d\n", lj->lioj_buffer_count,
646 lj->lioj_buffer_finished_count,
647 lj->lioj_queue_count,
648 lj->lioj_queue_finished_count);
653 uma_zfree(kaio_zone, ki);
659 * Select a job to run (called by an AIO daemon).
661 static struct aiocblist *
662 aio_selectjob(struct aiothreadlist *aiop)
665 struct aiocblist *aiocbe;
670 for (aiocbe = TAILQ_FIRST(&aio_jobs); aiocbe; aiocbe =
671 TAILQ_NEXT(aiocbe, list)) {
672 userp = aiocbe->userproc;
673 ki = userp->p_aioinfo;
675 if (ki->kaio_active_count < ki->kaio_maxactive_count) {
676 TAILQ_REMOVE(&aio_jobs, aiocbe, list);
687 * The AIO processing activity. This is the code that does the I/O request for
688 * the non-physio version of the operations. The normal vn operations are used,
689 * and this code should work in all instances for every type of file, including
690 * pipes, sockets, fifos, and regular files.
693 aio_process(struct aiocblist *aiocbe)
695 struct ucred *td_savedcred;
704 int oublock_st, oublock_end;
705 int inblock_st, inblock_end;
708 td_savedcred = td->td_ucred;
709 td->td_ucred = aiocbe->cred;
711 cb = &aiocbe->uaiocb;
712 fp = aiocbe->fd_file;
714 aiov.iov_base = (void *)(uintptr_t)cb->aio_buf;
715 aiov.iov_len = cb->aio_nbytes;
717 auio.uio_iov = &aiov;
719 auio.uio_offset = cb->aio_offset;
720 auio.uio_resid = cb->aio_nbytes;
721 cnt = cb->aio_nbytes;
722 auio.uio_segflg = UIO_USERSPACE;
725 inblock_st = mycp->p_stats->p_ru.ru_inblock;
726 oublock_st = mycp->p_stats->p_ru.ru_oublock;
728 * _aio_aqueue() acquires a reference to the file that is
729 * released in aio_free_entry().
731 if (cb->aio_lio_opcode == LIO_READ) {
732 auio.uio_rw = UIO_READ;
733 error = fo_read(fp, &auio, fp->f_cred, FOF_OFFSET, td);
735 auio.uio_rw = UIO_WRITE;
736 error = fo_write(fp, &auio, fp->f_cred, FOF_OFFSET, td);
738 inblock_end = mycp->p_stats->p_ru.ru_inblock;
739 oublock_end = mycp->p_stats->p_ru.ru_oublock;
741 aiocbe->inputcharge = inblock_end - inblock_st;
742 aiocbe->outputcharge = oublock_end - oublock_st;
744 if ((error) && (auio.uio_resid != cnt)) {
745 if (error == ERESTART || error == EINTR || error == EWOULDBLOCK)
747 if ((error == EPIPE) && (cb->aio_lio_opcode == LIO_WRITE)) {
748 PROC_LOCK(aiocbe->userproc);
749 psignal(aiocbe->userproc, SIGPIPE);
750 PROC_UNLOCK(aiocbe->userproc);
754 cnt -= auio.uio_resid;
755 cb->_aiocb_private.error = error;
756 cb->_aiocb_private.status = cnt;
757 td->td_ucred = td_savedcred;
761 * The AIO daemon, most of the actual work is done in aio_process,
762 * but the setup (and address space mgmt) is done in this routine.
765 aio_daemon(void *uproc)
768 struct aio_liojob *lj;
770 struct aiocblist *aiocbe;
771 struct aiothreadlist *aiop;
773 struct proc *curcp, *mycp, *userp;
774 struct vmspace *myvm, *tmpvm;
775 struct thread *td = curthread;
776 struct pgrp *newpgrp;
777 struct session *newsess;
780 * Local copies of curproc (cp) and vmspace (myvm)
783 myvm = mycp->p_vmspace;
785 KASSERT(mycp->p_textvp == NULL, ("kthread has a textvp"));
788 * Allocate and ready the aio control info. There is one aiop structure
791 aiop = uma_zalloc(aiop_zone, M_WAITOK);
792 aiop->aiothread = td;
793 aiop->aiothreadflags |= AIOP_FREE;
796 * Place thread (lightweight process) onto the AIO free thread list.
798 mtx_lock(&aio_freeproc_mtx);
799 TAILQ_INSERT_HEAD(&aio_freeproc, aiop, list);
800 mtx_unlock(&aio_freeproc_mtx);
803 * Get rid of our current filedescriptors. AIOD's don't need any
804 * filedescriptors, except as temporarily inherited from the client.
810 /* The daemon resides in its own pgrp. */
811 MALLOC(newpgrp, struct pgrp *, sizeof(struct pgrp), M_PGRP,
813 MALLOC(newsess, struct session *, sizeof(struct session), M_SESSION,
816 sx_xlock(&proctree_lock);
817 enterpgrp(mycp, mycp->p_pid, newpgrp, newsess);
818 sx_xunlock(&proctree_lock);
822 * Wakeup parent process. (Parent sleeps to keep from blasting away
823 * and creating too many daemons.)
829 * curcp is the current daemon process context.
830 * userp is the current user process context.
835 * Take daemon off of free queue
837 mtx_lock(&aio_freeproc_mtx);
838 if (aiop->aiothreadflags & AIOP_FREE) {
839 TAILQ_REMOVE(&aio_freeproc, aiop, list);
840 aiop->aiothreadflags &= ~AIOP_FREE;
842 mtx_unlock(&aio_freeproc_mtx);
847 while ((aiocbe = aio_selectjob(aiop)) != NULL) {
848 cb = &aiocbe->uaiocb;
849 userp = aiocbe->userproc;
851 aiocbe->jobstate = JOBST_JOBRUNNING;
854 * Connect to process address space for user program.
856 if (userp != curcp) {
858 * Save the current address space that we are
861 tmpvm = mycp->p_vmspace;
864 * Point to the new user address space, and
867 mycp->p_vmspace = userp->p_vmspace;
868 atomic_add_int(&mycp->p_vmspace->vm_refcnt, 1);
870 /* Activate the new mapping. */
871 pmap_activate(FIRST_THREAD_IN_PROC(mycp));
874 * If the old address space wasn't the daemons
875 * own address space, then we need to remove the
876 * daemon's reference from the other process
877 * that it was acting on behalf of.
885 ki = userp->p_aioinfo;
888 /* Account for currently active jobs. */
889 ki->kaio_active_count++;
891 /* Do the I/O function. */
894 /* Decrement the active job count. */
895 ki->kaio_active_count--;
898 * Increment the completion count for wakeup/signal
901 aiocbe->jobflags |= AIOCBLIST_DONE;
902 ki->kaio_queue_finished_count++;
904 lj->lioj_queue_finished_count++;
905 if ((ki->kaio_flags & KAIO_WAKEUP) || ((ki->kaio_flags
906 & KAIO_RUNDOWN) && (ki->kaio_active_count == 0))) {
907 ki->kaio_flags &= ~KAIO_WAKEUP;
912 if (lj && (lj->lioj_flags &
913 (LIOJ_SIGNAL|LIOJ_SIGNAL_POSTED)) == LIOJ_SIGNAL) {
914 if ((lj->lioj_queue_finished_count ==
915 lj->lioj_queue_count) &&
916 (lj->lioj_buffer_finished_count ==
917 lj->lioj_buffer_count)) {
920 lj->lioj_signal.sigev_signo);
922 lj->lioj_flags |= LIOJ_SIGNAL_POSTED;
927 aiocbe->jobstate = JOBST_JOBFINISHED;
930 TAILQ_REMOVE(&ki->kaio_jobqueue, aiocbe, plist);
931 TAILQ_INSERT_TAIL(&ki->kaio_jobdone, aiocbe, plist);
933 KNOTE_UNLOCKED(&aiocbe->klist, 0);
935 if (aiocbe->jobflags & AIOCBLIST_RUNDOWN) {
937 aiocbe->jobflags &= ~AIOCBLIST_RUNDOWN;
940 if (cb->aio_sigevent.sigev_notify == SIGEV_SIGNAL) {
942 psignal(userp, cb->aio_sigevent.sigev_signo);
948 * Disconnect from user address space.
951 /* Get the user address space to disconnect from. */
952 tmpvm = mycp->p_vmspace;
954 /* Get original address space for daemon. */
955 mycp->p_vmspace = myvm;
957 /* Activate the daemon's address space. */
958 pmap_activate(FIRST_THREAD_IN_PROC(mycp));
961 printf("AIOD: vmspace problem -- %d\n",
965 /* Remove our vmspace reference. */
971 mtx_lock(&aio_freeproc_mtx);
972 TAILQ_INSERT_HEAD(&aio_freeproc, aiop, list);
973 aiop->aiothreadflags |= AIOP_FREE;
976 * If daemon is inactive for a long time, allow it to exit,
977 * thereby freeing resources.
979 if (msleep(aiop->aiothread, &aio_freeproc_mtx, PDROP | PRIBIO,
980 "aiordy", aiod_lifetime)) {
982 if (TAILQ_EMPTY(&aio_jobs)) {
983 mtx_lock(&aio_freeproc_mtx);
984 if ((aiop->aiothreadflags & AIOP_FREE) &&
985 (num_aio_procs > target_aio_procs)) {
986 TAILQ_REMOVE(&aio_freeproc, aiop, list);
987 mtx_unlock(&aio_freeproc_mtx);
989 uma_zfree(aiop_zone, aiop);
992 if (mycp->p_vmspace->vm_refcnt <= 1) {
993 printf("AIOD: bad vm refcnt for"
994 " exiting daemon: %d\n",
995 mycp->p_vmspace->vm_refcnt);
1000 mtx_unlock(&aio_freeproc_mtx);
1008 * Create a new AIO daemon. This is mostly a kernel-thread fork routine. The
1009 * AIO daemon modifies its environment itself.
1017 error = kthread_create(aio_daemon, curproc, &p, RFNOWAIT, 0, "aiod%d",
1023 * Wait until daemon is started, but continue on just in case to
1024 * handle error conditions.
1026 error = tsleep(p, PZERO, "aiosta", aiod_timeout);
1034 * Try the high-performance, low-overhead physio method for eligible
1035 * VCHR devices. This method doesn't use an aio helper thread, and
1036 * thus has very low overhead.
1038 * Assumes that the caller, _aio_aqueue(), has incremented the file
1039 * structure's reference count, preventing its deallocation for the
1040 * duration of this call.
1043 aio_qphysio(struct proc *p, struct aiocblist *aiocbe)
1050 struct kaioinfo *ki;
1051 struct aio_liojob *lj;
1055 cb = &aiocbe->uaiocb;
1056 fp = aiocbe->fd_file;
1058 if (fp->f_type != DTYPE_VNODE)
1064 * If its not a disk, we don't want to return a positive error.
1065 * It causes the aio code to not fall through to try the thread
1066 * way when you're talking to a regular file.
1068 if (!vn_isdisk(vp, &error)) {
1069 if (error == ENOTBLK)
1075 if (cb->aio_nbytes % vp->v_bufobj.bo_bsize)
1078 if (cb->aio_nbytes >
1079 MAXPHYS - (((vm_offset_t) cb->aio_buf) & PAGE_MASK))
1083 if (ki->kaio_buffer_count >= ki->kaio_ballowed_count)
1086 ki->kaio_buffer_count++;
1090 lj->lioj_buffer_count++;
1092 /* Create and build a buffer header for a transfer. */
1093 bp = (struct buf *)getpbuf(NULL);
1097 * Get a copy of the kva from the physical buffer.
1101 bp->b_bcount = cb->aio_nbytes;
1102 bp->b_bufsize = cb->aio_nbytes;
1103 bp->b_iodone = aio_physwakeup;
1104 bp->b_saveaddr = bp->b_data;
1105 bp->b_data = (void *)(uintptr_t)cb->aio_buf;
1106 bp->b_offset = cb->aio_offset;
1107 bp->b_iooffset = cb->aio_offset;
1108 bp->b_blkno = btodb(cb->aio_offset);
1109 bp->b_iocmd = cb->aio_lio_opcode == LIO_WRITE ? BIO_WRITE : BIO_READ;
1112 * Bring buffer into kernel space.
1114 if (vmapbuf(bp) < 0) {
1121 bp->b_caller1 = (void *)aiocbe;
1122 TAILQ_INSERT_TAIL(&ki->kaio_bufqueue, aiocbe, plist);
1123 aiocbe->jobstate = JOBST_JOBQBUF;
1124 cb->_aiocb_private.status = cb->aio_nbytes;
1130 /* Perform transfer. */
1131 dev_strategy(vp->v_rdev, bp);
1137 * If we had an error invoking the request, or an error in processing
1138 * the request before we have returned, we process it as an error in
1139 * transfer. Note that such an I/O error is not indicated immediately,
1140 * but is returned using the aio_error mechanism. In this case,
1141 * aio_suspend will return immediately.
1143 if (bp->b_error || (bp->b_ioflags & BIO_ERROR)) {
1144 struct aiocb *job = aiocbe->uuaiocb;
1146 aiocbe->uaiocb._aiocb_private.status = 0;
1147 suword(&job->_aiocb_private.status, 0);
1148 aiocbe->uaiocb._aiocb_private.error = bp->b_error;
1149 suword(&job->_aiocb_private.error, bp->b_error);
1151 ki->kaio_buffer_finished_count++;
1153 if (aiocbe->jobstate != JOBST_JOBBFINISHED) {
1154 aiocbe->jobstate = JOBST_JOBBFINISHED;
1155 aiocbe->jobflags |= AIOCBLIST_DONE;
1156 TAILQ_REMOVE(&ki->kaio_bufqueue, aiocbe, plist);
1157 TAILQ_INSERT_TAIL(&ki->kaio_bufdone, aiocbe, plist);
1163 KNOTE_UNLOCKED(&aiocbe->klist, 0);
1167 ki->kaio_buffer_count--;
1169 lj->lioj_buffer_count--;
1176 * This waits/tests physio completion.
1179 aio_fphysio(struct aiocblist *iocb)
1188 while ((bp->b_flags & B_DONE) == 0) {
1189 if (tsleep(bp, PRIBIO, "physstr", aiod_timeout)) {
1190 if ((bp->b_flags & B_DONE) == 0) {
1192 return (EINPROGRESS);
1199 /* Release mapping into kernel space. */
1205 /* Check for an error. */
1206 if (bp->b_ioflags & BIO_ERROR)
1207 error = bp->b_error;
1214 * Wake up aio requests that may be serviceable now.
1217 aio_swake_cb(struct socket *so, struct sockbuf *sb)
1219 struct aiocblist *cb,*cbn;
1221 struct kaioinfo *ki = NULL;
1222 int opcode, wakecount = 0;
1223 struct aiothreadlist *aiop;
1225 if (sb == &so->so_snd) {
1227 SOCKBUF_LOCK(&so->so_snd);
1228 so->so_snd.sb_flags &= ~SB_AIO;
1229 SOCKBUF_UNLOCK(&so->so_snd);
1232 SOCKBUF_LOCK(&so->so_rcv);
1233 so->so_rcv.sb_flags &= ~SB_AIO;
1234 SOCKBUF_UNLOCK(&so->so_rcv);
1237 for (cb = TAILQ_FIRST(&so->so_aiojobq); cb; cb = cbn) {
1238 cbn = TAILQ_NEXT(cb, list);
1239 if (opcode == cb->uaiocb.aio_lio_opcode) {
1242 TAILQ_REMOVE(&so->so_aiojobq, cb, list);
1243 TAILQ_REMOVE(&ki->kaio_sockqueue, cb, plist);
1244 TAILQ_INSERT_TAIL(&aio_jobs, cb, list);
1245 TAILQ_INSERT_TAIL(&ki->kaio_jobqueue, cb, plist);
1247 if (cb->jobstate != JOBST_JOBQGLOBAL)
1248 panic("invalid queue value");
1252 while (wakecount--) {
1253 mtx_lock(&aio_freeproc_mtx);
1254 if ((aiop = TAILQ_FIRST(&aio_freeproc)) != 0) {
1255 TAILQ_REMOVE(&aio_freeproc, aiop, list);
1256 aiop->aiothreadflags &= ~AIOP_FREE;
1257 wakeup(aiop->aiothread);
1259 mtx_unlock(&aio_freeproc_mtx);
1264 * Queue a new AIO request. Choosing either the threaded or direct physio VCHR
1265 * technique is done in this code.
1268 _aio_aqueue(struct thread *td, struct aiocb *job, struct aio_liojob *lj, int type)
1270 struct proc *p = td->td_proc;
1271 struct filedesc *fdp;
1278 struct aiocblist *aiocbe;
1279 struct aiothreadlist *aiop;
1280 struct kaioinfo *ki;
1286 aiocbe = uma_zalloc(aiocb_zone, M_WAITOK);
1287 aiocbe->inputcharge = 0;
1288 aiocbe->outputcharge = 0;
1289 /* XXX - need a lock */
1290 knlist_init(&aiocbe->klist, NULL, NULL, NULL, NULL);
1292 suword(&job->_aiocb_private.status, -1);
1293 suword(&job->_aiocb_private.error, 0);
1294 suword(&job->_aiocb_private.kernelinfo, -1);
1296 error = copyin(job, &aiocbe->uaiocb, sizeof(aiocbe->uaiocb));
1298 suword(&job->_aiocb_private.error, error);
1299 uma_zfree(aiocb_zone, aiocbe);
1302 if (aiocbe->uaiocb.aio_sigevent.sigev_notify == SIGEV_SIGNAL &&
1303 !_SIG_VALID(aiocbe->uaiocb.aio_sigevent.sigev_signo)) {
1304 uma_zfree(aiocb_zone, aiocbe);
1308 /* Save userspace address of the job info. */
1309 aiocbe->uuaiocb = job;
1311 /* Get the opcode. */
1312 if (type != LIO_NOP)
1313 aiocbe->uaiocb.aio_lio_opcode = type;
1314 opcode = aiocbe->uaiocb.aio_lio_opcode;
1316 /* Get the fd info for process. */
1320 * Range check file descriptor.
1323 fd = aiocbe->uaiocb.aio_fildes;
1324 if (fd >= fdp->fd_nfiles) {
1325 FILEDESC_UNLOCK(fdp);
1326 uma_zfree(aiocb_zone, aiocbe);
1328 suword(&job->_aiocb_private.error, EBADF);
1332 fp = aiocbe->fd_file = fdp->fd_ofiles[fd];
1334 ((opcode == LIO_WRITE) && ((fp->f_flag & FWRITE) == 0)) ||
1335 ((opcode == LIO_READ) && ((fp->f_flag & FREAD) == 0))) {
1336 FILEDESC_UNLOCK(fdp);
1337 uma_zfree(aiocb_zone, aiocbe);
1339 suword(&job->_aiocb_private.error, EBADF);
1343 FILEDESC_UNLOCK(fdp);
1345 if (aiocbe->uaiocb.aio_offset == -1LL) {
1349 error = suword(&job->_aiocb_private.kernelinfo, jobrefid);
1354 aiocbe->uaiocb._aiocb_private.kernelinfo = (void *)(intptr_t)jobrefid;
1355 if (jobrefid == LONG_MAX)
1360 if (opcode == LIO_NOP) {
1362 uma_zfree(aiocb_zone, aiocbe);
1364 suword(&job->_aiocb_private.error, 0);
1365 suword(&job->_aiocb_private.status, 0);
1366 suword(&job->_aiocb_private.kernelinfo, 0);
1370 if ((opcode != LIO_READ) && (opcode != LIO_WRITE)) {
1372 suword(&job->_aiocb_private.status, 0);
1377 if (aiocbe->uaiocb.aio_sigevent.sigev_notify == SIGEV_KEVENT) {
1378 kev.ident = aiocbe->uaiocb.aio_sigevent.sigev_notify_kqueue;
1379 kev.udata = aiocbe->uaiocb.aio_sigevent.sigev_value.sigval_ptr;
1382 if ((u_int)kev.ident >= fdp->fd_nfiles ||
1383 (kq_fp = fdp->fd_ofiles[kev.ident]) == NULL ||
1384 (kq_fp->f_type != DTYPE_KQUEUE)) {
1389 kev.ident = (uintptr_t)aiocbe->uuaiocb;
1390 kev.filter = EVFILT_AIO;
1391 kev.flags = EV_ADD | EV_ENABLE | EV_FLAG1;
1392 kev.data = (intptr_t)aiocbe;
1393 error = kqueue_register(kq, &kev, td, 1);
1397 uma_zfree(aiocb_zone, aiocbe);
1399 suword(&job->_aiocb_private.error, error);
1404 suword(&job->_aiocb_private.error, EINPROGRESS);
1405 aiocbe->uaiocb._aiocb_private.error = EINPROGRESS;
1406 aiocbe->userproc = p;
1407 aiocbe->cred = crhold(td->td_ucred);
1408 aiocbe->jobflags = 0;
1412 if (fp->f_type == DTYPE_SOCKET) {
1414 * Alternate queueing for socket ops: Reach down into the
1415 * descriptor to get the socket data. Then check to see if the
1416 * socket is ready to be read or written (based on the requested
1419 * If it is not ready for io, then queue the aiocbe on the
1420 * socket, and set the flags so we get a call when sbnotify()
1423 * Note if opcode is neither LIO_WRITE nor LIO_READ we lock
1424 * and unlock the snd sockbuf for no reason.
1427 sb = (opcode == LIO_READ) ? &so->so_rcv : &so->so_snd;
1430 if (((opcode == LIO_READ) && (!soreadable(so))) || ((opcode ==
1431 LIO_WRITE) && (!sowriteable(so)))) {
1432 TAILQ_INSERT_TAIL(&so->so_aiojobq, aiocbe, list);
1433 TAILQ_INSERT_TAIL(&ki->kaio_sockqueue, aiocbe, plist);
1434 sb->sb_flags |= SB_AIO;
1435 aiocbe->jobstate = JOBST_JOBQGLOBAL; /* XXX */
1436 ki->kaio_queue_count++;
1447 if ((error = aio_qphysio(p, aiocbe)) == 0)
1450 suword(&job->_aiocb_private.status, 0);
1451 aiocbe->uaiocb._aiocb_private.error = error;
1452 suword(&job->_aiocb_private.error, error);
1456 /* No buffer for daemon I/O. */
1459 ki->kaio_queue_count++;
1461 lj->lioj_queue_count++;
1463 TAILQ_INSERT_TAIL(&ki->kaio_jobqueue, aiocbe, plist);
1464 TAILQ_INSERT_TAIL(&aio_jobs, aiocbe, list);
1466 aiocbe->jobstate = JOBST_JOBQGLOBAL;
1472 * If we don't have a free AIO process, and we are below our quota, then
1473 * start one. Otherwise, depend on the subsequent I/O completions to
1474 * pick-up this job. If we don't sucessfully create the new process
1475 * (thread) due to resource issues, we return an error for now (EAGAIN),
1476 * which is likely not the correct thing to do.
1478 mtx_lock(&aio_freeproc_mtx);
1480 if ((aiop = TAILQ_FIRST(&aio_freeproc)) != NULL) {
1481 TAILQ_REMOVE(&aio_freeproc, 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 mtx_unlock(&aio_freeproc_mtx);
1489 if ((error = aio_newproc()) == 0) {
1490 mtx_lock(&aio_freeproc_mtx);
1491 num_aio_resv_start--;
1494 mtx_lock(&aio_freeproc_mtx);
1495 num_aio_resv_start--;
1497 mtx_unlock(&aio_freeproc_mtx);
1503 * This routine queues an AIO request, checking for quotas.
1506 aio_aqueue(struct thread *td, struct aiocb *job, int type)
1508 struct proc *p = td->td_proc;
1509 struct kaioinfo *ki;
1511 if (p->p_aioinfo == NULL)
1512 aio_init_aioinfo(p);
1514 if (num_queue_count >= max_queue_count)
1518 if (ki->kaio_queue_count >= ki->kaio_qallowed_count)
1521 return _aio_aqueue(td, job, NULL, type);
1525 * Support the aio_return system call, as a side-effect, kernel resources are
1529 aio_return(struct thread *td, struct aio_return_args *uap)
1531 struct proc *p = td->td_proc;
1534 struct aiocblist *cb, *ncb;
1536 struct kaioinfo *ki;
1539 jobref = fuword(&ujob->_aiocb_private.kernelinfo);
1540 if (jobref == -1 || jobref == 0)
1547 TAILQ_FOREACH(cb, &ki->kaio_jobdone, plist) {
1548 if (((intptr_t) cb->uaiocb._aiocb_private.kernelinfo) ==
1550 if (cb->uaiocb.aio_lio_opcode == LIO_WRITE) {
1551 p->p_stats->p_ru.ru_oublock +=
1553 cb->outputcharge = 0;
1554 } else if (cb->uaiocb.aio_lio_opcode == LIO_READ) {
1555 p->p_stats->p_ru.ru_inblock += cb->inputcharge;
1556 cb->inputcharge = 0;
1562 for (cb = TAILQ_FIRST(&ki->kaio_bufdone); cb; cb = ncb) {
1563 ncb = TAILQ_NEXT(cb, plist);
1564 if (((intptr_t) cb->uaiocb._aiocb_private.kernelinfo)
1573 if (ujob == cb->uuaiocb) {
1575 cb->uaiocb._aiocb_private.status;
1577 td->td_retval[0] = EFAULT;
1585 * Allow a process to wakeup when any of the I/O requests are completed.
1588 aio_suspend(struct thread *td, struct aio_suspend_args *uap)
1590 struct proc *p = td->td_proc;
1593 struct aiocb *const *cbptr, *cbp;
1594 struct kaioinfo *ki;
1595 struct aiocblist *cb;
1600 struct aiocb **ujoblist;
1602 if (uap->nent < 0 || uap->nent > AIO_LISTIO_MAX)
1607 /* Get timespec struct. */
1608 if ((error = copyin(uap->timeout, &ts, sizeof(ts))) != 0)
1611 if (ts.tv_nsec < 0 || ts.tv_nsec >= 1000000000)
1614 TIMESPEC_TO_TIMEVAL(&atv, &ts);
1615 if (itimerfix(&atv))
1617 timo = tvtohz(&atv);
1625 ijoblist = uma_zalloc(aiol_zone, M_WAITOK);
1626 ujoblist = uma_zalloc(aiol_zone, M_WAITOK);
1627 cbptr = uap->aiocbp;
1629 for (i = 0; i < uap->nent; i++) {
1630 cbp = (struct aiocb *)(intptr_t)fuword(&cbptr[i]);
1633 ujoblist[njoblist] = cbp;
1634 ijoblist[njoblist] = fuword(&cbp->_aiocb_private.kernelinfo);
1638 if (njoblist == 0) {
1639 uma_zfree(aiol_zone, ijoblist);
1640 uma_zfree(aiol_zone, ujoblist);
1647 TAILQ_FOREACH(cb, &ki->kaio_jobdone, plist) {
1648 for (i = 0; i < njoblist; i++) {
1650 cb->uaiocb._aiocb_private.kernelinfo) ==
1653 if (ujoblist[i] != cb->uuaiocb)
1655 uma_zfree(aiol_zone, ijoblist);
1656 uma_zfree(aiol_zone, ujoblist);
1663 for (cb = TAILQ_FIRST(&ki->kaio_bufdone); cb; cb =
1664 TAILQ_NEXT(cb, plist)) {
1665 for (i = 0; i < njoblist; i++) {
1667 cb->uaiocb._aiocb_private.kernelinfo) ==
1671 if (ujoblist[i] != cb->uuaiocb)
1673 uma_zfree(aiol_zone, ijoblist);
1674 uma_zfree(aiol_zone, ujoblist);
1680 ki->kaio_flags |= KAIO_WAKEUP;
1681 error = msleep(p, &p->p_mtx, PDROP | PRIBIO | PCATCH, "aiospn",
1685 if (error == ERESTART || error == EINTR) {
1686 uma_zfree(aiol_zone, ijoblist);
1687 uma_zfree(aiol_zone, ujoblist);
1689 } else if (error == EWOULDBLOCK) {
1690 uma_zfree(aiol_zone, ijoblist);
1691 uma_zfree(aiol_zone, ujoblist);
1701 * aio_cancel cancels any non-physio aio operations not currently in
1705 aio_cancel(struct thread *td, struct aio_cancel_args *uap)
1707 struct proc *p = td->td_proc;
1708 struct kaioinfo *ki;
1709 struct aiocblist *cbe, *cbn;
1711 struct filedesc *fdp;
1720 if ((u_int)uap->fd >= fdp->fd_nfiles ||
1721 (fp = fdp->fd_ofiles[uap->fd]) == NULL)
1724 if (fp->f_type == DTYPE_VNODE) {
1727 if (vn_isdisk(vp,&error)) {
1728 td->td_retval[0] = AIO_NOTCANCELED;
1731 } else if (fp->f_type == DTYPE_SOCKET) {
1736 for (cbe = TAILQ_FIRST(&so->so_aiojobq); cbe; cbe = cbn) {
1737 cbn = TAILQ_NEXT(cbe, list);
1738 if ((uap->aiocbp == NULL) ||
1739 (uap->aiocbp == cbe->uuaiocb) ) {
1742 TAILQ_REMOVE(&so->so_aiojobq, cbe, list);
1743 TAILQ_REMOVE(&ki->kaio_sockqueue, cbe, plist);
1744 TAILQ_INSERT_TAIL(&ki->kaio_jobdone, cbe, plist);
1745 if (ki->kaio_flags & KAIO_WAKEUP) {
1748 cbe->jobstate = JOBST_JOBFINISHED;
1749 cbe->uaiocb._aiocb_private.status=-1;
1750 cbe->uaiocb._aiocb_private.error=ECANCELED;
1752 /* XXX cancelled, knote? */
1753 if (cbe->uaiocb.aio_sigevent.sigev_notify ==
1755 PROC_LOCK(cbe->userproc);
1756 psignal(cbe->userproc, cbe->uaiocb.aio_sigevent.sigev_signo);
1757 PROC_UNLOCK(cbe->userproc);
1765 if ((cancelled) && (uap->aiocbp)) {
1766 td->td_retval[0] = AIO_CANCELED;
1775 for (cbe = TAILQ_FIRST(&ki->kaio_jobqueue); cbe; cbe = cbn) {
1776 cbn = TAILQ_NEXT(cbe, plist);
1778 if ((uap->fd == cbe->uaiocb.aio_fildes) &&
1779 ((uap->aiocbp == NULL ) ||
1780 (uap->aiocbp == cbe->uuaiocb))) {
1782 if (cbe->jobstate == JOBST_JOBQGLOBAL) {
1783 TAILQ_REMOVE(&aio_jobs, cbe, list);
1784 TAILQ_REMOVE(&ki->kaio_jobqueue, cbe, plist);
1785 TAILQ_INSERT_TAIL(&ki->kaio_jobdone, cbe,
1788 ki->kaio_queue_finished_count++;
1789 cbe->jobstate = JOBST_JOBFINISHED;
1790 cbe->uaiocb._aiocb_private.status = -1;
1791 cbe->uaiocb._aiocb_private.error = ECANCELED;
1792 /* XXX cancelled, knote? */
1793 if (cbe->uaiocb.aio_sigevent.sigev_notify ==
1795 PROC_LOCK(cbe->userproc);
1796 psignal(cbe->userproc, cbe->uaiocb.aio_sigevent.sigev_signo);
1797 PROC_UNLOCK(cbe->userproc);
1807 td->td_retval[0] = AIO_NOTCANCELED;
1811 td->td_retval[0] = AIO_CANCELED;
1814 td->td_retval[0] = AIO_ALLDONE;
1820 * aio_error is implemented in the kernel level for compatibility purposes only.
1821 * For a user mode async implementation, it would be best to do it in a userland
1825 aio_error(struct thread *td, struct aio_error_args *uap)
1827 struct proc *p = td->td_proc;
1829 struct aiocblist *cb;
1830 struct kaioinfo *ki;
1837 jobref = fuword(&uap->aiocbp->_aiocb_private.kernelinfo);
1838 if ((jobref == -1) || (jobref == 0))
1842 TAILQ_FOREACH(cb, &ki->kaio_jobdone, plist) {
1843 if (((intptr_t)cb->uaiocb._aiocb_private.kernelinfo) ==
1846 td->td_retval[0] = cb->uaiocb._aiocb_private.error;
1853 for (cb = TAILQ_FIRST(&ki->kaio_jobqueue); cb; cb = TAILQ_NEXT(cb,
1855 if (((intptr_t)cb->uaiocb._aiocb_private.kernelinfo) ==
1858 td->td_retval[0] = EINPROGRESS;
1864 for (cb = TAILQ_FIRST(&ki->kaio_sockqueue); cb; cb = TAILQ_NEXT(cb,
1866 if (((intptr_t)cb->uaiocb._aiocb_private.kernelinfo) ==
1869 td->td_retval[0] = EINPROGRESS;
1877 for (cb = TAILQ_FIRST(&ki->kaio_bufdone); cb; cb = TAILQ_NEXT(cb,
1879 if (((intptr_t)cb->uaiocb._aiocb_private.kernelinfo) ==
1882 td->td_retval[0] = cb->uaiocb._aiocb_private.error;
1888 for (cb = TAILQ_FIRST(&ki->kaio_bufqueue); cb; cb = TAILQ_NEXT(cb,
1890 if (((intptr_t)cb->uaiocb._aiocb_private.kernelinfo) ==
1893 td->td_retval[0] = EINPROGRESS;
1905 status = fuword(&uap->aiocbp->_aiocb_private.status);
1907 return fuword(&uap->aiocbp->_aiocb_private.error);
1912 /* syscall - asynchronous read from a file (REALTIME) */
1914 aio_read(struct thread *td, struct aio_read_args *uap)
1917 return aio_aqueue(td, uap->aiocbp, LIO_READ);
1920 /* syscall - asynchronous write to a file (REALTIME) */
1922 aio_write(struct thread *td, struct aio_write_args *uap)
1925 return aio_aqueue(td, uap->aiocbp, LIO_WRITE);
1928 /* syscall - list directed I/O (REALTIME) */
1930 lio_listio(struct thread *td, struct lio_listio_args *uap)
1932 struct proc *p = td->td_proc;
1933 int nent, nentqueued;
1934 struct aiocb *iocb, * const *cbptr;
1935 struct aiocblist *cb;
1936 struct kaioinfo *ki;
1937 struct aio_liojob *lj;
1938 int error, runningcode;
1943 if ((uap->mode != LIO_NOWAIT) && (uap->mode != LIO_WAIT))
1947 if (nent < 0 || nent > AIO_LISTIO_MAX)
1950 if (p->p_aioinfo == NULL)
1951 aio_init_aioinfo(p);
1953 if ((nent + num_queue_count) > max_queue_count)
1957 if ((nent + ki->kaio_queue_count) > ki->kaio_qallowed_count)
1960 lj = uma_zalloc(aiolio_zone, M_WAITOK);
1965 lj->lioj_buffer_count = 0;
1966 lj->lioj_buffer_finished_count = 0;
1967 lj->lioj_queue_count = 0;
1968 lj->lioj_queue_finished_count = 0;
1973 if (uap->sig && (uap->mode == LIO_NOWAIT)) {
1974 error = copyin(uap->sig, &lj->lioj_signal,
1975 sizeof(lj->lioj_signal));
1977 uma_zfree(aiolio_zone, lj);
1980 if (!_SIG_VALID(lj->lioj_signal.sigev_signo)) {
1981 uma_zfree(aiolio_zone, lj);
1984 lj->lioj_flags |= LIOJ_SIGNAL;
1986 TAILQ_INSERT_TAIL(&ki->kaio_liojoblist, lj, lioj_list);
1988 * Get pointers to the list of I/O requests.
1992 cbptr = uap->acb_list;
1993 for (i = 0; i < uap->nent; i++) {
1994 iocb = (struct aiocb *)(intptr_t)fuword(&cbptr[i]);
1995 if (((intptr_t)iocb != -1) && ((intptr_t)iocb != 0)) {
1996 error = _aio_aqueue(td, iocb, lj, 0);
2005 * If we haven't queued any, then just return error.
2007 if (nentqueued == 0)
2011 * Calculate the appropriate error return.
2017 if (uap->mode == LIO_WAIT) {
2023 for (i = 0; i < uap->nent; i++) {
2025 * Fetch address of the control buf pointer in
2028 iocb = (struct aiocb *)
2029 (intptr_t)fuword(&cbptr[i]);
2030 if (((intptr_t)iocb == -1) || ((intptr_t)iocb
2035 * Fetch the associated command from user space.
2037 command = fuword(&iocb->aio_lio_opcode);
2038 if (command == LIO_NOP) {
2044 fuword(&iocb->_aiocb_private.kernelinfo);
2046 TAILQ_FOREACH(cb, &ki->kaio_jobdone, plist) {
2047 if (((intptr_t)cb->uaiocb._aiocb_private.kernelinfo)
2049 if (cb->uaiocb.aio_lio_opcode
2051 p->p_stats->p_ru.ru_oublock
2054 cb->outputcharge = 0;
2055 } else if (cb->uaiocb.aio_lio_opcode
2057 p->p_stats->p_ru.ru_inblock
2059 cb->inputcharge = 0;
2067 TAILQ_FOREACH(cb, &ki->kaio_bufdone, plist) {
2068 if (((intptr_t)cb->uaiocb._aiocb_private.kernelinfo)
2078 * If all I/Os have been disposed of, then we can
2081 if (found == nentqueued)
2082 return (runningcode);
2084 ki->kaio_flags |= KAIO_WAKEUP;
2085 error = tsleep(p, PRIBIO | PCATCH, "aiospn", 0);
2089 else if (error == EWOULDBLOCK)
2094 return (runningcode);
2098 * Interrupt handler for physio, performs the necessary process wakeups, and
2102 aio_physwakeup(struct buf *bp)
2104 struct aiocblist *aiocbe;
2106 struct kaioinfo *ki;
2107 struct aio_liojob *lj;
2112 aiocbe = (struct aiocblist *)bp->b_caller1;
2114 p = aiocbe->userproc;
2116 aiocbe->jobstate = JOBST_JOBBFINISHED;
2117 aiocbe->uaiocb._aiocb_private.status -= bp->b_resid;
2118 aiocbe->uaiocb._aiocb_private.error = 0;
2119 aiocbe->jobflags |= AIOCBLIST_DONE;
2121 if (bp->b_ioflags & BIO_ERROR)
2122 aiocbe->uaiocb._aiocb_private.error = bp->b_error;
2126 lj->lioj_buffer_finished_count++;
2129 * wakeup/signal if all of the interrupt jobs are done.
2131 if (lj->lioj_buffer_finished_count ==
2132 lj->lioj_buffer_count &&
2133 lj->lioj_queue_finished_count ==
2134 lj->lioj_queue_count) {
2136 * Post a signal if it is called for.
2138 if ((lj->lioj_flags &
2139 (LIOJ_SIGNAL|LIOJ_SIGNAL_POSTED)) ==
2142 psignal(p, lj->lioj_signal.sigev_signo);
2144 lj->lioj_flags |= LIOJ_SIGNAL_POSTED;
2151 ki->kaio_buffer_finished_count++;
2152 TAILQ_REMOVE(&ki->kaio_bufqueue, aiocbe, plist);
2153 TAILQ_INSERT_TAIL(&ki->kaio_bufdone, aiocbe, plist);
2155 KNOTE_UNLOCKED(&aiocbe->klist, 0);
2156 /* Do the wakeup. */
2157 if (ki->kaio_flags & (KAIO_RUNDOWN|KAIO_WAKEUP)) {
2158 ki->kaio_flags &= ~KAIO_WAKEUP;
2163 if (aiocbe->uaiocb.aio_sigevent.sigev_notify == SIGEV_SIGNAL) {
2165 psignal(p, aiocbe->uaiocb.aio_sigevent.sigev_signo);
2172 /* syscall - wait for the next completion of an aio request */
2174 aio_waitcomplete(struct thread *td, struct aio_waitcomplete_args *uap)
2176 struct proc *p = td->td_proc;
2179 struct kaioinfo *ki;
2180 struct aiocblist *cb = NULL;
2183 suword(uap->aiocbp, (int)NULL);
2187 /* Get timespec struct. */
2188 error = copyin(uap->timeout, &ts, sizeof(ts));
2192 if ((ts.tv_nsec < 0) || (ts.tv_nsec >= 1000000000))
2195 TIMESPEC_TO_TIMEVAL(&atv, &ts);
2196 if (itimerfix(&atv))
2198 timo = tvtohz(&atv);
2207 if ((cb = TAILQ_FIRST(&ki->kaio_jobdone)) != 0) {
2209 suword(uap->aiocbp, (uintptr_t)cb->uuaiocb);
2210 td->td_retval[0] = cb->uaiocb._aiocb_private.status;
2211 if (cb->uaiocb.aio_lio_opcode == LIO_WRITE) {
2212 p->p_stats->p_ru.ru_oublock +=
2214 cb->outputcharge = 0;
2215 } else if (cb->uaiocb.aio_lio_opcode == LIO_READ) {
2216 p->p_stats->p_ru.ru_inblock += cb->inputcharge;
2217 cb->inputcharge = 0;
2219 error = cb->uaiocb._aiocb_private.error;
2225 if ((cb = TAILQ_FIRST(&ki->kaio_bufdone)) != 0 ) {
2228 suword(uap->aiocbp, (uintptr_t)cb->uuaiocb);
2229 error = cb->uaiocb._aiocb_private.error;
2230 td->td_retval[0] = cb->uaiocb._aiocb_private.status;
2235 ki->kaio_flags |= KAIO_WAKEUP;
2236 error = msleep(p, &p->p_mtx, PDROP | PRIBIO | PCATCH, "aiowc",
2240 if (error == ERESTART)
2244 else if (error == EINTR)
2246 else if (error == EWOULDBLOCK)
2251 /* kqueue attach function */
2253 filt_aioattach(struct knote *kn)
2255 struct aiocblist *aiocbe = (struct aiocblist *)kn->kn_sdata;
2258 * The aiocbe pointer must be validated before using it, so
2259 * registration is restricted to the kernel; the user cannot
2262 if ((kn->kn_flags & EV_FLAG1) == 0)
2264 kn->kn_flags &= ~EV_FLAG1;
2266 knlist_add(&aiocbe->klist, kn, 0);
2271 /* kqueue detach function */
2273 filt_aiodetach(struct knote *kn)
2275 struct aiocblist *aiocbe = (struct aiocblist *)kn->kn_sdata;
2277 knlist_remove(&aiocbe->klist, kn, 0);
2280 /* kqueue filter function */
2283 filt_aio(struct knote *kn, long hint)
2285 struct aiocblist *aiocbe = (struct aiocblist *)kn->kn_sdata;
2287 kn->kn_data = aiocbe->uaiocb._aiocb_private.error;
2288 if (aiocbe->jobstate != JOBST_JOBFINISHED &&
2289 aiocbe->jobstate != JOBST_JOBBFINISHED)
2291 kn->kn_flags |= EV_EOF;