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