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