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>
70 #include <vm/vnode_pager.h>
75 * Counter for allocating reference ids to new jobs. Wrapped to 1 on
76 * overflow. (XXX will be removed soon.)
78 static u_long jobrefid;
81 * Counter for aio_fsync.
83 static uint64_t jobseqno;
85 #ifndef MAX_AIO_PER_PROC
86 #define MAX_AIO_PER_PROC 32
89 #ifndef MAX_AIO_QUEUE_PER_PROC
90 #define MAX_AIO_QUEUE_PER_PROC 256
94 #define MAX_AIO_QUEUE 1024 /* Bigger than MAX_AIO_QUEUE_PER_PROC */
98 #define MAX_BUF_AIO 16
101 FEATURE(aio, "Asynchronous I/O");
102 SYSCTL_DECL(_p1003_1b);
104 static MALLOC_DEFINE(M_LIO, "lio", "listio aio control block list");
105 static MALLOC_DEFINE(M_AIO, "aio", "structures for asynchronous I/O");
107 static SYSCTL_NODE(_vfs, OID_AUTO, aio, CTLFLAG_RW | CTLFLAG_MPSAFE, 0,
108 "Async IO management");
110 static int enable_aio_unsafe = 0;
111 SYSCTL_INT(_vfs_aio, OID_AUTO, enable_unsafe, CTLFLAG_RW, &enable_aio_unsafe, 0,
112 "Permit asynchronous IO on all file types, not just known-safe types");
114 static unsigned int unsafe_warningcnt = 1;
115 SYSCTL_UINT(_vfs_aio, OID_AUTO, unsafe_warningcnt, CTLFLAG_RW,
116 &unsafe_warningcnt, 0,
117 "Warnings that will be triggered upon failed IO requests on unsafe files");
119 static int max_aio_procs = MAX_AIO_PROCS;
120 SYSCTL_INT(_vfs_aio, OID_AUTO, max_aio_procs, CTLFLAG_RW, &max_aio_procs, 0,
121 "Maximum number of kernel processes to use for handling async IO ");
123 static int num_aio_procs = 0;
124 SYSCTL_INT(_vfs_aio, OID_AUTO, num_aio_procs, CTLFLAG_RD, &num_aio_procs, 0,
125 "Number of presently active kernel processes for async IO");
128 * The code will adjust the actual number of AIO processes towards this
129 * number when it gets a chance.
131 static int target_aio_procs = TARGET_AIO_PROCS;
132 SYSCTL_INT(_vfs_aio, OID_AUTO, target_aio_procs, CTLFLAG_RW, &target_aio_procs,
134 "Preferred number of ready kernel processes for async IO");
136 static int max_queue_count = MAX_AIO_QUEUE;
137 SYSCTL_INT(_vfs_aio, OID_AUTO, max_aio_queue, CTLFLAG_RW, &max_queue_count, 0,
138 "Maximum number of aio requests to queue, globally");
140 static int num_queue_count = 0;
141 SYSCTL_INT(_vfs_aio, OID_AUTO, num_queue_count, CTLFLAG_RD, &num_queue_count, 0,
142 "Number of queued aio requests");
144 static int num_buf_aio = 0;
145 SYSCTL_INT(_vfs_aio, OID_AUTO, num_buf_aio, CTLFLAG_RD, &num_buf_aio, 0,
146 "Number of aio requests presently handled by the buf subsystem");
148 static int num_unmapped_aio = 0;
149 SYSCTL_INT(_vfs_aio, OID_AUTO, num_unmapped_aio, CTLFLAG_RD, &num_unmapped_aio,
151 "Number of aio requests presently handled by unmapped I/O buffers");
153 /* Number of async I/O processes in the process of being started */
154 /* XXX This should be local to aio_aqueue() */
155 static int num_aio_resv_start = 0;
157 static int aiod_lifetime;
158 SYSCTL_INT(_vfs_aio, OID_AUTO, aiod_lifetime, CTLFLAG_RW, &aiod_lifetime, 0,
159 "Maximum lifetime for idle aiod");
161 static int max_aio_per_proc = MAX_AIO_PER_PROC;
162 SYSCTL_INT(_vfs_aio, OID_AUTO, max_aio_per_proc, CTLFLAG_RW, &max_aio_per_proc,
164 "Maximum active aio requests per process");
166 static int max_aio_queue_per_proc = MAX_AIO_QUEUE_PER_PROC;
167 SYSCTL_INT(_vfs_aio, OID_AUTO, max_aio_queue_per_proc, CTLFLAG_RW,
168 &max_aio_queue_per_proc, 0,
169 "Maximum queued aio requests per process");
171 static int max_buf_aio = MAX_BUF_AIO;
172 SYSCTL_INT(_vfs_aio, OID_AUTO, max_buf_aio, CTLFLAG_RW, &max_buf_aio, 0,
173 "Maximum buf aio requests per process");
176 * Though redundant with vfs.aio.max_aio_queue_per_proc, POSIX requires
177 * sysconf(3) to support AIO_LISTIO_MAX, and we implement that with
178 * vfs.aio.aio_listio_max.
180 SYSCTL_INT(_p1003_1b, CTL_P1003_1B_AIO_LISTIO_MAX, aio_listio_max,
181 CTLFLAG_RD | CTLFLAG_CAPRD, &max_aio_queue_per_proc,
182 0, "Maximum aio requests for a single lio_listio call");
184 #ifdef COMPAT_FREEBSD6
185 typedef struct oaiocb {
186 int aio_fildes; /* File descriptor */
187 off_t aio_offset; /* File offset for I/O */
188 volatile void *aio_buf; /* I/O buffer in process space */
189 size_t aio_nbytes; /* Number of bytes for I/O */
190 struct osigevent aio_sigevent; /* Signal to deliver */
191 int aio_lio_opcode; /* LIO opcode */
192 int aio_reqprio; /* Request priority -- ignored */
193 struct __aiocb_private _aiocb_private;
198 * Below is a key of locks used to protect each member of struct kaiocb
199 * aioliojob and kaioinfo and any backends.
201 * * - need not protected
202 * a - locked by kaioinfo lock
203 * b - locked by backend lock, the backend lock can be null in some cases,
204 * for example, BIO belongs to this type, in this case, proc lock is
206 * c - locked by aio_job_mtx, the lock for the generic file I/O backend.
210 * If the routine that services an AIO request blocks while running in an
211 * AIO kernel process it can starve other I/O requests. BIO requests
212 * queued via aio_qbio() complete asynchronously and do not use AIO kernel
213 * processes at all. Socket I/O requests use a separate pool of
214 * kprocs and also force non-blocking I/O. Other file I/O requests
215 * use the generic fo_read/fo_write operations which can block. The
216 * fsync and mlock operations can also block while executing. Ideally
217 * none of these requests would block while executing.
219 * Note that the service routines cannot toggle O_NONBLOCK in the file
220 * structure directly while handling a request due to races with
225 #define KAIOCB_QUEUEING 0x01
226 #define KAIOCB_CANCELLED 0x02
227 #define KAIOCB_CANCELLING 0x04
228 #define KAIOCB_CHECKSYNC 0x08
229 #define KAIOCB_CLEARED 0x10
230 #define KAIOCB_FINISHED 0x20
233 #define KAIOCB_IO_FOFFSET 0x01
238 #define AIOP_FREE 0x1 /* proc on free queue */
241 int aioprocflags; /* (c) AIO proc flags */
242 TAILQ_ENTRY(aioproc) list; /* (c) list of processes */
243 struct proc *aioproc; /* (*) the AIO proc */
247 * data-structure for lio signal management
250 int lioj_flags; /* (a) listio flags */
251 int lioj_count; /* (a) count of jobs */
252 int lioj_finished_count; /* (a) count of finished jobs */
253 struct sigevent lioj_signal; /* (a) signal on all I/O done */
254 TAILQ_ENTRY(aioliojob) lioj_list; /* (a) lio list */
255 struct knlist klist; /* (a) list of knotes */
256 ksiginfo_t lioj_ksi; /* (a) Realtime signal info */
259 #define LIOJ_SIGNAL 0x1 /* signal on all done (lio) */
260 #define LIOJ_SIGNAL_POSTED 0x2 /* signal has been posted */
261 #define LIOJ_KEVENT_POSTED 0x4 /* kevent triggered */
264 * per process aio data structure
267 struct mtx kaio_mtx; /* the lock to protect this struct */
268 int kaio_flags; /* (a) per process kaio flags */
269 int kaio_active_count; /* (c) number of currently used AIOs */
270 int kaio_count; /* (a) size of AIO queue */
271 int kaio_buffer_count; /* (a) number of bio buffers */
272 TAILQ_HEAD(,kaiocb) kaio_all; /* (a) all AIOs in a process */
273 TAILQ_HEAD(,kaiocb) kaio_done; /* (a) done queue for process */
274 TAILQ_HEAD(,aioliojob) kaio_liojoblist; /* (a) list of lio jobs */
275 TAILQ_HEAD(,kaiocb) kaio_jobqueue; /* (a) job queue for process */
276 TAILQ_HEAD(,kaiocb) kaio_syncqueue; /* (a) queue for aio_fsync */
277 TAILQ_HEAD(,kaiocb) kaio_syncready; /* (a) second q for aio_fsync */
278 struct task kaio_task; /* (*) task to kick aio processes */
279 struct task kaio_sync_task; /* (*) task to schedule fsync jobs */
282 #define AIO_LOCK(ki) mtx_lock(&(ki)->kaio_mtx)
283 #define AIO_UNLOCK(ki) mtx_unlock(&(ki)->kaio_mtx)
284 #define AIO_LOCK_ASSERT(ki, f) mtx_assert(&(ki)->kaio_mtx, (f))
285 #define AIO_MTX(ki) (&(ki)->kaio_mtx)
287 #define KAIO_RUNDOWN 0x1 /* process is being run down */
288 #define KAIO_WAKEUP 0x2 /* wakeup process when AIO completes */
291 * Operations used to interact with userland aio control blocks.
292 * Different ABIs provide their own operations.
295 int (*aio_copyin)(struct aiocb *ujob, struct kaiocb *kjob, int ty);
296 long (*fetch_status)(struct aiocb *ujob);
297 long (*fetch_error)(struct aiocb *ujob);
298 int (*store_status)(struct aiocb *ujob, long status);
299 int (*store_error)(struct aiocb *ujob, long error);
300 int (*store_kernelinfo)(struct aiocb *ujob, long jobref);
301 int (*store_aiocb)(struct aiocb **ujobp, struct aiocb *ujob);
304 static TAILQ_HEAD(,aioproc) aio_freeproc; /* (c) Idle daemons */
305 static struct sema aio_newproc_sem;
306 static struct mtx aio_job_mtx;
307 static TAILQ_HEAD(,kaiocb) aio_jobs; /* (c) Async job list */
308 static struct unrhdr *aiod_unr;
310 static void aio_biocleanup(struct bio *bp);
311 void aio_init_aioinfo(struct proc *p);
312 static int aio_onceonly(void);
313 static int aio_free_entry(struct kaiocb *job);
314 static void aio_process_rw(struct kaiocb *job);
315 static void aio_process_sync(struct kaiocb *job);
316 static void aio_process_mlock(struct kaiocb *job);
317 static void aio_schedule_fsync(void *context, int pending);
318 static int aio_newproc(int *);
319 int aio_aqueue(struct thread *td, struct aiocb *ujob,
320 struct aioliojob *lio, int type, struct aiocb_ops *ops);
321 static int aio_queue_file(struct file *fp, struct kaiocb *job);
322 static void aio_biowakeup(struct bio *bp);
323 static void aio_proc_rundown(void *arg, struct proc *p);
324 static void aio_proc_rundown_exec(void *arg, struct proc *p,
325 struct image_params *imgp);
326 static int aio_qbio(struct proc *p, struct kaiocb *job);
327 static void aio_daemon(void *param);
328 static void aio_bio_done_notify(struct proc *userp, struct kaiocb *job);
329 static bool aio_clear_cancel_function_locked(struct kaiocb *job);
330 static int aio_kick(struct proc *userp);
331 static void aio_kick_nowait(struct proc *userp);
332 static void aio_kick_helper(void *context, int pending);
333 static int filt_aioattach(struct knote *kn);
334 static void filt_aiodetach(struct knote *kn);
335 static int filt_aio(struct knote *kn, long hint);
336 static int filt_lioattach(struct knote *kn);
337 static void filt_liodetach(struct knote *kn);
338 static int filt_lio(struct knote *kn, long hint);
342 * kaio Per process async io info
343 * aiocb async io jobs
344 * aiolio list io jobs
346 static uma_zone_t kaio_zone, aiocb_zone, aiolio_zone;
348 /* kqueue filters for aio */
349 static struct filterops aio_filtops = {
351 .f_attach = filt_aioattach,
352 .f_detach = filt_aiodetach,
355 static struct filterops lio_filtops = {
357 .f_attach = filt_lioattach,
358 .f_detach = filt_liodetach,
362 static eventhandler_tag exit_tag, exec_tag;
364 TASKQUEUE_DEFINE_THREAD(aiod_kick);
367 * Main operations function for use as a kernel module.
370 aio_modload(struct module *module, int cmd, void *arg)
387 static moduledata_t aio_mod = {
393 DECLARE_MODULE(aio, aio_mod, SI_SUB_VFS, SI_ORDER_ANY);
394 MODULE_VERSION(aio, 1);
397 * Startup initialization
403 exit_tag = EVENTHANDLER_REGISTER(process_exit, aio_proc_rundown, NULL,
404 EVENTHANDLER_PRI_ANY);
405 exec_tag = EVENTHANDLER_REGISTER(process_exec, aio_proc_rundown_exec,
406 NULL, EVENTHANDLER_PRI_ANY);
407 kqueue_add_filteropts(EVFILT_AIO, &aio_filtops);
408 kqueue_add_filteropts(EVFILT_LIO, &lio_filtops);
409 TAILQ_INIT(&aio_freeproc);
410 sema_init(&aio_newproc_sem, 0, "aio_new_proc");
411 mtx_init(&aio_job_mtx, "aio_job", NULL, MTX_DEF);
412 TAILQ_INIT(&aio_jobs);
413 aiod_unr = new_unrhdr(1, INT_MAX, NULL);
414 kaio_zone = uma_zcreate("AIO", sizeof(struct kaioinfo), NULL, NULL,
415 NULL, NULL, UMA_ALIGN_PTR, 0);
416 aiocb_zone = uma_zcreate("AIOCB", sizeof(struct kaiocb), NULL, NULL,
417 NULL, NULL, UMA_ALIGN_PTR, 0);
418 aiolio_zone = uma_zcreate("AIOLIO", sizeof(struct aioliojob), NULL,
419 NULL, NULL, NULL, UMA_ALIGN_PTR, 0);
420 aiod_lifetime = AIOD_LIFETIME_DEFAULT;
422 p31b_setcfg(CTL_P1003_1B_ASYNCHRONOUS_IO, _POSIX_ASYNCHRONOUS_IO);
423 p31b_setcfg(CTL_P1003_1B_AIO_MAX, MAX_AIO_QUEUE);
424 p31b_setcfg(CTL_P1003_1B_AIO_PRIO_DELTA_MAX, 0);
430 * Init the per-process aioinfo structure. The aioinfo limits are set
431 * per-process for user limit (resource) management.
434 aio_init_aioinfo(struct proc *p)
438 ki = uma_zalloc(kaio_zone, M_WAITOK);
439 mtx_init(&ki->kaio_mtx, "aiomtx", NULL, MTX_DEF | MTX_NEW);
441 ki->kaio_active_count = 0;
443 ki->kaio_buffer_count = 0;
444 TAILQ_INIT(&ki->kaio_all);
445 TAILQ_INIT(&ki->kaio_done);
446 TAILQ_INIT(&ki->kaio_jobqueue);
447 TAILQ_INIT(&ki->kaio_liojoblist);
448 TAILQ_INIT(&ki->kaio_syncqueue);
449 TAILQ_INIT(&ki->kaio_syncready);
450 TASK_INIT(&ki->kaio_task, 0, aio_kick_helper, p);
451 TASK_INIT(&ki->kaio_sync_task, 0, aio_schedule_fsync, ki);
453 if (p->p_aioinfo == NULL) {
458 mtx_destroy(&ki->kaio_mtx);
459 uma_zfree(kaio_zone, ki);
462 while (num_aio_procs < MIN(target_aio_procs, max_aio_procs))
467 aio_sendsig(struct proc *p, struct sigevent *sigev, ksiginfo_t *ksi, bool ext)
472 error = sigev_findtd(p, sigev, &td);
476 ksiginfo_set_sigev(ksi, sigev);
477 ksi->ksi_code = SI_ASYNCIO;
478 ksi->ksi_flags |= ext ? (KSI_EXT | KSI_INS) : 0;
479 tdsendsignal(p, td, ksi->ksi_signo, ksi);
486 * Free a job entry. Wait for completion if it is currently active, but don't
487 * delay forever. If we delay, we return a flag that says that we have to
488 * restart the queue scan.
491 aio_free_entry(struct kaiocb *job)
494 struct aioliojob *lj;
502 AIO_LOCK_ASSERT(ki, MA_OWNED);
503 MPASS(job->jobflags & KAIOCB_FINISHED);
505 atomic_subtract_int(&num_queue_count, 1);
508 MPASS(ki->kaio_count >= 0);
510 TAILQ_REMOVE(&ki->kaio_done, job, plist);
511 TAILQ_REMOVE(&ki->kaio_all, job, allist);
516 lj->lioj_finished_count--;
518 if (lj->lioj_count == 0) {
519 TAILQ_REMOVE(&ki->kaio_liojoblist, lj, lioj_list);
520 /* lio is going away, we need to destroy any knotes */
521 knlist_delete(&lj->klist, curthread, 1);
523 sigqueue_take(&lj->lioj_ksi);
525 uma_zfree(aiolio_zone, lj);
529 /* job is going away, we need to destroy any knotes */
530 knlist_delete(&job->klist, curthread, 1);
532 sigqueue_take(&job->ksi);
538 * The thread argument here is used to find the owning process
539 * and is also passed to fo_close() which may pass it to various
540 * places such as devsw close() routines. Because of that, we
541 * need a thread pointer from the process owning the job that is
542 * persistent and won't disappear out from under us or move to
545 * Currently, all the callers of this function call it to remove
546 * a kaiocb from the current process' job list either via a
547 * syscall or due to the current process calling exit() or
548 * execve(). Thus, we know that p == curproc. We also know that
549 * curthread can't exit since we are curthread.
551 * Therefore, we use curthread as the thread to pass to
552 * knlist_delete(). This does mean that it is possible for the
553 * thread pointer at close time to differ from the thread pointer
554 * at open time, but this is already true of file descriptors in
555 * a multithreaded process.
558 fdrop(job->fd_file, curthread);
560 if (job->uiop != &job->uio)
562 uma_zfree(aiocb_zone, job);
569 aio_proc_rundown_exec(void *arg, struct proc *p,
570 struct image_params *imgp __unused)
572 aio_proc_rundown(arg, p);
576 aio_cancel_job(struct proc *p, struct kaioinfo *ki, struct kaiocb *job)
578 aio_cancel_fn_t *func;
581 AIO_LOCK_ASSERT(ki, MA_OWNED);
582 if (job->jobflags & (KAIOCB_CANCELLED | KAIOCB_FINISHED))
584 MPASS((job->jobflags & KAIOCB_CANCELLING) == 0);
585 job->jobflags |= KAIOCB_CANCELLED;
587 func = job->cancel_fn;
590 * If there is no cancel routine, just leave the job marked as
591 * cancelled. The job should be in active use by a caller who
592 * should complete it normally or when it fails to install a
599 * Set the CANCELLING flag so that aio_complete() will defer
600 * completions of this job. This prevents the job from being
601 * freed out from under the cancel callback. After the
602 * callback any deferred completion (whether from the callback
603 * or any other source) will be completed.
605 job->jobflags |= KAIOCB_CANCELLING;
609 job->jobflags &= ~KAIOCB_CANCELLING;
610 if (job->jobflags & KAIOCB_FINISHED) {
611 cancelled = job->uaiocb._aiocb_private.error == ECANCELED;
612 TAILQ_REMOVE(&ki->kaio_jobqueue, job, plist);
613 aio_bio_done_notify(p, job);
616 * The cancel callback might have scheduled an
617 * operation to cancel this request, but it is
618 * only counted as cancelled if the request is
619 * cancelled when the callback returns.
627 * Rundown the jobs for a given process.
630 aio_proc_rundown(void *arg, struct proc *p)
633 struct aioliojob *lj;
634 struct kaiocb *job, *jobn;
636 KASSERT(curthread->td_proc == p,
637 ("%s: called on non-curproc", __func__));
643 ki->kaio_flags |= KAIO_RUNDOWN;
648 * Try to cancel all pending requests. This code simulates
649 * aio_cancel on all pending I/O requests.
651 TAILQ_FOREACH_SAFE(job, &ki->kaio_jobqueue, plist, jobn) {
652 aio_cancel_job(p, ki, job);
655 /* Wait for all running I/O to be finished */
656 if (TAILQ_FIRST(&ki->kaio_jobqueue) || ki->kaio_active_count != 0) {
657 ki->kaio_flags |= KAIO_WAKEUP;
658 msleep(&p->p_aioinfo, AIO_MTX(ki), PRIBIO, "aioprn", hz);
662 /* Free all completed I/O requests. */
663 while ((job = TAILQ_FIRST(&ki->kaio_done)) != NULL)
666 while ((lj = TAILQ_FIRST(&ki->kaio_liojoblist)) != NULL) {
667 if (lj->lioj_count == 0) {
668 TAILQ_REMOVE(&ki->kaio_liojoblist, lj, lioj_list);
669 knlist_delete(&lj->klist, curthread, 1);
671 sigqueue_take(&lj->lioj_ksi);
673 uma_zfree(aiolio_zone, lj);
675 panic("LIO job not cleaned up: C:%d, FC:%d\n",
676 lj->lioj_count, lj->lioj_finished_count);
680 taskqueue_drain(taskqueue_aiod_kick, &ki->kaio_task);
681 taskqueue_drain(taskqueue_aiod_kick, &ki->kaio_sync_task);
682 mtx_destroy(&ki->kaio_mtx);
683 uma_zfree(kaio_zone, ki);
688 * Select a job to run (called by an AIO daemon).
690 static struct kaiocb *
691 aio_selectjob(struct aioproc *aiop)
697 mtx_assert(&aio_job_mtx, MA_OWNED);
699 TAILQ_FOREACH(job, &aio_jobs, list) {
700 userp = job->userproc;
701 ki = userp->p_aioinfo;
703 if (ki->kaio_active_count < max_aio_per_proc) {
704 TAILQ_REMOVE(&aio_jobs, job, list);
705 if (!aio_clear_cancel_function(job))
708 /* Account for currently active jobs. */
709 ki->kaio_active_count++;
717 * Move all data to a permanent storage device. This code
718 * simulates the fsync and fdatasync syscalls.
721 aio_fsync_vnode(struct thread *td, struct vnode *vp, int op)
727 error = vn_start_write(vp, &mp, V_WAIT | V_PCATCH);
730 vn_lock(vp, LK_EXCLUSIVE | LK_RETRY);
731 vnode_pager_clean_async(vp);
733 error = VOP_FDATASYNC(vp, td);
735 error = VOP_FSYNC(vp, MNT_WAIT, td);
738 vn_finished_write(mp);
739 if (error != ERELOOKUP)
746 * The AIO processing activity for LIO_READ/LIO_WRITE. This is the code that
747 * does the I/O request for the non-bio version of the operations. The normal
748 * vn operations are used, and this code should work in all instances for every
749 * type of file, including pipes, sockets, fifos, and regular files.
751 * XXX I don't think it works well for socket, pipe, and fifo.
754 aio_process_rw(struct kaiocb *job)
756 struct ucred *td_savedcred;
760 long msgsnd_st, msgsnd_end;
761 long msgrcv_st, msgrcv_end;
762 long oublock_st, oublock_end;
763 long inblock_st, inblock_end;
766 KASSERT(job->uaiocb.aio_lio_opcode == LIO_READ ||
767 job->uaiocb.aio_lio_opcode == LIO_READV ||
768 job->uaiocb.aio_lio_opcode == LIO_WRITE ||
769 job->uaiocb.aio_lio_opcode == LIO_WRITEV,
770 ("%s: opcode %d", __func__, job->uaiocb.aio_lio_opcode));
772 aio_switch_vmspace(job);
774 td_savedcred = td->td_ucred;
775 td->td_ucred = job->cred;
776 job->uiop->uio_td = td;
779 opcode = job->uaiocb.aio_lio_opcode;
780 cnt = job->uiop->uio_resid;
782 msgrcv_st = td->td_ru.ru_msgrcv;
783 msgsnd_st = td->td_ru.ru_msgsnd;
784 inblock_st = td->td_ru.ru_inblock;
785 oublock_st = td->td_ru.ru_oublock;
788 * aio_aqueue() acquires a reference to the file that is
789 * released in aio_free_entry().
791 if (opcode == LIO_READ || opcode == LIO_READV) {
792 if (job->uiop->uio_resid == 0)
795 error = fo_read(fp, job->uiop, fp->f_cred,
796 (job->ioflags & KAIOCB_IO_FOFFSET) != 0 ? 0 :
799 if (fp->f_type == DTYPE_VNODE)
801 error = fo_write(fp, job->uiop, fp->f_cred, (job->ioflags &
802 KAIOCB_IO_FOFFSET) != 0 ? 0 : 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 & ~LIO_FOFFSET) {
1563 opcode = job->uaiocb.aio_lio_opcode & ~LIO_FOFFSET;
1564 if ((job->uaiocb.aio_lio_opcode & LIO_FOFFSET) != 0)
1565 job->ioflags |= KAIOCB_IO_FOFFSET;
1572 opcode = job->uaiocb.aio_lio_opcode = type;
1574 ksiginfo_init(&job->ksi);
1576 /* Save userspace address of the job info. */
1580 * Validate the opcode and fetch the file object for the specified
1583 * XXXRW: Moved the opcode validation up here so that we don't
1584 * retrieve a file descriptor without knowing what the capabiltity
1587 fd = job->uaiocb.aio_fildes;
1591 error = fget_write(td, fd, &cap_pwrite_rights, &fp);
1595 error = fget_read(td, fd, &cap_pread_rights, &fp);
1599 error = fget(td, fd, &cap_fsync_rights, &fp);
1604 error = fget(td, fd, &cap_no_rights, &fp);
1612 if ((opcode & LIO_SYNC) && fp->f_vnode == NULL) {
1617 if ((opcode == LIO_READ || opcode == LIO_READV ||
1618 opcode == LIO_WRITE || opcode == LIO_WRITEV) &&
1619 job->uaiocb.aio_offset < 0 &&
1620 (fp->f_vnode == NULL || fp->f_vnode->v_type != VCHR)) {
1625 if (fp != NULL && fp->f_ops == &path_fileops) {
1632 mtx_lock(&aio_job_mtx);
1634 job->seqno = jobseqno++;
1635 mtx_unlock(&aio_job_mtx);
1636 error = ops->store_kernelinfo(ujob, jid);
1641 job->uaiocb._aiocb_private.kernelinfo = (void *)(intptr_t)jid;
1643 if (opcode == LIO_NOP) {
1645 MPASS(job->uiop == &job->uio || job->uiop == NULL);
1646 uma_zfree(aiocb_zone, job);
1650 if (job->uaiocb.aio_sigevent.sigev_notify != SIGEV_KEVENT)
1652 evflags = job->uaiocb.aio_sigevent.sigev_notify_kevent_flags;
1653 if ((evflags & ~(EV_CLEAR | EV_DISPATCH | EV_ONESHOT)) != 0) {
1657 kqfd = job->uaiocb.aio_sigevent.sigev_notify_kqueue;
1658 memset(&kev, 0, sizeof(kev));
1659 kev.ident = (uintptr_t)job->ujob;
1660 kev.filter = EVFILT_AIO;
1661 kev.flags = EV_ADD | EV_ENABLE | EV_FLAG1 | evflags;
1662 kev.data = (intptr_t)job;
1663 kev.udata = job->uaiocb.aio_sigevent.sigev_value.sival_ptr;
1664 error = kqfd_register(kqfd, &kev, td, M_WAITOK);
1670 ops->store_error(ujob, EINPROGRESS);
1671 job->uaiocb._aiocb_private.error = EINPROGRESS;
1673 job->cred = crhold(td->td_ucred);
1674 job->jobflags = KAIOCB_QUEUEING;
1677 if (opcode & LIO_VECTORED) {
1678 /* Use the uio copied in by aio_copyin */
1679 MPASS(job->uiop != &job->uio && job->uiop != NULL);
1681 /* Setup the inline uio */
1682 job->iov[0].iov_base = (void *)(uintptr_t)job->uaiocb.aio_buf;
1683 job->iov[0].iov_len = job->uaiocb.aio_nbytes;
1684 job->uio.uio_iov = job->iov;
1685 job->uio.uio_iovcnt = 1;
1686 job->uio.uio_resid = job->uaiocb.aio_nbytes;
1687 job->uio.uio_segflg = UIO_USERSPACE;
1688 job->uiop = &job->uio;
1690 switch (opcode & (LIO_READ | LIO_WRITE)) {
1692 job->uiop->uio_rw = UIO_READ;
1695 job->uiop->uio_rw = UIO_WRITE;
1698 job->uiop->uio_offset = job->uaiocb.aio_offset;
1699 job->uiop->uio_td = td;
1701 if (opcode == LIO_MLOCK) {
1702 aio_schedule(job, aio_process_mlock);
1704 } else if (fp->f_ops->fo_aio_queue == NULL)
1705 error = aio_queue_file(fp, job);
1707 error = fo_aio_queue(fp, job);
1712 job->jobflags &= ~KAIOCB_QUEUEING;
1713 TAILQ_INSERT_TAIL(&ki->kaio_all, job, allist);
1717 atomic_add_int(&num_queue_count, 1);
1718 if (job->jobflags & KAIOCB_FINISHED) {
1720 * The queue callback completed the request synchronously.
1721 * The bulk of the completion is deferred in that case
1724 aio_bio_done_notify(p, job);
1726 TAILQ_INSERT_TAIL(&ki->kaio_jobqueue, job, plist);
1735 knlist_delete(&job->klist, curthread, 0);
1737 if (job->uiop != &job->uio)
1739 uma_zfree(aiocb_zone, job);
1741 ops->store_error(ujob, error);
1746 aio_cancel_daemon_job(struct kaiocb *job)
1749 mtx_lock(&aio_job_mtx);
1750 if (!aio_cancel_cleared(job))
1751 TAILQ_REMOVE(&aio_jobs, job, list);
1752 mtx_unlock(&aio_job_mtx);
1757 aio_schedule(struct kaiocb *job, aio_handle_fn_t *func)
1760 mtx_lock(&aio_job_mtx);
1761 if (!aio_set_cancel_function(job, aio_cancel_daemon_job)) {
1762 mtx_unlock(&aio_job_mtx);
1766 job->handle_fn = func;
1767 TAILQ_INSERT_TAIL(&aio_jobs, job, list);
1768 aio_kick_nowait(job->userproc);
1769 mtx_unlock(&aio_job_mtx);
1773 aio_cancel_sync(struct kaiocb *job)
1775 struct kaioinfo *ki;
1777 ki = job->userproc->p_aioinfo;
1779 if (!aio_cancel_cleared(job))
1780 TAILQ_REMOVE(&ki->kaio_syncqueue, job, list);
1786 aio_queue_file(struct file *fp, struct kaiocb *job)
1788 struct kaioinfo *ki;
1789 struct kaiocb *job2;
1795 ki = job->userproc->p_aioinfo;
1796 error = aio_qbio(job->userproc, job);
1800 if (fp->f_type == DTYPE_VNODE) {
1802 if (vp->v_type == VREG || vp->v_type == VDIR) {
1803 mp = fp->f_vnode->v_mount;
1804 if (mp == NULL || (mp->mnt_flag & MNT_LOCAL) != 0)
1808 if (!(safe || enable_aio_unsafe)) {
1809 counted_warning(&unsafe_warningcnt,
1810 "is attempting to use unsafe AIO requests");
1811 return (EOPNOTSUPP);
1814 if (job->uaiocb.aio_lio_opcode & (LIO_WRITE | LIO_READ)) {
1815 aio_schedule(job, aio_process_rw);
1817 } else if (job->uaiocb.aio_lio_opcode & LIO_SYNC) {
1819 TAILQ_FOREACH(job2, &ki->kaio_jobqueue, plist) {
1820 if (job2->fd_file == job->fd_file &&
1821 ((job2->uaiocb.aio_lio_opcode & LIO_SYNC) == 0) &&
1822 job2->seqno < job->seqno) {
1823 job2->jobflags |= KAIOCB_CHECKSYNC;
1827 if (job->pending != 0) {
1828 if (!aio_set_cancel_function_locked(job,
1834 TAILQ_INSERT_TAIL(&ki->kaio_syncqueue, job, list);
1839 aio_schedule(job, aio_process_sync);
1848 aio_kick_nowait(struct proc *userp)
1850 struct kaioinfo *ki = userp->p_aioinfo;
1851 struct aioproc *aiop;
1853 mtx_assert(&aio_job_mtx, MA_OWNED);
1854 if ((aiop = TAILQ_FIRST(&aio_freeproc)) != NULL) {
1855 TAILQ_REMOVE(&aio_freeproc, aiop, list);
1856 aiop->aioprocflags &= ~AIOP_FREE;
1857 wakeup(aiop->aioproc);
1858 } else if (num_aio_resv_start + num_aio_procs < max_aio_procs &&
1859 ki->kaio_active_count + num_aio_resv_start < max_aio_per_proc) {
1860 taskqueue_enqueue(taskqueue_aiod_kick, &ki->kaio_task);
1865 aio_kick(struct proc *userp)
1867 struct kaioinfo *ki = userp->p_aioinfo;
1868 struct aioproc *aiop;
1871 mtx_assert(&aio_job_mtx, MA_OWNED);
1873 if ((aiop = TAILQ_FIRST(&aio_freeproc)) != NULL) {
1874 TAILQ_REMOVE(&aio_freeproc, aiop, list);
1875 aiop->aioprocflags &= ~AIOP_FREE;
1876 wakeup(aiop->aioproc);
1877 } else if (num_aio_resv_start + num_aio_procs < max_aio_procs &&
1878 ki->kaio_active_count + num_aio_resv_start < max_aio_per_proc) {
1879 num_aio_resv_start++;
1880 mtx_unlock(&aio_job_mtx);
1881 error = aio_newproc(&num_aio_resv_start);
1882 mtx_lock(&aio_job_mtx);
1884 num_aio_resv_start--;
1894 aio_kick_helper(void *context, int pending)
1896 struct proc *userp = context;
1898 mtx_lock(&aio_job_mtx);
1899 while (--pending >= 0) {
1900 if (aio_kick(userp))
1903 mtx_unlock(&aio_job_mtx);
1907 * Support the aio_return system call, as a side-effect, kernel resources are
1911 kern_aio_return(struct thread *td, struct aiocb *ujob, struct aiocb_ops *ops)
1913 struct proc *p = td->td_proc;
1915 struct kaioinfo *ki;
1922 TAILQ_FOREACH(job, &ki->kaio_done, plist) {
1923 if (job->ujob == ujob)
1927 MPASS(job->jobflags & KAIOCB_FINISHED);
1928 status = job->uaiocb._aiocb_private.status;
1929 error = job->uaiocb._aiocb_private.error;
1930 td->td_retval[0] = status;
1931 td->td_ru.ru_oublock += job->outblock;
1932 td->td_ru.ru_inblock += job->inblock;
1933 td->td_ru.ru_msgsnd += job->msgsnd;
1934 td->td_ru.ru_msgrcv += job->msgrcv;
1935 aio_free_entry(job);
1937 ops->store_error(ujob, error);
1938 ops->store_status(ujob, status);
1947 sys_aio_return(struct thread *td, struct aio_return_args *uap)
1950 return (kern_aio_return(td, uap->aiocbp, &aiocb_ops));
1954 * Allow a process to wakeup when any of the I/O requests are completed.
1957 kern_aio_suspend(struct thread *td, int njoblist, struct aiocb **ujoblist,
1958 struct timespec *ts)
1960 struct proc *p = td->td_proc;
1962 struct kaioinfo *ki;
1963 struct kaiocb *firstjob, *job;
1968 if (ts->tv_nsec < 0 || ts->tv_nsec >= 1000000000)
1971 TIMESPEC_TO_TIMEVAL(&atv, ts);
1972 if (itimerfix(&atv))
1974 timo = tvtohz(&atv);
1988 TAILQ_FOREACH(job, &ki->kaio_all, allist) {
1989 for (i = 0; i < njoblist; i++) {
1990 if (job->ujob == ujoblist[i]) {
1991 if (firstjob == NULL)
1993 if (job->jobflags & KAIOCB_FINISHED)
1998 /* All tasks were finished. */
1999 if (firstjob == NULL)
2002 ki->kaio_flags |= KAIO_WAKEUP;
2003 error = msleep(&p->p_aioinfo, AIO_MTX(ki), PRIBIO | PCATCH,
2005 if (error == ERESTART)
2016 sys_aio_suspend(struct thread *td, struct aio_suspend_args *uap)
2018 struct timespec ts, *tsp;
2019 struct aiocb **ujoblist;
2022 if (uap->nent < 0 || uap->nent > max_aio_queue_per_proc)
2026 /* Get timespec struct. */
2027 if ((error = copyin(uap->timeout, &ts, sizeof(ts))) != 0)
2033 ujoblist = malloc(uap->nent * sizeof(ujoblist[0]), M_AIO, M_WAITOK);
2034 error = copyin(uap->aiocbp, ujoblist, uap->nent * sizeof(ujoblist[0]));
2036 error = kern_aio_suspend(td, uap->nent, ujoblist, tsp);
2037 free(ujoblist, M_AIO);
2042 * aio_cancel cancels any non-bio aio operations not currently in progress.
2045 sys_aio_cancel(struct thread *td, struct aio_cancel_args *uap)
2047 struct proc *p = td->td_proc;
2048 struct kaioinfo *ki;
2049 struct kaiocb *job, *jobn;
2053 int notcancelled = 0;
2056 /* Lookup file object. */
2057 error = fget(td, uap->fd, &cap_no_rights, &fp);
2065 if (fp->f_type == DTYPE_VNODE) {
2067 if (vn_isdisk(vp)) {
2069 td->td_retval[0] = AIO_NOTCANCELED;
2075 TAILQ_FOREACH_SAFE(job, &ki->kaio_jobqueue, plist, jobn) {
2076 if ((uap->fd == job->uaiocb.aio_fildes) &&
2077 ((uap->aiocbp == NULL) ||
2078 (uap->aiocbp == job->ujob))) {
2079 if (aio_cancel_job(p, ki, job)) {
2084 if (uap->aiocbp != NULL)
2093 if (uap->aiocbp != NULL) {
2095 td->td_retval[0] = AIO_CANCELED;
2101 td->td_retval[0] = AIO_NOTCANCELED;
2106 td->td_retval[0] = AIO_CANCELED;
2110 td->td_retval[0] = AIO_ALLDONE;
2116 * aio_error is implemented in the kernel level for compatibility purposes
2117 * only. For a user mode async implementation, it would be best to do it in
2118 * a userland subroutine.
2121 kern_aio_error(struct thread *td, struct aiocb *ujob, struct aiocb_ops *ops)
2123 struct proc *p = td->td_proc;
2125 struct kaioinfo *ki;
2130 td->td_retval[0] = EINVAL;
2135 TAILQ_FOREACH(job, &ki->kaio_all, allist) {
2136 if (job->ujob == ujob) {
2137 if (job->jobflags & KAIOCB_FINISHED)
2139 job->uaiocb._aiocb_private.error;
2141 td->td_retval[0] = EINPROGRESS;
2149 * Hack for failure of aio_aqueue.
2151 status = ops->fetch_status(ujob);
2153 td->td_retval[0] = ops->fetch_error(ujob);
2157 td->td_retval[0] = EINVAL;
2162 sys_aio_error(struct thread *td, struct aio_error_args *uap)
2165 return (kern_aio_error(td, uap->aiocbp, &aiocb_ops));
2168 /* syscall - asynchronous read from a file (REALTIME) */
2169 #ifdef COMPAT_FREEBSD6
2171 freebsd6_aio_read(struct thread *td, struct freebsd6_aio_read_args *uap)
2174 return (aio_aqueue(td, (struct aiocb *)uap->aiocbp, NULL, LIO_READ,
2175 &aiocb_ops_osigevent));
2180 sys_aio_read(struct thread *td, struct aio_read_args *uap)
2183 return (aio_aqueue(td, uap->aiocbp, NULL, LIO_READ, &aiocb_ops));
2187 sys_aio_readv(struct thread *td, struct aio_readv_args *uap)
2190 return (aio_aqueue(td, uap->aiocbp, NULL, LIO_READV, &aiocb_ops));
2193 /* syscall - asynchronous write to a file (REALTIME) */
2194 #ifdef COMPAT_FREEBSD6
2196 freebsd6_aio_write(struct thread *td, struct freebsd6_aio_write_args *uap)
2199 return (aio_aqueue(td, (struct aiocb *)uap->aiocbp, NULL, LIO_WRITE,
2200 &aiocb_ops_osigevent));
2205 sys_aio_write(struct thread *td, struct aio_write_args *uap)
2208 return (aio_aqueue(td, uap->aiocbp, NULL, LIO_WRITE, &aiocb_ops));
2212 sys_aio_writev(struct thread *td, struct aio_writev_args *uap)
2215 return (aio_aqueue(td, uap->aiocbp, NULL, LIO_WRITEV, &aiocb_ops));
2219 sys_aio_mlock(struct thread *td, struct aio_mlock_args *uap)
2222 return (aio_aqueue(td, uap->aiocbp, NULL, LIO_MLOCK, &aiocb_ops));
2226 kern_lio_listio(struct thread *td, int mode, struct aiocb * const *uacb_list,
2227 struct aiocb **acb_list, int nent, struct sigevent *sig,
2228 struct aiocb_ops *ops)
2230 struct proc *p = td->td_proc;
2232 struct kaioinfo *ki;
2233 struct aioliojob *lj;
2239 if ((mode != LIO_NOWAIT) && (mode != LIO_WAIT))
2242 if (nent < 0 || nent > max_aio_queue_per_proc)
2245 if (p->p_aioinfo == NULL)
2246 aio_init_aioinfo(p);
2250 lj = uma_zalloc(aiolio_zone, M_WAITOK);
2253 lj->lioj_finished_count = 0;
2254 lj->lioj_signal.sigev_notify = SIGEV_NONE;
2255 knlist_init_mtx(&lj->klist, AIO_MTX(ki));
2256 ksiginfo_init(&lj->lioj_ksi);
2261 if (sig && (mode == LIO_NOWAIT)) {
2262 bcopy(sig, &lj->lioj_signal, sizeof(lj->lioj_signal));
2263 if (lj->lioj_signal.sigev_notify == SIGEV_KEVENT) {
2264 /* Assume only new style KEVENT */
2265 memset(&kev, 0, sizeof(kev));
2266 kev.filter = EVFILT_LIO;
2267 kev.flags = EV_ADD | EV_ENABLE | EV_FLAG1;
2268 kev.ident = (uintptr_t)uacb_list; /* something unique */
2269 kev.data = (intptr_t)lj;
2270 /* pass user defined sigval data */
2271 kev.udata = lj->lioj_signal.sigev_value.sival_ptr;
2272 error = kqfd_register(
2273 lj->lioj_signal.sigev_notify_kqueue, &kev, td,
2276 uma_zfree(aiolio_zone, lj);
2279 } else if (lj->lioj_signal.sigev_notify == SIGEV_NONE) {
2281 } else if (lj->lioj_signal.sigev_notify == SIGEV_SIGNAL ||
2282 lj->lioj_signal.sigev_notify == SIGEV_THREAD_ID) {
2283 if (!_SIG_VALID(lj->lioj_signal.sigev_signo)) {
2284 uma_zfree(aiolio_zone, lj);
2287 lj->lioj_flags |= LIOJ_SIGNAL;
2289 uma_zfree(aiolio_zone, lj);
2295 TAILQ_INSERT_TAIL(&ki->kaio_liojoblist, lj, lioj_list);
2297 * Add extra aiocb count to avoid the lio to be freed
2298 * by other threads doing aio_waitcomplete or aio_return,
2299 * and prevent event from being sent until we have queued
2306 * Get pointers to the list of I/O requests.
2310 for (i = 0; i < nent; i++) {
2313 error = aio_aqueue(td, job, lj, LIO_NOP, ops);
2314 if (error == EAGAIN)
2316 else if (error != 0)
2323 if (mode == LIO_WAIT) {
2324 while (lj->lioj_count - 1 != lj->lioj_finished_count) {
2325 ki->kaio_flags |= KAIO_WAKEUP;
2326 error = msleep(&p->p_aioinfo, AIO_MTX(ki),
2327 PRIBIO | PCATCH, "aiospn", 0);
2328 if (error == ERESTART)
2334 if (lj->lioj_count - 1 == lj->lioj_finished_count) {
2335 if (lj->lioj_signal.sigev_notify == SIGEV_KEVENT) {
2336 lj->lioj_flags |= LIOJ_KEVENT_POSTED;
2337 KNOTE_LOCKED(&lj->klist, 1);
2339 if ((lj->lioj_flags & (LIOJ_SIGNAL |
2340 LIOJ_SIGNAL_POSTED)) == LIOJ_SIGNAL &&
2341 (lj->lioj_signal.sigev_notify == SIGEV_SIGNAL ||
2342 lj->lioj_signal.sigev_notify == SIGEV_THREAD_ID)) {
2343 aio_sendsig(p, &lj->lioj_signal, &lj->lioj_ksi,
2344 lj->lioj_count != 1);
2345 lj->lioj_flags |= LIOJ_SIGNAL_POSTED;
2350 if (lj->lioj_count == 0) {
2351 TAILQ_REMOVE(&ki->kaio_liojoblist, lj, lioj_list);
2352 knlist_delete(&lj->klist, curthread, 1);
2354 sigqueue_take(&lj->lioj_ksi);
2357 uma_zfree(aiolio_zone, lj);
2369 /* syscall - list directed I/O (REALTIME) */
2370 #ifdef COMPAT_FREEBSD6
2372 freebsd6_lio_listio(struct thread *td, struct freebsd6_lio_listio_args *uap)
2374 struct aiocb **acb_list;
2375 struct sigevent *sigp, sig;
2376 struct osigevent osig;
2379 if ((uap->mode != LIO_NOWAIT) && (uap->mode != LIO_WAIT))
2383 if (nent < 0 || nent > max_aio_queue_per_proc)
2386 if (uap->sig && (uap->mode == LIO_NOWAIT)) {
2387 error = copyin(uap->sig, &osig, sizeof(osig));
2390 error = convert_old_sigevent(&osig, &sig);
2397 acb_list = malloc(sizeof(struct aiocb *) * nent, M_LIO, M_WAITOK);
2398 error = copyin(uap->acb_list, acb_list, nent * sizeof(acb_list[0]));
2400 error = kern_lio_listio(td, uap->mode,
2401 (struct aiocb * const *)uap->acb_list, acb_list, nent, sigp,
2402 &aiocb_ops_osigevent);
2403 free(acb_list, M_LIO);
2408 /* syscall - list directed I/O (REALTIME) */
2410 sys_lio_listio(struct thread *td, struct lio_listio_args *uap)
2412 struct aiocb **acb_list;
2413 struct sigevent *sigp, sig;
2416 if ((uap->mode != LIO_NOWAIT) && (uap->mode != LIO_WAIT))
2420 if (nent < 0 || nent > max_aio_queue_per_proc)
2423 if (uap->sig && (uap->mode == LIO_NOWAIT)) {
2424 error = copyin(uap->sig, &sig, sizeof(sig));
2431 acb_list = malloc(sizeof(struct aiocb *) * nent, M_LIO, M_WAITOK);
2432 error = copyin(uap->acb_list, acb_list, nent * sizeof(acb_list[0]));
2434 error = kern_lio_listio(td, uap->mode, uap->acb_list, acb_list,
2435 nent, sigp, &aiocb_ops);
2436 free(acb_list, M_LIO);
2441 aio_biocleanup(struct bio *bp)
2443 struct kaiocb *job = (struct kaiocb *)bp->bio_caller1;
2444 struct kaioinfo *ki;
2445 struct buf *pbuf = (struct buf *)bp->bio_caller2;
2447 /* Release mapping into kernel space. */
2449 MPASS(pbuf->b_npages <= atop(maxphys) + 1);
2450 pmap_qremove((vm_offset_t)pbuf->b_data, pbuf->b_npages);
2451 vm_page_unhold_pages(pbuf->b_pages, pbuf->b_npages);
2452 uma_zfree(pbuf_zone, pbuf);
2453 atomic_subtract_int(&num_buf_aio, 1);
2454 ki = job->userproc->p_aioinfo;
2456 ki->kaio_buffer_count--;
2459 MPASS(bp->bio_ma_n <= atop(maxphys) + 1);
2460 vm_page_unhold_pages(bp->bio_ma, bp->bio_ma_n);
2461 free(bp->bio_ma, M_TEMP);
2462 atomic_subtract_int(&num_unmapped_aio, 1);
2468 aio_biowakeup(struct bio *bp)
2470 struct kaiocb *job = (struct kaiocb *)bp->bio_caller1;
2472 long bcount = bp->bio_bcount;
2473 long resid = bp->bio_resid;
2475 int bio_error = bp->bio_error;
2476 uint16_t flags = bp->bio_flags;
2478 opcode = job->uaiocb.aio_lio_opcode;
2482 nbytes = bcount - resid;
2483 atomic_add_acq_long(&job->nbytes, nbytes);
2484 nblks = btodb(nbytes);
2487 * If multiple bios experienced an error, the job will reflect the
2488 * error of whichever failed bio completed last.
2490 if (flags & BIO_ERROR)
2491 atomic_store_int(&job->error, bio_error);
2492 if (opcode & LIO_WRITE)
2493 atomic_add_int(&job->outblock, nblks);
2495 atomic_add_int(&job->inblock, nblks);
2497 if (refcount_release(&job->nbio)) {
2498 bio_error = atomic_load_int(&job->error);
2500 aio_complete(job, -1, bio_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_NOP)
2831 type = kcb->aio_lio_opcode;
2832 if (type & LIO_VECTORED) {
2833 iov32 = PTRIN(job32.aio_iov);
2834 CP(job32, *kcb, aio_iovcnt);
2835 /* malloc a uio and copy in the iovec */
2836 error = freebsd32_copyinuio(iov32,
2837 kcb->aio_iovcnt, &kjob->uiop);
2841 PTRIN_CP(job32, *kcb, aio_buf);
2842 CP(job32, *kcb, aio_nbytes);
2844 CP(job32, *kcb, aio_reqprio);
2845 CP(job32, *kcb, _aiocb_private.status);
2846 CP(job32, *kcb, _aiocb_private.error);
2847 PTRIN_CP(job32, *kcb, _aiocb_private.kernelinfo);
2848 error = convert_sigevent32(&job32.aio_sigevent, &kcb->aio_sigevent);
2854 aiocb32_fetch_status(struct aiocb *ujob)
2856 struct aiocb32 *ujob32;
2858 ujob32 = (struct aiocb32 *)ujob;
2859 return (fuword32(&ujob32->_aiocb_private.status));
2863 aiocb32_fetch_error(struct aiocb *ujob)
2865 struct aiocb32 *ujob32;
2867 ujob32 = (struct aiocb32 *)ujob;
2868 return (fuword32(&ujob32->_aiocb_private.error));
2872 aiocb32_store_status(struct aiocb *ujob, long status)
2874 struct aiocb32 *ujob32;
2876 ujob32 = (struct aiocb32 *)ujob;
2877 return (suword32(&ujob32->_aiocb_private.status, status));
2881 aiocb32_store_error(struct aiocb *ujob, long error)
2883 struct aiocb32 *ujob32;
2885 ujob32 = (struct aiocb32 *)ujob;
2886 return (suword32(&ujob32->_aiocb_private.error, error));
2890 aiocb32_store_kernelinfo(struct aiocb *ujob, long jobref)
2892 struct aiocb32 *ujob32;
2894 ujob32 = (struct aiocb32 *)ujob;
2895 return (suword32(&ujob32->_aiocb_private.kernelinfo, jobref));
2899 aiocb32_store_aiocb(struct aiocb **ujobp, struct aiocb *ujob)
2902 return (suword32(ujobp, (long)ujob));
2905 static struct aiocb_ops aiocb32_ops = {
2906 .aio_copyin = aiocb32_copyin,
2907 .fetch_status = aiocb32_fetch_status,
2908 .fetch_error = aiocb32_fetch_error,
2909 .store_status = aiocb32_store_status,
2910 .store_error = aiocb32_store_error,
2911 .store_kernelinfo = aiocb32_store_kernelinfo,
2912 .store_aiocb = aiocb32_store_aiocb,
2915 #ifdef COMPAT_FREEBSD6
2916 static struct aiocb_ops aiocb32_ops_osigevent = {
2917 .aio_copyin = aiocb32_copyin_old_sigevent,
2918 .fetch_status = aiocb32_fetch_status,
2919 .fetch_error = aiocb32_fetch_error,
2920 .store_status = aiocb32_store_status,
2921 .store_error = aiocb32_store_error,
2922 .store_kernelinfo = aiocb32_store_kernelinfo,
2923 .store_aiocb = aiocb32_store_aiocb,
2928 freebsd32_aio_return(struct thread *td, struct freebsd32_aio_return_args *uap)
2931 return (kern_aio_return(td, (struct aiocb *)uap->aiocbp, &aiocb32_ops));
2935 freebsd32_aio_suspend(struct thread *td, struct freebsd32_aio_suspend_args *uap)
2937 struct timespec32 ts32;
2938 struct timespec ts, *tsp;
2939 struct aiocb **ujoblist;
2940 uint32_t *ujoblist32;
2943 if (uap->nent < 0 || uap->nent > max_aio_queue_per_proc)
2947 /* Get timespec struct. */
2948 if ((error = copyin(uap->timeout, &ts32, sizeof(ts32))) != 0)
2950 CP(ts32, ts, tv_sec);
2951 CP(ts32, ts, tv_nsec);
2956 ujoblist = malloc(uap->nent * sizeof(ujoblist[0]), M_AIO, M_WAITOK);
2957 ujoblist32 = (uint32_t *)ujoblist;
2958 error = copyin(uap->aiocbp, ujoblist32, uap->nent *
2959 sizeof(ujoblist32[0]));
2961 for (i = uap->nent - 1; i >= 0; i--)
2962 ujoblist[i] = PTRIN(ujoblist32[i]);
2964 error = kern_aio_suspend(td, uap->nent, ujoblist, tsp);
2966 free(ujoblist, M_AIO);
2971 freebsd32_aio_error(struct thread *td, struct freebsd32_aio_error_args *uap)
2974 return (kern_aio_error(td, (struct aiocb *)uap->aiocbp, &aiocb32_ops));
2977 #ifdef COMPAT_FREEBSD6
2979 freebsd6_freebsd32_aio_read(struct thread *td,
2980 struct freebsd6_freebsd32_aio_read_args *uap)
2983 return (aio_aqueue(td, (struct aiocb *)uap->aiocbp, NULL, LIO_READ,
2984 &aiocb32_ops_osigevent));
2989 freebsd32_aio_read(struct thread *td, struct freebsd32_aio_read_args *uap)
2992 return (aio_aqueue(td, (struct aiocb *)uap->aiocbp, NULL, LIO_READ,
2997 freebsd32_aio_readv(struct thread *td, struct freebsd32_aio_readv_args *uap)
3000 return (aio_aqueue(td, (struct aiocb *)uap->aiocbp, NULL, LIO_READV,
3004 #ifdef COMPAT_FREEBSD6
3006 freebsd6_freebsd32_aio_write(struct thread *td,
3007 struct freebsd6_freebsd32_aio_write_args *uap)
3010 return (aio_aqueue(td, (struct aiocb *)uap->aiocbp, NULL, LIO_WRITE,
3011 &aiocb32_ops_osigevent));
3016 freebsd32_aio_write(struct thread *td, struct freebsd32_aio_write_args *uap)
3019 return (aio_aqueue(td, (struct aiocb *)uap->aiocbp, NULL, LIO_WRITE,
3024 freebsd32_aio_writev(struct thread *td, struct freebsd32_aio_writev_args *uap)
3027 return (aio_aqueue(td, (struct aiocb *)uap->aiocbp, NULL, LIO_WRITEV,
3032 freebsd32_aio_mlock(struct thread *td, struct freebsd32_aio_mlock_args *uap)
3035 return (aio_aqueue(td, (struct aiocb *)uap->aiocbp, NULL, LIO_MLOCK,
3040 freebsd32_aio_waitcomplete(struct thread *td,
3041 struct freebsd32_aio_waitcomplete_args *uap)
3043 struct timespec32 ts32;
3044 struct timespec ts, *tsp;
3048 /* Get timespec struct. */
3049 error = copyin(uap->timeout, &ts32, sizeof(ts32));
3052 CP(ts32, ts, tv_sec);
3053 CP(ts32, ts, tv_nsec);
3058 return (kern_aio_waitcomplete(td, (struct aiocb **)uap->aiocbp, tsp,
3063 freebsd32_aio_fsync(struct thread *td, struct freebsd32_aio_fsync_args *uap)
3066 return (kern_aio_fsync(td, uap->op, (struct aiocb *)uap->aiocbp,
3070 #ifdef COMPAT_FREEBSD6
3072 freebsd6_freebsd32_lio_listio(struct thread *td,
3073 struct freebsd6_freebsd32_lio_listio_args *uap)
3075 struct aiocb **acb_list;
3076 struct sigevent *sigp, sig;
3077 struct osigevent32 osig;
3078 uint32_t *acb_list32;
3081 if ((uap->mode != LIO_NOWAIT) && (uap->mode != LIO_WAIT))
3085 if (nent < 0 || nent > max_aio_queue_per_proc)
3088 if (uap->sig && (uap->mode == LIO_NOWAIT)) {
3089 error = copyin(uap->sig, &osig, sizeof(osig));
3092 error = convert_old_sigevent32(&osig, &sig);
3099 acb_list32 = malloc(sizeof(uint32_t) * nent, M_LIO, M_WAITOK);
3100 error = copyin(uap->acb_list, acb_list32, nent * sizeof(uint32_t));
3102 free(acb_list32, M_LIO);
3105 acb_list = malloc(sizeof(struct aiocb *) * nent, M_LIO, M_WAITOK);
3106 for (i = 0; i < nent; i++)
3107 acb_list[i] = PTRIN(acb_list32[i]);
3108 free(acb_list32, M_LIO);
3110 error = kern_lio_listio(td, uap->mode,
3111 (struct aiocb * const *)uap->acb_list, acb_list, nent, sigp,
3112 &aiocb32_ops_osigevent);
3113 free(acb_list, M_LIO);
3119 freebsd32_lio_listio(struct thread *td, struct freebsd32_lio_listio_args *uap)
3121 struct aiocb **acb_list;
3122 struct sigevent *sigp, sig;
3123 struct sigevent32 sig32;
3124 uint32_t *acb_list32;
3127 if ((uap->mode != LIO_NOWAIT) && (uap->mode != LIO_WAIT))
3131 if (nent < 0 || nent > max_aio_queue_per_proc)
3134 if (uap->sig && (uap->mode == LIO_NOWAIT)) {
3135 error = copyin(uap->sig, &sig32, sizeof(sig32));
3138 error = convert_sigevent32(&sig32, &sig);
3145 acb_list32 = malloc(sizeof(uint32_t) * nent, M_LIO, M_WAITOK);
3146 error = copyin(uap->acb_list, acb_list32, nent * sizeof(uint32_t));
3148 free(acb_list32, M_LIO);
3151 acb_list = malloc(sizeof(struct aiocb *) * nent, M_LIO, M_WAITOK);
3152 for (i = 0; i < nent; i++)
3153 acb_list[i] = PTRIN(acb_list32[i]);
3154 free(acb_list32, M_LIO);
3156 error = kern_lio_listio(td, uap->mode,
3157 (struct aiocb * const *)uap->acb_list, acb_list, nent, sigp,
3159 free(acb_list, M_LIO);