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