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