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