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, (csw->d_flags & D_UNMAPPED_IO) == 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;
1572 struct aiocblist *aiocbe, *cb;
1573 struct kaioinfo *ki;
1582 if (p->p_aioinfo == NULL)
1583 aio_init_aioinfo(p);
1587 ops->store_status(job, -1);
1588 ops->store_error(job, 0);
1589 ops->store_kernelinfo(job, -1);
1591 if (num_queue_count >= max_queue_count ||
1592 ki->kaio_count >= ki->kaio_qallowed_count) {
1593 ops->store_error(job, EAGAIN);
1597 aiocbe = uma_zalloc(aiocb_zone, M_WAITOK | M_ZERO);
1598 knlist_init_mtx(&aiocbe->klist, AIO_MTX(ki));
1600 error = ops->copyin(job, &aiocbe->uaiocb);
1602 ops->store_error(job, error);
1603 uma_zfree(aiocb_zone, aiocbe);
1607 /* XXX: aio_nbytes is later casted to signed types. */
1608 if (aiocbe->uaiocb.aio_nbytes > INT_MAX) {
1609 uma_zfree(aiocb_zone, aiocbe);
1613 if (aiocbe->uaiocb.aio_sigevent.sigev_notify != SIGEV_KEVENT &&
1614 aiocbe->uaiocb.aio_sigevent.sigev_notify != SIGEV_SIGNAL &&
1615 aiocbe->uaiocb.aio_sigevent.sigev_notify != SIGEV_THREAD_ID &&
1616 aiocbe->uaiocb.aio_sigevent.sigev_notify != SIGEV_NONE) {
1617 ops->store_error(job, EINVAL);
1618 uma_zfree(aiocb_zone, aiocbe);
1622 if ((aiocbe->uaiocb.aio_sigevent.sigev_notify == SIGEV_SIGNAL ||
1623 aiocbe->uaiocb.aio_sigevent.sigev_notify == SIGEV_THREAD_ID) &&
1624 !_SIG_VALID(aiocbe->uaiocb.aio_sigevent.sigev_signo)) {
1625 uma_zfree(aiocb_zone, aiocbe);
1629 ksiginfo_init(&aiocbe->ksi);
1631 /* Save userspace address of the job info. */
1632 aiocbe->uuaiocb = job;
1634 /* Get the opcode. */
1635 if (type != LIO_NOP)
1636 aiocbe->uaiocb.aio_lio_opcode = type;
1637 opcode = aiocbe->uaiocb.aio_lio_opcode;
1640 * Validate the opcode and fetch the file object for the specified
1643 * XXXRW: Moved the opcode validation up here so that we don't
1644 * retrieve a file descriptor without knowing what the capabiltity
1647 fd = aiocbe->uaiocb.aio_fildes;
1650 error = fget_write(td, fd, CAP_PWRITE, &fp);
1653 error = fget_read(td, fd, CAP_PREAD, &fp);
1656 error = fget(td, fd, CAP_FSYNC, &fp);
1662 error = fget(td, fd, CAP_NONE, &fp);
1668 uma_zfree(aiocb_zone, aiocbe);
1669 ops->store_error(job, error);
1673 if (opcode == LIO_SYNC && fp->f_vnode == NULL) {
1678 if (opcode != LIO_SYNC && aiocbe->uaiocb.aio_offset == -1LL) {
1683 aiocbe->fd_file = fp;
1685 mtx_lock(&aio_job_mtx);
1687 aiocbe->seqno = jobseqno++;
1688 mtx_unlock(&aio_job_mtx);
1689 error = ops->store_kernelinfo(job, jid);
1694 aiocbe->uaiocb._aiocb_private.kernelinfo = (void *)(intptr_t)jid;
1696 if (opcode == LIO_NOP) {
1698 uma_zfree(aiocb_zone, aiocbe);
1702 if (aiocbe->uaiocb.aio_sigevent.sigev_notify != SIGEV_KEVENT)
1704 evflags = aiocbe->uaiocb.aio_sigevent.sigev_notify_kevent_flags;
1705 if ((evflags & ~(EV_CLEAR | EV_DISPATCH | EV_ONESHOT)) != 0) {
1709 kqfd = aiocbe->uaiocb.aio_sigevent.sigev_notify_kqueue;
1710 kev.ident = (uintptr_t)aiocbe->uuaiocb;
1711 kev.filter = EVFILT_AIO;
1712 kev.flags = EV_ADD | EV_ENABLE | EV_FLAG1 | evflags;
1713 kev.data = (intptr_t)aiocbe;
1714 kev.udata = aiocbe->uaiocb.aio_sigevent.sigev_value.sival_ptr;
1715 error = kqfd_register(kqfd, &kev, td, 1);
1720 uma_zfree(aiocb_zone, aiocbe);
1721 ops->store_error(job, error);
1726 ops->store_error(job, EINPROGRESS);
1727 aiocbe->uaiocb._aiocb_private.error = EINPROGRESS;
1728 aiocbe->userproc = p;
1729 aiocbe->cred = crhold(td->td_ucred);
1730 aiocbe->jobflags = 0;
1733 if (opcode == LIO_SYNC)
1736 if (fp && fp->f_type == DTYPE_SOCKET) {
1738 * Alternate queueing for socket ops: Reach down into the
1739 * descriptor to get the socket data. Then check to see if the
1740 * socket is ready to be read or written (based on the requested
1743 * If it is not ready for io, then queue the aiocbe on the
1744 * socket, and set the flags so we get a call when sbnotify()
1747 * Note if opcode is neither LIO_WRITE nor LIO_READ we lock
1748 * and unlock the snd sockbuf for no reason.
1751 sb = (opcode == LIO_READ) ? &so->so_rcv : &so->so_snd;
1753 if (((opcode == LIO_READ) && (!soreadable(so))) || ((opcode ==
1754 LIO_WRITE) && (!sowriteable(so)))) {
1755 sb->sb_flags |= SB_AIO;
1757 mtx_lock(&aio_job_mtx);
1758 TAILQ_INSERT_TAIL(&so->so_aiojobq, aiocbe, list);
1759 mtx_unlock(&aio_job_mtx);
1762 TAILQ_INSERT_TAIL(&ki->kaio_all, aiocbe, allist);
1763 TAILQ_INSERT_TAIL(&ki->kaio_jobqueue, aiocbe, plist);
1764 aiocbe->jobstate = JOBST_JOBQSOCK;
1770 atomic_add_int(&num_queue_count, 1);
1777 if ((error = aio_qphysio(p, aiocbe)) == 0)
1781 aiocbe->uaiocb._aiocb_private.error = error;
1782 ops->store_error(job, error);
1787 /* No buffer for daemon I/O. */
1789 atomic_add_int(&num_queue_count, 1);
1795 TAILQ_INSERT_TAIL(&ki->kaio_jobqueue, aiocbe, plist);
1796 TAILQ_INSERT_TAIL(&ki->kaio_all, aiocbe, allist);
1797 if (opcode == LIO_SYNC) {
1798 TAILQ_FOREACH(cb, &ki->kaio_jobqueue, plist) {
1799 if (cb->fd_file == aiocbe->fd_file &&
1800 cb->uaiocb.aio_lio_opcode != LIO_SYNC &&
1801 cb->seqno < aiocbe->seqno) {
1802 cb->jobflags |= AIOCBLIST_CHECKSYNC;
1806 TAILQ_FOREACH(cb, &ki->kaio_bufqueue, plist) {
1807 if (cb->fd_file == aiocbe->fd_file &&
1808 cb->uaiocb.aio_lio_opcode != LIO_SYNC &&
1809 cb->seqno < aiocbe->seqno) {
1810 cb->jobflags |= AIOCBLIST_CHECKSYNC;
1814 if (aiocbe->pending != 0) {
1815 TAILQ_INSERT_TAIL(&ki->kaio_syncqueue, aiocbe, list);
1816 aiocbe->jobstate = JOBST_JOBQSYNC;
1821 mtx_lock(&aio_job_mtx);
1822 TAILQ_INSERT_TAIL(&aio_jobs, aiocbe, list);
1823 aiocbe->jobstate = JOBST_JOBQGLOBAL;
1825 mtx_unlock(&aio_job_mtx);
1833 aio_kick_nowait(struct proc *userp)
1835 struct kaioinfo *ki = userp->p_aioinfo;
1836 struct aiothreadlist *aiop;
1838 mtx_assert(&aio_job_mtx, MA_OWNED);
1839 if ((aiop = TAILQ_FIRST(&aio_freeproc)) != NULL) {
1840 TAILQ_REMOVE(&aio_freeproc, aiop, list);
1841 aiop->aiothreadflags &= ~AIOP_FREE;
1842 wakeup(aiop->aiothread);
1843 } else if (((num_aio_resv_start + num_aio_procs) < max_aio_procs) &&
1844 ((ki->kaio_active_count + num_aio_resv_start) <
1845 ki->kaio_maxactive_count)) {
1846 taskqueue_enqueue(taskqueue_aiod_bio, &ki->kaio_task);
1851 aio_kick(struct proc *userp)
1853 struct kaioinfo *ki = userp->p_aioinfo;
1854 struct aiothreadlist *aiop;
1857 mtx_assert(&aio_job_mtx, MA_OWNED);
1859 if ((aiop = TAILQ_FIRST(&aio_freeproc)) != NULL) {
1860 TAILQ_REMOVE(&aio_freeproc, aiop, list);
1861 aiop->aiothreadflags &= ~AIOP_FREE;
1862 wakeup(aiop->aiothread);
1863 } else if (((num_aio_resv_start + num_aio_procs) < max_aio_procs) &&
1864 ((ki->kaio_active_count + num_aio_resv_start) <
1865 ki->kaio_maxactive_count)) {
1866 num_aio_resv_start++;
1867 mtx_unlock(&aio_job_mtx);
1868 error = aio_newproc(&num_aio_resv_start);
1869 mtx_lock(&aio_job_mtx);
1871 num_aio_resv_start--;
1881 aio_kick_helper(void *context, int pending)
1883 struct proc *userp = context;
1885 mtx_lock(&aio_job_mtx);
1886 while (--pending >= 0) {
1887 if (aio_kick(userp))
1890 mtx_unlock(&aio_job_mtx);
1894 * Support the aio_return system call, as a side-effect, kernel resources are
1898 kern_aio_return(struct thread *td, struct aiocb *uaiocb, struct aiocb_ops *ops)
1900 struct proc *p = td->td_proc;
1901 struct aiocblist *cb;
1902 struct kaioinfo *ki;
1909 TAILQ_FOREACH(cb, &ki->kaio_done, plist) {
1910 if (cb->uuaiocb == uaiocb)
1914 MPASS(cb->jobstate == JOBST_JOBFINISHED);
1915 status = cb->uaiocb._aiocb_private.status;
1916 error = cb->uaiocb._aiocb_private.error;
1917 td->td_retval[0] = status;
1918 if (cb->uaiocb.aio_lio_opcode == LIO_WRITE) {
1919 td->td_ru.ru_oublock += cb->outputcharge;
1920 cb->outputcharge = 0;
1921 } else if (cb->uaiocb.aio_lio_opcode == LIO_READ) {
1922 td->td_ru.ru_inblock += cb->inputcharge;
1923 cb->inputcharge = 0;
1927 ops->store_error(uaiocb, error);
1928 ops->store_status(uaiocb, status);
1937 sys_aio_return(struct thread *td, struct aio_return_args *uap)
1940 return (kern_aio_return(td, uap->aiocbp, &aiocb_ops));
1944 * Allow a process to wakeup when any of the I/O requests are completed.
1947 kern_aio_suspend(struct thread *td, int njoblist, struct aiocb **ujoblist,
1948 struct timespec *ts)
1950 struct proc *p = td->td_proc;
1952 struct kaioinfo *ki;
1953 struct aiocblist *cb, *cbfirst;
1958 if (ts->tv_nsec < 0 || ts->tv_nsec >= 1000000000)
1961 TIMESPEC_TO_TIMEVAL(&atv, ts);
1962 if (itimerfix(&atv))
1964 timo = tvtohz(&atv);
1978 TAILQ_FOREACH(cb, &ki->kaio_all, allist) {
1979 for (i = 0; i < njoblist; i++) {
1980 if (cb->uuaiocb == ujoblist[i]) {
1981 if (cbfirst == NULL)
1983 if (cb->jobstate == JOBST_JOBFINISHED)
1988 /* All tasks were finished. */
1989 if (cbfirst == NULL)
1992 ki->kaio_flags |= KAIO_WAKEUP;
1993 error = msleep(&p->p_aioinfo, AIO_MTX(ki), PRIBIO | PCATCH,
1995 if (error == ERESTART)
2006 sys_aio_suspend(struct thread *td, struct aio_suspend_args *uap)
2008 struct timespec ts, *tsp;
2009 struct aiocb **ujoblist;
2012 if (uap->nent < 0 || uap->nent > AIO_LISTIO_MAX)
2016 /* Get timespec struct. */
2017 if ((error = copyin(uap->timeout, &ts, sizeof(ts))) != 0)
2023 ujoblist = uma_zalloc(aiol_zone, M_WAITOK);
2024 error = copyin(uap->aiocbp, ujoblist, uap->nent * sizeof(ujoblist[0]));
2026 error = kern_aio_suspend(td, uap->nent, ujoblist, tsp);
2027 uma_zfree(aiol_zone, ujoblist);
2032 * aio_cancel cancels any non-physio aio operations not currently in
2036 sys_aio_cancel(struct thread *td, struct aio_cancel_args *uap)
2038 struct proc *p = td->td_proc;
2039 struct kaioinfo *ki;
2040 struct aiocblist *cbe, *cbn;
2046 int notcancelled = 0;
2049 /* Lookup file object. */
2050 error = fget(td, uap->fd, 0, &fp);
2058 if (fp->f_type == DTYPE_VNODE) {
2060 if (vn_isdisk(vp, &error)) {
2062 td->td_retval[0] = AIO_NOTCANCELED;
2068 TAILQ_FOREACH_SAFE(cbe, &ki->kaio_jobqueue, plist, cbn) {
2069 if ((uap->fd == cbe->uaiocb.aio_fildes) &&
2070 ((uap->aiocbp == NULL) ||
2071 (uap->aiocbp == cbe->uuaiocb))) {
2074 mtx_lock(&aio_job_mtx);
2075 if (cbe->jobstate == JOBST_JOBQGLOBAL) {
2076 TAILQ_REMOVE(&aio_jobs, cbe, list);
2078 } else if (cbe->jobstate == JOBST_JOBQSOCK) {
2079 MPASS(fp->f_type == DTYPE_SOCKET);
2081 TAILQ_REMOVE(&so->so_aiojobq, cbe, list);
2083 } else if (cbe->jobstate == JOBST_JOBQSYNC) {
2084 TAILQ_REMOVE(&ki->kaio_syncqueue, cbe, list);
2087 mtx_unlock(&aio_job_mtx);
2090 TAILQ_REMOVE(&ki->kaio_jobqueue, cbe, plist);
2091 cbe->uaiocb._aiocb_private.status = -1;
2092 cbe->uaiocb._aiocb_private.error = ECANCELED;
2093 aio_bio_done_notify(p, cbe, DONE_QUEUE);
2098 if (uap->aiocbp != NULL)
2107 if (uap->aiocbp != NULL) {
2109 td->td_retval[0] = AIO_CANCELED;
2115 td->td_retval[0] = AIO_NOTCANCELED;
2120 td->td_retval[0] = AIO_CANCELED;
2124 td->td_retval[0] = AIO_ALLDONE;
2130 * aio_error is implemented in the kernel level for compatibility purposes
2131 * only. For a user mode async implementation, it would be best to do it in
2132 * a userland subroutine.
2135 kern_aio_error(struct thread *td, struct aiocb *aiocbp, struct aiocb_ops *ops)
2137 struct proc *p = td->td_proc;
2138 struct aiocblist *cb;
2139 struct kaioinfo *ki;
2144 td->td_retval[0] = EINVAL;
2149 TAILQ_FOREACH(cb, &ki->kaio_all, allist) {
2150 if (cb->uuaiocb == aiocbp) {
2151 if (cb->jobstate == JOBST_JOBFINISHED)
2153 cb->uaiocb._aiocb_private.error;
2155 td->td_retval[0] = EINPROGRESS;
2163 * Hack for failure of aio_aqueue.
2165 status = ops->fetch_status(aiocbp);
2167 td->td_retval[0] = ops->fetch_error(aiocbp);
2171 td->td_retval[0] = EINVAL;
2176 sys_aio_error(struct thread *td, struct aio_error_args *uap)
2179 return (kern_aio_error(td, uap->aiocbp, &aiocb_ops));
2182 /* syscall - asynchronous read from a file (REALTIME) */
2184 sys_oaio_read(struct thread *td, struct oaio_read_args *uap)
2187 return (aio_aqueue(td, (struct aiocb *)uap->aiocbp, NULL, LIO_READ,
2188 &aiocb_ops_osigevent));
2192 sys_aio_read(struct thread *td, struct aio_read_args *uap)
2195 return (aio_aqueue(td, uap->aiocbp, NULL, LIO_READ, &aiocb_ops));
2198 /* syscall - asynchronous write to a file (REALTIME) */
2200 sys_oaio_write(struct thread *td, struct oaio_write_args *uap)
2203 return (aio_aqueue(td, (struct aiocb *)uap->aiocbp, NULL, LIO_WRITE,
2204 &aiocb_ops_osigevent));
2208 sys_aio_write(struct thread *td, struct aio_write_args *uap)
2211 return (aio_aqueue(td, uap->aiocbp, NULL, LIO_WRITE, &aiocb_ops));
2215 sys_aio_mlock(struct thread *td, struct aio_mlock_args *uap)
2218 return (aio_aqueue(td, uap->aiocbp, NULL, LIO_MLOCK, &aiocb_ops));
2222 kern_lio_listio(struct thread *td, int mode, struct aiocb * const *uacb_list,
2223 struct aiocb **acb_list, int nent, struct sigevent *sig,
2224 struct aiocb_ops *ops)
2226 struct proc *p = td->td_proc;
2228 struct kaioinfo *ki;
2229 struct aioliojob *lj;
2235 if ((mode != LIO_NOWAIT) && (mode != LIO_WAIT))
2238 if (nent < 0 || nent > AIO_LISTIO_MAX)
2241 if (p->p_aioinfo == NULL)
2242 aio_init_aioinfo(p);
2246 lj = uma_zalloc(aiolio_zone, M_WAITOK);
2249 lj->lioj_finished_count = 0;
2250 knlist_init_mtx(&lj->klist, AIO_MTX(ki));
2251 ksiginfo_init(&lj->lioj_ksi);
2256 if (sig && (mode == LIO_NOWAIT)) {
2257 bcopy(sig, &lj->lioj_signal, sizeof(lj->lioj_signal));
2258 if (lj->lioj_signal.sigev_notify == SIGEV_KEVENT) {
2259 /* Assume only new style KEVENT */
2260 kev.filter = EVFILT_LIO;
2261 kev.flags = EV_ADD | EV_ENABLE | EV_FLAG1;
2262 kev.ident = (uintptr_t)uacb_list; /* something unique */
2263 kev.data = (intptr_t)lj;
2264 /* pass user defined sigval data */
2265 kev.udata = lj->lioj_signal.sigev_value.sival_ptr;
2266 error = kqfd_register(
2267 lj->lioj_signal.sigev_notify_kqueue, &kev, td, 1);
2269 uma_zfree(aiolio_zone, lj);
2272 } else if (lj->lioj_signal.sigev_notify == SIGEV_NONE) {
2274 } else if (lj->lioj_signal.sigev_notify == SIGEV_SIGNAL ||
2275 lj->lioj_signal.sigev_notify == SIGEV_THREAD_ID) {
2276 if (!_SIG_VALID(lj->lioj_signal.sigev_signo)) {
2277 uma_zfree(aiolio_zone, lj);
2280 lj->lioj_flags |= LIOJ_SIGNAL;
2282 uma_zfree(aiolio_zone, lj);
2288 TAILQ_INSERT_TAIL(&ki->kaio_liojoblist, lj, lioj_list);
2290 * Add extra aiocb count to avoid the lio to be freed
2291 * by other threads doing aio_waitcomplete or aio_return,
2292 * and prevent event from being sent until we have queued
2299 * Get pointers to the list of I/O requests.
2302 for (i = 0; i < nent; i++) {
2305 error = aio_aqueue(td, iocb, lj, LIO_NOP, ops);
2313 if (mode == LIO_WAIT) {
2314 while (lj->lioj_count - 1 != lj->lioj_finished_count) {
2315 ki->kaio_flags |= KAIO_WAKEUP;
2316 error = msleep(&p->p_aioinfo, AIO_MTX(ki),
2317 PRIBIO | PCATCH, "aiospn", 0);
2318 if (error == ERESTART)
2324 if (lj->lioj_count - 1 == lj->lioj_finished_count) {
2325 if (lj->lioj_signal.sigev_notify == SIGEV_KEVENT) {
2326 lj->lioj_flags |= LIOJ_KEVENT_POSTED;
2327 KNOTE_LOCKED(&lj->klist, 1);
2329 if ((lj->lioj_flags & (LIOJ_SIGNAL|LIOJ_SIGNAL_POSTED))
2331 && (lj->lioj_signal.sigev_notify == SIGEV_SIGNAL ||
2332 lj->lioj_signal.sigev_notify == SIGEV_THREAD_ID)) {
2333 aio_sendsig(p, &lj->lioj_signal,
2335 lj->lioj_flags |= LIOJ_SIGNAL_POSTED;
2340 if (lj->lioj_count == 0) {
2341 TAILQ_REMOVE(&ki->kaio_liojoblist, lj, lioj_list);
2342 knlist_delete(&lj->klist, curthread, 1);
2344 sigqueue_take(&lj->lioj_ksi);
2347 uma_zfree(aiolio_zone, lj);
2356 /* syscall - list directed I/O (REALTIME) */
2358 sys_olio_listio(struct thread *td, struct olio_listio_args *uap)
2360 struct aiocb **acb_list;
2361 struct sigevent *sigp, sig;
2362 struct osigevent osig;
2365 if ((uap->mode != LIO_NOWAIT) && (uap->mode != LIO_WAIT))
2369 if (nent < 0 || nent > AIO_LISTIO_MAX)
2372 if (uap->sig && (uap->mode == LIO_NOWAIT)) {
2373 error = copyin(uap->sig, &osig, sizeof(osig));
2376 error = convert_old_sigevent(&osig, &sig);
2383 acb_list = malloc(sizeof(struct aiocb *) * nent, M_LIO, M_WAITOK);
2384 error = copyin(uap->acb_list, acb_list, nent * sizeof(acb_list[0]));
2386 error = kern_lio_listio(td, uap->mode,
2387 (struct aiocb * const *)uap->acb_list, acb_list, nent, sigp,
2388 &aiocb_ops_osigevent);
2389 free(acb_list, M_LIO);
2393 /* syscall - list directed I/O (REALTIME) */
2395 sys_lio_listio(struct thread *td, struct lio_listio_args *uap)
2397 struct aiocb **acb_list;
2398 struct sigevent *sigp, sig;
2401 if ((uap->mode != LIO_NOWAIT) && (uap->mode != LIO_WAIT))
2405 if (nent < 0 || nent > AIO_LISTIO_MAX)
2408 if (uap->sig && (uap->mode == LIO_NOWAIT)) {
2409 error = copyin(uap->sig, &sig, sizeof(sig));
2416 acb_list = malloc(sizeof(struct aiocb *) * nent, M_LIO, M_WAITOK);
2417 error = copyin(uap->acb_list, acb_list, nent * sizeof(acb_list[0]));
2419 error = kern_lio_listio(td, uap->mode, uap->acb_list, acb_list,
2420 nent, sigp, &aiocb_ops);
2421 free(acb_list, M_LIO);
2426 * Called from interrupt thread for physio, we should return as fast
2427 * as possible, so we schedule a biohelper task.
2430 aio_physwakeup(struct buf *bp)
2432 struct aiocblist *aiocbe;
2434 aiocbe = (struct aiocblist *)bp->b_caller1;
2435 taskqueue_enqueue(taskqueue_aiod_bio, &aiocbe->biotask);
2439 * Task routine to perform heavy tasks, process wakeup, and signals.
2442 biohelper(void *context, int pending)
2444 struct aiocblist *aiocbe = context;
2447 struct kaioinfo *ki;
2451 userp = aiocbe->userproc;
2452 ki = userp->p_aioinfo;
2454 aiocbe->uaiocb._aiocb_private.status -= bp->b_resid;
2455 aiocbe->uaiocb._aiocb_private.error = 0;
2456 if (bp->b_ioflags & BIO_ERROR)
2457 aiocbe->uaiocb._aiocb_private.error = bp->b_error;
2458 nblks = btodb(aiocbe->uaiocb.aio_nbytes);
2459 if (aiocbe->uaiocb.aio_lio_opcode == LIO_WRITE)
2460 aiocbe->outputcharge += nblks;
2462 aiocbe->inputcharge += nblks;
2464 TAILQ_REMOVE(&userp->p_aioinfo->kaio_bufqueue, aiocbe, plist);
2465 ki->kaio_buffer_count--;
2466 aio_bio_done_notify(userp, aiocbe, DONE_BUF);
2469 /* Release mapping into kernel space. */
2472 atomic_subtract_int(&num_buf_aio, 1);
2475 /* syscall - wait for the next completion of an aio request */
2477 kern_aio_waitcomplete(struct thread *td, struct aiocb **aiocbp,
2478 struct timespec *ts, struct aiocb_ops *ops)
2480 struct proc *p = td->td_proc;
2482 struct kaioinfo *ki;
2483 struct aiocblist *cb;
2484 struct aiocb *uuaiocb;
2485 int error, status, timo;
2487 ops->store_aiocb(aiocbp, NULL);
2491 if ((ts->tv_nsec < 0) || (ts->tv_nsec >= 1000000000))
2494 TIMESPEC_TO_TIMEVAL(&atv, ts);
2495 if (itimerfix(&atv))
2497 timo = tvtohz(&atv);
2500 if (p->p_aioinfo == NULL)
2501 aio_init_aioinfo(p);
2507 while ((cb = TAILQ_FIRST(&ki->kaio_done)) == NULL) {
2508 ki->kaio_flags |= KAIO_WAKEUP;
2509 error = msleep(&p->p_aioinfo, AIO_MTX(ki), PRIBIO | PCATCH,
2511 if (timo && error == ERESTART)
2518 MPASS(cb->jobstate == JOBST_JOBFINISHED);
2519 uuaiocb = cb->uuaiocb;
2520 status = cb->uaiocb._aiocb_private.status;
2521 error = cb->uaiocb._aiocb_private.error;
2522 td->td_retval[0] = status;
2523 if (cb->uaiocb.aio_lio_opcode == LIO_WRITE) {
2524 td->td_ru.ru_oublock += cb->outputcharge;
2525 cb->outputcharge = 0;
2526 } else if (cb->uaiocb.aio_lio_opcode == LIO_READ) {
2527 td->td_ru.ru_inblock += cb->inputcharge;
2528 cb->inputcharge = 0;
2532 ops->store_aiocb(aiocbp, uuaiocb);
2533 ops->store_error(uuaiocb, error);
2534 ops->store_status(uuaiocb, status);
2542 sys_aio_waitcomplete(struct thread *td, struct aio_waitcomplete_args *uap)
2544 struct timespec ts, *tsp;
2548 /* Get timespec struct. */
2549 error = copyin(uap->timeout, &ts, sizeof(ts));
2556 return (kern_aio_waitcomplete(td, uap->aiocbp, tsp, &aiocb_ops));
2560 kern_aio_fsync(struct thread *td, int op, struct aiocb *aiocbp,
2561 struct aiocb_ops *ops)
2563 struct proc *p = td->td_proc;
2564 struct kaioinfo *ki;
2566 if (op != O_SYNC) /* XXX lack of O_DSYNC */
2570 aio_init_aioinfo(p);
2571 return (aio_aqueue(td, aiocbp, NULL, LIO_SYNC, ops));
2575 sys_aio_fsync(struct thread *td, struct aio_fsync_args *uap)
2578 return (kern_aio_fsync(td, uap->op, uap->aiocbp, &aiocb_ops));
2581 /* kqueue attach function */
2583 filt_aioattach(struct knote *kn)
2585 struct aiocblist *aiocbe = (struct aiocblist *)kn->kn_sdata;
2588 * The aiocbe pointer must be validated before using it, so
2589 * registration is restricted to the kernel; the user cannot
2592 if ((kn->kn_flags & EV_FLAG1) == 0)
2594 kn->kn_ptr.p_aio = aiocbe;
2595 kn->kn_flags &= ~EV_FLAG1;
2597 knlist_add(&aiocbe->klist, kn, 0);
2602 /* kqueue detach function */
2604 filt_aiodetach(struct knote *kn)
2608 knl = &kn->kn_ptr.p_aio->klist;
2609 knl->kl_lock(knl->kl_lockarg);
2610 if (!knlist_empty(knl))
2611 knlist_remove(knl, kn, 1);
2612 knl->kl_unlock(knl->kl_lockarg);
2615 /* kqueue filter function */
2618 filt_aio(struct knote *kn, long hint)
2620 struct aiocblist *aiocbe = kn->kn_ptr.p_aio;
2622 kn->kn_data = aiocbe->uaiocb._aiocb_private.error;
2623 if (aiocbe->jobstate != JOBST_JOBFINISHED)
2625 kn->kn_flags |= EV_EOF;
2629 /* kqueue attach function */
2631 filt_lioattach(struct knote *kn)
2633 struct aioliojob * lj = (struct aioliojob *)kn->kn_sdata;
2636 * The aioliojob pointer must be validated before using it, so
2637 * registration is restricted to the kernel; the user cannot
2640 if ((kn->kn_flags & EV_FLAG1) == 0)
2642 kn->kn_ptr.p_lio = lj;
2643 kn->kn_flags &= ~EV_FLAG1;
2645 knlist_add(&lj->klist, kn, 0);
2650 /* kqueue detach function */
2652 filt_liodetach(struct knote *kn)
2656 knl = &kn->kn_ptr.p_lio->klist;
2657 knl->kl_lock(knl->kl_lockarg);
2658 if (!knlist_empty(knl))
2659 knlist_remove(knl, kn, 1);
2660 knl->kl_unlock(knl->kl_lockarg);
2663 /* kqueue filter function */
2666 filt_lio(struct knote *kn, long hint)
2668 struct aioliojob * lj = kn->kn_ptr.p_lio;
2670 return (lj->lioj_flags & LIOJ_KEVENT_POSTED);
2673 #ifdef COMPAT_FREEBSD32
2675 struct __aiocb_private32 {
2678 uint32_t kernelinfo;
2681 typedef struct oaiocb32 {
2682 int aio_fildes; /* File descriptor */
2683 uint64_t aio_offset __packed; /* File offset for I/O */
2684 uint32_t aio_buf; /* I/O buffer in process space */
2685 uint32_t aio_nbytes; /* Number of bytes for I/O */
2686 struct osigevent32 aio_sigevent; /* Signal to deliver */
2687 int aio_lio_opcode; /* LIO opcode */
2688 int aio_reqprio; /* Request priority -- ignored */
2689 struct __aiocb_private32 _aiocb_private;
2692 typedef struct aiocb32 {
2693 int32_t aio_fildes; /* File descriptor */
2694 uint64_t aio_offset __packed; /* File offset for I/O */
2695 uint32_t aio_buf; /* I/O buffer in process space */
2696 uint32_t aio_nbytes; /* Number of bytes for I/O */
2698 uint32_t __spare2__;
2699 int aio_lio_opcode; /* LIO opcode */
2700 int aio_reqprio; /* Request priority -- ignored */
2701 struct __aiocb_private32 _aiocb_private;
2702 struct sigevent32 aio_sigevent; /* Signal to deliver */
2706 convert_old_sigevent32(struct osigevent32 *osig, struct sigevent *nsig)
2710 * Only SIGEV_NONE, SIGEV_SIGNAL, and SIGEV_KEVENT are
2711 * supported by AIO with the old sigevent structure.
2713 CP(*osig, *nsig, sigev_notify);
2714 switch (nsig->sigev_notify) {
2718 nsig->sigev_signo = osig->__sigev_u.__sigev_signo;
2721 nsig->sigev_notify_kqueue =
2722 osig->__sigev_u.__sigev_notify_kqueue;
2723 PTRIN_CP(*osig, *nsig, sigev_value.sival_ptr);
2732 aiocb32_copyin_old_sigevent(struct aiocb *ujob, struct aiocb *kjob)
2734 struct oaiocb32 job32;
2737 bzero(kjob, sizeof(struct aiocb));
2738 error = copyin(ujob, &job32, sizeof(job32));
2742 CP(job32, *kjob, aio_fildes);
2743 CP(job32, *kjob, aio_offset);
2744 PTRIN_CP(job32, *kjob, aio_buf);
2745 CP(job32, *kjob, aio_nbytes);
2746 CP(job32, *kjob, aio_lio_opcode);
2747 CP(job32, *kjob, aio_reqprio);
2748 CP(job32, *kjob, _aiocb_private.status);
2749 CP(job32, *kjob, _aiocb_private.error);
2750 PTRIN_CP(job32, *kjob, _aiocb_private.kernelinfo);
2751 return (convert_old_sigevent32(&job32.aio_sigevent,
2752 &kjob->aio_sigevent));
2756 aiocb32_copyin(struct aiocb *ujob, struct aiocb *kjob)
2758 struct aiocb32 job32;
2761 error = copyin(ujob, &job32, sizeof(job32));
2764 CP(job32, *kjob, aio_fildes);
2765 CP(job32, *kjob, aio_offset);
2766 PTRIN_CP(job32, *kjob, aio_buf);
2767 CP(job32, *kjob, aio_nbytes);
2768 CP(job32, *kjob, aio_lio_opcode);
2769 CP(job32, *kjob, aio_reqprio);
2770 CP(job32, *kjob, _aiocb_private.status);
2771 CP(job32, *kjob, _aiocb_private.error);
2772 PTRIN_CP(job32, *kjob, _aiocb_private.kernelinfo);
2773 return (convert_sigevent32(&job32.aio_sigevent, &kjob->aio_sigevent));
2777 aiocb32_fetch_status(struct aiocb *ujob)
2779 struct aiocb32 *ujob32;
2781 ujob32 = (struct aiocb32 *)ujob;
2782 return (fuword32(&ujob32->_aiocb_private.status));
2786 aiocb32_fetch_error(struct aiocb *ujob)
2788 struct aiocb32 *ujob32;
2790 ujob32 = (struct aiocb32 *)ujob;
2791 return (fuword32(&ujob32->_aiocb_private.error));
2795 aiocb32_store_status(struct aiocb *ujob, long status)
2797 struct aiocb32 *ujob32;
2799 ujob32 = (struct aiocb32 *)ujob;
2800 return (suword32(&ujob32->_aiocb_private.status, status));
2804 aiocb32_store_error(struct aiocb *ujob, long error)
2806 struct aiocb32 *ujob32;
2808 ujob32 = (struct aiocb32 *)ujob;
2809 return (suword32(&ujob32->_aiocb_private.error, error));
2813 aiocb32_store_kernelinfo(struct aiocb *ujob, long jobref)
2815 struct aiocb32 *ujob32;
2817 ujob32 = (struct aiocb32 *)ujob;
2818 return (suword32(&ujob32->_aiocb_private.kernelinfo, jobref));
2822 aiocb32_store_aiocb(struct aiocb **ujobp, struct aiocb *ujob)
2825 return (suword32(ujobp, (long)ujob));
2828 static struct aiocb_ops aiocb32_ops = {
2829 .copyin = aiocb32_copyin,
2830 .fetch_status = aiocb32_fetch_status,
2831 .fetch_error = aiocb32_fetch_error,
2832 .store_status = aiocb32_store_status,
2833 .store_error = aiocb32_store_error,
2834 .store_kernelinfo = aiocb32_store_kernelinfo,
2835 .store_aiocb = aiocb32_store_aiocb,
2838 static struct aiocb_ops aiocb32_ops_osigevent = {
2839 .copyin = aiocb32_copyin_old_sigevent,
2840 .fetch_status = aiocb32_fetch_status,
2841 .fetch_error = aiocb32_fetch_error,
2842 .store_status = aiocb32_store_status,
2843 .store_error = aiocb32_store_error,
2844 .store_kernelinfo = aiocb32_store_kernelinfo,
2845 .store_aiocb = aiocb32_store_aiocb,
2849 freebsd32_aio_return(struct thread *td, struct freebsd32_aio_return_args *uap)
2852 return (kern_aio_return(td, (struct aiocb *)uap->aiocbp, &aiocb32_ops));
2856 freebsd32_aio_suspend(struct thread *td, struct freebsd32_aio_suspend_args *uap)
2858 struct timespec32 ts32;
2859 struct timespec ts, *tsp;
2860 struct aiocb **ujoblist;
2861 uint32_t *ujoblist32;
2864 if (uap->nent < 0 || uap->nent > AIO_LISTIO_MAX)
2868 /* Get timespec struct. */
2869 if ((error = copyin(uap->timeout, &ts32, sizeof(ts32))) != 0)
2871 CP(ts32, ts, tv_sec);
2872 CP(ts32, ts, tv_nsec);
2877 ujoblist = uma_zalloc(aiol_zone, M_WAITOK);
2878 ujoblist32 = (uint32_t *)ujoblist;
2879 error = copyin(uap->aiocbp, ujoblist32, uap->nent *
2880 sizeof(ujoblist32[0]));
2882 for (i = uap->nent; i > 0; i--)
2883 ujoblist[i] = PTRIN(ujoblist32[i]);
2885 error = kern_aio_suspend(td, uap->nent, ujoblist, tsp);
2887 uma_zfree(aiol_zone, ujoblist);
2892 freebsd32_aio_cancel(struct thread *td, struct freebsd32_aio_cancel_args *uap)
2895 return (sys_aio_cancel(td, (struct aio_cancel_args *)uap));
2899 freebsd32_aio_error(struct thread *td, struct freebsd32_aio_error_args *uap)
2902 return (kern_aio_error(td, (struct aiocb *)uap->aiocbp, &aiocb32_ops));
2906 freebsd32_oaio_read(struct thread *td, struct freebsd32_oaio_read_args *uap)
2909 return (aio_aqueue(td, (struct aiocb *)uap->aiocbp, NULL, LIO_READ,
2910 &aiocb32_ops_osigevent));
2914 freebsd32_aio_read(struct thread *td, struct freebsd32_aio_read_args *uap)
2917 return (aio_aqueue(td, (struct aiocb *)uap->aiocbp, NULL, LIO_READ,
2922 freebsd32_oaio_write(struct thread *td, struct freebsd32_oaio_write_args *uap)
2925 return (aio_aqueue(td, (struct aiocb *)uap->aiocbp, NULL, LIO_WRITE,
2926 &aiocb32_ops_osigevent));
2930 freebsd32_aio_write(struct thread *td, struct freebsd32_aio_write_args *uap)
2933 return (aio_aqueue(td, (struct aiocb *)uap->aiocbp, NULL, LIO_WRITE,
2938 freebsd32_aio_mlock(struct thread *td, struct freebsd32_aio_mlock_args *uap)
2941 return (aio_aqueue(td, (struct aiocb *)uap->aiocbp, NULL, LIO_MLOCK,
2946 freebsd32_aio_waitcomplete(struct thread *td,
2947 struct freebsd32_aio_waitcomplete_args *uap)
2949 struct timespec32 ts32;
2950 struct timespec ts, *tsp;
2954 /* Get timespec struct. */
2955 error = copyin(uap->timeout, &ts32, sizeof(ts32));
2958 CP(ts32, ts, tv_sec);
2959 CP(ts32, ts, tv_nsec);
2964 return (kern_aio_waitcomplete(td, (struct aiocb **)uap->aiocbp, tsp,
2969 freebsd32_aio_fsync(struct thread *td, struct freebsd32_aio_fsync_args *uap)
2972 return (kern_aio_fsync(td, uap->op, (struct aiocb *)uap->aiocbp,
2977 freebsd32_olio_listio(struct thread *td, struct freebsd32_olio_listio_args *uap)
2979 struct aiocb **acb_list;
2980 struct sigevent *sigp, sig;
2981 struct osigevent32 osig;
2982 uint32_t *acb_list32;
2985 if ((uap->mode != LIO_NOWAIT) && (uap->mode != LIO_WAIT))
2989 if (nent < 0 || nent > AIO_LISTIO_MAX)
2992 if (uap->sig && (uap->mode == LIO_NOWAIT)) {
2993 error = copyin(uap->sig, &osig, sizeof(osig));
2996 error = convert_old_sigevent32(&osig, &sig);
3003 acb_list32 = malloc(sizeof(uint32_t) * nent, M_LIO, M_WAITOK);
3004 error = copyin(uap->acb_list, acb_list32, nent * sizeof(uint32_t));
3006 free(acb_list32, M_LIO);
3009 acb_list = malloc(sizeof(struct aiocb *) * nent, M_LIO, M_WAITOK);
3010 for (i = 0; i < nent; i++)
3011 acb_list[i] = PTRIN(acb_list32[i]);
3012 free(acb_list32, M_LIO);
3014 error = kern_lio_listio(td, uap->mode,
3015 (struct aiocb * const *)uap->acb_list, acb_list, nent, sigp,
3016 &aiocb32_ops_osigevent);
3017 free(acb_list, M_LIO);
3022 freebsd32_lio_listio(struct thread *td, struct freebsd32_lio_listio_args *uap)
3024 struct aiocb **acb_list;
3025 struct sigevent *sigp, sig;
3026 struct sigevent32 sig32;
3027 uint32_t *acb_list32;
3030 if ((uap->mode != LIO_NOWAIT) && (uap->mode != LIO_WAIT))
3034 if (nent < 0 || nent > AIO_LISTIO_MAX)
3037 if (uap->sig && (uap->mode == LIO_NOWAIT)) {
3038 error = copyin(uap->sig, &sig32, sizeof(sig32));
3041 error = convert_sigevent32(&sig32, &sig);
3048 acb_list32 = malloc(sizeof(uint32_t) * nent, M_LIO, M_WAITOK);
3049 error = copyin(uap->acb_list, acb_list32, nent * sizeof(uint32_t));
3051 free(acb_list32, M_LIO);
3054 acb_list = malloc(sizeof(struct aiocb *) * nent, M_LIO, M_WAITOK);
3055 for (i = 0; i < nent; i++)
3056 acb_list[i] = PTRIN(acb_list32[i]);
3057 free(acb_list32, M_LIO);
3059 error = kern_lio_listio(td, uap->mode,
3060 (struct aiocb * const *)uap->acb_list, acb_list, nent, sigp,
3062 free(acb_list, M_LIO);