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 * $FreeBSD$ 17 */ 18 19 /* 20 * This file contains support for the POSIX 1003.1B AIO/LIO facility. 21 */ 22 23 #include <sys/param.h> 24 #include <sys/systm.h> 25 #include <sys/malloc.h> 26 #include <sys/bio.h> 27 #include <sys/buf.h> 28 #include <sys/sysproto.h> 29 #include <sys/filedesc.h> 30 #include <sys/kernel.h> 31 #include <sys/kthread.h> 32 #include <sys/fcntl.h> 33 #include <sys/file.h> 34 #include <sys/lock.h> 35 #include <sys/mutex.h> 36 #include <sys/unistd.h> 37 #include <sys/proc.h> 38 #include <sys/resourcevar.h> 39 #include <sys/signalvar.h> 40 #include <sys/protosw.h> 41 #include <sys/socketvar.h> 42 #include <sys/syscall.h> 43 #include <sys/sysent.h> 44 #include <sys/sysctl.h> 45 #include <sys/sx.h> 46 #include <sys/vnode.h> 47 #include <sys/conf.h> 48 #include <sys/event.h> 49 50 #include <posix4/posix4.h> 51 #include <vm/vm.h> 52 #include <vm/vm_extern.h> 53 #include <vm/pmap.h> 54 #include <vm/vm_map.h> 55 #include <vm/uma.h> 56 #include <sys/aio.h> 57 58 #include <machine/limits.h> 59 60 #include "opt_vfs_aio.h" 61 62 /* 63 * Counter for allocating reference ids to new jobs. Wrapped to 1 on 64 * overflow. 65 */ 66 static long jobrefid; 67 68 #define JOBST_NULL 0x0 69 #define JOBST_JOBQGLOBAL 0x2 70 #define JOBST_JOBRUNNING 0x3 71 #define JOBST_JOBFINISHED 0x4 72 #define JOBST_JOBQBUF 0x5 73 #define JOBST_JOBBFINISHED 0x6 74 75 #ifndef MAX_AIO_PER_PROC 76 #define MAX_AIO_PER_PROC 32 77 #endif 78 79 #ifndef MAX_AIO_QUEUE_PER_PROC 80 #define MAX_AIO_QUEUE_PER_PROC 256 /* Bigger than AIO_LISTIO_MAX */ 81 #endif 82 83 #ifndef MAX_AIO_PROCS 84 #define MAX_AIO_PROCS 32 85 #endif 86 87 #ifndef MAX_AIO_QUEUE 88 #define MAX_AIO_QUEUE 1024 /* Bigger than AIO_LISTIO_MAX */ 89 #endif 90 91 #ifndef TARGET_AIO_PROCS 92 #define TARGET_AIO_PROCS 4 93 #endif 94 95 #ifndef MAX_BUF_AIO 96 #define MAX_BUF_AIO 16 97 #endif 98 99 #ifndef AIOD_TIMEOUT_DEFAULT 100 #define AIOD_TIMEOUT_DEFAULT (10 * hz) 101 #endif 102 103 #ifndef AIOD_LIFETIME_DEFAULT 104 #define AIOD_LIFETIME_DEFAULT (30 * hz) 105 #endif 106 107 SYSCTL_NODE(_vfs, OID_AUTO, aio, CTLFLAG_RW, 0, "Async IO management"); 108 109 static int max_aio_procs = MAX_AIO_PROCS; 110 SYSCTL_INT(_vfs_aio, OID_AUTO, max_aio_procs, 111 CTLFLAG_RW, &max_aio_procs, 0, 112 "Maximum number of kernel threads to use for handling async IO "); 113 114 static int num_aio_procs = 0; 115 SYSCTL_INT(_vfs_aio, OID_AUTO, num_aio_procs, 116 CTLFLAG_RD, &num_aio_procs, 0, 117 "Number of presently active kernel threads for async IO"); 118 119 /* 120 * The code will adjust the actual number of AIO processes towards this 121 * number when it gets a chance. 122 */ 123 static int target_aio_procs = TARGET_AIO_PROCS; 124 SYSCTL_INT(_vfs_aio, OID_AUTO, target_aio_procs, CTLFLAG_RW, &target_aio_procs, 125 0, "Preferred number of ready kernel threads for async IO"); 126 127 static int max_queue_count = MAX_AIO_QUEUE; 128 SYSCTL_INT(_vfs_aio, OID_AUTO, max_aio_queue, CTLFLAG_RW, &max_queue_count, 0, 129 "Maximum number of aio requests to queue, globally"); 130 131 static int num_queue_count = 0; 132 SYSCTL_INT(_vfs_aio, OID_AUTO, num_queue_count, CTLFLAG_RD, &num_queue_count, 0, 133 "Number of queued aio requests"); 134 135 static int num_buf_aio = 0; 136 SYSCTL_INT(_vfs_aio, OID_AUTO, num_buf_aio, CTLFLAG_RD, &num_buf_aio, 0, 137 "Number of aio requests presently handled by the buf subsystem"); 138 139 /* Number of async I/O thread in the process of being started */ 140 /* XXX This should be local to _aio_aqueue() */ 141 static int num_aio_resv_start = 0; 142 143 static int aiod_timeout; 144 SYSCTL_INT(_vfs_aio, OID_AUTO, aiod_timeout, CTLFLAG_RW, &aiod_timeout, 0, 145 "Timeout value for synchronous aio operations"); 146 147 static int aiod_lifetime; 148 SYSCTL_INT(_vfs_aio, OID_AUTO, aiod_lifetime, CTLFLAG_RW, &aiod_lifetime, 0, 149 "Maximum lifetime for idle aiod"); 150 151 static int unloadable = 0; 152 SYSCTL_INT(_vfs_aio, OID_AUTO, unloadable, CTLFLAG_RW, &unloadable, 0, 153 "Allow unload of aio (not recommended)"); 154 155 156 static int max_aio_per_proc = MAX_AIO_PER_PROC; 157 SYSCTL_INT(_vfs_aio, OID_AUTO, max_aio_per_proc, CTLFLAG_RW, &max_aio_per_proc, 158 0, "Maximum active aio requests per process (stored in the process)"); 159 160 static int max_aio_queue_per_proc = MAX_AIO_QUEUE_PER_PROC; 161 SYSCTL_INT(_vfs_aio, OID_AUTO, max_aio_queue_per_proc, CTLFLAG_RW, 162 &max_aio_queue_per_proc, 0, 163 "Maximum queued aio requests per process (stored in the process)"); 164 165 static int max_buf_aio = MAX_BUF_AIO; 166 SYSCTL_INT(_vfs_aio, OID_AUTO, max_buf_aio, CTLFLAG_RW, &max_buf_aio, 0, 167 "Maximum buf aio requests per process (stored in the process)"); 168 169 struct aiocblist { 170 TAILQ_ENTRY(aiocblist) list; /* List of jobs */ 171 TAILQ_ENTRY(aiocblist) plist; /* List of jobs for proc */ 172 int jobflags; 173 int jobstate; 174 int inputcharge; 175 int outputcharge; 176 struct callout_handle timeouthandle; 177 struct buf *bp; /* Buffer pointer */ 178 struct proc *userproc; /* User process */ /* Not td! */ 179 struct ucred *cred; /* Active credential when created */ 180 struct file *fd_file; /* Pointer to file structure */ 181 struct aio_liojob *lio; /* Optional lio job */ 182 struct aiocb *uuaiocb; /* Pointer in userspace of aiocb */ 183 struct klist klist; /* list of knotes */ 184 struct aiocb uaiocb; /* Kernel I/O control block */ 185 }; 186 187 /* jobflags */ 188 #define AIOCBLIST_RUNDOWN 0x4 189 #define AIOCBLIST_DONE 0x10 190 191 /* 192 * AIO process info 193 */ 194 #define AIOP_FREE 0x1 /* proc on free queue */ 195 #define AIOP_SCHED 0x2 /* proc explicitly scheduled */ 196 197 struct aiothreadlist { 198 int aiothreadflags; /* AIO proc flags */ 199 TAILQ_ENTRY(aiothreadlist) list; /* List of processes */ 200 struct thread *aiothread; /* The AIO thread */ 201 }; 202 203 /* 204 * data-structure for lio signal management 205 */ 206 struct aio_liojob { 207 int lioj_flags; 208 int lioj_buffer_count; 209 int lioj_buffer_finished_count; 210 int lioj_queue_count; 211 int lioj_queue_finished_count; 212 struct sigevent lioj_signal; /* signal on all I/O done */ 213 TAILQ_ENTRY(aio_liojob) lioj_list; 214 struct kaioinfo *lioj_ki; 215 }; 216 #define LIOJ_SIGNAL 0x1 /* signal on all done (lio) */ 217 #define LIOJ_SIGNAL_POSTED 0x2 /* signal has been posted */ 218 219 /* 220 * per process aio data structure 221 */ 222 struct kaioinfo { 223 int kaio_flags; /* per process kaio flags */ 224 int kaio_maxactive_count; /* maximum number of AIOs */ 225 int kaio_active_count; /* number of currently used AIOs */ 226 int kaio_qallowed_count; /* maxiumu size of AIO queue */ 227 int kaio_queue_count; /* size of AIO queue */ 228 int kaio_ballowed_count; /* maximum number of buffers */ 229 int kaio_queue_finished_count; /* number of daemon jobs finished */ 230 int kaio_buffer_count; /* number of physio buffers */ 231 int kaio_buffer_finished_count; /* count of I/O done */ 232 struct proc *kaio_p; /* process that uses this kaio block */ 233 TAILQ_HEAD(,aio_liojob) kaio_liojoblist; /* list of lio jobs */ 234 TAILQ_HEAD(,aiocblist) kaio_jobqueue; /* job queue for process */ 235 TAILQ_HEAD(,aiocblist) kaio_jobdone; /* done queue for process */ 236 TAILQ_HEAD(,aiocblist) kaio_bufqueue; /* buffer job queue for process */ 237 TAILQ_HEAD(,aiocblist) kaio_bufdone; /* buffer done queue for process */ 238 TAILQ_HEAD(,aiocblist) kaio_sockqueue; /* queue for aios waiting on sockets */ 239 }; 240 241 #define KAIO_RUNDOWN 0x1 /* process is being run down */ 242 #define KAIO_WAKEUP 0x2 /* wakeup process when there is a significant event */ 243 244 static TAILQ_HEAD(,aiothreadlist) aio_activeproc; /* Active daemons */ 245 static TAILQ_HEAD(,aiothreadlist) aio_freeproc; /* Idle daemons */ 246 static TAILQ_HEAD(,aiocblist) aio_jobs; /* Async job list */ 247 static TAILQ_HEAD(,aiocblist) aio_bufjobs; /* Phys I/O job list */ 248 249 static void aio_init_aioinfo(struct proc *p); 250 static void aio_onceonly(void); 251 static int aio_free_entry(struct aiocblist *aiocbe); 252 static void aio_process(struct aiocblist *aiocbe); 253 static int aio_newproc(void); 254 static int aio_aqueue(struct thread *td, struct aiocb *job, int type); 255 static void aio_physwakeup(struct buf *bp); 256 static void aio_proc_rundown(struct proc *p); 257 static int aio_fphysio(struct aiocblist *aiocbe); 258 static int aio_qphysio(struct proc *p, struct aiocblist *iocb); 259 static void aio_daemon(void *uproc); 260 static void aio_swake_cb(struct socket *, struct sockbuf *); 261 static int aio_unload(void); 262 static void process_signal(void *aioj); 263 static int filt_aioattach(struct knote *kn); 264 static void filt_aiodetach(struct knote *kn); 265 static int filt_aio(struct knote *kn, long hint); 266 267 /* 268 * Zones for: 269 * kaio Per process async io info 270 * aiop async io thread data 271 * aiocb async io jobs 272 * aiol list io job pointer - internal to aio_suspend XXX 273 * aiolio list io jobs 274 */ 275 static uma_zone_t kaio_zone, aiop_zone, aiocb_zone, aiol_zone, aiolio_zone; 276 277 /* kqueue filters for aio */ 278 static struct filterops aio_filtops = 279 { 0, filt_aioattach, filt_aiodetach, filt_aio }; 280 281 /* 282 * Main operations function for use as a kernel module. 283 */ 284 static int 285 aio_modload(struct module *module, int cmd, void *arg) 286 { 287 int error = 0; 288 289 switch (cmd) { 290 case MOD_LOAD: 291 aio_onceonly(); 292 break; 293 case MOD_UNLOAD: 294 error = aio_unload(); 295 break; 296 case MOD_SHUTDOWN: 297 break; 298 default: 299 error = EINVAL; 300 break; 301 } 302 return (error); 303 } 304 305 static moduledata_t aio_mod = { 306 "aio", 307 &aio_modload, 308 NULL 309 }; 310 311 SYSCALL_MODULE_HELPER(aio_return); 312 SYSCALL_MODULE_HELPER(aio_suspend); 313 SYSCALL_MODULE_HELPER(aio_cancel); 314 SYSCALL_MODULE_HELPER(aio_error); 315 SYSCALL_MODULE_HELPER(aio_read); 316 SYSCALL_MODULE_HELPER(aio_write); 317 SYSCALL_MODULE_HELPER(aio_waitcomplete); 318 SYSCALL_MODULE_HELPER(lio_listio); 319 320 DECLARE_MODULE(aio, aio_mod, 321 SI_SUB_VFS, SI_ORDER_ANY); 322 MODULE_VERSION(aio, 1); 323 324 /* 325 * Startup initialization 326 */ 327 static void 328 aio_onceonly(void) 329 { 330 331 /* XXX: should probably just use so->callback */ 332 aio_swake = &aio_swake_cb; 333 at_exit(aio_proc_rundown); 334 at_exec(aio_proc_rundown); 335 kqueue_add_filteropts(EVFILT_AIO, &aio_filtops); 336 TAILQ_INIT(&aio_freeproc); 337 TAILQ_INIT(&aio_activeproc); 338 TAILQ_INIT(&aio_jobs); 339 TAILQ_INIT(&aio_bufjobs); 340 kaio_zone = uma_zcreate("AIO", sizeof(struct kaioinfo), NULL, NULL, 341 NULL, NULL, UMA_ALIGN_PTR, UMA_ZONE_NOFREE); 342 aiop_zone = uma_zcreate("AIOP", sizeof(struct aiothreadlist), NULL, 343 NULL, NULL, NULL, UMA_ALIGN_PTR, UMA_ZONE_NOFREE); 344 aiocb_zone = uma_zcreate("AIOCB", sizeof(struct aiocblist), NULL, NULL, 345 NULL, NULL, UMA_ALIGN_PTR, UMA_ZONE_NOFREE); 346 aiol_zone = uma_zcreate("AIOL", AIO_LISTIO_MAX*sizeof(intptr_t) , NULL, 347 NULL, NULL, NULL, UMA_ALIGN_PTR, UMA_ZONE_NOFREE); 348 aiolio_zone = uma_zcreate("AIOLIO", sizeof(struct aio_liojob), NULL, 349 NULL, NULL, NULL, UMA_ALIGN_PTR, UMA_ZONE_NOFREE); 350 aiod_timeout = AIOD_TIMEOUT_DEFAULT; 351 aiod_lifetime = AIOD_LIFETIME_DEFAULT; 352 jobrefid = 1; 353 async_io_version = _POSIX_VERSION; 354 p31b_setcfg(CTL_P1003_1B_AIO_LISTIO_MAX, AIO_LISTIO_MAX); 355 p31b_setcfg(CTL_P1003_1B_AIO_MAX, MAX_AIO_QUEUE); 356 p31b_setcfg(CTL_P1003_1B_AIO_PRIO_DELTA_MAX, 0); 357 } 358 359 /* 360 * Callback for unload of AIO when used as a module. 361 */ 362 static int 363 aio_unload(void) 364 { 365 366 /* 367 * XXX: no unloads by default, it's too dangerous. 368 * perhaps we could do it if locked out callers and then 369 * did an aio_proc_rundown() on each process. 370 */ 371 if (!unloadable) 372 return (EOPNOTSUPP); 373 374 async_io_version = 0; 375 aio_swake = NULL; 376 rm_at_exit(aio_proc_rundown); 377 rm_at_exec(aio_proc_rundown); 378 kqueue_del_filteropts(EVFILT_AIO); 379 p31b_setcfg(CTL_P1003_1B_AIO_LISTIO_MAX, -1); 380 p31b_setcfg(CTL_P1003_1B_AIO_MAX, -1); 381 p31b_setcfg(CTL_P1003_1B_AIO_PRIO_DELTA_MAX, -1); 382 return (0); 383 } 384 385 /* 386 * Init the per-process aioinfo structure. The aioinfo limits are set 387 * per-process for user limit (resource) management. 388 */ 389 static void 390 aio_init_aioinfo(struct proc *p) 391 { 392 struct kaioinfo *ki; 393 394 if (p->p_aioinfo == NULL) { 395 ki = uma_zalloc(kaio_zone, M_WAITOK); 396 p->p_aioinfo = ki; 397 ki->kaio_flags = 0; 398 ki->kaio_maxactive_count = max_aio_per_proc; 399 ki->kaio_active_count = 0; 400 ki->kaio_qallowed_count = max_aio_queue_per_proc; 401 ki->kaio_queue_count = 0; 402 ki->kaio_ballowed_count = max_buf_aio; 403 ki->kaio_buffer_count = 0; 404 ki->kaio_buffer_finished_count = 0; 405 ki->kaio_p = p; 406 TAILQ_INIT(&ki->kaio_jobdone); 407 TAILQ_INIT(&ki->kaio_jobqueue); 408 TAILQ_INIT(&ki->kaio_bufdone); 409 TAILQ_INIT(&ki->kaio_bufqueue); 410 TAILQ_INIT(&ki->kaio_liojoblist); 411 TAILQ_INIT(&ki->kaio_sockqueue); 412 } 413 414 while (num_aio_procs < target_aio_procs) 415 aio_newproc(); 416 } 417 418 /* 419 * Free a job entry. Wait for completion if it is currently active, but don't 420 * delay forever. If we delay, we return a flag that says that we have to 421 * restart the queue scan. 422 */ 423 static int 424 aio_free_entry(struct aiocblist *aiocbe) 425 { 426 struct kaioinfo *ki; 427 struct aio_liojob *lj; 428 struct proc *p; 429 int error; 430 int s; 431 432 if (aiocbe->jobstate == JOBST_NULL) 433 panic("aio_free_entry: freeing already free job"); 434 435 p = aiocbe->userproc; 436 ki = p->p_aioinfo; 437 lj = aiocbe->lio; 438 if (ki == NULL) 439 panic("aio_free_entry: missing p->p_aioinfo"); 440 441 while (aiocbe->jobstate == JOBST_JOBRUNNING) { 442 aiocbe->jobflags |= AIOCBLIST_RUNDOWN; 443 tsleep(aiocbe, PRIBIO, "jobwai", 0); 444 } 445 if (aiocbe->bp == NULL) { 446 if (ki->kaio_queue_count <= 0) 447 panic("aio_free_entry: process queue size <= 0"); 448 if (num_queue_count <= 0) 449 panic("aio_free_entry: system wide queue size <= 0"); 450 451 if (lj) { 452 lj->lioj_queue_count--; 453 if (aiocbe->jobflags & AIOCBLIST_DONE) 454 lj->lioj_queue_finished_count--; 455 } 456 ki->kaio_queue_count--; 457 if (aiocbe->jobflags & AIOCBLIST_DONE) 458 ki->kaio_queue_finished_count--; 459 num_queue_count--; 460 } else { 461 if (lj) { 462 lj->lioj_buffer_count--; 463 if (aiocbe->jobflags & AIOCBLIST_DONE) 464 lj->lioj_buffer_finished_count--; 465 } 466 if (aiocbe->jobflags & AIOCBLIST_DONE) 467 ki->kaio_buffer_finished_count--; 468 ki->kaio_buffer_count--; 469 num_buf_aio--; 470 } 471 472 /* aiocbe is going away, we need to destroy any knotes */ 473 /* XXXKSE Note the thread here is used to eventually find the 474 * owning process again, but it is also used to do a fo_close 475 * and that requires the thread. (but does it require the 476 * OWNING thread? (or maybe the running thread?) 477 * There is a semantic problem here... 478 */ 479 knote_remove(FIRST_THREAD_IN_PROC(p), &aiocbe->klist); /* XXXKSE */ 480 481 if ((ki->kaio_flags & KAIO_WAKEUP) || ((ki->kaio_flags & KAIO_RUNDOWN) 482 && ((ki->kaio_buffer_count == 0) && (ki->kaio_queue_count == 0)))) { 483 ki->kaio_flags &= ~KAIO_WAKEUP; 484 wakeup(p); 485 } 486 487 if (aiocbe->jobstate == JOBST_JOBQBUF) { 488 if ((error = aio_fphysio(aiocbe)) != 0) 489 return (error); 490 if (aiocbe->jobstate != JOBST_JOBBFINISHED) 491 panic("aio_free_entry: invalid physio finish-up state"); 492 s = splbio(); 493 TAILQ_REMOVE(&ki->kaio_bufdone, aiocbe, plist); 494 splx(s); 495 } else if (aiocbe->jobstate == JOBST_JOBQGLOBAL) { 496 s = splnet(); 497 TAILQ_REMOVE(&aio_jobs, aiocbe, list); 498 TAILQ_REMOVE(&ki->kaio_jobqueue, aiocbe, plist); 499 splx(s); 500 } else if (aiocbe->jobstate == JOBST_JOBFINISHED) 501 TAILQ_REMOVE(&ki->kaio_jobdone, aiocbe, plist); 502 else if (aiocbe->jobstate == JOBST_JOBBFINISHED) { 503 s = splbio(); 504 TAILQ_REMOVE(&ki->kaio_bufdone, aiocbe, plist); 505 splx(s); 506 if (aiocbe->bp) { 507 vunmapbuf(aiocbe->bp); 508 relpbuf(aiocbe->bp, NULL); 509 aiocbe->bp = NULL; 510 } 511 } 512 if (lj && (lj->lioj_buffer_count == 0) && (lj->lioj_queue_count == 0)) { 513 TAILQ_REMOVE(&ki->kaio_liojoblist, lj, lioj_list); 514 uma_zfree(aiolio_zone, lj); 515 } 516 aiocbe->jobstate = JOBST_NULL; 517 untimeout(process_signal, aiocbe, aiocbe->timeouthandle); 518 fdrop(aiocbe->fd_file, curthread); 519 crfree(aiocbe->cred); 520 uma_zfree(aiocb_zone, aiocbe); 521 return (0); 522 } 523 524 /* 525 * Rundown the jobs for a given process. 526 */ 527 static void 528 aio_proc_rundown(struct proc *p) 529 { 530 int s; 531 struct kaioinfo *ki; 532 struct aio_liojob *lj, *ljn; 533 struct aiocblist *aiocbe, *aiocbn; 534 struct file *fp; 535 struct socket *so; 536 537 ki = p->p_aioinfo; 538 if (ki == NULL) 539 return; 540 541 ki->kaio_flags |= LIOJ_SIGNAL_POSTED; 542 while ((ki->kaio_active_count > 0) || (ki->kaio_buffer_count > 543 ki->kaio_buffer_finished_count)) { 544 ki->kaio_flags |= KAIO_RUNDOWN; 545 if (tsleep(p, PRIBIO, "kaiowt", aiod_timeout)) 546 break; 547 } 548 549 /* 550 * Move any aio ops that are waiting on socket I/O to the normal job 551 * queues so they are cleaned up with any others. 552 */ 553 s = splnet(); 554 for (aiocbe = TAILQ_FIRST(&ki->kaio_sockqueue); aiocbe; aiocbe = 555 aiocbn) { 556 aiocbn = TAILQ_NEXT(aiocbe, plist); 557 fp = aiocbe->fd_file; 558 if (fp != NULL) { 559 so = fp->f_data; 560 TAILQ_REMOVE(&so->so_aiojobq, aiocbe, list); 561 if (TAILQ_EMPTY(&so->so_aiojobq)) { 562 so->so_snd.sb_flags &= ~SB_AIO; 563 so->so_rcv.sb_flags &= ~SB_AIO; 564 } 565 } 566 TAILQ_REMOVE(&ki->kaio_sockqueue, aiocbe, plist); 567 TAILQ_INSERT_HEAD(&aio_jobs, aiocbe, list); 568 TAILQ_INSERT_HEAD(&ki->kaio_jobqueue, aiocbe, plist); 569 } 570 splx(s); 571 572 restart1: 573 for (aiocbe = TAILQ_FIRST(&ki->kaio_jobdone); aiocbe; aiocbe = aiocbn) { 574 aiocbn = TAILQ_NEXT(aiocbe, plist); 575 if (aio_free_entry(aiocbe)) 576 goto restart1; 577 } 578 579 restart2: 580 for (aiocbe = TAILQ_FIRST(&ki->kaio_jobqueue); aiocbe; aiocbe = 581 aiocbn) { 582 aiocbn = TAILQ_NEXT(aiocbe, plist); 583 if (aio_free_entry(aiocbe)) 584 goto restart2; 585 } 586 587 /* 588 * Note the use of lots of splbio here, trying to avoid splbio for long chains 589 * of I/O. Probably unnecessary. 590 */ 591 restart3: 592 s = splbio(); 593 while (TAILQ_FIRST(&ki->kaio_bufqueue)) { 594 ki->kaio_flags |= KAIO_WAKEUP; 595 tsleep(p, PRIBIO, "aioprn", 0); 596 splx(s); 597 goto restart3; 598 } 599 splx(s); 600 601 restart4: 602 s = splbio(); 603 for (aiocbe = TAILQ_FIRST(&ki->kaio_bufdone); aiocbe; aiocbe = aiocbn) { 604 aiocbn = TAILQ_NEXT(aiocbe, plist); 605 if (aio_free_entry(aiocbe)) { 606 splx(s); 607 goto restart4; 608 } 609 } 610 splx(s); 611 612 /* 613 * If we've slept, jobs might have moved from one queue to another. 614 * Retry rundown if we didn't manage to empty the queues. 615 */ 616 if (TAILQ_FIRST(&ki->kaio_jobdone) != NULL || 617 TAILQ_FIRST(&ki->kaio_jobqueue) != NULL || 618 TAILQ_FIRST(&ki->kaio_bufqueue) != NULL || 619 TAILQ_FIRST(&ki->kaio_bufdone) != NULL) 620 goto restart1; 621 622 for (lj = TAILQ_FIRST(&ki->kaio_liojoblist); lj; lj = ljn) { 623 ljn = TAILQ_NEXT(lj, lioj_list); 624 if ((lj->lioj_buffer_count == 0) && (lj->lioj_queue_count == 625 0)) { 626 TAILQ_REMOVE(&ki->kaio_liojoblist, lj, lioj_list); 627 uma_zfree(aiolio_zone, lj); 628 } else { 629 #ifdef DIAGNOSTIC 630 printf("LIO job not cleaned up: B:%d, BF:%d, Q:%d, " 631 "QF:%d\n", lj->lioj_buffer_count, 632 lj->lioj_buffer_finished_count, 633 lj->lioj_queue_count, 634 lj->lioj_queue_finished_count); 635 #endif 636 } 637 } 638 639 uma_zfree(kaio_zone, ki); 640 p->p_aioinfo = NULL; 641 } 642 643 /* 644 * Select a job to run (called by an AIO daemon). 645 */ 646 static struct aiocblist * 647 aio_selectjob(struct aiothreadlist *aiop) 648 { 649 int s; 650 struct aiocblist *aiocbe; 651 struct kaioinfo *ki; 652 struct proc *userp; 653 654 s = splnet(); 655 for (aiocbe = TAILQ_FIRST(&aio_jobs); aiocbe; aiocbe = 656 TAILQ_NEXT(aiocbe, list)) { 657 userp = aiocbe->userproc; 658 ki = userp->p_aioinfo; 659 660 if (ki->kaio_active_count < ki->kaio_maxactive_count) { 661 TAILQ_REMOVE(&aio_jobs, aiocbe, list); 662 splx(s); 663 return (aiocbe); 664 } 665 } 666 splx(s); 667 668 return (NULL); 669 } 670 671 /* 672 * The AIO processing activity. This is the code that does the I/O request for 673 * the non-physio version of the operations. The normal vn operations are used, 674 * and this code should work in all instances for every type of file, including 675 * pipes, sockets, fifos, and regular files. 676 */ 677 static void 678 aio_process(struct aiocblist *aiocbe) 679 { 680 struct ucred *td_savedcred; 681 struct thread *td; 682 struct proc *mycp; 683 struct aiocb *cb; 684 struct file *fp; 685 struct uio auio; 686 struct iovec aiov; 687 int cnt; 688 int error; 689 int oublock_st, oublock_end; 690 int inblock_st, inblock_end; 691 692 td = curthread; 693 td_savedcred = td->td_ucred; 694 td->td_ucred = aiocbe->cred; 695 mycp = td->td_proc; 696 cb = &aiocbe->uaiocb; 697 fp = aiocbe->fd_file; 698 699 aiov.iov_base = (void *)(uintptr_t)cb->aio_buf; 700 aiov.iov_len = cb->aio_nbytes; 701 702 auio.uio_iov = &aiov; 703 auio.uio_iovcnt = 1; 704 auio.uio_offset = cb->aio_offset; 705 auio.uio_resid = cb->aio_nbytes; 706 cnt = cb->aio_nbytes; 707 auio.uio_segflg = UIO_USERSPACE; 708 auio.uio_td = td; 709 710 inblock_st = mycp->p_stats->p_ru.ru_inblock; 711 oublock_st = mycp->p_stats->p_ru.ru_oublock; 712 /* 713 * _aio_aqueue() acquires a reference to the file that is 714 * released in aio_free_entry(). 715 */ 716 if (cb->aio_lio_opcode == LIO_READ) { 717 auio.uio_rw = UIO_READ; 718 error = fo_read(fp, &auio, fp->f_cred, FOF_OFFSET, td); 719 } else { 720 auio.uio_rw = UIO_WRITE; 721 error = fo_write(fp, &auio, fp->f_cred, FOF_OFFSET, td); 722 } 723 inblock_end = mycp->p_stats->p_ru.ru_inblock; 724 oublock_end = mycp->p_stats->p_ru.ru_oublock; 725 726 aiocbe->inputcharge = inblock_end - inblock_st; 727 aiocbe->outputcharge = oublock_end - oublock_st; 728 729 if ((error) && (auio.uio_resid != cnt)) { 730 if (error == ERESTART || error == EINTR || error == EWOULDBLOCK) 731 error = 0; 732 if ((error == EPIPE) && (cb->aio_lio_opcode == LIO_WRITE)) { 733 PROC_LOCK(aiocbe->userproc); 734 psignal(aiocbe->userproc, SIGPIPE); 735 PROC_UNLOCK(aiocbe->userproc); 736 } 737 } 738 739 cnt -= auio.uio_resid; 740 cb->_aiocb_private.error = error; 741 cb->_aiocb_private.status = cnt; 742 td->td_ucred = td_savedcred; 743 } 744 745 /* 746 * The AIO daemon, most of the actual work is done in aio_process, 747 * but the setup (and address space mgmt) is done in this routine. 748 */ 749 static void 750 aio_daemon(void *uproc) 751 { 752 int s; 753 struct aio_liojob *lj; 754 struct aiocb *cb; 755 struct aiocblist *aiocbe; 756 struct aiothreadlist *aiop; 757 struct kaioinfo *ki; 758 struct proc *curcp, *mycp, *userp; 759 struct vmspace *myvm, *tmpvm; 760 struct thread *td = curthread; 761 struct pgrp *newpgrp; 762 struct session *newsess; 763 764 mtx_lock(&Giant); 765 /* 766 * Local copies of curproc (cp) and vmspace (myvm) 767 */ 768 mycp = td->td_proc; 769 myvm = mycp->p_vmspace; 770 771 if (mycp->p_textvp) { 772 vrele(mycp->p_textvp); 773 mycp->p_textvp = NULL; 774 } 775 776 /* 777 * Allocate and ready the aio control info. There is one aiop structure 778 * per daemon. 779 */ 780 aiop = uma_zalloc(aiop_zone, M_WAITOK); 781 aiop->aiothread = td; 782 aiop->aiothreadflags |= AIOP_FREE; 783 784 s = splnet(); 785 786 /* 787 * Place thread (lightweight process) onto the AIO free thread list. 788 */ 789 if (TAILQ_EMPTY(&aio_freeproc)) 790 wakeup(&aio_freeproc); 791 TAILQ_INSERT_HEAD(&aio_freeproc, aiop, list); 792 793 splx(s); 794 795 /* 796 * Get rid of our current filedescriptors. AIOD's don't need any 797 * filedescriptors, except as temporarily inherited from the client. 798 */ 799 fdfree(td); 800 801 mtx_unlock(&Giant); 802 /* The daemon resides in its own pgrp. */ 803 MALLOC(newpgrp, struct pgrp *, sizeof(struct pgrp), M_PGRP, 804 M_WAITOK | M_ZERO); 805 MALLOC(newsess, struct session *, sizeof(struct session), M_SESSION, 806 M_WAITOK | M_ZERO); 807 808 sx_xlock(&proctree_lock); 809 enterpgrp(mycp, mycp->p_pid, newpgrp, newsess); 810 sx_xunlock(&proctree_lock); 811 mtx_lock(&Giant); 812 813 /* Mark special process type. */ 814 mycp->p_flag |= P_SYSTEM; 815 816 /* 817 * Wakeup parent process. (Parent sleeps to keep from blasting away 818 * and creating too many daemons.) 819 */ 820 wakeup(mycp); 821 822 for (;;) { 823 /* 824 * curcp is the current daemon process context. 825 * userp is the current user process context. 826 */ 827 curcp = mycp; 828 829 /* 830 * Take daemon off of free queue 831 */ 832 if (aiop->aiothreadflags & AIOP_FREE) { 833 s = splnet(); 834 TAILQ_REMOVE(&aio_freeproc, aiop, list); 835 TAILQ_INSERT_TAIL(&aio_activeproc, aiop, list); 836 aiop->aiothreadflags &= ~AIOP_FREE; 837 splx(s); 838 } 839 aiop->aiothreadflags &= ~AIOP_SCHED; 840 841 /* 842 * Check for jobs. 843 */ 844 while ((aiocbe = aio_selectjob(aiop)) != NULL) { 845 cb = &aiocbe->uaiocb; 846 userp = aiocbe->userproc; 847 848 aiocbe->jobstate = JOBST_JOBRUNNING; 849 850 /* 851 * Connect to process address space for user program. 852 */ 853 if (userp != curcp) { 854 /* 855 * Save the current address space that we are 856 * connected to. 857 */ 858 tmpvm = mycp->p_vmspace; 859 860 /* 861 * Point to the new user address space, and 862 * refer to it. 863 */ 864 mycp->p_vmspace = userp->p_vmspace; 865 mycp->p_vmspace->vm_refcnt++; 866 867 /* Activate the new mapping. */ 868 pmap_activate(FIRST_THREAD_IN_PROC(mycp)); 869 870 /* 871 * If the old address space wasn't the daemons 872 * own address space, then we need to remove the 873 * daemon's reference from the other process 874 * that it was acting on behalf of. 875 */ 876 if (tmpvm != myvm) { 877 vmspace_free(tmpvm); 878 } 879 curcp = userp; 880 } 881 882 ki = userp->p_aioinfo; 883 lj = aiocbe->lio; 884 885 /* Account for currently active jobs. */ 886 ki->kaio_active_count++; 887 888 /* Do the I/O function. */ 889 aio_process(aiocbe); 890 891 /* Decrement the active job count. */ 892 ki->kaio_active_count--; 893 894 /* 895 * Increment the completion count for wakeup/signal 896 * comparisons. 897 */ 898 aiocbe->jobflags |= AIOCBLIST_DONE; 899 ki->kaio_queue_finished_count++; 900 if (lj) 901 lj->lioj_queue_finished_count++; 902 if ((ki->kaio_flags & KAIO_WAKEUP) || ((ki->kaio_flags 903 & KAIO_RUNDOWN) && (ki->kaio_active_count == 0))) { 904 ki->kaio_flags &= ~KAIO_WAKEUP; 905 wakeup(userp); 906 } 907 908 s = splbio(); 909 if (lj && (lj->lioj_flags & 910 (LIOJ_SIGNAL|LIOJ_SIGNAL_POSTED)) == LIOJ_SIGNAL) { 911 if ((lj->lioj_queue_finished_count == 912 lj->lioj_queue_count) && 913 (lj->lioj_buffer_finished_count == 914 lj->lioj_buffer_count)) { 915 PROC_LOCK(userp); 916 psignal(userp, 917 lj->lioj_signal.sigev_signo); 918 PROC_UNLOCK(userp); 919 lj->lioj_flags |= LIOJ_SIGNAL_POSTED; 920 } 921 } 922 splx(s); 923 924 aiocbe->jobstate = JOBST_JOBFINISHED; 925 926 s = splnet(); 927 TAILQ_REMOVE(&ki->kaio_jobqueue, aiocbe, plist); 928 TAILQ_INSERT_TAIL(&ki->kaio_jobdone, aiocbe, plist); 929 splx(s); 930 KNOTE(&aiocbe->klist, 0); 931 932 if (aiocbe->jobflags & AIOCBLIST_RUNDOWN) { 933 wakeup(aiocbe); 934 aiocbe->jobflags &= ~AIOCBLIST_RUNDOWN; 935 } 936 937 if (cb->aio_sigevent.sigev_notify == SIGEV_SIGNAL) { 938 PROC_LOCK(userp); 939 psignal(userp, cb->aio_sigevent.sigev_signo); 940 PROC_UNLOCK(userp); 941 } 942 } 943 944 /* 945 * Disconnect from user address space. 946 */ 947 if (curcp != mycp) { 948 /* Get the user address space to disconnect from. */ 949 tmpvm = mycp->p_vmspace; 950 951 /* Get original address space for daemon. */ 952 mycp->p_vmspace = myvm; 953 954 /* Activate the daemon's address space. */ 955 pmap_activate(FIRST_THREAD_IN_PROC(mycp)); 956 #ifdef DIAGNOSTIC 957 if (tmpvm == myvm) { 958 printf("AIOD: vmspace problem -- %d\n", 959 mycp->p_pid); 960 } 961 #endif 962 /* Remove our vmspace reference. */ 963 vmspace_free(tmpvm); 964 965 curcp = mycp; 966 } 967 968 /* 969 * If we are the first to be put onto the free queue, wakeup 970 * anyone waiting for a daemon. 971 */ 972 s = splnet(); 973 TAILQ_REMOVE(&aio_activeproc, aiop, list); 974 if (TAILQ_EMPTY(&aio_freeproc)) 975 wakeup(&aio_freeproc); 976 TAILQ_INSERT_HEAD(&aio_freeproc, aiop, list); 977 aiop->aiothreadflags |= AIOP_FREE; 978 splx(s); 979 980 /* 981 * If daemon is inactive for a long time, allow it to exit, 982 * thereby freeing resources. 983 */ 984 if ((aiop->aiothreadflags & AIOP_SCHED) == 0 && 985 tsleep(aiop->aiothread, PRIBIO, "aiordy", aiod_lifetime)) { 986 s = splnet(); 987 if (TAILQ_EMPTY(&aio_jobs)) { 988 if ((aiop->aiothreadflags & AIOP_FREE) && 989 (num_aio_procs > target_aio_procs)) { 990 TAILQ_REMOVE(&aio_freeproc, aiop, list); 991 splx(s); 992 uma_zfree(aiop_zone, aiop); 993 num_aio_procs--; 994 #ifdef DIAGNOSTIC 995 if (mycp->p_vmspace->vm_refcnt <= 1) { 996 printf("AIOD: bad vm refcnt for" 997 " exiting daemon: %d\n", 998 mycp->p_vmspace->vm_refcnt); 999 } 1000 #endif 1001 kthread_exit(0); 1002 } 1003 } 1004 splx(s); 1005 } 1006 } 1007 } 1008 1009 /* 1010 * Create a new AIO daemon. This is mostly a kernel-thread fork routine. The 1011 * AIO daemon modifies its environment itself. 1012 */ 1013 static int 1014 aio_newproc(void) 1015 { 1016 int error; 1017 struct proc *p; 1018 1019 error = kthread_create(aio_daemon, curproc, &p, RFNOWAIT, 0, "aiod%d", 1020 num_aio_procs); 1021 if (error) 1022 return (error); 1023 1024 /* 1025 * Wait until daemon is started, but continue on just in case to 1026 * handle error conditions. 1027 */ 1028 error = tsleep(p, PZERO, "aiosta", aiod_timeout); 1029 1030 num_aio_procs++; 1031 1032 return (error); 1033 } 1034 1035 /* 1036 * Try the high-performance, low-overhead physio method for eligible 1037 * VCHR devices. This method doesn't use an aio helper thread, and 1038 * thus has very low overhead. 1039 * 1040 * Assumes that the caller, _aio_aqueue(), has incremented the file 1041 * structure's reference count, preventing its deallocation for the 1042 * duration of this call. 1043 */ 1044 static int 1045 aio_qphysio(struct proc *p, struct aiocblist *aiocbe) 1046 { 1047 int error; 1048 struct aiocb *cb; 1049 struct file *fp; 1050 struct buf *bp; 1051 struct vnode *vp; 1052 struct kaioinfo *ki; 1053 struct aio_liojob *lj; 1054 int s; 1055 int notify; 1056 1057 cb = &aiocbe->uaiocb; 1058 fp = aiocbe->fd_file; 1059 1060 if (fp->f_type != DTYPE_VNODE) 1061 return (-1); 1062 1063 vp = fp->f_data; 1064 1065 /* 1066 * If its not a disk, we don't want to return a positive error. 1067 * It causes the aio code to not fall through to try the thread 1068 * way when you're talking to a regular file. 1069 */ 1070 if (!vn_isdisk(vp, &error)) { 1071 if (error == ENOTBLK) 1072 return (-1); 1073 else 1074 return (error); 1075 } 1076 1077 if (cb->aio_nbytes % vp->v_rdev->si_bsize_phys) 1078 return (-1); 1079 1080 if (cb->aio_nbytes > 1081 MAXPHYS - (((vm_offset_t) cb->aio_buf) & PAGE_MASK)) 1082 return (-1); 1083 1084 ki = p->p_aioinfo; 1085 if (ki->kaio_buffer_count >= ki->kaio_ballowed_count) 1086 return (-1); 1087 1088 ki->kaio_buffer_count++; 1089 1090 lj = aiocbe->lio; 1091 if (lj) 1092 lj->lioj_buffer_count++; 1093 1094 /* Create and build a buffer header for a transfer. */ 1095 bp = (struct buf *)getpbuf(NULL); 1096 BUF_KERNPROC(bp); 1097 1098 /* 1099 * Get a copy of the kva from the physical buffer. 1100 */ 1101 bp->b_caller1 = p; 1102 bp->b_dev = vp->v_rdev; 1103 error = bp->b_error = 0; 1104 1105 bp->b_bcount = cb->aio_nbytes; 1106 bp->b_bufsize = cb->aio_nbytes; 1107 bp->b_flags = B_PHYS; 1108 bp->b_iodone = aio_physwakeup; 1109 bp->b_saveaddr = bp->b_data; 1110 bp->b_data = (void *)(uintptr_t)cb->aio_buf; 1111 bp->b_blkno = btodb(cb->aio_offset); 1112 1113 if (cb->aio_lio_opcode == LIO_WRITE) { 1114 bp->b_iocmd = BIO_WRITE; 1115 if (!useracc(bp->b_data, bp->b_bufsize, VM_PROT_READ)) { 1116 error = EFAULT; 1117 goto doerror; 1118 } 1119 } else { 1120 bp->b_iocmd = BIO_READ; 1121 if (!useracc(bp->b_data, bp->b_bufsize, VM_PROT_WRITE)) { 1122 error = EFAULT; 1123 goto doerror; 1124 } 1125 } 1126 1127 /* 1128 * Bring buffer into kernel space. 1129 * 1130 * Note that useracc() alone is not a 1131 * sufficient test. vmapbuf() can still fail 1132 * due to a smaller file mapped into a larger 1133 * area of VM, or if userland races against 1134 * vmapbuf() after the useracc() check. 1135 */ 1136 if (vmapbuf(bp) < 0) { 1137 error = EFAULT; 1138 goto doerror; 1139 } 1140 1141 s = splbio(); 1142 aiocbe->bp = bp; 1143 bp->b_spc = (void *)aiocbe; 1144 TAILQ_INSERT_TAIL(&aio_bufjobs, aiocbe, list); 1145 TAILQ_INSERT_TAIL(&ki->kaio_bufqueue, aiocbe, plist); 1146 aiocbe->jobstate = JOBST_JOBQBUF; 1147 cb->_aiocb_private.status = cb->aio_nbytes; 1148 num_buf_aio++; 1149 bp->b_error = 0; 1150 1151 splx(s); 1152 1153 /* Perform transfer. */ 1154 DEV_STRATEGY(bp); 1155 1156 notify = 0; 1157 s = splbio(); 1158 1159 /* 1160 * If we had an error invoking the request, or an error in processing 1161 * the request before we have returned, we process it as an error in 1162 * transfer. Note that such an I/O error is not indicated immediately, 1163 * but is returned using the aio_error mechanism. In this case, 1164 * aio_suspend will return immediately. 1165 */ 1166 if (bp->b_error || (bp->b_ioflags & BIO_ERROR)) { 1167 struct aiocb *job = aiocbe->uuaiocb; 1168 1169 aiocbe->uaiocb._aiocb_private.status = 0; 1170 suword(&job->_aiocb_private.status, 0); 1171 aiocbe->uaiocb._aiocb_private.error = bp->b_error; 1172 suword(&job->_aiocb_private.error, bp->b_error); 1173 1174 ki->kaio_buffer_finished_count++; 1175 1176 if (aiocbe->jobstate != JOBST_JOBBFINISHED) { 1177 aiocbe->jobstate = JOBST_JOBBFINISHED; 1178 aiocbe->jobflags |= AIOCBLIST_DONE; 1179 TAILQ_REMOVE(&aio_bufjobs, aiocbe, list); 1180 TAILQ_REMOVE(&ki->kaio_bufqueue, aiocbe, plist); 1181 TAILQ_INSERT_TAIL(&ki->kaio_bufdone, aiocbe, plist); 1182 notify = 1; 1183 } 1184 } 1185 splx(s); 1186 if (notify) 1187 KNOTE(&aiocbe->klist, 0); 1188 return (0); 1189 1190 doerror: 1191 ki->kaio_buffer_count--; 1192 if (lj) 1193 lj->lioj_buffer_count--; 1194 aiocbe->bp = NULL; 1195 relpbuf(bp, NULL); 1196 return (error); 1197 } 1198 1199 /* 1200 * This waits/tests physio completion. 1201 */ 1202 static int 1203 aio_fphysio(struct aiocblist *iocb) 1204 { 1205 int s; 1206 struct buf *bp; 1207 int error; 1208 1209 bp = iocb->bp; 1210 1211 s = splbio(); 1212 while ((bp->b_flags & B_DONE) == 0) { 1213 if (tsleep(bp, PRIBIO, "physstr", aiod_timeout)) { 1214 if ((bp->b_flags & B_DONE) == 0) { 1215 splx(s); 1216 return (EINPROGRESS); 1217 } else 1218 break; 1219 } 1220 } 1221 splx(s); 1222 1223 /* Release mapping into kernel space. */ 1224 vunmapbuf(bp); 1225 iocb->bp = 0; 1226 1227 error = 0; 1228 1229 /* Check for an error. */ 1230 if (bp->b_ioflags & BIO_ERROR) 1231 error = bp->b_error; 1232 1233 relpbuf(bp, NULL); 1234 return (error); 1235 } 1236 1237 /* 1238 * Wake up aio requests that may be serviceable now. 1239 */ 1240 static void 1241 aio_swake_cb(struct socket *so, struct sockbuf *sb) 1242 { 1243 struct aiocblist *cb,*cbn; 1244 struct proc *p; 1245 struct kaioinfo *ki = NULL; 1246 int opcode, wakecount = 0; 1247 struct aiothreadlist *aiop; 1248 1249 if (sb == &so->so_snd) { 1250 opcode = LIO_WRITE; 1251 so->so_snd.sb_flags &= ~SB_AIO; 1252 } else { 1253 opcode = LIO_READ; 1254 so->so_rcv.sb_flags &= ~SB_AIO; 1255 } 1256 1257 for (cb = TAILQ_FIRST(&so->so_aiojobq); cb; cb = cbn) { 1258 cbn = TAILQ_NEXT(cb, list); 1259 if (opcode == cb->uaiocb.aio_lio_opcode) { 1260 p = cb->userproc; 1261 ki = p->p_aioinfo; 1262 TAILQ_REMOVE(&so->so_aiojobq, cb, list); 1263 TAILQ_REMOVE(&ki->kaio_sockqueue, cb, plist); 1264 TAILQ_INSERT_TAIL(&aio_jobs, cb, list); 1265 TAILQ_INSERT_TAIL(&ki->kaio_jobqueue, cb, plist); 1266 wakecount++; 1267 if (cb->jobstate != JOBST_JOBQGLOBAL) 1268 panic("invalid queue value"); 1269 } 1270 } 1271 1272 while (wakecount--) { 1273 if ((aiop = TAILQ_FIRST(&aio_freeproc)) != 0) { 1274 TAILQ_REMOVE(&aio_freeproc, aiop, list); 1275 TAILQ_INSERT_TAIL(&aio_activeproc, aiop, list); 1276 aiop->aiothreadflags &= ~AIOP_FREE; 1277 wakeup(aiop->aiothread); 1278 } 1279 } 1280 } 1281 1282 /* 1283 * Queue a new AIO request. Choosing either the threaded or direct physio VCHR 1284 * technique is done in this code. 1285 */ 1286 static int 1287 _aio_aqueue(struct thread *td, struct aiocb *job, struct aio_liojob *lj, int type) 1288 { 1289 struct proc *p = td->td_proc; 1290 struct filedesc *fdp; 1291 struct file *fp; 1292 unsigned int fd; 1293 struct socket *so; 1294 int s; 1295 int error; 1296 int opcode, user_opcode; 1297 struct aiocblist *aiocbe; 1298 struct aiothreadlist *aiop; 1299 struct kaioinfo *ki; 1300 struct kevent kev; 1301 struct kqueue *kq; 1302 struct file *kq_fp; 1303 1304 aiocbe = uma_zalloc(aiocb_zone, M_WAITOK); 1305 aiocbe->inputcharge = 0; 1306 aiocbe->outputcharge = 0; 1307 callout_handle_init(&aiocbe->timeouthandle); 1308 SLIST_INIT(&aiocbe->klist); 1309 1310 suword(&job->_aiocb_private.status, -1); 1311 suword(&job->_aiocb_private.error, 0); 1312 suword(&job->_aiocb_private.kernelinfo, -1); 1313 1314 error = copyin(job, &aiocbe->uaiocb, sizeof(aiocbe->uaiocb)); 1315 if (error) { 1316 suword(&job->_aiocb_private.error, error); 1317 uma_zfree(aiocb_zone, aiocbe); 1318 return (error); 1319 } 1320 if (aiocbe->uaiocb.aio_sigevent.sigev_notify == SIGEV_SIGNAL && 1321 !_SIG_VALID(aiocbe->uaiocb.aio_sigevent.sigev_signo)) { 1322 uma_zfree(aiocb_zone, aiocbe); 1323 return (EINVAL); 1324 } 1325 1326 /* Save userspace address of the job info. */ 1327 aiocbe->uuaiocb = job; 1328 1329 /* Get the opcode. */ 1330 user_opcode = aiocbe->uaiocb.aio_lio_opcode; 1331 if (type != LIO_NOP) 1332 aiocbe->uaiocb.aio_lio_opcode = type; 1333 opcode = aiocbe->uaiocb.aio_lio_opcode; 1334 1335 /* Get the fd info for process. */ 1336 fdp = p->p_fd; 1337 1338 /* 1339 * Range check file descriptor. 1340 */ 1341 FILEDESC_LOCK(fdp); 1342 fd = aiocbe->uaiocb.aio_fildes; 1343 if (fd >= fdp->fd_nfiles) { 1344 FILEDESC_UNLOCK(fdp); 1345 uma_zfree(aiocb_zone, aiocbe); 1346 if (type == 0) 1347 suword(&job->_aiocb_private.error, EBADF); 1348 return (EBADF); 1349 } 1350 1351 fp = aiocbe->fd_file = fdp->fd_ofiles[fd]; 1352 if ((fp == NULL) || ((opcode == LIO_WRITE) && ((fp->f_flag & FWRITE) == 1353 0))) { 1354 FILEDESC_UNLOCK(fdp); 1355 uma_zfree(aiocb_zone, aiocbe); 1356 if (type == 0) 1357 suword(&job->_aiocb_private.error, EBADF); 1358 return (EBADF); 1359 } 1360 fhold(fp); 1361 FILEDESC_UNLOCK(fdp); 1362 1363 if (aiocbe->uaiocb.aio_offset == -1LL) { 1364 error = EINVAL; 1365 goto aqueue_fail; 1366 } 1367 error = suword(&job->_aiocb_private.kernelinfo, jobrefid); 1368 if (error) { 1369 error = EINVAL; 1370 goto aqueue_fail; 1371 } 1372 aiocbe->uaiocb._aiocb_private.kernelinfo = (void *)(intptr_t)jobrefid; 1373 if (jobrefid == LONG_MAX) 1374 jobrefid = 1; 1375 else 1376 jobrefid++; 1377 1378 if (opcode == LIO_NOP) { 1379 fdrop(fp, td); 1380 uma_zfree(aiocb_zone, aiocbe); 1381 if (type == 0) { 1382 suword(&job->_aiocb_private.error, 0); 1383 suword(&job->_aiocb_private.status, 0); 1384 suword(&job->_aiocb_private.kernelinfo, 0); 1385 } 1386 return (0); 1387 } 1388 if ((opcode != LIO_READ) && (opcode != LIO_WRITE)) { 1389 if (type == 0) 1390 suword(&job->_aiocb_private.status, 0); 1391 error = EINVAL; 1392 goto aqueue_fail; 1393 } 1394 1395 if (aiocbe->uaiocb.aio_sigevent.sigev_notify == SIGEV_KEVENT) { 1396 kev.ident = aiocbe->uaiocb.aio_sigevent.sigev_notify_kqueue; 1397 kev.udata = aiocbe->uaiocb.aio_sigevent.sigev_value.sigval_ptr; 1398 } 1399 else { 1400 /* 1401 * This method for requesting kevent-based notification won't 1402 * work on the alpha, since we're passing in a pointer 1403 * via aio_lio_opcode, which is an int. Use the SIGEV_KEVENT- 1404 * based method instead. 1405 */ 1406 if (user_opcode == LIO_NOP || user_opcode == LIO_READ || 1407 user_opcode == LIO_WRITE) 1408 goto no_kqueue; 1409 1410 error = copyin((struct kevent *)(uintptr_t)user_opcode, 1411 &kev, sizeof(kev)); 1412 if (error) 1413 goto aqueue_fail; 1414 } 1415 if ((u_int)kev.ident >= fdp->fd_nfiles || 1416 (kq_fp = fdp->fd_ofiles[kev.ident]) == NULL || 1417 (kq_fp->f_type != DTYPE_KQUEUE)) { 1418 error = EBADF; 1419 goto aqueue_fail; 1420 } 1421 kq = kq_fp->f_data; 1422 kev.ident = (uintptr_t)aiocbe->uuaiocb; 1423 kev.filter = EVFILT_AIO; 1424 kev.flags = EV_ADD | EV_ENABLE | EV_FLAG1; 1425 kev.data = (intptr_t)aiocbe; 1426 error = kqueue_register(kq, &kev, td); 1427 aqueue_fail: 1428 if (error) { 1429 fdrop(fp, td); 1430 uma_zfree(aiocb_zone, aiocbe); 1431 if (type == 0) 1432 suword(&job->_aiocb_private.error, error); 1433 goto done; 1434 } 1435 no_kqueue: 1436 1437 suword(&job->_aiocb_private.error, EINPROGRESS); 1438 aiocbe->uaiocb._aiocb_private.error = EINPROGRESS; 1439 aiocbe->userproc = p; 1440 aiocbe->cred = crhold(td->td_ucred); 1441 aiocbe->jobflags = 0; 1442 aiocbe->lio = lj; 1443 ki = p->p_aioinfo; 1444 1445 if (fp->f_type == DTYPE_SOCKET) { 1446 /* 1447 * Alternate queueing for socket ops: Reach down into the 1448 * descriptor to get the socket data. Then check to see if the 1449 * socket is ready to be read or written (based on the requested 1450 * operation). 1451 * 1452 * If it is not ready for io, then queue the aiocbe on the 1453 * socket, and set the flags so we get a call when sbnotify() 1454 * happens. 1455 */ 1456 so = fp->f_data; 1457 s = splnet(); 1458 if (((opcode == LIO_READ) && (!soreadable(so))) || ((opcode == 1459 LIO_WRITE) && (!sowriteable(so)))) { 1460 TAILQ_INSERT_TAIL(&so->so_aiojobq, aiocbe, list); 1461 TAILQ_INSERT_TAIL(&ki->kaio_sockqueue, aiocbe, plist); 1462 if (opcode == LIO_READ) 1463 so->so_rcv.sb_flags |= SB_AIO; 1464 else 1465 so->so_snd.sb_flags |= SB_AIO; 1466 aiocbe->jobstate = JOBST_JOBQGLOBAL; /* XXX */ 1467 ki->kaio_queue_count++; 1468 num_queue_count++; 1469 splx(s); 1470 error = 0; 1471 goto done; 1472 } 1473 splx(s); 1474 } 1475 1476 if ((error = aio_qphysio(p, aiocbe)) == 0) 1477 goto done; 1478 if (error > 0) { 1479 suword(&job->_aiocb_private.status, 0); 1480 aiocbe->uaiocb._aiocb_private.error = error; 1481 suword(&job->_aiocb_private.error, error); 1482 goto done; 1483 } 1484 1485 /* No buffer for daemon I/O. */ 1486 aiocbe->bp = NULL; 1487 1488 ki->kaio_queue_count++; 1489 if (lj) 1490 lj->lioj_queue_count++; 1491 s = splnet(); 1492 TAILQ_INSERT_TAIL(&ki->kaio_jobqueue, aiocbe, plist); 1493 TAILQ_INSERT_TAIL(&aio_jobs, aiocbe, list); 1494 splx(s); 1495 aiocbe->jobstate = JOBST_JOBQGLOBAL; 1496 1497 num_queue_count++; 1498 error = 0; 1499 1500 /* 1501 * If we don't have a free AIO process, and we are below our quota, then 1502 * start one. Otherwise, depend on the subsequent I/O completions to 1503 * pick-up this job. If we don't sucessfully create the new process 1504 * (thread) due to resource issues, we return an error for now (EAGAIN), 1505 * which is likely not the correct thing to do. 1506 */ 1507 s = splnet(); 1508 retryproc: 1509 if ((aiop = TAILQ_FIRST(&aio_freeproc)) != NULL) { 1510 TAILQ_REMOVE(&aio_freeproc, aiop, list); 1511 TAILQ_INSERT_TAIL(&aio_activeproc, aiop, list); 1512 aiop->aiothreadflags &= ~AIOP_FREE; 1513 wakeup(aiop->aiothread); 1514 } else if (((num_aio_resv_start + num_aio_procs) < max_aio_procs) && 1515 ((ki->kaio_active_count + num_aio_resv_start) < 1516 ki->kaio_maxactive_count)) { 1517 num_aio_resv_start++; 1518 if ((error = aio_newproc()) == 0) { 1519 num_aio_resv_start--; 1520 goto retryproc; 1521 } 1522 num_aio_resv_start--; 1523 } 1524 splx(s); 1525 done: 1526 return (error); 1527 } 1528 1529 /* 1530 * This routine queues an AIO request, checking for quotas. 1531 */ 1532 static int 1533 aio_aqueue(struct thread *td, struct aiocb *job, int type) 1534 { 1535 struct proc *p = td->td_proc; 1536 struct kaioinfo *ki; 1537 1538 if (p->p_aioinfo == NULL) 1539 aio_init_aioinfo(p); 1540 1541 if (num_queue_count >= max_queue_count) 1542 return (EAGAIN); 1543 1544 ki = p->p_aioinfo; 1545 if (ki->kaio_queue_count >= ki->kaio_qallowed_count) 1546 return (EAGAIN); 1547 1548 return _aio_aqueue(td, job, NULL, type); 1549 } 1550 1551 /* 1552 * Support the aio_return system call, as a side-effect, kernel resources are 1553 * released. 1554 */ 1555 int 1556 aio_return(struct thread *td, struct aio_return_args *uap) 1557 { 1558 struct proc *p = td->td_proc; 1559 int s; 1560 long jobref; 1561 struct aiocblist *cb, *ncb; 1562 struct aiocb *ujob; 1563 struct kaioinfo *ki; 1564 1565 ujob = uap->aiocbp; 1566 jobref = fuword(&ujob->_aiocb_private.kernelinfo); 1567 if (jobref == -1 || jobref == 0) 1568 return (EINVAL); 1569 1570 ki = p->p_aioinfo; 1571 if (ki == NULL) 1572 return (EINVAL); 1573 TAILQ_FOREACH(cb, &ki->kaio_jobdone, plist) { 1574 if (((intptr_t) cb->uaiocb._aiocb_private.kernelinfo) == 1575 jobref) { 1576 if (cb->uaiocb.aio_lio_opcode == LIO_WRITE) { 1577 p->p_stats->p_ru.ru_oublock += 1578 cb->outputcharge; 1579 cb->outputcharge = 0; 1580 } else if (cb->uaiocb.aio_lio_opcode == LIO_READ) { 1581 p->p_stats->p_ru.ru_inblock += cb->inputcharge; 1582 cb->inputcharge = 0; 1583 } 1584 goto done; 1585 } 1586 } 1587 s = splbio(); 1588 for (cb = TAILQ_FIRST(&ki->kaio_bufdone); cb; cb = ncb) { 1589 ncb = TAILQ_NEXT(cb, plist); 1590 if (((intptr_t) cb->uaiocb._aiocb_private.kernelinfo) 1591 == jobref) { 1592 break; 1593 } 1594 } 1595 splx(s); 1596 done: 1597 if (cb != NULL) { 1598 if (ujob == cb->uuaiocb) { 1599 td->td_retval[0] = 1600 cb->uaiocb._aiocb_private.status; 1601 } else 1602 td->td_retval[0] = EFAULT; 1603 aio_free_entry(cb); 1604 return (0); 1605 } 1606 return (EINVAL); 1607 } 1608 1609 /* 1610 * Allow a process to wakeup when any of the I/O requests are completed. 1611 */ 1612 int 1613 aio_suspend(struct thread *td, struct aio_suspend_args *uap) 1614 { 1615 struct proc *p = td->td_proc; 1616 struct timeval atv; 1617 struct timespec ts; 1618 struct aiocb *const *cbptr, *cbp; 1619 struct kaioinfo *ki; 1620 struct aiocblist *cb; 1621 int i; 1622 int njoblist; 1623 int error, s, timo; 1624 long *ijoblist; 1625 struct aiocb **ujoblist; 1626 1627 if (uap->nent < 0 || uap->nent > AIO_LISTIO_MAX) 1628 return (EINVAL); 1629 1630 timo = 0; 1631 if (uap->timeout) { 1632 /* Get timespec struct. */ 1633 if ((error = copyin(uap->timeout, &ts, sizeof(ts))) != 0) 1634 return (error); 1635 1636 if (ts.tv_nsec < 0 || ts.tv_nsec >= 1000000000) 1637 return (EINVAL); 1638 1639 TIMESPEC_TO_TIMEVAL(&atv, &ts); 1640 if (itimerfix(&atv)) 1641 return (EINVAL); 1642 timo = tvtohz(&atv); 1643 } 1644 1645 ki = p->p_aioinfo; 1646 if (ki == NULL) 1647 return (EAGAIN); 1648 1649 njoblist = 0; 1650 ijoblist = uma_zalloc(aiol_zone, M_WAITOK); 1651 ujoblist = uma_zalloc(aiol_zone, M_WAITOK); 1652 cbptr = uap->aiocbp; 1653 1654 for (i = 0; i < uap->nent; i++) { 1655 cbp = (struct aiocb *)(intptr_t)fuword(&cbptr[i]); 1656 if (cbp == 0) 1657 continue; 1658 ujoblist[njoblist] = cbp; 1659 ijoblist[njoblist] = fuword(&cbp->_aiocb_private.kernelinfo); 1660 njoblist++; 1661 } 1662 1663 if (njoblist == 0) { 1664 uma_zfree(aiol_zone, ijoblist); 1665 uma_zfree(aiol_zone, ujoblist); 1666 return (0); 1667 } 1668 1669 error = 0; 1670 for (;;) { 1671 TAILQ_FOREACH(cb, &ki->kaio_jobdone, plist) { 1672 for (i = 0; i < njoblist; i++) { 1673 if (((intptr_t) 1674 cb->uaiocb._aiocb_private.kernelinfo) == 1675 ijoblist[i]) { 1676 if (ujoblist[i] != cb->uuaiocb) 1677 error = EINVAL; 1678 uma_zfree(aiol_zone, ijoblist); 1679 uma_zfree(aiol_zone, ujoblist); 1680 return (error); 1681 } 1682 } 1683 } 1684 1685 s = splbio(); 1686 for (cb = TAILQ_FIRST(&ki->kaio_bufdone); cb; cb = 1687 TAILQ_NEXT(cb, plist)) { 1688 for (i = 0; i < njoblist; i++) { 1689 if (((intptr_t) 1690 cb->uaiocb._aiocb_private.kernelinfo) == 1691 ijoblist[i]) { 1692 splx(s); 1693 if (ujoblist[i] != cb->uuaiocb) 1694 error = EINVAL; 1695 uma_zfree(aiol_zone, ijoblist); 1696 uma_zfree(aiol_zone, ujoblist); 1697 return (error); 1698 } 1699 } 1700 } 1701 1702 ki->kaio_flags |= KAIO_WAKEUP; 1703 error = tsleep(p, PRIBIO | PCATCH, "aiospn", timo); 1704 splx(s); 1705 1706 if (error == ERESTART || error == EINTR) { 1707 uma_zfree(aiol_zone, ijoblist); 1708 uma_zfree(aiol_zone, ujoblist); 1709 return (EINTR); 1710 } else if (error == EWOULDBLOCK) { 1711 uma_zfree(aiol_zone, ijoblist); 1712 uma_zfree(aiol_zone, ujoblist); 1713 return (EAGAIN); 1714 } 1715 } 1716 1717 /* NOTREACHED */ 1718 return (EINVAL); 1719 } 1720 1721 /* 1722 * aio_cancel cancels any non-physio aio operations not currently in 1723 * progress. 1724 */ 1725 int 1726 aio_cancel(struct thread *td, struct aio_cancel_args *uap) 1727 { 1728 struct proc *p = td->td_proc; 1729 struct kaioinfo *ki; 1730 struct aiocblist *cbe, *cbn; 1731 struct file *fp; 1732 struct filedesc *fdp; 1733 struct socket *so; 1734 struct proc *po; 1735 int s,error; 1736 int cancelled=0; 1737 int notcancelled=0; 1738 struct vnode *vp; 1739 1740 fdp = p->p_fd; 1741 if ((u_int)uap->fd >= fdp->fd_nfiles || 1742 (fp = fdp->fd_ofiles[uap->fd]) == NULL) 1743 return (EBADF); 1744 1745 if (fp->f_type == DTYPE_VNODE) { 1746 vp = fp->f_data; 1747 1748 if (vn_isdisk(vp,&error)) { 1749 td->td_retval[0] = AIO_NOTCANCELED; 1750 return (0); 1751 } 1752 } else if (fp->f_type == DTYPE_SOCKET) { 1753 so = fp->f_data; 1754 1755 s = splnet(); 1756 1757 for (cbe = TAILQ_FIRST(&so->so_aiojobq); cbe; cbe = cbn) { 1758 cbn = TAILQ_NEXT(cbe, list); 1759 if ((uap->aiocbp == NULL) || 1760 (uap->aiocbp == cbe->uuaiocb) ) { 1761 po = cbe->userproc; 1762 ki = po->p_aioinfo; 1763 TAILQ_REMOVE(&so->so_aiojobq, cbe, list); 1764 TAILQ_REMOVE(&ki->kaio_sockqueue, cbe, plist); 1765 TAILQ_INSERT_TAIL(&ki->kaio_jobdone, cbe, plist); 1766 if (ki->kaio_flags & KAIO_WAKEUP) { 1767 wakeup(po); 1768 } 1769 cbe->jobstate = JOBST_JOBFINISHED; 1770 cbe->uaiocb._aiocb_private.status=-1; 1771 cbe->uaiocb._aiocb_private.error=ECANCELED; 1772 cancelled++; 1773 /* XXX cancelled, knote? */ 1774 if (cbe->uaiocb.aio_sigevent.sigev_notify == 1775 SIGEV_SIGNAL) { 1776 PROC_LOCK(cbe->userproc); 1777 psignal(cbe->userproc, cbe->uaiocb.aio_sigevent.sigev_signo); 1778 PROC_UNLOCK(cbe->userproc); 1779 } 1780 if (uap->aiocbp) 1781 break; 1782 } 1783 } 1784 splx(s); 1785 1786 if ((cancelled) && (uap->aiocbp)) { 1787 td->td_retval[0] = AIO_CANCELED; 1788 return (0); 1789 } 1790 } 1791 ki=p->p_aioinfo; 1792 if (ki == NULL) 1793 goto done; 1794 s = splnet(); 1795 1796 for (cbe = TAILQ_FIRST(&ki->kaio_jobqueue); cbe; cbe = cbn) { 1797 cbn = TAILQ_NEXT(cbe, plist); 1798 1799 if ((uap->fd == cbe->uaiocb.aio_fildes) && 1800 ((uap->aiocbp == NULL ) || 1801 (uap->aiocbp == cbe->uuaiocb))) { 1802 1803 if (cbe->jobstate == JOBST_JOBQGLOBAL) { 1804 TAILQ_REMOVE(&aio_jobs, cbe, list); 1805 TAILQ_REMOVE(&ki->kaio_jobqueue, cbe, plist); 1806 TAILQ_INSERT_TAIL(&ki->kaio_jobdone, cbe, 1807 plist); 1808 cancelled++; 1809 ki->kaio_queue_finished_count++; 1810 cbe->jobstate = JOBST_JOBFINISHED; 1811 cbe->uaiocb._aiocb_private.status = -1; 1812 cbe->uaiocb._aiocb_private.error = ECANCELED; 1813 /* XXX cancelled, knote? */ 1814 if (cbe->uaiocb.aio_sigevent.sigev_notify == 1815 SIGEV_SIGNAL) { 1816 PROC_LOCK(cbe->userproc); 1817 psignal(cbe->userproc, cbe->uaiocb.aio_sigevent.sigev_signo); 1818 PROC_UNLOCK(cbe->userproc); 1819 } 1820 } else { 1821 notcancelled++; 1822 } 1823 } 1824 } 1825 splx(s); 1826 done: 1827 if (notcancelled) { 1828 td->td_retval[0] = AIO_NOTCANCELED; 1829 return (0); 1830 } 1831 if (cancelled) { 1832 td->td_retval[0] = AIO_CANCELED; 1833 return (0); 1834 } 1835 td->td_retval[0] = AIO_ALLDONE; 1836 1837 return (0); 1838 } 1839 1840 /* 1841 * aio_error is implemented in the kernel level for compatibility purposes only. 1842 * For a user mode async implementation, it would be best to do it in a userland 1843 * subroutine. 1844 */ 1845 int 1846 aio_error(struct thread *td, struct aio_error_args *uap) 1847 { 1848 struct proc *p = td->td_proc; 1849 int s; 1850 struct aiocblist *cb; 1851 struct kaioinfo *ki; 1852 long jobref; 1853 1854 ki = p->p_aioinfo; 1855 if (ki == NULL) 1856 return (EINVAL); 1857 1858 jobref = fuword(&uap->aiocbp->_aiocb_private.kernelinfo); 1859 if ((jobref == -1) || (jobref == 0)) 1860 return (EINVAL); 1861 1862 TAILQ_FOREACH(cb, &ki->kaio_jobdone, plist) { 1863 if (((intptr_t)cb->uaiocb._aiocb_private.kernelinfo) == 1864 jobref) { 1865 td->td_retval[0] = cb->uaiocb._aiocb_private.error; 1866 return (0); 1867 } 1868 } 1869 1870 s = splnet(); 1871 1872 for (cb = TAILQ_FIRST(&ki->kaio_jobqueue); cb; cb = TAILQ_NEXT(cb, 1873 plist)) { 1874 if (((intptr_t)cb->uaiocb._aiocb_private.kernelinfo) == 1875 jobref) { 1876 td->td_retval[0] = EINPROGRESS; 1877 splx(s); 1878 return (0); 1879 } 1880 } 1881 1882 for (cb = TAILQ_FIRST(&ki->kaio_sockqueue); cb; cb = TAILQ_NEXT(cb, 1883 plist)) { 1884 if (((intptr_t)cb->uaiocb._aiocb_private.kernelinfo) == 1885 jobref) { 1886 td->td_retval[0] = EINPROGRESS; 1887 splx(s); 1888 return (0); 1889 } 1890 } 1891 splx(s); 1892 1893 s = splbio(); 1894 for (cb = TAILQ_FIRST(&ki->kaio_bufdone); cb; cb = TAILQ_NEXT(cb, 1895 plist)) { 1896 if (((intptr_t)cb->uaiocb._aiocb_private.kernelinfo) == 1897 jobref) { 1898 td->td_retval[0] = cb->uaiocb._aiocb_private.error; 1899 splx(s); 1900 return (0); 1901 } 1902 } 1903 1904 for (cb = TAILQ_FIRST(&ki->kaio_bufqueue); cb; cb = TAILQ_NEXT(cb, 1905 plist)) { 1906 if (((intptr_t)cb->uaiocb._aiocb_private.kernelinfo) == 1907 jobref) { 1908 td->td_retval[0] = EINPROGRESS; 1909 splx(s); 1910 return (0); 1911 } 1912 } 1913 splx(s); 1914 1915 #if (0) 1916 /* 1917 * Hack for lio. 1918 */ 1919 status = fuword(&uap->aiocbp->_aiocb_private.status); 1920 if (status == -1) 1921 return fuword(&uap->aiocbp->_aiocb_private.error); 1922 #endif 1923 return (EINVAL); 1924 } 1925 1926 /* syscall - asynchronous read from a file (REALTIME) */ 1927 int 1928 aio_read(struct thread *td, struct aio_read_args *uap) 1929 { 1930 1931 return aio_aqueue(td, uap->aiocbp, LIO_READ); 1932 } 1933 1934 /* syscall - asynchronous write to a file (REALTIME) */ 1935 int 1936 aio_write(struct thread *td, struct aio_write_args *uap) 1937 { 1938 1939 return aio_aqueue(td, uap->aiocbp, LIO_WRITE); 1940 } 1941 1942 /* syscall - list directed I/O (REALTIME) */ 1943 int 1944 lio_listio(struct thread *td, struct lio_listio_args *uap) 1945 { 1946 struct proc *p = td->td_proc; 1947 int nent, nentqueued; 1948 struct aiocb *iocb, * const *cbptr; 1949 struct aiocblist *cb; 1950 struct kaioinfo *ki; 1951 struct aio_liojob *lj; 1952 int error, runningcode; 1953 int nerror; 1954 int i; 1955 int s; 1956 1957 if ((uap->mode != LIO_NOWAIT) && (uap->mode != LIO_WAIT)) 1958 return (EINVAL); 1959 1960 nent = uap->nent; 1961 if (nent < 0 || nent > AIO_LISTIO_MAX) 1962 return (EINVAL); 1963 1964 if (p->p_aioinfo == NULL) 1965 aio_init_aioinfo(p); 1966 1967 if ((nent + num_queue_count) > max_queue_count) 1968 return (EAGAIN); 1969 1970 ki = p->p_aioinfo; 1971 if ((nent + ki->kaio_queue_count) > ki->kaio_qallowed_count) 1972 return (EAGAIN); 1973 1974 lj = uma_zalloc(aiolio_zone, M_WAITOK); 1975 if (!lj) 1976 return (EAGAIN); 1977 1978 lj->lioj_flags = 0; 1979 lj->lioj_buffer_count = 0; 1980 lj->lioj_buffer_finished_count = 0; 1981 lj->lioj_queue_count = 0; 1982 lj->lioj_queue_finished_count = 0; 1983 lj->lioj_ki = ki; 1984 1985 /* 1986 * Setup signal. 1987 */ 1988 if (uap->sig && (uap->mode == LIO_NOWAIT)) { 1989 error = copyin(uap->sig, &lj->lioj_signal, 1990 sizeof(lj->lioj_signal)); 1991 if (error) { 1992 uma_zfree(aiolio_zone, lj); 1993 return (error); 1994 } 1995 if (!_SIG_VALID(lj->lioj_signal.sigev_signo)) { 1996 uma_zfree(aiolio_zone, lj); 1997 return (EINVAL); 1998 } 1999 lj->lioj_flags |= LIOJ_SIGNAL; 2000 } 2001 TAILQ_INSERT_TAIL(&ki->kaio_liojoblist, lj, lioj_list); 2002 /* 2003 * Get pointers to the list of I/O requests. 2004 */ 2005 nerror = 0; 2006 nentqueued = 0; 2007 cbptr = uap->acb_list; 2008 for (i = 0; i < uap->nent; i++) { 2009 iocb = (struct aiocb *)(intptr_t)fuword(&cbptr[i]); 2010 if (((intptr_t)iocb != -1) && ((intptr_t)iocb != 0)) { 2011 error = _aio_aqueue(td, iocb, lj, 0); 2012 if (error == 0) 2013 nentqueued++; 2014 else 2015 nerror++; 2016 } 2017 } 2018 2019 /* 2020 * If we haven't queued any, then just return error. 2021 */ 2022 if (nentqueued == 0) 2023 return (0); 2024 2025 /* 2026 * Calculate the appropriate error return. 2027 */ 2028 runningcode = 0; 2029 if (nerror) 2030 runningcode = EIO; 2031 2032 if (uap->mode == LIO_WAIT) { 2033 int command, found, jobref; 2034 2035 for (;;) { 2036 found = 0; 2037 for (i = 0; i < uap->nent; i++) { 2038 /* 2039 * Fetch address of the control buf pointer in 2040 * user space. 2041 */ 2042 iocb = (struct aiocb *) 2043 (intptr_t)fuword(&cbptr[i]); 2044 if (((intptr_t)iocb == -1) || ((intptr_t)iocb 2045 == 0)) 2046 continue; 2047 2048 /* 2049 * Fetch the associated command from user space. 2050 */ 2051 command = fuword(&iocb->aio_lio_opcode); 2052 if (command == LIO_NOP) { 2053 found++; 2054 continue; 2055 } 2056 2057 jobref = 2058 fuword(&iocb->_aiocb_private.kernelinfo); 2059 2060 TAILQ_FOREACH(cb, &ki->kaio_jobdone, plist) { 2061 if (((intptr_t)cb->uaiocb._aiocb_private.kernelinfo) 2062 == jobref) { 2063 if (cb->uaiocb.aio_lio_opcode 2064 == LIO_WRITE) { 2065 p->p_stats->p_ru.ru_oublock 2066 += 2067 cb->outputcharge; 2068 cb->outputcharge = 0; 2069 } else if (cb->uaiocb.aio_lio_opcode 2070 == LIO_READ) { 2071 p->p_stats->p_ru.ru_inblock 2072 += cb->inputcharge; 2073 cb->inputcharge = 0; 2074 } 2075 found++; 2076 break; 2077 } 2078 } 2079 2080 s = splbio(); 2081 TAILQ_FOREACH(cb, &ki->kaio_bufdone, plist) { 2082 if (((intptr_t)cb->uaiocb._aiocb_private.kernelinfo) 2083 == jobref) { 2084 found++; 2085 break; 2086 } 2087 } 2088 splx(s); 2089 } 2090 2091 /* 2092 * If all I/Os have been disposed of, then we can 2093 * return. 2094 */ 2095 if (found == nentqueued) 2096 return (runningcode); 2097 2098 ki->kaio_flags |= KAIO_WAKEUP; 2099 error = tsleep(p, PRIBIO | PCATCH, "aiospn", 0); 2100 2101 if (error == EINTR) 2102 return (EINTR); 2103 else if (error == EWOULDBLOCK) 2104 return (EAGAIN); 2105 } 2106 } 2107 2108 return (runningcode); 2109 } 2110 2111 /* 2112 * This is a weird hack so that we can post a signal. It is safe to do so from 2113 * a timeout routine, but *not* from an interrupt routine. 2114 */ 2115 static void 2116 process_signal(void *aioj) 2117 { 2118 struct aiocblist *aiocbe = aioj; 2119 struct aio_liojob *lj = aiocbe->lio; 2120 struct aiocb *cb = &aiocbe->uaiocb; 2121 2122 if ((lj) && (lj->lioj_signal.sigev_notify == SIGEV_SIGNAL) && 2123 (lj->lioj_queue_count == lj->lioj_queue_finished_count)) { 2124 PROC_LOCK(lj->lioj_ki->kaio_p); 2125 psignal(lj->lioj_ki->kaio_p, lj->lioj_signal.sigev_signo); 2126 PROC_UNLOCK(lj->lioj_ki->kaio_p); 2127 lj->lioj_flags |= LIOJ_SIGNAL_POSTED; 2128 } 2129 2130 if (cb->aio_sigevent.sigev_notify == SIGEV_SIGNAL) { 2131 PROC_LOCK(aiocbe->userproc); 2132 psignal(aiocbe->userproc, cb->aio_sigevent.sigev_signo); 2133 PROC_UNLOCK(aiocbe->userproc); 2134 } 2135 } 2136 2137 /* 2138 * Interrupt handler for physio, performs the necessary process wakeups, and 2139 * signals. 2140 */ 2141 static void 2142 aio_physwakeup(struct buf *bp) 2143 { 2144 struct aiocblist *aiocbe; 2145 struct proc *p; 2146 struct kaioinfo *ki; 2147 struct aio_liojob *lj; 2148 2149 wakeup(bp); 2150 2151 aiocbe = (struct aiocblist *)bp->b_spc; 2152 if (aiocbe) { 2153 p = bp->b_caller1; 2154 2155 aiocbe->jobstate = JOBST_JOBBFINISHED; 2156 aiocbe->uaiocb._aiocb_private.status -= bp->b_resid; 2157 aiocbe->uaiocb._aiocb_private.error = 0; 2158 aiocbe->jobflags |= AIOCBLIST_DONE; 2159 2160 if (bp->b_ioflags & BIO_ERROR) 2161 aiocbe->uaiocb._aiocb_private.error = bp->b_error; 2162 2163 lj = aiocbe->lio; 2164 if (lj) { 2165 lj->lioj_buffer_finished_count++; 2166 2167 /* 2168 * wakeup/signal if all of the interrupt jobs are done. 2169 */ 2170 if (lj->lioj_buffer_finished_count == 2171 lj->lioj_buffer_count) { 2172 /* 2173 * Post a signal if it is called for. 2174 */ 2175 if ((lj->lioj_flags & 2176 (LIOJ_SIGNAL|LIOJ_SIGNAL_POSTED)) == 2177 LIOJ_SIGNAL) { 2178 lj->lioj_flags |= LIOJ_SIGNAL_POSTED; 2179 aiocbe->timeouthandle = 2180 timeout(process_signal, 2181 aiocbe, 0); 2182 } 2183 } 2184 } 2185 2186 ki = p->p_aioinfo; 2187 if (ki) { 2188 ki->kaio_buffer_finished_count++; 2189 TAILQ_REMOVE(&aio_bufjobs, aiocbe, list); 2190 TAILQ_REMOVE(&ki->kaio_bufqueue, aiocbe, plist); 2191 TAILQ_INSERT_TAIL(&ki->kaio_bufdone, aiocbe, plist); 2192 2193 KNOTE(&aiocbe->klist, 0); 2194 /* Do the wakeup. */ 2195 if (ki->kaio_flags & (KAIO_RUNDOWN|KAIO_WAKEUP)) { 2196 ki->kaio_flags &= ~KAIO_WAKEUP; 2197 wakeup(p); 2198 } 2199 } 2200 2201 if (aiocbe->uaiocb.aio_sigevent.sigev_notify == SIGEV_SIGNAL) 2202 aiocbe->timeouthandle = 2203 timeout(process_signal, aiocbe, 0); 2204 } 2205 } 2206 2207 /* syscall - wait for the next completion of an aio request */ 2208 int 2209 aio_waitcomplete(struct thread *td, struct aio_waitcomplete_args *uap) 2210 { 2211 struct proc *p = td->td_proc; 2212 struct timeval atv; 2213 struct timespec ts; 2214 struct kaioinfo *ki; 2215 struct aiocblist *cb = NULL; 2216 int error, s, timo; 2217 2218 suword(uap->aiocbp, (int)NULL); 2219 2220 timo = 0; 2221 if (uap->timeout) { 2222 /* Get timespec struct. */ 2223 error = copyin(uap->timeout, &ts, sizeof(ts)); 2224 if (error) 2225 return (error); 2226 2227 if ((ts.tv_nsec < 0) || (ts.tv_nsec >= 1000000000)) 2228 return (EINVAL); 2229 2230 TIMESPEC_TO_TIMEVAL(&atv, &ts); 2231 if (itimerfix(&atv)) 2232 return (EINVAL); 2233 timo = tvtohz(&atv); 2234 } 2235 2236 ki = p->p_aioinfo; 2237 if (ki == NULL) 2238 return (EAGAIN); 2239 2240 for (;;) { 2241 if ((cb = TAILQ_FIRST(&ki->kaio_jobdone)) != 0) { 2242 suword(uap->aiocbp, (uintptr_t)cb->uuaiocb); 2243 td->td_retval[0] = cb->uaiocb._aiocb_private.status; 2244 if (cb->uaiocb.aio_lio_opcode == LIO_WRITE) { 2245 p->p_stats->p_ru.ru_oublock += 2246 cb->outputcharge; 2247 cb->outputcharge = 0; 2248 } else if (cb->uaiocb.aio_lio_opcode == LIO_READ) { 2249 p->p_stats->p_ru.ru_inblock += cb->inputcharge; 2250 cb->inputcharge = 0; 2251 } 2252 aio_free_entry(cb); 2253 return (cb->uaiocb._aiocb_private.error); 2254 } 2255 2256 s = splbio(); 2257 if ((cb = TAILQ_FIRST(&ki->kaio_bufdone)) != 0 ) { 2258 splx(s); 2259 suword(uap->aiocbp, (uintptr_t)cb->uuaiocb); 2260 td->td_retval[0] = cb->uaiocb._aiocb_private.status; 2261 aio_free_entry(cb); 2262 return (cb->uaiocb._aiocb_private.error); 2263 } 2264 2265 ki->kaio_flags |= KAIO_WAKEUP; 2266 error = tsleep(p, PRIBIO | PCATCH, "aiowc", timo); 2267 splx(s); 2268 2269 if (error == ERESTART) 2270 return (EINTR); 2271 else if (error < 0) 2272 return (error); 2273 else if (error == EINTR) 2274 return (EINTR); 2275 else if (error == EWOULDBLOCK) 2276 return (EAGAIN); 2277 } 2278 } 2279 2280 /* kqueue attach function */ 2281 static int 2282 filt_aioattach(struct knote *kn) 2283 { 2284 struct aiocblist *aiocbe = (struct aiocblist *)kn->kn_sdata; 2285 2286 /* 2287 * The aiocbe pointer must be validated before using it, so 2288 * registration is restricted to the kernel; the user cannot 2289 * set EV_FLAG1. 2290 */ 2291 if ((kn->kn_flags & EV_FLAG1) == 0) 2292 return (EPERM); 2293 kn->kn_flags &= ~EV_FLAG1; 2294 2295 SLIST_INSERT_HEAD(&aiocbe->klist, kn, kn_selnext); 2296 2297 return (0); 2298 } 2299 2300 /* kqueue detach function */ 2301 static void 2302 filt_aiodetach(struct knote *kn) 2303 { 2304 struct aiocblist *aiocbe = (struct aiocblist *)kn->kn_sdata; 2305 2306 SLIST_REMOVE(&aiocbe->klist, kn, knote, kn_selnext); 2307 } 2308 2309 /* kqueue filter function */ 2310 /*ARGSUSED*/ 2311 static int 2312 filt_aio(struct knote *kn, long hint) 2313 { 2314 struct aiocblist *aiocbe = (struct aiocblist *)kn->kn_sdata; 2315 2316 kn->kn_data = aiocbe->uaiocb._aiocb_private.error; 2317 if (aiocbe->jobstate != JOBST_JOBFINISHED && 2318 aiocbe->jobstate != JOBST_JOBBFINISHED) 2319 return (0); 2320 kn->kn_flags |= EV_EOF; 2321 return (1); 2322 } 2323