2 * Copyright (c) 1997 John S. Dyson. All rights reserved.
4 * Redistribution and use in source and binary forms, with or without
5 * modification, are permitted provided that the following conditions
7 * 1. Redistributions of source code must retain the above copyright
8 * notice, this list of conditions and the following disclaimer.
9 * 2. John S. Dyson's name may not be used to endorse or promote products
10 * derived from this software without specific prior written permission.
12 * DISCLAIMER: This code isn't warranted to do anything useful. Anything
13 * bad that happens because of using this software isn't the responsibility
14 * of the author. This software is distributed AS-IS.
18 * This file contains support for the POSIX 1003.1B AIO/LIO facility.
21 #include <sys/cdefs.h>
22 __FBSDID("$FreeBSD$");
24 #include "opt_compat.h"
26 #include <sys/param.h>
27 #include <sys/systm.h>
28 #include <sys/malloc.h>
31 #include <sys/eventhandler.h>
32 #include <sys/sysproto.h>
33 #include <sys/filedesc.h>
34 #include <sys/kernel.h>
35 #include <sys/module.h>
36 #include <sys/kthread.h>
37 #include <sys/fcntl.h>
39 #include <sys/limits.h>
41 #include <sys/mutex.h>
42 #include <sys/unistd.h>
43 #include <sys/posix4.h>
45 #include <sys/resourcevar.h>
46 #include <sys/signalvar.h>
47 #include <sys/protosw.h>
49 #include <sys/socket.h>
50 #include <sys/socketvar.h>
51 #include <sys/syscall.h>
52 #include <sys/sysent.h>
53 #include <sys/sysctl.h>
55 #include <sys/taskqueue.h>
56 #include <sys/vnode.h>
58 #include <sys/event.h>
59 #include <sys/mount.h>
61 #include <machine/atomic.h>
64 #include <vm/vm_extern.h>
66 #include <vm/vm_map.h>
67 #include <vm/vm_object.h>
71 #include "opt_vfs_aio.h"
74 * Counter for allocating reference ids to new jobs. Wrapped to 1 on
75 * overflow. (XXX will be removed soon.)
77 static u_long jobrefid;
80 * Counter for aio_fsync.
82 static uint64_t jobseqno;
85 #define JOBST_JOBQSOCK 1
86 #define JOBST_JOBQGLOBAL 2
87 #define JOBST_JOBRUNNING 3
88 #define JOBST_JOBFINISHED 4
89 #define JOBST_JOBQBUF 5
90 #define JOBST_JOBQSYNC 6
92 #ifndef MAX_AIO_PER_PROC
93 #define MAX_AIO_PER_PROC 32
96 #ifndef MAX_AIO_QUEUE_PER_PROC
97 #define MAX_AIO_QUEUE_PER_PROC 256 /* Bigger than AIO_LISTIO_MAX */
100 #ifndef MAX_AIO_PROCS
101 #define MAX_AIO_PROCS 32
104 #ifndef MAX_AIO_QUEUE
105 #define MAX_AIO_QUEUE 1024 /* Bigger than AIO_LISTIO_MAX */
108 #ifndef TARGET_AIO_PROCS
109 #define TARGET_AIO_PROCS 4
113 #define MAX_BUF_AIO 16
116 #ifndef AIOD_TIMEOUT_DEFAULT
117 #define AIOD_TIMEOUT_DEFAULT (10 * hz)
120 #ifndef AIOD_LIFETIME_DEFAULT
121 #define AIOD_LIFETIME_DEFAULT (30 * hz)
124 FEATURE(aio, "Asynchronous I/O");
126 static MALLOC_DEFINE(M_LIO, "lio", "listio aio control block list");
128 static SYSCTL_NODE(_vfs, OID_AUTO, aio, CTLFLAG_RW, 0, "Async IO management");
130 static int max_aio_procs = MAX_AIO_PROCS;
131 SYSCTL_INT(_vfs_aio, OID_AUTO, max_aio_procs,
132 CTLFLAG_RW, &max_aio_procs, 0,
133 "Maximum number of kernel threads to use for handling async IO ");
135 static int num_aio_procs = 0;
136 SYSCTL_INT(_vfs_aio, OID_AUTO, num_aio_procs,
137 CTLFLAG_RD, &num_aio_procs, 0,
138 "Number of presently active kernel threads for async IO");
141 * The code will adjust the actual number of AIO processes towards this
142 * number when it gets a chance.
144 static int target_aio_procs = TARGET_AIO_PROCS;
145 SYSCTL_INT(_vfs_aio, OID_AUTO, target_aio_procs, CTLFLAG_RW, &target_aio_procs,
146 0, "Preferred number of ready kernel threads for async IO");
148 static int max_queue_count = MAX_AIO_QUEUE;
149 SYSCTL_INT(_vfs_aio, OID_AUTO, max_aio_queue, CTLFLAG_RW, &max_queue_count, 0,
150 "Maximum number of aio requests to queue, globally");
152 static int num_queue_count = 0;
153 SYSCTL_INT(_vfs_aio, OID_AUTO, num_queue_count, CTLFLAG_RD, &num_queue_count, 0,
154 "Number of queued aio requests");
156 static int num_buf_aio = 0;
157 SYSCTL_INT(_vfs_aio, OID_AUTO, num_buf_aio, CTLFLAG_RD, &num_buf_aio, 0,
158 "Number of aio requests presently handled by the buf subsystem");
160 /* Number of async I/O thread in the process of being started */
161 /* XXX This should be local to aio_aqueue() */
162 static int num_aio_resv_start = 0;
164 static int aiod_timeout;
165 SYSCTL_INT(_vfs_aio, OID_AUTO, aiod_timeout, CTLFLAG_RW, &aiod_timeout, 0,
166 "Timeout value for synchronous aio operations");
168 static int aiod_lifetime;
169 SYSCTL_INT(_vfs_aio, OID_AUTO, aiod_lifetime, CTLFLAG_RW, &aiod_lifetime, 0,
170 "Maximum lifetime for idle aiod");
172 static int unloadable = 0;
173 SYSCTL_INT(_vfs_aio, OID_AUTO, unloadable, CTLFLAG_RW, &unloadable, 0,
174 "Allow unload of aio (not recommended)");
177 static int max_aio_per_proc = MAX_AIO_PER_PROC;
178 SYSCTL_INT(_vfs_aio, OID_AUTO, max_aio_per_proc, CTLFLAG_RW, &max_aio_per_proc,
179 0, "Maximum active aio requests per process (stored in the process)");
181 static int max_aio_queue_per_proc = MAX_AIO_QUEUE_PER_PROC;
182 SYSCTL_INT(_vfs_aio, OID_AUTO, max_aio_queue_per_proc, CTLFLAG_RW,
183 &max_aio_queue_per_proc, 0,
184 "Maximum queued aio requests per process (stored in the process)");
186 static int max_buf_aio = MAX_BUF_AIO;
187 SYSCTL_INT(_vfs_aio, OID_AUTO, max_buf_aio, CTLFLAG_RW, &max_buf_aio, 0,
188 "Maximum buf aio requests per process (stored in the process)");
190 typedef struct oaiocb {
191 int aio_fildes; /* File descriptor */
192 off_t aio_offset; /* File offset for I/O */
193 volatile void *aio_buf; /* I/O buffer in process space */
194 size_t aio_nbytes; /* Number of bytes for I/O */
195 struct osigevent aio_sigevent; /* Signal to deliver */
196 int aio_lio_opcode; /* LIO opcode */
197 int aio_reqprio; /* Request priority -- ignored */
198 struct __aiocb_private _aiocb_private;
202 * Below is a key of locks used to protect each member of struct aiocblist
203 * aioliojob and kaioinfo and any backends.
205 * * - need not protected
206 * a - locked by kaioinfo lock
207 * b - locked by backend lock, the backend lock can be null in some cases,
208 * for example, BIO belongs to this type, in this case, proc lock is
210 * c - locked by aio_job_mtx, the lock for the generic file I/O backend.
214 * Current, there is only two backends: BIO and generic file I/O.
215 * socket I/O is served by generic file I/O, this is not a good idea, since
216 * disk file I/O and any other types without O_NONBLOCK flag can block daemon
217 * threads, if there is no thread to serve socket I/O, the socket I/O will be
218 * delayed too long or starved, we should create some threads dedicated to
219 * sockets to do non-blocking I/O, same for pipe and fifo, for these I/O
220 * systems we really need non-blocking interface, fiddling O_NONBLOCK in file
221 * structure is not safe because there is race between userland and aio
226 TAILQ_ENTRY(aiocblist) list; /* (b) internal list of for backend */
227 TAILQ_ENTRY(aiocblist) plist; /* (a) list of jobs for each backend */
228 TAILQ_ENTRY(aiocblist) allist; /* (a) list of all jobs in proc */
229 int jobflags; /* (a) job flags */
230 int jobstate; /* (b) job state */
231 int inputcharge; /* (*) input blockes */
232 int outputcharge; /* (*) output blockes */
233 struct buf *bp; /* (*) private to BIO backend,
236 struct proc *userproc; /* (*) user process */
237 struct ucred *cred; /* (*) active credential when created */
238 struct file *fd_file; /* (*) pointer to file structure */
239 struct aioliojob *lio; /* (*) optional lio job */
240 struct aiocb *uuaiocb; /* (*) pointer in userspace of aiocb */
241 struct knlist klist; /* (a) list of knotes */
242 struct aiocb uaiocb; /* (*) kernel I/O control block */
243 ksiginfo_t ksi; /* (a) realtime signal info */
244 struct task biotask; /* (*) private to BIO backend */
245 uint64_t seqno; /* (*) job number */
246 int pending; /* (a) number of pending I/O, aio_fsync only */
250 #define AIOCBLIST_DONE 0x01
251 #define AIOCBLIST_BUFDONE 0x02
252 #define AIOCBLIST_RUNDOWN 0x04
253 #define AIOCBLIST_CHECKSYNC 0x08
258 #define AIOP_FREE 0x1 /* proc on free queue */
260 struct aiothreadlist {
261 int aiothreadflags; /* (c) AIO proc flags */
262 TAILQ_ENTRY(aiothreadlist) list; /* (c) list of processes */
263 struct thread *aiothread; /* (*) the AIO thread */
267 * data-structure for lio signal management
270 int lioj_flags; /* (a) listio flags */
271 int lioj_count; /* (a) listio flags */
272 int lioj_finished_count; /* (a) listio flags */
273 struct sigevent lioj_signal; /* (a) signal on all I/O done */
274 TAILQ_ENTRY(aioliojob) lioj_list; /* (a) lio list */
275 struct knlist klist; /* (a) list of knotes */
276 ksiginfo_t lioj_ksi; /* (a) Realtime signal info */
279 #define LIOJ_SIGNAL 0x1 /* signal on all done (lio) */
280 #define LIOJ_SIGNAL_POSTED 0x2 /* signal has been posted */
281 #define LIOJ_KEVENT_POSTED 0x4 /* kevent triggered */
284 * per process aio data structure
287 struct mtx kaio_mtx; /* the lock to protect this struct */
288 int kaio_flags; /* (a) per process kaio flags */
289 int kaio_maxactive_count; /* (*) maximum number of AIOs */
290 int kaio_active_count; /* (c) number of currently used AIOs */
291 int kaio_qallowed_count; /* (*) maxiumu size of AIO queue */
292 int kaio_count; /* (a) size of AIO queue */
293 int kaio_ballowed_count; /* (*) maximum number of buffers */
294 int kaio_buffer_count; /* (a) number of physio buffers */
295 TAILQ_HEAD(,aiocblist) kaio_all; /* (a) all AIOs in the process */
296 TAILQ_HEAD(,aiocblist) kaio_done; /* (a) done queue for process */
297 TAILQ_HEAD(,aioliojob) kaio_liojoblist; /* (a) list of lio jobs */
298 TAILQ_HEAD(,aiocblist) kaio_jobqueue; /* (a) job queue for process */
299 TAILQ_HEAD(,aiocblist) kaio_bufqueue; /* (a) buffer job queue for process */
300 TAILQ_HEAD(,aiocblist) kaio_sockqueue; /* (a) queue for aios waiting on sockets,
303 TAILQ_HEAD(,aiocblist) kaio_syncqueue; /* (a) queue for aio_fsync */
304 struct task kaio_task; /* (*) task to kick aio threads */
307 #define AIO_LOCK(ki) mtx_lock(&(ki)->kaio_mtx)
308 #define AIO_UNLOCK(ki) mtx_unlock(&(ki)->kaio_mtx)
309 #define AIO_LOCK_ASSERT(ki, f) mtx_assert(&(ki)->kaio_mtx, (f))
310 #define AIO_MTX(ki) (&(ki)->kaio_mtx)
312 #define KAIO_RUNDOWN 0x1 /* process is being run down */
313 #define KAIO_WAKEUP 0x2 /* wakeup process when there is a significant event */
316 * Operations used to interact with userland aio control blocks.
317 * Different ABIs provide their own operations.
320 int (*copyin)(struct aiocb *ujob, struct aiocb *kjob);
321 long (*fetch_status)(struct aiocb *ujob);
322 long (*fetch_error)(struct aiocb *ujob);
323 int (*store_status)(struct aiocb *ujob, long status);
324 int (*store_error)(struct aiocb *ujob, long error);
325 int (*store_kernelinfo)(struct aiocb *ujob, long jobref);
326 int (*store_aiocb)(struct aiocb **ujobp, struct aiocb *ujob);
329 static TAILQ_HEAD(,aiothreadlist) aio_freeproc; /* (c) Idle daemons */
330 static struct sema aio_newproc_sem;
331 static struct mtx aio_job_mtx;
332 static struct mtx aio_sock_mtx;
333 static TAILQ_HEAD(,aiocblist) aio_jobs; /* (c) Async job list */
334 static struct unrhdr *aiod_unr;
336 void aio_init_aioinfo(struct proc *p);
337 static int aio_onceonly(void);
338 static int aio_free_entry(struct aiocblist *aiocbe);
339 static void aio_process(struct aiocblist *aiocbe);
340 static int aio_newproc(int *);
341 int aio_aqueue(struct thread *td, struct aiocb *job,
342 struct aioliojob *lio, int type, struct aiocb_ops *ops);
343 static void aio_physwakeup(struct buf *bp);
344 static void aio_proc_rundown(void *arg, struct proc *p);
345 static void aio_proc_rundown_exec(void *arg, struct proc *p, struct image_params *imgp);
346 static int aio_qphysio(struct proc *p, struct aiocblist *iocb);
347 static void biohelper(void *, int);
348 static void aio_daemon(void *param);
349 static void aio_swake_cb(struct socket *, struct sockbuf *);
350 static int aio_unload(void);
351 static void aio_bio_done_notify(struct proc *userp, struct aiocblist *aiocbe, int type);
354 static int aio_kick(struct proc *userp);
355 static void aio_kick_nowait(struct proc *userp);
356 static void aio_kick_helper(void *context, int pending);
357 static int filt_aioattach(struct knote *kn);
358 static void filt_aiodetach(struct knote *kn);
359 static int filt_aio(struct knote *kn, long hint);
360 static int filt_lioattach(struct knote *kn);
361 static void filt_liodetach(struct knote *kn);
362 static int filt_lio(struct knote *kn, long hint);
366 * kaio Per process async io info
367 * aiop async io thread data
368 * aiocb async io jobs
369 * aiol list io job pointer - internal to aio_suspend XXX
370 * aiolio list io jobs
372 static uma_zone_t kaio_zone, aiop_zone, aiocb_zone, aiol_zone, aiolio_zone;
374 /* kqueue filters for aio */
375 static struct filterops aio_filtops =
376 { 0, filt_aioattach, filt_aiodetach, filt_aio };
377 static struct filterops lio_filtops =
378 { 0, filt_lioattach, filt_liodetach, filt_lio };
380 static eventhandler_tag exit_tag, exec_tag;
382 TASKQUEUE_DEFINE_THREAD(aiod_bio);
385 * Main operations function for use as a kernel module.
388 aio_modload(struct module *module, int cmd, void *arg)
397 error = aio_unload();
408 static moduledata_t aio_mod = {
414 static struct syscall_helper_data aio_syscalls[] = {
415 SYSCALL_INIT_HELPER(aio_cancel),
416 SYSCALL_INIT_HELPER(aio_error),
417 SYSCALL_INIT_HELPER(aio_fsync),
418 SYSCALL_INIT_HELPER(aio_read),
419 SYSCALL_INIT_HELPER(aio_return),
420 SYSCALL_INIT_HELPER(aio_suspend),
421 SYSCALL_INIT_HELPER(aio_waitcomplete),
422 SYSCALL_INIT_HELPER(aio_write),
423 SYSCALL_INIT_HELPER(lio_listio),
424 SYSCALL_INIT_HELPER(oaio_read),
425 SYSCALL_INIT_HELPER(oaio_write),
426 SYSCALL_INIT_HELPER(olio_listio),
430 #ifdef COMPAT_FREEBSD32
431 #include <sys/mount.h>
432 #include <sys/socket.h>
433 #include <compat/freebsd32/freebsd32.h>
434 #include <compat/freebsd32/freebsd32_proto.h>
435 #include <compat/freebsd32/freebsd32_signal.h>
436 #include <compat/freebsd32/freebsd32_syscall.h>
437 #include <compat/freebsd32/freebsd32_util.h>
439 static struct syscall_helper_data aio32_syscalls[] = {
440 SYSCALL32_INIT_HELPER(freebsd32_aio_return),
441 SYSCALL32_INIT_HELPER(freebsd32_aio_suspend),
442 SYSCALL32_INIT_HELPER(freebsd32_aio_cancel),
443 SYSCALL32_INIT_HELPER(freebsd32_aio_error),
444 SYSCALL32_INIT_HELPER(freebsd32_aio_fsync),
445 SYSCALL32_INIT_HELPER(freebsd32_aio_read),
446 SYSCALL32_INIT_HELPER(freebsd32_aio_write),
447 SYSCALL32_INIT_HELPER(freebsd32_aio_waitcomplete),
448 SYSCALL32_INIT_HELPER(freebsd32_lio_listio),
449 SYSCALL32_INIT_HELPER(freebsd32_oaio_read),
450 SYSCALL32_INIT_HELPER(freebsd32_oaio_write),
451 SYSCALL32_INIT_HELPER(freebsd32_olio_listio),
456 DECLARE_MODULE(aio, aio_mod,
457 SI_SUB_VFS, SI_ORDER_ANY);
458 MODULE_VERSION(aio, 1);
461 * Startup initialization
468 /* XXX: should probably just use so->callback */
469 aio_swake = &aio_swake_cb;
470 exit_tag = EVENTHANDLER_REGISTER(process_exit, aio_proc_rundown, NULL,
471 EVENTHANDLER_PRI_ANY);
472 exec_tag = EVENTHANDLER_REGISTER(process_exec, aio_proc_rundown_exec, NULL,
473 EVENTHANDLER_PRI_ANY);
474 kqueue_add_filteropts(EVFILT_AIO, &aio_filtops);
475 kqueue_add_filteropts(EVFILT_LIO, &lio_filtops);
476 TAILQ_INIT(&aio_freeproc);
477 sema_init(&aio_newproc_sem, 0, "aio_new_proc");
478 mtx_init(&aio_job_mtx, "aio_job", NULL, MTX_DEF);
479 mtx_init(&aio_sock_mtx, "aio_sock", NULL, MTX_DEF);
480 TAILQ_INIT(&aio_jobs);
481 aiod_unr = new_unrhdr(1, INT_MAX, NULL);
482 kaio_zone = uma_zcreate("AIO", sizeof(struct kaioinfo), NULL, NULL,
483 NULL, NULL, UMA_ALIGN_PTR, UMA_ZONE_NOFREE);
484 aiop_zone = uma_zcreate("AIOP", sizeof(struct aiothreadlist), NULL,
485 NULL, NULL, NULL, UMA_ALIGN_PTR, UMA_ZONE_NOFREE);
486 aiocb_zone = uma_zcreate("AIOCB", sizeof(struct aiocblist), NULL, NULL,
487 NULL, NULL, UMA_ALIGN_PTR, UMA_ZONE_NOFREE);
488 aiol_zone = uma_zcreate("AIOL", AIO_LISTIO_MAX*sizeof(intptr_t) , NULL,
489 NULL, NULL, NULL, UMA_ALIGN_PTR, UMA_ZONE_NOFREE);
490 aiolio_zone = uma_zcreate("AIOLIO", sizeof(struct aioliojob), NULL,
491 NULL, NULL, NULL, UMA_ALIGN_PTR, UMA_ZONE_NOFREE);
492 aiod_timeout = AIOD_TIMEOUT_DEFAULT;
493 aiod_lifetime = AIOD_LIFETIME_DEFAULT;
495 async_io_version = _POSIX_VERSION;
496 p31b_setcfg(CTL_P1003_1B_AIO_LISTIO_MAX, AIO_LISTIO_MAX);
497 p31b_setcfg(CTL_P1003_1B_AIO_MAX, MAX_AIO_QUEUE);
498 p31b_setcfg(CTL_P1003_1B_AIO_PRIO_DELTA_MAX, 0);
500 error = syscall_helper_register(aio_syscalls);
503 #ifdef COMPAT_FREEBSD32
504 error = syscall32_helper_register(aio32_syscalls);
512 * Callback for unload of AIO when used as a module.
520 * XXX: no unloads by default, it's too dangerous.
521 * perhaps we could do it if locked out callers and then
522 * did an aio_proc_rundown() on each process.
524 * jhb: aio_proc_rundown() needs to run on curproc though,
525 * so I don't think that would fly.
530 #ifdef COMPAT_FREEBSD32
531 syscall32_helper_unregister(aio32_syscalls);
533 syscall_helper_unregister(aio_syscalls);
535 error = kqueue_del_filteropts(EVFILT_AIO);
538 error = kqueue_del_filteropts(EVFILT_LIO);
541 async_io_version = 0;
543 taskqueue_free(taskqueue_aiod_bio);
544 delete_unrhdr(aiod_unr);
545 uma_zdestroy(kaio_zone);
546 uma_zdestroy(aiop_zone);
547 uma_zdestroy(aiocb_zone);
548 uma_zdestroy(aiol_zone);
549 uma_zdestroy(aiolio_zone);
550 EVENTHANDLER_DEREGISTER(process_exit, exit_tag);
551 EVENTHANDLER_DEREGISTER(process_exec, exec_tag);
552 mtx_destroy(&aio_job_mtx);
553 mtx_destroy(&aio_sock_mtx);
554 sema_destroy(&aio_newproc_sem);
555 p31b_setcfg(CTL_P1003_1B_AIO_LISTIO_MAX, -1);
556 p31b_setcfg(CTL_P1003_1B_AIO_MAX, -1);
557 p31b_setcfg(CTL_P1003_1B_AIO_PRIO_DELTA_MAX, -1);
562 * Init the per-process aioinfo structure. The aioinfo limits are set
563 * per-process for user limit (resource) management.
566 aio_init_aioinfo(struct proc *p)
570 ki = uma_zalloc(kaio_zone, M_WAITOK);
571 mtx_init(&ki->kaio_mtx, "aiomtx", NULL, MTX_DEF);
573 ki->kaio_maxactive_count = max_aio_per_proc;
574 ki->kaio_active_count = 0;
575 ki->kaio_qallowed_count = max_aio_queue_per_proc;
577 ki->kaio_ballowed_count = max_buf_aio;
578 ki->kaio_buffer_count = 0;
579 TAILQ_INIT(&ki->kaio_all);
580 TAILQ_INIT(&ki->kaio_done);
581 TAILQ_INIT(&ki->kaio_jobqueue);
582 TAILQ_INIT(&ki->kaio_bufqueue);
583 TAILQ_INIT(&ki->kaio_liojoblist);
584 TAILQ_INIT(&ki->kaio_sockqueue);
585 TAILQ_INIT(&ki->kaio_syncqueue);
586 TASK_INIT(&ki->kaio_task, 0, aio_kick_helper, p);
588 if (p->p_aioinfo == NULL) {
593 mtx_destroy(&ki->kaio_mtx);
594 uma_zfree(kaio_zone, ki);
597 while (num_aio_procs < MIN(target_aio_procs, max_aio_procs))
602 aio_sendsig(struct proc *p, struct sigevent *sigev, ksiginfo_t *ksi)
608 ksi->ksi_code = SI_ASYNCIO;
609 ksi->ksi_flags |= KSI_EXT | KSI_INS;
610 ret = psignal_event(p, sigev, ksi);
617 * Free a job entry. Wait for completion if it is currently active, but don't
618 * delay forever. If we delay, we return a flag that says that we have to
619 * restart the queue scan.
622 aio_free_entry(struct aiocblist *aiocbe)
625 struct aioliojob *lj;
628 p = aiocbe->userproc;
633 AIO_LOCK_ASSERT(ki, MA_OWNED);
634 MPASS(aiocbe->jobstate == JOBST_JOBFINISHED);
636 atomic_subtract_int(&num_queue_count, 1);
639 MPASS(ki->kaio_count >= 0);
641 TAILQ_REMOVE(&ki->kaio_done, aiocbe, plist);
642 TAILQ_REMOVE(&ki->kaio_all, aiocbe, allist);
647 lj->lioj_finished_count--;
649 if (lj->lioj_count == 0) {
650 TAILQ_REMOVE(&ki->kaio_liojoblist, lj, lioj_list);
651 /* lio is going away, we need to destroy any knotes */
652 knlist_delete(&lj->klist, curthread, 1);
654 sigqueue_take(&lj->lioj_ksi);
656 uma_zfree(aiolio_zone, lj);
660 /* aiocbe is going away, we need to destroy any knotes */
661 knlist_delete(&aiocbe->klist, curthread, 1);
663 sigqueue_take(&aiocbe->ksi);
666 MPASS(aiocbe->bp == NULL);
667 aiocbe->jobstate = JOBST_NULL;
671 * The thread argument here is used to find the owning process
672 * and is also passed to fo_close() which may pass it to various
673 * places such as devsw close() routines. Because of that, we
674 * need a thread pointer from the process owning the job that is
675 * persistent and won't disappear out from under us or move to
678 * Currently, all the callers of this function call it to remove
679 * an aiocblist from the current process' job list either via a
680 * syscall or due to the current process calling exit() or
681 * execve(). Thus, we know that p == curproc. We also know that
682 * curthread can't exit since we are curthread.
684 * Therefore, we use curthread as the thread to pass to
685 * knlist_delete(). This does mean that it is possible for the
686 * thread pointer at close time to differ from the thread pointer
687 * at open time, but this is already true of file descriptors in
688 * a multithreaded process.
690 fdrop(aiocbe->fd_file, curthread);
691 crfree(aiocbe->cred);
692 uma_zfree(aiocb_zone, aiocbe);
699 aio_proc_rundown_exec(void *arg, struct proc *p, struct image_params *imgp __unused)
701 aio_proc_rundown(arg, p);
705 * Rundown the jobs for a given process.
708 aio_proc_rundown(void *arg, struct proc *p)
711 struct aioliojob *lj;
712 struct aiocblist *cbe, *cbn;
717 KASSERT(curthread->td_proc == p,
718 ("%s: called on non-curproc", __func__));
724 ki->kaio_flags |= KAIO_RUNDOWN;
729 * Try to cancel all pending requests. This code simulates
730 * aio_cancel on all pending I/O requests.
732 TAILQ_FOREACH_SAFE(cbe, &ki->kaio_jobqueue, plist, cbn) {
734 mtx_lock(&aio_job_mtx);
735 if (cbe->jobstate == JOBST_JOBQGLOBAL) {
736 TAILQ_REMOVE(&aio_jobs, cbe, list);
738 } else if (cbe->jobstate == JOBST_JOBQSOCK) {
740 MPASS(fp->f_type == DTYPE_SOCKET);
742 TAILQ_REMOVE(&so->so_aiojobq, cbe, list);
744 } else if (cbe->jobstate == JOBST_JOBQSYNC) {
745 TAILQ_REMOVE(&ki->kaio_syncqueue, cbe, list);
748 mtx_unlock(&aio_job_mtx);
751 cbe->jobstate = JOBST_JOBFINISHED;
752 cbe->uaiocb._aiocb_private.status = -1;
753 cbe->uaiocb._aiocb_private.error = ECANCELED;
754 TAILQ_REMOVE(&ki->kaio_jobqueue, cbe, plist);
755 aio_bio_done_notify(p, cbe, DONE_QUEUE);
759 /* Wait for all running I/O to be finished */
760 if (TAILQ_FIRST(&ki->kaio_bufqueue) ||
761 TAILQ_FIRST(&ki->kaio_jobqueue)) {
762 ki->kaio_flags |= KAIO_WAKEUP;
763 msleep(&p->p_aioinfo, AIO_MTX(ki), PRIBIO, "aioprn", hz);
767 /* Free all completed I/O requests. */
768 while ((cbe = TAILQ_FIRST(&ki->kaio_done)) != NULL)
771 while ((lj = TAILQ_FIRST(&ki->kaio_liojoblist)) != NULL) {
772 if (lj->lioj_count == 0) {
773 TAILQ_REMOVE(&ki->kaio_liojoblist, lj, lioj_list);
774 knlist_delete(&lj->klist, curthread, 1);
776 sigqueue_take(&lj->lioj_ksi);
778 uma_zfree(aiolio_zone, lj);
780 panic("LIO job not cleaned up: C:%d, FC:%d\n",
781 lj->lioj_count, lj->lioj_finished_count);
785 taskqueue_drain(taskqueue_aiod_bio, &ki->kaio_task);
786 mtx_destroy(&ki->kaio_mtx);
787 uma_zfree(kaio_zone, ki);
792 * Select a job to run (called by an AIO daemon).
794 static struct aiocblist *
795 aio_selectjob(struct aiothreadlist *aiop)
797 struct aiocblist *aiocbe;
801 mtx_assert(&aio_job_mtx, MA_OWNED);
802 TAILQ_FOREACH(aiocbe, &aio_jobs, list) {
803 userp = aiocbe->userproc;
804 ki = userp->p_aioinfo;
806 if (ki->kaio_active_count < ki->kaio_maxactive_count) {
807 TAILQ_REMOVE(&aio_jobs, aiocbe, list);
808 /* Account for currently active jobs. */
809 ki->kaio_active_count++;
810 aiocbe->jobstate = JOBST_JOBRUNNING;
818 * Move all data to a permanent storage device, this code
819 * simulates fsync syscall.
822 aio_fsync_vnode(struct thread *td, struct vnode *vp)
828 vfslocked = VFS_LOCK_GIANT(vp->v_mount);
829 if ((error = vn_start_write(vp, &mp, V_WAIT | PCATCH)) != 0)
831 vn_lock(vp, LK_EXCLUSIVE | LK_RETRY);
832 if (vp->v_object != NULL) {
833 VM_OBJECT_LOCK(vp->v_object);
834 vm_object_page_clean(vp->v_object, 0, 0, 0);
835 VM_OBJECT_UNLOCK(vp->v_object);
837 error = VOP_FSYNC(vp, MNT_WAIT, td);
840 vn_finished_write(mp);
842 VFS_UNLOCK_GIANT(vfslocked);
847 * The AIO processing activity. This is the code that does the I/O request for
848 * the non-physio version of the operations. The normal vn operations are used,
849 * and this code should work in all instances for every type of file, including
850 * pipes, sockets, fifos, and regular files.
852 * XXX I don't think it works well for socket, pipe, and fifo.
855 aio_process(struct aiocblist *aiocbe)
857 struct ucred *td_savedcred;
866 int oublock_st, oublock_end;
867 int inblock_st, inblock_end;
870 td_savedcred = td->td_ucred;
871 td->td_ucred = aiocbe->cred;
872 cb = &aiocbe->uaiocb;
873 fp = aiocbe->fd_file;
875 if (cb->aio_lio_opcode == LIO_SYNC) {
878 if (fp->f_vnode != NULL)
879 error = aio_fsync_vnode(td, fp->f_vnode);
880 cb->_aiocb_private.error = error;
881 cb->_aiocb_private.status = 0;
882 td->td_ucred = td_savedcred;
886 aiov.iov_base = (void *)(uintptr_t)cb->aio_buf;
887 aiov.iov_len = cb->aio_nbytes;
889 auio.uio_iov = &aiov;
891 auio.uio_offset = cb->aio_offset;
892 auio.uio_resid = cb->aio_nbytes;
893 cnt = cb->aio_nbytes;
894 auio.uio_segflg = UIO_USERSPACE;
897 inblock_st = td->td_ru.ru_inblock;
898 oublock_st = td->td_ru.ru_oublock;
900 * aio_aqueue() acquires a reference to the file that is
901 * released in aio_free_entry().
903 if (cb->aio_lio_opcode == LIO_READ) {
904 auio.uio_rw = UIO_READ;
905 if (auio.uio_resid == 0)
908 error = fo_read(fp, &auio, fp->f_cred, FOF_OFFSET, td);
910 if (fp->f_type == DTYPE_VNODE)
912 auio.uio_rw = UIO_WRITE;
913 error = fo_write(fp, &auio, fp->f_cred, FOF_OFFSET, td);
915 inblock_end = td->td_ru.ru_inblock;
916 oublock_end = td->td_ru.ru_oublock;
918 aiocbe->inputcharge = inblock_end - inblock_st;
919 aiocbe->outputcharge = oublock_end - oublock_st;
921 if ((error) && (auio.uio_resid != cnt)) {
922 if (error == ERESTART || error == EINTR || error == EWOULDBLOCK)
924 if ((error == EPIPE) && (cb->aio_lio_opcode == LIO_WRITE)) {
926 if (fp->f_type == DTYPE_SOCKET) {
928 if (so->so_options & SO_NOSIGPIPE)
932 PROC_LOCK(aiocbe->userproc);
933 psignal(aiocbe->userproc, SIGPIPE);
934 PROC_UNLOCK(aiocbe->userproc);
939 cnt -= auio.uio_resid;
940 cb->_aiocb_private.error = error;
941 cb->_aiocb_private.status = cnt;
942 td->td_ucred = td_savedcred;
946 aio_bio_done_notify(struct proc *userp, struct aiocblist *aiocbe, int type)
948 struct aioliojob *lj;
950 struct aiocblist *scb, *scbn;
953 ki = userp->p_aioinfo;
954 AIO_LOCK_ASSERT(ki, MA_OWNED);
958 lj->lioj_finished_count++;
959 if (lj->lioj_count == lj->lioj_finished_count)
962 if (type == DONE_QUEUE) {
963 aiocbe->jobflags |= AIOCBLIST_DONE;
965 aiocbe->jobflags |= AIOCBLIST_BUFDONE;
967 TAILQ_INSERT_TAIL(&ki->kaio_done, aiocbe, plist);
968 aiocbe->jobstate = JOBST_JOBFINISHED;
970 if (ki->kaio_flags & KAIO_RUNDOWN)
971 goto notification_done;
973 if (aiocbe->uaiocb.aio_sigevent.sigev_notify == SIGEV_SIGNAL ||
974 aiocbe->uaiocb.aio_sigevent.sigev_notify == SIGEV_THREAD_ID)
975 aio_sendsig(userp, &aiocbe->uaiocb.aio_sigevent, &aiocbe->ksi);
977 KNOTE_LOCKED(&aiocbe->klist, 1);
980 if (lj->lioj_signal.sigev_notify == SIGEV_KEVENT) {
981 lj->lioj_flags |= LIOJ_KEVENT_POSTED;
982 KNOTE_LOCKED(&lj->klist, 1);
984 if ((lj->lioj_flags & (LIOJ_SIGNAL|LIOJ_SIGNAL_POSTED))
986 && (lj->lioj_signal.sigev_notify == SIGEV_SIGNAL ||
987 lj->lioj_signal.sigev_notify == SIGEV_THREAD_ID)) {
988 aio_sendsig(userp, &lj->lioj_signal, &lj->lioj_ksi);
989 lj->lioj_flags |= LIOJ_SIGNAL_POSTED;
994 if (aiocbe->jobflags & AIOCBLIST_CHECKSYNC) {
995 TAILQ_FOREACH_SAFE(scb, &ki->kaio_syncqueue, list, scbn) {
996 if (aiocbe->fd_file == scb->fd_file &&
997 aiocbe->seqno < scb->seqno) {
998 if (--scb->pending == 0) {
999 mtx_lock(&aio_job_mtx);
1000 scb->jobstate = JOBST_JOBQGLOBAL;
1001 TAILQ_REMOVE(&ki->kaio_syncqueue, scb, list);
1002 TAILQ_INSERT_TAIL(&aio_jobs, scb, list);
1003 aio_kick_nowait(userp);
1004 mtx_unlock(&aio_job_mtx);
1009 if (ki->kaio_flags & KAIO_WAKEUP) {
1010 ki->kaio_flags &= ~KAIO_WAKEUP;
1011 wakeup(&userp->p_aioinfo);
1016 * The AIO daemon, most of the actual work is done in aio_process,
1017 * but the setup (and address space mgmt) is done in this routine.
1020 aio_daemon(void *_id)
1022 struct aiocblist *aiocbe;
1023 struct aiothreadlist *aiop;
1024 struct kaioinfo *ki;
1025 struct proc *curcp, *mycp, *userp;
1026 struct vmspace *myvm, *tmpvm;
1027 struct thread *td = curthread;
1028 int id = (intptr_t)_id;
1031 * Local copies of curproc (cp) and vmspace (myvm)
1034 myvm = mycp->p_vmspace;
1036 KASSERT(mycp->p_textvp == NULL, ("kthread has a textvp"));
1039 * Allocate and ready the aio control info. There is one aiop structure
1042 aiop = uma_zalloc(aiop_zone, M_WAITOK);
1043 aiop->aiothread = td;
1044 aiop->aiothreadflags = 0;
1046 /* The daemon resides in its own pgrp. */
1050 * Wakeup parent process. (Parent sleeps to keep from blasting away
1051 * and creating too many daemons.)
1053 sema_post(&aio_newproc_sem);
1055 mtx_lock(&aio_job_mtx);
1058 * curcp is the current daemon process context.
1059 * userp is the current user process context.
1064 * Take daemon off of free queue
1066 if (aiop->aiothreadflags & AIOP_FREE) {
1067 TAILQ_REMOVE(&aio_freeproc, aiop, list);
1068 aiop->aiothreadflags &= ~AIOP_FREE;
1074 while ((aiocbe = aio_selectjob(aiop)) != NULL) {
1075 mtx_unlock(&aio_job_mtx);
1076 userp = aiocbe->userproc;
1079 * Connect to process address space for user program.
1081 if (userp != curcp) {
1083 * Save the current address space that we are
1086 tmpvm = mycp->p_vmspace;
1089 * Point to the new user address space, and
1092 mycp->p_vmspace = userp->p_vmspace;
1093 atomic_add_int(&mycp->p_vmspace->vm_refcnt, 1);
1095 /* Activate the new mapping. */
1096 pmap_activate(FIRST_THREAD_IN_PROC(mycp));
1099 * If the old address space wasn't the daemons
1100 * own address space, then we need to remove the
1101 * daemon's reference from the other process
1102 * that it was acting on behalf of.
1104 if (tmpvm != myvm) {
1105 vmspace_free(tmpvm);
1110 ki = userp->p_aioinfo;
1112 /* Do the I/O function. */
1113 aio_process(aiocbe);
1115 mtx_lock(&aio_job_mtx);
1116 /* Decrement the active job count. */
1117 ki->kaio_active_count--;
1118 mtx_unlock(&aio_job_mtx);
1121 TAILQ_REMOVE(&ki->kaio_jobqueue, aiocbe, plist);
1122 aio_bio_done_notify(userp, aiocbe, DONE_QUEUE);
1125 mtx_lock(&aio_job_mtx);
1129 * Disconnect from user address space.
1131 if (curcp != mycp) {
1133 mtx_unlock(&aio_job_mtx);
1135 /* Get the user address space to disconnect from. */
1136 tmpvm = mycp->p_vmspace;
1138 /* Get original address space for daemon. */
1139 mycp->p_vmspace = myvm;
1141 /* Activate the daemon's address space. */
1142 pmap_activate(FIRST_THREAD_IN_PROC(mycp));
1144 if (tmpvm == myvm) {
1145 printf("AIOD: vmspace problem -- %d\n",
1149 /* Remove our vmspace reference. */
1150 vmspace_free(tmpvm);
1154 mtx_lock(&aio_job_mtx);
1156 * We have to restart to avoid race, we only sleep if
1157 * no job can be selected, that should be
1163 mtx_assert(&aio_job_mtx, MA_OWNED);
1165 TAILQ_INSERT_HEAD(&aio_freeproc, aiop, list);
1166 aiop->aiothreadflags |= AIOP_FREE;
1169 * If daemon is inactive for a long time, allow it to exit,
1170 * thereby freeing resources.
1172 if (msleep(aiop->aiothread, &aio_job_mtx, PRIBIO, "aiordy",
1174 if (TAILQ_EMPTY(&aio_jobs)) {
1175 if ((aiop->aiothreadflags & AIOP_FREE) &&
1176 (num_aio_procs > target_aio_procs)) {
1177 TAILQ_REMOVE(&aio_freeproc, aiop, list);
1179 mtx_unlock(&aio_job_mtx);
1180 uma_zfree(aiop_zone, aiop);
1181 free_unr(aiod_unr, id);
1183 if (mycp->p_vmspace->vm_refcnt <= 1) {
1184 printf("AIOD: bad vm refcnt for"
1185 " exiting daemon: %d\n",
1186 mycp->p_vmspace->vm_refcnt);
1194 mtx_unlock(&aio_job_mtx);
1195 panic("shouldn't be here\n");
1199 * Create a new AIO daemon. This is mostly a kernel-thread fork routine. The
1200 * AIO daemon modifies its environment itself.
1203 aio_newproc(int *start)
1209 id = alloc_unr(aiod_unr);
1210 error = kproc_create(aio_daemon, (void *)(intptr_t)id, &p,
1211 RFNOWAIT, 0, "aiod%d", id);
1214 * Wait until daemon is started.
1216 sema_wait(&aio_newproc_sem);
1217 mtx_lock(&aio_job_mtx);
1221 mtx_unlock(&aio_job_mtx);
1223 free_unr(aiod_unr, id);
1229 * Try the high-performance, low-overhead physio method for eligible
1230 * VCHR devices. This method doesn't use an aio helper thread, and
1231 * thus has very low overhead.
1233 * Assumes that the caller, aio_aqueue(), has incremented the file
1234 * structure's reference count, preventing its deallocation for the
1235 * duration of this call.
1238 aio_qphysio(struct proc *p, struct aiocblist *aiocbe)
1244 struct kaioinfo *ki;
1245 struct aioliojob *lj;
1248 cb = &aiocbe->uaiocb;
1249 fp = aiocbe->fd_file;
1251 if (fp->f_type != DTYPE_VNODE)
1257 * If its not a disk, we don't want to return a positive error.
1258 * It causes the aio code to not fall through to try the thread
1259 * way when you're talking to a regular file.
1261 if (!vn_isdisk(vp, &error)) {
1262 if (error == ENOTBLK)
1268 if (vp->v_bufobj.bo_bsize == 0)
1271 if (cb->aio_nbytes % vp->v_bufobj.bo_bsize)
1274 if (cb->aio_nbytes > vp->v_rdev->si_iosize_max)
1277 if (cb->aio_nbytes >
1278 MAXPHYS - (((vm_offset_t) cb->aio_buf) & PAGE_MASK))
1282 if (ki->kaio_buffer_count >= ki->kaio_ballowed_count)
1285 /* Create and build a buffer header for a transfer. */
1286 bp = (struct buf *)getpbuf(NULL);
1291 ki->kaio_buffer_count++;
1298 * Get a copy of the kva from the physical buffer.
1302 bp->b_bcount = cb->aio_nbytes;
1303 bp->b_bufsize = cb->aio_nbytes;
1304 bp->b_iodone = aio_physwakeup;
1305 bp->b_saveaddr = bp->b_data;
1306 bp->b_data = (void *)(uintptr_t)cb->aio_buf;
1307 bp->b_offset = cb->aio_offset;
1308 bp->b_iooffset = cb->aio_offset;
1309 bp->b_blkno = btodb(cb->aio_offset);
1310 bp->b_iocmd = cb->aio_lio_opcode == LIO_WRITE ? BIO_WRITE : BIO_READ;
1313 * Bring buffer into kernel space.
1315 if (vmapbuf(bp) < 0) {
1322 bp->b_caller1 = (void *)aiocbe;
1323 TAILQ_INSERT_TAIL(&ki->kaio_bufqueue, aiocbe, plist);
1324 TAILQ_INSERT_TAIL(&ki->kaio_all, aiocbe, allist);
1325 aiocbe->jobstate = JOBST_JOBQBUF;
1326 cb->_aiocb_private.status = cb->aio_nbytes;
1329 atomic_add_int(&num_queue_count, 1);
1330 atomic_add_int(&num_buf_aio, 1);
1334 TASK_INIT(&aiocbe->biotask, 0, biohelper, aiocbe);
1336 /* Perform transfer. */
1337 dev_strategy(vp->v_rdev, bp);
1343 ki->kaio_buffer_count--;
1353 * Wake up aio requests that may be serviceable now.
1356 aio_swake_cb(struct socket *so, struct sockbuf *sb)
1358 struct aiocblist *cb, *cbn;
1361 SOCKBUF_LOCK_ASSERT(sb);
1362 if (sb == &so->so_snd)
1367 sb->sb_flags &= ~SB_AIO;
1368 mtx_lock(&aio_job_mtx);
1369 TAILQ_FOREACH_SAFE(cb, &so->so_aiojobq, list, cbn) {
1370 if (opcode == cb->uaiocb.aio_lio_opcode) {
1371 if (cb->jobstate != JOBST_JOBQSOCK)
1372 panic("invalid queue value");
1374 * We don't have actual sockets backend yet,
1375 * so we simply move the requests to the generic
1378 TAILQ_REMOVE(&so->so_aiojobq, cb, list);
1379 TAILQ_INSERT_TAIL(&aio_jobs, cb, list);
1380 aio_kick_nowait(cb->userproc);
1383 mtx_unlock(&aio_job_mtx);
1387 convert_old_sigevent(struct osigevent *osig, struct sigevent *nsig)
1391 * Only SIGEV_NONE, SIGEV_SIGNAL, and SIGEV_KEVENT are
1392 * supported by AIO with the old sigevent structure.
1394 nsig->sigev_notify = osig->sigev_notify;
1395 switch (nsig->sigev_notify) {
1399 nsig->sigev_signo = osig->__sigev_u.__sigev_signo;
1402 nsig->sigev_notify_kqueue =
1403 osig->__sigev_u.__sigev_notify_kqueue;
1404 nsig->sigev_value.sival_ptr = osig->sigev_value.sival_ptr;
1413 aiocb_copyin_old_sigevent(struct aiocb *ujob, struct aiocb *kjob)
1415 struct oaiocb *ojob;
1418 bzero(kjob, sizeof(struct aiocb));
1419 error = copyin(ujob, kjob, sizeof(struct oaiocb));
1422 ojob = (struct oaiocb *)kjob;
1423 return (convert_old_sigevent(&ojob->aio_sigevent, &kjob->aio_sigevent));
1427 aiocb_copyin(struct aiocb *ujob, struct aiocb *kjob)
1430 return (copyin(ujob, kjob, sizeof(struct aiocb)));
1434 aiocb_fetch_status(struct aiocb *ujob)
1437 return (fuword(&ujob->_aiocb_private.status));
1441 aiocb_fetch_error(struct aiocb *ujob)
1444 return (fuword(&ujob->_aiocb_private.error));
1448 aiocb_store_status(struct aiocb *ujob, long status)
1451 return (suword(&ujob->_aiocb_private.status, status));
1455 aiocb_store_error(struct aiocb *ujob, long error)
1458 return (suword(&ujob->_aiocb_private.error, error));
1462 aiocb_store_kernelinfo(struct aiocb *ujob, long jobref)
1465 return (suword(&ujob->_aiocb_private.kernelinfo, jobref));
1469 aiocb_store_aiocb(struct aiocb **ujobp, struct aiocb *ujob)
1472 return (suword(ujobp, (long)ujob));
1475 static struct aiocb_ops aiocb_ops = {
1476 .copyin = aiocb_copyin,
1477 .fetch_status = aiocb_fetch_status,
1478 .fetch_error = aiocb_fetch_error,
1479 .store_status = aiocb_store_status,
1480 .store_error = aiocb_store_error,
1481 .store_kernelinfo = aiocb_store_kernelinfo,
1482 .store_aiocb = aiocb_store_aiocb,
1485 static struct aiocb_ops aiocb_ops_osigevent = {
1486 .copyin = aiocb_copyin_old_sigevent,
1487 .fetch_status = aiocb_fetch_status,
1488 .fetch_error = aiocb_fetch_error,
1489 .store_status = aiocb_store_status,
1490 .store_error = aiocb_store_error,
1491 .store_kernelinfo = aiocb_store_kernelinfo,
1492 .store_aiocb = aiocb_store_aiocb,
1496 * Queue a new AIO request. Choosing either the threaded or direct physio VCHR
1497 * technique is done in this code.
1500 aio_aqueue(struct thread *td, struct aiocb *job, struct aioliojob *lj,
1501 int type, struct aiocb_ops *ops)
1503 struct proc *p = td->td_proc;
1506 struct aiocblist *aiocbe, *cb;
1507 struct kaioinfo *ki;
1515 if (p->p_aioinfo == NULL)
1516 aio_init_aioinfo(p);
1520 ops->store_status(job, -1);
1521 ops->store_error(job, 0);
1522 ops->store_kernelinfo(job, -1);
1524 if (num_queue_count >= max_queue_count ||
1525 ki->kaio_count >= ki->kaio_qallowed_count) {
1526 ops->store_error(job, EAGAIN);
1530 aiocbe = uma_zalloc(aiocb_zone, M_WAITOK | M_ZERO);
1531 aiocbe->inputcharge = 0;
1532 aiocbe->outputcharge = 0;
1533 knlist_init_mtx(&aiocbe->klist, AIO_MTX(ki));
1535 error = ops->copyin(job, &aiocbe->uaiocb);
1537 ops->store_error(job, error);
1538 uma_zfree(aiocb_zone, aiocbe);
1542 if (aiocbe->uaiocb.aio_sigevent.sigev_notify != SIGEV_KEVENT &&
1543 aiocbe->uaiocb.aio_sigevent.sigev_notify != SIGEV_SIGNAL &&
1544 aiocbe->uaiocb.aio_sigevent.sigev_notify != SIGEV_THREAD_ID &&
1545 aiocbe->uaiocb.aio_sigevent.sigev_notify != SIGEV_NONE) {
1546 ops->store_error(job, EINVAL);
1547 uma_zfree(aiocb_zone, aiocbe);
1551 if ((aiocbe->uaiocb.aio_sigevent.sigev_notify == SIGEV_SIGNAL ||
1552 aiocbe->uaiocb.aio_sigevent.sigev_notify == SIGEV_THREAD_ID) &&
1553 !_SIG_VALID(aiocbe->uaiocb.aio_sigevent.sigev_signo)) {
1554 uma_zfree(aiocb_zone, aiocbe);
1558 ksiginfo_init(&aiocbe->ksi);
1560 /* Save userspace address of the job info. */
1561 aiocbe->uuaiocb = job;
1563 /* Get the opcode. */
1564 if (type != LIO_NOP)
1565 aiocbe->uaiocb.aio_lio_opcode = type;
1566 opcode = aiocbe->uaiocb.aio_lio_opcode;
1568 /* Fetch the file object for the specified file descriptor. */
1569 fd = aiocbe->uaiocb.aio_fildes;
1572 error = fget_write(td, fd, &fp);
1575 error = fget_read(td, fd, &fp);
1578 error = fget(td, fd, &fp);
1581 uma_zfree(aiocb_zone, aiocbe);
1582 ops->store_error(job, error);
1586 if (opcode == LIO_SYNC && fp->f_vnode == NULL) {
1591 if (opcode != LIO_SYNC && aiocbe->uaiocb.aio_offset == -1LL) {
1596 aiocbe->fd_file = fp;
1598 mtx_lock(&aio_job_mtx);
1600 aiocbe->seqno = jobseqno++;
1601 mtx_unlock(&aio_job_mtx);
1602 error = ops->store_kernelinfo(job, jid);
1607 aiocbe->uaiocb._aiocb_private.kernelinfo = (void *)(intptr_t)jid;
1609 if (opcode == LIO_NOP) {
1611 uma_zfree(aiocb_zone, aiocbe);
1614 if ((opcode != LIO_READ) && (opcode != LIO_WRITE) &&
1615 (opcode != LIO_SYNC)) {
1620 if (aiocbe->uaiocb.aio_sigevent.sigev_notify != SIGEV_KEVENT)
1622 kqfd = aiocbe->uaiocb.aio_sigevent.sigev_notify_kqueue;
1623 kev.ident = (uintptr_t)aiocbe->uuaiocb;
1624 kev.filter = EVFILT_AIO;
1625 kev.flags = EV_ADD | EV_ENABLE | EV_FLAG1;
1626 kev.data = (intptr_t)aiocbe;
1627 kev.udata = aiocbe->uaiocb.aio_sigevent.sigev_value.sival_ptr;
1628 error = kqfd_register(kqfd, &kev, td, 1);
1632 uma_zfree(aiocb_zone, aiocbe);
1633 ops->store_error(job, error);
1638 ops->store_error(job, EINPROGRESS);
1639 aiocbe->uaiocb._aiocb_private.error = EINPROGRESS;
1640 aiocbe->userproc = p;
1641 aiocbe->cred = crhold(td->td_ucred);
1642 aiocbe->jobflags = 0;
1645 if (opcode == LIO_SYNC)
1648 if (fp->f_type == DTYPE_SOCKET) {
1650 * Alternate queueing for socket ops: Reach down into the
1651 * descriptor to get the socket data. Then check to see if the
1652 * socket is ready to be read or written (based on the requested
1655 * If it is not ready for io, then queue the aiocbe on the
1656 * socket, and set the flags so we get a call when sbnotify()
1659 * Note if opcode is neither LIO_WRITE nor LIO_READ we lock
1660 * and unlock the snd sockbuf for no reason.
1663 sb = (opcode == LIO_READ) ? &so->so_rcv : &so->so_snd;
1665 if (((opcode == LIO_READ) && (!soreadable(so))) || ((opcode ==
1666 LIO_WRITE) && (!sowriteable(so)))) {
1667 sb->sb_flags |= SB_AIO;
1669 mtx_lock(&aio_job_mtx);
1670 TAILQ_INSERT_TAIL(&so->so_aiojobq, aiocbe, list);
1671 mtx_unlock(&aio_job_mtx);
1674 TAILQ_INSERT_TAIL(&ki->kaio_all, aiocbe, allist);
1675 TAILQ_INSERT_TAIL(&ki->kaio_jobqueue, aiocbe, plist);
1676 aiocbe->jobstate = JOBST_JOBQSOCK;
1682 atomic_add_int(&num_queue_count, 1);
1689 if ((error = aio_qphysio(p, aiocbe)) == 0)
1693 aiocbe->uaiocb._aiocb_private.error = error;
1694 ops->store_error(job, error);
1699 /* No buffer for daemon I/O. */
1701 atomic_add_int(&num_queue_count, 1);
1707 TAILQ_INSERT_TAIL(&ki->kaio_jobqueue, aiocbe, plist);
1708 TAILQ_INSERT_TAIL(&ki->kaio_all, aiocbe, allist);
1709 if (opcode == LIO_SYNC) {
1710 TAILQ_FOREACH(cb, &ki->kaio_jobqueue, plist) {
1711 if (cb->fd_file == aiocbe->fd_file &&
1712 cb->uaiocb.aio_lio_opcode != LIO_SYNC &&
1713 cb->seqno < aiocbe->seqno) {
1714 cb->jobflags |= AIOCBLIST_CHECKSYNC;
1718 TAILQ_FOREACH(cb, &ki->kaio_bufqueue, plist) {
1719 if (cb->fd_file == aiocbe->fd_file &&
1720 cb->uaiocb.aio_lio_opcode != LIO_SYNC &&
1721 cb->seqno < aiocbe->seqno) {
1722 cb->jobflags |= AIOCBLIST_CHECKSYNC;
1726 if (aiocbe->pending != 0) {
1727 TAILQ_INSERT_TAIL(&ki->kaio_syncqueue, aiocbe, list);
1728 aiocbe->jobstate = JOBST_JOBQSYNC;
1733 mtx_lock(&aio_job_mtx);
1734 TAILQ_INSERT_TAIL(&aio_jobs, aiocbe, list);
1735 aiocbe->jobstate = JOBST_JOBQGLOBAL;
1737 mtx_unlock(&aio_job_mtx);
1745 aio_kick_nowait(struct proc *userp)
1747 struct kaioinfo *ki = userp->p_aioinfo;
1748 struct aiothreadlist *aiop;
1750 mtx_assert(&aio_job_mtx, MA_OWNED);
1751 if ((aiop = TAILQ_FIRST(&aio_freeproc)) != NULL) {
1752 TAILQ_REMOVE(&aio_freeproc, aiop, list);
1753 aiop->aiothreadflags &= ~AIOP_FREE;
1754 wakeup(aiop->aiothread);
1755 } else if (((num_aio_resv_start + num_aio_procs) < max_aio_procs) &&
1756 ((ki->kaio_active_count + num_aio_resv_start) <
1757 ki->kaio_maxactive_count)) {
1758 taskqueue_enqueue(taskqueue_aiod_bio, &ki->kaio_task);
1763 aio_kick(struct proc *userp)
1765 struct kaioinfo *ki = userp->p_aioinfo;
1766 struct aiothreadlist *aiop;
1769 mtx_assert(&aio_job_mtx, MA_OWNED);
1771 if ((aiop = TAILQ_FIRST(&aio_freeproc)) != NULL) {
1772 TAILQ_REMOVE(&aio_freeproc, aiop, list);
1773 aiop->aiothreadflags &= ~AIOP_FREE;
1774 wakeup(aiop->aiothread);
1775 } else if (((num_aio_resv_start + num_aio_procs) < max_aio_procs) &&
1776 ((ki->kaio_active_count + num_aio_resv_start) <
1777 ki->kaio_maxactive_count)) {
1778 num_aio_resv_start++;
1779 mtx_unlock(&aio_job_mtx);
1780 error = aio_newproc(&num_aio_resv_start);
1781 mtx_lock(&aio_job_mtx);
1783 num_aio_resv_start--;
1793 aio_kick_helper(void *context, int pending)
1795 struct proc *userp = context;
1797 mtx_lock(&aio_job_mtx);
1798 while (--pending >= 0) {
1799 if (aio_kick(userp))
1802 mtx_unlock(&aio_job_mtx);
1806 * Support the aio_return system call, as a side-effect, kernel resources are
1810 kern_aio_return(struct thread *td, struct aiocb *uaiocb, struct aiocb_ops *ops)
1812 struct proc *p = td->td_proc;
1813 struct aiocblist *cb;
1814 struct kaioinfo *ki;
1821 TAILQ_FOREACH(cb, &ki->kaio_done, plist) {
1822 if (cb->uuaiocb == uaiocb)
1826 MPASS(cb->jobstate == JOBST_JOBFINISHED);
1827 status = cb->uaiocb._aiocb_private.status;
1828 error = cb->uaiocb._aiocb_private.error;
1829 td->td_retval[0] = status;
1830 if (cb->uaiocb.aio_lio_opcode == LIO_WRITE) {
1831 td->td_ru.ru_oublock += cb->outputcharge;
1832 cb->outputcharge = 0;
1833 } else if (cb->uaiocb.aio_lio_opcode == LIO_READ) {
1834 td->td_ru.ru_inblock += cb->inputcharge;
1835 cb->inputcharge = 0;
1839 ops->store_error(uaiocb, error);
1840 ops->store_status(uaiocb, status);
1849 aio_return(struct thread *td, struct aio_return_args *uap)
1852 return (kern_aio_return(td, uap->aiocbp, &aiocb_ops));
1856 * Allow a process to wakeup when any of the I/O requests are completed.
1859 kern_aio_suspend(struct thread *td, int njoblist, struct aiocb **ujoblist,
1860 struct timespec *ts)
1862 struct proc *p = td->td_proc;
1864 struct kaioinfo *ki;
1865 struct aiocblist *cb, *cbfirst;
1870 if (ts->tv_nsec < 0 || ts->tv_nsec >= 1000000000)
1873 TIMESPEC_TO_TIMEVAL(&atv, ts);
1874 if (itimerfix(&atv))
1876 timo = tvtohz(&atv);
1890 TAILQ_FOREACH(cb, &ki->kaio_all, allist) {
1891 for (i = 0; i < njoblist; i++) {
1892 if (cb->uuaiocb == ujoblist[i]) {
1893 if (cbfirst == NULL)
1895 if (cb->jobstate == JOBST_JOBFINISHED)
1900 /* All tasks were finished. */
1901 if (cbfirst == NULL)
1904 ki->kaio_flags |= KAIO_WAKEUP;
1905 error = msleep(&p->p_aioinfo, AIO_MTX(ki), PRIBIO | PCATCH,
1907 if (error == ERESTART)
1918 aio_suspend(struct thread *td, struct aio_suspend_args *uap)
1920 struct timespec ts, *tsp;
1921 struct aiocb **ujoblist;
1924 if (uap->nent < 0 || uap->nent > AIO_LISTIO_MAX)
1928 /* Get timespec struct. */
1929 if ((error = copyin(uap->timeout, &ts, sizeof(ts))) != 0)
1935 ujoblist = uma_zalloc(aiol_zone, M_WAITOK);
1936 error = copyin(uap->aiocbp, ujoblist, uap->nent * sizeof(ujoblist[0]));
1938 error = kern_aio_suspend(td, uap->nent, ujoblist, tsp);
1939 uma_zfree(aiol_zone, ujoblist);
1944 * aio_cancel cancels any non-physio aio operations not currently in
1948 aio_cancel(struct thread *td, struct aio_cancel_args *uap)
1950 struct proc *p = td->td_proc;
1951 struct kaioinfo *ki;
1952 struct aiocblist *cbe, *cbn;
1958 int notcancelled = 0;
1961 /* Lookup file object. */
1962 error = fget(td, uap->fd, &fp);
1970 if (fp->f_type == DTYPE_VNODE) {
1972 if (vn_isdisk(vp, &error)) {
1974 td->td_retval[0] = AIO_NOTCANCELED;
1980 TAILQ_FOREACH_SAFE(cbe, &ki->kaio_jobqueue, plist, cbn) {
1981 if ((uap->fd == cbe->uaiocb.aio_fildes) &&
1982 ((uap->aiocbp == NULL) ||
1983 (uap->aiocbp == cbe->uuaiocb))) {
1986 mtx_lock(&aio_job_mtx);
1987 if (cbe->jobstate == JOBST_JOBQGLOBAL) {
1988 TAILQ_REMOVE(&aio_jobs, cbe, list);
1990 } else if (cbe->jobstate == JOBST_JOBQSOCK) {
1991 MPASS(fp->f_type == DTYPE_SOCKET);
1993 TAILQ_REMOVE(&so->so_aiojobq, cbe, list);
1995 } else if (cbe->jobstate == JOBST_JOBQSYNC) {
1996 TAILQ_REMOVE(&ki->kaio_syncqueue, cbe, list);
1999 mtx_unlock(&aio_job_mtx);
2002 TAILQ_REMOVE(&ki->kaio_jobqueue, cbe, plist);
2003 cbe->uaiocb._aiocb_private.status = -1;
2004 cbe->uaiocb._aiocb_private.error = ECANCELED;
2005 aio_bio_done_notify(p, cbe, DONE_QUEUE);
2010 if (uap->aiocbp != NULL)
2019 if (uap->aiocbp != NULL) {
2021 td->td_retval[0] = AIO_CANCELED;
2027 td->td_retval[0] = AIO_NOTCANCELED;
2032 td->td_retval[0] = AIO_CANCELED;
2036 td->td_retval[0] = AIO_ALLDONE;
2042 * aio_error is implemented in the kernel level for compatibility purposes
2043 * only. For a user mode async implementation, it would be best to do it in
2044 * a userland subroutine.
2047 kern_aio_error(struct thread *td, struct aiocb *aiocbp, struct aiocb_ops *ops)
2049 struct proc *p = td->td_proc;
2050 struct aiocblist *cb;
2051 struct kaioinfo *ki;
2056 td->td_retval[0] = EINVAL;
2061 TAILQ_FOREACH(cb, &ki->kaio_all, allist) {
2062 if (cb->uuaiocb == aiocbp) {
2063 if (cb->jobstate == JOBST_JOBFINISHED)
2065 cb->uaiocb._aiocb_private.error;
2067 td->td_retval[0] = EINPROGRESS;
2075 * Hack for failure of aio_aqueue.
2077 status = ops->fetch_status(aiocbp);
2079 td->td_retval[0] = ops->fetch_error(aiocbp);
2083 td->td_retval[0] = EINVAL;
2088 aio_error(struct thread *td, struct aio_error_args *uap)
2091 return (kern_aio_error(td, uap->aiocbp, &aiocb_ops));
2094 /* syscall - asynchronous read from a file (REALTIME) */
2096 oaio_read(struct thread *td, struct oaio_read_args *uap)
2099 return (aio_aqueue(td, (struct aiocb *)uap->aiocbp, NULL, LIO_READ,
2100 &aiocb_ops_osigevent));
2104 aio_read(struct thread *td, struct aio_read_args *uap)
2107 return (aio_aqueue(td, uap->aiocbp, NULL, LIO_READ, &aiocb_ops));
2110 /* syscall - asynchronous write to a file (REALTIME) */
2112 oaio_write(struct thread *td, struct oaio_write_args *uap)
2115 return (aio_aqueue(td, (struct aiocb *)uap->aiocbp, NULL, LIO_WRITE,
2116 &aiocb_ops_osigevent));
2120 aio_write(struct thread *td, struct aio_write_args *uap)
2123 return (aio_aqueue(td, uap->aiocbp, NULL, LIO_WRITE, &aiocb_ops));
2127 kern_lio_listio(struct thread *td, int mode, struct aiocb * const *uacb_list,
2128 struct aiocb **acb_list, int nent, struct sigevent *sig,
2129 struct aiocb_ops *ops)
2131 struct proc *p = td->td_proc;
2133 struct kaioinfo *ki;
2134 struct aioliojob *lj;
2140 if ((mode != LIO_NOWAIT) && (mode != LIO_WAIT))
2143 if (nent < 0 || nent > AIO_LISTIO_MAX)
2146 if (p->p_aioinfo == NULL)
2147 aio_init_aioinfo(p);
2151 lj = uma_zalloc(aiolio_zone, M_WAITOK);
2154 lj->lioj_finished_count = 0;
2155 knlist_init_mtx(&lj->klist, AIO_MTX(ki));
2156 ksiginfo_init(&lj->lioj_ksi);
2161 if (sig && (mode == LIO_NOWAIT)) {
2162 bcopy(sig, &lj->lioj_signal, sizeof(lj->lioj_signal));
2163 if (lj->lioj_signal.sigev_notify == SIGEV_KEVENT) {
2164 /* Assume only new style KEVENT */
2165 kev.filter = EVFILT_LIO;
2166 kev.flags = EV_ADD | EV_ENABLE | EV_FLAG1;
2167 kev.ident = (uintptr_t)uacb_list; /* something unique */
2168 kev.data = (intptr_t)lj;
2169 /* pass user defined sigval data */
2170 kev.udata = lj->lioj_signal.sigev_value.sival_ptr;
2171 error = kqfd_register(
2172 lj->lioj_signal.sigev_notify_kqueue, &kev, td, 1);
2174 uma_zfree(aiolio_zone, lj);
2177 } else if (lj->lioj_signal.sigev_notify == SIGEV_NONE) {
2179 } else if (lj->lioj_signal.sigev_notify == SIGEV_SIGNAL ||
2180 lj->lioj_signal.sigev_notify == SIGEV_THREAD_ID) {
2181 if (!_SIG_VALID(lj->lioj_signal.sigev_signo)) {
2182 uma_zfree(aiolio_zone, lj);
2185 lj->lioj_flags |= LIOJ_SIGNAL;
2187 uma_zfree(aiolio_zone, lj);
2193 TAILQ_INSERT_TAIL(&ki->kaio_liojoblist, lj, lioj_list);
2195 * Add extra aiocb count to avoid the lio to be freed
2196 * by other threads doing aio_waitcomplete or aio_return,
2197 * and prevent event from being sent until we have queued
2204 * Get pointers to the list of I/O requests.
2207 for (i = 0; i < nent; i++) {
2210 error = aio_aqueue(td, iocb, lj, LIO_NOP, ops);
2218 if (mode == LIO_WAIT) {
2219 while (lj->lioj_count - 1 != lj->lioj_finished_count) {
2220 ki->kaio_flags |= KAIO_WAKEUP;
2221 error = msleep(&p->p_aioinfo, AIO_MTX(ki),
2222 PRIBIO | PCATCH, "aiospn", 0);
2223 if (error == ERESTART)
2229 if (lj->lioj_count - 1 == lj->lioj_finished_count) {
2230 if (lj->lioj_signal.sigev_notify == SIGEV_KEVENT) {
2231 lj->lioj_flags |= LIOJ_KEVENT_POSTED;
2232 KNOTE_LOCKED(&lj->klist, 1);
2234 if ((lj->lioj_flags & (LIOJ_SIGNAL|LIOJ_SIGNAL_POSTED))
2236 && (lj->lioj_signal.sigev_notify == SIGEV_SIGNAL ||
2237 lj->lioj_signal.sigev_notify == SIGEV_THREAD_ID)) {
2238 aio_sendsig(p, &lj->lioj_signal,
2240 lj->lioj_flags |= LIOJ_SIGNAL_POSTED;
2245 if (lj->lioj_count == 0) {
2246 TAILQ_REMOVE(&ki->kaio_liojoblist, lj, lioj_list);
2247 knlist_delete(&lj->klist, curthread, 1);
2249 sigqueue_take(&lj->lioj_ksi);
2252 uma_zfree(aiolio_zone, lj);
2261 /* syscall - list directed I/O (REALTIME) */
2263 olio_listio(struct thread *td, struct olio_listio_args *uap)
2265 struct aiocb **acb_list;
2266 struct sigevent *sigp, sig;
2267 struct osigevent osig;
2270 if ((uap->mode != LIO_NOWAIT) && (uap->mode != LIO_WAIT))
2274 if (nent < 0 || nent > AIO_LISTIO_MAX)
2277 if (uap->sig && (uap->mode == LIO_NOWAIT)) {
2278 error = copyin(uap->sig, &osig, sizeof(osig));
2281 error = convert_old_sigevent(&osig, &sig);
2288 acb_list = malloc(sizeof(struct aiocb *) * nent, M_LIO, M_WAITOK);
2289 error = copyin(uap->acb_list, acb_list, nent * sizeof(acb_list[0]));
2291 error = kern_lio_listio(td, uap->mode,
2292 (struct aiocb * const *)uap->acb_list, acb_list, nent, sigp,
2293 &aiocb_ops_osigevent);
2294 free(acb_list, M_LIO);
2298 /* syscall - list directed I/O (REALTIME) */
2300 lio_listio(struct thread *td, struct lio_listio_args *uap)
2302 struct aiocb **acb_list;
2303 struct sigevent *sigp, sig;
2306 if ((uap->mode != LIO_NOWAIT) && (uap->mode != LIO_WAIT))
2310 if (nent < 0 || nent > AIO_LISTIO_MAX)
2313 if (uap->sig && (uap->mode == LIO_NOWAIT)) {
2314 error = copyin(uap->sig, &sig, sizeof(sig));
2321 acb_list = malloc(sizeof(struct aiocb *) * nent, M_LIO, M_WAITOK);
2322 error = copyin(uap->acb_list, acb_list, nent * sizeof(acb_list[0]));
2324 error = kern_lio_listio(td, uap->mode, uap->acb_list, acb_list,
2325 nent, sigp, &aiocb_ops);
2326 free(acb_list, M_LIO);
2331 * Called from interrupt thread for physio, we should return as fast
2332 * as possible, so we schedule a biohelper task.
2335 aio_physwakeup(struct buf *bp)
2337 struct aiocblist *aiocbe;
2339 aiocbe = (struct aiocblist *)bp->b_caller1;
2340 taskqueue_enqueue(taskqueue_aiod_bio, &aiocbe->biotask);
2344 * Task routine to perform heavy tasks, process wakeup, and signals.
2347 biohelper(void *context, int pending)
2349 struct aiocblist *aiocbe = context;
2352 struct kaioinfo *ki;
2356 userp = aiocbe->userproc;
2357 ki = userp->p_aioinfo;
2359 aiocbe->uaiocb._aiocb_private.status -= bp->b_resid;
2360 aiocbe->uaiocb._aiocb_private.error = 0;
2361 if (bp->b_ioflags & BIO_ERROR)
2362 aiocbe->uaiocb._aiocb_private.error = bp->b_error;
2363 nblks = btodb(aiocbe->uaiocb.aio_nbytes);
2364 if (aiocbe->uaiocb.aio_lio_opcode == LIO_WRITE)
2365 aiocbe->outputcharge += nblks;
2367 aiocbe->inputcharge += nblks;
2369 TAILQ_REMOVE(&userp->p_aioinfo->kaio_bufqueue, aiocbe, plist);
2370 ki->kaio_buffer_count--;
2371 aio_bio_done_notify(userp, aiocbe, DONE_BUF);
2374 /* Release mapping into kernel space. */
2377 atomic_subtract_int(&num_buf_aio, 1);
2380 /* syscall - wait for the next completion of an aio request */
2382 kern_aio_waitcomplete(struct thread *td, struct aiocb **aiocbp,
2383 struct timespec *ts, struct aiocb_ops *ops)
2385 struct proc *p = td->td_proc;
2387 struct kaioinfo *ki;
2388 struct aiocblist *cb;
2389 struct aiocb *uuaiocb;
2390 int error, status, timo;
2392 ops->store_aiocb(aiocbp, NULL);
2396 if ((ts->tv_nsec < 0) || (ts->tv_nsec >= 1000000000))
2399 TIMESPEC_TO_TIMEVAL(&atv, ts);
2400 if (itimerfix(&atv))
2402 timo = tvtohz(&atv);
2405 if (p->p_aioinfo == NULL)
2406 aio_init_aioinfo(p);
2412 while ((cb = TAILQ_FIRST(&ki->kaio_done)) == NULL) {
2413 ki->kaio_flags |= KAIO_WAKEUP;
2414 error = msleep(&p->p_aioinfo, AIO_MTX(ki), PRIBIO | PCATCH,
2416 if (timo && error == ERESTART)
2423 MPASS(cb->jobstate == JOBST_JOBFINISHED);
2424 uuaiocb = cb->uuaiocb;
2425 status = cb->uaiocb._aiocb_private.status;
2426 error = cb->uaiocb._aiocb_private.error;
2427 td->td_retval[0] = status;
2428 if (cb->uaiocb.aio_lio_opcode == LIO_WRITE) {
2429 td->td_ru.ru_oublock += cb->outputcharge;
2430 cb->outputcharge = 0;
2431 } else if (cb->uaiocb.aio_lio_opcode == LIO_READ) {
2432 td->td_ru.ru_inblock += cb->inputcharge;
2433 cb->inputcharge = 0;
2437 ops->store_aiocb(aiocbp, uuaiocb);
2438 ops->store_error(uuaiocb, error);
2439 ops->store_status(uuaiocb, status);
2447 aio_waitcomplete(struct thread *td, struct aio_waitcomplete_args *uap)
2449 struct timespec ts, *tsp;
2453 /* Get timespec struct. */
2454 error = copyin(uap->timeout, &ts, sizeof(ts));
2461 return (kern_aio_waitcomplete(td, uap->aiocbp, tsp, &aiocb_ops));
2465 kern_aio_fsync(struct thread *td, int op, struct aiocb *aiocbp,
2466 struct aiocb_ops *ops)
2468 struct proc *p = td->td_proc;
2469 struct kaioinfo *ki;
2471 if (op != O_SYNC) /* XXX lack of O_DSYNC */
2475 aio_init_aioinfo(p);
2476 return (aio_aqueue(td, aiocbp, NULL, LIO_SYNC, ops));
2480 aio_fsync(struct thread *td, struct aio_fsync_args *uap)
2483 return (kern_aio_fsync(td, uap->op, uap->aiocbp, &aiocb_ops));
2486 /* kqueue attach function */
2488 filt_aioattach(struct knote *kn)
2490 struct aiocblist *aiocbe = (struct aiocblist *)kn->kn_sdata;
2493 * The aiocbe pointer must be validated before using it, so
2494 * registration is restricted to the kernel; the user cannot
2497 if ((kn->kn_flags & EV_FLAG1) == 0)
2499 kn->kn_ptr.p_aio = aiocbe;
2500 kn->kn_flags &= ~EV_FLAG1;
2502 knlist_add(&aiocbe->klist, kn, 0);
2507 /* kqueue detach function */
2509 filt_aiodetach(struct knote *kn)
2511 struct aiocblist *aiocbe = kn->kn_ptr.p_aio;
2513 if (!knlist_empty(&aiocbe->klist))
2514 knlist_remove(&aiocbe->klist, kn, 0);
2517 /* kqueue filter function */
2520 filt_aio(struct knote *kn, long hint)
2522 struct aiocblist *aiocbe = kn->kn_ptr.p_aio;
2524 kn->kn_data = aiocbe->uaiocb._aiocb_private.error;
2525 if (aiocbe->jobstate != JOBST_JOBFINISHED)
2527 kn->kn_flags |= EV_EOF;
2531 /* kqueue attach function */
2533 filt_lioattach(struct knote *kn)
2535 struct aioliojob * lj = (struct aioliojob *)kn->kn_sdata;
2538 * The aioliojob pointer must be validated before using it, so
2539 * registration is restricted to the kernel; the user cannot
2542 if ((kn->kn_flags & EV_FLAG1) == 0)
2544 kn->kn_ptr.p_lio = lj;
2545 kn->kn_flags &= ~EV_FLAG1;
2547 knlist_add(&lj->klist, kn, 0);
2552 /* kqueue detach function */
2554 filt_liodetach(struct knote *kn)
2556 struct aioliojob * lj = kn->kn_ptr.p_lio;
2558 if (!knlist_empty(&lj->klist))
2559 knlist_remove(&lj->klist, kn, 0);
2562 /* kqueue filter function */
2565 filt_lio(struct knote *kn, long hint)
2567 struct aioliojob * lj = kn->kn_ptr.p_lio;
2569 return (lj->lioj_flags & LIOJ_KEVENT_POSTED);
2572 #ifdef COMPAT_FREEBSD32
2574 struct __aiocb_private32 {
2577 uint32_t kernelinfo;
2580 typedef struct oaiocb32 {
2581 int aio_fildes; /* File descriptor */
2582 uint64_t aio_offset __packed; /* File offset for I/O */
2583 uint32_t aio_buf; /* I/O buffer in process space */
2584 uint32_t aio_nbytes; /* Number of bytes for I/O */
2585 struct osigevent32 aio_sigevent; /* Signal to deliver */
2586 int aio_lio_opcode; /* LIO opcode */
2587 int aio_reqprio; /* Request priority -- ignored */
2588 struct __aiocb_private32 _aiocb_private;
2591 typedef struct aiocb32 {
2592 int32_t aio_fildes; /* File descriptor */
2593 uint64_t aio_offset __packed; /* File offset for I/O */
2594 uint32_t aio_buf; /* I/O buffer in process space */
2595 uint32_t aio_nbytes; /* Number of bytes for I/O */
2597 uint32_t __spare2__;
2598 int aio_lio_opcode; /* LIO opcode */
2599 int aio_reqprio; /* Request priority -- ignored */
2600 struct __aiocb_private32 _aiocb_private;
2601 struct sigevent32 aio_sigevent; /* Signal to deliver */
2605 convert_old_sigevent32(struct osigevent32 *osig, struct sigevent *nsig)
2609 * Only SIGEV_NONE, SIGEV_SIGNAL, and SIGEV_KEVENT are
2610 * supported by AIO with the old sigevent structure.
2612 CP(*osig, *nsig, sigev_notify);
2613 switch (nsig->sigev_notify) {
2617 nsig->sigev_signo = osig->__sigev_u.__sigev_signo;
2620 nsig->sigev_notify_kqueue =
2621 osig->__sigev_u.__sigev_notify_kqueue;
2622 PTRIN_CP(*osig, *nsig, sigev_value.sival_ptr);
2631 aiocb32_copyin_old_sigevent(struct aiocb *ujob, struct aiocb *kjob)
2633 struct oaiocb32 job32;
2636 bzero(kjob, sizeof(struct aiocb));
2637 error = copyin(ujob, &job32, sizeof(job32));
2641 CP(job32, *kjob, aio_fildes);
2642 CP(job32, *kjob, aio_offset);
2643 PTRIN_CP(job32, *kjob, aio_buf);
2644 CP(job32, *kjob, aio_nbytes);
2645 CP(job32, *kjob, aio_lio_opcode);
2646 CP(job32, *kjob, aio_reqprio);
2647 CP(job32, *kjob, _aiocb_private.status);
2648 CP(job32, *kjob, _aiocb_private.error);
2649 PTRIN_CP(job32, *kjob, _aiocb_private.kernelinfo);
2650 return (convert_old_sigevent32(&job32.aio_sigevent,
2651 &kjob->aio_sigevent));
2655 convert_sigevent32(struct sigevent32 *sig32, struct sigevent *sig)
2658 CP(*sig32, *sig, sigev_notify);
2659 switch (sig->sigev_notify) {
2662 case SIGEV_THREAD_ID:
2663 CP(*sig32, *sig, sigev_notify_thread_id);
2666 CP(*sig32, *sig, sigev_signo);
2669 CP(*sig32, *sig, sigev_notify_kqueue);
2670 PTRIN_CP(*sig32, *sig, sigev_value.sival_ptr);
2679 aiocb32_copyin(struct aiocb *ujob, struct aiocb *kjob)
2681 struct aiocb32 job32;
2684 error = copyin(ujob, &job32, sizeof(job32));
2687 CP(job32, *kjob, aio_fildes);
2688 CP(job32, *kjob, aio_offset);
2689 PTRIN_CP(job32, *kjob, aio_buf);
2690 CP(job32, *kjob, aio_nbytes);
2691 CP(job32, *kjob, aio_lio_opcode);
2692 CP(job32, *kjob, aio_reqprio);
2693 CP(job32, *kjob, _aiocb_private.status);
2694 CP(job32, *kjob, _aiocb_private.error);
2695 PTRIN_CP(job32, *kjob, _aiocb_private.kernelinfo);
2696 return (convert_sigevent32(&job32.aio_sigevent, &kjob->aio_sigevent));
2700 aiocb32_fetch_status(struct aiocb *ujob)
2702 struct aiocb32 *ujob32;
2704 ujob32 = (struct aiocb32 *)ujob;
2705 return (fuword32(&ujob32->_aiocb_private.status));
2709 aiocb32_fetch_error(struct aiocb *ujob)
2711 struct aiocb32 *ujob32;
2713 ujob32 = (struct aiocb32 *)ujob;
2714 return (fuword32(&ujob32->_aiocb_private.error));
2718 aiocb32_store_status(struct aiocb *ujob, long status)
2720 struct aiocb32 *ujob32;
2722 ujob32 = (struct aiocb32 *)ujob;
2723 return (suword32(&ujob32->_aiocb_private.status, status));
2727 aiocb32_store_error(struct aiocb *ujob, long error)
2729 struct aiocb32 *ujob32;
2731 ujob32 = (struct aiocb32 *)ujob;
2732 return (suword32(&ujob32->_aiocb_private.error, error));
2736 aiocb32_store_kernelinfo(struct aiocb *ujob, long jobref)
2738 struct aiocb32 *ujob32;
2740 ujob32 = (struct aiocb32 *)ujob;
2741 return (suword32(&ujob32->_aiocb_private.kernelinfo, jobref));
2745 aiocb32_store_aiocb(struct aiocb **ujobp, struct aiocb *ujob)
2748 return (suword32(ujobp, (long)ujob));
2751 static struct aiocb_ops aiocb32_ops = {
2752 .copyin = aiocb32_copyin,
2753 .fetch_status = aiocb32_fetch_status,
2754 .fetch_error = aiocb32_fetch_error,
2755 .store_status = aiocb32_store_status,
2756 .store_error = aiocb32_store_error,
2757 .store_kernelinfo = aiocb32_store_kernelinfo,
2758 .store_aiocb = aiocb32_store_aiocb,
2761 static struct aiocb_ops aiocb32_ops_osigevent = {
2762 .copyin = aiocb32_copyin_old_sigevent,
2763 .fetch_status = aiocb32_fetch_status,
2764 .fetch_error = aiocb32_fetch_error,
2765 .store_status = aiocb32_store_status,
2766 .store_error = aiocb32_store_error,
2767 .store_kernelinfo = aiocb32_store_kernelinfo,
2768 .store_aiocb = aiocb32_store_aiocb,
2772 freebsd32_aio_return(struct thread *td, struct freebsd32_aio_return_args *uap)
2775 return (kern_aio_return(td, (struct aiocb *)uap->aiocbp, &aiocb32_ops));
2779 freebsd32_aio_suspend(struct thread *td, struct freebsd32_aio_suspend_args *uap)
2781 struct timespec32 ts32;
2782 struct timespec ts, *tsp;
2783 struct aiocb **ujoblist;
2784 uint32_t *ujoblist32;
2787 if (uap->nent < 0 || uap->nent > AIO_LISTIO_MAX)
2791 /* Get timespec struct. */
2792 if ((error = copyin(uap->timeout, &ts32, sizeof(ts32))) != 0)
2794 CP(ts32, ts, tv_sec);
2795 CP(ts32, ts, tv_nsec);
2800 ujoblist = uma_zalloc(aiol_zone, M_WAITOK);
2801 ujoblist32 = (uint32_t *)ujoblist;
2802 error = copyin(uap->aiocbp, ujoblist32, uap->nent *
2803 sizeof(ujoblist32[0]));
2805 for (i = uap->nent; i > 0; i--)
2806 ujoblist[i] = PTRIN(ujoblist32[i]);
2808 error = kern_aio_suspend(td, uap->nent, ujoblist, tsp);
2810 uma_zfree(aiol_zone, ujoblist);
2815 freebsd32_aio_cancel(struct thread *td, struct freebsd32_aio_cancel_args *uap)
2818 return (aio_cancel(td, (struct aio_cancel_args *)uap));
2822 freebsd32_aio_error(struct thread *td, struct freebsd32_aio_error_args *uap)
2825 return (kern_aio_error(td, (struct aiocb *)uap->aiocbp, &aiocb32_ops));
2829 freebsd32_oaio_read(struct thread *td, struct freebsd32_oaio_read_args *uap)
2832 return (aio_aqueue(td, (struct aiocb *)uap->aiocbp, NULL, LIO_READ,
2833 &aiocb32_ops_osigevent));
2837 freebsd32_aio_read(struct thread *td, struct freebsd32_aio_read_args *uap)
2840 return (aio_aqueue(td, (struct aiocb *)uap->aiocbp, NULL, LIO_READ,
2845 freebsd32_oaio_write(struct thread *td, struct freebsd32_oaio_write_args *uap)
2848 return (aio_aqueue(td, (struct aiocb *)uap->aiocbp, NULL, LIO_WRITE,
2849 &aiocb32_ops_osigevent));
2853 freebsd32_aio_write(struct thread *td, struct freebsd32_aio_write_args *uap)
2856 return (aio_aqueue(td, (struct aiocb *)uap->aiocbp, NULL, LIO_WRITE,
2861 freebsd32_aio_waitcomplete(struct thread *td,
2862 struct freebsd32_aio_waitcomplete_args *uap)
2864 struct timespec32 ts32;
2865 struct timespec ts, *tsp;
2869 /* Get timespec struct. */
2870 error = copyin(uap->timeout, &ts32, sizeof(ts32));
2873 CP(ts32, ts, tv_sec);
2874 CP(ts32, ts, tv_nsec);
2879 return (kern_aio_waitcomplete(td, (struct aiocb **)uap->aiocbp, tsp,
2884 freebsd32_aio_fsync(struct thread *td, struct freebsd32_aio_fsync_args *uap)
2887 return (kern_aio_fsync(td, uap->op, (struct aiocb *)uap->aiocbp,
2892 freebsd32_olio_listio(struct thread *td, struct freebsd32_olio_listio_args *uap)
2894 struct aiocb **acb_list;
2895 struct sigevent *sigp, sig;
2896 struct osigevent32 osig;
2897 uint32_t *acb_list32;
2900 if ((uap->mode != LIO_NOWAIT) && (uap->mode != LIO_WAIT))
2904 if (nent < 0 || nent > AIO_LISTIO_MAX)
2907 if (uap->sig && (uap->mode == LIO_NOWAIT)) {
2908 error = copyin(uap->sig, &osig, sizeof(osig));
2911 error = convert_old_sigevent32(&osig, &sig);
2918 acb_list32 = malloc(sizeof(uint32_t) * nent, M_LIO, M_WAITOK);
2919 error = copyin(uap->acb_list, acb_list32, nent * sizeof(uint32_t));
2921 free(acb_list32, M_LIO);
2924 acb_list = malloc(sizeof(struct aiocb *) * nent, M_LIO, M_WAITOK);
2925 for (i = 0; i < nent; i++)
2926 acb_list[i] = PTRIN(acb_list32[i]);
2927 free(acb_list32, M_LIO);
2929 error = kern_lio_listio(td, uap->mode,
2930 (struct aiocb * const *)uap->acb_list, acb_list, nent, sigp,
2931 &aiocb32_ops_osigevent);
2932 free(acb_list, M_LIO);
2937 freebsd32_lio_listio(struct thread *td, struct freebsd32_lio_listio_args *uap)
2939 struct aiocb **acb_list;
2940 struct sigevent *sigp, sig;
2941 struct sigevent32 sig32;
2942 uint32_t *acb_list32;
2945 if ((uap->mode != LIO_NOWAIT) && (uap->mode != LIO_WAIT))
2949 if (nent < 0 || nent > AIO_LISTIO_MAX)
2952 if (uap->sig && (uap->mode == LIO_NOWAIT)) {
2953 error = copyin(uap->sig, &sig32, sizeof(sig32));
2956 error = convert_sigevent32(&sig32, &sig);
2963 acb_list32 = malloc(sizeof(uint32_t) * nent, M_LIO, M_WAITOK);
2964 error = copyin(uap->acb_list, acb_list32, nent * sizeof(uint32_t));
2966 free(acb_list32, M_LIO);
2969 acb_list = malloc(sizeof(struct aiocb *) * nent, M_LIO, M_WAITOK);
2970 for (i = 0; i < nent; i++)
2971 acb_list[i] = PTRIN(acb_list32[i]);
2972 free(acb_list32, M_LIO);
2974 error = kern_lio_listio(td, uap->mode,
2975 (struct aiocb * const *)uap->acb_list, acb_list, nent, sigp,
2977 free(acb_list, M_LIO);