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