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>
50 #include <sys/socket.h>
51 #include <sys/socketvar.h>
52 #include <sys/syscall.h>
53 #include <sys/sysent.h>
54 #include <sys/sysctl.h>
56 #include <sys/taskqueue.h>
57 #include <sys/vnode.h>
59 #include <sys/event.h>
60 #include <sys/mount.h>
62 #include <machine/atomic.h>
65 #include <vm/vm_extern.h>
67 #include <vm/vm_map.h>
68 #include <vm/vm_object.h>
72 #include "opt_vfs_aio.h"
75 * Counter for allocating reference ids to new jobs. Wrapped to 1 on
76 * overflow. (XXX will be removed soon.)
78 static u_long jobrefid;
81 * Counter for aio_fsync.
83 static uint64_t jobseqno;
86 #define JOBST_JOBQSOCK 1
87 #define JOBST_JOBQGLOBAL 2
88 #define JOBST_JOBRUNNING 3
89 #define JOBST_JOBFINISHED 4
90 #define JOBST_JOBQBUF 5
91 #define JOBST_JOBQSYNC 6
93 #ifndef MAX_AIO_PER_PROC
94 #define MAX_AIO_PER_PROC 32
97 #ifndef MAX_AIO_QUEUE_PER_PROC
98 #define MAX_AIO_QUEUE_PER_PROC 256 /* Bigger than AIO_LISTIO_MAX */
101 #ifndef MAX_AIO_PROCS
102 #define MAX_AIO_PROCS 32
105 #ifndef MAX_AIO_QUEUE
106 #define MAX_AIO_QUEUE 1024 /* Bigger than AIO_LISTIO_MAX */
109 #ifndef TARGET_AIO_PROCS
110 #define TARGET_AIO_PROCS 4
114 #define MAX_BUF_AIO 16
117 #ifndef AIOD_TIMEOUT_DEFAULT
118 #define AIOD_TIMEOUT_DEFAULT (10 * hz)
121 #ifndef AIOD_LIFETIME_DEFAULT
122 #define AIOD_LIFETIME_DEFAULT (30 * hz)
125 FEATURE(aio, "Asynchronous I/O");
127 static MALLOC_DEFINE(M_LIO, "lio", "listio aio control block list");
129 static SYSCTL_NODE(_vfs, OID_AUTO, aio, CTLFLAG_RW, 0, "Async IO management");
131 static int max_aio_procs = MAX_AIO_PROCS;
132 SYSCTL_INT(_vfs_aio, OID_AUTO, max_aio_procs,
133 CTLFLAG_RW, &max_aio_procs, 0,
134 "Maximum number of kernel threads to use for handling async IO ");
136 static int num_aio_procs = 0;
137 SYSCTL_INT(_vfs_aio, OID_AUTO, num_aio_procs,
138 CTLFLAG_RD, &num_aio_procs, 0,
139 "Number of presently active kernel threads for async IO");
142 * The code will adjust the actual number of AIO processes towards this
143 * number when it gets a chance.
145 static int target_aio_procs = TARGET_AIO_PROCS;
146 SYSCTL_INT(_vfs_aio, OID_AUTO, target_aio_procs, CTLFLAG_RW, &target_aio_procs,
147 0, "Preferred number of ready kernel threads for async IO");
149 static int max_queue_count = MAX_AIO_QUEUE;
150 SYSCTL_INT(_vfs_aio, OID_AUTO, max_aio_queue, CTLFLAG_RW, &max_queue_count, 0,
151 "Maximum number of aio requests to queue, globally");
153 static int num_queue_count = 0;
154 SYSCTL_INT(_vfs_aio, OID_AUTO, num_queue_count, CTLFLAG_RD, &num_queue_count, 0,
155 "Number of queued aio requests");
157 static int num_buf_aio = 0;
158 SYSCTL_INT(_vfs_aio, OID_AUTO, num_buf_aio, CTLFLAG_RD, &num_buf_aio, 0,
159 "Number of aio requests presently handled by the buf subsystem");
161 /* Number of async I/O thread in the process of being started */
162 /* XXX This should be local to aio_aqueue() */
163 static int num_aio_resv_start = 0;
165 static int aiod_timeout;
166 SYSCTL_INT(_vfs_aio, OID_AUTO, aiod_timeout, CTLFLAG_RW, &aiod_timeout, 0,
167 "Timeout value for synchronous aio operations");
169 static int aiod_lifetime;
170 SYSCTL_INT(_vfs_aio, OID_AUTO, aiod_lifetime, CTLFLAG_RW, &aiod_lifetime, 0,
171 "Maximum lifetime for idle aiod");
173 static int unloadable = 0;
174 SYSCTL_INT(_vfs_aio, OID_AUTO, unloadable, CTLFLAG_RW, &unloadable, 0,
175 "Allow unload of aio (not recommended)");
178 static int max_aio_per_proc = MAX_AIO_PER_PROC;
179 SYSCTL_INT(_vfs_aio, OID_AUTO, max_aio_per_proc, CTLFLAG_RW, &max_aio_per_proc,
180 0, "Maximum active aio requests per process (stored in the process)");
182 static int max_aio_queue_per_proc = MAX_AIO_QUEUE_PER_PROC;
183 SYSCTL_INT(_vfs_aio, OID_AUTO, max_aio_queue_per_proc, CTLFLAG_RW,
184 &max_aio_queue_per_proc, 0,
185 "Maximum queued aio requests per process (stored in the process)");
187 static int max_buf_aio = MAX_BUF_AIO;
188 SYSCTL_INT(_vfs_aio, OID_AUTO, max_buf_aio, CTLFLAG_RW, &max_buf_aio, 0,
189 "Maximum buf aio requests per process (stored in the process)");
191 typedef struct oaiocb {
192 int aio_fildes; /* File descriptor */
193 off_t aio_offset; /* File offset for I/O */
194 volatile void *aio_buf; /* I/O buffer in process space */
195 size_t aio_nbytes; /* Number of bytes for I/O */
196 struct osigevent aio_sigevent; /* Signal to deliver */
197 int aio_lio_opcode; /* LIO opcode */
198 int aio_reqprio; /* Request priority -- ignored */
199 struct __aiocb_private _aiocb_private;
203 * Below is a key of locks used to protect each member of struct aiocblist
204 * aioliojob and kaioinfo and any backends.
206 * * - need not protected
207 * a - locked by kaioinfo lock
208 * b - locked by backend lock, the backend lock can be null in some cases,
209 * for example, BIO belongs to this type, in this case, proc lock is
211 * c - locked by aio_job_mtx, the lock for the generic file I/O backend.
215 * Current, there is only two backends: BIO and generic file I/O.
216 * socket I/O is served by generic file I/O, this is not a good idea, since
217 * disk file I/O and any other types without O_NONBLOCK flag can block daemon
218 * threads, if there is no thread to serve socket I/O, the socket I/O will be
219 * delayed too long or starved, we should create some threads dedicated to
220 * sockets to do non-blocking I/O, same for pipe and fifo, for these I/O
221 * systems we really need non-blocking interface, fiddling O_NONBLOCK in file
222 * structure is not safe because there is race between userland and aio
227 TAILQ_ENTRY(aiocblist) list; /* (b) internal list of for backend */
228 TAILQ_ENTRY(aiocblist) plist; /* (a) list of jobs for each backend */
229 TAILQ_ENTRY(aiocblist) allist; /* (a) list of all jobs in proc */
230 int jobflags; /* (a) job flags */
231 int jobstate; /* (b) job state */
232 int inputcharge; /* (*) input blockes */
233 int outputcharge; /* (*) output blockes */
234 struct buf *bp; /* (*) private to BIO backend,
237 struct proc *userproc; /* (*) user process */
238 struct ucred *cred; /* (*) active credential when created */
239 struct file *fd_file; /* (*) pointer to file structure */
240 struct aioliojob *lio; /* (*) optional lio job */
241 struct aiocb *uuaiocb; /* (*) pointer in userspace of aiocb */
242 struct knlist klist; /* (a) list of knotes */
243 struct aiocb uaiocb; /* (*) kernel I/O control block */
244 ksiginfo_t ksi; /* (a) realtime signal info */
245 struct task biotask; /* (*) private to BIO backend */
246 uint64_t seqno; /* (*) job number */
247 int pending; /* (a) number of pending I/O, aio_fsync only */
251 #define AIOCBLIST_DONE 0x01
252 #define AIOCBLIST_BUFDONE 0x02
253 #define AIOCBLIST_RUNDOWN 0x04
254 #define AIOCBLIST_CHECKSYNC 0x08
259 #define AIOP_FREE 0x1 /* proc on free queue */
261 struct aiothreadlist {
262 int aiothreadflags; /* (c) AIO proc flags */
263 TAILQ_ENTRY(aiothreadlist) list; /* (c) list of processes */
264 struct thread *aiothread; /* (*) the AIO thread */
268 * data-structure for lio signal management
271 int lioj_flags; /* (a) listio flags */
272 int lioj_count; /* (a) listio flags */
273 int lioj_finished_count; /* (a) listio flags */
274 struct sigevent lioj_signal; /* (a) signal on all I/O done */
275 TAILQ_ENTRY(aioliojob) lioj_list; /* (a) lio list */
276 struct knlist klist; /* (a) list of knotes */
277 ksiginfo_t lioj_ksi; /* (a) Realtime signal info */
280 #define LIOJ_SIGNAL 0x1 /* signal on all done (lio) */
281 #define LIOJ_SIGNAL_POSTED 0x2 /* signal has been posted */
282 #define LIOJ_KEVENT_POSTED 0x4 /* kevent triggered */
285 * per process aio data structure
288 struct mtx kaio_mtx; /* the lock to protect this struct */
289 int kaio_flags; /* (a) per process kaio flags */
290 int kaio_maxactive_count; /* (*) maximum number of AIOs */
291 int kaio_active_count; /* (c) number of currently used AIOs */
292 int kaio_qallowed_count; /* (*) maxiumu size of AIO queue */
293 int kaio_count; /* (a) size of AIO queue */
294 int kaio_ballowed_count; /* (*) maximum number of buffers */
295 int kaio_buffer_count; /* (a) number of physio buffers */
296 TAILQ_HEAD(,aiocblist) kaio_all; /* (a) all AIOs in the process */
297 TAILQ_HEAD(,aiocblist) kaio_done; /* (a) done queue for process */
298 TAILQ_HEAD(,aioliojob) kaio_liojoblist; /* (a) list of lio jobs */
299 TAILQ_HEAD(,aiocblist) kaio_jobqueue; /* (a) job queue for process */
300 TAILQ_HEAD(,aiocblist) kaio_bufqueue; /* (a) buffer job queue for process */
301 TAILQ_HEAD(,aiocblist) kaio_sockqueue; /* (a) queue for aios waiting on sockets,
304 TAILQ_HEAD(,aiocblist) kaio_syncqueue; /* (a) queue for aio_fsync */
305 struct task kaio_task; /* (*) task to kick aio threads */
308 #define AIO_LOCK(ki) mtx_lock(&(ki)->kaio_mtx)
309 #define AIO_UNLOCK(ki) mtx_unlock(&(ki)->kaio_mtx)
310 #define AIO_LOCK_ASSERT(ki, f) mtx_assert(&(ki)->kaio_mtx, (f))
311 #define AIO_MTX(ki) (&(ki)->kaio_mtx)
313 #define KAIO_RUNDOWN 0x1 /* process is being run down */
314 #define KAIO_WAKEUP 0x2 /* wakeup process when there is a significant event */
317 * Operations used to interact with userland aio control blocks.
318 * Different ABIs provide their own operations.
321 int (*copyin)(struct aiocb *ujob, struct aiocb *kjob);
322 long (*fetch_status)(struct aiocb *ujob);
323 long (*fetch_error)(struct aiocb *ujob);
324 int (*store_status)(struct aiocb *ujob, long status);
325 int (*store_error)(struct aiocb *ujob, long error);
326 int (*store_kernelinfo)(struct aiocb *ujob, long jobref);
327 int (*store_aiocb)(struct aiocb **ujobp, struct aiocb *ujob);
330 static TAILQ_HEAD(,aiothreadlist) aio_freeproc; /* (c) Idle daemons */
331 static struct sema aio_newproc_sem;
332 static struct mtx aio_job_mtx;
333 static struct mtx aio_sock_mtx;
334 static TAILQ_HEAD(,aiocblist) aio_jobs; /* (c) Async job list */
335 static struct unrhdr *aiod_unr;
337 void aio_init_aioinfo(struct proc *p);
338 static int aio_onceonly(void);
339 static int aio_free_entry(struct aiocblist *aiocbe);
340 static void aio_process(struct aiocblist *aiocbe);
341 static int aio_newproc(int *);
342 int aio_aqueue(struct thread *td, struct aiocb *job,
343 struct aioliojob *lio, int type, struct aiocb_ops *ops);
344 static void aio_physwakeup(struct buf *bp);
345 static void aio_proc_rundown(void *arg, struct proc *p);
346 static void aio_proc_rundown_exec(void *arg, struct proc *p, struct image_params *imgp);
347 static int aio_qphysio(struct proc *p, struct aiocblist *iocb);
348 static void biohelper(void *, int);
349 static void aio_daemon(void *param);
350 static void aio_swake_cb(struct socket *, struct sockbuf *);
351 static int aio_unload(void);
352 static void aio_bio_done_notify(struct proc *userp, struct aiocblist *aiocbe, int type);
355 static int aio_kick(struct proc *userp);
356 static void aio_kick_nowait(struct proc *userp);
357 static void aio_kick_helper(void *context, int pending);
358 static int filt_aioattach(struct knote *kn);
359 static void filt_aiodetach(struct knote *kn);
360 static int filt_aio(struct knote *kn, long hint);
361 static int filt_lioattach(struct knote *kn);
362 static void filt_liodetach(struct knote *kn);
363 static int filt_lio(struct knote *kn, long hint);
367 * kaio Per process async io info
368 * aiop async io thread data
369 * aiocb async io jobs
370 * aiol list io job pointer - internal to aio_suspend XXX
371 * aiolio list io jobs
373 static uma_zone_t kaio_zone, aiop_zone, aiocb_zone, aiol_zone, aiolio_zone;
375 /* kqueue filters for aio */
376 static struct filterops aio_filtops = {
378 .f_attach = filt_aioattach,
379 .f_detach = filt_aiodetach,
382 static struct filterops lio_filtops = {
384 .f_attach = filt_lioattach,
385 .f_detach = filt_liodetach,
389 static eventhandler_tag exit_tag, exec_tag;
391 TASKQUEUE_DEFINE_THREAD(aiod_bio);
394 * Main operations function for use as a kernel module.
397 aio_modload(struct module *module, int cmd, void *arg)
406 error = aio_unload();
417 static moduledata_t aio_mod = {
423 static struct syscall_helper_data aio_syscalls[] = {
424 SYSCALL_INIT_HELPER(aio_cancel),
425 SYSCALL_INIT_HELPER(aio_error),
426 SYSCALL_INIT_HELPER(aio_fsync),
427 SYSCALL_INIT_HELPER(aio_read),
428 SYSCALL_INIT_HELPER(aio_return),
429 SYSCALL_INIT_HELPER(aio_suspend),
430 SYSCALL_INIT_HELPER(aio_waitcomplete),
431 SYSCALL_INIT_HELPER(aio_write),
432 SYSCALL_INIT_HELPER(lio_listio),
433 SYSCALL_INIT_HELPER(oaio_read),
434 SYSCALL_INIT_HELPER(oaio_write),
435 SYSCALL_INIT_HELPER(olio_listio),
439 #ifdef COMPAT_FREEBSD32
440 #include <sys/mount.h>
441 #include <sys/socket.h>
442 #include <compat/freebsd32/freebsd32.h>
443 #include <compat/freebsd32/freebsd32_proto.h>
444 #include <compat/freebsd32/freebsd32_signal.h>
445 #include <compat/freebsd32/freebsd32_syscall.h>
446 #include <compat/freebsd32/freebsd32_util.h>
448 static struct syscall_helper_data aio32_syscalls[] = {
449 SYSCALL32_INIT_HELPER(freebsd32_aio_return),
450 SYSCALL32_INIT_HELPER(freebsd32_aio_suspend),
451 SYSCALL32_INIT_HELPER(freebsd32_aio_cancel),
452 SYSCALL32_INIT_HELPER(freebsd32_aio_error),
453 SYSCALL32_INIT_HELPER(freebsd32_aio_fsync),
454 SYSCALL32_INIT_HELPER(freebsd32_aio_read),
455 SYSCALL32_INIT_HELPER(freebsd32_aio_write),
456 SYSCALL32_INIT_HELPER(freebsd32_aio_waitcomplete),
457 SYSCALL32_INIT_HELPER(freebsd32_lio_listio),
458 SYSCALL32_INIT_HELPER(freebsd32_oaio_read),
459 SYSCALL32_INIT_HELPER(freebsd32_oaio_write),
460 SYSCALL32_INIT_HELPER(freebsd32_olio_listio),
465 DECLARE_MODULE(aio, aio_mod,
466 SI_SUB_VFS, SI_ORDER_ANY);
467 MODULE_VERSION(aio, 1);
470 * Startup initialization
477 /* XXX: should probably just use so->callback */
478 aio_swake = &aio_swake_cb;
479 exit_tag = EVENTHANDLER_REGISTER(process_exit, aio_proc_rundown, NULL,
480 EVENTHANDLER_PRI_ANY);
481 exec_tag = EVENTHANDLER_REGISTER(process_exec, aio_proc_rundown_exec, NULL,
482 EVENTHANDLER_PRI_ANY);
483 kqueue_add_filteropts(EVFILT_AIO, &aio_filtops);
484 kqueue_add_filteropts(EVFILT_LIO, &lio_filtops);
485 TAILQ_INIT(&aio_freeproc);
486 sema_init(&aio_newproc_sem, 0, "aio_new_proc");
487 mtx_init(&aio_job_mtx, "aio_job", NULL, MTX_DEF);
488 mtx_init(&aio_sock_mtx, "aio_sock", NULL, MTX_DEF);
489 TAILQ_INIT(&aio_jobs);
490 aiod_unr = new_unrhdr(1, INT_MAX, NULL);
491 kaio_zone = uma_zcreate("AIO", sizeof(struct kaioinfo), NULL, NULL,
492 NULL, NULL, UMA_ALIGN_PTR, UMA_ZONE_NOFREE);
493 aiop_zone = uma_zcreate("AIOP", sizeof(struct aiothreadlist), NULL,
494 NULL, NULL, NULL, UMA_ALIGN_PTR, UMA_ZONE_NOFREE);
495 aiocb_zone = uma_zcreate("AIOCB", sizeof(struct aiocblist), NULL, NULL,
496 NULL, NULL, UMA_ALIGN_PTR, UMA_ZONE_NOFREE);
497 aiol_zone = uma_zcreate("AIOL", AIO_LISTIO_MAX*sizeof(intptr_t) , NULL,
498 NULL, NULL, NULL, UMA_ALIGN_PTR, UMA_ZONE_NOFREE);
499 aiolio_zone = uma_zcreate("AIOLIO", sizeof(struct aioliojob), NULL,
500 NULL, NULL, NULL, UMA_ALIGN_PTR, UMA_ZONE_NOFREE);
501 aiod_timeout = AIOD_TIMEOUT_DEFAULT;
502 aiod_lifetime = AIOD_LIFETIME_DEFAULT;
504 async_io_version = _POSIX_VERSION;
505 p31b_setcfg(CTL_P1003_1B_AIO_LISTIO_MAX, AIO_LISTIO_MAX);
506 p31b_setcfg(CTL_P1003_1B_AIO_MAX, MAX_AIO_QUEUE);
507 p31b_setcfg(CTL_P1003_1B_AIO_PRIO_DELTA_MAX, 0);
509 error = syscall_helper_register(aio_syscalls);
512 #ifdef COMPAT_FREEBSD32
513 error = syscall32_helper_register(aio32_syscalls);
521 * Callback for unload of AIO when used as a module.
529 * XXX: no unloads by default, it's too dangerous.
530 * perhaps we could do it if locked out callers and then
531 * did an aio_proc_rundown() on each process.
533 * jhb: aio_proc_rundown() needs to run on curproc though,
534 * so I don't think that would fly.
539 #ifdef COMPAT_FREEBSD32
540 syscall32_helper_unregister(aio32_syscalls);
542 syscall_helper_unregister(aio_syscalls);
544 error = kqueue_del_filteropts(EVFILT_AIO);
547 error = kqueue_del_filteropts(EVFILT_LIO);
550 async_io_version = 0;
552 taskqueue_free(taskqueue_aiod_bio);
553 delete_unrhdr(aiod_unr);
554 uma_zdestroy(kaio_zone);
555 uma_zdestroy(aiop_zone);
556 uma_zdestroy(aiocb_zone);
557 uma_zdestroy(aiol_zone);
558 uma_zdestroy(aiolio_zone);
559 EVENTHANDLER_DEREGISTER(process_exit, exit_tag);
560 EVENTHANDLER_DEREGISTER(process_exec, exec_tag);
561 mtx_destroy(&aio_job_mtx);
562 mtx_destroy(&aio_sock_mtx);
563 sema_destroy(&aio_newproc_sem);
564 p31b_setcfg(CTL_P1003_1B_AIO_LISTIO_MAX, -1);
565 p31b_setcfg(CTL_P1003_1B_AIO_MAX, -1);
566 p31b_setcfg(CTL_P1003_1B_AIO_PRIO_DELTA_MAX, -1);
571 * Init the per-process aioinfo structure. The aioinfo limits are set
572 * per-process for user limit (resource) management.
575 aio_init_aioinfo(struct proc *p)
579 ki = uma_zalloc(kaio_zone, M_WAITOK);
580 mtx_init(&ki->kaio_mtx, "aiomtx", NULL, MTX_DEF);
582 ki->kaio_maxactive_count = max_aio_per_proc;
583 ki->kaio_active_count = 0;
584 ki->kaio_qallowed_count = max_aio_queue_per_proc;
586 ki->kaio_ballowed_count = max_buf_aio;
587 ki->kaio_buffer_count = 0;
588 TAILQ_INIT(&ki->kaio_all);
589 TAILQ_INIT(&ki->kaio_done);
590 TAILQ_INIT(&ki->kaio_jobqueue);
591 TAILQ_INIT(&ki->kaio_bufqueue);
592 TAILQ_INIT(&ki->kaio_liojoblist);
593 TAILQ_INIT(&ki->kaio_sockqueue);
594 TAILQ_INIT(&ki->kaio_syncqueue);
595 TASK_INIT(&ki->kaio_task, 0, aio_kick_helper, p);
597 if (p->p_aioinfo == NULL) {
602 mtx_destroy(&ki->kaio_mtx);
603 uma_zfree(kaio_zone, ki);
606 while (num_aio_procs < MIN(target_aio_procs, max_aio_procs))
611 aio_sendsig(struct proc *p, struct sigevent *sigev, ksiginfo_t *ksi)
616 error = sigev_findtd(p, sigev, &td);
620 ksiginfo_set_sigev(ksi, sigev);
621 ksi->ksi_code = SI_ASYNCIO;
622 ksi->ksi_flags |= KSI_EXT | KSI_INS;
623 tdsendsignal(p, td, ksi->ksi_signo, ksi);
630 * Free a job entry. Wait for completion if it is currently active, but don't
631 * delay forever. If we delay, we return a flag that says that we have to
632 * restart the queue scan.
635 aio_free_entry(struct aiocblist *aiocbe)
638 struct aioliojob *lj;
641 p = aiocbe->userproc;
646 AIO_LOCK_ASSERT(ki, MA_OWNED);
647 MPASS(aiocbe->jobstate == JOBST_JOBFINISHED);
649 atomic_subtract_int(&num_queue_count, 1);
652 MPASS(ki->kaio_count >= 0);
654 TAILQ_REMOVE(&ki->kaio_done, aiocbe, plist);
655 TAILQ_REMOVE(&ki->kaio_all, aiocbe, allist);
660 lj->lioj_finished_count--;
662 if (lj->lioj_count == 0) {
663 TAILQ_REMOVE(&ki->kaio_liojoblist, lj, lioj_list);
664 /* lio is going away, we need to destroy any knotes */
665 knlist_delete(&lj->klist, curthread, 1);
667 sigqueue_take(&lj->lioj_ksi);
669 uma_zfree(aiolio_zone, lj);
673 /* aiocbe is going away, we need to destroy any knotes */
674 knlist_delete(&aiocbe->klist, curthread, 1);
676 sigqueue_take(&aiocbe->ksi);
679 MPASS(aiocbe->bp == NULL);
680 aiocbe->jobstate = JOBST_NULL;
684 * The thread argument here is used to find the owning process
685 * and is also passed to fo_close() which may pass it to various
686 * places such as devsw close() routines. Because of that, we
687 * need a thread pointer from the process owning the job that is
688 * persistent and won't disappear out from under us or move to
691 * Currently, all the callers of this function call it to remove
692 * an aiocblist from the current process' job list either via a
693 * syscall or due to the current process calling exit() or
694 * execve(). Thus, we know that p == curproc. We also know that
695 * curthread can't exit since we are curthread.
697 * Therefore, we use curthread as the thread to pass to
698 * knlist_delete(). This does mean that it is possible for the
699 * thread pointer at close time to differ from the thread pointer
700 * at open time, but this is already true of file descriptors in
701 * a multithreaded process.
703 fdrop(aiocbe->fd_file, curthread);
704 crfree(aiocbe->cred);
705 uma_zfree(aiocb_zone, aiocbe);
712 aio_proc_rundown_exec(void *arg, struct proc *p, struct image_params *imgp __unused)
714 aio_proc_rundown(arg, p);
718 * Rundown the jobs for a given process.
721 aio_proc_rundown(void *arg, struct proc *p)
724 struct aioliojob *lj;
725 struct aiocblist *cbe, *cbn;
730 KASSERT(curthread->td_proc == p,
731 ("%s: called on non-curproc", __func__));
737 ki->kaio_flags |= KAIO_RUNDOWN;
742 * Try to cancel all pending requests. This code simulates
743 * aio_cancel on all pending I/O requests.
745 TAILQ_FOREACH_SAFE(cbe, &ki->kaio_jobqueue, plist, cbn) {
747 mtx_lock(&aio_job_mtx);
748 if (cbe->jobstate == JOBST_JOBQGLOBAL) {
749 TAILQ_REMOVE(&aio_jobs, cbe, list);
751 } else if (cbe->jobstate == JOBST_JOBQSOCK) {
753 MPASS(fp->f_type == DTYPE_SOCKET);
755 TAILQ_REMOVE(&so->so_aiojobq, cbe, list);
757 } else if (cbe->jobstate == JOBST_JOBQSYNC) {
758 TAILQ_REMOVE(&ki->kaio_syncqueue, cbe, list);
761 mtx_unlock(&aio_job_mtx);
764 cbe->jobstate = JOBST_JOBFINISHED;
765 cbe->uaiocb._aiocb_private.status = -1;
766 cbe->uaiocb._aiocb_private.error = ECANCELED;
767 TAILQ_REMOVE(&ki->kaio_jobqueue, cbe, plist);
768 aio_bio_done_notify(p, cbe, DONE_QUEUE);
772 /* Wait for all running I/O to be finished */
773 if (TAILQ_FIRST(&ki->kaio_bufqueue) ||
774 TAILQ_FIRST(&ki->kaio_jobqueue)) {
775 ki->kaio_flags |= KAIO_WAKEUP;
776 msleep(&p->p_aioinfo, AIO_MTX(ki), PRIBIO, "aioprn", hz);
780 /* Free all completed I/O requests. */
781 while ((cbe = TAILQ_FIRST(&ki->kaio_done)) != NULL)
784 while ((lj = TAILQ_FIRST(&ki->kaio_liojoblist)) != NULL) {
785 if (lj->lioj_count == 0) {
786 TAILQ_REMOVE(&ki->kaio_liojoblist, lj, lioj_list);
787 knlist_delete(&lj->klist, curthread, 1);
789 sigqueue_take(&lj->lioj_ksi);
791 uma_zfree(aiolio_zone, lj);
793 panic("LIO job not cleaned up: C:%d, FC:%d\n",
794 lj->lioj_count, lj->lioj_finished_count);
798 taskqueue_drain(taskqueue_aiod_bio, &ki->kaio_task);
799 mtx_destroy(&ki->kaio_mtx);
800 uma_zfree(kaio_zone, ki);
805 * Select a job to run (called by an AIO daemon).
807 static struct aiocblist *
808 aio_selectjob(struct aiothreadlist *aiop)
810 struct aiocblist *aiocbe;
814 mtx_assert(&aio_job_mtx, MA_OWNED);
815 TAILQ_FOREACH(aiocbe, &aio_jobs, list) {
816 userp = aiocbe->userproc;
817 ki = userp->p_aioinfo;
819 if (ki->kaio_active_count < ki->kaio_maxactive_count) {
820 TAILQ_REMOVE(&aio_jobs, aiocbe, list);
821 /* Account for currently active jobs. */
822 ki->kaio_active_count++;
823 aiocbe->jobstate = JOBST_JOBRUNNING;
831 * Move all data to a permanent storage device, this code
832 * simulates fsync syscall.
835 aio_fsync_vnode(struct thread *td, struct vnode *vp)
841 vfslocked = VFS_LOCK_GIANT(vp->v_mount);
842 if ((error = vn_start_write(vp, &mp, V_WAIT | PCATCH)) != 0)
844 vn_lock(vp, LK_EXCLUSIVE | LK_RETRY);
845 if (vp->v_object != NULL) {
846 VM_OBJECT_LOCK(vp->v_object);
847 vm_object_page_clean(vp->v_object, 0, 0, 0);
848 VM_OBJECT_UNLOCK(vp->v_object);
850 error = VOP_FSYNC(vp, MNT_WAIT, td);
853 vn_finished_write(mp);
855 VFS_UNLOCK_GIANT(vfslocked);
860 * The AIO processing activity. This is the code that does the I/O request for
861 * the non-physio version of the operations. The normal vn operations are used,
862 * and this code should work in all instances for every type of file, including
863 * pipes, sockets, fifos, and regular files.
865 * XXX I don't think it works well for socket, pipe, and fifo.
868 aio_process(struct aiocblist *aiocbe)
870 struct ucred *td_savedcred;
879 int oublock_st, oublock_end;
880 int inblock_st, inblock_end;
883 td_savedcred = td->td_ucred;
884 td->td_ucred = aiocbe->cred;
885 cb = &aiocbe->uaiocb;
886 fp = aiocbe->fd_file;
888 if (cb->aio_lio_opcode == LIO_SYNC) {
891 if (fp->f_vnode != NULL)
892 error = aio_fsync_vnode(td, fp->f_vnode);
893 cb->_aiocb_private.error = error;
894 cb->_aiocb_private.status = 0;
895 td->td_ucred = td_savedcred;
899 aiov.iov_base = (void *)(uintptr_t)cb->aio_buf;
900 aiov.iov_len = cb->aio_nbytes;
902 auio.uio_iov = &aiov;
904 auio.uio_offset = cb->aio_offset;
905 auio.uio_resid = cb->aio_nbytes;
906 cnt = cb->aio_nbytes;
907 auio.uio_segflg = UIO_USERSPACE;
910 inblock_st = td->td_ru.ru_inblock;
911 oublock_st = td->td_ru.ru_oublock;
913 * aio_aqueue() acquires a reference to the file that is
914 * released in aio_free_entry().
916 if (cb->aio_lio_opcode == LIO_READ) {
917 auio.uio_rw = UIO_READ;
918 if (auio.uio_resid == 0)
921 error = fo_read(fp, &auio, fp->f_cred, FOF_OFFSET, td);
923 if (fp->f_type == DTYPE_VNODE)
925 auio.uio_rw = UIO_WRITE;
926 error = fo_write(fp, &auio, fp->f_cred, FOF_OFFSET, td);
928 inblock_end = td->td_ru.ru_inblock;
929 oublock_end = td->td_ru.ru_oublock;
931 aiocbe->inputcharge = inblock_end - inblock_st;
932 aiocbe->outputcharge = oublock_end - oublock_st;
934 if ((error) && (auio.uio_resid != cnt)) {
935 if (error == ERESTART || error == EINTR || error == EWOULDBLOCK)
937 if ((error == EPIPE) && (cb->aio_lio_opcode == LIO_WRITE)) {
939 if (fp->f_type == DTYPE_SOCKET) {
941 if (so->so_options & SO_NOSIGPIPE)
945 PROC_LOCK(aiocbe->userproc);
946 kern_psignal(aiocbe->userproc, SIGPIPE);
947 PROC_UNLOCK(aiocbe->userproc);
952 cnt -= auio.uio_resid;
953 cb->_aiocb_private.error = error;
954 cb->_aiocb_private.status = cnt;
955 td->td_ucred = td_savedcred;
959 aio_bio_done_notify(struct proc *userp, struct aiocblist *aiocbe, int type)
961 struct aioliojob *lj;
963 struct aiocblist *scb, *scbn;
966 ki = userp->p_aioinfo;
967 AIO_LOCK_ASSERT(ki, MA_OWNED);
971 lj->lioj_finished_count++;
972 if (lj->lioj_count == lj->lioj_finished_count)
975 if (type == DONE_QUEUE) {
976 aiocbe->jobflags |= AIOCBLIST_DONE;
978 aiocbe->jobflags |= AIOCBLIST_BUFDONE;
980 TAILQ_INSERT_TAIL(&ki->kaio_done, aiocbe, plist);
981 aiocbe->jobstate = JOBST_JOBFINISHED;
983 if (ki->kaio_flags & KAIO_RUNDOWN)
984 goto notification_done;
986 if (aiocbe->uaiocb.aio_sigevent.sigev_notify == SIGEV_SIGNAL ||
987 aiocbe->uaiocb.aio_sigevent.sigev_notify == SIGEV_THREAD_ID)
988 aio_sendsig(userp, &aiocbe->uaiocb.aio_sigevent, &aiocbe->ksi);
990 KNOTE_LOCKED(&aiocbe->klist, 1);
993 if (lj->lioj_signal.sigev_notify == SIGEV_KEVENT) {
994 lj->lioj_flags |= LIOJ_KEVENT_POSTED;
995 KNOTE_LOCKED(&lj->klist, 1);
997 if ((lj->lioj_flags & (LIOJ_SIGNAL|LIOJ_SIGNAL_POSTED))
999 && (lj->lioj_signal.sigev_notify == SIGEV_SIGNAL ||
1000 lj->lioj_signal.sigev_notify == SIGEV_THREAD_ID)) {
1001 aio_sendsig(userp, &lj->lioj_signal, &lj->lioj_ksi);
1002 lj->lioj_flags |= LIOJ_SIGNAL_POSTED;
1007 if (aiocbe->jobflags & AIOCBLIST_CHECKSYNC) {
1008 TAILQ_FOREACH_SAFE(scb, &ki->kaio_syncqueue, list, scbn) {
1009 if (aiocbe->fd_file == scb->fd_file &&
1010 aiocbe->seqno < scb->seqno) {
1011 if (--scb->pending == 0) {
1012 mtx_lock(&aio_job_mtx);
1013 scb->jobstate = JOBST_JOBQGLOBAL;
1014 TAILQ_REMOVE(&ki->kaio_syncqueue, scb, list);
1015 TAILQ_INSERT_TAIL(&aio_jobs, scb, list);
1016 aio_kick_nowait(userp);
1017 mtx_unlock(&aio_job_mtx);
1022 if (ki->kaio_flags & KAIO_WAKEUP) {
1023 ki->kaio_flags &= ~KAIO_WAKEUP;
1024 wakeup(&userp->p_aioinfo);
1029 * The AIO daemon, most of the actual work is done in aio_process,
1030 * but the setup (and address space mgmt) is done in this routine.
1033 aio_daemon(void *_id)
1035 struct aiocblist *aiocbe;
1036 struct aiothreadlist *aiop;
1037 struct kaioinfo *ki;
1038 struct proc *curcp, *mycp, *userp;
1039 struct vmspace *myvm, *tmpvm;
1040 struct thread *td = curthread;
1041 int id = (intptr_t)_id;
1044 * Local copies of curproc (cp) and vmspace (myvm)
1047 myvm = mycp->p_vmspace;
1049 KASSERT(mycp->p_textvp == NULL, ("kthread has a textvp"));
1052 * Allocate and ready the aio control info. There is one aiop structure
1055 aiop = uma_zalloc(aiop_zone, M_WAITOK);
1056 aiop->aiothread = td;
1057 aiop->aiothreadflags = 0;
1059 /* The daemon resides in its own pgrp. */
1060 sys_setsid(td, NULL);
1063 * Wakeup parent process. (Parent sleeps to keep from blasting away
1064 * and creating too many daemons.)
1066 sema_post(&aio_newproc_sem);
1068 mtx_lock(&aio_job_mtx);
1071 * curcp is the current daemon process context.
1072 * userp is the current user process context.
1077 * Take daemon off of free queue
1079 if (aiop->aiothreadflags & AIOP_FREE) {
1080 TAILQ_REMOVE(&aio_freeproc, aiop, list);
1081 aiop->aiothreadflags &= ~AIOP_FREE;
1087 while ((aiocbe = aio_selectjob(aiop)) != NULL) {
1088 mtx_unlock(&aio_job_mtx);
1089 userp = aiocbe->userproc;
1092 * Connect to process address space for user program.
1094 if (userp != curcp) {
1096 * Save the current address space that we are
1099 tmpvm = mycp->p_vmspace;
1102 * Point to the new user address space, and
1105 mycp->p_vmspace = userp->p_vmspace;
1106 atomic_add_int(&mycp->p_vmspace->vm_refcnt, 1);
1108 /* Activate the new mapping. */
1109 pmap_activate(FIRST_THREAD_IN_PROC(mycp));
1112 * If the old address space wasn't the daemons
1113 * own address space, then we need to remove the
1114 * daemon's reference from the other process
1115 * that it was acting on behalf of.
1117 if (tmpvm != myvm) {
1118 vmspace_free(tmpvm);
1123 ki = userp->p_aioinfo;
1125 /* Do the I/O function. */
1126 aio_process(aiocbe);
1128 mtx_lock(&aio_job_mtx);
1129 /* Decrement the active job count. */
1130 ki->kaio_active_count--;
1131 mtx_unlock(&aio_job_mtx);
1134 TAILQ_REMOVE(&ki->kaio_jobqueue, aiocbe, plist);
1135 aio_bio_done_notify(userp, aiocbe, DONE_QUEUE);
1138 mtx_lock(&aio_job_mtx);
1142 * Disconnect from user address space.
1144 if (curcp != mycp) {
1146 mtx_unlock(&aio_job_mtx);
1148 /* Get the user address space to disconnect from. */
1149 tmpvm = mycp->p_vmspace;
1151 /* Get original address space for daemon. */
1152 mycp->p_vmspace = myvm;
1154 /* Activate the daemon's address space. */
1155 pmap_activate(FIRST_THREAD_IN_PROC(mycp));
1157 if (tmpvm == myvm) {
1158 printf("AIOD: vmspace problem -- %d\n",
1162 /* Remove our vmspace reference. */
1163 vmspace_free(tmpvm);
1167 mtx_lock(&aio_job_mtx);
1169 * We have to restart to avoid race, we only sleep if
1170 * no job can be selected, that should be
1176 mtx_assert(&aio_job_mtx, MA_OWNED);
1178 TAILQ_INSERT_HEAD(&aio_freeproc, aiop, list);
1179 aiop->aiothreadflags |= AIOP_FREE;
1182 * If daemon is inactive for a long time, allow it to exit,
1183 * thereby freeing resources.
1185 if (msleep(aiop->aiothread, &aio_job_mtx, PRIBIO, "aiordy",
1187 if (TAILQ_EMPTY(&aio_jobs)) {
1188 if ((aiop->aiothreadflags & AIOP_FREE) &&
1189 (num_aio_procs > target_aio_procs)) {
1190 TAILQ_REMOVE(&aio_freeproc, aiop, list);
1192 mtx_unlock(&aio_job_mtx);
1193 uma_zfree(aiop_zone, aiop);
1194 free_unr(aiod_unr, id);
1196 if (mycp->p_vmspace->vm_refcnt <= 1) {
1197 printf("AIOD: bad vm refcnt for"
1198 " exiting daemon: %d\n",
1199 mycp->p_vmspace->vm_refcnt);
1207 mtx_unlock(&aio_job_mtx);
1208 panic("shouldn't be here\n");
1212 * Create a new AIO daemon. This is mostly a kernel-thread fork routine. The
1213 * AIO daemon modifies its environment itself.
1216 aio_newproc(int *start)
1222 id = alloc_unr(aiod_unr);
1223 error = kproc_create(aio_daemon, (void *)(intptr_t)id, &p,
1224 RFNOWAIT, 0, "aiod%d", id);
1227 * Wait until daemon is started.
1229 sema_wait(&aio_newproc_sem);
1230 mtx_lock(&aio_job_mtx);
1234 mtx_unlock(&aio_job_mtx);
1236 free_unr(aiod_unr, id);
1242 * Try the high-performance, low-overhead physio method for eligible
1243 * VCHR devices. This method doesn't use an aio helper thread, and
1244 * thus has very low overhead.
1246 * Assumes that the caller, aio_aqueue(), has incremented the file
1247 * structure's reference count, preventing its deallocation for the
1248 * duration of this call.
1251 aio_qphysio(struct proc *p, struct aiocblist *aiocbe)
1259 struct kaioinfo *ki;
1260 struct aioliojob *lj;
1263 cb = &aiocbe->uaiocb;
1264 fp = aiocbe->fd_file;
1266 if (fp->f_type != DTYPE_VNODE)
1272 * If its not a disk, we don't want to return a positive error.
1273 * It causes the aio code to not fall through to try the thread
1274 * way when you're talking to a regular file.
1276 if (!vn_isdisk(vp, &error)) {
1277 if (error == ENOTBLK)
1283 if (vp->v_bufobj.bo_bsize == 0)
1286 if (cb->aio_nbytes % vp->v_bufobj.bo_bsize)
1289 if (cb->aio_nbytes >
1290 MAXPHYS - (((vm_offset_t) cb->aio_buf) & PAGE_MASK))
1294 if (ki->kaio_buffer_count >= ki->kaio_ballowed_count)
1298 csw = devvn_refthread(vp, &dev, &ref);
1301 if (cb->aio_nbytes > dev->si_iosize_max) {
1306 /* Create and build a buffer header for a transfer. */
1307 bp = (struct buf *)getpbuf(NULL);
1312 ki->kaio_buffer_count++;
1319 * Get a copy of the kva from the physical buffer.
1323 bp->b_bcount = cb->aio_nbytes;
1324 bp->b_bufsize = cb->aio_nbytes;
1325 bp->b_iodone = aio_physwakeup;
1326 bp->b_saveaddr = bp->b_data;
1327 bp->b_data = (void *)(uintptr_t)cb->aio_buf;
1328 bp->b_offset = cb->aio_offset;
1329 bp->b_iooffset = cb->aio_offset;
1330 bp->b_blkno = btodb(cb->aio_offset);
1331 bp->b_iocmd = cb->aio_lio_opcode == LIO_WRITE ? BIO_WRITE : BIO_READ;
1334 * Bring buffer into kernel space.
1336 if (vmapbuf(bp, (dev->si_flags & SI_UNMAPPED) == 0) < 0) {
1343 bp->b_caller1 = (void *)aiocbe;
1344 TAILQ_INSERT_TAIL(&ki->kaio_bufqueue, aiocbe, plist);
1345 TAILQ_INSERT_TAIL(&ki->kaio_all, aiocbe, allist);
1346 aiocbe->jobstate = JOBST_JOBQBUF;
1347 cb->_aiocb_private.status = cb->aio_nbytes;
1350 atomic_add_int(&num_queue_count, 1);
1351 atomic_add_int(&num_buf_aio, 1);
1355 TASK_INIT(&aiocbe->biotask, 0, biohelper, aiocbe);
1357 /* Perform transfer. */
1358 dev_strategy_csw(dev, csw, bp);
1359 dev_relthread(dev, ref);
1365 ki->kaio_buffer_count--;
1372 dev_relthread(dev, ref);
1377 * Wake up aio requests that may be serviceable now.
1380 aio_swake_cb(struct socket *so, struct sockbuf *sb)
1382 struct aiocblist *cb, *cbn;
1385 SOCKBUF_LOCK_ASSERT(sb);
1386 if (sb == &so->so_snd)
1391 sb->sb_flags &= ~SB_AIO;
1392 mtx_lock(&aio_job_mtx);
1393 TAILQ_FOREACH_SAFE(cb, &so->so_aiojobq, list, cbn) {
1394 if (opcode == cb->uaiocb.aio_lio_opcode) {
1395 if (cb->jobstate != JOBST_JOBQSOCK)
1396 panic("invalid queue value");
1398 * We don't have actual sockets backend yet,
1399 * so we simply move the requests to the generic
1402 TAILQ_REMOVE(&so->so_aiojobq, cb, list);
1403 TAILQ_INSERT_TAIL(&aio_jobs, cb, list);
1404 aio_kick_nowait(cb->userproc);
1407 mtx_unlock(&aio_job_mtx);
1411 convert_old_sigevent(struct osigevent *osig, struct sigevent *nsig)
1415 * Only SIGEV_NONE, SIGEV_SIGNAL, and SIGEV_KEVENT are
1416 * supported by AIO with the old sigevent structure.
1418 nsig->sigev_notify = osig->sigev_notify;
1419 switch (nsig->sigev_notify) {
1423 nsig->sigev_signo = osig->__sigev_u.__sigev_signo;
1426 nsig->sigev_notify_kqueue =
1427 osig->__sigev_u.__sigev_notify_kqueue;
1428 nsig->sigev_value.sival_ptr = osig->sigev_value.sival_ptr;
1437 aiocb_copyin_old_sigevent(struct aiocb *ujob, struct aiocb *kjob)
1439 struct oaiocb *ojob;
1442 bzero(kjob, sizeof(struct aiocb));
1443 error = copyin(ujob, kjob, sizeof(struct oaiocb));
1446 ojob = (struct oaiocb *)kjob;
1447 return (convert_old_sigevent(&ojob->aio_sigevent, &kjob->aio_sigevent));
1451 aiocb_copyin(struct aiocb *ujob, struct aiocb *kjob)
1454 return (copyin(ujob, kjob, sizeof(struct aiocb)));
1458 aiocb_fetch_status(struct aiocb *ujob)
1461 return (fuword(&ujob->_aiocb_private.status));
1465 aiocb_fetch_error(struct aiocb *ujob)
1468 return (fuword(&ujob->_aiocb_private.error));
1472 aiocb_store_status(struct aiocb *ujob, long status)
1475 return (suword(&ujob->_aiocb_private.status, status));
1479 aiocb_store_error(struct aiocb *ujob, long error)
1482 return (suword(&ujob->_aiocb_private.error, error));
1486 aiocb_store_kernelinfo(struct aiocb *ujob, long jobref)
1489 return (suword(&ujob->_aiocb_private.kernelinfo, jobref));
1493 aiocb_store_aiocb(struct aiocb **ujobp, struct aiocb *ujob)
1496 return (suword(ujobp, (long)ujob));
1499 static struct aiocb_ops aiocb_ops = {
1500 .copyin = aiocb_copyin,
1501 .fetch_status = aiocb_fetch_status,
1502 .fetch_error = aiocb_fetch_error,
1503 .store_status = aiocb_store_status,
1504 .store_error = aiocb_store_error,
1505 .store_kernelinfo = aiocb_store_kernelinfo,
1506 .store_aiocb = aiocb_store_aiocb,
1509 static struct aiocb_ops aiocb_ops_osigevent = {
1510 .copyin = aiocb_copyin_old_sigevent,
1511 .fetch_status = aiocb_fetch_status,
1512 .fetch_error = aiocb_fetch_error,
1513 .store_status = aiocb_store_status,
1514 .store_error = aiocb_store_error,
1515 .store_kernelinfo = aiocb_store_kernelinfo,
1516 .store_aiocb = aiocb_store_aiocb,
1520 * Queue a new AIO request. Choosing either the threaded or direct physio VCHR
1521 * technique is done in this code.
1524 aio_aqueue(struct thread *td, struct aiocb *job, struct aioliojob *lj,
1525 int type, struct aiocb_ops *ops)
1527 struct proc *p = td->td_proc;
1530 struct aiocblist *aiocbe, *cb;
1531 struct kaioinfo *ki;
1540 if (p->p_aioinfo == NULL)
1541 aio_init_aioinfo(p);
1545 ops->store_status(job, -1);
1546 ops->store_error(job, 0);
1547 ops->store_kernelinfo(job, -1);
1549 if (num_queue_count >= max_queue_count ||
1550 ki->kaio_count >= ki->kaio_qallowed_count) {
1551 ops->store_error(job, EAGAIN);
1555 aiocbe = uma_zalloc(aiocb_zone, M_WAITOK | M_ZERO);
1556 aiocbe->inputcharge = 0;
1557 aiocbe->outputcharge = 0;
1558 knlist_init_mtx(&aiocbe->klist, AIO_MTX(ki));
1560 error = ops->copyin(job, &aiocbe->uaiocb);
1562 ops->store_error(job, error);
1563 uma_zfree(aiocb_zone, aiocbe);
1567 if (aiocbe->uaiocb.aio_sigevent.sigev_notify != SIGEV_KEVENT &&
1568 aiocbe->uaiocb.aio_sigevent.sigev_notify != SIGEV_SIGNAL &&
1569 aiocbe->uaiocb.aio_sigevent.sigev_notify != SIGEV_THREAD_ID &&
1570 aiocbe->uaiocb.aio_sigevent.sigev_notify != SIGEV_NONE) {
1571 ops->store_error(job, EINVAL);
1572 uma_zfree(aiocb_zone, aiocbe);
1576 if ((aiocbe->uaiocb.aio_sigevent.sigev_notify == SIGEV_SIGNAL ||
1577 aiocbe->uaiocb.aio_sigevent.sigev_notify == SIGEV_THREAD_ID) &&
1578 !_SIG_VALID(aiocbe->uaiocb.aio_sigevent.sigev_signo)) {
1579 uma_zfree(aiocb_zone, aiocbe);
1583 ksiginfo_init(&aiocbe->ksi);
1585 /* Save userspace address of the job info. */
1586 aiocbe->uuaiocb = job;
1588 /* Get the opcode. */
1589 if (type != LIO_NOP)
1590 aiocbe->uaiocb.aio_lio_opcode = type;
1591 opcode = aiocbe->uaiocb.aio_lio_opcode;
1594 * Validate the opcode and fetch the file object for the specified
1597 * XXXRW: Moved the opcode validation up here so that we don't
1598 * retrieve a file descriptor without knowing what the capabiltity
1601 fd = aiocbe->uaiocb.aio_fildes;
1604 error = fget_write(td, fd, CAP_WRITE | CAP_SEEK, &fp);
1607 error = fget_read(td, fd, CAP_READ | CAP_SEEK, &fp);
1610 error = fget(td, fd, CAP_FSYNC, &fp);
1613 error = fget(td, fd, 0, &fp);
1619 uma_zfree(aiocb_zone, aiocbe);
1620 ops->store_error(job, error);
1624 if (opcode == LIO_SYNC && fp->f_vnode == NULL) {
1629 if (opcode != LIO_SYNC && aiocbe->uaiocb.aio_offset == -1LL) {
1634 aiocbe->fd_file = fp;
1636 mtx_lock(&aio_job_mtx);
1638 aiocbe->seqno = jobseqno++;
1639 mtx_unlock(&aio_job_mtx);
1640 error = ops->store_kernelinfo(job, jid);
1645 aiocbe->uaiocb._aiocb_private.kernelinfo = (void *)(intptr_t)jid;
1647 if (opcode == LIO_NOP) {
1649 uma_zfree(aiocb_zone, aiocbe);
1653 if (aiocbe->uaiocb.aio_sigevent.sigev_notify != SIGEV_KEVENT)
1655 evflags = aiocbe->uaiocb.aio_sigevent.sigev_notify_kevent_flags;
1656 if ((evflags & ~(EV_CLEAR | EV_DISPATCH | EV_ONESHOT)) != 0) {
1660 kqfd = aiocbe->uaiocb.aio_sigevent.sigev_notify_kqueue;
1661 kev.ident = (uintptr_t)aiocbe->uuaiocb;
1662 kev.filter = EVFILT_AIO;
1663 kev.flags = EV_ADD | EV_ENABLE | EV_FLAG1 | evflags;
1664 kev.data = (intptr_t)aiocbe;
1665 kev.udata = aiocbe->uaiocb.aio_sigevent.sigev_value.sival_ptr;
1666 error = kqfd_register(kqfd, &kev, td, 1);
1670 uma_zfree(aiocb_zone, aiocbe);
1671 ops->store_error(job, error);
1676 ops->store_error(job, EINPROGRESS);
1677 aiocbe->uaiocb._aiocb_private.error = EINPROGRESS;
1678 aiocbe->userproc = p;
1679 aiocbe->cred = crhold(td->td_ucred);
1680 aiocbe->jobflags = 0;
1683 if (opcode == LIO_SYNC)
1686 if (fp->f_type == DTYPE_SOCKET) {
1688 * Alternate queueing for socket ops: Reach down into the
1689 * descriptor to get the socket data. Then check to see if the
1690 * socket is ready to be read or written (based on the requested
1693 * If it is not ready for io, then queue the aiocbe on the
1694 * socket, and set the flags so we get a call when sbnotify()
1697 * Note if opcode is neither LIO_WRITE nor LIO_READ we lock
1698 * and unlock the snd sockbuf for no reason.
1701 sb = (opcode == LIO_READ) ? &so->so_rcv : &so->so_snd;
1703 if (((opcode == LIO_READ) && (!soreadable(so))) || ((opcode ==
1704 LIO_WRITE) && (!sowriteable(so)))) {
1705 sb->sb_flags |= SB_AIO;
1707 mtx_lock(&aio_job_mtx);
1708 TAILQ_INSERT_TAIL(&so->so_aiojobq, aiocbe, list);
1709 mtx_unlock(&aio_job_mtx);
1712 TAILQ_INSERT_TAIL(&ki->kaio_all, aiocbe, allist);
1713 TAILQ_INSERT_TAIL(&ki->kaio_jobqueue, aiocbe, plist);
1714 aiocbe->jobstate = JOBST_JOBQSOCK;
1720 atomic_add_int(&num_queue_count, 1);
1727 if ((error = aio_qphysio(p, aiocbe)) == 0)
1731 aiocbe->uaiocb._aiocb_private.error = error;
1732 ops->store_error(job, error);
1737 /* No buffer for daemon I/O. */
1739 atomic_add_int(&num_queue_count, 1);
1745 TAILQ_INSERT_TAIL(&ki->kaio_jobqueue, aiocbe, plist);
1746 TAILQ_INSERT_TAIL(&ki->kaio_all, aiocbe, allist);
1747 if (opcode == LIO_SYNC) {
1748 TAILQ_FOREACH(cb, &ki->kaio_jobqueue, plist) {
1749 if (cb->fd_file == aiocbe->fd_file &&
1750 cb->uaiocb.aio_lio_opcode != LIO_SYNC &&
1751 cb->seqno < aiocbe->seqno) {
1752 cb->jobflags |= AIOCBLIST_CHECKSYNC;
1756 TAILQ_FOREACH(cb, &ki->kaio_bufqueue, plist) {
1757 if (cb->fd_file == aiocbe->fd_file &&
1758 cb->uaiocb.aio_lio_opcode != LIO_SYNC &&
1759 cb->seqno < aiocbe->seqno) {
1760 cb->jobflags |= AIOCBLIST_CHECKSYNC;
1764 if (aiocbe->pending != 0) {
1765 TAILQ_INSERT_TAIL(&ki->kaio_syncqueue, aiocbe, list);
1766 aiocbe->jobstate = JOBST_JOBQSYNC;
1771 mtx_lock(&aio_job_mtx);
1772 TAILQ_INSERT_TAIL(&aio_jobs, aiocbe, list);
1773 aiocbe->jobstate = JOBST_JOBQGLOBAL;
1775 mtx_unlock(&aio_job_mtx);
1783 aio_kick_nowait(struct proc *userp)
1785 struct kaioinfo *ki = userp->p_aioinfo;
1786 struct aiothreadlist *aiop;
1788 mtx_assert(&aio_job_mtx, MA_OWNED);
1789 if ((aiop = TAILQ_FIRST(&aio_freeproc)) != NULL) {
1790 TAILQ_REMOVE(&aio_freeproc, aiop, list);
1791 aiop->aiothreadflags &= ~AIOP_FREE;
1792 wakeup(aiop->aiothread);
1793 } else if (((num_aio_resv_start + num_aio_procs) < max_aio_procs) &&
1794 ((ki->kaio_active_count + num_aio_resv_start) <
1795 ki->kaio_maxactive_count)) {
1796 taskqueue_enqueue(taskqueue_aiod_bio, &ki->kaio_task);
1801 aio_kick(struct proc *userp)
1803 struct kaioinfo *ki = userp->p_aioinfo;
1804 struct aiothreadlist *aiop;
1807 mtx_assert(&aio_job_mtx, MA_OWNED);
1809 if ((aiop = TAILQ_FIRST(&aio_freeproc)) != NULL) {
1810 TAILQ_REMOVE(&aio_freeproc, aiop, list);
1811 aiop->aiothreadflags &= ~AIOP_FREE;
1812 wakeup(aiop->aiothread);
1813 } else if (((num_aio_resv_start + num_aio_procs) < max_aio_procs) &&
1814 ((ki->kaio_active_count + num_aio_resv_start) <
1815 ki->kaio_maxactive_count)) {
1816 num_aio_resv_start++;
1817 mtx_unlock(&aio_job_mtx);
1818 error = aio_newproc(&num_aio_resv_start);
1819 mtx_lock(&aio_job_mtx);
1821 num_aio_resv_start--;
1831 aio_kick_helper(void *context, int pending)
1833 struct proc *userp = context;
1835 mtx_lock(&aio_job_mtx);
1836 while (--pending >= 0) {
1837 if (aio_kick(userp))
1840 mtx_unlock(&aio_job_mtx);
1844 * Support the aio_return system call, as a side-effect, kernel resources are
1848 kern_aio_return(struct thread *td, struct aiocb *uaiocb, struct aiocb_ops *ops)
1850 struct proc *p = td->td_proc;
1851 struct aiocblist *cb;
1852 struct kaioinfo *ki;
1859 TAILQ_FOREACH(cb, &ki->kaio_done, plist) {
1860 if (cb->uuaiocb == uaiocb)
1864 MPASS(cb->jobstate == JOBST_JOBFINISHED);
1865 status = cb->uaiocb._aiocb_private.status;
1866 error = cb->uaiocb._aiocb_private.error;
1867 td->td_retval[0] = status;
1868 if (cb->uaiocb.aio_lio_opcode == LIO_WRITE) {
1869 td->td_ru.ru_oublock += cb->outputcharge;
1870 cb->outputcharge = 0;
1871 } else if (cb->uaiocb.aio_lio_opcode == LIO_READ) {
1872 td->td_ru.ru_inblock += cb->inputcharge;
1873 cb->inputcharge = 0;
1877 ops->store_error(uaiocb, error);
1878 ops->store_status(uaiocb, status);
1887 sys_aio_return(struct thread *td, struct aio_return_args *uap)
1890 return (kern_aio_return(td, uap->aiocbp, &aiocb_ops));
1894 * Allow a process to wakeup when any of the I/O requests are completed.
1897 kern_aio_suspend(struct thread *td, int njoblist, struct aiocb **ujoblist,
1898 struct timespec *ts)
1900 struct proc *p = td->td_proc;
1902 struct kaioinfo *ki;
1903 struct aiocblist *cb, *cbfirst;
1908 if (ts->tv_nsec < 0 || ts->tv_nsec >= 1000000000)
1911 TIMESPEC_TO_TIMEVAL(&atv, ts);
1912 if (itimerfix(&atv))
1914 timo = tvtohz(&atv);
1928 TAILQ_FOREACH(cb, &ki->kaio_all, allist) {
1929 for (i = 0; i < njoblist; i++) {
1930 if (cb->uuaiocb == ujoblist[i]) {
1931 if (cbfirst == NULL)
1933 if (cb->jobstate == JOBST_JOBFINISHED)
1938 /* All tasks were finished. */
1939 if (cbfirst == NULL)
1942 ki->kaio_flags |= KAIO_WAKEUP;
1943 error = msleep(&p->p_aioinfo, AIO_MTX(ki), PRIBIO | PCATCH,
1945 if (error == ERESTART)
1956 sys_aio_suspend(struct thread *td, struct aio_suspend_args *uap)
1958 struct timespec ts, *tsp;
1959 struct aiocb **ujoblist;
1962 if (uap->nent < 0 || uap->nent > AIO_LISTIO_MAX)
1966 /* Get timespec struct. */
1967 if ((error = copyin(uap->timeout, &ts, sizeof(ts))) != 0)
1973 ujoblist = uma_zalloc(aiol_zone, M_WAITOK);
1974 error = copyin(uap->aiocbp, ujoblist, uap->nent * sizeof(ujoblist[0]));
1976 error = kern_aio_suspend(td, uap->nent, ujoblist, tsp);
1977 uma_zfree(aiol_zone, ujoblist);
1982 * aio_cancel cancels any non-physio aio operations not currently in
1986 sys_aio_cancel(struct thread *td, struct aio_cancel_args *uap)
1988 struct proc *p = td->td_proc;
1989 struct kaioinfo *ki;
1990 struct aiocblist *cbe, *cbn;
1996 int notcancelled = 0;
1999 /* Lookup file object. */
2000 error = fget(td, uap->fd, 0, &fp);
2008 if (fp->f_type == DTYPE_VNODE) {
2010 if (vn_isdisk(vp, &error)) {
2012 td->td_retval[0] = AIO_NOTCANCELED;
2018 TAILQ_FOREACH_SAFE(cbe, &ki->kaio_jobqueue, plist, cbn) {
2019 if ((uap->fd == cbe->uaiocb.aio_fildes) &&
2020 ((uap->aiocbp == NULL) ||
2021 (uap->aiocbp == cbe->uuaiocb))) {
2024 mtx_lock(&aio_job_mtx);
2025 if (cbe->jobstate == JOBST_JOBQGLOBAL) {
2026 TAILQ_REMOVE(&aio_jobs, cbe, list);
2028 } else if (cbe->jobstate == JOBST_JOBQSOCK) {
2029 MPASS(fp->f_type == DTYPE_SOCKET);
2031 TAILQ_REMOVE(&so->so_aiojobq, cbe, list);
2033 } else if (cbe->jobstate == JOBST_JOBQSYNC) {
2034 TAILQ_REMOVE(&ki->kaio_syncqueue, cbe, list);
2037 mtx_unlock(&aio_job_mtx);
2040 TAILQ_REMOVE(&ki->kaio_jobqueue, cbe, plist);
2041 cbe->uaiocb._aiocb_private.status = -1;
2042 cbe->uaiocb._aiocb_private.error = ECANCELED;
2043 aio_bio_done_notify(p, cbe, DONE_QUEUE);
2048 if (uap->aiocbp != NULL)
2057 if (uap->aiocbp != NULL) {
2059 td->td_retval[0] = AIO_CANCELED;
2065 td->td_retval[0] = AIO_NOTCANCELED;
2070 td->td_retval[0] = AIO_CANCELED;
2074 td->td_retval[0] = AIO_ALLDONE;
2080 * aio_error is implemented in the kernel level for compatibility purposes
2081 * only. For a user mode async implementation, it would be best to do it in
2082 * a userland subroutine.
2085 kern_aio_error(struct thread *td, struct aiocb *aiocbp, struct aiocb_ops *ops)
2087 struct proc *p = td->td_proc;
2088 struct aiocblist *cb;
2089 struct kaioinfo *ki;
2094 td->td_retval[0] = EINVAL;
2099 TAILQ_FOREACH(cb, &ki->kaio_all, allist) {
2100 if (cb->uuaiocb == aiocbp) {
2101 if (cb->jobstate == JOBST_JOBFINISHED)
2103 cb->uaiocb._aiocb_private.error;
2105 td->td_retval[0] = EINPROGRESS;
2113 * Hack for failure of aio_aqueue.
2115 status = ops->fetch_status(aiocbp);
2117 td->td_retval[0] = ops->fetch_error(aiocbp);
2121 td->td_retval[0] = EINVAL;
2126 sys_aio_error(struct thread *td, struct aio_error_args *uap)
2129 return (kern_aio_error(td, uap->aiocbp, &aiocb_ops));
2132 /* syscall - asynchronous read from a file (REALTIME) */
2134 sys_oaio_read(struct thread *td, struct oaio_read_args *uap)
2137 return (aio_aqueue(td, (struct aiocb *)uap->aiocbp, NULL, LIO_READ,
2138 &aiocb_ops_osigevent));
2142 sys_aio_read(struct thread *td, struct aio_read_args *uap)
2145 return (aio_aqueue(td, uap->aiocbp, NULL, LIO_READ, &aiocb_ops));
2148 /* syscall - asynchronous write to a file (REALTIME) */
2150 sys_oaio_write(struct thread *td, struct oaio_write_args *uap)
2153 return (aio_aqueue(td, (struct aiocb *)uap->aiocbp, NULL, LIO_WRITE,
2154 &aiocb_ops_osigevent));
2158 sys_aio_write(struct thread *td, struct aio_write_args *uap)
2161 return (aio_aqueue(td, uap->aiocbp, NULL, LIO_WRITE, &aiocb_ops));
2165 kern_lio_listio(struct thread *td, int mode, struct aiocb * const *uacb_list,
2166 struct aiocb **acb_list, int nent, struct sigevent *sig,
2167 struct aiocb_ops *ops)
2169 struct proc *p = td->td_proc;
2171 struct kaioinfo *ki;
2172 struct aioliojob *lj;
2178 if ((mode != LIO_NOWAIT) && (mode != LIO_WAIT))
2181 if (nent < 0 || nent > AIO_LISTIO_MAX)
2184 if (p->p_aioinfo == NULL)
2185 aio_init_aioinfo(p);
2189 lj = uma_zalloc(aiolio_zone, M_WAITOK);
2192 lj->lioj_finished_count = 0;
2193 knlist_init_mtx(&lj->klist, AIO_MTX(ki));
2194 ksiginfo_init(&lj->lioj_ksi);
2199 if (sig && (mode == LIO_NOWAIT)) {
2200 bcopy(sig, &lj->lioj_signal, sizeof(lj->lioj_signal));
2201 if (lj->lioj_signal.sigev_notify == SIGEV_KEVENT) {
2202 /* Assume only new style KEVENT */
2203 kev.filter = EVFILT_LIO;
2204 kev.flags = EV_ADD | EV_ENABLE | EV_FLAG1;
2205 kev.ident = (uintptr_t)uacb_list; /* something unique */
2206 kev.data = (intptr_t)lj;
2207 /* pass user defined sigval data */
2208 kev.udata = lj->lioj_signal.sigev_value.sival_ptr;
2209 error = kqfd_register(
2210 lj->lioj_signal.sigev_notify_kqueue, &kev, td, 1);
2212 uma_zfree(aiolio_zone, lj);
2215 } else if (lj->lioj_signal.sigev_notify == SIGEV_NONE) {
2217 } else if (lj->lioj_signal.sigev_notify == SIGEV_SIGNAL ||
2218 lj->lioj_signal.sigev_notify == SIGEV_THREAD_ID) {
2219 if (!_SIG_VALID(lj->lioj_signal.sigev_signo)) {
2220 uma_zfree(aiolio_zone, lj);
2223 lj->lioj_flags |= LIOJ_SIGNAL;
2225 uma_zfree(aiolio_zone, lj);
2231 TAILQ_INSERT_TAIL(&ki->kaio_liojoblist, lj, lioj_list);
2233 * Add extra aiocb count to avoid the lio to be freed
2234 * by other threads doing aio_waitcomplete or aio_return,
2235 * and prevent event from being sent until we have queued
2242 * Get pointers to the list of I/O requests.
2245 for (i = 0; i < nent; i++) {
2248 error = aio_aqueue(td, iocb, lj, LIO_NOP, ops);
2256 if (mode == LIO_WAIT) {
2257 while (lj->lioj_count - 1 != lj->lioj_finished_count) {
2258 ki->kaio_flags |= KAIO_WAKEUP;
2259 error = msleep(&p->p_aioinfo, AIO_MTX(ki),
2260 PRIBIO | PCATCH, "aiospn", 0);
2261 if (error == ERESTART)
2267 if (lj->lioj_count - 1 == lj->lioj_finished_count) {
2268 if (lj->lioj_signal.sigev_notify == SIGEV_KEVENT) {
2269 lj->lioj_flags |= LIOJ_KEVENT_POSTED;
2270 KNOTE_LOCKED(&lj->klist, 1);
2272 if ((lj->lioj_flags & (LIOJ_SIGNAL|LIOJ_SIGNAL_POSTED))
2274 && (lj->lioj_signal.sigev_notify == SIGEV_SIGNAL ||
2275 lj->lioj_signal.sigev_notify == SIGEV_THREAD_ID)) {
2276 aio_sendsig(p, &lj->lioj_signal,
2278 lj->lioj_flags |= LIOJ_SIGNAL_POSTED;
2283 if (lj->lioj_count == 0) {
2284 TAILQ_REMOVE(&ki->kaio_liojoblist, lj, lioj_list);
2285 knlist_delete(&lj->klist, curthread, 1);
2287 sigqueue_take(&lj->lioj_ksi);
2290 uma_zfree(aiolio_zone, lj);
2299 /* syscall - list directed I/O (REALTIME) */
2301 sys_olio_listio(struct thread *td, struct olio_listio_args *uap)
2303 struct aiocb **acb_list;
2304 struct sigevent *sigp, sig;
2305 struct osigevent osig;
2308 if ((uap->mode != LIO_NOWAIT) && (uap->mode != LIO_WAIT))
2312 if (nent < 0 || nent > AIO_LISTIO_MAX)
2315 if (uap->sig && (uap->mode == LIO_NOWAIT)) {
2316 error = copyin(uap->sig, &osig, sizeof(osig));
2319 error = convert_old_sigevent(&osig, &sig);
2326 acb_list = malloc(sizeof(struct aiocb *) * nent, M_LIO, M_WAITOK);
2327 error = copyin(uap->acb_list, acb_list, nent * sizeof(acb_list[0]));
2329 error = kern_lio_listio(td, uap->mode,
2330 (struct aiocb * const *)uap->acb_list, acb_list, nent, sigp,
2331 &aiocb_ops_osigevent);
2332 free(acb_list, M_LIO);
2336 /* syscall - list directed I/O (REALTIME) */
2338 sys_lio_listio(struct thread *td, struct lio_listio_args *uap)
2340 struct aiocb **acb_list;
2341 struct sigevent *sigp, sig;
2344 if ((uap->mode != LIO_NOWAIT) && (uap->mode != LIO_WAIT))
2348 if (nent < 0 || nent > AIO_LISTIO_MAX)
2351 if (uap->sig && (uap->mode == LIO_NOWAIT)) {
2352 error = copyin(uap->sig, &sig, sizeof(sig));
2359 acb_list = malloc(sizeof(struct aiocb *) * nent, M_LIO, M_WAITOK);
2360 error = copyin(uap->acb_list, acb_list, nent * sizeof(acb_list[0]));
2362 error = kern_lio_listio(td, uap->mode, uap->acb_list, acb_list,
2363 nent, sigp, &aiocb_ops);
2364 free(acb_list, M_LIO);
2369 * Called from interrupt thread for physio, we should return as fast
2370 * as possible, so we schedule a biohelper task.
2373 aio_physwakeup(struct buf *bp)
2375 struct aiocblist *aiocbe;
2377 aiocbe = (struct aiocblist *)bp->b_caller1;
2378 taskqueue_enqueue(taskqueue_aiod_bio, &aiocbe->biotask);
2382 * Task routine to perform heavy tasks, process wakeup, and signals.
2385 biohelper(void *context, int pending)
2387 struct aiocblist *aiocbe = context;
2390 struct kaioinfo *ki;
2394 userp = aiocbe->userproc;
2395 ki = userp->p_aioinfo;
2397 aiocbe->uaiocb._aiocb_private.status -= bp->b_resid;
2398 aiocbe->uaiocb._aiocb_private.error = 0;
2399 if (bp->b_ioflags & BIO_ERROR)
2400 aiocbe->uaiocb._aiocb_private.error = bp->b_error;
2401 nblks = btodb(aiocbe->uaiocb.aio_nbytes);
2402 if (aiocbe->uaiocb.aio_lio_opcode == LIO_WRITE)
2403 aiocbe->outputcharge += nblks;
2405 aiocbe->inputcharge += nblks;
2407 TAILQ_REMOVE(&userp->p_aioinfo->kaio_bufqueue, aiocbe, plist);
2408 ki->kaio_buffer_count--;
2409 aio_bio_done_notify(userp, aiocbe, DONE_BUF);
2412 /* Release mapping into kernel space. */
2415 atomic_subtract_int(&num_buf_aio, 1);
2418 /* syscall - wait for the next completion of an aio request */
2420 kern_aio_waitcomplete(struct thread *td, struct aiocb **aiocbp,
2421 struct timespec *ts, struct aiocb_ops *ops)
2423 struct proc *p = td->td_proc;
2425 struct kaioinfo *ki;
2426 struct aiocblist *cb;
2427 struct aiocb *uuaiocb;
2428 int error, status, timo;
2430 ops->store_aiocb(aiocbp, NULL);
2434 if ((ts->tv_nsec < 0) || (ts->tv_nsec >= 1000000000))
2437 TIMESPEC_TO_TIMEVAL(&atv, ts);
2438 if (itimerfix(&atv))
2440 timo = tvtohz(&atv);
2443 if (p->p_aioinfo == NULL)
2444 aio_init_aioinfo(p);
2450 while ((cb = TAILQ_FIRST(&ki->kaio_done)) == NULL) {
2451 ki->kaio_flags |= KAIO_WAKEUP;
2452 error = msleep(&p->p_aioinfo, AIO_MTX(ki), PRIBIO | PCATCH,
2454 if (timo && error == ERESTART)
2461 MPASS(cb->jobstate == JOBST_JOBFINISHED);
2462 uuaiocb = cb->uuaiocb;
2463 status = cb->uaiocb._aiocb_private.status;
2464 error = cb->uaiocb._aiocb_private.error;
2465 td->td_retval[0] = status;
2466 if (cb->uaiocb.aio_lio_opcode == LIO_WRITE) {
2467 td->td_ru.ru_oublock += cb->outputcharge;
2468 cb->outputcharge = 0;
2469 } else if (cb->uaiocb.aio_lio_opcode == LIO_READ) {
2470 td->td_ru.ru_inblock += cb->inputcharge;
2471 cb->inputcharge = 0;
2475 ops->store_aiocb(aiocbp, uuaiocb);
2476 ops->store_error(uuaiocb, error);
2477 ops->store_status(uuaiocb, status);
2485 sys_aio_waitcomplete(struct thread *td, struct aio_waitcomplete_args *uap)
2487 struct timespec ts, *tsp;
2491 /* Get timespec struct. */
2492 error = copyin(uap->timeout, &ts, sizeof(ts));
2499 return (kern_aio_waitcomplete(td, uap->aiocbp, tsp, &aiocb_ops));
2503 kern_aio_fsync(struct thread *td, int op, struct aiocb *aiocbp,
2504 struct aiocb_ops *ops)
2506 struct proc *p = td->td_proc;
2507 struct kaioinfo *ki;
2509 if (op != O_SYNC) /* XXX lack of O_DSYNC */
2513 aio_init_aioinfo(p);
2514 return (aio_aqueue(td, aiocbp, NULL, LIO_SYNC, ops));
2518 sys_aio_fsync(struct thread *td, struct aio_fsync_args *uap)
2521 return (kern_aio_fsync(td, uap->op, uap->aiocbp, &aiocb_ops));
2524 /* kqueue attach function */
2526 filt_aioattach(struct knote *kn)
2528 struct aiocblist *aiocbe = (struct aiocblist *)kn->kn_sdata;
2531 * The aiocbe pointer must be validated before using it, so
2532 * registration is restricted to the kernel; the user cannot
2535 if ((kn->kn_flags & EV_FLAG1) == 0)
2537 kn->kn_ptr.p_aio = aiocbe;
2538 kn->kn_flags &= ~EV_FLAG1;
2540 knlist_add(&aiocbe->klist, kn, 0);
2545 /* kqueue detach function */
2547 filt_aiodetach(struct knote *kn)
2551 knl = &kn->kn_ptr.p_aio->klist;
2552 knl->kl_lock(knl->kl_lockarg);
2553 if (!knlist_empty(knl))
2554 knlist_remove(knl, kn, 1);
2555 knl->kl_unlock(knl->kl_lockarg);
2558 /* kqueue filter function */
2561 filt_aio(struct knote *kn, long hint)
2563 struct aiocblist *aiocbe = kn->kn_ptr.p_aio;
2565 kn->kn_data = aiocbe->uaiocb._aiocb_private.error;
2566 if (aiocbe->jobstate != JOBST_JOBFINISHED)
2568 kn->kn_flags |= EV_EOF;
2572 /* kqueue attach function */
2574 filt_lioattach(struct knote *kn)
2576 struct aioliojob * lj = (struct aioliojob *)kn->kn_sdata;
2579 * The aioliojob pointer must be validated before using it, so
2580 * registration is restricted to the kernel; the user cannot
2583 if ((kn->kn_flags & EV_FLAG1) == 0)
2585 kn->kn_ptr.p_lio = lj;
2586 kn->kn_flags &= ~EV_FLAG1;
2588 knlist_add(&lj->klist, kn, 0);
2593 /* kqueue detach function */
2595 filt_liodetach(struct knote *kn)
2599 knl = &kn->kn_ptr.p_lio->klist;
2600 knl->kl_lock(knl->kl_lockarg);
2601 if (!knlist_empty(knl))
2602 knlist_remove(knl, kn, 1);
2603 knl->kl_unlock(knl->kl_lockarg);
2606 /* kqueue filter function */
2609 filt_lio(struct knote *kn, long hint)
2611 struct aioliojob * lj = kn->kn_ptr.p_lio;
2613 return (lj->lioj_flags & LIOJ_KEVENT_POSTED);
2616 #ifdef COMPAT_FREEBSD32
2618 struct __aiocb_private32 {
2621 uint32_t kernelinfo;
2624 typedef struct oaiocb32 {
2625 int aio_fildes; /* File descriptor */
2626 uint64_t aio_offset __packed; /* File offset for I/O */
2627 uint32_t aio_buf; /* I/O buffer in process space */
2628 uint32_t aio_nbytes; /* Number of bytes for I/O */
2629 struct osigevent32 aio_sigevent; /* Signal to deliver */
2630 int aio_lio_opcode; /* LIO opcode */
2631 int aio_reqprio; /* Request priority -- ignored */
2632 struct __aiocb_private32 _aiocb_private;
2635 typedef struct aiocb32 {
2636 int32_t aio_fildes; /* File descriptor */
2637 uint64_t aio_offset __packed; /* File offset for I/O */
2638 uint32_t aio_buf; /* I/O buffer in process space */
2639 uint32_t aio_nbytes; /* Number of bytes for I/O */
2641 uint32_t __spare2__;
2642 int aio_lio_opcode; /* LIO opcode */
2643 int aio_reqprio; /* Request priority -- ignored */
2644 struct __aiocb_private32 _aiocb_private;
2645 struct sigevent32 aio_sigevent; /* Signal to deliver */
2649 convert_old_sigevent32(struct osigevent32 *osig, struct sigevent *nsig)
2653 * Only SIGEV_NONE, SIGEV_SIGNAL, and SIGEV_KEVENT are
2654 * supported by AIO with the old sigevent structure.
2656 CP(*osig, *nsig, sigev_notify);
2657 switch (nsig->sigev_notify) {
2661 nsig->sigev_signo = osig->__sigev_u.__sigev_signo;
2664 nsig->sigev_notify_kqueue =
2665 osig->__sigev_u.__sigev_notify_kqueue;
2666 PTRIN_CP(*osig, *nsig, sigev_value.sival_ptr);
2675 aiocb32_copyin_old_sigevent(struct aiocb *ujob, struct aiocb *kjob)
2677 struct oaiocb32 job32;
2680 bzero(kjob, sizeof(struct aiocb));
2681 error = copyin(ujob, &job32, sizeof(job32));
2685 CP(job32, *kjob, aio_fildes);
2686 CP(job32, *kjob, aio_offset);
2687 PTRIN_CP(job32, *kjob, aio_buf);
2688 CP(job32, *kjob, aio_nbytes);
2689 CP(job32, *kjob, aio_lio_opcode);
2690 CP(job32, *kjob, aio_reqprio);
2691 CP(job32, *kjob, _aiocb_private.status);
2692 CP(job32, *kjob, _aiocb_private.error);
2693 PTRIN_CP(job32, *kjob, _aiocb_private.kernelinfo);
2694 return (convert_old_sigevent32(&job32.aio_sigevent,
2695 &kjob->aio_sigevent));
2699 aiocb32_copyin(struct aiocb *ujob, struct aiocb *kjob)
2701 struct aiocb32 job32;
2704 error = copyin(ujob, &job32, sizeof(job32));
2707 CP(job32, *kjob, aio_fildes);
2708 CP(job32, *kjob, aio_offset);
2709 PTRIN_CP(job32, *kjob, aio_buf);
2710 CP(job32, *kjob, aio_nbytes);
2711 CP(job32, *kjob, aio_lio_opcode);
2712 CP(job32, *kjob, aio_reqprio);
2713 CP(job32, *kjob, _aiocb_private.status);
2714 CP(job32, *kjob, _aiocb_private.error);
2715 PTRIN_CP(job32, *kjob, _aiocb_private.kernelinfo);
2716 return (convert_sigevent32(&job32.aio_sigevent, &kjob->aio_sigevent));
2720 aiocb32_fetch_status(struct aiocb *ujob)
2722 struct aiocb32 *ujob32;
2724 ujob32 = (struct aiocb32 *)ujob;
2725 return (fuword32(&ujob32->_aiocb_private.status));
2729 aiocb32_fetch_error(struct aiocb *ujob)
2731 struct aiocb32 *ujob32;
2733 ujob32 = (struct aiocb32 *)ujob;
2734 return (fuword32(&ujob32->_aiocb_private.error));
2738 aiocb32_store_status(struct aiocb *ujob, long status)
2740 struct aiocb32 *ujob32;
2742 ujob32 = (struct aiocb32 *)ujob;
2743 return (suword32(&ujob32->_aiocb_private.status, status));
2747 aiocb32_store_error(struct aiocb *ujob, long error)
2749 struct aiocb32 *ujob32;
2751 ujob32 = (struct aiocb32 *)ujob;
2752 return (suword32(&ujob32->_aiocb_private.error, error));
2756 aiocb32_store_kernelinfo(struct aiocb *ujob, long jobref)
2758 struct aiocb32 *ujob32;
2760 ujob32 = (struct aiocb32 *)ujob;
2761 return (suword32(&ujob32->_aiocb_private.kernelinfo, jobref));
2765 aiocb32_store_aiocb(struct aiocb **ujobp, struct aiocb *ujob)
2768 return (suword32(ujobp, (long)ujob));
2771 static struct aiocb_ops aiocb32_ops = {
2772 .copyin = aiocb32_copyin,
2773 .fetch_status = aiocb32_fetch_status,
2774 .fetch_error = aiocb32_fetch_error,
2775 .store_status = aiocb32_store_status,
2776 .store_error = aiocb32_store_error,
2777 .store_kernelinfo = aiocb32_store_kernelinfo,
2778 .store_aiocb = aiocb32_store_aiocb,
2781 static struct aiocb_ops aiocb32_ops_osigevent = {
2782 .copyin = aiocb32_copyin_old_sigevent,
2783 .fetch_status = aiocb32_fetch_status,
2784 .fetch_error = aiocb32_fetch_error,
2785 .store_status = aiocb32_store_status,
2786 .store_error = aiocb32_store_error,
2787 .store_kernelinfo = aiocb32_store_kernelinfo,
2788 .store_aiocb = aiocb32_store_aiocb,
2792 freebsd32_aio_return(struct thread *td, struct freebsd32_aio_return_args *uap)
2795 return (kern_aio_return(td, (struct aiocb *)uap->aiocbp, &aiocb32_ops));
2799 freebsd32_aio_suspend(struct thread *td, struct freebsd32_aio_suspend_args *uap)
2801 struct timespec32 ts32;
2802 struct timespec ts, *tsp;
2803 struct aiocb **ujoblist;
2804 uint32_t *ujoblist32;
2807 if (uap->nent < 0 || uap->nent > AIO_LISTIO_MAX)
2811 /* Get timespec struct. */
2812 if ((error = copyin(uap->timeout, &ts32, sizeof(ts32))) != 0)
2814 CP(ts32, ts, tv_sec);
2815 CP(ts32, ts, tv_nsec);
2820 ujoblist = uma_zalloc(aiol_zone, M_WAITOK);
2821 ujoblist32 = (uint32_t *)ujoblist;
2822 error = copyin(uap->aiocbp, ujoblist32, uap->nent *
2823 sizeof(ujoblist32[0]));
2825 for (i = uap->nent; i > 0; i--)
2826 ujoblist[i] = PTRIN(ujoblist32[i]);
2828 error = kern_aio_suspend(td, uap->nent, ujoblist, tsp);
2830 uma_zfree(aiol_zone, ujoblist);
2835 freebsd32_aio_cancel(struct thread *td, struct freebsd32_aio_cancel_args *uap)
2838 return (sys_aio_cancel(td, (struct aio_cancel_args *)uap));
2842 freebsd32_aio_error(struct thread *td, struct freebsd32_aio_error_args *uap)
2845 return (kern_aio_error(td, (struct aiocb *)uap->aiocbp, &aiocb32_ops));
2849 freebsd32_oaio_read(struct thread *td, struct freebsd32_oaio_read_args *uap)
2852 return (aio_aqueue(td, (struct aiocb *)uap->aiocbp, NULL, LIO_READ,
2853 &aiocb32_ops_osigevent));
2857 freebsd32_aio_read(struct thread *td, struct freebsd32_aio_read_args *uap)
2860 return (aio_aqueue(td, (struct aiocb *)uap->aiocbp, NULL, LIO_READ,
2865 freebsd32_oaio_write(struct thread *td, struct freebsd32_oaio_write_args *uap)
2868 return (aio_aqueue(td, (struct aiocb *)uap->aiocbp, NULL, LIO_WRITE,
2869 &aiocb32_ops_osigevent));
2873 freebsd32_aio_write(struct thread *td, struct freebsd32_aio_write_args *uap)
2876 return (aio_aqueue(td, (struct aiocb *)uap->aiocbp, NULL, LIO_WRITE,
2881 freebsd32_aio_waitcomplete(struct thread *td,
2882 struct freebsd32_aio_waitcomplete_args *uap)
2884 struct timespec32 ts32;
2885 struct timespec ts, *tsp;
2889 /* Get timespec struct. */
2890 error = copyin(uap->timeout, &ts32, sizeof(ts32));
2893 CP(ts32, ts, tv_sec);
2894 CP(ts32, ts, tv_nsec);
2899 return (kern_aio_waitcomplete(td, (struct aiocb **)uap->aiocbp, tsp,
2904 freebsd32_aio_fsync(struct thread *td, struct freebsd32_aio_fsync_args *uap)
2907 return (kern_aio_fsync(td, uap->op, (struct aiocb *)uap->aiocbp,
2912 freebsd32_olio_listio(struct thread *td, struct freebsd32_olio_listio_args *uap)
2914 struct aiocb **acb_list;
2915 struct sigevent *sigp, sig;
2916 struct osigevent32 osig;
2917 uint32_t *acb_list32;
2920 if ((uap->mode != LIO_NOWAIT) && (uap->mode != LIO_WAIT))
2924 if (nent < 0 || nent > AIO_LISTIO_MAX)
2927 if (uap->sig && (uap->mode == LIO_NOWAIT)) {
2928 error = copyin(uap->sig, &osig, sizeof(osig));
2931 error = convert_old_sigevent32(&osig, &sig);
2938 acb_list32 = malloc(sizeof(uint32_t) * nent, M_LIO, M_WAITOK);
2939 error = copyin(uap->acb_list, acb_list32, nent * sizeof(uint32_t));
2941 free(acb_list32, M_LIO);
2944 acb_list = malloc(sizeof(struct aiocb *) * nent, M_LIO, M_WAITOK);
2945 for (i = 0; i < nent; i++)
2946 acb_list[i] = PTRIN(acb_list32[i]);
2947 free(acb_list32, M_LIO);
2949 error = kern_lio_listio(td, uap->mode,
2950 (struct aiocb * const *)uap->acb_list, acb_list, nent, sigp,
2951 &aiocb32_ops_osigevent);
2952 free(acb_list, M_LIO);
2957 freebsd32_lio_listio(struct thread *td, struct freebsd32_lio_listio_args *uap)
2959 struct aiocb **acb_list;
2960 struct sigevent *sigp, sig;
2961 struct sigevent32 sig32;
2962 uint32_t *acb_list32;
2965 if ((uap->mode != LIO_NOWAIT) && (uap->mode != LIO_WAIT))
2969 if (nent < 0 || nent > AIO_LISTIO_MAX)
2972 if (uap->sig && (uap->mode == LIO_NOWAIT)) {
2973 error = copyin(uap->sig, &sig32, sizeof(sig32));
2976 error = convert_sigevent32(&sig32, &sig);
2983 acb_list32 = malloc(sizeof(uint32_t) * nent, M_LIO, M_WAITOK);
2984 error = copyin(uap->acb_list, acb_list32, nent * sizeof(uint32_t));
2986 free(acb_list32, M_LIO);
2989 acb_list = malloc(sizeof(struct aiocb *) * nent, M_LIO, M_WAITOK);
2990 for (i = 0; i < nent; i++)
2991 acb_list[i] = PTRIN(acb_list32[i]);
2992 free(acb_list32, M_LIO);
2994 error = kern_lio_listio(td, uap->mode,
2995 (struct aiocb * const *)uap->acb_list, acb_list, nent, sigp,
2997 free(acb_list, M_LIO);