vfs_bio.c revision 19996
1/* 2 * Copyright (c) 1994 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. Redistributions in binary form must reproduce the above copyright 12 * notice, this list of conditions and the following disclaimer in the 13 * documentation and/or other materials provided with the distribution. 14 * 3. Absolutely no warranty of function or purpose is made by the author 15 * John S. Dyson. 16 * 4. This work was done expressly for inclusion into FreeBSD. Other use 17 * is allowed if this notation is included. 18 * 5. Modifications may be freely made to this file if the above conditions 19 * are met. 20 * 21 * $Id: vfs_bio.c,v 1.105 1996/11/17 02:10:48 dyson Exp $ 22 */ 23 24/* 25 * this file contains a new buffer I/O scheme implementing a coherent 26 * VM object and buffer cache scheme. Pains have been taken to make 27 * sure that the performance degradation associated with schemes such 28 * as this is not realized. 29 * 30 * Author: John S. Dyson 31 * Significant help during the development and debugging phases 32 * had been provided by David Greenman, also of the FreeBSD core team. 33 */ 34 35#include "opt_bounce.h" 36 37#define VMIO 38#include <sys/param.h> 39#include <sys/systm.h> 40#include <sys/sysproto.h> 41#include <sys/kernel.h> 42#include <sys/sysctl.h> 43#include <sys/proc.h> 44#include <sys/vnode.h> 45#include <sys/vmmeter.h> 46#include <vm/vm.h> 47#include <vm/vm_param.h> 48#include <vm/vm_prot.h> 49#include <vm/vm_kern.h> 50#include <vm/vm_pageout.h> 51#include <vm/vm_page.h> 52#include <vm/vm_object.h> 53#include <vm/vm_extern.h> 54#include <sys/buf.h> 55#include <sys/mount.h> 56#include <sys/malloc.h> 57#include <sys/resourcevar.h> 58#include <sys/proc.h> 59 60#include <miscfs/specfs/specdev.h> 61 62static void vfs_update __P((void)); 63static struct proc *updateproc; 64static struct kproc_desc up_kp = { 65 "update", 66 vfs_update, 67 &updateproc 68}; 69SYSINIT_KT(update, SI_SUB_KTHREAD_UPDATE, SI_ORDER_FIRST, kproc_start, &up_kp) 70 71struct buf *buf; /* buffer header pool */ 72struct swqueue bswlist; 73 74int count_lock_queue __P((void)); 75static void vm_hold_free_pages(struct buf * bp, vm_offset_t from, 76 vm_offset_t to); 77static void vm_hold_load_pages(struct buf * bp, vm_offset_t from, 78 vm_offset_t to); 79static void vfs_clean_pages(struct buf * bp); 80static void vfs_setdirty(struct buf *bp); 81static void vfs_vmio_release(struct buf *bp); 82 83int needsbuffer; 84 85/* 86 * Internal update daemon, process 3 87 * The variable vfs_update_wakeup allows for internal syncs. 88 */ 89int vfs_update_wakeup; 90 91 92/* 93 * buffers base kva 94 */ 95caddr_t buffers_kva; 96 97/* 98 * bogus page -- for I/O to/from partially complete buffers 99 * this is a temporary solution to the problem, but it is not 100 * really that bad. it would be better to split the buffer 101 * for input in the case of buffers partially already in memory, 102 * but the code is intricate enough already. 103 */ 104vm_page_t bogus_page; 105static vm_offset_t bogus_offset; 106 107static int bufspace, maxbufspace, vmiospace, maxvmiobufspace, 108 bufmallocspace, maxbufmallocspace; 109 110static struct bufhashhdr bufhashtbl[BUFHSZ], invalhash; 111static struct bqueues bufqueues[BUFFER_QUEUES]; 112 113extern int vm_swap_size; 114 115#define BUF_MAXUSE 16 116 117/* 118 * Initialize buffer headers and related structures. 119 */ 120void 121bufinit() 122{ 123 struct buf *bp; 124 int i; 125 126 TAILQ_INIT(&bswlist); 127 LIST_INIT(&invalhash); 128 129 /* first, make a null hash table */ 130 for (i = 0; i < BUFHSZ; i++) 131 LIST_INIT(&bufhashtbl[i]); 132 133 /* next, make a null set of free lists */ 134 for (i = 0; i < BUFFER_QUEUES; i++) 135 TAILQ_INIT(&bufqueues[i]); 136 137 buffers_kva = (caddr_t) kmem_alloc_pageable(buffer_map, MAXBSIZE * nbuf); 138 /* finally, initialize each buffer header and stick on empty q */ 139 for (i = 0; i < nbuf; i++) { 140 bp = &buf[i]; 141 bzero(bp, sizeof *bp); 142 bp->b_flags = B_INVAL; /* we're just an empty header */ 143 bp->b_dev = NODEV; 144 bp->b_rcred = NOCRED; 145 bp->b_wcred = NOCRED; 146 bp->b_qindex = QUEUE_EMPTY; 147 bp->b_vnbufs.le_next = NOLIST; 148 bp->b_data = buffers_kva + i * MAXBSIZE; 149 TAILQ_INSERT_TAIL(&bufqueues[QUEUE_EMPTY], bp, b_freelist); 150 LIST_INSERT_HEAD(&invalhash, bp, b_hash); 151 } 152/* 153 * maxbufspace is currently calculated to support all filesystem blocks 154 * to be 8K. If you happen to use a 16K filesystem, the size of the buffer 155 * cache is still the same as it would be for 8K filesystems. This 156 * keeps the size of the buffer cache "in check" for big block filesystems. 157 */ 158 maxbufspace = (nbuf + 8) * PAGE_SIZE; 159/* 160 * reserve 1/3 of the buffers for metadata (VDIR) which might not be VMIO'ed 161 */ 162 maxvmiobufspace = 2 * maxbufspace / 3; 163/* 164 * Limit the amount of malloc memory since it is wired permanently into 165 * the kernel space. Even though this is accounted for in the buffer 166 * allocation, we don't want the malloced region to grow uncontrolled. 167 * The malloc scheme improves memory utilization significantly on average 168 * (small) directories. 169 */ 170 maxbufmallocspace = maxbufspace / 20; 171 172 bogus_offset = kmem_alloc_pageable(kernel_map, PAGE_SIZE); 173 bogus_page = vm_page_alloc(kernel_object, 174 ((bogus_offset - VM_MIN_KERNEL_ADDRESS) >> PAGE_SHIFT), 175 VM_ALLOC_NORMAL); 176 177} 178 179/* 180 * remove the buffer from the appropriate free list 181 */ 182void 183bremfree(struct buf * bp) 184{ 185 int s = splbio(); 186 187 if (bp->b_qindex != QUEUE_NONE) { 188 TAILQ_REMOVE(&bufqueues[bp->b_qindex], bp, b_freelist); 189 bp->b_qindex = QUEUE_NONE; 190 } else { 191 panic("bremfree: removing a buffer when not on a queue"); 192 } 193 splx(s); 194} 195 196/* 197 * Get a buffer with the specified data. Look in the cache first. 198 */ 199int 200bread(struct vnode * vp, daddr_t blkno, int size, struct ucred * cred, 201 struct buf ** bpp) 202{ 203 struct buf *bp; 204 205 bp = getblk(vp, blkno, size, 0, 0); 206 *bpp = bp; 207 208 /* if not found in cache, do some I/O */ 209 if ((bp->b_flags & B_CACHE) == 0) { 210 if (curproc != NULL) 211 curproc->p_stats->p_ru.ru_inblock++; 212 bp->b_flags |= B_READ; 213 bp->b_flags &= ~(B_DONE | B_ERROR | B_INVAL); 214 if (bp->b_rcred == NOCRED) { 215 if (cred != NOCRED) 216 crhold(cred); 217 bp->b_rcred = cred; 218 } 219 vfs_busy_pages(bp, 0); 220 VOP_STRATEGY(bp); 221 return (biowait(bp)); 222 } 223 return (0); 224} 225 226/* 227 * Operates like bread, but also starts asynchronous I/O on 228 * read-ahead blocks. 229 */ 230int 231breadn(struct vnode * vp, daddr_t blkno, int size, 232 daddr_t * rablkno, int *rabsize, 233 int cnt, struct ucred * cred, struct buf ** bpp) 234{ 235 struct buf *bp, *rabp; 236 int i; 237 int rv = 0, readwait = 0; 238 239 *bpp = bp = getblk(vp, blkno, size, 0, 0); 240 241 /* if not found in cache, do some I/O */ 242 if ((bp->b_flags & B_CACHE) == 0) { 243 if (curproc != NULL) 244 curproc->p_stats->p_ru.ru_inblock++; 245 bp->b_flags |= B_READ; 246 bp->b_flags &= ~(B_DONE | B_ERROR | B_INVAL); 247 if (bp->b_rcred == NOCRED) { 248 if (cred != NOCRED) 249 crhold(cred); 250 bp->b_rcred = cred; 251 } 252 vfs_busy_pages(bp, 0); 253 VOP_STRATEGY(bp); 254 ++readwait; 255 } 256 for (i = 0; i < cnt; i++, rablkno++, rabsize++) { 257 if (inmem(vp, *rablkno)) 258 continue; 259 rabp = getblk(vp, *rablkno, *rabsize, 0, 0); 260 261 if ((rabp->b_flags & B_CACHE) == 0) { 262 if (curproc != NULL) 263 curproc->p_stats->p_ru.ru_inblock++; 264 rabp->b_flags |= B_READ | B_ASYNC; 265 rabp->b_flags &= ~(B_DONE | B_ERROR | B_INVAL); 266 if (rabp->b_rcred == NOCRED) { 267 if (cred != NOCRED) 268 crhold(cred); 269 rabp->b_rcred = cred; 270 } 271 vfs_busy_pages(rabp, 0); 272 VOP_STRATEGY(rabp); 273 } else { 274 brelse(rabp); 275 } 276 } 277 278 if (readwait) { 279 rv = biowait(bp); 280 } 281 return (rv); 282} 283 284/* 285 * Write, release buffer on completion. (Done by iodone 286 * if async.) 287 */ 288int 289bwrite(struct buf * bp) 290{ 291 int oldflags = bp->b_flags; 292 293 if (bp->b_flags & B_INVAL) { 294 brelse(bp); 295 return (0); 296 } 297 if (!(bp->b_flags & B_BUSY)) 298 panic("bwrite: buffer is not busy???"); 299 300 bp->b_flags &= ~(B_READ | B_DONE | B_ERROR | B_DELWRI); 301 bp->b_flags |= B_WRITEINPROG; 302 303 if ((oldflags & (B_ASYNC|B_DELWRI)) == (B_ASYNC|B_DELWRI)) { 304 reassignbuf(bp, bp->b_vp); 305 } 306 307 bp->b_vp->v_numoutput++; 308 vfs_busy_pages(bp, 1); 309 if (curproc != NULL) 310 curproc->p_stats->p_ru.ru_oublock++; 311 VOP_STRATEGY(bp); 312 313 /* 314 * Handle ordered writes here. 315 * If the write was originally flagged as ordered, 316 * then we check to see if it was converted to async. 317 * If it was converted to async, and is done now, then 318 * we release the buffer. Otherwise we clear the 319 * ordered flag because it is not needed anymore. 320 * 321 * Note that biodone has been modified so that it does 322 * not release ordered buffers. This allows us to have 323 * a chance to determine whether or not the driver 324 * has set the async flag in the strategy routine. Otherwise 325 * if biodone was not modified, then the buffer may have been 326 * reused before we have had a chance to check the flag. 327 */ 328 329 if ((oldflags & B_ORDERED) == B_ORDERED) { 330 int s; 331 s = splbio(); 332 if (bp->b_flags & B_ASYNC) { 333 if ((bp->b_flags & B_DONE)) { 334 if ((bp->b_flags & (B_NOCACHE | B_INVAL | B_ERROR | B_RELBUF)) != 0) 335 brelse(bp); 336 else 337 bqrelse(bp); 338 } 339 splx(s); 340 return (0); 341 } else { 342 bp->b_flags &= ~B_ORDERED; 343 } 344 splx(s); 345 } 346 347 if ((oldflags & B_ASYNC) == 0) { 348 int rtval = biowait(bp); 349 350 if (oldflags & B_DELWRI) { 351 reassignbuf(bp, bp->b_vp); 352 } 353 brelse(bp); 354 return (rtval); 355 } 356 return (0); 357} 358 359int 360vn_bwrite(ap) 361 struct vop_bwrite_args *ap; 362{ 363 return (bwrite(ap->a_bp)); 364} 365 366/* 367 * Delayed write. (Buffer is marked dirty). 368 */ 369void 370bdwrite(struct buf * bp) 371{ 372 373 if ((bp->b_flags & B_BUSY) == 0) { 374 panic("bdwrite: buffer is not busy"); 375 } 376 if (bp->b_flags & B_INVAL) { 377 brelse(bp); 378 return; 379 } 380 if (bp->b_flags & B_TAPE) { 381 bawrite(bp); 382 return; 383 } 384 bp->b_flags &= ~(B_READ|B_RELBUF); 385 if ((bp->b_flags & B_DELWRI) == 0) { 386 bp->b_flags |= B_DONE | B_DELWRI; 387 reassignbuf(bp, bp->b_vp); 388 } 389 390 /* 391 * This bmap keeps the system from needing to do the bmap later, 392 * perhaps when the system is attempting to do a sync. Since it 393 * is likely that the indirect block -- or whatever other datastructure 394 * that the filesystem needs is still in memory now, it is a good 395 * thing to do this. Note also, that if the pageout daemon is 396 * requesting a sync -- there might not be enough memory to do 397 * the bmap then... So, this is important to do. 398 */ 399 if( bp->b_lblkno == bp->b_blkno) { 400 VOP_BMAP(bp->b_vp, bp->b_lblkno, NULL, &bp->b_blkno, NULL, NULL); 401 } 402 403 /* 404 * Set the *dirty* buffer range based upon the VM system dirty pages. 405 */ 406 vfs_setdirty(bp); 407 408 /* 409 * We need to do this here to satisfy the vnode_pager and the 410 * pageout daemon, so that it thinks that the pages have been 411 * "cleaned". Note that since the pages are in a delayed write 412 * buffer -- the VFS layer "will" see that the pages get written 413 * out on the next sync, or perhaps the cluster will be completed. 414 */ 415 vfs_clean_pages(bp); 416 bqrelse(bp); 417 return; 418} 419 420/* 421 * Asynchronous write. 422 * Start output on a buffer, but do not wait for it to complete. 423 * The buffer is released when the output completes. 424 */ 425void 426bawrite(struct buf * bp) 427{ 428 bp->b_flags |= B_ASYNC; 429 (void) VOP_BWRITE(bp); 430} 431 432/* 433 * Ordered write. 434 * Start output on a buffer, but only wait for it to complete if the 435 * output device cannot guarantee ordering in some other way. Devices 436 * that can perform asynchronous ordered writes will set the B_ASYNC 437 * flag in their strategy routine. 438 * The buffer is released when the output completes. 439 */ 440int 441bowrite(struct buf * bp) 442{ 443 bp->b_flags |= B_ORDERED; 444 return (VOP_BWRITE(bp)); 445} 446 447/* 448 * Release a buffer. 449 */ 450void 451brelse(struct buf * bp) 452{ 453 int s; 454 455 if (bp->b_flags & B_CLUSTER) { 456 relpbuf(bp); 457 return; 458 } 459 /* anyone need a "free" block? */ 460 s = splbio(); 461 462 /* anyone need this block? */ 463 if (bp->b_flags & B_WANTED) { 464 bp->b_flags &= ~(B_WANTED | B_AGE); 465 wakeup(bp); 466 } 467 468 if (bp->b_flags & B_LOCKED) 469 bp->b_flags &= ~B_ERROR; 470 471 if ((bp->b_flags & (B_NOCACHE | B_INVAL | B_ERROR)) || 472 (bp->b_bufsize <= 0)) { 473 bp->b_flags |= B_INVAL; 474 bp->b_flags &= ~(B_DELWRI | B_CACHE); 475 if (((bp->b_flags & B_VMIO) == 0) && bp->b_vp) { 476 if (bp->b_bufsize) 477 allocbuf(bp, 0); 478 brelvp(bp); 479 } 480 } 481 482 /* 483 * VMIO buffer rundown. It is not very necessary to keep a VMIO buffer 484 * constituted, so the B_INVAL flag is used to *invalidate* the buffer, 485 * but the VM object is kept around. The B_NOCACHE flag is used to 486 * invalidate the pages in the VM object. 487 */ 488 if (bp->b_flags & B_VMIO) { 489 vm_ooffset_t foff; 490 vm_object_t obj; 491 int i, resid; 492 vm_page_t m; 493 struct vnode *vp; 494 int iototal = bp->b_bufsize; 495 496 vp = bp->b_vp; 497 if (!vp) 498 panic("brelse: missing vp"); 499 500 if (bp->b_npages) { 501 vm_pindex_t poff; 502 obj = (vm_object_t) vp->v_object; 503 if (vp->v_type == VBLK) 504 foff = ((vm_ooffset_t) bp->b_lblkno) << DEV_BSHIFT; 505 else 506 foff = (vm_ooffset_t) vp->v_mount->mnt_stat.f_iosize * bp->b_lblkno; 507 poff = OFF_TO_IDX(foff); 508 for (i = 0; i < bp->b_npages; i++) { 509 m = bp->b_pages[i]; 510 if (m == bogus_page) { 511 m = vm_page_lookup(obj, poff + i); 512 if (!m) { 513 panic("brelse: page missing\n"); 514 } 515 bp->b_pages[i] = m; 516 pmap_qenter(trunc_page(bp->b_data), 517 bp->b_pages, bp->b_npages); 518 } 519 resid = IDX_TO_OFF(m->pindex+1) - foff; 520 if (resid > iototal) 521 resid = iototal; 522 if (resid > 0) { 523 /* 524 * Don't invalidate the page if the local machine has already 525 * modified it. This is the lesser of two evils, and should 526 * be fixed. 527 */ 528 if (bp->b_flags & (B_NOCACHE | B_ERROR)) { 529 vm_page_test_dirty(m); 530 if (m->dirty == 0) { 531 vm_page_set_invalid(m, (vm_offset_t) foff, resid); 532 if (m->valid == 0) 533 vm_page_protect(m, VM_PROT_NONE); 534 } 535 } 536 if (resid >= PAGE_SIZE) { 537 if ((m->valid & VM_PAGE_BITS_ALL) != VM_PAGE_BITS_ALL) { 538 bp->b_flags |= B_INVAL; 539 } 540 } else { 541 if (!vm_page_is_valid(m, 542 (((vm_offset_t) bp->b_data) & PAGE_MASK), resid)) { 543 bp->b_flags |= B_INVAL; 544 } 545 } 546 } 547 foff += resid; 548 iototal -= resid; 549 } 550 } 551 if (bp->b_flags & (B_INVAL | B_RELBUF)) 552 vfs_vmio_release(bp); 553 } 554 if (bp->b_qindex != QUEUE_NONE) 555 panic("brelse: free buffer onto another queue???"); 556 557 /* enqueue */ 558 /* buffers with no memory */ 559 if (bp->b_bufsize == 0) { 560 bp->b_qindex = QUEUE_EMPTY; 561 TAILQ_INSERT_HEAD(&bufqueues[QUEUE_EMPTY], bp, b_freelist); 562 LIST_REMOVE(bp, b_hash); 563 LIST_INSERT_HEAD(&invalhash, bp, b_hash); 564 bp->b_dev = NODEV; 565 if (needsbuffer) { 566 wakeup(&needsbuffer); 567 needsbuffer=0; 568 } 569 /* buffers with junk contents */ 570 } else if (bp->b_flags & (B_ERROR | B_INVAL | B_NOCACHE | B_RELBUF)) { 571 bp->b_qindex = QUEUE_AGE; 572 TAILQ_INSERT_HEAD(&bufqueues[QUEUE_AGE], bp, b_freelist); 573 LIST_REMOVE(bp, b_hash); 574 LIST_INSERT_HEAD(&invalhash, bp, b_hash); 575 bp->b_dev = NODEV; 576 if (needsbuffer) { 577 wakeup(&needsbuffer); 578 needsbuffer=0; 579 } 580 /* buffers that are locked */ 581 } else if (bp->b_flags & B_LOCKED) { 582 bp->b_qindex = QUEUE_LOCKED; 583 TAILQ_INSERT_TAIL(&bufqueues[QUEUE_LOCKED], bp, b_freelist); 584 /* buffers with stale but valid contents */ 585 } else if (bp->b_flags & B_AGE) { 586 bp->b_qindex = QUEUE_AGE; 587 TAILQ_INSERT_TAIL(&bufqueues[QUEUE_AGE], bp, b_freelist); 588 if (needsbuffer) { 589 wakeup(&needsbuffer); 590 needsbuffer=0; 591 } 592 /* buffers with valid and quite potentially reuseable contents */ 593 } else { 594 bp->b_qindex = QUEUE_LRU; 595 TAILQ_INSERT_TAIL(&bufqueues[QUEUE_LRU], bp, b_freelist); 596 if (needsbuffer) { 597 wakeup(&needsbuffer); 598 needsbuffer=0; 599 } 600 } 601 602 /* unlock */ 603 bp->b_flags &= ~(B_ORDERED | B_WANTED | B_BUSY | 604 B_ASYNC | B_NOCACHE | B_AGE | B_RELBUF); 605 splx(s); 606} 607 608/* 609 * Release a buffer. 610 */ 611void 612bqrelse(struct buf * bp) 613{ 614 int s; 615 616 s = splbio(); 617 618 619 /* anyone need this block? */ 620 if (bp->b_flags & B_WANTED) { 621 bp->b_flags &= ~(B_WANTED | B_AGE); 622 wakeup(bp); 623 } 624 625 if (bp->b_qindex != QUEUE_NONE) 626 panic("bqrelse: free buffer onto another queue???"); 627 628 if (bp->b_flags & B_LOCKED) { 629 bp->b_flags &= ~B_ERROR; 630 bp->b_qindex = QUEUE_LOCKED; 631 TAILQ_INSERT_TAIL(&bufqueues[QUEUE_LOCKED], bp, b_freelist); 632 /* buffers with stale but valid contents */ 633 } else { 634 bp->b_qindex = QUEUE_LRU; 635 TAILQ_INSERT_TAIL(&bufqueues[QUEUE_LRU], bp, b_freelist); 636 if (needsbuffer) { 637 wakeup(&needsbuffer); 638 needsbuffer=0; 639 } 640 } 641 642 /* unlock */ 643 bp->b_flags &= ~(B_ORDERED | B_WANTED | B_BUSY | 644 B_ASYNC | B_NOCACHE | B_AGE | B_RELBUF); 645 splx(s); 646} 647 648static void 649vfs_vmio_release(bp) 650 struct buf *bp; 651{ 652 int i; 653 vm_page_t m; 654 655 for (i = 0; i < bp->b_npages; i++) { 656 m = bp->b_pages[i]; 657 bp->b_pages[i] = NULL; 658 vm_page_unwire(m); 659 /* 660 * We don't mess with busy pages, it is 661 * the responsibility of the process that 662 * busied the pages to deal with them. 663 */ 664 if ((m->flags & PG_BUSY) || (m->busy != 0)) 665 continue; 666 667 if (m->wire_count == 0) { 668 669 if (m->flags & PG_WANTED) { 670 m->flags &= ~PG_WANTED; 671 wakeup(m); 672 } 673 674 /* 675 * If this is an async free -- we cannot place 676 * pages onto the cache queue, so our policy for 677 * such buffers is to avoid the cache queue, and 678 * only modify the active queue or free queue. 679 */ 680 if ((bp->b_flags & B_ASYNC) == 0) { 681 682 /* 683 * In the case of sync buffer frees, we can do pretty much 684 * anything to any of the memory queues. Specifically, 685 * the cache queue is free to be modified. 686 */ 687 if (m->valid) { 688 if(m->dirty == 0) 689 vm_page_test_dirty(m); 690 /* 691 * this keeps pressure off of the process memory 692 */ 693 if ((vm_swap_size == 0) || 694 (cnt.v_free_count < cnt.v_free_min)) { 695 if ((m->dirty == 0) && 696 (m->hold_count == 0)) 697 vm_page_cache(m); 698 else 699 vm_page_deactivate(m); 700 } 701 } else if (m->hold_count == 0) { 702 vm_page_protect(m, VM_PROT_NONE); 703 vm_page_free(m); 704 } 705 } else { 706 /* 707 * If async, then at least we clear the 708 * act_count. 709 */ 710 m->act_count = 0; 711 } 712 } 713 } 714 bufspace -= bp->b_bufsize; 715 vmiospace -= bp->b_bufsize; 716 pmap_qremove(trunc_page((vm_offset_t) bp->b_data), bp->b_npages); 717 bp->b_npages = 0; 718 bp->b_bufsize = 0; 719 bp->b_flags &= ~B_VMIO; 720 if (bp->b_vp) 721 brelvp(bp); 722} 723 724/* 725 * Check to see if a block is currently memory resident. 726 */ 727__inline struct buf * 728gbincore(struct vnode * vp, daddr_t blkno) 729{ 730 struct buf *bp; 731 struct bufhashhdr *bh; 732 733 bh = BUFHASH(vp, blkno); 734 bp = bh->lh_first; 735 736 /* Search hash chain */ 737 while (bp != NULL) { 738 /* hit */ 739 if (bp->b_vp == vp && bp->b_lblkno == blkno && 740 (bp->b_flags & B_INVAL) == 0) { 741 break; 742 } 743 bp = bp->b_hash.le_next; 744 } 745 return (bp); 746} 747 748/* 749 * this routine implements clustered async writes for 750 * clearing out B_DELWRI buffers... This is much better 751 * than the old way of writing only one buffer at a time. 752 */ 753int 754vfs_bio_awrite(struct buf * bp) 755{ 756 int i; 757 daddr_t lblkno = bp->b_lblkno; 758 struct vnode *vp = bp->b_vp; 759 int s; 760 int ncl; 761 struct buf *bpa; 762 int nwritten; 763 764 s = splbio(); 765 /* 766 * right now we support clustered writing only to regular files 767 */ 768 if ((vp->v_type == VREG) && 769 (vp->v_mount != 0) && /* Only on nodes that have the size info */ 770 (bp->b_flags & (B_CLUSTEROK | B_INVAL)) == B_CLUSTEROK) { 771 int size; 772 int maxcl; 773 774 size = vp->v_mount->mnt_stat.f_iosize; 775 maxcl = MAXPHYS / size; 776 777 for (i = 1; i < maxcl; i++) { 778 if ((bpa = gbincore(vp, lblkno + i)) && 779 ((bpa->b_flags & (B_BUSY | B_DELWRI | B_CLUSTEROK | B_INVAL)) == 780 (B_DELWRI | B_CLUSTEROK)) && 781 (bpa->b_bufsize == size)) { 782 if ((bpa->b_blkno == bpa->b_lblkno) || 783 (bpa->b_blkno != bp->b_blkno + ((i * size) >> DEV_BSHIFT))) 784 break; 785 } else { 786 break; 787 } 788 } 789 ncl = i; 790 /* 791 * this is a possible cluster write 792 */ 793 if (ncl != 1) { 794 nwritten = cluster_wbuild(vp, size, lblkno, ncl); 795 splx(s); 796 return nwritten; 797 } 798 } 799 bremfree(bp); 800 splx(s); 801 /* 802 * default (old) behavior, writing out only one block 803 */ 804 bp->b_flags |= B_BUSY | B_ASYNC; 805 nwritten = bp->b_bufsize; 806 (void) VOP_BWRITE(bp); 807 return nwritten; 808} 809 810 811/* 812 * Find a buffer header which is available for use. 813 */ 814static struct buf * 815getnewbuf(int slpflag, int slptimeo, int doingvmio) 816{ 817 struct buf *bp; 818 int nbyteswritten = 0; 819 820start: 821 if (bufspace >= maxbufspace) 822 goto trytofreespace; 823 824 /* can we constitute a new buffer? */ 825 if ((bp = TAILQ_FIRST(&bufqueues[QUEUE_EMPTY]))) { 826 if (bp->b_qindex != QUEUE_EMPTY) 827 panic("getnewbuf: inconsistent EMPTY queue, qindex=%d", 828 bp->b_qindex); 829 bp->b_flags |= B_BUSY; 830 bremfree(bp); 831 goto fillbuf; 832 } 833trytofreespace: 834 /* 835 * We keep the file I/O from hogging metadata I/O 836 * This is desirable because file data is cached in the 837 * VM/Buffer cache even if a buffer is freed. 838 */ 839 if ((bp = TAILQ_FIRST(&bufqueues[QUEUE_AGE]))) { 840 if (bp->b_qindex != QUEUE_AGE) 841 panic("getnewbuf: inconsistent AGE queue, qindex=%d", 842 bp->b_qindex); 843 } else if ((bp = TAILQ_FIRST(&bufqueues[QUEUE_LRU]))) { 844 if (bp->b_qindex != QUEUE_LRU) 845 panic("getnewbuf: inconsistent LRU queue, qindex=%d", 846 bp->b_qindex); 847 } 848 if (!bp) { 849 /* wait for a free buffer of any kind */ 850 needsbuffer = 1; 851 tsleep(&needsbuffer, 852 (PRIBIO + 1) | slpflag, "newbuf", slptimeo); 853 return (0); 854 } 855 856#if defined(DIAGNOSTIC) 857 if (bp->b_flags & B_BUSY) { 858 panic("getnewbuf: busy buffer on free list\n"); 859 } 860#endif 861 862 /* 863 * We are fairly aggressive about freeing VMIO buffers, but since 864 * the buffering is intact without buffer headers, there is not 865 * much loss. We gain by maintaining non-VMIOed metadata in buffers. 866 */ 867 if ((bp->b_qindex == QUEUE_LRU) && (bp->b_usecount > 0)) { 868 if ((bp->b_flags & B_VMIO) == 0 || 869 (vmiospace < maxvmiobufspace)) { 870 --bp->b_usecount; 871 TAILQ_REMOVE(&bufqueues[QUEUE_LRU], bp, b_freelist); 872 if (TAILQ_FIRST(&bufqueues[QUEUE_LRU]) != NULL) { 873 TAILQ_INSERT_TAIL(&bufqueues[QUEUE_LRU], bp, b_freelist); 874 goto start; 875 } 876 TAILQ_INSERT_TAIL(&bufqueues[QUEUE_LRU], bp, b_freelist); 877 } 878 } 879 880 /* if we are a delayed write, convert to an async write */ 881 if ((bp->b_flags & (B_DELWRI | B_INVAL)) == B_DELWRI) { 882 nbyteswritten += vfs_bio_awrite(bp); 883 if (!slpflag && !slptimeo) { 884 return (0); 885 } 886 goto start; 887 } 888 889 if (bp->b_flags & B_WANTED) { 890 bp->b_flags &= ~B_WANTED; 891 wakeup(bp); 892 } 893 bremfree(bp); 894 bp->b_flags |= B_BUSY; 895 896 if (bp->b_flags & B_VMIO) { 897 bp->b_flags &= ~B_ASYNC; 898 vfs_vmio_release(bp); 899 } 900 901 if (bp->b_vp) 902 brelvp(bp); 903 904fillbuf: 905 /* we are not free, nor do we contain interesting data */ 906 if (bp->b_rcred != NOCRED) { 907 crfree(bp->b_rcred); 908 bp->b_rcred = NOCRED; 909 } 910 if (bp->b_wcred != NOCRED) { 911 crfree(bp->b_wcred); 912 bp->b_wcred = NOCRED; 913 } 914 915 LIST_REMOVE(bp, b_hash); 916 LIST_INSERT_HEAD(&invalhash, bp, b_hash); 917 if (bp->b_bufsize) { 918 allocbuf(bp, 0); 919 } 920 bp->b_flags = B_BUSY; 921 bp->b_dev = NODEV; 922 bp->b_vp = NULL; 923 bp->b_blkno = bp->b_lblkno = 0; 924 bp->b_iodone = 0; 925 bp->b_error = 0; 926 bp->b_resid = 0; 927 bp->b_bcount = 0; 928 bp->b_npages = 0; 929 bp->b_data = buffers_kva + (bp - buf) * MAXBSIZE; 930 bp->b_dirtyoff = bp->b_dirtyend = 0; 931 bp->b_validoff = bp->b_validend = 0; 932 bp->b_usecount = 4; 933 if (bufspace >= maxbufspace + nbyteswritten) { 934 bp->b_flags |= B_INVAL; 935 brelse(bp); 936 goto trytofreespace; 937 } 938 return (bp); 939} 940 941/* 942 * Check to see if a block is currently memory resident. 943 */ 944struct buf * 945incore(struct vnode * vp, daddr_t blkno) 946{ 947 struct buf *bp; 948 949 int s = splbio(); 950 bp = gbincore(vp, blkno); 951 splx(s); 952 return (bp); 953} 954 955/* 956 * Returns true if no I/O is needed to access the 957 * associated VM object. This is like incore except 958 * it also hunts around in the VM system for the data. 959 */ 960 961int 962inmem(struct vnode * vp, daddr_t blkno) 963{ 964 vm_object_t obj; 965 vm_offset_t toff, tinc; 966 vm_page_t m; 967 vm_ooffset_t off; 968 969 if (incore(vp, blkno)) 970 return 1; 971 if (vp->v_mount == NULL) 972 return 0; 973 if ((vp->v_object == NULL) || (vp->v_flag & VVMIO) == 0) 974 return 0; 975 976 obj = vp->v_object; 977 tinc = PAGE_SIZE; 978 if (tinc > vp->v_mount->mnt_stat.f_iosize) 979 tinc = vp->v_mount->mnt_stat.f_iosize; 980 off = blkno * vp->v_mount->mnt_stat.f_iosize; 981 982 for (toff = 0; toff < vp->v_mount->mnt_stat.f_iosize; toff += tinc) { 983 984 m = vm_page_lookup(obj, OFF_TO_IDX(off + toff)); 985 if (!m) 986 return 0; 987 if (vm_page_is_valid(m, (vm_offset_t) (toff + off), tinc) == 0) 988 return 0; 989 } 990 return 1; 991} 992 993/* 994 * now we set the dirty range for the buffer -- 995 * for NFS -- if the file is mapped and pages have 996 * been written to, let it know. We want the 997 * entire range of the buffer to be marked dirty if 998 * any of the pages have been written to for consistancy 999 * with the b_validoff, b_validend set in the nfs write 1000 * code, and used by the nfs read code. 1001 */ 1002static void 1003vfs_setdirty(struct buf *bp) { 1004 int i; 1005 vm_object_t object; 1006 vm_offset_t boffset, offset; 1007 /* 1008 * We qualify the scan for modified pages on whether the 1009 * object has been flushed yet. The OBJ_WRITEABLE flag 1010 * is not cleared simply by protecting pages off. 1011 */ 1012 if ((bp->b_flags & B_VMIO) && 1013 ((object = bp->b_pages[0]->object)->flags & (OBJ_WRITEABLE|OBJ_CLEANING))) { 1014 /* 1015 * test the pages to see if they have been modified directly 1016 * by users through the VM system. 1017 */ 1018 for (i = 0; i < bp->b_npages; i++) 1019 vm_page_test_dirty(bp->b_pages[i]); 1020 1021 /* 1022 * scan forwards for the first page modified 1023 */ 1024 for (i = 0; i < bp->b_npages; i++) { 1025 if (bp->b_pages[i]->dirty) { 1026 break; 1027 } 1028 } 1029 boffset = (i << PAGE_SHIFT); 1030 if (boffset < bp->b_dirtyoff) { 1031 bp->b_dirtyoff = boffset; 1032 } 1033 1034 /* 1035 * scan backwards for the last page modified 1036 */ 1037 for (i = bp->b_npages - 1; i >= 0; --i) { 1038 if (bp->b_pages[i]->dirty) { 1039 break; 1040 } 1041 } 1042 boffset = (i + 1); 1043 offset = boffset + bp->b_pages[0]->pindex; 1044 if (offset >= object->size) 1045 boffset = object->size - bp->b_pages[0]->pindex; 1046 if (bp->b_dirtyend < (boffset << PAGE_SHIFT)) 1047 bp->b_dirtyend = (boffset << PAGE_SHIFT); 1048 } 1049} 1050 1051/* 1052 * Get a block given a specified block and offset into a file/device. 1053 */ 1054struct buf * 1055getblk(struct vnode * vp, daddr_t blkno, int size, int slpflag, int slptimeo) 1056{ 1057 struct buf *bp; 1058 int s; 1059 struct bufhashhdr *bh; 1060 1061 if (size > MAXBSIZE) 1062 panic("getblk: size(%d) > MAXBSIZE(%d)\n", size, MAXBSIZE); 1063 1064 s = splbio(); 1065loop: 1066 if ((bp = gbincore(vp, blkno))) { 1067 if (bp->b_flags & B_BUSY) { 1068 bp->b_flags |= B_WANTED; 1069 if (bp->b_usecount < BUF_MAXUSE) 1070 ++bp->b_usecount; 1071 if (!tsleep(bp, 1072 (PRIBIO + 1) | slpflag, "getblk", slptimeo)) 1073 goto loop; 1074 1075 splx(s); 1076 return (struct buf *) NULL; 1077 } 1078 bp->b_flags |= B_BUSY | B_CACHE; 1079 bremfree(bp); 1080 1081 /* 1082 * check for size inconsistancies (note that they shouldn't happen 1083 * but do when filesystems don't handle the size changes correctly.) 1084 * We are conservative on metadata and don't just extend the buffer 1085 * but write and re-constitute it. 1086 */ 1087 1088 if (bp->b_bcount != size) { 1089 if (bp->b_flags & B_VMIO) { 1090 allocbuf(bp, size); 1091 } else { 1092 bp->b_flags |= B_NOCACHE; 1093 VOP_BWRITE(bp); 1094 goto loop; 1095 } 1096 } 1097 1098 if (bp->b_usecount < BUF_MAXUSE) 1099 ++bp->b_usecount; 1100 splx(s); 1101 return (bp); 1102 } else { 1103 vm_object_t obj; 1104 int doingvmio; 1105 1106 if ((obj = vp->v_object) && (vp->v_flag & VVMIO)) { 1107 doingvmio = 1; 1108 } else { 1109 doingvmio = 0; 1110 } 1111 if ((bp = getnewbuf(slpflag, slptimeo, doingvmio)) == 0) { 1112 if (slpflag || slptimeo) { 1113 splx(s); 1114 return NULL; 1115 } 1116 goto loop; 1117 } 1118 1119 /* 1120 * This code is used to make sure that a buffer is not 1121 * created while the getnewbuf routine is blocked. 1122 * Normally the vnode is locked so this isn't a problem. 1123 * VBLK type I/O requests, however, don't lock the vnode. 1124 */ 1125 if (!VOP_ISLOCKED(vp) && gbincore(vp, blkno)) { 1126 bp->b_flags |= B_INVAL; 1127 brelse(bp); 1128 goto loop; 1129 } 1130 1131 /* 1132 * Insert the buffer into the hash, so that it can 1133 * be found by incore. 1134 */ 1135 bp->b_blkno = bp->b_lblkno = blkno; 1136 bgetvp(vp, bp); 1137 LIST_REMOVE(bp, b_hash); 1138 bh = BUFHASH(vp, blkno); 1139 LIST_INSERT_HEAD(bh, bp, b_hash); 1140 1141 if (doingvmio) { 1142 bp->b_flags |= (B_VMIO | B_CACHE); 1143#if defined(VFS_BIO_DEBUG) 1144 if (vp->v_type != VREG && vp->v_type != VBLK) 1145 printf("getblk: vmioing file type %d???\n", vp->v_type); 1146#endif 1147 } else { 1148 bp->b_flags &= ~B_VMIO; 1149 } 1150 splx(s); 1151 1152 allocbuf(bp, size); 1153#ifdef PC98 1154 /* 1155 * 1024byte/sector support 1156 */ 1157#define B_XXX2 0x8000000 1158 if (vp->v_flag & 0x10000) bp->b_flags |= B_XXX2; 1159#endif 1160 return (bp); 1161 } 1162} 1163 1164/* 1165 * Get an empty, disassociated buffer of given size. 1166 */ 1167struct buf * 1168geteblk(int size) 1169{ 1170 struct buf *bp; 1171 int s; 1172 1173 s = splbio(); 1174 while ((bp = getnewbuf(0, 0, 0)) == 0); 1175 splx(s); 1176 allocbuf(bp, size); 1177 bp->b_flags |= B_INVAL; 1178 return (bp); 1179} 1180 1181 1182/* 1183 * This code constitutes the buffer memory from either anonymous system 1184 * memory (in the case of non-VMIO operations) or from an associated 1185 * VM object (in the case of VMIO operations). 1186 * 1187 * Note that this code is tricky, and has many complications to resolve 1188 * deadlock or inconsistant data situations. Tread lightly!!! 1189 * 1190 * Modify the length of a buffer's underlying buffer storage without 1191 * destroying information (unless, of course the buffer is shrinking). 1192 */ 1193int 1194allocbuf(struct buf * bp, int size) 1195{ 1196 1197 int s; 1198 int newbsize, mbsize; 1199 int i; 1200 1201 if (!(bp->b_flags & B_BUSY)) 1202 panic("allocbuf: buffer not busy"); 1203 1204 if ((bp->b_flags & B_VMIO) == 0) { 1205 caddr_t origbuf; 1206 int origbufsize; 1207 /* 1208 * Just get anonymous memory from the kernel 1209 */ 1210 mbsize = (size + DEV_BSIZE - 1) & ~(DEV_BSIZE - 1); 1211#if !defined(NO_B_MALLOC) 1212 if (bp->b_flags & B_MALLOC) 1213 newbsize = mbsize; 1214 else 1215#endif 1216 newbsize = round_page(size); 1217 1218 if (newbsize < bp->b_bufsize) { 1219#if !defined(NO_B_MALLOC) 1220 /* 1221 * malloced buffers are not shrunk 1222 */ 1223 if (bp->b_flags & B_MALLOC) { 1224 if (newbsize) { 1225 bp->b_bcount = size; 1226 } else { 1227 free(bp->b_data, M_BIOBUF); 1228 bufspace -= bp->b_bufsize; 1229 bufmallocspace -= bp->b_bufsize; 1230 bp->b_data = (caddr_t) buffers_kva + (bp - buf) * MAXBSIZE; 1231 bp->b_bufsize = 0; 1232 bp->b_bcount = 0; 1233 bp->b_flags &= ~B_MALLOC; 1234 } 1235 return 1; 1236 } 1237#endif 1238 vm_hold_free_pages( 1239 bp, 1240 (vm_offset_t) bp->b_data + newbsize, 1241 (vm_offset_t) bp->b_data + bp->b_bufsize); 1242 } else if (newbsize > bp->b_bufsize) { 1243#if !defined(NO_B_MALLOC) 1244 /* 1245 * We only use malloced memory on the first allocation. 1246 * and revert to page-allocated memory when the buffer grows. 1247 */ 1248 if ( (bufmallocspace < maxbufmallocspace) && 1249 (bp->b_bufsize == 0) && 1250 (mbsize <= PAGE_SIZE/2)) { 1251 1252 bp->b_data = malloc(mbsize, M_BIOBUF, M_WAITOK); 1253 bp->b_bufsize = mbsize; 1254 bp->b_bcount = size; 1255 bp->b_flags |= B_MALLOC; 1256 bufspace += mbsize; 1257 bufmallocspace += mbsize; 1258 return 1; 1259 } 1260#endif 1261 origbuf = NULL; 1262 origbufsize = 0; 1263#if !defined(NO_B_MALLOC) 1264 /* 1265 * If the buffer is growing on it's other-than-first allocation, 1266 * then we revert to the page-allocation scheme. 1267 */ 1268 if (bp->b_flags & B_MALLOC) { 1269 origbuf = bp->b_data; 1270 origbufsize = bp->b_bufsize; 1271 bp->b_data = (caddr_t) buffers_kva + (bp - buf) * MAXBSIZE; 1272 bufspace -= bp->b_bufsize; 1273 bufmallocspace -= bp->b_bufsize; 1274 bp->b_bufsize = 0; 1275 bp->b_flags &= ~B_MALLOC; 1276 newbsize = round_page(newbsize); 1277 } 1278#endif 1279 vm_hold_load_pages( 1280 bp, 1281 (vm_offset_t) bp->b_data + bp->b_bufsize, 1282 (vm_offset_t) bp->b_data + newbsize); 1283#if !defined(NO_B_MALLOC) 1284 if (origbuf) { 1285 bcopy(origbuf, bp->b_data, origbufsize); 1286 free(origbuf, M_BIOBUF); 1287 } 1288#endif 1289 } 1290 } else { 1291 vm_page_t m; 1292 int desiredpages; 1293 1294 newbsize = (size + DEV_BSIZE - 1) & ~(DEV_BSIZE - 1); 1295 desiredpages = (round_page(newbsize) >> PAGE_SHIFT); 1296 1297#if !defined(NO_B_MALLOC) 1298 if (bp->b_flags & B_MALLOC) 1299 panic("allocbuf: VMIO buffer can't be malloced"); 1300#endif 1301 1302 if (newbsize < bp->b_bufsize) { 1303 if (desiredpages < bp->b_npages) { 1304 for (i = desiredpages; i < bp->b_npages; i++) { 1305 /* 1306 * the page is not freed here -- it 1307 * is the responsibility of vnode_pager_setsize 1308 */ 1309 m = bp->b_pages[i]; 1310#if defined(DIAGNOSTIC) 1311 if (m == bogus_page) 1312 panic("allocbuf: bogus page found"); 1313#endif 1314 s = splvm(); 1315 while ((m->flags & PG_BUSY) || (m->busy != 0)) { 1316 m->flags |= PG_WANTED; 1317 tsleep(m, PVM, "biodep", 0); 1318 } 1319 splx(s); 1320 1321 bp->b_pages[i] = NULL; 1322 vm_page_unwire(m); 1323 } 1324 pmap_qremove((vm_offset_t) trunc_page(bp->b_data) + 1325 (desiredpages << PAGE_SHIFT), (bp->b_npages - desiredpages)); 1326 bp->b_npages = desiredpages; 1327 } 1328 } else if (newbsize > bp->b_bufsize) { 1329 vm_object_t obj; 1330 vm_offset_t tinc, toff; 1331 vm_ooffset_t off; 1332 vm_pindex_t objoff; 1333 int pageindex, curbpnpages; 1334 struct vnode *vp; 1335 int bsize; 1336 1337 vp = bp->b_vp; 1338 1339 if (vp->v_type == VBLK) 1340 bsize = DEV_BSIZE; 1341 else 1342 bsize = vp->v_mount->mnt_stat.f_iosize; 1343 1344 if (bp->b_npages < desiredpages) { 1345 obj = vp->v_object; 1346 tinc = PAGE_SIZE; 1347 if (tinc > bsize) 1348 tinc = bsize; 1349 off = (vm_ooffset_t) bp->b_lblkno * bsize; 1350 curbpnpages = bp->b_npages; 1351 doretry: 1352 bp->b_flags |= B_CACHE; 1353 for (toff = 0; toff < newbsize; toff += tinc) { 1354 int bytesinpage; 1355 1356 pageindex = toff >> PAGE_SHIFT; 1357 objoff = OFF_TO_IDX(off + toff); 1358 if (pageindex < curbpnpages) { 1359 1360 m = bp->b_pages[pageindex]; 1361#ifdef VFS_BIO_DIAG 1362 if (m->pindex != objoff) 1363 panic("allocbuf: page changed offset??!!!?"); 1364#endif 1365 bytesinpage = tinc; 1366 if (tinc > (newbsize - toff)) 1367 bytesinpage = newbsize - toff; 1368 if ((bp->b_flags & B_CACHE) && 1369 !vm_page_is_valid(m, 1370 (vm_offset_t) ((toff + off) & PAGE_MASK), 1371 bytesinpage)) { 1372 bp->b_flags &= ~B_CACHE; 1373 } 1374 continue; 1375 } 1376 m = vm_page_lookup(obj, objoff); 1377 if (!m) { 1378 m = vm_page_alloc(obj, objoff, VM_ALLOC_NORMAL); 1379 if (!m) { 1380 VM_WAIT; 1381 goto doretry; 1382 } 1383 /* 1384 * Normally it is unwise to clear PG_BUSY without 1385 * PAGE_WAKEUP -- but it is okay here, as there is 1386 * no chance for blocking between here and vm_page_alloc 1387 */ 1388 m->flags &= ~PG_BUSY; 1389 vm_page_wire(m); 1390 bp->b_flags &= ~B_CACHE; 1391 } else if (m->flags & PG_BUSY) { 1392 s = splvm(); 1393 if (m->flags & PG_BUSY) { 1394 m->flags |= PG_WANTED; 1395 tsleep(m, PVM, "pgtblk", 0); 1396 } 1397 splx(s); 1398 goto doretry; 1399 } else { 1400 if ((curproc != pageproc) && 1401 ((m->queue - m->pc) == PQ_CACHE) && 1402 ((cnt.v_free_count + cnt.v_cache_count) < 1403 (cnt.v_free_min + cnt.v_cache_min))) { 1404 pagedaemon_wakeup(); 1405 } 1406 bytesinpage = tinc; 1407 if (tinc > (newbsize - toff)) 1408 bytesinpage = newbsize - toff; 1409 if ((bp->b_flags & B_CACHE) && 1410 !vm_page_is_valid(m, 1411 (vm_offset_t) ((toff + off) & PAGE_MASK), 1412 bytesinpage)) { 1413 bp->b_flags &= ~B_CACHE; 1414 } 1415 vm_page_wire(m); 1416 } 1417 bp->b_pages[pageindex] = m; 1418 curbpnpages = pageindex + 1; 1419 } 1420 bp->b_data = (caddr_t) trunc_page(bp->b_data); 1421 bp->b_npages = curbpnpages; 1422 pmap_qenter((vm_offset_t) bp->b_data, 1423 bp->b_pages, bp->b_npages); 1424 ((vm_offset_t) bp->b_data) |= off & PAGE_MASK; 1425 } 1426 } 1427 } 1428 if (bp->b_flags & B_VMIO) 1429 vmiospace += bp->b_bufsize; 1430 bufspace += (newbsize - bp->b_bufsize); 1431 bp->b_bufsize = newbsize; 1432 bp->b_bcount = size; 1433 return 1; 1434} 1435 1436/* 1437 * Wait for buffer I/O completion, returning error status. 1438 */ 1439int 1440biowait(register struct buf * bp) 1441{ 1442 int s; 1443 1444 s = splbio(); 1445 while ((bp->b_flags & B_DONE) == 0) 1446 tsleep(bp, PRIBIO, "biowait", 0); 1447 splx(s); 1448 if (bp->b_flags & B_EINTR) { 1449 bp->b_flags &= ~B_EINTR; 1450 return (EINTR); 1451 } 1452 if (bp->b_flags & B_ERROR) { 1453 return (bp->b_error ? bp->b_error : EIO); 1454 } else { 1455 return (0); 1456 } 1457} 1458 1459/* 1460 * Finish I/O on a buffer, calling an optional function. 1461 * This is usually called from interrupt level, so process blocking 1462 * is not *a good idea*. 1463 */ 1464void 1465biodone(register struct buf * bp) 1466{ 1467 int s; 1468 1469 s = splbio(); 1470 if (!(bp->b_flags & B_BUSY)) 1471 panic("biodone: buffer not busy"); 1472 1473 if (bp->b_flags & B_DONE) { 1474 splx(s); 1475 printf("biodone: buffer already done\n"); 1476 return; 1477 } 1478 bp->b_flags |= B_DONE; 1479 1480 if ((bp->b_flags & B_READ) == 0) { 1481 vwakeup(bp); 1482 } 1483#ifdef BOUNCE_BUFFERS 1484 if (bp->b_flags & B_BOUNCE) 1485 vm_bounce_free(bp); 1486#endif 1487 1488 /* call optional completion function if requested */ 1489 if (bp->b_flags & B_CALL) { 1490 bp->b_flags &= ~B_CALL; 1491 (*bp->b_iodone) (bp); 1492 splx(s); 1493 return; 1494 } 1495 if (bp->b_flags & B_VMIO) { 1496 int i, resid; 1497 vm_ooffset_t foff; 1498 vm_page_t m; 1499 vm_object_t obj; 1500 int iosize; 1501 struct vnode *vp = bp->b_vp; 1502 1503 if (vp->v_type == VBLK) 1504 foff = (vm_ooffset_t) DEV_BSIZE * bp->b_lblkno; 1505 else 1506 foff = (vm_ooffset_t) vp->v_mount->mnt_stat.f_iosize * bp->b_lblkno; 1507 obj = vp->v_object; 1508 if (!obj) { 1509 panic("biodone: no object"); 1510 } 1511#if defined(VFS_BIO_DEBUG) 1512 if (obj->paging_in_progress < bp->b_npages) { 1513 printf("biodone: paging in progress(%d) < bp->b_npages(%d)\n", 1514 obj->paging_in_progress, bp->b_npages); 1515 } 1516#endif 1517 iosize = bp->b_bufsize; 1518 for (i = 0; i < bp->b_npages; i++) { 1519 int bogusflag = 0; 1520 m = bp->b_pages[i]; 1521 if (m == bogus_page) { 1522 bogusflag = 1; 1523 m = vm_page_lookup(obj, OFF_TO_IDX(foff)); 1524 if (!m) { 1525#if defined(VFS_BIO_DEBUG) 1526 printf("biodone: page disappeared\n"); 1527#endif 1528 --obj->paging_in_progress; 1529 continue; 1530 } 1531 bp->b_pages[i] = m; 1532 pmap_qenter(trunc_page(bp->b_data), bp->b_pages, bp->b_npages); 1533 } 1534#if defined(VFS_BIO_DEBUG) 1535 if (OFF_TO_IDX(foff) != m->pindex) { 1536 printf("biodone: foff(%d)/m->pindex(%d) mismatch\n", foff, m->pindex); 1537 } 1538#endif 1539 resid = IDX_TO_OFF(m->pindex + 1) - foff; 1540 if (resid > iosize) 1541 resid = iosize; 1542 /* 1543 * In the write case, the valid and clean bits are 1544 * already changed correctly, so we only need to do this 1545 * here in the read case. 1546 */ 1547 if ((bp->b_flags & B_READ) && !bogusflag && resid > 0) { 1548 vm_page_set_validclean(m, 1549 (vm_offset_t) (foff & PAGE_MASK), resid); 1550 } 1551 1552 /* 1553 * when debugging new filesystems or buffer I/O methods, this 1554 * is the most common error that pops up. if you see this, you 1555 * have not set the page busy flag correctly!!! 1556 */ 1557 if (m->busy == 0) { 1558 printf("biodone: page busy < 0, " 1559 "pindex: %d, foff: 0x(%x,%x), " 1560 "resid: %d, index: %d\n", 1561 (int) m->pindex, (int)(foff >> 32), 1562 (int) foff & 0xffffffff, resid, i); 1563 if (vp->v_type != VBLK) 1564 printf(" iosize: %ld, lblkno: %d, flags: 0x%lx, npages: %d\n", 1565 bp->b_vp->v_mount->mnt_stat.f_iosize, 1566 (int) bp->b_lblkno, 1567 bp->b_flags, bp->b_npages); 1568 else 1569 printf(" VDEV, lblkno: %d, flags: 0x%lx, npages: %d\n", 1570 (int) bp->b_lblkno, 1571 bp->b_flags, bp->b_npages); 1572 printf(" valid: 0x%x, dirty: 0x%x, wired: %d\n", 1573 m->valid, m->dirty, m->wire_count); 1574 panic("biodone: page busy < 0\n"); 1575 } 1576 --m->busy; 1577 if ((m->busy == 0) && (m->flags & PG_WANTED)) { 1578 m->flags &= ~PG_WANTED; 1579 wakeup(m); 1580 } 1581 --obj->paging_in_progress; 1582 foff += resid; 1583 iosize -= resid; 1584 } 1585 if (obj && obj->paging_in_progress == 0 && 1586 (obj->flags & OBJ_PIPWNT)) { 1587 obj->flags &= ~OBJ_PIPWNT; 1588 wakeup(obj); 1589 } 1590 } 1591 /* 1592 * For asynchronous completions, release the buffer now. The brelse 1593 * checks for B_WANTED and will do the wakeup there if necessary - so 1594 * no need to do a wakeup here in the async case. 1595 */ 1596 1597 if (bp->b_flags & B_ASYNC) { 1598 if ((bp->b_flags & B_ORDERED) == 0) { 1599 if ((bp->b_flags & (B_NOCACHE | B_INVAL | B_ERROR | B_RELBUF)) != 0) 1600 brelse(bp); 1601 else 1602 bqrelse(bp); 1603 } 1604 } else { 1605 bp->b_flags &= ~B_WANTED; 1606 wakeup(bp); 1607 } 1608 splx(s); 1609} 1610 1611int 1612count_lock_queue() 1613{ 1614 int count; 1615 struct buf *bp; 1616 1617 count = 0; 1618 for (bp = TAILQ_FIRST(&bufqueues[QUEUE_LOCKED]); 1619 bp != NULL; 1620 bp = TAILQ_NEXT(bp, b_freelist)) 1621 count++; 1622 return (count); 1623} 1624 1625int vfs_update_interval = 30; 1626 1627static void 1628vfs_update() 1629{ 1630 (void) spl0(); /* XXX redundant? wrong place? */ 1631 while (1) { 1632 tsleep(&vfs_update_wakeup, PUSER, "update", 1633 hz * vfs_update_interval); 1634 vfs_update_wakeup = 0; 1635 sync(curproc, NULL, NULL); 1636 } 1637} 1638 1639static int 1640sysctl_kern_updateinterval SYSCTL_HANDLER_ARGS 1641{ 1642 int error = sysctl_handle_int(oidp, 1643 oidp->oid_arg1, oidp->oid_arg2, req); 1644 if (!error) 1645 wakeup(&vfs_update_wakeup); 1646 return error; 1647} 1648 1649SYSCTL_PROC(_kern, KERN_UPDATEINTERVAL, update, CTLTYPE_INT|CTLFLAG_RW, 1650 &vfs_update_interval, 0, sysctl_kern_updateinterval, "I", ""); 1651 1652 1653/* 1654 * This routine is called in lieu of iodone in the case of 1655 * incomplete I/O. This keeps the busy status for pages 1656 * consistant. 1657 */ 1658void 1659vfs_unbusy_pages(struct buf * bp) 1660{ 1661 int i; 1662 1663 if (bp->b_flags & B_VMIO) { 1664 struct vnode *vp = bp->b_vp; 1665 vm_object_t obj = vp->v_object; 1666 vm_ooffset_t foff; 1667 1668 foff = (vm_ooffset_t) vp->v_mount->mnt_stat.f_iosize * bp->b_lblkno; 1669 1670 for (i = 0; i < bp->b_npages; i++) { 1671 vm_page_t m = bp->b_pages[i]; 1672 1673 if (m == bogus_page) { 1674 m = vm_page_lookup(obj, OFF_TO_IDX(foff) + i); 1675 if (!m) { 1676 panic("vfs_unbusy_pages: page missing\n"); 1677 } 1678 bp->b_pages[i] = m; 1679 pmap_qenter(trunc_page(bp->b_data), bp->b_pages, bp->b_npages); 1680 } 1681 --obj->paging_in_progress; 1682 --m->busy; 1683 if ((m->busy == 0) && (m->flags & PG_WANTED)) { 1684 m->flags &= ~PG_WANTED; 1685 wakeup(m); 1686 } 1687 } 1688 if (obj->paging_in_progress == 0 && 1689 (obj->flags & OBJ_PIPWNT)) { 1690 obj->flags &= ~OBJ_PIPWNT; 1691 wakeup(obj); 1692 } 1693 } 1694} 1695 1696/* 1697 * This routine is called before a device strategy routine. 1698 * It is used to tell the VM system that paging I/O is in 1699 * progress, and treat the pages associated with the buffer 1700 * almost as being PG_BUSY. Also the object paging_in_progress 1701 * flag is handled to make sure that the object doesn't become 1702 * inconsistant. 1703 */ 1704void 1705vfs_busy_pages(struct buf * bp, int clear_modify) 1706{ 1707 int i; 1708 1709 if (bp->b_flags & B_VMIO) { 1710 vm_object_t obj = bp->b_vp->v_object; 1711 vm_ooffset_t foff; 1712 int iocount = bp->b_bufsize; 1713 1714 if (bp->b_vp->v_type == VBLK) 1715 foff = (vm_ooffset_t) DEV_BSIZE * bp->b_lblkno; 1716 else 1717 foff = (vm_ooffset_t) bp->b_vp->v_mount->mnt_stat.f_iosize * bp->b_lblkno; 1718 vfs_setdirty(bp); 1719 for (i = 0; i < bp->b_npages; i++) { 1720 vm_page_t m = bp->b_pages[i]; 1721 int resid = IDX_TO_OFF(m->pindex + 1) - foff; 1722 1723 if (resid > iocount) 1724 resid = iocount; 1725 if ((bp->b_flags & B_CLUSTER) == 0) { 1726 obj->paging_in_progress++; 1727 m->busy++; 1728 } 1729 vm_page_protect(m, VM_PROT_NONE); 1730 if (clear_modify) { 1731 vm_page_set_validclean(m, 1732 (vm_offset_t) (foff & PAGE_MASK), resid); 1733 } else if (bp->b_bcount >= PAGE_SIZE) { 1734 if (m->valid && (bp->b_flags & B_CACHE) == 0) { 1735 bp->b_pages[i] = bogus_page; 1736 pmap_qenter(trunc_page(bp->b_data), bp->b_pages, bp->b_npages); 1737 } 1738 } 1739 foff += resid; 1740 iocount -= resid; 1741 } 1742 } 1743} 1744 1745/* 1746 * Tell the VM system that the pages associated with this buffer 1747 * are clean. This is used for delayed writes where the data is 1748 * going to go to disk eventually without additional VM intevention. 1749 */ 1750void 1751vfs_clean_pages(struct buf * bp) 1752{ 1753 int i; 1754 1755 if (bp->b_flags & B_VMIO) { 1756 vm_ooffset_t foff; 1757 int iocount = bp->b_bufsize; 1758 1759 if (bp->b_vp->v_type == VBLK) 1760 foff = (vm_ooffset_t) DEV_BSIZE * bp->b_lblkno; 1761 else 1762 foff = (vm_ooffset_t) bp->b_vp->v_mount->mnt_stat.f_iosize * bp->b_lblkno; 1763 1764 for (i = 0; i < bp->b_npages; i++) { 1765 vm_page_t m = bp->b_pages[i]; 1766 int resid = IDX_TO_OFF(m->pindex + 1) - foff; 1767 1768 if (resid > iocount) 1769 resid = iocount; 1770 if (resid > 0) { 1771 vm_page_set_validclean(m, 1772 ((vm_offset_t) foff & PAGE_MASK), resid); 1773 } 1774 foff += resid; 1775 iocount -= resid; 1776 } 1777 } 1778} 1779 1780void 1781vfs_bio_clrbuf(struct buf *bp) { 1782 int i; 1783 if( bp->b_flags & B_VMIO) { 1784 if( (bp->b_npages == 1) && (bp->b_bufsize < PAGE_SIZE)) { 1785 int mask; 1786 mask = 0; 1787 for(i=0;i<bp->b_bufsize;i+=DEV_BSIZE) 1788 mask |= (1 << (i/DEV_BSIZE)); 1789 if( bp->b_pages[0]->valid != mask) { 1790 bzero(bp->b_data, bp->b_bufsize); 1791 } 1792 bp->b_pages[0]->valid = mask; 1793 bp->b_resid = 0; 1794 return; 1795 } 1796 for(i=0;i<bp->b_npages;i++) { 1797 if( bp->b_pages[i]->valid == VM_PAGE_BITS_ALL) 1798 continue; 1799 if( bp->b_pages[i]->valid == 0) { 1800 if ((bp->b_pages[i]->flags & PG_ZERO) == 0) { 1801 bzero(bp->b_data + (i << PAGE_SHIFT), PAGE_SIZE); 1802 } 1803 } else { 1804 int j; 1805 for(j=0;j<PAGE_SIZE/DEV_BSIZE;j++) { 1806 if( (bp->b_pages[i]->valid & (1<<j)) == 0) 1807 bzero(bp->b_data + (i << PAGE_SHIFT) + j * DEV_BSIZE, DEV_BSIZE); 1808 } 1809 } 1810 /* bp->b_pages[i]->valid = VM_PAGE_BITS_ALL; */ 1811 } 1812 bp->b_resid = 0; 1813 } else { 1814 clrbuf(bp); 1815 } 1816} 1817 1818/* 1819 * vm_hold_load_pages and vm_hold_unload pages get pages into 1820 * a buffers address space. The pages are anonymous and are 1821 * not associated with a file object. 1822 */ 1823void 1824vm_hold_load_pages(struct buf * bp, vm_offset_t from, vm_offset_t to) 1825{ 1826 vm_offset_t pg; 1827 vm_page_t p; 1828 int index; 1829 1830 to = round_page(to); 1831 from = round_page(from); 1832 index = (from - trunc_page(bp->b_data)) >> PAGE_SHIFT; 1833 1834 for (pg = from; pg < to; pg += PAGE_SIZE, index++) { 1835 1836tryagain: 1837 1838 p = vm_page_alloc(kernel_object, ((pg - VM_MIN_KERNEL_ADDRESS) >> PAGE_SHIFT), 1839 VM_ALLOC_NORMAL); 1840 if (!p) { 1841 VM_WAIT; 1842 goto tryagain; 1843 } 1844 vm_page_wire(p); 1845 pmap_kenter(pg, VM_PAGE_TO_PHYS(p)); 1846 bp->b_pages[index] = p; 1847 PAGE_WAKEUP(p); 1848 } 1849 bp->b_npages = to >> PAGE_SHIFT; 1850} 1851 1852void 1853vm_hold_free_pages(struct buf * bp, vm_offset_t from, vm_offset_t to) 1854{ 1855 vm_offset_t pg; 1856 vm_page_t p; 1857 int index; 1858 1859 from = round_page(from); 1860 to = round_page(to); 1861 index = (from - trunc_page(bp->b_data)) >> PAGE_SHIFT; 1862 1863 for (pg = from; pg < to; pg += PAGE_SIZE, index++) { 1864 p = bp->b_pages[index]; 1865 if (p && (index < bp->b_npages)) { 1866 if (p->busy) { 1867 printf("vm_hold_free_pages: blkno: %d, lblkno: %d\n", 1868 bp->b_blkno, bp->b_lblkno); 1869 } 1870 bp->b_pages[index] = NULL; 1871 pmap_kremove(pg); 1872 vm_page_unwire(p); 1873 vm_page_free(p); 1874 } 1875 } 1876 bp->b_npages = from >> PAGE_SHIFT; 1877} 1878