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");
209 static long barrierwrites;
210 SYSCTL_LONG(_vfs, OID_AUTO, barrierwrites, CTLFLAG_RW, &barrierwrites, 0,
211 "Number of barrier writes");
214 * Wakeup point for bufdaemon, as well as indicator of whether it is already
215 * active. Set to 1 when the bufdaemon is already "on" the queue, 0 when it
218 static int bd_request;
221 * Request for the buf daemon to write more buffers than is indicated by
222 * lodirtybuf. This may be necessary to push out excess dependencies or
223 * defragment the address space where a simple count of the number of dirty
224 * buffers is insufficient to characterize the demand for flushing them.
226 static int bd_speedupreq;
229 * This lock synchronizes access to bd_request.
231 static struct mtx bdlock;
234 * bogus page -- for I/O to/from partially complete buffers
235 * this is a temporary solution to the problem, but it is not
236 * really that bad. it would be better to split the buffer
237 * for input in the case of buffers partially already in memory,
238 * but the code is intricate enough already.
240 vm_page_t bogus_page;
243 * Synchronization (sleep/wakeup) variable for active buffer space requests.
244 * Set when wait starts, cleared prior to wakeup().
245 * Used in runningbufwakeup() and waitrunningbufspace().
247 static int runningbufreq;
250 * This lock protects the runningbufreq and synchronizes runningbufwakeup and
251 * waitrunningbufspace().
253 static struct mtx rbreqlock;
256 * Synchronization (sleep/wakeup) variable for buffer requests.
257 * Can contain the VFS_BIO_NEED flags defined below; setting/clearing is done
259 * Used in numdirtywakeup(), bufspacewakeup(), bufcountwakeup(), bwillwrite(),
260 * getnewbuf(), and getblk().
262 static int needsbuffer;
265 * Lock that protects needsbuffer and the sleeps/wakeups surrounding it.
267 static struct mtx nblock;
270 * Definitions for the buffer free lists.
272 #define BUFFER_QUEUES 5 /* number of free buffer queues */
274 #define QUEUE_NONE 0 /* on no queue */
275 #define QUEUE_CLEAN 1 /* non-B_DELWRI buffers */
276 #define QUEUE_DIRTY 2 /* B_DELWRI buffers */
277 #define QUEUE_EMPTYKVA 3 /* empty buffer headers w/KVA assignment */
278 #define QUEUE_EMPTY 4 /* empty buffer headers */
279 #define QUEUE_SENTINEL 1024 /* not an queue index, but mark for sentinel */
281 /* Queues for free buffers with various properties */
282 static TAILQ_HEAD(bqueues, buf) bufqueues[BUFFER_QUEUES] = { { 0 } };
284 /* Lock for the bufqueues */
285 static struct mtx bqlock;
288 * Single global constant for BUF_WMESG, to avoid getting multiple references.
289 * buf_wmesg is referred from macros.
291 const char *buf_wmesg = BUF_WMESG;
293 #define VFS_BIO_NEED_ANY 0x01 /* any freeable buffer */
294 #define VFS_BIO_NEED_DIRTYFLUSH 0x02 /* waiting for dirty buffer flush */
295 #define VFS_BIO_NEED_FREE 0x04 /* wait for free bufs, hi hysteresis */
296 #define VFS_BIO_NEED_BUFSPACE 0x08 /* wait for buf space, lo hysteresis */
298 #if defined(COMPAT_FREEBSD4) || defined(COMPAT_FREEBSD5) || \
299 defined(COMPAT_FREEBSD6) || defined(COMPAT_FREEBSD7)
301 sysctl_bufspace(SYSCTL_HANDLER_ARGS)
306 if (sizeof(int) == sizeof(long) || req->oldlen >= sizeof(long))
307 return (sysctl_handle_long(oidp, arg1, arg2, req));
308 lvalue = *(long *)arg1;
309 if (lvalue > INT_MAX)
310 /* On overflow, still write out a long to trigger ENOMEM. */
311 return (sysctl_handle_long(oidp, &lvalue, 0, req));
313 return (sysctl_handle_int(oidp, &ivalue, 0, req));
318 extern void ffs_rawread_setup(void);
319 #endif /* DIRECTIO */
323 * If someone is blocked due to there being too many dirty buffers,
324 * and numdirtybuffers is now reasonable, wake them up.
328 numdirtywakeup(int level)
331 if (numdirtybuffers <= level) {
333 if (needsbuffer & VFS_BIO_NEED_DIRTYFLUSH) {
334 needsbuffer &= ~VFS_BIO_NEED_DIRTYFLUSH;
335 wakeup(&needsbuffer);
344 * Called when buffer space is potentially available for recovery.
345 * getnewbuf() will block on this flag when it is unable to free
346 * sufficient buffer space. Buffer space becomes recoverable when
347 * bp's get placed back in the queues.
355 * If someone is waiting for BUF space, wake them up. Even
356 * though we haven't freed the kva space yet, the waiting
357 * process will be able to now.
360 if (needsbuffer & VFS_BIO_NEED_BUFSPACE) {
361 needsbuffer &= ~VFS_BIO_NEED_BUFSPACE;
362 wakeup(&needsbuffer);
368 * runningbufwakeup() - in-progress I/O accounting.
372 runningbufwakeup(struct buf *bp)
375 if (bp->b_runningbufspace) {
376 atomic_subtract_long(&runningbufspace, bp->b_runningbufspace);
377 bp->b_runningbufspace = 0;
378 mtx_lock(&rbreqlock);
379 if (runningbufreq && runningbufspace <= lorunningspace) {
381 wakeup(&runningbufreq);
383 mtx_unlock(&rbreqlock);
390 * Called when a buffer has been added to one of the free queues to
391 * account for the buffer and to wakeup anyone waiting for free buffers.
392 * This typically occurs when large amounts of metadata are being handled
393 * by the buffer cache ( else buffer space runs out first, usually ).
397 bufcountwakeup(struct buf *bp)
401 KASSERT((bp->b_vflags & BV_INFREECNT) == 0,
402 ("buf %p already counted as free", bp));
403 if (bp->b_bufobj != NULL)
404 mtx_assert(BO_MTX(bp->b_bufobj), MA_OWNED);
405 bp->b_vflags |= BV_INFREECNT;
406 old = atomic_fetchadd_int(&numfreebuffers, 1);
407 KASSERT(old >= 0 && old < nbuf,
408 ("numfreebuffers climbed to %d", old + 1));
411 needsbuffer &= ~VFS_BIO_NEED_ANY;
412 if (numfreebuffers >= hifreebuffers)
413 needsbuffer &= ~VFS_BIO_NEED_FREE;
414 wakeup(&needsbuffer);
420 * waitrunningbufspace()
422 * runningbufspace is a measure of the amount of I/O currently
423 * running. This routine is used in async-write situations to
424 * prevent creating huge backups of pending writes to a device.
425 * Only asynchronous writes are governed by this function.
427 * Reads will adjust runningbufspace, but will not block based on it.
428 * The read load has a side effect of reducing the allowed write load.
430 * This does NOT turn an async write into a sync write. It waits
431 * for earlier writes to complete and generally returns before the
432 * caller's write has reached the device.
435 waitrunningbufspace(void)
438 mtx_lock(&rbreqlock);
439 while (runningbufspace > hirunningspace) {
441 msleep(&runningbufreq, &rbreqlock, PVM, "wdrain", 0);
443 mtx_unlock(&rbreqlock);
448 * vfs_buf_test_cache:
450 * Called when a buffer is extended. This function clears the B_CACHE
451 * bit if the newly extended portion of the buffer does not contain
456 vfs_buf_test_cache(struct buf *bp,
457 vm_ooffset_t foff, vm_offset_t off, vm_offset_t size,
461 VM_OBJECT_LOCK_ASSERT(m->object, MA_OWNED);
462 if (bp->b_flags & B_CACHE) {
463 int base = (foff + off) & PAGE_MASK;
464 if (vm_page_is_valid(m, base, size) == 0)
465 bp->b_flags &= ~B_CACHE;
469 /* Wake up the buffer daemon if necessary */
472 bd_wakeup(int dirtybuflevel)
476 if (bd_request == 0 && numdirtybuffers >= dirtybuflevel) {
484 * bd_speedup - speedup the buffer cache flushing code
494 if (bd_speedupreq == 0 || bd_request == 0)
504 * Calculating buffer cache scaling values and reserve space for buffer
505 * headers. This is called during low level kernel initialization and
506 * may be called more then once. We CANNOT write to the memory area
507 * being reserved at this time.
510 kern_vfs_bio_buffer_alloc(caddr_t v, long physmem_est)
516 * physmem_est is in pages. Convert it to kilobytes (assumes
517 * PAGE_SIZE is >= 1K)
519 physmem_est = physmem_est * (PAGE_SIZE / 1024);
522 * The nominal buffer size (and minimum KVA allocation) is BKVASIZE.
523 * For the first 64MB of ram nominally allocate sufficient buffers to
524 * cover 1/4 of our ram. Beyond the first 64MB allocate additional
525 * buffers to cover 1/10 of our ram over 64MB. When auto-sizing
526 * the buffer cache we limit the eventual kva reservation to
529 * factor represents the 1/4 x ram conversion.
532 int factor = 4 * BKVASIZE / 1024;
535 if (physmem_est > 4096)
536 nbuf += min((physmem_est - 4096) / factor,
538 if (physmem_est > 65536)
539 nbuf += (physmem_est - 65536) * 2 / (factor * 5);
541 if (maxbcache && nbuf > maxbcache / BKVASIZE)
542 nbuf = maxbcache / BKVASIZE;
547 /* XXX Avoid unsigned long overflows later on with maxbufspace. */
548 maxbuf = (LONG_MAX / 3) / BKVASIZE;
551 printf("Warning: nbufs lowered from %d to %ld\n", nbuf,
557 * swbufs are used as temporary holders for I/O, such as paging I/O.
558 * We have no less then 16 and no more then 256.
560 nswbuf = max(min(nbuf/4, 256), 16);
562 if (nswbuf < NSWBUF_MIN)
570 * Reserve space for the buffer cache buffers
573 v = (caddr_t)(swbuf + nswbuf);
575 v = (caddr_t)(buf + nbuf);
580 /* Initialize the buffer subsystem. Called before use of any buffers. */
587 mtx_init(&bqlock, "buf queue lock", NULL, MTX_DEF);
588 mtx_init(&rbreqlock, "runningbufspace lock", NULL, MTX_DEF);
589 mtx_init(&nblock, "needsbuffer lock", NULL, MTX_DEF);
590 mtx_init(&bdlock, "buffer daemon lock", NULL, MTX_DEF);
592 /* next, make a null set of free lists */
593 for (i = 0; i < BUFFER_QUEUES; i++)
594 TAILQ_INIT(&bufqueues[i]);
596 /* finally, initialize each buffer header and stick on empty q */
597 for (i = 0; i < nbuf; i++) {
599 bzero(bp, sizeof *bp);
600 bp->b_flags = B_INVAL; /* we're just an empty header */
601 bp->b_rcred = NOCRED;
602 bp->b_wcred = NOCRED;
603 bp->b_qindex = QUEUE_EMPTY;
604 bp->b_vflags = BV_INFREECNT; /* buf is counted as free */
606 LIST_INIT(&bp->b_dep);
608 TAILQ_INSERT_TAIL(&bufqueues[QUEUE_EMPTY], bp, b_freelist);
612 * maxbufspace is the absolute maximum amount of buffer space we are
613 * allowed to reserve in KVM and in real terms. The absolute maximum
614 * is nominally used by buf_daemon. hibufspace is the nominal maximum
615 * used by most other processes. The differential is required to
616 * ensure that buf_daemon is able to run when other processes might
617 * be blocked waiting for buffer space.
619 * maxbufspace is based on BKVASIZE. Allocating buffers larger then
620 * this may result in KVM fragmentation which is not handled optimally
623 maxbufspace = (long)nbuf * BKVASIZE;
624 hibufspace = lmax(3 * maxbufspace / 4, maxbufspace - MAXBSIZE * 10);
625 lobufspace = hibufspace - MAXBSIZE;
628 * Note: The 16 MiB upper limit for hirunningspace was chosen
629 * arbitrarily and may need further tuning. It corresponds to
630 * 128 outstanding write IO requests (if IO size is 128 KiB),
631 * which fits with many RAID controllers' tagged queuing limits.
632 * The lower 1 MiB limit is the historical upper limit for
635 hirunningspace = lmax(lmin(roundup(hibufspace / 64, MAXBSIZE),
636 16 * 1024 * 1024), 1024 * 1024);
637 lorunningspace = roundup((hirunningspace * 2) / 3, MAXBSIZE);
640 * Limit the amount of malloc memory since it is wired permanently into
641 * the kernel space. Even though this is accounted for in the buffer
642 * allocation, we don't want the malloced region to grow uncontrolled.
643 * The malloc scheme improves memory utilization significantly on average
644 * (small) directories.
646 maxbufmallocspace = hibufspace / 20;
649 * Reduce the chance of a deadlock occuring by limiting the number
650 * of delayed-write dirty buffers we allow to stack up.
652 hidirtybuffers = nbuf / 4 + 20;
653 dirtybufthresh = hidirtybuffers * 9 / 10;
656 * To support extreme low-memory systems, make sure hidirtybuffers cannot
657 * eat up all available buffer space. This occurs when our minimum cannot
658 * be met. We try to size hidirtybuffers to 3/4 our buffer space assuming
659 * BKVASIZE'd buffers.
661 while ((long)hidirtybuffers * BKVASIZE > 3 * hibufspace / 4) {
662 hidirtybuffers >>= 1;
664 lodirtybuffers = hidirtybuffers / 2;
667 * Try to keep the number of free buffers in the specified range,
668 * and give special processes (e.g. like buf_daemon) access to an
671 lofreebuffers = nbuf / 18 + 5;
672 hifreebuffers = 2 * lofreebuffers;
673 numfreebuffers = nbuf;
675 bogus_page = vm_page_alloc(NULL, 0, VM_ALLOC_NOOBJ |
676 VM_ALLOC_NORMAL | VM_ALLOC_WIRED);
680 * bfreekva() - free the kva allocation for a buffer.
682 * Since this call frees up buffer space, we call bufspacewakeup().
685 bfreekva(struct buf *bp)
689 atomic_add_int(&buffreekvacnt, 1);
690 atomic_subtract_long(&bufspace, bp->b_kvasize);
691 vm_map_remove(buffer_map, (vm_offset_t) bp->b_kvabase,
692 (vm_offset_t) bp->b_kvabase + bp->b_kvasize);
701 * Mark the buffer for removal from the appropriate free list in brelse.
705 bremfree(struct buf *bp)
709 CTR3(KTR_BUF, "bremfree(%p) vp %p flags %X", bp, bp->b_vp, bp->b_flags);
710 KASSERT((bp->b_flags & B_REMFREE) == 0,
711 ("bremfree: buffer %p already marked for delayed removal.", bp));
712 KASSERT(bp->b_qindex != QUEUE_NONE,
713 ("bremfree: buffer %p not on a queue.", bp));
716 bp->b_flags |= B_REMFREE;
717 /* Fixup numfreebuffers count. */
718 if ((bp->b_flags & B_INVAL) || (bp->b_flags & B_DELWRI) == 0) {
719 KASSERT((bp->b_vflags & BV_INFREECNT) != 0,
720 ("buf %p not counted in numfreebuffers", bp));
721 if (bp->b_bufobj != NULL)
722 mtx_assert(BO_MTX(bp->b_bufobj), MA_OWNED);
723 bp->b_vflags &= ~BV_INFREECNT;
724 old = atomic_fetchadd_int(&numfreebuffers, -1);
725 KASSERT(old > 0, ("numfreebuffers dropped to %d", old - 1));
732 * Force an immediate removal from a free list. Used only in nfs when
733 * it abuses the b_freelist pointer.
736 bremfreef(struct buf *bp)
746 * Removes a buffer from the free list, must be called with the
750 bremfreel(struct buf *bp)
754 CTR3(KTR_BUF, "bremfreel(%p) vp %p flags %X",
755 bp, bp->b_vp, bp->b_flags);
756 KASSERT(bp->b_qindex != QUEUE_NONE,
757 ("bremfreel: buffer %p not on a queue.", bp));
759 mtx_assert(&bqlock, MA_OWNED);
761 TAILQ_REMOVE(&bufqueues[bp->b_qindex], bp, b_freelist);
762 bp->b_qindex = QUEUE_NONE;
764 * If this was a delayed bremfree() we only need to remove the buffer
765 * from the queue and return the stats are already done.
767 if (bp->b_flags & B_REMFREE) {
768 bp->b_flags &= ~B_REMFREE;
772 * Fixup numfreebuffers count. If the buffer is invalid or not
773 * delayed-write, the buffer was free and we must decrement
776 if ((bp->b_flags & B_INVAL) || (bp->b_flags & B_DELWRI) == 0) {
777 KASSERT((bp->b_vflags & BV_INFREECNT) != 0,
778 ("buf %p not counted in numfreebuffers", bp));
779 if (bp->b_bufobj != NULL)
780 mtx_assert(BO_MTX(bp->b_bufobj), MA_OWNED);
781 bp->b_vflags &= ~BV_INFREECNT;
782 old = atomic_fetchadd_int(&numfreebuffers, -1);
783 KASSERT(old > 0, ("numfreebuffers dropped to %d", old - 1));
788 * Attempt to initiate asynchronous I/O on read-ahead blocks. We must
789 * clear BIO_ERROR and B_INVAL prior to initiating I/O . If B_CACHE is set,
790 * the buffer is valid and we do not have to do anything.
793 breada(struct vnode * vp, daddr_t * rablkno, int * rabsize,
794 int cnt, struct ucred * cred)
799 for (i = 0; i < cnt; i++, rablkno++, rabsize++) {
800 if (inmem(vp, *rablkno))
802 rabp = getblk(vp, *rablkno, *rabsize, 0, 0, 0);
804 if ((rabp->b_flags & B_CACHE) == 0) {
805 if (!TD_IS_IDLETHREAD(curthread))
806 curthread->td_ru.ru_inblock++;
807 rabp->b_flags |= B_ASYNC;
808 rabp->b_flags &= ~B_INVAL;
809 rabp->b_ioflags &= ~BIO_ERROR;
810 rabp->b_iocmd = BIO_READ;
811 if (rabp->b_rcred == NOCRED && cred != NOCRED)
812 rabp->b_rcred = crhold(cred);
813 vfs_busy_pages(rabp, 0);
815 rabp->b_iooffset = dbtob(rabp->b_blkno);
824 * Entry point for bread() and breadn() via #defines in sys/buf.h.
826 * Get a buffer with the specified data. Look in the cache first. We
827 * must clear BIO_ERROR and B_INVAL prior to initiating I/O. If B_CACHE
828 * is set, the buffer is valid and we do not have to do anything, see
829 * getblk(). Also starts asynchronous I/O on read-ahead blocks.
832 breadn_flags(struct vnode * vp, daddr_t blkno, int size,
833 daddr_t * rablkno, int *rabsize, int cnt,
834 struct ucred * cred, int flags, struct buf **bpp)
837 int rv = 0, readwait = 0;
839 CTR3(KTR_BUF, "breadn(%p, %jd, %d)", vp, blkno, size);
841 * Can only return NULL if GB_LOCK_NOWAIT flag is specified.
843 *bpp = bp = getblk(vp, blkno, size, 0, 0, flags);
847 /* if not found in cache, do some I/O */
848 if ((bp->b_flags & B_CACHE) == 0) {
849 if (!TD_IS_IDLETHREAD(curthread))
850 curthread->td_ru.ru_inblock++;
851 bp->b_iocmd = BIO_READ;
852 bp->b_flags &= ~B_INVAL;
853 bp->b_ioflags &= ~BIO_ERROR;
854 if (bp->b_rcred == NOCRED && cred != NOCRED)
855 bp->b_rcred = crhold(cred);
856 vfs_busy_pages(bp, 0);
857 bp->b_iooffset = dbtob(bp->b_blkno);
862 breada(vp, rablkno, rabsize, cnt, cred);
871 * Write, release buffer on completion. (Done by iodone
872 * if async). Do not bother writing anything if the buffer
875 * Note that we set B_CACHE here, indicating that buffer is
876 * fully valid and thus cacheable. This is true even of NFS
877 * now so we set it generally. This could be set either here
878 * or in biodone() since the I/O is synchronous. We put it
882 bufwrite(struct buf *bp)
888 CTR3(KTR_BUF, "bufwrite(%p) vp %p flags %X", bp, bp->b_vp, bp->b_flags);
889 if (bp->b_flags & B_INVAL) {
894 if (bp->b_flags & B_BARRIER)
897 oldflags = bp->b_flags;
901 if (bp->b_pin_count > 0)
904 KASSERT(!(bp->b_vflags & BV_BKGRDINPROG),
905 ("FFS background buffer should not get here %p", bp));
909 vp_md = vp->v_vflag & VV_MD;
913 /* Mark the buffer clean */
916 bp->b_flags &= ~B_DONE;
917 bp->b_ioflags &= ~BIO_ERROR;
918 bp->b_flags |= B_CACHE;
919 bp->b_iocmd = BIO_WRITE;
921 bufobj_wref(bp->b_bufobj);
922 vfs_busy_pages(bp, 1);
925 * Normal bwrites pipeline writes
927 bp->b_runningbufspace = bp->b_bufsize;
928 atomic_add_long(&runningbufspace, bp->b_runningbufspace);
930 if (!TD_IS_IDLETHREAD(curthread))
931 curthread->td_ru.ru_oublock++;
932 if (oldflags & B_ASYNC)
934 bp->b_iooffset = dbtob(bp->b_blkno);
937 if ((oldflags & B_ASYNC) == 0) {
938 int rtval = bufwait(bp);
943 * don't allow the async write to saturate the I/O
944 * system. We will not deadlock here because
945 * we are blocking waiting for I/O that is already in-progress
946 * to complete. We do not block here if it is the update
947 * or syncer daemon trying to clean up as that can lead
950 if ((curthread->td_pflags & TDP_NORUNNINGBUF) == 0 && !vp_md)
951 waitrunningbufspace();
958 bufbdflush(struct bufobj *bo, struct buf *bp)
962 if (bo->bo_dirty.bv_cnt > dirtybufthresh + 10) {
963 (void) VOP_FSYNC(bp->b_vp, MNT_NOWAIT, curthread);
965 } else if (bo->bo_dirty.bv_cnt > dirtybufthresh) {
968 * Try to find a buffer to flush.
970 TAILQ_FOREACH(nbp, &bo->bo_dirty.bv_hd, b_bobufs) {
971 if ((nbp->b_vflags & BV_BKGRDINPROG) ||
973 LK_EXCLUSIVE | LK_NOWAIT, NULL))
976 panic("bdwrite: found ourselves");
978 /* Don't countdeps with the bo lock held. */
979 if (buf_countdeps(nbp, 0)) {
984 if (nbp->b_flags & B_CLUSTEROK) {
990 dirtybufferflushes++;
999 * Delayed write. (Buffer is marked dirty). Do not bother writing
1000 * anything if the buffer is marked invalid.
1002 * Note that since the buffer must be completely valid, we can safely
1003 * set B_CACHE. In fact, we have to set B_CACHE here rather then in
1004 * biodone() in order to prevent getblk from writing the buffer
1005 * out synchronously.
1008 bdwrite(struct buf *bp)
1010 struct thread *td = curthread;
1014 CTR3(KTR_BUF, "bdwrite(%p) vp %p flags %X", bp, bp->b_vp, bp->b_flags);
1015 KASSERT(bp->b_bufobj != NULL, ("No b_bufobj %p", bp));
1016 KASSERT((bp->b_flags & B_BARRIER) == 0,
1017 ("Barrier request in delayed write %p", bp));
1018 BUF_ASSERT_HELD(bp);
1020 if (bp->b_flags & B_INVAL) {
1026 * If we have too many dirty buffers, don't create any more.
1027 * If we are wildly over our limit, then force a complete
1028 * cleanup. Otherwise, just keep the situation from getting
1029 * out of control. Note that we have to avoid a recursive
1030 * disaster and not try to clean up after our own cleanup!
1034 if ((td->td_pflags & (TDP_COWINPROGRESS|TDP_INBDFLUSH)) == 0) {
1035 td->td_pflags |= TDP_INBDFLUSH;
1037 td->td_pflags &= ~TDP_INBDFLUSH;
1043 * Set B_CACHE, indicating that the buffer is fully valid. This is
1044 * true even of NFS now.
1046 bp->b_flags |= B_CACHE;
1049 * This bmap keeps the system from needing to do the bmap later,
1050 * perhaps when the system is attempting to do a sync. Since it
1051 * is likely that the indirect block -- or whatever other datastructure
1052 * that the filesystem needs is still in memory now, it is a good
1053 * thing to do this. Note also, that if the pageout daemon is
1054 * requesting a sync -- there might not be enough memory to do
1055 * the bmap then... So, this is important to do.
1057 if (vp->v_type != VCHR && bp->b_lblkno == bp->b_blkno) {
1058 VOP_BMAP(vp, bp->b_lblkno, NULL, &bp->b_blkno, NULL, NULL);
1062 * Set the *dirty* buffer range based upon the VM system dirty
1065 * Mark the buffer pages as clean. We need to do this here to
1066 * satisfy the vnode_pager and the pageout daemon, so that it
1067 * thinks that the pages have been "cleaned". Note that since
1068 * the pages are in a delayed write buffer -- the VFS layer
1069 * "will" see that the pages get written out on the next sync,
1070 * or perhaps the cluster will be completed.
1072 vfs_clean_pages_dirty_buf(bp);
1076 * Wakeup the buffer flushing daemon if we have a lot of dirty
1077 * buffers (midpoint between our recovery point and our stall
1080 bd_wakeup((lodirtybuffers + hidirtybuffers) / 2);
1083 * note: we cannot initiate I/O from a bdwrite even if we wanted to,
1084 * due to the softdep code.
1091 * Turn buffer into delayed write request. We must clear BIO_READ and
1092 * B_RELBUF, and we must set B_DELWRI. We reassign the buffer to
1093 * itself to properly update it in the dirty/clean lists. We mark it
1094 * B_DONE to ensure that any asynchronization of the buffer properly
1095 * clears B_DONE ( else a panic will occur later ).
1097 * bdirty() is kinda like bdwrite() - we have to clear B_INVAL which
1098 * might have been set pre-getblk(). Unlike bwrite/bdwrite, bdirty()
1099 * should only be called if the buffer is known-good.
1101 * Since the buffer is not on a queue, we do not update the numfreebuffers
1104 * The buffer must be on QUEUE_NONE.
1107 bdirty(struct buf *bp)
1110 CTR3(KTR_BUF, "bdirty(%p) vp %p flags %X",
1111 bp, bp->b_vp, bp->b_flags);
1112 KASSERT(bp->b_bufobj != NULL, ("No b_bufobj %p", bp));
1113 KASSERT(bp->b_flags & B_REMFREE || bp->b_qindex == QUEUE_NONE,
1114 ("bdirty: buffer %p still on queue %d", bp, bp->b_qindex));
1115 BUF_ASSERT_HELD(bp);
1116 bp->b_flags &= ~(B_RELBUF);
1117 bp->b_iocmd = BIO_WRITE;
1119 if ((bp->b_flags & B_DELWRI) == 0) {
1120 bp->b_flags |= /* XXX B_DONE | */ B_DELWRI;
1122 atomic_add_int(&numdirtybuffers, 1);
1123 bd_wakeup((lodirtybuffers + hidirtybuffers) / 2);
1130 * Clear B_DELWRI for buffer.
1132 * Since the buffer is not on a queue, we do not update the numfreebuffers
1135 * The buffer must be on QUEUE_NONE.
1139 bundirty(struct buf *bp)
1142 CTR3(KTR_BUF, "bundirty(%p) vp %p flags %X", bp, bp->b_vp, bp->b_flags);
1143 KASSERT(bp->b_bufobj != NULL, ("No b_bufobj %p", bp));
1144 KASSERT(bp->b_flags & B_REMFREE || bp->b_qindex == QUEUE_NONE,
1145 ("bundirty: buffer %p still on queue %d", bp, bp->b_qindex));
1146 BUF_ASSERT_HELD(bp);
1148 if (bp->b_flags & B_DELWRI) {
1149 bp->b_flags &= ~B_DELWRI;
1151 atomic_subtract_int(&numdirtybuffers, 1);
1152 numdirtywakeup(lodirtybuffers);
1155 * Since it is now being written, we can clear its deferred write flag.
1157 bp->b_flags &= ~B_DEFERRED;
1163 * Asynchronous write. Start output on a buffer, but do not wait for
1164 * it to complete. The buffer is released when the output completes.
1166 * bwrite() ( or the VOP routine anyway ) is responsible for handling
1167 * B_INVAL buffers. Not us.
1170 bawrite(struct buf *bp)
1173 bp->b_flags |= B_ASYNC;
1180 * Asynchronous barrier write. Start output on a buffer, but do not
1181 * wait for it to complete. Place a write barrier after this write so
1182 * that this buffer and all buffers written before it are committed to
1183 * the disk before any buffers written after this write are committed
1184 * to the disk. The buffer is released when the output completes.
1187 babarrierwrite(struct buf *bp)
1190 bp->b_flags |= B_ASYNC | B_BARRIER;
1197 * Synchronous barrier write. Start output on a buffer and wait for
1198 * it to complete. Place a write barrier after this write so that
1199 * this buffer and all buffers written before it are committed to
1200 * the disk before any buffers written after this write are committed
1201 * to the disk. The buffer is released when the output completes.
1204 bbarrierwrite(struct buf *bp)
1207 bp->b_flags |= B_BARRIER;
1208 return (bwrite(bp));
1214 * Called prior to the locking of any vnodes when we are expecting to
1215 * write. We do not want to starve the buffer cache with too many
1216 * dirty buffers so we block here. By blocking prior to the locking
1217 * of any vnodes we attempt to avoid the situation where a locked vnode
1218 * prevents the various system daemons from flushing related buffers.
1225 if (numdirtybuffers >= hidirtybuffers) {
1227 while (numdirtybuffers >= hidirtybuffers) {
1229 needsbuffer |= VFS_BIO_NEED_DIRTYFLUSH;
1230 msleep(&needsbuffer, &nblock,
1231 (PRIBIO + 4), "flswai", 0);
1233 mtx_unlock(&nblock);
1238 * Return true if we have too many dirty buffers.
1241 buf_dirty_count_severe(void)
1244 return(numdirtybuffers >= hidirtybuffers);
1247 static __noinline int
1248 buf_vm_page_count_severe(void)
1251 KFAIL_POINT_CODE(DEBUG_FP, buf_pressure, return 1);
1253 return vm_page_count_severe();
1259 * Release a busy buffer and, if requested, free its resources. The
1260 * buffer will be stashed in the appropriate bufqueue[] allowing it
1261 * to be accessed later as a cache entity or reused for other purposes.
1264 brelse(struct buf *bp)
1266 CTR3(KTR_BUF, "brelse(%p) vp %p flags %X",
1267 bp, bp->b_vp, bp->b_flags);
1268 KASSERT(!(bp->b_flags & (B_CLUSTER|B_PAGING)),
1269 ("brelse: inappropriate B_PAGING or B_CLUSTER bp %p", bp));
1271 if (BUF_LOCKRECURSED(bp)) {
1273 * Do not process, in particular, do not handle the
1274 * B_INVAL/B_RELBUF and do not release to free list.
1280 if (bp->b_flags & B_MANAGED) {
1285 if (bp->b_iocmd == BIO_WRITE && (bp->b_ioflags & BIO_ERROR) &&
1286 bp->b_error == EIO && !(bp->b_flags & B_INVAL)) {
1288 * Failed write, redirty. Must clear BIO_ERROR to prevent
1289 * pages from being scrapped. If the error is anything
1290 * other than an I/O error (EIO), assume that retrying
1293 bp->b_ioflags &= ~BIO_ERROR;
1295 } else if ((bp->b_flags & (B_NOCACHE | B_INVAL)) ||
1296 (bp->b_ioflags & BIO_ERROR) || (bp->b_bufsize <= 0)) {
1298 * Either a failed I/O or we were asked to free or not
1301 bp->b_flags |= B_INVAL;
1302 if (!LIST_EMPTY(&bp->b_dep))
1304 if (bp->b_flags & B_DELWRI) {
1305 atomic_subtract_int(&numdirtybuffers, 1);
1306 numdirtywakeup(lodirtybuffers);
1308 bp->b_flags &= ~(B_DELWRI | B_CACHE);
1309 if ((bp->b_flags & B_VMIO) == 0) {
1318 * We must clear B_RELBUF if B_DELWRI is set. If vfs_vmio_release()
1319 * is called with B_DELWRI set, the underlying pages may wind up
1320 * getting freed causing a previous write (bdwrite()) to get 'lost'
1321 * because pages associated with a B_DELWRI bp are marked clean.
1323 * We still allow the B_INVAL case to call vfs_vmio_release(), even
1324 * if B_DELWRI is set.
1326 * If B_DELWRI is not set we may have to set B_RELBUF if we are low
1327 * on pages to return pages to the VM page queues.
1329 if (bp->b_flags & B_DELWRI)
1330 bp->b_flags &= ~B_RELBUF;
1331 else if (buf_vm_page_count_severe()) {
1333 * The locking of the BO_LOCK is not necessary since
1334 * BKGRDINPROG cannot be set while we hold the buf
1335 * lock, it can only be cleared if it is already
1339 if (!(bp->b_vflags & BV_BKGRDINPROG))
1340 bp->b_flags |= B_RELBUF;
1342 bp->b_flags |= B_RELBUF;
1346 * VMIO buffer rundown. It is not very necessary to keep a VMIO buffer
1347 * constituted, not even NFS buffers now. Two flags effect this. If
1348 * B_INVAL, the struct buf is invalidated but the VM object is kept
1349 * around ( i.e. so it is trivial to reconstitute the buffer later ).
1351 * If BIO_ERROR or B_NOCACHE is set, pages in the VM object will be
1352 * invalidated. BIO_ERROR cannot be set for a failed write unless the
1353 * buffer is also B_INVAL because it hits the re-dirtying code above.
1355 * Normally we can do this whether a buffer is B_DELWRI or not. If
1356 * the buffer is an NFS buffer, it is tracking piecemeal writes or
1357 * the commit state and we cannot afford to lose the buffer. If the
1358 * buffer has a background write in progress, we need to keep it
1359 * around to prevent it from being reconstituted and starting a second
1362 if ((bp->b_flags & B_VMIO)
1363 && !(bp->b_vp->v_mount != NULL &&
1364 (bp->b_vp->v_mount->mnt_vfc->vfc_flags & VFCF_NETWORK) != 0 &&
1365 !vn_isdisk(bp->b_vp, NULL) &&
1366 (bp->b_flags & B_DELWRI))
1375 obj = bp->b_bufobj->bo_object;
1378 * Get the base offset and length of the buffer. Note that
1379 * in the VMIO case if the buffer block size is not
1380 * page-aligned then b_data pointer may not be page-aligned.
1381 * But our b_pages[] array *IS* page aligned.
1383 * block sizes less then DEV_BSIZE (usually 512) are not
1384 * supported due to the page granularity bits (m->valid,
1385 * m->dirty, etc...).
1387 * See man buf(9) for more information
1389 resid = bp->b_bufsize;
1390 foff = bp->b_offset;
1391 VM_OBJECT_LOCK(obj);
1392 for (i = 0; i < bp->b_npages; i++) {
1398 * If we hit a bogus page, fixup *all* the bogus pages
1401 if (m == bogus_page) {
1402 poff = OFF_TO_IDX(bp->b_offset);
1405 for (j = i; j < bp->b_npages; j++) {
1407 mtmp = bp->b_pages[j];
1408 if (mtmp == bogus_page) {
1409 mtmp = vm_page_lookup(obj, poff + j);
1411 panic("brelse: page missing\n");
1413 bp->b_pages[j] = mtmp;
1417 if ((bp->b_flags & B_INVAL) == 0) {
1419 trunc_page((vm_offset_t)bp->b_data),
1420 bp->b_pages, bp->b_npages);
1424 if ((bp->b_flags & B_NOCACHE) ||
1425 (bp->b_ioflags & BIO_ERROR &&
1426 bp->b_iocmd == BIO_READ)) {
1427 int poffset = foff & PAGE_MASK;
1428 int presid = resid > (PAGE_SIZE - poffset) ?
1429 (PAGE_SIZE - poffset) : resid;
1431 KASSERT(presid >= 0, ("brelse: extra page"));
1432 vm_page_set_invalid(m, poffset, presid);
1434 printf("avoided corruption bug in bogus_page/brelse code\n");
1436 resid -= PAGE_SIZE - (foff & PAGE_MASK);
1437 foff = (foff + PAGE_SIZE) & ~(off_t)PAGE_MASK;
1439 VM_OBJECT_UNLOCK(obj);
1440 if (bp->b_flags & (B_INVAL | B_RELBUF))
1441 vfs_vmio_release(bp);
1443 } else if (bp->b_flags & B_VMIO) {
1445 if (bp->b_flags & (B_INVAL | B_RELBUF)) {
1446 vfs_vmio_release(bp);
1449 } else if ((bp->b_flags & (B_INVAL | B_RELBUF)) != 0) {
1450 if (bp->b_bufsize != 0)
1452 if (bp->b_vp != NULL)
1458 /* Handle delayed bremfree() processing. */
1459 if (bp->b_flags & B_REMFREE) {
1469 if (bp->b_qindex != QUEUE_NONE)
1470 panic("brelse: free buffer onto another queue???");
1473 * If the buffer has junk contents signal it and eventually
1474 * clean up B_DELWRI and diassociate the vnode so that gbincore()
1477 if (bp->b_bufsize == 0 || (bp->b_ioflags & BIO_ERROR) != 0 ||
1478 (bp->b_flags & (B_INVAL | B_NOCACHE | B_RELBUF)) != 0)
1479 bp->b_flags |= B_INVAL;
1480 if (bp->b_flags & B_INVAL) {
1481 if (bp->b_flags & B_DELWRI)
1487 /* buffers with no memory */
1488 if (bp->b_bufsize == 0) {
1489 bp->b_xflags &= ~(BX_BKGRDWRITE | BX_ALTDATA);
1490 if (bp->b_vflags & BV_BKGRDINPROG)
1491 panic("losing buffer 1");
1492 if (bp->b_kvasize) {
1493 bp->b_qindex = QUEUE_EMPTYKVA;
1495 bp->b_qindex = QUEUE_EMPTY;
1497 TAILQ_INSERT_HEAD(&bufqueues[bp->b_qindex], bp, b_freelist);
1498 /* buffers with junk contents */
1499 } else if (bp->b_flags & (B_INVAL | B_NOCACHE | B_RELBUF) ||
1500 (bp->b_ioflags & BIO_ERROR)) {
1501 bp->b_xflags &= ~(BX_BKGRDWRITE | BX_ALTDATA);
1502 if (bp->b_vflags & BV_BKGRDINPROG)
1503 panic("losing buffer 2");
1504 bp->b_qindex = QUEUE_CLEAN;
1505 TAILQ_INSERT_HEAD(&bufqueues[QUEUE_CLEAN], bp, b_freelist);
1506 /* remaining buffers */
1508 if (bp->b_flags & B_DELWRI)
1509 bp->b_qindex = QUEUE_DIRTY;
1511 bp->b_qindex = QUEUE_CLEAN;
1512 if (bp->b_flags & B_AGE)
1513 TAILQ_INSERT_HEAD(&bufqueues[bp->b_qindex], bp, b_freelist);
1515 TAILQ_INSERT_TAIL(&bufqueues[bp->b_qindex], bp, b_freelist);
1517 mtx_unlock(&bqlock);
1520 * Fixup numfreebuffers count. The bp is on an appropriate queue
1521 * unless locked. We then bump numfreebuffers if it is not B_DELWRI.
1522 * We've already handled the B_INVAL case ( B_DELWRI will be clear
1523 * if B_INVAL is set ).
1526 if (!(bp->b_flags & B_DELWRI)) {
1538 * Something we can maybe free or reuse
1540 if (bp->b_bufsize || bp->b_kvasize)
1543 bp->b_flags &= ~(B_ASYNC | B_NOCACHE | B_AGE | B_RELBUF | B_DIRECT);
1544 if ((bp->b_flags & B_DELWRI) == 0 && (bp->b_xflags & BX_VNDIRTY))
1545 panic("brelse: not dirty");
1551 * Release a buffer back to the appropriate queue but do not try to free
1552 * it. The buffer is expected to be used again soon.
1554 * bqrelse() is used by bdwrite() to requeue a delayed write, and used by
1555 * biodone() to requeue an async I/O on completion. It is also used when
1556 * known good buffers need to be requeued but we think we may need the data
1559 * XXX we should be able to leave the B_RELBUF hint set on completion.
1562 bqrelse(struct buf *bp)
1566 CTR3(KTR_BUF, "bqrelse(%p) vp %p flags %X", bp, bp->b_vp, bp->b_flags);
1567 KASSERT(!(bp->b_flags & (B_CLUSTER|B_PAGING)),
1568 ("bqrelse: inappropriate B_PAGING or B_CLUSTER bp %p", bp));
1570 if (BUF_LOCKRECURSED(bp)) {
1571 /* do not release to free list */
1577 if (bp->b_flags & B_MANAGED) {
1578 if (bp->b_flags & B_REMFREE) {
1585 mtx_unlock(&bqlock);
1587 bp->b_flags &= ~(B_ASYNC | B_NOCACHE | B_AGE | B_RELBUF);
1593 /* Handle delayed bremfree() processing. */
1594 if (bp->b_flags & B_REMFREE) {
1601 if (bp->b_qindex != QUEUE_NONE)
1602 panic("bqrelse: free buffer onto another queue???");
1603 /* buffers with stale but valid contents */
1604 if (bp->b_flags & B_DELWRI) {
1605 bp->b_qindex = QUEUE_DIRTY;
1606 TAILQ_INSERT_TAIL(&bufqueues[bp->b_qindex], bp, b_freelist);
1609 * The locking of the BO_LOCK for checking of the
1610 * BV_BKGRDINPROG is not necessary since the
1611 * BV_BKGRDINPROG cannot be set while we hold the buf
1612 * lock, it can only be cleared if it is already
1615 if (!buf_vm_page_count_severe() || (bp->b_vflags & BV_BKGRDINPROG)) {
1616 bp->b_qindex = QUEUE_CLEAN;
1617 TAILQ_INSERT_TAIL(&bufqueues[QUEUE_CLEAN], bp,
1621 * We are too low on memory, we have to try to free
1622 * the buffer (most importantly: the wired pages
1623 * making up its backing store) *now*.
1625 mtx_unlock(&bqlock);
1630 mtx_unlock(&bqlock);
1632 if ((bp->b_flags & B_INVAL) || !(bp->b_flags & B_DELWRI)) {
1641 * Something we can maybe free or reuse.
1643 if (bp->b_bufsize && !(bp->b_flags & B_DELWRI))
1646 bp->b_flags &= ~(B_ASYNC | B_NOCACHE | B_AGE | B_RELBUF);
1647 if ((bp->b_flags & B_DELWRI) == 0 && (bp->b_xflags & BX_VNDIRTY))
1648 panic("bqrelse: not dirty");
1653 /* Give pages used by the bp back to the VM system (where possible) */
1655 vfs_vmio_release(struct buf *bp)
1660 pmap_qremove(trunc_page((vm_offset_t)bp->b_data), bp->b_npages);
1661 VM_OBJECT_LOCK(bp->b_bufobj->bo_object);
1662 for (i = 0; i < bp->b_npages; i++) {
1664 bp->b_pages[i] = NULL;
1666 * In order to keep page LRU ordering consistent, put
1667 * everything on the inactive queue.
1670 vm_page_unwire(m, 0);
1672 * We don't mess with busy pages, it is
1673 * the responsibility of the process that
1674 * busied the pages to deal with them.
1676 if ((m->oflags & VPO_BUSY) == 0 && m->busy == 0 &&
1677 m->wire_count == 0) {
1679 * Might as well free the page if we can and it has
1680 * no valid data. We also free the page if the
1681 * buffer was used for direct I/O
1683 if ((bp->b_flags & B_ASYNC) == 0 && !m->valid) {
1685 } else if (bp->b_flags & B_DIRECT) {
1686 vm_page_try_to_free(m);
1687 } else if (buf_vm_page_count_severe()) {
1688 vm_page_try_to_cache(m);
1693 VM_OBJECT_UNLOCK(bp->b_bufobj->bo_object);
1695 if (bp->b_bufsize) {
1700 bp->b_flags &= ~B_VMIO;
1706 * Check to see if a block at a particular lbn is available for a clustered
1710 vfs_bio_clcheck(struct vnode *vp, int size, daddr_t lblkno, daddr_t blkno)
1717 /* If the buf isn't in core skip it */
1718 if ((bpa = gbincore(&vp->v_bufobj, lblkno)) == NULL)
1721 /* If the buf is busy we don't want to wait for it */
1722 if (BUF_LOCK(bpa, LK_EXCLUSIVE | LK_NOWAIT, NULL) != 0)
1725 /* Only cluster with valid clusterable delayed write buffers */
1726 if ((bpa->b_flags & (B_DELWRI | B_CLUSTEROK | B_INVAL)) !=
1727 (B_DELWRI | B_CLUSTEROK))
1730 if (bpa->b_bufsize != size)
1734 * Check to see if it is in the expected place on disk and that the
1735 * block has been mapped.
1737 if ((bpa->b_blkno != bpa->b_lblkno) && (bpa->b_blkno == blkno))
1747 * Implement clustered async writes for clearing out B_DELWRI buffers.
1748 * This is much better then the old way of writing only one buffer at
1749 * a time. Note that we may not be presented with the buffers in the
1750 * correct order, so we search for the cluster in both directions.
1753 vfs_bio_awrite(struct buf *bp)
1758 daddr_t lblkno = bp->b_lblkno;
1759 struct vnode *vp = bp->b_vp;
1767 * right now we support clustered writing only to regular files. If
1768 * we find a clusterable block we could be in the middle of a cluster
1769 * rather then at the beginning.
1771 if ((vp->v_type == VREG) &&
1772 (vp->v_mount != 0) && /* Only on nodes that have the size info */
1773 (bp->b_flags & (B_CLUSTEROK | B_INVAL)) == B_CLUSTEROK) {
1775 size = vp->v_mount->mnt_stat.f_iosize;
1776 maxcl = MAXPHYS / size;
1779 for (i = 1; i < maxcl; i++)
1780 if (vfs_bio_clcheck(vp, size, lblkno + i,
1781 bp->b_blkno + ((i * size) >> DEV_BSHIFT)) == 0)
1784 for (j = 1; i + j <= maxcl && j <= lblkno; j++)
1785 if (vfs_bio_clcheck(vp, size, lblkno - j,
1786 bp->b_blkno - ((j * size) >> DEV_BSHIFT)) == 0)
1792 * this is a possible cluster write
1796 nwritten = cluster_wbuild(vp, size, lblkno - j, ncl);
1801 bp->b_flags |= B_ASYNC;
1803 * default (old) behavior, writing out only one block
1805 * XXX returns b_bufsize instead of b_bcount for nwritten?
1807 nwritten = bp->b_bufsize;
1816 * Find and initialize a new buffer header, freeing up existing buffers
1817 * in the bufqueues as necessary. The new buffer is returned locked.
1819 * Important: B_INVAL is not set. If the caller wishes to throw the
1820 * buffer away, the caller must set B_INVAL prior to calling brelse().
1823 * We have insufficient buffer headers
1824 * We have insufficient buffer space
1825 * buffer_map is too fragmented ( space reservation fails )
1826 * If we have to flush dirty buffers ( but we try to avoid this )
1828 * To avoid VFS layer recursion we do not flush dirty buffers ourselves.
1829 * Instead we ask the buf daemon to do it for us. We attempt to
1830 * avoid piecemeal wakeups of the pageout daemon.
1834 getnewbuf(struct vnode *vp, int slpflag, int slptimeo, int size, int maxsize,
1842 static int flushingbufs;
1846 * We can't afford to block since we might be holding a vnode lock,
1847 * which may prevent system daemons from running. We deal with
1848 * low-memory situations by proactively returning memory and running
1849 * async I/O rather then sync I/O.
1851 atomic_add_int(&getnewbufcalls, 1);
1852 atomic_subtract_int(&getnewbufrestarts, 1);
1854 atomic_add_int(&getnewbufrestarts, 1);
1857 * Setup for scan. If we do not have enough free buffers,
1858 * we setup a degenerate case that immediately fails. Note
1859 * that if we are specially marked process, we are allowed to
1860 * dip into our reserves.
1862 * The scanning sequence is nominally: EMPTY->EMPTYKVA->CLEAN
1864 * We start with EMPTYKVA. If the list is empty we backup to EMPTY.
1865 * However, there are a number of cases (defragging, reusing, ...)
1866 * where we cannot backup.
1869 nqindex = QUEUE_EMPTYKVA;
1870 nbp = TAILQ_FIRST(&bufqueues[QUEUE_EMPTYKVA]);
1874 * If no EMPTYKVA buffers and we are either
1875 * defragging or reusing, locate a CLEAN buffer
1876 * to free or reuse. If bufspace useage is low
1877 * skip this step so we can allocate a new buffer.
1879 if (defrag || bufspace >= lobufspace) {
1880 nqindex = QUEUE_CLEAN;
1881 nbp = TAILQ_FIRST(&bufqueues[QUEUE_CLEAN]);
1885 * If we could not find or were not allowed to reuse a
1886 * CLEAN buffer, check to see if it is ok to use an EMPTY
1887 * buffer. We can only use an EMPTY buffer if allocating
1888 * its KVA would not otherwise run us out of buffer space.
1890 if (nbp == NULL && defrag == 0 &&
1891 bufspace + maxsize < hibufspace) {
1892 nqindex = QUEUE_EMPTY;
1893 nbp = TAILQ_FIRST(&bufqueues[QUEUE_EMPTY]);
1898 * Run scan, possibly freeing data and/or kva mappings on the fly
1902 while ((bp = nbp) != NULL) {
1903 int qindex = nqindex;
1906 * Calculate next bp ( we can only use it if we do not block
1907 * or do other fancy things ).
1909 if ((nbp = TAILQ_NEXT(bp, b_freelist)) == NULL) {
1912 nqindex = QUEUE_EMPTYKVA;
1913 if ((nbp = TAILQ_FIRST(&bufqueues[QUEUE_EMPTYKVA])))
1916 case QUEUE_EMPTYKVA:
1917 nqindex = QUEUE_CLEAN;
1918 if ((nbp = TAILQ_FIRST(&bufqueues[QUEUE_CLEAN])))
1929 * If we are defragging then we need a buffer with
1930 * b_kvasize != 0. XXX this situation should no longer
1931 * occur, if defrag is non-zero the buffer's b_kvasize
1932 * should also be non-zero at this point. XXX
1934 if (defrag && bp->b_kvasize == 0) {
1935 printf("Warning: defrag empty buffer %p\n", bp);
1940 * Start freeing the bp. This is somewhat involved. nbp
1941 * remains valid only for QUEUE_EMPTY[KVA] bp's.
1943 if (BUF_LOCK(bp, LK_EXCLUSIVE | LK_NOWAIT, NULL) != 0)
1946 BO_LOCK(bp->b_bufobj);
1947 if (bp->b_vflags & BV_BKGRDINPROG) {
1948 BO_UNLOCK(bp->b_bufobj);
1952 BO_UNLOCK(bp->b_bufobj);
1955 "getnewbuf(%p) vp %p flags %X kvasize %d bufsize %d "
1956 "queue %d (recycling)", bp, bp->b_vp, bp->b_flags,
1957 bp->b_kvasize, bp->b_bufsize, qindex);
1962 KASSERT(bp->b_qindex == qindex, ("getnewbuf: inconsistant queue %d bp %p", qindex, bp));
1965 * Note: we no longer distinguish between VMIO and non-VMIO
1969 KASSERT((bp->b_flags & B_DELWRI) == 0, ("delwri buffer %p found in queue %d", bp, qindex));
1971 if (bp->b_bufobj != NULL)
1972 BO_LOCK(bp->b_bufobj);
1974 if (bp->b_bufobj != NULL)
1975 BO_UNLOCK(bp->b_bufobj);
1976 mtx_unlock(&bqlock);
1978 if (qindex == QUEUE_CLEAN) {
1979 if (bp->b_flags & B_VMIO) {
1980 bp->b_flags &= ~B_ASYNC;
1981 vfs_vmio_release(bp);
1988 * NOTE: nbp is now entirely invalid. We can only restart
1989 * the scan from this point on.
1991 * Get the rest of the buffer freed up. b_kva* is still
1992 * valid after this operation.
1995 if (bp->b_rcred != NOCRED) {
1996 crfree(bp->b_rcred);
1997 bp->b_rcred = NOCRED;
1999 if (bp->b_wcred != NOCRED) {
2000 crfree(bp->b_wcred);
2001 bp->b_wcred = NOCRED;
2003 if (!LIST_EMPTY(&bp->b_dep))
2005 if (bp->b_vflags & BV_BKGRDINPROG)
2006 panic("losing buffer 3");
2007 KASSERT(bp->b_vp == NULL,
2008 ("bp: %p still has vnode %p. qindex: %d",
2009 bp, bp->b_vp, qindex));
2010 KASSERT((bp->b_xflags & (BX_VNCLEAN|BX_VNDIRTY)) == 0,
2011 ("bp: %p still on a buffer list. xflags %X",
2020 KASSERT((bp->b_vflags & BV_INFREECNT) == 0,
2021 ("buf %p still counted as free?", bp));
2024 bp->b_blkno = bp->b_lblkno = 0;
2025 bp->b_offset = NOOFFSET;
2031 bp->b_dirtyoff = bp->b_dirtyend = 0;
2032 bp->b_bufobj = NULL;
2033 bp->b_pin_count = 0;
2034 bp->b_fsprivate1 = NULL;
2035 bp->b_fsprivate2 = NULL;
2036 bp->b_fsprivate3 = NULL;
2038 LIST_INIT(&bp->b_dep);
2041 * If we are defragging then free the buffer.
2044 bp->b_flags |= B_INVAL;
2052 * Notify any waiters for the buffer lock about
2053 * identity change by freeing the buffer.
2055 if (qindex == QUEUE_CLEAN && BUF_LOCKWAITERS(bp)) {
2056 bp->b_flags |= B_INVAL;
2063 * If we are overcomitted then recover the buffer and its
2064 * KVM space. This occurs in rare situations when multiple
2065 * processes are blocked in getnewbuf() or allocbuf().
2067 if (bufspace >= hibufspace)
2069 if (flushingbufs && bp->b_kvasize != 0) {
2070 bp->b_flags |= B_INVAL;
2075 if (bufspace < lobufspace)
2081 * If we exhausted our list, sleep as appropriate. We may have to
2082 * wakeup various daemons and write out some dirty buffers.
2084 * Generally we are sleeping due to insufficient buffer space.
2088 int flags, norunbuf;
2093 flags = VFS_BIO_NEED_BUFSPACE;
2095 } else if (bufspace >= hibufspace) {
2097 flags = VFS_BIO_NEED_BUFSPACE;
2100 flags = VFS_BIO_NEED_ANY;
2103 needsbuffer |= flags;
2104 mtx_unlock(&nblock);
2105 mtx_unlock(&bqlock);
2107 bd_speedup(); /* heeeelp */
2108 if (gbflags & GB_NOWAIT_BD)
2112 while (needsbuffer & flags) {
2113 if (vp != NULL && (td->td_pflags & TDP_BUFNEED) == 0) {
2114 mtx_unlock(&nblock);
2116 * getblk() is called with a vnode
2117 * locked, and some majority of the
2118 * dirty buffers may as well belong to
2119 * the vnode. Flushing the buffers
2120 * there would make a progress that
2121 * cannot be achieved by the
2122 * buf_daemon, that cannot lock the
2125 norunbuf = ~(TDP_BUFNEED | TDP_NORUNNINGBUF) |
2126 (td->td_pflags & TDP_NORUNNINGBUF);
2127 /* play bufdaemon */
2128 td->td_pflags |= TDP_BUFNEED | TDP_NORUNNINGBUF;
2129 fl = buf_do_flush(vp);
2130 td->td_pflags &= norunbuf;
2134 if ((needsbuffer & flags) == 0)
2137 if (msleep(&needsbuffer, &nblock,
2138 (PRIBIO + 4) | slpflag, waitmsg, slptimeo)) {
2139 mtx_unlock(&nblock);
2143 mtx_unlock(&nblock);
2146 * We finally have a valid bp. We aren't quite out of the
2147 * woods, we still have to reserve kva space. In order
2148 * to keep fragmentation sane we only allocate kva in
2151 maxsize = (maxsize + BKVAMASK) & ~BKVAMASK;
2153 if (maxsize != bp->b_kvasize) {
2154 vm_offset_t addr = 0;
2159 vm_map_lock(buffer_map);
2160 if (vm_map_findspace(buffer_map,
2161 vm_map_min(buffer_map), maxsize, &addr)) {
2163 * Buffer map is too fragmented.
2164 * We must defragment the map.
2166 atomic_add_int(&bufdefragcnt, 1);
2167 vm_map_unlock(buffer_map);
2169 bp->b_flags |= B_INVAL;
2173 rv = vm_map_insert(buffer_map, NULL, 0, addr,
2174 addr + maxsize, VM_PROT_ALL, VM_PROT_ALL,
2176 KASSERT(rv == KERN_SUCCESS,
2177 ("vm_map_insert(buffer_map) rv %d", rv));
2178 vm_map_unlock(buffer_map);
2179 bp->b_kvabase = (caddr_t)addr;
2180 bp->b_kvasize = maxsize;
2181 atomic_add_long(&bufspace, bp->b_kvasize);
2182 atomic_add_int(&bufreusecnt, 1);
2184 bp->b_saveaddr = bp->b_kvabase;
2185 bp->b_data = bp->b_saveaddr;
2193 * buffer flushing daemon. Buffers are normally flushed by the
2194 * update daemon but if it cannot keep up this process starts to
2195 * take the load in an attempt to prevent getnewbuf() from blocking.
2198 static struct kproc_desc buf_kp = {
2203 SYSINIT(bufdaemon, SI_SUB_KTHREAD_BUF, SI_ORDER_FIRST, kproc_start, &buf_kp);
2206 buf_do_flush(struct vnode *vp)
2210 flushed = flushbufqueues(vp, QUEUE_DIRTY, 0);
2213 * Could not find any buffers without rollback
2214 * dependencies, so just write the first one
2215 * in the hopes of eventually making progress.
2217 flushbufqueues(vp, QUEUE_DIRTY, 1);
2228 * This process needs to be suspended prior to shutdown sync.
2230 EVENTHANDLER_REGISTER(shutdown_pre_sync, kproc_shutdown, bufdaemonproc,
2234 * This process is allowed to take the buffer cache to the limit
2236 curthread->td_pflags |= TDP_NORUNNINGBUF | TDP_BUFNEED;
2240 mtx_unlock(&bdlock);
2242 kproc_suspend_check(bufdaemonproc);
2243 lodirtysave = lodirtybuffers;
2244 if (bd_speedupreq) {
2245 lodirtybuffers = numdirtybuffers / 2;
2249 * Do the flush. Limit the amount of in-transit I/O we
2250 * allow to build up, otherwise we would completely saturate
2251 * the I/O system. Wakeup any waiting processes before we
2252 * normally would so they can run in parallel with our drain.
2254 while (numdirtybuffers > lodirtybuffers) {
2255 if (buf_do_flush(NULL) == 0)
2257 kern_yield(PRI_USER);
2259 lodirtybuffers = lodirtysave;
2262 * Only clear bd_request if we have reached our low water
2263 * mark. The buf_daemon normally waits 1 second and
2264 * then incrementally flushes any dirty buffers that have
2265 * built up, within reason.
2267 * If we were unable to hit our low water mark and couldn't
2268 * find any flushable buffers, we sleep half a second.
2269 * Otherwise we loop immediately.
2272 if (numdirtybuffers <= lodirtybuffers) {
2274 * We reached our low water mark, reset the
2275 * request and sleep until we are needed again.
2276 * The sleep is just so the suspend code works.
2279 msleep(&bd_request, &bdlock, PVM, "psleep", hz);
2282 * We couldn't find any flushable dirty buffers but
2283 * still have too many dirty buffers, we
2284 * have to sleep and try again. (rare)
2286 msleep(&bd_request, &bdlock, PVM, "qsleep", hz / 10);
2294 * Try to flush a buffer in the dirty queue. We must be careful to
2295 * free up B_INVAL buffers instead of write them, which NFS is
2296 * particularly sensitive to.
2298 static int flushwithdeps = 0;
2299 SYSCTL_INT(_vfs, OID_AUTO, flushwithdeps, CTLFLAG_RW, &flushwithdeps,
2300 0, "Number of buffers flushed with dependecies that require rollbacks");
2303 flushbufqueues(struct vnode *lvp, int queue, int flushdeps)
2305 struct buf *sentinel;
2314 target = numdirtybuffers - lodirtybuffers;
2315 if (flushdeps && target > 2)
2318 target = flushbufqtarget;
2321 sentinel = malloc(sizeof(struct buf), M_TEMP, M_WAITOK | M_ZERO);
2322 sentinel->b_qindex = QUEUE_SENTINEL;
2324 TAILQ_INSERT_HEAD(&bufqueues[queue], sentinel, b_freelist);
2325 while (flushed != target) {
2326 bp = TAILQ_NEXT(sentinel, b_freelist);
2328 TAILQ_REMOVE(&bufqueues[queue], sentinel, b_freelist);
2329 TAILQ_INSERT_AFTER(&bufqueues[queue], bp, sentinel,
2334 * Skip sentinels inserted by other invocations of the
2335 * flushbufqueues(), taking care to not reorder them.
2337 if (bp->b_qindex == QUEUE_SENTINEL)
2340 * Only flush the buffers that belong to the
2341 * vnode locked by the curthread.
2343 if (lvp != NULL && bp->b_vp != lvp)
2345 if (BUF_LOCK(bp, LK_EXCLUSIVE | LK_NOWAIT, NULL) != 0)
2347 if (bp->b_pin_count > 0) {
2351 BO_LOCK(bp->b_bufobj);
2352 if ((bp->b_vflags & BV_BKGRDINPROG) != 0 ||
2353 (bp->b_flags & B_DELWRI) == 0) {
2354 BO_UNLOCK(bp->b_bufobj);
2358 BO_UNLOCK(bp->b_bufobj);
2359 if (bp->b_flags & B_INVAL) {
2361 mtx_unlock(&bqlock);
2364 numdirtywakeup((lodirtybuffers + hidirtybuffers) / 2);
2369 if (!LIST_EMPTY(&bp->b_dep) && buf_countdeps(bp, 0)) {
2370 if (flushdeps == 0) {
2378 * We must hold the lock on a vnode before writing
2379 * one of its buffers. Otherwise we may confuse, or
2380 * in the case of a snapshot vnode, deadlock the
2383 * The lock order here is the reverse of the normal
2384 * of vnode followed by buf lock. This is ok because
2385 * the NOWAIT will prevent deadlock.
2388 if (vn_start_write(vp, &mp, V_NOWAIT) != 0) {
2392 if (vn_lock(vp, LK_EXCLUSIVE | LK_NOWAIT | LK_CANRECURSE) == 0) {
2393 mtx_unlock(&bqlock);
2394 CTR3(KTR_BUF, "flushbufqueue(%p) vp %p flags %X",
2395 bp, bp->b_vp, bp->b_flags);
2396 if (curproc == bufdaemonproc)
2403 vn_finished_write(mp);
2405 flushwithdeps += hasdeps;
2409 * Sleeping on runningbufspace while holding
2410 * vnode lock leads to deadlock.
2412 if (curproc == bufdaemonproc)
2413 waitrunningbufspace();
2414 numdirtywakeup((lodirtybuffers + hidirtybuffers) / 2);
2418 vn_finished_write(mp);
2421 TAILQ_REMOVE(&bufqueues[queue], sentinel, b_freelist);
2422 mtx_unlock(&bqlock);
2423 free(sentinel, M_TEMP);
2428 * Check to see if a block is currently memory resident.
2431 incore(struct bufobj *bo, daddr_t blkno)
2436 bp = gbincore(bo, blkno);
2442 * Returns true if no I/O is needed to access the
2443 * associated VM object. This is like incore except
2444 * it also hunts around in the VM system for the data.
2448 inmem(struct vnode * vp, daddr_t blkno)
2451 vm_offset_t toff, tinc, size;
2455 ASSERT_VOP_LOCKED(vp, "inmem");
2457 if (incore(&vp->v_bufobj, blkno))
2459 if (vp->v_mount == NULL)
2466 if (size > vp->v_mount->mnt_stat.f_iosize)
2467 size = vp->v_mount->mnt_stat.f_iosize;
2468 off = (vm_ooffset_t)blkno * (vm_ooffset_t)vp->v_mount->mnt_stat.f_iosize;
2470 VM_OBJECT_LOCK(obj);
2471 for (toff = 0; toff < vp->v_mount->mnt_stat.f_iosize; toff += tinc) {
2472 m = vm_page_lookup(obj, OFF_TO_IDX(off + toff));
2476 if (tinc > PAGE_SIZE - ((toff + off) & PAGE_MASK))
2477 tinc = PAGE_SIZE - ((toff + off) & PAGE_MASK);
2478 if (vm_page_is_valid(m,
2479 (vm_offset_t) ((toff + off) & PAGE_MASK), tinc) == 0)
2482 VM_OBJECT_UNLOCK(obj);
2486 VM_OBJECT_UNLOCK(obj);
2491 * Set the dirty range for a buffer based on the status of the dirty
2492 * bits in the pages comprising the buffer. The range is limited
2493 * to the size of the buffer.
2495 * Tell the VM system that the pages associated with this buffer
2496 * are clean. This is used for delayed writes where the data is
2497 * going to go to disk eventually without additional VM intevention.
2499 * Note that while we only really need to clean through to b_bcount, we
2500 * just go ahead and clean through to b_bufsize.
2503 vfs_clean_pages_dirty_buf(struct buf *bp)
2505 vm_ooffset_t foff, noff, eoff;
2509 if ((bp->b_flags & B_VMIO) == 0 || bp->b_bufsize == 0)
2512 foff = bp->b_offset;
2513 KASSERT(bp->b_offset != NOOFFSET,
2514 ("vfs_clean_pages_dirty_buf: no buffer offset"));
2516 VM_OBJECT_LOCK(bp->b_bufobj->bo_object);
2517 vfs_drain_busy_pages(bp);
2518 vfs_setdirty_locked_object(bp);
2519 for (i = 0; i < bp->b_npages; i++) {
2520 noff = (foff + PAGE_SIZE) & ~(off_t)PAGE_MASK;
2522 if (eoff > bp->b_offset + bp->b_bufsize)
2523 eoff = bp->b_offset + bp->b_bufsize;
2525 vfs_page_set_validclean(bp, foff, m);
2526 /* vm_page_clear_dirty(m, foff & PAGE_MASK, eoff - foff); */
2529 VM_OBJECT_UNLOCK(bp->b_bufobj->bo_object);
2533 vfs_setdirty_locked_object(struct buf *bp)
2538 object = bp->b_bufobj->bo_object;
2539 VM_OBJECT_LOCK_ASSERT(object, MA_OWNED);
2542 * We qualify the scan for modified pages on whether the
2543 * object has been flushed yet.
2545 if ((object->flags & OBJ_MIGHTBEDIRTY) != 0) {
2546 vm_offset_t boffset;
2547 vm_offset_t eoffset;
2550 * test the pages to see if they have been modified directly
2551 * by users through the VM system.
2553 for (i = 0; i < bp->b_npages; i++)
2554 vm_page_test_dirty(bp->b_pages[i]);
2557 * Calculate the encompassing dirty range, boffset and eoffset,
2558 * (eoffset - boffset) bytes.
2561 for (i = 0; i < bp->b_npages; i++) {
2562 if (bp->b_pages[i]->dirty)
2565 boffset = (i << PAGE_SHIFT) - (bp->b_offset & PAGE_MASK);
2567 for (i = bp->b_npages - 1; i >= 0; --i) {
2568 if (bp->b_pages[i]->dirty) {
2572 eoffset = ((i + 1) << PAGE_SHIFT) - (bp->b_offset & PAGE_MASK);
2575 * Fit it to the buffer.
2578 if (eoffset > bp->b_bcount)
2579 eoffset = bp->b_bcount;
2582 * If we have a good dirty range, merge with the existing
2586 if (boffset < eoffset) {
2587 if (bp->b_dirtyoff > boffset)
2588 bp->b_dirtyoff = boffset;
2589 if (bp->b_dirtyend < eoffset)
2590 bp->b_dirtyend = eoffset;
2598 * Get a block given a specified block and offset into a file/device.
2599 * The buffers B_DONE bit will be cleared on return, making it almost
2600 * ready for an I/O initiation. B_INVAL may or may not be set on
2601 * return. The caller should clear B_INVAL prior to initiating a
2604 * For a non-VMIO buffer, B_CACHE is set to the opposite of B_INVAL for
2605 * an existing buffer.
2607 * For a VMIO buffer, B_CACHE is modified according to the backing VM.
2608 * If getblk()ing a previously 0-sized invalid buffer, B_CACHE is set
2609 * and then cleared based on the backing VM. If the previous buffer is
2610 * non-0-sized but invalid, B_CACHE will be cleared.
2612 * If getblk() must create a new buffer, the new buffer is returned with
2613 * both B_INVAL and B_CACHE clear unless it is a VMIO buffer, in which
2614 * case it is returned with B_INVAL clear and B_CACHE set based on the
2617 * getblk() also forces a bwrite() for any B_DELWRI buffer whos
2618 * B_CACHE bit is clear.
2620 * What this means, basically, is that the caller should use B_CACHE to
2621 * determine whether the buffer is fully valid or not and should clear
2622 * B_INVAL prior to issuing a read. If the caller intends to validate
2623 * the buffer by loading its data area with something, the caller needs
2624 * to clear B_INVAL. If the caller does this without issuing an I/O,
2625 * the caller should set B_CACHE ( as an optimization ), else the caller
2626 * should issue the I/O and biodone() will set B_CACHE if the I/O was
2627 * a write attempt or if it was a successfull read. If the caller
2628 * intends to issue a READ, the caller must clear B_INVAL and BIO_ERROR
2629 * prior to issuing the READ. biodone() will *not* clear B_INVAL.
2632 getblk(struct vnode * vp, daddr_t blkno, int size, int slpflag, int slptimeo,
2639 CTR3(KTR_BUF, "getblk(%p, %ld, %d)", vp, (long)blkno, size);
2640 ASSERT_VOP_LOCKED(vp, "getblk");
2641 if (size > MAXBSIZE)
2642 panic("getblk: size(%d) > MAXBSIZE(%d)\n", size, MAXBSIZE);
2647 * Block if we are low on buffers. Certain processes are allowed
2648 * to completely exhaust the buffer cache.
2650 * If this check ever becomes a bottleneck it may be better to
2651 * move it into the else, when gbincore() fails. At the moment
2652 * it isn't a problem.
2654 if (numfreebuffers == 0) {
2655 if (TD_IS_IDLETHREAD(curthread))
2658 needsbuffer |= VFS_BIO_NEED_ANY;
2659 mtx_unlock(&nblock);
2663 bp = gbincore(bo, blkno);
2667 * Buffer is in-core. If the buffer is not busy nor managed,
2668 * it must be on a queue.
2670 lockflags = LK_EXCLUSIVE | LK_SLEEPFAIL | LK_INTERLOCK;
2672 if (flags & GB_LOCK_NOWAIT)
2673 lockflags |= LK_NOWAIT;
2675 error = BUF_TIMELOCK(bp, lockflags,
2676 BO_MTX(bo), "getblk", slpflag, slptimeo);
2679 * If we slept and got the lock we have to restart in case
2680 * the buffer changed identities.
2682 if (error == ENOLCK)
2684 /* We timed out or were interrupted. */
2687 /* If recursed, assume caller knows the rules. */
2688 else if (BUF_LOCKRECURSED(bp))
2692 * The buffer is locked. B_CACHE is cleared if the buffer is
2693 * invalid. Otherwise, for a non-VMIO buffer, B_CACHE is set
2694 * and for a VMIO buffer B_CACHE is adjusted according to the
2697 if (bp->b_flags & B_INVAL)
2698 bp->b_flags &= ~B_CACHE;
2699 else if ((bp->b_flags & (B_VMIO | B_INVAL)) == 0)
2700 bp->b_flags |= B_CACHE;
2701 if (bp->b_flags & B_MANAGED)
2702 MPASS(bp->b_qindex == QUEUE_NONE);
2710 * check for size inconsistancies for non-VMIO case.
2713 if (bp->b_bcount != size) {
2714 if ((bp->b_flags & B_VMIO) == 0 ||
2715 (size > bp->b_kvasize)) {
2716 if (bp->b_flags & B_DELWRI) {
2718 * If buffer is pinned and caller does
2719 * not want sleep waiting for it to be
2720 * unpinned, bail out
2722 if (bp->b_pin_count > 0) {
2723 if (flags & GB_LOCK_NOWAIT) {
2730 bp->b_flags |= B_NOCACHE;
2733 if (LIST_EMPTY(&bp->b_dep)) {
2734 bp->b_flags |= B_RELBUF;
2737 bp->b_flags |= B_NOCACHE;
2746 * If the size is inconsistant in the VMIO case, we can resize
2747 * the buffer. This might lead to B_CACHE getting set or
2748 * cleared. If the size has not changed, B_CACHE remains
2749 * unchanged from its previous state.
2752 if (bp->b_bcount != size)
2755 KASSERT(bp->b_offset != NOOFFSET,
2756 ("getblk: no buffer offset"));
2759 * A buffer with B_DELWRI set and B_CACHE clear must
2760 * be committed before we can return the buffer in
2761 * order to prevent the caller from issuing a read
2762 * ( due to B_CACHE not being set ) and overwriting
2765 * Most callers, including NFS and FFS, need this to
2766 * operate properly either because they assume they
2767 * can issue a read if B_CACHE is not set, or because
2768 * ( for example ) an uncached B_DELWRI might loop due
2769 * to softupdates re-dirtying the buffer. In the latter
2770 * case, B_CACHE is set after the first write completes,
2771 * preventing further loops.
2772 * NOTE! b*write() sets B_CACHE. If we cleared B_CACHE
2773 * above while extending the buffer, we cannot allow the
2774 * buffer to remain with B_CACHE set after the write
2775 * completes or it will represent a corrupt state. To
2776 * deal with this we set B_NOCACHE to scrap the buffer
2779 * We might be able to do something fancy, like setting
2780 * B_CACHE in bwrite() except if B_DELWRI is already set,
2781 * so the below call doesn't set B_CACHE, but that gets real
2782 * confusing. This is much easier.
2785 if ((bp->b_flags & (B_CACHE|B_DELWRI)) == B_DELWRI) {
2786 bp->b_flags |= B_NOCACHE;
2790 bp->b_flags &= ~B_DONE;
2792 int bsize, maxsize, vmio;
2796 * Buffer is not in-core, create new buffer. The buffer
2797 * returned by getnewbuf() is locked. Note that the returned
2798 * buffer is also considered valid (not marked B_INVAL).
2802 * If the user does not want us to create the buffer, bail out
2805 if (flags & GB_NOCREAT)
2807 bsize = vn_isdisk(vp, NULL) ? DEV_BSIZE : bo->bo_bsize;
2808 offset = blkno * bsize;
2809 vmio = vp->v_object != NULL;
2810 maxsize = vmio ? size + (offset & PAGE_MASK) : size;
2811 maxsize = imax(maxsize, bsize);
2813 bp = getnewbuf(vp, slpflag, slptimeo, size, maxsize, flags);
2815 if (slpflag || slptimeo)
2821 * This code is used to make sure that a buffer is not
2822 * created while the getnewbuf routine is blocked.
2823 * This can be a problem whether the vnode is locked or not.
2824 * If the buffer is created out from under us, we have to
2825 * throw away the one we just created.
2827 * Note: this must occur before we associate the buffer
2828 * with the vp especially considering limitations in
2829 * the splay tree implementation when dealing with duplicate
2833 if (gbincore(bo, blkno)) {
2835 bp->b_flags |= B_INVAL;
2841 * Insert the buffer into the hash, so that it can
2842 * be found by incore.
2844 bp->b_blkno = bp->b_lblkno = blkno;
2845 bp->b_offset = offset;
2850 * set B_VMIO bit. allocbuf() the buffer bigger. Since the
2851 * buffer size starts out as 0, B_CACHE will be set by
2852 * allocbuf() for the VMIO case prior to it testing the
2853 * backing store for validity.
2857 bp->b_flags |= B_VMIO;
2858 KASSERT(vp->v_object == bp->b_bufobj->bo_object,
2859 ("ARGH! different b_bufobj->bo_object %p %p %p\n",
2860 bp, vp->v_object, bp->b_bufobj->bo_object));
2862 bp->b_flags &= ~B_VMIO;
2863 KASSERT(bp->b_bufobj->bo_object == NULL,
2864 ("ARGH! has b_bufobj->bo_object %p %p\n",
2865 bp, bp->b_bufobj->bo_object));
2869 bp->b_flags &= ~B_DONE;
2871 CTR4(KTR_BUF, "getblk(%p, %ld, %d) = %p", vp, (long)blkno, size, bp);
2872 BUF_ASSERT_HELD(bp);
2874 KASSERT(bp->b_bufobj == bo,
2875 ("bp %p wrong b_bufobj %p should be %p", bp, bp->b_bufobj, bo));
2880 * Get an empty, disassociated buffer of given size. The buffer is initially
2884 geteblk(int size, int flags)
2889 maxsize = (size + BKVAMASK) & ~BKVAMASK;
2890 while ((bp = getnewbuf(NULL, 0, 0, size, maxsize, flags)) == NULL) {
2891 if ((flags & GB_NOWAIT_BD) &&
2892 (curthread->td_pflags & TDP_BUFNEED) != 0)
2896 bp->b_flags |= B_INVAL; /* b_dep cleared by getnewbuf() */
2897 BUF_ASSERT_HELD(bp);
2903 * This code constitutes the buffer memory from either anonymous system
2904 * memory (in the case of non-VMIO operations) or from an associated
2905 * VM object (in the case of VMIO operations). This code is able to
2906 * resize a buffer up or down.
2908 * Note that this code is tricky, and has many complications to resolve
2909 * deadlock or inconsistant data situations. Tread lightly!!!
2910 * There are B_CACHE and B_DELWRI interactions that must be dealt with by
2911 * the caller. Calling this code willy nilly can result in the loss of data.
2913 * allocbuf() only adjusts B_CACHE for VMIO buffers. getblk() deals with
2914 * B_CACHE for the non-VMIO case.
2918 allocbuf(struct buf *bp, int size)
2920 int newbsize, mbsize;
2923 BUF_ASSERT_HELD(bp);
2925 if (bp->b_kvasize < size)
2926 panic("allocbuf: buffer too small");
2928 if ((bp->b_flags & B_VMIO) == 0) {
2932 * Just get anonymous memory from the kernel. Don't
2933 * mess with B_CACHE.
2935 mbsize = (size + DEV_BSIZE - 1) & ~(DEV_BSIZE - 1);
2936 if (bp->b_flags & B_MALLOC)
2939 newbsize = round_page(size);
2941 if (newbsize < bp->b_bufsize) {
2943 * malloced buffers are not shrunk
2945 if (bp->b_flags & B_MALLOC) {
2947 bp->b_bcount = size;
2949 free(bp->b_data, M_BIOBUF);
2950 if (bp->b_bufsize) {
2951 atomic_subtract_long(
2957 bp->b_saveaddr = bp->b_kvabase;
2958 bp->b_data = bp->b_saveaddr;
2960 bp->b_flags &= ~B_MALLOC;
2964 vm_hold_free_pages(bp, newbsize);
2965 } else if (newbsize > bp->b_bufsize) {
2967 * We only use malloced memory on the first allocation.
2968 * and revert to page-allocated memory when the buffer
2972 * There is a potential smp race here that could lead
2973 * to bufmallocspace slightly passing the max. It
2974 * is probably extremely rare and not worth worrying
2977 if ( (bufmallocspace < maxbufmallocspace) &&
2978 (bp->b_bufsize == 0) &&
2979 (mbsize <= PAGE_SIZE/2)) {
2981 bp->b_data = malloc(mbsize, M_BIOBUF, M_WAITOK);
2982 bp->b_bufsize = mbsize;
2983 bp->b_bcount = size;
2984 bp->b_flags |= B_MALLOC;
2985 atomic_add_long(&bufmallocspace, mbsize);
2991 * If the buffer is growing on its other-than-first allocation,
2992 * then we revert to the page-allocation scheme.
2994 if (bp->b_flags & B_MALLOC) {
2995 origbuf = bp->b_data;
2996 origbufsize = bp->b_bufsize;
2997 bp->b_data = bp->b_kvabase;
2998 if (bp->b_bufsize) {
2999 atomic_subtract_long(&bufmallocspace,
3004 bp->b_flags &= ~B_MALLOC;
3005 newbsize = round_page(newbsize);
3009 (vm_offset_t) bp->b_data + bp->b_bufsize,
3010 (vm_offset_t) bp->b_data + newbsize);
3012 bcopy(origbuf, bp->b_data, origbufsize);
3013 free(origbuf, M_BIOBUF);
3019 newbsize = (size + DEV_BSIZE - 1) & ~(DEV_BSIZE - 1);
3020 desiredpages = (size == 0) ? 0 :
3021 num_pages((bp->b_offset & PAGE_MASK) + newbsize);
3023 if (bp->b_flags & B_MALLOC)
3024 panic("allocbuf: VMIO buffer can't be malloced");
3026 * Set B_CACHE initially if buffer is 0 length or will become
3029 if (size == 0 || bp->b_bufsize == 0)
3030 bp->b_flags |= B_CACHE;
3032 if (newbsize < bp->b_bufsize) {
3034 * DEV_BSIZE aligned new buffer size is less then the
3035 * DEV_BSIZE aligned existing buffer size. Figure out
3036 * if we have to remove any pages.
3038 if (desiredpages < bp->b_npages) {
3041 pmap_qremove((vm_offset_t)trunc_page(
3042 (vm_offset_t)bp->b_data) +
3043 (desiredpages << PAGE_SHIFT),
3044 (bp->b_npages - desiredpages));
3045 VM_OBJECT_LOCK(bp->b_bufobj->bo_object);
3046 for (i = desiredpages; i < bp->b_npages; i++) {
3048 * the page is not freed here -- it
3049 * is the responsibility of
3050 * vnode_pager_setsize
3053 KASSERT(m != bogus_page,
3054 ("allocbuf: bogus page found"));
3055 while (vm_page_sleep_if_busy(m, TRUE,
3059 bp->b_pages[i] = NULL;
3061 vm_page_unwire(m, 0);
3064 VM_OBJECT_UNLOCK(bp->b_bufobj->bo_object);
3065 bp->b_npages = desiredpages;
3067 } else if (size > bp->b_bcount) {
3069 * We are growing the buffer, possibly in a
3070 * byte-granular fashion.
3077 * Step 1, bring in the VM pages from the object,
3078 * allocating them if necessary. We must clear
3079 * B_CACHE if these pages are not valid for the
3080 * range covered by the buffer.
3083 obj = bp->b_bufobj->bo_object;
3085 VM_OBJECT_LOCK(obj);
3086 while (bp->b_npages < desiredpages) {
3090 * We must allocate system pages since blocking
3091 * here could interfere with paging I/O, no
3092 * matter which process we are.
3094 * We can only test VPO_BUSY here. Blocking on
3095 * m->busy might lead to a deadlock:
3096 * vm_fault->getpages->cluster_read->allocbuf
3097 * Thus, we specify VM_ALLOC_IGN_SBUSY.
3099 m = vm_page_grab(obj, OFF_TO_IDX(bp->b_offset) +
3100 bp->b_npages, VM_ALLOC_NOBUSY |
3101 VM_ALLOC_SYSTEM | VM_ALLOC_WIRED |
3102 VM_ALLOC_RETRY | VM_ALLOC_IGN_SBUSY |
3103 VM_ALLOC_COUNT(desiredpages - bp->b_npages));
3105 bp->b_flags &= ~B_CACHE;
3106 bp->b_pages[bp->b_npages] = m;
3111 * Step 2. We've loaded the pages into the buffer,
3112 * we have to figure out if we can still have B_CACHE
3113 * set. Note that B_CACHE is set according to the
3114 * byte-granular range ( bcount and size ), new the
3115 * aligned range ( newbsize ).
3117 * The VM test is against m->valid, which is DEV_BSIZE
3118 * aligned. Needless to say, the validity of the data
3119 * needs to also be DEV_BSIZE aligned. Note that this
3120 * fails with NFS if the server or some other client
3121 * extends the file's EOF. If our buffer is resized,
3122 * B_CACHE may remain set! XXX
3125 toff = bp->b_bcount;
3126 tinc = PAGE_SIZE - ((bp->b_offset + toff) & PAGE_MASK);
3128 while ((bp->b_flags & B_CACHE) && toff < size) {
3131 if (tinc > (size - toff))
3134 pi = ((bp->b_offset & PAGE_MASK) + toff) >>
3147 VM_OBJECT_UNLOCK(obj);
3150 * Step 3, fixup the KVM pmap. Remember that
3151 * bp->b_data is relative to bp->b_offset, but
3152 * bp->b_offset may be offset into the first page.
3155 bp->b_data = (caddr_t)
3156 trunc_page((vm_offset_t)bp->b_data);
3158 (vm_offset_t)bp->b_data,
3163 bp->b_data = (caddr_t)((vm_offset_t)bp->b_data |
3164 (vm_offset_t)(bp->b_offset & PAGE_MASK));
3167 if (newbsize < bp->b_bufsize)
3169 bp->b_bufsize = newbsize; /* actual buffer allocation */
3170 bp->b_bcount = size; /* requested buffer size */
3175 biodone(struct bio *bp)
3178 void (*done)(struct bio *);
3180 mtxp = mtx_pool_find(mtxpool_sleep, bp);
3182 bp->bio_flags |= BIO_DONE;
3183 done = bp->bio_done;
3192 * Wait for a BIO to finish.
3194 * XXX: resort to a timeout for now. The optimal locking (if any) for this
3195 * case is not yet clear.
3198 biowait(struct bio *bp, const char *wchan)
3202 mtxp = mtx_pool_find(mtxpool_sleep, bp);
3204 while ((bp->bio_flags & BIO_DONE) == 0)
3205 msleep(bp, mtxp, PRIBIO, wchan, hz / 10);
3207 if (bp->bio_error != 0)
3208 return (bp->bio_error);
3209 if (!(bp->bio_flags & BIO_ERROR))
3215 biofinish(struct bio *bp, struct devstat *stat, int error)
3219 bp->bio_error = error;
3220 bp->bio_flags |= BIO_ERROR;
3223 devstat_end_transaction_bio(stat, bp);
3230 * Wait for buffer I/O completion, returning error status. The buffer
3231 * is left locked and B_DONE on return. B_EINTR is converted into an EINTR
3232 * error and cleared.
3235 bufwait(struct buf *bp)
3237 if (bp->b_iocmd == BIO_READ)
3238 bwait(bp, PRIBIO, "biord");
3240 bwait(bp, PRIBIO, "biowr");
3241 if (bp->b_flags & B_EINTR) {
3242 bp->b_flags &= ~B_EINTR;
3245 if (bp->b_ioflags & BIO_ERROR) {
3246 return (bp->b_error ? bp->b_error : EIO);
3253 * Call back function from struct bio back up to struct buf.
3256 bufdonebio(struct bio *bip)
3260 bp = bip->bio_caller2;
3261 bp->b_resid = bp->b_bcount - bip->bio_completed;
3262 bp->b_resid = bip->bio_resid; /* XXX: remove */
3263 bp->b_ioflags = bip->bio_flags;
3264 bp->b_error = bip->bio_error;
3266 bp->b_ioflags |= BIO_ERROR;
3272 dev_strategy(struct cdev *dev, struct buf *bp)
3278 if ((!bp->b_iocmd) || (bp->b_iocmd & (bp->b_iocmd - 1)))
3279 panic("b_iocmd botch");
3284 /* Try again later */
3285 tsleep(&bp, PRIBIO, "dev_strat", hz/10);
3287 bip->bio_cmd = bp->b_iocmd;
3288 bip->bio_offset = bp->b_iooffset;
3289 bip->bio_length = bp->b_bcount;
3290 bip->bio_bcount = bp->b_bcount; /* XXX: remove */
3291 bip->bio_data = bp->b_data;
3292 bip->bio_done = bufdonebio;
3293 bip->bio_caller2 = bp;
3295 KASSERT(dev->si_refcount > 0,
3296 ("dev_strategy on un-referenced struct cdev *(%s)",
3298 csw = dev_refthread(dev, &ref);
3301 bp->b_error = ENXIO;
3302 bp->b_ioflags = BIO_ERROR;
3306 (*csw->d_strategy)(bip);
3307 dev_relthread(dev, ref);
3313 * Finish I/O on a buffer, optionally calling a completion function.
3314 * This is usually called from an interrupt so process blocking is
3317 * biodone is also responsible for setting B_CACHE in a B_VMIO bp.
3318 * In a non-VMIO bp, B_CACHE will be set on the next getblk()
3319 * assuming B_INVAL is clear.
3321 * For the VMIO case, we set B_CACHE if the op was a read and no
3322 * read error occured, or if the op was a write. B_CACHE is never
3323 * set if the buffer is invalid or otherwise uncacheable.
3325 * biodone does not mess with B_INVAL, allowing the I/O routine or the
3326 * initiator to leave B_INVAL set to brelse the buffer out of existance
3327 * in the biodone routine.
3330 bufdone(struct buf *bp)
3332 struct bufobj *dropobj;
3333 void (*biodone)(struct buf *);
3335 CTR3(KTR_BUF, "bufdone(%p) vp %p flags %X", bp, bp->b_vp, bp->b_flags);
3338 KASSERT(!(bp->b_flags & B_DONE), ("biodone: bp %p already done", bp));
3339 BUF_ASSERT_HELD(bp);
3341 runningbufwakeup(bp);
3342 if (bp->b_iocmd == BIO_WRITE)
3343 dropobj = bp->b_bufobj;
3344 /* call optional completion function if requested */
3345 if (bp->b_iodone != NULL) {
3346 biodone = bp->b_iodone;
3347 bp->b_iodone = NULL;
3350 bufobj_wdrop(dropobj);
3357 bufobj_wdrop(dropobj);
3361 bufdone_finish(struct buf *bp)
3363 BUF_ASSERT_HELD(bp);
3365 if (!LIST_EMPTY(&bp->b_dep))
3368 if (bp->b_flags & B_VMIO) {
3373 int bogus, i, iosize;
3375 obj = bp->b_bufobj->bo_object;
3376 KASSERT(obj->paging_in_progress >= bp->b_npages,
3377 ("biodone_finish: paging in progress(%d) < b_npages(%d)",
3378 obj->paging_in_progress, bp->b_npages));
3381 KASSERT(vp->v_holdcnt > 0,
3382 ("biodone_finish: vnode %p has zero hold count", vp));
3383 KASSERT(vp->v_object != NULL,
3384 ("biodone_finish: vnode %p has no vm_object", vp));
3386 foff = bp->b_offset;
3387 KASSERT(bp->b_offset != NOOFFSET,
3388 ("biodone_finish: bp %p has no buffer offset", bp));
3391 * Set B_CACHE if the op was a normal read and no error
3392 * occured. B_CACHE is set for writes in the b*write()
3395 iosize = bp->b_bcount - bp->b_resid;
3396 if (bp->b_iocmd == BIO_READ &&
3397 !(bp->b_flags & (B_INVAL|B_NOCACHE)) &&
3398 !(bp->b_ioflags & BIO_ERROR)) {
3399 bp->b_flags |= B_CACHE;
3402 VM_OBJECT_LOCK(obj);
3403 for (i = 0; i < bp->b_npages; i++) {
3407 resid = ((foff + PAGE_SIZE) & ~(off_t)PAGE_MASK) - foff;
3412 * cleanup bogus pages, restoring the originals
3415 if (m == bogus_page) {
3416 bogus = bogusflag = 1;
3417 m = vm_page_lookup(obj, OFF_TO_IDX(foff));
3419 panic("biodone: page disappeared!");
3422 KASSERT(OFF_TO_IDX(foff) == m->pindex,
3423 ("biodone_finish: foff(%jd)/pindex(%ju) mismatch",
3424 (intmax_t)foff, (uintmax_t)m->pindex));
3427 * In the write case, the valid and clean bits are
3428 * already changed correctly ( see bdwrite() ), so we
3429 * only need to do this here in the read case.
3431 if ((bp->b_iocmd == BIO_READ) && !bogusflag && resid > 0) {
3432 KASSERT((m->dirty & vm_page_bits(foff &
3433 PAGE_MASK, resid)) == 0, ("bufdone_finish:"
3434 " page %p has unexpected dirty bits", m));
3435 vfs_page_set_valid(bp, foff, m);
3438 vm_page_io_finish(m);
3439 vm_object_pip_subtract(obj, 1);
3440 foff = (foff + PAGE_SIZE) & ~(off_t)PAGE_MASK;
3443 vm_object_pip_wakeupn(obj, 0);
3444 VM_OBJECT_UNLOCK(obj);
3446 pmap_qenter(trunc_page((vm_offset_t)bp->b_data),
3447 bp->b_pages, bp->b_npages);
3451 * For asynchronous completions, release the buffer now. The brelse
3452 * will do a wakeup there if necessary - so no need to do a wakeup
3453 * here in the async case. The sync case always needs to do a wakeup.
3456 if (bp->b_flags & B_ASYNC) {
3457 if ((bp->b_flags & (B_NOCACHE | B_INVAL | B_RELBUF)) || (bp->b_ioflags & BIO_ERROR))
3466 * This routine is called in lieu of iodone in the case of
3467 * incomplete I/O. This keeps the busy status for pages
3471 vfs_unbusy_pages(struct buf *bp)
3477 runningbufwakeup(bp);
3478 if (!(bp->b_flags & B_VMIO))
3481 obj = bp->b_bufobj->bo_object;
3482 VM_OBJECT_LOCK(obj);
3483 for (i = 0; i < bp->b_npages; i++) {
3485 if (m == bogus_page) {
3486 m = vm_page_lookup(obj, OFF_TO_IDX(bp->b_offset) + i);
3488 panic("vfs_unbusy_pages: page missing\n");
3490 pmap_qenter(trunc_page((vm_offset_t)bp->b_data),
3491 bp->b_pages, bp->b_npages);
3493 vm_object_pip_subtract(obj, 1);
3494 vm_page_io_finish(m);
3496 vm_object_pip_wakeupn(obj, 0);
3497 VM_OBJECT_UNLOCK(obj);
3501 * vfs_page_set_valid:
3503 * Set the valid bits in a page based on the supplied offset. The
3504 * range is restricted to the buffer's size.
3506 * This routine is typically called after a read completes.
3509 vfs_page_set_valid(struct buf *bp, vm_ooffset_t off, vm_page_t m)
3514 * Compute the end offset, eoff, such that [off, eoff) does not span a
3515 * page boundary and eoff is not greater than the end of the buffer.
3516 * The end of the buffer, in this case, is our file EOF, not the
3517 * allocation size of the buffer.
3519 eoff = (off + PAGE_SIZE) & ~(vm_ooffset_t)PAGE_MASK;
3520 if (eoff > bp->b_offset + bp->b_bcount)
3521 eoff = bp->b_offset + bp->b_bcount;
3524 * Set valid range. This is typically the entire buffer and thus the
3528 vm_page_set_valid_range(m, off & PAGE_MASK, eoff - off);
3532 * vfs_page_set_validclean:
3534 * Set the valid bits and clear the dirty bits in a page based on the
3535 * supplied offset. The range is restricted to the buffer's size.
3538 vfs_page_set_validclean(struct buf *bp, vm_ooffset_t off, vm_page_t m)
3540 vm_ooffset_t soff, eoff;
3543 * Start and end offsets in buffer. eoff - soff may not cross a
3544 * page boundry or cross the end of the buffer. The end of the
3545 * buffer, in this case, is our file EOF, not the allocation size
3549 eoff = (off + PAGE_SIZE) & ~(off_t)PAGE_MASK;
3550 if (eoff > bp->b_offset + bp->b_bcount)
3551 eoff = bp->b_offset + bp->b_bcount;
3554 * Set valid range. This is typically the entire buffer and thus the
3558 vm_page_set_validclean(
3560 (vm_offset_t) (soff & PAGE_MASK),
3561 (vm_offset_t) (eoff - soff)
3567 * Ensure that all buffer pages are not busied by VPO_BUSY flag. If
3568 * any page is busy, drain the flag.
3571 vfs_drain_busy_pages(struct buf *bp)
3576 VM_OBJECT_LOCK_ASSERT(bp->b_bufobj->bo_object, MA_OWNED);
3578 for (i = 0; i < bp->b_npages; i++) {
3580 if ((m->oflags & VPO_BUSY) != 0) {
3581 for (; last_busied < i; last_busied++)
3582 vm_page_busy(bp->b_pages[last_busied]);
3583 while ((m->oflags & VPO_BUSY) != 0)
3584 vm_page_sleep(m, "vbpage");
3587 for (i = 0; i < last_busied; i++)
3588 vm_page_wakeup(bp->b_pages[i]);
3592 * This routine is called before a device strategy routine.
3593 * It is used to tell the VM system that paging I/O is in
3594 * progress, and treat the pages associated with the buffer
3595 * almost as being VPO_BUSY. Also the object paging_in_progress
3596 * flag is handled to make sure that the object doesn't become
3599 * Since I/O has not been initiated yet, certain buffer flags
3600 * such as BIO_ERROR or B_INVAL may be in an inconsistant state
3601 * and should be ignored.
3604 vfs_busy_pages(struct buf *bp, int clear_modify)
3611 if (!(bp->b_flags & B_VMIO))
3614 obj = bp->b_bufobj->bo_object;
3615 foff = bp->b_offset;
3616 KASSERT(bp->b_offset != NOOFFSET,
3617 ("vfs_busy_pages: no buffer offset"));
3618 VM_OBJECT_LOCK(obj);
3619 vfs_drain_busy_pages(bp);
3620 if (bp->b_bufsize != 0)
3621 vfs_setdirty_locked_object(bp);
3623 for (i = 0; i < bp->b_npages; i++) {
3626 if ((bp->b_flags & B_CLUSTER) == 0) {
3627 vm_object_pip_add(obj, 1);
3628 vm_page_io_start(m);
3631 * When readying a buffer for a read ( i.e
3632 * clear_modify == 0 ), it is important to do
3633 * bogus_page replacement for valid pages in
3634 * partially instantiated buffers. Partially
3635 * instantiated buffers can, in turn, occur when
3636 * reconstituting a buffer from its VM backing store
3637 * base. We only have to do this if B_CACHE is
3638 * clear ( which causes the I/O to occur in the
3639 * first place ). The replacement prevents the read
3640 * I/O from overwriting potentially dirty VM-backed
3641 * pages. XXX bogus page replacement is, uh, bogus.
3642 * It may not work properly with small-block devices.
3643 * We need to find a better way.
3646 pmap_remove_write(m);
3647 vfs_page_set_validclean(bp, foff, m);
3648 } else if (m->valid == VM_PAGE_BITS_ALL &&
3649 (bp->b_flags & B_CACHE) == 0) {
3650 bp->b_pages[i] = bogus_page;
3653 foff = (foff + PAGE_SIZE) & ~(off_t)PAGE_MASK;
3655 VM_OBJECT_UNLOCK(obj);
3657 pmap_qenter(trunc_page((vm_offset_t)bp->b_data),
3658 bp->b_pages, bp->b_npages);
3662 * vfs_bio_set_valid:
3664 * Set the range within the buffer to valid. The range is
3665 * relative to the beginning of the buffer, b_offset. Note that
3666 * b_offset itself may be offset from the beginning of the first
3670 vfs_bio_set_valid(struct buf *bp, int base, int size)
3675 if (!(bp->b_flags & B_VMIO))
3679 * Fixup base to be relative to beginning of first page.
3680 * Set initial n to be the maximum number of bytes in the
3681 * first page that can be validated.
3683 base += (bp->b_offset & PAGE_MASK);
3684 n = PAGE_SIZE - (base & PAGE_MASK);
3686 VM_OBJECT_LOCK(bp->b_bufobj->bo_object);
3687 for (i = base / PAGE_SIZE; size > 0 && i < bp->b_npages; ++i) {
3691 vm_page_set_valid_range(m, base & PAGE_MASK, n);
3696 VM_OBJECT_UNLOCK(bp->b_bufobj->bo_object);
3702 * If the specified buffer is a non-VMIO buffer, clear the entire
3703 * buffer. If the specified buffer is a VMIO buffer, clear and
3704 * validate only the previously invalid portions of the buffer.
3705 * This routine essentially fakes an I/O, so we need to clear
3706 * BIO_ERROR and B_INVAL.
3708 * Note that while we only theoretically need to clear through b_bcount,
3709 * we go ahead and clear through b_bufsize.
3712 vfs_bio_clrbuf(struct buf *bp)
3717 if ((bp->b_flags & (B_VMIO | B_MALLOC)) != B_VMIO) {
3721 bp->b_flags &= ~B_INVAL;
3722 bp->b_ioflags &= ~BIO_ERROR;
3723 VM_OBJECT_LOCK(bp->b_bufobj->bo_object);
3724 if ((bp->b_npages == 1) && (bp->b_bufsize < PAGE_SIZE) &&
3725 (bp->b_offset & PAGE_MASK) == 0) {
3726 if (bp->b_pages[0] == bogus_page)
3728 mask = (1 << (bp->b_bufsize / DEV_BSIZE)) - 1;
3729 VM_OBJECT_LOCK_ASSERT(bp->b_pages[0]->object, MA_OWNED);
3730 if ((bp->b_pages[0]->valid & mask) == mask)
3732 if ((bp->b_pages[0]->valid & mask) == 0) {
3733 bzero(bp->b_data, bp->b_bufsize);
3734 bp->b_pages[0]->valid |= mask;
3738 ea = sa = bp->b_data;
3739 for(i = 0; i < bp->b_npages; i++, sa = ea) {
3740 ea = (caddr_t)trunc_page((vm_offset_t)sa + PAGE_SIZE);
3741 ea = (caddr_t)(vm_offset_t)ulmin(
3742 (u_long)(vm_offset_t)ea,
3743 (u_long)(vm_offset_t)bp->b_data + bp->b_bufsize);
3744 if (bp->b_pages[i] == bogus_page)
3746 j = ((vm_offset_t)sa & PAGE_MASK) / DEV_BSIZE;
3747 mask = ((1 << ((ea - sa) / DEV_BSIZE)) - 1) << j;
3748 VM_OBJECT_LOCK_ASSERT(bp->b_pages[i]->object, MA_OWNED);
3749 if ((bp->b_pages[i]->valid & mask) == mask)
3751 if ((bp->b_pages[i]->valid & mask) == 0)
3754 for (; sa < ea; sa += DEV_BSIZE, j++) {
3755 if ((bp->b_pages[i]->valid & (1 << j)) == 0)
3756 bzero(sa, DEV_BSIZE);
3759 bp->b_pages[i]->valid |= mask;
3762 VM_OBJECT_UNLOCK(bp->b_bufobj->bo_object);
3767 * vm_hold_load_pages and vm_hold_free_pages get pages into
3768 * a buffers address space. The pages are anonymous and are
3769 * not associated with a file object.
3772 vm_hold_load_pages(struct buf *bp, vm_offset_t from, vm_offset_t to)
3778 to = round_page(to);
3779 from = round_page(from);
3780 index = (from - trunc_page((vm_offset_t)bp->b_data)) >> PAGE_SHIFT;
3782 for (pg = from; pg < to; pg += PAGE_SIZE, index++) {
3785 * note: must allocate system pages since blocking here
3786 * could interfere with paging I/O, no matter which
3789 p = vm_page_alloc(NULL, 0, VM_ALLOC_SYSTEM | VM_ALLOC_NOOBJ |
3790 VM_ALLOC_WIRED | VM_ALLOC_COUNT((to - pg) >> PAGE_SHIFT));
3795 pmap_qenter(pg, &p, 1);
3796 bp->b_pages[index] = p;
3798 bp->b_npages = index;
3801 /* Return pages associated with this buf to the vm system */
3803 vm_hold_free_pages(struct buf *bp, int newbsize)
3807 int index, newnpages;
3809 from = round_page((vm_offset_t)bp->b_data + newbsize);
3810 newnpages = (from - trunc_page((vm_offset_t)bp->b_data)) >> PAGE_SHIFT;
3811 if (bp->b_npages > newnpages)
3812 pmap_qremove(from, bp->b_npages - newnpages);
3813 for (index = newnpages; index < bp->b_npages; index++) {
3814 p = bp->b_pages[index];
3815 bp->b_pages[index] = NULL;
3817 printf("vm_hold_free_pages: blkno: %jd, lblkno: %jd\n",
3818 (intmax_t)bp->b_blkno, (intmax_t)bp->b_lblkno);
3821 atomic_subtract_int(&cnt.v_wire_count, 1);
3823 bp->b_npages = newnpages;
3827 * Map an IO request into kernel virtual address space.
3829 * All requests are (re)mapped into kernel VA space.
3830 * Notice that we use b_bufsize for the size of the buffer
3831 * to be mapped. b_bcount might be modified by the driver.
3833 * Note that even if the caller determines that the address space should
3834 * be valid, a race or a smaller-file mapped into a larger space may
3835 * actually cause vmapbuf() to fail, so all callers of vmapbuf() MUST
3836 * check the return value.
3839 vmapbuf(struct buf *bp)
3845 if (bp->b_bufsize < 0)
3847 prot = VM_PROT_READ;
3848 if (bp->b_iocmd == BIO_READ)
3849 prot |= VM_PROT_WRITE; /* Less backwards than it looks */
3850 if ((pidx = vm_fault_quick_hold_pages(&curproc->p_vmspace->vm_map,
3851 (vm_offset_t)bp->b_data, bp->b_bufsize, prot, bp->b_pages,
3852 btoc(MAXPHYS))) < 0)
3854 pmap_qenter((vm_offset_t)bp->b_saveaddr, bp->b_pages, pidx);
3856 kva = bp->b_saveaddr;
3857 bp->b_npages = pidx;
3858 bp->b_saveaddr = bp->b_data;
3859 bp->b_data = kva + (((vm_offset_t) bp->b_data) & PAGE_MASK);
3864 * Free the io map PTEs associated with this IO operation.
3865 * We also invalidate the TLB entries and restore the original b_addr.
3868 vunmapbuf(struct buf *bp)
3872 npages = bp->b_npages;
3873 pmap_qremove(trunc_page((vm_offset_t)bp->b_data), npages);
3874 vm_page_unhold_pages(bp->b_pages, npages);
3876 bp->b_data = bp->b_saveaddr;
3880 bdone(struct buf *bp)
3884 mtxp = mtx_pool_find(mtxpool_sleep, bp);
3886 bp->b_flags |= B_DONE;
3892 bwait(struct buf *bp, u_char pri, const char *wchan)
3896 mtxp = mtx_pool_find(mtxpool_sleep, bp);
3898 while ((bp->b_flags & B_DONE) == 0)
3899 msleep(bp, mtxp, pri, wchan, 0);
3904 bufsync(struct bufobj *bo, int waitfor)
3907 return (VOP_FSYNC(bo->__bo_vnode, waitfor, curthread));
3911 bufstrategy(struct bufobj *bo, struct buf *bp)
3917 KASSERT(vp == bo->bo_private, ("Inconsistent vnode bufstrategy"));
3918 KASSERT(vp->v_type != VCHR && vp->v_type != VBLK,
3919 ("Wrong vnode in bufstrategy(bp=%p, vp=%p)", bp, vp));
3920 i = VOP_STRATEGY(vp, bp);
3921 KASSERT(i == 0, ("VOP_STRATEGY failed bp=%p vp=%p", bp, bp->b_vp));
3925 bufobj_wrefl(struct bufobj *bo)
3928 KASSERT(bo != NULL, ("NULL bo in bufobj_wref"));
3929 ASSERT_BO_LOCKED(bo);
3934 bufobj_wref(struct bufobj *bo)
3937 KASSERT(bo != NULL, ("NULL bo in bufobj_wref"));
3944 bufobj_wdrop(struct bufobj *bo)
3947 KASSERT(bo != NULL, ("NULL bo in bufobj_wdrop"));
3949 KASSERT(bo->bo_numoutput > 0, ("bufobj_wdrop non-positive count"));
3950 if ((--bo->bo_numoutput == 0) && (bo->bo_flag & BO_WWAIT)) {
3951 bo->bo_flag &= ~BO_WWAIT;
3952 wakeup(&bo->bo_numoutput);
3958 bufobj_wwait(struct bufobj *bo, int slpflag, int timeo)
3962 KASSERT(bo != NULL, ("NULL bo in bufobj_wwait"));
3963 ASSERT_BO_LOCKED(bo);
3965 while (bo->bo_numoutput) {
3966 bo->bo_flag |= BO_WWAIT;
3967 error = msleep(&bo->bo_numoutput, BO_MTX(bo),
3968 slpflag | (PRIBIO + 1), "bo_wwait", timeo);
3976 bpin(struct buf *bp)
3980 mtxp = mtx_pool_find(mtxpool_sleep, bp);
3987 bunpin(struct buf *bp)
3991 mtxp = mtx_pool_find(mtxpool_sleep, bp);
3993 if (--bp->b_pin_count == 0)
3999 bunpin_wait(struct buf *bp)
4003 mtxp = mtx_pool_find(mtxpool_sleep, bp);
4005 while (bp->b_pin_count > 0)
4006 msleep(bp, mtxp, PRIBIO, "bwunpin", 0);
4010 #include "opt_ddb.h"
4012 #include <ddb/ddb.h>
4014 /* DDB command to show buffer data */
4015 DB_SHOW_COMMAND(buffer, db_show_buffer)
4018 struct buf *bp = (struct buf *)addr;
4021 db_printf("usage: show buffer <addr>\n");
4025 db_printf("buf at %p\n", bp);
4026 db_printf("b_flags = 0x%b, b_xflags=0x%b, b_vflags=0x%b\n",
4027 (u_int)bp->b_flags, PRINT_BUF_FLAGS, (u_int)bp->b_xflags,
4028 PRINT_BUF_XFLAGS, (u_int)bp->b_vflags, PRINT_BUF_VFLAGS);
4030 "b_error = %d, b_bufsize = %ld, b_bcount = %ld, b_resid = %ld\n"
4031 "b_bufobj = (%p), b_data = %p, b_blkno = %jd, b_lblkno = %jd, "
4033 bp->b_error, bp->b_bufsize, bp->b_bcount, bp->b_resid,
4034 bp->b_bufobj, bp->b_data, (intmax_t)bp->b_blkno,
4035 (intmax_t)bp->b_lblkno, bp->b_dep.lh_first);
4038 db_printf("b_npages = %d, pages(OBJ, IDX, PA): ", bp->b_npages);
4039 for (i = 0; i < bp->b_npages; i++) {
4042 db_printf("(%p, 0x%lx, 0x%lx)", (void *)m->object,
4043 (u_long)m->pindex, (u_long)VM_PAGE_TO_PHYS(m));
4044 if ((i + 1) < bp->b_npages)
4050 BUF_LOCKPRINTINFO(bp);
4053 DB_SHOW_COMMAND(lockedbufs, lockedbufs)
4058 for (i = 0; i < nbuf; i++) {
4060 if (BUF_ISLOCKED(bp)) {
4061 db_show_buffer((uintptr_t)bp, 1, 0, NULL);
4067 DB_SHOW_COMMAND(vnodebufs, db_show_vnodebufs)
4073 db_printf("usage: show vnodebufs <addr>\n");
4076 vp = (struct vnode *)addr;
4077 db_printf("Clean buffers:\n");
4078 TAILQ_FOREACH(bp, &vp->v_bufobj.bo_clean.bv_hd, b_bobufs) {
4079 db_show_buffer((uintptr_t)bp, 1, 0, NULL);
4082 db_printf("Dirty buffers:\n");
4083 TAILQ_FOREACH(bp, &vp->v_bufobj.bo_dirty.bv_hd, b_bobufs) {
4084 db_show_buffer((uintptr_t)bp, 1, 0, NULL);
4089 DB_COMMAND(countfreebufs, db_coundfreebufs)
4092 int i, used = 0, nfree = 0;
4095 db_printf("usage: countfreebufs\n");
4099 for (i = 0; i < nbuf; i++) {
4101 if ((bp->b_vflags & BV_INFREECNT) != 0)
4107 db_printf("Counted %d free, %d used (%d tot)\n", nfree, used,
4109 db_printf("numfreebuffers is %d\n", numfreebuffers);