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