2 * SPDX-License-Identifier: BSD-2-Clause-FreeBSD
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/cdefs.h>
24 __FBSDID("$FreeBSD$");
26 #include <sys/param.h>
27 #include <sys/systm.h>
28 #include <sys/malloc.h>
31 #include <sys/capsicum.h>
32 #include <sys/eventhandler.h>
33 #include <sys/sysproto.h>
34 #include <sys/filedesc.h>
35 #include <sys/kernel.h>
36 #include <sys/module.h>
37 #include <sys/kthread.h>
38 #include <sys/fcntl.h>
40 #include <sys/limits.h>
42 #include <sys/mutex.h>
43 #include <sys/unistd.h>
44 #include <sys/posix4.h>
46 #include <sys/resourcevar.h>
47 #include <sys/signalvar.h>
48 #include <sys/syscallsubr.h>
49 #include <sys/protosw.h>
50 #include <sys/rwlock.h>
52 #include <sys/socket.h>
53 #include <sys/socketvar.h>
54 #include <sys/syscall.h>
55 #include <sys/sysctl.h>
56 #include <sys/syslog.h>
58 #include <sys/taskqueue.h>
59 #include <sys/vnode.h>
61 #include <sys/event.h>
62 #include <sys/mount.h>
63 #include <geom/geom.h>
65 #include <machine/atomic.h>
68 #include <vm/vm_page.h>
69 #include <vm/vm_extern.h>
71 #include <vm/vm_map.h>
72 #include <vm/vm_object.h>
77 * Counter for allocating reference ids to new jobs. Wrapped to 1 on
78 * overflow. (XXX will be removed soon.)
80 static u_long jobrefid;
83 * Counter for aio_fsync.
85 static uint64_t jobseqno;
87 #ifndef MAX_AIO_PER_PROC
88 #define MAX_AIO_PER_PROC 32
91 #ifndef MAX_AIO_QUEUE_PER_PROC
92 #define MAX_AIO_QUEUE_PER_PROC 256
96 #define MAX_AIO_QUEUE 1024 /* Bigger than MAX_AIO_QUEUE_PER_PROC */
100 #define MAX_BUF_AIO 16
103 FEATURE(aio, "Asynchronous I/O");
104 SYSCTL_DECL(_p1003_1b);
106 static MALLOC_DEFINE(M_LIO, "lio", "listio aio control block list");
107 static MALLOC_DEFINE(M_AIO, "aio", "structures for asynchronous I/O");
109 static SYSCTL_NODE(_vfs, OID_AUTO, aio, CTLFLAG_RW | CTLFLAG_MPSAFE, 0,
110 "Async IO management");
112 static int enable_aio_unsafe = 0;
113 SYSCTL_INT(_vfs_aio, OID_AUTO, enable_unsafe, CTLFLAG_RW, &enable_aio_unsafe, 0,
114 "Permit asynchronous IO on all file types, not just known-safe types");
116 static unsigned int unsafe_warningcnt = 1;
117 SYSCTL_UINT(_vfs_aio, OID_AUTO, unsafe_warningcnt, CTLFLAG_RW,
118 &unsafe_warningcnt, 0,
119 "Warnings that will be triggered upon failed IO requests on unsafe files");
121 static int max_aio_procs = MAX_AIO_PROCS;
122 SYSCTL_INT(_vfs_aio, OID_AUTO, max_aio_procs, CTLFLAG_RW, &max_aio_procs, 0,
123 "Maximum number of kernel processes to use for handling async IO ");
125 static int num_aio_procs = 0;
126 SYSCTL_INT(_vfs_aio, OID_AUTO, num_aio_procs, CTLFLAG_RD, &num_aio_procs, 0,
127 "Number of presently active kernel processes for async IO");
130 * The code will adjust the actual number of AIO processes towards this
131 * number when it gets a chance.
133 static int target_aio_procs = TARGET_AIO_PROCS;
134 SYSCTL_INT(_vfs_aio, OID_AUTO, target_aio_procs, CTLFLAG_RW, &target_aio_procs,
136 "Preferred number of ready kernel processes for async IO");
138 static int max_queue_count = MAX_AIO_QUEUE;
139 SYSCTL_INT(_vfs_aio, OID_AUTO, max_aio_queue, CTLFLAG_RW, &max_queue_count, 0,
140 "Maximum number of aio requests to queue, globally");
142 static int num_queue_count = 0;
143 SYSCTL_INT(_vfs_aio, OID_AUTO, num_queue_count, CTLFLAG_RD, &num_queue_count, 0,
144 "Number of queued aio requests");
146 static int num_buf_aio = 0;
147 SYSCTL_INT(_vfs_aio, OID_AUTO, num_buf_aio, CTLFLAG_RD, &num_buf_aio, 0,
148 "Number of aio requests presently handled by the buf subsystem");
150 static int num_unmapped_aio = 0;
151 SYSCTL_INT(_vfs_aio, OID_AUTO, num_unmapped_aio, CTLFLAG_RD, &num_unmapped_aio,
153 "Number of aio requests presently handled by unmapped I/O buffers");
155 /* Number of async I/O processes in the process of being started */
156 /* XXX This should be local to aio_aqueue() */
157 static int num_aio_resv_start = 0;
159 static int aiod_lifetime;
160 SYSCTL_INT(_vfs_aio, OID_AUTO, aiod_lifetime, CTLFLAG_RW, &aiod_lifetime, 0,
161 "Maximum lifetime for idle aiod");
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,
166 "Maximum active aio requests per process");
168 static int max_aio_queue_per_proc = MAX_AIO_QUEUE_PER_PROC;
169 SYSCTL_INT(_vfs_aio, OID_AUTO, max_aio_queue_per_proc, CTLFLAG_RW,
170 &max_aio_queue_per_proc, 0,
171 "Maximum queued aio requests per process");
173 static int max_buf_aio = MAX_BUF_AIO;
174 SYSCTL_INT(_vfs_aio, OID_AUTO, max_buf_aio, CTLFLAG_RW, &max_buf_aio, 0,
175 "Maximum buf aio requests per process");
178 * Though redundant with vfs.aio.max_aio_queue_per_proc, POSIX requires
179 * sysconf(3) to support AIO_LISTIO_MAX, and we implement that with
180 * vfs.aio.aio_listio_max.
182 SYSCTL_INT(_p1003_1b, CTL_P1003_1B_AIO_LISTIO_MAX, aio_listio_max,
183 CTLFLAG_RD | CTLFLAG_CAPRD, &max_aio_queue_per_proc,
184 0, "Maximum aio requests for a single lio_listio call");
186 #ifdef COMPAT_FREEBSD6
187 typedef struct oaiocb {
188 int aio_fildes; /* File descriptor */
189 off_t aio_offset; /* File offset for I/O */
190 volatile void *aio_buf; /* I/O buffer in process space */
191 size_t aio_nbytes; /* Number of bytes for I/O */
192 struct osigevent aio_sigevent; /* Signal to deliver */
193 int aio_lio_opcode; /* LIO opcode */
194 int aio_reqprio; /* Request priority -- ignored */
195 struct __aiocb_private _aiocb_private;
200 * Below is a key of locks used to protect each member of struct kaiocb
201 * aioliojob and kaioinfo and any backends.
203 * * - need not protected
204 * a - locked by kaioinfo lock
205 * b - locked by backend lock, the backend lock can be null in some cases,
206 * for example, BIO belongs to this type, in this case, proc lock is
208 * c - locked by aio_job_mtx, the lock for the generic file I/O backend.
212 * If the routine that services an AIO request blocks while running in an
213 * AIO kernel process it can starve other I/O requests. BIO requests
214 * queued via aio_qbio() complete asynchronously and do not use AIO kernel
215 * processes at all. Socket I/O requests use a separate pool of
216 * kprocs and also force non-blocking I/O. Other file I/O requests
217 * use the generic fo_read/fo_write operations which can block. The
218 * fsync and mlock operations can also block while executing. Ideally
219 * none of these requests would block while executing.
221 * Note that the service routines cannot toggle O_NONBLOCK in the file
222 * structure directly while handling a request due to races with
227 #define KAIOCB_QUEUEING 0x01
228 #define KAIOCB_CANCELLED 0x02
229 #define KAIOCB_CANCELLING 0x04
230 #define KAIOCB_CHECKSYNC 0x08
231 #define KAIOCB_CLEARED 0x10
232 #define KAIOCB_FINISHED 0x20
237 #define AIOP_FREE 0x1 /* proc on free queue */
240 int aioprocflags; /* (c) AIO proc flags */
241 TAILQ_ENTRY(aioproc) list; /* (c) list of processes */
242 struct proc *aioproc; /* (*) the AIO proc */
246 * data-structure for lio signal management
249 int lioj_flags; /* (a) listio flags */
250 int lioj_count; /* (a) count of jobs */
251 int lioj_finished_count; /* (a) count of finished jobs */
252 struct sigevent lioj_signal; /* (a) signal on all I/O done */
253 TAILQ_ENTRY(aioliojob) lioj_list; /* (a) lio list */
254 struct knlist klist; /* (a) list of knotes */
255 ksiginfo_t lioj_ksi; /* (a) Realtime signal info */
258 #define LIOJ_SIGNAL 0x1 /* signal on all done (lio) */
259 #define LIOJ_SIGNAL_POSTED 0x2 /* signal has been posted */
260 #define LIOJ_KEVENT_POSTED 0x4 /* kevent triggered */
263 * per process aio data structure
266 struct mtx kaio_mtx; /* the lock to protect this struct */
267 int kaio_flags; /* (a) per process kaio flags */
268 int kaio_active_count; /* (c) number of currently used AIOs */
269 int kaio_count; /* (a) size of AIO queue */
270 int kaio_buffer_count; /* (a) number of bio buffers */
271 TAILQ_HEAD(,kaiocb) kaio_all; /* (a) all AIOs in a process */
272 TAILQ_HEAD(,kaiocb) kaio_done; /* (a) done queue for process */
273 TAILQ_HEAD(,aioliojob) kaio_liojoblist; /* (a) list of lio jobs */
274 TAILQ_HEAD(,kaiocb) kaio_jobqueue; /* (a) job queue for process */
275 TAILQ_HEAD(,kaiocb) kaio_syncqueue; /* (a) queue for aio_fsync */
276 TAILQ_HEAD(,kaiocb) kaio_syncready; /* (a) second q for aio_fsync */
277 struct task kaio_task; /* (*) task to kick aio processes */
278 struct task kaio_sync_task; /* (*) task to schedule fsync jobs */
281 #define AIO_LOCK(ki) mtx_lock(&(ki)->kaio_mtx)
282 #define AIO_UNLOCK(ki) mtx_unlock(&(ki)->kaio_mtx)
283 #define AIO_LOCK_ASSERT(ki, f) mtx_assert(&(ki)->kaio_mtx, (f))
284 #define AIO_MTX(ki) (&(ki)->kaio_mtx)
286 #define KAIO_RUNDOWN 0x1 /* process is being run down */
287 #define KAIO_WAKEUP 0x2 /* wakeup process when AIO completes */
290 * Operations used to interact with userland aio control blocks.
291 * Different ABIs provide their own operations.
294 int (*aio_copyin)(struct aiocb *ujob, struct kaiocb *kjob, int ty);
295 long (*fetch_status)(struct aiocb *ujob);
296 long (*fetch_error)(struct aiocb *ujob);
297 int (*store_status)(struct aiocb *ujob, long status);
298 int (*store_error)(struct aiocb *ujob, long error);
299 int (*store_kernelinfo)(struct aiocb *ujob, long jobref);
300 int (*store_aiocb)(struct aiocb **ujobp, struct aiocb *ujob);
303 static TAILQ_HEAD(,aioproc) aio_freeproc; /* (c) Idle daemons */
304 static struct sema aio_newproc_sem;
305 static struct mtx aio_job_mtx;
306 static TAILQ_HEAD(,kaiocb) aio_jobs; /* (c) Async job list */
307 static struct unrhdr *aiod_unr;
309 static void aio_biocleanup(struct bio *bp);
310 void aio_init_aioinfo(struct proc *p);
311 static int aio_onceonly(void);
312 static int aio_free_entry(struct kaiocb *job);
313 static void aio_process_rw(struct kaiocb *job);
314 static void aio_process_sync(struct kaiocb *job);
315 static void aio_process_mlock(struct kaiocb *job);
316 static void aio_schedule_fsync(void *context, int pending);
317 static int aio_newproc(int *);
318 int aio_aqueue(struct thread *td, struct aiocb *ujob,
319 struct aioliojob *lio, int type, struct aiocb_ops *ops);
320 static int aio_queue_file(struct file *fp, struct kaiocb *job);
321 static void aio_biowakeup(struct bio *bp);
322 static void aio_proc_rundown(void *arg, struct proc *p);
323 static void aio_proc_rundown_exec(void *arg, struct proc *p,
324 struct image_params *imgp);
325 static int aio_qbio(struct proc *p, struct kaiocb *job);
326 static void aio_daemon(void *param);
327 static void aio_bio_done_notify(struct proc *userp, struct kaiocb *job);
328 static bool aio_clear_cancel_function_locked(struct kaiocb *job);
329 static int aio_kick(struct proc *userp);
330 static void aio_kick_nowait(struct proc *userp);
331 static void aio_kick_helper(void *context, int pending);
332 static int filt_aioattach(struct knote *kn);
333 static void filt_aiodetach(struct knote *kn);
334 static int filt_aio(struct knote *kn, long hint);
335 static int filt_lioattach(struct knote *kn);
336 static void filt_liodetach(struct knote *kn);
337 static int filt_lio(struct knote *kn, long hint);
341 * kaio Per process async io info
342 * aiocb async io jobs
343 * aiolio list io jobs
345 static uma_zone_t kaio_zone, aiocb_zone, aiolio_zone;
347 /* kqueue filters for aio */
348 static struct filterops aio_filtops = {
350 .f_attach = filt_aioattach,
351 .f_detach = filt_aiodetach,
354 static struct filterops lio_filtops = {
356 .f_attach = filt_lioattach,
357 .f_detach = filt_liodetach,
361 static eventhandler_tag exit_tag, exec_tag;
363 TASKQUEUE_DEFINE_THREAD(aiod_kick);
366 * Main operations function for use as a kernel module.
369 aio_modload(struct module *module, int cmd, void *arg)
386 static moduledata_t aio_mod = {
392 DECLARE_MODULE(aio, aio_mod, SI_SUB_VFS, SI_ORDER_ANY);
393 MODULE_VERSION(aio, 1);
396 * Startup initialization
402 exit_tag = EVENTHANDLER_REGISTER(process_exit, aio_proc_rundown, NULL,
403 EVENTHANDLER_PRI_ANY);
404 exec_tag = EVENTHANDLER_REGISTER(process_exec, aio_proc_rundown_exec,
405 NULL, EVENTHANDLER_PRI_ANY);
406 kqueue_add_filteropts(EVFILT_AIO, &aio_filtops);
407 kqueue_add_filteropts(EVFILT_LIO, &lio_filtops);
408 TAILQ_INIT(&aio_freeproc);
409 sema_init(&aio_newproc_sem, 0, "aio_new_proc");
410 mtx_init(&aio_job_mtx, "aio_job", NULL, MTX_DEF);
411 TAILQ_INIT(&aio_jobs);
412 aiod_unr = new_unrhdr(1, INT_MAX, NULL);
413 kaio_zone = uma_zcreate("AIO", sizeof(struct kaioinfo), NULL, NULL,
414 NULL, NULL, UMA_ALIGN_PTR, 0);
415 aiocb_zone = uma_zcreate("AIOCB", sizeof(struct kaiocb), NULL, NULL,
416 NULL, NULL, UMA_ALIGN_PTR, 0);
417 aiolio_zone = uma_zcreate("AIOLIO", sizeof(struct aioliojob), NULL,
418 NULL, NULL, NULL, UMA_ALIGN_PTR, 0);
419 aiod_lifetime = AIOD_LIFETIME_DEFAULT;
421 p31b_setcfg(CTL_P1003_1B_ASYNCHRONOUS_IO, _POSIX_ASYNCHRONOUS_IO);
422 p31b_setcfg(CTL_P1003_1B_AIO_MAX, MAX_AIO_QUEUE);
423 p31b_setcfg(CTL_P1003_1B_AIO_PRIO_DELTA_MAX, 0);
429 * Init the per-process aioinfo structure. The aioinfo limits are set
430 * per-process for user limit (resource) management.
433 aio_init_aioinfo(struct proc *p)
437 ki = uma_zalloc(kaio_zone, M_WAITOK);
438 mtx_init(&ki->kaio_mtx, "aiomtx", NULL, MTX_DEF | MTX_NEW);
440 ki->kaio_active_count = 0;
442 ki->kaio_buffer_count = 0;
443 TAILQ_INIT(&ki->kaio_all);
444 TAILQ_INIT(&ki->kaio_done);
445 TAILQ_INIT(&ki->kaio_jobqueue);
446 TAILQ_INIT(&ki->kaio_liojoblist);
447 TAILQ_INIT(&ki->kaio_syncqueue);
448 TAILQ_INIT(&ki->kaio_syncready);
449 TASK_INIT(&ki->kaio_task, 0, aio_kick_helper, p);
450 TASK_INIT(&ki->kaio_sync_task, 0, aio_schedule_fsync, ki);
452 if (p->p_aioinfo == NULL) {
457 mtx_destroy(&ki->kaio_mtx);
458 uma_zfree(kaio_zone, ki);
461 while (num_aio_procs < MIN(target_aio_procs, max_aio_procs))
466 aio_sendsig(struct proc *p, struct sigevent *sigev, ksiginfo_t *ksi, bool ext)
471 error = sigev_findtd(p, sigev, &td);
475 ksiginfo_set_sigev(ksi, sigev);
476 ksi->ksi_code = SI_ASYNCIO;
477 ksi->ksi_flags |= ext ? (KSI_EXT | KSI_INS) : 0;
478 tdsendsignal(p, td, ksi->ksi_signo, ksi);
485 * Free a job entry. Wait for completion if it is currently active, but don't
486 * delay forever. If we delay, we return a flag that says that we have to
487 * restart the queue scan.
490 aio_free_entry(struct kaiocb *job)
493 struct aioliojob *lj;
501 AIO_LOCK_ASSERT(ki, MA_OWNED);
502 MPASS(job->jobflags & KAIOCB_FINISHED);
504 atomic_subtract_int(&num_queue_count, 1);
507 MPASS(ki->kaio_count >= 0);
509 TAILQ_REMOVE(&ki->kaio_done, job, plist);
510 TAILQ_REMOVE(&ki->kaio_all, job, allist);
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);
522 sigqueue_take(&lj->lioj_ksi);
524 uma_zfree(aiolio_zone, lj);
528 /* job is going away, we need to destroy any knotes */
529 knlist_delete(&job->klist, curthread, 1);
531 sigqueue_take(&job->ksi);
537 * The thread argument here is used to find the owning process
538 * and is also passed to fo_close() which may pass it to various
539 * places such as devsw close() routines. Because of that, we
540 * need a thread pointer from the process owning the job that is
541 * persistent and won't disappear out from under us or move to
544 * Currently, all the callers of this function call it to remove
545 * a kaiocb from the current process' job list either via a
546 * syscall or due to the current process calling exit() or
547 * execve(). Thus, we know that p == curproc. We also know that
548 * curthread can't exit since we are curthread.
550 * Therefore, we use curthread as the thread to pass to
551 * knlist_delete(). This does mean that it is possible for the
552 * thread pointer at close time to differ from the thread pointer
553 * at open time, but this is already true of file descriptors in
554 * a multithreaded process.
557 fdrop(job->fd_file, curthread);
559 if (job->uiop != &job->uio)
560 free(job->uiop, M_IOV);
561 uma_zfree(aiocb_zone, job);
568 aio_proc_rundown_exec(void *arg, struct proc *p,
569 struct image_params *imgp __unused)
571 aio_proc_rundown(arg, p);
575 aio_cancel_job(struct proc *p, struct kaioinfo *ki, struct kaiocb *job)
577 aio_cancel_fn_t *func;
580 AIO_LOCK_ASSERT(ki, MA_OWNED);
581 if (job->jobflags & (KAIOCB_CANCELLED | KAIOCB_FINISHED))
583 MPASS((job->jobflags & KAIOCB_CANCELLING) == 0);
584 job->jobflags |= KAIOCB_CANCELLED;
586 func = job->cancel_fn;
589 * If there is no cancel routine, just leave the job marked as
590 * cancelled. The job should be in active use by a caller who
591 * should complete it normally or when it fails to install a
598 * Set the CANCELLING flag so that aio_complete() will defer
599 * completions of this job. This prevents the job from being
600 * freed out from under the cancel callback. After the
601 * callback any deferred completion (whether from the callback
602 * or any other source) will be completed.
604 job->jobflags |= KAIOCB_CANCELLING;
608 job->jobflags &= ~KAIOCB_CANCELLING;
609 if (job->jobflags & KAIOCB_FINISHED) {
610 cancelled = job->uaiocb._aiocb_private.error == ECANCELED;
611 TAILQ_REMOVE(&ki->kaio_jobqueue, job, plist);
612 aio_bio_done_notify(p, job);
615 * The cancel callback might have scheduled an
616 * operation to cancel this request, but it is
617 * only counted as cancelled if the request is
618 * cancelled when the callback returns.
626 * Rundown the jobs for a given process.
629 aio_proc_rundown(void *arg, struct proc *p)
632 struct aioliojob *lj;
633 struct kaiocb *job, *jobn;
635 KASSERT(curthread->td_proc == p,
636 ("%s: called on non-curproc", __func__));
642 ki->kaio_flags |= KAIO_RUNDOWN;
647 * Try to cancel all pending requests. This code simulates
648 * aio_cancel on all pending I/O requests.
650 TAILQ_FOREACH_SAFE(job, &ki->kaio_jobqueue, plist, jobn) {
651 aio_cancel_job(p, ki, job);
654 /* Wait for all running I/O to be finished */
655 if (TAILQ_FIRST(&ki->kaio_jobqueue) || ki->kaio_active_count != 0) {
656 ki->kaio_flags |= KAIO_WAKEUP;
657 msleep(&p->p_aioinfo, AIO_MTX(ki), PRIBIO, "aioprn", hz);
661 /* Free all completed I/O requests. */
662 while ((job = TAILQ_FIRST(&ki->kaio_done)) != NULL)
665 while ((lj = TAILQ_FIRST(&ki->kaio_liojoblist)) != NULL) {
666 if (lj->lioj_count == 0) {
667 TAILQ_REMOVE(&ki->kaio_liojoblist, lj, lioj_list);
668 knlist_delete(&lj->klist, curthread, 1);
670 sigqueue_take(&lj->lioj_ksi);
672 uma_zfree(aiolio_zone, lj);
674 panic("LIO job not cleaned up: C:%d, FC:%d\n",
675 lj->lioj_count, lj->lioj_finished_count);
679 taskqueue_drain(taskqueue_aiod_kick, &ki->kaio_task);
680 taskqueue_drain(taskqueue_aiod_kick, &ki->kaio_sync_task);
681 mtx_destroy(&ki->kaio_mtx);
682 uma_zfree(kaio_zone, ki);
687 * Select a job to run (called by an AIO daemon).
689 static struct kaiocb *
690 aio_selectjob(struct aioproc *aiop)
696 mtx_assert(&aio_job_mtx, MA_OWNED);
698 TAILQ_FOREACH(job, &aio_jobs, list) {
699 userp = job->userproc;
700 ki = userp->p_aioinfo;
702 if (ki->kaio_active_count < max_aio_per_proc) {
703 TAILQ_REMOVE(&aio_jobs, job, list);
704 if (!aio_clear_cancel_function(job))
707 /* Account for currently active jobs. */
708 ki->kaio_active_count++;
716 * Move all data to a permanent storage device. This code
717 * simulates the fsync and fdatasync syscalls.
720 aio_fsync_vnode(struct thread *td, struct vnode *vp, int op)
727 error = vn_start_write(vp, &mp, V_WAIT | PCATCH);
730 vn_lock(vp, LK_EXCLUSIVE | LK_RETRY);
733 VM_OBJECT_WLOCK(obj);
734 vm_object_page_clean(obj, 0, 0, 0);
735 VM_OBJECT_WUNLOCK(obj);
738 error = VOP_FDATASYNC(vp, td);
740 error = VOP_FSYNC(vp, MNT_WAIT, td);
743 vn_finished_write(mp);
744 if (error != ERELOOKUP)
751 * The AIO processing activity for LIO_READ/LIO_WRITE. This is the code that
752 * does the I/O request for the non-bio version of the operations. The normal
753 * vn operations are used, and this code should work in all instances for every
754 * type of file, including pipes, sockets, fifos, and regular files.
756 * XXX I don't think it works well for socket, pipe, and fifo.
759 aio_process_rw(struct kaiocb *job)
761 struct ucred *td_savedcred;
766 long msgsnd_st, msgsnd_end;
767 long msgrcv_st, msgrcv_end;
768 long oublock_st, oublock_end;
769 long inblock_st, inblock_end;
772 KASSERT(job->uaiocb.aio_lio_opcode == LIO_READ ||
773 job->uaiocb.aio_lio_opcode == LIO_READV ||
774 job->uaiocb.aio_lio_opcode == LIO_WRITE ||
775 job->uaiocb.aio_lio_opcode == LIO_WRITEV,
776 ("%s: opcode %d", __func__, job->uaiocb.aio_lio_opcode));
778 aio_switch_vmspace(job);
780 td_savedcred = td->td_ucred;
781 td->td_ucred = job->cred;
782 job->uiop->uio_td = td;
786 opcode = job->uaiocb.aio_lio_opcode;
787 cnt = job->uiop->uio_resid;
789 msgrcv_st = td->td_ru.ru_msgrcv;
790 msgsnd_st = td->td_ru.ru_msgsnd;
791 inblock_st = td->td_ru.ru_inblock;
792 oublock_st = td->td_ru.ru_oublock;
795 * aio_aqueue() acquires a reference to the file that is
796 * released in aio_free_entry().
798 if (opcode == LIO_READ || opcode == LIO_READV) {
799 if (job->uiop->uio_resid == 0)
802 error = fo_read(fp, job->uiop, fp->f_cred, FOF_OFFSET,
805 if (fp->f_type == DTYPE_VNODE)
807 error = fo_write(fp, job->uiop, fp->f_cred, FOF_OFFSET, td);
809 msgrcv_end = td->td_ru.ru_msgrcv;
810 msgsnd_end = td->td_ru.ru_msgsnd;
811 inblock_end = td->td_ru.ru_inblock;
812 oublock_end = td->td_ru.ru_oublock;
814 job->msgrcv = msgrcv_end - msgrcv_st;
815 job->msgsnd = msgsnd_end - msgsnd_st;
816 job->inblock = inblock_end - inblock_st;
817 job->outblock = oublock_end - oublock_st;
819 if (error != 0 && job->uiop->uio_resid != cnt) {
820 if (error == ERESTART || error == EINTR || error == EWOULDBLOCK)
822 if (error == EPIPE && (opcode & LIO_WRITE)) {
823 PROC_LOCK(job->userproc);
824 kern_psignal(job->userproc, SIGPIPE);
825 PROC_UNLOCK(job->userproc);
829 cnt -= job->uiop->uio_resid;
830 td->td_ucred = td_savedcred;
832 aio_complete(job, -1, error);
834 aio_complete(job, cnt, 0);
838 aio_process_sync(struct kaiocb *job)
840 struct thread *td = curthread;
841 struct ucred *td_savedcred = td->td_ucred;
842 struct file *fp = job->fd_file;
845 KASSERT(job->uaiocb.aio_lio_opcode & LIO_SYNC,
846 ("%s: opcode %d", __func__, job->uaiocb.aio_lio_opcode));
848 td->td_ucred = job->cred;
849 if (fp->f_vnode != NULL) {
850 error = aio_fsync_vnode(td, fp->f_vnode,
851 job->uaiocb.aio_lio_opcode);
853 td->td_ucred = td_savedcred;
855 aio_complete(job, -1, error);
857 aio_complete(job, 0, 0);
861 aio_process_mlock(struct kaiocb *job)
863 struct aiocb *cb = &job->uaiocb;
866 KASSERT(job->uaiocb.aio_lio_opcode == LIO_MLOCK,
867 ("%s: opcode %d", __func__, job->uaiocb.aio_lio_opcode));
869 aio_switch_vmspace(job);
870 error = kern_mlock(job->userproc, job->cred,
871 __DEVOLATILE(uintptr_t, cb->aio_buf), cb->aio_nbytes);
872 aio_complete(job, error != 0 ? -1 : 0, error);
876 aio_bio_done_notify(struct proc *userp, struct kaiocb *job)
878 struct aioliojob *lj;
880 struct kaiocb *sjob, *sjobn;
884 ki = userp->p_aioinfo;
885 AIO_LOCK_ASSERT(ki, MA_OWNED);
889 lj->lioj_finished_count++;
890 if (lj->lioj_count == lj->lioj_finished_count)
893 TAILQ_INSERT_TAIL(&ki->kaio_done, job, plist);
894 MPASS(job->jobflags & KAIOCB_FINISHED);
896 if (ki->kaio_flags & KAIO_RUNDOWN)
897 goto notification_done;
899 if (job->uaiocb.aio_sigevent.sigev_notify == SIGEV_SIGNAL ||
900 job->uaiocb.aio_sigevent.sigev_notify == SIGEV_THREAD_ID)
901 aio_sendsig(userp, &job->uaiocb.aio_sigevent, &job->ksi, true);
903 KNOTE_LOCKED(&job->klist, 1);
906 if (lj->lioj_signal.sigev_notify == SIGEV_KEVENT) {
907 lj->lioj_flags |= LIOJ_KEVENT_POSTED;
908 KNOTE_LOCKED(&lj->klist, 1);
910 if ((lj->lioj_flags & (LIOJ_SIGNAL | LIOJ_SIGNAL_POSTED))
912 (lj->lioj_signal.sigev_notify == SIGEV_SIGNAL ||
913 lj->lioj_signal.sigev_notify == SIGEV_THREAD_ID)) {
914 aio_sendsig(userp, &lj->lioj_signal, &lj->lioj_ksi,
916 lj->lioj_flags |= LIOJ_SIGNAL_POSTED;
921 if (job->jobflags & KAIOCB_CHECKSYNC) {
922 schedule_fsync = false;
923 TAILQ_FOREACH_SAFE(sjob, &ki->kaio_syncqueue, list, sjobn) {
924 if (job->fd_file != sjob->fd_file ||
925 job->seqno >= sjob->seqno)
927 if (--sjob->pending > 0)
929 TAILQ_REMOVE(&ki->kaio_syncqueue, sjob, list);
930 if (!aio_clear_cancel_function_locked(sjob))
932 TAILQ_INSERT_TAIL(&ki->kaio_syncready, sjob, list);
933 schedule_fsync = true;
936 taskqueue_enqueue(taskqueue_aiod_kick,
937 &ki->kaio_sync_task);
939 if (ki->kaio_flags & KAIO_WAKEUP) {
940 ki->kaio_flags &= ~KAIO_WAKEUP;
941 wakeup(&userp->p_aioinfo);
946 aio_schedule_fsync(void *context, int pending)
953 while (!TAILQ_EMPTY(&ki->kaio_syncready)) {
954 job = TAILQ_FIRST(&ki->kaio_syncready);
955 TAILQ_REMOVE(&ki->kaio_syncready, job, list);
957 aio_schedule(job, aio_process_sync);
964 aio_cancel_cleared(struct kaiocb *job)
968 * The caller should hold the same queue lock held when
969 * aio_clear_cancel_function() was called and set this flag
970 * ensuring this check sees an up-to-date value. However,
971 * there is no way to assert that.
973 return ((job->jobflags & KAIOCB_CLEARED) != 0);
977 aio_clear_cancel_function_locked(struct kaiocb *job)
980 AIO_LOCK_ASSERT(job->userproc->p_aioinfo, MA_OWNED);
981 MPASS(job->cancel_fn != NULL);
982 if (job->jobflags & KAIOCB_CANCELLING) {
983 job->jobflags |= KAIOCB_CLEARED;
986 job->cancel_fn = NULL;
991 aio_clear_cancel_function(struct kaiocb *job)
996 ki = job->userproc->p_aioinfo;
998 ret = aio_clear_cancel_function_locked(job);
1004 aio_set_cancel_function_locked(struct kaiocb *job, aio_cancel_fn_t *func)
1007 AIO_LOCK_ASSERT(job->userproc->p_aioinfo, MA_OWNED);
1008 if (job->jobflags & KAIOCB_CANCELLED)
1010 job->cancel_fn = func;
1015 aio_set_cancel_function(struct kaiocb *job, aio_cancel_fn_t *func)
1017 struct kaioinfo *ki;
1020 ki = job->userproc->p_aioinfo;
1022 ret = aio_set_cancel_function_locked(job, func);
1028 aio_complete(struct kaiocb *job, long status, int error)
1030 struct kaioinfo *ki;
1033 job->uaiocb._aiocb_private.error = error;
1034 job->uaiocb._aiocb_private.status = status;
1036 userp = job->userproc;
1037 ki = userp->p_aioinfo;
1040 KASSERT(!(job->jobflags & KAIOCB_FINISHED),
1041 ("duplicate aio_complete"));
1042 job->jobflags |= KAIOCB_FINISHED;
1043 if ((job->jobflags & (KAIOCB_QUEUEING | KAIOCB_CANCELLING)) == 0) {
1044 TAILQ_REMOVE(&ki->kaio_jobqueue, job, plist);
1045 aio_bio_done_notify(userp, job);
1051 aio_cancel(struct kaiocb *job)
1054 aio_complete(job, -1, ECANCELED);
1058 aio_switch_vmspace(struct kaiocb *job)
1061 vmspace_switch_aio(job->userproc->p_vmspace);
1065 * The AIO daemon, most of the actual work is done in aio_process_*,
1066 * but the setup (and address space mgmt) is done in this routine.
1069 aio_daemon(void *_id)
1072 struct aioproc *aiop;
1073 struct kaioinfo *ki;
1075 struct vmspace *myvm;
1076 struct thread *td = curthread;
1077 int id = (intptr_t)_id;
1080 * Grab an extra reference on the daemon's vmspace so that it
1081 * doesn't get freed by jobs that switch to a different
1085 myvm = vmspace_acquire_ref(p);
1087 KASSERT(p->p_textvp == NULL, ("kthread has a textvp"));
1090 * Allocate and ready the aio control info. There is one aiop structure
1093 aiop = malloc(sizeof(*aiop), M_AIO, M_WAITOK);
1095 aiop->aioprocflags = 0;
1098 * Wakeup parent process. (Parent sleeps to keep from blasting away
1099 * and creating too many daemons.)
1101 sema_post(&aio_newproc_sem);
1103 mtx_lock(&aio_job_mtx);
1106 * Take daemon off of free queue
1108 if (aiop->aioprocflags & AIOP_FREE) {
1109 TAILQ_REMOVE(&aio_freeproc, aiop, list);
1110 aiop->aioprocflags &= ~AIOP_FREE;
1116 while ((job = aio_selectjob(aiop)) != NULL) {
1117 mtx_unlock(&aio_job_mtx);
1119 ki = job->userproc->p_aioinfo;
1120 job->handle_fn(job);
1122 mtx_lock(&aio_job_mtx);
1123 /* Decrement the active job count. */
1124 ki->kaio_active_count--;
1128 * Disconnect from user address space.
1130 if (p->p_vmspace != myvm) {
1131 mtx_unlock(&aio_job_mtx);
1132 vmspace_switch_aio(myvm);
1133 mtx_lock(&aio_job_mtx);
1135 * We have to restart to avoid race, we only sleep if
1136 * no job can be selected.
1141 mtx_assert(&aio_job_mtx, MA_OWNED);
1143 TAILQ_INSERT_HEAD(&aio_freeproc, aiop, list);
1144 aiop->aioprocflags |= AIOP_FREE;
1147 * If daemon is inactive for a long time, allow it to exit,
1148 * thereby freeing resources.
1150 if (msleep(p, &aio_job_mtx, PRIBIO, "aiordy",
1151 aiod_lifetime) == EWOULDBLOCK && TAILQ_EMPTY(&aio_jobs) &&
1152 (aiop->aioprocflags & AIOP_FREE) &&
1153 num_aio_procs > target_aio_procs)
1156 TAILQ_REMOVE(&aio_freeproc, aiop, list);
1158 mtx_unlock(&aio_job_mtx);
1160 free_unr(aiod_unr, id);
1163 KASSERT(p->p_vmspace == myvm,
1164 ("AIOD: bad vmspace for exiting daemon"));
1165 KASSERT(refcount_load(&myvm->vm_refcnt) > 1,
1166 ("AIOD: bad vm refcnt for exiting daemon: %d",
1167 refcount_load(&myvm->vm_refcnt)));
1172 * Create a new AIO daemon. This is mostly a kernel-thread fork routine. The
1173 * AIO daemon modifies its environment itself.
1176 aio_newproc(int *start)
1182 id = alloc_unr(aiod_unr);
1183 error = kproc_create(aio_daemon, (void *)(intptr_t)id, &p,
1184 RFNOWAIT, 0, "aiod%d", id);
1187 * Wait until daemon is started.
1189 sema_wait(&aio_newproc_sem);
1190 mtx_lock(&aio_job_mtx);
1194 mtx_unlock(&aio_job_mtx);
1196 free_unr(aiod_unr, id);
1202 * Try the high-performance, low-overhead bio method for eligible
1203 * VCHR devices. This method doesn't use an aio helper thread, and
1204 * thus has very low overhead.
1206 * Assumes that the caller, aio_aqueue(), has incremented the file
1207 * structure's reference count, preventing its deallocation for the
1208 * duration of this call.
1211 aio_qbio(struct proc *p, struct kaiocb *job)
1219 struct kaioinfo *ki;
1220 struct bio **bios = NULL;
1222 int bio_cmd, error, i, iovcnt, opcode, poff, ref;
1228 opcode = cb->aio_lio_opcode;
1230 if (!(opcode == LIO_WRITE || opcode == LIO_WRITEV ||
1231 opcode == LIO_READ || opcode == LIO_READV))
1233 if (fp == NULL || fp->f_type != DTYPE_VNODE)
1237 if (vp->v_type != VCHR)
1239 if (vp->v_bufobj.bo_bsize == 0)
1242 bio_cmd = (opcode & LIO_WRITE) ? BIO_WRITE : BIO_READ;
1243 iovcnt = job->uiop->uio_iovcnt;
1244 if (iovcnt > max_buf_aio)
1246 for (i = 0; i < iovcnt; i++) {
1247 if (job->uiop->uio_iov[i].iov_len % vp->v_bufobj.bo_bsize != 0)
1249 if (job->uiop->uio_iov[i].iov_len > maxphys) {
1254 offset = cb->aio_offset;
1257 csw = devvn_refthread(vp, &dev, &ref);
1261 if ((csw->d_flags & D_DISK) == 0) {
1265 if (job->uiop->uio_resid > dev->si_iosize_max) {
1273 use_unmapped = (dev->si_flags & SI_UNMAPPED) && unmapped_buf_allowed;
1274 if (!use_unmapped) {
1276 if (ki->kaio_buffer_count + iovcnt > max_buf_aio) {
1281 ki->kaio_buffer_count += iovcnt;
1285 bios = malloc(sizeof(struct bio *) * iovcnt, M_TEMP, M_WAITOK);
1286 atomic_store_int(&job->nbio, iovcnt);
1287 for (i = 0; i < iovcnt; i++) {
1288 struct vm_page** pages;
1294 buf = job->uiop->uio_iov[i].iov_base;
1295 nbytes = job->uiop->uio_iov[i].iov_len;
1297 bios[i] = g_alloc_bio();
1300 poff = (vm_offset_t)buf & PAGE_MASK;
1303 pages = malloc(sizeof(vm_page_t) * (atop(round_page(
1304 nbytes)) + 1), M_TEMP, M_WAITOK | M_ZERO);
1306 pbuf = uma_zalloc(pbuf_zone, M_WAITOK);
1308 pages = pbuf->b_pages;
1311 bp->bio_length = nbytes;
1312 bp->bio_bcount = nbytes;
1313 bp->bio_done = aio_biowakeup;
1314 bp->bio_offset = offset;
1315 bp->bio_cmd = bio_cmd;
1317 bp->bio_caller1 = job;
1318 bp->bio_caller2 = pbuf;
1320 prot = VM_PROT_READ;
1321 if (opcode == LIO_READ || opcode == LIO_READV)
1322 prot |= VM_PROT_WRITE; /* Less backwards than it looks */
1323 npages = vm_fault_quick_hold_pages(&curproc->p_vmspace->vm_map,
1324 (vm_offset_t)buf, bp->bio_length, prot, pages,
1328 uma_zfree(pbuf_zone, pbuf);
1330 free(pages, M_TEMP);
1337 pmap_qenter((vm_offset_t)pbuf->b_data, pages, npages);
1338 bp->bio_data = pbuf->b_data + poff;
1339 pbuf->b_npages = npages;
1340 atomic_add_int(&num_buf_aio, 1);
1343 bp->bio_ma_n = npages;
1344 bp->bio_ma_offset = poff;
1345 bp->bio_data = unmapped_buf;
1346 bp->bio_flags |= BIO_UNMAPPED;
1347 atomic_add_int(&num_unmapped_aio, 1);
1353 /* Perform transfer. */
1354 for (i = 0; i < iovcnt; i++)
1355 csw->d_strategy(bios[i]);
1358 dev_relthread(dev, ref);
1363 aio_biocleanup(bios[i]);
1366 dev_relthread(dev, ref);
1370 #ifdef COMPAT_FREEBSD6
1372 convert_old_sigevent(struct osigevent *osig, struct sigevent *nsig)
1376 * Only SIGEV_NONE, SIGEV_SIGNAL, and SIGEV_KEVENT are
1377 * supported by AIO with the old sigevent structure.
1379 nsig->sigev_notify = osig->sigev_notify;
1380 switch (nsig->sigev_notify) {
1384 nsig->sigev_signo = osig->__sigev_u.__sigev_signo;
1387 nsig->sigev_notify_kqueue =
1388 osig->__sigev_u.__sigev_notify_kqueue;
1389 nsig->sigev_value.sival_ptr = osig->sigev_value.sival_ptr;
1398 aiocb_copyin_old_sigevent(struct aiocb *ujob, struct kaiocb *kjob,
1401 struct oaiocb *ojob;
1402 struct aiocb *kcb = &kjob->uaiocb;
1405 bzero(kcb, sizeof(struct aiocb));
1406 error = copyin(ujob, kcb, sizeof(struct oaiocb));
1409 /* No need to copyin aio_iov, because it did not exist in FreeBSD 6 */
1410 ojob = (struct oaiocb *)kcb;
1411 return (convert_old_sigevent(&ojob->aio_sigevent, &kcb->aio_sigevent));
1416 aiocb_copyin(struct aiocb *ujob, struct kaiocb *kjob, int type)
1418 struct aiocb *kcb = &kjob->uaiocb;
1421 error = copyin(ujob, kcb, sizeof(struct aiocb));
1424 if (type & LIO_VECTORED) {
1425 /* malloc a uio and copy in the iovec */
1426 error = copyinuio(__DEVOLATILE(struct iovec*, kcb->aio_iov),
1427 kcb->aio_iovcnt, &kjob->uiop);
1434 aiocb_fetch_status(struct aiocb *ujob)
1437 return (fuword(&ujob->_aiocb_private.status));
1441 aiocb_fetch_error(struct aiocb *ujob)
1444 return (fuword(&ujob->_aiocb_private.error));
1448 aiocb_store_status(struct aiocb *ujob, long status)
1451 return (suword(&ujob->_aiocb_private.status, status));
1455 aiocb_store_error(struct aiocb *ujob, long error)
1458 return (suword(&ujob->_aiocb_private.error, error));
1462 aiocb_store_kernelinfo(struct aiocb *ujob, long jobref)
1465 return (suword(&ujob->_aiocb_private.kernelinfo, jobref));
1469 aiocb_store_aiocb(struct aiocb **ujobp, struct aiocb *ujob)
1472 return (suword(ujobp, (long)ujob));
1475 static struct aiocb_ops aiocb_ops = {
1476 .aio_copyin = aiocb_copyin,
1477 .fetch_status = aiocb_fetch_status,
1478 .fetch_error = aiocb_fetch_error,
1479 .store_status = aiocb_store_status,
1480 .store_error = aiocb_store_error,
1481 .store_kernelinfo = aiocb_store_kernelinfo,
1482 .store_aiocb = aiocb_store_aiocb,
1485 #ifdef COMPAT_FREEBSD6
1486 static struct aiocb_ops aiocb_ops_osigevent = {
1487 .aio_copyin = aiocb_copyin_old_sigevent,
1488 .fetch_status = aiocb_fetch_status,
1489 .fetch_error = aiocb_fetch_error,
1490 .store_status = aiocb_store_status,
1491 .store_error = aiocb_store_error,
1492 .store_kernelinfo = aiocb_store_kernelinfo,
1493 .store_aiocb = aiocb_store_aiocb,
1498 * Queue a new AIO request. Choosing either the threaded or direct bio VCHR
1499 * technique is done in this code.
1502 aio_aqueue(struct thread *td, struct aiocb *ujob, struct aioliojob *lj,
1503 int type, struct aiocb_ops *ops)
1505 struct proc *p = td->td_proc;
1506 struct file *fp = NULL;
1508 struct kaioinfo *ki;
1516 if (p->p_aioinfo == NULL)
1517 aio_init_aioinfo(p);
1521 ops->store_status(ujob, -1);
1522 ops->store_error(ujob, 0);
1523 ops->store_kernelinfo(ujob, -1);
1525 if (num_queue_count >= max_queue_count ||
1526 ki->kaio_count >= max_aio_queue_per_proc) {
1531 job = uma_zalloc(aiocb_zone, M_WAITOK | M_ZERO);
1532 knlist_init_mtx(&job->klist, AIO_MTX(ki));
1534 error = ops->aio_copyin(ujob, job, type);
1538 if (job->uaiocb.aio_nbytes > IOSIZE_MAX) {
1543 if (job->uaiocb.aio_sigevent.sigev_notify != SIGEV_KEVENT &&
1544 job->uaiocb.aio_sigevent.sigev_notify != SIGEV_SIGNAL &&
1545 job->uaiocb.aio_sigevent.sigev_notify != SIGEV_THREAD_ID &&
1546 job->uaiocb.aio_sigevent.sigev_notify != SIGEV_NONE) {
1551 if ((job->uaiocb.aio_sigevent.sigev_notify == SIGEV_SIGNAL ||
1552 job->uaiocb.aio_sigevent.sigev_notify == SIGEV_THREAD_ID) &&
1553 !_SIG_VALID(job->uaiocb.aio_sigevent.sigev_signo)) {
1558 /* Get the opcode. */
1559 if (type == LIO_NOP) {
1560 switch (job->uaiocb.aio_lio_opcode) {
1564 opcode = job->uaiocb.aio_lio_opcode;
1571 opcode = job->uaiocb.aio_lio_opcode = type;
1573 ksiginfo_init(&job->ksi);
1575 /* Save userspace address of the job info. */
1579 * Validate the opcode and fetch the file object for the specified
1582 * XXXRW: Moved the opcode validation up here so that we don't
1583 * retrieve a file descriptor without knowing what the capabiltity
1586 fd = job->uaiocb.aio_fildes;
1590 error = fget_write(td, fd, &cap_pwrite_rights, &fp);
1594 error = fget_read(td, fd, &cap_pread_rights, &fp);
1598 error = fget(td, fd, &cap_fsync_rights, &fp);
1603 error = fget(td, fd, &cap_no_rights, &fp);
1611 if ((opcode & LIO_SYNC) && fp->f_vnode == NULL) {
1616 if ((opcode == LIO_READ || opcode == LIO_READV ||
1617 opcode == LIO_WRITE || opcode == LIO_WRITEV) &&
1618 job->uaiocb.aio_offset < 0 &&
1619 (fp->f_vnode == NULL || fp->f_vnode->v_type != VCHR)) {
1624 if (fp != NULL && fp->f_ops == &path_fileops) {
1631 mtx_lock(&aio_job_mtx);
1633 job->seqno = jobseqno++;
1634 mtx_unlock(&aio_job_mtx);
1635 error = ops->store_kernelinfo(ujob, jid);
1640 job->uaiocb._aiocb_private.kernelinfo = (void *)(intptr_t)jid;
1642 if (opcode == LIO_NOP) {
1644 MPASS(job->uiop == &job->uio || job->uiop == NULL);
1645 uma_zfree(aiocb_zone, job);
1649 if (job->uaiocb.aio_sigevent.sigev_notify != SIGEV_KEVENT)
1651 evflags = job->uaiocb.aio_sigevent.sigev_notify_kevent_flags;
1652 if ((evflags & ~(EV_CLEAR | EV_DISPATCH | EV_ONESHOT)) != 0) {
1656 kqfd = job->uaiocb.aio_sigevent.sigev_notify_kqueue;
1657 memset(&kev, 0, sizeof(kev));
1658 kev.ident = (uintptr_t)job->ujob;
1659 kev.filter = EVFILT_AIO;
1660 kev.flags = EV_ADD | EV_ENABLE | EV_FLAG1 | evflags;
1661 kev.data = (intptr_t)job;
1662 kev.udata = job->uaiocb.aio_sigevent.sigev_value.sival_ptr;
1663 error = kqfd_register(kqfd, &kev, td, M_WAITOK);
1669 ops->store_error(ujob, EINPROGRESS);
1670 job->uaiocb._aiocb_private.error = EINPROGRESS;
1672 job->cred = crhold(td->td_ucred);
1673 job->jobflags = KAIOCB_QUEUEING;
1676 if (opcode & LIO_VECTORED) {
1677 /* Use the uio copied in by aio_copyin */
1678 MPASS(job->uiop != &job->uio && job->uiop != NULL);
1680 /* Setup the inline uio */
1681 job->iov[0].iov_base = (void *)(uintptr_t)job->uaiocb.aio_buf;
1682 job->iov[0].iov_len = job->uaiocb.aio_nbytes;
1683 job->uio.uio_iov = job->iov;
1684 job->uio.uio_iovcnt = 1;
1685 job->uio.uio_resid = job->uaiocb.aio_nbytes;
1686 job->uio.uio_segflg = UIO_USERSPACE;
1687 job->uiop = &job->uio;
1689 switch (opcode & (LIO_READ | LIO_WRITE)) {
1691 job->uiop->uio_rw = UIO_READ;
1694 job->uiop->uio_rw = UIO_WRITE;
1697 job->uiop->uio_offset = job->uaiocb.aio_offset;
1698 job->uiop->uio_td = td;
1700 if (opcode == LIO_MLOCK) {
1701 aio_schedule(job, aio_process_mlock);
1703 } else if (fp->f_ops->fo_aio_queue == NULL)
1704 error = aio_queue_file(fp, job);
1706 error = fo_aio_queue(fp, job);
1711 job->jobflags &= ~KAIOCB_QUEUEING;
1712 TAILQ_INSERT_TAIL(&ki->kaio_all, job, allist);
1716 atomic_add_int(&num_queue_count, 1);
1717 if (job->jobflags & KAIOCB_FINISHED) {
1719 * The queue callback completed the request synchronously.
1720 * The bulk of the completion is deferred in that case
1723 aio_bio_done_notify(p, job);
1725 TAILQ_INSERT_TAIL(&ki->kaio_jobqueue, job, plist);
1734 knlist_delete(&job->klist, curthread, 0);
1736 if (job->uiop != &job->uio)
1737 free(job->uiop, M_IOV);
1738 uma_zfree(aiocb_zone, job);
1740 ops->store_error(ujob, error);
1745 aio_cancel_daemon_job(struct kaiocb *job)
1748 mtx_lock(&aio_job_mtx);
1749 if (!aio_cancel_cleared(job))
1750 TAILQ_REMOVE(&aio_jobs, job, list);
1751 mtx_unlock(&aio_job_mtx);
1756 aio_schedule(struct kaiocb *job, aio_handle_fn_t *func)
1759 mtx_lock(&aio_job_mtx);
1760 if (!aio_set_cancel_function(job, aio_cancel_daemon_job)) {
1761 mtx_unlock(&aio_job_mtx);
1765 job->handle_fn = func;
1766 TAILQ_INSERT_TAIL(&aio_jobs, job, list);
1767 aio_kick_nowait(job->userproc);
1768 mtx_unlock(&aio_job_mtx);
1772 aio_cancel_sync(struct kaiocb *job)
1774 struct kaioinfo *ki;
1776 ki = job->userproc->p_aioinfo;
1778 if (!aio_cancel_cleared(job))
1779 TAILQ_REMOVE(&ki->kaio_syncqueue, job, list);
1785 aio_queue_file(struct file *fp, struct kaiocb *job)
1787 struct kaioinfo *ki;
1788 struct kaiocb *job2;
1794 ki = job->userproc->p_aioinfo;
1795 error = aio_qbio(job->userproc, job);
1799 if (fp->f_type == DTYPE_VNODE) {
1801 if (vp->v_type == VREG || vp->v_type == VDIR) {
1802 mp = fp->f_vnode->v_mount;
1803 if (mp == NULL || (mp->mnt_flag & MNT_LOCAL) != 0)
1807 if (!(safe || enable_aio_unsafe)) {
1808 counted_warning(&unsafe_warningcnt,
1809 "is attempting to use unsafe AIO requests");
1810 return (EOPNOTSUPP);
1813 if (job->uaiocb.aio_lio_opcode & (LIO_WRITE | LIO_READ)) {
1814 aio_schedule(job, aio_process_rw);
1816 } else if (job->uaiocb.aio_lio_opcode & LIO_SYNC) {
1818 TAILQ_FOREACH(job2, &ki->kaio_jobqueue, plist) {
1819 if (job2->fd_file == job->fd_file &&
1820 ((job2->uaiocb.aio_lio_opcode & LIO_SYNC) == 0) &&
1821 job2->seqno < job->seqno) {
1822 job2->jobflags |= KAIOCB_CHECKSYNC;
1826 if (job->pending != 0) {
1827 if (!aio_set_cancel_function_locked(job,
1833 TAILQ_INSERT_TAIL(&ki->kaio_syncqueue, job, list);
1838 aio_schedule(job, aio_process_sync);
1847 aio_kick_nowait(struct proc *userp)
1849 struct kaioinfo *ki = userp->p_aioinfo;
1850 struct aioproc *aiop;
1852 mtx_assert(&aio_job_mtx, MA_OWNED);
1853 if ((aiop = TAILQ_FIRST(&aio_freeproc)) != NULL) {
1854 TAILQ_REMOVE(&aio_freeproc, aiop, list);
1855 aiop->aioprocflags &= ~AIOP_FREE;
1856 wakeup(aiop->aioproc);
1857 } else if (num_aio_resv_start + num_aio_procs < max_aio_procs &&
1858 ki->kaio_active_count + num_aio_resv_start < max_aio_per_proc) {
1859 taskqueue_enqueue(taskqueue_aiod_kick, &ki->kaio_task);
1864 aio_kick(struct proc *userp)
1866 struct kaioinfo *ki = userp->p_aioinfo;
1867 struct aioproc *aiop;
1870 mtx_assert(&aio_job_mtx, MA_OWNED);
1872 if ((aiop = TAILQ_FIRST(&aio_freeproc)) != NULL) {
1873 TAILQ_REMOVE(&aio_freeproc, aiop, list);
1874 aiop->aioprocflags &= ~AIOP_FREE;
1875 wakeup(aiop->aioproc);
1876 } else if (num_aio_resv_start + num_aio_procs < max_aio_procs &&
1877 ki->kaio_active_count + num_aio_resv_start < max_aio_per_proc) {
1878 num_aio_resv_start++;
1879 mtx_unlock(&aio_job_mtx);
1880 error = aio_newproc(&num_aio_resv_start);
1881 mtx_lock(&aio_job_mtx);
1883 num_aio_resv_start--;
1893 aio_kick_helper(void *context, int pending)
1895 struct proc *userp = context;
1897 mtx_lock(&aio_job_mtx);
1898 while (--pending >= 0) {
1899 if (aio_kick(userp))
1902 mtx_unlock(&aio_job_mtx);
1906 * Support the aio_return system call, as a side-effect, kernel resources are
1910 kern_aio_return(struct thread *td, struct aiocb *ujob, struct aiocb_ops *ops)
1912 struct proc *p = td->td_proc;
1914 struct kaioinfo *ki;
1921 TAILQ_FOREACH(job, &ki->kaio_done, plist) {
1922 if (job->ujob == ujob)
1926 MPASS(job->jobflags & KAIOCB_FINISHED);
1927 status = job->uaiocb._aiocb_private.status;
1928 error = job->uaiocb._aiocb_private.error;
1929 td->td_retval[0] = status;
1930 td->td_ru.ru_oublock += job->outblock;
1931 td->td_ru.ru_inblock += job->inblock;
1932 td->td_ru.ru_msgsnd += job->msgsnd;
1933 td->td_ru.ru_msgrcv += job->msgrcv;
1934 aio_free_entry(job);
1936 ops->store_error(ujob, error);
1937 ops->store_status(ujob, status);
1946 sys_aio_return(struct thread *td, struct aio_return_args *uap)
1949 return (kern_aio_return(td, uap->aiocbp, &aiocb_ops));
1953 * Allow a process to wakeup when any of the I/O requests are completed.
1956 kern_aio_suspend(struct thread *td, int njoblist, struct aiocb **ujoblist,
1957 struct timespec *ts)
1959 struct proc *p = td->td_proc;
1961 struct kaioinfo *ki;
1962 struct kaiocb *firstjob, *job;
1967 if (ts->tv_nsec < 0 || ts->tv_nsec >= 1000000000)
1970 TIMESPEC_TO_TIMEVAL(&atv, ts);
1971 if (itimerfix(&atv))
1973 timo = tvtohz(&atv);
1987 TAILQ_FOREACH(job, &ki->kaio_all, allist) {
1988 for (i = 0; i < njoblist; i++) {
1989 if (job->ujob == ujoblist[i]) {
1990 if (firstjob == NULL)
1992 if (job->jobflags & KAIOCB_FINISHED)
1997 /* All tasks were finished. */
1998 if (firstjob == NULL)
2001 ki->kaio_flags |= KAIO_WAKEUP;
2002 error = msleep(&p->p_aioinfo, AIO_MTX(ki), PRIBIO | PCATCH,
2004 if (error == ERESTART)
2015 sys_aio_suspend(struct thread *td, struct aio_suspend_args *uap)
2017 struct timespec ts, *tsp;
2018 struct aiocb **ujoblist;
2021 if (uap->nent < 0 || uap->nent > max_aio_queue_per_proc)
2025 /* Get timespec struct. */
2026 if ((error = copyin(uap->timeout, &ts, sizeof(ts))) != 0)
2032 ujoblist = malloc(uap->nent * sizeof(ujoblist[0]), M_AIO, M_WAITOK);
2033 error = copyin(uap->aiocbp, ujoblist, uap->nent * sizeof(ujoblist[0]));
2035 error = kern_aio_suspend(td, uap->nent, ujoblist, tsp);
2036 free(ujoblist, M_AIO);
2041 * aio_cancel cancels any non-bio aio operations not currently in progress.
2044 sys_aio_cancel(struct thread *td, struct aio_cancel_args *uap)
2046 struct proc *p = td->td_proc;
2047 struct kaioinfo *ki;
2048 struct kaiocb *job, *jobn;
2052 int notcancelled = 0;
2055 /* Lookup file object. */
2056 error = fget(td, uap->fd, &cap_no_rights, &fp);
2064 if (fp->f_type == DTYPE_VNODE) {
2066 if (vn_isdisk(vp)) {
2068 td->td_retval[0] = AIO_NOTCANCELED;
2074 TAILQ_FOREACH_SAFE(job, &ki->kaio_jobqueue, plist, jobn) {
2075 if ((uap->fd == job->uaiocb.aio_fildes) &&
2076 ((uap->aiocbp == NULL) ||
2077 (uap->aiocbp == job->ujob))) {
2078 if (aio_cancel_job(p, ki, job)) {
2083 if (uap->aiocbp != NULL)
2092 if (uap->aiocbp != NULL) {
2094 td->td_retval[0] = AIO_CANCELED;
2100 td->td_retval[0] = AIO_NOTCANCELED;
2105 td->td_retval[0] = AIO_CANCELED;
2109 td->td_retval[0] = AIO_ALLDONE;
2115 * aio_error is implemented in the kernel level for compatibility purposes
2116 * only. For a user mode async implementation, it would be best to do it in
2117 * a userland subroutine.
2120 kern_aio_error(struct thread *td, struct aiocb *ujob, struct aiocb_ops *ops)
2122 struct proc *p = td->td_proc;
2124 struct kaioinfo *ki;
2129 td->td_retval[0] = EINVAL;
2134 TAILQ_FOREACH(job, &ki->kaio_all, allist) {
2135 if (job->ujob == ujob) {
2136 if (job->jobflags & KAIOCB_FINISHED)
2138 job->uaiocb._aiocb_private.error;
2140 td->td_retval[0] = EINPROGRESS;
2148 * Hack for failure of aio_aqueue.
2150 status = ops->fetch_status(ujob);
2152 td->td_retval[0] = ops->fetch_error(ujob);
2156 td->td_retval[0] = EINVAL;
2161 sys_aio_error(struct thread *td, struct aio_error_args *uap)
2164 return (kern_aio_error(td, uap->aiocbp, &aiocb_ops));
2167 /* syscall - asynchronous read from a file (REALTIME) */
2168 #ifdef COMPAT_FREEBSD6
2170 freebsd6_aio_read(struct thread *td, struct freebsd6_aio_read_args *uap)
2173 return (aio_aqueue(td, (struct aiocb *)uap->aiocbp, NULL, LIO_READ,
2174 &aiocb_ops_osigevent));
2179 sys_aio_read(struct thread *td, struct aio_read_args *uap)
2182 return (aio_aqueue(td, uap->aiocbp, NULL, LIO_READ, &aiocb_ops));
2186 sys_aio_readv(struct thread *td, struct aio_readv_args *uap)
2189 return (aio_aqueue(td, uap->aiocbp, NULL, LIO_READV, &aiocb_ops));
2192 /* syscall - asynchronous write to a file (REALTIME) */
2193 #ifdef COMPAT_FREEBSD6
2195 freebsd6_aio_write(struct thread *td, struct freebsd6_aio_write_args *uap)
2198 return (aio_aqueue(td, (struct aiocb *)uap->aiocbp, NULL, LIO_WRITE,
2199 &aiocb_ops_osigevent));
2204 sys_aio_write(struct thread *td, struct aio_write_args *uap)
2207 return (aio_aqueue(td, uap->aiocbp, NULL, LIO_WRITE, &aiocb_ops));
2211 sys_aio_writev(struct thread *td, struct aio_writev_args *uap)
2214 return (aio_aqueue(td, uap->aiocbp, NULL, LIO_WRITEV, &aiocb_ops));
2218 sys_aio_mlock(struct thread *td, struct aio_mlock_args *uap)
2221 return (aio_aqueue(td, uap->aiocbp, NULL, LIO_MLOCK, &aiocb_ops));
2225 kern_lio_listio(struct thread *td, int mode, struct aiocb * const *uacb_list,
2226 struct aiocb **acb_list, int nent, struct sigevent *sig,
2227 struct aiocb_ops *ops)
2229 struct proc *p = td->td_proc;
2231 struct kaioinfo *ki;
2232 struct aioliojob *lj;
2238 if ((mode != LIO_NOWAIT) && (mode != LIO_WAIT))
2241 if (nent < 0 || nent > max_aio_queue_per_proc)
2244 if (p->p_aioinfo == NULL)
2245 aio_init_aioinfo(p);
2249 lj = uma_zalloc(aiolio_zone, M_WAITOK);
2252 lj->lioj_finished_count = 0;
2253 lj->lioj_signal.sigev_notify = SIGEV_NONE;
2254 knlist_init_mtx(&lj->klist, AIO_MTX(ki));
2255 ksiginfo_init(&lj->lioj_ksi);
2260 if (sig && (mode == LIO_NOWAIT)) {
2261 bcopy(sig, &lj->lioj_signal, sizeof(lj->lioj_signal));
2262 if (lj->lioj_signal.sigev_notify == SIGEV_KEVENT) {
2263 /* Assume only new style KEVENT */
2264 memset(&kev, 0, sizeof(kev));
2265 kev.filter = EVFILT_LIO;
2266 kev.flags = EV_ADD | EV_ENABLE | EV_FLAG1;
2267 kev.ident = (uintptr_t)uacb_list; /* something unique */
2268 kev.data = (intptr_t)lj;
2269 /* pass user defined sigval data */
2270 kev.udata = lj->lioj_signal.sigev_value.sival_ptr;
2271 error = kqfd_register(
2272 lj->lioj_signal.sigev_notify_kqueue, &kev, td,
2275 uma_zfree(aiolio_zone, lj);
2278 } else if (lj->lioj_signal.sigev_notify == SIGEV_NONE) {
2280 } else if (lj->lioj_signal.sigev_notify == SIGEV_SIGNAL ||
2281 lj->lioj_signal.sigev_notify == SIGEV_THREAD_ID) {
2282 if (!_SIG_VALID(lj->lioj_signal.sigev_signo)) {
2283 uma_zfree(aiolio_zone, lj);
2286 lj->lioj_flags |= LIOJ_SIGNAL;
2288 uma_zfree(aiolio_zone, lj);
2294 TAILQ_INSERT_TAIL(&ki->kaio_liojoblist, lj, lioj_list);
2296 * Add extra aiocb count to avoid the lio to be freed
2297 * by other threads doing aio_waitcomplete or aio_return,
2298 * and prevent event from being sent until we have queued
2305 * Get pointers to the list of I/O requests.
2309 for (i = 0; i < nent; i++) {
2312 error = aio_aqueue(td, job, lj, LIO_NOP, ops);
2313 if (error == EAGAIN)
2315 else if (error != 0)
2322 if (mode == LIO_WAIT) {
2323 while (lj->lioj_count - 1 != lj->lioj_finished_count) {
2324 ki->kaio_flags |= KAIO_WAKEUP;
2325 error = msleep(&p->p_aioinfo, AIO_MTX(ki),
2326 PRIBIO | PCATCH, "aiospn", 0);
2327 if (error == ERESTART)
2333 if (lj->lioj_count - 1 == lj->lioj_finished_count) {
2334 if (lj->lioj_signal.sigev_notify == SIGEV_KEVENT) {
2335 lj->lioj_flags |= LIOJ_KEVENT_POSTED;
2336 KNOTE_LOCKED(&lj->klist, 1);
2338 if ((lj->lioj_flags & (LIOJ_SIGNAL |
2339 LIOJ_SIGNAL_POSTED)) == LIOJ_SIGNAL &&
2340 (lj->lioj_signal.sigev_notify == SIGEV_SIGNAL ||
2341 lj->lioj_signal.sigev_notify == SIGEV_THREAD_ID)) {
2342 aio_sendsig(p, &lj->lioj_signal, &lj->lioj_ksi,
2343 lj->lioj_count != 1);
2344 lj->lioj_flags |= LIOJ_SIGNAL_POSTED;
2349 if (lj->lioj_count == 0) {
2350 TAILQ_REMOVE(&ki->kaio_liojoblist, lj, lioj_list);
2351 knlist_delete(&lj->klist, curthread, 1);
2353 sigqueue_take(&lj->lioj_ksi);
2356 uma_zfree(aiolio_zone, lj);
2368 /* syscall - list directed I/O (REALTIME) */
2369 #ifdef COMPAT_FREEBSD6
2371 freebsd6_lio_listio(struct thread *td, struct freebsd6_lio_listio_args *uap)
2373 struct aiocb **acb_list;
2374 struct sigevent *sigp, sig;
2375 struct osigevent osig;
2378 if ((uap->mode != LIO_NOWAIT) && (uap->mode != LIO_WAIT))
2382 if (nent < 0 || nent > max_aio_queue_per_proc)
2385 if (uap->sig && (uap->mode == LIO_NOWAIT)) {
2386 error = copyin(uap->sig, &osig, sizeof(osig));
2389 error = convert_old_sigevent(&osig, &sig);
2396 acb_list = malloc(sizeof(struct aiocb *) * nent, M_LIO, M_WAITOK);
2397 error = copyin(uap->acb_list, acb_list, nent * sizeof(acb_list[0]));
2399 error = kern_lio_listio(td, uap->mode,
2400 (struct aiocb * const *)uap->acb_list, acb_list, nent, sigp,
2401 &aiocb_ops_osigevent);
2402 free(acb_list, M_LIO);
2407 /* syscall - list directed I/O (REALTIME) */
2409 sys_lio_listio(struct thread *td, struct lio_listio_args *uap)
2411 struct aiocb **acb_list;
2412 struct sigevent *sigp, sig;
2415 if ((uap->mode != LIO_NOWAIT) && (uap->mode != LIO_WAIT))
2419 if (nent < 0 || nent > max_aio_queue_per_proc)
2422 if (uap->sig && (uap->mode == LIO_NOWAIT)) {
2423 error = copyin(uap->sig, &sig, sizeof(sig));
2430 acb_list = malloc(sizeof(struct aiocb *) * nent, M_LIO, M_WAITOK);
2431 error = copyin(uap->acb_list, acb_list, nent * sizeof(acb_list[0]));
2433 error = kern_lio_listio(td, uap->mode, uap->acb_list, acb_list,
2434 nent, sigp, &aiocb_ops);
2435 free(acb_list, M_LIO);
2440 aio_biocleanup(struct bio *bp)
2442 struct kaiocb *job = (struct kaiocb *)bp->bio_caller1;
2443 struct kaioinfo *ki;
2444 struct buf *pbuf = (struct buf *)bp->bio_caller2;
2446 /* Release mapping into kernel space. */
2448 MPASS(pbuf->b_npages <= atop(maxphys) + 1);
2449 pmap_qremove((vm_offset_t)pbuf->b_data, pbuf->b_npages);
2450 vm_page_unhold_pages(pbuf->b_pages, pbuf->b_npages);
2451 uma_zfree(pbuf_zone, pbuf);
2452 atomic_subtract_int(&num_buf_aio, 1);
2453 ki = job->userproc->p_aioinfo;
2455 ki->kaio_buffer_count--;
2458 MPASS(bp->bio_ma_n <= atop(maxphys) + 1);
2459 vm_page_unhold_pages(bp->bio_ma, bp->bio_ma_n);
2460 free(bp->bio_ma, M_TEMP);
2461 atomic_subtract_int(&num_unmapped_aio, 1);
2467 aio_biowakeup(struct bio *bp)
2469 struct kaiocb *job = (struct kaiocb *)bp->bio_caller1;
2471 long bcount = bp->bio_bcount;
2472 long resid = bp->bio_resid;
2473 int error, opcode, nblks;
2474 int bio_error = bp->bio_error;
2475 uint16_t flags = bp->bio_flags;
2477 opcode = job->uaiocb.aio_lio_opcode;
2481 nbytes =bcount - resid;
2482 atomic_add_acq_long(&job->nbytes, nbytes);
2483 nblks = btodb(nbytes);
2486 * If multiple bios experienced an error, the job will reflect the
2487 * error of whichever failed bio completed last.
2489 if (flags & BIO_ERROR)
2490 atomic_set_int(&job->error, bio_error);
2491 if (opcode & LIO_WRITE)
2492 atomic_add_int(&job->outblock, nblks);
2494 atomic_add_int(&job->inblock, nblks);
2495 atomic_subtract_int(&job->nbio, 1);
2498 if (atomic_load_int(&job->nbio) == 0) {
2499 if (atomic_load_int(&job->error))
2500 aio_complete(job, -1, job->error);
2502 aio_complete(job, atomic_load_long(&job->nbytes), 0);
2506 /* syscall - wait for the next completion of an aio request */
2508 kern_aio_waitcomplete(struct thread *td, struct aiocb **ujobp,
2509 struct timespec *ts, struct aiocb_ops *ops)
2511 struct proc *p = td->td_proc;
2513 struct kaioinfo *ki;
2519 ops->store_aiocb(ujobp, NULL);
2523 } else if (ts->tv_sec == 0 && ts->tv_nsec == 0) {
2526 if ((ts->tv_nsec < 0) || (ts->tv_nsec >= 1000000000))
2529 TIMESPEC_TO_TIMEVAL(&atv, ts);
2530 if (itimerfix(&atv))
2532 timo = tvtohz(&atv);
2535 if (p->p_aioinfo == NULL)
2536 aio_init_aioinfo(p);
2542 while ((job = TAILQ_FIRST(&ki->kaio_done)) == NULL) {
2544 error = EWOULDBLOCK;
2547 ki->kaio_flags |= KAIO_WAKEUP;
2548 error = msleep(&p->p_aioinfo, AIO_MTX(ki), PRIBIO | PCATCH,
2550 if (timo && error == ERESTART)
2557 MPASS(job->jobflags & KAIOCB_FINISHED);
2559 status = job->uaiocb._aiocb_private.status;
2560 error = job->uaiocb._aiocb_private.error;
2561 td->td_retval[0] = status;
2562 td->td_ru.ru_oublock += job->outblock;
2563 td->td_ru.ru_inblock += job->inblock;
2564 td->td_ru.ru_msgsnd += job->msgsnd;
2565 td->td_ru.ru_msgrcv += job->msgrcv;
2566 aio_free_entry(job);
2568 ops->store_aiocb(ujobp, ujob);
2569 ops->store_error(ujob, error);
2570 ops->store_status(ujob, status);
2578 sys_aio_waitcomplete(struct thread *td, struct aio_waitcomplete_args *uap)
2580 struct timespec ts, *tsp;
2584 /* Get timespec struct. */
2585 error = copyin(uap->timeout, &ts, sizeof(ts));
2592 return (kern_aio_waitcomplete(td, uap->aiocbp, tsp, &aiocb_ops));
2596 kern_aio_fsync(struct thread *td, int op, struct aiocb *ujob,
2597 struct aiocb_ops *ops)
2612 return (aio_aqueue(td, ujob, NULL, listop, ops));
2616 sys_aio_fsync(struct thread *td, struct aio_fsync_args *uap)
2619 return (kern_aio_fsync(td, uap->op, uap->aiocbp, &aiocb_ops));
2622 /* kqueue attach function */
2624 filt_aioattach(struct knote *kn)
2628 job = (struct kaiocb *)(uintptr_t)kn->kn_sdata;
2631 * The job pointer must be validated before using it, so
2632 * registration is restricted to the kernel; the user cannot
2635 if ((kn->kn_flags & EV_FLAG1) == 0)
2637 kn->kn_ptr.p_aio = job;
2638 kn->kn_flags &= ~EV_FLAG1;
2640 knlist_add(&job->klist, kn, 0);
2645 /* kqueue detach function */
2647 filt_aiodetach(struct knote *kn)
2651 knl = &kn->kn_ptr.p_aio->klist;
2652 knl->kl_lock(knl->kl_lockarg);
2653 if (!knlist_empty(knl))
2654 knlist_remove(knl, kn, 1);
2655 knl->kl_unlock(knl->kl_lockarg);
2658 /* kqueue filter function */
2661 filt_aio(struct knote *kn, long hint)
2663 struct kaiocb *job = kn->kn_ptr.p_aio;
2665 kn->kn_data = job->uaiocb._aiocb_private.error;
2666 if (!(job->jobflags & KAIOCB_FINISHED))
2668 kn->kn_flags |= EV_EOF;
2672 /* kqueue attach function */
2674 filt_lioattach(struct knote *kn)
2676 struct aioliojob *lj;
2678 lj = (struct aioliojob *)(uintptr_t)kn->kn_sdata;
2681 * The aioliojob pointer must be validated before using it, so
2682 * registration is restricted to the kernel; the user cannot
2685 if ((kn->kn_flags & EV_FLAG1) == 0)
2687 kn->kn_ptr.p_lio = lj;
2688 kn->kn_flags &= ~EV_FLAG1;
2690 knlist_add(&lj->klist, kn, 0);
2695 /* kqueue detach function */
2697 filt_liodetach(struct knote *kn)
2701 knl = &kn->kn_ptr.p_lio->klist;
2702 knl->kl_lock(knl->kl_lockarg);
2703 if (!knlist_empty(knl))
2704 knlist_remove(knl, kn, 1);
2705 knl->kl_unlock(knl->kl_lockarg);
2708 /* kqueue filter function */
2711 filt_lio(struct knote *kn, long hint)
2713 struct aioliojob * lj = kn->kn_ptr.p_lio;
2715 return (lj->lioj_flags & LIOJ_KEVENT_POSTED);
2718 #ifdef COMPAT_FREEBSD32
2719 #include <sys/mount.h>
2720 #include <sys/socket.h>
2721 #include <sys/sysent.h>
2722 #include <compat/freebsd32/freebsd32.h>
2723 #include <compat/freebsd32/freebsd32_proto.h>
2724 #include <compat/freebsd32/freebsd32_signal.h>
2725 #include <compat/freebsd32/freebsd32_syscall.h>
2726 #include <compat/freebsd32/freebsd32_util.h>
2728 struct __aiocb_private32 {
2731 uint32_t kernelinfo;
2734 #ifdef COMPAT_FREEBSD6
2735 typedef struct oaiocb32 {
2736 int aio_fildes; /* File descriptor */
2737 uint64_t aio_offset __packed; /* File offset for I/O */
2738 uint32_t aio_buf; /* I/O buffer in process space */
2739 uint32_t aio_nbytes; /* Number of bytes for I/O */
2740 struct osigevent32 aio_sigevent; /* Signal to deliver */
2741 int aio_lio_opcode; /* LIO opcode */
2742 int aio_reqprio; /* Request priority -- ignored */
2743 struct __aiocb_private32 _aiocb_private;
2747 typedef struct aiocb32 {
2748 int32_t aio_fildes; /* File descriptor */
2749 uint64_t aio_offset __packed; /* File offset for I/O */
2750 uint32_t aio_buf; /* I/O buffer in process space */
2751 uint32_t aio_nbytes; /* Number of bytes for I/O */
2753 uint32_t __spare2__;
2754 int aio_lio_opcode; /* LIO opcode */
2755 int aio_reqprio; /* Request priority -- ignored */
2756 struct __aiocb_private32 _aiocb_private;
2757 struct sigevent32 aio_sigevent; /* Signal to deliver */
2760 #ifdef COMPAT_FREEBSD6
2762 convert_old_sigevent32(struct osigevent32 *osig, struct sigevent *nsig)
2766 * Only SIGEV_NONE, SIGEV_SIGNAL, and SIGEV_KEVENT are
2767 * supported by AIO with the old sigevent structure.
2769 CP(*osig, *nsig, sigev_notify);
2770 switch (nsig->sigev_notify) {
2774 nsig->sigev_signo = osig->__sigev_u.__sigev_signo;
2777 nsig->sigev_notify_kqueue =
2778 osig->__sigev_u.__sigev_notify_kqueue;
2779 PTRIN_CP(*osig, *nsig, sigev_value.sival_ptr);
2788 aiocb32_copyin_old_sigevent(struct aiocb *ujob, struct kaiocb *kjob,
2791 struct oaiocb32 job32;
2792 struct aiocb *kcb = &kjob->uaiocb;
2795 bzero(kcb, sizeof(struct aiocb));
2796 error = copyin(ujob, &job32, sizeof(job32));
2800 /* No need to copyin aio_iov, because it did not exist in FreeBSD 6 */
2802 CP(job32, *kcb, aio_fildes);
2803 CP(job32, *kcb, aio_offset);
2804 PTRIN_CP(job32, *kcb, aio_buf);
2805 CP(job32, *kcb, aio_nbytes);
2806 CP(job32, *kcb, aio_lio_opcode);
2807 CP(job32, *kcb, aio_reqprio);
2808 CP(job32, *kcb, _aiocb_private.status);
2809 CP(job32, *kcb, _aiocb_private.error);
2810 PTRIN_CP(job32, *kcb, _aiocb_private.kernelinfo);
2811 return (convert_old_sigevent32(&job32.aio_sigevent,
2812 &kcb->aio_sigevent));
2817 aiocb32_copyin(struct aiocb *ujob, struct kaiocb *kjob, int type)
2819 struct aiocb32 job32;
2820 struct aiocb *kcb = &kjob->uaiocb;
2821 struct iovec32 *iov32;
2824 error = copyin(ujob, &job32, sizeof(job32));
2827 CP(job32, *kcb, aio_fildes);
2828 CP(job32, *kcb, aio_offset);
2829 CP(job32, *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);