vfs_bio.c revision 35256
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 * $Id: vfs_bio.c,v 1.161 1998/04/15 17:46:30 bde Exp $ 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 28#include "opt_bounce.h" 29 30#define VMIO 31#include <sys/param.h> 32#include <sys/systm.h> 33#include <sys/sysproto.h> 34#include <sys/kernel.h> 35#include <sys/sysctl.h> 36#include <sys/proc.h> 37#include <sys/vnode.h> 38#include <sys/vmmeter.h> 39#include <sys/lock.h> 40#include <miscfs/specfs/specdev.h> 41#include <vm/vm.h> 42#include <vm/vm_param.h> 43#include <vm/vm_prot.h> 44#include <vm/vm_kern.h> 45#include <vm/vm_pageout.h> 46#include <vm/vm_page.h> 47#include <vm/vm_object.h> 48#include <vm/vm_extern.h> 49#include <vm/vm_map.h> 50#include <sys/buf.h> 51#include <sys/mount.h> 52#include <sys/malloc.h> 53#include <sys/resourcevar.h> 54 55static MALLOC_DEFINE(M_BIOBUF, "BIO buffer", "BIO buffer"); 56 57struct bio_ops bioops; /* I/O operation notification */ 58 59#if 0 /* replaced bu sched_sync */ 60static void vfs_update __P((void)); 61static struct proc *updateproc; 62static struct kproc_desc up_kp = { 63 "update", 64 vfs_update, 65 &updateproc 66}; 67SYSINIT_KT(update, SI_SUB_KTHREAD_UPDATE, SI_ORDER_FIRST, kproc_start, &up_kp) 68#endif 69 70struct buf *buf; /* buffer header pool */ 71struct swqueue bswlist; 72 73static int count_lock_queue __P((void)); 74static void vm_hold_free_pages(struct buf * bp, vm_offset_t from, 75 vm_offset_t to); 76static void vm_hold_load_pages(struct buf * bp, vm_offset_t from, 77 vm_offset_t to); 78static void vfs_buf_set_valid(struct buf *bp, vm_ooffset_t foff, 79 vm_offset_t off, vm_offset_t size, 80 vm_page_t m); 81static void vfs_page_set_valid(struct buf *bp, vm_ooffset_t off, 82 int pageno, vm_page_t m); 83static void vfs_clean_pages(struct buf * bp); 84static void vfs_setdirty(struct buf *bp); 85static void vfs_vmio_release(struct buf *bp); 86static void flushdirtybuffers(int slpflag, int slptimeo); 87 88int needsbuffer; 89 90/* 91 * Internal update daemon, process 3 92 * The variable vfs_update_wakeup allows for internal syncs. 93 */ 94int vfs_update_wakeup; 95 96 97/* 98 * buffers base kva 99 */ 100 101/* 102 * bogus page -- for I/O to/from partially complete buffers 103 * this is a temporary solution to the problem, but it is not 104 * really that bad. it would be better to split the buffer 105 * for input in the case of buffers partially already in memory, 106 * but the code is intricate enough already. 107 */ 108vm_page_t bogus_page; 109static vm_offset_t bogus_offset; 110 111static int bufspace, maxbufspace, vmiospace, maxvmiobufspace, 112 bufmallocspace, maxbufmallocspace; 113int numdirtybuffers; 114static int lodirtybuffers, hidirtybuffers; 115static int numfreebuffers, lofreebuffers, hifreebuffers; 116static int kvafreespace; 117 118SYSCTL_INT(_vfs, OID_AUTO, numdirtybuffers, CTLFLAG_RD, 119 &numdirtybuffers, 0, ""); 120SYSCTL_INT(_vfs, OID_AUTO, lodirtybuffers, CTLFLAG_RW, 121 &lodirtybuffers, 0, ""); 122SYSCTL_INT(_vfs, OID_AUTO, hidirtybuffers, CTLFLAG_RW, 123 &hidirtybuffers, 0, ""); 124SYSCTL_INT(_vfs, OID_AUTO, numfreebuffers, CTLFLAG_RD, 125 &numfreebuffers, 0, ""); 126SYSCTL_INT(_vfs, OID_AUTO, lofreebuffers, CTLFLAG_RW, 127 &lofreebuffers, 0, ""); 128SYSCTL_INT(_vfs, OID_AUTO, hifreebuffers, CTLFLAG_RW, 129 &hifreebuffers, 0, ""); 130SYSCTL_INT(_vfs, OID_AUTO, maxbufspace, CTLFLAG_RW, 131 &maxbufspace, 0, ""); 132SYSCTL_INT(_vfs, OID_AUTO, bufspace, CTLFLAG_RD, 133 &bufspace, 0, ""); 134SYSCTL_INT(_vfs, OID_AUTO, maxvmiobufspace, CTLFLAG_RW, 135 &maxvmiobufspace, 0, ""); 136SYSCTL_INT(_vfs, OID_AUTO, vmiospace, CTLFLAG_RD, 137 &vmiospace, 0, ""); 138SYSCTL_INT(_vfs, OID_AUTO, maxmallocbufspace, CTLFLAG_RW, 139 &maxbufmallocspace, 0, ""); 140SYSCTL_INT(_vfs, OID_AUTO, bufmallocspace, CTLFLAG_RD, 141 &bufmallocspace, 0, ""); 142SYSCTL_INT(_vfs, OID_AUTO, kvafreespace, CTLFLAG_RD, 143 &kvafreespace, 0, ""); 144 145static LIST_HEAD(bufhashhdr, buf) bufhashtbl[BUFHSZ], invalhash; 146struct bqueues bufqueues[BUFFER_QUEUES] = {0}; 147 148extern int vm_swap_size; 149 150#define BUF_MAXUSE 24 151 152#define VFS_BIO_NEED_ANY 1 153#define VFS_BIO_NEED_LOWLIMIT 2 154#define VFS_BIO_NEED_FREE 4 155 156/* 157 * Initialize buffer headers and related structures. 158 */ 159void 160bufinit() 161{ 162 struct buf *bp; 163 int i; 164 165 TAILQ_INIT(&bswlist); 166 LIST_INIT(&invalhash); 167 168 /* first, make a null hash table */ 169 for (i = 0; i < BUFHSZ; i++) 170 LIST_INIT(&bufhashtbl[i]); 171 172 /* next, make a null set of free lists */ 173 for (i = 0; i < BUFFER_QUEUES; i++) 174 TAILQ_INIT(&bufqueues[i]); 175 176 /* finally, initialize each buffer header and stick on empty q */ 177 for (i = 0; i < nbuf; i++) { 178 bp = &buf[i]; 179 bzero(bp, sizeof *bp); 180 bp->b_flags = B_INVAL; /* we're just an empty header */ 181 bp->b_dev = NODEV; 182 bp->b_rcred = NOCRED; 183 bp->b_wcred = NOCRED; 184 bp->b_qindex = QUEUE_EMPTY; 185 bp->b_vnbufs.le_next = NOLIST; 186 LIST_INIT(&bp->b_dep); 187 TAILQ_INSERT_TAIL(&bufqueues[QUEUE_EMPTY], bp, b_freelist); 188 LIST_INSERT_HEAD(&invalhash, bp, b_hash); 189 } 190/* 191 * maxbufspace is currently calculated to support all filesystem blocks 192 * to be 8K. If you happen to use a 16K filesystem, the size of the buffer 193 * cache is still the same as it would be for 8K filesystems. This 194 * keeps the size of the buffer cache "in check" for big block filesystems. 195 */ 196 maxbufspace = (nbuf + 8) * DFLTBSIZE; 197/* 198 * reserve 1/3 of the buffers for metadata (VDIR) which might not be VMIO'ed 199 */ 200 maxvmiobufspace = 2 * maxbufspace / 3; 201/* 202 * Limit the amount of malloc memory since it is wired permanently into 203 * the kernel space. Even though this is accounted for in the buffer 204 * allocation, we don't want the malloced region to grow uncontrolled. 205 * The malloc scheme improves memory utilization significantly on average 206 * (small) directories. 207 */ 208 maxbufmallocspace = maxbufspace / 20; 209 210/* 211 * Remove the probability of deadlock conditions by limiting the 212 * number of dirty buffers. 213 */ 214 hidirtybuffers = nbuf / 8 + 20; 215 lodirtybuffers = nbuf / 16 + 10; 216 numdirtybuffers = 0; 217 lofreebuffers = nbuf / 18 + 5; 218 hifreebuffers = 2 * lofreebuffers; 219 numfreebuffers = nbuf; 220 kvafreespace = 0; 221 222 bogus_offset = kmem_alloc_pageable(kernel_map, PAGE_SIZE); 223 bogus_page = vm_page_alloc(kernel_object, 224 ((bogus_offset - VM_MIN_KERNEL_ADDRESS) >> PAGE_SHIFT), 225 VM_ALLOC_NORMAL); 226 227} 228 229/* 230 * Free the kva allocation for a buffer 231 * Must be called only at splbio or higher, 232 * as this is the only locking for buffer_map. 233 */ 234static void 235bfreekva(struct buf * bp) 236{ 237 if (bp->b_kvasize == 0) 238 return; 239 240 vm_map_delete(buffer_map, 241 (vm_offset_t) bp->b_kvabase, 242 (vm_offset_t) bp->b_kvabase + bp->b_kvasize); 243 244 bp->b_kvasize = 0; 245 246} 247 248/* 249 * remove the buffer from the appropriate free list 250 */ 251void 252bremfree(struct buf * bp) 253{ 254 int s = splbio(); 255 256 if (bp->b_qindex != QUEUE_NONE) { 257 if (bp->b_qindex == QUEUE_EMPTY) { 258 kvafreespace -= bp->b_kvasize; 259 } 260 TAILQ_REMOVE(&bufqueues[bp->b_qindex], bp, b_freelist); 261 bp->b_qindex = QUEUE_NONE; 262 } else { 263#if !defined(MAX_PERF) 264 panic("bremfree: removing a buffer when not on a queue"); 265#endif 266 } 267 if ((bp->b_flags & B_INVAL) || 268 (bp->b_flags & (B_DELWRI|B_LOCKED)) == 0) 269 --numfreebuffers; 270 splx(s); 271} 272 273 274/* 275 * Get a buffer with the specified data. Look in the cache first. 276 */ 277int 278bread(struct vnode * vp, daddr_t blkno, int size, struct ucred * cred, 279 struct buf ** bpp) 280{ 281 struct buf *bp; 282 283 bp = getblk(vp, blkno, size, 0, 0); 284 *bpp = bp; 285 286 /* if not found in cache, do some I/O */ 287 if ((bp->b_flags & B_CACHE) == 0) { 288 if (curproc != NULL) 289 curproc->p_stats->p_ru.ru_inblock++; 290 bp->b_flags |= B_READ; 291 bp->b_flags &= ~(B_DONE | B_ERROR | B_INVAL); 292 if (bp->b_rcred == NOCRED) { 293 if (cred != NOCRED) 294 crhold(cred); 295 bp->b_rcred = cred; 296 } 297 vfs_busy_pages(bp, 0); 298 VOP_STRATEGY(bp); 299 return (biowait(bp)); 300 } 301 return (0); 302} 303 304/* 305 * Operates like bread, but also starts asynchronous I/O on 306 * read-ahead blocks. 307 */ 308int 309breadn(struct vnode * vp, daddr_t blkno, int size, 310 daddr_t * rablkno, int *rabsize, 311 int cnt, struct ucred * cred, struct buf ** bpp) 312{ 313 struct buf *bp, *rabp; 314 int i; 315 int rv = 0, readwait = 0; 316 317 *bpp = bp = getblk(vp, blkno, size, 0, 0); 318 319 /* if not found in cache, do some I/O */ 320 if ((bp->b_flags & B_CACHE) == 0) { 321 if (curproc != NULL) 322 curproc->p_stats->p_ru.ru_inblock++; 323 bp->b_flags |= B_READ; 324 bp->b_flags &= ~(B_DONE | B_ERROR | B_INVAL); 325 if (bp->b_rcred == NOCRED) { 326 if (cred != NOCRED) 327 crhold(cred); 328 bp->b_rcred = cred; 329 } 330 vfs_busy_pages(bp, 0); 331 VOP_STRATEGY(bp); 332 ++readwait; 333 } 334 for (i = 0; i < cnt; i++, rablkno++, rabsize++) { 335 if (inmem(vp, *rablkno)) 336 continue; 337 rabp = getblk(vp, *rablkno, *rabsize, 0, 0); 338 339 if ((rabp->b_flags & B_CACHE) == 0) { 340 if (curproc != NULL) 341 curproc->p_stats->p_ru.ru_inblock++; 342 rabp->b_flags |= B_READ | B_ASYNC; 343 rabp->b_flags &= ~(B_DONE | B_ERROR | B_INVAL); 344 if (rabp->b_rcred == NOCRED) { 345 if (cred != NOCRED) 346 crhold(cred); 347 rabp->b_rcred = cred; 348 } 349 vfs_busy_pages(rabp, 0); 350 VOP_STRATEGY(rabp); 351 } else { 352 brelse(rabp); 353 } 354 } 355 356 if (readwait) { 357 rv = biowait(bp); 358 } 359 return (rv); 360} 361 362/* 363 * Write, release buffer on completion. (Done by iodone 364 * if async.) 365 */ 366int 367bwrite(struct buf * bp) 368{ 369 int oldflags; 370 struct vnode *vp; 371 struct mount *mp; 372 373 374 if (bp->b_flags & B_INVAL) { 375 brelse(bp); 376 return (0); 377 } 378 379 oldflags = bp->b_flags; 380 381#if !defined(MAX_PERF) 382 if ((bp->b_flags & B_BUSY) == 0) 383 panic("bwrite: buffer is not busy???"); 384#endif 385 386 bp->b_flags &= ~(B_READ | B_DONE | B_ERROR | B_DELWRI); 387 bp->b_flags |= B_WRITEINPROG; 388 389 if ((oldflags & B_DELWRI) == B_DELWRI) { 390 --numdirtybuffers; 391 reassignbuf(bp, bp->b_vp); 392 } 393 394 bp->b_vp->v_numoutput++; 395 vfs_busy_pages(bp, 1); 396 if (curproc != NULL) 397 curproc->p_stats->p_ru.ru_oublock++; 398 VOP_STRATEGY(bp); 399 400 /* 401 * Collect statistics on synchronous and asynchronous writes. 402 * Writes to block devices are charged to their associated 403 * filesystem (if any). 404 */ 405 if ((vp = bp->b_vp) != NULL) { 406 if (vp->v_type == VBLK) 407 mp = vp->v_specmountpoint; 408 else 409 mp = vp->v_mount; 410 if (mp != NULL) 411 if ((oldflags & B_ASYNC) == 0) 412 mp->mnt_stat.f_syncwrites++; 413 else 414 mp->mnt_stat.f_asyncwrites++; 415 } 416 417 if ((oldflags & B_ASYNC) == 0) { 418 int rtval = biowait(bp); 419 brelse(bp); 420 return (rtval); 421 } 422 return (0); 423} 424 425__inline void 426vfs_bio_need_satisfy(void) { 427 ++numfreebuffers; 428 if (!needsbuffer) 429 return; 430 if (numdirtybuffers < lodirtybuffers) { 431 needsbuffer &= ~(VFS_BIO_NEED_ANY | VFS_BIO_NEED_LOWLIMIT); 432 } else { 433 needsbuffer &= ~VFS_BIO_NEED_ANY; 434 } 435 if (numfreebuffers >= hifreebuffers) { 436 needsbuffer &= ~VFS_BIO_NEED_FREE; 437 } 438 wakeup(&needsbuffer); 439} 440 441/* 442 * Delayed write. (Buffer is marked dirty). 443 */ 444void 445bdwrite(struct buf * bp) 446{ 447 int s; 448 struct vnode *vp; 449 450#if !defined(MAX_PERF) 451 if ((bp->b_flags & B_BUSY) == 0) { 452 panic("bdwrite: buffer is not busy"); 453 } 454#endif 455 456 if (bp->b_flags & B_INVAL) { 457 brelse(bp); 458 return; 459 } 460 if (bp->b_flags & B_TAPE) { 461 bawrite(bp); 462 return; 463 } 464 bp->b_flags &= ~(B_READ|B_RELBUF); 465 if ((bp->b_flags & B_DELWRI) == 0) { 466 bp->b_flags |= B_DONE | B_DELWRI; 467 reassignbuf(bp, bp->b_vp); 468 ++numdirtybuffers; 469 } 470 471 /* 472 * This bmap keeps the system from needing to do the bmap later, 473 * perhaps when the system is attempting to do a sync. Since it 474 * is likely that the indirect block -- or whatever other datastructure 475 * that the filesystem needs is still in memory now, it is a good 476 * thing to do this. Note also, that if the pageout daemon is 477 * requesting a sync -- there might not be enough memory to do 478 * the bmap then... So, this is important to do. 479 */ 480 if (bp->b_lblkno == bp->b_blkno) { 481 VOP_BMAP(bp->b_vp, bp->b_lblkno, NULL, &bp->b_blkno, NULL, NULL); 482 } 483 484 /* 485 * Set the *dirty* buffer range based upon the VM system dirty pages. 486 */ 487 vfs_setdirty(bp); 488 489 /* 490 * We need to do this here to satisfy the vnode_pager and the 491 * pageout daemon, so that it thinks that the pages have been 492 * "cleaned". Note that since the pages are in a delayed write 493 * buffer -- the VFS layer "will" see that the pages get written 494 * out on the next sync, or perhaps the cluster will be completed. 495 */ 496 vfs_clean_pages(bp); 497 bqrelse(bp); 498 499 /* 500 * XXX The soft dependency code is not prepared to 501 * have I/O done when a bdwrite is requested. For 502 * now we just let the write be delayed if it is 503 * requested by the soft dependency code. 504 */ 505 if ((vp = bp->b_vp) && 506 (vp->v_type == VBLK && vp->v_specmountpoint && 507 (vp->v_specmountpoint->mnt_flag & MNT_SOFTDEP)) || 508 (vp->v_mount && (vp->v_mount->mnt_flag & MNT_SOFTDEP))) 509 return; 510 511 if (numdirtybuffers >= hidirtybuffers) 512 flushdirtybuffers(0, 0); 513 514 return; 515} 516 517 518/* 519 * Same as first half of bdwrite, mark buffer dirty, but do not release it. 520 * Check how this compares with vfs_setdirty(); XXX [JRE] 521 */ 522void 523bdirty(bp) 524 struct buf *bp; 525{ 526 int s; 527 528 bp->b_flags &= ~(B_READ|B_RELBUF); /* XXX ??? check this */ 529 if ((bp->b_flags & B_DELWRI) == 0) { 530 bp->b_flags |= B_DONE | B_DELWRI; /* why done? XXX JRE */ 531 reassignbuf(bp, bp->b_vp); 532 ++numdirtybuffers; 533 } 534} 535 536/* 537 * Asynchronous write. 538 * Start output on a buffer, but do not wait for it to complete. 539 * The buffer is released when the output completes. 540 */ 541void 542bawrite(struct buf * bp) 543{ 544 bp->b_flags |= B_ASYNC; 545 (void) VOP_BWRITE(bp); 546} 547 548/* 549 * Ordered write. 550 * Start output on a buffer, but only wait for it to complete if the 551 * output device cannot guarantee ordering in some other way. Devices 552 * that can perform asynchronous ordered writes will set the B_ASYNC 553 * flag in their strategy routine. 554 * The buffer is released when the output completes. 555 */ 556int 557bowrite(struct buf * bp) 558{ 559 /* 560 * XXX Add in B_ASYNC once the SCSI 561 * layer can deal with ordered 562 * writes properly. 563 */ 564 bp->b_flags |= B_ORDERED; 565 return (VOP_BWRITE(bp)); 566} 567 568/* 569 * Release a buffer. 570 */ 571void 572brelse(struct buf * bp) 573{ 574 int s; 575 576 if (bp->b_flags & B_CLUSTER) { 577 relpbuf(bp); 578 return; 579 } 580 581 s = splbio(); 582 583 /* anyone need this block? */ 584 if (bp->b_flags & B_WANTED) { 585 bp->b_flags &= ~(B_WANTED | B_AGE); 586 wakeup(bp); 587 } 588 589 if (bp->b_flags & B_LOCKED) 590 bp->b_flags &= ~B_ERROR; 591 592 if ((bp->b_flags & (B_NOCACHE | B_INVAL | B_ERROR)) || 593 (bp->b_bufsize <= 0)) { 594 bp->b_flags |= B_INVAL; 595 if (LIST_FIRST(&bp->b_dep) != NULL && bioops.io_deallocate) 596 (*bioops.io_deallocate)(bp); 597 if (bp->b_flags & B_DELWRI) 598 --numdirtybuffers; 599 bp->b_flags &= ~(B_DELWRI | B_CACHE); 600 if ((bp->b_flags & B_VMIO) == 0) { 601 if (bp->b_bufsize) 602 allocbuf(bp, 0); 603 if (bp->b_vp) 604 brelvp(bp); 605 } 606 } 607 608 /* 609 * VMIO buffer rundown. It is not very necessary to keep a VMIO buffer 610 * constituted, so the B_INVAL flag is used to *invalidate* the buffer, 611 * but the VM object is kept around. The B_NOCACHE flag is used to 612 * invalidate the pages in the VM object. 613 * 614 * If the buffer is a partially filled NFS buffer, keep it 615 * since invalidating it now will lose informatio. The valid 616 * flags in the vm_pages have only DEV_BSIZE resolution but 617 * the b_validoff, b_validend fields have byte resolution. 618 * This can avoid unnecessary re-reads of the buffer. 619 * XXX this seems to cause performance problems. 620 */ 621 if ((bp->b_flags & B_VMIO) 622 && !(bp->b_vp->v_tag == VT_NFS && 623 bp->b_vp->v_type != VBLK && 624 (bp->b_flags & B_DELWRI) != 0) 625#ifdef notdef 626 && (bp->b_vp->v_tag != VT_NFS 627 || bp->b_vp->v_type == VBLK 628 || (bp->b_flags & (B_NOCACHE | B_INVAL | B_ERROR)) 629 || bp->b_validend == 0 630 || (bp->b_validoff == 0 631 && bp->b_validend == bp->b_bufsize)) 632#endif 633 ) { 634 635 int i, j, resid; 636 vm_page_t m; 637 off_t foff; 638 vm_pindex_t poff; 639 vm_object_t obj; 640 struct vnode *vp; 641 642 vp = bp->b_vp; 643 644 resid = bp->b_bufsize; 645 foff = bp->b_offset; 646 647 for (i = 0; i < bp->b_npages; i++) { 648 m = bp->b_pages[i]; 649 m->flags &= ~PG_ZERO; 650 if (m == bogus_page) { 651 652 obj = (vm_object_t) vp->v_object; 653 poff = OFF_TO_IDX(bp->b_offset); 654 655 for (j = i; j < bp->b_npages; j++) { 656 m = bp->b_pages[j]; 657 if (m == bogus_page) { 658 m = vm_page_lookup(obj, poff + j); 659#if !defined(MAX_PERF) 660 if (!m) { 661 panic("brelse: page missing\n"); 662 } 663#endif 664 bp->b_pages[j] = m; 665 } 666 } 667 668 if ((bp->b_flags & B_INVAL) == 0) { 669 pmap_qenter(trunc_page(bp->b_data), bp->b_pages, bp->b_npages); 670 } 671 } 672 if (bp->b_flags & (B_NOCACHE|B_ERROR)) { 673 int poffset = foff & PAGE_MASK; 674 int presid = resid > (PAGE_SIZE - poffset) ? 675 (PAGE_SIZE - poffset) : resid; 676 vm_page_set_invalid(m, poffset, presid); 677 } 678 resid -= PAGE_SIZE; 679 } 680 681 if (bp->b_flags & (B_INVAL | B_RELBUF)) 682 vfs_vmio_release(bp); 683 684 } else if (bp->b_flags & B_VMIO) { 685 686 if (bp->b_flags & (B_INVAL | B_RELBUF)) 687 vfs_vmio_release(bp); 688 689 } 690 691#if !defined(MAX_PERF) 692 if (bp->b_qindex != QUEUE_NONE) 693 panic("brelse: free buffer onto another queue???"); 694#endif 695 696 /* enqueue */ 697 /* buffers with no memory */ 698 if (bp->b_bufsize == 0) { 699 bp->b_flags |= B_INVAL; 700 bp->b_qindex = QUEUE_EMPTY; 701 TAILQ_INSERT_HEAD(&bufqueues[QUEUE_EMPTY], bp, b_freelist); 702 LIST_REMOVE(bp, b_hash); 703 LIST_INSERT_HEAD(&invalhash, bp, b_hash); 704 bp->b_dev = NODEV; 705 kvafreespace += bp->b_kvasize; 706 707 /* buffers with junk contents */ 708 } else if (bp->b_flags & (B_ERROR | B_INVAL | B_NOCACHE | B_RELBUF)) { 709 bp->b_flags |= B_INVAL; 710 bp->b_qindex = QUEUE_AGE; 711 TAILQ_INSERT_HEAD(&bufqueues[QUEUE_AGE], bp, b_freelist); 712 LIST_REMOVE(bp, b_hash); 713 LIST_INSERT_HEAD(&invalhash, bp, b_hash); 714 bp->b_dev = NODEV; 715 716 /* buffers that are locked */ 717 } else if (bp->b_flags & B_LOCKED) { 718 bp->b_qindex = QUEUE_LOCKED; 719 TAILQ_INSERT_TAIL(&bufqueues[QUEUE_LOCKED], bp, b_freelist); 720 721 /* buffers with stale but valid contents */ 722 } else if (bp->b_flags & B_AGE) { 723 bp->b_qindex = QUEUE_AGE; 724 TAILQ_INSERT_TAIL(&bufqueues[QUEUE_AGE], bp, b_freelist); 725 726 /* buffers with valid and quite potentially reuseable contents */ 727 } else { 728 bp->b_qindex = QUEUE_LRU; 729 TAILQ_INSERT_TAIL(&bufqueues[QUEUE_LRU], bp, b_freelist); 730 } 731 732 if ((bp->b_flags & B_INVAL) || 733 (bp->b_flags & (B_LOCKED|B_DELWRI)) == 0) { 734 if (bp->b_flags & B_DELWRI) { 735 --numdirtybuffers; 736 bp->b_flags &= ~B_DELWRI; 737 } 738 vfs_bio_need_satisfy(); 739 } 740 741 /* unlock */ 742 bp->b_flags &= ~(B_ORDERED | B_WANTED | B_BUSY | 743 B_ASYNC | B_NOCACHE | B_AGE | B_RELBUF); 744 splx(s); 745} 746 747/* 748 * Release a buffer. 749 */ 750void 751bqrelse(struct buf * bp) 752{ 753 int s; 754 755 s = splbio(); 756 757 /* anyone need this block? */ 758 if (bp->b_flags & B_WANTED) { 759 bp->b_flags &= ~(B_WANTED | B_AGE); 760 wakeup(bp); 761 } 762 763#if !defined(MAX_PERF) 764 if (bp->b_qindex != QUEUE_NONE) 765 panic("bqrelse: free buffer onto another queue???"); 766#endif 767 768 if (bp->b_flags & B_LOCKED) { 769 bp->b_flags &= ~B_ERROR; 770 bp->b_qindex = QUEUE_LOCKED; 771 TAILQ_INSERT_TAIL(&bufqueues[QUEUE_LOCKED], bp, b_freelist); 772 /* buffers with stale but valid contents */ 773 } else { 774 bp->b_qindex = QUEUE_LRU; 775 TAILQ_INSERT_TAIL(&bufqueues[QUEUE_LRU], bp, b_freelist); 776 } 777 778 if ((bp->b_flags & (B_LOCKED|B_DELWRI)) == 0) { 779 vfs_bio_need_satisfy(); 780 } 781 782 /* unlock */ 783 bp->b_flags &= ~(B_ORDERED | B_WANTED | B_BUSY | 784 B_ASYNC | B_NOCACHE | B_AGE | B_RELBUF); 785 splx(s); 786} 787 788static void 789vfs_vmio_release(bp) 790 struct buf *bp; 791{ 792 int i; 793 vm_page_t m; 794 795 for (i = 0; i < bp->b_npages; i++) { 796 m = bp->b_pages[i]; 797 bp->b_pages[i] = NULL; 798 vm_page_unwire(m); 799 800 /* 801 * We don't mess with busy pages, it is 802 * the responsibility of the process that 803 * busied the pages to deal with them. 804 */ 805 if ((m->flags & PG_BUSY) || (m->busy != 0)) 806 continue; 807 808 if (m->wire_count == 0) { 809 810 /* 811 * If this is an async free -- we cannot place 812 * pages onto the cache queue. If it is an 813 * async free, then we don't modify any queues. 814 * This is probably in error (for perf reasons), 815 * and we will eventually need to build 816 * a more complete infrastructure to support I/O 817 * rundown. 818 */ 819 if ((bp->b_flags & B_ASYNC) == 0) { 820 821 /* 822 * In the case of sync buffer frees, we can do pretty much 823 * anything to any of the memory queues. Specifically, 824 * the cache queue is okay to be modified. 825 */ 826 if (m->valid) { 827 if(m->dirty == 0) 828 vm_page_test_dirty(m); 829 /* 830 * this keeps pressure off of the process memory 831 */ 832 if (m->dirty == 0 && m->hold_count == 0) 833 vm_page_cache(m); 834 else 835 vm_page_deactivate(m); 836 m->flags &= ~PG_ZERO; 837 } else if (m->hold_count == 0) { 838 m->flags |= PG_BUSY; 839 vm_page_protect(m, VM_PROT_NONE); 840 vm_page_free(m); 841 } 842 } else { 843 /* 844 * If async, then at least we clear the 845 * act_count. 846 */ 847 m->act_count = 0; 848 m->flags &= ~PG_ZERO; 849 } 850 } 851 } 852 bufspace -= bp->b_bufsize; 853 vmiospace -= bp->b_bufsize; 854 pmap_qremove(trunc_page((vm_offset_t) bp->b_data), bp->b_npages); 855 bp->b_npages = 0; 856 bp->b_bufsize = 0; 857 bp->b_flags &= ~B_VMIO; 858 if (bp->b_vp) 859 brelvp(bp); 860} 861 862/* 863 * Check to see if a block is currently memory resident. 864 */ 865struct buf * 866gbincore(struct vnode * vp, daddr_t blkno) 867{ 868 struct buf *bp; 869 struct bufhashhdr *bh; 870 871 bh = BUFHASH(vp, blkno); 872 bp = bh->lh_first; 873 874 /* Search hash chain */ 875 while (bp != NULL) { 876 /* hit */ 877 if (bp->b_vp == vp && bp->b_lblkno == blkno && 878 (bp->b_flags & B_INVAL) == 0) { 879 break; 880 } 881 bp = bp->b_hash.le_next; 882 } 883 return (bp); 884} 885 886/* 887 * this routine implements clustered async writes for 888 * clearing out B_DELWRI buffers... This is much better 889 * than the old way of writing only one buffer at a time. 890 */ 891int 892vfs_bio_awrite(struct buf * bp) 893{ 894 int i; 895 daddr_t lblkno = bp->b_lblkno; 896 struct vnode *vp = bp->b_vp; 897 int s; 898 int ncl; 899 struct buf *bpa; 900 int nwritten; 901 int size; 902 int maxcl; 903 904 s = splbio(); 905 /* 906 * right now we support clustered writing only to regular files 907 */ 908 if ((vp->v_type == VREG) && 909 (vp->v_mount != 0) && /* Only on nodes that have the size info */ 910 (bp->b_flags & (B_CLUSTEROK | B_INVAL)) == B_CLUSTEROK) { 911 912 size = vp->v_mount->mnt_stat.f_iosize; 913 maxcl = MAXPHYS / size; 914 915 for (i = 1; i < maxcl; i++) { 916 if ((bpa = gbincore(vp, lblkno + i)) && 917 ((bpa->b_flags & (B_BUSY | B_DELWRI | B_CLUSTEROK | B_INVAL)) == 918 (B_DELWRI | B_CLUSTEROK)) && 919 (bpa->b_bufsize == size)) { 920 if ((bpa->b_blkno == bpa->b_lblkno) || 921 (bpa->b_blkno != bp->b_blkno + ((i * size) >> DEV_BSHIFT))) 922 break; 923 } else { 924 break; 925 } 926 } 927 ncl = i; 928 /* 929 * this is a possible cluster write 930 */ 931 if (ncl != 1) { 932 nwritten = cluster_wbuild(vp, size, lblkno, ncl); 933 splx(s); 934 return nwritten; 935 } 936 } 937 938 bremfree(bp); 939 bp->b_flags |= B_BUSY | B_ASYNC; 940 941 splx(s); 942 /* 943 * default (old) behavior, writing out only one block 944 */ 945 nwritten = bp->b_bufsize; 946 (void) VOP_BWRITE(bp); 947 return nwritten; 948} 949 950 951/* 952 * Find a buffer header which is available for use. 953 */ 954static struct buf * 955getnewbuf(struct vnode *vp, daddr_t blkno, 956 int slpflag, int slptimeo, int size, int maxsize) 957{ 958 struct buf *bp, *bp1; 959 int nbyteswritten = 0; 960 vm_offset_t addr; 961 static int writerecursion = 0; 962 963start: 964 if (bufspace >= maxbufspace) 965 goto trytofreespace; 966 967 /* can we constitute a new buffer? */ 968 if ((bp = TAILQ_FIRST(&bufqueues[QUEUE_EMPTY]))) { 969#if !defined(MAX_PERF) 970 if (bp->b_qindex != QUEUE_EMPTY) 971 panic("getnewbuf: inconsistent EMPTY queue, qindex=%d", 972 bp->b_qindex); 973#endif 974 bp->b_flags |= B_BUSY; 975 bremfree(bp); 976 goto fillbuf; 977 } 978trytofreespace: 979 /* 980 * We keep the file I/O from hogging metadata I/O 981 * This is desirable because file data is cached in the 982 * VM/Buffer cache even if a buffer is freed. 983 */ 984 if ((bp = TAILQ_FIRST(&bufqueues[QUEUE_AGE]))) { 985#if !defined(MAX_PERF) 986 if (bp->b_qindex != QUEUE_AGE) 987 panic("getnewbuf: inconsistent AGE queue, qindex=%d", 988 bp->b_qindex); 989#endif 990 } else if ((bp = TAILQ_FIRST(&bufqueues[QUEUE_LRU]))) { 991#if !defined(MAX_PERF) 992 if (bp->b_qindex != QUEUE_LRU) 993 panic("getnewbuf: inconsistent LRU queue, qindex=%d", 994 bp->b_qindex); 995#endif 996 } 997 if (!bp) { 998 /* wait for a free buffer of any kind */ 999 needsbuffer |= VFS_BIO_NEED_ANY; 1000 do 1001 tsleep(&needsbuffer, (PRIBIO + 4) | slpflag, "newbuf", 1002 slptimeo); 1003 while (needsbuffer & VFS_BIO_NEED_ANY); 1004 return (0); 1005 } 1006 1007#if defined(DIAGNOSTIC) 1008 if (bp->b_flags & B_BUSY) { 1009 panic("getnewbuf: busy buffer on free list\n"); 1010 } 1011#endif 1012 1013 /* 1014 * We are fairly aggressive about freeing VMIO buffers, but since 1015 * the buffering is intact without buffer headers, there is not 1016 * much loss. We gain by maintaining non-VMIOed metadata in buffers. 1017 */ 1018 if ((bp->b_qindex == QUEUE_LRU) && (bp->b_usecount > 0)) { 1019 if ((bp->b_flags & B_VMIO) == 0 || 1020 (vmiospace < maxvmiobufspace)) { 1021 --bp->b_usecount; 1022 TAILQ_REMOVE(&bufqueues[QUEUE_LRU], bp, b_freelist); 1023 if (TAILQ_FIRST(&bufqueues[QUEUE_LRU]) != NULL) { 1024 TAILQ_INSERT_TAIL(&bufqueues[QUEUE_LRU], bp, b_freelist); 1025 goto start; 1026 } 1027 TAILQ_INSERT_TAIL(&bufqueues[QUEUE_LRU], bp, b_freelist); 1028 } 1029 } 1030 1031 1032 /* if we are a delayed write, convert to an async write */ 1033 if ((bp->b_flags & (B_DELWRI | B_INVAL)) == B_DELWRI) { 1034 1035 /* 1036 * If our delayed write is likely to be used soon, then 1037 * recycle back onto the LRU queue. 1038 */ 1039 if (vp && (bp->b_vp == vp) && (bp->b_qindex == QUEUE_LRU) && 1040 (bp->b_lblkno >= blkno) && (maxsize > 0)) { 1041 1042 if (bp->b_usecount > 0) { 1043 if (bp->b_lblkno < blkno + (MAXPHYS / maxsize)) { 1044 1045 TAILQ_REMOVE(&bufqueues[QUEUE_LRU], bp, b_freelist); 1046 1047 if (TAILQ_FIRST(&bufqueues[QUEUE_LRU]) != NULL) { 1048 TAILQ_INSERT_TAIL(&bufqueues[QUEUE_LRU], bp, b_freelist); 1049 bp->b_usecount--; 1050 goto start; 1051 } 1052 TAILQ_INSERT_TAIL(&bufqueues[QUEUE_LRU], bp, b_freelist); 1053 } 1054 } 1055 } 1056 1057 /* 1058 * Certain layered filesystems can recursively re-enter the vfs_bio 1059 * code, due to delayed writes. This helps keep the system from 1060 * deadlocking. 1061 */ 1062 if (writerecursion > 0) { 1063 if (writerecursion > 5) { 1064 bp = TAILQ_FIRST(&bufqueues[QUEUE_AGE]); 1065 while (bp) { 1066 if ((bp->b_flags & B_DELWRI) == 0) 1067 break; 1068 bp = TAILQ_NEXT(bp, b_freelist); 1069 } 1070 if (bp == NULL) { 1071 bp = TAILQ_FIRST(&bufqueues[QUEUE_LRU]); 1072 while (bp) { 1073 if ((bp->b_flags & B_DELWRI) == 0) 1074 break; 1075 bp = TAILQ_NEXT(bp, b_freelist); 1076 } 1077 } 1078 if (bp == NULL) 1079 panic("getnewbuf: cannot get buffer, infinite recursion failure"); 1080 } else { 1081 bremfree(bp); 1082 bp->b_flags |= B_BUSY | B_AGE | B_ASYNC; 1083 nbyteswritten += bp->b_bufsize; 1084 ++writerecursion; 1085 VOP_BWRITE(bp); 1086 --writerecursion; 1087 if (!slpflag && !slptimeo) { 1088 return (0); 1089 } 1090 goto start; 1091 } 1092 } else { 1093 ++writerecursion; 1094 nbyteswritten += vfs_bio_awrite(bp); 1095 --writerecursion; 1096 if (!slpflag && !slptimeo) { 1097 return (0); 1098 } 1099 goto start; 1100 } 1101 } 1102 1103 if (bp->b_flags & B_WANTED) { 1104 bp->b_flags &= ~B_WANTED; 1105 wakeup(bp); 1106 } 1107 bremfree(bp); 1108 bp->b_flags |= B_BUSY; 1109 1110 if (bp->b_flags & B_VMIO) { 1111 bp->b_flags &= ~B_ASYNC; 1112 vfs_vmio_release(bp); 1113 } 1114 1115 if (bp->b_vp) 1116 brelvp(bp); 1117 1118fillbuf: 1119 1120 /* we are not free, nor do we contain interesting data */ 1121 if (bp->b_rcred != NOCRED) { 1122 crfree(bp->b_rcred); 1123 bp->b_rcred = NOCRED; 1124 } 1125 if (bp->b_wcred != NOCRED) { 1126 crfree(bp->b_wcred); 1127 bp->b_wcred = NOCRED; 1128 } 1129 if (LIST_FIRST(&bp->b_dep) != NULL && 1130 bioops.io_deallocate) 1131 (*bioops.io_deallocate)(bp); 1132 1133 LIST_REMOVE(bp, b_hash); 1134 LIST_INSERT_HEAD(&invalhash, bp, b_hash); 1135 if (bp->b_bufsize) { 1136 allocbuf(bp, 0); 1137 } 1138 bp->b_flags = B_BUSY; 1139 bp->b_dev = NODEV; 1140 bp->b_vp = NULL; 1141 bp->b_blkno = bp->b_lblkno = 0; 1142 bp->b_offset = NOOFFSET; 1143 bp->b_iodone = 0; 1144 bp->b_error = 0; 1145 bp->b_resid = 0; 1146 bp->b_bcount = 0; 1147 bp->b_npages = 0; 1148 bp->b_dirtyoff = bp->b_dirtyend = 0; 1149 bp->b_validoff = bp->b_validend = 0; 1150 bp->b_usecount = 5; 1151 /* Here, not kern_physio.c, is where this should be done*/ 1152 LIST_INIT(&bp->b_dep); 1153 1154 maxsize = (maxsize + PAGE_MASK) & ~PAGE_MASK; 1155 1156 /* 1157 * we assume that buffer_map is not at address 0 1158 */ 1159 addr = 0; 1160 if (maxsize != bp->b_kvasize) { 1161 bfreekva(bp); 1162 1163findkvaspace: 1164 /* 1165 * See if we have buffer kva space 1166 */ 1167 if (vm_map_findspace(buffer_map, 1168 vm_map_min(buffer_map), maxsize, &addr)) { 1169 if (kvafreespace > 0) { 1170 int totfree = 0, freed; 1171 do { 1172 freed = 0; 1173 for (bp1 = TAILQ_FIRST(&bufqueues[QUEUE_EMPTY]); 1174 bp1 != NULL; bp1 = TAILQ_NEXT(bp1, b_freelist)) { 1175 if (bp1->b_kvasize != 0) { 1176 totfree += bp1->b_kvasize; 1177 freed = bp1->b_kvasize; 1178 bremfree(bp1); 1179 bfreekva(bp1); 1180 brelse(bp1); 1181 break; 1182 } 1183 } 1184 } while (freed); 1185 /* 1186 * if we found free space, then retry with the same buffer. 1187 */ 1188 if (totfree) 1189 goto findkvaspace; 1190 } 1191 bp->b_flags |= B_INVAL; 1192 brelse(bp); 1193 goto trytofreespace; 1194 } 1195 } 1196 1197 /* 1198 * See if we are below are allocated minimum 1199 */ 1200 if (bufspace >= (maxbufspace + nbyteswritten)) { 1201 bp->b_flags |= B_INVAL; 1202 brelse(bp); 1203 goto trytofreespace; 1204 } 1205 1206 /* 1207 * create a map entry for the buffer -- in essence 1208 * reserving the kva space. 1209 */ 1210 if (addr) { 1211 vm_map_insert(buffer_map, NULL, 0, 1212 addr, addr + maxsize, 1213 VM_PROT_ALL, VM_PROT_ALL, MAP_NOFAULT); 1214 1215 bp->b_kvabase = (caddr_t) addr; 1216 bp->b_kvasize = maxsize; 1217 } 1218 bp->b_data = bp->b_kvabase; 1219 1220 return (bp); 1221} 1222 1223static void 1224waitfreebuffers(int slpflag, int slptimeo) { 1225 while (numfreebuffers < hifreebuffers) { 1226 flushdirtybuffers(slpflag, slptimeo); 1227 if (numfreebuffers < hifreebuffers) 1228 break; 1229 needsbuffer |= VFS_BIO_NEED_FREE; 1230 if (tsleep(&needsbuffer, (PRIBIO + 4)|slpflag, "biofre", slptimeo)) 1231 break; 1232 } 1233} 1234 1235static void 1236flushdirtybuffers(int slpflag, int slptimeo) { 1237 int s; 1238 static pid_t flushing = 0; 1239 1240 s = splbio(); 1241 1242 if (flushing) { 1243 if (flushing == curproc->p_pid) { 1244 splx(s); 1245 return; 1246 } 1247 while (flushing) { 1248 if (tsleep(&flushing, (PRIBIO + 4)|slpflag, "biofls", slptimeo)) { 1249 splx(s); 1250 return; 1251 } 1252 } 1253 } 1254 flushing = curproc->p_pid; 1255 1256 while (numdirtybuffers > lodirtybuffers) { 1257 struct buf *bp; 1258 needsbuffer |= VFS_BIO_NEED_LOWLIMIT; 1259 bp = TAILQ_FIRST(&bufqueues[QUEUE_AGE]); 1260 if (bp == NULL) 1261 bp = TAILQ_FIRST(&bufqueues[QUEUE_LRU]); 1262 1263 while (bp && ((bp->b_flags & B_DELWRI) == 0)) { 1264 bp = TAILQ_NEXT(bp, b_freelist); 1265 } 1266 1267 if (bp) { 1268 vfs_bio_awrite(bp); 1269 continue; 1270 } 1271 break; 1272 } 1273 1274 flushing = 0; 1275 wakeup(&flushing); 1276 splx(s); 1277} 1278 1279/* 1280 * Check to see if a block is currently memory resident. 1281 */ 1282struct buf * 1283incore(struct vnode * vp, daddr_t blkno) 1284{ 1285 struct buf *bp; 1286 1287 int s = splbio(); 1288 bp = gbincore(vp, blkno); 1289 splx(s); 1290 return (bp); 1291} 1292 1293/* 1294 * Returns true if no I/O is needed to access the 1295 * associated VM object. This is like incore except 1296 * it also hunts around in the VM system for the data. 1297 */ 1298 1299int 1300inmem(struct vnode * vp, daddr_t blkno) 1301{ 1302 vm_object_t obj; 1303 vm_offset_t toff, tinc; 1304 vm_page_t m; 1305 vm_ooffset_t off; 1306 1307 if (incore(vp, blkno)) 1308 return 1; 1309 if (vp->v_mount == NULL) 1310 return 0; 1311 if ((vp->v_object == NULL) || (vp->v_flag & VOBJBUF) == 0) 1312 return 0; 1313 1314 obj = vp->v_object; 1315 tinc = PAGE_SIZE; 1316 if (tinc > vp->v_mount->mnt_stat.f_iosize) 1317 tinc = vp->v_mount->mnt_stat.f_iosize; 1318 off = blkno * vp->v_mount->mnt_stat.f_iosize; 1319 1320 for (toff = 0; toff < vp->v_mount->mnt_stat.f_iosize; toff += tinc) { 1321 1322 m = vm_page_lookup(obj, OFF_TO_IDX(off + toff)); 1323 if (!m) 1324 return 0; 1325 if (vm_page_is_valid(m, (vm_offset_t) (toff + off), tinc) == 0) 1326 return 0; 1327 } 1328 return 1; 1329} 1330 1331/* 1332 * now we set the dirty range for the buffer -- 1333 * for NFS -- if the file is mapped and pages have 1334 * been written to, let it know. We want the 1335 * entire range of the buffer to be marked dirty if 1336 * any of the pages have been written to for consistancy 1337 * with the b_validoff, b_validend set in the nfs write 1338 * code, and used by the nfs read code. 1339 */ 1340static void 1341vfs_setdirty(struct buf *bp) { 1342 int i; 1343 vm_object_t object; 1344 vm_offset_t boffset, offset; 1345 /* 1346 * We qualify the scan for modified pages on whether the 1347 * object has been flushed yet. The OBJ_WRITEABLE flag 1348 * is not cleared simply by protecting pages off. 1349 */ 1350 if ((bp->b_flags & B_VMIO) && 1351 ((object = bp->b_pages[0]->object)->flags & (OBJ_WRITEABLE|OBJ_CLEANING))) { 1352 /* 1353 * test the pages to see if they have been modified directly 1354 * by users through the VM system. 1355 */ 1356 for (i = 0; i < bp->b_npages; i++) { 1357 bp->b_pages[i]->flags &= ~PG_ZERO; 1358 vm_page_test_dirty(bp->b_pages[i]); 1359 } 1360 1361 /* 1362 * scan forwards for the first page modified 1363 */ 1364 for (i = 0; i < bp->b_npages; i++) { 1365 if (bp->b_pages[i]->dirty) { 1366 break; 1367 } 1368 } 1369 boffset = (i << PAGE_SHIFT); 1370 if (boffset < bp->b_dirtyoff) { 1371 bp->b_dirtyoff = boffset; 1372 } 1373 1374 /* 1375 * scan backwards for the last page modified 1376 */ 1377 for (i = bp->b_npages - 1; i >= 0; --i) { 1378 if (bp->b_pages[i]->dirty) { 1379 break; 1380 } 1381 } 1382 boffset = (i + 1); 1383 offset = boffset + bp->b_pages[0]->pindex; 1384 if (offset >= object->size) 1385 boffset = object->size - bp->b_pages[0]->pindex; 1386 if (bp->b_dirtyend < (boffset << PAGE_SHIFT)) 1387 bp->b_dirtyend = (boffset << PAGE_SHIFT); 1388 } 1389} 1390 1391/* 1392 * Get a block given a specified block and offset into a file/device. 1393 */ 1394struct buf * 1395getblk(struct vnode * vp, daddr_t blkno, int size, int slpflag, int slptimeo) 1396{ 1397 struct buf *bp; 1398 int i, s; 1399 struct bufhashhdr *bh; 1400 int maxsize; 1401 int generation; 1402 int checksize; 1403 1404 if (vp->v_mount) { 1405 maxsize = vp->v_mount->mnt_stat.f_iosize; 1406 /* 1407 * This happens on mount points. 1408 */ 1409 if (maxsize < size) 1410 maxsize = size; 1411 } else { 1412 maxsize = size; 1413 } 1414 1415#if !defined(MAX_PERF) 1416 if (size > MAXBSIZE) 1417 panic("getblk: size(%d) > MAXBSIZE(%d)\n", size, MAXBSIZE); 1418#endif 1419 1420 s = splbio(); 1421loop: 1422 if (numfreebuffers < lofreebuffers) { 1423 waitfreebuffers(slpflag, slptimeo); 1424 } 1425 1426 if ((bp = gbincore(vp, blkno))) { 1427loop1: 1428 if (bp->b_flags & B_BUSY) { 1429 1430 bp->b_flags |= B_WANTED; 1431 if (bp->b_usecount < BUF_MAXUSE) 1432 ++bp->b_usecount; 1433 1434 if (!tsleep(bp, 1435 (PRIBIO + 4) | slpflag, "getblk", slptimeo)) { 1436 goto loop; 1437 } 1438 1439 splx(s); 1440 return (struct buf *) NULL; 1441 } 1442 bp->b_flags |= B_BUSY | B_CACHE; 1443 bremfree(bp); 1444 1445 /* 1446 * check for size inconsistancies (note that they shouldn't 1447 * happen but do when filesystems don't handle the size changes 1448 * correctly.) We are conservative on metadata and don't just 1449 * extend the buffer but write (if needed) and re-constitute it. 1450 */ 1451 1452 if (bp->b_bcount != size) { 1453 if ((bp->b_flags & B_VMIO) && (size <= bp->b_kvasize)) { 1454 allocbuf(bp, size); 1455 } else { 1456 if (bp->b_flags & B_DELWRI) { 1457 bp->b_flags |= B_NOCACHE; 1458 VOP_BWRITE(bp); 1459 } else { 1460 if (bp->b_flags & B_VMIO) { 1461 bp->b_flags |= B_RELBUF; 1462 brelse(bp); 1463 } else { 1464 bp->b_flags |= B_NOCACHE; 1465 VOP_BWRITE(bp); 1466 } 1467 } 1468 goto loop; 1469 } 1470 } 1471 1472#ifdef DIAGNOSTIC 1473 if (bp->b_offset == NOOFFSET) 1474 panic("getblk: no buffer offset"); 1475#endif 1476 1477 /* 1478 * Check that the constituted buffer really deserves for the 1479 * B_CACHE bit to be set. B_VMIO type buffers might not 1480 * contain fully valid pages. Normal (old-style) buffers 1481 * should be fully valid. 1482 */ 1483 if (bp->b_flags & B_VMIO) { 1484 checksize = bp->b_bufsize; 1485 for (i = 0; i < bp->b_npages; i++) { 1486 int resid; 1487 int poffset; 1488 poffset = bp->b_offset & PAGE_MASK; 1489 resid = (checksize > (PAGE_SIZE - poffset)) ? 1490 (PAGE_SIZE - poffset) : checksize; 1491 if (!vm_page_is_valid(bp->b_pages[i], poffset, resid)) { 1492 bp->b_flags &= ~(B_CACHE | B_DONE); 1493 break; 1494 } 1495 checksize -= resid; 1496 } 1497 } 1498 1499 if (bp->b_usecount < BUF_MAXUSE) 1500 ++bp->b_usecount; 1501 splx(s); 1502 return (bp); 1503 } else { 1504 vm_object_t obj; 1505 1506 if ((bp = getnewbuf(vp, blkno, 1507 slpflag, slptimeo, size, maxsize)) == 0) { 1508 if (slpflag || slptimeo) { 1509 splx(s); 1510 return NULL; 1511 } 1512 goto loop; 1513 } 1514 1515 /* 1516 * This code is used to make sure that a buffer is not 1517 * created while the getnewbuf routine is blocked. 1518 * Normally the vnode is locked so this isn't a problem. 1519 * VBLK type I/O requests, however, don't lock the vnode. 1520 */ 1521 if (!VOP_ISLOCKED(vp) && gbincore(vp, blkno)) { 1522 bp->b_flags |= B_INVAL; 1523 brelse(bp); 1524 goto loop; 1525 } 1526 1527 /* 1528 * Insert the buffer into the hash, so that it can 1529 * be found by incore. 1530 */ 1531 bp->b_blkno = bp->b_lblkno = blkno; 1532 1533 if (vp->v_type != VBLK) 1534 bp->b_offset = (off_t) blkno * maxsize; 1535 else 1536 bp->b_offset = (off_t) blkno * DEV_BSIZE; 1537 1538 bgetvp(vp, bp); 1539 LIST_REMOVE(bp, b_hash); 1540 bh = BUFHASH(vp, blkno); 1541 LIST_INSERT_HEAD(bh, bp, b_hash); 1542 1543 if ((obj = vp->v_object) && (vp->v_flag & VOBJBUF)) { 1544 bp->b_flags |= (B_VMIO | B_CACHE); 1545#if defined(VFS_BIO_DEBUG) 1546 if (vp->v_type != VREG && vp->v_type != VBLK) 1547 printf("getblk: vmioing file type %d???\n", vp->v_type); 1548#endif 1549 } else { 1550 bp->b_flags &= ~B_VMIO; 1551 } 1552 1553 allocbuf(bp, size); 1554 1555 splx(s); 1556 return (bp); 1557 } 1558} 1559 1560/* 1561 * Get an empty, disassociated buffer of given size. 1562 */ 1563struct buf * 1564geteblk(int size) 1565{ 1566 struct buf *bp; 1567 int s; 1568 1569 s = splbio(); 1570 while ((bp = getnewbuf(0, (daddr_t) 0, 0, 0, size, MAXBSIZE)) == 0); 1571 splx(s); 1572 allocbuf(bp, size); 1573 bp->b_flags |= B_INVAL; 1574 return (bp); 1575} 1576 1577 1578/* 1579 * This code constitutes the buffer memory from either anonymous system 1580 * memory (in the case of non-VMIO operations) or from an associated 1581 * VM object (in the case of VMIO operations). 1582 * 1583 * Note that this code is tricky, and has many complications to resolve 1584 * deadlock or inconsistant data situations. Tread lightly!!! 1585 * 1586 * Modify the length of a buffer's underlying buffer storage without 1587 * destroying information (unless, of course the buffer is shrinking). 1588 */ 1589int 1590allocbuf(struct buf * bp, int size) 1591{ 1592 1593 int s; 1594 int newbsize, mbsize; 1595 int i; 1596 1597#if !defined(MAX_PERF) 1598 if (!(bp->b_flags & B_BUSY)) 1599 panic("allocbuf: buffer not busy"); 1600 1601 if (bp->b_kvasize < size) 1602 panic("allocbuf: buffer too small"); 1603#endif 1604 1605 if ((bp->b_flags & B_VMIO) == 0) { 1606 caddr_t origbuf; 1607 int origbufsize; 1608 /* 1609 * Just get anonymous memory from the kernel 1610 */ 1611 mbsize = (size + DEV_BSIZE - 1) & ~(DEV_BSIZE - 1); 1612#if !defined(NO_B_MALLOC) 1613 if (bp->b_flags & B_MALLOC) 1614 newbsize = mbsize; 1615 else 1616#endif 1617 newbsize = round_page(size); 1618 1619 if (newbsize < bp->b_bufsize) { 1620#if !defined(NO_B_MALLOC) 1621 /* 1622 * malloced buffers are not shrunk 1623 */ 1624 if (bp->b_flags & B_MALLOC) { 1625 if (newbsize) { 1626 bp->b_bcount = size; 1627 } else { 1628 free(bp->b_data, M_BIOBUF); 1629 bufspace -= bp->b_bufsize; 1630 bufmallocspace -= bp->b_bufsize; 1631 bp->b_data = bp->b_kvabase; 1632 bp->b_bufsize = 0; 1633 bp->b_bcount = 0; 1634 bp->b_flags &= ~B_MALLOC; 1635 } 1636 return 1; 1637 } 1638#endif 1639 vm_hold_free_pages( 1640 bp, 1641 (vm_offset_t) bp->b_data + newbsize, 1642 (vm_offset_t) bp->b_data + bp->b_bufsize); 1643 } else if (newbsize > bp->b_bufsize) { 1644#if !defined(NO_B_MALLOC) 1645 /* 1646 * We only use malloced memory on the first allocation. 1647 * and revert to page-allocated memory when the buffer grows. 1648 */ 1649 if ( (bufmallocspace < maxbufmallocspace) && 1650 (bp->b_bufsize == 0) && 1651 (mbsize <= PAGE_SIZE/2)) { 1652 1653 bp->b_data = malloc(mbsize, M_BIOBUF, M_WAITOK); 1654 bp->b_bufsize = mbsize; 1655 bp->b_bcount = size; 1656 bp->b_flags |= B_MALLOC; 1657 bufspace += mbsize; 1658 bufmallocspace += mbsize; 1659 return 1; 1660 } 1661#endif 1662 origbuf = NULL; 1663 origbufsize = 0; 1664#if !defined(NO_B_MALLOC) 1665 /* 1666 * If the buffer is growing on its other-than-first allocation, 1667 * then we revert to the page-allocation scheme. 1668 */ 1669 if (bp->b_flags & B_MALLOC) { 1670 origbuf = bp->b_data; 1671 origbufsize = bp->b_bufsize; 1672 bp->b_data = bp->b_kvabase; 1673 bufspace -= bp->b_bufsize; 1674 bufmallocspace -= bp->b_bufsize; 1675 bp->b_bufsize = 0; 1676 bp->b_flags &= ~B_MALLOC; 1677 newbsize = round_page(newbsize); 1678 } 1679#endif 1680 vm_hold_load_pages( 1681 bp, 1682 (vm_offset_t) bp->b_data + bp->b_bufsize, 1683 (vm_offset_t) bp->b_data + newbsize); 1684#if !defined(NO_B_MALLOC) 1685 if (origbuf) { 1686 bcopy(origbuf, bp->b_data, origbufsize); 1687 free(origbuf, M_BIOBUF); 1688 } 1689#endif 1690 } 1691 } else { 1692 vm_page_t m; 1693 int desiredpages; 1694 1695 newbsize = (size + DEV_BSIZE - 1) & ~(DEV_BSIZE - 1); 1696 desiredpages = (round_page(newbsize) >> PAGE_SHIFT); 1697 1698#if !defined(NO_B_MALLOC) 1699 if (bp->b_flags & B_MALLOC) 1700 panic("allocbuf: VMIO buffer can't be malloced"); 1701#endif 1702 1703 if (newbsize < bp->b_bufsize) { 1704 if (desiredpages < bp->b_npages) { 1705 for (i = desiredpages; i < bp->b_npages; i++) { 1706 /* 1707 * the page is not freed here -- it 1708 * is the responsibility of vnode_pager_setsize 1709 */ 1710 m = bp->b_pages[i]; 1711#if defined(DIAGNOSTIC) 1712 if (m == bogus_page) 1713 panic("allocbuf: bogus page found"); 1714#endif 1715 vm_page_sleep(m, "biodep", &m->busy); 1716 1717 bp->b_pages[i] = NULL; 1718 vm_page_unwire(m); 1719 } 1720 pmap_qremove((vm_offset_t) trunc_page(bp->b_data) + 1721 (desiredpages << PAGE_SHIFT), (bp->b_npages - desiredpages)); 1722 bp->b_npages = desiredpages; 1723 } 1724 } else if (newbsize > bp->b_bufsize) { 1725 vm_object_t obj; 1726 vm_offset_t tinc, toff; 1727 vm_ooffset_t off; 1728 vm_pindex_t objoff; 1729 int pageindex, curbpnpages; 1730 struct vnode *vp; 1731 int bsize; 1732 int orig_validoff = bp->b_validoff; 1733 int orig_validend = bp->b_validend; 1734 1735 vp = bp->b_vp; 1736 1737 if (vp->v_type == VBLK) 1738 bsize = DEV_BSIZE; 1739 else 1740 bsize = vp->v_mount->mnt_stat.f_iosize; 1741 1742 if (bp->b_npages < desiredpages) { 1743 obj = vp->v_object; 1744 tinc = PAGE_SIZE; 1745 if (tinc > bsize) 1746 tinc = bsize; 1747 1748 off = bp->b_offset; 1749#ifdef DIAGNOSTIC 1750 if (bp->b_offset == NOOFFSET) 1751 panic("allocbuf: no buffer offset"); 1752#endif 1753 1754 curbpnpages = bp->b_npages; 1755 doretry: 1756 bp->b_validoff = orig_validoff; 1757 bp->b_validend = orig_validend; 1758 bp->b_flags |= B_CACHE; 1759 for (toff = 0; toff < newbsize; toff += tinc) { 1760 int bytesinpage; 1761 1762 pageindex = toff >> PAGE_SHIFT; 1763 objoff = OFF_TO_IDX(off + toff); 1764 if (pageindex < curbpnpages) { 1765 1766 m = bp->b_pages[pageindex]; 1767#ifdef VFS_BIO_DIAG 1768 if (m->pindex != objoff) 1769 panic("allocbuf: page changed offset??!!!?"); 1770#endif 1771 bytesinpage = tinc; 1772 if (tinc > (newbsize - toff)) 1773 bytesinpage = newbsize - toff; 1774 if (bp->b_flags & B_CACHE) 1775 vfs_buf_set_valid(bp, off, toff, bytesinpage, m); 1776 continue; 1777 } 1778 m = vm_page_lookup(obj, objoff); 1779 if (!m) { 1780 m = vm_page_alloc(obj, objoff, VM_ALLOC_NORMAL); 1781 if (!m) { 1782 VM_WAIT; 1783 vm_pageout_deficit += (desiredpages - bp->b_npages); 1784 goto doretry; 1785 } 1786 1787 vm_page_wire(m); 1788 m->flags &= ~PG_BUSY; 1789 bp->b_flags &= ~B_CACHE; 1790 1791 } else if (m->flags & PG_BUSY) { 1792 s = splvm(); 1793 if (m->flags & PG_BUSY) { 1794 m->flags |= PG_WANTED; 1795 tsleep(m, PVM, "pgtblk", 0); 1796 } 1797 splx(s); 1798 goto doretry; 1799 } else { 1800 if ((curproc != pageproc) && 1801 ((m->queue - m->pc) == PQ_CACHE) && 1802 ((cnt.v_free_count + cnt.v_cache_count) < 1803 (cnt.v_free_min + cnt.v_cache_min))) { 1804 pagedaemon_wakeup(); 1805 } 1806 bytesinpage = tinc; 1807 if (tinc > (newbsize - toff)) 1808 bytesinpage = newbsize - toff; 1809 if (bp->b_flags & B_CACHE) 1810 vfs_buf_set_valid(bp, off, toff, bytesinpage, m); 1811 m->flags &= ~PG_ZERO; 1812 vm_page_wire(m); 1813 } 1814 bp->b_pages[pageindex] = m; 1815 curbpnpages = pageindex + 1; 1816 } 1817 if (vp->v_tag == VT_NFS && 1818 vp->v_type != VBLK) { 1819 if (bp->b_dirtyend > 0) { 1820 bp->b_validoff = min(bp->b_validoff, bp->b_dirtyoff); 1821 bp->b_validend = max(bp->b_validend, bp->b_dirtyend); 1822 } 1823 if (bp->b_validend == 0) 1824 bp->b_flags &= ~B_CACHE; 1825 } 1826 bp->b_data = (caddr_t) trunc_page(bp->b_data); 1827 bp->b_npages = curbpnpages; 1828 pmap_qenter((vm_offset_t) bp->b_data, 1829 bp->b_pages, bp->b_npages); 1830 ((vm_offset_t) bp->b_data) |= off & PAGE_MASK; 1831 } 1832 } 1833 } 1834 if (bp->b_flags & B_VMIO) 1835 vmiospace += (newbsize - bp->b_bufsize); 1836 bufspace += (newbsize - bp->b_bufsize); 1837 bp->b_bufsize = newbsize; 1838 bp->b_bcount = size; 1839 return 1; 1840} 1841 1842/* 1843 * Wait for buffer I/O completion, returning error status. 1844 */ 1845int 1846biowait(register struct buf * bp) 1847{ 1848 int s; 1849 1850 s = splbio(); 1851 while ((bp->b_flags & B_DONE) == 0) 1852#if defined(NO_SCHEDULE_MODS) 1853 tsleep(bp, PRIBIO, "biowait", 0); 1854#else 1855 if (bp->b_flags & B_READ) 1856 tsleep(bp, PRIBIO, "biord", 0); 1857 else 1858 tsleep(bp, PRIBIO, "biowr", 0); 1859#endif 1860 splx(s); 1861 if (bp->b_flags & B_EINTR) { 1862 bp->b_flags &= ~B_EINTR; 1863 return (EINTR); 1864 } 1865 if (bp->b_flags & B_ERROR) { 1866 return (bp->b_error ? bp->b_error : EIO); 1867 } else { 1868 return (0); 1869 } 1870} 1871 1872/* 1873 * Finish I/O on a buffer, calling an optional function. 1874 * This is usually called from interrupt level, so process blocking 1875 * is not *a good idea*. 1876 */ 1877void 1878biodone(register struct buf * bp) 1879{ 1880 int s; 1881 1882 s = splbio(); 1883 1884#if !defined(MAX_PERF) 1885 if (!(bp->b_flags & B_BUSY)) 1886 panic("biodone: buffer not busy"); 1887#endif 1888 1889 if (bp->b_flags & B_DONE) { 1890 splx(s); 1891#if !defined(MAX_PERF) 1892 printf("biodone: buffer already done\n"); 1893#endif 1894 return; 1895 } 1896 bp->b_flags |= B_DONE; 1897 1898 if ((bp->b_flags & B_READ) == 0) { 1899 vwakeup(bp); 1900 } 1901 1902#ifdef BOUNCE_BUFFERS 1903 if (bp->b_flags & B_BOUNCE) { 1904 vm_bounce_free(bp); 1905 } 1906#endif 1907 1908 /* call optional completion function if requested */ 1909 if (bp->b_flags & B_CALL) { 1910 bp->b_flags &= ~B_CALL; 1911 (*bp->b_iodone) (bp); 1912 splx(s); 1913 return; 1914 } 1915 if (LIST_FIRST(&bp->b_dep) != NULL && bioops.io_complete) 1916 (*bioops.io_complete)(bp); 1917 1918 if (bp->b_flags & B_VMIO) { 1919 int i, resid; 1920 vm_ooffset_t foff; 1921 vm_page_t m; 1922 vm_object_t obj; 1923 int iosize; 1924 struct vnode *vp = bp->b_vp; 1925 1926 obj = vp->v_object; 1927 1928#if defined(VFS_BIO_DEBUG) 1929 if (vp->v_usecount == 0) { 1930 panic("biodone: zero vnode ref count"); 1931 } 1932 1933 if (vp->v_object == NULL) { 1934 panic("biodone: missing VM object"); 1935 } 1936 1937 if ((vp->v_flag & VOBJBUF) == 0) { 1938 panic("biodone: vnode is not setup for merged cache"); 1939 } 1940#endif 1941 1942 foff = bp->b_offset; 1943#ifdef DIAGNOSTIC 1944 if (bp->b_offset == NOOFFSET) 1945 panic("biodone: no buffer offset"); 1946#endif 1947 1948#if !defined(MAX_PERF) 1949 if (!obj) { 1950 panic("biodone: no object"); 1951 } 1952#endif 1953#if defined(VFS_BIO_DEBUG) 1954 if (obj->paging_in_progress < bp->b_npages) { 1955 printf("biodone: paging in progress(%d) < bp->b_npages(%d)\n", 1956 obj->paging_in_progress, bp->b_npages); 1957 } 1958#endif 1959 iosize = bp->b_bufsize; 1960 for (i = 0; i < bp->b_npages; i++) { 1961 int bogusflag = 0; 1962 m = bp->b_pages[i]; 1963 if (m == bogus_page) { 1964 bogusflag = 1; 1965 m = vm_page_lookup(obj, OFF_TO_IDX(foff)); 1966 if (!m) { 1967#if defined(VFS_BIO_DEBUG) 1968 printf("biodone: page disappeared\n"); 1969#endif 1970 --obj->paging_in_progress; 1971 continue; 1972 } 1973 bp->b_pages[i] = m; 1974 pmap_qenter(trunc_page(bp->b_data), bp->b_pages, bp->b_npages); 1975 } 1976#if defined(VFS_BIO_DEBUG) 1977 if (OFF_TO_IDX(foff) != m->pindex) { 1978 printf("biodone: foff(%d)/m->pindex(%d) mismatch\n", foff, m->pindex); 1979 } 1980#endif 1981 resid = IDX_TO_OFF(m->pindex + 1) - foff; 1982 if (resid > iosize) 1983 resid = iosize; 1984 1985 /* 1986 * In the write case, the valid and clean bits are 1987 * already changed correctly, so we only need to do this 1988 * here in the read case. 1989 */ 1990 if ((bp->b_flags & B_READ) && !bogusflag && resid > 0) { 1991 vfs_page_set_valid(bp, foff, i, m); 1992 } 1993 m->flags &= ~PG_ZERO; 1994 1995 /* 1996 * when debugging new filesystems or buffer I/O methods, this 1997 * is the most common error that pops up. if you see this, you 1998 * have not set the page busy flag correctly!!! 1999 */ 2000 if (m->busy == 0) { 2001#if !defined(MAX_PERF) 2002 printf("biodone: page busy < 0, " 2003 "pindex: %d, foff: 0x(%x,%x), " 2004 "resid: %d, index: %d\n", 2005 (int) m->pindex, (int)(foff >> 32), 2006 (int) foff & 0xffffffff, resid, i); 2007#endif 2008 if (vp->v_type != VBLK) 2009#if !defined(MAX_PERF) 2010 printf(" iosize: %ld, lblkno: %d, flags: 0x%lx, npages: %d\n", 2011 bp->b_vp->v_mount->mnt_stat.f_iosize, 2012 (int) bp->b_lblkno, 2013 bp->b_flags, bp->b_npages); 2014 else 2015 printf(" VDEV, lblkno: %d, flags: 0x%lx, npages: %d\n", 2016 (int) bp->b_lblkno, 2017 bp->b_flags, bp->b_npages); 2018 printf(" valid: 0x%x, dirty: 0x%x, wired: %d\n", 2019 m->valid, m->dirty, m->wire_count); 2020#endif 2021 panic("biodone: page busy < 0\n"); 2022 } 2023 PAGE_BWAKEUP(m); 2024 --obj->paging_in_progress; 2025 foff += resid; 2026 iosize -= resid; 2027 } 2028 if (obj && 2029 (obj->paging_in_progress == 0) && 2030 (obj->flags & OBJ_PIPWNT)) { 2031 obj->flags &= ~OBJ_PIPWNT; 2032 wakeup(obj); 2033 } 2034 } 2035 /* 2036 * For asynchronous completions, release the buffer now. The brelse 2037 * checks for B_WANTED and will do the wakeup there if necessary - so 2038 * no need to do a wakeup here in the async case. 2039 */ 2040 2041 if (bp->b_flags & B_ASYNC) { 2042 if ((bp->b_flags & (B_NOCACHE | B_INVAL | B_ERROR | B_RELBUF)) != 0) 2043 brelse(bp); 2044 else 2045 bqrelse(bp); 2046 } else { 2047 bp->b_flags &= ~B_WANTED; 2048 wakeup(bp); 2049 } 2050 splx(s); 2051} 2052 2053static int 2054count_lock_queue() 2055{ 2056 int count; 2057 struct buf *bp; 2058 2059 count = 0; 2060 for (bp = TAILQ_FIRST(&bufqueues[QUEUE_LOCKED]); 2061 bp != NULL; 2062 bp = TAILQ_NEXT(bp, b_freelist)) 2063 count++; 2064 return (count); 2065} 2066 2067#if 0 /* not with kirks code */ 2068static int vfs_update_interval = 30; 2069 2070static void 2071vfs_update() 2072{ 2073 while (1) { 2074 tsleep(&vfs_update_wakeup, PUSER, "update", 2075 hz * vfs_update_interval); 2076 vfs_update_wakeup = 0; 2077 sync(curproc, NULL); 2078 } 2079} 2080 2081static int 2082sysctl_kern_updateinterval SYSCTL_HANDLER_ARGS 2083{ 2084 int error = sysctl_handle_int(oidp, 2085 oidp->oid_arg1, oidp->oid_arg2, req); 2086 if (!error) 2087 wakeup(&vfs_update_wakeup); 2088 return error; 2089} 2090 2091SYSCTL_PROC(_kern, KERN_UPDATEINTERVAL, update, CTLTYPE_INT|CTLFLAG_RW, 2092 &vfs_update_interval, 0, sysctl_kern_updateinterval, "I", ""); 2093 2094#endif 2095 2096 2097/* 2098 * This routine is called in lieu of iodone in the case of 2099 * incomplete I/O. This keeps the busy status for pages 2100 * consistant. 2101 */ 2102void 2103vfs_unbusy_pages(struct buf * bp) 2104{ 2105 int i; 2106 2107 if (bp->b_flags & B_VMIO) { 2108 struct vnode *vp = bp->b_vp; 2109 vm_object_t obj = vp->v_object; 2110 2111 for (i = 0; i < bp->b_npages; i++) { 2112 vm_page_t m = bp->b_pages[i]; 2113 2114 if (m == bogus_page) { 2115 m = vm_page_lookup(obj, OFF_TO_IDX(bp->b_offset) + i); 2116#if !defined(MAX_PERF) 2117 if (!m) { 2118 panic("vfs_unbusy_pages: page missing\n"); 2119 } 2120#endif 2121 bp->b_pages[i] = m; 2122 pmap_qenter(trunc_page(bp->b_data), bp->b_pages, bp->b_npages); 2123 } 2124 --obj->paging_in_progress; 2125 m->flags &= ~PG_ZERO; 2126 PAGE_BWAKEUP(m); 2127 } 2128 if (obj->paging_in_progress == 0 && 2129 (obj->flags & OBJ_PIPWNT)) { 2130 obj->flags &= ~OBJ_PIPWNT; 2131 wakeup(obj); 2132 } 2133 } 2134} 2135 2136/* 2137 * Set NFS' b_validoff and b_validend fields from the valid bits 2138 * of a page. If the consumer is not NFS, and the page is not 2139 * valid for the entire range, clear the B_CACHE flag to force 2140 * the consumer to re-read the page. 2141 */ 2142static void 2143vfs_buf_set_valid(struct buf *bp, 2144 vm_ooffset_t foff, vm_offset_t off, vm_offset_t size, 2145 vm_page_t m) 2146{ 2147 if (bp->b_vp->v_tag == VT_NFS && bp->b_vp->v_type != VBLK) { 2148 vm_offset_t svalid, evalid; 2149 int validbits = m->valid; 2150 2151 /* 2152 * This only bothers with the first valid range in the 2153 * page. 2154 */ 2155 svalid = off; 2156 while (validbits && !(validbits & 1)) { 2157 svalid += DEV_BSIZE; 2158 validbits >>= 1; 2159 } 2160 evalid = svalid; 2161 while (validbits & 1) { 2162 evalid += DEV_BSIZE; 2163 validbits >>= 1; 2164 } 2165 /* 2166 * Make sure this range is contiguous with the range 2167 * built up from previous pages. If not, then we will 2168 * just use the range from the previous pages. 2169 */ 2170 if (svalid == bp->b_validend) { 2171 bp->b_validoff = min(bp->b_validoff, svalid); 2172 bp->b_validend = max(bp->b_validend, evalid); 2173 } 2174 } else if (!vm_page_is_valid(m, 2175 (vm_offset_t) ((foff + off) & PAGE_MASK), 2176 size)) { 2177 bp->b_flags &= ~B_CACHE; 2178 } 2179} 2180 2181/* 2182 * Set the valid bits in a page, taking care of the b_validoff, 2183 * b_validend fields which NFS uses to optimise small reads. Off is 2184 * the offset within the file and pageno is the page index within the buf. 2185 */ 2186static void 2187vfs_page_set_valid(struct buf *bp, vm_ooffset_t off, int pageno, vm_page_t m) 2188{ 2189 struct vnode *vp = bp->b_vp; 2190 vm_ooffset_t soff, eoff; 2191 2192 soff = off; 2193 eoff = off + min(PAGE_SIZE, bp->b_bufsize); 2194 if (vp->v_tag == VT_NFS && vp->v_type != VBLK) { 2195 vm_ooffset_t sv, ev; 2196 vm_page_set_invalid(m, 2197 (vm_offset_t) (soff & PAGE_MASK), 2198 (vm_offset_t) (eoff - soff)); 2199 off = off - pageno * PAGE_SIZE; 2200 sv = off + ((bp->b_validoff + DEV_BSIZE - 1) & ~(DEV_BSIZE - 1)); 2201 ev = off + ((bp->b_validend + DEV_BSIZE - 1) & ~(DEV_BSIZE - 1)); 2202 soff = max(sv, soff); 2203 eoff = min(ev, eoff); 2204 } 2205 if (eoff > soff) 2206 vm_page_set_validclean(m, 2207 (vm_offset_t) (soff & PAGE_MASK), 2208 (vm_offset_t) (eoff - soff)); 2209} 2210 2211/* 2212 * This routine is called before a device strategy routine. 2213 * It is used to tell the VM system that paging I/O is in 2214 * progress, and treat the pages associated with the buffer 2215 * almost as being PG_BUSY. Also the object paging_in_progress 2216 * flag is handled to make sure that the object doesn't become 2217 * inconsistant. 2218 */ 2219void 2220vfs_busy_pages(struct buf * bp, int clear_modify) 2221{ 2222 int i,s; 2223 2224 if (bp->b_flags & B_VMIO) { 2225 struct vnode *vp = bp->b_vp; 2226 vm_object_t obj = vp->v_object; 2227 vm_ooffset_t foff; 2228 2229 foff = bp->b_offset; 2230#ifdef DIAGNOSTIC 2231 if (bp->b_offset == NOOFFSET) 2232 panic("vfs_busy_pages: no buffer offset"); 2233#endif 2234 2235 vfs_setdirty(bp); 2236 2237retry: 2238 for (i = 0; i < bp->b_npages; i++) { 2239 vm_page_t m = bp->b_pages[i]; 2240 if (vm_page_sleep(m, "vbpage", NULL)) 2241 goto retry; 2242 } 2243 2244 for (i = 0; i < bp->b_npages; i++, foff += PAGE_SIZE) { 2245 vm_page_t m = bp->b_pages[i]; 2246 2247 m->flags &= ~PG_ZERO; 2248 if ((bp->b_flags & B_CLUSTER) == 0) { 2249 obj->paging_in_progress++; 2250 m->busy++; 2251 } 2252 2253 vm_page_protect(m, VM_PROT_NONE); 2254 if (clear_modify) 2255 vfs_page_set_valid(bp, foff, i, m); 2256 else if (bp->b_bcount >= PAGE_SIZE) { 2257 if (m->valid && (bp->b_flags & B_CACHE) == 0) { 2258 bp->b_pages[i] = bogus_page; 2259 pmap_qenter(trunc_page(bp->b_data), bp->b_pages, bp->b_npages); 2260 } 2261 } 2262 } 2263 } 2264} 2265 2266/* 2267 * Tell the VM system that the pages associated with this buffer 2268 * are clean. This is used for delayed writes where the data is 2269 * going to go to disk eventually without additional VM intevention. 2270 */ 2271void 2272vfs_clean_pages(struct buf * bp) 2273{ 2274 int i; 2275 2276 if (bp->b_flags & B_VMIO) { 2277 struct vnode *vp = bp->b_vp; 2278 vm_ooffset_t foff; 2279 foff = bp->b_offset; 2280 2281#ifdef DIAGNOSTIC 2282 if (bp->b_offset == NOOFFSET) 2283 panic("vfs_clean_pages: no buffer offset"); 2284#endif 2285 2286 for (i = 0; i < bp->b_npages; i++, foff += PAGE_SIZE) { 2287 vm_page_t m = bp->b_pages[i]; 2288 vfs_page_set_valid(bp, foff, i, m); 2289 } 2290 } 2291} 2292 2293void 2294vfs_bio_clrbuf(struct buf *bp) { 2295 int i; 2296 if ((bp->b_flags & (B_VMIO | B_MALLOC)) == B_VMIO) { 2297 if( (bp->b_npages == 1) && (bp->b_bufsize < PAGE_SIZE)) { 2298 int mask; 2299 mask = 0; 2300 for(i=0;i<bp->b_bufsize;i+=DEV_BSIZE) 2301 mask |= (1 << (i/DEV_BSIZE)); 2302 if(((bp->b_pages[0]->flags & PG_ZERO) == 0) && 2303 (bp->b_pages[0]->valid != mask)) { 2304 bzero(bp->b_data, bp->b_bufsize); 2305 } 2306 bp->b_pages[0]->valid = mask; 2307 bp->b_resid = 0; 2308 return; 2309 } 2310 for(i=0;i<bp->b_npages;i++) { 2311 if( bp->b_pages[i]->valid == VM_PAGE_BITS_ALL) 2312 continue; 2313 if( bp->b_pages[i]->valid == 0) { 2314 if ((bp->b_pages[i]->flags & PG_ZERO) == 0) { 2315 bzero(bp->b_data + (i << PAGE_SHIFT), PAGE_SIZE); 2316 } 2317 } else { 2318 int j; 2319 for(j=0;j<PAGE_SIZE/DEV_BSIZE;j++) { 2320 if (((bp->b_pages[i]->flags & PG_ZERO) == 0) && 2321 (bp->b_pages[i]->valid & (1<<j)) == 0) 2322 bzero(bp->b_data + (i << PAGE_SHIFT) + j * DEV_BSIZE, DEV_BSIZE); 2323 } 2324 } 2325 bp->b_pages[i]->valid = VM_PAGE_BITS_ALL; 2326 bp->b_pages[i]->flags &= ~PG_ZERO; 2327 } 2328 bp->b_resid = 0; 2329 } else { 2330 clrbuf(bp); 2331 } 2332} 2333 2334/* 2335 * vm_hold_load_pages and vm_hold_unload pages get pages into 2336 * a buffers address space. The pages are anonymous and are 2337 * not associated with a file object. 2338 */ 2339void 2340vm_hold_load_pages(struct buf * bp, vm_offset_t from, vm_offset_t to) 2341{ 2342 vm_offset_t pg; 2343 vm_page_t p; 2344 int index; 2345 2346 to = round_page(to); 2347 from = round_page(from); 2348 index = (from - trunc_page(bp->b_data)) >> PAGE_SHIFT; 2349 2350 for (pg = from; pg < to; pg += PAGE_SIZE, index++) { 2351 2352tryagain: 2353 2354 p = vm_page_alloc(kernel_object, 2355 ((pg - VM_MIN_KERNEL_ADDRESS) >> PAGE_SHIFT), 2356 VM_ALLOC_NORMAL); 2357 if (!p) { 2358 vm_pageout_deficit += (to - from) >> PAGE_SHIFT; 2359 VM_WAIT; 2360 goto tryagain; 2361 } 2362 vm_page_wire(p); 2363 p->valid = VM_PAGE_BITS_ALL; 2364 p->flags &= ~PG_ZERO; 2365 pmap_kenter(pg, VM_PAGE_TO_PHYS(p)); 2366 bp->b_pages[index] = p; 2367 PAGE_WAKEUP(p); 2368 } 2369 bp->b_npages = index; 2370} 2371 2372void 2373vm_hold_free_pages(struct buf * bp, vm_offset_t from, vm_offset_t to) 2374{ 2375 vm_offset_t pg; 2376 vm_page_t p; 2377 int index, newnpages; 2378 2379 from = round_page(from); 2380 to = round_page(to); 2381 newnpages = index = (from - trunc_page(bp->b_data)) >> PAGE_SHIFT; 2382 2383 for (pg = from; pg < to; pg += PAGE_SIZE, index++) { 2384 p = bp->b_pages[index]; 2385 if (p && (index < bp->b_npages)) { 2386#if !defined(MAX_PERF) 2387 if (p->busy) { 2388 printf("vm_hold_free_pages: blkno: %d, lblkno: %d\n", 2389 bp->b_blkno, bp->b_lblkno); 2390 } 2391#endif 2392 bp->b_pages[index] = NULL; 2393 pmap_kremove(pg); 2394 p->flags |= PG_BUSY; 2395 vm_page_unwire(p); 2396 vm_page_free(p); 2397 } 2398 } 2399 bp->b_npages = newnpages; 2400} 2401 2402 2403#include "opt_ddb.h" 2404#ifdef DDB 2405#include <ddb/ddb.h> 2406 2407DB_SHOW_COMMAND(buffer, db_show_buffer) 2408{ 2409 /* get args */ 2410 struct buf *bp = (struct buf *)addr; 2411 2412 if (!have_addr) { 2413 db_printf("usage: show buffer <addr>\n"); 2414 return; 2415 } 2416 2417 db_printf("b_proc = %p,\nb_flags = 0x%b\n", (void *)bp->b_proc, 2418 bp->b_flags, "\20\40bounce\37cluster\36vmio\35ram\34ordered" 2419 "\33paging\32xxx\31writeinprog\30wanted\27relbuf\26tape" 2420 "\25read\24raw\23phys\22clusterok\21malloc\20nocache" 2421 "\17locked\16inval\15gathered\14error\13eintr\12done\11dirty" 2422 "\10delwri\7call\6cache\5busy\4bad\3async\2needcommit\1age"); 2423 db_printf("b_error = %d, b_bufsize = %ld, b_bcount = %ld, " 2424 "b_resid = %ld\nb_dev = 0x%x, b_data = %p, " 2425 "b_blkno = %d, b_pblkno = %d\n", 2426 bp->b_error, bp->b_bufsize, bp->b_bcount, bp->b_resid, 2427 bp->b_dev, bp->b_data, bp->b_blkno, bp->b_pblkno); 2428 if (bp->b_npages) { 2429 int i; 2430 db_printf("b_npages = %d, pages(OBJ, IDX, PA): ", bp->b_npages); 2431 for (i = 0; i < bp->b_npages; i++) { 2432 vm_page_t m; 2433 m = bp->b_pages[i]; 2434 db_printf("(0x%x, 0x%x, 0x%x)", m->object, m->pindex, 2435 VM_PAGE_TO_PHYS(m)); 2436 if ((i + 1) < bp->b_npages) 2437 db_printf(","); 2438 } 2439 db_printf("\n"); 2440 } 2441} 2442#endif /* DDB */ 2443