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>
46 #include <sys/socket.h>
47 #include <sys/socketvar.h>
48 #include <sys/syscall.h>
49 #include <sys/sysent.h>
50 #include <sys/sysctl.h>
52 #include <sys/taskqueue.h>
53 #include <sys/vnode.h>
55 #include <sys/event.h>
57 #include <machine/atomic.h>
59 #include <posix4/posix4.h>
61 #include <vm/vm_extern.h>
63 #include <vm/vm_map.h>
67 #include "opt_vfs_aio.h"
70 * Counter for allocating reference ids to new jobs. Wrapped to 1 on
75 #define JOBST_NULL 0x0
76 #define JOBST_JOBQSOCK 0x1
77 #define JOBST_JOBQGLOBAL 0x2
78 #define JOBST_JOBRUNNING 0x3
79 #define JOBST_JOBFINISHED 0x4
80 #define JOBST_JOBQBUF 0x5
82 #ifndef MAX_AIO_PER_PROC
83 #define MAX_AIO_PER_PROC 32
86 #ifndef MAX_AIO_QUEUE_PER_PROC
87 #define MAX_AIO_QUEUE_PER_PROC 256 /* Bigger than AIO_LISTIO_MAX */
91 #define MAX_AIO_PROCS 32
95 #define MAX_AIO_QUEUE 1024 /* Bigger than AIO_LISTIO_MAX */
98 #ifndef TARGET_AIO_PROCS
99 #define TARGET_AIO_PROCS 4
103 #define MAX_BUF_AIO 16
106 #ifndef AIOD_TIMEOUT_DEFAULT
107 #define AIOD_TIMEOUT_DEFAULT (10 * hz)
110 #ifndef AIOD_LIFETIME_DEFAULT
111 #define AIOD_LIFETIME_DEFAULT (30 * hz)
114 static SYSCTL_NODE(_vfs, OID_AUTO, aio, CTLFLAG_RW, 0, "Async IO management");
116 static int max_aio_procs = MAX_AIO_PROCS;
117 SYSCTL_INT(_vfs_aio, OID_AUTO, max_aio_procs,
118 CTLFLAG_RW, &max_aio_procs, 0,
119 "Maximum number of kernel threads to use for handling async IO ");
121 static int num_aio_procs = 0;
122 SYSCTL_INT(_vfs_aio, OID_AUTO, num_aio_procs,
123 CTLFLAG_RD, &num_aio_procs, 0,
124 "Number of presently active kernel threads for async IO");
127 * The code will adjust the actual number of AIO processes towards this
128 * number when it gets a chance.
130 static int target_aio_procs = TARGET_AIO_PROCS;
131 SYSCTL_INT(_vfs_aio, OID_AUTO, target_aio_procs, CTLFLAG_RW, &target_aio_procs,
132 0, "Preferred number of ready kernel threads for async IO");
134 static int max_queue_count = MAX_AIO_QUEUE;
135 SYSCTL_INT(_vfs_aio, OID_AUTO, max_aio_queue, CTLFLAG_RW, &max_queue_count, 0,
136 "Maximum number of aio requests to queue, globally");
138 static int num_queue_count = 0;
139 SYSCTL_INT(_vfs_aio, OID_AUTO, num_queue_count, CTLFLAG_RD, &num_queue_count, 0,
140 "Number of queued aio requests");
142 static int num_buf_aio = 0;
143 SYSCTL_INT(_vfs_aio, OID_AUTO, num_buf_aio, CTLFLAG_RD, &num_buf_aio, 0,
144 "Number of aio requests presently handled by the buf subsystem");
146 /* Number of async I/O thread in the process of being started */
147 /* XXX This should be local to aio_aqueue() */
148 static int num_aio_resv_start = 0;
150 static int aiod_timeout;
151 SYSCTL_INT(_vfs_aio, OID_AUTO, aiod_timeout, CTLFLAG_RW, &aiod_timeout, 0,
152 "Timeout value for synchronous aio operations");
154 static int aiod_lifetime;
155 SYSCTL_INT(_vfs_aio, OID_AUTO, aiod_lifetime, CTLFLAG_RW, &aiod_lifetime, 0,
156 "Maximum lifetime for idle aiod");
158 static int unloadable = 0;
159 SYSCTL_INT(_vfs_aio, OID_AUTO, unloadable, CTLFLAG_RW, &unloadable, 0,
160 "Allow unload of aio (not recommended)");
163 static int max_aio_per_proc = MAX_AIO_PER_PROC;
164 SYSCTL_INT(_vfs_aio, OID_AUTO, max_aio_per_proc, CTLFLAG_RW, &max_aio_per_proc,
165 0, "Maximum active aio requests per process (stored in the process)");
167 static int max_aio_queue_per_proc = MAX_AIO_QUEUE_PER_PROC;
168 SYSCTL_INT(_vfs_aio, OID_AUTO, max_aio_queue_per_proc, CTLFLAG_RW,
169 &max_aio_queue_per_proc, 0,
170 "Maximum queued aio requests per process (stored in the process)");
172 static int max_buf_aio = MAX_BUF_AIO;
173 SYSCTL_INT(_vfs_aio, OID_AUTO, max_buf_aio, CTLFLAG_RW, &max_buf_aio, 0,
174 "Maximum buf aio requests per process (stored in the process)");
176 typedef struct oaiocb {
177 int aio_fildes; /* File descriptor */
178 off_t aio_offset; /* File offset for I/O */
179 volatile void *aio_buf; /* I/O buffer in process space */
180 size_t aio_nbytes; /* Number of bytes for I/O */
181 struct osigevent aio_sigevent; /* Signal to deliver */
182 int aio_lio_opcode; /* LIO opcode */
183 int aio_reqprio; /* Request priority -- ignored */
184 struct __aiocb_private _aiocb_private;
188 * Below is a key of locks used to protect each member of struct aiocblist
189 * aioliojob and kaioinfo and any backends.
191 * * - need not protected
192 * a - locked by proc mtx
193 * b - locked by backend lock, the backend lock can be null in some cases,
194 * for example, BIO belongs to this type, in this case, proc lock is
196 * c - locked by aio_job_mtx, the lock for the generic file I/O backend.
200 * Current, there is only two backends: BIO and generic file I/O.
201 * socket I/O is served by generic file I/O, this is not a good idea, since
202 * disk file I/O and any other types without O_NONBLOCK flag can block daemon
203 * threads, if there is no thread to serve socket I/O, the socket I/O will be
204 * delayed too long or starved, we should create some threads dedicated to
205 * sockets to do non-blocking I/O, same for pipe and fifo, for these I/O
206 * systems we really need non-blocking interface, fiddling O_NONBLOCK in file
207 * structure is not safe because there is race between userland and aio
212 TAILQ_ENTRY(aiocblist) list; /* (b) internal list of for backend */
213 TAILQ_ENTRY(aiocblist) plist; /* (a) list of jobs for each backend */
214 TAILQ_ENTRY(aiocblist) allist; /* (a) list of all jobs in proc */
215 int jobflags; /* (a) job flags */
216 int jobstate; /* (b) job state */
217 int inputcharge; /* (*) input blockes */
218 int outputcharge; /* (*) output blockes */
219 struct buf *bp; /* (*) private to BIO backend,
222 struct proc *userproc; /* (*) user process */
223 struct ucred *cred; /* (*) active credential when created */
224 struct file *fd_file; /* (*) pointer to file structure */
225 struct aioliojob *lio; /* (*) optional lio job */
226 struct aiocb *uuaiocb; /* (*) pointer in userspace of aiocb */
227 struct knlist klist; /* (a) list of knotes */
228 struct aiocb uaiocb; /* (*) kernel I/O control block */
229 ksiginfo_t ksi; /* (a) realtime signal info */
230 struct task biotask; /* (*) private to BIO backend */
234 #define AIOCBLIST_RUNDOWN 0x04
235 #define AIOCBLIST_DONE 0x10
236 #define AIOCBLIST_BUFDONE 0x20
241 #define AIOP_FREE 0x1 /* proc on free queue */
243 struct aiothreadlist {
244 int aiothreadflags; /* (c) AIO proc flags */
245 TAILQ_ENTRY(aiothreadlist) list; /* (c) list of processes */
246 struct thread *aiothread; /* (*) the AIO thread */
250 * data-structure for lio signal management
253 int lioj_flags; /* (a) listio flags */
254 int lioj_count; /* (a) listio flags */
255 int lioj_finished_count; /* (a) listio flags */
256 struct sigevent lioj_signal; /* (a) signal on all I/O done */
257 TAILQ_ENTRY(aioliojob) lioj_list; /* (a) lio list */
258 struct knlist klist; /* (a) list of knotes */
259 ksiginfo_t lioj_ksi; /* (a) Realtime signal info */
262 #define LIOJ_SIGNAL 0x1 /* signal on all done (lio) */
263 #define LIOJ_SIGNAL_POSTED 0x2 /* signal has been posted */
264 #define LIOJ_KEVENT_POSTED 0x4 /* kevent triggered */
267 * per process aio data structure
270 int kaio_flags; /* (a) per process kaio flags */
271 int kaio_maxactive_count; /* (*) maximum number of AIOs */
272 int kaio_active_count; /* (c) number of currently used AIOs */
273 int kaio_qallowed_count; /* (*) maxiumu size of AIO queue */
274 int kaio_count; /* (a) size of AIO queue */
275 int kaio_ballowed_count; /* (*) maximum number of buffers */
276 int kaio_buffer_count; /* (a) number of physio buffers */
277 TAILQ_HEAD(,aiocblist) kaio_all; /* (a) all AIOs in the process */
278 TAILQ_HEAD(,aiocblist) kaio_done; /* (a) done queue for process */
279 TAILQ_HEAD(,aioliojob) kaio_liojoblist; /* (a) list of lio jobs */
280 TAILQ_HEAD(,aiocblist) kaio_jobqueue; /* (a) job queue for process */
281 TAILQ_HEAD(,aiocblist) kaio_bufqueue; /* (a) buffer job queue for process */
282 TAILQ_HEAD(,aiocblist) kaio_sockqueue; /* (a) queue for aios waiting on sockets,
287 #define KAIO_RUNDOWN 0x1 /* process is being run down */
288 #define KAIO_WAKEUP 0x2 /* wakeup process when there is a significant event */
290 static TAILQ_HEAD(,aiothreadlist) aio_freeproc; /* (c) Idle daemons */
291 static struct sema aio_newproc_sem;
292 static struct mtx aio_job_mtx;
293 static struct mtx aio_sock_mtx;
294 static TAILQ_HEAD(,aiocblist) aio_jobs; /* (c) Async job list */
295 static struct unrhdr *aiod_unr;
297 static void aio_init_aioinfo(struct proc *p);
298 static void aio_onceonly(void);
299 static int aio_free_entry(struct aiocblist *aiocbe);
300 static void aio_process(struct aiocblist *aiocbe);
301 static int aio_newproc(int *);
302 static int aio_aqueue(struct thread *td, struct aiocb *job,
303 struct aioliojob *lio, int type, int osigev);
304 static void aio_physwakeup(struct buf *bp);
305 static void aio_proc_rundown(void *arg, struct proc *p);
306 static int aio_qphysio(struct proc *p, struct aiocblist *iocb);
307 static void biohelper(void *, int);
308 static void aio_daemon(void *param);
309 static void aio_swake_cb(struct socket *, struct sockbuf *);
310 static int aio_unload(void);
311 static int filt_aioattach(struct knote *kn);
312 static void filt_aiodetach(struct knote *kn);
313 static int filt_aio(struct knote *kn, long hint);
314 static int filt_lioattach(struct knote *kn);
315 static void filt_liodetach(struct knote *kn);
316 static int filt_lio(struct knote *kn, long hint);
319 static void aio_bio_done_notify( struct proc *userp, struct aiocblist *aiocbe, int type);
320 static int do_lio_listio(struct thread *td, struct lio_listio_args *uap,
325 * kaio Per process async io info
326 * aiop async io thread data
327 * aiocb async io jobs
328 * aiol list io job pointer - internal to aio_suspend XXX
329 * aiolio list io jobs
331 static uma_zone_t kaio_zone, aiop_zone, aiocb_zone, aiol_zone, aiolio_zone;
333 /* kqueue filters for aio */
334 static struct filterops aio_filtops =
335 { 0, filt_aioattach, filt_aiodetach, filt_aio };
336 static struct filterops lio_filtops =
337 { 0, filt_lioattach, filt_liodetach, filt_lio };
339 static eventhandler_tag exit_tag, exec_tag;
341 TASKQUEUE_DEFINE_THREAD(aiod_bio);
344 * Main operations function for use as a kernel module.
347 aio_modload(struct module *module, int cmd, void *arg)
356 error = aio_unload();
367 static moduledata_t aio_mod = {
373 SYSCALL_MODULE_HELPER(aio_return);
374 SYSCALL_MODULE_HELPER(aio_suspend);
375 SYSCALL_MODULE_HELPER(aio_cancel);
376 SYSCALL_MODULE_HELPER(aio_error);
377 SYSCALL_MODULE_HELPER(aio_read);
378 SYSCALL_MODULE_HELPER(aio_write);
379 SYSCALL_MODULE_HELPER(aio_waitcomplete);
380 SYSCALL_MODULE_HELPER(lio_listio);
381 SYSCALL_MODULE_HELPER(oaio_read);
382 SYSCALL_MODULE_HELPER(oaio_write);
383 SYSCALL_MODULE_HELPER(olio_listio);
385 DECLARE_MODULE(aio, aio_mod,
386 SI_SUB_VFS, SI_ORDER_ANY);
387 MODULE_VERSION(aio, 1);
390 * Startup initialization
396 /* XXX: should probably just use so->callback */
397 aio_swake = &aio_swake_cb;
398 exit_tag = EVENTHANDLER_REGISTER(process_exit, aio_proc_rundown, NULL,
399 EVENTHANDLER_PRI_ANY);
400 exec_tag = EVENTHANDLER_REGISTER(process_exec, aio_proc_rundown, NULL,
401 EVENTHANDLER_PRI_ANY);
402 kqueue_add_filteropts(EVFILT_AIO, &aio_filtops);
403 kqueue_add_filteropts(EVFILT_LIO, &lio_filtops);
404 TAILQ_INIT(&aio_freeproc);
405 sema_init(&aio_newproc_sem, 0, "aio_new_proc");
406 mtx_init(&aio_job_mtx, "aio_job", NULL, MTX_DEF);
407 mtx_init(&aio_sock_mtx, "aio_sock", NULL, MTX_DEF);
408 TAILQ_INIT(&aio_jobs);
409 aiod_unr = new_unrhdr(1, INT_MAX, NULL);
410 kaio_zone = uma_zcreate("AIO", sizeof(struct kaioinfo), NULL, NULL,
411 NULL, NULL, UMA_ALIGN_PTR, UMA_ZONE_NOFREE);
412 aiop_zone = uma_zcreate("AIOP", sizeof(struct aiothreadlist), NULL,
413 NULL, NULL, NULL, UMA_ALIGN_PTR, UMA_ZONE_NOFREE);
414 aiocb_zone = uma_zcreate("AIOCB", sizeof(struct aiocblist), NULL, NULL,
415 NULL, NULL, UMA_ALIGN_PTR, UMA_ZONE_NOFREE);
416 aiol_zone = uma_zcreate("AIOL", AIO_LISTIO_MAX*sizeof(intptr_t) , NULL,
417 NULL, NULL, NULL, UMA_ALIGN_PTR, UMA_ZONE_NOFREE);
418 aiolio_zone = uma_zcreate("AIOLIO", sizeof(struct aioliojob), NULL,
419 NULL, NULL, NULL, UMA_ALIGN_PTR, UMA_ZONE_NOFREE);
420 aiod_timeout = AIOD_TIMEOUT_DEFAULT;
421 aiod_lifetime = AIOD_LIFETIME_DEFAULT;
423 async_io_version = _POSIX_VERSION;
424 p31b_setcfg(CTL_P1003_1B_AIO_LISTIO_MAX, AIO_LISTIO_MAX);
425 p31b_setcfg(CTL_P1003_1B_AIO_MAX, MAX_AIO_QUEUE);
426 p31b_setcfg(CTL_P1003_1B_AIO_PRIO_DELTA_MAX, 0);
430 * Callback for unload of AIO when used as a module.
438 * XXX: no unloads by default, it's too dangerous.
439 * perhaps we could do it if locked out callers and then
440 * did an aio_proc_rundown() on each process.
442 * jhb: aio_proc_rundown() needs to run on curproc though,
443 * so I don't think that would fly.
448 error = kqueue_del_filteropts(EVFILT_AIO);
451 error = kqueue_del_filteropts(EVFILT_LIO);
454 async_io_version = 0;
456 taskqueue_free(taskqueue_aiod_bio);
457 delete_unrhdr(aiod_unr);
458 uma_zdestroy(kaio_zone);
459 uma_zdestroy(aiop_zone);
460 uma_zdestroy(aiocb_zone);
461 uma_zdestroy(aiol_zone);
462 uma_zdestroy(aiolio_zone);
463 EVENTHANDLER_DEREGISTER(process_exit, exit_tag);
464 EVENTHANDLER_DEREGISTER(process_exec, exec_tag);
465 mtx_destroy(&aio_job_mtx);
466 mtx_destroy(&aio_sock_mtx);
467 sema_destroy(&aio_newproc_sem);
468 p31b_setcfg(CTL_P1003_1B_AIO_LISTIO_MAX, -1);
469 p31b_setcfg(CTL_P1003_1B_AIO_MAX, -1);
470 p31b_setcfg(CTL_P1003_1B_AIO_PRIO_DELTA_MAX, -1);
475 * Init the per-process aioinfo structure. The aioinfo limits are set
476 * per-process for user limit (resource) management.
479 aio_init_aioinfo(struct proc *p)
483 ki = uma_zalloc(kaio_zone, M_WAITOK);
485 ki->kaio_maxactive_count = max_aio_per_proc;
486 ki->kaio_active_count = 0;
487 ki->kaio_qallowed_count = max_aio_queue_per_proc;
489 ki->kaio_ballowed_count = max_buf_aio;
490 ki->kaio_buffer_count = 0;
491 TAILQ_INIT(&ki->kaio_all);
492 TAILQ_INIT(&ki->kaio_done);
493 TAILQ_INIT(&ki->kaio_jobqueue);
494 TAILQ_INIT(&ki->kaio_bufqueue);
495 TAILQ_INIT(&ki->kaio_liojoblist);
496 TAILQ_INIT(&ki->kaio_sockqueue);
498 if (p->p_aioinfo == NULL) {
503 uma_zfree(kaio_zone, ki);
506 while (num_aio_procs < target_aio_procs)
511 aio_sendsig(struct proc *p, struct sigevent *sigev, ksiginfo_t *ksi)
513 PROC_LOCK_ASSERT(p, MA_OWNED);
515 ksi->ksi_code = SI_ASYNCIO;
516 ksi->ksi_flags |= KSI_EXT | KSI_INS;
517 return (psignal_event(p, sigev, ksi));
523 * Free a job entry. Wait for completion if it is currently active, but don't
524 * delay forever. If we delay, we return a flag that says that we have to
525 * restart the queue scan.
528 aio_free_entry(struct aiocblist *aiocbe)
531 struct aioliojob *lj;
534 p = aiocbe->userproc;
536 PROC_LOCK_ASSERT(p, MA_OWNED);
538 MPASS(aiocbe->jobstate == JOBST_JOBFINISHED);
543 atomic_subtract_int(&num_queue_count, 1);
546 MPASS(ki->kaio_count >= 0);
548 TAILQ_REMOVE(&ki->kaio_done, aiocbe, plist);
549 TAILQ_REMOVE(&ki->kaio_all, aiocbe, allist);
554 lj->lioj_finished_count--;
556 if (lj->lioj_count == 0) {
557 TAILQ_REMOVE(&ki->kaio_liojoblist, lj, lioj_list);
558 /* lio is going away, we need to destroy any knotes */
559 knlist_delete(&lj->klist, curthread, 1);
560 sigqueue_take(&lj->lioj_ksi);
561 uma_zfree(aiolio_zone, lj);
565 /* aiocbe is going away, we need to destroy any knotes */
566 knlist_delete(&aiocbe->klist, curthread, 1);
567 sigqueue_take(&aiocbe->ksi);
569 MPASS(aiocbe->bp == NULL);
570 aiocbe->jobstate = JOBST_NULL;
574 * The thread argument here is used to find the owning process
575 * and is also passed to fo_close() which may pass it to various
576 * places such as devsw close() routines. Because of that, we
577 * need a thread pointer from the process owning the job that is
578 * persistent and won't disappear out from under us or move to
581 * Currently, all the callers of this function call it to remove
582 * an aiocblist from the current process' job list either via a
583 * syscall or due to the current process calling exit() or
584 * execve(). Thus, we know that p == curproc. We also know that
585 * curthread can't exit since we are curthread.
587 * Therefore, we use curthread as the thread to pass to
588 * knlist_delete(). This does mean that it is possible for the
589 * thread pointer at close time to differ from the thread pointer
590 * at open time, but this is already true of file descriptors in
591 * a multithreaded process.
593 fdrop(aiocbe->fd_file, curthread);
594 crfree(aiocbe->cred);
595 uma_zfree(aiocb_zone, aiocbe);
602 * Rundown the jobs for a given process.
605 aio_proc_rundown(void *arg, struct proc *p)
608 struct aioliojob *lj;
609 struct aiocblist *cbe, *cbn;
614 KASSERT(curthread->td_proc == p,
615 ("%s: called on non-curproc", __func__));
621 ki->kaio_flags |= KAIO_RUNDOWN;
626 * Try to cancel all pending requests. This code simulates
627 * aio_cancel on all pending I/O requests.
629 TAILQ_FOREACH_SAFE(cbe, &ki->kaio_jobqueue, plist, cbn) {
631 mtx_lock(&aio_job_mtx);
632 if (cbe->jobstate == JOBST_JOBQGLOBAL) {
633 TAILQ_REMOVE(&aio_jobs, cbe, list);
635 } else if (cbe->jobstate == JOBST_JOBQSOCK) {
637 MPASS(fp->f_type == DTYPE_SOCKET);
639 TAILQ_REMOVE(&so->so_aiojobq, cbe, list);
642 mtx_unlock(&aio_job_mtx);
645 cbe->jobstate = JOBST_JOBFINISHED;
646 cbe->uaiocb._aiocb_private.status = -1;
647 cbe->uaiocb._aiocb_private.error = ECANCELED;
648 TAILQ_REMOVE(&ki->kaio_jobqueue, cbe, plist);
649 aio_bio_done_notify(p, cbe, DONE_QUEUE);
653 /* Wait for all running I/O to be finished */
654 if (TAILQ_FIRST(&ki->kaio_bufqueue) ||
655 TAILQ_FIRST(&ki->kaio_jobqueue)) {
656 ki->kaio_flags |= KAIO_WAKEUP;
657 msleep(&p->p_aioinfo, &p->p_mtx, PRIBIO, "aioprn", hz);
661 /* Free all completed I/O requests. */
662 while ((cbe = TAILQ_FIRST(&ki->kaio_done)) != NULL)
665 while ((lj = TAILQ_FIRST(&ki->kaio_liojoblist)) != NULL) {
666 if (lj->lioj_count == 0) {
667 TAILQ_REMOVE(&ki->kaio_liojoblist, lj, lioj_list);
668 knlist_delete(&lj->klist, curthread, 1);
669 sigqueue_take(&lj->lioj_ksi);
670 uma_zfree(aiolio_zone, lj);
672 panic("LIO job not cleaned up: C:%d, FC:%d\n",
673 lj->lioj_count, lj->lioj_finished_count);
677 uma_zfree(kaio_zone, ki);
683 * Select a job to run (called by an AIO daemon).
685 static struct aiocblist *
686 aio_selectjob(struct aiothreadlist *aiop)
688 struct aiocblist *aiocbe;
692 mtx_assert(&aio_job_mtx, MA_OWNED);
693 TAILQ_FOREACH(aiocbe, &aio_jobs, list) {
694 userp = aiocbe->userproc;
695 ki = userp->p_aioinfo;
697 if (ki->kaio_active_count < ki->kaio_maxactive_count) {
698 TAILQ_REMOVE(&aio_jobs, aiocbe, list);
699 /* Account for currently active jobs. */
700 ki->kaio_active_count++;
701 aiocbe->jobstate = JOBST_JOBRUNNING;
709 * The AIO processing activity. This is the code that does the I/O request for
710 * the non-physio version of the operations. The normal vn operations are used,
711 * and this code should work in all instances for every type of file, including
712 * pipes, sockets, fifos, and regular files.
714 * XXX I don't think it works well for socket, pipe, and fifo.
717 aio_process(struct aiocblist *aiocbe)
719 struct ucred *td_savedcred;
729 int oublock_st, oublock_end;
730 int inblock_st, inblock_end;
733 td_savedcred = td->td_ucred;
734 td->td_ucred = aiocbe->cred;
736 cb = &aiocbe->uaiocb;
737 fp = aiocbe->fd_file;
739 aiov.iov_base = (void *)(uintptr_t)cb->aio_buf;
740 aiov.iov_len = cb->aio_nbytes;
742 auio.uio_iov = &aiov;
744 auio.uio_offset = cb->aio_offset;
745 auio.uio_resid = cb->aio_nbytes;
746 cnt = cb->aio_nbytes;
747 auio.uio_segflg = UIO_USERSPACE;
750 inblock_st = mycp->p_stats->p_ru.ru_inblock;
751 oublock_st = mycp->p_stats->p_ru.ru_oublock;
753 * aio_aqueue() acquires a reference to the file that is
754 * released in aio_free_entry().
756 if (cb->aio_lio_opcode == LIO_READ) {
757 auio.uio_rw = UIO_READ;
758 error = fo_read(fp, &auio, fp->f_cred, FOF_OFFSET, td);
760 if (fp->f_type == DTYPE_VNODE)
762 auio.uio_rw = UIO_WRITE;
763 error = fo_write(fp, &auio, fp->f_cred, FOF_OFFSET, td);
765 inblock_end = mycp->p_stats->p_ru.ru_inblock;
766 oublock_end = mycp->p_stats->p_ru.ru_oublock;
768 aiocbe->inputcharge = inblock_end - inblock_st;
769 aiocbe->outputcharge = oublock_end - oublock_st;
771 if ((error) && (auio.uio_resid != cnt)) {
772 if (error == ERESTART || error == EINTR || error == EWOULDBLOCK)
774 if ((error == EPIPE) && (cb->aio_lio_opcode == LIO_WRITE)) {
776 if (fp->f_type == DTYPE_SOCKET) {
778 if (so->so_options & SO_NOSIGPIPE)
782 PROC_LOCK(aiocbe->userproc);
783 psignal(aiocbe->userproc, SIGPIPE);
784 PROC_UNLOCK(aiocbe->userproc);
789 cnt -= auio.uio_resid;
790 cb->_aiocb_private.error = error;
791 cb->_aiocb_private.status = cnt;
792 td->td_ucred = td_savedcred;
796 aio_bio_done_notify(struct proc *userp, struct aiocblist *aiocbe, int type)
798 struct aioliojob *lj;
802 PROC_LOCK_ASSERT(userp, MA_OWNED);
803 ki = userp->p_aioinfo;
807 lj->lioj_finished_count++;
808 if (lj->lioj_count == lj->lioj_finished_count)
811 if (type == DONE_QUEUE) {
812 aiocbe->jobflags |= AIOCBLIST_DONE;
814 aiocbe->jobflags |= AIOCBLIST_BUFDONE;
816 TAILQ_INSERT_TAIL(&ki->kaio_done, aiocbe, plist);
817 aiocbe->jobstate = JOBST_JOBFINISHED;
819 if (ki->kaio_flags & KAIO_RUNDOWN)
820 goto notification_done;
822 if (aiocbe->uaiocb.aio_sigevent.sigev_notify == SIGEV_SIGNAL ||
823 aiocbe->uaiocb.aio_sigevent.sigev_notify == SIGEV_THREAD_ID)
824 aio_sendsig(userp, &aiocbe->uaiocb.aio_sigevent, &aiocbe->ksi);
826 KNOTE_LOCKED(&aiocbe->klist, 1);
829 if (lj->lioj_signal.sigev_notify == SIGEV_KEVENT) {
830 lj->lioj_flags |= LIOJ_KEVENT_POSTED;
831 KNOTE_LOCKED(&lj->klist, 1);
833 if ((lj->lioj_flags & (LIOJ_SIGNAL|LIOJ_SIGNAL_POSTED))
835 && (lj->lioj_signal.sigev_notify == SIGEV_SIGNAL ||
836 lj->lioj_signal.sigev_notify == SIGEV_THREAD_ID)) {
837 aio_sendsig(userp, &lj->lioj_signal, &lj->lioj_ksi);
838 lj->lioj_flags |= LIOJ_SIGNAL_POSTED;
843 if (ki->kaio_flags & KAIO_WAKEUP) {
844 ki->kaio_flags &= ~KAIO_WAKEUP;
845 wakeup(&userp->p_aioinfo);
850 * The AIO daemon, most of the actual work is done in aio_process,
851 * but the setup (and address space mgmt) is done in this routine.
854 aio_daemon(void *_id)
856 struct aiocblist *aiocbe;
857 struct aiothreadlist *aiop;
859 struct proc *curcp, *mycp, *userp;
860 struct vmspace *myvm, *tmpvm;
861 struct thread *td = curthread;
862 int id = (intptr_t)_id;
865 * Local copies of curproc (cp) and vmspace (myvm)
868 myvm = mycp->p_vmspace;
870 KASSERT(mycp->p_textvp == NULL, ("kthread has a textvp"));
873 * Allocate and ready the aio control info. There is one aiop structure
876 aiop = uma_zalloc(aiop_zone, M_WAITOK);
877 aiop->aiothread = td;
878 aiop->aiothreadflags = AIOP_FREE;
881 * Place thread (lightweight process) onto the AIO free thread list.
883 mtx_lock(&aio_job_mtx);
884 TAILQ_INSERT_HEAD(&aio_freeproc, aiop, list);
885 mtx_unlock(&aio_job_mtx);
888 * Get rid of our current filedescriptors. AIOD's don't need any
889 * filedescriptors, except as temporarily inherited from the client.
893 /* The daemon resides in its own pgrp. */
897 * Wakeup parent process. (Parent sleeps to keep from blasting away
898 * and creating too many daemons.)
900 sema_post(&aio_newproc_sem);
902 mtx_lock(&aio_job_mtx);
905 * curcp is the current daemon process context.
906 * userp is the current user process context.
911 * Take daemon off of free queue
913 if (aiop->aiothreadflags & AIOP_FREE) {
914 TAILQ_REMOVE(&aio_freeproc, aiop, list);
915 aiop->aiothreadflags &= ~AIOP_FREE;
921 while ((aiocbe = aio_selectjob(aiop)) != NULL) {
922 mtx_unlock(&aio_job_mtx);
923 userp = aiocbe->userproc;
926 * Connect to process address space for user program.
928 if (userp != curcp) {
930 * Save the current address space that we are
933 tmpvm = mycp->p_vmspace;
936 * Point to the new user address space, and
939 mycp->p_vmspace = userp->p_vmspace;
940 atomic_add_int(&mycp->p_vmspace->vm_refcnt, 1);
942 /* Activate the new mapping. */
943 pmap_activate(FIRST_THREAD_IN_PROC(mycp));
946 * If the old address space wasn't the daemons
947 * own address space, then we need to remove the
948 * daemon's reference from the other process
949 * that it was acting on behalf of.
957 ki = userp->p_aioinfo;
959 /* Do the I/O function. */
962 mtx_lock(&aio_job_mtx);
963 /* Decrement the active job count. */
964 ki->kaio_active_count--;
965 mtx_unlock(&aio_job_mtx);
968 TAILQ_REMOVE(&ki->kaio_jobqueue, aiocbe, plist);
969 aio_bio_done_notify(userp, aiocbe, DONE_QUEUE);
972 mtx_lock(&aio_job_mtx);
976 * Disconnect from user address space.
980 mtx_unlock(&aio_job_mtx);
982 /* Get the user address space to disconnect from. */
983 tmpvm = mycp->p_vmspace;
985 /* Get original address space for daemon. */
986 mycp->p_vmspace = myvm;
988 /* Activate the daemon's address space. */
989 pmap_activate(FIRST_THREAD_IN_PROC(mycp));
992 printf("AIOD: vmspace problem -- %d\n",
996 /* Remove our vmspace reference. */
1001 mtx_lock(&aio_job_mtx);
1003 * We have to restart to avoid race, we only sleep if
1004 * no job can be selected, that should be
1010 mtx_assert(&aio_job_mtx, MA_OWNED);
1012 TAILQ_INSERT_HEAD(&aio_freeproc, aiop, list);
1013 aiop->aiothreadflags |= AIOP_FREE;
1016 * If daemon is inactive for a long time, allow it to exit,
1017 * thereby freeing resources.
1019 if (msleep(aiop->aiothread, &aio_job_mtx, PRIBIO, "aiordy",
1021 if (TAILQ_EMPTY(&aio_jobs)) {
1022 if ((aiop->aiothreadflags & AIOP_FREE) &&
1023 (num_aio_procs > target_aio_procs)) {
1024 TAILQ_REMOVE(&aio_freeproc, aiop, list);
1026 mtx_unlock(&aio_job_mtx);
1027 uma_zfree(aiop_zone, aiop);
1028 free_unr(aiod_unr, id);
1030 if (mycp->p_vmspace->vm_refcnt <= 1) {
1031 printf("AIOD: bad vm refcnt for"
1032 " exiting daemon: %d\n",
1033 mycp->p_vmspace->vm_refcnt);
1041 mtx_unlock(&aio_job_mtx);
1042 panic("shouldn't be here\n");
1046 * Create a new AIO daemon. This is mostly a kernel-thread fork routine. The
1047 * AIO daemon modifies its environment itself.
1050 aio_newproc(int *start)
1056 id = alloc_unr(aiod_unr);
1057 error = kthread_create(aio_daemon, (void *)(intptr_t)id, &p,
1058 RFNOWAIT, 0, "aiod%d", id);
1061 * Wait until daemon is started.
1063 sema_wait(&aio_newproc_sem);
1064 mtx_lock(&aio_job_mtx);
1068 mtx_unlock(&aio_job_mtx);
1070 free_unr(aiod_unr, id);
1076 * Try the high-performance, low-overhead physio method for eligible
1077 * VCHR devices. This method doesn't use an aio helper thread, and
1078 * thus has very low overhead.
1080 * Assumes that the caller, aio_aqueue(), has incremented the file
1081 * structure's reference count, preventing its deallocation for the
1082 * duration of this call.
1085 aio_qphysio(struct proc *p, struct aiocblist *aiocbe)
1091 struct kaioinfo *ki;
1092 struct aioliojob *lj;
1095 cb = &aiocbe->uaiocb;
1096 fp = aiocbe->fd_file;
1098 if (fp->f_type != DTYPE_VNODE)
1104 * If its not a disk, we don't want to return a positive error.
1105 * It causes the aio code to not fall through to try the thread
1106 * way when you're talking to a regular file.
1108 if (!vn_isdisk(vp, &error)) {
1109 if (error == ENOTBLK)
1115 if (vp->v_bufobj.bo_bsize == 0)
1118 if (cb->aio_nbytes % vp->v_bufobj.bo_bsize)
1121 if (cb->aio_nbytes > vp->v_rdev->si_iosize_max)
1124 if (cb->aio_nbytes >
1125 MAXPHYS - (((vm_offset_t) cb->aio_buf) & PAGE_MASK))
1129 if (ki->kaio_buffer_count >= ki->kaio_ballowed_count)
1132 /* Create and build a buffer header for a transfer. */
1133 bp = (struct buf *)getpbuf(NULL);
1138 ki->kaio_buffer_count++;
1145 * Get a copy of the kva from the physical buffer.
1149 bp->b_bcount = cb->aio_nbytes;
1150 bp->b_bufsize = cb->aio_nbytes;
1151 bp->b_iodone = aio_physwakeup;
1152 bp->b_saveaddr = bp->b_data;
1153 bp->b_data = (void *)(uintptr_t)cb->aio_buf;
1154 bp->b_offset = cb->aio_offset;
1155 bp->b_iooffset = cb->aio_offset;
1156 bp->b_blkno = btodb(cb->aio_offset);
1157 bp->b_iocmd = cb->aio_lio_opcode == LIO_WRITE ? BIO_WRITE : BIO_READ;
1160 * Bring buffer into kernel space.
1162 if (vmapbuf(bp) < 0) {
1169 bp->b_caller1 = (void *)aiocbe;
1170 TAILQ_INSERT_TAIL(&ki->kaio_bufqueue, aiocbe, plist);
1171 TAILQ_INSERT_TAIL(&ki->kaio_all, aiocbe, allist);
1172 aiocbe->jobstate = JOBST_JOBQBUF;
1173 cb->_aiocb_private.status = cb->aio_nbytes;
1176 atomic_add_int(&num_queue_count, 1);
1177 atomic_add_int(&num_buf_aio, 1);
1181 TASK_INIT(&aiocbe->biotask, 0, biohelper, aiocbe);
1183 /* Perform transfer. */
1184 dev_strategy(vp->v_rdev, bp);
1190 ki->kaio_buffer_count--;
1200 * Wake up aio requests that may be serviceable now.
1203 aio_swake_cb(struct socket *so, struct sockbuf *sb)
1205 struct aiocblist *cb, *cbn;
1206 int opcode, wakecount = 0;
1207 struct aiothreadlist *aiop;
1209 if (sb == &so->so_snd)
1215 sb->sb_flags &= ~SB_AIO;
1216 mtx_lock(&aio_job_mtx);
1217 TAILQ_FOREACH_SAFE(cb, &so->so_aiojobq, list, cbn) {
1218 if (opcode == cb->uaiocb.aio_lio_opcode) {
1219 if (cb->jobstate != JOBST_JOBQSOCK)
1220 panic("invalid queue value");
1222 * We don't have actual sockets backend yet,
1223 * so we simply move the requests to the generic
1226 TAILQ_REMOVE(&so->so_aiojobq, cb, list);
1227 TAILQ_INSERT_TAIL(&aio_jobs, cb, list);
1231 mtx_unlock(&aio_job_mtx);
1234 while (wakecount--) {
1235 mtx_lock(&aio_job_mtx);
1236 if ((aiop = TAILQ_FIRST(&aio_freeproc)) != NULL) {
1237 TAILQ_REMOVE(&aio_freeproc, aiop, list);
1238 aiop->aiothreadflags &= ~AIOP_FREE;
1239 wakeup(aiop->aiothread);
1241 mtx_unlock(&aio_job_mtx);
1246 * Queue a new AIO request. Choosing either the threaded or direct physio VCHR
1247 * technique is done in this code.
1250 aio_aqueue(struct thread *td, struct aiocb *job, struct aioliojob *lj,
1251 int type, int oldsigev)
1253 struct proc *p = td->td_proc;
1256 struct aiocblist *aiocbe;
1257 struct aiothreadlist *aiop;
1258 struct kaioinfo *ki;
1268 if (p->p_aioinfo == NULL)
1269 aio_init_aioinfo(p);
1273 suword(&job->_aiocb_private.status, -1);
1274 suword(&job->_aiocb_private.error, 0);
1275 suword(&job->_aiocb_private.kernelinfo, -1);
1277 if (num_queue_count >= max_queue_count ||
1278 ki->kaio_count >= ki->kaio_qallowed_count) {
1279 suword(&job->_aiocb_private.error, EAGAIN);
1283 aiocbe = uma_zalloc(aiocb_zone, M_WAITOK | M_ZERO);
1284 aiocbe->inputcharge = 0;
1285 aiocbe->outputcharge = 0;
1286 knlist_init(&aiocbe->klist, &p->p_mtx, NULL, NULL, NULL);
1289 bzero(&aiocbe->uaiocb, sizeof(struct aiocb));
1290 error = copyin(job, &aiocbe->uaiocb, sizeof(struct oaiocb));
1291 bcopy(&aiocbe->uaiocb.__spare__, &aiocbe->uaiocb.aio_sigevent,
1292 sizeof(struct osigevent));
1294 error = copyin(job, &aiocbe->uaiocb, sizeof(struct aiocb));
1297 suword(&job->_aiocb_private.error, error);
1298 uma_zfree(aiocb_zone, aiocbe);
1302 if (aiocbe->uaiocb.aio_sigevent.sigev_notify != SIGEV_KEVENT &&
1303 aiocbe->uaiocb.aio_sigevent.sigev_notify != SIGEV_SIGNAL &&
1304 aiocbe->uaiocb.aio_sigevent.sigev_notify != SIGEV_THREAD_ID &&
1305 aiocbe->uaiocb.aio_sigevent.sigev_notify != SIGEV_NONE) {
1306 suword(&job->_aiocb_private.error, EINVAL);
1307 uma_zfree(aiocb_zone, aiocbe);
1311 if ((aiocbe->uaiocb.aio_sigevent.sigev_notify == SIGEV_SIGNAL ||
1312 aiocbe->uaiocb.aio_sigevent.sigev_notify == SIGEV_THREAD_ID) &&
1313 !_SIG_VALID(aiocbe->uaiocb.aio_sigevent.sigev_signo)) {
1314 uma_zfree(aiocb_zone, aiocbe);
1318 ksiginfo_init(&aiocbe->ksi);
1320 /* Save userspace address of the job info. */
1321 aiocbe->uuaiocb = job;
1323 /* Get the opcode. */
1324 if (type != LIO_NOP)
1325 aiocbe->uaiocb.aio_lio_opcode = type;
1326 opcode = aiocbe->uaiocb.aio_lio_opcode;
1328 /* Fetch the file object for the specified file descriptor. */
1329 fd = aiocbe->uaiocb.aio_fildes;
1332 error = fget_write(td, fd, &fp);
1335 error = fget_read(td, fd, &fp);
1338 error = fget(td, fd, &fp);
1341 uma_zfree(aiocb_zone, aiocbe);
1342 suword(&job->_aiocb_private.error, error);
1345 aiocbe->fd_file = fp;
1347 if (aiocbe->uaiocb.aio_offset == -1LL) {
1352 mtx_lock(&aio_job_mtx);
1354 if (jobrefid == LONG_MAX)
1358 mtx_unlock(&aio_job_mtx);
1360 error = suword(&job->_aiocb_private.kernelinfo, jid);
1365 aiocbe->uaiocb._aiocb_private.kernelinfo = (void *)(intptr_t)jid;
1367 if (opcode == LIO_NOP) {
1369 uma_zfree(aiocb_zone, aiocbe);
1372 if ((opcode != LIO_READ) && (opcode != LIO_WRITE)) {
1377 if (aiocbe->uaiocb.aio_sigevent.sigev_notify == SIGEV_KEVENT) {
1378 kev.ident = aiocbe->uaiocb.aio_sigevent.sigev_notify_kqueue;
1381 error = fget(td, (u_int)kev.ident, &kq_fp);
1384 if (kq_fp->f_type != DTYPE_KQUEUE) {
1390 kev.ident = (uintptr_t)aiocbe->uuaiocb;
1391 kev.filter = EVFILT_AIO;
1392 kev.flags = EV_ADD | EV_ENABLE | EV_FLAG1;
1393 kev.data = (intptr_t)aiocbe;
1394 kev.udata = aiocbe->uaiocb.aio_sigevent.sigev_value.sival_ptr;
1395 error = kqueue_register(kq, &kev, td, 1);
1400 uma_zfree(aiocb_zone, aiocbe);
1401 suword(&job->_aiocb_private.error, error);
1406 suword(&job->_aiocb_private.error, EINPROGRESS);
1407 aiocbe->uaiocb._aiocb_private.error = EINPROGRESS;
1408 aiocbe->userproc = p;
1409 aiocbe->cred = crhold(td->td_ucred);
1410 aiocbe->jobflags = 0;
1413 if (fp->f_type == DTYPE_SOCKET) {
1415 * Alternate queueing for socket ops: Reach down into the
1416 * descriptor to get the socket data. Then check to see if the
1417 * socket is ready to be read or written (based on the requested
1420 * If it is not ready for io, then queue the aiocbe on the
1421 * socket, and set the flags so we get a call when sbnotify()
1424 * Note if opcode is neither LIO_WRITE nor LIO_READ we lock
1425 * and unlock the snd sockbuf for no reason.
1428 sb = (opcode == LIO_READ) ? &so->so_rcv : &so->so_snd;
1430 if (((opcode == LIO_READ) && (!soreadable(so))) || ((opcode ==
1431 LIO_WRITE) && (!sowriteable(so)))) {
1432 sb->sb_flags |= SB_AIO;
1434 mtx_lock(&aio_job_mtx);
1435 TAILQ_INSERT_TAIL(&so->so_aiojobq, aiocbe, list);
1436 mtx_unlock(&aio_job_mtx);
1439 TAILQ_INSERT_TAIL(&ki->kaio_all, aiocbe, allist);
1440 TAILQ_INSERT_TAIL(&ki->kaio_jobqueue, aiocbe, plist);
1441 aiocbe->jobstate = JOBST_JOBQSOCK;
1447 atomic_add_int(&num_queue_count, 1);
1454 if ((error = aio_qphysio(p, aiocbe)) == 0)
1458 aiocbe->uaiocb._aiocb_private.error = error;
1459 suword(&job->_aiocb_private.error, error);
1463 /* No buffer for daemon I/O. */
1470 TAILQ_INSERT_TAIL(&ki->kaio_jobqueue, aiocbe, plist);
1471 TAILQ_INSERT_TAIL(&ki->kaio_all, aiocbe, allist);
1473 mtx_lock(&aio_job_mtx);
1474 TAILQ_INSERT_TAIL(&aio_jobs, aiocbe, list);
1475 aiocbe->jobstate = JOBST_JOBQGLOBAL;
1478 atomic_add_int(&num_queue_count, 1);
1481 * If we don't have a free AIO process, and we are below our quota, then
1482 * start one. Otherwise, depend on the subsequent I/O completions to
1483 * pick-up this job. If we don't sucessfully create the new process
1484 * (thread) due to resource issues, we return an error for now (EAGAIN),
1485 * which is likely not the correct thing to do.
1489 if ((aiop = TAILQ_FIRST(&aio_freeproc)) != NULL) {
1490 TAILQ_REMOVE(&aio_freeproc, aiop, list);
1491 aiop->aiothreadflags &= ~AIOP_FREE;
1492 wakeup(aiop->aiothread);
1493 } else if (((num_aio_resv_start + num_aio_procs) < max_aio_procs) &&
1494 ((ki->kaio_active_count + num_aio_resv_start) <
1495 ki->kaio_maxactive_count)) {
1496 num_aio_resv_start++;
1497 mtx_unlock(&aio_job_mtx);
1498 error = aio_newproc(&num_aio_resv_start);
1499 mtx_lock(&aio_job_mtx);
1501 num_aio_resv_start--;
1505 mtx_unlock(&aio_job_mtx);
1512 * Support the aio_return system call, as a side-effect, kernel resources are
1516 aio_return(struct thread *td, struct aio_return_args *uap)
1518 struct proc *p = td->td_proc;
1519 struct aiocblist *cb;
1520 struct aiocb *uaiocb;
1521 struct kaioinfo *ki;
1527 uaiocb = uap->aiocbp;
1529 TAILQ_FOREACH(cb, &ki->kaio_done, plist) {
1530 if (cb->uuaiocb == uaiocb)
1534 MPASS(cb->jobstate == JOBST_JOBFINISHED);
1535 status = cb->uaiocb._aiocb_private.status;
1536 error = cb->uaiocb._aiocb_private.error;
1537 td->td_retval[0] = status;
1538 if (cb->uaiocb.aio_lio_opcode == LIO_WRITE) {
1539 p->p_stats->p_ru.ru_oublock +=
1541 cb->outputcharge = 0;
1542 } else if (cb->uaiocb.aio_lio_opcode == LIO_READ) {
1543 p->p_stats->p_ru.ru_inblock += cb->inputcharge;
1544 cb->inputcharge = 0;
1548 suword(&uaiocb->_aiocb_private.error, error);
1549 suword(&uaiocb->_aiocb_private.status, status);
1558 * Allow a process to wakeup when any of the I/O requests are completed.
1561 aio_suspend(struct thread *td, struct aio_suspend_args *uap)
1563 struct proc *p = td->td_proc;
1566 struct aiocb *const *cbptr, *cbp;
1567 struct kaioinfo *ki;
1568 struct aiocblist *cb, *cbfirst;
1569 struct aiocb **ujoblist;
1575 if (uap->nent < 0 || uap->nent > AIO_LISTIO_MAX)
1580 /* Get timespec struct. */
1581 if ((error = copyin(uap->timeout, &ts, sizeof(ts))) != 0)
1584 if (ts.tv_nsec < 0 || ts.tv_nsec >= 1000000000)
1587 TIMESPEC_TO_TIMEVAL(&atv, &ts);
1588 if (itimerfix(&atv))
1590 timo = tvtohz(&atv);
1598 ujoblist = uma_zalloc(aiol_zone, M_WAITOK);
1599 cbptr = uap->aiocbp;
1601 for (i = 0; i < uap->nent; i++) {
1602 cbp = (struct aiocb *)(intptr_t)fuword(&cbptr[i]);
1605 ujoblist[njoblist] = cbp;
1609 if (njoblist == 0) {
1610 uma_zfree(aiol_zone, ujoblist);
1618 TAILQ_FOREACH(cb, &ki->kaio_all, allist) {
1619 for (i = 0; i < njoblist; i++) {
1620 if (cb->uuaiocb == ujoblist[i]) {
1621 if (cbfirst == NULL)
1623 if (cb->jobstate == JOBST_JOBFINISHED)
1628 /* All tasks were finished. */
1629 if (cbfirst == NULL)
1632 ki->kaio_flags |= KAIO_WAKEUP;
1633 error = msleep(&p->p_aioinfo, &p->p_mtx, PRIBIO | PCATCH,
1635 if (error == ERESTART)
1642 uma_zfree(aiol_zone, ujoblist);
1647 * aio_cancel cancels any non-physio aio operations not currently in
1651 aio_cancel(struct thread *td, struct aio_cancel_args *uap)
1653 struct proc *p = td->td_proc;
1654 struct kaioinfo *ki;
1655 struct aiocblist *cbe, *cbn;
1661 int notcancelled = 0;
1664 /* Lookup file object. */
1665 error = fget(td, uap->fd, &fp);
1673 if (fp->f_type == DTYPE_VNODE) {
1675 if (vn_isdisk(vp, &error)) {
1677 td->td_retval[0] = AIO_NOTCANCELED;
1683 TAILQ_FOREACH_SAFE(cbe, &ki->kaio_jobqueue, plist, cbn) {
1684 if ((uap->fd == cbe->uaiocb.aio_fildes) &&
1685 ((uap->aiocbp == NULL) ||
1686 (uap->aiocbp == cbe->uuaiocb))) {
1689 mtx_lock(&aio_job_mtx);
1690 if (cbe->jobstate == JOBST_JOBQGLOBAL) {
1691 TAILQ_REMOVE(&aio_jobs, cbe, list);
1693 } else if (cbe->jobstate == JOBST_JOBQSOCK) {
1694 MPASS(fp->f_type == DTYPE_SOCKET);
1696 TAILQ_REMOVE(&so->so_aiojobq, cbe, list);
1699 mtx_unlock(&aio_job_mtx);
1702 TAILQ_REMOVE(&ki->kaio_jobqueue, cbe, plist);
1703 cbe->uaiocb._aiocb_private.status = -1;
1704 cbe->uaiocb._aiocb_private.error = ECANCELED;
1705 aio_bio_done_notify(p, cbe, DONE_QUEUE);
1710 if (uap->aiocbp != NULL)
1719 if (uap->aiocbp != NULL) {
1721 td->td_retval[0] = AIO_CANCELED;
1727 td->td_retval[0] = AIO_NOTCANCELED;
1732 td->td_retval[0] = AIO_CANCELED;
1736 td->td_retval[0] = AIO_ALLDONE;
1742 * aio_error is implemented in the kernel level for compatibility purposes only.
1743 * For a user mode async implementation, it would be best to do it in a userland
1747 aio_error(struct thread *td, struct aio_error_args *uap)
1749 struct proc *p = td->td_proc;
1750 struct aiocblist *cb;
1751 struct kaioinfo *ki;
1756 td->td_retval[0] = EINVAL;
1761 TAILQ_FOREACH(cb, &ki->kaio_all, allist) {
1762 if (cb->uuaiocb == uap->aiocbp) {
1763 if (cb->jobstate == JOBST_JOBFINISHED)
1765 cb->uaiocb._aiocb_private.error;
1767 td->td_retval[0] = EINPROGRESS;
1775 * Hack for failure of aio_aqueue.
1777 status = fuword(&uap->aiocbp->_aiocb_private.status);
1779 td->td_retval[0] = fuword(&uap->aiocbp->_aiocb_private.error);
1783 td->td_retval[0] = EINVAL;
1787 /* syscall - asynchronous read from a file (REALTIME) */
1789 oaio_read(struct thread *td, struct oaio_read_args *uap)
1792 return aio_aqueue(td, (struct aiocb *)uap->aiocbp, NULL, LIO_READ, 1);
1796 aio_read(struct thread *td, struct aio_read_args *uap)
1799 return aio_aqueue(td, uap->aiocbp, NULL, LIO_READ, 0);
1802 /* syscall - asynchronous write to a file (REALTIME) */
1804 oaio_write(struct thread *td, struct oaio_write_args *uap)
1807 return aio_aqueue(td, (struct aiocb *)uap->aiocbp, NULL, LIO_WRITE, 1);
1811 aio_write(struct thread *td, struct aio_write_args *uap)
1814 return aio_aqueue(td, uap->aiocbp, NULL, LIO_WRITE, 0);
1817 /* syscall - list directed I/O (REALTIME) */
1819 olio_listio(struct thread *td, struct olio_listio_args *uap)
1821 return do_lio_listio(td, (struct lio_listio_args *)uap, 1);
1824 /* syscall - list directed I/O (REALTIME) */
1826 lio_listio(struct thread *td, struct lio_listio_args *uap)
1828 return do_lio_listio(td, uap, 0);
1832 do_lio_listio(struct thread *td, struct lio_listio_args *uap, int oldsigev)
1834 struct proc *p = td->td_proc;
1835 struct aiocb *iocb, * const *cbptr;
1836 struct kaioinfo *ki;
1837 struct aioliojob *lj;
1846 if ((uap->mode != LIO_NOWAIT) && (uap->mode != LIO_WAIT))
1850 if (nent < 0 || nent > AIO_LISTIO_MAX)
1853 if (p->p_aioinfo == NULL)
1854 aio_init_aioinfo(p);
1858 lj = uma_zalloc(aiolio_zone, M_WAITOK);
1861 lj->lioj_finished_count = 0;
1862 knlist_init(&lj->klist, &p->p_mtx, NULL, NULL, NULL);
1863 ksiginfo_init(&lj->lioj_ksi);
1868 if (uap->sig && (uap->mode == LIO_NOWAIT)) {
1869 bzero(&lj->lioj_signal, sizeof(&lj->lioj_signal));
1870 error = copyin(uap->sig, &lj->lioj_signal,
1871 oldsigev ? sizeof(struct osigevent) :
1872 sizeof(struct sigevent));
1874 uma_zfree(aiolio_zone, lj);
1878 if (lj->lioj_signal.sigev_notify == SIGEV_KEVENT) {
1879 /* Assume only new style KEVENT */
1880 error = fget(td, lj->lioj_signal.sigev_notify_kqueue,
1883 uma_zfree(aiolio_zone, lj);
1886 if (kq_fp->f_type != DTYPE_KQUEUE) {
1888 uma_zfree(aiolio_zone, lj);
1891 kq = (struct kqueue *)kq_fp->f_data;
1892 kev.filter = EVFILT_LIO;
1893 kev.flags = EV_ADD | EV_ENABLE | EV_FLAG1;
1894 kev.ident = (uintptr_t)lj; /* something unique */
1895 kev.data = (intptr_t)lj;
1896 /* pass user defined sigval data */
1897 kev.udata = lj->lioj_signal.sigev_value.sival_ptr;
1898 error = kqueue_register(kq, &kev, td, 1);
1901 uma_zfree(aiolio_zone, lj);
1904 } else if (lj->lioj_signal.sigev_notify == SIGEV_NONE) {
1906 } else if (lj->lioj_signal.sigev_notify == SIGEV_SIGNAL ||
1907 lj->lioj_signal.sigev_notify == SIGEV_THREAD_ID) {
1908 if (!_SIG_VALID(lj->lioj_signal.sigev_signo)) {
1909 uma_zfree(aiolio_zone, lj);
1912 lj->lioj_flags |= LIOJ_SIGNAL;
1914 uma_zfree(aiolio_zone, lj);
1920 TAILQ_INSERT_TAIL(&ki->kaio_liojoblist, lj, lioj_list);
1922 * Add extra aiocb count to avoid the lio to be freed
1923 * by other threads doing aio_waitcomplete or aio_return,
1924 * and prevent event from being sent until we have queued
1931 * Get pointers to the list of I/O requests.
1934 cbptr = uap->acb_list;
1935 for (i = 0; i < uap->nent; i++) {
1936 iocb = (struct aiocb *)(intptr_t)fuword(&cbptr[i]);
1937 if (((intptr_t)iocb != -1) && ((intptr_t)iocb != 0)) {
1938 error = aio_aqueue(td, iocb, lj, LIO_NOP, oldsigev);
1946 if (uap->mode == LIO_WAIT) {
1947 while (lj->lioj_count - 1 != lj->lioj_finished_count) {
1948 ki->kaio_flags |= KAIO_WAKEUP;
1949 error = msleep(&p->p_aioinfo, &p->p_mtx,
1950 PRIBIO | PCATCH, "aiospn", 0);
1951 if (error == ERESTART)
1957 if (lj->lioj_count - 1 == lj->lioj_finished_count) {
1958 if (lj->lioj_signal.sigev_notify == SIGEV_KEVENT) {
1959 lj->lioj_flags |= LIOJ_KEVENT_POSTED;
1960 KNOTE_LOCKED(&lj->klist, 1);
1962 if ((lj->lioj_flags & (LIOJ_SIGNAL|LIOJ_SIGNAL_POSTED))
1964 && (lj->lioj_signal.sigev_notify == SIGEV_SIGNAL ||
1965 lj->lioj_signal.sigev_notify == SIGEV_THREAD_ID)) {
1966 aio_sendsig(p, &lj->lioj_signal,
1968 lj->lioj_flags |= LIOJ_SIGNAL_POSTED;
1973 if (lj->lioj_count == 0) {
1974 TAILQ_REMOVE(&ki->kaio_liojoblist, lj, lioj_list);
1975 knlist_delete(&lj->klist, curthread, 1);
1976 sigqueue_take(&lj->lioj_ksi);
1978 uma_zfree(aiolio_zone, lj);
1988 * Called from interrupt thread for physio, we should return as fast
1989 * as possible, so we schedule a biohelper task.
1992 aio_physwakeup(struct buf *bp)
1994 struct aiocblist *aiocbe;
1996 aiocbe = (struct aiocblist *)bp->b_caller1;
1997 taskqueue_enqueue(taskqueue_aiod_bio, &aiocbe->biotask);
2001 * Task routine to perform heavy tasks, process wakeup, and signals.
2004 biohelper(void *context, int pending)
2006 struct aiocblist *aiocbe = context;
2009 struct kaioinfo *ki;
2013 userp = aiocbe->userproc;
2014 ki = userp->p_aioinfo;
2016 aiocbe->uaiocb._aiocb_private.status -= bp->b_resid;
2017 aiocbe->uaiocb._aiocb_private.error = 0;
2018 if (bp->b_ioflags & BIO_ERROR)
2019 aiocbe->uaiocb._aiocb_private.error = bp->b_error;
2020 nblks = btodb(aiocbe->uaiocb.aio_nbytes);
2021 if (aiocbe->uaiocb.aio_lio_opcode == LIO_WRITE)
2022 aiocbe->outputcharge += nblks;
2024 aiocbe->inputcharge += nblks;
2026 TAILQ_REMOVE(&userp->p_aioinfo->kaio_bufqueue, aiocbe, plist);
2027 ki->kaio_buffer_count--;
2028 aio_bio_done_notify(userp, aiocbe, DONE_BUF);
2031 /* Release mapping into kernel space. */
2034 atomic_subtract_int(&num_buf_aio, 1);
2037 /* syscall - wait for the next completion of an aio request */
2039 aio_waitcomplete(struct thread *td, struct aio_waitcomplete_args *uap)
2041 struct proc *p = td->td_proc;
2044 struct kaioinfo *ki;
2045 struct aiocblist *cb;
2046 struct aiocb *uuaiocb;
2047 int error, status, timo;
2049 suword(uap->aiocbp, (long)NULL);
2053 /* Get timespec struct. */
2054 error = copyin(uap->timeout, &ts, sizeof(ts));
2058 if ((ts.tv_nsec < 0) || (ts.tv_nsec >= 1000000000))
2061 TIMESPEC_TO_TIMEVAL(&atv, &ts);
2062 if (itimerfix(&atv))
2064 timo = tvtohz(&atv);
2067 if (p->p_aioinfo == NULL)
2068 aio_init_aioinfo(p);
2074 while ((cb = TAILQ_FIRST(&ki->kaio_done)) == NULL) {
2075 ki->kaio_flags |= KAIO_WAKEUP;
2076 error = msleep(&p->p_aioinfo, &p->p_mtx, PRIBIO | PCATCH,
2078 if (timo && error == ERESTART)
2085 MPASS(cb->jobstate == JOBST_JOBFINISHED);
2086 uuaiocb = cb->uuaiocb;
2087 status = cb->uaiocb._aiocb_private.status;
2088 error = cb->uaiocb._aiocb_private.error;
2089 td->td_retval[0] = status;
2090 if (cb->uaiocb.aio_lio_opcode == LIO_WRITE) {
2091 p->p_stats->p_ru.ru_oublock += cb->outputcharge;
2092 cb->outputcharge = 0;
2093 } else if (cb->uaiocb.aio_lio_opcode == LIO_READ) {
2094 p->p_stats->p_ru.ru_inblock += cb->inputcharge;
2095 cb->inputcharge = 0;
2099 suword(uap->aiocbp, (long)uuaiocb);
2100 suword(&uuaiocb->_aiocb_private.error, error);
2101 suword(&uuaiocb->_aiocb_private.status, status);
2108 /* kqueue attach function */
2110 filt_aioattach(struct knote *kn)
2112 struct aiocblist *aiocbe = (struct aiocblist *)kn->kn_sdata;
2115 * The aiocbe pointer must be validated before using it, so
2116 * registration is restricted to the kernel; the user cannot
2119 if ((kn->kn_flags & EV_FLAG1) == 0)
2121 kn->kn_flags &= ~EV_FLAG1;
2123 knlist_add(&aiocbe->klist, kn, 0);
2128 /* kqueue detach function */
2130 filt_aiodetach(struct knote *kn)
2132 struct aiocblist *aiocbe = (struct aiocblist *)kn->kn_sdata;
2134 if (!knlist_empty(&aiocbe->klist))
2135 knlist_remove(&aiocbe->klist, kn, 0);
2138 /* kqueue filter function */
2141 filt_aio(struct knote *kn, long hint)
2143 struct aiocblist *aiocbe = (struct aiocblist *)kn->kn_sdata;
2145 kn->kn_data = aiocbe->uaiocb._aiocb_private.error;
2146 if (aiocbe->jobstate != JOBST_JOBFINISHED)
2148 kn->kn_flags |= EV_EOF;
2152 /* kqueue attach function */
2154 filt_lioattach(struct knote *kn)
2156 struct aioliojob * lj = (struct aioliojob *)kn->kn_sdata;
2159 * The aioliojob pointer must be validated before using it, so
2160 * registration is restricted to the kernel; the user cannot
2163 if ((kn->kn_flags & EV_FLAG1) == 0)
2165 kn->kn_flags &= ~EV_FLAG1;
2167 knlist_add(&lj->klist, kn, 0);
2172 /* kqueue detach function */
2174 filt_liodetach(struct knote *kn)
2176 struct aioliojob * lj = (struct aioliojob *)kn->kn_sdata;
2178 if (!knlist_empty(&lj->klist))
2179 knlist_remove(&lj->klist, kn, 0);
2182 /* kqueue filter function */
2185 filt_lio(struct knote *kn, long hint)
2187 struct aioliojob * lj = (struct aioliojob *)kn->kn_sdata;
2189 return (lj->lioj_flags & LIOJ_KEVENT_POSTED);