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/capability.h>
32 #include <sys/eventhandler.h>
33 #include <sys/sysproto.h>
34 #include <sys/filedesc.h>
35 #include <sys/kernel.h>
36 #include <sys/module.h>
37 #include <sys/kthread.h>
38 #include <sys/fcntl.h>
40 #include <sys/limits.h>
42 #include <sys/mutex.h>
43 #include <sys/unistd.h>
44 #include <sys/posix4.h>
46 #include <sys/resourcevar.h>
47 #include <sys/signalvar.h>
48 #include <sys/protosw.h>
49 #include <sys/rwlock.h>
51 #include <sys/socket.h>
52 #include <sys/socketvar.h>
53 #include <sys/syscall.h>
54 #include <sys/sysent.h>
55 #include <sys/sysctl.h>
57 #include <sys/taskqueue.h>
58 #include <sys/vnode.h>
60 #include <sys/event.h>
61 #include <sys/mount.h>
63 #include <machine/atomic.h>
66 #include <vm/vm_extern.h>
68 #include <vm/vm_map.h>
69 #include <vm/vm_object.h>
73 #include "opt_vfs_aio.h"
76 * Counter for allocating reference ids to new jobs. Wrapped to 1 on
77 * overflow. (XXX will be removed soon.)
79 static u_long jobrefid;
82 * Counter for aio_fsync.
84 static uint64_t jobseqno;
87 #define JOBST_JOBQSOCK 1
88 #define JOBST_JOBQGLOBAL 2
89 #define JOBST_JOBRUNNING 3
90 #define JOBST_JOBFINISHED 4
91 #define JOBST_JOBQBUF 5
92 #define JOBST_JOBQSYNC 6
94 #ifndef MAX_AIO_PER_PROC
95 #define MAX_AIO_PER_PROC 32
98 #ifndef MAX_AIO_QUEUE_PER_PROC
99 #define MAX_AIO_QUEUE_PER_PROC 256 /* Bigger than AIO_LISTIO_MAX */
102 #ifndef MAX_AIO_PROCS
103 #define MAX_AIO_PROCS 32
106 #ifndef MAX_AIO_QUEUE
107 #define MAX_AIO_QUEUE 1024 /* Bigger than AIO_LISTIO_MAX */
110 #ifndef TARGET_AIO_PROCS
111 #define TARGET_AIO_PROCS 4
115 #define MAX_BUF_AIO 16
118 #ifndef AIOD_TIMEOUT_DEFAULT
119 #define AIOD_TIMEOUT_DEFAULT (10 * hz)
122 #ifndef AIOD_LIFETIME_DEFAULT
123 #define AIOD_LIFETIME_DEFAULT (30 * hz)
126 FEATURE(aio, "Asynchronous I/O");
128 static MALLOC_DEFINE(M_LIO, "lio", "listio aio control block list");
130 static SYSCTL_NODE(_vfs, OID_AUTO, aio, CTLFLAG_RW, 0, "Async IO management");
132 static int max_aio_procs = MAX_AIO_PROCS;
133 SYSCTL_INT(_vfs_aio, OID_AUTO, max_aio_procs,
134 CTLFLAG_RW, &max_aio_procs, 0,
135 "Maximum number of kernel threads to use for handling async IO ");
137 static int num_aio_procs = 0;
138 SYSCTL_INT(_vfs_aio, OID_AUTO, num_aio_procs,
139 CTLFLAG_RD, &num_aio_procs, 0,
140 "Number of presently active kernel threads for async IO");
143 * The code will adjust the actual number of AIO processes towards this
144 * number when it gets a chance.
146 static int target_aio_procs = TARGET_AIO_PROCS;
147 SYSCTL_INT(_vfs_aio, OID_AUTO, target_aio_procs, CTLFLAG_RW, &target_aio_procs,
148 0, "Preferred number of ready kernel threads for async IO");
150 static int max_queue_count = MAX_AIO_QUEUE;
151 SYSCTL_INT(_vfs_aio, OID_AUTO, max_aio_queue, CTLFLAG_RW, &max_queue_count, 0,
152 "Maximum number of aio requests to queue, globally");
154 static int num_queue_count = 0;
155 SYSCTL_INT(_vfs_aio, OID_AUTO, num_queue_count, CTLFLAG_RD, &num_queue_count, 0,
156 "Number of queued aio requests");
158 static int num_buf_aio = 0;
159 SYSCTL_INT(_vfs_aio, OID_AUTO, num_buf_aio, CTLFLAG_RD, &num_buf_aio, 0,
160 "Number of aio requests presently handled by the buf subsystem");
162 /* Number of async I/O thread in the process of being started */
163 /* XXX This should be local to aio_aqueue() */
164 static int num_aio_resv_start = 0;
166 static int aiod_timeout;
167 SYSCTL_INT(_vfs_aio, OID_AUTO, aiod_timeout, CTLFLAG_RW, &aiod_timeout, 0,
168 "Timeout value for synchronous aio operations");
170 static int aiod_lifetime;
171 SYSCTL_INT(_vfs_aio, OID_AUTO, aiod_lifetime, CTLFLAG_RW, &aiod_lifetime, 0,
172 "Maximum lifetime for idle aiod");
174 static int unloadable = 0;
175 SYSCTL_INT(_vfs_aio, OID_AUTO, unloadable, CTLFLAG_RW, &unloadable, 0,
176 "Allow unload of aio (not recommended)");
179 static int max_aio_per_proc = MAX_AIO_PER_PROC;
180 SYSCTL_INT(_vfs_aio, OID_AUTO, max_aio_per_proc, CTLFLAG_RW, &max_aio_per_proc,
181 0, "Maximum active aio requests per process (stored in the process)");
183 static int max_aio_queue_per_proc = MAX_AIO_QUEUE_PER_PROC;
184 SYSCTL_INT(_vfs_aio, OID_AUTO, max_aio_queue_per_proc, CTLFLAG_RW,
185 &max_aio_queue_per_proc, 0,
186 "Maximum queued aio requests per process (stored in the process)");
188 static int max_buf_aio = MAX_BUF_AIO;
189 SYSCTL_INT(_vfs_aio, OID_AUTO, max_buf_aio, CTLFLAG_RW, &max_buf_aio, 0,
190 "Maximum buf aio requests per process (stored in the process)");
192 typedef struct oaiocb {
193 int aio_fildes; /* File descriptor */
194 off_t aio_offset; /* File offset for I/O */
195 volatile void *aio_buf; /* I/O buffer in process space */
196 size_t aio_nbytes; /* Number of bytes for I/O */
197 struct osigevent aio_sigevent; /* Signal to deliver */
198 int aio_lio_opcode; /* LIO opcode */
199 int aio_reqprio; /* Request priority -- ignored */
200 struct __aiocb_private _aiocb_private;
204 * Below is a key of locks used to protect each member of struct aiocblist
205 * aioliojob and kaioinfo and any backends.
207 * * - need not protected
208 * a - locked by kaioinfo lock
209 * b - locked by backend lock, the backend lock can be null in some cases,
210 * for example, BIO belongs to this type, in this case, proc lock is
212 * c - locked by aio_job_mtx, the lock for the generic file I/O backend.
216 * Current, there is only two backends: BIO and generic file I/O.
217 * socket I/O is served by generic file I/O, this is not a good idea, since
218 * disk file I/O and any other types without O_NONBLOCK flag can block daemon
219 * threads, if there is no thread to serve socket I/O, the socket I/O will be
220 * delayed too long or starved, we should create some threads dedicated to
221 * sockets to do non-blocking I/O, same for pipe and fifo, for these I/O
222 * systems we really need non-blocking interface, fiddling O_NONBLOCK in file
223 * structure is not safe because there is race between userland and aio
228 TAILQ_ENTRY(aiocblist) list; /* (b) internal list of for backend */
229 TAILQ_ENTRY(aiocblist) plist; /* (a) list of jobs for each backend */
230 TAILQ_ENTRY(aiocblist) allist; /* (a) list of all jobs in proc */
231 int jobflags; /* (a) job flags */
232 int jobstate; /* (b) job state */
233 int inputcharge; /* (*) input blockes */
234 int outputcharge; /* (*) output blockes */
235 struct buf *bp; /* (*) private to BIO backend,
238 struct proc *userproc; /* (*) user process */
239 struct ucred *cred; /* (*) active credential when created */
240 struct file *fd_file; /* (*) pointer to file structure */
241 struct aioliojob *lio; /* (*) optional lio job */
242 struct aiocb *uuaiocb; /* (*) pointer in userspace of aiocb */
243 struct knlist klist; /* (a) list of knotes */
244 struct aiocb uaiocb; /* (*) kernel I/O control block */
245 ksiginfo_t ksi; /* (a) realtime signal info */
246 struct task biotask; /* (*) private to BIO backend */
247 uint64_t seqno; /* (*) job number */
248 int pending; /* (a) number of pending I/O, aio_fsync only */
252 #define AIOCBLIST_DONE 0x01
253 #define AIOCBLIST_BUFDONE 0x02
254 #define AIOCBLIST_RUNDOWN 0x04
255 #define AIOCBLIST_CHECKSYNC 0x08
260 #define AIOP_FREE 0x1 /* proc on free queue */
262 struct aiothreadlist {
263 int aiothreadflags; /* (c) AIO proc flags */
264 TAILQ_ENTRY(aiothreadlist) list; /* (c) list of processes */
265 struct thread *aiothread; /* (*) the AIO thread */
269 * data-structure for lio signal management
272 int lioj_flags; /* (a) listio flags */
273 int lioj_count; /* (a) listio flags */
274 int lioj_finished_count; /* (a) listio flags */
275 struct sigevent lioj_signal; /* (a) signal on all I/O done */
276 TAILQ_ENTRY(aioliojob) lioj_list; /* (a) lio list */
277 struct knlist klist; /* (a) list of knotes */
278 ksiginfo_t lioj_ksi; /* (a) Realtime signal info */
281 #define LIOJ_SIGNAL 0x1 /* signal on all done (lio) */
282 #define LIOJ_SIGNAL_POSTED 0x2 /* signal has been posted */
283 #define LIOJ_KEVENT_POSTED 0x4 /* kevent triggered */
286 * per process aio data structure
289 struct mtx kaio_mtx; /* the lock to protect this struct */
290 int kaio_flags; /* (a) per process kaio flags */
291 int kaio_maxactive_count; /* (*) maximum number of AIOs */
292 int kaio_active_count; /* (c) number of currently used AIOs */
293 int kaio_qallowed_count; /* (*) maxiumu size of AIO queue */
294 int kaio_count; /* (a) size of AIO queue */
295 int kaio_ballowed_count; /* (*) maximum number of buffers */
296 int kaio_buffer_count; /* (a) number of physio buffers */
297 TAILQ_HEAD(,aiocblist) kaio_all; /* (a) all AIOs in the process */
298 TAILQ_HEAD(,aiocblist) kaio_done; /* (a) done queue for process */
299 TAILQ_HEAD(,aioliojob) kaio_liojoblist; /* (a) list of lio jobs */
300 TAILQ_HEAD(,aiocblist) kaio_jobqueue; /* (a) job queue for process */
301 TAILQ_HEAD(,aiocblist) kaio_bufqueue; /* (a) buffer job queue for process */
302 TAILQ_HEAD(,aiocblist) kaio_sockqueue; /* (a) queue for aios waiting on sockets,
305 TAILQ_HEAD(,aiocblist) kaio_syncqueue; /* (a) queue for aio_fsync */
306 struct task kaio_task; /* (*) task to kick aio threads */
309 #define AIO_LOCK(ki) mtx_lock(&(ki)->kaio_mtx)
310 #define AIO_UNLOCK(ki) mtx_unlock(&(ki)->kaio_mtx)
311 #define AIO_LOCK_ASSERT(ki, f) mtx_assert(&(ki)->kaio_mtx, (f))
312 #define AIO_MTX(ki) (&(ki)->kaio_mtx)
314 #define KAIO_RUNDOWN 0x1 /* process is being run down */
315 #define KAIO_WAKEUP 0x2 /* wakeup process when there is a significant event */
318 * Operations used to interact with userland aio control blocks.
319 * Different ABIs provide their own operations.
322 int (*copyin)(struct aiocb *ujob, struct aiocb *kjob);
323 long (*fetch_status)(struct aiocb *ujob);
324 long (*fetch_error)(struct aiocb *ujob);
325 int (*store_status)(struct aiocb *ujob, long status);
326 int (*store_error)(struct aiocb *ujob, long error);
327 int (*store_kernelinfo)(struct aiocb *ujob, long jobref);
328 int (*store_aiocb)(struct aiocb **ujobp, struct aiocb *ujob);
331 static TAILQ_HEAD(,aiothreadlist) aio_freeproc; /* (c) Idle daemons */
332 static struct sema aio_newproc_sem;
333 static struct mtx aio_job_mtx;
334 static struct mtx aio_sock_mtx;
335 static TAILQ_HEAD(,aiocblist) aio_jobs; /* (c) Async job list */
336 static struct unrhdr *aiod_unr;
338 void aio_init_aioinfo(struct proc *p);
339 static int aio_onceonly(void);
340 static int aio_free_entry(struct aiocblist *aiocbe);
341 static void aio_process_rw(struct aiocblist *aiocbe);
342 static void aio_process_sync(struct aiocblist *aiocbe);
343 static void aio_process_mlock(struct aiocblist *aiocbe);
344 static int aio_newproc(int *);
345 int aio_aqueue(struct thread *td, struct aiocb *job,
346 struct aioliojob *lio, int type, struct aiocb_ops *ops);
347 static void aio_physwakeup(struct buf *bp);
348 static void aio_proc_rundown(void *arg, struct proc *p);
349 static void aio_proc_rundown_exec(void *arg, struct proc *p, struct image_params *imgp);
350 static int aio_qphysio(struct proc *p, struct aiocblist *iocb);
351 static void biohelper(void *, int);
352 static void aio_daemon(void *param);
353 static void aio_swake_cb(struct socket *, struct sockbuf *);
354 static int aio_unload(void);
355 static void aio_bio_done_notify(struct proc *userp, struct aiocblist *aiocbe, int type);
358 static int aio_kick(struct proc *userp);
359 static void aio_kick_nowait(struct proc *userp);
360 static void aio_kick_helper(void *context, int pending);
361 static int filt_aioattach(struct knote *kn);
362 static void filt_aiodetach(struct knote *kn);
363 static int filt_aio(struct knote *kn, long hint);
364 static int filt_lioattach(struct knote *kn);
365 static void filt_liodetach(struct knote *kn);
366 static int filt_lio(struct knote *kn, long hint);
370 * kaio Per process async io info
371 * aiop async io thread data
372 * aiocb async io jobs
373 * aiol list io job pointer - internal to aio_suspend XXX
374 * aiolio list io jobs
376 static uma_zone_t kaio_zone, aiop_zone, aiocb_zone, aiol_zone, aiolio_zone;
378 /* kqueue filters for aio */
379 static struct filterops aio_filtops = {
381 .f_attach = filt_aioattach,
382 .f_detach = filt_aiodetach,
385 static struct filterops lio_filtops = {
387 .f_attach = filt_lioattach,
388 .f_detach = filt_liodetach,
392 static eventhandler_tag exit_tag, exec_tag;
394 TASKQUEUE_DEFINE_THREAD(aiod_bio);
397 * Main operations function for use as a kernel module.
400 aio_modload(struct module *module, int cmd, void *arg)
409 error = aio_unload();
420 static moduledata_t aio_mod = {
426 static struct syscall_helper_data aio_syscalls[] = {
427 SYSCALL_INIT_HELPER(aio_cancel),
428 SYSCALL_INIT_HELPER(aio_error),
429 SYSCALL_INIT_HELPER(aio_fsync),
430 SYSCALL_INIT_HELPER(aio_mlock),
431 SYSCALL_INIT_HELPER(aio_read),
432 SYSCALL_INIT_HELPER(aio_return),
433 SYSCALL_INIT_HELPER(aio_suspend),
434 SYSCALL_INIT_HELPER(aio_waitcomplete),
435 SYSCALL_INIT_HELPER(aio_write),
436 SYSCALL_INIT_HELPER(lio_listio),
437 SYSCALL_INIT_HELPER(oaio_read),
438 SYSCALL_INIT_HELPER(oaio_write),
439 SYSCALL_INIT_HELPER(olio_listio),
443 #ifdef COMPAT_FREEBSD32
444 #include <sys/mount.h>
445 #include <sys/socket.h>
446 #include <compat/freebsd32/freebsd32.h>
447 #include <compat/freebsd32/freebsd32_proto.h>
448 #include <compat/freebsd32/freebsd32_signal.h>
449 #include <compat/freebsd32/freebsd32_syscall.h>
450 #include <compat/freebsd32/freebsd32_util.h>
452 static struct syscall_helper_data aio32_syscalls[] = {
453 SYSCALL32_INIT_HELPER(freebsd32_aio_return),
454 SYSCALL32_INIT_HELPER(freebsd32_aio_suspend),
455 SYSCALL32_INIT_HELPER(freebsd32_aio_cancel),
456 SYSCALL32_INIT_HELPER(freebsd32_aio_error),
457 SYSCALL32_INIT_HELPER(freebsd32_aio_fsync),
458 SYSCALL32_INIT_HELPER(freebsd32_aio_mlock),
459 SYSCALL32_INIT_HELPER(freebsd32_aio_read),
460 SYSCALL32_INIT_HELPER(freebsd32_aio_write),
461 SYSCALL32_INIT_HELPER(freebsd32_aio_waitcomplete),
462 SYSCALL32_INIT_HELPER(freebsd32_lio_listio),
463 SYSCALL32_INIT_HELPER(freebsd32_oaio_read),
464 SYSCALL32_INIT_HELPER(freebsd32_oaio_write),
465 SYSCALL32_INIT_HELPER(freebsd32_olio_listio),
470 DECLARE_MODULE(aio, aio_mod,
471 SI_SUB_VFS, SI_ORDER_ANY);
472 MODULE_VERSION(aio, 1);
475 * Startup initialization
482 /* XXX: should probably just use so->callback */
483 aio_swake = &aio_swake_cb;
484 exit_tag = EVENTHANDLER_REGISTER(process_exit, aio_proc_rundown, NULL,
485 EVENTHANDLER_PRI_ANY);
486 exec_tag = EVENTHANDLER_REGISTER(process_exec, aio_proc_rundown_exec, NULL,
487 EVENTHANDLER_PRI_ANY);
488 kqueue_add_filteropts(EVFILT_AIO, &aio_filtops);
489 kqueue_add_filteropts(EVFILT_LIO, &lio_filtops);
490 TAILQ_INIT(&aio_freeproc);
491 sema_init(&aio_newproc_sem, 0, "aio_new_proc");
492 mtx_init(&aio_job_mtx, "aio_job", NULL, MTX_DEF);
493 mtx_init(&aio_sock_mtx, "aio_sock", NULL, MTX_DEF);
494 TAILQ_INIT(&aio_jobs);
495 aiod_unr = new_unrhdr(1, INT_MAX, NULL);
496 kaio_zone = uma_zcreate("AIO", sizeof(struct kaioinfo), NULL, NULL,
497 NULL, NULL, UMA_ALIGN_PTR, UMA_ZONE_NOFREE);
498 aiop_zone = uma_zcreate("AIOP", sizeof(struct aiothreadlist), NULL,
499 NULL, NULL, NULL, UMA_ALIGN_PTR, UMA_ZONE_NOFREE);
500 aiocb_zone = uma_zcreate("AIOCB", sizeof(struct aiocblist), NULL, NULL,
501 NULL, NULL, UMA_ALIGN_PTR, UMA_ZONE_NOFREE);
502 aiol_zone = uma_zcreate("AIOL", AIO_LISTIO_MAX*sizeof(intptr_t) , NULL,
503 NULL, NULL, NULL, UMA_ALIGN_PTR, UMA_ZONE_NOFREE);
504 aiolio_zone = uma_zcreate("AIOLIO", sizeof(struct aioliojob), NULL,
505 NULL, NULL, NULL, UMA_ALIGN_PTR, UMA_ZONE_NOFREE);
506 aiod_timeout = AIOD_TIMEOUT_DEFAULT;
507 aiod_lifetime = AIOD_LIFETIME_DEFAULT;
509 async_io_version = _POSIX_VERSION;
510 p31b_setcfg(CTL_P1003_1B_AIO_LISTIO_MAX, AIO_LISTIO_MAX);
511 p31b_setcfg(CTL_P1003_1B_AIO_MAX, MAX_AIO_QUEUE);
512 p31b_setcfg(CTL_P1003_1B_AIO_PRIO_DELTA_MAX, 0);
514 error = syscall_helper_register(aio_syscalls);
517 #ifdef COMPAT_FREEBSD32
518 error = syscall32_helper_register(aio32_syscalls);
526 * Callback for unload of AIO when used as a module.
534 * XXX: no unloads by default, it's too dangerous.
535 * perhaps we could do it if locked out callers and then
536 * did an aio_proc_rundown() on each process.
538 * jhb: aio_proc_rundown() needs to run on curproc though,
539 * so I don't think that would fly.
544 #ifdef COMPAT_FREEBSD32
545 syscall32_helper_unregister(aio32_syscalls);
547 syscall_helper_unregister(aio_syscalls);
549 error = kqueue_del_filteropts(EVFILT_AIO);
552 error = kqueue_del_filteropts(EVFILT_LIO);
555 async_io_version = 0;
557 taskqueue_free(taskqueue_aiod_bio);
558 delete_unrhdr(aiod_unr);
559 uma_zdestroy(kaio_zone);
560 uma_zdestroy(aiop_zone);
561 uma_zdestroy(aiocb_zone);
562 uma_zdestroy(aiol_zone);
563 uma_zdestroy(aiolio_zone);
564 EVENTHANDLER_DEREGISTER(process_exit, exit_tag);
565 EVENTHANDLER_DEREGISTER(process_exec, exec_tag);
566 mtx_destroy(&aio_job_mtx);
567 mtx_destroy(&aio_sock_mtx);
568 sema_destroy(&aio_newproc_sem);
569 p31b_setcfg(CTL_P1003_1B_AIO_LISTIO_MAX, -1);
570 p31b_setcfg(CTL_P1003_1B_AIO_MAX, -1);
571 p31b_setcfg(CTL_P1003_1B_AIO_PRIO_DELTA_MAX, -1);
576 * Init the per-process aioinfo structure. The aioinfo limits are set
577 * per-process for user limit (resource) management.
580 aio_init_aioinfo(struct proc *p)
584 ki = uma_zalloc(kaio_zone, M_WAITOK);
585 mtx_init(&ki->kaio_mtx, "aiomtx", NULL, MTX_DEF);
587 ki->kaio_maxactive_count = max_aio_per_proc;
588 ki->kaio_active_count = 0;
589 ki->kaio_qallowed_count = max_aio_queue_per_proc;
591 ki->kaio_ballowed_count = max_buf_aio;
592 ki->kaio_buffer_count = 0;
593 TAILQ_INIT(&ki->kaio_all);
594 TAILQ_INIT(&ki->kaio_done);
595 TAILQ_INIT(&ki->kaio_jobqueue);
596 TAILQ_INIT(&ki->kaio_bufqueue);
597 TAILQ_INIT(&ki->kaio_liojoblist);
598 TAILQ_INIT(&ki->kaio_sockqueue);
599 TAILQ_INIT(&ki->kaio_syncqueue);
600 TASK_INIT(&ki->kaio_task, 0, aio_kick_helper, p);
602 if (p->p_aioinfo == NULL) {
607 mtx_destroy(&ki->kaio_mtx);
608 uma_zfree(kaio_zone, ki);
611 while (num_aio_procs < MIN(target_aio_procs, max_aio_procs))
616 aio_sendsig(struct proc *p, struct sigevent *sigev, ksiginfo_t *ksi)
621 error = sigev_findtd(p, sigev, &td);
625 ksiginfo_set_sigev(ksi, sigev);
626 ksi->ksi_code = SI_ASYNCIO;
627 ksi->ksi_flags |= KSI_EXT | KSI_INS;
628 tdsendsignal(p, td, ksi->ksi_signo, ksi);
635 * Free a job entry. Wait for completion if it is currently active, but don't
636 * delay forever. If we delay, we return a flag that says that we have to
637 * restart the queue scan.
640 aio_free_entry(struct aiocblist *aiocbe)
643 struct aioliojob *lj;
646 p = aiocbe->userproc;
651 AIO_LOCK_ASSERT(ki, MA_OWNED);
652 MPASS(aiocbe->jobstate == JOBST_JOBFINISHED);
654 atomic_subtract_int(&num_queue_count, 1);
657 MPASS(ki->kaio_count >= 0);
659 TAILQ_REMOVE(&ki->kaio_done, aiocbe, plist);
660 TAILQ_REMOVE(&ki->kaio_all, aiocbe, allist);
665 lj->lioj_finished_count--;
667 if (lj->lioj_count == 0) {
668 TAILQ_REMOVE(&ki->kaio_liojoblist, lj, lioj_list);
669 /* lio is going away, we need to destroy any knotes */
670 knlist_delete(&lj->klist, curthread, 1);
672 sigqueue_take(&lj->lioj_ksi);
674 uma_zfree(aiolio_zone, lj);
678 /* aiocbe is going away, we need to destroy any knotes */
679 knlist_delete(&aiocbe->klist, curthread, 1);
681 sigqueue_take(&aiocbe->ksi);
684 MPASS(aiocbe->bp == NULL);
685 aiocbe->jobstate = JOBST_NULL;
689 * The thread argument here is used to find the owning process
690 * and is also passed to fo_close() which may pass it to various
691 * places such as devsw close() routines. Because of that, we
692 * need a thread pointer from the process owning the job that is
693 * persistent and won't disappear out from under us or move to
696 * Currently, all the callers of this function call it to remove
697 * an aiocblist from the current process' job list either via a
698 * syscall or due to the current process calling exit() or
699 * execve(). Thus, we know that p == curproc. We also know that
700 * curthread can't exit since we are curthread.
702 * Therefore, we use curthread as the thread to pass to
703 * knlist_delete(). This does mean that it is possible for the
704 * thread pointer at close time to differ from the thread pointer
705 * at open time, but this is already true of file descriptors in
706 * a multithreaded process.
709 fdrop(aiocbe->fd_file, curthread);
710 crfree(aiocbe->cred);
711 uma_zfree(aiocb_zone, aiocbe);
718 aio_proc_rundown_exec(void *arg, struct proc *p, struct image_params *imgp __unused)
720 aio_proc_rundown(arg, p);
724 * Rundown the jobs for a given process.
727 aio_proc_rundown(void *arg, struct proc *p)
730 struct aioliojob *lj;
731 struct aiocblist *cbe, *cbn;
736 KASSERT(curthread->td_proc == p,
737 ("%s: called on non-curproc", __func__));
743 ki->kaio_flags |= KAIO_RUNDOWN;
748 * Try to cancel all pending requests. This code simulates
749 * aio_cancel on all pending I/O requests.
751 TAILQ_FOREACH_SAFE(cbe, &ki->kaio_jobqueue, plist, cbn) {
753 mtx_lock(&aio_job_mtx);
754 if (cbe->jobstate == JOBST_JOBQGLOBAL) {
755 TAILQ_REMOVE(&aio_jobs, cbe, list);
757 } else if (cbe->jobstate == JOBST_JOBQSOCK) {
759 MPASS(fp->f_type == DTYPE_SOCKET);
761 TAILQ_REMOVE(&so->so_aiojobq, cbe, list);
763 } else if (cbe->jobstate == JOBST_JOBQSYNC) {
764 TAILQ_REMOVE(&ki->kaio_syncqueue, cbe, list);
767 mtx_unlock(&aio_job_mtx);
770 cbe->jobstate = JOBST_JOBFINISHED;
771 cbe->uaiocb._aiocb_private.status = -1;
772 cbe->uaiocb._aiocb_private.error = ECANCELED;
773 TAILQ_REMOVE(&ki->kaio_jobqueue, cbe, plist);
774 aio_bio_done_notify(p, cbe, DONE_QUEUE);
778 /* Wait for all running I/O to be finished */
779 if (TAILQ_FIRST(&ki->kaio_bufqueue) ||
780 TAILQ_FIRST(&ki->kaio_jobqueue)) {
781 ki->kaio_flags |= KAIO_WAKEUP;
782 msleep(&p->p_aioinfo, AIO_MTX(ki), PRIBIO, "aioprn", hz);
786 /* Free all completed I/O requests. */
787 while ((cbe = TAILQ_FIRST(&ki->kaio_done)) != NULL)
790 while ((lj = TAILQ_FIRST(&ki->kaio_liojoblist)) != NULL) {
791 if (lj->lioj_count == 0) {
792 TAILQ_REMOVE(&ki->kaio_liojoblist, lj, lioj_list);
793 knlist_delete(&lj->klist, curthread, 1);
795 sigqueue_take(&lj->lioj_ksi);
797 uma_zfree(aiolio_zone, lj);
799 panic("LIO job not cleaned up: C:%d, FC:%d\n",
800 lj->lioj_count, lj->lioj_finished_count);
804 taskqueue_drain(taskqueue_aiod_bio, &ki->kaio_task);
805 mtx_destroy(&ki->kaio_mtx);
806 uma_zfree(kaio_zone, ki);
811 * Select a job to run (called by an AIO daemon).
813 static struct aiocblist *
814 aio_selectjob(struct aiothreadlist *aiop)
816 struct aiocblist *aiocbe;
820 mtx_assert(&aio_job_mtx, MA_OWNED);
821 TAILQ_FOREACH(aiocbe, &aio_jobs, list) {
822 userp = aiocbe->userproc;
823 ki = userp->p_aioinfo;
825 if (ki->kaio_active_count < ki->kaio_maxactive_count) {
826 TAILQ_REMOVE(&aio_jobs, aiocbe, list);
827 /* Account for currently active jobs. */
828 ki->kaio_active_count++;
829 aiocbe->jobstate = JOBST_JOBRUNNING;
837 * Move all data to a permanent storage device, this code
838 * simulates fsync syscall.
841 aio_fsync_vnode(struct thread *td, struct vnode *vp)
846 if ((error = vn_start_write(vp, &mp, V_WAIT | PCATCH)) != 0)
848 vn_lock(vp, LK_EXCLUSIVE | LK_RETRY);
849 if (vp->v_object != NULL) {
850 VM_OBJECT_WLOCK(vp->v_object);
851 vm_object_page_clean(vp->v_object, 0, 0, 0);
852 VM_OBJECT_WUNLOCK(vp->v_object);
854 error = VOP_FSYNC(vp, MNT_WAIT, td);
857 vn_finished_write(mp);
863 * The AIO processing activity for LIO_READ/LIO_WRITE. This is the code that
864 * does the I/O request for the non-physio version of the operations. The
865 * normal vn operations are used, and this code should work in all instances
866 * for every type of file, including pipes, sockets, fifos, and regular files.
868 * XXX I don't think it works well for socket, pipe, and fifo.
871 aio_process_rw(struct aiocblist *aiocbe)
873 struct ucred *td_savedcred;
882 int oublock_st, oublock_end;
883 int inblock_st, inblock_end;
885 KASSERT(aiocbe->uaiocb.aio_lio_opcode == LIO_READ ||
886 aiocbe->uaiocb.aio_lio_opcode == LIO_WRITE,
887 ("%s: opcode %d", __func__, aiocbe->uaiocb.aio_lio_opcode));
890 td_savedcred = td->td_ucred;
891 td->td_ucred = aiocbe->cred;
892 cb = &aiocbe->uaiocb;
893 fp = aiocbe->fd_file;
895 aiov.iov_base = (void *)(uintptr_t)cb->aio_buf;
896 aiov.iov_len = cb->aio_nbytes;
898 auio.uio_iov = &aiov;
900 auio.uio_offset = cb->aio_offset;
901 auio.uio_resid = cb->aio_nbytes;
902 cnt = cb->aio_nbytes;
903 auio.uio_segflg = UIO_USERSPACE;
906 inblock_st = td->td_ru.ru_inblock;
907 oublock_st = td->td_ru.ru_oublock;
909 * aio_aqueue() acquires a reference to the file that is
910 * released in aio_free_entry().
912 if (cb->aio_lio_opcode == LIO_READ) {
913 auio.uio_rw = UIO_READ;
914 if (auio.uio_resid == 0)
917 error = fo_read(fp, &auio, fp->f_cred, FOF_OFFSET, td);
919 if (fp->f_type == DTYPE_VNODE)
921 auio.uio_rw = UIO_WRITE;
922 error = fo_write(fp, &auio, fp->f_cred, FOF_OFFSET, td);
924 inblock_end = td->td_ru.ru_inblock;
925 oublock_end = td->td_ru.ru_oublock;
927 aiocbe->inputcharge = inblock_end - inblock_st;
928 aiocbe->outputcharge = oublock_end - oublock_st;
930 if ((error) && (auio.uio_resid != cnt)) {
931 if (error == ERESTART || error == EINTR || error == EWOULDBLOCK)
933 if ((error == EPIPE) && (cb->aio_lio_opcode == LIO_WRITE)) {
935 if (fp->f_type == DTYPE_SOCKET) {
937 if (so->so_options & SO_NOSIGPIPE)
941 PROC_LOCK(aiocbe->userproc);
942 kern_psignal(aiocbe->userproc, SIGPIPE);
943 PROC_UNLOCK(aiocbe->userproc);
948 cnt -= auio.uio_resid;
949 cb->_aiocb_private.error = error;
950 cb->_aiocb_private.status = cnt;
951 td->td_ucred = td_savedcred;
955 aio_process_sync(struct aiocblist *aiocbe)
957 struct thread *td = curthread;
958 struct ucred *td_savedcred = td->td_ucred;
959 struct aiocb *cb = &aiocbe->uaiocb;
960 struct file *fp = aiocbe->fd_file;
963 KASSERT(aiocbe->uaiocb.aio_lio_opcode == LIO_SYNC,
964 ("%s: opcode %d", __func__, aiocbe->uaiocb.aio_lio_opcode));
966 td->td_ucred = aiocbe->cred;
967 if (fp->f_vnode != NULL)
968 error = aio_fsync_vnode(td, fp->f_vnode);
969 cb->_aiocb_private.error = error;
970 cb->_aiocb_private.status = 0;
971 td->td_ucred = td_savedcred;
975 aio_process_mlock(struct aiocblist *aiocbe)
977 struct aiocb *cb = &aiocbe->uaiocb;
980 KASSERT(aiocbe->uaiocb.aio_lio_opcode == LIO_MLOCK,
981 ("%s: opcode %d", __func__, aiocbe->uaiocb.aio_lio_opcode));
983 error = vm_mlock(aiocbe->userproc, aiocbe->cred,
984 __DEVOLATILE(void *, cb->aio_buf), cb->aio_nbytes);
985 cb->_aiocb_private.error = error;
986 cb->_aiocb_private.status = 0;
990 aio_bio_done_notify(struct proc *userp, struct aiocblist *aiocbe, int type)
992 struct aioliojob *lj;
994 struct aiocblist *scb, *scbn;
997 ki = userp->p_aioinfo;
998 AIO_LOCK_ASSERT(ki, MA_OWNED);
1002 lj->lioj_finished_count++;
1003 if (lj->lioj_count == lj->lioj_finished_count)
1006 if (type == DONE_QUEUE) {
1007 aiocbe->jobflags |= AIOCBLIST_DONE;
1009 aiocbe->jobflags |= AIOCBLIST_BUFDONE;
1011 TAILQ_INSERT_TAIL(&ki->kaio_done, aiocbe, plist);
1012 aiocbe->jobstate = JOBST_JOBFINISHED;
1014 if (ki->kaio_flags & KAIO_RUNDOWN)
1015 goto notification_done;
1017 if (aiocbe->uaiocb.aio_sigevent.sigev_notify == SIGEV_SIGNAL ||
1018 aiocbe->uaiocb.aio_sigevent.sigev_notify == SIGEV_THREAD_ID)
1019 aio_sendsig(userp, &aiocbe->uaiocb.aio_sigevent, &aiocbe->ksi);
1021 KNOTE_LOCKED(&aiocbe->klist, 1);
1024 if (lj->lioj_signal.sigev_notify == SIGEV_KEVENT) {
1025 lj->lioj_flags |= LIOJ_KEVENT_POSTED;
1026 KNOTE_LOCKED(&lj->klist, 1);
1028 if ((lj->lioj_flags & (LIOJ_SIGNAL|LIOJ_SIGNAL_POSTED))
1030 && (lj->lioj_signal.sigev_notify == SIGEV_SIGNAL ||
1031 lj->lioj_signal.sigev_notify == SIGEV_THREAD_ID)) {
1032 aio_sendsig(userp, &lj->lioj_signal, &lj->lioj_ksi);
1033 lj->lioj_flags |= LIOJ_SIGNAL_POSTED;
1038 if (aiocbe->jobflags & AIOCBLIST_CHECKSYNC) {
1039 TAILQ_FOREACH_SAFE(scb, &ki->kaio_syncqueue, list, scbn) {
1040 if (aiocbe->fd_file == scb->fd_file &&
1041 aiocbe->seqno < scb->seqno) {
1042 if (--scb->pending == 0) {
1043 mtx_lock(&aio_job_mtx);
1044 scb->jobstate = JOBST_JOBQGLOBAL;
1045 TAILQ_REMOVE(&ki->kaio_syncqueue, scb, list);
1046 TAILQ_INSERT_TAIL(&aio_jobs, scb, list);
1047 aio_kick_nowait(userp);
1048 mtx_unlock(&aio_job_mtx);
1053 if (ki->kaio_flags & KAIO_WAKEUP) {
1054 ki->kaio_flags &= ~KAIO_WAKEUP;
1055 wakeup(&userp->p_aioinfo);
1060 * The AIO daemon, most of the actual work is done in aio_process_*,
1061 * but the setup (and address space mgmt) is done in this routine.
1064 aio_daemon(void *_id)
1066 struct aiocblist *aiocbe;
1067 struct aiothreadlist *aiop;
1068 struct kaioinfo *ki;
1069 struct proc *curcp, *mycp, *userp;
1070 struct vmspace *myvm, *tmpvm;
1071 struct thread *td = curthread;
1072 int id = (intptr_t)_id;
1075 * Local copies of curproc (cp) and vmspace (myvm)
1078 myvm = mycp->p_vmspace;
1080 KASSERT(mycp->p_textvp == NULL, ("kthread has a textvp"));
1083 * Allocate and ready the aio control info. There is one aiop structure
1086 aiop = uma_zalloc(aiop_zone, M_WAITOK);
1087 aiop->aiothread = td;
1088 aiop->aiothreadflags = 0;
1090 /* The daemon resides in its own pgrp. */
1091 sys_setsid(td, NULL);
1094 * Wakeup parent process. (Parent sleeps to keep from blasting away
1095 * and creating too many daemons.)
1097 sema_post(&aio_newproc_sem);
1099 mtx_lock(&aio_job_mtx);
1102 * curcp is the current daemon process context.
1103 * userp is the current user process context.
1108 * Take daemon off of free queue
1110 if (aiop->aiothreadflags & AIOP_FREE) {
1111 TAILQ_REMOVE(&aio_freeproc, aiop, list);
1112 aiop->aiothreadflags &= ~AIOP_FREE;
1118 while ((aiocbe = aio_selectjob(aiop)) != NULL) {
1119 mtx_unlock(&aio_job_mtx);
1120 userp = aiocbe->userproc;
1123 * Connect to process address space for user program.
1125 if (userp != curcp) {
1127 * Save the current address space that we are
1130 tmpvm = mycp->p_vmspace;
1133 * Point to the new user address space, and
1136 mycp->p_vmspace = userp->p_vmspace;
1137 atomic_add_int(&mycp->p_vmspace->vm_refcnt, 1);
1139 /* Activate the new mapping. */
1140 pmap_activate(FIRST_THREAD_IN_PROC(mycp));
1143 * If the old address space wasn't the daemons
1144 * own address space, then we need to remove the
1145 * daemon's reference from the other process
1146 * that it was acting on behalf of.
1148 if (tmpvm != myvm) {
1149 vmspace_free(tmpvm);
1154 ki = userp->p_aioinfo;
1156 /* Do the I/O function. */
1157 switch(aiocbe->uaiocb.aio_lio_opcode) {
1160 aio_process_rw(aiocbe);
1163 aio_process_sync(aiocbe);
1166 aio_process_mlock(aiocbe);
1170 mtx_lock(&aio_job_mtx);
1171 /* Decrement the active job count. */
1172 ki->kaio_active_count--;
1173 mtx_unlock(&aio_job_mtx);
1176 TAILQ_REMOVE(&ki->kaio_jobqueue, aiocbe, plist);
1177 aio_bio_done_notify(userp, aiocbe, DONE_QUEUE);
1180 mtx_lock(&aio_job_mtx);
1184 * Disconnect from user address space.
1186 if (curcp != mycp) {
1188 mtx_unlock(&aio_job_mtx);
1190 /* Get the user address space to disconnect from. */
1191 tmpvm = mycp->p_vmspace;
1193 /* Get original address space for daemon. */
1194 mycp->p_vmspace = myvm;
1196 /* Activate the daemon's address space. */
1197 pmap_activate(FIRST_THREAD_IN_PROC(mycp));
1199 if (tmpvm == myvm) {
1200 printf("AIOD: vmspace problem -- %d\n",
1204 /* Remove our vmspace reference. */
1205 vmspace_free(tmpvm);
1209 mtx_lock(&aio_job_mtx);
1211 * We have to restart to avoid race, we only sleep if
1212 * no job can be selected, that should be
1218 mtx_assert(&aio_job_mtx, MA_OWNED);
1220 TAILQ_INSERT_HEAD(&aio_freeproc, aiop, list);
1221 aiop->aiothreadflags |= AIOP_FREE;
1224 * If daemon is inactive for a long time, allow it to exit,
1225 * thereby freeing resources.
1227 if (msleep(aiop->aiothread, &aio_job_mtx, PRIBIO, "aiordy",
1229 if (TAILQ_EMPTY(&aio_jobs)) {
1230 if ((aiop->aiothreadflags & AIOP_FREE) &&
1231 (num_aio_procs > target_aio_procs)) {
1232 TAILQ_REMOVE(&aio_freeproc, aiop, list);
1234 mtx_unlock(&aio_job_mtx);
1235 uma_zfree(aiop_zone, aiop);
1236 free_unr(aiod_unr, id);
1238 if (mycp->p_vmspace->vm_refcnt <= 1) {
1239 printf("AIOD: bad vm refcnt for"
1240 " exiting daemon: %d\n",
1241 mycp->p_vmspace->vm_refcnt);
1249 mtx_unlock(&aio_job_mtx);
1250 panic("shouldn't be here\n");
1254 * Create a new AIO daemon. This is mostly a kernel-thread fork routine. The
1255 * AIO daemon modifies its environment itself.
1258 aio_newproc(int *start)
1264 id = alloc_unr(aiod_unr);
1265 error = kproc_create(aio_daemon, (void *)(intptr_t)id, &p,
1266 RFNOWAIT, 0, "aiod%d", id);
1269 * Wait until daemon is started.
1271 sema_wait(&aio_newproc_sem);
1272 mtx_lock(&aio_job_mtx);
1276 mtx_unlock(&aio_job_mtx);
1278 free_unr(aiod_unr, id);
1284 * Try the high-performance, low-overhead physio method for eligible
1285 * VCHR devices. This method doesn't use an aio helper thread, and
1286 * thus has very low overhead.
1288 * Assumes that the caller, aio_aqueue(), has incremented the file
1289 * structure's reference count, preventing its deallocation for the
1290 * duration of this call.
1293 aio_qphysio(struct proc *p, struct aiocblist *aiocbe)
1301 struct kaioinfo *ki;
1302 struct aioliojob *lj;
1305 cb = &aiocbe->uaiocb;
1306 fp = aiocbe->fd_file;
1308 if (fp == NULL || fp->f_type != DTYPE_VNODE)
1314 * If its not a disk, we don't want to return a positive error.
1315 * It causes the aio code to not fall through to try the thread
1316 * way when you're talking to a regular file.
1318 if (!vn_isdisk(vp, &error)) {
1319 if (error == ENOTBLK)
1325 if (vp->v_bufobj.bo_bsize == 0)
1328 if (cb->aio_nbytes % vp->v_bufobj.bo_bsize)
1331 if (cb->aio_nbytes >
1332 MAXPHYS - (((vm_offset_t) cb->aio_buf) & PAGE_MASK))
1336 if (ki->kaio_buffer_count >= ki->kaio_ballowed_count)
1340 csw = devvn_refthread(vp, &dev, &ref);
1343 if (cb->aio_nbytes > dev->si_iosize_max) {
1348 /* Create and build a buffer header for a transfer. */
1349 bp = (struct buf *)getpbuf(NULL);
1354 ki->kaio_buffer_count++;
1361 * Get a copy of the kva from the physical buffer.
1365 bp->b_bcount = cb->aio_nbytes;
1366 bp->b_bufsize = cb->aio_nbytes;
1367 bp->b_iodone = aio_physwakeup;
1368 bp->b_saveaddr = bp->b_data;
1369 bp->b_data = (void *)(uintptr_t)cb->aio_buf;
1370 bp->b_offset = cb->aio_offset;
1371 bp->b_iooffset = cb->aio_offset;
1372 bp->b_blkno = btodb(cb->aio_offset);
1373 bp->b_iocmd = cb->aio_lio_opcode == LIO_WRITE ? BIO_WRITE : BIO_READ;
1376 * Bring buffer into kernel space.
1378 if (vmapbuf(bp, (dev->si_flags & SI_UNMAPPED) == 0) < 0) {
1385 bp->b_caller1 = (void *)aiocbe;
1386 TAILQ_INSERT_TAIL(&ki->kaio_bufqueue, aiocbe, plist);
1387 TAILQ_INSERT_TAIL(&ki->kaio_all, aiocbe, allist);
1388 aiocbe->jobstate = JOBST_JOBQBUF;
1389 cb->_aiocb_private.status = cb->aio_nbytes;
1392 atomic_add_int(&num_queue_count, 1);
1393 atomic_add_int(&num_buf_aio, 1);
1397 TASK_INIT(&aiocbe->biotask, 0, biohelper, aiocbe);
1399 /* Perform transfer. */
1400 dev_strategy_csw(dev, csw, bp);
1401 dev_relthread(dev, ref);
1407 ki->kaio_buffer_count--;
1414 dev_relthread(dev, ref);
1419 * Wake up aio requests that may be serviceable now.
1422 aio_swake_cb(struct socket *so, struct sockbuf *sb)
1424 struct aiocblist *cb, *cbn;
1427 SOCKBUF_LOCK_ASSERT(sb);
1428 if (sb == &so->so_snd)
1433 sb->sb_flags &= ~SB_AIO;
1434 mtx_lock(&aio_job_mtx);
1435 TAILQ_FOREACH_SAFE(cb, &so->so_aiojobq, list, cbn) {
1436 if (opcode == cb->uaiocb.aio_lio_opcode) {
1437 if (cb->jobstate != JOBST_JOBQSOCK)
1438 panic("invalid queue value");
1440 * We don't have actual sockets backend yet,
1441 * so we simply move the requests to the generic
1444 TAILQ_REMOVE(&so->so_aiojobq, cb, list);
1445 TAILQ_INSERT_TAIL(&aio_jobs, cb, list);
1446 aio_kick_nowait(cb->userproc);
1449 mtx_unlock(&aio_job_mtx);
1453 convert_old_sigevent(struct osigevent *osig, struct sigevent *nsig)
1457 * Only SIGEV_NONE, SIGEV_SIGNAL, and SIGEV_KEVENT are
1458 * supported by AIO with the old sigevent structure.
1460 nsig->sigev_notify = osig->sigev_notify;
1461 switch (nsig->sigev_notify) {
1465 nsig->sigev_signo = osig->__sigev_u.__sigev_signo;
1468 nsig->sigev_notify_kqueue =
1469 osig->__sigev_u.__sigev_notify_kqueue;
1470 nsig->sigev_value.sival_ptr = osig->sigev_value.sival_ptr;
1479 aiocb_copyin_old_sigevent(struct aiocb *ujob, struct aiocb *kjob)
1481 struct oaiocb *ojob;
1484 bzero(kjob, sizeof(struct aiocb));
1485 error = copyin(ujob, kjob, sizeof(struct oaiocb));
1488 ojob = (struct oaiocb *)kjob;
1489 return (convert_old_sigevent(&ojob->aio_sigevent, &kjob->aio_sigevent));
1493 aiocb_copyin(struct aiocb *ujob, struct aiocb *kjob)
1496 return (copyin(ujob, kjob, sizeof(struct aiocb)));
1500 aiocb_fetch_status(struct aiocb *ujob)
1503 return (fuword(&ujob->_aiocb_private.status));
1507 aiocb_fetch_error(struct aiocb *ujob)
1510 return (fuword(&ujob->_aiocb_private.error));
1514 aiocb_store_status(struct aiocb *ujob, long status)
1517 return (suword(&ujob->_aiocb_private.status, status));
1521 aiocb_store_error(struct aiocb *ujob, long error)
1524 return (suword(&ujob->_aiocb_private.error, error));
1528 aiocb_store_kernelinfo(struct aiocb *ujob, long jobref)
1531 return (suword(&ujob->_aiocb_private.kernelinfo, jobref));
1535 aiocb_store_aiocb(struct aiocb **ujobp, struct aiocb *ujob)
1538 return (suword(ujobp, (long)ujob));
1541 static struct aiocb_ops aiocb_ops = {
1542 .copyin = aiocb_copyin,
1543 .fetch_status = aiocb_fetch_status,
1544 .fetch_error = aiocb_fetch_error,
1545 .store_status = aiocb_store_status,
1546 .store_error = aiocb_store_error,
1547 .store_kernelinfo = aiocb_store_kernelinfo,
1548 .store_aiocb = aiocb_store_aiocb,
1551 static struct aiocb_ops aiocb_ops_osigevent = {
1552 .copyin = aiocb_copyin_old_sigevent,
1553 .fetch_status = aiocb_fetch_status,
1554 .fetch_error = aiocb_fetch_error,
1555 .store_status = aiocb_store_status,
1556 .store_error = aiocb_store_error,
1557 .store_kernelinfo = aiocb_store_kernelinfo,
1558 .store_aiocb = aiocb_store_aiocb,
1562 * Queue a new AIO request. Choosing either the threaded or direct physio VCHR
1563 * technique is done in this code.
1566 aio_aqueue(struct thread *td, struct aiocb *job, struct aioliojob *lj,
1567 int type, struct aiocb_ops *ops)
1569 struct proc *p = td->td_proc;
1570 cap_rights_t rights;
1573 struct aiocblist *aiocbe, *cb;
1574 struct kaioinfo *ki;
1583 if (p->p_aioinfo == NULL)
1584 aio_init_aioinfo(p);
1588 ops->store_status(job, -1);
1589 ops->store_error(job, 0);
1590 ops->store_kernelinfo(job, -1);
1592 if (num_queue_count >= max_queue_count ||
1593 ki->kaio_count >= ki->kaio_qallowed_count) {
1594 ops->store_error(job, EAGAIN);
1598 aiocbe = uma_zalloc(aiocb_zone, M_WAITOK | M_ZERO);
1599 knlist_init_mtx(&aiocbe->klist, AIO_MTX(ki));
1601 error = ops->copyin(job, &aiocbe->uaiocb);
1603 ops->store_error(job, error);
1604 uma_zfree(aiocb_zone, aiocbe);
1608 /* XXX: aio_nbytes is later casted to signed types. */
1609 if (aiocbe->uaiocb.aio_nbytes > INT_MAX) {
1610 uma_zfree(aiocb_zone, aiocbe);
1614 if (aiocbe->uaiocb.aio_sigevent.sigev_notify != SIGEV_KEVENT &&
1615 aiocbe->uaiocb.aio_sigevent.sigev_notify != SIGEV_SIGNAL &&
1616 aiocbe->uaiocb.aio_sigevent.sigev_notify != SIGEV_THREAD_ID &&
1617 aiocbe->uaiocb.aio_sigevent.sigev_notify != SIGEV_NONE) {
1618 ops->store_error(job, EINVAL);
1619 uma_zfree(aiocb_zone, aiocbe);
1623 if ((aiocbe->uaiocb.aio_sigevent.sigev_notify == SIGEV_SIGNAL ||
1624 aiocbe->uaiocb.aio_sigevent.sigev_notify == SIGEV_THREAD_ID) &&
1625 !_SIG_VALID(aiocbe->uaiocb.aio_sigevent.sigev_signo)) {
1626 uma_zfree(aiocb_zone, aiocbe);
1630 ksiginfo_init(&aiocbe->ksi);
1632 /* Save userspace address of the job info. */
1633 aiocbe->uuaiocb = job;
1635 /* Get the opcode. */
1636 if (type != LIO_NOP)
1637 aiocbe->uaiocb.aio_lio_opcode = type;
1638 opcode = aiocbe->uaiocb.aio_lio_opcode;
1641 * Validate the opcode and fetch the file object for the specified
1644 * XXXRW: Moved the opcode validation up here so that we don't
1645 * retrieve a file descriptor without knowing what the capabiltity
1648 fd = aiocbe->uaiocb.aio_fildes;
1651 error = fget_write(td, fd,
1652 cap_rights_init(&rights, CAP_PWRITE), &fp);
1655 error = fget_read(td, fd,
1656 cap_rights_init(&rights, CAP_PREAD), &fp);
1659 error = fget(td, fd, cap_rights_init(&rights, CAP_FSYNC), &fp);
1665 error = fget(td, fd, cap_rights_init(&rights), &fp);
1671 uma_zfree(aiocb_zone, aiocbe);
1672 ops->store_error(job, error);
1676 if (opcode == LIO_SYNC && fp->f_vnode == NULL) {
1681 if (opcode != LIO_SYNC && aiocbe->uaiocb.aio_offset == -1LL) {
1686 aiocbe->fd_file = fp;
1688 mtx_lock(&aio_job_mtx);
1690 aiocbe->seqno = jobseqno++;
1691 mtx_unlock(&aio_job_mtx);
1692 error = ops->store_kernelinfo(job, jid);
1697 aiocbe->uaiocb._aiocb_private.kernelinfo = (void *)(intptr_t)jid;
1699 if (opcode == LIO_NOP) {
1701 uma_zfree(aiocb_zone, aiocbe);
1705 if (aiocbe->uaiocb.aio_sigevent.sigev_notify != SIGEV_KEVENT)
1707 evflags = aiocbe->uaiocb.aio_sigevent.sigev_notify_kevent_flags;
1708 if ((evflags & ~(EV_CLEAR | EV_DISPATCH | EV_ONESHOT)) != 0) {
1712 kqfd = aiocbe->uaiocb.aio_sigevent.sigev_notify_kqueue;
1713 kev.ident = (uintptr_t)aiocbe->uuaiocb;
1714 kev.filter = EVFILT_AIO;
1715 kev.flags = EV_ADD | EV_ENABLE | EV_FLAG1 | evflags;
1716 kev.data = (intptr_t)aiocbe;
1717 kev.udata = aiocbe->uaiocb.aio_sigevent.sigev_value.sival_ptr;
1718 error = kqfd_register(kqfd, &kev, td, 1);
1723 uma_zfree(aiocb_zone, aiocbe);
1724 ops->store_error(job, error);
1729 ops->store_error(job, EINPROGRESS);
1730 aiocbe->uaiocb._aiocb_private.error = EINPROGRESS;
1731 aiocbe->userproc = p;
1732 aiocbe->cred = crhold(td->td_ucred);
1733 aiocbe->jobflags = 0;
1736 if (opcode == LIO_SYNC)
1739 if (fp && fp->f_type == DTYPE_SOCKET) {
1741 * Alternate queueing for socket ops: Reach down into the
1742 * descriptor to get the socket data. Then check to see if the
1743 * socket is ready to be read or written (based on the requested
1746 * If it is not ready for io, then queue the aiocbe on the
1747 * socket, and set the flags so we get a call when sbnotify()
1750 * Note if opcode is neither LIO_WRITE nor LIO_READ we lock
1751 * and unlock the snd sockbuf for no reason.
1754 sb = (opcode == LIO_READ) ? &so->so_rcv : &so->so_snd;
1756 if (((opcode == LIO_READ) && (!soreadable(so))) || ((opcode ==
1757 LIO_WRITE) && (!sowriteable(so)))) {
1758 sb->sb_flags |= SB_AIO;
1760 mtx_lock(&aio_job_mtx);
1761 TAILQ_INSERT_TAIL(&so->so_aiojobq, aiocbe, list);
1762 mtx_unlock(&aio_job_mtx);
1765 TAILQ_INSERT_TAIL(&ki->kaio_all, aiocbe, allist);
1766 TAILQ_INSERT_TAIL(&ki->kaio_jobqueue, aiocbe, plist);
1767 aiocbe->jobstate = JOBST_JOBQSOCK;
1773 atomic_add_int(&num_queue_count, 1);
1780 if ((error = aio_qphysio(p, aiocbe)) == 0)
1784 aiocbe->uaiocb._aiocb_private.error = error;
1785 ops->store_error(job, error);
1790 /* No buffer for daemon I/O. */
1792 atomic_add_int(&num_queue_count, 1);
1798 TAILQ_INSERT_TAIL(&ki->kaio_jobqueue, aiocbe, plist);
1799 TAILQ_INSERT_TAIL(&ki->kaio_all, aiocbe, allist);
1800 if (opcode == LIO_SYNC) {
1801 TAILQ_FOREACH(cb, &ki->kaio_jobqueue, plist) {
1802 if (cb->fd_file == aiocbe->fd_file &&
1803 cb->uaiocb.aio_lio_opcode != LIO_SYNC &&
1804 cb->seqno < aiocbe->seqno) {
1805 cb->jobflags |= AIOCBLIST_CHECKSYNC;
1809 TAILQ_FOREACH(cb, &ki->kaio_bufqueue, plist) {
1810 if (cb->fd_file == aiocbe->fd_file &&
1811 cb->uaiocb.aio_lio_opcode != LIO_SYNC &&
1812 cb->seqno < aiocbe->seqno) {
1813 cb->jobflags |= AIOCBLIST_CHECKSYNC;
1817 if (aiocbe->pending != 0) {
1818 TAILQ_INSERT_TAIL(&ki->kaio_syncqueue, aiocbe, list);
1819 aiocbe->jobstate = JOBST_JOBQSYNC;
1824 mtx_lock(&aio_job_mtx);
1825 TAILQ_INSERT_TAIL(&aio_jobs, aiocbe, list);
1826 aiocbe->jobstate = JOBST_JOBQGLOBAL;
1828 mtx_unlock(&aio_job_mtx);
1836 aio_kick_nowait(struct proc *userp)
1838 struct kaioinfo *ki = userp->p_aioinfo;
1839 struct aiothreadlist *aiop;
1841 mtx_assert(&aio_job_mtx, MA_OWNED);
1842 if ((aiop = TAILQ_FIRST(&aio_freeproc)) != NULL) {
1843 TAILQ_REMOVE(&aio_freeproc, aiop, list);
1844 aiop->aiothreadflags &= ~AIOP_FREE;
1845 wakeup(aiop->aiothread);
1846 } else if (((num_aio_resv_start + num_aio_procs) < max_aio_procs) &&
1847 ((ki->kaio_active_count + num_aio_resv_start) <
1848 ki->kaio_maxactive_count)) {
1849 taskqueue_enqueue(taskqueue_aiod_bio, &ki->kaio_task);
1854 aio_kick(struct proc *userp)
1856 struct kaioinfo *ki = userp->p_aioinfo;
1857 struct aiothreadlist *aiop;
1860 mtx_assert(&aio_job_mtx, MA_OWNED);
1862 if ((aiop = TAILQ_FIRST(&aio_freeproc)) != NULL) {
1863 TAILQ_REMOVE(&aio_freeproc, aiop, list);
1864 aiop->aiothreadflags &= ~AIOP_FREE;
1865 wakeup(aiop->aiothread);
1866 } else if (((num_aio_resv_start + num_aio_procs) < max_aio_procs) &&
1867 ((ki->kaio_active_count + num_aio_resv_start) <
1868 ki->kaio_maxactive_count)) {
1869 num_aio_resv_start++;
1870 mtx_unlock(&aio_job_mtx);
1871 error = aio_newproc(&num_aio_resv_start);
1872 mtx_lock(&aio_job_mtx);
1874 num_aio_resv_start--;
1884 aio_kick_helper(void *context, int pending)
1886 struct proc *userp = context;
1888 mtx_lock(&aio_job_mtx);
1889 while (--pending >= 0) {
1890 if (aio_kick(userp))
1893 mtx_unlock(&aio_job_mtx);
1897 * Support the aio_return system call, as a side-effect, kernel resources are
1901 kern_aio_return(struct thread *td, struct aiocb *uaiocb, struct aiocb_ops *ops)
1903 struct proc *p = td->td_proc;
1904 struct aiocblist *cb;
1905 struct kaioinfo *ki;
1912 TAILQ_FOREACH(cb, &ki->kaio_done, plist) {
1913 if (cb->uuaiocb == uaiocb)
1917 MPASS(cb->jobstate == JOBST_JOBFINISHED);
1918 status = cb->uaiocb._aiocb_private.status;
1919 error = cb->uaiocb._aiocb_private.error;
1920 td->td_retval[0] = status;
1921 if (cb->uaiocb.aio_lio_opcode == LIO_WRITE) {
1922 td->td_ru.ru_oublock += cb->outputcharge;
1923 cb->outputcharge = 0;
1924 } else if (cb->uaiocb.aio_lio_opcode == LIO_READ) {
1925 td->td_ru.ru_inblock += cb->inputcharge;
1926 cb->inputcharge = 0;
1930 ops->store_error(uaiocb, error);
1931 ops->store_status(uaiocb, status);
1940 sys_aio_return(struct thread *td, struct aio_return_args *uap)
1943 return (kern_aio_return(td, uap->aiocbp, &aiocb_ops));
1947 * Allow a process to wakeup when any of the I/O requests are completed.
1950 kern_aio_suspend(struct thread *td, int njoblist, struct aiocb **ujoblist,
1951 struct timespec *ts)
1953 struct proc *p = td->td_proc;
1955 struct kaioinfo *ki;
1956 struct aiocblist *cb, *cbfirst;
1961 if (ts->tv_nsec < 0 || ts->tv_nsec >= 1000000000)
1964 TIMESPEC_TO_TIMEVAL(&atv, ts);
1965 if (itimerfix(&atv))
1967 timo = tvtohz(&atv);
1981 TAILQ_FOREACH(cb, &ki->kaio_all, allist) {
1982 for (i = 0; i < njoblist; i++) {
1983 if (cb->uuaiocb == ujoblist[i]) {
1984 if (cbfirst == NULL)
1986 if (cb->jobstate == JOBST_JOBFINISHED)
1991 /* All tasks were finished. */
1992 if (cbfirst == NULL)
1995 ki->kaio_flags |= KAIO_WAKEUP;
1996 error = msleep(&p->p_aioinfo, AIO_MTX(ki), PRIBIO | PCATCH,
1998 if (error == ERESTART)
2009 sys_aio_suspend(struct thread *td, struct aio_suspend_args *uap)
2011 struct timespec ts, *tsp;
2012 struct aiocb **ujoblist;
2015 if (uap->nent < 0 || uap->nent > AIO_LISTIO_MAX)
2019 /* Get timespec struct. */
2020 if ((error = copyin(uap->timeout, &ts, sizeof(ts))) != 0)
2026 ujoblist = uma_zalloc(aiol_zone, M_WAITOK);
2027 error = copyin(uap->aiocbp, ujoblist, uap->nent * sizeof(ujoblist[0]));
2029 error = kern_aio_suspend(td, uap->nent, ujoblist, tsp);
2030 uma_zfree(aiol_zone, ujoblist);
2035 * aio_cancel cancels any non-physio aio operations not currently in
2039 sys_aio_cancel(struct thread *td, struct aio_cancel_args *uap)
2041 struct proc *p = td->td_proc;
2042 struct kaioinfo *ki;
2043 struct aiocblist *cbe, *cbn;
2049 int notcancelled = 0;
2052 /* Lookup file object. */
2053 error = fget(td, uap->fd, NULL, &fp);
2061 if (fp->f_type == DTYPE_VNODE) {
2063 if (vn_isdisk(vp, &error)) {
2065 td->td_retval[0] = AIO_NOTCANCELED;
2071 TAILQ_FOREACH_SAFE(cbe, &ki->kaio_jobqueue, plist, cbn) {
2072 if ((uap->fd == cbe->uaiocb.aio_fildes) &&
2073 ((uap->aiocbp == NULL) ||
2074 (uap->aiocbp == cbe->uuaiocb))) {
2077 mtx_lock(&aio_job_mtx);
2078 if (cbe->jobstate == JOBST_JOBQGLOBAL) {
2079 TAILQ_REMOVE(&aio_jobs, cbe, list);
2081 } else if (cbe->jobstate == JOBST_JOBQSOCK) {
2082 MPASS(fp->f_type == DTYPE_SOCKET);
2084 TAILQ_REMOVE(&so->so_aiojobq, cbe, list);
2086 } else if (cbe->jobstate == JOBST_JOBQSYNC) {
2087 TAILQ_REMOVE(&ki->kaio_syncqueue, cbe, list);
2090 mtx_unlock(&aio_job_mtx);
2093 TAILQ_REMOVE(&ki->kaio_jobqueue, cbe, plist);
2094 cbe->uaiocb._aiocb_private.status = -1;
2095 cbe->uaiocb._aiocb_private.error = ECANCELED;
2096 aio_bio_done_notify(p, cbe, DONE_QUEUE);
2101 if (uap->aiocbp != NULL)
2110 if (uap->aiocbp != NULL) {
2112 td->td_retval[0] = AIO_CANCELED;
2118 td->td_retval[0] = AIO_NOTCANCELED;
2123 td->td_retval[0] = AIO_CANCELED;
2127 td->td_retval[0] = AIO_ALLDONE;
2133 * aio_error is implemented in the kernel level for compatibility purposes
2134 * only. For a user mode async implementation, it would be best to do it in
2135 * a userland subroutine.
2138 kern_aio_error(struct thread *td, struct aiocb *aiocbp, struct aiocb_ops *ops)
2140 struct proc *p = td->td_proc;
2141 struct aiocblist *cb;
2142 struct kaioinfo *ki;
2147 td->td_retval[0] = EINVAL;
2152 TAILQ_FOREACH(cb, &ki->kaio_all, allist) {
2153 if (cb->uuaiocb == aiocbp) {
2154 if (cb->jobstate == JOBST_JOBFINISHED)
2156 cb->uaiocb._aiocb_private.error;
2158 td->td_retval[0] = EINPROGRESS;
2166 * Hack for failure of aio_aqueue.
2168 status = ops->fetch_status(aiocbp);
2170 td->td_retval[0] = ops->fetch_error(aiocbp);
2174 td->td_retval[0] = EINVAL;
2179 sys_aio_error(struct thread *td, struct aio_error_args *uap)
2182 return (kern_aio_error(td, uap->aiocbp, &aiocb_ops));
2185 /* syscall - asynchronous read from a file (REALTIME) */
2187 sys_oaio_read(struct thread *td, struct oaio_read_args *uap)
2190 return (aio_aqueue(td, (struct aiocb *)uap->aiocbp, NULL, LIO_READ,
2191 &aiocb_ops_osigevent));
2195 sys_aio_read(struct thread *td, struct aio_read_args *uap)
2198 return (aio_aqueue(td, uap->aiocbp, NULL, LIO_READ, &aiocb_ops));
2201 /* syscall - asynchronous write to a file (REALTIME) */
2203 sys_oaio_write(struct thread *td, struct oaio_write_args *uap)
2206 return (aio_aqueue(td, (struct aiocb *)uap->aiocbp, NULL, LIO_WRITE,
2207 &aiocb_ops_osigevent));
2211 sys_aio_write(struct thread *td, struct aio_write_args *uap)
2214 return (aio_aqueue(td, uap->aiocbp, NULL, LIO_WRITE, &aiocb_ops));
2218 sys_aio_mlock(struct thread *td, struct aio_mlock_args *uap)
2221 return (aio_aqueue(td, uap->aiocbp, NULL, LIO_MLOCK, &aiocb_ops));
2225 kern_lio_listio(struct thread *td, int mode, struct aiocb * const *uacb_list,
2226 struct aiocb **acb_list, int nent, struct sigevent *sig,
2227 struct aiocb_ops *ops)
2229 struct proc *p = td->td_proc;
2231 struct kaioinfo *ki;
2232 struct aioliojob *lj;
2238 if ((mode != LIO_NOWAIT) && (mode != LIO_WAIT))
2241 if (nent < 0 || nent > AIO_LISTIO_MAX)
2244 if (p->p_aioinfo == NULL)
2245 aio_init_aioinfo(p);
2249 lj = uma_zalloc(aiolio_zone, M_WAITOK);
2252 lj->lioj_finished_count = 0;
2253 knlist_init_mtx(&lj->klist, AIO_MTX(ki));
2254 ksiginfo_init(&lj->lioj_ksi);
2259 if (sig && (mode == LIO_NOWAIT)) {
2260 bcopy(sig, &lj->lioj_signal, sizeof(lj->lioj_signal));
2261 if (lj->lioj_signal.sigev_notify == SIGEV_KEVENT) {
2262 /* Assume only new style KEVENT */
2263 kev.filter = EVFILT_LIO;
2264 kev.flags = EV_ADD | EV_ENABLE | EV_FLAG1;
2265 kev.ident = (uintptr_t)uacb_list; /* something unique */
2266 kev.data = (intptr_t)lj;
2267 /* pass user defined sigval data */
2268 kev.udata = lj->lioj_signal.sigev_value.sival_ptr;
2269 error = kqfd_register(
2270 lj->lioj_signal.sigev_notify_kqueue, &kev, td, 1);
2272 uma_zfree(aiolio_zone, lj);
2275 } else if (lj->lioj_signal.sigev_notify == SIGEV_NONE) {
2277 } else if (lj->lioj_signal.sigev_notify == SIGEV_SIGNAL ||
2278 lj->lioj_signal.sigev_notify == SIGEV_THREAD_ID) {
2279 if (!_SIG_VALID(lj->lioj_signal.sigev_signo)) {
2280 uma_zfree(aiolio_zone, lj);
2283 lj->lioj_flags |= LIOJ_SIGNAL;
2285 uma_zfree(aiolio_zone, lj);
2291 TAILQ_INSERT_TAIL(&ki->kaio_liojoblist, lj, lioj_list);
2293 * Add extra aiocb count to avoid the lio to be freed
2294 * by other threads doing aio_waitcomplete or aio_return,
2295 * and prevent event from being sent until we have queued
2302 * Get pointers to the list of I/O requests.
2305 for (i = 0; i < nent; i++) {
2308 error = aio_aqueue(td, iocb, lj, LIO_NOP, ops);
2316 if (mode == LIO_WAIT) {
2317 while (lj->lioj_count - 1 != lj->lioj_finished_count) {
2318 ki->kaio_flags |= KAIO_WAKEUP;
2319 error = msleep(&p->p_aioinfo, AIO_MTX(ki),
2320 PRIBIO | PCATCH, "aiospn", 0);
2321 if (error == ERESTART)
2327 if (lj->lioj_count - 1 == lj->lioj_finished_count) {
2328 if (lj->lioj_signal.sigev_notify == SIGEV_KEVENT) {
2329 lj->lioj_flags |= LIOJ_KEVENT_POSTED;
2330 KNOTE_LOCKED(&lj->klist, 1);
2332 if ((lj->lioj_flags & (LIOJ_SIGNAL|LIOJ_SIGNAL_POSTED))
2334 && (lj->lioj_signal.sigev_notify == SIGEV_SIGNAL ||
2335 lj->lioj_signal.sigev_notify == SIGEV_THREAD_ID)) {
2336 aio_sendsig(p, &lj->lioj_signal,
2338 lj->lioj_flags |= LIOJ_SIGNAL_POSTED;
2343 if (lj->lioj_count == 0) {
2344 TAILQ_REMOVE(&ki->kaio_liojoblist, lj, lioj_list);
2345 knlist_delete(&lj->klist, curthread, 1);
2347 sigqueue_take(&lj->lioj_ksi);
2350 uma_zfree(aiolio_zone, lj);
2359 /* syscall - list directed I/O (REALTIME) */
2361 sys_olio_listio(struct thread *td, struct olio_listio_args *uap)
2363 struct aiocb **acb_list;
2364 struct sigevent *sigp, sig;
2365 struct osigevent osig;
2368 if ((uap->mode != LIO_NOWAIT) && (uap->mode != LIO_WAIT))
2372 if (nent < 0 || nent > AIO_LISTIO_MAX)
2375 if (uap->sig && (uap->mode == LIO_NOWAIT)) {
2376 error = copyin(uap->sig, &osig, sizeof(osig));
2379 error = convert_old_sigevent(&osig, &sig);
2386 acb_list = malloc(sizeof(struct aiocb *) * nent, M_LIO, M_WAITOK);
2387 error = copyin(uap->acb_list, acb_list, nent * sizeof(acb_list[0]));
2389 error = kern_lio_listio(td, uap->mode,
2390 (struct aiocb * const *)uap->acb_list, acb_list, nent, sigp,
2391 &aiocb_ops_osigevent);
2392 free(acb_list, M_LIO);
2396 /* syscall - list directed I/O (REALTIME) */
2398 sys_lio_listio(struct thread *td, struct lio_listio_args *uap)
2400 struct aiocb **acb_list;
2401 struct sigevent *sigp, sig;
2404 if ((uap->mode != LIO_NOWAIT) && (uap->mode != LIO_WAIT))
2408 if (nent < 0 || nent > AIO_LISTIO_MAX)
2411 if (uap->sig && (uap->mode == LIO_NOWAIT)) {
2412 error = copyin(uap->sig, &sig, sizeof(sig));
2419 acb_list = malloc(sizeof(struct aiocb *) * nent, M_LIO, M_WAITOK);
2420 error = copyin(uap->acb_list, acb_list, nent * sizeof(acb_list[0]));
2422 error = kern_lio_listio(td, uap->mode, uap->acb_list, acb_list,
2423 nent, sigp, &aiocb_ops);
2424 free(acb_list, M_LIO);
2429 * Called from interrupt thread for physio, we should return as fast
2430 * as possible, so we schedule a biohelper task.
2433 aio_physwakeup(struct buf *bp)
2435 struct aiocblist *aiocbe;
2437 aiocbe = (struct aiocblist *)bp->b_caller1;
2438 taskqueue_enqueue(taskqueue_aiod_bio, &aiocbe->biotask);
2442 * Task routine to perform heavy tasks, process wakeup, and signals.
2445 biohelper(void *context, int pending)
2447 struct aiocblist *aiocbe = context;
2450 struct kaioinfo *ki;
2454 userp = aiocbe->userproc;
2455 ki = userp->p_aioinfo;
2457 aiocbe->uaiocb._aiocb_private.status -= bp->b_resid;
2458 aiocbe->uaiocb._aiocb_private.error = 0;
2459 if (bp->b_ioflags & BIO_ERROR)
2460 aiocbe->uaiocb._aiocb_private.error = bp->b_error;
2461 nblks = btodb(aiocbe->uaiocb.aio_nbytes);
2462 if (aiocbe->uaiocb.aio_lio_opcode == LIO_WRITE)
2463 aiocbe->outputcharge += nblks;
2465 aiocbe->inputcharge += nblks;
2467 TAILQ_REMOVE(&userp->p_aioinfo->kaio_bufqueue, aiocbe, plist);
2468 ki->kaio_buffer_count--;
2469 aio_bio_done_notify(userp, aiocbe, DONE_BUF);
2472 /* Release mapping into kernel space. */
2475 atomic_subtract_int(&num_buf_aio, 1);
2478 /* syscall - wait for the next completion of an aio request */
2480 kern_aio_waitcomplete(struct thread *td, struct aiocb **aiocbp,
2481 struct timespec *ts, struct aiocb_ops *ops)
2483 struct proc *p = td->td_proc;
2485 struct kaioinfo *ki;
2486 struct aiocblist *cb;
2487 struct aiocb *uuaiocb;
2488 int error, status, timo;
2490 ops->store_aiocb(aiocbp, NULL);
2494 if ((ts->tv_nsec < 0) || (ts->tv_nsec >= 1000000000))
2497 TIMESPEC_TO_TIMEVAL(&atv, ts);
2498 if (itimerfix(&atv))
2500 timo = tvtohz(&atv);
2503 if (p->p_aioinfo == NULL)
2504 aio_init_aioinfo(p);
2510 while ((cb = TAILQ_FIRST(&ki->kaio_done)) == NULL) {
2511 ki->kaio_flags |= KAIO_WAKEUP;
2512 error = msleep(&p->p_aioinfo, AIO_MTX(ki), PRIBIO | PCATCH,
2514 if (timo && error == ERESTART)
2521 MPASS(cb->jobstate == JOBST_JOBFINISHED);
2522 uuaiocb = cb->uuaiocb;
2523 status = cb->uaiocb._aiocb_private.status;
2524 error = cb->uaiocb._aiocb_private.error;
2525 td->td_retval[0] = status;
2526 if (cb->uaiocb.aio_lio_opcode == LIO_WRITE) {
2527 td->td_ru.ru_oublock += cb->outputcharge;
2528 cb->outputcharge = 0;
2529 } else if (cb->uaiocb.aio_lio_opcode == LIO_READ) {
2530 td->td_ru.ru_inblock += cb->inputcharge;
2531 cb->inputcharge = 0;
2535 ops->store_aiocb(aiocbp, uuaiocb);
2536 ops->store_error(uuaiocb, error);
2537 ops->store_status(uuaiocb, status);
2545 sys_aio_waitcomplete(struct thread *td, struct aio_waitcomplete_args *uap)
2547 struct timespec ts, *tsp;
2551 /* Get timespec struct. */
2552 error = copyin(uap->timeout, &ts, sizeof(ts));
2559 return (kern_aio_waitcomplete(td, uap->aiocbp, tsp, &aiocb_ops));
2563 kern_aio_fsync(struct thread *td, int op, struct aiocb *aiocbp,
2564 struct aiocb_ops *ops)
2566 struct proc *p = td->td_proc;
2567 struct kaioinfo *ki;
2569 if (op != O_SYNC) /* XXX lack of O_DSYNC */
2573 aio_init_aioinfo(p);
2574 return (aio_aqueue(td, aiocbp, NULL, LIO_SYNC, ops));
2578 sys_aio_fsync(struct thread *td, struct aio_fsync_args *uap)
2581 return (kern_aio_fsync(td, uap->op, uap->aiocbp, &aiocb_ops));
2584 /* kqueue attach function */
2586 filt_aioattach(struct knote *kn)
2588 struct aiocblist *aiocbe = (struct aiocblist *)kn->kn_sdata;
2591 * The aiocbe pointer must be validated before using it, so
2592 * registration is restricted to the kernel; the user cannot
2595 if ((kn->kn_flags & EV_FLAG1) == 0)
2597 kn->kn_ptr.p_aio = aiocbe;
2598 kn->kn_flags &= ~EV_FLAG1;
2600 knlist_add(&aiocbe->klist, kn, 0);
2605 /* kqueue detach function */
2607 filt_aiodetach(struct knote *kn)
2611 knl = &kn->kn_ptr.p_aio->klist;
2612 knl->kl_lock(knl->kl_lockarg);
2613 if (!knlist_empty(knl))
2614 knlist_remove(knl, kn, 1);
2615 knl->kl_unlock(knl->kl_lockarg);
2618 /* kqueue filter function */
2621 filt_aio(struct knote *kn, long hint)
2623 struct aiocblist *aiocbe = kn->kn_ptr.p_aio;
2625 kn->kn_data = aiocbe->uaiocb._aiocb_private.error;
2626 if (aiocbe->jobstate != JOBST_JOBFINISHED)
2628 kn->kn_flags |= EV_EOF;
2632 /* kqueue attach function */
2634 filt_lioattach(struct knote *kn)
2636 struct aioliojob * lj = (struct aioliojob *)kn->kn_sdata;
2639 * The aioliojob pointer must be validated before using it, so
2640 * registration is restricted to the kernel; the user cannot
2643 if ((kn->kn_flags & EV_FLAG1) == 0)
2645 kn->kn_ptr.p_lio = lj;
2646 kn->kn_flags &= ~EV_FLAG1;
2648 knlist_add(&lj->klist, kn, 0);
2653 /* kqueue detach function */
2655 filt_liodetach(struct knote *kn)
2659 knl = &kn->kn_ptr.p_lio->klist;
2660 knl->kl_lock(knl->kl_lockarg);
2661 if (!knlist_empty(knl))
2662 knlist_remove(knl, kn, 1);
2663 knl->kl_unlock(knl->kl_lockarg);
2666 /* kqueue filter function */
2669 filt_lio(struct knote *kn, long hint)
2671 struct aioliojob * lj = kn->kn_ptr.p_lio;
2673 return (lj->lioj_flags & LIOJ_KEVENT_POSTED);
2676 #ifdef COMPAT_FREEBSD32
2678 struct __aiocb_private32 {
2681 uint32_t kernelinfo;
2684 typedef struct oaiocb32 {
2685 int aio_fildes; /* File descriptor */
2686 uint64_t aio_offset __packed; /* File offset for I/O */
2687 uint32_t aio_buf; /* I/O buffer in process space */
2688 uint32_t aio_nbytes; /* Number of bytes for I/O */
2689 struct osigevent32 aio_sigevent; /* Signal to deliver */
2690 int aio_lio_opcode; /* LIO opcode */
2691 int aio_reqprio; /* Request priority -- ignored */
2692 struct __aiocb_private32 _aiocb_private;
2695 typedef struct aiocb32 {
2696 int32_t aio_fildes; /* File descriptor */
2697 uint64_t aio_offset __packed; /* File offset for I/O */
2698 uint32_t aio_buf; /* I/O buffer in process space */
2699 uint32_t aio_nbytes; /* Number of bytes for I/O */
2701 uint32_t __spare2__;
2702 int aio_lio_opcode; /* LIO opcode */
2703 int aio_reqprio; /* Request priority -- ignored */
2704 struct __aiocb_private32 _aiocb_private;
2705 struct sigevent32 aio_sigevent; /* Signal to deliver */
2709 convert_old_sigevent32(struct osigevent32 *osig, struct sigevent *nsig)
2713 * Only SIGEV_NONE, SIGEV_SIGNAL, and SIGEV_KEVENT are
2714 * supported by AIO with the old sigevent structure.
2716 CP(*osig, *nsig, sigev_notify);
2717 switch (nsig->sigev_notify) {
2721 nsig->sigev_signo = osig->__sigev_u.__sigev_signo;
2724 nsig->sigev_notify_kqueue =
2725 osig->__sigev_u.__sigev_notify_kqueue;
2726 PTRIN_CP(*osig, *nsig, sigev_value.sival_ptr);
2735 aiocb32_copyin_old_sigevent(struct aiocb *ujob, struct aiocb *kjob)
2737 struct oaiocb32 job32;
2740 bzero(kjob, sizeof(struct aiocb));
2741 error = copyin(ujob, &job32, sizeof(job32));
2745 CP(job32, *kjob, aio_fildes);
2746 CP(job32, *kjob, aio_offset);
2747 PTRIN_CP(job32, *kjob, aio_buf);
2748 CP(job32, *kjob, aio_nbytes);
2749 CP(job32, *kjob, aio_lio_opcode);
2750 CP(job32, *kjob, aio_reqprio);
2751 CP(job32, *kjob, _aiocb_private.status);
2752 CP(job32, *kjob, _aiocb_private.error);
2753 PTRIN_CP(job32, *kjob, _aiocb_private.kernelinfo);
2754 return (convert_old_sigevent32(&job32.aio_sigevent,
2755 &kjob->aio_sigevent));
2759 aiocb32_copyin(struct aiocb *ujob, struct aiocb *kjob)
2761 struct aiocb32 job32;
2764 error = copyin(ujob, &job32, sizeof(job32));
2767 CP(job32, *kjob, aio_fildes);
2768 CP(job32, *kjob, aio_offset);
2769 PTRIN_CP(job32, *kjob, aio_buf);
2770 CP(job32, *kjob, aio_nbytes);
2771 CP(job32, *kjob, aio_lio_opcode);
2772 CP(job32, *kjob, aio_reqprio);
2773 CP(job32, *kjob, _aiocb_private.status);
2774 CP(job32, *kjob, _aiocb_private.error);
2775 PTRIN_CP(job32, *kjob, _aiocb_private.kernelinfo);
2776 return (convert_sigevent32(&job32.aio_sigevent, &kjob->aio_sigevent));
2780 aiocb32_fetch_status(struct aiocb *ujob)
2782 struct aiocb32 *ujob32;
2784 ujob32 = (struct aiocb32 *)ujob;
2785 return (fuword32(&ujob32->_aiocb_private.status));
2789 aiocb32_fetch_error(struct aiocb *ujob)
2791 struct aiocb32 *ujob32;
2793 ujob32 = (struct aiocb32 *)ujob;
2794 return (fuword32(&ujob32->_aiocb_private.error));
2798 aiocb32_store_status(struct aiocb *ujob, long status)
2800 struct aiocb32 *ujob32;
2802 ujob32 = (struct aiocb32 *)ujob;
2803 return (suword32(&ujob32->_aiocb_private.status, status));
2807 aiocb32_store_error(struct aiocb *ujob, long error)
2809 struct aiocb32 *ujob32;
2811 ujob32 = (struct aiocb32 *)ujob;
2812 return (suword32(&ujob32->_aiocb_private.error, error));
2816 aiocb32_store_kernelinfo(struct aiocb *ujob, long jobref)
2818 struct aiocb32 *ujob32;
2820 ujob32 = (struct aiocb32 *)ujob;
2821 return (suword32(&ujob32->_aiocb_private.kernelinfo, jobref));
2825 aiocb32_store_aiocb(struct aiocb **ujobp, struct aiocb *ujob)
2828 return (suword32(ujobp, (long)ujob));
2831 static struct aiocb_ops aiocb32_ops = {
2832 .copyin = aiocb32_copyin,
2833 .fetch_status = aiocb32_fetch_status,
2834 .fetch_error = aiocb32_fetch_error,
2835 .store_status = aiocb32_store_status,
2836 .store_error = aiocb32_store_error,
2837 .store_kernelinfo = aiocb32_store_kernelinfo,
2838 .store_aiocb = aiocb32_store_aiocb,
2841 static struct aiocb_ops aiocb32_ops_osigevent = {
2842 .copyin = aiocb32_copyin_old_sigevent,
2843 .fetch_status = aiocb32_fetch_status,
2844 .fetch_error = aiocb32_fetch_error,
2845 .store_status = aiocb32_store_status,
2846 .store_error = aiocb32_store_error,
2847 .store_kernelinfo = aiocb32_store_kernelinfo,
2848 .store_aiocb = aiocb32_store_aiocb,
2852 freebsd32_aio_return(struct thread *td, struct freebsd32_aio_return_args *uap)
2855 return (kern_aio_return(td, (struct aiocb *)uap->aiocbp, &aiocb32_ops));
2859 freebsd32_aio_suspend(struct thread *td, struct freebsd32_aio_suspend_args *uap)
2861 struct timespec32 ts32;
2862 struct timespec ts, *tsp;
2863 struct aiocb **ujoblist;
2864 uint32_t *ujoblist32;
2867 if (uap->nent < 0 || uap->nent > AIO_LISTIO_MAX)
2871 /* Get timespec struct. */
2872 if ((error = copyin(uap->timeout, &ts32, sizeof(ts32))) != 0)
2874 CP(ts32, ts, tv_sec);
2875 CP(ts32, ts, tv_nsec);
2880 ujoblist = uma_zalloc(aiol_zone, M_WAITOK);
2881 ujoblist32 = (uint32_t *)ujoblist;
2882 error = copyin(uap->aiocbp, ujoblist32, uap->nent *
2883 sizeof(ujoblist32[0]));
2885 for (i = uap->nent; i > 0; i--)
2886 ujoblist[i] = PTRIN(ujoblist32[i]);
2888 error = kern_aio_suspend(td, uap->nent, ujoblist, tsp);
2890 uma_zfree(aiol_zone, ujoblist);
2895 freebsd32_aio_cancel(struct thread *td, struct freebsd32_aio_cancel_args *uap)
2898 return (sys_aio_cancel(td, (struct aio_cancel_args *)uap));
2902 freebsd32_aio_error(struct thread *td, struct freebsd32_aio_error_args *uap)
2905 return (kern_aio_error(td, (struct aiocb *)uap->aiocbp, &aiocb32_ops));
2909 freebsd32_oaio_read(struct thread *td, struct freebsd32_oaio_read_args *uap)
2912 return (aio_aqueue(td, (struct aiocb *)uap->aiocbp, NULL, LIO_READ,
2913 &aiocb32_ops_osigevent));
2917 freebsd32_aio_read(struct thread *td, struct freebsd32_aio_read_args *uap)
2920 return (aio_aqueue(td, (struct aiocb *)uap->aiocbp, NULL, LIO_READ,
2925 freebsd32_oaio_write(struct thread *td, struct freebsd32_oaio_write_args *uap)
2928 return (aio_aqueue(td, (struct aiocb *)uap->aiocbp, NULL, LIO_WRITE,
2929 &aiocb32_ops_osigevent));
2933 freebsd32_aio_write(struct thread *td, struct freebsd32_aio_write_args *uap)
2936 return (aio_aqueue(td, (struct aiocb *)uap->aiocbp, NULL, LIO_WRITE,
2941 freebsd32_aio_mlock(struct thread *td, struct freebsd32_aio_mlock_args *uap)
2944 return (aio_aqueue(td, (struct aiocb *)uap->aiocbp, NULL, LIO_MLOCK,
2949 freebsd32_aio_waitcomplete(struct thread *td,
2950 struct freebsd32_aio_waitcomplete_args *uap)
2952 struct timespec32 ts32;
2953 struct timespec ts, *tsp;
2957 /* Get timespec struct. */
2958 error = copyin(uap->timeout, &ts32, sizeof(ts32));
2961 CP(ts32, ts, tv_sec);
2962 CP(ts32, ts, tv_nsec);
2967 return (kern_aio_waitcomplete(td, (struct aiocb **)uap->aiocbp, tsp,
2972 freebsd32_aio_fsync(struct thread *td, struct freebsd32_aio_fsync_args *uap)
2975 return (kern_aio_fsync(td, uap->op, (struct aiocb *)uap->aiocbp,
2980 freebsd32_olio_listio(struct thread *td, struct freebsd32_olio_listio_args *uap)
2982 struct aiocb **acb_list;
2983 struct sigevent *sigp, sig;
2984 struct osigevent32 osig;
2985 uint32_t *acb_list32;
2988 if ((uap->mode != LIO_NOWAIT) && (uap->mode != LIO_WAIT))
2992 if (nent < 0 || nent > AIO_LISTIO_MAX)
2995 if (uap->sig && (uap->mode == LIO_NOWAIT)) {
2996 error = copyin(uap->sig, &osig, sizeof(osig));
2999 error = convert_old_sigevent32(&osig, &sig);
3006 acb_list32 = malloc(sizeof(uint32_t) * nent, M_LIO, M_WAITOK);
3007 error = copyin(uap->acb_list, acb_list32, nent * sizeof(uint32_t));
3009 free(acb_list32, M_LIO);
3012 acb_list = malloc(sizeof(struct aiocb *) * nent, M_LIO, M_WAITOK);
3013 for (i = 0; i < nent; i++)
3014 acb_list[i] = PTRIN(acb_list32[i]);
3015 free(acb_list32, M_LIO);
3017 error = kern_lio_listio(td, uap->mode,
3018 (struct aiocb * const *)uap->acb_list, acb_list, nent, sigp,
3019 &aiocb32_ops_osigevent);
3020 free(acb_list, M_LIO);
3025 freebsd32_lio_listio(struct thread *td, struct freebsd32_lio_listio_args *uap)
3027 struct aiocb **acb_list;
3028 struct sigevent *sigp, sig;
3029 struct sigevent32 sig32;
3030 uint32_t *acb_list32;
3033 if ((uap->mode != LIO_NOWAIT) && (uap->mode != LIO_WAIT))
3037 if (nent < 0 || nent > AIO_LISTIO_MAX)
3040 if (uap->sig && (uap->mode == LIO_NOWAIT)) {
3041 error = copyin(uap->sig, &sig32, sizeof(sig32));
3044 error = convert_sigevent32(&sig32, &sig);
3051 acb_list32 = malloc(sizeof(uint32_t) * nent, M_LIO, M_WAITOK);
3052 error = copyin(uap->acb_list, acb_list32, nent * sizeof(uint32_t));
3054 free(acb_list32, M_LIO);
3057 acb_list = malloc(sizeof(struct aiocb *) * nent, M_LIO, M_WAITOK);
3058 for (i = 0; i < nent; i++)
3059 acb_list[i] = PTRIN(acb_list32[i]);
3060 free(acb_list32, M_LIO);
3062 error = kern_lio_listio(td, uap->mode,
3063 (struct aiocb * const *)uap->acb_list, acb_list, nent, sigp,
3065 free(acb_list, M_LIO);