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