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