1 /* $NetBSD: vfs_bio.c,v 1.34 1994/11/22 01:31:02 mycroft Exp $ */ 2 3 /*- 4 * Copyright (c) 1994 Christopher G. Demetriou 5 * Copyright (c) 1982, 1986, 1989, 1993 6 * The Regents of the University of California. All rights reserved. 7 * (c) UNIX System Laboratories, Inc. 8 * All or some portions of this file are derived from material licensed 9 * to the University of California by American Telephone and Telegraph 10 * Co. or Unix System Laboratories, Inc. and are reproduced herein with 11 * the permission of UNIX System Laboratories, Inc. 12 * 13 * Redistribution and use in source and binary forms, with or without 14 * modification, are permitted provided that the following conditions 15 * are met: 16 * 1. Redistributions of source code must retain the above copyright 17 * notice, this list of conditions and the following disclaimer. 18 * 2. Redistributions in binary form must reproduce the above copyright 19 * notice, this list of conditions and the following disclaimer in the 20 * documentation and/or other materials provided with the distribution. 21 * 3. All advertising materials mentioning features or use of this software 22 * must display the following acknowledgement: 23 * This product includes software developed by the University of 24 * California, Berkeley and its contributors. 25 * 4. Neither the name of the University nor the names of its contributors 26 * may be used to endorse or promote products derived from this software 27 * without specific prior written permission. 28 * 29 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND 30 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 31 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 32 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE 33 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 34 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS 35 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 36 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT 37 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY 38 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 39 * SUCH DAMAGE. 40 * 41 * @(#)vfs_bio.c 8.6 (Berkeley) 1/11/94 42 */ 43 44 /* 45 * Some references: 46 * Bach: The Design of the UNIX Operating System (Prentice Hall, 1986) 47 * Leffler, et al.: The Design and Implementation of the 4.3BSD 48 * UNIX Operating System (Addison Welley, 1989) 49 */ 50 51 #include <sys/param.h> 52 #include <sys/systm.h> 53 #include <sys/proc.h> 54 #include <sys/buf.h> 55 #include <sys/vnode.h> 56 #include <sys/mount.h> 57 #include <sys/trace.h> 58 #include <sys/malloc.h> 59 #include <sys/resourcevar.h> 60 61 /* Macros to clear/set/test flags. */ 62 #define SET(t, f) (t) |= (f) 63 #define CLR(t, f) (t) &= ~(f) 64 #define ISSET(t, f) ((t) & (f)) 65 66 /* 67 * Definitions for the buffer hash lists. 68 */ 69 #define BUFHASH(dvp, lbn) \ 70 (&bufhashtbl[((long)(dvp) / sizeof(*(dvp)) + (int)(lbn)) & bufhash]) 71 LIST_HEAD(bufhashhdr, buf) *bufhashtbl, invalhash; 72 u_long bufhash; 73 74 /* 75 * Insq/Remq for the buffer hash lists. 76 */ 77 #define binshash(bp, dp) LIST_INSERT_HEAD(dp, bp, b_hash) 78 #define bremhash(bp) LIST_REMOVE(bp, b_hash) 79 80 /* 81 * Definitions for the buffer free lists. 82 */ 83 #define BQUEUES 4 /* number of free buffer queues */ 84 85 #define BQ_LOCKED 0 /* super-blocks &c */ 86 #define BQ_LRU 1 /* lru, useful buffers */ 87 #define BQ_AGE 2 /* rubbish */ 88 #define BQ_EMPTY 3 /* buffer headers with no memory */ 89 90 TAILQ_HEAD(bqueues, buf) bufqueues[BQUEUES]; 91 int needbuffer; 92 93 /* 94 * Insq/Remq for the buffer free lists. 95 */ 96 #define binsheadfree(bp, dp) TAILQ_INSERT_HEAD(dp, bp, b_freelist) 97 #define binstailfree(bp, dp) TAILQ_INSERT_TAIL(dp, bp, b_freelist) 98 99 void 100 bremfree(bp) 101 struct buf *bp; 102 { 103 struct bqueues *dp = NULL; 104 105 /* 106 * We only calculate the head of the freelist when removing 107 * the last element of the list as that is the only time that 108 * it is needed (e.g. to reset the tail pointer). 109 * 110 * NB: This makes an assumption about how tailq's are implemented. 111 */ 112 if (bp->b_freelist.tqe_next == NULL) { 113 for (dp = bufqueues; dp < &bufqueues[BQUEUES]; dp++) 114 if (dp->tqh_last == &bp->b_freelist.tqe_next) 115 break; 116 if (dp == &bufqueues[BQUEUES]) 117 panic("bremfree: lost tail"); 118 } 119 TAILQ_REMOVE(dp, bp, b_freelist); 120 } 121 122 /* 123 * Initialize buffers and hash links for buffers. 124 */ 125 void 126 bufinit() 127 { 128 register struct buf *bp; 129 struct bqueues *dp; 130 register int i; 131 int base, residual; 132 133 for (dp = bufqueues; dp < &bufqueues[BQUEUES]; dp++) 134 TAILQ_INIT(dp); 135 bufhashtbl = hashinit(nbuf, M_CACHE, &bufhash); 136 base = bufpages / nbuf; 137 residual = bufpages % nbuf; 138 for (i = 0; i < nbuf; i++) { 139 bp = &buf[i]; 140 bzero((char *)bp, sizeof *bp); 141 bp->b_dev = NODEV; 142 bp->b_rcred = NOCRED; 143 bp->b_wcred = NOCRED; 144 bp->b_vnbufs.le_next = NOLIST; 145 bp->b_data = buffers + i * MAXBSIZE; 146 if (i < residual) 147 bp->b_bufsize = (base + 1) * CLBYTES; 148 else 149 bp->b_bufsize = base * CLBYTES; 150 bp->b_flags = B_INVAL; 151 dp = bp->b_bufsize ? &bufqueues[BQ_AGE] : &bufqueues[BQ_EMPTY]; 152 binsheadfree(bp, dp); 153 binshash(bp, &invalhash); 154 } 155 } 156 157 __inline struct buf * 158 bio_doread(vp, blkno, size, cred, async) 159 struct vnode *vp; 160 daddr_t blkno; 161 int size; 162 struct ucred *cred; 163 int async; 164 { 165 register struct buf *bp; 166 167 bp = getblk(vp, blkno, size, 0, 0); 168 169 /* 170 * If buffer does not have data valid, start a read. 171 * Note that if buffer is B_INVAL, getblk() won't return it. 172 * Therefore, it's valid if it's I/O has completed or been delayed. 173 */ 174 if (!ISSET(bp->b_flags, (B_DONE | B_DELWRI))) { 175 /* Start I/O for the buffer (keeping credentials). */ 176 SET(bp->b_flags, B_READ | async); 177 if (cred != NOCRED && bp->b_rcred == NOCRED) { 178 crhold(cred); 179 bp->b_rcred = cred; 180 } 181 VOP_STRATEGY(bp); 182 183 /* Pay for the read. */ 184 curproc->p_stats->p_ru.ru_inblock++; /* XXX */ 185 } else if (async) { 186 brelse(bp); 187 } 188 189 return (bp); 190 } 191 192 /* 193 * Read a disk block. 194 * This algorithm described in Bach (p.54). 195 */ 196 bread(vp, blkno, size, cred, bpp) 197 struct vnode *vp; 198 daddr_t blkno; 199 int size; 200 struct ucred *cred; 201 struct buf **bpp; 202 { 203 register struct buf *bp; 204 205 /* Get buffer for block. */ 206 bp = *bpp = bio_doread(vp, blkno, size, cred, 0); 207 208 /* Wait for the read to complete, and return result. */ 209 return (biowait(bp)); 210 } 211 212 /* 213 * Read-ahead multiple disk blocks. The first is sync, the rest async. 214 * Trivial modification to the breada algorithm presented in Bach (p.55). 215 */ 216 breadn(vp, blkno, size, rablks, rasizes, nrablks, cred, bpp) 217 struct vnode *vp; 218 daddr_t blkno; int size; 219 daddr_t rablks[]; int rasizes[]; 220 int nrablks; 221 struct ucred *cred; 222 struct buf **bpp; 223 { 224 register struct buf *bp; 225 int i; 226 227 bp = *bpp = bio_doread(vp, blkno, size, cred, 0); 228 229 /* 230 * For each of the read-ahead blocks, start a read, if necessary. 231 */ 232 for (i = 0; i < nrablks; i++) { 233 /* If it's in the cache, just go on to next one. */ 234 if (incore(vp, rablks[i])) 235 continue; 236 237 /* Get a buffer for the read-ahead block */ 238 (void) bio_doread(vp, rablks[i], rasizes[i], cred, B_ASYNC); 239 } 240 241 /* Otherwise, we had to start a read for it; wait until it's valid. */ 242 return (biowait(bp)); 243 } 244 245 /* 246 * Read with single-block read-ahead. Defined in Bach (p.55), but 247 * implemented as a call to breadn(). 248 * XXX for compatibility with old file systems. 249 */ 250 breada(vp, blkno, size, rablkno, rabsize, cred, bpp) 251 struct vnode *vp; 252 daddr_t blkno; int size; 253 daddr_t rablkno; int rabsize; 254 struct ucred *cred; 255 struct buf **bpp; 256 { 257 258 return (breadn(vp, blkno, size, &rablkno, &rabsize, 1, cred, bpp)); 259 } 260 261 /* 262 * Block write. Described in Bach (p.56) 263 */ 264 bwrite(bp) 265 struct buf *bp; 266 { 267 int rv, s, sync, wasdelayed; 268 269 /* Remember buffer type, to switch on it later. */ 270 sync = !ISSET(bp->b_flags, B_ASYNC); 271 wasdelayed = ISSET(bp->b_flags, B_DELWRI); 272 CLR(bp->b_flags, (B_READ | B_DONE | B_ERROR | B_DELWRI)); 273 274 if (!sync) { 275 /* 276 * If not synchronous, pay for the I/O operation and make 277 * sure the buf is on the correct vnode queue. We have 278 * to do this now, because if we don't, the vnode may not 279 * be properly notified that its I/O has completed. 280 */ 281 if (wasdelayed) 282 reassignbuf(bp, bp->b_vp); 283 else 284 curproc->p_stats->p_ru.ru_oublock++; 285 } 286 287 /* Initiate disk write. Make sure the appropriate party is charged. */ 288 SET(bp->b_flags, B_WRITEINPROG); 289 bp->b_vp->v_numoutput++; 290 VOP_STRATEGY(bp); 291 292 if (sync) { 293 /* 294 * If I/O was synchronous, wait for it to complete. 295 */ 296 rv = biowait(bp); 297 298 /* 299 * Pay for the I/O operation, if it's not been paid for, and 300 * make sure it's on the correct vnode queue. (async operatings 301 * were payed for above.) 302 */ 303 if (wasdelayed) 304 reassignbuf(bp, bp->b_vp); 305 else 306 curproc->p_stats->p_ru.ru_oublock++; 307 308 /* Release the buffer. */ 309 brelse(bp); 310 311 return (rv); 312 } else { 313 return (0); 314 } 315 } 316 317 int 318 vn_bwrite(ap) 319 struct vop_bwrite_args *ap; 320 { 321 322 return (bwrite(ap->a_bp)); 323 } 324 325 /* 326 * Delayed write. 327 * 328 * The buffer is marked dirty, but is not queued for I/O. 329 * This routine should be used when the buffer is expected 330 * to be modified again soon, typically a small write that 331 * partially fills a buffer. 332 * 333 * NB: magnetic tapes cannot be delayed; they must be 334 * written in the order that the writes are requested. 335 * 336 * Described in Leffler, et al. (pp. 208-213). 337 */ 338 void 339 bdwrite(bp) 340 struct buf *bp; 341 { 342 343 /* 344 * If the block hasn't been seen before: 345 * (1) Mark it as having been seen, 346 * (2) Charge for the write. 347 * (3) Make sure it's on its vnode's correct block list, 348 */ 349 if (!ISSET(bp->b_flags, B_DELWRI)) { 350 SET(bp->b_flags, B_DELWRI); 351 curproc->p_stats->p_ru.ru_oublock++; /* XXX */ 352 reassignbuf(bp, bp->b_vp); 353 } 354 355 /* If this is a tape block, write it the block now. */ 356 if (ISSET(bp->b_flags, B_TAPE)) { 357 bwrite(bp); 358 return; 359 } 360 361 /* Otherwise, the "write" is done, so mark and release the buffer. */ 362 SET(bp->b_flags, B_DONE); 363 brelse(bp); 364 } 365 366 /* 367 * Asynchronous block write; just an asynchronous bwrite(). 368 */ 369 void 370 bawrite(bp) 371 struct buf *bp; 372 { 373 374 SET(bp->b_flags, B_ASYNC); 375 VOP_BWRITE(bp); 376 } 377 378 /* 379 * Release a buffer on to the free lists. 380 * Described in Bach (p. 46). 381 */ 382 void 383 brelse(bp) 384 struct buf *bp; 385 { 386 struct bqueues *bufq; 387 int s; 388 389 /* Wake up any processes waiting for any buffer to become free. */ 390 if (needbuffer) { 391 needbuffer = 0; 392 wakeup(&needbuffer); 393 } 394 395 /* Wake up any proceeses waiting for _this_ buffer to become free. */ 396 if (ISSET(bp->b_flags, B_WANTED)) { 397 CLR(bp->b_flags, B_WANTED); 398 wakeup(bp); 399 } 400 401 /* Block disk interrupts. */ 402 s = splbio(); 403 404 /* 405 * Determine which queue the buffer should be on, then put it there. 406 */ 407 408 /* If it's locked, don't report an error; try again later. */ 409 if (ISSET(bp->b_flags, (B_LOCKED|B_ERROR)) == (B_LOCKED|B_ERROR)) 410 CLR(bp->b_flags, B_ERROR); 411 412 /* If it's not cacheable, or an error, mark it invalid. */ 413 if (ISSET(bp->b_flags, (B_NOCACHE|B_ERROR))) 414 SET(bp->b_flags, B_INVAL); 415 416 if ((bp->b_bufsize <= 0) || ISSET(bp->b_flags, B_INVAL)) { 417 /* 418 * If it's invalid or empty, dissociate it from its vnode 419 * and put on the head of the appropriate queue. 420 */ 421 if (bp->b_vp) 422 brelvp(bp); 423 CLR(bp->b_flags, B_DELWRI); 424 if (bp->b_bufsize <= 0) 425 /* no data */ 426 bufq = &bufqueues[BQ_EMPTY]; 427 else 428 /* invalid data */ 429 bufq = &bufqueues[BQ_AGE]; 430 binsheadfree(bp, bufq); 431 } else { 432 /* 433 * It has valid data. Put it on the end of the appropriate 434 * queue, so that it'll stick around for as long as possible. 435 */ 436 if (ISSET(bp->b_flags, B_LOCKED)) 437 /* locked in core */ 438 bufq = &bufqueues[BQ_LOCKED]; 439 else if (ISSET(bp->b_flags, B_AGE)) 440 /* stale but valid data */ 441 bufq = &bufqueues[BQ_AGE]; 442 else 443 /* valid data */ 444 bufq = &bufqueues[BQ_LRU]; 445 binstailfree(bp, bufq); 446 } 447 448 /* Unlock the buffer. */ 449 CLR(bp->b_flags, (B_AGE | B_ASYNC | B_BUSY | B_NOCACHE)); 450 451 /* Allow disk interrupts. */ 452 splx(s); 453 } 454 455 /* 456 * Determine if a block is in the cache. 457 * Just look on what would be its hash chain. If it's there, return 458 * a pointer to it, unless it's marked invalid. If it's marked invalid, 459 * we normally don't return the buffer, unless the caller explicitly 460 * wants us to. 461 */ 462 struct buf * 463 incore(vp, blkno) 464 struct vnode *vp; 465 daddr_t blkno; 466 { 467 struct buf *bp; 468 469 bp = BUFHASH(vp, blkno)->lh_first; 470 471 /* Search hash chain */ 472 for (; bp != NULL; bp = bp->b_hash.le_next) { 473 if (bp->b_lblkno == blkno && bp->b_vp == vp && 474 !ISSET(bp->b_flags, B_INVAL)) 475 return (bp); 476 } 477 478 return (0); 479 } 480 481 /* 482 * Get a block of requested size that is associated with 483 * a given vnode and block offset. If it is found in the 484 * block cache, mark it as having been found, make it busy 485 * and return it. Otherwise, return an empty block of the 486 * correct size. It is up to the caller to insure that the 487 * cached blocks be of the correct size. 488 */ 489 struct buf * 490 getblk(vp, blkno, size, slpflag, slptimeo) 491 register struct vnode *vp; 492 daddr_t blkno; 493 int size, slpflag, slptimeo; 494 { 495 struct buf *bp; 496 int s, err; 497 498 start: 499 s = splbio(); 500 if (bp = incore(vp, blkno)) { /* XXX NFS VOP_BWRITE foolishness */ 501 if (ISSET(bp->b_flags, B_BUSY)) { 502 SET(bp->b_flags, B_WANTED); 503 err = tsleep(bp, slpflag | (PRIBIO + 1), "getblk", 504 slptimeo); 505 splx(s); 506 if (err) 507 return (NULL); 508 goto start; 509 } 510 SET(bp->b_flags, (B_BUSY | B_CACHE)); 511 bremfree(bp); 512 splx(s); 513 allocbuf(bp, size); 514 } else { 515 splx(s); 516 if ((bp = getnewbuf(slpflag, slptimeo)) == NULL) 517 goto start; 518 binshash(bp, BUFHASH(vp, blkno)); 519 allocbuf(bp, size); 520 bp->b_blkno = bp->b_lblkno = blkno; 521 s = splbio(); 522 bgetvp(vp, bp); 523 splx(s); 524 } 525 return (bp); 526 } 527 528 /* 529 * Get an empty, disassociated buffer of given size. 530 */ 531 struct buf * 532 geteblk(size) 533 int size; 534 { 535 struct buf *bp; 536 537 while ((bp = getnewbuf(0, 0)) == 0) 538 ; 539 SET(bp->b_flags, B_INVAL); 540 binshash(bp, &invalhash); 541 allocbuf(bp, size); 542 543 return (bp); 544 } 545 546 /* 547 * Expand or contract the actual memory allocated to a buffer. 548 * 549 * If the buffer shrinks, data is lost, so it's up to the 550 * caller to have written it out *first*; this routine will not 551 * start a write. If the buffer grows, it's the callers 552 * responsibility to fill out the buffer's additional contents. 553 */ 554 allocbuf(bp, size) 555 struct buf *bp; 556 int size; 557 { 558 struct buf *nbp; 559 vm_size_t desired_size; 560 int s; 561 562 desired_size = roundup(size, CLBYTES); 563 if (desired_size > MAXBSIZE) 564 panic("allocbuf: buffer larger than MAXBSIZE requested"); 565 566 if (bp->b_bufsize == desired_size) 567 goto out; 568 569 /* 570 * If the buffer is smaller than the desired size, we need to snarf 571 * it from other buffers. Get buffers (via getnewbuf()), and 572 * steal their pages. 573 */ 574 while (bp->b_bufsize < desired_size) { 575 int amt; 576 577 /* find a buffer */ 578 while ((nbp = getnewbuf(0, 0)) == NULL) 579 ; 580 SET(nbp->b_flags, B_INVAL); 581 binshash(nbp, &invalhash); 582 583 /* and steal its pages, up to the amount we need */ 584 amt = min(nbp->b_bufsize, (desired_size - bp->b_bufsize)); 585 pagemove((nbp->b_data + nbp->b_bufsize - amt), 586 bp->b_data + bp->b_bufsize, amt); 587 bp->b_bufsize += amt; 588 nbp->b_bufsize -= amt; 589 590 /* reduce transfer count if we stole some data */ 591 if (nbp->b_bcount > nbp->b_bufsize) 592 nbp->b_bcount = nbp->b_bufsize; 593 594 #ifdef DIAGNOSTIC 595 if (nbp->b_bufsize < 0) 596 panic("allocbuf: negative bufsize"); 597 #endif 598 599 brelse(nbp); 600 } 601 602 /* 603 * If we want a buffer smaller than the current size, 604 * shrink this buffer. Grab a buf head from the EMPTY queue, 605 * move a page onto it, and put it on front of the AGE queue. 606 * If there are no free buffer headers, leave the buffer alone. 607 */ 608 if (bp->b_bufsize > desired_size) { 609 s = splbio(); 610 if ((nbp = bufqueues[BQ_EMPTY].tqh_first) == NULL) { 611 /* No free buffer head */ 612 splx(s); 613 goto out; 614 } 615 bremfree(nbp); 616 SET(nbp->b_flags, B_BUSY); 617 splx(s); 618 619 /* move the page to it and note this change */ 620 pagemove(bp->b_data + desired_size, 621 nbp->b_data, bp->b_bufsize - desired_size); 622 nbp->b_bufsize = bp->b_bufsize - desired_size; 623 bp->b_bufsize = desired_size; 624 nbp->b_bcount = 0; 625 SET(nbp->b_flags, B_INVAL); 626 627 /* release the newly-filled buffer and leave */ 628 brelse(nbp); 629 } 630 631 out: 632 bp->b_bcount = size; 633 } 634 635 /* 636 * Find a buffer which is available for use. 637 * Select something from a free list. 638 * Preference is to AGE list, then LRU list. 639 */ 640 struct buf * 641 getnewbuf(slpflag, slptimeo) 642 int slpflag, slptimeo; 643 { 644 register struct buf *bp; 645 int s; 646 647 start: 648 s = splbio(); 649 if ((bp = bufqueues[BQ_AGE].tqh_first) != NULL || 650 (bp = bufqueues[BQ_LRU].tqh_first) != NULL) { 651 bremfree(bp); 652 } else { 653 /* wait for a free buffer of any kind */ 654 needbuffer = 1; 655 tsleep(&needbuffer, slpflag|(PRIBIO+1), "getnewbuf", slptimeo); 656 splx(s); 657 return (0); 658 } 659 660 /* Buffer is no longer on free lists. */ 661 SET(bp->b_flags, B_BUSY); 662 splx(s); 663 664 /* If buffer was a delayed write, start it, and go back to the top. */ 665 if (ISSET(bp->b_flags, B_DELWRI)) { 666 bawrite (bp); 667 goto start; 668 } 669 670 /* disassociate us from our vnode, if we had one... */ 671 s = splbio(); 672 if (bp->b_vp) 673 brelvp(bp); 674 splx(s); 675 676 /* clear out various other fields */ 677 bp->b_flags = B_BUSY; 678 bp->b_dev = NODEV; 679 bp->b_blkno = bp->b_lblkno = 0; 680 bp->b_iodone = 0; 681 bp->b_error = 0; 682 bp->b_resid = 0; 683 bp->b_bcount = 0; 684 bp->b_dirtyoff = bp->b_dirtyend = 0; 685 bp->b_validoff = bp->b_validend = 0; 686 687 /* nuke any credentials we were holding */ 688 if (bp->b_rcred != NOCRED) { 689 crfree(bp->b_rcred); 690 bp->b_rcred = NOCRED; 691 } 692 if (bp->b_wcred != NOCRED) { 693 crfree(bp->b_wcred); 694 bp->b_wcred = NOCRED; 695 } 696 697 bremhash(bp); 698 return (bp); 699 } 700 701 /* 702 * Wait for operations on the buffer to complete. 703 * When they do, extract and return the I/O's error value. 704 */ 705 int 706 biowait(bp) 707 struct buf *bp; 708 { 709 int s; 710 711 s = splbio(); 712 while (!ISSET(bp->b_flags, B_DONE)) 713 tsleep(bp, PRIBIO + 1, "biowait", 0); 714 splx(s); 715 716 /* check for interruption of I/O (e.g. via NFS), then errors. */ 717 if (ISSET(bp->b_flags, B_EINTR)) { 718 CLR(bp->b_flags, B_EINTR); 719 return (EINTR); 720 } else if (ISSET(bp->b_flags, B_ERROR)) 721 return (bp->b_error ? bp->b_error : EIO); 722 else 723 return (0); 724 } 725 726 /* 727 * Mark I/O complete on a buffer. 728 * 729 * If a callback has been requested, e.g. the pageout 730 * daemon, do so. Otherwise, awaken waiting processes. 731 * 732 * [ Leffler, et al., says on p.247: 733 * "This routine wakes up the blocked process, frees the buffer 734 * for an asynchronous write, or, for a request by the pagedaemon 735 * process, invokes a procedure specified in the buffer structure" ] 736 * 737 * In real life, the pagedaemon (or other system processes) wants 738 * to do async stuff to, and doesn't want the buffer brelse()'d. 739 * (for swap pager, that puts swap buffers on the free lists (!!!), 740 * for the vn device, that puts malloc'd buffers on the free lists!) 741 */ 742 void 743 biodone(bp) 744 struct buf *bp; 745 { 746 if (ISSET(bp->b_flags, B_DONE)) 747 panic("biodone already"); 748 SET(bp->b_flags, B_DONE); /* note that it's done */ 749 750 if (!ISSET(bp->b_flags, B_READ)) /* wake up reader */ 751 vwakeup(bp); 752 753 if (ISSET(bp->b_flags, B_CALL)) { /* if necessary, call out */ 754 CLR(bp->b_flags, B_CALL); /* but note callout done */ 755 (*bp->b_iodone)(bp); 756 } else if (ISSET(bp->b_flags, B_ASYNC)) /* if async, release it */ 757 brelse(bp); 758 else { /* or just wakeup the buffer */ 759 CLR(bp->b_flags, B_WANTED); 760 wakeup(bp); 761 } 762 } 763 764 /* 765 * Return a count of buffers on the "locked" queue. 766 */ 767 int 768 count_lock_queue() 769 { 770 register struct buf *bp; 771 register int n = 0; 772 773 for (bp = bufqueues[BQ_LOCKED].tqh_first; bp; 774 bp = bp->b_freelist.tqe_next) 775 n++; 776 return (n); 777 } 778 779 #ifdef DIAGNOSTIC 780 /* 781 * Print out statistics on the current allocation of the buffer pool. 782 * Can be enabled to print out on every ``sync'' by setting "syncprt" 783 * in vfs_syscalls.c using sysctl. 784 */ 785 void 786 vfs_bufstats() 787 { 788 int s, i, j, count; 789 register struct buf *bp; 790 register struct bqueues *dp; 791 int counts[MAXBSIZE/CLBYTES+1]; 792 static char *bname[BQUEUES] = { "LOCKED", "LRU", "AGE", "EMPTY" }; 793 794 for (dp = bufqueues, i = 0; dp < &bufqueues[BQUEUES]; dp++, i++) { 795 count = 0; 796 for (j = 0; j <= MAXBSIZE/CLBYTES; j++) 797 counts[j] = 0; 798 s = splbio(); 799 for (bp = dp->tqh_first; bp; bp = bp->b_freelist.tqe_next) { 800 counts[bp->b_bufsize/CLBYTES]++; 801 count++; 802 } 803 splx(s); 804 printf("%s: total-%d", bname[i], count); 805 for (j = 0; j <= MAXBSIZE/CLBYTES; j++) 806 if (counts[j] != 0) 807 printf(", %d-%d", j * CLBYTES, counts[j]); 808 printf("\n"); 809 } 810 } 811 #endif /* DIAGNOSTIC */ 812