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