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