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