vfs_bio.c revision 177472
1/*- 2 * Copyright (c) 2004 Poul-Henning Kamp 3 * Copyright (c) 1994,1997 John S. Dyson 4 * All rights reserved. 5 * 6 * Redistribution and use in source and binary forms, with or without 7 * modification, are permitted provided that the following conditions 8 * are met: 9 * 1. Redistributions of source code must retain the above copyright 10 * notice, this list of conditions and the following disclaimer. 11 * 2. Redistributions in binary form must reproduce the above copyright 12 * notice, this list of conditions and the following disclaimer in the 13 * documentation and/or other materials provided with the distribution. 14 * 15 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND 16 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 17 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 18 * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE 19 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 20 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS 21 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 22 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT 23 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY 24 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 25 * SUCH DAMAGE. 26 */ 27 28/* 29 * this file contains a new buffer I/O scheme implementing a coherent 30 * VM object and buffer cache scheme. Pains have been taken to make 31 * sure that the performance degradation associated with schemes such 32 * as this is not realized. 33 * 34 * Author: John S. Dyson 35 * Significant help during the development and debugging phases 36 * had been provided by David Greenman, also of the FreeBSD core team. 37 * 38 * see man buf(9) for more info. 39 */ 40 41#include <sys/cdefs.h> 42__FBSDID("$FreeBSD: head/sys/kern/vfs_bio.c 177472 2008-03-21 10:00:05Z jeff $"); 43 44#include <sys/param.h> 45#include <sys/systm.h> 46#include <sys/bio.h> 47#include <sys/conf.h> 48#include <sys/buf.h> 49#include <sys/devicestat.h> 50#include <sys/eventhandler.h> 51#include <sys/limits.h> 52#include <sys/lock.h> 53#include <sys/malloc.h> 54#include <sys/mount.h> 55#include <sys/mutex.h> 56#include <sys/kernel.h> 57#include <sys/kthread.h> 58#include <sys/proc.h> 59#include <sys/resourcevar.h> 60#include <sys/sysctl.h> 61#include <sys/vmmeter.h> 62#include <sys/vnode.h> 63#include <geom/geom.h> 64#include <vm/vm.h> 65#include <vm/vm_param.h> 66#include <vm/vm_kern.h> 67#include <vm/vm_pageout.h> 68#include <vm/vm_page.h> 69#include <vm/vm_object.h> 70#include <vm/vm_extern.h> 71#include <vm/vm_map.h> 72#include "opt_directio.h" 73#include "opt_swap.h" 74 75static MALLOC_DEFINE(M_BIOBUF, "biobuf", "BIO buffer"); 76 77struct bio_ops bioops; /* I/O operation notification */ 78 79struct buf_ops buf_ops_bio = { 80 .bop_name = "buf_ops_bio", 81 .bop_write = bufwrite, 82 .bop_strategy = bufstrategy, 83 .bop_sync = bufsync, 84 .bop_bdflush = bufbdflush, 85}; 86 87/* 88 * XXX buf is global because kern_shutdown.c and ffs_checkoverlap has 89 * carnal knowledge of buffers. This knowledge should be moved to vfs_bio.c. 90 */ 91struct buf *buf; /* buffer header pool */ 92 93static struct proc *bufdaemonproc; 94 95static int inmem(struct vnode *vp, daddr_t blkno); 96static void vm_hold_free_pages(struct buf *bp, vm_offset_t from, 97 vm_offset_t to); 98static void vm_hold_load_pages(struct buf *bp, vm_offset_t from, 99 vm_offset_t to); 100static void vfs_page_set_valid(struct buf *bp, vm_ooffset_t off, 101 vm_page_t m); 102static void vfs_clean_pages(struct buf *bp); 103static void vfs_setdirty(struct buf *bp); 104static void vfs_setdirty_locked_object(struct buf *bp); 105static void vfs_vmio_release(struct buf *bp); 106static int vfs_bio_clcheck(struct vnode *vp, int size, 107 daddr_t lblkno, daddr_t blkno); 108static int flushbufqueues(int, int); 109static void buf_daemon(void); 110static void bremfreel(struct buf *bp); 111 112int vmiodirenable = TRUE; 113SYSCTL_INT(_vfs, OID_AUTO, vmiodirenable, CTLFLAG_RW, &vmiodirenable, 0, 114 "Use the VM system for directory writes"); 115int runningbufspace; 116SYSCTL_INT(_vfs, OID_AUTO, runningbufspace, CTLFLAG_RD, &runningbufspace, 0, 117 "Amount of presently outstanding async buffer io"); 118static int bufspace; 119SYSCTL_INT(_vfs, OID_AUTO, bufspace, CTLFLAG_RD, &bufspace, 0, 120 "KVA memory used for bufs"); 121static int maxbufspace; 122SYSCTL_INT(_vfs, OID_AUTO, maxbufspace, CTLFLAG_RD, &maxbufspace, 0, 123 "Maximum allowed value of bufspace (including buf_daemon)"); 124static int bufmallocspace; 125SYSCTL_INT(_vfs, OID_AUTO, bufmallocspace, CTLFLAG_RD, &bufmallocspace, 0, 126 "Amount of malloced memory for buffers"); 127static int maxbufmallocspace; 128SYSCTL_INT(_vfs, OID_AUTO, maxmallocbufspace, CTLFLAG_RW, &maxbufmallocspace, 0, 129 "Maximum amount of malloced memory for buffers"); 130static int lobufspace; 131SYSCTL_INT(_vfs, OID_AUTO, lobufspace, CTLFLAG_RD, &lobufspace, 0, 132 "Minimum amount of buffers we want to have"); 133int hibufspace; 134SYSCTL_INT(_vfs, OID_AUTO, hibufspace, CTLFLAG_RD, &hibufspace, 0, 135 "Maximum allowed value of bufspace (excluding buf_daemon)"); 136static int bufreusecnt; 137SYSCTL_INT(_vfs, OID_AUTO, bufreusecnt, CTLFLAG_RW, &bufreusecnt, 0, 138 "Number of times we have reused a buffer"); 139static int buffreekvacnt; 140SYSCTL_INT(_vfs, OID_AUTO, buffreekvacnt, CTLFLAG_RW, &buffreekvacnt, 0, 141 "Number of times we have freed the KVA space from some buffer"); 142static int bufdefragcnt; 143SYSCTL_INT(_vfs, OID_AUTO, bufdefragcnt, CTLFLAG_RW, &bufdefragcnt, 0, 144 "Number of times we have had to repeat buffer allocation to defragment"); 145static int lorunningspace; 146SYSCTL_INT(_vfs, OID_AUTO, lorunningspace, CTLFLAG_RW, &lorunningspace, 0, 147 "Minimum preferred space used for in-progress I/O"); 148static int hirunningspace; 149SYSCTL_INT(_vfs, OID_AUTO, hirunningspace, CTLFLAG_RW, &hirunningspace, 0, 150 "Maximum amount of space to use for in-progress I/O"); 151int dirtybufferflushes; 152SYSCTL_INT(_vfs, OID_AUTO, dirtybufferflushes, CTLFLAG_RW, &dirtybufferflushes, 153 0, "Number of bdwrite to bawrite conversions to limit dirty buffers"); 154int bdwriteskip; 155SYSCTL_INT(_vfs, OID_AUTO, bdwriteskip, CTLFLAG_RW, &bdwriteskip, 156 0, "Number of buffers supplied to bdwrite with snapshot deadlock risk"); 157int altbufferflushes; 158SYSCTL_INT(_vfs, OID_AUTO, altbufferflushes, CTLFLAG_RW, &altbufferflushes, 159 0, "Number of fsync flushes to limit dirty buffers"); 160static int recursiveflushes; 161SYSCTL_INT(_vfs, OID_AUTO, recursiveflushes, CTLFLAG_RW, &recursiveflushes, 162 0, "Number of flushes skipped due to being recursive"); 163static int numdirtybuffers; 164SYSCTL_INT(_vfs, OID_AUTO, numdirtybuffers, CTLFLAG_RD, &numdirtybuffers, 0, 165 "Number of buffers that are dirty (has unwritten changes) at the moment"); 166static int lodirtybuffers; 167SYSCTL_INT(_vfs, OID_AUTO, lodirtybuffers, CTLFLAG_RW, &lodirtybuffers, 0, 168 "How many buffers we want to have free before bufdaemon can sleep"); 169static int hidirtybuffers; 170SYSCTL_INT(_vfs, OID_AUTO, hidirtybuffers, CTLFLAG_RW, &hidirtybuffers, 0, 171 "When the number of dirty buffers is considered severe"); 172int dirtybufthresh; 173SYSCTL_INT(_vfs, OID_AUTO, dirtybufthresh, CTLFLAG_RW, &dirtybufthresh, 174 0, "Number of bdwrite to bawrite conversions to clear dirty buffers"); 175static int numfreebuffers; 176SYSCTL_INT(_vfs, OID_AUTO, numfreebuffers, CTLFLAG_RD, &numfreebuffers, 0, 177 "Number of free buffers"); 178static int lofreebuffers; 179SYSCTL_INT(_vfs, OID_AUTO, lofreebuffers, CTLFLAG_RW, &lofreebuffers, 0, 180 "XXX Unused"); 181static int hifreebuffers; 182SYSCTL_INT(_vfs, OID_AUTO, hifreebuffers, CTLFLAG_RW, &hifreebuffers, 0, 183 "XXX Complicatedly unused"); 184static int getnewbufcalls; 185SYSCTL_INT(_vfs, OID_AUTO, getnewbufcalls, CTLFLAG_RW, &getnewbufcalls, 0, 186 "Number of calls to getnewbuf"); 187static int getnewbufrestarts; 188SYSCTL_INT(_vfs, OID_AUTO, getnewbufrestarts, CTLFLAG_RW, &getnewbufrestarts, 0, 189 "Number of times getnewbuf has had to restart a buffer aquisition"); 190 191/* 192 * Wakeup point for bufdaemon, as well as indicator of whether it is already 193 * active. Set to 1 when the bufdaemon is already "on" the queue, 0 when it 194 * is idling. 195 */ 196static int bd_request; 197 198/* 199 * This lock synchronizes access to bd_request. 200 */ 201static struct mtx bdlock; 202 203/* 204 * bogus page -- for I/O to/from partially complete buffers 205 * this is a temporary solution to the problem, but it is not 206 * really that bad. it would be better to split the buffer 207 * for input in the case of buffers partially already in memory, 208 * but the code is intricate enough already. 209 */ 210vm_page_t bogus_page; 211 212/* 213 * Synchronization (sleep/wakeup) variable for active buffer space requests. 214 * Set when wait starts, cleared prior to wakeup(). 215 * Used in runningbufwakeup() and waitrunningbufspace(). 216 */ 217static int runningbufreq; 218 219/* 220 * This lock protects the runningbufreq and synchronizes runningbufwakeup and 221 * waitrunningbufspace(). 222 */ 223static struct mtx rbreqlock; 224 225/* 226 * Synchronization (sleep/wakeup) variable for buffer requests. 227 * Can contain the VFS_BIO_NEED flags defined below; setting/clearing is done 228 * by and/or. 229 * Used in numdirtywakeup(), bufspacewakeup(), bufcountwakeup(), bwillwrite(), 230 * getnewbuf(), and getblk(). 231 */ 232static int needsbuffer; 233 234/* 235 * Lock that protects needsbuffer and the sleeps/wakeups surrounding it. 236 */ 237static struct mtx nblock; 238 239/* 240 * Definitions for the buffer free lists. 241 */ 242#define BUFFER_QUEUES 6 /* number of free buffer queues */ 243 244#define QUEUE_NONE 0 /* on no queue */ 245#define QUEUE_CLEAN 1 /* non-B_DELWRI buffers */ 246#define QUEUE_DIRTY 2 /* B_DELWRI buffers */ 247#define QUEUE_DIRTY_GIANT 3 /* B_DELWRI buffers that need giant */ 248#define QUEUE_EMPTYKVA 4 /* empty buffer headers w/KVA assignment */ 249#define QUEUE_EMPTY 5 /* empty buffer headers */ 250 251/* Queues for free buffers with various properties */ 252static TAILQ_HEAD(bqueues, buf) bufqueues[BUFFER_QUEUES] = { { 0 } }; 253 254/* Lock for the bufqueues */ 255static struct mtx bqlock; 256 257/* 258 * Single global constant for BUF_WMESG, to avoid getting multiple references. 259 * buf_wmesg is referred from macros. 260 */ 261const char *buf_wmesg = BUF_WMESG; 262 263#define VFS_BIO_NEED_ANY 0x01 /* any freeable buffer */ 264#define VFS_BIO_NEED_DIRTYFLUSH 0x02 /* waiting for dirty buffer flush */ 265#define VFS_BIO_NEED_FREE 0x04 /* wait for free bufs, hi hysteresis */ 266#define VFS_BIO_NEED_BUFSPACE 0x08 /* wait for buf space, lo hysteresis */ 267 268#ifdef DIRECTIO 269extern void ffs_rawread_setup(void); 270#endif /* DIRECTIO */ 271/* 272 * numdirtywakeup: 273 * 274 * If someone is blocked due to there being too many dirty buffers, 275 * and numdirtybuffers is now reasonable, wake them up. 276 */ 277 278static __inline void 279numdirtywakeup(int level) 280{ 281 282 if (numdirtybuffers <= level) { 283 mtx_lock(&nblock); 284 if (needsbuffer & VFS_BIO_NEED_DIRTYFLUSH) { 285 needsbuffer &= ~VFS_BIO_NEED_DIRTYFLUSH; 286 wakeup(&needsbuffer); 287 } 288 mtx_unlock(&nblock); 289 } 290} 291 292/* 293 * bufspacewakeup: 294 * 295 * Called when buffer space is potentially available for recovery. 296 * getnewbuf() will block on this flag when it is unable to free 297 * sufficient buffer space. Buffer space becomes recoverable when 298 * bp's get placed back in the queues. 299 */ 300 301static __inline void 302bufspacewakeup(void) 303{ 304 305 /* 306 * If someone is waiting for BUF space, wake them up. Even 307 * though we haven't freed the kva space yet, the waiting 308 * process will be able to now. 309 */ 310 mtx_lock(&nblock); 311 if (needsbuffer & VFS_BIO_NEED_BUFSPACE) { 312 needsbuffer &= ~VFS_BIO_NEED_BUFSPACE; 313 wakeup(&needsbuffer); 314 } 315 mtx_unlock(&nblock); 316} 317 318/* 319 * runningbufwakeup() - in-progress I/O accounting. 320 * 321 */ 322void 323runningbufwakeup(struct buf *bp) 324{ 325 326 if (bp->b_runningbufspace) { 327 atomic_subtract_int(&runningbufspace, bp->b_runningbufspace); 328 bp->b_runningbufspace = 0; 329 mtx_lock(&rbreqlock); 330 if (runningbufreq && runningbufspace <= lorunningspace) { 331 runningbufreq = 0; 332 wakeup(&runningbufreq); 333 } 334 mtx_unlock(&rbreqlock); 335 } 336} 337 338/* 339 * bufcountwakeup: 340 * 341 * Called when a buffer has been added to one of the free queues to 342 * account for the buffer and to wakeup anyone waiting for free buffers. 343 * This typically occurs when large amounts of metadata are being handled 344 * by the buffer cache ( else buffer space runs out first, usually ). 345 */ 346 347static __inline void 348bufcountwakeup(void) 349{ 350 351 atomic_add_int(&numfreebuffers, 1); 352 mtx_lock(&nblock); 353 if (needsbuffer) { 354 needsbuffer &= ~VFS_BIO_NEED_ANY; 355 if (numfreebuffers >= hifreebuffers) 356 needsbuffer &= ~VFS_BIO_NEED_FREE; 357 wakeup(&needsbuffer); 358 } 359 mtx_unlock(&nblock); 360} 361 362/* 363 * waitrunningbufspace() 364 * 365 * runningbufspace is a measure of the amount of I/O currently 366 * running. This routine is used in async-write situations to 367 * prevent creating huge backups of pending writes to a device. 368 * Only asynchronous writes are governed by this function. 369 * 370 * Reads will adjust runningbufspace, but will not block based on it. 371 * The read load has a side effect of reducing the allowed write load. 372 * 373 * This does NOT turn an async write into a sync write. It waits 374 * for earlier writes to complete and generally returns before the 375 * caller's write has reached the device. 376 */ 377void 378waitrunningbufspace(void) 379{ 380 381 mtx_lock(&rbreqlock); 382 while (runningbufspace > hirunningspace) { 383 ++runningbufreq; 384 msleep(&runningbufreq, &rbreqlock, PVM, "wdrain", 0); 385 } 386 mtx_unlock(&rbreqlock); 387} 388 389 390/* 391 * vfs_buf_test_cache: 392 * 393 * Called when a buffer is extended. This function clears the B_CACHE 394 * bit if the newly extended portion of the buffer does not contain 395 * valid data. 396 */ 397static __inline 398void 399vfs_buf_test_cache(struct buf *bp, 400 vm_ooffset_t foff, vm_offset_t off, vm_offset_t size, 401 vm_page_t m) 402{ 403 404 VM_OBJECT_LOCK_ASSERT(m->object, MA_OWNED); 405 if (bp->b_flags & B_CACHE) { 406 int base = (foff + off) & PAGE_MASK; 407 if (vm_page_is_valid(m, base, size) == 0) 408 bp->b_flags &= ~B_CACHE; 409 } 410} 411 412/* Wake up the buffer daemon if necessary */ 413static __inline 414void 415bd_wakeup(int dirtybuflevel) 416{ 417 418 mtx_lock(&bdlock); 419 if (bd_request == 0 && numdirtybuffers >= dirtybuflevel) { 420 bd_request = 1; 421 wakeup(&bd_request); 422 } 423 mtx_unlock(&bdlock); 424} 425 426/* 427 * bd_speedup - speedup the buffer cache flushing code 428 */ 429 430static __inline 431void 432bd_speedup(void) 433{ 434 435 bd_wakeup(1); 436} 437 438/* 439 * Calculating buffer cache scaling values and reserve space for buffer 440 * headers. This is called during low level kernel initialization and 441 * may be called more then once. We CANNOT write to the memory area 442 * being reserved at this time. 443 */ 444caddr_t 445kern_vfs_bio_buffer_alloc(caddr_t v, long physmem_est) 446{ 447 int maxbuf; 448 449 /* 450 * physmem_est is in pages. Convert it to kilobytes (assumes 451 * PAGE_SIZE is >= 1K) 452 */ 453 physmem_est = physmem_est * (PAGE_SIZE / 1024); 454 455 /* 456 * The nominal buffer size (and minimum KVA allocation) is BKVASIZE. 457 * For the first 64MB of ram nominally allocate sufficient buffers to 458 * cover 1/4 of our ram. Beyond the first 64MB allocate additional 459 * buffers to cover 1/10 of our ram over 64MB. When auto-sizing 460 * the buffer cache we limit the eventual kva reservation to 461 * maxbcache bytes. 462 * 463 * factor represents the 1/4 x ram conversion. 464 */ 465 if (nbuf == 0) { 466 int factor = 4 * BKVASIZE / 1024; 467 468 nbuf = 50; 469 if (physmem_est > 4096) 470 nbuf += min((physmem_est - 4096) / factor, 471 65536 / factor); 472 if (physmem_est > 65536) 473 nbuf += (physmem_est - 65536) * 2 / (factor * 5); 474 475 if (maxbcache && nbuf > maxbcache / BKVASIZE) 476 nbuf = maxbcache / BKVASIZE; 477 478 /* XXX Avoid integer overflows later on with maxbufspace. */ 479 maxbuf = (INT_MAX / 3) / BKVASIZE; 480 if (nbuf > maxbuf) 481 nbuf = maxbuf; 482 } 483 484#if 0 485 /* 486 * Do not allow the buffer_map to be more then 1/2 the size of the 487 * kernel_map. 488 */ 489 if (nbuf > (kernel_map->max_offset - kernel_map->min_offset) / 490 (BKVASIZE * 2)) { 491 nbuf = (kernel_map->max_offset - kernel_map->min_offset) / 492 (BKVASIZE * 2); 493 printf("Warning: nbufs capped at %d\n", nbuf); 494 } 495#endif 496 497 /* 498 * swbufs are used as temporary holders for I/O, such as paging I/O. 499 * We have no less then 16 and no more then 256. 500 */ 501 nswbuf = max(min(nbuf/4, 256), 16); 502#ifdef NSWBUF_MIN 503 if (nswbuf < NSWBUF_MIN) 504 nswbuf = NSWBUF_MIN; 505#endif 506#ifdef DIRECTIO 507 ffs_rawread_setup(); 508#endif 509 510 /* 511 * Reserve space for the buffer cache buffers 512 */ 513 swbuf = (void *)v; 514 v = (caddr_t)(swbuf + nswbuf); 515 buf = (void *)v; 516 v = (caddr_t)(buf + nbuf); 517 518 return(v); 519} 520 521/* Initialize the buffer subsystem. Called before use of any buffers. */ 522void 523bufinit(void) 524{ 525 struct buf *bp; 526 int i; 527 528 mtx_init(&bqlock, "buf queue lock", NULL, MTX_DEF); 529 mtx_init(&rbreqlock, "runningbufspace lock", NULL, MTX_DEF); 530 mtx_init(&nblock, "needsbuffer lock", NULL, MTX_DEF); 531 mtx_init(&bdlock, "buffer daemon lock", NULL, MTX_DEF); 532 533 /* next, make a null set of free lists */ 534 for (i = 0; i < BUFFER_QUEUES; i++) 535 TAILQ_INIT(&bufqueues[i]); 536 537 /* finally, initialize each buffer header and stick on empty q */ 538 for (i = 0; i < nbuf; i++) { 539 bp = &buf[i]; 540 bzero(bp, sizeof *bp); 541 bp->b_flags = B_INVAL; /* we're just an empty header */ 542 bp->b_rcred = NOCRED; 543 bp->b_wcred = NOCRED; 544 bp->b_qindex = QUEUE_EMPTY; 545 bp->b_vflags = 0; 546 bp->b_xflags = 0; 547 bp->b_waiters = 0; 548 LIST_INIT(&bp->b_dep); 549 BUF_LOCKINIT(bp); 550 TAILQ_INSERT_TAIL(&bufqueues[QUEUE_EMPTY], bp, b_freelist); 551 } 552 553 /* 554 * maxbufspace is the absolute maximum amount of buffer space we are 555 * allowed to reserve in KVM and in real terms. The absolute maximum 556 * is nominally used by buf_daemon. hibufspace is the nominal maximum 557 * used by most other processes. The differential is required to 558 * ensure that buf_daemon is able to run when other processes might 559 * be blocked waiting for buffer space. 560 * 561 * maxbufspace is based on BKVASIZE. Allocating buffers larger then 562 * this may result in KVM fragmentation which is not handled optimally 563 * by the system. 564 */ 565 maxbufspace = nbuf * BKVASIZE; 566 hibufspace = imax(3 * maxbufspace / 4, maxbufspace - MAXBSIZE * 10); 567 lobufspace = hibufspace - MAXBSIZE; 568 569 lorunningspace = 512 * 1024; 570 hirunningspace = 1024 * 1024; 571 572/* 573 * Limit the amount of malloc memory since it is wired permanently into 574 * the kernel space. Even though this is accounted for in the buffer 575 * allocation, we don't want the malloced region to grow uncontrolled. 576 * The malloc scheme improves memory utilization significantly on average 577 * (small) directories. 578 */ 579 maxbufmallocspace = hibufspace / 20; 580 581/* 582 * Reduce the chance of a deadlock occuring by limiting the number 583 * of delayed-write dirty buffers we allow to stack up. 584 */ 585 hidirtybuffers = nbuf / 4 + 20; 586 dirtybufthresh = hidirtybuffers * 9 / 10; 587 numdirtybuffers = 0; 588/* 589 * To support extreme low-memory systems, make sure hidirtybuffers cannot 590 * eat up all available buffer space. This occurs when our minimum cannot 591 * be met. We try to size hidirtybuffers to 3/4 our buffer space assuming 592 * BKVASIZE'd (8K) buffers. 593 */ 594 while (hidirtybuffers * BKVASIZE > 3 * hibufspace / 4) { 595 hidirtybuffers >>= 1; 596 } 597 lodirtybuffers = hidirtybuffers / 2; 598 599/* 600 * Try to keep the number of free buffers in the specified range, 601 * and give special processes (e.g. like buf_daemon) access to an 602 * emergency reserve. 603 */ 604 lofreebuffers = nbuf / 18 + 5; 605 hifreebuffers = 2 * lofreebuffers; 606 numfreebuffers = nbuf; 607 608/* 609 * Maximum number of async ops initiated per buf_daemon loop. This is 610 * somewhat of a hack at the moment, we really need to limit ourselves 611 * based on the number of bytes of I/O in-transit that were initiated 612 * from buf_daemon. 613 */ 614 615 bogus_page = vm_page_alloc(NULL, 0, VM_ALLOC_NOOBJ | 616 VM_ALLOC_NORMAL | VM_ALLOC_WIRED); 617} 618 619/* 620 * bfreekva() - free the kva allocation for a buffer. 621 * 622 * Since this call frees up buffer space, we call bufspacewakeup(). 623 */ 624static void 625bfreekva(struct buf *bp) 626{ 627 628 if (bp->b_kvasize) { 629 atomic_add_int(&buffreekvacnt, 1); 630 atomic_subtract_int(&bufspace, bp->b_kvasize); 631 vm_map_remove(buffer_map, (vm_offset_t) bp->b_kvabase, 632 (vm_offset_t) bp->b_kvabase + bp->b_kvasize); 633 bp->b_kvasize = 0; 634 bufspacewakeup(); 635 } 636} 637 638/* 639 * bremfree: 640 * 641 * Mark the buffer for removal from the appropriate free list in brelse. 642 * 643 */ 644void 645bremfree(struct buf *bp) 646{ 647 648 CTR3(KTR_BUF, "bremfree(%p) vp %p flags %X", bp, bp->b_vp, bp->b_flags); 649 KASSERT((bp->b_flags & B_REMFREE) == 0, 650 ("bremfree: buffer %p already marked for delayed removal.", bp)); 651 KASSERT(bp->b_qindex != QUEUE_NONE, 652 ("bremfree: buffer %p not on a queue.", bp)); 653 BUF_ASSERT_HELD(bp); 654 655 bp->b_flags |= B_REMFREE; 656 /* Fixup numfreebuffers count. */ 657 if ((bp->b_flags & B_INVAL) || (bp->b_flags & B_DELWRI) == 0) 658 atomic_subtract_int(&numfreebuffers, 1); 659} 660 661/* 662 * bremfreef: 663 * 664 * Force an immediate removal from a free list. Used only in nfs when 665 * it abuses the b_freelist pointer. 666 */ 667void 668bremfreef(struct buf *bp) 669{ 670 mtx_lock(&bqlock); 671 bremfreel(bp); 672 mtx_unlock(&bqlock); 673} 674 675/* 676 * bremfreel: 677 * 678 * Removes a buffer from the free list, must be called with the 679 * bqlock held. 680 */ 681static void 682bremfreel(struct buf *bp) 683{ 684 CTR3(KTR_BUF, "bremfreel(%p) vp %p flags %X", 685 bp, bp->b_vp, bp->b_flags); 686 KASSERT(bp->b_qindex != QUEUE_NONE, 687 ("bremfreel: buffer %p not on a queue.", bp)); 688 BUF_ASSERT_HELD(bp); 689 mtx_assert(&bqlock, MA_OWNED); 690 691 TAILQ_REMOVE(&bufqueues[bp->b_qindex], bp, b_freelist); 692 bp->b_qindex = QUEUE_NONE; 693 /* 694 * If this was a delayed bremfree() we only need to remove the buffer 695 * from the queue and return the stats are already done. 696 */ 697 if (bp->b_flags & B_REMFREE) { 698 bp->b_flags &= ~B_REMFREE; 699 return; 700 } 701 /* 702 * Fixup numfreebuffers count. If the buffer is invalid or not 703 * delayed-write, the buffer was free and we must decrement 704 * numfreebuffers. 705 */ 706 if ((bp->b_flags & B_INVAL) || (bp->b_flags & B_DELWRI) == 0) 707 atomic_subtract_int(&numfreebuffers, 1); 708} 709 710 711/* 712 * Get a buffer with the specified data. Look in the cache first. We 713 * must clear BIO_ERROR and B_INVAL prior to initiating I/O. If B_CACHE 714 * is set, the buffer is valid and we do not have to do anything ( see 715 * getblk() ). This is really just a special case of breadn(). 716 */ 717int 718bread(struct vnode * vp, daddr_t blkno, int size, struct ucred * cred, 719 struct buf **bpp) 720{ 721 722 return (breadn(vp, blkno, size, 0, 0, 0, cred, bpp)); 723} 724 725/* 726 * Attempt to initiate asynchronous I/O on read-ahead blocks. We must 727 * clear BIO_ERROR and B_INVAL prior to initiating I/O . If B_CACHE is set, 728 * the buffer is valid and we do not have to do anything. 729 */ 730void 731breada(struct vnode * vp, daddr_t * rablkno, int * rabsize, 732 int cnt, struct ucred * cred) 733{ 734 struct buf *rabp; 735 int i; 736 737 for (i = 0; i < cnt; i++, rablkno++, rabsize++) { 738 if (inmem(vp, *rablkno)) 739 continue; 740 rabp = getblk(vp, *rablkno, *rabsize, 0, 0, 0); 741 742 if ((rabp->b_flags & B_CACHE) == 0) { 743 if (!TD_IS_IDLETHREAD(curthread)) 744 curthread->td_ru.ru_inblock++; 745 rabp->b_flags |= B_ASYNC; 746 rabp->b_flags &= ~B_INVAL; 747 rabp->b_ioflags &= ~BIO_ERROR; 748 rabp->b_iocmd = BIO_READ; 749 if (rabp->b_rcred == NOCRED && cred != NOCRED) 750 rabp->b_rcred = crhold(cred); 751 vfs_busy_pages(rabp, 0); 752 BUF_KERNPROC(rabp); 753 rabp->b_iooffset = dbtob(rabp->b_blkno); 754 bstrategy(rabp); 755 } else { 756 brelse(rabp); 757 } 758 } 759} 760 761/* 762 * Operates like bread, but also starts asynchronous I/O on 763 * read-ahead blocks. 764 */ 765int 766breadn(struct vnode * vp, daddr_t blkno, int size, 767 daddr_t * rablkno, int *rabsize, 768 int cnt, struct ucred * cred, struct buf **bpp) 769{ 770 struct buf *bp; 771 int rv = 0, readwait = 0; 772 773 CTR3(KTR_BUF, "breadn(%p, %jd, %d)", vp, blkno, size); 774 *bpp = bp = getblk(vp, blkno, size, 0, 0, 0); 775 776 /* if not found in cache, do some I/O */ 777 if ((bp->b_flags & B_CACHE) == 0) { 778 if (!TD_IS_IDLETHREAD(curthread)) 779 curthread->td_ru.ru_inblock++; 780 bp->b_iocmd = BIO_READ; 781 bp->b_flags &= ~B_INVAL; 782 bp->b_ioflags &= ~BIO_ERROR; 783 if (bp->b_rcred == NOCRED && cred != NOCRED) 784 bp->b_rcred = crhold(cred); 785 vfs_busy_pages(bp, 0); 786 bp->b_iooffset = dbtob(bp->b_blkno); 787 bstrategy(bp); 788 ++readwait; 789 } 790 791 breada(vp, rablkno, rabsize, cnt, cred); 792 793 if (readwait) { 794 rv = bufwait(bp); 795 } 796 return (rv); 797} 798 799/* 800 * Write, release buffer on completion. (Done by iodone 801 * if async). Do not bother writing anything if the buffer 802 * is invalid. 803 * 804 * Note that we set B_CACHE here, indicating that buffer is 805 * fully valid and thus cacheable. This is true even of NFS 806 * now so we set it generally. This could be set either here 807 * or in biodone() since the I/O is synchronous. We put it 808 * here. 809 */ 810int 811bufwrite(struct buf *bp) 812{ 813 int oldflags; 814 struct vnode *vp; 815 int vp_md; 816 817 CTR3(KTR_BUF, "bufwrite(%p) vp %p flags %X", bp, bp->b_vp, bp->b_flags); 818 if (bp->b_flags & B_INVAL) { 819 brelse(bp); 820 return (0); 821 } 822 823 oldflags = bp->b_flags; 824 825 BUF_ASSERT_HELD(bp); 826 827 if (bp->b_pin_count > 0) 828 bunpin_wait(bp); 829 830 KASSERT(!(bp->b_vflags & BV_BKGRDINPROG), 831 ("FFS background buffer should not get here %p", bp)); 832 833 vp = bp->b_vp; 834 if (vp) 835 vp_md = vp->v_vflag & VV_MD; 836 else 837 vp_md = 0; 838 839 /* Mark the buffer clean */ 840 bundirty(bp); 841 842 bp->b_flags &= ~B_DONE; 843 bp->b_ioflags &= ~BIO_ERROR; 844 bp->b_flags |= B_CACHE; 845 bp->b_iocmd = BIO_WRITE; 846 847 bufobj_wref(bp->b_bufobj); 848 vfs_busy_pages(bp, 1); 849 850 /* 851 * Normal bwrites pipeline writes 852 */ 853 bp->b_runningbufspace = bp->b_bufsize; 854 atomic_add_int(&runningbufspace, bp->b_runningbufspace); 855 856 if (!TD_IS_IDLETHREAD(curthread)) 857 curthread->td_ru.ru_oublock++; 858 if (oldflags & B_ASYNC) 859 BUF_KERNPROC(bp); 860 bp->b_iooffset = dbtob(bp->b_blkno); 861 bstrategy(bp); 862 863 if ((oldflags & B_ASYNC) == 0) { 864 int rtval = bufwait(bp); 865 brelse(bp); 866 return (rtval); 867 } else { 868 /* 869 * don't allow the async write to saturate the I/O 870 * system. We will not deadlock here because 871 * we are blocking waiting for I/O that is already in-progress 872 * to complete. We do not block here if it is the update 873 * or syncer daemon trying to clean up as that can lead 874 * to deadlock. 875 */ 876 if ((curthread->td_pflags & TDP_NORUNNINGBUF) == 0 && !vp_md) 877 waitrunningbufspace(); 878 } 879 880 return (0); 881} 882 883void 884bufbdflush(struct bufobj *bo, struct buf *bp) 885{ 886 struct buf *nbp; 887 888 if (bo->bo_dirty.bv_cnt > dirtybufthresh + 10) { 889 (void) VOP_FSYNC(bp->b_vp, MNT_NOWAIT, curthread); 890 altbufferflushes++; 891 } else if (bo->bo_dirty.bv_cnt > dirtybufthresh) { 892 BO_LOCK(bo); 893 /* 894 * Try to find a buffer to flush. 895 */ 896 TAILQ_FOREACH(nbp, &bo->bo_dirty.bv_hd, b_bobufs) { 897 if ((nbp->b_vflags & BV_BKGRDINPROG) || 898 BUF_LOCK(nbp, 899 LK_EXCLUSIVE | LK_NOWAIT, NULL)) 900 continue; 901 if (bp == nbp) 902 panic("bdwrite: found ourselves"); 903 BO_UNLOCK(bo); 904 /* Don't countdeps with the bo lock held. */ 905 if (buf_countdeps(nbp, 0)) { 906 BO_LOCK(bo); 907 BUF_UNLOCK(nbp); 908 continue; 909 } 910 if (nbp->b_flags & B_CLUSTEROK) { 911 vfs_bio_awrite(nbp); 912 } else { 913 bremfree(nbp); 914 bawrite(nbp); 915 } 916 dirtybufferflushes++; 917 break; 918 } 919 if (nbp == NULL) 920 BO_UNLOCK(bo); 921 } 922} 923 924/* 925 * Delayed write. (Buffer is marked dirty). Do not bother writing 926 * anything if the buffer is marked invalid. 927 * 928 * Note that since the buffer must be completely valid, we can safely 929 * set B_CACHE. In fact, we have to set B_CACHE here rather then in 930 * biodone() in order to prevent getblk from writing the buffer 931 * out synchronously. 932 */ 933void 934bdwrite(struct buf *bp) 935{ 936 struct thread *td = curthread; 937 struct vnode *vp; 938 struct bufobj *bo; 939 940 CTR3(KTR_BUF, "bdwrite(%p) vp %p flags %X", bp, bp->b_vp, bp->b_flags); 941 KASSERT(bp->b_bufobj != NULL, ("No b_bufobj %p", bp)); 942 BUF_ASSERT_HELD(bp); 943 944 if (bp->b_flags & B_INVAL) { 945 brelse(bp); 946 return; 947 } 948 949 /* 950 * If we have too many dirty buffers, don't create any more. 951 * If we are wildly over our limit, then force a complete 952 * cleanup. Otherwise, just keep the situation from getting 953 * out of control. Note that we have to avoid a recursive 954 * disaster and not try to clean up after our own cleanup! 955 */ 956 vp = bp->b_vp; 957 bo = bp->b_bufobj; 958 if ((td->td_pflags & (TDP_COWINPROGRESS|TDP_INBDFLUSH)) == 0) { 959 td->td_pflags |= TDP_INBDFLUSH; 960 BO_BDFLUSH(bo, bp); 961 td->td_pflags &= ~TDP_INBDFLUSH; 962 } else 963 recursiveflushes++; 964 965 bdirty(bp); 966 /* 967 * Set B_CACHE, indicating that the buffer is fully valid. This is 968 * true even of NFS now. 969 */ 970 bp->b_flags |= B_CACHE; 971 972 /* 973 * This bmap keeps the system from needing to do the bmap later, 974 * perhaps when the system is attempting to do a sync. Since it 975 * is likely that the indirect block -- or whatever other datastructure 976 * that the filesystem needs is still in memory now, it is a good 977 * thing to do this. Note also, that if the pageout daemon is 978 * requesting a sync -- there might not be enough memory to do 979 * the bmap then... So, this is important to do. 980 */ 981 if (vp->v_type != VCHR && bp->b_lblkno == bp->b_blkno) { 982 VOP_BMAP(vp, bp->b_lblkno, NULL, &bp->b_blkno, NULL, NULL); 983 } 984 985 /* 986 * Set the *dirty* buffer range based upon the VM system dirty pages. 987 */ 988 vfs_setdirty(bp); 989 990 /* 991 * We need to do this here to satisfy the vnode_pager and the 992 * pageout daemon, so that it thinks that the pages have been 993 * "cleaned". Note that since the pages are in a delayed write 994 * buffer -- the VFS layer "will" see that the pages get written 995 * out on the next sync, or perhaps the cluster will be completed. 996 */ 997 vfs_clean_pages(bp); 998 bqrelse(bp); 999 1000 /* 1001 * Wakeup the buffer flushing daemon if we have a lot of dirty 1002 * buffers (midpoint between our recovery point and our stall 1003 * point). 1004 */ 1005 bd_wakeup((lodirtybuffers + hidirtybuffers) / 2); 1006 1007 /* 1008 * note: we cannot initiate I/O from a bdwrite even if we wanted to, 1009 * due to the softdep code. 1010 */ 1011} 1012 1013/* 1014 * bdirty: 1015 * 1016 * Turn buffer into delayed write request. We must clear BIO_READ and 1017 * B_RELBUF, and we must set B_DELWRI. We reassign the buffer to 1018 * itself to properly update it in the dirty/clean lists. We mark it 1019 * B_DONE to ensure that any asynchronization of the buffer properly 1020 * clears B_DONE ( else a panic will occur later ). 1021 * 1022 * bdirty() is kinda like bdwrite() - we have to clear B_INVAL which 1023 * might have been set pre-getblk(). Unlike bwrite/bdwrite, bdirty() 1024 * should only be called if the buffer is known-good. 1025 * 1026 * Since the buffer is not on a queue, we do not update the numfreebuffers 1027 * count. 1028 * 1029 * The buffer must be on QUEUE_NONE. 1030 */ 1031void 1032bdirty(struct buf *bp) 1033{ 1034 1035 CTR3(KTR_BUF, "bdirty(%p) vp %p flags %X", 1036 bp, bp->b_vp, bp->b_flags); 1037 KASSERT(bp->b_bufobj != NULL, ("No b_bufobj %p", bp)); 1038 KASSERT(bp->b_flags & B_REMFREE || bp->b_qindex == QUEUE_NONE, 1039 ("bdirty: buffer %p still on queue %d", bp, bp->b_qindex)); 1040 BUF_ASSERT_HELD(bp); 1041 bp->b_flags &= ~(B_RELBUF); 1042 bp->b_iocmd = BIO_WRITE; 1043 1044 if ((bp->b_flags & B_DELWRI) == 0) { 1045 bp->b_flags |= /* XXX B_DONE | */ B_DELWRI; 1046 reassignbuf(bp); 1047 atomic_add_int(&numdirtybuffers, 1); 1048 bd_wakeup((lodirtybuffers + hidirtybuffers) / 2); 1049 } 1050} 1051 1052/* 1053 * bundirty: 1054 * 1055 * Clear B_DELWRI for buffer. 1056 * 1057 * Since the buffer is not on a queue, we do not update the numfreebuffers 1058 * count. 1059 * 1060 * The buffer must be on QUEUE_NONE. 1061 */ 1062 1063void 1064bundirty(struct buf *bp) 1065{ 1066 1067 CTR3(KTR_BUF, "bundirty(%p) vp %p flags %X", bp, bp->b_vp, bp->b_flags); 1068 KASSERT(bp->b_bufobj != NULL, ("No b_bufobj %p", bp)); 1069 KASSERT(bp->b_flags & B_REMFREE || bp->b_qindex == QUEUE_NONE, 1070 ("bundirty: buffer %p still on queue %d", bp, bp->b_qindex)); 1071 BUF_ASSERT_HELD(bp); 1072 1073 if (bp->b_flags & B_DELWRI) { 1074 bp->b_flags &= ~B_DELWRI; 1075 reassignbuf(bp); 1076 atomic_subtract_int(&numdirtybuffers, 1); 1077 numdirtywakeup(lodirtybuffers); 1078 } 1079 /* 1080 * Since it is now being written, we can clear its deferred write flag. 1081 */ 1082 bp->b_flags &= ~B_DEFERRED; 1083} 1084 1085/* 1086 * bawrite: 1087 * 1088 * Asynchronous write. Start output on a buffer, but do not wait for 1089 * it to complete. The buffer is released when the output completes. 1090 * 1091 * bwrite() ( or the VOP routine anyway ) is responsible for handling 1092 * B_INVAL buffers. Not us. 1093 */ 1094void 1095bawrite(struct buf *bp) 1096{ 1097 1098 bp->b_flags |= B_ASYNC; 1099 (void) bwrite(bp); 1100} 1101 1102/* 1103 * bwillwrite: 1104 * 1105 * Called prior to the locking of any vnodes when we are expecting to 1106 * write. We do not want to starve the buffer cache with too many 1107 * dirty buffers so we block here. By blocking prior to the locking 1108 * of any vnodes we attempt to avoid the situation where a locked vnode 1109 * prevents the various system daemons from flushing related buffers. 1110 */ 1111 1112void 1113bwillwrite(void) 1114{ 1115 1116 if (numdirtybuffers >= hidirtybuffers) { 1117 mtx_lock(&nblock); 1118 while (numdirtybuffers >= hidirtybuffers) { 1119 bd_wakeup(1); 1120 needsbuffer |= VFS_BIO_NEED_DIRTYFLUSH; 1121 msleep(&needsbuffer, &nblock, 1122 (PRIBIO + 4), "flswai", 0); 1123 } 1124 mtx_unlock(&nblock); 1125 } 1126} 1127 1128/* 1129 * Return true if we have too many dirty buffers. 1130 */ 1131int 1132buf_dirty_count_severe(void) 1133{ 1134 1135 return(numdirtybuffers >= hidirtybuffers); 1136} 1137 1138/* 1139 * brelse: 1140 * 1141 * Release a busy buffer and, if requested, free its resources. The 1142 * buffer will be stashed in the appropriate bufqueue[] allowing it 1143 * to be accessed later as a cache entity or reused for other purposes. 1144 */ 1145void 1146brelse(struct buf *bp) 1147{ 1148 CTR3(KTR_BUF, "brelse(%p) vp %p flags %X", 1149 bp, bp->b_vp, bp->b_flags); 1150 KASSERT(!(bp->b_flags & (B_CLUSTER|B_PAGING)), 1151 ("brelse: inappropriate B_PAGING or B_CLUSTER bp %p", bp)); 1152 1153 if (bp->b_flags & B_MANAGED) { 1154 bqrelse(bp); 1155 return; 1156 } 1157 1158 if (bp->b_iocmd == BIO_WRITE && (bp->b_ioflags & BIO_ERROR) && 1159 bp->b_error == EIO && !(bp->b_flags & B_INVAL)) { 1160 /* 1161 * Failed write, redirty. Must clear BIO_ERROR to prevent 1162 * pages from being scrapped. If the error is anything 1163 * other than an I/O error (EIO), assume that retryingi 1164 * is futile. 1165 */ 1166 bp->b_ioflags &= ~BIO_ERROR; 1167 bdirty(bp); 1168 } else if ((bp->b_flags & (B_NOCACHE | B_INVAL)) || 1169 (bp->b_ioflags & BIO_ERROR) || (bp->b_bufsize <= 0)) { 1170 /* 1171 * Either a failed I/O or we were asked to free or not 1172 * cache the buffer. 1173 */ 1174 bp->b_flags |= B_INVAL; 1175 if (!LIST_EMPTY(&bp->b_dep)) 1176 buf_deallocate(bp); 1177 if (bp->b_flags & B_DELWRI) { 1178 atomic_subtract_int(&numdirtybuffers, 1); 1179 numdirtywakeup(lodirtybuffers); 1180 } 1181 bp->b_flags &= ~(B_DELWRI | B_CACHE); 1182 if ((bp->b_flags & B_VMIO) == 0) { 1183 if (bp->b_bufsize) 1184 allocbuf(bp, 0); 1185 if (bp->b_vp) 1186 (void) brelvp(bp); 1187 } 1188 } 1189 1190 /* 1191 * We must clear B_RELBUF if B_DELWRI is set. If vfs_vmio_release() 1192 * is called with B_DELWRI set, the underlying pages may wind up 1193 * getting freed causing a previous write (bdwrite()) to get 'lost' 1194 * because pages associated with a B_DELWRI bp are marked clean. 1195 * 1196 * We still allow the B_INVAL case to call vfs_vmio_release(), even 1197 * if B_DELWRI is set. 1198 * 1199 * If B_DELWRI is not set we may have to set B_RELBUF if we are low 1200 * on pages to return pages to the VM page queues. 1201 */ 1202 if (bp->b_flags & B_DELWRI) 1203 bp->b_flags &= ~B_RELBUF; 1204 else if (vm_page_count_severe()) { 1205 /* 1206 * XXX This lock may not be necessary since BKGRDINPROG 1207 * cannot be set while we hold the buf lock, it can only be 1208 * cleared if it is already pending. 1209 */ 1210 if (bp->b_vp) { 1211 BO_LOCK(bp->b_bufobj); 1212 if (!(bp->b_vflags & BV_BKGRDINPROG)) 1213 bp->b_flags |= B_RELBUF; 1214 BO_UNLOCK(bp->b_bufobj); 1215 } else 1216 bp->b_flags |= B_RELBUF; 1217 } 1218 1219 /* 1220 * VMIO buffer rundown. It is not very necessary to keep a VMIO buffer 1221 * constituted, not even NFS buffers now. Two flags effect this. If 1222 * B_INVAL, the struct buf is invalidated but the VM object is kept 1223 * around ( i.e. so it is trivial to reconstitute the buffer later ). 1224 * 1225 * If BIO_ERROR or B_NOCACHE is set, pages in the VM object will be 1226 * invalidated. BIO_ERROR cannot be set for a failed write unless the 1227 * buffer is also B_INVAL because it hits the re-dirtying code above. 1228 * 1229 * Normally we can do this whether a buffer is B_DELWRI or not. If 1230 * the buffer is an NFS buffer, it is tracking piecemeal writes or 1231 * the commit state and we cannot afford to lose the buffer. If the 1232 * buffer has a background write in progress, we need to keep it 1233 * around to prevent it from being reconstituted and starting a second 1234 * background write. 1235 */ 1236 if ((bp->b_flags & B_VMIO) 1237 && !(bp->b_vp->v_mount != NULL && 1238 (bp->b_vp->v_mount->mnt_vfc->vfc_flags & VFCF_NETWORK) != 0 && 1239 !vn_isdisk(bp->b_vp, NULL) && 1240 (bp->b_flags & B_DELWRI)) 1241 ) { 1242 1243 int i, j, resid; 1244 vm_page_t m; 1245 off_t foff; 1246 vm_pindex_t poff; 1247 vm_object_t obj; 1248 1249 obj = bp->b_bufobj->bo_object; 1250 1251 /* 1252 * Get the base offset and length of the buffer. Note that 1253 * in the VMIO case if the buffer block size is not 1254 * page-aligned then b_data pointer may not be page-aligned. 1255 * But our b_pages[] array *IS* page aligned. 1256 * 1257 * block sizes less then DEV_BSIZE (usually 512) are not 1258 * supported due to the page granularity bits (m->valid, 1259 * m->dirty, etc...). 1260 * 1261 * See man buf(9) for more information 1262 */ 1263 resid = bp->b_bufsize; 1264 foff = bp->b_offset; 1265 VM_OBJECT_LOCK(obj); 1266 for (i = 0; i < bp->b_npages; i++) { 1267 int had_bogus = 0; 1268 1269 m = bp->b_pages[i]; 1270 1271 /* 1272 * If we hit a bogus page, fixup *all* the bogus pages 1273 * now. 1274 */ 1275 if (m == bogus_page) { 1276 poff = OFF_TO_IDX(bp->b_offset); 1277 had_bogus = 1; 1278 1279 for (j = i; j < bp->b_npages; j++) { 1280 vm_page_t mtmp; 1281 mtmp = bp->b_pages[j]; 1282 if (mtmp == bogus_page) { 1283 mtmp = vm_page_lookup(obj, poff + j); 1284 if (!mtmp) { 1285 panic("brelse: page missing\n"); 1286 } 1287 bp->b_pages[j] = mtmp; 1288 } 1289 } 1290 1291 if ((bp->b_flags & B_INVAL) == 0) { 1292 pmap_qenter( 1293 trunc_page((vm_offset_t)bp->b_data), 1294 bp->b_pages, bp->b_npages); 1295 } 1296 m = bp->b_pages[i]; 1297 } 1298 if ((bp->b_flags & B_NOCACHE) || 1299 (bp->b_ioflags & BIO_ERROR)) { 1300 int poffset = foff & PAGE_MASK; 1301 int presid = resid > (PAGE_SIZE - poffset) ? 1302 (PAGE_SIZE - poffset) : resid; 1303 1304 KASSERT(presid >= 0, ("brelse: extra page")); 1305 vm_page_lock_queues(); 1306 vm_page_set_invalid(m, poffset, presid); 1307 vm_page_unlock_queues(); 1308 if (had_bogus) 1309 printf("avoided corruption bug in bogus_page/brelse code\n"); 1310 } 1311 resid -= PAGE_SIZE - (foff & PAGE_MASK); 1312 foff = (foff + PAGE_SIZE) & ~(off_t)PAGE_MASK; 1313 } 1314 VM_OBJECT_UNLOCK(obj); 1315 if (bp->b_flags & (B_INVAL | B_RELBUF)) 1316 vfs_vmio_release(bp); 1317 1318 } else if (bp->b_flags & B_VMIO) { 1319 1320 if (bp->b_flags & (B_INVAL | B_RELBUF)) { 1321 vfs_vmio_release(bp); 1322 } 1323 1324 } else if ((bp->b_flags & (B_INVAL | B_RELBUF)) != 0) { 1325 if (bp->b_bufsize != 0) 1326 allocbuf(bp, 0); 1327 if (bp->b_vp != NULL) 1328 (void) brelvp(bp); 1329 } 1330 1331 if (BUF_LOCKRECURSED(bp)) { 1332 /* do not release to free list */ 1333 BUF_UNLOCK(bp); 1334 return; 1335 } 1336 1337 /* enqueue */ 1338 mtx_lock(&bqlock); 1339 /* Handle delayed bremfree() processing. */ 1340 if (bp->b_flags & B_REMFREE) 1341 bremfreel(bp); 1342 if (bp->b_qindex != QUEUE_NONE) 1343 panic("brelse: free buffer onto another queue???"); 1344 1345 /* buffers with no memory */ 1346 if (bp->b_bufsize == 0) { 1347 bp->b_flags |= B_INVAL; 1348 bp->b_xflags &= ~(BX_BKGRDWRITE | BX_ALTDATA); 1349 if (bp->b_vflags & BV_BKGRDINPROG) 1350 panic("losing buffer 1"); 1351 if (bp->b_kvasize) { 1352 bp->b_qindex = QUEUE_EMPTYKVA; 1353 } else { 1354 bp->b_qindex = QUEUE_EMPTY; 1355 } 1356 TAILQ_INSERT_HEAD(&bufqueues[bp->b_qindex], bp, b_freelist); 1357 /* buffers with junk contents */ 1358 } else if (bp->b_flags & (B_INVAL | B_NOCACHE | B_RELBUF) || 1359 (bp->b_ioflags & BIO_ERROR)) { 1360 bp->b_flags |= B_INVAL; 1361 bp->b_xflags &= ~(BX_BKGRDWRITE | BX_ALTDATA); 1362 if (bp->b_vflags & BV_BKGRDINPROG) 1363 panic("losing buffer 2"); 1364 bp->b_qindex = QUEUE_CLEAN; 1365 TAILQ_INSERT_HEAD(&bufqueues[QUEUE_CLEAN], bp, b_freelist); 1366 /* remaining buffers */ 1367 } else { 1368 if ((bp->b_flags & (B_DELWRI|B_NEEDSGIANT)) == 1369 (B_DELWRI|B_NEEDSGIANT)) 1370 bp->b_qindex = QUEUE_DIRTY_GIANT; 1371 if (bp->b_flags & B_DELWRI) 1372 bp->b_qindex = QUEUE_DIRTY; 1373 else 1374 bp->b_qindex = QUEUE_CLEAN; 1375 if (bp->b_flags & B_AGE) 1376 TAILQ_INSERT_HEAD(&bufqueues[bp->b_qindex], bp, b_freelist); 1377 else 1378 TAILQ_INSERT_TAIL(&bufqueues[bp->b_qindex], bp, b_freelist); 1379 } 1380 mtx_unlock(&bqlock); 1381 1382 /* 1383 * If B_INVAL and B_DELWRI is set, clear B_DELWRI. We have already 1384 * placed the buffer on the correct queue. We must also disassociate 1385 * the device and vnode for a B_INVAL buffer so gbincore() doesn't 1386 * find it. 1387 */ 1388 if (bp->b_flags & B_INVAL) { 1389 if (bp->b_flags & B_DELWRI) 1390 bundirty(bp); 1391 if (bp->b_vp) 1392 (void) brelvp(bp); 1393 } 1394 1395 /* 1396 * Fixup numfreebuffers count. The bp is on an appropriate queue 1397 * unless locked. We then bump numfreebuffers if it is not B_DELWRI. 1398 * We've already handled the B_INVAL case ( B_DELWRI will be clear 1399 * if B_INVAL is set ). 1400 */ 1401 1402 if (!(bp->b_flags & B_DELWRI)) 1403 bufcountwakeup(); 1404 1405 /* 1406 * Something we can maybe free or reuse 1407 */ 1408 if (bp->b_bufsize || bp->b_kvasize) 1409 bufspacewakeup(); 1410 1411 bp->b_flags &= ~(B_ASYNC | B_NOCACHE | B_AGE | B_RELBUF | B_DIRECT); 1412 if ((bp->b_flags & B_DELWRI) == 0 && (bp->b_xflags & BX_VNDIRTY)) 1413 panic("brelse: not dirty"); 1414 /* unlock */ 1415 BUF_UNLOCK(bp); 1416} 1417 1418/* 1419 * Release a buffer back to the appropriate queue but do not try to free 1420 * it. The buffer is expected to be used again soon. 1421 * 1422 * bqrelse() is used by bdwrite() to requeue a delayed write, and used by 1423 * biodone() to requeue an async I/O on completion. It is also used when 1424 * known good buffers need to be requeued but we think we may need the data 1425 * again soon. 1426 * 1427 * XXX we should be able to leave the B_RELBUF hint set on completion. 1428 */ 1429void 1430bqrelse(struct buf *bp) 1431{ 1432 CTR3(KTR_BUF, "bqrelse(%p) vp %p flags %X", bp, bp->b_vp, bp->b_flags); 1433 KASSERT(!(bp->b_flags & (B_CLUSTER|B_PAGING)), 1434 ("bqrelse: inappropriate B_PAGING or B_CLUSTER bp %p", bp)); 1435 1436 if (BUF_LOCKRECURSED(bp)) { 1437 /* do not release to free list */ 1438 BUF_UNLOCK(bp); 1439 return; 1440 } 1441 1442 if (bp->b_flags & B_MANAGED) { 1443 if (bp->b_flags & B_REMFREE) { 1444 mtx_lock(&bqlock); 1445 bremfreel(bp); 1446 mtx_unlock(&bqlock); 1447 } 1448 bp->b_flags &= ~(B_ASYNC | B_NOCACHE | B_AGE | B_RELBUF); 1449 BUF_UNLOCK(bp); 1450 return; 1451 } 1452 1453 mtx_lock(&bqlock); 1454 /* Handle delayed bremfree() processing. */ 1455 if (bp->b_flags & B_REMFREE) 1456 bremfreel(bp); 1457 if (bp->b_qindex != QUEUE_NONE) 1458 panic("bqrelse: free buffer onto another queue???"); 1459 /* buffers with stale but valid contents */ 1460 if (bp->b_flags & B_DELWRI) { 1461 if (bp->b_flags & B_NEEDSGIANT) 1462 bp->b_qindex = QUEUE_DIRTY_GIANT; 1463 else 1464 bp->b_qindex = QUEUE_DIRTY; 1465 TAILQ_INSERT_TAIL(&bufqueues[bp->b_qindex], bp, b_freelist); 1466 } else { 1467 /* 1468 * XXX This lock may not be necessary since BKGRDINPROG 1469 * cannot be set while we hold the buf lock, it can only be 1470 * cleared if it is already pending. 1471 */ 1472 BO_LOCK(bp->b_bufobj); 1473 if (!vm_page_count_severe() || bp->b_vflags & BV_BKGRDINPROG) { 1474 BO_UNLOCK(bp->b_bufobj); 1475 bp->b_qindex = QUEUE_CLEAN; 1476 TAILQ_INSERT_TAIL(&bufqueues[QUEUE_CLEAN], bp, 1477 b_freelist); 1478 } else { 1479 /* 1480 * We are too low on memory, we have to try to free 1481 * the buffer (most importantly: the wired pages 1482 * making up its backing store) *now*. 1483 */ 1484 BO_UNLOCK(bp->b_bufobj); 1485 mtx_unlock(&bqlock); 1486 brelse(bp); 1487 return; 1488 } 1489 } 1490 mtx_unlock(&bqlock); 1491 1492 if ((bp->b_flags & B_INVAL) || !(bp->b_flags & B_DELWRI)) 1493 bufcountwakeup(); 1494 1495 /* 1496 * Something we can maybe free or reuse. 1497 */ 1498 if (bp->b_bufsize && !(bp->b_flags & B_DELWRI)) 1499 bufspacewakeup(); 1500 1501 bp->b_flags &= ~(B_ASYNC | B_NOCACHE | B_AGE | B_RELBUF); 1502 if ((bp->b_flags & B_DELWRI) == 0 && (bp->b_xflags & BX_VNDIRTY)) 1503 panic("bqrelse: not dirty"); 1504 /* unlock */ 1505 BUF_UNLOCK(bp); 1506} 1507 1508/* Give pages used by the bp back to the VM system (where possible) */ 1509static void 1510vfs_vmio_release(struct buf *bp) 1511{ 1512 int i; 1513 vm_page_t m; 1514 1515 VM_OBJECT_LOCK(bp->b_bufobj->bo_object); 1516 vm_page_lock_queues(); 1517 for (i = 0; i < bp->b_npages; i++) { 1518 m = bp->b_pages[i]; 1519 bp->b_pages[i] = NULL; 1520 /* 1521 * In order to keep page LRU ordering consistent, put 1522 * everything on the inactive queue. 1523 */ 1524 vm_page_unwire(m, 0); 1525 /* 1526 * We don't mess with busy pages, it is 1527 * the responsibility of the process that 1528 * busied the pages to deal with them. 1529 */ 1530 if ((m->oflags & VPO_BUSY) || (m->busy != 0)) 1531 continue; 1532 1533 if (m->wire_count == 0) { 1534 /* 1535 * Might as well free the page if we can and it has 1536 * no valid data. We also free the page if the 1537 * buffer was used for direct I/O 1538 */ 1539 if ((bp->b_flags & B_ASYNC) == 0 && !m->valid && 1540 m->hold_count == 0) { 1541 vm_page_free(m); 1542 } else if (bp->b_flags & B_DIRECT) { 1543 vm_page_try_to_free(m); 1544 } else if (vm_page_count_severe()) { 1545 vm_page_try_to_cache(m); 1546 } 1547 } 1548 } 1549 vm_page_unlock_queues(); 1550 VM_OBJECT_UNLOCK(bp->b_bufobj->bo_object); 1551 pmap_qremove(trunc_page((vm_offset_t) bp->b_data), bp->b_npages); 1552 1553 if (bp->b_bufsize) { 1554 bufspacewakeup(); 1555 bp->b_bufsize = 0; 1556 } 1557 bp->b_npages = 0; 1558 bp->b_flags &= ~B_VMIO; 1559 if (bp->b_vp) 1560 (void) brelvp(bp); 1561} 1562 1563/* 1564 * Check to see if a block at a particular lbn is available for a clustered 1565 * write. 1566 */ 1567static int 1568vfs_bio_clcheck(struct vnode *vp, int size, daddr_t lblkno, daddr_t blkno) 1569{ 1570 struct buf *bpa; 1571 int match; 1572 1573 match = 0; 1574 1575 /* If the buf isn't in core skip it */ 1576 if ((bpa = gbincore(&vp->v_bufobj, lblkno)) == NULL) 1577 return (0); 1578 1579 /* If the buf is busy we don't want to wait for it */ 1580 if (BUF_LOCK(bpa, LK_EXCLUSIVE | LK_NOWAIT, NULL) != 0) 1581 return (0); 1582 1583 /* Only cluster with valid clusterable delayed write buffers */ 1584 if ((bpa->b_flags & (B_DELWRI | B_CLUSTEROK | B_INVAL)) != 1585 (B_DELWRI | B_CLUSTEROK)) 1586 goto done; 1587 1588 if (bpa->b_bufsize != size) 1589 goto done; 1590 1591 /* 1592 * Check to see if it is in the expected place on disk and that the 1593 * block has been mapped. 1594 */ 1595 if ((bpa->b_blkno != bpa->b_lblkno) && (bpa->b_blkno == blkno)) 1596 match = 1; 1597done: 1598 BUF_UNLOCK(bpa); 1599 return (match); 1600} 1601 1602/* 1603 * vfs_bio_awrite: 1604 * 1605 * Implement clustered async writes for clearing out B_DELWRI buffers. 1606 * This is much better then the old way of writing only one buffer at 1607 * a time. Note that we may not be presented with the buffers in the 1608 * correct order, so we search for the cluster in both directions. 1609 */ 1610int 1611vfs_bio_awrite(struct buf *bp) 1612{ 1613 int i; 1614 int j; 1615 daddr_t lblkno = bp->b_lblkno; 1616 struct vnode *vp = bp->b_vp; 1617 int ncl; 1618 int nwritten; 1619 int size; 1620 int maxcl; 1621 1622 /* 1623 * right now we support clustered writing only to regular files. If 1624 * we find a clusterable block we could be in the middle of a cluster 1625 * rather then at the beginning. 1626 */ 1627 if ((vp->v_type == VREG) && 1628 (vp->v_mount != 0) && /* Only on nodes that have the size info */ 1629 (bp->b_flags & (B_CLUSTEROK | B_INVAL)) == B_CLUSTEROK) { 1630 1631 size = vp->v_mount->mnt_stat.f_iosize; 1632 maxcl = MAXPHYS / size; 1633 1634 VI_LOCK(vp); 1635 for (i = 1; i < maxcl; i++) 1636 if (vfs_bio_clcheck(vp, size, lblkno + i, 1637 bp->b_blkno + ((i * size) >> DEV_BSHIFT)) == 0) 1638 break; 1639 1640 for (j = 1; i + j <= maxcl && j <= lblkno; j++) 1641 if (vfs_bio_clcheck(vp, size, lblkno - j, 1642 bp->b_blkno - ((j * size) >> DEV_BSHIFT)) == 0) 1643 break; 1644 1645 VI_UNLOCK(vp); 1646 --j; 1647 ncl = i + j; 1648 /* 1649 * this is a possible cluster write 1650 */ 1651 if (ncl != 1) { 1652 BUF_UNLOCK(bp); 1653 nwritten = cluster_wbuild(vp, size, lblkno - j, ncl); 1654 return nwritten; 1655 } 1656 } 1657 bremfree(bp); 1658 bp->b_flags |= B_ASYNC; 1659 /* 1660 * default (old) behavior, writing out only one block 1661 * 1662 * XXX returns b_bufsize instead of b_bcount for nwritten? 1663 */ 1664 nwritten = bp->b_bufsize; 1665 (void) bwrite(bp); 1666 1667 return nwritten; 1668} 1669 1670/* 1671 * getnewbuf: 1672 * 1673 * Find and initialize a new buffer header, freeing up existing buffers 1674 * in the bufqueues as necessary. The new buffer is returned locked. 1675 * 1676 * Important: B_INVAL is not set. If the caller wishes to throw the 1677 * buffer away, the caller must set B_INVAL prior to calling brelse(). 1678 * 1679 * We block if: 1680 * We have insufficient buffer headers 1681 * We have insufficient buffer space 1682 * buffer_map is too fragmented ( space reservation fails ) 1683 * If we have to flush dirty buffers ( but we try to avoid this ) 1684 * 1685 * To avoid VFS layer recursion we do not flush dirty buffers ourselves. 1686 * Instead we ask the buf daemon to do it for us. We attempt to 1687 * avoid piecemeal wakeups of the pageout daemon. 1688 */ 1689 1690static struct buf * 1691getnewbuf(int slpflag, int slptimeo, int size, int maxsize) 1692{ 1693 struct buf *bp; 1694 struct buf *nbp; 1695 int defrag = 0; 1696 int nqindex; 1697 int waiters = 0; 1698 static int flushingbufs; 1699 1700 /* 1701 * We can't afford to block since we might be holding a vnode lock, 1702 * which may prevent system daemons from running. We deal with 1703 * low-memory situations by proactively returning memory and running 1704 * async I/O rather then sync I/O. 1705 */ 1706 1707 atomic_add_int(&getnewbufcalls, 1); 1708 atomic_subtract_int(&getnewbufrestarts, 1); 1709restart: 1710 atomic_add_int(&getnewbufrestarts, 1); 1711 1712 /* 1713 * Setup for scan. If we do not have enough free buffers, 1714 * we setup a degenerate case that immediately fails. Note 1715 * that if we are specially marked process, we are allowed to 1716 * dip into our reserves. 1717 * 1718 * The scanning sequence is nominally: EMPTY->EMPTYKVA->CLEAN 1719 * 1720 * We start with EMPTYKVA. If the list is empty we backup to EMPTY. 1721 * However, there are a number of cases (defragging, reusing, ...) 1722 * where we cannot backup. 1723 */ 1724 mtx_lock(&bqlock); 1725 nqindex = QUEUE_EMPTYKVA; 1726 nbp = TAILQ_FIRST(&bufqueues[QUEUE_EMPTYKVA]); 1727 1728 if (nbp == NULL) { 1729 /* 1730 * If no EMPTYKVA buffers and we are either 1731 * defragging or reusing, locate a CLEAN buffer 1732 * to free or reuse. If bufspace useage is low 1733 * skip this step so we can allocate a new buffer. 1734 */ 1735 if (defrag || bufspace >= lobufspace) { 1736 nqindex = QUEUE_CLEAN; 1737 nbp = TAILQ_FIRST(&bufqueues[QUEUE_CLEAN]); 1738 } 1739 1740 /* 1741 * If we could not find or were not allowed to reuse a 1742 * CLEAN buffer, check to see if it is ok to use an EMPTY 1743 * buffer. We can only use an EMPTY buffer if allocating 1744 * its KVA would not otherwise run us out of buffer space. 1745 */ 1746 if (nbp == NULL && defrag == 0 && 1747 bufspace + maxsize < hibufspace) { 1748 nqindex = QUEUE_EMPTY; 1749 nbp = TAILQ_FIRST(&bufqueues[QUEUE_EMPTY]); 1750 } 1751 } 1752 1753 /* 1754 * Run scan, possibly freeing data and/or kva mappings on the fly 1755 * depending. 1756 */ 1757 1758 while ((bp = nbp) != NULL) { 1759 int qindex = nqindex; 1760 1761 /* 1762 * Calculate next bp ( we can only use it if we do not block 1763 * or do other fancy things ). 1764 */ 1765 if ((nbp = TAILQ_NEXT(bp, b_freelist)) == NULL) { 1766 switch(qindex) { 1767 case QUEUE_EMPTY: 1768 nqindex = QUEUE_EMPTYKVA; 1769 if ((nbp = TAILQ_FIRST(&bufqueues[QUEUE_EMPTYKVA]))) 1770 break; 1771 /* FALLTHROUGH */ 1772 case QUEUE_EMPTYKVA: 1773 nqindex = QUEUE_CLEAN; 1774 if ((nbp = TAILQ_FIRST(&bufqueues[QUEUE_CLEAN]))) 1775 break; 1776 /* FALLTHROUGH */ 1777 case QUEUE_CLEAN: 1778 /* 1779 * nbp is NULL. 1780 */ 1781 break; 1782 } 1783 } 1784 /* 1785 * If we are defragging then we need a buffer with 1786 * b_kvasize != 0. XXX this situation should no longer 1787 * occur, if defrag is non-zero the buffer's b_kvasize 1788 * should also be non-zero at this point. XXX 1789 */ 1790 if (defrag && bp->b_kvasize == 0) { 1791 printf("Warning: defrag empty buffer %p\n", bp); 1792 continue; 1793 } 1794 1795 /* 1796 * Start freeing the bp. This is somewhat involved. nbp 1797 * remains valid only for QUEUE_EMPTY[KVA] bp's. 1798 */ 1799 if (BUF_LOCK(bp, LK_EXCLUSIVE | LK_NOWAIT, NULL) != 0) 1800 continue; 1801 if (bp->b_vp) { 1802 BO_LOCK(bp->b_bufobj); 1803 if (bp->b_vflags & BV_BKGRDINPROG) { 1804 BO_UNLOCK(bp->b_bufobj); 1805 BUF_UNLOCK(bp); 1806 continue; 1807 } 1808 BO_UNLOCK(bp->b_bufobj); 1809 } 1810 CTR6(KTR_BUF, 1811 "getnewbuf(%p) vp %p flags %X kvasize %d bufsize %d " 1812 "queue %d (recycling)", bp, bp->b_vp, bp->b_flags, 1813 bp->b_kvasize, bp->b_bufsize, qindex); 1814 1815 /* 1816 * Sanity Checks 1817 */ 1818 KASSERT(bp->b_qindex == qindex, ("getnewbuf: inconsistant queue %d bp %p", qindex, bp)); 1819 1820 /* 1821 * Note: we no longer distinguish between VMIO and non-VMIO 1822 * buffers. 1823 */ 1824 1825 KASSERT((bp->b_flags & B_DELWRI) == 0, ("delwri buffer %p found in queue %d", bp, qindex)); 1826 1827 bremfreel(bp); 1828 mtx_unlock(&bqlock); 1829 1830 if (qindex == QUEUE_CLEAN) { 1831 if (bp->b_flags & B_VMIO) { 1832 bp->b_flags &= ~B_ASYNC; 1833 vfs_vmio_release(bp); 1834 } 1835 if (bp->b_vp) 1836 waiters = brelvp(bp); 1837 } 1838 1839 /* 1840 * NOTE: nbp is now entirely invalid. We can only restart 1841 * the scan from this point on. 1842 * 1843 * Get the rest of the buffer freed up. b_kva* is still 1844 * valid after this operation. 1845 */ 1846 1847 if (bp->b_rcred != NOCRED) { 1848 crfree(bp->b_rcred); 1849 bp->b_rcred = NOCRED; 1850 } 1851 if (bp->b_wcred != NOCRED) { 1852 crfree(bp->b_wcred); 1853 bp->b_wcred = NOCRED; 1854 } 1855 if (!LIST_EMPTY(&bp->b_dep)) 1856 buf_deallocate(bp); 1857 if (bp->b_vflags & BV_BKGRDINPROG) 1858 panic("losing buffer 3"); 1859 KASSERT(bp->b_vp == NULL, 1860 ("bp: %p still has vnode %p. qindex: %d", 1861 bp, bp->b_vp, qindex)); 1862 KASSERT((bp->b_xflags & (BX_VNCLEAN|BX_VNDIRTY)) == 0, 1863 ("bp: %p still on a buffer list. xflags %X", 1864 bp, bp->b_xflags)); 1865 1866 if (bp->b_bufsize) 1867 allocbuf(bp, 0); 1868 1869 bp->b_flags = 0; 1870 bp->b_ioflags = 0; 1871 bp->b_xflags = 0; 1872 bp->b_vflags = 0; 1873 bp->b_vp = NULL; 1874 bp->b_blkno = bp->b_lblkno = 0; 1875 bp->b_offset = NOOFFSET; 1876 bp->b_iodone = 0; 1877 bp->b_error = 0; 1878 bp->b_resid = 0; 1879 bp->b_bcount = 0; 1880 bp->b_npages = 0; 1881 bp->b_dirtyoff = bp->b_dirtyend = 0; 1882 bp->b_bufobj = NULL; 1883 bp->b_pin_count = 0; 1884 bp->b_fsprivate1 = NULL; 1885 bp->b_fsprivate2 = NULL; 1886 bp->b_fsprivate3 = NULL; 1887 1888 LIST_INIT(&bp->b_dep); 1889 1890 /* 1891 * If we are defragging then free the buffer. 1892 */ 1893 if (defrag) { 1894 bp->b_flags |= B_INVAL; 1895 bfreekva(bp); 1896 brelse(bp); 1897 defrag = 0; 1898 goto restart; 1899 } 1900 1901 /* 1902 * Notify any waiters for the buffer lock about 1903 * identity change by freeing the buffer. 1904 */ 1905 if (qindex == QUEUE_CLEAN && waiters > 0) { 1906 bp->b_flags |= B_INVAL; 1907 bfreekva(bp); 1908 brelse(bp); 1909 goto restart; 1910 } 1911 1912 /* 1913 * If we are overcomitted then recover the buffer and its 1914 * KVM space. This occurs in rare situations when multiple 1915 * processes are blocked in getnewbuf() or allocbuf(). 1916 */ 1917 if (bufspace >= hibufspace) 1918 flushingbufs = 1; 1919 if (flushingbufs && bp->b_kvasize != 0) { 1920 bp->b_flags |= B_INVAL; 1921 bfreekva(bp); 1922 brelse(bp); 1923 goto restart; 1924 } 1925 if (bufspace < lobufspace) 1926 flushingbufs = 0; 1927 break; 1928 } 1929 1930 /* 1931 * If we exhausted our list, sleep as appropriate. We may have to 1932 * wakeup various daemons and write out some dirty buffers. 1933 * 1934 * Generally we are sleeping due to insufficient buffer space. 1935 */ 1936 1937 if (bp == NULL) { 1938 int flags; 1939 char *waitmsg; 1940 1941 if (defrag) { 1942 flags = VFS_BIO_NEED_BUFSPACE; 1943 waitmsg = "nbufkv"; 1944 } else if (bufspace >= hibufspace) { 1945 waitmsg = "nbufbs"; 1946 flags = VFS_BIO_NEED_BUFSPACE; 1947 } else { 1948 waitmsg = "newbuf"; 1949 flags = VFS_BIO_NEED_ANY; 1950 } 1951 mtx_lock(&nblock); 1952 needsbuffer |= flags; 1953 mtx_unlock(&nblock); 1954 mtx_unlock(&bqlock); 1955 1956 bd_speedup(); /* heeeelp */ 1957 1958 mtx_lock(&nblock); 1959 while (needsbuffer & flags) { 1960 if (msleep(&needsbuffer, &nblock, 1961 (PRIBIO + 4) | slpflag, waitmsg, slptimeo)) { 1962 mtx_unlock(&nblock); 1963 return (NULL); 1964 } 1965 } 1966 mtx_unlock(&nblock); 1967 } else { 1968 /* 1969 * We finally have a valid bp. We aren't quite out of the 1970 * woods, we still have to reserve kva space. In order 1971 * to keep fragmentation sane we only allocate kva in 1972 * BKVASIZE chunks. 1973 */ 1974 maxsize = (maxsize + BKVAMASK) & ~BKVAMASK; 1975 1976 if (maxsize != bp->b_kvasize) { 1977 vm_offset_t addr = 0; 1978 1979 bfreekva(bp); 1980 1981 vm_map_lock(buffer_map); 1982 if (vm_map_findspace(buffer_map, 1983 vm_map_min(buffer_map), maxsize, &addr)) { 1984 /* 1985 * Uh oh. Buffer map is to fragmented. We 1986 * must defragment the map. 1987 */ 1988 atomic_add_int(&bufdefragcnt, 1); 1989 vm_map_unlock(buffer_map); 1990 defrag = 1; 1991 bp->b_flags |= B_INVAL; 1992 brelse(bp); 1993 goto restart; 1994 } 1995 if (addr) { 1996 vm_map_insert(buffer_map, NULL, 0, 1997 addr, addr + maxsize, 1998 VM_PROT_ALL, VM_PROT_ALL, MAP_NOFAULT); 1999 2000 bp->b_kvabase = (caddr_t) addr; 2001 bp->b_kvasize = maxsize; 2002 atomic_add_int(&bufspace, bp->b_kvasize); 2003 atomic_add_int(&bufreusecnt, 1); 2004 } 2005 vm_map_unlock(buffer_map); 2006 } 2007 bp->b_saveaddr = bp->b_kvabase; 2008 bp->b_data = bp->b_saveaddr; 2009 } 2010 return(bp); 2011} 2012 2013/* 2014 * buf_daemon: 2015 * 2016 * buffer flushing daemon. Buffers are normally flushed by the 2017 * update daemon but if it cannot keep up this process starts to 2018 * take the load in an attempt to prevent getnewbuf() from blocking. 2019 */ 2020 2021static struct kproc_desc buf_kp = { 2022 "bufdaemon", 2023 buf_daemon, 2024 &bufdaemonproc 2025}; 2026SYSINIT(bufdaemon, SI_SUB_KTHREAD_BUF, SI_ORDER_FIRST, kproc_start, &buf_kp); 2027 2028static void 2029buf_daemon() 2030{ 2031 2032 /* 2033 * This process needs to be suspended prior to shutdown sync. 2034 */ 2035 EVENTHANDLER_REGISTER(shutdown_pre_sync, kproc_shutdown, bufdaemonproc, 2036 SHUTDOWN_PRI_LAST); 2037 2038 /* 2039 * This process is allowed to take the buffer cache to the limit 2040 */ 2041 curthread->td_pflags |= TDP_NORUNNINGBUF; 2042 mtx_lock(&bdlock); 2043 for (;;) { 2044 bd_request = 0; 2045 mtx_unlock(&bdlock); 2046 2047 kproc_suspend_check(bufdaemonproc); 2048 2049 /* 2050 * Do the flush. Limit the amount of in-transit I/O we 2051 * allow to build up, otherwise we would completely saturate 2052 * the I/O system. Wakeup any waiting processes before we 2053 * normally would so they can run in parallel with our drain. 2054 */ 2055 while (numdirtybuffers > lodirtybuffers) { 2056 int flushed; 2057 2058 flushed = flushbufqueues(QUEUE_DIRTY, 0); 2059 /* The list empty check here is slightly racy */ 2060 if (!TAILQ_EMPTY(&bufqueues[QUEUE_DIRTY_GIANT])) { 2061 mtx_lock(&Giant); 2062 flushed += flushbufqueues(QUEUE_DIRTY_GIANT, 0); 2063 mtx_unlock(&Giant); 2064 } 2065 if (flushed == 0) { 2066 /* 2067 * Could not find any buffers without rollback 2068 * dependencies, so just write the first one 2069 * in the hopes of eventually making progress. 2070 */ 2071 flushbufqueues(QUEUE_DIRTY, 1); 2072 if (!TAILQ_EMPTY( 2073 &bufqueues[QUEUE_DIRTY_GIANT])) { 2074 mtx_lock(&Giant); 2075 flushbufqueues(QUEUE_DIRTY_GIANT, 1); 2076 mtx_unlock(&Giant); 2077 } 2078 break; 2079 } 2080 uio_yield(); 2081 } 2082 2083 /* 2084 * Only clear bd_request if we have reached our low water 2085 * mark. The buf_daemon normally waits 1 second and 2086 * then incrementally flushes any dirty buffers that have 2087 * built up, within reason. 2088 * 2089 * If we were unable to hit our low water mark and couldn't 2090 * find any flushable buffers, we sleep half a second. 2091 * Otherwise we loop immediately. 2092 */ 2093 mtx_lock(&bdlock); 2094 if (numdirtybuffers <= lodirtybuffers) { 2095 /* 2096 * We reached our low water mark, reset the 2097 * request and sleep until we are needed again. 2098 * The sleep is just so the suspend code works. 2099 */ 2100 bd_request = 0; 2101 msleep(&bd_request, &bdlock, PVM, "psleep", hz); 2102 } else { 2103 /* 2104 * We couldn't find any flushable dirty buffers but 2105 * still have too many dirty buffers, we 2106 * have to sleep and try again. (rare) 2107 */ 2108 msleep(&bd_request, &bdlock, PVM, "qsleep", hz / 10); 2109 } 2110 } 2111} 2112 2113/* 2114 * flushbufqueues: 2115 * 2116 * Try to flush a buffer in the dirty queue. We must be careful to 2117 * free up B_INVAL buffers instead of write them, which NFS is 2118 * particularly sensitive to. 2119 */ 2120static int flushwithdeps = 0; 2121SYSCTL_INT(_vfs, OID_AUTO, flushwithdeps, CTLFLAG_RW, &flushwithdeps, 2122 0, "Number of buffers flushed with dependecies that require rollbacks"); 2123 2124static int 2125flushbufqueues(int queue, int flushdeps) 2126{ 2127 struct buf sentinel; 2128 struct vnode *vp; 2129 struct mount *mp; 2130 struct buf *bp; 2131 int hasdeps; 2132 int flushed; 2133 int target; 2134 2135 target = numdirtybuffers - lodirtybuffers; 2136 if (flushdeps && target > 2) 2137 target /= 2; 2138 flushed = 0; 2139 bp = NULL; 2140 mtx_lock(&bqlock); 2141 TAILQ_INSERT_TAIL(&bufqueues[queue], &sentinel, b_freelist); 2142 while (flushed != target) { 2143 bp = TAILQ_FIRST(&bufqueues[queue]); 2144 if (bp == &sentinel) 2145 break; 2146 TAILQ_REMOVE(&bufqueues[queue], bp, b_freelist); 2147 TAILQ_INSERT_TAIL(&bufqueues[queue], bp, b_freelist); 2148 2149 if (BUF_LOCK(bp, LK_EXCLUSIVE | LK_NOWAIT, NULL) != 0) 2150 continue; 2151 if (bp->b_pin_count > 0) { 2152 BUF_UNLOCK(bp); 2153 continue; 2154 } 2155 BO_LOCK(bp->b_bufobj); 2156 if ((bp->b_vflags & BV_BKGRDINPROG) != 0 || 2157 (bp->b_flags & B_DELWRI) == 0) { 2158 BO_UNLOCK(bp->b_bufobj); 2159 BUF_UNLOCK(bp); 2160 continue; 2161 } 2162 BO_UNLOCK(bp->b_bufobj); 2163 if (bp->b_flags & B_INVAL) { 2164 bremfreel(bp); 2165 mtx_unlock(&bqlock); 2166 brelse(bp); 2167 flushed++; 2168 numdirtywakeup((lodirtybuffers + hidirtybuffers) / 2); 2169 mtx_lock(&bqlock); 2170 continue; 2171 } 2172 2173 if (!LIST_EMPTY(&bp->b_dep) && buf_countdeps(bp, 0)) { 2174 if (flushdeps == 0) { 2175 BUF_UNLOCK(bp); 2176 continue; 2177 } 2178 hasdeps = 1; 2179 } else 2180 hasdeps = 0; 2181 /* 2182 * We must hold the lock on a vnode before writing 2183 * one of its buffers. Otherwise we may confuse, or 2184 * in the case of a snapshot vnode, deadlock the 2185 * system. 2186 * 2187 * The lock order here is the reverse of the normal 2188 * of vnode followed by buf lock. This is ok because 2189 * the NOWAIT will prevent deadlock. 2190 */ 2191 vp = bp->b_vp; 2192 if (vn_start_write(vp, &mp, V_NOWAIT) != 0) { 2193 BUF_UNLOCK(bp); 2194 continue; 2195 } 2196 if (vn_lock(vp, LK_EXCLUSIVE | LK_NOWAIT) == 0) { 2197 mtx_unlock(&bqlock); 2198 CTR3(KTR_BUF, "flushbufqueue(%p) vp %p flags %X", 2199 bp, bp->b_vp, bp->b_flags); 2200 vfs_bio_awrite(bp); 2201 vn_finished_write(mp); 2202 VOP_UNLOCK(vp, 0); 2203 flushwithdeps += hasdeps; 2204 flushed++; 2205 waitrunningbufspace(); 2206 numdirtywakeup((lodirtybuffers + hidirtybuffers) / 2); 2207 mtx_lock(&bqlock); 2208 continue; 2209 } 2210 vn_finished_write(mp); 2211 BUF_UNLOCK(bp); 2212 } 2213 TAILQ_REMOVE(&bufqueues[queue], &sentinel, b_freelist); 2214 mtx_unlock(&bqlock); 2215 return (flushed); 2216} 2217 2218/* 2219 * Check to see if a block is currently memory resident. 2220 */ 2221struct buf * 2222incore(struct bufobj *bo, daddr_t blkno) 2223{ 2224 struct buf *bp; 2225 2226 BO_LOCK(bo); 2227 bp = gbincore(bo, blkno); 2228 BO_UNLOCK(bo); 2229 return (bp); 2230} 2231 2232/* 2233 * Returns true if no I/O is needed to access the 2234 * associated VM object. This is like incore except 2235 * it also hunts around in the VM system for the data. 2236 */ 2237 2238static int 2239inmem(struct vnode * vp, daddr_t blkno) 2240{ 2241 vm_object_t obj; 2242 vm_offset_t toff, tinc, size; 2243 vm_page_t m; 2244 vm_ooffset_t off; 2245 2246 ASSERT_VOP_LOCKED(vp, "inmem"); 2247 2248 if (incore(&vp->v_bufobj, blkno)) 2249 return 1; 2250 if (vp->v_mount == NULL) 2251 return 0; 2252 obj = vp->v_object; 2253 if (obj == NULL) 2254 return (0); 2255 2256 size = PAGE_SIZE; 2257 if (size > vp->v_mount->mnt_stat.f_iosize) 2258 size = vp->v_mount->mnt_stat.f_iosize; 2259 off = (vm_ooffset_t)blkno * (vm_ooffset_t)vp->v_mount->mnt_stat.f_iosize; 2260 2261 VM_OBJECT_LOCK(obj); 2262 for (toff = 0; toff < vp->v_mount->mnt_stat.f_iosize; toff += tinc) { 2263 m = vm_page_lookup(obj, OFF_TO_IDX(off + toff)); 2264 if (!m) 2265 goto notinmem; 2266 tinc = size; 2267 if (tinc > PAGE_SIZE - ((toff + off) & PAGE_MASK)) 2268 tinc = PAGE_SIZE - ((toff + off) & PAGE_MASK); 2269 if (vm_page_is_valid(m, 2270 (vm_offset_t) ((toff + off) & PAGE_MASK), tinc) == 0) 2271 goto notinmem; 2272 } 2273 VM_OBJECT_UNLOCK(obj); 2274 return 1; 2275 2276notinmem: 2277 VM_OBJECT_UNLOCK(obj); 2278 return (0); 2279} 2280 2281/* 2282 * vfs_setdirty: 2283 * 2284 * Sets the dirty range for a buffer based on the status of the dirty 2285 * bits in the pages comprising the buffer. 2286 * 2287 * The range is limited to the size of the buffer. 2288 * 2289 * This routine is primarily used by NFS, but is generalized for the 2290 * B_VMIO case. 2291 */ 2292static void 2293vfs_setdirty(struct buf *bp) 2294{ 2295 2296 /* 2297 * Degenerate case - empty buffer 2298 */ 2299 2300 if (bp->b_bufsize == 0) 2301 return; 2302 2303 /* 2304 * We qualify the scan for modified pages on whether the 2305 * object has been flushed yet. 2306 */ 2307 2308 if ((bp->b_flags & B_VMIO) == 0) 2309 return; 2310 2311 VM_OBJECT_LOCK(bp->b_bufobj->bo_object); 2312 vfs_setdirty_locked_object(bp); 2313 VM_OBJECT_UNLOCK(bp->b_bufobj->bo_object); 2314} 2315 2316static void 2317vfs_setdirty_locked_object(struct buf *bp) 2318{ 2319 vm_object_t object; 2320 int i; 2321 2322 object = bp->b_bufobj->bo_object; 2323 VM_OBJECT_LOCK_ASSERT(object, MA_OWNED); 2324 if (object->flags & (OBJ_MIGHTBEDIRTY|OBJ_CLEANING)) { 2325 vm_offset_t boffset; 2326 vm_offset_t eoffset; 2327 2328 vm_page_lock_queues(); 2329 /* 2330 * test the pages to see if they have been modified directly 2331 * by users through the VM system. 2332 */ 2333 for (i = 0; i < bp->b_npages; i++) 2334 vm_page_test_dirty(bp->b_pages[i]); 2335 2336 /* 2337 * Calculate the encompassing dirty range, boffset and eoffset, 2338 * (eoffset - boffset) bytes. 2339 */ 2340 2341 for (i = 0; i < bp->b_npages; i++) { 2342 if (bp->b_pages[i]->dirty) 2343 break; 2344 } 2345 boffset = (i << PAGE_SHIFT) - (bp->b_offset & PAGE_MASK); 2346 2347 for (i = bp->b_npages - 1; i >= 0; --i) { 2348 if (bp->b_pages[i]->dirty) { 2349 break; 2350 } 2351 } 2352 eoffset = ((i + 1) << PAGE_SHIFT) - (bp->b_offset & PAGE_MASK); 2353 2354 vm_page_unlock_queues(); 2355 /* 2356 * Fit it to the buffer. 2357 */ 2358 2359 if (eoffset > bp->b_bcount) 2360 eoffset = bp->b_bcount; 2361 2362 /* 2363 * If we have a good dirty range, merge with the existing 2364 * dirty range. 2365 */ 2366 2367 if (boffset < eoffset) { 2368 if (bp->b_dirtyoff > boffset) 2369 bp->b_dirtyoff = boffset; 2370 if (bp->b_dirtyend < eoffset) 2371 bp->b_dirtyend = eoffset; 2372 } 2373 } 2374} 2375 2376/* 2377 * getblk: 2378 * 2379 * Get a block given a specified block and offset into a file/device. 2380 * The buffers B_DONE bit will be cleared on return, making it almost 2381 * ready for an I/O initiation. B_INVAL may or may not be set on 2382 * return. The caller should clear B_INVAL prior to initiating a 2383 * READ. 2384 * 2385 * For a non-VMIO buffer, B_CACHE is set to the opposite of B_INVAL for 2386 * an existing buffer. 2387 * 2388 * For a VMIO buffer, B_CACHE is modified according to the backing VM. 2389 * If getblk()ing a previously 0-sized invalid buffer, B_CACHE is set 2390 * and then cleared based on the backing VM. If the previous buffer is 2391 * non-0-sized but invalid, B_CACHE will be cleared. 2392 * 2393 * If getblk() must create a new buffer, the new buffer is returned with 2394 * both B_INVAL and B_CACHE clear unless it is a VMIO buffer, in which 2395 * case it is returned with B_INVAL clear and B_CACHE set based on the 2396 * backing VM. 2397 * 2398 * getblk() also forces a bwrite() for any B_DELWRI buffer whos 2399 * B_CACHE bit is clear. 2400 * 2401 * What this means, basically, is that the caller should use B_CACHE to 2402 * determine whether the buffer is fully valid or not and should clear 2403 * B_INVAL prior to issuing a read. If the caller intends to validate 2404 * the buffer by loading its data area with something, the caller needs 2405 * to clear B_INVAL. If the caller does this without issuing an I/O, 2406 * the caller should set B_CACHE ( as an optimization ), else the caller 2407 * should issue the I/O and biodone() will set B_CACHE if the I/O was 2408 * a write attempt or if it was a successfull read. If the caller 2409 * intends to issue a READ, the caller must clear B_INVAL and BIO_ERROR 2410 * prior to issuing the READ. biodone() will *not* clear B_INVAL. 2411 */ 2412struct buf * 2413getblk(struct vnode * vp, daddr_t blkno, int size, int slpflag, int slptimeo, 2414 int flags) 2415{ 2416 struct buf *bp; 2417 struct bufobj *bo; 2418 int error; 2419 2420 CTR3(KTR_BUF, "getblk(%p, %ld, %d)", vp, (long)blkno, size); 2421 ASSERT_VOP_LOCKED(vp, "getblk"); 2422 if (size > MAXBSIZE) 2423 panic("getblk: size(%d) > MAXBSIZE(%d)\n", size, MAXBSIZE); 2424 2425 bo = &vp->v_bufobj; 2426loop: 2427 /* 2428 * Block if we are low on buffers. Certain processes are allowed 2429 * to completely exhaust the buffer cache. 2430 * 2431 * If this check ever becomes a bottleneck it may be better to 2432 * move it into the else, when gbincore() fails. At the moment 2433 * it isn't a problem. 2434 * 2435 * XXX remove if 0 sections (clean this up after its proven) 2436 */ 2437 if (numfreebuffers == 0) { 2438 if (TD_IS_IDLETHREAD(curthread)) 2439 return NULL; 2440 mtx_lock(&nblock); 2441 needsbuffer |= VFS_BIO_NEED_ANY; 2442 mtx_unlock(&nblock); 2443 } 2444 2445 BO_LOCK(bo); 2446 bp = gbincore(bo, blkno); 2447 if (bp != NULL) { 2448 int lockflags; 2449 /* 2450 * Buffer is in-core. If the buffer is not busy, it must 2451 * be on a queue. 2452 */ 2453 lockflags = LK_EXCLUSIVE | LK_SLEEPFAIL | LK_INTERLOCK; 2454 2455 if (flags & GB_LOCK_NOWAIT) 2456 lockflags |= LK_NOWAIT; 2457 2458 error = BUF_TIMELOCK(bp, lockflags, 2459 VI_MTX(vp), "getblk", slpflag, slptimeo); 2460 2461 /* 2462 * If we slept and got the lock we have to restart in case 2463 * the buffer changed identities. 2464 */ 2465 if (error == ENOLCK) 2466 goto loop; 2467 /* We timed out or were interrupted. */ 2468 else if (error) 2469 return (NULL); 2470 2471 /* 2472 * The buffer is locked. B_CACHE is cleared if the buffer is 2473 * invalid. Otherwise, for a non-VMIO buffer, B_CACHE is set 2474 * and for a VMIO buffer B_CACHE is adjusted according to the 2475 * backing VM cache. 2476 */ 2477 if (bp->b_flags & B_INVAL) 2478 bp->b_flags &= ~B_CACHE; 2479 else if ((bp->b_flags & (B_VMIO | B_INVAL)) == 0) 2480 bp->b_flags |= B_CACHE; 2481 bremfree(bp); 2482 2483 /* 2484 * check for size inconsistancies for non-VMIO case. 2485 */ 2486 2487 if (bp->b_bcount != size) { 2488 if ((bp->b_flags & B_VMIO) == 0 || 2489 (size > bp->b_kvasize)) { 2490 if (bp->b_flags & B_DELWRI) { 2491 /* 2492 * If buffer is pinned and caller does 2493 * not want sleep waiting for it to be 2494 * unpinned, bail out 2495 * */ 2496 if (bp->b_pin_count > 0) { 2497 if (flags & GB_LOCK_NOWAIT) { 2498 bqrelse(bp); 2499 return (NULL); 2500 } else { 2501 bunpin_wait(bp); 2502 } 2503 } 2504 bp->b_flags |= B_NOCACHE; 2505 bwrite(bp); 2506 } else { 2507 if (LIST_EMPTY(&bp->b_dep)) { 2508 bp->b_flags |= B_RELBUF; 2509 brelse(bp); 2510 } else { 2511 bp->b_flags |= B_NOCACHE; 2512 bwrite(bp); 2513 } 2514 } 2515 goto loop; 2516 } 2517 } 2518 2519 /* 2520 * If the size is inconsistant in the VMIO case, we can resize 2521 * the buffer. This might lead to B_CACHE getting set or 2522 * cleared. If the size has not changed, B_CACHE remains 2523 * unchanged from its previous state. 2524 */ 2525 2526 if (bp->b_bcount != size) 2527 allocbuf(bp, size); 2528 2529 KASSERT(bp->b_offset != NOOFFSET, 2530 ("getblk: no buffer offset")); 2531 2532 /* 2533 * A buffer with B_DELWRI set and B_CACHE clear must 2534 * be committed before we can return the buffer in 2535 * order to prevent the caller from issuing a read 2536 * ( due to B_CACHE not being set ) and overwriting 2537 * it. 2538 * 2539 * Most callers, including NFS and FFS, need this to 2540 * operate properly either because they assume they 2541 * can issue a read if B_CACHE is not set, or because 2542 * ( for example ) an uncached B_DELWRI might loop due 2543 * to softupdates re-dirtying the buffer. In the latter 2544 * case, B_CACHE is set after the first write completes, 2545 * preventing further loops. 2546 * NOTE! b*write() sets B_CACHE. If we cleared B_CACHE 2547 * above while extending the buffer, we cannot allow the 2548 * buffer to remain with B_CACHE set after the write 2549 * completes or it will represent a corrupt state. To 2550 * deal with this we set B_NOCACHE to scrap the buffer 2551 * after the write. 2552 * 2553 * We might be able to do something fancy, like setting 2554 * B_CACHE in bwrite() except if B_DELWRI is already set, 2555 * so the below call doesn't set B_CACHE, but that gets real 2556 * confusing. This is much easier. 2557 */ 2558 2559 if ((bp->b_flags & (B_CACHE|B_DELWRI)) == B_DELWRI) { 2560 bp->b_flags |= B_NOCACHE; 2561 bwrite(bp); 2562 goto loop; 2563 } 2564 bp->b_flags &= ~B_DONE; 2565 } else { 2566 int bsize, maxsize, vmio; 2567 off_t offset; 2568 2569 /* 2570 * Buffer is not in-core, create new buffer. The buffer 2571 * returned by getnewbuf() is locked. Note that the returned 2572 * buffer is also considered valid (not marked B_INVAL). 2573 */ 2574 BO_UNLOCK(bo); 2575 /* 2576 * If the user does not want us to create the buffer, bail out 2577 * here. 2578 */ 2579 if (flags & GB_NOCREAT) 2580 return NULL; 2581 bsize = bo->bo_bsize; 2582 offset = blkno * bsize; 2583 vmio = vp->v_object != NULL; 2584 maxsize = vmio ? size + (offset & PAGE_MASK) : size; 2585 maxsize = imax(maxsize, bsize); 2586 2587 bp = getnewbuf(slpflag, slptimeo, size, maxsize); 2588 if (bp == NULL) { 2589 if (slpflag || slptimeo) 2590 return NULL; 2591 goto loop; 2592 } 2593 2594 /* 2595 * This code is used to make sure that a buffer is not 2596 * created while the getnewbuf routine is blocked. 2597 * This can be a problem whether the vnode is locked or not. 2598 * If the buffer is created out from under us, we have to 2599 * throw away the one we just created. 2600 * 2601 * Note: this must occur before we associate the buffer 2602 * with the vp especially considering limitations in 2603 * the splay tree implementation when dealing with duplicate 2604 * lblkno's. 2605 */ 2606 BO_LOCK(bo); 2607 if (gbincore(bo, blkno)) { 2608 BO_UNLOCK(bo); 2609 bp->b_flags |= B_INVAL; 2610 brelse(bp); 2611 goto loop; 2612 } 2613 2614 /* 2615 * Insert the buffer into the hash, so that it can 2616 * be found by incore. 2617 */ 2618 bp->b_blkno = bp->b_lblkno = blkno; 2619 bp->b_offset = offset; 2620 bgetvp(vp, bp); 2621 BO_UNLOCK(bo); 2622 2623 /* 2624 * set B_VMIO bit. allocbuf() the buffer bigger. Since the 2625 * buffer size starts out as 0, B_CACHE will be set by 2626 * allocbuf() for the VMIO case prior to it testing the 2627 * backing store for validity. 2628 */ 2629 2630 if (vmio) { 2631 bp->b_flags |= B_VMIO; 2632#if defined(VFS_BIO_DEBUG) 2633 if (vn_canvmio(vp) != TRUE) 2634 printf("getblk: VMIO on vnode type %d\n", 2635 vp->v_type); 2636#endif 2637 KASSERT(vp->v_object == bp->b_bufobj->bo_object, 2638 ("ARGH! different b_bufobj->bo_object %p %p %p\n", 2639 bp, vp->v_object, bp->b_bufobj->bo_object)); 2640 } else { 2641 bp->b_flags &= ~B_VMIO; 2642 KASSERT(bp->b_bufobj->bo_object == NULL, 2643 ("ARGH! has b_bufobj->bo_object %p %p\n", 2644 bp, bp->b_bufobj->bo_object)); 2645 } 2646 2647 allocbuf(bp, size); 2648 bp->b_flags &= ~B_DONE; 2649 } 2650 CTR4(KTR_BUF, "getblk(%p, %ld, %d) = %p", vp, (long)blkno, size, bp); 2651 BUF_ASSERT_HELD(bp); 2652 KASSERT(bp->b_bufobj == bo, 2653 ("bp %p wrong b_bufobj %p should be %p", bp, bp->b_bufobj, bo)); 2654 return (bp); 2655} 2656 2657/* 2658 * Get an empty, disassociated buffer of given size. The buffer is initially 2659 * set to B_INVAL. 2660 */ 2661struct buf * 2662geteblk(int size) 2663{ 2664 struct buf *bp; 2665 int maxsize; 2666 2667 maxsize = (size + BKVAMASK) & ~BKVAMASK; 2668 while ((bp = getnewbuf(0, 0, size, maxsize)) == 0) 2669 continue; 2670 allocbuf(bp, size); 2671 bp->b_flags |= B_INVAL; /* b_dep cleared by getnewbuf() */ 2672 BUF_ASSERT_HELD(bp); 2673 return (bp); 2674} 2675 2676 2677/* 2678 * This code constitutes the buffer memory from either anonymous system 2679 * memory (in the case of non-VMIO operations) or from an associated 2680 * VM object (in the case of VMIO operations). This code is able to 2681 * resize a buffer up or down. 2682 * 2683 * Note that this code is tricky, and has many complications to resolve 2684 * deadlock or inconsistant data situations. Tread lightly!!! 2685 * There are B_CACHE and B_DELWRI interactions that must be dealt with by 2686 * the caller. Calling this code willy nilly can result in the loss of data. 2687 * 2688 * allocbuf() only adjusts B_CACHE for VMIO buffers. getblk() deals with 2689 * B_CACHE for the non-VMIO case. 2690 */ 2691 2692int 2693allocbuf(struct buf *bp, int size) 2694{ 2695 int newbsize, mbsize; 2696 int i; 2697 2698 BUF_ASSERT_HELD(bp); 2699 2700 if (bp->b_kvasize < size) 2701 panic("allocbuf: buffer too small"); 2702 2703 if ((bp->b_flags & B_VMIO) == 0) { 2704 caddr_t origbuf; 2705 int origbufsize; 2706 /* 2707 * Just get anonymous memory from the kernel. Don't 2708 * mess with B_CACHE. 2709 */ 2710 mbsize = (size + DEV_BSIZE - 1) & ~(DEV_BSIZE - 1); 2711 if (bp->b_flags & B_MALLOC) 2712 newbsize = mbsize; 2713 else 2714 newbsize = round_page(size); 2715 2716 if (newbsize < bp->b_bufsize) { 2717 /* 2718 * malloced buffers are not shrunk 2719 */ 2720 if (bp->b_flags & B_MALLOC) { 2721 if (newbsize) { 2722 bp->b_bcount = size; 2723 } else { 2724 free(bp->b_data, M_BIOBUF); 2725 if (bp->b_bufsize) { 2726 atomic_subtract_int( 2727 &bufmallocspace, 2728 bp->b_bufsize); 2729 bufspacewakeup(); 2730 bp->b_bufsize = 0; 2731 } 2732 bp->b_saveaddr = bp->b_kvabase; 2733 bp->b_data = bp->b_saveaddr; 2734 bp->b_bcount = 0; 2735 bp->b_flags &= ~B_MALLOC; 2736 } 2737 return 1; 2738 } 2739 vm_hold_free_pages( 2740 bp, 2741 (vm_offset_t) bp->b_data + newbsize, 2742 (vm_offset_t) bp->b_data + bp->b_bufsize); 2743 } else if (newbsize > bp->b_bufsize) { 2744 /* 2745 * We only use malloced memory on the first allocation. 2746 * and revert to page-allocated memory when the buffer 2747 * grows. 2748 */ 2749 /* 2750 * There is a potential smp race here that could lead 2751 * to bufmallocspace slightly passing the max. It 2752 * is probably extremely rare and not worth worrying 2753 * over. 2754 */ 2755 if ( (bufmallocspace < maxbufmallocspace) && 2756 (bp->b_bufsize == 0) && 2757 (mbsize <= PAGE_SIZE/2)) { 2758 2759 bp->b_data = malloc(mbsize, M_BIOBUF, M_WAITOK); 2760 bp->b_bufsize = mbsize; 2761 bp->b_bcount = size; 2762 bp->b_flags |= B_MALLOC; 2763 atomic_add_int(&bufmallocspace, mbsize); 2764 return 1; 2765 } 2766 origbuf = NULL; 2767 origbufsize = 0; 2768 /* 2769 * If the buffer is growing on its other-than-first allocation, 2770 * then we revert to the page-allocation scheme. 2771 */ 2772 if (bp->b_flags & B_MALLOC) { 2773 origbuf = bp->b_data; 2774 origbufsize = bp->b_bufsize; 2775 bp->b_data = bp->b_kvabase; 2776 if (bp->b_bufsize) { 2777 atomic_subtract_int(&bufmallocspace, 2778 bp->b_bufsize); 2779 bufspacewakeup(); 2780 bp->b_bufsize = 0; 2781 } 2782 bp->b_flags &= ~B_MALLOC; 2783 newbsize = round_page(newbsize); 2784 } 2785 vm_hold_load_pages( 2786 bp, 2787 (vm_offset_t) bp->b_data + bp->b_bufsize, 2788 (vm_offset_t) bp->b_data + newbsize); 2789 if (origbuf) { 2790 bcopy(origbuf, bp->b_data, origbufsize); 2791 free(origbuf, M_BIOBUF); 2792 } 2793 } 2794 } else { 2795 int desiredpages; 2796 2797 newbsize = (size + DEV_BSIZE - 1) & ~(DEV_BSIZE - 1); 2798 desiredpages = (size == 0) ? 0 : 2799 num_pages((bp->b_offset & PAGE_MASK) + newbsize); 2800 2801 if (bp->b_flags & B_MALLOC) 2802 panic("allocbuf: VMIO buffer can't be malloced"); 2803 /* 2804 * Set B_CACHE initially if buffer is 0 length or will become 2805 * 0-length. 2806 */ 2807 if (size == 0 || bp->b_bufsize == 0) 2808 bp->b_flags |= B_CACHE; 2809 2810 if (newbsize < bp->b_bufsize) { 2811 /* 2812 * DEV_BSIZE aligned new buffer size is less then the 2813 * DEV_BSIZE aligned existing buffer size. Figure out 2814 * if we have to remove any pages. 2815 */ 2816 if (desiredpages < bp->b_npages) { 2817 vm_page_t m; 2818 2819 VM_OBJECT_LOCK(bp->b_bufobj->bo_object); 2820 vm_page_lock_queues(); 2821 for (i = desiredpages; i < bp->b_npages; i++) { 2822 /* 2823 * the page is not freed here -- it 2824 * is the responsibility of 2825 * vnode_pager_setsize 2826 */ 2827 m = bp->b_pages[i]; 2828 KASSERT(m != bogus_page, 2829 ("allocbuf: bogus page found")); 2830 while (vm_page_sleep_if_busy(m, TRUE, "biodep")) 2831 vm_page_lock_queues(); 2832 2833 bp->b_pages[i] = NULL; 2834 vm_page_unwire(m, 0); 2835 } 2836 vm_page_unlock_queues(); 2837 VM_OBJECT_UNLOCK(bp->b_bufobj->bo_object); 2838 pmap_qremove((vm_offset_t) trunc_page((vm_offset_t)bp->b_data) + 2839 (desiredpages << PAGE_SHIFT), (bp->b_npages - desiredpages)); 2840 bp->b_npages = desiredpages; 2841 } 2842 } else if (size > bp->b_bcount) { 2843 /* 2844 * We are growing the buffer, possibly in a 2845 * byte-granular fashion. 2846 */ 2847 struct vnode *vp; 2848 vm_object_t obj; 2849 vm_offset_t toff; 2850 vm_offset_t tinc; 2851 2852 /* 2853 * Step 1, bring in the VM pages from the object, 2854 * allocating them if necessary. We must clear 2855 * B_CACHE if these pages are not valid for the 2856 * range covered by the buffer. 2857 */ 2858 2859 vp = bp->b_vp; 2860 obj = bp->b_bufobj->bo_object; 2861 2862 VM_OBJECT_LOCK(obj); 2863 while (bp->b_npages < desiredpages) { 2864 vm_page_t m; 2865 vm_pindex_t pi; 2866 2867 pi = OFF_TO_IDX(bp->b_offset) + bp->b_npages; 2868 if ((m = vm_page_lookup(obj, pi)) == NULL) { 2869 /* 2870 * note: must allocate system pages 2871 * since blocking here could intefere 2872 * with paging I/O, no matter which 2873 * process we are. 2874 */ 2875 m = vm_page_alloc(obj, pi, 2876 VM_ALLOC_NOBUSY | VM_ALLOC_SYSTEM | 2877 VM_ALLOC_WIRED); 2878 if (m == NULL) { 2879 atomic_add_int(&vm_pageout_deficit, 2880 desiredpages - bp->b_npages); 2881 VM_OBJECT_UNLOCK(obj); 2882 VM_WAIT; 2883 VM_OBJECT_LOCK(obj); 2884 } else { 2885 if (m->valid == 0) 2886 bp->b_flags &= ~B_CACHE; 2887 bp->b_pages[bp->b_npages] = m; 2888 ++bp->b_npages; 2889 } 2890 continue; 2891 } 2892 2893 /* 2894 * We found a page. If we have to sleep on it, 2895 * retry because it might have gotten freed out 2896 * from under us. 2897 * 2898 * We can only test VPO_BUSY here. Blocking on 2899 * m->busy might lead to a deadlock: 2900 * 2901 * vm_fault->getpages->cluster_read->allocbuf 2902 * 2903 */ 2904 if (vm_page_sleep_if_busy(m, FALSE, "pgtblk")) 2905 continue; 2906 2907 /* 2908 * We have a good page. 2909 */ 2910 vm_page_lock_queues(); 2911 vm_page_wire(m); 2912 vm_page_unlock_queues(); 2913 bp->b_pages[bp->b_npages] = m; 2914 ++bp->b_npages; 2915 } 2916 2917 /* 2918 * Step 2. We've loaded the pages into the buffer, 2919 * we have to figure out if we can still have B_CACHE 2920 * set. Note that B_CACHE is set according to the 2921 * byte-granular range ( bcount and size ), new the 2922 * aligned range ( newbsize ). 2923 * 2924 * The VM test is against m->valid, which is DEV_BSIZE 2925 * aligned. Needless to say, the validity of the data 2926 * needs to also be DEV_BSIZE aligned. Note that this 2927 * fails with NFS if the server or some other client 2928 * extends the file's EOF. If our buffer is resized, 2929 * B_CACHE may remain set! XXX 2930 */ 2931 2932 toff = bp->b_bcount; 2933 tinc = PAGE_SIZE - ((bp->b_offset + toff) & PAGE_MASK); 2934 2935 while ((bp->b_flags & B_CACHE) && toff < size) { 2936 vm_pindex_t pi; 2937 2938 if (tinc > (size - toff)) 2939 tinc = size - toff; 2940 2941 pi = ((bp->b_offset & PAGE_MASK) + toff) >> 2942 PAGE_SHIFT; 2943 2944 vfs_buf_test_cache( 2945 bp, 2946 bp->b_offset, 2947 toff, 2948 tinc, 2949 bp->b_pages[pi] 2950 ); 2951 toff += tinc; 2952 tinc = PAGE_SIZE; 2953 } 2954 VM_OBJECT_UNLOCK(obj); 2955 2956 /* 2957 * Step 3, fixup the KVM pmap. Remember that 2958 * bp->b_data is relative to bp->b_offset, but 2959 * bp->b_offset may be offset into the first page. 2960 */ 2961 2962 bp->b_data = (caddr_t) 2963 trunc_page((vm_offset_t)bp->b_data); 2964 pmap_qenter( 2965 (vm_offset_t)bp->b_data, 2966 bp->b_pages, 2967 bp->b_npages 2968 ); 2969 2970 bp->b_data = (caddr_t)((vm_offset_t)bp->b_data | 2971 (vm_offset_t)(bp->b_offset & PAGE_MASK)); 2972 } 2973 } 2974 if (newbsize < bp->b_bufsize) 2975 bufspacewakeup(); 2976 bp->b_bufsize = newbsize; /* actual buffer allocation */ 2977 bp->b_bcount = size; /* requested buffer size */ 2978 return 1; 2979} 2980 2981void 2982biodone(struct bio *bp) 2983{ 2984 struct mtx *mtxp; 2985 void (*done)(struct bio *); 2986 2987 mtxp = mtx_pool_find(mtxpool_sleep, bp); 2988 mtx_lock(mtxp); 2989 bp->bio_flags |= BIO_DONE; 2990 done = bp->bio_done; 2991 if (done == NULL) 2992 wakeup(bp); 2993 mtx_unlock(mtxp); 2994 if (done != NULL) 2995 done(bp); 2996} 2997 2998/* 2999 * Wait for a BIO to finish. 3000 * 3001 * XXX: resort to a timeout for now. The optimal locking (if any) for this 3002 * case is not yet clear. 3003 */ 3004int 3005biowait(struct bio *bp, const char *wchan) 3006{ 3007 struct mtx *mtxp; 3008 3009 mtxp = mtx_pool_find(mtxpool_sleep, bp); 3010 mtx_lock(mtxp); 3011 while ((bp->bio_flags & BIO_DONE) == 0) 3012 msleep(bp, mtxp, PRIBIO, wchan, hz / 10); 3013 mtx_unlock(mtxp); 3014 if (bp->bio_error != 0) 3015 return (bp->bio_error); 3016 if (!(bp->bio_flags & BIO_ERROR)) 3017 return (0); 3018 return (EIO); 3019} 3020 3021void 3022biofinish(struct bio *bp, struct devstat *stat, int error) 3023{ 3024 3025 if (error) { 3026 bp->bio_error = error; 3027 bp->bio_flags |= BIO_ERROR; 3028 } 3029 if (stat != NULL) 3030 devstat_end_transaction_bio(stat, bp); 3031 biodone(bp); 3032} 3033 3034/* 3035 * bufwait: 3036 * 3037 * Wait for buffer I/O completion, returning error status. The buffer 3038 * is left locked and B_DONE on return. B_EINTR is converted into an EINTR 3039 * error and cleared. 3040 */ 3041int 3042bufwait(struct buf *bp) 3043{ 3044 if (bp->b_iocmd == BIO_READ) 3045 bwait(bp, PRIBIO, "biord"); 3046 else 3047 bwait(bp, PRIBIO, "biowr"); 3048 if (bp->b_flags & B_EINTR) { 3049 bp->b_flags &= ~B_EINTR; 3050 return (EINTR); 3051 } 3052 if (bp->b_ioflags & BIO_ERROR) { 3053 return (bp->b_error ? bp->b_error : EIO); 3054 } else { 3055 return (0); 3056 } 3057} 3058 3059 /* 3060 * Call back function from struct bio back up to struct buf. 3061 */ 3062static void 3063bufdonebio(struct bio *bip) 3064{ 3065 struct buf *bp; 3066 3067 bp = bip->bio_caller2; 3068 bp->b_resid = bp->b_bcount - bip->bio_completed; 3069 bp->b_resid = bip->bio_resid; /* XXX: remove */ 3070 bp->b_ioflags = bip->bio_flags; 3071 bp->b_error = bip->bio_error; 3072 if (bp->b_error) 3073 bp->b_ioflags |= BIO_ERROR; 3074 bufdone(bp); 3075 g_destroy_bio(bip); 3076} 3077 3078void 3079dev_strategy(struct cdev *dev, struct buf *bp) 3080{ 3081 struct cdevsw *csw; 3082 struct bio *bip; 3083 3084 if ((!bp->b_iocmd) || (bp->b_iocmd & (bp->b_iocmd - 1))) 3085 panic("b_iocmd botch"); 3086 for (;;) { 3087 bip = g_new_bio(); 3088 if (bip != NULL) 3089 break; 3090 /* Try again later */ 3091 tsleep(&bp, PRIBIO, "dev_strat", hz/10); 3092 } 3093 bip->bio_cmd = bp->b_iocmd; 3094 bip->bio_offset = bp->b_iooffset; 3095 bip->bio_length = bp->b_bcount; 3096 bip->bio_bcount = bp->b_bcount; /* XXX: remove */ 3097 bip->bio_data = bp->b_data; 3098 bip->bio_done = bufdonebio; 3099 bip->bio_caller2 = bp; 3100 bip->bio_dev = dev; 3101 KASSERT(dev->si_refcount > 0, 3102 ("dev_strategy on un-referenced struct cdev *(%s)", 3103 devtoname(dev))); 3104 csw = dev_refthread(dev); 3105 if (csw == NULL) { 3106 g_destroy_bio(bip); 3107 bp->b_error = ENXIO; 3108 bp->b_ioflags = BIO_ERROR; 3109 bufdone(bp); 3110 return; 3111 } 3112 (*csw->d_strategy)(bip); 3113 dev_relthread(dev); 3114} 3115 3116/* 3117 * bufdone: 3118 * 3119 * Finish I/O on a buffer, optionally calling a completion function. 3120 * This is usually called from an interrupt so process blocking is 3121 * not allowed. 3122 * 3123 * biodone is also responsible for setting B_CACHE in a B_VMIO bp. 3124 * In a non-VMIO bp, B_CACHE will be set on the next getblk() 3125 * assuming B_INVAL is clear. 3126 * 3127 * For the VMIO case, we set B_CACHE if the op was a read and no 3128 * read error occured, or if the op was a write. B_CACHE is never 3129 * set if the buffer is invalid or otherwise uncacheable. 3130 * 3131 * biodone does not mess with B_INVAL, allowing the I/O routine or the 3132 * initiator to leave B_INVAL set to brelse the buffer out of existance 3133 * in the biodone routine. 3134 */ 3135void 3136bufdone(struct buf *bp) 3137{ 3138 struct bufobj *dropobj; 3139 void (*biodone)(struct buf *); 3140 3141 CTR3(KTR_BUF, "bufdone(%p) vp %p flags %X", bp, bp->b_vp, bp->b_flags); 3142 dropobj = NULL; 3143 3144 KASSERT(!(bp->b_flags & B_DONE), ("biodone: bp %p already done", bp)); 3145 BUF_ASSERT_HELD(bp); 3146 3147 runningbufwakeup(bp); 3148 if (bp->b_iocmd == BIO_WRITE) 3149 dropobj = bp->b_bufobj; 3150 /* call optional completion function if requested */ 3151 if (bp->b_iodone != NULL) { 3152 biodone = bp->b_iodone; 3153 bp->b_iodone = NULL; 3154 (*biodone) (bp); 3155 if (dropobj) 3156 bufobj_wdrop(dropobj); 3157 return; 3158 } 3159 3160 bufdone_finish(bp); 3161 3162 if (dropobj) 3163 bufobj_wdrop(dropobj); 3164} 3165 3166void 3167bufdone_finish(struct buf *bp) 3168{ 3169 BUF_ASSERT_HELD(bp); 3170 3171 if (!LIST_EMPTY(&bp->b_dep)) 3172 buf_complete(bp); 3173 3174 if (bp->b_flags & B_VMIO) { 3175 int i; 3176 vm_ooffset_t foff; 3177 vm_page_t m; 3178 vm_object_t obj; 3179 int iosize; 3180 struct vnode *vp = bp->b_vp; 3181 boolean_t are_queues_locked; 3182 3183 obj = bp->b_bufobj->bo_object; 3184 3185#if defined(VFS_BIO_DEBUG) 3186 mp_fixme("usecount and vflag accessed without locks."); 3187 if (vp->v_usecount == 0) { 3188 panic("biodone: zero vnode ref count"); 3189 } 3190 3191 KASSERT(vp->v_object != NULL, 3192 ("biodone: vnode %p has no vm_object", vp)); 3193#endif 3194 3195 foff = bp->b_offset; 3196 KASSERT(bp->b_offset != NOOFFSET, 3197 ("biodone: no buffer offset")); 3198 3199 VM_OBJECT_LOCK(obj); 3200#if defined(VFS_BIO_DEBUG) 3201 if (obj->paging_in_progress < bp->b_npages) { 3202 printf("biodone: paging in progress(%d) < bp->b_npages(%d)\n", 3203 obj->paging_in_progress, bp->b_npages); 3204 } 3205#endif 3206 3207 /* 3208 * Set B_CACHE if the op was a normal read and no error 3209 * occured. B_CACHE is set for writes in the b*write() 3210 * routines. 3211 */ 3212 iosize = bp->b_bcount - bp->b_resid; 3213 if (bp->b_iocmd == BIO_READ && 3214 !(bp->b_flags & (B_INVAL|B_NOCACHE)) && 3215 !(bp->b_ioflags & BIO_ERROR)) { 3216 bp->b_flags |= B_CACHE; 3217 } 3218 if (bp->b_iocmd == BIO_READ) { 3219 vm_page_lock_queues(); 3220 are_queues_locked = TRUE; 3221 } else 3222 are_queues_locked = FALSE; 3223 for (i = 0; i < bp->b_npages; i++) { 3224 int bogusflag = 0; 3225 int resid; 3226 3227 resid = ((foff + PAGE_SIZE) & ~(off_t)PAGE_MASK) - foff; 3228 if (resid > iosize) 3229 resid = iosize; 3230 3231 /* 3232 * cleanup bogus pages, restoring the originals 3233 */ 3234 m = bp->b_pages[i]; 3235 if (m == bogus_page) { 3236 bogusflag = 1; 3237 m = vm_page_lookup(obj, OFF_TO_IDX(foff)); 3238 if (m == NULL) 3239 panic("biodone: page disappeared!"); 3240 bp->b_pages[i] = m; 3241 pmap_qenter(trunc_page((vm_offset_t)bp->b_data), 3242 bp->b_pages, bp->b_npages); 3243 } 3244#if defined(VFS_BIO_DEBUG) 3245 if (OFF_TO_IDX(foff) != m->pindex) { 3246 printf( 3247"biodone: foff(%jd)/m->pindex(%ju) mismatch\n", 3248 (intmax_t)foff, (uintmax_t)m->pindex); 3249 } 3250#endif 3251 3252 /* 3253 * In the write case, the valid and clean bits are 3254 * already changed correctly ( see bdwrite() ), so we 3255 * only need to do this here in the read case. 3256 */ 3257 if ((bp->b_iocmd == BIO_READ) && !bogusflag && resid > 0) { 3258 vfs_page_set_valid(bp, foff, m); 3259 } 3260 3261 /* 3262 * when debugging new filesystems or buffer I/O methods, this 3263 * is the most common error that pops up. if you see this, you 3264 * have not set the page busy flag correctly!!! 3265 */ 3266 if (m->busy == 0) { 3267 printf("biodone: page busy < 0, " 3268 "pindex: %d, foff: 0x(%x,%x), " 3269 "resid: %d, index: %d\n", 3270 (int) m->pindex, (int)(foff >> 32), 3271 (int) foff & 0xffffffff, resid, i); 3272 if (!vn_isdisk(vp, NULL)) 3273 printf(" iosize: %jd, lblkno: %jd, flags: 0x%x, npages: %d\n", 3274 (intmax_t)bp->b_vp->v_mount->mnt_stat.f_iosize, 3275 (intmax_t) bp->b_lblkno, 3276 bp->b_flags, bp->b_npages); 3277 else 3278 printf(" VDEV, lblkno: %jd, flags: 0x%x, npages: %d\n", 3279 (intmax_t) bp->b_lblkno, 3280 bp->b_flags, bp->b_npages); 3281 printf(" valid: 0x%lx, dirty: 0x%lx, wired: %d\n", 3282 (u_long)m->valid, (u_long)m->dirty, 3283 m->wire_count); 3284 panic("biodone: page busy < 0\n"); 3285 } 3286 vm_page_io_finish(m); 3287 vm_object_pip_subtract(obj, 1); 3288 foff = (foff + PAGE_SIZE) & ~(off_t)PAGE_MASK; 3289 iosize -= resid; 3290 } 3291 if (are_queues_locked) 3292 vm_page_unlock_queues(); 3293 vm_object_pip_wakeupn(obj, 0); 3294 VM_OBJECT_UNLOCK(obj); 3295 } 3296 3297 /* 3298 * For asynchronous completions, release the buffer now. The brelse 3299 * will do a wakeup there if necessary - so no need to do a wakeup 3300 * here in the async case. The sync case always needs to do a wakeup. 3301 */ 3302 3303 if (bp->b_flags & B_ASYNC) { 3304 if ((bp->b_flags & (B_NOCACHE | B_INVAL | B_RELBUF)) || (bp->b_ioflags & BIO_ERROR)) 3305 brelse(bp); 3306 else 3307 bqrelse(bp); 3308 } else 3309 bdone(bp); 3310} 3311 3312/* 3313 * This routine is called in lieu of iodone in the case of 3314 * incomplete I/O. This keeps the busy status for pages 3315 * consistant. 3316 */ 3317void 3318vfs_unbusy_pages(struct buf *bp) 3319{ 3320 int i; 3321 vm_object_t obj; 3322 vm_page_t m; 3323 3324 runningbufwakeup(bp); 3325 if (!(bp->b_flags & B_VMIO)) 3326 return; 3327 3328 obj = bp->b_bufobj->bo_object; 3329 VM_OBJECT_LOCK(obj); 3330 for (i = 0; i < bp->b_npages; i++) { 3331 m = bp->b_pages[i]; 3332 if (m == bogus_page) { 3333 m = vm_page_lookup(obj, OFF_TO_IDX(bp->b_offset) + i); 3334 if (!m) 3335 panic("vfs_unbusy_pages: page missing\n"); 3336 bp->b_pages[i] = m; 3337 pmap_qenter(trunc_page((vm_offset_t)bp->b_data), 3338 bp->b_pages, bp->b_npages); 3339 } 3340 vm_object_pip_subtract(obj, 1); 3341 vm_page_io_finish(m); 3342 } 3343 vm_object_pip_wakeupn(obj, 0); 3344 VM_OBJECT_UNLOCK(obj); 3345} 3346 3347/* 3348 * vfs_page_set_valid: 3349 * 3350 * Set the valid bits in a page based on the supplied offset. The 3351 * range is restricted to the buffer's size. 3352 * 3353 * This routine is typically called after a read completes. 3354 */ 3355static void 3356vfs_page_set_valid(struct buf *bp, vm_ooffset_t off, vm_page_t m) 3357{ 3358 vm_ooffset_t soff, eoff; 3359 3360 mtx_assert(&vm_page_queue_mtx, MA_OWNED); 3361 /* 3362 * Start and end offsets in buffer. eoff - soff may not cross a 3363 * page boundry or cross the end of the buffer. The end of the 3364 * buffer, in this case, is our file EOF, not the allocation size 3365 * of the buffer. 3366 */ 3367 soff = off; 3368 eoff = (off + PAGE_SIZE) & ~(off_t)PAGE_MASK; 3369 if (eoff > bp->b_offset + bp->b_bcount) 3370 eoff = bp->b_offset + bp->b_bcount; 3371 3372 /* 3373 * Set valid range. This is typically the entire buffer and thus the 3374 * entire page. 3375 */ 3376 if (eoff > soff) { 3377 vm_page_set_validclean( 3378 m, 3379 (vm_offset_t) (soff & PAGE_MASK), 3380 (vm_offset_t) (eoff - soff) 3381 ); 3382 } 3383} 3384 3385/* 3386 * This routine is called before a device strategy routine. 3387 * It is used to tell the VM system that paging I/O is in 3388 * progress, and treat the pages associated with the buffer 3389 * almost as being VPO_BUSY. Also the object paging_in_progress 3390 * flag is handled to make sure that the object doesn't become 3391 * inconsistant. 3392 * 3393 * Since I/O has not been initiated yet, certain buffer flags 3394 * such as BIO_ERROR or B_INVAL may be in an inconsistant state 3395 * and should be ignored. 3396 */ 3397void 3398vfs_busy_pages(struct buf *bp, int clear_modify) 3399{ 3400 int i, bogus; 3401 vm_object_t obj; 3402 vm_ooffset_t foff; 3403 vm_page_t m; 3404 3405 if (!(bp->b_flags & B_VMIO)) 3406 return; 3407 3408 obj = bp->b_bufobj->bo_object; 3409 foff = bp->b_offset; 3410 KASSERT(bp->b_offset != NOOFFSET, 3411 ("vfs_busy_pages: no buffer offset")); 3412 VM_OBJECT_LOCK(obj); 3413 if (bp->b_bufsize != 0) 3414 vfs_setdirty_locked_object(bp); 3415retry: 3416 for (i = 0; i < bp->b_npages; i++) { 3417 m = bp->b_pages[i]; 3418 3419 if (vm_page_sleep_if_busy(m, FALSE, "vbpage")) 3420 goto retry; 3421 } 3422 bogus = 0; 3423 vm_page_lock_queues(); 3424 for (i = 0; i < bp->b_npages; i++) { 3425 m = bp->b_pages[i]; 3426 3427 if ((bp->b_flags & B_CLUSTER) == 0) { 3428 vm_object_pip_add(obj, 1); 3429 vm_page_io_start(m); 3430 } 3431 /* 3432 * When readying a buffer for a read ( i.e 3433 * clear_modify == 0 ), it is important to do 3434 * bogus_page replacement for valid pages in 3435 * partially instantiated buffers. Partially 3436 * instantiated buffers can, in turn, occur when 3437 * reconstituting a buffer from its VM backing store 3438 * base. We only have to do this if B_CACHE is 3439 * clear ( which causes the I/O to occur in the 3440 * first place ). The replacement prevents the read 3441 * I/O from overwriting potentially dirty VM-backed 3442 * pages. XXX bogus page replacement is, uh, bogus. 3443 * It may not work properly with small-block devices. 3444 * We need to find a better way. 3445 */ 3446 pmap_remove_all(m); 3447 if (clear_modify) 3448 vfs_page_set_valid(bp, foff, m); 3449 else if (m->valid == VM_PAGE_BITS_ALL && 3450 (bp->b_flags & B_CACHE) == 0) { 3451 bp->b_pages[i] = bogus_page; 3452 bogus++; 3453 } 3454 foff = (foff + PAGE_SIZE) & ~(off_t)PAGE_MASK; 3455 } 3456 vm_page_unlock_queues(); 3457 VM_OBJECT_UNLOCK(obj); 3458 if (bogus) 3459 pmap_qenter(trunc_page((vm_offset_t)bp->b_data), 3460 bp->b_pages, bp->b_npages); 3461} 3462 3463/* 3464 * Tell the VM system that the pages associated with this buffer 3465 * are clean. This is used for delayed writes where the data is 3466 * going to go to disk eventually without additional VM intevention. 3467 * 3468 * Note that while we only really need to clean through to b_bcount, we 3469 * just go ahead and clean through to b_bufsize. 3470 */ 3471static void 3472vfs_clean_pages(struct buf *bp) 3473{ 3474 int i; 3475 vm_ooffset_t foff, noff, eoff; 3476 vm_page_t m; 3477 3478 if (!(bp->b_flags & B_VMIO)) 3479 return; 3480 3481 foff = bp->b_offset; 3482 KASSERT(bp->b_offset != NOOFFSET, 3483 ("vfs_clean_pages: no buffer offset")); 3484 VM_OBJECT_LOCK(bp->b_bufobj->bo_object); 3485 vm_page_lock_queues(); 3486 for (i = 0; i < bp->b_npages; i++) { 3487 m = bp->b_pages[i]; 3488 noff = (foff + PAGE_SIZE) & ~(off_t)PAGE_MASK; 3489 eoff = noff; 3490 3491 if (eoff > bp->b_offset + bp->b_bufsize) 3492 eoff = bp->b_offset + bp->b_bufsize; 3493 vfs_page_set_valid(bp, foff, m); 3494 /* vm_page_clear_dirty(m, foff & PAGE_MASK, eoff - foff); */ 3495 foff = noff; 3496 } 3497 vm_page_unlock_queues(); 3498 VM_OBJECT_UNLOCK(bp->b_bufobj->bo_object); 3499} 3500 3501/* 3502 * vfs_bio_set_validclean: 3503 * 3504 * Set the range within the buffer to valid and clean. The range is 3505 * relative to the beginning of the buffer, b_offset. Note that b_offset 3506 * itself may be offset from the beginning of the first page. 3507 * 3508 */ 3509 3510void 3511vfs_bio_set_validclean(struct buf *bp, int base, int size) 3512{ 3513 int i, n; 3514 vm_page_t m; 3515 3516 if (!(bp->b_flags & B_VMIO)) 3517 return; 3518 /* 3519 * Fixup base to be relative to beginning of first page. 3520 * Set initial n to be the maximum number of bytes in the 3521 * first page that can be validated. 3522 */ 3523 3524 base += (bp->b_offset & PAGE_MASK); 3525 n = PAGE_SIZE - (base & PAGE_MASK); 3526 3527 VM_OBJECT_LOCK(bp->b_bufobj->bo_object); 3528 vm_page_lock_queues(); 3529 for (i = base / PAGE_SIZE; size > 0 && i < bp->b_npages; ++i) { 3530 m = bp->b_pages[i]; 3531 if (n > size) 3532 n = size; 3533 vm_page_set_validclean(m, base & PAGE_MASK, n); 3534 base += n; 3535 size -= n; 3536 n = PAGE_SIZE; 3537 } 3538 vm_page_unlock_queues(); 3539 VM_OBJECT_UNLOCK(bp->b_bufobj->bo_object); 3540} 3541 3542/* 3543 * vfs_bio_clrbuf: 3544 * 3545 * clear a buffer. This routine essentially fakes an I/O, so we need 3546 * to clear BIO_ERROR and B_INVAL. 3547 * 3548 * Note that while we only theoretically need to clear through b_bcount, 3549 * we go ahead and clear through b_bufsize. 3550 */ 3551 3552void 3553vfs_bio_clrbuf(struct buf *bp) 3554{ 3555 int i, j, mask = 0; 3556 caddr_t sa, ea; 3557 3558 if ((bp->b_flags & (B_VMIO | B_MALLOC)) != B_VMIO) { 3559 clrbuf(bp); 3560 return; 3561 } 3562 3563 bp->b_flags &= ~B_INVAL; 3564 bp->b_ioflags &= ~BIO_ERROR; 3565 VM_OBJECT_LOCK(bp->b_bufobj->bo_object); 3566 if ((bp->b_npages == 1) && (bp->b_bufsize < PAGE_SIZE) && 3567 (bp->b_offset & PAGE_MASK) == 0) { 3568 if (bp->b_pages[0] == bogus_page) 3569 goto unlock; 3570 mask = (1 << (bp->b_bufsize / DEV_BSIZE)) - 1; 3571 VM_OBJECT_LOCK_ASSERT(bp->b_pages[0]->object, MA_OWNED); 3572 if ((bp->b_pages[0]->valid & mask) == mask) 3573 goto unlock; 3574 if (((bp->b_pages[0]->flags & PG_ZERO) == 0) && 3575 ((bp->b_pages[0]->valid & mask) == 0)) { 3576 bzero(bp->b_data, bp->b_bufsize); 3577 bp->b_pages[0]->valid |= mask; 3578 goto unlock; 3579 } 3580 } 3581 ea = sa = bp->b_data; 3582 for(i = 0; i < bp->b_npages; i++, sa = ea) { 3583 ea = (caddr_t)trunc_page((vm_offset_t)sa + PAGE_SIZE); 3584 ea = (caddr_t)(vm_offset_t)ulmin( 3585 (u_long)(vm_offset_t)ea, 3586 (u_long)(vm_offset_t)bp->b_data + bp->b_bufsize); 3587 if (bp->b_pages[i] == bogus_page) 3588 continue; 3589 j = ((vm_offset_t)sa & PAGE_MASK) / DEV_BSIZE; 3590 mask = ((1 << ((ea - sa) / DEV_BSIZE)) - 1) << j; 3591 VM_OBJECT_LOCK_ASSERT(bp->b_pages[i]->object, MA_OWNED); 3592 if ((bp->b_pages[i]->valid & mask) == mask) 3593 continue; 3594 if ((bp->b_pages[i]->valid & mask) == 0) { 3595 if ((bp->b_pages[i]->flags & PG_ZERO) == 0) 3596 bzero(sa, ea - sa); 3597 } else { 3598 for (; sa < ea; sa += DEV_BSIZE, j++) { 3599 if (((bp->b_pages[i]->flags & PG_ZERO) == 0) && 3600 (bp->b_pages[i]->valid & (1 << j)) == 0) 3601 bzero(sa, DEV_BSIZE); 3602 } 3603 } 3604 bp->b_pages[i]->valid |= mask; 3605 } 3606unlock: 3607 VM_OBJECT_UNLOCK(bp->b_bufobj->bo_object); 3608 bp->b_resid = 0; 3609} 3610 3611/* 3612 * vm_hold_load_pages and vm_hold_free_pages get pages into 3613 * a buffers address space. The pages are anonymous and are 3614 * not associated with a file object. 3615 */ 3616static void 3617vm_hold_load_pages(struct buf *bp, vm_offset_t from, vm_offset_t to) 3618{ 3619 vm_offset_t pg; 3620 vm_page_t p; 3621 int index; 3622 3623 to = round_page(to); 3624 from = round_page(from); 3625 index = (from - trunc_page((vm_offset_t)bp->b_data)) >> PAGE_SHIFT; 3626 3627 VM_OBJECT_LOCK(kernel_object); 3628 for (pg = from; pg < to; pg += PAGE_SIZE, index++) { 3629tryagain: 3630 /* 3631 * note: must allocate system pages since blocking here 3632 * could intefere with paging I/O, no matter which 3633 * process we are. 3634 */ 3635 p = vm_page_alloc(kernel_object, 3636 ((pg - VM_MIN_KERNEL_ADDRESS) >> PAGE_SHIFT), 3637 VM_ALLOC_NOBUSY | VM_ALLOC_SYSTEM | VM_ALLOC_WIRED); 3638 if (!p) { 3639 atomic_add_int(&vm_pageout_deficit, 3640 (to - pg) >> PAGE_SHIFT); 3641 VM_OBJECT_UNLOCK(kernel_object); 3642 VM_WAIT; 3643 VM_OBJECT_LOCK(kernel_object); 3644 goto tryagain; 3645 } 3646 p->valid = VM_PAGE_BITS_ALL; 3647 pmap_qenter(pg, &p, 1); 3648 bp->b_pages[index] = p; 3649 } 3650 VM_OBJECT_UNLOCK(kernel_object); 3651 bp->b_npages = index; 3652} 3653 3654/* Return pages associated with this buf to the vm system */ 3655static void 3656vm_hold_free_pages(struct buf *bp, vm_offset_t from, vm_offset_t to) 3657{ 3658 vm_offset_t pg; 3659 vm_page_t p; 3660 int index, newnpages; 3661 3662 from = round_page(from); 3663 to = round_page(to); 3664 newnpages = index = (from - trunc_page((vm_offset_t)bp->b_data)) >> PAGE_SHIFT; 3665 3666 VM_OBJECT_LOCK(kernel_object); 3667 for (pg = from; pg < to; pg += PAGE_SIZE, index++) { 3668 p = bp->b_pages[index]; 3669 if (p && (index < bp->b_npages)) { 3670 if (p->busy) { 3671 printf( 3672 "vm_hold_free_pages: blkno: %jd, lblkno: %jd\n", 3673 (intmax_t)bp->b_blkno, 3674 (intmax_t)bp->b_lblkno); 3675 } 3676 bp->b_pages[index] = NULL; 3677 pmap_qremove(pg, 1); 3678 vm_page_lock_queues(); 3679 vm_page_unwire(p, 0); 3680 vm_page_free(p); 3681 vm_page_unlock_queues(); 3682 } 3683 } 3684 VM_OBJECT_UNLOCK(kernel_object); 3685 bp->b_npages = newnpages; 3686} 3687 3688/* 3689 * Map an IO request into kernel virtual address space. 3690 * 3691 * All requests are (re)mapped into kernel VA space. 3692 * Notice that we use b_bufsize for the size of the buffer 3693 * to be mapped. b_bcount might be modified by the driver. 3694 * 3695 * Note that even if the caller determines that the address space should 3696 * be valid, a race or a smaller-file mapped into a larger space may 3697 * actually cause vmapbuf() to fail, so all callers of vmapbuf() MUST 3698 * check the return value. 3699 */ 3700int 3701vmapbuf(struct buf *bp) 3702{ 3703 caddr_t addr, kva; 3704 vm_prot_t prot; 3705 int pidx, i; 3706 struct vm_page *m; 3707 struct pmap *pmap = &curproc->p_vmspace->vm_pmap; 3708 3709 if (bp->b_bufsize < 0) 3710 return (-1); 3711 prot = VM_PROT_READ; 3712 if (bp->b_iocmd == BIO_READ) 3713 prot |= VM_PROT_WRITE; /* Less backwards than it looks */ 3714 for (addr = (caddr_t)trunc_page((vm_offset_t)bp->b_data), pidx = 0; 3715 addr < bp->b_data + bp->b_bufsize; 3716 addr += PAGE_SIZE, pidx++) { 3717 /* 3718 * Do the vm_fault if needed; do the copy-on-write thing 3719 * when reading stuff off device into memory. 3720 * 3721 * NOTE! Must use pmap_extract() because addr may be in 3722 * the userland address space, and kextract is only guarenteed 3723 * to work for the kernland address space (see: sparc64 port). 3724 */ 3725retry: 3726 if (vm_fault_quick(addr >= bp->b_data ? addr : bp->b_data, 3727 prot) < 0) { 3728 vm_page_lock_queues(); 3729 for (i = 0; i < pidx; ++i) { 3730 vm_page_unhold(bp->b_pages[i]); 3731 bp->b_pages[i] = NULL; 3732 } 3733 vm_page_unlock_queues(); 3734 return(-1); 3735 } 3736 m = pmap_extract_and_hold(pmap, (vm_offset_t)addr, prot); 3737 if (m == NULL) 3738 goto retry; 3739 bp->b_pages[pidx] = m; 3740 } 3741 if (pidx > btoc(MAXPHYS)) 3742 panic("vmapbuf: mapped more than MAXPHYS"); 3743 pmap_qenter((vm_offset_t)bp->b_saveaddr, bp->b_pages, pidx); 3744 3745 kva = bp->b_saveaddr; 3746 bp->b_npages = pidx; 3747 bp->b_saveaddr = bp->b_data; 3748 bp->b_data = kva + (((vm_offset_t) bp->b_data) & PAGE_MASK); 3749 return(0); 3750} 3751 3752/* 3753 * Free the io map PTEs associated with this IO operation. 3754 * We also invalidate the TLB entries and restore the original b_addr. 3755 */ 3756void 3757vunmapbuf(struct buf *bp) 3758{ 3759 int pidx; 3760 int npages; 3761 3762 npages = bp->b_npages; 3763 pmap_qremove(trunc_page((vm_offset_t)bp->b_data), npages); 3764 vm_page_lock_queues(); 3765 for (pidx = 0; pidx < npages; pidx++) 3766 vm_page_unhold(bp->b_pages[pidx]); 3767 vm_page_unlock_queues(); 3768 3769 bp->b_data = bp->b_saveaddr; 3770} 3771 3772void 3773bdone(struct buf *bp) 3774{ 3775 struct mtx *mtxp; 3776 3777 mtxp = mtx_pool_find(mtxpool_sleep, bp); 3778 mtx_lock(mtxp); 3779 bp->b_flags |= B_DONE; 3780 wakeup(bp); 3781 mtx_unlock(mtxp); 3782} 3783 3784void 3785bwait(struct buf *bp, u_char pri, const char *wchan) 3786{ 3787 struct mtx *mtxp; 3788 3789 mtxp = mtx_pool_find(mtxpool_sleep, bp); 3790 mtx_lock(mtxp); 3791 while ((bp->b_flags & B_DONE) == 0) 3792 msleep(bp, mtxp, pri, wchan, 0); 3793 mtx_unlock(mtxp); 3794} 3795 3796int 3797bufsync(struct bufobj *bo, int waitfor, struct thread *td) 3798{ 3799 3800 return (VOP_FSYNC(bo->__bo_vnode, waitfor, td)); 3801} 3802 3803void 3804bufstrategy(struct bufobj *bo, struct buf *bp) 3805{ 3806 int i = 0; 3807 struct vnode *vp; 3808 3809 vp = bp->b_vp; 3810 KASSERT(vp == bo->bo_private, ("Inconsistent vnode bufstrategy")); 3811 KASSERT(vp->v_type != VCHR && vp->v_type != VBLK, 3812 ("Wrong vnode in bufstrategy(bp=%p, vp=%p)", bp, vp)); 3813 i = VOP_STRATEGY(vp, bp); 3814 KASSERT(i == 0, ("VOP_STRATEGY failed bp=%p vp=%p", bp, bp->b_vp)); 3815} 3816 3817void 3818bufobj_wrefl(struct bufobj *bo) 3819{ 3820 3821 KASSERT(bo != NULL, ("NULL bo in bufobj_wref")); 3822 ASSERT_BO_LOCKED(bo); 3823 bo->bo_numoutput++; 3824} 3825 3826void 3827bufobj_wref(struct bufobj *bo) 3828{ 3829 3830 KASSERT(bo != NULL, ("NULL bo in bufobj_wref")); 3831 BO_LOCK(bo); 3832 bo->bo_numoutput++; 3833 BO_UNLOCK(bo); 3834} 3835 3836void 3837bufobj_wdrop(struct bufobj *bo) 3838{ 3839 3840 KASSERT(bo != NULL, ("NULL bo in bufobj_wdrop")); 3841 BO_LOCK(bo); 3842 KASSERT(bo->bo_numoutput > 0, ("bufobj_wdrop non-positive count")); 3843 if ((--bo->bo_numoutput == 0) && (bo->bo_flag & BO_WWAIT)) { 3844 bo->bo_flag &= ~BO_WWAIT; 3845 wakeup(&bo->bo_numoutput); 3846 } 3847 BO_UNLOCK(bo); 3848} 3849 3850int 3851bufobj_wwait(struct bufobj *bo, int slpflag, int timeo) 3852{ 3853 int error; 3854 3855 KASSERT(bo != NULL, ("NULL bo in bufobj_wwait")); 3856 ASSERT_BO_LOCKED(bo); 3857 error = 0; 3858 while (bo->bo_numoutput) { 3859 bo->bo_flag |= BO_WWAIT; 3860 error = msleep(&bo->bo_numoutput, BO_MTX(bo), 3861 slpflag | (PRIBIO + 1), "bo_wwait", timeo); 3862 if (error) 3863 break; 3864 } 3865 return (error); 3866} 3867 3868void 3869bpin(struct buf *bp) 3870{ 3871 struct mtx *mtxp; 3872 3873 mtxp = mtx_pool_find(mtxpool_sleep, bp); 3874 mtx_lock(mtxp); 3875 bp->b_pin_count++; 3876 mtx_unlock(mtxp); 3877} 3878 3879void 3880bunpin(struct buf *bp) 3881{ 3882 struct mtx *mtxp; 3883 3884 mtxp = mtx_pool_find(mtxpool_sleep, bp); 3885 mtx_lock(mtxp); 3886 if (--bp->b_pin_count == 0) 3887 wakeup(bp); 3888 mtx_unlock(mtxp); 3889} 3890 3891void 3892bunpin_wait(struct buf *bp) 3893{ 3894 struct mtx *mtxp; 3895 3896 mtxp = mtx_pool_find(mtxpool_sleep, bp); 3897 mtx_lock(mtxp); 3898 while (bp->b_pin_count > 0) 3899 msleep(bp, mtxp, PRIBIO, "bwunpin", 0); 3900 mtx_unlock(mtxp); 3901} 3902 3903#include "opt_ddb.h" 3904#ifdef DDB 3905#include <ddb/ddb.h> 3906 3907/* DDB command to show buffer data */ 3908DB_SHOW_COMMAND(buffer, db_show_buffer) 3909{ 3910 /* get args */ 3911 struct buf *bp = (struct buf *)addr; 3912 3913 if (!have_addr) { 3914 db_printf("usage: show buffer <addr>\n"); 3915 return; 3916 } 3917 3918 db_printf("buf at %p\n", bp); 3919 db_printf("b_flags = 0x%b\n", (u_int)bp->b_flags, PRINT_BUF_FLAGS); 3920 db_printf( 3921 "b_error = %d, b_bufsize = %ld, b_bcount = %ld, b_resid = %ld\n" 3922 "b_bufobj = (%p), b_data = %p, b_blkno = %jd\n", 3923 bp->b_error, bp->b_bufsize, bp->b_bcount, bp->b_resid, 3924 bp->b_bufobj, bp->b_data, (intmax_t)bp->b_blkno); 3925 if (bp->b_npages) { 3926 int i; 3927 db_printf("b_npages = %d, pages(OBJ, IDX, PA): ", bp->b_npages); 3928 for (i = 0; i < bp->b_npages; i++) { 3929 vm_page_t m; 3930 m = bp->b_pages[i]; 3931 db_printf("(%p, 0x%lx, 0x%lx)", (void *)m->object, 3932 (u_long)m->pindex, (u_long)VM_PAGE_TO_PHYS(m)); 3933 if ((i + 1) < bp->b_npages) 3934 db_printf(","); 3935 } 3936 db_printf("\n"); 3937 } 3938 lockmgr_printinfo(&bp->b_lock); 3939} 3940 3941DB_SHOW_COMMAND(lockedbufs, lockedbufs) 3942{ 3943 struct buf *bp; 3944 int i; 3945 3946 for (i = 0; i < nbuf; i++) { 3947 bp = &buf[i]; 3948 if (BUF_ISLOCKED(bp)) { 3949 db_show_buffer((uintptr_t)bp, 1, 0, NULL); 3950 db_printf("\n"); 3951 } 3952 } 3953} 3954#endif /* DDB */ 3955