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, int newbsize);
99 static void vm_hold_load_pages(struct buf *bp, vm_offset_t from,
101 static void vfs_page_set_valid(struct buf *bp, vm_ooffset_t off, vm_page_t m);
102 static void vfs_page_set_validclean(struct buf *bp, vm_ooffset_t off,
104 static void vfs_drain_busy_pages(struct buf *bp);
105 static void vfs_clean_pages_dirty_buf(struct buf *bp);
106 static void vfs_setdirty_locked_object(struct buf *bp);
107 static void vfs_vmio_release(struct buf *bp);
108 static int vfs_bio_clcheck(struct vnode *vp, int size,
109 daddr_t lblkno, daddr_t blkno);
110 static int buf_do_flush(struct vnode *vp);
111 static int flushbufqueues(struct vnode *, int, int);
112 static void buf_daemon(void);
113 static void bremfreel(struct buf *bp);
114 #if defined(COMPAT_FREEBSD4) || defined(COMPAT_FREEBSD5) || \
115 defined(COMPAT_FREEBSD6) || defined(COMPAT_FREEBSD7)
116 static int sysctl_bufspace(SYSCTL_HANDLER_ARGS);
119 int vmiodirenable = TRUE;
120 SYSCTL_INT(_vfs, OID_AUTO, vmiodirenable, CTLFLAG_RW, &vmiodirenable, 0,
121 "Use the VM system for directory writes");
122 long runningbufspace;
123 SYSCTL_LONG(_vfs, OID_AUTO, runningbufspace, CTLFLAG_RD, &runningbufspace, 0,
124 "Amount of presently outstanding async buffer io");
125 static long bufspace;
126 #if defined(COMPAT_FREEBSD4) || defined(COMPAT_FREEBSD5) || \
127 defined(COMPAT_FREEBSD6) || defined(COMPAT_FREEBSD7)
128 SYSCTL_PROC(_vfs, OID_AUTO, bufspace, CTLTYPE_LONG|CTLFLAG_MPSAFE|CTLFLAG_RD,
129 &bufspace, 0, sysctl_bufspace, "L", "Virtual memory used for buffers");
131 SYSCTL_LONG(_vfs, OID_AUTO, bufspace, CTLFLAG_RD, &bufspace, 0,
132 "Virtual memory used for buffers");
134 static long maxbufspace;
135 SYSCTL_LONG(_vfs, OID_AUTO, maxbufspace, CTLFLAG_RD, &maxbufspace, 0,
136 "Maximum allowed value of bufspace (including buf_daemon)");
137 static long bufmallocspace;
138 SYSCTL_LONG(_vfs, OID_AUTO, bufmallocspace, CTLFLAG_RD, &bufmallocspace, 0,
139 "Amount of malloced memory for buffers");
140 static long maxbufmallocspace;
141 SYSCTL_LONG(_vfs, OID_AUTO, maxmallocbufspace, CTLFLAG_RW, &maxbufmallocspace, 0,
142 "Maximum amount of malloced memory for buffers");
143 static long lobufspace;
144 SYSCTL_LONG(_vfs, OID_AUTO, lobufspace, CTLFLAG_RD, &lobufspace, 0,
145 "Minimum amount of buffers we want to have");
147 SYSCTL_LONG(_vfs, OID_AUTO, hibufspace, CTLFLAG_RD, &hibufspace, 0,
148 "Maximum allowed value of bufspace (excluding buf_daemon)");
149 static int bufreusecnt;
150 SYSCTL_INT(_vfs, OID_AUTO, bufreusecnt, CTLFLAG_RW, &bufreusecnt, 0,
151 "Number of times we have reused a buffer");
152 static int buffreekvacnt;
153 SYSCTL_INT(_vfs, OID_AUTO, buffreekvacnt, CTLFLAG_RW, &buffreekvacnt, 0,
154 "Number of times we have freed the KVA space from some buffer");
155 static int bufdefragcnt;
156 SYSCTL_INT(_vfs, OID_AUTO, bufdefragcnt, CTLFLAG_RW, &bufdefragcnt, 0,
157 "Number of times we have had to repeat buffer allocation to defragment");
158 static long lorunningspace;
159 SYSCTL_LONG(_vfs, OID_AUTO, lorunningspace, CTLFLAG_RW, &lorunningspace, 0,
160 "Minimum preferred space used for in-progress I/O");
161 static long hirunningspace;
162 SYSCTL_LONG(_vfs, OID_AUTO, hirunningspace, CTLFLAG_RW, &hirunningspace, 0,
163 "Maximum amount of space to use for in-progress I/O");
164 int dirtybufferflushes;
165 SYSCTL_INT(_vfs, OID_AUTO, dirtybufferflushes, CTLFLAG_RW, &dirtybufferflushes,
166 0, "Number of bdwrite to bawrite conversions to limit dirty buffers");
168 SYSCTL_INT(_vfs, OID_AUTO, bdwriteskip, CTLFLAG_RW, &bdwriteskip,
169 0, "Number of buffers supplied to bdwrite with snapshot deadlock risk");
170 int altbufferflushes;
171 SYSCTL_INT(_vfs, OID_AUTO, altbufferflushes, CTLFLAG_RW, &altbufferflushes,
172 0, "Number of fsync flushes to limit dirty buffers");
173 static int recursiveflushes;
174 SYSCTL_INT(_vfs, OID_AUTO, recursiveflushes, CTLFLAG_RW, &recursiveflushes,
175 0, "Number of flushes skipped due to being recursive");
176 static int numdirtybuffers;
177 SYSCTL_INT(_vfs, OID_AUTO, numdirtybuffers, CTLFLAG_RD, &numdirtybuffers, 0,
178 "Number of buffers that are dirty (has unwritten changes) at the moment");
179 static int lodirtybuffers;
180 SYSCTL_INT(_vfs, OID_AUTO, lodirtybuffers, CTLFLAG_RW, &lodirtybuffers, 0,
181 "How many buffers we want to have free before bufdaemon can sleep");
182 static int hidirtybuffers;
183 SYSCTL_INT(_vfs, OID_AUTO, hidirtybuffers, CTLFLAG_RW, &hidirtybuffers, 0,
184 "When the number of dirty buffers is considered severe");
186 SYSCTL_INT(_vfs, OID_AUTO, dirtybufthresh, CTLFLAG_RW, &dirtybufthresh,
187 0, "Number of bdwrite to bawrite conversions to clear dirty buffers");
188 static int numfreebuffers;
189 SYSCTL_INT(_vfs, OID_AUTO, numfreebuffers, CTLFLAG_RD, &numfreebuffers, 0,
190 "Number of free buffers");
191 static int lofreebuffers;
192 SYSCTL_INT(_vfs, OID_AUTO, lofreebuffers, CTLFLAG_RW, &lofreebuffers, 0,
194 static int hifreebuffers;
195 SYSCTL_INT(_vfs, OID_AUTO, hifreebuffers, CTLFLAG_RW, &hifreebuffers, 0,
196 "XXX Complicatedly unused");
197 static int getnewbufcalls;
198 SYSCTL_INT(_vfs, OID_AUTO, getnewbufcalls, CTLFLAG_RW, &getnewbufcalls, 0,
199 "Number of calls to getnewbuf");
200 static int getnewbufrestarts;
201 SYSCTL_INT(_vfs, OID_AUTO, getnewbufrestarts, CTLFLAG_RW, &getnewbufrestarts, 0,
202 "Number of times getnewbuf has had to restart a buffer aquisition");
203 static int flushbufqtarget = 100;
204 SYSCTL_INT(_vfs, OID_AUTO, flushbufqtarget, CTLFLAG_RW, &flushbufqtarget, 0,
205 "Amount of work to do in flushbufqueues when helping bufdaemon");
206 static long notbufdflashes;
207 SYSCTL_LONG(_vfs, OID_AUTO, notbufdflashes, CTLFLAG_RD, ¬bufdflashes, 0,
208 "Number of dirty buffer flushes done by the bufdaemon helpers");
211 * Wakeup point for bufdaemon, as well as indicator of whether it is already
212 * active. Set to 1 when the bufdaemon is already "on" the queue, 0 when it
215 static int bd_request;
218 * Request for the buf daemon to write more buffers than is indicated by
219 * lodirtybuf. This may be necessary to push out excess dependencies or
220 * defragment the address space where a simple count of the number of dirty
221 * buffers is insufficient to characterize the demand for flushing them.
223 static int bd_speedupreq;
226 * This lock synchronizes access to bd_request.
228 static struct mtx bdlock;
231 * bogus page -- for I/O to/from partially complete buffers
232 * this is a temporary solution to the problem, but it is not
233 * really that bad. it would be better to split the buffer
234 * for input in the case of buffers partially already in memory,
235 * but the code is intricate enough already.
237 vm_page_t bogus_page;
240 * Synchronization (sleep/wakeup) variable for active buffer space requests.
241 * Set when wait starts, cleared prior to wakeup().
242 * Used in runningbufwakeup() and waitrunningbufspace().
244 static int runningbufreq;
247 * This lock protects the runningbufreq and synchronizes runningbufwakeup and
248 * waitrunningbufspace().
250 static struct mtx rbreqlock;
253 * Synchronization (sleep/wakeup) variable for buffer requests.
254 * Can contain the VFS_BIO_NEED flags defined below; setting/clearing is done
256 * Used in numdirtywakeup(), bufspacewakeup(), bufcountwakeup(), bwillwrite(),
257 * getnewbuf(), and getblk().
259 static int needsbuffer;
262 * Lock that protects needsbuffer and the sleeps/wakeups surrounding it.
264 static struct mtx nblock;
267 * Definitions for the buffer free lists.
269 #define BUFFER_QUEUES 6 /* number of free buffer queues */
271 #define QUEUE_NONE 0 /* on no queue */
272 #define QUEUE_CLEAN 1 /* non-B_DELWRI buffers */
273 #define QUEUE_DIRTY 2 /* B_DELWRI buffers */
274 #define QUEUE_DIRTY_GIANT 3 /* B_DELWRI buffers that need giant */
275 #define QUEUE_EMPTYKVA 4 /* empty buffer headers w/KVA assignment */
276 #define QUEUE_EMPTY 5 /* empty buffer headers */
277 #define QUEUE_SENTINEL 1024 /* not an queue index, but mark for sentinel */
279 /* Queues for free buffers with various properties */
280 static TAILQ_HEAD(bqueues, buf) bufqueues[BUFFER_QUEUES] = { { 0 } };
282 /* Lock for the bufqueues */
283 static struct mtx bqlock;
286 * Single global constant for BUF_WMESG, to avoid getting multiple references.
287 * buf_wmesg is referred from macros.
289 const char *buf_wmesg = BUF_WMESG;
291 #define VFS_BIO_NEED_ANY 0x01 /* any freeable buffer */
292 #define VFS_BIO_NEED_DIRTYFLUSH 0x02 /* waiting for dirty buffer flush */
293 #define VFS_BIO_NEED_FREE 0x04 /* wait for free bufs, hi hysteresis */
294 #define VFS_BIO_NEED_BUFSPACE 0x08 /* wait for buf space, lo hysteresis */
296 #if defined(COMPAT_FREEBSD4) || defined(COMPAT_FREEBSD5) || \
297 defined(COMPAT_FREEBSD6) || defined(COMPAT_FREEBSD7)
299 sysctl_bufspace(SYSCTL_HANDLER_ARGS)
304 if (sizeof(int) == sizeof(long) || req->oldlen >= sizeof(long))
305 return (sysctl_handle_long(oidp, arg1, arg2, req));
306 lvalue = *(long *)arg1;
307 if (lvalue > INT_MAX)
308 /* On overflow, still write out a long to trigger ENOMEM. */
309 return (sysctl_handle_long(oidp, &lvalue, 0, req));
311 return (sysctl_handle_int(oidp, &ivalue, 0, req));
316 extern void ffs_rawread_setup(void);
317 #endif /* DIRECTIO */
321 * If someone is blocked due to there being too many dirty buffers,
322 * and numdirtybuffers is now reasonable, wake them up.
326 numdirtywakeup(int level)
329 if (numdirtybuffers <= level) {
331 if (needsbuffer & VFS_BIO_NEED_DIRTYFLUSH) {
332 needsbuffer &= ~VFS_BIO_NEED_DIRTYFLUSH;
333 wakeup(&needsbuffer);
342 * Called when buffer space is potentially available for recovery.
343 * getnewbuf() will block on this flag when it is unable to free
344 * sufficient buffer space. Buffer space becomes recoverable when
345 * bp's get placed back in the queues.
353 * If someone is waiting for BUF space, wake them up. Even
354 * though we haven't freed the kva space yet, the waiting
355 * process will be able to now.
358 if (needsbuffer & VFS_BIO_NEED_BUFSPACE) {
359 needsbuffer &= ~VFS_BIO_NEED_BUFSPACE;
360 wakeup(&needsbuffer);
366 * runningbufwakeup() - in-progress I/O accounting.
370 runningbufwakeup(struct buf *bp)
373 if (bp->b_runningbufspace) {
374 atomic_subtract_long(&runningbufspace, bp->b_runningbufspace);
375 bp->b_runningbufspace = 0;
376 mtx_lock(&rbreqlock);
377 if (runningbufreq && runningbufspace <= lorunningspace) {
379 wakeup(&runningbufreq);
381 mtx_unlock(&rbreqlock);
388 * Called when a buffer has been added to one of the free queues to
389 * account for the buffer and to wakeup anyone waiting for free buffers.
390 * This typically occurs when large amounts of metadata are being handled
391 * by the buffer cache ( else buffer space runs out first, usually ).
395 bufcountwakeup(struct buf *bp)
399 KASSERT((bp->b_vflags & BV_INFREECNT) == 0,
400 ("buf %p already counted as free", bp));
401 if (bp->b_bufobj != NULL)
402 mtx_assert(BO_MTX(bp->b_bufobj), MA_OWNED);
403 bp->b_vflags |= BV_INFREECNT;
404 old = atomic_fetchadd_int(&numfreebuffers, 1);
405 KASSERT(old >= 0 && old < nbuf,
406 ("numfreebuffers climbed to %d", old + 1));
409 needsbuffer &= ~VFS_BIO_NEED_ANY;
410 if (numfreebuffers >= hifreebuffers)
411 needsbuffer &= ~VFS_BIO_NEED_FREE;
412 wakeup(&needsbuffer);
418 * waitrunningbufspace()
420 * runningbufspace is a measure of the amount of I/O currently
421 * running. This routine is used in async-write situations to
422 * prevent creating huge backups of pending writes to a device.
423 * Only asynchronous writes are governed by this function.
425 * Reads will adjust runningbufspace, but will not block based on it.
426 * The read load has a side effect of reducing the allowed write load.
428 * This does NOT turn an async write into a sync write. It waits
429 * for earlier writes to complete and generally returns before the
430 * caller's write has reached the device.
433 waitrunningbufspace(void)
436 mtx_lock(&rbreqlock);
437 while (runningbufspace > hirunningspace) {
439 msleep(&runningbufreq, &rbreqlock, PVM, "wdrain", 0);
441 mtx_unlock(&rbreqlock);
446 * vfs_buf_test_cache:
448 * Called when a buffer is extended. This function clears the B_CACHE
449 * bit if the newly extended portion of the buffer does not contain
454 vfs_buf_test_cache(struct buf *bp,
455 vm_ooffset_t foff, vm_offset_t off, vm_offset_t size,
459 VM_OBJECT_LOCK_ASSERT(m->object, MA_OWNED);
460 if (bp->b_flags & B_CACHE) {
461 int base = (foff + off) & PAGE_MASK;
462 if (vm_page_is_valid(m, base, size) == 0)
463 bp->b_flags &= ~B_CACHE;
467 /* Wake up the buffer daemon if necessary */
470 bd_wakeup(int dirtybuflevel)
474 if (bd_request == 0 && numdirtybuffers >= dirtybuflevel) {
482 * bd_speedup - speedup the buffer cache flushing code
492 if (bd_speedupreq == 0 || bd_request == 0)
502 * Calculating buffer cache scaling values and reserve space for buffer
503 * headers. This is called during low level kernel initialization and
504 * may be called more then once. We CANNOT write to the memory area
505 * being reserved at this time.
508 kern_vfs_bio_buffer_alloc(caddr_t v, long physmem_est)
514 * physmem_est is in pages. Convert it to kilobytes (assumes
515 * PAGE_SIZE is >= 1K)
517 physmem_est = physmem_est * (PAGE_SIZE / 1024);
520 * The nominal buffer size (and minimum KVA allocation) is BKVASIZE.
521 * For the first 64MB of ram nominally allocate sufficient buffers to
522 * cover 1/4 of our ram. Beyond the first 64MB allocate additional
523 * buffers to cover 1/10 of our ram over 64MB. When auto-sizing
524 * the buffer cache we limit the eventual kva reservation to
527 * factor represents the 1/4 x ram conversion.
530 int factor = 4 * BKVASIZE / 1024;
533 if (physmem_est > 4096)
534 nbuf += min((physmem_est - 4096) / factor,
536 if (physmem_est > 65536)
537 nbuf += (physmem_est - 65536) * 2 / (factor * 5);
539 if (maxbcache && nbuf > maxbcache / BKVASIZE)
540 nbuf = maxbcache / BKVASIZE;
545 /* XXX Avoid unsigned long overflows later on with maxbufspace. */
546 maxbuf = (LONG_MAX / 3) / BKVASIZE;
549 printf("Warning: nbufs lowered from %d to %ld\n", nbuf,
555 * swbufs are used as temporary holders for I/O, such as paging I/O.
556 * We have no less then 16 and no more then 256.
558 nswbuf = max(min(nbuf/4, 256), 16);
560 if (nswbuf < NSWBUF_MIN)
568 * Reserve space for the buffer cache buffers
571 v = (caddr_t)(swbuf + nswbuf);
573 v = (caddr_t)(buf + nbuf);
578 /* Initialize the buffer subsystem. Called before use of any buffers. */
585 mtx_init(&bqlock, "buf queue lock", NULL, MTX_DEF);
586 mtx_init(&rbreqlock, "runningbufspace lock", NULL, MTX_DEF);
587 mtx_init(&nblock, "needsbuffer lock", NULL, MTX_DEF);
588 mtx_init(&bdlock, "buffer daemon lock", NULL, MTX_DEF);
590 /* next, make a null set of free lists */
591 for (i = 0; i < BUFFER_QUEUES; i++)
592 TAILQ_INIT(&bufqueues[i]);
594 /* finally, initialize each buffer header and stick on empty q */
595 for (i = 0; i < nbuf; i++) {
597 bzero(bp, sizeof *bp);
598 bp->b_flags = B_INVAL; /* we're just an empty header */
599 bp->b_rcred = NOCRED;
600 bp->b_wcred = NOCRED;
601 bp->b_qindex = QUEUE_EMPTY;
602 bp->b_vflags = BV_INFREECNT; /* buf is counted as free */
604 LIST_INIT(&bp->b_dep);
606 TAILQ_INSERT_TAIL(&bufqueues[QUEUE_EMPTY], bp, b_freelist);
610 * maxbufspace is the absolute maximum amount of buffer space we are
611 * allowed to reserve in KVM and in real terms. The absolute maximum
612 * is nominally used by buf_daemon. hibufspace is the nominal maximum
613 * used by most other processes. The differential is required to
614 * ensure that buf_daemon is able to run when other processes might
615 * be blocked waiting for buffer space.
617 * maxbufspace is based on BKVASIZE. Allocating buffers larger then
618 * this may result in KVM fragmentation which is not handled optimally
621 maxbufspace = (long)nbuf * BKVASIZE;
622 hibufspace = lmax(3 * maxbufspace / 4, maxbufspace - MAXBSIZE * 10);
623 lobufspace = hibufspace - MAXBSIZE;
626 * Note: The 16 MiB upper limit for hirunningspace was chosen
627 * arbitrarily and may need further tuning. It corresponds to
628 * 128 outstanding write IO requests (if IO size is 128 KiB),
629 * which fits with many RAID controllers' tagged queuing limits.
630 * The lower 1 MiB limit is the historical upper limit for
633 hirunningspace = lmax(lmin(roundup(hibufspace / 64, MAXBSIZE),
634 16 * 1024 * 1024), 1024 * 1024);
635 lorunningspace = roundup((hirunningspace * 2) / 3, MAXBSIZE);
638 * Limit the amount of malloc memory since it is wired permanently into
639 * the kernel space. Even though this is accounted for in the buffer
640 * allocation, we don't want the malloced region to grow uncontrolled.
641 * The malloc scheme improves memory utilization significantly on average
642 * (small) directories.
644 maxbufmallocspace = hibufspace / 20;
647 * Reduce the chance of a deadlock occuring by limiting the number
648 * of delayed-write dirty buffers we allow to stack up.
650 hidirtybuffers = nbuf / 4 + 20;
651 dirtybufthresh = hidirtybuffers * 9 / 10;
654 * To support extreme low-memory systems, make sure hidirtybuffers cannot
655 * eat up all available buffer space. This occurs when our minimum cannot
656 * be met. We try to size hidirtybuffers to 3/4 our buffer space assuming
657 * BKVASIZE'd buffers.
659 while ((long)hidirtybuffers * BKVASIZE > 3 * hibufspace / 4) {
660 hidirtybuffers >>= 1;
662 lodirtybuffers = hidirtybuffers / 2;
665 * Try to keep the number of free buffers in the specified range,
666 * and give special processes (e.g. like buf_daemon) access to an
669 lofreebuffers = nbuf / 18 + 5;
670 hifreebuffers = 2 * lofreebuffers;
671 numfreebuffers = nbuf;
673 bogus_page = vm_page_alloc(NULL, 0, VM_ALLOC_NOOBJ |
674 VM_ALLOC_NORMAL | VM_ALLOC_WIRED);
678 * bfreekva() - free the kva allocation for a buffer.
680 * Since this call frees up buffer space, we call bufspacewakeup().
683 bfreekva(struct buf *bp)
687 atomic_add_int(&buffreekvacnt, 1);
688 atomic_subtract_long(&bufspace, bp->b_kvasize);
689 vm_map_remove(buffer_map, (vm_offset_t) bp->b_kvabase,
690 (vm_offset_t) bp->b_kvabase + bp->b_kvasize);
699 * Mark the buffer for removal from the appropriate free list in brelse.
703 bremfree(struct buf *bp)
707 CTR3(KTR_BUF, "bremfree(%p) vp %p flags %X", bp, bp->b_vp, bp->b_flags);
708 KASSERT((bp->b_flags & B_REMFREE) == 0,
709 ("bremfree: buffer %p already marked for delayed removal.", bp));
710 KASSERT(bp->b_qindex != QUEUE_NONE,
711 ("bremfree: buffer %p not on a queue.", bp));
714 bp->b_flags |= B_REMFREE;
715 /* Fixup numfreebuffers count. */
716 if ((bp->b_flags & B_INVAL) || (bp->b_flags & B_DELWRI) == 0) {
717 KASSERT((bp->b_vflags & BV_INFREECNT) != 0,
718 ("buf %p not counted in numfreebuffers", bp));
719 if (bp->b_bufobj != NULL)
720 mtx_assert(BO_MTX(bp->b_bufobj), MA_OWNED);
721 bp->b_vflags &= ~BV_INFREECNT;
722 old = atomic_fetchadd_int(&numfreebuffers, -1);
723 KASSERT(old > 0, ("numfreebuffers dropped to %d", old - 1));
730 * Force an immediate removal from a free list. Used only in nfs when
731 * it abuses the b_freelist pointer.
734 bremfreef(struct buf *bp)
744 * Removes a buffer from the free list, must be called with the
748 bremfreel(struct buf *bp)
752 CTR3(KTR_BUF, "bremfreel(%p) vp %p flags %X",
753 bp, bp->b_vp, bp->b_flags);
754 KASSERT(bp->b_qindex != QUEUE_NONE,
755 ("bremfreel: buffer %p not on a queue.", bp));
757 mtx_assert(&bqlock, MA_OWNED);
759 TAILQ_REMOVE(&bufqueues[bp->b_qindex], bp, b_freelist);
760 bp->b_qindex = QUEUE_NONE;
762 * If this was a delayed bremfree() we only need to remove the buffer
763 * from the queue and return the stats are already done.
765 if (bp->b_flags & B_REMFREE) {
766 bp->b_flags &= ~B_REMFREE;
770 * Fixup numfreebuffers count. If the buffer is invalid or not
771 * delayed-write, the buffer was free and we must decrement
774 if ((bp->b_flags & B_INVAL) || (bp->b_flags & B_DELWRI) == 0) {
775 KASSERT((bp->b_vflags & BV_INFREECNT) != 0,
776 ("buf %p not counted in numfreebuffers", bp));
777 if (bp->b_bufobj != NULL)
778 mtx_assert(BO_MTX(bp->b_bufobj), MA_OWNED);
779 bp->b_vflags &= ~BV_INFREECNT;
780 old = atomic_fetchadd_int(&numfreebuffers, -1);
781 KASSERT(old > 0, ("numfreebuffers dropped to %d", old - 1));
786 * Get a buffer with the specified data.
789 bread(struct vnode * vp, daddr_t blkno, int size, struct ucred * cred,
793 return (breadn_flags(vp, blkno, size, 0, 0, 0, cred, 0, bpp));
797 * Attempt to initiate asynchronous I/O on read-ahead blocks. We must
798 * clear BIO_ERROR and B_INVAL prior to initiating I/O . If B_CACHE is set,
799 * the buffer is valid and we do not have to do anything.
802 breada(struct vnode * vp, daddr_t * rablkno, int * rabsize,
803 int cnt, struct ucred * cred)
808 for (i = 0; i < cnt; i++, rablkno++, rabsize++) {
809 if (inmem(vp, *rablkno))
811 rabp = getblk(vp, *rablkno, *rabsize, 0, 0, 0);
813 if ((rabp->b_flags & B_CACHE) == 0) {
814 if (!TD_IS_IDLETHREAD(curthread))
815 curthread->td_ru.ru_inblock++;
816 rabp->b_flags |= B_ASYNC;
817 rabp->b_flags &= ~B_INVAL;
818 rabp->b_ioflags &= ~BIO_ERROR;
819 rabp->b_iocmd = BIO_READ;
820 if (rabp->b_rcred == NOCRED && cred != NOCRED)
821 rabp->b_rcred = crhold(cred);
822 vfs_busy_pages(rabp, 0);
824 rabp->b_iooffset = dbtob(rabp->b_blkno);
833 * Operates like bread, but also starts asynchronous I/O on
837 breadn(struct vnode * vp, daddr_t blkno, int size,
838 daddr_t * rablkno, int *rabsize,
839 int cnt, struct ucred * cred, struct buf **bpp)
842 return (breadn_flags(vp, blkno, size, rablkno, rabsize, cnt,
847 * Entry point for bread() and breadn().
849 * Get a buffer with the specified data. Look in the cache first. We
850 * must clear BIO_ERROR and B_INVAL prior to initiating I/O. If B_CACHE
851 * is set, the buffer is valid and we do not have to do anything, see
852 * getblk(). Also starts asynchronous I/O on read-ahead blocks.
855 breadn_flags(struct vnode * vp, daddr_t blkno, int size,
856 daddr_t * rablkno, int *rabsize, int cnt,
857 struct ucred * cred, int flags, struct buf **bpp)
860 int rv = 0, readwait = 0;
862 CTR3(KTR_BUF, "breadn(%p, %jd, %d)", vp, blkno, size);
864 * Can only return NULL if GB_LOCK_NOWAIT flag is specified.
866 *bpp = bp = getblk(vp, blkno, size, 0, 0, flags);
870 /* if not found in cache, do some I/O */
871 if ((bp->b_flags & B_CACHE) == 0) {
872 if (!TD_IS_IDLETHREAD(curthread))
873 curthread->td_ru.ru_inblock++;
874 bp->b_iocmd = BIO_READ;
875 bp->b_flags &= ~B_INVAL;
876 bp->b_ioflags &= ~BIO_ERROR;
877 if (bp->b_rcred == NOCRED && cred != NOCRED)
878 bp->b_rcred = crhold(cred);
879 vfs_busy_pages(bp, 0);
880 bp->b_iooffset = dbtob(bp->b_blkno);
885 breada(vp, rablkno, rabsize, cnt, cred);
894 * Write, release buffer on completion. (Done by iodone
895 * if async). Do not bother writing anything if the buffer
898 * Note that we set B_CACHE here, indicating that buffer is
899 * fully valid and thus cacheable. This is true even of NFS
900 * now so we set it generally. This could be set either here
901 * or in biodone() since the I/O is synchronous. We put it
905 bufwrite(struct buf *bp)
911 CTR3(KTR_BUF, "bufwrite(%p) vp %p flags %X", bp, bp->b_vp, bp->b_flags);
912 if (bp->b_flags & B_INVAL) {
917 oldflags = bp->b_flags;
921 if (bp->b_pin_count > 0)
924 KASSERT(!(bp->b_vflags & BV_BKGRDINPROG),
925 ("FFS background buffer should not get here %p", bp));
929 vp_md = vp->v_vflag & VV_MD;
933 /* Mark the buffer clean */
936 bp->b_flags &= ~B_DONE;
937 bp->b_ioflags &= ~BIO_ERROR;
938 bp->b_flags |= B_CACHE;
939 bp->b_iocmd = BIO_WRITE;
941 bufobj_wref(bp->b_bufobj);
942 vfs_busy_pages(bp, 1);
945 * Normal bwrites pipeline writes
947 bp->b_runningbufspace = bp->b_bufsize;
948 atomic_add_long(&runningbufspace, bp->b_runningbufspace);
950 if (!TD_IS_IDLETHREAD(curthread))
951 curthread->td_ru.ru_oublock++;
952 if (oldflags & B_ASYNC)
954 bp->b_iooffset = dbtob(bp->b_blkno);
957 if ((oldflags & B_ASYNC) == 0) {
958 int rtval = bufwait(bp);
963 * don't allow the async write to saturate the I/O
964 * system. We will not deadlock here because
965 * we are blocking waiting for I/O that is already in-progress
966 * to complete. We do not block here if it is the update
967 * or syncer daemon trying to clean up as that can lead
970 if ((curthread->td_pflags & TDP_NORUNNINGBUF) == 0 && !vp_md)
971 waitrunningbufspace();
978 bufbdflush(struct bufobj *bo, struct buf *bp)
982 if (bo->bo_dirty.bv_cnt > dirtybufthresh + 10) {
983 (void) VOP_FSYNC(bp->b_vp, MNT_NOWAIT, curthread);
985 } else if (bo->bo_dirty.bv_cnt > dirtybufthresh) {
988 * Try to find a buffer to flush.
990 TAILQ_FOREACH(nbp, &bo->bo_dirty.bv_hd, b_bobufs) {
991 if ((nbp->b_vflags & BV_BKGRDINPROG) ||
993 LK_EXCLUSIVE | LK_NOWAIT, NULL))
996 panic("bdwrite: found ourselves");
998 /* Don't countdeps with the bo lock held. */
999 if (buf_countdeps(nbp, 0)) {
1004 if (nbp->b_flags & B_CLUSTEROK) {
1005 vfs_bio_awrite(nbp);
1010 dirtybufferflushes++;
1019 * Delayed write. (Buffer is marked dirty). Do not bother writing
1020 * anything if the buffer is marked invalid.
1022 * Note that since the buffer must be completely valid, we can safely
1023 * set B_CACHE. In fact, we have to set B_CACHE here rather then in
1024 * biodone() in order to prevent getblk from writing the buffer
1025 * out synchronously.
1028 bdwrite(struct buf *bp)
1030 struct thread *td = curthread;
1034 CTR3(KTR_BUF, "bdwrite(%p) vp %p flags %X", bp, bp->b_vp, bp->b_flags);
1035 KASSERT(bp->b_bufobj != NULL, ("No b_bufobj %p", bp));
1036 BUF_ASSERT_HELD(bp);
1038 if (bp->b_flags & B_INVAL) {
1044 * If we have too many dirty buffers, don't create any more.
1045 * If we are wildly over our limit, then force a complete
1046 * cleanup. Otherwise, just keep the situation from getting
1047 * out of control. Note that we have to avoid a recursive
1048 * disaster and not try to clean up after our own cleanup!
1052 if ((td->td_pflags & (TDP_COWINPROGRESS|TDP_INBDFLUSH)) == 0) {
1053 td->td_pflags |= TDP_INBDFLUSH;
1055 td->td_pflags &= ~TDP_INBDFLUSH;
1061 * Set B_CACHE, indicating that the buffer is fully valid. This is
1062 * true even of NFS now.
1064 bp->b_flags |= B_CACHE;
1067 * This bmap keeps the system from needing to do the bmap later,
1068 * perhaps when the system is attempting to do a sync. Since it
1069 * is likely that the indirect block -- or whatever other datastructure
1070 * that the filesystem needs is still in memory now, it is a good
1071 * thing to do this. Note also, that if the pageout daemon is
1072 * requesting a sync -- there might not be enough memory to do
1073 * the bmap then... So, this is important to do.
1075 if (vp->v_type != VCHR && bp->b_lblkno == bp->b_blkno) {
1076 VOP_BMAP(vp, bp->b_lblkno, NULL, &bp->b_blkno, NULL, NULL);
1080 * Set the *dirty* buffer range based upon the VM system dirty
1083 * Mark the buffer pages as clean. We need to do this here to
1084 * satisfy the vnode_pager and the pageout daemon, so that it
1085 * thinks that the pages have been "cleaned". Note that since
1086 * the pages are in a delayed write buffer -- the VFS layer
1087 * "will" see that the pages get written out on the next sync,
1088 * or perhaps the cluster will be completed.
1090 vfs_clean_pages_dirty_buf(bp);
1094 * Wakeup the buffer flushing daemon if we have a lot of dirty
1095 * buffers (midpoint between our recovery point and our stall
1098 bd_wakeup((lodirtybuffers + hidirtybuffers) / 2);
1101 * note: we cannot initiate I/O from a bdwrite even if we wanted to,
1102 * due to the softdep code.
1109 * Turn buffer into delayed write request. We must clear BIO_READ and
1110 * B_RELBUF, and we must set B_DELWRI. We reassign the buffer to
1111 * itself to properly update it in the dirty/clean lists. We mark it
1112 * B_DONE to ensure that any asynchronization of the buffer properly
1113 * clears B_DONE ( else a panic will occur later ).
1115 * bdirty() is kinda like bdwrite() - we have to clear B_INVAL which
1116 * might have been set pre-getblk(). Unlike bwrite/bdwrite, bdirty()
1117 * should only be called if the buffer is known-good.
1119 * Since the buffer is not on a queue, we do not update the numfreebuffers
1122 * The buffer must be on QUEUE_NONE.
1125 bdirty(struct buf *bp)
1128 CTR3(KTR_BUF, "bdirty(%p) vp %p flags %X",
1129 bp, bp->b_vp, bp->b_flags);
1130 KASSERT(bp->b_bufobj != NULL, ("No b_bufobj %p", bp));
1131 KASSERT(bp->b_flags & B_REMFREE || bp->b_qindex == QUEUE_NONE,
1132 ("bdirty: buffer %p still on queue %d", bp, bp->b_qindex));
1133 BUF_ASSERT_HELD(bp);
1134 bp->b_flags &= ~(B_RELBUF);
1135 bp->b_iocmd = BIO_WRITE;
1137 if ((bp->b_flags & B_DELWRI) == 0) {
1138 bp->b_flags |= /* XXX B_DONE | */ B_DELWRI;
1140 atomic_add_int(&numdirtybuffers, 1);
1141 bd_wakeup((lodirtybuffers + hidirtybuffers) / 2);
1148 * Clear B_DELWRI for buffer.
1150 * Since the buffer is not on a queue, we do not update the numfreebuffers
1153 * The buffer must be on QUEUE_NONE.
1157 bundirty(struct buf *bp)
1160 CTR3(KTR_BUF, "bundirty(%p) vp %p flags %X", bp, bp->b_vp, bp->b_flags);
1161 KASSERT(bp->b_bufobj != NULL, ("No b_bufobj %p", bp));
1162 KASSERT(bp->b_flags & B_REMFREE || bp->b_qindex == QUEUE_NONE,
1163 ("bundirty: buffer %p still on queue %d", bp, bp->b_qindex));
1164 BUF_ASSERT_HELD(bp);
1166 if (bp->b_flags & B_DELWRI) {
1167 bp->b_flags &= ~B_DELWRI;
1169 atomic_subtract_int(&numdirtybuffers, 1);
1170 numdirtywakeup(lodirtybuffers);
1173 * Since it is now being written, we can clear its deferred write flag.
1175 bp->b_flags &= ~B_DEFERRED;
1181 * Asynchronous write. Start output on a buffer, but do not wait for
1182 * it to complete. The buffer is released when the output completes.
1184 * bwrite() ( or the VOP routine anyway ) is responsible for handling
1185 * B_INVAL buffers. Not us.
1188 bawrite(struct buf *bp)
1191 bp->b_flags |= B_ASYNC;
1198 * Called prior to the locking of any vnodes when we are expecting to
1199 * write. We do not want to starve the buffer cache with too many
1200 * dirty buffers so we block here. By blocking prior to the locking
1201 * of any vnodes we attempt to avoid the situation where a locked vnode
1202 * prevents the various system daemons from flushing related buffers.
1209 if (numdirtybuffers >= hidirtybuffers) {
1211 while (numdirtybuffers >= hidirtybuffers) {
1213 needsbuffer |= VFS_BIO_NEED_DIRTYFLUSH;
1214 msleep(&needsbuffer, &nblock,
1215 (PRIBIO + 4), "flswai", 0);
1217 mtx_unlock(&nblock);
1222 * Return true if we have too many dirty buffers.
1225 buf_dirty_count_severe(void)
1228 return(numdirtybuffers >= hidirtybuffers);
1231 static __noinline int
1232 buf_vm_page_count_severe(void)
1235 KFAIL_POINT_CODE(DEBUG_FP, buf_pressure, return 1);
1237 return vm_page_count_severe();
1243 * Release a busy buffer and, if requested, free its resources. The
1244 * buffer will be stashed in the appropriate bufqueue[] allowing it
1245 * to be accessed later as a cache entity or reused for other purposes.
1248 brelse(struct buf *bp)
1250 CTR3(KTR_BUF, "brelse(%p) vp %p flags %X",
1251 bp, bp->b_vp, bp->b_flags);
1252 KASSERT(!(bp->b_flags & (B_CLUSTER|B_PAGING)),
1253 ("brelse: inappropriate B_PAGING or B_CLUSTER bp %p", bp));
1255 if (bp->b_flags & B_MANAGED) {
1260 if (bp->b_iocmd == BIO_WRITE && (bp->b_ioflags & BIO_ERROR) &&
1261 bp->b_error == EIO && !(bp->b_flags & B_INVAL)) {
1263 * Failed write, redirty. Must clear BIO_ERROR to prevent
1264 * pages from being scrapped. If the error is anything
1265 * other than an I/O error (EIO), assume that retrying
1268 bp->b_ioflags &= ~BIO_ERROR;
1270 } else if ((bp->b_flags & (B_NOCACHE | B_INVAL)) ||
1271 (bp->b_ioflags & BIO_ERROR) || (bp->b_bufsize <= 0)) {
1273 * Either a failed I/O or we were asked to free or not
1276 bp->b_flags |= B_INVAL;
1277 if (!LIST_EMPTY(&bp->b_dep))
1279 if (bp->b_flags & B_DELWRI) {
1280 atomic_subtract_int(&numdirtybuffers, 1);
1281 numdirtywakeup(lodirtybuffers);
1283 bp->b_flags &= ~(B_DELWRI | B_CACHE);
1284 if ((bp->b_flags & B_VMIO) == 0) {
1293 * We must clear B_RELBUF if B_DELWRI is set. If vfs_vmio_release()
1294 * is called with B_DELWRI set, the underlying pages may wind up
1295 * getting freed causing a previous write (bdwrite()) to get 'lost'
1296 * because pages associated with a B_DELWRI bp are marked clean.
1298 * We still allow the B_INVAL case to call vfs_vmio_release(), even
1299 * if B_DELWRI is set.
1301 * If B_DELWRI is not set we may have to set B_RELBUF if we are low
1302 * on pages to return pages to the VM page queues.
1304 if (bp->b_flags & B_DELWRI)
1305 bp->b_flags &= ~B_RELBUF;
1306 else if (buf_vm_page_count_severe()) {
1308 * The locking of the BO_LOCK is not necessary since
1309 * BKGRDINPROG cannot be set while we hold the buf
1310 * lock, it can only be cleared if it is already
1314 if (!(bp->b_vflags & BV_BKGRDINPROG))
1315 bp->b_flags |= B_RELBUF;
1317 bp->b_flags |= B_RELBUF;
1321 * VMIO buffer rundown. It is not very necessary to keep a VMIO buffer
1322 * constituted, not even NFS buffers now. Two flags effect this. If
1323 * B_INVAL, the struct buf is invalidated but the VM object is kept
1324 * around ( i.e. so it is trivial to reconstitute the buffer later ).
1326 * If BIO_ERROR or B_NOCACHE is set, pages in the VM object will be
1327 * invalidated. BIO_ERROR cannot be set for a failed write unless the
1328 * buffer is also B_INVAL because it hits the re-dirtying code above.
1330 * Normally we can do this whether a buffer is B_DELWRI or not. If
1331 * the buffer is an NFS buffer, it is tracking piecemeal writes or
1332 * the commit state and we cannot afford to lose the buffer. If the
1333 * buffer has a background write in progress, we need to keep it
1334 * around to prevent it from being reconstituted and starting a second
1337 if ((bp->b_flags & B_VMIO)
1338 && !(bp->b_vp->v_mount != NULL &&
1339 (bp->b_vp->v_mount->mnt_vfc->vfc_flags & VFCF_NETWORK) != 0 &&
1340 !vn_isdisk(bp->b_vp, NULL) &&
1341 (bp->b_flags & B_DELWRI))
1350 obj = bp->b_bufobj->bo_object;
1353 * Get the base offset and length of the buffer. Note that
1354 * in the VMIO case if the buffer block size is not
1355 * page-aligned then b_data pointer may not be page-aligned.
1356 * But our b_pages[] array *IS* page aligned.
1358 * block sizes less then DEV_BSIZE (usually 512) are not
1359 * supported due to the page granularity bits (m->valid,
1360 * m->dirty, etc...).
1362 * See man buf(9) for more information
1364 resid = bp->b_bufsize;
1365 foff = bp->b_offset;
1366 VM_OBJECT_LOCK(obj);
1367 for (i = 0; i < bp->b_npages; i++) {
1373 * If we hit a bogus page, fixup *all* the bogus pages
1376 if (m == bogus_page) {
1377 poff = OFF_TO_IDX(bp->b_offset);
1380 for (j = i; j < bp->b_npages; j++) {
1382 mtmp = bp->b_pages[j];
1383 if (mtmp == bogus_page) {
1384 mtmp = vm_page_lookup(obj, poff + j);
1386 panic("brelse: page missing\n");
1388 bp->b_pages[j] = mtmp;
1392 if ((bp->b_flags & B_INVAL) == 0) {
1394 trunc_page((vm_offset_t)bp->b_data),
1395 bp->b_pages, bp->b_npages);
1399 if ((bp->b_flags & B_NOCACHE) ||
1400 (bp->b_ioflags & BIO_ERROR &&
1401 bp->b_iocmd == BIO_READ)) {
1402 int poffset = foff & PAGE_MASK;
1403 int presid = resid > (PAGE_SIZE - poffset) ?
1404 (PAGE_SIZE - poffset) : resid;
1406 KASSERT(presid >= 0, ("brelse: extra page"));
1407 vm_page_set_invalid(m, poffset, presid);
1409 printf("avoided corruption bug in bogus_page/brelse code\n");
1411 resid -= PAGE_SIZE - (foff & PAGE_MASK);
1412 foff = (foff + PAGE_SIZE) & ~(off_t)PAGE_MASK;
1414 VM_OBJECT_UNLOCK(obj);
1415 if (bp->b_flags & (B_INVAL | B_RELBUF))
1416 vfs_vmio_release(bp);
1418 } else if (bp->b_flags & B_VMIO) {
1420 if (bp->b_flags & (B_INVAL | B_RELBUF)) {
1421 vfs_vmio_release(bp);
1424 } else if ((bp->b_flags & (B_INVAL | B_RELBUF)) != 0) {
1425 if (bp->b_bufsize != 0)
1427 if (bp->b_vp != NULL)
1431 if (BUF_LOCKRECURSED(bp)) {
1432 /* do not release to free list */
1439 /* Handle delayed bremfree() processing. */
1440 if (bp->b_flags & B_REMFREE) {
1450 if (bp->b_qindex != QUEUE_NONE)
1451 panic("brelse: free buffer onto another queue???");
1454 * If the buffer has junk contents signal it and eventually
1455 * clean up B_DELWRI and diassociate the vnode so that gbincore()
1458 if (bp->b_bufsize == 0 || (bp->b_ioflags & BIO_ERROR) != 0 ||
1459 (bp->b_flags & (B_INVAL | B_NOCACHE | B_RELBUF)) != 0)
1460 bp->b_flags |= B_INVAL;
1461 if (bp->b_flags & B_INVAL) {
1462 if (bp->b_flags & B_DELWRI)
1468 /* buffers with no memory */
1469 if (bp->b_bufsize == 0) {
1470 bp->b_xflags &= ~(BX_BKGRDWRITE | BX_ALTDATA);
1471 if (bp->b_vflags & BV_BKGRDINPROG)
1472 panic("losing buffer 1");
1473 if (bp->b_kvasize) {
1474 bp->b_qindex = QUEUE_EMPTYKVA;
1476 bp->b_qindex = QUEUE_EMPTY;
1478 TAILQ_INSERT_HEAD(&bufqueues[bp->b_qindex], bp, b_freelist);
1479 /* buffers with junk contents */
1480 } else if (bp->b_flags & (B_INVAL | B_NOCACHE | B_RELBUF) ||
1481 (bp->b_ioflags & BIO_ERROR)) {
1482 bp->b_xflags &= ~(BX_BKGRDWRITE | BX_ALTDATA);
1483 if (bp->b_vflags & BV_BKGRDINPROG)
1484 panic("losing buffer 2");
1485 bp->b_qindex = QUEUE_CLEAN;
1486 TAILQ_INSERT_HEAD(&bufqueues[QUEUE_CLEAN], bp, b_freelist);
1487 /* remaining buffers */
1489 if ((bp->b_flags & (B_DELWRI|B_NEEDSGIANT)) ==
1490 (B_DELWRI|B_NEEDSGIANT))
1491 bp->b_qindex = QUEUE_DIRTY_GIANT;
1492 else if (bp->b_flags & B_DELWRI)
1493 bp->b_qindex = QUEUE_DIRTY;
1495 bp->b_qindex = QUEUE_CLEAN;
1496 if (bp->b_flags & B_AGE)
1497 TAILQ_INSERT_HEAD(&bufqueues[bp->b_qindex], bp, b_freelist);
1499 TAILQ_INSERT_TAIL(&bufqueues[bp->b_qindex], bp, b_freelist);
1501 mtx_unlock(&bqlock);
1504 * Fixup numfreebuffers count. The bp is on an appropriate queue
1505 * unless locked. We then bump numfreebuffers if it is not B_DELWRI.
1506 * We've already handled the B_INVAL case ( B_DELWRI will be clear
1507 * if B_INVAL is set ).
1510 if (!(bp->b_flags & B_DELWRI)) {
1522 * Something we can maybe free or reuse
1524 if (bp->b_bufsize || bp->b_kvasize)
1527 bp->b_flags &= ~(B_ASYNC | B_NOCACHE | B_AGE | B_RELBUF | B_DIRECT);
1528 if ((bp->b_flags & B_DELWRI) == 0 && (bp->b_xflags & BX_VNDIRTY))
1529 panic("brelse: not dirty");
1535 * Release a buffer back to the appropriate queue but do not try to free
1536 * it. The buffer is expected to be used again soon.
1538 * bqrelse() is used by bdwrite() to requeue a delayed write, and used by
1539 * biodone() to requeue an async I/O on completion. It is also used when
1540 * known good buffers need to be requeued but we think we may need the data
1543 * XXX we should be able to leave the B_RELBUF hint set on completion.
1546 bqrelse(struct buf *bp)
1550 CTR3(KTR_BUF, "bqrelse(%p) vp %p flags %X", bp, bp->b_vp, bp->b_flags);
1551 KASSERT(!(bp->b_flags & (B_CLUSTER|B_PAGING)),
1552 ("bqrelse: inappropriate B_PAGING or B_CLUSTER bp %p", bp));
1554 if (BUF_LOCKRECURSED(bp)) {
1555 /* do not release to free list */
1561 if (bp->b_flags & B_MANAGED) {
1562 if (bp->b_flags & B_REMFREE) {
1569 mtx_unlock(&bqlock);
1571 bp->b_flags &= ~(B_ASYNC | B_NOCACHE | B_AGE | B_RELBUF);
1577 /* Handle delayed bremfree() processing. */
1578 if (bp->b_flags & B_REMFREE) {
1585 if (bp->b_qindex != QUEUE_NONE)
1586 panic("bqrelse: free buffer onto another queue???");
1587 /* buffers with stale but valid contents */
1588 if (bp->b_flags & B_DELWRI) {
1589 if (bp->b_flags & B_NEEDSGIANT)
1590 bp->b_qindex = QUEUE_DIRTY_GIANT;
1592 bp->b_qindex = QUEUE_DIRTY;
1593 TAILQ_INSERT_TAIL(&bufqueues[bp->b_qindex], bp, b_freelist);
1596 * The locking of the BO_LOCK for checking of the
1597 * BV_BKGRDINPROG is not necessary since the
1598 * BV_BKGRDINPROG cannot be set while we hold the buf
1599 * lock, it can only be cleared if it is already
1602 if (!buf_vm_page_count_severe() || (bp->b_vflags & BV_BKGRDINPROG)) {
1603 bp->b_qindex = QUEUE_CLEAN;
1604 TAILQ_INSERT_TAIL(&bufqueues[QUEUE_CLEAN], bp,
1608 * We are too low on memory, we have to try to free
1609 * the buffer (most importantly: the wired pages
1610 * making up its backing store) *now*.
1612 mtx_unlock(&bqlock);
1617 mtx_unlock(&bqlock);
1619 if ((bp->b_flags & B_INVAL) || !(bp->b_flags & B_DELWRI)) {
1628 * Something we can maybe free or reuse.
1630 if (bp->b_bufsize && !(bp->b_flags & B_DELWRI))
1633 bp->b_flags &= ~(B_ASYNC | B_NOCACHE | B_AGE | B_RELBUF);
1634 if ((bp->b_flags & B_DELWRI) == 0 && (bp->b_xflags & BX_VNDIRTY))
1635 panic("bqrelse: not dirty");
1640 /* Give pages used by the bp back to the VM system (where possible) */
1642 vfs_vmio_release(struct buf *bp)
1647 pmap_qremove(trunc_page((vm_offset_t)bp->b_data), bp->b_npages);
1648 VM_OBJECT_LOCK(bp->b_bufobj->bo_object);
1649 for (i = 0; i < bp->b_npages; i++) {
1651 bp->b_pages[i] = NULL;
1653 * In order to keep page LRU ordering consistent, put
1654 * everything on the inactive queue.
1657 vm_page_unwire(m, 0);
1659 * We don't mess with busy pages, it is
1660 * the responsibility of the process that
1661 * busied the pages to deal with them.
1663 if ((m->oflags & VPO_BUSY) == 0 && m->busy == 0 &&
1664 m->wire_count == 0) {
1666 * Might as well free the page if we can and it has
1667 * no valid data. We also free the page if the
1668 * buffer was used for direct I/O
1670 if ((bp->b_flags & B_ASYNC) == 0 && !m->valid) {
1672 } else if (bp->b_flags & B_DIRECT) {
1673 vm_page_try_to_free(m);
1674 } else if (buf_vm_page_count_severe()) {
1675 vm_page_try_to_cache(m);
1680 VM_OBJECT_UNLOCK(bp->b_bufobj->bo_object);
1682 if (bp->b_bufsize) {
1687 bp->b_flags &= ~B_VMIO;
1693 * Check to see if a block at a particular lbn is available for a clustered
1697 vfs_bio_clcheck(struct vnode *vp, int size, daddr_t lblkno, daddr_t blkno)
1704 /* If the buf isn't in core skip it */
1705 if ((bpa = gbincore(&vp->v_bufobj, lblkno)) == NULL)
1708 /* If the buf is busy we don't want to wait for it */
1709 if (BUF_LOCK(bpa, LK_EXCLUSIVE | LK_NOWAIT, NULL) != 0)
1712 /* Only cluster with valid clusterable delayed write buffers */
1713 if ((bpa->b_flags & (B_DELWRI | B_CLUSTEROK | B_INVAL)) !=
1714 (B_DELWRI | B_CLUSTEROK))
1717 if (bpa->b_bufsize != size)
1721 * Check to see if it is in the expected place on disk and that the
1722 * block has been mapped.
1724 if ((bpa->b_blkno != bpa->b_lblkno) && (bpa->b_blkno == blkno))
1734 * Implement clustered async writes for clearing out B_DELWRI buffers.
1735 * This is much better then the old way of writing only one buffer at
1736 * a time. Note that we may not be presented with the buffers in the
1737 * correct order, so we search for the cluster in both directions.
1740 vfs_bio_awrite(struct buf *bp)
1745 daddr_t lblkno = bp->b_lblkno;
1746 struct vnode *vp = bp->b_vp;
1754 * right now we support clustered writing only to regular files. If
1755 * we find a clusterable block we could be in the middle of a cluster
1756 * rather then at the beginning.
1758 if ((vp->v_type == VREG) &&
1759 (vp->v_mount != 0) && /* Only on nodes that have the size info */
1760 (bp->b_flags & (B_CLUSTEROK | B_INVAL)) == B_CLUSTEROK) {
1762 size = vp->v_mount->mnt_stat.f_iosize;
1763 maxcl = MAXPHYS / size;
1766 for (i = 1; i < maxcl; i++)
1767 if (vfs_bio_clcheck(vp, size, lblkno + i,
1768 bp->b_blkno + ((i * size) >> DEV_BSHIFT)) == 0)
1771 for (j = 1; i + j <= maxcl && j <= lblkno; j++)
1772 if (vfs_bio_clcheck(vp, size, lblkno - j,
1773 bp->b_blkno - ((j * size) >> DEV_BSHIFT)) == 0)
1779 * this is a possible cluster write
1783 nwritten = cluster_wbuild(vp, size, lblkno - j, ncl);
1788 bp->b_flags |= B_ASYNC;
1790 * default (old) behavior, writing out only one block
1792 * XXX returns b_bufsize instead of b_bcount for nwritten?
1794 nwritten = bp->b_bufsize;
1803 * Find and initialize a new buffer header, freeing up existing buffers
1804 * in the bufqueues as necessary. The new buffer is returned locked.
1806 * Important: B_INVAL is not set. If the caller wishes to throw the
1807 * buffer away, the caller must set B_INVAL prior to calling brelse().
1810 * We have insufficient buffer headers
1811 * We have insufficient buffer space
1812 * buffer_map is too fragmented ( space reservation fails )
1813 * If we have to flush dirty buffers ( but we try to avoid this )
1815 * To avoid VFS layer recursion we do not flush dirty buffers ourselves.
1816 * Instead we ask the buf daemon to do it for us. We attempt to
1817 * avoid piecemeal wakeups of the pageout daemon.
1821 getnewbuf(struct vnode *vp, int slpflag, int slptimeo, int size, int maxsize,
1829 static int flushingbufs;
1833 * We can't afford to block since we might be holding a vnode lock,
1834 * which may prevent system daemons from running. We deal with
1835 * low-memory situations by proactively returning memory and running
1836 * async I/O rather then sync I/O.
1838 atomic_add_int(&getnewbufcalls, 1);
1839 atomic_subtract_int(&getnewbufrestarts, 1);
1841 atomic_add_int(&getnewbufrestarts, 1);
1844 * Setup for scan. If we do not have enough free buffers,
1845 * we setup a degenerate case that immediately fails. Note
1846 * that if we are specially marked process, we are allowed to
1847 * dip into our reserves.
1849 * The scanning sequence is nominally: EMPTY->EMPTYKVA->CLEAN
1851 * We start with EMPTYKVA. If the list is empty we backup to EMPTY.
1852 * However, there are a number of cases (defragging, reusing, ...)
1853 * where we cannot backup.
1856 nqindex = QUEUE_EMPTYKVA;
1857 nbp = TAILQ_FIRST(&bufqueues[QUEUE_EMPTYKVA]);
1861 * If no EMPTYKVA buffers and we are either
1862 * defragging or reusing, locate a CLEAN buffer
1863 * to free or reuse. If bufspace useage is low
1864 * skip this step so we can allocate a new buffer.
1866 if (defrag || bufspace >= lobufspace) {
1867 nqindex = QUEUE_CLEAN;
1868 nbp = TAILQ_FIRST(&bufqueues[QUEUE_CLEAN]);
1872 * If we could not find or were not allowed to reuse a
1873 * CLEAN buffer, check to see if it is ok to use an EMPTY
1874 * buffer. We can only use an EMPTY buffer if allocating
1875 * its KVA would not otherwise run us out of buffer space.
1877 if (nbp == NULL && defrag == 0 &&
1878 bufspace + maxsize < hibufspace) {
1879 nqindex = QUEUE_EMPTY;
1880 nbp = TAILQ_FIRST(&bufqueues[QUEUE_EMPTY]);
1885 * Run scan, possibly freeing data and/or kva mappings on the fly
1889 while ((bp = nbp) != NULL) {
1890 int qindex = nqindex;
1893 * Calculate next bp ( we can only use it if we do not block
1894 * or do other fancy things ).
1896 if ((nbp = TAILQ_NEXT(bp, b_freelist)) == NULL) {
1899 nqindex = QUEUE_EMPTYKVA;
1900 if ((nbp = TAILQ_FIRST(&bufqueues[QUEUE_EMPTYKVA])))
1903 case QUEUE_EMPTYKVA:
1904 nqindex = QUEUE_CLEAN;
1905 if ((nbp = TAILQ_FIRST(&bufqueues[QUEUE_CLEAN])))
1916 * If we are defragging then we need a buffer with
1917 * b_kvasize != 0. XXX this situation should no longer
1918 * occur, if defrag is non-zero the buffer's b_kvasize
1919 * should also be non-zero at this point. XXX
1921 if (defrag && bp->b_kvasize == 0) {
1922 printf("Warning: defrag empty buffer %p\n", bp);
1927 * Start freeing the bp. This is somewhat involved. nbp
1928 * remains valid only for QUEUE_EMPTY[KVA] bp's.
1930 if (BUF_LOCK(bp, LK_EXCLUSIVE | LK_NOWAIT, NULL) != 0)
1933 BO_LOCK(bp->b_bufobj);
1934 if (bp->b_vflags & BV_BKGRDINPROG) {
1935 BO_UNLOCK(bp->b_bufobj);
1939 BO_UNLOCK(bp->b_bufobj);
1942 "getnewbuf(%p) vp %p flags %X kvasize %d bufsize %d "
1943 "queue %d (recycling)", bp, bp->b_vp, bp->b_flags,
1944 bp->b_kvasize, bp->b_bufsize, qindex);
1949 KASSERT(bp->b_qindex == qindex, ("getnewbuf: inconsistant queue %d bp %p", qindex, bp));
1952 * Note: we no longer distinguish between VMIO and non-VMIO
1956 KASSERT((bp->b_flags & B_DELWRI) == 0, ("delwri buffer %p found in queue %d", bp, qindex));
1958 if (bp->b_bufobj != NULL)
1959 BO_LOCK(bp->b_bufobj);
1961 if (bp->b_bufobj != NULL)
1962 BO_UNLOCK(bp->b_bufobj);
1963 mtx_unlock(&bqlock);
1965 if (qindex == QUEUE_CLEAN) {
1966 if (bp->b_flags & B_VMIO) {
1967 bp->b_flags &= ~B_ASYNC;
1968 vfs_vmio_release(bp);
1975 * NOTE: nbp is now entirely invalid. We can only restart
1976 * the scan from this point on.
1978 * Get the rest of the buffer freed up. b_kva* is still
1979 * valid after this operation.
1982 if (bp->b_rcred != NOCRED) {
1983 crfree(bp->b_rcred);
1984 bp->b_rcred = NOCRED;
1986 if (bp->b_wcred != NOCRED) {
1987 crfree(bp->b_wcred);
1988 bp->b_wcred = NOCRED;
1990 if (!LIST_EMPTY(&bp->b_dep))
1992 if (bp->b_vflags & BV_BKGRDINPROG)
1993 panic("losing buffer 3");
1994 KASSERT(bp->b_vp == NULL,
1995 ("bp: %p still has vnode %p. qindex: %d",
1996 bp, bp->b_vp, qindex));
1997 KASSERT((bp->b_xflags & (BX_VNCLEAN|BX_VNDIRTY)) == 0,
1998 ("bp: %p still on a buffer list. xflags %X",
2007 KASSERT((bp->b_vflags & BV_INFREECNT) == 0,
2008 ("buf %p still counted as free?", bp));
2011 bp->b_blkno = bp->b_lblkno = 0;
2012 bp->b_offset = NOOFFSET;
2018 bp->b_dirtyoff = bp->b_dirtyend = 0;
2019 bp->b_bufobj = NULL;
2020 bp->b_pin_count = 0;
2021 bp->b_fsprivate1 = NULL;
2022 bp->b_fsprivate2 = NULL;
2023 bp->b_fsprivate3 = NULL;
2025 LIST_INIT(&bp->b_dep);
2028 * If we are defragging then free the buffer.
2031 bp->b_flags |= B_INVAL;
2039 * Notify any waiters for the buffer lock about
2040 * identity change by freeing the buffer.
2042 if (qindex == QUEUE_CLEAN && BUF_LOCKWAITERS(bp)) {
2043 bp->b_flags |= B_INVAL;
2050 * If we are overcomitted then recover the buffer and its
2051 * KVM space. This occurs in rare situations when multiple
2052 * processes are blocked in getnewbuf() or allocbuf().
2054 if (bufspace >= hibufspace)
2056 if (flushingbufs && bp->b_kvasize != 0) {
2057 bp->b_flags |= B_INVAL;
2062 if (bufspace < lobufspace)
2068 * If we exhausted our list, sleep as appropriate. We may have to
2069 * wakeup various daemons and write out some dirty buffers.
2071 * Generally we are sleeping due to insufficient buffer space.
2075 int flags, norunbuf;
2080 flags = VFS_BIO_NEED_BUFSPACE;
2082 } else if (bufspace >= hibufspace) {
2084 flags = VFS_BIO_NEED_BUFSPACE;
2087 flags = VFS_BIO_NEED_ANY;
2090 needsbuffer |= flags;
2091 mtx_unlock(&nblock);
2092 mtx_unlock(&bqlock);
2094 bd_speedup(); /* heeeelp */
2095 if (gbflags & GB_NOWAIT_BD)
2099 while (needsbuffer & flags) {
2100 if (vp != NULL && (td->td_pflags & TDP_BUFNEED) == 0) {
2101 mtx_unlock(&nblock);
2103 * getblk() is called with a vnode
2104 * locked, and some majority of the
2105 * dirty buffers may as well belong to
2106 * the vnode. Flushing the buffers
2107 * there would make a progress that
2108 * cannot be achieved by the
2109 * buf_daemon, that cannot lock the
2112 norunbuf = ~(TDP_BUFNEED | TDP_NORUNNINGBUF) |
2113 (td->td_pflags & TDP_NORUNNINGBUF);
2114 /* play bufdaemon */
2115 td->td_pflags |= TDP_BUFNEED | TDP_NORUNNINGBUF;
2116 fl = buf_do_flush(vp);
2117 td->td_pflags &= norunbuf;
2121 if ((needsbuffer & flags) == 0)
2124 if (msleep(&needsbuffer, &nblock,
2125 (PRIBIO + 4) | slpflag, waitmsg, slptimeo)) {
2126 mtx_unlock(&nblock);
2130 mtx_unlock(&nblock);
2133 * We finally have a valid bp. We aren't quite out of the
2134 * woods, we still have to reserve kva space. In order
2135 * to keep fragmentation sane we only allocate kva in
2138 maxsize = (maxsize + BKVAMASK) & ~BKVAMASK;
2140 if (maxsize != bp->b_kvasize) {
2141 vm_offset_t addr = 0;
2145 vm_map_lock(buffer_map);
2146 if (vm_map_findspace(buffer_map,
2147 vm_map_min(buffer_map), maxsize, &addr)) {
2149 * Uh oh. Buffer map is to fragmented. We
2150 * must defragment the map.
2152 atomic_add_int(&bufdefragcnt, 1);
2153 vm_map_unlock(buffer_map);
2155 bp->b_flags |= B_INVAL;
2160 vm_map_insert(buffer_map, NULL, 0,
2161 addr, addr + maxsize,
2162 VM_PROT_ALL, VM_PROT_ALL, MAP_NOFAULT);
2164 bp->b_kvabase = (caddr_t) addr;
2165 bp->b_kvasize = maxsize;
2166 atomic_add_long(&bufspace, bp->b_kvasize);
2167 atomic_add_int(&bufreusecnt, 1);
2169 vm_map_unlock(buffer_map);
2171 bp->b_saveaddr = bp->b_kvabase;
2172 bp->b_data = bp->b_saveaddr;
2180 * buffer flushing daemon. Buffers are normally flushed by the
2181 * update daemon but if it cannot keep up this process starts to
2182 * take the load in an attempt to prevent getnewbuf() from blocking.
2185 static struct kproc_desc buf_kp = {
2190 SYSINIT(bufdaemon, SI_SUB_KTHREAD_BUF, SI_ORDER_FIRST, kproc_start, &buf_kp);
2193 buf_do_flush(struct vnode *vp)
2197 flushed = flushbufqueues(vp, QUEUE_DIRTY, 0);
2198 /* The list empty check here is slightly racy */
2199 if (!TAILQ_EMPTY(&bufqueues[QUEUE_DIRTY_GIANT])) {
2201 flushed += flushbufqueues(vp, QUEUE_DIRTY_GIANT, 0);
2206 * Could not find any buffers without rollback
2207 * dependencies, so just write the first one
2208 * in the hopes of eventually making progress.
2210 flushbufqueues(vp, QUEUE_DIRTY, 1);
2212 &bufqueues[QUEUE_DIRTY_GIANT])) {
2214 flushbufqueues(vp, QUEUE_DIRTY_GIANT, 1);
2227 * This process needs to be suspended prior to shutdown sync.
2229 EVENTHANDLER_REGISTER(shutdown_pre_sync, kproc_shutdown, bufdaemonproc,
2233 * This process is allowed to take the buffer cache to the limit
2235 curthread->td_pflags |= TDP_NORUNNINGBUF | TDP_BUFNEED;
2239 mtx_unlock(&bdlock);
2241 kproc_suspend_check(bufdaemonproc);
2242 lodirtysave = lodirtybuffers;
2243 if (bd_speedupreq) {
2244 lodirtybuffers = numdirtybuffers / 2;
2248 * Do the flush. Limit the amount of in-transit I/O we
2249 * allow to build up, otherwise we would completely saturate
2250 * the I/O system. Wakeup any waiting processes before we
2251 * normally would so they can run in parallel with our drain.
2253 while (numdirtybuffers > lodirtybuffers) {
2254 if (buf_do_flush(NULL) == 0)
2256 kern_yield(PRI_UNCHANGED);
2258 lodirtybuffers = lodirtysave;
2261 * Only clear bd_request if we have reached our low water
2262 * mark. The buf_daemon normally waits 1 second and
2263 * then incrementally flushes any dirty buffers that have
2264 * built up, within reason.
2266 * If we were unable to hit our low water mark and couldn't
2267 * find any flushable buffers, we sleep half a second.
2268 * Otherwise we loop immediately.
2271 if (numdirtybuffers <= lodirtybuffers) {
2273 * We reached our low water mark, reset the
2274 * request and sleep until we are needed again.
2275 * The sleep is just so the suspend code works.
2278 msleep(&bd_request, &bdlock, PVM, "psleep", hz);
2281 * We couldn't find any flushable dirty buffers but
2282 * still have too many dirty buffers, we
2283 * have to sleep and try again. (rare)
2285 msleep(&bd_request, &bdlock, PVM, "qsleep", hz / 10);
2293 * Try to flush a buffer in the dirty queue. We must be careful to
2294 * free up B_INVAL buffers instead of write them, which NFS is
2295 * particularly sensitive to.
2297 static int flushwithdeps = 0;
2298 SYSCTL_INT(_vfs, OID_AUTO, flushwithdeps, CTLFLAG_RW, &flushwithdeps,
2299 0, "Number of buffers flushed with dependecies that require rollbacks");
2302 flushbufqueues(struct vnode *lvp, int queue, int flushdeps)
2304 struct buf *sentinel;
2313 target = numdirtybuffers - lodirtybuffers;
2314 if (flushdeps && target > 2)
2317 target = flushbufqtarget;
2320 sentinel = malloc(sizeof(struct buf), M_TEMP, M_WAITOK | M_ZERO);
2321 sentinel->b_qindex = QUEUE_SENTINEL;
2323 TAILQ_INSERT_HEAD(&bufqueues[queue], sentinel, b_freelist);
2324 while (flushed != target) {
2325 bp = TAILQ_NEXT(sentinel, b_freelist);
2327 TAILQ_REMOVE(&bufqueues[queue], sentinel, b_freelist);
2328 TAILQ_INSERT_AFTER(&bufqueues[queue], bp, sentinel,
2333 * Skip sentinels inserted by other invocations of the
2334 * flushbufqueues(), taking care to not reorder them.
2336 if (bp->b_qindex == QUEUE_SENTINEL)
2339 * Only flush the buffers that belong to the
2340 * vnode locked by the curthread.
2342 if (lvp != NULL && bp->b_vp != lvp)
2344 if (BUF_LOCK(bp, LK_EXCLUSIVE | LK_NOWAIT, NULL) != 0)
2346 if (bp->b_pin_count > 0) {
2350 BO_LOCK(bp->b_bufobj);
2351 if ((bp->b_vflags & BV_BKGRDINPROG) != 0 ||
2352 (bp->b_flags & B_DELWRI) == 0) {
2353 BO_UNLOCK(bp->b_bufobj);
2357 BO_UNLOCK(bp->b_bufobj);
2358 if (bp->b_flags & B_INVAL) {
2360 mtx_unlock(&bqlock);
2363 numdirtywakeup((lodirtybuffers + hidirtybuffers) / 2);
2368 if (!LIST_EMPTY(&bp->b_dep) && buf_countdeps(bp, 0)) {
2369 if (flushdeps == 0) {
2377 * We must hold the lock on a vnode before writing
2378 * one of its buffers. Otherwise we may confuse, or
2379 * in the case of a snapshot vnode, deadlock the
2382 * The lock order here is the reverse of the normal
2383 * of vnode followed by buf lock. This is ok because
2384 * the NOWAIT will prevent deadlock.
2387 if (vn_start_write(vp, &mp, V_NOWAIT) != 0) {
2391 if (vn_lock(vp, LK_EXCLUSIVE | LK_NOWAIT | LK_CANRECURSE) == 0) {
2392 mtx_unlock(&bqlock);
2393 CTR3(KTR_BUF, "flushbufqueue(%p) vp %p flags %X",
2394 bp, bp->b_vp, bp->b_flags);
2395 if (curproc == bufdaemonproc)
2402 vn_finished_write(mp);
2404 flushwithdeps += hasdeps;
2408 * Sleeping on runningbufspace while holding
2409 * vnode lock leads to deadlock.
2411 if (curproc == bufdaemonproc)
2412 waitrunningbufspace();
2413 numdirtywakeup((lodirtybuffers + hidirtybuffers) / 2);
2417 vn_finished_write(mp);
2420 TAILQ_REMOVE(&bufqueues[queue], sentinel, b_freelist);
2421 mtx_unlock(&bqlock);
2422 free(sentinel, M_TEMP);
2427 * Check to see if a block is currently memory resident.
2430 incore(struct bufobj *bo, daddr_t blkno)
2435 bp = gbincore(bo, blkno);
2441 * Returns true if no I/O is needed to access the
2442 * associated VM object. This is like incore except
2443 * it also hunts around in the VM system for the data.
2447 inmem(struct vnode * vp, daddr_t blkno)
2450 vm_offset_t toff, tinc, size;
2454 ASSERT_VOP_LOCKED(vp, "inmem");
2456 if (incore(&vp->v_bufobj, blkno))
2458 if (vp->v_mount == NULL)
2465 if (size > vp->v_mount->mnt_stat.f_iosize)
2466 size = vp->v_mount->mnt_stat.f_iosize;
2467 off = (vm_ooffset_t)blkno * (vm_ooffset_t)vp->v_mount->mnt_stat.f_iosize;
2469 VM_OBJECT_LOCK(obj);
2470 for (toff = 0; toff < vp->v_mount->mnt_stat.f_iosize; toff += tinc) {
2471 m = vm_page_lookup(obj, OFF_TO_IDX(off + toff));
2475 if (tinc > PAGE_SIZE - ((toff + off) & PAGE_MASK))
2476 tinc = PAGE_SIZE - ((toff + off) & PAGE_MASK);
2477 if (vm_page_is_valid(m,
2478 (vm_offset_t) ((toff + off) & PAGE_MASK), tinc) == 0)
2481 VM_OBJECT_UNLOCK(obj);
2485 VM_OBJECT_UNLOCK(obj);
2490 * Set the dirty range for a buffer based on the status of the dirty
2491 * bits in the pages comprising the buffer. The range is limited
2492 * to the size of the buffer.
2494 * Tell the VM system that the pages associated with this buffer
2495 * are clean. This is used for delayed writes where the data is
2496 * going to go to disk eventually without additional VM intevention.
2498 * Note that while we only really need to clean through to b_bcount, we
2499 * just go ahead and clean through to b_bufsize.
2502 vfs_clean_pages_dirty_buf(struct buf *bp)
2504 vm_ooffset_t foff, noff, eoff;
2508 if ((bp->b_flags & B_VMIO) == 0 || bp->b_bufsize == 0)
2511 foff = bp->b_offset;
2512 KASSERT(bp->b_offset != NOOFFSET,
2513 ("vfs_clean_pages_dirty_buf: no buffer offset"));
2515 VM_OBJECT_LOCK(bp->b_bufobj->bo_object);
2516 vfs_drain_busy_pages(bp);
2517 vfs_setdirty_locked_object(bp);
2518 for (i = 0; i < bp->b_npages; i++) {
2519 noff = (foff + PAGE_SIZE) & ~(off_t)PAGE_MASK;
2521 if (eoff > bp->b_offset + bp->b_bufsize)
2522 eoff = bp->b_offset + bp->b_bufsize;
2524 vfs_page_set_validclean(bp, foff, m);
2525 /* vm_page_clear_dirty(m, foff & PAGE_MASK, eoff - foff); */
2528 VM_OBJECT_UNLOCK(bp->b_bufobj->bo_object);
2532 vfs_setdirty_locked_object(struct buf *bp)
2537 object = bp->b_bufobj->bo_object;
2538 VM_OBJECT_LOCK_ASSERT(object, MA_OWNED);
2541 * We qualify the scan for modified pages on whether the
2542 * object has been flushed yet.
2544 if ((object->flags & OBJ_MIGHTBEDIRTY) != 0) {
2545 vm_offset_t boffset;
2546 vm_offset_t eoffset;
2549 * test the pages to see if they have been modified directly
2550 * by users through the VM system.
2552 for (i = 0; i < bp->b_npages; i++)
2553 vm_page_test_dirty(bp->b_pages[i]);
2556 * Calculate the encompassing dirty range, boffset and eoffset,
2557 * (eoffset - boffset) bytes.
2560 for (i = 0; i < bp->b_npages; i++) {
2561 if (bp->b_pages[i]->dirty)
2564 boffset = (i << PAGE_SHIFT) - (bp->b_offset & PAGE_MASK);
2566 for (i = bp->b_npages - 1; i >= 0; --i) {
2567 if (bp->b_pages[i]->dirty) {
2571 eoffset = ((i + 1) << PAGE_SHIFT) - (bp->b_offset & PAGE_MASK);
2574 * Fit it to the buffer.
2577 if (eoffset > bp->b_bcount)
2578 eoffset = bp->b_bcount;
2581 * If we have a good dirty range, merge with the existing
2585 if (boffset < eoffset) {
2586 if (bp->b_dirtyoff > boffset)
2587 bp->b_dirtyoff = boffset;
2588 if (bp->b_dirtyend < eoffset)
2589 bp->b_dirtyend = eoffset;
2597 * Get a block given a specified block and offset into a file/device.
2598 * The buffers B_DONE bit will be cleared on return, making it almost
2599 * ready for an I/O initiation. B_INVAL may or may not be set on
2600 * return. The caller should clear B_INVAL prior to initiating a
2603 * For a non-VMIO buffer, B_CACHE is set to the opposite of B_INVAL for
2604 * an existing buffer.
2606 * For a VMIO buffer, B_CACHE is modified according to the backing VM.
2607 * If getblk()ing a previously 0-sized invalid buffer, B_CACHE is set
2608 * and then cleared based on the backing VM. If the previous buffer is
2609 * non-0-sized but invalid, B_CACHE will be cleared.
2611 * If getblk() must create a new buffer, the new buffer is returned with
2612 * both B_INVAL and B_CACHE clear unless it is a VMIO buffer, in which
2613 * case it is returned with B_INVAL clear and B_CACHE set based on the
2616 * getblk() also forces a bwrite() for any B_DELWRI buffer whos
2617 * B_CACHE bit is clear.
2619 * What this means, basically, is that the caller should use B_CACHE to
2620 * determine whether the buffer is fully valid or not and should clear
2621 * B_INVAL prior to issuing a read. If the caller intends to validate
2622 * the buffer by loading its data area with something, the caller needs
2623 * to clear B_INVAL. If the caller does this without issuing an I/O,
2624 * the caller should set B_CACHE ( as an optimization ), else the caller
2625 * should issue the I/O and biodone() will set B_CACHE if the I/O was
2626 * a write attempt or if it was a successfull read. If the caller
2627 * intends to issue a READ, the caller must clear B_INVAL and BIO_ERROR
2628 * prior to issuing the READ. biodone() will *not* clear B_INVAL.
2631 getblk(struct vnode * vp, daddr_t blkno, int size, int slpflag, int slptimeo,
2638 CTR3(KTR_BUF, "getblk(%p, %ld, %d)", vp, (long)blkno, size);
2639 ASSERT_VOP_LOCKED(vp, "getblk");
2640 if (size > MAXBSIZE)
2641 panic("getblk: size(%d) > MAXBSIZE(%d)\n", size, MAXBSIZE);
2646 * Block if we are low on buffers. Certain processes are allowed
2647 * to completely exhaust the buffer cache.
2649 * If this check ever becomes a bottleneck it may be better to
2650 * move it into the else, when gbincore() fails. At the moment
2651 * it isn't a problem.
2653 * XXX remove if 0 sections (clean this up after its proven)
2655 if (numfreebuffers == 0) {
2656 if (TD_IS_IDLETHREAD(curthread))
2659 needsbuffer |= VFS_BIO_NEED_ANY;
2660 mtx_unlock(&nblock);
2664 bp = gbincore(bo, blkno);
2668 * Buffer is in-core. If the buffer is not busy, it must
2671 lockflags = LK_EXCLUSIVE | LK_SLEEPFAIL | LK_INTERLOCK;
2673 if (flags & GB_LOCK_NOWAIT)
2674 lockflags |= LK_NOWAIT;
2676 error = BUF_TIMELOCK(bp, lockflags,
2677 BO_MTX(bo), "getblk", slpflag, slptimeo);
2680 * If we slept and got the lock we have to restart in case
2681 * the buffer changed identities.
2683 if (error == ENOLCK)
2685 /* We timed out or were interrupted. */
2690 * The buffer is locked. B_CACHE is cleared if the buffer is
2691 * invalid. Otherwise, for a non-VMIO buffer, B_CACHE is set
2692 * and for a VMIO buffer B_CACHE is adjusted according to the
2695 if (bp->b_flags & B_INVAL)
2696 bp->b_flags &= ~B_CACHE;
2697 else if ((bp->b_flags & (B_VMIO | B_INVAL)) == 0)
2698 bp->b_flags |= B_CACHE;
2704 * check for size inconsistancies for non-VMIO case.
2707 if (bp->b_bcount != size) {
2708 if ((bp->b_flags & B_VMIO) == 0 ||
2709 (size > bp->b_kvasize)) {
2710 if (bp->b_flags & B_DELWRI) {
2712 * If buffer is pinned and caller does
2713 * not want sleep waiting for it to be
2714 * unpinned, bail out
2716 if (bp->b_pin_count > 0) {
2717 if (flags & GB_LOCK_NOWAIT) {
2724 bp->b_flags |= B_NOCACHE;
2727 if (LIST_EMPTY(&bp->b_dep)) {
2728 bp->b_flags |= B_RELBUF;
2731 bp->b_flags |= B_NOCACHE;
2740 * If the size is inconsistant in the VMIO case, we can resize
2741 * the buffer. This might lead to B_CACHE getting set or
2742 * cleared. If the size has not changed, B_CACHE remains
2743 * unchanged from its previous state.
2746 if (bp->b_bcount != size)
2749 KASSERT(bp->b_offset != NOOFFSET,
2750 ("getblk: no buffer offset"));
2753 * A buffer with B_DELWRI set and B_CACHE clear must
2754 * be committed before we can return the buffer in
2755 * order to prevent the caller from issuing a read
2756 * ( due to B_CACHE not being set ) and overwriting
2759 * Most callers, including NFS and FFS, need this to
2760 * operate properly either because they assume they
2761 * can issue a read if B_CACHE is not set, or because
2762 * ( for example ) an uncached B_DELWRI might loop due
2763 * to softupdates re-dirtying the buffer. In the latter
2764 * case, B_CACHE is set after the first write completes,
2765 * preventing further loops.
2766 * NOTE! b*write() sets B_CACHE. If we cleared B_CACHE
2767 * above while extending the buffer, we cannot allow the
2768 * buffer to remain with B_CACHE set after the write
2769 * completes or it will represent a corrupt state. To
2770 * deal with this we set B_NOCACHE to scrap the buffer
2773 * We might be able to do something fancy, like setting
2774 * B_CACHE in bwrite() except if B_DELWRI is already set,
2775 * so the below call doesn't set B_CACHE, but that gets real
2776 * confusing. This is much easier.
2779 if ((bp->b_flags & (B_CACHE|B_DELWRI)) == B_DELWRI) {
2780 bp->b_flags |= B_NOCACHE;
2784 bp->b_flags &= ~B_DONE;
2786 int bsize, maxsize, vmio;
2790 * Buffer is not in-core, create new buffer. The buffer
2791 * returned by getnewbuf() is locked. Note that the returned
2792 * buffer is also considered valid (not marked B_INVAL).
2796 * If the user does not want us to create the buffer, bail out
2799 if (flags & GB_NOCREAT)
2801 bsize = vn_isdisk(vp, NULL) ? DEV_BSIZE : bo->bo_bsize;
2802 offset = blkno * bsize;
2803 vmio = vp->v_object != NULL;
2804 maxsize = vmio ? size + (offset & PAGE_MASK) : size;
2805 maxsize = imax(maxsize, bsize);
2807 bp = getnewbuf(vp, slpflag, slptimeo, size, maxsize, flags);
2809 if (slpflag || slptimeo)
2815 * This code is used to make sure that a buffer is not
2816 * created while the getnewbuf routine is blocked.
2817 * This can be a problem whether the vnode is locked or not.
2818 * If the buffer is created out from under us, we have to
2819 * throw away the one we just created.
2821 * Note: this must occur before we associate the buffer
2822 * with the vp especially considering limitations in
2823 * the splay tree implementation when dealing with duplicate
2827 if (gbincore(bo, blkno)) {
2829 bp->b_flags |= B_INVAL;
2835 * Insert the buffer into the hash, so that it can
2836 * be found by incore.
2838 bp->b_blkno = bp->b_lblkno = blkno;
2839 bp->b_offset = offset;
2844 * set B_VMIO bit. allocbuf() the buffer bigger. Since the
2845 * buffer size starts out as 0, B_CACHE will be set by
2846 * allocbuf() for the VMIO case prior to it testing the
2847 * backing store for validity.
2851 bp->b_flags |= B_VMIO;
2852 KASSERT(vp->v_object == bp->b_bufobj->bo_object,
2853 ("ARGH! different b_bufobj->bo_object %p %p %p\n",
2854 bp, vp->v_object, bp->b_bufobj->bo_object));
2856 bp->b_flags &= ~B_VMIO;
2857 KASSERT(bp->b_bufobj->bo_object == NULL,
2858 ("ARGH! has b_bufobj->bo_object %p %p\n",
2859 bp, bp->b_bufobj->bo_object));
2863 bp->b_flags &= ~B_DONE;
2865 CTR4(KTR_BUF, "getblk(%p, %ld, %d) = %p", vp, (long)blkno, size, bp);
2866 BUF_ASSERT_HELD(bp);
2867 KASSERT(bp->b_bufobj == bo,
2868 ("bp %p wrong b_bufobj %p should be %p", bp, bp->b_bufobj, bo));
2873 * Get an empty, disassociated buffer of given size. The buffer is initially
2877 geteblk(int size, int flags)
2882 maxsize = (size + BKVAMASK) & ~BKVAMASK;
2883 while ((bp = getnewbuf(NULL, 0, 0, size, maxsize, flags)) == NULL) {
2884 if ((flags & GB_NOWAIT_BD) &&
2885 (curthread->td_pflags & TDP_BUFNEED) != 0)
2889 bp->b_flags |= B_INVAL; /* b_dep cleared by getnewbuf() */
2890 BUF_ASSERT_HELD(bp);
2896 * This code constitutes the buffer memory from either anonymous system
2897 * memory (in the case of non-VMIO operations) or from an associated
2898 * VM object (in the case of VMIO operations). This code is able to
2899 * resize a buffer up or down.
2901 * Note that this code is tricky, and has many complications to resolve
2902 * deadlock or inconsistant data situations. Tread lightly!!!
2903 * There are B_CACHE and B_DELWRI interactions that must be dealt with by
2904 * the caller. Calling this code willy nilly can result in the loss of data.
2906 * allocbuf() only adjusts B_CACHE for VMIO buffers. getblk() deals with
2907 * B_CACHE for the non-VMIO case.
2911 allocbuf(struct buf *bp, int size)
2913 int newbsize, mbsize;
2916 BUF_ASSERT_HELD(bp);
2918 if (bp->b_kvasize < size)
2919 panic("allocbuf: buffer too small");
2921 if ((bp->b_flags & B_VMIO) == 0) {
2925 * Just get anonymous memory from the kernel. Don't
2926 * mess with B_CACHE.
2928 mbsize = (size + DEV_BSIZE - 1) & ~(DEV_BSIZE - 1);
2929 if (bp->b_flags & B_MALLOC)
2932 newbsize = round_page(size);
2934 if (newbsize < bp->b_bufsize) {
2936 * malloced buffers are not shrunk
2938 if (bp->b_flags & B_MALLOC) {
2940 bp->b_bcount = size;
2942 free(bp->b_data, M_BIOBUF);
2943 if (bp->b_bufsize) {
2944 atomic_subtract_long(
2950 bp->b_saveaddr = bp->b_kvabase;
2951 bp->b_data = bp->b_saveaddr;
2953 bp->b_flags &= ~B_MALLOC;
2957 vm_hold_free_pages(bp, newbsize);
2958 } else if (newbsize > bp->b_bufsize) {
2960 * We only use malloced memory on the first allocation.
2961 * and revert to page-allocated memory when the buffer
2965 * There is a potential smp race here that could lead
2966 * to bufmallocspace slightly passing the max. It
2967 * is probably extremely rare and not worth worrying
2970 if ( (bufmallocspace < maxbufmallocspace) &&
2971 (bp->b_bufsize == 0) &&
2972 (mbsize <= PAGE_SIZE/2)) {
2974 bp->b_data = malloc(mbsize, M_BIOBUF, M_WAITOK);
2975 bp->b_bufsize = mbsize;
2976 bp->b_bcount = size;
2977 bp->b_flags |= B_MALLOC;
2978 atomic_add_long(&bufmallocspace, mbsize);
2984 * If the buffer is growing on its other-than-first allocation,
2985 * then we revert to the page-allocation scheme.
2987 if (bp->b_flags & B_MALLOC) {
2988 origbuf = bp->b_data;
2989 origbufsize = bp->b_bufsize;
2990 bp->b_data = bp->b_kvabase;
2991 if (bp->b_bufsize) {
2992 atomic_subtract_long(&bufmallocspace,
2997 bp->b_flags &= ~B_MALLOC;
2998 newbsize = round_page(newbsize);
3002 (vm_offset_t) bp->b_data + bp->b_bufsize,
3003 (vm_offset_t) bp->b_data + newbsize);
3005 bcopy(origbuf, bp->b_data, origbufsize);
3006 free(origbuf, M_BIOBUF);
3012 newbsize = (size + DEV_BSIZE - 1) & ~(DEV_BSIZE - 1);
3013 desiredpages = (size == 0) ? 0 :
3014 num_pages((bp->b_offset & PAGE_MASK) + newbsize);
3016 if (bp->b_flags & B_MALLOC)
3017 panic("allocbuf: VMIO buffer can't be malloced");
3019 * Set B_CACHE initially if buffer is 0 length or will become
3022 if (size == 0 || bp->b_bufsize == 0)
3023 bp->b_flags |= B_CACHE;
3025 if (newbsize < bp->b_bufsize) {
3027 * DEV_BSIZE aligned new buffer size is less then the
3028 * DEV_BSIZE aligned existing buffer size. Figure out
3029 * if we have to remove any pages.
3031 if (desiredpages < bp->b_npages) {
3034 pmap_qremove((vm_offset_t)trunc_page(
3035 (vm_offset_t)bp->b_data) +
3036 (desiredpages << PAGE_SHIFT),
3037 (bp->b_npages - desiredpages));
3038 VM_OBJECT_LOCK(bp->b_bufobj->bo_object);
3039 for (i = desiredpages; i < bp->b_npages; i++) {
3041 * the page is not freed here -- it
3042 * is the responsibility of
3043 * vnode_pager_setsize
3046 KASSERT(m != bogus_page,
3047 ("allocbuf: bogus page found"));
3048 while (vm_page_sleep_if_busy(m, TRUE,
3052 bp->b_pages[i] = NULL;
3054 vm_page_unwire(m, 0);
3057 VM_OBJECT_UNLOCK(bp->b_bufobj->bo_object);
3058 bp->b_npages = desiredpages;
3060 } else if (size > bp->b_bcount) {
3062 * We are growing the buffer, possibly in a
3063 * byte-granular fashion.
3070 * Step 1, bring in the VM pages from the object,
3071 * allocating them if necessary. We must clear
3072 * B_CACHE if these pages are not valid for the
3073 * range covered by the buffer.
3076 obj = bp->b_bufobj->bo_object;
3078 VM_OBJECT_LOCK(obj);
3079 while (bp->b_npages < desiredpages) {
3083 * We must allocate system pages since blocking
3084 * here could interfere with paging I/O, no
3085 * matter which process we are.
3087 * We can only test VPO_BUSY here. Blocking on
3088 * m->busy might lead to a deadlock:
3089 * vm_fault->getpages->cluster_read->allocbuf
3090 * Thus, we specify VM_ALLOC_IGN_SBUSY.
3092 m = vm_page_grab(obj, OFF_TO_IDX(bp->b_offset) +
3093 bp->b_npages, VM_ALLOC_NOBUSY |
3094 VM_ALLOC_SYSTEM | VM_ALLOC_WIRED |
3095 VM_ALLOC_RETRY | VM_ALLOC_IGN_SBUSY |
3096 VM_ALLOC_COUNT(desiredpages - bp->b_npages));
3098 bp->b_flags &= ~B_CACHE;
3099 bp->b_pages[bp->b_npages] = m;
3104 * Step 2. We've loaded the pages into the buffer,
3105 * we have to figure out if we can still have B_CACHE
3106 * set. Note that B_CACHE is set according to the
3107 * byte-granular range ( bcount and size ), new the
3108 * aligned range ( newbsize ).
3110 * The VM test is against m->valid, which is DEV_BSIZE
3111 * aligned. Needless to say, the validity of the data
3112 * needs to also be DEV_BSIZE aligned. Note that this
3113 * fails with NFS if the server or some other client
3114 * extends the file's EOF. If our buffer is resized,
3115 * B_CACHE may remain set! XXX
3118 toff = bp->b_bcount;
3119 tinc = PAGE_SIZE - ((bp->b_offset + toff) & PAGE_MASK);
3121 while ((bp->b_flags & B_CACHE) && toff < size) {
3124 if (tinc > (size - toff))
3127 pi = ((bp->b_offset & PAGE_MASK) + toff) >>
3140 VM_OBJECT_UNLOCK(obj);
3143 * Step 3, fixup the KVM pmap. Remember that
3144 * bp->b_data is relative to bp->b_offset, but
3145 * bp->b_offset may be offset into the first page.
3148 bp->b_data = (caddr_t)
3149 trunc_page((vm_offset_t)bp->b_data);
3151 (vm_offset_t)bp->b_data,
3156 bp->b_data = (caddr_t)((vm_offset_t)bp->b_data |
3157 (vm_offset_t)(bp->b_offset & PAGE_MASK));
3160 if (newbsize < bp->b_bufsize)
3162 bp->b_bufsize = newbsize; /* actual buffer allocation */
3163 bp->b_bcount = size; /* requested buffer size */
3168 biodone(struct bio *bp)
3171 void (*done)(struct bio *);
3173 mtxp = mtx_pool_find(mtxpool_sleep, bp);
3175 bp->bio_flags |= BIO_DONE;
3176 done = bp->bio_done;
3185 * Wait for a BIO to finish.
3187 * XXX: resort to a timeout for now. The optimal locking (if any) for this
3188 * case is not yet clear.
3191 biowait(struct bio *bp, const char *wchan)
3195 mtxp = mtx_pool_find(mtxpool_sleep, bp);
3197 while ((bp->bio_flags & BIO_DONE) == 0)
3198 msleep(bp, mtxp, PRIBIO, wchan, hz / 10);
3200 if (bp->bio_error != 0)
3201 return (bp->bio_error);
3202 if (!(bp->bio_flags & BIO_ERROR))
3208 biofinish(struct bio *bp, struct devstat *stat, int error)
3212 bp->bio_error = error;
3213 bp->bio_flags |= BIO_ERROR;
3216 devstat_end_transaction_bio(stat, bp);
3223 * Wait for buffer I/O completion, returning error status. The buffer
3224 * is left locked and B_DONE on return. B_EINTR is converted into an EINTR
3225 * error and cleared.
3228 bufwait(struct buf *bp)
3230 if (bp->b_iocmd == BIO_READ)
3231 bwait(bp, PRIBIO, "biord");
3233 bwait(bp, PRIBIO, "biowr");
3234 if (bp->b_flags & B_EINTR) {
3235 bp->b_flags &= ~B_EINTR;
3238 if (bp->b_ioflags & BIO_ERROR) {
3239 return (bp->b_error ? bp->b_error : EIO);
3246 * Call back function from struct bio back up to struct buf.
3249 bufdonebio(struct bio *bip)
3253 bp = bip->bio_caller2;
3254 bp->b_resid = bp->b_bcount - bip->bio_completed;
3255 bp->b_resid = bip->bio_resid; /* XXX: remove */
3256 bp->b_ioflags = bip->bio_flags;
3257 bp->b_error = bip->bio_error;
3259 bp->b_ioflags |= BIO_ERROR;
3265 dev_strategy(struct cdev *dev, struct buf *bp)
3271 if ((!bp->b_iocmd) || (bp->b_iocmd & (bp->b_iocmd - 1)))
3272 panic("b_iocmd botch");
3277 /* Try again later */
3278 tsleep(&bp, PRIBIO, "dev_strat", hz/10);
3280 bip->bio_cmd = bp->b_iocmd;
3281 bip->bio_offset = bp->b_iooffset;
3282 bip->bio_length = bp->b_bcount;
3283 bip->bio_bcount = bp->b_bcount; /* XXX: remove */
3284 bip->bio_data = bp->b_data;
3285 bip->bio_done = bufdonebio;
3286 bip->bio_caller2 = bp;
3288 KASSERT(dev->si_refcount > 0,
3289 ("dev_strategy on un-referenced struct cdev *(%s)",
3291 csw = dev_refthread(dev, &ref);
3294 bp->b_error = ENXIO;
3295 bp->b_ioflags = BIO_ERROR;
3299 (*csw->d_strategy)(bip);
3300 dev_relthread(dev, ref);
3306 * Finish I/O on a buffer, optionally calling a completion function.
3307 * This is usually called from an interrupt so process blocking is
3310 * biodone is also responsible for setting B_CACHE in a B_VMIO bp.
3311 * In a non-VMIO bp, B_CACHE will be set on the next getblk()
3312 * assuming B_INVAL is clear.
3314 * For the VMIO case, we set B_CACHE if the op was a read and no
3315 * read error occured, or if the op was a write. B_CACHE is never
3316 * set if the buffer is invalid or otherwise uncacheable.
3318 * biodone does not mess with B_INVAL, allowing the I/O routine or the
3319 * initiator to leave B_INVAL set to brelse the buffer out of existance
3320 * in the biodone routine.
3323 bufdone(struct buf *bp)
3325 struct bufobj *dropobj;
3326 void (*biodone)(struct buf *);
3328 CTR3(KTR_BUF, "bufdone(%p) vp %p flags %X", bp, bp->b_vp, bp->b_flags);
3331 KASSERT(!(bp->b_flags & B_DONE), ("biodone: bp %p already done", bp));
3332 BUF_ASSERT_HELD(bp);
3334 runningbufwakeup(bp);
3335 if (bp->b_iocmd == BIO_WRITE)
3336 dropobj = bp->b_bufobj;
3337 /* call optional completion function if requested */
3338 if (bp->b_iodone != NULL) {
3339 biodone = bp->b_iodone;
3340 bp->b_iodone = NULL;
3343 bufobj_wdrop(dropobj);
3350 bufobj_wdrop(dropobj);
3354 bufdone_finish(struct buf *bp)
3356 BUF_ASSERT_HELD(bp);
3358 if (!LIST_EMPTY(&bp->b_dep))
3361 if (bp->b_flags & B_VMIO) {
3366 int bogus, i, iosize;
3368 obj = bp->b_bufobj->bo_object;
3369 KASSERT(obj->paging_in_progress >= bp->b_npages,
3370 ("biodone_finish: paging in progress(%d) < b_npages(%d)",
3371 obj->paging_in_progress, bp->b_npages));
3374 KASSERT(vp->v_holdcnt > 0,
3375 ("biodone_finish: vnode %p has zero hold count", vp));
3376 KASSERT(vp->v_object != NULL,
3377 ("biodone_finish: vnode %p has no vm_object", vp));
3379 foff = bp->b_offset;
3380 KASSERT(bp->b_offset != NOOFFSET,
3381 ("biodone_finish: bp %p has no buffer offset", bp));
3384 * Set B_CACHE if the op was a normal read and no error
3385 * occured. B_CACHE is set for writes in the b*write()
3388 iosize = bp->b_bcount - bp->b_resid;
3389 if (bp->b_iocmd == BIO_READ &&
3390 !(bp->b_flags & (B_INVAL|B_NOCACHE)) &&
3391 !(bp->b_ioflags & BIO_ERROR)) {
3392 bp->b_flags |= B_CACHE;
3395 VM_OBJECT_LOCK(obj);
3396 for (i = 0; i < bp->b_npages; i++) {
3400 resid = ((foff + PAGE_SIZE) & ~(off_t)PAGE_MASK) - foff;
3405 * cleanup bogus pages, restoring the originals
3408 if (m == bogus_page) {
3409 bogus = bogusflag = 1;
3410 m = vm_page_lookup(obj, OFF_TO_IDX(foff));
3412 panic("biodone: page disappeared!");
3415 KASSERT(OFF_TO_IDX(foff) == m->pindex,
3416 ("biodone_finish: foff(%jd)/pindex(%ju) mismatch",
3417 (intmax_t)foff, (uintmax_t)m->pindex));
3420 * In the write case, the valid and clean bits are
3421 * already changed correctly ( see bdwrite() ), so we
3422 * only need to do this here in the read case.
3424 if ((bp->b_iocmd == BIO_READ) && !bogusflag && resid > 0) {
3425 KASSERT((m->dirty & vm_page_bits(foff &
3426 PAGE_MASK, resid)) == 0, ("bufdone_finish:"
3427 " page %p has unexpected dirty bits", m));
3428 vfs_page_set_valid(bp, foff, m);
3431 vm_page_io_finish(m);
3432 vm_object_pip_subtract(obj, 1);
3433 foff = (foff + PAGE_SIZE) & ~(off_t)PAGE_MASK;
3436 vm_object_pip_wakeupn(obj, 0);
3437 VM_OBJECT_UNLOCK(obj);
3439 pmap_qenter(trunc_page((vm_offset_t)bp->b_data),
3440 bp->b_pages, bp->b_npages);
3444 * For asynchronous completions, release the buffer now. The brelse
3445 * will do a wakeup there if necessary - so no need to do a wakeup
3446 * here in the async case. The sync case always needs to do a wakeup.
3449 if (bp->b_flags & B_ASYNC) {
3450 if ((bp->b_flags & (B_NOCACHE | B_INVAL | B_RELBUF)) || (bp->b_ioflags & BIO_ERROR))
3459 * This routine is called in lieu of iodone in the case of
3460 * incomplete I/O. This keeps the busy status for pages
3464 vfs_unbusy_pages(struct buf *bp)
3470 runningbufwakeup(bp);
3471 if (!(bp->b_flags & B_VMIO))
3474 obj = bp->b_bufobj->bo_object;
3475 VM_OBJECT_LOCK(obj);
3476 for (i = 0; i < bp->b_npages; i++) {
3478 if (m == bogus_page) {
3479 m = vm_page_lookup(obj, OFF_TO_IDX(bp->b_offset) + i);
3481 panic("vfs_unbusy_pages: page missing\n");
3483 pmap_qenter(trunc_page((vm_offset_t)bp->b_data),
3484 bp->b_pages, bp->b_npages);
3486 vm_object_pip_subtract(obj, 1);
3487 vm_page_io_finish(m);
3489 vm_object_pip_wakeupn(obj, 0);
3490 VM_OBJECT_UNLOCK(obj);
3494 * vfs_page_set_valid:
3496 * Set the valid bits in a page based on the supplied offset. The
3497 * range is restricted to the buffer's size.
3499 * This routine is typically called after a read completes.
3502 vfs_page_set_valid(struct buf *bp, vm_ooffset_t off, vm_page_t m)
3507 * Compute the end offset, eoff, such that [off, eoff) does not span a
3508 * page boundary and eoff is not greater than the end of the buffer.
3509 * The end of the buffer, in this case, is our file EOF, not the
3510 * allocation size of the buffer.
3512 eoff = (off + PAGE_SIZE) & ~(vm_ooffset_t)PAGE_MASK;
3513 if (eoff > bp->b_offset + bp->b_bcount)
3514 eoff = bp->b_offset + bp->b_bcount;
3517 * Set valid range. This is typically the entire buffer and thus the
3521 vm_page_set_valid(m, off & PAGE_MASK, eoff - off);
3525 * vfs_page_set_validclean:
3527 * Set the valid bits and clear the dirty bits in a page based on the
3528 * supplied offset. The range is restricted to the buffer's size.
3531 vfs_page_set_validclean(struct buf *bp, vm_ooffset_t off, vm_page_t m)
3533 vm_ooffset_t soff, eoff;
3536 * Start and end offsets in buffer. eoff - soff may not cross a
3537 * page boundry or cross the end of the buffer. The end of the
3538 * buffer, in this case, is our file EOF, not the allocation size
3542 eoff = (off + PAGE_SIZE) & ~(off_t)PAGE_MASK;
3543 if (eoff > bp->b_offset + bp->b_bcount)
3544 eoff = bp->b_offset + bp->b_bcount;
3547 * Set valid range. This is typically the entire buffer and thus the
3551 vm_page_set_validclean(
3553 (vm_offset_t) (soff & PAGE_MASK),
3554 (vm_offset_t) (eoff - soff)
3560 * Ensure that all buffer pages are not busied by VPO_BUSY flag. If
3561 * any page is busy, drain the flag.
3564 vfs_drain_busy_pages(struct buf *bp)
3569 VM_OBJECT_LOCK_ASSERT(bp->b_bufobj->bo_object, MA_OWNED);
3571 for (i = 0; i < bp->b_npages; i++) {
3573 if ((m->oflags & VPO_BUSY) != 0) {
3574 for (; last_busied < i; last_busied++)
3575 vm_page_busy(bp->b_pages[last_busied]);
3576 while ((m->oflags & VPO_BUSY) != 0)
3577 vm_page_sleep(m, "vbpage");
3580 for (i = 0; i < last_busied; i++)
3581 vm_page_wakeup(bp->b_pages[i]);
3585 * This routine is called before a device strategy routine.
3586 * It is used to tell the VM system that paging I/O is in
3587 * progress, and treat the pages associated with the buffer
3588 * almost as being VPO_BUSY. Also the object paging_in_progress
3589 * flag is handled to make sure that the object doesn't become
3592 * Since I/O has not been initiated yet, certain buffer flags
3593 * such as BIO_ERROR or B_INVAL may be in an inconsistant state
3594 * and should be ignored.
3597 vfs_busy_pages(struct buf *bp, int clear_modify)
3604 if (!(bp->b_flags & B_VMIO))
3607 obj = bp->b_bufobj->bo_object;
3608 foff = bp->b_offset;
3609 KASSERT(bp->b_offset != NOOFFSET,
3610 ("vfs_busy_pages: no buffer offset"));
3611 VM_OBJECT_LOCK(obj);
3612 vfs_drain_busy_pages(bp);
3613 if (bp->b_bufsize != 0)
3614 vfs_setdirty_locked_object(bp);
3616 for (i = 0; i < bp->b_npages; i++) {
3619 if ((bp->b_flags & B_CLUSTER) == 0) {
3620 vm_object_pip_add(obj, 1);
3621 vm_page_io_start(m);
3624 * When readying a buffer for a read ( i.e
3625 * clear_modify == 0 ), it is important to do
3626 * bogus_page replacement for valid pages in
3627 * partially instantiated buffers. Partially
3628 * instantiated buffers can, in turn, occur when
3629 * reconstituting a buffer from its VM backing store
3630 * base. We only have to do this if B_CACHE is
3631 * clear ( which causes the I/O to occur in the
3632 * first place ). The replacement prevents the read
3633 * I/O from overwriting potentially dirty VM-backed
3634 * pages. XXX bogus page replacement is, uh, bogus.
3635 * It may not work properly with small-block devices.
3636 * We need to find a better way.
3639 pmap_remove_write(m);
3640 vfs_page_set_validclean(bp, foff, m);
3641 } else if (m->valid == VM_PAGE_BITS_ALL &&
3642 (bp->b_flags & B_CACHE) == 0) {
3643 bp->b_pages[i] = bogus_page;
3646 foff = (foff + PAGE_SIZE) & ~(off_t)PAGE_MASK;
3648 VM_OBJECT_UNLOCK(obj);
3650 pmap_qenter(trunc_page((vm_offset_t)bp->b_data),
3651 bp->b_pages, bp->b_npages);
3655 * vfs_bio_set_valid:
3657 * Set the range within the buffer to valid. The range is
3658 * relative to the beginning of the buffer, b_offset. Note that
3659 * b_offset itself may be offset from the beginning of the first
3663 vfs_bio_set_valid(struct buf *bp, int base, int size)
3668 if (!(bp->b_flags & B_VMIO))
3672 * Fixup base to be relative to beginning of first page.
3673 * Set initial n to be the maximum number of bytes in the
3674 * first page that can be validated.
3676 base += (bp->b_offset & PAGE_MASK);
3677 n = PAGE_SIZE - (base & PAGE_MASK);
3679 VM_OBJECT_LOCK(bp->b_bufobj->bo_object);
3680 for (i = base / PAGE_SIZE; size > 0 && i < bp->b_npages; ++i) {
3684 vm_page_set_valid(m, base & PAGE_MASK, n);
3689 VM_OBJECT_UNLOCK(bp->b_bufobj->bo_object);
3695 * If the specified buffer is a non-VMIO buffer, clear the entire
3696 * buffer. If the specified buffer is a VMIO buffer, clear and
3697 * validate only the previously invalid portions of the buffer.
3698 * This routine essentially fakes an I/O, so we need to clear
3699 * BIO_ERROR and B_INVAL.
3701 * Note that while we only theoretically need to clear through b_bcount,
3702 * we go ahead and clear through b_bufsize.
3705 vfs_bio_clrbuf(struct buf *bp)
3710 if ((bp->b_flags & (B_VMIO | B_MALLOC)) != B_VMIO) {
3714 bp->b_flags &= ~B_INVAL;
3715 bp->b_ioflags &= ~BIO_ERROR;
3716 VM_OBJECT_LOCK(bp->b_bufobj->bo_object);
3717 if ((bp->b_npages == 1) && (bp->b_bufsize < PAGE_SIZE) &&
3718 (bp->b_offset & PAGE_MASK) == 0) {
3719 if (bp->b_pages[0] == bogus_page)
3721 mask = (1 << (bp->b_bufsize / DEV_BSIZE)) - 1;
3722 VM_OBJECT_LOCK_ASSERT(bp->b_pages[0]->object, MA_OWNED);
3723 if ((bp->b_pages[0]->valid & mask) == mask)
3725 if ((bp->b_pages[0]->valid & mask) == 0) {
3726 bzero(bp->b_data, bp->b_bufsize);
3727 bp->b_pages[0]->valid |= mask;
3731 ea = sa = bp->b_data;
3732 for(i = 0; i < bp->b_npages; i++, sa = ea) {
3733 ea = (caddr_t)trunc_page((vm_offset_t)sa + PAGE_SIZE);
3734 ea = (caddr_t)(vm_offset_t)ulmin(
3735 (u_long)(vm_offset_t)ea,
3736 (u_long)(vm_offset_t)bp->b_data + bp->b_bufsize);
3737 if (bp->b_pages[i] == bogus_page)
3739 j = ((vm_offset_t)sa & PAGE_MASK) / DEV_BSIZE;
3740 mask = ((1 << ((ea - sa) / DEV_BSIZE)) - 1) << j;
3741 VM_OBJECT_LOCK_ASSERT(bp->b_pages[i]->object, MA_OWNED);
3742 if ((bp->b_pages[i]->valid & mask) == mask)
3744 if ((bp->b_pages[i]->valid & mask) == 0)
3747 for (; sa < ea; sa += DEV_BSIZE, j++) {
3748 if ((bp->b_pages[i]->valid & (1 << j)) == 0)
3749 bzero(sa, DEV_BSIZE);
3752 bp->b_pages[i]->valid |= mask;
3755 VM_OBJECT_UNLOCK(bp->b_bufobj->bo_object);
3760 * vm_hold_load_pages and vm_hold_free_pages get pages into
3761 * a buffers address space. The pages are anonymous and are
3762 * not associated with a file object.
3765 vm_hold_load_pages(struct buf *bp, vm_offset_t from, vm_offset_t to)
3771 to = round_page(to);
3772 from = round_page(from);
3773 index = (from - trunc_page((vm_offset_t)bp->b_data)) >> PAGE_SHIFT;
3775 for (pg = from; pg < to; pg += PAGE_SIZE, index++) {
3778 * note: must allocate system pages since blocking here
3779 * could interfere with paging I/O, no matter which
3782 p = vm_page_alloc(NULL, 0, VM_ALLOC_SYSTEM | VM_ALLOC_NOOBJ |
3783 VM_ALLOC_WIRED | VM_ALLOC_COUNT((to - pg) >> PAGE_SHIFT));
3788 pmap_qenter(pg, &p, 1);
3789 bp->b_pages[index] = p;
3791 bp->b_npages = index;
3794 /* Return pages associated with this buf to the vm system */
3796 vm_hold_free_pages(struct buf *bp, int newbsize)
3800 int index, newnpages;
3802 from = round_page((vm_offset_t)bp->b_data + newbsize);
3803 newnpages = (from - trunc_page((vm_offset_t)bp->b_data)) >> PAGE_SHIFT;
3804 if (bp->b_npages > newnpages)
3805 pmap_qremove(from, bp->b_npages - newnpages);
3806 for (index = newnpages; index < bp->b_npages; index++) {
3807 p = bp->b_pages[index];
3808 bp->b_pages[index] = NULL;
3810 printf("vm_hold_free_pages: blkno: %jd, lblkno: %jd\n",
3811 (intmax_t)bp->b_blkno, (intmax_t)bp->b_lblkno);
3814 atomic_subtract_int(&cnt.v_wire_count, 1);
3816 bp->b_npages = newnpages;
3820 * Map an IO request into kernel virtual address space.
3822 * All requests are (re)mapped into kernel VA space.
3823 * Notice that we use b_bufsize for the size of the buffer
3824 * to be mapped. b_bcount might be modified by the driver.
3826 * Note that even if the caller determines that the address space should
3827 * be valid, a race or a smaller-file mapped into a larger space may
3828 * actually cause vmapbuf() to fail, so all callers of vmapbuf() MUST
3829 * check the return value.
3832 vmapbuf(struct buf *bp)
3838 if (bp->b_bufsize < 0)
3840 prot = VM_PROT_READ;
3841 if (bp->b_iocmd == BIO_READ)
3842 prot |= VM_PROT_WRITE; /* Less backwards than it looks */
3843 if ((pidx = vm_fault_quick_hold_pages(&curproc->p_vmspace->vm_map,
3844 (vm_offset_t)bp->b_data, bp->b_bufsize, prot, bp->b_pages,
3845 btoc(MAXPHYS))) < 0)
3847 pmap_qenter((vm_offset_t)bp->b_saveaddr, bp->b_pages, pidx);
3849 kva = bp->b_saveaddr;
3850 bp->b_npages = pidx;
3851 bp->b_saveaddr = bp->b_data;
3852 bp->b_data = kva + (((vm_offset_t) bp->b_data) & PAGE_MASK);
3857 * Free the io map PTEs associated with this IO operation.
3858 * We also invalidate the TLB entries and restore the original b_addr.
3861 vunmapbuf(struct buf *bp)
3865 npages = bp->b_npages;
3866 pmap_qremove(trunc_page((vm_offset_t)bp->b_data), npages);
3867 vm_page_unhold_pages(bp->b_pages, npages);
3869 bp->b_data = bp->b_saveaddr;
3873 bdone(struct buf *bp)
3877 mtxp = mtx_pool_find(mtxpool_sleep, bp);
3879 bp->b_flags |= B_DONE;
3885 bwait(struct buf *bp, u_char pri, const char *wchan)
3889 mtxp = mtx_pool_find(mtxpool_sleep, bp);
3891 while ((bp->b_flags & B_DONE) == 0)
3892 msleep(bp, mtxp, pri, wchan, 0);
3897 bufsync(struct bufobj *bo, int waitfor)
3900 return (VOP_FSYNC(bo->__bo_vnode, waitfor, curthread));
3904 bufstrategy(struct bufobj *bo, struct buf *bp)
3910 KASSERT(vp == bo->bo_private, ("Inconsistent vnode bufstrategy"));
3911 KASSERT(vp->v_type != VCHR && vp->v_type != VBLK,
3912 ("Wrong vnode in bufstrategy(bp=%p, vp=%p)", bp, vp));
3913 i = VOP_STRATEGY(vp, bp);
3914 KASSERT(i == 0, ("VOP_STRATEGY failed bp=%p vp=%p", bp, bp->b_vp));
3918 bufobj_wrefl(struct bufobj *bo)
3921 KASSERT(bo != NULL, ("NULL bo in bufobj_wref"));
3922 ASSERT_BO_LOCKED(bo);
3927 bufobj_wref(struct bufobj *bo)
3930 KASSERT(bo != NULL, ("NULL bo in bufobj_wref"));
3937 bufobj_wdrop(struct bufobj *bo)
3940 KASSERT(bo != NULL, ("NULL bo in bufobj_wdrop"));
3942 KASSERT(bo->bo_numoutput > 0, ("bufobj_wdrop non-positive count"));
3943 if ((--bo->bo_numoutput == 0) && (bo->bo_flag & BO_WWAIT)) {
3944 bo->bo_flag &= ~BO_WWAIT;
3945 wakeup(&bo->bo_numoutput);
3951 bufobj_wwait(struct bufobj *bo, int slpflag, int timeo)
3955 KASSERT(bo != NULL, ("NULL bo in bufobj_wwait"));
3956 ASSERT_BO_LOCKED(bo);
3958 while (bo->bo_numoutput) {
3959 bo->bo_flag |= BO_WWAIT;
3960 error = msleep(&bo->bo_numoutput, BO_MTX(bo),
3961 slpflag | (PRIBIO + 1), "bo_wwait", timeo);
3969 bpin(struct buf *bp)
3973 mtxp = mtx_pool_find(mtxpool_sleep, bp);
3980 bunpin(struct buf *bp)
3984 mtxp = mtx_pool_find(mtxpool_sleep, bp);
3986 if (--bp->b_pin_count == 0)
3992 bunpin_wait(struct buf *bp)
3996 mtxp = mtx_pool_find(mtxpool_sleep, bp);
3998 while (bp->b_pin_count > 0)
3999 msleep(bp, mtxp, PRIBIO, "bwunpin", 0);
4003 #include "opt_ddb.h"
4005 #include <ddb/ddb.h>
4007 /* DDB command to show buffer data */
4008 DB_SHOW_COMMAND(buffer, db_show_buffer)
4011 struct buf *bp = (struct buf *)addr;
4014 db_printf("usage: show buffer <addr>\n");
4018 db_printf("buf at %p\n", bp);
4019 db_printf("b_flags = 0x%b, b_xflags=0x%b, b_vflags=0x%b\n",
4020 (u_int)bp->b_flags, PRINT_BUF_FLAGS, (u_int)bp->b_xflags,
4021 PRINT_BUF_XFLAGS, (u_int)bp->b_vflags, PRINT_BUF_VFLAGS);
4023 "b_error = %d, b_bufsize = %ld, b_bcount = %ld, b_resid = %ld\n"
4024 "b_bufobj = (%p), b_data = %p, b_blkno = %jd, b_lblkno = %jd, "
4026 bp->b_error, bp->b_bufsize, bp->b_bcount, bp->b_resid,
4027 bp->b_bufobj, bp->b_data, (intmax_t)bp->b_blkno,
4028 (intmax_t)bp->b_lblkno, bp->b_dep.lh_first);
4031 db_printf("b_npages = %d, pages(OBJ, IDX, PA): ", bp->b_npages);
4032 for (i = 0; i < bp->b_npages; i++) {
4035 db_printf("(%p, 0x%lx, 0x%lx)", (void *)m->object,
4036 (u_long)m->pindex, (u_long)VM_PAGE_TO_PHYS(m));
4037 if ((i + 1) < bp->b_npages)
4043 BUF_LOCKPRINTINFO(bp);
4046 DB_SHOW_COMMAND(lockedbufs, lockedbufs)
4051 for (i = 0; i < nbuf; i++) {
4053 if (BUF_ISLOCKED(bp)) {
4054 db_show_buffer((uintptr_t)bp, 1, 0, NULL);
4060 DB_SHOW_COMMAND(vnodebufs, db_show_vnodebufs)
4066 db_printf("usage: show vnodebufs <addr>\n");
4069 vp = (struct vnode *)addr;
4070 db_printf("Clean buffers:\n");
4071 TAILQ_FOREACH(bp, &vp->v_bufobj.bo_clean.bv_hd, b_bobufs) {
4072 db_show_buffer((uintptr_t)bp, 1, 0, NULL);
4075 db_printf("Dirty buffers:\n");
4076 TAILQ_FOREACH(bp, &vp->v_bufobj.bo_dirty.bv_hd, b_bobufs) {
4077 db_show_buffer((uintptr_t)bp, 1, 0, NULL);
4082 DB_COMMAND(countfreebufs, db_coundfreebufs)
4085 int i, used = 0, nfree = 0;
4088 db_printf("usage: countfreebufs\n");
4092 for (i = 0; i < nbuf; i++) {
4094 if ((bp->b_vflags & BV_INFREECNT) != 0)
4100 db_printf("Counted %d free, %d used (%d tot)\n", nfree, used,
4102 db_printf("numfreebuffers is %d\n", numfreebuffers);