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