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