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