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_drain_busy_pages(struct buf *bp);
106 static void vfs_clean_pages_dirty_buf(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 * This lock synchronizes access to bd_request.
221 static struct mtx bdlock;
224 * bogus page -- for I/O to/from partially complete buffers
225 * this is a temporary solution to the problem, but it is not
226 * really that bad. it would be better to split the buffer
227 * for input in the case of buffers partially already in memory,
228 * but the code is intricate enough already.
230 vm_page_t bogus_page;
233 * Synchronization (sleep/wakeup) variable for active buffer space requests.
234 * Set when wait starts, cleared prior to wakeup().
235 * Used in runningbufwakeup() and waitrunningbufspace().
237 static int runningbufreq;
240 * This lock protects the runningbufreq and synchronizes runningbufwakeup and
241 * waitrunningbufspace().
243 static struct mtx rbreqlock;
246 * Synchronization (sleep/wakeup) variable for buffer requests.
247 * Can contain the VFS_BIO_NEED flags defined below; setting/clearing is done
249 * Used in numdirtywakeup(), bufspacewakeup(), bufcountwakeup(), bwillwrite(),
250 * getnewbuf(), and getblk().
252 static int needsbuffer;
255 * Lock that protects needsbuffer and the sleeps/wakeups surrounding it.
257 static struct mtx nblock;
260 * Definitions for the buffer free lists.
262 #define BUFFER_QUEUES 6 /* number of free buffer queues */
264 #define QUEUE_NONE 0 /* on no queue */
265 #define QUEUE_CLEAN 1 /* non-B_DELWRI buffers */
266 #define QUEUE_DIRTY 2 /* B_DELWRI buffers */
267 #define QUEUE_DIRTY_GIANT 3 /* B_DELWRI buffers that need giant */
268 #define QUEUE_EMPTYKVA 4 /* empty buffer headers w/KVA assignment */
269 #define QUEUE_EMPTY 5 /* empty buffer headers */
270 #define QUEUE_SENTINEL 1024 /* not an queue index, but mark for sentinel */
272 /* Queues for free buffers with various properties */
273 static TAILQ_HEAD(bqueues, buf) bufqueues[BUFFER_QUEUES] = { { 0 } };
275 /* Lock for the bufqueues */
276 static struct mtx bqlock;
279 * Single global constant for BUF_WMESG, to avoid getting multiple references.
280 * buf_wmesg is referred from macros.
282 const char *buf_wmesg = BUF_WMESG;
284 #define VFS_BIO_NEED_ANY 0x01 /* any freeable buffer */
285 #define VFS_BIO_NEED_DIRTYFLUSH 0x02 /* waiting for dirty buffer flush */
286 #define VFS_BIO_NEED_FREE 0x04 /* wait for free bufs, hi hysteresis */
287 #define VFS_BIO_NEED_BUFSPACE 0x08 /* wait for buf space, lo hysteresis */
289 #if defined(COMPAT_FREEBSD4) || defined(COMPAT_FREEBSD5) || \
290 defined(COMPAT_FREEBSD6) || defined(COMPAT_FREEBSD7)
292 sysctl_bufspace(SYSCTL_HANDLER_ARGS)
297 if (sizeof(int) == sizeof(long) || req->oldlen >= sizeof(long))
298 return (sysctl_handle_long(oidp, arg1, arg2, req));
299 lvalue = *(long *)arg1;
300 if (lvalue > INT_MAX)
301 /* On overflow, still write out a long to trigger ENOMEM. */
302 return (sysctl_handle_long(oidp, &lvalue, 0, req));
304 return (sysctl_handle_int(oidp, &ivalue, 0, req));
309 extern void ffs_rawread_setup(void);
310 #endif /* DIRECTIO */
314 * If someone is blocked due to there being too many dirty buffers,
315 * and numdirtybuffers is now reasonable, wake them up.
319 numdirtywakeup(int level)
322 if (numdirtybuffers <= level) {
324 if (needsbuffer & VFS_BIO_NEED_DIRTYFLUSH) {
325 needsbuffer &= ~VFS_BIO_NEED_DIRTYFLUSH;
326 wakeup(&needsbuffer);
335 * Called when buffer space is potentially available for recovery.
336 * getnewbuf() will block on this flag when it is unable to free
337 * sufficient buffer space. Buffer space becomes recoverable when
338 * bp's get placed back in the queues.
346 * If someone is waiting for BUF space, wake them up. Even
347 * though we haven't freed the kva space yet, the waiting
348 * process will be able to now.
351 if (needsbuffer & VFS_BIO_NEED_BUFSPACE) {
352 needsbuffer &= ~VFS_BIO_NEED_BUFSPACE;
353 wakeup(&needsbuffer);
359 * runningbufwakeup() - in-progress I/O accounting.
363 runningbufwakeup(struct buf *bp)
366 if (bp->b_runningbufspace) {
367 atomic_subtract_long(&runningbufspace, bp->b_runningbufspace);
368 bp->b_runningbufspace = 0;
369 mtx_lock(&rbreqlock);
370 if (runningbufreq && runningbufspace <= lorunningspace) {
372 wakeup(&runningbufreq);
374 mtx_unlock(&rbreqlock);
381 * Called when a buffer has been added to one of the free queues to
382 * account for the buffer and to wakeup anyone waiting for free buffers.
383 * This typically occurs when large amounts of metadata are being handled
384 * by the buffer cache ( else buffer space runs out first, usually ).
391 atomic_add_int(&numfreebuffers, 1);
394 needsbuffer &= ~VFS_BIO_NEED_ANY;
395 if (numfreebuffers >= hifreebuffers)
396 needsbuffer &= ~VFS_BIO_NEED_FREE;
397 wakeup(&needsbuffer);
403 * waitrunningbufspace()
405 * runningbufspace is a measure of the amount of I/O currently
406 * running. This routine is used in async-write situations to
407 * prevent creating huge backups of pending writes to a device.
408 * Only asynchronous writes are governed by this function.
410 * Reads will adjust runningbufspace, but will not block based on it.
411 * The read load has a side effect of reducing the allowed write load.
413 * This does NOT turn an async write into a sync write. It waits
414 * for earlier writes to complete and generally returns before the
415 * caller's write has reached the device.
418 waitrunningbufspace(void)
421 mtx_lock(&rbreqlock);
422 while (runningbufspace > hirunningspace) {
424 msleep(&runningbufreq, &rbreqlock, PVM, "wdrain", 0);
426 mtx_unlock(&rbreqlock);
431 * vfs_buf_test_cache:
433 * Called when a buffer is extended. This function clears the B_CACHE
434 * bit if the newly extended portion of the buffer does not contain
439 vfs_buf_test_cache(struct buf *bp,
440 vm_ooffset_t foff, vm_offset_t off, vm_offset_t size,
444 VM_OBJECT_LOCK_ASSERT(m->object, MA_OWNED);
445 if (bp->b_flags & B_CACHE) {
446 int base = (foff + off) & PAGE_MASK;
447 if (vm_page_is_valid(m, base, size) == 0)
448 bp->b_flags &= ~B_CACHE;
452 /* Wake up the buffer daemon if necessary */
455 bd_wakeup(int dirtybuflevel)
459 if (bd_request == 0 && numdirtybuffers >= dirtybuflevel) {
467 * bd_speedup - speedup the buffer cache flushing code
479 * Calculating buffer cache scaling values and reserve space for buffer
480 * headers. This is called during low level kernel initialization and
481 * may be called more then once. We CANNOT write to the memory area
482 * being reserved at this time.
485 kern_vfs_bio_buffer_alloc(caddr_t v, long physmem_est)
491 * physmem_est is in pages. Convert it to kilobytes (assumes
492 * PAGE_SIZE is >= 1K)
494 physmem_est = physmem_est * (PAGE_SIZE / 1024);
497 * The nominal buffer size (and minimum KVA allocation) is BKVASIZE.
498 * For the first 64MB of ram nominally allocate sufficient buffers to
499 * cover 1/4 of our ram. Beyond the first 64MB allocate additional
500 * buffers to cover 1/10 of our ram over 64MB. When auto-sizing
501 * the buffer cache we limit the eventual kva reservation to
504 * factor represents the 1/4 x ram conversion.
507 int factor = 4 * BKVASIZE / 1024;
510 if (physmem_est > 4096)
511 nbuf += min((physmem_est - 4096) / factor,
513 if (physmem_est > 65536)
514 nbuf += (physmem_est - 65536) * 2 / (factor * 5);
516 if (maxbcache && nbuf > maxbcache / BKVASIZE)
517 nbuf = maxbcache / BKVASIZE;
522 /* XXX Avoid unsigned long overflows later on with maxbufspace. */
523 maxbuf = (LONG_MAX / 3) / BKVASIZE;
526 printf("Warning: nbufs lowered from %d to %ld\n", nbuf,
532 * swbufs are used as temporary holders for I/O, such as paging I/O.
533 * We have no less then 16 and no more then 256.
535 nswbuf = max(min(nbuf/4, 256), 16);
537 if (nswbuf < NSWBUF_MIN)
545 * Reserve space for the buffer cache buffers
548 v = (caddr_t)(swbuf + nswbuf);
550 v = (caddr_t)(buf + nbuf);
555 /* Initialize the buffer subsystem. Called before use of any buffers. */
562 mtx_init(&bqlock, "buf queue lock", NULL, MTX_DEF);
563 mtx_init(&rbreqlock, "runningbufspace lock", NULL, MTX_DEF);
564 mtx_init(&nblock, "needsbuffer lock", NULL, MTX_DEF);
565 mtx_init(&bdlock, "buffer daemon lock", NULL, MTX_DEF);
567 /* next, make a null set of free lists */
568 for (i = 0; i < BUFFER_QUEUES; i++)
569 TAILQ_INIT(&bufqueues[i]);
571 /* finally, initialize each buffer header and stick on empty q */
572 for (i = 0; i < nbuf; i++) {
574 bzero(bp, sizeof *bp);
575 bp->b_flags = B_INVAL; /* we're just an empty header */
576 bp->b_rcred = NOCRED;
577 bp->b_wcred = NOCRED;
578 bp->b_qindex = QUEUE_EMPTY;
581 LIST_INIT(&bp->b_dep);
583 TAILQ_INSERT_TAIL(&bufqueues[QUEUE_EMPTY], bp, b_freelist);
587 * maxbufspace is the absolute maximum amount of buffer space we are
588 * allowed to reserve in KVM and in real terms. The absolute maximum
589 * is nominally used by buf_daemon. hibufspace is the nominal maximum
590 * used by most other processes. The differential is required to
591 * ensure that buf_daemon is able to run when other processes might
592 * be blocked waiting for buffer space.
594 * maxbufspace is based on BKVASIZE. Allocating buffers larger then
595 * this may result in KVM fragmentation which is not handled optimally
598 maxbufspace = (long)nbuf * BKVASIZE;
599 hibufspace = lmax(3 * maxbufspace / 4, maxbufspace - MAXBSIZE * 10);
600 lobufspace = hibufspace - MAXBSIZE;
602 lorunningspace = 512 * 1024;
603 hirunningspace = 1024 * 1024;
606 * Limit the amount of malloc memory since it is wired permanently into
607 * the kernel space. Even though this is accounted for in the buffer
608 * allocation, we don't want the malloced region to grow uncontrolled.
609 * The malloc scheme improves memory utilization significantly on average
610 * (small) directories.
612 maxbufmallocspace = hibufspace / 20;
615 * Reduce the chance of a deadlock occuring by limiting the number
616 * of delayed-write dirty buffers we allow to stack up.
618 hidirtybuffers = nbuf / 4 + 20;
619 dirtybufthresh = hidirtybuffers * 9 / 10;
622 * To support extreme low-memory systems, make sure hidirtybuffers cannot
623 * eat up all available buffer space. This occurs when our minimum cannot
624 * be met. We try to size hidirtybuffers to 3/4 our buffer space assuming
625 * BKVASIZE'd (8K) buffers.
627 while ((long)hidirtybuffers * BKVASIZE > 3 * hibufspace / 4) {
628 hidirtybuffers >>= 1;
630 lodirtybuffers = hidirtybuffers / 2;
633 * Try to keep the number of free buffers in the specified range,
634 * and give special processes (e.g. like buf_daemon) access to an
637 lofreebuffers = nbuf / 18 + 5;
638 hifreebuffers = 2 * lofreebuffers;
639 numfreebuffers = nbuf;
641 bogus_page = vm_page_alloc(NULL, 0, VM_ALLOC_NOOBJ |
642 VM_ALLOC_NORMAL | VM_ALLOC_WIRED);
646 * bfreekva() - free the kva allocation for a buffer.
648 * Since this call frees up buffer space, we call bufspacewakeup().
651 bfreekva(struct buf *bp)
655 atomic_add_int(&buffreekvacnt, 1);
656 atomic_subtract_long(&bufspace, bp->b_kvasize);
657 vm_map_remove(buffer_map, (vm_offset_t) bp->b_kvabase,
658 (vm_offset_t) bp->b_kvabase + bp->b_kvasize);
667 * Mark the buffer for removal from the appropriate free list in brelse.
671 bremfree(struct buf *bp)
674 CTR3(KTR_BUF, "bremfree(%p) vp %p flags %X", bp, bp->b_vp, bp->b_flags);
675 KASSERT((bp->b_flags & B_REMFREE) == 0,
676 ("bremfree: buffer %p already marked for delayed removal.", bp));
677 KASSERT(bp->b_qindex != QUEUE_NONE,
678 ("bremfree: buffer %p not on a queue.", bp));
681 bp->b_flags |= B_REMFREE;
682 /* Fixup numfreebuffers count. */
683 if ((bp->b_flags & B_INVAL) || (bp->b_flags & B_DELWRI) == 0)
684 atomic_subtract_int(&numfreebuffers, 1);
690 * Force an immediate removal from a free list. Used only in nfs when
691 * it abuses the b_freelist pointer.
694 bremfreef(struct buf *bp)
704 * Removes a buffer from the free list, must be called with the
708 bremfreel(struct buf *bp)
710 CTR3(KTR_BUF, "bremfreel(%p) vp %p flags %X",
711 bp, bp->b_vp, bp->b_flags);
712 KASSERT(bp->b_qindex != QUEUE_NONE,
713 ("bremfreel: buffer %p not on a queue.", bp));
715 mtx_assert(&bqlock, MA_OWNED);
717 TAILQ_REMOVE(&bufqueues[bp->b_qindex], bp, b_freelist);
718 bp->b_qindex = QUEUE_NONE;
720 * If this was a delayed bremfree() we only need to remove the buffer
721 * from the queue and return the stats are already done.
723 if (bp->b_flags & B_REMFREE) {
724 bp->b_flags &= ~B_REMFREE;
728 * Fixup numfreebuffers count. If the buffer is invalid or not
729 * delayed-write, the buffer was free and we must decrement
732 if ((bp->b_flags & B_INVAL) || (bp->b_flags & B_DELWRI) == 0)
733 atomic_subtract_int(&numfreebuffers, 1);
738 * Get a buffer with the specified data. Look in the cache first. We
739 * must clear BIO_ERROR and B_INVAL prior to initiating I/O. If B_CACHE
740 * is set, the buffer is valid and we do not have to do anything ( see
741 * getblk() ). This is really just a special case of breadn().
744 bread(struct vnode * vp, daddr_t blkno, int size, struct ucred * cred,
748 return (breadn(vp, blkno, size, 0, 0, 0, cred, bpp));
752 * Attempt to initiate asynchronous I/O on read-ahead blocks. We must
753 * clear BIO_ERROR and B_INVAL prior to initiating I/O . If B_CACHE is set,
754 * the buffer is valid and we do not have to do anything.
757 breada(struct vnode * vp, daddr_t * rablkno, int * rabsize,
758 int cnt, struct ucred * cred)
763 for (i = 0; i < cnt; i++, rablkno++, rabsize++) {
764 if (inmem(vp, *rablkno))
766 rabp = getblk(vp, *rablkno, *rabsize, 0, 0, 0);
768 if ((rabp->b_flags & B_CACHE) == 0) {
769 if (!TD_IS_IDLETHREAD(curthread))
770 curthread->td_ru.ru_inblock++;
771 rabp->b_flags |= B_ASYNC;
772 rabp->b_flags &= ~B_INVAL;
773 rabp->b_ioflags &= ~BIO_ERROR;
774 rabp->b_iocmd = BIO_READ;
775 if (rabp->b_rcred == NOCRED && cred != NOCRED)
776 rabp->b_rcred = crhold(cred);
777 vfs_busy_pages(rabp, 0);
779 rabp->b_iooffset = dbtob(rabp->b_blkno);
788 * Operates like bread, but also starts asynchronous I/O on
792 breadn(struct vnode * vp, daddr_t blkno, int size,
793 daddr_t * rablkno, int *rabsize,
794 int cnt, struct ucred * cred, struct buf **bpp)
797 int rv = 0, readwait = 0;
799 CTR3(KTR_BUF, "breadn(%p, %jd, %d)", vp, blkno, size);
800 *bpp = bp = getblk(vp, blkno, size, 0, 0, 0);
802 /* if not found in cache, do some I/O */
803 if ((bp->b_flags & B_CACHE) == 0) {
804 if (!TD_IS_IDLETHREAD(curthread))
805 curthread->td_ru.ru_inblock++;
806 bp->b_iocmd = BIO_READ;
807 bp->b_flags &= ~B_INVAL;
808 bp->b_ioflags &= ~BIO_ERROR;
809 if (bp->b_rcred == NOCRED && cred != NOCRED)
810 bp->b_rcred = crhold(cred);
811 vfs_busy_pages(bp, 0);
812 bp->b_iooffset = dbtob(bp->b_blkno);
817 breada(vp, rablkno, rabsize, cnt, cred);
826 * Write, release buffer on completion. (Done by iodone
827 * if async). Do not bother writing anything if the buffer
830 * Note that we set B_CACHE here, indicating that buffer is
831 * fully valid and thus cacheable. This is true even of NFS
832 * now so we set it generally. This could be set either here
833 * or in biodone() since the I/O is synchronous. We put it
837 bufwrite(struct buf *bp)
843 CTR3(KTR_BUF, "bufwrite(%p) vp %p flags %X", bp, bp->b_vp, bp->b_flags);
844 if (bp->b_flags & B_INVAL) {
849 oldflags = bp->b_flags;
853 if (bp->b_pin_count > 0)
856 KASSERT(!(bp->b_vflags & BV_BKGRDINPROG),
857 ("FFS background buffer should not get here %p", bp));
861 vp_md = vp->v_vflag & VV_MD;
865 /* Mark the buffer clean */
868 bp->b_flags &= ~B_DONE;
869 bp->b_ioflags &= ~BIO_ERROR;
870 bp->b_flags |= B_CACHE;
871 bp->b_iocmd = BIO_WRITE;
873 bufobj_wref(bp->b_bufobj);
874 vfs_busy_pages(bp, 1);
877 * Normal bwrites pipeline writes
879 bp->b_runningbufspace = bp->b_bufsize;
880 atomic_add_long(&runningbufspace, bp->b_runningbufspace);
882 if (!TD_IS_IDLETHREAD(curthread))
883 curthread->td_ru.ru_oublock++;
884 if (oldflags & B_ASYNC)
886 bp->b_iooffset = dbtob(bp->b_blkno);
889 if ((oldflags & B_ASYNC) == 0) {
890 int rtval = bufwait(bp);
895 * don't allow the async write to saturate the I/O
896 * system. We will not deadlock here because
897 * we are blocking waiting for I/O that is already in-progress
898 * to complete. We do not block here if it is the update
899 * or syncer daemon trying to clean up as that can lead
902 if ((curthread->td_pflags & TDP_NORUNNINGBUF) == 0 && !vp_md)
903 waitrunningbufspace();
910 bufbdflush(struct bufobj *bo, struct buf *bp)
914 if (bo->bo_dirty.bv_cnt > dirtybufthresh + 10) {
915 (void) VOP_FSYNC(bp->b_vp, MNT_NOWAIT, curthread);
917 } else if (bo->bo_dirty.bv_cnt > dirtybufthresh) {
920 * Try to find a buffer to flush.
922 TAILQ_FOREACH(nbp, &bo->bo_dirty.bv_hd, b_bobufs) {
923 if ((nbp->b_vflags & BV_BKGRDINPROG) ||
925 LK_EXCLUSIVE | LK_NOWAIT, NULL))
928 panic("bdwrite: found ourselves");
930 /* Don't countdeps with the bo lock held. */
931 if (buf_countdeps(nbp, 0)) {
936 if (nbp->b_flags & B_CLUSTEROK) {
942 dirtybufferflushes++;
951 * Delayed write. (Buffer is marked dirty). Do not bother writing
952 * anything if the buffer is marked invalid.
954 * Note that since the buffer must be completely valid, we can safely
955 * set B_CACHE. In fact, we have to set B_CACHE here rather then in
956 * biodone() in order to prevent getblk from writing the buffer
960 bdwrite(struct buf *bp)
962 struct thread *td = curthread;
966 CTR3(KTR_BUF, "bdwrite(%p) vp %p flags %X", bp, bp->b_vp, bp->b_flags);
967 KASSERT(bp->b_bufobj != NULL, ("No b_bufobj %p", bp));
970 if (bp->b_flags & B_INVAL) {
976 * If we have too many dirty buffers, don't create any more.
977 * If we are wildly over our limit, then force a complete
978 * cleanup. Otherwise, just keep the situation from getting
979 * out of control. Note that we have to avoid a recursive
980 * disaster and not try to clean up after our own cleanup!
984 if ((td->td_pflags & (TDP_COWINPROGRESS|TDP_INBDFLUSH)) == 0) {
985 td->td_pflags |= TDP_INBDFLUSH;
987 td->td_pflags &= ~TDP_INBDFLUSH;
993 * Set B_CACHE, indicating that the buffer is fully valid. This is
994 * true even of NFS now.
996 bp->b_flags |= B_CACHE;
999 * This bmap keeps the system from needing to do the bmap later,
1000 * perhaps when the system is attempting to do a sync. Since it
1001 * is likely that the indirect block -- or whatever other datastructure
1002 * that the filesystem needs is still in memory now, it is a good
1003 * thing to do this. Note also, that if the pageout daemon is
1004 * requesting a sync -- there might not be enough memory to do
1005 * the bmap then... So, this is important to do.
1007 if (vp->v_type != VCHR && bp->b_lblkno == bp->b_blkno) {
1008 VOP_BMAP(vp, bp->b_lblkno, NULL, &bp->b_blkno, NULL, NULL);
1012 * Set the *dirty* buffer range based upon the VM system dirty
1015 * Mark the buffer pages as clean. We need to do this here to
1016 * satisfy the vnode_pager and the pageout daemon, so that it
1017 * thinks that the pages have been "cleaned". Note that since
1018 * the pages are in a delayed write buffer -- the VFS layer
1019 * "will" see that the pages get written out on the next sync,
1020 * or perhaps the cluster will be completed.
1022 vfs_clean_pages_dirty_buf(bp);
1026 * Wakeup the buffer flushing daemon if we have a lot of dirty
1027 * buffers (midpoint between our recovery point and our stall
1030 bd_wakeup((lodirtybuffers + hidirtybuffers) / 2);
1033 * note: we cannot initiate I/O from a bdwrite even if we wanted to,
1034 * due to the softdep code.
1041 * Turn buffer into delayed write request. We must clear BIO_READ and
1042 * B_RELBUF, and we must set B_DELWRI. We reassign the buffer to
1043 * itself to properly update it in the dirty/clean lists. We mark it
1044 * B_DONE to ensure that any asynchronization of the buffer properly
1045 * clears B_DONE ( else a panic will occur later ).
1047 * bdirty() is kinda like bdwrite() - we have to clear B_INVAL which
1048 * might have been set pre-getblk(). Unlike bwrite/bdwrite, bdirty()
1049 * should only be called if the buffer is known-good.
1051 * Since the buffer is not on a queue, we do not update the numfreebuffers
1054 * The buffer must be on QUEUE_NONE.
1057 bdirty(struct buf *bp)
1060 CTR3(KTR_BUF, "bdirty(%p) vp %p flags %X",
1061 bp, bp->b_vp, bp->b_flags);
1062 KASSERT(bp->b_bufobj != NULL, ("No b_bufobj %p", bp));
1063 KASSERT(bp->b_flags & B_REMFREE || bp->b_qindex == QUEUE_NONE,
1064 ("bdirty: buffer %p still on queue %d", bp, bp->b_qindex));
1065 BUF_ASSERT_HELD(bp);
1066 bp->b_flags &= ~(B_RELBUF);
1067 bp->b_iocmd = BIO_WRITE;
1069 if ((bp->b_flags & B_DELWRI) == 0) {
1070 bp->b_flags |= /* XXX B_DONE | */ B_DELWRI;
1072 atomic_add_int(&numdirtybuffers, 1);
1073 bd_wakeup((lodirtybuffers + hidirtybuffers) / 2);
1080 * Clear B_DELWRI for buffer.
1082 * Since the buffer is not on a queue, we do not update the numfreebuffers
1085 * The buffer must be on QUEUE_NONE.
1089 bundirty(struct buf *bp)
1092 CTR3(KTR_BUF, "bundirty(%p) vp %p flags %X", bp, bp->b_vp, bp->b_flags);
1093 KASSERT(bp->b_bufobj != NULL, ("No b_bufobj %p", bp));
1094 KASSERT(bp->b_flags & B_REMFREE || bp->b_qindex == QUEUE_NONE,
1095 ("bundirty: buffer %p still on queue %d", bp, bp->b_qindex));
1096 BUF_ASSERT_HELD(bp);
1098 if (bp->b_flags & B_DELWRI) {
1099 bp->b_flags &= ~B_DELWRI;
1101 atomic_subtract_int(&numdirtybuffers, 1);
1102 numdirtywakeup(lodirtybuffers);
1105 * Since it is now being written, we can clear its deferred write flag.
1107 bp->b_flags &= ~B_DEFERRED;
1113 * Asynchronous write. Start output on a buffer, but do not wait for
1114 * it to complete. The buffer is released when the output completes.
1116 * bwrite() ( or the VOP routine anyway ) is responsible for handling
1117 * B_INVAL buffers. Not us.
1120 bawrite(struct buf *bp)
1123 bp->b_flags |= B_ASYNC;
1130 * Called prior to the locking of any vnodes when we are expecting to
1131 * write. We do not want to starve the buffer cache with too many
1132 * dirty buffers so we block here. By blocking prior to the locking
1133 * of any vnodes we attempt to avoid the situation where a locked vnode
1134 * prevents the various system daemons from flushing related buffers.
1141 if (numdirtybuffers >= hidirtybuffers) {
1143 while (numdirtybuffers >= hidirtybuffers) {
1145 needsbuffer |= VFS_BIO_NEED_DIRTYFLUSH;
1146 msleep(&needsbuffer, &nblock,
1147 (PRIBIO + 4), "flswai", 0);
1149 mtx_unlock(&nblock);
1154 * Return true if we have too many dirty buffers.
1157 buf_dirty_count_severe(void)
1160 return(numdirtybuffers >= hidirtybuffers);
1163 static __noinline int
1164 buf_vm_page_count_severe(void)
1167 KFAIL_POINT_CODE(DEBUG_FP, buf_pressure, return 1);
1169 return vm_page_count_severe();
1175 * Release a busy buffer and, if requested, free its resources. The
1176 * buffer will be stashed in the appropriate bufqueue[] allowing it
1177 * to be accessed later as a cache entity or reused for other purposes.
1180 brelse(struct buf *bp)
1182 CTR3(KTR_BUF, "brelse(%p) vp %p flags %X",
1183 bp, bp->b_vp, bp->b_flags);
1184 KASSERT(!(bp->b_flags & (B_CLUSTER|B_PAGING)),
1185 ("brelse: inappropriate B_PAGING or B_CLUSTER bp %p", bp));
1187 if (bp->b_flags & B_MANAGED) {
1192 if (bp->b_iocmd == BIO_WRITE && (bp->b_ioflags & BIO_ERROR) &&
1193 bp->b_error == EIO && !(bp->b_flags & B_INVAL)) {
1195 * Failed write, redirty. Must clear BIO_ERROR to prevent
1196 * pages from being scrapped. If the error is anything
1197 * other than an I/O error (EIO), assume that retrying
1200 bp->b_ioflags &= ~BIO_ERROR;
1202 } else if ((bp->b_flags & (B_NOCACHE | B_INVAL)) ||
1203 (bp->b_ioflags & BIO_ERROR) || (bp->b_bufsize <= 0)) {
1205 * Either a failed I/O or we were asked to free or not
1208 bp->b_flags |= B_INVAL;
1209 if (!LIST_EMPTY(&bp->b_dep))
1211 if (bp->b_flags & B_DELWRI) {
1212 atomic_subtract_int(&numdirtybuffers, 1);
1213 numdirtywakeup(lodirtybuffers);
1215 bp->b_flags &= ~(B_DELWRI | B_CACHE);
1216 if ((bp->b_flags & B_VMIO) == 0) {
1225 * We must clear B_RELBUF if B_DELWRI is set. If vfs_vmio_release()
1226 * is called with B_DELWRI set, the underlying pages may wind up
1227 * getting freed causing a previous write (bdwrite()) to get 'lost'
1228 * because pages associated with a B_DELWRI bp are marked clean.
1230 * We still allow the B_INVAL case to call vfs_vmio_release(), even
1231 * if B_DELWRI is set.
1233 * If B_DELWRI is not set we may have to set B_RELBUF if we are low
1234 * on pages to return pages to the VM page queues.
1236 if (bp->b_flags & B_DELWRI)
1237 bp->b_flags &= ~B_RELBUF;
1238 else if (buf_vm_page_count_severe()) {
1240 * The locking of the BO_LOCK is not necessary since
1241 * BKGRDINPROG cannot be set while we hold the buf
1242 * lock, it can only be cleared if it is already
1246 if (!(bp->b_vflags & BV_BKGRDINPROG))
1247 bp->b_flags |= B_RELBUF;
1249 bp->b_flags |= B_RELBUF;
1253 * VMIO buffer rundown. It is not very necessary to keep a VMIO buffer
1254 * constituted, not even NFS buffers now. Two flags effect this. If
1255 * B_INVAL, the struct buf is invalidated but the VM object is kept
1256 * around ( i.e. so it is trivial to reconstitute the buffer later ).
1258 * If BIO_ERROR or B_NOCACHE is set, pages in the VM object will be
1259 * invalidated. BIO_ERROR cannot be set for a failed write unless the
1260 * buffer is also B_INVAL because it hits the re-dirtying code above.
1262 * Normally we can do this whether a buffer is B_DELWRI or not. If
1263 * the buffer is an NFS buffer, it is tracking piecemeal writes or
1264 * the commit state and we cannot afford to lose the buffer. If the
1265 * buffer has a background write in progress, we need to keep it
1266 * around to prevent it from being reconstituted and starting a second
1269 if ((bp->b_flags & B_VMIO)
1270 && !(bp->b_vp->v_mount != NULL &&
1271 (bp->b_vp->v_mount->mnt_vfc->vfc_flags & VFCF_NETWORK) != 0 &&
1272 !vn_isdisk(bp->b_vp, NULL) &&
1273 (bp->b_flags & B_DELWRI))
1282 obj = bp->b_bufobj->bo_object;
1285 * Get the base offset and length of the buffer. Note that
1286 * in the VMIO case if the buffer block size is not
1287 * page-aligned then b_data pointer may not be page-aligned.
1288 * But our b_pages[] array *IS* page aligned.
1290 * block sizes less then DEV_BSIZE (usually 512) are not
1291 * supported due to the page granularity bits (m->valid,
1292 * m->dirty, etc...).
1294 * See man buf(9) for more information
1296 resid = bp->b_bufsize;
1297 foff = bp->b_offset;
1298 VM_OBJECT_LOCK(obj);
1299 for (i = 0; i < bp->b_npages; i++) {
1305 * If we hit a bogus page, fixup *all* the bogus pages
1308 if (m == bogus_page) {
1309 poff = OFF_TO_IDX(bp->b_offset);
1312 for (j = i; j < bp->b_npages; j++) {
1314 mtmp = bp->b_pages[j];
1315 if (mtmp == bogus_page) {
1316 mtmp = vm_page_lookup(obj, poff + j);
1318 panic("brelse: page missing\n");
1320 bp->b_pages[j] = mtmp;
1324 if ((bp->b_flags & B_INVAL) == 0) {
1326 trunc_page((vm_offset_t)bp->b_data),
1327 bp->b_pages, bp->b_npages);
1331 if ((bp->b_flags & B_NOCACHE) ||
1332 (bp->b_ioflags & BIO_ERROR &&
1333 bp->b_iocmd == BIO_READ)) {
1334 int poffset = foff & PAGE_MASK;
1335 int presid = resid > (PAGE_SIZE - poffset) ?
1336 (PAGE_SIZE - poffset) : resid;
1338 KASSERT(presid >= 0, ("brelse: extra page"));
1339 vm_page_lock_queues();
1340 vm_page_set_invalid(m, poffset, presid);
1341 vm_page_unlock_queues();
1343 printf("avoided corruption bug in bogus_page/brelse code\n");
1345 resid -= PAGE_SIZE - (foff & PAGE_MASK);
1346 foff = (foff + PAGE_SIZE) & ~(off_t)PAGE_MASK;
1348 VM_OBJECT_UNLOCK(obj);
1349 if (bp->b_flags & (B_INVAL | B_RELBUF))
1350 vfs_vmio_release(bp);
1352 } else if (bp->b_flags & B_VMIO) {
1354 if (bp->b_flags & (B_INVAL | B_RELBUF)) {
1355 vfs_vmio_release(bp);
1358 } else if ((bp->b_flags & (B_INVAL | B_RELBUF)) != 0) {
1359 if (bp->b_bufsize != 0)
1361 if (bp->b_vp != NULL)
1365 if (BUF_LOCKRECURSED(bp)) {
1366 /* do not release to free list */
1373 /* Handle delayed bremfree() processing. */
1374 if (bp->b_flags & B_REMFREE)
1376 if (bp->b_qindex != QUEUE_NONE)
1377 panic("brelse: free buffer onto another queue???");
1380 * If the buffer has junk contents signal it and eventually
1381 * clean up B_DELWRI and diassociate the vnode so that gbincore()
1384 if (bp->b_bufsize == 0 || (bp->b_ioflags & BIO_ERROR) != 0 ||
1385 (bp->b_flags & (B_INVAL | B_NOCACHE | B_RELBUF)) != 0)
1386 bp->b_flags |= B_INVAL;
1387 if (bp->b_flags & B_INVAL) {
1388 if (bp->b_flags & B_DELWRI)
1394 /* buffers with no memory */
1395 if (bp->b_bufsize == 0) {
1396 bp->b_xflags &= ~(BX_BKGRDWRITE | BX_ALTDATA);
1397 if (bp->b_vflags & BV_BKGRDINPROG)
1398 panic("losing buffer 1");
1399 if (bp->b_kvasize) {
1400 bp->b_qindex = QUEUE_EMPTYKVA;
1402 bp->b_qindex = QUEUE_EMPTY;
1404 TAILQ_INSERT_HEAD(&bufqueues[bp->b_qindex], bp, b_freelist);
1405 /* buffers with junk contents */
1406 } else if (bp->b_flags & (B_INVAL | B_NOCACHE | B_RELBUF) ||
1407 (bp->b_ioflags & BIO_ERROR)) {
1408 bp->b_xflags &= ~(BX_BKGRDWRITE | BX_ALTDATA);
1409 if (bp->b_vflags & BV_BKGRDINPROG)
1410 panic("losing buffer 2");
1411 bp->b_qindex = QUEUE_CLEAN;
1412 TAILQ_INSERT_HEAD(&bufqueues[QUEUE_CLEAN], bp, b_freelist);
1413 /* remaining buffers */
1415 if ((bp->b_flags & (B_DELWRI|B_NEEDSGIANT)) ==
1416 (B_DELWRI|B_NEEDSGIANT))
1417 bp->b_qindex = QUEUE_DIRTY_GIANT;
1418 else if (bp->b_flags & B_DELWRI)
1419 bp->b_qindex = QUEUE_DIRTY;
1421 bp->b_qindex = QUEUE_CLEAN;
1422 if (bp->b_flags & B_AGE)
1423 TAILQ_INSERT_HEAD(&bufqueues[bp->b_qindex], bp, b_freelist);
1425 TAILQ_INSERT_TAIL(&bufqueues[bp->b_qindex], bp, b_freelist);
1427 mtx_unlock(&bqlock);
1430 * Fixup numfreebuffers count. The bp is on an appropriate queue
1431 * unless locked. We then bump numfreebuffers if it is not B_DELWRI.
1432 * We've already handled the B_INVAL case ( B_DELWRI will be clear
1433 * if B_INVAL is set ).
1436 if (!(bp->b_flags & B_DELWRI))
1440 * Something we can maybe free or reuse
1442 if (bp->b_bufsize || bp->b_kvasize)
1445 bp->b_flags &= ~(B_ASYNC | B_NOCACHE | B_AGE | B_RELBUF | B_DIRECT);
1446 if ((bp->b_flags & B_DELWRI) == 0 && (bp->b_xflags & BX_VNDIRTY))
1447 panic("brelse: not dirty");
1453 * Release a buffer back to the appropriate queue but do not try to free
1454 * it. The buffer is expected to be used again soon.
1456 * bqrelse() is used by bdwrite() to requeue a delayed write, and used by
1457 * biodone() to requeue an async I/O on completion. It is also used when
1458 * known good buffers need to be requeued but we think we may need the data
1461 * XXX we should be able to leave the B_RELBUF hint set on completion.
1464 bqrelse(struct buf *bp)
1466 CTR3(KTR_BUF, "bqrelse(%p) vp %p flags %X", bp, bp->b_vp, bp->b_flags);
1467 KASSERT(!(bp->b_flags & (B_CLUSTER|B_PAGING)),
1468 ("bqrelse: inappropriate B_PAGING or B_CLUSTER bp %p", bp));
1470 if (BUF_LOCKRECURSED(bp)) {
1471 /* do not release to free list */
1476 if (bp->b_flags & B_MANAGED) {
1477 if (bp->b_flags & B_REMFREE) {
1480 mtx_unlock(&bqlock);
1482 bp->b_flags &= ~(B_ASYNC | B_NOCACHE | B_AGE | B_RELBUF);
1488 /* Handle delayed bremfree() processing. */
1489 if (bp->b_flags & B_REMFREE)
1491 if (bp->b_qindex != QUEUE_NONE)
1492 panic("bqrelse: free buffer onto another queue???");
1493 /* buffers with stale but valid contents */
1494 if (bp->b_flags & B_DELWRI) {
1495 if (bp->b_flags & B_NEEDSGIANT)
1496 bp->b_qindex = QUEUE_DIRTY_GIANT;
1498 bp->b_qindex = QUEUE_DIRTY;
1499 TAILQ_INSERT_TAIL(&bufqueues[bp->b_qindex], bp, b_freelist);
1502 * The locking of the BO_LOCK for checking of the
1503 * BV_BKGRDINPROG is not necessary since the
1504 * BV_BKGRDINPROG cannot be set while we hold the buf
1505 * lock, it can only be cleared if it is already
1508 if (!buf_vm_page_count_severe() || (bp->b_vflags & BV_BKGRDINPROG)) {
1509 bp->b_qindex = QUEUE_CLEAN;
1510 TAILQ_INSERT_TAIL(&bufqueues[QUEUE_CLEAN], bp,
1514 * We are too low on memory, we have to try to free
1515 * the buffer (most importantly: the wired pages
1516 * making up its backing store) *now*.
1518 mtx_unlock(&bqlock);
1523 mtx_unlock(&bqlock);
1525 if ((bp->b_flags & B_INVAL) || !(bp->b_flags & B_DELWRI))
1529 * Something we can maybe free or reuse.
1531 if (bp->b_bufsize && !(bp->b_flags & B_DELWRI))
1534 bp->b_flags &= ~(B_ASYNC | B_NOCACHE | B_AGE | B_RELBUF);
1535 if ((bp->b_flags & B_DELWRI) == 0 && (bp->b_xflags & BX_VNDIRTY))
1536 panic("bqrelse: not dirty");
1541 /* Give pages used by the bp back to the VM system (where possible) */
1543 vfs_vmio_release(struct buf *bp)
1548 VM_OBJECT_LOCK(bp->b_bufobj->bo_object);
1549 vm_page_lock_queues();
1550 for (i = 0; i < bp->b_npages; i++) {
1552 bp->b_pages[i] = NULL;
1554 * In order to keep page LRU ordering consistent, put
1555 * everything on the inactive queue.
1557 vm_page_unwire(m, 0);
1559 * We don't mess with busy pages, it is
1560 * the responsibility of the process that
1561 * busied the pages to deal with them.
1563 if ((m->oflags & VPO_BUSY) || (m->busy != 0))
1566 if (m->wire_count == 0) {
1568 * Might as well free the page if we can and it has
1569 * no valid data. We also free the page if the
1570 * buffer was used for direct I/O
1572 if ((bp->b_flags & B_ASYNC) == 0 && !m->valid &&
1573 m->hold_count == 0) {
1575 } else if (bp->b_flags & B_DIRECT) {
1576 vm_page_try_to_free(m);
1577 } else if (buf_vm_page_count_severe()) {
1578 vm_page_try_to_cache(m);
1582 vm_page_unlock_queues();
1583 VM_OBJECT_UNLOCK(bp->b_bufobj->bo_object);
1584 pmap_qremove(trunc_page((vm_offset_t) bp->b_data), bp->b_npages);
1586 if (bp->b_bufsize) {
1591 bp->b_flags &= ~B_VMIO;
1597 * Check to see if a block at a particular lbn is available for a clustered
1601 vfs_bio_clcheck(struct vnode *vp, int size, daddr_t lblkno, daddr_t blkno)
1608 /* If the buf isn't in core skip it */
1609 if ((bpa = gbincore(&vp->v_bufobj, lblkno)) == NULL)
1612 /* If the buf is busy we don't want to wait for it */
1613 if (BUF_LOCK(bpa, LK_EXCLUSIVE | LK_NOWAIT, NULL) != 0)
1616 /* Only cluster with valid clusterable delayed write buffers */
1617 if ((bpa->b_flags & (B_DELWRI | B_CLUSTEROK | B_INVAL)) !=
1618 (B_DELWRI | B_CLUSTEROK))
1621 if (bpa->b_bufsize != size)
1625 * Check to see if it is in the expected place on disk and that the
1626 * block has been mapped.
1628 if ((bpa->b_blkno != bpa->b_lblkno) && (bpa->b_blkno == blkno))
1638 * Implement clustered async writes for clearing out B_DELWRI buffers.
1639 * This is much better then the old way of writing only one buffer at
1640 * a time. Note that we may not be presented with the buffers in the
1641 * correct order, so we search for the cluster in both directions.
1644 vfs_bio_awrite(struct buf *bp)
1649 daddr_t lblkno = bp->b_lblkno;
1650 struct vnode *vp = bp->b_vp;
1658 * right now we support clustered writing only to regular files. If
1659 * we find a clusterable block we could be in the middle of a cluster
1660 * rather then at the beginning.
1662 if ((vp->v_type == VREG) &&
1663 (vp->v_mount != 0) && /* Only on nodes that have the size info */
1664 (bp->b_flags & (B_CLUSTEROK | B_INVAL)) == B_CLUSTEROK) {
1666 size = vp->v_mount->mnt_stat.f_iosize;
1667 maxcl = MAXPHYS / size;
1670 for (i = 1; i < maxcl; i++)
1671 if (vfs_bio_clcheck(vp, size, lblkno + i,
1672 bp->b_blkno + ((i * size) >> DEV_BSHIFT)) == 0)
1675 for (j = 1; i + j <= maxcl && j <= lblkno; j++)
1676 if (vfs_bio_clcheck(vp, size, lblkno - j,
1677 bp->b_blkno - ((j * size) >> DEV_BSHIFT)) == 0)
1683 * this is a possible cluster write
1687 nwritten = cluster_wbuild(vp, size, lblkno - j, ncl);
1692 bp->b_flags |= B_ASYNC;
1694 * default (old) behavior, writing out only one block
1696 * XXX returns b_bufsize instead of b_bcount for nwritten?
1698 nwritten = bp->b_bufsize;
1707 * Find and initialize a new buffer header, freeing up existing buffers
1708 * in the bufqueues as necessary. The new buffer is returned locked.
1710 * Important: B_INVAL is not set. If the caller wishes to throw the
1711 * buffer away, the caller must set B_INVAL prior to calling brelse().
1714 * We have insufficient buffer headers
1715 * We have insufficient buffer space
1716 * buffer_map is too fragmented ( space reservation fails )
1717 * If we have to flush dirty buffers ( but we try to avoid this )
1719 * To avoid VFS layer recursion we do not flush dirty buffers ourselves.
1720 * Instead we ask the buf daemon to do it for us. We attempt to
1721 * avoid piecemeal wakeups of the pageout daemon.
1725 getnewbuf(struct vnode *vp, int slpflag, int slptimeo, int size, int maxsize,
1733 static int flushingbufs;
1737 * We can't afford to block since we might be holding a vnode lock,
1738 * which may prevent system daemons from running. We deal with
1739 * low-memory situations by proactively returning memory and running
1740 * async I/O rather then sync I/O.
1742 atomic_add_int(&getnewbufcalls, 1);
1743 atomic_subtract_int(&getnewbufrestarts, 1);
1745 atomic_add_int(&getnewbufrestarts, 1);
1748 * Setup for scan. If we do not have enough free buffers,
1749 * we setup a degenerate case that immediately fails. Note
1750 * that if we are specially marked process, we are allowed to
1751 * dip into our reserves.
1753 * The scanning sequence is nominally: EMPTY->EMPTYKVA->CLEAN
1755 * We start with EMPTYKVA. If the list is empty we backup to EMPTY.
1756 * However, there are a number of cases (defragging, reusing, ...)
1757 * where we cannot backup.
1760 nqindex = QUEUE_EMPTYKVA;
1761 nbp = TAILQ_FIRST(&bufqueues[QUEUE_EMPTYKVA]);
1765 * If no EMPTYKVA buffers and we are either
1766 * defragging or reusing, locate a CLEAN buffer
1767 * to free or reuse. If bufspace useage is low
1768 * skip this step so we can allocate a new buffer.
1770 if (defrag || bufspace >= lobufspace) {
1771 nqindex = QUEUE_CLEAN;
1772 nbp = TAILQ_FIRST(&bufqueues[QUEUE_CLEAN]);
1776 * If we could not find or were not allowed to reuse a
1777 * CLEAN buffer, check to see if it is ok to use an EMPTY
1778 * buffer. We can only use an EMPTY buffer if allocating
1779 * its KVA would not otherwise run us out of buffer space.
1781 if (nbp == NULL && defrag == 0 &&
1782 bufspace + maxsize < hibufspace) {
1783 nqindex = QUEUE_EMPTY;
1784 nbp = TAILQ_FIRST(&bufqueues[QUEUE_EMPTY]);
1789 * Run scan, possibly freeing data and/or kva mappings on the fly
1793 while ((bp = nbp) != NULL) {
1794 int qindex = nqindex;
1797 * Calculate next bp ( we can only use it if we do not block
1798 * or do other fancy things ).
1800 if ((nbp = TAILQ_NEXT(bp, b_freelist)) == NULL) {
1803 nqindex = QUEUE_EMPTYKVA;
1804 if ((nbp = TAILQ_FIRST(&bufqueues[QUEUE_EMPTYKVA])))
1807 case QUEUE_EMPTYKVA:
1808 nqindex = QUEUE_CLEAN;
1809 if ((nbp = TAILQ_FIRST(&bufqueues[QUEUE_CLEAN])))
1820 * If we are defragging then we need a buffer with
1821 * b_kvasize != 0. XXX this situation should no longer
1822 * occur, if defrag is non-zero the buffer's b_kvasize
1823 * should also be non-zero at this point. XXX
1825 if (defrag && bp->b_kvasize == 0) {
1826 printf("Warning: defrag empty buffer %p\n", bp);
1831 * Start freeing the bp. This is somewhat involved. nbp
1832 * remains valid only for QUEUE_EMPTY[KVA] bp's.
1834 if (BUF_LOCK(bp, LK_EXCLUSIVE | LK_NOWAIT, NULL) != 0)
1837 BO_LOCK(bp->b_bufobj);
1838 if (bp->b_vflags & BV_BKGRDINPROG) {
1839 BO_UNLOCK(bp->b_bufobj);
1843 BO_UNLOCK(bp->b_bufobj);
1846 "getnewbuf(%p) vp %p flags %X kvasize %d bufsize %d "
1847 "queue %d (recycling)", bp, bp->b_vp, bp->b_flags,
1848 bp->b_kvasize, bp->b_bufsize, qindex);
1853 KASSERT(bp->b_qindex == qindex, ("getnewbuf: inconsistant queue %d bp %p", qindex, bp));
1856 * Note: we no longer distinguish between VMIO and non-VMIO
1860 KASSERT((bp->b_flags & B_DELWRI) == 0, ("delwri buffer %p found in queue %d", bp, qindex));
1863 mtx_unlock(&bqlock);
1865 if (qindex == QUEUE_CLEAN) {
1866 if (bp->b_flags & B_VMIO) {
1867 bp->b_flags &= ~B_ASYNC;
1868 vfs_vmio_release(bp);
1875 * NOTE: nbp is now entirely invalid. We can only restart
1876 * the scan from this point on.
1878 * Get the rest of the buffer freed up. b_kva* is still
1879 * valid after this operation.
1882 if (bp->b_rcred != NOCRED) {
1883 crfree(bp->b_rcred);
1884 bp->b_rcred = NOCRED;
1886 if (bp->b_wcred != NOCRED) {
1887 crfree(bp->b_wcred);
1888 bp->b_wcred = NOCRED;
1890 if (!LIST_EMPTY(&bp->b_dep))
1892 if (bp->b_vflags & BV_BKGRDINPROG)
1893 panic("losing buffer 3");
1894 KASSERT(bp->b_vp == NULL,
1895 ("bp: %p still has vnode %p. qindex: %d",
1896 bp, bp->b_vp, qindex));
1897 KASSERT((bp->b_xflags & (BX_VNCLEAN|BX_VNDIRTY)) == 0,
1898 ("bp: %p still on a buffer list. xflags %X",
1909 bp->b_blkno = bp->b_lblkno = 0;
1910 bp->b_offset = NOOFFSET;
1916 bp->b_dirtyoff = bp->b_dirtyend = 0;
1917 bp->b_bufobj = NULL;
1918 bp->b_pin_count = 0;
1919 bp->b_fsprivate1 = NULL;
1920 bp->b_fsprivate2 = NULL;
1921 bp->b_fsprivate3 = NULL;
1923 LIST_INIT(&bp->b_dep);
1926 * If we are defragging then free the buffer.
1929 bp->b_flags |= B_INVAL;
1937 * Notify any waiters for the buffer lock about
1938 * identity change by freeing the buffer.
1940 if (qindex == QUEUE_CLEAN && BUF_LOCKWAITERS(bp)) {
1941 bp->b_flags |= B_INVAL;
1948 * If we are overcomitted then recover the buffer and its
1949 * KVM space. This occurs in rare situations when multiple
1950 * processes are blocked in getnewbuf() or allocbuf().
1952 if (bufspace >= hibufspace)
1954 if (flushingbufs && bp->b_kvasize != 0) {
1955 bp->b_flags |= B_INVAL;
1960 if (bufspace < lobufspace)
1966 * If we exhausted our list, sleep as appropriate. We may have to
1967 * wakeup various daemons and write out some dirty buffers.
1969 * Generally we are sleeping due to insufficient buffer space.
1973 int flags, norunbuf;
1978 flags = VFS_BIO_NEED_BUFSPACE;
1980 } else if (bufspace >= hibufspace) {
1982 flags = VFS_BIO_NEED_BUFSPACE;
1985 flags = VFS_BIO_NEED_ANY;
1988 needsbuffer |= flags;
1989 mtx_unlock(&nblock);
1990 mtx_unlock(&bqlock);
1992 bd_speedup(); /* heeeelp */
1993 if (gbflags & GB_NOWAIT_BD)
1997 while (needsbuffer & flags) {
1998 if (vp != NULL && (td->td_pflags & TDP_BUFNEED) == 0) {
1999 mtx_unlock(&nblock);
2001 * getblk() is called with a vnode
2002 * locked, and some majority of the
2003 * dirty buffers may as well belong to
2004 * the vnode. Flushing the buffers
2005 * there would make a progress that
2006 * cannot be achieved by the
2007 * buf_daemon, that cannot lock the
2010 norunbuf = ~(TDP_BUFNEED | TDP_NORUNNINGBUF) |
2011 (td->td_pflags & TDP_NORUNNINGBUF);
2012 /* play bufdaemon */
2013 td->td_pflags |= TDP_BUFNEED | TDP_NORUNNINGBUF;
2014 fl = buf_do_flush(vp);
2015 td->td_pflags &= norunbuf;
2019 if ((needsbuffer & flags) == 0)
2022 if (msleep(&needsbuffer, &nblock,
2023 (PRIBIO + 4) | slpflag, waitmsg, slptimeo)) {
2024 mtx_unlock(&nblock);
2028 mtx_unlock(&nblock);
2031 * We finally have a valid bp. We aren't quite out of the
2032 * woods, we still have to reserve kva space. In order
2033 * to keep fragmentation sane we only allocate kva in
2036 maxsize = (maxsize + BKVAMASK) & ~BKVAMASK;
2038 if (maxsize != bp->b_kvasize) {
2039 vm_offset_t addr = 0;
2043 vm_map_lock(buffer_map);
2044 if (vm_map_findspace(buffer_map,
2045 vm_map_min(buffer_map), maxsize, &addr)) {
2047 * Uh oh. Buffer map is to fragmented. We
2048 * must defragment the map.
2050 atomic_add_int(&bufdefragcnt, 1);
2051 vm_map_unlock(buffer_map);
2053 bp->b_flags |= B_INVAL;
2058 vm_map_insert(buffer_map, NULL, 0,
2059 addr, addr + maxsize,
2060 VM_PROT_ALL, VM_PROT_ALL, MAP_NOFAULT);
2062 bp->b_kvabase = (caddr_t) addr;
2063 bp->b_kvasize = maxsize;
2064 atomic_add_long(&bufspace, bp->b_kvasize);
2065 atomic_add_int(&bufreusecnt, 1);
2067 vm_map_unlock(buffer_map);
2069 bp->b_saveaddr = bp->b_kvabase;
2070 bp->b_data = bp->b_saveaddr;
2078 * buffer flushing daemon. Buffers are normally flushed by the
2079 * update daemon but if it cannot keep up this process starts to
2080 * take the load in an attempt to prevent getnewbuf() from blocking.
2083 static struct kproc_desc buf_kp = {
2088 SYSINIT(bufdaemon, SI_SUB_KTHREAD_BUF, SI_ORDER_FIRST, kproc_start, &buf_kp);
2091 buf_do_flush(struct vnode *vp)
2095 flushed = flushbufqueues(vp, QUEUE_DIRTY, 0);
2096 /* The list empty check here is slightly racy */
2097 if (!TAILQ_EMPTY(&bufqueues[QUEUE_DIRTY_GIANT])) {
2099 flushed += flushbufqueues(vp, QUEUE_DIRTY_GIANT, 0);
2104 * Could not find any buffers without rollback
2105 * dependencies, so just write the first one
2106 * in the hopes of eventually making progress.
2108 flushbufqueues(vp, QUEUE_DIRTY, 1);
2110 &bufqueues[QUEUE_DIRTY_GIANT])) {
2112 flushbufqueues(vp, QUEUE_DIRTY_GIANT, 1);
2124 * This process needs to be suspended prior to shutdown sync.
2126 EVENTHANDLER_REGISTER(shutdown_pre_sync, kproc_shutdown, bufdaemonproc,
2130 * This process is allowed to take the buffer cache to the limit
2132 curthread->td_pflags |= TDP_NORUNNINGBUF | TDP_BUFNEED;
2136 mtx_unlock(&bdlock);
2138 kproc_suspend_check(bufdaemonproc);
2141 * Do the flush. Limit the amount of in-transit I/O we
2142 * allow to build up, otherwise we would completely saturate
2143 * the I/O system. Wakeup any waiting processes before we
2144 * normally would so they can run in parallel with our drain.
2146 while (numdirtybuffers > lodirtybuffers) {
2147 if (buf_do_flush(NULL) == 0)
2153 * Only clear bd_request if we have reached our low water
2154 * mark. The buf_daemon normally waits 1 second and
2155 * then incrementally flushes any dirty buffers that have
2156 * built up, within reason.
2158 * If we were unable to hit our low water mark and couldn't
2159 * find any flushable buffers, we sleep half a second.
2160 * Otherwise we loop immediately.
2163 if (numdirtybuffers <= lodirtybuffers) {
2165 * We reached our low water mark, reset the
2166 * request and sleep until we are needed again.
2167 * The sleep is just so the suspend code works.
2170 msleep(&bd_request, &bdlock, PVM, "psleep", hz);
2173 * We couldn't find any flushable dirty buffers but
2174 * still have too many dirty buffers, we
2175 * have to sleep and try again. (rare)
2177 msleep(&bd_request, &bdlock, PVM, "qsleep", hz / 10);
2185 * Try to flush a buffer in the dirty queue. We must be careful to
2186 * free up B_INVAL buffers instead of write them, which NFS is
2187 * particularly sensitive to.
2189 static int flushwithdeps = 0;
2190 SYSCTL_INT(_vfs, OID_AUTO, flushwithdeps, CTLFLAG_RW, &flushwithdeps,
2191 0, "Number of buffers flushed with dependecies that require rollbacks");
2194 flushbufqueues(struct vnode *lvp, int queue, int flushdeps)
2196 struct buf *sentinel;
2205 target = numdirtybuffers - lodirtybuffers;
2206 if (flushdeps && target > 2)
2209 target = flushbufqtarget;
2212 sentinel = malloc(sizeof(struct buf), M_TEMP, M_WAITOK | M_ZERO);
2213 sentinel->b_qindex = QUEUE_SENTINEL;
2215 TAILQ_INSERT_HEAD(&bufqueues[queue], sentinel, b_freelist);
2216 while (flushed != target) {
2217 bp = TAILQ_NEXT(sentinel, b_freelist);
2219 TAILQ_REMOVE(&bufqueues[queue], sentinel, b_freelist);
2220 TAILQ_INSERT_AFTER(&bufqueues[queue], bp, sentinel,
2225 * Skip sentinels inserted by other invocations of the
2226 * flushbufqueues(), taking care to not reorder them.
2228 if (bp->b_qindex == QUEUE_SENTINEL)
2231 * Only flush the buffers that belong to the
2232 * vnode locked by the curthread.
2234 if (lvp != NULL && bp->b_vp != lvp)
2236 if (BUF_LOCK(bp, LK_EXCLUSIVE | LK_NOWAIT, NULL) != 0)
2238 if (bp->b_pin_count > 0) {
2242 BO_LOCK(bp->b_bufobj);
2243 if ((bp->b_vflags & BV_BKGRDINPROG) != 0 ||
2244 (bp->b_flags & B_DELWRI) == 0) {
2245 BO_UNLOCK(bp->b_bufobj);
2249 BO_UNLOCK(bp->b_bufobj);
2250 if (bp->b_flags & B_INVAL) {
2252 mtx_unlock(&bqlock);
2255 numdirtywakeup((lodirtybuffers + hidirtybuffers) / 2);
2260 if (!LIST_EMPTY(&bp->b_dep) && buf_countdeps(bp, 0)) {
2261 if (flushdeps == 0) {
2269 * We must hold the lock on a vnode before writing
2270 * one of its buffers. Otherwise we may confuse, or
2271 * in the case of a snapshot vnode, deadlock the
2274 * The lock order here is the reverse of the normal
2275 * of vnode followed by buf lock. This is ok because
2276 * the NOWAIT will prevent deadlock.
2279 if (vn_start_write(vp, &mp, V_NOWAIT) != 0) {
2283 if (vn_lock(vp, LK_EXCLUSIVE | LK_NOWAIT | LK_CANRECURSE) == 0) {
2284 mtx_unlock(&bqlock);
2285 CTR3(KTR_BUF, "flushbufqueue(%p) vp %p flags %X",
2286 bp, bp->b_vp, bp->b_flags);
2287 if (curproc == bufdaemonproc)
2294 vn_finished_write(mp);
2296 flushwithdeps += hasdeps;
2300 * Sleeping on runningbufspace while holding
2301 * vnode lock leads to deadlock.
2303 if (curproc == bufdaemonproc)
2304 waitrunningbufspace();
2305 numdirtywakeup((lodirtybuffers + hidirtybuffers) / 2);
2309 vn_finished_write(mp);
2312 TAILQ_REMOVE(&bufqueues[queue], sentinel, b_freelist);
2313 mtx_unlock(&bqlock);
2314 free(sentinel, M_TEMP);
2319 * Check to see if a block is currently memory resident.
2322 incore(struct bufobj *bo, daddr_t blkno)
2327 bp = gbincore(bo, blkno);
2333 * Returns true if no I/O is needed to access the
2334 * associated VM object. This is like incore except
2335 * it also hunts around in the VM system for the data.
2339 inmem(struct vnode * vp, daddr_t blkno)
2342 vm_offset_t toff, tinc, size;
2346 ASSERT_VOP_LOCKED(vp, "inmem");
2348 if (incore(&vp->v_bufobj, blkno))
2350 if (vp->v_mount == NULL)
2357 if (size > vp->v_mount->mnt_stat.f_iosize)
2358 size = vp->v_mount->mnt_stat.f_iosize;
2359 off = (vm_ooffset_t)blkno * (vm_ooffset_t)vp->v_mount->mnt_stat.f_iosize;
2361 VM_OBJECT_LOCK(obj);
2362 for (toff = 0; toff < vp->v_mount->mnt_stat.f_iosize; toff += tinc) {
2363 m = vm_page_lookup(obj, OFF_TO_IDX(off + toff));
2367 if (tinc > PAGE_SIZE - ((toff + off) & PAGE_MASK))
2368 tinc = PAGE_SIZE - ((toff + off) & PAGE_MASK);
2369 if (vm_page_is_valid(m,
2370 (vm_offset_t) ((toff + off) & PAGE_MASK), tinc) == 0)
2373 VM_OBJECT_UNLOCK(obj);
2377 VM_OBJECT_UNLOCK(obj);
2382 * Set the dirty range for a buffer based on the status of the dirty
2383 * bits in the pages comprising the buffer. The range is limited
2384 * to the size of the buffer.
2386 * Tell the VM system that the pages associated with this buffer
2387 * are clean. This is used for delayed writes where the data is
2388 * going to go to disk eventually without additional VM intevention.
2390 * Note that while we only really need to clean through to b_bcount, we
2391 * just go ahead and clean through to b_bufsize.
2394 vfs_clean_pages_dirty_buf(struct buf *bp)
2396 vm_ooffset_t foff, noff, eoff;
2400 if ((bp->b_flags & B_VMIO) == 0 || bp->b_bufsize == 0)
2403 foff = bp->b_offset;
2404 KASSERT(bp->b_offset != NOOFFSET,
2405 ("vfs_clean_pages_dirty_buf: no buffer offset"));
2407 VM_OBJECT_LOCK(bp->b_bufobj->bo_object);
2408 vfs_drain_busy_pages(bp);
2409 vfs_setdirty_locked_object(bp);
2410 vm_page_lock_queues();
2411 for (i = 0; i < bp->b_npages; i++) {
2412 noff = (foff + PAGE_SIZE) & ~(off_t)PAGE_MASK;
2414 if (eoff > bp->b_offset + bp->b_bufsize)
2415 eoff = bp->b_offset + bp->b_bufsize;
2417 vfs_page_set_validclean(bp, foff, m);
2418 /* vm_page_clear_dirty(m, foff & PAGE_MASK, eoff - foff); */
2421 vm_page_unlock_queues();
2422 VM_OBJECT_UNLOCK(bp->b_bufobj->bo_object);
2426 vfs_setdirty_locked_object(struct buf *bp)
2431 object = bp->b_bufobj->bo_object;
2432 VM_OBJECT_LOCK_ASSERT(object, MA_OWNED);
2435 * We qualify the scan for modified pages on whether the
2436 * object has been flushed yet.
2438 if (object->flags & (OBJ_MIGHTBEDIRTY|OBJ_CLEANING)) {
2439 vm_offset_t boffset;
2440 vm_offset_t eoffset;
2442 vm_page_lock_queues();
2444 * test the pages to see if they have been modified directly
2445 * by users through the VM system.
2447 for (i = 0; i < bp->b_npages; i++)
2448 vm_page_test_dirty(bp->b_pages[i]);
2451 * Calculate the encompassing dirty range, boffset and eoffset,
2452 * (eoffset - boffset) bytes.
2455 for (i = 0; i < bp->b_npages; i++) {
2456 if (bp->b_pages[i]->dirty)
2459 boffset = (i << PAGE_SHIFT) - (bp->b_offset & PAGE_MASK);
2461 for (i = bp->b_npages - 1; i >= 0; --i) {
2462 if (bp->b_pages[i]->dirty) {
2466 eoffset = ((i + 1) << PAGE_SHIFT) - (bp->b_offset & PAGE_MASK);
2468 vm_page_unlock_queues();
2470 * Fit it to the buffer.
2473 if (eoffset > bp->b_bcount)
2474 eoffset = bp->b_bcount;
2477 * If we have a good dirty range, merge with the existing
2481 if (boffset < eoffset) {
2482 if (bp->b_dirtyoff > boffset)
2483 bp->b_dirtyoff = boffset;
2484 if (bp->b_dirtyend < eoffset)
2485 bp->b_dirtyend = eoffset;
2493 * Get a block given a specified block and offset into a file/device.
2494 * The buffers B_DONE bit will be cleared on return, making it almost
2495 * ready for an I/O initiation. B_INVAL may or may not be set on
2496 * return. The caller should clear B_INVAL prior to initiating a
2499 * For a non-VMIO buffer, B_CACHE is set to the opposite of B_INVAL for
2500 * an existing buffer.
2502 * For a VMIO buffer, B_CACHE is modified according to the backing VM.
2503 * If getblk()ing a previously 0-sized invalid buffer, B_CACHE is set
2504 * and then cleared based on the backing VM. If the previous buffer is
2505 * non-0-sized but invalid, B_CACHE will be cleared.
2507 * If getblk() must create a new buffer, the new buffer is returned with
2508 * both B_INVAL and B_CACHE clear unless it is a VMIO buffer, in which
2509 * case it is returned with B_INVAL clear and B_CACHE set based on the
2512 * getblk() also forces a bwrite() for any B_DELWRI buffer whos
2513 * B_CACHE bit is clear.
2515 * What this means, basically, is that the caller should use B_CACHE to
2516 * determine whether the buffer is fully valid or not and should clear
2517 * B_INVAL prior to issuing a read. If the caller intends to validate
2518 * the buffer by loading its data area with something, the caller needs
2519 * to clear B_INVAL. If the caller does this without issuing an I/O,
2520 * the caller should set B_CACHE ( as an optimization ), else the caller
2521 * should issue the I/O and biodone() will set B_CACHE if the I/O was
2522 * a write attempt or if it was a successfull read. If the caller
2523 * intends to issue a READ, the caller must clear B_INVAL and BIO_ERROR
2524 * prior to issuing the READ. biodone() will *not* clear B_INVAL.
2527 getblk(struct vnode * vp, daddr_t blkno, int size, int slpflag, int slptimeo,
2534 CTR3(KTR_BUF, "getblk(%p, %ld, %d)", vp, (long)blkno, size);
2535 ASSERT_VOP_LOCKED(vp, "getblk");
2536 if (size > MAXBSIZE)
2537 panic("getblk: size(%d) > MAXBSIZE(%d)\n", size, MAXBSIZE);
2542 * Block if we are low on buffers. Certain processes are allowed
2543 * to completely exhaust the buffer cache.
2545 * If this check ever becomes a bottleneck it may be better to
2546 * move it into the else, when gbincore() fails. At the moment
2547 * it isn't a problem.
2549 * XXX remove if 0 sections (clean this up after its proven)
2551 if (numfreebuffers == 0) {
2552 if (TD_IS_IDLETHREAD(curthread))
2555 needsbuffer |= VFS_BIO_NEED_ANY;
2556 mtx_unlock(&nblock);
2560 bp = gbincore(bo, blkno);
2564 * Buffer is in-core. If the buffer is not busy, it must
2567 lockflags = LK_EXCLUSIVE | LK_SLEEPFAIL | LK_INTERLOCK;
2569 if (flags & GB_LOCK_NOWAIT)
2570 lockflags |= LK_NOWAIT;
2572 error = BUF_TIMELOCK(bp, lockflags,
2573 BO_MTX(bo), "getblk", slpflag, slptimeo);
2576 * If we slept and got the lock we have to restart in case
2577 * the buffer changed identities.
2579 if (error == ENOLCK)
2581 /* We timed out or were interrupted. */
2586 * The buffer is locked. B_CACHE is cleared if the buffer is
2587 * invalid. Otherwise, for a non-VMIO buffer, B_CACHE is set
2588 * and for a VMIO buffer B_CACHE is adjusted according to the
2591 if (bp->b_flags & B_INVAL)
2592 bp->b_flags &= ~B_CACHE;
2593 else if ((bp->b_flags & (B_VMIO | B_INVAL)) == 0)
2594 bp->b_flags |= B_CACHE;
2598 * check for size inconsistancies for non-VMIO case.
2601 if (bp->b_bcount != size) {
2602 if ((bp->b_flags & B_VMIO) == 0 ||
2603 (size > bp->b_kvasize)) {
2604 if (bp->b_flags & B_DELWRI) {
2606 * If buffer is pinned and caller does
2607 * not want sleep waiting for it to be
2608 * unpinned, bail out
2610 if (bp->b_pin_count > 0) {
2611 if (flags & GB_LOCK_NOWAIT) {
2618 bp->b_flags |= B_NOCACHE;
2621 if (LIST_EMPTY(&bp->b_dep)) {
2622 bp->b_flags |= B_RELBUF;
2625 bp->b_flags |= B_NOCACHE;
2634 * If the size is inconsistant in the VMIO case, we can resize
2635 * the buffer. This might lead to B_CACHE getting set or
2636 * cleared. If the size has not changed, B_CACHE remains
2637 * unchanged from its previous state.
2640 if (bp->b_bcount != size)
2643 KASSERT(bp->b_offset != NOOFFSET,
2644 ("getblk: no buffer offset"));
2647 * A buffer with B_DELWRI set and B_CACHE clear must
2648 * be committed before we can return the buffer in
2649 * order to prevent the caller from issuing a read
2650 * ( due to B_CACHE not being set ) and overwriting
2653 * Most callers, including NFS and FFS, need this to
2654 * operate properly either because they assume they
2655 * can issue a read if B_CACHE is not set, or because
2656 * ( for example ) an uncached B_DELWRI might loop due
2657 * to softupdates re-dirtying the buffer. In the latter
2658 * case, B_CACHE is set after the first write completes,
2659 * preventing further loops.
2660 * NOTE! b*write() sets B_CACHE. If we cleared B_CACHE
2661 * above while extending the buffer, we cannot allow the
2662 * buffer to remain with B_CACHE set after the write
2663 * completes or it will represent a corrupt state. To
2664 * deal with this we set B_NOCACHE to scrap the buffer
2667 * We might be able to do something fancy, like setting
2668 * B_CACHE in bwrite() except if B_DELWRI is already set,
2669 * so the below call doesn't set B_CACHE, but that gets real
2670 * confusing. This is much easier.
2673 if ((bp->b_flags & (B_CACHE|B_DELWRI)) == B_DELWRI) {
2674 bp->b_flags |= B_NOCACHE;
2678 bp->b_flags &= ~B_DONE;
2680 int bsize, maxsize, vmio;
2684 * Buffer is not in-core, create new buffer. The buffer
2685 * returned by getnewbuf() is locked. Note that the returned
2686 * buffer is also considered valid (not marked B_INVAL).
2690 * If the user does not want us to create the buffer, bail out
2693 if (flags & GB_NOCREAT)
2695 bsize = vn_isdisk(vp, NULL) ? DEV_BSIZE : bo->bo_bsize;
2696 offset = blkno * bsize;
2697 vmio = vp->v_object != NULL;
2698 maxsize = vmio ? size + (offset & PAGE_MASK) : size;
2699 maxsize = imax(maxsize, bsize);
2701 bp = getnewbuf(vp, slpflag, slptimeo, size, maxsize, flags);
2703 if (slpflag || slptimeo)
2709 * This code is used to make sure that a buffer is not
2710 * created while the getnewbuf routine is blocked.
2711 * This can be a problem whether the vnode is locked or not.
2712 * If the buffer is created out from under us, we have to
2713 * throw away the one we just created.
2715 * Note: this must occur before we associate the buffer
2716 * with the vp especially considering limitations in
2717 * the splay tree implementation when dealing with duplicate
2721 if (gbincore(bo, blkno)) {
2723 bp->b_flags |= B_INVAL;
2729 * Insert the buffer into the hash, so that it can
2730 * be found by incore.
2732 bp->b_blkno = bp->b_lblkno = blkno;
2733 bp->b_offset = offset;
2738 * set B_VMIO bit. allocbuf() the buffer bigger. Since the
2739 * buffer size starts out as 0, B_CACHE will be set by
2740 * allocbuf() for the VMIO case prior to it testing the
2741 * backing store for validity.
2745 bp->b_flags |= B_VMIO;
2746 #if defined(VFS_BIO_DEBUG)
2747 if (vn_canvmio(vp) != TRUE)
2748 printf("getblk: VMIO on vnode type %d\n",
2751 KASSERT(vp->v_object == bp->b_bufobj->bo_object,
2752 ("ARGH! different b_bufobj->bo_object %p %p %p\n",
2753 bp, vp->v_object, bp->b_bufobj->bo_object));
2755 bp->b_flags &= ~B_VMIO;
2756 KASSERT(bp->b_bufobj->bo_object == NULL,
2757 ("ARGH! has b_bufobj->bo_object %p %p\n",
2758 bp, bp->b_bufobj->bo_object));
2762 bp->b_flags &= ~B_DONE;
2764 CTR4(KTR_BUF, "getblk(%p, %ld, %d) = %p", vp, (long)blkno, size, bp);
2765 BUF_ASSERT_HELD(bp);
2766 KASSERT(bp->b_bufobj == bo,
2767 ("bp %p wrong b_bufobj %p should be %p", bp, bp->b_bufobj, bo));
2772 * Get an empty, disassociated buffer of given size. The buffer is initially
2776 geteblk(int size, int flags)
2781 maxsize = (size + BKVAMASK) & ~BKVAMASK;
2782 while ((bp = getnewbuf(NULL, 0, 0, size, maxsize, flags)) == NULL) {
2783 if ((flags & GB_NOWAIT_BD) &&
2784 (curthread->td_pflags & TDP_BUFNEED) != 0)
2788 bp->b_flags |= B_INVAL; /* b_dep cleared by getnewbuf() */
2789 BUF_ASSERT_HELD(bp);
2795 * This code constitutes the buffer memory from either anonymous system
2796 * memory (in the case of non-VMIO operations) or from an associated
2797 * VM object (in the case of VMIO operations). This code is able to
2798 * resize a buffer up or down.
2800 * Note that this code is tricky, and has many complications to resolve
2801 * deadlock or inconsistant data situations. Tread lightly!!!
2802 * There are B_CACHE and B_DELWRI interactions that must be dealt with by
2803 * the caller. Calling this code willy nilly can result in the loss of data.
2805 * allocbuf() only adjusts B_CACHE for VMIO buffers. getblk() deals with
2806 * B_CACHE for the non-VMIO case.
2810 allocbuf(struct buf *bp, int size)
2812 int newbsize, mbsize;
2815 BUF_ASSERT_HELD(bp);
2817 if (bp->b_kvasize < size)
2818 panic("allocbuf: buffer too small");
2820 if ((bp->b_flags & B_VMIO) == 0) {
2824 * Just get anonymous memory from the kernel. Don't
2825 * mess with B_CACHE.
2827 mbsize = (size + DEV_BSIZE - 1) & ~(DEV_BSIZE - 1);
2828 if (bp->b_flags & B_MALLOC)
2831 newbsize = round_page(size);
2833 if (newbsize < bp->b_bufsize) {
2835 * malloced buffers are not shrunk
2837 if (bp->b_flags & B_MALLOC) {
2839 bp->b_bcount = size;
2841 free(bp->b_data, M_BIOBUF);
2842 if (bp->b_bufsize) {
2843 atomic_subtract_long(
2849 bp->b_saveaddr = bp->b_kvabase;
2850 bp->b_data = bp->b_saveaddr;
2852 bp->b_flags &= ~B_MALLOC;
2858 (vm_offset_t) bp->b_data + newbsize,
2859 (vm_offset_t) bp->b_data + bp->b_bufsize);
2860 } else if (newbsize > bp->b_bufsize) {
2862 * We only use malloced memory on the first allocation.
2863 * and revert to page-allocated memory when the buffer
2867 * There is a potential smp race here that could lead
2868 * to bufmallocspace slightly passing the max. It
2869 * is probably extremely rare and not worth worrying
2872 if ( (bufmallocspace < maxbufmallocspace) &&
2873 (bp->b_bufsize == 0) &&
2874 (mbsize <= PAGE_SIZE/2)) {
2876 bp->b_data = malloc(mbsize, M_BIOBUF, M_WAITOK);
2877 bp->b_bufsize = mbsize;
2878 bp->b_bcount = size;
2879 bp->b_flags |= B_MALLOC;
2880 atomic_add_long(&bufmallocspace, mbsize);
2886 * If the buffer is growing on its other-than-first allocation,
2887 * then we revert to the page-allocation scheme.
2889 if (bp->b_flags & B_MALLOC) {
2890 origbuf = bp->b_data;
2891 origbufsize = bp->b_bufsize;
2892 bp->b_data = bp->b_kvabase;
2893 if (bp->b_bufsize) {
2894 atomic_subtract_long(&bufmallocspace,
2899 bp->b_flags &= ~B_MALLOC;
2900 newbsize = round_page(newbsize);
2904 (vm_offset_t) bp->b_data + bp->b_bufsize,
2905 (vm_offset_t) bp->b_data + newbsize);
2907 bcopy(origbuf, bp->b_data, origbufsize);
2908 free(origbuf, M_BIOBUF);
2914 newbsize = (size + DEV_BSIZE - 1) & ~(DEV_BSIZE - 1);
2915 desiredpages = (size == 0) ? 0 :
2916 num_pages((bp->b_offset & PAGE_MASK) + newbsize);
2918 if (bp->b_flags & B_MALLOC)
2919 panic("allocbuf: VMIO buffer can't be malloced");
2921 * Set B_CACHE initially if buffer is 0 length or will become
2924 if (size == 0 || bp->b_bufsize == 0)
2925 bp->b_flags |= B_CACHE;
2927 if (newbsize < bp->b_bufsize) {
2929 * DEV_BSIZE aligned new buffer size is less then the
2930 * DEV_BSIZE aligned existing buffer size. Figure out
2931 * if we have to remove any pages.
2933 if (desiredpages < bp->b_npages) {
2936 VM_OBJECT_LOCK(bp->b_bufobj->bo_object);
2937 vm_page_lock_queues();
2938 for (i = desiredpages; i < bp->b_npages; i++) {
2940 * the page is not freed here -- it
2941 * is the responsibility of
2942 * vnode_pager_setsize
2945 KASSERT(m != bogus_page,
2946 ("allocbuf: bogus page found"));
2947 while (vm_page_sleep_if_busy(m, TRUE, "biodep"))
2948 vm_page_lock_queues();
2950 bp->b_pages[i] = NULL;
2951 vm_page_unwire(m, 0);
2953 vm_page_unlock_queues();
2954 VM_OBJECT_UNLOCK(bp->b_bufobj->bo_object);
2955 pmap_qremove((vm_offset_t) trunc_page((vm_offset_t)bp->b_data) +
2956 (desiredpages << PAGE_SHIFT), (bp->b_npages - desiredpages));
2957 bp->b_npages = desiredpages;
2959 } else if (size > bp->b_bcount) {
2961 * We are growing the buffer, possibly in a
2962 * byte-granular fashion.
2969 * Step 1, bring in the VM pages from the object,
2970 * allocating them if necessary. We must clear
2971 * B_CACHE if these pages are not valid for the
2972 * range covered by the buffer.
2975 obj = bp->b_bufobj->bo_object;
2977 VM_OBJECT_LOCK(obj);
2978 while (bp->b_npages < desiredpages) {
2982 pi = OFF_TO_IDX(bp->b_offset) + bp->b_npages;
2983 if ((m = vm_page_lookup(obj, pi)) == NULL) {
2985 * note: must allocate system pages
2986 * since blocking here could intefere
2987 * with paging I/O, no matter which
2990 m = vm_page_alloc(obj, pi,
2991 VM_ALLOC_NOBUSY | VM_ALLOC_SYSTEM |
2994 atomic_add_int(&vm_pageout_deficit,
2995 desiredpages - bp->b_npages);
2996 VM_OBJECT_UNLOCK(obj);
2998 VM_OBJECT_LOCK(obj);
3001 bp->b_flags &= ~B_CACHE;
3002 bp->b_pages[bp->b_npages] = m;
3009 * We found a page. If we have to sleep on it,
3010 * retry because it might have gotten freed out
3013 * We can only test VPO_BUSY here. Blocking on
3014 * m->busy might lead to a deadlock:
3016 * vm_fault->getpages->cluster_read->allocbuf
3019 if (vm_page_sleep_if_busy(m, FALSE, "pgtblk"))
3023 * We have a good page.
3025 vm_page_lock_queues();
3027 vm_page_unlock_queues();
3028 bp->b_pages[bp->b_npages] = m;
3033 * Step 2. We've loaded the pages into the buffer,
3034 * we have to figure out if we can still have B_CACHE
3035 * set. Note that B_CACHE is set according to the
3036 * byte-granular range ( bcount and size ), new the
3037 * aligned range ( newbsize ).
3039 * The VM test is against m->valid, which is DEV_BSIZE
3040 * aligned. Needless to say, the validity of the data
3041 * needs to also be DEV_BSIZE aligned. Note that this
3042 * fails with NFS if the server or some other client
3043 * extends the file's EOF. If our buffer is resized,
3044 * B_CACHE may remain set! XXX
3047 toff = bp->b_bcount;
3048 tinc = PAGE_SIZE - ((bp->b_offset + toff) & PAGE_MASK);
3050 while ((bp->b_flags & B_CACHE) && toff < size) {
3053 if (tinc > (size - toff))
3056 pi = ((bp->b_offset & PAGE_MASK) + toff) >>
3069 VM_OBJECT_UNLOCK(obj);
3072 * Step 3, fixup the KVM pmap. Remember that
3073 * bp->b_data is relative to bp->b_offset, but
3074 * bp->b_offset may be offset into the first page.
3077 bp->b_data = (caddr_t)
3078 trunc_page((vm_offset_t)bp->b_data);
3080 (vm_offset_t)bp->b_data,
3085 bp->b_data = (caddr_t)((vm_offset_t)bp->b_data |
3086 (vm_offset_t)(bp->b_offset & PAGE_MASK));
3089 if (newbsize < bp->b_bufsize)
3091 bp->b_bufsize = newbsize; /* actual buffer allocation */
3092 bp->b_bcount = size; /* requested buffer size */
3097 biodone(struct bio *bp)
3100 void (*done)(struct bio *);
3102 mtxp = mtx_pool_find(mtxpool_sleep, bp);
3104 bp->bio_flags |= BIO_DONE;
3105 done = bp->bio_done;
3114 * Wait for a BIO to finish.
3116 * XXX: resort to a timeout for now. The optimal locking (if any) for this
3117 * case is not yet clear.
3120 biowait(struct bio *bp, const char *wchan)
3124 mtxp = mtx_pool_find(mtxpool_sleep, bp);
3126 while ((bp->bio_flags & BIO_DONE) == 0)
3127 msleep(bp, mtxp, PRIBIO, wchan, hz / 10);
3129 if (bp->bio_error != 0)
3130 return (bp->bio_error);
3131 if (!(bp->bio_flags & BIO_ERROR))
3137 biofinish(struct bio *bp, struct devstat *stat, int error)
3141 bp->bio_error = error;
3142 bp->bio_flags |= BIO_ERROR;
3145 devstat_end_transaction_bio(stat, bp);
3152 * Wait for buffer I/O completion, returning error status. The buffer
3153 * is left locked and B_DONE on return. B_EINTR is converted into an EINTR
3154 * error and cleared.
3157 bufwait(struct buf *bp)
3159 if (bp->b_iocmd == BIO_READ)
3160 bwait(bp, PRIBIO, "biord");
3162 bwait(bp, PRIBIO, "biowr");
3163 if (bp->b_flags & B_EINTR) {
3164 bp->b_flags &= ~B_EINTR;
3167 if (bp->b_ioflags & BIO_ERROR) {
3168 return (bp->b_error ? bp->b_error : EIO);
3175 * Call back function from struct bio back up to struct buf.
3178 bufdonebio(struct bio *bip)
3182 bp = bip->bio_caller2;
3183 bp->b_resid = bp->b_bcount - bip->bio_completed;
3184 bp->b_resid = bip->bio_resid; /* XXX: remove */
3185 bp->b_ioflags = bip->bio_flags;
3186 bp->b_error = bip->bio_error;
3188 bp->b_ioflags |= BIO_ERROR;
3194 dev_strategy(struct cdev *dev, struct buf *bp)
3200 if ((!bp->b_iocmd) || (bp->b_iocmd & (bp->b_iocmd - 1)))
3201 panic("b_iocmd botch");
3206 /* Try again later */
3207 tsleep(&bp, PRIBIO, "dev_strat", hz/10);
3209 bip->bio_cmd = bp->b_iocmd;
3210 bip->bio_offset = bp->b_iooffset;
3211 bip->bio_length = bp->b_bcount;
3212 bip->bio_bcount = bp->b_bcount; /* XXX: remove */
3213 bip->bio_data = bp->b_data;
3214 bip->bio_done = bufdonebio;
3215 bip->bio_caller2 = bp;
3217 KASSERT(dev->si_refcount > 0,
3218 ("dev_strategy on un-referenced struct cdev *(%s)",
3220 csw = dev_refthread(dev, &ref);
3223 bp->b_error = ENXIO;
3224 bp->b_ioflags = BIO_ERROR;
3228 (*csw->d_strategy)(bip);
3229 dev_relthread(dev, ref);
3235 * Finish I/O on a buffer, optionally calling a completion function.
3236 * This is usually called from an interrupt so process blocking is
3239 * biodone is also responsible for setting B_CACHE in a B_VMIO bp.
3240 * In a non-VMIO bp, B_CACHE will be set on the next getblk()
3241 * assuming B_INVAL is clear.
3243 * For the VMIO case, we set B_CACHE if the op was a read and no
3244 * read error occured, or if the op was a write. B_CACHE is never
3245 * set if the buffer is invalid or otherwise uncacheable.
3247 * biodone does not mess with B_INVAL, allowing the I/O routine or the
3248 * initiator to leave B_INVAL set to brelse the buffer out of existance
3249 * in the biodone routine.
3252 bufdone(struct buf *bp)
3254 struct bufobj *dropobj;
3255 void (*biodone)(struct buf *);
3257 CTR3(KTR_BUF, "bufdone(%p) vp %p flags %X", bp, bp->b_vp, bp->b_flags);
3260 KASSERT(!(bp->b_flags & B_DONE), ("biodone: bp %p already done", bp));
3261 BUF_ASSERT_HELD(bp);
3263 runningbufwakeup(bp);
3264 if (bp->b_iocmd == BIO_WRITE)
3265 dropobj = bp->b_bufobj;
3266 /* call optional completion function if requested */
3267 if (bp->b_iodone != NULL) {
3268 biodone = bp->b_iodone;
3269 bp->b_iodone = NULL;
3272 bufobj_wdrop(dropobj);
3279 bufobj_wdrop(dropobj);
3283 bufdone_finish(struct buf *bp)
3285 BUF_ASSERT_HELD(bp);
3287 if (!LIST_EMPTY(&bp->b_dep))
3290 if (bp->b_flags & B_VMIO) {
3296 struct vnode *vp = bp->b_vp;
3298 obj = bp->b_bufobj->bo_object;
3300 #if defined(VFS_BIO_DEBUG)
3301 mp_fixme("usecount and vflag accessed without locks.");
3302 if (vp->v_usecount == 0) {
3303 panic("biodone: zero vnode ref count");
3306 KASSERT(vp->v_object != NULL,
3307 ("biodone: vnode %p has no vm_object", vp));
3310 foff = bp->b_offset;
3311 KASSERT(bp->b_offset != NOOFFSET,
3312 ("biodone: no buffer offset"));
3314 VM_OBJECT_LOCK(obj);
3315 #if defined(VFS_BIO_DEBUG)
3316 if (obj->paging_in_progress < bp->b_npages) {
3317 printf("biodone: paging in progress(%d) < bp->b_npages(%d)\n",
3318 obj->paging_in_progress, bp->b_npages);
3323 * Set B_CACHE if the op was a normal read and no error
3324 * occured. B_CACHE is set for writes in the b*write()
3327 iosize = bp->b_bcount - bp->b_resid;
3328 if (bp->b_iocmd == BIO_READ &&
3329 !(bp->b_flags & (B_INVAL|B_NOCACHE)) &&
3330 !(bp->b_ioflags & BIO_ERROR)) {
3331 bp->b_flags |= B_CACHE;
3333 for (i = 0; i < bp->b_npages; i++) {
3337 resid = ((foff + PAGE_SIZE) & ~(off_t)PAGE_MASK) - foff;
3342 * cleanup bogus pages, restoring the originals
3345 if (m == bogus_page) {
3347 m = vm_page_lookup(obj, OFF_TO_IDX(foff));
3349 panic("biodone: page disappeared!");
3351 pmap_qenter(trunc_page((vm_offset_t)bp->b_data),
3352 bp->b_pages, bp->b_npages);
3354 #if defined(VFS_BIO_DEBUG)
3355 if (OFF_TO_IDX(foff) != m->pindex) {
3357 "biodone: foff(%jd)/m->pindex(%ju) mismatch\n",
3358 (intmax_t)foff, (uintmax_t)m->pindex);
3363 * In the write case, the valid and clean bits are
3364 * already changed correctly ( see bdwrite() ), so we
3365 * only need to do this here in the read case.
3367 if ((bp->b_iocmd == BIO_READ) && !bogusflag && resid > 0) {
3368 KASSERT((m->dirty & vm_page_bits(foff &
3369 PAGE_MASK, resid)) == 0, ("bufdone_finish:"
3370 " page %p has unexpected dirty bits", m));
3371 vfs_page_set_valid(bp, foff, m);
3375 * when debugging new filesystems or buffer I/O methods, this
3376 * is the most common error that pops up. if you see this, you
3377 * have not set the page busy flag correctly!!!
3380 printf("biodone: page busy < 0, "
3381 "pindex: %d, foff: 0x(%x,%x), "
3382 "resid: %d, index: %d\n",
3383 (int) m->pindex, (int)(foff >> 32),
3384 (int) foff & 0xffffffff, resid, i);
3385 if (!vn_isdisk(vp, NULL))
3386 printf(" iosize: %jd, lblkno: %jd, flags: 0x%x, npages: %d\n",
3387 (intmax_t)bp->b_vp->v_mount->mnt_stat.f_iosize,
3388 (intmax_t) bp->b_lblkno,
3389 bp->b_flags, bp->b_npages);
3391 printf(" VDEV, lblkno: %jd, flags: 0x%x, npages: %d\n",
3392 (intmax_t) bp->b_lblkno,
3393 bp->b_flags, bp->b_npages);
3394 printf(" valid: 0x%lx, dirty: 0x%lx, wired: %d\n",
3395 (u_long)m->valid, (u_long)m->dirty,
3397 panic("biodone: page busy < 0\n");
3399 vm_page_io_finish(m);
3400 vm_object_pip_subtract(obj, 1);
3401 foff = (foff + PAGE_SIZE) & ~(off_t)PAGE_MASK;
3404 vm_object_pip_wakeupn(obj, 0);
3405 VM_OBJECT_UNLOCK(obj);
3409 * For asynchronous completions, release the buffer now. The brelse
3410 * will do a wakeup there if necessary - so no need to do a wakeup
3411 * here in the async case. The sync case always needs to do a wakeup.
3414 if (bp->b_flags & B_ASYNC) {
3415 if ((bp->b_flags & (B_NOCACHE | B_INVAL | B_RELBUF)) || (bp->b_ioflags & BIO_ERROR))
3424 * This routine is called in lieu of iodone in the case of
3425 * incomplete I/O. This keeps the busy status for pages
3429 vfs_unbusy_pages(struct buf *bp)
3435 runningbufwakeup(bp);
3436 if (!(bp->b_flags & B_VMIO))
3439 obj = bp->b_bufobj->bo_object;
3440 VM_OBJECT_LOCK(obj);
3441 for (i = 0; i < bp->b_npages; i++) {
3443 if (m == bogus_page) {
3444 m = vm_page_lookup(obj, OFF_TO_IDX(bp->b_offset) + i);
3446 panic("vfs_unbusy_pages: page missing\n");
3448 pmap_qenter(trunc_page((vm_offset_t)bp->b_data),
3449 bp->b_pages, bp->b_npages);
3451 vm_object_pip_subtract(obj, 1);
3452 vm_page_io_finish(m);
3454 vm_object_pip_wakeupn(obj, 0);
3455 VM_OBJECT_UNLOCK(obj);
3459 * vfs_page_set_valid:
3461 * Set the valid bits in a page based on the supplied offset. The
3462 * range is restricted to the buffer's size.
3464 * This routine is typically called after a read completes.
3467 vfs_page_set_valid(struct buf *bp, vm_ooffset_t off, vm_page_t m)
3472 * Compute the end offset, eoff, such that [off, eoff) does not span a
3473 * page boundary and eoff is not greater than the end of the buffer.
3474 * The end of the buffer, in this case, is our file EOF, not the
3475 * allocation size of the buffer.
3477 eoff = (off + PAGE_SIZE) & ~(vm_ooffset_t)PAGE_MASK;
3478 if (eoff > bp->b_offset + bp->b_bcount)
3479 eoff = bp->b_offset + bp->b_bcount;
3482 * Set valid range. This is typically the entire buffer and thus the
3486 vm_page_set_valid(m, off & PAGE_MASK, eoff - off);
3490 * vfs_page_set_validclean:
3492 * Set the valid bits and clear the dirty bits in a page based on the
3493 * supplied offset. The range is restricted to the buffer's size.
3496 vfs_page_set_validclean(struct buf *bp, vm_ooffset_t off, vm_page_t m)
3498 vm_ooffset_t soff, eoff;
3500 mtx_assert(&vm_page_queue_mtx, MA_OWNED);
3502 * Start and end offsets in buffer. eoff - soff may not cross a
3503 * page boundry or cross the end of the buffer. The end of the
3504 * buffer, in this case, is our file EOF, not the allocation size
3508 eoff = (off + PAGE_SIZE) & ~(off_t)PAGE_MASK;
3509 if (eoff > bp->b_offset + bp->b_bcount)
3510 eoff = bp->b_offset + bp->b_bcount;
3513 * Set valid range. This is typically the entire buffer and thus the
3517 vm_page_set_validclean(
3519 (vm_offset_t) (soff & PAGE_MASK),
3520 (vm_offset_t) (eoff - soff)
3526 * Ensure that all buffer pages are not busied by VPO_BUSY flag. If
3527 * any page is busy, drain the flag.
3530 vfs_drain_busy_pages(struct buf *bp)
3535 VM_OBJECT_LOCK_ASSERT(bp->b_bufobj->bo_object, MA_OWNED);
3537 for (i = 0; i < bp->b_npages; i++) {
3539 if ((m->oflags & VPO_BUSY) != 0) {
3540 for (; last_busied < i; last_busied++)
3541 vm_page_busy(bp->b_pages[last_busied]);
3542 while ((m->oflags & VPO_BUSY) != 0)
3543 vm_page_sleep(m, "vbpage");
3546 for (i = 0; i < last_busied; i++)
3547 vm_page_wakeup(bp->b_pages[i]);
3551 * This routine is called before a device strategy routine.
3552 * It is used to tell the VM system that paging I/O is in
3553 * progress, and treat the pages associated with the buffer
3554 * almost as being VPO_BUSY. Also the object paging_in_progress
3555 * flag is handled to make sure that the object doesn't become
3558 * Since I/O has not been initiated yet, certain buffer flags
3559 * such as BIO_ERROR or B_INVAL may be in an inconsistant state
3560 * and should be ignored.
3563 vfs_busy_pages(struct buf *bp, int clear_modify)
3570 if (!(bp->b_flags & B_VMIO))
3573 obj = bp->b_bufobj->bo_object;
3574 foff = bp->b_offset;
3575 KASSERT(bp->b_offset != NOOFFSET,
3576 ("vfs_busy_pages: no buffer offset"));
3577 VM_OBJECT_LOCK(obj);
3578 vfs_drain_busy_pages(bp);
3579 if (bp->b_bufsize != 0)
3580 vfs_setdirty_locked_object(bp);
3583 vm_page_lock_queues();
3584 for (i = 0; i < bp->b_npages; i++) {
3587 if ((bp->b_flags & B_CLUSTER) == 0) {
3588 vm_object_pip_add(obj, 1);
3589 vm_page_io_start(m);
3592 * When readying a buffer for a read ( i.e
3593 * clear_modify == 0 ), it is important to do
3594 * bogus_page replacement for valid pages in
3595 * partially instantiated buffers. Partially
3596 * instantiated buffers can, in turn, occur when
3597 * reconstituting a buffer from its VM backing store
3598 * base. We only have to do this if B_CACHE is
3599 * clear ( which causes the I/O to occur in the
3600 * first place ). The replacement prevents the read
3601 * I/O from overwriting potentially dirty VM-backed
3602 * pages. XXX bogus page replacement is, uh, bogus.
3603 * It may not work properly with small-block devices.
3604 * We need to find a better way.
3607 pmap_remove_write(m);
3608 vfs_page_set_validclean(bp, foff, m);
3609 } else if (m->valid == VM_PAGE_BITS_ALL &&
3610 (bp->b_flags & B_CACHE) == 0) {
3611 bp->b_pages[i] = bogus_page;
3614 foff = (foff + PAGE_SIZE) & ~(off_t)PAGE_MASK;
3617 vm_page_unlock_queues();
3618 VM_OBJECT_UNLOCK(obj);
3620 pmap_qenter(trunc_page((vm_offset_t)bp->b_data),
3621 bp->b_pages, bp->b_npages);
3625 * vfs_bio_set_valid:
3627 * Set the range within the buffer to valid. The range is
3628 * relative to the beginning of the buffer, b_offset. Note that
3629 * b_offset itself may be offset from the beginning of the first
3633 vfs_bio_set_valid(struct buf *bp, int base, int size)
3638 if (!(bp->b_flags & B_VMIO))
3642 * Fixup base to be relative to beginning of first page.
3643 * Set initial n to be the maximum number of bytes in the
3644 * first page that can be validated.
3646 base += (bp->b_offset & PAGE_MASK);
3647 n = PAGE_SIZE - (base & PAGE_MASK);
3649 VM_OBJECT_LOCK(bp->b_bufobj->bo_object);
3650 for (i = base / PAGE_SIZE; size > 0 && i < bp->b_npages; ++i) {
3654 vm_page_set_valid(m, base & PAGE_MASK, n);
3659 VM_OBJECT_UNLOCK(bp->b_bufobj->bo_object);
3665 * If the specified buffer is a non-VMIO buffer, clear the entire
3666 * buffer. If the specified buffer is a VMIO buffer, clear and
3667 * validate only the previously invalid portions of the buffer.
3668 * This routine essentially fakes an I/O, so we need to clear
3669 * BIO_ERROR and B_INVAL.
3671 * Note that while we only theoretically need to clear through b_bcount,
3672 * we go ahead and clear through b_bufsize.
3675 vfs_bio_clrbuf(struct buf *bp)
3680 if ((bp->b_flags & (B_VMIO | B_MALLOC)) != B_VMIO) {
3684 bp->b_flags &= ~B_INVAL;
3685 bp->b_ioflags &= ~BIO_ERROR;
3686 VM_OBJECT_LOCK(bp->b_bufobj->bo_object);
3687 if ((bp->b_npages == 1) && (bp->b_bufsize < PAGE_SIZE) &&
3688 (bp->b_offset & PAGE_MASK) == 0) {
3689 if (bp->b_pages[0] == bogus_page)
3691 mask = (1 << (bp->b_bufsize / DEV_BSIZE)) - 1;
3692 VM_OBJECT_LOCK_ASSERT(bp->b_pages[0]->object, MA_OWNED);
3693 if ((bp->b_pages[0]->valid & mask) == mask)
3695 if ((bp->b_pages[0]->valid & mask) == 0) {
3696 bzero(bp->b_data, bp->b_bufsize);
3697 bp->b_pages[0]->valid |= mask;
3701 ea = sa = bp->b_data;
3702 for(i = 0; i < bp->b_npages; i++, sa = ea) {
3703 ea = (caddr_t)trunc_page((vm_offset_t)sa + PAGE_SIZE);
3704 ea = (caddr_t)(vm_offset_t)ulmin(
3705 (u_long)(vm_offset_t)ea,
3706 (u_long)(vm_offset_t)bp->b_data + bp->b_bufsize);
3707 if (bp->b_pages[i] == bogus_page)
3709 j = ((vm_offset_t)sa & PAGE_MASK) / DEV_BSIZE;
3710 mask = ((1 << ((ea - sa) / DEV_BSIZE)) - 1) << j;
3711 VM_OBJECT_LOCK_ASSERT(bp->b_pages[i]->object, MA_OWNED);
3712 if ((bp->b_pages[i]->valid & mask) == mask)
3714 if ((bp->b_pages[i]->valid & mask) == 0)
3717 for (; sa < ea; sa += DEV_BSIZE, j++) {
3718 if ((bp->b_pages[i]->valid & (1 << j)) == 0)
3719 bzero(sa, DEV_BSIZE);
3722 bp->b_pages[i]->valid |= mask;
3725 VM_OBJECT_UNLOCK(bp->b_bufobj->bo_object);
3730 * vm_hold_load_pages and vm_hold_free_pages get pages into
3731 * a buffers address space. The pages are anonymous and are
3732 * not associated with a file object.
3735 vm_hold_load_pages(struct buf *bp, vm_offset_t from, vm_offset_t to)
3741 to = round_page(to);
3742 from = round_page(from);
3743 index = (from - trunc_page((vm_offset_t)bp->b_data)) >> PAGE_SHIFT;
3745 for (pg = from; pg < to; pg += PAGE_SIZE, index++) {
3748 * note: must allocate system pages since blocking here
3749 * could interfere with paging I/O, no matter which
3752 p = vm_page_alloc(NULL, pg >> PAGE_SHIFT, VM_ALLOC_NOOBJ |
3753 VM_ALLOC_SYSTEM | VM_ALLOC_WIRED);
3755 atomic_add_int(&vm_pageout_deficit,
3756 (to - pg) >> PAGE_SHIFT);
3760 pmap_qenter(pg, &p, 1);
3761 bp->b_pages[index] = p;
3763 bp->b_npages = index;
3766 /* Return pages associated with this buf to the vm system */
3768 vm_hold_free_pages(struct buf *bp, vm_offset_t from, vm_offset_t to)
3772 int index, newnpages;
3774 from = round_page(from);
3775 to = round_page(to);
3776 newnpages = index = (from - trunc_page((vm_offset_t)bp->b_data)) >> PAGE_SHIFT;
3778 for (pg = from; pg < to; pg += PAGE_SIZE, index++) {
3779 p = bp->b_pages[index];
3780 if (p && (index < bp->b_npages)) {
3783 "vm_hold_free_pages: blkno: %jd, lblkno: %jd\n",
3784 (intmax_t)bp->b_blkno,
3785 (intmax_t)bp->b_lblkno);
3787 bp->b_pages[index] = NULL;
3788 pmap_qremove(pg, 1);
3791 atomic_subtract_int(&cnt.v_wire_count, 1);
3794 bp->b_npages = newnpages;
3798 * Map an IO request into kernel virtual address space.
3800 * All requests are (re)mapped into kernel VA space.
3801 * Notice that we use b_bufsize for the size of the buffer
3802 * to be mapped. b_bcount might be modified by the driver.
3804 * Note that even if the caller determines that the address space should
3805 * be valid, a race or a smaller-file mapped into a larger space may
3806 * actually cause vmapbuf() to fail, so all callers of vmapbuf() MUST
3807 * check the return value.
3810 vmapbuf(struct buf *bp)
3816 struct pmap *pmap = &curproc->p_vmspace->vm_pmap;
3818 if (bp->b_bufsize < 0)
3820 prot = VM_PROT_READ;
3821 if (bp->b_iocmd == BIO_READ)
3822 prot |= VM_PROT_WRITE; /* Less backwards than it looks */
3823 for (addr = (caddr_t)trunc_page((vm_offset_t)bp->b_data), pidx = 0;
3824 addr < bp->b_data + bp->b_bufsize;
3825 addr += PAGE_SIZE, pidx++) {
3827 * Do the vm_fault if needed; do the copy-on-write thing
3828 * when reading stuff off device into memory.
3830 * NOTE! Must use pmap_extract() because addr may be in
3831 * the userland address space, and kextract is only guarenteed
3832 * to work for the kernland address space (see: sparc64 port).
3835 if (vm_fault_quick(addr >= bp->b_data ? addr : bp->b_data,
3837 vm_page_lock_queues();
3838 for (i = 0; i < pidx; ++i) {
3839 vm_page_unhold(bp->b_pages[i]);
3840 bp->b_pages[i] = NULL;
3842 vm_page_unlock_queues();
3845 m = pmap_extract_and_hold(pmap, (vm_offset_t)addr, prot);
3848 bp->b_pages[pidx] = m;
3850 if (pidx > btoc(MAXPHYS))
3851 panic("vmapbuf: mapped more than MAXPHYS");
3852 pmap_qenter((vm_offset_t)bp->b_saveaddr, bp->b_pages, pidx);
3854 kva = bp->b_saveaddr;
3855 bp->b_npages = pidx;
3856 bp->b_saveaddr = bp->b_data;
3857 bp->b_data = kva + (((vm_offset_t) bp->b_data) & PAGE_MASK);
3862 * Free the io map PTEs associated with this IO operation.
3863 * We also invalidate the TLB entries and restore the original b_addr.
3866 vunmapbuf(struct buf *bp)
3871 npages = bp->b_npages;
3872 pmap_qremove(trunc_page((vm_offset_t)bp->b_data), npages);
3873 vm_page_lock_queues();
3874 for (pidx = 0; pidx < npages; pidx++)
3875 vm_page_unhold(bp->b_pages[pidx]);
3876 vm_page_unlock_queues();
3878 bp->b_data = bp->b_saveaddr;
3882 bdone(struct buf *bp)
3886 mtxp = mtx_pool_find(mtxpool_sleep, bp);
3888 bp->b_flags |= B_DONE;
3894 bwait(struct buf *bp, u_char pri, const char *wchan)
3898 mtxp = mtx_pool_find(mtxpool_sleep, bp);
3900 while ((bp->b_flags & B_DONE) == 0)
3901 msleep(bp, mtxp, pri, wchan, 0);
3906 bufsync(struct bufobj *bo, int waitfor)
3909 return (VOP_FSYNC(bo->__bo_vnode, waitfor, curthread));
3913 bufstrategy(struct bufobj *bo, struct buf *bp)
3919 KASSERT(vp == bo->bo_private, ("Inconsistent vnode bufstrategy"));
3920 KASSERT(vp->v_type != VCHR && vp->v_type != VBLK,
3921 ("Wrong vnode in bufstrategy(bp=%p, vp=%p)", bp, vp));
3922 i = VOP_STRATEGY(vp, bp);
3923 KASSERT(i == 0, ("VOP_STRATEGY failed bp=%p vp=%p", bp, bp->b_vp));
3927 bufobj_wrefl(struct bufobj *bo)
3930 KASSERT(bo != NULL, ("NULL bo in bufobj_wref"));
3931 ASSERT_BO_LOCKED(bo);
3936 bufobj_wref(struct bufobj *bo)
3939 KASSERT(bo != NULL, ("NULL bo in bufobj_wref"));
3946 bufobj_wdrop(struct bufobj *bo)
3949 KASSERT(bo != NULL, ("NULL bo in bufobj_wdrop"));
3951 KASSERT(bo->bo_numoutput > 0, ("bufobj_wdrop non-positive count"));
3952 if ((--bo->bo_numoutput == 0) && (bo->bo_flag & BO_WWAIT)) {
3953 bo->bo_flag &= ~BO_WWAIT;
3954 wakeup(&bo->bo_numoutput);
3960 bufobj_wwait(struct bufobj *bo, int slpflag, int timeo)
3964 KASSERT(bo != NULL, ("NULL bo in bufobj_wwait"));
3965 ASSERT_BO_LOCKED(bo);
3967 while (bo->bo_numoutput) {
3968 bo->bo_flag |= BO_WWAIT;
3969 error = msleep(&bo->bo_numoutput, BO_MTX(bo),
3970 slpflag | (PRIBIO + 1), "bo_wwait", timeo);
3978 bpin(struct buf *bp)
3982 mtxp = mtx_pool_find(mtxpool_sleep, bp);
3989 bunpin(struct buf *bp)
3993 mtxp = mtx_pool_find(mtxpool_sleep, bp);
3995 if (--bp->b_pin_count == 0)
4001 bunpin_wait(struct buf *bp)
4005 mtxp = mtx_pool_find(mtxpool_sleep, bp);
4007 while (bp->b_pin_count > 0)
4008 msleep(bp, mtxp, PRIBIO, "bwunpin", 0);
4012 #include "opt_ddb.h"
4014 #include <ddb/ddb.h>
4016 /* DDB command to show buffer data */
4017 DB_SHOW_COMMAND(buffer, db_show_buffer)
4020 struct buf *bp = (struct buf *)addr;
4023 db_printf("usage: show buffer <addr>\n");
4027 db_printf("buf at %p\n", bp);
4028 db_printf("b_flags = 0x%b\n", (u_int)bp->b_flags, PRINT_BUF_FLAGS);
4030 "b_error = %d, b_bufsize = %ld, b_bcount = %ld, b_resid = %ld\n"
4031 "b_bufobj = (%p), b_data = %p, b_blkno = %jd, b_dep = %p\n",
4032 bp->b_error, bp->b_bufsize, bp->b_bcount, bp->b_resid,
4033 bp->b_bufobj, bp->b_data, (intmax_t)bp->b_blkno,
4034 bp->b_dep.lh_first);
4037 db_printf("b_npages = %d, pages(OBJ, IDX, PA): ", bp->b_npages);
4038 for (i = 0; i < bp->b_npages; i++) {
4041 db_printf("(%p, 0x%lx, 0x%lx)", (void *)m->object,
4042 (u_long)m->pindex, (u_long)VM_PAGE_TO_PHYS(m));
4043 if ((i + 1) < bp->b_npages)
4049 lockmgr_printinfo(&bp->b_lock);
4052 DB_SHOW_COMMAND(lockedbufs, lockedbufs)
4057 for (i = 0; i < nbuf; i++) {
4059 if (BUF_ISLOCKED(bp)) {
4060 db_show_buffer((uintptr_t)bp, 1, 0, NULL);
4066 DB_SHOW_COMMAND(vnodebufs, db_show_vnodebufs)
4072 db_printf("usage: show vnodebufs <addr>\n");
4075 vp = (struct vnode *)addr;
4076 db_printf("Clean buffers:\n");
4077 TAILQ_FOREACH(bp, &vp->v_bufobj.bo_clean.bv_hd, b_bobufs) {
4078 db_show_buffer((uintptr_t)bp, 1, 0, NULL);
4081 db_printf("Dirty buffers:\n");
4082 TAILQ_FOREACH(bp, &vp->v_bufobj.bo_dirty.bv_hd, b_bobufs) {
4083 db_show_buffer((uintptr_t)bp, 1, 0, NULL);