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/sysent.h>
56 #include <sys/sysctl.h>
57 #include <sys/syslog.h>
59 #include <sys/taskqueue.h>
60 #include <sys/vnode.h>
62 #include <sys/event.h>
63 #include <sys/mount.h>
64 #include <geom/geom.h>
66 #include <machine/atomic.h>
69 #include <vm/vm_page.h>
70 #include <vm/vm_extern.h>
72 #include <vm/vm_map.h>
73 #include <vm/vm_object.h>
78 * Counter for allocating reference ids to new jobs. Wrapped to 1 on
79 * overflow. (XXX will be removed soon.)
81 static u_long jobrefid;
84 * Counter for aio_fsync.
86 static uint64_t jobseqno;
88 #ifndef MAX_AIO_PER_PROC
89 #define MAX_AIO_PER_PROC 32
92 #ifndef MAX_AIO_QUEUE_PER_PROC
93 #define MAX_AIO_QUEUE_PER_PROC 256
97 #define MAX_AIO_QUEUE 1024 /* Bigger than MAX_AIO_QUEUE_PER_PROC */
101 #define MAX_BUF_AIO 16
104 FEATURE(aio, "Asynchronous I/O");
105 SYSCTL_DECL(_p1003_1b);
107 static MALLOC_DEFINE(M_LIO, "lio", "listio aio control block list");
108 static MALLOC_DEFINE(M_AIOS, "aios", "aio_suspend aio control block list");
110 static SYSCTL_NODE(_vfs, OID_AUTO, aio, CTLFLAG_RW | CTLFLAG_MPSAFE, 0,
111 "Async IO management");
113 static int enable_aio_unsafe = 0;
114 SYSCTL_INT(_vfs_aio, OID_AUTO, enable_unsafe, CTLFLAG_RW, &enable_aio_unsafe, 0,
115 "Permit asynchronous IO on all file types, not just known-safe types");
117 static unsigned int unsafe_warningcnt = 1;
118 SYSCTL_UINT(_vfs_aio, OID_AUTO, unsafe_warningcnt, CTLFLAG_RW,
119 &unsafe_warningcnt, 0,
120 "Warnings that will be triggered upon failed IO requests on unsafe files");
122 static int max_aio_procs = MAX_AIO_PROCS;
123 SYSCTL_INT(_vfs_aio, OID_AUTO, max_aio_procs, CTLFLAG_RW, &max_aio_procs, 0,
124 "Maximum number of kernel processes to use for handling async IO ");
126 static int num_aio_procs = 0;
127 SYSCTL_INT(_vfs_aio, OID_AUTO, num_aio_procs, CTLFLAG_RD, &num_aio_procs, 0,
128 "Number of presently active kernel processes for async IO");
131 * The code will adjust the actual number of AIO processes towards this
132 * number when it gets a chance.
134 static int target_aio_procs = TARGET_AIO_PROCS;
135 SYSCTL_INT(_vfs_aio, OID_AUTO, target_aio_procs, CTLFLAG_RW, &target_aio_procs,
137 "Preferred number of ready kernel processes for async IO");
139 static int max_queue_count = MAX_AIO_QUEUE;
140 SYSCTL_INT(_vfs_aio, OID_AUTO, max_aio_queue, CTLFLAG_RW, &max_queue_count, 0,
141 "Maximum number of aio requests to queue, globally");
143 static int num_queue_count = 0;
144 SYSCTL_INT(_vfs_aio, OID_AUTO, num_queue_count, CTLFLAG_RD, &num_queue_count, 0,
145 "Number of queued aio requests");
147 static int num_buf_aio = 0;
148 SYSCTL_INT(_vfs_aio, OID_AUTO, num_buf_aio, CTLFLAG_RD, &num_buf_aio, 0,
149 "Number of aio requests presently handled by the buf subsystem");
151 static int num_unmapped_aio = 0;
152 SYSCTL_INT(_vfs_aio, OID_AUTO, num_unmapped_aio, CTLFLAG_RD, &num_unmapped_aio,
154 "Number of aio requests presently handled by unmapped I/O buffers");
156 /* Number of async I/O processes in the process of being started */
157 /* XXX This should be local to aio_aqueue() */
158 static int num_aio_resv_start = 0;
160 static int aiod_lifetime;
161 SYSCTL_INT(_vfs_aio, OID_AUTO, aiod_lifetime, CTLFLAG_RW, &aiod_lifetime, 0,
162 "Maximum lifetime for idle aiod");
164 static int max_aio_per_proc = MAX_AIO_PER_PROC;
165 SYSCTL_INT(_vfs_aio, OID_AUTO, max_aio_per_proc, CTLFLAG_RW, &max_aio_per_proc,
167 "Maximum active aio requests per process");
169 static int max_aio_queue_per_proc = MAX_AIO_QUEUE_PER_PROC;
170 SYSCTL_INT(_vfs_aio, OID_AUTO, max_aio_queue_per_proc, CTLFLAG_RW,
171 &max_aio_queue_per_proc, 0,
172 "Maximum queued aio requests per process");
174 static int max_buf_aio = MAX_BUF_AIO;
175 SYSCTL_INT(_vfs_aio, OID_AUTO, max_buf_aio, CTLFLAG_RW, &max_buf_aio, 0,
176 "Maximum buf aio requests per process");
179 * Though redundant with vfs.aio.max_aio_queue_per_proc, POSIX requires
180 * sysconf(3) to support AIO_LISTIO_MAX, and we implement that with
181 * vfs.aio.aio_listio_max.
183 SYSCTL_INT(_p1003_1b, CTL_P1003_1B_AIO_LISTIO_MAX, aio_listio_max,
184 CTLFLAG_RD | CTLFLAG_CAPRD, &max_aio_queue_per_proc,
185 0, "Maximum aio requests for a single lio_listio call");
187 #ifdef COMPAT_FREEBSD6
188 typedef struct oaiocb {
189 int aio_fildes; /* File descriptor */
190 off_t aio_offset; /* File offset for I/O */
191 volatile void *aio_buf; /* I/O buffer in process space */
192 size_t aio_nbytes; /* Number of bytes for I/O */
193 struct osigevent aio_sigevent; /* Signal to deliver */
194 int aio_lio_opcode; /* LIO opcode */
195 int aio_reqprio; /* Request priority -- ignored */
196 struct __aiocb_private _aiocb_private;
201 * Below is a key of locks used to protect each member of struct kaiocb
202 * aioliojob and kaioinfo and any backends.
204 * * - need not protected
205 * a - locked by kaioinfo lock
206 * b - locked by backend lock, the backend lock can be null in some cases,
207 * for example, BIO belongs to this type, in this case, proc lock is
209 * c - locked by aio_job_mtx, the lock for the generic file I/O backend.
213 * If the routine that services an AIO request blocks while running in an
214 * AIO kernel process it can starve other I/O requests. BIO requests
215 * queued via aio_qbio() complete asynchronously and do not use AIO kernel
216 * processes at all. Socket I/O requests use a separate pool of
217 * kprocs and also force non-blocking I/O. Other file I/O requests
218 * use the generic fo_read/fo_write operations which can block. The
219 * fsync and mlock operations can also block while executing. Ideally
220 * none of these requests would block while executing.
222 * Note that the service routines cannot toggle O_NONBLOCK in the file
223 * structure directly while handling a request due to races with
228 #define KAIOCB_QUEUEING 0x01
229 #define KAIOCB_CANCELLED 0x02
230 #define KAIOCB_CANCELLING 0x04
231 #define KAIOCB_CHECKSYNC 0x08
232 #define KAIOCB_CLEARED 0x10
233 #define KAIOCB_FINISHED 0x20
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 * aiop async io process data
344 * aiocb async io jobs
345 * aiolio list io jobs
347 static uma_zone_t kaio_zone, aiop_zone, aiocb_zone, aiolio_zone;
349 /* kqueue filters for aio */
350 static struct filterops aio_filtops = {
352 .f_attach = filt_aioattach,
353 .f_detach = filt_aiodetach,
356 static struct filterops lio_filtops = {
358 .f_attach = filt_lioattach,
359 .f_detach = filt_liodetach,
363 static eventhandler_tag exit_tag, exec_tag;
365 TASKQUEUE_DEFINE_THREAD(aiod_kick);
368 * Main operations function for use as a kernel module.
371 aio_modload(struct module *module, int cmd, void *arg)
388 static moduledata_t aio_mod = {
394 DECLARE_MODULE(aio, aio_mod, SI_SUB_VFS, SI_ORDER_ANY);
395 MODULE_VERSION(aio, 1);
398 * Startup initialization
404 exit_tag = EVENTHANDLER_REGISTER(process_exit, aio_proc_rundown, NULL,
405 EVENTHANDLER_PRI_ANY);
406 exec_tag = EVENTHANDLER_REGISTER(process_exec, aio_proc_rundown_exec,
407 NULL, EVENTHANDLER_PRI_ANY);
408 kqueue_add_filteropts(EVFILT_AIO, &aio_filtops);
409 kqueue_add_filteropts(EVFILT_LIO, &lio_filtops);
410 TAILQ_INIT(&aio_freeproc);
411 sema_init(&aio_newproc_sem, 0, "aio_new_proc");
412 mtx_init(&aio_job_mtx, "aio_job", NULL, MTX_DEF);
413 TAILQ_INIT(&aio_jobs);
414 aiod_unr = new_unrhdr(1, INT_MAX, NULL);
415 kaio_zone = uma_zcreate("AIO", sizeof(struct kaioinfo), NULL, NULL,
416 NULL, NULL, UMA_ALIGN_PTR, UMA_ZONE_NOFREE);
417 aiop_zone = uma_zcreate("AIOP", sizeof(struct aioproc), NULL,
418 NULL, NULL, NULL, UMA_ALIGN_PTR, UMA_ZONE_NOFREE);
419 aiocb_zone = uma_zcreate("AIOCB", sizeof(struct kaiocb), NULL, NULL,
420 NULL, NULL, UMA_ALIGN_PTR, UMA_ZONE_NOFREE);
421 aiolio_zone = uma_zcreate("AIOLIO", sizeof(struct aioliojob), NULL,
422 NULL, NULL, NULL, UMA_ALIGN_PTR, UMA_ZONE_NOFREE);
423 aiod_lifetime = AIOD_LIFETIME_DEFAULT;
425 p31b_setcfg(CTL_P1003_1B_ASYNCHRONOUS_IO, _POSIX_ASYNCHRONOUS_IO);
426 p31b_setcfg(CTL_P1003_1B_AIO_MAX, MAX_AIO_QUEUE);
427 p31b_setcfg(CTL_P1003_1B_AIO_PRIO_DELTA_MAX, 0);
433 * Init the per-process aioinfo structure. The aioinfo limits are set
434 * per-process for user limit (resource) management.
437 aio_init_aioinfo(struct proc *p)
441 ki = uma_zalloc(kaio_zone, M_WAITOK);
442 mtx_init(&ki->kaio_mtx, "aiomtx", NULL, MTX_DEF | MTX_NEW);
444 ki->kaio_active_count = 0;
446 ki->kaio_buffer_count = 0;
447 TAILQ_INIT(&ki->kaio_all);
448 TAILQ_INIT(&ki->kaio_done);
449 TAILQ_INIT(&ki->kaio_jobqueue);
450 TAILQ_INIT(&ki->kaio_liojoblist);
451 TAILQ_INIT(&ki->kaio_syncqueue);
452 TAILQ_INIT(&ki->kaio_syncready);
453 TASK_INIT(&ki->kaio_task, 0, aio_kick_helper, p);
454 TASK_INIT(&ki->kaio_sync_task, 0, aio_schedule_fsync, ki);
456 if (p->p_aioinfo == NULL) {
461 mtx_destroy(&ki->kaio_mtx);
462 uma_zfree(kaio_zone, ki);
465 while (num_aio_procs < MIN(target_aio_procs, max_aio_procs))
470 aio_sendsig(struct proc *p, struct sigevent *sigev, ksiginfo_t *ksi, bool ext)
475 error = sigev_findtd(p, sigev, &td);
479 ksiginfo_set_sigev(ksi, sigev);
480 ksi->ksi_code = SI_ASYNCIO;
481 ksi->ksi_flags |= ext ? (KSI_EXT | KSI_INS) : 0;
482 tdsendsignal(p, td, ksi->ksi_signo, ksi);
489 * Free a job entry. Wait for completion if it is currently active, but don't
490 * delay forever. If we delay, we return a flag that says that we have to
491 * restart the queue scan.
494 aio_free_entry(struct kaiocb *job)
497 struct aioliojob *lj;
505 AIO_LOCK_ASSERT(ki, MA_OWNED);
506 MPASS(job->jobflags & KAIOCB_FINISHED);
508 atomic_subtract_int(&num_queue_count, 1);
511 MPASS(ki->kaio_count >= 0);
513 TAILQ_REMOVE(&ki->kaio_done, job, plist);
514 TAILQ_REMOVE(&ki->kaio_all, job, allist);
519 lj->lioj_finished_count--;
521 if (lj->lioj_count == 0) {
522 TAILQ_REMOVE(&ki->kaio_liojoblist, lj, lioj_list);
523 /* lio is going away, we need to destroy any knotes */
524 knlist_delete(&lj->klist, curthread, 1);
526 sigqueue_take(&lj->lioj_ksi);
528 uma_zfree(aiolio_zone, lj);
532 /* job is going away, we need to destroy any knotes */
533 knlist_delete(&job->klist, curthread, 1);
535 sigqueue_take(&job->ksi);
541 * The thread argument here is used to find the owning process
542 * and is also passed to fo_close() which may pass it to various
543 * places such as devsw close() routines. Because of that, we
544 * need a thread pointer from the process owning the job that is
545 * persistent and won't disappear out from under us or move to
548 * Currently, all the callers of this function call it to remove
549 * a kaiocb from the current process' job list either via a
550 * syscall or due to the current process calling exit() or
551 * execve(). Thus, we know that p == curproc. We also know that
552 * curthread can't exit since we are curthread.
554 * Therefore, we use curthread as the thread to pass to
555 * knlist_delete(). This does mean that it is possible for the
556 * thread pointer at close time to differ from the thread pointer
557 * at open time, but this is already true of file descriptors in
558 * a multithreaded process.
561 fdrop(job->fd_file, curthread);
563 if (job->uiop != &job->uio)
564 free(job->uiop, M_IOV);
565 uma_zfree(aiocb_zone, job);
572 aio_proc_rundown_exec(void *arg, struct proc *p,
573 struct image_params *imgp __unused)
575 aio_proc_rundown(arg, p);
579 aio_cancel_job(struct proc *p, struct kaioinfo *ki, struct kaiocb *job)
581 aio_cancel_fn_t *func;
584 AIO_LOCK_ASSERT(ki, MA_OWNED);
585 if (job->jobflags & (KAIOCB_CANCELLED | KAIOCB_FINISHED))
587 MPASS((job->jobflags & KAIOCB_CANCELLING) == 0);
588 job->jobflags |= KAIOCB_CANCELLED;
590 func = job->cancel_fn;
593 * If there is no cancel routine, just leave the job marked as
594 * cancelled. The job should be in active use by a caller who
595 * should complete it normally or when it fails to install a
602 * Set the CANCELLING flag so that aio_complete() will defer
603 * completions of this job. This prevents the job from being
604 * freed out from under the cancel callback. After the
605 * callback any deferred completion (whether from the callback
606 * or any other source) will be completed.
608 job->jobflags |= KAIOCB_CANCELLING;
612 job->jobflags &= ~KAIOCB_CANCELLING;
613 if (job->jobflags & KAIOCB_FINISHED) {
614 cancelled = job->uaiocb._aiocb_private.error == ECANCELED;
615 TAILQ_REMOVE(&ki->kaio_jobqueue, job, plist);
616 aio_bio_done_notify(p, job);
619 * The cancel callback might have scheduled an
620 * operation to cancel this request, but it is
621 * only counted as cancelled if the request is
622 * cancelled when the callback returns.
630 * Rundown the jobs for a given process.
633 aio_proc_rundown(void *arg, struct proc *p)
636 struct aioliojob *lj;
637 struct kaiocb *job, *jobn;
639 KASSERT(curthread->td_proc == p,
640 ("%s: called on non-curproc", __func__));
646 ki->kaio_flags |= KAIO_RUNDOWN;
651 * Try to cancel all pending requests. This code simulates
652 * aio_cancel on all pending I/O requests.
654 TAILQ_FOREACH_SAFE(job, &ki->kaio_jobqueue, plist, jobn) {
655 aio_cancel_job(p, ki, job);
658 /* Wait for all running I/O to be finished */
659 if (TAILQ_FIRST(&ki->kaio_jobqueue) || ki->kaio_active_count != 0) {
660 ki->kaio_flags |= KAIO_WAKEUP;
661 msleep(&p->p_aioinfo, AIO_MTX(ki), PRIBIO, "aioprn", hz);
665 /* Free all completed I/O requests. */
666 while ((job = TAILQ_FIRST(&ki->kaio_done)) != NULL)
669 while ((lj = TAILQ_FIRST(&ki->kaio_liojoblist)) != NULL) {
670 if (lj->lioj_count == 0) {
671 TAILQ_REMOVE(&ki->kaio_liojoblist, lj, lioj_list);
672 knlist_delete(&lj->klist, curthread, 1);
674 sigqueue_take(&lj->lioj_ksi);
676 uma_zfree(aiolio_zone, lj);
678 panic("LIO job not cleaned up: C:%d, FC:%d\n",
679 lj->lioj_count, lj->lioj_finished_count);
683 taskqueue_drain(taskqueue_aiod_kick, &ki->kaio_task);
684 taskqueue_drain(taskqueue_aiod_kick, &ki->kaio_sync_task);
685 mtx_destroy(&ki->kaio_mtx);
686 uma_zfree(kaio_zone, ki);
691 * Select a job to run (called by an AIO daemon).
693 static struct kaiocb *
694 aio_selectjob(struct aioproc *aiop)
700 mtx_assert(&aio_job_mtx, MA_OWNED);
702 TAILQ_FOREACH(job, &aio_jobs, list) {
703 userp = job->userproc;
704 ki = userp->p_aioinfo;
706 if (ki->kaio_active_count < max_aio_per_proc) {
707 TAILQ_REMOVE(&aio_jobs, job, list);
708 if (!aio_clear_cancel_function(job))
711 /* Account for currently active jobs. */
712 ki->kaio_active_count++;
720 * Move all data to a permanent storage device. This code
721 * simulates the fsync and fdatasync syscalls.
724 aio_fsync_vnode(struct thread *td, struct vnode *vp, int op)
729 if ((error = vn_start_write(vp, &mp, V_WAIT | PCATCH)) != 0)
731 vn_lock(vp, LK_EXCLUSIVE | LK_RETRY);
732 if (vp->v_object != NULL) {
733 VM_OBJECT_WLOCK(vp->v_object);
734 vm_object_page_clean(vp->v_object, 0, 0, 0);
735 VM_OBJECT_WUNLOCK(vp->v_object);
738 error = VOP_FDATASYNC(vp, td);
740 error = VOP_FSYNC(vp, MNT_WAIT, td);
743 vn_finished_write(mp);
749 * The AIO processing activity for LIO_READ/LIO_WRITE. This is the code that
750 * does the I/O request for the non-bio version of the operations. The normal
751 * vn operations are used, and this code should work in all instances for every
752 * type of file, including pipes, sockets, fifos, and regular files.
754 * XXX I don't think it works well for socket, pipe, and fifo.
757 aio_process_rw(struct kaiocb *job)
759 struct ucred *td_savedcred;
764 long msgsnd_st, msgsnd_end;
765 long msgrcv_st, msgrcv_end;
766 long oublock_st, oublock_end;
767 long inblock_st, inblock_end;
770 KASSERT(job->uaiocb.aio_lio_opcode == LIO_READ ||
771 job->uaiocb.aio_lio_opcode == LIO_READV ||
772 job->uaiocb.aio_lio_opcode == LIO_WRITE ||
773 job->uaiocb.aio_lio_opcode == LIO_WRITEV,
774 ("%s: opcode %d", __func__, job->uaiocb.aio_lio_opcode));
776 aio_switch_vmspace(job);
778 td_savedcred = td->td_ucred;
779 td->td_ucred = job->cred;
780 job->uiop->uio_td = td;
784 opcode = job->uaiocb.aio_lio_opcode;
785 cnt = job->uiop->uio_resid;
787 msgrcv_st = td->td_ru.ru_msgrcv;
788 msgsnd_st = td->td_ru.ru_msgsnd;
789 inblock_st = td->td_ru.ru_inblock;
790 oublock_st = td->td_ru.ru_oublock;
793 * aio_aqueue() acquires a reference to the file that is
794 * released in aio_free_entry().
796 if (opcode == LIO_READ || opcode == LIO_READV) {
797 if (job->uiop->uio_resid == 0)
800 error = fo_read(fp, job->uiop, fp->f_cred, FOF_OFFSET,
803 if (fp->f_type == DTYPE_VNODE)
805 error = fo_write(fp, job->uiop, fp->f_cred, FOF_OFFSET, td);
807 msgrcv_end = td->td_ru.ru_msgrcv;
808 msgsnd_end = td->td_ru.ru_msgsnd;
809 inblock_end = td->td_ru.ru_inblock;
810 oublock_end = td->td_ru.ru_oublock;
812 job->msgrcv = msgrcv_end - msgrcv_st;
813 job->msgsnd = msgsnd_end - msgsnd_st;
814 job->inblock = inblock_end - inblock_st;
815 job->outblock = oublock_end - oublock_st;
817 if (error != 0 && job->uiop->uio_resid != cnt) {
818 if (error == ERESTART || error == EINTR || error == EWOULDBLOCK)
820 if (error == EPIPE && (opcode & LIO_WRITE)) {
821 PROC_LOCK(job->userproc);
822 kern_psignal(job->userproc, SIGPIPE);
823 PROC_UNLOCK(job->userproc);
827 cnt -= job->uiop->uio_resid;
828 td->td_ucred = td_savedcred;
830 aio_complete(job, -1, error);
832 aio_complete(job, cnt, 0);
836 aio_process_sync(struct kaiocb *job)
838 struct thread *td = curthread;
839 struct ucred *td_savedcred = td->td_ucred;
840 struct file *fp = job->fd_file;
843 KASSERT(job->uaiocb.aio_lio_opcode & LIO_SYNC,
844 ("%s: opcode %d", __func__, job->uaiocb.aio_lio_opcode));
846 td->td_ucred = job->cred;
847 if (fp->f_vnode != NULL) {
848 error = aio_fsync_vnode(td, fp->f_vnode,
849 job->uaiocb.aio_lio_opcode);
851 td->td_ucred = td_savedcred;
853 aio_complete(job, -1, error);
855 aio_complete(job, 0, 0);
859 aio_process_mlock(struct kaiocb *job)
861 struct aiocb *cb = &job->uaiocb;
864 KASSERT(job->uaiocb.aio_lio_opcode == LIO_MLOCK,
865 ("%s: opcode %d", __func__, job->uaiocb.aio_lio_opcode));
867 aio_switch_vmspace(job);
868 error = kern_mlock(job->userproc, job->cred,
869 __DEVOLATILE(uintptr_t, cb->aio_buf), cb->aio_nbytes);
870 aio_complete(job, error != 0 ? -1 : 0, error);
874 aio_bio_done_notify(struct proc *userp, struct kaiocb *job)
876 struct aioliojob *lj;
878 struct kaiocb *sjob, *sjobn;
882 ki = userp->p_aioinfo;
883 AIO_LOCK_ASSERT(ki, MA_OWNED);
887 lj->lioj_finished_count++;
888 if (lj->lioj_count == lj->lioj_finished_count)
891 TAILQ_INSERT_TAIL(&ki->kaio_done, job, plist);
892 MPASS(job->jobflags & KAIOCB_FINISHED);
894 if (ki->kaio_flags & KAIO_RUNDOWN)
895 goto notification_done;
897 if (job->uaiocb.aio_sigevent.sigev_notify == SIGEV_SIGNAL ||
898 job->uaiocb.aio_sigevent.sigev_notify == SIGEV_THREAD_ID)
899 aio_sendsig(userp, &job->uaiocb.aio_sigevent, &job->ksi, true);
901 KNOTE_LOCKED(&job->klist, 1);
904 if (lj->lioj_signal.sigev_notify == SIGEV_KEVENT) {
905 lj->lioj_flags |= LIOJ_KEVENT_POSTED;
906 KNOTE_LOCKED(&lj->klist, 1);
908 if ((lj->lioj_flags & (LIOJ_SIGNAL | LIOJ_SIGNAL_POSTED))
910 (lj->lioj_signal.sigev_notify == SIGEV_SIGNAL ||
911 lj->lioj_signal.sigev_notify == SIGEV_THREAD_ID)) {
912 aio_sendsig(userp, &lj->lioj_signal, &lj->lioj_ksi,
914 lj->lioj_flags |= LIOJ_SIGNAL_POSTED;
919 if (job->jobflags & KAIOCB_CHECKSYNC) {
920 schedule_fsync = false;
921 TAILQ_FOREACH_SAFE(sjob, &ki->kaio_syncqueue, list, sjobn) {
922 if (job->fd_file != sjob->fd_file ||
923 job->seqno >= sjob->seqno)
925 if (--sjob->pending > 0)
927 TAILQ_REMOVE(&ki->kaio_syncqueue, sjob, list);
928 if (!aio_clear_cancel_function_locked(sjob))
930 TAILQ_INSERT_TAIL(&ki->kaio_syncready, sjob, list);
931 schedule_fsync = true;
934 taskqueue_enqueue(taskqueue_aiod_kick,
935 &ki->kaio_sync_task);
937 if (ki->kaio_flags & KAIO_WAKEUP) {
938 ki->kaio_flags &= ~KAIO_WAKEUP;
939 wakeup(&userp->p_aioinfo);
944 aio_schedule_fsync(void *context, int pending)
951 while (!TAILQ_EMPTY(&ki->kaio_syncready)) {
952 job = TAILQ_FIRST(&ki->kaio_syncready);
953 TAILQ_REMOVE(&ki->kaio_syncready, job, list);
955 aio_schedule(job, aio_process_sync);
962 aio_cancel_cleared(struct kaiocb *job)
966 * The caller should hold the same queue lock held when
967 * aio_clear_cancel_function() was called and set this flag
968 * ensuring this check sees an up-to-date value. However,
969 * there is no way to assert that.
971 return ((job->jobflags & KAIOCB_CLEARED) != 0);
975 aio_clear_cancel_function_locked(struct kaiocb *job)
978 AIO_LOCK_ASSERT(job->userproc->p_aioinfo, MA_OWNED);
979 MPASS(job->cancel_fn != NULL);
980 if (job->jobflags & KAIOCB_CANCELLING) {
981 job->jobflags |= KAIOCB_CLEARED;
984 job->cancel_fn = NULL;
989 aio_clear_cancel_function(struct kaiocb *job)
994 ki = job->userproc->p_aioinfo;
996 ret = aio_clear_cancel_function_locked(job);
1002 aio_set_cancel_function_locked(struct kaiocb *job, aio_cancel_fn_t *func)
1005 AIO_LOCK_ASSERT(job->userproc->p_aioinfo, MA_OWNED);
1006 if (job->jobflags & KAIOCB_CANCELLED)
1008 job->cancel_fn = func;
1013 aio_set_cancel_function(struct kaiocb *job, aio_cancel_fn_t *func)
1015 struct kaioinfo *ki;
1018 ki = job->userproc->p_aioinfo;
1020 ret = aio_set_cancel_function_locked(job, func);
1026 aio_complete(struct kaiocb *job, long status, int error)
1028 struct kaioinfo *ki;
1031 job->uaiocb._aiocb_private.error = error;
1032 job->uaiocb._aiocb_private.status = status;
1034 userp = job->userproc;
1035 ki = userp->p_aioinfo;
1038 KASSERT(!(job->jobflags & KAIOCB_FINISHED),
1039 ("duplicate aio_complete"));
1040 job->jobflags |= KAIOCB_FINISHED;
1041 if ((job->jobflags & (KAIOCB_QUEUEING | KAIOCB_CANCELLING)) == 0) {
1042 TAILQ_REMOVE(&ki->kaio_jobqueue, job, plist);
1043 aio_bio_done_notify(userp, job);
1049 aio_cancel(struct kaiocb *job)
1052 aio_complete(job, -1, ECANCELED);
1056 aio_switch_vmspace(struct kaiocb *job)
1059 vmspace_switch_aio(job->userproc->p_vmspace);
1063 * The AIO daemon, most of the actual work is done in aio_process_*,
1064 * but the setup (and address space mgmt) is done in this routine.
1067 aio_daemon(void *_id)
1070 struct aioproc *aiop;
1071 struct kaioinfo *ki;
1073 struct vmspace *myvm;
1074 struct thread *td = curthread;
1075 int id = (intptr_t)_id;
1078 * Grab an extra reference on the daemon's vmspace so that it
1079 * doesn't get freed by jobs that switch to a different
1083 myvm = vmspace_acquire_ref(p);
1085 KASSERT(p->p_textvp == NULL, ("kthread has a textvp"));
1088 * Allocate and ready the aio control info. There is one aiop structure
1091 aiop = uma_zalloc(aiop_zone, M_WAITOK);
1093 aiop->aioprocflags = 0;
1096 * Wakeup parent process. (Parent sleeps to keep from blasting away
1097 * and creating too many daemons.)
1099 sema_post(&aio_newproc_sem);
1101 mtx_lock(&aio_job_mtx);
1104 * Take daemon off of free queue
1106 if (aiop->aioprocflags & AIOP_FREE) {
1107 TAILQ_REMOVE(&aio_freeproc, aiop, list);
1108 aiop->aioprocflags &= ~AIOP_FREE;
1114 while ((job = aio_selectjob(aiop)) != NULL) {
1115 mtx_unlock(&aio_job_mtx);
1117 ki = job->userproc->p_aioinfo;
1118 job->handle_fn(job);
1120 mtx_lock(&aio_job_mtx);
1121 /* Decrement the active job count. */
1122 ki->kaio_active_count--;
1126 * Disconnect from user address space.
1128 if (p->p_vmspace != myvm) {
1129 mtx_unlock(&aio_job_mtx);
1130 vmspace_switch_aio(myvm);
1131 mtx_lock(&aio_job_mtx);
1133 * We have to restart to avoid race, we only sleep if
1134 * no job can be selected.
1139 mtx_assert(&aio_job_mtx, MA_OWNED);
1141 TAILQ_INSERT_HEAD(&aio_freeproc, aiop, list);
1142 aiop->aioprocflags |= AIOP_FREE;
1145 * If daemon is inactive for a long time, allow it to exit,
1146 * thereby freeing resources.
1148 if (msleep(p, &aio_job_mtx, PRIBIO, "aiordy",
1149 aiod_lifetime) == EWOULDBLOCK && TAILQ_EMPTY(&aio_jobs) &&
1150 (aiop->aioprocflags & AIOP_FREE) &&
1151 num_aio_procs > target_aio_procs)
1154 TAILQ_REMOVE(&aio_freeproc, aiop, list);
1156 mtx_unlock(&aio_job_mtx);
1157 uma_zfree(aiop_zone, aiop);
1158 free_unr(aiod_unr, id);
1161 KASSERT(p->p_vmspace == myvm,
1162 ("AIOD: bad vmspace for exiting daemon"));
1163 KASSERT(refcount_load(&myvm->vm_refcnt) > 1,
1164 ("AIOD: bad vm refcnt for exiting daemon: %d",
1165 refcount_load(&myvm->vm_refcnt)));
1170 * Create a new AIO daemon. This is mostly a kernel-thread fork routine. The
1171 * AIO daemon modifies its environment itself.
1174 aio_newproc(int *start)
1180 id = alloc_unr(aiod_unr);
1181 error = kproc_create(aio_daemon, (void *)(intptr_t)id, &p,
1182 RFNOWAIT, 0, "aiod%d", id);
1185 * Wait until daemon is started.
1187 sema_wait(&aio_newproc_sem);
1188 mtx_lock(&aio_job_mtx);
1192 mtx_unlock(&aio_job_mtx);
1194 free_unr(aiod_unr, id);
1200 * Try the high-performance, low-overhead bio method for eligible
1201 * VCHR devices. This method doesn't use an aio helper thread, and
1202 * thus has very low overhead.
1204 * Assumes that the caller, aio_aqueue(), has incremented the file
1205 * structure's reference count, preventing its deallocation for the
1206 * duration of this call.
1209 aio_qbio(struct proc *p, struct kaiocb *job)
1217 struct kaioinfo *ki;
1218 struct bio **bios = NULL;
1220 int bio_cmd, error, i, iovcnt, opcode, poff, ref;
1226 opcode = cb->aio_lio_opcode;
1228 if (!(opcode == LIO_WRITE || opcode == LIO_WRITEV ||
1229 opcode == LIO_READ || opcode == LIO_READV))
1231 if (fp == NULL || fp->f_type != DTYPE_VNODE)
1235 if (vp->v_type != VCHR)
1237 if (vp->v_bufobj.bo_bsize == 0)
1240 bio_cmd = (opcode & LIO_WRITE) ? BIO_WRITE : BIO_READ;
1241 iovcnt = job->uiop->uio_iovcnt;
1242 if (iovcnt > max_buf_aio)
1244 for (i = 0; i < iovcnt; i++) {
1245 if (job->uiop->uio_iov[i].iov_len % vp->v_bufobj.bo_bsize != 0)
1247 if (job->uiop->uio_iov[i].iov_len > maxphys) {
1252 offset = cb->aio_offset;
1255 csw = devvn_refthread(vp, &dev, &ref);
1259 if ((csw->d_flags & D_DISK) == 0) {
1263 if (job->uiop->uio_resid > dev->si_iosize_max) {
1271 use_unmapped = (dev->si_flags & SI_UNMAPPED) && unmapped_buf_allowed;
1272 if (!use_unmapped) {
1274 if (ki->kaio_buffer_count + iovcnt > max_buf_aio) {
1279 ki->kaio_buffer_count += iovcnt;
1283 bios = malloc(sizeof(struct bio *) * iovcnt, M_TEMP, M_WAITOK);
1284 atomic_store_int(&job->nbio, iovcnt);
1285 for (i = 0; i < iovcnt; i++) {
1286 struct vm_page** pages;
1292 buf = job->uiop->uio_iov[i].iov_base;
1293 nbytes = job->uiop->uio_iov[i].iov_len;
1295 bios[i] = g_alloc_bio();
1298 poff = (vm_offset_t)buf & PAGE_MASK;
1301 pages = malloc(sizeof(vm_page_t) * (atop(round_page(
1302 nbytes)) + 1), M_TEMP, M_WAITOK | M_ZERO);
1304 pbuf = uma_zalloc(pbuf_zone, M_WAITOK);
1306 pages = pbuf->b_pages;
1309 bp->bio_length = nbytes;
1310 bp->bio_bcount = nbytes;
1311 bp->bio_done = aio_biowakeup;
1312 bp->bio_offset = offset;
1313 bp->bio_cmd = bio_cmd;
1315 bp->bio_caller1 = job;
1316 bp->bio_caller2 = pbuf;
1318 prot = VM_PROT_READ;
1319 if (opcode == LIO_READ || opcode == LIO_READV)
1320 prot |= VM_PROT_WRITE; /* Less backwards than it looks */
1321 npages = vm_fault_quick_hold_pages(&curproc->p_vmspace->vm_map,
1322 (vm_offset_t)buf, bp->bio_length, prot, pages,
1326 uma_zfree(pbuf_zone, pbuf);
1328 free(pages, M_TEMP);
1335 pmap_qenter((vm_offset_t)pbuf->b_data, pages, npages);
1336 bp->bio_data = pbuf->b_data + poff;
1337 pbuf->b_npages = npages;
1338 atomic_add_int(&num_buf_aio, 1);
1341 bp->bio_ma_n = npages;
1342 bp->bio_ma_offset = poff;
1343 bp->bio_data = unmapped_buf;
1344 bp->bio_flags |= BIO_UNMAPPED;
1345 atomic_add_int(&num_unmapped_aio, 1);
1351 /* Perform transfer. */
1352 for (i = 0; i < iovcnt; i++)
1353 csw->d_strategy(bios[i]);
1356 dev_relthread(dev, ref);
1361 aio_biocleanup(bios[i]);
1364 dev_relthread(dev, ref);
1368 #ifdef COMPAT_FREEBSD6
1370 convert_old_sigevent(struct osigevent *osig, struct sigevent *nsig)
1374 * Only SIGEV_NONE, SIGEV_SIGNAL, and SIGEV_KEVENT are
1375 * supported by AIO with the old sigevent structure.
1377 nsig->sigev_notify = osig->sigev_notify;
1378 switch (nsig->sigev_notify) {
1382 nsig->sigev_signo = osig->__sigev_u.__sigev_signo;
1385 nsig->sigev_notify_kqueue =
1386 osig->__sigev_u.__sigev_notify_kqueue;
1387 nsig->sigev_value.sival_ptr = osig->sigev_value.sival_ptr;
1396 aiocb_copyin_old_sigevent(struct aiocb *ujob, struct kaiocb *kjob,
1399 struct oaiocb *ojob;
1400 struct aiocb *kcb = &kjob->uaiocb;
1403 bzero(kcb, sizeof(struct aiocb));
1404 error = copyin(ujob, kcb, sizeof(struct oaiocb));
1407 /* No need to copyin aio_iov, because it did not exist in FreeBSD 6 */
1408 ojob = (struct oaiocb *)kcb;
1409 return (convert_old_sigevent(&ojob->aio_sigevent, &kcb->aio_sigevent));
1414 aiocb_copyin(struct aiocb *ujob, struct kaiocb *kjob, int type)
1416 struct aiocb *kcb = &kjob->uaiocb;
1419 error = copyin(ujob, kcb, sizeof(struct aiocb));
1422 if (type & LIO_VECTORED) {
1423 /* malloc a uio and copy in the iovec */
1424 error = copyinuio(__DEVOLATILE(struct iovec*, kcb->aio_iov),
1425 kcb->aio_iovcnt, &kjob->uiop);
1432 aiocb_fetch_status(struct aiocb *ujob)
1435 return (fuword(&ujob->_aiocb_private.status));
1439 aiocb_fetch_error(struct aiocb *ujob)
1442 return (fuword(&ujob->_aiocb_private.error));
1446 aiocb_store_status(struct aiocb *ujob, long status)
1449 return (suword(&ujob->_aiocb_private.status, status));
1453 aiocb_store_error(struct aiocb *ujob, long error)
1456 return (suword(&ujob->_aiocb_private.error, error));
1460 aiocb_store_kernelinfo(struct aiocb *ujob, long jobref)
1463 return (suword(&ujob->_aiocb_private.kernelinfo, jobref));
1467 aiocb_store_aiocb(struct aiocb **ujobp, struct aiocb *ujob)
1470 return (suword(ujobp, (long)ujob));
1473 static struct aiocb_ops aiocb_ops = {
1474 .aio_copyin = aiocb_copyin,
1475 .fetch_status = aiocb_fetch_status,
1476 .fetch_error = aiocb_fetch_error,
1477 .store_status = aiocb_store_status,
1478 .store_error = aiocb_store_error,
1479 .store_kernelinfo = aiocb_store_kernelinfo,
1480 .store_aiocb = aiocb_store_aiocb,
1483 #ifdef COMPAT_FREEBSD6
1484 static struct aiocb_ops aiocb_ops_osigevent = {
1485 .aio_copyin = aiocb_copyin_old_sigevent,
1486 .fetch_status = aiocb_fetch_status,
1487 .fetch_error = aiocb_fetch_error,
1488 .store_status = aiocb_store_status,
1489 .store_error = aiocb_store_error,
1490 .store_kernelinfo = aiocb_store_kernelinfo,
1491 .store_aiocb = aiocb_store_aiocb,
1496 * Queue a new AIO request. Choosing either the threaded or direct bio VCHR
1497 * technique is done in this code.
1500 aio_aqueue(struct thread *td, struct aiocb *ujob, struct aioliojob *lj,
1501 int type, struct aiocb_ops *ops)
1503 struct proc *p = td->td_proc;
1504 struct file *fp = NULL;
1506 struct kaioinfo *ki;
1514 if (p->p_aioinfo == NULL)
1515 aio_init_aioinfo(p);
1519 ops->store_status(ujob, -1);
1520 ops->store_error(ujob, 0);
1521 ops->store_kernelinfo(ujob, -1);
1523 if (num_queue_count >= max_queue_count ||
1524 ki->kaio_count >= max_aio_queue_per_proc) {
1529 job = uma_zalloc(aiocb_zone, M_WAITOK | M_ZERO);
1530 knlist_init_mtx(&job->klist, AIO_MTX(ki));
1532 error = ops->aio_copyin(ujob, job, type);
1536 if (job->uaiocb.aio_nbytes > IOSIZE_MAX) {
1541 if (job->uaiocb.aio_sigevent.sigev_notify != SIGEV_KEVENT &&
1542 job->uaiocb.aio_sigevent.sigev_notify != SIGEV_SIGNAL &&
1543 job->uaiocb.aio_sigevent.sigev_notify != SIGEV_THREAD_ID &&
1544 job->uaiocb.aio_sigevent.sigev_notify != SIGEV_NONE) {
1549 if ((job->uaiocb.aio_sigevent.sigev_notify == SIGEV_SIGNAL ||
1550 job->uaiocb.aio_sigevent.sigev_notify == SIGEV_THREAD_ID) &&
1551 !_SIG_VALID(job->uaiocb.aio_sigevent.sigev_signo)) {
1556 /* Get the opcode. */
1557 if (type == LIO_NOP) {
1558 switch (job->uaiocb.aio_lio_opcode) {
1562 opcode = job->uaiocb.aio_lio_opcode;
1569 opcode = job->uaiocb.aio_lio_opcode = type;
1571 ksiginfo_init(&job->ksi);
1573 /* Save userspace address of the job info. */
1577 * Validate the opcode and fetch the file object for the specified
1580 * XXXRW: Moved the opcode validation up here so that we don't
1581 * retrieve a file descriptor without knowing what the capabiltity
1584 fd = job->uaiocb.aio_fildes;
1588 error = fget_write(td, fd, &cap_pwrite_rights, &fp);
1592 error = fget_read(td, fd, &cap_pread_rights, &fp);
1596 error = fget(td, fd, &cap_fsync_rights, &fp);
1601 error = fget(td, fd, &cap_no_rights, &fp);
1609 if ((opcode & LIO_SYNC) && fp->f_vnode == NULL) {
1614 if ((opcode == LIO_READ || opcode == LIO_READV ||
1615 opcode == LIO_WRITE || opcode == LIO_WRITEV) &&
1616 job->uaiocb.aio_offset < 0 &&
1617 (fp->f_vnode == NULL || fp->f_vnode->v_type != VCHR)) {
1624 mtx_lock(&aio_job_mtx);
1626 job->seqno = jobseqno++;
1627 mtx_unlock(&aio_job_mtx);
1628 error = ops->store_kernelinfo(ujob, jid);
1633 job->uaiocb._aiocb_private.kernelinfo = (void *)(intptr_t)jid;
1635 if (opcode == LIO_NOP) {
1637 MPASS(job->uiop == &job->uio || job->uiop == NULL);
1638 uma_zfree(aiocb_zone, job);
1642 if (job->uaiocb.aio_sigevent.sigev_notify != SIGEV_KEVENT)
1644 evflags = job->uaiocb.aio_sigevent.sigev_notify_kevent_flags;
1645 if ((evflags & ~(EV_CLEAR | EV_DISPATCH | EV_ONESHOT)) != 0) {
1649 kqfd = job->uaiocb.aio_sigevent.sigev_notify_kqueue;
1650 memset(&kev, 0, sizeof(kev));
1651 kev.ident = (uintptr_t)job->ujob;
1652 kev.filter = EVFILT_AIO;
1653 kev.flags = EV_ADD | EV_ENABLE | EV_FLAG1 | evflags;
1654 kev.data = (intptr_t)job;
1655 kev.udata = job->uaiocb.aio_sigevent.sigev_value.sival_ptr;
1656 error = kqfd_register(kqfd, &kev, td, M_WAITOK);
1662 ops->store_error(ujob, EINPROGRESS);
1663 job->uaiocb._aiocb_private.error = EINPROGRESS;
1665 job->cred = crhold(td->td_ucred);
1666 job->jobflags = KAIOCB_QUEUEING;
1669 if (opcode & LIO_VECTORED) {
1670 /* Use the uio copied in by aio_copyin */
1671 MPASS(job->uiop != &job->uio && job->uiop != NULL);
1673 /* Setup the inline uio */
1674 job->iov[0].iov_base = (void *)(uintptr_t)job->uaiocb.aio_buf;
1675 job->iov[0].iov_len = job->uaiocb.aio_nbytes;
1676 job->uio.uio_iov = job->iov;
1677 job->uio.uio_iovcnt = 1;
1678 job->uio.uio_resid = job->uaiocb.aio_nbytes;
1679 job->uio.uio_segflg = UIO_USERSPACE;
1680 job->uiop = &job->uio;
1682 switch (opcode & (LIO_READ | LIO_WRITE)) {
1684 job->uiop->uio_rw = UIO_READ;
1687 job->uiop->uio_rw = UIO_WRITE;
1690 job->uiop->uio_offset = job->uaiocb.aio_offset;
1691 job->uiop->uio_td = td;
1693 if (opcode == LIO_MLOCK) {
1694 aio_schedule(job, aio_process_mlock);
1696 } else if (fp->f_ops->fo_aio_queue == NULL)
1697 error = aio_queue_file(fp, job);
1699 error = fo_aio_queue(fp, job);
1704 job->jobflags &= ~KAIOCB_QUEUEING;
1705 TAILQ_INSERT_TAIL(&ki->kaio_all, job, allist);
1709 atomic_add_int(&num_queue_count, 1);
1710 if (job->jobflags & KAIOCB_FINISHED) {
1712 * The queue callback completed the request synchronously.
1713 * The bulk of the completion is deferred in that case
1716 aio_bio_done_notify(p, job);
1718 TAILQ_INSERT_TAIL(&ki->kaio_jobqueue, job, plist);
1727 knlist_delete(&job->klist, curthread, 0);
1729 if (job->uiop != &job->uio)
1730 free(job->uiop, M_IOV);
1731 uma_zfree(aiocb_zone, job);
1733 ops->store_error(ujob, error);
1738 aio_cancel_daemon_job(struct kaiocb *job)
1741 mtx_lock(&aio_job_mtx);
1742 if (!aio_cancel_cleared(job))
1743 TAILQ_REMOVE(&aio_jobs, job, list);
1744 mtx_unlock(&aio_job_mtx);
1749 aio_schedule(struct kaiocb *job, aio_handle_fn_t *func)
1752 mtx_lock(&aio_job_mtx);
1753 if (!aio_set_cancel_function(job, aio_cancel_daemon_job)) {
1754 mtx_unlock(&aio_job_mtx);
1758 job->handle_fn = func;
1759 TAILQ_INSERT_TAIL(&aio_jobs, job, list);
1760 aio_kick_nowait(job->userproc);
1761 mtx_unlock(&aio_job_mtx);
1765 aio_cancel_sync(struct kaiocb *job)
1767 struct kaioinfo *ki;
1769 ki = job->userproc->p_aioinfo;
1771 if (!aio_cancel_cleared(job))
1772 TAILQ_REMOVE(&ki->kaio_syncqueue, job, list);
1778 aio_queue_file(struct file *fp, struct kaiocb *job)
1780 struct kaioinfo *ki;
1781 struct kaiocb *job2;
1787 ki = job->userproc->p_aioinfo;
1788 error = aio_qbio(job->userproc, job);
1792 if (fp->f_type == DTYPE_VNODE) {
1794 if (vp->v_type == VREG || vp->v_type == VDIR) {
1795 mp = fp->f_vnode->v_mount;
1796 if (mp == NULL || (mp->mnt_flag & MNT_LOCAL) != 0)
1800 if (!(safe || enable_aio_unsafe)) {
1801 counted_warning(&unsafe_warningcnt,
1802 "is attempting to use unsafe AIO requests");
1803 return (EOPNOTSUPP);
1806 if (job->uaiocb.aio_lio_opcode & (LIO_WRITE | LIO_READ)) {
1807 aio_schedule(job, aio_process_rw);
1809 } else if (job->uaiocb.aio_lio_opcode & LIO_SYNC) {
1811 TAILQ_FOREACH(job2, &ki->kaio_jobqueue, plist) {
1812 if (job2->fd_file == job->fd_file &&
1813 ((job2->uaiocb.aio_lio_opcode & LIO_SYNC) == 0) &&
1814 job2->seqno < job->seqno) {
1815 job2->jobflags |= KAIOCB_CHECKSYNC;
1819 if (job->pending != 0) {
1820 if (!aio_set_cancel_function_locked(job,
1826 TAILQ_INSERT_TAIL(&ki->kaio_syncqueue, job, list);
1831 aio_schedule(job, aio_process_sync);
1840 aio_kick_nowait(struct proc *userp)
1842 struct kaioinfo *ki = userp->p_aioinfo;
1843 struct aioproc *aiop;
1845 mtx_assert(&aio_job_mtx, MA_OWNED);
1846 if ((aiop = TAILQ_FIRST(&aio_freeproc)) != NULL) {
1847 TAILQ_REMOVE(&aio_freeproc, aiop, list);
1848 aiop->aioprocflags &= ~AIOP_FREE;
1849 wakeup(aiop->aioproc);
1850 } else if (num_aio_resv_start + num_aio_procs < max_aio_procs &&
1851 ki->kaio_active_count + num_aio_resv_start < max_aio_per_proc) {
1852 taskqueue_enqueue(taskqueue_aiod_kick, &ki->kaio_task);
1857 aio_kick(struct proc *userp)
1859 struct kaioinfo *ki = userp->p_aioinfo;
1860 struct aioproc *aiop;
1863 mtx_assert(&aio_job_mtx, MA_OWNED);
1865 if ((aiop = TAILQ_FIRST(&aio_freeproc)) != NULL) {
1866 TAILQ_REMOVE(&aio_freeproc, aiop, list);
1867 aiop->aioprocflags &= ~AIOP_FREE;
1868 wakeup(aiop->aioproc);
1869 } else if (num_aio_resv_start + num_aio_procs < max_aio_procs &&
1870 ki->kaio_active_count + num_aio_resv_start < max_aio_per_proc) {
1871 num_aio_resv_start++;
1872 mtx_unlock(&aio_job_mtx);
1873 error = aio_newproc(&num_aio_resv_start);
1874 mtx_lock(&aio_job_mtx);
1876 num_aio_resv_start--;
1886 aio_kick_helper(void *context, int pending)
1888 struct proc *userp = context;
1890 mtx_lock(&aio_job_mtx);
1891 while (--pending >= 0) {
1892 if (aio_kick(userp))
1895 mtx_unlock(&aio_job_mtx);
1899 * Support the aio_return system call, as a side-effect, kernel resources are
1903 kern_aio_return(struct thread *td, struct aiocb *ujob, struct aiocb_ops *ops)
1905 struct proc *p = td->td_proc;
1907 struct kaioinfo *ki;
1914 TAILQ_FOREACH(job, &ki->kaio_done, plist) {
1915 if (job->ujob == ujob)
1919 MPASS(job->jobflags & KAIOCB_FINISHED);
1920 status = job->uaiocb._aiocb_private.status;
1921 error = job->uaiocb._aiocb_private.error;
1922 td->td_retval[0] = status;
1923 td->td_ru.ru_oublock += job->outblock;
1924 td->td_ru.ru_inblock += job->inblock;
1925 td->td_ru.ru_msgsnd += job->msgsnd;
1926 td->td_ru.ru_msgrcv += job->msgrcv;
1927 aio_free_entry(job);
1929 ops->store_error(ujob, error);
1930 ops->store_status(ujob, status);
1939 sys_aio_return(struct thread *td, struct aio_return_args *uap)
1942 return (kern_aio_return(td, uap->aiocbp, &aiocb_ops));
1946 * Allow a process to wakeup when any of the I/O requests are completed.
1949 kern_aio_suspend(struct thread *td, int njoblist, struct aiocb **ujoblist,
1950 struct timespec *ts)
1952 struct proc *p = td->td_proc;
1954 struct kaioinfo *ki;
1955 struct kaiocb *firstjob, *job;
1960 if (ts->tv_nsec < 0 || ts->tv_nsec >= 1000000000)
1963 TIMESPEC_TO_TIMEVAL(&atv, ts);
1964 if (itimerfix(&atv))
1966 timo = tvtohz(&atv);
1980 TAILQ_FOREACH(job, &ki->kaio_all, allist) {
1981 for (i = 0; i < njoblist; i++) {
1982 if (job->ujob == ujoblist[i]) {
1983 if (firstjob == NULL)
1985 if (job->jobflags & KAIOCB_FINISHED)
1990 /* All tasks were finished. */
1991 if (firstjob == NULL)
1994 ki->kaio_flags |= KAIO_WAKEUP;
1995 error = msleep(&p->p_aioinfo, AIO_MTX(ki), PRIBIO | PCATCH,
1997 if (error == ERESTART)
2008 sys_aio_suspend(struct thread *td, struct aio_suspend_args *uap)
2010 struct timespec ts, *tsp;
2011 struct aiocb **ujoblist;
2014 if (uap->nent < 0 || uap->nent > max_aio_queue_per_proc)
2018 /* Get timespec struct. */
2019 if ((error = copyin(uap->timeout, &ts, sizeof(ts))) != 0)
2025 ujoblist = malloc(uap->nent * sizeof(ujoblist[0]), M_AIOS, M_WAITOK);
2026 error = copyin(uap->aiocbp, ujoblist, uap->nent * sizeof(ujoblist[0]));
2028 error = kern_aio_suspend(td, uap->nent, ujoblist, tsp);
2029 free(ujoblist, M_AIOS);
2034 * aio_cancel cancels any non-bio aio operations not currently in progress.
2037 sys_aio_cancel(struct thread *td, struct aio_cancel_args *uap)
2039 struct proc *p = td->td_proc;
2040 struct kaioinfo *ki;
2041 struct kaiocb *job, *jobn;
2045 int notcancelled = 0;
2048 /* Lookup file object. */
2049 error = fget(td, uap->fd, &cap_no_rights, &fp);
2057 if (fp->f_type == DTYPE_VNODE) {
2059 if (vn_isdisk(vp)) {
2061 td->td_retval[0] = AIO_NOTCANCELED;
2067 TAILQ_FOREACH_SAFE(job, &ki->kaio_jobqueue, plist, jobn) {
2068 if ((uap->fd == job->uaiocb.aio_fildes) &&
2069 ((uap->aiocbp == NULL) ||
2070 (uap->aiocbp == job->ujob))) {
2071 if (aio_cancel_job(p, ki, job)) {
2076 if (uap->aiocbp != NULL)
2085 if (uap->aiocbp != NULL) {
2087 td->td_retval[0] = AIO_CANCELED;
2093 td->td_retval[0] = AIO_NOTCANCELED;
2098 td->td_retval[0] = AIO_CANCELED;
2102 td->td_retval[0] = AIO_ALLDONE;
2108 * aio_error is implemented in the kernel level for compatibility purposes
2109 * only. For a user mode async implementation, it would be best to do it in
2110 * a userland subroutine.
2113 kern_aio_error(struct thread *td, struct aiocb *ujob, struct aiocb_ops *ops)
2115 struct proc *p = td->td_proc;
2117 struct kaioinfo *ki;
2122 td->td_retval[0] = EINVAL;
2127 TAILQ_FOREACH(job, &ki->kaio_all, allist) {
2128 if (job->ujob == ujob) {
2129 if (job->jobflags & KAIOCB_FINISHED)
2131 job->uaiocb._aiocb_private.error;
2133 td->td_retval[0] = EINPROGRESS;
2141 * Hack for failure of aio_aqueue.
2143 status = ops->fetch_status(ujob);
2145 td->td_retval[0] = ops->fetch_error(ujob);
2149 td->td_retval[0] = EINVAL;
2154 sys_aio_error(struct thread *td, struct aio_error_args *uap)
2157 return (kern_aio_error(td, uap->aiocbp, &aiocb_ops));
2160 /* syscall - asynchronous read from a file (REALTIME) */
2161 #ifdef COMPAT_FREEBSD6
2163 freebsd6_aio_read(struct thread *td, struct freebsd6_aio_read_args *uap)
2166 return (aio_aqueue(td, (struct aiocb *)uap->aiocbp, NULL, LIO_READ,
2167 &aiocb_ops_osigevent));
2172 sys_aio_read(struct thread *td, struct aio_read_args *uap)
2175 return (aio_aqueue(td, uap->aiocbp, NULL, LIO_READ, &aiocb_ops));
2179 sys_aio_readv(struct thread *td, struct aio_readv_args *uap)
2182 return (aio_aqueue(td, uap->aiocbp, NULL, LIO_READV, &aiocb_ops));
2185 /* syscall - asynchronous write to a file (REALTIME) */
2186 #ifdef COMPAT_FREEBSD6
2188 freebsd6_aio_write(struct thread *td, struct freebsd6_aio_write_args *uap)
2191 return (aio_aqueue(td, (struct aiocb *)uap->aiocbp, NULL, LIO_WRITE,
2192 &aiocb_ops_osigevent));
2197 sys_aio_write(struct thread *td, struct aio_write_args *uap)
2200 return (aio_aqueue(td, uap->aiocbp, NULL, LIO_WRITE, &aiocb_ops));
2204 sys_aio_writev(struct thread *td, struct aio_writev_args *uap)
2207 return (aio_aqueue(td, uap->aiocbp, NULL, LIO_WRITEV, &aiocb_ops));
2211 sys_aio_mlock(struct thread *td, struct aio_mlock_args *uap)
2214 return (aio_aqueue(td, uap->aiocbp, NULL, LIO_MLOCK, &aiocb_ops));
2218 kern_lio_listio(struct thread *td, int mode, struct aiocb * const *uacb_list,
2219 struct aiocb **acb_list, int nent, struct sigevent *sig,
2220 struct aiocb_ops *ops)
2222 struct proc *p = td->td_proc;
2224 struct kaioinfo *ki;
2225 struct aioliojob *lj;
2231 if ((mode != LIO_NOWAIT) && (mode != LIO_WAIT))
2234 if (nent < 0 || nent > max_aio_queue_per_proc)
2237 if (p->p_aioinfo == NULL)
2238 aio_init_aioinfo(p);
2242 lj = uma_zalloc(aiolio_zone, M_WAITOK);
2245 lj->lioj_finished_count = 0;
2246 knlist_init_mtx(&lj->klist, AIO_MTX(ki));
2247 ksiginfo_init(&lj->lioj_ksi);
2252 if (sig && (mode == LIO_NOWAIT)) {
2253 bcopy(sig, &lj->lioj_signal, sizeof(lj->lioj_signal));
2254 if (lj->lioj_signal.sigev_notify == SIGEV_KEVENT) {
2255 /* Assume only new style KEVENT */
2256 memset(&kev, 0, sizeof(kev));
2257 kev.filter = EVFILT_LIO;
2258 kev.flags = EV_ADD | EV_ENABLE | EV_FLAG1;
2259 kev.ident = (uintptr_t)uacb_list; /* something unique */
2260 kev.data = (intptr_t)lj;
2261 /* pass user defined sigval data */
2262 kev.udata = lj->lioj_signal.sigev_value.sival_ptr;
2263 error = kqfd_register(
2264 lj->lioj_signal.sigev_notify_kqueue, &kev, td,
2267 uma_zfree(aiolio_zone, lj);
2270 } else if (lj->lioj_signal.sigev_notify == SIGEV_NONE) {
2272 } else if (lj->lioj_signal.sigev_notify == SIGEV_SIGNAL ||
2273 lj->lioj_signal.sigev_notify == SIGEV_THREAD_ID) {
2274 if (!_SIG_VALID(lj->lioj_signal.sigev_signo)) {
2275 uma_zfree(aiolio_zone, lj);
2278 lj->lioj_flags |= LIOJ_SIGNAL;
2280 uma_zfree(aiolio_zone, lj);
2286 TAILQ_INSERT_TAIL(&ki->kaio_liojoblist, lj, lioj_list);
2288 * Add extra aiocb count to avoid the lio to be freed
2289 * by other threads doing aio_waitcomplete or aio_return,
2290 * and prevent event from being sent until we have queued
2297 * Get pointers to the list of I/O requests.
2301 for (i = 0; i < nent; i++) {
2304 error = aio_aqueue(td, job, lj, LIO_NOP, ops);
2305 if (error == EAGAIN)
2307 else if (error != 0)
2314 if (mode == LIO_WAIT) {
2315 while (lj->lioj_count - 1 != lj->lioj_finished_count) {
2316 ki->kaio_flags |= KAIO_WAKEUP;
2317 error = msleep(&p->p_aioinfo, AIO_MTX(ki),
2318 PRIBIO | PCATCH, "aiospn", 0);
2319 if (error == ERESTART)
2325 if (lj->lioj_count - 1 == lj->lioj_finished_count) {
2326 if (lj->lioj_signal.sigev_notify == SIGEV_KEVENT) {
2327 lj->lioj_flags |= LIOJ_KEVENT_POSTED;
2328 KNOTE_LOCKED(&lj->klist, 1);
2330 if ((lj->lioj_flags & (LIOJ_SIGNAL |
2331 LIOJ_SIGNAL_POSTED)) == LIOJ_SIGNAL &&
2332 (lj->lioj_signal.sigev_notify == SIGEV_SIGNAL ||
2333 lj->lioj_signal.sigev_notify == SIGEV_THREAD_ID)) {
2334 aio_sendsig(p, &lj->lioj_signal, &lj->lioj_ksi,
2335 lj->lioj_count != 1);
2336 lj->lioj_flags |= LIOJ_SIGNAL_POSTED;
2341 if (lj->lioj_count == 0) {
2342 TAILQ_REMOVE(&ki->kaio_liojoblist, lj, lioj_list);
2343 knlist_delete(&lj->klist, curthread, 1);
2345 sigqueue_take(&lj->lioj_ksi);
2348 uma_zfree(aiolio_zone, lj);
2360 /* syscall - list directed I/O (REALTIME) */
2361 #ifdef COMPAT_FREEBSD6
2363 freebsd6_lio_listio(struct thread *td, struct freebsd6_lio_listio_args *uap)
2365 struct aiocb **acb_list;
2366 struct sigevent *sigp, sig;
2367 struct osigevent osig;
2370 if ((uap->mode != LIO_NOWAIT) && (uap->mode != LIO_WAIT))
2374 if (nent < 0 || nent > max_aio_queue_per_proc)
2377 if (uap->sig && (uap->mode == LIO_NOWAIT)) {
2378 error = copyin(uap->sig, &osig, sizeof(osig));
2381 error = convert_old_sigevent(&osig, &sig);
2388 acb_list = malloc(sizeof(struct aiocb *) * nent, M_LIO, M_WAITOK);
2389 error = copyin(uap->acb_list, acb_list, nent * sizeof(acb_list[0]));
2391 error = kern_lio_listio(td, uap->mode,
2392 (struct aiocb * const *)uap->acb_list, acb_list, nent, sigp,
2393 &aiocb_ops_osigevent);
2394 free(acb_list, M_LIO);
2399 /* syscall - list directed I/O (REALTIME) */
2401 sys_lio_listio(struct thread *td, struct lio_listio_args *uap)
2403 struct aiocb **acb_list;
2404 struct sigevent *sigp, sig;
2407 if ((uap->mode != LIO_NOWAIT) && (uap->mode != LIO_WAIT))
2411 if (nent < 0 || nent > max_aio_queue_per_proc)
2414 if (uap->sig && (uap->mode == LIO_NOWAIT)) {
2415 error = copyin(uap->sig, &sig, sizeof(sig));
2422 acb_list = malloc(sizeof(struct aiocb *) * nent, M_LIO, M_WAITOK);
2423 error = copyin(uap->acb_list, acb_list, nent * sizeof(acb_list[0]));
2425 error = kern_lio_listio(td, uap->mode, uap->acb_list, acb_list,
2426 nent, sigp, &aiocb_ops);
2427 free(acb_list, M_LIO);
2432 aio_biocleanup(struct bio *bp)
2434 struct kaiocb *job = (struct kaiocb *)bp->bio_caller1;
2435 struct kaioinfo *ki;
2436 struct buf *pbuf = (struct buf *)bp->bio_caller2;
2438 /* Release mapping into kernel space. */
2440 MPASS(pbuf->b_npages <= atop(maxphys) + 1);
2441 pmap_qremove((vm_offset_t)pbuf->b_data, pbuf->b_npages);
2442 vm_page_unhold_pages(pbuf->b_pages, pbuf->b_npages);
2443 uma_zfree(pbuf_zone, pbuf);
2444 atomic_subtract_int(&num_buf_aio, 1);
2445 ki = job->userproc->p_aioinfo;
2447 ki->kaio_buffer_count--;
2450 MPASS(bp->bio_ma_n <= atop(maxphys) + 1);
2451 vm_page_unhold_pages(bp->bio_ma, bp->bio_ma_n);
2452 free(bp->bio_ma, M_TEMP);
2453 atomic_subtract_int(&num_unmapped_aio, 1);
2459 aio_biowakeup(struct bio *bp)
2461 struct kaiocb *job = (struct kaiocb *)bp->bio_caller1;
2463 long bcount = bp->bio_bcount;
2464 long resid = bp->bio_resid;
2465 int error, opcode, nblks;
2466 int bio_error = bp->bio_error;
2467 uint16_t flags = bp->bio_flags;
2469 opcode = job->uaiocb.aio_lio_opcode;
2473 nbytes =bcount - resid;
2474 atomic_add_acq_long(&job->nbytes, nbytes);
2475 nblks = btodb(nbytes);
2478 * If multiple bios experienced an error, the job will reflect the
2479 * error of whichever failed bio completed last.
2481 if (flags & BIO_ERROR)
2482 atomic_set_int(&job->error, bio_error);
2483 if (opcode & LIO_WRITE)
2484 atomic_add_int(&job->outblock, nblks);
2486 atomic_add_int(&job->inblock, nblks);
2487 atomic_subtract_int(&job->nbio, 1);
2490 if (atomic_load_int(&job->nbio) == 0) {
2491 if (atomic_load_int(&job->error))
2492 aio_complete(job, -1, job->error);
2494 aio_complete(job, atomic_load_long(&job->nbytes), 0);
2498 /* syscall - wait for the next completion of an aio request */
2500 kern_aio_waitcomplete(struct thread *td, struct aiocb **ujobp,
2501 struct timespec *ts, struct aiocb_ops *ops)
2503 struct proc *p = td->td_proc;
2505 struct kaioinfo *ki;
2511 ops->store_aiocb(ujobp, NULL);
2515 } else if (ts->tv_sec == 0 && ts->tv_nsec == 0) {
2518 if ((ts->tv_nsec < 0) || (ts->tv_nsec >= 1000000000))
2521 TIMESPEC_TO_TIMEVAL(&atv, ts);
2522 if (itimerfix(&atv))
2524 timo = tvtohz(&atv);
2527 if (p->p_aioinfo == NULL)
2528 aio_init_aioinfo(p);
2534 while ((job = TAILQ_FIRST(&ki->kaio_done)) == NULL) {
2536 error = EWOULDBLOCK;
2539 ki->kaio_flags |= KAIO_WAKEUP;
2540 error = msleep(&p->p_aioinfo, AIO_MTX(ki), PRIBIO | PCATCH,
2542 if (timo && error == ERESTART)
2549 MPASS(job->jobflags & KAIOCB_FINISHED);
2551 status = job->uaiocb._aiocb_private.status;
2552 error = job->uaiocb._aiocb_private.error;
2553 td->td_retval[0] = status;
2554 td->td_ru.ru_oublock += job->outblock;
2555 td->td_ru.ru_inblock += job->inblock;
2556 td->td_ru.ru_msgsnd += job->msgsnd;
2557 td->td_ru.ru_msgrcv += job->msgrcv;
2558 aio_free_entry(job);
2560 ops->store_aiocb(ujobp, ujob);
2561 ops->store_error(ujob, error);
2562 ops->store_status(ujob, status);
2570 sys_aio_waitcomplete(struct thread *td, struct aio_waitcomplete_args *uap)
2572 struct timespec ts, *tsp;
2576 /* Get timespec struct. */
2577 error = copyin(uap->timeout, &ts, sizeof(ts));
2584 return (kern_aio_waitcomplete(td, uap->aiocbp, tsp, &aiocb_ops));
2588 kern_aio_fsync(struct thread *td, int op, struct aiocb *ujob,
2589 struct aiocb_ops *ops)
2604 return (aio_aqueue(td, ujob, NULL, listop, ops));
2608 sys_aio_fsync(struct thread *td, struct aio_fsync_args *uap)
2611 return (kern_aio_fsync(td, uap->op, uap->aiocbp, &aiocb_ops));
2614 /* kqueue attach function */
2616 filt_aioattach(struct knote *kn)
2620 job = (struct kaiocb *)(uintptr_t)kn->kn_sdata;
2623 * The job pointer must be validated before using it, so
2624 * registration is restricted to the kernel; the user cannot
2627 if ((kn->kn_flags & EV_FLAG1) == 0)
2629 kn->kn_ptr.p_aio = job;
2630 kn->kn_flags &= ~EV_FLAG1;
2632 knlist_add(&job->klist, kn, 0);
2637 /* kqueue detach function */
2639 filt_aiodetach(struct knote *kn)
2643 knl = &kn->kn_ptr.p_aio->klist;
2644 knl->kl_lock(knl->kl_lockarg);
2645 if (!knlist_empty(knl))
2646 knlist_remove(knl, kn, 1);
2647 knl->kl_unlock(knl->kl_lockarg);
2650 /* kqueue filter function */
2653 filt_aio(struct knote *kn, long hint)
2655 struct kaiocb *job = kn->kn_ptr.p_aio;
2657 kn->kn_data = job->uaiocb._aiocb_private.error;
2658 if (!(job->jobflags & KAIOCB_FINISHED))
2660 kn->kn_flags |= EV_EOF;
2664 /* kqueue attach function */
2666 filt_lioattach(struct knote *kn)
2668 struct aioliojob *lj;
2670 lj = (struct aioliojob *)(uintptr_t)kn->kn_sdata;
2673 * The aioliojob pointer must be validated before using it, so
2674 * registration is restricted to the kernel; the user cannot
2677 if ((kn->kn_flags & EV_FLAG1) == 0)
2679 kn->kn_ptr.p_lio = lj;
2680 kn->kn_flags &= ~EV_FLAG1;
2682 knlist_add(&lj->klist, kn, 0);
2687 /* kqueue detach function */
2689 filt_liodetach(struct knote *kn)
2693 knl = &kn->kn_ptr.p_lio->klist;
2694 knl->kl_lock(knl->kl_lockarg);
2695 if (!knlist_empty(knl))
2696 knlist_remove(knl, kn, 1);
2697 knl->kl_unlock(knl->kl_lockarg);
2700 /* kqueue filter function */
2703 filt_lio(struct knote *kn, long hint)
2705 struct aioliojob * lj = kn->kn_ptr.p_lio;
2707 return (lj->lioj_flags & LIOJ_KEVENT_POSTED);
2710 #ifdef COMPAT_FREEBSD32
2711 #include <sys/mount.h>
2712 #include <sys/socket.h>
2713 #include <compat/freebsd32/freebsd32.h>
2714 #include <compat/freebsd32/freebsd32_proto.h>
2715 #include <compat/freebsd32/freebsd32_signal.h>
2716 #include <compat/freebsd32/freebsd32_syscall.h>
2717 #include <compat/freebsd32/freebsd32_util.h>
2719 struct __aiocb_private32 {
2722 uint32_t kernelinfo;
2725 #ifdef COMPAT_FREEBSD6
2726 typedef struct oaiocb32 {
2727 int aio_fildes; /* File descriptor */
2728 uint64_t aio_offset __packed; /* File offset for I/O */
2729 uint32_t aio_buf; /* I/O buffer in process space */
2730 uint32_t aio_nbytes; /* Number of bytes for I/O */
2731 struct osigevent32 aio_sigevent; /* Signal to deliver */
2732 int aio_lio_opcode; /* LIO opcode */
2733 int aio_reqprio; /* Request priority -- ignored */
2734 struct __aiocb_private32 _aiocb_private;
2738 typedef struct aiocb32 {
2739 int32_t aio_fildes; /* File descriptor */
2740 uint64_t aio_offset __packed; /* File offset for I/O */
2741 uint32_t aio_buf; /* I/O buffer in process space */
2742 uint32_t aio_nbytes; /* Number of bytes for I/O */
2744 uint32_t __spare2__;
2745 int aio_lio_opcode; /* LIO opcode */
2746 int aio_reqprio; /* Request priority -- ignored */
2747 struct __aiocb_private32 _aiocb_private;
2748 struct sigevent32 aio_sigevent; /* Signal to deliver */
2751 #ifdef COMPAT_FREEBSD6
2753 convert_old_sigevent32(struct osigevent32 *osig, struct sigevent *nsig)
2757 * Only SIGEV_NONE, SIGEV_SIGNAL, and SIGEV_KEVENT are
2758 * supported by AIO with the old sigevent structure.
2760 CP(*osig, *nsig, sigev_notify);
2761 switch (nsig->sigev_notify) {
2765 nsig->sigev_signo = osig->__sigev_u.__sigev_signo;
2768 nsig->sigev_notify_kqueue =
2769 osig->__sigev_u.__sigev_notify_kqueue;
2770 PTRIN_CP(*osig, *nsig, sigev_value.sival_ptr);
2779 aiocb32_copyin_old_sigevent(struct aiocb *ujob, struct kaiocb *kjob,
2782 struct oaiocb32 job32;
2783 struct aiocb *kcb = &kjob->uaiocb;
2786 bzero(kcb, sizeof(struct aiocb));
2787 error = copyin(ujob, &job32, sizeof(job32));
2791 /* No need to copyin aio_iov, because it did not exist in FreeBSD 6 */
2793 CP(job32, *kcb, aio_fildes);
2794 CP(job32, *kcb, aio_offset);
2795 PTRIN_CP(job32, *kcb, aio_buf);
2796 CP(job32, *kcb, aio_nbytes);
2797 CP(job32, *kcb, aio_lio_opcode);
2798 CP(job32, *kcb, aio_reqprio);
2799 CP(job32, *kcb, _aiocb_private.status);
2800 CP(job32, *kcb, _aiocb_private.error);
2801 PTRIN_CP(job32, *kcb, _aiocb_private.kernelinfo);
2802 return (convert_old_sigevent32(&job32.aio_sigevent,
2803 &kcb->aio_sigevent));
2808 aiocb32_copyin(struct aiocb *ujob, struct kaiocb *kjob, int type)
2810 struct aiocb32 job32;
2811 struct aiocb *kcb = &kjob->uaiocb;
2812 struct iovec32 *iov32;
2815 error = copyin(ujob, &job32, sizeof(job32));
2818 CP(job32, *kcb, aio_fildes);
2819 CP(job32, *kcb, aio_offset);
2820 CP(job32, *kcb, aio_lio_opcode);
2821 if (type & LIO_VECTORED) {
2822 iov32 = PTRIN(job32.aio_iov);
2823 CP(job32, *kcb, aio_iovcnt);
2824 /* malloc a uio and copy in the iovec */
2825 error = freebsd32_copyinuio(iov32,
2826 kcb->aio_iovcnt, &kjob->uiop);
2830 PTRIN_CP(job32, *kcb, aio_buf);
2831 CP(job32, *kcb, aio_nbytes);
2833 CP(job32, *kcb, aio_reqprio);
2834 CP(job32, *kcb, _aiocb_private.status);
2835 CP(job32, *kcb, _aiocb_private.error);
2836 PTRIN_CP(job32, *kcb, _aiocb_private.kernelinfo);
2837 error = convert_sigevent32(&job32.aio_sigevent, &kcb->aio_sigevent);
2843 aiocb32_fetch_status(struct aiocb *ujob)
2845 struct aiocb32 *ujob32;
2847 ujob32 = (struct aiocb32 *)ujob;
2848 return (fuword32(&ujob32->_aiocb_private.status));
2852 aiocb32_fetch_error(struct aiocb *ujob)
2854 struct aiocb32 *ujob32;
2856 ujob32 = (struct aiocb32 *)ujob;
2857 return (fuword32(&ujob32->_aiocb_private.error));
2861 aiocb32_store_status(struct aiocb *ujob, long status)
2863 struct aiocb32 *ujob32;
2865 ujob32 = (struct aiocb32 *)ujob;
2866 return (suword32(&ujob32->_aiocb_private.status, status));
2870 aiocb32_store_error(struct aiocb *ujob, long error)
2872 struct aiocb32 *ujob32;
2874 ujob32 = (struct aiocb32 *)ujob;
2875 return (suword32(&ujob32->_aiocb_private.error, error));
2879 aiocb32_store_kernelinfo(struct aiocb *ujob, long jobref)
2881 struct aiocb32 *ujob32;
2883 ujob32 = (struct aiocb32 *)ujob;
2884 return (suword32(&ujob32->_aiocb_private.kernelinfo, jobref));
2888 aiocb32_store_aiocb(struct aiocb **ujobp, struct aiocb *ujob)
2891 return (suword32(ujobp, (long)ujob));
2894 static struct aiocb_ops aiocb32_ops = {
2895 .aio_copyin = aiocb32_copyin,
2896 .fetch_status = aiocb32_fetch_status,
2897 .fetch_error = aiocb32_fetch_error,
2898 .store_status = aiocb32_store_status,
2899 .store_error = aiocb32_store_error,
2900 .store_kernelinfo = aiocb32_store_kernelinfo,
2901 .store_aiocb = aiocb32_store_aiocb,
2904 #ifdef COMPAT_FREEBSD6
2905 static struct aiocb_ops aiocb32_ops_osigevent = {
2906 .aio_copyin = aiocb32_copyin_old_sigevent,
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,
2917 freebsd32_aio_return(struct thread *td, struct freebsd32_aio_return_args *uap)
2920 return (kern_aio_return(td, (struct aiocb *)uap->aiocbp, &aiocb32_ops));
2924 freebsd32_aio_suspend(struct thread *td, struct freebsd32_aio_suspend_args *uap)
2926 struct timespec32 ts32;
2927 struct timespec ts, *tsp;
2928 struct aiocb **ujoblist;
2929 uint32_t *ujoblist32;
2932 if (uap->nent < 0 || uap->nent > max_aio_queue_per_proc)
2936 /* Get timespec struct. */
2937 if ((error = copyin(uap->timeout, &ts32, sizeof(ts32))) != 0)
2939 CP(ts32, ts, tv_sec);
2940 CP(ts32, ts, tv_nsec);
2945 ujoblist = malloc(uap->nent * sizeof(ujoblist[0]), M_AIOS, M_WAITOK);
2946 ujoblist32 = (uint32_t *)ujoblist;
2947 error = copyin(uap->aiocbp, ujoblist32, uap->nent *
2948 sizeof(ujoblist32[0]));
2950 for (i = uap->nent - 1; i >= 0; i--)
2951 ujoblist[i] = PTRIN(ujoblist32[i]);
2953 error = kern_aio_suspend(td, uap->nent, ujoblist, tsp);
2955 free(ujoblist, M_AIOS);
2960 freebsd32_aio_error(struct thread *td, struct freebsd32_aio_error_args *uap)
2963 return (kern_aio_error(td, (struct aiocb *)uap->aiocbp, &aiocb32_ops));
2966 #ifdef COMPAT_FREEBSD6
2968 freebsd6_freebsd32_aio_read(struct thread *td,
2969 struct freebsd6_freebsd32_aio_read_args *uap)
2972 return (aio_aqueue(td, (struct aiocb *)uap->aiocbp, NULL, LIO_READ,
2973 &aiocb32_ops_osigevent));
2978 freebsd32_aio_read(struct thread *td, struct freebsd32_aio_read_args *uap)
2981 return (aio_aqueue(td, (struct aiocb *)uap->aiocbp, NULL, LIO_READ,
2986 freebsd32_aio_readv(struct thread *td, struct freebsd32_aio_readv_args *uap)
2989 return (aio_aqueue(td, (struct aiocb *)uap->aiocbp, NULL, LIO_READV,
2993 #ifdef COMPAT_FREEBSD6
2995 freebsd6_freebsd32_aio_write(struct thread *td,
2996 struct freebsd6_freebsd32_aio_write_args *uap)
2999 return (aio_aqueue(td, (struct aiocb *)uap->aiocbp, NULL, LIO_WRITE,
3000 &aiocb32_ops_osigevent));
3005 freebsd32_aio_write(struct thread *td, struct freebsd32_aio_write_args *uap)
3008 return (aio_aqueue(td, (struct aiocb *)uap->aiocbp, NULL, LIO_WRITE,
3013 freebsd32_aio_writev(struct thread *td, struct freebsd32_aio_writev_args *uap)
3016 return (aio_aqueue(td, (struct aiocb *)uap->aiocbp, NULL, LIO_WRITEV,
3021 freebsd32_aio_mlock(struct thread *td, struct freebsd32_aio_mlock_args *uap)
3024 return (aio_aqueue(td, (struct aiocb *)uap->aiocbp, NULL, LIO_MLOCK,
3029 freebsd32_aio_waitcomplete(struct thread *td,
3030 struct freebsd32_aio_waitcomplete_args *uap)
3032 struct timespec32 ts32;
3033 struct timespec ts, *tsp;
3037 /* Get timespec struct. */
3038 error = copyin(uap->timeout, &ts32, sizeof(ts32));
3041 CP(ts32, ts, tv_sec);
3042 CP(ts32, ts, tv_nsec);
3047 return (kern_aio_waitcomplete(td, (struct aiocb **)uap->aiocbp, tsp,
3052 freebsd32_aio_fsync(struct thread *td, struct freebsd32_aio_fsync_args *uap)
3055 return (kern_aio_fsync(td, uap->op, (struct aiocb *)uap->aiocbp,
3059 #ifdef COMPAT_FREEBSD6
3061 freebsd6_freebsd32_lio_listio(struct thread *td,
3062 struct freebsd6_freebsd32_lio_listio_args *uap)
3064 struct aiocb **acb_list;
3065 struct sigevent *sigp, sig;
3066 struct osigevent32 osig;
3067 uint32_t *acb_list32;
3070 if ((uap->mode != LIO_NOWAIT) && (uap->mode != LIO_WAIT))
3074 if (nent < 0 || nent > max_aio_queue_per_proc)
3077 if (uap->sig && (uap->mode == LIO_NOWAIT)) {
3078 error = copyin(uap->sig, &osig, sizeof(osig));
3081 error = convert_old_sigevent32(&osig, &sig);
3088 acb_list32 = malloc(sizeof(uint32_t) * nent, M_LIO, M_WAITOK);
3089 error = copyin(uap->acb_list, acb_list32, nent * sizeof(uint32_t));
3091 free(acb_list32, M_LIO);
3094 acb_list = malloc(sizeof(struct aiocb *) * nent, M_LIO, M_WAITOK);
3095 for (i = 0; i < nent; i++)
3096 acb_list[i] = PTRIN(acb_list32[i]);
3097 free(acb_list32, M_LIO);
3099 error = kern_lio_listio(td, uap->mode,
3100 (struct aiocb * const *)uap->acb_list, acb_list, nent, sigp,
3101 &aiocb32_ops_osigevent);
3102 free(acb_list, M_LIO);
3108 freebsd32_lio_listio(struct thread *td, struct freebsd32_lio_listio_args *uap)
3110 struct aiocb **acb_list;
3111 struct sigevent *sigp, sig;
3112 struct sigevent32 sig32;
3113 uint32_t *acb_list32;
3116 if ((uap->mode != LIO_NOWAIT) && (uap->mode != LIO_WAIT))
3120 if (nent < 0 || nent > max_aio_queue_per_proc)
3123 if (uap->sig && (uap->mode == LIO_NOWAIT)) {
3124 error = copyin(uap->sig, &sig32, sizeof(sig32));
3127 error = convert_sigevent32(&sig32, &sig);
3134 acb_list32 = malloc(sizeof(uint32_t) * nent, M_LIO, M_WAITOK);
3135 error = copyin(uap->acb_list, acb_list32, nent * sizeof(uint32_t));
3137 free(acb_list32, M_LIO);
3140 acb_list = malloc(sizeof(struct aiocb *) * nent, M_LIO, M_WAITOK);
3141 for (i = 0; i < nent; i++)
3142 acb_list[i] = PTRIN(acb_list32[i]);
3143 free(acb_list32, M_LIO);
3145 error = kern_lio_listio(td, uap->mode,
3146 (struct aiocb * const *)uap->acb_list, acb_list, nent, sigp,
3148 free(acb_list, M_LIO);