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