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, 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) listio flags */
252 int lioj_finished_count; /* (a) listio flags */
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 (*copyin)(struct aiocb *ujob, struct aiocb *kjob);
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 void aio_init_aioinfo(struct proc *p);
311 static int aio_onceonly(void);
312 static int aio_free_entry(struct kaiocb *job);
313 static void aio_process_rw(struct kaiocb *job);
314 static void aio_process_sync(struct kaiocb *job);
315 static void aio_process_mlock(struct kaiocb *job);
316 static void aio_schedule_fsync(void *context, int pending);
317 static int aio_newproc(int *);
318 int aio_aqueue(struct thread *td, struct aiocb *ujob,
319 struct aioliojob *lio, int type, struct aiocb_ops *ops);
320 static int aio_queue_file(struct file *fp, struct kaiocb *job);
321 static void aio_biowakeup(struct bio *bp);
322 static void aio_proc_rundown(void *arg, struct proc *p);
323 static void aio_proc_rundown_exec(void *arg, struct proc *p,
324 struct image_params *imgp);
325 static int aio_qbio(struct proc *p, struct kaiocb *job);
326 static void aio_daemon(void *param);
327 static void aio_bio_done_notify(struct proc *userp, struct kaiocb *job);
328 static bool aio_clear_cancel_function_locked(struct kaiocb *job);
329 static int aio_kick(struct proc *userp);
330 static void aio_kick_nowait(struct proc *userp);
331 static void aio_kick_helper(void *context, int pending);
332 static int filt_aioattach(struct knote *kn);
333 static void filt_aiodetach(struct knote *kn);
334 static int filt_aio(struct knote *kn, long hint);
335 static int filt_lioattach(struct knote *kn);
336 static void filt_liodetach(struct knote *kn);
337 static int filt_lio(struct knote *kn, long hint);
341 * kaio Per process async io info
342 * aiop async io process data
343 * aiocb async io jobs
344 * aiolio list io jobs
346 static uma_zone_t kaio_zone, aiop_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, UMA_ZONE_NOFREE);
416 aiop_zone = uma_zcreate("AIOP", sizeof(struct aioproc), NULL,
417 NULL, NULL, NULL, UMA_ALIGN_PTR, UMA_ZONE_NOFREE);
418 aiocb_zone = uma_zcreate("AIOCB", sizeof(struct kaiocb), NULL, NULL,
419 NULL, NULL, UMA_ALIGN_PTR, UMA_ZONE_NOFREE);
420 aiolio_zone = uma_zcreate("AIOLIO", sizeof(struct aioliojob), NULL,
421 NULL, NULL, NULL, UMA_ALIGN_PTR, UMA_ZONE_NOFREE);
422 aiod_lifetime = AIOD_LIFETIME_DEFAULT;
424 p31b_setcfg(CTL_P1003_1B_ASYNCHRONOUS_IO, _POSIX_ASYNCHRONOUS_IO);
425 p31b_setcfg(CTL_P1003_1B_AIO_MAX, MAX_AIO_QUEUE);
426 p31b_setcfg(CTL_P1003_1B_AIO_PRIO_DELTA_MAX, 0);
432 * Init the per-process aioinfo structure. The aioinfo limits are set
433 * per-process for user limit (resource) management.
436 aio_init_aioinfo(struct proc *p)
440 ki = uma_zalloc(kaio_zone, M_WAITOK);
441 mtx_init(&ki->kaio_mtx, "aiomtx", NULL, MTX_DEF | MTX_NEW);
443 ki->kaio_active_count = 0;
445 ki->kaio_buffer_count = 0;
446 TAILQ_INIT(&ki->kaio_all);
447 TAILQ_INIT(&ki->kaio_done);
448 TAILQ_INIT(&ki->kaio_jobqueue);
449 TAILQ_INIT(&ki->kaio_liojoblist);
450 TAILQ_INIT(&ki->kaio_syncqueue);
451 TAILQ_INIT(&ki->kaio_syncready);
452 TASK_INIT(&ki->kaio_task, 0, aio_kick_helper, p);
453 TASK_INIT(&ki->kaio_sync_task, 0, aio_schedule_fsync, ki);
455 if (p->p_aioinfo == NULL) {
460 mtx_destroy(&ki->kaio_mtx);
461 uma_zfree(kaio_zone, ki);
464 while (num_aio_procs < MIN(target_aio_procs, max_aio_procs))
469 aio_sendsig(struct proc *p, struct sigevent *sigev, ksiginfo_t *ksi)
474 error = sigev_findtd(p, sigev, &td);
478 ksiginfo_set_sigev(ksi, sigev);
479 ksi->ksi_code = SI_ASYNCIO;
480 ksi->ksi_flags |= KSI_EXT | KSI_INS;
481 tdsendsignal(p, td, ksi->ksi_signo, ksi);
488 * Free a job entry. Wait for completion if it is currently active, but don't
489 * delay forever. If we delay, we return a flag that says that we have to
490 * restart the queue scan.
493 aio_free_entry(struct kaiocb *job)
496 struct aioliojob *lj;
504 AIO_LOCK_ASSERT(ki, MA_OWNED);
505 MPASS(job->jobflags & KAIOCB_FINISHED);
507 atomic_subtract_int(&num_queue_count, 1);
510 MPASS(ki->kaio_count >= 0);
512 TAILQ_REMOVE(&ki->kaio_done, job, plist);
513 TAILQ_REMOVE(&ki->kaio_all, job, allist);
518 lj->lioj_finished_count--;
520 if (lj->lioj_count == 0) {
521 TAILQ_REMOVE(&ki->kaio_liojoblist, lj, lioj_list);
522 /* lio is going away, we need to destroy any knotes */
523 knlist_delete(&lj->klist, curthread, 1);
525 sigqueue_take(&lj->lioj_ksi);
527 uma_zfree(aiolio_zone, lj);
531 /* job is going away, we need to destroy any knotes */
532 knlist_delete(&job->klist, curthread, 1);
534 sigqueue_take(&job->ksi);
540 * The thread argument here is used to find the owning process
541 * and is also passed to fo_close() which may pass it to various
542 * places such as devsw close() routines. Because of that, we
543 * need a thread pointer from the process owning the job that is
544 * persistent and won't disappear out from under us or move to
547 * Currently, all the callers of this function call it to remove
548 * a kaiocb from the current process' job list either via a
549 * syscall or due to the current process calling exit() or
550 * execve(). Thus, we know that p == curproc. We also know that
551 * curthread can't exit since we are curthread.
553 * Therefore, we use curthread as the thread to pass to
554 * knlist_delete(). This does mean that it is possible for the
555 * thread pointer at close time to differ from the thread pointer
556 * at open time, but this is already true of file descriptors in
557 * a multithreaded process.
560 fdrop(job->fd_file, curthread);
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 syscall.
721 aio_fsync_vnode(struct thread *td, struct vnode *vp)
726 if ((error = vn_start_write(vp, &mp, V_WAIT | PCATCH)) != 0)
728 vn_lock(vp, LK_EXCLUSIVE | LK_RETRY);
729 if (vp->v_object != NULL) {
730 VM_OBJECT_WLOCK(vp->v_object);
731 vm_object_page_clean(vp->v_object, 0, 0, 0);
732 VM_OBJECT_WUNLOCK(vp->v_object);
734 error = VOP_FSYNC(vp, MNT_WAIT, td);
737 vn_finished_write(mp);
743 * The AIO processing activity for LIO_READ/LIO_WRITE. This is the code that
744 * does the I/O request for the non-bio version of the operations. The normal
745 * vn operations are used, and this code should work in all instances for every
746 * type of file, including pipes, sockets, fifos, and regular files.
748 * XXX I don't think it works well for socket, pipe, and fifo.
751 aio_process_rw(struct kaiocb *job)
753 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_WRITE,
768 ("%s: opcode %d", __func__, job->uaiocb.aio_lio_opcode));
770 aio_switch_vmspace(job);
772 td_savedcred = td->td_ucred;
773 td->td_ucred = job->cred;
777 aiov.iov_base = (void *)(uintptr_t)cb->aio_buf;
778 aiov.iov_len = cb->aio_nbytes;
780 auio.uio_iov = &aiov;
782 auio.uio_offset = cb->aio_offset;
783 auio.uio_resid = cb->aio_nbytes;
784 cnt = cb->aio_nbytes;
785 auio.uio_segflg = UIO_USERSPACE;
788 msgrcv_st = td->td_ru.ru_msgrcv;
789 msgsnd_st = td->td_ru.ru_msgsnd;
790 inblock_st = td->td_ru.ru_inblock;
791 oublock_st = td->td_ru.ru_oublock;
794 * aio_aqueue() acquires a reference to the file that is
795 * released in aio_free_entry().
797 if (cb->aio_lio_opcode == LIO_READ) {
798 auio.uio_rw = UIO_READ;
799 if (auio.uio_resid == 0)
802 error = fo_read(fp, &auio, fp->f_cred, FOF_OFFSET, td);
804 if (fp->f_type == DTYPE_VNODE)
806 auio.uio_rw = UIO_WRITE;
807 error = fo_write(fp, &auio, fp->f_cred, FOF_OFFSET, td);
809 msgrcv_end = td->td_ru.ru_msgrcv;
810 msgsnd_end = td->td_ru.ru_msgsnd;
811 inblock_end = td->td_ru.ru_inblock;
812 oublock_end = td->td_ru.ru_oublock;
814 job->msgrcv = msgrcv_end - msgrcv_st;
815 job->msgsnd = msgsnd_end - msgsnd_st;
816 job->inblock = inblock_end - inblock_st;
817 job->outblock = oublock_end - oublock_st;
819 if ((error) && (auio.uio_resid != cnt)) {
820 if (error == ERESTART || error == EINTR || error == EWOULDBLOCK)
822 if ((error == EPIPE) && (cb->aio_lio_opcode == LIO_WRITE)) {
823 PROC_LOCK(job->userproc);
824 kern_psignal(job->userproc, SIGPIPE);
825 PROC_UNLOCK(job->userproc);
829 cnt -= auio.uio_resid;
830 td->td_ucred = td_savedcred;
832 aio_complete(job, -1, error);
834 aio_complete(job, cnt, 0);
838 aio_process_sync(struct kaiocb *job)
840 struct thread *td = curthread;
841 struct ucred *td_savedcred = td->td_ucred;
842 struct file *fp = job->fd_file;
845 KASSERT(job->uaiocb.aio_lio_opcode == LIO_SYNC,
846 ("%s: opcode %d", __func__, job->uaiocb.aio_lio_opcode));
848 td->td_ucred = job->cred;
849 if (fp->f_vnode != NULL)
850 error = aio_fsync_vnode(td, fp->f_vnode);
851 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);
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);
913 lj->lioj_flags |= LIOJ_SIGNAL_POSTED;
918 if (job->jobflags & KAIOCB_CHECKSYNC) {
919 schedule_fsync = false;
920 TAILQ_FOREACH_SAFE(sjob, &ki->kaio_syncqueue, list, sjobn) {
921 if (job->fd_file != sjob->fd_file ||
922 job->seqno >= sjob->seqno)
924 if (--sjob->pending > 0)
926 TAILQ_REMOVE(&ki->kaio_syncqueue, sjob, list);
927 if (!aio_clear_cancel_function_locked(sjob))
929 TAILQ_INSERT_TAIL(&ki->kaio_syncready, sjob, list);
930 schedule_fsync = true;
933 taskqueue_enqueue(taskqueue_aiod_kick,
934 &ki->kaio_sync_task);
936 if (ki->kaio_flags & KAIO_WAKEUP) {
937 ki->kaio_flags &= ~KAIO_WAKEUP;
938 wakeup(&userp->p_aioinfo);
943 aio_schedule_fsync(void *context, int pending)
950 while (!TAILQ_EMPTY(&ki->kaio_syncready)) {
951 job = TAILQ_FIRST(&ki->kaio_syncready);
952 TAILQ_REMOVE(&ki->kaio_syncready, job, list);
954 aio_schedule(job, aio_process_sync);
961 aio_cancel_cleared(struct kaiocb *job)
965 * The caller should hold the same queue lock held when
966 * aio_clear_cancel_function() was called and set this flag
967 * ensuring this check sees an up-to-date value. However,
968 * there is no way to assert that.
970 return ((job->jobflags & KAIOCB_CLEARED) != 0);
974 aio_clear_cancel_function_locked(struct kaiocb *job)
977 AIO_LOCK_ASSERT(job->userproc->p_aioinfo, MA_OWNED);
978 MPASS(job->cancel_fn != NULL);
979 if (job->jobflags & KAIOCB_CANCELLING) {
980 job->jobflags |= KAIOCB_CLEARED;
983 job->cancel_fn = NULL;
988 aio_clear_cancel_function(struct kaiocb *job)
993 ki = job->userproc->p_aioinfo;
995 ret = aio_clear_cancel_function_locked(job);
1001 aio_set_cancel_function_locked(struct kaiocb *job, aio_cancel_fn_t *func)
1004 AIO_LOCK_ASSERT(job->userproc->p_aioinfo, MA_OWNED);
1005 if (job->jobflags & KAIOCB_CANCELLED)
1007 job->cancel_fn = func;
1012 aio_set_cancel_function(struct kaiocb *job, aio_cancel_fn_t *func)
1014 struct kaioinfo *ki;
1017 ki = job->userproc->p_aioinfo;
1019 ret = aio_set_cancel_function_locked(job, func);
1025 aio_complete(struct kaiocb *job, long status, int error)
1027 struct kaioinfo *ki;
1030 job->uaiocb._aiocb_private.error = error;
1031 job->uaiocb._aiocb_private.status = status;
1033 userp = job->userproc;
1034 ki = userp->p_aioinfo;
1037 KASSERT(!(job->jobflags & KAIOCB_FINISHED),
1038 ("duplicate aio_complete"));
1039 job->jobflags |= KAIOCB_FINISHED;
1040 if ((job->jobflags & (KAIOCB_QUEUEING | KAIOCB_CANCELLING)) == 0) {
1041 TAILQ_REMOVE(&ki->kaio_jobqueue, job, plist);
1042 aio_bio_done_notify(userp, job);
1048 aio_cancel(struct kaiocb *job)
1051 aio_complete(job, -1, ECANCELED);
1055 aio_switch_vmspace(struct kaiocb *job)
1058 vmspace_switch_aio(job->userproc->p_vmspace);
1062 * The AIO daemon, most of the actual work is done in aio_process_*,
1063 * but the setup (and address space mgmt) is done in this routine.
1066 aio_daemon(void *_id)
1069 struct aioproc *aiop;
1070 struct kaioinfo *ki;
1072 struct vmspace *myvm;
1073 struct thread *td = curthread;
1074 int id = (intptr_t)_id;
1077 * Grab an extra reference on the daemon's vmspace so that it
1078 * doesn't get freed by jobs that switch to a different
1082 myvm = vmspace_acquire_ref(p);
1084 KASSERT(p->p_textvp == NULL, ("kthread has a textvp"));
1087 * Allocate and ready the aio control info. There is one aiop structure
1090 aiop = uma_zalloc(aiop_zone, M_WAITOK);
1092 aiop->aioprocflags = 0;
1095 * Wakeup parent process. (Parent sleeps to keep from blasting away
1096 * and creating too many daemons.)
1098 sema_post(&aio_newproc_sem);
1100 mtx_lock(&aio_job_mtx);
1103 * Take daemon off of free queue
1105 if (aiop->aioprocflags & AIOP_FREE) {
1106 TAILQ_REMOVE(&aio_freeproc, aiop, list);
1107 aiop->aioprocflags &= ~AIOP_FREE;
1113 while ((job = aio_selectjob(aiop)) != NULL) {
1114 mtx_unlock(&aio_job_mtx);
1116 ki = job->userproc->p_aioinfo;
1117 job->handle_fn(job);
1119 mtx_lock(&aio_job_mtx);
1120 /* Decrement the active job count. */
1121 ki->kaio_active_count--;
1125 * Disconnect from user address space.
1127 if (p->p_vmspace != myvm) {
1128 mtx_unlock(&aio_job_mtx);
1129 vmspace_switch_aio(myvm);
1130 mtx_lock(&aio_job_mtx);
1132 * We have to restart to avoid race, we only sleep if
1133 * no job can be selected.
1138 mtx_assert(&aio_job_mtx, MA_OWNED);
1140 TAILQ_INSERT_HEAD(&aio_freeproc, aiop, list);
1141 aiop->aioprocflags |= AIOP_FREE;
1144 * If daemon is inactive for a long time, allow it to exit,
1145 * thereby freeing resources.
1147 if (msleep(p, &aio_job_mtx, PRIBIO, "aiordy",
1148 aiod_lifetime) == EWOULDBLOCK && TAILQ_EMPTY(&aio_jobs) &&
1149 (aiop->aioprocflags & AIOP_FREE) &&
1150 num_aio_procs > target_aio_procs)
1153 TAILQ_REMOVE(&aio_freeproc, aiop, list);
1155 mtx_unlock(&aio_job_mtx);
1156 uma_zfree(aiop_zone, aiop);
1157 free_unr(aiod_unr, id);
1160 KASSERT(p->p_vmspace == myvm,
1161 ("AIOD: bad vmspace for exiting daemon"));
1162 KASSERT(myvm->vm_refcnt > 1,
1163 ("AIOD: bad vm refcnt for exiting daemon: %d", myvm->vm_refcnt));
1168 * Create a new AIO daemon. This is mostly a kernel-thread fork routine. The
1169 * AIO daemon modifies its environment itself.
1172 aio_newproc(int *start)
1178 id = alloc_unr(aiod_unr);
1179 error = kproc_create(aio_daemon, (void *)(intptr_t)id, &p,
1180 RFNOWAIT, 0, "aiod%d", id);
1183 * Wait until daemon is started.
1185 sema_wait(&aio_newproc_sem);
1186 mtx_lock(&aio_job_mtx);
1190 mtx_unlock(&aio_job_mtx);
1192 free_unr(aiod_unr, id);
1198 * Try the high-performance, low-overhead bio method for eligible
1199 * VCHR devices. This method doesn't use an aio helper thread, and
1200 * thus has very low overhead.
1202 * Assumes that the caller, aio_aqueue(), has incremented the file
1203 * structure's reference count, preventing its deallocation for the
1204 * duration of this call.
1207 aio_qbio(struct proc *p, struct kaiocb *job)
1216 struct kaioinfo *ki;
1217 int error, ref, poff;
1223 if (!(cb->aio_lio_opcode == LIO_WRITE ||
1224 cb->aio_lio_opcode == LIO_READ))
1226 if (fp == NULL || fp->f_type != DTYPE_VNODE)
1230 if (vp->v_type != VCHR)
1232 if (vp->v_bufobj.bo_bsize == 0)
1234 if (cb->aio_nbytes % vp->v_bufobj.bo_bsize)
1238 csw = devvn_refthread(vp, &dev, &ref);
1242 if ((csw->d_flags & D_DISK) == 0) {
1246 if (cb->aio_nbytes > dev->si_iosize_max) {
1252 poff = (vm_offset_t)cb->aio_buf & PAGE_MASK;
1253 if ((dev->si_flags & SI_UNMAPPED) && unmapped_buf_allowed) {
1254 if (cb->aio_nbytes > MAXPHYS) {
1261 if (cb->aio_nbytes > MAXPHYS - poff) {
1265 if (ki->kaio_buffer_count >= max_buf_aio) {
1270 job->pbuf = pbuf = uma_zalloc(pbuf_zone, M_WAITOK);
1273 ki->kaio_buffer_count++;
1276 job->bp = bp = g_alloc_bio();
1278 bp->bio_length = cb->aio_nbytes;
1279 bp->bio_bcount = cb->aio_nbytes;
1280 bp->bio_done = aio_biowakeup;
1281 bp->bio_data = (void *)(uintptr_t)cb->aio_buf;
1282 bp->bio_offset = cb->aio_offset;
1283 bp->bio_cmd = cb->aio_lio_opcode == LIO_WRITE ? BIO_WRITE : BIO_READ;
1285 bp->bio_caller1 = (void *)job;
1287 prot = VM_PROT_READ;
1288 if (cb->aio_lio_opcode == LIO_READ)
1289 prot |= VM_PROT_WRITE; /* Less backwards than it looks */
1290 job->npages = vm_fault_quick_hold_pages(&curproc->p_vmspace->vm_map,
1291 (vm_offset_t)bp->bio_data, bp->bio_length, prot, job->pages,
1292 nitems(job->pages));
1293 if (job->npages < 0) {
1298 pmap_qenter((vm_offset_t)pbuf->b_data,
1299 job->pages, job->npages);
1300 bp->bio_data = pbuf->b_data + poff;
1301 atomic_add_int(&num_buf_aio, 1);
1303 bp->bio_ma = job->pages;
1304 bp->bio_ma_n = job->npages;
1305 bp->bio_ma_offset = poff;
1306 bp->bio_data = unmapped_buf;
1307 bp->bio_flags |= BIO_UNMAPPED;
1308 atomic_add_int(&num_unmapped_aio, 1);
1311 /* Perform transfer. */
1312 csw->d_strategy(bp);
1313 dev_relthread(dev, ref);
1319 ki->kaio_buffer_count--;
1321 uma_zfree(pbuf_zone, pbuf);
1327 dev_relthread(dev, ref);
1331 #ifdef COMPAT_FREEBSD6
1333 convert_old_sigevent(struct osigevent *osig, struct sigevent *nsig)
1337 * Only SIGEV_NONE, SIGEV_SIGNAL, and SIGEV_KEVENT are
1338 * supported by AIO with the old sigevent structure.
1340 nsig->sigev_notify = osig->sigev_notify;
1341 switch (nsig->sigev_notify) {
1345 nsig->sigev_signo = osig->__sigev_u.__sigev_signo;
1348 nsig->sigev_notify_kqueue =
1349 osig->__sigev_u.__sigev_notify_kqueue;
1350 nsig->sigev_value.sival_ptr = osig->sigev_value.sival_ptr;
1359 aiocb_copyin_old_sigevent(struct aiocb *ujob, struct aiocb *kjob)
1361 struct oaiocb *ojob;
1364 bzero(kjob, sizeof(struct aiocb));
1365 error = copyin(ujob, kjob, sizeof(struct oaiocb));
1368 ojob = (struct oaiocb *)kjob;
1369 return (convert_old_sigevent(&ojob->aio_sigevent, &kjob->aio_sigevent));
1374 aiocb_copyin(struct aiocb *ujob, struct aiocb *kjob)
1377 return (copyin(ujob, kjob, sizeof(struct aiocb)));
1381 aiocb_fetch_status(struct aiocb *ujob)
1384 return (fuword(&ujob->_aiocb_private.status));
1388 aiocb_fetch_error(struct aiocb *ujob)
1391 return (fuword(&ujob->_aiocb_private.error));
1395 aiocb_store_status(struct aiocb *ujob, long status)
1398 return (suword(&ujob->_aiocb_private.status, status));
1402 aiocb_store_error(struct aiocb *ujob, long error)
1405 return (suword(&ujob->_aiocb_private.error, error));
1409 aiocb_store_kernelinfo(struct aiocb *ujob, long jobref)
1412 return (suword(&ujob->_aiocb_private.kernelinfo, jobref));
1416 aiocb_store_aiocb(struct aiocb **ujobp, struct aiocb *ujob)
1419 return (suword(ujobp, (long)ujob));
1422 static struct aiocb_ops aiocb_ops = {
1423 .copyin = aiocb_copyin,
1424 .fetch_status = aiocb_fetch_status,
1425 .fetch_error = aiocb_fetch_error,
1426 .store_status = aiocb_store_status,
1427 .store_error = aiocb_store_error,
1428 .store_kernelinfo = aiocb_store_kernelinfo,
1429 .store_aiocb = aiocb_store_aiocb,
1432 #ifdef COMPAT_FREEBSD6
1433 static struct aiocb_ops aiocb_ops_osigevent = {
1434 .copyin = aiocb_copyin_old_sigevent,
1435 .fetch_status = aiocb_fetch_status,
1436 .fetch_error = aiocb_fetch_error,
1437 .store_status = aiocb_store_status,
1438 .store_error = aiocb_store_error,
1439 .store_kernelinfo = aiocb_store_kernelinfo,
1440 .store_aiocb = aiocb_store_aiocb,
1445 * Queue a new AIO request. Choosing either the threaded or direct bio VCHR
1446 * technique is done in this code.
1449 aio_aqueue(struct thread *td, struct aiocb *ujob, struct aioliojob *lj,
1450 int type, struct aiocb_ops *ops)
1452 struct proc *p = td->td_proc;
1455 struct kaioinfo *ki;
1463 if (p->p_aioinfo == NULL)
1464 aio_init_aioinfo(p);
1468 ops->store_status(ujob, -1);
1469 ops->store_error(ujob, 0);
1470 ops->store_kernelinfo(ujob, -1);
1472 if (num_queue_count >= max_queue_count ||
1473 ki->kaio_count >= max_aio_queue_per_proc) {
1474 ops->store_error(ujob, EAGAIN);
1478 job = uma_zalloc(aiocb_zone, M_WAITOK | M_ZERO);
1479 knlist_init_mtx(&job->klist, AIO_MTX(ki));
1481 error = ops->copyin(ujob, &job->uaiocb);
1483 ops->store_error(ujob, error);
1484 uma_zfree(aiocb_zone, job);
1488 if (job->uaiocb.aio_nbytes > IOSIZE_MAX) {
1489 uma_zfree(aiocb_zone, job);
1493 if (job->uaiocb.aio_sigevent.sigev_notify != SIGEV_KEVENT &&
1494 job->uaiocb.aio_sigevent.sigev_notify != SIGEV_SIGNAL &&
1495 job->uaiocb.aio_sigevent.sigev_notify != SIGEV_THREAD_ID &&
1496 job->uaiocb.aio_sigevent.sigev_notify != SIGEV_NONE) {
1497 ops->store_error(ujob, EINVAL);
1498 uma_zfree(aiocb_zone, job);
1502 if ((job->uaiocb.aio_sigevent.sigev_notify == SIGEV_SIGNAL ||
1503 job->uaiocb.aio_sigevent.sigev_notify == SIGEV_THREAD_ID) &&
1504 !_SIG_VALID(job->uaiocb.aio_sigevent.sigev_signo)) {
1505 uma_zfree(aiocb_zone, job);
1509 ksiginfo_init(&job->ksi);
1511 /* Save userspace address of the job info. */
1514 /* Get the opcode. */
1515 if (type != LIO_NOP)
1516 job->uaiocb.aio_lio_opcode = type;
1517 opcode = job->uaiocb.aio_lio_opcode;
1520 * Validate the opcode and fetch the file object for the specified
1523 * XXXRW: Moved the opcode validation up here so that we don't
1524 * retrieve a file descriptor without knowing what the capabiltity
1527 fd = job->uaiocb.aio_fildes;
1530 error = fget_write(td, fd, &cap_pwrite_rights, &fp);
1533 error = fget_read(td, fd, &cap_pread_rights, &fp);
1536 error = fget(td, fd, &cap_fsync_rights, &fp);
1542 error = fget(td, fd, &cap_no_rights, &fp);
1548 uma_zfree(aiocb_zone, job);
1549 ops->store_error(ujob, error);
1553 if (opcode == LIO_SYNC && fp->f_vnode == NULL) {
1558 if ((opcode == LIO_READ || opcode == LIO_WRITE) &&
1559 job->uaiocb.aio_offset < 0 &&
1560 (fp->f_vnode == NULL || fp->f_vnode->v_type != VCHR)) {
1567 mtx_lock(&aio_job_mtx);
1569 job->seqno = jobseqno++;
1570 mtx_unlock(&aio_job_mtx);
1571 error = ops->store_kernelinfo(ujob, jid);
1576 job->uaiocb._aiocb_private.kernelinfo = (void *)(intptr_t)jid;
1578 if (opcode == LIO_NOP) {
1580 uma_zfree(aiocb_zone, job);
1584 if (job->uaiocb.aio_sigevent.sigev_notify != SIGEV_KEVENT)
1586 evflags = job->uaiocb.aio_sigevent.sigev_notify_kevent_flags;
1587 if ((evflags & ~(EV_CLEAR | EV_DISPATCH | EV_ONESHOT)) != 0) {
1591 kqfd = job->uaiocb.aio_sigevent.sigev_notify_kqueue;
1592 memset(&kev, 0, sizeof(kev));
1593 kev.ident = (uintptr_t)job->ujob;
1594 kev.filter = EVFILT_AIO;
1595 kev.flags = EV_ADD | EV_ENABLE | EV_FLAG1 | evflags;
1596 kev.data = (intptr_t)job;
1597 kev.udata = job->uaiocb.aio_sigevent.sigev_value.sival_ptr;
1598 error = kqfd_register(kqfd, &kev, td, M_WAITOK);
1604 ops->store_error(ujob, EINPROGRESS);
1605 job->uaiocb._aiocb_private.error = EINPROGRESS;
1607 job->cred = crhold(td->td_ucred);
1608 job->jobflags = KAIOCB_QUEUEING;
1611 if (opcode == LIO_MLOCK) {
1612 aio_schedule(job, aio_process_mlock);
1614 } else if (fp->f_ops->fo_aio_queue == NULL)
1615 error = aio_queue_file(fp, job);
1617 error = fo_aio_queue(fp, job);
1622 job->jobflags &= ~KAIOCB_QUEUEING;
1623 TAILQ_INSERT_TAIL(&ki->kaio_all, job, allist);
1627 atomic_add_int(&num_queue_count, 1);
1628 if (job->jobflags & KAIOCB_FINISHED) {
1630 * The queue callback completed the request synchronously.
1631 * The bulk of the completion is deferred in that case
1634 aio_bio_done_notify(p, job);
1636 TAILQ_INSERT_TAIL(&ki->kaio_jobqueue, job, plist);
1641 knlist_delete(&job->klist, curthread, 0);
1644 uma_zfree(aiocb_zone, job);
1645 ops->store_error(ujob, error);
1650 aio_cancel_daemon_job(struct kaiocb *job)
1653 mtx_lock(&aio_job_mtx);
1654 if (!aio_cancel_cleared(job))
1655 TAILQ_REMOVE(&aio_jobs, job, list);
1656 mtx_unlock(&aio_job_mtx);
1661 aio_schedule(struct kaiocb *job, aio_handle_fn_t *func)
1664 mtx_lock(&aio_job_mtx);
1665 if (!aio_set_cancel_function(job, aio_cancel_daemon_job)) {
1666 mtx_unlock(&aio_job_mtx);
1670 job->handle_fn = func;
1671 TAILQ_INSERT_TAIL(&aio_jobs, job, list);
1672 aio_kick_nowait(job->userproc);
1673 mtx_unlock(&aio_job_mtx);
1677 aio_cancel_sync(struct kaiocb *job)
1679 struct kaioinfo *ki;
1681 ki = job->userproc->p_aioinfo;
1683 if (!aio_cancel_cleared(job))
1684 TAILQ_REMOVE(&ki->kaio_syncqueue, job, list);
1690 aio_queue_file(struct file *fp, struct kaiocb *job)
1692 struct kaioinfo *ki;
1693 struct kaiocb *job2;
1699 ki = job->userproc->p_aioinfo;
1700 error = aio_qbio(job->userproc, job);
1704 if (fp->f_type == DTYPE_VNODE) {
1706 if (vp->v_type == VREG || vp->v_type == VDIR) {
1707 mp = fp->f_vnode->v_mount;
1708 if (mp == NULL || (mp->mnt_flag & MNT_LOCAL) != 0)
1712 if (!(safe || enable_aio_unsafe)) {
1713 counted_warning(&unsafe_warningcnt,
1714 "is attempting to use unsafe AIO requests");
1715 return (EOPNOTSUPP);
1718 switch (job->uaiocb.aio_lio_opcode) {
1721 aio_schedule(job, aio_process_rw);
1726 TAILQ_FOREACH(job2, &ki->kaio_jobqueue, plist) {
1727 if (job2->fd_file == job->fd_file &&
1728 job2->uaiocb.aio_lio_opcode != LIO_SYNC &&
1729 job2->seqno < job->seqno) {
1730 job2->jobflags |= KAIOCB_CHECKSYNC;
1734 if (job->pending != 0) {
1735 if (!aio_set_cancel_function_locked(job,
1741 TAILQ_INSERT_TAIL(&ki->kaio_syncqueue, job, list);
1746 aio_schedule(job, aio_process_sync);
1756 aio_kick_nowait(struct proc *userp)
1758 struct kaioinfo *ki = userp->p_aioinfo;
1759 struct aioproc *aiop;
1761 mtx_assert(&aio_job_mtx, MA_OWNED);
1762 if ((aiop = TAILQ_FIRST(&aio_freeproc)) != NULL) {
1763 TAILQ_REMOVE(&aio_freeproc, aiop, list);
1764 aiop->aioprocflags &= ~AIOP_FREE;
1765 wakeup(aiop->aioproc);
1766 } else if (num_aio_resv_start + num_aio_procs < max_aio_procs &&
1767 ki->kaio_active_count + num_aio_resv_start < max_aio_per_proc) {
1768 taskqueue_enqueue(taskqueue_aiod_kick, &ki->kaio_task);
1773 aio_kick(struct proc *userp)
1775 struct kaioinfo *ki = userp->p_aioinfo;
1776 struct aioproc *aiop;
1779 mtx_assert(&aio_job_mtx, MA_OWNED);
1781 if ((aiop = TAILQ_FIRST(&aio_freeproc)) != NULL) {
1782 TAILQ_REMOVE(&aio_freeproc, aiop, list);
1783 aiop->aioprocflags &= ~AIOP_FREE;
1784 wakeup(aiop->aioproc);
1785 } else if (num_aio_resv_start + num_aio_procs < max_aio_procs &&
1786 ki->kaio_active_count + num_aio_resv_start < max_aio_per_proc) {
1787 num_aio_resv_start++;
1788 mtx_unlock(&aio_job_mtx);
1789 error = aio_newproc(&num_aio_resv_start);
1790 mtx_lock(&aio_job_mtx);
1792 num_aio_resv_start--;
1802 aio_kick_helper(void *context, int pending)
1804 struct proc *userp = context;
1806 mtx_lock(&aio_job_mtx);
1807 while (--pending >= 0) {
1808 if (aio_kick(userp))
1811 mtx_unlock(&aio_job_mtx);
1815 * Support the aio_return system call, as a side-effect, kernel resources are
1819 kern_aio_return(struct thread *td, struct aiocb *ujob, struct aiocb_ops *ops)
1821 struct proc *p = td->td_proc;
1823 struct kaioinfo *ki;
1830 TAILQ_FOREACH(job, &ki->kaio_done, plist) {
1831 if (job->ujob == ujob)
1835 MPASS(job->jobflags & KAIOCB_FINISHED);
1836 status = job->uaiocb._aiocb_private.status;
1837 error = job->uaiocb._aiocb_private.error;
1838 td->td_retval[0] = status;
1839 td->td_ru.ru_oublock += job->outblock;
1840 td->td_ru.ru_inblock += job->inblock;
1841 td->td_ru.ru_msgsnd += job->msgsnd;
1842 td->td_ru.ru_msgrcv += job->msgrcv;
1843 aio_free_entry(job);
1845 ops->store_error(ujob, error);
1846 ops->store_status(ujob, status);
1855 sys_aio_return(struct thread *td, struct aio_return_args *uap)
1858 return (kern_aio_return(td, uap->aiocbp, &aiocb_ops));
1862 * Allow a process to wakeup when any of the I/O requests are completed.
1865 kern_aio_suspend(struct thread *td, int njoblist, struct aiocb **ujoblist,
1866 struct timespec *ts)
1868 struct proc *p = td->td_proc;
1870 struct kaioinfo *ki;
1871 struct kaiocb *firstjob, *job;
1876 if (ts->tv_nsec < 0 || ts->tv_nsec >= 1000000000)
1879 TIMESPEC_TO_TIMEVAL(&atv, ts);
1880 if (itimerfix(&atv))
1882 timo = tvtohz(&atv);
1896 TAILQ_FOREACH(job, &ki->kaio_all, allist) {
1897 for (i = 0; i < njoblist; i++) {
1898 if (job->ujob == ujoblist[i]) {
1899 if (firstjob == NULL)
1901 if (job->jobflags & KAIOCB_FINISHED)
1906 /* All tasks were finished. */
1907 if (firstjob == NULL)
1910 ki->kaio_flags |= KAIO_WAKEUP;
1911 error = msleep(&p->p_aioinfo, AIO_MTX(ki), PRIBIO | PCATCH,
1913 if (error == ERESTART)
1924 sys_aio_suspend(struct thread *td, struct aio_suspend_args *uap)
1926 struct timespec ts, *tsp;
1927 struct aiocb **ujoblist;
1930 if (uap->nent < 0 || uap->nent > max_aio_queue_per_proc)
1934 /* Get timespec struct. */
1935 if ((error = copyin(uap->timeout, &ts, sizeof(ts))) != 0)
1941 ujoblist = malloc(uap->nent * sizeof(ujoblist[0]), M_AIOS, M_WAITOK);
1942 error = copyin(uap->aiocbp, ujoblist, uap->nent * sizeof(ujoblist[0]));
1944 error = kern_aio_suspend(td, uap->nent, ujoblist, tsp);
1945 free(ujoblist, M_AIOS);
1950 * aio_cancel cancels any non-bio aio operations not currently in progress.
1953 sys_aio_cancel(struct thread *td, struct aio_cancel_args *uap)
1955 struct proc *p = td->td_proc;
1956 struct kaioinfo *ki;
1957 struct kaiocb *job, *jobn;
1961 int notcancelled = 0;
1964 /* Lookup file object. */
1965 error = fget(td, uap->fd, &cap_no_rights, &fp);
1973 if (fp->f_type == DTYPE_VNODE) {
1975 if (vn_isdisk(vp, &error)) {
1977 td->td_retval[0] = AIO_NOTCANCELED;
1983 TAILQ_FOREACH_SAFE(job, &ki->kaio_jobqueue, plist, jobn) {
1984 if ((uap->fd == job->uaiocb.aio_fildes) &&
1985 ((uap->aiocbp == NULL) ||
1986 (uap->aiocbp == job->ujob))) {
1987 if (aio_cancel_job(p, ki, job)) {
1992 if (uap->aiocbp != NULL)
2001 if (uap->aiocbp != NULL) {
2003 td->td_retval[0] = AIO_CANCELED;
2009 td->td_retval[0] = AIO_NOTCANCELED;
2014 td->td_retval[0] = AIO_CANCELED;
2018 td->td_retval[0] = AIO_ALLDONE;
2024 * aio_error is implemented in the kernel level for compatibility purposes
2025 * only. For a user mode async implementation, it would be best to do it in
2026 * a userland subroutine.
2029 kern_aio_error(struct thread *td, struct aiocb *ujob, struct aiocb_ops *ops)
2031 struct proc *p = td->td_proc;
2033 struct kaioinfo *ki;
2038 td->td_retval[0] = EINVAL;
2043 TAILQ_FOREACH(job, &ki->kaio_all, allist) {
2044 if (job->ujob == ujob) {
2045 if (job->jobflags & KAIOCB_FINISHED)
2047 job->uaiocb._aiocb_private.error;
2049 td->td_retval[0] = EINPROGRESS;
2057 * Hack for failure of aio_aqueue.
2059 status = ops->fetch_status(ujob);
2061 td->td_retval[0] = ops->fetch_error(ujob);
2065 td->td_retval[0] = EINVAL;
2070 sys_aio_error(struct thread *td, struct aio_error_args *uap)
2073 return (kern_aio_error(td, uap->aiocbp, &aiocb_ops));
2076 /* syscall - asynchronous read from a file (REALTIME) */
2077 #ifdef COMPAT_FREEBSD6
2079 freebsd6_aio_read(struct thread *td, struct freebsd6_aio_read_args *uap)
2082 return (aio_aqueue(td, (struct aiocb *)uap->aiocbp, NULL, LIO_READ,
2083 &aiocb_ops_osigevent));
2088 sys_aio_read(struct thread *td, struct aio_read_args *uap)
2091 return (aio_aqueue(td, uap->aiocbp, NULL, LIO_READ, &aiocb_ops));
2094 /* syscall - asynchronous write to a file (REALTIME) */
2095 #ifdef COMPAT_FREEBSD6
2097 freebsd6_aio_write(struct thread *td, struct freebsd6_aio_write_args *uap)
2100 return (aio_aqueue(td, (struct aiocb *)uap->aiocbp, NULL, LIO_WRITE,
2101 &aiocb_ops_osigevent));
2106 sys_aio_write(struct thread *td, struct aio_write_args *uap)
2109 return (aio_aqueue(td, uap->aiocbp, NULL, LIO_WRITE, &aiocb_ops));
2113 sys_aio_mlock(struct thread *td, struct aio_mlock_args *uap)
2116 return (aio_aqueue(td, uap->aiocbp, NULL, LIO_MLOCK, &aiocb_ops));
2120 kern_lio_listio(struct thread *td, int mode, struct aiocb * const *uacb_list,
2121 struct aiocb **acb_list, int nent, struct sigevent *sig,
2122 struct aiocb_ops *ops)
2124 struct proc *p = td->td_proc;
2126 struct kaioinfo *ki;
2127 struct aioliojob *lj;
2133 if ((mode != LIO_NOWAIT) && (mode != LIO_WAIT))
2136 if (nent < 0 || nent > max_aio_queue_per_proc)
2139 if (p->p_aioinfo == NULL)
2140 aio_init_aioinfo(p);
2144 lj = uma_zalloc(aiolio_zone, M_WAITOK);
2147 lj->lioj_finished_count = 0;
2148 knlist_init_mtx(&lj->klist, AIO_MTX(ki));
2149 ksiginfo_init(&lj->lioj_ksi);
2154 if (sig && (mode == LIO_NOWAIT)) {
2155 bcopy(sig, &lj->lioj_signal, sizeof(lj->lioj_signal));
2156 if (lj->lioj_signal.sigev_notify == SIGEV_KEVENT) {
2157 /* Assume only new style KEVENT */
2158 memset(&kev, 0, sizeof(kev));
2159 kev.filter = EVFILT_LIO;
2160 kev.flags = EV_ADD | EV_ENABLE | EV_FLAG1;
2161 kev.ident = (uintptr_t)uacb_list; /* something unique */
2162 kev.data = (intptr_t)lj;
2163 /* pass user defined sigval data */
2164 kev.udata = lj->lioj_signal.sigev_value.sival_ptr;
2165 error = kqfd_register(
2166 lj->lioj_signal.sigev_notify_kqueue, &kev, td,
2169 uma_zfree(aiolio_zone, lj);
2172 } else if (lj->lioj_signal.sigev_notify == SIGEV_NONE) {
2174 } else if (lj->lioj_signal.sigev_notify == SIGEV_SIGNAL ||
2175 lj->lioj_signal.sigev_notify == SIGEV_THREAD_ID) {
2176 if (!_SIG_VALID(lj->lioj_signal.sigev_signo)) {
2177 uma_zfree(aiolio_zone, lj);
2180 lj->lioj_flags |= LIOJ_SIGNAL;
2182 uma_zfree(aiolio_zone, lj);
2188 TAILQ_INSERT_TAIL(&ki->kaio_liojoblist, lj, lioj_list);
2190 * Add extra aiocb count to avoid the lio to be freed
2191 * by other threads doing aio_waitcomplete or aio_return,
2192 * and prevent event from being sent until we have queued
2199 * Get pointers to the list of I/O requests.
2203 for (i = 0; i < nent; i++) {
2206 error = aio_aqueue(td, job, lj, LIO_NOP, ops);
2207 if (error == EAGAIN)
2209 else if (error != 0)
2216 if (mode == LIO_WAIT) {
2217 while (lj->lioj_count - 1 != lj->lioj_finished_count) {
2218 ki->kaio_flags |= KAIO_WAKEUP;
2219 error = msleep(&p->p_aioinfo, AIO_MTX(ki),
2220 PRIBIO | PCATCH, "aiospn", 0);
2221 if (error == ERESTART)
2227 if (lj->lioj_count - 1 == lj->lioj_finished_count) {
2228 if (lj->lioj_signal.sigev_notify == SIGEV_KEVENT) {
2229 lj->lioj_flags |= LIOJ_KEVENT_POSTED;
2230 KNOTE_LOCKED(&lj->klist, 1);
2232 if ((lj->lioj_flags & (LIOJ_SIGNAL|LIOJ_SIGNAL_POSTED))
2234 && (lj->lioj_signal.sigev_notify == SIGEV_SIGNAL ||
2235 lj->lioj_signal.sigev_notify == SIGEV_THREAD_ID)) {
2236 aio_sendsig(p, &lj->lioj_signal,
2238 lj->lioj_flags |= LIOJ_SIGNAL_POSTED;
2243 if (lj->lioj_count == 0) {
2244 TAILQ_REMOVE(&ki->kaio_liojoblist, lj, lioj_list);
2245 knlist_delete(&lj->klist, curthread, 1);
2247 sigqueue_take(&lj->lioj_ksi);
2250 uma_zfree(aiolio_zone, lj);
2262 /* syscall - list directed I/O (REALTIME) */
2263 #ifdef COMPAT_FREEBSD6
2265 freebsd6_lio_listio(struct thread *td, struct freebsd6_lio_listio_args *uap)
2267 struct aiocb **acb_list;
2268 struct sigevent *sigp, sig;
2269 struct osigevent osig;
2272 if ((uap->mode != LIO_NOWAIT) && (uap->mode != LIO_WAIT))
2276 if (nent < 0 || nent > max_aio_queue_per_proc)
2279 if (uap->sig && (uap->mode == LIO_NOWAIT)) {
2280 error = copyin(uap->sig, &osig, sizeof(osig));
2283 error = convert_old_sigevent(&osig, &sig);
2290 acb_list = malloc(sizeof(struct aiocb *) * nent, M_LIO, M_WAITOK);
2291 error = copyin(uap->acb_list, acb_list, nent * sizeof(acb_list[0]));
2293 error = kern_lio_listio(td, uap->mode,
2294 (struct aiocb * const *)uap->acb_list, acb_list, nent, sigp,
2295 &aiocb_ops_osigevent);
2296 free(acb_list, M_LIO);
2301 /* syscall - list directed I/O (REALTIME) */
2303 sys_lio_listio(struct thread *td, struct lio_listio_args *uap)
2305 struct aiocb **acb_list;
2306 struct sigevent *sigp, sig;
2309 if ((uap->mode != LIO_NOWAIT) && (uap->mode != LIO_WAIT))
2313 if (nent < 0 || nent > max_aio_queue_per_proc)
2316 if (uap->sig && (uap->mode == LIO_NOWAIT)) {
2317 error = copyin(uap->sig, &sig, sizeof(sig));
2324 acb_list = malloc(sizeof(struct aiocb *) * nent, M_LIO, M_WAITOK);
2325 error = copyin(uap->acb_list, acb_list, nent * sizeof(acb_list[0]));
2327 error = kern_lio_listio(td, uap->mode, uap->acb_list, acb_list,
2328 nent, sigp, &aiocb_ops);
2329 free(acb_list, M_LIO);
2334 aio_biowakeup(struct bio *bp)
2336 struct kaiocb *job = (struct kaiocb *)bp->bio_caller1;
2338 struct kaioinfo *ki;
2342 /* Release mapping into kernel space. */
2343 userp = job->userproc;
2344 ki = userp->p_aioinfo;
2346 pmap_qremove((vm_offset_t)job->pbuf->b_data, job->npages);
2347 uma_zfree(pbuf_zone, job->pbuf);
2349 atomic_subtract_int(&num_buf_aio, 1);
2351 ki->kaio_buffer_count--;
2354 atomic_subtract_int(&num_unmapped_aio, 1);
2355 vm_page_unhold_pages(job->pages, job->npages);
2359 nbytes = job->uaiocb.aio_nbytes - bp->bio_resid;
2361 if (bp->bio_flags & BIO_ERROR)
2362 error = bp->bio_error;
2363 nblks = btodb(nbytes);
2364 if (job->uaiocb.aio_lio_opcode == LIO_WRITE)
2365 job->outblock += nblks;
2367 job->inblock += nblks;
2370 aio_complete(job, -1, error);
2372 aio_complete(job, nbytes, 0);
2377 /* syscall - wait for the next completion of an aio request */
2379 kern_aio_waitcomplete(struct thread *td, struct aiocb **ujobp,
2380 struct timespec *ts, struct aiocb_ops *ops)
2382 struct proc *p = td->td_proc;
2384 struct kaioinfo *ki;
2390 ops->store_aiocb(ujobp, NULL);
2394 } else if (ts->tv_sec == 0 && ts->tv_nsec == 0) {
2397 if ((ts->tv_nsec < 0) || (ts->tv_nsec >= 1000000000))
2400 TIMESPEC_TO_TIMEVAL(&atv, ts);
2401 if (itimerfix(&atv))
2403 timo = tvtohz(&atv);
2406 if (p->p_aioinfo == NULL)
2407 aio_init_aioinfo(p);
2413 while ((job = TAILQ_FIRST(&ki->kaio_done)) == NULL) {
2415 error = EWOULDBLOCK;
2418 ki->kaio_flags |= KAIO_WAKEUP;
2419 error = msleep(&p->p_aioinfo, AIO_MTX(ki), PRIBIO | PCATCH,
2421 if (timo && error == ERESTART)
2428 MPASS(job->jobflags & KAIOCB_FINISHED);
2430 status = job->uaiocb._aiocb_private.status;
2431 error = job->uaiocb._aiocb_private.error;
2432 td->td_retval[0] = status;
2433 td->td_ru.ru_oublock += job->outblock;
2434 td->td_ru.ru_inblock += job->inblock;
2435 td->td_ru.ru_msgsnd += job->msgsnd;
2436 td->td_ru.ru_msgrcv += job->msgrcv;
2437 aio_free_entry(job);
2439 ops->store_aiocb(ujobp, ujob);
2440 ops->store_error(ujob, error);
2441 ops->store_status(ujob, status);
2449 sys_aio_waitcomplete(struct thread *td, struct aio_waitcomplete_args *uap)
2451 struct timespec ts, *tsp;
2455 /* Get timespec struct. */
2456 error = copyin(uap->timeout, &ts, sizeof(ts));
2463 return (kern_aio_waitcomplete(td, uap->aiocbp, tsp, &aiocb_ops));
2467 kern_aio_fsync(struct thread *td, int op, struct aiocb *ujob,
2468 struct aiocb_ops *ops)
2471 if (op != O_SYNC) /* XXX lack of O_DSYNC */
2473 return (aio_aqueue(td, ujob, NULL, LIO_SYNC, ops));
2477 sys_aio_fsync(struct thread *td, struct aio_fsync_args *uap)
2480 return (kern_aio_fsync(td, uap->op, uap->aiocbp, &aiocb_ops));
2483 /* kqueue attach function */
2485 filt_aioattach(struct knote *kn)
2489 job = (struct kaiocb *)(uintptr_t)kn->kn_sdata;
2492 * The job pointer must be validated before using it, so
2493 * registration is restricted to the kernel; the user cannot
2496 if ((kn->kn_flags & EV_FLAG1) == 0)
2498 kn->kn_ptr.p_aio = job;
2499 kn->kn_flags &= ~EV_FLAG1;
2501 knlist_add(&job->klist, kn, 0);
2506 /* kqueue detach function */
2508 filt_aiodetach(struct knote *kn)
2512 knl = &kn->kn_ptr.p_aio->klist;
2513 knl->kl_lock(knl->kl_lockarg);
2514 if (!knlist_empty(knl))
2515 knlist_remove(knl, kn, 1);
2516 knl->kl_unlock(knl->kl_lockarg);
2519 /* kqueue filter function */
2522 filt_aio(struct knote *kn, long hint)
2524 struct kaiocb *job = kn->kn_ptr.p_aio;
2526 kn->kn_data = job->uaiocb._aiocb_private.error;
2527 if (!(job->jobflags & KAIOCB_FINISHED))
2529 kn->kn_flags |= EV_EOF;
2533 /* kqueue attach function */
2535 filt_lioattach(struct knote *kn)
2537 struct aioliojob *lj;
2539 lj = (struct aioliojob *)(uintptr_t)kn->kn_sdata;
2542 * The aioliojob pointer must be validated before using it, so
2543 * registration is restricted to the kernel; the user cannot
2546 if ((kn->kn_flags & EV_FLAG1) == 0)
2548 kn->kn_ptr.p_lio = lj;
2549 kn->kn_flags &= ~EV_FLAG1;
2551 knlist_add(&lj->klist, kn, 0);
2556 /* kqueue detach function */
2558 filt_liodetach(struct knote *kn)
2562 knl = &kn->kn_ptr.p_lio->klist;
2563 knl->kl_lock(knl->kl_lockarg);
2564 if (!knlist_empty(knl))
2565 knlist_remove(knl, kn, 1);
2566 knl->kl_unlock(knl->kl_lockarg);
2569 /* kqueue filter function */
2572 filt_lio(struct knote *kn, long hint)
2574 struct aioliojob * lj = kn->kn_ptr.p_lio;
2576 return (lj->lioj_flags & LIOJ_KEVENT_POSTED);
2579 #ifdef COMPAT_FREEBSD32
2580 #include <sys/mount.h>
2581 #include <sys/socket.h>
2582 #include <compat/freebsd32/freebsd32.h>
2583 #include <compat/freebsd32/freebsd32_proto.h>
2584 #include <compat/freebsd32/freebsd32_signal.h>
2585 #include <compat/freebsd32/freebsd32_syscall.h>
2586 #include <compat/freebsd32/freebsd32_util.h>
2588 struct __aiocb_private32 {
2591 uint32_t kernelinfo;
2594 #ifdef COMPAT_FREEBSD6
2595 typedef struct oaiocb32 {
2596 int aio_fildes; /* File descriptor */
2597 uint64_t aio_offset __packed; /* File offset for I/O */
2598 uint32_t aio_buf; /* I/O buffer in process space */
2599 uint32_t aio_nbytes; /* Number of bytes for I/O */
2600 struct osigevent32 aio_sigevent; /* Signal to deliver */
2601 int aio_lio_opcode; /* LIO opcode */
2602 int aio_reqprio; /* Request priority -- ignored */
2603 struct __aiocb_private32 _aiocb_private;
2607 typedef struct aiocb32 {
2608 int32_t aio_fildes; /* File descriptor */
2609 uint64_t aio_offset __packed; /* File offset for I/O */
2610 uint32_t aio_buf; /* I/O buffer in process space */
2611 uint32_t aio_nbytes; /* Number of bytes for I/O */
2613 uint32_t __spare2__;
2614 int aio_lio_opcode; /* LIO opcode */
2615 int aio_reqprio; /* Request priority -- ignored */
2616 struct __aiocb_private32 _aiocb_private;
2617 struct sigevent32 aio_sigevent; /* Signal to deliver */
2620 #ifdef COMPAT_FREEBSD6
2622 convert_old_sigevent32(struct osigevent32 *osig, struct sigevent *nsig)
2626 * Only SIGEV_NONE, SIGEV_SIGNAL, and SIGEV_KEVENT are
2627 * supported by AIO with the old sigevent structure.
2629 CP(*osig, *nsig, sigev_notify);
2630 switch (nsig->sigev_notify) {
2634 nsig->sigev_signo = osig->__sigev_u.__sigev_signo;
2637 nsig->sigev_notify_kqueue =
2638 osig->__sigev_u.__sigev_notify_kqueue;
2639 PTRIN_CP(*osig, *nsig, sigev_value.sival_ptr);
2648 aiocb32_copyin_old_sigevent(struct aiocb *ujob, struct aiocb *kjob)
2650 struct oaiocb32 job32;
2653 bzero(kjob, sizeof(struct aiocb));
2654 error = copyin(ujob, &job32, sizeof(job32));
2658 CP(job32, *kjob, aio_fildes);
2659 CP(job32, *kjob, aio_offset);
2660 PTRIN_CP(job32, *kjob, aio_buf);
2661 CP(job32, *kjob, aio_nbytes);
2662 CP(job32, *kjob, aio_lio_opcode);
2663 CP(job32, *kjob, aio_reqprio);
2664 CP(job32, *kjob, _aiocb_private.status);
2665 CP(job32, *kjob, _aiocb_private.error);
2666 PTRIN_CP(job32, *kjob, _aiocb_private.kernelinfo);
2667 return (convert_old_sigevent32(&job32.aio_sigevent,
2668 &kjob->aio_sigevent));
2673 aiocb32_copyin(struct aiocb *ujob, struct aiocb *kjob)
2675 struct aiocb32 job32;
2678 error = copyin(ujob, &job32, sizeof(job32));
2681 CP(job32, *kjob, aio_fildes);
2682 CP(job32, *kjob, aio_offset);
2683 PTRIN_CP(job32, *kjob, aio_buf);
2684 CP(job32, *kjob, aio_nbytes);
2685 CP(job32, *kjob, aio_lio_opcode);
2686 CP(job32, *kjob, aio_reqprio);
2687 CP(job32, *kjob, _aiocb_private.status);
2688 CP(job32, *kjob, _aiocb_private.error);
2689 PTRIN_CP(job32, *kjob, _aiocb_private.kernelinfo);
2690 return (convert_sigevent32(&job32.aio_sigevent, &kjob->aio_sigevent));
2694 aiocb32_fetch_status(struct aiocb *ujob)
2696 struct aiocb32 *ujob32;
2698 ujob32 = (struct aiocb32 *)ujob;
2699 return (fuword32(&ujob32->_aiocb_private.status));
2703 aiocb32_fetch_error(struct aiocb *ujob)
2705 struct aiocb32 *ujob32;
2707 ujob32 = (struct aiocb32 *)ujob;
2708 return (fuword32(&ujob32->_aiocb_private.error));
2712 aiocb32_store_status(struct aiocb *ujob, long status)
2714 struct aiocb32 *ujob32;
2716 ujob32 = (struct aiocb32 *)ujob;
2717 return (suword32(&ujob32->_aiocb_private.status, status));
2721 aiocb32_store_error(struct aiocb *ujob, long error)
2723 struct aiocb32 *ujob32;
2725 ujob32 = (struct aiocb32 *)ujob;
2726 return (suword32(&ujob32->_aiocb_private.error, error));
2730 aiocb32_store_kernelinfo(struct aiocb *ujob, long jobref)
2732 struct aiocb32 *ujob32;
2734 ujob32 = (struct aiocb32 *)ujob;
2735 return (suword32(&ujob32->_aiocb_private.kernelinfo, jobref));
2739 aiocb32_store_aiocb(struct aiocb **ujobp, struct aiocb *ujob)
2742 return (suword32(ujobp, (long)ujob));
2745 static struct aiocb_ops aiocb32_ops = {
2746 .copyin = aiocb32_copyin,
2747 .fetch_status = aiocb32_fetch_status,
2748 .fetch_error = aiocb32_fetch_error,
2749 .store_status = aiocb32_store_status,
2750 .store_error = aiocb32_store_error,
2751 .store_kernelinfo = aiocb32_store_kernelinfo,
2752 .store_aiocb = aiocb32_store_aiocb,
2755 #ifdef COMPAT_FREEBSD6
2756 static struct aiocb_ops aiocb32_ops_osigevent = {
2757 .copyin = aiocb32_copyin_old_sigevent,
2758 .fetch_status = aiocb32_fetch_status,
2759 .fetch_error = aiocb32_fetch_error,
2760 .store_status = aiocb32_store_status,
2761 .store_error = aiocb32_store_error,
2762 .store_kernelinfo = aiocb32_store_kernelinfo,
2763 .store_aiocb = aiocb32_store_aiocb,
2768 freebsd32_aio_return(struct thread *td, struct freebsd32_aio_return_args *uap)
2771 return (kern_aio_return(td, (struct aiocb *)uap->aiocbp, &aiocb32_ops));
2775 freebsd32_aio_suspend(struct thread *td, struct freebsd32_aio_suspend_args *uap)
2777 struct timespec32 ts32;
2778 struct timespec ts, *tsp;
2779 struct aiocb **ujoblist;
2780 uint32_t *ujoblist32;
2783 if (uap->nent < 0 || uap->nent > max_aio_queue_per_proc)
2787 /* Get timespec struct. */
2788 if ((error = copyin(uap->timeout, &ts32, sizeof(ts32))) != 0)
2790 CP(ts32, ts, tv_sec);
2791 CP(ts32, ts, tv_nsec);
2796 ujoblist = malloc(uap->nent * sizeof(ujoblist[0]), M_AIOS, M_WAITOK);
2797 ujoblist32 = (uint32_t *)ujoblist;
2798 error = copyin(uap->aiocbp, ujoblist32, uap->nent *
2799 sizeof(ujoblist32[0]));
2801 for (i = uap->nent - 1; i >= 0; i--)
2802 ujoblist[i] = PTRIN(ujoblist32[i]);
2804 error = kern_aio_suspend(td, uap->nent, ujoblist, tsp);
2806 free(ujoblist, M_AIOS);
2811 freebsd32_aio_error(struct thread *td, struct freebsd32_aio_error_args *uap)
2814 return (kern_aio_error(td, (struct aiocb *)uap->aiocbp, &aiocb32_ops));
2817 #ifdef COMPAT_FREEBSD6
2819 freebsd6_freebsd32_aio_read(struct thread *td,
2820 struct freebsd6_freebsd32_aio_read_args *uap)
2823 return (aio_aqueue(td, (struct aiocb *)uap->aiocbp, NULL, LIO_READ,
2824 &aiocb32_ops_osigevent));
2829 freebsd32_aio_read(struct thread *td, struct freebsd32_aio_read_args *uap)
2832 return (aio_aqueue(td, (struct aiocb *)uap->aiocbp, NULL, LIO_READ,
2836 #ifdef COMPAT_FREEBSD6
2838 freebsd6_freebsd32_aio_write(struct thread *td,
2839 struct freebsd6_freebsd32_aio_write_args *uap)
2842 return (aio_aqueue(td, (struct aiocb *)uap->aiocbp, NULL, LIO_WRITE,
2843 &aiocb32_ops_osigevent));
2848 freebsd32_aio_write(struct thread *td, struct freebsd32_aio_write_args *uap)
2851 return (aio_aqueue(td, (struct aiocb *)uap->aiocbp, NULL, LIO_WRITE,
2856 freebsd32_aio_mlock(struct thread *td, struct freebsd32_aio_mlock_args *uap)
2859 return (aio_aqueue(td, (struct aiocb *)uap->aiocbp, NULL, LIO_MLOCK,
2864 freebsd32_aio_waitcomplete(struct thread *td,
2865 struct freebsd32_aio_waitcomplete_args *uap)
2867 struct timespec32 ts32;
2868 struct timespec ts, *tsp;
2872 /* Get timespec struct. */
2873 error = copyin(uap->timeout, &ts32, sizeof(ts32));
2876 CP(ts32, ts, tv_sec);
2877 CP(ts32, ts, tv_nsec);
2882 return (kern_aio_waitcomplete(td, (struct aiocb **)uap->aiocbp, tsp,
2887 freebsd32_aio_fsync(struct thread *td, struct freebsd32_aio_fsync_args *uap)
2890 return (kern_aio_fsync(td, uap->op, (struct aiocb *)uap->aiocbp,
2894 #ifdef COMPAT_FREEBSD6
2896 freebsd6_freebsd32_lio_listio(struct thread *td,
2897 struct freebsd6_freebsd32_lio_listio_args *uap)
2899 struct aiocb **acb_list;
2900 struct sigevent *sigp, sig;
2901 struct osigevent32 osig;
2902 uint32_t *acb_list32;
2905 if ((uap->mode != LIO_NOWAIT) && (uap->mode != LIO_WAIT))
2909 if (nent < 0 || nent > max_aio_queue_per_proc)
2912 if (uap->sig && (uap->mode == LIO_NOWAIT)) {
2913 error = copyin(uap->sig, &osig, sizeof(osig));
2916 error = convert_old_sigevent32(&osig, &sig);
2923 acb_list32 = malloc(sizeof(uint32_t) * nent, M_LIO, M_WAITOK);
2924 error = copyin(uap->acb_list, acb_list32, nent * sizeof(uint32_t));
2926 free(acb_list32, M_LIO);
2929 acb_list = malloc(sizeof(struct aiocb *) * nent, M_LIO, M_WAITOK);
2930 for (i = 0; i < nent; i++)
2931 acb_list[i] = PTRIN(acb_list32[i]);
2932 free(acb_list32, M_LIO);
2934 error = kern_lio_listio(td, uap->mode,
2935 (struct aiocb * const *)uap->acb_list, acb_list, nent, sigp,
2936 &aiocb32_ops_osigevent);
2937 free(acb_list, M_LIO);
2943 freebsd32_lio_listio(struct thread *td, struct freebsd32_lio_listio_args *uap)
2945 struct aiocb **acb_list;
2946 struct sigevent *sigp, sig;
2947 struct sigevent32 sig32;
2948 uint32_t *acb_list32;
2951 if ((uap->mode != LIO_NOWAIT) && (uap->mode != LIO_WAIT))
2955 if (nent < 0 || nent > max_aio_queue_per_proc)
2958 if (uap->sig && (uap->mode == LIO_NOWAIT)) {
2959 error = copyin(uap->sig, &sig32, sizeof(sig32));
2962 error = convert_sigevent32(&sig32, &sig);
2969 acb_list32 = malloc(sizeof(uint32_t) * nent, M_LIO, M_WAITOK);
2970 error = copyin(uap->acb_list, acb_list32, nent * sizeof(uint32_t));
2972 free(acb_list32, M_LIO);
2975 acb_list = malloc(sizeof(struct aiocb *) * nent, M_LIO, M_WAITOK);
2976 for (i = 0; i < nent; i++)
2977 acb_list[i] = PTRIN(acb_list32[i]);
2978 free(acb_list32, M_LIO);
2980 error = kern_lio_listio(td, uap->mode,
2981 (struct aiocb * const *)uap->acb_list, acb_list, nent, sigp,
2983 free(acb_list, M_LIO);