2 * SPDX-License-Identifier: BSD-2-Clause
4 * Copyright (c) 1997 John S. Dyson. All rights reserved.
6 * Redistribution and use in source and binary forms, with or without
7 * modification, are permitted provided that the following conditions
9 * 1. Redistributions of source code must retain the above copyright
10 * notice, this list of conditions and the following disclaimer.
11 * 2. John S. Dyson's name may not be used to endorse or promote products
12 * derived from this software without specific prior written permission.
14 * DISCLAIMER: This code isn't warranted to do anything useful. Anything
15 * bad that happens because of using this software isn't the responsibility
16 * of the author. This software is distributed AS-IS.
20 * This file contains support for the POSIX 1003.1B AIO/LIO facility.
23 #include <sys/param.h>
24 #include <sys/systm.h>
25 #include <sys/malloc.h>
28 #include <sys/capsicum.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>
41 #include <sys/posix4.h>
43 #include <sys/resourcevar.h>
44 #include <sys/signalvar.h>
45 #include <sys/syscallsubr.h>
46 #include <sys/protosw.h>
47 #include <sys/rwlock.h>
49 #include <sys/socket.h>
50 #include <sys/socketvar.h>
51 #include <sys/syscall.h>
52 #include <sys/sysctl.h>
53 #include <sys/syslog.h>
55 #include <sys/taskqueue.h>
56 #include <sys/vnode.h>
58 #include <sys/event.h>
59 #include <sys/mount.h>
60 #include <geom/geom.h>
62 #include <machine/atomic.h>
65 #include <vm/vm_page.h>
66 #include <vm/vm_extern.h>
68 #include <vm/vm_map.h>
69 #include <vm/vm_object.h>
74 * Counter for allocating reference ids to new jobs. Wrapped to 1 on
75 * overflow. (XXX will be removed soon.)
77 static u_long jobrefid;
80 * Counter for aio_fsync.
82 static uint64_t jobseqno;
84 #ifndef MAX_AIO_PER_PROC
85 #define MAX_AIO_PER_PROC 32
88 #ifndef MAX_AIO_QUEUE_PER_PROC
89 #define MAX_AIO_QUEUE_PER_PROC 256
93 #define MAX_AIO_QUEUE 1024 /* Bigger than MAX_AIO_QUEUE_PER_PROC */
97 #define MAX_BUF_AIO 16
100 FEATURE(aio, "Asynchronous I/O");
101 SYSCTL_DECL(_p1003_1b);
103 static MALLOC_DEFINE(M_LIO, "lio", "listio aio control block list");
104 static MALLOC_DEFINE(M_AIO, "aio", "structures for asynchronous I/O");
106 static SYSCTL_NODE(_vfs, OID_AUTO, aio, CTLFLAG_RW | CTLFLAG_MPSAFE, 0,
107 "Async IO management");
109 static int enable_aio_unsafe = 0;
110 SYSCTL_INT(_vfs_aio, OID_AUTO, enable_unsafe, CTLFLAG_RW, &enable_aio_unsafe, 0,
111 "Permit asynchronous IO on all file types, not just known-safe types");
113 static unsigned int unsafe_warningcnt = 1;
114 SYSCTL_UINT(_vfs_aio, OID_AUTO, unsafe_warningcnt, CTLFLAG_RW,
115 &unsafe_warningcnt, 0,
116 "Warnings that will be triggered upon failed IO requests on unsafe files");
118 static int max_aio_procs = MAX_AIO_PROCS;
119 SYSCTL_INT(_vfs_aio, OID_AUTO, max_aio_procs, CTLFLAG_RW, &max_aio_procs, 0,
120 "Maximum number of kernel processes to use for handling async IO ");
122 static int num_aio_procs = 0;
123 SYSCTL_INT(_vfs_aio, OID_AUTO, num_aio_procs, CTLFLAG_RD, &num_aio_procs, 0,
124 "Number of presently active kernel processes 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,
133 "Preferred number of ready kernel processes for async IO");
135 static int max_queue_count = MAX_AIO_QUEUE;
136 SYSCTL_INT(_vfs_aio, OID_AUTO, max_aio_queue, CTLFLAG_RW, &max_queue_count, 0,
137 "Maximum number of aio requests to queue, globally");
139 static int num_queue_count = 0;
140 SYSCTL_INT(_vfs_aio, OID_AUTO, num_queue_count, CTLFLAG_RD, &num_queue_count, 0,
141 "Number of queued aio requests");
143 static int num_buf_aio = 0;
144 SYSCTL_INT(_vfs_aio, OID_AUTO, num_buf_aio, CTLFLAG_RD, &num_buf_aio, 0,
145 "Number of aio requests presently handled by the buf subsystem");
147 static int num_unmapped_aio = 0;
148 SYSCTL_INT(_vfs_aio, OID_AUTO, num_unmapped_aio, CTLFLAG_RD, &num_unmapped_aio,
150 "Number of aio requests presently handled by unmapped I/O buffers");
152 /* Number of async I/O processes in the process of being started */
153 /* XXX This should be local to aio_aqueue() */
154 static int num_aio_resv_start = 0;
156 static int aiod_lifetime;
157 SYSCTL_INT(_vfs_aio, OID_AUTO, aiod_lifetime, CTLFLAG_RW, &aiod_lifetime, 0,
158 "Maximum lifetime for idle aiod");
160 static int max_aio_per_proc = MAX_AIO_PER_PROC;
161 SYSCTL_INT(_vfs_aio, OID_AUTO, max_aio_per_proc, CTLFLAG_RW, &max_aio_per_proc,
163 "Maximum active aio requests per process");
165 static int max_aio_queue_per_proc = MAX_AIO_QUEUE_PER_PROC;
166 SYSCTL_INT(_vfs_aio, OID_AUTO, max_aio_queue_per_proc, CTLFLAG_RW,
167 &max_aio_queue_per_proc, 0,
168 "Maximum queued aio requests per process");
170 static int max_buf_aio = MAX_BUF_AIO;
171 SYSCTL_INT(_vfs_aio, OID_AUTO, max_buf_aio, CTLFLAG_RW, &max_buf_aio, 0,
172 "Maximum buf aio requests per process");
175 * Though redundant with vfs.aio.max_aio_queue_per_proc, POSIX requires
176 * sysconf(3) to support AIO_LISTIO_MAX, and we implement that with
177 * vfs.aio.aio_listio_max.
179 SYSCTL_INT(_p1003_1b, CTL_P1003_1B_AIO_LISTIO_MAX, aio_listio_max,
180 CTLFLAG_RD | CTLFLAG_CAPRD, &max_aio_queue_per_proc,
181 0, "Maximum aio requests for a single lio_listio call");
183 #ifdef COMPAT_FREEBSD6
184 typedef struct oaiocb {
185 int aio_fildes; /* File descriptor */
186 off_t aio_offset; /* File offset for I/O */
187 volatile void *aio_buf; /* I/O buffer in process space */
188 size_t aio_nbytes; /* Number of bytes for I/O */
189 struct osigevent aio_sigevent; /* Signal to deliver */
190 int aio_lio_opcode; /* LIO opcode */
191 int aio_reqprio; /* Request priority -- ignored */
192 struct __aiocb_private _aiocb_private;
197 * Below is a key of locks used to protect each member of struct kaiocb
198 * aioliojob and kaioinfo and any backends.
200 * * - need not protected
201 * a - locked by kaioinfo lock
202 * b - locked by backend lock, the backend lock can be null in some cases,
203 * for example, BIO belongs to this type, in this case, proc lock is
205 * c - locked by aio_job_mtx, the lock for the generic file I/O backend.
209 * If the routine that services an AIO request blocks while running in an
210 * AIO kernel process it can starve other I/O requests. BIO requests
211 * queued via aio_qbio() complete asynchronously and do not use AIO kernel
212 * processes at all. Socket I/O requests use a separate pool of
213 * kprocs and also force non-blocking I/O. Other file I/O requests
214 * use the generic fo_read/fo_write operations which can block. The
215 * fsync and mlock operations can also block while executing. Ideally
216 * none of these requests would block while executing.
218 * Note that the service routines cannot toggle O_NONBLOCK in the file
219 * structure directly while handling a request due to races with
224 #define KAIOCB_QUEUEING 0x01
225 #define KAIOCB_CANCELLED 0x02
226 #define KAIOCB_CANCELLING 0x04
227 #define KAIOCB_CHECKSYNC 0x08
228 #define KAIOCB_CLEARED 0x10
229 #define KAIOCB_FINISHED 0x20
234 #define AIOP_FREE 0x1 /* proc on free queue */
237 int aioprocflags; /* (c) AIO proc flags */
238 TAILQ_ENTRY(aioproc) list; /* (c) list of processes */
239 struct proc *aioproc; /* (*) the AIO proc */
243 * data-structure for lio signal management
246 int lioj_flags; /* (a) listio flags */
247 int lioj_count; /* (a) count of jobs */
248 int lioj_finished_count; /* (a) count of finished jobs */
249 struct sigevent lioj_signal; /* (a) signal on all I/O done */
250 TAILQ_ENTRY(aioliojob) lioj_list; /* (a) lio list */
251 struct knlist klist; /* (a) list of knotes */
252 ksiginfo_t lioj_ksi; /* (a) Realtime signal info */
255 #define LIOJ_SIGNAL 0x1 /* signal on all done (lio) */
256 #define LIOJ_SIGNAL_POSTED 0x2 /* signal has been posted */
257 #define LIOJ_KEVENT_POSTED 0x4 /* kevent triggered */
260 * per process aio data structure
263 struct mtx kaio_mtx; /* the lock to protect this struct */
264 int kaio_flags; /* (a) per process kaio flags */
265 int kaio_active_count; /* (c) number of currently used AIOs */
266 int kaio_count; /* (a) size of AIO queue */
267 int kaio_buffer_count; /* (a) number of bio buffers */
268 TAILQ_HEAD(,kaiocb) kaio_all; /* (a) all AIOs in a process */
269 TAILQ_HEAD(,kaiocb) kaio_done; /* (a) done queue for process */
270 TAILQ_HEAD(,aioliojob) kaio_liojoblist; /* (a) list of lio jobs */
271 TAILQ_HEAD(,kaiocb) kaio_jobqueue; /* (a) job queue for process */
272 TAILQ_HEAD(,kaiocb) kaio_syncqueue; /* (a) queue for aio_fsync */
273 TAILQ_HEAD(,kaiocb) kaio_syncready; /* (a) second q for aio_fsync */
274 struct task kaio_task; /* (*) task to kick aio processes */
275 struct task kaio_sync_task; /* (*) task to schedule fsync jobs */
278 #define AIO_LOCK(ki) mtx_lock(&(ki)->kaio_mtx)
279 #define AIO_UNLOCK(ki) mtx_unlock(&(ki)->kaio_mtx)
280 #define AIO_LOCK_ASSERT(ki, f) mtx_assert(&(ki)->kaio_mtx, (f))
281 #define AIO_MTX(ki) (&(ki)->kaio_mtx)
283 #define KAIO_RUNDOWN 0x1 /* process is being run down */
284 #define KAIO_WAKEUP 0x2 /* wakeup process when AIO completes */
287 * Operations used to interact with userland aio control blocks.
288 * Different ABIs provide their own operations.
291 int (*aio_copyin)(struct aiocb *ujob, struct kaiocb *kjob, int ty);
292 long (*fetch_status)(struct aiocb *ujob);
293 long (*fetch_error)(struct aiocb *ujob);
294 int (*store_status)(struct aiocb *ujob, long status);
295 int (*store_error)(struct aiocb *ujob, long error);
296 int (*store_kernelinfo)(struct aiocb *ujob, long jobref);
297 int (*store_aiocb)(struct aiocb **ujobp, struct aiocb *ujob);
300 static TAILQ_HEAD(,aioproc) aio_freeproc; /* (c) Idle daemons */
301 static struct sema aio_newproc_sem;
302 static struct mtx aio_job_mtx;
303 static TAILQ_HEAD(,kaiocb) aio_jobs; /* (c) Async job list */
304 static struct unrhdr *aiod_unr;
306 static void aio_biocleanup(struct bio *bp);
307 void aio_init_aioinfo(struct proc *p);
308 static int aio_onceonly(void);
309 static int aio_free_entry(struct kaiocb *job);
310 static void aio_process_rw(struct kaiocb *job);
311 static void aio_process_sync(struct kaiocb *job);
312 static void aio_process_mlock(struct kaiocb *job);
313 static void aio_schedule_fsync(void *context, int pending);
314 static int aio_newproc(int *);
315 int aio_aqueue(struct thread *td, struct aiocb *ujob,
316 struct aioliojob *lio, int type, struct aiocb_ops *ops);
317 static int aio_queue_file(struct file *fp, struct kaiocb *job);
318 static void aio_biowakeup(struct bio *bp);
319 static void aio_proc_rundown(void *arg, struct proc *p);
320 static void aio_proc_rundown_exec(void *arg, struct proc *p,
321 struct image_params *imgp);
322 static int aio_qbio(struct proc *p, struct kaiocb *job);
323 static void aio_daemon(void *param);
324 static void aio_bio_done_notify(struct proc *userp, struct kaiocb *job);
325 static bool aio_clear_cancel_function_locked(struct kaiocb *job);
326 static int aio_kick(struct proc *userp);
327 static void aio_kick_nowait(struct proc *userp);
328 static void aio_kick_helper(void *context, int pending);
329 static int filt_aioattach(struct knote *kn);
330 static void filt_aiodetach(struct knote *kn);
331 static int filt_aio(struct knote *kn, long hint);
332 static int filt_lioattach(struct knote *kn);
333 static void filt_liodetach(struct knote *kn);
334 static int filt_lio(struct knote *kn, long hint);
338 * kaio Per process async io info
339 * aiocb async io jobs
340 * aiolio list io jobs
342 static uma_zone_t kaio_zone, aiocb_zone, aiolio_zone;
344 /* kqueue filters for aio */
345 static struct filterops aio_filtops = {
347 .f_attach = filt_aioattach,
348 .f_detach = filt_aiodetach,
351 static struct filterops lio_filtops = {
353 .f_attach = filt_lioattach,
354 .f_detach = filt_liodetach,
358 static eventhandler_tag exit_tag, exec_tag;
360 TASKQUEUE_DEFINE_THREAD(aiod_kick);
363 * Main operations function for use as a kernel module.
366 aio_modload(struct module *module, int cmd, void *arg)
383 static moduledata_t aio_mod = {
389 DECLARE_MODULE(aio, aio_mod, SI_SUB_VFS, SI_ORDER_ANY);
390 MODULE_VERSION(aio, 1);
393 * Startup initialization
399 exit_tag = EVENTHANDLER_REGISTER(process_exit, aio_proc_rundown, NULL,
400 EVENTHANDLER_PRI_ANY);
401 exec_tag = EVENTHANDLER_REGISTER(process_exec, aio_proc_rundown_exec,
402 NULL, EVENTHANDLER_PRI_ANY);
403 kqueue_add_filteropts(EVFILT_AIO, &aio_filtops);
404 kqueue_add_filteropts(EVFILT_LIO, &lio_filtops);
405 TAILQ_INIT(&aio_freeproc);
406 sema_init(&aio_newproc_sem, 0, "aio_new_proc");
407 mtx_init(&aio_job_mtx, "aio_job", 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, 0);
412 aiocb_zone = uma_zcreate("AIOCB", sizeof(struct kaiocb), NULL, NULL,
413 NULL, NULL, UMA_ALIGN_PTR, 0);
414 aiolio_zone = uma_zcreate("AIOLIO", sizeof(struct aioliojob), NULL,
415 NULL, NULL, NULL, UMA_ALIGN_PTR, 0);
416 aiod_lifetime = AIOD_LIFETIME_DEFAULT;
418 p31b_setcfg(CTL_P1003_1B_ASYNCHRONOUS_IO, _POSIX_ASYNCHRONOUS_IO);
419 p31b_setcfg(CTL_P1003_1B_AIO_MAX, MAX_AIO_QUEUE);
420 p31b_setcfg(CTL_P1003_1B_AIO_PRIO_DELTA_MAX, 0);
426 * Init the per-process aioinfo structure. The aioinfo limits are set
427 * per-process for user limit (resource) management.
430 aio_init_aioinfo(struct proc *p)
434 ki = uma_zalloc(kaio_zone, M_WAITOK);
435 mtx_init(&ki->kaio_mtx, "aiomtx", NULL, MTX_DEF | MTX_NEW);
437 ki->kaio_active_count = 0;
439 ki->kaio_buffer_count = 0;
440 TAILQ_INIT(&ki->kaio_all);
441 TAILQ_INIT(&ki->kaio_done);
442 TAILQ_INIT(&ki->kaio_jobqueue);
443 TAILQ_INIT(&ki->kaio_liojoblist);
444 TAILQ_INIT(&ki->kaio_syncqueue);
445 TAILQ_INIT(&ki->kaio_syncready);
446 TASK_INIT(&ki->kaio_task, 0, aio_kick_helper, p);
447 TASK_INIT(&ki->kaio_sync_task, 0, aio_schedule_fsync, ki);
449 if (p->p_aioinfo == NULL) {
454 mtx_destroy(&ki->kaio_mtx);
455 uma_zfree(kaio_zone, ki);
458 while (num_aio_procs < MIN(target_aio_procs, max_aio_procs))
463 aio_sendsig(struct proc *p, struct sigevent *sigev, ksiginfo_t *ksi, bool ext)
468 error = sigev_findtd(p, sigev, &td);
472 ksiginfo_set_sigev(ksi, sigev);
473 ksi->ksi_code = SI_ASYNCIO;
474 ksi->ksi_flags |= ext ? (KSI_EXT | KSI_INS) : 0;
475 tdsendsignal(p, td, ksi->ksi_signo, ksi);
482 * Free a job entry. Wait for completion if it is currently active, but don't
483 * delay forever. If we delay, we return a flag that says that we have to
484 * restart the queue scan.
487 aio_free_entry(struct kaiocb *job)
490 struct aioliojob *lj;
498 AIO_LOCK_ASSERT(ki, MA_OWNED);
499 MPASS(job->jobflags & KAIOCB_FINISHED);
501 atomic_subtract_int(&num_queue_count, 1);
504 MPASS(ki->kaio_count >= 0);
506 TAILQ_REMOVE(&ki->kaio_done, job, plist);
507 TAILQ_REMOVE(&ki->kaio_all, job, allist);
512 lj->lioj_finished_count--;
514 if (lj->lioj_count == 0) {
515 TAILQ_REMOVE(&ki->kaio_liojoblist, lj, lioj_list);
516 /* lio is going away, we need to destroy any knotes */
517 knlist_delete(&lj->klist, curthread, 1);
519 sigqueue_take(&lj->lioj_ksi);
521 uma_zfree(aiolio_zone, lj);
525 /* job is going away, we need to destroy any knotes */
526 knlist_delete(&job->klist, curthread, 1);
528 sigqueue_take(&job->ksi);
534 * The thread argument here is used to find the owning process
535 * and is also passed to fo_close() which may pass it to various
536 * places such as devsw close() routines. Because of that, we
537 * need a thread pointer from the process owning the job that is
538 * persistent and won't disappear out from under us or move to
541 * Currently, all the callers of this function call it to remove
542 * a kaiocb from the current process' job list either via a
543 * syscall or due to the current process calling exit() or
544 * execve(). Thus, we know that p == curproc. We also know that
545 * curthread can't exit since we are curthread.
547 * Therefore, we use curthread as the thread to pass to
548 * knlist_delete(). This does mean that it is possible for the
549 * thread pointer at close time to differ from the thread pointer
550 * at open time, but this is already true of file descriptors in
551 * a multithreaded process.
554 fdrop(job->fd_file, curthread);
556 if (job->uiop != &job->uio)
557 free(job->uiop, M_IOV);
558 uma_zfree(aiocb_zone, job);
565 aio_proc_rundown_exec(void *arg, struct proc *p,
566 struct image_params *imgp __unused)
568 aio_proc_rundown(arg, p);
572 aio_cancel_job(struct proc *p, struct kaioinfo *ki, struct kaiocb *job)
574 aio_cancel_fn_t *func;
577 AIO_LOCK_ASSERT(ki, MA_OWNED);
578 if (job->jobflags & (KAIOCB_CANCELLED | KAIOCB_FINISHED))
580 MPASS((job->jobflags & KAIOCB_CANCELLING) == 0);
581 job->jobflags |= KAIOCB_CANCELLED;
583 func = job->cancel_fn;
586 * If there is no cancel routine, just leave the job marked as
587 * cancelled. The job should be in active use by a caller who
588 * should complete it normally or when it fails to install a
595 * Set the CANCELLING flag so that aio_complete() will defer
596 * completions of this job. This prevents the job from being
597 * freed out from under the cancel callback. After the
598 * callback any deferred completion (whether from the callback
599 * or any other source) will be completed.
601 job->jobflags |= KAIOCB_CANCELLING;
605 job->jobflags &= ~KAIOCB_CANCELLING;
606 if (job->jobflags & KAIOCB_FINISHED) {
607 cancelled = job->uaiocb._aiocb_private.error == ECANCELED;
608 TAILQ_REMOVE(&ki->kaio_jobqueue, job, plist);
609 aio_bio_done_notify(p, job);
612 * The cancel callback might have scheduled an
613 * operation to cancel this request, but it is
614 * only counted as cancelled if the request is
615 * cancelled when the callback returns.
623 * Rundown the jobs for a given process.
626 aio_proc_rundown(void *arg, struct proc *p)
629 struct aioliojob *lj;
630 struct kaiocb *job, *jobn;
632 KASSERT(curthread->td_proc == p,
633 ("%s: called on non-curproc", __func__));
639 ki->kaio_flags |= KAIO_RUNDOWN;
644 * Try to cancel all pending requests. This code simulates
645 * aio_cancel on all pending I/O requests.
647 TAILQ_FOREACH_SAFE(job, &ki->kaio_jobqueue, plist, jobn) {
648 aio_cancel_job(p, ki, job);
651 /* Wait for all running I/O to be finished */
652 if (TAILQ_FIRST(&ki->kaio_jobqueue) || ki->kaio_active_count != 0) {
653 ki->kaio_flags |= KAIO_WAKEUP;
654 msleep(&p->p_aioinfo, AIO_MTX(ki), PRIBIO, "aioprn", hz);
658 /* Free all completed I/O requests. */
659 while ((job = TAILQ_FIRST(&ki->kaio_done)) != NULL)
662 while ((lj = TAILQ_FIRST(&ki->kaio_liojoblist)) != NULL) {
663 if (lj->lioj_count == 0) {
664 TAILQ_REMOVE(&ki->kaio_liojoblist, lj, lioj_list);
665 knlist_delete(&lj->klist, curthread, 1);
667 sigqueue_take(&lj->lioj_ksi);
669 uma_zfree(aiolio_zone, lj);
671 panic("LIO job not cleaned up: C:%d, FC:%d\n",
672 lj->lioj_count, lj->lioj_finished_count);
676 taskqueue_drain(taskqueue_aiod_kick, &ki->kaio_task);
677 taskqueue_drain(taskqueue_aiod_kick, &ki->kaio_sync_task);
678 mtx_destroy(&ki->kaio_mtx);
679 uma_zfree(kaio_zone, ki);
684 * Select a job to run (called by an AIO daemon).
686 static struct kaiocb *
687 aio_selectjob(struct aioproc *aiop)
693 mtx_assert(&aio_job_mtx, MA_OWNED);
695 TAILQ_FOREACH(job, &aio_jobs, list) {
696 userp = job->userproc;
697 ki = userp->p_aioinfo;
699 if (ki->kaio_active_count < max_aio_per_proc) {
700 TAILQ_REMOVE(&aio_jobs, job, list);
701 if (!aio_clear_cancel_function(job))
704 /* Account for currently active jobs. */
705 ki->kaio_active_count++;
713 * Move all data to a permanent storage device. This code
714 * simulates the fsync and fdatasync syscalls.
717 aio_fsync_vnode(struct thread *td, struct vnode *vp, int op)
724 error = vn_start_write(vp, &mp, V_WAIT | V_PCATCH);
727 vn_lock(vp, LK_EXCLUSIVE | LK_RETRY);
730 VM_OBJECT_WLOCK(obj);
731 vm_object_page_clean(obj, 0, 0, 0);
732 VM_OBJECT_WUNLOCK(obj);
735 error = VOP_FDATASYNC(vp, td);
737 error = VOP_FSYNC(vp, MNT_WAIT, td);
740 vn_finished_write(mp);
741 if (error != ERELOOKUP)
748 * The AIO processing activity for LIO_READ/LIO_WRITE. This is the code that
749 * does the I/O request for the non-bio version of the operations. The normal
750 * vn operations are used, and this code should work in all instances for every
751 * type of file, including pipes, sockets, fifos, and regular files.
753 * XXX I don't think it works well for socket, pipe, and fifo.
756 aio_process_rw(struct kaiocb *job)
758 struct ucred *td_savedcred;
762 long msgsnd_st, msgsnd_end;
763 long msgrcv_st, msgrcv_end;
764 long oublock_st, oublock_end;
765 long inblock_st, inblock_end;
768 KASSERT(job->uaiocb.aio_lio_opcode == LIO_READ ||
769 job->uaiocb.aio_lio_opcode == LIO_READV ||
770 job->uaiocb.aio_lio_opcode == LIO_WRITE ||
771 job->uaiocb.aio_lio_opcode == LIO_WRITEV,
772 ("%s: opcode %d", __func__, job->uaiocb.aio_lio_opcode));
774 aio_switch_vmspace(job);
776 td_savedcred = td->td_ucred;
777 td->td_ucred = job->cred;
778 job->uiop->uio_td = td;
781 opcode = job->uaiocb.aio_lio_opcode;
782 cnt = job->uiop->uio_resid;
784 msgrcv_st = td->td_ru.ru_msgrcv;
785 msgsnd_st = td->td_ru.ru_msgsnd;
786 inblock_st = td->td_ru.ru_inblock;
787 oublock_st = td->td_ru.ru_oublock;
790 * aio_aqueue() acquires a reference to the file that is
791 * released in aio_free_entry().
793 if (opcode == LIO_READ || opcode == LIO_READV) {
794 if (job->uiop->uio_resid == 0)
797 error = fo_read(fp, job->uiop, fp->f_cred, FOF_OFFSET,
800 if (fp->f_type == DTYPE_VNODE)
802 error = fo_write(fp, job->uiop, fp->f_cred, FOF_OFFSET, td);
804 msgrcv_end = td->td_ru.ru_msgrcv;
805 msgsnd_end = td->td_ru.ru_msgsnd;
806 inblock_end = td->td_ru.ru_inblock;
807 oublock_end = td->td_ru.ru_oublock;
809 job->msgrcv = msgrcv_end - msgrcv_st;
810 job->msgsnd = msgsnd_end - msgsnd_st;
811 job->inblock = inblock_end - inblock_st;
812 job->outblock = oublock_end - oublock_st;
814 if (error != 0 && job->uiop->uio_resid != cnt) {
815 if (error == ERESTART || error == EINTR || error == EWOULDBLOCK)
817 if (error == EPIPE && (opcode & LIO_WRITE)) {
818 PROC_LOCK(job->userproc);
819 kern_psignal(job->userproc, SIGPIPE);
820 PROC_UNLOCK(job->userproc);
824 cnt -= job->uiop->uio_resid;
825 td->td_ucred = td_savedcred;
827 aio_complete(job, -1, error);
829 aio_complete(job, cnt, 0);
833 aio_process_sync(struct kaiocb *job)
835 struct thread *td = curthread;
836 struct ucred *td_savedcred = td->td_ucred;
837 struct file *fp = job->fd_file;
840 KASSERT(job->uaiocb.aio_lio_opcode & LIO_SYNC,
841 ("%s: opcode %d", __func__, job->uaiocb.aio_lio_opcode));
843 td->td_ucred = job->cred;
844 if (fp->f_vnode != NULL) {
845 error = aio_fsync_vnode(td, fp->f_vnode,
846 job->uaiocb.aio_lio_opcode);
848 td->td_ucred = td_savedcred;
850 aio_complete(job, -1, error);
852 aio_complete(job, 0, 0);
856 aio_process_mlock(struct kaiocb *job)
858 struct aiocb *cb = &job->uaiocb;
861 KASSERT(job->uaiocb.aio_lio_opcode == LIO_MLOCK,
862 ("%s: opcode %d", __func__, job->uaiocb.aio_lio_opcode));
864 aio_switch_vmspace(job);
865 error = kern_mlock(job->userproc, job->cred,
866 __DEVOLATILE(uintptr_t, cb->aio_buf), cb->aio_nbytes);
867 aio_complete(job, error != 0 ? -1 : 0, error);
871 aio_bio_done_notify(struct proc *userp, struct kaiocb *job)
873 struct aioliojob *lj;
875 struct kaiocb *sjob, *sjobn;
879 ki = userp->p_aioinfo;
880 AIO_LOCK_ASSERT(ki, MA_OWNED);
884 lj->lioj_finished_count++;
885 if (lj->lioj_count == lj->lioj_finished_count)
888 TAILQ_INSERT_TAIL(&ki->kaio_done, job, plist);
889 MPASS(job->jobflags & KAIOCB_FINISHED);
891 if (ki->kaio_flags & KAIO_RUNDOWN)
892 goto notification_done;
894 if (job->uaiocb.aio_sigevent.sigev_notify == SIGEV_SIGNAL ||
895 job->uaiocb.aio_sigevent.sigev_notify == SIGEV_THREAD_ID)
896 aio_sendsig(userp, &job->uaiocb.aio_sigevent, &job->ksi, true);
898 KNOTE_LOCKED(&job->klist, 1);
901 if (lj->lioj_signal.sigev_notify == SIGEV_KEVENT) {
902 lj->lioj_flags |= LIOJ_KEVENT_POSTED;
903 KNOTE_LOCKED(&lj->klist, 1);
905 if ((lj->lioj_flags & (LIOJ_SIGNAL | LIOJ_SIGNAL_POSTED))
907 (lj->lioj_signal.sigev_notify == SIGEV_SIGNAL ||
908 lj->lioj_signal.sigev_notify == SIGEV_THREAD_ID)) {
909 aio_sendsig(userp, &lj->lioj_signal, &lj->lioj_ksi,
911 lj->lioj_flags |= LIOJ_SIGNAL_POSTED;
916 if (job->jobflags & KAIOCB_CHECKSYNC) {
917 schedule_fsync = false;
918 TAILQ_FOREACH_SAFE(sjob, &ki->kaio_syncqueue, list, sjobn) {
919 if (job->fd_file != sjob->fd_file ||
920 job->seqno >= sjob->seqno)
922 if (--sjob->pending > 0)
924 TAILQ_REMOVE(&ki->kaio_syncqueue, sjob, list);
925 if (!aio_clear_cancel_function_locked(sjob))
927 TAILQ_INSERT_TAIL(&ki->kaio_syncready, sjob, list);
928 schedule_fsync = true;
931 taskqueue_enqueue(taskqueue_aiod_kick,
932 &ki->kaio_sync_task);
934 if (ki->kaio_flags & KAIO_WAKEUP) {
935 ki->kaio_flags &= ~KAIO_WAKEUP;
936 wakeup(&userp->p_aioinfo);
941 aio_schedule_fsync(void *context, int pending)
948 while (!TAILQ_EMPTY(&ki->kaio_syncready)) {
949 job = TAILQ_FIRST(&ki->kaio_syncready);
950 TAILQ_REMOVE(&ki->kaio_syncready, job, list);
952 aio_schedule(job, aio_process_sync);
959 aio_cancel_cleared(struct kaiocb *job)
963 * The caller should hold the same queue lock held when
964 * aio_clear_cancel_function() was called and set this flag
965 * ensuring this check sees an up-to-date value. However,
966 * there is no way to assert that.
968 return ((job->jobflags & KAIOCB_CLEARED) != 0);
972 aio_clear_cancel_function_locked(struct kaiocb *job)
975 AIO_LOCK_ASSERT(job->userproc->p_aioinfo, MA_OWNED);
976 MPASS(job->cancel_fn != NULL);
977 if (job->jobflags & KAIOCB_CANCELLING) {
978 job->jobflags |= KAIOCB_CLEARED;
981 job->cancel_fn = NULL;
986 aio_clear_cancel_function(struct kaiocb *job)
991 ki = job->userproc->p_aioinfo;
993 ret = aio_clear_cancel_function_locked(job);
999 aio_set_cancel_function_locked(struct kaiocb *job, aio_cancel_fn_t *func)
1002 AIO_LOCK_ASSERT(job->userproc->p_aioinfo, MA_OWNED);
1003 if (job->jobflags & KAIOCB_CANCELLED)
1005 job->cancel_fn = func;
1010 aio_set_cancel_function(struct kaiocb *job, aio_cancel_fn_t *func)
1012 struct kaioinfo *ki;
1015 ki = job->userproc->p_aioinfo;
1017 ret = aio_set_cancel_function_locked(job, func);
1023 aio_complete(struct kaiocb *job, long status, int error)
1025 struct kaioinfo *ki;
1028 job->uaiocb._aiocb_private.error = error;
1029 job->uaiocb._aiocb_private.status = status;
1031 userp = job->userproc;
1032 ki = userp->p_aioinfo;
1035 KASSERT(!(job->jobflags & KAIOCB_FINISHED),
1036 ("duplicate aio_complete"));
1037 job->jobflags |= KAIOCB_FINISHED;
1038 if ((job->jobflags & (KAIOCB_QUEUEING | KAIOCB_CANCELLING)) == 0) {
1039 TAILQ_REMOVE(&ki->kaio_jobqueue, job, plist);
1040 aio_bio_done_notify(userp, job);
1046 aio_cancel(struct kaiocb *job)
1049 aio_complete(job, -1, ECANCELED);
1053 aio_switch_vmspace(struct kaiocb *job)
1056 vmspace_switch_aio(job->userproc->p_vmspace);
1060 * The AIO daemon, most of the actual work is done in aio_process_*,
1061 * but the setup (and address space mgmt) is done in this routine.
1064 aio_daemon(void *_id)
1067 struct aioproc *aiop;
1068 struct kaioinfo *ki;
1070 struct vmspace *myvm;
1071 struct thread *td = curthread;
1072 int id = (intptr_t)_id;
1075 * Grab an extra reference on the daemon's vmspace so that it
1076 * doesn't get freed by jobs that switch to a different
1080 myvm = vmspace_acquire_ref(p);
1082 KASSERT(p->p_textvp == NULL, ("kthread has a textvp"));
1085 * Allocate and ready the aio control info. There is one aiop structure
1088 aiop = malloc(sizeof(*aiop), M_AIO, M_WAITOK);
1090 aiop->aioprocflags = 0;
1093 * Wakeup parent process. (Parent sleeps to keep from blasting away
1094 * and creating too many daemons.)
1096 sema_post(&aio_newproc_sem);
1098 mtx_lock(&aio_job_mtx);
1101 * Take daemon off of free queue
1103 if (aiop->aioprocflags & AIOP_FREE) {
1104 TAILQ_REMOVE(&aio_freeproc, aiop, list);
1105 aiop->aioprocflags &= ~AIOP_FREE;
1111 while ((job = aio_selectjob(aiop)) != NULL) {
1112 mtx_unlock(&aio_job_mtx);
1114 ki = job->userproc->p_aioinfo;
1115 job->handle_fn(job);
1117 mtx_lock(&aio_job_mtx);
1118 /* Decrement the active job count. */
1119 ki->kaio_active_count--;
1123 * Disconnect from user address space.
1125 if (p->p_vmspace != myvm) {
1126 mtx_unlock(&aio_job_mtx);
1127 vmspace_switch_aio(myvm);
1128 mtx_lock(&aio_job_mtx);
1130 * We have to restart to avoid race, we only sleep if
1131 * no job can be selected.
1136 mtx_assert(&aio_job_mtx, MA_OWNED);
1138 TAILQ_INSERT_HEAD(&aio_freeproc, aiop, list);
1139 aiop->aioprocflags |= AIOP_FREE;
1142 * If daemon is inactive for a long time, allow it to exit,
1143 * thereby freeing resources.
1145 if (msleep(p, &aio_job_mtx, PRIBIO, "aiordy",
1146 aiod_lifetime) == EWOULDBLOCK && TAILQ_EMPTY(&aio_jobs) &&
1147 (aiop->aioprocflags & AIOP_FREE) &&
1148 num_aio_procs > target_aio_procs)
1151 TAILQ_REMOVE(&aio_freeproc, aiop, list);
1153 mtx_unlock(&aio_job_mtx);
1155 free_unr(aiod_unr, id);
1158 KASSERT(p->p_vmspace == myvm,
1159 ("AIOD: bad vmspace for exiting daemon"));
1160 KASSERT(refcount_load(&myvm->vm_refcnt) > 1,
1161 ("AIOD: bad vm refcnt for exiting daemon: %d",
1162 refcount_load(&myvm->vm_refcnt)));
1167 * Create a new AIO daemon. This is mostly a kernel-thread fork routine. The
1168 * AIO daemon modifies its environment itself.
1171 aio_newproc(int *start)
1177 id = alloc_unr(aiod_unr);
1178 error = kproc_create(aio_daemon, (void *)(intptr_t)id, &p,
1179 RFNOWAIT, 0, "aiod%d", id);
1182 * Wait until daemon is started.
1184 sema_wait(&aio_newproc_sem);
1185 mtx_lock(&aio_job_mtx);
1189 mtx_unlock(&aio_job_mtx);
1191 free_unr(aiod_unr, id);
1197 * Try the high-performance, low-overhead bio method for eligible
1198 * VCHR devices. This method doesn't use an aio helper thread, and
1199 * thus has very low overhead.
1201 * Assumes that the caller, aio_aqueue(), has incremented the file
1202 * structure's reference count, preventing its deallocation for the
1203 * duration of this call.
1206 aio_qbio(struct proc *p, struct kaiocb *job)
1214 struct kaioinfo *ki;
1215 struct bio **bios = NULL;
1217 int bio_cmd, error, i, iovcnt, opcode, poff, ref;
1223 opcode = cb->aio_lio_opcode;
1225 if (!(opcode == LIO_WRITE || opcode == LIO_WRITEV ||
1226 opcode == LIO_READ || opcode == LIO_READV))
1228 if (fp == NULL || fp->f_type != DTYPE_VNODE)
1232 if (vp->v_type != VCHR)
1234 if (vp->v_bufobj.bo_bsize == 0)
1237 bio_cmd = (opcode & LIO_WRITE) ? BIO_WRITE : BIO_READ;
1238 iovcnt = job->uiop->uio_iovcnt;
1239 if (iovcnt > max_buf_aio)
1241 for (i = 0; i < iovcnt; i++) {
1242 if (job->uiop->uio_iov[i].iov_len % vp->v_bufobj.bo_bsize != 0)
1244 if (job->uiop->uio_iov[i].iov_len > maxphys) {
1249 offset = cb->aio_offset;
1252 csw = devvn_refthread(vp, &dev, &ref);
1256 if ((csw->d_flags & D_DISK) == 0) {
1260 if (job->uiop->uio_resid > dev->si_iosize_max) {
1268 use_unmapped = (dev->si_flags & SI_UNMAPPED) && unmapped_buf_allowed;
1269 if (!use_unmapped) {
1271 if (ki->kaio_buffer_count + iovcnt > max_buf_aio) {
1276 ki->kaio_buffer_count += iovcnt;
1280 bios = malloc(sizeof(struct bio *) * iovcnt, M_TEMP, M_WAITOK);
1281 refcount_init(&job->nbio, iovcnt);
1282 for (i = 0; i < iovcnt; i++) {
1283 struct vm_page** pages;
1289 buf = job->uiop->uio_iov[i].iov_base;
1290 nbytes = job->uiop->uio_iov[i].iov_len;
1292 bios[i] = g_alloc_bio();
1295 poff = (vm_offset_t)buf & PAGE_MASK;
1298 pages = malloc(sizeof(vm_page_t) * (atop(round_page(
1299 nbytes)) + 1), M_TEMP, M_WAITOK | M_ZERO);
1301 pbuf = uma_zalloc(pbuf_zone, M_WAITOK);
1303 pages = pbuf->b_pages;
1306 bp->bio_length = nbytes;
1307 bp->bio_bcount = nbytes;
1308 bp->bio_done = aio_biowakeup;
1309 bp->bio_offset = offset;
1310 bp->bio_cmd = bio_cmd;
1312 bp->bio_caller1 = job;
1313 bp->bio_caller2 = pbuf;
1315 prot = VM_PROT_READ;
1316 if (opcode == LIO_READ || opcode == LIO_READV)
1317 prot |= VM_PROT_WRITE; /* Less backwards than it looks */
1318 npages = vm_fault_quick_hold_pages(&curproc->p_vmspace->vm_map,
1319 (vm_offset_t)buf, bp->bio_length, prot, pages,
1323 uma_zfree(pbuf_zone, pbuf);
1325 free(pages, M_TEMP);
1332 pmap_qenter((vm_offset_t)pbuf->b_data, pages, npages);
1333 bp->bio_data = pbuf->b_data + poff;
1334 pbuf->b_npages = npages;
1335 atomic_add_int(&num_buf_aio, 1);
1338 bp->bio_ma_n = npages;
1339 bp->bio_ma_offset = poff;
1340 bp->bio_data = unmapped_buf;
1341 bp->bio_flags |= BIO_UNMAPPED;
1342 atomic_add_int(&num_unmapped_aio, 1);
1348 /* Perform transfer. */
1349 for (i = 0; i < iovcnt; i++)
1350 csw->d_strategy(bios[i]);
1353 dev_relthread(dev, ref);
1358 aio_biocleanup(bios[i]);
1361 dev_relthread(dev, ref);
1365 #ifdef COMPAT_FREEBSD6
1367 convert_old_sigevent(struct osigevent *osig, struct sigevent *nsig)
1371 * Only SIGEV_NONE, SIGEV_SIGNAL, and SIGEV_KEVENT are
1372 * supported by AIO with the old sigevent structure.
1374 nsig->sigev_notify = osig->sigev_notify;
1375 switch (nsig->sigev_notify) {
1379 nsig->sigev_signo = osig->__sigev_u.__sigev_signo;
1382 nsig->sigev_notify_kqueue =
1383 osig->__sigev_u.__sigev_notify_kqueue;
1384 nsig->sigev_value.sival_ptr = osig->sigev_value.sival_ptr;
1393 aiocb_copyin_old_sigevent(struct aiocb *ujob, struct kaiocb *kjob,
1396 struct oaiocb *ojob;
1397 struct aiocb *kcb = &kjob->uaiocb;
1400 bzero(kcb, sizeof(struct aiocb));
1401 error = copyin(ujob, kcb, sizeof(struct oaiocb));
1404 /* No need to copyin aio_iov, because it did not exist in FreeBSD 6 */
1405 ojob = (struct oaiocb *)kcb;
1406 return (convert_old_sigevent(&ojob->aio_sigevent, &kcb->aio_sigevent));
1411 aiocb_copyin(struct aiocb *ujob, struct kaiocb *kjob, int type)
1413 struct aiocb *kcb = &kjob->uaiocb;
1416 error = copyin(ujob, kcb, sizeof(struct aiocb));
1419 if (type == LIO_NOP)
1420 type = kcb->aio_lio_opcode;
1421 if (type & LIO_VECTORED) {
1422 /* malloc a uio and copy in the iovec */
1423 error = copyinuio(__DEVOLATILE(struct iovec*, kcb->aio_iov),
1424 kcb->aio_iovcnt, &kjob->uiop);
1431 aiocb_fetch_status(struct aiocb *ujob)
1434 return (fuword(&ujob->_aiocb_private.status));
1438 aiocb_fetch_error(struct aiocb *ujob)
1441 return (fuword(&ujob->_aiocb_private.error));
1445 aiocb_store_status(struct aiocb *ujob, long status)
1448 return (suword(&ujob->_aiocb_private.status, status));
1452 aiocb_store_error(struct aiocb *ujob, long error)
1455 return (suword(&ujob->_aiocb_private.error, error));
1459 aiocb_store_kernelinfo(struct aiocb *ujob, long jobref)
1462 return (suword(&ujob->_aiocb_private.kernelinfo, jobref));
1466 aiocb_store_aiocb(struct aiocb **ujobp, struct aiocb *ujob)
1469 return (suword(ujobp, (long)ujob));
1472 static struct aiocb_ops aiocb_ops = {
1473 .aio_copyin = aiocb_copyin,
1474 .fetch_status = aiocb_fetch_status,
1475 .fetch_error = aiocb_fetch_error,
1476 .store_status = aiocb_store_status,
1477 .store_error = aiocb_store_error,
1478 .store_kernelinfo = aiocb_store_kernelinfo,
1479 .store_aiocb = aiocb_store_aiocb,
1482 #ifdef COMPAT_FREEBSD6
1483 static struct aiocb_ops aiocb_ops_osigevent = {
1484 .aio_copyin = aiocb_copyin_old_sigevent,
1485 .fetch_status = aiocb_fetch_status,
1486 .fetch_error = aiocb_fetch_error,
1487 .store_status = aiocb_store_status,
1488 .store_error = aiocb_store_error,
1489 .store_kernelinfo = aiocb_store_kernelinfo,
1490 .store_aiocb = aiocb_store_aiocb,
1495 * Queue a new AIO request. Choosing either the threaded or direct bio VCHR
1496 * technique is done in this code.
1499 aio_aqueue(struct thread *td, struct aiocb *ujob, struct aioliojob *lj,
1500 int type, struct aiocb_ops *ops)
1502 struct proc *p = td->td_proc;
1503 struct file *fp = NULL;
1505 struct kaioinfo *ki;
1513 if (p->p_aioinfo == NULL)
1514 aio_init_aioinfo(p);
1518 ops->store_status(ujob, -1);
1519 ops->store_error(ujob, 0);
1520 ops->store_kernelinfo(ujob, -1);
1522 if (num_queue_count >= max_queue_count ||
1523 ki->kaio_count >= max_aio_queue_per_proc) {
1528 job = uma_zalloc(aiocb_zone, M_WAITOK | M_ZERO);
1529 knlist_init_mtx(&job->klist, AIO_MTX(ki));
1531 error = ops->aio_copyin(ujob, job, type);
1535 if (job->uaiocb.aio_nbytes > IOSIZE_MAX) {
1540 if (job->uaiocb.aio_sigevent.sigev_notify != SIGEV_KEVENT &&
1541 job->uaiocb.aio_sigevent.sigev_notify != SIGEV_SIGNAL &&
1542 job->uaiocb.aio_sigevent.sigev_notify != SIGEV_THREAD_ID &&
1543 job->uaiocb.aio_sigevent.sigev_notify != SIGEV_NONE) {
1548 if ((job->uaiocb.aio_sigevent.sigev_notify == SIGEV_SIGNAL ||
1549 job->uaiocb.aio_sigevent.sigev_notify == SIGEV_THREAD_ID) &&
1550 !_SIG_VALID(job->uaiocb.aio_sigevent.sigev_signo)) {
1555 /* Get the opcode. */
1556 if (type == LIO_NOP) {
1557 switch (job->uaiocb.aio_lio_opcode) {
1563 opcode = job->uaiocb.aio_lio_opcode;
1570 opcode = job->uaiocb.aio_lio_opcode = type;
1572 ksiginfo_init(&job->ksi);
1574 /* Save userspace address of the job info. */
1578 * Validate the opcode and fetch the file object for the specified
1581 * XXXRW: Moved the opcode validation up here so that we don't
1582 * retrieve a file descriptor without knowing what the capabiltity
1585 fd = job->uaiocb.aio_fildes;
1589 error = fget_write(td, fd, &cap_pwrite_rights, &fp);
1593 error = fget_read(td, fd, &cap_pread_rights, &fp);
1597 error = fget(td, fd, &cap_fsync_rights, &fp);
1602 error = fget(td, fd, &cap_no_rights, &fp);
1610 if ((opcode & LIO_SYNC) && fp->f_vnode == NULL) {
1615 if ((opcode == LIO_READ || opcode == LIO_READV ||
1616 opcode == LIO_WRITE || opcode == LIO_WRITEV) &&
1617 job->uaiocb.aio_offset < 0 &&
1618 (fp->f_vnode == NULL || fp->f_vnode->v_type != VCHR)) {
1623 if (fp != NULL && fp->f_ops == &path_fileops) {
1630 mtx_lock(&aio_job_mtx);
1632 job->seqno = jobseqno++;
1633 mtx_unlock(&aio_job_mtx);
1634 error = ops->store_kernelinfo(ujob, jid);
1639 job->uaiocb._aiocb_private.kernelinfo = (void *)(intptr_t)jid;
1641 if (opcode == LIO_NOP) {
1643 MPASS(job->uiop == &job->uio || job->uiop == NULL);
1644 uma_zfree(aiocb_zone, job);
1648 if (job->uaiocb.aio_sigevent.sigev_notify != SIGEV_KEVENT)
1650 evflags = job->uaiocb.aio_sigevent.sigev_notify_kevent_flags;
1651 if ((evflags & ~(EV_CLEAR | EV_DISPATCH | EV_ONESHOT)) != 0) {
1655 kqfd = job->uaiocb.aio_sigevent.sigev_notify_kqueue;
1656 memset(&kev, 0, sizeof(kev));
1657 kev.ident = (uintptr_t)job->ujob;
1658 kev.filter = EVFILT_AIO;
1659 kev.flags = EV_ADD | EV_ENABLE | EV_FLAG1 | evflags;
1660 kev.data = (intptr_t)job;
1661 kev.udata = job->uaiocb.aio_sigevent.sigev_value.sival_ptr;
1662 error = kqfd_register(kqfd, &kev, td, M_WAITOK);
1668 ops->store_error(ujob, EINPROGRESS);
1669 job->uaiocb._aiocb_private.error = EINPROGRESS;
1671 job->cred = crhold(td->td_ucred);
1672 job->jobflags = KAIOCB_QUEUEING;
1675 if (opcode & LIO_VECTORED) {
1676 /* Use the uio copied in by aio_copyin */
1677 MPASS(job->uiop != &job->uio && job->uiop != NULL);
1679 /* Setup the inline uio */
1680 job->iov[0].iov_base = (void *)(uintptr_t)job->uaiocb.aio_buf;
1681 job->iov[0].iov_len = job->uaiocb.aio_nbytes;
1682 job->uio.uio_iov = job->iov;
1683 job->uio.uio_iovcnt = 1;
1684 job->uio.uio_resid = job->uaiocb.aio_nbytes;
1685 job->uio.uio_segflg = UIO_USERSPACE;
1686 job->uiop = &job->uio;
1688 switch (opcode & (LIO_READ | LIO_WRITE)) {
1690 job->uiop->uio_rw = UIO_READ;
1693 job->uiop->uio_rw = UIO_WRITE;
1696 job->uiop->uio_offset = job->uaiocb.aio_offset;
1697 job->uiop->uio_td = td;
1699 if (opcode == LIO_MLOCK) {
1700 aio_schedule(job, aio_process_mlock);
1702 } else if (fp->f_ops->fo_aio_queue == NULL)
1703 error = aio_queue_file(fp, job);
1705 error = fo_aio_queue(fp, job);
1710 job->jobflags &= ~KAIOCB_QUEUEING;
1711 TAILQ_INSERT_TAIL(&ki->kaio_all, job, allist);
1715 atomic_add_int(&num_queue_count, 1);
1716 if (job->jobflags & KAIOCB_FINISHED) {
1718 * The queue callback completed the request synchronously.
1719 * The bulk of the completion is deferred in that case
1722 aio_bio_done_notify(p, job);
1724 TAILQ_INSERT_TAIL(&ki->kaio_jobqueue, job, plist);
1733 knlist_delete(&job->klist, curthread, 0);
1735 if (job->uiop != &job->uio)
1736 free(job->uiop, M_IOV);
1737 uma_zfree(aiocb_zone, job);
1739 ops->store_error(ujob, error);
1744 aio_cancel_daemon_job(struct kaiocb *job)
1747 mtx_lock(&aio_job_mtx);
1748 if (!aio_cancel_cleared(job))
1749 TAILQ_REMOVE(&aio_jobs, job, list);
1750 mtx_unlock(&aio_job_mtx);
1755 aio_schedule(struct kaiocb *job, aio_handle_fn_t *func)
1758 mtx_lock(&aio_job_mtx);
1759 if (!aio_set_cancel_function(job, aio_cancel_daemon_job)) {
1760 mtx_unlock(&aio_job_mtx);
1764 job->handle_fn = func;
1765 TAILQ_INSERT_TAIL(&aio_jobs, job, list);
1766 aio_kick_nowait(job->userproc);
1767 mtx_unlock(&aio_job_mtx);
1771 aio_cancel_sync(struct kaiocb *job)
1773 struct kaioinfo *ki;
1775 ki = job->userproc->p_aioinfo;
1777 if (!aio_cancel_cleared(job))
1778 TAILQ_REMOVE(&ki->kaio_syncqueue, job, list);
1784 aio_queue_file(struct file *fp, struct kaiocb *job)
1786 struct kaioinfo *ki;
1787 struct kaiocb *job2;
1793 ki = job->userproc->p_aioinfo;
1794 error = aio_qbio(job->userproc, job);
1798 if (fp->f_type == DTYPE_VNODE) {
1800 if (vp->v_type == VREG || vp->v_type == VDIR) {
1801 mp = fp->f_vnode->v_mount;
1802 if (mp == NULL || (mp->mnt_flag & MNT_LOCAL) != 0)
1806 if (!(safe || enable_aio_unsafe)) {
1807 counted_warning(&unsafe_warningcnt,
1808 "is attempting to use unsafe AIO requests");
1809 return (EOPNOTSUPP);
1812 if (job->uaiocb.aio_lio_opcode & (LIO_WRITE | LIO_READ)) {
1813 aio_schedule(job, aio_process_rw);
1815 } else if (job->uaiocb.aio_lio_opcode & LIO_SYNC) {
1817 TAILQ_FOREACH(job2, &ki->kaio_jobqueue, plist) {
1818 if (job2->fd_file == job->fd_file &&
1819 ((job2->uaiocb.aio_lio_opcode & LIO_SYNC) == 0) &&
1820 job2->seqno < job->seqno) {
1821 job2->jobflags |= KAIOCB_CHECKSYNC;
1825 if (job->pending != 0) {
1826 if (!aio_set_cancel_function_locked(job,
1832 TAILQ_INSERT_TAIL(&ki->kaio_syncqueue, job, list);
1837 aio_schedule(job, aio_process_sync);
1846 aio_kick_nowait(struct proc *userp)
1848 struct kaioinfo *ki = userp->p_aioinfo;
1849 struct aioproc *aiop;
1851 mtx_assert(&aio_job_mtx, MA_OWNED);
1852 if ((aiop = TAILQ_FIRST(&aio_freeproc)) != NULL) {
1853 TAILQ_REMOVE(&aio_freeproc, aiop, list);
1854 aiop->aioprocflags &= ~AIOP_FREE;
1855 wakeup(aiop->aioproc);
1856 } else if (num_aio_resv_start + num_aio_procs < max_aio_procs &&
1857 ki->kaio_active_count + num_aio_resv_start < max_aio_per_proc) {
1858 taskqueue_enqueue(taskqueue_aiod_kick, &ki->kaio_task);
1863 aio_kick(struct proc *userp)
1865 struct kaioinfo *ki = userp->p_aioinfo;
1866 struct aioproc *aiop;
1869 mtx_assert(&aio_job_mtx, MA_OWNED);
1871 if ((aiop = TAILQ_FIRST(&aio_freeproc)) != NULL) {
1872 TAILQ_REMOVE(&aio_freeproc, aiop, list);
1873 aiop->aioprocflags &= ~AIOP_FREE;
1874 wakeup(aiop->aioproc);
1875 } else if (num_aio_resv_start + num_aio_procs < max_aio_procs &&
1876 ki->kaio_active_count + num_aio_resv_start < max_aio_per_proc) {
1877 num_aio_resv_start++;
1878 mtx_unlock(&aio_job_mtx);
1879 error = aio_newproc(&num_aio_resv_start);
1880 mtx_lock(&aio_job_mtx);
1882 num_aio_resv_start--;
1892 aio_kick_helper(void *context, int pending)
1894 struct proc *userp = context;
1896 mtx_lock(&aio_job_mtx);
1897 while (--pending >= 0) {
1898 if (aio_kick(userp))
1901 mtx_unlock(&aio_job_mtx);
1905 * Support the aio_return system call, as a side-effect, kernel resources are
1909 kern_aio_return(struct thread *td, struct aiocb *ujob, struct aiocb_ops *ops)
1911 struct proc *p = td->td_proc;
1913 struct kaioinfo *ki;
1920 TAILQ_FOREACH(job, &ki->kaio_done, plist) {
1921 if (job->ujob == ujob)
1925 MPASS(job->jobflags & KAIOCB_FINISHED);
1926 status = job->uaiocb._aiocb_private.status;
1927 error = job->uaiocb._aiocb_private.error;
1928 td->td_retval[0] = status;
1929 td->td_ru.ru_oublock += job->outblock;
1930 td->td_ru.ru_inblock += job->inblock;
1931 td->td_ru.ru_msgsnd += job->msgsnd;
1932 td->td_ru.ru_msgrcv += job->msgrcv;
1933 aio_free_entry(job);
1935 ops->store_error(ujob, error);
1936 ops->store_status(ujob, status);
1945 sys_aio_return(struct thread *td, struct aio_return_args *uap)
1948 return (kern_aio_return(td, uap->aiocbp, &aiocb_ops));
1952 * Allow a process to wakeup when any of the I/O requests are completed.
1955 kern_aio_suspend(struct thread *td, int njoblist, struct aiocb **ujoblist,
1956 struct timespec *ts)
1958 struct proc *p = td->td_proc;
1960 struct kaioinfo *ki;
1961 struct kaiocb *firstjob, *job;
1966 if (ts->tv_nsec < 0 || ts->tv_nsec >= 1000000000)
1969 TIMESPEC_TO_TIMEVAL(&atv, ts);
1970 if (itimerfix(&atv))
1972 timo = tvtohz(&atv);
1986 TAILQ_FOREACH(job, &ki->kaio_all, allist) {
1987 for (i = 0; i < njoblist; i++) {
1988 if (job->ujob == ujoblist[i]) {
1989 if (firstjob == NULL)
1991 if (job->jobflags & KAIOCB_FINISHED)
1996 /* All tasks were finished. */
1997 if (firstjob == NULL)
2000 ki->kaio_flags |= KAIO_WAKEUP;
2001 error = msleep(&p->p_aioinfo, AIO_MTX(ki), PRIBIO | PCATCH,
2003 if (error == ERESTART)
2014 sys_aio_suspend(struct thread *td, struct aio_suspend_args *uap)
2016 struct timespec ts, *tsp;
2017 struct aiocb **ujoblist;
2020 if (uap->nent < 0 || uap->nent > max_aio_queue_per_proc)
2024 /* Get timespec struct. */
2025 if ((error = copyin(uap->timeout, &ts, sizeof(ts))) != 0)
2031 ujoblist = malloc(uap->nent * sizeof(ujoblist[0]), M_AIO, M_WAITOK);
2032 error = copyin(uap->aiocbp, ujoblist, uap->nent * sizeof(ujoblist[0]));
2034 error = kern_aio_suspend(td, uap->nent, ujoblist, tsp);
2035 free(ujoblist, M_AIO);
2040 * aio_cancel cancels any non-bio aio operations not currently in progress.
2043 sys_aio_cancel(struct thread *td, struct aio_cancel_args *uap)
2045 struct proc *p = td->td_proc;
2046 struct kaioinfo *ki;
2047 struct kaiocb *job, *jobn;
2051 int notcancelled = 0;
2054 /* Lookup file object. */
2055 error = fget(td, uap->fd, &cap_no_rights, &fp);
2063 if (fp->f_type == DTYPE_VNODE) {
2065 if (vn_isdisk(vp)) {
2067 td->td_retval[0] = AIO_NOTCANCELED;
2073 TAILQ_FOREACH_SAFE(job, &ki->kaio_jobqueue, plist, jobn) {
2074 if ((uap->fd == job->uaiocb.aio_fildes) &&
2075 ((uap->aiocbp == NULL) ||
2076 (uap->aiocbp == job->ujob))) {
2077 if (aio_cancel_job(p, ki, job)) {
2082 if (uap->aiocbp != NULL)
2091 if (uap->aiocbp != NULL) {
2093 td->td_retval[0] = AIO_CANCELED;
2099 td->td_retval[0] = AIO_NOTCANCELED;
2104 td->td_retval[0] = AIO_CANCELED;
2108 td->td_retval[0] = AIO_ALLDONE;
2114 * aio_error is implemented in the kernel level for compatibility purposes
2115 * only. For a user mode async implementation, it would be best to do it in
2116 * a userland subroutine.
2119 kern_aio_error(struct thread *td, struct aiocb *ujob, struct aiocb_ops *ops)
2121 struct proc *p = td->td_proc;
2123 struct kaioinfo *ki;
2128 td->td_retval[0] = EINVAL;
2133 TAILQ_FOREACH(job, &ki->kaio_all, allist) {
2134 if (job->ujob == ujob) {
2135 if (job->jobflags & KAIOCB_FINISHED)
2137 job->uaiocb._aiocb_private.error;
2139 td->td_retval[0] = EINPROGRESS;
2147 * Hack for failure of aio_aqueue.
2149 status = ops->fetch_status(ujob);
2151 td->td_retval[0] = ops->fetch_error(ujob);
2155 td->td_retval[0] = EINVAL;
2160 sys_aio_error(struct thread *td, struct aio_error_args *uap)
2163 return (kern_aio_error(td, uap->aiocbp, &aiocb_ops));
2166 /* syscall - asynchronous read from a file (REALTIME) */
2167 #ifdef COMPAT_FREEBSD6
2169 freebsd6_aio_read(struct thread *td, struct freebsd6_aio_read_args *uap)
2172 return (aio_aqueue(td, (struct aiocb *)uap->aiocbp, NULL, LIO_READ,
2173 &aiocb_ops_osigevent));
2178 sys_aio_read(struct thread *td, struct aio_read_args *uap)
2181 return (aio_aqueue(td, uap->aiocbp, NULL, LIO_READ, &aiocb_ops));
2185 sys_aio_readv(struct thread *td, struct aio_readv_args *uap)
2188 return (aio_aqueue(td, uap->aiocbp, NULL, LIO_READV, &aiocb_ops));
2191 /* syscall - asynchronous write to a file (REALTIME) */
2192 #ifdef COMPAT_FREEBSD6
2194 freebsd6_aio_write(struct thread *td, struct freebsd6_aio_write_args *uap)
2197 return (aio_aqueue(td, (struct aiocb *)uap->aiocbp, NULL, LIO_WRITE,
2198 &aiocb_ops_osigevent));
2203 sys_aio_write(struct thread *td, struct aio_write_args *uap)
2206 return (aio_aqueue(td, uap->aiocbp, NULL, LIO_WRITE, &aiocb_ops));
2210 sys_aio_writev(struct thread *td, struct aio_writev_args *uap)
2213 return (aio_aqueue(td, uap->aiocbp, NULL, LIO_WRITEV, &aiocb_ops));
2217 sys_aio_mlock(struct thread *td, struct aio_mlock_args *uap)
2220 return (aio_aqueue(td, uap->aiocbp, NULL, LIO_MLOCK, &aiocb_ops));
2224 kern_lio_listio(struct thread *td, int mode, struct aiocb * const *uacb_list,
2225 struct aiocb **acb_list, int nent, struct sigevent *sig,
2226 struct aiocb_ops *ops)
2228 struct proc *p = td->td_proc;
2230 struct kaioinfo *ki;
2231 struct aioliojob *lj;
2237 if ((mode != LIO_NOWAIT) && (mode != LIO_WAIT))
2240 if (nent < 0 || nent > max_aio_queue_per_proc)
2243 if (p->p_aioinfo == NULL)
2244 aio_init_aioinfo(p);
2248 lj = uma_zalloc(aiolio_zone, M_WAITOK);
2251 lj->lioj_finished_count = 0;
2252 lj->lioj_signal.sigev_notify = SIGEV_NONE;
2253 knlist_init_mtx(&lj->klist, AIO_MTX(ki));
2254 ksiginfo_init(&lj->lioj_ksi);
2259 if (sig && (mode == LIO_NOWAIT)) {
2260 bcopy(sig, &lj->lioj_signal, sizeof(lj->lioj_signal));
2261 if (lj->lioj_signal.sigev_notify == SIGEV_KEVENT) {
2262 /* Assume only new style KEVENT */
2263 memset(&kev, 0, sizeof(kev));
2264 kev.filter = EVFILT_LIO;
2265 kev.flags = EV_ADD | EV_ENABLE | EV_FLAG1;
2266 kev.ident = (uintptr_t)uacb_list; /* something unique */
2267 kev.data = (intptr_t)lj;
2268 /* pass user defined sigval data */
2269 kev.udata = lj->lioj_signal.sigev_value.sival_ptr;
2270 error = kqfd_register(
2271 lj->lioj_signal.sigev_notify_kqueue, &kev, td,
2274 uma_zfree(aiolio_zone, lj);
2277 } else if (lj->lioj_signal.sigev_notify == SIGEV_NONE) {
2279 } else if (lj->lioj_signal.sigev_notify == SIGEV_SIGNAL ||
2280 lj->lioj_signal.sigev_notify == SIGEV_THREAD_ID) {
2281 if (!_SIG_VALID(lj->lioj_signal.sigev_signo)) {
2282 uma_zfree(aiolio_zone, lj);
2285 lj->lioj_flags |= LIOJ_SIGNAL;
2287 uma_zfree(aiolio_zone, lj);
2293 TAILQ_INSERT_TAIL(&ki->kaio_liojoblist, lj, lioj_list);
2295 * Add extra aiocb count to avoid the lio to be freed
2296 * by other threads doing aio_waitcomplete or aio_return,
2297 * and prevent event from being sent until we have queued
2304 * Get pointers to the list of I/O requests.
2308 for (i = 0; i < nent; i++) {
2311 error = aio_aqueue(td, job, lj, LIO_NOP, ops);
2312 if (error == EAGAIN)
2314 else if (error != 0)
2321 if (mode == LIO_WAIT) {
2322 while (lj->lioj_count - 1 != lj->lioj_finished_count) {
2323 ki->kaio_flags |= KAIO_WAKEUP;
2324 error = msleep(&p->p_aioinfo, AIO_MTX(ki),
2325 PRIBIO | PCATCH, "aiospn", 0);
2326 if (error == ERESTART)
2332 if (lj->lioj_count - 1 == lj->lioj_finished_count) {
2333 if (lj->lioj_signal.sigev_notify == SIGEV_KEVENT) {
2334 lj->lioj_flags |= LIOJ_KEVENT_POSTED;
2335 KNOTE_LOCKED(&lj->klist, 1);
2337 if ((lj->lioj_flags & (LIOJ_SIGNAL |
2338 LIOJ_SIGNAL_POSTED)) == LIOJ_SIGNAL &&
2339 (lj->lioj_signal.sigev_notify == SIGEV_SIGNAL ||
2340 lj->lioj_signal.sigev_notify == SIGEV_THREAD_ID)) {
2341 aio_sendsig(p, &lj->lioj_signal, &lj->lioj_ksi,
2342 lj->lioj_count != 1);
2343 lj->lioj_flags |= LIOJ_SIGNAL_POSTED;
2348 if (lj->lioj_count == 0) {
2349 TAILQ_REMOVE(&ki->kaio_liojoblist, lj, lioj_list);
2350 knlist_delete(&lj->klist, curthread, 1);
2352 sigqueue_take(&lj->lioj_ksi);
2355 uma_zfree(aiolio_zone, lj);
2367 /* syscall - list directed I/O (REALTIME) */
2368 #ifdef COMPAT_FREEBSD6
2370 freebsd6_lio_listio(struct thread *td, struct freebsd6_lio_listio_args *uap)
2372 struct aiocb **acb_list;
2373 struct sigevent *sigp, sig;
2374 struct osigevent osig;
2377 if ((uap->mode != LIO_NOWAIT) && (uap->mode != LIO_WAIT))
2381 if (nent < 0 || nent > max_aio_queue_per_proc)
2384 if (uap->sig && (uap->mode == LIO_NOWAIT)) {
2385 error = copyin(uap->sig, &osig, sizeof(osig));
2388 error = convert_old_sigevent(&osig, &sig);
2395 acb_list = malloc(sizeof(struct aiocb *) * nent, M_LIO, M_WAITOK);
2396 error = copyin(uap->acb_list, acb_list, nent * sizeof(acb_list[0]));
2398 error = kern_lio_listio(td, uap->mode,
2399 (struct aiocb * const *)uap->acb_list, acb_list, nent, sigp,
2400 &aiocb_ops_osigevent);
2401 free(acb_list, M_LIO);
2406 /* syscall - list directed I/O (REALTIME) */
2408 sys_lio_listio(struct thread *td, struct lio_listio_args *uap)
2410 struct aiocb **acb_list;
2411 struct sigevent *sigp, sig;
2414 if ((uap->mode != LIO_NOWAIT) && (uap->mode != LIO_WAIT))
2418 if (nent < 0 || nent > max_aio_queue_per_proc)
2421 if (uap->sig && (uap->mode == LIO_NOWAIT)) {
2422 error = copyin(uap->sig, &sig, sizeof(sig));
2429 acb_list = malloc(sizeof(struct aiocb *) * nent, M_LIO, M_WAITOK);
2430 error = copyin(uap->acb_list, acb_list, nent * sizeof(acb_list[0]));
2432 error = kern_lio_listio(td, uap->mode, uap->acb_list, acb_list,
2433 nent, sigp, &aiocb_ops);
2434 free(acb_list, M_LIO);
2439 aio_biocleanup(struct bio *bp)
2441 struct kaiocb *job = (struct kaiocb *)bp->bio_caller1;
2442 struct kaioinfo *ki;
2443 struct buf *pbuf = (struct buf *)bp->bio_caller2;
2445 /* Release mapping into kernel space. */
2447 MPASS(pbuf->b_npages <= atop(maxphys) + 1);
2448 pmap_qremove((vm_offset_t)pbuf->b_data, pbuf->b_npages);
2449 vm_page_unhold_pages(pbuf->b_pages, pbuf->b_npages);
2450 uma_zfree(pbuf_zone, pbuf);
2451 atomic_subtract_int(&num_buf_aio, 1);
2452 ki = job->userproc->p_aioinfo;
2454 ki->kaio_buffer_count--;
2457 MPASS(bp->bio_ma_n <= atop(maxphys) + 1);
2458 vm_page_unhold_pages(bp->bio_ma, bp->bio_ma_n);
2459 free(bp->bio_ma, M_TEMP);
2460 atomic_subtract_int(&num_unmapped_aio, 1);
2466 aio_biowakeup(struct bio *bp)
2468 struct kaiocb *job = (struct kaiocb *)bp->bio_caller1;
2470 long bcount = bp->bio_bcount;
2471 long resid = bp->bio_resid;
2473 int bio_error = bp->bio_error;
2474 uint16_t flags = bp->bio_flags;
2476 opcode = job->uaiocb.aio_lio_opcode;
2480 nbytes = bcount - resid;
2481 atomic_add_acq_long(&job->nbytes, nbytes);
2482 nblks = btodb(nbytes);
2485 * If multiple bios experienced an error, the job will reflect the
2486 * error of whichever failed bio completed last.
2488 if (flags & BIO_ERROR)
2489 atomic_store_int(&job->error, bio_error);
2490 if (opcode & LIO_WRITE)
2491 atomic_add_int(&job->outblock, nblks);
2493 atomic_add_int(&job->inblock, nblks);
2495 if (refcount_release(&job->nbio)) {
2496 bio_error = atomic_load_int(&job->error);
2498 aio_complete(job, -1, bio_error);
2500 aio_complete(job, atomic_load_long(&job->nbytes), 0);
2504 /* syscall - wait for the next completion of an aio request */
2506 kern_aio_waitcomplete(struct thread *td, struct aiocb **ujobp,
2507 struct timespec *ts, struct aiocb_ops *ops)
2509 struct proc *p = td->td_proc;
2511 struct kaioinfo *ki;
2517 ops->store_aiocb(ujobp, NULL);
2521 } else if (ts->tv_sec == 0 && ts->tv_nsec == 0) {
2524 if ((ts->tv_nsec < 0) || (ts->tv_nsec >= 1000000000))
2527 TIMESPEC_TO_TIMEVAL(&atv, ts);
2528 if (itimerfix(&atv))
2530 timo = tvtohz(&atv);
2533 if (p->p_aioinfo == NULL)
2534 aio_init_aioinfo(p);
2540 while ((job = TAILQ_FIRST(&ki->kaio_done)) == NULL) {
2542 error = EWOULDBLOCK;
2545 ki->kaio_flags |= KAIO_WAKEUP;
2546 error = msleep(&p->p_aioinfo, AIO_MTX(ki), PRIBIO | PCATCH,
2548 if (timo && error == ERESTART)
2555 MPASS(job->jobflags & KAIOCB_FINISHED);
2557 status = job->uaiocb._aiocb_private.status;
2558 error = job->uaiocb._aiocb_private.error;
2559 td->td_retval[0] = status;
2560 td->td_ru.ru_oublock += job->outblock;
2561 td->td_ru.ru_inblock += job->inblock;
2562 td->td_ru.ru_msgsnd += job->msgsnd;
2563 td->td_ru.ru_msgrcv += job->msgrcv;
2564 aio_free_entry(job);
2566 ops->store_aiocb(ujobp, ujob);
2567 ops->store_error(ujob, error);
2568 ops->store_status(ujob, status);
2576 sys_aio_waitcomplete(struct thread *td, struct aio_waitcomplete_args *uap)
2578 struct timespec ts, *tsp;
2582 /* Get timespec struct. */
2583 error = copyin(uap->timeout, &ts, sizeof(ts));
2590 return (kern_aio_waitcomplete(td, uap->aiocbp, tsp, &aiocb_ops));
2594 kern_aio_fsync(struct thread *td, int op, struct aiocb *ujob,
2595 struct aiocb_ops *ops)
2610 return (aio_aqueue(td, ujob, NULL, listop, ops));
2614 sys_aio_fsync(struct thread *td, struct aio_fsync_args *uap)
2617 return (kern_aio_fsync(td, uap->op, uap->aiocbp, &aiocb_ops));
2620 /* kqueue attach function */
2622 filt_aioattach(struct knote *kn)
2626 job = (struct kaiocb *)(uintptr_t)kn->kn_sdata;
2629 * The job pointer must be validated before using it, so
2630 * registration is restricted to the kernel; the user cannot
2633 if ((kn->kn_flags & EV_FLAG1) == 0)
2635 kn->kn_ptr.p_aio = job;
2636 kn->kn_flags &= ~EV_FLAG1;
2638 knlist_add(&job->klist, kn, 0);
2643 /* kqueue detach function */
2645 filt_aiodetach(struct knote *kn)
2649 knl = &kn->kn_ptr.p_aio->klist;
2650 knl->kl_lock(knl->kl_lockarg);
2651 if (!knlist_empty(knl))
2652 knlist_remove(knl, kn, 1);
2653 knl->kl_unlock(knl->kl_lockarg);
2656 /* kqueue filter function */
2659 filt_aio(struct knote *kn, long hint)
2661 struct kaiocb *job = kn->kn_ptr.p_aio;
2663 kn->kn_data = job->uaiocb._aiocb_private.error;
2664 if (!(job->jobflags & KAIOCB_FINISHED))
2666 kn->kn_flags |= EV_EOF;
2670 /* kqueue attach function */
2672 filt_lioattach(struct knote *kn)
2674 struct aioliojob *lj;
2676 lj = (struct aioliojob *)(uintptr_t)kn->kn_sdata;
2679 * The aioliojob pointer must be validated before using it, so
2680 * registration is restricted to the kernel; the user cannot
2683 if ((kn->kn_flags & EV_FLAG1) == 0)
2685 kn->kn_ptr.p_lio = lj;
2686 kn->kn_flags &= ~EV_FLAG1;
2688 knlist_add(&lj->klist, kn, 0);
2693 /* kqueue detach function */
2695 filt_liodetach(struct knote *kn)
2699 knl = &kn->kn_ptr.p_lio->klist;
2700 knl->kl_lock(knl->kl_lockarg);
2701 if (!knlist_empty(knl))
2702 knlist_remove(knl, kn, 1);
2703 knl->kl_unlock(knl->kl_lockarg);
2706 /* kqueue filter function */
2709 filt_lio(struct knote *kn, long hint)
2711 struct aioliojob * lj = kn->kn_ptr.p_lio;
2713 return (lj->lioj_flags & LIOJ_KEVENT_POSTED);
2716 #ifdef COMPAT_FREEBSD32
2717 #include <sys/mount.h>
2718 #include <sys/socket.h>
2719 #include <sys/sysent.h>
2720 #include <compat/freebsd32/freebsd32.h>
2721 #include <compat/freebsd32/freebsd32_proto.h>
2722 #include <compat/freebsd32/freebsd32_signal.h>
2723 #include <compat/freebsd32/freebsd32_syscall.h>
2724 #include <compat/freebsd32/freebsd32_util.h>
2726 struct __aiocb_private32 {
2729 uint32_t kernelinfo;
2732 #ifdef COMPAT_FREEBSD6
2733 typedef struct oaiocb32 {
2734 int aio_fildes; /* File descriptor */
2735 uint64_t aio_offset __packed; /* File offset for I/O */
2736 uint32_t aio_buf; /* I/O buffer in process space */
2737 uint32_t aio_nbytes; /* Number of bytes for I/O */
2738 struct osigevent32 aio_sigevent; /* Signal to deliver */
2739 int aio_lio_opcode; /* LIO opcode */
2740 int aio_reqprio; /* Request priority -- ignored */
2741 struct __aiocb_private32 _aiocb_private;
2745 typedef struct aiocb32 {
2746 int32_t aio_fildes; /* File descriptor */
2747 uint64_t aio_offset __packed; /* File offset for I/O */
2748 uint32_t aio_buf; /* I/O buffer in process space */
2749 uint32_t aio_nbytes; /* Number of bytes for I/O */
2751 uint32_t __spare2__;
2752 int aio_lio_opcode; /* LIO opcode */
2753 int aio_reqprio; /* Request priority -- ignored */
2754 struct __aiocb_private32 _aiocb_private;
2755 struct sigevent32 aio_sigevent; /* Signal to deliver */
2758 #ifdef COMPAT_FREEBSD6
2760 convert_old_sigevent32(struct osigevent32 *osig, struct sigevent *nsig)
2764 * Only SIGEV_NONE, SIGEV_SIGNAL, and SIGEV_KEVENT are
2765 * supported by AIO with the old sigevent structure.
2767 CP(*osig, *nsig, sigev_notify);
2768 switch (nsig->sigev_notify) {
2772 nsig->sigev_signo = osig->__sigev_u.__sigev_signo;
2775 nsig->sigev_notify_kqueue =
2776 osig->__sigev_u.__sigev_notify_kqueue;
2777 PTRIN_CP(*osig, *nsig, sigev_value.sival_ptr);
2786 aiocb32_copyin_old_sigevent(struct aiocb *ujob, struct kaiocb *kjob,
2789 struct oaiocb32 job32;
2790 struct aiocb *kcb = &kjob->uaiocb;
2793 bzero(kcb, sizeof(struct aiocb));
2794 error = copyin(ujob, &job32, sizeof(job32));
2798 /* No need to copyin aio_iov, because it did not exist in FreeBSD 6 */
2800 CP(job32, *kcb, aio_fildes);
2801 CP(job32, *kcb, aio_offset);
2802 PTRIN_CP(job32, *kcb, aio_buf);
2803 CP(job32, *kcb, aio_nbytes);
2804 CP(job32, *kcb, aio_lio_opcode);
2805 CP(job32, *kcb, aio_reqprio);
2806 CP(job32, *kcb, _aiocb_private.status);
2807 CP(job32, *kcb, _aiocb_private.error);
2808 PTRIN_CP(job32, *kcb, _aiocb_private.kernelinfo);
2809 return (convert_old_sigevent32(&job32.aio_sigevent,
2810 &kcb->aio_sigevent));
2815 aiocb32_copyin(struct aiocb *ujob, struct kaiocb *kjob, int type)
2817 struct aiocb32 job32;
2818 struct aiocb *kcb = &kjob->uaiocb;
2819 struct iovec32 *iov32;
2822 error = copyin(ujob, &job32, sizeof(job32));
2825 CP(job32, *kcb, aio_fildes);
2826 CP(job32, *kcb, aio_offset);
2827 CP(job32, *kcb, aio_lio_opcode);
2828 if (type == LIO_NOP)
2829 type = kcb->aio_lio_opcode;
2830 if (type & LIO_VECTORED) {
2831 iov32 = PTRIN(job32.aio_iov);
2832 CP(job32, *kcb, aio_iovcnt);
2833 /* malloc a uio and copy in the iovec */
2834 error = freebsd32_copyinuio(iov32,
2835 kcb->aio_iovcnt, &kjob->uiop);
2839 PTRIN_CP(job32, *kcb, aio_buf);
2840 CP(job32, *kcb, aio_nbytes);
2842 CP(job32, *kcb, aio_reqprio);
2843 CP(job32, *kcb, _aiocb_private.status);
2844 CP(job32, *kcb, _aiocb_private.error);
2845 PTRIN_CP(job32, *kcb, _aiocb_private.kernelinfo);
2846 error = convert_sigevent32(&job32.aio_sigevent, &kcb->aio_sigevent);
2852 aiocb32_fetch_status(struct aiocb *ujob)
2854 struct aiocb32 *ujob32;
2856 ujob32 = (struct aiocb32 *)ujob;
2857 return (fuword32(&ujob32->_aiocb_private.status));
2861 aiocb32_fetch_error(struct aiocb *ujob)
2863 struct aiocb32 *ujob32;
2865 ujob32 = (struct aiocb32 *)ujob;
2866 return (fuword32(&ujob32->_aiocb_private.error));
2870 aiocb32_store_status(struct aiocb *ujob, long status)
2872 struct aiocb32 *ujob32;
2874 ujob32 = (struct aiocb32 *)ujob;
2875 return (suword32(&ujob32->_aiocb_private.status, status));
2879 aiocb32_store_error(struct aiocb *ujob, long error)
2881 struct aiocb32 *ujob32;
2883 ujob32 = (struct aiocb32 *)ujob;
2884 return (suword32(&ujob32->_aiocb_private.error, error));
2888 aiocb32_store_kernelinfo(struct aiocb *ujob, long jobref)
2890 struct aiocb32 *ujob32;
2892 ujob32 = (struct aiocb32 *)ujob;
2893 return (suword32(&ujob32->_aiocb_private.kernelinfo, jobref));
2897 aiocb32_store_aiocb(struct aiocb **ujobp, struct aiocb *ujob)
2900 return (suword32(ujobp, (long)ujob));
2903 static struct aiocb_ops aiocb32_ops = {
2904 .aio_copyin = aiocb32_copyin,
2905 .fetch_status = aiocb32_fetch_status,
2906 .fetch_error = aiocb32_fetch_error,
2907 .store_status = aiocb32_store_status,
2908 .store_error = aiocb32_store_error,
2909 .store_kernelinfo = aiocb32_store_kernelinfo,
2910 .store_aiocb = aiocb32_store_aiocb,
2913 #ifdef COMPAT_FREEBSD6
2914 static struct aiocb_ops aiocb32_ops_osigevent = {
2915 .aio_copyin = aiocb32_copyin_old_sigevent,
2916 .fetch_status = aiocb32_fetch_status,
2917 .fetch_error = aiocb32_fetch_error,
2918 .store_status = aiocb32_store_status,
2919 .store_error = aiocb32_store_error,
2920 .store_kernelinfo = aiocb32_store_kernelinfo,
2921 .store_aiocb = aiocb32_store_aiocb,
2926 freebsd32_aio_return(struct thread *td, struct freebsd32_aio_return_args *uap)
2929 return (kern_aio_return(td, (struct aiocb *)uap->aiocbp, &aiocb32_ops));
2933 freebsd32_aio_suspend(struct thread *td, struct freebsd32_aio_suspend_args *uap)
2935 struct timespec32 ts32;
2936 struct timespec ts, *tsp;
2937 struct aiocb **ujoblist;
2938 uint32_t *ujoblist32;
2941 if (uap->nent < 0 || uap->nent > max_aio_queue_per_proc)
2945 /* Get timespec struct. */
2946 if ((error = copyin(uap->timeout, &ts32, sizeof(ts32))) != 0)
2948 CP(ts32, ts, tv_sec);
2949 CP(ts32, ts, tv_nsec);
2954 ujoblist = malloc(uap->nent * sizeof(ujoblist[0]), M_AIO, M_WAITOK);
2955 ujoblist32 = (uint32_t *)ujoblist;
2956 error = copyin(uap->aiocbp, ujoblist32, uap->nent *
2957 sizeof(ujoblist32[0]));
2959 for (i = uap->nent - 1; i >= 0; i--)
2960 ujoblist[i] = PTRIN(ujoblist32[i]);
2962 error = kern_aio_suspend(td, uap->nent, ujoblist, tsp);
2964 free(ujoblist, M_AIO);
2969 freebsd32_aio_error(struct thread *td, struct freebsd32_aio_error_args *uap)
2972 return (kern_aio_error(td, (struct aiocb *)uap->aiocbp, &aiocb32_ops));
2975 #ifdef COMPAT_FREEBSD6
2977 freebsd6_freebsd32_aio_read(struct thread *td,
2978 struct freebsd6_freebsd32_aio_read_args *uap)
2981 return (aio_aqueue(td, (struct aiocb *)uap->aiocbp, NULL, LIO_READ,
2982 &aiocb32_ops_osigevent));
2987 freebsd32_aio_read(struct thread *td, struct freebsd32_aio_read_args *uap)
2990 return (aio_aqueue(td, (struct aiocb *)uap->aiocbp, NULL, LIO_READ,
2995 freebsd32_aio_readv(struct thread *td, struct freebsd32_aio_readv_args *uap)
2998 return (aio_aqueue(td, (struct aiocb *)uap->aiocbp, NULL, LIO_READV,
3002 #ifdef COMPAT_FREEBSD6
3004 freebsd6_freebsd32_aio_write(struct thread *td,
3005 struct freebsd6_freebsd32_aio_write_args *uap)
3008 return (aio_aqueue(td, (struct aiocb *)uap->aiocbp, NULL, LIO_WRITE,
3009 &aiocb32_ops_osigevent));
3014 freebsd32_aio_write(struct thread *td, struct freebsd32_aio_write_args *uap)
3017 return (aio_aqueue(td, (struct aiocb *)uap->aiocbp, NULL, LIO_WRITE,
3022 freebsd32_aio_writev(struct thread *td, struct freebsd32_aio_writev_args *uap)
3025 return (aio_aqueue(td, (struct aiocb *)uap->aiocbp, NULL, LIO_WRITEV,
3030 freebsd32_aio_mlock(struct thread *td, struct freebsd32_aio_mlock_args *uap)
3033 return (aio_aqueue(td, (struct aiocb *)uap->aiocbp, NULL, LIO_MLOCK,
3038 freebsd32_aio_waitcomplete(struct thread *td,
3039 struct freebsd32_aio_waitcomplete_args *uap)
3041 struct timespec32 ts32;
3042 struct timespec ts, *tsp;
3046 /* Get timespec struct. */
3047 error = copyin(uap->timeout, &ts32, sizeof(ts32));
3050 CP(ts32, ts, tv_sec);
3051 CP(ts32, ts, tv_nsec);
3056 return (kern_aio_waitcomplete(td, (struct aiocb **)uap->aiocbp, tsp,
3061 freebsd32_aio_fsync(struct thread *td, struct freebsd32_aio_fsync_args *uap)
3064 return (kern_aio_fsync(td, uap->op, (struct aiocb *)uap->aiocbp,
3068 #ifdef COMPAT_FREEBSD6
3070 freebsd6_freebsd32_lio_listio(struct thread *td,
3071 struct freebsd6_freebsd32_lio_listio_args *uap)
3073 struct aiocb **acb_list;
3074 struct sigevent *sigp, sig;
3075 struct osigevent32 osig;
3076 uint32_t *acb_list32;
3079 if ((uap->mode != LIO_NOWAIT) && (uap->mode != LIO_WAIT))
3083 if (nent < 0 || nent > max_aio_queue_per_proc)
3086 if (uap->sig && (uap->mode == LIO_NOWAIT)) {
3087 error = copyin(uap->sig, &osig, sizeof(osig));
3090 error = convert_old_sigevent32(&osig, &sig);
3097 acb_list32 = malloc(sizeof(uint32_t) * nent, M_LIO, M_WAITOK);
3098 error = copyin(uap->acb_list, acb_list32, nent * sizeof(uint32_t));
3100 free(acb_list32, M_LIO);
3103 acb_list = malloc(sizeof(struct aiocb *) * nent, M_LIO, M_WAITOK);
3104 for (i = 0; i < nent; i++)
3105 acb_list[i] = PTRIN(acb_list32[i]);
3106 free(acb_list32, M_LIO);
3108 error = kern_lio_listio(td, uap->mode,
3109 (struct aiocb * const *)uap->acb_list, acb_list, nent, sigp,
3110 &aiocb32_ops_osigevent);
3111 free(acb_list, M_LIO);
3117 freebsd32_lio_listio(struct thread *td, struct freebsd32_lio_listio_args *uap)
3119 struct aiocb **acb_list;
3120 struct sigevent *sigp, sig;
3121 struct sigevent32 sig32;
3122 uint32_t *acb_list32;
3125 if ((uap->mode != LIO_NOWAIT) && (uap->mode != LIO_WAIT))
3129 if (nent < 0 || nent > max_aio_queue_per_proc)
3132 if (uap->sig && (uap->mode == LIO_NOWAIT)) {
3133 error = copyin(uap->sig, &sig32, sizeof(sig32));
3136 error = convert_sigevent32(&sig32, &sig);
3143 acb_list32 = malloc(sizeof(uint32_t) * nent, M_LIO, M_WAITOK);
3144 error = copyin(uap->acb_list, acb_list32, nent * sizeof(uint32_t));
3146 free(acb_list32, M_LIO);
3149 acb_list = malloc(sizeof(struct aiocb *) * nent, M_LIO, M_WAITOK);
3150 for (i = 0; i < nent; i++)
3151 acb_list[i] = PTRIN(acb_list32[i]);
3152 free(acb_list32, M_LIO);
3154 error = kern_lio_listio(td, uap->mode,
3155 (struct aiocb * const *)uap->acb_list, acb_list, nent, sigp,
3157 free(acb_list, M_LIO);