2 * Copyright (c) 1997 John S. Dyson. All rights reserved.
4 * Redistribution and use in source and binary forms, with or without
5 * modification, are permitted provided that the following conditions
7 * 1. Redistributions of source code must retain the above copyright
8 * notice, this list of conditions and the following disclaimer.
9 * 2. John S. Dyson's name may not be used to endorse or promote products
10 * derived from this software without specific prior written permission.
12 * DISCLAIMER: This code isn't warranted to do anything useful. Anything
13 * bad that happens because of using this software isn't the responsibility
14 * of the author. This software is distributed AS-IS.
18 * This file contains support for the POSIX 1003.1B AIO/LIO facility.
21 #include <sys/cdefs.h>
22 __FBSDID("$FreeBSD$");
24 #include "opt_compat.h"
26 #include <sys/param.h>
27 #include <sys/systm.h>
28 #include <sys/malloc.h>
31 #include <sys/eventhandler.h>
32 #include <sys/sysproto.h>
33 #include <sys/filedesc.h>
34 #include <sys/kernel.h>
35 #include <sys/module.h>
36 #include <sys/kthread.h>
37 #include <sys/fcntl.h>
39 #include <sys/limits.h>
41 #include <sys/mutex.h>
42 #include <sys/unistd.h>
43 #include <sys/posix4.h>
45 #include <sys/resourcevar.h>
46 #include <sys/signalvar.h>
47 #include <sys/protosw.h>
49 #include <sys/socket.h>
50 #include <sys/socketvar.h>
51 #include <sys/syscall.h>
52 #include <sys/sysent.h>
53 #include <sys/sysctl.h>
55 #include <sys/taskqueue.h>
56 #include <sys/vnode.h>
58 #include <sys/event.h>
59 #include <sys/mount.h>
61 #include <machine/atomic.h>
64 #include <vm/vm_extern.h>
66 #include <vm/vm_map.h>
67 #include <vm/vm_object.h>
71 #include "opt_vfs_aio.h"
74 * Counter for allocating reference ids to new jobs. Wrapped to 1 on
75 * overflow. (XXX will be removed soon.)
77 static u_long jobrefid;
80 * Counter for aio_fsync.
82 static uint64_t jobseqno;
85 #define JOBST_JOBQSOCK 1
86 #define JOBST_JOBQGLOBAL 2
87 #define JOBST_JOBRUNNING 3
88 #define JOBST_JOBFINISHED 4
89 #define JOBST_JOBQBUF 5
90 #define JOBST_JOBQSYNC 6
92 #ifndef MAX_AIO_PER_PROC
93 #define MAX_AIO_PER_PROC 32
96 #ifndef MAX_AIO_QUEUE_PER_PROC
97 #define MAX_AIO_QUEUE_PER_PROC 256 /* Bigger than AIO_LISTIO_MAX */
100 #ifndef MAX_AIO_PROCS
101 #define MAX_AIO_PROCS 32
104 #ifndef MAX_AIO_QUEUE
105 #define MAX_AIO_QUEUE 1024 /* Bigger than AIO_LISTIO_MAX */
108 #ifndef TARGET_AIO_PROCS
109 #define TARGET_AIO_PROCS 4
113 #define MAX_BUF_AIO 16
116 #ifndef AIOD_TIMEOUT_DEFAULT
117 #define AIOD_TIMEOUT_DEFAULT (10 * hz)
120 #ifndef AIOD_LIFETIME_DEFAULT
121 #define AIOD_LIFETIME_DEFAULT (30 * hz)
124 FEATURE(aio, "Asynchronous I/O");
126 static MALLOC_DEFINE(M_LIO, "lio", "listio aio control block list");
128 static SYSCTL_NODE(_vfs, OID_AUTO, aio, CTLFLAG_RW, 0, "Async IO management");
130 static int max_aio_procs = MAX_AIO_PROCS;
131 SYSCTL_INT(_vfs_aio, OID_AUTO, max_aio_procs,
132 CTLFLAG_RW, &max_aio_procs, 0,
133 "Maximum number of kernel threads to use for handling async IO ");
135 static int num_aio_procs = 0;
136 SYSCTL_INT(_vfs_aio, OID_AUTO, num_aio_procs,
137 CTLFLAG_RD, &num_aio_procs, 0,
138 "Number of presently active kernel threads for async IO");
141 * The code will adjust the actual number of AIO processes towards this
142 * number when it gets a chance.
144 static int target_aio_procs = TARGET_AIO_PROCS;
145 SYSCTL_INT(_vfs_aio, OID_AUTO, target_aio_procs, CTLFLAG_RW, &target_aio_procs,
146 0, "Preferred number of ready kernel threads for async IO");
148 static int max_queue_count = MAX_AIO_QUEUE;
149 SYSCTL_INT(_vfs_aio, OID_AUTO, max_aio_queue, CTLFLAG_RW, &max_queue_count, 0,
150 "Maximum number of aio requests to queue, globally");
152 static int num_queue_count = 0;
153 SYSCTL_INT(_vfs_aio, OID_AUTO, num_queue_count, CTLFLAG_RD, &num_queue_count, 0,
154 "Number of queued aio requests");
156 static int num_buf_aio = 0;
157 SYSCTL_INT(_vfs_aio, OID_AUTO, num_buf_aio, CTLFLAG_RD, &num_buf_aio, 0,
158 "Number of aio requests presently handled by the buf subsystem");
160 /* Number of async I/O thread in the process of being started */
161 /* XXX This should be local to aio_aqueue() */
162 static int num_aio_resv_start = 0;
164 static int aiod_timeout;
165 SYSCTL_INT(_vfs_aio, OID_AUTO, aiod_timeout, CTLFLAG_RW, &aiod_timeout, 0,
166 "Timeout value for synchronous aio operations");
168 static int aiod_lifetime;
169 SYSCTL_INT(_vfs_aio, OID_AUTO, aiod_lifetime, CTLFLAG_RW, &aiod_lifetime, 0,
170 "Maximum lifetime for idle aiod");
172 static int unloadable = 0;
173 SYSCTL_INT(_vfs_aio, OID_AUTO, unloadable, CTLFLAG_RW, &unloadable, 0,
174 "Allow unload of aio (not recommended)");
177 static int max_aio_per_proc = MAX_AIO_PER_PROC;
178 SYSCTL_INT(_vfs_aio, OID_AUTO, max_aio_per_proc, CTLFLAG_RW, &max_aio_per_proc,
179 0, "Maximum active aio requests per process (stored in the process)");
181 static int max_aio_queue_per_proc = MAX_AIO_QUEUE_PER_PROC;
182 SYSCTL_INT(_vfs_aio, OID_AUTO, max_aio_queue_per_proc, CTLFLAG_RW,
183 &max_aio_queue_per_proc, 0,
184 "Maximum queued aio requests per process (stored in the process)");
186 static int max_buf_aio = MAX_BUF_AIO;
187 SYSCTL_INT(_vfs_aio, OID_AUTO, max_buf_aio, CTLFLAG_RW, &max_buf_aio, 0,
188 "Maximum buf aio requests per process (stored in the process)");
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;
202 * Below is a key of locks used to protect each member of struct aiocblist
203 * aioliojob and kaioinfo and any backends.
205 * * - need not protected
206 * a - locked by kaioinfo lock
207 * b - locked by backend lock, the backend lock can be null in some cases,
208 * for example, BIO belongs to this type, in this case, proc lock is
210 * c - locked by aio_job_mtx, the lock for the generic file I/O backend.
214 * Current, there is only two backends: BIO and generic file I/O.
215 * socket I/O is served by generic file I/O, this is not a good idea, since
216 * disk file I/O and any other types without O_NONBLOCK flag can block daemon
217 * threads, if there is no thread to serve socket I/O, the socket I/O will be
218 * delayed too long or starved, we should create some threads dedicated to
219 * sockets to do non-blocking I/O, same for pipe and fifo, for these I/O
220 * systems we really need non-blocking interface, fiddling O_NONBLOCK in file
221 * structure is not safe because there is race between userland and aio
226 TAILQ_ENTRY(aiocblist) list; /* (b) internal list of for backend */
227 TAILQ_ENTRY(aiocblist) plist; /* (a) list of jobs for each backend */
228 TAILQ_ENTRY(aiocblist) allist; /* (a) list of all jobs in proc */
229 int jobflags; /* (a) job flags */
230 int jobstate; /* (b) job state */
231 int inputcharge; /* (*) input blockes */
232 int outputcharge; /* (*) output blockes */
233 struct buf *bp; /* (*) private to BIO backend,
236 struct proc *userproc; /* (*) user process */
237 struct ucred *cred; /* (*) active credential when created */
238 struct file *fd_file; /* (*) pointer to file structure */
239 struct aioliojob *lio; /* (*) optional lio job */
240 struct aiocb *uuaiocb; /* (*) pointer in userspace of aiocb */
241 struct knlist klist; /* (a) list of knotes */
242 struct aiocb uaiocb; /* (*) kernel I/O control block */
243 ksiginfo_t ksi; /* (a) realtime signal info */
244 struct task biotask; /* (*) private to BIO backend */
245 uint64_t seqno; /* (*) job number */
246 int pending; /* (a) number of pending I/O, aio_fsync only */
250 #define AIOCBLIST_DONE 0x01
251 #define AIOCBLIST_BUFDONE 0x02
252 #define AIOCBLIST_RUNDOWN 0x04
253 #define AIOCBLIST_CHECKSYNC 0x08
258 #define AIOP_FREE 0x1 /* proc on free queue */
260 struct aiothreadlist {
261 int aiothreadflags; /* (c) AIO proc flags */
262 TAILQ_ENTRY(aiothreadlist) list; /* (c) list of processes */
263 struct thread *aiothread; /* (*) the AIO thread */
267 * data-structure for lio signal management
270 int lioj_flags; /* (a) listio flags */
271 int lioj_count; /* (a) listio flags */
272 int lioj_finished_count; /* (a) listio flags */
273 struct sigevent lioj_signal; /* (a) signal on all I/O done */
274 TAILQ_ENTRY(aioliojob) lioj_list; /* (a) lio list */
275 struct knlist klist; /* (a) list of knotes */
276 ksiginfo_t lioj_ksi; /* (a) Realtime signal info */
279 #define LIOJ_SIGNAL 0x1 /* signal on all done (lio) */
280 #define LIOJ_SIGNAL_POSTED 0x2 /* signal has been posted */
281 #define LIOJ_KEVENT_POSTED 0x4 /* kevent triggered */
284 * per process aio data structure
287 struct mtx kaio_mtx; /* the lock to protect this struct */
288 int kaio_flags; /* (a) per process kaio flags */
289 int kaio_maxactive_count; /* (*) maximum number of AIOs */
290 int kaio_active_count; /* (c) number of currently used AIOs */
291 int kaio_qallowed_count; /* (*) maxiumu size of AIO queue */
292 int kaio_count; /* (a) size of AIO queue */
293 int kaio_ballowed_count; /* (*) maximum number of buffers */
294 int kaio_buffer_count; /* (a) number of physio buffers */
295 TAILQ_HEAD(,aiocblist) kaio_all; /* (a) all AIOs in the process */
296 TAILQ_HEAD(,aiocblist) kaio_done; /* (a) done queue for process */
297 TAILQ_HEAD(,aioliojob) kaio_liojoblist; /* (a) list of lio jobs */
298 TAILQ_HEAD(,aiocblist) kaio_jobqueue; /* (a) job queue for process */
299 TAILQ_HEAD(,aiocblist) kaio_bufqueue; /* (a) buffer job queue for process */
300 TAILQ_HEAD(,aiocblist) kaio_sockqueue; /* (a) queue for aios waiting on sockets,
303 TAILQ_HEAD(,aiocblist) kaio_syncqueue; /* (a) queue for aio_fsync */
304 struct task kaio_task; /* (*) task to kick aio threads */
307 #define AIO_LOCK(ki) mtx_lock(&(ki)->kaio_mtx)
308 #define AIO_UNLOCK(ki) mtx_unlock(&(ki)->kaio_mtx)
309 #define AIO_LOCK_ASSERT(ki, f) mtx_assert(&(ki)->kaio_mtx, (f))
310 #define AIO_MTX(ki) (&(ki)->kaio_mtx)
312 #define KAIO_RUNDOWN 0x1 /* process is being run down */
313 #define KAIO_WAKEUP 0x2 /* wakeup process when there is a significant event */
316 * Operations used to interact with userland aio control blocks.
317 * Different ABIs provide their own operations.
320 int (*copyin)(struct aiocb *ujob, struct aiocb *kjob);
321 long (*fetch_status)(struct aiocb *ujob);
322 long (*fetch_error)(struct aiocb *ujob);
323 int (*store_status)(struct aiocb *ujob, long status);
324 int (*store_error)(struct aiocb *ujob, long error);
325 int (*store_kernelinfo)(struct aiocb *ujob, long jobref);
326 int (*store_aiocb)(struct aiocb **ujobp, struct aiocb *ujob);
329 static TAILQ_HEAD(,aiothreadlist) aio_freeproc; /* (c) Idle daemons */
330 static struct sema aio_newproc_sem;
331 static struct mtx aio_job_mtx;
332 static struct mtx aio_sock_mtx;
333 static TAILQ_HEAD(,aiocblist) aio_jobs; /* (c) Async job list */
334 static struct unrhdr *aiod_unr;
336 void aio_init_aioinfo(struct proc *p);
337 static void aio_onceonly(void);
338 static int aio_free_entry(struct aiocblist *aiocbe);
339 static void aio_process(struct aiocblist *aiocbe);
340 static int aio_newproc(int *);
341 int aio_aqueue(struct thread *td, struct aiocb *job,
342 struct aioliojob *lio, int type, struct aiocb_ops *ops);
343 static void aio_physwakeup(struct buf *bp);
344 static void aio_proc_rundown(void *arg, struct proc *p);
345 static void aio_proc_rundown_exec(void *arg, struct proc *p, struct image_params *imgp);
346 static int aio_qphysio(struct proc *p, struct aiocblist *iocb);
347 static void biohelper(void *, int);
348 static void aio_daemon(void *param);
349 static void aio_swake_cb(struct socket *, struct sockbuf *);
350 static int aio_unload(void);
351 static void aio_bio_done_notify(struct proc *userp, struct aiocblist *aiocbe, int type);
354 static int aio_kick(struct proc *userp);
355 static void aio_kick_nowait(struct proc *userp);
356 static void aio_kick_helper(void *context, int pending);
357 static int filt_aioattach(struct knote *kn);
358 static void filt_aiodetach(struct knote *kn);
359 static int filt_aio(struct knote *kn, long hint);
360 static int filt_lioattach(struct knote *kn);
361 static void filt_liodetach(struct knote *kn);
362 static int filt_lio(struct knote *kn, long hint);
366 * kaio Per process async io info
367 * aiop async io thread data
368 * aiocb async io jobs
369 * aiol list io job pointer - internal to aio_suspend XXX
370 * aiolio list io jobs
372 static uma_zone_t kaio_zone, aiop_zone, aiocb_zone, aiol_zone, aiolio_zone;
374 /* kqueue filters for aio */
375 static struct filterops aio_filtops =
376 { 0, filt_aioattach, filt_aiodetach, filt_aio };
377 static struct filterops lio_filtops =
378 { 0, filt_lioattach, filt_liodetach, filt_lio };
380 static eventhandler_tag exit_tag, exec_tag;
382 TASKQUEUE_DEFINE_THREAD(aiod_bio);
385 * Main operations function for use as a kernel module.
388 aio_modload(struct module *module, int cmd, void *arg)
397 error = aio_unload();
408 static moduledata_t aio_mod = {
414 SYSCALL_MODULE_HELPER(aio_cancel);
415 SYSCALL_MODULE_HELPER(aio_error);
416 SYSCALL_MODULE_HELPER(aio_fsync);
417 SYSCALL_MODULE_HELPER(aio_read);
418 SYSCALL_MODULE_HELPER(aio_return);
419 SYSCALL_MODULE_HELPER(aio_suspend);
420 SYSCALL_MODULE_HELPER(aio_waitcomplete);
421 SYSCALL_MODULE_HELPER(aio_write);
422 SYSCALL_MODULE_HELPER(lio_listio);
423 SYSCALL_MODULE_HELPER(oaio_read);
424 SYSCALL_MODULE_HELPER(oaio_write);
425 SYSCALL_MODULE_HELPER(olio_listio);
427 DECLARE_MODULE(aio, aio_mod,
428 SI_SUB_VFS, SI_ORDER_ANY);
429 MODULE_VERSION(aio, 1);
432 * Startup initialization
438 /* XXX: should probably just use so->callback */
439 aio_swake = &aio_swake_cb;
440 exit_tag = EVENTHANDLER_REGISTER(process_exit, aio_proc_rundown, NULL,
441 EVENTHANDLER_PRI_ANY);
442 exec_tag = EVENTHANDLER_REGISTER(process_exec, aio_proc_rundown_exec, NULL,
443 EVENTHANDLER_PRI_ANY);
444 kqueue_add_filteropts(EVFILT_AIO, &aio_filtops);
445 kqueue_add_filteropts(EVFILT_LIO, &lio_filtops);
446 TAILQ_INIT(&aio_freeproc);
447 sema_init(&aio_newproc_sem, 0, "aio_new_proc");
448 mtx_init(&aio_job_mtx, "aio_job", NULL, MTX_DEF);
449 mtx_init(&aio_sock_mtx, "aio_sock", NULL, MTX_DEF);
450 TAILQ_INIT(&aio_jobs);
451 aiod_unr = new_unrhdr(1, INT_MAX, NULL);
452 kaio_zone = uma_zcreate("AIO", sizeof(struct kaioinfo), NULL, NULL,
453 NULL, NULL, UMA_ALIGN_PTR, UMA_ZONE_NOFREE);
454 aiop_zone = uma_zcreate("AIOP", sizeof(struct aiothreadlist), NULL,
455 NULL, NULL, NULL, UMA_ALIGN_PTR, UMA_ZONE_NOFREE);
456 aiocb_zone = uma_zcreate("AIOCB", sizeof(struct aiocblist), NULL, NULL,
457 NULL, NULL, UMA_ALIGN_PTR, UMA_ZONE_NOFREE);
458 aiol_zone = uma_zcreate("AIOL", AIO_LISTIO_MAX*sizeof(intptr_t) , NULL,
459 NULL, NULL, NULL, UMA_ALIGN_PTR, UMA_ZONE_NOFREE);
460 aiolio_zone = uma_zcreate("AIOLIO", sizeof(struct aioliojob), NULL,
461 NULL, NULL, NULL, UMA_ALIGN_PTR, UMA_ZONE_NOFREE);
462 aiod_timeout = AIOD_TIMEOUT_DEFAULT;
463 aiod_lifetime = AIOD_LIFETIME_DEFAULT;
465 async_io_version = _POSIX_VERSION;
466 p31b_setcfg(CTL_P1003_1B_AIO_LISTIO_MAX, AIO_LISTIO_MAX);
467 p31b_setcfg(CTL_P1003_1B_AIO_MAX, MAX_AIO_QUEUE);
468 p31b_setcfg(CTL_P1003_1B_AIO_PRIO_DELTA_MAX, 0);
472 * Callback for unload of AIO when used as a module.
480 * XXX: no unloads by default, it's too dangerous.
481 * perhaps we could do it if locked out callers and then
482 * did an aio_proc_rundown() on each process.
484 * jhb: aio_proc_rundown() needs to run on curproc though,
485 * so I don't think that would fly.
490 error = kqueue_del_filteropts(EVFILT_AIO);
493 error = kqueue_del_filteropts(EVFILT_LIO);
496 async_io_version = 0;
498 taskqueue_free(taskqueue_aiod_bio);
499 delete_unrhdr(aiod_unr);
500 uma_zdestroy(kaio_zone);
501 uma_zdestroy(aiop_zone);
502 uma_zdestroy(aiocb_zone);
503 uma_zdestroy(aiol_zone);
504 uma_zdestroy(aiolio_zone);
505 EVENTHANDLER_DEREGISTER(process_exit, exit_tag);
506 EVENTHANDLER_DEREGISTER(process_exec, exec_tag);
507 mtx_destroy(&aio_job_mtx);
508 mtx_destroy(&aio_sock_mtx);
509 sema_destroy(&aio_newproc_sem);
510 p31b_setcfg(CTL_P1003_1B_AIO_LISTIO_MAX, -1);
511 p31b_setcfg(CTL_P1003_1B_AIO_MAX, -1);
512 p31b_setcfg(CTL_P1003_1B_AIO_PRIO_DELTA_MAX, -1);
517 * Init the per-process aioinfo structure. The aioinfo limits are set
518 * per-process for user limit (resource) management.
521 aio_init_aioinfo(struct proc *p)
525 ki = uma_zalloc(kaio_zone, M_WAITOK);
526 mtx_init(&ki->kaio_mtx, "aiomtx", NULL, MTX_DEF);
528 ki->kaio_maxactive_count = max_aio_per_proc;
529 ki->kaio_active_count = 0;
530 ki->kaio_qallowed_count = max_aio_queue_per_proc;
532 ki->kaio_ballowed_count = max_buf_aio;
533 ki->kaio_buffer_count = 0;
534 TAILQ_INIT(&ki->kaio_all);
535 TAILQ_INIT(&ki->kaio_done);
536 TAILQ_INIT(&ki->kaio_jobqueue);
537 TAILQ_INIT(&ki->kaio_bufqueue);
538 TAILQ_INIT(&ki->kaio_liojoblist);
539 TAILQ_INIT(&ki->kaio_sockqueue);
540 TAILQ_INIT(&ki->kaio_syncqueue);
541 TASK_INIT(&ki->kaio_task, 0, aio_kick_helper, p);
543 if (p->p_aioinfo == NULL) {
548 mtx_destroy(&ki->kaio_mtx);
549 uma_zfree(kaio_zone, ki);
552 while (num_aio_procs < MIN(target_aio_procs, max_aio_procs))
557 aio_sendsig(struct proc *p, struct sigevent *sigev, ksiginfo_t *ksi)
563 ksi->ksi_code = SI_ASYNCIO;
564 ksi->ksi_flags |= KSI_EXT | KSI_INS;
565 ret = psignal_event(p, sigev, ksi);
572 * Free a job entry. Wait for completion if it is currently active, but don't
573 * delay forever. If we delay, we return a flag that says that we have to
574 * restart the queue scan.
577 aio_free_entry(struct aiocblist *aiocbe)
580 struct aioliojob *lj;
583 p = aiocbe->userproc;
588 AIO_LOCK_ASSERT(ki, MA_OWNED);
589 MPASS(aiocbe->jobstate == JOBST_JOBFINISHED);
591 atomic_subtract_int(&num_queue_count, 1);
594 MPASS(ki->kaio_count >= 0);
596 TAILQ_REMOVE(&ki->kaio_done, aiocbe, plist);
597 TAILQ_REMOVE(&ki->kaio_all, aiocbe, allist);
602 lj->lioj_finished_count--;
604 if (lj->lioj_count == 0) {
605 TAILQ_REMOVE(&ki->kaio_liojoblist, lj, lioj_list);
606 /* lio is going away, we need to destroy any knotes */
607 knlist_delete(&lj->klist, curthread, 1);
609 sigqueue_take(&lj->lioj_ksi);
611 uma_zfree(aiolio_zone, lj);
615 /* aiocbe is going away, we need to destroy any knotes */
616 knlist_delete(&aiocbe->klist, curthread, 1);
618 sigqueue_take(&aiocbe->ksi);
621 MPASS(aiocbe->bp == NULL);
622 aiocbe->jobstate = JOBST_NULL;
626 * The thread argument here is used to find the owning process
627 * and is also passed to fo_close() which may pass it to various
628 * places such as devsw close() routines. Because of that, we
629 * need a thread pointer from the process owning the job that is
630 * persistent and won't disappear out from under us or move to
633 * Currently, all the callers of this function call it to remove
634 * an aiocblist from the current process' job list either via a
635 * syscall or due to the current process calling exit() or
636 * execve(). Thus, we know that p == curproc. We also know that
637 * curthread can't exit since we are curthread.
639 * Therefore, we use curthread as the thread to pass to
640 * knlist_delete(). This does mean that it is possible for the
641 * thread pointer at close time to differ from the thread pointer
642 * at open time, but this is already true of file descriptors in
643 * a multithreaded process.
645 fdrop(aiocbe->fd_file, curthread);
646 crfree(aiocbe->cred);
647 uma_zfree(aiocb_zone, aiocbe);
654 aio_proc_rundown_exec(void *arg, struct proc *p, struct image_params *imgp __unused)
656 aio_proc_rundown(arg, p);
660 * Rundown the jobs for a given process.
663 aio_proc_rundown(void *arg, struct proc *p)
666 struct aioliojob *lj;
667 struct aiocblist *cbe, *cbn;
672 KASSERT(curthread->td_proc == p,
673 ("%s: called on non-curproc", __func__));
679 ki->kaio_flags |= KAIO_RUNDOWN;
684 * Try to cancel all pending requests. This code simulates
685 * aio_cancel on all pending I/O requests.
687 TAILQ_FOREACH_SAFE(cbe, &ki->kaio_jobqueue, plist, cbn) {
689 mtx_lock(&aio_job_mtx);
690 if (cbe->jobstate == JOBST_JOBQGLOBAL) {
691 TAILQ_REMOVE(&aio_jobs, cbe, list);
693 } else if (cbe->jobstate == JOBST_JOBQSOCK) {
695 MPASS(fp->f_type == DTYPE_SOCKET);
697 TAILQ_REMOVE(&so->so_aiojobq, cbe, list);
699 } else if (cbe->jobstate == JOBST_JOBQSYNC) {
700 TAILQ_REMOVE(&ki->kaio_syncqueue, cbe, list);
703 mtx_unlock(&aio_job_mtx);
706 cbe->jobstate = JOBST_JOBFINISHED;
707 cbe->uaiocb._aiocb_private.status = -1;
708 cbe->uaiocb._aiocb_private.error = ECANCELED;
709 TAILQ_REMOVE(&ki->kaio_jobqueue, cbe, plist);
710 aio_bio_done_notify(p, cbe, DONE_QUEUE);
714 /* Wait for all running I/O to be finished */
715 if (TAILQ_FIRST(&ki->kaio_bufqueue) ||
716 TAILQ_FIRST(&ki->kaio_jobqueue)) {
717 ki->kaio_flags |= KAIO_WAKEUP;
718 msleep(&p->p_aioinfo, AIO_MTX(ki), PRIBIO, "aioprn", hz);
722 /* Free all completed I/O requests. */
723 while ((cbe = TAILQ_FIRST(&ki->kaio_done)) != NULL)
726 while ((lj = TAILQ_FIRST(&ki->kaio_liojoblist)) != NULL) {
727 if (lj->lioj_count == 0) {
728 TAILQ_REMOVE(&ki->kaio_liojoblist, lj, lioj_list);
729 knlist_delete(&lj->klist, curthread, 1);
731 sigqueue_take(&lj->lioj_ksi);
733 uma_zfree(aiolio_zone, lj);
735 panic("LIO job not cleaned up: C:%d, FC:%d\n",
736 lj->lioj_count, lj->lioj_finished_count);
740 taskqueue_drain(taskqueue_aiod_bio, &ki->kaio_task);
741 mtx_destroy(&ki->kaio_mtx);
742 uma_zfree(kaio_zone, ki);
747 * Select a job to run (called by an AIO daemon).
749 static struct aiocblist *
750 aio_selectjob(struct aiothreadlist *aiop)
752 struct aiocblist *aiocbe;
756 mtx_assert(&aio_job_mtx, MA_OWNED);
757 TAILQ_FOREACH(aiocbe, &aio_jobs, list) {
758 userp = aiocbe->userproc;
759 ki = userp->p_aioinfo;
761 if (ki->kaio_active_count < ki->kaio_maxactive_count) {
762 TAILQ_REMOVE(&aio_jobs, aiocbe, list);
763 /* Account for currently active jobs. */
764 ki->kaio_active_count++;
765 aiocbe->jobstate = JOBST_JOBRUNNING;
773 * Move all data to a permanent storage device, this code
774 * simulates fsync syscall.
777 aio_fsync_vnode(struct thread *td, struct vnode *vp)
783 vfslocked = VFS_LOCK_GIANT(vp->v_mount);
784 if ((error = vn_start_write(vp, &mp, V_WAIT | PCATCH)) != 0)
786 vn_lock(vp, LK_EXCLUSIVE | LK_RETRY);
787 if (vp->v_object != NULL) {
788 VM_OBJECT_LOCK(vp->v_object);
789 vm_object_page_clean(vp->v_object, 0, 0, 0);
790 VM_OBJECT_UNLOCK(vp->v_object);
792 error = VOP_FSYNC(vp, MNT_WAIT, td);
795 vn_finished_write(mp);
797 VFS_UNLOCK_GIANT(vfslocked);
802 * The AIO processing activity. This is the code that does the I/O request for
803 * the non-physio version of the operations. The normal vn operations are used,
804 * and this code should work in all instances for every type of file, including
805 * pipes, sockets, fifos, and regular files.
807 * XXX I don't think it works well for socket, pipe, and fifo.
810 aio_process(struct aiocblist *aiocbe)
812 struct ucred *td_savedcred;
821 int oublock_st, oublock_end;
822 int inblock_st, inblock_end;
825 td_savedcred = td->td_ucred;
826 td->td_ucred = aiocbe->cred;
827 cb = &aiocbe->uaiocb;
828 fp = aiocbe->fd_file;
830 if (cb->aio_lio_opcode == LIO_SYNC) {
833 if (fp->f_vnode != NULL)
834 error = aio_fsync_vnode(td, fp->f_vnode);
835 cb->_aiocb_private.error = error;
836 cb->_aiocb_private.status = 0;
837 td->td_ucred = td_savedcred;
841 aiov.iov_base = (void *)(uintptr_t)cb->aio_buf;
842 aiov.iov_len = cb->aio_nbytes;
844 auio.uio_iov = &aiov;
846 auio.uio_offset = cb->aio_offset;
847 auio.uio_resid = cb->aio_nbytes;
848 cnt = cb->aio_nbytes;
849 auio.uio_segflg = UIO_USERSPACE;
852 inblock_st = td->td_ru.ru_inblock;
853 oublock_st = td->td_ru.ru_oublock;
855 * aio_aqueue() acquires a reference to the file that is
856 * released in aio_free_entry().
858 if (cb->aio_lio_opcode == LIO_READ) {
859 auio.uio_rw = UIO_READ;
860 if (auio.uio_resid == 0)
863 error = fo_read(fp, &auio, fp->f_cred, FOF_OFFSET, td);
865 if (fp->f_type == DTYPE_VNODE)
867 auio.uio_rw = UIO_WRITE;
868 error = fo_write(fp, &auio, fp->f_cred, FOF_OFFSET, td);
870 inblock_end = td->td_ru.ru_inblock;
871 oublock_end = td->td_ru.ru_oublock;
873 aiocbe->inputcharge = inblock_end - inblock_st;
874 aiocbe->outputcharge = oublock_end - oublock_st;
876 if ((error) && (auio.uio_resid != cnt)) {
877 if (error == ERESTART || error == EINTR || error == EWOULDBLOCK)
879 if ((error == EPIPE) && (cb->aio_lio_opcode == LIO_WRITE)) {
881 if (fp->f_type == DTYPE_SOCKET) {
883 if (so->so_options & SO_NOSIGPIPE)
887 PROC_LOCK(aiocbe->userproc);
888 psignal(aiocbe->userproc, SIGPIPE);
889 PROC_UNLOCK(aiocbe->userproc);
894 cnt -= auio.uio_resid;
895 cb->_aiocb_private.error = error;
896 cb->_aiocb_private.status = cnt;
897 td->td_ucred = td_savedcred;
901 aio_bio_done_notify(struct proc *userp, struct aiocblist *aiocbe, int type)
903 struct aioliojob *lj;
905 struct aiocblist *scb, *scbn;
908 ki = userp->p_aioinfo;
909 AIO_LOCK_ASSERT(ki, MA_OWNED);
913 lj->lioj_finished_count++;
914 if (lj->lioj_count == lj->lioj_finished_count)
917 if (type == DONE_QUEUE) {
918 aiocbe->jobflags |= AIOCBLIST_DONE;
920 aiocbe->jobflags |= AIOCBLIST_BUFDONE;
922 TAILQ_INSERT_TAIL(&ki->kaio_done, aiocbe, plist);
923 aiocbe->jobstate = JOBST_JOBFINISHED;
925 if (ki->kaio_flags & KAIO_RUNDOWN)
926 goto notification_done;
928 if (aiocbe->uaiocb.aio_sigevent.sigev_notify == SIGEV_SIGNAL ||
929 aiocbe->uaiocb.aio_sigevent.sigev_notify == SIGEV_THREAD_ID)
930 aio_sendsig(userp, &aiocbe->uaiocb.aio_sigevent, &aiocbe->ksi);
932 KNOTE_LOCKED(&aiocbe->klist, 1);
935 if (lj->lioj_signal.sigev_notify == SIGEV_KEVENT) {
936 lj->lioj_flags |= LIOJ_KEVENT_POSTED;
937 KNOTE_LOCKED(&lj->klist, 1);
939 if ((lj->lioj_flags & (LIOJ_SIGNAL|LIOJ_SIGNAL_POSTED))
941 && (lj->lioj_signal.sigev_notify == SIGEV_SIGNAL ||
942 lj->lioj_signal.sigev_notify == SIGEV_THREAD_ID)) {
943 aio_sendsig(userp, &lj->lioj_signal, &lj->lioj_ksi);
944 lj->lioj_flags |= LIOJ_SIGNAL_POSTED;
949 if (aiocbe->jobflags & AIOCBLIST_CHECKSYNC) {
950 TAILQ_FOREACH_SAFE(scb, &ki->kaio_syncqueue, list, scbn) {
951 if (aiocbe->fd_file == scb->fd_file &&
952 aiocbe->seqno < scb->seqno) {
953 if (--scb->pending == 0) {
954 mtx_lock(&aio_job_mtx);
955 scb->jobstate = JOBST_JOBQGLOBAL;
956 TAILQ_REMOVE(&ki->kaio_syncqueue, scb, list);
957 TAILQ_INSERT_TAIL(&aio_jobs, scb, list);
958 aio_kick_nowait(userp);
959 mtx_unlock(&aio_job_mtx);
964 if (ki->kaio_flags & KAIO_WAKEUP) {
965 ki->kaio_flags &= ~KAIO_WAKEUP;
966 wakeup(&userp->p_aioinfo);
971 * The AIO daemon, most of the actual work is done in aio_process,
972 * but the setup (and address space mgmt) is done in this routine.
975 aio_daemon(void *_id)
977 struct aiocblist *aiocbe;
978 struct aiothreadlist *aiop;
980 struct proc *curcp, *mycp, *userp;
981 struct vmspace *myvm, *tmpvm;
982 struct thread *td = curthread;
983 int id = (intptr_t)_id;
986 * Local copies of curproc (cp) and vmspace (myvm)
989 myvm = mycp->p_vmspace;
991 KASSERT(mycp->p_textvp == NULL, ("kthread has a textvp"));
994 * Allocate and ready the aio control info. There is one aiop structure
997 aiop = uma_zalloc(aiop_zone, M_WAITOK);
998 aiop->aiothread = td;
999 aiop->aiothreadflags = 0;
1001 /* The daemon resides in its own pgrp. */
1005 * Wakeup parent process. (Parent sleeps to keep from blasting away
1006 * and creating too many daemons.)
1008 sema_post(&aio_newproc_sem);
1010 mtx_lock(&aio_job_mtx);
1013 * curcp is the current daemon process context.
1014 * userp is the current user process context.
1019 * Take daemon off of free queue
1021 if (aiop->aiothreadflags & AIOP_FREE) {
1022 TAILQ_REMOVE(&aio_freeproc, aiop, list);
1023 aiop->aiothreadflags &= ~AIOP_FREE;
1029 while ((aiocbe = aio_selectjob(aiop)) != NULL) {
1030 mtx_unlock(&aio_job_mtx);
1031 userp = aiocbe->userproc;
1034 * Connect to process address space for user program.
1036 if (userp != curcp) {
1038 * Save the current address space that we are
1041 tmpvm = mycp->p_vmspace;
1044 * Point to the new user address space, and
1047 mycp->p_vmspace = userp->p_vmspace;
1048 atomic_add_int(&mycp->p_vmspace->vm_refcnt, 1);
1050 /* Activate the new mapping. */
1051 pmap_activate(FIRST_THREAD_IN_PROC(mycp));
1054 * If the old address space wasn't the daemons
1055 * own address space, then we need to remove the
1056 * daemon's reference from the other process
1057 * that it was acting on behalf of.
1059 if (tmpvm != myvm) {
1060 vmspace_free(tmpvm);
1065 ki = userp->p_aioinfo;
1067 /* Do the I/O function. */
1068 aio_process(aiocbe);
1070 mtx_lock(&aio_job_mtx);
1071 /* Decrement the active job count. */
1072 ki->kaio_active_count--;
1073 mtx_unlock(&aio_job_mtx);
1076 TAILQ_REMOVE(&ki->kaio_jobqueue, aiocbe, plist);
1077 aio_bio_done_notify(userp, aiocbe, DONE_QUEUE);
1080 mtx_lock(&aio_job_mtx);
1084 * Disconnect from user address space.
1086 if (curcp != mycp) {
1088 mtx_unlock(&aio_job_mtx);
1090 /* Get the user address space to disconnect from. */
1091 tmpvm = mycp->p_vmspace;
1093 /* Get original address space for daemon. */
1094 mycp->p_vmspace = myvm;
1096 /* Activate the daemon's address space. */
1097 pmap_activate(FIRST_THREAD_IN_PROC(mycp));
1099 if (tmpvm == myvm) {
1100 printf("AIOD: vmspace problem -- %d\n",
1104 /* Remove our vmspace reference. */
1105 vmspace_free(tmpvm);
1109 mtx_lock(&aio_job_mtx);
1111 * We have to restart to avoid race, we only sleep if
1112 * no job can be selected, that should be
1118 mtx_assert(&aio_job_mtx, MA_OWNED);
1120 TAILQ_INSERT_HEAD(&aio_freeproc, aiop, list);
1121 aiop->aiothreadflags |= AIOP_FREE;
1124 * If daemon is inactive for a long time, allow it to exit,
1125 * thereby freeing resources.
1127 if (msleep(aiop->aiothread, &aio_job_mtx, PRIBIO, "aiordy",
1129 if (TAILQ_EMPTY(&aio_jobs)) {
1130 if ((aiop->aiothreadflags & AIOP_FREE) &&
1131 (num_aio_procs > target_aio_procs)) {
1132 TAILQ_REMOVE(&aio_freeproc, aiop, list);
1134 mtx_unlock(&aio_job_mtx);
1135 uma_zfree(aiop_zone, aiop);
1136 free_unr(aiod_unr, id);
1138 if (mycp->p_vmspace->vm_refcnt <= 1) {
1139 printf("AIOD: bad vm refcnt for"
1140 " exiting daemon: %d\n",
1141 mycp->p_vmspace->vm_refcnt);
1149 mtx_unlock(&aio_job_mtx);
1150 panic("shouldn't be here\n");
1154 * Create a new AIO daemon. This is mostly a kernel-thread fork routine. The
1155 * AIO daemon modifies its environment itself.
1158 aio_newproc(int *start)
1164 id = alloc_unr(aiod_unr);
1165 error = kproc_create(aio_daemon, (void *)(intptr_t)id, &p,
1166 RFNOWAIT, 0, "aiod%d", id);
1169 * Wait until daemon is started.
1171 sema_wait(&aio_newproc_sem);
1172 mtx_lock(&aio_job_mtx);
1176 mtx_unlock(&aio_job_mtx);
1178 free_unr(aiod_unr, id);
1184 * Try the high-performance, low-overhead physio method for eligible
1185 * VCHR devices. This method doesn't use an aio helper thread, and
1186 * thus has very low overhead.
1188 * Assumes that the caller, aio_aqueue(), has incremented the file
1189 * structure's reference count, preventing its deallocation for the
1190 * duration of this call.
1193 aio_qphysio(struct proc *p, struct aiocblist *aiocbe)
1199 struct kaioinfo *ki;
1200 struct aioliojob *lj;
1203 cb = &aiocbe->uaiocb;
1204 fp = aiocbe->fd_file;
1206 if (fp->f_type != DTYPE_VNODE)
1212 * If its not a disk, we don't want to return a positive error.
1213 * It causes the aio code to not fall through to try the thread
1214 * way when you're talking to a regular file.
1216 if (!vn_isdisk(vp, &error)) {
1217 if (error == ENOTBLK)
1223 if (vp->v_bufobj.bo_bsize == 0)
1226 if (cb->aio_nbytes % vp->v_bufobj.bo_bsize)
1229 if (cb->aio_nbytes > vp->v_rdev->si_iosize_max)
1232 if (cb->aio_nbytes >
1233 MAXPHYS - (((vm_offset_t) cb->aio_buf) & PAGE_MASK))
1237 if (ki->kaio_buffer_count >= ki->kaio_ballowed_count)
1240 /* Create and build a buffer header for a transfer. */
1241 bp = (struct buf *)getpbuf(NULL);
1246 ki->kaio_buffer_count++;
1253 * Get a copy of the kva from the physical buffer.
1257 bp->b_bcount = cb->aio_nbytes;
1258 bp->b_bufsize = cb->aio_nbytes;
1259 bp->b_iodone = aio_physwakeup;
1260 bp->b_saveaddr = bp->b_data;
1261 bp->b_data = (void *)(uintptr_t)cb->aio_buf;
1262 bp->b_offset = cb->aio_offset;
1263 bp->b_iooffset = cb->aio_offset;
1264 bp->b_blkno = btodb(cb->aio_offset);
1265 bp->b_iocmd = cb->aio_lio_opcode == LIO_WRITE ? BIO_WRITE : BIO_READ;
1268 * Bring buffer into kernel space.
1270 if (vmapbuf(bp) < 0) {
1277 bp->b_caller1 = (void *)aiocbe;
1278 TAILQ_INSERT_TAIL(&ki->kaio_bufqueue, aiocbe, plist);
1279 TAILQ_INSERT_TAIL(&ki->kaio_all, aiocbe, allist);
1280 aiocbe->jobstate = JOBST_JOBQBUF;
1281 cb->_aiocb_private.status = cb->aio_nbytes;
1284 atomic_add_int(&num_queue_count, 1);
1285 atomic_add_int(&num_buf_aio, 1);
1289 TASK_INIT(&aiocbe->biotask, 0, biohelper, aiocbe);
1291 /* Perform transfer. */
1292 dev_strategy(vp->v_rdev, bp);
1298 ki->kaio_buffer_count--;
1308 * Wake up aio requests that may be serviceable now.
1311 aio_swake_cb(struct socket *so, struct sockbuf *sb)
1313 struct aiocblist *cb, *cbn;
1316 SOCKBUF_LOCK_ASSERT(sb);
1317 if (sb == &so->so_snd)
1322 sb->sb_flags &= ~SB_AIO;
1323 mtx_lock(&aio_job_mtx);
1324 TAILQ_FOREACH_SAFE(cb, &so->so_aiojobq, list, cbn) {
1325 if (opcode == cb->uaiocb.aio_lio_opcode) {
1326 if (cb->jobstate != JOBST_JOBQSOCK)
1327 panic("invalid queue value");
1329 * We don't have actual sockets backend yet,
1330 * so we simply move the requests to the generic
1333 TAILQ_REMOVE(&so->so_aiojobq, cb, list);
1334 TAILQ_INSERT_TAIL(&aio_jobs, cb, list);
1335 aio_kick_nowait(cb->userproc);
1338 mtx_unlock(&aio_job_mtx);
1342 convert_old_sigevent(struct osigevent *osig, struct sigevent *nsig)
1346 * Only SIGEV_NONE, SIGEV_SIGNAL, and SIGEV_KEVENT are
1347 * supported by AIO with the old sigevent structure.
1349 nsig->sigev_notify = osig->sigev_notify;
1350 switch (nsig->sigev_notify) {
1354 nsig->sigev_signo = osig->__sigev_u.__sigev_signo;
1357 nsig->sigev_notify_kqueue =
1358 osig->__sigev_u.__sigev_notify_kqueue;
1359 nsig->sigev_value.sival_ptr = osig->sigev_value.sival_ptr;
1368 aiocb_copyin_old_sigevent(struct aiocb *ujob, struct aiocb *kjob)
1370 struct oaiocb *ojob;
1373 bzero(kjob, sizeof(struct aiocb));
1374 error = copyin(ujob, kjob, sizeof(struct oaiocb));
1377 ojob = (struct oaiocb *)kjob;
1378 return (convert_old_sigevent(&ojob->aio_sigevent, &kjob->aio_sigevent));
1382 aiocb_copyin(struct aiocb *ujob, struct aiocb *kjob)
1385 return (copyin(ujob, kjob, sizeof(struct aiocb)));
1389 aiocb_fetch_status(struct aiocb *ujob)
1392 return (fuword(&ujob->_aiocb_private.status));
1396 aiocb_fetch_error(struct aiocb *ujob)
1399 return (fuword(&ujob->_aiocb_private.error));
1403 aiocb_store_status(struct aiocb *ujob, long status)
1406 return (suword(&ujob->_aiocb_private.status, status));
1410 aiocb_store_error(struct aiocb *ujob, long error)
1413 return (suword(&ujob->_aiocb_private.error, error));
1417 aiocb_store_kernelinfo(struct aiocb *ujob, long jobref)
1420 return (suword(&ujob->_aiocb_private.kernelinfo, jobref));
1424 aiocb_store_aiocb(struct aiocb **ujobp, struct aiocb *ujob)
1427 return (suword(ujobp, (long)ujob));
1430 static struct aiocb_ops aiocb_ops = {
1431 .copyin = aiocb_copyin,
1432 .fetch_status = aiocb_fetch_status,
1433 .fetch_error = aiocb_fetch_error,
1434 .store_status = aiocb_store_status,
1435 .store_error = aiocb_store_error,
1436 .store_kernelinfo = aiocb_store_kernelinfo,
1437 .store_aiocb = aiocb_store_aiocb,
1440 static struct aiocb_ops aiocb_ops_osigevent = {
1441 .copyin = aiocb_copyin_old_sigevent,
1442 .fetch_status = aiocb_fetch_status,
1443 .fetch_error = aiocb_fetch_error,
1444 .store_status = aiocb_store_status,
1445 .store_error = aiocb_store_error,
1446 .store_kernelinfo = aiocb_store_kernelinfo,
1447 .store_aiocb = aiocb_store_aiocb,
1451 * Queue a new AIO request. Choosing either the threaded or direct physio VCHR
1452 * technique is done in this code.
1455 aio_aqueue(struct thread *td, struct aiocb *job, struct aioliojob *lj,
1456 int type, struct aiocb_ops *ops)
1458 struct proc *p = td->td_proc;
1461 struct aiocblist *aiocbe, *cb;
1462 struct kaioinfo *ki;
1470 if (p->p_aioinfo == NULL)
1471 aio_init_aioinfo(p);
1475 ops->store_status(job, -1);
1476 ops->store_error(job, 0);
1477 ops->store_kernelinfo(job, -1);
1479 if (num_queue_count >= max_queue_count ||
1480 ki->kaio_count >= ki->kaio_qallowed_count) {
1481 ops->store_error(job, EAGAIN);
1485 aiocbe = uma_zalloc(aiocb_zone, M_WAITOK | M_ZERO);
1486 aiocbe->inputcharge = 0;
1487 aiocbe->outputcharge = 0;
1488 knlist_init_mtx(&aiocbe->klist, AIO_MTX(ki));
1490 error = ops->copyin(job, &aiocbe->uaiocb);
1492 ops->store_error(job, error);
1493 uma_zfree(aiocb_zone, aiocbe);
1497 if (aiocbe->uaiocb.aio_sigevent.sigev_notify != SIGEV_KEVENT &&
1498 aiocbe->uaiocb.aio_sigevent.sigev_notify != SIGEV_SIGNAL &&
1499 aiocbe->uaiocb.aio_sigevent.sigev_notify != SIGEV_THREAD_ID &&
1500 aiocbe->uaiocb.aio_sigevent.sigev_notify != SIGEV_NONE) {
1501 ops->store_error(job, EINVAL);
1502 uma_zfree(aiocb_zone, aiocbe);
1506 if ((aiocbe->uaiocb.aio_sigevent.sigev_notify == SIGEV_SIGNAL ||
1507 aiocbe->uaiocb.aio_sigevent.sigev_notify == SIGEV_THREAD_ID) &&
1508 !_SIG_VALID(aiocbe->uaiocb.aio_sigevent.sigev_signo)) {
1509 uma_zfree(aiocb_zone, aiocbe);
1513 ksiginfo_init(&aiocbe->ksi);
1515 /* Save userspace address of the job info. */
1516 aiocbe->uuaiocb = job;
1518 /* Get the opcode. */
1519 if (type != LIO_NOP)
1520 aiocbe->uaiocb.aio_lio_opcode = type;
1521 opcode = aiocbe->uaiocb.aio_lio_opcode;
1523 /* Fetch the file object for the specified file descriptor. */
1524 fd = aiocbe->uaiocb.aio_fildes;
1527 error = fget_write(td, fd, &fp);
1530 error = fget_read(td, fd, &fp);
1533 error = fget(td, fd, &fp);
1536 uma_zfree(aiocb_zone, aiocbe);
1537 ops->store_error(job, error);
1541 if (opcode == LIO_SYNC && fp->f_vnode == NULL) {
1546 if (opcode != LIO_SYNC && aiocbe->uaiocb.aio_offset == -1LL) {
1551 aiocbe->fd_file = fp;
1553 mtx_lock(&aio_job_mtx);
1555 aiocbe->seqno = jobseqno++;
1556 mtx_unlock(&aio_job_mtx);
1557 error = ops->store_kernelinfo(job, jid);
1562 aiocbe->uaiocb._aiocb_private.kernelinfo = (void *)(intptr_t)jid;
1564 if (opcode == LIO_NOP) {
1566 uma_zfree(aiocb_zone, aiocbe);
1569 if ((opcode != LIO_READ) && (opcode != LIO_WRITE) &&
1570 (opcode != LIO_SYNC)) {
1575 if (aiocbe->uaiocb.aio_sigevent.sigev_notify != SIGEV_KEVENT)
1577 kqfd = aiocbe->uaiocb.aio_sigevent.sigev_notify_kqueue;
1578 kev.ident = (uintptr_t)aiocbe->uuaiocb;
1579 kev.filter = EVFILT_AIO;
1580 kev.flags = EV_ADD | EV_ENABLE | EV_FLAG1;
1581 kev.data = (intptr_t)aiocbe;
1582 kev.udata = aiocbe->uaiocb.aio_sigevent.sigev_value.sival_ptr;
1583 error = kqfd_register(kqfd, &kev, td, 1);
1587 uma_zfree(aiocb_zone, aiocbe);
1588 ops->store_error(job, error);
1593 ops->store_error(job, EINPROGRESS);
1594 aiocbe->uaiocb._aiocb_private.error = EINPROGRESS;
1595 aiocbe->userproc = p;
1596 aiocbe->cred = crhold(td->td_ucred);
1597 aiocbe->jobflags = 0;
1600 if (opcode == LIO_SYNC)
1603 if (fp->f_type == DTYPE_SOCKET) {
1605 * Alternate queueing for socket ops: Reach down into the
1606 * descriptor to get the socket data. Then check to see if the
1607 * socket is ready to be read or written (based on the requested
1610 * If it is not ready for io, then queue the aiocbe on the
1611 * socket, and set the flags so we get a call when sbnotify()
1614 * Note if opcode is neither LIO_WRITE nor LIO_READ we lock
1615 * and unlock the snd sockbuf for no reason.
1618 sb = (opcode == LIO_READ) ? &so->so_rcv : &so->so_snd;
1620 if (((opcode == LIO_READ) && (!soreadable(so))) || ((opcode ==
1621 LIO_WRITE) && (!sowriteable(so)))) {
1622 sb->sb_flags |= SB_AIO;
1624 mtx_lock(&aio_job_mtx);
1625 TAILQ_INSERT_TAIL(&so->so_aiojobq, aiocbe, list);
1626 mtx_unlock(&aio_job_mtx);
1629 TAILQ_INSERT_TAIL(&ki->kaio_all, aiocbe, allist);
1630 TAILQ_INSERT_TAIL(&ki->kaio_jobqueue, aiocbe, plist);
1631 aiocbe->jobstate = JOBST_JOBQSOCK;
1637 atomic_add_int(&num_queue_count, 1);
1644 if ((error = aio_qphysio(p, aiocbe)) == 0)
1648 aiocbe->uaiocb._aiocb_private.error = error;
1649 ops->store_error(job, error);
1654 /* No buffer for daemon I/O. */
1656 atomic_add_int(&num_queue_count, 1);
1662 TAILQ_INSERT_TAIL(&ki->kaio_jobqueue, aiocbe, plist);
1663 TAILQ_INSERT_TAIL(&ki->kaio_all, aiocbe, allist);
1664 if (opcode == LIO_SYNC) {
1665 TAILQ_FOREACH(cb, &ki->kaio_jobqueue, plist) {
1666 if (cb->fd_file == aiocbe->fd_file &&
1667 cb->uaiocb.aio_lio_opcode != LIO_SYNC &&
1668 cb->seqno < aiocbe->seqno) {
1669 cb->jobflags |= AIOCBLIST_CHECKSYNC;
1673 TAILQ_FOREACH(cb, &ki->kaio_bufqueue, plist) {
1674 if (cb->fd_file == aiocbe->fd_file &&
1675 cb->uaiocb.aio_lio_opcode != LIO_SYNC &&
1676 cb->seqno < aiocbe->seqno) {
1677 cb->jobflags |= AIOCBLIST_CHECKSYNC;
1681 if (aiocbe->pending != 0) {
1682 TAILQ_INSERT_TAIL(&ki->kaio_syncqueue, aiocbe, list);
1683 aiocbe->jobstate = JOBST_JOBQSYNC;
1688 mtx_lock(&aio_job_mtx);
1689 TAILQ_INSERT_TAIL(&aio_jobs, aiocbe, list);
1690 aiocbe->jobstate = JOBST_JOBQGLOBAL;
1692 mtx_unlock(&aio_job_mtx);
1700 aio_kick_nowait(struct proc *userp)
1702 struct kaioinfo *ki = userp->p_aioinfo;
1703 struct aiothreadlist *aiop;
1705 mtx_assert(&aio_job_mtx, MA_OWNED);
1706 if ((aiop = TAILQ_FIRST(&aio_freeproc)) != NULL) {
1707 TAILQ_REMOVE(&aio_freeproc, aiop, list);
1708 aiop->aiothreadflags &= ~AIOP_FREE;
1709 wakeup(aiop->aiothread);
1710 } else if (((num_aio_resv_start + num_aio_procs) < max_aio_procs) &&
1711 ((ki->kaio_active_count + num_aio_resv_start) <
1712 ki->kaio_maxactive_count)) {
1713 taskqueue_enqueue(taskqueue_aiod_bio, &ki->kaio_task);
1718 aio_kick(struct proc *userp)
1720 struct kaioinfo *ki = userp->p_aioinfo;
1721 struct aiothreadlist *aiop;
1724 mtx_assert(&aio_job_mtx, MA_OWNED);
1726 if ((aiop = TAILQ_FIRST(&aio_freeproc)) != NULL) {
1727 TAILQ_REMOVE(&aio_freeproc, aiop, list);
1728 aiop->aiothreadflags &= ~AIOP_FREE;
1729 wakeup(aiop->aiothread);
1730 } else if (((num_aio_resv_start + num_aio_procs) < max_aio_procs) &&
1731 ((ki->kaio_active_count + num_aio_resv_start) <
1732 ki->kaio_maxactive_count)) {
1733 num_aio_resv_start++;
1734 mtx_unlock(&aio_job_mtx);
1735 error = aio_newproc(&num_aio_resv_start);
1736 mtx_lock(&aio_job_mtx);
1738 num_aio_resv_start--;
1748 aio_kick_helper(void *context, int pending)
1750 struct proc *userp = context;
1752 mtx_lock(&aio_job_mtx);
1753 while (--pending >= 0) {
1754 if (aio_kick(userp))
1757 mtx_unlock(&aio_job_mtx);
1761 * Support the aio_return system call, as a side-effect, kernel resources are
1765 kern_aio_return(struct thread *td, struct aiocb *uaiocb, struct aiocb_ops *ops)
1767 struct proc *p = td->td_proc;
1768 struct aiocblist *cb;
1769 struct kaioinfo *ki;
1776 TAILQ_FOREACH(cb, &ki->kaio_done, plist) {
1777 if (cb->uuaiocb == uaiocb)
1781 MPASS(cb->jobstate == JOBST_JOBFINISHED);
1782 status = cb->uaiocb._aiocb_private.status;
1783 error = cb->uaiocb._aiocb_private.error;
1784 td->td_retval[0] = status;
1785 if (cb->uaiocb.aio_lio_opcode == LIO_WRITE) {
1786 td->td_ru.ru_oublock += cb->outputcharge;
1787 cb->outputcharge = 0;
1788 } else if (cb->uaiocb.aio_lio_opcode == LIO_READ) {
1789 td->td_ru.ru_inblock += cb->inputcharge;
1790 cb->inputcharge = 0;
1794 ops->store_error(uaiocb, error);
1795 ops->store_status(uaiocb, status);
1804 aio_return(struct thread *td, struct aio_return_args *uap)
1807 return (kern_aio_return(td, uap->aiocbp, &aiocb_ops));
1811 * Allow a process to wakeup when any of the I/O requests are completed.
1814 kern_aio_suspend(struct thread *td, int njoblist, struct aiocb **ujoblist,
1815 struct timespec *ts)
1817 struct proc *p = td->td_proc;
1819 struct kaioinfo *ki;
1820 struct aiocblist *cb, *cbfirst;
1825 if (ts->tv_nsec < 0 || ts->tv_nsec >= 1000000000)
1828 TIMESPEC_TO_TIMEVAL(&atv, ts);
1829 if (itimerfix(&atv))
1831 timo = tvtohz(&atv);
1845 TAILQ_FOREACH(cb, &ki->kaio_all, allist) {
1846 for (i = 0; i < njoblist; i++) {
1847 if (cb->uuaiocb == ujoblist[i]) {
1848 if (cbfirst == NULL)
1850 if (cb->jobstate == JOBST_JOBFINISHED)
1855 /* All tasks were finished. */
1856 if (cbfirst == NULL)
1859 ki->kaio_flags |= KAIO_WAKEUP;
1860 error = msleep(&p->p_aioinfo, AIO_MTX(ki), PRIBIO | PCATCH,
1862 if (error == ERESTART)
1873 aio_suspend(struct thread *td, struct aio_suspend_args *uap)
1875 struct timespec ts, *tsp;
1876 struct aiocb **ujoblist;
1879 if (uap->nent < 0 || uap->nent > AIO_LISTIO_MAX)
1883 /* Get timespec struct. */
1884 if ((error = copyin(uap->timeout, &ts, sizeof(ts))) != 0)
1890 ujoblist = uma_zalloc(aiol_zone, M_WAITOK);
1891 error = copyin(uap->aiocbp, ujoblist, uap->nent * sizeof(ujoblist[0]));
1893 error = kern_aio_suspend(td, uap->nent, ujoblist, tsp);
1894 uma_zfree(aiol_zone, ujoblist);
1899 * aio_cancel cancels any non-physio aio operations not currently in
1903 aio_cancel(struct thread *td, struct aio_cancel_args *uap)
1905 struct proc *p = td->td_proc;
1906 struct kaioinfo *ki;
1907 struct aiocblist *cbe, *cbn;
1913 int notcancelled = 0;
1916 /* Lookup file object. */
1917 error = fget(td, uap->fd, &fp);
1925 if (fp->f_type == DTYPE_VNODE) {
1927 if (vn_isdisk(vp, &error)) {
1929 td->td_retval[0] = AIO_NOTCANCELED;
1935 TAILQ_FOREACH_SAFE(cbe, &ki->kaio_jobqueue, plist, cbn) {
1936 if ((uap->fd == cbe->uaiocb.aio_fildes) &&
1937 ((uap->aiocbp == NULL) ||
1938 (uap->aiocbp == cbe->uuaiocb))) {
1941 mtx_lock(&aio_job_mtx);
1942 if (cbe->jobstate == JOBST_JOBQGLOBAL) {
1943 TAILQ_REMOVE(&aio_jobs, cbe, list);
1945 } else if (cbe->jobstate == JOBST_JOBQSOCK) {
1946 MPASS(fp->f_type == DTYPE_SOCKET);
1948 TAILQ_REMOVE(&so->so_aiojobq, cbe, list);
1950 } else if (cbe->jobstate == JOBST_JOBQSYNC) {
1951 TAILQ_REMOVE(&ki->kaio_syncqueue, cbe, list);
1954 mtx_unlock(&aio_job_mtx);
1957 TAILQ_REMOVE(&ki->kaio_jobqueue, cbe, plist);
1958 cbe->uaiocb._aiocb_private.status = -1;
1959 cbe->uaiocb._aiocb_private.error = ECANCELED;
1960 aio_bio_done_notify(p, cbe, DONE_QUEUE);
1965 if (uap->aiocbp != NULL)
1974 if (uap->aiocbp != NULL) {
1976 td->td_retval[0] = AIO_CANCELED;
1982 td->td_retval[0] = AIO_NOTCANCELED;
1987 td->td_retval[0] = AIO_CANCELED;
1991 td->td_retval[0] = AIO_ALLDONE;
1997 * aio_error is implemented in the kernel level for compatibility purposes
1998 * only. For a user mode async implementation, it would be best to do it in
1999 * a userland subroutine.
2002 kern_aio_error(struct thread *td, struct aiocb *aiocbp, struct aiocb_ops *ops)
2004 struct proc *p = td->td_proc;
2005 struct aiocblist *cb;
2006 struct kaioinfo *ki;
2011 td->td_retval[0] = EINVAL;
2016 TAILQ_FOREACH(cb, &ki->kaio_all, allist) {
2017 if (cb->uuaiocb == aiocbp) {
2018 if (cb->jobstate == JOBST_JOBFINISHED)
2020 cb->uaiocb._aiocb_private.error;
2022 td->td_retval[0] = EINPROGRESS;
2030 * Hack for failure of aio_aqueue.
2032 status = ops->fetch_status(aiocbp);
2034 td->td_retval[0] = ops->fetch_error(aiocbp);
2038 td->td_retval[0] = EINVAL;
2043 aio_error(struct thread *td, struct aio_error_args *uap)
2046 return (kern_aio_error(td, uap->aiocbp, &aiocb_ops));
2049 /* syscall - asynchronous read from a file (REALTIME) */
2051 oaio_read(struct thread *td, struct oaio_read_args *uap)
2054 return (aio_aqueue(td, (struct aiocb *)uap->aiocbp, NULL, LIO_READ,
2055 &aiocb_ops_osigevent));
2059 aio_read(struct thread *td, struct aio_read_args *uap)
2062 return (aio_aqueue(td, uap->aiocbp, NULL, LIO_READ, &aiocb_ops));
2065 /* syscall - asynchronous write to a file (REALTIME) */
2067 oaio_write(struct thread *td, struct oaio_write_args *uap)
2070 return (aio_aqueue(td, (struct aiocb *)uap->aiocbp, NULL, LIO_WRITE,
2071 &aiocb_ops_osigevent));
2075 aio_write(struct thread *td, struct aio_write_args *uap)
2078 return (aio_aqueue(td, uap->aiocbp, NULL, LIO_WRITE, &aiocb_ops));
2082 kern_lio_listio(struct thread *td, int mode, struct aiocb * const *uacb_list,
2083 struct aiocb **acb_list, int nent, struct sigevent *sig,
2084 struct aiocb_ops *ops)
2086 struct proc *p = td->td_proc;
2088 struct kaioinfo *ki;
2089 struct aioliojob *lj;
2095 if ((mode != LIO_NOWAIT) && (mode != LIO_WAIT))
2098 if (nent < 0 || nent > AIO_LISTIO_MAX)
2101 if (p->p_aioinfo == NULL)
2102 aio_init_aioinfo(p);
2106 lj = uma_zalloc(aiolio_zone, M_WAITOK);
2109 lj->lioj_finished_count = 0;
2110 knlist_init_mtx(&lj->klist, AIO_MTX(ki));
2111 ksiginfo_init(&lj->lioj_ksi);
2116 if (sig && (mode == LIO_NOWAIT)) {
2117 bcopy(sig, &lj->lioj_signal, sizeof(lj->lioj_signal));
2118 if (lj->lioj_signal.sigev_notify == SIGEV_KEVENT) {
2119 /* Assume only new style KEVENT */
2120 kev.filter = EVFILT_LIO;
2121 kev.flags = EV_ADD | EV_ENABLE | EV_FLAG1;
2122 kev.ident = (uintptr_t)uacb_list; /* something unique */
2123 kev.data = (intptr_t)lj;
2124 /* pass user defined sigval data */
2125 kev.udata = lj->lioj_signal.sigev_value.sival_ptr;
2126 error = kqfd_register(
2127 lj->lioj_signal.sigev_notify_kqueue, &kev, td, 1);
2129 uma_zfree(aiolio_zone, lj);
2132 } else if (lj->lioj_signal.sigev_notify == SIGEV_NONE) {
2134 } else if (lj->lioj_signal.sigev_notify == SIGEV_SIGNAL ||
2135 lj->lioj_signal.sigev_notify == SIGEV_THREAD_ID) {
2136 if (!_SIG_VALID(lj->lioj_signal.sigev_signo)) {
2137 uma_zfree(aiolio_zone, lj);
2140 lj->lioj_flags |= LIOJ_SIGNAL;
2142 uma_zfree(aiolio_zone, lj);
2148 TAILQ_INSERT_TAIL(&ki->kaio_liojoblist, lj, lioj_list);
2150 * Add extra aiocb count to avoid the lio to be freed
2151 * by other threads doing aio_waitcomplete or aio_return,
2152 * and prevent event from being sent until we have queued
2159 * Get pointers to the list of I/O requests.
2162 for (i = 0; i < nent; i++) {
2165 error = aio_aqueue(td, iocb, lj, LIO_NOP, ops);
2173 if (mode == LIO_WAIT) {
2174 while (lj->lioj_count - 1 != lj->lioj_finished_count) {
2175 ki->kaio_flags |= KAIO_WAKEUP;
2176 error = msleep(&p->p_aioinfo, AIO_MTX(ki),
2177 PRIBIO | PCATCH, "aiospn", 0);
2178 if (error == ERESTART)
2184 if (lj->lioj_count - 1 == lj->lioj_finished_count) {
2185 if (lj->lioj_signal.sigev_notify == SIGEV_KEVENT) {
2186 lj->lioj_flags |= LIOJ_KEVENT_POSTED;
2187 KNOTE_LOCKED(&lj->klist, 1);
2189 if ((lj->lioj_flags & (LIOJ_SIGNAL|LIOJ_SIGNAL_POSTED))
2191 && (lj->lioj_signal.sigev_notify == SIGEV_SIGNAL ||
2192 lj->lioj_signal.sigev_notify == SIGEV_THREAD_ID)) {
2193 aio_sendsig(p, &lj->lioj_signal,
2195 lj->lioj_flags |= LIOJ_SIGNAL_POSTED;
2200 if (lj->lioj_count == 0) {
2201 TAILQ_REMOVE(&ki->kaio_liojoblist, lj, lioj_list);
2202 knlist_delete(&lj->klist, curthread, 1);
2204 sigqueue_take(&lj->lioj_ksi);
2207 uma_zfree(aiolio_zone, lj);
2216 /* syscall - list directed I/O (REALTIME) */
2218 olio_listio(struct thread *td, struct olio_listio_args *uap)
2220 struct aiocb **acb_list;
2221 struct sigevent *sigp, sig;
2222 struct osigevent osig;
2225 if ((uap->mode != LIO_NOWAIT) && (uap->mode != LIO_WAIT))
2229 if (nent < 0 || nent > AIO_LISTIO_MAX)
2232 if (uap->sig && (uap->mode == LIO_NOWAIT)) {
2233 error = copyin(uap->sig, &osig, sizeof(osig));
2236 error = convert_old_sigevent(&osig, &sig);
2243 acb_list = malloc(sizeof(struct aiocb *) * nent, M_LIO, M_WAITOK);
2244 error = copyin(uap->acb_list, acb_list, nent * sizeof(acb_list[0]));
2246 error = kern_lio_listio(td, uap->mode,
2247 (struct aiocb * const *)uap->acb_list, acb_list, nent, sigp,
2248 &aiocb_ops_osigevent);
2249 free(acb_list, M_LIO);
2253 /* syscall - list directed I/O (REALTIME) */
2255 lio_listio(struct thread *td, struct lio_listio_args *uap)
2257 struct aiocb **acb_list;
2258 struct sigevent *sigp, sig;
2261 if ((uap->mode != LIO_NOWAIT) && (uap->mode != LIO_WAIT))
2265 if (nent < 0 || nent > AIO_LISTIO_MAX)
2268 if (uap->sig && (uap->mode == LIO_NOWAIT)) {
2269 error = copyin(uap->sig, &sig, sizeof(sig));
2276 acb_list = malloc(sizeof(struct aiocb *) * nent, M_LIO, M_WAITOK);
2277 error = copyin(uap->acb_list, acb_list, nent * sizeof(acb_list[0]));
2279 error = kern_lio_listio(td, uap->mode, uap->acb_list, acb_list,
2280 nent, sigp, &aiocb_ops);
2281 free(acb_list, M_LIO);
2286 * Called from interrupt thread for physio, we should return as fast
2287 * as possible, so we schedule a biohelper task.
2290 aio_physwakeup(struct buf *bp)
2292 struct aiocblist *aiocbe;
2294 aiocbe = (struct aiocblist *)bp->b_caller1;
2295 taskqueue_enqueue(taskqueue_aiod_bio, &aiocbe->biotask);
2299 * Task routine to perform heavy tasks, process wakeup, and signals.
2302 biohelper(void *context, int pending)
2304 struct aiocblist *aiocbe = context;
2307 struct kaioinfo *ki;
2311 userp = aiocbe->userproc;
2312 ki = userp->p_aioinfo;
2314 aiocbe->uaiocb._aiocb_private.status -= bp->b_resid;
2315 aiocbe->uaiocb._aiocb_private.error = 0;
2316 if (bp->b_ioflags & BIO_ERROR)
2317 aiocbe->uaiocb._aiocb_private.error = bp->b_error;
2318 nblks = btodb(aiocbe->uaiocb.aio_nbytes);
2319 if (aiocbe->uaiocb.aio_lio_opcode == LIO_WRITE)
2320 aiocbe->outputcharge += nblks;
2322 aiocbe->inputcharge += nblks;
2324 TAILQ_REMOVE(&userp->p_aioinfo->kaio_bufqueue, aiocbe, plist);
2325 ki->kaio_buffer_count--;
2326 aio_bio_done_notify(userp, aiocbe, DONE_BUF);
2329 /* Release mapping into kernel space. */
2332 atomic_subtract_int(&num_buf_aio, 1);
2335 /* syscall - wait for the next completion of an aio request */
2337 kern_aio_waitcomplete(struct thread *td, struct aiocb **aiocbp,
2338 struct timespec *ts, struct aiocb_ops *ops)
2340 struct proc *p = td->td_proc;
2342 struct kaioinfo *ki;
2343 struct aiocblist *cb;
2344 struct aiocb *uuaiocb;
2345 int error, status, timo;
2347 ops->store_aiocb(aiocbp, NULL);
2351 if ((ts->tv_nsec < 0) || (ts->tv_nsec >= 1000000000))
2354 TIMESPEC_TO_TIMEVAL(&atv, ts);
2355 if (itimerfix(&atv))
2357 timo = tvtohz(&atv);
2360 if (p->p_aioinfo == NULL)
2361 aio_init_aioinfo(p);
2367 while ((cb = TAILQ_FIRST(&ki->kaio_done)) == NULL) {
2368 ki->kaio_flags |= KAIO_WAKEUP;
2369 error = msleep(&p->p_aioinfo, AIO_MTX(ki), PRIBIO | PCATCH,
2371 if (timo && error == ERESTART)
2378 MPASS(cb->jobstate == JOBST_JOBFINISHED);
2379 uuaiocb = cb->uuaiocb;
2380 status = cb->uaiocb._aiocb_private.status;
2381 error = cb->uaiocb._aiocb_private.error;
2382 td->td_retval[0] = status;
2383 if (cb->uaiocb.aio_lio_opcode == LIO_WRITE) {
2384 td->td_ru.ru_oublock += cb->outputcharge;
2385 cb->outputcharge = 0;
2386 } else if (cb->uaiocb.aio_lio_opcode == LIO_READ) {
2387 td->td_ru.ru_inblock += cb->inputcharge;
2388 cb->inputcharge = 0;
2392 ops->store_aiocb(aiocbp, uuaiocb);
2393 ops->store_error(uuaiocb, error);
2394 ops->store_status(uuaiocb, status);
2402 aio_waitcomplete(struct thread *td, struct aio_waitcomplete_args *uap)
2404 struct timespec ts, *tsp;
2408 /* Get timespec struct. */
2409 error = copyin(uap->timeout, &ts, sizeof(ts));
2416 return (kern_aio_waitcomplete(td, uap->aiocbp, tsp, &aiocb_ops));
2420 kern_aio_fsync(struct thread *td, int op, struct aiocb *aiocbp,
2421 struct aiocb_ops *ops)
2423 struct proc *p = td->td_proc;
2424 struct kaioinfo *ki;
2426 if (op != O_SYNC) /* XXX lack of O_DSYNC */
2430 aio_init_aioinfo(p);
2431 return (aio_aqueue(td, aiocbp, NULL, LIO_SYNC, ops));
2435 aio_fsync(struct thread *td, struct aio_fsync_args *uap)
2438 return (kern_aio_fsync(td, uap->op, uap->aiocbp, &aiocb_ops));
2441 /* kqueue attach function */
2443 filt_aioattach(struct knote *kn)
2445 struct aiocblist *aiocbe = (struct aiocblist *)kn->kn_sdata;
2448 * The aiocbe pointer must be validated before using it, so
2449 * registration is restricted to the kernel; the user cannot
2452 if ((kn->kn_flags & EV_FLAG1) == 0)
2454 kn->kn_ptr.p_aio = aiocbe;
2455 kn->kn_flags &= ~EV_FLAG1;
2457 knlist_add(&aiocbe->klist, kn, 0);
2462 /* kqueue detach function */
2464 filt_aiodetach(struct knote *kn)
2466 struct aiocblist *aiocbe = kn->kn_ptr.p_aio;
2468 if (!knlist_empty(&aiocbe->klist))
2469 knlist_remove(&aiocbe->klist, kn, 0);
2472 /* kqueue filter function */
2475 filt_aio(struct knote *kn, long hint)
2477 struct aiocblist *aiocbe = kn->kn_ptr.p_aio;
2479 kn->kn_data = aiocbe->uaiocb._aiocb_private.error;
2480 if (aiocbe->jobstate != JOBST_JOBFINISHED)
2482 kn->kn_flags |= EV_EOF;
2486 /* kqueue attach function */
2488 filt_lioattach(struct knote *kn)
2490 struct aioliojob * lj = (struct aioliojob *)kn->kn_sdata;
2493 * The aioliojob pointer must be validated before using it, so
2494 * registration is restricted to the kernel; the user cannot
2497 if ((kn->kn_flags & EV_FLAG1) == 0)
2499 kn->kn_ptr.p_lio = lj;
2500 kn->kn_flags &= ~EV_FLAG1;
2502 knlist_add(&lj->klist, kn, 0);
2507 /* kqueue detach function */
2509 filt_liodetach(struct knote *kn)
2511 struct aioliojob * lj = kn->kn_ptr.p_lio;
2513 if (!knlist_empty(&lj->klist))
2514 knlist_remove(&lj->klist, kn, 0);
2517 /* kqueue filter function */
2520 filt_lio(struct knote *kn, long hint)
2522 struct aioliojob * lj = kn->kn_ptr.p_lio;
2524 return (lj->lioj_flags & LIOJ_KEVENT_POSTED);
2527 #ifdef COMPAT_FREEBSD32
2528 #include <sys/mount.h>
2529 #include <sys/socket.h>
2530 #include <compat/freebsd32/freebsd32.h>
2531 #include <compat/freebsd32/freebsd32_proto.h>
2532 #include <compat/freebsd32/freebsd32_signal.h>
2533 #include <compat/freebsd32/freebsd32_syscall.h>
2534 #include <compat/freebsd32/freebsd32_util.h>
2536 struct __aiocb_private32 {
2539 uint32_t kernelinfo;
2542 typedef struct oaiocb32 {
2543 int aio_fildes; /* File descriptor */
2544 uint64_t aio_offset __packed; /* File offset for I/O */
2545 uint32_t aio_buf; /* I/O buffer in process space */
2546 uint32_t aio_nbytes; /* Number of bytes for I/O */
2547 struct osigevent32 aio_sigevent; /* Signal to deliver */
2548 int aio_lio_opcode; /* LIO opcode */
2549 int aio_reqprio; /* Request priority -- ignored */
2550 struct __aiocb_private32 _aiocb_private;
2553 typedef struct aiocb32 {
2554 int32_t aio_fildes; /* File descriptor */
2555 uint64_t aio_offset __packed; /* File offset for I/O */
2556 uint32_t aio_buf; /* I/O buffer in process space */
2557 uint32_t aio_nbytes; /* Number of bytes for I/O */
2559 uint32_t __spare2__;
2560 int aio_lio_opcode; /* LIO opcode */
2561 int aio_reqprio; /* Request priority -- ignored */
2562 struct __aiocb_private32 _aiocb_private;
2563 struct sigevent32 aio_sigevent; /* Signal to deliver */
2567 convert_old_sigevent32(struct osigevent32 *osig, struct sigevent *nsig)
2571 * Only SIGEV_NONE, SIGEV_SIGNAL, and SIGEV_KEVENT are
2572 * supported by AIO with the old sigevent structure.
2574 CP(*osig, *nsig, sigev_notify);
2575 switch (nsig->sigev_notify) {
2579 nsig->sigev_signo = osig->__sigev_u.__sigev_signo;
2582 nsig->sigev_notify_kqueue =
2583 osig->__sigev_u.__sigev_notify_kqueue;
2584 PTRIN_CP(*osig, *nsig, sigev_value.sival_ptr);
2593 aiocb32_copyin_old_sigevent(struct aiocb *ujob, struct aiocb *kjob)
2595 struct oaiocb32 job32;
2598 bzero(kjob, sizeof(struct aiocb));
2599 error = copyin(ujob, &job32, sizeof(job32));
2603 CP(job32, *kjob, aio_fildes);
2604 CP(job32, *kjob, aio_offset);
2605 PTRIN_CP(job32, *kjob, aio_buf);
2606 CP(job32, *kjob, aio_nbytes);
2607 CP(job32, *kjob, aio_lio_opcode);
2608 CP(job32, *kjob, aio_reqprio);
2609 CP(job32, *kjob, _aiocb_private.status);
2610 CP(job32, *kjob, _aiocb_private.error);
2611 PTRIN_CP(job32, *kjob, _aiocb_private.kernelinfo);
2612 return (convert_old_sigevent32(&job32.aio_sigevent,
2613 &kjob->aio_sigevent));
2617 convert_sigevent32(struct sigevent32 *sig32, struct sigevent *sig)
2620 CP(*sig32, *sig, sigev_notify);
2621 switch (sig->sigev_notify) {
2624 case SIGEV_THREAD_ID:
2625 CP(*sig32, *sig, sigev_notify_thread_id);
2628 CP(*sig32, *sig, sigev_signo);
2631 CP(*sig32, *sig, sigev_notify_kqueue);
2632 PTRIN_CP(*sig32, *sig, sigev_value.sival_ptr);
2641 aiocb32_copyin(struct aiocb *ujob, struct aiocb *kjob)
2643 struct aiocb32 job32;
2646 error = copyin(ujob, &job32, sizeof(job32));
2649 CP(job32, *kjob, aio_fildes);
2650 CP(job32, *kjob, aio_offset);
2651 PTRIN_CP(job32, *kjob, aio_buf);
2652 CP(job32, *kjob, aio_nbytes);
2653 CP(job32, *kjob, aio_lio_opcode);
2654 CP(job32, *kjob, aio_reqprio);
2655 CP(job32, *kjob, _aiocb_private.status);
2656 CP(job32, *kjob, _aiocb_private.error);
2657 PTRIN_CP(job32, *kjob, _aiocb_private.kernelinfo);
2658 return (convert_sigevent32(&job32.aio_sigevent, &kjob->aio_sigevent));
2662 aiocb32_fetch_status(struct aiocb *ujob)
2664 struct aiocb32 *ujob32;
2666 ujob32 = (struct aiocb32 *)ujob;
2667 return (fuword32(&ujob32->_aiocb_private.status));
2671 aiocb32_fetch_error(struct aiocb *ujob)
2673 struct aiocb32 *ujob32;
2675 ujob32 = (struct aiocb32 *)ujob;
2676 return (fuword32(&ujob32->_aiocb_private.error));
2680 aiocb32_store_status(struct aiocb *ujob, long status)
2682 struct aiocb32 *ujob32;
2684 ujob32 = (struct aiocb32 *)ujob;
2685 return (suword32(&ujob32->_aiocb_private.status, status));
2689 aiocb32_store_error(struct aiocb *ujob, long error)
2691 struct aiocb32 *ujob32;
2693 ujob32 = (struct aiocb32 *)ujob;
2694 return (suword32(&ujob32->_aiocb_private.error, error));
2698 aiocb32_store_kernelinfo(struct aiocb *ujob, long jobref)
2700 struct aiocb32 *ujob32;
2702 ujob32 = (struct aiocb32 *)ujob;
2703 return (suword32(&ujob32->_aiocb_private.kernelinfo, jobref));
2707 aiocb32_store_aiocb(struct aiocb **ujobp, struct aiocb *ujob)
2710 return (suword32(ujobp, (long)ujob));
2713 static struct aiocb_ops aiocb32_ops = {
2714 .copyin = aiocb32_copyin,
2715 .fetch_status = aiocb32_fetch_status,
2716 .fetch_error = aiocb32_fetch_error,
2717 .store_status = aiocb32_store_status,
2718 .store_error = aiocb32_store_error,
2719 .store_kernelinfo = aiocb32_store_kernelinfo,
2720 .store_aiocb = aiocb32_store_aiocb,
2723 static struct aiocb_ops aiocb32_ops_osigevent = {
2724 .copyin = aiocb32_copyin_old_sigevent,
2725 .fetch_status = aiocb32_fetch_status,
2726 .fetch_error = aiocb32_fetch_error,
2727 .store_status = aiocb32_store_status,
2728 .store_error = aiocb32_store_error,
2729 .store_kernelinfo = aiocb32_store_kernelinfo,
2730 .store_aiocb = aiocb32_store_aiocb,
2734 freebsd32_aio_return(struct thread *td, struct freebsd32_aio_return_args *uap)
2737 return (kern_aio_return(td, (struct aiocb *)uap->aiocbp, &aiocb32_ops));
2741 freebsd32_aio_suspend(struct thread *td, struct freebsd32_aio_suspend_args *uap)
2743 struct timespec32 ts32;
2744 struct timespec ts, *tsp;
2745 struct aiocb **ujoblist;
2746 uint32_t *ujoblist32;
2749 if (uap->nent < 0 || uap->nent > AIO_LISTIO_MAX)
2753 /* Get timespec struct. */
2754 if ((error = copyin(uap->timeout, &ts32, sizeof(ts32))) != 0)
2756 CP(ts32, ts, tv_sec);
2757 CP(ts32, ts, tv_nsec);
2762 ujoblist = uma_zalloc(aiol_zone, M_WAITOK);
2763 ujoblist32 = (uint32_t *)ujoblist;
2764 error = copyin(uap->aiocbp, ujoblist32, uap->nent *
2765 sizeof(ujoblist32[0]));
2767 for (i = uap->nent; i > 0; i--)
2768 ujoblist[i] = PTRIN(ujoblist32[i]);
2770 error = kern_aio_suspend(td, uap->nent, ujoblist, tsp);
2772 uma_zfree(aiol_zone, ujoblist);
2777 freebsd32_aio_cancel(struct thread *td, struct freebsd32_aio_cancel_args *uap)
2780 return (aio_cancel(td, (struct aio_cancel_args *)uap));
2784 freebsd32_aio_error(struct thread *td, struct freebsd32_aio_error_args *uap)
2787 return (kern_aio_error(td, (struct aiocb *)uap->aiocbp, &aiocb32_ops));
2791 freebsd32_oaio_read(struct thread *td, struct freebsd32_oaio_read_args *uap)
2794 return (aio_aqueue(td, (struct aiocb *)uap->aiocbp, NULL, LIO_READ,
2795 &aiocb32_ops_osigevent));
2799 freebsd32_aio_read(struct thread *td, struct freebsd32_aio_read_args *uap)
2802 return (aio_aqueue(td, (struct aiocb *)uap->aiocbp, NULL, LIO_READ,
2807 freebsd32_oaio_write(struct thread *td, struct freebsd32_oaio_write_args *uap)
2810 return (aio_aqueue(td, (struct aiocb *)uap->aiocbp, NULL, LIO_WRITE,
2811 &aiocb32_ops_osigevent));
2815 freebsd32_aio_write(struct thread *td, struct freebsd32_aio_write_args *uap)
2818 return (aio_aqueue(td, (struct aiocb *)uap->aiocbp, NULL, LIO_WRITE,
2823 freebsd32_aio_waitcomplete(struct thread *td,
2824 struct freebsd32_aio_waitcomplete_args *uap)
2826 struct timespec32 ts32;
2827 struct timespec ts, *tsp;
2831 /* Get timespec struct. */
2832 error = copyin(uap->timeout, &ts32, sizeof(ts32));
2835 CP(ts32, ts, tv_sec);
2836 CP(ts32, ts, tv_nsec);
2841 return (kern_aio_waitcomplete(td, (struct aiocb **)uap->aiocbp, tsp,
2846 freebsd32_aio_fsync(struct thread *td, struct freebsd32_aio_fsync_args *uap)
2849 return (kern_aio_fsync(td, uap->op, (struct aiocb *)uap->aiocbp,
2854 freebsd32_olio_listio(struct thread *td, struct freebsd32_olio_listio_args *uap)
2856 struct aiocb **acb_list;
2857 struct sigevent *sigp, sig;
2858 struct osigevent32 osig;
2859 uint32_t *acb_list32;
2862 if ((uap->mode != LIO_NOWAIT) && (uap->mode != LIO_WAIT))
2866 if (nent < 0 || nent > AIO_LISTIO_MAX)
2869 if (uap->sig && (uap->mode == LIO_NOWAIT)) {
2870 error = copyin(uap->sig, &osig, sizeof(osig));
2873 error = convert_old_sigevent32(&osig, &sig);
2880 acb_list32 = malloc(sizeof(uint32_t) * nent, M_LIO, M_WAITOK);
2881 error = copyin(uap->acb_list, acb_list32, nent * sizeof(uint32_t));
2883 free(acb_list32, M_LIO);
2886 acb_list = malloc(sizeof(struct aiocb *) * nent, M_LIO, M_WAITOK);
2887 for (i = 0; i < nent; i++)
2888 acb_list[i] = PTRIN(acb_list32[i]);
2889 free(acb_list32, M_LIO);
2891 error = kern_lio_listio(td, uap->mode,
2892 (struct aiocb * const *)uap->acb_list, acb_list, nent, sigp,
2893 &aiocb32_ops_osigevent);
2894 free(acb_list, M_LIO);
2899 freebsd32_lio_listio(struct thread *td, struct freebsd32_lio_listio_args *uap)
2901 struct aiocb **acb_list;
2902 struct sigevent *sigp, sig;
2903 struct sigevent32 sig32;
2904 uint32_t *acb_list32;
2907 if ((uap->mode != LIO_NOWAIT) && (uap->mode != LIO_WAIT))
2911 if (nent < 0 || nent > AIO_LISTIO_MAX)
2914 if (uap->sig && (uap->mode == LIO_NOWAIT)) {
2915 error = copyin(uap->sig, &sig32, sizeof(sig32));
2918 error = convert_sigevent32(&sig32, &sig);
2925 acb_list32 = malloc(sizeof(uint32_t) * nent, M_LIO, M_WAITOK);
2926 error = copyin(uap->acb_list, acb_list32, nent * sizeof(uint32_t));
2928 free(acb_list32, M_LIO);
2931 acb_list = malloc(sizeof(struct aiocb *) * nent, M_LIO, M_WAITOK);
2932 for (i = 0; i < nent; i++)
2933 acb_list[i] = PTRIN(acb_list32[i]);
2934 free(acb_list32, M_LIO);
2936 error = kern_lio_listio(td, uap->mode,
2937 (struct aiocb * const *)uap->acb_list, acb_list, nent, sigp,
2939 free(acb_list, M_LIO);
2943 SYSCALL32_MODULE_HELPER(freebsd32_aio_return);
2944 SYSCALL32_MODULE_HELPER(freebsd32_aio_suspend);
2945 SYSCALL32_MODULE_HELPER(freebsd32_aio_cancel);
2946 SYSCALL32_MODULE_HELPER(freebsd32_aio_error);
2947 SYSCALL32_MODULE_HELPER(freebsd32_aio_fsync);
2948 SYSCALL32_MODULE_HELPER(freebsd32_aio_read);
2949 SYSCALL32_MODULE_HELPER(freebsd32_aio_write);
2950 SYSCALL32_MODULE_HELPER(freebsd32_aio_waitcomplete);
2951 SYSCALL32_MODULE_HELPER(freebsd32_lio_listio);
2952 SYSCALL32_MODULE_HELPER(freebsd32_oaio_read);
2953 SYSCALL32_MODULE_HELPER(freebsd32_oaio_write);
2954 SYSCALL32_MODULE_HELPER(freebsd32_olio_listio);