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