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