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