]> CyberLeo.Net >> Repos - FreeBSD/releng/10.2.git/blob - sys/kern/vfs_aio.c
- Copy stable/10@285827 to releng/10.2 in preparation for 10.2-RC1
[FreeBSD/releng/10.2.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         }
1359
1360         AIO_LOCK(ki);
1361         ki->kaio_count++;
1362         if (!unmap)
1363                 ki->kaio_buffer_count++;
1364         lj = aiocbe->lio;
1365         if (lj)
1366                 lj->lioj_count++;
1367         TAILQ_INSERT_TAIL(&ki->kaio_bufqueue, aiocbe, plist);
1368         TAILQ_INSERT_TAIL(&ki->kaio_all, aiocbe, allist);
1369         aiocbe->jobstate = JOBST_JOBQBUF;
1370         cb->_aiocb_private.status = cb->aio_nbytes;
1371         AIO_UNLOCK(ki);
1372
1373         bp->bio_length = cb->aio_nbytes;
1374         bp->bio_bcount = cb->aio_nbytes;
1375         bp->bio_done = aio_physwakeup;
1376         bp->bio_data = (void *)(uintptr_t)cb->aio_buf;
1377         bp->bio_offset = cb->aio_offset;
1378         bp->bio_cmd = cb->aio_lio_opcode == LIO_WRITE ? BIO_WRITE : BIO_READ;
1379         bp->bio_dev = dev;
1380         bp->bio_caller1 = (void *)aiocbe;
1381
1382         prot = VM_PROT_READ;
1383         if (cb->aio_lio_opcode == LIO_READ)
1384                 prot |= VM_PROT_WRITE;  /* Less backwards than it looks */
1385         if ((aiocbe->npages = vm_fault_quick_hold_pages(
1386             &curproc->p_vmspace->vm_map,
1387             (vm_offset_t)bp->bio_data, bp->bio_length, prot, aiocbe->pages,
1388             sizeof(aiocbe->pages)/sizeof(aiocbe->pages[0]))) < 0) {
1389                 error = EFAULT;
1390                 goto doerror;
1391         }
1392         if (!unmap) {
1393                 pmap_qenter((vm_offset_t)pbuf->b_data,
1394                     aiocbe->pages, aiocbe->npages);
1395                 bp->bio_data = pbuf->b_data + poff;
1396         } else {
1397                 bp->bio_ma = aiocbe->pages;
1398                 bp->bio_ma_n = aiocbe->npages;
1399                 bp->bio_ma_offset = poff;
1400                 bp->bio_data = unmapped_buf;
1401                 bp->bio_flags |= BIO_UNMAPPED;
1402         }
1403
1404         atomic_add_int(&num_queue_count, 1);
1405         if (!unmap)
1406                 atomic_add_int(&num_buf_aio, 1);
1407
1408         /* Perform transfer. */
1409         csw->d_strategy(bp);
1410         dev_relthread(dev, ref);
1411         return (0);
1412
1413 doerror:
1414         AIO_LOCK(ki);
1415         aiocbe->jobstate = JOBST_NULL;
1416         TAILQ_REMOVE(&ki->kaio_bufqueue, aiocbe, plist);
1417         TAILQ_REMOVE(&ki->kaio_all, aiocbe, allist);
1418         ki->kaio_count--;
1419         if (!unmap)
1420                 ki->kaio_buffer_count--;
1421         if (lj)
1422                 lj->lioj_count--;
1423         AIO_UNLOCK(ki);
1424         if (pbuf) {
1425                 relpbuf(pbuf, NULL);
1426                 aiocbe->pbuf = NULL;
1427         }
1428         g_destroy_bio(bp);
1429         aiocbe->bp = NULL;
1430 unref:
1431         dev_relthread(dev, ref);
1432         return (error);
1433 }
1434
1435 /*
1436  * Wake up aio requests that may be serviceable now.
1437  */
1438 static void
1439 aio_swake_cb(struct socket *so, struct sockbuf *sb)
1440 {
1441         struct aiocblist *cb, *cbn;
1442         int opcode;
1443
1444         SOCKBUF_LOCK_ASSERT(sb);
1445         if (sb == &so->so_snd)
1446                 opcode = LIO_WRITE;
1447         else
1448                 opcode = LIO_READ;
1449
1450         sb->sb_flags &= ~SB_AIO;
1451         mtx_lock(&aio_job_mtx);
1452         TAILQ_FOREACH_SAFE(cb, &so->so_aiojobq, list, cbn) {
1453                 if (opcode == cb->uaiocb.aio_lio_opcode) {
1454                         if (cb->jobstate != JOBST_JOBQSOCK)
1455                                 panic("invalid queue value");
1456                         /* XXX
1457                          * We don't have actual sockets backend yet,
1458                          * so we simply move the requests to the generic
1459                          * file I/O backend.
1460                          */
1461                         TAILQ_REMOVE(&so->so_aiojobq, cb, list);
1462                         TAILQ_INSERT_TAIL(&aio_jobs, cb, list);
1463                         aio_kick_nowait(cb->userproc);
1464                 }
1465         }
1466         mtx_unlock(&aio_job_mtx);
1467 }
1468
1469 static int
1470 convert_old_sigevent(struct osigevent *osig, struct sigevent *nsig)
1471 {
1472
1473         /*
1474          * Only SIGEV_NONE, SIGEV_SIGNAL, and SIGEV_KEVENT are
1475          * supported by AIO with the old sigevent structure.
1476          */
1477         nsig->sigev_notify = osig->sigev_notify;
1478         switch (nsig->sigev_notify) {
1479         case SIGEV_NONE:
1480                 break;
1481         case SIGEV_SIGNAL:
1482                 nsig->sigev_signo = osig->__sigev_u.__sigev_signo;
1483                 break;
1484         case SIGEV_KEVENT:
1485                 nsig->sigev_notify_kqueue =
1486                     osig->__sigev_u.__sigev_notify_kqueue;
1487                 nsig->sigev_value.sival_ptr = osig->sigev_value.sival_ptr;
1488                 break;
1489         default:
1490                 return (EINVAL);
1491         }
1492         return (0);
1493 }
1494
1495 static int
1496 aiocb_copyin_old_sigevent(struct aiocb *ujob, struct aiocb *kjob)
1497 {
1498         struct oaiocb *ojob;
1499         int error;
1500
1501         bzero(kjob, sizeof(struct aiocb));
1502         error = copyin(ujob, kjob, sizeof(struct oaiocb));
1503         if (error)
1504                 return (error);
1505         ojob = (struct oaiocb *)kjob;
1506         return (convert_old_sigevent(&ojob->aio_sigevent, &kjob->aio_sigevent));
1507 }
1508
1509 static int
1510 aiocb_copyin(struct aiocb *ujob, struct aiocb *kjob)
1511 {
1512
1513         return (copyin(ujob, kjob, sizeof(struct aiocb)));
1514 }
1515
1516 static long
1517 aiocb_fetch_status(struct aiocb *ujob)
1518 {
1519
1520         return (fuword(&ujob->_aiocb_private.status));
1521 }
1522
1523 static long
1524 aiocb_fetch_error(struct aiocb *ujob)
1525 {
1526
1527         return (fuword(&ujob->_aiocb_private.error));
1528 }
1529
1530 static int
1531 aiocb_store_status(struct aiocb *ujob, long status)
1532 {
1533
1534         return (suword(&ujob->_aiocb_private.status, status));
1535 }
1536
1537 static int
1538 aiocb_store_error(struct aiocb *ujob, long error)
1539 {
1540
1541         return (suword(&ujob->_aiocb_private.error, error));
1542 }
1543
1544 static int
1545 aiocb_store_kernelinfo(struct aiocb *ujob, long jobref)
1546 {
1547
1548         return (suword(&ujob->_aiocb_private.kernelinfo, jobref));
1549 }
1550
1551 static int
1552 aiocb_store_aiocb(struct aiocb **ujobp, struct aiocb *ujob)
1553 {
1554
1555         return (suword(ujobp, (long)ujob));
1556 }
1557
1558 static struct aiocb_ops aiocb_ops = {
1559         .copyin = aiocb_copyin,
1560         .fetch_status = aiocb_fetch_status,
1561         .fetch_error = aiocb_fetch_error,
1562         .store_status = aiocb_store_status,
1563         .store_error = aiocb_store_error,
1564         .store_kernelinfo = aiocb_store_kernelinfo,
1565         .store_aiocb = aiocb_store_aiocb,
1566 };
1567
1568 static struct aiocb_ops aiocb_ops_osigevent = {
1569         .copyin = aiocb_copyin_old_sigevent,
1570         .fetch_status = aiocb_fetch_status,
1571         .fetch_error = aiocb_fetch_error,
1572         .store_status = aiocb_store_status,
1573         .store_error = aiocb_store_error,
1574         .store_kernelinfo = aiocb_store_kernelinfo,
1575         .store_aiocb = aiocb_store_aiocb,
1576 };
1577
1578 /*
1579  * Queue a new AIO request.  Choosing either the threaded or direct physio VCHR
1580  * technique is done in this code.
1581  */
1582 int
1583 aio_aqueue(struct thread *td, struct aiocb *job, struct aioliojob *lj,
1584         int type, struct aiocb_ops *ops)
1585 {
1586         struct proc *p = td->td_proc;
1587         cap_rights_t rights;
1588         struct file *fp;
1589         struct socket *so;
1590         struct aiocblist *aiocbe, *cb;
1591         struct kaioinfo *ki;
1592         struct kevent kev;
1593         struct sockbuf *sb;
1594         int opcode;
1595         int error;
1596         int fd, kqfd;
1597         int jid;
1598         u_short evflags;
1599
1600         if (p->p_aioinfo == NULL)
1601                 aio_init_aioinfo(p);
1602
1603         ki = p->p_aioinfo;
1604
1605         ops->store_status(job, -1);
1606         ops->store_error(job, 0);
1607         ops->store_kernelinfo(job, -1);
1608
1609         if (num_queue_count >= max_queue_count ||
1610             ki->kaio_count >= ki->kaio_qallowed_count) {
1611                 ops->store_error(job, EAGAIN);
1612                 return (EAGAIN);
1613         }
1614
1615         aiocbe = uma_zalloc(aiocb_zone, M_WAITOK | M_ZERO);
1616         knlist_init_mtx(&aiocbe->klist, AIO_MTX(ki));
1617
1618         error = ops->copyin(job, &aiocbe->uaiocb);
1619         if (error) {
1620                 ops->store_error(job, error);
1621                 uma_zfree(aiocb_zone, aiocbe);
1622                 return (error);
1623         }
1624
1625         /* XXX: aio_nbytes is later casted to signed types. */
1626         if (aiocbe->uaiocb.aio_nbytes > INT_MAX) {
1627                 uma_zfree(aiocb_zone, aiocbe);
1628                 return (EINVAL);
1629         }
1630
1631         if (aiocbe->uaiocb.aio_sigevent.sigev_notify != SIGEV_KEVENT &&
1632             aiocbe->uaiocb.aio_sigevent.sigev_notify != SIGEV_SIGNAL &&
1633             aiocbe->uaiocb.aio_sigevent.sigev_notify != SIGEV_THREAD_ID &&
1634             aiocbe->uaiocb.aio_sigevent.sigev_notify != SIGEV_NONE) {
1635                 ops->store_error(job, EINVAL);
1636                 uma_zfree(aiocb_zone, aiocbe);
1637                 return (EINVAL);
1638         }
1639
1640         if ((aiocbe->uaiocb.aio_sigevent.sigev_notify == SIGEV_SIGNAL ||
1641              aiocbe->uaiocb.aio_sigevent.sigev_notify == SIGEV_THREAD_ID) &&
1642                 !_SIG_VALID(aiocbe->uaiocb.aio_sigevent.sigev_signo)) {
1643                 uma_zfree(aiocb_zone, aiocbe);
1644                 return (EINVAL);
1645         }
1646
1647         ksiginfo_init(&aiocbe->ksi);
1648
1649         /* Save userspace address of the job info. */
1650         aiocbe->uuaiocb = job;
1651
1652         /* Get the opcode. */
1653         if (type != LIO_NOP)
1654                 aiocbe->uaiocb.aio_lio_opcode = type;
1655         opcode = aiocbe->uaiocb.aio_lio_opcode;
1656
1657         /*
1658          * Validate the opcode and fetch the file object for the specified
1659          * file descriptor.
1660          *
1661          * XXXRW: Moved the opcode validation up here so that we don't
1662          * retrieve a file descriptor without knowing what the capabiltity
1663          * should be.
1664          */
1665         fd = aiocbe->uaiocb.aio_fildes;
1666         switch (opcode) {
1667         case LIO_WRITE:
1668                 error = fget_write(td, fd,
1669                     cap_rights_init(&rights, CAP_PWRITE), &fp);
1670                 break;
1671         case LIO_READ:
1672                 error = fget_read(td, fd,
1673                     cap_rights_init(&rights, CAP_PREAD), &fp);
1674                 break;
1675         case LIO_SYNC:
1676                 error = fget(td, fd, cap_rights_init(&rights, CAP_FSYNC), &fp);
1677                 break;
1678         case LIO_MLOCK:
1679                 fp = NULL;
1680                 break;
1681         case LIO_NOP:
1682                 error = fget(td, fd, cap_rights_init(&rights), &fp);
1683                 break;
1684         default:
1685                 error = EINVAL;
1686         }
1687         if (error) {
1688                 uma_zfree(aiocb_zone, aiocbe);
1689                 ops->store_error(job, error);
1690                 return (error);
1691         }
1692
1693         if (opcode == LIO_SYNC && fp->f_vnode == NULL) {
1694                 error = EINVAL;
1695                 goto aqueue_fail;
1696         }
1697
1698         if (opcode != LIO_SYNC && aiocbe->uaiocb.aio_offset == -1LL) {
1699                 error = EINVAL;
1700                 goto aqueue_fail;
1701         }
1702
1703         aiocbe->fd_file = fp;
1704
1705         mtx_lock(&aio_job_mtx);
1706         jid = jobrefid++;
1707         aiocbe->seqno = jobseqno++;
1708         mtx_unlock(&aio_job_mtx);
1709         error = ops->store_kernelinfo(job, jid);
1710         if (error) {
1711                 error = EINVAL;
1712                 goto aqueue_fail;
1713         }
1714         aiocbe->uaiocb._aiocb_private.kernelinfo = (void *)(intptr_t)jid;
1715
1716         if (opcode == LIO_NOP) {
1717                 fdrop(fp, td);
1718                 uma_zfree(aiocb_zone, aiocbe);
1719                 return (0);
1720         }
1721
1722         if (aiocbe->uaiocb.aio_sigevent.sigev_notify != SIGEV_KEVENT)
1723                 goto no_kqueue;
1724         evflags = aiocbe->uaiocb.aio_sigevent.sigev_notify_kevent_flags;
1725         if ((evflags & ~(EV_CLEAR | EV_DISPATCH | EV_ONESHOT)) != 0) {
1726                 error = EINVAL;
1727                 goto aqueue_fail;
1728         }
1729         kqfd = aiocbe->uaiocb.aio_sigevent.sigev_notify_kqueue;
1730         kev.ident = (uintptr_t)aiocbe->uuaiocb;
1731         kev.filter = EVFILT_AIO;
1732         kev.flags = EV_ADD | EV_ENABLE | EV_FLAG1 | evflags;
1733         kev.data = (intptr_t)aiocbe;
1734         kev.udata = aiocbe->uaiocb.aio_sigevent.sigev_value.sival_ptr;
1735         error = kqfd_register(kqfd, &kev, td, 1);
1736 aqueue_fail:
1737         if (error) {
1738                 if (fp)
1739                         fdrop(fp, td);
1740                 uma_zfree(aiocb_zone, aiocbe);
1741                 ops->store_error(job, error);
1742                 goto done;
1743         }
1744 no_kqueue:
1745
1746         ops->store_error(job, EINPROGRESS);
1747         aiocbe->uaiocb._aiocb_private.error = EINPROGRESS;
1748         aiocbe->userproc = p;
1749         aiocbe->cred = crhold(td->td_ucred);
1750         aiocbe->jobflags = 0;
1751         aiocbe->lio = lj;
1752
1753         if (opcode == LIO_SYNC)
1754                 goto queueit;
1755
1756         if (fp && fp->f_type == DTYPE_SOCKET) {
1757                 /*
1758                  * Alternate queueing for socket ops: Reach down into the
1759                  * descriptor to get the socket data.  Then check to see if the
1760                  * socket is ready to be read or written (based on the requested
1761                  * operation).
1762                  *
1763                  * If it is not ready for io, then queue the aiocbe on the
1764                  * socket, and set the flags so we get a call when sbnotify()
1765                  * happens.
1766                  *
1767                  * Note if opcode is neither LIO_WRITE nor LIO_READ we lock
1768                  * and unlock the snd sockbuf for no reason.
1769                  */
1770                 so = fp->f_data;
1771                 sb = (opcode == LIO_READ) ? &so->so_rcv : &so->so_snd;
1772                 SOCKBUF_LOCK(sb);
1773                 if (((opcode == LIO_READ) && (!soreadable(so))) || ((opcode ==
1774                     LIO_WRITE) && (!sowriteable(so)))) {
1775                         sb->sb_flags |= SB_AIO;
1776
1777                         mtx_lock(&aio_job_mtx);
1778                         TAILQ_INSERT_TAIL(&so->so_aiojobq, aiocbe, list);
1779                         mtx_unlock(&aio_job_mtx);
1780
1781                         AIO_LOCK(ki);
1782                         TAILQ_INSERT_TAIL(&ki->kaio_all, aiocbe, allist);
1783                         TAILQ_INSERT_TAIL(&ki->kaio_jobqueue, aiocbe, plist);
1784                         aiocbe->jobstate = JOBST_JOBQSOCK;
1785                         ki->kaio_count++;
1786                         if (lj)
1787                                 lj->lioj_count++;
1788                         AIO_UNLOCK(ki);
1789                         SOCKBUF_UNLOCK(sb);
1790                         atomic_add_int(&num_queue_count, 1);
1791                         error = 0;
1792                         goto done;
1793                 }
1794                 SOCKBUF_UNLOCK(sb);
1795         }
1796
1797         if ((error = aio_qphysio(p, aiocbe)) == 0)
1798                 goto done;
1799 #if 0
1800         if (error > 0) {
1801                 aiocbe->uaiocb._aiocb_private.error = error;
1802                 ops->store_error(job, error);
1803                 goto done;
1804         }
1805 #endif
1806 queueit:
1807         atomic_add_int(&num_queue_count, 1);
1808
1809         AIO_LOCK(ki);
1810         ki->kaio_count++;
1811         if (lj)
1812                 lj->lioj_count++;
1813         TAILQ_INSERT_TAIL(&ki->kaio_jobqueue, aiocbe, plist);
1814         TAILQ_INSERT_TAIL(&ki->kaio_all, aiocbe, allist);
1815         if (opcode == LIO_SYNC) {
1816                 TAILQ_FOREACH(cb, &ki->kaio_jobqueue, plist) {
1817                         if (cb->fd_file == aiocbe->fd_file &&
1818                             cb->uaiocb.aio_lio_opcode != LIO_SYNC &&
1819                             cb->seqno < aiocbe->seqno) {
1820                                 cb->jobflags |= AIOCBLIST_CHECKSYNC;
1821                                 aiocbe->pending++;
1822                         }
1823                 }
1824                 TAILQ_FOREACH(cb, &ki->kaio_bufqueue, plist) {
1825                         if (cb->fd_file == aiocbe->fd_file &&
1826                             cb->uaiocb.aio_lio_opcode != LIO_SYNC &&
1827                             cb->seqno < aiocbe->seqno) {
1828                                 cb->jobflags |= AIOCBLIST_CHECKSYNC;
1829                                 aiocbe->pending++;
1830                         }
1831                 }
1832                 if (aiocbe->pending != 0) {
1833                         TAILQ_INSERT_TAIL(&ki->kaio_syncqueue, aiocbe, list);
1834                         aiocbe->jobstate = JOBST_JOBQSYNC;
1835                         AIO_UNLOCK(ki);
1836                         goto done;
1837                 }
1838         }
1839         mtx_lock(&aio_job_mtx);
1840         TAILQ_INSERT_TAIL(&aio_jobs, aiocbe, list);
1841         aiocbe->jobstate = JOBST_JOBQGLOBAL;
1842         aio_kick_nowait(p);
1843         mtx_unlock(&aio_job_mtx);
1844         AIO_UNLOCK(ki);
1845         error = 0;
1846 done:
1847         return (error);
1848 }
1849
1850 static void
1851 aio_kick_nowait(struct proc *userp)
1852 {
1853         struct kaioinfo *ki = userp->p_aioinfo;
1854         struct aiothreadlist *aiop;
1855
1856         mtx_assert(&aio_job_mtx, MA_OWNED);
1857         if ((aiop = TAILQ_FIRST(&aio_freeproc)) != NULL) {
1858                 TAILQ_REMOVE(&aio_freeproc, aiop, list);
1859                 aiop->aiothreadflags &= ~AIOP_FREE;
1860                 wakeup(aiop->aiothread);
1861         } else if (((num_aio_resv_start + num_aio_procs) < max_aio_procs) &&
1862             ((ki->kaio_active_count + num_aio_resv_start) <
1863             ki->kaio_maxactive_count)) {
1864                 taskqueue_enqueue(taskqueue_aiod_bio, &ki->kaio_task);
1865         }
1866 }
1867
1868 static int
1869 aio_kick(struct proc *userp)
1870 {
1871         struct kaioinfo *ki = userp->p_aioinfo;
1872         struct aiothreadlist *aiop;
1873         int error, ret = 0;
1874
1875         mtx_assert(&aio_job_mtx, MA_OWNED);
1876 retryproc:
1877         if ((aiop = TAILQ_FIRST(&aio_freeproc)) != NULL) {
1878                 TAILQ_REMOVE(&aio_freeproc, aiop, list);
1879                 aiop->aiothreadflags &= ~AIOP_FREE;
1880                 wakeup(aiop->aiothread);
1881         } else if (((num_aio_resv_start + num_aio_procs) < max_aio_procs) &&
1882             ((ki->kaio_active_count + num_aio_resv_start) <
1883             ki->kaio_maxactive_count)) {
1884                 num_aio_resv_start++;
1885                 mtx_unlock(&aio_job_mtx);
1886                 error = aio_newproc(&num_aio_resv_start);
1887                 mtx_lock(&aio_job_mtx);
1888                 if (error) {
1889                         num_aio_resv_start--;
1890                         goto retryproc;
1891                 }
1892         } else {
1893                 ret = -1;
1894         }
1895         return (ret);
1896 }
1897
1898 static void
1899 aio_kick_helper(void *context, int pending)
1900 {
1901         struct proc *userp = context;
1902
1903         mtx_lock(&aio_job_mtx);
1904         while (--pending >= 0) {
1905                 if (aio_kick(userp))
1906                         break;
1907         }
1908         mtx_unlock(&aio_job_mtx);
1909 }
1910
1911 /*
1912  * Support the aio_return system call, as a side-effect, kernel resources are
1913  * released.
1914  */
1915 static int
1916 kern_aio_return(struct thread *td, struct aiocb *uaiocb, struct aiocb_ops *ops)
1917 {
1918         struct proc *p = td->td_proc;
1919         struct aiocblist *cb;
1920         struct kaioinfo *ki;
1921         int status, error;
1922
1923         ki = p->p_aioinfo;
1924         if (ki == NULL)
1925                 return (EINVAL);
1926         AIO_LOCK(ki);
1927         TAILQ_FOREACH(cb, &ki->kaio_done, plist) {
1928                 if (cb->uuaiocb == uaiocb)
1929                         break;
1930         }
1931         if (cb != NULL) {
1932                 MPASS(cb->jobstate == JOBST_JOBFINISHED);
1933                 status = cb->uaiocb._aiocb_private.status;
1934                 error = cb->uaiocb._aiocb_private.error;
1935                 td->td_retval[0] = status;
1936                 if (cb->uaiocb.aio_lio_opcode == LIO_WRITE) {
1937                         td->td_ru.ru_oublock += cb->outputcharge;
1938                         cb->outputcharge = 0;
1939                 } else if (cb->uaiocb.aio_lio_opcode == LIO_READ) {
1940                         td->td_ru.ru_inblock += cb->inputcharge;
1941                         cb->inputcharge = 0;
1942                 }
1943                 aio_free_entry(cb);
1944                 AIO_UNLOCK(ki);
1945                 ops->store_error(uaiocb, error);
1946                 ops->store_status(uaiocb, status);
1947         } else {
1948                 error = EINVAL;
1949                 AIO_UNLOCK(ki);
1950         }
1951         return (error);
1952 }
1953
1954 int
1955 sys_aio_return(struct thread *td, struct aio_return_args *uap)
1956 {
1957
1958         return (kern_aio_return(td, uap->aiocbp, &aiocb_ops));
1959 }
1960
1961 /*
1962  * Allow a process to wakeup when any of the I/O requests are completed.
1963  */
1964 static int
1965 kern_aio_suspend(struct thread *td, int njoblist, struct aiocb **ujoblist,
1966     struct timespec *ts)
1967 {
1968         struct proc *p = td->td_proc;
1969         struct timeval atv;
1970         struct kaioinfo *ki;
1971         struct aiocblist *cb, *cbfirst;
1972         int error, i, timo;
1973
1974         timo = 0;
1975         if (ts) {
1976                 if (ts->tv_nsec < 0 || ts->tv_nsec >= 1000000000)
1977                         return (EINVAL);
1978
1979                 TIMESPEC_TO_TIMEVAL(&atv, ts);
1980                 if (itimerfix(&atv))
1981                         return (EINVAL);
1982                 timo = tvtohz(&atv);
1983         }
1984
1985         ki = p->p_aioinfo;
1986         if (ki == NULL)
1987                 return (EAGAIN);
1988
1989         if (njoblist == 0)
1990                 return (0);
1991
1992         AIO_LOCK(ki);
1993         for (;;) {
1994                 cbfirst = NULL;
1995                 error = 0;
1996                 TAILQ_FOREACH(cb, &ki->kaio_all, allist) {
1997                         for (i = 0; i < njoblist; i++) {
1998                                 if (cb->uuaiocb == ujoblist[i]) {
1999                                         if (cbfirst == NULL)
2000                                                 cbfirst = cb;
2001                                         if (cb->jobstate == JOBST_JOBFINISHED)
2002                                                 goto RETURN;
2003                                 }
2004                         }
2005                 }
2006                 /* All tasks were finished. */
2007                 if (cbfirst == NULL)
2008                         break;
2009
2010                 ki->kaio_flags |= KAIO_WAKEUP;
2011                 error = msleep(&p->p_aioinfo, AIO_MTX(ki), PRIBIO | PCATCH,
2012                     "aiospn", timo);
2013                 if (error == ERESTART)
2014                         error = EINTR;
2015                 if (error)
2016                         break;
2017         }
2018 RETURN:
2019         AIO_UNLOCK(ki);
2020         return (error);
2021 }
2022
2023 int
2024 sys_aio_suspend(struct thread *td, struct aio_suspend_args *uap)
2025 {
2026         struct timespec ts, *tsp;
2027         struct aiocb **ujoblist;
2028         int error;
2029
2030         if (uap->nent < 0 || uap->nent > AIO_LISTIO_MAX)
2031                 return (EINVAL);
2032
2033         if (uap->timeout) {
2034                 /* Get timespec struct. */
2035                 if ((error = copyin(uap->timeout, &ts, sizeof(ts))) != 0)
2036                         return (error);
2037                 tsp = &ts;
2038         } else
2039                 tsp = NULL;
2040
2041         ujoblist = uma_zalloc(aiol_zone, M_WAITOK);
2042         error = copyin(uap->aiocbp, ujoblist, uap->nent * sizeof(ujoblist[0]));
2043         if (error == 0)
2044                 error = kern_aio_suspend(td, uap->nent, ujoblist, tsp);
2045         uma_zfree(aiol_zone, ujoblist);
2046         return (error);
2047 }
2048
2049 /*
2050  * aio_cancel cancels any non-physio aio operations not currently in
2051  * progress.
2052  */
2053 int
2054 sys_aio_cancel(struct thread *td, struct aio_cancel_args *uap)
2055 {
2056         struct proc *p = td->td_proc;
2057         struct kaioinfo *ki;
2058         struct aiocblist *cbe, *cbn;
2059         struct file *fp;
2060         struct socket *so;
2061         int error;
2062         int remove;
2063         int cancelled = 0;
2064         int notcancelled = 0;
2065         struct vnode *vp;
2066
2067         /* Lookup file object. */
2068         error = fget(td, uap->fd, NULL, &fp);
2069         if (error)
2070                 return (error);
2071
2072         ki = p->p_aioinfo;
2073         if (ki == NULL)
2074                 goto done;
2075
2076         if (fp->f_type == DTYPE_VNODE) {
2077                 vp = fp->f_vnode;
2078                 if (vn_isdisk(vp, &error)) {
2079                         fdrop(fp, td);
2080                         td->td_retval[0] = AIO_NOTCANCELED;
2081                         return (0);
2082                 }
2083         }
2084
2085         AIO_LOCK(ki);
2086         TAILQ_FOREACH_SAFE(cbe, &ki->kaio_jobqueue, plist, cbn) {
2087                 if ((uap->fd == cbe->uaiocb.aio_fildes) &&
2088                     ((uap->aiocbp == NULL) ||
2089                      (uap->aiocbp == cbe->uuaiocb))) {
2090                         remove = 0;
2091
2092                         mtx_lock(&aio_job_mtx);
2093                         if (cbe->jobstate == JOBST_JOBQGLOBAL) {
2094                                 TAILQ_REMOVE(&aio_jobs, cbe, list);
2095                                 remove = 1;
2096                         } else if (cbe->jobstate == JOBST_JOBQSOCK) {
2097                                 MPASS(fp->f_type == DTYPE_SOCKET);
2098                                 so = fp->f_data;
2099                                 TAILQ_REMOVE(&so->so_aiojobq, cbe, list);
2100                                 remove = 1;
2101                         } else if (cbe->jobstate == JOBST_JOBQSYNC) {
2102                                 TAILQ_REMOVE(&ki->kaio_syncqueue, cbe, list);
2103                                 remove = 1;
2104                         }
2105                         mtx_unlock(&aio_job_mtx);
2106
2107                         if (remove) {
2108                                 TAILQ_REMOVE(&ki->kaio_jobqueue, cbe, plist);
2109                                 cbe->uaiocb._aiocb_private.status = -1;
2110                                 cbe->uaiocb._aiocb_private.error = ECANCELED;
2111                                 aio_bio_done_notify(p, cbe, DONE_QUEUE);
2112                                 cancelled++;
2113                         } else {
2114                                 notcancelled++;
2115                         }
2116                         if (uap->aiocbp != NULL)
2117                                 break;
2118                 }
2119         }
2120         AIO_UNLOCK(ki);
2121
2122 done:
2123         fdrop(fp, td);
2124
2125         if (uap->aiocbp != NULL) {
2126                 if (cancelled) {
2127                         td->td_retval[0] = AIO_CANCELED;
2128                         return (0);
2129                 }
2130         }
2131
2132         if (notcancelled) {
2133                 td->td_retval[0] = AIO_NOTCANCELED;
2134                 return (0);
2135         }
2136
2137         if (cancelled) {
2138                 td->td_retval[0] = AIO_CANCELED;
2139                 return (0);
2140         }
2141
2142         td->td_retval[0] = AIO_ALLDONE;
2143
2144         return (0);
2145 }
2146
2147 /*
2148  * aio_error is implemented in the kernel level for compatibility purposes
2149  * only.  For a user mode async implementation, it would be best to do it in
2150  * a userland subroutine.
2151  */
2152 static int
2153 kern_aio_error(struct thread *td, struct aiocb *aiocbp, struct aiocb_ops *ops)
2154 {
2155         struct proc *p = td->td_proc;
2156         struct aiocblist *cb;
2157         struct kaioinfo *ki;
2158         int status;
2159
2160         ki = p->p_aioinfo;
2161         if (ki == NULL) {
2162                 td->td_retval[0] = EINVAL;
2163                 return (0);
2164         }
2165
2166         AIO_LOCK(ki);
2167         TAILQ_FOREACH(cb, &ki->kaio_all, allist) {
2168                 if (cb->uuaiocb == aiocbp) {
2169                         if (cb->jobstate == JOBST_JOBFINISHED)
2170                                 td->td_retval[0] =
2171                                         cb->uaiocb._aiocb_private.error;
2172                         else
2173                                 td->td_retval[0] = EINPROGRESS;
2174                         AIO_UNLOCK(ki);
2175                         return (0);
2176                 }
2177         }
2178         AIO_UNLOCK(ki);
2179
2180         /*
2181          * Hack for failure of aio_aqueue.
2182          */
2183         status = ops->fetch_status(aiocbp);
2184         if (status == -1) {
2185                 td->td_retval[0] = ops->fetch_error(aiocbp);
2186                 return (0);
2187         }
2188
2189         td->td_retval[0] = EINVAL;
2190         return (0);
2191 }
2192
2193 int
2194 sys_aio_error(struct thread *td, struct aio_error_args *uap)
2195 {
2196
2197         return (kern_aio_error(td, uap->aiocbp, &aiocb_ops));
2198 }
2199
2200 /* syscall - asynchronous read from a file (REALTIME) */
2201 int
2202 sys_oaio_read(struct thread *td, struct oaio_read_args *uap)
2203 {
2204
2205         return (aio_aqueue(td, (struct aiocb *)uap->aiocbp, NULL, LIO_READ,
2206             &aiocb_ops_osigevent));
2207 }
2208
2209 int
2210 sys_aio_read(struct thread *td, struct aio_read_args *uap)
2211 {
2212
2213         return (aio_aqueue(td, uap->aiocbp, NULL, LIO_READ, &aiocb_ops));
2214 }
2215
2216 /* syscall - asynchronous write to a file (REALTIME) */
2217 int
2218 sys_oaio_write(struct thread *td, struct oaio_write_args *uap)
2219 {
2220
2221         return (aio_aqueue(td, (struct aiocb *)uap->aiocbp, NULL, LIO_WRITE,
2222             &aiocb_ops_osigevent));
2223 }
2224
2225 int
2226 sys_aio_write(struct thread *td, struct aio_write_args *uap)
2227 {
2228
2229         return (aio_aqueue(td, uap->aiocbp, NULL, LIO_WRITE, &aiocb_ops));
2230 }
2231
2232 int
2233 sys_aio_mlock(struct thread *td, struct aio_mlock_args *uap)
2234 {
2235
2236         return (aio_aqueue(td, uap->aiocbp, NULL, LIO_MLOCK, &aiocb_ops));
2237 }
2238
2239 static int
2240 kern_lio_listio(struct thread *td, int mode, struct aiocb * const *uacb_list,
2241     struct aiocb **acb_list, int nent, struct sigevent *sig,
2242     struct aiocb_ops *ops)
2243 {
2244         struct proc *p = td->td_proc;
2245         struct aiocb *iocb;
2246         struct kaioinfo *ki;
2247         struct aioliojob *lj;
2248         struct kevent kev;
2249         int error;
2250         int nerror;
2251         int i;
2252
2253         if ((mode != LIO_NOWAIT) && (mode != LIO_WAIT))
2254                 return (EINVAL);
2255
2256         if (nent < 0 || nent > AIO_LISTIO_MAX)
2257                 return (EINVAL);
2258
2259         if (p->p_aioinfo == NULL)
2260                 aio_init_aioinfo(p);
2261
2262         ki = p->p_aioinfo;
2263
2264         lj = uma_zalloc(aiolio_zone, M_WAITOK);
2265         lj->lioj_flags = 0;
2266         lj->lioj_count = 0;
2267         lj->lioj_finished_count = 0;
2268         knlist_init_mtx(&lj->klist, AIO_MTX(ki));
2269         ksiginfo_init(&lj->lioj_ksi);
2270
2271         /*
2272          * Setup signal.
2273          */
2274         if (sig && (mode == LIO_NOWAIT)) {
2275                 bcopy(sig, &lj->lioj_signal, sizeof(lj->lioj_signal));
2276                 if (lj->lioj_signal.sigev_notify == SIGEV_KEVENT) {
2277                         /* Assume only new style KEVENT */
2278                         kev.filter = EVFILT_LIO;
2279                         kev.flags = EV_ADD | EV_ENABLE | EV_FLAG1;
2280                         kev.ident = (uintptr_t)uacb_list; /* something unique */
2281                         kev.data = (intptr_t)lj;
2282                         /* pass user defined sigval data */
2283                         kev.udata = lj->lioj_signal.sigev_value.sival_ptr;
2284                         error = kqfd_register(
2285                             lj->lioj_signal.sigev_notify_kqueue, &kev, td, 1);
2286                         if (error) {
2287                                 uma_zfree(aiolio_zone, lj);
2288                                 return (error);
2289                         }
2290                 } else if (lj->lioj_signal.sigev_notify == SIGEV_NONE) {
2291                         ;
2292                 } else if (lj->lioj_signal.sigev_notify == SIGEV_SIGNAL ||
2293                            lj->lioj_signal.sigev_notify == SIGEV_THREAD_ID) {
2294                                 if (!_SIG_VALID(lj->lioj_signal.sigev_signo)) {
2295                                         uma_zfree(aiolio_zone, lj);
2296                                         return EINVAL;
2297                                 }
2298                                 lj->lioj_flags |= LIOJ_SIGNAL;
2299                 } else {
2300                         uma_zfree(aiolio_zone, lj);
2301                         return EINVAL;
2302                 }
2303         }
2304
2305         AIO_LOCK(ki);
2306         TAILQ_INSERT_TAIL(&ki->kaio_liojoblist, lj, lioj_list);
2307         /*
2308          * Add extra aiocb count to avoid the lio to be freed
2309          * by other threads doing aio_waitcomplete or aio_return,
2310          * and prevent event from being sent until we have queued
2311          * all tasks.
2312          */
2313         lj->lioj_count = 1;
2314         AIO_UNLOCK(ki);
2315
2316         /*
2317          * Get pointers to the list of I/O requests.
2318          */
2319         nerror = 0;
2320         for (i = 0; i < nent; i++) {
2321                 iocb = acb_list[i];
2322                 if (iocb != NULL) {
2323                         error = aio_aqueue(td, iocb, lj, LIO_NOP, ops);
2324                         if (error != 0)
2325                                 nerror++;
2326                 }
2327         }
2328
2329         error = 0;
2330         AIO_LOCK(ki);
2331         if (mode == LIO_WAIT) {
2332                 while (lj->lioj_count - 1 != lj->lioj_finished_count) {
2333                         ki->kaio_flags |= KAIO_WAKEUP;
2334                         error = msleep(&p->p_aioinfo, AIO_MTX(ki),
2335                             PRIBIO | PCATCH, "aiospn", 0);
2336                         if (error == ERESTART)
2337                                 error = EINTR;
2338                         if (error)
2339                                 break;
2340                 }
2341         } else {
2342                 if (lj->lioj_count - 1 == lj->lioj_finished_count) {
2343                         if (lj->lioj_signal.sigev_notify == SIGEV_KEVENT) {
2344                                 lj->lioj_flags |= LIOJ_KEVENT_POSTED;
2345                                 KNOTE_LOCKED(&lj->klist, 1);
2346                         }
2347                         if ((lj->lioj_flags & (LIOJ_SIGNAL|LIOJ_SIGNAL_POSTED))
2348                             == LIOJ_SIGNAL
2349                             && (lj->lioj_signal.sigev_notify == SIGEV_SIGNAL ||
2350                             lj->lioj_signal.sigev_notify == SIGEV_THREAD_ID)) {
2351                                 aio_sendsig(p, &lj->lioj_signal,
2352                                             &lj->lioj_ksi);
2353                                 lj->lioj_flags |= LIOJ_SIGNAL_POSTED;
2354                         }
2355                 }
2356         }
2357         lj->lioj_count--;
2358         if (lj->lioj_count == 0) {
2359                 TAILQ_REMOVE(&ki->kaio_liojoblist, lj, lioj_list);
2360                 knlist_delete(&lj->klist, curthread, 1);
2361                 PROC_LOCK(p);
2362                 sigqueue_take(&lj->lioj_ksi);
2363                 PROC_UNLOCK(p);
2364                 AIO_UNLOCK(ki);
2365                 uma_zfree(aiolio_zone, lj);
2366         } else
2367                 AIO_UNLOCK(ki);
2368
2369         if (nerror)
2370                 return (EIO);
2371         return (error);
2372 }
2373
2374 /* syscall - list directed I/O (REALTIME) */
2375 int
2376 sys_olio_listio(struct thread *td, struct olio_listio_args *uap)
2377 {
2378         struct aiocb **acb_list;
2379         struct sigevent *sigp, sig;
2380         struct osigevent osig;
2381         int error, nent;
2382
2383         if ((uap->mode != LIO_NOWAIT) && (uap->mode != LIO_WAIT))
2384                 return (EINVAL);
2385
2386         nent = uap->nent;
2387         if (nent < 0 || nent > AIO_LISTIO_MAX)
2388                 return (EINVAL);
2389
2390         if (uap->sig && (uap->mode == LIO_NOWAIT)) {
2391                 error = copyin(uap->sig, &osig, sizeof(osig));
2392                 if (error)
2393                         return (error);
2394                 error = convert_old_sigevent(&osig, &sig);
2395                 if (error)
2396                         return (error);
2397                 sigp = &sig;
2398         } else
2399                 sigp = NULL;
2400
2401         acb_list = malloc(sizeof(struct aiocb *) * nent, M_LIO, M_WAITOK);
2402         error = copyin(uap->acb_list, acb_list, nent * sizeof(acb_list[0]));
2403         if (error == 0)
2404                 error = kern_lio_listio(td, uap->mode,
2405                     (struct aiocb * const *)uap->acb_list, acb_list, nent, sigp,
2406                     &aiocb_ops_osigevent);
2407         free(acb_list, M_LIO);
2408         return (error);
2409 }
2410
2411 /* syscall - list directed I/O (REALTIME) */
2412 int
2413 sys_lio_listio(struct thread *td, struct lio_listio_args *uap)
2414 {
2415         struct aiocb **acb_list;
2416         struct sigevent *sigp, sig;
2417         int error, nent;
2418
2419         if ((uap->mode != LIO_NOWAIT) && (uap->mode != LIO_WAIT))
2420                 return (EINVAL);
2421
2422         nent = uap->nent;
2423         if (nent < 0 || nent > AIO_LISTIO_MAX)
2424                 return (EINVAL);
2425
2426         if (uap->sig && (uap->mode == LIO_NOWAIT)) {
2427                 error = copyin(uap->sig, &sig, sizeof(sig));
2428                 if (error)
2429                         return (error);
2430                 sigp = &sig;
2431         } else
2432                 sigp = NULL;
2433
2434         acb_list = malloc(sizeof(struct aiocb *) * nent, M_LIO, M_WAITOK);
2435         error = copyin(uap->acb_list, acb_list, nent * sizeof(acb_list[0]));
2436         if (error == 0)
2437                 error = kern_lio_listio(td, uap->mode, uap->acb_list, acb_list,
2438                     nent, sigp, &aiocb_ops);
2439         free(acb_list, M_LIO);
2440         return (error);
2441 }
2442
2443 static void
2444 aio_physwakeup(struct bio *bp)
2445 {
2446         struct aiocblist *aiocbe = (struct aiocblist *)bp->bio_caller1;
2447         struct proc *userp;
2448         struct kaioinfo *ki;
2449         int nblks;
2450
2451         /* Release mapping into kernel space. */
2452         if (aiocbe->pbuf) {
2453                 pmap_qremove((vm_offset_t)aiocbe->pbuf->b_data, aiocbe->npages);
2454                 relpbuf(aiocbe->pbuf, NULL);
2455                 aiocbe->pbuf = NULL;
2456                 atomic_subtract_int(&num_buf_aio, 1);
2457         }
2458         vm_page_unhold_pages(aiocbe->pages, aiocbe->npages);
2459
2460         bp = aiocbe->bp;
2461         aiocbe->bp = NULL;
2462         userp = aiocbe->userproc;
2463         ki = userp->p_aioinfo;
2464         AIO_LOCK(ki);
2465         aiocbe->uaiocb._aiocb_private.status -= bp->bio_resid;
2466         aiocbe->uaiocb._aiocb_private.error = 0;
2467         if (bp->bio_flags & BIO_ERROR)
2468                 aiocbe->uaiocb._aiocb_private.error = bp->bio_error;
2469         nblks = btodb(aiocbe->uaiocb.aio_nbytes);
2470         if (aiocbe->uaiocb.aio_lio_opcode == LIO_WRITE)
2471                 aiocbe->outputcharge += nblks;
2472         else
2473                 aiocbe->inputcharge += nblks;
2474         TAILQ_REMOVE(&userp->p_aioinfo->kaio_bufqueue, aiocbe, plist);
2475         ki->kaio_buffer_count--;
2476         aio_bio_done_notify(userp, aiocbe, DONE_BUF);
2477         AIO_UNLOCK(ki);
2478
2479         g_destroy_bio(bp);
2480 }
2481
2482 /* syscall - wait for the next completion of an aio request */
2483 static int
2484 kern_aio_waitcomplete(struct thread *td, struct aiocb **aiocbp,
2485     struct timespec *ts, struct aiocb_ops *ops)
2486 {
2487         struct proc *p = td->td_proc;
2488         struct timeval atv;
2489         struct kaioinfo *ki;
2490         struct aiocblist *cb;
2491         struct aiocb *uuaiocb;
2492         int error, status, timo;
2493
2494         ops->store_aiocb(aiocbp, NULL);
2495
2496         timo = 0;
2497         if (ts) {
2498                 if ((ts->tv_nsec < 0) || (ts->tv_nsec >= 1000000000))
2499                         return (EINVAL);
2500
2501                 TIMESPEC_TO_TIMEVAL(&atv, ts);
2502                 if (itimerfix(&atv))
2503                         return (EINVAL);
2504                 timo = tvtohz(&atv);
2505         }
2506
2507         if (p->p_aioinfo == NULL)
2508                 aio_init_aioinfo(p);
2509         ki = p->p_aioinfo;
2510
2511         error = 0;
2512         cb = NULL;
2513         AIO_LOCK(ki);
2514         while ((cb = TAILQ_FIRST(&ki->kaio_done)) == NULL) {
2515                 ki->kaio_flags |= KAIO_WAKEUP;
2516                 error = msleep(&p->p_aioinfo, AIO_MTX(ki), PRIBIO | PCATCH,
2517                     "aiowc", timo);
2518                 if (timo && error == ERESTART)
2519                         error = EINTR;
2520                 if (error)
2521                         break;
2522         }
2523
2524         if (cb != NULL) {
2525                 MPASS(cb->jobstate == JOBST_JOBFINISHED);
2526                 uuaiocb = cb->uuaiocb;
2527                 status = cb->uaiocb._aiocb_private.status;
2528                 error = cb->uaiocb._aiocb_private.error;
2529                 td->td_retval[0] = status;
2530                 if (cb->uaiocb.aio_lio_opcode == LIO_WRITE) {
2531                         td->td_ru.ru_oublock += cb->outputcharge;
2532                         cb->outputcharge = 0;
2533                 } else if (cb->uaiocb.aio_lio_opcode == LIO_READ) {
2534                         td->td_ru.ru_inblock += cb->inputcharge;
2535                         cb->inputcharge = 0;
2536                 }
2537                 aio_free_entry(cb);
2538                 AIO_UNLOCK(ki);
2539                 ops->store_aiocb(aiocbp, uuaiocb);
2540                 ops->store_error(uuaiocb, error);
2541                 ops->store_status(uuaiocb, status);
2542         } else
2543                 AIO_UNLOCK(ki);
2544
2545         return (error);
2546 }
2547
2548 int
2549 sys_aio_waitcomplete(struct thread *td, struct aio_waitcomplete_args *uap)
2550 {
2551         struct timespec ts, *tsp;
2552         int error;
2553
2554         if (uap->timeout) {
2555                 /* Get timespec struct. */
2556                 error = copyin(uap->timeout, &ts, sizeof(ts));
2557                 if (error)
2558                         return (error);
2559                 tsp = &ts;
2560         } else
2561                 tsp = NULL;
2562
2563         return (kern_aio_waitcomplete(td, uap->aiocbp, tsp, &aiocb_ops));
2564 }
2565
2566 static int
2567 kern_aio_fsync(struct thread *td, int op, struct aiocb *aiocbp,
2568     struct aiocb_ops *ops)
2569 {
2570         struct proc *p = td->td_proc;
2571         struct kaioinfo *ki;
2572
2573         if (op != O_SYNC) /* XXX lack of O_DSYNC */
2574                 return (EINVAL);
2575         ki = p->p_aioinfo;
2576         if (ki == NULL)
2577                 aio_init_aioinfo(p);
2578         return (aio_aqueue(td, aiocbp, NULL, LIO_SYNC, ops));
2579 }
2580
2581 int
2582 sys_aio_fsync(struct thread *td, struct aio_fsync_args *uap)
2583 {
2584
2585         return (kern_aio_fsync(td, uap->op, uap->aiocbp, &aiocb_ops));
2586 }
2587
2588 /* kqueue attach function */
2589 static int
2590 filt_aioattach(struct knote *kn)
2591 {
2592         struct aiocblist *aiocbe = (struct aiocblist *)kn->kn_sdata;
2593
2594         /*
2595          * The aiocbe pointer must be validated before using it, so
2596          * registration is restricted to the kernel; the user cannot
2597          * set EV_FLAG1.
2598          */
2599         if ((kn->kn_flags & EV_FLAG1) == 0)
2600                 return (EPERM);
2601         kn->kn_ptr.p_aio = aiocbe;
2602         kn->kn_flags &= ~EV_FLAG1;
2603
2604         knlist_add(&aiocbe->klist, kn, 0);
2605
2606         return (0);
2607 }
2608
2609 /* kqueue detach function */
2610 static void
2611 filt_aiodetach(struct knote *kn)
2612 {
2613         struct knlist *knl;
2614
2615         knl = &kn->kn_ptr.p_aio->klist;
2616         knl->kl_lock(knl->kl_lockarg);
2617         if (!knlist_empty(knl))
2618                 knlist_remove(knl, kn, 1);
2619         knl->kl_unlock(knl->kl_lockarg);
2620 }
2621
2622 /* kqueue filter function */
2623 /*ARGSUSED*/
2624 static int
2625 filt_aio(struct knote *kn, long hint)
2626 {
2627         struct aiocblist *aiocbe = kn->kn_ptr.p_aio;
2628
2629         kn->kn_data = aiocbe->uaiocb._aiocb_private.error;
2630         if (aiocbe->jobstate != JOBST_JOBFINISHED)
2631                 return (0);
2632         kn->kn_flags |= EV_EOF;
2633         return (1);
2634 }
2635
2636 /* kqueue attach function */
2637 static int
2638 filt_lioattach(struct knote *kn)
2639 {
2640         struct aioliojob * lj = (struct aioliojob *)kn->kn_sdata;
2641
2642         /*
2643          * The aioliojob pointer must be validated before using it, so
2644          * registration is restricted to the kernel; the user cannot
2645          * set EV_FLAG1.
2646          */
2647         if ((kn->kn_flags & EV_FLAG1) == 0)
2648                 return (EPERM);
2649         kn->kn_ptr.p_lio = lj;
2650         kn->kn_flags &= ~EV_FLAG1;
2651
2652         knlist_add(&lj->klist, kn, 0);
2653
2654         return (0);
2655 }
2656
2657 /* kqueue detach function */
2658 static void
2659 filt_liodetach(struct knote *kn)
2660 {
2661         struct knlist *knl;
2662
2663         knl = &kn->kn_ptr.p_lio->klist;
2664         knl->kl_lock(knl->kl_lockarg);
2665         if (!knlist_empty(knl))
2666                 knlist_remove(knl, kn, 1);
2667         knl->kl_unlock(knl->kl_lockarg);
2668 }
2669
2670 /* kqueue filter function */
2671 /*ARGSUSED*/
2672 static int
2673 filt_lio(struct knote *kn, long hint)
2674 {
2675         struct aioliojob * lj = kn->kn_ptr.p_lio;
2676
2677         return (lj->lioj_flags & LIOJ_KEVENT_POSTED);
2678 }
2679
2680 #ifdef COMPAT_FREEBSD32
2681
2682 struct __aiocb_private32 {
2683         int32_t status;
2684         int32_t error;
2685         uint32_t kernelinfo;
2686 };
2687
2688 typedef struct oaiocb32 {
2689         int     aio_fildes;             /* File descriptor */
2690         uint64_t aio_offset __packed;   /* File offset for I/O */
2691         uint32_t aio_buf;               /* I/O buffer in process space */
2692         uint32_t aio_nbytes;            /* Number of bytes for I/O */
2693         struct  osigevent32 aio_sigevent; /* Signal to deliver */
2694         int     aio_lio_opcode;         /* LIO opcode */
2695         int     aio_reqprio;            /* Request priority -- ignored */
2696         struct  __aiocb_private32 _aiocb_private;
2697 } oaiocb32_t;
2698
2699 typedef struct aiocb32 {
2700         int32_t aio_fildes;             /* File descriptor */
2701         uint64_t aio_offset __packed;   /* File offset for I/O */
2702         uint32_t aio_buf;               /* I/O buffer in process space */
2703         uint32_t aio_nbytes;            /* Number of bytes for I/O */
2704         int     __spare__[2];
2705         uint32_t __spare2__;
2706         int     aio_lio_opcode;         /* LIO opcode */
2707         int     aio_reqprio;            /* Request priority -- ignored */
2708         struct __aiocb_private32 _aiocb_private;
2709         struct sigevent32 aio_sigevent; /* Signal to deliver */
2710 } aiocb32_t;
2711
2712 static int
2713 convert_old_sigevent32(struct osigevent32 *osig, struct sigevent *nsig)
2714 {
2715
2716         /*
2717          * Only SIGEV_NONE, SIGEV_SIGNAL, and SIGEV_KEVENT are
2718          * supported by AIO with the old sigevent structure.
2719          */
2720         CP(*osig, *nsig, sigev_notify);
2721         switch (nsig->sigev_notify) {
2722         case SIGEV_NONE:
2723                 break;
2724         case SIGEV_SIGNAL:
2725                 nsig->sigev_signo = osig->__sigev_u.__sigev_signo;
2726                 break;
2727         case SIGEV_KEVENT:
2728                 nsig->sigev_notify_kqueue =
2729                     osig->__sigev_u.__sigev_notify_kqueue;
2730                 PTRIN_CP(*osig, *nsig, sigev_value.sival_ptr);
2731                 break;
2732         default:
2733                 return (EINVAL);
2734         }
2735         return (0);
2736 }
2737
2738 static int
2739 aiocb32_copyin_old_sigevent(struct aiocb *ujob, struct aiocb *kjob)
2740 {
2741         struct oaiocb32 job32;
2742         int error;
2743
2744         bzero(kjob, sizeof(struct aiocb));
2745         error = copyin(ujob, &job32, sizeof(job32));
2746         if (error)
2747                 return (error);
2748
2749         CP(job32, *kjob, aio_fildes);
2750         CP(job32, *kjob, aio_offset);
2751         PTRIN_CP(job32, *kjob, aio_buf);
2752         CP(job32, *kjob, aio_nbytes);
2753         CP(job32, *kjob, aio_lio_opcode);
2754         CP(job32, *kjob, aio_reqprio);
2755         CP(job32, *kjob, _aiocb_private.status);
2756         CP(job32, *kjob, _aiocb_private.error);
2757         PTRIN_CP(job32, *kjob, _aiocb_private.kernelinfo);
2758         return (convert_old_sigevent32(&job32.aio_sigevent,
2759             &kjob->aio_sigevent));
2760 }
2761
2762 static int
2763 aiocb32_copyin(struct aiocb *ujob, struct aiocb *kjob)
2764 {
2765         struct aiocb32 job32;
2766         int error;
2767
2768         error = copyin(ujob, &job32, sizeof(job32));
2769         if (error)
2770                 return (error);
2771         CP(job32, *kjob, aio_fildes);
2772         CP(job32, *kjob, aio_offset);
2773         PTRIN_CP(job32, *kjob, aio_buf);
2774         CP(job32, *kjob, aio_nbytes);
2775         CP(job32, *kjob, aio_lio_opcode);
2776         CP(job32, *kjob, aio_reqprio);
2777         CP(job32, *kjob, _aiocb_private.status);
2778         CP(job32, *kjob, _aiocb_private.error);
2779         PTRIN_CP(job32, *kjob, _aiocb_private.kernelinfo);
2780         return (convert_sigevent32(&job32.aio_sigevent, &kjob->aio_sigevent));
2781 }
2782
2783 static long
2784 aiocb32_fetch_status(struct aiocb *ujob)
2785 {
2786         struct aiocb32 *ujob32;
2787
2788         ujob32 = (struct aiocb32 *)ujob;
2789         return (fuword32(&ujob32->_aiocb_private.status));
2790 }
2791
2792 static long
2793 aiocb32_fetch_error(struct aiocb *ujob)
2794 {
2795         struct aiocb32 *ujob32;
2796
2797         ujob32 = (struct aiocb32 *)ujob;
2798         return (fuword32(&ujob32->_aiocb_private.error));
2799 }
2800
2801 static int
2802 aiocb32_store_status(struct aiocb *ujob, long status)
2803 {
2804         struct aiocb32 *ujob32;
2805
2806         ujob32 = (struct aiocb32 *)ujob;
2807         return (suword32(&ujob32->_aiocb_private.status, status));
2808 }
2809
2810 static int
2811 aiocb32_store_error(struct aiocb *ujob, long error)
2812 {
2813         struct aiocb32 *ujob32;
2814
2815         ujob32 = (struct aiocb32 *)ujob;
2816         return (suword32(&ujob32->_aiocb_private.error, error));
2817 }
2818
2819 static int
2820 aiocb32_store_kernelinfo(struct aiocb *ujob, long jobref)
2821 {
2822         struct aiocb32 *ujob32;
2823
2824         ujob32 = (struct aiocb32 *)ujob;
2825         return (suword32(&ujob32->_aiocb_private.kernelinfo, jobref));
2826 }
2827
2828 static int
2829 aiocb32_store_aiocb(struct aiocb **ujobp, struct aiocb *ujob)
2830 {
2831
2832         return (suword32(ujobp, (long)ujob));
2833 }
2834
2835 static struct aiocb_ops aiocb32_ops = {
2836         .copyin = aiocb32_copyin,
2837         .fetch_status = aiocb32_fetch_status,
2838         .fetch_error = aiocb32_fetch_error,
2839         .store_status = aiocb32_store_status,
2840         .store_error = aiocb32_store_error,
2841         .store_kernelinfo = aiocb32_store_kernelinfo,
2842         .store_aiocb = aiocb32_store_aiocb,
2843 };
2844
2845 static struct aiocb_ops aiocb32_ops_osigevent = {
2846         .copyin = aiocb32_copyin_old_sigevent,
2847         .fetch_status = aiocb32_fetch_status,
2848         .fetch_error = aiocb32_fetch_error,
2849         .store_status = aiocb32_store_status,
2850         .store_error = aiocb32_store_error,
2851         .store_kernelinfo = aiocb32_store_kernelinfo,
2852         .store_aiocb = aiocb32_store_aiocb,
2853 };
2854
2855 int
2856 freebsd32_aio_return(struct thread *td, struct freebsd32_aio_return_args *uap)
2857 {
2858
2859         return (kern_aio_return(td, (struct aiocb *)uap->aiocbp, &aiocb32_ops));
2860 }
2861
2862 int
2863 freebsd32_aio_suspend(struct thread *td, struct freebsd32_aio_suspend_args *uap)
2864 {
2865         struct timespec32 ts32;
2866         struct timespec ts, *tsp;
2867         struct aiocb **ujoblist;
2868         uint32_t *ujoblist32;
2869         int error, i;
2870
2871         if (uap->nent < 0 || uap->nent > AIO_LISTIO_MAX)
2872                 return (EINVAL);
2873
2874         if (uap->timeout) {
2875                 /* Get timespec struct. */
2876                 if ((error = copyin(uap->timeout, &ts32, sizeof(ts32))) != 0)
2877                         return (error);
2878                 CP(ts32, ts, tv_sec);
2879                 CP(ts32, ts, tv_nsec);
2880                 tsp = &ts;
2881         } else
2882                 tsp = NULL;
2883
2884         ujoblist = uma_zalloc(aiol_zone, M_WAITOK);
2885         ujoblist32 = (uint32_t *)ujoblist;
2886         error = copyin(uap->aiocbp, ujoblist32, uap->nent *
2887             sizeof(ujoblist32[0]));
2888         if (error == 0) {
2889                 for (i = uap->nent; i > 0; i--)
2890                         ujoblist[i] = PTRIN(ujoblist32[i]);
2891
2892                 error = kern_aio_suspend(td, uap->nent, ujoblist, tsp);
2893         }
2894         uma_zfree(aiol_zone, ujoblist);
2895         return (error);
2896 }
2897
2898 int
2899 freebsd32_aio_cancel(struct thread *td, struct freebsd32_aio_cancel_args *uap)
2900 {
2901
2902         return (sys_aio_cancel(td, (struct aio_cancel_args *)uap));
2903 }
2904
2905 int
2906 freebsd32_aio_error(struct thread *td, struct freebsd32_aio_error_args *uap)
2907 {
2908
2909         return (kern_aio_error(td, (struct aiocb *)uap->aiocbp, &aiocb32_ops));
2910 }
2911
2912 int
2913 freebsd32_oaio_read(struct thread *td, struct freebsd32_oaio_read_args *uap)
2914 {
2915
2916         return (aio_aqueue(td, (struct aiocb *)uap->aiocbp, NULL, LIO_READ,
2917             &aiocb32_ops_osigevent));
2918 }
2919
2920 int
2921 freebsd32_aio_read(struct thread *td, struct freebsd32_aio_read_args *uap)
2922 {
2923
2924         return (aio_aqueue(td, (struct aiocb *)uap->aiocbp, NULL, LIO_READ,
2925             &aiocb32_ops));
2926 }
2927
2928 int
2929 freebsd32_oaio_write(struct thread *td, struct freebsd32_oaio_write_args *uap)
2930 {
2931
2932         return (aio_aqueue(td, (struct aiocb *)uap->aiocbp, NULL, LIO_WRITE,
2933             &aiocb32_ops_osigevent));
2934 }
2935
2936 int
2937 freebsd32_aio_write(struct thread *td, struct freebsd32_aio_write_args *uap)
2938 {
2939
2940         return (aio_aqueue(td, (struct aiocb *)uap->aiocbp, NULL, LIO_WRITE,
2941             &aiocb32_ops));
2942 }
2943
2944 int
2945 freebsd32_aio_mlock(struct thread *td, struct freebsd32_aio_mlock_args *uap)
2946 {
2947
2948         return (aio_aqueue(td, (struct aiocb *)uap->aiocbp, NULL, LIO_MLOCK,
2949             &aiocb32_ops));
2950 }
2951
2952 int
2953 freebsd32_aio_waitcomplete(struct thread *td,
2954     struct freebsd32_aio_waitcomplete_args *uap)
2955 {
2956         struct timespec32 ts32;
2957         struct timespec ts, *tsp;
2958         int error;
2959
2960         if (uap->timeout) {
2961                 /* Get timespec struct. */
2962                 error = copyin(uap->timeout, &ts32, sizeof(ts32));
2963                 if (error)
2964                         return (error);
2965                 CP(ts32, ts, tv_sec);
2966                 CP(ts32, ts, tv_nsec);
2967                 tsp = &ts;
2968         } else
2969                 tsp = NULL;
2970
2971         return (kern_aio_waitcomplete(td, (struct aiocb **)uap->aiocbp, tsp,
2972             &aiocb32_ops));
2973 }
2974
2975 int
2976 freebsd32_aio_fsync(struct thread *td, struct freebsd32_aio_fsync_args *uap)
2977 {
2978
2979         return (kern_aio_fsync(td, uap->op, (struct aiocb *)uap->aiocbp,
2980             &aiocb32_ops));
2981 }
2982
2983 int
2984 freebsd32_olio_listio(struct thread *td, struct freebsd32_olio_listio_args *uap)
2985 {
2986         struct aiocb **acb_list;
2987         struct sigevent *sigp, sig;
2988         struct osigevent32 osig;
2989         uint32_t *acb_list32;
2990         int error, i, nent;
2991
2992         if ((uap->mode != LIO_NOWAIT) && (uap->mode != LIO_WAIT))
2993                 return (EINVAL);
2994
2995         nent = uap->nent;
2996         if (nent < 0 || nent > AIO_LISTIO_MAX)
2997                 return (EINVAL);
2998
2999         if (uap->sig && (uap->mode == LIO_NOWAIT)) {
3000                 error = copyin(uap->sig, &osig, sizeof(osig));
3001                 if (error)
3002                         return (error);
3003                 error = convert_old_sigevent32(&osig, &sig);
3004                 if (error)
3005                         return (error);
3006                 sigp = &sig;
3007         } else
3008                 sigp = NULL;
3009
3010         acb_list32 = malloc(sizeof(uint32_t) * nent, M_LIO, M_WAITOK);
3011         error = copyin(uap->acb_list, acb_list32, nent * sizeof(uint32_t));
3012         if (error) {
3013                 free(acb_list32, M_LIO);
3014                 return (error);
3015         }
3016         acb_list = malloc(sizeof(struct aiocb *) * nent, M_LIO, M_WAITOK);
3017         for (i = 0; i < nent; i++)
3018                 acb_list[i] = PTRIN(acb_list32[i]);
3019         free(acb_list32, M_LIO);
3020
3021         error = kern_lio_listio(td, uap->mode,
3022             (struct aiocb * const *)uap->acb_list, acb_list, nent, sigp,
3023             &aiocb32_ops_osigevent);
3024         free(acb_list, M_LIO);
3025         return (error);
3026 }
3027
3028 int
3029 freebsd32_lio_listio(struct thread *td, struct freebsd32_lio_listio_args *uap)
3030 {
3031         struct aiocb **acb_list;
3032         struct sigevent *sigp, sig;
3033         struct sigevent32 sig32;
3034         uint32_t *acb_list32;
3035         int error, i, nent;
3036
3037         if ((uap->mode != LIO_NOWAIT) && (uap->mode != LIO_WAIT))
3038                 return (EINVAL);
3039
3040         nent = uap->nent;
3041         if (nent < 0 || nent > AIO_LISTIO_MAX)
3042                 return (EINVAL);
3043
3044         if (uap->sig && (uap->mode == LIO_NOWAIT)) {
3045                 error = copyin(uap->sig, &sig32, sizeof(sig32));
3046                 if (error)
3047                         return (error);
3048                 error = convert_sigevent32(&sig32, &sig);
3049                 if (error)
3050                         return (error);
3051                 sigp = &sig;
3052         } else
3053                 sigp = NULL;
3054
3055         acb_list32 = malloc(sizeof(uint32_t) * nent, M_LIO, M_WAITOK);
3056         error = copyin(uap->acb_list, acb_list32, nent * sizeof(uint32_t));
3057         if (error) {
3058                 free(acb_list32, M_LIO);
3059                 return (error);
3060         }
3061         acb_list = malloc(sizeof(struct aiocb *) * nent, M_LIO, M_WAITOK);
3062         for (i = 0; i < nent; i++)
3063                 acb_list[i] = PTRIN(acb_list32[i]);
3064         free(acb_list32, M_LIO);
3065
3066         error = kern_lio_listio(td, uap->mode,
3067             (struct aiocb * const *)uap->acb_list, acb_list, nent, sigp,
3068             &aiocb32_ops);
3069         free(acb_list, M_LIO);
3070         return (error);
3071 }
3072
3073 #endif