1 /*-
2 * SPDX-License-Identifier: BSD-2-Clause
3 *
4 * Copyright (c) 1997 John S. Dyson. All rights reserved.
5 *
6 * Redistribution and use in source and binary forms, with or without
7 * modification, are permitted provided that the following conditions
8 * are met:
9 * 1. Redistributions of source code must retain the above copyright
10 * notice, this list of conditions and the following disclaimer.
11 * 2. John S. Dyson's name may not be used to endorse or promote products
12 * derived from this software without specific prior written permission.
13 *
14 * DISCLAIMER: This code isn't warranted to do anything useful. Anything
15 * bad that happens because of using this software isn't the responsibility
16 * of the author. This software is distributed AS-IS.
17 */
18
19 /*
20 * This file contains support for the POSIX 1003.1B AIO/LIO facility.
21 */
22
23 #include <sys/param.h>
24 #include <sys/systm.h>
25 #include <sys/malloc.h>
26 #include <sys/bio.h>
27 #include <sys/buf.h>
28 #include <sys/capsicum.h>
29 #include <sys/eventhandler.h>
30 #include <sys/sysproto.h>
31 #include <sys/filedesc.h>
32 #include <sys/kernel.h>
33 #include <sys/module.h>
34 #include <sys/kthread.h>
35 #include <sys/fcntl.h>
36 #include <sys/file.h>
37 #include <sys/limits.h>
38 #include <sys/lock.h>
39 #include <sys/mutex.h>
40 #include <sys/unistd.h>
41 #include <sys/posix4.h>
42 #include <sys/proc.h>
43 #include <sys/resourcevar.h>
44 #include <sys/signalvar.h>
45 #include <sys/syscallsubr.h>
46 #include <sys/protosw.h>
47 #include <sys/rwlock.h>
48 #include <sys/sema.h>
49 #include <sys/socket.h>
50 #include <sys/socketvar.h>
51 #include <sys/syscall.h>
52 #include <sys/sysctl.h>
53 #include <sys/syslog.h>
54 #include <sys/sx.h>
55 #include <sys/taskqueue.h>
56 #include <sys/vnode.h>
57 #include <sys/conf.h>
58 #include <sys/event.h>
59 #include <sys/mount.h>
60 #include <geom/geom.h>
61
62 #include <machine/atomic.h>
63
64 #include <vm/vm.h>
65 #include <vm/vm_page.h>
66 #include <vm/vm_extern.h>
67 #include <vm/pmap.h>
68 #include <vm/vm_map.h>
69 #include <vm/vm_object.h>
70 #include <vm/vnode_pager.h>
71 #include <vm/uma.h>
72 #include <sys/aio.h>
73
74 /*
75 * Counter for aio_fsync.
76 */
77 static uint64_t jobseqno;
78
79 #ifndef MAX_AIO_PER_PROC
80 #define MAX_AIO_PER_PROC 32
81 #endif
82
83 #ifndef MAX_AIO_QUEUE_PER_PROC
84 #define MAX_AIO_QUEUE_PER_PROC 256
85 #endif
86
87 #ifndef MAX_AIO_QUEUE
88 #define MAX_AIO_QUEUE 1024 /* Bigger than MAX_AIO_QUEUE_PER_PROC */
89 #endif
90
91 #ifndef MAX_BUF_AIO
92 #define MAX_BUF_AIO 16
93 #endif
94
95 FEATURE(aio, "Asynchronous I/O");
96 SYSCTL_DECL(_p1003_1b);
97
98 static MALLOC_DEFINE(M_LIO, "lio", "listio aio control block list");
99 static MALLOC_DEFINE(M_AIO, "aio", "structures for asynchronous I/O");
100
101 static SYSCTL_NODE(_vfs, OID_AUTO, aio, CTLFLAG_RW | CTLFLAG_MPSAFE, 0,
102 "Async IO management");
103
104 static int enable_aio_unsafe = 0;
105 SYSCTL_INT(_vfs_aio, OID_AUTO, enable_unsafe, CTLFLAG_RW, &enable_aio_unsafe, 0,
106 "Permit asynchronous IO on all file types, not just known-safe types");
107
108 static unsigned int unsafe_warningcnt = 1;
109 SYSCTL_UINT(_vfs_aio, OID_AUTO, unsafe_warningcnt, CTLFLAG_RW,
110 &unsafe_warningcnt, 0,
111 "Warnings that will be triggered upon failed IO requests on unsafe files");
112
113 static int max_aio_procs = MAX_AIO_PROCS;
114 SYSCTL_INT(_vfs_aio, OID_AUTO, max_aio_procs, CTLFLAG_RW, &max_aio_procs, 0,
115 "Maximum number of kernel processes to use for handling async IO ");
116
117 static int num_aio_procs = 0;
118 SYSCTL_INT(_vfs_aio, OID_AUTO, num_aio_procs, CTLFLAG_RD, &num_aio_procs, 0,
119 "Number of presently active kernel processes for async IO");
120
121 /*
122 * The code will adjust the actual number of AIO processes towards this
123 * number when it gets a chance.
124 */
125 static int target_aio_procs = TARGET_AIO_PROCS;
126 SYSCTL_INT(_vfs_aio, OID_AUTO, target_aio_procs, CTLFLAG_RW, &target_aio_procs,
127 0,
128 "Preferred number of ready kernel processes for async IO");
129
130 static int max_queue_count = MAX_AIO_QUEUE;
131 SYSCTL_INT(_vfs_aio, OID_AUTO, max_aio_queue, CTLFLAG_RW, &max_queue_count, 0,
132 "Maximum number of aio requests to queue, globally");
133
134 static int num_queue_count = 0;
135 SYSCTL_INT(_vfs_aio, OID_AUTO, num_queue_count, CTLFLAG_RD, &num_queue_count, 0,
136 "Number of queued aio requests");
137
138 static int num_buf_aio = 0;
139 SYSCTL_INT(_vfs_aio, OID_AUTO, num_buf_aio, CTLFLAG_RD, &num_buf_aio, 0,
140 "Number of aio requests presently handled by the buf subsystem");
141
142 static int num_unmapped_aio = 0;
143 SYSCTL_INT(_vfs_aio, OID_AUTO, num_unmapped_aio, CTLFLAG_RD, &num_unmapped_aio,
144 0,
145 "Number of aio requests presently handled by unmapped I/O buffers");
146
147 /* Number of async I/O processes in the process of being started */
148 /* XXX This should be local to aio_aqueue() */
149 static int num_aio_resv_start = 0;
150
151 static int aiod_lifetime;
152 SYSCTL_INT(_vfs_aio, OID_AUTO, aiod_lifetime, CTLFLAG_RW, &aiod_lifetime, 0,
153 "Maximum lifetime for idle aiod");
154
155 static int max_aio_per_proc = MAX_AIO_PER_PROC;
156 SYSCTL_INT(_vfs_aio, OID_AUTO, max_aio_per_proc, CTLFLAG_RW, &max_aio_per_proc,
157 0,
158 "Maximum active aio requests per process");
159
160 static int max_aio_queue_per_proc = MAX_AIO_QUEUE_PER_PROC;
161 SYSCTL_INT(_vfs_aio, OID_AUTO, max_aio_queue_per_proc, CTLFLAG_RW,
162 &max_aio_queue_per_proc, 0,
163 "Maximum queued aio requests per process");
164
165 static int max_buf_aio = MAX_BUF_AIO;
166 SYSCTL_INT(_vfs_aio, OID_AUTO, max_buf_aio, CTLFLAG_RW, &max_buf_aio, 0,
167 "Maximum buf aio requests per process");
168
169 /*
170 * Though redundant with vfs.aio.max_aio_queue_per_proc, POSIX requires
171 * sysconf(3) to support AIO_LISTIO_MAX, and we implement that with
172 * vfs.aio.aio_listio_max.
173 */
174 SYSCTL_INT(_p1003_1b, CTL_P1003_1B_AIO_LISTIO_MAX, aio_listio_max,
175 CTLFLAG_RD | CTLFLAG_CAPRD, &max_aio_queue_per_proc,
176 0, "Maximum aio requests for a single lio_listio call");
177
178 #ifdef COMPAT_FREEBSD6
179 typedef struct oaiocb {
180 int aio_fildes; /* File descriptor */
181 off_t aio_offset; /* File offset for I/O */
182 volatile void *aio_buf; /* I/O buffer in process space */
183 size_t aio_nbytes; /* Number of bytes for I/O */
184 struct osigevent aio_sigevent; /* Signal to deliver */
185 int aio_lio_opcode; /* LIO opcode */
186 int aio_reqprio; /* Request priority -- ignored */
187 struct __aiocb_private _aiocb_private;
188 } oaiocb_t;
189 #endif
190
191 /*
192 * Below is a key of locks used to protect each member of struct kaiocb
193 * aioliojob and kaioinfo and any backends.
194 *
195 * * - need not protected
196 * a - locked by kaioinfo lock
197 * b - locked by backend lock, the backend lock can be null in some cases,
198 * for example, BIO belongs to this type, in this case, proc lock is
199 * reused.
200 * c - locked by aio_job_mtx, the lock for the generic file I/O backend.
201 */
202
203 /*
204 * If the routine that services an AIO request blocks while running in an
205 * AIO kernel process it can starve other I/O requests. BIO requests
206 * queued via aio_qbio() complete asynchronously and do not use AIO kernel
207 * processes at all. Socket I/O requests use a separate pool of
208 * kprocs and also force non-blocking I/O. Other file I/O requests
209 * use the generic fo_read/fo_write operations which can block. The
210 * fsync and mlock operations can also block while executing. Ideally
211 * none of these requests would block while executing.
212 *
213 * Note that the service routines cannot toggle O_NONBLOCK in the file
214 * structure directly while handling a request due to races with
215 * userland threads.
216 */
217
218 /* jobflags */
219 #define KAIOCB_QUEUEING 0x01
220 #define KAIOCB_CANCELLED 0x02
221 #define KAIOCB_CANCELLING 0x04
222 #define KAIOCB_CHECKSYNC 0x08
223 #define KAIOCB_CLEARED 0x10
224 #define KAIOCB_FINISHED 0x20
225
226 /* ioflags */
227 #define KAIOCB_IO_FOFFSET 0x01
228
229 /*
230 * AIO process info
231 */
232 #define AIOP_FREE 0x1 /* proc on free queue */
233
234 struct aioproc {
235 int aioprocflags; /* (c) AIO proc flags */
236 TAILQ_ENTRY(aioproc) list; /* (c) list of processes */
237 struct proc *aioproc; /* (*) the AIO proc */
238 };
239
240 /*
241 * data-structure for lio signal management
242 */
243 struct aioliojob {
244 int lioj_flags; /* (a) listio flags */
245 int lioj_count; /* (a) count of jobs */
246 int lioj_finished_count; /* (a) count of finished jobs */
247 struct sigevent lioj_signal; /* (a) signal on all I/O done */
248 TAILQ_ENTRY(aioliojob) lioj_list; /* (a) lio list */
249 struct knlist klist; /* (a) list of knotes */
250 ksiginfo_t lioj_ksi; /* (a) Realtime signal info */
251 };
252
253 #define LIOJ_SIGNAL 0x1 /* signal on all done (lio) */
254 #define LIOJ_SIGNAL_POSTED 0x2 /* signal has been posted */
255 #define LIOJ_KEVENT_POSTED 0x4 /* kevent triggered */
256
257 /*
258 * per process aio data structure
259 */
260 struct kaioinfo {
261 struct mtx kaio_mtx; /* the lock to protect this struct */
262 int kaio_flags; /* (a) per process kaio flags */
263 int kaio_active_count; /* (c) number of currently used AIOs */
264 int kaio_count; /* (a) size of AIO queue */
265 int kaio_buffer_count; /* (a) number of bio buffers */
266 TAILQ_HEAD(,kaiocb) kaio_all; /* (a) all AIOs in a process */
267 TAILQ_HEAD(,kaiocb) kaio_done; /* (a) done queue for process */
268 TAILQ_HEAD(,aioliojob) kaio_liojoblist; /* (a) list of lio jobs */
269 TAILQ_HEAD(,kaiocb) kaio_jobqueue; /* (a) job queue for process */
270 TAILQ_HEAD(,kaiocb) kaio_syncqueue; /* (a) queue for aio_fsync */
271 TAILQ_HEAD(,kaiocb) kaio_syncready; /* (a) second q for aio_fsync */
272 struct task kaio_task; /* (*) task to kick aio processes */
273 struct task kaio_sync_task; /* (*) task to schedule fsync jobs */
274 };
275
276 #define AIO_LOCK(ki) mtx_lock(&(ki)->kaio_mtx)
277 #define AIO_UNLOCK(ki) mtx_unlock(&(ki)->kaio_mtx)
278 #define AIO_LOCK_ASSERT(ki, f) mtx_assert(&(ki)->kaio_mtx, (f))
279 #define AIO_MTX(ki) (&(ki)->kaio_mtx)
280
281 #define KAIO_RUNDOWN 0x1 /* process is being run down */
282 #define KAIO_WAKEUP 0x2 /* wakeup process when AIO completes */
283
284 /*
285 * Operations used to interact with userland aio control blocks.
286 * Different ABIs provide their own operations.
287 */
288 struct aiocb_ops {
289 int (*aio_copyin)(struct aiocb *ujob, struct kaiocb *kjob, int ty);
290 long (*fetch_status)(struct aiocb *ujob);
291 long (*fetch_error)(struct aiocb *ujob);
292 int (*store_status)(struct aiocb *ujob, long status);
293 int (*store_error)(struct aiocb *ujob, long error);
294 int (*store_aiocb)(struct aiocb **ujobp, struct aiocb *ujob);
295 };
296
297 static TAILQ_HEAD(,aioproc) aio_freeproc; /* (c) Idle daemons */
298 static struct sema aio_newproc_sem;
299 static struct mtx aio_job_mtx;
300 static TAILQ_HEAD(,kaiocb) aio_jobs; /* (c) Async job list */
301 static struct unrhdr *aiod_unr;
302
303 static void aio_biocleanup(struct bio *bp);
304 void aio_init_aioinfo(struct proc *p);
305 static int aio_onceonly(void);
306 static int aio_free_entry(struct kaiocb *job);
307 static void aio_process_rw(struct kaiocb *job);
308 static void aio_process_sync(struct kaiocb *job);
309 static void aio_process_mlock(struct kaiocb *job);
310 static void aio_schedule_fsync(void *context, int pending);
311 static int aio_newproc(int *);
312 int aio_aqueue(struct thread *td, struct aiocb *ujob,
313 struct aioliojob *lio, int type, struct aiocb_ops *ops);
314 static int aio_queue_file(struct file *fp, struct kaiocb *job);
315 static void aio_biowakeup(struct bio *bp);
316 static void aio_proc_rundown(void *arg, struct proc *p);
317 static void aio_proc_rundown_exec(void *arg, struct proc *p,
318 struct image_params *imgp);
319 static int aio_qbio(struct proc *p, struct kaiocb *job);
320 static void aio_daemon(void *param);
321 static void aio_bio_done_notify(struct proc *userp, struct kaiocb *job);
322 static bool aio_clear_cancel_function_locked(struct kaiocb *job);
323 static int aio_kick(struct proc *userp);
324 static void aio_kick_nowait(struct proc *userp);
325 static void aio_kick_helper(void *context, int pending);
326 static int filt_aioattach(struct knote *kn);
327 static void filt_aiodetach(struct knote *kn);
328 static int filt_aio(struct knote *kn, long hint);
329 static int filt_lioattach(struct knote *kn);
330 static void filt_liodetach(struct knote *kn);
331 static int filt_lio(struct knote *kn, long hint);
332
333 /*
334 * Zones for:
335 * kaio Per process async io info
336 * aiocb async io jobs
337 * aiolio list io jobs
338 */
339 static uma_zone_t kaio_zone, aiocb_zone, aiolio_zone;
340
341 /* kqueue filters for aio */
342 static const struct filterops aio_filtops = {
343 .f_isfd = 0,
344 .f_attach = filt_aioattach,
345 .f_detach = filt_aiodetach,
346 .f_event = filt_aio,
347 };
348 static const struct filterops lio_filtops = {
349 .f_isfd = 0,
350 .f_attach = filt_lioattach,
351 .f_detach = filt_liodetach,
352 .f_event = filt_lio
353 };
354
355 static eventhandler_tag exit_tag, exec_tag;
356
357 TASKQUEUE_DEFINE_THREAD(aiod_kick);
358
359 /*
360 * Main operations function for use as a kernel module.
361 */
362 static int
aio_modload(struct module * module,int cmd,void * arg)363 aio_modload(struct module *module, int cmd, void *arg)
364 {
365 int error = 0;
366
367 switch (cmd) {
368 case MOD_LOAD:
369 aio_onceonly();
370 break;
371 case MOD_SHUTDOWN:
372 break;
373 default:
374 error = EOPNOTSUPP;
375 break;
376 }
377 return (error);
378 }
379
380 static moduledata_t aio_mod = {
381 "aio",
382 &aio_modload,
383 NULL
384 };
385
386 DECLARE_MODULE(aio, aio_mod, SI_SUB_VFS, SI_ORDER_ANY);
387 MODULE_VERSION(aio, 1);
388
389 /*
390 * Startup initialization
391 */
392 static int
aio_onceonly(void)393 aio_onceonly(void)
394 {
395
396 exit_tag = EVENTHANDLER_REGISTER(process_exit, aio_proc_rundown, NULL,
397 EVENTHANDLER_PRI_ANY);
398 exec_tag = EVENTHANDLER_REGISTER(process_exec, aio_proc_rundown_exec,
399 NULL, EVENTHANDLER_PRI_ANY);
400 kqueue_add_filteropts(EVFILT_AIO, &aio_filtops);
401 kqueue_add_filteropts(EVFILT_LIO, &lio_filtops);
402 TAILQ_INIT(&aio_freeproc);
403 sema_init(&aio_newproc_sem, 0, "aio_new_proc");
404 mtx_init(&aio_job_mtx, "aio_job", NULL, MTX_DEF);
405 TAILQ_INIT(&aio_jobs);
406 aiod_unr = new_unrhdr(1, INT_MAX, NULL);
407 kaio_zone = uma_zcreate("AIO", sizeof(struct kaioinfo), NULL, NULL,
408 NULL, NULL, UMA_ALIGN_PTR, 0);
409 aiocb_zone = uma_zcreate("AIOCB", sizeof(struct kaiocb), NULL, NULL,
410 NULL, NULL, UMA_ALIGN_PTR, 0);
411 aiolio_zone = uma_zcreate("AIOLIO", sizeof(struct aioliojob), NULL,
412 NULL, NULL, NULL, UMA_ALIGN_PTR, 0);
413 aiod_lifetime = AIOD_LIFETIME_DEFAULT;
414 p31b_setcfg(CTL_P1003_1B_ASYNCHRONOUS_IO, _POSIX_ASYNCHRONOUS_IO);
415 p31b_setcfg(CTL_P1003_1B_AIO_MAX, MAX_AIO_QUEUE);
416 p31b_setcfg(CTL_P1003_1B_AIO_PRIO_DELTA_MAX, 0);
417
418 return (0);
419 }
420
421 /*
422 * Init the per-process aioinfo structure. The aioinfo limits are set
423 * per-process for user limit (resource) management.
424 */
425 void
aio_init_aioinfo(struct proc * p)426 aio_init_aioinfo(struct proc *p)
427 {
428 struct kaioinfo *ki;
429
430 ki = uma_zalloc(kaio_zone, M_WAITOK);
431 mtx_init(&ki->kaio_mtx, "aiomtx", NULL, MTX_DEF | MTX_NEW);
432 ki->kaio_flags = 0;
433 ki->kaio_active_count = 0;
434 ki->kaio_count = 0;
435 ki->kaio_buffer_count = 0;
436 TAILQ_INIT(&ki->kaio_all);
437 TAILQ_INIT(&ki->kaio_done);
438 TAILQ_INIT(&ki->kaio_jobqueue);
439 TAILQ_INIT(&ki->kaio_liojoblist);
440 TAILQ_INIT(&ki->kaio_syncqueue);
441 TAILQ_INIT(&ki->kaio_syncready);
442 TASK_INIT(&ki->kaio_task, 0, aio_kick_helper, p);
443 TASK_INIT(&ki->kaio_sync_task, 0, aio_schedule_fsync, ki);
444 PROC_LOCK(p);
445 if (p->p_aioinfo == NULL) {
446 p->p_aioinfo = ki;
447 PROC_UNLOCK(p);
448 } else {
449 PROC_UNLOCK(p);
450 mtx_destroy(&ki->kaio_mtx);
451 uma_zfree(kaio_zone, ki);
452 }
453
454 while (num_aio_procs < MIN(target_aio_procs, max_aio_procs))
455 aio_newproc(NULL);
456 }
457
458 static int
aio_sendsig(struct proc * p,struct sigevent * sigev,ksiginfo_t * ksi,bool ext)459 aio_sendsig(struct proc *p, struct sigevent *sigev, ksiginfo_t *ksi, bool ext)
460 {
461 struct thread *td;
462 int error;
463
464 error = sigev_findtd(p, sigev, &td);
465 if (error)
466 return (error);
467 if (!KSI_ONQ(ksi)) {
468 ksiginfo_set_sigev(ksi, sigev);
469 ksi->ksi_code = SI_ASYNCIO;
470 ksi->ksi_flags |= ext ? (KSI_EXT | KSI_INS) : 0;
471 tdsendsignal(p, td, ksi->ksi_signo, ksi);
472 }
473 PROC_UNLOCK(p);
474 return (error);
475 }
476
477 /*
478 * Free a job entry. Wait for completion if it is currently active, but don't
479 * delay forever. If we delay, we return a flag that says that we have to
480 * restart the queue scan.
481 */
482 static int
aio_free_entry(struct kaiocb * job)483 aio_free_entry(struct kaiocb *job)
484 {
485 struct kaioinfo *ki;
486 struct aioliojob *lj;
487 struct proc *p;
488
489 p = job->userproc;
490 MPASS(curproc == p);
491 ki = p->p_aioinfo;
492 MPASS(ki != NULL);
493
494 AIO_LOCK_ASSERT(ki, MA_OWNED);
495 MPASS(job->jobflags & KAIOCB_FINISHED);
496
497 atomic_subtract_int(&num_queue_count, 1);
498
499 ki->kaio_count--;
500 MPASS(ki->kaio_count >= 0);
501
502 TAILQ_REMOVE(&ki->kaio_done, job, plist);
503 TAILQ_REMOVE(&ki->kaio_all, job, allist);
504
505 lj = job->lio;
506 if (lj) {
507 lj->lioj_count--;
508 lj->lioj_finished_count--;
509
510 if (lj->lioj_count == 0) {
511 TAILQ_REMOVE(&ki->kaio_liojoblist, lj, lioj_list);
512 /* lio is going away, we need to destroy any knotes */
513 knlist_delete(&lj->klist, curthread, 1);
514 PROC_LOCK(p);
515 sigqueue_take(&lj->lioj_ksi);
516 PROC_UNLOCK(p);
517 uma_zfree(aiolio_zone, lj);
518 }
519 }
520
521 /* job is going away, we need to destroy any knotes */
522 knlist_delete(&job->klist, curthread, 1);
523 PROC_LOCK(p);
524 sigqueue_take(&job->ksi);
525 PROC_UNLOCK(p);
526
527 AIO_UNLOCK(ki);
528
529 /*
530 * The thread argument here is used to find the owning process
531 * and is also passed to fo_close() which may pass it to various
532 * places such as devsw close() routines. Because of that, we
533 * need a thread pointer from the process owning the job that is
534 * persistent and won't disappear out from under us or move to
535 * another process.
536 *
537 * Currently, all the callers of this function call it to remove
538 * a kaiocb from the current process' job list either via a
539 * syscall or due to the current process calling exit() or
540 * execve(). Thus, we know that p == curproc. We also know that
541 * curthread can't exit since we are curthread.
542 *
543 * Therefore, we use curthread as the thread to pass to
544 * knlist_delete(). This does mean that it is possible for the
545 * thread pointer at close time to differ from the thread pointer
546 * at open time, but this is already true of file descriptors in
547 * a multithreaded process.
548 */
549 if (job->fd_file)
550 fdrop(job->fd_file, curthread);
551 crfree(job->cred);
552 if (job->uiop != &job->uio)
553 freeuio(job->uiop);
554 uma_zfree(aiocb_zone, job);
555 AIO_LOCK(ki);
556
557 return (0);
558 }
559
560 static void
aio_proc_rundown_exec(void * arg,struct proc * p,struct image_params * imgp __unused)561 aio_proc_rundown_exec(void *arg, struct proc *p,
562 struct image_params *imgp __unused)
563 {
564 aio_proc_rundown(arg, p);
565 }
566
567 static int
aio_cancel_job(struct proc * p,struct kaioinfo * ki,struct kaiocb * job)568 aio_cancel_job(struct proc *p, struct kaioinfo *ki, struct kaiocb *job)
569 {
570 aio_cancel_fn_t *func;
571 int cancelled;
572
573 AIO_LOCK_ASSERT(ki, MA_OWNED);
574 if (job->jobflags & (KAIOCB_CANCELLED | KAIOCB_FINISHED))
575 return (0);
576 MPASS((job->jobflags & KAIOCB_CANCELLING) == 0);
577 job->jobflags |= KAIOCB_CANCELLED;
578
579 func = job->cancel_fn;
580
581 /*
582 * If there is no cancel routine, just leave the job marked as
583 * cancelled. The job should be in active use by a caller who
584 * should complete it normally or when it fails to install a
585 * cancel routine.
586 */
587 if (func == NULL)
588 return (0);
589
590 /*
591 * Set the CANCELLING flag so that aio_complete() will defer
592 * completions of this job. This prevents the job from being
593 * freed out from under the cancel callback. After the
594 * callback any deferred completion (whether from the callback
595 * or any other source) will be completed.
596 */
597 job->jobflags |= KAIOCB_CANCELLING;
598 AIO_UNLOCK(ki);
599 func(job);
600 AIO_LOCK(ki);
601 job->jobflags &= ~KAIOCB_CANCELLING;
602 if (job->jobflags & KAIOCB_FINISHED) {
603 cancelled = job->uaiocb._aiocb_private.error == ECANCELED;
604 TAILQ_REMOVE(&ki->kaio_jobqueue, job, plist);
605 aio_bio_done_notify(p, job);
606 } else {
607 /*
608 * The cancel callback might have scheduled an
609 * operation to cancel this request, but it is
610 * only counted as cancelled if the request is
611 * cancelled when the callback returns.
612 */
613 cancelled = 0;
614 }
615 return (cancelled);
616 }
617
618 /*
619 * Rundown the jobs for a given process.
620 */
621 static void
aio_proc_rundown(void * arg,struct proc * p)622 aio_proc_rundown(void *arg, struct proc *p)
623 {
624 struct kaioinfo *ki;
625 struct aioliojob *lj;
626 struct kaiocb *job, *jobn;
627
628 KASSERT(curthread->td_proc == p,
629 ("%s: called on non-curproc", __func__));
630 ki = p->p_aioinfo;
631 if (ki == NULL)
632 return;
633
634 AIO_LOCK(ki);
635 ki->kaio_flags |= KAIO_RUNDOWN;
636
637 restart:
638
639 /*
640 * Try to cancel all pending requests. This code simulates
641 * aio_cancel on all pending I/O requests.
642 */
643 TAILQ_FOREACH_SAFE(job, &ki->kaio_jobqueue, plist, jobn) {
644 aio_cancel_job(p, ki, job);
645 }
646
647 /* Wait for all running I/O to be finished */
648 if (TAILQ_FIRST(&ki->kaio_jobqueue) || ki->kaio_active_count != 0) {
649 ki->kaio_flags |= KAIO_WAKEUP;
650 msleep(&p->p_aioinfo, AIO_MTX(ki), PRIBIO, "aioprn", hz);
651 goto restart;
652 }
653
654 /* Free all completed I/O requests. */
655 while ((job = TAILQ_FIRST(&ki->kaio_done)) != NULL)
656 aio_free_entry(job);
657
658 while ((lj = TAILQ_FIRST(&ki->kaio_liojoblist)) != NULL) {
659 if (lj->lioj_count == 0) {
660 TAILQ_REMOVE(&ki->kaio_liojoblist, lj, lioj_list);
661 knlist_delete(&lj->klist, curthread, 1);
662 PROC_LOCK(p);
663 sigqueue_take(&lj->lioj_ksi);
664 PROC_UNLOCK(p);
665 uma_zfree(aiolio_zone, lj);
666 } else {
667 panic("LIO job not cleaned up: C:%d, FC:%d\n",
668 lj->lioj_count, lj->lioj_finished_count);
669 }
670 }
671 AIO_UNLOCK(ki);
672 taskqueue_drain(taskqueue_aiod_kick, &ki->kaio_task);
673 taskqueue_drain(taskqueue_aiod_kick, &ki->kaio_sync_task);
674 mtx_destroy(&ki->kaio_mtx);
675 uma_zfree(kaio_zone, ki);
676 p->p_aioinfo = NULL;
677 }
678
679 /*
680 * Select a job to run (called by an AIO daemon).
681 */
682 static struct kaiocb *
aio_selectjob(struct aioproc * aiop)683 aio_selectjob(struct aioproc *aiop)
684 {
685 struct kaiocb *job;
686 struct kaioinfo *ki;
687 struct proc *userp;
688
689 mtx_assert(&aio_job_mtx, MA_OWNED);
690 restart:
691 TAILQ_FOREACH(job, &aio_jobs, list) {
692 userp = job->userproc;
693 ki = userp->p_aioinfo;
694
695 if (ki->kaio_active_count < max_aio_per_proc) {
696 TAILQ_REMOVE(&aio_jobs, job, list);
697 if (!aio_clear_cancel_function(job))
698 goto restart;
699
700 /* Account for currently active jobs. */
701 ki->kaio_active_count++;
702 break;
703 }
704 }
705 return (job);
706 }
707
708 /*
709 * Move all data to a permanent storage device. This code
710 * simulates the fsync and fdatasync syscalls.
711 */
712 static int
aio_fsync_vnode(struct thread * td,struct vnode * vp,int op)713 aio_fsync_vnode(struct thread *td, struct vnode *vp, int op)
714 {
715 struct mount *mp;
716 int error;
717
718 for (;;) {
719 error = vn_start_write(vp, &mp, V_WAIT | V_PCATCH);
720 if (error != 0)
721 break;
722 vn_lock(vp, LK_EXCLUSIVE | LK_RETRY);
723 vnode_pager_clean_async(vp);
724 if (op == LIO_DSYNC)
725 error = VOP_FDATASYNC(vp, td);
726 else
727 error = VOP_FSYNC(vp, MNT_WAIT, td);
728
729 VOP_UNLOCK(vp);
730 vn_finished_write(mp);
731 if (error != ERELOOKUP)
732 break;
733 }
734 return (error);
735 }
736
737 /*
738 * The AIO processing activity for LIO_READ/LIO_WRITE. This is the code that
739 * does the I/O request for the non-bio version of the operations. The normal
740 * vn operations are used, and this code should work in all instances for every
741 * type of file, including pipes, sockets, fifos, and regular files.
742 *
743 * XXX I don't think it works well for socket, pipe, and fifo.
744 */
745 static void
aio_process_rw(struct kaiocb * job)746 aio_process_rw(struct kaiocb *job)
747 {
748 struct ucred *td_savedcred;
749 struct thread *td;
750 struct file *fp;
751 ssize_t cnt;
752 long msgsnd_st, msgsnd_end;
753 long msgrcv_st, msgrcv_end;
754 long oublock_st, oublock_end;
755 long inblock_st, inblock_end;
756 int error, opcode;
757
758 opcode = job->uaiocb.aio_lio_opcode & ~LIO_FOFFSET;
759 KASSERT(opcode == LIO_READ || opcode == LIO_READV ||
760 opcode == LIO_WRITE || opcode == LIO_WRITEV,
761 ("%s: opcode %d", __func__, job->uaiocb.aio_lio_opcode));
762
763 aio_switch_vmspace(job);
764 td = curthread;
765 td_savedcred = td->td_ucred;
766 td->td_ucred = job->cred;
767 job->uiop->uio_td = td;
768 fp = job->fd_file;
769
770 cnt = job->uiop->uio_resid;
771
772 msgrcv_st = td->td_ru.ru_msgrcv;
773 msgsnd_st = td->td_ru.ru_msgsnd;
774 inblock_st = td->td_ru.ru_inblock;
775 oublock_st = td->td_ru.ru_oublock;
776
777 /*
778 * aio_aqueue() acquires a reference to the file that is
779 * released in aio_free_entry().
780 */
781 if (opcode == LIO_READ || opcode == LIO_READV) {
782 if (job->uiop->uio_resid == 0)
783 error = 0;
784 else
785 error = fo_read(fp, job->uiop, fp->f_cred,
786 (job->ioflags & KAIOCB_IO_FOFFSET) != 0 ? 0 :
787 FOF_OFFSET, td);
788 } else {
789 if (fp->f_type == DTYPE_VNODE)
790 bwillwrite();
791 error = fo_write(fp, job->uiop, fp->f_cred, (job->ioflags &
792 KAIOCB_IO_FOFFSET) != 0 ? 0 : FOF_OFFSET, td);
793 }
794 msgrcv_end = td->td_ru.ru_msgrcv;
795 msgsnd_end = td->td_ru.ru_msgsnd;
796 inblock_end = td->td_ru.ru_inblock;
797 oublock_end = td->td_ru.ru_oublock;
798
799 job->msgrcv = msgrcv_end - msgrcv_st;
800 job->msgsnd = msgsnd_end - msgsnd_st;
801 job->inblock = inblock_end - inblock_st;
802 job->outblock = oublock_end - oublock_st;
803
804 if (error != 0 && job->uiop->uio_resid != cnt) {
805 if (error == ERESTART || error == EINTR || error == EWOULDBLOCK)
806 error = 0;
807 if (error == EPIPE && (opcode & LIO_WRITE)) {
808 PROC_LOCK(job->userproc);
809 kern_psignal(job->userproc, SIGPIPE);
810 PROC_UNLOCK(job->userproc);
811 }
812 }
813
814 cnt -= job->uiop->uio_resid;
815 td->td_ucred = td_savedcred;
816 if (error)
817 aio_complete(job, -1, error);
818 else
819 aio_complete(job, cnt, 0);
820 }
821
822 static void
aio_process_sync(struct kaiocb * job)823 aio_process_sync(struct kaiocb *job)
824 {
825 struct thread *td = curthread;
826 struct ucred *td_savedcred = td->td_ucred;
827 struct file *fp = job->fd_file;
828 int error = 0;
829
830 KASSERT(job->uaiocb.aio_lio_opcode & LIO_SYNC,
831 ("%s: opcode %d", __func__, job->uaiocb.aio_lio_opcode));
832
833 td->td_ucred = job->cred;
834 if (fp->f_vnode != NULL) {
835 error = aio_fsync_vnode(td, fp->f_vnode,
836 job->uaiocb.aio_lio_opcode);
837 }
838 td->td_ucred = td_savedcred;
839 if (error)
840 aio_complete(job, -1, error);
841 else
842 aio_complete(job, 0, 0);
843 }
844
845 static void
aio_process_mlock(struct kaiocb * job)846 aio_process_mlock(struct kaiocb *job)
847 {
848 struct aiocb *cb = &job->uaiocb;
849 int error;
850
851 KASSERT(job->uaiocb.aio_lio_opcode == LIO_MLOCK,
852 ("%s: opcode %d", __func__, job->uaiocb.aio_lio_opcode));
853
854 aio_switch_vmspace(job);
855 error = kern_mlock(job->userproc, job->cred,
856 __DEVOLATILE(uintptr_t, cb->aio_buf), cb->aio_nbytes);
857 aio_complete(job, error != 0 ? -1 : 0, error);
858 }
859
860 static void
aio_bio_done_notify(struct proc * userp,struct kaiocb * job)861 aio_bio_done_notify(struct proc *userp, struct kaiocb *job)
862 {
863 struct aioliojob *lj;
864 struct kaioinfo *ki;
865 struct kaiocb *sjob, *sjobn;
866 int lj_done;
867 bool schedule_fsync;
868
869 ki = userp->p_aioinfo;
870 AIO_LOCK_ASSERT(ki, MA_OWNED);
871 lj = job->lio;
872 lj_done = 0;
873 if (lj) {
874 lj->lioj_finished_count++;
875 if (lj->lioj_count == lj->lioj_finished_count)
876 lj_done = 1;
877 }
878 TAILQ_INSERT_TAIL(&ki->kaio_done, job, plist);
879 MPASS(job->jobflags & KAIOCB_FINISHED);
880
881 if (ki->kaio_flags & KAIO_RUNDOWN)
882 goto notification_done;
883
884 if (job->uaiocb.aio_sigevent.sigev_notify == SIGEV_SIGNAL ||
885 job->uaiocb.aio_sigevent.sigev_notify == SIGEV_THREAD_ID)
886 aio_sendsig(userp, &job->uaiocb.aio_sigevent, &job->ksi, true);
887
888 KNOTE_LOCKED(&job->klist, 1);
889
890 if (lj_done) {
891 if (lj->lioj_signal.sigev_notify == SIGEV_KEVENT) {
892 lj->lioj_flags |= LIOJ_KEVENT_POSTED;
893 KNOTE_LOCKED(&lj->klist, 1);
894 }
895 if ((lj->lioj_flags & (LIOJ_SIGNAL | LIOJ_SIGNAL_POSTED))
896 == LIOJ_SIGNAL &&
897 (lj->lioj_signal.sigev_notify == SIGEV_SIGNAL ||
898 lj->lioj_signal.sigev_notify == SIGEV_THREAD_ID)) {
899 aio_sendsig(userp, &lj->lioj_signal, &lj->lioj_ksi,
900 true);
901 lj->lioj_flags |= LIOJ_SIGNAL_POSTED;
902 }
903 }
904
905 notification_done:
906 if (job->jobflags & KAIOCB_CHECKSYNC) {
907 schedule_fsync = false;
908 TAILQ_FOREACH_SAFE(sjob, &ki->kaio_syncqueue, list, sjobn) {
909 if (job->fd_file != sjob->fd_file ||
910 job->seqno >= sjob->seqno)
911 continue;
912 if (--sjob->pending > 0)
913 continue;
914 TAILQ_REMOVE(&ki->kaio_syncqueue, sjob, list);
915 if (!aio_clear_cancel_function_locked(sjob))
916 continue;
917 TAILQ_INSERT_TAIL(&ki->kaio_syncready, sjob, list);
918 schedule_fsync = true;
919 }
920 if (schedule_fsync)
921 taskqueue_enqueue(taskqueue_aiod_kick,
922 &ki->kaio_sync_task);
923 }
924 if (ki->kaio_flags & KAIO_WAKEUP) {
925 ki->kaio_flags &= ~KAIO_WAKEUP;
926 wakeup(&userp->p_aioinfo);
927 }
928 }
929
930 static void
aio_schedule_fsync(void * context,int pending)931 aio_schedule_fsync(void *context, int pending)
932 {
933 struct kaioinfo *ki;
934 struct kaiocb *job;
935
936 ki = context;
937 AIO_LOCK(ki);
938 while (!TAILQ_EMPTY(&ki->kaio_syncready)) {
939 job = TAILQ_FIRST(&ki->kaio_syncready);
940 TAILQ_REMOVE(&ki->kaio_syncready, job, list);
941 AIO_UNLOCK(ki);
942 aio_schedule(job, aio_process_sync);
943 AIO_LOCK(ki);
944 }
945 AIO_UNLOCK(ki);
946 }
947
948 bool
aio_cancel_cleared(struct kaiocb * job)949 aio_cancel_cleared(struct kaiocb *job)
950 {
951
952 /*
953 * The caller should hold the same queue lock held when
954 * aio_clear_cancel_function() was called and set this flag
955 * ensuring this check sees an up-to-date value. However,
956 * there is no way to assert that.
957 */
958 return ((job->jobflags & KAIOCB_CLEARED) != 0);
959 }
960
961 static bool
aio_clear_cancel_function_locked(struct kaiocb * job)962 aio_clear_cancel_function_locked(struct kaiocb *job)
963 {
964
965 AIO_LOCK_ASSERT(job->userproc->p_aioinfo, MA_OWNED);
966 MPASS(job->cancel_fn != NULL);
967 if (job->jobflags & KAIOCB_CANCELLING) {
968 job->jobflags |= KAIOCB_CLEARED;
969 return (false);
970 }
971 job->cancel_fn = NULL;
972 return (true);
973 }
974
975 bool
aio_clear_cancel_function(struct kaiocb * job)976 aio_clear_cancel_function(struct kaiocb *job)
977 {
978 struct kaioinfo *ki;
979 bool ret;
980
981 ki = job->userproc->p_aioinfo;
982 AIO_LOCK(ki);
983 ret = aio_clear_cancel_function_locked(job);
984 AIO_UNLOCK(ki);
985 return (ret);
986 }
987
988 static bool
aio_set_cancel_function_locked(struct kaiocb * job,aio_cancel_fn_t * func)989 aio_set_cancel_function_locked(struct kaiocb *job, aio_cancel_fn_t *func)
990 {
991
992 AIO_LOCK_ASSERT(job->userproc->p_aioinfo, MA_OWNED);
993 if (job->jobflags & KAIOCB_CANCELLED)
994 return (false);
995 job->cancel_fn = func;
996 return (true);
997 }
998
999 bool
aio_set_cancel_function(struct kaiocb * job,aio_cancel_fn_t * func)1000 aio_set_cancel_function(struct kaiocb *job, aio_cancel_fn_t *func)
1001 {
1002 struct kaioinfo *ki;
1003 bool ret;
1004
1005 ki = job->userproc->p_aioinfo;
1006 AIO_LOCK(ki);
1007 ret = aio_set_cancel_function_locked(job, func);
1008 AIO_UNLOCK(ki);
1009 return (ret);
1010 }
1011
1012 void
aio_complete(struct kaiocb * job,long status,int error)1013 aio_complete(struct kaiocb *job, long status, int error)
1014 {
1015 struct kaioinfo *ki;
1016 struct proc *userp;
1017
1018 job->uaiocb._aiocb_private.error = error;
1019 job->uaiocb._aiocb_private.status = status;
1020
1021 userp = job->userproc;
1022 ki = userp->p_aioinfo;
1023
1024 AIO_LOCK(ki);
1025 KASSERT(!(job->jobflags & KAIOCB_FINISHED),
1026 ("duplicate aio_complete"));
1027 job->jobflags |= KAIOCB_FINISHED;
1028 if ((job->jobflags & (KAIOCB_QUEUEING | KAIOCB_CANCELLING)) == 0) {
1029 TAILQ_REMOVE(&ki->kaio_jobqueue, job, plist);
1030 aio_bio_done_notify(userp, job);
1031 }
1032 AIO_UNLOCK(ki);
1033 }
1034
1035 void
aio_cancel(struct kaiocb * job)1036 aio_cancel(struct kaiocb *job)
1037 {
1038
1039 aio_complete(job, -1, ECANCELED);
1040 }
1041
1042 void
aio_switch_vmspace(struct kaiocb * job)1043 aio_switch_vmspace(struct kaiocb *job)
1044 {
1045
1046 vmspace_switch_aio(job->userproc->p_vmspace);
1047 }
1048
1049 /*
1050 * The AIO daemon, most of the actual work is done in aio_process_*,
1051 * but the setup (and address space mgmt) is done in this routine.
1052 */
1053 static void
aio_daemon(void * _id)1054 aio_daemon(void *_id)
1055 {
1056 struct kaiocb *job;
1057 struct aioproc *aiop;
1058 struct kaioinfo *ki;
1059 struct proc *p;
1060 struct vmspace *myvm;
1061 struct thread *td = curthread;
1062 int id = (intptr_t)_id;
1063
1064 /*
1065 * Grab an extra reference on the daemon's vmspace so that it
1066 * doesn't get freed by jobs that switch to a different
1067 * vmspace.
1068 */
1069 p = td->td_proc;
1070 myvm = vmspace_acquire_ref(p);
1071
1072 KASSERT(p->p_textvp == NULL, ("kthread has a textvp"));
1073
1074 /*
1075 * Allocate and ready the aio control info. There is one aiop structure
1076 * per daemon.
1077 */
1078 aiop = malloc(sizeof(*aiop), M_AIO, M_WAITOK);
1079 aiop->aioproc = p;
1080 aiop->aioprocflags = 0;
1081
1082 /*
1083 * Wakeup parent process. (Parent sleeps to keep from blasting away
1084 * and creating too many daemons.)
1085 */
1086 sema_post(&aio_newproc_sem);
1087
1088 mtx_lock(&aio_job_mtx);
1089 for (;;) {
1090 /*
1091 * Take daemon off of free queue
1092 */
1093 if (aiop->aioprocflags & AIOP_FREE) {
1094 TAILQ_REMOVE(&aio_freeproc, aiop, list);
1095 aiop->aioprocflags &= ~AIOP_FREE;
1096 }
1097
1098 /*
1099 * Check for jobs.
1100 */
1101 while ((job = aio_selectjob(aiop)) != NULL) {
1102 mtx_unlock(&aio_job_mtx);
1103
1104 ki = job->userproc->p_aioinfo;
1105 job->handle_fn(job);
1106
1107 mtx_lock(&aio_job_mtx);
1108 /* Decrement the active job count. */
1109 ki->kaio_active_count--;
1110 }
1111
1112 /*
1113 * Disconnect from user address space.
1114 */
1115 if (p->p_vmspace != myvm) {
1116 mtx_unlock(&aio_job_mtx);
1117 vmspace_switch_aio(myvm);
1118 mtx_lock(&aio_job_mtx);
1119 /*
1120 * We have to restart to avoid race, we only sleep if
1121 * no job can be selected.
1122 */
1123 continue;
1124 }
1125
1126 mtx_assert(&aio_job_mtx, MA_OWNED);
1127
1128 TAILQ_INSERT_HEAD(&aio_freeproc, aiop, list);
1129 aiop->aioprocflags |= AIOP_FREE;
1130
1131 /*
1132 * If daemon is inactive for a long time, allow it to exit,
1133 * thereby freeing resources.
1134 */
1135 if (msleep(p, &aio_job_mtx, PRIBIO, "aiordy",
1136 aiod_lifetime) == EWOULDBLOCK && TAILQ_EMPTY(&aio_jobs) &&
1137 (aiop->aioprocflags & AIOP_FREE) &&
1138 num_aio_procs > target_aio_procs)
1139 break;
1140 }
1141 TAILQ_REMOVE(&aio_freeproc, aiop, list);
1142 num_aio_procs--;
1143 mtx_unlock(&aio_job_mtx);
1144 free(aiop, M_AIO);
1145 free_unr(aiod_unr, id);
1146 vmspace_free(myvm);
1147
1148 KASSERT(p->p_vmspace == myvm,
1149 ("AIOD: bad vmspace for exiting daemon"));
1150 KASSERT(refcount_load(&myvm->vm_refcnt) > 1,
1151 ("AIOD: bad vm refcnt for exiting daemon: %d",
1152 refcount_load(&myvm->vm_refcnt)));
1153 kproc_exit(0);
1154 }
1155
1156 /*
1157 * Create a new AIO daemon. This is mostly a kernel-thread fork routine. The
1158 * AIO daemon modifies its environment itself.
1159 */
1160 static int
aio_newproc(int * start)1161 aio_newproc(int *start)
1162 {
1163 int error;
1164 struct proc *p;
1165 int id;
1166
1167 id = alloc_unr(aiod_unr);
1168 error = kproc_create(aio_daemon, (void *)(intptr_t)id, &p,
1169 RFNOWAIT, 0, "aiod%d", id);
1170 if (error == 0) {
1171 /*
1172 * Wait until daemon is started.
1173 */
1174 sema_wait(&aio_newproc_sem);
1175 mtx_lock(&aio_job_mtx);
1176 num_aio_procs++;
1177 if (start != NULL)
1178 (*start)--;
1179 mtx_unlock(&aio_job_mtx);
1180 } else {
1181 free_unr(aiod_unr, id);
1182 }
1183 return (error);
1184 }
1185
1186 /*
1187 * Try the high-performance, low-overhead bio method for eligible
1188 * VCHR devices. This method doesn't use an aio helper thread, and
1189 * thus has very low overhead.
1190 *
1191 * Assumes that the caller, aio_aqueue(), has incremented the file
1192 * structure's reference count, preventing its deallocation for the
1193 * duration of this call.
1194 */
1195 static int
aio_qbio(struct proc * p,struct kaiocb * job)1196 aio_qbio(struct proc *p, struct kaiocb *job)
1197 {
1198 struct aiocb *cb;
1199 struct file *fp;
1200 struct buf *pbuf;
1201 struct vnode *vp;
1202 struct cdevsw *csw;
1203 struct cdev *dev;
1204 struct kaioinfo *ki;
1205 struct bio **bios = NULL;
1206 off_t offset;
1207 int bio_cmd, error, i, iovcnt, opcode, poff, ref;
1208 vm_prot_t prot;
1209 bool use_unmapped;
1210
1211 cb = &job->uaiocb;
1212 fp = job->fd_file;
1213 opcode = cb->aio_lio_opcode;
1214
1215 if (!(opcode == LIO_WRITE || opcode == LIO_WRITEV ||
1216 opcode == LIO_READ || opcode == LIO_READV))
1217 return (-1);
1218 if (fp == NULL || fp->f_type != DTYPE_VNODE)
1219 return (-1);
1220
1221 vp = fp->f_vnode;
1222 if (vp->v_type != VCHR)
1223 return (-1);
1224 if (vp->v_bufobj.bo_bsize == 0)
1225 return (-1);
1226
1227 bio_cmd = (opcode & LIO_WRITE) ? BIO_WRITE : BIO_READ;
1228 iovcnt = job->uiop->uio_iovcnt;
1229 if (iovcnt > max_buf_aio)
1230 return (-1);
1231 for (i = 0; i < iovcnt; i++) {
1232 if (job->uiop->uio_iov[i].iov_len % vp->v_bufobj.bo_bsize != 0)
1233 return (-1);
1234 if (job->uiop->uio_iov[i].iov_len > maxphys) {
1235 error = -1;
1236 return (-1);
1237 }
1238 }
1239 offset = cb->aio_offset;
1240
1241 ref = 0;
1242 csw = devvn_refthread(vp, &dev, &ref);
1243 if (csw == NULL)
1244 return (ENXIO);
1245
1246 if ((csw->d_flags & D_DISK) == 0) {
1247 error = -1;
1248 goto unref;
1249 }
1250 if (job->uiop->uio_resid > dev->si_iosize_max) {
1251 error = -1;
1252 goto unref;
1253 }
1254
1255 ki = p->p_aioinfo;
1256 job->error = 0;
1257
1258 use_unmapped = (dev->si_flags & SI_UNMAPPED) && unmapped_buf_allowed;
1259 if (!use_unmapped) {
1260 AIO_LOCK(ki);
1261 if (ki->kaio_buffer_count + iovcnt > max_buf_aio) {
1262 AIO_UNLOCK(ki);
1263 error = EAGAIN;
1264 goto unref;
1265 }
1266 ki->kaio_buffer_count += iovcnt;
1267 AIO_UNLOCK(ki);
1268 }
1269
1270 bios = malloc(sizeof(struct bio *) * iovcnt, M_TEMP, M_WAITOK);
1271 refcount_init(&job->nbio, iovcnt);
1272 for (i = 0; i < iovcnt; i++) {
1273 struct vm_page** pages;
1274 struct bio *bp;
1275 void *buf;
1276 size_t nbytes;
1277 int npages;
1278
1279 buf = job->uiop->uio_iov[i].iov_base;
1280 nbytes = job->uiop->uio_iov[i].iov_len;
1281
1282 bios[i] = g_alloc_bio();
1283 bp = bios[i];
1284
1285 poff = (vm_offset_t)buf & PAGE_MASK;
1286 if (use_unmapped) {
1287 pbuf = NULL;
1288 pages = malloc(sizeof(vm_page_t) * (atop(round_page(
1289 nbytes)) + 1), M_TEMP, M_WAITOK | M_ZERO);
1290 } else {
1291 pbuf = uma_zalloc(pbuf_zone, M_WAITOK);
1292 BUF_KERNPROC(pbuf);
1293 pages = pbuf->b_pages;
1294 }
1295
1296 bp->bio_length = nbytes;
1297 bp->bio_bcount = nbytes;
1298 bp->bio_done = aio_biowakeup;
1299 bp->bio_offset = offset;
1300 bp->bio_cmd = bio_cmd;
1301 bp->bio_dev = dev;
1302 bp->bio_caller1 = job;
1303 bp->bio_caller2 = pbuf;
1304
1305 prot = VM_PROT_READ;
1306 if (opcode == LIO_READ || opcode == LIO_READV)
1307 prot |= VM_PROT_WRITE; /* Less backwards than it looks */
1308 npages = vm_fault_quick_hold_pages(&curproc->p_vmspace->vm_map,
1309 (vm_offset_t)buf, bp->bio_length, prot, pages,
1310 atop(maxphys) + 1);
1311 if (npages < 0) {
1312 if (pbuf != NULL)
1313 uma_zfree(pbuf_zone, pbuf);
1314 else
1315 free(pages, M_TEMP);
1316 error = EFAULT;
1317 g_destroy_bio(bp);
1318 i--;
1319 goto destroy_bios;
1320 }
1321 if (pbuf != NULL) {
1322 pmap_qenter((vm_offset_t)pbuf->b_data, pages, npages);
1323 bp->bio_data = pbuf->b_data + poff;
1324 pbuf->b_npages = npages;
1325 atomic_add_int(&num_buf_aio, 1);
1326 } else {
1327 bp->bio_ma = pages;
1328 bp->bio_ma_n = npages;
1329 bp->bio_ma_offset = poff;
1330 bp->bio_data = unmapped_buf;
1331 bp->bio_flags |= BIO_UNMAPPED;
1332 atomic_add_int(&num_unmapped_aio, 1);
1333 }
1334
1335 offset += nbytes;
1336 }
1337
1338 /* Perform transfer. */
1339 for (i = 0; i < iovcnt; i++)
1340 csw->d_strategy(bios[i]);
1341 free(bios, M_TEMP);
1342
1343 dev_relthread(dev, ref);
1344 return (0);
1345
1346 destroy_bios:
1347 for (; i >= 0; i--)
1348 aio_biocleanup(bios[i]);
1349 free(bios, M_TEMP);
1350 unref:
1351 dev_relthread(dev, ref);
1352 return (error);
1353 }
1354
1355 #ifdef COMPAT_FREEBSD6
1356 static int
convert_old_sigevent(struct osigevent * osig,struct sigevent * nsig)1357 convert_old_sigevent(struct osigevent *osig, struct sigevent *nsig)
1358 {
1359
1360 /*
1361 * Only SIGEV_NONE, SIGEV_SIGNAL, and SIGEV_KEVENT are
1362 * supported by AIO with the old sigevent structure.
1363 */
1364 nsig->sigev_notify = osig->sigev_notify;
1365 switch (nsig->sigev_notify) {
1366 case SIGEV_NONE:
1367 break;
1368 case SIGEV_SIGNAL:
1369 nsig->sigev_signo = osig->__sigev_u.__sigev_signo;
1370 break;
1371 case SIGEV_KEVENT:
1372 nsig->sigev_notify_kqueue =
1373 osig->__sigev_u.__sigev_notify_kqueue;
1374 nsig->sigev_value.sival_ptr = osig->sigev_value.sival_ptr;
1375 break;
1376 default:
1377 return (EINVAL);
1378 }
1379 return (0);
1380 }
1381
1382 static int
aiocb_copyin_old_sigevent(struct aiocb * ujob,struct kaiocb * kjob,int type __unused)1383 aiocb_copyin_old_sigevent(struct aiocb *ujob, struct kaiocb *kjob,
1384 int type __unused)
1385 {
1386 struct oaiocb *ojob;
1387 struct aiocb *kcb = &kjob->uaiocb;
1388 int error;
1389
1390 bzero(kcb, sizeof(struct aiocb));
1391 error = copyin(ujob, kcb, sizeof(struct oaiocb));
1392 if (error)
1393 return (error);
1394 /* No need to copyin aio_iov, because it did not exist in FreeBSD 6 */
1395 ojob = (struct oaiocb *)kcb;
1396 return (convert_old_sigevent(&ojob->aio_sigevent, &kcb->aio_sigevent));
1397 }
1398 #endif
1399
1400 static int
aiocb_copyin(struct aiocb * ujob,struct kaiocb * kjob,int type)1401 aiocb_copyin(struct aiocb *ujob, struct kaiocb *kjob, int type)
1402 {
1403 struct aiocb *kcb = &kjob->uaiocb;
1404 int error;
1405
1406 error = copyin(ujob, kcb, sizeof(struct aiocb));
1407 if (error)
1408 return (error);
1409 if (type == LIO_NOP)
1410 type = kcb->aio_lio_opcode;
1411 if (type & LIO_VECTORED) {
1412 /* malloc a uio and copy in the iovec */
1413 error = copyinuio(__DEVOLATILE(struct iovec*, kcb->aio_iov),
1414 kcb->aio_iovcnt, &kjob->uiop);
1415 }
1416
1417 return (error);
1418 }
1419
1420 static long
aiocb_fetch_status(struct aiocb * ujob)1421 aiocb_fetch_status(struct aiocb *ujob)
1422 {
1423
1424 return (fuword(&ujob->_aiocb_private.status));
1425 }
1426
1427 static long
aiocb_fetch_error(struct aiocb * ujob)1428 aiocb_fetch_error(struct aiocb *ujob)
1429 {
1430
1431 return (fuword(&ujob->_aiocb_private.error));
1432 }
1433
1434 static int
aiocb_store_status(struct aiocb * ujob,long status)1435 aiocb_store_status(struct aiocb *ujob, long status)
1436 {
1437
1438 return (suword(&ujob->_aiocb_private.status, status));
1439 }
1440
1441 static int
aiocb_store_error(struct aiocb * ujob,long error)1442 aiocb_store_error(struct aiocb *ujob, long error)
1443 {
1444
1445 return (suword(&ujob->_aiocb_private.error, error));
1446 }
1447
1448 static int
aiocb_store_aiocb(struct aiocb ** ujobp,struct aiocb * ujob)1449 aiocb_store_aiocb(struct aiocb **ujobp, struct aiocb *ujob)
1450 {
1451
1452 return (suword(ujobp, (long)ujob));
1453 }
1454
1455 static struct aiocb_ops aiocb_ops = {
1456 .aio_copyin = aiocb_copyin,
1457 .fetch_status = aiocb_fetch_status,
1458 .fetch_error = aiocb_fetch_error,
1459 .store_status = aiocb_store_status,
1460 .store_error = aiocb_store_error,
1461 .store_aiocb = aiocb_store_aiocb,
1462 };
1463
1464 #ifdef COMPAT_FREEBSD6
1465 static struct aiocb_ops aiocb_ops_osigevent = {
1466 .aio_copyin = aiocb_copyin_old_sigevent,
1467 .fetch_status = aiocb_fetch_status,
1468 .fetch_error = aiocb_fetch_error,
1469 .store_status = aiocb_store_status,
1470 .store_error = aiocb_store_error,
1471 .store_aiocb = aiocb_store_aiocb,
1472 };
1473 #endif
1474
1475 /*
1476 * Queue a new AIO request. Choosing either the threaded or direct bio VCHR
1477 * technique is done in this code.
1478 */
1479 int
aio_aqueue(struct thread * td,struct aiocb * ujob,struct aioliojob * lj,int type,struct aiocb_ops * ops)1480 aio_aqueue(struct thread *td, struct aiocb *ujob, struct aioliojob *lj,
1481 int type, struct aiocb_ops *ops)
1482 {
1483 struct proc *p = td->td_proc;
1484 struct file *fp = NULL;
1485 struct kaiocb *job;
1486 struct kaioinfo *ki;
1487 struct kevent kev;
1488 int opcode;
1489 int error;
1490 int fd, kqfd;
1491 u_short evflags;
1492
1493 if (p->p_aioinfo == NULL)
1494 aio_init_aioinfo(p);
1495
1496 ki = p->p_aioinfo;
1497
1498 ops->store_status(ujob, -1);
1499 ops->store_error(ujob, 0);
1500
1501 if (num_queue_count >= max_queue_count ||
1502 ki->kaio_count >= max_aio_queue_per_proc) {
1503 error = EAGAIN;
1504 goto err1;
1505 }
1506
1507 job = uma_zalloc(aiocb_zone, M_WAITOK | M_ZERO);
1508 knlist_init_mtx(&job->klist, AIO_MTX(ki));
1509
1510 error = ops->aio_copyin(ujob, job, type);
1511 if (error)
1512 goto err2;
1513
1514 if (job->uaiocb.aio_nbytes > IOSIZE_MAX) {
1515 error = EINVAL;
1516 goto err2;
1517 }
1518
1519 if (job->uaiocb.aio_sigevent.sigev_notify != SIGEV_KEVENT &&
1520 job->uaiocb.aio_sigevent.sigev_notify != SIGEV_SIGNAL &&
1521 job->uaiocb.aio_sigevent.sigev_notify != SIGEV_THREAD_ID &&
1522 job->uaiocb.aio_sigevent.sigev_notify != SIGEV_NONE) {
1523 error = EINVAL;
1524 goto err2;
1525 }
1526
1527 if ((job->uaiocb.aio_sigevent.sigev_notify == SIGEV_SIGNAL ||
1528 job->uaiocb.aio_sigevent.sigev_notify == SIGEV_THREAD_ID) &&
1529 !_SIG_VALID(job->uaiocb.aio_sigevent.sigev_signo)) {
1530 error = EINVAL;
1531 goto err2;
1532 }
1533
1534 /* Get the opcode. */
1535 if (type == LIO_NOP) {
1536 switch (job->uaiocb.aio_lio_opcode & ~LIO_FOFFSET) {
1537 case LIO_WRITE:
1538 case LIO_WRITEV:
1539 case LIO_NOP:
1540 case LIO_READ:
1541 case LIO_READV:
1542 opcode = job->uaiocb.aio_lio_opcode & ~LIO_FOFFSET;
1543 if ((job->uaiocb.aio_lio_opcode & LIO_FOFFSET) != 0)
1544 job->ioflags |= KAIOCB_IO_FOFFSET;
1545 break;
1546 default:
1547 error = EINVAL;
1548 goto err2;
1549 }
1550 } else
1551 opcode = job->uaiocb.aio_lio_opcode = type;
1552
1553 ksiginfo_init(&job->ksi);
1554
1555 /* Save userspace address of the job info. */
1556 job->ujob = ujob;
1557
1558 /*
1559 * Validate the opcode and fetch the file object for the specified
1560 * file descriptor.
1561 *
1562 * XXXRW: Moved the opcode validation up here so that we don't
1563 * retrieve a file descriptor without knowing what the capabiltity
1564 * should be.
1565 */
1566 fd = job->uaiocb.aio_fildes;
1567 switch (opcode) {
1568 case LIO_WRITE:
1569 case LIO_WRITEV:
1570 error = fget_write(td, fd, &cap_pwrite_rights, &fp);
1571 break;
1572 case LIO_READ:
1573 case LIO_READV:
1574 error = fget_read(td, fd, &cap_pread_rights, &fp);
1575 break;
1576 case LIO_SYNC:
1577 case LIO_DSYNC:
1578 error = fget(td, fd, &cap_fsync_rights, &fp);
1579 break;
1580 case LIO_MLOCK:
1581 break;
1582 case LIO_NOP:
1583 error = fget(td, fd, &cap_no_rights, &fp);
1584 break;
1585 default:
1586 error = EINVAL;
1587 }
1588 if (error)
1589 goto err3;
1590
1591 if ((opcode & LIO_SYNC) && fp->f_vnode == NULL) {
1592 error = EINVAL;
1593 goto err3;
1594 }
1595
1596 if ((opcode == LIO_READ || opcode == LIO_READV ||
1597 opcode == LIO_WRITE || opcode == LIO_WRITEV) &&
1598 job->uaiocb.aio_offset < 0 &&
1599 (fp->f_vnode == NULL || fp->f_vnode->v_type != VCHR)) {
1600 error = EINVAL;
1601 goto err3;
1602 }
1603
1604 if (fp != NULL && fp->f_ops == &path_fileops) {
1605 error = EBADF;
1606 goto err3;
1607 }
1608
1609 job->fd_file = fp;
1610
1611 mtx_lock(&aio_job_mtx);
1612 job->seqno = jobseqno++;
1613 mtx_unlock(&aio_job_mtx);
1614 if (opcode == LIO_NOP) {
1615 fdrop(fp, td);
1616 MPASS(job->uiop == &job->uio || job->uiop == NULL);
1617 uma_zfree(aiocb_zone, job);
1618 return (0);
1619 }
1620
1621 if (job->uaiocb.aio_sigevent.sigev_notify != SIGEV_KEVENT)
1622 goto no_kqueue;
1623 evflags = job->uaiocb.aio_sigevent.sigev_notify_kevent_flags;
1624 if ((evflags & ~(EV_CLEAR | EV_DISPATCH | EV_ONESHOT)) != 0) {
1625 error = EINVAL;
1626 goto err3;
1627 }
1628 kqfd = job->uaiocb.aio_sigevent.sigev_notify_kqueue;
1629 memset(&kev, 0, sizeof(kev));
1630 kev.ident = (uintptr_t)job->ujob;
1631 kev.filter = EVFILT_AIO;
1632 kev.flags = EV_ADD | EV_ENABLE | EV_FLAG1 | evflags;
1633 kev.data = (intptr_t)job;
1634 kev.udata = job->uaiocb.aio_sigevent.sigev_value.sival_ptr;
1635 error = kqfd_register(kqfd, &kev, td, M_WAITOK);
1636 if (error)
1637 goto err3;
1638
1639 no_kqueue:
1640
1641 ops->store_error(ujob, EINPROGRESS);
1642 job->uaiocb._aiocb_private.error = EINPROGRESS;
1643 job->userproc = p;
1644 job->cred = crhold(td->td_ucred);
1645 job->jobflags = KAIOCB_QUEUEING;
1646 job->lio = lj;
1647
1648 if (opcode & LIO_VECTORED) {
1649 /* Use the uio copied in by aio_copyin */
1650 MPASS(job->uiop != &job->uio && job->uiop != NULL);
1651 } else {
1652 /* Setup the inline uio */
1653 job->iov[0].iov_base = (void *)(uintptr_t)job->uaiocb.aio_buf;
1654 job->iov[0].iov_len = job->uaiocb.aio_nbytes;
1655 job->uio.uio_iov = job->iov;
1656 job->uio.uio_iovcnt = 1;
1657 job->uio.uio_resid = job->uaiocb.aio_nbytes;
1658 job->uio.uio_segflg = UIO_USERSPACE;
1659 job->uiop = &job->uio;
1660 }
1661 switch (opcode & (LIO_READ | LIO_WRITE)) {
1662 case LIO_READ:
1663 job->uiop->uio_rw = UIO_READ;
1664 break;
1665 case LIO_WRITE:
1666 job->uiop->uio_rw = UIO_WRITE;
1667 break;
1668 }
1669 job->uiop->uio_offset = job->uaiocb.aio_offset;
1670 job->uiop->uio_td = td;
1671
1672 if (opcode == LIO_MLOCK) {
1673 aio_schedule(job, aio_process_mlock);
1674 error = 0;
1675 } else if (fp->f_ops->fo_aio_queue == NULL)
1676 error = aio_queue_file(fp, job);
1677 else
1678 error = fo_aio_queue(fp, job);
1679 if (error)
1680 goto err4;
1681
1682 AIO_LOCK(ki);
1683 job->jobflags &= ~KAIOCB_QUEUEING;
1684 TAILQ_INSERT_TAIL(&ki->kaio_all, job, allist);
1685 ki->kaio_count++;
1686 if (lj)
1687 lj->lioj_count++;
1688 atomic_add_int(&num_queue_count, 1);
1689 if (job->jobflags & KAIOCB_FINISHED) {
1690 /*
1691 * The queue callback completed the request synchronously.
1692 * The bulk of the completion is deferred in that case
1693 * until this point.
1694 */
1695 aio_bio_done_notify(p, job);
1696 } else
1697 TAILQ_INSERT_TAIL(&ki->kaio_jobqueue, job, plist);
1698 AIO_UNLOCK(ki);
1699 return (0);
1700
1701 err4:
1702 crfree(job->cred);
1703 err3:
1704 if (fp)
1705 fdrop(fp, td);
1706 knlist_delete(&job->klist, curthread, 0);
1707 err2:
1708 if (job->uiop != &job->uio)
1709 freeuio(job->uiop);
1710 uma_zfree(aiocb_zone, job);
1711 err1:
1712 ops->store_error(ujob, error);
1713 return (error);
1714 }
1715
1716 static void
aio_cancel_daemon_job(struct kaiocb * job)1717 aio_cancel_daemon_job(struct kaiocb *job)
1718 {
1719
1720 mtx_lock(&aio_job_mtx);
1721 if (!aio_cancel_cleared(job))
1722 TAILQ_REMOVE(&aio_jobs, job, list);
1723 mtx_unlock(&aio_job_mtx);
1724 aio_cancel(job);
1725 }
1726
1727 void
aio_schedule(struct kaiocb * job,aio_handle_fn_t * func)1728 aio_schedule(struct kaiocb *job, aio_handle_fn_t *func)
1729 {
1730
1731 mtx_lock(&aio_job_mtx);
1732 if (!aio_set_cancel_function(job, aio_cancel_daemon_job)) {
1733 mtx_unlock(&aio_job_mtx);
1734 aio_cancel(job);
1735 return;
1736 }
1737 job->handle_fn = func;
1738 TAILQ_INSERT_TAIL(&aio_jobs, job, list);
1739 aio_kick_nowait(job->userproc);
1740 mtx_unlock(&aio_job_mtx);
1741 }
1742
1743 static void
aio_cancel_sync(struct kaiocb * job)1744 aio_cancel_sync(struct kaiocb *job)
1745 {
1746 struct kaioinfo *ki;
1747
1748 ki = job->userproc->p_aioinfo;
1749 AIO_LOCK(ki);
1750 if (!aio_cancel_cleared(job))
1751 TAILQ_REMOVE(&ki->kaio_syncqueue, job, list);
1752 AIO_UNLOCK(ki);
1753 aio_cancel(job);
1754 }
1755
1756 int
aio_queue_file(struct file * fp,struct kaiocb * job)1757 aio_queue_file(struct file *fp, struct kaiocb *job)
1758 {
1759 struct kaioinfo *ki;
1760 struct kaiocb *job2;
1761 struct vnode *vp;
1762 struct mount *mp;
1763 int error;
1764 bool safe;
1765
1766 ki = job->userproc->p_aioinfo;
1767 error = aio_qbio(job->userproc, job);
1768 if (error >= 0)
1769 return (error);
1770 safe = false;
1771 if (fp->f_type == DTYPE_VNODE) {
1772 vp = fp->f_vnode;
1773 if (vp->v_type == VREG || vp->v_type == VDIR) {
1774 mp = fp->f_vnode->v_mount;
1775 if (mp == NULL || (mp->mnt_flag & MNT_LOCAL) != 0)
1776 safe = true;
1777 }
1778 }
1779 if (!(safe || enable_aio_unsafe)) {
1780 counted_warning(&unsafe_warningcnt,
1781 "is attempting to use unsafe AIO requests");
1782 return (EOPNOTSUPP);
1783 }
1784
1785 if (job->uaiocb.aio_lio_opcode & (LIO_WRITE | LIO_READ)) {
1786 aio_schedule(job, aio_process_rw);
1787 error = 0;
1788 } else if (job->uaiocb.aio_lio_opcode & LIO_SYNC) {
1789 AIO_LOCK(ki);
1790 TAILQ_FOREACH(job2, &ki->kaio_jobqueue, plist) {
1791 if (job2->fd_file == job->fd_file &&
1792 ((job2->uaiocb.aio_lio_opcode & LIO_SYNC) == 0) &&
1793 job2->seqno < job->seqno) {
1794 job2->jobflags |= KAIOCB_CHECKSYNC;
1795 job->pending++;
1796 }
1797 }
1798 if (job->pending != 0) {
1799 if (!aio_set_cancel_function_locked(job,
1800 aio_cancel_sync)) {
1801 AIO_UNLOCK(ki);
1802 aio_cancel(job);
1803 return (0);
1804 }
1805 TAILQ_INSERT_TAIL(&ki->kaio_syncqueue, job, list);
1806 AIO_UNLOCK(ki);
1807 return (0);
1808 }
1809 AIO_UNLOCK(ki);
1810 aio_schedule(job, aio_process_sync);
1811 error = 0;
1812 } else {
1813 error = EINVAL;
1814 }
1815 return (error);
1816 }
1817
1818 static void
aio_kick_nowait(struct proc * userp)1819 aio_kick_nowait(struct proc *userp)
1820 {
1821 struct kaioinfo *ki = userp->p_aioinfo;
1822 struct aioproc *aiop;
1823
1824 mtx_assert(&aio_job_mtx, MA_OWNED);
1825 if ((aiop = TAILQ_FIRST(&aio_freeproc)) != NULL) {
1826 TAILQ_REMOVE(&aio_freeproc, aiop, list);
1827 aiop->aioprocflags &= ~AIOP_FREE;
1828 wakeup(aiop->aioproc);
1829 } else if (num_aio_resv_start + num_aio_procs < max_aio_procs &&
1830 ki->kaio_active_count + num_aio_resv_start < max_aio_per_proc) {
1831 taskqueue_enqueue(taskqueue_aiod_kick, &ki->kaio_task);
1832 }
1833 }
1834
1835 static int
aio_kick(struct proc * userp)1836 aio_kick(struct proc *userp)
1837 {
1838 struct kaioinfo *ki = userp->p_aioinfo;
1839 struct aioproc *aiop;
1840 int error, ret = 0;
1841
1842 mtx_assert(&aio_job_mtx, MA_OWNED);
1843 retryproc:
1844 if ((aiop = TAILQ_FIRST(&aio_freeproc)) != NULL) {
1845 TAILQ_REMOVE(&aio_freeproc, aiop, list);
1846 aiop->aioprocflags &= ~AIOP_FREE;
1847 wakeup(aiop->aioproc);
1848 } else if (num_aio_resv_start + num_aio_procs < max_aio_procs &&
1849 ki->kaio_active_count + num_aio_resv_start < max_aio_per_proc) {
1850 num_aio_resv_start++;
1851 mtx_unlock(&aio_job_mtx);
1852 error = aio_newproc(&num_aio_resv_start);
1853 mtx_lock(&aio_job_mtx);
1854 if (error) {
1855 num_aio_resv_start--;
1856 goto retryproc;
1857 }
1858 } else {
1859 ret = -1;
1860 }
1861 return (ret);
1862 }
1863
1864 static void
aio_kick_helper(void * context,int pending)1865 aio_kick_helper(void *context, int pending)
1866 {
1867 struct proc *userp = context;
1868
1869 mtx_lock(&aio_job_mtx);
1870 while (--pending >= 0) {
1871 if (aio_kick(userp))
1872 break;
1873 }
1874 mtx_unlock(&aio_job_mtx);
1875 }
1876
1877 /*
1878 * Support the aio_return system call, as a side-effect, kernel resources are
1879 * released.
1880 */
1881 static int
kern_aio_return(struct thread * td,struct aiocb * ujob,struct aiocb_ops * ops)1882 kern_aio_return(struct thread *td, struct aiocb *ujob, struct aiocb_ops *ops)
1883 {
1884 struct proc *p = td->td_proc;
1885 struct kaiocb *job;
1886 struct kaioinfo *ki;
1887 long status, error;
1888
1889 ki = p->p_aioinfo;
1890 if (ki == NULL)
1891 return (EINVAL);
1892 AIO_LOCK(ki);
1893 TAILQ_FOREACH(job, &ki->kaio_done, plist) {
1894 if (job->ujob == ujob)
1895 break;
1896 }
1897 if (job != NULL) {
1898 MPASS(job->jobflags & KAIOCB_FINISHED);
1899 status = job->uaiocb._aiocb_private.status;
1900 error = job->uaiocb._aiocb_private.error;
1901 td->td_retval[0] = status;
1902 td->td_ru.ru_oublock += job->outblock;
1903 td->td_ru.ru_inblock += job->inblock;
1904 td->td_ru.ru_msgsnd += job->msgsnd;
1905 td->td_ru.ru_msgrcv += job->msgrcv;
1906 aio_free_entry(job);
1907 AIO_UNLOCK(ki);
1908 ops->store_error(ujob, error);
1909 ops->store_status(ujob, status);
1910 } else {
1911 error = EINVAL;
1912 AIO_UNLOCK(ki);
1913 }
1914 return (error);
1915 }
1916
1917 int
sys_aio_return(struct thread * td,struct aio_return_args * uap)1918 sys_aio_return(struct thread *td, struct aio_return_args *uap)
1919 {
1920
1921 return (kern_aio_return(td, uap->aiocbp, &aiocb_ops));
1922 }
1923
1924 /*
1925 * Allow a process to wakeup when any of the I/O requests are completed.
1926 */
1927 static int
kern_aio_suspend(struct thread * td,int njoblist,struct aiocb ** ujoblist,struct timespec * ts)1928 kern_aio_suspend(struct thread *td, int njoblist, struct aiocb **ujoblist,
1929 struct timespec *ts)
1930 {
1931 struct proc *p = td->td_proc;
1932 struct timeval atv;
1933 struct kaioinfo *ki;
1934 struct kaiocb *firstjob, *job;
1935 int error, i, timo;
1936
1937 timo = 0;
1938 if (ts) {
1939 if (ts->tv_nsec < 0 || ts->tv_nsec >= 1000000000)
1940 return (EINVAL);
1941
1942 TIMESPEC_TO_TIMEVAL(&atv, ts);
1943 if (itimerfix(&atv))
1944 return (EINVAL);
1945 timo = tvtohz(&atv);
1946 }
1947
1948 ki = p->p_aioinfo;
1949 if (ki == NULL)
1950 return (EAGAIN);
1951
1952 if (njoblist == 0)
1953 return (0);
1954
1955 AIO_LOCK(ki);
1956 for (;;) {
1957 firstjob = NULL;
1958 error = 0;
1959 TAILQ_FOREACH(job, &ki->kaio_all, allist) {
1960 for (i = 0; i < njoblist; i++) {
1961 if (job->ujob == ujoblist[i]) {
1962 if (firstjob == NULL)
1963 firstjob = job;
1964 if (job->jobflags & KAIOCB_FINISHED)
1965 goto RETURN;
1966 }
1967 }
1968 }
1969 /* All tasks were finished. */
1970 if (firstjob == NULL)
1971 break;
1972
1973 ki->kaio_flags |= KAIO_WAKEUP;
1974 error = msleep(&p->p_aioinfo, AIO_MTX(ki), PRIBIO | PCATCH,
1975 "aiospn", timo);
1976 if (error == ERESTART)
1977 error = EINTR;
1978 if (error)
1979 break;
1980 }
1981 RETURN:
1982 AIO_UNLOCK(ki);
1983 return (error);
1984 }
1985
1986 int
sys_aio_suspend(struct thread * td,struct aio_suspend_args * uap)1987 sys_aio_suspend(struct thread *td, struct aio_suspend_args *uap)
1988 {
1989 struct timespec ts, *tsp;
1990 struct aiocb **ujoblist;
1991 int error;
1992
1993 if (uap->nent < 0 || uap->nent > max_aio_queue_per_proc)
1994 return (EINVAL);
1995
1996 if (uap->timeout) {
1997 /* Get timespec struct. */
1998 if ((error = copyin(uap->timeout, &ts, sizeof(ts))) != 0)
1999 return (error);
2000 tsp = &ts;
2001 } else
2002 tsp = NULL;
2003
2004 ujoblist = malloc(uap->nent * sizeof(ujoblist[0]), M_AIO, M_WAITOK);
2005 error = copyin(uap->aiocbp, ujoblist, uap->nent * sizeof(ujoblist[0]));
2006 if (error == 0)
2007 error = kern_aio_suspend(td, uap->nent, ujoblist, tsp);
2008 free(ujoblist, M_AIO);
2009 return (error);
2010 }
2011
2012 /*
2013 * aio_cancel cancels any non-bio aio operations not currently in progress.
2014 */
2015 int
sys_aio_cancel(struct thread * td,struct aio_cancel_args * uap)2016 sys_aio_cancel(struct thread *td, struct aio_cancel_args *uap)
2017 {
2018 struct proc *p = td->td_proc;
2019 struct kaioinfo *ki;
2020 struct kaiocb *job, *jobn;
2021 struct file *fp;
2022 int error;
2023 int cancelled = 0;
2024 int notcancelled = 0;
2025 struct vnode *vp;
2026
2027 /* Lookup file object. */
2028 error = fget(td, uap->fd, &cap_no_rights, &fp);
2029 if (error)
2030 return (error);
2031
2032 ki = p->p_aioinfo;
2033 if (ki == NULL)
2034 goto done;
2035
2036 if (fp->f_type == DTYPE_VNODE) {
2037 vp = fp->f_vnode;
2038 if (vn_isdisk(vp)) {
2039 fdrop(fp, td);
2040 td->td_retval[0] = AIO_NOTCANCELED;
2041 return (0);
2042 }
2043 }
2044
2045 AIO_LOCK(ki);
2046 TAILQ_FOREACH_SAFE(job, &ki->kaio_jobqueue, plist, jobn) {
2047 if ((uap->fd == job->uaiocb.aio_fildes) &&
2048 ((uap->aiocbp == NULL) ||
2049 (uap->aiocbp == job->ujob))) {
2050 if (aio_cancel_job(p, ki, job)) {
2051 cancelled++;
2052 } else {
2053 notcancelled++;
2054 }
2055 if (uap->aiocbp != NULL)
2056 break;
2057 }
2058 }
2059 AIO_UNLOCK(ki);
2060
2061 done:
2062 fdrop(fp, td);
2063
2064 if (uap->aiocbp != NULL) {
2065 if (cancelled) {
2066 td->td_retval[0] = AIO_CANCELED;
2067 return (0);
2068 }
2069 }
2070
2071 if (notcancelled) {
2072 td->td_retval[0] = AIO_NOTCANCELED;
2073 return (0);
2074 }
2075
2076 if (cancelled) {
2077 td->td_retval[0] = AIO_CANCELED;
2078 return (0);
2079 }
2080
2081 td->td_retval[0] = AIO_ALLDONE;
2082
2083 return (0);
2084 }
2085
2086 /*
2087 * aio_error is implemented in the kernel level for compatibility purposes
2088 * only. For a user mode async implementation, it would be best to do it in
2089 * a userland subroutine.
2090 */
2091 static int
kern_aio_error(struct thread * td,struct aiocb * ujob,struct aiocb_ops * ops)2092 kern_aio_error(struct thread *td, struct aiocb *ujob, struct aiocb_ops *ops)
2093 {
2094 struct proc *p = td->td_proc;
2095 struct kaiocb *job;
2096 struct kaioinfo *ki;
2097 int status;
2098
2099 ki = p->p_aioinfo;
2100 if (ki == NULL) {
2101 td->td_retval[0] = EINVAL;
2102 return (0);
2103 }
2104
2105 AIO_LOCK(ki);
2106 TAILQ_FOREACH(job, &ki->kaio_all, allist) {
2107 if (job->ujob == ujob) {
2108 if (job->jobflags & KAIOCB_FINISHED)
2109 td->td_retval[0] =
2110 job->uaiocb._aiocb_private.error;
2111 else
2112 td->td_retval[0] = EINPROGRESS;
2113 AIO_UNLOCK(ki);
2114 return (0);
2115 }
2116 }
2117 AIO_UNLOCK(ki);
2118
2119 /*
2120 * Hack for failure of aio_aqueue.
2121 */
2122 status = ops->fetch_status(ujob);
2123 if (status == -1) {
2124 td->td_retval[0] = ops->fetch_error(ujob);
2125 return (0);
2126 }
2127
2128 td->td_retval[0] = EINVAL;
2129 return (0);
2130 }
2131
2132 int
sys_aio_error(struct thread * td,struct aio_error_args * uap)2133 sys_aio_error(struct thread *td, struct aio_error_args *uap)
2134 {
2135
2136 return (kern_aio_error(td, uap->aiocbp, &aiocb_ops));
2137 }
2138
2139 /* syscall - asynchronous read from a file (REALTIME) */
2140 #ifdef COMPAT_FREEBSD6
2141 int
freebsd6_aio_read(struct thread * td,struct freebsd6_aio_read_args * uap)2142 freebsd6_aio_read(struct thread *td, struct freebsd6_aio_read_args *uap)
2143 {
2144
2145 return (aio_aqueue(td, (struct aiocb *)uap->aiocbp, NULL, LIO_READ,
2146 &aiocb_ops_osigevent));
2147 }
2148 #endif
2149
2150 int
sys_aio_read(struct thread * td,struct aio_read_args * uap)2151 sys_aio_read(struct thread *td, struct aio_read_args *uap)
2152 {
2153
2154 return (aio_aqueue(td, uap->aiocbp, NULL, LIO_READ, &aiocb_ops));
2155 }
2156
2157 int
sys_aio_readv(struct thread * td,struct aio_readv_args * uap)2158 sys_aio_readv(struct thread *td, struct aio_readv_args *uap)
2159 {
2160
2161 return (aio_aqueue(td, uap->aiocbp, NULL, LIO_READV, &aiocb_ops));
2162 }
2163
2164 /* syscall - asynchronous write to a file (REALTIME) */
2165 #ifdef COMPAT_FREEBSD6
2166 int
freebsd6_aio_write(struct thread * td,struct freebsd6_aio_write_args * uap)2167 freebsd6_aio_write(struct thread *td, struct freebsd6_aio_write_args *uap)
2168 {
2169
2170 return (aio_aqueue(td, (struct aiocb *)uap->aiocbp, NULL, LIO_WRITE,
2171 &aiocb_ops_osigevent));
2172 }
2173 #endif
2174
2175 int
sys_aio_write(struct thread * td,struct aio_write_args * uap)2176 sys_aio_write(struct thread *td, struct aio_write_args *uap)
2177 {
2178
2179 return (aio_aqueue(td, uap->aiocbp, NULL, LIO_WRITE, &aiocb_ops));
2180 }
2181
2182 int
sys_aio_writev(struct thread * td,struct aio_writev_args * uap)2183 sys_aio_writev(struct thread *td, struct aio_writev_args *uap)
2184 {
2185
2186 return (aio_aqueue(td, uap->aiocbp, NULL, LIO_WRITEV, &aiocb_ops));
2187 }
2188
2189 int
sys_aio_mlock(struct thread * td,struct aio_mlock_args * uap)2190 sys_aio_mlock(struct thread *td, struct aio_mlock_args *uap)
2191 {
2192
2193 return (aio_aqueue(td, uap->aiocbp, NULL, LIO_MLOCK, &aiocb_ops));
2194 }
2195
2196 static int
kern_lio_listio(struct thread * td,int mode,struct aiocb * const * uacb_list,struct aiocb ** acb_list,int nent,struct sigevent * sig,struct aiocb_ops * ops)2197 kern_lio_listio(struct thread *td, int mode, struct aiocb * const *uacb_list,
2198 struct aiocb **acb_list, int nent, struct sigevent *sig,
2199 struct aiocb_ops *ops)
2200 {
2201 struct proc *p = td->td_proc;
2202 struct aiocb *job;
2203 struct kaioinfo *ki;
2204 struct aioliojob *lj;
2205 struct kevent kev;
2206 int error;
2207 int nagain, nerror;
2208 int i;
2209
2210 if ((mode != LIO_NOWAIT) && (mode != LIO_WAIT))
2211 return (EINVAL);
2212
2213 if (nent < 0 || nent > max_aio_queue_per_proc)
2214 return (EINVAL);
2215
2216 if (p->p_aioinfo == NULL)
2217 aio_init_aioinfo(p);
2218
2219 ki = p->p_aioinfo;
2220
2221 lj = uma_zalloc(aiolio_zone, M_WAITOK);
2222 lj->lioj_flags = 0;
2223 lj->lioj_count = 0;
2224 lj->lioj_finished_count = 0;
2225 lj->lioj_signal.sigev_notify = SIGEV_NONE;
2226 knlist_init_mtx(&lj->klist, AIO_MTX(ki));
2227 ksiginfo_init(&lj->lioj_ksi);
2228
2229 /*
2230 * Setup signal.
2231 */
2232 if (sig && (mode == LIO_NOWAIT)) {
2233 bcopy(sig, &lj->lioj_signal, sizeof(lj->lioj_signal));
2234 if (lj->lioj_signal.sigev_notify == SIGEV_KEVENT) {
2235 /* Assume only new style KEVENT */
2236 memset(&kev, 0, sizeof(kev));
2237 kev.filter = EVFILT_LIO;
2238 kev.flags = EV_ADD | EV_ENABLE | EV_FLAG1;
2239 kev.ident = (uintptr_t)uacb_list; /* something unique */
2240 kev.data = (intptr_t)lj;
2241 /* pass user defined sigval data */
2242 kev.udata = lj->lioj_signal.sigev_value.sival_ptr;
2243 error = kqfd_register(
2244 lj->lioj_signal.sigev_notify_kqueue, &kev, td,
2245 M_WAITOK);
2246 if (error) {
2247 uma_zfree(aiolio_zone, lj);
2248 return (error);
2249 }
2250 } else if (lj->lioj_signal.sigev_notify == SIGEV_NONE) {
2251 ;
2252 } else if (lj->lioj_signal.sigev_notify == SIGEV_SIGNAL ||
2253 lj->lioj_signal.sigev_notify == SIGEV_THREAD_ID) {
2254 if (!_SIG_VALID(lj->lioj_signal.sigev_signo)) {
2255 uma_zfree(aiolio_zone, lj);
2256 return EINVAL;
2257 }
2258 lj->lioj_flags |= LIOJ_SIGNAL;
2259 } else {
2260 uma_zfree(aiolio_zone, lj);
2261 return EINVAL;
2262 }
2263 }
2264
2265 AIO_LOCK(ki);
2266 TAILQ_INSERT_TAIL(&ki->kaio_liojoblist, lj, lioj_list);
2267 /*
2268 * Add extra aiocb count to avoid the lio to be freed
2269 * by other threads doing aio_waitcomplete or aio_return,
2270 * and prevent event from being sent until we have queued
2271 * all tasks.
2272 */
2273 lj->lioj_count = 1;
2274 AIO_UNLOCK(ki);
2275
2276 /*
2277 * Get pointers to the list of I/O requests.
2278 */
2279 nagain = 0;
2280 nerror = 0;
2281 for (i = 0; i < nent; i++) {
2282 job = acb_list[i];
2283 if (job != NULL) {
2284 error = aio_aqueue(td, job, lj, LIO_NOP, ops);
2285 if (error == EAGAIN)
2286 nagain++;
2287 else if (error != 0)
2288 nerror++;
2289 }
2290 }
2291
2292 error = 0;
2293 AIO_LOCK(ki);
2294 if (mode == LIO_WAIT) {
2295 while (lj->lioj_count - 1 != lj->lioj_finished_count) {
2296 ki->kaio_flags |= KAIO_WAKEUP;
2297 error = msleep(&p->p_aioinfo, AIO_MTX(ki),
2298 PRIBIO | PCATCH, "aiospn", 0);
2299 if (error == ERESTART)
2300 error = EINTR;
2301 if (error)
2302 break;
2303 }
2304 } else {
2305 if (lj->lioj_count - 1 == lj->lioj_finished_count) {
2306 if (lj->lioj_signal.sigev_notify == SIGEV_KEVENT) {
2307 lj->lioj_flags |= LIOJ_KEVENT_POSTED;
2308 KNOTE_LOCKED(&lj->klist, 1);
2309 }
2310 if ((lj->lioj_flags & (LIOJ_SIGNAL |
2311 LIOJ_SIGNAL_POSTED)) == LIOJ_SIGNAL &&
2312 (lj->lioj_signal.sigev_notify == SIGEV_SIGNAL ||
2313 lj->lioj_signal.sigev_notify == SIGEV_THREAD_ID)) {
2314 aio_sendsig(p, &lj->lioj_signal, &lj->lioj_ksi,
2315 lj->lioj_count != 1);
2316 lj->lioj_flags |= LIOJ_SIGNAL_POSTED;
2317 }
2318 }
2319 }
2320 lj->lioj_count--;
2321 if (lj->lioj_count == 0) {
2322 TAILQ_REMOVE(&ki->kaio_liojoblist, lj, lioj_list);
2323 knlist_delete(&lj->klist, curthread, 1);
2324 PROC_LOCK(p);
2325 sigqueue_take(&lj->lioj_ksi);
2326 PROC_UNLOCK(p);
2327 AIO_UNLOCK(ki);
2328 uma_zfree(aiolio_zone, lj);
2329 } else
2330 AIO_UNLOCK(ki);
2331
2332 if (nerror)
2333 return (EIO);
2334 else if (nagain)
2335 return (EAGAIN);
2336 else
2337 return (error);
2338 }
2339
2340 /* syscall - list directed I/O (REALTIME) */
2341 #ifdef COMPAT_FREEBSD6
2342 int
freebsd6_lio_listio(struct thread * td,struct freebsd6_lio_listio_args * uap)2343 freebsd6_lio_listio(struct thread *td, struct freebsd6_lio_listio_args *uap)
2344 {
2345 struct aiocb **acb_list;
2346 struct sigevent *sigp, sig;
2347 struct osigevent osig;
2348 int error, nent;
2349
2350 if ((uap->mode != LIO_NOWAIT) && (uap->mode != LIO_WAIT))
2351 return (EINVAL);
2352
2353 nent = uap->nent;
2354 if (nent < 0 || nent > max_aio_queue_per_proc)
2355 return (EINVAL);
2356
2357 if (uap->sig && (uap->mode == LIO_NOWAIT)) {
2358 error = copyin(uap->sig, &osig, sizeof(osig));
2359 if (error)
2360 return (error);
2361 error = convert_old_sigevent(&osig, &sig);
2362 if (error)
2363 return (error);
2364 sigp = &sig;
2365 } else
2366 sigp = NULL;
2367
2368 acb_list = malloc(sizeof(struct aiocb *) * nent, M_LIO, M_WAITOK);
2369 error = copyin(uap->acb_list, acb_list, nent * sizeof(acb_list[0]));
2370 if (error == 0)
2371 error = kern_lio_listio(td, uap->mode,
2372 (struct aiocb * const *)uap->acb_list, acb_list, nent, sigp,
2373 &aiocb_ops_osigevent);
2374 free(acb_list, M_LIO);
2375 return (error);
2376 }
2377 #endif
2378
2379 /* syscall - list directed I/O (REALTIME) */
2380 int
sys_lio_listio(struct thread * td,struct lio_listio_args * uap)2381 sys_lio_listio(struct thread *td, struct lio_listio_args *uap)
2382 {
2383 struct aiocb **acb_list;
2384 struct sigevent *sigp, sig;
2385 int error, nent;
2386
2387 if ((uap->mode != LIO_NOWAIT) && (uap->mode != LIO_WAIT))
2388 return (EINVAL);
2389
2390 nent = uap->nent;
2391 if (nent < 0 || nent > max_aio_queue_per_proc)
2392 return (EINVAL);
2393
2394 if (uap->sig && (uap->mode == LIO_NOWAIT)) {
2395 error = copyin(uap->sig, &sig, sizeof(sig));
2396 if (error)
2397 return (error);
2398 sigp = &sig;
2399 } else
2400 sigp = NULL;
2401
2402 acb_list = malloc(sizeof(struct aiocb *) * nent, M_LIO, M_WAITOK);
2403 error = copyin(uap->acb_list, acb_list, nent * sizeof(acb_list[0]));
2404 if (error == 0)
2405 error = kern_lio_listio(td, uap->mode, uap->acb_list, acb_list,
2406 nent, sigp, &aiocb_ops);
2407 free(acb_list, M_LIO);
2408 return (error);
2409 }
2410
2411 static void
aio_biocleanup(struct bio * bp)2412 aio_biocleanup(struct bio *bp)
2413 {
2414 struct kaiocb *job = (struct kaiocb *)bp->bio_caller1;
2415 struct kaioinfo *ki;
2416 struct buf *pbuf = (struct buf *)bp->bio_caller2;
2417
2418 /* Release mapping into kernel space. */
2419 if (pbuf != NULL) {
2420 MPASS(pbuf->b_npages <= atop(maxphys) + 1);
2421 pmap_qremove((vm_offset_t)pbuf->b_data, pbuf->b_npages);
2422 vm_page_unhold_pages(pbuf->b_pages, pbuf->b_npages);
2423 uma_zfree(pbuf_zone, pbuf);
2424 atomic_subtract_int(&num_buf_aio, 1);
2425 ki = job->userproc->p_aioinfo;
2426 AIO_LOCK(ki);
2427 ki->kaio_buffer_count--;
2428 AIO_UNLOCK(ki);
2429 } else {
2430 MPASS(bp->bio_ma_n <= atop(maxphys) + 1);
2431 vm_page_unhold_pages(bp->bio_ma, bp->bio_ma_n);
2432 free(bp->bio_ma, M_TEMP);
2433 atomic_subtract_int(&num_unmapped_aio, 1);
2434 }
2435 g_destroy_bio(bp);
2436 }
2437
2438 static void
aio_biowakeup(struct bio * bp)2439 aio_biowakeup(struct bio *bp)
2440 {
2441 struct kaiocb *job = (struct kaiocb *)bp->bio_caller1;
2442 size_t nbytes;
2443 long bcount = bp->bio_bcount;
2444 long resid = bp->bio_resid;
2445 int opcode, nblks;
2446 int bio_error = bp->bio_error;
2447 uint16_t flags = bp->bio_flags;
2448
2449 opcode = job->uaiocb.aio_lio_opcode;
2450
2451 aio_biocleanup(bp);
2452
2453 nbytes = bcount - resid;
2454 atomic_add_acq_long(&job->nbytes, nbytes);
2455 nblks = btodb(nbytes);
2456
2457 /*
2458 * If multiple bios experienced an error, the job will reflect the
2459 * error of whichever failed bio completed last.
2460 */
2461 if (flags & BIO_ERROR)
2462 atomic_store_int(&job->error, bio_error);
2463 if (opcode & LIO_WRITE)
2464 atomic_add_int(&job->outblock, nblks);
2465 else
2466 atomic_add_int(&job->inblock, nblks);
2467
2468 if (refcount_release(&job->nbio)) {
2469 bio_error = atomic_load_int(&job->error);
2470 if (bio_error != 0)
2471 aio_complete(job, -1, bio_error);
2472 else
2473 aio_complete(job, atomic_load_long(&job->nbytes), 0);
2474 }
2475 }
2476
2477 /* syscall - wait for the next completion of an aio request */
2478 static int
kern_aio_waitcomplete(struct thread * td,struct aiocb ** ujobp,struct timespec * ts,struct aiocb_ops * ops)2479 kern_aio_waitcomplete(struct thread *td, struct aiocb **ujobp,
2480 struct timespec *ts, struct aiocb_ops *ops)
2481 {
2482 struct proc *p = td->td_proc;
2483 struct timeval atv;
2484 struct kaioinfo *ki;
2485 struct kaiocb *job;
2486 struct aiocb *ujob;
2487 long error, status;
2488 int timo;
2489
2490 ops->store_aiocb(ujobp, NULL);
2491
2492 if (ts == NULL) {
2493 timo = 0;
2494 } else if (ts->tv_sec == 0 && ts->tv_nsec == 0) {
2495 timo = -1;
2496 } else {
2497 if ((ts->tv_nsec < 0) || (ts->tv_nsec >= 1000000000))
2498 return (EINVAL);
2499
2500 TIMESPEC_TO_TIMEVAL(&atv, ts);
2501 if (itimerfix(&atv))
2502 return (EINVAL);
2503 timo = tvtohz(&atv);
2504 }
2505
2506 if (p->p_aioinfo == NULL)
2507 aio_init_aioinfo(p);
2508 ki = p->p_aioinfo;
2509
2510 error = 0;
2511 job = NULL;
2512 AIO_LOCK(ki);
2513 while ((job = TAILQ_FIRST(&ki->kaio_done)) == NULL) {
2514 if (timo == -1) {
2515 error = EWOULDBLOCK;
2516 break;
2517 }
2518 ki->kaio_flags |= KAIO_WAKEUP;
2519 error = msleep(&p->p_aioinfo, AIO_MTX(ki), PRIBIO | PCATCH,
2520 "aiowc", timo);
2521 if (timo && error == ERESTART)
2522 error = EINTR;
2523 if (error)
2524 break;
2525 }
2526
2527 if (job != NULL) {
2528 MPASS(job->jobflags & KAIOCB_FINISHED);
2529 ujob = job->ujob;
2530 status = job->uaiocb._aiocb_private.status;
2531 error = job->uaiocb._aiocb_private.error;
2532 td->td_retval[0] = status;
2533 td->td_ru.ru_oublock += job->outblock;
2534 td->td_ru.ru_inblock += job->inblock;
2535 td->td_ru.ru_msgsnd += job->msgsnd;
2536 td->td_ru.ru_msgrcv += job->msgrcv;
2537 aio_free_entry(job);
2538 AIO_UNLOCK(ki);
2539 ops->store_aiocb(ujobp, ujob);
2540 ops->store_error(ujob, error);
2541 ops->store_status(ujob, status);
2542 } else
2543 AIO_UNLOCK(ki);
2544
2545 return (error);
2546 }
2547
2548 int
sys_aio_waitcomplete(struct thread * td,struct aio_waitcomplete_args * uap)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
kern_aio_fsync(struct thread * td,int op,struct aiocb * ujob,struct aiocb_ops * ops)2567 kern_aio_fsync(struct thread *td, int op, struct aiocb *ujob,
2568 struct aiocb_ops *ops)
2569 {
2570 int listop;
2571
2572 switch (op) {
2573 case O_SYNC:
2574 listop = LIO_SYNC;
2575 break;
2576 case O_DSYNC:
2577 listop = LIO_DSYNC;
2578 break;
2579 default:
2580 return (EINVAL);
2581 }
2582
2583 return (aio_aqueue(td, ujob, NULL, listop, ops));
2584 }
2585
2586 int
sys_aio_fsync(struct thread * td,struct aio_fsync_args * uap)2587 sys_aio_fsync(struct thread *td, struct aio_fsync_args *uap)
2588 {
2589
2590 return (kern_aio_fsync(td, uap->op, uap->aiocbp, &aiocb_ops));
2591 }
2592
2593 /* kqueue attach function */
2594 static int
filt_aioattach(struct knote * kn)2595 filt_aioattach(struct knote *kn)
2596 {
2597 struct kaiocb *job;
2598
2599 job = (struct kaiocb *)(uintptr_t)kn->kn_sdata;
2600
2601 /*
2602 * The job pointer must be validated before using it, so
2603 * registration is restricted to the kernel; the user cannot
2604 * set EV_FLAG1.
2605 */
2606 if ((kn->kn_flags & EV_FLAG1) == 0)
2607 return (EPERM);
2608 kn->kn_ptr.p_aio = job;
2609 kn->kn_flags &= ~EV_FLAG1;
2610
2611 knlist_add(&job->klist, kn, 0);
2612
2613 return (0);
2614 }
2615
2616 /* kqueue detach function */
2617 static void
filt_aiodetach(struct knote * kn)2618 filt_aiodetach(struct knote *kn)
2619 {
2620 struct knlist *knl;
2621
2622 knl = &kn->kn_ptr.p_aio->klist;
2623 knl->kl_lock(knl->kl_lockarg);
2624 if (!knlist_empty(knl))
2625 knlist_remove(knl, kn, 1);
2626 knl->kl_unlock(knl->kl_lockarg);
2627 }
2628
2629 /* kqueue filter function */
2630 /*ARGSUSED*/
2631 static int
filt_aio(struct knote * kn,long hint)2632 filt_aio(struct knote *kn, long hint)
2633 {
2634 struct kaiocb *job = kn->kn_ptr.p_aio;
2635
2636 kn->kn_data = job->uaiocb._aiocb_private.error;
2637 if (!(job->jobflags & KAIOCB_FINISHED))
2638 return (0);
2639 kn->kn_flags |= EV_EOF;
2640 return (1);
2641 }
2642
2643 /* kqueue attach function */
2644 static int
filt_lioattach(struct knote * kn)2645 filt_lioattach(struct knote *kn)
2646 {
2647 struct aioliojob *lj;
2648
2649 lj = (struct aioliojob *)(uintptr_t)kn->kn_sdata;
2650
2651 /*
2652 * The aioliojob pointer must be validated before using it, so
2653 * registration is restricted to the kernel; the user cannot
2654 * set EV_FLAG1.
2655 */
2656 if ((kn->kn_flags & EV_FLAG1) == 0)
2657 return (EPERM);
2658 kn->kn_ptr.p_lio = lj;
2659 kn->kn_flags &= ~EV_FLAG1;
2660
2661 knlist_add(&lj->klist, kn, 0);
2662
2663 return (0);
2664 }
2665
2666 /* kqueue detach function */
2667 static void
filt_liodetach(struct knote * kn)2668 filt_liodetach(struct knote *kn)
2669 {
2670 struct knlist *knl;
2671
2672 knl = &kn->kn_ptr.p_lio->klist;
2673 knl->kl_lock(knl->kl_lockarg);
2674 if (!knlist_empty(knl))
2675 knlist_remove(knl, kn, 1);
2676 knl->kl_unlock(knl->kl_lockarg);
2677 }
2678
2679 /* kqueue filter function */
2680 /*ARGSUSED*/
2681 static int
filt_lio(struct knote * kn,long hint)2682 filt_lio(struct knote *kn, long hint)
2683 {
2684 struct aioliojob * lj = kn->kn_ptr.p_lio;
2685
2686 return (lj->lioj_flags & LIOJ_KEVENT_POSTED);
2687 }
2688
2689 #ifdef COMPAT_FREEBSD32
2690 #include <sys/mount.h>
2691 #include <sys/socket.h>
2692 #include <sys/sysent.h>
2693 #include <compat/freebsd32/freebsd32.h>
2694 #include <compat/freebsd32/freebsd32_proto.h>
2695 #include <compat/freebsd32/freebsd32_signal.h>
2696 #include <compat/freebsd32/freebsd32_syscall.h>
2697 #include <compat/freebsd32/freebsd32_util.h>
2698
2699 struct __aiocb_private32 {
2700 int32_t status;
2701 int32_t error;
2702 uint32_t spare;
2703 };
2704
2705 #ifdef COMPAT_FREEBSD6
2706 typedef struct oaiocb32 {
2707 int aio_fildes; /* File descriptor */
2708 uint64_t aio_offset __packed; /* File offset for I/O */
2709 uint32_t aio_buf; /* I/O buffer in process space */
2710 uint32_t aio_nbytes; /* Number of bytes for I/O */
2711 struct osigevent32 aio_sigevent; /* Signal to deliver */
2712 int aio_lio_opcode; /* LIO opcode */
2713 int aio_reqprio; /* Request priority -- ignored */
2714 struct __aiocb_private32 _aiocb_private;
2715 } oaiocb32_t;
2716 #endif
2717
2718 typedef struct aiocb32 {
2719 int32_t aio_fildes; /* File descriptor */
2720 uint64_t aio_offset __packed; /* File offset for I/O */
2721 uint32_t aio_buf; /* I/O buffer in process space */
2722 uint32_t aio_nbytes; /* Number of bytes for I/O */
2723 int __spare__[2];
2724 uint32_t __spare2__;
2725 int aio_lio_opcode; /* LIO opcode */
2726 int aio_reqprio; /* Request priority -- ignored */
2727 struct __aiocb_private32 _aiocb_private;
2728 struct sigevent32 aio_sigevent; /* Signal to deliver */
2729 } aiocb32_t;
2730
2731 #ifdef COMPAT_FREEBSD6
2732 static int
convert_old_sigevent32(struct osigevent32 * osig,struct sigevent * nsig)2733 convert_old_sigevent32(struct osigevent32 *osig, struct sigevent *nsig)
2734 {
2735
2736 /*
2737 * Only SIGEV_NONE, SIGEV_SIGNAL, and SIGEV_KEVENT are
2738 * supported by AIO with the old sigevent structure.
2739 */
2740 CP(*osig, *nsig, sigev_notify);
2741 switch (nsig->sigev_notify) {
2742 case SIGEV_NONE:
2743 break;
2744 case SIGEV_SIGNAL:
2745 nsig->sigev_signo = osig->__sigev_u.__sigev_signo;
2746 break;
2747 case SIGEV_KEVENT:
2748 nsig->sigev_notify_kqueue =
2749 osig->__sigev_u.__sigev_notify_kqueue;
2750 PTRIN_CP(*osig, *nsig, sigev_value.sival_ptr);
2751 break;
2752 default:
2753 return (EINVAL);
2754 }
2755 return (0);
2756 }
2757
2758 static int
aiocb32_copyin_old_sigevent(struct aiocb * ujob,struct kaiocb * kjob,int type __unused)2759 aiocb32_copyin_old_sigevent(struct aiocb *ujob, struct kaiocb *kjob,
2760 int type __unused)
2761 {
2762 struct oaiocb32 job32;
2763 struct aiocb *kcb = &kjob->uaiocb;
2764 int error;
2765
2766 bzero(kcb, sizeof(struct aiocb));
2767 error = copyin(ujob, &job32, sizeof(job32));
2768 if (error)
2769 return (error);
2770
2771 /* No need to copyin aio_iov, because it did not exist in FreeBSD 6 */
2772
2773 CP(job32, *kcb, aio_fildes);
2774 CP(job32, *kcb, aio_offset);
2775 PTRIN_CP(job32, *kcb, aio_buf);
2776 CP(job32, *kcb, aio_nbytes);
2777 CP(job32, *kcb, aio_lio_opcode);
2778 CP(job32, *kcb, aio_reqprio);
2779 CP(job32, *kcb, _aiocb_private.status);
2780 CP(job32, *kcb, _aiocb_private.error);
2781 return (convert_old_sigevent32(&job32.aio_sigevent,
2782 &kcb->aio_sigevent));
2783 }
2784 #endif
2785
2786 static int
aiocb32_copyin(struct aiocb * ujob,struct kaiocb * kjob,int type)2787 aiocb32_copyin(struct aiocb *ujob, struct kaiocb *kjob, int type)
2788 {
2789 struct aiocb32 job32;
2790 struct aiocb *kcb = &kjob->uaiocb;
2791 struct iovec32 *iov32;
2792 int error;
2793
2794 error = copyin(ujob, &job32, sizeof(job32));
2795 if (error)
2796 return (error);
2797 CP(job32, *kcb, aio_fildes);
2798 CP(job32, *kcb, aio_offset);
2799 CP(job32, *kcb, aio_lio_opcode);
2800 if (type == LIO_NOP)
2801 type = kcb->aio_lio_opcode;
2802 if (type & LIO_VECTORED) {
2803 iov32 = PTRIN(job32.aio_iov);
2804 CP(job32, *kcb, aio_iovcnt);
2805 /* malloc a uio and copy in the iovec */
2806 error = freebsd32_copyinuio(iov32,
2807 kcb->aio_iovcnt, &kjob->uiop);
2808 if (error)
2809 return (error);
2810 } else {
2811 PTRIN_CP(job32, *kcb, aio_buf);
2812 CP(job32, *kcb, aio_nbytes);
2813 }
2814 CP(job32, *kcb, aio_reqprio);
2815 CP(job32, *kcb, _aiocb_private.status);
2816 CP(job32, *kcb, _aiocb_private.error);
2817 error = convert_sigevent32(&job32.aio_sigevent, &kcb->aio_sigevent);
2818
2819 return (error);
2820 }
2821
2822 static long
aiocb32_fetch_status(struct aiocb * ujob)2823 aiocb32_fetch_status(struct aiocb *ujob)
2824 {
2825 struct aiocb32 *ujob32;
2826
2827 ujob32 = (struct aiocb32 *)ujob;
2828 return (fuword32(&ujob32->_aiocb_private.status));
2829 }
2830
2831 static long
aiocb32_fetch_error(struct aiocb * ujob)2832 aiocb32_fetch_error(struct aiocb *ujob)
2833 {
2834 struct aiocb32 *ujob32;
2835
2836 ujob32 = (struct aiocb32 *)ujob;
2837 return (fuword32(&ujob32->_aiocb_private.error));
2838 }
2839
2840 static int
aiocb32_store_status(struct aiocb * ujob,long status)2841 aiocb32_store_status(struct aiocb *ujob, long status)
2842 {
2843 struct aiocb32 *ujob32;
2844
2845 ujob32 = (struct aiocb32 *)ujob;
2846 return (suword32(&ujob32->_aiocb_private.status, status));
2847 }
2848
2849 static int
aiocb32_store_error(struct aiocb * ujob,long error)2850 aiocb32_store_error(struct aiocb *ujob, long error)
2851 {
2852 struct aiocb32 *ujob32;
2853
2854 ujob32 = (struct aiocb32 *)ujob;
2855 return (suword32(&ujob32->_aiocb_private.error, error));
2856 }
2857
2858 static int
aiocb32_store_aiocb(struct aiocb ** ujobp,struct aiocb * ujob)2859 aiocb32_store_aiocb(struct aiocb **ujobp, struct aiocb *ujob)
2860 {
2861
2862 return (suword32(ujobp, (long)ujob));
2863 }
2864
2865 static struct aiocb_ops aiocb32_ops = {
2866 .aio_copyin = aiocb32_copyin,
2867 .fetch_status = aiocb32_fetch_status,
2868 .fetch_error = aiocb32_fetch_error,
2869 .store_status = aiocb32_store_status,
2870 .store_error = aiocb32_store_error,
2871 .store_aiocb = aiocb32_store_aiocb,
2872 };
2873
2874 #ifdef COMPAT_FREEBSD6
2875 static struct aiocb_ops aiocb32_ops_osigevent = {
2876 .aio_copyin = aiocb32_copyin_old_sigevent,
2877 .fetch_status = aiocb32_fetch_status,
2878 .fetch_error = aiocb32_fetch_error,
2879 .store_status = aiocb32_store_status,
2880 .store_error = aiocb32_store_error,
2881 .store_aiocb = aiocb32_store_aiocb,
2882 };
2883 #endif
2884
2885 int
freebsd32_aio_return(struct thread * td,struct freebsd32_aio_return_args * uap)2886 freebsd32_aio_return(struct thread *td, struct freebsd32_aio_return_args *uap)
2887 {
2888
2889 return (kern_aio_return(td, (struct aiocb *)uap->aiocbp, &aiocb32_ops));
2890 }
2891
2892 int
freebsd32_aio_suspend(struct thread * td,struct freebsd32_aio_suspend_args * uap)2893 freebsd32_aio_suspend(struct thread *td, struct freebsd32_aio_suspend_args *uap)
2894 {
2895 struct timespec32 ts32;
2896 struct timespec ts, *tsp;
2897 struct aiocb **ujoblist;
2898 uint32_t *ujoblist32;
2899 int error, i;
2900
2901 if (uap->nent < 0 || uap->nent > max_aio_queue_per_proc)
2902 return (EINVAL);
2903
2904 if (uap->timeout) {
2905 /* Get timespec struct. */
2906 if ((error = copyin(uap->timeout, &ts32, sizeof(ts32))) != 0)
2907 return (error);
2908 CP(ts32, ts, tv_sec);
2909 CP(ts32, ts, tv_nsec);
2910 tsp = &ts;
2911 } else
2912 tsp = NULL;
2913
2914 ujoblist = malloc(uap->nent * sizeof(ujoblist[0]), M_AIO, M_WAITOK);
2915 ujoblist32 = (uint32_t *)ujoblist;
2916 error = copyin(uap->aiocbp, ujoblist32, uap->nent *
2917 sizeof(ujoblist32[0]));
2918 if (error == 0) {
2919 for (i = uap->nent - 1; i >= 0; i--)
2920 ujoblist[i] = PTRIN(ujoblist32[i]);
2921
2922 error = kern_aio_suspend(td, uap->nent, ujoblist, tsp);
2923 }
2924 free(ujoblist, M_AIO);
2925 return (error);
2926 }
2927
2928 int
freebsd32_aio_error(struct thread * td,struct freebsd32_aio_error_args * uap)2929 freebsd32_aio_error(struct thread *td, struct freebsd32_aio_error_args *uap)
2930 {
2931
2932 return (kern_aio_error(td, (struct aiocb *)uap->aiocbp, &aiocb32_ops));
2933 }
2934
2935 #ifdef COMPAT_FREEBSD6
2936 int
freebsd6_freebsd32_aio_read(struct thread * td,struct freebsd6_freebsd32_aio_read_args * uap)2937 freebsd6_freebsd32_aio_read(struct thread *td,
2938 struct freebsd6_freebsd32_aio_read_args *uap)
2939 {
2940
2941 return (aio_aqueue(td, (struct aiocb *)uap->aiocbp, NULL, LIO_READ,
2942 &aiocb32_ops_osigevent));
2943 }
2944 #endif
2945
2946 int
freebsd32_aio_read(struct thread * td,struct freebsd32_aio_read_args * uap)2947 freebsd32_aio_read(struct thread *td, struct freebsd32_aio_read_args *uap)
2948 {
2949
2950 return (aio_aqueue(td, (struct aiocb *)uap->aiocbp, NULL, LIO_READ,
2951 &aiocb32_ops));
2952 }
2953
2954 int
freebsd32_aio_readv(struct thread * td,struct freebsd32_aio_readv_args * uap)2955 freebsd32_aio_readv(struct thread *td, struct freebsd32_aio_readv_args *uap)
2956 {
2957
2958 return (aio_aqueue(td, (struct aiocb *)uap->aiocbp, NULL, LIO_READV,
2959 &aiocb32_ops));
2960 }
2961
2962 #ifdef COMPAT_FREEBSD6
2963 int
freebsd6_freebsd32_aio_write(struct thread * td,struct freebsd6_freebsd32_aio_write_args * uap)2964 freebsd6_freebsd32_aio_write(struct thread *td,
2965 struct freebsd6_freebsd32_aio_write_args *uap)
2966 {
2967
2968 return (aio_aqueue(td, (struct aiocb *)uap->aiocbp, NULL, LIO_WRITE,
2969 &aiocb32_ops_osigevent));
2970 }
2971 #endif
2972
2973 int
freebsd32_aio_write(struct thread * td,struct freebsd32_aio_write_args * uap)2974 freebsd32_aio_write(struct thread *td, struct freebsd32_aio_write_args *uap)
2975 {
2976
2977 return (aio_aqueue(td, (struct aiocb *)uap->aiocbp, NULL, LIO_WRITE,
2978 &aiocb32_ops));
2979 }
2980
2981 int
freebsd32_aio_writev(struct thread * td,struct freebsd32_aio_writev_args * uap)2982 freebsd32_aio_writev(struct thread *td, struct freebsd32_aio_writev_args *uap)
2983 {
2984
2985 return (aio_aqueue(td, (struct aiocb *)uap->aiocbp, NULL, LIO_WRITEV,
2986 &aiocb32_ops));
2987 }
2988
2989 int
freebsd32_aio_mlock(struct thread * td,struct freebsd32_aio_mlock_args * uap)2990 freebsd32_aio_mlock(struct thread *td, struct freebsd32_aio_mlock_args *uap)
2991 {
2992
2993 return (aio_aqueue(td, (struct aiocb *)uap->aiocbp, NULL, LIO_MLOCK,
2994 &aiocb32_ops));
2995 }
2996
2997 int
freebsd32_aio_waitcomplete(struct thread * td,struct freebsd32_aio_waitcomplete_args * uap)2998 freebsd32_aio_waitcomplete(struct thread *td,
2999 struct freebsd32_aio_waitcomplete_args *uap)
3000 {
3001 struct timespec32 ts32;
3002 struct timespec ts, *tsp;
3003 int error;
3004
3005 if (uap->timeout) {
3006 /* Get timespec struct. */
3007 error = copyin(uap->timeout, &ts32, sizeof(ts32));
3008 if (error)
3009 return (error);
3010 CP(ts32, ts, tv_sec);
3011 CP(ts32, ts, tv_nsec);
3012 tsp = &ts;
3013 } else
3014 tsp = NULL;
3015
3016 return (kern_aio_waitcomplete(td, (struct aiocb **)uap->aiocbp, tsp,
3017 &aiocb32_ops));
3018 }
3019
3020 int
freebsd32_aio_fsync(struct thread * td,struct freebsd32_aio_fsync_args * uap)3021 freebsd32_aio_fsync(struct thread *td, struct freebsd32_aio_fsync_args *uap)
3022 {
3023
3024 return (kern_aio_fsync(td, uap->op, (struct aiocb *)uap->aiocbp,
3025 &aiocb32_ops));
3026 }
3027
3028 #ifdef COMPAT_FREEBSD6
3029 int
freebsd6_freebsd32_lio_listio(struct thread * td,struct freebsd6_freebsd32_lio_listio_args * uap)3030 freebsd6_freebsd32_lio_listio(struct thread *td,
3031 struct freebsd6_freebsd32_lio_listio_args *uap)
3032 {
3033 struct aiocb **acb_list;
3034 struct sigevent *sigp, sig;
3035 struct osigevent32 osig;
3036 uint32_t *acb_list32;
3037 int error, i, nent;
3038
3039 if ((uap->mode != LIO_NOWAIT) && (uap->mode != LIO_WAIT))
3040 return (EINVAL);
3041
3042 nent = uap->nent;
3043 if (nent < 0 || nent > max_aio_queue_per_proc)
3044 return (EINVAL);
3045
3046 if (uap->sig && (uap->mode == LIO_NOWAIT)) {
3047 error = copyin(uap->sig, &osig, sizeof(osig));
3048 if (error)
3049 return (error);
3050 error = convert_old_sigevent32(&osig, &sig);
3051 if (error)
3052 return (error);
3053 sigp = &sig;
3054 } else
3055 sigp = NULL;
3056
3057 acb_list32 = malloc(sizeof(uint32_t) * nent, M_LIO, M_WAITOK);
3058 error = copyin(uap->acb_list, acb_list32, nent * sizeof(uint32_t));
3059 if (error) {
3060 free(acb_list32, M_LIO);
3061 return (error);
3062 }
3063 acb_list = malloc(sizeof(struct aiocb *) * nent, M_LIO, M_WAITOK);
3064 for (i = 0; i < nent; i++)
3065 acb_list[i] = PTRIN(acb_list32[i]);
3066 free(acb_list32, M_LIO);
3067
3068 error = kern_lio_listio(td, uap->mode,
3069 (struct aiocb * const *)uap->acb_list, acb_list, nent, sigp,
3070 &aiocb32_ops_osigevent);
3071 free(acb_list, M_LIO);
3072 return (error);
3073 }
3074 #endif
3075
3076 int
freebsd32_lio_listio(struct thread * td,struct freebsd32_lio_listio_args * uap)3077 freebsd32_lio_listio(struct thread *td, struct freebsd32_lio_listio_args *uap)
3078 {
3079 struct aiocb **acb_list;
3080 struct sigevent *sigp, sig;
3081 struct sigevent32 sig32;
3082 uint32_t *acb_list32;
3083 int error, i, nent;
3084
3085 if ((uap->mode != LIO_NOWAIT) && (uap->mode != LIO_WAIT))
3086 return (EINVAL);
3087
3088 nent = uap->nent;
3089 if (nent < 0 || nent > max_aio_queue_per_proc)
3090 return (EINVAL);
3091
3092 if (uap->sig && (uap->mode == LIO_NOWAIT)) {
3093 error = copyin(uap->sig, &sig32, sizeof(sig32));
3094 if (error)
3095 return (error);
3096 error = convert_sigevent32(&sig32, &sig);
3097 if (error)
3098 return (error);
3099 sigp = &sig;
3100 } else
3101 sigp = NULL;
3102
3103 acb_list32 = malloc(sizeof(uint32_t) * nent, M_LIO, M_WAITOK);
3104 error = copyin(uap->acb_list, acb_list32, nent * sizeof(uint32_t));
3105 if (error) {
3106 free(acb_list32, M_LIO);
3107 return (error);
3108 }
3109 acb_list = malloc(sizeof(struct aiocb *) * nent, M_LIO, M_WAITOK);
3110 for (i = 0; i < nent; i++)
3111 acb_list[i] = PTRIN(acb_list32[i]);
3112 free(acb_list32, M_LIO);
3113
3114 error = kern_lio_listio(td, uap->mode,
3115 (struct aiocb * const *)uap->acb_list, acb_list, nent, sigp,
3116 &aiocb32_ops);
3117 free(acb_list, M_LIO);
3118 return (error);
3119 }
3120
3121 #endif
3122