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