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