1 /* 2 * Copyright (c) 1997 John S. Dyson. All rights reserved. 3 * 4 * Redistribution and use in source and binary forms, with or without 5 * modification, are permitted provided that the following conditions 6 * are met: 7 * 1. Redistributions of source code must retain the above copyright 8 * notice, this list of conditions and the following disclaimer. 9 * 2. John S. Dyson's name may not be used to endorse or promote products 10 * derived from this software without specific prior written permission. 11 * 12 * DISCLAIMER: This code isn't warranted to do anything useful. Anything 13 * bad that happens because of using this software isn't the responsibility 14 * of the author. This software is distributed AS-IS. 15 */ 16 17 /* 18 * This file contains support for the POSIX 1003.1B AIO/LIO facility. 19 */ 20 21 #include <sys/cdefs.h> 22 __FBSDID("$FreeBSD$"); 23 24 #include <sys/param.h> 25 #include <sys/systm.h> 26 #include <sys/malloc.h> 27 #include <sys/bio.h> 28 #include <sys/buf.h> 29 #include <sys/eventhandler.h> 30 #include <sys/sysproto.h> 31 #include <sys/filedesc.h> 32 #include <sys/kernel.h> 33 #include <sys/kthread.h> 34 #include <sys/fcntl.h> 35 #include <sys/file.h> 36 #include <sys/limits.h> 37 #include <sys/lock.h> 38 #include <sys/mutex.h> 39 #include <sys/unistd.h> 40 #include <sys/proc.h> 41 #include <sys/resourcevar.h> 42 #include <sys/signalvar.h> 43 #include <sys/protosw.h> 44 #include <sys/socketvar.h> 45 #include <sys/syscall.h> 46 #include <sys/sysent.h> 47 #include <sys/sysctl.h> 48 #include <sys/sx.h> 49 #include <sys/vnode.h> 50 #include <sys/conf.h> 51 #include <sys/event.h> 52 53 #include <posix4/posix4.h> 54 #include <vm/vm.h> 55 #include <vm/vm_extern.h> 56 #include <vm/pmap.h> 57 #include <vm/vm_map.h> 58 #include <vm/uma.h> 59 #include <sys/aio.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 mtx_lock(&Giant); 547 ki->kaio_flags |= LIOJ_SIGNAL_POSTED; 548 while ((ki->kaio_active_count > 0) || (ki->kaio_buffer_count > 549 ki->kaio_buffer_finished_count)) { 550 ki->kaio_flags |= KAIO_RUNDOWN; 551 if (tsleep(p, PRIBIO, "kaiowt", aiod_timeout)) 552 break; 553 } 554 555 /* 556 * Move any aio ops that are waiting on socket I/O to the normal job 557 * queues so they are cleaned up with any others. 558 */ 559 s = splnet(); 560 for (aiocbe = TAILQ_FIRST(&ki->kaio_sockqueue); aiocbe; aiocbe = 561 aiocbn) { 562 aiocbn = TAILQ_NEXT(aiocbe, plist); 563 fp = aiocbe->fd_file; 564 if (fp != NULL) { 565 so = fp->f_data; 566 TAILQ_REMOVE(&so->so_aiojobq, aiocbe, list); 567 if (TAILQ_EMPTY(&so->so_aiojobq)) { 568 so->so_snd.sb_flags &= ~SB_AIO; 569 so->so_rcv.sb_flags &= ~SB_AIO; 570 } 571 } 572 TAILQ_REMOVE(&ki->kaio_sockqueue, aiocbe, plist); 573 TAILQ_INSERT_HEAD(&aio_jobs, aiocbe, list); 574 TAILQ_INSERT_HEAD(&ki->kaio_jobqueue, aiocbe, plist); 575 } 576 splx(s); 577 578 restart1: 579 for (aiocbe = TAILQ_FIRST(&ki->kaio_jobdone); aiocbe; aiocbe = aiocbn) { 580 aiocbn = TAILQ_NEXT(aiocbe, plist); 581 if (aio_free_entry(aiocbe)) 582 goto restart1; 583 } 584 585 restart2: 586 for (aiocbe = TAILQ_FIRST(&ki->kaio_jobqueue); aiocbe; aiocbe = 587 aiocbn) { 588 aiocbn = TAILQ_NEXT(aiocbe, plist); 589 if (aio_free_entry(aiocbe)) 590 goto restart2; 591 } 592 593 /* 594 * Note the use of lots of splbio here, trying to avoid splbio for long chains 595 * of I/O. Probably unnecessary. 596 */ 597 restart3: 598 s = splbio(); 599 while (TAILQ_FIRST(&ki->kaio_bufqueue)) { 600 ki->kaio_flags |= KAIO_WAKEUP; 601 tsleep(p, PRIBIO, "aioprn", 0); 602 splx(s); 603 goto restart3; 604 } 605 splx(s); 606 607 restart4: 608 s = splbio(); 609 for (aiocbe = TAILQ_FIRST(&ki->kaio_bufdone); aiocbe; aiocbe = aiocbn) { 610 aiocbn = TAILQ_NEXT(aiocbe, plist); 611 if (aio_free_entry(aiocbe)) { 612 splx(s); 613 goto restart4; 614 } 615 } 616 splx(s); 617 618 /* 619 * If we've slept, jobs might have moved from one queue to another. 620 * Retry rundown if we didn't manage to empty the queues. 621 */ 622 if (TAILQ_FIRST(&ki->kaio_jobdone) != NULL || 623 TAILQ_FIRST(&ki->kaio_jobqueue) != NULL || 624 TAILQ_FIRST(&ki->kaio_bufqueue) != NULL || 625 TAILQ_FIRST(&ki->kaio_bufdone) != NULL) 626 goto restart1; 627 628 for (lj = TAILQ_FIRST(&ki->kaio_liojoblist); lj; lj = ljn) { 629 ljn = TAILQ_NEXT(lj, lioj_list); 630 if ((lj->lioj_buffer_count == 0) && (lj->lioj_queue_count == 631 0)) { 632 TAILQ_REMOVE(&ki->kaio_liojoblist, lj, lioj_list); 633 uma_zfree(aiolio_zone, lj); 634 } else { 635 #ifdef DIAGNOSTIC 636 printf("LIO job not cleaned up: B:%d, BF:%d, Q:%d, " 637 "QF:%d\n", lj->lioj_buffer_count, 638 lj->lioj_buffer_finished_count, 639 lj->lioj_queue_count, 640 lj->lioj_queue_finished_count); 641 #endif 642 } 643 } 644 645 uma_zfree(kaio_zone, ki); 646 p->p_aioinfo = NULL; 647 mtx_unlock(&Giant); 648 } 649 650 /* 651 * Select a job to run (called by an AIO daemon). 652 */ 653 static struct aiocblist * 654 aio_selectjob(struct aiothreadlist *aiop) 655 { 656 int s; 657 struct aiocblist *aiocbe; 658 struct kaioinfo *ki; 659 struct proc *userp; 660 661 s = splnet(); 662 for (aiocbe = TAILQ_FIRST(&aio_jobs); aiocbe; aiocbe = 663 TAILQ_NEXT(aiocbe, list)) { 664 userp = aiocbe->userproc; 665 ki = userp->p_aioinfo; 666 667 if (ki->kaio_active_count < ki->kaio_maxactive_count) { 668 TAILQ_REMOVE(&aio_jobs, aiocbe, list); 669 splx(s); 670 return (aiocbe); 671 } 672 } 673 splx(s); 674 675 return (NULL); 676 } 677 678 /* 679 * The AIO processing activity. This is the code that does the I/O request for 680 * the non-physio version of the operations. The normal vn operations are used, 681 * and this code should work in all instances for every type of file, including 682 * pipes, sockets, fifos, and regular files. 683 */ 684 static void 685 aio_process(struct aiocblist *aiocbe) 686 { 687 struct ucred *td_savedcred; 688 struct thread *td; 689 struct proc *mycp; 690 struct aiocb *cb; 691 struct file *fp; 692 struct uio auio; 693 struct iovec aiov; 694 int cnt; 695 int error; 696 int oublock_st, oublock_end; 697 int inblock_st, inblock_end; 698 699 td = curthread; 700 td_savedcred = td->td_ucred; 701 td->td_ucred = aiocbe->cred; 702 mycp = td->td_proc; 703 cb = &aiocbe->uaiocb; 704 fp = aiocbe->fd_file; 705 706 aiov.iov_base = (void *)(uintptr_t)cb->aio_buf; 707 aiov.iov_len = cb->aio_nbytes; 708 709 auio.uio_iov = &aiov; 710 auio.uio_iovcnt = 1; 711 auio.uio_offset = cb->aio_offset; 712 auio.uio_resid = cb->aio_nbytes; 713 cnt = cb->aio_nbytes; 714 auio.uio_segflg = UIO_USERSPACE; 715 auio.uio_td = td; 716 717 inblock_st = mycp->p_stats->p_ru.ru_inblock; 718 oublock_st = mycp->p_stats->p_ru.ru_oublock; 719 /* 720 * _aio_aqueue() acquires a reference to the file that is 721 * released in aio_free_entry(). 722 */ 723 if (cb->aio_lio_opcode == LIO_READ) { 724 auio.uio_rw = UIO_READ; 725 error = fo_read(fp, &auio, fp->f_cred, FOF_OFFSET, td); 726 } else { 727 auio.uio_rw = UIO_WRITE; 728 error = fo_write(fp, &auio, fp->f_cred, FOF_OFFSET, td); 729 } 730 inblock_end = mycp->p_stats->p_ru.ru_inblock; 731 oublock_end = mycp->p_stats->p_ru.ru_oublock; 732 733 aiocbe->inputcharge = inblock_end - inblock_st; 734 aiocbe->outputcharge = oublock_end - oublock_st; 735 736 if ((error) && (auio.uio_resid != cnt)) { 737 if (error == ERESTART || error == EINTR || error == EWOULDBLOCK) 738 error = 0; 739 if ((error == EPIPE) && (cb->aio_lio_opcode == LIO_WRITE)) { 740 PROC_LOCK(aiocbe->userproc); 741 psignal(aiocbe->userproc, SIGPIPE); 742 PROC_UNLOCK(aiocbe->userproc); 743 } 744 } 745 746 cnt -= auio.uio_resid; 747 cb->_aiocb_private.error = error; 748 cb->_aiocb_private.status = cnt; 749 td->td_ucred = td_savedcred; 750 } 751 752 /* 753 * The AIO daemon, most of the actual work is done in aio_process, 754 * but the setup (and address space mgmt) is done in this routine. 755 */ 756 static void 757 aio_daemon(void *uproc) 758 { 759 int s; 760 struct aio_liojob *lj; 761 struct aiocb *cb; 762 struct aiocblist *aiocbe; 763 struct aiothreadlist *aiop; 764 struct kaioinfo *ki; 765 struct proc *curcp, *mycp, *userp; 766 struct vmspace *myvm, *tmpvm; 767 struct thread *td = curthread; 768 struct pgrp *newpgrp; 769 struct session *newsess; 770 771 mtx_lock(&Giant); 772 /* 773 * Local copies of curproc (cp) and vmspace (myvm) 774 */ 775 mycp = td->td_proc; 776 myvm = mycp->p_vmspace; 777 778 KASSERT(mycp->p_textvp == NULL, ("kthread has a textvp")); 779 780 /* 781 * Allocate and ready the aio control info. There is one aiop structure 782 * per daemon. 783 */ 784 aiop = uma_zalloc(aiop_zone, M_WAITOK); 785 aiop->aiothread = td; 786 aiop->aiothreadflags |= AIOP_FREE; 787 788 s = splnet(); 789 790 /* 791 * Place thread (lightweight process) onto the AIO free thread list. 792 */ 793 if (TAILQ_EMPTY(&aio_freeproc)) 794 wakeup(&aio_freeproc); 795 TAILQ_INSERT_HEAD(&aio_freeproc, aiop, list); 796 797 splx(s); 798 799 /* 800 * Get rid of our current filedescriptors. AIOD's don't need any 801 * filedescriptors, except as temporarily inherited from the client. 802 */ 803 fdfree(td); 804 805 mtx_unlock(&Giant); 806 /* The daemon resides in its own pgrp. */ 807 MALLOC(newpgrp, struct pgrp *, sizeof(struct pgrp), M_PGRP, 808 M_WAITOK | M_ZERO); 809 MALLOC(newsess, struct session *, sizeof(struct session), M_SESSION, 810 M_WAITOK | M_ZERO); 811 812 sx_xlock(&proctree_lock); 813 enterpgrp(mycp, mycp->p_pid, newpgrp, newsess); 814 sx_xunlock(&proctree_lock); 815 mtx_lock(&Giant); 816 817 /* 818 * Wakeup parent process. (Parent sleeps to keep from blasting away 819 * and creating too many daemons.) 820 */ 821 wakeup(mycp); 822 823 for (;;) { 824 /* 825 * curcp is the current daemon process context. 826 * userp is the current user process context. 827 */ 828 curcp = mycp; 829 830 /* 831 * Take daemon off of free queue 832 */ 833 if (aiop->aiothreadflags & AIOP_FREE) { 834 s = splnet(); 835 TAILQ_REMOVE(&aio_freeproc, aiop, list); 836 TAILQ_INSERT_TAIL(&aio_activeproc, aiop, list); 837 aiop->aiothreadflags &= ~AIOP_FREE; 838 splx(s); 839 } 840 aiop->aiothreadflags &= ~AIOP_SCHED; 841 842 /* 843 * Check for jobs. 844 */ 845 while ((aiocbe = aio_selectjob(aiop)) != NULL) { 846 cb = &aiocbe->uaiocb; 847 userp = aiocbe->userproc; 848 849 aiocbe->jobstate = JOBST_JOBRUNNING; 850 851 /* 852 * Connect to process address space for user program. 853 */ 854 if (userp != curcp) { 855 /* 856 * Save the current address space that we are 857 * connected to. 858 */ 859 tmpvm = mycp->p_vmspace; 860 861 /* 862 * Point to the new user address space, and 863 * refer to it. 864 */ 865 mycp->p_vmspace = userp->p_vmspace; 866 mycp->p_vmspace->vm_refcnt++; 867 868 /* Activate the new mapping. */ 869 pmap_activate(FIRST_THREAD_IN_PROC(mycp)); 870 871 /* 872 * If the old address space wasn't the daemons 873 * own address space, then we need to remove the 874 * daemon's reference from the other process 875 * that it was acting on behalf of. 876 */ 877 if (tmpvm != myvm) { 878 vmspace_free(tmpvm); 879 } 880 curcp = userp; 881 } 882 883 ki = userp->p_aioinfo; 884 lj = aiocbe->lio; 885 886 /* Account for currently active jobs. */ 887 ki->kaio_active_count++; 888 889 /* Do the I/O function. */ 890 aio_process(aiocbe); 891 892 /* Decrement the active job count. */ 893 ki->kaio_active_count--; 894 895 /* 896 * Increment the completion count for wakeup/signal 897 * comparisons. 898 */ 899 aiocbe->jobflags |= AIOCBLIST_DONE; 900 ki->kaio_queue_finished_count++; 901 if (lj) 902 lj->lioj_queue_finished_count++; 903 if ((ki->kaio_flags & KAIO_WAKEUP) || ((ki->kaio_flags 904 & KAIO_RUNDOWN) && (ki->kaio_active_count == 0))) { 905 ki->kaio_flags &= ~KAIO_WAKEUP; 906 wakeup(userp); 907 } 908 909 s = splbio(); 910 if (lj && (lj->lioj_flags & 911 (LIOJ_SIGNAL|LIOJ_SIGNAL_POSTED)) == LIOJ_SIGNAL) { 912 if ((lj->lioj_queue_finished_count == 913 lj->lioj_queue_count) && 914 (lj->lioj_buffer_finished_count == 915 lj->lioj_buffer_count)) { 916 PROC_LOCK(userp); 917 psignal(userp, 918 lj->lioj_signal.sigev_signo); 919 PROC_UNLOCK(userp); 920 lj->lioj_flags |= LIOJ_SIGNAL_POSTED; 921 } 922 } 923 splx(s); 924 925 aiocbe->jobstate = JOBST_JOBFINISHED; 926 927 s = splnet(); 928 TAILQ_REMOVE(&ki->kaio_jobqueue, aiocbe, plist); 929 TAILQ_INSERT_TAIL(&ki->kaio_jobdone, aiocbe, plist); 930 splx(s); 931 KNOTE(&aiocbe->klist, 0); 932 933 if (aiocbe->jobflags & AIOCBLIST_RUNDOWN) { 934 wakeup(aiocbe); 935 aiocbe->jobflags &= ~AIOCBLIST_RUNDOWN; 936 } 937 938 if (cb->aio_sigevent.sigev_notify == SIGEV_SIGNAL) { 939 PROC_LOCK(userp); 940 psignal(userp, cb->aio_sigevent.sigev_signo); 941 PROC_UNLOCK(userp); 942 } 943 } 944 945 /* 946 * Disconnect from user address space. 947 */ 948 if (curcp != mycp) { 949 /* Get the user address space to disconnect from. */ 950 tmpvm = mycp->p_vmspace; 951 952 /* Get original address space for daemon. */ 953 mycp->p_vmspace = myvm; 954 955 /* Activate the daemon's address space. */ 956 pmap_activate(FIRST_THREAD_IN_PROC(mycp)); 957 #ifdef DIAGNOSTIC 958 if (tmpvm == myvm) { 959 printf("AIOD: vmspace problem -- %d\n", 960 mycp->p_pid); 961 } 962 #endif 963 /* Remove our vmspace reference. */ 964 vmspace_free(tmpvm); 965 966 curcp = mycp; 967 } 968 969 /* 970 * If we are the first to be put onto the free queue, wakeup 971 * anyone waiting for a daemon. 972 */ 973 s = splnet(); 974 TAILQ_REMOVE(&aio_activeproc, aiop, list); 975 if (TAILQ_EMPTY(&aio_freeproc)) 976 wakeup(&aio_freeproc); 977 TAILQ_INSERT_HEAD(&aio_freeproc, aiop, list); 978 aiop->aiothreadflags |= AIOP_FREE; 979 splx(s); 980 981 /* 982 * If daemon is inactive for a long time, allow it to exit, 983 * thereby freeing resources. 984 */ 985 if ((aiop->aiothreadflags & AIOP_SCHED) == 0 && 986 tsleep(aiop->aiothread, PRIBIO, "aiordy", aiod_lifetime)) { 987 s = splnet(); 988 if (TAILQ_EMPTY(&aio_jobs)) { 989 if ((aiop->aiothreadflags & AIOP_FREE) && 990 (num_aio_procs > target_aio_procs)) { 991 TAILQ_REMOVE(&aio_freeproc, aiop, list); 992 splx(s); 993 uma_zfree(aiop_zone, aiop); 994 num_aio_procs--; 995 #ifdef DIAGNOSTIC 996 if (mycp->p_vmspace->vm_refcnt <= 1) { 997 printf("AIOD: bad vm refcnt for" 998 " exiting daemon: %d\n", 999 mycp->p_vmspace->vm_refcnt); 1000 } 1001 #endif 1002 kthread_exit(0); 1003 } 1004 } 1005 splx(s); 1006 } 1007 } 1008 } 1009 1010 /* 1011 * Create a new AIO daemon. This is mostly a kernel-thread fork routine. The 1012 * AIO daemon modifies its environment itself. 1013 */ 1014 static int 1015 aio_newproc(void) 1016 { 1017 int error; 1018 struct proc *p; 1019 1020 error = kthread_create(aio_daemon, curproc, &p, RFNOWAIT, 0, "aiod%d", 1021 num_aio_procs); 1022 if (error) 1023 return (error); 1024 1025 /* 1026 * Wait until daemon is started, but continue on just in case to 1027 * handle error conditions. 1028 */ 1029 error = tsleep(p, PZERO, "aiosta", aiod_timeout); 1030 1031 num_aio_procs++; 1032 1033 return (error); 1034 } 1035 1036 /* 1037 * Try the high-performance, low-overhead physio method for eligible 1038 * VCHR devices. This method doesn't use an aio helper thread, and 1039 * thus has very low overhead. 1040 * 1041 * Assumes that the caller, _aio_aqueue(), has incremented the file 1042 * structure's reference count, preventing its deallocation for the 1043 * duration of this call. 1044 */ 1045 static int 1046 aio_qphysio(struct proc *p, struct aiocblist *aiocbe) 1047 { 1048 int error; 1049 struct aiocb *cb; 1050 struct file *fp; 1051 struct buf *bp; 1052 struct vnode *vp; 1053 struct kaioinfo *ki; 1054 struct aio_liojob *lj; 1055 int s; 1056 int notify; 1057 1058 cb = &aiocbe->uaiocb; 1059 fp = aiocbe->fd_file; 1060 1061 if (fp->f_type != DTYPE_VNODE) 1062 return (-1); 1063 1064 vp = fp->f_vnode; 1065 1066 /* 1067 * If its not a disk, we don't want to return a positive error. 1068 * It causes the aio code to not fall through to try the thread 1069 * way when you're talking to a regular file. 1070 */ 1071 if (!vn_isdisk(vp, &error)) { 1072 if (error == ENOTBLK) 1073 return (-1); 1074 else 1075 return (error); 1076 } 1077 1078 if (cb->aio_nbytes % vp->v_rdev->si_bsize_phys) 1079 return (-1); 1080 1081 if (cb->aio_nbytes > 1082 MAXPHYS - (((vm_offset_t) cb->aio_buf) & PAGE_MASK)) 1083 return (-1); 1084 1085 ki = p->p_aioinfo; 1086 if (ki->kaio_buffer_count >= ki->kaio_ballowed_count) 1087 return (-1); 1088 1089 ki->kaio_buffer_count++; 1090 1091 lj = aiocbe->lio; 1092 if (lj) 1093 lj->lioj_buffer_count++; 1094 1095 /* Create and build a buffer header for a transfer. */ 1096 bp = (struct buf *)getpbuf(NULL); 1097 BUF_KERNPROC(bp); 1098 1099 /* 1100 * Get a copy of the kva from the physical buffer. 1101 */ 1102 bp->b_dev = vp->v_rdev; 1103 error = 0; 1104 1105 bp->b_bcount = cb->aio_nbytes; 1106 bp->b_bufsize = cb->aio_nbytes; 1107 bp->b_iodone = aio_physwakeup; 1108 bp->b_saveaddr = bp->b_data; 1109 bp->b_data = (void *)(uintptr_t)cb->aio_buf; 1110 bp->b_offset = cb->aio_offset; 1111 bp->b_iooffset = cb->aio_offset; 1112 bp->b_blkno = btodb(cb->aio_offset); 1113 bp->b_iocmd = cb->aio_lio_opcode == LIO_WRITE ? BIO_WRITE : BIO_READ; 1114 1115 /* 1116 * Bring buffer into kernel space. 1117 */ 1118 if (vmapbuf(bp) < 0) { 1119 error = EFAULT; 1120 goto doerror; 1121 } 1122 1123 s = splbio(); 1124 aiocbe->bp = bp; 1125 bp->b_caller1 = (void *)aiocbe; 1126 TAILQ_INSERT_TAIL(&aio_bufjobs, aiocbe, list); 1127 TAILQ_INSERT_TAIL(&ki->kaio_bufqueue, aiocbe, plist); 1128 aiocbe->jobstate = JOBST_JOBQBUF; 1129 cb->_aiocb_private.status = cb->aio_nbytes; 1130 num_buf_aio++; 1131 bp->b_error = 0; 1132 1133 splx(s); 1134 1135 /* Perform transfer. */ 1136 DEV_STRATEGY(bp); 1137 1138 notify = 0; 1139 s = splbio(); 1140 1141 /* 1142 * If we had an error invoking the request, or an error in processing 1143 * the request before we have returned, we process it as an error in 1144 * transfer. Note that such an I/O error is not indicated immediately, 1145 * but is returned using the aio_error mechanism. In this case, 1146 * aio_suspend will return immediately. 1147 */ 1148 if (bp->b_error || (bp->b_ioflags & BIO_ERROR)) { 1149 struct aiocb *job = aiocbe->uuaiocb; 1150 1151 aiocbe->uaiocb._aiocb_private.status = 0; 1152 suword(&job->_aiocb_private.status, 0); 1153 aiocbe->uaiocb._aiocb_private.error = bp->b_error; 1154 suword(&job->_aiocb_private.error, bp->b_error); 1155 1156 ki->kaio_buffer_finished_count++; 1157 1158 if (aiocbe->jobstate != JOBST_JOBBFINISHED) { 1159 aiocbe->jobstate = JOBST_JOBBFINISHED; 1160 aiocbe->jobflags |= AIOCBLIST_DONE; 1161 TAILQ_REMOVE(&aio_bufjobs, aiocbe, list); 1162 TAILQ_REMOVE(&ki->kaio_bufqueue, aiocbe, plist); 1163 TAILQ_INSERT_TAIL(&ki->kaio_bufdone, aiocbe, plist); 1164 notify = 1; 1165 } 1166 } 1167 splx(s); 1168 if (notify) 1169 KNOTE(&aiocbe->klist, 0); 1170 return (0); 1171 1172 doerror: 1173 ki->kaio_buffer_count--; 1174 if (lj) 1175 lj->lioj_buffer_count--; 1176 aiocbe->bp = NULL; 1177 relpbuf(bp, NULL); 1178 return (error); 1179 } 1180 1181 /* 1182 * This waits/tests physio completion. 1183 */ 1184 static int 1185 aio_fphysio(struct aiocblist *iocb) 1186 { 1187 int s; 1188 struct buf *bp; 1189 int error; 1190 1191 bp = iocb->bp; 1192 1193 s = splbio(); 1194 while ((bp->b_flags & B_DONE) == 0) { 1195 if (tsleep(bp, PRIBIO, "physstr", aiod_timeout)) { 1196 if ((bp->b_flags & B_DONE) == 0) { 1197 splx(s); 1198 return (EINPROGRESS); 1199 } else 1200 break; 1201 } 1202 } 1203 splx(s); 1204 1205 /* Release mapping into kernel space. */ 1206 vunmapbuf(bp); 1207 iocb->bp = 0; 1208 1209 error = 0; 1210 1211 /* Check for an error. */ 1212 if (bp->b_ioflags & BIO_ERROR) 1213 error = bp->b_error; 1214 1215 relpbuf(bp, NULL); 1216 return (error); 1217 } 1218 1219 /* 1220 * Wake up aio requests that may be serviceable now. 1221 */ 1222 static void 1223 aio_swake_cb(struct socket *so, struct sockbuf *sb) 1224 { 1225 struct aiocblist *cb,*cbn; 1226 struct proc *p; 1227 struct kaioinfo *ki = NULL; 1228 int opcode, wakecount = 0; 1229 struct aiothreadlist *aiop; 1230 1231 if (sb == &so->so_snd) { 1232 opcode = LIO_WRITE; 1233 so->so_snd.sb_flags &= ~SB_AIO; 1234 } else { 1235 opcode = LIO_READ; 1236 so->so_rcv.sb_flags &= ~SB_AIO; 1237 } 1238 1239 for (cb = TAILQ_FIRST(&so->so_aiojobq); cb; cb = cbn) { 1240 cbn = TAILQ_NEXT(cb, list); 1241 if (opcode == cb->uaiocb.aio_lio_opcode) { 1242 p = cb->userproc; 1243 ki = p->p_aioinfo; 1244 TAILQ_REMOVE(&so->so_aiojobq, cb, list); 1245 TAILQ_REMOVE(&ki->kaio_sockqueue, cb, plist); 1246 TAILQ_INSERT_TAIL(&aio_jobs, cb, list); 1247 TAILQ_INSERT_TAIL(&ki->kaio_jobqueue, cb, plist); 1248 wakecount++; 1249 if (cb->jobstate != JOBST_JOBQGLOBAL) 1250 panic("invalid queue value"); 1251 } 1252 } 1253 1254 while (wakecount--) { 1255 if ((aiop = TAILQ_FIRST(&aio_freeproc)) != 0) { 1256 TAILQ_REMOVE(&aio_freeproc, aiop, list); 1257 TAILQ_INSERT_TAIL(&aio_activeproc, aiop, list); 1258 aiop->aiothreadflags &= ~AIOP_FREE; 1259 wakeup(aiop->aiothread); 1260 } 1261 } 1262 } 1263 1264 /* 1265 * Queue a new AIO request. Choosing either the threaded or direct physio VCHR 1266 * technique is done in this code. 1267 */ 1268 static int 1269 _aio_aqueue(struct thread *td, struct aiocb *job, struct aio_liojob *lj, int type) 1270 { 1271 struct proc *p = td->td_proc; 1272 struct filedesc *fdp; 1273 struct file *fp; 1274 unsigned int fd; 1275 struct socket *so; 1276 int s; 1277 int error; 1278 int opcode, user_opcode; 1279 struct aiocblist *aiocbe; 1280 struct aiothreadlist *aiop; 1281 struct kaioinfo *ki; 1282 struct kevent kev; 1283 struct kqueue *kq; 1284 struct file *kq_fp; 1285 1286 aiocbe = uma_zalloc(aiocb_zone, M_WAITOK); 1287 aiocbe->inputcharge = 0; 1288 aiocbe->outputcharge = 0; 1289 callout_handle_init(&aiocbe->timeouthandle); 1290 SLIST_INIT(&aiocbe->klist); 1291 1292 suword(&job->_aiocb_private.status, -1); 1293 suword(&job->_aiocb_private.error, 0); 1294 suword(&job->_aiocb_private.kernelinfo, -1); 1295 1296 error = copyin(job, &aiocbe->uaiocb, sizeof(aiocbe->uaiocb)); 1297 if (error) { 1298 suword(&job->_aiocb_private.error, error); 1299 uma_zfree(aiocb_zone, aiocbe); 1300 return (error); 1301 } 1302 if (aiocbe->uaiocb.aio_sigevent.sigev_notify == SIGEV_SIGNAL && 1303 !_SIG_VALID(aiocbe->uaiocb.aio_sigevent.sigev_signo)) { 1304 uma_zfree(aiocb_zone, aiocbe); 1305 return (EINVAL); 1306 } 1307 1308 /* Save userspace address of the job info. */ 1309 aiocbe->uuaiocb = job; 1310 1311 /* Get the opcode. */ 1312 user_opcode = aiocbe->uaiocb.aio_lio_opcode; 1313 if (type != LIO_NOP) 1314 aiocbe->uaiocb.aio_lio_opcode = type; 1315 opcode = aiocbe->uaiocb.aio_lio_opcode; 1316 1317 /* Get the fd info for process. */ 1318 fdp = p->p_fd; 1319 1320 /* 1321 * Range check file descriptor. 1322 */ 1323 FILEDESC_LOCK(fdp); 1324 fd = aiocbe->uaiocb.aio_fildes; 1325 if (fd >= fdp->fd_nfiles) { 1326 FILEDESC_UNLOCK(fdp); 1327 uma_zfree(aiocb_zone, aiocbe); 1328 if (type == 0) 1329 suword(&job->_aiocb_private.error, EBADF); 1330 return (EBADF); 1331 } 1332 1333 fp = aiocbe->fd_file = fdp->fd_ofiles[fd]; 1334 if ((fp == NULL) || 1335 ((opcode == LIO_WRITE) && ((fp->f_flag & FWRITE) == 0)) || 1336 ((opcode == LIO_READ) && ((fp->f_flag & FREAD) == 0))) { 1337 FILEDESC_UNLOCK(fdp); 1338 uma_zfree(aiocb_zone, aiocbe); 1339 if (type == 0) 1340 suword(&job->_aiocb_private.error, EBADF); 1341 return (EBADF); 1342 } 1343 fhold(fp); 1344 FILEDESC_UNLOCK(fdp); 1345 1346 if (aiocbe->uaiocb.aio_offset == -1LL) { 1347 error = EINVAL; 1348 goto aqueue_fail; 1349 } 1350 error = suword(&job->_aiocb_private.kernelinfo, jobrefid); 1351 if (error) { 1352 error = EINVAL; 1353 goto aqueue_fail; 1354 } 1355 aiocbe->uaiocb._aiocb_private.kernelinfo = (void *)(intptr_t)jobrefid; 1356 if (jobrefid == LONG_MAX) 1357 jobrefid = 1; 1358 else 1359 jobrefid++; 1360 1361 if (opcode == LIO_NOP) { 1362 fdrop(fp, td); 1363 uma_zfree(aiocb_zone, aiocbe); 1364 if (type == 0) { 1365 suword(&job->_aiocb_private.error, 0); 1366 suword(&job->_aiocb_private.status, 0); 1367 suword(&job->_aiocb_private.kernelinfo, 0); 1368 } 1369 return (0); 1370 } 1371 if ((opcode != LIO_READ) && (opcode != LIO_WRITE)) { 1372 if (type == 0) 1373 suword(&job->_aiocb_private.status, 0); 1374 error = EINVAL; 1375 goto aqueue_fail; 1376 } 1377 1378 if (aiocbe->uaiocb.aio_sigevent.sigev_notify == SIGEV_KEVENT) { 1379 kev.ident = aiocbe->uaiocb.aio_sigevent.sigev_notify_kqueue; 1380 kev.udata = aiocbe->uaiocb.aio_sigevent.sigev_value.sigval_ptr; 1381 } 1382 else { 1383 /* 1384 * This method for requesting kevent-based notification won't 1385 * work on the alpha, since we're passing in a pointer 1386 * via aio_lio_opcode, which is an int. Use the SIGEV_KEVENT- 1387 * based method instead. 1388 */ 1389 if (user_opcode == LIO_NOP || user_opcode == LIO_READ || 1390 user_opcode == LIO_WRITE) 1391 goto no_kqueue; 1392 1393 error = copyin((struct kevent *)(uintptr_t)user_opcode, 1394 &kev, sizeof(kev)); 1395 if (error) 1396 goto aqueue_fail; 1397 } 1398 if ((u_int)kev.ident >= fdp->fd_nfiles || 1399 (kq_fp = fdp->fd_ofiles[kev.ident]) == NULL || 1400 (kq_fp->f_type != DTYPE_KQUEUE)) { 1401 error = EBADF; 1402 goto aqueue_fail; 1403 } 1404 kq = kq_fp->f_data; 1405 kev.ident = (uintptr_t)aiocbe->uuaiocb; 1406 kev.filter = EVFILT_AIO; 1407 kev.flags = EV_ADD | EV_ENABLE | EV_FLAG1; 1408 kev.data = (intptr_t)aiocbe; 1409 error = kqueue_register(kq, &kev, td); 1410 aqueue_fail: 1411 if (error) { 1412 fdrop(fp, td); 1413 uma_zfree(aiocb_zone, aiocbe); 1414 if (type == 0) 1415 suword(&job->_aiocb_private.error, error); 1416 goto done; 1417 } 1418 no_kqueue: 1419 1420 suword(&job->_aiocb_private.error, EINPROGRESS); 1421 aiocbe->uaiocb._aiocb_private.error = EINPROGRESS; 1422 aiocbe->userproc = p; 1423 aiocbe->cred = crhold(td->td_ucred); 1424 aiocbe->jobflags = 0; 1425 aiocbe->lio = lj; 1426 ki = p->p_aioinfo; 1427 1428 if (fp->f_type == DTYPE_SOCKET) { 1429 /* 1430 * Alternate queueing for socket ops: Reach down into the 1431 * descriptor to get the socket data. Then check to see if the 1432 * socket is ready to be read or written (based on the requested 1433 * operation). 1434 * 1435 * If it is not ready for io, then queue the aiocbe on the 1436 * socket, and set the flags so we get a call when sbnotify() 1437 * happens. 1438 */ 1439 so = fp->f_data; 1440 s = splnet(); 1441 if (((opcode == LIO_READ) && (!soreadable(so))) || ((opcode == 1442 LIO_WRITE) && (!sowriteable(so)))) { 1443 TAILQ_INSERT_TAIL(&so->so_aiojobq, aiocbe, list); 1444 TAILQ_INSERT_TAIL(&ki->kaio_sockqueue, aiocbe, plist); 1445 if (opcode == LIO_READ) 1446 so->so_rcv.sb_flags |= SB_AIO; 1447 else 1448 so->so_snd.sb_flags |= SB_AIO; 1449 aiocbe->jobstate = JOBST_JOBQGLOBAL; /* XXX */ 1450 ki->kaio_queue_count++; 1451 num_queue_count++; 1452 splx(s); 1453 error = 0; 1454 goto done; 1455 } 1456 splx(s); 1457 } 1458 1459 if ((error = aio_qphysio(p, aiocbe)) == 0) 1460 goto done; 1461 if (error > 0) { 1462 suword(&job->_aiocb_private.status, 0); 1463 aiocbe->uaiocb._aiocb_private.error = error; 1464 suword(&job->_aiocb_private.error, error); 1465 goto done; 1466 } 1467 1468 /* No buffer for daemon I/O. */ 1469 aiocbe->bp = NULL; 1470 1471 ki->kaio_queue_count++; 1472 if (lj) 1473 lj->lioj_queue_count++; 1474 s = splnet(); 1475 TAILQ_INSERT_TAIL(&ki->kaio_jobqueue, aiocbe, plist); 1476 TAILQ_INSERT_TAIL(&aio_jobs, aiocbe, list); 1477 splx(s); 1478 aiocbe->jobstate = JOBST_JOBQGLOBAL; 1479 1480 num_queue_count++; 1481 error = 0; 1482 1483 /* 1484 * If we don't have a free AIO process, and we are below our quota, then 1485 * start one. Otherwise, depend on the subsequent I/O completions to 1486 * pick-up this job. If we don't sucessfully create the new process 1487 * (thread) due to resource issues, we return an error for now (EAGAIN), 1488 * which is likely not the correct thing to do. 1489 */ 1490 s = splnet(); 1491 retryproc: 1492 if ((aiop = TAILQ_FIRST(&aio_freeproc)) != NULL) { 1493 TAILQ_REMOVE(&aio_freeproc, aiop, list); 1494 TAILQ_INSERT_TAIL(&aio_activeproc, aiop, list); 1495 aiop->aiothreadflags &= ~AIOP_FREE; 1496 wakeup(aiop->aiothread); 1497 } else if (((num_aio_resv_start + num_aio_procs) < max_aio_procs) && 1498 ((ki->kaio_active_count + num_aio_resv_start) < 1499 ki->kaio_maxactive_count)) { 1500 num_aio_resv_start++; 1501 if ((error = aio_newproc()) == 0) { 1502 num_aio_resv_start--; 1503 goto retryproc; 1504 } 1505 num_aio_resv_start--; 1506 } 1507 splx(s); 1508 done: 1509 return (error); 1510 } 1511 1512 /* 1513 * This routine queues an AIO request, checking for quotas. 1514 */ 1515 static int 1516 aio_aqueue(struct thread *td, struct aiocb *job, int type) 1517 { 1518 struct proc *p = td->td_proc; 1519 struct kaioinfo *ki; 1520 1521 if (p->p_aioinfo == NULL) 1522 aio_init_aioinfo(p); 1523 1524 if (num_queue_count >= max_queue_count) 1525 return (EAGAIN); 1526 1527 ki = p->p_aioinfo; 1528 if (ki->kaio_queue_count >= ki->kaio_qallowed_count) 1529 return (EAGAIN); 1530 1531 return _aio_aqueue(td, job, NULL, type); 1532 } 1533 1534 /* 1535 * Support the aio_return system call, as a side-effect, kernel resources are 1536 * released. 1537 */ 1538 int 1539 aio_return(struct thread *td, struct aio_return_args *uap) 1540 { 1541 struct proc *p = td->td_proc; 1542 int s; 1543 long jobref; 1544 struct aiocblist *cb, *ncb; 1545 struct aiocb *ujob; 1546 struct kaioinfo *ki; 1547 1548 ujob = uap->aiocbp; 1549 jobref = fuword(&ujob->_aiocb_private.kernelinfo); 1550 if (jobref == -1 || jobref == 0) 1551 return (EINVAL); 1552 1553 ki = p->p_aioinfo; 1554 if (ki == NULL) 1555 return (EINVAL); 1556 TAILQ_FOREACH(cb, &ki->kaio_jobdone, plist) { 1557 if (((intptr_t) cb->uaiocb._aiocb_private.kernelinfo) == 1558 jobref) { 1559 if (cb->uaiocb.aio_lio_opcode == LIO_WRITE) { 1560 p->p_stats->p_ru.ru_oublock += 1561 cb->outputcharge; 1562 cb->outputcharge = 0; 1563 } else if (cb->uaiocb.aio_lio_opcode == LIO_READ) { 1564 p->p_stats->p_ru.ru_inblock += cb->inputcharge; 1565 cb->inputcharge = 0; 1566 } 1567 goto done; 1568 } 1569 } 1570 s = splbio(); 1571 for (cb = TAILQ_FIRST(&ki->kaio_bufdone); cb; cb = ncb) { 1572 ncb = TAILQ_NEXT(cb, plist); 1573 if (((intptr_t) cb->uaiocb._aiocb_private.kernelinfo) 1574 == jobref) { 1575 break; 1576 } 1577 } 1578 splx(s); 1579 done: 1580 if (cb != NULL) { 1581 if (ujob == cb->uuaiocb) { 1582 td->td_retval[0] = 1583 cb->uaiocb._aiocb_private.status; 1584 } else 1585 td->td_retval[0] = EFAULT; 1586 aio_free_entry(cb); 1587 return (0); 1588 } 1589 return (EINVAL); 1590 } 1591 1592 /* 1593 * Allow a process to wakeup when any of the I/O requests are completed. 1594 */ 1595 int 1596 aio_suspend(struct thread *td, struct aio_suspend_args *uap) 1597 { 1598 struct proc *p = td->td_proc; 1599 struct timeval atv; 1600 struct timespec ts; 1601 struct aiocb *const *cbptr, *cbp; 1602 struct kaioinfo *ki; 1603 struct aiocblist *cb; 1604 int i; 1605 int njoblist; 1606 int error, s, timo; 1607 long *ijoblist; 1608 struct aiocb **ujoblist; 1609 1610 if (uap->nent < 0 || uap->nent > AIO_LISTIO_MAX) 1611 return (EINVAL); 1612 1613 timo = 0; 1614 if (uap->timeout) { 1615 /* Get timespec struct. */ 1616 if ((error = copyin(uap->timeout, &ts, sizeof(ts))) != 0) 1617 return (error); 1618 1619 if (ts.tv_nsec < 0 || ts.tv_nsec >= 1000000000) 1620 return (EINVAL); 1621 1622 TIMESPEC_TO_TIMEVAL(&atv, &ts); 1623 if (itimerfix(&atv)) 1624 return (EINVAL); 1625 timo = tvtohz(&atv); 1626 } 1627 1628 ki = p->p_aioinfo; 1629 if (ki == NULL) 1630 return (EAGAIN); 1631 1632 njoblist = 0; 1633 ijoblist = uma_zalloc(aiol_zone, M_WAITOK); 1634 ujoblist = uma_zalloc(aiol_zone, M_WAITOK); 1635 cbptr = uap->aiocbp; 1636 1637 for (i = 0; i < uap->nent; i++) { 1638 cbp = (struct aiocb *)(intptr_t)fuword(&cbptr[i]); 1639 if (cbp == 0) 1640 continue; 1641 ujoblist[njoblist] = cbp; 1642 ijoblist[njoblist] = fuword(&cbp->_aiocb_private.kernelinfo); 1643 njoblist++; 1644 } 1645 1646 if (njoblist == 0) { 1647 uma_zfree(aiol_zone, ijoblist); 1648 uma_zfree(aiol_zone, ujoblist); 1649 return (0); 1650 } 1651 1652 error = 0; 1653 for (;;) { 1654 TAILQ_FOREACH(cb, &ki->kaio_jobdone, plist) { 1655 for (i = 0; i < njoblist; i++) { 1656 if (((intptr_t) 1657 cb->uaiocb._aiocb_private.kernelinfo) == 1658 ijoblist[i]) { 1659 if (ujoblist[i] != cb->uuaiocb) 1660 error = EINVAL; 1661 uma_zfree(aiol_zone, ijoblist); 1662 uma_zfree(aiol_zone, ujoblist); 1663 return (error); 1664 } 1665 } 1666 } 1667 1668 s = splbio(); 1669 for (cb = TAILQ_FIRST(&ki->kaio_bufdone); cb; cb = 1670 TAILQ_NEXT(cb, plist)) { 1671 for (i = 0; i < njoblist; i++) { 1672 if (((intptr_t) 1673 cb->uaiocb._aiocb_private.kernelinfo) == 1674 ijoblist[i]) { 1675 splx(s); 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 ki->kaio_flags |= KAIO_WAKEUP; 1686 error = tsleep(p, PRIBIO | PCATCH, "aiospn", timo); 1687 splx(s); 1688 1689 if (error == ERESTART || error == EINTR) { 1690 uma_zfree(aiol_zone, ijoblist); 1691 uma_zfree(aiol_zone, ujoblist); 1692 return (EINTR); 1693 } else if (error == EWOULDBLOCK) { 1694 uma_zfree(aiol_zone, ijoblist); 1695 uma_zfree(aiol_zone, ujoblist); 1696 return (EAGAIN); 1697 } 1698 } 1699 1700 /* NOTREACHED */ 1701 return (EINVAL); 1702 } 1703 1704 /* 1705 * aio_cancel cancels any non-physio aio operations not currently in 1706 * progress. 1707 */ 1708 int 1709 aio_cancel(struct thread *td, struct aio_cancel_args *uap) 1710 { 1711 struct proc *p = td->td_proc; 1712 struct kaioinfo *ki; 1713 struct aiocblist *cbe, *cbn; 1714 struct file *fp; 1715 struct filedesc *fdp; 1716 struct socket *so; 1717 struct proc *po; 1718 int s,error; 1719 int cancelled=0; 1720 int notcancelled=0; 1721 struct vnode *vp; 1722 1723 fdp = p->p_fd; 1724 if ((u_int)uap->fd >= fdp->fd_nfiles || 1725 (fp = fdp->fd_ofiles[uap->fd]) == NULL) 1726 return (EBADF); 1727 1728 if (fp->f_type == DTYPE_VNODE) { 1729 vp = fp->f_vnode; 1730 1731 if (vn_isdisk(vp,&error)) { 1732 td->td_retval[0] = AIO_NOTCANCELED; 1733 return (0); 1734 } 1735 } else if (fp->f_type == DTYPE_SOCKET) { 1736 so = fp->f_data; 1737 1738 s = splnet(); 1739 1740 for (cbe = TAILQ_FIRST(&so->so_aiojobq); cbe; cbe = cbn) { 1741 cbn = TAILQ_NEXT(cbe, list); 1742 if ((uap->aiocbp == NULL) || 1743 (uap->aiocbp == cbe->uuaiocb) ) { 1744 po = cbe->userproc; 1745 ki = po->p_aioinfo; 1746 TAILQ_REMOVE(&so->so_aiojobq, cbe, list); 1747 TAILQ_REMOVE(&ki->kaio_sockqueue, cbe, plist); 1748 TAILQ_INSERT_TAIL(&ki->kaio_jobdone, cbe, plist); 1749 if (ki->kaio_flags & KAIO_WAKEUP) { 1750 wakeup(po); 1751 } 1752 cbe->jobstate = JOBST_JOBFINISHED; 1753 cbe->uaiocb._aiocb_private.status=-1; 1754 cbe->uaiocb._aiocb_private.error=ECANCELED; 1755 cancelled++; 1756 /* XXX cancelled, knote? */ 1757 if (cbe->uaiocb.aio_sigevent.sigev_notify == 1758 SIGEV_SIGNAL) { 1759 PROC_LOCK(cbe->userproc); 1760 psignal(cbe->userproc, cbe->uaiocb.aio_sigevent.sigev_signo); 1761 PROC_UNLOCK(cbe->userproc); 1762 } 1763 if (uap->aiocbp) 1764 break; 1765 } 1766 } 1767 splx(s); 1768 1769 if ((cancelled) && (uap->aiocbp)) { 1770 td->td_retval[0] = AIO_CANCELED; 1771 return (0); 1772 } 1773 } 1774 ki=p->p_aioinfo; 1775 if (ki == NULL) 1776 goto done; 1777 s = splnet(); 1778 1779 for (cbe = TAILQ_FIRST(&ki->kaio_jobqueue); cbe; cbe = cbn) { 1780 cbn = TAILQ_NEXT(cbe, plist); 1781 1782 if ((uap->fd == cbe->uaiocb.aio_fildes) && 1783 ((uap->aiocbp == NULL ) || 1784 (uap->aiocbp == cbe->uuaiocb))) { 1785 1786 if (cbe->jobstate == JOBST_JOBQGLOBAL) { 1787 TAILQ_REMOVE(&aio_jobs, cbe, list); 1788 TAILQ_REMOVE(&ki->kaio_jobqueue, cbe, plist); 1789 TAILQ_INSERT_TAIL(&ki->kaio_jobdone, cbe, 1790 plist); 1791 cancelled++; 1792 ki->kaio_queue_finished_count++; 1793 cbe->jobstate = JOBST_JOBFINISHED; 1794 cbe->uaiocb._aiocb_private.status = -1; 1795 cbe->uaiocb._aiocb_private.error = ECANCELED; 1796 /* XXX cancelled, knote? */ 1797 if (cbe->uaiocb.aio_sigevent.sigev_notify == 1798 SIGEV_SIGNAL) { 1799 PROC_LOCK(cbe->userproc); 1800 psignal(cbe->userproc, cbe->uaiocb.aio_sigevent.sigev_signo); 1801 PROC_UNLOCK(cbe->userproc); 1802 } 1803 } else { 1804 notcancelled++; 1805 } 1806 } 1807 } 1808 splx(s); 1809 done: 1810 if (notcancelled) { 1811 td->td_retval[0] = AIO_NOTCANCELED; 1812 return (0); 1813 } 1814 if (cancelled) { 1815 td->td_retval[0] = AIO_CANCELED; 1816 return (0); 1817 } 1818 td->td_retval[0] = AIO_ALLDONE; 1819 1820 return (0); 1821 } 1822 1823 /* 1824 * aio_error is implemented in the kernel level for compatibility purposes only. 1825 * For a user mode async implementation, it would be best to do it in a userland 1826 * subroutine. 1827 */ 1828 int 1829 aio_error(struct thread *td, struct aio_error_args *uap) 1830 { 1831 struct proc *p = td->td_proc; 1832 int s; 1833 struct aiocblist *cb; 1834 struct kaioinfo *ki; 1835 long jobref; 1836 1837 ki = p->p_aioinfo; 1838 if (ki == NULL) 1839 return (EINVAL); 1840 1841 jobref = fuword(&uap->aiocbp->_aiocb_private.kernelinfo); 1842 if ((jobref == -1) || (jobref == 0)) 1843 return (EINVAL); 1844 1845 TAILQ_FOREACH(cb, &ki->kaio_jobdone, plist) { 1846 if (((intptr_t)cb->uaiocb._aiocb_private.kernelinfo) == 1847 jobref) { 1848 td->td_retval[0] = cb->uaiocb._aiocb_private.error; 1849 return (0); 1850 } 1851 } 1852 1853 s = splnet(); 1854 1855 for (cb = TAILQ_FIRST(&ki->kaio_jobqueue); cb; cb = TAILQ_NEXT(cb, 1856 plist)) { 1857 if (((intptr_t)cb->uaiocb._aiocb_private.kernelinfo) == 1858 jobref) { 1859 td->td_retval[0] = EINPROGRESS; 1860 splx(s); 1861 return (0); 1862 } 1863 } 1864 1865 for (cb = TAILQ_FIRST(&ki->kaio_sockqueue); cb; cb = TAILQ_NEXT(cb, 1866 plist)) { 1867 if (((intptr_t)cb->uaiocb._aiocb_private.kernelinfo) == 1868 jobref) { 1869 td->td_retval[0] = EINPROGRESS; 1870 splx(s); 1871 return (0); 1872 } 1873 } 1874 splx(s); 1875 1876 s = splbio(); 1877 for (cb = TAILQ_FIRST(&ki->kaio_bufdone); cb; cb = TAILQ_NEXT(cb, 1878 plist)) { 1879 if (((intptr_t)cb->uaiocb._aiocb_private.kernelinfo) == 1880 jobref) { 1881 td->td_retval[0] = cb->uaiocb._aiocb_private.error; 1882 splx(s); 1883 return (0); 1884 } 1885 } 1886 1887 for (cb = TAILQ_FIRST(&ki->kaio_bufqueue); cb; cb = TAILQ_NEXT(cb, 1888 plist)) { 1889 if (((intptr_t)cb->uaiocb._aiocb_private.kernelinfo) == 1890 jobref) { 1891 td->td_retval[0] = EINPROGRESS; 1892 splx(s); 1893 return (0); 1894 } 1895 } 1896 splx(s); 1897 1898 #if (0) 1899 /* 1900 * Hack for lio. 1901 */ 1902 status = fuword(&uap->aiocbp->_aiocb_private.status); 1903 if (status == -1) 1904 return fuword(&uap->aiocbp->_aiocb_private.error); 1905 #endif 1906 return (EINVAL); 1907 } 1908 1909 /* syscall - asynchronous read from a file (REALTIME) */ 1910 int 1911 aio_read(struct thread *td, struct aio_read_args *uap) 1912 { 1913 1914 return aio_aqueue(td, uap->aiocbp, LIO_READ); 1915 } 1916 1917 /* syscall - asynchronous write to a file (REALTIME) */ 1918 int 1919 aio_write(struct thread *td, struct aio_write_args *uap) 1920 { 1921 1922 return aio_aqueue(td, uap->aiocbp, LIO_WRITE); 1923 } 1924 1925 /* syscall - list directed I/O (REALTIME) */ 1926 int 1927 lio_listio(struct thread *td, struct lio_listio_args *uap) 1928 { 1929 struct proc *p = td->td_proc; 1930 int nent, nentqueued; 1931 struct aiocb *iocb, * const *cbptr; 1932 struct aiocblist *cb; 1933 struct kaioinfo *ki; 1934 struct aio_liojob *lj; 1935 int error, runningcode; 1936 int nerror; 1937 int i; 1938 int s; 1939 1940 if ((uap->mode != LIO_NOWAIT) && (uap->mode != LIO_WAIT)) 1941 return (EINVAL); 1942 1943 nent = uap->nent; 1944 if (nent < 0 || nent > AIO_LISTIO_MAX) 1945 return (EINVAL); 1946 1947 if (p->p_aioinfo == NULL) 1948 aio_init_aioinfo(p); 1949 1950 if ((nent + num_queue_count) > max_queue_count) 1951 return (EAGAIN); 1952 1953 ki = p->p_aioinfo; 1954 if ((nent + ki->kaio_queue_count) > ki->kaio_qallowed_count) 1955 return (EAGAIN); 1956 1957 lj = uma_zalloc(aiolio_zone, M_WAITOK); 1958 if (!lj) 1959 return (EAGAIN); 1960 1961 lj->lioj_flags = 0; 1962 lj->lioj_buffer_count = 0; 1963 lj->lioj_buffer_finished_count = 0; 1964 lj->lioj_queue_count = 0; 1965 lj->lioj_queue_finished_count = 0; 1966 lj->lioj_ki = ki; 1967 1968 /* 1969 * Setup signal. 1970 */ 1971 if (uap->sig && (uap->mode == LIO_NOWAIT)) { 1972 error = copyin(uap->sig, &lj->lioj_signal, 1973 sizeof(lj->lioj_signal)); 1974 if (error) { 1975 uma_zfree(aiolio_zone, lj); 1976 return (error); 1977 } 1978 if (!_SIG_VALID(lj->lioj_signal.sigev_signo)) { 1979 uma_zfree(aiolio_zone, lj); 1980 return (EINVAL); 1981 } 1982 lj->lioj_flags |= LIOJ_SIGNAL; 1983 } 1984 TAILQ_INSERT_TAIL(&ki->kaio_liojoblist, lj, lioj_list); 1985 /* 1986 * Get pointers to the list of I/O requests. 1987 */ 1988 nerror = 0; 1989 nentqueued = 0; 1990 cbptr = uap->acb_list; 1991 for (i = 0; i < uap->nent; i++) { 1992 iocb = (struct aiocb *)(intptr_t)fuword(&cbptr[i]); 1993 if (((intptr_t)iocb != -1) && ((intptr_t)iocb != 0)) { 1994 error = _aio_aqueue(td, iocb, lj, 0); 1995 if (error == 0) 1996 nentqueued++; 1997 else 1998 nerror++; 1999 } 2000 } 2001 2002 /* 2003 * If we haven't queued any, then just return error. 2004 */ 2005 if (nentqueued == 0) 2006 return (0); 2007 2008 /* 2009 * Calculate the appropriate error return. 2010 */ 2011 runningcode = 0; 2012 if (nerror) 2013 runningcode = EIO; 2014 2015 if (uap->mode == LIO_WAIT) { 2016 int command, found, jobref; 2017 2018 for (;;) { 2019 found = 0; 2020 for (i = 0; i < uap->nent; i++) { 2021 /* 2022 * Fetch address of the control buf pointer in 2023 * user space. 2024 */ 2025 iocb = (struct aiocb *) 2026 (intptr_t)fuword(&cbptr[i]); 2027 if (((intptr_t)iocb == -1) || ((intptr_t)iocb 2028 == 0)) 2029 continue; 2030 2031 /* 2032 * Fetch the associated command from user space. 2033 */ 2034 command = fuword(&iocb->aio_lio_opcode); 2035 if (command == LIO_NOP) { 2036 found++; 2037 continue; 2038 } 2039 2040 jobref = 2041 fuword(&iocb->_aiocb_private.kernelinfo); 2042 2043 TAILQ_FOREACH(cb, &ki->kaio_jobdone, plist) { 2044 if (((intptr_t)cb->uaiocb._aiocb_private.kernelinfo) 2045 == jobref) { 2046 if (cb->uaiocb.aio_lio_opcode 2047 == LIO_WRITE) { 2048 p->p_stats->p_ru.ru_oublock 2049 += 2050 cb->outputcharge; 2051 cb->outputcharge = 0; 2052 } else if (cb->uaiocb.aio_lio_opcode 2053 == LIO_READ) { 2054 p->p_stats->p_ru.ru_inblock 2055 += cb->inputcharge; 2056 cb->inputcharge = 0; 2057 } 2058 found++; 2059 break; 2060 } 2061 } 2062 2063 s = splbio(); 2064 TAILQ_FOREACH(cb, &ki->kaio_bufdone, plist) { 2065 if (((intptr_t)cb->uaiocb._aiocb_private.kernelinfo) 2066 == jobref) { 2067 found++; 2068 break; 2069 } 2070 } 2071 splx(s); 2072 } 2073 2074 /* 2075 * If all I/Os have been disposed of, then we can 2076 * return. 2077 */ 2078 if (found == nentqueued) 2079 return (runningcode); 2080 2081 ki->kaio_flags |= KAIO_WAKEUP; 2082 error = tsleep(p, PRIBIO | PCATCH, "aiospn", 0); 2083 2084 if (error == EINTR) 2085 return (EINTR); 2086 else if (error == EWOULDBLOCK) 2087 return (EAGAIN); 2088 } 2089 } 2090 2091 return (runningcode); 2092 } 2093 2094 /* 2095 * This is a weird hack so that we can post a signal. It is safe to do so from 2096 * a timeout routine, but *not* from an interrupt routine. 2097 */ 2098 static void 2099 process_signal(void *aioj) 2100 { 2101 struct aiocblist *aiocbe = aioj; 2102 struct aio_liojob *lj = aiocbe->lio; 2103 struct aiocb *cb = &aiocbe->uaiocb; 2104 2105 if ((lj) && (lj->lioj_signal.sigev_notify == SIGEV_SIGNAL) && 2106 (lj->lioj_queue_count == lj->lioj_queue_finished_count)) { 2107 PROC_LOCK(lj->lioj_ki->kaio_p); 2108 psignal(lj->lioj_ki->kaio_p, lj->lioj_signal.sigev_signo); 2109 PROC_UNLOCK(lj->lioj_ki->kaio_p); 2110 lj->lioj_flags |= LIOJ_SIGNAL_POSTED; 2111 } 2112 2113 if (cb->aio_sigevent.sigev_notify == SIGEV_SIGNAL) { 2114 PROC_LOCK(aiocbe->userproc); 2115 psignal(aiocbe->userproc, cb->aio_sigevent.sigev_signo); 2116 PROC_UNLOCK(aiocbe->userproc); 2117 } 2118 } 2119 2120 /* 2121 * Interrupt handler for physio, performs the necessary process wakeups, and 2122 * signals. 2123 */ 2124 static void 2125 aio_physwakeup(struct buf *bp) 2126 { 2127 struct aiocblist *aiocbe; 2128 struct proc *p; 2129 struct kaioinfo *ki; 2130 struct aio_liojob *lj; 2131 2132 wakeup(bp); 2133 2134 aiocbe = (struct aiocblist *)bp->b_caller1; 2135 if (aiocbe) { 2136 p = aiocbe->userproc; 2137 2138 aiocbe->jobstate = JOBST_JOBBFINISHED; 2139 aiocbe->uaiocb._aiocb_private.status -= bp->b_resid; 2140 aiocbe->uaiocb._aiocb_private.error = 0; 2141 aiocbe->jobflags |= AIOCBLIST_DONE; 2142 2143 if (bp->b_ioflags & BIO_ERROR) 2144 aiocbe->uaiocb._aiocb_private.error = bp->b_error; 2145 2146 lj = aiocbe->lio; 2147 if (lj) { 2148 lj->lioj_buffer_finished_count++; 2149 2150 /* 2151 * wakeup/signal if all of the interrupt jobs are done. 2152 */ 2153 if (lj->lioj_buffer_finished_count == 2154 lj->lioj_buffer_count) { 2155 /* 2156 * Post a signal if it is called for. 2157 */ 2158 if ((lj->lioj_flags & 2159 (LIOJ_SIGNAL|LIOJ_SIGNAL_POSTED)) == 2160 LIOJ_SIGNAL) { 2161 lj->lioj_flags |= LIOJ_SIGNAL_POSTED; 2162 aiocbe->timeouthandle = 2163 timeout(process_signal, 2164 aiocbe, 0); 2165 } 2166 } 2167 } 2168 2169 ki = p->p_aioinfo; 2170 if (ki) { 2171 ki->kaio_buffer_finished_count++; 2172 TAILQ_REMOVE(&aio_bufjobs, aiocbe, list); 2173 TAILQ_REMOVE(&ki->kaio_bufqueue, aiocbe, plist); 2174 TAILQ_INSERT_TAIL(&ki->kaio_bufdone, aiocbe, plist); 2175 2176 KNOTE(&aiocbe->klist, 0); 2177 /* Do the wakeup. */ 2178 if (ki->kaio_flags & (KAIO_RUNDOWN|KAIO_WAKEUP)) { 2179 ki->kaio_flags &= ~KAIO_WAKEUP; 2180 wakeup(p); 2181 } 2182 } 2183 2184 if (aiocbe->uaiocb.aio_sigevent.sigev_notify == SIGEV_SIGNAL) 2185 aiocbe->timeouthandle = 2186 timeout(process_signal, aiocbe, 0); 2187 } 2188 } 2189 2190 /* syscall - wait for the next completion of an aio request */ 2191 int 2192 aio_waitcomplete(struct thread *td, struct aio_waitcomplete_args *uap) 2193 { 2194 struct proc *p = td->td_proc; 2195 struct timeval atv; 2196 struct timespec ts; 2197 struct kaioinfo *ki; 2198 struct aiocblist *cb = NULL; 2199 int error, s, timo; 2200 2201 suword(uap->aiocbp, (int)NULL); 2202 2203 timo = 0; 2204 if (uap->timeout) { 2205 /* Get timespec struct. */ 2206 error = copyin(uap->timeout, &ts, sizeof(ts)); 2207 if (error) 2208 return (error); 2209 2210 if ((ts.tv_nsec < 0) || (ts.tv_nsec >= 1000000000)) 2211 return (EINVAL); 2212 2213 TIMESPEC_TO_TIMEVAL(&atv, &ts); 2214 if (itimerfix(&atv)) 2215 return (EINVAL); 2216 timo = tvtohz(&atv); 2217 } 2218 2219 ki = p->p_aioinfo; 2220 if (ki == NULL) 2221 return (EAGAIN); 2222 2223 for (;;) { 2224 if ((cb = TAILQ_FIRST(&ki->kaio_jobdone)) != 0) { 2225 suword(uap->aiocbp, (uintptr_t)cb->uuaiocb); 2226 td->td_retval[0] = cb->uaiocb._aiocb_private.status; 2227 if (cb->uaiocb.aio_lio_opcode == LIO_WRITE) { 2228 p->p_stats->p_ru.ru_oublock += 2229 cb->outputcharge; 2230 cb->outputcharge = 0; 2231 } else if (cb->uaiocb.aio_lio_opcode == LIO_READ) { 2232 p->p_stats->p_ru.ru_inblock += cb->inputcharge; 2233 cb->inputcharge = 0; 2234 } 2235 aio_free_entry(cb); 2236 return (cb->uaiocb._aiocb_private.error); 2237 } 2238 2239 s = splbio(); 2240 if ((cb = TAILQ_FIRST(&ki->kaio_bufdone)) != 0 ) { 2241 splx(s); 2242 suword(uap->aiocbp, (uintptr_t)cb->uuaiocb); 2243 td->td_retval[0] = cb->uaiocb._aiocb_private.status; 2244 aio_free_entry(cb); 2245 return (cb->uaiocb._aiocb_private.error); 2246 } 2247 2248 ki->kaio_flags |= KAIO_WAKEUP; 2249 error = tsleep(p, PRIBIO | PCATCH, "aiowc", timo); 2250 splx(s); 2251 2252 if (error == ERESTART) 2253 return (EINTR); 2254 else if (error < 0) 2255 return (error); 2256 else if (error == EINTR) 2257 return (EINTR); 2258 else if (error == EWOULDBLOCK) 2259 return (EAGAIN); 2260 } 2261 } 2262 2263 /* kqueue attach function */ 2264 static int 2265 filt_aioattach(struct knote *kn) 2266 { 2267 struct aiocblist *aiocbe = (struct aiocblist *)kn->kn_sdata; 2268 2269 /* 2270 * The aiocbe pointer must be validated before using it, so 2271 * registration is restricted to the kernel; the user cannot 2272 * set EV_FLAG1. 2273 */ 2274 if ((kn->kn_flags & EV_FLAG1) == 0) 2275 return (EPERM); 2276 kn->kn_flags &= ~EV_FLAG1; 2277 2278 SLIST_INSERT_HEAD(&aiocbe->klist, kn, kn_selnext); 2279 2280 return (0); 2281 } 2282 2283 /* kqueue detach function */ 2284 static void 2285 filt_aiodetach(struct knote *kn) 2286 { 2287 struct aiocblist *aiocbe = (struct aiocblist *)kn->kn_sdata; 2288 2289 SLIST_REMOVE(&aiocbe->klist, kn, knote, kn_selnext); 2290 } 2291 2292 /* kqueue filter function */ 2293 /*ARGSUSED*/ 2294 static int 2295 filt_aio(struct knote *kn, long hint) 2296 { 2297 struct aiocblist *aiocbe = (struct aiocblist *)kn->kn_sdata; 2298 2299 kn->kn_data = aiocbe->uaiocb._aiocb_private.error; 2300 if (aiocbe->jobstate != JOBST_JOBFINISHED && 2301 aiocbe->jobstate != JOBST_JOBBFINISHED) 2302 return (0); 2303 kn->kn_flags |= EV_EOF; 2304 return (1); 2305 } 2306