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 | CTLFLAG_MPSAFE, 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) listio flags */ 252 int lioj_finished_count; /* (a) listio flags */ 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 (*aio_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) 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 |= KSI_EXT | KSI_INS; 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); 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); 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 lj->lioj_flags |= LIOJ_SIGNAL_POSTED; 914 } 915 } 916 917 notification_done: 918 if (job->jobflags & KAIOCB_CHECKSYNC) { 919 schedule_fsync = false; 920 TAILQ_FOREACH_SAFE(sjob, &ki->kaio_syncqueue, list, sjobn) { 921 if (job->fd_file != sjob->fd_file || 922 job->seqno >= sjob->seqno) 923 continue; 924 if (--sjob->pending > 0) 925 continue; 926 TAILQ_REMOVE(&ki->kaio_syncqueue, sjob, list); 927 if (!aio_clear_cancel_function_locked(sjob)) 928 continue; 929 TAILQ_INSERT_TAIL(&ki->kaio_syncready, sjob, list); 930 schedule_fsync = true; 931 } 932 if (schedule_fsync) 933 taskqueue_enqueue(taskqueue_aiod_kick, 934 &ki->kaio_sync_task); 935 } 936 if (ki->kaio_flags & KAIO_WAKEUP) { 937 ki->kaio_flags &= ~KAIO_WAKEUP; 938 wakeup(&userp->p_aioinfo); 939 } 940 } 941 942 static void 943 aio_schedule_fsync(void *context, int pending) 944 { 945 struct kaioinfo *ki; 946 struct kaiocb *job; 947 948 ki = context; 949 AIO_LOCK(ki); 950 while (!TAILQ_EMPTY(&ki->kaio_syncready)) { 951 job = TAILQ_FIRST(&ki->kaio_syncready); 952 TAILQ_REMOVE(&ki->kaio_syncready, job, list); 953 AIO_UNLOCK(ki); 954 aio_schedule(job, aio_process_sync); 955 AIO_LOCK(ki); 956 } 957 AIO_UNLOCK(ki); 958 } 959 960 bool 961 aio_cancel_cleared(struct kaiocb *job) 962 { 963 964 /* 965 * The caller should hold the same queue lock held when 966 * aio_clear_cancel_function() was called and set this flag 967 * ensuring this check sees an up-to-date value. However, 968 * there is no way to assert that. 969 */ 970 return ((job->jobflags & KAIOCB_CLEARED) != 0); 971 } 972 973 static bool 974 aio_clear_cancel_function_locked(struct kaiocb *job) 975 { 976 977 AIO_LOCK_ASSERT(job->userproc->p_aioinfo, MA_OWNED); 978 MPASS(job->cancel_fn != NULL); 979 if (job->jobflags & KAIOCB_CANCELLING) { 980 job->jobflags |= KAIOCB_CLEARED; 981 return (false); 982 } 983 job->cancel_fn = NULL; 984 return (true); 985 } 986 987 bool 988 aio_clear_cancel_function(struct kaiocb *job) 989 { 990 struct kaioinfo *ki; 991 bool ret; 992 993 ki = job->userproc->p_aioinfo; 994 AIO_LOCK(ki); 995 ret = aio_clear_cancel_function_locked(job); 996 AIO_UNLOCK(ki); 997 return (ret); 998 } 999 1000 static bool 1001 aio_set_cancel_function_locked(struct kaiocb *job, aio_cancel_fn_t *func) 1002 { 1003 1004 AIO_LOCK_ASSERT(job->userproc->p_aioinfo, MA_OWNED); 1005 if (job->jobflags & KAIOCB_CANCELLED) 1006 return (false); 1007 job->cancel_fn = func; 1008 return (true); 1009 } 1010 1011 bool 1012 aio_set_cancel_function(struct kaiocb *job, aio_cancel_fn_t *func) 1013 { 1014 struct kaioinfo *ki; 1015 bool ret; 1016 1017 ki = job->userproc->p_aioinfo; 1018 AIO_LOCK(ki); 1019 ret = aio_set_cancel_function_locked(job, func); 1020 AIO_UNLOCK(ki); 1021 return (ret); 1022 } 1023 1024 void 1025 aio_complete(struct kaiocb *job, long status, int error) 1026 { 1027 struct kaioinfo *ki; 1028 struct proc *userp; 1029 1030 job->uaiocb._aiocb_private.error = error; 1031 job->uaiocb._aiocb_private.status = status; 1032 1033 userp = job->userproc; 1034 ki = userp->p_aioinfo; 1035 1036 AIO_LOCK(ki); 1037 KASSERT(!(job->jobflags & KAIOCB_FINISHED), 1038 ("duplicate aio_complete")); 1039 job->jobflags |= KAIOCB_FINISHED; 1040 if ((job->jobflags & (KAIOCB_QUEUEING | KAIOCB_CANCELLING)) == 0) { 1041 TAILQ_REMOVE(&ki->kaio_jobqueue, job, plist); 1042 aio_bio_done_notify(userp, job); 1043 } 1044 AIO_UNLOCK(ki); 1045 } 1046 1047 void 1048 aio_cancel(struct kaiocb *job) 1049 { 1050 1051 aio_complete(job, -1, ECANCELED); 1052 } 1053 1054 void 1055 aio_switch_vmspace(struct kaiocb *job) 1056 { 1057 1058 vmspace_switch_aio(job->userproc->p_vmspace); 1059 } 1060 1061 /* 1062 * The AIO daemon, most of the actual work is done in aio_process_*, 1063 * but the setup (and address space mgmt) is done in this routine. 1064 */ 1065 static void 1066 aio_daemon(void *_id) 1067 { 1068 struct kaiocb *job; 1069 struct aioproc *aiop; 1070 struct kaioinfo *ki; 1071 struct proc *p; 1072 struct vmspace *myvm; 1073 struct thread *td = curthread; 1074 int id = (intptr_t)_id; 1075 1076 /* 1077 * Grab an extra reference on the daemon's vmspace so that it 1078 * doesn't get freed by jobs that switch to a different 1079 * vmspace. 1080 */ 1081 p = td->td_proc; 1082 myvm = vmspace_acquire_ref(p); 1083 1084 KASSERT(p->p_textvp == NULL, ("kthread has a textvp")); 1085 1086 /* 1087 * Allocate and ready the aio control info. There is one aiop structure 1088 * per daemon. 1089 */ 1090 aiop = uma_zalloc(aiop_zone, M_WAITOK); 1091 aiop->aioproc = p; 1092 aiop->aioprocflags = 0; 1093 1094 /* 1095 * Wakeup parent process. (Parent sleeps to keep from blasting away 1096 * and creating too many daemons.) 1097 */ 1098 sema_post(&aio_newproc_sem); 1099 1100 mtx_lock(&aio_job_mtx); 1101 for (;;) { 1102 /* 1103 * Take daemon off of free queue 1104 */ 1105 if (aiop->aioprocflags & AIOP_FREE) { 1106 TAILQ_REMOVE(&aio_freeproc, aiop, list); 1107 aiop->aioprocflags &= ~AIOP_FREE; 1108 } 1109 1110 /* 1111 * Check for jobs. 1112 */ 1113 while ((job = aio_selectjob(aiop)) != NULL) { 1114 mtx_unlock(&aio_job_mtx); 1115 1116 ki = job->userproc->p_aioinfo; 1117 job->handle_fn(job); 1118 1119 mtx_lock(&aio_job_mtx); 1120 /* Decrement the active job count. */ 1121 ki->kaio_active_count--; 1122 } 1123 1124 /* 1125 * Disconnect from user address space. 1126 */ 1127 if (p->p_vmspace != myvm) { 1128 mtx_unlock(&aio_job_mtx); 1129 vmspace_switch_aio(myvm); 1130 mtx_lock(&aio_job_mtx); 1131 /* 1132 * We have to restart to avoid race, we only sleep if 1133 * no job can be selected. 1134 */ 1135 continue; 1136 } 1137 1138 mtx_assert(&aio_job_mtx, MA_OWNED); 1139 1140 TAILQ_INSERT_HEAD(&aio_freeproc, aiop, list); 1141 aiop->aioprocflags |= AIOP_FREE; 1142 1143 /* 1144 * If daemon is inactive for a long time, allow it to exit, 1145 * thereby freeing resources. 1146 */ 1147 if (msleep(p, &aio_job_mtx, PRIBIO, "aiordy", 1148 aiod_lifetime) == EWOULDBLOCK && TAILQ_EMPTY(&aio_jobs) && 1149 (aiop->aioprocflags & AIOP_FREE) && 1150 num_aio_procs > target_aio_procs) 1151 break; 1152 } 1153 TAILQ_REMOVE(&aio_freeproc, aiop, list); 1154 num_aio_procs--; 1155 mtx_unlock(&aio_job_mtx); 1156 uma_zfree(aiop_zone, aiop); 1157 free_unr(aiod_unr, id); 1158 vmspace_free(myvm); 1159 1160 KASSERT(p->p_vmspace == myvm, 1161 ("AIOD: bad vmspace for exiting daemon")); 1162 KASSERT(myvm->vm_refcnt > 1, 1163 ("AIOD: bad vm refcnt for exiting daemon: %d", myvm->vm_refcnt)); 1164 kproc_exit(0); 1165 } 1166 1167 /* 1168 * Create a new AIO daemon. This is mostly a kernel-thread fork routine. The 1169 * AIO daemon modifies its environment itself. 1170 */ 1171 static int 1172 aio_newproc(int *start) 1173 { 1174 int error; 1175 struct proc *p; 1176 int id; 1177 1178 id = alloc_unr(aiod_unr); 1179 error = kproc_create(aio_daemon, (void *)(intptr_t)id, &p, 1180 RFNOWAIT, 0, "aiod%d", id); 1181 if (error == 0) { 1182 /* 1183 * Wait until daemon is started. 1184 */ 1185 sema_wait(&aio_newproc_sem); 1186 mtx_lock(&aio_job_mtx); 1187 num_aio_procs++; 1188 if (start != NULL) 1189 (*start)--; 1190 mtx_unlock(&aio_job_mtx); 1191 } else { 1192 free_unr(aiod_unr, id); 1193 } 1194 return (error); 1195 } 1196 1197 /* 1198 * Try the high-performance, low-overhead bio method for eligible 1199 * VCHR devices. This method doesn't use an aio helper thread, and 1200 * thus has very low overhead. 1201 * 1202 * Assumes that the caller, aio_aqueue(), has incremented the file 1203 * structure's reference count, preventing its deallocation for the 1204 * duration of this call. 1205 */ 1206 static int 1207 aio_qbio(struct proc *p, struct kaiocb *job) 1208 { 1209 struct aiocb *cb; 1210 struct file *fp; 1211 struct bio *bp; 1212 struct buf *pbuf; 1213 struct vnode *vp; 1214 struct cdevsw *csw; 1215 struct cdev *dev; 1216 struct kaioinfo *ki; 1217 int error, ref, poff; 1218 vm_prot_t prot; 1219 1220 cb = &job->uaiocb; 1221 fp = job->fd_file; 1222 1223 if (!(cb->aio_lio_opcode == LIO_WRITE || 1224 cb->aio_lio_opcode == LIO_READ)) 1225 return (-1); 1226 if (fp == NULL || fp->f_type != DTYPE_VNODE) 1227 return (-1); 1228 1229 vp = fp->f_vnode; 1230 if (vp->v_type != VCHR) 1231 return (-1); 1232 if (vp->v_bufobj.bo_bsize == 0) 1233 return (-1); 1234 if (cb->aio_nbytes % vp->v_bufobj.bo_bsize) 1235 return (-1); 1236 1237 ref = 0; 1238 csw = devvn_refthread(vp, &dev, &ref); 1239 if (csw == NULL) 1240 return (ENXIO); 1241 1242 if ((csw->d_flags & D_DISK) == 0) { 1243 error = -1; 1244 goto unref; 1245 } 1246 if (cb->aio_nbytes > dev->si_iosize_max) { 1247 error = -1; 1248 goto unref; 1249 } 1250 1251 ki = p->p_aioinfo; 1252 poff = (vm_offset_t)cb->aio_buf & PAGE_MASK; 1253 if ((dev->si_flags & SI_UNMAPPED) && unmapped_buf_allowed) { 1254 if (cb->aio_nbytes > MAXPHYS) { 1255 error = -1; 1256 goto unref; 1257 } 1258 1259 pbuf = NULL; 1260 } else { 1261 if (cb->aio_nbytes > MAXPHYS - poff) { 1262 error = -1; 1263 goto unref; 1264 } 1265 if (ki->kaio_buffer_count >= max_buf_aio) { 1266 error = EAGAIN; 1267 goto unref; 1268 } 1269 1270 job->pbuf = pbuf = uma_zalloc(pbuf_zone, M_WAITOK); 1271 BUF_KERNPROC(pbuf); 1272 AIO_LOCK(ki); 1273 ki->kaio_buffer_count++; 1274 AIO_UNLOCK(ki); 1275 } 1276 job->bp = bp = g_alloc_bio(); 1277 1278 bp->bio_length = cb->aio_nbytes; 1279 bp->bio_bcount = cb->aio_nbytes; 1280 bp->bio_done = aio_biowakeup; 1281 bp->bio_offset = cb->aio_offset; 1282 bp->bio_cmd = cb->aio_lio_opcode == LIO_WRITE ? BIO_WRITE : BIO_READ; 1283 bp->bio_dev = dev; 1284 bp->bio_caller1 = (void *)job; 1285 1286 prot = VM_PROT_READ; 1287 if (cb->aio_lio_opcode == LIO_READ) 1288 prot |= VM_PROT_WRITE; /* Less backwards than it looks */ 1289 job->npages = vm_fault_quick_hold_pages(&curproc->p_vmspace->vm_map, 1290 (vm_offset_t)cb->aio_buf, bp->bio_length, prot, job->pages, 1291 nitems(job->pages)); 1292 if (job->npages < 0) { 1293 error = EFAULT; 1294 goto doerror; 1295 } 1296 if (pbuf != NULL) { 1297 pmap_qenter((vm_offset_t)pbuf->b_data, 1298 job->pages, job->npages); 1299 bp->bio_data = pbuf->b_data + poff; 1300 atomic_add_int(&num_buf_aio, 1); 1301 } else { 1302 bp->bio_ma = job->pages; 1303 bp->bio_ma_n = job->npages; 1304 bp->bio_ma_offset = poff; 1305 bp->bio_data = unmapped_buf; 1306 bp->bio_flags |= BIO_UNMAPPED; 1307 atomic_add_int(&num_unmapped_aio, 1); 1308 } 1309 1310 /* Perform transfer. */ 1311 csw->d_strategy(bp); 1312 dev_relthread(dev, ref); 1313 return (0); 1314 1315 doerror: 1316 if (pbuf != NULL) { 1317 AIO_LOCK(ki); 1318 ki->kaio_buffer_count--; 1319 AIO_UNLOCK(ki); 1320 uma_zfree(pbuf_zone, pbuf); 1321 job->pbuf = NULL; 1322 } 1323 g_destroy_bio(bp); 1324 job->bp = NULL; 1325 unref: 1326 dev_relthread(dev, ref); 1327 return (error); 1328 } 1329 1330 #ifdef COMPAT_FREEBSD6 1331 static int 1332 convert_old_sigevent(struct osigevent *osig, struct sigevent *nsig) 1333 { 1334 1335 /* 1336 * Only SIGEV_NONE, SIGEV_SIGNAL, and SIGEV_KEVENT are 1337 * supported by AIO with the old sigevent structure. 1338 */ 1339 nsig->sigev_notify = osig->sigev_notify; 1340 switch (nsig->sigev_notify) { 1341 case SIGEV_NONE: 1342 break; 1343 case SIGEV_SIGNAL: 1344 nsig->sigev_signo = osig->__sigev_u.__sigev_signo; 1345 break; 1346 case SIGEV_KEVENT: 1347 nsig->sigev_notify_kqueue = 1348 osig->__sigev_u.__sigev_notify_kqueue; 1349 nsig->sigev_value.sival_ptr = osig->sigev_value.sival_ptr; 1350 break; 1351 default: 1352 return (EINVAL); 1353 } 1354 return (0); 1355 } 1356 1357 static int 1358 aiocb_copyin_old_sigevent(struct aiocb *ujob, struct aiocb *kjob) 1359 { 1360 struct oaiocb *ojob; 1361 int error; 1362 1363 bzero(kjob, sizeof(struct aiocb)); 1364 error = copyin(ujob, kjob, sizeof(struct oaiocb)); 1365 if (error) 1366 return (error); 1367 ojob = (struct oaiocb *)kjob; 1368 return (convert_old_sigevent(&ojob->aio_sigevent, &kjob->aio_sigevent)); 1369 } 1370 #endif 1371 1372 static int 1373 aiocb_copyin(struct aiocb *ujob, struct aiocb *kjob) 1374 { 1375 1376 return (copyin(ujob, kjob, sizeof(struct aiocb))); 1377 } 1378 1379 static long 1380 aiocb_fetch_status(struct aiocb *ujob) 1381 { 1382 1383 return (fuword(&ujob->_aiocb_private.status)); 1384 } 1385 1386 static long 1387 aiocb_fetch_error(struct aiocb *ujob) 1388 { 1389 1390 return (fuword(&ujob->_aiocb_private.error)); 1391 } 1392 1393 static int 1394 aiocb_store_status(struct aiocb *ujob, long status) 1395 { 1396 1397 return (suword(&ujob->_aiocb_private.status, status)); 1398 } 1399 1400 static int 1401 aiocb_store_error(struct aiocb *ujob, long error) 1402 { 1403 1404 return (suword(&ujob->_aiocb_private.error, error)); 1405 } 1406 1407 static int 1408 aiocb_store_kernelinfo(struct aiocb *ujob, long jobref) 1409 { 1410 1411 return (suword(&ujob->_aiocb_private.kernelinfo, jobref)); 1412 } 1413 1414 static int 1415 aiocb_store_aiocb(struct aiocb **ujobp, struct aiocb *ujob) 1416 { 1417 1418 return (suword(ujobp, (long)ujob)); 1419 } 1420 1421 static struct aiocb_ops aiocb_ops = { 1422 .aio_copyin = aiocb_copyin, 1423 .fetch_status = aiocb_fetch_status, 1424 .fetch_error = aiocb_fetch_error, 1425 .store_status = aiocb_store_status, 1426 .store_error = aiocb_store_error, 1427 .store_kernelinfo = aiocb_store_kernelinfo, 1428 .store_aiocb = aiocb_store_aiocb, 1429 }; 1430 1431 #ifdef COMPAT_FREEBSD6 1432 static struct aiocb_ops aiocb_ops_osigevent = { 1433 .aio_copyin = aiocb_copyin_old_sigevent, 1434 .fetch_status = aiocb_fetch_status, 1435 .fetch_error = aiocb_fetch_error, 1436 .store_status = aiocb_store_status, 1437 .store_error = aiocb_store_error, 1438 .store_kernelinfo = aiocb_store_kernelinfo, 1439 .store_aiocb = aiocb_store_aiocb, 1440 }; 1441 #endif 1442 1443 /* 1444 * Queue a new AIO request. Choosing either the threaded or direct bio VCHR 1445 * technique is done in this code. 1446 */ 1447 int 1448 aio_aqueue(struct thread *td, struct aiocb *ujob, struct aioliojob *lj, 1449 int type, struct aiocb_ops *ops) 1450 { 1451 struct proc *p = td->td_proc; 1452 struct file *fp; 1453 struct kaiocb *job; 1454 struct kaioinfo *ki; 1455 struct kevent kev; 1456 int opcode; 1457 int error; 1458 int fd, kqfd; 1459 int jid; 1460 u_short evflags; 1461 1462 if (p->p_aioinfo == NULL) 1463 aio_init_aioinfo(p); 1464 1465 ki = p->p_aioinfo; 1466 1467 ops->store_status(ujob, -1); 1468 ops->store_error(ujob, 0); 1469 ops->store_kernelinfo(ujob, -1); 1470 1471 if (num_queue_count >= max_queue_count || 1472 ki->kaio_count >= max_aio_queue_per_proc) { 1473 ops->store_error(ujob, EAGAIN); 1474 return (EAGAIN); 1475 } 1476 1477 job = uma_zalloc(aiocb_zone, M_WAITOK | M_ZERO); 1478 knlist_init_mtx(&job->klist, AIO_MTX(ki)); 1479 1480 error = ops->aio_copyin(ujob, &job->uaiocb); 1481 if (error) { 1482 ops->store_error(ujob, error); 1483 uma_zfree(aiocb_zone, job); 1484 return (error); 1485 } 1486 1487 if (job->uaiocb.aio_nbytes > IOSIZE_MAX) { 1488 uma_zfree(aiocb_zone, job); 1489 return (EINVAL); 1490 } 1491 1492 if (job->uaiocb.aio_sigevent.sigev_notify != SIGEV_KEVENT && 1493 job->uaiocb.aio_sigevent.sigev_notify != SIGEV_SIGNAL && 1494 job->uaiocb.aio_sigevent.sigev_notify != SIGEV_THREAD_ID && 1495 job->uaiocb.aio_sigevent.sigev_notify != SIGEV_NONE) { 1496 ops->store_error(ujob, EINVAL); 1497 uma_zfree(aiocb_zone, job); 1498 return (EINVAL); 1499 } 1500 1501 if ((job->uaiocb.aio_sigevent.sigev_notify == SIGEV_SIGNAL || 1502 job->uaiocb.aio_sigevent.sigev_notify == SIGEV_THREAD_ID) && 1503 !_SIG_VALID(job->uaiocb.aio_sigevent.sigev_signo)) { 1504 uma_zfree(aiocb_zone, job); 1505 return (EINVAL); 1506 } 1507 1508 ksiginfo_init(&job->ksi); 1509 1510 /* Save userspace address of the job info. */ 1511 job->ujob = ujob; 1512 1513 /* Get the opcode. */ 1514 if (type != LIO_NOP) 1515 job->uaiocb.aio_lio_opcode = type; 1516 opcode = job->uaiocb.aio_lio_opcode; 1517 1518 /* 1519 * Validate the opcode and fetch the file object for the specified 1520 * file descriptor. 1521 * 1522 * XXXRW: Moved the opcode validation up here so that we don't 1523 * retrieve a file descriptor without knowing what the capabiltity 1524 * should be. 1525 */ 1526 fd = job->uaiocb.aio_fildes; 1527 switch (opcode) { 1528 case LIO_WRITE: 1529 error = fget_write(td, fd, &cap_pwrite_rights, &fp); 1530 break; 1531 case LIO_READ: 1532 error = fget_read(td, fd, &cap_pread_rights, &fp); 1533 break; 1534 case LIO_SYNC: 1535 error = fget(td, fd, &cap_fsync_rights, &fp); 1536 break; 1537 case LIO_MLOCK: 1538 fp = NULL; 1539 break; 1540 case LIO_NOP: 1541 error = fget(td, fd, &cap_no_rights, &fp); 1542 break; 1543 default: 1544 error = EINVAL; 1545 } 1546 if (error) { 1547 uma_zfree(aiocb_zone, job); 1548 ops->store_error(ujob, error); 1549 return (error); 1550 } 1551 1552 if (opcode == LIO_SYNC && fp->f_vnode == NULL) { 1553 error = EINVAL; 1554 goto aqueue_fail; 1555 } 1556 1557 if ((opcode == LIO_READ || opcode == LIO_WRITE) && 1558 job->uaiocb.aio_offset < 0 && 1559 (fp->f_vnode == NULL || fp->f_vnode->v_type != VCHR)) { 1560 error = EINVAL; 1561 goto aqueue_fail; 1562 } 1563 1564 job->fd_file = fp; 1565 1566 mtx_lock(&aio_job_mtx); 1567 jid = jobrefid++; 1568 job->seqno = jobseqno++; 1569 mtx_unlock(&aio_job_mtx); 1570 error = ops->store_kernelinfo(ujob, jid); 1571 if (error) { 1572 error = EINVAL; 1573 goto aqueue_fail; 1574 } 1575 job->uaiocb._aiocb_private.kernelinfo = (void *)(intptr_t)jid; 1576 1577 if (opcode == LIO_NOP) { 1578 fdrop(fp, td); 1579 uma_zfree(aiocb_zone, job); 1580 return (0); 1581 } 1582 1583 if (job->uaiocb.aio_sigevent.sigev_notify != SIGEV_KEVENT) 1584 goto no_kqueue; 1585 evflags = job->uaiocb.aio_sigevent.sigev_notify_kevent_flags; 1586 if ((evflags & ~(EV_CLEAR | EV_DISPATCH | EV_ONESHOT)) != 0) { 1587 error = EINVAL; 1588 goto aqueue_fail; 1589 } 1590 kqfd = job->uaiocb.aio_sigevent.sigev_notify_kqueue; 1591 memset(&kev, 0, sizeof(kev)); 1592 kev.ident = (uintptr_t)job->ujob; 1593 kev.filter = EVFILT_AIO; 1594 kev.flags = EV_ADD | EV_ENABLE | EV_FLAG1 | evflags; 1595 kev.data = (intptr_t)job; 1596 kev.udata = job->uaiocb.aio_sigevent.sigev_value.sival_ptr; 1597 error = kqfd_register(kqfd, &kev, td, M_WAITOK); 1598 if (error) 1599 goto aqueue_fail; 1600 1601 no_kqueue: 1602 1603 ops->store_error(ujob, EINPROGRESS); 1604 job->uaiocb._aiocb_private.error = EINPROGRESS; 1605 job->userproc = p; 1606 job->cred = crhold(td->td_ucred); 1607 job->jobflags = KAIOCB_QUEUEING; 1608 job->lio = lj; 1609 1610 if (opcode == LIO_MLOCK) { 1611 aio_schedule(job, aio_process_mlock); 1612 error = 0; 1613 } else if (fp->f_ops->fo_aio_queue == NULL) 1614 error = aio_queue_file(fp, job); 1615 else 1616 error = fo_aio_queue(fp, job); 1617 if (error) 1618 goto aqueue_fail; 1619 1620 AIO_LOCK(ki); 1621 job->jobflags &= ~KAIOCB_QUEUEING; 1622 TAILQ_INSERT_TAIL(&ki->kaio_all, job, allist); 1623 ki->kaio_count++; 1624 if (lj) 1625 lj->lioj_count++; 1626 atomic_add_int(&num_queue_count, 1); 1627 if (job->jobflags & KAIOCB_FINISHED) { 1628 /* 1629 * The queue callback completed the request synchronously. 1630 * The bulk of the completion is deferred in that case 1631 * until this point. 1632 */ 1633 aio_bio_done_notify(p, job); 1634 } else 1635 TAILQ_INSERT_TAIL(&ki->kaio_jobqueue, job, plist); 1636 AIO_UNLOCK(ki); 1637 return (0); 1638 1639 aqueue_fail: 1640 knlist_delete(&job->klist, curthread, 0); 1641 if (fp) 1642 fdrop(fp, td); 1643 uma_zfree(aiocb_zone, job); 1644 ops->store_error(ujob, error); 1645 return (error); 1646 } 1647 1648 static void 1649 aio_cancel_daemon_job(struct kaiocb *job) 1650 { 1651 1652 mtx_lock(&aio_job_mtx); 1653 if (!aio_cancel_cleared(job)) 1654 TAILQ_REMOVE(&aio_jobs, job, list); 1655 mtx_unlock(&aio_job_mtx); 1656 aio_cancel(job); 1657 } 1658 1659 void 1660 aio_schedule(struct kaiocb *job, aio_handle_fn_t *func) 1661 { 1662 1663 mtx_lock(&aio_job_mtx); 1664 if (!aio_set_cancel_function(job, aio_cancel_daemon_job)) { 1665 mtx_unlock(&aio_job_mtx); 1666 aio_cancel(job); 1667 return; 1668 } 1669 job->handle_fn = func; 1670 TAILQ_INSERT_TAIL(&aio_jobs, job, list); 1671 aio_kick_nowait(job->userproc); 1672 mtx_unlock(&aio_job_mtx); 1673 } 1674 1675 static void 1676 aio_cancel_sync(struct kaiocb *job) 1677 { 1678 struct kaioinfo *ki; 1679 1680 ki = job->userproc->p_aioinfo; 1681 AIO_LOCK(ki); 1682 if (!aio_cancel_cleared(job)) 1683 TAILQ_REMOVE(&ki->kaio_syncqueue, job, list); 1684 AIO_UNLOCK(ki); 1685 aio_cancel(job); 1686 } 1687 1688 int 1689 aio_queue_file(struct file *fp, struct kaiocb *job) 1690 { 1691 struct kaioinfo *ki; 1692 struct kaiocb *job2; 1693 struct vnode *vp; 1694 struct mount *mp; 1695 int error; 1696 bool safe; 1697 1698 ki = job->userproc->p_aioinfo; 1699 error = aio_qbio(job->userproc, job); 1700 if (error >= 0) 1701 return (error); 1702 safe = false; 1703 if (fp->f_type == DTYPE_VNODE) { 1704 vp = fp->f_vnode; 1705 if (vp->v_type == VREG || vp->v_type == VDIR) { 1706 mp = fp->f_vnode->v_mount; 1707 if (mp == NULL || (mp->mnt_flag & MNT_LOCAL) != 0) 1708 safe = true; 1709 } 1710 } 1711 if (!(safe || enable_aio_unsafe)) { 1712 counted_warning(&unsafe_warningcnt, 1713 "is attempting to use unsafe AIO requests"); 1714 return (EOPNOTSUPP); 1715 } 1716 1717 switch (job->uaiocb.aio_lio_opcode) { 1718 case LIO_READ: 1719 case LIO_WRITE: 1720 aio_schedule(job, aio_process_rw); 1721 error = 0; 1722 break; 1723 case LIO_SYNC: 1724 AIO_LOCK(ki); 1725 TAILQ_FOREACH(job2, &ki->kaio_jobqueue, plist) { 1726 if (job2->fd_file == job->fd_file && 1727 job2->uaiocb.aio_lio_opcode != LIO_SYNC && 1728 job2->seqno < job->seqno) { 1729 job2->jobflags |= KAIOCB_CHECKSYNC; 1730 job->pending++; 1731 } 1732 } 1733 if (job->pending != 0) { 1734 if (!aio_set_cancel_function_locked(job, 1735 aio_cancel_sync)) { 1736 AIO_UNLOCK(ki); 1737 aio_cancel(job); 1738 return (0); 1739 } 1740 TAILQ_INSERT_TAIL(&ki->kaio_syncqueue, job, list); 1741 AIO_UNLOCK(ki); 1742 return (0); 1743 } 1744 AIO_UNLOCK(ki); 1745 aio_schedule(job, aio_process_sync); 1746 error = 0; 1747 break; 1748 default: 1749 error = EINVAL; 1750 } 1751 return (error); 1752 } 1753 1754 static void 1755 aio_kick_nowait(struct proc *userp) 1756 { 1757 struct kaioinfo *ki = userp->p_aioinfo; 1758 struct aioproc *aiop; 1759 1760 mtx_assert(&aio_job_mtx, MA_OWNED); 1761 if ((aiop = TAILQ_FIRST(&aio_freeproc)) != NULL) { 1762 TAILQ_REMOVE(&aio_freeproc, aiop, list); 1763 aiop->aioprocflags &= ~AIOP_FREE; 1764 wakeup(aiop->aioproc); 1765 } else if (num_aio_resv_start + num_aio_procs < max_aio_procs && 1766 ki->kaio_active_count + num_aio_resv_start < max_aio_per_proc) { 1767 taskqueue_enqueue(taskqueue_aiod_kick, &ki->kaio_task); 1768 } 1769 } 1770 1771 static int 1772 aio_kick(struct proc *userp) 1773 { 1774 struct kaioinfo *ki = userp->p_aioinfo; 1775 struct aioproc *aiop; 1776 int error, ret = 0; 1777 1778 mtx_assert(&aio_job_mtx, MA_OWNED); 1779 retryproc: 1780 if ((aiop = TAILQ_FIRST(&aio_freeproc)) != NULL) { 1781 TAILQ_REMOVE(&aio_freeproc, aiop, list); 1782 aiop->aioprocflags &= ~AIOP_FREE; 1783 wakeup(aiop->aioproc); 1784 } else if (num_aio_resv_start + num_aio_procs < max_aio_procs && 1785 ki->kaio_active_count + num_aio_resv_start < max_aio_per_proc) { 1786 num_aio_resv_start++; 1787 mtx_unlock(&aio_job_mtx); 1788 error = aio_newproc(&num_aio_resv_start); 1789 mtx_lock(&aio_job_mtx); 1790 if (error) { 1791 num_aio_resv_start--; 1792 goto retryproc; 1793 } 1794 } else { 1795 ret = -1; 1796 } 1797 return (ret); 1798 } 1799 1800 static void 1801 aio_kick_helper(void *context, int pending) 1802 { 1803 struct proc *userp = context; 1804 1805 mtx_lock(&aio_job_mtx); 1806 while (--pending >= 0) { 1807 if (aio_kick(userp)) 1808 break; 1809 } 1810 mtx_unlock(&aio_job_mtx); 1811 } 1812 1813 /* 1814 * Support the aio_return system call, as a side-effect, kernel resources are 1815 * released. 1816 */ 1817 static int 1818 kern_aio_return(struct thread *td, struct aiocb *ujob, struct aiocb_ops *ops) 1819 { 1820 struct proc *p = td->td_proc; 1821 struct kaiocb *job; 1822 struct kaioinfo *ki; 1823 long status, error; 1824 1825 ki = p->p_aioinfo; 1826 if (ki == NULL) 1827 return (EINVAL); 1828 AIO_LOCK(ki); 1829 TAILQ_FOREACH(job, &ki->kaio_done, plist) { 1830 if (job->ujob == ujob) 1831 break; 1832 } 1833 if (job != NULL) { 1834 MPASS(job->jobflags & KAIOCB_FINISHED); 1835 status = job->uaiocb._aiocb_private.status; 1836 error = job->uaiocb._aiocb_private.error; 1837 td->td_retval[0] = status; 1838 td->td_ru.ru_oublock += job->outblock; 1839 td->td_ru.ru_inblock += job->inblock; 1840 td->td_ru.ru_msgsnd += job->msgsnd; 1841 td->td_ru.ru_msgrcv += job->msgrcv; 1842 aio_free_entry(job); 1843 AIO_UNLOCK(ki); 1844 ops->store_error(ujob, error); 1845 ops->store_status(ujob, status); 1846 } else { 1847 error = EINVAL; 1848 AIO_UNLOCK(ki); 1849 } 1850 return (error); 1851 } 1852 1853 int 1854 sys_aio_return(struct thread *td, struct aio_return_args *uap) 1855 { 1856 1857 return (kern_aio_return(td, uap->aiocbp, &aiocb_ops)); 1858 } 1859 1860 /* 1861 * Allow a process to wakeup when any of the I/O requests are completed. 1862 */ 1863 static int 1864 kern_aio_suspend(struct thread *td, int njoblist, struct aiocb **ujoblist, 1865 struct timespec *ts) 1866 { 1867 struct proc *p = td->td_proc; 1868 struct timeval atv; 1869 struct kaioinfo *ki; 1870 struct kaiocb *firstjob, *job; 1871 int error, i, timo; 1872 1873 timo = 0; 1874 if (ts) { 1875 if (ts->tv_nsec < 0 || ts->tv_nsec >= 1000000000) 1876 return (EINVAL); 1877 1878 TIMESPEC_TO_TIMEVAL(&atv, ts); 1879 if (itimerfix(&atv)) 1880 return (EINVAL); 1881 timo = tvtohz(&atv); 1882 } 1883 1884 ki = p->p_aioinfo; 1885 if (ki == NULL) 1886 return (EAGAIN); 1887 1888 if (njoblist == 0) 1889 return (0); 1890 1891 AIO_LOCK(ki); 1892 for (;;) { 1893 firstjob = NULL; 1894 error = 0; 1895 TAILQ_FOREACH(job, &ki->kaio_all, allist) { 1896 for (i = 0; i < njoblist; i++) { 1897 if (job->ujob == ujoblist[i]) { 1898 if (firstjob == NULL) 1899 firstjob = job; 1900 if (job->jobflags & KAIOCB_FINISHED) 1901 goto RETURN; 1902 } 1903 } 1904 } 1905 /* All tasks were finished. */ 1906 if (firstjob == NULL) 1907 break; 1908 1909 ki->kaio_flags |= KAIO_WAKEUP; 1910 error = msleep(&p->p_aioinfo, AIO_MTX(ki), PRIBIO | PCATCH, 1911 "aiospn", timo); 1912 if (error == ERESTART) 1913 error = EINTR; 1914 if (error) 1915 break; 1916 } 1917 RETURN: 1918 AIO_UNLOCK(ki); 1919 return (error); 1920 } 1921 1922 int 1923 sys_aio_suspend(struct thread *td, struct aio_suspend_args *uap) 1924 { 1925 struct timespec ts, *tsp; 1926 struct aiocb **ujoblist; 1927 int error; 1928 1929 if (uap->nent < 0 || uap->nent > max_aio_queue_per_proc) 1930 return (EINVAL); 1931 1932 if (uap->timeout) { 1933 /* Get timespec struct. */ 1934 if ((error = copyin(uap->timeout, &ts, sizeof(ts))) != 0) 1935 return (error); 1936 tsp = &ts; 1937 } else 1938 tsp = NULL; 1939 1940 ujoblist = malloc(uap->nent * sizeof(ujoblist[0]), M_AIOS, M_WAITOK); 1941 error = copyin(uap->aiocbp, ujoblist, uap->nent * sizeof(ujoblist[0])); 1942 if (error == 0) 1943 error = kern_aio_suspend(td, uap->nent, ujoblist, tsp); 1944 free(ujoblist, M_AIOS); 1945 return (error); 1946 } 1947 1948 /* 1949 * aio_cancel cancels any non-bio aio operations not currently in progress. 1950 */ 1951 int 1952 sys_aio_cancel(struct thread *td, struct aio_cancel_args *uap) 1953 { 1954 struct proc *p = td->td_proc; 1955 struct kaioinfo *ki; 1956 struct kaiocb *job, *jobn; 1957 struct file *fp; 1958 int error; 1959 int cancelled = 0; 1960 int notcancelled = 0; 1961 struct vnode *vp; 1962 1963 /* Lookup file object. */ 1964 error = fget(td, uap->fd, &cap_no_rights, &fp); 1965 if (error) 1966 return (error); 1967 1968 ki = p->p_aioinfo; 1969 if (ki == NULL) 1970 goto done; 1971 1972 if (fp->f_type == DTYPE_VNODE) { 1973 vp = fp->f_vnode; 1974 if (vn_isdisk(vp)) { 1975 fdrop(fp, td); 1976 td->td_retval[0] = AIO_NOTCANCELED; 1977 return (0); 1978 } 1979 } 1980 1981 AIO_LOCK(ki); 1982 TAILQ_FOREACH_SAFE(job, &ki->kaio_jobqueue, plist, jobn) { 1983 if ((uap->fd == job->uaiocb.aio_fildes) && 1984 ((uap->aiocbp == NULL) || 1985 (uap->aiocbp == job->ujob))) { 1986 if (aio_cancel_job(p, ki, job)) { 1987 cancelled++; 1988 } else { 1989 notcancelled++; 1990 } 1991 if (uap->aiocbp != NULL) 1992 break; 1993 } 1994 } 1995 AIO_UNLOCK(ki); 1996 1997 done: 1998 fdrop(fp, td); 1999 2000 if (uap->aiocbp != NULL) { 2001 if (cancelled) { 2002 td->td_retval[0] = AIO_CANCELED; 2003 return (0); 2004 } 2005 } 2006 2007 if (notcancelled) { 2008 td->td_retval[0] = AIO_NOTCANCELED; 2009 return (0); 2010 } 2011 2012 if (cancelled) { 2013 td->td_retval[0] = AIO_CANCELED; 2014 return (0); 2015 } 2016 2017 td->td_retval[0] = AIO_ALLDONE; 2018 2019 return (0); 2020 } 2021 2022 /* 2023 * aio_error is implemented in the kernel level for compatibility purposes 2024 * only. For a user mode async implementation, it would be best to do it in 2025 * a userland subroutine. 2026 */ 2027 static int 2028 kern_aio_error(struct thread *td, struct aiocb *ujob, struct aiocb_ops *ops) 2029 { 2030 struct proc *p = td->td_proc; 2031 struct kaiocb *job; 2032 struct kaioinfo *ki; 2033 int status; 2034 2035 ki = p->p_aioinfo; 2036 if (ki == NULL) { 2037 td->td_retval[0] = EINVAL; 2038 return (0); 2039 } 2040 2041 AIO_LOCK(ki); 2042 TAILQ_FOREACH(job, &ki->kaio_all, allist) { 2043 if (job->ujob == ujob) { 2044 if (job->jobflags & KAIOCB_FINISHED) 2045 td->td_retval[0] = 2046 job->uaiocb._aiocb_private.error; 2047 else 2048 td->td_retval[0] = EINPROGRESS; 2049 AIO_UNLOCK(ki); 2050 return (0); 2051 } 2052 } 2053 AIO_UNLOCK(ki); 2054 2055 /* 2056 * Hack for failure of aio_aqueue. 2057 */ 2058 status = ops->fetch_status(ujob); 2059 if (status == -1) { 2060 td->td_retval[0] = ops->fetch_error(ujob); 2061 return (0); 2062 } 2063 2064 td->td_retval[0] = EINVAL; 2065 return (0); 2066 } 2067 2068 int 2069 sys_aio_error(struct thread *td, struct aio_error_args *uap) 2070 { 2071 2072 return (kern_aio_error(td, uap->aiocbp, &aiocb_ops)); 2073 } 2074 2075 /* syscall - asynchronous read from a file (REALTIME) */ 2076 #ifdef COMPAT_FREEBSD6 2077 int 2078 freebsd6_aio_read(struct thread *td, struct freebsd6_aio_read_args *uap) 2079 { 2080 2081 return (aio_aqueue(td, (struct aiocb *)uap->aiocbp, NULL, LIO_READ, 2082 &aiocb_ops_osigevent)); 2083 } 2084 #endif 2085 2086 int 2087 sys_aio_read(struct thread *td, struct aio_read_args *uap) 2088 { 2089 2090 return (aio_aqueue(td, uap->aiocbp, NULL, LIO_READ, &aiocb_ops)); 2091 } 2092 2093 /* syscall - asynchronous write to a file (REALTIME) */ 2094 #ifdef COMPAT_FREEBSD6 2095 int 2096 freebsd6_aio_write(struct thread *td, struct freebsd6_aio_write_args *uap) 2097 { 2098 2099 return (aio_aqueue(td, (struct aiocb *)uap->aiocbp, NULL, LIO_WRITE, 2100 &aiocb_ops_osigevent)); 2101 } 2102 #endif 2103 2104 int 2105 sys_aio_write(struct thread *td, struct aio_write_args *uap) 2106 { 2107 2108 return (aio_aqueue(td, uap->aiocbp, NULL, LIO_WRITE, &aiocb_ops)); 2109 } 2110 2111 int 2112 sys_aio_mlock(struct thread *td, struct aio_mlock_args *uap) 2113 { 2114 2115 return (aio_aqueue(td, uap->aiocbp, NULL, LIO_MLOCK, &aiocb_ops)); 2116 } 2117 2118 static int 2119 kern_lio_listio(struct thread *td, int mode, struct aiocb * const *uacb_list, 2120 struct aiocb **acb_list, int nent, struct sigevent *sig, 2121 struct aiocb_ops *ops) 2122 { 2123 struct proc *p = td->td_proc; 2124 struct aiocb *job; 2125 struct kaioinfo *ki; 2126 struct aioliojob *lj; 2127 struct kevent kev; 2128 int error; 2129 int nagain, nerror; 2130 int i; 2131 2132 if ((mode != LIO_NOWAIT) && (mode != LIO_WAIT)) 2133 return (EINVAL); 2134 2135 if (nent < 0 || nent > max_aio_queue_per_proc) 2136 return (EINVAL); 2137 2138 if (p->p_aioinfo == NULL) 2139 aio_init_aioinfo(p); 2140 2141 ki = p->p_aioinfo; 2142 2143 lj = uma_zalloc(aiolio_zone, M_WAITOK); 2144 lj->lioj_flags = 0; 2145 lj->lioj_count = 0; 2146 lj->lioj_finished_count = 0; 2147 knlist_init_mtx(&lj->klist, AIO_MTX(ki)); 2148 ksiginfo_init(&lj->lioj_ksi); 2149 2150 /* 2151 * Setup signal. 2152 */ 2153 if (sig && (mode == LIO_NOWAIT)) { 2154 bcopy(sig, &lj->lioj_signal, sizeof(lj->lioj_signal)); 2155 if (lj->lioj_signal.sigev_notify == SIGEV_KEVENT) { 2156 /* Assume only new style KEVENT */ 2157 memset(&kev, 0, sizeof(kev)); 2158 kev.filter = EVFILT_LIO; 2159 kev.flags = EV_ADD | EV_ENABLE | EV_FLAG1; 2160 kev.ident = (uintptr_t)uacb_list; /* something unique */ 2161 kev.data = (intptr_t)lj; 2162 /* pass user defined sigval data */ 2163 kev.udata = lj->lioj_signal.sigev_value.sival_ptr; 2164 error = kqfd_register( 2165 lj->lioj_signal.sigev_notify_kqueue, &kev, td, 2166 M_WAITOK); 2167 if (error) { 2168 uma_zfree(aiolio_zone, lj); 2169 return (error); 2170 } 2171 } else if (lj->lioj_signal.sigev_notify == SIGEV_NONE) { 2172 ; 2173 } else if (lj->lioj_signal.sigev_notify == SIGEV_SIGNAL || 2174 lj->lioj_signal.sigev_notify == SIGEV_THREAD_ID) { 2175 if (!_SIG_VALID(lj->lioj_signal.sigev_signo)) { 2176 uma_zfree(aiolio_zone, lj); 2177 return EINVAL; 2178 } 2179 lj->lioj_flags |= LIOJ_SIGNAL; 2180 } else { 2181 uma_zfree(aiolio_zone, lj); 2182 return EINVAL; 2183 } 2184 } 2185 2186 AIO_LOCK(ki); 2187 TAILQ_INSERT_TAIL(&ki->kaio_liojoblist, lj, lioj_list); 2188 /* 2189 * Add extra aiocb count to avoid the lio to be freed 2190 * by other threads doing aio_waitcomplete or aio_return, 2191 * and prevent event from being sent until we have queued 2192 * all tasks. 2193 */ 2194 lj->lioj_count = 1; 2195 AIO_UNLOCK(ki); 2196 2197 /* 2198 * Get pointers to the list of I/O requests. 2199 */ 2200 nagain = 0; 2201 nerror = 0; 2202 for (i = 0; i < nent; i++) { 2203 job = acb_list[i]; 2204 if (job != NULL) { 2205 error = aio_aqueue(td, job, lj, LIO_NOP, ops); 2206 if (error == EAGAIN) 2207 nagain++; 2208 else if (error != 0) 2209 nerror++; 2210 } 2211 } 2212 2213 error = 0; 2214 AIO_LOCK(ki); 2215 if (mode == LIO_WAIT) { 2216 while (lj->lioj_count - 1 != lj->lioj_finished_count) { 2217 ki->kaio_flags |= KAIO_WAKEUP; 2218 error = msleep(&p->p_aioinfo, AIO_MTX(ki), 2219 PRIBIO | PCATCH, "aiospn", 0); 2220 if (error == ERESTART) 2221 error = EINTR; 2222 if (error) 2223 break; 2224 } 2225 } else { 2226 if (lj->lioj_count - 1 == lj->lioj_finished_count) { 2227 if (lj->lioj_signal.sigev_notify == SIGEV_KEVENT) { 2228 lj->lioj_flags |= LIOJ_KEVENT_POSTED; 2229 KNOTE_LOCKED(&lj->klist, 1); 2230 } 2231 if ((lj->lioj_flags & (LIOJ_SIGNAL|LIOJ_SIGNAL_POSTED)) 2232 == LIOJ_SIGNAL 2233 && (lj->lioj_signal.sigev_notify == SIGEV_SIGNAL || 2234 lj->lioj_signal.sigev_notify == SIGEV_THREAD_ID)) { 2235 aio_sendsig(p, &lj->lioj_signal, 2236 &lj->lioj_ksi); 2237 lj->lioj_flags |= LIOJ_SIGNAL_POSTED; 2238 } 2239 } 2240 } 2241 lj->lioj_count--; 2242 if (lj->lioj_count == 0) { 2243 TAILQ_REMOVE(&ki->kaio_liojoblist, lj, lioj_list); 2244 knlist_delete(&lj->klist, curthread, 1); 2245 PROC_LOCK(p); 2246 sigqueue_take(&lj->lioj_ksi); 2247 PROC_UNLOCK(p); 2248 AIO_UNLOCK(ki); 2249 uma_zfree(aiolio_zone, lj); 2250 } else 2251 AIO_UNLOCK(ki); 2252 2253 if (nerror) 2254 return (EIO); 2255 else if (nagain) 2256 return (EAGAIN); 2257 else 2258 return (error); 2259 } 2260 2261 /* syscall - list directed I/O (REALTIME) */ 2262 #ifdef COMPAT_FREEBSD6 2263 int 2264 freebsd6_lio_listio(struct thread *td, struct freebsd6_lio_listio_args *uap) 2265 { 2266 struct aiocb **acb_list; 2267 struct sigevent *sigp, sig; 2268 struct osigevent osig; 2269 int error, nent; 2270 2271 if ((uap->mode != LIO_NOWAIT) && (uap->mode != LIO_WAIT)) 2272 return (EINVAL); 2273 2274 nent = uap->nent; 2275 if (nent < 0 || nent > max_aio_queue_per_proc) 2276 return (EINVAL); 2277 2278 if (uap->sig && (uap->mode == LIO_NOWAIT)) { 2279 error = copyin(uap->sig, &osig, sizeof(osig)); 2280 if (error) 2281 return (error); 2282 error = convert_old_sigevent(&osig, &sig); 2283 if (error) 2284 return (error); 2285 sigp = &sig; 2286 } else 2287 sigp = NULL; 2288 2289 acb_list = malloc(sizeof(struct aiocb *) * nent, M_LIO, M_WAITOK); 2290 error = copyin(uap->acb_list, acb_list, nent * sizeof(acb_list[0])); 2291 if (error == 0) 2292 error = kern_lio_listio(td, uap->mode, 2293 (struct aiocb * const *)uap->acb_list, acb_list, nent, sigp, 2294 &aiocb_ops_osigevent); 2295 free(acb_list, M_LIO); 2296 return (error); 2297 } 2298 #endif 2299 2300 /* syscall - list directed I/O (REALTIME) */ 2301 int 2302 sys_lio_listio(struct thread *td, struct lio_listio_args *uap) 2303 { 2304 struct aiocb **acb_list; 2305 struct sigevent *sigp, sig; 2306 int error, nent; 2307 2308 if ((uap->mode != LIO_NOWAIT) && (uap->mode != LIO_WAIT)) 2309 return (EINVAL); 2310 2311 nent = uap->nent; 2312 if (nent < 0 || nent > max_aio_queue_per_proc) 2313 return (EINVAL); 2314 2315 if (uap->sig && (uap->mode == LIO_NOWAIT)) { 2316 error = copyin(uap->sig, &sig, sizeof(sig)); 2317 if (error) 2318 return (error); 2319 sigp = &sig; 2320 } else 2321 sigp = NULL; 2322 2323 acb_list = malloc(sizeof(struct aiocb *) * nent, M_LIO, M_WAITOK); 2324 error = copyin(uap->acb_list, acb_list, nent * sizeof(acb_list[0])); 2325 if (error == 0) 2326 error = kern_lio_listio(td, uap->mode, uap->acb_list, acb_list, 2327 nent, sigp, &aiocb_ops); 2328 free(acb_list, M_LIO); 2329 return (error); 2330 } 2331 2332 static void 2333 aio_biowakeup(struct bio *bp) 2334 { 2335 struct kaiocb *job = (struct kaiocb *)bp->bio_caller1; 2336 struct proc *userp; 2337 struct kaioinfo *ki; 2338 size_t nbytes; 2339 int error, nblks; 2340 2341 /* Release mapping into kernel space. */ 2342 userp = job->userproc; 2343 ki = userp->p_aioinfo; 2344 if (job->pbuf) { 2345 pmap_qremove((vm_offset_t)job->pbuf->b_data, job->npages); 2346 uma_zfree(pbuf_zone, job->pbuf); 2347 job->pbuf = NULL; 2348 atomic_subtract_int(&num_buf_aio, 1); 2349 AIO_LOCK(ki); 2350 ki->kaio_buffer_count--; 2351 AIO_UNLOCK(ki); 2352 } else 2353 atomic_subtract_int(&num_unmapped_aio, 1); 2354 vm_page_unhold_pages(job->pages, job->npages); 2355 2356 bp = job->bp; 2357 job->bp = NULL; 2358 nbytes = job->uaiocb.aio_nbytes - bp->bio_resid; 2359 error = 0; 2360 if (bp->bio_flags & BIO_ERROR) 2361 error = bp->bio_error; 2362 nblks = btodb(nbytes); 2363 if (job->uaiocb.aio_lio_opcode == LIO_WRITE) 2364 job->outblock += nblks; 2365 else 2366 job->inblock += nblks; 2367 2368 if (error) 2369 aio_complete(job, -1, error); 2370 else 2371 aio_complete(job, nbytes, 0); 2372 2373 g_destroy_bio(bp); 2374 } 2375 2376 /* syscall - wait for the next completion of an aio request */ 2377 static int 2378 kern_aio_waitcomplete(struct thread *td, struct aiocb **ujobp, 2379 struct timespec *ts, struct aiocb_ops *ops) 2380 { 2381 struct proc *p = td->td_proc; 2382 struct timeval atv; 2383 struct kaioinfo *ki; 2384 struct kaiocb *job; 2385 struct aiocb *ujob; 2386 long error, status; 2387 int timo; 2388 2389 ops->store_aiocb(ujobp, NULL); 2390 2391 if (ts == NULL) { 2392 timo = 0; 2393 } else if (ts->tv_sec == 0 && ts->tv_nsec == 0) { 2394 timo = -1; 2395 } else { 2396 if ((ts->tv_nsec < 0) || (ts->tv_nsec >= 1000000000)) 2397 return (EINVAL); 2398 2399 TIMESPEC_TO_TIMEVAL(&atv, ts); 2400 if (itimerfix(&atv)) 2401 return (EINVAL); 2402 timo = tvtohz(&atv); 2403 } 2404 2405 if (p->p_aioinfo == NULL) 2406 aio_init_aioinfo(p); 2407 ki = p->p_aioinfo; 2408 2409 error = 0; 2410 job = NULL; 2411 AIO_LOCK(ki); 2412 while ((job = TAILQ_FIRST(&ki->kaio_done)) == NULL) { 2413 if (timo == -1) { 2414 error = EWOULDBLOCK; 2415 break; 2416 } 2417 ki->kaio_flags |= KAIO_WAKEUP; 2418 error = msleep(&p->p_aioinfo, AIO_MTX(ki), PRIBIO | PCATCH, 2419 "aiowc", timo); 2420 if (timo && error == ERESTART) 2421 error = EINTR; 2422 if (error) 2423 break; 2424 } 2425 2426 if (job != NULL) { 2427 MPASS(job->jobflags & KAIOCB_FINISHED); 2428 ujob = job->ujob; 2429 status = job->uaiocb._aiocb_private.status; 2430 error = job->uaiocb._aiocb_private.error; 2431 td->td_retval[0] = status; 2432 td->td_ru.ru_oublock += job->outblock; 2433 td->td_ru.ru_inblock += job->inblock; 2434 td->td_ru.ru_msgsnd += job->msgsnd; 2435 td->td_ru.ru_msgrcv += job->msgrcv; 2436 aio_free_entry(job); 2437 AIO_UNLOCK(ki); 2438 ops->store_aiocb(ujobp, ujob); 2439 ops->store_error(ujob, error); 2440 ops->store_status(ujob, status); 2441 } else 2442 AIO_UNLOCK(ki); 2443 2444 return (error); 2445 } 2446 2447 int 2448 sys_aio_waitcomplete(struct thread *td, struct aio_waitcomplete_args *uap) 2449 { 2450 struct timespec ts, *tsp; 2451 int error; 2452 2453 if (uap->timeout) { 2454 /* Get timespec struct. */ 2455 error = copyin(uap->timeout, &ts, sizeof(ts)); 2456 if (error) 2457 return (error); 2458 tsp = &ts; 2459 } else 2460 tsp = NULL; 2461 2462 return (kern_aio_waitcomplete(td, uap->aiocbp, tsp, &aiocb_ops)); 2463 } 2464 2465 static int 2466 kern_aio_fsync(struct thread *td, int op, struct aiocb *ujob, 2467 struct aiocb_ops *ops) 2468 { 2469 2470 if (op != O_SYNC) /* XXX lack of O_DSYNC */ 2471 return (EINVAL); 2472 return (aio_aqueue(td, ujob, NULL, LIO_SYNC, ops)); 2473 } 2474 2475 int 2476 sys_aio_fsync(struct thread *td, struct aio_fsync_args *uap) 2477 { 2478 2479 return (kern_aio_fsync(td, uap->op, uap->aiocbp, &aiocb_ops)); 2480 } 2481 2482 /* kqueue attach function */ 2483 static int 2484 filt_aioattach(struct knote *kn) 2485 { 2486 struct kaiocb *job; 2487 2488 job = (struct kaiocb *)(uintptr_t)kn->kn_sdata; 2489 2490 /* 2491 * The job pointer must be validated before using it, so 2492 * registration is restricted to the kernel; the user cannot 2493 * set EV_FLAG1. 2494 */ 2495 if ((kn->kn_flags & EV_FLAG1) == 0) 2496 return (EPERM); 2497 kn->kn_ptr.p_aio = job; 2498 kn->kn_flags &= ~EV_FLAG1; 2499 2500 knlist_add(&job->klist, kn, 0); 2501 2502 return (0); 2503 } 2504 2505 /* kqueue detach function */ 2506 static void 2507 filt_aiodetach(struct knote *kn) 2508 { 2509 struct knlist *knl; 2510 2511 knl = &kn->kn_ptr.p_aio->klist; 2512 knl->kl_lock(knl->kl_lockarg); 2513 if (!knlist_empty(knl)) 2514 knlist_remove(knl, kn, 1); 2515 knl->kl_unlock(knl->kl_lockarg); 2516 } 2517 2518 /* kqueue filter function */ 2519 /*ARGSUSED*/ 2520 static int 2521 filt_aio(struct knote *kn, long hint) 2522 { 2523 struct kaiocb *job = kn->kn_ptr.p_aio; 2524 2525 kn->kn_data = job->uaiocb._aiocb_private.error; 2526 if (!(job->jobflags & KAIOCB_FINISHED)) 2527 return (0); 2528 kn->kn_flags |= EV_EOF; 2529 return (1); 2530 } 2531 2532 /* kqueue attach function */ 2533 static int 2534 filt_lioattach(struct knote *kn) 2535 { 2536 struct aioliojob *lj; 2537 2538 lj = (struct aioliojob *)(uintptr_t)kn->kn_sdata; 2539 2540 /* 2541 * The aioliojob pointer must be validated before using it, so 2542 * registration is restricted to the kernel; the user cannot 2543 * set EV_FLAG1. 2544 */ 2545 if ((kn->kn_flags & EV_FLAG1) == 0) 2546 return (EPERM); 2547 kn->kn_ptr.p_lio = lj; 2548 kn->kn_flags &= ~EV_FLAG1; 2549 2550 knlist_add(&lj->klist, kn, 0); 2551 2552 return (0); 2553 } 2554 2555 /* kqueue detach function */ 2556 static void 2557 filt_liodetach(struct knote *kn) 2558 { 2559 struct knlist *knl; 2560 2561 knl = &kn->kn_ptr.p_lio->klist; 2562 knl->kl_lock(knl->kl_lockarg); 2563 if (!knlist_empty(knl)) 2564 knlist_remove(knl, kn, 1); 2565 knl->kl_unlock(knl->kl_lockarg); 2566 } 2567 2568 /* kqueue filter function */ 2569 /*ARGSUSED*/ 2570 static int 2571 filt_lio(struct knote *kn, long hint) 2572 { 2573 struct aioliojob * lj = kn->kn_ptr.p_lio; 2574 2575 return (lj->lioj_flags & LIOJ_KEVENT_POSTED); 2576 } 2577 2578 #ifdef COMPAT_FREEBSD32 2579 #include <sys/mount.h> 2580 #include <sys/socket.h> 2581 #include <compat/freebsd32/freebsd32.h> 2582 #include <compat/freebsd32/freebsd32_proto.h> 2583 #include <compat/freebsd32/freebsd32_signal.h> 2584 #include <compat/freebsd32/freebsd32_syscall.h> 2585 #include <compat/freebsd32/freebsd32_util.h> 2586 2587 struct __aiocb_private32 { 2588 int32_t status; 2589 int32_t error; 2590 uint32_t kernelinfo; 2591 }; 2592 2593 #ifdef COMPAT_FREEBSD6 2594 typedef struct oaiocb32 { 2595 int aio_fildes; /* File descriptor */ 2596 uint64_t aio_offset __packed; /* File offset for I/O */ 2597 uint32_t aio_buf; /* I/O buffer in process space */ 2598 uint32_t aio_nbytes; /* Number of bytes for I/O */ 2599 struct osigevent32 aio_sigevent; /* Signal to deliver */ 2600 int aio_lio_opcode; /* LIO opcode */ 2601 int aio_reqprio; /* Request priority -- ignored */ 2602 struct __aiocb_private32 _aiocb_private; 2603 } oaiocb32_t; 2604 #endif 2605 2606 typedef struct aiocb32 { 2607 int32_t aio_fildes; /* File descriptor */ 2608 uint64_t aio_offset __packed; /* File offset for I/O */ 2609 uint32_t aio_buf; /* I/O buffer in process space */ 2610 uint32_t aio_nbytes; /* Number of bytes for I/O */ 2611 int __spare__[2]; 2612 uint32_t __spare2__; 2613 int aio_lio_opcode; /* LIO opcode */ 2614 int aio_reqprio; /* Request priority -- ignored */ 2615 struct __aiocb_private32 _aiocb_private; 2616 struct sigevent32 aio_sigevent; /* Signal to deliver */ 2617 } aiocb32_t; 2618 2619 #ifdef COMPAT_FREEBSD6 2620 static int 2621 convert_old_sigevent32(struct osigevent32 *osig, struct sigevent *nsig) 2622 { 2623 2624 /* 2625 * Only SIGEV_NONE, SIGEV_SIGNAL, and SIGEV_KEVENT are 2626 * supported by AIO with the old sigevent structure. 2627 */ 2628 CP(*osig, *nsig, sigev_notify); 2629 switch (nsig->sigev_notify) { 2630 case SIGEV_NONE: 2631 break; 2632 case SIGEV_SIGNAL: 2633 nsig->sigev_signo = osig->__sigev_u.__sigev_signo; 2634 break; 2635 case SIGEV_KEVENT: 2636 nsig->sigev_notify_kqueue = 2637 osig->__sigev_u.__sigev_notify_kqueue; 2638 PTRIN_CP(*osig, *nsig, sigev_value.sival_ptr); 2639 break; 2640 default: 2641 return (EINVAL); 2642 } 2643 return (0); 2644 } 2645 2646 static int 2647 aiocb32_copyin_old_sigevent(struct aiocb *ujob, struct aiocb *kjob) 2648 { 2649 struct oaiocb32 job32; 2650 int error; 2651 2652 bzero(kjob, sizeof(struct aiocb)); 2653 error = copyin(ujob, &job32, sizeof(job32)); 2654 if (error) 2655 return (error); 2656 2657 CP(job32, *kjob, aio_fildes); 2658 CP(job32, *kjob, aio_offset); 2659 PTRIN_CP(job32, *kjob, aio_buf); 2660 CP(job32, *kjob, aio_nbytes); 2661 CP(job32, *kjob, aio_lio_opcode); 2662 CP(job32, *kjob, aio_reqprio); 2663 CP(job32, *kjob, _aiocb_private.status); 2664 CP(job32, *kjob, _aiocb_private.error); 2665 PTRIN_CP(job32, *kjob, _aiocb_private.kernelinfo); 2666 return (convert_old_sigevent32(&job32.aio_sigevent, 2667 &kjob->aio_sigevent)); 2668 } 2669 #endif 2670 2671 static int 2672 aiocb32_copyin(struct aiocb *ujob, struct aiocb *kjob) 2673 { 2674 struct aiocb32 job32; 2675 int error; 2676 2677 error = copyin(ujob, &job32, sizeof(job32)); 2678 if (error) 2679 return (error); 2680 CP(job32, *kjob, aio_fildes); 2681 CP(job32, *kjob, aio_offset); 2682 PTRIN_CP(job32, *kjob, aio_buf); 2683 CP(job32, *kjob, aio_nbytes); 2684 CP(job32, *kjob, aio_lio_opcode); 2685 CP(job32, *kjob, aio_reqprio); 2686 CP(job32, *kjob, _aiocb_private.status); 2687 CP(job32, *kjob, _aiocb_private.error); 2688 PTRIN_CP(job32, *kjob, _aiocb_private.kernelinfo); 2689 return (convert_sigevent32(&job32.aio_sigevent, &kjob->aio_sigevent)); 2690 } 2691 2692 static long 2693 aiocb32_fetch_status(struct aiocb *ujob) 2694 { 2695 struct aiocb32 *ujob32; 2696 2697 ujob32 = (struct aiocb32 *)ujob; 2698 return (fuword32(&ujob32->_aiocb_private.status)); 2699 } 2700 2701 static long 2702 aiocb32_fetch_error(struct aiocb *ujob) 2703 { 2704 struct aiocb32 *ujob32; 2705 2706 ujob32 = (struct aiocb32 *)ujob; 2707 return (fuword32(&ujob32->_aiocb_private.error)); 2708 } 2709 2710 static int 2711 aiocb32_store_status(struct aiocb *ujob, long status) 2712 { 2713 struct aiocb32 *ujob32; 2714 2715 ujob32 = (struct aiocb32 *)ujob; 2716 return (suword32(&ujob32->_aiocb_private.status, status)); 2717 } 2718 2719 static int 2720 aiocb32_store_error(struct aiocb *ujob, long error) 2721 { 2722 struct aiocb32 *ujob32; 2723 2724 ujob32 = (struct aiocb32 *)ujob; 2725 return (suword32(&ujob32->_aiocb_private.error, error)); 2726 } 2727 2728 static int 2729 aiocb32_store_kernelinfo(struct aiocb *ujob, long jobref) 2730 { 2731 struct aiocb32 *ujob32; 2732 2733 ujob32 = (struct aiocb32 *)ujob; 2734 return (suword32(&ujob32->_aiocb_private.kernelinfo, jobref)); 2735 } 2736 2737 static int 2738 aiocb32_store_aiocb(struct aiocb **ujobp, struct aiocb *ujob) 2739 { 2740 2741 return (suword32(ujobp, (long)ujob)); 2742 } 2743 2744 static struct aiocb_ops aiocb32_ops = { 2745 .aio_copyin = aiocb32_copyin, 2746 .fetch_status = aiocb32_fetch_status, 2747 .fetch_error = aiocb32_fetch_error, 2748 .store_status = aiocb32_store_status, 2749 .store_error = aiocb32_store_error, 2750 .store_kernelinfo = aiocb32_store_kernelinfo, 2751 .store_aiocb = aiocb32_store_aiocb, 2752 }; 2753 2754 #ifdef COMPAT_FREEBSD6 2755 static struct aiocb_ops aiocb32_ops_osigevent = { 2756 .aio_copyin = aiocb32_copyin_old_sigevent, 2757 .fetch_status = aiocb32_fetch_status, 2758 .fetch_error = aiocb32_fetch_error, 2759 .store_status = aiocb32_store_status, 2760 .store_error = aiocb32_store_error, 2761 .store_kernelinfo = aiocb32_store_kernelinfo, 2762 .store_aiocb = aiocb32_store_aiocb, 2763 }; 2764 #endif 2765 2766 int 2767 freebsd32_aio_return(struct thread *td, struct freebsd32_aio_return_args *uap) 2768 { 2769 2770 return (kern_aio_return(td, (struct aiocb *)uap->aiocbp, &aiocb32_ops)); 2771 } 2772 2773 int 2774 freebsd32_aio_suspend(struct thread *td, struct freebsd32_aio_suspend_args *uap) 2775 { 2776 struct timespec32 ts32; 2777 struct timespec ts, *tsp; 2778 struct aiocb **ujoblist; 2779 uint32_t *ujoblist32; 2780 int error, i; 2781 2782 if (uap->nent < 0 || uap->nent > max_aio_queue_per_proc) 2783 return (EINVAL); 2784 2785 if (uap->timeout) { 2786 /* Get timespec struct. */ 2787 if ((error = copyin(uap->timeout, &ts32, sizeof(ts32))) != 0) 2788 return (error); 2789 CP(ts32, ts, tv_sec); 2790 CP(ts32, ts, tv_nsec); 2791 tsp = &ts; 2792 } else 2793 tsp = NULL; 2794 2795 ujoblist = malloc(uap->nent * sizeof(ujoblist[0]), M_AIOS, M_WAITOK); 2796 ujoblist32 = (uint32_t *)ujoblist; 2797 error = copyin(uap->aiocbp, ujoblist32, uap->nent * 2798 sizeof(ujoblist32[0])); 2799 if (error == 0) { 2800 for (i = uap->nent - 1; i >= 0; i--) 2801 ujoblist[i] = PTRIN(ujoblist32[i]); 2802 2803 error = kern_aio_suspend(td, uap->nent, ujoblist, tsp); 2804 } 2805 free(ujoblist, M_AIOS); 2806 return (error); 2807 } 2808 2809 int 2810 freebsd32_aio_error(struct thread *td, struct freebsd32_aio_error_args *uap) 2811 { 2812 2813 return (kern_aio_error(td, (struct aiocb *)uap->aiocbp, &aiocb32_ops)); 2814 } 2815 2816 #ifdef COMPAT_FREEBSD6 2817 int 2818 freebsd6_freebsd32_aio_read(struct thread *td, 2819 struct freebsd6_freebsd32_aio_read_args *uap) 2820 { 2821 2822 return (aio_aqueue(td, (struct aiocb *)uap->aiocbp, NULL, LIO_READ, 2823 &aiocb32_ops_osigevent)); 2824 } 2825 #endif 2826 2827 int 2828 freebsd32_aio_read(struct thread *td, struct freebsd32_aio_read_args *uap) 2829 { 2830 2831 return (aio_aqueue(td, (struct aiocb *)uap->aiocbp, NULL, LIO_READ, 2832 &aiocb32_ops)); 2833 } 2834 2835 #ifdef COMPAT_FREEBSD6 2836 int 2837 freebsd6_freebsd32_aio_write(struct thread *td, 2838 struct freebsd6_freebsd32_aio_write_args *uap) 2839 { 2840 2841 return (aio_aqueue(td, (struct aiocb *)uap->aiocbp, NULL, LIO_WRITE, 2842 &aiocb32_ops_osigevent)); 2843 } 2844 #endif 2845 2846 int 2847 freebsd32_aio_write(struct thread *td, struct freebsd32_aio_write_args *uap) 2848 { 2849 2850 return (aio_aqueue(td, (struct aiocb *)uap->aiocbp, NULL, LIO_WRITE, 2851 &aiocb32_ops)); 2852 } 2853 2854 int 2855 freebsd32_aio_mlock(struct thread *td, struct freebsd32_aio_mlock_args *uap) 2856 { 2857 2858 return (aio_aqueue(td, (struct aiocb *)uap->aiocbp, NULL, LIO_MLOCK, 2859 &aiocb32_ops)); 2860 } 2861 2862 int 2863 freebsd32_aio_waitcomplete(struct thread *td, 2864 struct freebsd32_aio_waitcomplete_args *uap) 2865 { 2866 struct timespec32 ts32; 2867 struct timespec ts, *tsp; 2868 int error; 2869 2870 if (uap->timeout) { 2871 /* Get timespec struct. */ 2872 error = copyin(uap->timeout, &ts32, sizeof(ts32)); 2873 if (error) 2874 return (error); 2875 CP(ts32, ts, tv_sec); 2876 CP(ts32, ts, tv_nsec); 2877 tsp = &ts; 2878 } else 2879 tsp = NULL; 2880 2881 return (kern_aio_waitcomplete(td, (struct aiocb **)uap->aiocbp, tsp, 2882 &aiocb32_ops)); 2883 } 2884 2885 int 2886 freebsd32_aio_fsync(struct thread *td, struct freebsd32_aio_fsync_args *uap) 2887 { 2888 2889 return (kern_aio_fsync(td, uap->op, (struct aiocb *)uap->aiocbp, 2890 &aiocb32_ops)); 2891 } 2892 2893 #ifdef COMPAT_FREEBSD6 2894 int 2895 freebsd6_freebsd32_lio_listio(struct thread *td, 2896 struct freebsd6_freebsd32_lio_listio_args *uap) 2897 { 2898 struct aiocb **acb_list; 2899 struct sigevent *sigp, sig; 2900 struct osigevent32 osig; 2901 uint32_t *acb_list32; 2902 int error, i, nent; 2903 2904 if ((uap->mode != LIO_NOWAIT) && (uap->mode != LIO_WAIT)) 2905 return (EINVAL); 2906 2907 nent = uap->nent; 2908 if (nent < 0 || nent > max_aio_queue_per_proc) 2909 return (EINVAL); 2910 2911 if (uap->sig && (uap->mode == LIO_NOWAIT)) { 2912 error = copyin(uap->sig, &osig, sizeof(osig)); 2913 if (error) 2914 return (error); 2915 error = convert_old_sigevent32(&osig, &sig); 2916 if (error) 2917 return (error); 2918 sigp = &sig; 2919 } else 2920 sigp = NULL; 2921 2922 acb_list32 = malloc(sizeof(uint32_t) * nent, M_LIO, M_WAITOK); 2923 error = copyin(uap->acb_list, acb_list32, nent * sizeof(uint32_t)); 2924 if (error) { 2925 free(acb_list32, M_LIO); 2926 return (error); 2927 } 2928 acb_list = malloc(sizeof(struct aiocb *) * nent, M_LIO, M_WAITOK); 2929 for (i = 0; i < nent; i++) 2930 acb_list[i] = PTRIN(acb_list32[i]); 2931 free(acb_list32, M_LIO); 2932 2933 error = kern_lio_listio(td, uap->mode, 2934 (struct aiocb * const *)uap->acb_list, acb_list, nent, sigp, 2935 &aiocb32_ops_osigevent); 2936 free(acb_list, M_LIO); 2937 return (error); 2938 } 2939 #endif 2940 2941 int 2942 freebsd32_lio_listio(struct thread *td, struct freebsd32_lio_listio_args *uap) 2943 { 2944 struct aiocb **acb_list; 2945 struct sigevent *sigp, sig; 2946 struct sigevent32 sig32; 2947 uint32_t *acb_list32; 2948 int error, i, nent; 2949 2950 if ((uap->mode != LIO_NOWAIT) && (uap->mode != LIO_WAIT)) 2951 return (EINVAL); 2952 2953 nent = uap->nent; 2954 if (nent < 0 || nent > max_aio_queue_per_proc) 2955 return (EINVAL); 2956 2957 if (uap->sig && (uap->mode == LIO_NOWAIT)) { 2958 error = copyin(uap->sig, &sig32, sizeof(sig32)); 2959 if (error) 2960 return (error); 2961 error = convert_sigevent32(&sig32, &sig); 2962 if (error) 2963 return (error); 2964 sigp = &sig; 2965 } else 2966 sigp = NULL; 2967 2968 acb_list32 = malloc(sizeof(uint32_t) * nent, M_LIO, M_WAITOK); 2969 error = copyin(uap->acb_list, acb_list32, nent * sizeof(uint32_t)); 2970 if (error) { 2971 free(acb_list32, M_LIO); 2972 return (error); 2973 } 2974 acb_list = malloc(sizeof(struct aiocb *) * nent, M_LIO, M_WAITOK); 2975 for (i = 0; i < nent; i++) 2976 acb_list[i] = PTRIN(acb_list32[i]); 2977 free(acb_list32, M_LIO); 2978 2979 error = kern_lio_listio(td, uap->mode, 2980 (struct aiocb * const *)uap->acb_list, acb_list, nent, sigp, 2981 &aiocb32_ops); 2982 free(acb_list, M_LIO); 2983 return (error); 2984 } 2985 2986 #endif 2987