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