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