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 int 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 = {
377 .f_attach = filt_aioattach,
378 .f_detach = filt_aiodetach,
381 static struct filterops lio_filtops = {
383 .f_attach = filt_lioattach,
384 .f_detach = filt_liodetach,
388 static eventhandler_tag exit_tag, exec_tag;
390 TASKQUEUE_DEFINE_THREAD(aiod_bio);
393 * Main operations function for use as a kernel module.
396 aio_modload(struct module *module, int cmd, void *arg)
405 error = aio_unload();
416 static moduledata_t aio_mod = {
422 static struct syscall_helper_data aio_syscalls[] = {
423 SYSCALL_INIT_HELPER(aio_cancel),
424 SYSCALL_INIT_HELPER(aio_error),
425 SYSCALL_INIT_HELPER(aio_fsync),
426 SYSCALL_INIT_HELPER(aio_read),
427 SYSCALL_INIT_HELPER(aio_return),
428 SYSCALL_INIT_HELPER(aio_suspend),
429 SYSCALL_INIT_HELPER(aio_waitcomplete),
430 SYSCALL_INIT_HELPER(aio_write),
431 SYSCALL_INIT_HELPER(lio_listio),
432 SYSCALL_INIT_HELPER(oaio_read),
433 SYSCALL_INIT_HELPER(oaio_write),
434 SYSCALL_INIT_HELPER(olio_listio),
438 #ifdef COMPAT_FREEBSD32
439 #include <sys/mount.h>
440 #include <sys/socket.h>
441 #include <compat/freebsd32/freebsd32.h>
442 #include <compat/freebsd32/freebsd32_proto.h>
443 #include <compat/freebsd32/freebsd32_signal.h>
444 #include <compat/freebsd32/freebsd32_syscall.h>
445 #include <compat/freebsd32/freebsd32_util.h>
447 static struct syscall_helper_data aio32_syscalls[] = {
448 SYSCALL32_INIT_HELPER(freebsd32_aio_return),
449 SYSCALL32_INIT_HELPER(freebsd32_aio_suspend),
450 SYSCALL32_INIT_HELPER(freebsd32_aio_cancel),
451 SYSCALL32_INIT_HELPER(freebsd32_aio_error),
452 SYSCALL32_INIT_HELPER(freebsd32_aio_fsync),
453 SYSCALL32_INIT_HELPER(freebsd32_aio_read),
454 SYSCALL32_INIT_HELPER(freebsd32_aio_write),
455 SYSCALL32_INIT_HELPER(freebsd32_aio_waitcomplete),
456 SYSCALL32_INIT_HELPER(freebsd32_lio_listio),
457 SYSCALL32_INIT_HELPER(freebsd32_oaio_read),
458 SYSCALL32_INIT_HELPER(freebsd32_oaio_write),
459 SYSCALL32_INIT_HELPER(freebsd32_olio_listio),
464 DECLARE_MODULE(aio, aio_mod,
465 SI_SUB_VFS, SI_ORDER_ANY);
466 MODULE_VERSION(aio, 1);
469 * Startup initialization
476 /* XXX: should probably just use so->callback */
477 aio_swake = &aio_swake_cb;
478 exit_tag = EVENTHANDLER_REGISTER(process_exit, aio_proc_rundown, NULL,
479 EVENTHANDLER_PRI_ANY);
480 exec_tag = EVENTHANDLER_REGISTER(process_exec, aio_proc_rundown_exec, NULL,
481 EVENTHANDLER_PRI_ANY);
482 kqueue_add_filteropts(EVFILT_AIO, &aio_filtops);
483 kqueue_add_filteropts(EVFILT_LIO, &lio_filtops);
484 TAILQ_INIT(&aio_freeproc);
485 sema_init(&aio_newproc_sem, 0, "aio_new_proc");
486 mtx_init(&aio_job_mtx, "aio_job", NULL, MTX_DEF);
487 mtx_init(&aio_sock_mtx, "aio_sock", NULL, MTX_DEF);
488 TAILQ_INIT(&aio_jobs);
489 aiod_unr = new_unrhdr(1, INT_MAX, NULL);
490 kaio_zone = uma_zcreate("AIO", sizeof(struct kaioinfo), NULL, NULL,
491 NULL, NULL, UMA_ALIGN_PTR, UMA_ZONE_NOFREE);
492 aiop_zone = uma_zcreate("AIOP", sizeof(struct aiothreadlist), NULL,
493 NULL, NULL, NULL, UMA_ALIGN_PTR, UMA_ZONE_NOFREE);
494 aiocb_zone = uma_zcreate("AIOCB", sizeof(struct aiocblist), NULL, NULL,
495 NULL, NULL, UMA_ALIGN_PTR, UMA_ZONE_NOFREE);
496 aiol_zone = uma_zcreate("AIOL", AIO_LISTIO_MAX*sizeof(intptr_t) , NULL,
497 NULL, NULL, NULL, UMA_ALIGN_PTR, UMA_ZONE_NOFREE);
498 aiolio_zone = uma_zcreate("AIOLIO", sizeof(struct aioliojob), NULL,
499 NULL, NULL, NULL, UMA_ALIGN_PTR, UMA_ZONE_NOFREE);
500 aiod_timeout = AIOD_TIMEOUT_DEFAULT;
501 aiod_lifetime = AIOD_LIFETIME_DEFAULT;
503 async_io_version = _POSIX_VERSION;
504 p31b_setcfg(CTL_P1003_1B_AIO_LISTIO_MAX, AIO_LISTIO_MAX);
505 p31b_setcfg(CTL_P1003_1B_AIO_MAX, MAX_AIO_QUEUE);
506 p31b_setcfg(CTL_P1003_1B_AIO_PRIO_DELTA_MAX, 0);
508 error = syscall_helper_register(aio_syscalls);
511 #ifdef COMPAT_FREEBSD32
512 error = syscall32_helper_register(aio32_syscalls);
520 * Callback for unload of AIO when used as a module.
528 * XXX: no unloads by default, it's too dangerous.
529 * perhaps we could do it if locked out callers and then
530 * did an aio_proc_rundown() on each process.
532 * jhb: aio_proc_rundown() needs to run on curproc though,
533 * so I don't think that would fly.
538 #ifdef COMPAT_FREEBSD32
539 syscall32_helper_unregister(aio32_syscalls);
541 syscall_helper_unregister(aio_syscalls);
543 error = kqueue_del_filteropts(EVFILT_AIO);
546 error = kqueue_del_filteropts(EVFILT_LIO);
549 async_io_version = 0;
551 taskqueue_free(taskqueue_aiod_bio);
552 delete_unrhdr(aiod_unr);
553 uma_zdestroy(kaio_zone);
554 uma_zdestroy(aiop_zone);
555 uma_zdestroy(aiocb_zone);
556 uma_zdestroy(aiol_zone);
557 uma_zdestroy(aiolio_zone);
558 EVENTHANDLER_DEREGISTER(process_exit, exit_tag);
559 EVENTHANDLER_DEREGISTER(process_exec, exec_tag);
560 mtx_destroy(&aio_job_mtx);
561 mtx_destroy(&aio_sock_mtx);
562 sema_destroy(&aio_newproc_sem);
563 p31b_setcfg(CTL_P1003_1B_AIO_LISTIO_MAX, -1);
564 p31b_setcfg(CTL_P1003_1B_AIO_MAX, -1);
565 p31b_setcfg(CTL_P1003_1B_AIO_PRIO_DELTA_MAX, -1);
570 * Init the per-process aioinfo structure. The aioinfo limits are set
571 * per-process for user limit (resource) management.
574 aio_init_aioinfo(struct proc *p)
578 ki = uma_zalloc(kaio_zone, M_WAITOK);
579 mtx_init(&ki->kaio_mtx, "aiomtx", NULL, MTX_DEF);
581 ki->kaio_maxactive_count = max_aio_per_proc;
582 ki->kaio_active_count = 0;
583 ki->kaio_qallowed_count = max_aio_queue_per_proc;
585 ki->kaio_ballowed_count = max_buf_aio;
586 ki->kaio_buffer_count = 0;
587 TAILQ_INIT(&ki->kaio_all);
588 TAILQ_INIT(&ki->kaio_done);
589 TAILQ_INIT(&ki->kaio_jobqueue);
590 TAILQ_INIT(&ki->kaio_bufqueue);
591 TAILQ_INIT(&ki->kaio_liojoblist);
592 TAILQ_INIT(&ki->kaio_sockqueue);
593 TAILQ_INIT(&ki->kaio_syncqueue);
594 TASK_INIT(&ki->kaio_task, 0, aio_kick_helper, p);
596 if (p->p_aioinfo == NULL) {
601 mtx_destroy(&ki->kaio_mtx);
602 uma_zfree(kaio_zone, ki);
605 while (num_aio_procs < MIN(target_aio_procs, max_aio_procs))
610 aio_sendsig(struct proc *p, struct sigevent *sigev, ksiginfo_t *ksi)
615 error = sigev_findtd(p, sigev, &td);
619 ksiginfo_set_sigev(ksi, sigev);
620 ksi->ksi_code = SI_ASYNCIO;
621 ksi->ksi_flags |= KSI_EXT | KSI_INS;
622 tdsendsignal(p, td, ksi->ksi_signo, ksi);
629 * Free a job entry. Wait for completion if it is currently active, but don't
630 * delay forever. If we delay, we return a flag that says that we have to
631 * restart the queue scan.
634 aio_free_entry(struct aiocblist *aiocbe)
637 struct aioliojob *lj;
640 p = aiocbe->userproc;
645 AIO_LOCK_ASSERT(ki, MA_OWNED);
646 MPASS(aiocbe->jobstate == JOBST_JOBFINISHED);
648 atomic_subtract_int(&num_queue_count, 1);
651 MPASS(ki->kaio_count >= 0);
653 TAILQ_REMOVE(&ki->kaio_done, aiocbe, plist);
654 TAILQ_REMOVE(&ki->kaio_all, aiocbe, allist);
659 lj->lioj_finished_count--;
661 if (lj->lioj_count == 0) {
662 TAILQ_REMOVE(&ki->kaio_liojoblist, lj, lioj_list);
663 /* lio is going away, we need to destroy any knotes */
664 knlist_delete(&lj->klist, curthread, 1);
666 sigqueue_take(&lj->lioj_ksi);
668 uma_zfree(aiolio_zone, lj);
672 /* aiocbe is going away, we need to destroy any knotes */
673 knlist_delete(&aiocbe->klist, curthread, 1);
675 sigqueue_take(&aiocbe->ksi);
678 MPASS(aiocbe->bp == NULL);
679 aiocbe->jobstate = JOBST_NULL;
683 * The thread argument here is used to find the owning process
684 * and is also passed to fo_close() which may pass it to various
685 * places such as devsw close() routines. Because of that, we
686 * need a thread pointer from the process owning the job that is
687 * persistent and won't disappear out from under us or move to
690 * Currently, all the callers of this function call it to remove
691 * an aiocblist from the current process' job list either via a
692 * syscall or due to the current process calling exit() or
693 * execve(). Thus, we know that p == curproc. We also know that
694 * curthread can't exit since we are curthread.
696 * Therefore, we use curthread as the thread to pass to
697 * knlist_delete(). This does mean that it is possible for the
698 * thread pointer at close time to differ from the thread pointer
699 * at open time, but this is already true of file descriptors in
700 * a multithreaded process.
702 fdrop(aiocbe->fd_file, curthread);
703 crfree(aiocbe->cred);
704 uma_zfree(aiocb_zone, aiocbe);
711 aio_proc_rundown_exec(void *arg, struct proc *p, struct image_params *imgp __unused)
713 aio_proc_rundown(arg, p);
717 * Rundown the jobs for a given process.
720 aio_proc_rundown(void *arg, struct proc *p)
723 struct aioliojob *lj;
724 struct aiocblist *cbe, *cbn;
729 KASSERT(curthread->td_proc == p,
730 ("%s: called on non-curproc", __func__));
736 ki->kaio_flags |= KAIO_RUNDOWN;
741 * Try to cancel all pending requests. This code simulates
742 * aio_cancel on all pending I/O requests.
744 TAILQ_FOREACH_SAFE(cbe, &ki->kaio_jobqueue, plist, cbn) {
746 mtx_lock(&aio_job_mtx);
747 if (cbe->jobstate == JOBST_JOBQGLOBAL) {
748 TAILQ_REMOVE(&aio_jobs, cbe, list);
750 } else if (cbe->jobstate == JOBST_JOBQSOCK) {
752 MPASS(fp->f_type == DTYPE_SOCKET);
754 TAILQ_REMOVE(&so->so_aiojobq, cbe, list);
756 } else if (cbe->jobstate == JOBST_JOBQSYNC) {
757 TAILQ_REMOVE(&ki->kaio_syncqueue, cbe, list);
760 mtx_unlock(&aio_job_mtx);
763 cbe->jobstate = JOBST_JOBFINISHED;
764 cbe->uaiocb._aiocb_private.status = -1;
765 cbe->uaiocb._aiocb_private.error = ECANCELED;
766 TAILQ_REMOVE(&ki->kaio_jobqueue, cbe, plist);
767 aio_bio_done_notify(p, cbe, DONE_QUEUE);
771 /* Wait for all running I/O to be finished */
772 if (TAILQ_FIRST(&ki->kaio_bufqueue) ||
773 TAILQ_FIRST(&ki->kaio_jobqueue)) {
774 ki->kaio_flags |= KAIO_WAKEUP;
775 msleep(&p->p_aioinfo, AIO_MTX(ki), PRIBIO, "aioprn", hz);
779 /* Free all completed I/O requests. */
780 while ((cbe = TAILQ_FIRST(&ki->kaio_done)) != NULL)
783 while ((lj = TAILQ_FIRST(&ki->kaio_liojoblist)) != NULL) {
784 if (lj->lioj_count == 0) {
785 TAILQ_REMOVE(&ki->kaio_liojoblist, lj, lioj_list);
786 knlist_delete(&lj->klist, curthread, 1);
788 sigqueue_take(&lj->lioj_ksi);
790 uma_zfree(aiolio_zone, lj);
792 panic("LIO job not cleaned up: C:%d, FC:%d\n",
793 lj->lioj_count, lj->lioj_finished_count);
797 taskqueue_drain(taskqueue_aiod_bio, &ki->kaio_task);
798 mtx_destroy(&ki->kaio_mtx);
799 uma_zfree(kaio_zone, ki);
804 * Select a job to run (called by an AIO daemon).
806 static struct aiocblist *
807 aio_selectjob(struct aiothreadlist *aiop)
809 struct aiocblist *aiocbe;
813 mtx_assert(&aio_job_mtx, MA_OWNED);
814 TAILQ_FOREACH(aiocbe, &aio_jobs, list) {
815 userp = aiocbe->userproc;
816 ki = userp->p_aioinfo;
818 if (ki->kaio_active_count < ki->kaio_maxactive_count) {
819 TAILQ_REMOVE(&aio_jobs, aiocbe, list);
820 /* Account for currently active jobs. */
821 ki->kaio_active_count++;
822 aiocbe->jobstate = JOBST_JOBRUNNING;
830 * Move all data to a permanent storage device, this code
831 * simulates fsync syscall.
834 aio_fsync_vnode(struct thread *td, struct vnode *vp)
840 vfslocked = VFS_LOCK_GIANT(vp->v_mount);
841 if ((error = vn_start_write(vp, &mp, V_WAIT | PCATCH)) != 0)
843 vn_lock(vp, LK_EXCLUSIVE | LK_RETRY);
844 if (vp->v_object != NULL) {
845 VM_OBJECT_LOCK(vp->v_object);
846 vm_object_page_clean(vp->v_object, 0, 0, 0);
847 VM_OBJECT_UNLOCK(vp->v_object);
849 error = VOP_FSYNC(vp, MNT_WAIT, td);
852 vn_finished_write(mp);
854 VFS_UNLOCK_GIANT(vfslocked);
859 * The AIO processing activity. This is the code that does the I/O request for
860 * the non-physio version of the operations. The normal vn operations are used,
861 * and this code should work in all instances for every type of file, including
862 * pipes, sockets, fifos, and regular files.
864 * XXX I don't think it works well for socket, pipe, and fifo.
867 aio_process(struct aiocblist *aiocbe)
869 struct ucred *td_savedcred;
878 int oublock_st, oublock_end;
879 int inblock_st, inblock_end;
882 td_savedcred = td->td_ucred;
883 td->td_ucred = aiocbe->cred;
884 cb = &aiocbe->uaiocb;
885 fp = aiocbe->fd_file;
887 if (cb->aio_lio_opcode == LIO_SYNC) {
890 if (fp->f_vnode != NULL)
891 error = aio_fsync_vnode(td, fp->f_vnode);
892 cb->_aiocb_private.error = error;
893 cb->_aiocb_private.status = 0;
894 td->td_ucred = td_savedcred;
898 aiov.iov_base = (void *)(uintptr_t)cb->aio_buf;
899 aiov.iov_len = cb->aio_nbytes;
901 auio.uio_iov = &aiov;
903 auio.uio_offset = cb->aio_offset;
904 auio.uio_resid = cb->aio_nbytes;
905 cnt = cb->aio_nbytes;
906 auio.uio_segflg = UIO_USERSPACE;
909 inblock_st = td->td_ru.ru_inblock;
910 oublock_st = td->td_ru.ru_oublock;
912 * aio_aqueue() acquires a reference to the file that is
913 * released in aio_free_entry().
915 if (cb->aio_lio_opcode == LIO_READ) {
916 auio.uio_rw = UIO_READ;
917 if (auio.uio_resid == 0)
920 error = fo_read(fp, &auio, fp->f_cred, FOF_OFFSET, td);
922 if (fp->f_type == DTYPE_VNODE)
924 auio.uio_rw = UIO_WRITE;
925 error = fo_write(fp, &auio, fp->f_cred, FOF_OFFSET, td);
927 inblock_end = td->td_ru.ru_inblock;
928 oublock_end = td->td_ru.ru_oublock;
930 aiocbe->inputcharge = inblock_end - inblock_st;
931 aiocbe->outputcharge = oublock_end - oublock_st;
933 if ((error) && (auio.uio_resid != cnt)) {
934 if (error == ERESTART || error == EINTR || error == EWOULDBLOCK)
936 if ((error == EPIPE) && (cb->aio_lio_opcode == LIO_WRITE)) {
938 if (fp->f_type == DTYPE_SOCKET) {
940 if (so->so_options & SO_NOSIGPIPE)
944 PROC_LOCK(aiocbe->userproc);
945 psignal(aiocbe->userproc, SIGPIPE);
946 PROC_UNLOCK(aiocbe->userproc);
951 cnt -= auio.uio_resid;
952 cb->_aiocb_private.error = error;
953 cb->_aiocb_private.status = cnt;
954 td->td_ucred = td_savedcred;
958 aio_bio_done_notify(struct proc *userp, struct aiocblist *aiocbe, int type)
960 struct aioliojob *lj;
962 struct aiocblist *scb, *scbn;
965 ki = userp->p_aioinfo;
966 AIO_LOCK_ASSERT(ki, MA_OWNED);
970 lj->lioj_finished_count++;
971 if (lj->lioj_count == lj->lioj_finished_count)
974 if (type == DONE_QUEUE) {
975 aiocbe->jobflags |= AIOCBLIST_DONE;
977 aiocbe->jobflags |= AIOCBLIST_BUFDONE;
979 TAILQ_INSERT_TAIL(&ki->kaio_done, aiocbe, plist);
980 aiocbe->jobstate = JOBST_JOBFINISHED;
982 if (ki->kaio_flags & KAIO_RUNDOWN)
983 goto notification_done;
985 if (aiocbe->uaiocb.aio_sigevent.sigev_notify == SIGEV_SIGNAL ||
986 aiocbe->uaiocb.aio_sigevent.sigev_notify == SIGEV_THREAD_ID)
987 aio_sendsig(userp, &aiocbe->uaiocb.aio_sigevent, &aiocbe->ksi);
989 KNOTE_LOCKED(&aiocbe->klist, 1);
992 if (lj->lioj_signal.sigev_notify == SIGEV_KEVENT) {
993 lj->lioj_flags |= LIOJ_KEVENT_POSTED;
994 KNOTE_LOCKED(&lj->klist, 1);
996 if ((lj->lioj_flags & (LIOJ_SIGNAL|LIOJ_SIGNAL_POSTED))
998 && (lj->lioj_signal.sigev_notify == SIGEV_SIGNAL ||
999 lj->lioj_signal.sigev_notify == SIGEV_THREAD_ID)) {
1000 aio_sendsig(userp, &lj->lioj_signal, &lj->lioj_ksi);
1001 lj->lioj_flags |= LIOJ_SIGNAL_POSTED;
1006 if (aiocbe->jobflags & AIOCBLIST_CHECKSYNC) {
1007 TAILQ_FOREACH_SAFE(scb, &ki->kaio_syncqueue, list, scbn) {
1008 if (aiocbe->fd_file == scb->fd_file &&
1009 aiocbe->seqno < scb->seqno) {
1010 if (--scb->pending == 0) {
1011 mtx_lock(&aio_job_mtx);
1012 scb->jobstate = JOBST_JOBQGLOBAL;
1013 TAILQ_REMOVE(&ki->kaio_syncqueue, scb, list);
1014 TAILQ_INSERT_TAIL(&aio_jobs, scb, list);
1015 aio_kick_nowait(userp);
1016 mtx_unlock(&aio_job_mtx);
1021 if (ki->kaio_flags & KAIO_WAKEUP) {
1022 ki->kaio_flags &= ~KAIO_WAKEUP;
1023 wakeup(&userp->p_aioinfo);
1028 * The AIO daemon, most of the actual work is done in aio_process,
1029 * but the setup (and address space mgmt) is done in this routine.
1032 aio_daemon(void *_id)
1034 struct aiocblist *aiocbe;
1035 struct aiothreadlist *aiop;
1036 struct kaioinfo *ki;
1037 struct proc *curcp, *mycp, *userp;
1038 struct vmspace *myvm, *tmpvm;
1039 struct thread *td = curthread;
1040 int id = (intptr_t)_id;
1043 * Local copies of curproc (cp) and vmspace (myvm)
1046 myvm = mycp->p_vmspace;
1048 KASSERT(mycp->p_textvp == NULL, ("kthread has a textvp"));
1051 * Allocate and ready the aio control info. There is one aiop structure
1054 aiop = uma_zalloc(aiop_zone, M_WAITOK);
1055 aiop->aiothread = td;
1056 aiop->aiothreadflags = 0;
1058 /* The daemon resides in its own pgrp. */
1062 * Wakeup parent process. (Parent sleeps to keep from blasting away
1063 * and creating too many daemons.)
1065 sema_post(&aio_newproc_sem);
1067 mtx_lock(&aio_job_mtx);
1070 * curcp is the current daemon process context.
1071 * userp is the current user process context.
1076 * Take daemon off of free queue
1078 if (aiop->aiothreadflags & AIOP_FREE) {
1079 TAILQ_REMOVE(&aio_freeproc, aiop, list);
1080 aiop->aiothreadflags &= ~AIOP_FREE;
1086 while ((aiocbe = aio_selectjob(aiop)) != NULL) {
1087 mtx_unlock(&aio_job_mtx);
1088 userp = aiocbe->userproc;
1091 * Connect to process address space for user program.
1093 if (userp != curcp) {
1095 * Save the current address space that we are
1098 tmpvm = mycp->p_vmspace;
1101 * Point to the new user address space, and
1104 mycp->p_vmspace = userp->p_vmspace;
1105 atomic_add_int(&mycp->p_vmspace->vm_refcnt, 1);
1107 /* Activate the new mapping. */
1108 pmap_activate(FIRST_THREAD_IN_PROC(mycp));
1111 * If the old address space wasn't the daemons
1112 * own address space, then we need to remove the
1113 * daemon's reference from the other process
1114 * that it was acting on behalf of.
1116 if (tmpvm != myvm) {
1117 vmspace_free(tmpvm);
1122 ki = userp->p_aioinfo;
1124 /* Do the I/O function. */
1125 aio_process(aiocbe);
1127 mtx_lock(&aio_job_mtx);
1128 /* Decrement the active job count. */
1129 ki->kaio_active_count--;
1130 mtx_unlock(&aio_job_mtx);
1133 TAILQ_REMOVE(&ki->kaio_jobqueue, aiocbe, plist);
1134 aio_bio_done_notify(userp, aiocbe, DONE_QUEUE);
1137 mtx_lock(&aio_job_mtx);
1141 * Disconnect from user address space.
1143 if (curcp != mycp) {
1145 mtx_unlock(&aio_job_mtx);
1147 /* Get the user address space to disconnect from. */
1148 tmpvm = mycp->p_vmspace;
1150 /* Get original address space for daemon. */
1151 mycp->p_vmspace = myvm;
1153 /* Activate the daemon's address space. */
1154 pmap_activate(FIRST_THREAD_IN_PROC(mycp));
1156 if (tmpvm == myvm) {
1157 printf("AIOD: vmspace problem -- %d\n",
1161 /* Remove our vmspace reference. */
1162 vmspace_free(tmpvm);
1166 mtx_lock(&aio_job_mtx);
1168 * We have to restart to avoid race, we only sleep if
1169 * no job can be selected, that should be
1175 mtx_assert(&aio_job_mtx, MA_OWNED);
1177 TAILQ_INSERT_HEAD(&aio_freeproc, aiop, list);
1178 aiop->aiothreadflags |= AIOP_FREE;
1181 * If daemon is inactive for a long time, allow it to exit,
1182 * thereby freeing resources.
1184 if (msleep(aiop->aiothread, &aio_job_mtx, PRIBIO, "aiordy",
1186 if (TAILQ_EMPTY(&aio_jobs)) {
1187 if ((aiop->aiothreadflags & AIOP_FREE) &&
1188 (num_aio_procs > target_aio_procs)) {
1189 TAILQ_REMOVE(&aio_freeproc, aiop, list);
1191 mtx_unlock(&aio_job_mtx);
1192 uma_zfree(aiop_zone, aiop);
1193 free_unr(aiod_unr, id);
1195 if (mycp->p_vmspace->vm_refcnt <= 1) {
1196 printf("AIOD: bad vm refcnt for"
1197 " exiting daemon: %d\n",
1198 mycp->p_vmspace->vm_refcnt);
1206 mtx_unlock(&aio_job_mtx);
1207 panic("shouldn't be here\n");
1211 * Create a new AIO daemon. This is mostly a kernel-thread fork routine. The
1212 * AIO daemon modifies its environment itself.
1215 aio_newproc(int *start)
1221 id = alloc_unr(aiod_unr);
1222 error = kproc_create(aio_daemon, (void *)(intptr_t)id, &p,
1223 RFNOWAIT, 0, "aiod%d", id);
1226 * Wait until daemon is started.
1228 sema_wait(&aio_newproc_sem);
1229 mtx_lock(&aio_job_mtx);
1233 mtx_unlock(&aio_job_mtx);
1235 free_unr(aiod_unr, id);
1241 * Try the high-performance, low-overhead physio method for eligible
1242 * VCHR devices. This method doesn't use an aio helper thread, and
1243 * thus has very low overhead.
1245 * Assumes that the caller, aio_aqueue(), has incremented the file
1246 * structure's reference count, preventing its deallocation for the
1247 * duration of this call.
1250 aio_qphysio(struct proc *p, struct aiocblist *aiocbe)
1256 struct kaioinfo *ki;
1257 struct aioliojob *lj;
1260 cb = &aiocbe->uaiocb;
1261 fp = aiocbe->fd_file;
1263 if (fp->f_type != DTYPE_VNODE)
1269 * If its not a disk, we don't want to return a positive error.
1270 * It causes the aio code to not fall through to try the thread
1271 * way when you're talking to a regular file.
1273 if (!vn_isdisk(vp, &error)) {
1274 if (error == ENOTBLK)
1280 if (vp->v_bufobj.bo_bsize == 0)
1283 if (cb->aio_nbytes % vp->v_bufobj.bo_bsize)
1286 if (cb->aio_nbytes > vp->v_rdev->si_iosize_max)
1289 if (cb->aio_nbytes >
1290 MAXPHYS - (((vm_offset_t) cb->aio_buf) & PAGE_MASK))
1294 if (ki->kaio_buffer_count >= ki->kaio_ballowed_count)
1297 /* Create and build a buffer header for a transfer. */
1298 bp = (struct buf *)getpbuf(NULL);
1303 ki->kaio_buffer_count++;
1310 * Get a copy of the kva from the physical buffer.
1314 bp->b_bcount = cb->aio_nbytes;
1315 bp->b_bufsize = cb->aio_nbytes;
1316 bp->b_iodone = aio_physwakeup;
1317 bp->b_saveaddr = bp->b_data;
1318 bp->b_data = (void *)(uintptr_t)cb->aio_buf;
1319 bp->b_offset = cb->aio_offset;
1320 bp->b_iooffset = cb->aio_offset;
1321 bp->b_blkno = btodb(cb->aio_offset);
1322 bp->b_iocmd = cb->aio_lio_opcode == LIO_WRITE ? BIO_WRITE : BIO_READ;
1325 * Bring buffer into kernel space.
1327 if (vmapbuf(bp) < 0) {
1334 bp->b_caller1 = (void *)aiocbe;
1335 TAILQ_INSERT_TAIL(&ki->kaio_bufqueue, aiocbe, plist);
1336 TAILQ_INSERT_TAIL(&ki->kaio_all, aiocbe, allist);
1337 aiocbe->jobstate = JOBST_JOBQBUF;
1338 cb->_aiocb_private.status = cb->aio_nbytes;
1341 atomic_add_int(&num_queue_count, 1);
1342 atomic_add_int(&num_buf_aio, 1);
1346 TASK_INIT(&aiocbe->biotask, 0, biohelper, aiocbe);
1348 /* Perform transfer. */
1349 dev_strategy(vp->v_rdev, bp);
1355 ki->kaio_buffer_count--;
1365 * Wake up aio requests that may be serviceable now.
1368 aio_swake_cb(struct socket *so, struct sockbuf *sb)
1370 struct aiocblist *cb, *cbn;
1373 SOCKBUF_LOCK_ASSERT(sb);
1374 if (sb == &so->so_snd)
1379 sb->sb_flags &= ~SB_AIO;
1380 mtx_lock(&aio_job_mtx);
1381 TAILQ_FOREACH_SAFE(cb, &so->so_aiojobq, list, cbn) {
1382 if (opcode == cb->uaiocb.aio_lio_opcode) {
1383 if (cb->jobstate != JOBST_JOBQSOCK)
1384 panic("invalid queue value");
1386 * We don't have actual sockets backend yet,
1387 * so we simply move the requests to the generic
1390 TAILQ_REMOVE(&so->so_aiojobq, cb, list);
1391 TAILQ_INSERT_TAIL(&aio_jobs, cb, list);
1392 aio_kick_nowait(cb->userproc);
1395 mtx_unlock(&aio_job_mtx);
1399 convert_old_sigevent(struct osigevent *osig, struct sigevent *nsig)
1403 * Only SIGEV_NONE, SIGEV_SIGNAL, and SIGEV_KEVENT are
1404 * supported by AIO with the old sigevent structure.
1406 nsig->sigev_notify = osig->sigev_notify;
1407 switch (nsig->sigev_notify) {
1411 nsig->sigev_signo = osig->__sigev_u.__sigev_signo;
1414 nsig->sigev_notify_kqueue =
1415 osig->__sigev_u.__sigev_notify_kqueue;
1416 nsig->sigev_value.sival_ptr = osig->sigev_value.sival_ptr;
1425 aiocb_copyin_old_sigevent(struct aiocb *ujob, struct aiocb *kjob)
1427 struct oaiocb *ojob;
1430 bzero(kjob, sizeof(struct aiocb));
1431 error = copyin(ujob, kjob, sizeof(struct oaiocb));
1434 ojob = (struct oaiocb *)kjob;
1435 return (convert_old_sigevent(&ojob->aio_sigevent, &kjob->aio_sigevent));
1439 aiocb_copyin(struct aiocb *ujob, struct aiocb *kjob)
1442 return (copyin(ujob, kjob, sizeof(struct aiocb)));
1446 aiocb_fetch_status(struct aiocb *ujob)
1449 return (fuword(&ujob->_aiocb_private.status));
1453 aiocb_fetch_error(struct aiocb *ujob)
1456 return (fuword(&ujob->_aiocb_private.error));
1460 aiocb_store_status(struct aiocb *ujob, long status)
1463 return (suword(&ujob->_aiocb_private.status, status));
1467 aiocb_store_error(struct aiocb *ujob, long error)
1470 return (suword(&ujob->_aiocb_private.error, error));
1474 aiocb_store_kernelinfo(struct aiocb *ujob, long jobref)
1477 return (suword(&ujob->_aiocb_private.kernelinfo, jobref));
1481 aiocb_store_aiocb(struct aiocb **ujobp, struct aiocb *ujob)
1484 return (suword(ujobp, (long)ujob));
1487 static struct aiocb_ops aiocb_ops = {
1488 .copyin = aiocb_copyin,
1489 .fetch_status = aiocb_fetch_status,
1490 .fetch_error = aiocb_fetch_error,
1491 .store_status = aiocb_store_status,
1492 .store_error = aiocb_store_error,
1493 .store_kernelinfo = aiocb_store_kernelinfo,
1494 .store_aiocb = aiocb_store_aiocb,
1497 static struct aiocb_ops aiocb_ops_osigevent = {
1498 .copyin = aiocb_copyin_old_sigevent,
1499 .fetch_status = aiocb_fetch_status,
1500 .fetch_error = aiocb_fetch_error,
1501 .store_status = aiocb_store_status,
1502 .store_error = aiocb_store_error,
1503 .store_kernelinfo = aiocb_store_kernelinfo,
1504 .store_aiocb = aiocb_store_aiocb,
1508 * Queue a new AIO request. Choosing either the threaded or direct physio VCHR
1509 * technique is done in this code.
1512 aio_aqueue(struct thread *td, struct aiocb *job, struct aioliojob *lj,
1513 int type, struct aiocb_ops *ops)
1515 struct proc *p = td->td_proc;
1518 struct aiocblist *aiocbe, *cb;
1519 struct kaioinfo *ki;
1527 if (p->p_aioinfo == NULL)
1528 aio_init_aioinfo(p);
1532 ops->store_status(job, -1);
1533 ops->store_error(job, 0);
1534 ops->store_kernelinfo(job, -1);
1536 if (num_queue_count >= max_queue_count ||
1537 ki->kaio_count >= ki->kaio_qallowed_count) {
1538 ops->store_error(job, EAGAIN);
1542 aiocbe = uma_zalloc(aiocb_zone, M_WAITOK | M_ZERO);
1543 aiocbe->inputcharge = 0;
1544 aiocbe->outputcharge = 0;
1545 knlist_init_mtx(&aiocbe->klist, AIO_MTX(ki));
1547 error = ops->copyin(job, &aiocbe->uaiocb);
1549 ops->store_error(job, error);
1550 uma_zfree(aiocb_zone, aiocbe);
1554 if (aiocbe->uaiocb.aio_sigevent.sigev_notify != SIGEV_KEVENT &&
1555 aiocbe->uaiocb.aio_sigevent.sigev_notify != SIGEV_SIGNAL &&
1556 aiocbe->uaiocb.aio_sigevent.sigev_notify != SIGEV_THREAD_ID &&
1557 aiocbe->uaiocb.aio_sigevent.sigev_notify != SIGEV_NONE) {
1558 ops->store_error(job, EINVAL);
1559 uma_zfree(aiocb_zone, aiocbe);
1563 if ((aiocbe->uaiocb.aio_sigevent.sigev_notify == SIGEV_SIGNAL ||
1564 aiocbe->uaiocb.aio_sigevent.sigev_notify == SIGEV_THREAD_ID) &&
1565 !_SIG_VALID(aiocbe->uaiocb.aio_sigevent.sigev_signo)) {
1566 uma_zfree(aiocb_zone, aiocbe);
1570 ksiginfo_init(&aiocbe->ksi);
1572 /* Save userspace address of the job info. */
1573 aiocbe->uuaiocb = job;
1575 /* Get the opcode. */
1576 if (type != LIO_NOP)
1577 aiocbe->uaiocb.aio_lio_opcode = type;
1578 opcode = aiocbe->uaiocb.aio_lio_opcode;
1580 /* Fetch the file object for the specified file descriptor. */
1581 fd = aiocbe->uaiocb.aio_fildes;
1584 error = fget_write(td, fd, &fp);
1587 error = fget_read(td, fd, &fp);
1590 error = fget(td, fd, &fp);
1593 uma_zfree(aiocb_zone, aiocbe);
1594 ops->store_error(job, error);
1598 if (opcode == LIO_SYNC && fp->f_vnode == NULL) {
1603 if (opcode != LIO_SYNC && aiocbe->uaiocb.aio_offset == -1LL) {
1608 aiocbe->fd_file = fp;
1610 mtx_lock(&aio_job_mtx);
1612 aiocbe->seqno = jobseqno++;
1613 mtx_unlock(&aio_job_mtx);
1614 error = ops->store_kernelinfo(job, jid);
1619 aiocbe->uaiocb._aiocb_private.kernelinfo = (void *)(intptr_t)jid;
1621 if (opcode == LIO_NOP) {
1623 uma_zfree(aiocb_zone, aiocbe);
1626 if ((opcode != LIO_READ) && (opcode != LIO_WRITE) &&
1627 (opcode != LIO_SYNC)) {
1632 if (aiocbe->uaiocb.aio_sigevent.sigev_notify != SIGEV_KEVENT)
1634 kqfd = aiocbe->uaiocb.aio_sigevent.sigev_notify_kqueue;
1635 kev.ident = (uintptr_t)aiocbe->uuaiocb;
1636 kev.filter = EVFILT_AIO;
1637 kev.flags = EV_ADD | EV_ENABLE | EV_FLAG1;
1638 kev.data = (intptr_t)aiocbe;
1639 kev.udata = aiocbe->uaiocb.aio_sigevent.sigev_value.sival_ptr;
1640 error = kqfd_register(kqfd, &kev, td, 1);
1644 uma_zfree(aiocb_zone, aiocbe);
1645 ops->store_error(job, error);
1650 ops->store_error(job, EINPROGRESS);
1651 aiocbe->uaiocb._aiocb_private.error = EINPROGRESS;
1652 aiocbe->userproc = p;
1653 aiocbe->cred = crhold(td->td_ucred);
1654 aiocbe->jobflags = 0;
1657 if (opcode == LIO_SYNC)
1660 if (fp->f_type == DTYPE_SOCKET) {
1662 * Alternate queueing for socket ops: Reach down into the
1663 * descriptor to get the socket data. Then check to see if the
1664 * socket is ready to be read or written (based on the requested
1667 * If it is not ready for io, then queue the aiocbe on the
1668 * socket, and set the flags so we get a call when sbnotify()
1671 * Note if opcode is neither LIO_WRITE nor LIO_READ we lock
1672 * and unlock the snd sockbuf for no reason.
1675 sb = (opcode == LIO_READ) ? &so->so_rcv : &so->so_snd;
1677 if (((opcode == LIO_READ) && (!soreadable(so))) || ((opcode ==
1678 LIO_WRITE) && (!sowriteable(so)))) {
1679 sb->sb_flags |= SB_AIO;
1681 mtx_lock(&aio_job_mtx);
1682 TAILQ_INSERT_TAIL(&so->so_aiojobq, aiocbe, list);
1683 mtx_unlock(&aio_job_mtx);
1686 TAILQ_INSERT_TAIL(&ki->kaio_all, aiocbe, allist);
1687 TAILQ_INSERT_TAIL(&ki->kaio_jobqueue, aiocbe, plist);
1688 aiocbe->jobstate = JOBST_JOBQSOCK;
1694 atomic_add_int(&num_queue_count, 1);
1701 if ((error = aio_qphysio(p, aiocbe)) == 0)
1705 aiocbe->uaiocb._aiocb_private.error = error;
1706 ops->store_error(job, error);
1711 /* No buffer for daemon I/O. */
1713 atomic_add_int(&num_queue_count, 1);
1719 TAILQ_INSERT_TAIL(&ki->kaio_jobqueue, aiocbe, plist);
1720 TAILQ_INSERT_TAIL(&ki->kaio_all, aiocbe, allist);
1721 if (opcode == LIO_SYNC) {
1722 TAILQ_FOREACH(cb, &ki->kaio_jobqueue, plist) {
1723 if (cb->fd_file == aiocbe->fd_file &&
1724 cb->uaiocb.aio_lio_opcode != LIO_SYNC &&
1725 cb->seqno < aiocbe->seqno) {
1726 cb->jobflags |= AIOCBLIST_CHECKSYNC;
1730 TAILQ_FOREACH(cb, &ki->kaio_bufqueue, plist) {
1731 if (cb->fd_file == aiocbe->fd_file &&
1732 cb->uaiocb.aio_lio_opcode != LIO_SYNC &&
1733 cb->seqno < aiocbe->seqno) {
1734 cb->jobflags |= AIOCBLIST_CHECKSYNC;
1738 if (aiocbe->pending != 0) {
1739 TAILQ_INSERT_TAIL(&ki->kaio_syncqueue, aiocbe, list);
1740 aiocbe->jobstate = JOBST_JOBQSYNC;
1745 mtx_lock(&aio_job_mtx);
1746 TAILQ_INSERT_TAIL(&aio_jobs, aiocbe, list);
1747 aiocbe->jobstate = JOBST_JOBQGLOBAL;
1749 mtx_unlock(&aio_job_mtx);
1757 aio_kick_nowait(struct proc *userp)
1759 struct kaioinfo *ki = userp->p_aioinfo;
1760 struct aiothreadlist *aiop;
1762 mtx_assert(&aio_job_mtx, MA_OWNED);
1763 if ((aiop = TAILQ_FIRST(&aio_freeproc)) != NULL) {
1764 TAILQ_REMOVE(&aio_freeproc, aiop, list);
1765 aiop->aiothreadflags &= ~AIOP_FREE;
1766 wakeup(aiop->aiothread);
1767 } else if (((num_aio_resv_start + num_aio_procs) < max_aio_procs) &&
1768 ((ki->kaio_active_count + num_aio_resv_start) <
1769 ki->kaio_maxactive_count)) {
1770 taskqueue_enqueue(taskqueue_aiod_bio, &ki->kaio_task);
1775 aio_kick(struct proc *userp)
1777 struct kaioinfo *ki = userp->p_aioinfo;
1778 struct aiothreadlist *aiop;
1781 mtx_assert(&aio_job_mtx, MA_OWNED);
1783 if ((aiop = TAILQ_FIRST(&aio_freeproc)) != NULL) {
1784 TAILQ_REMOVE(&aio_freeproc, aiop, list);
1785 aiop->aiothreadflags &= ~AIOP_FREE;
1786 wakeup(aiop->aiothread);
1787 } else if (((num_aio_resv_start + num_aio_procs) < max_aio_procs) &&
1788 ((ki->kaio_active_count + num_aio_resv_start) <
1789 ki->kaio_maxactive_count)) {
1790 num_aio_resv_start++;
1791 mtx_unlock(&aio_job_mtx);
1792 error = aio_newproc(&num_aio_resv_start);
1793 mtx_lock(&aio_job_mtx);
1795 num_aio_resv_start--;
1805 aio_kick_helper(void *context, int pending)
1807 struct proc *userp = context;
1809 mtx_lock(&aio_job_mtx);
1810 while (--pending >= 0) {
1811 if (aio_kick(userp))
1814 mtx_unlock(&aio_job_mtx);
1818 * Support the aio_return system call, as a side-effect, kernel resources are
1822 kern_aio_return(struct thread *td, struct aiocb *uaiocb, struct aiocb_ops *ops)
1824 struct proc *p = td->td_proc;
1825 struct aiocblist *cb;
1826 struct kaioinfo *ki;
1833 TAILQ_FOREACH(cb, &ki->kaio_done, plist) {
1834 if (cb->uuaiocb == uaiocb)
1838 MPASS(cb->jobstate == JOBST_JOBFINISHED);
1839 status = cb->uaiocb._aiocb_private.status;
1840 error = cb->uaiocb._aiocb_private.error;
1841 td->td_retval[0] = status;
1842 if (cb->uaiocb.aio_lio_opcode == LIO_WRITE) {
1843 td->td_ru.ru_oublock += cb->outputcharge;
1844 cb->outputcharge = 0;
1845 } else if (cb->uaiocb.aio_lio_opcode == LIO_READ) {
1846 td->td_ru.ru_inblock += cb->inputcharge;
1847 cb->inputcharge = 0;
1851 ops->store_error(uaiocb, error);
1852 ops->store_status(uaiocb, status);
1861 aio_return(struct thread *td, struct aio_return_args *uap)
1864 return (kern_aio_return(td, uap->aiocbp, &aiocb_ops));
1868 * Allow a process to wakeup when any of the I/O requests are completed.
1871 kern_aio_suspend(struct thread *td, int njoblist, struct aiocb **ujoblist,
1872 struct timespec *ts)
1874 struct proc *p = td->td_proc;
1876 struct kaioinfo *ki;
1877 struct aiocblist *cb, *cbfirst;
1882 if (ts->tv_nsec < 0 || ts->tv_nsec >= 1000000000)
1885 TIMESPEC_TO_TIMEVAL(&atv, ts);
1886 if (itimerfix(&atv))
1888 timo = tvtohz(&atv);
1902 TAILQ_FOREACH(cb, &ki->kaio_all, allist) {
1903 for (i = 0; i < njoblist; i++) {
1904 if (cb->uuaiocb == ujoblist[i]) {
1905 if (cbfirst == NULL)
1907 if (cb->jobstate == JOBST_JOBFINISHED)
1912 /* All tasks were finished. */
1913 if (cbfirst == NULL)
1916 ki->kaio_flags |= KAIO_WAKEUP;
1917 error = msleep(&p->p_aioinfo, AIO_MTX(ki), PRIBIO | PCATCH,
1919 if (error == ERESTART)
1930 aio_suspend(struct thread *td, struct aio_suspend_args *uap)
1932 struct timespec ts, *tsp;
1933 struct aiocb **ujoblist;
1936 if (uap->nent < 0 || uap->nent > AIO_LISTIO_MAX)
1940 /* Get timespec struct. */
1941 if ((error = copyin(uap->timeout, &ts, sizeof(ts))) != 0)
1947 ujoblist = uma_zalloc(aiol_zone, M_WAITOK);
1948 error = copyin(uap->aiocbp, ujoblist, uap->nent * sizeof(ujoblist[0]));
1950 error = kern_aio_suspend(td, uap->nent, ujoblist, tsp);
1951 uma_zfree(aiol_zone, ujoblist);
1956 * aio_cancel cancels any non-physio aio operations not currently in
1960 aio_cancel(struct thread *td, struct aio_cancel_args *uap)
1962 struct proc *p = td->td_proc;
1963 struct kaioinfo *ki;
1964 struct aiocblist *cbe, *cbn;
1970 int notcancelled = 0;
1973 /* Lookup file object. */
1974 error = fget(td, uap->fd, &fp);
1982 if (fp->f_type == DTYPE_VNODE) {
1984 if (vn_isdisk(vp, &error)) {
1986 td->td_retval[0] = AIO_NOTCANCELED;
1992 TAILQ_FOREACH_SAFE(cbe, &ki->kaio_jobqueue, plist, cbn) {
1993 if ((uap->fd == cbe->uaiocb.aio_fildes) &&
1994 ((uap->aiocbp == NULL) ||
1995 (uap->aiocbp == cbe->uuaiocb))) {
1998 mtx_lock(&aio_job_mtx);
1999 if (cbe->jobstate == JOBST_JOBQGLOBAL) {
2000 TAILQ_REMOVE(&aio_jobs, cbe, list);
2002 } else if (cbe->jobstate == JOBST_JOBQSOCK) {
2003 MPASS(fp->f_type == DTYPE_SOCKET);
2005 TAILQ_REMOVE(&so->so_aiojobq, cbe, list);
2007 } else if (cbe->jobstate == JOBST_JOBQSYNC) {
2008 TAILQ_REMOVE(&ki->kaio_syncqueue, cbe, list);
2011 mtx_unlock(&aio_job_mtx);
2014 TAILQ_REMOVE(&ki->kaio_jobqueue, cbe, plist);
2015 cbe->uaiocb._aiocb_private.status = -1;
2016 cbe->uaiocb._aiocb_private.error = ECANCELED;
2017 aio_bio_done_notify(p, cbe, DONE_QUEUE);
2022 if (uap->aiocbp != NULL)
2031 if (uap->aiocbp != NULL) {
2033 td->td_retval[0] = AIO_CANCELED;
2039 td->td_retval[0] = AIO_NOTCANCELED;
2044 td->td_retval[0] = AIO_CANCELED;
2048 td->td_retval[0] = AIO_ALLDONE;
2054 * aio_error is implemented in the kernel level for compatibility purposes
2055 * only. For a user mode async implementation, it would be best to do it in
2056 * a userland subroutine.
2059 kern_aio_error(struct thread *td, struct aiocb *aiocbp, struct aiocb_ops *ops)
2061 struct proc *p = td->td_proc;
2062 struct aiocblist *cb;
2063 struct kaioinfo *ki;
2068 td->td_retval[0] = EINVAL;
2073 TAILQ_FOREACH(cb, &ki->kaio_all, allist) {
2074 if (cb->uuaiocb == aiocbp) {
2075 if (cb->jobstate == JOBST_JOBFINISHED)
2077 cb->uaiocb._aiocb_private.error;
2079 td->td_retval[0] = EINPROGRESS;
2087 * Hack for failure of aio_aqueue.
2089 status = ops->fetch_status(aiocbp);
2091 td->td_retval[0] = ops->fetch_error(aiocbp);
2095 td->td_retval[0] = EINVAL;
2100 aio_error(struct thread *td, struct aio_error_args *uap)
2103 return (kern_aio_error(td, uap->aiocbp, &aiocb_ops));
2106 /* syscall - asynchronous read from a file (REALTIME) */
2108 oaio_read(struct thread *td, struct oaio_read_args *uap)
2111 return (aio_aqueue(td, (struct aiocb *)uap->aiocbp, NULL, LIO_READ,
2112 &aiocb_ops_osigevent));
2116 aio_read(struct thread *td, struct aio_read_args *uap)
2119 return (aio_aqueue(td, uap->aiocbp, NULL, LIO_READ, &aiocb_ops));
2122 /* syscall - asynchronous write to a file (REALTIME) */
2124 oaio_write(struct thread *td, struct oaio_write_args *uap)
2127 return (aio_aqueue(td, (struct aiocb *)uap->aiocbp, NULL, LIO_WRITE,
2128 &aiocb_ops_osigevent));
2132 aio_write(struct thread *td, struct aio_write_args *uap)
2135 return (aio_aqueue(td, uap->aiocbp, NULL, LIO_WRITE, &aiocb_ops));
2139 kern_lio_listio(struct thread *td, int mode, struct aiocb * const *uacb_list,
2140 struct aiocb **acb_list, int nent, struct sigevent *sig,
2141 struct aiocb_ops *ops)
2143 struct proc *p = td->td_proc;
2145 struct kaioinfo *ki;
2146 struct aioliojob *lj;
2152 if ((mode != LIO_NOWAIT) && (mode != LIO_WAIT))
2155 if (nent < 0 || nent > AIO_LISTIO_MAX)
2158 if (p->p_aioinfo == NULL)
2159 aio_init_aioinfo(p);
2163 lj = uma_zalloc(aiolio_zone, M_WAITOK);
2166 lj->lioj_finished_count = 0;
2167 knlist_init_mtx(&lj->klist, AIO_MTX(ki));
2168 ksiginfo_init(&lj->lioj_ksi);
2173 if (sig && (mode == LIO_NOWAIT)) {
2174 bcopy(sig, &lj->lioj_signal, sizeof(lj->lioj_signal));
2175 if (lj->lioj_signal.sigev_notify == SIGEV_KEVENT) {
2176 /* Assume only new style KEVENT */
2177 kev.filter = EVFILT_LIO;
2178 kev.flags = EV_ADD | EV_ENABLE | EV_FLAG1;
2179 kev.ident = (uintptr_t)uacb_list; /* something unique */
2180 kev.data = (intptr_t)lj;
2181 /* pass user defined sigval data */
2182 kev.udata = lj->lioj_signal.sigev_value.sival_ptr;
2183 error = kqfd_register(
2184 lj->lioj_signal.sigev_notify_kqueue, &kev, td, 1);
2186 uma_zfree(aiolio_zone, lj);
2189 } else if (lj->lioj_signal.sigev_notify == SIGEV_NONE) {
2191 } else if (lj->lioj_signal.sigev_notify == SIGEV_SIGNAL ||
2192 lj->lioj_signal.sigev_notify == SIGEV_THREAD_ID) {
2193 if (!_SIG_VALID(lj->lioj_signal.sigev_signo)) {
2194 uma_zfree(aiolio_zone, lj);
2197 lj->lioj_flags |= LIOJ_SIGNAL;
2199 uma_zfree(aiolio_zone, lj);
2205 TAILQ_INSERT_TAIL(&ki->kaio_liojoblist, lj, lioj_list);
2207 * Add extra aiocb count to avoid the lio to be freed
2208 * by other threads doing aio_waitcomplete or aio_return,
2209 * and prevent event from being sent until we have queued
2216 * Get pointers to the list of I/O requests.
2219 for (i = 0; i < nent; i++) {
2222 error = aio_aqueue(td, iocb, lj, LIO_NOP, ops);
2230 if (mode == LIO_WAIT) {
2231 while (lj->lioj_count - 1 != lj->lioj_finished_count) {
2232 ki->kaio_flags |= KAIO_WAKEUP;
2233 error = msleep(&p->p_aioinfo, AIO_MTX(ki),
2234 PRIBIO | PCATCH, "aiospn", 0);
2235 if (error == ERESTART)
2241 if (lj->lioj_count - 1 == lj->lioj_finished_count) {
2242 if (lj->lioj_signal.sigev_notify == SIGEV_KEVENT) {
2243 lj->lioj_flags |= LIOJ_KEVENT_POSTED;
2244 KNOTE_LOCKED(&lj->klist, 1);
2246 if ((lj->lioj_flags & (LIOJ_SIGNAL|LIOJ_SIGNAL_POSTED))
2248 && (lj->lioj_signal.sigev_notify == SIGEV_SIGNAL ||
2249 lj->lioj_signal.sigev_notify == SIGEV_THREAD_ID)) {
2250 aio_sendsig(p, &lj->lioj_signal,
2252 lj->lioj_flags |= LIOJ_SIGNAL_POSTED;
2257 if (lj->lioj_count == 0) {
2258 TAILQ_REMOVE(&ki->kaio_liojoblist, lj, lioj_list);
2259 knlist_delete(&lj->klist, curthread, 1);
2261 sigqueue_take(&lj->lioj_ksi);
2264 uma_zfree(aiolio_zone, lj);
2273 /* syscall - list directed I/O (REALTIME) */
2275 olio_listio(struct thread *td, struct olio_listio_args *uap)
2277 struct aiocb **acb_list;
2278 struct sigevent *sigp, sig;
2279 struct osigevent osig;
2282 if ((uap->mode != LIO_NOWAIT) && (uap->mode != LIO_WAIT))
2286 if (nent < 0 || nent > AIO_LISTIO_MAX)
2289 if (uap->sig && (uap->mode == LIO_NOWAIT)) {
2290 error = copyin(uap->sig, &osig, sizeof(osig));
2293 error = convert_old_sigevent(&osig, &sig);
2300 acb_list = malloc(sizeof(struct aiocb *) * nent, M_LIO, M_WAITOK);
2301 error = copyin(uap->acb_list, acb_list, nent * sizeof(acb_list[0]));
2303 error = kern_lio_listio(td, uap->mode,
2304 (struct aiocb * const *)uap->acb_list, acb_list, nent, sigp,
2305 &aiocb_ops_osigevent);
2306 free(acb_list, M_LIO);
2310 /* syscall - list directed I/O (REALTIME) */
2312 lio_listio(struct thread *td, struct lio_listio_args *uap)
2314 struct aiocb **acb_list;
2315 struct sigevent *sigp, sig;
2318 if ((uap->mode != LIO_NOWAIT) && (uap->mode != LIO_WAIT))
2322 if (nent < 0 || nent > AIO_LISTIO_MAX)
2325 if (uap->sig && (uap->mode == LIO_NOWAIT)) {
2326 error = copyin(uap->sig, &sig, sizeof(sig));
2333 acb_list = malloc(sizeof(struct aiocb *) * nent, M_LIO, M_WAITOK);
2334 error = copyin(uap->acb_list, acb_list, nent * sizeof(acb_list[0]));
2336 error = kern_lio_listio(td, uap->mode, uap->acb_list, acb_list,
2337 nent, sigp, &aiocb_ops);
2338 free(acb_list, M_LIO);
2343 * Called from interrupt thread for physio, we should return as fast
2344 * as possible, so we schedule a biohelper task.
2347 aio_physwakeup(struct buf *bp)
2349 struct aiocblist *aiocbe;
2351 aiocbe = (struct aiocblist *)bp->b_caller1;
2352 taskqueue_enqueue(taskqueue_aiod_bio, &aiocbe->biotask);
2356 * Task routine to perform heavy tasks, process wakeup, and signals.
2359 biohelper(void *context, int pending)
2361 struct aiocblist *aiocbe = context;
2364 struct kaioinfo *ki;
2368 userp = aiocbe->userproc;
2369 ki = userp->p_aioinfo;
2371 aiocbe->uaiocb._aiocb_private.status -= bp->b_resid;
2372 aiocbe->uaiocb._aiocb_private.error = 0;
2373 if (bp->b_ioflags & BIO_ERROR)
2374 aiocbe->uaiocb._aiocb_private.error = bp->b_error;
2375 nblks = btodb(aiocbe->uaiocb.aio_nbytes);
2376 if (aiocbe->uaiocb.aio_lio_opcode == LIO_WRITE)
2377 aiocbe->outputcharge += nblks;
2379 aiocbe->inputcharge += nblks;
2381 TAILQ_REMOVE(&userp->p_aioinfo->kaio_bufqueue, aiocbe, plist);
2382 ki->kaio_buffer_count--;
2383 aio_bio_done_notify(userp, aiocbe, DONE_BUF);
2386 /* Release mapping into kernel space. */
2389 atomic_subtract_int(&num_buf_aio, 1);
2392 /* syscall - wait for the next completion of an aio request */
2394 kern_aio_waitcomplete(struct thread *td, struct aiocb **aiocbp,
2395 struct timespec *ts, struct aiocb_ops *ops)
2397 struct proc *p = td->td_proc;
2399 struct kaioinfo *ki;
2400 struct aiocblist *cb;
2401 struct aiocb *uuaiocb;
2402 int error, status, timo;
2404 ops->store_aiocb(aiocbp, NULL);
2408 if ((ts->tv_nsec < 0) || (ts->tv_nsec >= 1000000000))
2411 TIMESPEC_TO_TIMEVAL(&atv, ts);
2412 if (itimerfix(&atv))
2414 timo = tvtohz(&atv);
2417 if (p->p_aioinfo == NULL)
2418 aio_init_aioinfo(p);
2424 while ((cb = TAILQ_FIRST(&ki->kaio_done)) == NULL) {
2425 ki->kaio_flags |= KAIO_WAKEUP;
2426 error = msleep(&p->p_aioinfo, AIO_MTX(ki), PRIBIO | PCATCH,
2428 if (timo && error == ERESTART)
2435 MPASS(cb->jobstate == JOBST_JOBFINISHED);
2436 uuaiocb = cb->uuaiocb;
2437 status = cb->uaiocb._aiocb_private.status;
2438 error = cb->uaiocb._aiocb_private.error;
2439 td->td_retval[0] = status;
2440 if (cb->uaiocb.aio_lio_opcode == LIO_WRITE) {
2441 td->td_ru.ru_oublock += cb->outputcharge;
2442 cb->outputcharge = 0;
2443 } else if (cb->uaiocb.aio_lio_opcode == LIO_READ) {
2444 td->td_ru.ru_inblock += cb->inputcharge;
2445 cb->inputcharge = 0;
2449 ops->store_aiocb(aiocbp, uuaiocb);
2450 ops->store_error(uuaiocb, error);
2451 ops->store_status(uuaiocb, status);
2459 aio_waitcomplete(struct thread *td, struct aio_waitcomplete_args *uap)
2461 struct timespec ts, *tsp;
2465 /* Get timespec struct. */
2466 error = copyin(uap->timeout, &ts, sizeof(ts));
2473 return (kern_aio_waitcomplete(td, uap->aiocbp, tsp, &aiocb_ops));
2477 kern_aio_fsync(struct thread *td, int op, struct aiocb *aiocbp,
2478 struct aiocb_ops *ops)
2480 struct proc *p = td->td_proc;
2481 struct kaioinfo *ki;
2483 if (op != O_SYNC) /* XXX lack of O_DSYNC */
2487 aio_init_aioinfo(p);
2488 return (aio_aqueue(td, aiocbp, NULL, LIO_SYNC, ops));
2492 aio_fsync(struct thread *td, struct aio_fsync_args *uap)
2495 return (kern_aio_fsync(td, uap->op, uap->aiocbp, &aiocb_ops));
2498 /* kqueue attach function */
2500 filt_aioattach(struct knote *kn)
2502 struct aiocblist *aiocbe = (struct aiocblist *)kn->kn_sdata;
2505 * The aiocbe pointer must be validated before using it, so
2506 * registration is restricted to the kernel; the user cannot
2509 if ((kn->kn_flags & EV_FLAG1) == 0)
2511 kn->kn_ptr.p_aio = aiocbe;
2512 kn->kn_flags &= ~EV_FLAG1;
2514 knlist_add(&aiocbe->klist, kn, 0);
2519 /* kqueue detach function */
2521 filt_aiodetach(struct knote *kn)
2523 struct aiocblist *aiocbe = kn->kn_ptr.p_aio;
2525 if (!knlist_empty(&aiocbe->klist))
2526 knlist_remove(&aiocbe->klist, kn, 0);
2529 /* kqueue filter function */
2532 filt_aio(struct knote *kn, long hint)
2534 struct aiocblist *aiocbe = kn->kn_ptr.p_aio;
2536 kn->kn_data = aiocbe->uaiocb._aiocb_private.error;
2537 if (aiocbe->jobstate != JOBST_JOBFINISHED)
2539 kn->kn_flags |= EV_EOF;
2543 /* kqueue attach function */
2545 filt_lioattach(struct knote *kn)
2547 struct aioliojob * lj = (struct aioliojob *)kn->kn_sdata;
2550 * The aioliojob pointer must be validated before using it, so
2551 * registration is restricted to the kernel; the user cannot
2554 if ((kn->kn_flags & EV_FLAG1) == 0)
2556 kn->kn_ptr.p_lio = lj;
2557 kn->kn_flags &= ~EV_FLAG1;
2559 knlist_add(&lj->klist, kn, 0);
2564 /* kqueue detach function */
2566 filt_liodetach(struct knote *kn)
2568 struct aioliojob * lj = kn->kn_ptr.p_lio;
2570 if (!knlist_empty(&lj->klist))
2571 knlist_remove(&lj->klist, kn, 0);
2574 /* kqueue filter function */
2577 filt_lio(struct knote *kn, long hint)
2579 struct aioliojob * lj = kn->kn_ptr.p_lio;
2581 return (lj->lioj_flags & LIOJ_KEVENT_POSTED);
2584 #ifdef COMPAT_FREEBSD32
2586 struct __aiocb_private32 {
2589 uint32_t kernelinfo;
2592 typedef struct oaiocb32 {
2593 int aio_fildes; /* File descriptor */
2594 uint64_t aio_offset __packed; /* File offset for I/O */
2595 uint32_t aio_buf; /* I/O buffer in process space */
2596 uint32_t aio_nbytes; /* Number of bytes for I/O */
2597 struct osigevent32 aio_sigevent; /* Signal to deliver */
2598 int aio_lio_opcode; /* LIO opcode */
2599 int aio_reqprio; /* Request priority -- ignored */
2600 struct __aiocb_private32 _aiocb_private;
2603 typedef struct aiocb32 {
2604 int32_t aio_fildes; /* File descriptor */
2605 uint64_t aio_offset __packed; /* File offset for I/O */
2606 uint32_t aio_buf; /* I/O buffer in process space */
2607 uint32_t aio_nbytes; /* Number of bytes for I/O */
2609 uint32_t __spare2__;
2610 int aio_lio_opcode; /* LIO opcode */
2611 int aio_reqprio; /* Request priority -- ignored */
2612 struct __aiocb_private32 _aiocb_private;
2613 struct sigevent32 aio_sigevent; /* Signal to deliver */
2617 convert_old_sigevent32(struct osigevent32 *osig, struct sigevent *nsig)
2621 * Only SIGEV_NONE, SIGEV_SIGNAL, and SIGEV_KEVENT are
2622 * supported by AIO with the old sigevent structure.
2624 CP(*osig, *nsig, sigev_notify);
2625 switch (nsig->sigev_notify) {
2629 nsig->sigev_signo = osig->__sigev_u.__sigev_signo;
2632 nsig->sigev_notify_kqueue =
2633 osig->__sigev_u.__sigev_notify_kqueue;
2634 PTRIN_CP(*osig, *nsig, sigev_value.sival_ptr);
2643 aiocb32_copyin_old_sigevent(struct aiocb *ujob, struct aiocb *kjob)
2645 struct oaiocb32 job32;
2648 bzero(kjob, sizeof(struct aiocb));
2649 error = copyin(ujob, &job32, sizeof(job32));
2653 CP(job32, *kjob, aio_fildes);
2654 CP(job32, *kjob, aio_offset);
2655 PTRIN_CP(job32, *kjob, aio_buf);
2656 CP(job32, *kjob, aio_nbytes);
2657 CP(job32, *kjob, aio_lio_opcode);
2658 CP(job32, *kjob, aio_reqprio);
2659 CP(job32, *kjob, _aiocb_private.status);
2660 CP(job32, *kjob, _aiocb_private.error);
2661 PTRIN_CP(job32, *kjob, _aiocb_private.kernelinfo);
2662 return (convert_old_sigevent32(&job32.aio_sigevent,
2663 &kjob->aio_sigevent));
2667 convert_sigevent32(struct sigevent32 *sig32, struct sigevent *sig)
2670 CP(*sig32, *sig, sigev_notify);
2671 switch (sig->sigev_notify) {
2674 case SIGEV_THREAD_ID:
2675 CP(*sig32, *sig, sigev_notify_thread_id);
2678 CP(*sig32, *sig, sigev_signo);
2681 CP(*sig32, *sig, sigev_notify_kqueue);
2682 PTRIN_CP(*sig32, *sig, sigev_value.sival_ptr);
2691 aiocb32_copyin(struct aiocb *ujob, struct aiocb *kjob)
2693 struct aiocb32 job32;
2696 error = copyin(ujob, &job32, sizeof(job32));
2699 CP(job32, *kjob, aio_fildes);
2700 CP(job32, *kjob, aio_offset);
2701 PTRIN_CP(job32, *kjob, aio_buf);
2702 CP(job32, *kjob, aio_nbytes);
2703 CP(job32, *kjob, aio_lio_opcode);
2704 CP(job32, *kjob, aio_reqprio);
2705 CP(job32, *kjob, _aiocb_private.status);
2706 CP(job32, *kjob, _aiocb_private.error);
2707 PTRIN_CP(job32, *kjob, _aiocb_private.kernelinfo);
2708 return (convert_sigevent32(&job32.aio_sigevent, &kjob->aio_sigevent));
2712 aiocb32_fetch_status(struct aiocb *ujob)
2714 struct aiocb32 *ujob32;
2716 ujob32 = (struct aiocb32 *)ujob;
2717 return (fuword32(&ujob32->_aiocb_private.status));
2721 aiocb32_fetch_error(struct aiocb *ujob)
2723 struct aiocb32 *ujob32;
2725 ujob32 = (struct aiocb32 *)ujob;
2726 return (fuword32(&ujob32->_aiocb_private.error));
2730 aiocb32_store_status(struct aiocb *ujob, long status)
2732 struct aiocb32 *ujob32;
2734 ujob32 = (struct aiocb32 *)ujob;
2735 return (suword32(&ujob32->_aiocb_private.status, status));
2739 aiocb32_store_error(struct aiocb *ujob, long error)
2741 struct aiocb32 *ujob32;
2743 ujob32 = (struct aiocb32 *)ujob;
2744 return (suword32(&ujob32->_aiocb_private.error, error));
2748 aiocb32_store_kernelinfo(struct aiocb *ujob, long jobref)
2750 struct aiocb32 *ujob32;
2752 ujob32 = (struct aiocb32 *)ujob;
2753 return (suword32(&ujob32->_aiocb_private.kernelinfo, jobref));
2757 aiocb32_store_aiocb(struct aiocb **ujobp, struct aiocb *ujob)
2760 return (suword32(ujobp, (long)ujob));
2763 static struct aiocb_ops aiocb32_ops = {
2764 .copyin = aiocb32_copyin,
2765 .fetch_status = aiocb32_fetch_status,
2766 .fetch_error = aiocb32_fetch_error,
2767 .store_status = aiocb32_store_status,
2768 .store_error = aiocb32_store_error,
2769 .store_kernelinfo = aiocb32_store_kernelinfo,
2770 .store_aiocb = aiocb32_store_aiocb,
2773 static struct aiocb_ops aiocb32_ops_osigevent = {
2774 .copyin = aiocb32_copyin_old_sigevent,
2775 .fetch_status = aiocb32_fetch_status,
2776 .fetch_error = aiocb32_fetch_error,
2777 .store_status = aiocb32_store_status,
2778 .store_error = aiocb32_store_error,
2779 .store_kernelinfo = aiocb32_store_kernelinfo,
2780 .store_aiocb = aiocb32_store_aiocb,
2784 freebsd32_aio_return(struct thread *td, struct freebsd32_aio_return_args *uap)
2787 return (kern_aio_return(td, (struct aiocb *)uap->aiocbp, &aiocb32_ops));
2791 freebsd32_aio_suspend(struct thread *td, struct freebsd32_aio_suspend_args *uap)
2793 struct timespec32 ts32;
2794 struct timespec ts, *tsp;
2795 struct aiocb **ujoblist;
2796 uint32_t *ujoblist32;
2799 if (uap->nent < 0 || uap->nent > AIO_LISTIO_MAX)
2803 /* Get timespec struct. */
2804 if ((error = copyin(uap->timeout, &ts32, sizeof(ts32))) != 0)
2806 CP(ts32, ts, tv_sec);
2807 CP(ts32, ts, tv_nsec);
2812 ujoblist = uma_zalloc(aiol_zone, M_WAITOK);
2813 ujoblist32 = (uint32_t *)ujoblist;
2814 error = copyin(uap->aiocbp, ujoblist32, uap->nent *
2815 sizeof(ujoblist32[0]));
2817 for (i = uap->nent; i > 0; i--)
2818 ujoblist[i] = PTRIN(ujoblist32[i]);
2820 error = kern_aio_suspend(td, uap->nent, ujoblist, tsp);
2822 uma_zfree(aiol_zone, ujoblist);
2827 freebsd32_aio_cancel(struct thread *td, struct freebsd32_aio_cancel_args *uap)
2830 return (aio_cancel(td, (struct aio_cancel_args *)uap));
2834 freebsd32_aio_error(struct thread *td, struct freebsd32_aio_error_args *uap)
2837 return (kern_aio_error(td, (struct aiocb *)uap->aiocbp, &aiocb32_ops));
2841 freebsd32_oaio_read(struct thread *td, struct freebsd32_oaio_read_args *uap)
2844 return (aio_aqueue(td, (struct aiocb *)uap->aiocbp, NULL, LIO_READ,
2845 &aiocb32_ops_osigevent));
2849 freebsd32_aio_read(struct thread *td, struct freebsd32_aio_read_args *uap)
2852 return (aio_aqueue(td, (struct aiocb *)uap->aiocbp, NULL, LIO_READ,
2857 freebsd32_oaio_write(struct thread *td, struct freebsd32_oaio_write_args *uap)
2860 return (aio_aqueue(td, (struct aiocb *)uap->aiocbp, NULL, LIO_WRITE,
2861 &aiocb32_ops_osigevent));
2865 freebsd32_aio_write(struct thread *td, struct freebsd32_aio_write_args *uap)
2868 return (aio_aqueue(td, (struct aiocb *)uap->aiocbp, NULL, LIO_WRITE,
2873 freebsd32_aio_waitcomplete(struct thread *td,
2874 struct freebsd32_aio_waitcomplete_args *uap)
2876 struct timespec32 ts32;
2877 struct timespec ts, *tsp;
2881 /* Get timespec struct. */
2882 error = copyin(uap->timeout, &ts32, sizeof(ts32));
2885 CP(ts32, ts, tv_sec);
2886 CP(ts32, ts, tv_nsec);
2891 return (kern_aio_waitcomplete(td, (struct aiocb **)uap->aiocbp, tsp,
2896 freebsd32_aio_fsync(struct thread *td, struct freebsd32_aio_fsync_args *uap)
2899 return (kern_aio_fsync(td, uap->op, (struct aiocb *)uap->aiocbp,
2904 freebsd32_olio_listio(struct thread *td, struct freebsd32_olio_listio_args *uap)
2906 struct aiocb **acb_list;
2907 struct sigevent *sigp, sig;
2908 struct osigevent32 osig;
2909 uint32_t *acb_list32;
2912 if ((uap->mode != LIO_NOWAIT) && (uap->mode != LIO_WAIT))
2916 if (nent < 0 || nent > AIO_LISTIO_MAX)
2919 if (uap->sig && (uap->mode == LIO_NOWAIT)) {
2920 error = copyin(uap->sig, &osig, sizeof(osig));
2923 error = convert_old_sigevent32(&osig, &sig);
2930 acb_list32 = malloc(sizeof(uint32_t) * nent, M_LIO, M_WAITOK);
2931 error = copyin(uap->acb_list, acb_list32, nent * sizeof(uint32_t));
2933 free(acb_list32, M_LIO);
2936 acb_list = malloc(sizeof(struct aiocb *) * nent, M_LIO, M_WAITOK);
2937 for (i = 0; i < nent; i++)
2938 acb_list[i] = PTRIN(acb_list32[i]);
2939 free(acb_list32, M_LIO);
2941 error = kern_lio_listio(td, uap->mode,
2942 (struct aiocb * const *)uap->acb_list, acb_list, nent, sigp,
2943 &aiocb32_ops_osigevent);
2944 free(acb_list, M_LIO);
2949 freebsd32_lio_listio(struct thread *td, struct freebsd32_lio_listio_args *uap)
2951 struct aiocb **acb_list;
2952 struct sigevent *sigp, sig;
2953 struct sigevent32 sig32;
2954 uint32_t *acb_list32;
2957 if ((uap->mode != LIO_NOWAIT) && (uap->mode != LIO_WAIT))
2961 if (nent < 0 || nent > AIO_LISTIO_MAX)
2964 if (uap->sig && (uap->mode == LIO_NOWAIT)) {
2965 error = copyin(uap->sig, &sig32, sizeof(sig32));
2968 error = convert_sigevent32(&sig32, &sig);
2975 acb_list32 = malloc(sizeof(uint32_t) * nent, M_LIO, M_WAITOK);
2976 error = copyin(uap->acb_list, acb_list32, nent * sizeof(uint32_t));
2978 free(acb_list32, M_LIO);
2981 acb_list = malloc(sizeof(struct aiocb *) * nent, M_LIO, M_WAITOK);
2982 for (i = 0; i < nent; i++)
2983 acb_list[i] = PTRIN(acb_list32[i]);
2984 free(acb_list32, M_LIO);
2986 error = kern_lio_listio(td, uap->mode,
2987 (struct aiocb * const *)uap->acb_list, acb_list, nent, sigp,
2989 free(acb_list, M_LIO);