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