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