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