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