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