vfs_bio.c revision 37615
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.166 1998/07/08 01:04:27 julian 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(vp, 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(vp, 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(vp, 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->b_vp, 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, 1326 (vm_offset_t) ((toff + off) & PAGE_MASK), tinc) == 0) 1327 return 0; 1328 } 1329 return 1; 1330} 1331 1332/* 1333 * now we set the dirty range for the buffer -- 1334 * for NFS -- if the file is mapped and pages have 1335 * been written to, let it know. We want the 1336 * entire range of the buffer to be marked dirty if 1337 * any of the pages have been written to for consistancy 1338 * with the b_validoff, b_validend set in the nfs write 1339 * code, and used by the nfs read code. 1340 */ 1341static void 1342vfs_setdirty(struct buf *bp) { 1343 int i; 1344 vm_object_t object; 1345 vm_offset_t boffset, offset; 1346 /* 1347 * We qualify the scan for modified pages on whether the 1348 * object has been flushed yet. The OBJ_WRITEABLE flag 1349 * is not cleared simply by protecting pages off. 1350 */ 1351 if ((bp->b_flags & B_VMIO) && 1352 ((object = bp->b_pages[0]->object)->flags & (OBJ_WRITEABLE|OBJ_CLEANING))) { 1353 /* 1354 * test the pages to see if they have been modified directly 1355 * by users through the VM system. 1356 */ 1357 for (i = 0; i < bp->b_npages; i++) { 1358 bp->b_pages[i]->flags &= ~PG_ZERO; 1359 vm_page_test_dirty(bp->b_pages[i]); 1360 } 1361 1362 /* 1363 * scan forwards for the first page modified 1364 */ 1365 for (i = 0; i < bp->b_npages; i++) { 1366 if (bp->b_pages[i]->dirty) { 1367 break; 1368 } 1369 } 1370 boffset = (i << PAGE_SHIFT); 1371 if (boffset < bp->b_dirtyoff) { 1372 bp->b_dirtyoff = boffset; 1373 } 1374 1375 /* 1376 * scan backwards for the last page modified 1377 */ 1378 for (i = bp->b_npages - 1; i >= 0; --i) { 1379 if (bp->b_pages[i]->dirty) { 1380 break; 1381 } 1382 } 1383 boffset = (i + 1); 1384 offset = boffset + bp->b_pages[0]->pindex; 1385 if (offset >= object->size) 1386 boffset = object->size - bp->b_pages[0]->pindex; 1387 if (bp->b_dirtyend < (boffset << PAGE_SHIFT)) 1388 bp->b_dirtyend = (boffset << PAGE_SHIFT); 1389 } 1390} 1391 1392/* 1393 * Get a block given a specified block and offset into a file/device. 1394 */ 1395struct buf * 1396getblk(struct vnode * vp, daddr_t blkno, int size, int slpflag, int slptimeo) 1397{ 1398 struct buf *bp; 1399 int i, s; 1400 struct bufhashhdr *bh; 1401 int maxsize; 1402 int generation; 1403 int checksize; 1404 1405 if (vp->v_mount) { 1406 maxsize = vp->v_mount->mnt_stat.f_iosize; 1407 /* 1408 * This happens on mount points. 1409 */ 1410 if (maxsize < size) 1411 maxsize = size; 1412 } else { 1413 maxsize = size; 1414 } 1415 1416#if !defined(MAX_PERF) 1417 if (size > MAXBSIZE) 1418 panic("getblk: size(%d) > MAXBSIZE(%d)\n", size, MAXBSIZE); 1419#endif 1420 1421 s = splbio(); 1422loop: 1423 if (numfreebuffers < lofreebuffers) { 1424 waitfreebuffers(slpflag, slptimeo); 1425 } 1426 1427 if ((bp = gbincore(vp, blkno))) { 1428loop1: 1429 if (bp->b_flags & B_BUSY) { 1430 1431 bp->b_flags |= B_WANTED; 1432 if (bp->b_usecount < BUF_MAXUSE) 1433 ++bp->b_usecount; 1434 1435 if (!tsleep(bp, 1436 (PRIBIO + 4) | slpflag, "getblk", slptimeo)) { 1437 goto loop; 1438 } 1439 1440 splx(s); 1441 return (struct buf *) NULL; 1442 } 1443 bp->b_flags |= B_BUSY | B_CACHE; 1444 bremfree(bp); 1445 1446 /* 1447 * check for size inconsistancies (note that they shouldn't 1448 * happen but do when filesystems don't handle the size changes 1449 * correctly.) We are conservative on metadata and don't just 1450 * extend the buffer but write (if needed) and re-constitute it. 1451 */ 1452 1453 if (bp->b_bcount != size) { 1454 if ((bp->b_flags & B_VMIO) && (size <= bp->b_kvasize)) { 1455 allocbuf(bp, size); 1456 } else { 1457 if (bp->b_flags & B_DELWRI) { 1458 bp->b_flags |= B_NOCACHE; 1459 VOP_BWRITE(bp); 1460 } else { 1461 if ((bp->b_flags & B_VMIO) && 1462 (LIST_FIRST(&bp->b_dep) == NULL)) { 1463 bp->b_flags |= B_RELBUF; 1464 brelse(bp); 1465 } else { 1466 bp->b_flags |= B_NOCACHE; 1467 VOP_BWRITE(bp); 1468 } 1469 } 1470 goto loop; 1471 } 1472 } 1473 1474#ifdef DIAGNOSTIC 1475 if (bp->b_offset == NOOFFSET) 1476 panic("getblk: no buffer offset"); 1477#endif 1478 1479 /* 1480 * Check that the constituted buffer really deserves for the 1481 * B_CACHE bit to be set. B_VMIO type buffers might not 1482 * contain fully valid pages. Normal (old-style) buffers 1483 * should be fully valid. 1484 */ 1485 if (bp->b_flags & B_VMIO) { 1486 checksize = bp->b_bufsize; 1487 for (i = 0; i < bp->b_npages; i++) { 1488 int resid; 1489 int poffset; 1490 poffset = bp->b_offset & PAGE_MASK; 1491 resid = (checksize > (PAGE_SIZE - poffset)) ? 1492 (PAGE_SIZE - poffset) : checksize; 1493 if (!vm_page_is_valid(bp->b_pages[i], poffset, resid)) { 1494 bp->b_flags &= ~(B_CACHE | B_DONE); 1495 break; 1496 } 1497 checksize -= resid; 1498 } 1499 } 1500 1501 if (bp->b_usecount < BUF_MAXUSE) 1502 ++bp->b_usecount; 1503 splx(s); 1504 return (bp); 1505 } else { 1506 vm_object_t obj; 1507 1508 if ((bp = getnewbuf(vp, blkno, 1509 slpflag, slptimeo, size, maxsize)) == 0) { 1510 if (slpflag || slptimeo) { 1511 splx(s); 1512 return NULL; 1513 } 1514 goto loop; 1515 } 1516 1517 /* 1518 * This code is used to make sure that a buffer is not 1519 * created while the getnewbuf routine is blocked. 1520 * Normally the vnode is locked so this isn't a problem. 1521 * VBLK type I/O requests, however, don't lock the vnode. 1522 */ 1523 if (!VOP_ISLOCKED(vp) && gbincore(vp, blkno)) { 1524 bp->b_flags |= B_INVAL; 1525 brelse(bp); 1526 goto loop; 1527 } 1528 1529 /* 1530 * Insert the buffer into the hash, so that it can 1531 * be found by incore. 1532 */ 1533 bp->b_blkno = bp->b_lblkno = blkno; 1534 1535 if (vp->v_type != VBLK) 1536 bp->b_offset = (off_t) blkno * maxsize; 1537 else 1538 bp->b_offset = (off_t) blkno * DEV_BSIZE; 1539 1540 bgetvp(vp, bp); 1541 LIST_REMOVE(bp, b_hash); 1542 bh = BUFHASH(vp, blkno); 1543 LIST_INSERT_HEAD(bh, bp, b_hash); 1544 1545 if ((obj = vp->v_object) && (vp->v_flag & VOBJBUF)) { 1546 bp->b_flags |= (B_VMIO | B_CACHE); 1547#if defined(VFS_BIO_DEBUG) 1548 if (vp->v_type != VREG && vp->v_type != VBLK) 1549 printf("getblk: vmioing file type %d???\n", vp->v_type); 1550#endif 1551 } else { 1552 bp->b_flags &= ~B_VMIO; 1553 } 1554 1555 allocbuf(bp, size); 1556 1557 splx(s); 1558 return (bp); 1559 } 1560} 1561 1562/* 1563 * Get an empty, disassociated buffer of given size. 1564 */ 1565struct buf * 1566geteblk(int size) 1567{ 1568 struct buf *bp; 1569 int s; 1570 1571 s = splbio(); 1572 while ((bp = getnewbuf(0, (daddr_t) 0, 0, 0, size, MAXBSIZE)) == 0); 1573 splx(s); 1574 allocbuf(bp, size); 1575 bp->b_flags |= B_INVAL; /* b_dep cleared by getnewbuf() */ 1576 return (bp); 1577} 1578 1579 1580/* 1581 * This code constitutes the buffer memory from either anonymous system 1582 * memory (in the case of non-VMIO operations) or from an associated 1583 * VM object (in the case of VMIO operations). 1584 * 1585 * Note that this code is tricky, and has many complications to resolve 1586 * deadlock or inconsistant data situations. Tread lightly!!! 1587 * 1588 * Modify the length of a buffer's underlying buffer storage without 1589 * destroying information (unless, of course the buffer is shrinking). 1590 */ 1591int 1592allocbuf(struct buf * bp, int size) 1593{ 1594 1595 int s; 1596 int newbsize, mbsize; 1597 int i; 1598 1599#if !defined(MAX_PERF) 1600 if (!(bp->b_flags & B_BUSY)) 1601 panic("allocbuf: buffer not busy"); 1602 1603 if (bp->b_kvasize < size) 1604 panic("allocbuf: buffer too small"); 1605#endif 1606 1607 if ((bp->b_flags & B_VMIO) == 0) { 1608 caddr_t origbuf; 1609 int origbufsize; 1610 /* 1611 * Just get anonymous memory from the kernel 1612 */ 1613 mbsize = (size + DEV_BSIZE - 1) & ~(DEV_BSIZE - 1); 1614#if !defined(NO_B_MALLOC) 1615 if (bp->b_flags & B_MALLOC) 1616 newbsize = mbsize; 1617 else 1618#endif 1619 newbsize = round_page(size); 1620 1621 if (newbsize < bp->b_bufsize) { 1622#if !defined(NO_B_MALLOC) 1623 /* 1624 * malloced buffers are not shrunk 1625 */ 1626 if (bp->b_flags & B_MALLOC) { 1627 if (newbsize) { 1628 bp->b_bcount = size; 1629 } else { 1630 free(bp->b_data, M_BIOBUF); 1631 bufspace -= bp->b_bufsize; 1632 bufmallocspace -= bp->b_bufsize; 1633 bp->b_data = bp->b_kvabase; 1634 bp->b_bufsize = 0; 1635 bp->b_bcount = 0; 1636 bp->b_flags &= ~B_MALLOC; 1637 } 1638 return 1; 1639 } 1640#endif 1641 vm_hold_free_pages( 1642 bp, 1643 (vm_offset_t) bp->b_data + newbsize, 1644 (vm_offset_t) bp->b_data + bp->b_bufsize); 1645 } else if (newbsize > bp->b_bufsize) { 1646#if !defined(NO_B_MALLOC) 1647 /* 1648 * We only use malloced memory on the first allocation. 1649 * and revert to page-allocated memory when the buffer grows. 1650 */ 1651 if ( (bufmallocspace < maxbufmallocspace) && 1652 (bp->b_bufsize == 0) && 1653 (mbsize <= PAGE_SIZE/2)) { 1654 1655 bp->b_data = malloc(mbsize, M_BIOBUF, M_WAITOK); 1656 bp->b_bufsize = mbsize; 1657 bp->b_bcount = size; 1658 bp->b_flags |= B_MALLOC; 1659 bufspace += mbsize; 1660 bufmallocspace += mbsize; 1661 return 1; 1662 } 1663#endif 1664 origbuf = NULL; 1665 origbufsize = 0; 1666#if !defined(NO_B_MALLOC) 1667 /* 1668 * If the buffer is growing on its other-than-first allocation, 1669 * then we revert to the page-allocation scheme. 1670 */ 1671 if (bp->b_flags & B_MALLOC) { 1672 origbuf = bp->b_data; 1673 origbufsize = bp->b_bufsize; 1674 bp->b_data = bp->b_kvabase; 1675 bufspace -= bp->b_bufsize; 1676 bufmallocspace -= bp->b_bufsize; 1677 bp->b_bufsize = 0; 1678 bp->b_flags &= ~B_MALLOC; 1679 newbsize = round_page(newbsize); 1680 } 1681#endif 1682 vm_hold_load_pages( 1683 bp, 1684 (vm_offset_t) bp->b_data + bp->b_bufsize, 1685 (vm_offset_t) bp->b_data + newbsize); 1686#if !defined(NO_B_MALLOC) 1687 if (origbuf) { 1688 bcopy(origbuf, bp->b_data, origbufsize); 1689 free(origbuf, M_BIOBUF); 1690 } 1691#endif 1692 } 1693 } else { 1694 vm_page_t m; 1695 int desiredpages; 1696 1697 newbsize = (size + DEV_BSIZE - 1) & ~(DEV_BSIZE - 1); 1698 desiredpages = (round_page(newbsize) >> PAGE_SHIFT); 1699 1700#if !defined(NO_B_MALLOC) 1701 if (bp->b_flags & B_MALLOC) 1702 panic("allocbuf: VMIO buffer can't be malloced"); 1703#endif 1704 1705 if (newbsize < bp->b_bufsize) { 1706 if (desiredpages < bp->b_npages) { 1707 for (i = desiredpages; i < bp->b_npages; i++) { 1708 /* 1709 * the page is not freed here -- it 1710 * is the responsibility of vnode_pager_setsize 1711 */ 1712 m = bp->b_pages[i]; 1713#if defined(DIAGNOSTIC) 1714 if (m == bogus_page) 1715 panic("allocbuf: bogus page found"); 1716#endif 1717 vm_page_sleep(m, "biodep", &m->busy); 1718 1719 bp->b_pages[i] = NULL; 1720 vm_page_unwire(m); 1721 } 1722 pmap_qremove((vm_offset_t) trunc_page(bp->b_data) + 1723 (desiredpages << PAGE_SHIFT), (bp->b_npages - desiredpages)); 1724 bp->b_npages = desiredpages; 1725 } 1726 } else if (newbsize > bp->b_bufsize) { 1727 vm_object_t obj; 1728 vm_offset_t tinc, toff; 1729 vm_ooffset_t off; 1730 vm_pindex_t objoff; 1731 int pageindex, curbpnpages; 1732 struct vnode *vp; 1733 int bsize; 1734 int orig_validoff = bp->b_validoff; 1735 int orig_validend = bp->b_validend; 1736 1737 vp = bp->b_vp; 1738 1739 if (vp->v_type == VBLK) 1740 bsize = DEV_BSIZE; 1741 else 1742 bsize = vp->v_mount->mnt_stat.f_iosize; 1743 1744 if (bp->b_npages < desiredpages) { 1745 obj = vp->v_object; 1746 tinc = PAGE_SIZE; 1747 if (tinc > bsize) 1748 tinc = bsize; 1749 1750 off = bp->b_offset; 1751#ifdef DIAGNOSTIC 1752 if (bp->b_offset == NOOFFSET) 1753 panic("allocbuf: no buffer offset"); 1754#endif 1755 1756 curbpnpages = bp->b_npages; 1757 doretry: 1758 bp->b_validoff = orig_validoff; 1759 bp->b_validend = orig_validend; 1760 bp->b_flags |= B_CACHE; 1761 for (toff = 0; toff < newbsize; toff += tinc) { 1762 int bytesinpage; 1763 1764 pageindex = toff >> PAGE_SHIFT; 1765 objoff = OFF_TO_IDX(off + toff); 1766 if (pageindex < curbpnpages) { 1767 1768 m = bp->b_pages[pageindex]; 1769#ifdef VFS_BIO_DIAG 1770 if (m->pindex != objoff) 1771 panic("allocbuf: page changed offset??!!!?"); 1772#endif 1773 bytesinpage = tinc; 1774 if (tinc > (newbsize - toff)) 1775 bytesinpage = newbsize - toff; 1776 if (bp->b_flags & B_CACHE) 1777 vfs_buf_set_valid(bp, off, toff, bytesinpage, m); 1778 continue; 1779 } 1780 m = vm_page_lookup(obj, objoff); 1781 if (!m) { 1782 m = vm_page_alloc(obj, objoff, VM_ALLOC_NORMAL); 1783 if (!m) { 1784 VM_WAIT; 1785 vm_pageout_deficit += (desiredpages - bp->b_npages); 1786 goto doretry; 1787 } 1788 1789 vm_page_wire(m); 1790 m->flags &= ~PG_BUSY; 1791 bp->b_flags &= ~B_CACHE; 1792 1793 } else if (m->flags & PG_BUSY) { 1794 s = splvm(); 1795 if (m->flags & PG_BUSY) { 1796 m->flags |= PG_WANTED; 1797 tsleep(m, PVM, "pgtblk", 0); 1798 } 1799 splx(s); 1800 goto doretry; 1801 } else { 1802 if ((curproc != pageproc) && 1803 ((m->queue - m->pc) == PQ_CACHE) && 1804 ((cnt.v_free_count + cnt.v_cache_count) < 1805 (cnt.v_free_min + cnt.v_cache_min))) { 1806 pagedaemon_wakeup(); 1807 } 1808 bytesinpage = tinc; 1809 if (tinc > (newbsize - toff)) 1810 bytesinpage = newbsize - toff; 1811 if (bp->b_flags & B_CACHE) 1812 vfs_buf_set_valid(bp, off, toff, bytesinpage, m); 1813 m->flags &= ~PG_ZERO; 1814 vm_page_wire(m); 1815 } 1816 bp->b_pages[pageindex] = m; 1817 curbpnpages = pageindex + 1; 1818 } 1819 if (vp->v_tag == VT_NFS && 1820 vp->v_type != VBLK) { 1821 if (bp->b_dirtyend > 0) { 1822 bp->b_validoff = min(bp->b_validoff, bp->b_dirtyoff); 1823 bp->b_validend = max(bp->b_validend, bp->b_dirtyend); 1824 } 1825 if (bp->b_validend == 0) 1826 bp->b_flags &= ~B_CACHE; 1827 } 1828 bp->b_data = (caddr_t) trunc_page(bp->b_data); 1829 bp->b_npages = curbpnpages; 1830 pmap_qenter((vm_offset_t) bp->b_data, 1831 bp->b_pages, bp->b_npages); 1832 ((vm_offset_t) bp->b_data) |= off & PAGE_MASK; 1833 } 1834 } 1835 } 1836 if (bp->b_flags & B_VMIO) 1837 vmiospace += (newbsize - bp->b_bufsize); 1838 bufspace += (newbsize - bp->b_bufsize); 1839 bp->b_bufsize = newbsize; 1840 bp->b_bcount = size; 1841 return 1; 1842} 1843 1844/* 1845 * Wait for buffer I/O completion, returning error status. 1846 */ 1847int 1848biowait(register struct buf * bp) 1849{ 1850 int s; 1851 1852 s = splbio(); 1853 while ((bp->b_flags & B_DONE) == 0) 1854#if defined(NO_SCHEDULE_MODS) 1855 tsleep(bp, PRIBIO, "biowait", 0); 1856#else 1857 if (bp->b_flags & B_READ) 1858 tsleep(bp, PRIBIO, "biord", 0); 1859 else 1860 tsleep(bp, PRIBIO, "biowr", 0); 1861#endif 1862 splx(s); 1863 if (bp->b_flags & B_EINTR) { 1864 bp->b_flags &= ~B_EINTR; 1865 return (EINTR); 1866 } 1867 if (bp->b_flags & B_ERROR) { 1868 return (bp->b_error ? bp->b_error : EIO); 1869 } else { 1870 return (0); 1871 } 1872} 1873 1874/* 1875 * Finish I/O on a buffer, calling an optional function. 1876 * This is usually called from interrupt level, so process blocking 1877 * is not *a good idea*. 1878 */ 1879void 1880biodone(register struct buf * bp) 1881{ 1882 int s; 1883 1884 s = splbio(); 1885 1886#if !defined(MAX_PERF) 1887 if (!(bp->b_flags & B_BUSY)) 1888 panic("biodone: buffer not busy"); 1889#endif 1890 1891 if (bp->b_flags & B_DONE) { 1892 splx(s); 1893#if !defined(MAX_PERF) 1894 printf("biodone: buffer already done\n"); 1895#endif 1896 return; 1897 } 1898 bp->b_flags |= B_DONE; 1899 1900 if ((bp->b_flags & B_READ) == 0) { 1901 vwakeup(bp); 1902 } 1903 1904#ifdef BOUNCE_BUFFERS 1905 if (bp->b_flags & B_BOUNCE) { 1906 vm_bounce_free(bp); 1907 } 1908#endif 1909 1910 /* call optional completion function if requested */ 1911 if (bp->b_flags & B_CALL) { 1912 bp->b_flags &= ~B_CALL; 1913 (*bp->b_iodone) (bp); 1914 splx(s); 1915 return; 1916 } 1917 if (LIST_FIRST(&bp->b_dep) != NULL && bioops.io_complete) 1918 (*bioops.io_complete)(bp); 1919 1920 if (bp->b_flags & B_VMIO) { 1921 int i, resid; 1922 vm_ooffset_t foff; 1923 vm_page_t m; 1924 vm_object_t obj; 1925 int iosize; 1926 struct vnode *vp = bp->b_vp; 1927 1928 obj = vp->v_object; 1929 1930#if defined(VFS_BIO_DEBUG) 1931 if (vp->v_usecount == 0) { 1932 panic("biodone: zero vnode ref count"); 1933 } 1934 1935 if (vp->v_object == NULL) { 1936 panic("biodone: missing VM object"); 1937 } 1938 1939 if ((vp->v_flag & VOBJBUF) == 0) { 1940 panic("biodone: vnode is not setup for merged cache"); 1941 } 1942#endif 1943 1944 foff = bp->b_offset; 1945#ifdef DIAGNOSTIC 1946 if (bp->b_offset == NOOFFSET) 1947 panic("biodone: no buffer offset"); 1948#endif 1949 1950#if !defined(MAX_PERF) 1951 if (!obj) { 1952 panic("biodone: no object"); 1953 } 1954#endif 1955#if defined(VFS_BIO_DEBUG) 1956 if (obj->paging_in_progress < bp->b_npages) { 1957 printf("biodone: paging in progress(%d) < bp->b_npages(%d)\n", 1958 obj->paging_in_progress, bp->b_npages); 1959 } 1960#endif 1961 iosize = bp->b_bufsize; 1962 for (i = 0; i < bp->b_npages; i++) { 1963 int bogusflag = 0; 1964 m = bp->b_pages[i]; 1965 if (m == bogus_page) { 1966 bogusflag = 1; 1967 m = vm_page_lookup(obj, OFF_TO_IDX(foff)); 1968 if (!m) { 1969#if defined(VFS_BIO_DEBUG) 1970 printf("biodone: page disappeared\n"); 1971#endif 1972 --obj->paging_in_progress; 1973 continue; 1974 } 1975 bp->b_pages[i] = m; 1976 pmap_qenter(trunc_page(bp->b_data), bp->b_pages, bp->b_npages); 1977 } 1978#if defined(VFS_BIO_DEBUG) 1979 if (OFF_TO_IDX(foff) != m->pindex) { 1980 printf("biodone: foff(%d)/m->pindex(%d) mismatch\n", foff, m->pindex); 1981 } 1982#endif 1983 resid = IDX_TO_OFF(m->pindex + 1) - foff; 1984 if (resid > iosize) 1985 resid = iosize; 1986 1987 /* 1988 * In the write case, the valid and clean bits are 1989 * already changed correctly, so we only need to do this 1990 * here in the read case. 1991 */ 1992 if ((bp->b_flags & B_READ) && !bogusflag && resid > 0) { 1993 vfs_page_set_valid(bp, foff, i, m); 1994 } 1995 m->flags &= ~PG_ZERO; 1996 1997 /* 1998 * when debugging new filesystems or buffer I/O methods, this 1999 * is the most common error that pops up. if you see this, you 2000 * have not set the page busy flag correctly!!! 2001 */ 2002 if (m->busy == 0) { 2003#if !defined(MAX_PERF) 2004 printf("biodone: page busy < 0, " 2005 "pindex: %d, foff: 0x(%x,%x), " 2006 "resid: %d, index: %d\n", 2007 (int) m->pindex, (int)(foff >> 32), 2008 (int) foff & 0xffffffff, resid, i); 2009#endif 2010 if (vp->v_type != VBLK) 2011#if !defined(MAX_PERF) 2012 printf(" iosize: %ld, lblkno: %d, flags: 0x%lx, npages: %d\n", 2013 bp->b_vp->v_mount->mnt_stat.f_iosize, 2014 (int) bp->b_lblkno, 2015 bp->b_flags, bp->b_npages); 2016 else 2017 printf(" VDEV, lblkno: %d, flags: 0x%lx, npages: %d\n", 2018 (int) bp->b_lblkno, 2019 bp->b_flags, bp->b_npages); 2020 printf(" valid: 0x%x, dirty: 0x%x, wired: %d\n", 2021 m->valid, m->dirty, m->wire_count); 2022#endif 2023 panic("biodone: page busy < 0\n"); 2024 } 2025 PAGE_BWAKEUP(m); 2026 --obj->paging_in_progress; 2027 foff += resid; 2028 iosize -= resid; 2029 } 2030 if (obj && 2031 (obj->paging_in_progress == 0) && 2032 (obj->flags & OBJ_PIPWNT)) { 2033 obj->flags &= ~OBJ_PIPWNT; 2034 wakeup(obj); 2035 } 2036 } 2037 /* 2038 * For asynchronous completions, release the buffer now. The brelse 2039 * checks for B_WANTED and will do the wakeup there if necessary - so 2040 * no need to do a wakeup here in the async case. 2041 */ 2042 2043 if (bp->b_flags & B_ASYNC) { 2044 if ((bp->b_flags & (B_NOCACHE | B_INVAL | B_ERROR | B_RELBUF)) != 0) 2045 brelse(bp); 2046 else 2047 bqrelse(bp); 2048 } else { 2049 bp->b_flags &= ~B_WANTED; 2050 wakeup(bp); 2051 } 2052 splx(s); 2053} 2054 2055static int 2056count_lock_queue() 2057{ 2058 int count; 2059 struct buf *bp; 2060 2061 count = 0; 2062 for (bp = TAILQ_FIRST(&bufqueues[QUEUE_LOCKED]); 2063 bp != NULL; 2064 bp = TAILQ_NEXT(bp, b_freelist)) 2065 count++; 2066 return (count); 2067} 2068 2069#if 0 /* not with kirks code */ 2070static int vfs_update_interval = 30; 2071 2072static void 2073vfs_update() 2074{ 2075 while (1) { 2076 tsleep(&vfs_update_wakeup, PUSER, "update", 2077 hz * vfs_update_interval); 2078 vfs_update_wakeup = 0; 2079 sync(curproc, NULL); 2080 } 2081} 2082 2083static int 2084sysctl_kern_updateinterval SYSCTL_HANDLER_ARGS 2085{ 2086 int error = sysctl_handle_int(oidp, 2087 oidp->oid_arg1, oidp->oid_arg2, req); 2088 if (!error) 2089 wakeup(&vfs_update_wakeup); 2090 return error; 2091} 2092 2093SYSCTL_PROC(_kern, KERN_UPDATEINTERVAL, update, CTLTYPE_INT|CTLFLAG_RW, 2094 &vfs_update_interval, 0, sysctl_kern_updateinterval, "I", ""); 2095 2096#endif 2097 2098 2099/* 2100 * This routine is called in lieu of iodone in the case of 2101 * incomplete I/O. This keeps the busy status for pages 2102 * consistant. 2103 */ 2104void 2105vfs_unbusy_pages(struct buf * bp) 2106{ 2107 int i; 2108 2109 if (bp->b_flags & B_VMIO) { 2110 struct vnode *vp = bp->b_vp; 2111 vm_object_t obj = vp->v_object; 2112 2113 for (i = 0; i < bp->b_npages; i++) { 2114 vm_page_t m = bp->b_pages[i]; 2115 2116 if (m == bogus_page) { 2117 m = vm_page_lookup(obj, OFF_TO_IDX(bp->b_offset) + i); 2118#if !defined(MAX_PERF) 2119 if (!m) { 2120 panic("vfs_unbusy_pages: page missing\n"); 2121 } 2122#endif 2123 bp->b_pages[i] = m; 2124 pmap_qenter(trunc_page(bp->b_data), bp->b_pages, bp->b_npages); 2125 } 2126 --obj->paging_in_progress; 2127 m->flags &= ~PG_ZERO; 2128 PAGE_BWAKEUP(m); 2129 } 2130 if (obj->paging_in_progress == 0 && 2131 (obj->flags & OBJ_PIPWNT)) { 2132 obj->flags &= ~OBJ_PIPWNT; 2133 wakeup(obj); 2134 } 2135 } 2136} 2137 2138/* 2139 * Set NFS' b_validoff and b_validend fields from the valid bits 2140 * of a page. If the consumer is not NFS, and the page is not 2141 * valid for the entire range, clear the B_CACHE flag to force 2142 * the consumer to re-read the page. 2143 */ 2144static void 2145vfs_buf_set_valid(struct buf *bp, 2146 vm_ooffset_t foff, vm_offset_t off, vm_offset_t size, 2147 vm_page_t m) 2148{ 2149 if (bp->b_vp->v_tag == VT_NFS && bp->b_vp->v_type != VBLK) { 2150 vm_offset_t svalid, evalid; 2151 int validbits = m->valid; 2152 2153 /* 2154 * This only bothers with the first valid range in the 2155 * page. 2156 */ 2157 svalid = off; 2158 while (validbits && !(validbits & 1)) { 2159 svalid += DEV_BSIZE; 2160 validbits >>= 1; 2161 } 2162 evalid = svalid; 2163 while (validbits & 1) { 2164 evalid += DEV_BSIZE; 2165 validbits >>= 1; 2166 } 2167 /* 2168 * Make sure this range is contiguous with the range 2169 * built up from previous pages. If not, then we will 2170 * just use the range from the previous pages. 2171 */ 2172 if (svalid == bp->b_validend) { 2173 bp->b_validoff = min(bp->b_validoff, svalid); 2174 bp->b_validend = max(bp->b_validend, evalid); 2175 } 2176 } else if (!vm_page_is_valid(m, 2177 (vm_offset_t) ((foff + off) & PAGE_MASK), 2178 size)) { 2179 bp->b_flags &= ~B_CACHE; 2180 } 2181} 2182 2183/* 2184 * Set the valid bits in a page, taking care of the b_validoff, 2185 * b_validend fields which NFS uses to optimise small reads. Off is 2186 * the offset within the file and pageno is the page index within the buf. 2187 */ 2188static void 2189vfs_page_set_valid(struct buf *bp, vm_ooffset_t off, int pageno, vm_page_t m) 2190{ 2191 struct vnode *vp = bp->b_vp; 2192 vm_ooffset_t soff, eoff; 2193 2194 soff = off; 2195 eoff = off + min(PAGE_SIZE, bp->b_bufsize); 2196 if (vp->v_tag == VT_NFS && vp->v_type != VBLK) { 2197 vm_ooffset_t sv, ev; 2198 vm_page_set_invalid(m, 2199 (vm_offset_t) (soff & PAGE_MASK), 2200 (vm_offset_t) (eoff - soff)); 2201 off = off - pageno * PAGE_SIZE; 2202 sv = off + ((bp->b_validoff + DEV_BSIZE - 1) & ~(DEV_BSIZE - 1)); 2203 ev = off + ((bp->b_validend + DEV_BSIZE - 1) & ~(DEV_BSIZE - 1)); 2204 soff = qmax(sv, soff); 2205 eoff = qmin(ev, eoff); 2206 } 2207 if (eoff > soff) 2208 vm_page_set_validclean(m, 2209 (vm_offset_t) (soff & PAGE_MASK), 2210 (vm_offset_t) (eoff - soff)); 2211} 2212 2213/* 2214 * This routine is called before a device strategy routine. 2215 * It is used to tell the VM system that paging I/O is in 2216 * progress, and treat the pages associated with the buffer 2217 * almost as being PG_BUSY. Also the object paging_in_progress 2218 * flag is handled to make sure that the object doesn't become 2219 * inconsistant. 2220 */ 2221void 2222vfs_busy_pages(struct buf * bp, int clear_modify) 2223{ 2224 int i,s; 2225 2226 if (bp->b_flags & B_VMIO) { 2227 struct vnode *vp = bp->b_vp; 2228 vm_object_t obj = vp->v_object; 2229 vm_ooffset_t foff; 2230 2231 foff = bp->b_offset; 2232#ifdef DIAGNOSTIC 2233 if (bp->b_offset == NOOFFSET) 2234 panic("vfs_busy_pages: no buffer offset"); 2235#endif 2236 2237 vfs_setdirty(bp); 2238 2239retry: 2240 for (i = 0; i < bp->b_npages; i++) { 2241 vm_page_t m = bp->b_pages[i]; 2242 if (vm_page_sleep(m, "vbpage", NULL)) 2243 goto retry; 2244 } 2245 2246 for (i = 0; i < bp->b_npages; i++, foff += PAGE_SIZE) { 2247 vm_page_t m = bp->b_pages[i]; 2248 2249 m->flags &= ~PG_ZERO; 2250 if ((bp->b_flags & B_CLUSTER) == 0) { 2251 obj->paging_in_progress++; 2252 m->busy++; 2253 } 2254 2255 vm_page_protect(m, VM_PROT_NONE); 2256 if (clear_modify) 2257 vfs_page_set_valid(bp, foff, i, m); 2258 else if (bp->b_bcount >= PAGE_SIZE) { 2259 if (m->valid && (bp->b_flags & B_CACHE) == 0) { 2260 bp->b_pages[i] = bogus_page; 2261 pmap_qenter(trunc_page(bp->b_data), bp->b_pages, bp->b_npages); 2262 } 2263 } 2264 } 2265 } 2266} 2267 2268/* 2269 * Tell the VM system that the pages associated with this buffer 2270 * are clean. This is used for delayed writes where the data is 2271 * going to go to disk eventually without additional VM intevention. 2272 */ 2273void 2274vfs_clean_pages(struct buf * bp) 2275{ 2276 int i; 2277 2278 if (bp->b_flags & B_VMIO) { 2279 struct vnode *vp = bp->b_vp; 2280 vm_ooffset_t foff; 2281 foff = bp->b_offset; 2282 2283#ifdef DIAGNOSTIC 2284 if (bp->b_offset == NOOFFSET) 2285 panic("vfs_clean_pages: no buffer offset"); 2286#endif 2287 2288 for (i = 0; i < bp->b_npages; i++, foff += PAGE_SIZE) { 2289 vm_page_t m = bp->b_pages[i]; 2290 vfs_page_set_valid(bp, foff, i, m); 2291 } 2292 } 2293} 2294 2295void 2296vfs_bio_clrbuf(struct buf *bp) { 2297 int i; 2298 if ((bp->b_flags & (B_VMIO | B_MALLOC)) == B_VMIO) { 2299 if( (bp->b_npages == 1) && (bp->b_bufsize < PAGE_SIZE)) { 2300 int mask; 2301 mask = 0; 2302 for(i=0;i<bp->b_bufsize;i+=DEV_BSIZE) 2303 mask |= (1 << (i/DEV_BSIZE)); 2304 if(((bp->b_pages[0]->flags & PG_ZERO) == 0) && 2305 (bp->b_pages[0]->valid != mask)) { 2306 bzero(bp->b_data, bp->b_bufsize); 2307 } 2308 bp->b_pages[0]->valid = mask; 2309 bp->b_resid = 0; 2310 return; 2311 } 2312 for(i=0;i<bp->b_npages;i++) { 2313 if( bp->b_pages[i]->valid == VM_PAGE_BITS_ALL) 2314 continue; 2315 if( bp->b_pages[i]->valid == 0) { 2316 if ((bp->b_pages[i]->flags & PG_ZERO) == 0) { 2317 bzero(bp->b_data + (i << PAGE_SHIFT), PAGE_SIZE); 2318 } 2319 } else { 2320 int j; 2321 for(j=0;j<PAGE_SIZE/DEV_BSIZE;j++) { 2322 if (((bp->b_pages[i]->flags & PG_ZERO) == 0) && 2323 (bp->b_pages[i]->valid & (1<<j)) == 0) 2324 bzero(bp->b_data + (i << PAGE_SHIFT) + j * DEV_BSIZE, DEV_BSIZE); 2325 } 2326 } 2327 bp->b_pages[i]->valid = VM_PAGE_BITS_ALL; 2328 bp->b_pages[i]->flags &= ~PG_ZERO; 2329 } 2330 bp->b_resid = 0; 2331 } else { 2332 clrbuf(bp); 2333 } 2334} 2335 2336/* 2337 * vm_hold_load_pages and vm_hold_unload pages get pages into 2338 * a buffers address space. The pages are anonymous and are 2339 * not associated with a file object. 2340 */ 2341void 2342vm_hold_load_pages(struct buf * bp, vm_offset_t from, vm_offset_t to) 2343{ 2344 vm_offset_t pg; 2345 vm_page_t p; 2346 int index; 2347 2348 to = round_page(to); 2349 from = round_page(from); 2350 index = (from - trunc_page(bp->b_data)) >> PAGE_SHIFT; 2351 2352 for (pg = from; pg < to; pg += PAGE_SIZE, index++) { 2353 2354tryagain: 2355 2356 p = vm_page_alloc(kernel_object, 2357 ((pg - VM_MIN_KERNEL_ADDRESS) >> PAGE_SHIFT), 2358 VM_ALLOC_NORMAL); 2359 if (!p) { 2360 vm_pageout_deficit += (to - from) >> PAGE_SHIFT; 2361 VM_WAIT; 2362 goto tryagain; 2363 } 2364 vm_page_wire(p); 2365 p->valid = VM_PAGE_BITS_ALL; 2366 p->flags &= ~PG_ZERO; 2367 pmap_kenter(pg, VM_PAGE_TO_PHYS(p)); 2368 bp->b_pages[index] = p; 2369 PAGE_WAKEUP(p); 2370 } 2371 bp->b_npages = index; 2372} 2373 2374void 2375vm_hold_free_pages(struct buf * bp, vm_offset_t from, vm_offset_t to) 2376{ 2377 vm_offset_t pg; 2378 vm_page_t p; 2379 int index, newnpages; 2380 2381 from = round_page(from); 2382 to = round_page(to); 2383 newnpages = index = (from - trunc_page(bp->b_data)) >> PAGE_SHIFT; 2384 2385 for (pg = from; pg < to; pg += PAGE_SIZE, index++) { 2386 p = bp->b_pages[index]; 2387 if (p && (index < bp->b_npages)) { 2388#if !defined(MAX_PERF) 2389 if (p->busy) { 2390 printf("vm_hold_free_pages: blkno: %d, lblkno: %d\n", 2391 bp->b_blkno, bp->b_lblkno); 2392 } 2393#endif 2394 bp->b_pages[index] = NULL; 2395 pmap_kremove(pg); 2396 p->flags |= PG_BUSY; 2397 vm_page_unwire(p); 2398 vm_page_free(p); 2399 } 2400 } 2401 bp->b_npages = newnpages; 2402} 2403 2404 2405#include "opt_ddb.h" 2406#ifdef DDB 2407#include <ddb/ddb.h> 2408 2409DB_SHOW_COMMAND(buffer, db_show_buffer) 2410{ 2411 /* get args */ 2412 struct buf *bp = (struct buf *)addr; 2413 2414 if (!have_addr) { 2415 db_printf("usage: show buffer <addr>\n"); 2416 return; 2417 } 2418 2419 db_printf("b_proc = %p,\nb_flags = 0x%b\n", (void *)bp->b_proc, 2420 (u_int)bp->b_flags, 2421 "\20\40bounce\37cluster\36vmio\35ram\34ordered" 2422 "\33paging\32xxx\31writeinprog\30wanted\27relbuf\26tape" 2423 "\25read\24raw\23phys\22clusterok\21malloc\20nocache" 2424 "\17locked\16inval\15gathered\14error\13eintr\12done\11dirty" 2425 "\10delwri\7call\6cache\5busy\4bad\3async\2needcommit\1age"); 2426 db_printf("b_error = %d, b_bufsize = %ld, b_bcount = %ld, " 2427 "b_resid = %ld\nb_dev = 0x%x, b_data = %p, " 2428 "b_blkno = %d, b_pblkno = %d\n", 2429 bp->b_error, bp->b_bufsize, bp->b_bcount, bp->b_resid, 2430 bp->b_dev, bp->b_data, bp->b_blkno, bp->b_pblkno); 2431 if (bp->b_npages) { 2432 int i; 2433 db_printf("b_npages = %d, pages(OBJ, IDX, PA): ", bp->b_npages); 2434 for (i = 0; i < bp->b_npages; i++) { 2435 vm_page_t m; 2436 m = bp->b_pages[i]; 2437 db_printf("(%p, 0x%lx, 0x%lx)", (void *)m->object, 2438 (u_long)m->pindex, (u_long)VM_PAGE_TO_PHYS(m)); 2439 if ((i + 1) < bp->b_npages) 2440 db_printf(","); 2441 } 2442 db_printf("\n"); 2443 } 2444} 2445#endif /* DDB */ 2446