1 /* $NetBSD: vfs_bio.c,v 1.190 2008/02/29 12:10:09 yamt Exp $ */ 2 3 /*- 4 * Copyright (c) 2007 The NetBSD Foundation, Inc. 5 * All rights reserved. 6 * 7 * This code is derived from software contributed to The NetBSD Foundation 8 * by Andrew Doran. 9 * 10 * Redistribution and use in source and binary forms, with or without 11 * modification, are permitted provided that the following conditions 12 * are met: 13 * 1. Redistributions of source code must retain the above copyright 14 * notice, this list of conditions and the following disclaimer. 15 * 2. Redistributions in binary form must reproduce the above copyright 16 * notice, this list of conditions and the following disclaimer in the 17 * documentation and/or other materials provided with the distribution. 18 * 3. All advertising materials mentioning features or use of this software 19 * must display the following acknowledgement: 20 * This product includes software developed by the NetBSD 21 * Foundation, Inc. and its contributors. 22 * 4. Neither the name of The NetBSD Foundation nor the names of its 23 * contributors may be used to endorse or promote products derived 24 * from this software without specific prior written permission. 25 * 26 * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS 27 * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED 28 * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR 29 * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS 30 * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR 31 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF 32 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS 33 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN 34 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) 35 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE 36 * POSSIBILITY OF SUCH DAMAGE. 37 */ 38 39 /*- 40 * Copyright (c) 1982, 1986, 1989, 1993 41 * The Regents of the University of California. All rights reserved. 42 * (c) UNIX System Laboratories, Inc. 43 * All or some portions of this file are derived from material licensed 44 * to the University of California by American Telephone and Telegraph 45 * Co. or Unix System Laboratories, Inc. and are reproduced herein with 46 * the permission of UNIX System Laboratories, Inc. 47 * 48 * Redistribution and use in source and binary forms, with or without 49 * modification, are permitted provided that the following conditions 50 * are met: 51 * 1. Redistributions of source code must retain the above copyright 52 * notice, this list of conditions and the following disclaimer. 53 * 2. Redistributions in binary form must reproduce the above copyright 54 * notice, this list of conditions and the following disclaimer in the 55 * documentation and/or other materials provided with the distribution. 56 * 3. Neither the name of the University nor the names of its contributors 57 * may be used to endorse or promote products derived from this software 58 * without specific prior written permission. 59 * 60 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND 61 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 62 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 63 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE 64 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 65 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS 66 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 67 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT 68 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY 69 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 70 * SUCH DAMAGE. 71 * 72 * @(#)vfs_bio.c 8.6 (Berkeley) 1/11/94 73 */ 74 75 /*- 76 * Copyright (c) 1994 Christopher G. Demetriou 77 * 78 * Redistribution and use in source and binary forms, with or without 79 * modification, are permitted provided that the following conditions 80 * are met: 81 * 1. Redistributions of source code must retain the above copyright 82 * notice, this list of conditions and the following disclaimer. 83 * 2. Redistributions in binary form must reproduce the above copyright 84 * notice, this list of conditions and the following disclaimer in the 85 * documentation and/or other materials provided with the distribution. 86 * 3. All advertising materials mentioning features or use of this software 87 * must display the following acknowledgement: 88 * This product includes software developed by the University of 89 * California, Berkeley and its contributors. 90 * 4. Neither the name of the University nor the names of its contributors 91 * may be used to endorse or promote products derived from this software 92 * without specific prior written permission. 93 * 94 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND 95 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 96 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 97 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE 98 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 99 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS 100 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 101 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT 102 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY 103 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 104 * SUCH DAMAGE. 105 * 106 * @(#)vfs_bio.c 8.6 (Berkeley) 1/11/94 107 */ 108 109 /* 110 * Some references: 111 * Bach: The Design of the UNIX Operating System (Prentice Hall, 1986) 112 * Leffler, et al.: The Design and Implementation of the 4.3BSD 113 * UNIX Operating System (Addison Welley, 1989) 114 */ 115 116 #include <sys/cdefs.h> 117 __KERNEL_RCSID(0, "$NetBSD: vfs_bio.c,v 1.190 2008/02/29 12:10:09 yamt Exp $"); 118 119 #include "fs_ffs.h" 120 #include "opt_bufcache.h" 121 122 #include <sys/param.h> 123 #include <sys/systm.h> 124 #include <sys/kernel.h> 125 #include <sys/proc.h> 126 #include <sys/buf.h> 127 #include <sys/vnode.h> 128 #include <sys/mount.h> 129 #include <sys/malloc.h> 130 #include <sys/resourcevar.h> 131 #include <sys/sysctl.h> 132 #include <sys/conf.h> 133 #include <sys/kauth.h> 134 #include <sys/intr.h> 135 #include <sys/cpu.h> 136 137 #include <uvm/uvm.h> 138 139 #include <miscfs/specfs/specdev.h> 140 141 #ifndef BUFPAGES 142 # define BUFPAGES 0 143 #endif 144 145 #ifdef BUFCACHE 146 # if (BUFCACHE < 5) || (BUFCACHE > 95) 147 # error BUFCACHE is not between 5 and 95 148 # endif 149 #else 150 # define BUFCACHE 15 151 #endif 152 153 u_int nbuf; /* XXX - for softdep_lockedbufs */ 154 u_int bufpages = BUFPAGES; /* optional hardwired count */ 155 u_int bufcache = BUFCACHE; /* max % of RAM to use for buffer cache */ 156 157 /* Function prototypes */ 158 struct bqueue; 159 160 static void buf_setwm(void); 161 static int buf_trim(void); 162 static void *bufpool_page_alloc(struct pool *, int); 163 static void bufpool_page_free(struct pool *, void *); 164 static buf_t *bio_doread(struct vnode *, daddr_t, int, 165 kauth_cred_t, int); 166 static buf_t *getnewbuf(int, int, int); 167 static int buf_lotsfree(void); 168 static int buf_canrelease(void); 169 static u_long buf_mempoolidx(u_long); 170 static u_long buf_roundsize(u_long); 171 static void *buf_malloc(size_t); 172 static void buf_mrelease(void *, size_t); 173 static void binsheadfree(buf_t *, struct bqueue *); 174 static void binstailfree(buf_t *, struct bqueue *); 175 int count_lock_queue(void); /* XXX */ 176 #ifdef DEBUG 177 static int checkfreelist(buf_t *, struct bqueue *); 178 #endif 179 static void biointr(void *); 180 static void biodone2(buf_t *); 181 static void bref(buf_t *); 182 static void brele(buf_t *); 183 184 /* 185 * Definitions for the buffer hash lists. 186 */ 187 #define BUFHASH(dvp, lbn) \ 188 (&bufhashtbl[(((long)(dvp) >> 8) + (int)(lbn)) & bufhash]) 189 LIST_HEAD(bufhashhdr, buf) *bufhashtbl, invalhash; 190 u_long bufhash; 191 struct bqueue bufqueues[BQUEUES]; 192 const struct bio_ops *bioopsp; /* I/O operation notification */ 193 194 static kcondvar_t needbuffer_cv; 195 196 /* 197 * Buffer queue lock. 198 */ 199 kmutex_t bufcache_lock; 200 kmutex_t buffer_lock; 201 202 /* Software ISR for completed transfers. */ 203 static void *biodone_sih; 204 205 /* Buffer pool for I/O buffers. */ 206 static pool_cache_t buf_cache; 207 static pool_cache_t bufio_cache; 208 209 /* XXX - somewhat gross.. */ 210 #if MAXBSIZE == 0x2000 211 #define NMEMPOOLS 5 212 #elif MAXBSIZE == 0x4000 213 #define NMEMPOOLS 6 214 #elif MAXBSIZE == 0x8000 215 #define NMEMPOOLS 7 216 #else 217 #define NMEMPOOLS 8 218 #endif 219 220 #define MEMPOOL_INDEX_OFFSET 9 /* smallest pool is 512 bytes */ 221 #if (1 << (NMEMPOOLS + MEMPOOL_INDEX_OFFSET - 1)) != MAXBSIZE 222 #error update vfs_bio buffer memory parameters 223 #endif 224 225 /* Buffer memory pools */ 226 static struct pool bmempools[NMEMPOOLS]; 227 228 struct vm_map *buf_map; 229 230 /* 231 * Buffer memory pool allocator. 232 */ 233 static void * 234 bufpool_page_alloc(struct pool *pp, int flags) 235 { 236 237 return (void *)uvm_km_alloc(buf_map, 238 MAXBSIZE, MAXBSIZE, 239 ((flags & PR_WAITOK) ? 0 : UVM_KMF_NOWAIT | UVM_KMF_TRYLOCK) 240 | UVM_KMF_WIRED); 241 } 242 243 static void 244 bufpool_page_free(struct pool *pp, void *v) 245 { 246 247 uvm_km_free(buf_map, (vaddr_t)v, MAXBSIZE, UVM_KMF_WIRED); 248 } 249 250 static struct pool_allocator bufmempool_allocator = { 251 .pa_alloc = bufpool_page_alloc, 252 .pa_free = bufpool_page_free, 253 .pa_pagesz = MAXBSIZE, 254 }; 255 256 /* Buffer memory management variables */ 257 u_long bufmem_valimit; 258 u_long bufmem_hiwater; 259 u_long bufmem_lowater; 260 u_long bufmem; 261 262 /* 263 * MD code can call this to set a hard limit on the amount 264 * of virtual memory used by the buffer cache. 265 */ 266 int 267 buf_setvalimit(vsize_t sz) 268 { 269 270 /* We need to accommodate at least NMEMPOOLS of MAXBSIZE each */ 271 if (sz < NMEMPOOLS * MAXBSIZE) 272 return EINVAL; 273 274 bufmem_valimit = sz; 275 return 0; 276 } 277 278 static void 279 buf_setwm(void) 280 { 281 282 bufmem_hiwater = buf_memcalc(); 283 /* lowater is approx. 2% of memory (with bufcache = 15) */ 284 #define BUFMEM_WMSHIFT 3 285 #define BUFMEM_HIWMMIN (64 * 1024 << BUFMEM_WMSHIFT) 286 if (bufmem_hiwater < BUFMEM_HIWMMIN) 287 /* Ensure a reasonable minimum value */ 288 bufmem_hiwater = BUFMEM_HIWMMIN; 289 bufmem_lowater = bufmem_hiwater >> BUFMEM_WMSHIFT; 290 } 291 292 #ifdef DEBUG 293 int debug_verify_freelist = 0; 294 static int 295 checkfreelist(buf_t *bp, struct bqueue *dp) 296 { 297 buf_t *b; 298 299 if (!debug_verify_freelist) 300 return 1; 301 302 TAILQ_FOREACH(b, &dp->bq_queue, b_freelist) { 303 if (b == bp) 304 return 1; 305 } 306 307 return 0; 308 } 309 #endif 310 311 /* 312 * Insq/Remq for the buffer hash lists. 313 * Call with buffer queue locked. 314 */ 315 static void 316 binsheadfree(buf_t *bp, struct bqueue *dp) 317 { 318 319 KASSERT(bp->b_freelistindex == -1); 320 TAILQ_INSERT_HEAD(&dp->bq_queue, bp, b_freelist); 321 dp->bq_bytes += bp->b_bufsize; 322 bp->b_freelistindex = dp - bufqueues; 323 } 324 325 static void 326 binstailfree(buf_t *bp, struct bqueue *dp) 327 { 328 329 KASSERT(bp->b_freelistindex == -1); 330 TAILQ_INSERT_TAIL(&dp->bq_queue, bp, b_freelist); 331 dp->bq_bytes += bp->b_bufsize; 332 bp->b_freelistindex = dp - bufqueues; 333 } 334 335 void 336 bremfree(buf_t *bp) 337 { 338 struct bqueue *dp; 339 int bqidx = bp->b_freelistindex; 340 341 KASSERT(mutex_owned(&bufcache_lock)); 342 343 KASSERT(bqidx != -1); 344 dp = &bufqueues[bqidx]; 345 KDASSERT(checkfreelist(bp, dp)); 346 KASSERT(dp->bq_bytes >= bp->b_bufsize); 347 TAILQ_REMOVE(&dp->bq_queue, bp, b_freelist); 348 dp->bq_bytes -= bp->b_bufsize; 349 350 /* For the sysctl helper. */ 351 if (bp == dp->bq_marker) 352 dp->bq_marker = NULL; 353 354 #if defined(DIAGNOSTIC) 355 bp->b_freelistindex = -1; 356 #endif /* defined(DIAGNOSTIC) */ 357 } 358 359 /* 360 * Add a reference to an buffer structure that came from buf_cache. 361 */ 362 static inline void 363 bref(buf_t *bp) 364 { 365 366 KASSERT(mutex_owned(&bufcache_lock)); 367 KASSERT(bp->b_refcnt > 0); 368 369 bp->b_refcnt++; 370 } 371 372 /* 373 * Free an unused buffer structure that came from buf_cache. 374 */ 375 static inline void 376 brele(buf_t *bp) 377 { 378 379 KASSERT(mutex_owned(&bufcache_lock)); 380 KASSERT(bp->b_refcnt > 0); 381 382 if (bp->b_refcnt-- == 1) { 383 buf_destroy(bp); 384 #ifdef DEBUG 385 memset((char *)bp, 0, sizeof(*bp)); 386 #endif 387 pool_cache_put(buf_cache, bp); 388 } 389 } 390 391 u_long 392 buf_memcalc(void) 393 { 394 u_long n; 395 396 /* 397 * Determine the upper bound of memory to use for buffers. 398 * 399 * - If bufpages is specified, use that as the number 400 * pages. 401 * 402 * - Otherwise, use bufcache as the percentage of 403 * physical memory. 404 */ 405 if (bufpages != 0) { 406 n = bufpages; 407 } else { 408 if (bufcache < 5) { 409 printf("forcing bufcache %d -> 5", bufcache); 410 bufcache = 5; 411 } 412 if (bufcache > 95) { 413 printf("forcing bufcache %d -> 95", bufcache); 414 bufcache = 95; 415 } 416 n = physmem / 100 * bufcache; 417 } 418 419 n <<= PAGE_SHIFT; 420 if (bufmem_valimit != 0 && n > bufmem_valimit) 421 n = bufmem_valimit; 422 423 return (n); 424 } 425 426 /* 427 * Initialize buffers and hash links for buffers. 428 */ 429 void 430 bufinit(void) 431 { 432 struct bqueue *dp; 433 int use_std; 434 u_int i; 435 436 mutex_init(&bufcache_lock, MUTEX_DEFAULT, IPL_NONE); 437 mutex_init(&buffer_lock, MUTEX_DEFAULT, IPL_NONE); 438 cv_init(&needbuffer_cv, "needbuf"); 439 440 /* 441 * Initialize buffer cache memory parameters. 442 */ 443 bufmem = 0; 444 buf_setwm(); 445 446 if (bufmem_valimit != 0) { 447 vaddr_t minaddr = 0, maxaddr; 448 buf_map = uvm_km_suballoc(kernel_map, &minaddr, &maxaddr, 449 bufmem_valimit, 0, false, 0); 450 if (buf_map == NULL) 451 panic("bufinit: cannot allocate submap"); 452 } else 453 buf_map = kernel_map; 454 455 /* On "small" machines use small pool page sizes where possible */ 456 use_std = (physmem < atop(16*1024*1024)); 457 458 /* 459 * Also use them on systems that can map the pool pages using 460 * a direct-mapped segment. 461 */ 462 #ifdef PMAP_MAP_POOLPAGE 463 use_std = 1; 464 #endif 465 466 buf_cache = pool_cache_init(sizeof(buf_t), 0, 0, 0, 467 "bufpl", NULL, IPL_SOFTBIO, NULL, NULL, NULL); 468 bufio_cache = pool_cache_init(sizeof(buf_t), 0, 0, 0, 469 "biopl", NULL, IPL_BIO, NULL, NULL, NULL); 470 471 bufmempool_allocator.pa_backingmap = buf_map; 472 for (i = 0; i < NMEMPOOLS; i++) { 473 struct pool_allocator *pa; 474 struct pool *pp = &bmempools[i]; 475 u_int size = 1 << (i + MEMPOOL_INDEX_OFFSET); 476 char *name = malloc(8, M_TEMP, M_WAITOK); 477 if (__predict_true(size >= 1024)) 478 (void)snprintf(name, 8, "buf%dk", size / 1024); 479 else 480 (void)snprintf(name, 8, "buf%db", size); 481 pa = (size <= PAGE_SIZE && use_std) 482 ? &pool_allocator_nointr 483 : &bufmempool_allocator; 484 pool_init(pp, size, 0, 0, 0, name, pa, IPL_NONE); 485 pool_setlowat(pp, 1); 486 pool_sethiwat(pp, 1); 487 } 488 489 /* Initialize the buffer queues */ 490 for (dp = bufqueues; dp < &bufqueues[BQUEUES]; dp++) { 491 TAILQ_INIT(&dp->bq_queue); 492 dp->bq_bytes = 0; 493 } 494 495 /* 496 * Estimate hash table size based on the amount of memory we 497 * intend to use for the buffer cache. The average buffer 498 * size is dependent on our clients (i.e. filesystems). 499 * 500 * For now, use an empirical 3K per buffer. 501 */ 502 nbuf = (bufmem_hiwater / 1024) / 3; 503 bufhashtbl = hashinit(nbuf, HASH_LIST, M_CACHE, M_WAITOK, &bufhash); 504 } 505 506 void 507 bufinit2(void) 508 { 509 510 biodone_sih = softint_establish(SOFTINT_BIO | SOFTINT_MPSAFE, biointr, 511 NULL); 512 if (biodone_sih == NULL) 513 panic("bufinit2: can't establish soft interrupt"); 514 } 515 516 static int 517 buf_lotsfree(void) 518 { 519 int try, thresh; 520 521 /* Always allocate if doing copy on write */ 522 if (curlwp->l_pflag & LP_UFSCOW) 523 return 1; 524 525 /* Always allocate if less than the low water mark. */ 526 if (bufmem < bufmem_lowater) 527 return 1; 528 529 /* Never allocate if greater than the high water mark. */ 530 if (bufmem > bufmem_hiwater) 531 return 0; 532 533 /* If there's anything on the AGE list, it should be eaten. */ 534 if (TAILQ_FIRST(&bufqueues[BQ_AGE].bq_queue) != NULL) 535 return 0; 536 537 /* 538 * The probabily of getting a new allocation is inversely 539 * proportional to the current size of the cache, using 540 * a granularity of 16 steps. 541 */ 542 try = random() & 0x0000000fL; 543 544 /* Don't use "16 * bufmem" here to avoid a 32-bit overflow. */ 545 thresh = (bufmem - bufmem_lowater) / 546 ((bufmem_hiwater - bufmem_lowater) / 16); 547 548 if (try >= thresh) 549 return 1; 550 551 /* Otherwise don't allocate. */ 552 return 0; 553 } 554 555 /* 556 * Return estimate of bytes we think need to be 557 * released to help resolve low memory conditions. 558 * 559 * => called with bufcache_lock held. 560 */ 561 static int 562 buf_canrelease(void) 563 { 564 int pagedemand, ninvalid = 0; 565 566 KASSERT(mutex_owned(&bufcache_lock)); 567 568 if (bufmem < bufmem_lowater) 569 return 0; 570 571 if (bufmem > bufmem_hiwater) 572 return bufmem - bufmem_hiwater; 573 574 ninvalid += bufqueues[BQ_AGE].bq_bytes; 575 576 pagedemand = uvmexp.freetarg - uvmexp.free; 577 if (pagedemand < 0) 578 return ninvalid; 579 return MAX(ninvalid, MIN(2 * MAXBSIZE, 580 MIN((bufmem - bufmem_lowater) / 16, pagedemand * PAGE_SIZE))); 581 } 582 583 /* 584 * Buffer memory allocation helper functions 585 */ 586 static u_long 587 buf_mempoolidx(u_long size) 588 { 589 u_int n = 0; 590 591 size -= 1; 592 size >>= MEMPOOL_INDEX_OFFSET; 593 while (size) { 594 size >>= 1; 595 n += 1; 596 } 597 if (n >= NMEMPOOLS) 598 panic("buf mem pool index %d", n); 599 return n; 600 } 601 602 static u_long 603 buf_roundsize(u_long size) 604 { 605 /* Round up to nearest power of 2 */ 606 return (1 << (buf_mempoolidx(size) + MEMPOOL_INDEX_OFFSET)); 607 } 608 609 static void * 610 buf_malloc(size_t size) 611 { 612 u_int n = buf_mempoolidx(size); 613 void *addr; 614 615 while (1) { 616 addr = pool_get(&bmempools[n], PR_NOWAIT); 617 if (addr != NULL) 618 break; 619 620 /* No memory, see if we can free some. If so, try again */ 621 mutex_enter(&bufcache_lock); 622 if (buf_drain(1) > 0) { 623 mutex_exit(&bufcache_lock); 624 continue; 625 } 626 627 if (curlwp == uvm.pagedaemon_lwp) { 628 mutex_exit(&bufcache_lock); 629 return NULL; 630 } 631 632 /* Wait for buffers to arrive on the LRU queue */ 633 cv_timedwait(&needbuffer_cv, &bufcache_lock, hz / 4); 634 mutex_exit(&bufcache_lock); 635 } 636 637 return addr; 638 } 639 640 static void 641 buf_mrelease(void *addr, size_t size) 642 { 643 644 pool_put(&bmempools[buf_mempoolidx(size)], addr); 645 } 646 647 /* 648 * bread()/breadn() helper. 649 */ 650 static buf_t * 651 bio_doread(struct vnode *vp, daddr_t blkno, int size, kauth_cred_t cred, 652 int async) 653 { 654 buf_t *bp; 655 struct mount *mp; 656 657 bp = getblk(vp, blkno, size, 0, 0); 658 659 #ifdef DIAGNOSTIC 660 if (bp == NULL) { 661 panic("bio_doread: no such buf"); 662 } 663 #endif 664 665 /* 666 * If buffer does not have data valid, start a read. 667 * Note that if buffer is BC_INVAL, getblk() won't return it. 668 * Therefore, it's valid if its I/O has completed or been delayed. 669 */ 670 if (!ISSET(bp->b_oflags, (BO_DONE | BO_DELWRI))) { 671 /* Start I/O for the buffer. */ 672 SET(bp->b_flags, B_READ | async); 673 if (async) 674 BIO_SETPRIO(bp, BPRIO_TIMELIMITED); 675 else 676 BIO_SETPRIO(bp, BPRIO_TIMECRITICAL); 677 VOP_STRATEGY(vp, bp); 678 679 /* Pay for the read. */ 680 curproc->p_stats->p_ru.ru_inblock++; 681 } else if (async) 682 brelse(bp, 0); 683 684 if (vp->v_type == VBLK) 685 mp = vp->v_specmountpoint; 686 else 687 mp = vp->v_mount; 688 689 /* 690 * Collect statistics on synchronous and asynchronous reads. 691 * Reads from block devices are charged to their associated 692 * filesystem (if any). 693 */ 694 if (mp != NULL) { 695 if (async == 0) 696 mp->mnt_stat.f_syncreads++; 697 else 698 mp->mnt_stat.f_asyncreads++; 699 } 700 701 return (bp); 702 } 703 704 /* 705 * Read a disk block. 706 * This algorithm described in Bach (p.54). 707 */ 708 int 709 bread(struct vnode *vp, daddr_t blkno, int size, kauth_cred_t cred, 710 buf_t **bpp) 711 { 712 buf_t *bp; 713 714 /* Get buffer for block. */ 715 bp = *bpp = bio_doread(vp, blkno, size, cred, 0); 716 717 /* Wait for the read to complete, and return result. */ 718 return (biowait(bp)); 719 } 720 721 /* 722 * Read-ahead multiple disk blocks. The first is sync, the rest async. 723 * Trivial modification to the breada algorithm presented in Bach (p.55). 724 */ 725 int 726 breadn(struct vnode *vp, daddr_t blkno, int size, daddr_t *rablks, 727 int *rasizes, int nrablks, kauth_cred_t cred, buf_t **bpp) 728 { 729 buf_t *bp; 730 int i; 731 732 bp = *bpp = bio_doread(vp, blkno, size, cred, 0); 733 734 /* 735 * For each of the read-ahead blocks, start a read, if necessary. 736 */ 737 mutex_enter(&bufcache_lock); 738 for (i = 0; i < nrablks; i++) { 739 /* If it's in the cache, just go on to next one. */ 740 if (incore(vp, rablks[i])) 741 continue; 742 743 /* Get a buffer for the read-ahead block */ 744 mutex_exit(&bufcache_lock); 745 (void) bio_doread(vp, rablks[i], rasizes[i], cred, B_ASYNC); 746 mutex_enter(&bufcache_lock); 747 } 748 mutex_exit(&bufcache_lock); 749 750 /* Otherwise, we had to start a read for it; wait until it's valid. */ 751 return (biowait(bp)); 752 } 753 754 /* 755 * Read with single-block read-ahead. Defined in Bach (p.55), but 756 * implemented as a call to breadn(). 757 * XXX for compatibility with old file systems. 758 */ 759 int 760 breada(struct vnode *vp, daddr_t blkno, int size, daddr_t rablkno, 761 int rabsize, kauth_cred_t cred, buf_t **bpp) 762 { 763 764 return (breadn(vp, blkno, size, &rablkno, &rabsize, 1, cred, bpp)); 765 } 766 767 /* 768 * Block write. Described in Bach (p.56) 769 */ 770 int 771 bwrite(buf_t *bp) 772 { 773 int rv, sync, wasdelayed; 774 struct vnode *vp; 775 struct mount *mp; 776 777 KASSERT(ISSET(bp->b_cflags, BC_BUSY)); 778 779 vp = bp->b_vp; 780 if (vp != NULL) { 781 KASSERT(bp->b_objlock == &vp->v_interlock); 782 if (vp->v_type == VBLK) 783 mp = vp->v_specmountpoint; 784 else 785 mp = vp->v_mount; 786 } else { 787 mp = NULL; 788 } 789 790 /* 791 * Remember buffer type, to switch on it later. If the write was 792 * synchronous, but the file system was mounted with MNT_ASYNC, 793 * convert it to a delayed write. 794 * XXX note that this relies on delayed tape writes being converted 795 * to async, not sync writes (which is safe, but ugly). 796 */ 797 sync = !ISSET(bp->b_flags, B_ASYNC); 798 if (sync && mp != NULL && ISSET(mp->mnt_flag, MNT_ASYNC)) { 799 bdwrite(bp); 800 return (0); 801 } 802 803 /* 804 * Collect statistics on synchronous and asynchronous writes. 805 * Writes to block devices are charged to their associated 806 * filesystem (if any). 807 */ 808 if (mp != NULL) { 809 if (sync) 810 mp->mnt_stat.f_syncwrites++; 811 else 812 mp->mnt_stat.f_asyncwrites++; 813 } 814 815 /* 816 * Pay for the I/O operation and make sure the buf is on the correct 817 * vnode queue. 818 */ 819 bp->b_error = 0; 820 wasdelayed = ISSET(bp->b_oflags, BO_DELWRI); 821 CLR(bp->b_flags, B_READ); 822 if (wasdelayed) { 823 mutex_enter(&bufcache_lock); 824 mutex_enter(bp->b_objlock); 825 CLR(bp->b_oflags, BO_DONE | BO_DELWRI); 826 reassignbuf(bp, bp->b_vp); 827 mutex_exit(&bufcache_lock); 828 } else { 829 curproc->p_stats->p_ru.ru_oublock++; 830 mutex_enter(bp->b_objlock); 831 CLR(bp->b_oflags, BO_DONE | BO_DELWRI); 832 } 833 if (vp != NULL) 834 vp->v_numoutput++; 835 mutex_exit(bp->b_objlock); 836 837 /* Initiate disk write. */ 838 if (sync) 839 BIO_SETPRIO(bp, BPRIO_TIMECRITICAL); 840 else 841 BIO_SETPRIO(bp, BPRIO_TIMELIMITED); 842 843 VOP_STRATEGY(vp, bp); 844 845 if (sync) { 846 /* If I/O was synchronous, wait for it to complete. */ 847 rv = biowait(bp); 848 849 /* Release the buffer. */ 850 brelse(bp, 0); 851 852 return (rv); 853 } else { 854 return (0); 855 } 856 } 857 858 int 859 vn_bwrite(void *v) 860 { 861 struct vop_bwrite_args *ap = v; 862 863 return (bwrite(ap->a_bp)); 864 } 865 866 /* 867 * Delayed write. 868 * 869 * The buffer is marked dirty, but is not queued for I/O. 870 * This routine should be used when the buffer is expected 871 * to be modified again soon, typically a small write that 872 * partially fills a buffer. 873 * 874 * NB: magnetic tapes cannot be delayed; they must be 875 * written in the order that the writes are requested. 876 * 877 * Described in Leffler, et al. (pp. 208-213). 878 */ 879 void 880 bdwrite(buf_t *bp) 881 { 882 883 KASSERT(ISSET(bp->b_cflags, BC_BUSY)); 884 885 /* If this is a tape block, write the block now. */ 886 if (bdev_type(bp->b_dev) == D_TAPE) { 887 bawrite(bp); 888 return; 889 } 890 891 /* 892 * If the block hasn't been seen before: 893 * (1) Mark it as having been seen, 894 * (2) Charge for the write, 895 * (3) Make sure it's on its vnode's correct block list. 896 */ 897 KASSERT(bp->b_vp == NULL || bp->b_objlock == &bp->b_vp->v_interlock); 898 899 if (!ISSET(bp->b_oflags, BO_DELWRI)) { 900 mutex_enter(&bufcache_lock); 901 mutex_enter(bp->b_objlock); 902 SET(bp->b_oflags, BO_DELWRI); 903 curproc->p_stats->p_ru.ru_oublock++; 904 reassignbuf(bp, bp->b_vp); 905 mutex_exit(&bufcache_lock); 906 } else { 907 mutex_enter(bp->b_objlock); 908 } 909 /* Otherwise, the "write" is done, so mark and release the buffer. */ 910 CLR(bp->b_oflags, BO_DONE); 911 mutex_exit(bp->b_objlock); 912 913 brelse(bp, 0); 914 } 915 916 /* 917 * Asynchronous block write; just an asynchronous bwrite(). 918 */ 919 void 920 bawrite(buf_t *bp) 921 { 922 923 KASSERT(ISSET(bp->b_cflags, BC_BUSY)); 924 925 SET(bp->b_flags, B_ASYNC); 926 VOP_BWRITE(bp); 927 } 928 929 /* 930 * Same as first half of bdwrite, mark buffer dirty, but do not release it. 931 * Call with the buffer interlock held. 932 * 933 * Note: called only from biodone() through ffs softdep's io_complete() 934 */ 935 void 936 bdirty(buf_t *bp) 937 { 938 939 KASSERT(mutex_owned(&bufcache_lock)); 940 KASSERT(bp->b_objlock == &bp->b_vp->v_interlock); 941 KASSERT(mutex_owned(bp->b_objlock)); 942 KASSERT(ISSET(bp->b_cflags, BC_BUSY)); 943 944 CLR(bp->b_cflags, BC_AGE); 945 946 if (!ISSET(bp->b_oflags, BO_DELWRI)) { 947 SET(bp->b_oflags, BO_DELWRI); 948 curproc->p_stats->p_ru.ru_oublock++; 949 reassignbuf(bp, bp->b_vp); 950 } 951 } 952 953 954 /* 955 * Release a buffer on to the free lists. 956 * Described in Bach (p. 46). 957 */ 958 void 959 brelsel(buf_t *bp, int set) 960 { 961 struct bqueue *bufq; 962 struct vnode *vp; 963 964 KASSERT(mutex_owned(&bufcache_lock)); 965 966 SET(bp->b_cflags, set); 967 968 KASSERT(ISSET(bp->b_cflags, BC_BUSY)); 969 KASSERT(bp->b_iodone == NULL); 970 971 /* Wake up any processes waiting for any buffer to become free. */ 972 cv_signal(&needbuffer_cv); 973 974 /* Wake up any proceeses waiting for _this_ buffer to become */ 975 if (ISSET(bp->b_cflags, BC_WANTED) != 0) { 976 CLR(bp->b_cflags, BC_WANTED|BC_AGE); 977 cv_broadcast(&bp->b_busy); 978 } 979 980 /* 981 * Determine which queue the buffer should be on, then put it there. 982 */ 983 984 /* If it's locked, don't report an error; try again later. */ 985 if (ISSET(bp->b_flags, B_LOCKED)) 986 bp->b_error = 0; 987 988 /* If it's not cacheable, or an error, mark it invalid. */ 989 if (ISSET(bp->b_cflags, BC_NOCACHE) || bp->b_error != 0) 990 SET(bp->b_cflags, BC_INVAL); 991 992 if (ISSET(bp->b_cflags, BC_VFLUSH)) { 993 /* 994 * This is a delayed write buffer that was just flushed to 995 * disk. It is still on the LRU queue. If it's become 996 * invalid, then we need to move it to a different queue; 997 * otherwise leave it in its current position. 998 */ 999 CLR(bp->b_cflags, BC_VFLUSH); 1000 if (!ISSET(bp->b_cflags, BC_INVAL|BC_AGE) && 1001 !ISSET(bp->b_flags, B_LOCKED) && bp->b_error == 0) { 1002 KDASSERT(checkfreelist(bp, &bufqueues[BQ_LRU])); 1003 goto already_queued; 1004 } else { 1005 bremfree(bp); 1006 } 1007 } 1008 1009 KDASSERT(checkfreelist(bp, &bufqueues[BQ_AGE])); 1010 KDASSERT(checkfreelist(bp, &bufqueues[BQ_LRU])); 1011 KDASSERT(checkfreelist(bp, &bufqueues[BQ_LOCKED])); 1012 1013 if ((bp->b_bufsize <= 0) || ISSET(bp->b_cflags, BC_INVAL)) { 1014 /* 1015 * If it's invalid or empty, dissociate it from its vnode 1016 * and put on the head of the appropriate queue. 1017 */ 1018 if (bioopsp != NULL) 1019 (*bioopsp->io_deallocate)(bp); 1020 1021 mutex_enter(bp->b_objlock); 1022 CLR(bp->b_oflags, BO_DONE|BO_DELWRI); 1023 if ((vp = bp->b_vp) != NULL) { 1024 KASSERT(bp->b_objlock == &vp->v_interlock); 1025 reassignbuf(bp, bp->b_vp); 1026 brelvp(bp); 1027 mutex_exit(&vp->v_interlock); 1028 } else { 1029 KASSERT(bp->b_objlock == &buffer_lock); 1030 mutex_exit(bp->b_objlock); 1031 } 1032 1033 if (bp->b_bufsize <= 0) 1034 /* no data */ 1035 goto already_queued; 1036 else 1037 /* invalid data */ 1038 bufq = &bufqueues[BQ_AGE]; 1039 binsheadfree(bp, bufq); 1040 } else { 1041 /* 1042 * It has valid data. Put it on the end of the appropriate 1043 * queue, so that it'll stick around for as long as possible. 1044 * If buf is AGE, but has dependencies, must put it on last 1045 * bufqueue to be scanned, ie LRU. This protects against the 1046 * livelock where BQ_AGE only has buffers with dependencies, 1047 * and we thus never get to the dependent buffers in BQ_LRU. 1048 */ 1049 if (ISSET(bp->b_flags, B_LOCKED)) { 1050 /* locked in core */ 1051 bufq = &bufqueues[BQ_LOCKED]; 1052 } else if (!ISSET(bp->b_cflags, BC_AGE)) { 1053 /* valid data */ 1054 bufq = &bufqueues[BQ_LRU]; 1055 } else { 1056 /* stale but valid data */ 1057 int has_deps; 1058 1059 if (bioopsp != NULL) 1060 has_deps = (*bioopsp->io_countdeps)(bp, 0); 1061 else 1062 has_deps = 0; 1063 bufq = has_deps ? &bufqueues[BQ_LRU] : 1064 &bufqueues[BQ_AGE]; 1065 } 1066 binstailfree(bp, bufq); 1067 } 1068 already_queued: 1069 /* Unlock the buffer. */ 1070 CLR(bp->b_cflags, BC_AGE|BC_BUSY|BC_NOCACHE); 1071 CLR(bp->b_flags, B_ASYNC); 1072 1073 if (bp->b_bufsize <= 0) 1074 brele(bp); 1075 } 1076 1077 void 1078 brelse(buf_t *bp, int set) 1079 { 1080 1081 mutex_enter(&bufcache_lock); 1082 brelsel(bp, set); 1083 mutex_exit(&bufcache_lock); 1084 } 1085 1086 /* 1087 * Determine if a block is in the cache. 1088 * Just look on what would be its hash chain. If it's there, return 1089 * a pointer to it, unless it's marked invalid. If it's marked invalid, 1090 * we normally don't return the buffer, unless the caller explicitly 1091 * wants us to. 1092 */ 1093 buf_t * 1094 incore(struct vnode *vp, daddr_t blkno) 1095 { 1096 buf_t *bp; 1097 1098 KASSERT(mutex_owned(&bufcache_lock)); 1099 1100 /* Search hash chain */ 1101 LIST_FOREACH(bp, BUFHASH(vp, blkno), b_hash) { 1102 if (bp->b_lblkno == blkno && bp->b_vp == vp && 1103 !ISSET(bp->b_cflags, BC_INVAL)) { 1104 KASSERT(bp->b_objlock == &vp->v_interlock); 1105 return (bp); 1106 } 1107 } 1108 1109 return (NULL); 1110 } 1111 1112 /* 1113 * Get a block of requested size that is associated with 1114 * a given vnode and block offset. If it is found in the 1115 * block cache, mark it as having been found, make it busy 1116 * and return it. Otherwise, return an empty block of the 1117 * correct size. It is up to the caller to insure that the 1118 * cached blocks be of the correct size. 1119 */ 1120 buf_t * 1121 getblk(struct vnode *vp, daddr_t blkno, int size, int slpflag, int slptimeo) 1122 { 1123 int err, preserve; 1124 buf_t *bp; 1125 1126 mutex_enter(&bufcache_lock); 1127 loop: 1128 bp = incore(vp, blkno); 1129 if (bp != NULL) { 1130 err = bbusy(bp, ((slpflag & PCATCH) != 0), slptimeo, NULL); 1131 if (err != 0) { 1132 if (err == EPASSTHROUGH) 1133 goto loop; 1134 mutex_exit(&bufcache_lock); 1135 return (NULL); 1136 } 1137 #ifdef DIAGNOSTIC 1138 if (ISSET(bp->b_oflags, BO_DONE|BO_DELWRI) && 1139 bp->b_bcount < size && vp->v_type != VBLK) 1140 panic("getblk: block size invariant failed"); 1141 #endif 1142 bremfree(bp); 1143 preserve = 1; 1144 } else { 1145 if ((bp = getnewbuf(slpflag, slptimeo, 0)) == NULL) 1146 goto loop; 1147 1148 if (incore(vp, blkno) != NULL) { 1149 /* The block has come into memory in the meantime. */ 1150 brelsel(bp, 0); 1151 goto loop; 1152 } 1153 1154 LIST_INSERT_HEAD(BUFHASH(vp, blkno), bp, b_hash); 1155 bp->b_blkno = bp->b_lblkno = bp->b_rawblkno = blkno; 1156 mutex_enter(&vp->v_interlock); 1157 bgetvp(vp, bp); 1158 mutex_exit(&vp->v_interlock); 1159 preserve = 0; 1160 } 1161 mutex_exit(&bufcache_lock); 1162 1163 /* 1164 * LFS can't track total size of B_LOCKED buffer (locked_queue_bytes) 1165 * if we re-size buffers here. 1166 */ 1167 if (ISSET(bp->b_flags, B_LOCKED)) { 1168 KASSERT(bp->b_bufsize >= size); 1169 } else { 1170 if (allocbuf(bp, size, preserve)) { 1171 mutex_enter(&bufcache_lock); 1172 LIST_REMOVE(bp, b_hash); 1173 mutex_exit(&bufcache_lock); 1174 brelse(bp, BC_INVAL); 1175 return NULL; 1176 } 1177 } 1178 BIO_SETPRIO(bp, BPRIO_DEFAULT); 1179 return (bp); 1180 } 1181 1182 /* 1183 * Get an empty, disassociated buffer of given size. 1184 */ 1185 buf_t * 1186 geteblk(int size) 1187 { 1188 buf_t *bp; 1189 int error; 1190 1191 mutex_enter(&bufcache_lock); 1192 while ((bp = getnewbuf(0, 0, 0)) == NULL) 1193 ; 1194 1195 SET(bp->b_cflags, BC_INVAL); 1196 LIST_INSERT_HEAD(&invalhash, bp, b_hash); 1197 mutex_exit(&bufcache_lock); 1198 BIO_SETPRIO(bp, BPRIO_DEFAULT); 1199 error = allocbuf(bp, size, 0); 1200 KASSERT(error == 0); 1201 return (bp); 1202 } 1203 1204 /* 1205 * Expand or contract the actual memory allocated to a buffer. 1206 * 1207 * If the buffer shrinks, data is lost, so it's up to the 1208 * caller to have written it out *first*; this routine will not 1209 * start a write. If the buffer grows, it's the callers 1210 * responsibility to fill out the buffer's additional contents. 1211 */ 1212 int 1213 allocbuf(buf_t *bp, int size, int preserve) 1214 { 1215 vsize_t oldsize, desired_size; 1216 void *addr; 1217 int delta; 1218 1219 desired_size = buf_roundsize(size); 1220 if (desired_size > MAXBSIZE) 1221 printf("allocbuf: buffer larger than MAXBSIZE requested"); 1222 1223 bp->b_bcount = size; 1224 1225 oldsize = bp->b_bufsize; 1226 if (oldsize == desired_size) 1227 return 0; 1228 1229 /* 1230 * If we want a buffer of a different size, re-allocate the 1231 * buffer's memory; copy old content only if needed. 1232 */ 1233 addr = buf_malloc(desired_size); 1234 if (addr == NULL) 1235 return ENOMEM; 1236 if (preserve) 1237 memcpy(addr, bp->b_data, MIN(oldsize,desired_size)); 1238 if (bp->b_data != NULL) 1239 buf_mrelease(bp->b_data, oldsize); 1240 bp->b_data = addr; 1241 bp->b_bufsize = desired_size; 1242 1243 /* 1244 * Update overall buffer memory counter (protected by bufcache_lock) 1245 */ 1246 delta = (long)desired_size - (long)oldsize; 1247 1248 mutex_enter(&bufcache_lock); 1249 if ((bufmem += delta) > bufmem_hiwater) { 1250 /* 1251 * Need to trim overall memory usage. 1252 */ 1253 while (buf_canrelease()) { 1254 if (curcpu()->ci_schedstate.spc_flags & 1255 SPCF_SHOULDYIELD) { 1256 mutex_exit(&bufcache_lock); 1257 preempt(); 1258 mutex_enter(&bufcache_lock); 1259 } 1260 if (buf_trim() == 0) 1261 break; 1262 } 1263 } 1264 mutex_exit(&bufcache_lock); 1265 return 0; 1266 } 1267 1268 /* 1269 * Find a buffer which is available for use. 1270 * Select something from a free list. 1271 * Preference is to AGE list, then LRU list. 1272 * 1273 * Called with the buffer queues locked. 1274 * Return buffer locked. 1275 */ 1276 buf_t * 1277 getnewbuf(int slpflag, int slptimeo, int from_bufq) 1278 { 1279 buf_t *bp; 1280 struct vnode *vp; 1281 1282 start: 1283 KASSERT(mutex_owned(&bufcache_lock)); 1284 1285 /* 1286 * Get a new buffer from the pool. 1287 */ 1288 if (!from_bufq && buf_lotsfree()) { 1289 mutex_exit(&bufcache_lock); 1290 bp = pool_cache_get(buf_cache, PR_NOWAIT); 1291 if (bp != NULL) { 1292 memset((char *)bp, 0, sizeof(*bp)); 1293 buf_init(bp); 1294 bp->b_dev = NODEV; 1295 bp->b_vnbufs.le_next = NOLIST; 1296 bp->b_cflags = BC_BUSY; 1297 bp->b_refcnt = 1; 1298 mutex_enter(&bufcache_lock); 1299 #if defined(DIAGNOSTIC) 1300 bp->b_freelistindex = -1; 1301 #endif /* defined(DIAGNOSTIC) */ 1302 return (bp); 1303 } 1304 mutex_enter(&bufcache_lock); 1305 } 1306 1307 if ((bp = TAILQ_FIRST(&bufqueues[BQ_AGE].bq_queue)) != NULL || 1308 (bp = TAILQ_FIRST(&bufqueues[BQ_LRU].bq_queue)) != NULL) { 1309 bremfree(bp); 1310 } else { 1311 /* 1312 * XXX: !from_bufq should be removed. 1313 */ 1314 if (!from_bufq || curlwp != uvm.pagedaemon_lwp) { 1315 /* wait for a free buffer of any kind */ 1316 if ((slpflag & PCATCH) != 0) 1317 (void)cv_timedwait_sig(&needbuffer_cv, 1318 &bufcache_lock, slptimeo); 1319 else 1320 (void)cv_timedwait(&needbuffer_cv, 1321 &bufcache_lock, slptimeo); 1322 } 1323 return (NULL); 1324 } 1325 1326 #ifdef DIAGNOSTIC 1327 if (bp->b_bufsize <= 0) 1328 panic("buffer %p: on queue but empty", bp); 1329 #endif 1330 1331 if (ISSET(bp->b_cflags, BC_VFLUSH)) { 1332 /* 1333 * This is a delayed write buffer being flushed to disk. Make 1334 * sure it gets aged out of the queue when it's finished, and 1335 * leave it off the LRU queue. 1336 */ 1337 CLR(bp->b_cflags, BC_VFLUSH); 1338 SET(bp->b_cflags, BC_AGE); 1339 goto start; 1340 } 1341 1342 /* Buffer is no longer on free lists. */ 1343 SET(bp->b_cflags, BC_BUSY); 1344 1345 /* 1346 * If buffer was a delayed write, start it and return NULL 1347 * (since we might sleep while starting the write). 1348 */ 1349 if (ISSET(bp->b_oflags, BO_DELWRI)) { 1350 /* 1351 * This buffer has gone through the LRU, so make sure it gets 1352 * reused ASAP. 1353 */ 1354 SET(bp->b_cflags, BC_AGE); 1355 mutex_exit(&bufcache_lock); 1356 bawrite(bp); 1357 mutex_enter(&bufcache_lock); 1358 return (NULL); 1359 } 1360 1361 vp = bp->b_vp; 1362 if (bioopsp != NULL) 1363 (*bioopsp->io_deallocate)(bp); 1364 1365 /* clear out various other fields */ 1366 bp->b_cflags = BC_BUSY; 1367 bp->b_oflags = 0; 1368 bp->b_flags = 0; 1369 bp->b_dev = NODEV; 1370 bp->b_blkno = 0; 1371 bp->b_lblkno = 0; 1372 bp->b_rawblkno = 0; 1373 bp->b_iodone = 0; 1374 bp->b_error = 0; 1375 bp->b_resid = 0; 1376 bp->b_bcount = 0; 1377 1378 LIST_REMOVE(bp, b_hash); 1379 1380 /* Disassociate us from our vnode, if we had one... */ 1381 if (vp != NULL) { 1382 mutex_enter(&vp->v_interlock); 1383 brelvp(bp); 1384 mutex_exit(&vp->v_interlock); 1385 } 1386 1387 return (bp); 1388 } 1389 1390 /* 1391 * Attempt to free an aged buffer off the queues. 1392 * Called with queue lock held. 1393 * Returns the amount of buffer memory freed. 1394 */ 1395 static int 1396 buf_trim(void) 1397 { 1398 buf_t *bp; 1399 long size = 0; 1400 1401 KASSERT(mutex_owned(&bufcache_lock)); 1402 1403 /* Instruct getnewbuf() to get buffers off the queues */ 1404 if ((bp = getnewbuf(PCATCH, 1, 1)) == NULL) 1405 return 0; 1406 1407 KASSERT((bp->b_cflags & BC_WANTED) == 0); 1408 size = bp->b_bufsize; 1409 bufmem -= size; 1410 if (size > 0) { 1411 buf_mrelease(bp->b_data, size); 1412 bp->b_bcount = bp->b_bufsize = 0; 1413 } 1414 /* brelse() will return the buffer to the global buffer pool */ 1415 brelsel(bp, 0); 1416 return size; 1417 } 1418 1419 int 1420 buf_drain(int n) 1421 { 1422 int size = 0, sz; 1423 1424 KASSERT(mutex_owned(&bufcache_lock)); 1425 1426 while (size < n && bufmem > bufmem_lowater) { 1427 sz = buf_trim(); 1428 if (sz <= 0) 1429 break; 1430 size += sz; 1431 } 1432 1433 return size; 1434 } 1435 1436 /* 1437 * Wait for operations on the buffer to complete. 1438 * When they do, extract and return the I/O's error value. 1439 */ 1440 int 1441 biowait(buf_t *bp) 1442 { 1443 1444 mutex_enter(bp->b_objlock); 1445 while (!ISSET(bp->b_oflags, BO_DONE | BO_DELWRI)) 1446 cv_wait(&bp->b_done, bp->b_objlock); 1447 mutex_exit(bp->b_objlock); 1448 1449 return bp->b_error; 1450 } 1451 1452 /* 1453 * Mark I/O complete on a buffer. 1454 * 1455 * If a callback has been requested, e.g. the pageout 1456 * daemon, do so. Otherwise, awaken waiting processes. 1457 * 1458 * [ Leffler, et al., says on p.247: 1459 * "This routine wakes up the blocked process, frees the buffer 1460 * for an asynchronous write, or, for a request by the pagedaemon 1461 * process, invokes a procedure specified in the buffer structure" ] 1462 * 1463 * In real life, the pagedaemon (or other system processes) wants 1464 * to do async stuff to, and doesn't want the buffer brelse()'d. 1465 * (for swap pager, that puts swap buffers on the free lists (!!!), 1466 * for the vn device, that puts malloc'd buffers on the free lists!) 1467 */ 1468 void 1469 biodone(buf_t *bp) 1470 { 1471 int s; 1472 1473 KASSERT(!ISSET(bp->b_oflags, BO_DONE)); 1474 1475 if (cpu_intr_p()) { 1476 /* From interrupt mode: defer to a soft interrupt. */ 1477 s = splvm(); 1478 TAILQ_INSERT_TAIL(&curcpu()->ci_data.cpu_biodone, bp, b_actq); 1479 softint_schedule(biodone_sih); 1480 splx(s); 1481 } else { 1482 /* Process now - the buffer may be freed soon. */ 1483 biodone2(bp); 1484 } 1485 } 1486 1487 static void 1488 biodone2(buf_t *bp) 1489 { 1490 void (*callout)(buf_t *); 1491 1492 if (bioopsp != NULL) 1493 (*bioopsp->io_complete)(bp); 1494 1495 mutex_enter(bp->b_objlock); 1496 /* Note that the transfer is done. */ 1497 if (ISSET(bp->b_oflags, BO_DONE)) 1498 panic("biodone2 already"); 1499 CLR(bp->b_flags, B_COWDONE); 1500 SET(bp->b_oflags, BO_DONE); 1501 BIO_SETPRIO(bp, BPRIO_DEFAULT); 1502 1503 /* Wake up waiting writers. */ 1504 if (!ISSET(bp->b_flags, B_READ)) 1505 vwakeup(bp); 1506 1507 if ((callout = bp->b_iodone) != NULL) { 1508 /* Note callout done, then call out. */ 1509 KERNEL_LOCK(1, NULL); /* XXXSMP */ 1510 bp->b_iodone = NULL; 1511 mutex_exit(bp->b_objlock); 1512 (*callout)(bp); 1513 KERNEL_UNLOCK_ONE(NULL); /* XXXSMP */ 1514 } else if (ISSET(bp->b_flags, B_ASYNC)) { 1515 /* If async, release. */ 1516 mutex_exit(bp->b_objlock); 1517 brelse(bp, 0); 1518 } else { 1519 /* Otherwise just wake up waiters in biowait(). */ 1520 cv_broadcast(&bp->b_done); 1521 mutex_exit(bp->b_objlock); 1522 } 1523 } 1524 1525 static void 1526 biointr(void *cookie) 1527 { 1528 struct cpu_info *ci; 1529 buf_t *bp; 1530 int s; 1531 1532 ci = curcpu(); 1533 1534 while (!TAILQ_EMPTY(&ci->ci_data.cpu_biodone)) { 1535 KASSERT(curcpu() == ci); 1536 1537 s = splvm(); 1538 bp = TAILQ_FIRST(&ci->ci_data.cpu_biodone); 1539 TAILQ_REMOVE(&ci->ci_data.cpu_biodone, bp, b_actq); 1540 splx(s); 1541 1542 biodone2(bp); 1543 } 1544 } 1545 1546 /* 1547 * Return a count of buffers on the "locked" queue. 1548 */ 1549 int 1550 count_lock_queue(void) 1551 { 1552 buf_t *bp; 1553 int n = 0; 1554 1555 mutex_enter(&bufcache_lock); 1556 TAILQ_FOREACH(bp, &bufqueues[BQ_LOCKED].bq_queue, b_freelist) 1557 n++; 1558 mutex_exit(&bufcache_lock); 1559 return (n); 1560 } 1561 1562 /* 1563 * Wait for all buffers to complete I/O 1564 * Return the number of "stuck" buffers. 1565 */ 1566 int 1567 buf_syncwait(void) 1568 { 1569 buf_t *bp; 1570 int iter, nbusy, nbusy_prev = 0, dcount, ihash; 1571 1572 dcount = 10000; 1573 for (iter = 0; iter < 20;) { 1574 mutex_enter(&bufcache_lock); 1575 nbusy = 0; 1576 for (ihash = 0; ihash < bufhash+1; ihash++) { 1577 LIST_FOREACH(bp, &bufhashtbl[ihash], b_hash) { 1578 if ((bp->b_cflags & (BC_BUSY|BC_INVAL)) == BC_BUSY) 1579 nbusy += ((bp->b_flags & B_READ) == 0); 1580 /* 1581 * With soft updates, some buffers that are 1582 * written will be remarked as dirty until other 1583 * buffers are written. 1584 */ 1585 if (bp->b_vp && bp->b_vp->v_mount 1586 && (bp->b_vp->v_mount->mnt_flag & MNT_SOFTDEP) 1587 && (bp->b_oflags & BO_DELWRI)) { 1588 bremfree(bp); 1589 bp->b_cflags |= BC_BUSY; 1590 nbusy++; 1591 mutex_exit(&bufcache_lock); 1592 bawrite(bp); 1593 if (dcount-- <= 0) { 1594 printf("softdep "); 1595 goto fail; 1596 } 1597 mutex_enter(&bufcache_lock); 1598 } 1599 } 1600 } 1601 mutex_exit(&bufcache_lock); 1602 1603 if (nbusy == 0) 1604 break; 1605 if (nbusy_prev == 0) 1606 nbusy_prev = nbusy; 1607 printf("%d ", nbusy); 1608 tsleep(&nbusy, PRIBIO, "bflush", 1609 (iter == 0) ? 1 : hz / 25 * iter); 1610 if (nbusy >= nbusy_prev) /* we didn't flush anything */ 1611 iter++; 1612 else 1613 nbusy_prev = nbusy; 1614 } 1615 1616 if (nbusy) { 1617 fail:; 1618 #if defined(DEBUG) || defined(DEBUG_HALT_BUSY) 1619 printf("giving up\nPrinting vnodes for busy buffers\n"); 1620 for (ihash = 0; ihash < bufhash+1; ihash++) { 1621 LIST_FOREACH(bp, &bufhashtbl[ihash], b_hash) { 1622 if ((bp->b_cflags & (BC_BUSY|BC_INVAL)) == BC_BUSY && 1623 (bp->b_flags & B_READ) == 0) 1624 vprint(NULL, bp->b_vp); 1625 } 1626 } 1627 #endif 1628 } 1629 1630 return nbusy; 1631 } 1632 1633 static void 1634 sysctl_fillbuf(buf_t *i, struct buf_sysctl *o) 1635 { 1636 1637 o->b_flags = i->b_flags | i->b_cflags | i->b_oflags; 1638 o->b_error = i->b_error; 1639 o->b_prio = i->b_prio; 1640 o->b_dev = i->b_dev; 1641 o->b_bufsize = i->b_bufsize; 1642 o->b_bcount = i->b_bcount; 1643 o->b_resid = i->b_resid; 1644 o->b_addr = PTRTOUINT64(i->b_data); 1645 o->b_blkno = i->b_blkno; 1646 o->b_rawblkno = i->b_rawblkno; 1647 o->b_iodone = PTRTOUINT64(i->b_iodone); 1648 o->b_proc = PTRTOUINT64(i->b_proc); 1649 o->b_vp = PTRTOUINT64(i->b_vp); 1650 o->b_saveaddr = PTRTOUINT64(i->b_saveaddr); 1651 o->b_lblkno = i->b_lblkno; 1652 } 1653 1654 #define KERN_BUFSLOP 20 1655 static int 1656 sysctl_dobuf(SYSCTLFN_ARGS) 1657 { 1658 buf_t *bp; 1659 struct buf_sysctl bs; 1660 struct bqueue *bq; 1661 char *dp; 1662 u_int i, op, arg; 1663 size_t len, needed, elem_size, out_size; 1664 int error, elem_count, retries; 1665 1666 if (namelen == 1 && name[0] == CTL_QUERY) 1667 return (sysctl_query(SYSCTLFN_CALL(rnode))); 1668 1669 if (namelen != 4) 1670 return (EINVAL); 1671 1672 retries = 100; 1673 retry: 1674 dp = oldp; 1675 len = (oldp != NULL) ? *oldlenp : 0; 1676 op = name[0]; 1677 arg = name[1]; 1678 elem_size = name[2]; 1679 elem_count = name[3]; 1680 out_size = MIN(sizeof(bs), elem_size); 1681 1682 /* 1683 * at the moment, these are just "placeholders" to make the 1684 * API for retrieving kern.buf data more extensible in the 1685 * future. 1686 * 1687 * XXX kern.buf currently has "netbsd32" issues. hopefully 1688 * these will be resolved at a later point. 1689 */ 1690 if (op != KERN_BUF_ALL || arg != KERN_BUF_ALL || 1691 elem_size < 1 || elem_count < 0) 1692 return (EINVAL); 1693 1694 error = 0; 1695 needed = 0; 1696 sysctl_unlock(); 1697 mutex_enter(&bufcache_lock); 1698 for (i = 0; i < BQUEUES; i++) { 1699 bq = &bufqueues[i]; 1700 TAILQ_FOREACH(bp, &bq->bq_queue, b_freelist) { 1701 bq->bq_marker = bp; 1702 if (len >= elem_size && elem_count > 0) { 1703 sysctl_fillbuf(bp, &bs); 1704 mutex_exit(&bufcache_lock); 1705 error = copyout(&bs, dp, out_size); 1706 mutex_enter(&bufcache_lock); 1707 if (error) 1708 break; 1709 if (bq->bq_marker != bp) { 1710 /* 1711 * This sysctl node is only for 1712 * statistics. Retry; if the 1713 * queue keeps changing, then 1714 * bail out. 1715 */ 1716 if (retries-- == 0) { 1717 error = EAGAIN; 1718 break; 1719 } 1720 mutex_exit(&bufcache_lock); 1721 goto retry; 1722 } 1723 dp += elem_size; 1724 len -= elem_size; 1725 } 1726 if (elem_count > 0) { 1727 needed += elem_size; 1728 if (elem_count != INT_MAX) 1729 elem_count--; 1730 } 1731 } 1732 if (error != 0) 1733 break; 1734 } 1735 mutex_exit(&bufcache_lock); 1736 sysctl_relock(); 1737 1738 *oldlenp = needed; 1739 if (oldp == NULL) 1740 *oldlenp += KERN_BUFSLOP * sizeof(buf_t); 1741 1742 return (error); 1743 } 1744 1745 static int 1746 sysctl_bufvm_update(SYSCTLFN_ARGS) 1747 { 1748 int t, error, rv; 1749 struct sysctlnode node; 1750 1751 node = *rnode; 1752 node.sysctl_data = &t; 1753 t = *(int *)rnode->sysctl_data; 1754 error = sysctl_lookup(SYSCTLFN_CALL(&node)); 1755 if (error || newp == NULL) 1756 return (error); 1757 1758 if (t < 0) 1759 return EINVAL; 1760 if (rnode->sysctl_data == &bufcache) { 1761 if (t > 100) 1762 return (EINVAL); 1763 bufcache = t; 1764 buf_setwm(); 1765 } else if (rnode->sysctl_data == &bufmem_lowater) { 1766 if (bufmem_hiwater - t < 16) 1767 return (EINVAL); 1768 bufmem_lowater = t; 1769 } else if (rnode->sysctl_data == &bufmem_hiwater) { 1770 if (t - bufmem_lowater < 16) 1771 return (EINVAL); 1772 bufmem_hiwater = t; 1773 } else 1774 return (EINVAL); 1775 1776 /* Drain until below new high water mark */ 1777 sysctl_unlock(); 1778 mutex_enter(&bufcache_lock); 1779 while ((t = bufmem - bufmem_hiwater) >= 0) { 1780 rv = buf_drain(t / (2 * 1024)); 1781 if (rv <= 0) 1782 break; 1783 } 1784 mutex_exit(&bufcache_lock); 1785 sysctl_relock(); 1786 1787 return 0; 1788 } 1789 1790 SYSCTL_SETUP(sysctl_kern_buf_setup, "sysctl kern.buf subtree setup") 1791 { 1792 1793 sysctl_createv(clog, 0, NULL, NULL, 1794 CTLFLAG_PERMANENT, 1795 CTLTYPE_NODE, "kern", NULL, 1796 NULL, 0, NULL, 0, 1797 CTL_KERN, CTL_EOL); 1798 sysctl_createv(clog, 0, NULL, NULL, 1799 CTLFLAG_PERMANENT, 1800 CTLTYPE_NODE, "buf", 1801 SYSCTL_DESCR("Kernel buffer cache information"), 1802 sysctl_dobuf, 0, NULL, 0, 1803 CTL_KERN, KERN_BUF, CTL_EOL); 1804 } 1805 1806 SYSCTL_SETUP(sysctl_vm_buf_setup, "sysctl vm.buf* subtree setup") 1807 { 1808 1809 sysctl_createv(clog, 0, NULL, NULL, 1810 CTLFLAG_PERMANENT, 1811 CTLTYPE_NODE, "vm", NULL, 1812 NULL, 0, NULL, 0, 1813 CTL_VM, CTL_EOL); 1814 1815 sysctl_createv(clog, 0, NULL, NULL, 1816 CTLFLAG_PERMANENT|CTLFLAG_READWRITE, 1817 CTLTYPE_INT, "bufcache", 1818 SYSCTL_DESCR("Percentage of physical memory to use for " 1819 "buffer cache"), 1820 sysctl_bufvm_update, 0, &bufcache, 0, 1821 CTL_VM, CTL_CREATE, CTL_EOL); 1822 sysctl_createv(clog, 0, NULL, NULL, 1823 CTLFLAG_PERMANENT|CTLFLAG_READONLY, 1824 CTLTYPE_INT, "bufmem", 1825 SYSCTL_DESCR("Amount of kernel memory used by buffer " 1826 "cache"), 1827 NULL, 0, &bufmem, 0, 1828 CTL_VM, CTL_CREATE, CTL_EOL); 1829 sysctl_createv(clog, 0, NULL, NULL, 1830 CTLFLAG_PERMANENT|CTLFLAG_READWRITE, 1831 CTLTYPE_INT, "bufmem_lowater", 1832 SYSCTL_DESCR("Minimum amount of kernel memory to " 1833 "reserve for buffer cache"), 1834 sysctl_bufvm_update, 0, &bufmem_lowater, 0, 1835 CTL_VM, CTL_CREATE, CTL_EOL); 1836 sysctl_createv(clog, 0, NULL, NULL, 1837 CTLFLAG_PERMANENT|CTLFLAG_READWRITE, 1838 CTLTYPE_INT, "bufmem_hiwater", 1839 SYSCTL_DESCR("Maximum amount of kernel memory to use " 1840 "for buffer cache"), 1841 sysctl_bufvm_update, 0, &bufmem_hiwater, 0, 1842 CTL_VM, CTL_CREATE, CTL_EOL); 1843 } 1844 1845 #ifdef DEBUG 1846 /* 1847 * Print out statistics on the current allocation of the buffer pool. 1848 * Can be enabled to print out on every ``sync'' by setting "syncprt" 1849 * in vfs_syscalls.c using sysctl. 1850 */ 1851 void 1852 vfs_bufstats(void) 1853 { 1854 int i, j, count; 1855 buf_t *bp; 1856 struct bqueue *dp; 1857 int counts[(MAXBSIZE / PAGE_SIZE) + 1]; 1858 static const char *bname[BQUEUES] = { "LOCKED", "LRU", "AGE" }; 1859 1860 for (dp = bufqueues, i = 0; dp < &bufqueues[BQUEUES]; dp++, i++) { 1861 count = 0; 1862 for (j = 0; j <= MAXBSIZE/PAGE_SIZE; j++) 1863 counts[j] = 0; 1864 TAILQ_FOREACH(bp, &dp->bq_queue, b_freelist) { 1865 counts[bp->b_bufsize/PAGE_SIZE]++; 1866 count++; 1867 } 1868 printf("%s: total-%d", bname[i], count); 1869 for (j = 0; j <= MAXBSIZE/PAGE_SIZE; j++) 1870 if (counts[j] != 0) 1871 printf(", %d-%d", j * PAGE_SIZE, counts[j]); 1872 printf("\n"); 1873 } 1874 } 1875 #endif /* DEBUG */ 1876 1877 /* ------------------------------ */ 1878 1879 buf_t * 1880 getiobuf(struct vnode *vp, bool waitok) 1881 { 1882 buf_t *bp; 1883 1884 bp = pool_cache_get(bufio_cache, (waitok ? PR_WAITOK : PR_NOWAIT)); 1885 if (bp == NULL) 1886 return bp; 1887 1888 buf_init(bp); 1889 1890 if ((bp->b_vp = vp) == NULL) 1891 bp->b_objlock = &buffer_lock; 1892 else 1893 bp->b_objlock = &vp->v_interlock; 1894 1895 return bp; 1896 } 1897 1898 void 1899 putiobuf(buf_t *bp) 1900 { 1901 1902 buf_destroy(bp); 1903 pool_cache_put(bufio_cache, bp); 1904 } 1905 1906 /* 1907 * nestiobuf_iodone: b_iodone callback for nested buffers. 1908 */ 1909 1910 void 1911 nestiobuf_iodone(buf_t *bp) 1912 { 1913 buf_t *mbp = bp->b_private; 1914 int error; 1915 int donebytes; 1916 1917 KASSERT(bp->b_bcount <= bp->b_bufsize); 1918 KASSERT(mbp != bp); 1919 1920 error = 0; 1921 if (bp->b_error == 0 && 1922 (bp->b_bcount < bp->b_bufsize || bp->b_resid > 0)) { 1923 /* 1924 * Not all got transfered, raise an error. We have no way to 1925 * propagate these conditions to mbp. 1926 */ 1927 error = EIO; 1928 } 1929 1930 donebytes = bp->b_bufsize; 1931 1932 putiobuf(bp); 1933 nestiobuf_done(mbp, donebytes, error); 1934 } 1935 1936 /* 1937 * nestiobuf_setup: setup a "nested" buffer. 1938 * 1939 * => 'mbp' is a "master" buffer which is being divided into sub pieces. 1940 * => 'bp' should be a buffer allocated by getiobuf. 1941 * => 'offset' is a byte offset in the master buffer. 1942 * => 'size' is a size in bytes of this nested buffer. 1943 */ 1944 1945 void 1946 nestiobuf_setup(buf_t *mbp, buf_t *bp, int offset, size_t size) 1947 { 1948 const int b_read = mbp->b_flags & B_READ; 1949 struct vnode *vp = mbp->b_vp; 1950 1951 KASSERT(mbp->b_bcount >= offset + size); 1952 bp->b_vp = vp; 1953 bp->b_objlock = mbp->b_objlock; 1954 bp->b_cflags = BC_BUSY; 1955 bp->b_flags = B_ASYNC | b_read; 1956 bp->b_iodone = nestiobuf_iodone; 1957 bp->b_data = (char *)mbp->b_data + offset; 1958 bp->b_resid = bp->b_bcount = size; 1959 bp->b_bufsize = bp->b_bcount; 1960 bp->b_private = mbp; 1961 BIO_COPYPRIO(bp, mbp); 1962 if (!b_read && vp != NULL) { 1963 mutex_enter(&vp->v_interlock); 1964 vp->v_numoutput++; 1965 mutex_exit(&vp->v_interlock); 1966 } 1967 } 1968 1969 /* 1970 * nestiobuf_done: propagate completion to the master buffer. 1971 * 1972 * => 'donebytes' specifies how many bytes in the 'mbp' is completed. 1973 * => 'error' is an errno(2) that 'donebytes' has been completed with. 1974 */ 1975 1976 void 1977 nestiobuf_done(buf_t *mbp, int donebytes, int error) 1978 { 1979 1980 if (donebytes == 0) { 1981 return; 1982 } 1983 mutex_enter(mbp->b_objlock); 1984 KASSERT(mbp->b_resid >= donebytes); 1985 mbp->b_resid -= donebytes; 1986 mbp->b_error = error; 1987 if (mbp->b_resid == 0) { 1988 mutex_exit(mbp->b_objlock); 1989 biodone(mbp); 1990 } else 1991 mutex_exit(mbp->b_objlock); 1992 } 1993 1994 void 1995 buf_init(buf_t *bp) 1996 { 1997 1998 LIST_INIT(&bp->b_dep); 1999 cv_init(&bp->b_busy, "biolock"); 2000 cv_init(&bp->b_done, "biowait"); 2001 bp->b_dev = NODEV; 2002 bp->b_error = 0; 2003 bp->b_flags = 0; 2004 bp->b_cflags = 0; 2005 bp->b_oflags = 0; 2006 bp->b_objlock = &buffer_lock; 2007 bp->b_iodone = NULL; 2008 BIO_SETPRIO(bp, BPRIO_DEFAULT); 2009 } 2010 2011 void 2012 buf_destroy(buf_t *bp) 2013 { 2014 2015 cv_destroy(&bp->b_done); 2016 cv_destroy(&bp->b_busy); 2017 } 2018 2019 int 2020 bbusy(buf_t *bp, bool intr, int timo, kmutex_t *interlock) 2021 { 2022 int error; 2023 2024 KASSERT(mutex_owned(&bufcache_lock)); 2025 2026 if ((bp->b_cflags & BC_BUSY) != 0) { 2027 if (curlwp == uvm.pagedaemon_lwp) 2028 return EDEADLK; 2029 bp->b_cflags |= BC_WANTED; 2030 bref(bp); 2031 if (interlock != NULL) 2032 mutex_exit(interlock); 2033 if (intr) { 2034 error = cv_timedwait_sig(&bp->b_busy, &bufcache_lock, 2035 timo); 2036 } else { 2037 error = cv_timedwait(&bp->b_busy, &bufcache_lock, 2038 timo); 2039 } 2040 brele(bp); 2041 if (interlock != NULL) 2042 mutex_enter(interlock); 2043 if (error != 0) 2044 return error; 2045 return EPASSTHROUGH; 2046 } 2047 bp->b_cflags |= BC_BUSY; 2048 2049 return 0; 2050 } 2051