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