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