2 * Copyright (c) 2004 Poul-Henning Kamp
3 * Copyright (c) 1994,1997 John S. Dyson
6 * Redistribution and use in source and binary forms, with or without
7 * modification, are permitted provided that the following conditions
9 * 1. Redistributions of source code must retain the above copyright
10 * notice, this list of conditions and the following disclaimer.
11 * 2. Redistributions in binary form must reproduce the above copyright
12 * notice, this list of conditions and the following disclaimer in the
13 * documentation and/or other materials provided with the distribution.
15 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
16 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
17 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
18 * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
19 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
20 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
21 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
22 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
23 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
24 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
29 * this file contains a new buffer I/O scheme implementing a coherent
30 * VM object and buffer cache scheme. Pains have been taken to make
31 * sure that the performance degradation associated with schemes such
32 * as this is not realized.
34 * Author: John S. Dyson
35 * Significant help during the development and debugging phases
36 * had been provided by David Greenman, also of the FreeBSD core team.
38 * see man buf(9) for more info.
41 #include <sys/cdefs.h>
42 __FBSDID("$FreeBSD$");
44 #include <sys/param.h>
45 #include <sys/systm.h>
49 #include <sys/devicestat.h>
50 #include <sys/eventhandler.h>
52 #include <sys/limits.h>
54 #include <sys/malloc.h>
55 #include <sys/mount.h>
56 #include <sys/mutex.h>
57 #include <sys/kernel.h>
58 #include <sys/kthread.h>
60 #include <sys/resourcevar.h>
61 #include <sys/sysctl.h>
62 #include <sys/vmmeter.h>
63 #include <sys/vnode.h>
64 #include <geom/geom.h>
66 #include <vm/vm_param.h>
67 #include <vm/vm_kern.h>
68 #include <vm/vm_pageout.h>
69 #include <vm/vm_page.h>
70 #include <vm/vm_object.h>
71 #include <vm/vm_extern.h>
72 #include <vm/vm_map.h>
73 #include "opt_compat.h"
74 #include "opt_directio.h"
77 static MALLOC_DEFINE(M_BIOBUF, "biobuf", "BIO buffer");
79 struct bio_ops bioops; /* I/O operation notification */
81 struct buf_ops buf_ops_bio = {
82 .bop_name = "buf_ops_bio",
83 .bop_write = bufwrite,
84 .bop_strategy = bufstrategy,
86 .bop_bdflush = bufbdflush,
90 * XXX buf is global because kern_shutdown.c and ffs_checkoverlap has
91 * carnal knowledge of buffers. This knowledge should be moved to vfs_bio.c.
93 struct buf *buf; /* buffer header pool */
95 static struct proc *bufdaemonproc;
97 static int inmem(struct vnode *vp, daddr_t blkno);
98 static void vm_hold_free_pages(struct buf *bp, vm_offset_t from,
100 static void vm_hold_load_pages(struct buf *bp, vm_offset_t from,
102 static void vfs_page_set_valid(struct buf *bp, vm_ooffset_t off, vm_page_t m);
103 static void vfs_page_set_validclean(struct buf *bp, vm_ooffset_t off,
105 static void vfs_clean_pages(struct buf *bp);
106 static void vfs_setdirty(struct buf *bp);
107 static void vfs_setdirty_locked_object(struct buf *bp);
108 static void vfs_vmio_release(struct buf *bp);
109 static int vfs_bio_clcheck(struct vnode *vp, int size,
110 daddr_t lblkno, daddr_t blkno);
111 static int buf_do_flush(struct vnode *vp);
112 static int flushbufqueues(struct vnode *, int, int);
113 static void buf_daemon(void);
114 static void bremfreel(struct buf *bp);
115 #if defined(COMPAT_FREEBSD4) || defined(COMPAT_FREEBSD5) || \
116 defined(COMPAT_FREEBSD6) || defined(COMPAT_FREEBSD7)
117 static int sysctl_bufspace(SYSCTL_HANDLER_ARGS);
120 int vmiodirenable = TRUE;
121 SYSCTL_INT(_vfs, OID_AUTO, vmiodirenable, CTLFLAG_RW, &vmiodirenable, 0,
122 "Use the VM system for directory writes");
123 long runningbufspace;
124 SYSCTL_LONG(_vfs, OID_AUTO, runningbufspace, CTLFLAG_RD, &runningbufspace, 0,
125 "Amount of presently outstanding async buffer io");
126 static long bufspace;
127 #if defined(COMPAT_FREEBSD4) || defined(COMPAT_FREEBSD5) || \
128 defined(COMPAT_FREEBSD6) || defined(COMPAT_FREEBSD7)
129 SYSCTL_PROC(_vfs, OID_AUTO, bufspace, CTLTYPE_LONG|CTLFLAG_MPSAFE|CTLFLAG_RD,
130 &bufspace, 0, sysctl_bufspace, "L", "Virtual memory used for buffers");
132 SYSCTL_LONG(_vfs, OID_AUTO, bufspace, CTLFLAG_RD, &bufspace, 0,
133 "Virtual memory used for buffers");
135 static long maxbufspace;
136 SYSCTL_LONG(_vfs, OID_AUTO, maxbufspace, CTLFLAG_RD, &maxbufspace, 0,
137 "Maximum allowed value of bufspace (including buf_daemon)");
138 static long bufmallocspace;
139 SYSCTL_LONG(_vfs, OID_AUTO, bufmallocspace, CTLFLAG_RD, &bufmallocspace, 0,
140 "Amount of malloced memory for buffers");
141 static long maxbufmallocspace;
142 SYSCTL_LONG(_vfs, OID_AUTO, maxmallocbufspace, CTLFLAG_RW, &maxbufmallocspace, 0,
143 "Maximum amount of malloced memory for buffers");
144 static long lobufspace;
145 SYSCTL_LONG(_vfs, OID_AUTO, lobufspace, CTLFLAG_RD, &lobufspace, 0,
146 "Minimum amount of buffers we want to have");
148 SYSCTL_LONG(_vfs, OID_AUTO, hibufspace, CTLFLAG_RD, &hibufspace, 0,
149 "Maximum allowed value of bufspace (excluding buf_daemon)");
150 static int bufreusecnt;
151 SYSCTL_INT(_vfs, OID_AUTO, bufreusecnt, CTLFLAG_RW, &bufreusecnt, 0,
152 "Number of times we have reused a buffer");
153 static int buffreekvacnt;
154 SYSCTL_INT(_vfs, OID_AUTO, buffreekvacnt, CTLFLAG_RW, &buffreekvacnt, 0,
155 "Number of times we have freed the KVA space from some buffer");
156 static int bufdefragcnt;
157 SYSCTL_INT(_vfs, OID_AUTO, bufdefragcnt, CTLFLAG_RW, &bufdefragcnt, 0,
158 "Number of times we have had to repeat buffer allocation to defragment");
159 static long lorunningspace;
160 SYSCTL_LONG(_vfs, OID_AUTO, lorunningspace, CTLFLAG_RW, &lorunningspace, 0,
161 "Minimum preferred space used for in-progress I/O");
162 static long hirunningspace;
163 SYSCTL_LONG(_vfs, OID_AUTO, hirunningspace, CTLFLAG_RW, &hirunningspace, 0,
164 "Maximum amount of space to use for in-progress I/O");
165 int dirtybufferflushes;
166 SYSCTL_INT(_vfs, OID_AUTO, dirtybufferflushes, CTLFLAG_RW, &dirtybufferflushes,
167 0, "Number of bdwrite to bawrite conversions to limit dirty buffers");
169 SYSCTL_INT(_vfs, OID_AUTO, bdwriteskip, CTLFLAG_RW, &bdwriteskip,
170 0, "Number of buffers supplied to bdwrite with snapshot deadlock risk");
171 int altbufferflushes;
172 SYSCTL_INT(_vfs, OID_AUTO, altbufferflushes, CTLFLAG_RW, &altbufferflushes,
173 0, "Number of fsync flushes to limit dirty buffers");
174 static int recursiveflushes;
175 SYSCTL_INT(_vfs, OID_AUTO, recursiveflushes, CTLFLAG_RW, &recursiveflushes,
176 0, "Number of flushes skipped due to being recursive");
177 static int numdirtybuffers;
178 SYSCTL_INT(_vfs, OID_AUTO, numdirtybuffers, CTLFLAG_RD, &numdirtybuffers, 0,
179 "Number of buffers that are dirty (has unwritten changes) at the moment");
180 static int lodirtybuffers;
181 SYSCTL_INT(_vfs, OID_AUTO, lodirtybuffers, CTLFLAG_RW, &lodirtybuffers, 0,
182 "How many buffers we want to have free before bufdaemon can sleep");
183 static int hidirtybuffers;
184 SYSCTL_INT(_vfs, OID_AUTO, hidirtybuffers, CTLFLAG_RW, &hidirtybuffers, 0,
185 "When the number of dirty buffers is considered severe");
187 SYSCTL_INT(_vfs, OID_AUTO, dirtybufthresh, CTLFLAG_RW, &dirtybufthresh,
188 0, "Number of bdwrite to bawrite conversions to clear dirty buffers");
189 static int numfreebuffers;
190 SYSCTL_INT(_vfs, OID_AUTO, numfreebuffers, CTLFLAG_RD, &numfreebuffers, 0,
191 "Number of free buffers");
192 static int lofreebuffers;
193 SYSCTL_INT(_vfs, OID_AUTO, lofreebuffers, CTLFLAG_RW, &lofreebuffers, 0,
195 static int hifreebuffers;
196 SYSCTL_INT(_vfs, OID_AUTO, hifreebuffers, CTLFLAG_RW, &hifreebuffers, 0,
197 "XXX Complicatedly unused");
198 static int getnewbufcalls;
199 SYSCTL_INT(_vfs, OID_AUTO, getnewbufcalls, CTLFLAG_RW, &getnewbufcalls, 0,
200 "Number of calls to getnewbuf");
201 static int getnewbufrestarts;
202 SYSCTL_INT(_vfs, OID_AUTO, getnewbufrestarts, CTLFLAG_RW, &getnewbufrestarts, 0,
203 "Number of times getnewbuf has had to restart a buffer aquisition");
204 static int flushbufqtarget = 100;
205 SYSCTL_INT(_vfs, OID_AUTO, flushbufqtarget, CTLFLAG_RW, &flushbufqtarget, 0,
206 "Amount of work to do in flushbufqueues when helping bufdaemon");
207 static long notbufdflashes;
208 SYSCTL_LONG(_vfs, OID_AUTO, notbufdflashes, CTLFLAG_RD, ¬bufdflashes, 0,
209 "Number of dirty buffer flushes done by the bufdaemon helpers");
212 * Wakeup point for bufdaemon, as well as indicator of whether it is already
213 * active. Set to 1 when the bufdaemon is already "on" the queue, 0 when it
216 static int bd_request;
219 * Request for the buf daemon to write more buffers than is indicated by
220 * lodirtybuf. This may be necessary to push out excess dependencies or
221 * defragment the address space where a simple count of the number of dirty
222 * buffers is insufficient to characterize the demand for flushing them.
224 static int bd_speedupreq;
227 * This lock synchronizes access to bd_request.
229 static struct mtx bdlock;
232 * bogus page -- for I/O to/from partially complete buffers
233 * this is a temporary solution to the problem, but it is not
234 * really that bad. it would be better to split the buffer
235 * for input in the case of buffers partially already in memory,
236 * but the code is intricate enough already.
238 vm_page_t bogus_page;
241 * Synchronization (sleep/wakeup) variable for active buffer space requests.
242 * Set when wait starts, cleared prior to wakeup().
243 * Used in runningbufwakeup() and waitrunningbufspace().
245 static int runningbufreq;
248 * This lock protects the runningbufreq and synchronizes runningbufwakeup and
249 * waitrunningbufspace().
251 static struct mtx rbreqlock;
254 * Synchronization (sleep/wakeup) variable for buffer requests.
255 * Can contain the VFS_BIO_NEED flags defined below; setting/clearing is done
257 * Used in numdirtywakeup(), bufspacewakeup(), bufcountwakeup(), bwillwrite(),
258 * getnewbuf(), and getblk().
260 static int needsbuffer;
263 * Lock that protects needsbuffer and the sleeps/wakeups surrounding it.
265 static struct mtx nblock;
268 * Definitions for the buffer free lists.
270 #define BUFFER_QUEUES 6 /* number of free buffer queues */
272 #define QUEUE_NONE 0 /* on no queue */
273 #define QUEUE_CLEAN 1 /* non-B_DELWRI buffers */
274 #define QUEUE_DIRTY 2 /* B_DELWRI buffers */
275 #define QUEUE_DIRTY_GIANT 3 /* B_DELWRI buffers that need giant */
276 #define QUEUE_EMPTYKVA 4 /* empty buffer headers w/KVA assignment */
277 #define QUEUE_EMPTY 5 /* empty buffer headers */
278 #define QUEUE_SENTINEL 1024 /* not an queue index, but mark for sentinel */
280 /* Queues for free buffers with various properties */
281 static TAILQ_HEAD(bqueues, buf) bufqueues[BUFFER_QUEUES] = { { 0 } };
283 /* Lock for the bufqueues */
284 static struct mtx bqlock;
287 * Single global constant for BUF_WMESG, to avoid getting multiple references.
288 * buf_wmesg is referred from macros.
290 const char *buf_wmesg = BUF_WMESG;
292 #define VFS_BIO_NEED_ANY 0x01 /* any freeable buffer */
293 #define VFS_BIO_NEED_DIRTYFLUSH 0x02 /* waiting for dirty buffer flush */
294 #define VFS_BIO_NEED_FREE 0x04 /* wait for free bufs, hi hysteresis */
295 #define VFS_BIO_NEED_BUFSPACE 0x08 /* wait for buf space, lo hysteresis */
297 #if defined(COMPAT_FREEBSD4) || defined(COMPAT_FREEBSD5) || \
298 defined(COMPAT_FREEBSD6) || defined(COMPAT_FREEBSD7)
300 sysctl_bufspace(SYSCTL_HANDLER_ARGS)
305 if (sizeof(int) == sizeof(long) || req->oldlen >= sizeof(long))
306 return (sysctl_handle_long(oidp, arg1, arg2, req));
307 lvalue = *(long *)arg1;
308 if (lvalue > INT_MAX)
309 /* On overflow, still write out a long to trigger ENOMEM. */
310 return (sysctl_handle_long(oidp, &lvalue, 0, req));
312 return (sysctl_handle_int(oidp, &ivalue, 0, req));
317 extern void ffs_rawread_setup(void);
318 #endif /* DIRECTIO */
322 * If someone is blocked due to there being too many dirty buffers,
323 * and numdirtybuffers is now reasonable, wake them up.
327 numdirtywakeup(int level)
330 if (numdirtybuffers <= level) {
332 if (needsbuffer & VFS_BIO_NEED_DIRTYFLUSH) {
333 needsbuffer &= ~VFS_BIO_NEED_DIRTYFLUSH;
334 wakeup(&needsbuffer);
343 * Called when buffer space is potentially available for recovery.
344 * getnewbuf() will block on this flag when it is unable to free
345 * sufficient buffer space. Buffer space becomes recoverable when
346 * bp's get placed back in the queues.
354 * If someone is waiting for BUF space, wake them up. Even
355 * though we haven't freed the kva space yet, the waiting
356 * process will be able to now.
359 if (needsbuffer & VFS_BIO_NEED_BUFSPACE) {
360 needsbuffer &= ~VFS_BIO_NEED_BUFSPACE;
361 wakeup(&needsbuffer);
367 * runningbufwakeup() - in-progress I/O accounting.
371 runningbufwakeup(struct buf *bp)
374 if (bp->b_runningbufspace) {
375 atomic_subtract_long(&runningbufspace, bp->b_runningbufspace);
376 bp->b_runningbufspace = 0;
377 mtx_lock(&rbreqlock);
378 if (runningbufreq && runningbufspace <= lorunningspace) {
380 wakeup(&runningbufreq);
382 mtx_unlock(&rbreqlock);
389 * Called when a buffer has been added to one of the free queues to
390 * account for the buffer and to wakeup anyone waiting for free buffers.
391 * This typically occurs when large amounts of metadata are being handled
392 * by the buffer cache ( else buffer space runs out first, usually ).
399 atomic_add_int(&numfreebuffers, 1);
402 needsbuffer &= ~VFS_BIO_NEED_ANY;
403 if (numfreebuffers >= hifreebuffers)
404 needsbuffer &= ~VFS_BIO_NEED_FREE;
405 wakeup(&needsbuffer);
411 * waitrunningbufspace()
413 * runningbufspace is a measure of the amount of I/O currently
414 * running. This routine is used in async-write situations to
415 * prevent creating huge backups of pending writes to a device.
416 * Only asynchronous writes are governed by this function.
418 * Reads will adjust runningbufspace, but will not block based on it.
419 * The read load has a side effect of reducing the allowed write load.
421 * This does NOT turn an async write into a sync write. It waits
422 * for earlier writes to complete and generally returns before the
423 * caller's write has reached the device.
426 waitrunningbufspace(void)
429 mtx_lock(&rbreqlock);
430 while (runningbufspace > hirunningspace) {
432 msleep(&runningbufreq, &rbreqlock, PVM, "wdrain", 0);
434 mtx_unlock(&rbreqlock);
439 * vfs_buf_test_cache:
441 * Called when a buffer is extended. This function clears the B_CACHE
442 * bit if the newly extended portion of the buffer does not contain
447 vfs_buf_test_cache(struct buf *bp,
448 vm_ooffset_t foff, vm_offset_t off, vm_offset_t size,
452 VM_OBJECT_LOCK_ASSERT(m->object, MA_OWNED);
453 if (bp->b_flags & B_CACHE) {
454 int base = (foff + off) & PAGE_MASK;
455 if (vm_page_is_valid(m, base, size) == 0)
456 bp->b_flags &= ~B_CACHE;
460 /* Wake up the buffer daemon if necessary */
463 bd_wakeup(int dirtybuflevel)
467 if (bd_request == 0 && numdirtybuffers >= dirtybuflevel) {
475 * bd_speedup - speedup the buffer cache flushing code
485 if (bd_speedupreq == 0 || bd_request == 0)
495 * Calculating buffer cache scaling values and reserve space for buffer
496 * headers. This is called during low level kernel initialization and
497 * may be called more then once. We CANNOT write to the memory area
498 * being reserved at this time.
501 kern_vfs_bio_buffer_alloc(caddr_t v, long physmem_est)
507 * physmem_est is in pages. Convert it to kilobytes (assumes
508 * PAGE_SIZE is >= 1K)
510 physmem_est = physmem_est * (PAGE_SIZE / 1024);
513 * The nominal buffer size (and minimum KVA allocation) is BKVASIZE.
514 * For the first 64MB of ram nominally allocate sufficient buffers to
515 * cover 1/4 of our ram. Beyond the first 64MB allocate additional
516 * buffers to cover 1/10 of our ram over 64MB. When auto-sizing
517 * the buffer cache we limit the eventual kva reservation to
520 * factor represents the 1/4 x ram conversion.
523 int factor = 4 * BKVASIZE / 1024;
526 if (physmem_est > 4096)
527 nbuf += min((physmem_est - 4096) / factor,
529 if (physmem_est > 65536)
530 nbuf += (physmem_est - 65536) * 2 / (factor * 5);
532 if (maxbcache && nbuf > maxbcache / BKVASIZE)
533 nbuf = maxbcache / BKVASIZE;
538 /* XXX Avoid unsigned long overflows later on with maxbufspace. */
539 maxbuf = (LONG_MAX / 3) / BKVASIZE;
542 printf("Warning: nbufs lowered from %d to %ld\n", nbuf,
548 * swbufs are used as temporary holders for I/O, such as paging I/O.
549 * We have no less then 16 and no more then 256.
551 nswbuf = max(min(nbuf/4, 256), 16);
553 if (nswbuf < NSWBUF_MIN)
561 * Reserve space for the buffer cache buffers
564 v = (caddr_t)(swbuf + nswbuf);
566 v = (caddr_t)(buf + nbuf);
571 /* Initialize the buffer subsystem. Called before use of any buffers. */
578 mtx_init(&bqlock, "buf queue lock", NULL, MTX_DEF);
579 mtx_init(&rbreqlock, "runningbufspace lock", NULL, MTX_DEF);
580 mtx_init(&nblock, "needsbuffer lock", NULL, MTX_DEF);
581 mtx_init(&bdlock, "buffer daemon lock", NULL, MTX_DEF);
583 /* next, make a null set of free lists */
584 for (i = 0; i < BUFFER_QUEUES; i++)
585 TAILQ_INIT(&bufqueues[i]);
587 /* finally, initialize each buffer header and stick on empty q */
588 for (i = 0; i < nbuf; i++) {
590 bzero(bp, sizeof *bp);
591 bp->b_flags = B_INVAL; /* we're just an empty header */
592 bp->b_rcred = NOCRED;
593 bp->b_wcred = NOCRED;
594 bp->b_qindex = QUEUE_EMPTY;
597 LIST_INIT(&bp->b_dep);
599 TAILQ_INSERT_TAIL(&bufqueues[QUEUE_EMPTY], bp, b_freelist);
603 * maxbufspace is the absolute maximum amount of buffer space we are
604 * allowed to reserve in KVM and in real terms. The absolute maximum
605 * is nominally used by buf_daemon. hibufspace is the nominal maximum
606 * used by most other processes. The differential is required to
607 * ensure that buf_daemon is able to run when other processes might
608 * be blocked waiting for buffer space.
610 * maxbufspace is based on BKVASIZE. Allocating buffers larger then
611 * this may result in KVM fragmentation which is not handled optimally
614 maxbufspace = (long)nbuf * BKVASIZE;
615 hibufspace = lmax(3 * maxbufspace / 4, maxbufspace - MAXBSIZE * 10);
616 lobufspace = hibufspace - MAXBSIZE;
618 lorunningspace = 512 * 1024;
619 hirunningspace = 1024 * 1024;
622 * Limit the amount of malloc memory since it is wired permanently into
623 * the kernel space. Even though this is accounted for in the buffer
624 * allocation, we don't want the malloced region to grow uncontrolled.
625 * The malloc scheme improves memory utilization significantly on average
626 * (small) directories.
628 maxbufmallocspace = hibufspace / 20;
631 * Reduce the chance of a deadlock occuring by limiting the number
632 * of delayed-write dirty buffers we allow to stack up.
634 hidirtybuffers = nbuf / 4 + 20;
635 dirtybufthresh = hidirtybuffers * 9 / 10;
638 * To support extreme low-memory systems, make sure hidirtybuffers cannot
639 * eat up all available buffer space. This occurs when our minimum cannot
640 * be met. We try to size hidirtybuffers to 3/4 our buffer space assuming
641 * BKVASIZE'd (8K) buffers.
643 while ((long)hidirtybuffers * BKVASIZE > 3 * hibufspace / 4) {
644 hidirtybuffers >>= 1;
646 lodirtybuffers = hidirtybuffers / 2;
649 * Try to keep the number of free buffers in the specified range,
650 * and give special processes (e.g. like buf_daemon) access to an
653 lofreebuffers = nbuf / 18 + 5;
654 hifreebuffers = 2 * lofreebuffers;
655 numfreebuffers = nbuf;
657 bogus_page = vm_page_alloc(NULL, 0, VM_ALLOC_NOOBJ |
658 VM_ALLOC_NORMAL | VM_ALLOC_WIRED);
662 * bfreekva() - free the kva allocation for a buffer.
664 * Since this call frees up buffer space, we call bufspacewakeup().
667 bfreekva(struct buf *bp)
671 atomic_add_int(&buffreekvacnt, 1);
672 atomic_subtract_long(&bufspace, bp->b_kvasize);
673 vm_map_remove(buffer_map, (vm_offset_t) bp->b_kvabase,
674 (vm_offset_t) bp->b_kvabase + bp->b_kvasize);
683 * Mark the buffer for removal from the appropriate free list in brelse.
687 bremfree(struct buf *bp)
690 CTR3(KTR_BUF, "bremfree(%p) vp %p flags %X", bp, bp->b_vp, bp->b_flags);
691 KASSERT((bp->b_flags & B_REMFREE) == 0,
692 ("bremfree: buffer %p already marked for delayed removal.", bp));
693 KASSERT(bp->b_qindex != QUEUE_NONE,
694 ("bremfree: buffer %p not on a queue.", bp));
697 bp->b_flags |= B_REMFREE;
698 /* Fixup numfreebuffers count. */
699 if ((bp->b_flags & B_INVAL) || (bp->b_flags & B_DELWRI) == 0)
700 atomic_subtract_int(&numfreebuffers, 1);
706 * Force an immediate removal from a free list. Used only in nfs when
707 * it abuses the b_freelist pointer.
710 bremfreef(struct buf *bp)
720 * Removes a buffer from the free list, must be called with the
724 bremfreel(struct buf *bp)
726 CTR3(KTR_BUF, "bremfreel(%p) vp %p flags %X",
727 bp, bp->b_vp, bp->b_flags);
728 KASSERT(bp->b_qindex != QUEUE_NONE,
729 ("bremfreel: buffer %p not on a queue.", bp));
731 mtx_assert(&bqlock, MA_OWNED);
733 TAILQ_REMOVE(&bufqueues[bp->b_qindex], bp, b_freelist);
734 bp->b_qindex = QUEUE_NONE;
736 * If this was a delayed bremfree() we only need to remove the buffer
737 * from the queue and return the stats are already done.
739 if (bp->b_flags & B_REMFREE) {
740 bp->b_flags &= ~B_REMFREE;
744 * Fixup numfreebuffers count. If the buffer is invalid or not
745 * delayed-write, the buffer was free and we must decrement
748 if ((bp->b_flags & B_INVAL) || (bp->b_flags & B_DELWRI) == 0)
749 atomic_subtract_int(&numfreebuffers, 1);
754 * Get a buffer with the specified data. Look in the cache first. We
755 * must clear BIO_ERROR and B_INVAL prior to initiating I/O. If B_CACHE
756 * is set, the buffer is valid and we do not have to do anything ( see
757 * getblk() ). This is really just a special case of breadn().
760 bread(struct vnode * vp, daddr_t blkno, int size, struct ucred * cred,
764 return (breadn(vp, blkno, size, 0, 0, 0, cred, bpp));
768 * Attempt to initiate asynchronous I/O on read-ahead blocks. We must
769 * clear BIO_ERROR and B_INVAL prior to initiating I/O . If B_CACHE is set,
770 * the buffer is valid and we do not have to do anything.
773 breada(struct vnode * vp, daddr_t * rablkno, int * rabsize,
774 int cnt, struct ucred * cred)
779 for (i = 0; i < cnt; i++, rablkno++, rabsize++) {
780 if (inmem(vp, *rablkno))
782 rabp = getblk(vp, *rablkno, *rabsize, 0, 0, 0);
784 if ((rabp->b_flags & B_CACHE) == 0) {
785 if (!TD_IS_IDLETHREAD(curthread))
786 curthread->td_ru.ru_inblock++;
787 rabp->b_flags |= B_ASYNC;
788 rabp->b_flags &= ~B_INVAL;
789 rabp->b_ioflags &= ~BIO_ERROR;
790 rabp->b_iocmd = BIO_READ;
791 if (rabp->b_rcred == NOCRED && cred != NOCRED)
792 rabp->b_rcred = crhold(cred);
793 vfs_busy_pages(rabp, 0);
795 rabp->b_iooffset = dbtob(rabp->b_blkno);
804 * Operates like bread, but also starts asynchronous I/O on
808 breadn(struct vnode * vp, daddr_t blkno, int size,
809 daddr_t * rablkno, int *rabsize,
810 int cnt, struct ucred * cred, struct buf **bpp)
813 int rv = 0, readwait = 0;
815 CTR3(KTR_BUF, "breadn(%p, %jd, %d)", vp, blkno, size);
816 *bpp = bp = getblk(vp, blkno, size, 0, 0, 0);
818 /* if not found in cache, do some I/O */
819 if ((bp->b_flags & B_CACHE) == 0) {
820 if (!TD_IS_IDLETHREAD(curthread))
821 curthread->td_ru.ru_inblock++;
822 bp->b_iocmd = BIO_READ;
823 bp->b_flags &= ~B_INVAL;
824 bp->b_ioflags &= ~BIO_ERROR;
825 if (bp->b_rcred == NOCRED && cred != NOCRED)
826 bp->b_rcred = crhold(cred);
827 vfs_busy_pages(bp, 0);
828 bp->b_iooffset = dbtob(bp->b_blkno);
833 breada(vp, rablkno, rabsize, cnt, cred);
842 * Write, release buffer on completion. (Done by iodone
843 * if async). Do not bother writing anything if the buffer
846 * Note that we set B_CACHE here, indicating that buffer is
847 * fully valid and thus cacheable. This is true even of NFS
848 * now so we set it generally. This could be set either here
849 * or in biodone() since the I/O is synchronous. We put it
853 bufwrite(struct buf *bp)
859 CTR3(KTR_BUF, "bufwrite(%p) vp %p flags %X", bp, bp->b_vp, bp->b_flags);
860 if (bp->b_flags & B_INVAL) {
865 oldflags = bp->b_flags;
869 if (bp->b_pin_count > 0)
872 KASSERT(!(bp->b_vflags & BV_BKGRDINPROG),
873 ("FFS background buffer should not get here %p", bp));
877 vp_md = vp->v_vflag & VV_MD;
881 /* Mark the buffer clean */
884 bp->b_flags &= ~B_DONE;
885 bp->b_ioflags &= ~BIO_ERROR;
886 bp->b_flags |= B_CACHE;
887 bp->b_iocmd = BIO_WRITE;
889 bufobj_wref(bp->b_bufobj);
890 vfs_busy_pages(bp, 1);
893 * Normal bwrites pipeline writes
895 bp->b_runningbufspace = bp->b_bufsize;
896 atomic_add_long(&runningbufspace, bp->b_runningbufspace);
898 if (!TD_IS_IDLETHREAD(curthread))
899 curthread->td_ru.ru_oublock++;
900 if (oldflags & B_ASYNC)
902 bp->b_iooffset = dbtob(bp->b_blkno);
905 if ((oldflags & B_ASYNC) == 0) {
906 int rtval = bufwait(bp);
911 * don't allow the async write to saturate the I/O
912 * system. We will not deadlock here because
913 * we are blocking waiting for I/O that is already in-progress
914 * to complete. We do not block here if it is the update
915 * or syncer daemon trying to clean up as that can lead
918 if ((curthread->td_pflags & TDP_NORUNNINGBUF) == 0 && !vp_md)
919 waitrunningbufspace();
926 bufbdflush(struct bufobj *bo, struct buf *bp)
930 if (bo->bo_dirty.bv_cnt > dirtybufthresh + 10) {
931 (void) VOP_FSYNC(bp->b_vp, MNT_NOWAIT, curthread);
933 } else if (bo->bo_dirty.bv_cnt > dirtybufthresh) {
936 * Try to find a buffer to flush.
938 TAILQ_FOREACH(nbp, &bo->bo_dirty.bv_hd, b_bobufs) {
939 if ((nbp->b_vflags & BV_BKGRDINPROG) ||
941 LK_EXCLUSIVE | LK_NOWAIT, NULL))
944 panic("bdwrite: found ourselves");
946 /* Don't countdeps with the bo lock held. */
947 if (buf_countdeps(nbp, 0)) {
952 if (nbp->b_flags & B_CLUSTEROK) {
958 dirtybufferflushes++;
967 * Delayed write. (Buffer is marked dirty). Do not bother writing
968 * anything if the buffer is marked invalid.
970 * Note that since the buffer must be completely valid, we can safely
971 * set B_CACHE. In fact, we have to set B_CACHE here rather then in
972 * biodone() in order to prevent getblk from writing the buffer
976 bdwrite(struct buf *bp)
978 struct thread *td = curthread;
982 CTR3(KTR_BUF, "bdwrite(%p) vp %p flags %X", bp, bp->b_vp, bp->b_flags);
983 KASSERT(bp->b_bufobj != NULL, ("No b_bufobj %p", bp));
986 if (bp->b_flags & B_INVAL) {
992 * If we have too many dirty buffers, don't create any more.
993 * If we are wildly over our limit, then force a complete
994 * cleanup. Otherwise, just keep the situation from getting
995 * out of control. Note that we have to avoid a recursive
996 * disaster and not try to clean up after our own cleanup!
1000 if ((td->td_pflags & (TDP_COWINPROGRESS|TDP_INBDFLUSH)) == 0) {
1001 td->td_pflags |= TDP_INBDFLUSH;
1003 td->td_pflags &= ~TDP_INBDFLUSH;
1009 * Set B_CACHE, indicating that the buffer is fully valid. This is
1010 * true even of NFS now.
1012 bp->b_flags |= B_CACHE;
1015 * This bmap keeps the system from needing to do the bmap later,
1016 * perhaps when the system is attempting to do a sync. Since it
1017 * is likely that the indirect block -- or whatever other datastructure
1018 * that the filesystem needs is still in memory now, it is a good
1019 * thing to do this. Note also, that if the pageout daemon is
1020 * requesting a sync -- there might not be enough memory to do
1021 * the bmap then... So, this is important to do.
1023 if (vp->v_type != VCHR && bp->b_lblkno == bp->b_blkno) {
1024 VOP_BMAP(vp, bp->b_lblkno, NULL, &bp->b_blkno, NULL, NULL);
1028 * Set the *dirty* buffer range based upon the VM system dirty pages.
1033 * We need to do this here to satisfy the vnode_pager and the
1034 * pageout daemon, so that it thinks that the pages have been
1035 * "cleaned". Note that since the pages are in a delayed write
1036 * buffer -- the VFS layer "will" see that the pages get written
1037 * out on the next sync, or perhaps the cluster will be completed.
1039 vfs_clean_pages(bp);
1043 * Wakeup the buffer flushing daemon if we have a lot of dirty
1044 * buffers (midpoint between our recovery point and our stall
1047 bd_wakeup((lodirtybuffers + hidirtybuffers) / 2);
1050 * note: we cannot initiate I/O from a bdwrite even if we wanted to,
1051 * due to the softdep code.
1058 * Turn buffer into delayed write request. We must clear BIO_READ and
1059 * B_RELBUF, and we must set B_DELWRI. We reassign the buffer to
1060 * itself to properly update it in the dirty/clean lists. We mark it
1061 * B_DONE to ensure that any asynchronization of the buffer properly
1062 * clears B_DONE ( else a panic will occur later ).
1064 * bdirty() is kinda like bdwrite() - we have to clear B_INVAL which
1065 * might have been set pre-getblk(). Unlike bwrite/bdwrite, bdirty()
1066 * should only be called if the buffer is known-good.
1068 * Since the buffer is not on a queue, we do not update the numfreebuffers
1071 * The buffer must be on QUEUE_NONE.
1074 bdirty(struct buf *bp)
1077 CTR3(KTR_BUF, "bdirty(%p) vp %p flags %X",
1078 bp, bp->b_vp, bp->b_flags);
1079 KASSERT(bp->b_bufobj != NULL, ("No b_bufobj %p", bp));
1080 KASSERT(bp->b_flags & B_REMFREE || bp->b_qindex == QUEUE_NONE,
1081 ("bdirty: buffer %p still on queue %d", bp, bp->b_qindex));
1082 BUF_ASSERT_HELD(bp);
1083 bp->b_flags &= ~(B_RELBUF);
1084 bp->b_iocmd = BIO_WRITE;
1086 if ((bp->b_flags & B_DELWRI) == 0) {
1087 bp->b_flags |= /* XXX B_DONE | */ B_DELWRI;
1089 atomic_add_int(&numdirtybuffers, 1);
1090 bd_wakeup((lodirtybuffers + hidirtybuffers) / 2);
1097 * Clear B_DELWRI for buffer.
1099 * Since the buffer is not on a queue, we do not update the numfreebuffers
1102 * The buffer must be on QUEUE_NONE.
1106 bundirty(struct buf *bp)
1109 CTR3(KTR_BUF, "bundirty(%p) vp %p flags %X", bp, bp->b_vp, bp->b_flags);
1110 KASSERT(bp->b_bufobj != NULL, ("No b_bufobj %p", bp));
1111 KASSERT(bp->b_flags & B_REMFREE || bp->b_qindex == QUEUE_NONE,
1112 ("bundirty: buffer %p still on queue %d", bp, bp->b_qindex));
1113 BUF_ASSERT_HELD(bp);
1115 if (bp->b_flags & B_DELWRI) {
1116 bp->b_flags &= ~B_DELWRI;
1118 atomic_subtract_int(&numdirtybuffers, 1);
1119 numdirtywakeup(lodirtybuffers);
1122 * Since it is now being written, we can clear its deferred write flag.
1124 bp->b_flags &= ~B_DEFERRED;
1130 * Asynchronous write. Start output on a buffer, but do not wait for
1131 * it to complete. The buffer is released when the output completes.
1133 * bwrite() ( or the VOP routine anyway ) is responsible for handling
1134 * B_INVAL buffers. Not us.
1137 bawrite(struct buf *bp)
1140 bp->b_flags |= B_ASYNC;
1147 * Called prior to the locking of any vnodes when we are expecting to
1148 * write. We do not want to starve the buffer cache with too many
1149 * dirty buffers so we block here. By blocking prior to the locking
1150 * of any vnodes we attempt to avoid the situation where a locked vnode
1151 * prevents the various system daemons from flushing related buffers.
1158 if (numdirtybuffers >= hidirtybuffers) {
1160 while (numdirtybuffers >= hidirtybuffers) {
1162 needsbuffer |= VFS_BIO_NEED_DIRTYFLUSH;
1163 msleep(&needsbuffer, &nblock,
1164 (PRIBIO + 4), "flswai", 0);
1166 mtx_unlock(&nblock);
1171 * Return true if we have too many dirty buffers.
1174 buf_dirty_count_severe(void)
1177 return(numdirtybuffers >= hidirtybuffers);
1180 static __noinline int
1181 buf_vm_page_count_severe(void)
1184 KFAIL_POINT_CODE(DEBUG_FP, buf_pressure, return 1);
1186 return vm_page_count_severe();
1192 * Release a busy buffer and, if requested, free its resources. The
1193 * buffer will be stashed in the appropriate bufqueue[] allowing it
1194 * to be accessed later as a cache entity or reused for other purposes.
1197 brelse(struct buf *bp)
1199 CTR3(KTR_BUF, "brelse(%p) vp %p flags %X",
1200 bp, bp->b_vp, bp->b_flags);
1201 KASSERT(!(bp->b_flags & (B_CLUSTER|B_PAGING)),
1202 ("brelse: inappropriate B_PAGING or B_CLUSTER bp %p", bp));
1204 if (bp->b_flags & B_MANAGED) {
1209 if (bp->b_iocmd == BIO_WRITE && (bp->b_ioflags & BIO_ERROR) &&
1210 bp->b_error == EIO && !(bp->b_flags & B_INVAL)) {
1212 * Failed write, redirty. Must clear BIO_ERROR to prevent
1213 * pages from being scrapped. If the error is anything
1214 * other than an I/O error (EIO), assume that retrying
1217 bp->b_ioflags &= ~BIO_ERROR;
1219 } else if ((bp->b_flags & (B_NOCACHE | B_INVAL)) ||
1220 (bp->b_ioflags & BIO_ERROR) || (bp->b_bufsize <= 0)) {
1222 * Either a failed I/O or we were asked to free or not
1225 bp->b_flags |= B_INVAL;
1226 if (!LIST_EMPTY(&bp->b_dep))
1228 if (bp->b_flags & B_DELWRI) {
1229 atomic_subtract_int(&numdirtybuffers, 1);
1230 numdirtywakeup(lodirtybuffers);
1232 bp->b_flags &= ~(B_DELWRI | B_CACHE);
1233 if ((bp->b_flags & B_VMIO) == 0) {
1242 * We must clear B_RELBUF if B_DELWRI is set. If vfs_vmio_release()
1243 * is called with B_DELWRI set, the underlying pages may wind up
1244 * getting freed causing a previous write (bdwrite()) to get 'lost'
1245 * because pages associated with a B_DELWRI bp are marked clean.
1247 * We still allow the B_INVAL case to call vfs_vmio_release(), even
1248 * if B_DELWRI is set.
1250 * If B_DELWRI is not set we may have to set B_RELBUF if we are low
1251 * on pages to return pages to the VM page queues.
1253 if (bp->b_flags & B_DELWRI)
1254 bp->b_flags &= ~B_RELBUF;
1255 else if (buf_vm_page_count_severe()) {
1257 * The locking of the BO_LOCK is not necessary since
1258 * BKGRDINPROG cannot be set while we hold the buf
1259 * lock, it can only be cleared if it is already
1263 if (!(bp->b_vflags & BV_BKGRDINPROG))
1264 bp->b_flags |= B_RELBUF;
1266 bp->b_flags |= B_RELBUF;
1270 * VMIO buffer rundown. It is not very necessary to keep a VMIO buffer
1271 * constituted, not even NFS buffers now. Two flags effect this. If
1272 * B_INVAL, the struct buf is invalidated but the VM object is kept
1273 * around ( i.e. so it is trivial to reconstitute the buffer later ).
1275 * If BIO_ERROR or B_NOCACHE is set, pages in the VM object will be
1276 * invalidated. BIO_ERROR cannot be set for a failed write unless the
1277 * buffer is also B_INVAL because it hits the re-dirtying code above.
1279 * Normally we can do this whether a buffer is B_DELWRI or not. If
1280 * the buffer is an NFS buffer, it is tracking piecemeal writes or
1281 * the commit state and we cannot afford to lose the buffer. If the
1282 * buffer has a background write in progress, we need to keep it
1283 * around to prevent it from being reconstituted and starting a second
1286 if ((bp->b_flags & B_VMIO)
1287 && !(bp->b_vp->v_mount != NULL &&
1288 (bp->b_vp->v_mount->mnt_vfc->vfc_flags & VFCF_NETWORK) != 0 &&
1289 !vn_isdisk(bp->b_vp, NULL) &&
1290 (bp->b_flags & B_DELWRI))
1299 obj = bp->b_bufobj->bo_object;
1302 * Get the base offset and length of the buffer. Note that
1303 * in the VMIO case if the buffer block size is not
1304 * page-aligned then b_data pointer may not be page-aligned.
1305 * But our b_pages[] array *IS* page aligned.
1307 * block sizes less then DEV_BSIZE (usually 512) are not
1308 * supported due to the page granularity bits (m->valid,
1309 * m->dirty, etc...).
1311 * See man buf(9) for more information
1313 resid = bp->b_bufsize;
1314 foff = bp->b_offset;
1315 VM_OBJECT_LOCK(obj);
1316 for (i = 0; i < bp->b_npages; i++) {
1322 * If we hit a bogus page, fixup *all* the bogus pages
1325 if (m == bogus_page) {
1326 poff = OFF_TO_IDX(bp->b_offset);
1329 for (j = i; j < bp->b_npages; j++) {
1331 mtmp = bp->b_pages[j];
1332 if (mtmp == bogus_page) {
1333 mtmp = vm_page_lookup(obj, poff + j);
1335 panic("brelse: page missing\n");
1337 bp->b_pages[j] = mtmp;
1341 if ((bp->b_flags & B_INVAL) == 0) {
1343 trunc_page((vm_offset_t)bp->b_data),
1344 bp->b_pages, bp->b_npages);
1348 if ((bp->b_flags & B_NOCACHE) ||
1349 (bp->b_ioflags & BIO_ERROR &&
1350 bp->b_iocmd == BIO_READ)) {
1351 int poffset = foff & PAGE_MASK;
1352 int presid = resid > (PAGE_SIZE - poffset) ?
1353 (PAGE_SIZE - poffset) : resid;
1355 KASSERT(presid >= 0, ("brelse: extra page"));
1356 vm_page_set_invalid(m, poffset, presid);
1358 printf("avoided corruption bug in bogus_page/brelse code\n");
1360 resid -= PAGE_SIZE - (foff & PAGE_MASK);
1361 foff = (foff + PAGE_SIZE) & ~(off_t)PAGE_MASK;
1363 VM_OBJECT_UNLOCK(obj);
1364 if (bp->b_flags & (B_INVAL | B_RELBUF))
1365 vfs_vmio_release(bp);
1367 } else if (bp->b_flags & B_VMIO) {
1369 if (bp->b_flags & (B_INVAL | B_RELBUF)) {
1370 vfs_vmio_release(bp);
1373 } else if ((bp->b_flags & (B_INVAL | B_RELBUF)) != 0) {
1374 if (bp->b_bufsize != 0)
1376 if (bp->b_vp != NULL)
1380 if (BUF_LOCKRECURSED(bp)) {
1381 /* do not release to free list */
1388 /* Handle delayed bremfree() processing. */
1389 if (bp->b_flags & B_REMFREE)
1391 if (bp->b_qindex != QUEUE_NONE)
1392 panic("brelse: free buffer onto another queue???");
1395 * If the buffer has junk contents signal it and eventually
1396 * clean up B_DELWRI and diassociate the vnode so that gbincore()
1399 if (bp->b_bufsize == 0 || (bp->b_ioflags & BIO_ERROR) != 0 ||
1400 (bp->b_flags & (B_INVAL | B_NOCACHE | B_RELBUF)) != 0)
1401 bp->b_flags |= B_INVAL;
1402 if (bp->b_flags & B_INVAL) {
1403 if (bp->b_flags & B_DELWRI)
1409 /* buffers with no memory */
1410 if (bp->b_bufsize == 0) {
1411 bp->b_xflags &= ~(BX_BKGRDWRITE | BX_ALTDATA);
1412 if (bp->b_vflags & BV_BKGRDINPROG)
1413 panic("losing buffer 1");
1414 if (bp->b_kvasize) {
1415 bp->b_qindex = QUEUE_EMPTYKVA;
1417 bp->b_qindex = QUEUE_EMPTY;
1419 TAILQ_INSERT_HEAD(&bufqueues[bp->b_qindex], bp, b_freelist);
1420 /* buffers with junk contents */
1421 } else if (bp->b_flags & (B_INVAL | B_NOCACHE | B_RELBUF) ||
1422 (bp->b_ioflags & BIO_ERROR)) {
1423 bp->b_xflags &= ~(BX_BKGRDWRITE | BX_ALTDATA);
1424 if (bp->b_vflags & BV_BKGRDINPROG)
1425 panic("losing buffer 2");
1426 bp->b_qindex = QUEUE_CLEAN;
1427 TAILQ_INSERT_HEAD(&bufqueues[QUEUE_CLEAN], bp, b_freelist);
1428 /* remaining buffers */
1430 if ((bp->b_flags & (B_DELWRI|B_NEEDSGIANT)) ==
1431 (B_DELWRI|B_NEEDSGIANT))
1432 bp->b_qindex = QUEUE_DIRTY_GIANT;
1433 else if (bp->b_flags & B_DELWRI)
1434 bp->b_qindex = QUEUE_DIRTY;
1436 bp->b_qindex = QUEUE_CLEAN;
1437 if (bp->b_flags & B_AGE)
1438 TAILQ_INSERT_HEAD(&bufqueues[bp->b_qindex], bp, b_freelist);
1440 TAILQ_INSERT_TAIL(&bufqueues[bp->b_qindex], bp, b_freelist);
1442 mtx_unlock(&bqlock);
1445 * Fixup numfreebuffers count. The bp is on an appropriate queue
1446 * unless locked. We then bump numfreebuffers if it is not B_DELWRI.
1447 * We've already handled the B_INVAL case ( B_DELWRI will be clear
1448 * if B_INVAL is set ).
1451 if (!(bp->b_flags & B_DELWRI))
1455 * Something we can maybe free or reuse
1457 if (bp->b_bufsize || bp->b_kvasize)
1460 bp->b_flags &= ~(B_ASYNC | B_NOCACHE | B_AGE | B_RELBUF | B_DIRECT);
1461 if ((bp->b_flags & B_DELWRI) == 0 && (bp->b_xflags & BX_VNDIRTY))
1462 panic("brelse: not dirty");
1468 * Release a buffer back to the appropriate queue but do not try to free
1469 * it. The buffer is expected to be used again soon.
1471 * bqrelse() is used by bdwrite() to requeue a delayed write, and used by
1472 * biodone() to requeue an async I/O on completion. It is also used when
1473 * known good buffers need to be requeued but we think we may need the data
1476 * XXX we should be able to leave the B_RELBUF hint set on completion.
1479 bqrelse(struct buf *bp)
1481 CTR3(KTR_BUF, "bqrelse(%p) vp %p flags %X", bp, bp->b_vp, bp->b_flags);
1482 KASSERT(!(bp->b_flags & (B_CLUSTER|B_PAGING)),
1483 ("bqrelse: inappropriate B_PAGING or B_CLUSTER bp %p", bp));
1485 if (BUF_LOCKRECURSED(bp)) {
1486 /* do not release to free list */
1491 if (bp->b_flags & B_MANAGED) {
1492 if (bp->b_flags & B_REMFREE) {
1495 mtx_unlock(&bqlock);
1497 bp->b_flags &= ~(B_ASYNC | B_NOCACHE | B_AGE | B_RELBUF);
1503 /* Handle delayed bremfree() processing. */
1504 if (bp->b_flags & B_REMFREE)
1506 if (bp->b_qindex != QUEUE_NONE)
1507 panic("bqrelse: free buffer onto another queue???");
1508 /* buffers with stale but valid contents */
1509 if (bp->b_flags & B_DELWRI) {
1510 if (bp->b_flags & B_NEEDSGIANT)
1511 bp->b_qindex = QUEUE_DIRTY_GIANT;
1513 bp->b_qindex = QUEUE_DIRTY;
1514 TAILQ_INSERT_TAIL(&bufqueues[bp->b_qindex], bp, b_freelist);
1517 * The locking of the BO_LOCK for checking of the
1518 * BV_BKGRDINPROG is not necessary since the
1519 * BV_BKGRDINPROG cannot be set while we hold the buf
1520 * lock, it can only be cleared if it is already
1523 if (!buf_vm_page_count_severe() || (bp->b_vflags & BV_BKGRDINPROG)) {
1524 bp->b_qindex = QUEUE_CLEAN;
1525 TAILQ_INSERT_TAIL(&bufqueues[QUEUE_CLEAN], bp,
1529 * We are too low on memory, we have to try to free
1530 * the buffer (most importantly: the wired pages
1531 * making up its backing store) *now*.
1533 mtx_unlock(&bqlock);
1538 mtx_unlock(&bqlock);
1540 if ((bp->b_flags & B_INVAL) || !(bp->b_flags & B_DELWRI))
1544 * Something we can maybe free or reuse.
1546 if (bp->b_bufsize && !(bp->b_flags & B_DELWRI))
1549 bp->b_flags &= ~(B_ASYNC | B_NOCACHE | B_AGE | B_RELBUF);
1550 if ((bp->b_flags & B_DELWRI) == 0 && (bp->b_xflags & BX_VNDIRTY))
1551 panic("bqrelse: not dirty");
1556 /* Give pages used by the bp back to the VM system (where possible) */
1558 vfs_vmio_release(struct buf *bp)
1563 VM_OBJECT_LOCK(bp->b_bufobj->bo_object);
1564 for (i = 0; i < bp->b_npages; i++) {
1566 bp->b_pages[i] = NULL;
1568 * In order to keep page LRU ordering consistent, put
1569 * everything on the inactive queue.
1572 vm_page_unwire(m, 0);
1574 * We don't mess with busy pages, it is
1575 * the responsibility of the process that
1576 * busied the pages to deal with them.
1578 if ((m->oflags & VPO_BUSY) == 0 && m->busy == 0 &&
1579 m->wire_count == 0) {
1581 * Might as well free the page if we can and it has
1582 * no valid data. We also free the page if the
1583 * buffer was used for direct I/O
1585 if ((bp->b_flags & B_ASYNC) == 0 && !m->valid &&
1586 m->hold_count == 0) {
1588 } else if (bp->b_flags & B_DIRECT) {
1589 vm_page_try_to_free(m);
1590 } else if (buf_vm_page_count_severe()) {
1591 vm_page_try_to_cache(m);
1596 VM_OBJECT_UNLOCK(bp->b_bufobj->bo_object);
1597 pmap_qremove(trunc_page((vm_offset_t) bp->b_data), bp->b_npages);
1599 if (bp->b_bufsize) {
1604 bp->b_flags &= ~B_VMIO;
1610 * Check to see if a block at a particular lbn is available for a clustered
1614 vfs_bio_clcheck(struct vnode *vp, int size, daddr_t lblkno, daddr_t blkno)
1621 /* If the buf isn't in core skip it */
1622 if ((bpa = gbincore(&vp->v_bufobj, lblkno)) == NULL)
1625 /* If the buf is busy we don't want to wait for it */
1626 if (BUF_LOCK(bpa, LK_EXCLUSIVE | LK_NOWAIT, NULL) != 0)
1629 /* Only cluster with valid clusterable delayed write buffers */
1630 if ((bpa->b_flags & (B_DELWRI | B_CLUSTEROK | B_INVAL)) !=
1631 (B_DELWRI | B_CLUSTEROK))
1634 if (bpa->b_bufsize != size)
1638 * Check to see if it is in the expected place on disk and that the
1639 * block has been mapped.
1641 if ((bpa->b_blkno != bpa->b_lblkno) && (bpa->b_blkno == blkno))
1651 * Implement clustered async writes for clearing out B_DELWRI buffers.
1652 * This is much better then the old way of writing only one buffer at
1653 * a time. Note that we may not be presented with the buffers in the
1654 * correct order, so we search for the cluster in both directions.
1657 vfs_bio_awrite(struct buf *bp)
1662 daddr_t lblkno = bp->b_lblkno;
1663 struct vnode *vp = bp->b_vp;
1671 * right now we support clustered writing only to regular files. If
1672 * we find a clusterable block we could be in the middle of a cluster
1673 * rather then at the beginning.
1675 if ((vp->v_type == VREG) &&
1676 (vp->v_mount != 0) && /* Only on nodes that have the size info */
1677 (bp->b_flags & (B_CLUSTEROK | B_INVAL)) == B_CLUSTEROK) {
1679 size = vp->v_mount->mnt_stat.f_iosize;
1680 maxcl = MAXPHYS / size;
1683 for (i = 1; i < maxcl; i++)
1684 if (vfs_bio_clcheck(vp, size, lblkno + i,
1685 bp->b_blkno + ((i * size) >> DEV_BSHIFT)) == 0)
1688 for (j = 1; i + j <= maxcl && j <= lblkno; j++)
1689 if (vfs_bio_clcheck(vp, size, lblkno - j,
1690 bp->b_blkno - ((j * size) >> DEV_BSHIFT)) == 0)
1696 * this is a possible cluster write
1700 nwritten = cluster_wbuild(vp, size, lblkno - j, ncl);
1705 bp->b_flags |= B_ASYNC;
1707 * default (old) behavior, writing out only one block
1709 * XXX returns b_bufsize instead of b_bcount for nwritten?
1711 nwritten = bp->b_bufsize;
1720 * Find and initialize a new buffer header, freeing up existing buffers
1721 * in the bufqueues as necessary. The new buffer is returned locked.
1723 * Important: B_INVAL is not set. If the caller wishes to throw the
1724 * buffer away, the caller must set B_INVAL prior to calling brelse().
1727 * We have insufficient buffer headers
1728 * We have insufficient buffer space
1729 * buffer_map is too fragmented ( space reservation fails )
1730 * If we have to flush dirty buffers ( but we try to avoid this )
1732 * To avoid VFS layer recursion we do not flush dirty buffers ourselves.
1733 * Instead we ask the buf daemon to do it for us. We attempt to
1734 * avoid piecemeal wakeups of the pageout daemon.
1738 getnewbuf(struct vnode *vp, int slpflag, int slptimeo, int size, int maxsize,
1746 static int flushingbufs;
1750 * We can't afford to block since we might be holding a vnode lock,
1751 * which may prevent system daemons from running. We deal with
1752 * low-memory situations by proactively returning memory and running
1753 * async I/O rather then sync I/O.
1755 atomic_add_int(&getnewbufcalls, 1);
1756 atomic_subtract_int(&getnewbufrestarts, 1);
1758 atomic_add_int(&getnewbufrestarts, 1);
1761 * Setup for scan. If we do not have enough free buffers,
1762 * we setup a degenerate case that immediately fails. Note
1763 * that if we are specially marked process, we are allowed to
1764 * dip into our reserves.
1766 * The scanning sequence is nominally: EMPTY->EMPTYKVA->CLEAN
1768 * We start with EMPTYKVA. If the list is empty we backup to EMPTY.
1769 * However, there are a number of cases (defragging, reusing, ...)
1770 * where we cannot backup.
1773 nqindex = QUEUE_EMPTYKVA;
1774 nbp = TAILQ_FIRST(&bufqueues[QUEUE_EMPTYKVA]);
1778 * If no EMPTYKVA buffers and we are either
1779 * defragging or reusing, locate a CLEAN buffer
1780 * to free or reuse. If bufspace useage is low
1781 * skip this step so we can allocate a new buffer.
1783 if (defrag || bufspace >= lobufspace) {
1784 nqindex = QUEUE_CLEAN;
1785 nbp = TAILQ_FIRST(&bufqueues[QUEUE_CLEAN]);
1789 * If we could not find or were not allowed to reuse a
1790 * CLEAN buffer, check to see if it is ok to use an EMPTY
1791 * buffer. We can only use an EMPTY buffer if allocating
1792 * its KVA would not otherwise run us out of buffer space.
1794 if (nbp == NULL && defrag == 0 &&
1795 bufspace + maxsize < hibufspace) {
1796 nqindex = QUEUE_EMPTY;
1797 nbp = TAILQ_FIRST(&bufqueues[QUEUE_EMPTY]);
1802 * Run scan, possibly freeing data and/or kva mappings on the fly
1806 while ((bp = nbp) != NULL) {
1807 int qindex = nqindex;
1810 * Calculate next bp ( we can only use it if we do not block
1811 * or do other fancy things ).
1813 if ((nbp = TAILQ_NEXT(bp, b_freelist)) == NULL) {
1816 nqindex = QUEUE_EMPTYKVA;
1817 if ((nbp = TAILQ_FIRST(&bufqueues[QUEUE_EMPTYKVA])))
1820 case QUEUE_EMPTYKVA:
1821 nqindex = QUEUE_CLEAN;
1822 if ((nbp = TAILQ_FIRST(&bufqueues[QUEUE_CLEAN])))
1833 * If we are defragging then we need a buffer with
1834 * b_kvasize != 0. XXX this situation should no longer
1835 * occur, if defrag is non-zero the buffer's b_kvasize
1836 * should also be non-zero at this point. XXX
1838 if (defrag && bp->b_kvasize == 0) {
1839 printf("Warning: defrag empty buffer %p\n", bp);
1844 * Start freeing the bp. This is somewhat involved. nbp
1845 * remains valid only for QUEUE_EMPTY[KVA] bp's.
1847 if (BUF_LOCK(bp, LK_EXCLUSIVE | LK_NOWAIT, NULL) != 0)
1850 BO_LOCK(bp->b_bufobj);
1851 if (bp->b_vflags & BV_BKGRDINPROG) {
1852 BO_UNLOCK(bp->b_bufobj);
1856 BO_UNLOCK(bp->b_bufobj);
1859 "getnewbuf(%p) vp %p flags %X kvasize %d bufsize %d "
1860 "queue %d (recycling)", bp, bp->b_vp, bp->b_flags,
1861 bp->b_kvasize, bp->b_bufsize, qindex);
1866 KASSERT(bp->b_qindex == qindex, ("getnewbuf: inconsistant queue %d bp %p", qindex, bp));
1869 * Note: we no longer distinguish between VMIO and non-VMIO
1873 KASSERT((bp->b_flags & B_DELWRI) == 0, ("delwri buffer %p found in queue %d", bp, qindex));
1876 mtx_unlock(&bqlock);
1878 if (qindex == QUEUE_CLEAN) {
1879 if (bp->b_flags & B_VMIO) {
1880 bp->b_flags &= ~B_ASYNC;
1881 vfs_vmio_release(bp);
1888 * NOTE: nbp is now entirely invalid. We can only restart
1889 * the scan from this point on.
1891 * Get the rest of the buffer freed up. b_kva* is still
1892 * valid after this operation.
1895 if (bp->b_rcred != NOCRED) {
1896 crfree(bp->b_rcred);
1897 bp->b_rcred = NOCRED;
1899 if (bp->b_wcred != NOCRED) {
1900 crfree(bp->b_wcred);
1901 bp->b_wcred = NOCRED;
1903 if (!LIST_EMPTY(&bp->b_dep))
1905 if (bp->b_vflags & BV_BKGRDINPROG)
1906 panic("losing buffer 3");
1907 KASSERT(bp->b_vp == NULL,
1908 ("bp: %p still has vnode %p. qindex: %d",
1909 bp, bp->b_vp, qindex));
1910 KASSERT((bp->b_xflags & (BX_VNCLEAN|BX_VNDIRTY)) == 0,
1911 ("bp: %p still on a buffer list. xflags %X",
1922 bp->b_blkno = bp->b_lblkno = 0;
1923 bp->b_offset = NOOFFSET;
1929 bp->b_dirtyoff = bp->b_dirtyend = 0;
1930 bp->b_bufobj = NULL;
1931 bp->b_pin_count = 0;
1932 bp->b_fsprivate1 = NULL;
1933 bp->b_fsprivate2 = NULL;
1934 bp->b_fsprivate3 = NULL;
1936 LIST_INIT(&bp->b_dep);
1939 * If we are defragging then free the buffer.
1942 bp->b_flags |= B_INVAL;
1950 * Notify any waiters for the buffer lock about
1951 * identity change by freeing the buffer.
1953 if (qindex == QUEUE_CLEAN && BUF_LOCKWAITERS(bp)) {
1954 bp->b_flags |= B_INVAL;
1961 * If we are overcomitted then recover the buffer and its
1962 * KVM space. This occurs in rare situations when multiple
1963 * processes are blocked in getnewbuf() or allocbuf().
1965 if (bufspace >= hibufspace)
1967 if (flushingbufs && bp->b_kvasize != 0) {
1968 bp->b_flags |= B_INVAL;
1973 if (bufspace < lobufspace)
1979 * If we exhausted our list, sleep as appropriate. We may have to
1980 * wakeup various daemons and write out some dirty buffers.
1982 * Generally we are sleeping due to insufficient buffer space.
1986 int flags, norunbuf;
1991 flags = VFS_BIO_NEED_BUFSPACE;
1993 } else if (bufspace >= hibufspace) {
1995 flags = VFS_BIO_NEED_BUFSPACE;
1998 flags = VFS_BIO_NEED_ANY;
2001 needsbuffer |= flags;
2002 mtx_unlock(&nblock);
2003 mtx_unlock(&bqlock);
2005 bd_speedup(); /* heeeelp */
2006 if (gbflags & GB_NOWAIT_BD)
2010 while (needsbuffer & flags) {
2011 if (vp != NULL && (td->td_pflags & TDP_BUFNEED) == 0) {
2012 mtx_unlock(&nblock);
2014 * getblk() is called with a vnode
2015 * locked, and some majority of the
2016 * dirty buffers may as well belong to
2017 * the vnode. Flushing the buffers
2018 * there would make a progress that
2019 * cannot be achieved by the
2020 * buf_daemon, that cannot lock the
2023 norunbuf = ~(TDP_BUFNEED | TDP_NORUNNINGBUF) |
2024 (td->td_pflags & TDP_NORUNNINGBUF);
2025 /* play bufdaemon */
2026 td->td_pflags |= TDP_BUFNEED | TDP_NORUNNINGBUF;
2027 fl = buf_do_flush(vp);
2028 td->td_pflags &= norunbuf;
2032 if ((needsbuffer & flags) == 0)
2035 if (msleep(&needsbuffer, &nblock,
2036 (PRIBIO + 4) | slpflag, waitmsg, slptimeo)) {
2037 mtx_unlock(&nblock);
2041 mtx_unlock(&nblock);
2044 * We finally have a valid bp. We aren't quite out of the
2045 * woods, we still have to reserve kva space. In order
2046 * to keep fragmentation sane we only allocate kva in
2049 maxsize = (maxsize + BKVAMASK) & ~BKVAMASK;
2051 if (maxsize != bp->b_kvasize) {
2052 vm_offset_t addr = 0;
2056 vm_map_lock(buffer_map);
2057 if (vm_map_findspace(buffer_map,
2058 vm_map_min(buffer_map), maxsize, &addr)) {
2060 * Uh oh. Buffer map is to fragmented. We
2061 * must defragment the map.
2063 atomic_add_int(&bufdefragcnt, 1);
2064 vm_map_unlock(buffer_map);
2066 bp->b_flags |= B_INVAL;
2071 vm_map_insert(buffer_map, NULL, 0,
2072 addr, addr + maxsize,
2073 VM_PROT_ALL, VM_PROT_ALL, MAP_NOFAULT);
2075 bp->b_kvabase = (caddr_t) addr;
2076 bp->b_kvasize = maxsize;
2077 atomic_add_long(&bufspace, bp->b_kvasize);
2078 atomic_add_int(&bufreusecnt, 1);
2080 vm_map_unlock(buffer_map);
2082 bp->b_saveaddr = bp->b_kvabase;
2083 bp->b_data = bp->b_saveaddr;
2091 * buffer flushing daemon. Buffers are normally flushed by the
2092 * update daemon but if it cannot keep up this process starts to
2093 * take the load in an attempt to prevent getnewbuf() from blocking.
2096 static struct kproc_desc buf_kp = {
2101 SYSINIT(bufdaemon, SI_SUB_KTHREAD_BUF, SI_ORDER_FIRST, kproc_start, &buf_kp);
2104 buf_do_flush(struct vnode *vp)
2108 flushed = flushbufqueues(vp, QUEUE_DIRTY, 0);
2109 /* The list empty check here is slightly racy */
2110 if (!TAILQ_EMPTY(&bufqueues[QUEUE_DIRTY_GIANT])) {
2112 flushed += flushbufqueues(vp, QUEUE_DIRTY_GIANT, 0);
2117 * Could not find any buffers without rollback
2118 * dependencies, so just write the first one
2119 * in the hopes of eventually making progress.
2121 flushbufqueues(vp, QUEUE_DIRTY, 1);
2123 &bufqueues[QUEUE_DIRTY_GIANT])) {
2125 flushbufqueues(vp, QUEUE_DIRTY_GIANT, 1);
2138 * This process needs to be suspended prior to shutdown sync.
2140 EVENTHANDLER_REGISTER(shutdown_pre_sync, kproc_shutdown, bufdaemonproc,
2144 * This process is allowed to take the buffer cache to the limit
2146 curthread->td_pflags |= TDP_NORUNNINGBUF | TDP_BUFNEED;
2150 mtx_unlock(&bdlock);
2152 kproc_suspend_check(bufdaemonproc);
2153 lodirtysave = lodirtybuffers;
2154 if (bd_speedupreq) {
2155 lodirtybuffers = numdirtybuffers / 2;
2159 * Do the flush. Limit the amount of in-transit I/O we
2160 * allow to build up, otherwise we would completely saturate
2161 * the I/O system. Wakeup any waiting processes before we
2162 * normally would so they can run in parallel with our drain.
2164 while (numdirtybuffers > lodirtybuffers) {
2165 if (buf_do_flush(NULL) == 0)
2169 lodirtybuffers = lodirtysave;
2172 * Only clear bd_request if we have reached our low water
2173 * mark. The buf_daemon normally waits 1 second and
2174 * then incrementally flushes any dirty buffers that have
2175 * built up, within reason.
2177 * If we were unable to hit our low water mark and couldn't
2178 * find any flushable buffers, we sleep half a second.
2179 * Otherwise we loop immediately.
2182 if (numdirtybuffers <= lodirtybuffers) {
2184 * We reached our low water mark, reset the
2185 * request and sleep until we are needed again.
2186 * The sleep is just so the suspend code works.
2189 msleep(&bd_request, &bdlock, PVM, "psleep", hz);
2192 * We couldn't find any flushable dirty buffers but
2193 * still have too many dirty buffers, we
2194 * have to sleep and try again. (rare)
2196 msleep(&bd_request, &bdlock, PVM, "qsleep", hz / 10);
2204 * Try to flush a buffer in the dirty queue. We must be careful to
2205 * free up B_INVAL buffers instead of write them, which NFS is
2206 * particularly sensitive to.
2208 static int flushwithdeps = 0;
2209 SYSCTL_INT(_vfs, OID_AUTO, flushwithdeps, CTLFLAG_RW, &flushwithdeps,
2210 0, "Number of buffers flushed with dependecies that require rollbacks");
2213 flushbufqueues(struct vnode *lvp, int queue, int flushdeps)
2215 struct buf *sentinel;
2224 target = numdirtybuffers - lodirtybuffers;
2225 if (flushdeps && target > 2)
2228 target = flushbufqtarget;
2231 sentinel = malloc(sizeof(struct buf), M_TEMP, M_WAITOK | M_ZERO);
2232 sentinel->b_qindex = QUEUE_SENTINEL;
2234 TAILQ_INSERT_HEAD(&bufqueues[queue], sentinel, b_freelist);
2235 while (flushed != target) {
2236 bp = TAILQ_NEXT(sentinel, b_freelist);
2238 TAILQ_REMOVE(&bufqueues[queue], sentinel, b_freelist);
2239 TAILQ_INSERT_AFTER(&bufqueues[queue], bp, sentinel,
2244 * Skip sentinels inserted by other invocations of the
2245 * flushbufqueues(), taking care to not reorder them.
2247 if (bp->b_qindex == QUEUE_SENTINEL)
2250 * Only flush the buffers that belong to the
2251 * vnode locked by the curthread.
2253 if (lvp != NULL && bp->b_vp != lvp)
2255 if (BUF_LOCK(bp, LK_EXCLUSIVE | LK_NOWAIT, NULL) != 0)
2257 if (bp->b_pin_count > 0) {
2261 BO_LOCK(bp->b_bufobj);
2262 if ((bp->b_vflags & BV_BKGRDINPROG) != 0 ||
2263 (bp->b_flags & B_DELWRI) == 0) {
2264 BO_UNLOCK(bp->b_bufobj);
2268 BO_UNLOCK(bp->b_bufobj);
2269 if (bp->b_flags & B_INVAL) {
2271 mtx_unlock(&bqlock);
2274 numdirtywakeup((lodirtybuffers + hidirtybuffers) / 2);
2279 if (!LIST_EMPTY(&bp->b_dep) && buf_countdeps(bp, 0)) {
2280 if (flushdeps == 0) {
2288 * We must hold the lock on a vnode before writing
2289 * one of its buffers. Otherwise we may confuse, or
2290 * in the case of a snapshot vnode, deadlock the
2293 * The lock order here is the reverse of the normal
2294 * of vnode followed by buf lock. This is ok because
2295 * the NOWAIT will prevent deadlock.
2298 if (vn_start_write(vp, &mp, V_NOWAIT) != 0) {
2302 if (vn_lock(vp, LK_EXCLUSIVE | LK_NOWAIT | LK_CANRECURSE) == 0) {
2303 mtx_unlock(&bqlock);
2304 CTR3(KTR_BUF, "flushbufqueue(%p) vp %p flags %X",
2305 bp, bp->b_vp, bp->b_flags);
2306 if (curproc == bufdaemonproc)
2313 vn_finished_write(mp);
2315 flushwithdeps += hasdeps;
2319 * Sleeping on runningbufspace while holding
2320 * vnode lock leads to deadlock.
2322 if (curproc == bufdaemonproc)
2323 waitrunningbufspace();
2324 numdirtywakeup((lodirtybuffers + hidirtybuffers) / 2);
2328 vn_finished_write(mp);
2331 TAILQ_REMOVE(&bufqueues[queue], sentinel, b_freelist);
2332 mtx_unlock(&bqlock);
2333 free(sentinel, M_TEMP);
2338 * Check to see if a block is currently memory resident.
2341 incore(struct bufobj *bo, daddr_t blkno)
2346 bp = gbincore(bo, blkno);
2352 * Returns true if no I/O is needed to access the
2353 * associated VM object. This is like incore except
2354 * it also hunts around in the VM system for the data.
2358 inmem(struct vnode * vp, daddr_t blkno)
2361 vm_offset_t toff, tinc, size;
2365 ASSERT_VOP_LOCKED(vp, "inmem");
2367 if (incore(&vp->v_bufobj, blkno))
2369 if (vp->v_mount == NULL)
2376 if (size > vp->v_mount->mnt_stat.f_iosize)
2377 size = vp->v_mount->mnt_stat.f_iosize;
2378 off = (vm_ooffset_t)blkno * (vm_ooffset_t)vp->v_mount->mnt_stat.f_iosize;
2380 VM_OBJECT_LOCK(obj);
2381 for (toff = 0; toff < vp->v_mount->mnt_stat.f_iosize; toff += tinc) {
2382 m = vm_page_lookup(obj, OFF_TO_IDX(off + toff));
2386 if (tinc > PAGE_SIZE - ((toff + off) & PAGE_MASK))
2387 tinc = PAGE_SIZE - ((toff + off) & PAGE_MASK);
2388 if (vm_page_is_valid(m,
2389 (vm_offset_t) ((toff + off) & PAGE_MASK), tinc) == 0)
2392 VM_OBJECT_UNLOCK(obj);
2396 VM_OBJECT_UNLOCK(obj);
2403 * Sets the dirty range for a buffer based on the status of the dirty
2404 * bits in the pages comprising the buffer.
2406 * The range is limited to the size of the buffer.
2408 * This routine is primarily used by NFS, but is generalized for the
2412 vfs_setdirty(struct buf *bp)
2416 * Degenerate case - empty buffer
2418 if (bp->b_bufsize == 0)
2421 if ((bp->b_flags & B_VMIO) == 0)
2424 VM_OBJECT_LOCK(bp->b_bufobj->bo_object);
2425 vfs_setdirty_locked_object(bp);
2426 VM_OBJECT_UNLOCK(bp->b_bufobj->bo_object);
2430 vfs_setdirty_locked_object(struct buf *bp)
2435 object = bp->b_bufobj->bo_object;
2436 VM_OBJECT_LOCK_ASSERT(object, MA_OWNED);
2439 * We qualify the scan for modified pages on whether the
2440 * object has been flushed yet.
2442 if (object->flags & (OBJ_MIGHTBEDIRTY|OBJ_CLEANING)) {
2443 vm_offset_t boffset;
2444 vm_offset_t eoffset;
2447 * test the pages to see if they have been modified directly
2448 * by users through the VM system.
2450 for (i = 0; i < bp->b_npages; i++)
2451 vm_page_test_dirty(bp->b_pages[i]);
2454 * Calculate the encompassing dirty range, boffset and eoffset,
2455 * (eoffset - boffset) bytes.
2458 for (i = 0; i < bp->b_npages; i++) {
2459 if (bp->b_pages[i]->dirty)
2462 boffset = (i << PAGE_SHIFT) - (bp->b_offset & PAGE_MASK);
2464 for (i = bp->b_npages - 1; i >= 0; --i) {
2465 if (bp->b_pages[i]->dirty) {
2469 eoffset = ((i + 1) << PAGE_SHIFT) - (bp->b_offset & PAGE_MASK);
2472 * Fit it to the buffer.
2475 if (eoffset > bp->b_bcount)
2476 eoffset = bp->b_bcount;
2479 * If we have a good dirty range, merge with the existing
2483 if (boffset < eoffset) {
2484 if (bp->b_dirtyoff > boffset)
2485 bp->b_dirtyoff = boffset;
2486 if (bp->b_dirtyend < eoffset)
2487 bp->b_dirtyend = eoffset;
2495 * Get a block given a specified block and offset into a file/device.
2496 * The buffers B_DONE bit will be cleared on return, making it almost
2497 * ready for an I/O initiation. B_INVAL may or may not be set on
2498 * return. The caller should clear B_INVAL prior to initiating a
2501 * For a non-VMIO buffer, B_CACHE is set to the opposite of B_INVAL for
2502 * an existing buffer.
2504 * For a VMIO buffer, B_CACHE is modified according to the backing VM.
2505 * If getblk()ing a previously 0-sized invalid buffer, B_CACHE is set
2506 * and then cleared based on the backing VM. If the previous buffer is
2507 * non-0-sized but invalid, B_CACHE will be cleared.
2509 * If getblk() must create a new buffer, the new buffer is returned with
2510 * both B_INVAL and B_CACHE clear unless it is a VMIO buffer, in which
2511 * case it is returned with B_INVAL clear and B_CACHE set based on the
2514 * getblk() also forces a bwrite() for any B_DELWRI buffer whos
2515 * B_CACHE bit is clear.
2517 * What this means, basically, is that the caller should use B_CACHE to
2518 * determine whether the buffer is fully valid or not and should clear
2519 * B_INVAL prior to issuing a read. If the caller intends to validate
2520 * the buffer by loading its data area with something, the caller needs
2521 * to clear B_INVAL. If the caller does this without issuing an I/O,
2522 * the caller should set B_CACHE ( as an optimization ), else the caller
2523 * should issue the I/O and biodone() will set B_CACHE if the I/O was
2524 * a write attempt or if it was a successfull read. If the caller
2525 * intends to issue a READ, the caller must clear B_INVAL and BIO_ERROR
2526 * prior to issuing the READ. biodone() will *not* clear B_INVAL.
2529 getblk(struct vnode * vp, daddr_t blkno, int size, int slpflag, int slptimeo,
2536 CTR3(KTR_BUF, "getblk(%p, %ld, %d)", vp, (long)blkno, size);
2537 ASSERT_VOP_LOCKED(vp, "getblk");
2538 if (size > MAXBSIZE)
2539 panic("getblk: size(%d) > MAXBSIZE(%d)\n", size, MAXBSIZE);
2544 * Block if we are low on buffers. Certain processes are allowed
2545 * to completely exhaust the buffer cache.
2547 * If this check ever becomes a bottleneck it may be better to
2548 * move it into the else, when gbincore() fails. At the moment
2549 * it isn't a problem.
2551 * XXX remove if 0 sections (clean this up after its proven)
2553 if (numfreebuffers == 0) {
2554 if (TD_IS_IDLETHREAD(curthread))
2557 needsbuffer |= VFS_BIO_NEED_ANY;
2558 mtx_unlock(&nblock);
2562 bp = gbincore(bo, blkno);
2566 * Buffer is in-core. If the buffer is not busy, it must
2569 lockflags = LK_EXCLUSIVE | LK_SLEEPFAIL | LK_INTERLOCK;
2571 if (flags & GB_LOCK_NOWAIT)
2572 lockflags |= LK_NOWAIT;
2574 error = BUF_TIMELOCK(bp, lockflags,
2575 BO_MTX(bo), "getblk", slpflag, slptimeo);
2578 * If we slept and got the lock we have to restart in case
2579 * the buffer changed identities.
2581 if (error == ENOLCK)
2583 /* We timed out or were interrupted. */
2588 * The buffer is locked. B_CACHE is cleared if the buffer is
2589 * invalid. Otherwise, for a non-VMIO buffer, B_CACHE is set
2590 * and for a VMIO buffer B_CACHE is adjusted according to the
2593 if (bp->b_flags & B_INVAL)
2594 bp->b_flags &= ~B_CACHE;
2595 else if ((bp->b_flags & (B_VMIO | B_INVAL)) == 0)
2596 bp->b_flags |= B_CACHE;
2600 * check for size inconsistancies for non-VMIO case.
2603 if (bp->b_bcount != size) {
2604 if ((bp->b_flags & B_VMIO) == 0 ||
2605 (size > bp->b_kvasize)) {
2606 if (bp->b_flags & B_DELWRI) {
2608 * If buffer is pinned and caller does
2609 * not want sleep waiting for it to be
2610 * unpinned, bail out
2612 if (bp->b_pin_count > 0) {
2613 if (flags & GB_LOCK_NOWAIT) {
2620 bp->b_flags |= B_NOCACHE;
2623 if (LIST_EMPTY(&bp->b_dep)) {
2624 bp->b_flags |= B_RELBUF;
2627 bp->b_flags |= B_NOCACHE;
2636 * If the size is inconsistant in the VMIO case, we can resize
2637 * the buffer. This might lead to B_CACHE getting set or
2638 * cleared. If the size has not changed, B_CACHE remains
2639 * unchanged from its previous state.
2642 if (bp->b_bcount != size)
2645 KASSERT(bp->b_offset != NOOFFSET,
2646 ("getblk: no buffer offset"));
2649 * A buffer with B_DELWRI set and B_CACHE clear must
2650 * be committed before we can return the buffer in
2651 * order to prevent the caller from issuing a read
2652 * ( due to B_CACHE not being set ) and overwriting
2655 * Most callers, including NFS and FFS, need this to
2656 * operate properly either because they assume they
2657 * can issue a read if B_CACHE is not set, or because
2658 * ( for example ) an uncached B_DELWRI might loop due
2659 * to softupdates re-dirtying the buffer. In the latter
2660 * case, B_CACHE is set after the first write completes,
2661 * preventing further loops.
2662 * NOTE! b*write() sets B_CACHE. If we cleared B_CACHE
2663 * above while extending the buffer, we cannot allow the
2664 * buffer to remain with B_CACHE set after the write
2665 * completes or it will represent a corrupt state. To
2666 * deal with this we set B_NOCACHE to scrap the buffer
2669 * We might be able to do something fancy, like setting
2670 * B_CACHE in bwrite() except if B_DELWRI is already set,
2671 * so the below call doesn't set B_CACHE, but that gets real
2672 * confusing. This is much easier.
2675 if ((bp->b_flags & (B_CACHE|B_DELWRI)) == B_DELWRI) {
2676 bp->b_flags |= B_NOCACHE;
2680 bp->b_flags &= ~B_DONE;
2682 int bsize, maxsize, vmio;
2686 * Buffer is not in-core, create new buffer. The buffer
2687 * returned by getnewbuf() is locked. Note that the returned
2688 * buffer is also considered valid (not marked B_INVAL).
2692 * If the user does not want us to create the buffer, bail out
2695 if (flags & GB_NOCREAT)
2697 bsize = vn_isdisk(vp, NULL) ? DEV_BSIZE : bo->bo_bsize;
2698 offset = blkno * bsize;
2699 vmio = vp->v_object != NULL;
2700 maxsize = vmio ? size + (offset & PAGE_MASK) : size;
2701 maxsize = imax(maxsize, bsize);
2703 bp = getnewbuf(vp, slpflag, slptimeo, size, maxsize, flags);
2705 if (slpflag || slptimeo)
2711 * This code is used to make sure that a buffer is not
2712 * created while the getnewbuf routine is blocked.
2713 * This can be a problem whether the vnode is locked or not.
2714 * If the buffer is created out from under us, we have to
2715 * throw away the one we just created.
2717 * Note: this must occur before we associate the buffer
2718 * with the vp especially considering limitations in
2719 * the splay tree implementation when dealing with duplicate
2723 if (gbincore(bo, blkno)) {
2725 bp->b_flags |= B_INVAL;
2731 * Insert the buffer into the hash, so that it can
2732 * be found by incore.
2734 bp->b_blkno = bp->b_lblkno = blkno;
2735 bp->b_offset = offset;
2740 * set B_VMIO bit. allocbuf() the buffer bigger. Since the
2741 * buffer size starts out as 0, B_CACHE will be set by
2742 * allocbuf() for the VMIO case prior to it testing the
2743 * backing store for validity.
2747 bp->b_flags |= B_VMIO;
2748 #if defined(VFS_BIO_DEBUG)
2749 if (vn_canvmio(vp) != TRUE)
2750 printf("getblk: VMIO on vnode type %d\n",
2753 KASSERT(vp->v_object == bp->b_bufobj->bo_object,
2754 ("ARGH! different b_bufobj->bo_object %p %p %p\n",
2755 bp, vp->v_object, bp->b_bufobj->bo_object));
2757 bp->b_flags &= ~B_VMIO;
2758 KASSERT(bp->b_bufobj->bo_object == NULL,
2759 ("ARGH! has b_bufobj->bo_object %p %p\n",
2760 bp, bp->b_bufobj->bo_object));
2764 bp->b_flags &= ~B_DONE;
2766 CTR4(KTR_BUF, "getblk(%p, %ld, %d) = %p", vp, (long)blkno, size, bp);
2767 BUF_ASSERT_HELD(bp);
2768 KASSERT(bp->b_bufobj == bo,
2769 ("bp %p wrong b_bufobj %p should be %p", bp, bp->b_bufobj, bo));
2774 * Get an empty, disassociated buffer of given size. The buffer is initially
2778 geteblk(int size, int flags)
2783 maxsize = (size + BKVAMASK) & ~BKVAMASK;
2784 while ((bp = getnewbuf(NULL, 0, 0, size, maxsize, flags)) == NULL) {
2785 if ((flags & GB_NOWAIT_BD) &&
2786 (curthread->td_pflags & TDP_BUFNEED) != 0)
2790 bp->b_flags |= B_INVAL; /* b_dep cleared by getnewbuf() */
2791 BUF_ASSERT_HELD(bp);
2797 * This code constitutes the buffer memory from either anonymous system
2798 * memory (in the case of non-VMIO operations) or from an associated
2799 * VM object (in the case of VMIO operations). This code is able to
2800 * resize a buffer up or down.
2802 * Note that this code is tricky, and has many complications to resolve
2803 * deadlock or inconsistant data situations. Tread lightly!!!
2804 * There are B_CACHE and B_DELWRI interactions that must be dealt with by
2805 * the caller. Calling this code willy nilly can result in the loss of data.
2807 * allocbuf() only adjusts B_CACHE for VMIO buffers. getblk() deals with
2808 * B_CACHE for the non-VMIO case.
2812 allocbuf(struct buf *bp, int size)
2814 int newbsize, mbsize;
2817 BUF_ASSERT_HELD(bp);
2819 if (bp->b_kvasize < size)
2820 panic("allocbuf: buffer too small");
2822 if ((bp->b_flags & B_VMIO) == 0) {
2826 * Just get anonymous memory from the kernel. Don't
2827 * mess with B_CACHE.
2829 mbsize = (size + DEV_BSIZE - 1) & ~(DEV_BSIZE - 1);
2830 if (bp->b_flags & B_MALLOC)
2833 newbsize = round_page(size);
2835 if (newbsize < bp->b_bufsize) {
2837 * malloced buffers are not shrunk
2839 if (bp->b_flags & B_MALLOC) {
2841 bp->b_bcount = size;
2843 free(bp->b_data, M_BIOBUF);
2844 if (bp->b_bufsize) {
2845 atomic_subtract_long(
2851 bp->b_saveaddr = bp->b_kvabase;
2852 bp->b_data = bp->b_saveaddr;
2854 bp->b_flags &= ~B_MALLOC;
2860 (vm_offset_t) bp->b_data + newbsize,
2861 (vm_offset_t) bp->b_data + bp->b_bufsize);
2862 } else if (newbsize > bp->b_bufsize) {
2864 * We only use malloced memory on the first allocation.
2865 * and revert to page-allocated memory when the buffer
2869 * There is a potential smp race here that could lead
2870 * to bufmallocspace slightly passing the max. It
2871 * is probably extremely rare and not worth worrying
2874 if ( (bufmallocspace < maxbufmallocspace) &&
2875 (bp->b_bufsize == 0) &&
2876 (mbsize <= PAGE_SIZE/2)) {
2878 bp->b_data = malloc(mbsize, M_BIOBUF, M_WAITOK);
2879 bp->b_bufsize = mbsize;
2880 bp->b_bcount = size;
2881 bp->b_flags |= B_MALLOC;
2882 atomic_add_long(&bufmallocspace, mbsize);
2888 * If the buffer is growing on its other-than-first allocation,
2889 * then we revert to the page-allocation scheme.
2891 if (bp->b_flags & B_MALLOC) {
2892 origbuf = bp->b_data;
2893 origbufsize = bp->b_bufsize;
2894 bp->b_data = bp->b_kvabase;
2895 if (bp->b_bufsize) {
2896 atomic_subtract_long(&bufmallocspace,
2901 bp->b_flags &= ~B_MALLOC;
2902 newbsize = round_page(newbsize);
2906 (vm_offset_t) bp->b_data + bp->b_bufsize,
2907 (vm_offset_t) bp->b_data + newbsize);
2909 bcopy(origbuf, bp->b_data, origbufsize);
2910 free(origbuf, M_BIOBUF);
2916 newbsize = (size + DEV_BSIZE - 1) & ~(DEV_BSIZE - 1);
2917 desiredpages = (size == 0) ? 0 :
2918 num_pages((bp->b_offset & PAGE_MASK) + newbsize);
2920 if (bp->b_flags & B_MALLOC)
2921 panic("allocbuf: VMIO buffer can't be malloced");
2923 * Set B_CACHE initially if buffer is 0 length or will become
2926 if (size == 0 || bp->b_bufsize == 0)
2927 bp->b_flags |= B_CACHE;
2929 if (newbsize < bp->b_bufsize) {
2931 * DEV_BSIZE aligned new buffer size is less then the
2932 * DEV_BSIZE aligned existing buffer size. Figure out
2933 * if we have to remove any pages.
2935 if (desiredpages < bp->b_npages) {
2938 VM_OBJECT_LOCK(bp->b_bufobj->bo_object);
2939 for (i = desiredpages; i < bp->b_npages; i++) {
2941 * the page is not freed here -- it
2942 * is the responsibility of
2943 * vnode_pager_setsize
2946 KASSERT(m != bogus_page,
2947 ("allocbuf: bogus page found"));
2948 while (vm_page_sleep_if_busy(m, TRUE,
2952 bp->b_pages[i] = NULL;
2954 vm_page_unwire(m, 0);
2957 VM_OBJECT_UNLOCK(bp->b_bufobj->bo_object);
2958 pmap_qremove((vm_offset_t) trunc_page((vm_offset_t)bp->b_data) +
2959 (desiredpages << PAGE_SHIFT), (bp->b_npages - desiredpages));
2960 bp->b_npages = desiredpages;
2962 } else if (size > bp->b_bcount) {
2964 * We are growing the buffer, possibly in a
2965 * byte-granular fashion.
2972 * Step 1, bring in the VM pages from the object,
2973 * allocating them if necessary. We must clear
2974 * B_CACHE if these pages are not valid for the
2975 * range covered by the buffer.
2978 obj = bp->b_bufobj->bo_object;
2980 VM_OBJECT_LOCK(obj);
2981 while (bp->b_npages < desiredpages) {
2985 pi = OFF_TO_IDX(bp->b_offset) + bp->b_npages;
2986 if ((m = vm_page_lookup(obj, pi)) == NULL) {
2988 * note: must allocate system pages
2989 * since blocking here could intefere
2990 * with paging I/O, no matter which
2993 m = vm_page_alloc(obj, pi,
2994 VM_ALLOC_NOBUSY | VM_ALLOC_SYSTEM |
2997 atomic_add_int(&vm_pageout_deficit,
2998 desiredpages - bp->b_npages);
2999 VM_OBJECT_UNLOCK(obj);
3001 VM_OBJECT_LOCK(obj);
3004 bp->b_flags &= ~B_CACHE;
3005 bp->b_pages[bp->b_npages] = m;
3012 * We found a page. If we have to sleep on it,
3013 * retry because it might have gotten freed out
3016 * We can only test VPO_BUSY here. Blocking on
3017 * m->busy might lead to a deadlock:
3019 * vm_fault->getpages->cluster_read->allocbuf
3022 if ((m->oflags & VPO_BUSY) != 0) {
3024 * Reference the page before unlocking
3025 * and sleeping so that the page daemon
3026 * is less likely to reclaim it.
3028 vm_page_lock_queues();
3029 vm_page_flag_set(m, PG_REFERENCED);
3030 vm_page_sleep(m, "pgtblk");
3035 * We have a good page.
3040 bp->b_pages[bp->b_npages] = m;
3045 * Step 2. We've loaded the pages into the buffer,
3046 * we have to figure out if we can still have B_CACHE
3047 * set. Note that B_CACHE is set according to the
3048 * byte-granular range ( bcount and size ), new the
3049 * aligned range ( newbsize ).
3051 * The VM test is against m->valid, which is DEV_BSIZE
3052 * aligned. Needless to say, the validity of the data
3053 * needs to also be DEV_BSIZE aligned. Note that this
3054 * fails with NFS if the server or some other client
3055 * extends the file's EOF. If our buffer is resized,
3056 * B_CACHE may remain set! XXX
3059 toff = bp->b_bcount;
3060 tinc = PAGE_SIZE - ((bp->b_offset + toff) & PAGE_MASK);
3062 while ((bp->b_flags & B_CACHE) && toff < size) {
3065 if (tinc > (size - toff))
3068 pi = ((bp->b_offset & PAGE_MASK) + toff) >>
3081 VM_OBJECT_UNLOCK(obj);
3084 * Step 3, fixup the KVM pmap. Remember that
3085 * bp->b_data is relative to bp->b_offset, but
3086 * bp->b_offset may be offset into the first page.
3089 bp->b_data = (caddr_t)
3090 trunc_page((vm_offset_t)bp->b_data);
3092 (vm_offset_t)bp->b_data,
3097 bp->b_data = (caddr_t)((vm_offset_t)bp->b_data |
3098 (vm_offset_t)(bp->b_offset & PAGE_MASK));
3101 if (newbsize < bp->b_bufsize)
3103 bp->b_bufsize = newbsize; /* actual buffer allocation */
3104 bp->b_bcount = size; /* requested buffer size */
3109 biodone(struct bio *bp)
3112 void (*done)(struct bio *);
3114 mtxp = mtx_pool_find(mtxpool_sleep, bp);
3116 bp->bio_flags |= BIO_DONE;
3117 done = bp->bio_done;
3126 * Wait for a BIO to finish.
3128 * XXX: resort to a timeout for now. The optimal locking (if any) for this
3129 * case is not yet clear.
3132 biowait(struct bio *bp, const char *wchan)
3136 mtxp = mtx_pool_find(mtxpool_sleep, bp);
3138 while ((bp->bio_flags & BIO_DONE) == 0)
3139 msleep(bp, mtxp, PRIBIO, wchan, hz / 10);
3141 if (bp->bio_error != 0)
3142 return (bp->bio_error);
3143 if (!(bp->bio_flags & BIO_ERROR))
3149 biofinish(struct bio *bp, struct devstat *stat, int error)
3153 bp->bio_error = error;
3154 bp->bio_flags |= BIO_ERROR;
3157 devstat_end_transaction_bio(stat, bp);
3164 * Wait for buffer I/O completion, returning error status. The buffer
3165 * is left locked and B_DONE on return. B_EINTR is converted into an EINTR
3166 * error and cleared.
3169 bufwait(struct buf *bp)
3171 if (bp->b_iocmd == BIO_READ)
3172 bwait(bp, PRIBIO, "biord");
3174 bwait(bp, PRIBIO, "biowr");
3175 if (bp->b_flags & B_EINTR) {
3176 bp->b_flags &= ~B_EINTR;
3179 if (bp->b_ioflags & BIO_ERROR) {
3180 return (bp->b_error ? bp->b_error : EIO);
3187 * Call back function from struct bio back up to struct buf.
3190 bufdonebio(struct bio *bip)
3194 bp = bip->bio_caller2;
3195 bp->b_resid = bp->b_bcount - bip->bio_completed;
3196 bp->b_resid = bip->bio_resid; /* XXX: remove */
3197 bp->b_ioflags = bip->bio_flags;
3198 bp->b_error = bip->bio_error;
3200 bp->b_ioflags |= BIO_ERROR;
3206 dev_strategy(struct cdev *dev, struct buf *bp)
3211 if ((!bp->b_iocmd) || (bp->b_iocmd & (bp->b_iocmd - 1)))
3212 panic("b_iocmd botch");
3217 /* Try again later */
3218 tsleep(&bp, PRIBIO, "dev_strat", hz/10);
3220 bip->bio_cmd = bp->b_iocmd;
3221 bip->bio_offset = bp->b_iooffset;
3222 bip->bio_length = bp->b_bcount;
3223 bip->bio_bcount = bp->b_bcount; /* XXX: remove */
3224 bip->bio_data = bp->b_data;
3225 bip->bio_done = bufdonebio;
3226 bip->bio_caller2 = bp;
3228 KASSERT(dev->si_refcount > 0,
3229 ("dev_strategy on un-referenced struct cdev *(%s)",
3231 csw = dev_refthread(dev);
3234 bp->b_error = ENXIO;
3235 bp->b_ioflags = BIO_ERROR;
3239 (*csw->d_strategy)(bip);
3246 * Finish I/O on a buffer, optionally calling a completion function.
3247 * This is usually called from an interrupt so process blocking is
3250 * biodone is also responsible for setting B_CACHE in a B_VMIO bp.
3251 * In a non-VMIO bp, B_CACHE will be set on the next getblk()
3252 * assuming B_INVAL is clear.
3254 * For the VMIO case, we set B_CACHE if the op was a read and no
3255 * read error occured, or if the op was a write. B_CACHE is never
3256 * set if the buffer is invalid or otherwise uncacheable.
3258 * biodone does not mess with B_INVAL, allowing the I/O routine or the
3259 * initiator to leave B_INVAL set to brelse the buffer out of existance
3260 * in the biodone routine.
3263 bufdone(struct buf *bp)
3265 struct bufobj *dropobj;
3266 void (*biodone)(struct buf *);
3268 CTR3(KTR_BUF, "bufdone(%p) vp %p flags %X", bp, bp->b_vp, bp->b_flags);
3271 KASSERT(!(bp->b_flags & B_DONE), ("biodone: bp %p already done", bp));
3272 BUF_ASSERT_HELD(bp);
3274 runningbufwakeup(bp);
3275 if (bp->b_iocmd == BIO_WRITE)
3276 dropobj = bp->b_bufobj;
3277 /* call optional completion function if requested */
3278 if (bp->b_iodone != NULL) {
3279 biodone = bp->b_iodone;
3280 bp->b_iodone = NULL;
3283 bufobj_wdrop(dropobj);
3290 bufobj_wdrop(dropobj);
3294 bufdone_finish(struct buf *bp)
3296 BUF_ASSERT_HELD(bp);
3298 if (!LIST_EMPTY(&bp->b_dep))
3301 if (bp->b_flags & B_VMIO) {
3307 struct vnode *vp = bp->b_vp;
3309 obj = bp->b_bufobj->bo_object;
3311 #if defined(VFS_BIO_DEBUG)
3312 mp_fixme("usecount and vflag accessed without locks.");
3313 if (vp->v_usecount == 0) {
3314 panic("biodone: zero vnode ref count");
3317 KASSERT(vp->v_object != NULL,
3318 ("biodone: vnode %p has no vm_object", vp));
3321 foff = bp->b_offset;
3322 KASSERT(bp->b_offset != NOOFFSET,
3323 ("biodone: no buffer offset"));
3325 VM_OBJECT_LOCK(obj);
3326 #if defined(VFS_BIO_DEBUG)
3327 if (obj->paging_in_progress < bp->b_npages) {
3328 printf("biodone: paging in progress(%d) < bp->b_npages(%d)\n",
3329 obj->paging_in_progress, bp->b_npages);
3334 * Set B_CACHE if the op was a normal read and no error
3335 * occured. B_CACHE is set for writes in the b*write()
3338 iosize = bp->b_bcount - bp->b_resid;
3339 if (bp->b_iocmd == BIO_READ &&
3340 !(bp->b_flags & (B_INVAL|B_NOCACHE)) &&
3341 !(bp->b_ioflags & BIO_ERROR)) {
3342 bp->b_flags |= B_CACHE;
3344 for (i = 0; i < bp->b_npages; i++) {
3348 resid = ((foff + PAGE_SIZE) & ~(off_t)PAGE_MASK) - foff;
3353 * cleanup bogus pages, restoring the originals
3356 if (m == bogus_page) {
3358 m = vm_page_lookup(obj, OFF_TO_IDX(foff));
3360 panic("biodone: page disappeared!");
3362 pmap_qenter(trunc_page((vm_offset_t)bp->b_data),
3363 bp->b_pages, bp->b_npages);
3365 #if defined(VFS_BIO_DEBUG)
3366 if (OFF_TO_IDX(foff) != m->pindex) {
3368 "biodone: foff(%jd)/m->pindex(%ju) mismatch\n",
3369 (intmax_t)foff, (uintmax_t)m->pindex);
3374 * In the write case, the valid and clean bits are
3375 * already changed correctly ( see bdwrite() ), so we
3376 * only need to do this here in the read case.
3378 if ((bp->b_iocmd == BIO_READ) && !bogusflag && resid > 0) {
3379 KASSERT((m->dirty & vm_page_bits(foff &
3380 PAGE_MASK, resid)) == 0, ("bufdone_finish:"
3381 " page %p has unexpected dirty bits", m));
3382 vfs_page_set_valid(bp, foff, m);
3386 * when debugging new filesystems or buffer I/O methods, this
3387 * is the most common error that pops up. if you see this, you
3388 * have not set the page busy flag correctly!!!
3391 printf("biodone: page busy < 0, "
3392 "pindex: %d, foff: 0x(%x,%x), "
3393 "resid: %d, index: %d\n",
3394 (int) m->pindex, (int)(foff >> 32),
3395 (int) foff & 0xffffffff, resid, i);
3396 if (!vn_isdisk(vp, NULL))
3397 printf(" iosize: %jd, lblkno: %jd, flags: 0x%x, npages: %d\n",
3398 (intmax_t)bp->b_vp->v_mount->mnt_stat.f_iosize,
3399 (intmax_t) bp->b_lblkno,
3400 bp->b_flags, bp->b_npages);
3402 printf(" VDEV, lblkno: %jd, flags: 0x%x, npages: %d\n",
3403 (intmax_t) bp->b_lblkno,
3404 bp->b_flags, bp->b_npages);
3405 printf(" valid: 0x%lx, dirty: 0x%lx, wired: %d\n",
3406 (u_long)m->valid, (u_long)m->dirty,
3408 panic("biodone: page busy < 0\n");
3410 vm_page_io_finish(m);
3411 vm_object_pip_subtract(obj, 1);
3412 foff = (foff + PAGE_SIZE) & ~(off_t)PAGE_MASK;
3415 vm_object_pip_wakeupn(obj, 0);
3416 VM_OBJECT_UNLOCK(obj);
3420 * For asynchronous completions, release the buffer now. The brelse
3421 * will do a wakeup there if necessary - so no need to do a wakeup
3422 * here in the async case. The sync case always needs to do a wakeup.
3425 if (bp->b_flags & B_ASYNC) {
3426 if ((bp->b_flags & (B_NOCACHE | B_INVAL | B_RELBUF)) || (bp->b_ioflags & BIO_ERROR))
3435 * This routine is called in lieu of iodone in the case of
3436 * incomplete I/O. This keeps the busy status for pages
3440 vfs_unbusy_pages(struct buf *bp)
3446 runningbufwakeup(bp);
3447 if (!(bp->b_flags & B_VMIO))
3450 obj = bp->b_bufobj->bo_object;
3451 VM_OBJECT_LOCK(obj);
3452 for (i = 0; i < bp->b_npages; i++) {
3454 if (m == bogus_page) {
3455 m = vm_page_lookup(obj, OFF_TO_IDX(bp->b_offset) + i);
3457 panic("vfs_unbusy_pages: page missing\n");
3459 pmap_qenter(trunc_page((vm_offset_t)bp->b_data),
3460 bp->b_pages, bp->b_npages);
3462 vm_object_pip_subtract(obj, 1);
3463 vm_page_io_finish(m);
3465 vm_object_pip_wakeupn(obj, 0);
3466 VM_OBJECT_UNLOCK(obj);
3470 * vfs_page_set_valid:
3472 * Set the valid bits in a page based on the supplied offset. The
3473 * range is restricted to the buffer's size.
3475 * This routine is typically called after a read completes.
3478 vfs_page_set_valid(struct buf *bp, vm_ooffset_t off, vm_page_t m)
3483 * Compute the end offset, eoff, such that [off, eoff) does not span a
3484 * page boundary and eoff is not greater than the end of the buffer.
3485 * The end of the buffer, in this case, is our file EOF, not the
3486 * allocation size of the buffer.
3488 eoff = (off + PAGE_SIZE) & ~(vm_ooffset_t)PAGE_MASK;
3489 if (eoff > bp->b_offset + bp->b_bcount)
3490 eoff = bp->b_offset + bp->b_bcount;
3493 * Set valid range. This is typically the entire buffer and thus the
3497 vm_page_set_valid(m, off & PAGE_MASK, eoff - off);
3501 * vfs_page_set_validclean:
3503 * Set the valid bits and clear the dirty bits in a page based on the
3504 * supplied offset. The range is restricted to the buffer's size.
3507 vfs_page_set_validclean(struct buf *bp, vm_ooffset_t off, vm_page_t m)
3509 vm_ooffset_t soff, eoff;
3512 * Start and end offsets in buffer. eoff - soff may not cross a
3513 * page boundry or cross the end of the buffer. The end of the
3514 * buffer, in this case, is our file EOF, not the allocation size
3518 eoff = (off + PAGE_SIZE) & ~(off_t)PAGE_MASK;
3519 if (eoff > bp->b_offset + bp->b_bcount)
3520 eoff = bp->b_offset + bp->b_bcount;
3523 * Set valid range. This is typically the entire buffer and thus the
3527 vm_page_set_validclean(
3529 (vm_offset_t) (soff & PAGE_MASK),
3530 (vm_offset_t) (eoff - soff)
3536 * This routine is called before a device strategy routine.
3537 * It is used to tell the VM system that paging I/O is in
3538 * progress, and treat the pages associated with the buffer
3539 * almost as being VPO_BUSY. Also the object paging_in_progress
3540 * flag is handled to make sure that the object doesn't become
3543 * Since I/O has not been initiated yet, certain buffer flags
3544 * such as BIO_ERROR or B_INVAL may be in an inconsistant state
3545 * and should be ignored.
3548 vfs_busy_pages(struct buf *bp, int clear_modify)
3555 if (!(bp->b_flags & B_VMIO))
3558 obj = bp->b_bufobj->bo_object;
3559 foff = bp->b_offset;
3560 KASSERT(bp->b_offset != NOOFFSET,
3561 ("vfs_busy_pages: no buffer offset"));
3562 VM_OBJECT_LOCK(obj);
3563 if (bp->b_bufsize != 0)
3564 vfs_setdirty_locked_object(bp);
3566 for (i = 0; i < bp->b_npages; i++) {
3569 if (vm_page_sleep_if_busy(m, FALSE, "vbpage"))
3573 for (i = 0; i < bp->b_npages; i++) {
3576 if ((bp->b_flags & B_CLUSTER) == 0) {
3577 vm_object_pip_add(obj, 1);
3578 vm_page_io_start(m);
3581 * When readying a buffer for a read ( i.e
3582 * clear_modify == 0 ), it is important to do
3583 * bogus_page replacement for valid pages in
3584 * partially instantiated buffers. Partially
3585 * instantiated buffers can, in turn, occur when
3586 * reconstituting a buffer from its VM backing store
3587 * base. We only have to do this if B_CACHE is
3588 * clear ( which causes the I/O to occur in the
3589 * first place ). The replacement prevents the read
3590 * I/O from overwriting potentially dirty VM-backed
3591 * pages. XXX bogus page replacement is, uh, bogus.
3592 * It may not work properly with small-block devices.
3593 * We need to find a better way.
3596 pmap_remove_write(m);
3597 vfs_page_set_validclean(bp, foff, m);
3598 } else if (m->valid == VM_PAGE_BITS_ALL &&
3599 (bp->b_flags & B_CACHE) == 0) {
3600 bp->b_pages[i] = bogus_page;
3603 foff = (foff + PAGE_SIZE) & ~(off_t)PAGE_MASK;
3605 VM_OBJECT_UNLOCK(obj);
3607 pmap_qenter(trunc_page((vm_offset_t)bp->b_data),
3608 bp->b_pages, bp->b_npages);
3612 * Tell the VM system that the pages associated with this buffer
3613 * are clean. This is used for delayed writes where the data is
3614 * going to go to disk eventually without additional VM intevention.
3616 * Note that while we only really need to clean through to b_bcount, we
3617 * just go ahead and clean through to b_bufsize.
3620 vfs_clean_pages(struct buf *bp)
3623 vm_ooffset_t foff, noff, eoff;
3626 if (!(bp->b_flags & B_VMIO))
3629 foff = bp->b_offset;
3630 KASSERT(bp->b_offset != NOOFFSET,
3631 ("vfs_clean_pages: no buffer offset"));
3632 VM_OBJECT_LOCK(bp->b_bufobj->bo_object);
3633 vm_page_lock_queues();
3634 for (i = 0; i < bp->b_npages; i++) {
3636 noff = (foff + PAGE_SIZE) & ~(off_t)PAGE_MASK;
3639 if (eoff > bp->b_offset + bp->b_bufsize)
3640 eoff = bp->b_offset + bp->b_bufsize;
3641 vfs_page_set_validclean(bp, foff, m);
3642 /* vm_page_clear_dirty(m, foff & PAGE_MASK, eoff - foff); */
3645 vm_page_unlock_queues();
3646 VM_OBJECT_UNLOCK(bp->b_bufobj->bo_object);
3650 * vfs_bio_set_valid:
3652 * Set the range within the buffer to valid. The range is
3653 * relative to the beginning of the buffer, b_offset. Note that
3654 * b_offset itself may be offset from the beginning of the first
3658 vfs_bio_set_valid(struct buf *bp, int base, int size)
3663 if (!(bp->b_flags & B_VMIO))
3667 * Fixup base to be relative to beginning of first page.
3668 * Set initial n to be the maximum number of bytes in the
3669 * first page that can be validated.
3671 base += (bp->b_offset & PAGE_MASK);
3672 n = PAGE_SIZE - (base & PAGE_MASK);
3674 VM_OBJECT_LOCK(bp->b_bufobj->bo_object);
3675 for (i = base / PAGE_SIZE; size > 0 && i < bp->b_npages; ++i) {
3679 vm_page_set_valid(m, base & PAGE_MASK, n);
3684 VM_OBJECT_UNLOCK(bp->b_bufobj->bo_object);
3690 * If the specified buffer is a non-VMIO buffer, clear the entire
3691 * buffer. If the specified buffer is a VMIO buffer, clear and
3692 * validate only the previously invalid portions of the buffer.
3693 * This routine essentially fakes an I/O, so we need to clear
3694 * BIO_ERROR and B_INVAL.
3696 * Note that while we only theoretically need to clear through b_bcount,
3697 * we go ahead and clear through b_bufsize.
3700 vfs_bio_clrbuf(struct buf *bp)
3705 if ((bp->b_flags & (B_VMIO | B_MALLOC)) != B_VMIO) {
3709 bp->b_flags &= ~B_INVAL;
3710 bp->b_ioflags &= ~BIO_ERROR;
3711 VM_OBJECT_LOCK(bp->b_bufobj->bo_object);
3712 if ((bp->b_npages == 1) && (bp->b_bufsize < PAGE_SIZE) &&
3713 (bp->b_offset & PAGE_MASK) == 0) {
3714 if (bp->b_pages[0] == bogus_page)
3716 mask = (1 << (bp->b_bufsize / DEV_BSIZE)) - 1;
3717 VM_OBJECT_LOCK_ASSERT(bp->b_pages[0]->object, MA_OWNED);
3718 if ((bp->b_pages[0]->valid & mask) == mask)
3720 if ((bp->b_pages[0]->valid & mask) == 0) {
3721 bzero(bp->b_data, bp->b_bufsize);
3722 bp->b_pages[0]->valid |= mask;
3726 ea = sa = bp->b_data;
3727 for(i = 0; i < bp->b_npages; i++, sa = ea) {
3728 ea = (caddr_t)trunc_page((vm_offset_t)sa + PAGE_SIZE);
3729 ea = (caddr_t)(vm_offset_t)ulmin(
3730 (u_long)(vm_offset_t)ea,
3731 (u_long)(vm_offset_t)bp->b_data + bp->b_bufsize);
3732 if (bp->b_pages[i] == bogus_page)
3734 j = ((vm_offset_t)sa & PAGE_MASK) / DEV_BSIZE;
3735 mask = ((1 << ((ea - sa) / DEV_BSIZE)) - 1) << j;
3736 VM_OBJECT_LOCK_ASSERT(bp->b_pages[i]->object, MA_OWNED);
3737 if ((bp->b_pages[i]->valid & mask) == mask)
3739 if ((bp->b_pages[i]->valid & mask) == 0)
3742 for (; sa < ea; sa += DEV_BSIZE, j++) {
3743 if ((bp->b_pages[i]->valid & (1 << j)) == 0)
3744 bzero(sa, DEV_BSIZE);
3747 bp->b_pages[i]->valid |= mask;
3750 VM_OBJECT_UNLOCK(bp->b_bufobj->bo_object);
3755 * vm_hold_load_pages and vm_hold_free_pages get pages into
3756 * a buffers address space. The pages are anonymous and are
3757 * not associated with a file object.
3760 vm_hold_load_pages(struct buf *bp, vm_offset_t from, vm_offset_t to)
3766 to = round_page(to);
3767 from = round_page(from);
3768 index = (from - trunc_page((vm_offset_t)bp->b_data)) >> PAGE_SHIFT;
3770 for (pg = from; pg < to; pg += PAGE_SIZE, index++) {
3773 * note: must allocate system pages since blocking here
3774 * could interfere with paging I/O, no matter which
3777 p = vm_page_alloc(NULL, pg >> PAGE_SHIFT, VM_ALLOC_NOOBJ |
3778 VM_ALLOC_SYSTEM | VM_ALLOC_WIRED);
3780 atomic_add_int(&vm_pageout_deficit,
3781 (to - pg) >> PAGE_SHIFT);
3785 pmap_qenter(pg, &p, 1);
3786 bp->b_pages[index] = p;
3788 bp->b_npages = index;
3791 /* Return pages associated with this buf to the vm system */
3793 vm_hold_free_pages(struct buf *bp, vm_offset_t from, vm_offset_t to)
3797 int index, newnpages;
3799 from = round_page(from);
3800 to = round_page(to);
3801 newnpages = index = (from - trunc_page((vm_offset_t)bp->b_data)) >> PAGE_SHIFT;
3803 for (pg = from; pg < to; pg += PAGE_SIZE, index++) {
3804 p = bp->b_pages[index];
3805 if (p && (index < bp->b_npages)) {
3808 "vm_hold_free_pages: blkno: %jd, lblkno: %jd\n",
3809 (intmax_t)bp->b_blkno,
3810 (intmax_t)bp->b_lblkno);
3812 bp->b_pages[index] = NULL;
3813 pmap_qremove(pg, 1);
3816 atomic_subtract_int(&cnt.v_wire_count, 1);
3819 bp->b_npages = newnpages;
3823 * Map an IO request into kernel virtual address space.
3825 * All requests are (re)mapped into kernel VA space.
3826 * Notice that we use b_bufsize for the size of the buffer
3827 * to be mapped. b_bcount might be modified by the driver.
3829 * Note that even if the caller determines that the address space should
3830 * be valid, a race or a smaller-file mapped into a larger space may
3831 * actually cause vmapbuf() to fail, so all callers of vmapbuf() MUST
3832 * check the return value.
3835 vmapbuf(struct buf *bp)
3841 struct pmap *pmap = &curproc->p_vmspace->vm_pmap;
3843 if (bp->b_bufsize < 0)
3845 prot = VM_PROT_READ;
3846 if (bp->b_iocmd == BIO_READ)
3847 prot |= VM_PROT_WRITE; /* Less backwards than it looks */
3848 for (addr = (caddr_t)trunc_page((vm_offset_t)bp->b_data), pidx = 0;
3849 addr < bp->b_data + bp->b_bufsize;
3850 addr += PAGE_SIZE, pidx++) {
3852 * Do the vm_fault if needed; do the copy-on-write thing
3853 * when reading stuff off device into memory.
3855 * NOTE! Must use pmap_extract() because addr may be in
3856 * the userland address space, and kextract is only guarenteed
3857 * to work for the kernland address space (see: sparc64 port).
3860 if (vm_fault_quick(addr >= bp->b_data ? addr : bp->b_data,
3862 for (i = 0; i < pidx; ++i) {
3863 vm_page_lock(bp->b_pages[i]);
3864 vm_page_unhold(bp->b_pages[i]);
3865 vm_page_unlock(bp->b_pages[i]);
3866 bp->b_pages[i] = NULL;
3870 m = pmap_extract_and_hold(pmap, (vm_offset_t)addr, prot);
3873 bp->b_pages[pidx] = m;
3875 if (pidx > btoc(MAXPHYS))
3876 panic("vmapbuf: mapped more than MAXPHYS");
3877 pmap_qenter((vm_offset_t)bp->b_saveaddr, bp->b_pages, pidx);
3879 kva = bp->b_saveaddr;
3880 bp->b_npages = pidx;
3881 bp->b_saveaddr = bp->b_data;
3882 bp->b_data = kva + (((vm_offset_t) bp->b_data) & PAGE_MASK);
3887 * Free the io map PTEs associated with this IO operation.
3888 * We also invalidate the TLB entries and restore the original b_addr.
3891 vunmapbuf(struct buf *bp)
3896 npages = bp->b_npages;
3897 pmap_qremove(trunc_page((vm_offset_t)bp->b_data), npages);
3898 for (pidx = 0; pidx < npages; pidx++) {
3899 vm_page_lock(bp->b_pages[pidx]);
3900 vm_page_unhold(bp->b_pages[pidx]);
3901 vm_page_unlock(bp->b_pages[pidx]);
3904 bp->b_data = bp->b_saveaddr;
3908 bdone(struct buf *bp)
3912 mtxp = mtx_pool_find(mtxpool_sleep, bp);
3914 bp->b_flags |= B_DONE;
3920 bwait(struct buf *bp, u_char pri, const char *wchan)
3924 mtxp = mtx_pool_find(mtxpool_sleep, bp);
3926 while ((bp->b_flags & B_DONE) == 0)
3927 msleep(bp, mtxp, pri, wchan, 0);
3932 bufsync(struct bufobj *bo, int waitfor)
3935 return (VOP_FSYNC(bo->__bo_vnode, waitfor, curthread));
3939 bufstrategy(struct bufobj *bo, struct buf *bp)
3945 KASSERT(vp == bo->bo_private, ("Inconsistent vnode bufstrategy"));
3946 KASSERT(vp->v_type != VCHR && vp->v_type != VBLK,
3947 ("Wrong vnode in bufstrategy(bp=%p, vp=%p)", bp, vp));
3948 i = VOP_STRATEGY(vp, bp);
3949 KASSERT(i == 0, ("VOP_STRATEGY failed bp=%p vp=%p", bp, bp->b_vp));
3953 bufobj_wrefl(struct bufobj *bo)
3956 KASSERT(bo != NULL, ("NULL bo in bufobj_wref"));
3957 ASSERT_BO_LOCKED(bo);
3962 bufobj_wref(struct bufobj *bo)
3965 KASSERT(bo != NULL, ("NULL bo in bufobj_wref"));
3972 bufobj_wdrop(struct bufobj *bo)
3975 KASSERT(bo != NULL, ("NULL bo in bufobj_wdrop"));
3977 KASSERT(bo->bo_numoutput > 0, ("bufobj_wdrop non-positive count"));
3978 if ((--bo->bo_numoutput == 0) && (bo->bo_flag & BO_WWAIT)) {
3979 bo->bo_flag &= ~BO_WWAIT;
3980 wakeup(&bo->bo_numoutput);
3986 bufobj_wwait(struct bufobj *bo, int slpflag, int timeo)
3990 KASSERT(bo != NULL, ("NULL bo in bufobj_wwait"));
3991 ASSERT_BO_LOCKED(bo);
3993 while (bo->bo_numoutput) {
3994 bo->bo_flag |= BO_WWAIT;
3995 error = msleep(&bo->bo_numoutput, BO_MTX(bo),
3996 slpflag | (PRIBIO + 1), "bo_wwait", timeo);
4004 bpin(struct buf *bp)
4008 mtxp = mtx_pool_find(mtxpool_sleep, bp);
4015 bunpin(struct buf *bp)
4019 mtxp = mtx_pool_find(mtxpool_sleep, bp);
4021 if (--bp->b_pin_count == 0)
4027 bunpin_wait(struct buf *bp)
4031 mtxp = mtx_pool_find(mtxpool_sleep, bp);
4033 while (bp->b_pin_count > 0)
4034 msleep(bp, mtxp, PRIBIO, "bwunpin", 0);
4038 #include "opt_ddb.h"
4040 #include <ddb/ddb.h>
4042 /* DDB command to show buffer data */
4043 DB_SHOW_COMMAND(buffer, db_show_buffer)
4046 struct buf *bp = (struct buf *)addr;
4049 db_printf("usage: show buffer <addr>\n");
4053 db_printf("buf at %p\n", bp);
4054 db_printf("b_flags = 0x%b\n", (u_int)bp->b_flags, PRINT_BUF_FLAGS);
4056 "b_error = %d, b_bufsize = %ld, b_bcount = %ld, b_resid = %ld\n"
4057 "b_bufobj = (%p), b_data = %p, b_blkno = %jd, b_dep = %p\n",
4058 bp->b_error, bp->b_bufsize, bp->b_bcount, bp->b_resid,
4059 bp->b_bufobj, bp->b_data, (intmax_t)bp->b_blkno,
4060 bp->b_dep.lh_first);
4063 db_printf("b_npages = %d, pages(OBJ, IDX, PA): ", bp->b_npages);
4064 for (i = 0; i < bp->b_npages; i++) {
4067 db_printf("(%p, 0x%lx, 0x%lx)", (void *)m->object,
4068 (u_long)m->pindex, (u_long)VM_PAGE_TO_PHYS(m));
4069 if ((i + 1) < bp->b_npages)
4075 lockmgr_printinfo(&bp->b_lock);
4078 DB_SHOW_COMMAND(lockedbufs, lockedbufs)
4083 for (i = 0; i < nbuf; i++) {
4085 if (BUF_ISLOCKED(bp)) {
4086 db_show_buffer((uintptr_t)bp, 1, 0, NULL);
4092 DB_SHOW_COMMAND(vnodebufs, db_show_vnodebufs)
4098 db_printf("usage: show vnodebufs <addr>\n");
4101 vp = (struct vnode *)addr;
4102 db_printf("Clean buffers:\n");
4103 TAILQ_FOREACH(bp, &vp->v_bufobj.bo_clean.bv_hd, b_bobufs) {
4104 db_show_buffer((uintptr_t)bp, 1, 0, NULL);
4107 db_printf("Dirty buffers:\n");
4108 TAILQ_FOREACH(bp, &vp->v_bufobj.bo_dirty.bv_hd, b_bobufs) {
4109 db_show_buffer((uintptr_t)bp, 1, 0, NULL);