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/capsicum.h>
32 #include <sys/eventhandler.h>
33 #include <sys/sysproto.h>
34 #include <sys/filedesc.h>
35 #include <sys/kernel.h>
36 #include <sys/module.h>
37 #include <sys/kthread.h>
38 #include <sys/fcntl.h>
40 #include <sys/limits.h>
42 #include <sys/mutex.h>
43 #include <sys/unistd.h>
44 #include <sys/posix4.h>
46 #include <sys/resourcevar.h>
47 #include <sys/signalvar.h>
48 #include <sys/protosw.h>
49 #include <sys/rwlock.h>
51 #include <sys/socket.h>
52 #include <sys/socketvar.h>
53 #include <sys/syscall.h>
54 #include <sys/sysent.h>
55 #include <sys/sysctl.h>
57 #include <sys/taskqueue.h>
58 #include <sys/vnode.h>
60 #include <sys/event.h>
61 #include <sys/mount.h>
62 #include <geom/geom.h>
64 #include <machine/atomic.h>
67 #include <vm/vm_page.h>
68 #include <vm/vm_extern.h>
70 #include <vm/vm_map.h>
71 #include <vm/vm_object.h>
75 #include "opt_vfs_aio.h"
78 * Counter for allocating reference ids to new jobs. Wrapped to 1 on
79 * overflow. (XXX will be removed soon.)
81 static u_long jobrefid;
84 * Counter for aio_fsync.
86 static uint64_t jobseqno;
89 #define JOBST_JOBQSOCK 1
90 #define JOBST_JOBQGLOBAL 2
91 #define JOBST_JOBRUNNING 3
92 #define JOBST_JOBFINISHED 4
93 #define JOBST_JOBQBUF 5
94 #define JOBST_JOBQSYNC 6
96 #ifndef MAX_AIO_PER_PROC
97 #define MAX_AIO_PER_PROC 32
100 #ifndef MAX_AIO_QUEUE_PER_PROC
101 #define MAX_AIO_QUEUE_PER_PROC 256 /* Bigger than AIO_LISTIO_MAX */
104 #ifndef MAX_AIO_PROCS
105 #define MAX_AIO_PROCS 32
108 #ifndef MAX_AIO_QUEUE
109 #define MAX_AIO_QUEUE 1024 /* Bigger than AIO_LISTIO_MAX */
112 #ifndef TARGET_AIO_PROCS
113 #define TARGET_AIO_PROCS 4
117 #define MAX_BUF_AIO 16
120 #ifndef AIOD_TIMEOUT_DEFAULT
121 #define AIOD_TIMEOUT_DEFAULT (10 * hz)
124 #ifndef AIOD_LIFETIME_DEFAULT
125 #define AIOD_LIFETIME_DEFAULT (30 * hz)
128 FEATURE(aio, "Asynchronous I/O");
130 static MALLOC_DEFINE(M_LIO, "lio", "listio aio control block list");
132 static SYSCTL_NODE(_vfs, OID_AUTO, aio, CTLFLAG_RW, 0, "Async IO management");
134 static int max_aio_procs = MAX_AIO_PROCS;
135 SYSCTL_INT(_vfs_aio, OID_AUTO, max_aio_procs,
136 CTLFLAG_RW, &max_aio_procs, 0,
137 "Maximum number of kernel threads to use for handling async IO ");
139 static int num_aio_procs = 0;
140 SYSCTL_INT(_vfs_aio, OID_AUTO, num_aio_procs,
141 CTLFLAG_RD, &num_aio_procs, 0,
142 "Number of presently active kernel threads for async IO");
145 * The code will adjust the actual number of AIO processes towards this
146 * number when it gets a chance.
148 static int target_aio_procs = TARGET_AIO_PROCS;
149 SYSCTL_INT(_vfs_aio, OID_AUTO, target_aio_procs, CTLFLAG_RW, &target_aio_procs,
150 0, "Preferred number of ready kernel threads for async IO");
152 static int max_queue_count = MAX_AIO_QUEUE;
153 SYSCTL_INT(_vfs_aio, OID_AUTO, max_aio_queue, CTLFLAG_RW, &max_queue_count, 0,
154 "Maximum number of aio requests to queue, globally");
156 static int num_queue_count = 0;
157 SYSCTL_INT(_vfs_aio, OID_AUTO, num_queue_count, CTLFLAG_RD, &num_queue_count, 0,
158 "Number of queued aio requests");
160 static int num_buf_aio = 0;
161 SYSCTL_INT(_vfs_aio, OID_AUTO, num_buf_aio, CTLFLAG_RD, &num_buf_aio, 0,
162 "Number of aio requests presently handled by the buf subsystem");
164 /* Number of async I/O thread in the process of being started */
165 /* XXX This should be local to aio_aqueue() */
166 static int num_aio_resv_start = 0;
168 static int aiod_timeout;
169 SYSCTL_INT(_vfs_aio, OID_AUTO, aiod_timeout, CTLFLAG_RW, &aiod_timeout, 0,
170 "Timeout value for synchronous aio operations");
172 static int aiod_lifetime;
173 SYSCTL_INT(_vfs_aio, OID_AUTO, aiod_lifetime, CTLFLAG_RW, &aiod_lifetime, 0,
174 "Maximum lifetime for idle aiod");
176 static int unloadable = 0;
177 SYSCTL_INT(_vfs_aio, OID_AUTO, unloadable, CTLFLAG_RW, &unloadable, 0,
178 "Allow unload of aio (not recommended)");
181 static int max_aio_per_proc = MAX_AIO_PER_PROC;
182 SYSCTL_INT(_vfs_aio, OID_AUTO, max_aio_per_proc, CTLFLAG_RW, &max_aio_per_proc,
183 0, "Maximum active aio requests per process (stored in the process)");
185 static int max_aio_queue_per_proc = MAX_AIO_QUEUE_PER_PROC;
186 SYSCTL_INT(_vfs_aio, OID_AUTO, max_aio_queue_per_proc, CTLFLAG_RW,
187 &max_aio_queue_per_proc, 0,
188 "Maximum queued aio requests per process (stored in the process)");
190 static int max_buf_aio = MAX_BUF_AIO;
191 SYSCTL_INT(_vfs_aio, OID_AUTO, max_buf_aio, CTLFLAG_RW, &max_buf_aio, 0,
192 "Maximum buf aio requests per process (stored in the process)");
194 typedef struct oaiocb {
195 int aio_fildes; /* File descriptor */
196 off_t aio_offset; /* File offset for I/O */
197 volatile void *aio_buf; /* I/O buffer in process space */
198 size_t aio_nbytes; /* Number of bytes for I/O */
199 struct osigevent aio_sigevent; /* Signal to deliver */
200 int aio_lio_opcode; /* LIO opcode */
201 int aio_reqprio; /* Request priority -- ignored */
202 struct __aiocb_private _aiocb_private;
206 * Below is a key of locks used to protect each member of struct aiocblist
207 * aioliojob and kaioinfo and any backends.
209 * * - need not protected
210 * a - locked by kaioinfo lock
211 * b - locked by backend lock, the backend lock can be null in some cases,
212 * for example, BIO belongs to this type, in this case, proc lock is
214 * c - locked by aio_job_mtx, the lock for the generic file I/O backend.
218 * Current, there is only two backends: BIO and generic file I/O.
219 * socket I/O is served by generic file I/O, this is not a good idea, since
220 * disk file I/O and any other types without O_NONBLOCK flag can block daemon
221 * threads, if there is no thread to serve socket I/O, the socket I/O will be
222 * delayed too long or starved, we should create some threads dedicated to
223 * sockets to do non-blocking I/O, same for pipe and fifo, for these I/O
224 * systems we really need non-blocking interface, fiddling O_NONBLOCK in file
225 * structure is not safe because there is race between userland and aio
230 TAILQ_ENTRY(aiocblist) list; /* (b) internal list of for backend */
231 TAILQ_ENTRY(aiocblist) plist; /* (a) list of jobs for each backend */
232 TAILQ_ENTRY(aiocblist) allist; /* (a) list of all jobs in proc */
233 int jobflags; /* (a) job flags */
234 int jobstate; /* (b) job state */
235 int inputcharge; /* (*) input blockes */
236 int outputcharge; /* (*) output blockes */
237 struct bio *bp; /* (*) BIO backend BIO pointer */
238 struct buf *pbuf; /* (*) BIO backend buffer pointer */
239 struct vm_page *pages[btoc(MAXPHYS)+1]; /* BIO backend pages */
240 int npages; /* BIO backend number of pages */
241 struct proc *userproc; /* (*) user process */
242 struct ucred *cred; /* (*) active credential when created */
243 struct file *fd_file; /* (*) pointer to file structure */
244 struct aioliojob *lio; /* (*) optional lio job */
245 struct aiocb *uuaiocb; /* (*) pointer in userspace of aiocb */
246 struct knlist klist; /* (a) list of knotes */
247 struct aiocb uaiocb; /* (*) kernel I/O control block */
248 ksiginfo_t ksi; /* (a) realtime signal info */
249 uint64_t seqno; /* (*) job number */
250 int pending; /* (a) number of pending I/O, aio_fsync only */
254 #define AIOCBLIST_DONE 0x01
255 #define AIOCBLIST_BUFDONE 0x02
256 #define AIOCBLIST_RUNDOWN 0x04
257 #define AIOCBLIST_CHECKSYNC 0x08
262 #define AIOP_FREE 0x1 /* proc on free queue */
264 struct aiothreadlist {
265 int aiothreadflags; /* (c) AIO proc flags */
266 TAILQ_ENTRY(aiothreadlist) list; /* (c) list of processes */
267 struct thread *aiothread; /* (*) the AIO thread */
271 * data-structure for lio signal management
274 int lioj_flags; /* (a) listio flags */
275 int lioj_count; /* (a) listio flags */
276 int lioj_finished_count; /* (a) listio flags */
277 struct sigevent lioj_signal; /* (a) signal on all I/O done */
278 TAILQ_ENTRY(aioliojob) lioj_list; /* (a) lio list */
279 struct knlist klist; /* (a) list of knotes */
280 ksiginfo_t lioj_ksi; /* (a) Realtime signal info */
283 #define LIOJ_SIGNAL 0x1 /* signal on all done (lio) */
284 #define LIOJ_SIGNAL_POSTED 0x2 /* signal has been posted */
285 #define LIOJ_KEVENT_POSTED 0x4 /* kevent triggered */
288 * per process aio data structure
291 struct mtx kaio_mtx; /* the lock to protect this struct */
292 int kaio_flags; /* (a) per process kaio flags */
293 int kaio_maxactive_count; /* (*) maximum number of AIOs */
294 int kaio_active_count; /* (c) number of currently used AIOs */
295 int kaio_qallowed_count; /* (*) maxiumu size of AIO queue */
296 int kaio_count; /* (a) size of AIO queue */
297 int kaio_ballowed_count; /* (*) maximum number of buffers */
298 int kaio_buffer_count; /* (a) number of physio buffers */
299 TAILQ_HEAD(,aiocblist) kaio_all; /* (a) all AIOs in the process */
300 TAILQ_HEAD(,aiocblist) kaio_done; /* (a) done queue for process */
301 TAILQ_HEAD(,aioliojob) kaio_liojoblist; /* (a) list of lio jobs */
302 TAILQ_HEAD(,aiocblist) kaio_jobqueue; /* (a) job queue for process */
303 TAILQ_HEAD(,aiocblist) kaio_bufqueue; /* (a) buffer job queue for process */
304 TAILQ_HEAD(,aiocblist) kaio_sockqueue; /* (a) queue for aios waiting on sockets,
307 TAILQ_HEAD(,aiocblist) kaio_syncqueue; /* (a) queue for aio_fsync */
308 struct task kaio_task; /* (*) task to kick aio threads */
311 #define AIO_LOCK(ki) mtx_lock(&(ki)->kaio_mtx)
312 #define AIO_UNLOCK(ki) mtx_unlock(&(ki)->kaio_mtx)
313 #define AIO_LOCK_ASSERT(ki, f) mtx_assert(&(ki)->kaio_mtx, (f))
314 #define AIO_MTX(ki) (&(ki)->kaio_mtx)
316 #define KAIO_RUNDOWN 0x1 /* process is being run down */
317 #define KAIO_WAKEUP 0x2 /* wakeup process when there is a significant event */
320 * Operations used to interact with userland aio control blocks.
321 * Different ABIs provide their own operations.
324 int (*copyin)(struct aiocb *ujob, struct aiocb *kjob);
325 long (*fetch_status)(struct aiocb *ujob);
326 long (*fetch_error)(struct aiocb *ujob);
327 int (*store_status)(struct aiocb *ujob, long status);
328 int (*store_error)(struct aiocb *ujob, long error);
329 int (*store_kernelinfo)(struct aiocb *ujob, long jobref);
330 int (*store_aiocb)(struct aiocb **ujobp, struct aiocb *ujob);
333 static TAILQ_HEAD(,aiothreadlist) aio_freeproc; /* (c) Idle daemons */
334 static struct sema aio_newproc_sem;
335 static struct mtx aio_job_mtx;
336 static struct mtx aio_sock_mtx;
337 static TAILQ_HEAD(,aiocblist) aio_jobs; /* (c) Async job list */
338 static struct unrhdr *aiod_unr;
340 void aio_init_aioinfo(struct proc *p);
341 static int aio_onceonly(void);
342 static int aio_free_entry(struct aiocblist *aiocbe);
343 static void aio_process_rw(struct aiocblist *aiocbe);
344 static void aio_process_sync(struct aiocblist *aiocbe);
345 static void aio_process_mlock(struct aiocblist *aiocbe);
346 static int aio_newproc(int *);
347 int aio_aqueue(struct thread *td, struct aiocb *job,
348 struct aioliojob *lio, int type, struct aiocb_ops *ops);
349 static void aio_physwakeup(struct bio *bp);
350 static void aio_proc_rundown(void *arg, struct proc *p);
351 static void aio_proc_rundown_exec(void *arg, struct proc *p, struct image_params *imgp);
352 static int aio_qphysio(struct proc *p, struct aiocblist *iocb);
353 static void aio_daemon(void *param);
354 static void aio_swake_cb(struct socket *, struct sockbuf *);
355 static int aio_unload(void);
356 static void aio_bio_done_notify(struct proc *userp, struct aiocblist *aiocbe, int type);
359 static int aio_kick(struct proc *userp);
360 static void aio_kick_nowait(struct proc *userp);
361 static void aio_kick_helper(void *context, int pending);
362 static int filt_aioattach(struct knote *kn);
363 static void filt_aiodetach(struct knote *kn);
364 static int filt_aio(struct knote *kn, long hint);
365 static int filt_lioattach(struct knote *kn);
366 static void filt_liodetach(struct knote *kn);
367 static int filt_lio(struct knote *kn, long hint);
371 * kaio Per process async io info
372 * aiop async io thread data
373 * aiocb async io jobs
374 * aiol list io job pointer - internal to aio_suspend XXX
375 * aiolio list io jobs
377 static uma_zone_t kaio_zone, aiop_zone, aiocb_zone, aiol_zone, aiolio_zone;
379 /* kqueue filters for aio */
380 static struct filterops aio_filtops = {
382 .f_attach = filt_aioattach,
383 .f_detach = filt_aiodetach,
386 static struct filterops lio_filtops = {
388 .f_attach = filt_lioattach,
389 .f_detach = filt_liodetach,
393 static eventhandler_tag exit_tag, exec_tag;
395 TASKQUEUE_DEFINE_THREAD(aiod_bio);
398 * Main operations function for use as a kernel module.
401 aio_modload(struct module *module, int cmd, void *arg)
410 error = aio_unload();
421 static moduledata_t aio_mod = {
427 static struct syscall_helper_data aio_syscalls[] = {
428 SYSCALL_INIT_HELPER(aio_cancel),
429 SYSCALL_INIT_HELPER(aio_error),
430 SYSCALL_INIT_HELPER(aio_fsync),
431 SYSCALL_INIT_HELPER(aio_mlock),
432 SYSCALL_INIT_HELPER(aio_read),
433 SYSCALL_INIT_HELPER(aio_return),
434 SYSCALL_INIT_HELPER(aio_suspend),
435 SYSCALL_INIT_HELPER(aio_waitcomplete),
436 SYSCALL_INIT_HELPER(aio_write),
437 SYSCALL_INIT_HELPER(lio_listio),
438 SYSCALL_INIT_HELPER(oaio_read),
439 SYSCALL_INIT_HELPER(oaio_write),
440 SYSCALL_INIT_HELPER(olio_listio),
444 #ifdef COMPAT_FREEBSD32
445 #include <sys/mount.h>
446 #include <sys/socket.h>
447 #include <compat/freebsd32/freebsd32.h>
448 #include <compat/freebsd32/freebsd32_proto.h>
449 #include <compat/freebsd32/freebsd32_signal.h>
450 #include <compat/freebsd32/freebsd32_syscall.h>
451 #include <compat/freebsd32/freebsd32_util.h>
453 static struct syscall_helper_data aio32_syscalls[] = {
454 SYSCALL32_INIT_HELPER(freebsd32_aio_return),
455 SYSCALL32_INIT_HELPER(freebsd32_aio_suspend),
456 SYSCALL32_INIT_HELPER(freebsd32_aio_cancel),
457 SYSCALL32_INIT_HELPER(freebsd32_aio_error),
458 SYSCALL32_INIT_HELPER(freebsd32_aio_fsync),
459 SYSCALL32_INIT_HELPER(freebsd32_aio_mlock),
460 SYSCALL32_INIT_HELPER(freebsd32_aio_read),
461 SYSCALL32_INIT_HELPER(freebsd32_aio_write),
462 SYSCALL32_INIT_HELPER(freebsd32_aio_waitcomplete),
463 SYSCALL32_INIT_HELPER(freebsd32_lio_listio),
464 SYSCALL32_INIT_HELPER(freebsd32_oaio_read),
465 SYSCALL32_INIT_HELPER(freebsd32_oaio_write),
466 SYSCALL32_INIT_HELPER(freebsd32_olio_listio),
471 DECLARE_MODULE(aio, aio_mod,
472 SI_SUB_VFS, SI_ORDER_ANY);
473 MODULE_VERSION(aio, 1);
476 * Startup initialization
483 /* XXX: should probably just use so->callback */
484 aio_swake = &aio_swake_cb;
485 exit_tag = EVENTHANDLER_REGISTER(process_exit, aio_proc_rundown, NULL,
486 EVENTHANDLER_PRI_ANY);
487 exec_tag = EVENTHANDLER_REGISTER(process_exec, aio_proc_rundown_exec, NULL,
488 EVENTHANDLER_PRI_ANY);
489 kqueue_add_filteropts(EVFILT_AIO, &aio_filtops);
490 kqueue_add_filteropts(EVFILT_LIO, &lio_filtops);
491 TAILQ_INIT(&aio_freeproc);
492 sema_init(&aio_newproc_sem, 0, "aio_new_proc");
493 mtx_init(&aio_job_mtx, "aio_job", NULL, MTX_DEF);
494 mtx_init(&aio_sock_mtx, "aio_sock", NULL, MTX_DEF);
495 TAILQ_INIT(&aio_jobs);
496 aiod_unr = new_unrhdr(1, INT_MAX, NULL);
497 kaio_zone = uma_zcreate("AIO", sizeof(struct kaioinfo), NULL, NULL,
498 NULL, NULL, UMA_ALIGN_PTR, UMA_ZONE_NOFREE);
499 aiop_zone = uma_zcreate("AIOP", sizeof(struct aiothreadlist), NULL,
500 NULL, NULL, NULL, UMA_ALIGN_PTR, UMA_ZONE_NOFREE);
501 aiocb_zone = uma_zcreate("AIOCB", sizeof(struct aiocblist), NULL, NULL,
502 NULL, NULL, UMA_ALIGN_PTR, UMA_ZONE_NOFREE);
503 aiol_zone = uma_zcreate("AIOL", AIO_LISTIO_MAX*sizeof(intptr_t) , NULL,
504 NULL, NULL, NULL, UMA_ALIGN_PTR, UMA_ZONE_NOFREE);
505 aiolio_zone = uma_zcreate("AIOLIO", sizeof(struct aioliojob), NULL,
506 NULL, NULL, NULL, UMA_ALIGN_PTR, UMA_ZONE_NOFREE);
507 aiod_timeout = AIOD_TIMEOUT_DEFAULT;
508 aiod_lifetime = AIOD_LIFETIME_DEFAULT;
510 async_io_version = _POSIX_VERSION;
511 p31b_setcfg(CTL_P1003_1B_AIO_LISTIO_MAX, AIO_LISTIO_MAX);
512 p31b_setcfg(CTL_P1003_1B_AIO_MAX, MAX_AIO_QUEUE);
513 p31b_setcfg(CTL_P1003_1B_AIO_PRIO_DELTA_MAX, 0);
515 error = syscall_helper_register(aio_syscalls);
518 #ifdef COMPAT_FREEBSD32
519 error = syscall32_helper_register(aio32_syscalls);
527 * Callback for unload of AIO when used as a module.
535 * XXX: no unloads by default, it's too dangerous.
536 * perhaps we could do it if locked out callers and then
537 * did an aio_proc_rundown() on each process.
539 * jhb: aio_proc_rundown() needs to run on curproc though,
540 * so I don't think that would fly.
545 #ifdef COMPAT_FREEBSD32
546 syscall32_helper_unregister(aio32_syscalls);
548 syscall_helper_unregister(aio_syscalls);
550 error = kqueue_del_filteropts(EVFILT_AIO);
553 error = kqueue_del_filteropts(EVFILT_LIO);
556 async_io_version = 0;
558 taskqueue_free(taskqueue_aiod_bio);
559 delete_unrhdr(aiod_unr);
560 uma_zdestroy(kaio_zone);
561 uma_zdestroy(aiop_zone);
562 uma_zdestroy(aiocb_zone);
563 uma_zdestroy(aiol_zone);
564 uma_zdestroy(aiolio_zone);
565 EVENTHANDLER_DEREGISTER(process_exit, exit_tag);
566 EVENTHANDLER_DEREGISTER(process_exec, exec_tag);
567 mtx_destroy(&aio_job_mtx);
568 mtx_destroy(&aio_sock_mtx);
569 sema_destroy(&aio_newproc_sem);
570 p31b_setcfg(CTL_P1003_1B_AIO_LISTIO_MAX, -1);
571 p31b_setcfg(CTL_P1003_1B_AIO_MAX, -1);
572 p31b_setcfg(CTL_P1003_1B_AIO_PRIO_DELTA_MAX, -1);
577 * Init the per-process aioinfo structure. The aioinfo limits are set
578 * per-process for user limit (resource) management.
581 aio_init_aioinfo(struct proc *p)
585 ki = uma_zalloc(kaio_zone, M_WAITOK);
586 mtx_init(&ki->kaio_mtx, "aiomtx", NULL, MTX_DEF);
588 ki->kaio_maxactive_count = max_aio_per_proc;
589 ki->kaio_active_count = 0;
590 ki->kaio_qallowed_count = max_aio_queue_per_proc;
592 ki->kaio_ballowed_count = max_buf_aio;
593 ki->kaio_buffer_count = 0;
594 TAILQ_INIT(&ki->kaio_all);
595 TAILQ_INIT(&ki->kaio_done);
596 TAILQ_INIT(&ki->kaio_jobqueue);
597 TAILQ_INIT(&ki->kaio_bufqueue);
598 TAILQ_INIT(&ki->kaio_liojoblist);
599 TAILQ_INIT(&ki->kaio_sockqueue);
600 TAILQ_INIT(&ki->kaio_syncqueue);
601 TASK_INIT(&ki->kaio_task, 0, aio_kick_helper, p);
603 if (p->p_aioinfo == NULL) {
608 mtx_destroy(&ki->kaio_mtx);
609 uma_zfree(kaio_zone, ki);
612 while (num_aio_procs < MIN(target_aio_procs, max_aio_procs))
617 aio_sendsig(struct proc *p, struct sigevent *sigev, ksiginfo_t *ksi)
622 error = sigev_findtd(p, sigev, &td);
626 ksiginfo_set_sigev(ksi, sigev);
627 ksi->ksi_code = SI_ASYNCIO;
628 ksi->ksi_flags |= KSI_EXT | KSI_INS;
629 tdsendsignal(p, td, ksi->ksi_signo, ksi);
636 * Free a job entry. Wait for completion if it is currently active, but don't
637 * delay forever. If we delay, we return a flag that says that we have to
638 * restart the queue scan.
641 aio_free_entry(struct aiocblist *aiocbe)
644 struct aioliojob *lj;
647 p = aiocbe->userproc;
652 AIO_LOCK_ASSERT(ki, MA_OWNED);
653 MPASS(aiocbe->jobstate == JOBST_JOBFINISHED);
655 atomic_subtract_int(&num_queue_count, 1);
658 MPASS(ki->kaio_count >= 0);
660 TAILQ_REMOVE(&ki->kaio_done, aiocbe, plist);
661 TAILQ_REMOVE(&ki->kaio_all, aiocbe, allist);
666 lj->lioj_finished_count--;
668 if (lj->lioj_count == 0) {
669 TAILQ_REMOVE(&ki->kaio_liojoblist, lj, lioj_list);
670 /* lio is going away, we need to destroy any knotes */
671 knlist_delete(&lj->klist, curthread, 1);
673 sigqueue_take(&lj->lioj_ksi);
675 uma_zfree(aiolio_zone, lj);
679 /* aiocbe is going away, we need to destroy any knotes */
680 knlist_delete(&aiocbe->klist, curthread, 1);
682 sigqueue_take(&aiocbe->ksi);
685 MPASS(aiocbe->bp == NULL);
686 aiocbe->jobstate = JOBST_NULL;
690 * The thread argument here is used to find the owning process
691 * and is also passed to fo_close() which may pass it to various
692 * places such as devsw close() routines. Because of that, we
693 * need a thread pointer from the process owning the job that is
694 * persistent and won't disappear out from under us or move to
697 * Currently, all the callers of this function call it to remove
698 * an aiocblist from the current process' job list either via a
699 * syscall or due to the current process calling exit() or
700 * execve(). Thus, we know that p == curproc. We also know that
701 * curthread can't exit since we are curthread.
703 * Therefore, we use curthread as the thread to pass to
704 * knlist_delete(). This does mean that it is possible for the
705 * thread pointer at close time to differ from the thread pointer
706 * at open time, but this is already true of file descriptors in
707 * a multithreaded process.
710 fdrop(aiocbe->fd_file, curthread);
711 crfree(aiocbe->cred);
712 uma_zfree(aiocb_zone, aiocbe);
719 aio_proc_rundown_exec(void *arg, struct proc *p, struct image_params *imgp __unused)
721 aio_proc_rundown(arg, p);
725 * Rundown the jobs for a given process.
728 aio_proc_rundown(void *arg, struct proc *p)
731 struct aioliojob *lj;
732 struct aiocblist *cbe, *cbn;
737 KASSERT(curthread->td_proc == p,
738 ("%s: called on non-curproc", __func__));
744 ki->kaio_flags |= KAIO_RUNDOWN;
749 * Try to cancel all pending requests. This code simulates
750 * aio_cancel on all pending I/O requests.
752 TAILQ_FOREACH_SAFE(cbe, &ki->kaio_jobqueue, plist, cbn) {
754 mtx_lock(&aio_job_mtx);
755 if (cbe->jobstate == JOBST_JOBQGLOBAL) {
756 TAILQ_REMOVE(&aio_jobs, cbe, list);
758 } else if (cbe->jobstate == JOBST_JOBQSOCK) {
760 MPASS(fp->f_type == DTYPE_SOCKET);
762 TAILQ_REMOVE(&so->so_aiojobq, cbe, list);
764 } else if (cbe->jobstate == JOBST_JOBQSYNC) {
765 TAILQ_REMOVE(&ki->kaio_syncqueue, cbe, list);
768 mtx_unlock(&aio_job_mtx);
771 cbe->jobstate = JOBST_JOBFINISHED;
772 cbe->uaiocb._aiocb_private.status = -1;
773 cbe->uaiocb._aiocb_private.error = ECANCELED;
774 TAILQ_REMOVE(&ki->kaio_jobqueue, cbe, plist);
775 aio_bio_done_notify(p, cbe, DONE_QUEUE);
779 /* Wait for all running I/O to be finished */
780 if (TAILQ_FIRST(&ki->kaio_bufqueue) ||
781 TAILQ_FIRST(&ki->kaio_jobqueue)) {
782 ki->kaio_flags |= KAIO_WAKEUP;
783 msleep(&p->p_aioinfo, AIO_MTX(ki), PRIBIO, "aioprn", hz);
787 /* Free all completed I/O requests. */
788 while ((cbe = TAILQ_FIRST(&ki->kaio_done)) != NULL)
791 while ((lj = TAILQ_FIRST(&ki->kaio_liojoblist)) != NULL) {
792 if (lj->lioj_count == 0) {
793 TAILQ_REMOVE(&ki->kaio_liojoblist, lj, lioj_list);
794 knlist_delete(&lj->klist, curthread, 1);
796 sigqueue_take(&lj->lioj_ksi);
798 uma_zfree(aiolio_zone, lj);
800 panic("LIO job not cleaned up: C:%d, FC:%d\n",
801 lj->lioj_count, lj->lioj_finished_count);
805 taskqueue_drain(taskqueue_aiod_bio, &ki->kaio_task);
806 mtx_destroy(&ki->kaio_mtx);
807 uma_zfree(kaio_zone, ki);
812 * Select a job to run (called by an AIO daemon).
814 static struct aiocblist *
815 aio_selectjob(struct aiothreadlist *aiop)
817 struct aiocblist *aiocbe;
821 mtx_assert(&aio_job_mtx, MA_OWNED);
822 TAILQ_FOREACH(aiocbe, &aio_jobs, list) {
823 userp = aiocbe->userproc;
824 ki = userp->p_aioinfo;
826 if (ki->kaio_active_count < ki->kaio_maxactive_count) {
827 TAILQ_REMOVE(&aio_jobs, aiocbe, list);
828 /* Account for currently active jobs. */
829 ki->kaio_active_count++;
830 aiocbe->jobstate = JOBST_JOBRUNNING;
838 * Move all data to a permanent storage device, this code
839 * simulates fsync syscall.
842 aio_fsync_vnode(struct thread *td, struct vnode *vp)
847 if ((error = vn_start_write(vp, &mp, V_WAIT | PCATCH)) != 0)
849 vn_lock(vp, LK_EXCLUSIVE | LK_RETRY);
850 if (vp->v_object != NULL) {
851 VM_OBJECT_WLOCK(vp->v_object);
852 vm_object_page_clean(vp->v_object, 0, 0, 0);
853 VM_OBJECT_WUNLOCK(vp->v_object);
855 error = VOP_FSYNC(vp, MNT_WAIT, td);
858 vn_finished_write(mp);
864 * The AIO processing activity for LIO_READ/LIO_WRITE. This is the code that
865 * does the I/O request for the non-physio version of the operations. The
866 * normal vn operations are used, and this code should work in all instances
867 * for every type of file, including pipes, sockets, fifos, and regular files.
869 * XXX I don't think it works well for socket, pipe, and fifo.
872 aio_process_rw(struct aiocblist *aiocbe)
874 struct ucred *td_savedcred;
883 int oublock_st, oublock_end;
884 int inblock_st, inblock_end;
886 KASSERT(aiocbe->uaiocb.aio_lio_opcode == LIO_READ ||
887 aiocbe->uaiocb.aio_lio_opcode == LIO_WRITE,
888 ("%s: opcode %d", __func__, aiocbe->uaiocb.aio_lio_opcode));
891 td_savedcred = td->td_ucred;
892 td->td_ucred = aiocbe->cred;
893 cb = &aiocbe->uaiocb;
894 fp = aiocbe->fd_file;
896 aiov.iov_base = (void *)(uintptr_t)cb->aio_buf;
897 aiov.iov_len = cb->aio_nbytes;
899 auio.uio_iov = &aiov;
901 auio.uio_offset = cb->aio_offset;
902 auio.uio_resid = cb->aio_nbytes;
903 cnt = cb->aio_nbytes;
904 auio.uio_segflg = UIO_USERSPACE;
907 inblock_st = td->td_ru.ru_inblock;
908 oublock_st = td->td_ru.ru_oublock;
910 * aio_aqueue() acquires a reference to the file that is
911 * released in aio_free_entry().
913 if (cb->aio_lio_opcode == LIO_READ) {
914 auio.uio_rw = UIO_READ;
915 if (auio.uio_resid == 0)
918 error = fo_read(fp, &auio, fp->f_cred, FOF_OFFSET, td);
920 if (fp->f_type == DTYPE_VNODE)
922 auio.uio_rw = UIO_WRITE;
923 error = fo_write(fp, &auio, fp->f_cred, FOF_OFFSET, td);
925 inblock_end = td->td_ru.ru_inblock;
926 oublock_end = td->td_ru.ru_oublock;
928 aiocbe->inputcharge = inblock_end - inblock_st;
929 aiocbe->outputcharge = oublock_end - oublock_st;
931 if ((error) && (auio.uio_resid != cnt)) {
932 if (error == ERESTART || error == EINTR || error == EWOULDBLOCK)
934 if ((error == EPIPE) && (cb->aio_lio_opcode == LIO_WRITE)) {
936 if (fp->f_type == DTYPE_SOCKET) {
938 if (so->so_options & SO_NOSIGPIPE)
942 PROC_LOCK(aiocbe->userproc);
943 kern_psignal(aiocbe->userproc, SIGPIPE);
944 PROC_UNLOCK(aiocbe->userproc);
949 cnt -= auio.uio_resid;
950 cb->_aiocb_private.error = error;
951 cb->_aiocb_private.status = cnt;
952 td->td_ucred = td_savedcred;
956 aio_process_sync(struct aiocblist *aiocbe)
958 struct thread *td = curthread;
959 struct ucred *td_savedcred = td->td_ucred;
960 struct aiocb *cb = &aiocbe->uaiocb;
961 struct file *fp = aiocbe->fd_file;
964 KASSERT(aiocbe->uaiocb.aio_lio_opcode == LIO_SYNC,
965 ("%s: opcode %d", __func__, aiocbe->uaiocb.aio_lio_opcode));
967 td->td_ucred = aiocbe->cred;
968 if (fp->f_vnode != NULL)
969 error = aio_fsync_vnode(td, fp->f_vnode);
970 cb->_aiocb_private.error = error;
971 cb->_aiocb_private.status = 0;
972 td->td_ucred = td_savedcred;
976 aio_process_mlock(struct aiocblist *aiocbe)
978 struct aiocb *cb = &aiocbe->uaiocb;
981 KASSERT(aiocbe->uaiocb.aio_lio_opcode == LIO_MLOCK,
982 ("%s: opcode %d", __func__, aiocbe->uaiocb.aio_lio_opcode));
984 error = vm_mlock(aiocbe->userproc, aiocbe->cred,
985 __DEVOLATILE(void *, cb->aio_buf), cb->aio_nbytes);
986 cb->_aiocb_private.error = error;
987 cb->_aiocb_private.status = 0;
991 aio_bio_done_notify(struct proc *userp, struct aiocblist *aiocbe, int type)
993 struct aioliojob *lj;
995 struct aiocblist *scb, *scbn;
998 ki = userp->p_aioinfo;
999 AIO_LOCK_ASSERT(ki, MA_OWNED);
1003 lj->lioj_finished_count++;
1004 if (lj->lioj_count == lj->lioj_finished_count)
1007 if (type == DONE_QUEUE) {
1008 aiocbe->jobflags |= AIOCBLIST_DONE;
1010 aiocbe->jobflags |= AIOCBLIST_BUFDONE;
1012 TAILQ_INSERT_TAIL(&ki->kaio_done, aiocbe, plist);
1013 aiocbe->jobstate = JOBST_JOBFINISHED;
1015 if (ki->kaio_flags & KAIO_RUNDOWN)
1016 goto notification_done;
1018 if (aiocbe->uaiocb.aio_sigevent.sigev_notify == SIGEV_SIGNAL ||
1019 aiocbe->uaiocb.aio_sigevent.sigev_notify == SIGEV_THREAD_ID)
1020 aio_sendsig(userp, &aiocbe->uaiocb.aio_sigevent, &aiocbe->ksi);
1022 KNOTE_LOCKED(&aiocbe->klist, 1);
1025 if (lj->lioj_signal.sigev_notify == SIGEV_KEVENT) {
1026 lj->lioj_flags |= LIOJ_KEVENT_POSTED;
1027 KNOTE_LOCKED(&lj->klist, 1);
1029 if ((lj->lioj_flags & (LIOJ_SIGNAL|LIOJ_SIGNAL_POSTED))
1031 && (lj->lioj_signal.sigev_notify == SIGEV_SIGNAL ||
1032 lj->lioj_signal.sigev_notify == SIGEV_THREAD_ID)) {
1033 aio_sendsig(userp, &lj->lioj_signal, &lj->lioj_ksi);
1034 lj->lioj_flags |= LIOJ_SIGNAL_POSTED;
1039 if (aiocbe->jobflags & AIOCBLIST_CHECKSYNC) {
1040 TAILQ_FOREACH_SAFE(scb, &ki->kaio_syncqueue, list, scbn) {
1041 if (aiocbe->fd_file == scb->fd_file &&
1042 aiocbe->seqno < scb->seqno) {
1043 if (--scb->pending == 0) {
1044 mtx_lock(&aio_job_mtx);
1045 scb->jobstate = JOBST_JOBQGLOBAL;
1046 TAILQ_REMOVE(&ki->kaio_syncqueue, scb, list);
1047 TAILQ_INSERT_TAIL(&aio_jobs, scb, list);
1048 aio_kick_nowait(userp);
1049 mtx_unlock(&aio_job_mtx);
1054 if (ki->kaio_flags & KAIO_WAKEUP) {
1055 ki->kaio_flags &= ~KAIO_WAKEUP;
1056 wakeup(&userp->p_aioinfo);
1061 * The AIO daemon, most of the actual work is done in aio_process_*,
1062 * but the setup (and address space mgmt) is done in this routine.
1065 aio_daemon(void *_id)
1067 struct aiocblist *aiocbe;
1068 struct aiothreadlist *aiop;
1069 struct kaioinfo *ki;
1070 struct proc *curcp, *mycp, *userp;
1071 struct vmspace *myvm, *tmpvm;
1072 struct thread *td = curthread;
1073 int id = (intptr_t)_id;
1076 * Local copies of curproc (cp) and vmspace (myvm)
1079 myvm = mycp->p_vmspace;
1081 KASSERT(mycp->p_textvp == NULL, ("kthread has a textvp"));
1084 * Allocate and ready the aio control info. There is one aiop structure
1087 aiop = uma_zalloc(aiop_zone, M_WAITOK);
1088 aiop->aiothread = td;
1089 aiop->aiothreadflags = 0;
1091 /* The daemon resides in its own pgrp. */
1092 sys_setsid(td, NULL);
1095 * Wakeup parent process. (Parent sleeps to keep from blasting away
1096 * and creating too many daemons.)
1098 sema_post(&aio_newproc_sem);
1100 mtx_lock(&aio_job_mtx);
1103 * curcp is the current daemon process context.
1104 * userp is the current user process context.
1109 * Take daemon off of free queue
1111 if (aiop->aiothreadflags & AIOP_FREE) {
1112 TAILQ_REMOVE(&aio_freeproc, aiop, list);
1113 aiop->aiothreadflags &= ~AIOP_FREE;
1119 while ((aiocbe = aio_selectjob(aiop)) != NULL) {
1120 mtx_unlock(&aio_job_mtx);
1121 userp = aiocbe->userproc;
1124 * Connect to process address space for user program.
1126 if (userp != curcp) {
1128 * Save the current address space that we are
1131 tmpvm = mycp->p_vmspace;
1134 * Point to the new user address space, and
1137 mycp->p_vmspace = userp->p_vmspace;
1138 atomic_add_int(&mycp->p_vmspace->vm_refcnt, 1);
1140 /* Activate the new mapping. */
1141 pmap_activate(FIRST_THREAD_IN_PROC(mycp));
1144 * If the old address space wasn't the daemons
1145 * own address space, then we need to remove the
1146 * daemon's reference from the other process
1147 * that it was acting on behalf of.
1149 if (tmpvm != myvm) {
1150 vmspace_free(tmpvm);
1155 ki = userp->p_aioinfo;
1157 /* Do the I/O function. */
1158 switch(aiocbe->uaiocb.aio_lio_opcode) {
1161 aio_process_rw(aiocbe);
1164 aio_process_sync(aiocbe);
1167 aio_process_mlock(aiocbe);
1171 mtx_lock(&aio_job_mtx);
1172 /* Decrement the active job count. */
1173 ki->kaio_active_count--;
1174 mtx_unlock(&aio_job_mtx);
1177 TAILQ_REMOVE(&ki->kaio_jobqueue, aiocbe, plist);
1178 aio_bio_done_notify(userp, aiocbe, DONE_QUEUE);
1181 mtx_lock(&aio_job_mtx);
1185 * Disconnect from user address space.
1187 if (curcp != mycp) {
1189 mtx_unlock(&aio_job_mtx);
1191 /* Get the user address space to disconnect from. */
1192 tmpvm = mycp->p_vmspace;
1194 /* Get original address space for daemon. */
1195 mycp->p_vmspace = myvm;
1197 /* Activate the daemon's address space. */
1198 pmap_activate(FIRST_THREAD_IN_PROC(mycp));
1200 if (tmpvm == myvm) {
1201 printf("AIOD: vmspace problem -- %d\n",
1205 /* Remove our vmspace reference. */
1206 vmspace_free(tmpvm);
1210 mtx_lock(&aio_job_mtx);
1212 * We have to restart to avoid race, we only sleep if
1213 * no job can be selected, that should be
1219 mtx_assert(&aio_job_mtx, MA_OWNED);
1221 TAILQ_INSERT_HEAD(&aio_freeproc, aiop, list);
1222 aiop->aiothreadflags |= AIOP_FREE;
1225 * If daemon is inactive for a long time, allow it to exit,
1226 * thereby freeing resources.
1228 if (msleep(aiop->aiothread, &aio_job_mtx, PRIBIO, "aiordy",
1230 if (TAILQ_EMPTY(&aio_jobs)) {
1231 if ((aiop->aiothreadflags & AIOP_FREE) &&
1232 (num_aio_procs > target_aio_procs)) {
1233 TAILQ_REMOVE(&aio_freeproc, aiop, list);
1235 mtx_unlock(&aio_job_mtx);
1236 uma_zfree(aiop_zone, aiop);
1237 free_unr(aiod_unr, id);
1239 if (mycp->p_vmspace->vm_refcnt <= 1) {
1240 printf("AIOD: bad vm refcnt for"
1241 " exiting daemon: %d\n",
1242 mycp->p_vmspace->vm_refcnt);
1250 mtx_unlock(&aio_job_mtx);
1251 panic("shouldn't be here\n");
1255 * Create a new AIO daemon. This is mostly a kernel-thread fork routine. The
1256 * AIO daemon modifies its environment itself.
1259 aio_newproc(int *start)
1265 id = alloc_unr(aiod_unr);
1266 error = kproc_create(aio_daemon, (void *)(intptr_t)id, &p,
1267 RFNOWAIT, 0, "aiod%d", id);
1270 * Wait until daemon is started.
1272 sema_wait(&aio_newproc_sem);
1273 mtx_lock(&aio_job_mtx);
1277 mtx_unlock(&aio_job_mtx);
1279 free_unr(aiod_unr, id);
1285 * Try the high-performance, low-overhead physio method for eligible
1286 * VCHR devices. This method doesn't use an aio helper thread, and
1287 * thus has very low overhead.
1289 * Assumes that the caller, aio_aqueue(), has incremented the file
1290 * structure's reference count, preventing its deallocation for the
1291 * duration of this call.
1294 aio_qphysio(struct proc *p, struct aiocblist *aiocbe)
1303 struct kaioinfo *ki;
1304 struct aioliojob *lj;
1305 int error, ref, unmap, poff;
1308 cb = &aiocbe->uaiocb;
1309 fp = aiocbe->fd_file;
1311 if (fp == NULL || fp->f_type != DTYPE_VNODE)
1315 if (vp->v_type != VCHR)
1317 if (vp->v_bufobj.bo_bsize == 0)
1319 if (cb->aio_nbytes % vp->v_bufobj.bo_bsize)
1323 csw = devvn_refthread(vp, &dev, &ref);
1327 if ((csw->d_flags & D_DISK) == 0) {
1331 if (cb->aio_nbytes > dev->si_iosize_max) {
1337 poff = (vm_offset_t)cb->aio_buf & PAGE_MASK;
1338 unmap = ((dev->si_flags & SI_UNMAPPED) && unmapped_buf_allowed);
1340 if (cb->aio_nbytes > MAXPHYS) {
1345 if (cb->aio_nbytes > MAXPHYS - poff) {
1349 if (ki->kaio_buffer_count >= ki->kaio_ballowed_count) {
1354 aiocbe->bp = bp = g_alloc_bio();
1356 aiocbe->pbuf = pbuf = (struct buf *)getpbuf(NULL);
1363 ki->kaio_buffer_count++;
1367 TAILQ_INSERT_TAIL(&ki->kaio_bufqueue, aiocbe, plist);
1368 TAILQ_INSERT_TAIL(&ki->kaio_all, aiocbe, allist);
1369 aiocbe->jobstate = JOBST_JOBQBUF;
1370 cb->_aiocb_private.status = cb->aio_nbytes;
1373 bp->bio_length = cb->aio_nbytes;
1374 bp->bio_bcount = cb->aio_nbytes;
1375 bp->bio_done = aio_physwakeup;
1376 bp->bio_data = (void *)(uintptr_t)cb->aio_buf;
1377 bp->bio_offset = cb->aio_offset;
1378 bp->bio_cmd = cb->aio_lio_opcode == LIO_WRITE ? BIO_WRITE : BIO_READ;
1380 bp->bio_caller1 = (void *)aiocbe;
1382 prot = VM_PROT_READ;
1383 if (cb->aio_lio_opcode == LIO_READ)
1384 prot |= VM_PROT_WRITE; /* Less backwards than it looks */
1385 if ((aiocbe->npages = vm_fault_quick_hold_pages(
1386 &curproc->p_vmspace->vm_map,
1387 (vm_offset_t)bp->bio_data, bp->bio_length, prot, aiocbe->pages,
1388 sizeof(aiocbe->pages)/sizeof(aiocbe->pages[0]))) < 0) {
1393 pmap_qenter((vm_offset_t)pbuf->b_data,
1394 aiocbe->pages, aiocbe->npages);
1395 bp->bio_data = pbuf->b_data + poff;
1397 bp->bio_ma = aiocbe->pages;
1398 bp->bio_ma_n = aiocbe->npages;
1399 bp->bio_ma_offset = poff;
1400 bp->bio_data = unmapped_buf;
1401 bp->bio_flags |= BIO_UNMAPPED;
1404 atomic_add_int(&num_queue_count, 1);
1406 atomic_add_int(&num_buf_aio, 1);
1408 /* Perform transfer. */
1409 csw->d_strategy(bp);
1410 dev_relthread(dev, ref);
1415 aiocbe->jobstate = JOBST_NULL;
1416 TAILQ_REMOVE(&ki->kaio_bufqueue, aiocbe, plist);
1417 TAILQ_REMOVE(&ki->kaio_all, aiocbe, allist);
1420 ki->kaio_buffer_count--;
1425 relpbuf(pbuf, NULL);
1426 aiocbe->pbuf = NULL;
1431 dev_relthread(dev, ref);
1436 * Wake up aio requests that may be serviceable now.
1439 aio_swake_cb(struct socket *so, struct sockbuf *sb)
1441 struct aiocblist *cb, *cbn;
1444 SOCKBUF_LOCK_ASSERT(sb);
1445 if (sb == &so->so_snd)
1450 sb->sb_flags &= ~SB_AIO;
1451 mtx_lock(&aio_job_mtx);
1452 TAILQ_FOREACH_SAFE(cb, &so->so_aiojobq, list, cbn) {
1453 if (opcode == cb->uaiocb.aio_lio_opcode) {
1454 if (cb->jobstate != JOBST_JOBQSOCK)
1455 panic("invalid queue value");
1457 * We don't have actual sockets backend yet,
1458 * so we simply move the requests to the generic
1461 TAILQ_REMOVE(&so->so_aiojobq, cb, list);
1462 TAILQ_INSERT_TAIL(&aio_jobs, cb, list);
1463 aio_kick_nowait(cb->userproc);
1466 mtx_unlock(&aio_job_mtx);
1470 convert_old_sigevent(struct osigevent *osig, struct sigevent *nsig)
1474 * Only SIGEV_NONE, SIGEV_SIGNAL, and SIGEV_KEVENT are
1475 * supported by AIO with the old sigevent structure.
1477 nsig->sigev_notify = osig->sigev_notify;
1478 switch (nsig->sigev_notify) {
1482 nsig->sigev_signo = osig->__sigev_u.__sigev_signo;
1485 nsig->sigev_notify_kqueue =
1486 osig->__sigev_u.__sigev_notify_kqueue;
1487 nsig->sigev_value.sival_ptr = osig->sigev_value.sival_ptr;
1496 aiocb_copyin_old_sigevent(struct aiocb *ujob, struct aiocb *kjob)
1498 struct oaiocb *ojob;
1501 bzero(kjob, sizeof(struct aiocb));
1502 error = copyin(ujob, kjob, sizeof(struct oaiocb));
1505 ojob = (struct oaiocb *)kjob;
1506 return (convert_old_sigevent(&ojob->aio_sigevent, &kjob->aio_sigevent));
1510 aiocb_copyin(struct aiocb *ujob, struct aiocb *kjob)
1513 return (copyin(ujob, kjob, sizeof(struct aiocb)));
1517 aiocb_fetch_status(struct aiocb *ujob)
1520 return (fuword(&ujob->_aiocb_private.status));
1524 aiocb_fetch_error(struct aiocb *ujob)
1527 return (fuword(&ujob->_aiocb_private.error));
1531 aiocb_store_status(struct aiocb *ujob, long status)
1534 return (suword(&ujob->_aiocb_private.status, status));
1538 aiocb_store_error(struct aiocb *ujob, long error)
1541 return (suword(&ujob->_aiocb_private.error, error));
1545 aiocb_store_kernelinfo(struct aiocb *ujob, long jobref)
1548 return (suword(&ujob->_aiocb_private.kernelinfo, jobref));
1552 aiocb_store_aiocb(struct aiocb **ujobp, struct aiocb *ujob)
1555 return (suword(ujobp, (long)ujob));
1558 static struct aiocb_ops aiocb_ops = {
1559 .copyin = aiocb_copyin,
1560 .fetch_status = aiocb_fetch_status,
1561 .fetch_error = aiocb_fetch_error,
1562 .store_status = aiocb_store_status,
1563 .store_error = aiocb_store_error,
1564 .store_kernelinfo = aiocb_store_kernelinfo,
1565 .store_aiocb = aiocb_store_aiocb,
1568 static struct aiocb_ops aiocb_ops_osigevent = {
1569 .copyin = aiocb_copyin_old_sigevent,
1570 .fetch_status = aiocb_fetch_status,
1571 .fetch_error = aiocb_fetch_error,
1572 .store_status = aiocb_store_status,
1573 .store_error = aiocb_store_error,
1574 .store_kernelinfo = aiocb_store_kernelinfo,
1575 .store_aiocb = aiocb_store_aiocb,
1579 * Queue a new AIO request. Choosing either the threaded or direct physio VCHR
1580 * technique is done in this code.
1583 aio_aqueue(struct thread *td, struct aiocb *job, struct aioliojob *lj,
1584 int type, struct aiocb_ops *ops)
1586 struct proc *p = td->td_proc;
1587 cap_rights_t rights;
1590 struct aiocblist *aiocbe, *cb;
1591 struct kaioinfo *ki;
1600 if (p->p_aioinfo == NULL)
1601 aio_init_aioinfo(p);
1605 ops->store_status(job, -1);
1606 ops->store_error(job, 0);
1607 ops->store_kernelinfo(job, -1);
1609 if (num_queue_count >= max_queue_count ||
1610 ki->kaio_count >= ki->kaio_qallowed_count) {
1611 ops->store_error(job, EAGAIN);
1615 aiocbe = uma_zalloc(aiocb_zone, M_WAITOK | M_ZERO);
1616 knlist_init_mtx(&aiocbe->klist, AIO_MTX(ki));
1618 error = ops->copyin(job, &aiocbe->uaiocb);
1620 ops->store_error(job, error);
1621 uma_zfree(aiocb_zone, aiocbe);
1625 /* XXX: aio_nbytes is later casted to signed types. */
1626 if (aiocbe->uaiocb.aio_nbytes > INT_MAX) {
1627 uma_zfree(aiocb_zone, aiocbe);
1631 if (aiocbe->uaiocb.aio_sigevent.sigev_notify != SIGEV_KEVENT &&
1632 aiocbe->uaiocb.aio_sigevent.sigev_notify != SIGEV_SIGNAL &&
1633 aiocbe->uaiocb.aio_sigevent.sigev_notify != SIGEV_THREAD_ID &&
1634 aiocbe->uaiocb.aio_sigevent.sigev_notify != SIGEV_NONE) {
1635 ops->store_error(job, EINVAL);
1636 uma_zfree(aiocb_zone, aiocbe);
1640 if ((aiocbe->uaiocb.aio_sigevent.sigev_notify == SIGEV_SIGNAL ||
1641 aiocbe->uaiocb.aio_sigevent.sigev_notify == SIGEV_THREAD_ID) &&
1642 !_SIG_VALID(aiocbe->uaiocb.aio_sigevent.sigev_signo)) {
1643 uma_zfree(aiocb_zone, aiocbe);
1647 ksiginfo_init(&aiocbe->ksi);
1649 /* Save userspace address of the job info. */
1650 aiocbe->uuaiocb = job;
1652 /* Get the opcode. */
1653 if (type != LIO_NOP)
1654 aiocbe->uaiocb.aio_lio_opcode = type;
1655 opcode = aiocbe->uaiocb.aio_lio_opcode;
1658 * Validate the opcode and fetch the file object for the specified
1661 * XXXRW: Moved the opcode validation up here so that we don't
1662 * retrieve a file descriptor without knowing what the capabiltity
1665 fd = aiocbe->uaiocb.aio_fildes;
1668 error = fget_write(td, fd,
1669 cap_rights_init(&rights, CAP_PWRITE), &fp);
1672 error = fget_read(td, fd,
1673 cap_rights_init(&rights, CAP_PREAD), &fp);
1676 error = fget(td, fd, cap_rights_init(&rights, CAP_FSYNC), &fp);
1682 error = fget(td, fd, cap_rights_init(&rights), &fp);
1688 uma_zfree(aiocb_zone, aiocbe);
1689 ops->store_error(job, error);
1693 if (opcode == LIO_SYNC && fp->f_vnode == NULL) {
1698 if (opcode != LIO_SYNC && aiocbe->uaiocb.aio_offset == -1LL) {
1703 aiocbe->fd_file = fp;
1705 mtx_lock(&aio_job_mtx);
1707 aiocbe->seqno = jobseqno++;
1708 mtx_unlock(&aio_job_mtx);
1709 error = ops->store_kernelinfo(job, jid);
1714 aiocbe->uaiocb._aiocb_private.kernelinfo = (void *)(intptr_t)jid;
1716 if (opcode == LIO_NOP) {
1718 uma_zfree(aiocb_zone, aiocbe);
1722 if (aiocbe->uaiocb.aio_sigevent.sigev_notify != SIGEV_KEVENT)
1724 evflags = aiocbe->uaiocb.aio_sigevent.sigev_notify_kevent_flags;
1725 if ((evflags & ~(EV_CLEAR | EV_DISPATCH | EV_ONESHOT)) != 0) {
1729 kqfd = aiocbe->uaiocb.aio_sigevent.sigev_notify_kqueue;
1730 kev.ident = (uintptr_t)aiocbe->uuaiocb;
1731 kev.filter = EVFILT_AIO;
1732 kev.flags = EV_ADD | EV_ENABLE | EV_FLAG1 | evflags;
1733 kev.data = (intptr_t)aiocbe;
1734 kev.udata = aiocbe->uaiocb.aio_sigevent.sigev_value.sival_ptr;
1735 error = kqfd_register(kqfd, &kev, td, 1);
1740 uma_zfree(aiocb_zone, aiocbe);
1741 ops->store_error(job, error);
1746 ops->store_error(job, EINPROGRESS);
1747 aiocbe->uaiocb._aiocb_private.error = EINPROGRESS;
1748 aiocbe->userproc = p;
1749 aiocbe->cred = crhold(td->td_ucred);
1750 aiocbe->jobflags = 0;
1753 if (opcode == LIO_SYNC)
1756 if (fp && fp->f_type == DTYPE_SOCKET) {
1758 * Alternate queueing for socket ops: Reach down into the
1759 * descriptor to get the socket data. Then check to see if the
1760 * socket is ready to be read or written (based on the requested
1763 * If it is not ready for io, then queue the aiocbe on the
1764 * socket, and set the flags so we get a call when sbnotify()
1767 * Note if opcode is neither LIO_WRITE nor LIO_READ we lock
1768 * and unlock the snd sockbuf for no reason.
1771 sb = (opcode == LIO_READ) ? &so->so_rcv : &so->so_snd;
1773 if (((opcode == LIO_READ) && (!soreadable(so))) || ((opcode ==
1774 LIO_WRITE) && (!sowriteable(so)))) {
1775 sb->sb_flags |= SB_AIO;
1777 mtx_lock(&aio_job_mtx);
1778 TAILQ_INSERT_TAIL(&so->so_aiojobq, aiocbe, list);
1779 mtx_unlock(&aio_job_mtx);
1782 TAILQ_INSERT_TAIL(&ki->kaio_all, aiocbe, allist);
1783 TAILQ_INSERT_TAIL(&ki->kaio_jobqueue, aiocbe, plist);
1784 aiocbe->jobstate = JOBST_JOBQSOCK;
1790 atomic_add_int(&num_queue_count, 1);
1797 if ((error = aio_qphysio(p, aiocbe)) == 0)
1801 aiocbe->uaiocb._aiocb_private.error = error;
1802 ops->store_error(job, error);
1807 atomic_add_int(&num_queue_count, 1);
1813 TAILQ_INSERT_TAIL(&ki->kaio_jobqueue, aiocbe, plist);
1814 TAILQ_INSERT_TAIL(&ki->kaio_all, aiocbe, allist);
1815 if (opcode == LIO_SYNC) {
1816 TAILQ_FOREACH(cb, &ki->kaio_jobqueue, plist) {
1817 if (cb->fd_file == aiocbe->fd_file &&
1818 cb->uaiocb.aio_lio_opcode != LIO_SYNC &&
1819 cb->seqno < aiocbe->seqno) {
1820 cb->jobflags |= AIOCBLIST_CHECKSYNC;
1824 TAILQ_FOREACH(cb, &ki->kaio_bufqueue, plist) {
1825 if (cb->fd_file == aiocbe->fd_file &&
1826 cb->uaiocb.aio_lio_opcode != LIO_SYNC &&
1827 cb->seqno < aiocbe->seqno) {
1828 cb->jobflags |= AIOCBLIST_CHECKSYNC;
1832 if (aiocbe->pending != 0) {
1833 TAILQ_INSERT_TAIL(&ki->kaio_syncqueue, aiocbe, list);
1834 aiocbe->jobstate = JOBST_JOBQSYNC;
1839 mtx_lock(&aio_job_mtx);
1840 TAILQ_INSERT_TAIL(&aio_jobs, aiocbe, list);
1841 aiocbe->jobstate = JOBST_JOBQGLOBAL;
1843 mtx_unlock(&aio_job_mtx);
1851 aio_kick_nowait(struct proc *userp)
1853 struct kaioinfo *ki = userp->p_aioinfo;
1854 struct aiothreadlist *aiop;
1856 mtx_assert(&aio_job_mtx, MA_OWNED);
1857 if ((aiop = TAILQ_FIRST(&aio_freeproc)) != NULL) {
1858 TAILQ_REMOVE(&aio_freeproc, aiop, list);
1859 aiop->aiothreadflags &= ~AIOP_FREE;
1860 wakeup(aiop->aiothread);
1861 } else if (((num_aio_resv_start + num_aio_procs) < max_aio_procs) &&
1862 ((ki->kaio_active_count + num_aio_resv_start) <
1863 ki->kaio_maxactive_count)) {
1864 taskqueue_enqueue(taskqueue_aiod_bio, &ki->kaio_task);
1869 aio_kick(struct proc *userp)
1871 struct kaioinfo *ki = userp->p_aioinfo;
1872 struct aiothreadlist *aiop;
1875 mtx_assert(&aio_job_mtx, MA_OWNED);
1877 if ((aiop = TAILQ_FIRST(&aio_freeproc)) != NULL) {
1878 TAILQ_REMOVE(&aio_freeproc, aiop, list);
1879 aiop->aiothreadflags &= ~AIOP_FREE;
1880 wakeup(aiop->aiothread);
1881 } else if (((num_aio_resv_start + num_aio_procs) < max_aio_procs) &&
1882 ((ki->kaio_active_count + num_aio_resv_start) <
1883 ki->kaio_maxactive_count)) {
1884 num_aio_resv_start++;
1885 mtx_unlock(&aio_job_mtx);
1886 error = aio_newproc(&num_aio_resv_start);
1887 mtx_lock(&aio_job_mtx);
1889 num_aio_resv_start--;
1899 aio_kick_helper(void *context, int pending)
1901 struct proc *userp = context;
1903 mtx_lock(&aio_job_mtx);
1904 while (--pending >= 0) {
1905 if (aio_kick(userp))
1908 mtx_unlock(&aio_job_mtx);
1912 * Support the aio_return system call, as a side-effect, kernel resources are
1916 kern_aio_return(struct thread *td, struct aiocb *uaiocb, struct aiocb_ops *ops)
1918 struct proc *p = td->td_proc;
1919 struct aiocblist *cb;
1920 struct kaioinfo *ki;
1927 TAILQ_FOREACH(cb, &ki->kaio_done, plist) {
1928 if (cb->uuaiocb == uaiocb)
1932 MPASS(cb->jobstate == JOBST_JOBFINISHED);
1933 status = cb->uaiocb._aiocb_private.status;
1934 error = cb->uaiocb._aiocb_private.error;
1935 td->td_retval[0] = status;
1936 if (cb->uaiocb.aio_lio_opcode == LIO_WRITE) {
1937 td->td_ru.ru_oublock += cb->outputcharge;
1938 cb->outputcharge = 0;
1939 } else if (cb->uaiocb.aio_lio_opcode == LIO_READ) {
1940 td->td_ru.ru_inblock += cb->inputcharge;
1941 cb->inputcharge = 0;
1945 ops->store_error(uaiocb, error);
1946 ops->store_status(uaiocb, status);
1955 sys_aio_return(struct thread *td, struct aio_return_args *uap)
1958 return (kern_aio_return(td, uap->aiocbp, &aiocb_ops));
1962 * Allow a process to wakeup when any of the I/O requests are completed.
1965 kern_aio_suspend(struct thread *td, int njoblist, struct aiocb **ujoblist,
1966 struct timespec *ts)
1968 struct proc *p = td->td_proc;
1970 struct kaioinfo *ki;
1971 struct aiocblist *cb, *cbfirst;
1976 if (ts->tv_nsec < 0 || ts->tv_nsec >= 1000000000)
1979 TIMESPEC_TO_TIMEVAL(&atv, ts);
1980 if (itimerfix(&atv))
1982 timo = tvtohz(&atv);
1996 TAILQ_FOREACH(cb, &ki->kaio_all, allist) {
1997 for (i = 0; i < njoblist; i++) {
1998 if (cb->uuaiocb == ujoblist[i]) {
1999 if (cbfirst == NULL)
2001 if (cb->jobstate == JOBST_JOBFINISHED)
2006 /* All tasks were finished. */
2007 if (cbfirst == NULL)
2010 ki->kaio_flags |= KAIO_WAKEUP;
2011 error = msleep(&p->p_aioinfo, AIO_MTX(ki), PRIBIO | PCATCH,
2013 if (error == ERESTART)
2024 sys_aio_suspend(struct thread *td, struct aio_suspend_args *uap)
2026 struct timespec ts, *tsp;
2027 struct aiocb **ujoblist;
2030 if (uap->nent < 0 || uap->nent > AIO_LISTIO_MAX)
2034 /* Get timespec struct. */
2035 if ((error = copyin(uap->timeout, &ts, sizeof(ts))) != 0)
2041 ujoblist = uma_zalloc(aiol_zone, M_WAITOK);
2042 error = copyin(uap->aiocbp, ujoblist, uap->nent * sizeof(ujoblist[0]));
2044 error = kern_aio_suspend(td, uap->nent, ujoblist, tsp);
2045 uma_zfree(aiol_zone, ujoblist);
2050 * aio_cancel cancels any non-physio aio operations not currently in
2054 sys_aio_cancel(struct thread *td, struct aio_cancel_args *uap)
2056 struct proc *p = td->td_proc;
2057 struct kaioinfo *ki;
2058 struct aiocblist *cbe, *cbn;
2064 int notcancelled = 0;
2067 /* Lookup file object. */
2068 error = fget(td, uap->fd, NULL, &fp);
2076 if (fp->f_type == DTYPE_VNODE) {
2078 if (vn_isdisk(vp, &error)) {
2080 td->td_retval[0] = AIO_NOTCANCELED;
2086 TAILQ_FOREACH_SAFE(cbe, &ki->kaio_jobqueue, plist, cbn) {
2087 if ((uap->fd == cbe->uaiocb.aio_fildes) &&
2088 ((uap->aiocbp == NULL) ||
2089 (uap->aiocbp == cbe->uuaiocb))) {
2092 mtx_lock(&aio_job_mtx);
2093 if (cbe->jobstate == JOBST_JOBQGLOBAL) {
2094 TAILQ_REMOVE(&aio_jobs, cbe, list);
2096 } else if (cbe->jobstate == JOBST_JOBQSOCK) {
2097 MPASS(fp->f_type == DTYPE_SOCKET);
2099 TAILQ_REMOVE(&so->so_aiojobq, cbe, list);
2101 } else if (cbe->jobstate == JOBST_JOBQSYNC) {
2102 TAILQ_REMOVE(&ki->kaio_syncqueue, cbe, list);
2105 mtx_unlock(&aio_job_mtx);
2108 TAILQ_REMOVE(&ki->kaio_jobqueue, cbe, plist);
2109 cbe->uaiocb._aiocb_private.status = -1;
2110 cbe->uaiocb._aiocb_private.error = ECANCELED;
2111 aio_bio_done_notify(p, cbe, DONE_QUEUE);
2116 if (uap->aiocbp != NULL)
2125 if (uap->aiocbp != NULL) {
2127 td->td_retval[0] = AIO_CANCELED;
2133 td->td_retval[0] = AIO_NOTCANCELED;
2138 td->td_retval[0] = AIO_CANCELED;
2142 td->td_retval[0] = AIO_ALLDONE;
2148 * aio_error is implemented in the kernel level for compatibility purposes
2149 * only. For a user mode async implementation, it would be best to do it in
2150 * a userland subroutine.
2153 kern_aio_error(struct thread *td, struct aiocb *aiocbp, struct aiocb_ops *ops)
2155 struct proc *p = td->td_proc;
2156 struct aiocblist *cb;
2157 struct kaioinfo *ki;
2162 td->td_retval[0] = EINVAL;
2167 TAILQ_FOREACH(cb, &ki->kaio_all, allist) {
2168 if (cb->uuaiocb == aiocbp) {
2169 if (cb->jobstate == JOBST_JOBFINISHED)
2171 cb->uaiocb._aiocb_private.error;
2173 td->td_retval[0] = EINPROGRESS;
2181 * Hack for failure of aio_aqueue.
2183 status = ops->fetch_status(aiocbp);
2185 td->td_retval[0] = ops->fetch_error(aiocbp);
2189 td->td_retval[0] = EINVAL;
2194 sys_aio_error(struct thread *td, struct aio_error_args *uap)
2197 return (kern_aio_error(td, uap->aiocbp, &aiocb_ops));
2200 /* syscall - asynchronous read from a file (REALTIME) */
2202 sys_oaio_read(struct thread *td, struct oaio_read_args *uap)
2205 return (aio_aqueue(td, (struct aiocb *)uap->aiocbp, NULL, LIO_READ,
2206 &aiocb_ops_osigevent));
2210 sys_aio_read(struct thread *td, struct aio_read_args *uap)
2213 return (aio_aqueue(td, uap->aiocbp, NULL, LIO_READ, &aiocb_ops));
2216 /* syscall - asynchronous write to a file (REALTIME) */
2218 sys_oaio_write(struct thread *td, struct oaio_write_args *uap)
2221 return (aio_aqueue(td, (struct aiocb *)uap->aiocbp, NULL, LIO_WRITE,
2222 &aiocb_ops_osigevent));
2226 sys_aio_write(struct thread *td, struct aio_write_args *uap)
2229 return (aio_aqueue(td, uap->aiocbp, NULL, LIO_WRITE, &aiocb_ops));
2233 sys_aio_mlock(struct thread *td, struct aio_mlock_args *uap)
2236 return (aio_aqueue(td, uap->aiocbp, NULL, LIO_MLOCK, &aiocb_ops));
2240 kern_lio_listio(struct thread *td, int mode, struct aiocb * const *uacb_list,
2241 struct aiocb **acb_list, int nent, struct sigevent *sig,
2242 struct aiocb_ops *ops)
2244 struct proc *p = td->td_proc;
2246 struct kaioinfo *ki;
2247 struct aioliojob *lj;
2253 if ((mode != LIO_NOWAIT) && (mode != LIO_WAIT))
2256 if (nent < 0 || nent > AIO_LISTIO_MAX)
2259 if (p->p_aioinfo == NULL)
2260 aio_init_aioinfo(p);
2264 lj = uma_zalloc(aiolio_zone, M_WAITOK);
2267 lj->lioj_finished_count = 0;
2268 knlist_init_mtx(&lj->klist, AIO_MTX(ki));
2269 ksiginfo_init(&lj->lioj_ksi);
2274 if (sig && (mode == LIO_NOWAIT)) {
2275 bcopy(sig, &lj->lioj_signal, sizeof(lj->lioj_signal));
2276 if (lj->lioj_signal.sigev_notify == SIGEV_KEVENT) {
2277 /* Assume only new style KEVENT */
2278 kev.filter = EVFILT_LIO;
2279 kev.flags = EV_ADD | EV_ENABLE | EV_FLAG1;
2280 kev.ident = (uintptr_t)uacb_list; /* something unique */
2281 kev.data = (intptr_t)lj;
2282 /* pass user defined sigval data */
2283 kev.udata = lj->lioj_signal.sigev_value.sival_ptr;
2284 error = kqfd_register(
2285 lj->lioj_signal.sigev_notify_kqueue, &kev, td, 1);
2287 uma_zfree(aiolio_zone, lj);
2290 } else if (lj->lioj_signal.sigev_notify == SIGEV_NONE) {
2292 } else if (lj->lioj_signal.sigev_notify == SIGEV_SIGNAL ||
2293 lj->lioj_signal.sigev_notify == SIGEV_THREAD_ID) {
2294 if (!_SIG_VALID(lj->lioj_signal.sigev_signo)) {
2295 uma_zfree(aiolio_zone, lj);
2298 lj->lioj_flags |= LIOJ_SIGNAL;
2300 uma_zfree(aiolio_zone, lj);
2306 TAILQ_INSERT_TAIL(&ki->kaio_liojoblist, lj, lioj_list);
2308 * Add extra aiocb count to avoid the lio to be freed
2309 * by other threads doing aio_waitcomplete or aio_return,
2310 * and prevent event from being sent until we have queued
2317 * Get pointers to the list of I/O requests.
2320 for (i = 0; i < nent; i++) {
2323 error = aio_aqueue(td, iocb, lj, LIO_NOP, ops);
2331 if (mode == LIO_WAIT) {
2332 while (lj->lioj_count - 1 != lj->lioj_finished_count) {
2333 ki->kaio_flags |= KAIO_WAKEUP;
2334 error = msleep(&p->p_aioinfo, AIO_MTX(ki),
2335 PRIBIO | PCATCH, "aiospn", 0);
2336 if (error == ERESTART)
2342 if (lj->lioj_count - 1 == lj->lioj_finished_count) {
2343 if (lj->lioj_signal.sigev_notify == SIGEV_KEVENT) {
2344 lj->lioj_flags |= LIOJ_KEVENT_POSTED;
2345 KNOTE_LOCKED(&lj->klist, 1);
2347 if ((lj->lioj_flags & (LIOJ_SIGNAL|LIOJ_SIGNAL_POSTED))
2349 && (lj->lioj_signal.sigev_notify == SIGEV_SIGNAL ||
2350 lj->lioj_signal.sigev_notify == SIGEV_THREAD_ID)) {
2351 aio_sendsig(p, &lj->lioj_signal,
2353 lj->lioj_flags |= LIOJ_SIGNAL_POSTED;
2358 if (lj->lioj_count == 0) {
2359 TAILQ_REMOVE(&ki->kaio_liojoblist, lj, lioj_list);
2360 knlist_delete(&lj->klist, curthread, 1);
2362 sigqueue_take(&lj->lioj_ksi);
2365 uma_zfree(aiolio_zone, lj);
2374 /* syscall - list directed I/O (REALTIME) */
2376 sys_olio_listio(struct thread *td, struct olio_listio_args *uap)
2378 struct aiocb **acb_list;
2379 struct sigevent *sigp, sig;
2380 struct osigevent osig;
2383 if ((uap->mode != LIO_NOWAIT) && (uap->mode != LIO_WAIT))
2387 if (nent < 0 || nent > AIO_LISTIO_MAX)
2390 if (uap->sig && (uap->mode == LIO_NOWAIT)) {
2391 error = copyin(uap->sig, &osig, sizeof(osig));
2394 error = convert_old_sigevent(&osig, &sig);
2401 acb_list = malloc(sizeof(struct aiocb *) * nent, M_LIO, M_WAITOK);
2402 error = copyin(uap->acb_list, acb_list, nent * sizeof(acb_list[0]));
2404 error = kern_lio_listio(td, uap->mode,
2405 (struct aiocb * const *)uap->acb_list, acb_list, nent, sigp,
2406 &aiocb_ops_osigevent);
2407 free(acb_list, M_LIO);
2411 /* syscall - list directed I/O (REALTIME) */
2413 sys_lio_listio(struct thread *td, struct lio_listio_args *uap)
2415 struct aiocb **acb_list;
2416 struct sigevent *sigp, sig;
2419 if ((uap->mode != LIO_NOWAIT) && (uap->mode != LIO_WAIT))
2423 if (nent < 0 || nent > AIO_LISTIO_MAX)
2426 if (uap->sig && (uap->mode == LIO_NOWAIT)) {
2427 error = copyin(uap->sig, &sig, sizeof(sig));
2434 acb_list = malloc(sizeof(struct aiocb *) * nent, M_LIO, M_WAITOK);
2435 error = copyin(uap->acb_list, acb_list, nent * sizeof(acb_list[0]));
2437 error = kern_lio_listio(td, uap->mode, uap->acb_list, acb_list,
2438 nent, sigp, &aiocb_ops);
2439 free(acb_list, M_LIO);
2444 aio_physwakeup(struct bio *bp)
2446 struct aiocblist *aiocbe = (struct aiocblist *)bp->bio_caller1;
2448 struct kaioinfo *ki;
2451 /* Release mapping into kernel space. */
2453 pmap_qremove((vm_offset_t)aiocbe->pbuf->b_data, aiocbe->npages);
2454 relpbuf(aiocbe->pbuf, NULL);
2455 aiocbe->pbuf = NULL;
2456 atomic_subtract_int(&num_buf_aio, 1);
2458 vm_page_unhold_pages(aiocbe->pages, aiocbe->npages);
2462 userp = aiocbe->userproc;
2463 ki = userp->p_aioinfo;
2465 aiocbe->uaiocb._aiocb_private.status -= bp->bio_resid;
2466 aiocbe->uaiocb._aiocb_private.error = 0;
2467 if (bp->bio_flags & BIO_ERROR)
2468 aiocbe->uaiocb._aiocb_private.error = bp->bio_error;
2469 nblks = btodb(aiocbe->uaiocb.aio_nbytes);
2470 if (aiocbe->uaiocb.aio_lio_opcode == LIO_WRITE)
2471 aiocbe->outputcharge += nblks;
2473 aiocbe->inputcharge += nblks;
2474 TAILQ_REMOVE(&userp->p_aioinfo->kaio_bufqueue, aiocbe, plist);
2475 ki->kaio_buffer_count--;
2476 aio_bio_done_notify(userp, aiocbe, DONE_BUF);
2482 /* syscall - wait for the next completion of an aio request */
2484 kern_aio_waitcomplete(struct thread *td, struct aiocb **aiocbp,
2485 struct timespec *ts, struct aiocb_ops *ops)
2487 struct proc *p = td->td_proc;
2489 struct kaioinfo *ki;
2490 struct aiocblist *cb;
2491 struct aiocb *uuaiocb;
2492 int error, status, timo;
2494 ops->store_aiocb(aiocbp, NULL);
2498 if ((ts->tv_nsec < 0) || (ts->tv_nsec >= 1000000000))
2501 TIMESPEC_TO_TIMEVAL(&atv, ts);
2502 if (itimerfix(&atv))
2504 timo = tvtohz(&atv);
2507 if (p->p_aioinfo == NULL)
2508 aio_init_aioinfo(p);
2514 while ((cb = TAILQ_FIRST(&ki->kaio_done)) == NULL) {
2515 ki->kaio_flags |= KAIO_WAKEUP;
2516 error = msleep(&p->p_aioinfo, AIO_MTX(ki), PRIBIO | PCATCH,
2518 if (timo && error == ERESTART)
2525 MPASS(cb->jobstate == JOBST_JOBFINISHED);
2526 uuaiocb = cb->uuaiocb;
2527 status = cb->uaiocb._aiocb_private.status;
2528 error = cb->uaiocb._aiocb_private.error;
2529 td->td_retval[0] = status;
2530 if (cb->uaiocb.aio_lio_opcode == LIO_WRITE) {
2531 td->td_ru.ru_oublock += cb->outputcharge;
2532 cb->outputcharge = 0;
2533 } else if (cb->uaiocb.aio_lio_opcode == LIO_READ) {
2534 td->td_ru.ru_inblock += cb->inputcharge;
2535 cb->inputcharge = 0;
2539 ops->store_aiocb(aiocbp, uuaiocb);
2540 ops->store_error(uuaiocb, error);
2541 ops->store_status(uuaiocb, status);
2549 sys_aio_waitcomplete(struct thread *td, struct aio_waitcomplete_args *uap)
2551 struct timespec ts, *tsp;
2555 /* Get timespec struct. */
2556 error = copyin(uap->timeout, &ts, sizeof(ts));
2563 return (kern_aio_waitcomplete(td, uap->aiocbp, tsp, &aiocb_ops));
2567 kern_aio_fsync(struct thread *td, int op, struct aiocb *aiocbp,
2568 struct aiocb_ops *ops)
2570 struct proc *p = td->td_proc;
2571 struct kaioinfo *ki;
2573 if (op != O_SYNC) /* XXX lack of O_DSYNC */
2577 aio_init_aioinfo(p);
2578 return (aio_aqueue(td, aiocbp, NULL, LIO_SYNC, ops));
2582 sys_aio_fsync(struct thread *td, struct aio_fsync_args *uap)
2585 return (kern_aio_fsync(td, uap->op, uap->aiocbp, &aiocb_ops));
2588 /* kqueue attach function */
2590 filt_aioattach(struct knote *kn)
2592 struct aiocblist *aiocbe = (struct aiocblist *)kn->kn_sdata;
2595 * The aiocbe pointer must be validated before using it, so
2596 * registration is restricted to the kernel; the user cannot
2599 if ((kn->kn_flags & EV_FLAG1) == 0)
2601 kn->kn_ptr.p_aio = aiocbe;
2602 kn->kn_flags &= ~EV_FLAG1;
2604 knlist_add(&aiocbe->klist, kn, 0);
2609 /* kqueue detach function */
2611 filt_aiodetach(struct knote *kn)
2615 knl = &kn->kn_ptr.p_aio->klist;
2616 knl->kl_lock(knl->kl_lockarg);
2617 if (!knlist_empty(knl))
2618 knlist_remove(knl, kn, 1);
2619 knl->kl_unlock(knl->kl_lockarg);
2622 /* kqueue filter function */
2625 filt_aio(struct knote *kn, long hint)
2627 struct aiocblist *aiocbe = kn->kn_ptr.p_aio;
2629 kn->kn_data = aiocbe->uaiocb._aiocb_private.error;
2630 if (aiocbe->jobstate != JOBST_JOBFINISHED)
2632 kn->kn_flags |= EV_EOF;
2636 /* kqueue attach function */
2638 filt_lioattach(struct knote *kn)
2640 struct aioliojob * lj = (struct aioliojob *)kn->kn_sdata;
2643 * The aioliojob pointer must be validated before using it, so
2644 * registration is restricted to the kernel; the user cannot
2647 if ((kn->kn_flags & EV_FLAG1) == 0)
2649 kn->kn_ptr.p_lio = lj;
2650 kn->kn_flags &= ~EV_FLAG1;
2652 knlist_add(&lj->klist, kn, 0);
2657 /* kqueue detach function */
2659 filt_liodetach(struct knote *kn)
2663 knl = &kn->kn_ptr.p_lio->klist;
2664 knl->kl_lock(knl->kl_lockarg);
2665 if (!knlist_empty(knl))
2666 knlist_remove(knl, kn, 1);
2667 knl->kl_unlock(knl->kl_lockarg);
2670 /* kqueue filter function */
2673 filt_lio(struct knote *kn, long hint)
2675 struct aioliojob * lj = kn->kn_ptr.p_lio;
2677 return (lj->lioj_flags & LIOJ_KEVENT_POSTED);
2680 #ifdef COMPAT_FREEBSD32
2682 struct __aiocb_private32 {
2685 uint32_t kernelinfo;
2688 typedef struct oaiocb32 {
2689 int aio_fildes; /* File descriptor */
2690 uint64_t aio_offset __packed; /* File offset for I/O */
2691 uint32_t aio_buf; /* I/O buffer in process space */
2692 uint32_t aio_nbytes; /* Number of bytes for I/O */
2693 struct osigevent32 aio_sigevent; /* Signal to deliver */
2694 int aio_lio_opcode; /* LIO opcode */
2695 int aio_reqprio; /* Request priority -- ignored */
2696 struct __aiocb_private32 _aiocb_private;
2699 typedef struct aiocb32 {
2700 int32_t aio_fildes; /* File descriptor */
2701 uint64_t aio_offset __packed; /* File offset for I/O */
2702 uint32_t aio_buf; /* I/O buffer in process space */
2703 uint32_t aio_nbytes; /* Number of bytes for I/O */
2705 uint32_t __spare2__;
2706 int aio_lio_opcode; /* LIO opcode */
2707 int aio_reqprio; /* Request priority -- ignored */
2708 struct __aiocb_private32 _aiocb_private;
2709 struct sigevent32 aio_sigevent; /* Signal to deliver */
2713 convert_old_sigevent32(struct osigevent32 *osig, struct sigevent *nsig)
2717 * Only SIGEV_NONE, SIGEV_SIGNAL, and SIGEV_KEVENT are
2718 * supported by AIO with the old sigevent structure.
2720 CP(*osig, *nsig, sigev_notify);
2721 switch (nsig->sigev_notify) {
2725 nsig->sigev_signo = osig->__sigev_u.__sigev_signo;
2728 nsig->sigev_notify_kqueue =
2729 osig->__sigev_u.__sigev_notify_kqueue;
2730 PTRIN_CP(*osig, *nsig, sigev_value.sival_ptr);
2739 aiocb32_copyin_old_sigevent(struct aiocb *ujob, struct aiocb *kjob)
2741 struct oaiocb32 job32;
2744 bzero(kjob, sizeof(struct aiocb));
2745 error = copyin(ujob, &job32, sizeof(job32));
2749 CP(job32, *kjob, aio_fildes);
2750 CP(job32, *kjob, aio_offset);
2751 PTRIN_CP(job32, *kjob, aio_buf);
2752 CP(job32, *kjob, aio_nbytes);
2753 CP(job32, *kjob, aio_lio_opcode);
2754 CP(job32, *kjob, aio_reqprio);
2755 CP(job32, *kjob, _aiocb_private.status);
2756 CP(job32, *kjob, _aiocb_private.error);
2757 PTRIN_CP(job32, *kjob, _aiocb_private.kernelinfo);
2758 return (convert_old_sigevent32(&job32.aio_sigevent,
2759 &kjob->aio_sigevent));
2763 aiocb32_copyin(struct aiocb *ujob, struct aiocb *kjob)
2765 struct aiocb32 job32;
2768 error = copyin(ujob, &job32, sizeof(job32));
2771 CP(job32, *kjob, aio_fildes);
2772 CP(job32, *kjob, aio_offset);
2773 PTRIN_CP(job32, *kjob, aio_buf);
2774 CP(job32, *kjob, aio_nbytes);
2775 CP(job32, *kjob, aio_lio_opcode);
2776 CP(job32, *kjob, aio_reqprio);
2777 CP(job32, *kjob, _aiocb_private.status);
2778 CP(job32, *kjob, _aiocb_private.error);
2779 PTRIN_CP(job32, *kjob, _aiocb_private.kernelinfo);
2780 return (convert_sigevent32(&job32.aio_sigevent, &kjob->aio_sigevent));
2784 aiocb32_fetch_status(struct aiocb *ujob)
2786 struct aiocb32 *ujob32;
2788 ujob32 = (struct aiocb32 *)ujob;
2789 return (fuword32(&ujob32->_aiocb_private.status));
2793 aiocb32_fetch_error(struct aiocb *ujob)
2795 struct aiocb32 *ujob32;
2797 ujob32 = (struct aiocb32 *)ujob;
2798 return (fuword32(&ujob32->_aiocb_private.error));
2802 aiocb32_store_status(struct aiocb *ujob, long status)
2804 struct aiocb32 *ujob32;
2806 ujob32 = (struct aiocb32 *)ujob;
2807 return (suword32(&ujob32->_aiocb_private.status, status));
2811 aiocb32_store_error(struct aiocb *ujob, long error)
2813 struct aiocb32 *ujob32;
2815 ujob32 = (struct aiocb32 *)ujob;
2816 return (suword32(&ujob32->_aiocb_private.error, error));
2820 aiocb32_store_kernelinfo(struct aiocb *ujob, long jobref)
2822 struct aiocb32 *ujob32;
2824 ujob32 = (struct aiocb32 *)ujob;
2825 return (suword32(&ujob32->_aiocb_private.kernelinfo, jobref));
2829 aiocb32_store_aiocb(struct aiocb **ujobp, struct aiocb *ujob)
2832 return (suword32(ujobp, (long)ujob));
2835 static struct aiocb_ops aiocb32_ops = {
2836 .copyin = aiocb32_copyin,
2837 .fetch_status = aiocb32_fetch_status,
2838 .fetch_error = aiocb32_fetch_error,
2839 .store_status = aiocb32_store_status,
2840 .store_error = aiocb32_store_error,
2841 .store_kernelinfo = aiocb32_store_kernelinfo,
2842 .store_aiocb = aiocb32_store_aiocb,
2845 static struct aiocb_ops aiocb32_ops_osigevent = {
2846 .copyin = aiocb32_copyin_old_sigevent,
2847 .fetch_status = aiocb32_fetch_status,
2848 .fetch_error = aiocb32_fetch_error,
2849 .store_status = aiocb32_store_status,
2850 .store_error = aiocb32_store_error,
2851 .store_kernelinfo = aiocb32_store_kernelinfo,
2852 .store_aiocb = aiocb32_store_aiocb,
2856 freebsd32_aio_return(struct thread *td, struct freebsd32_aio_return_args *uap)
2859 return (kern_aio_return(td, (struct aiocb *)uap->aiocbp, &aiocb32_ops));
2863 freebsd32_aio_suspend(struct thread *td, struct freebsd32_aio_suspend_args *uap)
2865 struct timespec32 ts32;
2866 struct timespec ts, *tsp;
2867 struct aiocb **ujoblist;
2868 uint32_t *ujoblist32;
2871 if (uap->nent < 0 || uap->nent > AIO_LISTIO_MAX)
2875 /* Get timespec struct. */
2876 if ((error = copyin(uap->timeout, &ts32, sizeof(ts32))) != 0)
2878 CP(ts32, ts, tv_sec);
2879 CP(ts32, ts, tv_nsec);
2884 ujoblist = uma_zalloc(aiol_zone, M_WAITOK);
2885 ujoblist32 = (uint32_t *)ujoblist;
2886 error = copyin(uap->aiocbp, ujoblist32, uap->nent *
2887 sizeof(ujoblist32[0]));
2889 for (i = uap->nent; i > 0; i--)
2890 ujoblist[i] = PTRIN(ujoblist32[i]);
2892 error = kern_aio_suspend(td, uap->nent, ujoblist, tsp);
2894 uma_zfree(aiol_zone, ujoblist);
2899 freebsd32_aio_cancel(struct thread *td, struct freebsd32_aio_cancel_args *uap)
2902 return (sys_aio_cancel(td, (struct aio_cancel_args *)uap));
2906 freebsd32_aio_error(struct thread *td, struct freebsd32_aio_error_args *uap)
2909 return (kern_aio_error(td, (struct aiocb *)uap->aiocbp, &aiocb32_ops));
2913 freebsd32_oaio_read(struct thread *td, struct freebsd32_oaio_read_args *uap)
2916 return (aio_aqueue(td, (struct aiocb *)uap->aiocbp, NULL, LIO_READ,
2917 &aiocb32_ops_osigevent));
2921 freebsd32_aio_read(struct thread *td, struct freebsd32_aio_read_args *uap)
2924 return (aio_aqueue(td, (struct aiocb *)uap->aiocbp, NULL, LIO_READ,
2929 freebsd32_oaio_write(struct thread *td, struct freebsd32_oaio_write_args *uap)
2932 return (aio_aqueue(td, (struct aiocb *)uap->aiocbp, NULL, LIO_WRITE,
2933 &aiocb32_ops_osigevent));
2937 freebsd32_aio_write(struct thread *td, struct freebsd32_aio_write_args *uap)
2940 return (aio_aqueue(td, (struct aiocb *)uap->aiocbp, NULL, LIO_WRITE,
2945 freebsd32_aio_mlock(struct thread *td, struct freebsd32_aio_mlock_args *uap)
2948 return (aio_aqueue(td, (struct aiocb *)uap->aiocbp, NULL, LIO_MLOCK,
2953 freebsd32_aio_waitcomplete(struct thread *td,
2954 struct freebsd32_aio_waitcomplete_args *uap)
2956 struct timespec32 ts32;
2957 struct timespec ts, *tsp;
2961 /* Get timespec struct. */
2962 error = copyin(uap->timeout, &ts32, sizeof(ts32));
2965 CP(ts32, ts, tv_sec);
2966 CP(ts32, ts, tv_nsec);
2971 return (kern_aio_waitcomplete(td, (struct aiocb **)uap->aiocbp, tsp,
2976 freebsd32_aio_fsync(struct thread *td, struct freebsd32_aio_fsync_args *uap)
2979 return (kern_aio_fsync(td, uap->op, (struct aiocb *)uap->aiocbp,
2984 freebsd32_olio_listio(struct thread *td, struct freebsd32_olio_listio_args *uap)
2986 struct aiocb **acb_list;
2987 struct sigevent *sigp, sig;
2988 struct osigevent32 osig;
2989 uint32_t *acb_list32;
2992 if ((uap->mode != LIO_NOWAIT) && (uap->mode != LIO_WAIT))
2996 if (nent < 0 || nent > AIO_LISTIO_MAX)
2999 if (uap->sig && (uap->mode == LIO_NOWAIT)) {
3000 error = copyin(uap->sig, &osig, sizeof(osig));
3003 error = convert_old_sigevent32(&osig, &sig);
3010 acb_list32 = malloc(sizeof(uint32_t) * nent, M_LIO, M_WAITOK);
3011 error = copyin(uap->acb_list, acb_list32, nent * sizeof(uint32_t));
3013 free(acb_list32, M_LIO);
3016 acb_list = malloc(sizeof(struct aiocb *) * nent, M_LIO, M_WAITOK);
3017 for (i = 0; i < nent; i++)
3018 acb_list[i] = PTRIN(acb_list32[i]);
3019 free(acb_list32, M_LIO);
3021 error = kern_lio_listio(td, uap->mode,
3022 (struct aiocb * const *)uap->acb_list, acb_list, nent, sigp,
3023 &aiocb32_ops_osigevent);
3024 free(acb_list, M_LIO);
3029 freebsd32_lio_listio(struct thread *td, struct freebsd32_lio_listio_args *uap)
3031 struct aiocb **acb_list;
3032 struct sigevent *sigp, sig;
3033 struct sigevent32 sig32;
3034 uint32_t *acb_list32;
3037 if ((uap->mode != LIO_NOWAIT) && (uap->mode != LIO_WAIT))
3041 if (nent < 0 || nent > AIO_LISTIO_MAX)
3044 if (uap->sig && (uap->mode == LIO_NOWAIT)) {
3045 error = copyin(uap->sig, &sig32, sizeof(sig32));
3048 error = convert_sigevent32(&sig32, &sig);
3055 acb_list32 = malloc(sizeof(uint32_t) * nent, M_LIO, M_WAITOK);
3056 error = copyin(uap->acb_list, acb_list32, nent * sizeof(uint32_t));
3058 free(acb_list32, M_LIO);
3061 acb_list = malloc(sizeof(struct aiocb *) * nent, M_LIO, M_WAITOK);
3062 for (i = 0; i < nent; i++)
3063 acb_list[i] = PTRIN(acb_list32[i]);
3064 free(acb_list32, M_LIO);
3066 error = kern_lio_listio(td, uap->mode,
3067 (struct aiocb * const *)uap->acb_list, acb_list, nent, sigp,
3069 free(acb_list, M_LIO);