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