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 TAILQ_ENTRY(aiocblist) list; /* List of jobs */
189 TAILQ_ENTRY(aiocblist) plist; /* List of jobs for proc */
190 TAILQ_ENTRY(aiocblist) allist;
195 struct buf *bp; /* Buffer pointer */
196 struct proc *userproc; /* User process */
197 struct ucred *cred; /* Active credential when created */
198 struct file *fd_file; /* Pointer to file structure */
199 struct aioliojob *lio; /* Optional lio job */
200 struct aiocb *uuaiocb; /* Pointer in userspace of aiocb */
201 struct knlist klist; /* list of knotes */
202 struct aiocb uaiocb; /* Kernel I/O control block */
203 ksiginfo_t ksi; /* Realtime signal info */
208 #define AIOCBLIST_RUNDOWN 0x04
209 #define AIOCBLIST_DONE 0x10
210 #define AIOCBLIST_BUFDONE 0x20
215 #define AIOP_FREE 0x1 /* proc on free queue */
217 struct aiothreadlist {
218 int aiothreadflags; /* AIO proc flags */
219 TAILQ_ENTRY(aiothreadlist) list; /* List of processes */
220 struct thread *aiothread; /* The AIO thread */
224 * data-structure for lio signal management
229 int lioj_finished_count;
230 struct sigevent lioj_signal; /* signal on all I/O done */
231 TAILQ_ENTRY(aioliojob) lioj_list;
232 struct knlist klist; /* list of knotes */
233 ksiginfo_t lioj_ksi; /* Realtime signal info */
236 #define LIOJ_SIGNAL 0x1 /* signal on all done (lio) */
237 #define LIOJ_SIGNAL_POSTED 0x2 /* signal has been posted */
238 #define LIOJ_KEVENT_POSTED 0x4 /* kevent triggered */
241 * per process aio data structure
244 int kaio_flags; /* per process kaio flags */
245 int kaio_maxactive_count; /* maximum number of AIOs */
246 int kaio_active_count; /* number of currently used AIOs */
247 int kaio_qallowed_count; /* maxiumu size of AIO queue */
248 int kaio_count; /* size of AIO queue */
249 int kaio_ballowed_count; /* maximum number of buffers */
250 int kaio_buffer_count; /* number of physio buffers */
251 TAILQ_HEAD(,aiocblist) kaio_all; /* all AIOs in the process */
252 TAILQ_HEAD(,aiocblist) kaio_done; /* done queue for process */
253 TAILQ_HEAD(,aioliojob) kaio_liojoblist; /* list of lio jobs */
254 TAILQ_HEAD(,aiocblist) kaio_jobqueue; /* job queue for process */
255 TAILQ_HEAD(,aiocblist) kaio_bufqueue; /* buffer job queue for process */
256 TAILQ_HEAD(,aiocblist) kaio_sockqueue; /* queue for aios waiting on sockets */
259 #define KAIO_RUNDOWN 0x1 /* process is being run down */
260 #define KAIO_WAKEUP 0x2 /* wakeup process when there is a significant event */
262 static TAILQ_HEAD(,aiothreadlist) aio_freeproc; /* Idle daemons */
263 static struct sema aio_newproc_sem;
264 static struct mtx aio_job_mtx;
265 static struct mtx aio_sock_mtx;
266 static TAILQ_HEAD(,aiocblist) aio_jobs; /* Async job list */
267 static struct unrhdr *aiod_unr;
269 static void aio_init_aioinfo(struct proc *p);
270 static void aio_onceonly(void);
271 static int aio_free_entry(struct aiocblist *aiocbe);
272 static void aio_process(struct aiocblist *aiocbe);
273 static int aio_newproc(int *);
274 static int aio_aqueue(struct thread *td, struct aiocb *job,
275 struct aioliojob *lio, int type, int osigev);
276 static void aio_physwakeup(struct buf *bp);
277 static void aio_proc_rundown(void *arg, struct proc *p);
278 static int aio_qphysio(struct proc *p, struct aiocblist *iocb);
279 static void biohelper(void *, int);
280 static void aio_daemon(void *param);
281 static void aio_swake_cb(struct socket *, struct sockbuf *);
282 static int aio_unload(void);
283 static int filt_aioattach(struct knote *kn);
284 static void filt_aiodetach(struct knote *kn);
285 static int filt_aio(struct knote *kn, long hint);
286 static int filt_lioattach(struct knote *kn);
287 static void filt_liodetach(struct knote *kn);
288 static int filt_lio(struct knote *kn, long hint);
291 static void aio_bio_done_notify( struct proc *userp, struct aiocblist *aiocbe, int type);
292 static int do_lio_listio(struct thread *td, struct lio_listio_args *uap,
297 * kaio Per process async io info
298 * aiop async io thread data
299 * aiocb async io jobs
300 * aiol list io job pointer - internal to aio_suspend XXX
301 * aiolio list io jobs
303 static uma_zone_t kaio_zone, aiop_zone, aiocb_zone, aiol_zone, aiolio_zone;
305 /* kqueue filters for aio */
306 static struct filterops aio_filtops =
307 { 0, filt_aioattach, filt_aiodetach, filt_aio };
308 static struct filterops lio_filtops =
309 { 0, filt_lioattach, filt_liodetach, filt_lio };
311 static eventhandler_tag exit_tag, exec_tag;
313 TASKQUEUE_DEFINE_THREAD(aiod_bio);
316 * Main operations function for use as a kernel module.
319 aio_modload(struct module *module, int cmd, void *arg)
328 error = aio_unload();
339 static moduledata_t aio_mod = {
345 SYSCALL_MODULE_HELPER(aio_return);
346 SYSCALL_MODULE_HELPER(aio_suspend);
347 SYSCALL_MODULE_HELPER(aio_cancel);
348 SYSCALL_MODULE_HELPER(aio_error);
349 SYSCALL_MODULE_HELPER(aio_read);
350 SYSCALL_MODULE_HELPER(aio_write);
351 SYSCALL_MODULE_HELPER(aio_waitcomplete);
352 SYSCALL_MODULE_HELPER(lio_listio);
353 SYSCALL_MODULE_HELPER(oaio_read);
354 SYSCALL_MODULE_HELPER(oaio_write);
355 SYSCALL_MODULE_HELPER(olio_listio);
357 DECLARE_MODULE(aio, aio_mod,
358 SI_SUB_VFS, SI_ORDER_ANY);
359 MODULE_VERSION(aio, 1);
362 * Startup initialization
368 /* XXX: should probably just use so->callback */
369 aio_swake = &aio_swake_cb;
370 exit_tag = EVENTHANDLER_REGISTER(process_exit, aio_proc_rundown, NULL,
371 EVENTHANDLER_PRI_ANY);
372 exec_tag = EVENTHANDLER_REGISTER(process_exec, aio_proc_rundown, NULL,
373 EVENTHANDLER_PRI_ANY);
374 kqueue_add_filteropts(EVFILT_AIO, &aio_filtops);
375 kqueue_add_filteropts(EVFILT_LIO, &lio_filtops);
376 TAILQ_INIT(&aio_freeproc);
377 sema_init(&aio_newproc_sem, 0, "aio_new_proc");
378 mtx_init(&aio_job_mtx, "aio_job", NULL, MTX_DEF);
379 mtx_init(&aio_sock_mtx, "aio_sock", NULL, MTX_DEF);
380 TAILQ_INIT(&aio_jobs);
381 aiod_unr = new_unrhdr(1, INT_MAX, NULL);
382 kaio_zone = uma_zcreate("AIO", sizeof(struct kaioinfo), NULL, NULL,
383 NULL, NULL, UMA_ALIGN_PTR, UMA_ZONE_NOFREE);
384 aiop_zone = uma_zcreate("AIOP", sizeof(struct aiothreadlist), NULL,
385 NULL, NULL, NULL, UMA_ALIGN_PTR, UMA_ZONE_NOFREE);
386 aiocb_zone = uma_zcreate("AIOCB", sizeof(struct aiocblist), NULL, NULL,
387 NULL, NULL, UMA_ALIGN_PTR, UMA_ZONE_NOFREE);
388 aiol_zone = uma_zcreate("AIOL", AIO_LISTIO_MAX*sizeof(intptr_t) , NULL,
389 NULL, NULL, NULL, UMA_ALIGN_PTR, UMA_ZONE_NOFREE);
390 aiolio_zone = uma_zcreate("AIOLIO", sizeof(struct aioliojob), NULL,
391 NULL, NULL, NULL, UMA_ALIGN_PTR, UMA_ZONE_NOFREE);
392 aiod_timeout = AIOD_TIMEOUT_DEFAULT;
393 aiod_lifetime = AIOD_LIFETIME_DEFAULT;
395 async_io_version = _POSIX_VERSION;
396 p31b_setcfg(CTL_P1003_1B_AIO_LISTIO_MAX, AIO_LISTIO_MAX);
397 p31b_setcfg(CTL_P1003_1B_AIO_MAX, MAX_AIO_QUEUE);
398 p31b_setcfg(CTL_P1003_1B_AIO_PRIO_DELTA_MAX, 0);
402 * Callback for unload of AIO when used as a module.
410 * XXX: no unloads by default, it's too dangerous.
411 * perhaps we could do it if locked out callers and then
412 * did an aio_proc_rundown() on each process.
414 * jhb: aio_proc_rundown() needs to run on curproc though,
415 * so I don't think that would fly.
420 error = kqueue_del_filteropts(EVFILT_AIO);
423 async_io_version = 0;
425 taskqueue_free(taskqueue_aiod_bio);
426 delete_unrhdr(aiod_unr);
427 EVENTHANDLER_DEREGISTER(process_exit, exit_tag);
428 EVENTHANDLER_DEREGISTER(process_exec, exec_tag);
429 mtx_destroy(&aio_job_mtx);
430 mtx_destroy(&aio_sock_mtx);
431 sema_destroy(&aio_newproc_sem);
432 p31b_setcfg(CTL_P1003_1B_AIO_LISTIO_MAX, -1);
433 p31b_setcfg(CTL_P1003_1B_AIO_MAX, -1);
434 p31b_setcfg(CTL_P1003_1B_AIO_PRIO_DELTA_MAX, -1);
439 * Init the per-process aioinfo structure. The aioinfo limits are set
440 * per-process for user limit (resource) management.
443 aio_init_aioinfo(struct proc *p)
447 ki = uma_zalloc(kaio_zone, M_WAITOK);
449 ki->kaio_maxactive_count = max_aio_per_proc;
450 ki->kaio_active_count = 0;
451 ki->kaio_qallowed_count = max_aio_queue_per_proc;
453 ki->kaio_ballowed_count = max_buf_aio;
454 ki->kaio_buffer_count = 0;
455 TAILQ_INIT(&ki->kaio_all);
456 TAILQ_INIT(&ki->kaio_done);
457 TAILQ_INIT(&ki->kaio_jobqueue);
458 TAILQ_INIT(&ki->kaio_bufqueue);
459 TAILQ_INIT(&ki->kaio_liojoblist);
460 TAILQ_INIT(&ki->kaio_sockqueue);
462 if (p->p_aioinfo == NULL) {
467 uma_zfree(kaio_zone, ki);
470 while (num_aio_procs < target_aio_procs)
475 aio_sendsig(struct proc *p, struct sigevent *sigev, ksiginfo_t *ksi)
477 PROC_LOCK_ASSERT(p, MA_OWNED);
479 ksi->ksi_code = SI_ASYNCIO;
480 ksi->ksi_flags |= KSI_EXT | KSI_INS;
481 return (psignal_event(p, sigev, ksi));
487 * Free a job entry. Wait for completion if it is currently active, but don't
488 * delay forever. If we delay, we return a flag that says that we have to
489 * restart the queue scan.
492 aio_free_entry(struct aiocblist *aiocbe)
495 struct aioliojob *lj;
498 p = aiocbe->userproc;
500 PROC_LOCK_ASSERT(p, MA_OWNED);
502 MPASS(aiocbe->jobstate == JOBST_JOBFINISHED);
507 atomic_subtract_int(&num_queue_count, 1);
510 MPASS(ki->kaio_count >= 0);
515 lj->lioj_finished_count--;
517 if (lj->lioj_count == 0) {
518 TAILQ_REMOVE(&ki->kaio_liojoblist, lj, lioj_list);
519 /* lio is going away, we need to destroy any knotes */
520 knlist_delete(&lj->klist, curthread, 1);
521 sigqueue_take(&lj->lioj_ksi);
522 uma_zfree(aiolio_zone, lj);
526 TAILQ_REMOVE(&ki->kaio_done, aiocbe, plist);
527 TAILQ_REMOVE(&ki->kaio_all, aiocbe, allist);
529 /* aiocbe is going away, we need to destroy any knotes */
530 knlist_delete(&aiocbe->klist, curthread, 1);
531 sigqueue_take(&aiocbe->ksi);
533 MPASS(aiocbe->bp == NULL);
534 aiocbe->jobstate = JOBST_NULL;
536 /* Wake up anyone who has interest to do cleanup work. */
537 if (ki->kaio_flags & (KAIO_WAKEUP | KAIO_RUNDOWN)) {
538 ki->kaio_flags &= ~KAIO_WAKEUP;
539 wakeup(&p->p_aioinfo);
544 * The thread argument here is used to find the owning process
545 * and is also passed to fo_close() which may pass it to various
546 * places such as devsw close() routines. Because of that, we
547 * need a thread pointer from the process owning the job that is
548 * persistent and won't disappear out from under us or move to
551 * Currently, all the callers of this function call it to remove
552 * an aiocblist from the current process' job list either via a
553 * syscall or due to the current process calling exit() or
554 * execve(). Thus, we know that p == curproc. We also know that
555 * curthread can't exit since we are curthread.
557 * Therefore, we use curthread as the thread to pass to
558 * knlist_delete(). This does mean that it is possible for the
559 * thread pointer at close time to differ from the thread pointer
560 * at open time, but this is already true of file descriptors in
561 * a multithreaded process.
563 fdrop(aiocbe->fd_file, curthread);
564 crfree(aiocbe->cred);
565 uma_zfree(aiocb_zone, aiocbe);
572 * Rundown the jobs for a given process.
575 aio_proc_rundown(void *arg, struct proc *p)
578 struct aioliojob *lj;
579 struct aiocblist *cbe, *cbn;
583 KASSERT(curthread->td_proc == p,
584 ("%s: called on non-curproc", __func__));
592 ki->kaio_flags |= KAIO_RUNDOWN;
595 * Try to cancel all pending requests. This code simulates
596 * aio_cancel on all pending I/O requests.
598 while ((cbe = TAILQ_FIRST(&ki->kaio_sockqueue))) {
601 mtx_lock(&aio_sock_mtx);
602 TAILQ_REMOVE(&so->so_aiojobq, cbe, list);
603 mtx_unlock(&aio_sock_mtx);
604 TAILQ_REMOVE(&ki->kaio_sockqueue, cbe, plist);
605 TAILQ_INSERT_HEAD(&ki->kaio_jobqueue, cbe, plist);
606 cbe->jobstate = JOBST_JOBQGLOBAL;
609 TAILQ_FOREACH_SAFE(cbe, &ki->kaio_jobqueue, plist, cbn) {
610 mtx_lock(&aio_job_mtx);
611 if (cbe->jobstate == JOBST_JOBQGLOBAL) {
612 TAILQ_REMOVE(&aio_jobs, cbe, list);
613 mtx_unlock(&aio_job_mtx);
614 cbe->jobstate = JOBST_JOBFINISHED;
615 cbe->uaiocb._aiocb_private.status = -1;
616 cbe->uaiocb._aiocb_private.error = ECANCELED;
617 TAILQ_REMOVE(&ki->kaio_jobqueue, cbe, plist);
618 aio_bio_done_notify(p, cbe, DONE_QUEUE);
620 mtx_unlock(&aio_job_mtx);
624 if (TAILQ_FIRST(&ki->kaio_sockqueue))
627 /* Wait for all running I/O to be finished */
628 if (TAILQ_FIRST(&ki->kaio_bufqueue) ||
629 TAILQ_FIRST(&ki->kaio_jobqueue)) {
630 ki->kaio_flags |= KAIO_WAKEUP;
631 msleep(&p->p_aioinfo, &p->p_mtx, PRIBIO, "aioprn", hz);
635 /* Free all completed I/O requests. */
636 while ((cbe = TAILQ_FIRST(&ki->kaio_done)) != NULL)
639 while ((lj = TAILQ_FIRST(&ki->kaio_liojoblist)) != NULL) {
640 if (lj->lioj_count == 0) {
641 TAILQ_REMOVE(&ki->kaio_liojoblist, lj, lioj_list);
642 knlist_delete(&lj->klist, curthread, 1);
643 sigqueue_take(&lj->lioj_ksi);
644 uma_zfree(aiolio_zone, lj);
646 panic("LIO job not cleaned up: C:%d, FC:%d\n",
647 lj->lioj_count, lj->lioj_finished_count);
651 uma_zfree(kaio_zone, ki);
657 * Select a job to run (called by an AIO daemon).
659 static struct aiocblist *
660 aio_selectjob(struct aiothreadlist *aiop)
662 struct aiocblist *aiocbe;
666 mtx_assert(&aio_job_mtx, MA_OWNED);
667 TAILQ_FOREACH(aiocbe, &aio_jobs, list) {
668 userp = aiocbe->userproc;
669 ki = userp->p_aioinfo;
671 if (ki->kaio_active_count < ki->kaio_maxactive_count) {
672 TAILQ_REMOVE(&aio_jobs, aiocbe, list);
673 /* Account for currently active jobs. */
674 ki->kaio_active_count++;
675 aiocbe->jobstate = JOBST_JOBRUNNING;
683 * The AIO processing activity. This is the code that does the I/O request for
684 * the non-physio version of the operations. The normal vn operations are used,
685 * and this code should work in all instances for every type of file, including
686 * pipes, sockets, fifos, and regular files.
688 * XXX I don't think these code work well with pipes, sockets and fifo, the
689 * problem is the aiod threads can be blocked if there is not data or no
690 * buffer space, and file was not opened with O_NONBLOCK, all aiod threads
691 * will be blocked if there is couple of such processes. We need a FOF_OFFSET
692 * like flag to override f_flag to tell low level system to do non-blocking
693 * I/O, we can not muck O_NONBLOCK because there is full of race between
694 * userland and aiod threads, although there is a trigger mechanism for socket,
695 * but it also does not work well if userland is misbehaviored.
698 aio_process(struct aiocblist *aiocbe)
700 struct ucred *td_savedcred;
710 int oublock_st, oublock_end;
711 int inblock_st, inblock_end;
714 td_savedcred = td->td_ucred;
715 td->td_ucred = aiocbe->cred;
717 cb = &aiocbe->uaiocb;
718 fp = aiocbe->fd_file;
720 aiov.iov_base = (void *)(uintptr_t)cb->aio_buf;
721 aiov.iov_len = cb->aio_nbytes;
723 auio.uio_iov = &aiov;
725 auio.uio_offset = cb->aio_offset;
726 auio.uio_resid = cb->aio_nbytes;
727 cnt = cb->aio_nbytes;
728 auio.uio_segflg = UIO_USERSPACE;
731 inblock_st = mycp->p_stats->p_ru.ru_inblock;
732 oublock_st = mycp->p_stats->p_ru.ru_oublock;
734 * aio_aqueue() acquires a reference to the file that is
735 * released in aio_free_entry().
737 if (cb->aio_lio_opcode == LIO_READ) {
738 auio.uio_rw = UIO_READ;
739 error = fo_read(fp, &auio, fp->f_cred, FOF_OFFSET, td);
741 auio.uio_rw = UIO_WRITE;
742 error = fo_write(fp, &auio, fp->f_cred, FOF_OFFSET, td);
744 inblock_end = mycp->p_stats->p_ru.ru_inblock;
745 oublock_end = mycp->p_stats->p_ru.ru_oublock;
747 aiocbe->inputcharge = inblock_end - inblock_st;
748 aiocbe->outputcharge = oublock_end - oublock_st;
750 if ((error) && (auio.uio_resid != cnt)) {
751 if (error == ERESTART || error == EINTR || error == EWOULDBLOCK)
753 if ((error == EPIPE) && (cb->aio_lio_opcode == LIO_WRITE)) {
755 if (fp->f_type == DTYPE_SOCKET) {
757 if (so->so_options & SO_NOSIGPIPE)
761 PROC_LOCK(aiocbe->userproc);
762 psignal(aiocbe->userproc, SIGPIPE);
763 PROC_UNLOCK(aiocbe->userproc);
768 cnt -= auio.uio_resid;
769 cb->_aiocb_private.error = error;
770 cb->_aiocb_private.status = cnt;
771 td->td_ucred = td_savedcred;
775 aio_bio_done_notify(struct proc *userp, struct aiocblist *aiocbe, int type)
777 struct aioliojob *lj;
781 PROC_LOCK_ASSERT(userp, MA_OWNED);
782 ki = userp->p_aioinfo;
786 lj->lioj_finished_count++;
787 if (lj->lioj_count == lj->lioj_finished_count)
790 if (type == DONE_QUEUE) {
791 aiocbe->jobflags |= AIOCBLIST_DONE;
793 aiocbe->jobflags |= AIOCBLIST_BUFDONE;
794 ki->kaio_buffer_count--;
796 TAILQ_INSERT_TAIL(&ki->kaio_done, aiocbe, plist);
797 aiocbe->jobstate = JOBST_JOBFINISHED;
798 if (aiocbe->uaiocb.aio_sigevent.sigev_notify == SIGEV_SIGNAL ||
799 aiocbe->uaiocb.aio_sigevent.sigev_notify == SIGEV_THREAD_ID)
800 aio_sendsig(userp, &aiocbe->uaiocb.aio_sigevent, &aiocbe->ksi);
802 KNOTE_LOCKED(&aiocbe->klist, 1);
805 if (lj->lioj_signal.sigev_notify == SIGEV_KEVENT) {
806 lj->lioj_flags |= LIOJ_KEVENT_POSTED;
807 KNOTE_LOCKED(&lj->klist, 1);
809 if ((lj->lioj_flags & (LIOJ_SIGNAL|LIOJ_SIGNAL_POSTED))
811 && (lj->lioj_signal.sigev_notify == SIGEV_SIGNAL ||
812 lj->lioj_signal.sigev_notify == SIGEV_THREAD_ID)) {
813 aio_sendsig(userp, &lj->lioj_signal, &lj->lioj_ksi);
814 lj->lioj_flags |= LIOJ_SIGNAL_POSTED;
817 if (ki->kaio_flags & (KAIO_RUNDOWN|KAIO_WAKEUP)) {
818 ki->kaio_flags &= ~KAIO_WAKEUP;
819 wakeup(&userp->p_aioinfo);
824 * The AIO daemon, most of the actual work is done in aio_process,
825 * but the setup (and address space mgmt) is done in this routine.
828 aio_daemon(void *_id)
830 struct aiocblist *aiocbe;
831 struct aiothreadlist *aiop;
833 struct proc *curcp, *mycp, *userp;
834 struct vmspace *myvm, *tmpvm;
835 struct thread *td = curthread;
836 struct pgrp *newpgrp;
837 struct session *newsess;
838 int id = (intptr_t)_id;
841 * Local copies of curproc (cp) and vmspace (myvm)
844 myvm = mycp->p_vmspace;
846 KASSERT(mycp->p_textvp == NULL, ("kthread has a textvp"));
849 * Allocate and ready the aio control info. There is one aiop structure
852 aiop = uma_zalloc(aiop_zone, M_WAITOK);
853 aiop->aiothread = td;
854 aiop->aiothreadflags |= AIOP_FREE;
857 * Place thread (lightweight process) onto the AIO free thread list.
859 mtx_lock(&aio_job_mtx);
860 TAILQ_INSERT_HEAD(&aio_freeproc, aiop, list);
861 mtx_unlock(&aio_job_mtx);
864 * Get rid of our current filedescriptors. AIOD's don't need any
865 * filedescriptors, except as temporarily inherited from the client.
869 /* The daemon resides in its own pgrp. */
870 MALLOC(newpgrp, struct pgrp *, sizeof(struct pgrp), M_PGRP,
872 MALLOC(newsess, struct session *, sizeof(struct session), M_SESSION,
875 sx_xlock(&proctree_lock);
876 enterpgrp(mycp, mycp->p_pid, newpgrp, newsess);
877 sx_xunlock(&proctree_lock);
880 * Wakeup parent process. (Parent sleeps to keep from blasting away
881 * and creating too many daemons.)
883 sema_post(&aio_newproc_sem);
885 mtx_lock(&aio_job_mtx);
888 * curcp is the current daemon process context.
889 * userp is the current user process context.
894 * Take daemon off of free queue
896 if (aiop->aiothreadflags & AIOP_FREE) {
897 TAILQ_REMOVE(&aio_freeproc, aiop, list);
898 aiop->aiothreadflags &= ~AIOP_FREE;
904 while ((aiocbe = aio_selectjob(aiop)) != NULL) {
905 mtx_unlock(&aio_job_mtx);
906 userp = aiocbe->userproc;
909 * Connect to process address space for user program.
911 if (userp != curcp) {
913 * Save the current address space that we are
916 tmpvm = mycp->p_vmspace;
919 * Point to the new user address space, and
922 mycp->p_vmspace = userp->p_vmspace;
923 atomic_add_int(&mycp->p_vmspace->vm_refcnt, 1);
925 /* Activate the new mapping. */
926 pmap_activate(FIRST_THREAD_IN_PROC(mycp));
929 * If the old address space wasn't the daemons
930 * own address space, then we need to remove the
931 * daemon's reference from the other process
932 * that it was acting on behalf of.
940 ki = userp->p_aioinfo;
942 /* Do the I/O function. */
945 mtx_lock(&aio_job_mtx);
946 /* Decrement the active job count. */
947 ki->kaio_active_count--;
948 mtx_unlock(&aio_job_mtx);
951 TAILQ_REMOVE(&ki->kaio_jobqueue, aiocbe, plist);
952 aio_bio_done_notify(userp, aiocbe, DONE_QUEUE);
953 if (aiocbe->jobflags & AIOCBLIST_RUNDOWN) {
955 aiocbe->jobflags &= ~AIOCBLIST_RUNDOWN;
959 mtx_lock(&aio_job_mtx);
963 * Disconnect from user address space.
967 mtx_unlock(&aio_job_mtx);
969 /* Get the user address space to disconnect from. */
970 tmpvm = mycp->p_vmspace;
972 /* Get original address space for daemon. */
973 mycp->p_vmspace = myvm;
975 /* Activate the daemon's address space. */
976 pmap_activate(FIRST_THREAD_IN_PROC(mycp));
979 printf("AIOD: vmspace problem -- %d\n",
983 /* Remove our vmspace reference. */
988 mtx_lock(&aio_job_mtx);
990 * We have to restart to avoid race, we only sleep if
991 * no job can be selected, that should be
997 mtx_assert(&aio_job_mtx, MA_OWNED);
999 TAILQ_INSERT_HEAD(&aio_freeproc, aiop, list);
1000 aiop->aiothreadflags |= AIOP_FREE;
1003 * If daemon is inactive for a long time, allow it to exit,
1004 * thereby freeing resources.
1006 if (msleep(aiop->aiothread, &aio_job_mtx, PRIBIO, "aiordy",
1008 if (TAILQ_EMPTY(&aio_jobs)) {
1009 if ((aiop->aiothreadflags & AIOP_FREE) &&
1010 (num_aio_procs > target_aio_procs)) {
1011 TAILQ_REMOVE(&aio_freeproc, aiop, list);
1013 mtx_unlock(&aio_job_mtx);
1014 uma_zfree(aiop_zone, aiop);
1015 free_unr(aiod_unr, id);
1017 if (mycp->p_vmspace->vm_refcnt <= 1) {
1018 printf("AIOD: bad vm refcnt for"
1019 " exiting daemon: %d\n",
1020 mycp->p_vmspace->vm_refcnt);
1028 mtx_unlock(&aio_job_mtx);
1029 panic("shouldn't be here\n");
1033 * Create a new AIO daemon. This is mostly a kernel-thread fork routine. The
1034 * AIO daemon modifies its environment itself.
1037 aio_newproc(int *start)
1043 id = alloc_unr(aiod_unr);
1044 error = kthread_create(aio_daemon, (void *)(intptr_t)id, &p,
1045 RFNOWAIT, 0, "aiod%d", id);
1048 * Wait until daemon is started.
1050 sema_wait(&aio_newproc_sem);
1051 mtx_lock(&aio_job_mtx);
1055 mtx_unlock(&aio_job_mtx);
1057 free_unr(aiod_unr, id);
1063 * Try the high-performance, low-overhead physio method for eligible
1064 * VCHR devices. This method doesn't use an aio helper thread, and
1065 * thus has very low overhead.
1067 * Assumes that the caller, aio_aqueue(), has incremented the file
1068 * structure's reference count, preventing its deallocation for the
1069 * duration of this call.
1072 aio_qphysio(struct proc *p, struct aiocblist *aiocbe)
1078 struct kaioinfo *ki;
1079 struct aioliojob *lj;
1082 cb = &aiocbe->uaiocb;
1083 fp = aiocbe->fd_file;
1085 if (fp->f_type != DTYPE_VNODE)
1091 * If its not a disk, we don't want to return a positive error.
1092 * It causes the aio code to not fall through to try the thread
1093 * way when you're talking to a regular file.
1095 if (!vn_isdisk(vp, &error)) {
1096 if (error == ENOTBLK)
1102 if (cb->aio_nbytes % vp->v_bufobj.bo_bsize)
1105 if (cb->aio_nbytes > vp->v_rdev->si_iosize_max)
1108 if (cb->aio_nbytes >
1109 MAXPHYS - (((vm_offset_t) cb->aio_buf) & PAGE_MASK))
1113 if (ki->kaio_buffer_count >= ki->kaio_ballowed_count)
1116 /* Create and build a buffer header for a transfer. */
1117 bp = (struct buf *)getpbuf(NULL);
1122 ki->kaio_buffer_count++;
1129 * Get a copy of the kva from the physical buffer.
1133 bp->b_bcount = cb->aio_nbytes;
1134 bp->b_bufsize = cb->aio_nbytes;
1135 bp->b_iodone = aio_physwakeup;
1136 bp->b_saveaddr = bp->b_data;
1137 bp->b_data = (void *)(uintptr_t)cb->aio_buf;
1138 bp->b_offset = cb->aio_offset;
1139 bp->b_iooffset = cb->aio_offset;
1140 bp->b_blkno = btodb(cb->aio_offset);
1141 bp->b_iocmd = cb->aio_lio_opcode == LIO_WRITE ? BIO_WRITE : BIO_READ;
1144 * Bring buffer into kernel space.
1146 if (vmapbuf(bp) < 0) {
1153 bp->b_caller1 = (void *)aiocbe;
1154 TAILQ_INSERT_TAIL(&ki->kaio_bufqueue, aiocbe, plist);
1155 TAILQ_INSERT_TAIL(&ki->kaio_all, aiocbe, allist);
1156 aiocbe->jobstate = JOBST_JOBQBUF;
1157 cb->_aiocb_private.status = cb->aio_nbytes;
1160 atomic_add_int(&num_queue_count, 1);
1161 atomic_add_int(&num_buf_aio, 1);
1165 TASK_INIT(&aiocbe->biotask, 0, biohelper, aiocbe);
1167 /* Perform transfer. */
1168 dev_strategy(vp->v_rdev, bp);
1174 ki->kaio_buffer_count--;
1184 * Wake up aio requests that may be serviceable now.
1187 aio_swake_cb(struct socket *so, struct sockbuf *sb)
1189 struct aiocblist *cb, *cbn;
1191 struct kaioinfo *ki = NULL;
1192 int opcode, wakecount = 0;
1193 struct aiothreadlist *aiop;
1195 if (sb == &so->so_snd) {
1197 SOCKBUF_LOCK(&so->so_snd);
1198 so->so_snd.sb_flags &= ~SB_AIO;
1199 SOCKBUF_UNLOCK(&so->so_snd);
1202 SOCKBUF_LOCK(&so->so_rcv);
1203 so->so_rcv.sb_flags &= ~SB_AIO;
1204 SOCKBUF_UNLOCK(&so->so_rcv);
1207 mtx_lock(&aio_sock_mtx);
1208 TAILQ_FOREACH_SAFE(cb, &so->so_aiojobq, list, cbn) {
1209 if (opcode == cb->uaiocb.aio_lio_opcode) {
1210 if (cb->jobstate != JOBST_JOBQSOCK)
1211 panic("invalid queue value");
1214 TAILQ_REMOVE(&so->so_aiojobq, cb, list);
1216 TAILQ_REMOVE(&ki->kaio_sockqueue, cb, plist);
1218 * XXX check AIO_RUNDOWN, and don't put on
1219 * jobqueue if it was set.
1221 TAILQ_INSERT_TAIL(&ki->kaio_jobqueue, cb, plist);
1222 cb->jobstate = JOBST_JOBQGLOBAL;
1223 mtx_lock(&aio_job_mtx);
1224 TAILQ_INSERT_TAIL(&aio_jobs, cb, list);
1225 mtx_unlock(&aio_job_mtx);
1230 mtx_unlock(&aio_sock_mtx);
1232 while (wakecount--) {
1233 mtx_lock(&aio_job_mtx);
1234 if ((aiop = TAILQ_FIRST(&aio_freeproc)) != NULL) {
1235 TAILQ_REMOVE(&aio_freeproc, aiop, list);
1236 aiop->aiothreadflags &= ~AIOP_FREE;
1237 wakeup(aiop->aiothread);
1239 mtx_unlock(&aio_job_mtx);
1244 * Queue a new AIO request. Choosing either the threaded or direct physio VCHR
1245 * technique is done in this code.
1248 aio_aqueue(struct thread *td, struct aiocb *job, struct aioliojob *lj,
1249 int type, int oldsigev)
1251 struct proc *p = td->td_proc;
1254 struct aiocblist *aiocbe;
1255 struct aiothreadlist *aiop;
1256 struct kaioinfo *ki;
1266 if (p->p_aioinfo == NULL)
1267 aio_init_aioinfo(p);
1271 suword(&job->_aiocb_private.status, -1);
1272 suword(&job->_aiocb_private.error, 0);
1273 suword(&job->_aiocb_private.kernelinfo, -1);
1275 if (num_queue_count >= max_queue_count ||
1276 ki->kaio_count >= ki->kaio_qallowed_count) {
1277 suword(&job->_aiocb_private.error, EAGAIN);
1281 aiocbe = uma_zalloc(aiocb_zone, M_WAITOK | M_ZERO);
1282 aiocbe->inputcharge = 0;
1283 aiocbe->outputcharge = 0;
1284 knlist_init(&aiocbe->klist, &p->p_mtx, NULL, NULL, NULL);
1287 bzero(&aiocbe->uaiocb, sizeof(struct aiocb));
1288 error = copyin(job, &aiocbe->uaiocb, sizeof(struct oaiocb));
1289 bcopy(&aiocbe->uaiocb.__spare__, &aiocbe->uaiocb.aio_sigevent,
1290 sizeof(struct osigevent));
1292 error = copyin(job, &aiocbe->uaiocb, sizeof(struct aiocb));
1295 suword(&job->_aiocb_private.error, error);
1296 uma_zfree(aiocb_zone, aiocbe);
1300 if (aiocbe->uaiocb.aio_sigevent.sigev_notify != SIGEV_KEVENT &&
1301 aiocbe->uaiocb.aio_sigevent.sigev_notify != SIGEV_SIGNAL &&
1302 aiocbe->uaiocb.aio_sigevent.sigev_notify != SIGEV_THREAD_ID &&
1303 aiocbe->uaiocb.aio_sigevent.sigev_notify != SIGEV_NONE) {
1304 suword(&job->_aiocb_private.error, EINVAL);
1305 uma_zfree(aiocb_zone, aiocbe);
1309 if ((aiocbe->uaiocb.aio_sigevent.sigev_notify == SIGEV_SIGNAL ||
1310 aiocbe->uaiocb.aio_sigevent.sigev_notify == SIGEV_THREAD_ID) &&
1311 !_SIG_VALID(aiocbe->uaiocb.aio_sigevent.sigev_signo)) {
1312 uma_zfree(aiocb_zone, aiocbe);
1316 ksiginfo_init(&aiocbe->ksi);
1318 /* Save userspace address of the job info. */
1319 aiocbe->uuaiocb = job;
1321 /* Get the opcode. */
1322 if (type != LIO_NOP)
1323 aiocbe->uaiocb.aio_lio_opcode = type;
1324 opcode = aiocbe->uaiocb.aio_lio_opcode;
1326 /* Fetch the file object for the specified file descriptor. */
1327 fd = aiocbe->uaiocb.aio_fildes;
1330 error = fget_write(td, fd, &fp);
1333 error = fget_read(td, fd, &fp);
1336 error = fget(td, fd, &fp);
1339 uma_zfree(aiocb_zone, aiocbe);
1340 suword(&job->_aiocb_private.error, EBADF);
1343 aiocbe->fd_file = fp;
1345 if (aiocbe->uaiocb.aio_offset == -1LL) {
1350 mtx_lock(&aio_job_mtx);
1352 if (jobrefid == LONG_MAX)
1356 mtx_unlock(&aio_job_mtx);
1358 error = suword(&job->_aiocb_private.kernelinfo, jid);
1363 aiocbe->uaiocb._aiocb_private.kernelinfo = (void *)(intptr_t)jid;
1365 if (opcode == LIO_NOP) {
1367 uma_zfree(aiocb_zone, aiocbe);
1370 if ((opcode != LIO_READ) && (opcode != LIO_WRITE)) {
1375 if (aiocbe->uaiocb.aio_sigevent.sigev_notify == SIGEV_KEVENT) {
1376 kev.ident = aiocbe->uaiocb.aio_sigevent.sigev_notify_kqueue;
1379 error = fget(td, (u_int)kev.ident, &kq_fp);
1382 if (kq_fp->f_type != DTYPE_KQUEUE) {
1388 kev.ident = (uintptr_t)aiocbe->uuaiocb;
1389 kev.filter = EVFILT_AIO;
1390 kev.flags = EV_ADD | EV_ENABLE | EV_FLAG1;
1391 kev.data = (intptr_t)aiocbe;
1392 kev.udata = aiocbe->uaiocb.aio_sigevent.sigev_value.sival_ptr;
1393 error = kqueue_register(kq, &kev, td, 1);
1398 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;
1411 if (fp->f_type == DTYPE_SOCKET) {
1413 * Alternate queueing for socket ops: Reach down into the
1414 * descriptor to get the socket data. Then check to see if the
1415 * socket is ready to be read or written (based on the requested
1418 * If it is not ready for io, then queue the aiocbe on the
1419 * socket, and set the flags so we get a call when sbnotify()
1422 * Note if opcode is neither LIO_WRITE nor LIO_READ we lock
1423 * and unlock the snd sockbuf for no reason.
1426 sb = (opcode == LIO_READ) ? &so->so_rcv : &so->so_snd;
1428 if (((opcode == LIO_READ) && (!soreadable(so))) || ((opcode ==
1429 LIO_WRITE) && (!sowriteable(so)))) {
1430 mtx_lock(&aio_sock_mtx);
1431 TAILQ_INSERT_TAIL(&so->so_aiojobq, aiocbe, list);
1432 mtx_unlock(&aio_sock_mtx);
1434 sb->sb_flags |= SB_AIO;
1436 TAILQ_INSERT_TAIL(&ki->kaio_sockqueue, aiocbe, plist);
1437 TAILQ_INSERT_TAIL(&ki->kaio_all, aiocbe, allist);
1438 aiocbe->jobstate = JOBST_JOBQSOCK;
1444 atomic_add_int(&num_queue_count, 1);
1451 if ((error = aio_qphysio(p, aiocbe)) == 0)
1455 aiocbe->uaiocb._aiocb_private.error = error;
1456 suword(&job->_aiocb_private.error, error);
1460 /* No buffer for daemon I/O. */
1467 TAILQ_INSERT_TAIL(&ki->kaio_jobqueue, aiocbe, plist);
1468 TAILQ_INSERT_TAIL(&ki->kaio_all, aiocbe, allist);
1470 mtx_lock(&aio_job_mtx);
1471 TAILQ_INSERT_TAIL(&aio_jobs, aiocbe, list);
1472 aiocbe->jobstate = JOBST_JOBQGLOBAL;
1475 atomic_add_int(&num_queue_count, 1);
1478 * If we don't have a free AIO process, and we are below our quota, then
1479 * start one. Otherwise, depend on the subsequent I/O completions to
1480 * pick-up this job. If we don't sucessfully create the new process
1481 * (thread) due to resource issues, we return an error for now (EAGAIN),
1482 * which is likely not the correct thing to do.
1486 if ((aiop = TAILQ_FIRST(&aio_freeproc)) != NULL) {
1487 TAILQ_REMOVE(&aio_freeproc, aiop, list);
1488 aiop->aiothreadflags &= ~AIOP_FREE;
1489 wakeup(aiop->aiothread);
1490 } else if (((num_aio_resv_start + num_aio_procs) < max_aio_procs) &&
1491 ((ki->kaio_active_count + num_aio_resv_start) <
1492 ki->kaio_maxactive_count)) {
1493 num_aio_resv_start++;
1494 mtx_unlock(&aio_job_mtx);
1495 error = aio_newproc(&num_aio_resv_start);
1496 mtx_lock(&aio_job_mtx);
1498 num_aio_resv_start--;
1502 mtx_unlock(&aio_job_mtx);
1509 * Support the aio_return system call, as a side-effect, kernel resources are
1513 aio_return(struct thread *td, struct aio_return_args *uap)
1515 struct proc *p = td->td_proc;
1516 struct aiocblist *cb;
1517 struct aiocb *uaiocb;
1518 struct kaioinfo *ki;
1524 uaiocb = uap->aiocbp;
1526 TAILQ_FOREACH(cb, &ki->kaio_done, plist) {
1527 if (cb->uuaiocb == uaiocb)
1531 MPASS(cb->jobstate == JOBST_JOBFINISHED);
1532 status = cb->uaiocb._aiocb_private.status;
1533 error = cb->uaiocb._aiocb_private.error;
1534 td->td_retval[0] = status;
1535 if (cb->uaiocb.aio_lio_opcode == LIO_WRITE) {
1536 p->p_stats->p_ru.ru_oublock +=
1538 cb->outputcharge = 0;
1539 } else if (cb->uaiocb.aio_lio_opcode == LIO_READ) {
1540 p->p_stats->p_ru.ru_inblock += cb->inputcharge;
1541 cb->inputcharge = 0;
1544 suword(&uaiocb->_aiocb_private.error, error);
1545 suword(&uaiocb->_aiocb_private.status, status);
1554 * Allow a process to wakeup when any of the I/O requests are completed.
1557 aio_suspend(struct thread *td, struct aio_suspend_args *uap)
1559 struct proc *p = td->td_proc;
1562 struct aiocb *const *cbptr, *cbp;
1563 struct kaioinfo *ki;
1564 struct aiocblist *cb, *cbfirst;
1565 struct aiocb **ujoblist;
1571 if (uap->nent < 0 || uap->nent > AIO_LISTIO_MAX)
1576 /* Get timespec struct. */
1577 if ((error = copyin(uap->timeout, &ts, sizeof(ts))) != 0)
1580 if (ts.tv_nsec < 0 || ts.tv_nsec >= 1000000000)
1583 TIMESPEC_TO_TIMEVAL(&atv, &ts);
1584 if (itimerfix(&atv))
1586 timo = tvtohz(&atv);
1594 ujoblist = uma_zalloc(aiol_zone, M_WAITOK);
1595 cbptr = uap->aiocbp;
1597 for (i = 0; i < uap->nent; i++) {
1598 cbp = (struct aiocb *)(intptr_t)fuword(&cbptr[i]);
1601 ujoblist[njoblist] = cbp;
1605 if (njoblist == 0) {
1606 uma_zfree(aiol_zone, ujoblist);
1614 TAILQ_FOREACH(cb, &ki->kaio_all, allist) {
1615 for (i = 0; i < njoblist; i++) {
1616 if (cb->uuaiocb == ujoblist[i]) {
1617 if (cbfirst == NULL)
1619 if (cb->jobstate == JOBST_JOBFINISHED)
1624 /* All tasks were finished. */
1625 if (cbfirst == NULL)
1628 ki->kaio_flags |= KAIO_WAKEUP;
1629 error = msleep(&p->p_aioinfo, &p->p_mtx, PRIBIO | PCATCH,
1631 if (error == ERESTART)
1638 uma_zfree(aiol_zone, ujoblist);
1643 * aio_cancel cancels any non-physio aio operations not currently in
1647 aio_cancel(struct thread *td, struct aio_cancel_args *uap)
1649 struct proc *p = td->td_proc;
1650 struct kaioinfo *ki;
1651 struct aiocblist *cbe, *cbn;
1656 int notcancelled = 0;
1659 /* Lookup file object. */
1660 error = fget(td, uap->fd, &fp);
1668 if (fp->f_type == DTYPE_VNODE) {
1670 if (vn_isdisk(vp, &error)) {
1672 td->td_retval[0] = AIO_NOTCANCELED;
1675 } else if (fp->f_type == DTYPE_SOCKET) {
1677 mtx_lock(&aio_sock_mtx);
1678 TAILQ_FOREACH_SAFE(cbe, &so->so_aiojobq, list, cbn) {
1679 if (cbe->userproc == p &&
1680 (uap->aiocbp == NULL ||
1681 uap->aiocbp == cbe->uuaiocb)) {
1682 TAILQ_REMOVE(&so->so_aiojobq, cbe, list);
1684 TAILQ_REMOVE(&ki->kaio_sockqueue, cbe, plist);
1685 cbe->jobstate = JOBST_JOBRUNNING;
1686 cbe->uaiocb._aiocb_private.status = -1;
1687 cbe->uaiocb._aiocb_private.error = ECANCELED;
1688 aio_bio_done_notify(p, cbe, DONE_QUEUE);
1691 if (uap->aiocbp != NULL)
1695 mtx_unlock(&aio_sock_mtx);
1696 if (cancelled && uap->aiocbp != NULL) {
1698 td->td_retval[0] = AIO_CANCELED;
1704 TAILQ_FOREACH_SAFE(cbe, &ki->kaio_jobqueue, plist, cbn) {
1705 if ((uap->fd == cbe->uaiocb.aio_fildes) &&
1706 ((uap->aiocbp == NULL) ||
1707 (uap->aiocbp == cbe->uuaiocb))) {
1708 mtx_lock(&aio_job_mtx);
1709 if (cbe->jobstate == JOBST_JOBQGLOBAL) {
1710 TAILQ_REMOVE(&aio_jobs, cbe, list);
1711 mtx_unlock(&aio_job_mtx);
1712 TAILQ_REMOVE(&ki->kaio_jobqueue, cbe, plist);
1713 cbe->uaiocb._aiocb_private.status = -1;
1714 cbe->uaiocb._aiocb_private.error = ECANCELED;
1715 aio_bio_done_notify(p, cbe, DONE_QUEUE);
1718 mtx_unlock(&aio_job_mtx);
1728 td->td_retval[0] = AIO_NOTCANCELED;
1732 td->td_retval[0] = AIO_CANCELED;
1735 td->td_retval[0] = AIO_ALLDONE;
1741 * aio_error is implemented in the kernel level for compatibility purposes only.
1742 * For a user mode async implementation, it would be best to do it in a userland
1746 aio_error(struct thread *td, struct aio_error_args *uap)
1748 struct proc *p = td->td_proc;
1749 struct aiocblist *cb;
1750 struct kaioinfo *ki;
1755 td->td_retval[0] = EINVAL;
1760 TAILQ_FOREACH(cb, &ki->kaio_all, allist) {
1761 if (cb->uuaiocb == uap->aiocbp) {
1762 if (cb->jobstate == JOBST_JOBFINISHED)
1764 cb->uaiocb._aiocb_private.error;
1766 td->td_retval[0] = EINPROGRESS;
1774 * Hack for failure of aio_aqueue.
1776 status = fuword(&uap->aiocbp->_aiocb_private.status);
1778 td->td_retval[0] = fuword(&uap->aiocbp->_aiocb_private.error);
1782 td->td_retval[0] = EINVAL;
1786 /* syscall - asynchronous read from a file (REALTIME) */
1788 oaio_read(struct thread *td, struct oaio_read_args *uap)
1791 return aio_aqueue(td, (struct aiocb *)uap->aiocbp, NULL, LIO_READ, 1);
1795 aio_read(struct thread *td, struct aio_read_args *uap)
1798 return aio_aqueue(td, uap->aiocbp, NULL, LIO_READ, 0);
1801 /* syscall - asynchronous write to a file (REALTIME) */
1803 oaio_write(struct thread *td, struct oaio_write_args *uap)
1806 return aio_aqueue(td, (struct aiocb *)uap->aiocbp, NULL, LIO_WRITE, 1);
1810 aio_write(struct thread *td, struct aio_write_args *uap)
1813 return aio_aqueue(td, uap->aiocbp, NULL, LIO_WRITE, 0);
1816 /* syscall - list directed I/O (REALTIME) */
1818 olio_listio(struct thread *td, struct olio_listio_args *uap)
1820 return do_lio_listio(td, (struct lio_listio_args *)uap, 1);
1823 /* syscall - list directed I/O (REALTIME) */
1825 lio_listio(struct thread *td, struct lio_listio_args *uap)
1827 return do_lio_listio(td, uap, 0);
1831 do_lio_listio(struct thread *td, struct lio_listio_args *uap, int oldsigev)
1833 struct proc *p = td->td_proc;
1834 struct aiocb *iocb, * const *cbptr;
1835 struct kaioinfo *ki;
1836 struct aioliojob *lj;
1845 if ((uap->mode != LIO_NOWAIT) && (uap->mode != LIO_WAIT))
1849 if (nent < 0 || nent > AIO_LISTIO_MAX)
1852 if (p->p_aioinfo == NULL)
1853 aio_init_aioinfo(p);
1857 lj = uma_zalloc(aiolio_zone, M_WAITOK);
1860 lj->lioj_finished_count = 0;
1861 knlist_init(&lj->klist, &p->p_mtx, NULL, NULL, NULL);
1862 ksiginfo_init(&lj->lioj_ksi);
1867 if (uap->sig && (uap->mode == LIO_NOWAIT)) {
1868 bzero(&lj->lioj_signal, sizeof(&lj->lioj_signal));
1869 error = copyin(uap->sig, &lj->lioj_signal,
1870 oldsigev ? sizeof(struct osigevent) :
1871 sizeof(struct sigevent));
1873 uma_zfree(aiolio_zone, lj);
1877 if (lj->lioj_signal.sigev_notify == SIGEV_KEVENT) {
1878 /* Assume only new style KEVENT */
1879 error = fget(td, lj->lioj_signal.sigev_notify_kqueue,
1882 uma_zfree(aiolio_zone, lj);
1885 if (kq_fp->f_type != DTYPE_KQUEUE) {
1887 uma_zfree(aiolio_zone, lj);
1890 kq = (struct kqueue *)kq_fp->f_data;
1891 kev.filter = EVFILT_LIO;
1892 kev.flags = EV_ADD | EV_ENABLE | EV_FLAG1;
1893 kev.ident = (uintptr_t)lj; /* something unique */
1894 kev.data = (intptr_t)lj;
1895 /* pass user defined sigval data */
1896 kev.udata = lj->lioj_signal.sigev_value.sival_ptr;
1897 error = kqueue_register(kq, &kev, td, 1);
1900 uma_zfree(aiolio_zone, lj);
1903 } else if (lj->lioj_signal.sigev_notify == SIGEV_NONE) {
1905 } else if (lj->lioj_signal.sigev_notify == SIGEV_SIGNAL ||
1906 lj->lioj_signal.sigev_notify == SIGEV_THREAD_ID) {
1907 if (!_SIG_VALID(lj->lioj_signal.sigev_signo)) {
1908 uma_zfree(aiolio_zone, lj);
1911 lj->lioj_flags |= LIOJ_SIGNAL;
1913 uma_zfree(aiolio_zone, lj);
1919 TAILQ_INSERT_TAIL(&ki->kaio_liojoblist, lj, lioj_list);
1921 * Add extra aiocb count to avoid the lio to be freed
1922 * by other threads doing aio_waitcomplete or aio_return,
1923 * and prevent event from being sent until we have queued
1930 * Get pointers to the list of I/O requests.
1933 cbptr = uap->acb_list;
1934 for (i = 0; i < uap->nent; i++) {
1935 iocb = (struct aiocb *)(intptr_t)fuword(&cbptr[i]);
1936 if (((intptr_t)iocb != -1) && ((intptr_t)iocb != 0)) {
1937 error = aio_aqueue(td, iocb, lj, 0, oldsigev);
1945 if (uap->mode == LIO_WAIT) {
1946 while (lj->lioj_count - 1 != lj->lioj_finished_count) {
1947 ki->kaio_flags |= KAIO_WAKEUP;
1948 error = msleep(&p->p_aioinfo, &p->p_mtx,
1949 PRIBIO | PCATCH, "aiospn", 0);
1950 if (error == ERESTART)
1956 if (lj->lioj_count - 1 == lj->lioj_finished_count) {
1957 if (lj->lioj_signal.sigev_notify == SIGEV_KEVENT) {
1958 lj->lioj_flags |= LIOJ_KEVENT_POSTED;
1959 KNOTE_LOCKED(&lj->klist, 1);
1961 if ((lj->lioj_flags & (LIOJ_SIGNAL|LIOJ_SIGNAL_POSTED))
1963 && (lj->lioj_signal.sigev_notify == SIGEV_SIGNAL ||
1964 lj->lioj_signal.sigev_notify == SIGEV_THREAD_ID)) {
1965 aio_sendsig(p, &lj->lioj_signal,
1967 lj->lioj_flags |= LIOJ_SIGNAL_POSTED;
1972 if (lj->lioj_count == 0) {
1973 TAILQ_REMOVE(&ki->kaio_liojoblist, lj, lioj_list);
1974 knlist_delete(&lj->klist, curthread, 1);
1975 sigqueue_take(&lj->lioj_ksi);
1977 uma_zfree(aiolio_zone, lj);
1987 * Called from interrupt thread for physio, we should return as fast
1988 * as possible, so we schedule a biohelper task.
1991 aio_physwakeup(struct buf *bp)
1993 struct aiocblist *aiocbe;
1995 aiocbe = (struct aiocblist *)bp->b_caller1;
1996 taskqueue_enqueue(taskqueue_aiod_bio, &aiocbe->biotask);
2000 * Task routine to perform heavy tasks, process wakeup, and signals.
2003 biohelper(void *context, int pending)
2005 struct aiocblist *aiocbe = context;
2011 userp = aiocbe->userproc;
2013 aiocbe->uaiocb._aiocb_private.status -= bp->b_resid;
2014 aiocbe->uaiocb._aiocb_private.error = 0;
2015 if (bp->b_ioflags & BIO_ERROR)
2016 aiocbe->uaiocb._aiocb_private.error = bp->b_error;
2017 nblks = btodb(aiocbe->uaiocb.aio_nbytes);
2018 if (aiocbe->uaiocb.aio_lio_opcode == LIO_WRITE)
2019 aiocbe->outputcharge += nblks;
2021 aiocbe->inputcharge += nblks;
2023 TAILQ_REMOVE(&userp->p_aioinfo->kaio_bufqueue, aiocbe, plist);
2024 aio_bio_done_notify(userp, aiocbe, DONE_BUF);
2027 /* Release mapping into kernel space. */
2030 atomic_subtract_int(&num_buf_aio, 1);
2033 /* syscall - wait for the next completion of an aio request */
2035 aio_waitcomplete(struct thread *td, struct aio_waitcomplete_args *uap)
2037 struct proc *p = td->td_proc;
2040 struct kaioinfo *ki;
2041 struct aiocblist *cb;
2042 struct aiocb *uuaiocb;
2043 int error, status, timo;
2045 suword(uap->aiocbp, (long)NULL);
2049 /* Get timespec struct. */
2050 error = copyin(uap->timeout, &ts, sizeof(ts));
2054 if ((ts.tv_nsec < 0) || (ts.tv_nsec >= 1000000000))
2057 TIMESPEC_TO_TIMEVAL(&atv, &ts);
2058 if (itimerfix(&atv))
2060 timo = tvtohz(&atv);
2063 if (p->p_aioinfo == NULL)
2064 aio_init_aioinfo(p);
2070 while ((cb = TAILQ_FIRST(&ki->kaio_done)) == NULL) {
2071 ki->kaio_flags |= KAIO_WAKEUP;
2072 error = msleep(&p->p_aioinfo, &p->p_mtx, PRIBIO | PCATCH,
2074 if (error == ERESTART)
2081 MPASS(cb->jobstate == JOBST_JOBFINISHED);
2082 uuaiocb = cb->uuaiocb;
2083 status = cb->uaiocb._aiocb_private.status;
2084 error = cb->uaiocb._aiocb_private.error;
2085 td->td_retval[0] = status;
2086 if (cb->uaiocb.aio_lio_opcode == LIO_WRITE) {
2087 p->p_stats->p_ru.ru_oublock += cb->outputcharge;
2088 cb->outputcharge = 0;
2089 } else if (cb->uaiocb.aio_lio_opcode == LIO_READ) {
2090 p->p_stats->p_ru.ru_inblock += cb->inputcharge;
2091 cb->inputcharge = 0;
2095 suword(uap->aiocbp, (long)uuaiocb);
2096 suword(&uuaiocb->_aiocb_private.error, error);
2097 suword(&uuaiocb->_aiocb_private.status, status);
2104 /* kqueue attach function */
2106 filt_aioattach(struct knote *kn)
2108 struct aiocblist *aiocbe = (struct aiocblist *)kn->kn_sdata;
2111 * The aiocbe pointer must be validated before using it, so
2112 * registration is restricted to the kernel; the user cannot
2115 if ((kn->kn_flags & EV_FLAG1) == 0)
2117 kn->kn_flags &= ~EV_FLAG1;
2119 knlist_add(&aiocbe->klist, kn, 0);
2124 /* kqueue detach function */
2126 filt_aiodetach(struct knote *kn)
2128 struct aiocblist *aiocbe = (struct aiocblist *)kn->kn_sdata;
2130 if (!knlist_empty(&aiocbe->klist))
2131 knlist_remove(&aiocbe->klist, kn, 0);
2134 /* kqueue filter function */
2137 filt_aio(struct knote *kn, long hint)
2139 struct aiocblist *aiocbe = (struct aiocblist *)kn->kn_sdata;
2141 kn->kn_data = aiocbe->uaiocb._aiocb_private.error;
2142 if (aiocbe->jobstate != JOBST_JOBFINISHED)
2144 kn->kn_flags |= EV_EOF;
2148 /* kqueue attach function */
2150 filt_lioattach(struct knote *kn)
2152 struct aioliojob * lj = (struct aioliojob *)kn->kn_sdata;
2155 * The aioliojob pointer must be validated before using it, so
2156 * registration is restricted to the kernel; the user cannot
2159 if ((kn->kn_flags & EV_FLAG1) == 0)
2161 kn->kn_flags &= ~EV_FLAG1;
2163 knlist_add(&lj->klist, kn, 0);
2168 /* kqueue detach function */
2170 filt_liodetach(struct knote *kn)
2172 struct aioliojob * lj = (struct aioliojob *)kn->kn_sdata;
2174 if (!knlist_empty(&lj->klist))
2175 knlist_remove(&lj->klist, kn, 0);
2178 /* kqueue filter function */
2181 filt_lio(struct knote *kn, long hint)
2183 struct aioliojob * lj = (struct aioliojob *)kn->kn_sdata;
2185 return (lj->lioj_flags & LIOJ_KEVENT_POSTED);