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_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,
129 "Async IO management");
131 static int max_aio_procs = MAX_AIO_PROCS;
132 SYSCTL_INT(_vfs_aio, OID_AUTO, max_aio_procs, CTLFLAG_RW, &max_aio_procs, 0,
133 "Maximum number of kernel processes to use for handling async IO ");
135 static int num_aio_procs = 0;
136 SYSCTL_INT(_vfs_aio, OID_AUTO, num_aio_procs, CTLFLAG_RD, &num_aio_procs, 0,
137 "Number of presently active kernel processes for async IO");
140 * The code will adjust the actual number of AIO processes towards this
141 * number when it gets a chance.
143 static int target_aio_procs = TARGET_AIO_PROCS;
144 SYSCTL_INT(_vfs_aio, OID_AUTO, target_aio_procs, CTLFLAG_RW, &target_aio_procs,
146 "Preferred number of ready kernel processes 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 processes 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_lifetime;
165 SYSCTL_INT(_vfs_aio, OID_AUTO, aiod_lifetime, CTLFLAG_RW, &aiod_lifetime, 0,
166 "Maximum lifetime for idle aiod");
168 static int unloadable = 0;
169 SYSCTL_INT(_vfs_aio, OID_AUTO, unloadable, CTLFLAG_RW, &unloadable, 0,
170 "Allow unload of aio (not recommended)");
173 static int max_aio_per_proc = MAX_AIO_PER_PROC;
174 SYSCTL_INT(_vfs_aio, OID_AUTO, max_aio_per_proc, CTLFLAG_RW, &max_aio_per_proc,
176 "Maximum active aio requests per process (stored in the process)");
178 static int max_aio_queue_per_proc = MAX_AIO_QUEUE_PER_PROC;
179 SYSCTL_INT(_vfs_aio, OID_AUTO, max_aio_queue_per_proc, CTLFLAG_RW,
180 &max_aio_queue_per_proc, 0,
181 "Maximum queued aio requests per process (stored in the process)");
183 static int max_buf_aio = MAX_BUF_AIO;
184 SYSCTL_INT(_vfs_aio, OID_AUTO, max_buf_aio, CTLFLAG_RW, &max_buf_aio, 0,
185 "Maximum buf aio requests per process (stored in the process)");
187 typedef struct oaiocb {
188 int aio_fildes; /* File descriptor */
189 off_t aio_offset; /* File offset for I/O */
190 volatile void *aio_buf; /* I/O buffer in process space */
191 size_t aio_nbytes; /* Number of bytes for I/O */
192 struct osigevent aio_sigevent; /* Signal to deliver */
193 int aio_lio_opcode; /* LIO opcode */
194 int aio_reqprio; /* Request priority -- ignored */
195 struct __aiocb_private _aiocb_private;
199 * Below is a key of locks used to protect each member of struct kaiocb
200 * aioliojob and kaioinfo and any backends.
202 * * - need not protected
203 * a - locked by kaioinfo lock
204 * b - locked by backend lock, the backend lock can be null in some cases,
205 * for example, BIO belongs to this type, in this case, proc lock is
207 * c - locked by aio_job_mtx, the lock for the generic file I/O backend.
211 * Current, there is only two backends: BIO and generic file I/O.
212 * socket I/O is served by generic file I/O, this is not a good idea, since
213 * disk file I/O and any other types without O_NONBLOCK flag can block daemon
214 * processes, if there is no thread to serve socket I/O, the socket I/O will be
215 * delayed too long or starved, we should create some processes dedicated to
216 * sockets to do non-blocking I/O, same for pipe and fifo, for these I/O
217 * systems we really need non-blocking interface, fiddling O_NONBLOCK in file
218 * structure is not safe because there is race between userland and aio
223 TAILQ_ENTRY(kaiocb) list; /* (b) internal list of for backend */
224 TAILQ_ENTRY(kaiocb) plist; /* (a) list of jobs for each backend */
225 TAILQ_ENTRY(kaiocb) allist; /* (a) list of all jobs in proc */
226 int jobflags; /* (a) job flags */
227 int jobstate; /* (b) job state */
228 int inputcharge; /* (*) input blockes */
229 int outputcharge; /* (*) output blockes */
230 struct bio *bp; /* (*) BIO backend BIO pointer */
231 struct buf *pbuf; /* (*) BIO backend buffer pointer */
232 struct vm_page *pages[btoc(MAXPHYS)+1]; /* BIO backend pages */
233 int npages; /* BIO backend number of pages */
234 struct proc *userproc; /* (*) user process */
235 struct ucred *cred; /* (*) active credential when created */
236 struct file *fd_file; /* (*) pointer to file structure */
237 struct aioliojob *lio; /* (*) optional lio job */
238 struct aiocb *ujob; /* (*) pointer in userspace of aiocb */
239 struct knlist klist; /* (a) list of knotes */
240 struct aiocb uaiocb; /* (*) kernel I/O control block */
241 ksiginfo_t ksi; /* (a) realtime signal info */
242 uint64_t seqno; /* (*) job number */
243 int pending; /* (a) number of pending I/O, aio_fsync only */
247 #define KAIOCB_DONE 0x01
248 #define KAIOCB_BUFDONE 0x02
249 #define KAIOCB_RUNDOWN 0x04
250 #define KAIOCB_CHECKSYNC 0x08
255 #define AIOP_FREE 0x1 /* proc on free queue */
258 int aioprocflags; /* (c) AIO proc flags */
259 TAILQ_ENTRY(aioproc) list; /* (c) list of processes */
260 struct proc *aioproc; /* (*) the AIO proc */
264 * data-structure for lio signal management
267 int lioj_flags; /* (a) listio flags */
268 int lioj_count; /* (a) listio flags */
269 int lioj_finished_count; /* (a) listio flags */
270 struct sigevent lioj_signal; /* (a) signal on all I/O done */
271 TAILQ_ENTRY(aioliojob) lioj_list; /* (a) lio list */
272 struct knlist klist; /* (a) list of knotes */
273 ksiginfo_t lioj_ksi; /* (a) Realtime signal info */
276 #define LIOJ_SIGNAL 0x1 /* signal on all done (lio) */
277 #define LIOJ_SIGNAL_POSTED 0x2 /* signal has been posted */
278 #define LIOJ_KEVENT_POSTED 0x4 /* kevent triggered */
281 * per process aio data structure
284 struct mtx kaio_mtx; /* the lock to protect this struct */
285 int kaio_flags; /* (a) per process kaio flags */
286 int kaio_maxactive_count; /* (*) maximum number of AIOs */
287 int kaio_active_count; /* (c) number of currently used AIOs */
288 int kaio_qallowed_count; /* (*) maxiumu size of AIO queue */
289 int kaio_count; /* (a) size of AIO queue */
290 int kaio_ballowed_count; /* (*) maximum number of buffers */
291 int kaio_buffer_count; /* (a) number of physio buffers */
292 TAILQ_HEAD(,kaiocb) kaio_all; /* (a) all AIOs in a process */
293 TAILQ_HEAD(,kaiocb) kaio_done; /* (a) done queue for process */
294 TAILQ_HEAD(,aioliojob) kaio_liojoblist; /* (a) list of lio jobs */
295 TAILQ_HEAD(,kaiocb) kaio_jobqueue; /* (a) job queue for process */
296 TAILQ_HEAD(,kaiocb) kaio_bufqueue; /* (a) buffer job queue */
297 TAILQ_HEAD(,kaiocb) kaio_syncqueue; /* (a) queue for aio_fsync */
298 struct task kaio_task; /* (*) task to kick aio processes */
301 #define AIO_LOCK(ki) mtx_lock(&(ki)->kaio_mtx)
302 #define AIO_UNLOCK(ki) mtx_unlock(&(ki)->kaio_mtx)
303 #define AIO_LOCK_ASSERT(ki, f) mtx_assert(&(ki)->kaio_mtx, (f))
304 #define AIO_MTX(ki) (&(ki)->kaio_mtx)
306 #define KAIO_RUNDOWN 0x1 /* process is being run down */
307 #define KAIO_WAKEUP 0x2 /* wakeup process when AIO completes */
310 * Operations used to interact with userland aio control blocks.
311 * Different ABIs provide their own operations.
314 int (*copyin)(struct aiocb *ujob, struct aiocb *kjob);
315 long (*fetch_status)(struct aiocb *ujob);
316 long (*fetch_error)(struct aiocb *ujob);
317 int (*store_status)(struct aiocb *ujob, long status);
318 int (*store_error)(struct aiocb *ujob, long error);
319 int (*store_kernelinfo)(struct aiocb *ujob, long jobref);
320 int (*store_aiocb)(struct aiocb **ujobp, struct aiocb *ujob);
323 static TAILQ_HEAD(,aioproc) aio_freeproc; /* (c) Idle daemons */
324 static struct sema aio_newproc_sem;
325 static struct mtx aio_job_mtx;
326 static TAILQ_HEAD(,kaiocb) aio_jobs; /* (c) Async job list */
327 static struct unrhdr *aiod_unr;
329 void aio_init_aioinfo(struct proc *p);
330 static int aio_onceonly(void);
331 static int aio_free_entry(struct kaiocb *job);
332 static void aio_process_rw(struct kaiocb *job);
333 static void aio_process_sync(struct kaiocb *job);
334 static void aio_process_mlock(struct kaiocb *job);
335 static int aio_newproc(int *);
336 int aio_aqueue(struct thread *td, struct aiocb *ujob,
337 struct aioliojob *lio, int type, struct aiocb_ops *ops);
338 static void aio_physwakeup(struct bio *bp);
339 static void aio_proc_rundown(void *arg, struct proc *p);
340 static void aio_proc_rundown_exec(void *arg, struct proc *p,
341 struct image_params *imgp);
342 static int aio_qphysio(struct proc *p, struct kaiocb *job);
343 static void aio_daemon(void *param);
344 static void aio_swake_cb(struct socket *, struct sockbuf *);
345 static int aio_unload(void);
346 static void aio_bio_done_notify(struct proc *userp, struct kaiocb *job,
350 static int aio_kick(struct proc *userp);
351 static void aio_kick_nowait(struct proc *userp);
352 static void aio_kick_helper(void *context, int pending);
353 static int filt_aioattach(struct knote *kn);
354 static void filt_aiodetach(struct knote *kn);
355 static int filt_aio(struct knote *kn, long hint);
356 static int filt_lioattach(struct knote *kn);
357 static void filt_liodetach(struct knote *kn);
358 static int filt_lio(struct knote *kn, long hint);
362 * kaio Per process async io info
363 * aiop async io process data
364 * aiocb async io jobs
365 * aiol list io job pointer - internal to aio_suspend XXX
366 * aiolio list io jobs
368 static uma_zone_t kaio_zone, aiop_zone, aiocb_zone, aiol_zone, aiolio_zone;
370 /* kqueue filters for aio */
371 static struct filterops aio_filtops = {
373 .f_attach = filt_aioattach,
374 .f_detach = filt_aiodetach,
377 static struct filterops lio_filtops = {
379 .f_attach = filt_lioattach,
380 .f_detach = filt_liodetach,
384 static eventhandler_tag exit_tag, exec_tag;
386 TASKQUEUE_DEFINE_THREAD(aiod_kick);
389 * Main operations function for use as a kernel module.
392 aio_modload(struct module *module, int cmd, void *arg)
401 error = aio_unload();
412 static moduledata_t aio_mod = {
418 static struct syscall_helper_data aio_syscalls[] = {
419 SYSCALL_INIT_HELPER(aio_cancel),
420 SYSCALL_INIT_HELPER(aio_error),
421 SYSCALL_INIT_HELPER(aio_fsync),
422 SYSCALL_INIT_HELPER(aio_mlock),
423 SYSCALL_INIT_HELPER(aio_read),
424 SYSCALL_INIT_HELPER(aio_return),
425 SYSCALL_INIT_HELPER(aio_suspend),
426 SYSCALL_INIT_HELPER(aio_waitcomplete),
427 SYSCALL_INIT_HELPER(aio_write),
428 SYSCALL_INIT_HELPER(lio_listio),
429 SYSCALL_INIT_HELPER(oaio_read),
430 SYSCALL_INIT_HELPER(oaio_write),
431 SYSCALL_INIT_HELPER(olio_listio),
435 #ifdef COMPAT_FREEBSD32
436 #include <sys/mount.h>
437 #include <sys/socket.h>
438 #include <compat/freebsd32/freebsd32.h>
439 #include <compat/freebsd32/freebsd32_proto.h>
440 #include <compat/freebsd32/freebsd32_signal.h>
441 #include <compat/freebsd32/freebsd32_syscall.h>
442 #include <compat/freebsd32/freebsd32_util.h>
444 static struct syscall_helper_data aio32_syscalls[] = {
445 SYSCALL32_INIT_HELPER(freebsd32_aio_return),
446 SYSCALL32_INIT_HELPER(freebsd32_aio_suspend),
447 SYSCALL32_INIT_HELPER(freebsd32_aio_cancel),
448 SYSCALL32_INIT_HELPER(freebsd32_aio_error),
449 SYSCALL32_INIT_HELPER(freebsd32_aio_fsync),
450 SYSCALL32_INIT_HELPER(freebsd32_aio_mlock),
451 SYSCALL32_INIT_HELPER(freebsd32_aio_read),
452 SYSCALL32_INIT_HELPER(freebsd32_aio_write),
453 SYSCALL32_INIT_HELPER(freebsd32_aio_waitcomplete),
454 SYSCALL32_INIT_HELPER(freebsd32_lio_listio),
455 SYSCALL32_INIT_HELPER(freebsd32_oaio_read),
456 SYSCALL32_INIT_HELPER(freebsd32_oaio_write),
457 SYSCALL32_INIT_HELPER(freebsd32_olio_listio),
462 DECLARE_MODULE(aio, aio_mod,
463 SI_SUB_VFS, SI_ORDER_ANY);
464 MODULE_VERSION(aio, 1);
467 * Startup initialization
474 /* XXX: should probably just use so->callback */
475 aio_swake = &aio_swake_cb;
476 exit_tag = EVENTHANDLER_REGISTER(process_exit, aio_proc_rundown, NULL,
477 EVENTHANDLER_PRI_ANY);
478 exec_tag = EVENTHANDLER_REGISTER(process_exec, aio_proc_rundown_exec,
479 NULL, EVENTHANDLER_PRI_ANY);
480 kqueue_add_filteropts(EVFILT_AIO, &aio_filtops);
481 kqueue_add_filteropts(EVFILT_LIO, &lio_filtops);
482 TAILQ_INIT(&aio_freeproc);
483 sema_init(&aio_newproc_sem, 0, "aio_new_proc");
484 mtx_init(&aio_job_mtx, "aio_job", NULL, MTX_DEF);
485 TAILQ_INIT(&aio_jobs);
486 aiod_unr = new_unrhdr(1, INT_MAX, NULL);
487 kaio_zone = uma_zcreate("AIO", sizeof(struct kaioinfo), NULL, NULL,
488 NULL, NULL, UMA_ALIGN_PTR, UMA_ZONE_NOFREE);
489 aiop_zone = uma_zcreate("AIOP", sizeof(struct aioproc), NULL,
490 NULL, NULL, NULL, UMA_ALIGN_PTR, UMA_ZONE_NOFREE);
491 aiocb_zone = uma_zcreate("AIOCB", sizeof(struct kaiocb), NULL, NULL,
492 NULL, NULL, UMA_ALIGN_PTR, UMA_ZONE_NOFREE);
493 aiol_zone = uma_zcreate("AIOL", AIO_LISTIO_MAX*sizeof(intptr_t) , NULL,
494 NULL, NULL, NULL, UMA_ALIGN_PTR, UMA_ZONE_NOFREE);
495 aiolio_zone = uma_zcreate("AIOLIO", sizeof(struct aioliojob), NULL,
496 NULL, NULL, NULL, UMA_ALIGN_PTR, UMA_ZONE_NOFREE);
497 aiod_lifetime = AIOD_LIFETIME_DEFAULT;
499 async_io_version = _POSIX_VERSION;
500 p31b_setcfg(CTL_P1003_1B_AIO_LISTIO_MAX, AIO_LISTIO_MAX);
501 p31b_setcfg(CTL_P1003_1B_AIO_MAX, MAX_AIO_QUEUE);
502 p31b_setcfg(CTL_P1003_1B_AIO_PRIO_DELTA_MAX, 0);
504 error = syscall_helper_register(aio_syscalls, SY_THR_STATIC_KLD);
507 #ifdef COMPAT_FREEBSD32
508 error = syscall32_helper_register(aio32_syscalls, SY_THR_STATIC_KLD);
516 * Callback for unload of AIO when used as a module.
524 * XXX: no unloads by default, it's too dangerous.
525 * perhaps we could do it if locked out callers and then
526 * did an aio_proc_rundown() on each process.
528 * jhb: aio_proc_rundown() needs to run on curproc though,
529 * so I don't think that would fly.
534 #ifdef COMPAT_FREEBSD32
535 syscall32_helper_unregister(aio32_syscalls);
537 syscall_helper_unregister(aio_syscalls);
539 error = kqueue_del_filteropts(EVFILT_AIO);
542 error = kqueue_del_filteropts(EVFILT_LIO);
545 async_io_version = 0;
547 taskqueue_free(taskqueue_aiod_kick);
548 delete_unrhdr(aiod_unr);
549 uma_zdestroy(kaio_zone);
550 uma_zdestroy(aiop_zone);
551 uma_zdestroy(aiocb_zone);
552 uma_zdestroy(aiol_zone);
553 uma_zdestroy(aiolio_zone);
554 EVENTHANDLER_DEREGISTER(process_exit, exit_tag);
555 EVENTHANDLER_DEREGISTER(process_exec, exec_tag);
556 mtx_destroy(&aio_job_mtx);
557 sema_destroy(&aio_newproc_sem);
558 p31b_setcfg(CTL_P1003_1B_AIO_LISTIO_MAX, -1);
559 p31b_setcfg(CTL_P1003_1B_AIO_MAX, -1);
560 p31b_setcfg(CTL_P1003_1B_AIO_PRIO_DELTA_MAX, -1);
565 * Init the per-process aioinfo structure. The aioinfo limits are set
566 * per-process for user limit (resource) management.
569 aio_init_aioinfo(struct proc *p)
573 ki = uma_zalloc(kaio_zone, M_WAITOK);
574 mtx_init(&ki->kaio_mtx, "aiomtx", NULL, MTX_DEF | MTX_NEW);
576 ki->kaio_maxactive_count = max_aio_per_proc;
577 ki->kaio_active_count = 0;
578 ki->kaio_qallowed_count = max_aio_queue_per_proc;
580 ki->kaio_ballowed_count = max_buf_aio;
581 ki->kaio_buffer_count = 0;
582 TAILQ_INIT(&ki->kaio_all);
583 TAILQ_INIT(&ki->kaio_done);
584 TAILQ_INIT(&ki->kaio_jobqueue);
585 TAILQ_INIT(&ki->kaio_bufqueue);
586 TAILQ_INIT(&ki->kaio_liojoblist);
587 TAILQ_INIT(&ki->kaio_syncqueue);
588 TASK_INIT(&ki->kaio_task, 0, aio_kick_helper, p);
590 if (p->p_aioinfo == NULL) {
595 mtx_destroy(&ki->kaio_mtx);
596 uma_zfree(kaio_zone, ki);
599 while (num_aio_procs < MIN(target_aio_procs, max_aio_procs))
604 aio_sendsig(struct proc *p, struct sigevent *sigev, ksiginfo_t *ksi)
609 error = sigev_findtd(p, sigev, &td);
613 ksiginfo_set_sigev(ksi, sigev);
614 ksi->ksi_code = SI_ASYNCIO;
615 ksi->ksi_flags |= KSI_EXT | KSI_INS;
616 tdsendsignal(p, td, ksi->ksi_signo, ksi);
623 * Free a job entry. Wait for completion if it is currently active, but don't
624 * delay forever. If we delay, we return a flag that says that we have to
625 * restart the queue scan.
628 aio_free_entry(struct kaiocb *job)
631 struct aioliojob *lj;
639 AIO_LOCK_ASSERT(ki, MA_OWNED);
640 MPASS(job->jobstate == JOBST_JOBFINISHED);
642 atomic_subtract_int(&num_queue_count, 1);
645 MPASS(ki->kaio_count >= 0);
647 TAILQ_REMOVE(&ki->kaio_done, job, plist);
648 TAILQ_REMOVE(&ki->kaio_all, job, allist);
653 lj->lioj_finished_count--;
655 if (lj->lioj_count == 0) {
656 TAILQ_REMOVE(&ki->kaio_liojoblist, lj, lioj_list);
657 /* lio is going away, we need to destroy any knotes */
658 knlist_delete(&lj->klist, curthread, 1);
660 sigqueue_take(&lj->lioj_ksi);
662 uma_zfree(aiolio_zone, lj);
666 /* job is going away, we need to destroy any knotes */
667 knlist_delete(&job->klist, curthread, 1);
669 sigqueue_take(&job->ksi);
672 MPASS(job->bp == NULL);
673 job->jobstate = JOBST_NULL;
677 * The thread argument here is used to find the owning process
678 * and is also passed to fo_close() which may pass it to various
679 * places such as devsw close() routines. Because of that, we
680 * need a thread pointer from the process owning the job that is
681 * persistent and won't disappear out from under us or move to
684 * Currently, all the callers of this function call it to remove
685 * a kaiocb from the current process' job list either via a
686 * syscall or due to the current process calling exit() or
687 * execve(). Thus, we know that p == curproc. We also know that
688 * curthread can't exit since we are curthread.
690 * Therefore, we use curthread as the thread to pass to
691 * knlist_delete(). This does mean that it is possible for the
692 * thread pointer at close time to differ from the thread pointer
693 * at open time, but this is already true of file descriptors in
694 * a multithreaded process.
697 fdrop(job->fd_file, curthread);
699 uma_zfree(aiocb_zone, job);
706 aio_proc_rundown_exec(void *arg, struct proc *p,
707 struct image_params *imgp __unused)
709 aio_proc_rundown(arg, p);
713 * Rundown the jobs for a given process.
716 aio_proc_rundown(void *arg, struct proc *p)
719 struct aioliojob *lj;
720 struct kaiocb *job, *jobn;
725 KASSERT(curthread->td_proc == p,
726 ("%s: called on non-curproc", __func__));
732 ki->kaio_flags |= KAIO_RUNDOWN;
737 * Try to cancel all pending requests. This code simulates
738 * aio_cancel on all pending I/O requests.
740 TAILQ_FOREACH_SAFE(job, &ki->kaio_jobqueue, plist, jobn) {
742 mtx_lock(&aio_job_mtx);
743 if (job->jobstate == JOBST_JOBQGLOBAL) {
744 TAILQ_REMOVE(&aio_jobs, job, list);
746 } else if (job->jobstate == JOBST_JOBQSOCK) {
748 MPASS(fp->f_type == DTYPE_SOCKET);
750 TAILQ_REMOVE(&so->so_aiojobq, job, list);
752 } else if (job->jobstate == JOBST_JOBQSYNC) {
753 TAILQ_REMOVE(&ki->kaio_syncqueue, job, list);
756 mtx_unlock(&aio_job_mtx);
759 job->jobstate = JOBST_JOBFINISHED;
760 job->uaiocb._aiocb_private.status = -1;
761 job->uaiocb._aiocb_private.error = ECANCELED;
762 TAILQ_REMOVE(&ki->kaio_jobqueue, job, plist);
763 aio_bio_done_notify(p, job, DONE_QUEUE);
767 /* Wait for all running I/O to be finished */
768 if (TAILQ_FIRST(&ki->kaio_bufqueue) ||
769 TAILQ_FIRST(&ki->kaio_jobqueue)) {
770 ki->kaio_flags |= KAIO_WAKEUP;
771 msleep(&p->p_aioinfo, AIO_MTX(ki), PRIBIO, "aioprn", hz);
775 /* Free all completed I/O requests. */
776 while ((job = TAILQ_FIRST(&ki->kaio_done)) != NULL)
779 while ((lj = TAILQ_FIRST(&ki->kaio_liojoblist)) != NULL) {
780 if (lj->lioj_count == 0) {
781 TAILQ_REMOVE(&ki->kaio_liojoblist, lj, lioj_list);
782 knlist_delete(&lj->klist, curthread, 1);
784 sigqueue_take(&lj->lioj_ksi);
786 uma_zfree(aiolio_zone, lj);
788 panic("LIO job not cleaned up: C:%d, FC:%d\n",
789 lj->lioj_count, lj->lioj_finished_count);
793 taskqueue_drain(taskqueue_aiod_kick, &ki->kaio_task);
794 mtx_destroy(&ki->kaio_mtx);
795 uma_zfree(kaio_zone, ki);
800 * Select a job to run (called by an AIO daemon).
802 static struct kaiocb *
803 aio_selectjob(struct aioproc *aiop)
809 mtx_assert(&aio_job_mtx, MA_OWNED);
810 TAILQ_FOREACH(job, &aio_jobs, list) {
811 userp = job->userproc;
812 ki = userp->p_aioinfo;
814 if (ki->kaio_active_count < ki->kaio_maxactive_count) {
815 TAILQ_REMOVE(&aio_jobs, job, list);
816 /* Account for currently active jobs. */
817 ki->kaio_active_count++;
818 job->jobstate = JOBST_JOBRUNNING;
826 * Move all data to a permanent storage device. This code
827 * simulates the fsync syscall.
830 aio_fsync_vnode(struct thread *td, struct vnode *vp)
835 if ((error = vn_start_write(vp, &mp, V_WAIT | PCATCH)) != 0)
837 vn_lock(vp, LK_EXCLUSIVE | LK_RETRY);
838 if (vp->v_object != NULL) {
839 VM_OBJECT_WLOCK(vp->v_object);
840 vm_object_page_clean(vp->v_object, 0, 0, 0);
841 VM_OBJECT_WUNLOCK(vp->v_object);
843 error = VOP_FSYNC(vp, MNT_WAIT, td);
846 vn_finished_write(mp);
852 * The AIO processing activity for LIO_READ/LIO_WRITE. This is the code that
853 * does the I/O request for the non-physio version of the operations. The
854 * normal vn operations are used, and this code should work in all instances
855 * for every type of file, including pipes, sockets, fifos, and regular files.
857 * XXX I don't think it works well for socket, pipe, and fifo.
860 aio_process_rw(struct kaiocb *job)
862 struct ucred *td_savedcred;
871 int oublock_st, oublock_end;
872 int inblock_st, inblock_end;
874 KASSERT(job->uaiocb.aio_lio_opcode == LIO_READ ||
875 job->uaiocb.aio_lio_opcode == LIO_WRITE,
876 ("%s: opcode %d", __func__, job->uaiocb.aio_lio_opcode));
879 td_savedcred = td->td_ucred;
880 td->td_ucred = job->cred;
884 aiov.iov_base = (void *)(uintptr_t)cb->aio_buf;
885 aiov.iov_len = cb->aio_nbytes;
887 auio.uio_iov = &aiov;
889 auio.uio_offset = cb->aio_offset;
890 auio.uio_resid = cb->aio_nbytes;
891 cnt = cb->aio_nbytes;
892 auio.uio_segflg = UIO_USERSPACE;
895 inblock_st = td->td_ru.ru_inblock;
896 oublock_st = td->td_ru.ru_oublock;
898 * aio_aqueue() acquires a reference to the file that is
899 * released in aio_free_entry().
901 if (cb->aio_lio_opcode == LIO_READ) {
902 auio.uio_rw = UIO_READ;
903 if (auio.uio_resid == 0)
906 error = fo_read(fp, &auio, fp->f_cred, FOF_OFFSET, td);
908 if (fp->f_type == DTYPE_VNODE)
910 auio.uio_rw = UIO_WRITE;
911 error = fo_write(fp, &auio, fp->f_cred, FOF_OFFSET, td);
913 inblock_end = td->td_ru.ru_inblock;
914 oublock_end = td->td_ru.ru_oublock;
916 job->inputcharge = inblock_end - inblock_st;
917 job->outputcharge = oublock_end - oublock_st;
919 if ((error) && (auio.uio_resid != cnt)) {
920 if (error == ERESTART || error == EINTR || error == EWOULDBLOCK)
922 if ((error == EPIPE) && (cb->aio_lio_opcode == LIO_WRITE)) {
924 if (fp->f_type == DTYPE_SOCKET) {
926 if (so->so_options & SO_NOSIGPIPE)
930 PROC_LOCK(job->userproc);
931 kern_psignal(job->userproc, SIGPIPE);
932 PROC_UNLOCK(job->userproc);
937 cnt -= auio.uio_resid;
938 cb->_aiocb_private.error = error;
939 cb->_aiocb_private.status = cnt;
940 td->td_ucred = td_savedcred;
944 aio_process_sync(struct kaiocb *job)
946 struct thread *td = curthread;
947 struct ucred *td_savedcred = td->td_ucred;
948 struct aiocb *cb = &job->uaiocb;
949 struct file *fp = job->fd_file;
952 KASSERT(job->uaiocb.aio_lio_opcode == LIO_SYNC,
953 ("%s: opcode %d", __func__, job->uaiocb.aio_lio_opcode));
955 td->td_ucred = job->cred;
956 if (fp->f_vnode != NULL)
957 error = aio_fsync_vnode(td, fp->f_vnode);
958 cb->_aiocb_private.error = error;
959 cb->_aiocb_private.status = 0;
960 td->td_ucred = td_savedcred;
964 aio_process_mlock(struct kaiocb *job)
966 struct aiocb *cb = &job->uaiocb;
969 KASSERT(job->uaiocb.aio_lio_opcode == LIO_MLOCK,
970 ("%s: opcode %d", __func__, job->uaiocb.aio_lio_opcode));
972 error = vm_mlock(job->userproc, job->cred,
973 __DEVOLATILE(void *, cb->aio_buf), cb->aio_nbytes);
974 cb->_aiocb_private.error = error;
975 cb->_aiocb_private.status = 0;
979 aio_bio_done_notify(struct proc *userp, struct kaiocb *job, int type)
981 struct aioliojob *lj;
983 struct kaiocb *sjob, *sjobn;
986 ki = userp->p_aioinfo;
987 AIO_LOCK_ASSERT(ki, MA_OWNED);
991 lj->lioj_finished_count++;
992 if (lj->lioj_count == lj->lioj_finished_count)
995 if (type == DONE_QUEUE) {
996 job->jobflags |= KAIOCB_DONE;
998 job->jobflags |= KAIOCB_BUFDONE;
1000 TAILQ_INSERT_TAIL(&ki->kaio_done, job, plist);
1001 job->jobstate = JOBST_JOBFINISHED;
1003 if (ki->kaio_flags & KAIO_RUNDOWN)
1004 goto notification_done;
1006 if (job->uaiocb.aio_sigevent.sigev_notify == SIGEV_SIGNAL ||
1007 job->uaiocb.aio_sigevent.sigev_notify == SIGEV_THREAD_ID)
1008 aio_sendsig(userp, &job->uaiocb.aio_sigevent, &job->ksi);
1010 KNOTE_LOCKED(&job->klist, 1);
1013 if (lj->lioj_signal.sigev_notify == SIGEV_KEVENT) {
1014 lj->lioj_flags |= LIOJ_KEVENT_POSTED;
1015 KNOTE_LOCKED(&lj->klist, 1);
1017 if ((lj->lioj_flags & (LIOJ_SIGNAL|LIOJ_SIGNAL_POSTED))
1019 && (lj->lioj_signal.sigev_notify == SIGEV_SIGNAL ||
1020 lj->lioj_signal.sigev_notify == SIGEV_THREAD_ID)) {
1021 aio_sendsig(userp, &lj->lioj_signal, &lj->lioj_ksi);
1022 lj->lioj_flags |= LIOJ_SIGNAL_POSTED;
1027 if (job->jobflags & KAIOCB_CHECKSYNC) {
1028 TAILQ_FOREACH_SAFE(sjob, &ki->kaio_syncqueue, list, sjobn) {
1029 if (job->fd_file == sjob->fd_file &&
1030 job->seqno < sjob->seqno) {
1031 if (--sjob->pending == 0) {
1032 mtx_lock(&aio_job_mtx);
1033 sjob->jobstate = JOBST_JOBQGLOBAL;
1034 TAILQ_REMOVE(&ki->kaio_syncqueue, sjob,
1036 TAILQ_INSERT_TAIL(&aio_jobs, sjob, list);
1037 aio_kick_nowait(userp);
1038 mtx_unlock(&aio_job_mtx);
1043 if (ki->kaio_flags & KAIO_WAKEUP) {
1044 ki->kaio_flags &= ~KAIO_WAKEUP;
1045 wakeup(&userp->p_aioinfo);
1050 aio_switch_vmspace(struct kaiocb *job)
1053 vmspace_switch_aio(job->userproc->p_vmspace);
1057 * The AIO daemon, most of the actual work is done in aio_process_*,
1058 * but the setup (and address space mgmt) is done in this routine.
1061 aio_daemon(void *_id)
1064 struct aioproc *aiop;
1065 struct kaioinfo *ki;
1066 struct proc *p, *userp;
1067 struct vmspace *myvm;
1068 struct thread *td = curthread;
1069 int id = (intptr_t)_id;
1072 * Grab an extra reference on the daemon's vmspace so that it
1073 * doesn't get freed by jobs that switch to a different
1077 myvm = vmspace_acquire_ref(p);
1079 KASSERT(p->p_textvp == NULL, ("kthread has a textvp"));
1082 * Allocate and ready the aio control info. There is one aiop structure
1085 aiop = uma_zalloc(aiop_zone, M_WAITOK);
1087 aiop->aioprocflags = 0;
1090 * Wakeup parent process. (Parent sleeps to keep from blasting away
1091 * and creating too many daemons.)
1093 sema_post(&aio_newproc_sem);
1095 mtx_lock(&aio_job_mtx);
1098 * Take daemon off of free queue
1100 if (aiop->aioprocflags & AIOP_FREE) {
1101 TAILQ_REMOVE(&aio_freeproc, aiop, list);
1102 aiop->aioprocflags &= ~AIOP_FREE;
1108 while ((job = aio_selectjob(aiop)) != NULL) {
1109 mtx_unlock(&aio_job_mtx);
1110 userp = job->userproc;
1113 * Connect to process address space for user program.
1115 aio_switch_vmspace(job);
1117 ki = userp->p_aioinfo;
1119 /* Do the I/O function. */
1120 switch(job->uaiocb.aio_lio_opcode) {
1123 aio_process_rw(job);
1126 aio_process_sync(job);
1129 aio_process_mlock(job);
1133 mtx_lock(&aio_job_mtx);
1134 /* Decrement the active job count. */
1135 ki->kaio_active_count--;
1136 mtx_unlock(&aio_job_mtx);
1139 TAILQ_REMOVE(&ki->kaio_jobqueue, job, plist);
1140 aio_bio_done_notify(userp, job, DONE_QUEUE);
1143 mtx_lock(&aio_job_mtx);
1147 * Disconnect from user address space.
1149 if (p->p_vmspace != myvm) {
1150 mtx_unlock(&aio_job_mtx);
1151 vmspace_switch_aio(myvm);
1152 mtx_lock(&aio_job_mtx);
1154 * We have to restart to avoid race, we only sleep if
1155 * no job can be selected.
1160 mtx_assert(&aio_job_mtx, MA_OWNED);
1162 TAILQ_INSERT_HEAD(&aio_freeproc, aiop, list);
1163 aiop->aioprocflags |= AIOP_FREE;
1166 * If daemon is inactive for a long time, allow it to exit,
1167 * thereby freeing resources.
1169 if (msleep(p, &aio_job_mtx, PRIBIO, "aiordy",
1170 aiod_lifetime) == EWOULDBLOCK && TAILQ_EMPTY(&aio_jobs) &&
1171 (aiop->aioprocflags & AIOP_FREE) &&
1172 num_aio_procs > target_aio_procs)
1175 TAILQ_REMOVE(&aio_freeproc, aiop, list);
1177 mtx_unlock(&aio_job_mtx);
1178 uma_zfree(aiop_zone, aiop);
1179 free_unr(aiod_unr, id);
1182 KASSERT(p->p_vmspace == myvm,
1183 ("AIOD: bad vmspace for exiting daemon"));
1184 KASSERT(myvm->vm_refcnt > 1,
1185 ("AIOD: bad vm refcnt for exiting daemon: %d", myvm->vm_refcnt));
1190 * Create a new AIO daemon. This is mostly a kernel-thread fork routine. The
1191 * AIO daemon modifies its environment itself.
1194 aio_newproc(int *start)
1200 id = alloc_unr(aiod_unr);
1201 error = kproc_create(aio_daemon, (void *)(intptr_t)id, &p,
1202 RFNOWAIT, 0, "aiod%d", id);
1205 * Wait until daemon is started.
1207 sema_wait(&aio_newproc_sem);
1208 mtx_lock(&aio_job_mtx);
1212 mtx_unlock(&aio_job_mtx);
1214 free_unr(aiod_unr, id);
1220 * Try the high-performance, low-overhead physio method for eligible
1221 * VCHR devices. This method doesn't use an aio helper thread, and
1222 * thus has very low overhead.
1224 * Assumes that the caller, aio_aqueue(), has incremented the file
1225 * structure's reference count, preventing its deallocation for the
1226 * duration of this call.
1229 aio_qphysio(struct proc *p, struct kaiocb *job)
1238 struct kaioinfo *ki;
1239 struct aioliojob *lj;
1240 int error, ref, unmap, poff;
1246 if (fp == NULL || fp->f_type != DTYPE_VNODE)
1250 if (vp->v_type != VCHR)
1252 if (vp->v_bufobj.bo_bsize == 0)
1254 if (cb->aio_nbytes % vp->v_bufobj.bo_bsize)
1258 csw = devvn_refthread(vp, &dev, &ref);
1262 if ((csw->d_flags & D_DISK) == 0) {
1266 if (cb->aio_nbytes > dev->si_iosize_max) {
1272 poff = (vm_offset_t)cb->aio_buf & PAGE_MASK;
1273 unmap = ((dev->si_flags & SI_UNMAPPED) && unmapped_buf_allowed);
1275 if (cb->aio_nbytes > MAXPHYS) {
1280 if (cb->aio_nbytes > MAXPHYS - poff) {
1284 if (ki->kaio_buffer_count >= ki->kaio_ballowed_count) {
1289 job->bp = bp = g_alloc_bio();
1291 job->pbuf = pbuf = (struct buf *)getpbuf(NULL);
1298 ki->kaio_buffer_count++;
1302 TAILQ_INSERT_TAIL(&ki->kaio_bufqueue, job, plist);
1303 TAILQ_INSERT_TAIL(&ki->kaio_all, job, allist);
1304 job->jobstate = JOBST_JOBQBUF;
1305 cb->_aiocb_private.status = cb->aio_nbytes;
1308 bp->bio_length = cb->aio_nbytes;
1309 bp->bio_bcount = cb->aio_nbytes;
1310 bp->bio_done = aio_physwakeup;
1311 bp->bio_data = (void *)(uintptr_t)cb->aio_buf;
1312 bp->bio_offset = cb->aio_offset;
1313 bp->bio_cmd = cb->aio_lio_opcode == LIO_WRITE ? BIO_WRITE : BIO_READ;
1315 bp->bio_caller1 = (void *)job;
1317 prot = VM_PROT_READ;
1318 if (cb->aio_lio_opcode == LIO_READ)
1319 prot |= VM_PROT_WRITE; /* Less backwards than it looks */
1320 if ((job->npages = vm_fault_quick_hold_pages(
1321 &curproc->p_vmspace->vm_map,
1322 (vm_offset_t)bp->bio_data, bp->bio_length, prot, job->pages,
1323 sizeof(job->pages)/sizeof(job->pages[0]))) < 0) {
1328 pmap_qenter((vm_offset_t)pbuf->b_data,
1329 job->pages, job->npages);
1330 bp->bio_data = pbuf->b_data + poff;
1332 bp->bio_ma = job->pages;
1333 bp->bio_ma_n = job->npages;
1334 bp->bio_ma_offset = poff;
1335 bp->bio_data = unmapped_buf;
1336 bp->bio_flags |= BIO_UNMAPPED;
1339 atomic_add_int(&num_queue_count, 1);
1341 atomic_add_int(&num_buf_aio, 1);
1343 /* Perform transfer. */
1344 csw->d_strategy(bp);
1345 dev_relthread(dev, ref);
1350 job->jobstate = JOBST_NULL;
1351 TAILQ_REMOVE(&ki->kaio_bufqueue, job, plist);
1352 TAILQ_REMOVE(&ki->kaio_all, job, allist);
1355 ki->kaio_buffer_count--;
1360 relpbuf(pbuf, NULL);
1366 dev_relthread(dev, ref);
1371 * Wake up aio requests that may be serviceable now.
1374 aio_swake_cb(struct socket *so, struct sockbuf *sb)
1376 struct kaiocb *job, *jobn;
1379 SOCKBUF_LOCK_ASSERT(sb);
1380 if (sb == &so->so_snd)
1385 sb->sb_flags &= ~SB_AIO;
1386 mtx_lock(&aio_job_mtx);
1387 TAILQ_FOREACH_SAFE(job, &so->so_aiojobq, list, jobn) {
1388 if (opcode == job->uaiocb.aio_lio_opcode) {
1389 if (job->jobstate != JOBST_JOBQSOCK)
1390 panic("invalid queue value");
1392 * We don't have actual sockets backend yet,
1393 * so we simply move the requests to the generic
1396 TAILQ_REMOVE(&so->so_aiojobq, job, list);
1397 TAILQ_INSERT_TAIL(&aio_jobs, job, list);
1398 aio_kick_nowait(job->userproc);
1401 mtx_unlock(&aio_job_mtx);
1405 convert_old_sigevent(struct osigevent *osig, struct sigevent *nsig)
1409 * Only SIGEV_NONE, SIGEV_SIGNAL, and SIGEV_KEVENT are
1410 * supported by AIO with the old sigevent structure.
1412 nsig->sigev_notify = osig->sigev_notify;
1413 switch (nsig->sigev_notify) {
1417 nsig->sigev_signo = osig->__sigev_u.__sigev_signo;
1420 nsig->sigev_notify_kqueue =
1421 osig->__sigev_u.__sigev_notify_kqueue;
1422 nsig->sigev_value.sival_ptr = osig->sigev_value.sival_ptr;
1431 aiocb_copyin_old_sigevent(struct aiocb *ujob, struct aiocb *kjob)
1433 struct oaiocb *ojob;
1436 bzero(kjob, sizeof(struct aiocb));
1437 error = copyin(ujob, kjob, sizeof(struct oaiocb));
1440 ojob = (struct oaiocb *)kjob;
1441 return (convert_old_sigevent(&ojob->aio_sigevent, &kjob->aio_sigevent));
1445 aiocb_copyin(struct aiocb *ujob, struct aiocb *kjob)
1448 return (copyin(ujob, kjob, sizeof(struct aiocb)));
1452 aiocb_fetch_status(struct aiocb *ujob)
1455 return (fuword(&ujob->_aiocb_private.status));
1459 aiocb_fetch_error(struct aiocb *ujob)
1462 return (fuword(&ujob->_aiocb_private.error));
1466 aiocb_store_status(struct aiocb *ujob, long status)
1469 return (suword(&ujob->_aiocb_private.status, status));
1473 aiocb_store_error(struct aiocb *ujob, long error)
1476 return (suword(&ujob->_aiocb_private.error, error));
1480 aiocb_store_kernelinfo(struct aiocb *ujob, long jobref)
1483 return (suword(&ujob->_aiocb_private.kernelinfo, jobref));
1487 aiocb_store_aiocb(struct aiocb **ujobp, struct aiocb *ujob)
1490 return (suword(ujobp, (long)ujob));
1493 static struct aiocb_ops aiocb_ops = {
1494 .copyin = aiocb_copyin,
1495 .fetch_status = aiocb_fetch_status,
1496 .fetch_error = aiocb_fetch_error,
1497 .store_status = aiocb_store_status,
1498 .store_error = aiocb_store_error,
1499 .store_kernelinfo = aiocb_store_kernelinfo,
1500 .store_aiocb = aiocb_store_aiocb,
1503 static struct aiocb_ops aiocb_ops_osigevent = {
1504 .copyin = aiocb_copyin_old_sigevent,
1505 .fetch_status = aiocb_fetch_status,
1506 .fetch_error = aiocb_fetch_error,
1507 .store_status = aiocb_store_status,
1508 .store_error = aiocb_store_error,
1509 .store_kernelinfo = aiocb_store_kernelinfo,
1510 .store_aiocb = aiocb_store_aiocb,
1514 * Queue a new AIO request. Choosing either the threaded or direct physio VCHR
1515 * technique is done in this code.
1518 aio_aqueue(struct thread *td, struct aiocb *ujob, struct aioliojob *lj,
1519 int type, struct aiocb_ops *ops)
1521 struct proc *p = td->td_proc;
1522 cap_rights_t rights;
1525 struct kaiocb *job, *job2;
1526 struct kaioinfo *ki;
1535 if (p->p_aioinfo == NULL)
1536 aio_init_aioinfo(p);
1540 ops->store_status(ujob, -1);
1541 ops->store_error(ujob, 0);
1542 ops->store_kernelinfo(ujob, -1);
1544 if (num_queue_count >= max_queue_count ||
1545 ki->kaio_count >= ki->kaio_qallowed_count) {
1546 ops->store_error(ujob, EAGAIN);
1550 job = uma_zalloc(aiocb_zone, M_WAITOK | M_ZERO);
1551 knlist_init_mtx(&job->klist, AIO_MTX(ki));
1553 error = ops->copyin(ujob, &job->uaiocb);
1555 ops->store_error(ujob, error);
1556 uma_zfree(aiocb_zone, job);
1560 /* XXX: aio_nbytes is later casted to signed types. */
1561 if (job->uaiocb.aio_nbytes > INT_MAX) {
1562 uma_zfree(aiocb_zone, job);
1566 if (job->uaiocb.aio_sigevent.sigev_notify != SIGEV_KEVENT &&
1567 job->uaiocb.aio_sigevent.sigev_notify != SIGEV_SIGNAL &&
1568 job->uaiocb.aio_sigevent.sigev_notify != SIGEV_THREAD_ID &&
1569 job->uaiocb.aio_sigevent.sigev_notify != SIGEV_NONE) {
1570 ops->store_error(ujob, EINVAL);
1571 uma_zfree(aiocb_zone, job);
1575 if ((job->uaiocb.aio_sigevent.sigev_notify == SIGEV_SIGNAL ||
1576 job->uaiocb.aio_sigevent.sigev_notify == SIGEV_THREAD_ID) &&
1577 !_SIG_VALID(job->uaiocb.aio_sigevent.sigev_signo)) {
1578 uma_zfree(aiocb_zone, job);
1582 ksiginfo_init(&job->ksi);
1584 /* Save userspace address of the job info. */
1587 /* Get the opcode. */
1588 if (type != LIO_NOP)
1589 job->uaiocb.aio_lio_opcode = type;
1590 opcode = job->uaiocb.aio_lio_opcode;
1593 * Validate the opcode and fetch the file object for the specified
1596 * XXXRW: Moved the opcode validation up here so that we don't
1597 * retrieve a file descriptor without knowing what the capabiltity
1600 fd = job->uaiocb.aio_fildes;
1603 error = fget_write(td, fd,
1604 cap_rights_init(&rights, CAP_PWRITE), &fp);
1607 error = fget_read(td, fd,
1608 cap_rights_init(&rights, CAP_PREAD), &fp);
1611 error = fget(td, fd, cap_rights_init(&rights, CAP_FSYNC), &fp);
1617 error = fget(td, fd, cap_rights_init(&rights), &fp);
1623 uma_zfree(aiocb_zone, job);
1624 ops->store_error(ujob, error);
1628 if (opcode == LIO_SYNC && fp->f_vnode == NULL) {
1633 if (opcode != LIO_SYNC && job->uaiocb.aio_offset == -1LL) {
1640 mtx_lock(&aio_job_mtx);
1642 job->seqno = jobseqno++;
1643 mtx_unlock(&aio_job_mtx);
1644 error = ops->store_kernelinfo(ujob, jid);
1649 job->uaiocb._aiocb_private.kernelinfo = (void *)(intptr_t)jid;
1651 if (opcode == LIO_NOP) {
1653 uma_zfree(aiocb_zone, job);
1657 if (job->uaiocb.aio_sigevent.sigev_notify != SIGEV_KEVENT)
1659 evflags = job->uaiocb.aio_sigevent.sigev_notify_kevent_flags;
1660 if ((evflags & ~(EV_CLEAR | EV_DISPATCH | EV_ONESHOT)) != 0) {
1664 kqfd = job->uaiocb.aio_sigevent.sigev_notify_kqueue;
1665 kev.ident = (uintptr_t)job->ujob;
1666 kev.filter = EVFILT_AIO;
1667 kev.flags = EV_ADD | EV_ENABLE | EV_FLAG1 | evflags;
1668 kev.data = (intptr_t)job;
1669 kev.udata = job->uaiocb.aio_sigevent.sigev_value.sival_ptr;
1670 error = kqfd_register(kqfd, &kev, td, 1);
1675 uma_zfree(aiocb_zone, job);
1676 ops->store_error(ujob, error);
1681 ops->store_error(ujob, EINPROGRESS);
1682 job->uaiocb._aiocb_private.error = EINPROGRESS;
1684 job->cred = crhold(td->td_ucred);
1688 if (opcode == LIO_SYNC)
1691 if (fp && fp->f_type == DTYPE_SOCKET) {
1693 * Alternate queueing for socket ops: Reach down into the
1694 * descriptor to get the socket data. Then check to see if the
1695 * socket is ready to be read or written (based on the requested
1698 * If it is not ready for io, then queue the job on the
1699 * socket, and set the flags so we get a call when sbnotify()
1702 * Note if opcode is neither LIO_WRITE nor LIO_READ we lock
1703 * and unlock the snd sockbuf for no reason.
1706 sb = (opcode == LIO_READ) ? &so->so_rcv : &so->so_snd;
1708 if (((opcode == LIO_READ) && (!soreadable(so))) || ((opcode ==
1709 LIO_WRITE) && (!sowriteable(so)))) {
1710 sb->sb_flags |= SB_AIO;
1712 mtx_lock(&aio_job_mtx);
1713 TAILQ_INSERT_TAIL(&so->so_aiojobq, job, list);
1714 mtx_unlock(&aio_job_mtx);
1717 TAILQ_INSERT_TAIL(&ki->kaio_all, job, allist);
1718 TAILQ_INSERT_TAIL(&ki->kaio_jobqueue, job, plist);
1719 job->jobstate = JOBST_JOBQSOCK;
1725 atomic_add_int(&num_queue_count, 1);
1732 if ((error = aio_qphysio(p, job)) == 0)
1736 job->uaiocb._aiocb_private.error = error;
1737 ops->store_error(ujob, error);
1742 atomic_add_int(&num_queue_count, 1);
1748 TAILQ_INSERT_TAIL(&ki->kaio_jobqueue, job, plist);
1749 TAILQ_INSERT_TAIL(&ki->kaio_all, job, allist);
1750 if (opcode == LIO_SYNC) {
1751 TAILQ_FOREACH(job2, &ki->kaio_jobqueue, plist) {
1752 if (job2->fd_file == job->fd_file &&
1753 job2->uaiocb.aio_lio_opcode != LIO_SYNC &&
1754 job2->seqno < job->seqno) {
1755 job2->jobflags |= KAIOCB_CHECKSYNC;
1759 TAILQ_FOREACH(job2, &ki->kaio_bufqueue, plist) {
1760 if (job2->fd_file == job->fd_file &&
1761 job2->uaiocb.aio_lio_opcode != LIO_SYNC &&
1762 job2->seqno < job->seqno) {
1763 job2->jobflags |= KAIOCB_CHECKSYNC;
1767 if (job->pending != 0) {
1768 TAILQ_INSERT_TAIL(&ki->kaio_syncqueue, job, list);
1769 job->jobstate = JOBST_JOBQSYNC;
1774 mtx_lock(&aio_job_mtx);
1775 TAILQ_INSERT_TAIL(&aio_jobs, job, list);
1776 job->jobstate = JOBST_JOBQGLOBAL;
1778 mtx_unlock(&aio_job_mtx);
1786 aio_kick_nowait(struct proc *userp)
1788 struct kaioinfo *ki = userp->p_aioinfo;
1789 struct aioproc *aiop;
1791 mtx_assert(&aio_job_mtx, MA_OWNED);
1792 if ((aiop = TAILQ_FIRST(&aio_freeproc)) != NULL) {
1793 TAILQ_REMOVE(&aio_freeproc, aiop, list);
1794 aiop->aioprocflags &= ~AIOP_FREE;
1795 wakeup(aiop->aioproc);
1796 } else if (num_aio_resv_start + num_aio_procs < max_aio_procs &&
1797 ki->kaio_active_count + num_aio_resv_start <
1798 ki->kaio_maxactive_count) {
1799 taskqueue_enqueue(taskqueue_aiod_kick, &ki->kaio_task);
1804 aio_kick(struct proc *userp)
1806 struct kaioinfo *ki = userp->p_aioinfo;
1807 struct aioproc *aiop;
1810 mtx_assert(&aio_job_mtx, MA_OWNED);
1812 if ((aiop = TAILQ_FIRST(&aio_freeproc)) != NULL) {
1813 TAILQ_REMOVE(&aio_freeproc, aiop, list);
1814 aiop->aioprocflags &= ~AIOP_FREE;
1815 wakeup(aiop->aioproc);
1816 } else if (num_aio_resv_start + num_aio_procs < max_aio_procs &&
1817 ki->kaio_active_count + num_aio_resv_start <
1818 ki->kaio_maxactive_count) {
1819 num_aio_resv_start++;
1820 mtx_unlock(&aio_job_mtx);
1821 error = aio_newproc(&num_aio_resv_start);
1822 mtx_lock(&aio_job_mtx);
1824 num_aio_resv_start--;
1834 aio_kick_helper(void *context, int pending)
1836 struct proc *userp = context;
1838 mtx_lock(&aio_job_mtx);
1839 while (--pending >= 0) {
1840 if (aio_kick(userp))
1843 mtx_unlock(&aio_job_mtx);
1847 * Support the aio_return system call, as a side-effect, kernel resources are
1851 kern_aio_return(struct thread *td, struct aiocb *ujob, struct aiocb_ops *ops)
1853 struct proc *p = td->td_proc;
1855 struct kaioinfo *ki;
1862 TAILQ_FOREACH(job, &ki->kaio_done, plist) {
1863 if (job->ujob == ujob)
1867 MPASS(job->jobstate == JOBST_JOBFINISHED);
1868 status = job->uaiocb._aiocb_private.status;
1869 error = job->uaiocb._aiocb_private.error;
1870 td->td_retval[0] = status;
1871 if (job->uaiocb.aio_lio_opcode == LIO_WRITE) {
1872 td->td_ru.ru_oublock += job->outputcharge;
1873 job->outputcharge = 0;
1874 } else if (job->uaiocb.aio_lio_opcode == LIO_READ) {
1875 td->td_ru.ru_inblock += job->inputcharge;
1876 job->inputcharge = 0;
1878 aio_free_entry(job);
1880 ops->store_error(ujob, error);
1881 ops->store_status(ujob, status);
1890 sys_aio_return(struct thread *td, struct aio_return_args *uap)
1893 return (kern_aio_return(td, uap->aiocbp, &aiocb_ops));
1897 * Allow a process to wakeup when any of the I/O requests are completed.
1900 kern_aio_suspend(struct thread *td, int njoblist, struct aiocb **ujoblist,
1901 struct timespec *ts)
1903 struct proc *p = td->td_proc;
1905 struct kaioinfo *ki;
1906 struct kaiocb *firstjob, *job;
1911 if (ts->tv_nsec < 0 || ts->tv_nsec >= 1000000000)
1914 TIMESPEC_TO_TIMEVAL(&atv, ts);
1915 if (itimerfix(&atv))
1917 timo = tvtohz(&atv);
1931 TAILQ_FOREACH(job, &ki->kaio_all, allist) {
1932 for (i = 0; i < njoblist; i++) {
1933 if (job->ujob == ujoblist[i]) {
1934 if (firstjob == NULL)
1936 if (job->jobstate == JOBST_JOBFINISHED)
1941 /* All tasks were finished. */
1942 if (firstjob == NULL)
1945 ki->kaio_flags |= KAIO_WAKEUP;
1946 error = msleep(&p->p_aioinfo, AIO_MTX(ki), PRIBIO | PCATCH,
1948 if (error == ERESTART)
1959 sys_aio_suspend(struct thread *td, struct aio_suspend_args *uap)
1961 struct timespec ts, *tsp;
1962 struct aiocb **ujoblist;
1965 if (uap->nent < 0 || uap->nent > AIO_LISTIO_MAX)
1969 /* Get timespec struct. */
1970 if ((error = copyin(uap->timeout, &ts, sizeof(ts))) != 0)
1976 ujoblist = uma_zalloc(aiol_zone, M_WAITOK);
1977 error = copyin(uap->aiocbp, ujoblist, uap->nent * sizeof(ujoblist[0]));
1979 error = kern_aio_suspend(td, uap->nent, ujoblist, tsp);
1980 uma_zfree(aiol_zone, ujoblist);
1985 * aio_cancel cancels any non-physio aio operations not currently in
1989 sys_aio_cancel(struct thread *td, struct aio_cancel_args *uap)
1991 struct proc *p = td->td_proc;
1992 struct kaioinfo *ki;
1993 struct kaiocb *job, *jobn;
1996 cap_rights_t rights;
2000 int notcancelled = 0;
2003 /* Lookup file object. */
2004 error = fget(td, uap->fd, cap_rights_init(&rights), &fp);
2012 if (fp->f_type == DTYPE_VNODE) {
2014 if (vn_isdisk(vp, &error)) {
2016 td->td_retval[0] = AIO_NOTCANCELED;
2022 TAILQ_FOREACH_SAFE(job, &ki->kaio_jobqueue, plist, jobn) {
2023 if ((uap->fd == job->uaiocb.aio_fildes) &&
2024 ((uap->aiocbp == NULL) ||
2025 (uap->aiocbp == job->ujob))) {
2028 mtx_lock(&aio_job_mtx);
2029 if (job->jobstate == JOBST_JOBQGLOBAL) {
2030 TAILQ_REMOVE(&aio_jobs, job, list);
2032 } else if (job->jobstate == JOBST_JOBQSOCK) {
2033 MPASS(fp->f_type == DTYPE_SOCKET);
2035 TAILQ_REMOVE(&so->so_aiojobq, job, list);
2037 } else if (job->jobstate == JOBST_JOBQSYNC) {
2038 TAILQ_REMOVE(&ki->kaio_syncqueue, job, list);
2041 mtx_unlock(&aio_job_mtx);
2044 TAILQ_REMOVE(&ki->kaio_jobqueue, job, plist);
2045 job->uaiocb._aiocb_private.status = -1;
2046 job->uaiocb._aiocb_private.error = ECANCELED;
2047 aio_bio_done_notify(p, job, DONE_QUEUE);
2052 if (uap->aiocbp != NULL)
2061 if (uap->aiocbp != NULL) {
2063 td->td_retval[0] = AIO_CANCELED;
2069 td->td_retval[0] = AIO_NOTCANCELED;
2074 td->td_retval[0] = AIO_CANCELED;
2078 td->td_retval[0] = AIO_ALLDONE;
2084 * aio_error is implemented in the kernel level for compatibility purposes
2085 * only. For a user mode async implementation, it would be best to do it in
2086 * a userland subroutine.
2089 kern_aio_error(struct thread *td, struct aiocb *ujob, struct aiocb_ops *ops)
2091 struct proc *p = td->td_proc;
2093 struct kaioinfo *ki;
2098 td->td_retval[0] = EINVAL;
2103 TAILQ_FOREACH(job, &ki->kaio_all, allist) {
2104 if (job->ujob == ujob) {
2105 if (job->jobstate == JOBST_JOBFINISHED)
2107 job->uaiocb._aiocb_private.error;
2109 td->td_retval[0] = EINPROGRESS;
2117 * Hack for failure of aio_aqueue.
2119 status = ops->fetch_status(ujob);
2121 td->td_retval[0] = ops->fetch_error(ujob);
2125 td->td_retval[0] = EINVAL;
2130 sys_aio_error(struct thread *td, struct aio_error_args *uap)
2133 return (kern_aio_error(td, uap->aiocbp, &aiocb_ops));
2136 /* syscall - asynchronous read from a file (REALTIME) */
2138 sys_oaio_read(struct thread *td, struct oaio_read_args *uap)
2141 return (aio_aqueue(td, (struct aiocb *)uap->aiocbp, NULL, LIO_READ,
2142 &aiocb_ops_osigevent));
2146 sys_aio_read(struct thread *td, struct aio_read_args *uap)
2149 return (aio_aqueue(td, uap->aiocbp, NULL, LIO_READ, &aiocb_ops));
2152 /* syscall - asynchronous write to a file (REALTIME) */
2154 sys_oaio_write(struct thread *td, struct oaio_write_args *uap)
2157 return (aio_aqueue(td, (struct aiocb *)uap->aiocbp, NULL, LIO_WRITE,
2158 &aiocb_ops_osigevent));
2162 sys_aio_write(struct thread *td, struct aio_write_args *uap)
2165 return (aio_aqueue(td, uap->aiocbp, NULL, LIO_WRITE, &aiocb_ops));
2169 sys_aio_mlock(struct thread *td, struct aio_mlock_args *uap)
2172 return (aio_aqueue(td, uap->aiocbp, NULL, LIO_MLOCK, &aiocb_ops));
2176 kern_lio_listio(struct thread *td, int mode, struct aiocb * const *uacb_list,
2177 struct aiocb **acb_list, int nent, struct sigevent *sig,
2178 struct aiocb_ops *ops)
2180 struct proc *p = td->td_proc;
2182 struct kaioinfo *ki;
2183 struct aioliojob *lj;
2189 if ((mode != LIO_NOWAIT) && (mode != LIO_WAIT))
2192 if (nent < 0 || nent > AIO_LISTIO_MAX)
2195 if (p->p_aioinfo == NULL)
2196 aio_init_aioinfo(p);
2200 lj = uma_zalloc(aiolio_zone, M_WAITOK);
2203 lj->lioj_finished_count = 0;
2204 knlist_init_mtx(&lj->klist, AIO_MTX(ki));
2205 ksiginfo_init(&lj->lioj_ksi);
2210 if (sig && (mode == LIO_NOWAIT)) {
2211 bcopy(sig, &lj->lioj_signal, sizeof(lj->lioj_signal));
2212 if (lj->lioj_signal.sigev_notify == SIGEV_KEVENT) {
2213 /* Assume only new style KEVENT */
2214 kev.filter = EVFILT_LIO;
2215 kev.flags = EV_ADD | EV_ENABLE | EV_FLAG1;
2216 kev.ident = (uintptr_t)uacb_list; /* something unique */
2217 kev.data = (intptr_t)lj;
2218 /* pass user defined sigval data */
2219 kev.udata = lj->lioj_signal.sigev_value.sival_ptr;
2220 error = kqfd_register(
2221 lj->lioj_signal.sigev_notify_kqueue, &kev, td, 1);
2223 uma_zfree(aiolio_zone, lj);
2226 } else if (lj->lioj_signal.sigev_notify == SIGEV_NONE) {
2228 } else if (lj->lioj_signal.sigev_notify == SIGEV_SIGNAL ||
2229 lj->lioj_signal.sigev_notify == SIGEV_THREAD_ID) {
2230 if (!_SIG_VALID(lj->lioj_signal.sigev_signo)) {
2231 uma_zfree(aiolio_zone, lj);
2234 lj->lioj_flags |= LIOJ_SIGNAL;
2236 uma_zfree(aiolio_zone, lj);
2242 TAILQ_INSERT_TAIL(&ki->kaio_liojoblist, lj, lioj_list);
2244 * Add extra aiocb count to avoid the lio to be freed
2245 * by other threads doing aio_waitcomplete or aio_return,
2246 * and prevent event from being sent until we have queued
2253 * Get pointers to the list of I/O requests.
2256 for (i = 0; i < nent; i++) {
2259 error = aio_aqueue(td, job, lj, LIO_NOP, ops);
2267 if (mode == LIO_WAIT) {
2268 while (lj->lioj_count - 1 != lj->lioj_finished_count) {
2269 ki->kaio_flags |= KAIO_WAKEUP;
2270 error = msleep(&p->p_aioinfo, AIO_MTX(ki),
2271 PRIBIO | PCATCH, "aiospn", 0);
2272 if (error == ERESTART)
2278 if (lj->lioj_count - 1 == lj->lioj_finished_count) {
2279 if (lj->lioj_signal.sigev_notify == SIGEV_KEVENT) {
2280 lj->lioj_flags |= LIOJ_KEVENT_POSTED;
2281 KNOTE_LOCKED(&lj->klist, 1);
2283 if ((lj->lioj_flags & (LIOJ_SIGNAL|LIOJ_SIGNAL_POSTED))
2285 && (lj->lioj_signal.sigev_notify == SIGEV_SIGNAL ||
2286 lj->lioj_signal.sigev_notify == SIGEV_THREAD_ID)) {
2287 aio_sendsig(p, &lj->lioj_signal,
2289 lj->lioj_flags |= LIOJ_SIGNAL_POSTED;
2294 if (lj->lioj_count == 0) {
2295 TAILQ_REMOVE(&ki->kaio_liojoblist, lj, lioj_list);
2296 knlist_delete(&lj->klist, curthread, 1);
2298 sigqueue_take(&lj->lioj_ksi);
2301 uma_zfree(aiolio_zone, lj);
2310 /* syscall - list directed I/O (REALTIME) */
2312 sys_olio_listio(struct thread *td, struct olio_listio_args *uap)
2314 struct aiocb **acb_list;
2315 struct sigevent *sigp, sig;
2316 struct osigevent osig;
2319 if ((uap->mode != LIO_NOWAIT) && (uap->mode != LIO_WAIT))
2323 if (nent < 0 || nent > AIO_LISTIO_MAX)
2326 if (uap->sig && (uap->mode == LIO_NOWAIT)) {
2327 error = copyin(uap->sig, &osig, sizeof(osig));
2330 error = convert_old_sigevent(&osig, &sig);
2337 acb_list = malloc(sizeof(struct aiocb *) * nent, M_LIO, M_WAITOK);
2338 error = copyin(uap->acb_list, acb_list, nent * sizeof(acb_list[0]));
2340 error = kern_lio_listio(td, uap->mode,
2341 (struct aiocb * const *)uap->acb_list, acb_list, nent, sigp,
2342 &aiocb_ops_osigevent);
2343 free(acb_list, M_LIO);
2347 /* syscall - list directed I/O (REALTIME) */
2349 sys_lio_listio(struct thread *td, struct lio_listio_args *uap)
2351 struct aiocb **acb_list;
2352 struct sigevent *sigp, sig;
2355 if ((uap->mode != LIO_NOWAIT) && (uap->mode != LIO_WAIT))
2359 if (nent < 0 || nent > AIO_LISTIO_MAX)
2362 if (uap->sig && (uap->mode == LIO_NOWAIT)) {
2363 error = copyin(uap->sig, &sig, sizeof(sig));
2370 acb_list = malloc(sizeof(struct aiocb *) * nent, M_LIO, M_WAITOK);
2371 error = copyin(uap->acb_list, acb_list, nent * sizeof(acb_list[0]));
2373 error = kern_lio_listio(td, uap->mode, uap->acb_list, acb_list,
2374 nent, sigp, &aiocb_ops);
2375 free(acb_list, M_LIO);
2380 aio_physwakeup(struct bio *bp)
2382 struct kaiocb *job = (struct kaiocb *)bp->bio_caller1;
2384 struct kaioinfo *ki;
2387 /* Release mapping into kernel space. */
2389 pmap_qremove((vm_offset_t)job->pbuf->b_data, job->npages);
2390 relpbuf(job->pbuf, NULL);
2392 atomic_subtract_int(&num_buf_aio, 1);
2394 vm_page_unhold_pages(job->pages, job->npages);
2398 userp = job->userproc;
2399 ki = userp->p_aioinfo;
2401 job->uaiocb._aiocb_private.status -= bp->bio_resid;
2402 job->uaiocb._aiocb_private.error = 0;
2403 if (bp->bio_flags & BIO_ERROR)
2404 job->uaiocb._aiocb_private.error = bp->bio_error;
2405 nblks = btodb(job->uaiocb.aio_nbytes);
2406 if (job->uaiocb.aio_lio_opcode == LIO_WRITE)
2407 job->outputcharge += nblks;
2409 job->inputcharge += nblks;
2410 TAILQ_REMOVE(&userp->p_aioinfo->kaio_bufqueue, job, plist);
2411 ki->kaio_buffer_count--;
2412 aio_bio_done_notify(userp, job, DONE_BUF);
2418 /* syscall - wait for the next completion of an aio request */
2420 kern_aio_waitcomplete(struct thread *td, struct aiocb **ujobp,
2421 struct timespec *ts, struct aiocb_ops *ops)
2423 struct proc *p = td->td_proc;
2425 struct kaioinfo *ki;
2428 int error, status, timo;
2430 ops->store_aiocb(ujobp, NULL);
2434 } else if (ts->tv_sec == 0 && ts->tv_nsec == 0) {
2437 if ((ts->tv_nsec < 0) || (ts->tv_nsec >= 1000000000))
2440 TIMESPEC_TO_TIMEVAL(&atv, ts);
2441 if (itimerfix(&atv))
2443 timo = tvtohz(&atv);
2446 if (p->p_aioinfo == NULL)
2447 aio_init_aioinfo(p);
2453 while ((job = TAILQ_FIRST(&ki->kaio_done)) == NULL) {
2455 error = EWOULDBLOCK;
2458 ki->kaio_flags |= KAIO_WAKEUP;
2459 error = msleep(&p->p_aioinfo, AIO_MTX(ki), PRIBIO | PCATCH,
2461 if (timo && error == ERESTART)
2468 MPASS(job->jobstate == JOBST_JOBFINISHED);
2470 status = job->uaiocb._aiocb_private.status;
2471 error = job->uaiocb._aiocb_private.error;
2472 td->td_retval[0] = status;
2473 if (job->uaiocb.aio_lio_opcode == LIO_WRITE) {
2474 td->td_ru.ru_oublock += job->outputcharge;
2475 job->outputcharge = 0;
2476 } else if (job->uaiocb.aio_lio_opcode == LIO_READ) {
2477 td->td_ru.ru_inblock += job->inputcharge;
2478 job->inputcharge = 0;
2480 aio_free_entry(job);
2482 ops->store_aiocb(ujobp, ujob);
2483 ops->store_error(ujob, error);
2484 ops->store_status(ujob, status);
2492 sys_aio_waitcomplete(struct thread *td, struct aio_waitcomplete_args *uap)
2494 struct timespec ts, *tsp;
2498 /* Get timespec struct. */
2499 error = copyin(uap->timeout, &ts, sizeof(ts));
2506 return (kern_aio_waitcomplete(td, uap->aiocbp, tsp, &aiocb_ops));
2510 kern_aio_fsync(struct thread *td, int op, struct aiocb *ujob,
2511 struct aiocb_ops *ops)
2513 struct proc *p = td->td_proc;
2514 struct kaioinfo *ki;
2516 if (op != O_SYNC) /* XXX lack of O_DSYNC */
2520 aio_init_aioinfo(p);
2521 return (aio_aqueue(td, ujob, NULL, LIO_SYNC, ops));
2525 sys_aio_fsync(struct thread *td, struct aio_fsync_args *uap)
2528 return (kern_aio_fsync(td, uap->op, uap->aiocbp, &aiocb_ops));
2531 /* kqueue attach function */
2533 filt_aioattach(struct knote *kn)
2535 struct kaiocb *job = (struct kaiocb *)kn->kn_sdata;
2538 * The job 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_aio = job;
2545 kn->kn_flags &= ~EV_FLAG1;
2547 knlist_add(&job->klist, kn, 0);
2552 /* kqueue detach function */
2554 filt_aiodetach(struct knote *kn)
2558 knl = &kn->kn_ptr.p_aio->klist;
2559 knl->kl_lock(knl->kl_lockarg);
2560 if (!knlist_empty(knl))
2561 knlist_remove(knl, kn, 1);
2562 knl->kl_unlock(knl->kl_lockarg);
2565 /* kqueue filter function */
2568 filt_aio(struct knote *kn, long hint)
2570 struct kaiocb *job = kn->kn_ptr.p_aio;
2572 kn->kn_data = job->uaiocb._aiocb_private.error;
2573 if (job->jobstate != JOBST_JOBFINISHED)
2575 kn->kn_flags |= EV_EOF;
2579 /* kqueue attach function */
2581 filt_lioattach(struct knote *kn)
2583 struct aioliojob * lj = (struct aioliojob *)kn->kn_sdata;
2586 * The aioliojob pointer must be validated before using it, so
2587 * registration is restricted to the kernel; the user cannot
2590 if ((kn->kn_flags & EV_FLAG1) == 0)
2592 kn->kn_ptr.p_lio = lj;
2593 kn->kn_flags &= ~EV_FLAG1;
2595 knlist_add(&lj->klist, kn, 0);
2600 /* kqueue detach function */
2602 filt_liodetach(struct knote *kn)
2606 knl = &kn->kn_ptr.p_lio->klist;
2607 knl->kl_lock(knl->kl_lockarg);
2608 if (!knlist_empty(knl))
2609 knlist_remove(knl, kn, 1);
2610 knl->kl_unlock(knl->kl_lockarg);
2613 /* kqueue filter function */
2616 filt_lio(struct knote *kn, long hint)
2618 struct aioliojob * lj = kn->kn_ptr.p_lio;
2620 return (lj->lioj_flags & LIOJ_KEVENT_POSTED);
2623 #ifdef COMPAT_FREEBSD32
2625 struct __aiocb_private32 {
2628 uint32_t kernelinfo;
2631 typedef struct oaiocb32 {
2632 int aio_fildes; /* File descriptor */
2633 uint64_t aio_offset __packed; /* File offset for I/O */
2634 uint32_t aio_buf; /* I/O buffer in process space */
2635 uint32_t aio_nbytes; /* Number of bytes for I/O */
2636 struct osigevent32 aio_sigevent; /* Signal to deliver */
2637 int aio_lio_opcode; /* LIO opcode */
2638 int aio_reqprio; /* Request priority -- ignored */
2639 struct __aiocb_private32 _aiocb_private;
2642 typedef struct aiocb32 {
2643 int32_t aio_fildes; /* File descriptor */
2644 uint64_t aio_offset __packed; /* File offset for I/O */
2645 uint32_t aio_buf; /* I/O buffer in process space */
2646 uint32_t aio_nbytes; /* Number of bytes for I/O */
2648 uint32_t __spare2__;
2649 int aio_lio_opcode; /* LIO opcode */
2650 int aio_reqprio; /* Request priority -- ignored */
2651 struct __aiocb_private32 _aiocb_private;
2652 struct sigevent32 aio_sigevent; /* Signal to deliver */
2656 convert_old_sigevent32(struct osigevent32 *osig, struct sigevent *nsig)
2660 * Only SIGEV_NONE, SIGEV_SIGNAL, and SIGEV_KEVENT are
2661 * supported by AIO with the old sigevent structure.
2663 CP(*osig, *nsig, sigev_notify);
2664 switch (nsig->sigev_notify) {
2668 nsig->sigev_signo = osig->__sigev_u.__sigev_signo;
2671 nsig->sigev_notify_kqueue =
2672 osig->__sigev_u.__sigev_notify_kqueue;
2673 PTRIN_CP(*osig, *nsig, sigev_value.sival_ptr);
2682 aiocb32_copyin_old_sigevent(struct aiocb *ujob, struct aiocb *kjob)
2684 struct oaiocb32 job32;
2687 bzero(kjob, sizeof(struct aiocb));
2688 error = copyin(ujob, &job32, sizeof(job32));
2692 CP(job32, *kjob, aio_fildes);
2693 CP(job32, *kjob, aio_offset);
2694 PTRIN_CP(job32, *kjob, aio_buf);
2695 CP(job32, *kjob, aio_nbytes);
2696 CP(job32, *kjob, aio_lio_opcode);
2697 CP(job32, *kjob, aio_reqprio);
2698 CP(job32, *kjob, _aiocb_private.status);
2699 CP(job32, *kjob, _aiocb_private.error);
2700 PTRIN_CP(job32, *kjob, _aiocb_private.kernelinfo);
2701 return (convert_old_sigevent32(&job32.aio_sigevent,
2702 &kjob->aio_sigevent));
2706 aiocb32_copyin(struct aiocb *ujob, struct aiocb *kjob)
2708 struct aiocb32 job32;
2711 error = copyin(ujob, &job32, sizeof(job32));
2714 CP(job32, *kjob, aio_fildes);
2715 CP(job32, *kjob, aio_offset);
2716 PTRIN_CP(job32, *kjob, aio_buf);
2717 CP(job32, *kjob, aio_nbytes);
2718 CP(job32, *kjob, aio_lio_opcode);
2719 CP(job32, *kjob, aio_reqprio);
2720 CP(job32, *kjob, _aiocb_private.status);
2721 CP(job32, *kjob, _aiocb_private.error);
2722 PTRIN_CP(job32, *kjob, _aiocb_private.kernelinfo);
2723 return (convert_sigevent32(&job32.aio_sigevent, &kjob->aio_sigevent));
2727 aiocb32_fetch_status(struct aiocb *ujob)
2729 struct aiocb32 *ujob32;
2731 ujob32 = (struct aiocb32 *)ujob;
2732 return (fuword32(&ujob32->_aiocb_private.status));
2736 aiocb32_fetch_error(struct aiocb *ujob)
2738 struct aiocb32 *ujob32;
2740 ujob32 = (struct aiocb32 *)ujob;
2741 return (fuword32(&ujob32->_aiocb_private.error));
2745 aiocb32_store_status(struct aiocb *ujob, long status)
2747 struct aiocb32 *ujob32;
2749 ujob32 = (struct aiocb32 *)ujob;
2750 return (suword32(&ujob32->_aiocb_private.status, status));
2754 aiocb32_store_error(struct aiocb *ujob, long error)
2756 struct aiocb32 *ujob32;
2758 ujob32 = (struct aiocb32 *)ujob;
2759 return (suword32(&ujob32->_aiocb_private.error, error));
2763 aiocb32_store_kernelinfo(struct aiocb *ujob, long jobref)
2765 struct aiocb32 *ujob32;
2767 ujob32 = (struct aiocb32 *)ujob;
2768 return (suword32(&ujob32->_aiocb_private.kernelinfo, jobref));
2772 aiocb32_store_aiocb(struct aiocb **ujobp, struct aiocb *ujob)
2775 return (suword32(ujobp, (long)ujob));
2778 static struct aiocb_ops aiocb32_ops = {
2779 .copyin = aiocb32_copyin,
2780 .fetch_status = aiocb32_fetch_status,
2781 .fetch_error = aiocb32_fetch_error,
2782 .store_status = aiocb32_store_status,
2783 .store_error = aiocb32_store_error,
2784 .store_kernelinfo = aiocb32_store_kernelinfo,
2785 .store_aiocb = aiocb32_store_aiocb,
2788 static struct aiocb_ops aiocb32_ops_osigevent = {
2789 .copyin = aiocb32_copyin_old_sigevent,
2790 .fetch_status = aiocb32_fetch_status,
2791 .fetch_error = aiocb32_fetch_error,
2792 .store_status = aiocb32_store_status,
2793 .store_error = aiocb32_store_error,
2794 .store_kernelinfo = aiocb32_store_kernelinfo,
2795 .store_aiocb = aiocb32_store_aiocb,
2799 freebsd32_aio_return(struct thread *td, struct freebsd32_aio_return_args *uap)
2802 return (kern_aio_return(td, (struct aiocb *)uap->aiocbp, &aiocb32_ops));
2806 freebsd32_aio_suspend(struct thread *td, struct freebsd32_aio_suspend_args *uap)
2808 struct timespec32 ts32;
2809 struct timespec ts, *tsp;
2810 struct aiocb **ujoblist;
2811 uint32_t *ujoblist32;
2814 if (uap->nent < 0 || uap->nent > AIO_LISTIO_MAX)
2818 /* Get timespec struct. */
2819 if ((error = copyin(uap->timeout, &ts32, sizeof(ts32))) != 0)
2821 CP(ts32, ts, tv_sec);
2822 CP(ts32, ts, tv_nsec);
2827 ujoblist = uma_zalloc(aiol_zone, M_WAITOK);
2828 ujoblist32 = (uint32_t *)ujoblist;
2829 error = copyin(uap->aiocbp, ujoblist32, uap->nent *
2830 sizeof(ujoblist32[0]));
2832 for (i = uap->nent; i > 0; i--)
2833 ujoblist[i] = PTRIN(ujoblist32[i]);
2835 error = kern_aio_suspend(td, uap->nent, ujoblist, tsp);
2837 uma_zfree(aiol_zone, ujoblist);
2842 freebsd32_aio_cancel(struct thread *td, struct freebsd32_aio_cancel_args *uap)
2845 return (sys_aio_cancel(td, (struct aio_cancel_args *)uap));
2849 freebsd32_aio_error(struct thread *td, struct freebsd32_aio_error_args *uap)
2852 return (kern_aio_error(td, (struct aiocb *)uap->aiocbp, &aiocb32_ops));
2856 freebsd32_oaio_read(struct thread *td, struct freebsd32_oaio_read_args *uap)
2859 return (aio_aqueue(td, (struct aiocb *)uap->aiocbp, NULL, LIO_READ,
2860 &aiocb32_ops_osigevent));
2864 freebsd32_aio_read(struct thread *td, struct freebsd32_aio_read_args *uap)
2867 return (aio_aqueue(td, (struct aiocb *)uap->aiocbp, NULL, LIO_READ,
2872 freebsd32_oaio_write(struct thread *td, struct freebsd32_oaio_write_args *uap)
2875 return (aio_aqueue(td, (struct aiocb *)uap->aiocbp, NULL, LIO_WRITE,
2876 &aiocb32_ops_osigevent));
2880 freebsd32_aio_write(struct thread *td, struct freebsd32_aio_write_args *uap)
2883 return (aio_aqueue(td, (struct aiocb *)uap->aiocbp, NULL, LIO_WRITE,
2888 freebsd32_aio_mlock(struct thread *td, struct freebsd32_aio_mlock_args *uap)
2891 return (aio_aqueue(td, (struct aiocb *)uap->aiocbp, NULL, LIO_MLOCK,
2896 freebsd32_aio_waitcomplete(struct thread *td,
2897 struct freebsd32_aio_waitcomplete_args *uap)
2899 struct timespec32 ts32;
2900 struct timespec ts, *tsp;
2904 /* Get timespec struct. */
2905 error = copyin(uap->timeout, &ts32, sizeof(ts32));
2908 CP(ts32, ts, tv_sec);
2909 CP(ts32, ts, tv_nsec);
2914 return (kern_aio_waitcomplete(td, (struct aiocb **)uap->aiocbp, tsp,
2919 freebsd32_aio_fsync(struct thread *td, struct freebsd32_aio_fsync_args *uap)
2922 return (kern_aio_fsync(td, uap->op, (struct aiocb *)uap->aiocbp,
2927 freebsd32_olio_listio(struct thread *td, struct freebsd32_olio_listio_args *uap)
2929 struct aiocb **acb_list;
2930 struct sigevent *sigp, sig;
2931 struct osigevent32 osig;
2932 uint32_t *acb_list32;
2935 if ((uap->mode != LIO_NOWAIT) && (uap->mode != LIO_WAIT))
2939 if (nent < 0 || nent > AIO_LISTIO_MAX)
2942 if (uap->sig && (uap->mode == LIO_NOWAIT)) {
2943 error = copyin(uap->sig, &osig, sizeof(osig));
2946 error = convert_old_sigevent32(&osig, &sig);
2953 acb_list32 = malloc(sizeof(uint32_t) * nent, M_LIO, M_WAITOK);
2954 error = copyin(uap->acb_list, acb_list32, nent * sizeof(uint32_t));
2956 free(acb_list32, M_LIO);
2959 acb_list = malloc(sizeof(struct aiocb *) * nent, M_LIO, M_WAITOK);
2960 for (i = 0; i < nent; i++)
2961 acb_list[i] = PTRIN(acb_list32[i]);
2962 free(acb_list32, M_LIO);
2964 error = kern_lio_listio(td, uap->mode,
2965 (struct aiocb * const *)uap->acb_list, acb_list, nent, sigp,
2966 &aiocb32_ops_osigevent);
2967 free(acb_list, M_LIO);
2972 freebsd32_lio_listio(struct thread *td, struct freebsd32_lio_listio_args *uap)
2974 struct aiocb **acb_list;
2975 struct sigevent *sigp, sig;
2976 struct sigevent32 sig32;
2977 uint32_t *acb_list32;
2980 if ((uap->mode != LIO_NOWAIT) && (uap->mode != LIO_WAIT))
2984 if (nent < 0 || nent > AIO_LISTIO_MAX)
2987 if (uap->sig && (uap->mode == LIO_NOWAIT)) {
2988 error = copyin(uap->sig, &sig32, sizeof(sig32));
2991 error = convert_sigevent32(&sig32, &sig);
2998 acb_list32 = malloc(sizeof(uint32_t) * nent, M_LIO, M_WAITOK);
2999 error = copyin(uap->acb_list, acb_list32, nent * sizeof(uint32_t));
3001 free(acb_list32, M_LIO);
3004 acb_list = malloc(sizeof(struct aiocb *) * nent, M_LIO, M_WAITOK);
3005 for (i = 0; i < nent; i++)
3006 acb_list[i] = PTRIN(acb_list32[i]);
3007 free(acb_list32, M_LIO);
3009 error = kern_lio_listio(td, uap->mode,
3010 (struct aiocb * const *)uap->acb_list, acb_list, nent, sigp,
3012 free(acb_list, M_LIO);