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