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 "opt_compat.h"
28 #include <sys/param.h>
29 #include <sys/systm.h>
30 #include <sys/malloc.h>
33 #include <sys/capsicum.h>
34 #include <sys/eventhandler.h>
35 #include <sys/sysproto.h>
36 #include <sys/filedesc.h>
37 #include <sys/kernel.h>
38 #include <sys/module.h>
39 #include <sys/kthread.h>
40 #include <sys/fcntl.h>
42 #include <sys/limits.h>
44 #include <sys/mutex.h>
45 #include <sys/unistd.h>
46 #include <sys/posix4.h>
48 #include <sys/resourcevar.h>
49 #include <sys/signalvar.h>
50 #include <sys/syscallsubr.h>
51 #include <sys/protosw.h>
52 #include <sys/rwlock.h>
54 #include <sys/socket.h>
55 #include <sys/socketvar.h>
56 #include <sys/syscall.h>
57 #include <sys/sysent.h>
58 #include <sys/sysctl.h>
59 #include <sys/syslog.h>
61 #include <sys/taskqueue.h>
62 #include <sys/vnode.h>
64 #include <sys/event.h>
65 #include <sys/mount.h>
66 #include <geom/geom.h>
68 #include <machine/atomic.h>
71 #include <vm/vm_page.h>
72 #include <vm/vm_extern.h>
74 #include <vm/vm_map.h>
75 #include <vm/vm_object.h>
80 * Counter for allocating reference ids to new jobs. Wrapped to 1 on
81 * overflow. (XXX will be removed soon.)
83 static u_long jobrefid;
86 * Counter for aio_fsync.
88 static uint64_t jobseqno;
90 #ifndef MAX_AIO_PER_PROC
91 #define MAX_AIO_PER_PROC 32
94 #ifndef MAX_AIO_QUEUE_PER_PROC
95 #define MAX_AIO_QUEUE_PER_PROC 256
99 #define MAX_AIO_QUEUE 1024 /* Bigger than MAX_AIO_QUEUE_PER_PROC */
103 #define MAX_BUF_AIO 16
106 FEATURE(aio, "Asynchronous I/O");
107 SYSCTL_DECL(_p1003_1b);
109 static MALLOC_DEFINE(M_LIO, "lio", "listio aio control block list");
110 static MALLOC_DEFINE(M_AIOS, "aios", "aio_suspend aio control block list");
112 static SYSCTL_NODE(_vfs, OID_AUTO, aio, CTLFLAG_RW, 0,
113 "Async IO management");
115 static int enable_aio_unsafe = 0;
116 SYSCTL_INT(_vfs_aio, OID_AUTO, enable_unsafe, CTLFLAG_RW, &enable_aio_unsafe, 0,
117 "Permit asynchronous IO on all file types, not just known-safe types");
119 static unsigned int unsafe_warningcnt = 1;
120 SYSCTL_UINT(_vfs_aio, OID_AUTO, unsafe_warningcnt, CTLFLAG_RW,
121 &unsafe_warningcnt, 0,
122 "Warnings that will be triggered upon failed IO requests on unsafe files");
124 static int max_aio_procs = MAX_AIO_PROCS;
125 SYSCTL_INT(_vfs_aio, OID_AUTO, max_aio_procs, CTLFLAG_RW, &max_aio_procs, 0,
126 "Maximum number of kernel processes to use for handling async IO ");
128 static int num_aio_procs = 0;
129 SYSCTL_INT(_vfs_aio, OID_AUTO, num_aio_procs, CTLFLAG_RD, &num_aio_procs, 0,
130 "Number of presently active kernel processes for async IO");
133 * The code will adjust the actual number of AIO processes towards this
134 * number when it gets a chance.
136 static int target_aio_procs = TARGET_AIO_PROCS;
137 SYSCTL_INT(_vfs_aio, OID_AUTO, target_aio_procs, CTLFLAG_RW, &target_aio_procs,
139 "Preferred number of ready kernel processes for async IO");
141 static int max_queue_count = MAX_AIO_QUEUE;
142 SYSCTL_INT(_vfs_aio, OID_AUTO, max_aio_queue, CTLFLAG_RW, &max_queue_count, 0,
143 "Maximum number of aio requests to queue, globally");
145 static int num_queue_count = 0;
146 SYSCTL_INT(_vfs_aio, OID_AUTO, num_queue_count, CTLFLAG_RD, &num_queue_count, 0,
147 "Number of queued aio requests");
149 static int num_buf_aio = 0;
150 SYSCTL_INT(_vfs_aio, OID_AUTO, num_buf_aio, CTLFLAG_RD, &num_buf_aio, 0,
151 "Number of aio requests presently handled by the buf subsystem");
153 static int num_unmapped_aio = 0;
154 SYSCTL_INT(_vfs_aio, OID_AUTO, num_unmapped_aio, CTLFLAG_RD, &num_unmapped_aio,
156 "Number of aio requests presently handled by unmapped I/O buffers");
158 /* Number of async I/O processes in the process of being started */
159 /* XXX This should be local to aio_aqueue() */
160 static int num_aio_resv_start = 0;
162 static int aiod_lifetime;
163 SYSCTL_INT(_vfs_aio, OID_AUTO, aiod_lifetime, CTLFLAG_RW, &aiod_lifetime, 0,
164 "Maximum lifetime for idle aiod");
166 static int max_aio_per_proc = MAX_AIO_PER_PROC;
167 SYSCTL_INT(_vfs_aio, OID_AUTO, max_aio_per_proc, CTLFLAG_RW, &max_aio_per_proc,
169 "Maximum active aio requests per process");
171 static int max_aio_queue_per_proc = MAX_AIO_QUEUE_PER_PROC;
172 SYSCTL_INT(_vfs_aio, OID_AUTO, max_aio_queue_per_proc, CTLFLAG_RW,
173 &max_aio_queue_per_proc, 0,
174 "Maximum queued aio requests per process");
176 static int max_buf_aio = MAX_BUF_AIO;
177 SYSCTL_INT(_vfs_aio, OID_AUTO, max_buf_aio, CTLFLAG_RW, &max_buf_aio, 0,
178 "Maximum buf aio requests per process");
181 * Though redundant with vfs.aio.max_aio_queue_per_proc, POSIX requires
182 * sysconf(3) to support AIO_LISTIO_MAX, and we implement that with
183 * vfs.aio.aio_listio_max.
185 SYSCTL_INT(_p1003_1b, CTL_P1003_1B_AIO_LISTIO_MAX, aio_listio_max,
186 CTLFLAG_RD | CTLFLAG_CAPRD, &max_aio_queue_per_proc,
187 0, "Maximum aio requests for a single lio_listio call");
189 #ifdef COMPAT_FREEBSD6
190 typedef struct oaiocb {
191 int aio_fildes; /* File descriptor */
192 off_t aio_offset; /* File offset for I/O */
193 volatile void *aio_buf; /* I/O buffer in process space */
194 size_t aio_nbytes; /* Number of bytes for I/O */
195 struct osigevent aio_sigevent; /* Signal to deliver */
196 int aio_lio_opcode; /* LIO opcode */
197 int aio_reqprio; /* Request priority -- ignored */
198 struct __aiocb_private _aiocb_private;
203 * Below is a key of locks used to protect each member of struct kaiocb
204 * aioliojob and kaioinfo and any backends.
206 * * - need not protected
207 * a - locked by kaioinfo lock
208 * b - locked by backend lock, the backend lock can be null in some cases,
209 * for example, BIO belongs to this type, in this case, proc lock is
211 * c - locked by aio_job_mtx, the lock for the generic file I/O backend.
215 * If the routine that services an AIO request blocks while running in an
216 * AIO kernel process it can starve other I/O requests. BIO requests
217 * queued via aio_qphysio() complete in GEOM and do not use AIO kernel
218 * processes at all. Socket I/O requests use a separate pool of
219 * kprocs and also force non-blocking I/O. Other file I/O requests
220 * use the generic fo_read/fo_write operations which can block. The
221 * fsync and mlock operations can also block while executing. Ideally
222 * none of these requests would block while executing.
224 * Note that the service routines cannot toggle O_NONBLOCK in the file
225 * structure directly while handling a request due to races with
230 #define KAIOCB_QUEUEING 0x01
231 #define KAIOCB_CANCELLED 0x02
232 #define KAIOCB_CANCELLING 0x04
233 #define KAIOCB_CHECKSYNC 0x08
234 #define KAIOCB_CLEARED 0x10
235 #define KAIOCB_FINISHED 0x20
240 #define AIOP_FREE 0x1 /* proc on free queue */
243 int aioprocflags; /* (c) AIO proc flags */
244 TAILQ_ENTRY(aioproc) list; /* (c) list of processes */
245 struct proc *aioproc; /* (*) the AIO proc */
249 * data-structure for lio signal management
252 int lioj_flags; /* (a) listio flags */
253 int lioj_count; /* (a) listio flags */
254 int lioj_finished_count; /* (a) listio flags */
255 struct sigevent lioj_signal; /* (a) signal on all I/O done */
256 TAILQ_ENTRY(aioliojob) lioj_list; /* (a) lio list */
257 struct knlist klist; /* (a) list of knotes */
258 ksiginfo_t lioj_ksi; /* (a) Realtime signal info */
261 #define LIOJ_SIGNAL 0x1 /* signal on all done (lio) */
262 #define LIOJ_SIGNAL_POSTED 0x2 /* signal has been posted */
263 #define LIOJ_KEVENT_POSTED 0x4 /* kevent triggered */
266 * per process aio data structure
269 struct mtx kaio_mtx; /* the lock to protect this struct */
270 int kaio_flags; /* (a) per process kaio flags */
271 int kaio_active_count; /* (c) number of currently used AIOs */
272 int kaio_count; /* (a) size of AIO queue */
273 int kaio_buffer_count; /* (a) number of physio buffers */
274 TAILQ_HEAD(,kaiocb) kaio_all; /* (a) all AIOs in a process */
275 TAILQ_HEAD(,kaiocb) kaio_done; /* (a) done queue for process */
276 TAILQ_HEAD(,aioliojob) kaio_liojoblist; /* (a) list of lio jobs */
277 TAILQ_HEAD(,kaiocb) kaio_jobqueue; /* (a) job queue for process */
278 TAILQ_HEAD(,kaiocb) kaio_syncqueue; /* (a) queue for aio_fsync */
279 TAILQ_HEAD(,kaiocb) kaio_syncready; /* (a) second q for aio_fsync */
280 struct task kaio_task; /* (*) task to kick aio processes */
281 struct task kaio_sync_task; /* (*) task to schedule fsync jobs */
284 #define AIO_LOCK(ki) mtx_lock(&(ki)->kaio_mtx)
285 #define AIO_UNLOCK(ki) mtx_unlock(&(ki)->kaio_mtx)
286 #define AIO_LOCK_ASSERT(ki, f) mtx_assert(&(ki)->kaio_mtx, (f))
287 #define AIO_MTX(ki) (&(ki)->kaio_mtx)
289 #define KAIO_RUNDOWN 0x1 /* process is being run down */
290 #define KAIO_WAKEUP 0x2 /* wakeup process when AIO completes */
293 * Operations used to interact with userland aio control blocks.
294 * Different ABIs provide their own operations.
297 int (*copyin)(struct aiocb *ujob, struct aiocb *kjob);
298 long (*fetch_status)(struct aiocb *ujob);
299 long (*fetch_error)(struct aiocb *ujob);
300 int (*store_status)(struct aiocb *ujob, long status);
301 int (*store_error)(struct aiocb *ujob, long error);
302 int (*store_kernelinfo)(struct aiocb *ujob, long jobref);
303 int (*store_aiocb)(struct aiocb **ujobp, struct aiocb *ujob);
306 static TAILQ_HEAD(,aioproc) aio_freeproc; /* (c) Idle daemons */
307 static struct sema aio_newproc_sem;
308 static struct mtx aio_job_mtx;
309 static TAILQ_HEAD(,kaiocb) aio_jobs; /* (c) Async job list */
310 static struct unrhdr *aiod_unr;
312 void aio_init_aioinfo(struct proc *p);
313 static int aio_onceonly(void);
314 static int aio_free_entry(struct kaiocb *job);
315 static void aio_process_rw(struct kaiocb *job);
316 static void aio_process_sync(struct kaiocb *job);
317 static void aio_process_mlock(struct kaiocb *job);
318 static void aio_schedule_fsync(void *context, int pending);
319 static int aio_newproc(int *);
320 int aio_aqueue(struct thread *td, struct aiocb *ujob,
321 struct aioliojob *lio, int type, struct aiocb_ops *ops);
322 static int aio_queue_file(struct file *fp, struct kaiocb *job);
323 static void aio_physwakeup(struct bio *bp);
324 static void aio_proc_rundown(void *arg, struct proc *p);
325 static void aio_proc_rundown_exec(void *arg, struct proc *p,
326 struct image_params *imgp);
327 static int aio_qphysio(struct proc *p, struct kaiocb *job);
328 static void aio_daemon(void *param);
329 static void aio_bio_done_notify(struct proc *userp, struct kaiocb *job);
330 static bool aio_clear_cancel_function_locked(struct kaiocb *job);
331 static int aio_kick(struct proc *userp);
332 static void aio_kick_nowait(struct proc *userp);
333 static void aio_kick_helper(void *context, int pending);
334 static int filt_aioattach(struct knote *kn);
335 static void filt_aiodetach(struct knote *kn);
336 static int filt_aio(struct knote *kn, long hint);
337 static int filt_lioattach(struct knote *kn);
338 static void filt_liodetach(struct knote *kn);
339 static int filt_lio(struct knote *kn, long hint);
343 * kaio Per process async io info
344 * aiop async io process data
345 * aiocb async io jobs
346 * aiolio list io jobs
348 static uma_zone_t kaio_zone, aiop_zone, aiocb_zone, aiolio_zone;
350 /* kqueue filters for aio */
351 static struct filterops aio_filtops = {
353 .f_attach = filt_aioattach,
354 .f_detach = filt_aiodetach,
357 static struct filterops lio_filtops = {
359 .f_attach = filt_lioattach,
360 .f_detach = filt_liodetach,
364 static eventhandler_tag exit_tag, exec_tag;
366 TASKQUEUE_DEFINE_THREAD(aiod_kick);
369 * Main operations function for use as a kernel module.
372 aio_modload(struct module *module, int cmd, void *arg)
389 static moduledata_t aio_mod = {
395 DECLARE_MODULE(aio, aio_mod, SI_SUB_VFS, SI_ORDER_ANY);
396 MODULE_VERSION(aio, 1);
399 * Startup initialization
405 exit_tag = EVENTHANDLER_REGISTER(process_exit, aio_proc_rundown, NULL,
406 EVENTHANDLER_PRI_ANY);
407 exec_tag = EVENTHANDLER_REGISTER(process_exec, aio_proc_rundown_exec,
408 NULL, EVENTHANDLER_PRI_ANY);
409 kqueue_add_filteropts(EVFILT_AIO, &aio_filtops);
410 kqueue_add_filteropts(EVFILT_LIO, &lio_filtops);
411 TAILQ_INIT(&aio_freeproc);
412 sema_init(&aio_newproc_sem, 0, "aio_new_proc");
413 mtx_init(&aio_job_mtx, "aio_job", NULL, MTX_DEF);
414 TAILQ_INIT(&aio_jobs);
415 aiod_unr = new_unrhdr(1, INT_MAX, NULL);
416 kaio_zone = uma_zcreate("AIO", sizeof(struct kaioinfo), NULL, NULL,
417 NULL, NULL, UMA_ALIGN_PTR, UMA_ZONE_NOFREE);
418 aiop_zone = uma_zcreate("AIOP", sizeof(struct aioproc), NULL,
419 NULL, NULL, NULL, UMA_ALIGN_PTR, UMA_ZONE_NOFREE);
420 aiocb_zone = uma_zcreate("AIOCB", sizeof(struct kaiocb), NULL, NULL,
421 NULL, NULL, UMA_ALIGN_PTR, UMA_ZONE_NOFREE);
422 aiolio_zone = uma_zcreate("AIOLIO", sizeof(struct aioliojob), NULL,
423 NULL, NULL, NULL, UMA_ALIGN_PTR, UMA_ZONE_NOFREE);
424 aiod_lifetime = AIOD_LIFETIME_DEFAULT;
426 p31b_setcfg(CTL_P1003_1B_ASYNCHRONOUS_IO, _POSIX_ASYNCHRONOUS_IO);
427 p31b_setcfg(CTL_P1003_1B_AIO_MAX, MAX_AIO_QUEUE);
428 p31b_setcfg(CTL_P1003_1B_AIO_PRIO_DELTA_MAX, 0);
434 * Init the per-process aioinfo structure. The aioinfo limits are set
435 * per-process for user limit (resource) management.
438 aio_init_aioinfo(struct proc *p)
442 ki = uma_zalloc(kaio_zone, M_WAITOK);
443 mtx_init(&ki->kaio_mtx, "aiomtx", NULL, MTX_DEF | MTX_NEW);
445 ki->kaio_active_count = 0;
447 ki->kaio_buffer_count = 0;
448 TAILQ_INIT(&ki->kaio_all);
449 TAILQ_INIT(&ki->kaio_done);
450 TAILQ_INIT(&ki->kaio_jobqueue);
451 TAILQ_INIT(&ki->kaio_liojoblist);
452 TAILQ_INIT(&ki->kaio_syncqueue);
453 TAILQ_INIT(&ki->kaio_syncready);
454 TASK_INIT(&ki->kaio_task, 0, aio_kick_helper, p);
455 TASK_INIT(&ki->kaio_sync_task, 0, aio_schedule_fsync, ki);
457 if (p->p_aioinfo == NULL) {
462 mtx_destroy(&ki->kaio_mtx);
463 uma_zfree(kaio_zone, ki);
466 while (num_aio_procs < MIN(target_aio_procs, max_aio_procs))
471 aio_sendsig(struct proc *p, struct sigevent *sigev, ksiginfo_t *ksi)
476 error = sigev_findtd(p, sigev, &td);
480 ksiginfo_set_sigev(ksi, sigev);
481 ksi->ksi_code = SI_ASYNCIO;
482 ksi->ksi_flags |= KSI_EXT | KSI_INS;
483 tdsendsignal(p, td, ksi->ksi_signo, ksi);
490 * Free a job entry. Wait for completion if it is currently active, but don't
491 * delay forever. If we delay, we return a flag that says that we have to
492 * restart the queue scan.
495 aio_free_entry(struct kaiocb *job)
498 struct aioliojob *lj;
506 AIO_LOCK_ASSERT(ki, MA_OWNED);
507 MPASS(job->jobflags & KAIOCB_FINISHED);
509 atomic_subtract_int(&num_queue_count, 1);
512 MPASS(ki->kaio_count >= 0);
514 TAILQ_REMOVE(&ki->kaio_done, job, plist);
515 TAILQ_REMOVE(&ki->kaio_all, job, allist);
520 lj->lioj_finished_count--;
522 if (lj->lioj_count == 0) {
523 TAILQ_REMOVE(&ki->kaio_liojoblist, lj, lioj_list);
524 /* lio is going away, we need to destroy any knotes */
525 knlist_delete(&lj->klist, curthread, 1);
527 sigqueue_take(&lj->lioj_ksi);
529 uma_zfree(aiolio_zone, lj);
533 /* job is going away, we need to destroy any knotes */
534 knlist_delete(&job->klist, curthread, 1);
536 sigqueue_take(&job->ksi);
542 * The thread argument here is used to find the owning process
543 * and is also passed to fo_close() which may pass it to various
544 * places such as devsw close() routines. Because of that, we
545 * need a thread pointer from the process owning the job that is
546 * persistent and won't disappear out from under us or move to
549 * Currently, all the callers of this function call it to remove
550 * a kaiocb from the current process' job list either via a
551 * syscall or due to the current process calling exit() or
552 * execve(). Thus, we know that p == curproc. We also know that
553 * curthread can't exit since we are curthread.
555 * Therefore, we use curthread as the thread to pass to
556 * knlist_delete(). This does mean that it is possible for the
557 * thread pointer at close time to differ from the thread pointer
558 * at open time, but this is already true of file descriptors in
559 * a multithreaded process.
562 fdrop(job->fd_file, curthread);
564 uma_zfree(aiocb_zone, job);
571 aio_proc_rundown_exec(void *arg, struct proc *p,
572 struct image_params *imgp __unused)
574 aio_proc_rundown(arg, p);
578 aio_cancel_job(struct proc *p, struct kaioinfo *ki, struct kaiocb *job)
580 aio_cancel_fn_t *func;
583 AIO_LOCK_ASSERT(ki, MA_OWNED);
584 if (job->jobflags & (KAIOCB_CANCELLED | KAIOCB_FINISHED))
586 MPASS((job->jobflags & KAIOCB_CANCELLING) == 0);
587 job->jobflags |= KAIOCB_CANCELLED;
589 func = job->cancel_fn;
592 * If there is no cancel routine, just leave the job marked as
593 * cancelled. The job should be in active use by a caller who
594 * should complete it normally or when it fails to install a
601 * Set the CANCELLING flag so that aio_complete() will defer
602 * completions of this job. This prevents the job from being
603 * freed out from under the cancel callback. After the
604 * callback any deferred completion (whether from the callback
605 * or any other source) will be completed.
607 job->jobflags |= KAIOCB_CANCELLING;
611 job->jobflags &= ~KAIOCB_CANCELLING;
612 if (job->jobflags & KAIOCB_FINISHED) {
613 cancelled = job->uaiocb._aiocb_private.error == ECANCELED;
614 TAILQ_REMOVE(&ki->kaio_jobqueue, job, plist);
615 aio_bio_done_notify(p, job);
618 * The cancel callback might have scheduled an
619 * operation to cancel this request, but it is
620 * only counted as cancelled if the request is
621 * cancelled when the callback returns.
629 * Rundown the jobs for a given process.
632 aio_proc_rundown(void *arg, struct proc *p)
635 struct aioliojob *lj;
636 struct kaiocb *job, *jobn;
638 KASSERT(curthread->td_proc == p,
639 ("%s: called on non-curproc", __func__));
645 ki->kaio_flags |= KAIO_RUNDOWN;
650 * Try to cancel all pending requests. This code simulates
651 * aio_cancel on all pending I/O requests.
653 TAILQ_FOREACH_SAFE(job, &ki->kaio_jobqueue, plist, jobn) {
654 aio_cancel_job(p, ki, job);
657 /* Wait for all running I/O to be finished */
658 if (TAILQ_FIRST(&ki->kaio_jobqueue) || ki->kaio_active_count != 0) {
659 ki->kaio_flags |= KAIO_WAKEUP;
660 msleep(&p->p_aioinfo, AIO_MTX(ki), PRIBIO, "aioprn", hz);
664 /* Free all completed I/O requests. */
665 while ((job = TAILQ_FIRST(&ki->kaio_done)) != NULL)
668 while ((lj = TAILQ_FIRST(&ki->kaio_liojoblist)) != NULL) {
669 if (lj->lioj_count == 0) {
670 TAILQ_REMOVE(&ki->kaio_liojoblist, lj, lioj_list);
671 knlist_delete(&lj->klist, curthread, 1);
673 sigqueue_take(&lj->lioj_ksi);
675 uma_zfree(aiolio_zone, lj);
677 panic("LIO job not cleaned up: C:%d, FC:%d\n",
678 lj->lioj_count, lj->lioj_finished_count);
682 taskqueue_drain(taskqueue_aiod_kick, &ki->kaio_task);
683 taskqueue_drain(taskqueue_aiod_kick, &ki->kaio_sync_task);
684 mtx_destroy(&ki->kaio_mtx);
685 uma_zfree(kaio_zone, ki);
690 * Select a job to run (called by an AIO daemon).
692 static struct kaiocb *
693 aio_selectjob(struct aioproc *aiop)
699 mtx_assert(&aio_job_mtx, MA_OWNED);
701 TAILQ_FOREACH(job, &aio_jobs, list) {
702 userp = job->userproc;
703 ki = userp->p_aioinfo;
705 if (ki->kaio_active_count < max_aio_per_proc) {
706 TAILQ_REMOVE(&aio_jobs, job, list);
707 if (!aio_clear_cancel_function(job))
710 /* Account for currently active jobs. */
711 ki->kaio_active_count++;
719 * Move all data to a permanent storage device. This code
720 * simulates the fsync syscall.
723 aio_fsync_vnode(struct thread *td, struct vnode *vp)
728 if ((error = vn_start_write(vp, &mp, V_WAIT | PCATCH)) != 0)
730 vn_lock(vp, LK_EXCLUSIVE | LK_RETRY);
731 if (vp->v_object != NULL) {
732 VM_OBJECT_WLOCK(vp->v_object);
733 vm_object_page_clean(vp->v_object, 0, 0, 0);
734 VM_OBJECT_WUNLOCK(vp->v_object);
736 error = VOP_FSYNC(vp, MNT_WAIT, td);
739 vn_finished_write(mp);
745 * The AIO processing activity for LIO_READ/LIO_WRITE. This is the code that
746 * does the I/O request for the non-physio version of the operations. The
747 * normal vn operations are used, and this code should work in all instances
748 * for every type of file, including pipes, sockets, fifos, and regular files.
750 * XXX I don't think it works well for socket, pipe, and fifo.
753 aio_process_rw(struct kaiocb *job)
755 struct ucred *td_savedcred;
762 long msgsnd_st, msgsnd_end;
763 long msgrcv_st, msgrcv_end;
764 long oublock_st, oublock_end;
765 long inblock_st, inblock_end;
768 KASSERT(job->uaiocb.aio_lio_opcode == LIO_READ ||
769 job->uaiocb.aio_lio_opcode == LIO_WRITE,
770 ("%s: opcode %d", __func__, job->uaiocb.aio_lio_opcode));
772 aio_switch_vmspace(job);
774 td_savedcred = td->td_ucred;
775 td->td_ucred = job->cred;
779 aiov.iov_base = (void *)(uintptr_t)cb->aio_buf;
780 aiov.iov_len = cb->aio_nbytes;
782 auio.uio_iov = &aiov;
784 auio.uio_offset = cb->aio_offset;
785 auio.uio_resid = cb->aio_nbytes;
786 cnt = cb->aio_nbytes;
787 auio.uio_segflg = UIO_USERSPACE;
790 msgrcv_st = td->td_ru.ru_msgrcv;
791 msgsnd_st = td->td_ru.ru_msgsnd;
792 inblock_st = td->td_ru.ru_inblock;
793 oublock_st = td->td_ru.ru_oublock;
796 * aio_aqueue() acquires a reference to the file that is
797 * released in aio_free_entry().
799 if (cb->aio_lio_opcode == LIO_READ) {
800 auio.uio_rw = UIO_READ;
801 if (auio.uio_resid == 0)
804 error = fo_read(fp, &auio, fp->f_cred, FOF_OFFSET, td);
806 if (fp->f_type == DTYPE_VNODE)
808 auio.uio_rw = UIO_WRITE;
809 error = fo_write(fp, &auio, fp->f_cred, FOF_OFFSET, td);
811 msgrcv_end = td->td_ru.ru_msgrcv;
812 msgsnd_end = td->td_ru.ru_msgsnd;
813 inblock_end = td->td_ru.ru_inblock;
814 oublock_end = td->td_ru.ru_oublock;
816 job->msgrcv = msgrcv_end - msgrcv_st;
817 job->msgsnd = msgsnd_end - msgsnd_st;
818 job->inblock = inblock_end - inblock_st;
819 job->outblock = oublock_end - oublock_st;
821 if ((error) && (auio.uio_resid != cnt)) {
822 if (error == ERESTART || error == EINTR || error == EWOULDBLOCK)
824 if ((error == EPIPE) && (cb->aio_lio_opcode == LIO_WRITE)) {
825 PROC_LOCK(job->userproc);
826 kern_psignal(job->userproc, SIGPIPE);
827 PROC_UNLOCK(job->userproc);
831 cnt -= auio.uio_resid;
832 td->td_ucred = td_savedcred;
834 aio_complete(job, -1, error);
836 aio_complete(job, cnt, 0);
840 aio_process_sync(struct kaiocb *job)
842 struct thread *td = curthread;
843 struct ucred *td_savedcred = td->td_ucred;
844 struct file *fp = job->fd_file;
847 KASSERT(job->uaiocb.aio_lio_opcode == LIO_SYNC,
848 ("%s: opcode %d", __func__, job->uaiocb.aio_lio_opcode));
850 td->td_ucred = job->cred;
851 if (fp->f_vnode != NULL)
852 error = aio_fsync_vnode(td, fp->f_vnode);
853 td->td_ucred = td_savedcred;
855 aio_complete(job, -1, error);
857 aio_complete(job, 0, 0);
861 aio_process_mlock(struct kaiocb *job)
863 struct aiocb *cb = &job->uaiocb;
866 KASSERT(job->uaiocb.aio_lio_opcode == LIO_MLOCK,
867 ("%s: opcode %d", __func__, job->uaiocb.aio_lio_opcode));
869 aio_switch_vmspace(job);
870 error = kern_mlock(job->userproc, job->cred,
871 __DEVOLATILE(uintptr_t, cb->aio_buf), cb->aio_nbytes);
872 aio_complete(job, error != 0 ? -1 : 0, error);
876 aio_bio_done_notify(struct proc *userp, struct kaiocb *job)
878 struct aioliojob *lj;
880 struct kaiocb *sjob, *sjobn;
884 ki = userp->p_aioinfo;
885 AIO_LOCK_ASSERT(ki, MA_OWNED);
889 lj->lioj_finished_count++;
890 if (lj->lioj_count == lj->lioj_finished_count)
893 TAILQ_INSERT_TAIL(&ki->kaio_done, job, plist);
894 MPASS(job->jobflags & KAIOCB_FINISHED);
896 if (ki->kaio_flags & KAIO_RUNDOWN)
897 goto notification_done;
899 if (job->uaiocb.aio_sigevent.sigev_notify == SIGEV_SIGNAL ||
900 job->uaiocb.aio_sigevent.sigev_notify == SIGEV_THREAD_ID)
901 aio_sendsig(userp, &job->uaiocb.aio_sigevent, &job->ksi);
903 KNOTE_LOCKED(&job->klist, 1);
906 if (lj->lioj_signal.sigev_notify == SIGEV_KEVENT) {
907 lj->lioj_flags |= LIOJ_KEVENT_POSTED;
908 KNOTE_LOCKED(&lj->klist, 1);
910 if ((lj->lioj_flags & (LIOJ_SIGNAL|LIOJ_SIGNAL_POSTED))
912 && (lj->lioj_signal.sigev_notify == SIGEV_SIGNAL ||
913 lj->lioj_signal.sigev_notify == SIGEV_THREAD_ID)) {
914 aio_sendsig(userp, &lj->lioj_signal, &lj->lioj_ksi);
915 lj->lioj_flags |= LIOJ_SIGNAL_POSTED;
920 if (job->jobflags & KAIOCB_CHECKSYNC) {
921 schedule_fsync = false;
922 TAILQ_FOREACH_SAFE(sjob, &ki->kaio_syncqueue, list, sjobn) {
923 if (job->fd_file != sjob->fd_file ||
924 job->seqno >= sjob->seqno)
926 if (--sjob->pending > 0)
928 TAILQ_REMOVE(&ki->kaio_syncqueue, sjob, list);
929 if (!aio_clear_cancel_function_locked(sjob))
931 TAILQ_INSERT_TAIL(&ki->kaio_syncready, sjob, list);
932 schedule_fsync = true;
935 taskqueue_enqueue(taskqueue_aiod_kick,
936 &ki->kaio_sync_task);
938 if (ki->kaio_flags & KAIO_WAKEUP) {
939 ki->kaio_flags &= ~KAIO_WAKEUP;
940 wakeup(&userp->p_aioinfo);
945 aio_schedule_fsync(void *context, int pending)
952 while (!TAILQ_EMPTY(&ki->kaio_syncready)) {
953 job = TAILQ_FIRST(&ki->kaio_syncready);
954 TAILQ_REMOVE(&ki->kaio_syncready, job, list);
956 aio_schedule(job, aio_process_sync);
963 aio_cancel_cleared(struct kaiocb *job)
967 * The caller should hold the same queue lock held when
968 * aio_clear_cancel_function() was called and set this flag
969 * ensuring this check sees an up-to-date value. However,
970 * there is no way to assert that.
972 return ((job->jobflags & KAIOCB_CLEARED) != 0);
976 aio_clear_cancel_function_locked(struct kaiocb *job)
979 AIO_LOCK_ASSERT(job->userproc->p_aioinfo, MA_OWNED);
980 MPASS(job->cancel_fn != NULL);
981 if (job->jobflags & KAIOCB_CANCELLING) {
982 job->jobflags |= KAIOCB_CLEARED;
985 job->cancel_fn = NULL;
990 aio_clear_cancel_function(struct kaiocb *job)
995 ki = job->userproc->p_aioinfo;
997 ret = aio_clear_cancel_function_locked(job);
1003 aio_set_cancel_function_locked(struct kaiocb *job, aio_cancel_fn_t *func)
1006 AIO_LOCK_ASSERT(job->userproc->p_aioinfo, MA_OWNED);
1007 if (job->jobflags & KAIOCB_CANCELLED)
1009 job->cancel_fn = func;
1014 aio_set_cancel_function(struct kaiocb *job, aio_cancel_fn_t *func)
1016 struct kaioinfo *ki;
1019 ki = job->userproc->p_aioinfo;
1021 ret = aio_set_cancel_function_locked(job, func);
1027 aio_complete(struct kaiocb *job, long status, int error)
1029 struct kaioinfo *ki;
1032 job->uaiocb._aiocb_private.error = error;
1033 job->uaiocb._aiocb_private.status = status;
1035 userp = job->userproc;
1036 ki = userp->p_aioinfo;
1039 KASSERT(!(job->jobflags & KAIOCB_FINISHED),
1040 ("duplicate aio_complete"));
1041 job->jobflags |= KAIOCB_FINISHED;
1042 if ((job->jobflags & (KAIOCB_QUEUEING | KAIOCB_CANCELLING)) == 0) {
1043 TAILQ_REMOVE(&ki->kaio_jobqueue, job, plist);
1044 aio_bio_done_notify(userp, job);
1050 aio_cancel(struct kaiocb *job)
1053 aio_complete(job, -1, ECANCELED);
1057 aio_switch_vmspace(struct kaiocb *job)
1060 vmspace_switch_aio(job->userproc->p_vmspace);
1064 * The AIO daemon, most of the actual work is done in aio_process_*,
1065 * but the setup (and address space mgmt) is done in this routine.
1068 aio_daemon(void *_id)
1071 struct aioproc *aiop;
1072 struct kaioinfo *ki;
1074 struct vmspace *myvm;
1075 struct thread *td = curthread;
1076 int id = (intptr_t)_id;
1079 * Grab an extra reference on the daemon's vmspace so that it
1080 * doesn't get freed by jobs that switch to a different
1084 myvm = vmspace_acquire_ref(p);
1086 KASSERT(p->p_textvp == NULL, ("kthread has a textvp"));
1089 * Allocate and ready the aio control info. There is one aiop structure
1092 aiop = uma_zalloc(aiop_zone, M_WAITOK);
1094 aiop->aioprocflags = 0;
1097 * Wakeup parent process. (Parent sleeps to keep from blasting away
1098 * and creating too many daemons.)
1100 sema_post(&aio_newproc_sem);
1102 mtx_lock(&aio_job_mtx);
1105 * Take daemon off of free queue
1107 if (aiop->aioprocflags & AIOP_FREE) {
1108 TAILQ_REMOVE(&aio_freeproc, aiop, list);
1109 aiop->aioprocflags &= ~AIOP_FREE;
1115 while ((job = aio_selectjob(aiop)) != NULL) {
1116 mtx_unlock(&aio_job_mtx);
1118 ki = job->userproc->p_aioinfo;
1119 job->handle_fn(job);
1121 mtx_lock(&aio_job_mtx);
1122 /* Decrement the active job count. */
1123 ki->kaio_active_count--;
1127 * Disconnect from user address space.
1129 if (p->p_vmspace != myvm) {
1130 mtx_unlock(&aio_job_mtx);
1131 vmspace_switch_aio(myvm);
1132 mtx_lock(&aio_job_mtx);
1134 * We have to restart to avoid race, we only sleep if
1135 * no job can be selected.
1140 mtx_assert(&aio_job_mtx, MA_OWNED);
1142 TAILQ_INSERT_HEAD(&aio_freeproc, aiop, list);
1143 aiop->aioprocflags |= AIOP_FREE;
1146 * If daemon is inactive for a long time, allow it to exit,
1147 * thereby freeing resources.
1149 if (msleep(p, &aio_job_mtx, PRIBIO, "aiordy",
1150 aiod_lifetime) == EWOULDBLOCK && TAILQ_EMPTY(&aio_jobs) &&
1151 (aiop->aioprocflags & AIOP_FREE) &&
1152 num_aio_procs > target_aio_procs)
1155 TAILQ_REMOVE(&aio_freeproc, aiop, list);
1157 mtx_unlock(&aio_job_mtx);
1158 uma_zfree(aiop_zone, aiop);
1159 free_unr(aiod_unr, id);
1162 KASSERT(p->p_vmspace == myvm,
1163 ("AIOD: bad vmspace for exiting daemon"));
1164 KASSERT(myvm->vm_refcnt > 1,
1165 ("AIOD: bad vm refcnt for exiting daemon: %d", 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 physio 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_qphysio(struct proc *p, struct kaiocb *job)
1218 struct kaioinfo *ki;
1219 int error, ref, poff;
1225 if (!(cb->aio_lio_opcode == LIO_WRITE ||
1226 cb->aio_lio_opcode == LIO_READ))
1228 if (fp == NULL || fp->f_type != DTYPE_VNODE)
1232 if (vp->v_type != VCHR)
1234 if (vp->v_bufobj.bo_bsize == 0)
1236 if (cb->aio_nbytes % vp->v_bufobj.bo_bsize)
1240 csw = devvn_refthread(vp, &dev, &ref);
1244 if ((csw->d_flags & D_DISK) == 0) {
1248 if (cb->aio_nbytes > dev->si_iosize_max) {
1254 poff = (vm_offset_t)cb->aio_buf & PAGE_MASK;
1255 if ((dev->si_flags & SI_UNMAPPED) && unmapped_buf_allowed) {
1256 if (cb->aio_nbytes > MAXPHYS) {
1263 if (cb->aio_nbytes > MAXPHYS - poff) {
1267 if (ki->kaio_buffer_count >= max_buf_aio) {
1272 job->pbuf = pbuf = (struct buf *)getpbuf(NULL);
1275 ki->kaio_buffer_count++;
1278 job->bp = bp = g_alloc_bio();
1280 bp->bio_length = cb->aio_nbytes;
1281 bp->bio_bcount = cb->aio_nbytes;
1282 bp->bio_done = aio_physwakeup;
1283 bp->bio_data = (void *)(uintptr_t)cb->aio_buf;
1284 bp->bio_offset = cb->aio_offset;
1285 bp->bio_cmd = cb->aio_lio_opcode == LIO_WRITE ? BIO_WRITE : BIO_READ;
1287 bp->bio_caller1 = (void *)job;
1289 prot = VM_PROT_READ;
1290 if (cb->aio_lio_opcode == LIO_READ)
1291 prot |= VM_PROT_WRITE; /* Less backwards than it looks */
1292 job->npages = vm_fault_quick_hold_pages(&curproc->p_vmspace->vm_map,
1293 (vm_offset_t)bp->bio_data, bp->bio_length, prot, job->pages,
1294 nitems(job->pages));
1295 if (job->npages < 0) {
1300 pmap_qenter((vm_offset_t)pbuf->b_data,
1301 job->pages, job->npages);
1302 bp->bio_data = pbuf->b_data + poff;
1303 atomic_add_int(&num_buf_aio, 1);
1305 bp->bio_ma = job->pages;
1306 bp->bio_ma_n = job->npages;
1307 bp->bio_ma_offset = poff;
1308 bp->bio_data = unmapped_buf;
1309 bp->bio_flags |= BIO_UNMAPPED;
1310 atomic_add_int(&num_unmapped_aio, 1);
1313 /* Perform transfer. */
1314 csw->d_strategy(bp);
1315 dev_relthread(dev, ref);
1321 ki->kaio_buffer_count--;
1323 relpbuf(pbuf, NULL);
1329 dev_relthread(dev, ref);
1333 #ifdef COMPAT_FREEBSD6
1335 convert_old_sigevent(struct osigevent *osig, struct sigevent *nsig)
1339 * Only SIGEV_NONE, SIGEV_SIGNAL, and SIGEV_KEVENT are
1340 * supported by AIO with the old sigevent structure.
1342 nsig->sigev_notify = osig->sigev_notify;
1343 switch (nsig->sigev_notify) {
1347 nsig->sigev_signo = osig->__sigev_u.__sigev_signo;
1350 nsig->sigev_notify_kqueue =
1351 osig->__sigev_u.__sigev_notify_kqueue;
1352 nsig->sigev_value.sival_ptr = osig->sigev_value.sival_ptr;
1361 aiocb_copyin_old_sigevent(struct aiocb *ujob, struct aiocb *kjob)
1363 struct oaiocb *ojob;
1366 bzero(kjob, sizeof(struct aiocb));
1367 error = copyin(ujob, kjob, sizeof(struct oaiocb));
1370 ojob = (struct oaiocb *)kjob;
1371 return (convert_old_sigevent(&ojob->aio_sigevent, &kjob->aio_sigevent));
1376 aiocb_copyin(struct aiocb *ujob, struct aiocb *kjob)
1379 return (copyin(ujob, kjob, sizeof(struct aiocb)));
1383 aiocb_fetch_status(struct aiocb *ujob)
1386 return (fuword(&ujob->_aiocb_private.status));
1390 aiocb_fetch_error(struct aiocb *ujob)
1393 return (fuword(&ujob->_aiocb_private.error));
1397 aiocb_store_status(struct aiocb *ujob, long status)
1400 return (suword(&ujob->_aiocb_private.status, status));
1404 aiocb_store_error(struct aiocb *ujob, long error)
1407 return (suword(&ujob->_aiocb_private.error, error));
1411 aiocb_store_kernelinfo(struct aiocb *ujob, long jobref)
1414 return (suword(&ujob->_aiocb_private.kernelinfo, jobref));
1418 aiocb_store_aiocb(struct aiocb **ujobp, struct aiocb *ujob)
1421 return (suword(ujobp, (long)ujob));
1424 static struct aiocb_ops aiocb_ops = {
1425 .copyin = aiocb_copyin,
1426 .fetch_status = aiocb_fetch_status,
1427 .fetch_error = aiocb_fetch_error,
1428 .store_status = aiocb_store_status,
1429 .store_error = aiocb_store_error,
1430 .store_kernelinfo = aiocb_store_kernelinfo,
1431 .store_aiocb = aiocb_store_aiocb,
1434 #ifdef COMPAT_FREEBSD6
1435 static struct aiocb_ops aiocb_ops_osigevent = {
1436 .copyin = aiocb_copyin_old_sigevent,
1437 .fetch_status = aiocb_fetch_status,
1438 .fetch_error = aiocb_fetch_error,
1439 .store_status = aiocb_store_status,
1440 .store_error = aiocb_store_error,
1441 .store_kernelinfo = aiocb_store_kernelinfo,
1442 .store_aiocb = aiocb_store_aiocb,
1447 * Queue a new AIO request. Choosing either the threaded or direct physio VCHR
1448 * technique is done in this code.
1451 aio_aqueue(struct thread *td, struct aiocb *ujob, struct aioliojob *lj,
1452 int type, struct aiocb_ops *ops)
1454 struct proc *p = td->td_proc;
1455 cap_rights_t rights;
1458 struct kaioinfo *ki;
1466 if (p->p_aioinfo == NULL)
1467 aio_init_aioinfo(p);
1471 ops->store_status(ujob, -1);
1472 ops->store_error(ujob, 0);
1473 ops->store_kernelinfo(ujob, -1);
1475 if (num_queue_count >= max_queue_count ||
1476 ki->kaio_count >= max_aio_queue_per_proc) {
1477 ops->store_error(ujob, EAGAIN);
1481 job = uma_zalloc(aiocb_zone, M_WAITOK | M_ZERO);
1482 knlist_init_mtx(&job->klist, AIO_MTX(ki));
1484 error = ops->copyin(ujob, &job->uaiocb);
1486 ops->store_error(ujob, error);
1487 uma_zfree(aiocb_zone, job);
1491 if (job->uaiocb.aio_nbytes > IOSIZE_MAX) {
1492 uma_zfree(aiocb_zone, job);
1496 if (job->uaiocb.aio_sigevent.sigev_notify != SIGEV_KEVENT &&
1497 job->uaiocb.aio_sigevent.sigev_notify != SIGEV_SIGNAL &&
1498 job->uaiocb.aio_sigevent.sigev_notify != SIGEV_THREAD_ID &&
1499 job->uaiocb.aio_sigevent.sigev_notify != SIGEV_NONE) {
1500 ops->store_error(ujob, EINVAL);
1501 uma_zfree(aiocb_zone, job);
1505 if ((job->uaiocb.aio_sigevent.sigev_notify == SIGEV_SIGNAL ||
1506 job->uaiocb.aio_sigevent.sigev_notify == SIGEV_THREAD_ID) &&
1507 !_SIG_VALID(job->uaiocb.aio_sigevent.sigev_signo)) {
1508 uma_zfree(aiocb_zone, job);
1512 ksiginfo_init(&job->ksi);
1514 /* Save userspace address of the job info. */
1517 /* Get the opcode. */
1518 if (type != LIO_NOP)
1519 job->uaiocb.aio_lio_opcode = type;
1520 opcode = job->uaiocb.aio_lio_opcode;
1523 * Validate the opcode and fetch the file object for the specified
1526 * XXXRW: Moved the opcode validation up here so that we don't
1527 * retrieve a file descriptor without knowing what the capabiltity
1530 fd = job->uaiocb.aio_fildes;
1533 error = fget_write(td, fd,
1534 cap_rights_init(&rights, CAP_PWRITE), &fp);
1537 error = fget_read(td, fd,
1538 cap_rights_init(&rights, CAP_PREAD), &fp);
1541 error = fget(td, fd, cap_rights_init(&rights, CAP_FSYNC), &fp);
1547 error = fget(td, fd, cap_rights_init(&rights), &fp);
1553 uma_zfree(aiocb_zone, job);
1554 ops->store_error(ujob, error);
1558 if (opcode == LIO_SYNC && fp->f_vnode == NULL) {
1563 if ((opcode == LIO_READ || opcode == LIO_WRITE) &&
1564 job->uaiocb.aio_offset < 0 &&
1565 (fp->f_vnode == NULL || fp->f_vnode->v_type != VCHR)) {
1572 mtx_lock(&aio_job_mtx);
1574 job->seqno = jobseqno++;
1575 mtx_unlock(&aio_job_mtx);
1576 error = ops->store_kernelinfo(ujob, jid);
1581 job->uaiocb._aiocb_private.kernelinfo = (void *)(intptr_t)jid;
1583 if (opcode == LIO_NOP) {
1585 uma_zfree(aiocb_zone, job);
1589 if (job->uaiocb.aio_sigevent.sigev_notify != SIGEV_KEVENT)
1591 evflags = job->uaiocb.aio_sigevent.sigev_notify_kevent_flags;
1592 if ((evflags & ~(EV_CLEAR | EV_DISPATCH | EV_ONESHOT)) != 0) {
1596 kqfd = job->uaiocb.aio_sigevent.sigev_notify_kqueue;
1597 kev.ident = (uintptr_t)job->ujob;
1598 kev.filter = EVFILT_AIO;
1599 kev.flags = EV_ADD | EV_ENABLE | EV_FLAG1 | evflags;
1600 kev.data = (intptr_t)job;
1601 kev.udata = job->uaiocb.aio_sigevent.sigev_value.sival_ptr;
1602 error = kqfd_register(kqfd, &kev, td, 1);
1608 ops->store_error(ujob, EINPROGRESS);
1609 job->uaiocb._aiocb_private.error = EINPROGRESS;
1611 job->cred = crhold(td->td_ucred);
1612 job->jobflags = KAIOCB_QUEUEING;
1615 if (opcode == LIO_MLOCK) {
1616 aio_schedule(job, aio_process_mlock);
1618 } else if (fp->f_ops->fo_aio_queue == NULL)
1619 error = aio_queue_file(fp, job);
1621 error = fo_aio_queue(fp, job);
1626 job->jobflags &= ~KAIOCB_QUEUEING;
1627 TAILQ_INSERT_TAIL(&ki->kaio_all, job, allist);
1631 atomic_add_int(&num_queue_count, 1);
1632 if (job->jobflags & KAIOCB_FINISHED) {
1634 * The queue callback completed the request synchronously.
1635 * The bulk of the completion is deferred in that case
1638 aio_bio_done_notify(p, job);
1640 TAILQ_INSERT_TAIL(&ki->kaio_jobqueue, job, plist);
1645 knlist_delete(&job->klist, curthread, 0);
1648 uma_zfree(aiocb_zone, job);
1649 ops->store_error(ujob, error);
1654 aio_cancel_daemon_job(struct kaiocb *job)
1657 mtx_lock(&aio_job_mtx);
1658 if (!aio_cancel_cleared(job))
1659 TAILQ_REMOVE(&aio_jobs, job, list);
1660 mtx_unlock(&aio_job_mtx);
1665 aio_schedule(struct kaiocb *job, aio_handle_fn_t *func)
1668 mtx_lock(&aio_job_mtx);
1669 if (!aio_set_cancel_function(job, aio_cancel_daemon_job)) {
1670 mtx_unlock(&aio_job_mtx);
1674 job->handle_fn = func;
1675 TAILQ_INSERT_TAIL(&aio_jobs, job, list);
1676 aio_kick_nowait(job->userproc);
1677 mtx_unlock(&aio_job_mtx);
1681 aio_cancel_sync(struct kaiocb *job)
1683 struct kaioinfo *ki;
1685 ki = job->userproc->p_aioinfo;
1687 if (!aio_cancel_cleared(job))
1688 TAILQ_REMOVE(&ki->kaio_syncqueue, job, list);
1694 aio_queue_file(struct file *fp, struct kaiocb *job)
1696 struct kaioinfo *ki;
1697 struct kaiocb *job2;
1703 ki = job->userproc->p_aioinfo;
1704 error = aio_qphysio(job->userproc, job);
1708 if (fp->f_type == DTYPE_VNODE) {
1710 if (vp->v_type == VREG || vp->v_type == VDIR) {
1711 mp = fp->f_vnode->v_mount;
1712 if (mp == NULL || (mp->mnt_flag & MNT_LOCAL) != 0)
1716 if (!(safe || enable_aio_unsafe)) {
1717 counted_warning(&unsafe_warningcnt,
1718 "is attempting to use unsafe AIO requests");
1719 return (EOPNOTSUPP);
1722 switch (job->uaiocb.aio_lio_opcode) {
1725 aio_schedule(job, aio_process_rw);
1730 TAILQ_FOREACH(job2, &ki->kaio_jobqueue, plist) {
1731 if (job2->fd_file == job->fd_file &&
1732 job2->uaiocb.aio_lio_opcode != LIO_SYNC &&
1733 job2->seqno < job->seqno) {
1734 job2->jobflags |= KAIOCB_CHECKSYNC;
1738 if (job->pending != 0) {
1739 if (!aio_set_cancel_function_locked(job,
1745 TAILQ_INSERT_TAIL(&ki->kaio_syncqueue, job, list);
1750 aio_schedule(job, aio_process_sync);
1760 aio_kick_nowait(struct proc *userp)
1762 struct kaioinfo *ki = userp->p_aioinfo;
1763 struct aioproc *aiop;
1765 mtx_assert(&aio_job_mtx, MA_OWNED);
1766 if ((aiop = TAILQ_FIRST(&aio_freeproc)) != NULL) {
1767 TAILQ_REMOVE(&aio_freeproc, aiop, list);
1768 aiop->aioprocflags &= ~AIOP_FREE;
1769 wakeup(aiop->aioproc);
1770 } else if (num_aio_resv_start + num_aio_procs < max_aio_procs &&
1771 ki->kaio_active_count + num_aio_resv_start < max_aio_per_proc) {
1772 taskqueue_enqueue(taskqueue_aiod_kick, &ki->kaio_task);
1777 aio_kick(struct proc *userp)
1779 struct kaioinfo *ki = userp->p_aioinfo;
1780 struct aioproc *aiop;
1783 mtx_assert(&aio_job_mtx, MA_OWNED);
1785 if ((aiop = TAILQ_FIRST(&aio_freeproc)) != NULL) {
1786 TAILQ_REMOVE(&aio_freeproc, aiop, list);
1787 aiop->aioprocflags &= ~AIOP_FREE;
1788 wakeup(aiop->aioproc);
1789 } else if (num_aio_resv_start + num_aio_procs < max_aio_procs &&
1790 ki->kaio_active_count + num_aio_resv_start < max_aio_per_proc) {
1791 num_aio_resv_start++;
1792 mtx_unlock(&aio_job_mtx);
1793 error = aio_newproc(&num_aio_resv_start);
1794 mtx_lock(&aio_job_mtx);
1796 num_aio_resv_start--;
1806 aio_kick_helper(void *context, int pending)
1808 struct proc *userp = context;
1810 mtx_lock(&aio_job_mtx);
1811 while (--pending >= 0) {
1812 if (aio_kick(userp))
1815 mtx_unlock(&aio_job_mtx);
1819 * Support the aio_return system call, as a side-effect, kernel resources are
1823 kern_aio_return(struct thread *td, struct aiocb *ujob, struct aiocb_ops *ops)
1825 struct proc *p = td->td_proc;
1827 struct kaioinfo *ki;
1834 TAILQ_FOREACH(job, &ki->kaio_done, plist) {
1835 if (job->ujob == ujob)
1839 MPASS(job->jobflags & KAIOCB_FINISHED);
1840 status = job->uaiocb._aiocb_private.status;
1841 error = job->uaiocb._aiocb_private.error;
1842 td->td_retval[0] = status;
1843 td->td_ru.ru_oublock += job->outblock;
1844 td->td_ru.ru_inblock += job->inblock;
1845 td->td_ru.ru_msgsnd += job->msgsnd;
1846 td->td_ru.ru_msgrcv += job->msgrcv;
1847 aio_free_entry(job);
1849 ops->store_error(ujob, error);
1850 ops->store_status(ujob, status);
1859 sys_aio_return(struct thread *td, struct aio_return_args *uap)
1862 return (kern_aio_return(td, uap->aiocbp, &aiocb_ops));
1866 * Allow a process to wakeup when any of the I/O requests are completed.
1869 kern_aio_suspend(struct thread *td, int njoblist, struct aiocb **ujoblist,
1870 struct timespec *ts)
1872 struct proc *p = td->td_proc;
1874 struct kaioinfo *ki;
1875 struct kaiocb *firstjob, *job;
1880 if (ts->tv_nsec < 0 || ts->tv_nsec >= 1000000000)
1883 TIMESPEC_TO_TIMEVAL(&atv, ts);
1884 if (itimerfix(&atv))
1886 timo = tvtohz(&atv);
1900 TAILQ_FOREACH(job, &ki->kaio_all, allist) {
1901 for (i = 0; i < njoblist; i++) {
1902 if (job->ujob == ujoblist[i]) {
1903 if (firstjob == NULL)
1905 if (job->jobflags & KAIOCB_FINISHED)
1910 /* All tasks were finished. */
1911 if (firstjob == NULL)
1914 ki->kaio_flags |= KAIO_WAKEUP;
1915 error = msleep(&p->p_aioinfo, AIO_MTX(ki), PRIBIO | PCATCH,
1917 if (error == ERESTART)
1928 sys_aio_suspend(struct thread *td, struct aio_suspend_args *uap)
1930 struct timespec ts, *tsp;
1931 struct aiocb **ujoblist;
1934 if (uap->nent < 0 || uap->nent > max_aio_queue_per_proc)
1938 /* Get timespec struct. */
1939 if ((error = copyin(uap->timeout, &ts, sizeof(ts))) != 0)
1945 ujoblist = malloc(uap->nent * sizeof(ujoblist[0]), M_AIOS, M_WAITOK);
1946 error = copyin(uap->aiocbp, ujoblist, uap->nent * sizeof(ujoblist[0]));
1948 error = kern_aio_suspend(td, uap->nent, ujoblist, tsp);
1949 free(ujoblist, M_AIOS);
1954 * aio_cancel cancels any non-physio aio operations not currently in
1958 sys_aio_cancel(struct thread *td, struct aio_cancel_args *uap)
1960 struct proc *p = td->td_proc;
1961 struct kaioinfo *ki;
1962 struct kaiocb *job, *jobn;
1964 cap_rights_t rights;
1967 int notcancelled = 0;
1970 /* Lookup file object. */
1971 error = fget(td, uap->fd, cap_rights_init(&rights), &fp);
1979 if (fp->f_type == DTYPE_VNODE) {
1981 if (vn_isdisk(vp, &error)) {
1983 td->td_retval[0] = AIO_NOTCANCELED;
1989 TAILQ_FOREACH_SAFE(job, &ki->kaio_jobqueue, plist, jobn) {
1990 if ((uap->fd == job->uaiocb.aio_fildes) &&
1991 ((uap->aiocbp == NULL) ||
1992 (uap->aiocbp == job->ujob))) {
1993 if (aio_cancel_job(p, ki, job)) {
1998 if (uap->aiocbp != NULL)
2007 if (uap->aiocbp != NULL) {
2009 td->td_retval[0] = AIO_CANCELED;
2015 td->td_retval[0] = AIO_NOTCANCELED;
2020 td->td_retval[0] = AIO_CANCELED;
2024 td->td_retval[0] = AIO_ALLDONE;
2030 * aio_error is implemented in the kernel level for compatibility purposes
2031 * only. For a user mode async implementation, it would be best to do it in
2032 * a userland subroutine.
2035 kern_aio_error(struct thread *td, struct aiocb *ujob, struct aiocb_ops *ops)
2037 struct proc *p = td->td_proc;
2039 struct kaioinfo *ki;
2044 td->td_retval[0] = EINVAL;
2049 TAILQ_FOREACH(job, &ki->kaio_all, allist) {
2050 if (job->ujob == ujob) {
2051 if (job->jobflags & KAIOCB_FINISHED)
2053 job->uaiocb._aiocb_private.error;
2055 td->td_retval[0] = EINPROGRESS;
2063 * Hack for failure of aio_aqueue.
2065 status = ops->fetch_status(ujob);
2067 td->td_retval[0] = ops->fetch_error(ujob);
2071 td->td_retval[0] = EINVAL;
2076 sys_aio_error(struct thread *td, struct aio_error_args *uap)
2079 return (kern_aio_error(td, uap->aiocbp, &aiocb_ops));
2082 /* syscall - asynchronous read from a file (REALTIME) */
2083 #ifdef COMPAT_FREEBSD6
2085 freebsd6_aio_read(struct thread *td, struct freebsd6_aio_read_args *uap)
2088 return (aio_aqueue(td, (struct aiocb *)uap->aiocbp, NULL, LIO_READ,
2089 &aiocb_ops_osigevent));
2094 sys_aio_read(struct thread *td, struct aio_read_args *uap)
2097 return (aio_aqueue(td, uap->aiocbp, NULL, LIO_READ, &aiocb_ops));
2100 /* syscall - asynchronous write to a file (REALTIME) */
2101 #ifdef COMPAT_FREEBSD6
2103 freebsd6_aio_write(struct thread *td, struct freebsd6_aio_write_args *uap)
2106 return (aio_aqueue(td, (struct aiocb *)uap->aiocbp, NULL, LIO_WRITE,
2107 &aiocb_ops_osigevent));
2112 sys_aio_write(struct thread *td, struct aio_write_args *uap)
2115 return (aio_aqueue(td, uap->aiocbp, NULL, LIO_WRITE, &aiocb_ops));
2119 sys_aio_mlock(struct thread *td, struct aio_mlock_args *uap)
2122 return (aio_aqueue(td, uap->aiocbp, NULL, LIO_MLOCK, &aiocb_ops));
2126 kern_lio_listio(struct thread *td, int mode, struct aiocb * const *uacb_list,
2127 struct aiocb **acb_list, int nent, struct sigevent *sig,
2128 struct aiocb_ops *ops)
2130 struct proc *p = td->td_proc;
2132 struct kaioinfo *ki;
2133 struct aioliojob *lj;
2139 if ((mode != LIO_NOWAIT) && (mode != LIO_WAIT))
2142 if (nent < 0 || nent > max_aio_queue_per_proc)
2145 if (p->p_aioinfo == NULL)
2146 aio_init_aioinfo(p);
2150 lj = uma_zalloc(aiolio_zone, M_WAITOK);
2153 lj->lioj_finished_count = 0;
2154 knlist_init_mtx(&lj->klist, AIO_MTX(ki));
2155 ksiginfo_init(&lj->lioj_ksi);
2160 if (sig && (mode == LIO_NOWAIT)) {
2161 bcopy(sig, &lj->lioj_signal, sizeof(lj->lioj_signal));
2162 if (lj->lioj_signal.sigev_notify == SIGEV_KEVENT) {
2163 /* Assume only new style KEVENT */
2164 kev.filter = EVFILT_LIO;
2165 kev.flags = EV_ADD | EV_ENABLE | EV_FLAG1;
2166 kev.ident = (uintptr_t)uacb_list; /* something unique */
2167 kev.data = (intptr_t)lj;
2168 /* pass user defined sigval data */
2169 kev.udata = lj->lioj_signal.sigev_value.sival_ptr;
2170 error = kqfd_register(
2171 lj->lioj_signal.sigev_notify_kqueue, &kev, td, 1);
2173 uma_zfree(aiolio_zone, lj);
2176 } else if (lj->lioj_signal.sigev_notify == SIGEV_NONE) {
2178 } else if (lj->lioj_signal.sigev_notify == SIGEV_SIGNAL ||
2179 lj->lioj_signal.sigev_notify == SIGEV_THREAD_ID) {
2180 if (!_SIG_VALID(lj->lioj_signal.sigev_signo)) {
2181 uma_zfree(aiolio_zone, lj);
2184 lj->lioj_flags |= LIOJ_SIGNAL;
2186 uma_zfree(aiolio_zone, lj);
2192 TAILQ_INSERT_TAIL(&ki->kaio_liojoblist, lj, lioj_list);
2194 * Add extra aiocb count to avoid the lio to be freed
2195 * by other threads doing aio_waitcomplete or aio_return,
2196 * and prevent event from being sent until we have queued
2203 * Get pointers to the list of I/O requests.
2206 for (i = 0; i < nent; i++) {
2209 error = aio_aqueue(td, job, lj, LIO_NOP, ops);
2217 if (mode == LIO_WAIT) {
2218 while (lj->lioj_count - 1 != lj->lioj_finished_count) {
2219 ki->kaio_flags |= KAIO_WAKEUP;
2220 error = msleep(&p->p_aioinfo, AIO_MTX(ki),
2221 PRIBIO | PCATCH, "aiospn", 0);
2222 if (error == ERESTART)
2228 if (lj->lioj_count - 1 == lj->lioj_finished_count) {
2229 if (lj->lioj_signal.sigev_notify == SIGEV_KEVENT) {
2230 lj->lioj_flags |= LIOJ_KEVENT_POSTED;
2231 KNOTE_LOCKED(&lj->klist, 1);
2233 if ((lj->lioj_flags & (LIOJ_SIGNAL|LIOJ_SIGNAL_POSTED))
2235 && (lj->lioj_signal.sigev_notify == SIGEV_SIGNAL ||
2236 lj->lioj_signal.sigev_notify == SIGEV_THREAD_ID)) {
2237 aio_sendsig(p, &lj->lioj_signal,
2239 lj->lioj_flags |= LIOJ_SIGNAL_POSTED;
2244 if (lj->lioj_count == 0) {
2245 TAILQ_REMOVE(&ki->kaio_liojoblist, lj, lioj_list);
2246 knlist_delete(&lj->klist, curthread, 1);
2248 sigqueue_take(&lj->lioj_ksi);
2251 uma_zfree(aiolio_zone, lj);
2260 /* syscall - list directed I/O (REALTIME) */
2261 #ifdef COMPAT_FREEBSD6
2263 freebsd6_lio_listio(struct thread *td, struct freebsd6_lio_listio_args *uap)
2265 struct aiocb **acb_list;
2266 struct sigevent *sigp, sig;
2267 struct osigevent osig;
2270 if ((uap->mode != LIO_NOWAIT) && (uap->mode != LIO_WAIT))
2274 if (nent < 0 || nent > max_aio_queue_per_proc)
2277 if (uap->sig && (uap->mode == LIO_NOWAIT)) {
2278 error = copyin(uap->sig, &osig, sizeof(osig));
2281 error = convert_old_sigevent(&osig, &sig);
2288 acb_list = malloc(sizeof(struct aiocb *) * nent, M_LIO, M_WAITOK);
2289 error = copyin(uap->acb_list, acb_list, nent * sizeof(acb_list[0]));
2291 error = kern_lio_listio(td, uap->mode,
2292 (struct aiocb * const *)uap->acb_list, acb_list, nent, sigp,
2293 &aiocb_ops_osigevent);
2294 free(acb_list, M_LIO);
2299 /* syscall - list directed I/O (REALTIME) */
2301 sys_lio_listio(struct thread *td, struct lio_listio_args *uap)
2303 struct aiocb **acb_list;
2304 struct sigevent *sigp, sig;
2307 if ((uap->mode != LIO_NOWAIT) && (uap->mode != LIO_WAIT))
2311 if (nent < 0 || nent > max_aio_queue_per_proc)
2314 if (uap->sig && (uap->mode == LIO_NOWAIT)) {
2315 error = copyin(uap->sig, &sig, sizeof(sig));
2322 acb_list = malloc(sizeof(struct aiocb *) * nent, M_LIO, M_WAITOK);
2323 error = copyin(uap->acb_list, acb_list, nent * sizeof(acb_list[0]));
2325 error = kern_lio_listio(td, uap->mode, uap->acb_list, acb_list,
2326 nent, sigp, &aiocb_ops);
2327 free(acb_list, M_LIO);
2332 aio_physwakeup(struct bio *bp)
2334 struct kaiocb *job = (struct kaiocb *)bp->bio_caller1;
2336 struct kaioinfo *ki;
2340 /* Release mapping into kernel space. */
2341 userp = job->userproc;
2342 ki = userp->p_aioinfo;
2344 pmap_qremove((vm_offset_t)job->pbuf->b_data, job->npages);
2345 relpbuf(job->pbuf, NULL);
2347 atomic_subtract_int(&num_buf_aio, 1);
2349 ki->kaio_buffer_count--;
2352 atomic_subtract_int(&num_unmapped_aio, 1);
2353 vm_page_unhold_pages(job->pages, job->npages);
2357 nbytes = job->uaiocb.aio_nbytes - bp->bio_resid;
2359 if (bp->bio_flags & BIO_ERROR)
2360 error = bp->bio_error;
2361 nblks = btodb(nbytes);
2362 if (job->uaiocb.aio_lio_opcode == LIO_WRITE)
2363 job->outblock += nblks;
2365 job->inblock += nblks;
2368 aio_complete(job, -1, error);
2370 aio_complete(job, nbytes, 0);
2375 /* syscall - wait for the next completion of an aio request */
2377 kern_aio_waitcomplete(struct thread *td, struct aiocb **ujobp,
2378 struct timespec *ts, struct aiocb_ops *ops)
2380 struct proc *p = td->td_proc;
2382 struct kaioinfo *ki;
2388 ops->store_aiocb(ujobp, NULL);
2392 } else if (ts->tv_sec == 0 && ts->tv_nsec == 0) {
2395 if ((ts->tv_nsec < 0) || (ts->tv_nsec >= 1000000000))
2398 TIMESPEC_TO_TIMEVAL(&atv, ts);
2399 if (itimerfix(&atv))
2401 timo = tvtohz(&atv);
2404 if (p->p_aioinfo == NULL)
2405 aio_init_aioinfo(p);
2411 while ((job = TAILQ_FIRST(&ki->kaio_done)) == NULL) {
2413 error = EWOULDBLOCK;
2416 ki->kaio_flags |= KAIO_WAKEUP;
2417 error = msleep(&p->p_aioinfo, AIO_MTX(ki), PRIBIO | PCATCH,
2419 if (timo && error == ERESTART)
2426 MPASS(job->jobflags & KAIOCB_FINISHED);
2428 status = job->uaiocb._aiocb_private.status;
2429 error = job->uaiocb._aiocb_private.error;
2430 td->td_retval[0] = status;
2431 td->td_ru.ru_oublock += job->outblock;
2432 td->td_ru.ru_inblock += job->inblock;
2433 td->td_ru.ru_msgsnd += job->msgsnd;
2434 td->td_ru.ru_msgrcv += job->msgrcv;
2435 aio_free_entry(job);
2437 ops->store_aiocb(ujobp, ujob);
2438 ops->store_error(ujob, error);
2439 ops->store_status(ujob, status);
2447 sys_aio_waitcomplete(struct thread *td, struct aio_waitcomplete_args *uap)
2449 struct timespec ts, *tsp;
2453 /* Get timespec struct. */
2454 error = copyin(uap->timeout, &ts, sizeof(ts));
2461 return (kern_aio_waitcomplete(td, uap->aiocbp, tsp, &aiocb_ops));
2465 kern_aio_fsync(struct thread *td, int op, struct aiocb *ujob,
2466 struct aiocb_ops *ops)
2469 if (op != O_SYNC) /* XXX lack of O_DSYNC */
2471 return (aio_aqueue(td, ujob, NULL, LIO_SYNC, ops));
2475 sys_aio_fsync(struct thread *td, struct aio_fsync_args *uap)
2478 return (kern_aio_fsync(td, uap->op, uap->aiocbp, &aiocb_ops));
2481 /* kqueue attach function */
2483 filt_aioattach(struct knote *kn)
2487 job = (struct kaiocb *)(uintptr_t)kn->kn_sdata;
2490 * The job pointer must be validated before using it, so
2491 * registration is restricted to the kernel; the user cannot
2494 if ((kn->kn_flags & EV_FLAG1) == 0)
2496 kn->kn_ptr.p_aio = job;
2497 kn->kn_flags &= ~EV_FLAG1;
2499 knlist_add(&job->klist, kn, 0);
2504 /* kqueue detach function */
2506 filt_aiodetach(struct knote *kn)
2510 knl = &kn->kn_ptr.p_aio->klist;
2511 knl->kl_lock(knl->kl_lockarg);
2512 if (!knlist_empty(knl))
2513 knlist_remove(knl, kn, 1);
2514 knl->kl_unlock(knl->kl_lockarg);
2517 /* kqueue filter function */
2520 filt_aio(struct knote *kn, long hint)
2522 struct kaiocb *job = kn->kn_ptr.p_aio;
2524 kn->kn_data = job->uaiocb._aiocb_private.error;
2525 if (!(job->jobflags & KAIOCB_FINISHED))
2527 kn->kn_flags |= EV_EOF;
2531 /* kqueue attach function */
2533 filt_lioattach(struct knote *kn)
2535 struct aioliojob *lj;
2537 lj = (struct aioliojob *)(uintptr_t)kn->kn_sdata;
2540 * The aioliojob pointer must be validated before using it, so
2541 * registration is restricted to the kernel; the user cannot
2544 if ((kn->kn_flags & EV_FLAG1) == 0)
2546 kn->kn_ptr.p_lio = lj;
2547 kn->kn_flags &= ~EV_FLAG1;
2549 knlist_add(&lj->klist, kn, 0);
2554 /* kqueue detach function */
2556 filt_liodetach(struct knote *kn)
2560 knl = &kn->kn_ptr.p_lio->klist;
2561 knl->kl_lock(knl->kl_lockarg);
2562 if (!knlist_empty(knl))
2563 knlist_remove(knl, kn, 1);
2564 knl->kl_unlock(knl->kl_lockarg);
2567 /* kqueue filter function */
2570 filt_lio(struct knote *kn, long hint)
2572 struct aioliojob * lj = kn->kn_ptr.p_lio;
2574 return (lj->lioj_flags & LIOJ_KEVENT_POSTED);
2577 #ifdef COMPAT_FREEBSD32
2578 #include <sys/mount.h>
2579 #include <sys/socket.h>
2580 #include <compat/freebsd32/freebsd32.h>
2581 #include <compat/freebsd32/freebsd32_proto.h>
2582 #include <compat/freebsd32/freebsd32_signal.h>
2583 #include <compat/freebsd32/freebsd32_syscall.h>
2584 #include <compat/freebsd32/freebsd32_util.h>
2586 struct __aiocb_private32 {
2589 uint32_t kernelinfo;
2592 #ifdef COMPAT_FREEBSD6
2593 typedef struct oaiocb32 {
2594 int aio_fildes; /* File descriptor */
2595 uint64_t aio_offset __packed; /* File offset for I/O */
2596 uint32_t aio_buf; /* I/O buffer in process space */
2597 uint32_t aio_nbytes; /* Number of bytes for I/O */
2598 struct osigevent32 aio_sigevent; /* Signal to deliver */
2599 int aio_lio_opcode; /* LIO opcode */
2600 int aio_reqprio; /* Request priority -- ignored */
2601 struct __aiocb_private32 _aiocb_private;
2605 typedef struct aiocb32 {
2606 int32_t aio_fildes; /* File descriptor */
2607 uint64_t aio_offset __packed; /* File offset for I/O */
2608 uint32_t aio_buf; /* I/O buffer in process space */
2609 uint32_t aio_nbytes; /* Number of bytes for I/O */
2611 uint32_t __spare2__;
2612 int aio_lio_opcode; /* LIO opcode */
2613 int aio_reqprio; /* Request priority -- ignored */
2614 struct __aiocb_private32 _aiocb_private;
2615 struct sigevent32 aio_sigevent; /* Signal to deliver */
2618 #ifdef COMPAT_FREEBSD6
2620 convert_old_sigevent32(struct osigevent32 *osig, struct sigevent *nsig)
2624 * Only SIGEV_NONE, SIGEV_SIGNAL, and SIGEV_KEVENT are
2625 * supported by AIO with the old sigevent structure.
2627 CP(*osig, *nsig, sigev_notify);
2628 switch (nsig->sigev_notify) {
2632 nsig->sigev_signo = osig->__sigev_u.__sigev_signo;
2635 nsig->sigev_notify_kqueue =
2636 osig->__sigev_u.__sigev_notify_kqueue;
2637 PTRIN_CP(*osig, *nsig, sigev_value.sival_ptr);
2646 aiocb32_copyin_old_sigevent(struct aiocb *ujob, struct aiocb *kjob)
2648 struct oaiocb32 job32;
2651 bzero(kjob, sizeof(struct aiocb));
2652 error = copyin(ujob, &job32, sizeof(job32));
2656 CP(job32, *kjob, aio_fildes);
2657 CP(job32, *kjob, aio_offset);
2658 PTRIN_CP(job32, *kjob, aio_buf);
2659 CP(job32, *kjob, aio_nbytes);
2660 CP(job32, *kjob, aio_lio_opcode);
2661 CP(job32, *kjob, aio_reqprio);
2662 CP(job32, *kjob, _aiocb_private.status);
2663 CP(job32, *kjob, _aiocb_private.error);
2664 PTRIN_CP(job32, *kjob, _aiocb_private.kernelinfo);
2665 return (convert_old_sigevent32(&job32.aio_sigevent,
2666 &kjob->aio_sigevent));
2671 aiocb32_copyin(struct aiocb *ujob, struct aiocb *kjob)
2673 struct aiocb32 job32;
2676 error = copyin(ujob, &job32, sizeof(job32));
2679 CP(job32, *kjob, aio_fildes);
2680 CP(job32, *kjob, aio_offset);
2681 PTRIN_CP(job32, *kjob, aio_buf);
2682 CP(job32, *kjob, aio_nbytes);
2683 CP(job32, *kjob, aio_lio_opcode);
2684 CP(job32, *kjob, aio_reqprio);
2685 CP(job32, *kjob, _aiocb_private.status);
2686 CP(job32, *kjob, _aiocb_private.error);
2687 PTRIN_CP(job32, *kjob, _aiocb_private.kernelinfo);
2688 return (convert_sigevent32(&job32.aio_sigevent, &kjob->aio_sigevent));
2692 aiocb32_fetch_status(struct aiocb *ujob)
2694 struct aiocb32 *ujob32;
2696 ujob32 = (struct aiocb32 *)ujob;
2697 return (fuword32(&ujob32->_aiocb_private.status));
2701 aiocb32_fetch_error(struct aiocb *ujob)
2703 struct aiocb32 *ujob32;
2705 ujob32 = (struct aiocb32 *)ujob;
2706 return (fuword32(&ujob32->_aiocb_private.error));
2710 aiocb32_store_status(struct aiocb *ujob, long status)
2712 struct aiocb32 *ujob32;
2714 ujob32 = (struct aiocb32 *)ujob;
2715 return (suword32(&ujob32->_aiocb_private.status, status));
2719 aiocb32_store_error(struct aiocb *ujob, long error)
2721 struct aiocb32 *ujob32;
2723 ujob32 = (struct aiocb32 *)ujob;
2724 return (suword32(&ujob32->_aiocb_private.error, error));
2728 aiocb32_store_kernelinfo(struct aiocb *ujob, long jobref)
2730 struct aiocb32 *ujob32;
2732 ujob32 = (struct aiocb32 *)ujob;
2733 return (suword32(&ujob32->_aiocb_private.kernelinfo, jobref));
2737 aiocb32_store_aiocb(struct aiocb **ujobp, struct aiocb *ujob)
2740 return (suword32(ujobp, (long)ujob));
2743 static struct aiocb_ops aiocb32_ops = {
2744 .copyin = aiocb32_copyin,
2745 .fetch_status = aiocb32_fetch_status,
2746 .fetch_error = aiocb32_fetch_error,
2747 .store_status = aiocb32_store_status,
2748 .store_error = aiocb32_store_error,
2749 .store_kernelinfo = aiocb32_store_kernelinfo,
2750 .store_aiocb = aiocb32_store_aiocb,
2753 #ifdef COMPAT_FREEBSD6
2754 static struct aiocb_ops aiocb32_ops_osigevent = {
2755 .copyin = aiocb32_copyin_old_sigevent,
2756 .fetch_status = aiocb32_fetch_status,
2757 .fetch_error = aiocb32_fetch_error,
2758 .store_status = aiocb32_store_status,
2759 .store_error = aiocb32_store_error,
2760 .store_kernelinfo = aiocb32_store_kernelinfo,
2761 .store_aiocb = aiocb32_store_aiocb,
2766 freebsd32_aio_return(struct thread *td, struct freebsd32_aio_return_args *uap)
2769 return (kern_aio_return(td, (struct aiocb *)uap->aiocbp, &aiocb32_ops));
2773 freebsd32_aio_suspend(struct thread *td, struct freebsd32_aio_suspend_args *uap)
2775 struct timespec32 ts32;
2776 struct timespec ts, *tsp;
2777 struct aiocb **ujoblist;
2778 uint32_t *ujoblist32;
2781 if (uap->nent < 0 || uap->nent > max_aio_queue_per_proc)
2785 /* Get timespec struct. */
2786 if ((error = copyin(uap->timeout, &ts32, sizeof(ts32))) != 0)
2788 CP(ts32, ts, tv_sec);
2789 CP(ts32, ts, tv_nsec);
2794 ujoblist = malloc(uap->nent * sizeof(ujoblist[0]), M_AIOS, M_WAITOK);
2795 ujoblist32 = (uint32_t *)ujoblist;
2796 error = copyin(uap->aiocbp, ujoblist32, uap->nent *
2797 sizeof(ujoblist32[0]));
2799 for (i = uap->nent - 1; i >= 0; i--)
2800 ujoblist[i] = PTRIN(ujoblist32[i]);
2802 error = kern_aio_suspend(td, uap->nent, ujoblist, tsp);
2804 free(ujoblist, M_AIOS);
2809 freebsd32_aio_error(struct thread *td, struct freebsd32_aio_error_args *uap)
2812 return (kern_aio_error(td, (struct aiocb *)uap->aiocbp, &aiocb32_ops));
2815 #ifdef COMPAT_FREEBSD6
2817 freebsd6_freebsd32_aio_read(struct thread *td,
2818 struct freebsd6_freebsd32_aio_read_args *uap)
2821 return (aio_aqueue(td, (struct aiocb *)uap->aiocbp, NULL, LIO_READ,
2822 &aiocb32_ops_osigevent));
2827 freebsd32_aio_read(struct thread *td, struct freebsd32_aio_read_args *uap)
2830 return (aio_aqueue(td, (struct aiocb *)uap->aiocbp, NULL, LIO_READ,
2834 #ifdef COMPAT_FREEBSD6
2836 freebsd6_freebsd32_aio_write(struct thread *td,
2837 struct freebsd6_freebsd32_aio_write_args *uap)
2840 return (aio_aqueue(td, (struct aiocb *)uap->aiocbp, NULL, LIO_WRITE,
2841 &aiocb32_ops_osigevent));
2846 freebsd32_aio_write(struct thread *td, struct freebsd32_aio_write_args *uap)
2849 return (aio_aqueue(td, (struct aiocb *)uap->aiocbp, NULL, LIO_WRITE,
2854 freebsd32_aio_mlock(struct thread *td, struct freebsd32_aio_mlock_args *uap)
2857 return (aio_aqueue(td, (struct aiocb *)uap->aiocbp, NULL, LIO_MLOCK,
2862 freebsd32_aio_waitcomplete(struct thread *td,
2863 struct freebsd32_aio_waitcomplete_args *uap)
2865 struct timespec32 ts32;
2866 struct timespec ts, *tsp;
2870 /* Get timespec struct. */
2871 error = copyin(uap->timeout, &ts32, sizeof(ts32));
2874 CP(ts32, ts, tv_sec);
2875 CP(ts32, ts, tv_nsec);
2880 return (kern_aio_waitcomplete(td, (struct aiocb **)uap->aiocbp, tsp,
2885 freebsd32_aio_fsync(struct thread *td, struct freebsd32_aio_fsync_args *uap)
2888 return (kern_aio_fsync(td, uap->op, (struct aiocb *)uap->aiocbp,
2892 #ifdef COMPAT_FREEBSD6
2894 freebsd6_freebsd32_lio_listio(struct thread *td,
2895 struct freebsd6_freebsd32_lio_listio_args *uap)
2897 struct aiocb **acb_list;
2898 struct sigevent *sigp, sig;
2899 struct osigevent32 osig;
2900 uint32_t *acb_list32;
2903 if ((uap->mode != LIO_NOWAIT) && (uap->mode != LIO_WAIT))
2907 if (nent < 0 || nent > max_aio_queue_per_proc)
2910 if (uap->sig && (uap->mode == LIO_NOWAIT)) {
2911 error = copyin(uap->sig, &osig, sizeof(osig));
2914 error = convert_old_sigevent32(&osig, &sig);
2921 acb_list32 = malloc(sizeof(uint32_t) * nent, M_LIO, M_WAITOK);
2922 error = copyin(uap->acb_list, acb_list32, nent * sizeof(uint32_t));
2924 free(acb_list32, M_LIO);
2927 acb_list = malloc(sizeof(struct aiocb *) * nent, M_LIO, M_WAITOK);
2928 for (i = 0; i < nent; i++)
2929 acb_list[i] = PTRIN(acb_list32[i]);
2930 free(acb_list32, M_LIO);
2932 error = kern_lio_listio(td, uap->mode,
2933 (struct aiocb * const *)uap->acb_list, acb_list, nent, sigp,
2934 &aiocb32_ops_osigevent);
2935 free(acb_list, M_LIO);
2941 freebsd32_lio_listio(struct thread *td, struct freebsd32_lio_listio_args *uap)
2943 struct aiocb **acb_list;
2944 struct sigevent *sigp, sig;
2945 struct sigevent32 sig32;
2946 uint32_t *acb_list32;
2949 if ((uap->mode != LIO_NOWAIT) && (uap->mode != LIO_WAIT))
2953 if (nent < 0 || nent > max_aio_queue_per_proc)
2956 if (uap->sig && (uap->mode == LIO_NOWAIT)) {
2957 error = copyin(uap->sig, &sig32, sizeof(sig32));
2960 error = convert_sigevent32(&sig32, &sig);
2967 acb_list32 = malloc(sizeof(uint32_t) * nent, M_LIO, M_WAITOK);
2968 error = copyin(uap->acb_list, acb_list32, nent * sizeof(uint32_t));
2970 free(acb_list32, M_LIO);
2973 acb_list = malloc(sizeof(struct aiocb *) * nent, M_LIO, M_WAITOK);
2974 for (i = 0; i < nent; i++)
2975 acb_list[i] = PTRIN(acb_list32[i]);
2976 free(acb_list32, M_LIO);
2978 error = kern_lio_listio(td, uap->mode,
2979 (struct aiocb * const *)uap->acb_list, acb_list, nent, sigp,
2981 free(acb_list, M_LIO);