]> CyberLeo.Net >> Repos - FreeBSD/stable/10.git/blob - sys/kern/vfs_aio.c
MFC r368207,368607:
[FreeBSD/stable/10.git] / sys / kern / vfs_aio.c
1 /*-
2  * Copyright (c) 1997 John S. Dyson.  All rights reserved.
3  *
4  * Redistribution and use in source and binary forms, with or without
5  * modification, are permitted provided that the following conditions
6  * are met:
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.
11  *
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.
15  */
16
17 /*
18  * This file contains support for the POSIX 1003.1B AIO/LIO facility.
19  */
20
21 #include <sys/cdefs.h>
22 __FBSDID("$FreeBSD$");
23
24 #include "opt_compat.h"
25
26 #include <sys/param.h>
27 #include <sys/systm.h>
28 #include <sys/malloc.h>
29 #include <sys/bio.h>
30 #include <sys/buf.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>
39 #include <sys/file.h>
40 #include <sys/limits.h>
41 #include <sys/lock.h>
42 #include <sys/mutex.h>
43 #include <sys/unistd.h>
44 #include <sys/posix4.h>
45 #include <sys/proc.h>
46 #include <sys/resourcevar.h>
47 #include <sys/signalvar.h>
48 #include <sys/protosw.h>
49 #include <sys/rwlock.h>
50 #include <sys/sema.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>
56 #include <sys/sx.h>
57 #include <sys/taskqueue.h>
58 #include <sys/vnode.h>
59 #include <sys/conf.h>
60 #include <sys/event.h>
61 #include <sys/mount.h>
62 #include <geom/geom.h>
63
64 #include <machine/atomic.h>
65
66 #include <vm/vm.h>
67 #include <vm/vm_page.h>
68 #include <vm/vm_extern.h>
69 #include <vm/pmap.h>
70 #include <vm/vm_map.h>
71 #include <vm/vm_object.h>
72 #include <vm/uma.h>
73 #include <sys/aio.h>
74
75 #include "opt_vfs_aio.h"
76
77 /*
78  * Counter for allocating reference ids to new jobs.  Wrapped to 1 on
79  * overflow. (XXX will be removed soon.)
80  */
81 static u_long jobrefid;
82
83 /*
84  * Counter for aio_fsync.
85  */
86 static uint64_t jobseqno;
87
88 #define JOBST_NULL              0
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
95
96 #ifndef MAX_AIO_PER_PROC
97 #define MAX_AIO_PER_PROC        32
98 #endif
99
100 #ifndef MAX_AIO_QUEUE_PER_PROC
101 #define MAX_AIO_QUEUE_PER_PROC  256 /* Bigger than AIO_LISTIO_MAX */
102 #endif
103
104 #ifndef MAX_AIO_PROCS
105 #define MAX_AIO_PROCS           32
106 #endif
107
108 #ifndef MAX_AIO_QUEUE
109 #define MAX_AIO_QUEUE           1024 /* Bigger than AIO_LISTIO_MAX */
110 #endif
111
112 #ifndef TARGET_AIO_PROCS
113 #define TARGET_AIO_PROCS        4
114 #endif
115
116 #ifndef MAX_BUF_AIO
117 #define MAX_BUF_AIO             16
118 #endif
119
120 #ifndef AIOD_TIMEOUT_DEFAULT
121 #define AIOD_TIMEOUT_DEFAULT    (10 * hz)
122 #endif
123
124 #ifndef AIOD_LIFETIME_DEFAULT
125 #define AIOD_LIFETIME_DEFAULT   (30 * hz)
126 #endif
127
128 FEATURE(aio, "Asynchronous I/O");
129
130 static MALLOC_DEFINE(M_LIO, "lio", "listio aio control block list");
131
132 static SYSCTL_NODE(_vfs, OID_AUTO, aio, CTLFLAG_RW, 0, "Async IO management");
133
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 ");
138
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");
143
144 /*
145  * The code will adjust the actual number of AIO processes towards this
146  * number when it gets a chance.
147  */
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");
151
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");
155
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");
159
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");
163
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;
167
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");
171
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");
175
176 static int unloadable = 0;
177 SYSCTL_INT(_vfs_aio, OID_AUTO, unloadable, CTLFLAG_RW, &unloadable, 0,
178     "Allow unload of aio (not recommended)");
179
180
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)");
184
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)");
189
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)");
193
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;
203 } oaiocb_t;
204
205 /*
206  * Below is a key of locks used to protect each member of struct aiocblist
207  * aioliojob and kaioinfo and any backends.
208  *
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
213  *     reused.
214  * c - locked by aio_job_mtx, the lock for the generic file I/O backend.
215  */
216
217 /*
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
226  * daemons.
227  */
228
229 struct aiocblist {
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 */
251 };
252
253 /* jobflags */
254 #define AIOCBLIST_DONE          0x01
255 #define AIOCBLIST_BUFDONE       0x02
256 #define AIOCBLIST_RUNDOWN       0x04
257 #define AIOCBLIST_CHECKSYNC     0x08
258
259 /*
260  * AIO process info
261  */
262 #define AIOP_FREE       0x1                     /* proc on free queue */
263
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 */
268 };
269
270 /*
271  * data-structure for lio signal management
272  */
273 struct aioliojob {
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 */
281 };
282
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 */
286
287 /*
288  * per process aio data structure
289  */
290 struct kaioinfo {
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,
305                                                  *  NOT USED YET.
306                                                  */
307         TAILQ_HEAD(,aiocblist) kaio_syncqueue;  /* (a) queue for aio_fsync */
308         struct  task    kaio_task;      /* (*) task to kick aio threads */
309 };
310
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)
315
316 #define KAIO_RUNDOWN    0x1     /* process is being run down */
317 #define KAIO_WAKEUP     0x2     /* wakeup process when there is a significant event */
318
319 /*
320  * Operations used to interact with userland aio control blocks.
321  * Different ABIs provide their own operations.
322  */
323 struct aiocb_ops {
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);
331 };
332
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;
339
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);
357 #define DONE_BUF        1
358 #define DONE_QUEUE      2
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);
368
369 /*
370  * Zones for:
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
376  */
377 static uma_zone_t kaio_zone, aiop_zone, aiocb_zone, aiol_zone, aiolio_zone;
378
379 /* kqueue filters for aio */
380 static struct filterops aio_filtops = {
381         .f_isfd = 0,
382         .f_attach = filt_aioattach,
383         .f_detach = filt_aiodetach,
384         .f_event = filt_aio,
385 };
386 static struct filterops lio_filtops = {
387         .f_isfd = 0,
388         .f_attach = filt_lioattach,
389         .f_detach = filt_liodetach,
390         .f_event = filt_lio
391 };
392
393 static eventhandler_tag exit_tag, exec_tag;
394
395 TASKQUEUE_DEFINE_THREAD(aiod_bio);
396
397 /*
398  * Main operations function for use as a kernel module.
399  */
400 static int
401 aio_modload(struct module *module, int cmd, void *arg)
402 {
403         int error = 0;
404
405         switch (cmd) {
406         case MOD_LOAD:
407                 aio_onceonly();
408                 break;
409         case MOD_UNLOAD:
410                 error = aio_unload();
411                 break;
412         case MOD_SHUTDOWN:
413                 break;
414         default:
415                 error = EINVAL;
416                 break;
417         }
418         return (error);
419 }
420
421 static moduledata_t aio_mod = {
422         "aio",
423         &aio_modload,
424         NULL
425 };
426
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),
441         SYSCALL_INIT_LAST
442 };
443
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>
452
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),
467         SYSCALL_INIT_LAST
468 };
469 #endif
470
471 DECLARE_MODULE(aio, aio_mod,
472         SI_SUB_VFS, SI_ORDER_ANY);
473 MODULE_VERSION(aio, 1);
474
475 /*
476  * Startup initialization
477  */
478 static int
479 aio_onceonly(void)
480 {
481         int error;
482
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;
509         jobrefid = 1;
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);
514
515         error = syscall_helper_register(aio_syscalls);
516         if (error)
517                 return (error);
518 #ifdef COMPAT_FREEBSD32
519         error = syscall32_helper_register(aio32_syscalls);
520         if (error)
521                 return (error);
522 #endif
523         return (0);
524 }
525
526 /*
527  * Callback for unload of AIO when used as a module.
528  */
529 static int
530 aio_unload(void)
531 {
532         int error;
533
534         /*
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.
538          *
539          * jhb: aio_proc_rundown() needs to run on curproc though,
540          * so I don't think that would fly.
541          */
542         if (!unloadable)
543                 return (EOPNOTSUPP);
544
545 #ifdef COMPAT_FREEBSD32
546         syscall32_helper_unregister(aio32_syscalls);
547 #endif
548         syscall_helper_unregister(aio_syscalls);
549
550         error = kqueue_del_filteropts(EVFILT_AIO);
551         if (error)
552                 return error;
553         error = kqueue_del_filteropts(EVFILT_LIO);
554         if (error)
555                 return error;
556         async_io_version = 0;
557         aio_swake = NULL;
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);
573         return (0);
574 }
575
576 /*
577  * Init the per-process aioinfo structure.  The aioinfo limits are set
578  * per-process for user limit (resource) management.
579  */
580 void
581 aio_init_aioinfo(struct proc *p)
582 {
583         struct kaioinfo *ki;
584
585         ki = uma_zalloc(kaio_zone, M_WAITOK);
586         mtx_init(&ki->kaio_mtx, "aiomtx", NULL, MTX_DEF);
587         ki->kaio_flags = 0;
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;
591         ki->kaio_count = 0;
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);
602         PROC_LOCK(p);
603         if (p->p_aioinfo == NULL) {
604                 p->p_aioinfo = ki;
605                 PROC_UNLOCK(p);
606         } else {
607                 PROC_UNLOCK(p);
608                 mtx_destroy(&ki->kaio_mtx);
609                 uma_zfree(kaio_zone, ki);
610         }
611
612         while (num_aio_procs < MIN(target_aio_procs, max_aio_procs))
613                 aio_newproc(NULL);
614 }
615
616 static int
617 aio_sendsig(struct proc *p, struct sigevent *sigev, ksiginfo_t *ksi)
618 {
619         struct thread *td;
620         int error;
621
622         error = sigev_findtd(p, sigev, &td);
623         if (error)
624                 return (error);
625         if (!KSI_ONQ(ksi)) {
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);
630         }
631         PROC_UNLOCK(p);
632         return (error);
633 }
634
635 /*
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.
639  */
640 static int
641 aio_free_entry(struct aiocblist *aiocbe)
642 {
643         struct kaioinfo *ki;
644         struct aioliojob *lj;
645         struct proc *p;
646
647         p = aiocbe->userproc;
648         MPASS(curproc == p);
649         ki = p->p_aioinfo;
650         MPASS(ki != NULL);
651
652         AIO_LOCK_ASSERT(ki, MA_OWNED);
653         MPASS(aiocbe->jobstate == JOBST_JOBFINISHED);
654
655         atomic_subtract_int(&num_queue_count, 1);
656
657         ki->kaio_count--;
658         MPASS(ki->kaio_count >= 0);
659
660         TAILQ_REMOVE(&ki->kaio_done, aiocbe, plist);
661         TAILQ_REMOVE(&ki->kaio_all, aiocbe, allist);
662
663         lj = aiocbe->lio;
664         if (lj) {
665                 lj->lioj_count--;
666                 lj->lioj_finished_count--;
667
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);
672                         PROC_LOCK(p);
673                         sigqueue_take(&lj->lioj_ksi);
674                         PROC_UNLOCK(p);
675                         uma_zfree(aiolio_zone, lj);
676                 }
677         }
678
679         /* aiocbe is going away, we need to destroy any knotes */
680         knlist_delete(&aiocbe->klist, curthread, 1);
681         PROC_LOCK(p);
682         sigqueue_take(&aiocbe->ksi);
683         PROC_UNLOCK(p);
684
685         MPASS(aiocbe->bp == NULL);
686         aiocbe->jobstate = JOBST_NULL;
687         AIO_UNLOCK(ki);
688
689         /*
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
695          * another process.
696          *
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.
702          *
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.
708          */
709         if (aiocbe->fd_file)
710                 fdrop(aiocbe->fd_file, curthread);
711         crfree(aiocbe->cred);
712         uma_zfree(aiocb_zone, aiocbe);
713         AIO_LOCK(ki);
714
715         return (0);
716 }
717
718 static void
719 aio_proc_rundown_exec(void *arg, struct proc *p, struct image_params *imgp __unused)
720 {
721         aio_proc_rundown(arg, p);
722 }
723
724 /*
725  * Rundown the jobs for a given process.
726  */
727 static void
728 aio_proc_rundown(void *arg, struct proc *p)
729 {
730         struct kaioinfo *ki;
731         struct aioliojob *lj;
732         struct aiocblist *cbe, *cbn;
733         struct file *fp;
734         struct socket *so;
735         int remove;
736
737         KASSERT(curthread->td_proc == p,
738             ("%s: called on non-curproc", __func__));
739         ki = p->p_aioinfo;
740         if (ki == NULL)
741                 return;
742
743         AIO_LOCK(ki);
744         ki->kaio_flags |= KAIO_RUNDOWN;
745
746 restart:
747
748         /*
749          * Try to cancel all pending requests. This code simulates
750          * aio_cancel on all pending I/O requests.
751          */
752         TAILQ_FOREACH_SAFE(cbe, &ki->kaio_jobqueue, plist, cbn) {
753                 remove = 0;
754                 mtx_lock(&aio_job_mtx);
755                 if (cbe->jobstate == JOBST_JOBQGLOBAL) {
756                         TAILQ_REMOVE(&aio_jobs, cbe, list);
757                         remove = 1;
758                 } else if (cbe->jobstate == JOBST_JOBQSOCK) {
759                         fp = cbe->fd_file;
760                         MPASS(fp->f_type == DTYPE_SOCKET);
761                         so = fp->f_data;
762                         TAILQ_REMOVE(&so->so_aiojobq, cbe, list);
763                         remove = 1;
764                 } else if (cbe->jobstate == JOBST_JOBQSYNC) {
765                         TAILQ_REMOVE(&ki->kaio_syncqueue, cbe, list);
766                         remove = 1;
767                 }
768                 mtx_unlock(&aio_job_mtx);
769
770                 if (remove) {
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);
776                 }
777         }
778
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);
784                 goto restart;
785         }
786
787         /* Free all completed I/O requests. */
788         while ((cbe = TAILQ_FIRST(&ki->kaio_done)) != NULL)
789                 aio_free_entry(cbe);
790
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);
795                         PROC_LOCK(p);
796                         sigqueue_take(&lj->lioj_ksi);
797                         PROC_UNLOCK(p);
798                         uma_zfree(aiolio_zone, lj);
799                 } else {
800                         panic("LIO job not cleaned up: C:%d, FC:%d\n",
801                             lj->lioj_count, lj->lioj_finished_count);
802                 }
803         }
804         AIO_UNLOCK(ki);
805         taskqueue_drain(taskqueue_aiod_bio, &ki->kaio_task);
806         mtx_destroy(&ki->kaio_mtx);
807         uma_zfree(kaio_zone, ki);
808         p->p_aioinfo = NULL;
809 }
810
811 /*
812  * Select a job to run (called by an AIO daemon).
813  */
814 static struct aiocblist *
815 aio_selectjob(struct aiothreadlist *aiop)
816 {
817         struct aiocblist *aiocbe;
818         struct kaioinfo *ki;
819         struct proc *userp;
820
821         mtx_assert(&aio_job_mtx, MA_OWNED);
822         TAILQ_FOREACH(aiocbe, &aio_jobs, list) {
823                 userp = aiocbe->userproc;
824                 ki = userp->p_aioinfo;
825
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;
831                         break;
832                 }
833         }
834         return (aiocbe);
835 }
836
837 /*
838  *  Move all data to a permanent storage device, this code
839  *  simulates fsync syscall.
840  */
841 static int
842 aio_fsync_vnode(struct thread *td, struct vnode *vp)
843 {
844         struct mount *mp;
845         int error;
846
847         if ((error = vn_start_write(vp, &mp, V_WAIT | PCATCH)) != 0)
848                 goto drop;
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);
854         }
855         error = VOP_FSYNC(vp, MNT_WAIT, td);
856
857         VOP_UNLOCK(vp, 0);
858         vn_finished_write(mp);
859 drop:
860         return (error);
861 }
862
863 /*
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.
868  *
869  * XXX I don't think it works well for socket, pipe, and fifo.
870  */
871 static void
872 aio_process_rw(struct aiocblist *aiocbe)
873 {
874         struct ucred *td_savedcred;
875         struct thread *td;
876         struct aiocb *cb;
877         struct file *fp;
878         struct socket *so;
879         struct uio auio;
880         struct iovec aiov;
881         int cnt;
882         int error;
883         int oublock_st, oublock_end;
884         int inblock_st, inblock_end;
885
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));
889
890         td = curthread;
891         td_savedcred = td->td_ucred;
892         td->td_ucred = aiocbe->cred;
893         cb = &aiocbe->uaiocb;
894         fp = aiocbe->fd_file;
895
896         aiov.iov_base = (void *)(uintptr_t)cb->aio_buf;
897         aiov.iov_len = cb->aio_nbytes;
898
899         auio.uio_iov = &aiov;
900         auio.uio_iovcnt = 1;
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;
905         auio.uio_td = td;
906
907         inblock_st = td->td_ru.ru_inblock;
908         oublock_st = td->td_ru.ru_oublock;
909         /*
910          * aio_aqueue() acquires a reference to the file that is
911          * released in aio_free_entry().
912          */
913         if (cb->aio_lio_opcode == LIO_READ) {
914                 auio.uio_rw = UIO_READ;
915                 if (auio.uio_resid == 0)
916                         error = 0;
917                 else
918                         error = fo_read(fp, &auio, fp->f_cred, FOF_OFFSET, td);
919         } else {
920                 if (fp->f_type == DTYPE_VNODE)
921                         bwillwrite();
922                 auio.uio_rw = UIO_WRITE;
923                 error = fo_write(fp, &auio, fp->f_cred, FOF_OFFSET, td);
924         }
925         inblock_end = td->td_ru.ru_inblock;
926         oublock_end = td->td_ru.ru_oublock;
927
928         aiocbe->inputcharge = inblock_end - inblock_st;
929         aiocbe->outputcharge = oublock_end - oublock_st;
930
931         if ((error) && (auio.uio_resid != cnt)) {
932                 if (error == ERESTART || error == EINTR || error == EWOULDBLOCK)
933                         error = 0;
934                 if ((error == EPIPE) && (cb->aio_lio_opcode == LIO_WRITE)) {
935                         int sigpipe = 1;
936                         if (fp->f_type == DTYPE_SOCKET) {
937                                 so = fp->f_data;
938                                 if (so->so_options & SO_NOSIGPIPE)
939                                         sigpipe = 0;
940                         }
941                         if (sigpipe) {
942                                 PROC_LOCK(aiocbe->userproc);
943                                 kern_psignal(aiocbe->userproc, SIGPIPE);
944                                 PROC_UNLOCK(aiocbe->userproc);
945                         }
946                 }
947         }
948
949         cnt -= auio.uio_resid;
950         cb->_aiocb_private.error = error;
951         cb->_aiocb_private.status = cnt;
952         td->td_ucred = td_savedcred;
953 }
954
955 static void
956 aio_process_sync(struct aiocblist *aiocbe)
957 {
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;
962         int error = 0;
963
964         KASSERT(aiocbe->uaiocb.aio_lio_opcode == LIO_SYNC,
965             ("%s: opcode %d", __func__, aiocbe->uaiocb.aio_lio_opcode));
966
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;
973 }
974
975 static void
976 aio_process_mlock(struct aiocblist *aiocbe)
977 {
978         struct aiocb *cb = &aiocbe->uaiocb;
979         int error;
980
981         KASSERT(aiocbe->uaiocb.aio_lio_opcode == LIO_MLOCK,
982             ("%s: opcode %d", __func__, aiocbe->uaiocb.aio_lio_opcode));
983
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;
988 }
989
990 static void
991 aio_bio_done_notify(struct proc *userp, struct aiocblist *aiocbe, int type)
992 {
993         struct aioliojob *lj;
994         struct kaioinfo *ki;
995         struct aiocblist *scb, *scbn;
996         int lj_done;
997
998         ki = userp->p_aioinfo;
999         AIO_LOCK_ASSERT(ki, MA_OWNED);
1000         lj = aiocbe->lio;
1001         lj_done = 0;
1002         if (lj) {
1003                 lj->lioj_finished_count++;
1004                 if (lj->lioj_count == lj->lioj_finished_count)
1005                         lj_done = 1;
1006         }
1007         if (type == DONE_QUEUE) {
1008                 aiocbe->jobflags |= AIOCBLIST_DONE;
1009         } else {
1010                 aiocbe->jobflags |= AIOCBLIST_BUFDONE;
1011         }
1012         TAILQ_INSERT_TAIL(&ki->kaio_done, aiocbe, plist);
1013         aiocbe->jobstate = JOBST_JOBFINISHED;
1014
1015         if (ki->kaio_flags & KAIO_RUNDOWN)
1016                 goto notification_done;
1017
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);
1021
1022         KNOTE_LOCKED(&aiocbe->klist, 1);
1023
1024         if (lj_done) {
1025                 if (lj->lioj_signal.sigev_notify == SIGEV_KEVENT) {
1026                         lj->lioj_flags |= LIOJ_KEVENT_POSTED;
1027                         KNOTE_LOCKED(&lj->klist, 1);
1028                 }
1029                 if ((lj->lioj_flags & (LIOJ_SIGNAL|LIOJ_SIGNAL_POSTED))
1030                     == LIOJ_SIGNAL
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;
1035                 }
1036         }
1037
1038 notification_done:
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);
1050                                 }
1051                         }
1052                 }
1053         }
1054         if (ki->kaio_flags & KAIO_WAKEUP) {
1055                 ki->kaio_flags &= ~KAIO_WAKEUP;
1056                 wakeup(&userp->p_aioinfo);
1057         }
1058 }
1059
1060 /*
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.
1063  */
1064 static void
1065 aio_daemon(void *_id)
1066 {
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;
1074
1075         /*
1076          * Local copies of curproc (cp) and vmspace (myvm)
1077          */
1078         mycp = td->td_proc;
1079         myvm = mycp->p_vmspace;
1080
1081         KASSERT(mycp->p_textvp == NULL, ("kthread has a textvp"));
1082
1083         /*
1084          * Allocate and ready the aio control info.  There is one aiop structure
1085          * per daemon.
1086          */
1087         aiop = uma_zalloc(aiop_zone, M_WAITOK);
1088         aiop->aiothread = td;
1089         aiop->aiothreadflags = 0;
1090
1091         /* The daemon resides in its own pgrp. */
1092         sys_setsid(td, NULL);
1093
1094         /*
1095          * Wakeup parent process.  (Parent sleeps to keep from blasting away
1096          * and creating too many daemons.)
1097          */
1098         sema_post(&aio_newproc_sem);
1099
1100         mtx_lock(&aio_job_mtx);
1101         for (;;) {
1102                 /*
1103                  * curcp is the current daemon process context.
1104                  * userp is the current user process context.
1105                  */
1106                 curcp = mycp;
1107
1108                 /*
1109                  * Take daemon off of free queue
1110                  */
1111                 if (aiop->aiothreadflags & AIOP_FREE) {
1112                         TAILQ_REMOVE(&aio_freeproc, aiop, list);
1113                         aiop->aiothreadflags &= ~AIOP_FREE;
1114                 }
1115
1116                 /*
1117                  * Check for jobs.
1118                  */
1119                 while ((aiocbe = aio_selectjob(aiop)) != NULL) {
1120                         mtx_unlock(&aio_job_mtx);
1121                         userp = aiocbe->userproc;
1122
1123                         /*
1124                          * Connect to process address space for user program.
1125                          */
1126                         if (userp != curcp) {
1127                                 /*
1128                                  * Save the current address space that we are
1129                                  * connected to.
1130                                  */
1131                                 tmpvm = mycp->p_vmspace;
1132
1133                                 /*
1134                                  * Point to the new user address space, and
1135                                  * refer to it.
1136                                  */
1137                                 mycp->p_vmspace = userp->p_vmspace;
1138                                 atomic_add_int(&mycp->p_vmspace->vm_refcnt, 1);
1139
1140                                 /* Activate the new mapping. */
1141                                 pmap_activate(FIRST_THREAD_IN_PROC(mycp));
1142
1143                                 /*
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.
1148                                  */
1149                                 if (tmpvm != myvm) {
1150                                         vmspace_free(tmpvm);
1151                                 }
1152                                 curcp = userp;
1153                         }
1154
1155                         ki = userp->p_aioinfo;
1156
1157                         /* Do the I/O function. */
1158                         switch(aiocbe->uaiocb.aio_lio_opcode) {
1159                         case LIO_READ:
1160                         case LIO_WRITE:
1161                                 aio_process_rw(aiocbe);
1162                                 break;
1163                         case LIO_SYNC:
1164                                 aio_process_sync(aiocbe);
1165                                 break;
1166                         case LIO_MLOCK:
1167                                 aio_process_mlock(aiocbe);
1168                                 break;
1169                         }
1170
1171                         mtx_lock(&aio_job_mtx);
1172                         /* Decrement the active job count. */
1173                         ki->kaio_active_count--;
1174                         mtx_unlock(&aio_job_mtx);
1175
1176                         AIO_LOCK(ki);
1177                         TAILQ_REMOVE(&ki->kaio_jobqueue, aiocbe, plist);
1178                         aio_bio_done_notify(userp, aiocbe, DONE_QUEUE);
1179                         AIO_UNLOCK(ki);
1180
1181                         mtx_lock(&aio_job_mtx);
1182                 }
1183
1184                 /*
1185                  * Disconnect from user address space.
1186                  */
1187                 if (curcp != mycp) {
1188
1189                         mtx_unlock(&aio_job_mtx);
1190
1191                         /* Get the user address space to disconnect from. */
1192                         tmpvm = mycp->p_vmspace;
1193
1194                         /* Get original address space for daemon. */
1195                         mycp->p_vmspace = myvm;
1196
1197                         /* Activate the daemon's address space. */
1198                         pmap_activate(FIRST_THREAD_IN_PROC(mycp));
1199 #ifdef DIAGNOSTIC
1200                         if (tmpvm == myvm) {
1201                                 printf("AIOD: vmspace problem -- %d\n",
1202                                     mycp->p_pid);
1203                         }
1204 #endif
1205                         /* Remove our vmspace reference. */
1206                         vmspace_free(tmpvm);
1207
1208                         curcp = mycp;
1209
1210                         mtx_lock(&aio_job_mtx);
1211                         /*
1212                          * We have to restart to avoid race, we only sleep if
1213                          * no job can be selected, that should be
1214                          * curcp == mycp.
1215                          */
1216                         continue;
1217                 }
1218
1219                 mtx_assert(&aio_job_mtx, MA_OWNED);
1220
1221                 TAILQ_INSERT_HEAD(&aio_freeproc, aiop, list);
1222                 aiop->aiothreadflags |= AIOP_FREE;
1223
1224                 /*
1225                  * If daemon is inactive for a long time, allow it to exit,
1226                  * thereby freeing resources.
1227                  */
1228                 if (msleep(aiop->aiothread, &aio_job_mtx, PRIBIO, "aiordy",
1229                     aiod_lifetime)) {
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);
1234                                         num_aio_procs--;
1235                                         mtx_unlock(&aio_job_mtx);
1236                                         uma_zfree(aiop_zone, aiop);
1237                                         free_unr(aiod_unr, id);
1238 #ifdef DIAGNOSTIC
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);
1243                                         }
1244 #endif
1245                                         kproc_exit(0);
1246                                 }
1247                         }
1248                 }
1249         }
1250         mtx_unlock(&aio_job_mtx);
1251         panic("shouldn't be here\n");
1252 }
1253
1254 /*
1255  * Create a new AIO daemon. This is mostly a kernel-thread fork routine. The
1256  * AIO daemon modifies its environment itself.
1257  */
1258 static int
1259 aio_newproc(int *start)
1260 {
1261         int error;
1262         struct proc *p;
1263         int id;
1264
1265         id = alloc_unr(aiod_unr);
1266         error = kproc_create(aio_daemon, (void *)(intptr_t)id, &p,
1267                 RFNOWAIT, 0, "aiod%d", id);
1268         if (error == 0) {
1269                 /*
1270                  * Wait until daemon is started.
1271                  */
1272                 sema_wait(&aio_newproc_sem);
1273                 mtx_lock(&aio_job_mtx);
1274                 num_aio_procs++;
1275                 if (start != NULL)
1276                         (*start)--;
1277                 mtx_unlock(&aio_job_mtx);
1278         } else {
1279                 free_unr(aiod_unr, id);
1280         }
1281         return (error);
1282 }
1283
1284 /*
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.
1288  *
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.
1292  */
1293 static int
1294 aio_qphysio(struct proc *p, struct aiocblist *aiocbe)
1295 {
1296         struct aiocb *cb;
1297         struct file *fp;
1298         struct bio *bp;
1299         struct buf *pbuf;
1300         struct vnode *vp;
1301         struct cdevsw *csw;
1302         struct cdev *dev;
1303         struct kaioinfo *ki;
1304         struct aioliojob *lj;
1305         int error, ref, unmap, poff;
1306         vm_prot_t prot;
1307
1308         cb = &aiocbe->uaiocb;
1309         fp = aiocbe->fd_file;
1310
1311         if (fp == NULL || fp->f_type != DTYPE_VNODE)
1312                 return (-1);
1313
1314         vp = fp->f_vnode;
1315         if (vp->v_type != VCHR)
1316                 return (-1);
1317         if (vp->v_bufobj.bo_bsize == 0)
1318                 return (-1);
1319         if (cb->aio_nbytes % vp->v_bufobj.bo_bsize)
1320                 return (-1);
1321
1322         ref = 0;
1323         csw = devvn_refthread(vp, &dev, &ref);
1324         if (csw == NULL)
1325                 return (ENXIO);
1326
1327         if ((csw->d_flags & D_DISK) == 0) {
1328                 error = -1;
1329                 goto unref;
1330         }
1331         if (cb->aio_nbytes > dev->si_iosize_max) {
1332                 error = -1;
1333                 goto unref;
1334         }
1335
1336         ki = p->p_aioinfo;
1337         poff = (vm_offset_t)cb->aio_buf & PAGE_MASK;
1338         unmap = ((dev->si_flags & SI_UNMAPPED) && unmapped_buf_allowed);
1339         if (unmap) {
1340                 if (cb->aio_nbytes > MAXPHYS) {
1341                         error = -1;
1342                         goto unref;
1343                 }
1344         } else {
1345                 if (cb->aio_nbytes > MAXPHYS - poff) {
1346                         error = -1;
1347                         goto unref;
1348                 }
1349                 if (ki->kaio_buffer_count >= ki->kaio_ballowed_count) {
1350                         error = -1;
1351                         goto unref;
1352                 }
1353         }
1354         aiocbe->bp = bp = g_alloc_bio();
1355         if (!unmap) {
1356                 aiocbe->pbuf = pbuf = (struct buf *)getpbuf(NULL);
1357                 BUF_KERNPROC(pbuf);
1358         } else
1359                 pbuf = NULL;
1360
1361         AIO_LOCK(ki);
1362         ki->kaio_count++;
1363         if (!unmap)
1364                 ki->kaio_buffer_count++;
1365         lj = aiocbe->lio;
1366         if (lj)
1367                 lj->lioj_count++;
1368         TAILQ_INSERT_TAIL(&ki->kaio_bufqueue, aiocbe, plist);
1369         TAILQ_INSERT_TAIL(&ki->kaio_all, aiocbe, allist);
1370         aiocbe->jobstate = JOBST_JOBQBUF;
1371         cb->_aiocb_private.status = cb->aio_nbytes;
1372         AIO_UNLOCK(ki);
1373
1374         bp->bio_length = cb->aio_nbytes;
1375         bp->bio_bcount = cb->aio_nbytes;
1376         bp->bio_done = aio_physwakeup;
1377         bp->bio_data = (void *)(uintptr_t)cb->aio_buf;
1378         bp->bio_offset = cb->aio_offset;
1379         bp->bio_cmd = cb->aio_lio_opcode == LIO_WRITE ? BIO_WRITE : BIO_READ;
1380         bp->bio_dev = dev;
1381         bp->bio_caller1 = (void *)aiocbe;
1382
1383         prot = VM_PROT_READ;
1384         if (cb->aio_lio_opcode == LIO_READ)
1385                 prot |= VM_PROT_WRITE;  /* Less backwards than it looks */
1386         if ((aiocbe->npages = vm_fault_quick_hold_pages(
1387             &curproc->p_vmspace->vm_map,
1388             (vm_offset_t)bp->bio_data, bp->bio_length, prot, aiocbe->pages,
1389             sizeof(aiocbe->pages)/sizeof(aiocbe->pages[0]))) < 0) {
1390                 error = EFAULT;
1391                 goto doerror;
1392         }
1393         if (!unmap) {
1394                 pmap_qenter((vm_offset_t)pbuf->b_data,
1395                     aiocbe->pages, aiocbe->npages);
1396                 bp->bio_data = pbuf->b_data + poff;
1397         } else {
1398                 bp->bio_ma = aiocbe->pages;
1399                 bp->bio_ma_n = aiocbe->npages;
1400                 bp->bio_ma_offset = poff;
1401                 bp->bio_data = unmapped_buf;
1402                 bp->bio_flags |= BIO_UNMAPPED;
1403         }
1404
1405         atomic_add_int(&num_queue_count, 1);
1406         if (!unmap)
1407                 atomic_add_int(&num_buf_aio, 1);
1408
1409         /* Perform transfer. */
1410         csw->d_strategy(bp);
1411         dev_relthread(dev, ref);
1412         return (0);
1413
1414 doerror:
1415         AIO_LOCK(ki);
1416         aiocbe->jobstate = JOBST_NULL;
1417         TAILQ_REMOVE(&ki->kaio_bufqueue, aiocbe, plist);
1418         TAILQ_REMOVE(&ki->kaio_all, aiocbe, allist);
1419         ki->kaio_count--;
1420         if (!unmap)
1421                 ki->kaio_buffer_count--;
1422         if (lj)
1423                 lj->lioj_count--;
1424         AIO_UNLOCK(ki);
1425         if (pbuf) {
1426                 relpbuf(pbuf, NULL);
1427                 aiocbe->pbuf = NULL;
1428         }
1429         g_destroy_bio(bp);
1430         aiocbe->bp = NULL;
1431 unref:
1432         dev_relthread(dev, ref);
1433         return (error);
1434 }
1435
1436 /*
1437  * Wake up aio requests that may be serviceable now.
1438  */
1439 static void
1440 aio_swake_cb(struct socket *so, struct sockbuf *sb)
1441 {
1442         struct aiocblist *cb, *cbn;
1443         int opcode;
1444
1445         SOCKBUF_LOCK_ASSERT(sb);
1446         if (sb == &so->so_snd)
1447                 opcode = LIO_WRITE;
1448         else
1449                 opcode = LIO_READ;
1450
1451         sb->sb_flags &= ~SB_AIO;
1452         mtx_lock(&aio_job_mtx);
1453         TAILQ_FOREACH_SAFE(cb, &so->so_aiojobq, list, cbn) {
1454                 if (opcode == cb->uaiocb.aio_lio_opcode) {
1455                         if (cb->jobstate != JOBST_JOBQSOCK)
1456                                 panic("invalid queue value");
1457                         /* XXX
1458                          * We don't have actual sockets backend yet,
1459                          * so we simply move the requests to the generic
1460                          * file I/O backend.
1461                          */
1462                         TAILQ_REMOVE(&so->so_aiojobq, cb, list);
1463                         TAILQ_INSERT_TAIL(&aio_jobs, cb, list);
1464                         aio_kick_nowait(cb->userproc);
1465                 }
1466         }
1467         mtx_unlock(&aio_job_mtx);
1468 }
1469
1470 static int
1471 convert_old_sigevent(struct osigevent *osig, struct sigevent *nsig)
1472 {
1473
1474         /*
1475          * Only SIGEV_NONE, SIGEV_SIGNAL, and SIGEV_KEVENT are
1476          * supported by AIO with the old sigevent structure.
1477          */
1478         nsig->sigev_notify = osig->sigev_notify;
1479         switch (nsig->sigev_notify) {
1480         case SIGEV_NONE:
1481                 break;
1482         case SIGEV_SIGNAL:
1483                 nsig->sigev_signo = osig->__sigev_u.__sigev_signo;
1484                 break;
1485         case SIGEV_KEVENT:
1486                 nsig->sigev_notify_kqueue =
1487                     osig->__sigev_u.__sigev_notify_kqueue;
1488                 nsig->sigev_value.sival_ptr = osig->sigev_value.sival_ptr;
1489                 break;
1490         default:
1491                 return (EINVAL);
1492         }
1493         return (0);
1494 }
1495
1496 static int
1497 aiocb_copyin_old_sigevent(struct aiocb *ujob, struct aiocb *kjob)
1498 {
1499         struct oaiocb *ojob;
1500         int error;
1501
1502         bzero(kjob, sizeof(struct aiocb));
1503         error = copyin(ujob, kjob, sizeof(struct oaiocb));
1504         if (error)
1505                 return (error);
1506         ojob = (struct oaiocb *)kjob;
1507         return (convert_old_sigevent(&ojob->aio_sigevent, &kjob->aio_sigevent));
1508 }
1509
1510 static int
1511 aiocb_copyin(struct aiocb *ujob, struct aiocb *kjob)
1512 {
1513
1514         return (copyin(ujob, kjob, sizeof(struct aiocb)));
1515 }
1516
1517 static long
1518 aiocb_fetch_status(struct aiocb *ujob)
1519 {
1520
1521         return (fuword(&ujob->_aiocb_private.status));
1522 }
1523
1524 static long
1525 aiocb_fetch_error(struct aiocb *ujob)
1526 {
1527
1528         return (fuword(&ujob->_aiocb_private.error));
1529 }
1530
1531 static int
1532 aiocb_store_status(struct aiocb *ujob, long status)
1533 {
1534
1535         return (suword(&ujob->_aiocb_private.status, status));
1536 }
1537
1538 static int
1539 aiocb_store_error(struct aiocb *ujob, long error)
1540 {
1541
1542         return (suword(&ujob->_aiocb_private.error, error));
1543 }
1544
1545 static int
1546 aiocb_store_kernelinfo(struct aiocb *ujob, long jobref)
1547 {
1548
1549         return (suword(&ujob->_aiocb_private.kernelinfo, jobref));
1550 }
1551
1552 static int
1553 aiocb_store_aiocb(struct aiocb **ujobp, struct aiocb *ujob)
1554 {
1555
1556         return (suword(ujobp, (long)ujob));
1557 }
1558
1559 static struct aiocb_ops aiocb_ops = {
1560         .copyin = aiocb_copyin,
1561         .fetch_status = aiocb_fetch_status,
1562         .fetch_error = aiocb_fetch_error,
1563         .store_status = aiocb_store_status,
1564         .store_error = aiocb_store_error,
1565         .store_kernelinfo = aiocb_store_kernelinfo,
1566         .store_aiocb = aiocb_store_aiocb,
1567 };
1568
1569 static struct aiocb_ops aiocb_ops_osigevent = {
1570         .copyin = aiocb_copyin_old_sigevent,
1571         .fetch_status = aiocb_fetch_status,
1572         .fetch_error = aiocb_fetch_error,
1573         .store_status = aiocb_store_status,
1574         .store_error = aiocb_store_error,
1575         .store_kernelinfo = aiocb_store_kernelinfo,
1576         .store_aiocb = aiocb_store_aiocb,
1577 };
1578
1579 /*
1580  * Queue a new AIO request.  Choosing either the threaded or direct physio VCHR
1581  * technique is done in this code.
1582  */
1583 int
1584 aio_aqueue(struct thread *td, struct aiocb *job, struct aioliojob *lj,
1585     int type, struct aiocb_ops *ops)
1586 {
1587         struct proc *p = td->td_proc;
1588         cap_rights_t rights;
1589         struct file *fp;
1590         struct socket *so;
1591         struct aiocblist *aiocbe, *cb;
1592         struct kaioinfo *ki;
1593         struct kevent kev;
1594         struct sockbuf *sb;
1595         int opcode;
1596         int error;
1597         int fd, kqfd;
1598         int jid;
1599         u_short evflags;
1600
1601         if (p->p_aioinfo == NULL)
1602                 aio_init_aioinfo(p);
1603
1604         ki = p->p_aioinfo;
1605
1606         ops->store_status(job, -1);
1607         ops->store_error(job, 0);
1608         ops->store_kernelinfo(job, -1);
1609
1610         if (num_queue_count >= max_queue_count ||
1611             ki->kaio_count >= ki->kaio_qallowed_count) {
1612                 ops->store_error(job, EAGAIN);
1613                 return (EAGAIN);
1614         }
1615
1616         aiocbe = uma_zalloc(aiocb_zone, M_WAITOK | M_ZERO);
1617         knlist_init_mtx(&aiocbe->klist, AIO_MTX(ki));
1618
1619         error = ops->copyin(job, &aiocbe->uaiocb);
1620         if (error) {
1621                 ops->store_error(job, error);
1622                 uma_zfree(aiocb_zone, aiocbe);
1623                 return (error);
1624         }
1625
1626         /* XXX: aio_nbytes is later casted to signed types. */
1627         if (aiocbe->uaiocb.aio_nbytes > INT_MAX) {
1628                 uma_zfree(aiocb_zone, aiocbe);
1629                 return (EINVAL);
1630         }
1631
1632         if (aiocbe->uaiocb.aio_sigevent.sigev_notify != SIGEV_KEVENT &&
1633             aiocbe->uaiocb.aio_sigevent.sigev_notify != SIGEV_SIGNAL &&
1634             aiocbe->uaiocb.aio_sigevent.sigev_notify != SIGEV_THREAD_ID &&
1635             aiocbe->uaiocb.aio_sigevent.sigev_notify != SIGEV_NONE) {
1636                 ops->store_error(job, EINVAL);
1637                 uma_zfree(aiocb_zone, aiocbe);
1638                 return (EINVAL);
1639         }
1640
1641         if ((aiocbe->uaiocb.aio_sigevent.sigev_notify == SIGEV_SIGNAL ||
1642              aiocbe->uaiocb.aio_sigevent.sigev_notify == SIGEV_THREAD_ID) &&
1643                 !_SIG_VALID(aiocbe->uaiocb.aio_sigevent.sigev_signo)) {
1644                 uma_zfree(aiocb_zone, aiocbe);
1645                 return (EINVAL);
1646         }
1647
1648         ksiginfo_init(&aiocbe->ksi);
1649
1650         /* Save userspace address of the job info. */
1651         aiocbe->uuaiocb = job;
1652
1653         /* Get the opcode. */
1654         if (type != LIO_NOP)
1655                 aiocbe->uaiocb.aio_lio_opcode = type;
1656         opcode = aiocbe->uaiocb.aio_lio_opcode;
1657
1658         /*
1659          * Validate the opcode and fetch the file object for the specified
1660          * file descriptor.
1661          *
1662          * XXXRW: Moved the opcode validation up here so that we don't
1663          * retrieve a file descriptor without knowing what the capabiltity
1664          * should be.
1665          */
1666         fd = aiocbe->uaiocb.aio_fildes;
1667         switch (opcode) {
1668         case LIO_WRITE:
1669                 error = fget_write(td, fd,
1670                     cap_rights_init(&rights, CAP_PWRITE), &fp);
1671                 break;
1672         case LIO_READ:
1673                 error = fget_read(td, fd,
1674                     cap_rights_init(&rights, CAP_PREAD), &fp);
1675                 break;
1676         case LIO_SYNC:
1677                 error = fget(td, fd, cap_rights_init(&rights, CAP_FSYNC), &fp);
1678                 break;
1679         case LIO_MLOCK:
1680                 fp = NULL;
1681                 break;
1682         case LIO_NOP:
1683                 error = fget(td, fd, cap_rights_init(&rights), &fp);
1684                 break;
1685         default:
1686                 error = EINVAL;
1687         }
1688         if (error) {
1689                 uma_zfree(aiocb_zone, aiocbe);
1690                 ops->store_error(job, error);
1691                 return (error);
1692         }
1693
1694         if (opcode == LIO_SYNC && fp->f_vnode == NULL) {
1695                 error = EINVAL;
1696                 goto aqueue_fail;
1697         }
1698
1699         if (opcode != LIO_SYNC && aiocbe->uaiocb.aio_offset == -1LL) {
1700                 error = EINVAL;
1701                 goto aqueue_fail;
1702         }
1703
1704         aiocbe->fd_file = fp;
1705
1706         mtx_lock(&aio_job_mtx);
1707         jid = jobrefid++;
1708         aiocbe->seqno = jobseqno++;
1709         mtx_unlock(&aio_job_mtx);
1710         error = ops->store_kernelinfo(job, jid);
1711         if (error) {
1712                 error = EINVAL;
1713                 goto aqueue_fail;
1714         }
1715         aiocbe->uaiocb._aiocb_private.kernelinfo = (void *)(intptr_t)jid;
1716
1717         if (opcode == LIO_NOP) {
1718                 fdrop(fp, td);
1719                 uma_zfree(aiocb_zone, aiocbe);
1720                 return (0);
1721         }
1722
1723         if (aiocbe->uaiocb.aio_sigevent.sigev_notify != SIGEV_KEVENT)
1724                 goto no_kqueue;
1725         evflags = aiocbe->uaiocb.aio_sigevent.sigev_notify_kevent_flags;
1726         if ((evflags & ~(EV_CLEAR | EV_DISPATCH | EV_ONESHOT)) != 0) {
1727                 error = EINVAL;
1728                 goto aqueue_fail;
1729         }
1730         kqfd = aiocbe->uaiocb.aio_sigevent.sigev_notify_kqueue;
1731         kev.ident = (uintptr_t)aiocbe->uuaiocb;
1732         kev.filter = EVFILT_AIO;
1733         kev.flags = EV_ADD | EV_ENABLE | EV_FLAG1 | evflags;
1734         kev.data = (intptr_t)aiocbe;
1735         kev.udata = aiocbe->uaiocb.aio_sigevent.sigev_value.sival_ptr;
1736         error = kqfd_register(kqfd, &kev, td, 1);
1737 aqueue_fail:
1738         if (error) {
1739                 if (fp)
1740                         fdrop(fp, td);
1741                 uma_zfree(aiocb_zone, aiocbe);
1742                 ops->store_error(job, error);
1743                 goto done;
1744         }
1745 no_kqueue:
1746
1747         ops->store_error(job, EINPROGRESS);
1748         aiocbe->uaiocb._aiocb_private.error = EINPROGRESS;
1749         aiocbe->userproc = p;
1750         aiocbe->cred = crhold(td->td_ucred);
1751         aiocbe->jobflags = 0;
1752         aiocbe->lio = lj;
1753
1754         if (opcode == LIO_SYNC)
1755                 goto queueit;
1756
1757         if (fp && fp->f_type == DTYPE_SOCKET) {
1758                 /*
1759                  * Alternate queueing for socket ops: Reach down into the
1760                  * descriptor to get the socket data.  Then check to see if the
1761                  * socket is ready to be read or written (based on the requested
1762                  * operation).
1763                  *
1764                  * If it is not ready for io, then queue the aiocbe on the
1765                  * socket, and set the flags so we get a call when sbnotify()
1766                  * happens.
1767                  *
1768                  * Note if opcode is neither LIO_WRITE nor LIO_READ we lock
1769                  * and unlock the snd sockbuf for no reason.
1770                  */
1771                 so = fp->f_data;
1772                 sb = (opcode == LIO_READ) ? &so->so_rcv : &so->so_snd;
1773                 SOCKBUF_LOCK(sb);
1774                 if (((opcode == LIO_READ) && (!soreadable(so))) || ((opcode ==
1775                     LIO_WRITE) && (!sowriteable(so)))) {
1776                         sb->sb_flags |= SB_AIO;
1777
1778                         mtx_lock(&aio_job_mtx);
1779                         TAILQ_INSERT_TAIL(&so->so_aiojobq, aiocbe, list);
1780                         mtx_unlock(&aio_job_mtx);
1781
1782                         AIO_LOCK(ki);
1783                         TAILQ_INSERT_TAIL(&ki->kaio_all, aiocbe, allist);
1784                         TAILQ_INSERT_TAIL(&ki->kaio_jobqueue, aiocbe, plist);
1785                         aiocbe->jobstate = JOBST_JOBQSOCK;
1786                         ki->kaio_count++;
1787                         if (lj)
1788                                 lj->lioj_count++;
1789                         AIO_UNLOCK(ki);
1790                         SOCKBUF_UNLOCK(sb);
1791                         atomic_add_int(&num_queue_count, 1);
1792                         error = 0;
1793                         goto done;
1794                 }
1795                 SOCKBUF_UNLOCK(sb);
1796         }
1797
1798         if ((error = aio_qphysio(p, aiocbe)) == 0)
1799                 goto done;
1800 #if 0
1801         if (error > 0) {
1802                 aiocbe->uaiocb._aiocb_private.error = error;
1803                 ops->store_error(job, error);
1804                 goto done;
1805         }
1806 #endif
1807 queueit:
1808         atomic_add_int(&num_queue_count, 1);
1809
1810         AIO_LOCK(ki);
1811         ki->kaio_count++;
1812         if (lj)
1813                 lj->lioj_count++;
1814         TAILQ_INSERT_TAIL(&ki->kaio_jobqueue, aiocbe, plist);
1815         TAILQ_INSERT_TAIL(&ki->kaio_all, aiocbe, allist);
1816         if (opcode == LIO_SYNC) {
1817                 TAILQ_FOREACH(cb, &ki->kaio_jobqueue, plist) {
1818                         if (cb->fd_file == aiocbe->fd_file &&
1819                             cb->uaiocb.aio_lio_opcode != LIO_SYNC &&
1820                             cb->seqno < aiocbe->seqno) {
1821                                 cb->jobflags |= AIOCBLIST_CHECKSYNC;
1822                                 aiocbe->pending++;
1823                         }
1824                 }
1825                 TAILQ_FOREACH(cb, &ki->kaio_bufqueue, plist) {
1826                         if (cb->fd_file == aiocbe->fd_file &&
1827                             cb->uaiocb.aio_lio_opcode != LIO_SYNC &&
1828                             cb->seqno < aiocbe->seqno) {
1829                                 cb->jobflags |= AIOCBLIST_CHECKSYNC;
1830                                 aiocbe->pending++;
1831                         }
1832                 }
1833                 if (aiocbe->pending != 0) {
1834                         TAILQ_INSERT_TAIL(&ki->kaio_syncqueue, aiocbe, list);
1835                         aiocbe->jobstate = JOBST_JOBQSYNC;
1836                         AIO_UNLOCK(ki);
1837                         goto done;
1838                 }
1839         }
1840         mtx_lock(&aio_job_mtx);
1841         TAILQ_INSERT_TAIL(&aio_jobs, aiocbe, list);
1842         aiocbe->jobstate = JOBST_JOBQGLOBAL;
1843         aio_kick_nowait(p);
1844         mtx_unlock(&aio_job_mtx);
1845         AIO_UNLOCK(ki);
1846         error = 0;
1847 done:
1848         return (error);
1849 }
1850
1851 static void
1852 aio_kick_nowait(struct proc *userp)
1853 {
1854         struct kaioinfo *ki = userp->p_aioinfo;
1855         struct aiothreadlist *aiop;
1856
1857         mtx_assert(&aio_job_mtx, MA_OWNED);
1858         if ((aiop = TAILQ_FIRST(&aio_freeproc)) != NULL) {
1859                 TAILQ_REMOVE(&aio_freeproc, aiop, list);
1860                 aiop->aiothreadflags &= ~AIOP_FREE;
1861                 wakeup(aiop->aiothread);
1862         } else if (((num_aio_resv_start + num_aio_procs) < max_aio_procs) &&
1863             ((ki->kaio_active_count + num_aio_resv_start) <
1864             ki->kaio_maxactive_count)) {
1865                 taskqueue_enqueue(taskqueue_aiod_bio, &ki->kaio_task);
1866         }
1867 }
1868
1869 static int
1870 aio_kick(struct proc *userp)
1871 {
1872         struct kaioinfo *ki = userp->p_aioinfo;
1873         struct aiothreadlist *aiop;
1874         int error, ret = 0;
1875
1876         mtx_assert(&aio_job_mtx, MA_OWNED);
1877 retryproc:
1878         if ((aiop = TAILQ_FIRST(&aio_freeproc)) != NULL) {
1879                 TAILQ_REMOVE(&aio_freeproc, aiop, list);
1880                 aiop->aiothreadflags &= ~AIOP_FREE;
1881                 wakeup(aiop->aiothread);
1882         } else if (((num_aio_resv_start + num_aio_procs) < max_aio_procs) &&
1883             ((ki->kaio_active_count + num_aio_resv_start) <
1884             ki->kaio_maxactive_count)) {
1885                 num_aio_resv_start++;
1886                 mtx_unlock(&aio_job_mtx);
1887                 error = aio_newproc(&num_aio_resv_start);
1888                 mtx_lock(&aio_job_mtx);
1889                 if (error) {
1890                         num_aio_resv_start--;
1891                         goto retryproc;
1892                 }
1893         } else {
1894                 ret = -1;
1895         }
1896         return (ret);
1897 }
1898
1899 static void
1900 aio_kick_helper(void *context, int pending)
1901 {
1902         struct proc *userp = context;
1903
1904         mtx_lock(&aio_job_mtx);
1905         while (--pending >= 0) {
1906                 if (aio_kick(userp))
1907                         break;
1908         }
1909         mtx_unlock(&aio_job_mtx);
1910 }
1911
1912 /*
1913  * Support the aio_return system call, as a side-effect, kernel resources are
1914  * released.
1915  */
1916 static int
1917 kern_aio_return(struct thread *td, struct aiocb *uaiocb, struct aiocb_ops *ops)
1918 {
1919         struct proc *p = td->td_proc;
1920         struct aiocblist *cb;
1921         struct kaioinfo *ki;
1922         int status, error;
1923
1924         ki = p->p_aioinfo;
1925         if (ki == NULL)
1926                 return (EINVAL);
1927         AIO_LOCK(ki);
1928         TAILQ_FOREACH(cb, &ki->kaio_done, plist) {
1929                 if (cb->uuaiocb == uaiocb)
1930                         break;
1931         }
1932         if (cb != NULL) {
1933                 MPASS(cb->jobstate == JOBST_JOBFINISHED);
1934                 status = cb->uaiocb._aiocb_private.status;
1935                 error = cb->uaiocb._aiocb_private.error;
1936                 td->td_retval[0] = status;
1937                 if (cb->uaiocb.aio_lio_opcode == LIO_WRITE) {
1938                         td->td_ru.ru_oublock += cb->outputcharge;
1939                         cb->outputcharge = 0;
1940                 } else if (cb->uaiocb.aio_lio_opcode == LIO_READ) {
1941                         td->td_ru.ru_inblock += cb->inputcharge;
1942                         cb->inputcharge = 0;
1943                 }
1944                 aio_free_entry(cb);
1945                 AIO_UNLOCK(ki);
1946                 ops->store_error(uaiocb, error);
1947                 ops->store_status(uaiocb, status);
1948         } else {
1949                 error = EINVAL;
1950                 AIO_UNLOCK(ki);
1951         }
1952         return (error);
1953 }
1954
1955 int
1956 sys_aio_return(struct thread *td, struct aio_return_args *uap)
1957 {
1958
1959         return (kern_aio_return(td, uap->aiocbp, &aiocb_ops));
1960 }
1961
1962 /*
1963  * Allow a process to wakeup when any of the I/O requests are completed.
1964  */
1965 static int
1966 kern_aio_suspend(struct thread *td, int njoblist, struct aiocb **ujoblist,
1967     struct timespec *ts)
1968 {
1969         struct proc *p = td->td_proc;
1970         struct timeval atv;
1971         struct kaioinfo *ki;
1972         struct aiocblist *cb, *cbfirst;
1973         int error, i, timo;
1974
1975         timo = 0;
1976         if (ts) {
1977                 if (ts->tv_nsec < 0 || ts->tv_nsec >= 1000000000)
1978                         return (EINVAL);
1979
1980                 TIMESPEC_TO_TIMEVAL(&atv, ts);
1981                 if (itimerfix(&atv))
1982                         return (EINVAL);
1983                 timo = tvtohz(&atv);
1984         }
1985
1986         ki = p->p_aioinfo;
1987         if (ki == NULL)
1988                 return (EAGAIN);
1989
1990         if (njoblist == 0)
1991                 return (0);
1992
1993         AIO_LOCK(ki);
1994         for (;;) {
1995                 cbfirst = NULL;
1996                 error = 0;
1997                 TAILQ_FOREACH(cb, &ki->kaio_all, allist) {
1998                         for (i = 0; i < njoblist; i++) {
1999                                 if (cb->uuaiocb == ujoblist[i]) {
2000                                         if (cbfirst == NULL)
2001                                                 cbfirst = cb;
2002                                         if (cb->jobstate == JOBST_JOBFINISHED)
2003                                                 goto RETURN;
2004                                 }
2005                         }
2006                 }
2007                 /* All tasks were finished. */
2008                 if (cbfirst == NULL)
2009                         break;
2010
2011                 ki->kaio_flags |= KAIO_WAKEUP;
2012                 error = msleep(&p->p_aioinfo, AIO_MTX(ki), PRIBIO | PCATCH,
2013                     "aiospn", timo);
2014                 if (error == ERESTART)
2015                         error = EINTR;
2016                 if (error)
2017                         break;
2018         }
2019 RETURN:
2020         AIO_UNLOCK(ki);
2021         return (error);
2022 }
2023
2024 int
2025 sys_aio_suspend(struct thread *td, struct aio_suspend_args *uap)
2026 {
2027         struct timespec ts, *tsp;
2028         struct aiocb **ujoblist;
2029         int error;
2030
2031         if (uap->nent < 0 || uap->nent > AIO_LISTIO_MAX)
2032                 return (EINVAL);
2033
2034         if (uap->timeout) {
2035                 /* Get timespec struct. */
2036                 if ((error = copyin(uap->timeout, &ts, sizeof(ts))) != 0)
2037                         return (error);
2038                 tsp = &ts;
2039         } else
2040                 tsp = NULL;
2041
2042         ujoblist = uma_zalloc(aiol_zone, M_WAITOK);
2043         error = copyin(uap->aiocbp, ujoblist, uap->nent * sizeof(ujoblist[0]));
2044         if (error == 0)
2045                 error = kern_aio_suspend(td, uap->nent, ujoblist, tsp);
2046         uma_zfree(aiol_zone, ujoblist);
2047         return (error);
2048 }
2049
2050 /*
2051  * aio_cancel cancels any non-physio aio operations not currently in
2052  * progress.
2053  */
2054 int
2055 sys_aio_cancel(struct thread *td, struct aio_cancel_args *uap)
2056 {
2057         struct proc *p = td->td_proc;
2058         struct kaioinfo *ki;
2059         struct aiocblist *cbe, *cbn;
2060         struct file *fp;
2061         struct socket *so;
2062         int error;
2063         int remove;
2064         int cancelled = 0;
2065         int notcancelled = 0;
2066         struct vnode *vp;
2067
2068         /* Lookup file object. */
2069         error = fget(td, uap->fd, NULL, &fp);
2070         if (error)
2071                 return (error);
2072
2073         ki = p->p_aioinfo;
2074         if (ki == NULL)
2075                 goto done;
2076
2077         if (fp->f_type == DTYPE_VNODE) {
2078                 vp = fp->f_vnode;
2079                 if (vn_isdisk(vp, &error)) {
2080                         fdrop(fp, td);
2081                         td->td_retval[0] = AIO_NOTCANCELED;
2082                         return (0);
2083                 }
2084         }
2085
2086         AIO_LOCK(ki);
2087         TAILQ_FOREACH_SAFE(cbe, &ki->kaio_jobqueue, plist, cbn) {
2088                 if ((uap->fd == cbe->uaiocb.aio_fildes) &&
2089                     ((uap->aiocbp == NULL) ||
2090                      (uap->aiocbp == cbe->uuaiocb))) {
2091                         remove = 0;
2092
2093                         mtx_lock(&aio_job_mtx);
2094                         if (cbe->jobstate == JOBST_JOBQGLOBAL) {
2095                                 TAILQ_REMOVE(&aio_jobs, cbe, list);
2096                                 remove = 1;
2097                         } else if (cbe->jobstate == JOBST_JOBQSOCK) {
2098                                 MPASS(fp->f_type == DTYPE_SOCKET);
2099                                 so = fp->f_data;
2100                                 TAILQ_REMOVE(&so->so_aiojobq, cbe, list);
2101                                 remove = 1;
2102                         } else if (cbe->jobstate == JOBST_JOBQSYNC) {
2103                                 TAILQ_REMOVE(&ki->kaio_syncqueue, cbe, list);
2104                                 remove = 1;
2105                         }
2106                         mtx_unlock(&aio_job_mtx);
2107
2108                         if (remove) {
2109                                 TAILQ_REMOVE(&ki->kaio_jobqueue, cbe, plist);
2110                                 cbe->uaiocb._aiocb_private.status = -1;
2111                                 cbe->uaiocb._aiocb_private.error = ECANCELED;
2112                                 aio_bio_done_notify(p, cbe, DONE_QUEUE);
2113                                 cancelled++;
2114                         } else {
2115                                 notcancelled++;
2116                         }
2117                         if (uap->aiocbp != NULL)
2118                                 break;
2119                 }
2120         }
2121         AIO_UNLOCK(ki);
2122
2123 done:
2124         fdrop(fp, td);
2125
2126         if (uap->aiocbp != NULL) {
2127                 if (cancelled) {
2128                         td->td_retval[0] = AIO_CANCELED;
2129                         return (0);
2130                 }
2131         }
2132
2133         if (notcancelled) {
2134                 td->td_retval[0] = AIO_NOTCANCELED;
2135                 return (0);
2136         }
2137
2138         if (cancelled) {
2139                 td->td_retval[0] = AIO_CANCELED;
2140                 return (0);
2141         }
2142
2143         td->td_retval[0] = AIO_ALLDONE;
2144
2145         return (0);
2146 }
2147
2148 /*
2149  * aio_error is implemented in the kernel level for compatibility purposes
2150  * only.  For a user mode async implementation, it would be best to do it in
2151  * a userland subroutine.
2152  */
2153 static int
2154 kern_aio_error(struct thread *td, struct aiocb *aiocbp, struct aiocb_ops *ops)
2155 {
2156         struct proc *p = td->td_proc;
2157         struct aiocblist *cb;
2158         struct kaioinfo *ki;
2159         int status;
2160
2161         ki = p->p_aioinfo;
2162         if (ki == NULL) {
2163                 td->td_retval[0] = EINVAL;
2164                 return (0);
2165         }
2166
2167         AIO_LOCK(ki);
2168         TAILQ_FOREACH(cb, &ki->kaio_all, allist) {
2169                 if (cb->uuaiocb == aiocbp) {
2170                         if (cb->jobstate == JOBST_JOBFINISHED)
2171                                 td->td_retval[0] =
2172                                         cb->uaiocb._aiocb_private.error;
2173                         else
2174                                 td->td_retval[0] = EINPROGRESS;
2175                         AIO_UNLOCK(ki);
2176                         return (0);
2177                 }
2178         }
2179         AIO_UNLOCK(ki);
2180
2181         /*
2182          * Hack for failure of aio_aqueue.
2183          */
2184         status = ops->fetch_status(aiocbp);
2185         if (status == -1) {
2186                 td->td_retval[0] = ops->fetch_error(aiocbp);
2187                 return (0);
2188         }
2189
2190         td->td_retval[0] = EINVAL;
2191         return (0);
2192 }
2193
2194 int
2195 sys_aio_error(struct thread *td, struct aio_error_args *uap)
2196 {
2197
2198         return (kern_aio_error(td, uap->aiocbp, &aiocb_ops));
2199 }
2200
2201 /* syscall - asynchronous read from a file (REALTIME) */
2202 int
2203 sys_oaio_read(struct thread *td, struct oaio_read_args *uap)
2204 {
2205
2206         return (aio_aqueue(td, (struct aiocb *)uap->aiocbp, NULL, LIO_READ,
2207             &aiocb_ops_osigevent));
2208 }
2209
2210 int
2211 sys_aio_read(struct thread *td, struct aio_read_args *uap)
2212 {
2213
2214         return (aio_aqueue(td, uap->aiocbp, NULL, LIO_READ, &aiocb_ops));
2215 }
2216
2217 /* syscall - asynchronous write to a file (REALTIME) */
2218 int
2219 sys_oaio_write(struct thread *td, struct oaio_write_args *uap)
2220 {
2221
2222         return (aio_aqueue(td, (struct aiocb *)uap->aiocbp, NULL, LIO_WRITE,
2223             &aiocb_ops_osigevent));
2224 }
2225
2226 int
2227 sys_aio_write(struct thread *td, struct aio_write_args *uap)
2228 {
2229
2230         return (aio_aqueue(td, uap->aiocbp, NULL, LIO_WRITE, &aiocb_ops));
2231 }
2232
2233 int
2234 sys_aio_mlock(struct thread *td, struct aio_mlock_args *uap)
2235 {
2236
2237         return (aio_aqueue(td, uap->aiocbp, NULL, LIO_MLOCK, &aiocb_ops));
2238 }
2239
2240 static int
2241 kern_lio_listio(struct thread *td, int mode, struct aiocb * const *uacb_list,
2242     struct aiocb **acb_list, int nent, struct sigevent *sig,
2243     struct aiocb_ops *ops)
2244 {
2245         struct proc *p = td->td_proc;
2246         struct aiocb *iocb;
2247         struct kaioinfo *ki;
2248         struct aioliojob *lj;
2249         struct kevent kev;
2250         int error;
2251         int nerror;
2252         int i;
2253
2254         if ((mode != LIO_NOWAIT) && (mode != LIO_WAIT))
2255                 return (EINVAL);
2256
2257         if (nent < 0 || nent > AIO_LISTIO_MAX)
2258                 return (EINVAL);
2259
2260         if (p->p_aioinfo == NULL)
2261                 aio_init_aioinfo(p);
2262
2263         ki = p->p_aioinfo;
2264
2265         lj = uma_zalloc(aiolio_zone, M_WAITOK);
2266         lj->lioj_flags = 0;
2267         lj->lioj_count = 0;
2268         lj->lioj_finished_count = 0;
2269         knlist_init_mtx(&lj->klist, AIO_MTX(ki));
2270         ksiginfo_init(&lj->lioj_ksi);
2271
2272         /*
2273          * Setup signal.
2274          */
2275         if (sig && (mode == LIO_NOWAIT)) {
2276                 bcopy(sig, &lj->lioj_signal, sizeof(lj->lioj_signal));
2277                 if (lj->lioj_signal.sigev_notify == SIGEV_KEVENT) {
2278                         /* Assume only new style KEVENT */
2279                         kev.filter = EVFILT_LIO;
2280                         kev.flags = EV_ADD | EV_ENABLE | EV_FLAG1;
2281                         kev.ident = (uintptr_t)uacb_list; /* something unique */
2282                         kev.data = (intptr_t)lj;
2283                         /* pass user defined sigval data */
2284                         kev.udata = lj->lioj_signal.sigev_value.sival_ptr;
2285                         error = kqfd_register(
2286                             lj->lioj_signal.sigev_notify_kqueue, &kev, td, 1);
2287                         if (error) {
2288                                 uma_zfree(aiolio_zone, lj);
2289                                 return (error);
2290                         }
2291                 } else if (lj->lioj_signal.sigev_notify == SIGEV_NONE) {
2292                         ;
2293                 } else if (lj->lioj_signal.sigev_notify == SIGEV_SIGNAL ||
2294                            lj->lioj_signal.sigev_notify == SIGEV_THREAD_ID) {
2295                                 if (!_SIG_VALID(lj->lioj_signal.sigev_signo)) {
2296                                         uma_zfree(aiolio_zone, lj);
2297                                         return EINVAL;
2298                                 }
2299                                 lj->lioj_flags |= LIOJ_SIGNAL;
2300                 } else {
2301                         uma_zfree(aiolio_zone, lj);
2302                         return EINVAL;
2303                 }
2304         }
2305
2306         AIO_LOCK(ki);
2307         TAILQ_INSERT_TAIL(&ki->kaio_liojoblist, lj, lioj_list);
2308         /*
2309          * Add extra aiocb count to avoid the lio to be freed
2310          * by other threads doing aio_waitcomplete or aio_return,
2311          * and prevent event from being sent until we have queued
2312          * all tasks.
2313          */
2314         lj->lioj_count = 1;
2315         AIO_UNLOCK(ki);
2316
2317         /*
2318          * Get pointers to the list of I/O requests.
2319          */
2320         nerror = 0;
2321         for (i = 0; i < nent; i++) {
2322                 iocb = acb_list[i];
2323                 if (iocb != NULL) {
2324                         error = aio_aqueue(td, iocb, lj, LIO_NOP, ops);
2325                         if (error != 0)
2326                                 nerror++;
2327                 }
2328         }
2329
2330         error = 0;
2331         AIO_LOCK(ki);
2332         if (mode == LIO_WAIT) {
2333                 while (lj->lioj_count - 1 != lj->lioj_finished_count) {
2334                         ki->kaio_flags |= KAIO_WAKEUP;
2335                         error = msleep(&p->p_aioinfo, AIO_MTX(ki),
2336                             PRIBIO | PCATCH, "aiospn", 0);
2337                         if (error == ERESTART)
2338                                 error = EINTR;
2339                         if (error)
2340                                 break;
2341                 }
2342         } else {
2343                 if (lj->lioj_count - 1 == lj->lioj_finished_count) {
2344                         if (lj->lioj_signal.sigev_notify == SIGEV_KEVENT) {
2345                                 lj->lioj_flags |= LIOJ_KEVENT_POSTED;
2346                                 KNOTE_LOCKED(&lj->klist, 1);
2347                         }
2348                         if ((lj->lioj_flags & (LIOJ_SIGNAL|LIOJ_SIGNAL_POSTED))
2349                             == LIOJ_SIGNAL
2350                             && (lj->lioj_signal.sigev_notify == SIGEV_SIGNAL ||
2351                             lj->lioj_signal.sigev_notify == SIGEV_THREAD_ID)) {
2352                                 aio_sendsig(p, &lj->lioj_signal,
2353                                             &lj->lioj_ksi);
2354                                 lj->lioj_flags |= LIOJ_SIGNAL_POSTED;
2355                         }
2356                 }
2357         }
2358         lj->lioj_count--;
2359         if (lj->lioj_count == 0) {
2360                 TAILQ_REMOVE(&ki->kaio_liojoblist, lj, lioj_list);
2361                 knlist_delete(&lj->klist, curthread, 1);
2362                 PROC_LOCK(p);
2363                 sigqueue_take(&lj->lioj_ksi);
2364                 PROC_UNLOCK(p);
2365                 AIO_UNLOCK(ki);
2366                 uma_zfree(aiolio_zone, lj);
2367         } else
2368                 AIO_UNLOCK(ki);
2369
2370         if (nerror)
2371                 return (EIO);
2372         return (error);
2373 }
2374
2375 /* syscall - list directed I/O (REALTIME) */
2376 int
2377 sys_olio_listio(struct thread *td, struct olio_listio_args *uap)
2378 {
2379         struct aiocb **acb_list;
2380         struct sigevent *sigp, sig;
2381         struct osigevent osig;
2382         int error, nent;
2383
2384         if ((uap->mode != LIO_NOWAIT) && (uap->mode != LIO_WAIT))
2385                 return (EINVAL);
2386
2387         nent = uap->nent;
2388         if (nent < 0 || nent > AIO_LISTIO_MAX)
2389                 return (EINVAL);
2390
2391         if (uap->sig && (uap->mode == LIO_NOWAIT)) {
2392                 error = copyin(uap->sig, &osig, sizeof(osig));
2393                 if (error)
2394                         return (error);
2395                 error = convert_old_sigevent(&osig, &sig);
2396                 if (error)
2397                         return (error);
2398                 sigp = &sig;
2399         } else
2400                 sigp = NULL;
2401
2402         acb_list = malloc(sizeof(struct aiocb *) * nent, M_LIO, M_WAITOK);
2403         error = copyin(uap->acb_list, acb_list, nent * sizeof(acb_list[0]));
2404         if (error == 0)
2405                 error = kern_lio_listio(td, uap->mode,
2406                     (struct aiocb * const *)uap->acb_list, acb_list, nent, sigp,
2407                     &aiocb_ops_osigevent);
2408         free(acb_list, M_LIO);
2409         return (error);
2410 }
2411
2412 /* syscall - list directed I/O (REALTIME) */
2413 int
2414 sys_lio_listio(struct thread *td, struct lio_listio_args *uap)
2415 {
2416         struct aiocb **acb_list;
2417         struct sigevent *sigp, sig;
2418         int error, nent;
2419
2420         if ((uap->mode != LIO_NOWAIT) && (uap->mode != LIO_WAIT))
2421                 return (EINVAL);
2422
2423         nent = uap->nent;
2424         if (nent < 0 || nent > AIO_LISTIO_MAX)
2425                 return (EINVAL);
2426
2427         if (uap->sig && (uap->mode == LIO_NOWAIT)) {
2428                 error = copyin(uap->sig, &sig, sizeof(sig));
2429                 if (error)
2430                         return (error);
2431                 sigp = &sig;
2432         } else
2433                 sigp = NULL;
2434
2435         acb_list = malloc(sizeof(struct aiocb *) * nent, M_LIO, M_WAITOK);
2436         error = copyin(uap->acb_list, acb_list, nent * sizeof(acb_list[0]));
2437         if (error == 0)
2438                 error = kern_lio_listio(td, uap->mode, uap->acb_list, acb_list,
2439                     nent, sigp, &aiocb_ops);
2440         free(acb_list, M_LIO);
2441         return (error);
2442 }
2443
2444 static void
2445 aio_physwakeup(struct bio *bp)
2446 {
2447         struct aiocblist *aiocbe = (struct aiocblist *)bp->bio_caller1;
2448         struct proc *userp;
2449         struct kaioinfo *ki;
2450         int nblks;
2451
2452         /* Release mapping into kernel space. */
2453         if (aiocbe->pbuf) {
2454                 pmap_qremove((vm_offset_t)aiocbe->pbuf->b_data, aiocbe->npages);
2455                 relpbuf(aiocbe->pbuf, NULL);
2456                 aiocbe->pbuf = NULL;
2457                 atomic_subtract_int(&num_buf_aio, 1);
2458         }
2459         vm_page_unhold_pages(aiocbe->pages, aiocbe->npages);
2460
2461         bp = aiocbe->bp;
2462         aiocbe->bp = NULL;
2463         userp = aiocbe->userproc;
2464         ki = userp->p_aioinfo;
2465         AIO_LOCK(ki);
2466         aiocbe->uaiocb._aiocb_private.status -= bp->bio_resid;
2467         aiocbe->uaiocb._aiocb_private.error = 0;
2468         if (bp->bio_flags & BIO_ERROR)
2469                 aiocbe->uaiocb._aiocb_private.error = bp->bio_error;
2470         nblks = btodb(aiocbe->uaiocb.aio_nbytes);
2471         if (aiocbe->uaiocb.aio_lio_opcode == LIO_WRITE)
2472                 aiocbe->outputcharge += nblks;
2473         else
2474                 aiocbe->inputcharge += nblks;
2475         TAILQ_REMOVE(&userp->p_aioinfo->kaio_bufqueue, aiocbe, plist);
2476         ki->kaio_buffer_count--;
2477         aio_bio_done_notify(userp, aiocbe, DONE_BUF);
2478         AIO_UNLOCK(ki);
2479
2480         g_destroy_bio(bp);
2481 }
2482
2483 /* syscall - wait for the next completion of an aio request */
2484 static int
2485 kern_aio_waitcomplete(struct thread *td, struct aiocb **aiocbp,
2486     struct timespec *ts, struct aiocb_ops *ops)
2487 {
2488         struct proc *p = td->td_proc;
2489         struct timeval atv;
2490         struct kaioinfo *ki;
2491         struct aiocblist *cb;
2492         struct aiocb *uuaiocb;
2493         int error, status, timo;
2494
2495         ops->store_aiocb(aiocbp, NULL);
2496
2497         timo = 0;
2498         if (ts) {
2499                 if ((ts->tv_nsec < 0) || (ts->tv_nsec >= 1000000000))
2500                         return (EINVAL);
2501
2502                 TIMESPEC_TO_TIMEVAL(&atv, ts);
2503                 if (itimerfix(&atv))
2504                         return (EINVAL);
2505                 timo = tvtohz(&atv);
2506         }
2507
2508         if (p->p_aioinfo == NULL)
2509                 aio_init_aioinfo(p);
2510         ki = p->p_aioinfo;
2511
2512         error = 0;
2513         cb = NULL;
2514         AIO_LOCK(ki);
2515         while ((cb = TAILQ_FIRST(&ki->kaio_done)) == NULL) {
2516                 ki->kaio_flags |= KAIO_WAKEUP;
2517                 error = msleep(&p->p_aioinfo, AIO_MTX(ki), PRIBIO | PCATCH,
2518                     "aiowc", timo);
2519                 if (timo && error == ERESTART)
2520                         error = EINTR;
2521                 if (error)
2522                         break;
2523         }
2524
2525         if (cb != NULL) {
2526                 MPASS(cb->jobstate == JOBST_JOBFINISHED);
2527                 uuaiocb = cb->uuaiocb;
2528                 status = cb->uaiocb._aiocb_private.status;
2529                 error = cb->uaiocb._aiocb_private.error;
2530                 td->td_retval[0] = status;
2531                 if (cb->uaiocb.aio_lio_opcode == LIO_WRITE) {
2532                         td->td_ru.ru_oublock += cb->outputcharge;
2533                         cb->outputcharge = 0;
2534                 } else if (cb->uaiocb.aio_lio_opcode == LIO_READ) {
2535                         td->td_ru.ru_inblock += cb->inputcharge;
2536                         cb->inputcharge = 0;
2537                 }
2538                 aio_free_entry(cb);
2539                 AIO_UNLOCK(ki);
2540                 ops->store_aiocb(aiocbp, uuaiocb);
2541                 ops->store_error(uuaiocb, error);
2542                 ops->store_status(uuaiocb, status);
2543         } else
2544                 AIO_UNLOCK(ki);
2545
2546         return (error);
2547 }
2548
2549 int
2550 sys_aio_waitcomplete(struct thread *td, struct aio_waitcomplete_args *uap)
2551 {
2552         struct timespec ts, *tsp;
2553         int error;
2554
2555         if (uap->timeout) {
2556                 /* Get timespec struct. */
2557                 error = copyin(uap->timeout, &ts, sizeof(ts));
2558                 if (error)
2559                         return (error);
2560                 tsp = &ts;
2561         } else
2562                 tsp = NULL;
2563
2564         return (kern_aio_waitcomplete(td, uap->aiocbp, tsp, &aiocb_ops));
2565 }
2566
2567 static int
2568 kern_aio_fsync(struct thread *td, int op, struct aiocb *aiocbp,
2569     struct aiocb_ops *ops)
2570 {
2571
2572         if (op != O_SYNC) /* XXX lack of O_DSYNC */
2573                 return (EINVAL);
2574         return (aio_aqueue(td, aiocbp, NULL, LIO_SYNC, ops));
2575 }
2576
2577 int
2578 sys_aio_fsync(struct thread *td, struct aio_fsync_args *uap)
2579 {
2580
2581         return (kern_aio_fsync(td, uap->op, uap->aiocbp, &aiocb_ops));
2582 }
2583
2584 /* kqueue attach function */
2585 static int
2586 filt_aioattach(struct knote *kn)
2587 {
2588         struct aiocblist *aiocbe = (struct aiocblist *)kn->kn_sdata;
2589
2590         /*
2591          * The aiocbe pointer must be validated before using it, so
2592          * registration is restricted to the kernel; the user cannot
2593          * set EV_FLAG1.
2594          */
2595         if ((kn->kn_flags & EV_FLAG1) == 0)
2596                 return (EPERM);
2597         kn->kn_ptr.p_aio = aiocbe;
2598         kn->kn_flags &= ~EV_FLAG1;
2599
2600         knlist_add(&aiocbe->klist, kn, 0);
2601
2602         return (0);
2603 }
2604
2605 /* kqueue detach function */
2606 static void
2607 filt_aiodetach(struct knote *kn)
2608 {
2609         struct knlist *knl;
2610
2611         knl = &kn->kn_ptr.p_aio->klist;
2612         knl->kl_lock(knl->kl_lockarg);
2613         if (!knlist_empty(knl))
2614                 knlist_remove(knl, kn, 1);
2615         knl->kl_unlock(knl->kl_lockarg);
2616 }
2617
2618 /* kqueue filter function */
2619 /*ARGSUSED*/
2620 static int
2621 filt_aio(struct knote *kn, long hint)
2622 {
2623         struct aiocblist *aiocbe = kn->kn_ptr.p_aio;
2624
2625         kn->kn_data = aiocbe->uaiocb._aiocb_private.error;
2626         if (aiocbe->jobstate != JOBST_JOBFINISHED)
2627                 return (0);
2628         kn->kn_flags |= EV_EOF;
2629         return (1);
2630 }
2631
2632 /* kqueue attach function */
2633 static int
2634 filt_lioattach(struct knote *kn)
2635 {
2636         struct aioliojob * lj = (struct aioliojob *)kn->kn_sdata;
2637
2638         /*
2639          * The aioliojob pointer must be validated before using it, so
2640          * registration is restricted to the kernel; the user cannot
2641          * set EV_FLAG1.
2642          */
2643         if ((kn->kn_flags & EV_FLAG1) == 0)
2644                 return (EPERM);
2645         kn->kn_ptr.p_lio = lj;
2646         kn->kn_flags &= ~EV_FLAG1;
2647
2648         knlist_add(&lj->klist, kn, 0);
2649
2650         return (0);
2651 }
2652
2653 /* kqueue detach function */
2654 static void
2655 filt_liodetach(struct knote *kn)
2656 {
2657         struct knlist *knl;
2658
2659         knl = &kn->kn_ptr.p_lio->klist;
2660         knl->kl_lock(knl->kl_lockarg);
2661         if (!knlist_empty(knl))
2662                 knlist_remove(knl, kn, 1);
2663         knl->kl_unlock(knl->kl_lockarg);
2664 }
2665
2666 /* kqueue filter function */
2667 /*ARGSUSED*/
2668 static int
2669 filt_lio(struct knote *kn, long hint)
2670 {
2671         struct aioliojob * lj = kn->kn_ptr.p_lio;
2672
2673         return (lj->lioj_flags & LIOJ_KEVENT_POSTED);
2674 }
2675
2676 #ifdef COMPAT_FREEBSD32
2677
2678 struct __aiocb_private32 {
2679         int32_t status;
2680         int32_t error;
2681         uint32_t kernelinfo;
2682 };
2683
2684 typedef struct oaiocb32 {
2685         int     aio_fildes;             /* File descriptor */
2686         uint64_t aio_offset __packed;   /* File offset for I/O */
2687         uint32_t aio_buf;               /* I/O buffer in process space */
2688         uint32_t aio_nbytes;            /* Number of bytes for I/O */
2689         struct  osigevent32 aio_sigevent; /* Signal to deliver */
2690         int     aio_lio_opcode;         /* LIO opcode */
2691         int     aio_reqprio;            /* Request priority -- ignored */
2692         struct  __aiocb_private32 _aiocb_private;
2693 } oaiocb32_t;
2694
2695 typedef struct aiocb32 {
2696         int32_t aio_fildes;             /* File descriptor */
2697         uint64_t aio_offset __packed;   /* File offset for I/O */
2698         uint32_t aio_buf;               /* I/O buffer in process space */
2699         uint32_t aio_nbytes;            /* Number of bytes for I/O */
2700         int     __spare__[2];
2701         uint32_t __spare2__;
2702         int     aio_lio_opcode;         /* LIO opcode */
2703         int     aio_reqprio;            /* Request priority -- ignored */
2704         struct __aiocb_private32 _aiocb_private;
2705         struct sigevent32 aio_sigevent; /* Signal to deliver */
2706 } aiocb32_t;
2707
2708 static int
2709 convert_old_sigevent32(struct osigevent32 *osig, struct sigevent *nsig)
2710 {
2711
2712         /*
2713          * Only SIGEV_NONE, SIGEV_SIGNAL, and SIGEV_KEVENT are
2714          * supported by AIO with the old sigevent structure.
2715          */
2716         CP(*osig, *nsig, sigev_notify);
2717         switch (nsig->sigev_notify) {
2718         case SIGEV_NONE:
2719                 break;
2720         case SIGEV_SIGNAL:
2721                 nsig->sigev_signo = osig->__sigev_u.__sigev_signo;
2722                 break;
2723         case SIGEV_KEVENT:
2724                 nsig->sigev_notify_kqueue =
2725                     osig->__sigev_u.__sigev_notify_kqueue;
2726                 PTRIN_CP(*osig, *nsig, sigev_value.sival_ptr);
2727                 break;
2728         default:
2729                 return (EINVAL);
2730         }
2731         return (0);
2732 }
2733
2734 static int
2735 aiocb32_copyin_old_sigevent(struct aiocb *ujob, struct aiocb *kjob)
2736 {
2737         struct oaiocb32 job32;
2738         int error;
2739
2740         bzero(kjob, sizeof(struct aiocb));
2741         error = copyin(ujob, &job32, sizeof(job32));
2742         if (error)
2743                 return (error);
2744
2745         CP(job32, *kjob, aio_fildes);
2746         CP(job32, *kjob, aio_offset);
2747         PTRIN_CP(job32, *kjob, aio_buf);
2748         CP(job32, *kjob, aio_nbytes);
2749         CP(job32, *kjob, aio_lio_opcode);
2750         CP(job32, *kjob, aio_reqprio);
2751         CP(job32, *kjob, _aiocb_private.status);
2752         CP(job32, *kjob, _aiocb_private.error);
2753         PTRIN_CP(job32, *kjob, _aiocb_private.kernelinfo);
2754         return (convert_old_sigevent32(&job32.aio_sigevent,
2755             &kjob->aio_sigevent));
2756 }
2757
2758 static int
2759 aiocb32_copyin(struct aiocb *ujob, struct aiocb *kjob)
2760 {
2761         struct aiocb32 job32;
2762         int error;
2763
2764         error = copyin(ujob, &job32, sizeof(job32));
2765         if (error)
2766                 return (error);
2767         CP(job32, *kjob, aio_fildes);
2768         CP(job32, *kjob, aio_offset);
2769         PTRIN_CP(job32, *kjob, aio_buf);
2770         CP(job32, *kjob, aio_nbytes);
2771         CP(job32, *kjob, aio_lio_opcode);
2772         CP(job32, *kjob, aio_reqprio);
2773         CP(job32, *kjob, _aiocb_private.status);
2774         CP(job32, *kjob, _aiocb_private.error);
2775         PTRIN_CP(job32, *kjob, _aiocb_private.kernelinfo);
2776         return (convert_sigevent32(&job32.aio_sigevent, &kjob->aio_sigevent));
2777 }
2778
2779 static long
2780 aiocb32_fetch_status(struct aiocb *ujob)
2781 {
2782         struct aiocb32 *ujob32;
2783
2784         ujob32 = (struct aiocb32 *)ujob;
2785         return (fuword32(&ujob32->_aiocb_private.status));
2786 }
2787
2788 static long
2789 aiocb32_fetch_error(struct aiocb *ujob)
2790 {
2791         struct aiocb32 *ujob32;
2792
2793         ujob32 = (struct aiocb32 *)ujob;
2794         return (fuword32(&ujob32->_aiocb_private.error));
2795 }
2796
2797 static int
2798 aiocb32_store_status(struct aiocb *ujob, long status)
2799 {
2800         struct aiocb32 *ujob32;
2801
2802         ujob32 = (struct aiocb32 *)ujob;
2803         return (suword32(&ujob32->_aiocb_private.status, status));
2804 }
2805
2806 static int
2807 aiocb32_store_error(struct aiocb *ujob, long error)
2808 {
2809         struct aiocb32 *ujob32;
2810
2811         ujob32 = (struct aiocb32 *)ujob;
2812         return (suword32(&ujob32->_aiocb_private.error, error));
2813 }
2814
2815 static int
2816 aiocb32_store_kernelinfo(struct aiocb *ujob, long jobref)
2817 {
2818         struct aiocb32 *ujob32;
2819
2820         ujob32 = (struct aiocb32 *)ujob;
2821         return (suword32(&ujob32->_aiocb_private.kernelinfo, jobref));
2822 }
2823
2824 static int
2825 aiocb32_store_aiocb(struct aiocb **ujobp, struct aiocb *ujob)
2826 {
2827
2828         return (suword32(ujobp, (long)ujob));
2829 }
2830
2831 static struct aiocb_ops aiocb32_ops = {
2832         .copyin = aiocb32_copyin,
2833         .fetch_status = aiocb32_fetch_status,
2834         .fetch_error = aiocb32_fetch_error,
2835         .store_status = aiocb32_store_status,
2836         .store_error = aiocb32_store_error,
2837         .store_kernelinfo = aiocb32_store_kernelinfo,
2838         .store_aiocb = aiocb32_store_aiocb,
2839 };
2840
2841 static struct aiocb_ops aiocb32_ops_osigevent = {
2842         .copyin = aiocb32_copyin_old_sigevent,
2843         .fetch_status = aiocb32_fetch_status,
2844         .fetch_error = aiocb32_fetch_error,
2845         .store_status = aiocb32_store_status,
2846         .store_error = aiocb32_store_error,
2847         .store_kernelinfo = aiocb32_store_kernelinfo,
2848         .store_aiocb = aiocb32_store_aiocb,
2849 };
2850
2851 int
2852 freebsd32_aio_return(struct thread *td, struct freebsd32_aio_return_args *uap)
2853 {
2854
2855         return (kern_aio_return(td, (struct aiocb *)uap->aiocbp, &aiocb32_ops));
2856 }
2857
2858 int
2859 freebsd32_aio_suspend(struct thread *td, struct freebsd32_aio_suspend_args *uap)
2860 {
2861         struct timespec32 ts32;
2862         struct timespec ts, *tsp;
2863         struct aiocb **ujoblist;
2864         uint32_t *ujoblist32;
2865         int error, i;
2866
2867         if (uap->nent < 0 || uap->nent > AIO_LISTIO_MAX)
2868                 return (EINVAL);
2869
2870         if (uap->timeout) {
2871                 /* Get timespec struct. */
2872                 if ((error = copyin(uap->timeout, &ts32, sizeof(ts32))) != 0)
2873                         return (error);
2874                 CP(ts32, ts, tv_sec);
2875                 CP(ts32, ts, tv_nsec);
2876                 tsp = &ts;
2877         } else
2878                 tsp = NULL;
2879
2880         ujoblist = uma_zalloc(aiol_zone, M_WAITOK);
2881         ujoblist32 = (uint32_t *)ujoblist;
2882         error = copyin(uap->aiocbp, ujoblist32, uap->nent *
2883             sizeof(ujoblist32[0]));
2884         if (error == 0) {
2885                 for (i = uap->nent; i > 0; i--)
2886                         ujoblist[i] = PTRIN(ujoblist32[i]);
2887
2888                 error = kern_aio_suspend(td, uap->nent, ujoblist, tsp);
2889         }
2890         uma_zfree(aiol_zone, ujoblist);
2891         return (error);
2892 }
2893
2894 int
2895 freebsd32_aio_cancel(struct thread *td, struct freebsd32_aio_cancel_args *uap)
2896 {
2897
2898         return (sys_aio_cancel(td, (struct aio_cancel_args *)uap));
2899 }
2900
2901 int
2902 freebsd32_aio_error(struct thread *td, struct freebsd32_aio_error_args *uap)
2903 {
2904
2905         return (kern_aio_error(td, (struct aiocb *)uap->aiocbp, &aiocb32_ops));
2906 }
2907
2908 int
2909 freebsd32_oaio_read(struct thread *td, struct freebsd32_oaio_read_args *uap)
2910 {
2911
2912         return (aio_aqueue(td, (struct aiocb *)uap->aiocbp, NULL, LIO_READ,
2913             &aiocb32_ops_osigevent));
2914 }
2915
2916 int
2917 freebsd32_aio_read(struct thread *td, struct freebsd32_aio_read_args *uap)
2918 {
2919
2920         return (aio_aqueue(td, (struct aiocb *)uap->aiocbp, NULL, LIO_READ,
2921             &aiocb32_ops));
2922 }
2923
2924 int
2925 freebsd32_oaio_write(struct thread *td, struct freebsd32_oaio_write_args *uap)
2926 {
2927
2928         return (aio_aqueue(td, (struct aiocb *)uap->aiocbp, NULL, LIO_WRITE,
2929             &aiocb32_ops_osigevent));
2930 }
2931
2932 int
2933 freebsd32_aio_write(struct thread *td, struct freebsd32_aio_write_args *uap)
2934 {
2935
2936         return (aio_aqueue(td, (struct aiocb *)uap->aiocbp, NULL, LIO_WRITE,
2937             &aiocb32_ops));
2938 }
2939
2940 int
2941 freebsd32_aio_mlock(struct thread *td, struct freebsd32_aio_mlock_args *uap)
2942 {
2943
2944         return (aio_aqueue(td, (struct aiocb *)uap->aiocbp, NULL, LIO_MLOCK,
2945             &aiocb32_ops));
2946 }
2947
2948 int
2949 freebsd32_aio_waitcomplete(struct thread *td,
2950     struct freebsd32_aio_waitcomplete_args *uap)
2951 {
2952         struct timespec32 ts32;
2953         struct timespec ts, *tsp;
2954         int error;
2955
2956         if (uap->timeout) {
2957                 /* Get timespec struct. */
2958                 error = copyin(uap->timeout, &ts32, sizeof(ts32));
2959                 if (error)
2960                         return (error);
2961                 CP(ts32, ts, tv_sec);
2962                 CP(ts32, ts, tv_nsec);
2963                 tsp = &ts;
2964         } else
2965                 tsp = NULL;
2966
2967         return (kern_aio_waitcomplete(td, (struct aiocb **)uap->aiocbp, tsp,
2968             &aiocb32_ops));
2969 }
2970
2971 int
2972 freebsd32_aio_fsync(struct thread *td, struct freebsd32_aio_fsync_args *uap)
2973 {
2974
2975         return (kern_aio_fsync(td, uap->op, (struct aiocb *)uap->aiocbp,
2976             &aiocb32_ops));
2977 }
2978
2979 int
2980 freebsd32_olio_listio(struct thread *td, struct freebsd32_olio_listio_args *uap)
2981 {
2982         struct aiocb **acb_list;
2983         struct sigevent *sigp, sig;
2984         struct osigevent32 osig;
2985         uint32_t *acb_list32;
2986         int error, i, nent;
2987
2988         if ((uap->mode != LIO_NOWAIT) && (uap->mode != LIO_WAIT))
2989                 return (EINVAL);
2990
2991         nent = uap->nent;
2992         if (nent < 0 || nent > AIO_LISTIO_MAX)
2993                 return (EINVAL);
2994
2995         if (uap->sig && (uap->mode == LIO_NOWAIT)) {
2996                 error = copyin(uap->sig, &osig, sizeof(osig));
2997                 if (error)
2998                         return (error);
2999                 error = convert_old_sigevent32(&osig, &sig);
3000                 if (error)
3001                         return (error);
3002                 sigp = &sig;
3003         } else
3004                 sigp = NULL;
3005
3006         acb_list32 = malloc(sizeof(uint32_t) * nent, M_LIO, M_WAITOK);
3007         error = copyin(uap->acb_list, acb_list32, nent * sizeof(uint32_t));
3008         if (error) {
3009                 free(acb_list32, M_LIO);
3010                 return (error);
3011         }
3012         acb_list = malloc(sizeof(struct aiocb *) * nent, M_LIO, M_WAITOK);
3013         for (i = 0; i < nent; i++)
3014                 acb_list[i] = PTRIN(acb_list32[i]);
3015         free(acb_list32, M_LIO);
3016
3017         error = kern_lio_listio(td, uap->mode,
3018             (struct aiocb * const *)uap->acb_list, acb_list, nent, sigp,
3019             &aiocb32_ops_osigevent);
3020         free(acb_list, M_LIO);
3021         return (error);
3022 }
3023
3024 int
3025 freebsd32_lio_listio(struct thread *td, struct freebsd32_lio_listio_args *uap)
3026 {
3027         struct aiocb **acb_list;
3028         struct sigevent *sigp, sig;
3029         struct sigevent32 sig32;
3030         uint32_t *acb_list32;
3031         int error, i, nent;
3032
3033         if ((uap->mode != LIO_NOWAIT) && (uap->mode != LIO_WAIT))
3034                 return (EINVAL);
3035
3036         nent = uap->nent;
3037         if (nent < 0 || nent > AIO_LISTIO_MAX)
3038                 return (EINVAL);
3039
3040         if (uap->sig && (uap->mode == LIO_NOWAIT)) {
3041                 error = copyin(uap->sig, &sig32, sizeof(sig32));
3042                 if (error)
3043                         return (error);
3044                 error = convert_sigevent32(&sig32, &sig);
3045                 if (error)
3046                         return (error);
3047                 sigp = &sig;
3048         } else
3049                 sigp = NULL;
3050
3051         acb_list32 = malloc(sizeof(uint32_t) * nent, M_LIO, M_WAITOK);
3052         error = copyin(uap->acb_list, acb_list32, nent * sizeof(uint32_t));
3053         if (error) {
3054                 free(acb_list32, M_LIO);
3055                 return (error);
3056         }
3057         acb_list = malloc(sizeof(struct aiocb *) * nent, M_LIO, M_WAITOK);
3058         for (i = 0; i < nent; i++)
3059                 acb_list[i] = PTRIN(acb_list32[i]);
3060         free(acb_list32, M_LIO);
3061
3062         error = kern_lio_listio(td, uap->mode,
3063             (struct aiocb * const *)uap->acb_list, acb_list, nent, sigp,
3064             &aiocb32_ops);
3065         free(acb_list, M_LIO);
3066         return (error);
3067 }
3068
3069 #endif