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